Skip to content

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

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

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

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

UnitsState::processUnitAttackOtherUnit (0x00531220) came to main with #238 at 79.5% / 72.1% (reccmp raw / normalised). The body is gynt's; this PR changes how it is laid out, not what it does. It reads 100.0% / 100.0% with OPENSHC_FUNCTION_VISIBILITY_UNITS off and on.

Function Address Before (raw / normalised) Now What matched it
processUnitAttackOtherUnit 0x00531220 79.5% / 72.1% 100.0% one return TRUE instead of 36, the "still alive" block last

"Before" is main's reading, the same with the option off and on. The status file said 57.0%.

What matched it

  • One return TRUE. Main's text returns TRUE from every arm, 36 times. MSVC then keeps the constant 1 in ebx from dying = 1 on and returns with mov eax, ebx, and it copies the epilogue into more arms (391 instructions against the original's 372). The original has the 1 in ecx for the word stores only (0x005313BC) and returns with mov eax, 1. Here the arms leave through break and the function returns TRUE once:
    • the special death sounds and the switch on the attacker's type stand in the default: of the switch on the victim's type;
    • isStalked == 0 stands around both switches. An early return TRUE for a stalked victim gets an epilogue of its own with mov eax, ecx, where the original jumps to the shared one (0x005314DC).
  • The block for a victim that is still alive (the under-attack sound, return FALSE) stands last, as in the original (0x005317E8), and the rest is inside health <= 0.
  • The spear arm falls through into the pike arm. It plays its own hit sound and breaks; otherwise it runs on into the pike arm, whose hit-sound test is false on that path, and gets the two death-spear sounds. The original has those two calls once (0x005316C4), reached from both arms, and it hands out the scratch registers of its calls in that order.
  • The fire thrower has an arm of its own in the chain that picks the death state, and US_STONE_DEATH_01 stands in the last else. Main's (type != UT_A_FIRETHROWER) + US_DEATH_03 adds in 16 bits; the original adds in 32 (add edx, 0x71, 0x005314B4).
  • Two orders: tunnelerFinishedDigging = 1 stands in front of _deathVariant (the order of two stores, 0x005314D0). The victim's type is read in the damage expression and _unit2Type is declared behind it, so that _unit2Type takes the dead unitID parameter's stack slot (0x0053126F) and the flag the local one.

Kept as it is

  • The nesting. The function is three levels deeper at its deepest point than on main. Each level is one of the points above, and each has a one-line comment: an early return for the surviving victim puts its block in the middle of the function (0.8898 in quick_diff), an early return TRUE for a stalked victim reads 0.9854, return TRUE in the shield's two arms 0.9557.
  • The fall-through has a one-line comment. Written out in both arms the two death sounds come out once all the same, but behind the spear arm instead of behind both (0.9542 against 0.9973).
  • _hitSound names fixedRng & 7 in the default arm, as main's processEntityDamageToUnit does. It gives the same code as the expression written out eight times. The original makes eight separate calls, each with its own constant, so it can't be one call with FX_BODY_HIT + _hitSound.
  • The death-state chain has eleven arms with the same body, the fire thrower's being new here. One test over the eleven types reads 0.8204 in quick_diff, a switch with grouped labels 0.8402. One comment line above the chain.
  • int const _unit2Type for a field that is a UnitTypeShort: as UnitTypeShort const it reads 0.7453. _hasSpecialDeathSound is declared next to it, at the top: declared at the top of the default: arm, where it is first used, it reads 0.8958. One comment line covers both.
  • _deathVariant is read behind the tunnelerFinishedDigging store, about 100 lines ahead of its first use: declared at that use it reads 0.9000. One comment line. It is uint const now, like _hitSound (the same object code).
  • Truth tests next to compares with a number: _hasSpecialDeathSound is a flag and is tested by truth; _deathVariant and _hitSound are numbers (0 or 1, 0 to 7) and are compared with their values.
  • DAT_UnitsState::instance in a member of UnitsState, twice (the owner for the fear-factor call, the owner in the under-attack test): main's text, and the original reads the owner through the global in both places (0x0053127D, 0x005317F1). this-> loses the match (0.7419 and 0.8242). Each has a one-line comment.
  • spriteID == 129, fixedRng & 1, isStalked compared with 0 and _tribeID without const are main's text.

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 branch is cut from main 0cea04d, and every build is of that main or of this branch.

  • Small builds, both modes (490 files, 500 functions: every /GL file of main, this function and five more UnitsState functions with their callers and callee closures, every file of a function-visibility group that touches them): BUILD_OK and 0 failures in both. processUnitAttackOtherUnit reads 100.0% / 100.0% in both.
    • Against the same build of main, in each mode: no other function reads differently (500 compared).
    • Its caller processMeleeInitiation reads 85.9% / 71.9% in all four builds.
  • Full build, option off (3041 files, 3059 functions; the file is on main's /GL list and stays there): BUILD_OK, 0 failures, average 99.35. Against the same build of main only processUnitAttackOtherUnit reads differently, 79.5% / 72.1% -> 100.0% / 100.0%.
  • The edits after the builds (the comment lines, const on _deathVariant) were checked by object compare, not rebuilt: the file compiled alone gives the same object code as the built text (every section but the debug ones compared byte for byte, with the relocations).
  • Checks: nothing to fix automatically; the style checks give 0 errors and 2 notes, both covered under "Kept as it is" (the truth tests, the global).
  • Not run in game.

Other PRs

#536, #539 and #540 are ours and open. They share no file with this one but the status file, in other lines. No other open PR changes processUnitAttackOtherUnit.cpp.

🤖 Generated with Claude Code

UnitsState::processUnitAttackOtherUnit from 79.5% / 72.1% (gynt's text, on main since sourcehold#238) to 100%.

One `return TRUE` at the end instead of one in every arm (36 on main). With those MSVC keeps the constant 1 in ebx from
`dying = 1` on and returns with `mov eax, ebx`; the original has the 1 in ecx for the word stores only (0x005313BC) and
returns with `mov eax, 1`. The arms now leave through `break`: the special death sounds and the switch on the attacker's
type stand in the `default:` of the switch on the victim's type, and `isStalked == 0` stands around both (an early
return for a stalked victim gets an epilogue of its own with `mov eax, ecx`). quick_diff 0.8467 -> 0.9515.

The block for a victim that is still alive stands last, as in the original (0x005317E8), with the rest inside
`health <= 0`. The spear arm plays its hit sound and otherwise falls through into the pike arm: the original has the
two death-spear sounds once (0x005316C4), reached from both arms, and hands out its calls' scratch registers in that
order. The fire thrower has an arm of its own in the death-state chain and US_STONE_DEATH_01 stands in the last else,
which gives the 32-bit `add edx, 0x71` (0x005314B4) for a 16-bit one. `tunnelerFinishedDigging = 1` stands in front of
`_deathVariant` (the order of two stores, 0x005314D0), and the victim's type is read in the damage expression and
declared behind it, so that `_unit2Type` takes the dead unitID parameter's stack slot (0x0053126F) and the flag the
local one. The same listing as the original (quick_diff 1.0000). The body is gynt's (775d904 56.1%, 017d972 56.8%,
3c8da9c 57.0%, 398ea19 79.5%).

Measured with real builds of this text on main 0cea04d (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 and in one with it
on (500 functions each). With the option off no other function reads differently from the same build of main. The
comment lines and `const` on `_deathVariant` came after those builds: the file compiled alone gives the same object code
with them (compared section by section).

Tried and not taken, each for the match (quick_diff): one test over the eleven attacker types for the death state 0.8204,
a switch with grouped labels 0.8402; `UnitTypeShort const _unit2Type` 0.7453; `_hasSpecialDeathSound` declared in the
arm that uses it 0.8958; `_deathVariant` declared at its first use 0.9000.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@gynt
gynt added this pull request to the merge queue Dec 2, 2026
Merged via the queue into sourcehold:main with commit a0dc309 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