Skip to content

One more Map::Units function at 100%: UpdateKnight - #444

Merged
gynt merged 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-knight-100
Nov 24, 2026
Merged

gynt merged 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-knight-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateKnight is from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and the commit credits him as co-author.

Function Address Before After
Units::UpdateKnight 0x00562AA0 gynt's draft 55.10% (bb5f008; 49.02% at 968bbc1) 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, every struct resolver and every function resolver on: 0 failed of 2,424 compile steps, 99.58 (99.5761) over 2,440 functions, against 99.58 (99.5759) over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). UpdateKnight reads 100.0% raw and normalised there, with no diff entry, and each of main's 2,439 functions reads exactly what it read in main's own build.
  • That build had the file before its last pass, which changed spelling only: names from main's headers in place of numbers and pointer arithmetic, ++, const, truthiness, an if / else for a ternary, two casts dropped and two comments reworded. The function's code is the same before and after: both files compile to the same 826 instructions, line for line. The one thing quick_diff prints differently is the address of the tile-cost table in two operands: 0x01EE2998 as a number before, DAT_TileMapState + 0x44F790 now, the same address (0.9964 becomes 0.9939 for that spelling). So the full build's result stands for this head.
  • Our own narrower run, with the final file: the 133 files of main's /GL list that exist on main, and UpdateKnight.cpp: 144 functions, average 99.56. UpdateKnight reads 100.0% raw and normalised, with no diff entries. The same build without the file has 143 functions, and each of them reads the same in both.
  • With Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 in the tree: a local merge of main with Reimplement 47 UnitsState functions at 100% and restyle 28 more #378, its own /GL list and UpdateKnight.cpp: 294 functions, average 99.60. UpdateKnight reads 100.0% raw and normalised again, with no diff entries, and each of the 293 other functions reads the same as in that build without the file.
  • 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 33 CHECK (explained below).
  • Not run in game.

What got it to 100%

The numbers in this paragraph are reccmp raw / normalised with the file on the /GL list (below). On our dev branch (not public), our first version read 84.6 / 78.2. With the first three points below, and one use of a local copy of the slot still left, it read 96.6 / 86.0. The last two points made it 100.0 / 100.0.

  • Every access reads the unit through DAT_CurrentUnitSlotID::instance. There is no local copy of the slot any more.

  • The clash sound reads the attacked unit's type once into an int, behind the frame test. The original compares it as an int (movsx, then cmp ecx, 24 and so on), not as a short.

  • The wall attack in the original's shape. The "building destroyed" case and the AI's give-up case are an if / else with the frame reset behind both; the AI's danger and tolerance are read in each of the two tests; the wall case comes first.

  • building is one of the function's locals and is set to 0 by a statement in front of the switch. The original reserves 4 bytes of frame (push ecx, a pop ecx at each of its 18 exits) and never reads or writes them, and for a wall it sets building from the zero register (mov esi, ebp at 0x00562FA1). Both come from this one statement: the variable is live across every call, so it gets a home in the frame, and the compiler then takes its value from the zero register where it is read and drops the store. Other places for the same zero, in quick_diff (the /GL callees in the unit):

    Form Frame Wall arm quick_diff
    building = 0; as a statement between the top's first if and the switch (this PR) 4 bytes, untouched mov esi, ebp 0.9927
    int building = 0; on the declaration at the top 4 bytes, untouched mov edi, ebp: isWall and building in each other's registers 0.9806
    building = 0; in the wall arm, and no local copy of the slot none xor esi, esi 0.9798
    int building = 0; at the top of the case none nothing 0.9687

    (0.9927 is with the next point still open.)

  • Behind a failed moveUnitToBehaviorTarget the if only stores the state and falls through to the case's last statement. The original computes the unit's offset twice from one load there (mov edx, [slot]; mov eax, edx; imul eax, eax, 1168; imul edx, edx, 1168 at 0x0056305E), once for the state store and once for animationCycleNumber = 0: the second half is the case's shared end (animationCycleNumber = 0; break;), which the compiler copied behind the if's body. With animationCycleNumber = 0; return; repeated inside the if, as before, the offset is computed once (0.9927 against 0.9964).

  • The quick_diff numbers are from before the last pass. Its last 0.0036 were three spellings of the same bytes (0xffff against -0x1 twice, the operand order of one test).

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

  • UpdateKnight.cpp. The one callee it has to see is on main's list already: hasUnitReachedDestination. quick_diff reads 0.9964 with it visible and 0.5043 without it, the same as with the file off the list. Its callee resumeMovementIfNoAttackTarget is on the list too and makes no difference (0.9964 without it).
  • What the line does to other functions: nothing that our narrower run can see (above).

Kept as they are

  • int building; without a value, and its one-line why in the code (one of the two declared without a value CHECKs): the table above. The other is int damage;, which each arm of the wall test assigns.
  • Seventeen (char) casts: the eleven frame-table reads into animationFrame, four tests of disappearFadeAlphaCountdown and two of engineerManningSiegeStateRef_checkType. The headers have these as byte, and the original reads them signed (see the proposal below).
  • (int)(mapTimeInTicks + slot) % 40: the original divides signed (cdq, idiv at 0x00562C33).
  • Two (UnitState)105 (gathering at the rally point): 105 has no name in main's enum; gynt's Reimplement 16 navigation and map-property functions #293 names it US_RALLYING. States 3 and 108 use main's US_STATE_3Unk and US_UNKNOWN_108.
  • units[slot].owner read again for the damage call's argument and for attacker, where a local owner exists: the original reads it again there too (movsx at 0x00562FDE and 0x00563093).
  • Placeholder names (eleven CHECKs): below.

What some placeholders hold

The knight is drawn in two parts, the horse through imageIDUnk and the rider through gfxNumber, each with its own frame table per state.

  • Frames_Shared_UnitClimbingUp.field_0x2264 and .field_0x2290: the horse's frames in melee, for odd and for even slots; .field_0x2290 is also the table of the wall attack. .field_0x22dc: the rider's frames in melee.
  • .field_0x2210 and .field_0x223c: horse and rider while cheering (state 3).
  • .field_0x2104 and .field_0x2084: the horse standing idle, for odd and for even slots; .field_0x2184: the rider's idle frames.
  • Unit::field_0x2ac: 10 while the knight cheers or stands idle. Nothing more is known from this function.

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

