Skip to content

Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer - #416

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

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

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Five more functions at 100%, with no header changes. Three are UI functions from our draft #328, and two are Map::Units functions from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), by way of our unpushed units dev branches. All five start from gynt's drafts; each commit cites the drafts it builds on and credits him as co-author. Two of them count as 100% under the rule gynt set in #414 (a function whose only difference is register allocation), and for those the raw reccmp number and the evidence are below.

Function Address Before After
InGameMenu::MenuItemRenderFunction_InGameMenu_MainButtons 0x004643F0 our draft #328, 49.0% (ee10c60); gynt's 47.63% (616eb44; 57.73% at 793ac41) 100.0%, under /GL
Roundtable::MenuItemActionHandler_Roundtable_Main 0x004AEA50 our draft #328, 71.2% (15884ad); gynt's 67.08% (a04df2a) 100.0%, under /GL
BuildingAndStatusMenu::MenuItemActionHandler_BuildingAndStatusMenu_BuySellMenuButtonsAndHands 0x00467040 our draft #328, 93.3% (60ea82a); gynt's 83.81% (30ace91) 100% per #414 (reccmp 93.9%)
Units::UpdateGhost 0x00543B80 gynt's draft 60.35% (e248d10; 52.63% at 9fd4871) 100.0%, under /GL
Units::UpdateHopsFarmer 0x00552F80 gynt's draft 50.99% (4c7f4b4) 100% per #414 (reccmp 99.7%), 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.
  • The three UI functions: an all-on build on main f489b72 (2,442 functions): MainButtons and the Roundtable handler 100.0% raw and normalised, BuySell 93.9% raw. The only main function that moved was InitBlendFilterArraysUnk, normalised 95.0% to 100.0% (97.9% raw both times). The review fixes since that build compile to the same code.
  • The two units: a build of main's whole /GL list plus the new lines, every resolver of those files on: UpdateGhost 100.0%, UpdateHopsFarmer 99.7%, its three callees 100.0%, nothing else moved.
  • Full build: main f489b72 with this branch and the other ready PR's branch, against a full build of main alone, every function resolver that has a .cpp switched on (2,449 functions against 2,439). All five read as above, and so do the /GL callees: Hops', isAIPlayer, getPictureNumberInGm, and Ghost's hasUnitReachedDestination and makeUnitStopWalkingByClearingPathProgressState. No function on main reads lower. InitBlendFilterArraysUnk reads higher normalised (95.0 to 100.0, raw unchanged), as with this branch alone. UI::Menu::handleMenuItems and Constructor_Menu show the known 0x11000000 naming artefact in the larger build: their instructions are the same, reccmp names the constant after a symbol of our DLL that sits at that address, and both read 100 with this branch alone. MapEditorLandscaping's GeneralButtons handler, which doesn't compile on main since the last sync, was left out (Reimplement 88 Map::TileMapState functions at 100% #366 changes it). That build had Hops before its last style pass and with UpdateWorker_ConsiderTakingABreakUnk on the list; the final Hops reads the same 0.9971 in quick_diff, and that callee was dropped from the list because it made no difference.
  • This branch's head alone (2c82df2), all-on build, against main f489b72 (3458fea only adds files under tools/): MainButtons, the Roundtable handler and UpdateGhost read 100.0% raw and normalised, UpdateHopsFarmer 99.7% (90.9% normalised) and BuySell 93.9% (85.9% normalised), with the same differing lines as described below. No function on main reads lower, apart from the 0x11000000 naming artefact on handleMenuItems and Constructor_Menu.
  • Please measure with both resolver switches on (the Globals and the function resolvers), as our numbers are. With its own resolver off, MainButtons reads 83.8% under /GL: nothing takes its address then, so LTCG gives it a private calling convention and drops the load of its argument.
  • Checks: openshc-pr-fix finds nothing to change, and openshc-pr-check gives 0 ERROR.
  • Not run in game.

The two at 100% per #414

