Skip to content

One more Map::Units function at 100%: UpdateBatteringRam - #471

Open
kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-C-round16-100
Open

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-C-round16-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateBatteringRam is a Map::Units function from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and the commit credits him as co-author. It shares most of its code with UpdateCatapult (#469), UpdateMangonel (#445) and UpdateTrebuchet (#453).

Function Address Before After
Units::UpdateBatteringRam 0x005459B0 gynt's draft 52.8% (#204, 974c32f) 100.0%, under /GL

Our work started from his 0ab0162 (52.83%); the two commits #204 has on the file since then are a style pass and a build fix. His later copy with other names, on his fork's reimpl/Wilps-2026-10-18 (8ca7861), already passes units[i].owner to createTribe, as the original does.

Measured.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie.
  • Full build: an all-on build of this commit (769388f) on main 25d2e9e, with both resolver sets on and the branch's own /GL list (main's plus the two lines this commit adds), and with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out and its resolver off, as in main's baseline: 0 failed of 2,424 compile steps, 99.58 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; since then main has only added files under tools/ (3458fea, feat(import): update requirements and documentation of tools/import #456) and the optional function-visibility units (cmake: optional wrapper units built from function-visibility.txt #399, cmake: function-visibility.txt, which callers must see which callees' bodies #398), off by default). UpdateBatteringRam reads 100.0 raw and normalised there too, with no diff entry. deselectUnit and hasUnitReachedDestination, on the /GL list with it, read 100.0 raw and normalised as they do on main. No main function reads differently: every one reads exactly what it read before (unrounded 99.5761 against 99.5759 raw, 99.5494 against 99.5492 normalised; 2,323 against 2,322 functions at 100.0 raw). Every function also reads exactly what it read in the same build of this commit's previous cut.
  • Our own narrower run: the 133 files of main's /GL list that exist on main, seven more files at 100% on main (six callees that the visibility manifest names, among them deselectUnit, and activateTraderState) and the unit: 151 functions, average 99.59. UpdateBatteringRam reads 100.0% raw and normalised, with no diff entry, and deselectUnit and hasUnitReachedDestination read 100.0% under /GL. Main alone with the same list without the unit: 99.58 over 150, each reading the same in both builds. That run was of this commit.
  • Please measure with both resolver switches on (the Globals and the function resolvers), as our numbers are.
  • Checks: our local style fixer finds nothing to change, and our local style checker gives 0 ERROR and 15 CHECK on the branch (explained below).
  • Not run in game.

What got it to 100%

The draft's statements are nearly all still there; what changed is their arrangement and what the compiler can see. On our dev branch (not public), reccmp raw under /GL: 85.4 (the changes of the Mangonel family: one counter for the function, createTribe's owner, the search as a loop with a flag, the slot through the global), 90.6 (newOwner of its own and memset), 99.7 (the scan's six tests as statements) and 100.0 (engineersInPlace first).

  • Every access reads the unit through DAT_CurrentUnitSlotID::instance, except one store (below).
  • createTribe gets the owner of units[searched], with searched the counter the scan for engineers left behind (imul ebp, ebp, 0x490 at 0x00545EBA), as in the original; Reimpl/map units updatefunctions #204's draft passes the ram's new owner (the Wilps copy already has units[i].owner). After the scan searched is maxUnitCount, so the tribe gets the owner of the slot past the last unit: most likely a bug in the original, kept for the match and marked fixme: in the code, as in One more Map::Units function at 100%: UpdateTrebuchet #453.
  • addUnitToTribe gets its arguments the other way round from its parameters and from every caller on main: the new tribe, then the ram (push ecx with the slot, then push eax with the tribe, 0x00545EDA / 0x00545EDB), where its body takes the unit first and the tribe second. So the original adds the unit whose id is the new tribe's to the tribe whose id is the ram's slot, and a slot of 1250 or more indexes past tribes[1250]: most likely a bug as well, kept for the match and marked fixme: in the code, as in the Catapult and One more Map::Units function at 100%: UpdateTrebuchet #453. The draft has the same order.
  • The loop that checks whether the AI's four engineers are back leaves by three separate tests, and the scan for engineers skips by six separate tests (90.6 to 99.7 for the scan). The original keeps a zero in edx across both loops: made again right behind the first (0x00545BCE, in front of a test that does not use it) and on the second's back edge (0x00545D30, behind the jump at 0x00545D2B), and it compares dying with dx (0x00545D40). As one chain, the constant 1 also moved from ecx (0x005459D6) to edi.
  • engineersInPlace is the function's first statement (99.7 to 100.0). The original copies the owner into displayColorPlayerID through ax (0x005459FB) and clears the counter behind it (0x00545A15); declared behind the two owner copies, the counter made our compiler use dx for the copy. Anywhere in front of the first copy gives the same code.
  • The search for the capturing owner has a variable of its own, copied to the crew's owner at the end of the capture (mov eax, edi at 0x00545EEC), and is a loop from 1 to 9 with a found flag: the original tests first, counts to 9 and returns behind the loop (0x00545E50 to 0x00545E5D).
  • The nine engineer counts are zeroed with memset: the original uses a fresh xor eax, eax for the nine stores (0x00545CF2) although edx holds its zero; the draft had a loop.
  • Smaller ones, each read off the listing: the engineer's id is a local in the loops over the crew; the distance compares are the original's way round (the other unit's position against the ram's). The height sums are written terrain heights first: the compiler reorders the terms, and the original's own order differs (0x00545B34, 0x00545D74). The FALSE comparisons and the == 0 on setDestinationNearTargetedBuilding and moveUnitToBehaviorTarget are truth tests, all += 1 are ++, the loop bounds are countof(...), and 0 for the unit type and the hit sound are UT_NONE and HSM_NONE, with the same code.

Behaviour against the draft. One place differs from #204's draft: the owner passed to createTribe (above). If maxUnitCount can reach 2500, the length of units, the original reads one element past the array there (inside UnitsState, but not a unit). The draft reads the slot through its local copy where the original keeps the slot from the start in ebx and esi (behind hasUnitReachedDestination, deselectUnit and stopUnitIfNextToTarget too), and through DAT_CurrentUnitSlotID::instance where the original reads it again. This code reads the global everywhere but one store. Our compiler sees that the first two callees leave the slot alone and keeps it in the same registers, so both give the original's code; as sources they would differ only if one of those callees changed the current slot. Everything else was read side by side, state by state, and behaves the same; ownLossesArray in 0ab0162, renamed in #204 since, is today's hitsDealtToPlayerArray (the same offset, 0xA8).

Lines added to cmake/compiler-flags-gl.txt

  • UpdateBatteringRam.cpp and UnitsState/deselectUnit.cpp (0x00535190, at 100% on main).
  • The unit has to see deselectUnit's body. In the loop that sends the four engineers to work the original reads the engineer's owner through edx behind deselectUnit (0x00545F7A) and calls clearOrDeselectUnitFromSelection without loading ecx again (0x00545F85). quick_diff: 0.7870 without deselectUnit's body, 0.9884 with it (the rest is quick_diff's own renderings: the jump over loop padding at 0x00545AFB, 0xFFFF, the stack arrays' offsets and call edi for timeGetTime).
  • hasUnitReachedDestination, which the unit also has to see (0.5635 without it), is on main's list already. processUnitLossStatistic makes no difference (0.9884 without it) and is not added.
  • Nothing else moves: nothing on main calls deselectUnit, and in our narrower run each of the 150 other functions reads the same.
  • cmake/function-visibility.txt gets an entry for UpdateBatteringRam with hasUnitReachedDestination and deselectUnit, as the Catapult's, Trebuchet's and Mangonel's entries have, at its address (between UpdateLady and UpdateJuggler).

Kept as they are

Five shapes have a one-line why in the code:

  • engineersInPlace is declared at the function's start (above). In the AI check's block: quick_diff 0.8274 against 0.9884; behind the owner copies it is the 99.7 code.
  • The AI check's three exits are three statements. As one || chain: 0.9105 against 0.9884.
  • The scan's six tests are six statements. As one || chain with continue, or as one && chain around the rest: 0.9448 each.
  • searched is declared in front of the scan, because createTribe reads units[searched] after it (the fixme: above).
  • The state store behind stopUnitIfNextToTarget goes through the copy of the slot made at the start. The original stores through esi, the unit's offset from the start, without reading the slot again (0x005464D3), so either its compiler saw that the callee leaves the slot alone, or its source read the slot through a copy there. stopUnitIfNextToTarget (0x00534240) is not reimplemented, so its body cannot be made visible; the copy gives the same code. Declared in the move state, in front of the call, the copy loses the match (quick_diff 0.9860 against 0.9884).

And without a comment:

  • dx and dy are declared without a value and set in both arms of an if / else, a pair in each of the two engineer loops, and the larger is max(dx, dy), as in the Catapult. A conditional expression gives the same code, but the style lint flags it.
  • The three casts: (char) on the two frame-table reads (read with movsx) and on disappearFadeAlphaCountdown > 32 (a signed compare). The headers have these as byte.

The other CHECKs: 4 for the dx / dy declarations above, 2 for the two loops without their counter in the header (searched, and newOwner, which the capture reads after its search), 1 for the manifest line (a file outside src/OpenSHC), 4 placeholder names (next section), and 1 false hit on Unit::field_0x58, a short, which the checker takes for another struct's BOOLEnum field of that name.

What some placeholders hold

  • Unit::field_0x3bc (Wilps retargetDelayOrSiegeTile): cleared when the ram reaches its destination (in One more Map::Units function at 100%: UpdateMangonel #445 and One more Map::Units function at 100%: UpdateTrebuchet #453 the negative countdown to the next search for a target).
  • Unit::field_0x58 (Wilps faceViewer): cleared in the unmanned state, as in the other siege units.
  • Frames_Shared_UnitClimbingUp.field_0x83a8 (Wilps animationFrames.animBatteringRamSwing): the ram's swing frame table into animationFrame, indexed by animationCycleNumber; at frame 0 the swing starts again, and frame 10 sets DAT_UnitHasBecomeIdle (Wilps DAT_UnitAnimationFinished), behind which the hit is dealt; the gate warning's time is DWORD_00ee1044 (Wilps TIME_LastRamGateWarningMoment).
  • The state names are borrowed from other units, as in One more Map::Units function at 100%: UpdateMangonel #445: 0 manned, US_FIRE_WEAPONUnk (6) unmanned, US_MELEE_ATTACK_WALL swinging at a gate or wall, US_UNKNOWN_108 (108) standing still (speed 0, frame offset 1).
  • The engineers' 109 (US_JESTER_ROAM_TO, Wilps US_FADE_TRANSITION) fades them into the ram: disappearFadeAlphaCountdown (Wilps fadeLevel) from 0 in steps of engineerManningSiegeStateRef_checkType (Wilps fadeAlphaStep, 2 here), then the state in cachedState (Wilps stateAfterFade, 5 here).
  • Three status entries inside the function are loop heads, not functions: 0x00545B00 (the AI check's loop, entered by the jump at 0x00545AFB), 0x00545D30 (the scan's back edge) and 0x00545F00 (the loop that sends the crew to work, entered by the jump at 0x00545EF7).

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

Units::UpdateBatteringRam, from gynt's draft 0ab0162 (52.83%). reccmp reads 100.0, raw and normalized, in a
narrower build of main 25d2e9e with this commit: the files of main's /GL list, the callees the visibility
manifest names and the unit (both resolver sets on). Against main alone with main's list, the other functions
read as before.

cmake/compiler-flags-gl.txt gets two lines: the unit and UnitsState/deselectUnit.cpp (0x00535190, at 100% on
main). The unit has to see deselectUnit's body: in the loop that sends the four engineers to work the original
reads the engineer's owner through edx behind deselectUnit (0x00545F7A) and calls clearOrDeselectUnitFromSelection
without loading ecx again (0x00545F85). hasUnitReachedDestination, which it also has to see, is on main's list.
cmake/function-visibility.txt gets an entry for the unit with these two callees, at its address.

What the code takes from the original's listing:

- engineersInPlace is declared first: the original copies the owner into displayColorPlayerID through ax
  (0x005459FB) and clears the counter behind it (0x00545A15); declared behind the owner copies, the counter made
  our compiler use dx for the copy.
- The loop that checks whether the AI's four engineers are back leaves by three separate tests, and the scan for
  engineers skips by six separate tests. The original keeps a zero in edx across both loops (made again behind the
  first at 0x00545BCE and on the second's back edge at 0x00545D30); written as one chain, either loop loses the
  match (quick_diff 0.9105 and 0.9448 against 0.9884).
- The search for the capturing owner is a loop from 1 to 9 with a found flag: the original tests first, counts to
  9 and returns behind the loop (0x00545E50 to 0x00545E5D).
- createTribe gets the owner of units[searched], with searched the counter the scan left behind (imul ebp at
  0x00545EBA); sourcehold#204's draft passes the ram's new owner. After the scan searched is maxUnitCount: most likely a
  bug in the original, kept for the match and marked fixme.
- addUnitToTribe gets the new tribe first and the ram second, as in the original (0x00545EDA / 0x00545EDB),
  although it takes (unitID, tribeID): most likely a second bug of the original, marked fixme like the first.
- Every access reads the unit through DAT_CurrentUnitSlotID::instance, except the state store behind
  stopUnitIfNextToTarget (0x00534240, not reimplemented), which goes through the copy made at the start: the
  original stores through esi there (0x005464D3).

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>

This branch has not been deployed

No deployments
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.

1 participant