Units::UpdateKnight, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (974c32f, after bb5f008
at 55.10%), through versions on our dev branch at 84.6% and 96.6%. Kept forms, each with quick_diff of the form
without it (the /GL callees in the unit; measured before the file's last pass over its spelling, when the final
form read 0.9964, the rest being quick_diff's spelling of three instructions):
- the clash sound reads the attacked unit's type once into an int, behind the frame test: the original compares it
  as an int (as a short 0.8985 against 0.9094 at that step);
- every unit access goes through the slot global, and there is no local copy of the unit ID;
- `building` is one of the function's locals and is set to 0 by a statement in front of the switch. This is what
  gives the original's frame, 4 bytes that are reserved (push ecx, a pop ecx at each of the 18 exits) and never
  touched, and its `mov esi, ebp` for a wall: the variable is live across the calls and gets the slot, and the
  compiler takes its value from the zero register and drops the store. With `building = 0;` in the wall arm there
  is no frame (0.9798 against 0.9927); as an initialiser on the declaration `isWall` and `building` come out in
  each other's registers (0.9806);
- the wall attack in the shape of the original: the destroyed and the AI cases are an if / else with the frame
  reset behind both, the AI's danger and tolerance are read in each test, and the wall case comes first;
- in the destroyed case the if behind moveUnitToBehaviorTarget only sets the state and falls through to the case's
  last statement. The original has that statement's code a second time behind the state store, with the unit's
  offset computed again; with `animationCycleNumber = 0; return;` repeated in the if the offset is computed once
  (0.9927 against 0.9964).

Numbers and pointer arithmetic that have names in main's headers are written with those names. The two tests in
which an AI player gives up on a wall, gate or tower read TileMapState::SEC_PathfindingCostTileMap1105 and
attackInfo.playerInfo[attacker - 1], as recomputeAttackAIZone does. The states 3 and 108, the wall bit of the
logic layer, the hit sound's material and the knight's body graphic use their enum values. State 105 has no name
in UnitState yet and keeps its cast.

compiler-flags-gl.txt gets UpdateKnight.cpp. The callee it needs visible is on the list already:
hasUnitReachedDestination (quick_diff without it 0.5043, the same as with no callee bodies at all, i.e. off the
list). resumeMovementIfNoAttackTarget, which it also calls and which is on the list, makes no difference to it.

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

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
gynt
gynt previously approved these changes Nov 24, 2026
@gynt
gynt merged commit 3e32620 into sourcehold:main Nov 24, 2026
4 checks passed
gynt added a commit that referenced this pull request Nov 24, 2026
Units::UpdateFireBallista, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (b66f4b3 at
51.30%, after c9a76ac at 47.90%), through versions on our dev branch from 83.3% to 92.6%. The forms that
matter, beyond the siege engine shapes it shares with UpdateBallista:
- the waiting state has one `int i` for its four loops. The original reads the crew search's index after the
  loop: createTribe gets units[i].owner with i where the search ended (`imul ebx, ebx, 1168` on the search's
  counter at 0x00578339), not the ballista's owner. With the shared index the link-time code generation also gives
  the crew's stores the original's registers (0x6d and 5 in ebx and edi); with an index per loop it swaps them;
- the two returning engineers are set out in a loop of two passes, which the compiler unrolls, as in
  UpdateBallista, and in the loop that checks them each reason to stop is an `if (...) { break; }` of its own. As
  one `||` condition the zero for the stores behind the loop is set behind it; the original keeps it in edx from
  in front of the loop (0x00577FC1);
- the search for another player's crew returns straight from the end of its loop:
  `for (crewOwner = 1;; ++crewOwner) { if (crewOwner >= 9) return; if (crewCount[crewOwner] == 2) break; }`;
- the moving state stores animationSheetFrameOffset at the start of both arms of
  `if (tracker == 0) { ... } else { if (tracker > 70) ... }`, which the compiler hoists to between the compare and
  its jump (0x00578BCB), and reads the unit through the slot global, with no function-wide copy of the unit ID
  (state 9 keeps its own, which the original holds in edi across its two calls);
- the distances to an engineer are taken with the engineer's coordinate first, in the crew search the y distance
  through two locals.
Each of these has a one-line comment in the code. The capture path keeps two calls as the original makes them,
each with a fixme: createTribe gets the owner of units[i] (i is maxUnitCount there), and addUnitToTribe gets the
tribe first and the unit second (0x00578359 to 0x00578360; 0x00522590 indexes the units with its first argument).
Main's style-lint flags every ternary on one line, so the six of the return check and the crew search are if /
else; the code is the same. Numbers and pointer arithmetic that have names in main's headers are written with
those names, as in UpdateKnight (#444).

compiler-flags-gl.txt gets UpdateFireBallista.cpp and UnitsState/deselectUnit.cpp, which is at 100% on main and was
on no /GL list. The captured ballista is deselected, and the compiler has to see deselectUnit's body: behind the
call at 0x005782F2 the original pushes edx (the slot, loaded at 0x005782E6) again at 0x00578300 and calls
clearOrDeselectUnitFromSelection at 0x00578302 without loading ecx again. quick_diff with the /GL callees in the
unit reads 0.9874 with it and 0.8796 without it. hasUnitReachedDestination, the other callee it has to see, is on
main's list already (0.5758 without it).

reccmp in a build of main's whole /GL list (the 133 files that exist) and the two files, with every resolver of
those files on: 145 functions, average 99.57. UpdateFireBallista 100.0% raw and normalised, no differing line;
deselectUnit 100.0%. In the same narrow build of main (3458fea) without the two lines (143 functions),
UpdateFireBallistaBuilding reads 99.6% (normalised 95.5), and 100.0% with them; main's own full build reads it at
100.0% already, so that is a reading of the narrow build. Each of the other 142 functions reads the same.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 29, 2026
…gynt's units branch, under /GL (100%) (sourcehold#512)

* reimplement: SHC_3BB0A8C1_0x00566750 100.0% Reimplemented

Units::UpdatePriest, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (b389b78 at 63.25%,
after 525a6eb at 57.72%), through our dev branch (99.9% on main before sourcehold#378). Against the dev branch's file:
- the walk to the unit to bless reads the unit through the slot global from its first line, call arguments
  included, with no assignment to `unitID` in it. changeDestinationByAmount, which sourcehold#378 brought to main's /GL
  list, is then visible to the compiler and leaves the slot alone;
- `animationSheetFrameOffset = 1` stands in front of `field_0x30_animRelated = 16` in both arms of that walk;
- `destinationNeeded` at the end of the preaching state goes through the global.
The rest is the dev branch's: `stateBasedSpeed = 0` written in front of the priest count, the campfire and inn
walks through the global, and one call argument through the global for the registers at the top.
The states 2, 3, 9, 11, 108 and 121 use UnitState's names, the fade's direction is written -2 as in main's
UpdateMerchant, and nothing is cast to short on its way into a short field.

compiler-flags-gl.txt gets UpdatePriest.cpp. IncrementAndOptionalUpdateAVValueRelated.cpp, a callee the unit has to
see, is on main's list already (sourcehold#416).

Real builds on main 655adce, every listed file's function resolver and all data resolvers on. A small one: main's
/GL list plus this branch's four lines (main's UpdateGateHouseLarge and UpdateGateHouseSmall left out: they don't
compile on 655adce), with an earlier spelling of UpdatePikeman. A full one: every file at this branch's head
against main alone (main's 12 files that don't compile on 655adce left out). UpdatePriest reads 100.0% raw and
normalised in both, with no differing line in the small one, and nothing on main reads lower in the full one.

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

* reimplement: SHC_3BB0A8C1_0x00541A30 100.0% Reimplemented

Units::UpdateMerchantHorse, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (4b7d935 at
46.84%, after e103100 at 41.35%), through our dev branch (99.1% on main before sourcehold#378). Against the dev branch's
file, one store: `updateTickTracker = 0`, the last store in front of resetUnitMovementState, goes through the slot
global. With commitUnitLocation's callees visible (setupUnitSharingTileIDs, which sourcehold#378 brought to main's /GL list,
and setupUnitSharingCurrentTilePosition), the compiler then sets up resetUnitMovementState's argument above the
last store only, as the original does. The rest is the dev branch's, with one call argument through the global for
the registers at the top.
The states 2 and 3 use UnitState's names, also where they are stored into cachedState, and the fade's direction is
written -2 as in main's UpdateMerchant.

compiler-flags-gl.txt gets UpdateMerchantHorse.cpp.

Real builds on main 655adce, every listed file's function resolver and all data resolvers on. A small one: main's
/GL list plus this branch's four lines (main's UpdateGateHouseLarge and UpdateGateHouseSmall left out: they don't
compile on 655adce), with an earlier spelling of UpdatePikeman. A full one: every file at this branch's head
against main alone (main's 12 files that don't compile on 655adce left out). UpdateMerchantHorse reads 100.0% raw
and normalised in both, with no differing line in the small one, and nothing on main reads lower in the full one.

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

* reimplement: SHC_3BB0A8C1_0x0055F110 100.0% Reimplemented

Units::UpdatePikeman, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (4630f86 at 41.54%),
through our dev branches (99.9% on main before sourcehold#378). The forms that matter:
- `int building = 0;` with the function's locals and no local copy of the unit ID, as in UpdateKnight (sourcehold#444): the
  original's 4 bytes of frame that are never touched and its zero register for a wall;
- the dig-moat block behind setDestinationForUnit reads the unit through the slot global, call argument included.
  With changeDestinationByAmount's body visible (sourcehold#378 brought it to main's /GL list) the compiler knows the call
  leaves the slot alone and stores the dig tile's X and Y in front of the pops, as the original does (0x0055F2AB,
  0x0055F2B2).
The states 2, 3 and 108 use UnitState's names; 105 and 124 have none on main and keep their casts. State 0's
choice between 2 and IDLE, the thrust picture's and the damage are if / else (main's style-lint flags a ternary).
The AI's wall-danger test reads `attackInfo.playerInfo[attacker - 1]` (currentPlayerID_OR_attackedPlayerID_OR_one
and someMinimumDistance) and `SEC_PathfindingCostTileMap1105`, where main's header now has these blocks (they were
raw offsets before); the LogicLayer masks use Logic1's names and the two orientation tests CD_UNSPECIFIED. The hit
sound keeps getBuildingHurtSFXID's result as a HitSoundMaterialInt and compares it with HSM_WOOD and HSM_STONE, as
UpdateArabianSwordsman does. Nothing is cast to short on its way into a short field or on a read of a short layer,
steps by one are ++ / --, and the gate-or-tower test for the damage is a truth test, as in the AI's test below it.
advanceMoatDigProgress and removeMoat return BOOLEnum, but their results stay compared with 1: the original has
`cmp eax, 1` there (0x0055FC1F, 0x0055FE11), and a truth test gives `test eax, eax`.

compiler-flags-gl.txt gets UpdatePikeman.cpp and ViewportRenderState/xyAreValid.cpp, a callee the unit has to see
(100% on main).

Real builds on main 655adce, every listed file's function resolver and all data resolvers on. A small one: main's
/GL list plus this branch's four lines (main's UpdateGateHouseLarge and UpdateGateHouseSmall left out: they don't
compile on 655adce), on this text. A full one: every file, on an earlier spelling of this file that differs only
in names and spellings and gives the same listing, against main alone (main's 12 files that don't compile on
655adce left out). UpdatePikeman and xyAreValid read 100.0% raw and normalised in both, with no differing line
in the small one, and nothing on main reads lower in the full one.

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

---------

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
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