Skip to content

Reimplement UpdatePriest, UpdateMerchantHorse and UpdatePikeman from gynt's units branch, under /GL (100%) - #512

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

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

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Three more Map::Units update functions at 100%, from gynt's units branch reimpl/map-units-updatefunctions (his draft #204). They waited for #378: each one keeps values in registers across a call into one of the UnitsState functions that #378 put on the /GL list, so it only matches once the compiler can see that callee's body. Each commit cites the draft it builds on and credits gynt as co-author. No header changes.

Function Address Before After
Units::UpdatePriest 0x00566750 gynt's draft 63.25% (b389b78; 57.72% at 525a6eb) 100.0%, under /GL
Units::UpdateMerchantHorse 0x00541A30 gynt's draft 46.84% (4b7d935; 41.35% at e103100) 100.0%, under /GL
Units::UpdatePikeman 0x0055F110 gynt's draft 41.54% (4630f86) 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 (a86aceb03) with --resolve-wrapped-calls --ignore-security-cookie.
  • Small build: main 655adce's /GL list with this branch's four lines (253 files, every listed file's resolver on), rerun on this head. All three read 100.0% raw and normalised with no differing line, and so does xyAreValid.
  • Full build: every file of main 655adce, with all function and data resolvers on, against the same build of this branch one spelling earlier. Since then only names and spellings in UpdatePikeman changed (the hit sound's HitSoundMaterial names, the damage as if / else, break; at the end of the last case, comments and a blank line), and its listing in the small build is the same, so the result stands. Both built with 0 failures, 2,907 and 2,910 files, and reccmp averages 99.54% over 2,925 and 2,928 functions. The three units come in at 100.0% raw and normalised, and xyAreValid stays at 100.0% on the /GL list. Nothing on main reads lower. The only other changes are UI::Menu::Constructor_Menu (98.4% to 100%) and handleMenuItems (97.9% to 100%). Earlier full builds where neither file changed showed the same jump, so it follows where functions land in the image. This PR doesn't touch them. The 12 main files listed under Other PRs were left out of both builds.
  • Checks: openshc-pr-fix finds nothing to change, and openshc-pr-check gives 0 errors and 89 notes, all of the kinds below.
  • Not run in game.

What got them to 100%

All three started from gynt's drafts and went through our development branches, where they reached 99.1-99.9% on main before #378. What was left in each was a value the original keeps in a register across a call into a function whose body the compiler could not see yet.

  • UpdatePriest: the walk to the unit to bless reads the unit through DAT_CurrentUnitSlotID from its first line, call arguments included, and doesn't assign unitID there. With changeDestinationByAmount visible, the compiler knows the call leaves the slot alone and keeps it where the original does. In both arms of that walk animationSheetFrameOffset = 1 comes before field_0x30_animRelated = 16, and destinationNeeded at the end of the preaching state goes through the slot too. IncrementAndOptionalUpdateAVValueRelated, the other callee it has to see, has been on main's list since Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416.
  • UpdateMerchantHorse: one store: updateTickTracker = 0, the last store before resetUnitMovementState, goes through the slot. With commitUnitLocation's callees visible (setupUnitSharingTileIDs from Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 and setupUnitSharingCurrentTilePosition), the compiler then sets up resetUnitMovementState's argument above that store only, as the original does.
  • UpdatePikeman: int building = 0; sits with the function's locals and there is no local copy of the unit ID, as in UpdateKnight (One more Map::Units function at 100%: UpdateKnight #444). That gives the original's 4 bytes of frame that are never touched and its zero register for a wall. The dig-moat block after setDestinationForUnit reads the unit through the slot, call argument included. With changeDestinationByAmount visible, the compiler stores the dig tile's X and Y before the pops, as the original does (0x0055F2AB, 0x0055F2B2).
  • In all three, the states use UnitState's names where main has one (2, 3, 9, 11, 108 and 121 in UpdatePriest, 2 and 3 in UpdateMerchantHorse, 2, 3 and 108 in UpdatePikeman), and in UpdatePriest and UpdateMerchantHorse the fade step is written -2, as in main's UpdateMerchant.

Names from main's newer headers

The drafts and our earlier texts were written against older headers. This branch uses what main declares now:

  • UpdatePikeman's wall-danger test for AI players read two raw offsets into attackInfo. Main's AttackInfo now has the per-player blocks (playerInfo[8], 96188 bytes each, from 0x4797C), so the test reads attackInfo.playerInfo[attacker - 1].currentPlayerID_OR_attackedPlayerID_OR_one and .someMinimumDistance. The byte it compares is DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[player][tile], which used to be written as player * 80400 + 0x1EE2998 + tile.
  • The LogicLayer masks use Logic1's names (L_WALL_OR_GATEHOUSE, L_MOAT, L_WALL_OR_GATEHOUSE | L_KEEP_NON_MANOR_HOUSE), and the two orientation tests use CD_UNSPECIFIED, as in Reimplement 47 UnitsState functions at 100% and restyle 28 more #378.
  • The hit sound in UpdatePikeman's wall attack keeps getBuildingHurtSFXID's result as a HitSoundMaterialInt named material and compares it with HSM_WOOD and HSM_STONE, as main's UpdateArabianSwordsman does.

Code generation is the same.

Four lines added to cmake/compiler-flags-gl.txt

  • UpdatePriest.cpp, UpdateMerchantHorse.cpp and UpdatePikeman.cpp: the units themselves. Under /GL, they see their callees' bodies that main's list already has (changeDestinationByAmount, setupUnitSharingTileIDs, commitUnitLocation, hasUnitReachedDestination, IncrementAndOptionalUpdateAVValueRelated and others).
  • Rendering/ViewportRenderState/xyAreValid.cpp: a callee UpdatePikeman has to see. It is 100% on main and was on no list. It reads 100.0% on the list too.

Kept as they are

  • (UnitState)105 and (UnitState)124 in UpdatePikeman. Main's UnitState has no names for them yet (see below).
  • The (char) casts on Unit::disappearFadeAlphaCountdown, Unit::engineerManningSiegeStateRef_checkType and the AnimationFrameData tables, as in Reimplement 5 Map::Units update functions from gynt's units branch, under /GL (100%) #393 and Reimplement 4 Map::Units functions and RenderAnimatedTutorialFloatOverlay, with UnitState names (100%) #409. The fields are byte in the headers, but the original reads them as signed (movsx, < 0, > 32).
  • (short) on both sides of one difference in UpdatePikeman's moat-filling block (targetedBuildingTile minus translationMatrix[...].addXgetTile, stored into the unit's short dig tile X). The other (short) casts are gone: main's header has BuildingLayer and UnitLayer as short now, and a store into a short field needs none.
  • (int)(mapTimeInTicks + slot) % 40 in UpdatePikeman. GameCore::mapTimeInTicks is a uint, and the original takes a signed remainder, as in UpdateKnight and the other unit functions on main.
  • int damage; and int thrustGfx; are declared before the if / else chain that sets them on every path.
  • Comments in UpdatePikeman say where both arms of a test keep a statement of their own, because writing it once loses the match.
  • advanceMoatDigProgress(...) == 1 and removeMoat(...) != 1 in UpdatePikeman stay comparisons with 1, each with a one-line comment. Both return BOOLEnum, but the original compares the result with 1 (cmp eax, 1 at 0x0055FC1F and 0x0055FE11). We tried truth tests: they compile to test eax, eax and lose the match.
  • moat > -1 in UpdatePikeman's two moat orders stays as it is: the original compares with -1 (cmp reg, -1 / jle), and moat >= 0 gives test / jl.
  • Tests in UpdatePikeman have different forms on purpose. IsGateOrTowerArray[...] is a BOOLEnum array, so it is a truth test. isDisappearingUnk, goToRallyPoint, isSelected, seated and killedFlagUnk are short fields in Unit.hpp, so they keep != 0.
  • int attacked sits in UpdatePikeman's first line: the original reads the unit's attackedUnitID there, before addUnitToNewTribe, and the melee state uses that value for the height difference, so it stays up there.
  • int attacker in UpdatePikeman's wall attack is the unit's owner read again after the calls, as the original does, rather than the owner read at the top.
  • int const churchType in UpdatePriest is read before the stores in front of the switch, as the original reads it.
  • field_0x58 = 1 in UpdatePikeman stays a number. The checker matches the name to a BOOLEnum field of SoundEffectsHelperData1, but Unit::field_0x58 is a short.
  • The checker's other notes are Ghidra's placeholder field names (field_0x3f4, field_0x5d28, ...), which we have nothing better for.

Proposed, not changed here

  • US_UNKNOWN_105 and US_UNKNOWN_124 in UnitState. UpdatePikeman's state switch (cmp eax, 0x7d at 0x0055F175, byte table 0x00560680, jump table 0x00560638) gives 105 an arm of its own (0x0055F474) and sends 124 to the arm of state 4, US_RELOAD_WEAPONUnk (0x0055FA7C). The original stores 105 at 0x0055F1E8 and 124 at 0x0055F2CC and 0x0055FA81 (mov word ptr [reg + 0x138880C], the units array plus state). Left out so that this PR needs no header sync.
  • int results for advanceMoatDigProgress and removeMoat. Both return BOOLEnum in TileMapState.hpp, but UpdatePikeman compares their results with 1 rather than testing them, which reads like an int result.
  • The moat states' names. US_DIG (125) is the state the pikeman's fill-moat order (UIT_FILL_MOAT) sets, and it fills the moat with removeMoat (commented "filling a moat" here; main's UpdateAssassin sets it for a moat to fill too). 124, set by the dig-moat order (UIT_DIG_MOAT), is the one that digs, with advanceMoatDigProgress. So US_DIG reads better as a fill state, and 124 could take the dig name.
  • The (char) casts above: retyping those fields to char in Ghidra would let the casts go here and in the unit functions on main (Reimplement 5 Map::Units update functions from gynt's units branch, under /GL (100%) #393 and Reimplement 4 Map::Units functions and RenderAnimatedTutorialFloatOverlay, with UnitState names (100%) #409 proposed this too).

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 3 commits November 28, 2026 11:16
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>
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>
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>
@gynt
gynt added this pull request to the merge queue Nov 29, 2026
Merged via the queue into sourcehold:main with commit a841629 Nov 29, 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