Repository navigation
UI: the minimap's drawing at 97.6% (draft) - #470
Draft
kotijekriv wants to merge 2 commits into
Draft
kotijekriv wants to merge 2 commits into
kotijekriv wants to merge 2 commits into
Conversation
…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>
|
Style guide reading (advisory, posted once) 2 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented 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.
WhereRule 8 — Let the compiler 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
MinimapViewState::drawMinimapgynt'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
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).shortin 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;drawMinimap98.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 takeBOOLEnumfor 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.lea ecx, [ecx]at 0x004B6C1D, 0x004B6DCD and 0x004B6F1D,lea esp, [esp]at 0x004B6F59 and 0x004B70F7,mov edi, ediat 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.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_0x34drive (2% of the rows per call, withcollectCliffEdgeTilesForClimbDataat 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:
mapSize <= 200(0x004B6827cmp ..., 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 inrenderMinimapTileColors(UI: the minimap's terrain colours at 100% #461) andrenderMinimapTileStripWithLuminescence(UI: the minimap's second picture at 100% #465).jgeto the epilogue). 10280b1 and e9bc49c have two emptyif (...) {}there and draw a clamped box. gynt has both returns himself in 8ca7861 on his reimpl/Wilps-2026-10-18.jle). Every version of the draft draws one only below 160. In the other mode the draft's test is right.this(0x004B66E8lea edi, [esi + 0x44]), where the draft goes through the global instance. These are the same whilethisis that instance, and all five call sites (0x004B73A9, 0x004B74F9, 0x004B75D0, 0x004B76A7, 0x004B76D0) pass their ownthison.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%
renderMinimapPreviewanddrawCurrentPixelon 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 withmov edx, ...; mul edxandadd eax, eax, where the compiler writesimulandlea. 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 undersrc/OpenSHCwould also trip the declaration check).widthandheightcount tiles from the point where they are divided, as in the original, which keeps the counts in their stack slots.MC_MAP_SIZE_400 - mapSizeis written out each time (a local for it changes the stack slots).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)
- 8in 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__forceinlinehelper, scoped bound locals in both orders, a per-player struct.widthFactorbeforeheightFactor). 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 separatecontinues in one or both loops, nested ifs, the dot's size as scope locals in both orders, a helper-shaped scope.0xffff, because drawBorderBox's colour isushortin the header (see the header findings).For whoever continues:
MiniMapDefinedData + 0x1FC: player 1's entry, the header'sPlayerColorColors_entry2, and the- 8is one player's eight words back. Only this arm has it; it is also the only arm with the frame, and it readsthisback 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.currentPlayerSlotIDinediaround the draw code (loaded at 0x004B6EEE, reloaded at 0x004B71C4); in the first loopediholds 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.ushort [4][2]per player: four animation frames in the two colour modes); see the header findings.(int)this->field_0x34 >= 100and the like: the original compares signed, and the header hasundefined4.Checker CHECKs (not changed)
field_0x34,field_0x38andfield_0x44; gynt's headers on reimpl/Wilps-2026-10-18 (8ca7861) call themincrementalRedrawPercent,pendingRedrawPassesandcompositedPixelBuffer.Header findings (nothing changed here)
short. The original passes it -1 as a dword (0x004B727Epush -1); with the header'sushortthe call compiles topush 0xffff, withshortto the original's push. Measured in two all-on full builds of main with this function added: withshort, no function on main reads differently, and all 19 of drawBorderBox's callers on main keep their numbers; withint, all 19 read differently: 18 leave 100.0 (drawMouseBasedBoxlowest at 95.2; the RankingGames render functions, ReceiveMapFrom, SendMapTo, TacticalPowerBar, several menu views' DoEveryFrame), and WinMain goes from 97.2 to 97.0. (Menu::handleMenuItemsandMenu::Constructor_Menuread 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 withushort color; the change is offered for it rather than made here, and with it this function loses its fifth difference.MiniMapDefinedData::PlayerColorColors(0x1EC) isushort[2][2]in the header, but this function reads it asushort[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_*andPlayerColorColors_entry2among 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.functioninsrc/precomp/addresses-SHC-3BB0A8C1.hppand stand asPending. Both are loop heads inside this function, which runs to 0x004B729C.MinimapViewState::field_0x34/field_0x38are the step-by-step redraw's strip start (in percent, stepped by 2) and its passes left;field_0x44is the picture (400 rows of 200 pixels) this function copies from.param_10in the header) is calledintoPreviewhere: above 0 the picture goes intoloadedMiniMapinstead of the screen.🤖 Generated with Claude Code