SCRMaps
/
Sign in

Everything is a trigger

Remastered runs no scripting language. epScript works because it turns into triggers.

8 min read

A StarCraft: Remastered map supports no runtime scripting language at all, yet epScript plainly runs during a game. The answer is simple: epScript is compiled into triggers and inserted into the map. What the game runs is always, only, triggers.

Once you know this, several things explain themselves at once: why a function call is not free, why a loop costs something, why a variable takes up memory. They are all triggers in the end.

Ordinary triggers have no arithmetic. All you get is the number modifier on an action - SetTo, Add, Subtract - plus the bitmask parameters Blizzard's Elias Bachaalany added in Remastered. Every operation epScript offers, up to square roots, is simulated out of those pieces.

So the arithmetic is not ordinary arithmetic. Add wraps to 0 past 0xFFFFFFFF. Subtract floors at 0 however much you take away - 10 minus 200000 is 0, not a negative number. If you need negatives you have to build them; they are not simply there.

var a = 0xFFFFFFFF;
DoActions(a.AddNumber(5));
println("a == {}", a);   // a == 4

var b = 10;
DoActions(b.SubtractNumber(200000));
println("b == {}", b);   // b == 0
Neither of these is a bug. This is what trigger arithmetic is.

That is the raw action, though. epScript's - operator is a different thing: since you cannot subtract, it is implemented by adding the two's complement, so the result does not floor at zero. The same "subtract" gives different answers as b.SubtractNumber(200000) and as b -= 200000.

function my_neg(x) {
    RawTrigger(actions = list(
        x.AddNumberX(0xFFFFFFFF, 0x55555555),   // 홀수 비트 반전 / flip the odd bits
        x.AddNumberX(0xFFFFFFFF, 0xAAAAAAAA),   // 짝수 비트 반전 / flip the even bits
        x.AddNumber(1),                         // +1 = 2의 보수
    ));
    return x;
}
This is how negation is actually built: two masked adds make the one's complement, then add one.

So where do those triggers go? Not into the map's TRIG section - and that is the key to the whole arrangement.

The game does not load the TRIG section as one block. It loads the triggers as a linked list and walks the nodes to find them at run time. So if you write trigger bytecode anywhere in memory you can reach and splice it into that list, it runs exactly like any other trigger. jjf28 named these Virtual Triggers.

trgk added one thing to that. The STR section is loaded as a whole, so a virtual trigger written into STR has a runtime address that is already known at compile time. Knowing the address lets triggers point at each other, and that is where branching and looping come from. eudplib is built on exactly this.

This is why a compiled EUD map opened in an editor shows an almost empty trigger list. Nothing is hidden: the code was never in TRIG. It sits in the STR section as trigger bytecode and gets linked into the list when the game starts.

Why a variable costs 72 bytesThe 222 triggers that run when the map starts