Skip to content

UI: the minimap's drawing at 98.3% (draft) - #470

Draft
kotijekriv wants to merge 11 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round47-development
Draft

kotijekriv wants to merge 11 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round47-development

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

One function at 98.3%, written from the original's listing with gynt's draft beside it for the names. This is a draft PR: four instructions still differ (below), so others can continue from here. Three header changes, each in a commit of its own; gynt allowed them here on 11-27 ("Feel free to make the header changes if it leads to improvements. Then I can address them in Ghidra."): drawBorderBox's colour is a short, MiniMapDefinedData::PlayerColorColors is ushort[9][4][2], and a new enum MinimapDrawFlags names the flags. The PlayerUnitColors typedef from the last push goes again, since the field has the type now.

Function Address Before reccmp raw Normalised
MinimapViewState::drawMinimap 0x004B66C0 gynt's 32.8% 98.3% 91.6%

gynt's best version is 10280b1 on his fork's reimpl/UI-Core-development ("32.8% | Reimplemented, asm differs"). His own commits for the draft are 34548c6 (19.14%) and f3cc8dd (23.32%), on #256 c899be1 and 95d388a, and the newest version is e9bc49c (2026-11-02, "reimplement: swap if/else arms across the UI view and modal folders") on reimpl/UI-MenuItems-development (23.3%).

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).
  • Narrow build: this commit's file, alone: 97.6 raw and 90.1 normalised.
  • After main 1f87a13 merged in and the typedef moved to its header (a325841): the function's listing is the same instruction for instruction as before the move, and a narrow build reads 97.6 raw and 90.1 normalised again.
  • After main 655adce merged in, a narrow build after each header step: the merge 97.6 / 90.1 (the same listing as before), drawBorderBox's colour as short (81451bc) 98.3 / 91.6, the flags enum, the colour table and the code using both (eac9a8b) 98.3 / 91.6 with the same listing, the typedef's removal (9187994, the head) 98.3 / 91.6.
  • Full build: an all-on build of main 655adce and one of this head (9187994), both resolver sets on, the usual three functions left out with their resolvers off, and also 12 files that don't compile on main 655adce with every resolver on (the gate and moat functions, renderDebugDataCurrentPlayerData, TransformAndRenderPercentage): 0 failed of 2,906 and 2,907 compile steps; drawMinimap 98.3 / 91.6 there, the same as in its narrow build; every main function reads what it read before (2,922 functions, average 99.54 in both builds), apart from Menu::handleMenuItems and Menu::Constructor_Menu, which is the 0x11000000 printing reading described in UI: the minimap's second picture at 100% #465. All 21 files on main that call drawBorderBox keep their numbers with the short colour.
  • Instructions: 937 on both sides (946 with the original's alignment fillers: seven fillers, lea ecx, [ecx] at 0x004B6C1D, 0x004B6DCD and 0x004B6F1D, lea esp, [esp] at 0x004B6F59 and 0x004B70F7, mov edi, edi at 0x004B6DEE and 0x004B70FE, and two jumps over them). Compared with the stack slots' numbers taken out, four instructions differ (below); every one of the 322 stack slot uses is on the original's slot.
  • Checks: our local style checker gives 0 ERROR and 61 CHECK (below). Not run in game.

What the function does

It draws the minimap. First it redraws the minimap's own pictures when the map's orientation changed, and runs the step-by-step redraw that field_0x38 / field_0x34 drive (2% of the rows per call, with collectCliffEdgeTilesForClimbData at the start of each pass). Then it works out which part of the picture (400 rows of 200 pixels) shows: centred on the given offsets when flag 1 is set, and clamped to the map. It copies that part to the pencil surface, or to the loaded map's preview when the last argument is above 0. It draws every unit on a visible tile as a dot in its player's colour; siege engines take the current animation frame, animals a colour of their own, and enemy assassins out of sight are not drawn. With the wildlife hidden (Alt+R), only units with an owner are drawn, own team and enemies in two colours. Finally, with flag 2, it draws the viewport's box.

Behaviour against the draft

Read block by block against every version of gynt's draft:

  • Maps of exactly 200 tiles. The original subtracts its 100-column and 100-row offset for mapSize <= 200 (0x004B6827 cmp ..., 200; jg). Every version of the draft has < 200, so on a 200 by 200 map it read the picture 100 columns and 100 rows away from where the original reads it. The same slip as in renderMinimapTileColors (UI: the minimap's terrain colours at 100% #461) and renderMinimapTileStripWithLuminescence (UI: the minimap's second picture at 100% #465).
  • The viewport box. Where the box sticks out on both sides of the minimap, the original returns and draws none (0x004B722C and 0x004B7261, jge to the epilogue). 10280b1 and e9bc49c have two empty if (...) {} there and draw a clamped box. gynt has both returns himself in 8ca7861 on his reimpl/Wilps-2026-10-18.
  • Enemy assassins with the wildlife hidden. The original draws one up to and including distance 160 (0x004B7050 jle). Every version of the draft draws one only below 160. In the other mode the draft's test is right.
  • The minimap buffer is taken from this (0x004B66E8 lea edi, [esi + 0x44]), where the draft goes through the global instance. These are the same while this is that instance, and all five call sites (0x004B73A9, 0x004B74F9, 0x004B75D0, 0x004B76A7, 0x004B76D0) pass their own this on.

Not the draft's mistake: the screen tile table indexed - 10 / - 8, which was right for the header of its day.

What got it to 98.3%

  • The blit and the pixel stores are handwritten assembly in the original, the same forms as renderMinimapPreview and drawCurrentPixel on main. The blit is 30 instructions (0x004B6B1D to 0x004B6B79) and each pixel store 10 or 12: the original's handwritten instructions and nothing else, every operand a C++ local. Nothing of them can move to C++: they build the address with mov edx, ...; mul edx and add eax, eax, where the compiler writes imul and lea. The blit's block (it has labels) also brings the frame pointer; the draft forced it with #pragma optimize("y", off). The pixel stores come in two forms, each used twice; with two forms and two uses each they stay blocks here rather than a macro (a macro header under src/OpenSHC would also trip the declaration check).
  • The loop variables of the two unit loops are declared once, at the function's level. Declared in each loop, they take other stack slots: with one declaration the two loops' uses add up, and the column counter and the row length land in two dead parameters' slots, as in the original. width and height count tiles from the point where they are divided, as in the original, which keeps the counts in their stack slots.
  • The variables the original has: the viewport box's position in two locals, and the two offset parameters as the tile position from there on. MC_MAP_SIZE_400 - mapSize is written out each time (a local for it changes the stack slots).
  • The first ten initialisations in an order found by a search over the orders (most orders move the first stores).
  • The colour switch in the original's block order (assassin, animals, siege engines, default), and the colour player written in each arm.
  • The two row loops step the row base in their headers.
  • drawBorderBox's colour as short in the header: the box's colour is pushed as -1 as in the original (0x004B727E), and the two loads in front of the first unit loop's pixel loops come in the original's order with it (0x004B6DC5, widthFactor before heightFactor).
  • The numbers are main's constants, as on UI: the minimap's second picture at 100% #465: the screen tile table's start, size and row lengths (VC_SCREEN_POINT_*), the map sizes and the 400 rows (MC_MAP_*), the small map's margin (MC_MAP_XY_LIMIT - MC_MAP_SIZE_200) / 2, the blit's row in bytes as main's MMC_BUFFER_ROW_BYTES through a using-declaration, because inline assembly cannot name a qualified constant, and the flags as MinimapDrawFlags.

What is left (four instructions)

  • The siege engines' colour index (two instructions, 0x004B6D71 and 0x004B6D75). The original keeps a - 8 in the index and its table base at 0x00B39560 (lea eax, [edx + ecx*2 - 8], then [eax*2 + 0xb39560]). About twenty spellings tried, all of which fold the constant into the base (0x00B39550): flat and 2-D indexes, the table taken from either field, signed and unsigned operands, the index in a variable of its own, a __forceinline helper, scoped bound locals in both orders, a per-player struct. With the table typed ushort[9][4][2] now, eight more fold it the same way: the field indexed directly, the table split at player 1 and indexed [slot - 1], the base taken at [1] with the - 8 inside the index instead of on top, flat and two-dimensional.
  • Two loads swapped (two instructions) at the first unit loop's back edge (0x004B6E21 and 0x004B6E24): the original reloads widthFactor before xPos behind the pixel loop, ours the other way round. This is a tie that header changes move. With drawBorderBox's colour as ushort it was the pair in front of the same loop's pixel loops (0x004B6DC5, widthFactor before heightFactor) and this one matched; with short it is the other way round; taking the two loop heads out of the address list (see the header findings) swapped a third pair as well (0x004B70CF, in the second loop). One pair is off in every state tried. Before: about a dozen variants (the bound test as separate continues in one or both loops, nested ifs, the dot's size as scope locals in both orders, a helper-shaped scope); now five more on the back edge (operands of the bound test turned round, the bound's product turned round, baseY before baseX): ties or worse.

For whoever continues:

  • The siege engines' colour. The base 0x00B39560 is MiniMapDefinedData + 0x1FC: player 1's entry, PlayerColorColors[1] (the old header's PlayerColorColors_entry2), and the - 8 is one player's eight words back. Only this arm has it; it is also the only arm with the frame, and it reads this back from its spill slot (0x004B6D5E). The other player-colour reads (0x004B6CEA, 0x004B6D8F, 0x004B6F01, 0x004B6F3D) use 0x00B39550 with no displacement (the animals' two 0x00B39558). So the original's source indexed this one table from player 1.
  • The swapped loads. In the second loop the original keeps currentPlayerSlotID in edi around the draw code (loaded at 0x004B6EEE, reloaded at 0x004B71C4); in the first loop edi holds nothing there. So the two loops differ in their register state in front of the block, not in the block's own code. The original loads widthFactor first at both places of the first loop; ours puts it between heightFactor and xPos, before the one and after the other, and which way round depends on the headers.

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

  • if (mapSize == MC_MAP_SIZE_160) {} else if (mapSize == MC_MAP_SIZE_200) xPos -= 2; behind the blit: the original tests 160 first (0x004B6B81). A 160-tile map keeps its 3-pixel shift and a 200-tile map one of its three.
  • (int)this->field_0x34 >= 100 and the like: the original compares signed, and the header has undefined4.
  • The loops that step by 2 at half size: the original's loops (the style-lint bot will post them).

Checker CHECKs (not changed)

  • 34 x SG 24 (declared without a value / a for loop's counter declared outside it): the function-level loop variables, see above.
  • Five inline assembly blocks: see above.
  • Placeholder names field_0x34, field_0x38 and field_0x44; gynt's headers on reimpl/Wilps-2026-10-18 (8ca7861) call them incrementalRedrawPercent, pendingRedrawPasses and compositedPixelBuffer.
  • Two loops that step by 2: the original's.
  • The header changes, each in a commit of its own (81451bc, 66a6efe, 33693e8, 9187994): see the header findings.
  • 68 field lines of MiniMapDefinedData.hpp replaced by one array field: the offsets are checked (0x1EC + 144 = 0x27C), and nothing on main or in an open PR uses the removed names.

Header findings

Changed here, each in a commit of its own; gynt allowed them here on 11-27 ("Feel free to make the header changes if it leads to improvements. Then I can address them in Ghidra."):

  • drawBorderBox's colour is a short (81451bc: PencilRenderCore.hpp and the resolver in PencilRenderCore.func.hpp). The original passes it -1 as a dword (0x004B727E push -1); with ushort the call compiles to push 0xffff. With short this function goes from 97.6 to 98.3: the push matches, and so do the two loads in front of the first unit loop's pixel loops. All 21 files on main that call it keep their numbers (the full build above); in the earlier pair of full builds, with int, all 19 callers then on main read differently, so the callers fit a 16-bit parameter and this function a signed one. gynt's open Reimplement 17 UI rendering, minimap and credits functions #294 implements drawBorderBox with ushort color: whichever of the two lands second has to bring its .cpp signature or this header line along.
  • MiniMapDefinedData::PlayerColorColors is ushort[9][4][2] (33693e8): per colour slot (0 to 8) four animation frames in the two colour modes, 0x1EC to 0x27C, where MinimapColorByLogicBit starts. This function reads it at [slot][0][mode] (0x004B6CEA, 0x004B6D8F, 0x004B6F01, 0x004B6F3D), [slot][frame][mode] (0x004B6D75) and [0][2][mode] (0x004B6C72, 0x004B6D51). The header had ushort[2][2] there and 68 single fields behind it (AnimalMinimapColour_RGB15/16 = [0][2], PlayerColorColors_entry2 = [1][0][0], field_0x1f8 to field_0x27a), all inside the table; none of them is used on main or in an open PR. The function indexes the field directly now, the same listing as with the cast, and the PlayerUnitColors typedef this PR added on 11-25 goes again (9187994).
  • New: UI/Enums/MinimapDrawFlags.hpp (66a6efe), in the generated form: MDF_CENTER_ON_OFFSETS = 1 (0x004B6970 test 1), MDF_VIEWPORT_BOX = 2 (0x004B71FF test 2), MDF_HALF_SIZE = 4 (0x004B6814 test 4). The callers on main pass 5 and 7, so the parameter stays a uint. The same listing.

Proposed, not changed here:

  • 0x004B6C20 and 0x004B7100 are not functions. They stand as type: function without a label in src/precomp/addresses-SHC-3BB0A8C1.hpp and as Pending in the status list, but both lie inside this function (0x004B66C0 to 0x004B729C), each behind alignment filler: the first unit loop's row head (reached from 0x004B6C1B jmp and 0x004B6ECD jl) and the second's (0x004B70F5 jmp, 0x004B712F jl). Nothing in the image calls them or holds their address. Taking the two entries out here was measured: every file includes that header, and the edit alone swapped a third pair of loads in this function (98.3 to 98.2). So it is left to Ghidra; the sync that drops them may move this function's load tie the same way.
  • Names (Ghidra's side): MinimapViewState::field_0x34 / field_0x38 are the step-by-step redraw's strip start (in percent, stepped by 2) and its passes left, and field_0x44 is the picture (400 rows of 200 pixels) this function copies from; gynt's headers on reimpl/Wilps-2026-10-18 (8ca7861) call them incrementalRedrawPercent, pendingRedrawPasses and compositedPixelBuffer. The last parameter (param_10 in the header) is intoPreview here: above 0 the picture goes into loadedMiniMap instead of the screen.

🤖 Generated with Claude Code

…index, two loads and the colour's push differ

MinimapViewState::drawMinimap, the minimap: it copies the terrain picture from the minimap buffer to the
screen (or to the loaded map's preview), draws every unit as a dot in its player's colour (with the wildlife
hidden: own team and enemies in two colours), and draws the viewport's box. 946 instructions, rewritten from
the original's listing, with gynt's draft beside it for the names: his best version is 10280b1 on his
fork's reimpl/UI-Core-development ("32.8% | Reimplemented, asm differs"), his own commits for it f3cc8dd
(23.32%) and 34548c6 (19.14%), on sourcehold#256 c899be1 and 95d388a, the newest e9bc49c on
reimpl/UI-MenuItems-development (23.3%).

Behaviour against the draft (every version):
- `mapSize <= 200` where the draft has `< 200`: on a 200-tile map the draft reads the picture 100 columns
  and 100 rows away from where the original reads it;
- the viewport box: the original returns where the draft has two empty ifs, so when the box sticks out on
  both sides it draws none (gynt has the returns himself in 8ca7861 on reimpl/Wilps-2026-10-18);
- with the wildlife hidden, the original shows an enemy assassin up to and including distance 160; the
  draft only below 160 (mode 1's test is right in the draft);
- the minimap buffer through `this` as in the original (the draft goes through the global instance; the same
  while `this` is that instance, and all five call sites pass their own `this`).

What the match needed: the blit and the four pixel stores are handwritten assembly in the original (inline
assembly here; it also brings the frame pointer); the loop variables of the two unit loops declared once at
the function's level (declared per loop, their stack slots differ); the box position as two locals and the
two offsets as the tile position; MC_MAP_SIZE_400 - mapSize written out each time; the first ten
initialisations in the order found by a search over the orders; the colour switch in the original's block
order. The numbers are main's map and screen-tile-table constants, as on sourcehold#465.

Left (five instructions, found by a slot-blind comparison): the siege engines' colour index (2), where the
original keeps a - 8 in the lea and its table base 0x00B39560, player 1's entry (every spelling tried folds it
into the base); two loads swapped in front of the first unit loop's pixel loops (2); and the box's colour,
pushed as -1 by the original and as 0xffff here (1): drawBorderBox's colour is `ushort` in the header, and
`short` gives the original's push (gynt's open sourcehold#294 has drawBorderBox with `ushort`, so it is not changed
here). Measured in a narrow build of this file on main 812d149: 97.6% raw, 90.1% normalised. Not run in
game.

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

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

2 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
8 Let the compiler unroll 2
Where

Rule 8 — Let the compiler unroll

  • src/OpenSHC/UI/MinimapViewState/drawMinimap.cpp:390 — loop steps by 2; try the plain loop and let MSVC unroll
  • src/OpenSHC/UI/MinimapViewState/drawMinimap.cpp:513 — loop steps by 2; try the plain loop and let MSVC unroll

kotijekriv and others added 4 commits November 22, 2026 20:06
The style bot suggests letting MSVC unroll the two `offsetX += 2` loops. They draw every other pixel at half
size, as in renderMinimapTileStripWithLuminescence (sourcehold#465), and get the same one-line comment. Written as a plain
loop that skips the odd pixels, the function no longer matches: in a quick compile 378 lines differ instead of
10. Comments only; the listing is the same as before.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new generated-form typedef, UI::MiniMap::PlayerUnitColors = ushort[4][2]: per player the four animation frames'
colours in the two colour modes. drawMinimap reads MiniMapDefinedData::PlayerColorColors through it, which declared
the type inside the function until now (gynt on sourcehold#488: new types go in .hpp files, not in .cpp files).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The function-local typedef goes: the type is PlayerUnitColors from the header commit before this one. The file-local
PICTURE_ROW_BYTES goes too: it is the same 400-byte row of a minimap buffer that main's MinimapConstants names
MMC_BUFFER_ROW_BYTES, and the assembly's two uses take it through a using-declaration, since inline assembly cannot
name a qualified constant. The function's listing is the same instruction for instruction.

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 24, 2026
@gynt

gynt commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Feel free to make the header changes if it leads to improvements. Then I can address them in Ghidra.

kotijekriv and others added 6 commits November 28, 2026 10:37
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
drawMinimap passes drawBorderBox -1 for the viewport box's colour, and the original pushes it as a dword
(0x004B727E push -1). With the header's ushort the call compiles to push 0xffff; with short it compiles to the
original's push. The colour is a 16-bit pixel value either way, so the width stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new generated-form enum for MinimapViewState::drawMinimap's flags argument: 1 centres the picture on the given
offsets (0x004B6970 test 1), 2 draws the viewport's box (0x004B71FF test 2), 4 draws at half size (0x004B6814
test 4). The callers on main pass 5 and 7, so the parameter stays a uint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
drawMinimap reads the table at 0x1EC per colour slot (0 to 8), per animation frame (four) and per colour mode (two):
[slot][0][mode] for most units (0x004B6CEA, 0x004B6D8F) and for the two colours with the wildlife hidden
(0x004B6F01, 0x004B6F3D), [slot][frame][mode] for the siege engines (0x004B6D75), [0][2][mode] for the animals and
the units without an owner (0x004B6C72, 0x004B6D51). Nine slots of 16 bytes end at 0x27C, where
MinimapColorByLogicBit starts. The header had ushort[2][2] there and 68 single fields behind it
(AnimalMinimapColour_RGB15/16 = [0][2], PlayerColorColors_entry2 = [1][0][0], field_0x1f8 to field_0x27a), all
inside the table; none of them is used on main.

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

The three flag tests take MinimapDrawFlags, and the colour reads index MiniMapDefinedData::PlayerColorColors
directly now that the header gives it its [9][4][2] shape, so the cast to PlayerUnitColors goes. The listing is the
same as with the cast.

With drawBorderBox's colour a short (the header commit before), the viewport box's colour is pushed as -1 as in
the original (0x004B727E), and the two loads in front of the first unit loop's pixel loops come in the original's
order (0x004B6DC5, widthFactor before heightFactor). The pair of loads at that loop's back edge (0x004B6E21,
widthFactor before xPos in the original) is the other way round now. Narrow build: 98.3 raw and 91.6 normalised,
from 97.6 and 90.1.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MiniMapDefinedData::PlayerColorColors is ushort[9][4][2] now, so the per-slot typedef this branch added for
drawMinimap's cast has no user left.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kotijekriv kotijekriv changed the title UI: the minimap's drawing at 97.6% (draft) UI: the minimap's drawing at 98.3% (draft) Nov 28, 2026
@kotijekriv

Copy link
Copy Markdown
Contributor Author

Thanks! I made the header changes, each in a commit of its own:

  • drawBorderBox's colour is a short now (header and resolver). That fixes the push of -1, and as a side effect one of the swapped load pairs too: drawMinimap goes from 97.6% to 98.3% (normalised 90.1 to 91.6). In a full build against main, every function on main reads the same, all 21 drawBorderBox callers included. Your Reimplement 17 UI rendering, minimap and credits functions #294 has drawBorderBox with ushort color, so whichever of the two lands second needs that line adjusted.
  • MiniMapDefinedData::PlayerColorColors is ushort[9][4][2] (0x1EC to 0x27C). The 68 single fields behind it were inside the table and nothing uses them. The function indexes it directly now, so the PlayerUnitColors typedef from last time is gone again.
  • A new enum MinimapDrawFlags (1 centre on offsets, 2 viewport box, 4 half size).

I left two things for Ghidra, in the PR text. 0x004B6C20 and 0x004B7100 are loop heads inside drawMinimap, not functions; taking them out of the address list here moved a load tie in this function, so I left that to your sync. Also the names for field_0x34/0x38/0x44 and param_10.

Four instructions are left: the siege engines' colour index (it still folds the -8 into the base whatever we write) and one pair of loads in a different order, which header changes keep moving around.

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

declaration/type change This requires changes to the generated/exported types or definitions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants