Skip to content

Map::Units::UpdateLion at 100% - #425

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

kotijekriv wants to merge 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-round4-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more function at 100%: Units::UpdateLion (0x0056B0E0), from gynt's units branch reimpl/map-units-updatefunctions (his draft #204). The commit credits him as co-author. As it stands it adds three new UnitState values, so the declaration check will fail until those values are on main (see below). There is also a question below about doing without them.

Version Commit Raw Normalised Where the number comes from
gynt's draft 105767f 52.81% not recorded his commit message
gynt's draft df6b948 57.43% not recorded his commit message
gynt's draft e3fc5d9 59.36% not recorded his commit message
gynt's draft, his last change to the file 974c32f 59.4% not recorded the status line in that commit
An earlier version on our dev branch not public 80.0% 78.2% reccmp, off the /GL list
A later version on our dev branch not public 87.9% 82.5% reccmp, on the /GL list
This PR 94377f0 100.0% 100.0% reccmp, on the /GL list

gynt's numbers are as he recorded them; we did not measure his commits again. The 100.0% was measured on a commit with the same tree as 94377f0 (tree 100067d7b); only the commit message differs.

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 branch on main 3458fea, both resolver sets on: 0 failed, 99.58 over 2,440 functions. UpdateLion reads 100.0 raw and normalised. One function on main moves, see below; every other one reads the same.
  • Our own narrower run: the 133 files of main's /GL list that exist on main, plus UpdateLion.cpp. UpdateLion reads 100.0% raw and normalised, with no diff entries. The average is 99.56 over 144 functions. The same list on main (3458fea) gives 99.56 over 143, and every function in both builds reads the same.
  • Five functions read below 100% in both of those builds, with the same numbers, so this PR does not cause them: updateBuildingPlacementRotationPreview 46.2%, playUnitSelectSpeech 94.1%, InitBlendFilterArraysUnk 97.9%, lightUpBuilding 99.3% and UpdateFireBallistaBuilding 99.6%. That run does not build every file, and we did not look further into them.
  • 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 19 CHECK (all explained below).
  • Not run in game.

What got it to 100%

Which statements read the unit through DAT_CurrentUnitSlotID::instance and which through the copy taken at the top was settled by measuring. That and the /GL line took the earlier version from 80.0% to 87.9%. Four shapes did the rest. Each quick_diff number below is the final file with that one shape undone; the final file reads 0.9939, and reccmp reads 100.0%. The 0.9939 is quick_diff's view of the two timeGetTime calls.

  • First state, the slot is read again. The original loads the slot behind setUnitFacingDirectionTowardsTarget (0x0056B185) and again behind findClosestAnimalStoreResult (0x0056B21C). The code has a local slot for this, read again behind each of the three searches. The compiler merges the three calls into the one the original has. Without those three reads: 0.9479, 488 instructions against 491. Through the global throughout: 0.9817.
  • State 205, the frame is a local. Each arm sets frame, which is stored once and then tested, as at 0x0056B631. Through the field: 0.9837.
  • Chase state, the target is a short with an int copy for the index. The original tests the 16-bit value, then sign-extends it ahead of the branch (the block at 0x0056B2BB). One int: 0.9888, 490 instructions.
  • Chase state, destinationNeeded is stored through the slot. This keeps the store ahead of the target's read, as at 0x0056B2BB. Through unitID: 0.9919.

Header change: three new UnitState values

Commit a6603f8 adds US_UNKNOWN_206, US_UNKNOWN_207 and US_UNKNOWN_208 to src/OpenSHC/Map/Units/States/UnitState.hpp, next to the existing US_UNKNOWN_205 and US_UNKNOWN_209. No existing value changes.

Value Address in UpdateLion Bytes Instruction What the state does there
US_UNKNOWN_208 0x0056B24A 66 81 FA D0 00 cmp dx, 0xD0 Stops walking, then goes back to state 0 once movementRelated reaches 8
US_UNKNOWN_207 0x0056B28A 66 81 FA CF 00 cmp dx, 0xCF Chases the prey
US_UNKNOWN_206 0x0056B68F 66 81 FA CE 00 cmp dx, 0xCE Goes straight back to state 0

The declaration check will fail on this PR until main has these values. check-declaration-changes.yml fails any PR that changes a .h or .hpp file under src/OpenSHC, labels it declaration/type change, and the merge queue rejects it too. The header is generated, so the values need to come in through main. Once they are there we will merge main in and this commit drops out.

Question for the maintainers: the three enumerators now, or casts until the Ghidra sync?

The three enumerators move one function on main through the TU symbol count (the effect found on #366). In the full build, GameStateStructures::migrateGameStateForMapVersion (0x0045C1C0) reads 99.8 / 95.5 in place of 100.0 / 100.0. Its source does not change and it is not on the /GL list. One load, mov si, word ptr [GameStateStructures + 337108] (source line 135), sits at a different place.

To check this, we compiled that one file on this branch and compared the instruction streams:

UnitState.hpp Instruction hash quick_diff
this branch's, with US_UNKNOWN_206 to 208 e37e9e77f033 0.9903
main's 3e9a9d650a3d 0.9925
main's, plus three typedef short lines after the first include e37e9e77f033 0.9903
the same, after the UnitsState.func.hpp include e37e9e77f033 0.9903
the same, after the last include e37e9e77f033 0.9903

So three extra symbols anywhere after the includes give exactly this branch's code. The move depends only on how many symbols there are, not on what they are called. The same function moved to 99.8 with the two Address enumerators on #420, and it will move the same way when these three values reach main through a Ghidra sync.

The alternative, measured: UpdateLion compares with (OpenSHC::Map::Units::States::UnitState)208, 207 and 206, on main's headers, with no header commit.

  • quick_diff gives exactly the same instruction stream as this branch: 0.9939, 491 instructions.
  • In a real build on main 3458fea (the 133 files of main's /GL list that exist on main, plus UpdateLion.cpp and migrateGameStateForMapVersion.cpp, both resolver sets on), UpdateLion reads 100.0 raw and normalised, and so does migrateGameStateForMapVersion.
  • The declaration check would then pass. The checker gives 21 CHECK in place of 19: the header CHECK goes, and three number-cast CHECKs come.

Which do you prefer? One option is the three enumerators now, with migrateGameStateForMapVersion at 99.8 until it is matched again. The other is the three casts until a Ghidra sync brings the values, which leaves every function on main as it is. Switching this PR to the casts is one commit.

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

  • UpdateLion.cpp. quick_diff reads 0.9939 with its callees visible and 0.8396 with none of them, which is the same as being off the list.
  • It needs two callees visible, and both are on main's list already: checkIfCitizenUnitIsAliveBasedOnState (0.8409 without it) and hasUnitReachedDestination (0.9591 without it).
  • makeUnitStopWalkingByClearingPathProgressState and resumeMovementIfNoAttackTarget are on the list as well and make no difference here (0.9939 without either).

The 19 CHECKs

Count Check Lines in UpdateLion.cpp Why it stays
1 Header change none (branch-wide) The section above: its own commit, with the evidence
9 (char) cast 31, 97, 198, 249, 277, 280, 313, 337, 368 The headers have these fields as byte, and the original reads them signed. For disappearFadeAlphaCountdown it compares with a signed jump: cmp al, bl; jle at 0x0056B113 (line 31) and cmp al, 0x20; jle at 0x0056B821 (line 368), each after a mov al of the byte. Each frame-table read is a movsx: 0x0056B89A (line 97), 0x0056B413 (198), 0x0056B579 (249), 0x0056B61B (277), 0x0056B62A (280), 0x0056B6C1 (313) and 0x0056B762 (337)
8 Placeholder name 29, 98, 199, 247, 250, 274, 278, 281 No better name yet; what this function shows is listed below
1 Declared without a value 275 (int frame;) Each arm sets it, and the shape is needed for the match. As a ternary: 0.9888, 492 instructions. With one table as the start value: 0.9837

What some placeholders hold

These are only what UpdateLion shows.

  • MapAndTimeState::field3170_0x27a4: incremented once at the top of every UpdateLion.
  • Unit::field_0x3c2: cleared in states 204 and 205.
  • AnimationFrameData.field_0x6d14: the frames for substate 100 of the moving state.
  • AnimationFrameData.field_0x703c: the melee attack frames.
  • AnimationFrameData.field_0x6ec4: the frames of state 204.
  • AnimationFrameData.field_0x6ca4 and .field_0x6cec: the frames of state 205, when seated is 0 and when it is not.

Proposed, not changed here

Other PRs

Checked with trial merges against each PR's head on 2026-11-19.

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 19, 2026 03:56
…_208

Units::UpdateLion (0x0056B0E0) tests its state against these numbers: cmp dx, 208 at 0x0056B24A, 207 at
0x0056B28A and 206 at 0x0056B68F. The values already in the enum do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateLion, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (974c32f, after e3fc5d9
at 59.36%), through an earlier version on our dev branch at 87.9%. Kept forms, each with quick_diff of the form
without it (the /GL callees in the unit; the final form reads 0.9939, all of it quick_diff's view of the two
timeGetTime calls):
- the first state reads the slot into a local behind setUnitFacingDirectionTowardsTarget and again behind each
  findClosestAnimalStoreResult (without those three reads 0.9479, through the global throughout 0.9817);
- state 205 sets a frame local in each arm, stores it once and tests the local (through the field 0.9837);
- the chase state keeps the target in a short with an int copy for the index (one int 0.9888) and stores
  destinationNeeded through the slot ahead of that read (through unitID 0.9919).

compiler-flags-gl.txt gets UpdateLion.cpp. The callees it needs visible are on the list already:
checkIfCitizenUnitIsAliveBasedOnState (quick_diff without it 0.8409) and hasUnitReachedDestination (0.9591).
With no callee bodies at all, i.e. off the list, 0.8396.

reccmp in a build of main's whole /GL list with every resolver of those files on: UpdateLion 100.0%. Every other
function in that build reads the same as in the same build of main (3458fea).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tryAttackUnitID returns BOOLEnum, and the lion's test was `== 0`; TheRedDaemon's review on sourcehold#300 asks for a BOOLEnum
result to be tested by truthiness, so it is `!tryAttackUnitID(...)` now. No other change.

Measured: with the precompiled header, quick_diff gives the same listing for the pushed head's text and this one. A
narrow real build (main's /GL list and this round's files of several of our PRs, with the struct resolvers and
those files' function resolvers on, sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie)
reads 100.0% raw and normalised with no differing line for each function this commit touches, and every other
function reads as in the same build without them.

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

declaration/type change This requires changes to the generated/exported types or definitions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant