Skip to content

One more Map::Units function at 100%: UpdateMangonel - #445

Open
kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-C-round13-100
Open

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-C-round13-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateMangonel is a Map::Units function from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and both commits credit him as co-author.

Function Address Before After
Units::UpdateMangonel 0x0056A3F0 gynt's draft 42.92% (bc487ad) 100.0%, under /GL

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 with both resolver sets on: 0 failed of 2,424 compile steps, 99.57 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). UpdateMangonel and deselectUnit read 100.0% raw and normalised, with no diff entry, and every other main function reads exactly what it read before, except two that read lower, and it is how reccmp prints a constant, not their code: Menu::handleMenuItems (0x004F6280) reads 97.9 (91.4 normalised) and Menu::Constructor_Menu (0x004F4100) reads 98.4 (95.2), both 100.0 before. Both compare a menu item's type with the constant 0x11000000, five compares in all. In this build's layout a symbol of our DLL lies at exactly the address 0x11000000, so reccmp prints the constant as that symbol's name on our side. The instructions are the same on both sides, as UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435's and UI: the credits scroll's fade and the win/defeat window at 100% #437's texts describe for their builds, and these two readings are why the average is a little lower (unrounded 99.5746 against 99.5759). That build was of the first commit. The second commit holds the review fixes; the function's compiler listing is the same line for line.
  • Our own narrower run, also of the first commit: the 133 files of main's /GL list that exist on main, seven more files at 100% on main (six callees that cmake: function-visibility.txt, which callers must see which callees' bodies #398's manifest names, and activateTraderState) and the unit: 151 functions, average 99.59, UpdateMangonel at 100.0% raw and normalised. Without the unit and the two new /GL lines: 99.58 over 150, each reading the same in both builds.
  • Please measure with both resolver switches on (the Globals and the function resolvers), as our numbers are.
  • Checks: openshc-pr-fix finds nothing to change, and openshc-pr-check gives 0 ERROR and 35 CHECK (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: 72.1, 80.0, 85.9, 86.2, 89.1, 89.8 and, with the last four points, 100.0.

  • Every access reads the unit through DAT_CurrentUnitSlotID::instance. The draft used a copy of the slot in about two places out of three; with no copy the compiler arrives at the original's loads.
  • The working path is the fall-through, three times: behind findClosestEnemyBuildingWithinRadius (je at 0x0056A55C) and behind acquireShootTarget in the manned and the waiting state (0x0056A5ED, 0x0056AAF0). The draft had early exits; now the call that works is the if body and the exit comes last.
  • Two small ones: the search for engineers stops at found >= 2 (0x0056A981), and the height sums are written with the terrain heights first: our compiler then loads the building heights first, as the original does (0x0056A734, 0x0056A8F8).
  • A local for the tower under a mangonel that is gone (quick_diff 0.9472 to 0.9559): the two places that clear the tower's engine type read the building layer once.
  • The block that sends the two engineers to work is one loop with the stores in source order (to 0.9721). The original computes the unit's offset anew at the block's start (0x0056A7D7) although esi holds it: the start of a loop the compiler unrolled. Its second copy has the stores in the source's order.
  • In the two loops over a destroyed or disbanded mangonel's crew, the engineer's id is a local (to 0.9820). With the nested index written twice, the compiler makes the two walking pointers in the other order.
  • The two distance blocks write the positions out (to 0.9950, and 100.0% in reccmp). With the mangonel's own position in a short local, the search loop kept the subtraction in each arm. What quick_diff still shows at 0.9950 is not in the code (two jumps over loop padding, three renderings of 0x80000007).

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

  • UpdateMangonel.cpp and UnitsState/deselectUnit.cpp (0x00535190, at 100% on main).
  • The unit has to see deselectUnit. Behind its call at 0x0056AA21 the original still uses edx (0x0056AA26) and calls clearOrDeselectUnitFromSelection (0x0056AA31) without loading ecx again, so its compiler knew the callee leaves both alone. quick_diff: 0.9223 without the body, 0.9950 with it.
  • Nothing else moves. hasUnitReachedDestination, the other callee the unit has to see, is on main's list already (cmake: function-visibility.txt, which callers must see which callees' bodies #398's manifest has both edges). processUnitLossStatistic makes no difference (0.9950 without it) and is not added. No other file on main's list calls deselectUnit.

Kept as they are

The first two have a one-line why in the code:

  • engineersInPlace is declared at the function's start, although only the unmanned state uses it: the original clears it at 0x0056A442, in front of the state tests. In its block: quick_diff 0.9100.
  • dx and dy are declared without a value and set in both arms of an if / else. Initialised with one arm's value: 0.6028. A conditional expression gives the same code, but the style lint flags it. They are not abs calls: the original uses cdq / xor / sub for the height difference and a compare and a branch for these. The larger of the two goes into a local of its own (written into dy: 0.9112; as dx < 30 && dy < 30: 0.7970).
  • == 0 and != 0 stay on dying and killedFlagUnk, which are short fields, as in Three more Map::Units functions at 100%: UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher #421.
  • The 6 casts: (char) on the four frame-table reads (two into animationFrame, two into imageIDUnk; read with movsx) and on disappearFadeAlphaCountdown > 32 (a signed jle at 0x0056AF6B), and (int) on Building::widthOrHeight (a signed halving at 0x0056ABDF). The headers have the first five as byte, the last as uint.

The other CHECKs: 21 placeholder names (next section), the 4 dx / dy declarations, and 4 false hits (the checker takes Unit::field_0x58, a short, for another struct's BOOLEnum field of that name).

What some placeholders hold

  • Unit::field_0x3bc: a negative countdown to the next search for a target: set to -40, -80 or -200 when the mangonel finds nothing to shoot at or has to pause, and counted up by one per update in the manned and the waiting state.
  • Unit::field_0x3c5: an instruction type waiting to be taken over: this function stores UIT_ATTACK_BUILDING there next to the same value in targetingType, and the catapult and the trebuchet (Reimpl/map units updatefunctions #204 drafts, not on main) copy it into targetingType when it holds UIT_ATTACK_LAND (5) or UIT_THROW_COW (22).
  • Frames_Shared_UnitClimbingUp.field_0x1a24, .field_0x1a60, .field_0x1a9c and .field_0x1ad8: the mangonel's frame tables for loading and firing, indexed by animationCycleNumber.
  • The state names are borrowed from other units. Here US_DETERMINE_NEXT_STATEUnk (0) is manned and looking for a target, US_FIRE_WEAPONUnk (6) unmanned, US_LOOK_AROUNDUnk (7) waiting for a target, US_STAND_UPUnk (8) turning towards it, US_STATE_2Unk (2) loading, US_RELOAD_WEAPONUnk (4) firing and US_STATE_9Unk (9) disbanded; 9 is what UnitsState::setTowerSiegeEngineToIdle (0x00537E00) stores, written there as US_STAND_UPUnk | US_IDLEUnk.
  • Two status entries are not functions: 0x0056A8A0 and 0x0056AF20 are loop heads inside UpdateMangonel.

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 21, 2026 06:47
Units::UpdateMangonel, from gynt's draft bc487ad (42.92%). reccmp reads 100.0, raw and normalized, in a build of
main 3458fea with this commit: the files of main's /GL list, the files sourcehold#398's visibility manifest names, and the
unit (141 files, 151 functions, both resolver sets on). Against main alone with the same list, the other 150
functions read as before.

cmake/compiler-flags-gl.txt gets two lines, the unit and UnitsState/deselectUnit.cpp (0x00535190, 100% on main). The
unit has to see that body: behind its call at 0x0056AA21 the original still uses edx from before the call
(0x0056AA26) and calls clearOrDeselectUnitFromSelection (0x0056AA31) without loading ecx again. No other file on the
/GL list calls deselectUnit.

Three things in the text are read off the original's listing:

- The block that sends the two engineers to work is one loop over manningEngineerRef. The original computes the
  unit's offset anew at the block's start (0x0056A7D7) instead of using esi, and its second copy has the stores in
  source order while the first has the constants' loads in between: a loop the compiler unrolled.
- In the two loops over a destroyed or disbanded mangonel's crew the engineer's id is a local. With the nested index
  written twice the compiler makes the two walking pointers in the other order.
- The two distance blocks write the positions out and set dx and dy in both arms of an if / else. With one arm's
  value as the initialiser the match is lost.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- The (short) casts on the two stores into short fields are gone.
- const on the locals that are assigned once.
- The search for engineers names its counter unitID and leaves a pass early with continue.
- UT_NONE for the unit type passed to clearOrDeselectUnitFromSelection, no return at the function's end, and the
  comment on dx and dy is reworded.
- engineersInPlace stays at the function's start and gets a comment. Declared in the block that uses it, the unit
  reads 0.9100 in the quick measure; the original clears it at 0x0056A442, in front of the state tests.

The function's /FA listing is the same as at 96ab2e4, label numbers aside.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <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