Skip to content

Reimplement UpdateHealer and UpdateSpearman from gynt's units branch (100%) - #545

Merged
gynt merged 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-w3-1-100
Dec 2, 2026
Merged

gynt merged 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-w3-1-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Two more Map::Units update functions, from gynt's units branch reimpl/map-units-updatefunctions (his draft #204). Both read 100.0% in the default build and with main's function-visibility option on. Each commit cites the commits of gynt's that measured the file and those that only touched its text.

UpdateSlinger, which was worked on together with these two, is in #544, the other PR of this round: it uses two enum names that are a header change, so it travels with them. Neither PR needs the other.

Function Address Before Default build Option on
Units::UpdateHealer 0x00540440 gynt's draft 57.61% (794dcd1) 100.0% 100.0%
Units::UpdateSpearman 0x0055DA50 gynt's draft 50.99% (0816e82) 100.0% 100.0%

"Before" is the status of gynt's last measured commit on each file; his later commits on them (6565c1b on UpdateSpearman, 974c32f on both) were not measured.

Measured.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed: the data resolvers with tools/reimplementation-control/enable_reimplemented_data.py, the function resolvers of the .cpp files in the build), and sourcehold's reccmp (a86aceb03) with --resolve-wrapped-calls --ignore-security-cookie. Normalised is main's reccmp_report.py figure, which ignores call and tail-jump targets and resolver addresses.
  • Small builds, both modes, of this branch as it was cut on main a0dc309: 482 files, 492 functions (every /GL file of main; the two units and six of main's, UpdatePitchman, UpdateDairyFarmer, UpdateEngineer, UpdateFireBallista, UpdateCatapult and UpdateHopsFarmer, with their callee closures; every file of a function-visibility group that touches them), BUILD_OK and 0 failures in both modes. UpdateHealer and UpdateSpearman read 100.0% / 100.0% (raw / normalised) in both, and so do the callees their entries name. With the option on, cmake/function-visibility-units.py makes 25 wrapper units, as on main; the two units, returnFalseStub and findAndDestroyAdjacentEnemyLadder are in the one that holds hasUnitReachedDestination.
  • Main has moved since those builds. The branch is now cut from d134c77, a sync of 98 header files with no .cpp in it. One of its renames reaches this PR: PlayerData::aiControlStatusRelated is aiHoldBackTicks there (same type, same offset), which UpdateSpearman reads in two lines; they are renamed in its commit. Both functions were then compiled on the tree the builds were made of and on the new head, three ways: the file alone, and with its callees pasted in front of it in the option-on layout and in the default one. In all three, each function's own section of the object file is the same, bytes and relocations, and quick_diff gives the same diff against the original in both layouts. So the builds were not repeated on the new main.
  • An earlier pair of the same builds, on main 0cea04d and with UpdateSlinger also in the tree (486 files, 496 functions), read the two functions 100.0% / 100.0% in both modes as well.
  • Against main: not like for like. There is no build of main a0dc309 on this list. Main's last builds of this kind were made for Reimplement UpdateShield, UpdateHunter, UpdateAppleFarmer, UpdateJester and UpdateSlave from gynt's units branch (100%, three with OPENSHC_FUNCTION_VISIBILITY_UNITS on) #537, before Reimplement six UnitsState functions at 100%, from gynt's text on main #539, Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541 and Reimplement UnitsState::processUnitAttackOtherUnit at 100%, from gynt's text on main #542 merged, and from a longer list (503 files, 513 functions); 478 functions are in both. Of those:
  • No full build. Every /GL file of main is in the small builds, and only those files take part in link-time code generation. So the builds hold the two new bodies and every function whose code the new bodies could shift: the other callers of the callees they call (hasUnitReachedDestination and the other members of its group, and returnFalseStub and findAndDestroyAdjacentEnemyLadder, which join that group). The files outside /GL are compiled one at a time and see none of the new bodies, and no .cpp names the two new functions.
  • Text edits: each edit made for review after the text first read 100% (casts that did nothing dropped, enum names for two numbers, one form for the case endings, comments) was compiled both ways, the file with its callees pasted in front of it, in the option-on layout and in the default one, and the two object files were compared section by section: the same object code. A form that changed the object code was not taken; those are under "Kept as they are" with what they read. The builds above are of the text with these edits; the two-line rename for the new main came after them.
  • Checks: the style checks (the maintainers' review asks and main's style_lint.py): nothing to fix automatically, no errors and 35 notes, all of the kinds under "Kept as they are" (11 casts, 22 placeholder names, one declaration without a value, one note on the mix of truth tests).
  • Not run in game.

What got them to 100%

The text started from his drafts and stood at 94.1% (UpdateHealer) and 93.6% (UpdateSpearman) on our development branches; each commit lists what changed, with the original's addresses. The last steps:

  • A store that both arms of an if share is written once, behind the if / else (UpdateHealer, twice). Behind two of its searches for a plague cloud the original computes the unit's offset once, in front of the test, and both arms store through it (0x005405D5, 0x005408EB). Written as an early return in the first arm, with the shared store repeated in each, the compiler loads the slot in front of the test and multiplies in each arm. quick_diff with the callees in the unit: 0.9472 -> 0.9611 -> 0.9782.
  • A local declared where the original computes it (UpdateHealer). The random number for the apothecary's choice is masked behind the six stores (0x005406EF) and takes the register the unit's offset had; declared at the top of the block it takes another one. 0.9782 -> 0.9860, where the lines left are the same bytes written two ways.
  • returnFalseStub (UpdateSpearman). The original calls the stub (0x00533090, xor eax, eax; ret 4) where no ladder was found and keeps the slot in edx over the call (0x0055DE0E), so it saw the stub's body and still called it. In our build a call whose result nobody reads is dropped once the body is visible, and with the body out of sight the slot is loaded again. With the result tested in an if whose arms both store the same state, the call stays and the test goes: the original's code. A comment says so. This is the form that parked the function at 93.6%.
  • The shape of a sibling that is at 100% on main (UpdateSpearman): the end of its dig state is UpdatePikeman's (the unit on the tile read once, the two closing stores on both paths), and the moat owner in its fill state goes through a local for returnOwnedMoatAtTile's result, as in UpdateSlave.
  • The wall bit tested from one local (UpdateSpearman): the wall attack reads the tile's logic entry into a local and tests logic & L_WALL_OR_GATEHOUSE at each of its four uses; a local that held the masked value put the mask in front of the building's load (0x0055E353 to 0x0055E364). 0.9581 -> 0.9668.

The entries

  • Both units join the group that holds hasUnitReachedDestination. UpdateHealer names three of its members. UpdateSpearman names three members and two /GL files from outside the group, returnFalseStub and findAndDestroyAdjacentEnemyLadder, which join it: no other .cpp calls either of them, so no other function sees a body it didn't see before or loses one.
  • The commit gives what each named callee reads when left out, and which others were tried.
  • The two units are on cmake/compiler-flags-gl.txt, as main's other units are; every callee they have to see is on that list already.

Kept as they are

Each ratio is quick_diff with the callees in the unit; the text as it is reads 0.9860 (UpdateHealer) and 0.9826 (UpdateSpearman), which are full matches in the builds.

  • The if on returnFalseStub's result with two arms that store the same state (UpdateSpearman): see above; a comment says why.
  • advanceMoatDigProgress and removeMoat compared with 1 (UpdateSpearman), as in UpdatePikeman and UpdateSlave, with a comment: a truth test reads 0.9811.
  • The dig state's two closing stores on both paths (UpdateSpearman), as in UpdatePikeman, with a comment: written once they read 0.9668 against 0.9804 at that step.
  • else if (random <= 7) (UpdateHealer, the apothecary's choice): always true, because the number was masked with 7. The original tests it (0x00540763: cmp esi, 7), and a comment says so; a plain else reads 0.9609.
  • The owner read from the unit again (UpdateSpearman). The function holds the unit's owner in owner from its first lines (0x0055DA62), and the original still reads it from the unit at five later places: three tests (0x0055DF9F, 0x0055EAC6, 0x0055EB6C), an argument of processDamageToBuildingThunk (0x0055E3D2) and the wall attack's local attacker (0x0055E48C). Through owner they read 0.9242, 0.9479, 0.9240, 0.9363 and 0.8982.
  • int damage; without a value (UpdateSpearman's wall attack), as in main's UpdatePikeman: both arms behind it assign it.
  • The casts that are left (the checker's 11 notes):
    • (char) on engineerManningSiegeStateRef_checkType (once in UpdateHealer, twice in UpdateSpearman): the field is byte in the header, and the fade tests it for below 0 and for above 0, so it has to be read signed.
    • (short) where an int is stored into a short field: the slot into the cloud's unitID_healer (UpdateHealer); ALG_MoatXResult and ALG_MoatYResult into the dig target, twice each, and DirectionAlgorithmState's orientation, twice (UpdateSpearman). ALG_MoatXResult and ALG_MoatYResult are undefined4 in the header (TileMapState.hpp); giving them a type is a header change with every user to measure, and it is not in this PR.
    • (int) on mapTimeInTicks in UpdateSpearman's % 40, as in UpdatePikeman: the field is uint, and the original divides signed (0x0055DE6F: cdq, idiv).
    • Gone since the first text, with the same object code: (char) on disappearFadeAlphaCountdown and on the frame tables (both char in the headers), (short) on BuildingLayer[tile] (short) and on both operands of the dig tile's x, and (byte) on the random number that UpdateHealer masks with 7.
  • BOOLEnum calls, IsGateOrTowerArray and animationCycleNumberHasJustIncremented (both BOOLEnum) tested by truthiness; int calls, the two-byte flags and DAT_UnitHasBecomeIdle (undefined4) compared with 0, as main does. That is the mix the checker notes in UpdateSpearman.
  • UpdateHealer's states are one else if chain whose arms fall out, as main's UpdatePitchman is written. UpdateSpearman's cases all end in return;.
  • Placeholder names (the checker's 22 notes) are Ghidra's, for which we have nothing better yet: the frame tables of UnitPropertiesDefinedData that the two functions read (field_0x14, field_0x66c, field_0x816c, field_0x81bc, field_0x8208 and twelve from field_0x1c04 to field_0x1e48), Unit::field_0x3f4 (a short; UpdateHealer tests it for more than 1 in its first state) and Unit::field_0x2ac (a short; UpdateSpearman stores 10 into it in two states).

Differences from the draft

We compared the operators and literals of every test against a number with gynt's drafts. None differs in what it tests. They are the same tests written the other way round with their arms exchanged (UpdateHealer: <= 300 as > 300; UpdateSpearman: <= 70 as > 70 twice, <= 96 as > 96, != 1015 as == 1015, >= 0 as < 0, != 1 as == 1), BOOLEnum calls and fields tested by truthiness where the drafts compare with 0, and hex constants in decimal or as their enum names. We did not read the two files line by line against the drafts.

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 3 commits December 2, 2026 16:43
Units::UpdateHealer at 100%, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on the
file: e368a00 at 47.79%, 2d6e422 at 57.41%, 794dcd1 at 57.61%, and the style commit 974c32f), through the
forms on our development branches (94.1% there).

What the match needed, against that text:

- Behind the two searches for a plague cloud that have an else path, the original computes the unit's offset once, in
  front of the test (0x005405D5 and 0x005408EB: mov ecx, [slot]; imul ecx, ecx, 0x490; cmp eax, ebx), and both arms
  store through ecx. That is an if / else with the store both arms share written once behind it: animationCycleNumber
  at the first (0x00540611 and 0x0054064C are the two copies the compiler made of it), destinationNeeded at the second.
  The text had an early return in the first arm and the shared store in each arm, with a local copy of the slot at the
  first site; that loads the slot in front of the test and multiplies in each arm, and it changes the order of the
  stores. quick_diff with the group's callees in the unit: 0.9472 -> 0.9611 with the second site, 0.9782 with both.
- The random number for the apothecary's choice is declared where the original masks it, behind the six stores
  (and edx, 7 at 0x005406EF, kept in esi over the call): at the top of the block it takes ebp. 0.9782 -> 0.9860, and
  what is left there is the same bytes written two ways (0xd8f0 and -0x2710, 0xff and -1).
- The last exit stores the cloud's velocity in front of destinationNeeded, as the original does (0x00540CC6,
  0x00540CCF), and the return that closed that arm is gone: with the two sites above written as they are, the arm
  falls out of the chain like the others (the same object code with and without it).

Number casts to UnitState and the two numbers stored into cachedState are the enum's names, calls that return BOOLEnum
are tested by truthiness, and the cloud's frame value 0x3f8 is decimal. disappearFadeAlphaCountdown and the frame tables
are char and the random number is masked with 7, so the `(char)` and `(byte)` casts on them are gone. None of these
changes the object code. `else if (random <= 7)` is always true behind `& 7`: the original tests it (0x00540763), a
comment says so, and a plain else reads 0.9609.

compiler-flags-gl.txt gets UpdateHealer.cpp only: the callees it has to see are on main's list already (quick_diff
0.6464 alone; without hasUnitReachedDestination 0.6867, checkIfCitizenUnitIsAliveBasedOnState 0.8556,
hasTunnelerNotFinishedDigging 0.9633).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Units::UpdateSpearman at 100%, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on the
file: be18ccc at 38.12%, 0816e82 at 50.99%, 6565c1b, and the style commit 974c32f), through the forms on
our development branches (93.6% there).

What the match needed, against that text:

- The call of UnitsState::returnFalseStub (0x00533090, `xor eax, eax; ret 4`, 100% on main). The original calls it
  where no ladder was found and keeps the slot in edx over the call (0x0055DE0E: push edx; mov ecx, <units>; call;
  imul edx, edx, 0x490), so it knew which registers the stub writes, and it did not drop the call. With the stub's
  body in the unit a call whose result nobody reads is dropped (as a plain function and with __declspec(noinline)
  alike), and with the body out of sight the slot is loaded again behind it. Tested in an if whose arms
  both store US_STATE_2Unk, the call stays, the test goes, and the code is the original's. A one-line comment says so.
- The wall attack reads the tile's logic entry once into a local and tests the wall bit at each of its four uses
  (`logic & L_WALL_OR_GATEHOUSE`), in place of a local that held the masked value: the original loads the entry
  into edx, the building into esi, and only then masks into edi (0x0055E353 to 0x0055E364). 0.9581 -> 0.9668 in
  quick_diff with the callees named below in the unit.
- The dig state ends as main's UpdatePikeman's does: the unit on the tile as `int const occupant`, and the two closing
  stores written in the arm for a finished tile and behind the block (once: 0.9668 against 0.9804).
- The fill state's moat owner goes through a local for returnOwnedMoatAtTile's result, as in main's UpdateSlave
  (0.9804 -> 0.9826, where what is left is the same bytes written two ways: 0xffff and -1, `call esi` for the
  import, `test dl, bl`).
- The warning's file name is main's s_General_Warning11_wav_005abe28 (the original pushes 0x005ABE28).

Number casts to UnitState are the enum's names, the two moat bits and the wall bit are LogicHelpers' names, calls that
return BOOLEnum and IsGateOrTowerArray are tested by truthiness, the damage of a hit on a building is an if / else (it
was a ternary), every case ends in `return;`, and the casts that did nothing are gone (`(char)` on
disappearFadeAlphaCountdown and the frame tables, `(short)` on BuildingLayer and on both operands of the dig tile's x):
none of these changes the object code. advanceMoatDigProgress and removeMoat stay compared with 1, as in UpdatePikeman:
a truth test reads 0.9811.

The owner is read from the unit again where the original reads it again: in the test at 0x0055DF9F, as an argument
of processDamageToBuildingThunk (0x0055E3D2), into the wall attack's `attacker` (0x0055E48C), and in the two tests at
0x0055EAC6 and 0x0055EB6C. Through the function's `owner` these read 0.9242, 0.9363, 0.8982, 0.9479 and 0.9240.

The player's field at 0x2274 is written aiHoldBackTicks, the name main's PlayerData.hpp has for it since the header
sync d134c77 (it was aiControlStatusRelated; same type, same offset): two lines, the same object code.

compiler-flags-gl.txt gets UpdateSpearman.cpp only: xyAreValid, returnFalseStub, findAndDestroyAdjacentEnemyLadder,
hasUnitReachedDestination and changeDestinationByAmount are on main's list already (quick_diff 0.5408 alone).

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

Two new entries, so that the two units read with OPENSHC_FUNCTION_VISIBILITY_UNITS on what they read in the default
build. Both join the group that holds hasUnitReachedDestination.

UpdateHealer names three callees, all members of that group already. UpdateSpearman names three members (xyAreValid,
hasUnitReachedDestination, changeDestinationByAmount) and two /GL files from outside it, returnFalseStub and
findAndDestroyAdjacentEnemyLadder, which join the group with it: no other .cpp calls either, so no other function
sees a body it didn't see before or loses one.

Each caller-callee pair is measured as sourcehold#491's were (quick_diff, the unit's file with the callee files pasted in front
of it, against the unit alone). The callees tried are the group's members that the unit calls directly or through
another member it calls; one is named when leaving it out lowers the ratio, and the named set reads what the whole
set reads:

- UpdateHealer 0.6464 alone, 0.9860 with its three: without hasUnitReachedDestination 0.6867,
  checkIfCitizenUnitIsAliveBasedOnState 0.8556, hasTunnelerNotFinishedDigging 0.9633. Not named (no change):
  commitUnitLocation, setupUnitSharingCurrentTilePosition, setupUnitSharingTileIDs,
  makeUnitStopWalkingByClearingPathProgressState.
- UpdateSpearman 0.5408 alone, 0.9826 with its five: without hasUnitReachedDestination 0.5580, xyAreValid 0.9370,
  changeDestinationByAmount 0.9614 (each measured on the three members alone, which read 0.9637); with
  findAndDestroyAdjacentEnemyLadder on top of the three 0.9725, with returnFalseStub 0.9737. The original keeps the
  unit's offset in esi over the first (0x0055DF59, stores through esi at 0x0055DF63) and the slot in edx over the
  second (0x0055DE14, used at 0x0055DE19).

On top of the named callees, each other /GL callee the unit calls was tried one at a time, without its own callees:
none changes a ratio. What 0.9860 and 0.9826 leave are lines quick_diff writes two ways (0xffff and -1, an import
called through a register); the builds in the pull request read the two units 100.0%.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@gynt
gynt added this pull request to the merge queue Dec 2, 2026
Merged via the queue into sourcehold:main with commit dcb430e Dec 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants