Skip to content

Reimplement 4 Map::Units functions and RenderAnimatedTutorialFloatOverlay, with UnitState names (100%) - #409

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

kotijekriv wants to merge 7 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-round2-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Five more functions at 100%, and one header commit that names six unit states. Four of the functions are Map::Units functions from gynt's units branch reimpl/map-units-updatefunctions (his draft #204). The fifth is UI::Rendering's tutorial arrow, from our draft #313, in the form TheRedDaemon worked out in his review there. Each commit cites the drafts it builds on and credits gynt as co-author; the arrow's commit credits TheRedDaemon too.

Function Address Before After
Units::CurrentUnitHasHealer 0x0053A020 gynt's draft 89.29% (6d8ad88) 100.0%
Units::UpdateRabbit 0x00542630 gynt's draft 79.00% (49bd9c9; 69.32% at 209f6d4) 100.0%
Units::UpdateDeer 0x00541DE0 gynt's draft 64.64% (6458bca; 36.18% at f7d126f) 100.0%
Units::UpdateMerchant 0x005416B0 gynt's draft 63.80% (8372c08; 53.39% at 0adb264) 100.0%, under /GL (see below)
UI::Rendering::RenderAnimatedTutorialFloatOverlay 0x004BC790 our draft #313, 67.2% (ce6bd1e); gynt's 60.8% (e8e27aa; 60.79% at 5236e98) 100.0%

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 (cce67bcab) with --resolve-wrapped-calls --ignore-security-cookie.
  • Full build: all 2,363 source files of main 72c6a2e with this branch, against a full build of main alone as the baseline (2,358 files), each with its own compiler-flags-gl.txt. All five read 100.0% raw and normalised, and so do UpdateMerchant's nine reimplemented callees. Of the 2,376 functions measured on main, 2,374 read exactly the same with this branch and none read lower. Two read higher, UI::Menu::Constructor_Menu (98.4% to 100.0%) and UI::Menu::handleMenuItems (97.9% to 100.0%). Those are the same two as in Reimplement 5 UI functions from gynt's drafts and #328 (100%) #407's measurement, for the same reason: both compare with the constant 0x11000000, a symbol in main's DLL happens to sit at that address, and reccmp names the constant after it until the DLL's layout moves. Neither file changes.
  • Checks: openshc-pr-fix finds nothing to change, and openshc-pr-check gives no errors.
  • Not run in game.

What got them to 100%

  • CurrentUnitHasHealer: the current player is read into a const local before the loop. The original loads currentPlayerSlotID before the loop's first test, the way PlayArmoryIsFullWarning and WarnIfPlayersGranaryIsFull read theirs. maxUnitCount is an int, so the draft's (int) cast goes.
  • The three update functions: after each call it can't see into, the original reads DAT_CurrentUnitSlotID again instead of keeping unitID. So the statements where it does that index units[DAT_CurrentUnitSlotID::instance], and the rest keep unitID. They were picked by a quick_diff search, one statement per step. In UpdateMerchant every call also passes the slot.
  • Two stores through a Unit* const local. This is the compiler's instruction scheduler, not the order of the statements. In UpdateRabbit and UpdateDeer, when hasUnitReachedDestination returns TRUE, animationCycleNumber and substate are written through the pointer. With every store through units[unitID], MSVC sees them as independent and puts the epilogue's pop edi before them (UpdateRabbit read 99.8%). Through a pointer it can't tell them apart from the state store, so they keep their place before it, as in the original. We found this by running the compiler under cdb and listing the scheduler's dependency edges. UpdateMerchant's state 3 uses the same lever to keep gfxNumber and field_0x30_animRelated after the stateBasedSpeed store. Each pointer has a one-line comment saying why.
  • UpdateRabbit: the state-203 path sits inside the > 0 test, the slowdown is an int compare with fixedRng & 0xf, and the death cases test animationFrame <= 0 first. These are the original's branch directions. tribe is an int local read at the top, so its load is a movsx as in the original.
  • UpdateDeer: state 204 tests substate through the slot, as state 203 does, so its substate isn't kept in a register for the increment, and the two states share one idle tail as in the original. In both seated branches of state 205, the frame-0 path sets DAT_UnitHasBecomeIdle and falls into the shared idle block; the original has that block copied into each path. frameOffset is declared at the top, because the original holds its 1 in edx from the start.
  • UpdateMerchant: in state 3 the three stores after the destination test go through the slot, which the original reads again after setDestinationForUnit. State 2's fade step is written as -2, the byte the original stores (0xFE). The rest is the /GL list change below.
  • RenderAnimatedTutorialFloatOverlay: TheRedDaemon's form from his review (Reimplement 22 rendering, UI and GameSynchronyState functions (not yet 100%) #313 (comment)). x and y step down by 8 and 32 before the per-resolution offset, and by 28 and 69 after the draw buffer is saved, and the offset comes from a chain of plain ifs. The original pushes the trailing 0 before it works out the other arguments, and this form gives that order. His second restore of the draw buffer after the first call compiles to the same code, so it is left out. Thanks a lot to TheRedDaemon for that one!

Header change (a commit of its own)

UnitState gets US_UNKNOWN_117, US_UNKNOWN_202, US_UNKNOWN_203, US_UNKNOWN_204, US_UNKNOWN_205 and US_UNKNOWN_209 (7724c54), in the US_UNKNOWN_<n> form, in place of (UnitState)N and numeric case labels in UpdateRabbit and UpdateDeer. Code generation is the same.

  • The switches: both functions dispatch on the unit's state through jump tables that run to 209. UpdateDeer's has its own entries for 117, 202 to 205 and 209 (0x00541E5F, cmp ecx, 0xd1, tables at 0x00542558 and 0x00542528). UpdateRabbit's has its own entries for 117, 203, 204 and 209, and sends 202 and 205 to its default (0x0054276E, cmp eax, 0xd1, tables at 0x00542C34 and 0x00542C0C).
  • The stores (mov word ptr [reg + 0x138880C]: the units array at 0x0138854C plus state at 0x2C0): UpdateDeer stores 205, 203 and 204 at 0x00541EBC, 0x00541ED5 and 0x00541EE0, and 117 at 0x0054244B and 0x0054249C. UpdateRabbit stores 203, 204 and 117 at 0x005427C6, 0x005427D3 and 0x00542B0E. scatterTribeUnitsRandomly stores 209 at 0x00523A0A; main's scatterTribeUnitsRandomly.cpp writes it as state.generic = 209 and can use the name once this is in.
  • Functions still to come store some of these too: UpdateCamel and UpdateLion (205 and 117), and UpdateCageDog (117).
  • The enum goes from US_STONE_DEATH_03 (116) to US_UNKNOWN_120, and ends at US_ASSASSIN_CLIMBING_DOWN (129).

Main's declaration check will fail on this PR and label it until the six values are synced to main. Then we merge main in, and the check passes.

One line added to cmake/compiler-flags-gl.txt

UpdateMerchant.cpp goes on the list. It reads 100.0% with the line and 78.0% without it.

The original keeps values in registers across checkIfCitizenUnitIsAliveBasedOnState and hasUnitReachedDestination, so UpdateMerchant has to see their bodies. Both are on main's list already. It also reads DAT_CurrentUnitSlotID again after activateTraderState, so that callee must stay off the list: with its body visible, the slot stays in esi across the call and UpdateMerchant reads 90.0%. UpdateMerchant is activateTraderState's only caller.

We checked each of these numbers again in small builds of UpdateMerchant and its nine reimplemented callees: 100.0% as the branch has it, 78.0% without the line, and 90.0% with activateTraderState on the list too. gynt's #378 puts two more of its callees on the list, clearHiddenFlagAndUpdatePosition and resetUnitMovementState. With those two added, UpdateMerchant still reads 100.0%, and so do they, so the two PRs can go in in either order.

Kept as they are, measured with quick_diff

Function Kept The cleaner form
CurrentUnitHasHealer the player read into a local before the loop read in the loop condition: 0.9259 -> 0.8519
UpdateRabbit calculatedOwnerPlayerIndex mapped by an else-if chain (9 to 2, 10 and 11 to 3, 12 to 4, 13 to 6, 14 to 8, 15 to 3) a switch: 0.9591 -> 0.9288
UpdateRabbit tribe read at the top read where it is used: 1.0000 -> 0.5216
UpdateDeer tribe read at the top read just before the switch: 0.9122
UpdateDeer frameOffset declared at the top declared in the movement case: 0.8300
RenderAnimatedTutorialFloatOverlay x and y stepped down in two places one step of 36 and 101: 0.8035 / 0.6356
RenderAnimatedTutorialFloatOverlay the offset as a chain of plain ifs an else-if chain: 0.8983

Most of these have a one-line comment in the code that says why.

One odd number kept: RenderAnimatedTutorialFloatOverlay's offset for 1680x1050 is 4050. Every other resolution gets its height minus 600, so 450 was probably meant, but the original has 4050 (mov ebx, 0xfd2 at 0x004BC862), so it stays, with a comment.

What some placeholders hold

  • INT_00df563c: the time (timeGetTime) when the tutorial arrow's current cycle of 10 frames started.
  • INT_00df5640: bit 0 says that start time has been set.
  • DAT_00df5540: the tutorial step transition (0, 1 or 2; InitTutorialStepTransition stores 1 or 2). While it is nonzero, the arrow isn't drawn.

Proposed, not changed here

One function leaves #313

  • UI::Rendering::RenderAnimatedTutorialFloatOverlay (0x004BC790)

#313 gets one commit that removes its .cpp file and puts its status line back to main's (21b73bc). Main has no file for it, and #313 changed no header for it. #313 still compiles as before without it: its one file that doesn't compile, RenderGreatestLordScreen, failed before this change too.

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 6 commits November 14, 2026 04:37
Units::UpdateDeer (0x00541DE0) and Units::UpdateRabbit (0x00542630) dispatch on the unit's state through jump
tables that run to 209 (cmp ecx, 0xd1 at 0x00541E5F with tables at 0x00542558 and 0x00542528; cmp eax, 0xd1 at
0x0054276E with tables at 0x00542C34 and 0x00542C0C). Deer's table has its own entries for 117, 202 to 205 and
209, Rabbit's for 117, 203, 204 and 209 (202 and 205 go to its default). They store these states (mov word ptr
[reg + 0x138880C]: the units array at 0x0138854C plus state at 0x2C0): Deer 205, 203 and 204 at 0x00541EBC,
0x00541ED5 and 0x00541EE0 and 117 at 0x0054244B and 0x0054249C, Rabbit 203, 204 and 117 at 0x005427C6,
0x005427D3 and 0x00542B0E, and TribesState::scatterTribeUnitsRandomly stores 209 at 0x00523A0A. The enum goes
from US_STONE_DEATH_03 (116) to US_UNKNOWN_120 and ends at US_ASSASSIN_CLIMBING_DOWN (129); the six are named in
the US_UNKNOWN_<n> form. Used in place of (UnitState)N and the numeric case labels; code generation is the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::CurrentUnitHasHealer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions, the branch of
PR sourcehold#204 (6d8ad88, 89.29%, "loop precheck scheduling"; the file last changed in 974c32f; 92.9 on main's headers
in the 43a17df baseline). The current player is read into a const local, currentPlayer, before the loop, as the
original loads currentPlayerSlotID before the loop's precheck, the way PlayArmoryIsFullWarning and
WarnIfPlayersGranaryIsFull read theirs (read in the condition: quick_diff 0.9259 -> 0.8519). maxUnitCount is an
int, so the (int) cast goes, and the loop counts unitID as main's unit loops do. reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateRabbit, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (209f6d4 at 69.32%,
49bd9c9 at 79.00%):
- units[] is indexed through unitID or through DAT_CurrentUnitSlotID::instance per statement, as a greedy
  quick_diff search picked them one statement per step;
- the US_UNKNOWN_203 path sits inside the `> 0` test, the slowdown is an int compare with (fixedRng & 0xf), and
  the death cases test animationFrame <= 0 first, the original's branch directions (structural_diff.py);
- tribe is an int local, so its load is movsx as in the original; it is read at the top, as reading it where it
  is used loses the match (quick_diff 1.0000 -> 0.5216);
- when hasUnitReachedDestination returns TRUE in the movement case, animationCycleNumber and substate are written
  through a local Unit pointer. The scheduler (Schedule, called from dop2_PreEncode) cannot tell those two stores
  apart from the state.generic store through units[unitID] and gives each a memory edge to it, so they come before
  pop edi and pop edi before the third store, as in the original; with all three through units[unitID] the stores
  are independent and pop edi went first. Found with cdb: tuple_dump.py shows the block in source order at
  dop2_PreEncode entry and reordered when it returns, and a breakpoint on DagMakeDep lists the edges;
