Skip to content

UI: the minimap's second picture at 100% - #465

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

kotijekriv wants to merge 4 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round45-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

One function at 100%, the sibling of renderMinimapTileColors (#461) and written the same way, from the original's listing. It comes out of our draft #313, where it stood at 21.4%. One header change, in a commit of its own: a new enum, OpenSHC::Rendering::ColorChannelMask (src/OpenSHC/Rendering/ColorChannelMask.hpp, 2246b2f), for the colour channels' masks. Main's declaration check fails on any new header under src/OpenSHC and labels the PR, so this PR waits for the header to reach main through the Ghidra sync, as the other declaration-change PRs do. No behaviour change against #313's code.

Function Address Before reccmp raw Normalised
MinimapViewState::renderMinimapTileStripWithLuminescence 0x004B5330 21.4% in #313 100.0% 100.0%

#313's version started from gynt's draft as it stood on his fork's reimpl/UI-Core-development, 10280b1 (2026-10-25; status line "9.3% | Reimplemented, asm differs"), with the body rewritten from the listing. His newest version is 4a1f8dd (2026-11-02, a style pass over the UI folders; "9.0% | Style pass, asm differs") on reimpl/UI-MenuItems-development, where 1056d3f, 2da2d36 and b60bb72 are earlier steps of it (9.03%); 24080c9 and 1955a67 (9.0%) are the same draft on reimpl/UI-Core (#256). What #313 changed against every version of the draft stays:

  • Maps of exactly 200 tiles, as in renderMinimapTileColors (UI: the minimap's terrain colours at 100% #461). The original takes the small maps' path for mapSize <= 200 (0x004B53E4: cmp ..., 200; jg). Every version of the draft has < 200. So on a 200 by 200 map, with the 4 by 2 pixel tiles that the caller hands it, the draft drew the table's left half into 400 rows of the buffer, where the original fills 200 rows with the table's centre.
  • Past the strip's end the original returns (0x004B54C3: jge to the epilogue). The draft has an empty if there. gynt has the return himself in 8ca7861 ("wilps-2026-10-18", 2026-10-27) on his reimpl/Wilps-2026-10-18. drawMinimap reaches it on every step of its step-by-step redraw (2% of the rows per call, see below). Without it, each call drew every row from the strip's start to the end of the table.
  • The buffer through this. Every version of the draft writes the second buffer through the global instance (the fixed address 0x01A31654, DAT_MinimapViewState 0x01A31610 plus 0x44). The original takes it from this (0x004B5343 to 0x004B534C), as this file does. The two agree while this is that instance. Seven of the ten call sites of setMapPropertyDependingOnMapSize pass that instance outright; drawMinimap's three pass its own this.
  • The screen tile table's base. This one is not the draft's mistake. The draft indexes it - 8, which was right for the header of its day (the array at 0x4E600). Main's header has since moved the array to 0x4E5E0 (2bd620b).

Update (review)

  • 2246b2f, header: OpenSHC::Rendering::ColorChannelMask in src/OpenSHC/Rendering/ColorChannelMask.hpp, next to ColorMode: the red, green and blue masks of an RGB555 and an RGB565 colour (CCM_RGB555_RED ... CCM_RGB_BLUE). It replaces the file-local enum; renderMinimapTileColors (UI: the minimap's terrain colours at 100% #461) has the same five masks.
  • b1a5287 and 1954ae2, code: the numbers are named by main's constants. From Map::MapConstants: the map's 400 rows and columns (MC_MAP_XY_LIMIT), the 200-tile test (MC_MAP_SIZE_200), and the small map's first row, its margin (MC_MAP_XY_LIMIT - MC_MAP_SIZE_200) / 2. From Rendering::Enums::ViewportConstants: the screen tile table's start and one table of 80400 entries per orientation (VC_SCREEN_POINT_TABLE_START + k * VC_SCREEN_POINT_TABLE_SIZE), and its short and long rows (200 and 201, and 401 for the pair). The inline assembly takes a buffer row in bytes (MC_MAP_XY_LIMIT / 2 * sizeof(ushort)) from a file-local enum, because inline assembly cannot name a qualified constant.
  • 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 as committed, alone. 100.0 raw and normalised, with no diff line. Reimplement 16 rendering, UI and GameSynchronyState functions (not yet 100%) #313's code in the same build: 21.4 / 21.4.
  • Full build: an all-on build of the previous cut of this branch on main 812d149 (the same code: the cuts since only change comments), 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 setMapPropertyDependingOnMapSize, 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 in UI: the minimap's terrain colours at 100% #461's build).
  • Instructions: the original's 387 (393 with its six alignment fillers: two lea ecx, [ecx], two lea ebx, [ebx] and two mov edi, edi), 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 12 CHECK (below); our local fixer finds nothing to change.
  • Not run in game.

What the function does

For a strip of the screen tile rows it writes the minimap's second buffer (field_0x44): each tile in the colour of what stands on it, scaled by the tile's luminescence, at full size or, zoomed out, at half size. A moat tile and a tree tile take a colour from their own tables (field_0x74, field_0x144); an L_PLAIN2_AND_PITCH tile takes field_0x20's colour if its display layer has MD_RUBBLE_EDGEUnk; every other tile takes the colour of the first entry of MinimapColorByLogicBit that shares a bit with its flags. A tile that ends up without a colour takes the pixel of the terrain picture (minimapPixelBuffer, which renderMinimapTileColors draws). Its one caller on main, setMapPropertyDependingOnMapSize, always asks for half size and passes its strip on: drawMinimap (0x004B66C0, not on main) asks for 2% of the rows at a time while it redraws the minimap (0x004B67CF, 0x004B67F2).

Read block by block against the listing, #313's code already did all of that. One arithmetic detail differs: the strip's end is divided signed here, as in the original (#313 divided unsigned; the same for every real argument).

What got it to 100%

  • The pixel stores are handwritten assembly in the original: the same two stores as in renderMinimapTileColors (UI: the minimap's terrain colours at 100% #461), instruction for instruction but for the stack slots, with the address computation of renderMinimapPreview and PencilRenderCore::drawCurrentPixel on main (add eax, eax and a mul edx by the row pitch, here the constant 400). They are two __asm blocks, one per size, 12 and 10 instructions. Once both functions are on main, the two files can share the stores as a macro in an *Asm.hpp, as AGENTS.md has it for a stream that repeats; that is left for then (a new header under src/OpenSHC trips the declaration check, and either PR would then wait for the other). Everything around them is C++: the loops, the bounds tests, the half-size divisions and the read of the terrain picture's pixel. The function keeps no frame pointer with them, as in the original.
  • The colour is a 16-bit local, and blue and the mix are written out in both colour-mode arms. Here the original has one shared end for the two arms, and the compiler merges the two identical ends itself. Written once behind the arms (as Reimplement 16 rendering, UI and GameSynchronyState functions (not yet 100%) #313 had it), the registers of the whole brightness block are different ones.
  • The channels initialised blue, green, red; red's and green's masks as statements right after their caps, blue's in the mix, as in renderMinimapTileColors.
  • The table loop. The entry's bits are in a local that is set before the tile's flags are read; the first test reads that local and the loop's test reads the table. The compiler takes the operands of the two tests in the order the variables are first set, so with the first test on the table and the loop's on the local both come out swapped, and with every test on the table, or with the local set after the flags, the first one does (99.5% and 99.7% in narrow builds). A flag tells "no entry" from "found"; the compiler turns it into the two jumps and never stores it. The loop is a guarded do-while: a plain while reads 99.7% (one of the two tests swapped, whether it tests the local or the table), and a for loop with a break at its end compiles to two instructions more.
  • The five simple initialisations before the buffer and the two strip-row divisions, so that every stack slot is the original's (the first column, the colour and y's stepping value sit in the slots of parameters 1, 4 and 5).
  • Smaller ones: rowBase holding the orientation's offset first; the viewport height read from the global a second time for the other row parity; the column loop's bound in its test; the tile read before x and y; y 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; rowBase stepped in the row loop's header.

One thing our quick single-file compile got wrong on the way: it orders the operands of those two tests the other way round than the real build does, so the form that matched there read 99.5% in reccmp. The numbers above are all from real builds.

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

  • int entry = 0; int entryBits = 0; at the top of the tile loop, and int blue = 0; int green = 0; int red = 0;: see "The table loop" and "The channels".
  • bool hasEntry, a flag that is never stored: see "The table loop".
  • The first table test reads entryBits and the loop's test reads the table again, although the local holds the same value: see "The table loop".
  • The colour-0 test is inside the innermost drawing loop (if (color == 0) before every pixel): the original tests it there (0x004B5730, 0x004B580F). Only the first pixel can find 0, unless the terrain picture's pixel is 0 as well.
  • The viewport height is read from the global twice. The original reads it once (0x004B53FD) 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 two values only tell the two kinds of row apart.
  • pixelX, a copy of x for the full-size store: the original stores it at the top of that branch (0x004B56FC).
  • 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.
  • The column loop starts two columns before the first one (firstColumn - 2), which renderMinimapTileColors does not: the original has the - 2 (0x004B54E8).

Checker CHECKs (not changed)

  • Placeholder names, with what this function shows them to be: MinimapViewState::field_0x44 is the minimap's second pixel buffer (this function writes it, 400 bytes a row like minimapPixelBuffer); MiniMapDefinedData::field_0x74 are the moat's colours (L_MOAT), field_0x144 the trees' (L_TREE | L_TREE_VARIATION), and field_0x20 the colour pair of L_PLAIN2_AND_PITCH tiles with MD_RUBBLE_EDGEUnk. gynt's headers on reimpl/Wilps-2026-10-18 (8ca7861) already call them compositedPixelBuffer, moatColors, treeColors and plain2PitchColor.
  • 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: the pixel store itself and nothing else (above).
  • The do-while: measured (above). The loop that steps by 2: the original's loop, not an unrolled one; a plain loop that skips the odd positions loses the match. main's style-lint bot will post one finding for each.

Header findings (not changed here)

  • Parameter 4 is used signed: both strip rows are signed divisions (0x004B533B to 0x004B5382), where the header has uint param_4; the code casts. (renderMinimapTileColors has the two types the other way round, int param_4, uint param_5.)
  • Parameter names this file uses, for the header's param_1 to param_5: flags (bit 2: half size), widthFactor, heightFactor, stripStart, stripLength.
  • 0x004B5480 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 0x004B58C8.

🤖 Generated with Claude Code

MinimapViewState::renderMinimapTileStripWithLuminescence, the minimap's second picture: for a strip of
the screen tile rows it takes the colour of what stands on each tile (moat, trees, plain tiles with one
display flag, or the first entry of MiniMapDefinedData's table that shares a bit with the tile's flags),
scales it by the tile's luminescence (RGB555 or RGB565) and writes it into the second minimap buffer, at
full or at half size; a tile without a colour takes the terrain picture's pixel. It is the sibling of
renderMinimapTileColors (0x004B58D0) and is written the same way, from the original's listing.

The function was a 21.4% draft in sourcehold#313 ("Pixel store and registers differ"). sourcehold#313's version started from
gynt's draft as it stood on his fork's reimpl/UI-Core-development, 10280b1 (2026-10-25; status line
"9.3% | Reimplemented, asm differs"), with the body rewritten from the listing. His newest version is
4a1f8dd (2026-11-02, a style pass over the UI folders; "9.0% | Style pass, asm differs") on
reimpl/UI-MenuItems-development, where 1056d3f, 2da2d36 and b60bb72 are earlier steps of it
(9.03%); 24080c9 and 1955a67 (9.0%) are the same draft on reimpl/UI-Core (sourcehold#256). No behaviour change
against sourcehold#313's code (the strip's end is now divided signed, as in the original; the same for every real
argument). What sourcehold#313 changed against every version of the draft stays:

- `mapSize <= 200` where the draft has `< 200` (every 200 by 200 map);
- the return past the strip's end, which the draft lacks (reached by drawMinimap's 2% strips). gynt has it
  himself in 8ca7861 on reimpl/Wilps-2026-10-18;
- the second buffer written through `this`, as in the original, where every version of the draft goes
  through the global instance (the same address while `this` is DAT_MinimapViewState);
- the screen tile table indexed from its base on today's main (not the draft's mistake: its `- 8` was right
  for the header of its day).

What the match needed:

- The pixel stores are handwritten assembly in the original: the same two stores as in
  renderMinimapTileColors, with the address computation of renderMinimapPreview and drawCurrentPixel
  (`add eax, eax` and a `mul edx` by the row pitch, here the constant 400). Two `__asm` blocks, one per
  size. No frame pointer, as in the original.
- The colour is a 16-bit local, and the blue channel and the mix are written out in both colour-mode
  arms. Here the original has one shared end for the two arms: the compiler merges the two identical
  ends itself. Written once behind the arms, the registers of the whole block are different ones.
- The channels initialised blue, green, red; red's and green's masks as statements right after their
  caps, blue's in the mix, as in renderMinimapTileColors.
- The table loop: the entry's bits in a local that is set before the tile's flags are read, the first
  test on that local and the loop's test on the table. The two tests take their operands in the order
  the variables are first set; with the first test on the table and the loop's on the local, both come
  out swapped. A flag tells "no entry" from "found"; the compiler turns it into the two jumps.
  The loop is a guarded do-while: a plain while loop reads 99.7% (one of the two tests swapped, whether
  it tests the local or the table), and a for loop with a break at its end compiles to two instructions
  more.
- The five simple initialisations before the buffer and the strip rows, so that every stack slot is the
  original's; the row base holding the orientation's offset first; the viewport height read from the
  global a second time; the column loop's bound in its test; the tile read before x and y; y inside the
  tile loop and a copy of x at the top of the full-size branch; the row base stepped in the row loop's
  header.

The channel masks have names in a small file-local enum, as in renderMinimapTileColors.

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)

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 1
9 Prefer for over while/do 1
Where

Rule 8 — Let the compiler unroll

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

Rule 9 — Prefer for over while/do

  • src/OpenSHC/UI/MinimapViewState/renderMinimapTileStripWithLuminescence.cpp:132 — do-loop; the guide asks for a for

kotijekriv and others added 3 commits November 22, 2026 15:32
…5 colour

A new enum in OpenSHC::Rendering beside ColorMode: the masks of the red, green and blue channels in the two
colour modes. renderMinimapTileStripWithLuminescence (and renderMinimapTileColors) scale each channel of a
minimap colour by the tile's light and cap it at these masks; they had them as a file-local enum.

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

Review: the channel masks come from the new ColorChannelMask header enum; the map's 400 rows and its 200-tile
size from Map::MapConstants (MC_MAP_XY_LIMIT, MC_MAP_SIZE_200); the screen tile table's start, its two row
lengths (200 and 201) and its 80400 entries per orientation from Rendering::Enums::ViewportConstants. The
orientation offsets are VC_SCREEN_POINT_TABLE_START + k * VC_SCREEN_POINT_TABLE_SIZE, and 401 is a short and a
long row. The two inline assembly blocks take the buffer's row length from a file-local enum, since inline
assembly cannot name a qualified constant.

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>
…e buffer's row from map constants

Review: the first row for a map of up to 200 tiles is the margin around it, (MC_MAP_XY_LIMIT -
MC_MAP_SIZE_200) / 2, and the assembly's row length is a buffer row of MC_MAP_XY_LIMIT / 2 pixels in bytes
(both minimap buffers are 400 rows of 200 ushorts).

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>
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 22, 2026
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 22, 2026
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>
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 23, 2026
…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

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