Skip to content

Reimplement 5 Map::Units update functions from gynt's units branch, under /GL (100%) - #393

Merged
gynt merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-B-round1-100
Nov 11, 2026
Merged

gynt merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-B-round1-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Five more Map::Units update functions from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), all at 100%. They are the ones his status marks "LTCG regs across calls": each matches only with /GL on it and on the callees it keeps registers across.

Function Address gynt's status His draft today
Units::UpdateBurningMan 0x0053BC20 66.1% 76.5%
Units::UpdateMiner 0x005401D0 51.8% 58.9%
Units::UpdateQuarryMason 0x00550A50 45.3% 62.2%
Units::UpdateDrunkard 0x00567320 48.3% 53.5%
Units::UpdateInnkeeper 0x00540EC0 50.7% 63.4%

"His draft today" is his file on our fork's copy of his branch (his 48b79a2 with main d5d965d merged in, plus a names-only build fix), in a full reccmp build with the Globals and the function resolvers on (2026-11-10). Each commit says which of his commits it builds on and carries him as co-author.

What made them match:

  • /GL (first commit). The original keeps a constant, or the unit's state, in edx across checkIfCitizenUnitIsAliveBasedOnState, which leaves edx alone (the first four). UpdateInnkeeper keeps the unit index in esi/edi across hasUnitReachedDestination, checkIfCitizenUnitIsAliveBasedOnState and getBuildingResourceAmountByUid. That only happens with link-time code generation, so the five go on cmake/compiler-flags-gl.txt with six UnitsState callees: checkIfCitizenUnitIsAliveBasedOnState, makeUnitStopWalkingByClearingPathProgressState, hasTunnelerNotFinishedDigging, hasUnitReachedDestination, commitUnitLocation and resumeMovementIfNoAttackTarget. All six are 100% on main and stay at 100% under /GL. UpdateInnkeeper's four BuildingsState callees are on the list already.
  • Three callees stay off the list, though they are 100% on main. setLastEncounteredTroopUnit makes no calls, so under /GL UpdateQuarryMason would skip the re-read of DAT_CurrentUnitSlotID that the original does after it (quick_diff 1.0000 -> 0.9901). setWorkplaceBuildingEntryAsTarget and resetUnitMovementState gain nothing, since they call into functions that are not under /GL.
  • Unit accesses through DAT_CurrentUnitSlotID. After each call it cannot see into, the original reads the current slot again instead of keeping unitID. So 10 to 15 statements per function index units[DAT_CurrentUnitSlotID::instance], as the original does; UpdateInnkeeper has no unitID local at all. Everything through unitID measures 0.62 to 0.91 in quick_diff (0.7154 BurningMan, 0.9013 Miner, 0.9059 QuarryMason, 0.6877 Drunkard, 0.6159 Innkeeper).
  • UpdateInnkeeper's ale tests were one step off in the draft: the original has jg/jge, so they are <= 150 and >= 400.

Measured on main 639c193. Main is merged in up to 02b5f47, whose newer commits add enum names only, and the changes here since the build are comment lines only.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), resolvers enabled locally (not committed), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie.
  • reccmp: a full build of the branch alone reads all five at 100.0% raw and normalized, and the ten /GL callees at 100%.
  • quick_diff for the measured comments is the variant that compares absolute global addresses, with the callees' .cpp files #included at the top of the caller. That emulates /GL for these functions to the instruction.
  • Checks: openshc-pr-check gives no errors, and status_regression.py finds no score below a known one.
  • Not run in game.

Header change (a commit of its own)

gynt's Ghidra sync 02b5f47 brought both values to main. Main is merged in (17a84e8), so this commit now matches main.

UnitState gets US_UNKNOWN_108 and US_UNKNOWN_120 (380b74f4b), in the US_UNKNOWN_<n> form, in place of (UnitState)108 and (UnitState)120; code generation is the same.

  • 120: UpdateBurningMan stores it (mov word ptr [ecx + 0x138880C], 0x78 at 0x0053BC9D: the units array at 0x0138854C plus state at 0x2C0) and tests it (cmp bp, 0x78 at 0x0053BCEA).
  • 108: UpdateMiner, UpdateQuarryMason and UpdateInnkeeper test it (cmp ax, 0x6c at 0x005402E3, cmp dx, 0x6c at 0x00550B66, cmp ax, 0x6c at 0x00541523).
  • The enum jumps from US_MELEE_ATTACK_WALL (107) to US_JESTER_ROAM_TO (109), and from US_STONE_DEATH_03 (116) to US_UNKNOWN_121.

Kept as they are, measured with quick_diff (each has a one-line comment)

Function Kept The other form
UpdateInnkeeper state 2: the success path in the then-arm, the unit removed after it an early return: 1.0000 -> 0.9723
UpdateInnkeeper state 3: the < 400 arm first, the full inn's commitUnitLocation after it 1.0000 -> 0.9822
UpdateInnkeeper in that arm: the accessible-storage path first 1.0000 -> 0.9881
UpdateQuarryMason the workplace read again after setLastEncounteredTroopUnit the workplace local: 1.0000 -> 0.8536
UpdateQuarryMason the blocks from the death state on as one else-if chain early returns: 1.0000 -> 0.9507
UpdateMiner the death, unit-is-alive and default blocks as one else-if chain early returns: 1.0000 -> 0.9385
UpdateDrunkard resourceToDeposit >= 6 for the next state (the original's setl) < 6 with the arms swapped: 1.0000 -> 0.9848

Proposed, not changed here

  • 25 (char) casts remain, on Unit::disappearFadeAlphaCountdown (+0x32E) and on fields of the AnimationFrameData tables. Both are byte in the headers, but the original compares them as signed (< 0, > 32), so without the cast < 0 is always false. Retyping them to char in Ghidra would let the casts go.
  • Frames_Shared_UnitClimbingUp (UnitPropertiesDefinedData +0x8B78, 33,748 bytes) is the whole AnimationFrameData table. All five read other animations from it (ANIM_Frames_UnitDeath, DAT_InnKeeperAnimationIndices, drunkard), so a name such as AnimationFrames would fit better.
  • DAT_UnitHasBecomeIdle is undefined4, but it is only ever stored 1 or tested against 0, so it could be BOOLEnum (as Reimplement 11 Map::Units functions from gynt's units branch (100%) #390 said too).
  • UpdateInnkeeper sets animationCycleNumber = 0 a second time when setDestinationForUnit fails (line 106), as the original does.
  • buildingIsAccessible returns int, so the tests on it stay == 0 / != 0.

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 7 commits November 11, 2026 11:59
…callees

UpdateBurningMan, UpdateMiner, UpdateQuarryMason, UpdateDrunkard and UpdateInnkeeper only match with link-time code
generation. The original keeps values in registers across the calls it could see into: a constant or the unit's
state in edx across checkIfCitizenUnitIsAliveBasedOnState (which leaves edx alone) in the first four, and the unit
index in esi/edi across hasUnitReachedDestination, checkIfCitizenUnitIsAliveBasedOnState and
getBuildingResourceAmountByUid in UpdateInnkeeper. After the calls it could not see into, it reads
DAT_CurrentUnitSlotID again.

The six UnitsState functions added here are 100% on main with their function resolvers on, and each stays at 100%
under /GL (full build of this branch alone). Who needs them:

- checkIfCitizenUnitIsAliveBasedOnState and makeUnitStopWalkingByClearingPathProgressState: all five;
- hasTunnelerNotFinishedDigging: UpdateBurningMan (its last 0.8 points) and UpdateDrunkard;
- hasUnitReachedDestination and commitUnitLocation: UpdateMiner, UpdateQuarryMason and UpdateInnkeeper;
- resumeMovementIfNoAttackTarget: UpdateQuarryMason and UpdateDrunkard.

UpdateInnkeeper's four BuildingsState callees are on main's list already and stay at 100% too: buildingIsAccessible,
getStorageBuildingForResourceTypeAndAmount, getBuildingResourceAmountByUid and addResourceToStockpile.

Three callees that are 100% on main stay off the list. setLastEncounteredTroopUnit (UpdateMiner, UpdateQuarryMason)
makes no calls, so under /GL the caller would know it leaves DAT_CurrentUnitSlotID alone and skip the re-read the
original does (with it in UpdateQuarryMason's translation unit: quick_diff 1.0000 -> 0.9901; UpdateMiner returns
right after the call and stays 1.0000). setWorkplaceBuildingEntryAsTarget and resetUnitMovementState
(UpdateInnkeeper) would gain nothing: they call on into functions that are not under /GL (buildingIsAccessible's
callees calculateCanPlayerUnitsNavigateToAreaFromArea and determineBuildingEntranceFromKeepArea, which are not
reimplemented yet, and commitPendingUnitPosition), so UpdateInnkeeper reads the slot again after them either way,
and with both in its translation unit its code is identical (quick_diff 1.0000). setDestinationForUnit,
computeLadderClimbPath and changeDestinationByAmount are not reimplemented yet.

quick_diff here is the variant that compares absolute global addresses, run with the callees' .cpp files #included
at the top of the caller; that emulates /GL for these functions to the instruction.

On reimpl/map-units-updatefunctions, gynt's status lines for these five read "LTCG regs across calls"
(UpdateInnkeeper: "LTCG_regs"), which is where this started.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateBurningMan (0x0053BC20) puts a unit into state 120 (mov word ptr [ecx + 0x138880C], 0x78 at 0x0053BC9D:
the units array at 0x0138854C plus state at 0x2C0) and tests for it (cmp bp, 0x78 at 0x0053BCEA). Units::UpdateMiner
(0x005401D0), Units::UpdateQuarryMason (0x00550A50) and Units::UpdateInnkeeper (0x00540EC0) test for state 108 (cmp
ax, 0x6c at 0x005402E3, cmp dx, 0x6c at 0x00550B66, cmp ax, 0x6c at 0x00541523). The enum goes from
US_MELEE_ATTACK_WALL (107) to US_JESTER_ROAM_TO (109) and from US_STONE_DEATH_03 (116) to US_UNKNOWN_121 (121); the
two are named in the US_UNKNOWN_<n> form. Used in place of (UnitState)108 and (UnitState)120; code generation is the
same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateBurningMan. Builds on gynt's bd4d1d5 ("66.14% LTCG regs across calls") as our fork's copy of his branch has
it, with main merged in and main's header renames followed (43a17df, names only), where his draft measures 76.5%
in reccmp (normalized 74.5%) in a full build with the Globals and the function resolvers on (2026-11-10). This
version is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
hasTunnelerNotFinishedDigging, checkIfCitizenUnitIsAliveBasedOnState and
makeUnitStopWalkingByClearingPathProgressState (first commit), which all stay at 100%.

The stores before the state read, the state-0 block, the unit-is-alive block and the three stores after
hasTunnelerNotFinishedDigging index the units through DAT_CurrentUnitSlotID. That keeps them in the original's order
and leaves edx for the constant 1 the original keeps across checkIfCitizenUnitIsAliveBasedOnState (everything
through unitID: quick_diff 1.0000 -> 0.7154, in one comment above unitID). State 120 is US_UNKNOWN_120 (header
commit), and computeLadderClimbPath's BOOLEnum is tested for truth.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateMiner. Builds on gynt's 5e96437 ("51.78% LTCG regs across calls") as our fork's copy of his branch has it,
with main merged in and main's header renames followed (43a17df, names only), where his draft measures 58.9% in
reccmp (normalized 58.9%) in a full build with the Globals and the function resolvers on (2026-11-10). This version
is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasUnitReachedDestination, commitUnitLocation and
makeUnitStopWalkingByClearingPathProgressState (first commit), which all stay at 100%.

Ten unit accesses go through DAT_CurrentUnitSlotID, which keeps the original's load and store order and its
registers (everything through unitID: quick_diff 1.0000 -> 0.9013), and the death, unit-is-alive and default blocks
form one else-if chain, so the shared tail sits in the second arm as in the original (early returns: quick_diff
1.0000 -> 0.9385). The worker slot takes unitID without a (short) cast, and state 108 is US_UNKNOWN_108 (header
commit).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateQuarryMason. Builds on gynt's ec84561 ("45.27% LTCG regs across calls") as our fork's copy of his branch has
it, with main merged in and main's header renames followed (43a17df, names only), where his draft measures 62.2%
in reccmp (normalized 62.2%) in a full build with the Globals and the function resolvers on (2026-11-10). This
version is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasUnitReachedDestination, commitUnitLocation,
makeUnitStopWalkingByClearingPathProgressState and resumeMovementIfNoAttackTarget (first commit), which all stay at
100%.

Twelve unit accesses go through DAT_CurrentUnitSlotID, which keeps the original's load and store order and its
registers (everything through unitID: quick_diff 1.0000 -> 0.9059). One of them is the workplace read at the end of
the disappear block, where the original reads the slot again after setLastEncounteredTroopUnit (the workplace local
there: quick_diff 1.0000 -> 0.8536). The blocks from the death state on form one else-if chain (early returns:
quick_diff 1.0000 -> 0.9507). The worker slot takes unitID without a (short) cast, and state 108 is US_UNKNOWN_108
(header commit).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateDrunkard. Builds on gynt's 171a691 ("48.31% LTCG regs across calls") as our fork's copy of his branch has
it, with main merged in and main's header renames followed (43a17df, names only), where his draft measures 53.5%
in reccmp (normalized 53.5%) in a full build with the Globals and the function resolvers on (2026-11-10). This
version is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasTunnelerNotFinishedDigging, makeUnitStopWalkingByClearingPathProgressState
and resumeMovementIfNoAttackTarget (first commit), which all stay at 100%.

Fifteen unit accesses go through DAT_CurrentUnitSlotID, which keeps the original's load and store order and its
registers (everything through unitID: quick_diff 1.0000 -> 0.6877), and the next state comes from an if that tests
resourceToDeposit >= 6, which gives the original's setl (< 6 with the arms swapped: quick_diff 1.0000 -> 0.9848).
computeLadderClimbPath's BOOLEnum is tested for truth.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateInnkeeper. Builds on gynt's 6b8b0c2 ("50.68% LTCG_regs") as our fork's copy of his branch has it, with main
merged in and main's header renames followed (43a17df, names only), where his draft measures 63.4% in reccmp
(normalized 58.5%) in a full build with the Globals and the function resolvers on (2026-11-10). This version is
100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasUnitReachedDestination, commitUnitLocation and
makeUnitStopWalkingByClearingPathProgressState (first commit) and buildingIsAccessible,
getStorageBuildingForResourceTypeAndAmount, getBuildingResourceAmountByUid and addResourceToStockpile (on main's
list), which all stay at 100%.

His draft had the ale-level tests one step off: < 150 and > 400 where the original has jg and jge, that is <= 150
and >= 400. Every unit access goes through DAT_CurrentUnitSlotID, which the original reads again after each call it
cannot see into (a unitID local: quick_diff 1.0000 -> 0.6159), and the substate wraps at >= 4.

Three tests that the draft wrote as early returns keep the success path in the then-arm and the failure after the
if, which is where the original puts the failure blocks (it jumps forward to them):

- state 2, ale fetched: when setWorkplaceBuildingEntryAsTarget fails, the unit is removed after the then-arm (an
  early return: quick_diff 1.0000 -> 0.9723);
- state 3, ale delivered: the < 400 then-arm comes first, and the full inn's commitUnitLocation after it (0.9822);
- inside that arm, the accessible-storage path comes first, and the other commitUnitLocation after it (0.9881).

The other failures return early, as in the draft: the unreached destination in states 2 and 3, the storage that is
not accessible after the serving animation and in state US_AIM_WEAPONUnk, and the full inn there.
setWorkplaceBuildingEntryAsTarget's BOOLEnum is tested for truth, the target slot takes storage without a (short)
cast, states 2 and 3 are US_STATE_2Unk and US_STATE_3Unk, and state 108 is US_UNKNOWN_108 (header commit).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 11, 2026

@gynt gynt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes were just pushed upstream btw

kotijekriv and others added 2 commits November 11, 2026 12:28
Its Ghidra sync brings UnitState's US_UNKNOWN_108 and US_UNKNOWN_120, so the header commit now matches main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ate functions

gynt on sourcehold#393: "These comments can go!" The top comment of UpdateBurningMan, UpdateMiner, UpdateQuarryMason,
UpdateDrunkard and UpdateInnkeeper explained why their unit accesses index through DAT_CurrentUnitSlotID. Comments
only; the code is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 11, 2026
@kotijekriv

Copy link
Copy Markdown
Contributor Author

Thanks! Main (02b5f47) is merged in (17a84e8), so US_UNKNOWN_108 and US_UNKNOWN_120 now come from your sync, and the header commit matches main. The DAT_CurrentUnitSlotID comments are gone from all five files (a57eb32).

The other one-line comments are still there: the three early-return ones in UpdateInnkeeper, the else-if chains in UpdateMiner and UpdateQuarryMason, the workplace re-read in UpdateQuarryMason and the >= 6 test in UpdateDrunkard. Should those go too?

@gynt

gynt commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Thanks! Main (02b5f47) is merged in (17a84e8), so US_UNKNOWN_108 and US_UNKNOWN_120 now come from your sync, and the header commit matches main. The DAT_CurrentUnitSlotID comments are gone from all five files (a57eb32).

The other one-line comments are still there: the three early-return ones in UpdateInnkeeper, the else-if chains in UpdateMiner and UpdateQuarryMason, the workplace re-read in UpdateQuarryMason and the >= 6 test in UpdateDrunkard. Should those go too?

Those are fine

@gynt
gynt added this pull request to the merge queue Nov 11, 2026
Merged via the queue into sourcehold:main with commit cbf4bc6 Nov 11, 2026
4 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