Skip to content

Reimplement 5 UI::MenuItems building menu and options functions at 100% - #344

Merged
gynt merged 5 commits into
sourcehold:mainfrom
kotijekriv:reimpl/batch12-100
Nov 6, 2026
Merged

gynt merged 5 commits into
sourcehold:mainfrom
kotijekriv:reimpl/batch12-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Five UI::MenuItems functions at 100%, each with its status line: the barracks' and the mercenary post's unit portraits, the build menu's delete tool, the game speed slider and the buttons of the building availability editor.

All five continue drafts on gynt's and 6and8's forks (reimpl/UI-MenuItems and -development, at 68-99% there). Every commit body says what changed against the draft and why. No header changes.

Other PRs: the ready #342 and #343 (the fix for main's 15 files), and the drafts #313, #318, #321, #327 and #328.

Built on top of main (d972299, and it merges cleanly into 57d3334) with the bundled MSVC1400-SP1, natively on Windows, against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a). The branch was built and measured on its own, with the struct and function resolvers enabled locally (not committed), using sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. None of it has been run in game.

It went through the same checks as #342: the points from the earlier reviews, a fresh-eyes review of every file (the behaviour fixes checked against the original's disassembly; its 22 points applied and measured), openshc-pr-check with no errors, and status_regression.py, which finds nothing below a known score on main or any fork.

Behaviour fixed compared with the drafts

Read from the original's asm; the commit bodies give the details.

  • BarrackUnitPortraits: when DAT_EnoughGoldForRequestedUnit is not set, it clears DAT_ButtonCurrentlyInteracting and returns. The draft cleared it and went on to draw the portrait.
  • DeleteAction: without a new click, deleting goes on while the button is held over a wall or moat; otherwise the two flags are cleared and it returns (the jumps at 0x0043887A and 0x0043888A). The draft returned in both cases.
  • BuildingAvailability Buttons render: both button groups index BuildingAvailabilityStructIndexForNameArray with the button number less 1000 or 2000, as the action handler does (the draft used field1234_0x312c / field1224_0x218c with the bare number, which reaches the same table). A row whose mapper has no menu (anything but the fletcher, poleturner, blacksmith and the two barracks) only sets ButtonUnknownZero and returns; the draft went on to RenderScenarioButtonWithText.
Class Function Address Raw Notes
BuildingAndStatusMenu MenuItemRenderFunction_BuildingAndStatusMenu_MercenaryPostPortraits 0x00466AB0 100.0% Finished from the draft on gynt's fork (70.0%).
BuildingAndStatusMenu MenuItemActionHandler_BuildingAndStatusMenu_BarrackUnitPortraits 0x00466950 100.0% Finished from the draft on gynt's fork (68.4%); the gold fix above.
BuildMenu MenuItemActionHandler_BuildMenu_DeleteAction 0x004387D0 100.0% Finished from 6and8's draft (78.7%); the wall/moat fix above. The cursor shows the delete as blocked with an enemy within 30 (15 outside a solitary game).
GameplayOptions MenuItemRenderFunction_GameplayOptions_SpeedSlider 0x00492080 100.0% Finished from the draft on gynt's fork (94.1%); one unused include, below.
BuildingAvailability MenuItemRenderFunction_BuildingAvailability_Buttons 0x004BB0D0 100.0% Finished from the draft on gynt's fork (99.3%); the two fixes above. Only the weapon workshops and the two barracks have a menu.

Kept as they are, and why

Each was measured; a one-line comment in the code says why.

  • Both portraits:
    • the low bound returns through its own copy of the shared tail. As one range test MSVC folds the two signed compares into one unsigned compare (quick_diff 0.8738 -> 0.7122, reccmp 100.0% -> 70.0% for the mercenary post; 0.8731 -> 0.7449 and 68.4% for the barracks). Nested ifs, the reverse order, literals, an enum cast, an index local, !(a || b) and > 69 && < 77 fold the same way;
    • a guarded if instead of an early return, as in EngineerAndMonkPortraits on main (early return: 0.8738 -> 0.8350 and 0.8731 -> 0.8325);
    • the store through the array before the pointer (through the pointer: 0.6400 and 0.6425).
  • DeleteAction:
    • the enemy distance is a local set by an if/else before isEnemyTooCloseUnk, as in RepairBuildingButton (in the call: 0.9679 -> 0.9116; a ternary local ties);
    • currentPlayerSlotID read from the field each time (as a local: 0.9157 -> 0.8072);
    • tileX read before tileY (y first: 0.9679 -> 0.9598);
    • two of the four height sums are terrain first, the order the binary loads them in (all building first: reccmp 100.0% -> 97.6%).
  • SpeedSlider includes OpenSHC/UI/Enums/MenuItemRenderFunctionType.hpp, which its code doesn't use. With the struct resolvers on and no other enum declared in the file, MSVC swaps the registers of the thumb's two loads (DAT_ButtonX and thumbXPos): quick_diff 0.9535 -> 0.9070, reccmp 100.0% -> 94.1%. VolumeSlider on main gets the same from SoundOptions.hpp, which includes SoundMenuClickType.hpp, and matches as written. MenuModalType, MenuItemType or ScreenResolutionEnum work too; BOOLEnum (a typedef), BGR24 and RGB15 don't, and operand order, names and the callee make no difference. We picked the render-type enum because this is a slider render. If you'd rather not have the include, the function goes in at 94.1%.
  • Buttons render:
    • one call per colour, which MSVC merges into the original's one (one call with a colour local: 0.9000 -> 0.7763 for the toggle text, 0.8018 for the "!");
    • the toggle text's number set in each branch of an if/else, for the original's neg/sbb (a default overridden: 0.9000 -> 0.8545);
    • the text position worked out in each branch, y then x (once before the test: 0.9000 -> 0.8909, reccmp 100.0% -> 99.3%);
    • ButtonUnknownZero set before the switch (in a default: arm: 0.9000 -> 0.8869).
  • Enum gaps, written as casts with main's one-line comment: GameCommandType has no 97, EntityType no 30 and BuildingFailReasonEnum no 1000. Proposals: GCT_UNKNOWN_97 (next to GCT_UNKNOWN_98), ET_UNKNOWN_30 (also spawned by spawnEraserTileEffect, spawnCrowForBuilding and spawnProjectileImpactDebris) and BFRE_UNKNOWN_1000. DeleteAction's DAT_GameCommandParam1 values 50 and -1 have no name yet.

Proposed, not changed here

  • INT_00df3350 is the index of the hovered unit in the barracks or mercenary post menu (0..6, from unitType - UT_E_ARCHER or - UT_A_ARCHER), -1 when none (Unknown_Min1); BarracksWeaponAvailability reads EuroUnitResourceCosts[it]. Something like INT_HoveredRecruitIndex?
  • ViewportState::field_0x0 is tested before every setupMouseTileXY (DeleteAction, HandleWallTerrainMouseDrag, renderPreviewAtMouseLocation, ReturnToBuildMenu, viewportAndScrollingRelated). It reads as "the mouse is over the map view"; a BOOLEnum if every store is 0/1.
  • BuildingNameRelatedStruct::field_0x4[3]: [0].identifier1 is the row's mapper (both BuildingAvailability Buttons functions switch on it with M_MAPPER_*).
  • DAT_DisableMercPostPortraits / Helpers::DisableMercPostPortraits() read the wrong way round: the merc post draws its portraits only while the global is non-zero, and DisableMercPostPortraits() sets it to 1.
  • M_MAPPER_BARRACKS_EURO (86) and M_MAPPER_BARRACKS_ARAB (87) in MappersEnum have their names swapped: 86 is the mercenary post (BT_MERCENARYPOST maps to 86), and the original's jump table sends 86 to modal 0x75, the Arab troops' dialog (handler and render use SEC_MercRecruitable). The MMT_DISABLE_* names and the menu names are right. This PR uses the enum's names as they are.
  • TileMapState::demolishBlocked, placementOnWall and placementOnMoat only ever hold 0 or 1 and read as flags: BOOLEnum candidates (now undefined4).

🤖 Generated with Claude Code

kotijekriv and others added 5 commits November 6, 2026 10:37
MenuItemRenderFunction_BuildingAndStatusMenu_MercenaryPostPortraits, from gynt's draft on the fork branch
reimpl/UI-MenuItems-development (55.6% there; 70.0% as a draft on batch4-development). The parameter is named
unitType, as GetUnitRecruitPermission takes it. The button's stateTransitionTimeBaseUnk_0x18 is cleared through the
array and the two calls go through a pointer to the button, which gives the original's shared offset (the pointer
for the store too: quick_diff 0.8738 -> 0.6400). Like the other getPictureNumberInGm callers it needs /GL (the
button pointer stays in ecx across the call), so the file is in cmake/compiler-flags-gl.txt, next to
getPictureNumberInGm.
Shape: the hovered portrait's 70..76 (Arabian mercenary) test returns through its own copy of the shared tail when
unitType is below 70, then tests the upper bound; MSVC merges the copy with the tail. As one range test (`unitType
>= UT_A_ARCHER && unitType <= UT_A_FIRETHROWER`) MSVC folds the two signed compares into one unsigned compare:
quick_diff 0.8738 -> 0.7122, reccmp 100.0% -> 70.0%. Nested ifs, the reverse order, literals, an enum cast, an
index local, `!(a || b)` and `> 69 && < 77` all fold the same way; both bounds returning on their own: 0.7070.
The drawing is guarded by DAT_DisableMercPostPortraits rather than returning early, as in EngineerAndMonkPortraits
on main (early return: quick_diff 0.8738 -> 0.8350). DAT_ButtonUnknownZero is set to TRUE/FALSE, as a BOOLEnum
since a9401a7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemActionHandler_BuildingAndStatusMenu_BarrackUnitPortraits, from gynt's draft on the fork branch
reimpl/UI-MenuItems (44.1% there; 68.4% as a draft on batch4-development), in the form of the
MercenaryPostPortraits render: the European units 22..28, IsEuroUnitRecruitableUnk in place of
GetUnitRecruitPermission and the greyed picture through renderGM. Despite the name it draws the barracks portraits.
The parameter is named unitType, as in IsEuroUnitRecruitableUnk. It needs /GL like the other getPictureNumberInGm
callers, so the file is in cmake/compiler-flags-gl.txt.
One behaviour fix: without DAT_EnoughGoldForRequestedUnit the original clears DAT_ButtonCurrentlyInteracting and
returns; the draft cleared it and went on to draw the portrait.
Shape: as in the MercenaryPostPortraits render, the lower bound of 22..28 returns through its own copy of the
shared tail; as one range test MSVC folds it into one unsigned compare: quick_diff 0.8731 -> 0.7449, reccmp 100.0%
-> 68.4%. The same guarded draw (early return: 0.8731 -> 0.8325) and the store through the array (through the
pointer: 0.8731 -> 0.6425).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemActionHandler_BuildMenu_DeleteAction, from 6and8's version on their fork's ci/postmerge (78.7% there;
gynt's draft on archive/UI-MenuViews-development-second-pass is at 55.9%).
One behaviour fix: without a new click the original goes on (drag-deleting) while the button is held over a wall or
moat, and only clears the two flags and returns otherwise (jumps at 0x0043887A and 0x0043888A); the draft returned
in both cases.
Shape: the player slot read from the field each time (as a local: quick_diff 0.9157 -> 0.8072); the enemy distance
in a local before isEnemyTooCloseUnk, in RepairBuildingButton's if/else form (inline in the call: 0.9679 -> 0.9116;
the if/else and a ternary local tie); tileX read before tileY (y first: 0.9679 -> 0.9598); and two of the four
height sums as terrainOrClimbHeight + buildingHeight, the order the binary loads them in (the draft's order: reccmp
100.0% -> 97.6%). The fail reason is read from the field each time (a local ties). Names for the literals:
Game::GM_EDITOR and GM_SIEGE_THAT, Game::GM_SOLITARY, TEXT_MISC2, TEXT_PANEL_FEEDBACK, FX_DEATH_CLUB2 and
BFRE_DEFAULT_CANT_PLACE_THAT_THERE (the BuildingFailReasonEnum include leaves the code as it is). Command 97,
entity type 30 and fail reason 1000 have no constants in GameCommandType, EntityType and BuildingFailReasonEnum
yet, so they stay numbers with main's one-line comment (as in spawnRandomFireEffectOnBuilding).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemRenderFunction_GameplayOptions_SpeedSlider, from gynt's draft on the fork branch reimpl/UI-MenuItems (81.4%
there; 94.1% as a draft on batch4-development), written as SoundOptions_VolumeSlider and NewInvasion_RepeatSlider
(both on main), the same slider with renderNumberToScreen2 for the speed.
Shape: the file includes OpenSHC/UI/Enums/MenuItemRenderFunctionType.hpp (MIRFT_SLIDER_OR_SCROLLBAR is this
function's render type) although nothing in it uses the enum. Without one more enum declared in the translation
unit, MSVC swaps the registers of the thumb's two loads (the button's x and thumbXPos) when the struct resolvers
are on: quick_diff 0.9535 -> 0.9070, reccmp 100.0% -> 94.1%. VolumeSlider gets the same from SoundOptions.hpp,
which includes SoundMenuClickType.hpp: its source compiled in the SoundOptions namespace matches as it is.
MenuModalType, MenuItemType or ScreenResolutionEnum would do the same; BOOLEnum, BGR24 and RGB15 (already declared)
do not. Operand order, declaring the button's y first, names and the callee don't change it.
isDragged is a BOOLEnum, as in VolumeSlider and RepeatSlider (the same code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MenuItemRenderFunction_BuildingAvailability_Buttons, from gynt's draft on the fork branch reimpl/UI-MenuItems
(60.1% there; 99.3% as a draft on batch4-development), without the gotos. Two behaviour fixes:
- both button groups read BuildingAvailabilityStructIndexForNameArray with the button number minus 1000 or 2000, as
  the action handler does (the draft indexed field1234_0x312c and field1224_0x218c with the bare button number,
  which lands on the same table);
- a row whose mapper has no menu (anything but the fletcher, poleturner, blacksmith and the two barracks,
  M_MAPPER_* as main's action handler names them) only sets ButtonUnknownZero and returns, as in the original's
  jump table; the draft went on to RenderScenarioButtonWithText.
Shape: the toggle text's number comes from a short local and an if/else, which gives the original's neg/sbb; the
text position is worked out in each branch, y then x, as in SaveLoadMap's Buttons render on main, so the last
argument's push lands between the halving of the width and the add of DAT_ButtonX as in the binary (worked out once
before the test: quick_diff 0.9000 -> 0.8909, reccmp 100.0% -> 99.3%; x then y ties).
Also kept, with their numbers: one call per colour, which MSVC merges (one call with a colour local: 0.9000 ->
0.7763 for the toggle text, 0.8018 for the "!"); ButtonUnknownZero set before the switch and cleared in the cases
(in a default: arm: 0.9000 -> 0.8869); textID set in each branch (a default overridden: 0.9000 -> 0.8545). The
MappersEnum include leaves the code as it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gynt
gynt added this pull request to the merge queue Nov 6, 2026
Merged via the queue into sourcehold:main with commit b80113a Nov 6, 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