Skip to content

One more Map::Units function at 100%: UpdateAssassin, with Unit::field_0x41a as a short - #450

Open
kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-assassin-100
Open

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-assassin-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

One more function at 100%, with one header change in a commit of its own: Unit::field_0x41a becomes a short. Units::UpdateAssassin is from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and its commit credits him as co-author. It shares UpdateKnight's wall attack (#444).

Function Address Before After
Units::UpdateAssassin 0x005744D0 gynt's draft 44.69% (7e9039d; 39.56% at 982d7f0) 100.0%, under /GL

Measured.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie.
  • Full build: an all-on build of the previous cut of this branch on main 3458fea (the same compiled code: the last cut only changes how three branches are spelled and adds comments, and a build of it reads the same), the header commit included, with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out and its resolver off, as in main's baseline: 0 failed of 2,424 compile steps, 99.57 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). UpdateAssassin reads 100.0 raw and normalised there too, with no diff entry. No .cpp on main uses Unit::field_0x41a, and the header change moves no function. Two functions on main read lower, and it is how reccmp prints a constant, not their code: Menu::handleMenuItems (0x004F6280) reads 97.9 (91.4 normalised) and Menu::Constructor_Menu (0x004F4100) reads 98.4 (95.2), both 100.0 before. Their only differing lines are five compares with the constant 0x11000000, which reccmp prints on our side as the name of a symbol of our DLL that lies at exactly that address in this build's layout; UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435 and UI: the credits scroll's fade and the win/defeat window at 100% #437 describe the same two readings. Unrounded the average is 99.5746 against 99.5759 for main; with those two at 100.0 it would be 99.5761. Every other main function reads exactly what it read before.
  • Our own narrower run: the 133 files of main's /GL list that exist on main, and UpdateAssassin.cpp, with the header commit: 144 functions, average 99.56. UpdateAssassin reads 100.0% raw and normalised, with no diff entries. The same build of main without the header commit and the file has 143 functions, and each of them reads the same in both.
  • Please measure with both resolver switches on (the Globals and the function resolvers), as our numbers are.
  • Checks: openshc-pr-fix finds nothing to change, and openshc-pr-check gives 0 ERROR and 79 CHECK (explained below).
  • Not run in game.

The header commit: Unit::field_0x41a is a short

It was undefined2. UpdateAssassin compares it signed in two places: cmp ax, bx; jle at 0x00574F12 and cmp cx, bx; jle at 0x005750C2 (ebx holds 0), each followed by add …, -2 and the store back. With the field as short the two (short) casts our dev version needed are gone. The only other code that touches it is 0x004E2630 (movsx at 0x004E2691), not on main; gynt's #241 has it as renderAssassinClimbingOverlay and makes the same change to this line, so the two hunks merge cleanly. No other function on main names the field, so the commit cannot change any other function; in our narrower run each of main's 143 functions reads the same with and without it.

The declaration check will fail and label this PR until main has this line. Then we merge main in, and the check passes.

What got it to 100%

Every number here is with the file on the /GL list (below). On our dev branch (not public) it went from 82.5% to 97.0%, mostly with the forms UpdateKnight has: the unit read through DAT_CurrentUnitSlotID::instance with no local copy of the slot, and the attacked unit's ID and type each read once into a local for the clash sound. Four more things were needed; the third is the same as in the melee units next to it.

  • building is declared with its value at the wall attack's tile check (int building = BuildingLayer[…]), in front of the idle test, so no path reads it unassigned. The other melee units keep 4 bytes of frame for building; the assassin's original has none, and ours had them until the variable got its value there (91.2% to 97.0%, together with the next point).
  • The fade countdown is decremented as a signed char (--(char&)…disappearFadeAlphaCountdown): in that form the compiler keeps -1 in a register across the decrement, as the original does (as x--; quick_diff reads 0.9872, 1,057 instructions, against 0.9924). A comment in the code says so.
  • The wall attack's second give-up place is a then-arm. The wall attack gives up in three places: where the building is destroyed and moveUnitToBehaviorTarget fails, where an AI player gives up on a wall, and where it gives up on a gate or tower. In the original the first and the third are each a state store with a copy of the case's end behind it, and the second is a jmp into the first one's block. With the first two written out (state = …; animationCycleNumber = 0; return;) the shared block computes the unit's offset once, 9 bytes short. With the first only setting the state and the second as if (!setDestinationNearTargetedBuilding(…)) { state = …; } else { return; } the compiler keeps a block with its own copy of the end at the first place and jumps into it from the second (quick_diff with the /GL callees in the unit 0.9886 to 0.9915). A comment in the code says why.
  • The idle state's end in its plain form. Digging or filling a moat makes the unit idle: if (dig or fill) { idle = 1; } if (idle == 0) { return; } animationCycleNumber = 0;. The original sets the flag behind the two compares and jumps over the flag test (cmp ax, 6; je; cmp ax, 7; jne; mov [idle], ecx; jmp from 0x00575506). Our dev version had an if / else-if / else chain there, left by an earlier search, which laid the store out behind the test; with the return in an else-if the function is four instructions longer (0.9896 against 0.9943).
  • quick_diff's last 0.0076 are spellings of the same bytes: 0xffff against -0x1 twice, the import of timeGetTime, one mask, and the tile-cost table's address in two operands.

Lines added to cmake/compiler-flags-gl.txt

  • UpdateAssassin.cpp. No callee needs a line: hasUnitReachedDestination and resumeMovementIfNoAttackTarget, which it calls, are on main's list already, and with main's list as it is the function compiles to the same code as with every finished callee of our dev list visible.
  • What the line does to other functions: nothing that our narrower run can see (above).

Kept as they are

  • int damage; without a value (the declared without a value CHECK): each arm of the wall test assigns it.
  • 21 (char) casts: 14 frame-table reads into animationFrame, four tests of disappearFadeAlphaCountdown, two of engineerManningSiegeStateRef_checkType and one of ownerOfAssassinScaledObject. The headers have these as byte, and the original reads them signed (see the proposal below). The --(char&) decrement belongs with them.
  • (int)(mapTimeInTicks + slot) % 40: the original divides signed (cdq at 0x00574678, idiv at 0x0057467E).
  • units[slot].owner read again in four places though owner is a local: the argument of processDamageToBuildingThunk, attacker, and twice in the wall-scaled warning. The original reads the field again there (movsx at 0x00574AD8, 0x00574B8C, 0x00574DC0 and 0x00574E0B), though ebp still holds it.
  • Three number casts to UnitState: 105 twice and 104 once. 105 is US_RALLYING in gynt's Reimplement 17 navigation and map-property functions #293, and those two casts go when that lands; 104 has no name in any open PR. State 108 uses main's US_UNKNOWN_108.
  • The rope's picture as an if chain, twice (the two similar blocks CHECKs): the thresholds 70, 100, 130, 160, 190 and 210 pick imageIDUnk 7 down to 2, else 1. The original has the chain of compares; as a for loop over the thresholds the function comes out 139 instructions shorter (quick_diff 0.8862 against 0.9924), which the comment in the code says in short.
  • The header CHECK only says to keep the header commit in the PR, which it is.
  • Placeholder names (50 CHECKs, 39 of them the three Unit fields of the next section): below.

What some placeholders hold

  • Unit::field_0x413: 1 while the assassin is on the rope (throwing the hook, climbing up, climbing down), 0 again when the climb ends.
  • Unit::field_0x418: how far up the rope the unit is. Climbing up it counts up with the animation until it reaches assassinHeightDifference - 35; climbing down it starts at assassinHeightDifference and falls by 4 per update until it is 30 or less; US_DEATH_01 takes 8 off per update, US_DEATH_03 and the stone deaths set it to 0, and starting to climb down leaves it alone; everywhere else it is 0.
  • Unit::field_0x41a: set to 32 when a climb starts and when it ends, and taken down by 2 per update while it is above 0 in US_ASSASSIN_CLIMBING_UP (0x00574F0B) and US_ASSASSIN_CLIMBING_DOWN (0x005750BB). 0x004E2630 reads it for the climbing overlay. This is the field the header commit types.
  • Unit::field_0x2ac: 10 while the unit stands idle, as in the other melee units.
  • DWORD_00ee1064: the timeGetTime() of the last wall-scaled warning ("General_Warning18"), which it limits to one per 30 s. Only this function uses it (0x00574DE6, 0x00574E3C).
  • Frame tables in Frames_Shared_UnitClimbingUp: .field_0x2c94 is the table of melee and of the wall attack; .field_0x2dac, .field_0x2df4 and .field_0x2e4c are the three tables of throwing the hook; .field_0x2ed8 is climbing up and .field_0x2eb0 starting to climb down; .field_0x93c is the table of US_DEATH_01 and US_DEATH_03, .field_0x8e4 that of the three stone deaths (114 to 116 share one case).

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 21, 2026 13:44
It was `undefined2`. Units::UpdateAssassin (0x005744D0) compares it signed in two places, `cmp ax, bx; jle` at
0x00574F12 and `cmp cx, bx; jle` at 0x005750C2 (ebx is 0), each followed by `add ..., -2` and the store back.
No other function on main names the field. The only other code that touches it is 0x004E2630 (`movsx` at
0x004E2691), not on main; gynt's sourcehold#241 has it as renderAssassinClimbingOverlay and makes the same change to this
line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateAssassin, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (7e9039d at 44.69%,
after 7ffb455 at 44.69% and 982d7f0 at 39.56%), through versions on our dev branch from 82.5% to 97.0%. The
forms that matter:
- the melee and wall-attack states have UpdateKnight's shapes: the unit is read through the slot global with no
  local copy of the unit ID, and the attacked unit's ID is read once into a local in front of
  playHurtSFXForUnit, its type once into an int behind it;
- `building` is declared with its value at the wall attack's tile check (`int building = BuildingLayer[...]`), so
  no path reads it unassigned; without that assignment the function gets 4 bytes of frame the original does not
  have;
- the fade countdown is decremented as a signed char (`--(char&)...`);
- of the wall attack's three give-up places the first and the third only set the state and fall to the case's
  end, and the second, the AI's give-up on a wall, is the then-arm of its test with the return in the else-arm.
  The compiler keeps a block with its own copy of the case's end at the first place and jumps into it from the
  second, as the original does (9 bytes);
- the idle state's end in its plain form: `if (dig or fill moat) { idle = 1; } if (idle == 0) { return; }
  animationCycleNumber = 0;`. The original sets the idle flag behind the two compares and jumps over the flag test
  (`cmp ax, 6; je; cmp ax, 7; jne; mov [idle], ecx; jmp` from 0x00575506); with the return in an else-if the
  function is four instructions longer;
- field_0x41a is compared as the short it now is in Unit.hpp (the commit before this one), without a cast.
The rope chain ends in an else-if / else, no else stands behind a return, and two comments give the why of the
then-arm and of the signed decrement; the code is the same (quick_diff, 1058 instructions, line for line).
Numbers and pointer arithmetic that have names in main's headers are written with those names, as in
UpdateKnight. The states 104 and 105 have no name in UnitState yet and keep their casts.

compiler-flags-gl.txt gets UpdateAssassin.cpp. No other line is needed: of its callees, hasUnitReachedDestination
and resumeMovementIfNoAttackTarget are on main's list already, and the function reads 100.0% with the list as it
is.

reccmp in a build of main's whole /GL list (the 133 files that exist) and this file, with the header commit and
every resolver of those files on: UpdateAssassin 100.0% raw and normalised, no differing line. Each of the 143
functions of main's list reads the same as in the same build of main (3458fea) without the header commit and
the file.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

declaration/type change This requires changes to the generated/exported types or definitions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant