SCRMaps
/
Sign in

Four ways to read the struct wrong

The same bytes mean different things per unit kind, a flag is not a number, the padding is a free per-unit variable - and there are four bytes that do not read back at all.

12 min read

CUnit reads by member name, so it looks like an ordinary struct. One range of it is not, and a unit loop written without knowing that compiles, never crashes, and reads the wrong value forever.

The 32 bytes from 0xC0 to 0xDF are a union: the same bytes overlaid six ways, and which reading is right depends on what kind of unit it is. Nothing inside the struct says which.

All of these are 0xC0
VulturespiderMineCount - how many mines are left.
Carrier · ReaverinHangarChild - the first unit inside the hangar.
Interceptor · Scarabparent - the unit it came out of.
Buildingsaddon - the attached add-on.
Workerspowerup - whatever it is carrying.
Beaconsits own use.

So a loop over every unit that reads unit.addon reads garbage for everything that is not a building - a worker's powerup pointer, a Carrier's hangar child. Filter on the unit type, or on the IsBuilding status flag, before touching anything in this range. The compiler will not stop you: the members all hang off the same class and it has no idea which unit you are holding.

The union ends at 0xDC. statusFlags and the timers after it mean the same thing on every unit again.

The second is flags. A flag member can be one bit or several, and it does not read or write like a number.

How a flag member behaves
Reading1 only when every bit in the mask is set, otherwise 0.
Assigning 0Clears every bit in the mask.
Assigning a non-zero valueSets every bit in the mask.

movementFlags.HoverUnit is three bits (0xC1), which makes the point: there is no way to read back a partly-set state, and you cannot store a number in a flag. When you need the raw bits, use check_status_flag(value, mask) and set_status_flag(value, mask), which take a mask and compare or assign that region exactly.

The third is not an error but a missed opportunity. The struct has several holes left by alignment, and shipped maps use them as a per-unit variable - 1,700 of them, with no array and no lookup.

The holes
0x26 (1 byte)Between flingyID and flingyMovementType.
0x52 (2 bytes)Alignment padding after orderUnitType.
0x66 (2 bytes)Alignment padding after unitType - the one most often used.
0x86 · 0x8C · 0x106 (1-2 bytes)Fields nobody has named.
0x125 (1 byte)unusedTimer - possibly meant for afterburner or the Ultralisk roar.

“unknown” means unknown to whoever named it, not proven unused. Test the one you pick with the unit types you actually spawn. The names are eudplib's, too, so another eudplib may not have them.

To give a hole a readable name, you cannot assign a member onto CUnit - the class has __slots__. Subclass it in a .py file and declare Member(0x66, MemberKind.WORD).

The last one is a range that does not read at all. The four bytes from 0xE0 to 0xE3 are bonded together.

0xE0 to 0xE3
0xE0resourceType - what a worker is carrying.
0xE1wireframeRandomizer - which wireframe colour arrangement it gets.
0xE2secondaryOrderState.
0xE3recentOrderTimer - counts 15 down to 0 after an order, holding the unit before it acts on its own.

Both of these are true. These four bytes do not read back - you get 0xFF, or the last value you wrote. Sweeping all 256 values with MemoryX finds nothing. And writing any one of them resets the other three to 0xFF.

The second is the trap. Pin 0xE3 to 0 to remove the chase delay and you have also wiped that unit's cargo type, wireframe arrangement and secondary-order state. What you see is gas gathered but minerals credited, a changed wireframe, and a building that stops producing - everything with a secondary order is in range: workers, every producing building, Reaver and Carrier, Shield Battery, Hatchery, Creep Colony.

So write all four at once if you write at all. Including 0xE2 = 2 in the same masked write keeps buildings producing, at the cost of their lights never going out. Pinning 0xE3 to 0 does make chasing noticeably smoother, but the range bug fires far more often, and with an AI script running it was reported to crash eventually. The compromise reached by someone who spent weeks on it: write 15 in LoopNewUnit for workers, producing buildings and hangar units, and 0 only for the units that need the responsiveness.

Structure offsets