Skip to content

Reimplement six UnitsState functions at 100%, from gynt's text on main - #539

Merged
gynt merged 6 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-unitsstate1-100
Dec 2, 2026
Merged

gynt merged 6 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-unitsstate1-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Six UnitsState functions that #238 put on main below 100%. The bodies are gynt's; each commit says what changed. A seventh, selectWorkerSpeechOrMoraleStateID, goes back to 100% with one of them. All read 100.0% / 100.0% (reccmp raw / normalised) with OPENSHC_FUNCTION_VISIBILITY_UNITS off and on.

Function Address Before (raw / normalised) Now What matched it
ifAnyUnitOnSameTileIsLadderInRightDirection 0x0052F5B0 62.9% / 62.9% 100.0% the wall bit tested apart from the rest of the mask
updateUnitFadeAndVisibilityNearStructures 0x00533D50 85.1% / 75.2% 100.0% vanish = 0 in its two arms, two declaration orders
isTowerTileOvercrowdedByCurrentPlayer 0x00536780 66.2% / 56.9% 100.0% off the /GL list, gynt's text
giveMoveCommand 0x00536E30 94.5% / 93.1% 100.0% _canReachDestination = FALSE in its two arms
isWorkerAtProductionIdleState 0x0053A070 48.1% / 46.2% 100.0% one arm per unit type
selectWorkerSpeechOrMoraleStateID 0x0053DDD0 83.9% / 71.1% 100.0% calls the one above; text unchanged
spawnUnit 0x0053E440 94.5% / 89.6% 100.0% the order of two groups of stores

"Before" is main's reading, the same with the option off and on. The old numbers in the status file were partly older still.

What matched them

  • Equal code written out where MSVC folds it again (four of the six). The compiler counts a variable's references before it merges equal blocks, so a store or a test written once instead of in each of its arms changes what gets a register or which stack slot:
    • isWorkerAtProductionIdleState: the arms per unit type raise the count of the state, which then takes eax (0x0053A07C). The jump table's entries stand in the order the types give.
    • giveMoveCommand: two stores of FALSE, one in the binary (0x00536F60). Only the stack slots of two locals were swapped before.
    • updateUnitFadeAndVisibilityNearStructures: two stores of 0, one in the binary (0x00533F23). With two, MSVC keeps the 0 in edx from 0x00533EAC on.
    • ifAnyUnitOnSameTileIsLadderInRightDirection: two tests of LogicLayer, one in the binary. With all twelve bits in one mask MSVC keeps the mask in ebx. The eleven bits left are the mask findFreeTileNearby and findRecentOrSignpostSpawnLocation test on main.
  • isTowerTileOvercrowdedByCurrentPlayer: the original reads the building's widthOrHeight again for each call in the loop (0x00536817), so it did not see into getBuildingSizeIndexMappingData, which is on main's /GL list. Off the list it matches as gynt wrote it (he recorded 100% in 0118583). No .cpp on main calls it.
  • spawnUnit: time directly behind the uid, the height behind the two micro positions, movementRelated in front of field_0xe8.
  • selectWorkerSpeechOrMoraleStateID: gynt had it at 100% (390b34b) until isWorkerAtProductionIdleState used edx (he says so in 1fab2cf). It no longer does.

Kept as they are

  • One line of comment in five places: why an arm or a mask is written out, with what loses the match. isTowerTileOvercrowdedByCurrentPlayer's two block comments are line comments now, and its do loop stays: a for tests first and loses the match (0.8882 against 0.9733 in quick_diff). The style-lint bot's one finding is that loop.
  • DAT_UnitsState::instance in members of UnitsState (giveMoveCommand, isTowerTileOvercrowdedByCurrentPlayer, updateUnitFadeAndVisibilityNearStructures): main's text; the original addresses the units through the global there.
  • Truth tests next to compares with 0: the truth tests are on flags: _canReachDestination, _isOnDefensiveStructure, the IsGateOrTowerArray entries and the calls that return BOOLEnum (selectionHasMobileAssaultUnits, calculatePathKeepAndWallsGatesNotAllowed, shouldSoundXNotBePlaying). The compares are on numbers: calculateCanPlayerUnitsNavigateToAreaFromArea returns int, _blockedAtDestination is a set of bits and the others are counts or indices.
  • The four locals at the top of updateUnitFadeAndVisibilityNearStructures start at 0 in that order for the match (0.9600 -> 0.9760 in quick_diff), and the tile's flags stay in a local read at the top: reading the field at its one test gives 0.9157.
  • spawnUnit: the catapult and trebuchet arms stay apart, because the original has both; one test with || gives 0.8467. (UnitLogicState)1 stays a number, because main's enum has no name for 1. field_0xe8 keeps the header's placeholder name: this function only clears it, which says nothing about what it holds.
  • isTowerTileOvercrowdedByCurrentPlayer: the selection loop keeps <= countof(...) - 1. With < MSVC compares with 27 and jl where the original has 26 and jle (0.9600 against 0.9733).
  • ifAnyUnitOnSameTileIsLadderInRightDirection: the four directions stay written out: a loop over them gives 0.3297.
  • The line taken from compiler-flags-gl.txt is on purpose, see above.

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 of this branch were made on main 4591b6f. It is cut from main 0cea04d, which adds six status lines (#538) and a tools script: nothing that is compiled.

Other PRs

#536 is ours and open. It shares no file with this one but the status file and compiler-flags-gl.txt, in other lines. prepareProjectileTarget, which needs a header change (one line in each of two headers), is in #540, ours and open; the full and final small builds above were made of both branches together. It shares only the status file with this one, in other lines.

🤖 Generated with Claude Code

kotijekriv and others added 6 commits December 2, 2026 02:52
UnitsState::ifAnyUnitOnSameTileIsLadderInRightDirection from 62.9% (gynt's text, on main since sourcehold#238) to 100%.

The wall bit is tested apart from the other eleven bits of the mask, in all four directions. MSVC makes one test of
0x4a5015b1 out of the two, as the original has it, and the first one loads the layer into a register (0x0052F5C5). With
all bits in one mask it keeps the mask in ebx over the function and nothing after the prolog lines up. The other eleven
bits are the mask findFreeTileNearby and findRecentOrSignpostSpawnLocation test on main. A loop over the four even
directions reads 0.3297 (quick_diff). The body is gynt's (aa4f048, 57.6%).

Measured with real builds of this branch's tree on main 4591b6f (bundled MSVC1400-SP1, function and data resolvers on
locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off, in one
with it on, and in a full build with it off (of this branch merged with the prepareProjectileTarget one). The comment's
new wording came after those builds. A small build with it off of the final text (this branch merged with the
prepareProjectileTarget one, 550 files, 560 functions) reads every function as the earlier small build with it off did.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UnitsState::updateUnitFadeAndVisibilityNearStructures from 85.1% / 75.2% (gynt's text, on main since sourcehold#238) to 100%.

Three edits, each measured with quick_diff (0.8032 before):

- `vanish = 0` is stored in the two arms it belongs to instead of in one arm behind an `||`. MSVC makes one store of
  them again (0x00533F23), but with two it keeps the 0 in edx from 0x00533EAC to the last test, as the original does:
  0.9600.
- The four locals that start at 0 are declared in the order fade type, on a structure, tower, gate: 0.9760.
- destinationBuildingID is read in front of destinationFlags, which puts the destination tile in esi (0x00533E5D):
  1.0000.

The body is gynt's (e3df755 56.3%, c6efbfc 77.0%, f2e1b62 85.1%).

Measured with real builds of this branch's tree on main 4591b6f (bundled MSVC1400-SP1, function and data resolvers on
locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off, in one with
it on, and in a full build with it off (of this branch merged with the prepareProjectileTarget one).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UnitsState::isTowerTileOvercrowdedByCurrentPlayer from 66.2% / 56.9% to 100% by taking it off the /GL list. The text is
gynt's but for its two comments, the name of the loop's index and the result written as TRUE / FALSE (bcbf6a4 73.7%;
0118583, where he recorded 100%).

The original reads the building's widthOrHeight again for each call in the loop (0x00536817) and loads
currentPlayerSlotID in front of the unit loop (0x00536850): it did not see into getBuildingSizeIndexMappingData, which
is on main's /GL list. No .cpp on main calls the function, so nothing else sees its body. The do loop stays: a for reads
0.8882 against 0.9733 (quick_diff; what is left of the 0.9733 is notation), and so does the selection loop's `<=
countof(...) - 1`: `<` compares with 27 and jl where the original has 26 and jle (0.9600).

Measured with real builds of this branch's tree on main 4591b6f (bundled MSVC1400-SP1, function and data resolvers on
locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off, in one
with it on, and in a full build with it off (of this branch merged with the prepareProjectileTarget one). The name of
the loop's index and the result written as TRUE / FALSE came after those builds. A small build with it off of the final
text (this branch merged with the prepareProjectileTarget one, 550 files, 560 functions) reads every function as the
earlier small build with it off did.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UnitsState::giveMoveCommand from 94.5% / 93.1% (gynt's text, on main since sourcehold#238) to 100%.

Only the stack slots of _mostFrequentUnitType and _canReachDestination were swapped. MSVC hands the dead parameters'
slots out by reference count, counted before it merges equal code. `_canReachDestination = FALSE` now stands in an arm
of its own for the blocked tile and for the failed path instead of once behind an `||`; the two stores become one again
(0x00536F60), but the variable counts one reference more and takes the first slot. The two calls that return BOOLEnum
are tested by truthiness (the same listing). The body is gynt's (742a29f 74.4%, c0b593b 94.5%).

Measured with real builds of this branch's tree on main 4591b6f (bundled MSVC1400-SP1, function and data resolvers on
locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off, in one
with it on, and in a full build with it off (of this branch merged with the prepareProjectileTarget one). The second
truth test and the plain 0 for maximumCount came after those builds. A small build with it off of the final text (this
branch merged with the prepareProjectileTarget one, 550 files, 560 functions) reads every function as the earlier small
build with it off did.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UnitsState::isWorkerAtProductionIdleState from 48.1% / 46.2% (gynt's text, on main since sourcehold#238) to 100%.

Each unit type has its own arm, in the order of the type's value, instead of one arm per state. MSVC folds the equal
arms (the jump table's order is the one the types give), but counts their references first, and the state then takes
eax as in the original (0x0053A07C) and the pointer and the type ecx (quick_diff 0.5057 -> 1.0000).

selectWorkerSpeechOrMoraleStateID 0x0053DDD0, which calls it, goes back from 83.9% / 71.1% to 100.0% / 100.0% with its
text unchanged: our function no longer uses edx. gynt described that in 1fab2cf and had it at 100% before
(390b34b). Its status line (74.4%) follows. The body here is gynt's (69a106e 64.0%, 1fab2cf 48.1%).

Measured with real builds of this branch's tree on main 4591b6f (bundled MSVC1400-SP1, function and data resolvers on
locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off, in one
with it on, and in a full build with it off (of this branch merged with the prepareProjectileTarget one). The name
US_UNKNOWN_10 for the state 10 came after those builds. A small build with it off of the final text (this branch merged
with the prepareProjectileTarget one, 550 files, 560 functions) reads every function as the earlier small build with it
off did.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UnitsState::spawnUnit from 94.5% / 89.6% (gynt's text, on main since sourcehold#238) to 100%.

Store order only. The unit's time is stored from mapTimeInTicks directly behind the uid (no local), and the height
behind the two micro positions: the scheduler then gives the original's order from 0x0053E491 on (quick_diff 0.9484 ->
0.9871). movementRelated is set in front of field_0xe8 (0x0053E58B), which swaps the last two stores: the same listing
as the original. The catapult and trebuchet arms stay apart: the original has both, and one test with || reads 0.8467.
The rest is gynt's (f012109 53.1%, 77ba3d7 92.9%, 351b48a 94.5%).

Measured with real builds of this branch's tree on main 4591b6f (bundled MSVC1400-SP1, function and data resolvers on
locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off, in one
with it on, and in a full build with it off (of this branch merged with the prepareProjectileTarget one). The names of
the constants, the last slot written from countof and the comment came after those builds. A small build with it off of
the final text (this branch merged with the prepareProjectileTarget one, 550 files, 560 functions) reads every function
as the earlier small build with it off did.

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 2, 2026

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

1 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 6 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
Where

Rule 9 — Prefer for over while/do

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

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