Skip to content

UI: the minimap's terrain colours at 100% - #461

Open
kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round44-100
Open

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round44-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Correction (2026-11-22): an earlier version of this text said that no caller reaches the return at the strip's end. That was wrong: drawMinimap's step-by-step redraw passes 2% strips, as described below. The commit message still has the clause "(no caller passes a partial strip today)"; please drop it if the PR is squashed.

One function at 100%, written from the original's listing with gynt's draft beside it. No header changes. Two behaviour fixes against his draft: one shows on every 200-tile map, the other (which he also made on another branch) runs on every step of the minimap's step-by-step redraw. Two more places differ from the draft without a change on today's callers (below).

Function Address Before reccmp raw Normalised
MinimapViewState::renderMinimapTileColors 0x004B58D0 gynt's 20.4% 100.0% 100.0%

gynt's draft is 4a1f8dd (2026-11-02, a style pass over the UI folders; status line "20.4% | Style pass, asm differs") on his fork's reimpl/UI-MenuItems-development; 0b3142f, 1de07cb and 0d4e470 are earlier steps of it at 20.36%. 10280b1 on reimpl/UI-Core-development has it at 20.2%, and a0e86e1 (20.4%) and ab8f1e3 (20.2%) are the same draft on reimpl/UI-Core (#256). The draft has the names of the 14 colour tables and the whole colour chain as the original has it.

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 as committed, alone. 100.0 raw and normalised, with no diff line. gynt's 4a1f8dd in the same build: 20.4 / 20.6.
  • Full build: an all-on build of the previous cut of this branch on main 812d149 (the same code: the last cut changes comments, renames three locals, types the colour-mode flag as BOOLEnum and writes five tests with continue, which compiles to the same listing), with both resolver sets on: 0 failed of 2,424 compile steps, 99.58 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 setTileColorsDependingOnMapSize, as before. Every main function reads exactly what it read before: unrounded 99.5761 against 99.5759 raw and 99.5494 against 99.5492 normalised, with 2,323 against 2,322 functions at 100.0 raw.
  • Instructions: the original's 457 (461 with its four alignment fillers: two lea ecx, [ecx], a nop and a lea ebx, [ebx]), in its order; reccmp's diff for the function is empty. The function makes no calls and has no switch table.
  • Checks: our local style checker gives 0 ERROR and 7 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

The behaviour fixes

The function draws the minimap's terrain: for a strip of the screen tile rows it takes each tile's colour from its terrain flags and MiniMapDefinedData's tables, scales it by the tile's luminescence and writes it into the minimap pixel buffer, at full size or, zoomed out, at half size. Its one caller on main, setTileColorsDependingOnMapSize, always asks for half size and passes its strip on: the whole table, or 2% of the rows at a time from drawMinimap's step-by-step redraw.

  • Maps of exactly 200 tiles. The original takes the small maps' path for mapSize <= 200 (0x004B597E: cmp ..., 200; jg): it draws the centre of the screen tile table, rows 100 to 299 and columns 50 to 149. The draft tests mapSize < 200, so a 200-tile map took the larger maps' path, where the column count is halved instead (columns / columnDivisor). With the 4 by 2 pixel tiles that the caller (which tests mapSize > 200) hands it, that is rows 0 to 399 and columns 0 to 99: the table's left half, drawn into 400 rows of the pixel buffer, where the original fills 200 with its centre. 200 is one of the game's four map sizes (160, 200, 300 and 400, the switch at 0x004AC0A0), so this is every 200 by 200 map. Every version of the draft has < 200.
  • Past the strip's end the original returns (0x004B5A6B: jge to the epilogue). 4a1f8dd has an empty if there and goes on drawing the rows after the strip. gynt has this return himself in 8ca7861 ("wilps-2026-10-18", 2026-10-27) on his reimpl/Wilps-2026-10-18. drawMinimap (0x004B66C0, not on main) reaches it on every step of its step-by-step redraw. While its field_0x38 is non-zero, it calls setTileColorsDependingOnMapSize(field_0x34, 2) (0x004B67C2, 0x004B67E5) and steps field_0x34 by 2 up to 100, so each call covers 8 of the 400 rows. Without the return, each of those calls drew every row from the strip's start to the end of the table. gynt's own drawMinimap on reimpl/Wilps-2026-10-18 makes the same calls (incrementalRedrawPercent, 2).

Two more differences change nothing on today's callers:

  • The pixel buffer. The draft writes through the fixed address 0x01A58754, which is the global DAT_MinimapViewState (0x01A31610) plus minimapPixelBuffer's 0x27144. The original takes it from this (0x004B58D5: add ecx, 0x27144), as this file does; the two agree while this is that instance, which seven of the ten call sites of setTileColorsDependingOnMapSize pass outright; drawMinimap's three pass its own this.
  • The screen tile table's base. Not the draft's mistake: it indexes screenPointToTileNumber + ... - 8, which was right for the header of its day (the array at 0x4E600). Main's header has since moved the array's start to 0x4E5E0 (2bd620b), so on today's main the draft reads 8 entries too low; here the index has no - 8.

Read block by block against the listing, 4a1f8dd does everything else the way the original does. (Its row-parity counter starts at 0 where the original's starts at firstRow * heightFactor; the two agree in parity, because firstRow is 0 or 100.)

What got it to 100%

  • The pixel stores are handwritten assembly in the original: mov edx, 400; mul edx, add eax, eax and the colour through ax, the same address computation as in renderMinimapPreview and PencilRenderCore::drawCurrentPixel on main (theirs takes the pitch from horizontalByteSize, here it is the constant 400). Here they are two __asm blocks, one per size (next section). With the two stores written in C++ instead, the same file compiles to 419 instructions against the original's 457.
  • The colour is a 16-bit local. As a uint, the compiler merges the two colour modes' identical ends into one block and splits the (ushort) of the mix over its operands, whatever the source form (about 40 were tried, inline helpers included). As a ushort the mix is or; or; movzx reg, ax; mov, with ecx in one arm and edx in the other, and both ends stay, as in the original.
  • The channels: each mask as a statement right after its cap (inside the mix, the masks move to the end), and green set before red: the compiler takes the mix's operands in the order the variables are first set, so the three are initialised blue, green, red (with red first, or with no initialisers, the last two ors swap).
  • The five simple initialisations before the two strip-row divisions. A local that is set while a parameter is still to be read cannot take that parameter's stack slot. With these five first, every stack slot is the original's: the row counter, the colour and x's stepping value sit in the slots of parameters 1, 4 and 5.
  • Where the assembly's operands are assigned: y declared inside the tile loop and a copy of x at the top of the full-size branch, so that their stores stand where the original has them.
  • Smaller ones: the viewport height read from the global a second time for the other row parity (viewportHeight + 1 is added up anew in every row and has no slot); the tile read before x; rowBase stepped in the row loop's header; the column loop started at firstRow / 2 (below); rowBase holding the orientation's offset first, with north writing its 8 again.

The inline assembly

Two blocks, 12 and 10 instructions: the pixel store itself and nothing else. The loops around them, the bounds tests, the half-size divisions and the colour are C++, and every operand of the blocks is a C++ local named there (buffer, pixelX, offsetX, y, offsetY, color; halfX, halfY). Nothing of the store can move to C++: it builds the address in edi with add eax, eax and a mul by 400 loaded into edx, where the compiler writes lea and imul, and it reads each operand from its stack slot. The function keeps no frame pointer with the two blocks, as the original keeps none (sub esp, 0x58, ebp a general register).

The sibling renderMinimapTileStripWithLuminescence (0x004B5330) has the same two stores (0x004B575D to 0x004B5783 and 0x004B5835 to 0x004B5853). When it is written, the two can share them as a macro in an *Asm.hpp, as AGENTS.md has it for a stream that repeats; with one user they stay blocks here.

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

  • for (int column = firstRow / 2; ...) next to firstColumn, which holds the same value: the original computes x's first value as (firstColumn - firstColumn) * widthFactor + rowX, the subtraction left in (0x004B5AA6: mov edx, esi; sub edx, esi). Started at firstColumn, the compiler folds it to rowX.
  • int blue = 0; int green = 0; int red = 0;, never read before they are set: see "The channels" above.
  • North writes rowBase = 8 although it holds 8 already: the original has that arm (0x004B59B4), and a path without it for an orientation that is none of the four.
  • The viewport height is read from the global twice. The original reads it once (0x004B5997) and keeps both it and viewportHeight + 1 in stack slots of their own; the second read is what gives the + 1 its own slot here. The rows alternate by comparing rowParity with viewportHeight + 1: the two values only tell the two kinds of row apart (every other row starts half a tile in and has one entry less in the table: 200 against 201).
  • The ford colours are indexed with & 7 like the 16-entry tables, on a table the header gives 8 entries (0x004B5B2E; a header finding below).
  • The stones colours are in the chain twice: for the L2_STONES_OR_DRIVEN_SANDUnk flag and again for ground higher than 16. The original jumps back to the first block for the second (0x004B5C94).
  • blue is masked inside the mix while red and green are masked by statements: the original masks blue last, in the mix's block.
  • pixelX, a copy of x for the full-size store: the original stores it at the top of that branch (0x004B5E19).
  • At half size only every other pixel is written: rows whose row * heightFactor is odd are skipped, and the columns go in steps of 2 from x's low bit.

Checker CHECKs (not changed)

  • Three locals declared without a value: rowLength and rowX are set in both arms of the row-parity test (rowLength is declared in front of the row loop because the loop's header steps rowBase by it), and color by every arm of the colour chain.
  • The two inline assembly blocks: see above.
  • The loop that steps by 2: it is the original's loop, not an unrolled one. A plain loop that skips the odd positions reads 0.877 in the quick compare (this file: 0.989, where the rest is the comparison's own noise on temporaries' names). main's style-lint bot will post one finding for it.

Header findings (nothing changed here)

  • MiniMapDefinedData::FordMinimapColors has 16 entries, not 8: it is indexed [mode + (random & 7) * 2] (0x004B5B2E to 0x004B5B34). The header's field_0x64[8] behind it is its second half.
  • Parameter 5 is used signed: (param_4 + param_5) * 400 / 100 is a signed division (0x004B58F8 to 0x004B591C), where the header has uint param_5; the code casts. The sibling renderMinimapTileStripWithLuminescence has the two types the other way round (uint param_4, int param_5).
  • Parameter names this file uses, for the header's param_1 to param_5: flags (bit 2: half size), widthFactor, heightFactor (a tile's width and height in pixels), stripStart, stripLength (in percent of the 400 screen tile rows).
  • MiniMapDefinedData::MinimapColorArray is the table for tiles with L_ROCKY (0x004B5B7F), as gynt's 2cb2df7, which named the 14 tables, notes.
  • 0x004B5A20 is typed function in src/precomp/addresses-SHC-3BB0A8C1.hpp (no label); it is the row loop's head inside this function, which runs to 0x004B5F6B.
  • AGENTS.md says MSVC keeps ebp in any function containing an __asm block. This function has two and keeps none, in the original and here; the rule seems to need a narrower wording. main's drawCurrentPixel (0x00468F20) is another: an __asm block and no frame pointer, where renderMinimapPreview (0x004B5280) has one.

🤖 Generated with Claude Code

MinimapViewState::renderMinimapTileColors, the minimap's terrain picture: for a strip of the screen tile
rows it takes each tile's colour from its terrain flags and MiniMapDefinedData's tables, scales it by the
tile's luminescence (RGB555 or RGB565) and writes it into the minimap pixel buffer, at full or at half
size. Written from the original's listing. gynt's draft is 4a1f8dd (2026-11-02, a style pass over the
UI folders; status line "20.4% | Style pass, asm differs") on his fork's reimpl/UI-MenuItems-development;
his 0b3142f, 1de07cb and 0d4e470 are earlier steps of it at 20.36%. 10280b1 on
reimpl/UI-Core-development has 20.2%, and a0e86e1 (20.4%) and ab8f1e3 (20.2%) are the same draft on
his reimpl/UI-Core (sourcehold#256). The draft has the tables' names and the whole colour chain as the original has
it.

Behaviour fixes against 4a1f8dd:

- the map-size test is `mapSize < 200` where the original has `<= 200` (0x004B597E: `cmp ..., 200; jg`),
  so a 200-tile map took the larger maps' path, where the column count is halved: rows 0 to 399 and
  columns 0 to 99 of the screen tile table, where the original draws the table's centre, rows 100 to 299
  and columns 50 to 149 (every 200 by 200 map);
- past the strip's end the original returns (0x004B5A6B); the draft has an empty `if` there and draws on
  (no caller passes a partial strip today). gynt has that return himself in 8ca7861 on
  reimpl/Wilps-2026-10-18.

Two places where the code differs from the draft without a change on today's callers:

- the pixels are written through `this->minimapPixelBuffer`, as in the original, not through the fixed
  address 0x01A58754, the same address while `this` is DAT_MinimapViewState;
- the screen tile table is indexed from its base on today's main (the draft's `- 8` was right for the
  header of its day and reads 8 entries too low now).

What the match needed:

- The pixel stores are handwritten assembly in the original: `add eax, eax` and a `mul edx` by the row
  pitch, the same address computation as in renderMinimapPreview and drawCurrentPixel (whose pitch is
  `horizontalByteSize`, not the constant 400). Here they are two `__asm` blocks, one per size. The
  function keeps no frame pointer with them, as in the original.
- The colour is a 16-bit local. As a uint, the two colour modes' ends are merged into one block and the
  (ushort) of the mix is split over its operands; the original has both ends written out.
- The channels: each mask as a statement right after its cap, and green set before red (the mix takes
  the two in the order they are first set, so the three are initialised blue, green, red).
- The five simple initialisations before the two strip-row divisions: a local set while a parameter is
  still to be read cannot take its stack slot. With them first, every stack slot is the original's (the
  row counter, the colour and x's stepping value sit in the dead parameters' slots).
- The viewport height read from the global a second time for the other parity; y declared inside the
  tile loop and a copy of x at the top of the full-size branch, so that the stores of the assembly's
  operands stand where the original has them; the tile read before x; rowBase stepped in the row loop's
  header; the column loop started at `firstRow / 2` (started at firstColumn, the compiler folds x's first
  value, which the original computes as `(firstColumn - firstColumn) * widthFactor + rowX`); rowBase
  holding the orientation's offset first, with north writing the 8 again.

The channel masks have names in a small file-local enum (the style lint asks for names or decimals);
the listing is the same with the hex numbers.

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>
@github-actions

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

1 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 1
Where

Rule 8 — Let the compiler unroll

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

…table sizes

The three 400s in the C++ (the strip's first and end rows, and the number of columns) are the map's 400 screen tile
rows and columns, Map::MC_MAP_XY_LIMIT. The assembly's two `mov edx, 400` are a row of the minimap buffer in bytes
(200 pixels of 2 bytes): a file-local BUFFER_ROW_BYTES, defined as MC_MAP_XY_LIMIT / 2 * sizeof(ushort), as
renderMinimapTileStripWithLuminescence in sourcehold#465 does, because inline assembly cannot name a qualified constant.

The screen tile table's numbers use main's ViewportConstants, as sourcehold#465 writes them: the table's start (8) is
VC_SCREEN_POINT_TABLE_START, the other orientations' tables (80408, 160808, 241208) are VC_SCREEN_POINT_TABLE_START
plus one, two and three VC_SCREEN_POINT_TABLE_SIZE, a short and a long row together (401) are
VC_SCREEN_POINT_SHORT_ROW + VC_SCREEN_POINT_LONG_ROW, and the row lengths (200, 201) are those two. The 200 of the
row count and of the map size test is Map::MC_MAP_SIZE_200, and such a map's first row (100) is (MC_MAP_XY_LIMIT -
MC_MAP_SIZE_200) / 2. The comment above the orientation test names the table's start instead of the 8. The file
includes MapConstants.hpp and ViewportConstants.hpp. No other change.

The colour masks stay a file-local enum for now: Rendering::ColorChannelMask, which names them, comes with sourcehold#465,
and this file switches to it once sourcehold#465 is on main.

Measured: with the precompiled header, quick_diff gives the same listing for the pushed head's text and this one
after MSVC's `$T` temp labels are normalised. A narrow real build (main's /GL list and this round's files of two of
our PRs, with the struct resolvers and those files' function resolvers on, sourcehold's reccmp with
--resolve-wrapped-calls --ignore-security-cookie) reads 100.0% raw and normalised with no differing line for each
function this commit touches, and every other function reads as in the same build without them.

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