Skip to content

Reimplement UnitsState::setDestinationForUnit at 100%, from gynt's text on main - #547

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

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

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Brings UnitsState::setDestinationForUnit (0x0053D3D0), merged below 100% in #238, to 100%. The body is gynt's; the commit says what changed and credits him as co-author. It reads 100.0% / 100.0% (reccmp raw / normalised) with OPENSHC_FUNCTION_VISIBILITY_UNITS off and on. The file comes off cmake/compiler-flags-gl.txt. No header change. The status line still had 64.2% from gynt's 154d302 (his later commits did not update it); it now says 100.0%.

Function Address Before (raw / normalised) Now What matched it
setDestinationForUnit 0x0053D3D0 71.1% / 68.9% off, 71.3% / 67.3% on 100.0% / 100.0% off and on the map test as a flag, the climb exits as if / else, the last test inverted, the despawn exits' store order, off the /GL list

What matched it

Five edits. The first three are measured in quick_diff (0.7747 on main's text, 316 instructions against the original's 270; 1.0000 with them, 270 instructions).

  • The map test is a flag (_onMap), tested once. The original falls through into its exit after the third test and goes on with jne 0x0053D439; with the test as one || condition MSVC jumps to the exit with je and puts it out of line.
  • The climb exits as if / else. In the two climb branches (under climbIsIllegal != 0, and under allAssassinsUnk == 0 && reusePathingInfo == 0) the register call and its -1 exit stand inside if (climbDataID != 0), and the climbDataID == 0 exit is its else. The original has one despawn block, at the first branch's -1 exit (0x0053D635), and the other exits jump into it; with the climbDataID == 0 exits first in the text MSVC puts that block at the end of the function.
  • The last test inverted (<= 0 becomes > 0). The success stores come first, inside if (_pathPlanSize > 0), and the despawn exit after them. With the exit first, its unitID load took edx and stayed a stub of its own (push edx; jmp); in the original it jumps into the first branch's block (jle 0x0053D635). In the success block the ladder exit's x and y are now stored after the destination tile, as in the first success block; main's text stores the y before it.
  • The despawn exits' store order. All seven despawn exits clear climbIsIllegal before allAssassinsUnk. Main's text had the other order in all seven; the original's shared block stores climbIsIllegal first (0x0053D63E). quick_diff can't see the order of two global stores, so only the real build showed it.
  • Off the /GL list. The original loads ecx again before every call. With the file on the /GL list MSVC sees that copyData, on the list too, keeps ecx, and drops the load before fillMemory_ByteValue (0x0053D729). computeLadderClimbPath, which makes the same two calls and reads 100% on main, is not on the list either.

The first real build of the first three edits (option off, the file still on the list, main's store order) read 98.9% / 95.9%, with exactly the last two differences; with all five the function reads 100%.

Fewer edits stay below 100% in quick_diff: the inverted test alone on main's text reads 0.8191, with the flag 0.8885, with the if / else but no flag 0.9234.

No behaviour change against main's text: the map test is the same condition by De Morgan, the inverted tests keep each branch's code, and the swapped stores write different fields before the same call or return.

Kept as they are

  • Gynt's names, his comments (now // lines) and the order of the stores in the first success block. One // line each above the flag, the two else exits and the inverted last test says why the plainer form loses the match.
  • _pathPlanSize is assigned in both arms and read after them, so declaring it where it is first assigned means int const _pathPlanSize = reusePathingInfo != 0 ? … : …; with a MACRO_CALL_MEMBER call in each arm. That compiles to the same object code (file compiled alone and compared), but the guide prefers if / else over ?: (SG 23), so main's form stays.
  • _onMap is a bool; reusePathingInfo, the counts and the fields are ints compared with 0.
  • this->units[unitID] throughout, as on main.

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. The full builds are of main 40cbb1c and of this branch on it. The small builds are of main 643f78b and of this branch on that, before it was rebased onto 40cbb1c, which changes only NeighbourFlagsGlobalAsm.hpp (the MACRO_ASM_JZ_NEAR macro and its comment); no file in src includes that header.

  • Full builds, option off, of main 40cbb1c and of this branch, each merged with our Reimplement UnitsState::prepareProjectileTarget at 100%, from gynt's text on main (one header line each in two files) #540 so that prepareProjectileTarget.cpp compiles, and leaving out findBestAttackTileByPathCost and resetTileAndClearMoat (they don't compile on main until Reimplement 66 TroopValueState functions at 100% #501 and Reimplement 67 Map::TileMapState functions (batch 2) #396) and TransformAndRenderPercentage (on our list from before Reimplement 59 TextManager and UserTextHandler functions at 100% #509): 3,102 files, BUILD_OK, 3,120 functions compared. Only setDestinationForUnit reads differently, 71.1% / 68.9% -> 100.0% / 100.0%, besides handleMenuItems and Constructor_Menu (97.9% / 91.4% and 98.4% / 95.2% on main, 100.0% / 100.0% here), which go between these two readings from one build to another with their files unchanged; we have seen it in most of our full builds since 2026-11-29. The branch built was b02b1a376c; the head differs from it in comments only.
  • Small builds, both modes (592 files, 602 functions: every /GL file of main, every .cpp that calls setDestinationForUnit, updateUnits or acquireShootTarget, the callee closures and every file of a function-visibility group that touches them; main's prepareProjectileTarget.cpp left out: main's header declares it void since d134c77 while the .cpp still returns int, and our Reimplement UnitsState::prepareProjectileTarget at 100%, from gynt's text on main (one header line each in two files) #540 changes the .cpp): BUILD_OK and 0 failures in all four.
    • Option off: against the same build of main only setDestinationForUnit reads differently, 71.1% / 68.9% -> 100.0% / 100.0% (602 compared).
    • Option on: against the same build of main only setDestinationForUnit reads differently, 71.3% / 67.3% -> 100.0% / 100.0% (602 compared).
  • After the builds only comments changed: gynt's three block comments became // lines and the four lines on the plainer forms were added. The file compiled alone gives the same object as the built text (every section but .debug$*: same bytes and relocations, in the same order).
  • The style-lint bot will flag _pathPlanSize (SG 24); answered under "Kept as they are".
  • Not run in game.

Other PRs

No open PR touches setDestinationForUnit.cpp. Every open PR that changes cmake/compiler-flags-gl.txt or the status file merges with this branch exactly as it merges with main (git merge-tree against both): ours (#536, #540, #544 and the drafts #313, #327 and #328) cleanly, and those that already conflict with main in compiler-flags-gl.txt (#234, #241, #275, #501, #527, #528 and #529) in the same files and no others. #540 (prepareProjectileTarget) is why that file is left out of the small builds above.

🤖 Generated with Claude Code

UnitsState::setDestinationForUnit from 71.1% / 68.9% (option off; 71.3% / 67.3% on) to 100%. The text is gynt's, on
main since sourcehold#238 (8fa4bea). His commits on it, newest first: f3c6879 (71.3%), eb81a28, 86bd3a0, 154d302,
f601aa4, 305507f. The status line still had 64.2% from 154d302; his later commits did not update it.

Five edits:

- The map test is a flag (`_onMap`), tested once. The original falls through into its exit after the third test and
  goes on with `jne 0x0053D439`; with one `||` condition MSVC jumps to the exit with `je` and puts it out of line.
- In the two climb branches (under `climbIsIllegal != 0`, and under `allAssassinsUnk == 0 && reusePathingInfo == 0`)
  the register call and its -1 exit stand inside `if (climbDataID != 0)`, with the `climbDataID == 0` exit as its
  `else`. The original has one despawn block, at the first branch's -1 exit (0x0053D635), and the other exits jump
  into it; with the `climbDataID == 0` exits first in the text MSVC puts that block at the end of the function.
- The last test is inverted (`<= 0` becomes `> 0`): the success stores come first, inside `if (_pathPlanSize > 0)`,
  and the despawn exit after them. With the exit first in the text its unitID load took edx and stayed a stub of its
  own (`push edx; jmp`); the original's exit jumps into the first branch's block (`jle 0x0053D635`). In the success
  block the ladder exit's x and y are stored after the destination tile, as in the first success block.
- All seven despawn exits clear climbIsIllegal before allAssassinsUnk. Main's text had the other order in all seven;
  the original's shared block stores climbIsIllegal first (0x0053D63E). quick_diff can't see the order of two global
  stores, so only the real build showed it.
- The file is off the /GL list: the original loads ecx again before every call, and with /GL MSVC drops the load
  before fillMemory_ByteValue (0x0053D729) because it sees that copyData keeps ecx.

No behaviour change against main's text: the map test is the same condition by De Morgan, the inverted tests keep each
branch's code, and the swapped stores write different fields before the same call or return.

quick_diff: 0.7747 on main's text, 1.0000 with the edits (270 instructions as the original). Fewer edits stay below:
the inverted test alone 0.8191, with the flag 0.8885, with the if / else but no flag 0.9234.

After the builds: gynt's three block comments are // lines, and one // line each says why the flag, the two else exits
and the inverted last test are needed. The file compiled alone gives the same object as the built text's (every
section but .debug$*: same bytes and relocations).

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

github-actions Bot commented Dec 3, 2026

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
24 Declare variables upon first use 1
Where

Rule 24 — Declare variables upon first use

  • src/OpenSHC/Map/Units/UnitsState/setDestinationForUnit.cpp:184 — declare _pathPlanSize where it is first assigned

@gynt
gynt added this pull request to the merge queue Dec 4, 2026
Merged via the queue into sourcehold:main with commit f28b77b Dec 4, 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