- states 117, 203, 204 and 209 use the US_UNKNOWN_<n> names of the header commit.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateDeer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (f7d126f at 36.18%,
6458bca at 64.64%, cases in jump-table order):
- units[] is indexed through unitID or through DAT_CurrentUnitSlotID::instance per statement, as a greedy
  quick_diff search picked them one statement per step; in states 204 and 205 the two forms were picked over
  every combination: 204 tests substate through the slot, as 203 does, so its substate is not kept in a register
  for the increment and the two states share one idle tail as in the original;
- when hasUnitReachedDestination returns TRUE, animationCycleNumber and substate are written through a local Unit
  pointer, so the two stores stay ahead of the epilogue's pop edi as in the original (the same lever as in
  UpdateRabbit);
- in both seated branches of state 205 the frame-0 path sets DAT_UnitHasBecomeIdle and falls through to the shared
  idle block instead of repeating it and returning: the original has that block copied into each path;
- frameOffset is declared at the top: the original holds its 1 in edx from the prologue, where it also adds it to
  deerCount (declared in the movement case: quick_diff 0.8300); tribe is read at the top too (just before the
  switch: 0.9122);
- states 117, 202 to 205 and 209 use the US_UNKNOWN_<n> names of the header commit.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateMerchant, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (0adb264 at 53.39%,
8372c08 at 63.80%), under /GL:
- every call that takes the unit passes DAT_CurrentUnitSlotID::instance, and units[] is indexed through unitID or
  through the slot per statement, as a greedy quick_diff search picked them one statement per step;
- in state 3 the three stores after the destination test go through the slot, which the original reads again
  after setDestinationForUnit, and gfxNumber and field_0x30_animRelated are written through a local Unit pointer,
  so they come after the stateBasedSpeed store as in the original;
- states 2 and 3 are US_STATE_2Unk and US_STATE_3Unk, and state 2's fade step is written as -2 (the byte the
  original stores, 0xFE; same code);
- compiler-flags-gl.txt lists UpdateMerchant.cpp: 100% with the line, 78.0% without it. The original keeps values
  in registers across checkIfCitizenUnitIsAliveBasedOnState and hasUnitReachedDestination, which main's list
  already has, and reads DAT_CurrentUnitSlotID again after activateTraderState, which stays off the list: with its
  body visible the slot stays in esi across the call (90.0% instead of 100%).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UI::Rendering::RenderAnimatedTutorialFloatOverlay, from 0287fe5 on reimpl/batch1-development, in the form
TheRedDaemon worked out in his review: x and y step down by 8 and 32 before the per-resolution offset, and by 28
and 69 after the draw buffer is saved, and the offset comes from a chain of plain ifs. The original pushes the
trailing 0 before it computes the other arguments, and this form gives that order; one subtraction of 36 and 101
loses the match (quick_diff 0.8035 in the first place, 0.6356 in the second), so the two steps stay, with a
comment. The 1680x1050 offset is the original's 4050, where every other offset is the screen height - 600; a fixme
says so.

The function is Gynt's: 5236e98 (60.79%) and 63cc49f on his fork's reimpl/UI-Rendering-development, and
e8e27aa (60.8%) on reimpl/UI-Rendering (his sourcehold#255); ce6bd1e took it to 67.2% on reimpl/batch1-development.

Co-authored-by: TheRedDaemon <66257843+TheRedDaemon@users.noreply.github.com>
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 14, 2026
Main's Ghidra sync 1b36c0a has this PR's six UnitState values, so the
PR no longer changes a header.

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 17, 2026
@kotijekriv

Copy link
Copy Markdown
Contributor Author

Main is merged in now (e086ba7). Its Ghidra sync 1b36c0a has this PR's six UnitState values, so the PR no longer changes a header and the declaration check passes.

Because of the symbol-count effect gynt found on #366, we measured again after the merge. This was a full build of main e086ba7 with this PR and #410 merged on it, against a full build of main alone.

  • All five functions here read 100.0%, raw and normalised.
  • So do UpdateMerchant's nine reimplemented callees.
  • No function on main reads lower than in the baseline.

MapEditorLandscaping's GeneralButtons action handler (0x004314E0) doesn't compile on main since the sync retyped editorRockType and rockOrientation. #366 changes that file, so it was left out of both builds.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant