Skip to content

UI: the single-player game launch at 100% - #466

Merged
TheRedDaemon merged 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round46-100
Nov 22, 2026
Merged

TheRedDaemon merged 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round46-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

One function at 100%, written from the original's listing with gynt's draft beside it for the names. No header changes. No behaviour change against his draft; one index differs because main's header has moved since (below).

Function Address Before reccmp raw Normalised
Actions::LaunchSinglePlayerGameUnk 0x0042C620 gynt's 28.4% 100.0% 100.0%

gynt's draft goes back to ce7ebb0 (2026-10-02, "reimplement: SHC_3BB0A8C1_0x0042C620 25.26% Style pass, asm differs"). Its best score, 28.35%, is ca39d7b (2026-10-03, the same subject), where he merged the draft's split name buffer (char local_7e0[4] and a local_7dc[1004] that nothing used) into one of 1008 bytes. His 409e2a3 ("Split buffer merged", on sourcehold's own reimpl/UI-Core) makes the same change and says why: the compiler dropped the unused half, so the frame came out 0x3fc against the original's 0x7e8, and the map's name overflowed the 4-byte buffer. The newest version is 15e5712 (2026-11-02, "style: rule 12, import 76 string literals across UI/Rendering and Helpers") on his fork's reimpl/UI-MenuItems-development, whose status line reads "28.4% | Style pass, asm differs". reimpl/UI-Core (#256) has the draft at 26.3% (2f1c52c), from before the merge. Its reimpl/UI-Core-development has it at 27.1% (10280b1), with the 4-byte buffer again. The draft does not compile on today's main (32 errors, all from names and types that have changed since: below), so there is no number for it from our build.

Update (review)

  • 3162a96: the six "can be made" settings (MapPropertiesState's SEC_*Producible_save shorts and GameCore's *Producible_logic ints) and the nine euroRecruitableCopy_* flags are only ever 0 or 1 in the original, so they now take TRUE and FALSE. Main has no 16-bit BOOLEnum, so the short fields keep their type; no header changes. (Its commit message says the values are copied "between the two sets of fields": the shorts are only stored as 1 or 0, or toggled in the map editor, and read into GameCore's ints, never the other way.)
  • 7fce4ff: euroRecruitable is indexed by the unit type counted from UT_E_ARCHER, as main's IsEuroUnitRecruitableUnk reads it, so the seven tests are written euroRecruitable[UT_E_XBOW - UT_E_ARCHER] and so on.
  • The same code: the quick compile's listing is unchanged by each commit, and a narrow build of the head reads 100.0 raw and normalised with no diff entry. No struct field changes type, so the full build above stands.

Measured

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed). In the full build 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) is left out and its resolver stays off, as in main's baseline.
  • Narrow build: this file's code, alone. 100.0 raw and normalised, with no diff line.
  • Full build: an all-on build of an earlier cut of this branch on main 812d149 (the same code: the cuts since only change comments and write one 50 as a countof, which compiles to the same listing), with both resolver sets on: 0 failed of 2,424 compile steps, 99.57 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/, and cmake: optional wrapper units built from function-visibility.txt #399 and cmake: function-visibility.txt, which callers must see which callees' bodies #398 add the optional function-visibility units, off by default). The new function reads 100.0 raw and normalised there too, with no diff entry, and so does its caller on main, MenuItemActionHandler_SingleplayerMapChoice_ButtonsAndHands (0x0042D640), as before. Two functions on main read lower, and it is how reccmp prints a constant, not their code: Menu::handleMenuItems (0x004F6280) reads 97.9 (91.4 normalised) and Menu::Constructor_Menu (0x004F4100) reads 98.4 (95.2), both 100.0 before. Both compare a menu item's type with the constant 0x11000000, five compares in all. In this build's layout a symbol of our DLL lies at exactly the address 0x11000000, so reccmp prints the constant as that symbol's name on our side. The instructions are the same on both sides; UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435 and UI: the credits scroll's fade and the win/defeat window at 100% #437 describe the same two readings. Every other main function reads exactly what it read before (unrounded 99.5746 against 99.5759 raw and 99.5439 against 99.5492 normalised; with those two at 100.0 it would be 99.5761 and 99.5494, as UI: the minimap's terrain colours at 100% #461's build, which also adds one function at 100%, reads).
  • Instructions: the original's 650 (663 with its thirteen alignment fillers: eight lea reg, [reg], a nop and a mov edi, edi, and three short jumps over them), in its order; reccmp's diff for the function is empty. The four-entry jump table of the mission type's switch (0x0042D130) is the original's, entry for entry.
  • Checks: our local style checker gives 0 ERROR and 5 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

What the function does, and the draft

It starts a single-player game on a map. The original calls it from two places: the map choice's start button (MenuItemActionHandler_SingleplayerMapChoice_ButtonsAndHands on main, a jump at 0x0042D72B, with 0) and MenuItemActionHandler_General_LaunchOrQuitMultiplayerGameUnk (0x00494D17, in #442, with 1: a builder game, after it has set the menu view that gynt's newer export calls MVT_RESTARTING_CUSTOM_MAP). With 0 the map is the one picked in the map list; with 1 it is the one whose name standaloneFilename already holds.

  1. It asks for a slot (GCT_ASK_FOR_SLOT_ASSIGNMENT), keeping its own, sets everything to "can be made" and "can be recruited", builds the map's file name and loads the map. Behind the load the six weapon settings are set again, together with the game's own copies.
  2. The difficulty is stored for the mission type (0 just build, 1 economic, 2 siege, 3 invasion), a siege gets its chained outcome events, and the fourteen recruitable values are copied from the map with nine flags derived from them.
  3. Siege: the wildlife dies, slots 1 and 2 take their colours, and for slot 1 the map's siege engines are added to a new attack wave at 66, 100, 133 or 166% by difficulty. The attacker's twenty troop counts are the ones picked in the map choice (advanced mode), or 70% of the map's for slot 2. All peasants are deleted.
  4. Invasion on difficulty 1 to 3, on a map for which mapHasCertainEvent says no: the troop value of the player's own units is added up, and every fourth, third or second of them is deleted (in up to three passes) until a quarter, a third or half of that value is gone. From the map choice it is always the first. Its start button sets the difficulty to 1 for an invasion (and for "just build") right before the call (0x0042D71D, MenuItemActionHandler_SingleplayerMapChoice_ButtonsAndHands on main), and loadMap keeps it (it saves it at 0x004C62D2 and writes it back at 0x004C639D). For a deleted UT_S_MANGONEL or UT_S_BALLISTA the building on its tile has its tower engine type cleared.
  5. Every mission type but the siege has its twenty siege troop counts and the tunnelers zeroed. "Just build" removes the jester and the lady. With the player in slot 2, that slot's starting gold is paid out at once, and a map with mapU4Int1 starts with 600 gold.
  6. The view goes to slot 1's keep (or to the first signpost), and the first menu view opens: an invasion's description unless called with 1, otherwise the build menu.

Read block by block against the listing, 15e5712 does all of this the way the original does. (Its two strcats start from (char*)&local_7e4 + 3, Ghidra's way of writing "one byte before the buffer"; that holds only while the compiler puts local_7e4 right in front of the buffer, as the original's frame has it. Here they are strcat.) What a port of it to today's main needs:

  • The picked map's index. The draft reads DAT_ArrayOfMapIndices[scroll offset + selection - 1]. That was right for the header of its day, where the array started one entry later ([500] at 0x884). Since 12854bc main has it as [501] at 0x880, which is where the original reads it (0x0042C6B9: mov edx, [eax*4 + 0x1126e28]), so here the index has no - 1. With the - 1 kept, today's main would load the list's entry before the picked one. gynt's own newer export (8ca7861 on reimpl/Wilps-2026-10-18) has it without the - 1 too.
  • DAT_SiegeInformationArray is int[7][3] on main (7c6bb9e) where the draft's header had int[21]. This file takes it as 20 numbers in a row through a pointer to its first element, as main's MenuItemActionHandler_SingleplayerMapChoice_MapTable does.
  • The build menu's tab is written as buildMenuTab = BMTT_CASTLE, the union member for the build menu that opens next. The draft writes the same 10 as tabType = BASMTT_HUNTERSHUT.
  • Four renamed fields: SiegeUnitCounts::field_0x48 / field_0x4c, Building::defensiveTowerEngineType (the draft's containsSiegeMangonel1OrBallista2) and PlayerData::buildingEntryInfos[0] (the draft's keep).

What got it to 100%

Each of these was measured against the alternative it names:

  • The block order. In the original the advanced mode's copy falls straight into the peasant loop, and the 70% block, the invasion block and the zeroing stand much later, behind the keep's part of the function. That is what the compiler makes when the zeroing has its own if (type != 2) in front of if (type == 2) { peasants }. With the zeroing as that test's else arm, all blocks stay in the written order (140 lines of the listing differ).
  • The twenty siege troop counts are walked as an array in all three places. The original keeps the 70% loop as a loop and has the copy and the zeroing unrolled: the copy in three register lanes, the zeroing with a zero register of its own (xor eax, eax) although the function holds a zero in ebx. Written as loops, the compiler makes exactly that; twenty written zero stores take ebx instead.
  • The nine recruitable flags are cleared in front of the copy loop, as in MenuItemActionHandler_MapEditorProperties_MainButtons (UI: the map editor's properties buttons handler at 100% #441). Behind the loop, the loop's copies take the register that holds the zero, and the zero is no longer in a register from the first test of the argument on (the string loops then test with test cl, cl where the original has cmp cl, bl).
  • The two name buffers are char[1005] and char[1001]: a map name's 1001 bytes (GameCore::standaloneFilename, ResourceManager::mapNames) and four more for ".map". That the frame needs both is gynt's find (409e2a3, above). With 1000 each, the frame is 0x7DC against the original's 0x7E8.
  • The first six stores in the order crossbow, pike, sword, bow, spear, mace (in ascending order two of them move), and the map index as selection + scroll offset (the other way round swaps the two loads).
  • In the invasion block the unit counter is declared in front of the value total; the other way round the two are set to zero in the other order.
  • The last menu call is written in each arm; one call with a ? : argument computes the number without a jump (neg; sbb; and).
  • No file-local enum for the mission types. A four-name enum in front of the function, declared and not used once, changes two registers of the second map name call (eax and ecx swap). So the mission types are numbers with a comment, as in main's other files.

Things in the file that look odd and are the original's

  • The six weapon settings are set twice, before the load and behind it, and the first time in a mixed order.
  • The map's name is copied into a local buffer before the load and written back to standaloneFilename behind it (when called with 1).
  • The mission type is tested again inside the siege block (type == 2 && slot == 1 inside type == 2): the original has both tests (0x0042C9C9 and 0x0042CA16). Without the second one 30 lines of the listing differ.
  • siegeEngineCounts[3] goes to score 2 and [2] to score 3, and [5] to score 8: the same pairs as in MenuItemActionHandler_MapEditorProperties_MainButtons (UI: the map editor's properties buttons handler at 100% #441). By the names in gynt's newer export (below), the map's list has the siege tower before the battering ram and the wave's list has the ram first, and [5] is the wave's siegeEngineSlot8Count.
  • The tunnelers take the eleventh picked number, the same one the copy puts into siegeInformation.arabianArchers (0x0042CC7B and 0x0042CC8C store the same register).
  • if (type != 2) followed by if (type == 2): see the block order above.
  • total is first the sum of the troop values and then the value still to take away, and pass = 10 ends the passes from inside the unit loop: the original has one register for the first and the store of 10 for the second (0x0042CFCD).
  • counter declared in front of total, and the argument compared with 1 at the end (0x0042D0BB): see above. A truth test compares with zero there.
  • The jester's and the lady's words are cleared twice: for every mission type behind the name copy (0x0042C9CF, 0x0042C9D6), and again in "just build"'s removal (0x0042CCD4, 0x0042CD0E).
  • The player's slot is saved around the slot request and written back behind it (0x0042C637, 0x0042C655).
  • missionNumber1to20 = 28 (0x0042CD92): the original's number, outside the 1 to 20 that main's sound code tests the field for.
  • The jester and the lady are removed only for "just build", each only if the player's record points at a unit with the recorded id.

Checker CHECKs (not changed)

  • A pointer to a field (siegeUnits, to siegeInformation.archers): the three loops over the twenty counts need the struct as an array, and the header has twenty named fields (a finding below).
  • Two (short) casts on currentResources[RT_GOLD]: an int stored into the two short fields lastMonthsGold and beforeLastMonthsGold.
  • DAT_00b960dc is a placeholder name (a finding below).
  • Ten case labels are numbers: the mission type (0 to 3) and the difficulty (0, 2, 3 and 1, 2, 3). Neither has an enum on main.

Header findings (nothing changed here)

  • 0x0042C9B0 and 0x0042CF20 are typed function in src/precomp/addresses-SHC-3BB0A8C1.hpp (no labels) and stand as Pending in the status list. Both are loop heads inside this function, which runs to 0x0042D12F: the second map name copy's loop and the head of the three passes, each behind alignment fillers. 0x0042D130 to 0x0042D13C (four /pointer lines, also Pending in the status list) are its jump table's four entries.
  • DAT_MapMissionType is an undefined4 with four values: 0 just build, 1 economic, 2 siege, 3 invasion (the comment in main's MenuItemActionHandler_ScenarioDescription_Main says the same). An enum in the header would name them in the eight files on main that use it, and in this one.
  • SiegeUnitCounts is walked as an array of 20 by the original (here three times; UI: the map editor's properties buttons handler at 100% #441's MenuItemActionHandler_MapEditorProperties_MainButtons copies it field by field). An array view beside the named fields, as BarracksRecruitabilityShort has one, would do without the pointer. Main's header already has the map's own copy, MapPropertiesState::SEC_SiegeInformation, as int[20] (8125c61), and this file indexes that directly.
  • Index 10 of the siege troop counts (SiegeUnitCounts::arabianArchers) is the number this function also stores as SEC_Section1067.tunnelersCount. Main's MenuItemActionHandler_SingleplayerMapChoice_MapTable also writes 70% of the tunnelers into slot 10 of the same picked army (siegeUnits[10] = ... tunnelersCount * 7 / 10).
  • The parameter (the header's param_1) is 0 or 1 at both call sites; this file calls it useStandaloneFilename, and it could be BOOLEnum there.
  • DefensiveTowerEngineType has no value for 0. This file writes 0 (no engine) into Building::defensiveTowerEngineType, as Reimplement 33 Synchrony::Commands functions at 100% #415, One more Map::Units function at 100%: UpdateMangonel #445 and One more Map::Units function at 100%: UpdateBallista #451 do.
  • Names in gynt's newer export (8ca7861 on reimpl/Wilps-2026-10-18) for things this file uses under main's names: the function is LaunchStandaloneMapGame there, DAT_00b960dc is DAT_MenuFadeState, MVT_UNKNOWN_49_DOES_NOTHINGUnk is MVT_RESTARTING_CUSTOM_MAP, GameCore::gameSuspended is briefingOpenedFromInGame and WindowAndDirectDraw::unk_resetViewportRelated is pendingOverlayBltMode. pathfindingUpdate_0x4a8ab0 is clearObstaclesAroundSignposts. AttackInfo::inv_count is currentAttackWaveSlot and someSinglePlayerScore is waveSiegeEngineCounts. The six SEC_Section1067.siegeEngineCounts are catapults, trebuchets, siegeTowers, batteringRams, portableShields and siegeEngineSlot8Count. jesterIDUnk / someUnitIDSelfRef_2 are jesterUnitID / jesterUnitUID, and ladyIDUnk / someUnitIDSelfRef are ladyUnitID / ladyUnitUID. unitJesterRelated / unitLadyRelated are jesterEnabled / ladyEnabled, and PlayerData::startResources is pendingResources. selectedLordType_2Unk / selectedLordTypeUnk are singlePlayerMapLordType / localSelectedLordType, missionNumber1to20 is missionNumber, and SiegeUnitCounts::field_0x48 / field_0x4c are unknownUnitCount18 / unknownUnitCount19.

🤖 Generated with Claude Code

Actions::LaunchSinglePlayerGameUnk starts a single-player game on a map: it loads the map picked in the
map choice (or, called with 1, the map whose name standaloneFilename already holds), sets what can be made
and recruited, applies the mission type's rules and opens the first menu view. For a siege that is the
map's siege engines as an attack wave scaled by the difficulty, the attacker's troops (the ones picked in
the map choice in the advanced mode, otherwise 70% of the map's for a player in slot 2) and no peasants;
for an invasion on difficulty 1 to 3 (from the map choice always 1: its start button sets it), on a map
for which mapHasCertainEvent says no, it takes every
fourth, third or second of the player's own units away until a quarter, a third or half of their troop
value is gone. 663 instructions, written from the original's listing.

The fields it uses come from gynt's draft, under main's names today. The draft goes back to ce7ebb0
(2026-10-02, "reimplement: SHC_3BB0A8C1_0x0042C620 25.26% Style pass, asm differs"). Its best score, 28.35%,
is ca39d7b (2026-10-03, the same subject), where he merged the draft's split name buffer (`char
local_7e0[4]` and a `local_7dc[1004]` that nothing used) into one of 1008 bytes; his 409e2a3 ("Split
buffer merged", on sourcehold's own reimpl/UI-Core) makes the same change and says why. The newest version
is 15e5712 (2026-11-02, "style: rule 12, import 76 string literals across UI/Rendering and Helpers") on
his fork's reimpl/UI-MenuItems-development, whose status line reads "28.4% | Style pass, asm differs".
reimpl/UI-Core-development has it at 27.1% (10280b1) and reimpl/UI-Core (sourcehold#256) at 26.3% (2f1c52c);
both still have the 4-byte name buffer.
That draft does not compile on today's main: DAT_SiegeInformationArray is `int[7][3]` now, and two
SiegeUnitCounts fields, Building's tower engine field and PlayerData's keep entry have other names.

No behaviour change against the draft: read block by block, both do the same. One thing a literal port
would get wrong: the draft reads the picked map from DAT_ArrayOfMapIndices[scroll offset + selection - 1],
which was right for the header of its day (the array started one entry later); on main the array starts
where the original reads it, so the index has no `- 1`. gynt's own newer export (8ca7861 on
reimpl/Wilps-2026-10-18) has it without the `- 1` too.

What the match needed, each measured against its alternative:

- The two name buffers are char[1005] and char[1001] (the frame's size, as gynt found in 409e2a3).
- The first six "can be made" stores in the order crossbow, pike, sword, bow, spear, mace; in ascending
  order two of the stores move.
- The picked map's index as selection + scroll offset; the other way round swaps the two loads.
- The nine recruitable flags cleared in front of the loop that copies the fourteen recruitable values.
  Behind it the loop takes the register that holds the function's zero, and the zero is no longer in a
  register from the first test of the argument on.
- The siege numbers are zeroed by their own `if (type != siege)` in front of the siege's peasant loop. As
  the else arm of that loop's test, the three blocks before it stay in the written order; the original
  has them behind the keep's part of the function.
- The twenty siege troop counts are walked as an array in all three places (copy, 70%, zero). The header
  has twenty named fields; twenty written zero stores take the function's zero register where the
  original makes a new one.
- In the invasion block the unit counter is declared in front of the value total; the other way round
  the two are set to zero in the other order.
- The last menu call written in each arm; one call with a `? :` argument computes the number without a
  jump.
- The argument is compared with 1 at the end, as in the original; a truth test compares with zero.

The mission types (0 just build, 1 economic, 2 siege, 3 invasion) are plain numbers with a comment, as in
main's other files: DAT_MapMissionType has no enum, and a file-local one changes two registers of the
second map name call (declared alone, without a single use).

Measured in a narrow build on main 3458fea of this file alone (main is now 812d149: sourcehold#399 and sourcehold#398 add
only the optional function-visibility units, off by default), struct resolvers and its resolver on:
100.0% raw and normalised, no diff entry. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
kotijekriv and others added 2 commits November 22, 2026 15:40
Review: the six "can be made" settings (MapPropertiesState's SEC_*Producible_save shorts and GameCore's
*Producible_logic ints) and the nine euroRecruitableCopy_* flags are only ever 0 or 1 in the original: every
store is a 1 or a 0, a toggle with `xor 1` (the map editor) or a copy between the two sets of fields, and every
read tests against 0. They now take TRUE and FALSE. Main has no 16-bit BOOLEnum, so the short fields keep their
type; no header changes. One comment says what the nine flags are.

The same code: the quick compile's listing is unchanged, and a narrow build of this file reads 100.0 raw and
normalised with no diff entry.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review: euroRecruitable is indexed by the unit type counted from UT_E_ARCHER (main's IsEuroUnitRecruitableUnk
reads it as euroRecruitable[unitType - UT_E_ARCHER]), so the seven tests behind the copy loop are written
euroRecruitable[UT_E_XBOW - UT_E_ARCHER] and so on.

The same code: the quick compile's listing is unchanged, and a narrow build of this file reads 100.0 raw and
normalised with no diff entry.

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