Skip to content

One more Map::Units function at 100%: UpdateTrebuchet - #453

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

kotijekriv wants to merge 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-C-round14-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateTrebuchet 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. It shares most of its code with UpdateMangonel (#445) and UpdateCatapult (a draft in #204).

Function Address Before After
Units::UpdateTrebuchet 0x00569410 gynt's draft 42.59% (7a7639c) 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 of the first commit on main 3458fea, with both resolver sets on, its three /GL lines, and 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out with 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; 3458fea only adds files under tools/). UpdateTrebuchet reads 100.0 raw and normalised there too, with no diff entry, and deselectUnit and isAIPlayer read 100.0 raw and normalised under /GL. Every main function reads exactly what it read before (unrounded 99.5761 against 99.5759), including isAIPlayer's one caller on main, BuildingsState::updateLordLadyJesterAndGhostUnits, which is not on the /GL list (100.0 in both builds). 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), isAIPlayer.cpp and the unit: 152 functions, average 99.59. UpdateTrebuchet reads 100.0% raw and normalised, with no diff entry, and deselectUnit and isAIPlayer read 100.0% under /GL. Main alone with the same list without isAIPlayer.cpp and the unit: 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 (on the first commit), and openshc-pr-check gives 0 ERROR and 48 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: 76.8, 80.9 (isAIPlayer's body visible), 82.7, 84.6, 85.2, 97.6 and 100.0.

  • The unit is read through DAT_CurrentUnitSlotID::instance where the original reads the slot again, and through the unitID copy where it keeps the slot in a register (settled by measuring).
  • createTribe gets the owner of units[i], with i the counter the search for engineers left behind (imul ebp, ebp, 0x490 at 0x00569A71), as in the original; the draft passed the trebuchet's new owner. After the search i 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. The search for the capturing owner has a variable of its own, copied to the crew owner at the end of the capture (mov eax, edi at 0x00569AA3).
  • Update (2026-11-22, a comment-only commit): addUnitToTribe gets its arguments the other way round from its parameters and from every caller on main: the new tribe, then the trebuchet (push ecx with the slot, then push eax with createTribe's result, 0x00569A91 / 0x00569A92). addUnitToTribe takes (unitID, tribeID): its body multiplies the first by the unit size and the second by the tribe size. So the original adds the unit whose id is the new tribe's to the tribe whose id is the trebuchet's slot, and a slot of 1250 or more indexes past tribes[1250]: most likely a bug as well, kept for the match and now marked fixme: in the code. The draft has the same order.
  • The block that sends the three engineers to work is one loop. The original computes the unit's offset anew at the block's start (0x005697BA) although esi holds it, the start of a loop the compiler unrolled; its second and third copy have the stores in the source's order.
  • In the loop over a destroyed trebuchet'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 height sums are written with the terrain heights first. The original loads the trebuchet's buildingHeight first (0x00569715, 0x00569923); written in that order, as the draft had it, our compiler loads the terrain heights first.
  • The test of the squared distance to the target has the differences written out (97.6 to 100.0, together with the next point). As dx and dy locals, each used twice, they were served registers before the two targets and took the scratch registers the original gives the targets (ecx, edx).
  • One dx / dy pair for the whole function, used by both engineer loops. Without the target block's pair, the loop that searches engineers lost its match (the other engineer's x went to edx instead of edi, and the subtraction stayed in each arm), and no form of that block, nor a shift of the translation unit's symbol count, brought it back. One pair for the whole function does, so the original's source most likely declared them once.

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

  • UpdateTrebuchet.cpp, UnitsState/deselectUnit.cpp (0x00535190) and GameSynchronyState/isAIPlayer.cpp (0x004010B0), both at 100% on main.
  • The unit has to see both bodies. Behind deselectUnit at 0x00569B25 the original still uses edx (0x00569B2A) and calls clearOrDeselectUnitFromSelection without loading ecx again (0x00569B35). Behind isAIPlayer (0x0056A05B, 0x0056A124) it pushes the slot from ebx for the next call without reading it again; behind TryAcquireAmmunitionOrPlanToBuyStone, which it cannot see, it reads it again (0x0056A06F). quick_diff: 0.7925 without deselectUnit's body, 0.9132 without isAIPlayer's, 0.9818 with both (the rest is one jump over loop padding at 0x0056A35B and the renderings of 0x80000007).
  • hasUnitReachedDestination, the third callee the unit has to see, is on main's list already. processUnitLossStatistic makes no difference to it (0.9818 without it) and is not added.
  • Nothing else moves: isAIPlayer's one other caller on main, BuildingsState::updateLordLadyJesterAndGhostUnits, is off the list, so it never sees the body, and nothing on main calls deselectUnit. In our narrower run each of the 150 other functions reads the same.
  • cmake: function-visibility.txt, which callers must see which callees' bodies #398's manifest has UpdateTrebuchet with hasUnitReachedDestination and deselectUnit; the isAIPlayer edge will be added there.

Kept as they are

Seven shapes 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 0x00569465, in front of the state tests, as in One more Map::Units function at 100%: UpdateMangonel #445. In its block: quick_diff 0.9501.
  • dx and dy are declared at the start without a value (above).
  • The differences in the distance test are written out (above).
  • The search for the capturing owner is an endless loop with both exit tests in its body, the bound first; the compiler turns it into the original's loop (updated 2026-11-22: this said "in the original's order", which the listing does not show). With the exit test in the loop header (three forms): 0.9437 to 0.9501.
  • The shot in the cow arm is written once, behind a flag: the original has one shootProjectile call (0x0056A164) for its paths.
  • The three counters of state 6 keep += 1. The other eight += 1 are ++ and the four -= 1 are --, all with the same code; with these three as ++ too the search for engineers gets other registers (0.9709).
  • searched is declared in front of the search for engineers, because createTribe reads units[searched] after it.

And without a comment:

  • The larger of dx and dy is max(dx, dy) (windef.h's macro, as main uses it), which is the same expression as the draft's ternary. A local of its own for it, or writing it into dy or dx, loses the match (0.9709, 0.8086, 0.8023).
  • The 9 casts: (char) on the six frame-table reads (read with movsx), on field_0x3c5 copied into targetingType (movsx ax, al at 0x00569582), and on disappearFadeAlphaCountdown > 32 (a signed compare); (int) on Building::widthOrHeight (a signed halving). The headers have these as byte and uint.

The other CHECKs: 3 for the += 1 above, 4 for the declarations above, 28 placeholder names (next section), and 3 false hits 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: the negative countdown to the next search for a target, as in One more Map::Units function at 100%: UpdateMangonel #445 (-40 or -80 here).
  • Unit::field_0x3c5: an instruction type waiting to be taken over: copied into targetingType when it holds UIT_ATTACK_LAND or UIT_THROW_COW, and set to UIT_NO_INSTRUCTION_OR_MOVEUnk after a cow is thrown.
  • Unit::field_0x3b0: set while a diseased cow is to be thrown (also when the AI decides on one through AICState::yieldCowThrow), and cleared after the shot.
  • Frames_Shared_UnitClimbingUp.field_0x183c, .field_0x18a0, .field_0x1904 (loading) and .field_0x196c, .field_0x19a8, .field_0x19e4 (firing): the trebuchet's frame tables into animationFrame, imageIDUnk and imageID2, indexed by animationCycleNumber.
  • The state names are borrowed from other units, as in One more Map::Units function at 100%: UpdateMangonel #445: 0 manned and looking for a target, US_FIRE_WEAPONUnk (6) unmanned, US_STAND_UPUnk (8) aiming, US_STATE_2Unk (2) loading, US_RELOAD_WEAPONUnk (4) firing.
  • One status entry is not a function: 0x0056A360 is the head of the loop over a destroyed trebuchet's crew (0x0056A35B jumps to it).

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 21, 2026 13:33
Units::UpdateTrebuchet, from gynt's draft 7a7639c (42.59%). reccmp reads 100.0, raw and normalized, in a narrower
build of main 3458fea with this commit: the files of main's /GL list, the callees sourcehold#398's visibility manifest
names, isAIPlayer.cpp and the unit (142 files, 152 functions, both resolver sets on). Against main alone with main's
list, the other 150 functions read as before.

cmake/compiler-flags-gl.txt gets three lines: the unit, UnitsState/deselectUnit.cpp (0x00535190) and
GameSynchronyState/isAIPlayer.cpp (0x004010B0), both at 100% on main. The unit has to see both bodies: behind its
calls to them (0x00569A25, 0x0056A05B, 0x0056A124) the original passes the slot from ebx to the next call without
reading it again. No other file on the /GL list calls either.

What the code takes from the original's listing:

- createTribe gets the owner of units[i], with i the counter the search for engineers left behind (0x00569A71).
- The block that sends the three engineers to work is one loop: the original computes the unit's offset anew at its
  start (0x005697BA), the start of a loop the compiler unrolled.
- The distance test to the target has its differences written out; as dx and dy locals they take the registers the
  original gives the targets.
- One dx / dy pair serves both engineer loops. With a pair per loop the search for engineers loses its match once
  the target block has no pair of its own.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- field_0x3c5 is compared with UIT_ATTACK_LAND and UIT_THROW_COW and set to UIT_NO_INSTRUCTION_OR_MOVEUnk.
- harassBuildingsWithSiegeAI's result is tested by truthiness.
- The comment on createTribe's argument is a fixme: the original passes units[i].owner with i = maxUnitCount.
- `--` for the four `-= 1`, and `++` for eight of the eleven `+= 1`. The three counters of state 6 keep `+= 1`, with
  a comment: with them as `++` too, the search for engineers gets other registers.
- const on ownX and ownY.
- The search's counter is `searched`, initialised where it is declared (it no longer shares its name with the crew
  loop's i), and present is engineersInPlace, as in UpdateMangonel.

The function's /FA listing is the same as at 6757ed1, apart from the two renamed locals' names.

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

Two comments, no code line changes; quick_diff gives the same listing line for line.

- addUnitToTribe takes (unitID, tribeID): its body multiplies the first by the unit size and the second by the
  tribe size, and every caller on main passes them so. The original passes the new tribe first and the
  trebuchet's slot second (push ecx with the slot, then push eax with createTribe's result, 0x00569A91 /
  0x00569A92). That is most likely a bug in the original, next to the createTribe one; it is kept for the match
  and now marked fixme.
- The capture search's comment said its two exit tests are in the original's order; our source tests the bound
  first and the compiler turns it into the original's loop, so the comment says that now.

Co-authored-by: Gynt <gynt@users.noreply.github.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