Skip to content

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

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

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

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One function at 97.6%, written from the original's listing with gynt's draft beside it for the names. This is a draft PR: five instructions still differ (below), so others can continue from here. No header changes.

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

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.
  • Full build: an all-on build of main 812d149 with this function at an earlier cut and with drawBorderBox's colour as short in the header, both resolver sets on, 0x004314E0 left out with its resolver off as in main's baseline: 0 failed of 2,424 compile steps; drawMinimap 98.3 / 90.3 there; every main function reads exactly what it read before (2,322 functions at 100.0 raw in both builds). The cuts since then change comments and names, spell the colour table and the numbers through main's constants, write the self-subtractions plainly and take BOOLEnum for two flags; the quick compile's listing is the same for each of those steps. Without the header change the colour's push is the fifth difference, hence 97.6.
  • 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, five 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 57 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 97.6%

  • 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.
  • 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, and the blit's row in bytes from a file-local enum, because inline assembly cannot name a qualified constant.

What is left (five 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.
  • Two loads swapped (two instructions) in front of the first unit loop's pixel loops (0x004B6DC5 and 0x004B6DC8: widthFactor before heightFactor). The second loop has them the other way round in the original too, and there ours matches. About a dozen variants tried: 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.
  • The box's colour (one instruction, 0x004B727E): the original pushes -1, ours 0xffff, because drawBorderBox's colour is ushort in the header (see the header findings).

For whoever continues:

  • The siege engines' colour. The base 0x00B39560 is MiniMapDefinedData + 0x1FC: player 1's entry, the 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.

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.
  • The colour table read through a file-local typedef (ushort [4][2] per player: four animation frames in the two colour modes); see the header findings.
  • (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.

Header findings (nothing changed here)

  • drawBorderBox's colour is a short. The original passes it -1 as a dword (0x004B727E push -1); with the header's ushort the call compiles to push 0xffff, with short to the original's push. Measured in two all-on full builds of main with this function added: with short, no function on main reads differently, and all 19 of drawBorderBox's callers on main keep their numbers; with int, all 19 read differently: 18 leave 100.0 (drawMouseBasedBox lowest at 95.2; the RankingGames render functions, ReceiveMapFrom, SendMapTo, TacticalPowerBar, several menu views' DoEveryFrame), and WinMain goes from 97.2 to 97.0. (Menu::handleMenuItems and Menu::Constructor_Menu read lower in that build too, but they do not call drawBorderBox: it is the 0x11000000 printing reading described in UI: the minimap's second picture at 100% #465.) So the callers fit a 16-bit parameter and drawMinimap a signed one. gynt's open Reimplement 18 UI rendering, minimap and credits functions #294 implements drawBorderBox with ushort color; the change is offered for it rather than made here, and with it this function loses its fifth difference.
  • MiniMapDefinedData::PlayerColorColors (0x1EC) is ushort[2][2] in the header, but this function reads it as ushort[9][4][2]: per player four frames in two colour modes, players 0 to 8, to 0x27C. The fields behind it in the header, AnimalMinimapColour_* and PlayerColorColors_entry2 among them, are inside that table; none of them is used on main or in an open PR. Not changed here, since it is a header change.
  • drawMinimap's flags have no enum: 1 centres the picture on the offsets, 2 draws the viewport's box, 4 draws at half size.
  • 0x004B6C20 and 0x004B7100 are typed function in src/precomp/addresses-SHC-3BB0A8C1.hpp and stand as Pending. Both are loop heads inside this function, which runs to 0x004B729C.
  • MinimapViewState::field_0x34 / field_0x38 are the step-by-step redraw's strip start (in percent, stepped by 2) and its passes left; field_0x44 is the picture (400 rows of 200 pixels) this function copies from.
  • The last parameter (param_10 in the header) is called 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

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>

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