Skip to content

Reimplement UnitsState::processMeleeInitiation with the original's code, from gynt's text on main - #543

Merged
gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-w4-2-100
Dec 2, 2026
Merged

gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-w4-2-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

UnitsState::processMeleeInitiation (0x00549C70) from 85.9% (reccmp; gynt's text, on main since #238) to the original's instructions. reccmp reads 99.0%: the seven lines it still shows are the same instructions with the same operand under another label ("What reccmp still shows" has the seven pairs and the arithmetic), and the status line counts the function as 100% with a note that says so, the way main's file does for 0x004D9290. The body is gynt's, the handwritten block included; four places change, and none changes what the function does.

Function Address Before Now What matched it
processMeleeInitiation 0x00549C70 85.9% 99.0%, counted as 100% the scan of the unit's own tile as a do loop, two goto in place of a flag and of a second copy of three stores, the order of two terms of one sum

"Before" is main's reading in a build, the same with OPENSHC_FUNCTION_VISIBILITY_UNITS off and on. Main's status line still says 84.5%: it is older than ff6859a, the commit that brought the text to 85.9%.

What matched it

Main's text had the original's instructions in the original's order almost everywhere. What differed was where this lives behind the handwritten block, and three things followed from that.

The quick_diff readings below are from the repository's tools/reimplementation-helper/batch/quick_diff.py: it compiles this one file alone and compares the listing with the original's disassembly, with every global address folded to one token. It gives a ratio from 0 to 1, not a reccmp percentage.

  • The scan of the unit's own tile is a do loop behind its own test. The original loads this into edi in front of the loop's test (0x00549F4E) and uses it from there up to the check of the unit already fought (0x0054A458). As a for loop MSVC loads this inside the loop, loads it again behind the 24 tile tests, and keeps a zero in edi from the end of the loop to the end of the function (quick_diff 0.8976 -> 0.9399).
  • goto attack. When the unit already fought is still in reach, the original turns towards it and jumps into the code that starts the attack: the jmp at 0x0054A52B lands at 0x0054A6BA, which is also reached by falling through from 0x0054A6B9. Main's text set a flag in the loop and tested it behind it. MSVC removes the flag, but then keeps this in ebx over the rest of the function, where the original reads it from the stack for each call (0x0054A6E5, 0x0054A737) and has 0x6a and 8 in edi and edx (0x0054A6C5).
  • goto noTarget. "Nobody found on the 24 tiles" and "no enemy around at all" end in the same three stores and the same return. The original has them once: the je at 0x00549FCB lands at 0x0054A652, which is also reached by falling through from 0x0054A650. Written twice, both copies stay.
  • _adjacentTiles[13] adds [SomeY + 1][5] in front of [SomeY + 1][6], the way _adjacentTiles[17] adds [2] in front of [3]. With [6] first MSVC put the two loads the other way round than the original has them (0x0054A2C0). quick_diff could not show it, because it reads every global address as the same token; the first real build did.

The first three together read 0.9796 in quick_diff, with the original's 734 instructions. What it still shows there is its own spelling of two indexed globals and of the local array, and the registers of 0x6a and 8, which only the /GL build gives (edx holds 8 across saveUnitStateBeforeInterruption).

Other forms, measured

These are quick_diff readings of the file compiled alone, the do loop in all of them. None of them went through a real build: the real build that reads 99.0% is of the form with the two goto, which reads 0.9796 here.

  • no goto, the flag kept (set in the loop, tested behind it), the stores written twice: 0.9399;
  • goto attack only, the stores written twice: 0.9567;
  • goto noTarget only, the flag kept: 0.9640;
  • no goto, the call in the loop and a flag that only skips the second search, the attack nested in an if on a found target so that both exits fall into the three stores: 0.9640;
  • goto attack into that nested if, no second goto: 0.9632;
  • no goto and no flag, the attack written out at both places: 0.9244 (MSVC lays the shared code out at the first one).

In each form with a flag this is back in ebx behind the second search.

What reccmp still shows

Seven instructions read directionTranslationMatrix one row above the current one. Six are written [SomeY][-1] to [SomeY][-7] (gynt's "negative columns of the current row", ff6859a) and one [SomeY - 1][0]. The matrix is the first member of TileMapState (offset 0, TileMapState.hpp:55), so with SomeY * 32 in the register their displacement is TileMapState's address less 4 to 32.

  • In the original TileMapState is at 0x01A93208. MinimapViewState is at 0x01A31610 and 400376 bytes long, so it ends where TileMapState starts, and reccmp names 0x01A93204 after the symbol it lies in: MinimapViewState+400372, which is TileMapState - 4.

  • In the build TileMapState's instance is the DLL's own, and what stands in front of it is whatever the linker put there. In the option-on build that is ViewportRenderState (1699848 bytes long, so ViewportRenderState+1699844 is again its end less 4). In the option-off build it is function-resolver objects, and reccmp prints their names for the same seven operands (99.0% there too).

  • The first of the seven, in the original and in the compiler's listing of this file (symbol demangled; 27865608 is 0x01A93208):

    original   0054A20F  mov ebx, dword ptr [ecx + 0x1A93204]
    ours                 mov ebx, DWORD PTR StructResolver::Instance<OpenSHC::Map::TileMapState,27865608>::instance[ecx-4]
    
  • All 85 global memory operands of the function are the original's, in the original's order (the listing's symbol plus offset against the original's absolute address).

The seven pairs as reccmp prints them (option-on build)
Original Ours From TileMapState
0x0054A20F mov ebx, [ecx + MinimapViewState+400372] 0x100ECC1F mov ebx, [ecx + ViewportRenderState+1699844] -4
0x0054A233 mov ebx, [ecx + MinimapViewState+400368] 0x100ECC43 mov ebx, [ecx + ViewportRenderState+1699840] -8
0x0054A239 add ebx, [ecx + MinimapViewState+400372] 0x100ECC49 add ebx, [ecx + ViewportRenderState+1699844] -4
0x0054A3D9 mov ebx, [ecx + MinimapViewState+400348] 0x100ECDE9 mov ebx, [ecx + ViewportRenderState+1699820] -28
0x0054A3DF add ebx, [ecx + MinimapViewState+400352] 0x100ECDEF add ebx, [ecx + ViewportRenderState+1699824] -24
0x0054A403 mov ebx, [ecx + MinimapViewState+400348] 0x100ECE13 mov ebx, [ecx + ViewportRenderState+1699820] -28
0x0054A425 mov edx, [eax + MinimapViewState+400344] 0x100ECE35 mov edx, [eax + ViewportRenderState+1699816] -32

400376 - 400372 = 4 and 1699848 - 1699844 = 4, and so on down the column: both sides are the same distance in front of TileMapState. The seven lines are in two hunks; reccmp shows nothing else for the function.

No source form can change these seven lines: any form that reads the row above ([SomeY][-1], [SomeY - 1][7]) comes to the same displacement, and reccmp takes the label from the DLL's data layout, not from this function.

  • Built: the file has both spellings, and compiled alone with all seven written [SomeY - 1][k] its listing has the same seven operands (in a body four instructions shorter: the negative columns are what give the original's instructions around them). The option-on and option-off builds print different labels for these operands, and main's own builds show the same seven lines.
  • From the header: that the matrix sits at offset 0, and the two sizes.

The status file already counts functions whose only difference is such a label as 100% with a note:

This PR writes its line the same way: "(counted: only the address labels of the seven directionTranslationMatrix[SomeY - 1] operands differ)".

Kept as it is

  • The handwritten block and everything in front of it are untouched. The style bot's 16 hex findings are that block's or edx, 0x... masks.
  • The second search is one level less nested (its else is gone), so its lines show as changed; they are gynt's. That is also why the style checks list things this PR did not write: _candidateTile and _tileSlot declared without a value (each is assigned in both arms of the if that follows), the placeholder names field_0x340, field_0x3d2, field_0x11cf4 and field_0x12054, and DAT_UnitsState::instance in a member (the original addresses substate and the three setFacingDirectionTowardUnitMicro calls through the global).
  • field_0x340 holds the uid of the unit this one fights: line 460 compares it with units[_otherUnitID].uid, and lines 557 and 570 store the two uids. Renaming it is a header change, for another PR.
  • The bound of both searches is countof(DAT_UnitPropertiesDefinedData::instance.field_0x11cf4[0]) in place of 24: the column count of field_0x11cf4[9][24], which both index with _slot. Same object code.
  • Truth tests next to compares with 0: the truth tests are on _hasEnemyOnOwnTile, a flag; the compares are on ids, tiles and counts, and on dying and isStalked as gynt wrote them.
  • One line of comment at each of the three places (the do loop and the two goto) says what the other form gives.
  • The block comment in front of the first search is one // line now.

Measured

Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. All builds are on main 0cea04d. The tree that was built is this branch's .cpp together with the two /GL lines of #541 (clearAllUnits, computeLadderClimbPath); this function calls neither and is called by neither.

  • Small build, option on, of the final code (519 files, 529 functions: every /GL file of main, this function, updateUnits, which calls it, the callee closures and every file of a function-visibility group that touches them): BUILD_OK, 0 failures, 99.0%, the seven lines above and nothing else. Against the same build of main every one of the 529 functions reads the same but this one and clearAllUnits, which is Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541's. updateUnits reads 93.2% in both.
  • Small build, option off, of the final code (the same 519 files): BUILD_OK, 0 failures, 99.0%, the same seven lines and nothing else; their labels there are the function-resolver objects named above. Against the same build of the text before the _adjacentTiles[13] change (98.8%, the seven lines and that one sum) every one of the 529 functions reads the same but this one. Against main's small build with the option off (436 of the files, 446 functions) the only functions with changed code are this one and Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541's two; five more read differently with the larger file list, as Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541's text lists them.
  • The text changed after the builds, the code did not: two comments (one of them between the two builds) and the two loop bounds as countof(...). The object file of the final text was compared with that of the built text: every section has the same bytes and the same relocations.
  • No full build: the PR changes one .cpp and its status line.
  • Checks: nothing to fix automatically; the style checks give no errors. Their notes are the two goto and the do loop (each has its comment), the 16 hex masks of the handwritten block, and the lines listed under "Kept as it is".
  • Not run in game.

Other PRs

#536, #539, #540 and #541 are ours and open. None touches this file, and the status file only in other lines. #541 takes two other files off cmake/compiler-flags-gl.txt; the builds above have its two lines in. #501 and #528 (TroopValueState) do not touch this file either.

🤖 Generated with Claude Code

UnitsState::processMeleeInitiation from 85.9% (gynt's text, on main since sourcehold#238) to the original's code. reccmp reads
99.0%: the seven lines it still shows are the same instructions with the same operand under another label (below), and
the status line counts the function as 100% with a note that says so, as main's file does for 0x004D9290.

quick_diff below is tools/reimplementation-helper/batch/quick_diff.py: this file compiled alone against the original's
disassembly, every global address folded to one token. A ratio from 0 to 1, not a reccmp percentage.

- The scan of the unit's own tile is a do loop behind its own test. The original loads `this` into edi in front of the
  loop's test (0x00549F4E) and uses it up to 0x0054A458. As a for loop MSVC loads it inside the loop and again behind the
  24 tile tests, and keeps a zero in edi from the loop's end to the function's end (quick_diff 0.8976 -> 0.9399).
- goto attack: when the unit already fought is still in reach, the original turns towards it and jumps into the code
  that starts the attack: the jmp at 0x0054A52B lands at 0x0054A6BA, which is also reached by falling through from
  0x0054A6B9. With a flag set in the loop and tested behind it MSVC keeps `this` in ebx over the rest of the function,
  where the original reads it from the stack for each call and has 0x6a and 8 in edi and edx (0x0054A6C5).
- goto noTarget: "nobody found on the 24 tiles" and "no enemy around" end in the same three stores and the same return,
  which the original has once: the je at 0x00549FCB lands at 0x0054A652, which is also reached by falling through from
  0x0054A650. Written twice, both copies stay.
- The three together: 0.9796, with the original's 734 instructions. Other forms, in quick_diff only: 0.9640 with one
  goto or with none (a flag that skips the second search, the attack nested in an if), 0.9244 with the attack written
  out at both places. The real build is of the form with the two goto.
- _adjacentTiles[13] adds [SomeY + 1][5] in front of [SomeY + 1][6], as _adjacentTiles[17] does with [2] and [3]: with
  [6] first MSVC put the two loads the other way round than the original has them (0x0054A2C0). quick_diff folds global
  addresses and did not show it; the first real build did.

What reccmp still shows: seven instructions (0x0054A20F, 0x0054A233, 0x0054A239, 0x0054A3D9, 0x0054A3DF, 0x0054A403,
0x0054A425) read directionTranslationMatrix one row above the current one, six written [SomeY][-1] to [-7] (gynt's
negative columns of the current row) and one [SomeY - 1][0]. The matrix is the first member of TileMapState, so their
displacement is TileMapState's address less 4 to 32. In the original TileMapState is at 0x01A93208 and MinimapViewState
(0x01A31610, 400376 bytes) ends where it starts, so reccmp prints MinimapViewState+400372 for TileMapState - 4. In the
DLL TileMapState's instance has other data in front of it and reccmp prints that: ViewportRenderState+1699844 (it is
1699848 bytes long) with the units option on, function-resolver objects with it off. The compiler's listing has
TileMapState's symbol less 4 to 32 in all seven, and all 85 global memory operands of the function are the original's
in the original's order. Any form that reads the row above ([SomeY][-1], [SomeY - 1][7]) comes to the same displacement
(both were compiled), and the label comes from the DLL's data layout, so no source form changes these lines; main's
build shows the same seven. The status line counts the function the way the file does for 0x004D9290 (sourcehold#510: the
instance[i - 1] address label) and for 0x00405130, 0x004274F0 and 0x00429A40 (sourcehold#408).

The rest is gynt's, the handwritten block included (7005c48 79.0%, 951fc13 84.5%, ff6859a 85.9%; main's status
line still said 84.5%). The second search loses one level of nesting, so its lines show as changed. The bound of both
searches is countof(field_0x11cf4[0]) in place of 24, the column count of the table they index: same object code.

Measured with real builds on main 0cea04d (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp), of
this file together with the two /GL lines of sourcehold#541: 99.0% in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS on
(519 files, 529 functions), where every other function but sourcehold#541's clearAllUnits reads as in the same build of main.
With the option off the same list reads 99.0% as well, the same seven lines (and 98.8% for the text before the
_adjacentTiles[13] change: those seven and that one sum). Two comments and the two loop bounds changed after the
builds; every section of the object file has the same bytes and relocations as before.

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

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

19 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
9 Prefer for over while/do 1
18 Use the name, not the number 16
20 Keep nesting low; no goto 2
Where

Rule 9 — Prefer for over while/do

  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:223 — do-loop; the guide asks for a for

Rule 18 — Use the name, not the number

  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:90 — hex constant 0x80000; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:94 — hex constant 0x800; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:117 — hex constant 0x400; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:121 — hex constant 0x100000; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:145 — hex constant 0x1000; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:149 — hex constant 0x40000; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:165 — hex constant 0x100; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:169 — hex constant 0x400000; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:173 — hex constant 0x800000; use a name, or decimal
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:179 — hex constant 0x200; use a name, or decimal
  • ... and 6 more

Rule 20 — Keep nesting low; no goto

  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:493 — goto; check how many predecessors the target has first
  • src/OpenSHC/Map/Units/UnitsState/processMeleeInitiation.cpp:564 — goto; check how many predecessors the target has first

@gynt
gynt self-requested a review December 2, 2026 13:54

@gynt gynt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way to avoid the do-while and the goto?

@kotijekriv

Copy link
Copy Markdown
Contributor Author

Not that we found. The alternatives were measured with the file compiled alone (quick_diff, where 1.0 is the original's listing). The form in this PR reads 0.9796 there and 99.0% in the real build.

The do loop. for, while and for (;;) with the test inside all give main's listing (0.8976). MSVC then reloads this inside the loop and keeps a zero in edi to the end of the function. The original tests the first value once in front of the loop and keeps this in one register. Of the forms tried, only if (x > 0) { do { ... } while (x > 0); } gave that: 0.8976 -> 0.9399 before the two goto.

The two goto. Each takes out a copy the original doesn't have:

  • no goto, a flag set in the loop and tested behind it, the three stores written twice: 0.9399;
  • goto attack only: 0.9567;
  • goto noTarget only: 0.9640;
  • no goto, with the attack nested in an if on a found target so that both exits fall into the three stores: 0.9640;
  • no goto and no flag, the attack written out at both places: 0.9244.

With a flag, this comes back in ebx behind the second search, where the original has it elsewhere. With the stores written twice, both copies stay.

If you prefer it without the goto, the best form without them is the nested if (0.9640 in quick_diff; not built yet). We can switch to it and write the status line with its real reading instead of the counted 100%. Which do you want?

@gynt
gynt added this pull request to the merge queue Dec 2, 2026
Merged via the queue into sourcehold:main with commit adf06f8 Dec 2, 2026
5 checks passed
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.

2 participants