The maintainers haven't settled this on #414: gynt set the rule, and TheRedDaemon wrote that he'd accept such functions for now as non-100%. If the status lines should carry the raw numbers (93.9% and 99.7%) instead, say so and we'll change them.

  • BuySell, reccmp 93.9%: ten line pairs differ. In eight only the register names differ: the two imul ..., 0x39f4 player-index computations use ecx and edx the other way round, and one load and one store use edx where the original has eax. The other two are jnes whose displacement is one byte off, because the original's mov [global], eax takes eax's shorter moffs encoding. Those two displacements are why reccmp's own register-swap check (effective) doesn't pass it yet. Same instruction count, same mnemonics in the same order, same memory operands. diff_triage's register-blind ratio is 1.000 (0.859 raw).
  • UpdateHopsFarmer, reccmp 99.7%: one hunk, at 0x005537BD. The original pushes the owner for getBuildingThatCanStoreThisResource with movsx ecx, [owner]; push ecx, ours with eax: the same two instructions and memory operand. Owner locals, int or short casts, and reading it through unitID or the slot all tie or lose, and a symbol-count sweep (0 to 95 dummy typedefs) reads the same at every count.

What got them to 100%

  • MainButtons: rewritten from the binary, with gynt's draft for the names. The original keeps the button's data pointer in ecx across getPictureNumberInGm (0x004630B0), which only LTCG can know is safe, so it needs /GL; without it the zero, the one and the picture offset land in other registers (80.8%).
  • Roundtable handler: from the binary. The seats come from DAT_RoundTableOrderArray (the decompiler had mapSendRequestStatePerPlayer + button + 26), the group numbers are char reads into ints, the highest group is a plain loop over players 1 to 8 that MSVC unrolls into the original's eight copies, and the buttons after the seat logic are an if chain with returns. Under /GL, reading the chosen lord's seat before the target's gives eax and ecx the original's roles. 87.2% without /GL in every form.
  • BuySell: what is left is register allocation only (above). Two scratch-register choices, other source shapes, a plain member call and /GL all tie or lose (/GL drops it to 80.2%).
  • UpdateGhost: owner is read once at the top through DAT_CurrentUnitSlotID::instance into an int const local, as the original keeps it in ecx up to findNextReachableDefensiveBuildingForPlayer. hasUnitReachedDestination and the final makeUnitStopWalkingByClearingPathProgressState get the slot, and in US_IDLEUnk everything from the destinationNeeded store after the timeGetTime test indexes units[] through it, as the original reads the slot again after those calls.
  • UpdateHopsFarmer: every unit access goes through the slot with the worker-helper callees visible, the states are one else-if chain, and a block-level quick_diff search put the storage lookup and addResourceToStockpile back on unitID. In the delivery state, the can-store and reachable-storage paths are the then-arms of their tests.

Behaviour fixed compared with the drafts

  • MainButtons: in the barracks and the mercenary post the original greys the buttons above -10 except the scribe (71) and -4; the draft greyed those at -10 and below. The greys of -1, -5 and -6 set the flags and go on to the draw, so the button is still drawn; the draft returned at once for -1 and one -5 case.

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

  • MenuItemRenderFunction_InGameMenu_MainButtons.cpp: 100.0% with it, 80.8% without. getPictureNumberInGm is on main's list already.
  • MenuItemActionHandler_Roundtable_Main.cpp and isAIPlayer.cpp: 100.0% with them, 87.2% without. Only isAIPlayer has to be visible.
  • UpdateGhost.cpp: 100.0% with it, 49.3% without, as hasUnitReachedDestination and makeUnitStopWalkingByClearingPathProgressState (both on main's list) have to be visible.
  • UpdateHopsFarmer.cpp and three callees it needs visible: ComputeGoodsProduced (quick_diff without it 0.9599), IncrementAndOptionalUpdateAVValueRelated (0.8182) and SetStateToFreetimeWalking (0.9899); with none of them on the list Hops reads 0.5950. All three are 100% on main, stay 100.0% on the list, and no other file on the list calls them. UpdateWorker_ConsiderTakingABreakUnk made no difference and stays off.

Kept as they are

  • MainButtons: the GM_CRUSADER_TUTORIAL || next to the != GM_CAMPAIGN_MISSION tests, and the inner != GM_SIEGE_THAT that repeats the outer one, look redundant, but the original makes both compares (0x004644AC and 0x0046459F). isSkirmishTrail != TRUE stays because the original compares the flag with 1.
  • Roundtable handler: it reads the seats again rather than keeping them, and clears the state only when it is set, as the original does.
  • UpdateHopsFarmer: the (char) byte-table reads and the (short) operands and stores, as in main's setFacingDirectionToward*.

What some placeholders hold

  • DAT_00df423c: the chosen lord's seat at the round table.
  • INT_00df4244: set when that seat is clicked again.
  • DAT_00df4240 / DAT_00df4298: the rat's / the sultan's genie line has been played (BOOLEnum candidates).
  • DAT_GameCore.field_0x6c: greys button -5 in campaign missions.
  • farmGrowState: holds the farmer's next UnitState (3 or 5), not a grow state.

Proposed, not changed here

  • An enum for PlayerData::storageMarketFailState, whose values 1 to 10 BuySell sets (the market's failure messages).
  • Two numbers without a BuildMenuTabType value: MainButtons compares the open tab with 48, which is BuildingsAndStatusMenuTabType::BASMTT_STOCKS, so the source reads it through activeMenuTab.tabType; and the 47 at MainButtons :122 (DAT_MenuOptionsActionParameter == 47) has no enum at all.
  • char for byte fields read as signed: Unit::disappearFadeAlphaCountdown and the AnimationFrameData frame tables are byte in the headers, but the original compares them as signed. As char the casts could go.

Three functions leave #328

  • InGameMenu::MenuItemRenderFunction_InGameMenu_MainButtons (0x004643F0)
  • Roundtable::MenuItemActionHandler_Roundtable_Main (0x004AEA50)
  • BuildingAndStatusMenu::MenuItemActionHandler_BuildingAndStatusMenu_BuySellMenuButtonsAndHands (0x00467040)

#328 gets one commit that removes their three .cpp files and puts their status lines back to main's (5ef28e7). The two units were never in our open drafts.

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 7 commits November 17, 2026 22:12
MenuItemRenderFunction_InGameMenu_MainButtons (0x004643F0, next commit) only matches with link-time code generation.
The original keeps the button's data pointer in ecx across ButtonGmData::getPictureNumberInGm (0x004630B0), which
leaves ecx alone, and reads the button's gm ID through it after the call; MSVC can only do that with both functions
under /GL. getPictureNumberInGm is already in this list on main.

MainButtons measures 100.0 with /GL (reccmp, all-on build on main f489b72: the Globals and every finished function
resolver on, so both functions' resolvers too). On main 3416498 it measured 80.8 without /GL, and 83.8 under /GL
with MainButtons' own resolver off: nothing then takes its address, and LTCG drops the load of its argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemRenderFunction_InGameMenu_MainButtons, rewritten from the binary with gynt's draft for the names
(e6a4dba, 7f2191d and b9a9e27 on his fork's reimpl/UI-MenuItems, 48.75% there; 793ac41 at 57.73% and
616eb44 at 47.63% on his reimpl/UI-MenuItems-development, 616eb44 being his newest reimplement commit on the file)
and our mechanical pass of it in sourcehold#328 (ee10c60, 49.0%; quick_diff 0.6164 on main). The function leaves sourcehold#328 with
this commit.

reccmp 100.0 (normalized 100.0) in an all-on build on main f489b72 under /GL (the cmake commit before this one),
which has its function resolver and getPictureNumberInGm's (0x004630B0) on; 80.8 without /GL (main 3416498). The original keeps the button's data pointer
in ecx across that call, which only LTCG can know is safe; without it the pointer has to survive the call some other
way, and the zero, the one and the picture offset go to ebx, ebp and edi in another order than the original's.

Fixes against the draft:
- In the barracks and the mercenary post the original greys the buttons above -10 but the scribe (71) and -4; the
  draft greyed those at -10 and below (616eb44 turned the bound round).
- The greys of -1, -5 and -6 set the flags and go on to the build-menu test and the draw, so the button is still drawn;
  the draft returned at once for -1 and for one -5 case.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemActionHandler_Roundtable_Main (0x004AEA50, next commit) only matches with link-time code generation. It calls
GameSynchronyState::isAIPlayer (0x004010B0) for each seat, and isAIPlayer changes only eax; with both under /GL MSVC
knows that, and gives the handler the original's registers (eax for the chosen lord's seat, ecx for the button where
the seats are read). Without /GL the handler reads 87.2 in every source form tried.

isAIPlayer is 100% on main with its function resolver on and stays at 100.0 under /GL (reccmp, all-on build on main
f489b72, built with the handler). It is the only callee the handler needs there: on main 3416498, with the
handler's other five finished callees (resetMouseState2, nextRandomNumber1, playWAVSFX, scheduleSFXVariation,
shouldSoundXNotBePlaying) also under /GL, the result was the same 100.0. No other file in this list calls isAIPlayer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemActionHandler_Roundtable_Main, from gynt's draft (c53b2b9 and 18ddd79 on his fork's reimpl/UI-MenuItems,
61.09% there; 65013b9 at 66.46% and a04df2a at 67.08% on his reimpl/UI-MenuItems-development, a04df2a being
his newest reimplement commit on the file), our mechanical pass of it in sourcehold#328 (15884ad, 71.2%) and our own rewrite
from the binary at 87.2% (local, never pushed). The function leaves sourcehold#328 with this commit.

reccmp 100.0 (normalized 100.0) in an all-on build on main f489b72 under /GL (the cmake commit before this one),
which has its function resolver and isAIPlayer's on; 87.2 without /GL (main 3416498).

From the binary: the chosen lord's and the target's seats are read from DAT_RoundTableOrderArray (the decompiler had
them as mapSendRequestStatePerPlayer + button + 26), the group numbers are char reads into ints with no casts, the
highest group in use is a plain loop over players 1 to 8 (MSVC unrolls it into the original's eight copies, as it does
the group count of button 100), the new group is one local stored twice, and the buttons after the seat logic are an
if chain with returns. The last difference under /GL was which of eax and ecx held the chosen seat and the button: the
original's comes from reading the chosen lord's seat before the target's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemActionHandler_BuildingAndStatusMenu_BuySellMenuButtonsAndHands, from gynt's draft (7cbb7b8 and fb51a06
on his fork's reimpl/UI-MenuItems, 83.17% there; 30ace91 at 83.81% on his reimpl/UI-MenuItems-development) and
our pass of it in sourcehold#328 (74bdd12, 83.8%; 60ea82a, 93.3%). The function leaves sourcehold#328 with this commit.

Register allocation only, per sourcehold#414, so the status line records 100.0. reccmp's own score is 93.9 in an all-on build
on main f489b72, and diff_triage's register-blind ratio is 1.000 (0.859 before renaming registers): every
remaining difference is a register name. Ten line pairs differ. In eight, the two `imul ..., 0x39f4` player-index
computations use ecx and edx the other way round and one load and one store use edx where the original uses eax.
The other two are jumps one byte off, because the original's `mov [global], eax` takes the shorter moffs encoding
that edx has not. Same instruction count, same mnemonics in the same order, same memory operands.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateGhost, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (9fd4871 at 52.63%,
e248d10 at 60.35%), under /GL:
- owner is read once at the top through DAT_CurrentUnitSlotID::instance, into an int const local that the
  nonInteractiveCitizenCountUnk increment and findNextReachableDefensiveBuildingForPlayer's first argument use: the
  original loads it before the stateBasedSpeed store and keeps it in ecx up to that call (quick_diff with the /GL
  callees in the unit 0.9568 -> 0.9784; the local read through unitID 0.7071, a short local 0.5833);
- makeUnitStopWalkingByClearingPathProgressState at the end and hasUnitReachedDestination get the slot, and in
  US_IDLEUnk every statement from the destinationNeeded store after the timeGetTime test to the end of the state
  indexes units[] through it, as the original reads the slot again after playSFXAtLocation and timeGetTime. unitID
  is then not live across those calls, and the timeGetTime pointer gets esi as in the original (0.9424 -> 0.9568;
  starting one statement later 0.9429). The other statements use unitID or the slot as a greedy quick_diff search
  picked them one statement per step;
- states 108 and 2 are US_UNKNOWN_108 and US_STATE_2Unk;
- three stores in blocks that otherwise use unitID stay through the slot (no comment, per gynt on sourcehold#393): the
  US_IDLEUnk state store in US_DETERMINE_NEXT_STATEUnk (through unitID: quick_diff 0.9784 -> 0.9640), the
  field_0x30_animRelated store in US_MELEE_ATTACK (0.9712) and the clamp to 32 in state 2 (0.9712);
- compiler-flags-gl.txt lists UpdateGhost.cpp: 100% with the line, 49.3% without it, as hasUnitReachedDestination
  and makeUnitStopWalkingByClearingPathProgressState, both on main's list, have to be visible.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateHopsFarmer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (4c7f4b4 at 50.99%),
through an earlier version on our dev branch at 99.7%: every unit access goes through
DAT_CurrentUnitSlotID::instance with the worker-helper callees visible, the states form one else-if chain, and a
block-level quick_diff search put the storage lookup back on unitID; the solitary-or-not goods amount is an if/else
with the call in each arm; in the delivery state the can-store path is the then-arm of canBuildingStoreTheAmount
and the reachable-storage path the then-arm of buildingIsAccessible. Kept forms (quick_diff with the /GL
callees in the unit; the first and last with a one-line why, the slot indexing without, per gynt on sourcehold#393):
- the delivery state's else arm reads the slot into unitID again, as the original does (without it 0.9949);
- setDestinationForUnit in the walk-to-field state takes unitID, as the original keeps the slot across
  UpdateWorker_ConsiderTakingABreakUnk there (through the slot 0.9864);
- ownFarm reads workplaceBuildingID_1 again after the calls next to farm (reusing farm 0.9272).
Style on top, all the same code (0.9971 before and after): DNE_UNKNOWN_2 for (DestinationNeededEnum)2 and no using
line, no casts on the stores into short fields and on the coordinate operands, BOOLEnum results tested by
truthiness, ++/--, int const storage, no unused BOOLEnum include.

compiler-flags-gl.txt gets UpdateHopsFarmer.cpp and the three callees it needs visible, each 100% on main and
called by no other file on the list: ComputeGoodsProduced (quick_diff without it 0.9599),
IncrementAndOptionalUpdateAVValueRelated (0.8182) and SetStateToFreetimeWalking (0.9899); with no callee bodies at
all, i.e. off the list, 0.5950. UpdateWorker_ConsiderTakingABreakUnk makes no difference (0.9971 without it) and
stays off. reccmp in a build of main's whole /GL list with every resolver of those files on: UpdateHopsFarmer
99.7%, the three callees 100.0%, nothing else moved.

The one difference left is register allocation only (sourcehold#414): the owner pushed for
getBuildingThatCanStoreThisResource is loaded into eax where the original uses ecx, the same two instructions and
memory operand. Owner locals, an int or short cast, and reading it through unitID or the slot all tie or lose; a
symbol-count sweep (0..95 dummy typedefs, after the includes and at the top) reads 0.9971 at every count.

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

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