Skip to content

One more Map::Units function at 100%: UpdateCatapult - #469

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

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

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateCatapult 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 UpdateMangonel (#445) and UpdateTrebuchet (#453).

Function Address Before After
Units::UpdateCatapult 0x00568320 gynt's draft 45.0% (#204, 974c32f) 100.0%, under /GL

Our work started from his efbb0df (44.97%); the two commits #204 has on the file since then are a style pass and a build fix.

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's previous cut on main 812d149 (its code differs from this one in comments, one else { if } now written else if, and one bound now written countof(crewCount); quick_diff gives the same listing line for line), with both resolver sets on and the branch's own /GL list (main's plus the three 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; 3458fea only adds files under tools/, and cmake: optional wrapper units built from function-visibility.txt #399 and cmake: function-visibility.txt, which callers must see which callees' bodies #398 add the optional function-visibility units, off by default). UpdateCatapult reads 100.0 raw and normalised there too, with no diff entry. deselectUnit and isAIPlayer, now on the /GL list, 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).
  • 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, and activateTraderState), isAIPlayer.cpp and the unit: 152 functions, average 99.59. UpdateCatapult reads 100.0% raw and normalised, with no diff entry, and deselectUnit and isAIPlayer read 100.0% under /GL. Main alone, the same list without isAIPlayer.cpp and the unit: 99.58 over 150, each reading the same in both builds (main's sources and /GL list; its tree differed only in a function-visibility.txt line the default build does not read). 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; our local style checker gives 0 ERROR and 54 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.6, 86.6 (every access through the slot global, isAIPlayer's body visible), 86.8 (the height sums), 87.5, 94.2, 96.5 (the distance test) and 100.0 (the check loop's three tests).

  • Every access reads the unit through DAT_CurrentUnitSlotID::instance; the draft's local copy of the slot is gone.
  • createTribe gets the owner of units[searched], with searched the counter the search for engineers left behind (imul ebx, ebx, 0x490 at 0x005688CB), as in the original; the draft passed the catapult's new owner. After the search 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 catapult (push edx with the slot, then push eax with the tribe, 0x005688EB / 0x005688EC), 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 catapult'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. The draft has the same order, and so has One more Map::Units function at 100%: UpdateTrebuchet #453.
  • The moving state stores speed -1 only for a delay above 70 (jle at 0x005693A9). The draft had it from 70 on.
  • The loop that checks whether the AI's two engineers are back leaves by three separate tests (96.5 to 100.0). The original keeps a zero in edx across that loop: made in front of it (0x00568554), again on its back edge (0x00568564) and again behind it (0x00568631), where the first test's exit jumps one instruction further (to 0x00568633) because edx still holds it. Written as one || chain with one break, our compiler made the zero only inside that block.
  • The block that sends the two engineers to work is one loop. The original computes the unit's offset anew at the block's start (0x0056863E) although esi holds it, the start of a loop the compiler unrolled, as in One more Map::Units function at 100%: UpdateMangonel #445 and One more Map::Units function at 100%: UpdateTrebuchet #453.
  • In the loop over a destroyed catapult'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 test of the squared distance to the target has the differences written out (94.2 to 96.5). As dx and dy locals they took the scratch registers the original gives the two targets (ecx, edx).
  • The store of -40 to field_0x3bc is written in each arm of the test on whether the owner is an AI, where the draft had one store behind the test.
  • The two height sums are written with the terrain heights first. The original loads the catapult's buildingHeight first (0x00568597, 0x00568792); written in that order, as the draft had it, our compiler loads the terrain heights first.
  • Smaller ones, each read off the listing: the engineer counter is a local of the whole function (its store sits at 0x00568372); the four tests of PathFindingState::field_0x9c between the path searches are <= 2 (2 in esi; cmp [field], esi at 0x00568A96 and three more, each with jg); the 16-tick test is ((fixedRng ^ ticks ^ 8) & 15) != 0 (xor eax, -8; test al, 15 at 0x005686D3); the frame offset of the moving state is stored in each of its three arms; the building's half size is a signed division.

Behaviour against the draft. Two places differ, both listed above: the owner passed to createTribe, and the delay of exactly 70 in the moving state (the draft stored speed -1 and cleared field_0x30_animRelated; the original stores speed 0 and leaves it at 21). A third differs only in theory: the half size is a signed division, as in the original (cdq; sub; sar at 0x00568CC0), where the draft's is unsigned; the two differ only for a width of 2^31 or more. Everything else was read side by side, state by state, and behaves the same, the addUnitToTribe order included. The draft's counter (and ownLossesArray in efbb0df) are today's availableCows and hitsDealtToPlayerArray (the same offsets, 0x2278 and 0xA8).

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

  • UpdateCatapult.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 0x00568884 the original pushes the slot from edx (0x00568892), which it loaded in front of that call, and calls clearOrDeselectUnitFromSelection without loading ecx again (0x00568894). Behind isAIPlayer (0x00569025, 0x005690EB) it pushes the slot from ebx for the next call without reading it again; behind TryAcquireAmmunitionOrPlanToBuyStone, which it cannot see, it reads it again (0x00569039, 0x005690FB). quick_diff: 0.9456 without deselectUnit's body, 0.9775 without isAIPlayer's, 0.9970 with both.
  • hasUnitReachedDestination, the third callee the unit has to see, is on main's list already. processUnitLossStatistic makes no difference to it (0.9970 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 gets one line: the isAIPlayer edge for UpdateCatapult, next to the two edges it has.

Kept as they are

Six 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 0x00568372, in front of the state tests, as in One more Map::Units function at 100%: UpdateMangonel #445 and One more Map::Units function at 100%: UpdateTrebuchet #453. In its block: quick_diff 0.9950 against 0.9970.
  • The three tests that leave the check loop are three statements (above). As one || chain: 0.9820 against 0.9980.
  • 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 (0x00568862). With the exit test in the loop header: 0.9880 against 0.9970.
  • The shot in the cow arm is written once, behind a flag: the original has one shootProjectile call (0x0056912C) for that arm's paths.
  • searched is declared in front of the search for engineers, because createTribe reads units[searched] after it.
  • The differences in the distance test are written out (above).

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. A conditional expression gives the same code, but the style lint flags it. The larger of the two is max(dx, dy) (windef.h's macro, as main uses it).
  • The path-cost layer is read by its name, DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[player][tile] (0x01EE2998), as main reads it in findBestAttackTileByPathCost; the draft read the address.
  • The 7 casts: (char) on the four frame-table reads (read with movsx), on field_0x3c5 copied into targetingType (movsx dx, al at 0x00568441), 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: 4 for the dx / dy declarations above, 2 for searched and the endless loop's header, 1 for the endless loop, 1 for the manifest line (a file outside src/OpenSHC), 36 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 and One more Map::Units function at 100%: UpdateTrebuchet #453 (-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.
  • Unit::field_0xd2: counted against 5 next to friendlyFireCounterUnk; an AI catapult above either limit moves somewhere else (state 9).
  • PathFindingState::field_0x9c: read after each pathFindingRelated call; while it is 2 or less the next search runs, with 5, 10, 20, 60 and 120 as the first argument.
  • Frames_Shared_UnitClimbingUp.field_0x1784, .field_0x17ac (loading) and .field_0x17d4, .field_0x17fc (firing): the catapult's frame tables into animationFrame and imageIDUnk, 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, US_STATE_9Unk (9) an AI catapult moving towards the enemy.
  • Two status entries are not functions: 0x00568730 is the head of the loop that searches for engineers (0x00568728 jumps to it), and 0x00569300 the head of the loop over a destroyed catapult's crew (0x005692F3 jumps to it).

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

Units::UpdateCatapult, from gynt's draft efbb0df (44.97%). reccmp reads 100.0, raw and normalized, in a narrower
build of main 812d149 with this commit: the files of main's /GL list, the callees the visibility manifest names,
activateTraderState, 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
deselectUnit (0x00568884) the original pushes the slot from edx and calls clearOrDeselectUnitFromSelection without
loading ecx again, and behind isAIPlayer (0x00569025, 0x005690EB) it pushes the slot from ebx without reading it
again. cmake/function-visibility.txt gets the isAIPlayer edge for the unit.

What the code takes from the original's listing:

- createTribe gets the owner of units[searched], with searched the counter the search for engineers left behind
  (0x005688CB); the draft passed the catapult's new owner.
- addUnitToTribe gets the new tribe first and the catapult second, as in the original (0x005688EB/EC),
  although it takes (unitID, tribeID): most likely a second bug of the original, marked fixme like the first.
- The moving state stores speed -1 only for a delay above 70 (jle at 0x005693A9); the draft had it from 70 on.
- The loop that checks whether the AI's two engineers are back leaves by three separate tests. The original keeps
  a zero in edx across that loop (0x00568554, made again at 0x00568564 and 0x00568631); as one || chain our
  compiler made the zero only behind the loop.
- The block that sends the two engineers to work is one loop: the original computes the unit's offset anew at its
  start (0x0056863E), 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.

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