Repository navigation
One more Map::Units function at 100%: UpdateCageDog - #438
Open
kotijekriv wants to merge 1 commit into
Open
kotijekriv wants to merge 1 commit into
kotijekriv wants to merge 1 commit into
Conversation
Units::UpdateCageDog, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on the file: 91be34d at 50.04%, 10e5688 at 54.52%, a3e1ab0 at 54.68%, the build fix 6565c1b, which names field133_0x278 predatorNearby, and the style commit 974c32f), through the forms on our development branches, where its history is. What the match needed, against the draft (reccmp raw on the development branch, on the /GL list; the draft as it stood there read 73.7): - Every access reads the unit through DAT_CurrentUnitSlotID::instance, and the draft's local copy `int unitID` is gone: the original keeps the global itself in a register and loads it again only behind a call that may change it, which a local copy never does (73.7 -> 88.4). - The dog is let out when the enemy troop value near the kennel is >= 25 (the cmp at 0x0056FB6C, the jge at 0x0056FB73); the draft had > 25. This one changes what the function does; it was in the 73.7 already. - Four conditions the other way round, in three places of the walking state (US_MOVE_TO_DESTINATION and 120): the frame table of its animation is chosen with field_0x3c2 != 0 first; the animation handles the last frame (animationFrame <= 0) first and has the two gfxNumber stores behind it, again with != 0 first; and the predator test has the predator arm first (88.4 -> 95.3). - In state 207 the enemy's ID is a `short` for the test against 0 and an `int` copy, taken ahead of the test, for the index. The original tests the 16-bit value and sign-extends it ahead of the branch (from 0x0056FD11); with one short the extension came behind the branch (95.3 -> 96.0). - In state 206 the animation block is written out in each arm of the seated test: each arm stores its frame and then holds the whole if / else that adds to gfxNumber. The original loads the facing in each arm of that if / else, into eax in the last-frame arm (0x00570063) and into edx in the other (0x0057007B); eax is the register the frame was in, free again in the last-frame arm. With one if / else behind the join of the two frame arms, the frame is a value with two definitions: the compiler keeps eax for it through the last-frame arm, takes edx for the facing in both arms, and then moves the two identical loads above the branch. Written out per arm, each frame has one definition, the registers are the original's, and the compiler merges the two identical tails itself, so the code has the block once, as the original (the two frame loads at 0x00570049 and 0x00570052, the join at 0x00570059). quick_diff 0.9890 -> 0.9949, 589 instructions against 589. - UnitsState::tryAttackUnitID has to be visible. The original loads the slot into esi in front of the call (0x0056FACC), calls it (0x0056FAD9) and still uses esi behind it (0x0056FAE6), so its compiler knew that the call leaves DAT_CurrentUnitSlotID alone. It is a leaf function, at 100.0% on main and on no /GL list so far; with its body visible ours keeps the slot too (quick_diff 0.9949 -> 1.0000; 96.0 -> 100.0 with both). The states stay blocks that return, as in the draft: this function matches so. The text follows main's unit functions: no compare with TRUE or FALSE (BOOLEnum results by truthiness), main's names US_UNKNOWN_117 and US_UNKNOWN_120, const on the locals that are assigned once, the steps by one as ++ and --, the fade's step as -2 where the draft had 0xfe (main's UpdateMerchant writes the same field so; the dog fades in when it is let out), state 0's enemy passed straight into tryAttackUnitID where the draft had a local that was used once, and a why at the block written out twice. compiler-flags-gl.txt gets UpdateCageDog.cpp and UnitsState/tryAttackUnitID.cpp. Leaving one callee out at a time (quick_diff with the /GL callees in the unit, 1.0000 with all of them), it needs four: checkIfCitizenUnitIsAliveBasedOnState (0.7162 without it), hasUnitReachedDestination (0.4142) and makeUnitStopWalkingByClearingPathProgressState (0.8651), which are on main's list, and tryAttackUnitID (0.9949). Three more on main's list make no difference to it (1.0000 without each): buildingIsAccessible, commitUnitLocation and resumeMovementIfNoAttackTarget. One callee that is on no list must stay off it for this function: with playSFXAtLocation visible it reads 0.9527. Measured with reccmp, both resolver sets on: - a /GL build of this file with its callee closure (15 files): UpdateCageDog 100.0% raw and normalised, no diff entry; tryAttackUnitID reads 100.0% on the list as off it; - a build of parseHlp.cpp and every file of the /GL list that exists, with this commit 136 files: 146 functions, average 99.57, UpdateCageDog 100.0% with no diff entry; the same build without this file and with main's /GL list (tryAttackUnitID built, but not under /GL), which is main (3458fea): 145 functions, average 99.57, and each of them reads the same in both, tryAttackUnitID among them. Five functions read below 100% in both, with the same numbers: updateBuildingPlacementRotationPreview 46.2, playUnitSelectSpeech 94.1, InitBlendFilterArraysUnk 97.9, lightUpBuilding 99.3 and parseHlp 99.7. Those two builds ran on the text one style step before this one (0xfe for -2, and the enemy of state 0 in a local). The instruction stream is the same line for line: on this text the closure build above reads 100.0% with no diff entry, and quick_diff reads 1.0000 with 589 instructions against 589, as on the text before. Not run in game. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This was referenced Nov 21, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One more function at 100%, with no header changes.
Units::UpdateCageDogis from gynt's units branchreimpl/map-units-updatefunctions(his draft #204), and the commit credits him as co-author.Units::UpdateCageDog/GLgynt's numbers are as he recorded them in his commit messages; we did not measure his commits again. His last change to the file is the style commit 974c32f.
The steps in between, on our dev branch (not public), reccmp raw / normalised with the file on the
/GLlist:UpdateCageDog/GLlistshortand aninttryAttackUnitIDvisible)Measured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie.UpdateCageDogreads 100.0% raw and normalised with no diff entry, andtryAttackUnitIDstays at 100.0% under/GL. No function on main reads differently exceptMenu::handleMenuItems(97.9%) andMenu::Constructor_Menu(98.4%), and those two only through a reccmp printing artefact: five compares with the constant 0x11000000, which reccmp prints as a symbol name on our side because one of our DLL's symbols lies at exactly that address in this layout; the instructions and bytes are the same, and the same two show it in UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435, UI: the population report's graph at 100% #436 and UI: the credits scroll's fade and the win/defeat window at 100% #437. 811ccf2fca compiles to the same instructions as this PR's commit: the review changed only how the fade's-2is written and passed one call result straight on (quick_diff 1.0000, 589 = 589, for both)./GLlist that exist on main,parseHlp.cpp(the first entry of our local build list, not on the/GLlist) and the two files this PR puts on the list.UpdateCageDogreads 100.0% raw and normalised, with no diff entries. The average is 99.57 over 146 functions. The same build withoutUpdateCageDog.cppand with main's/GLlist (tryAttackUnitIDbuilt, but not under/GL), which is main (3458fea), gives 99.57 over 145, and each of those 145 functions reads the same in both builds. Both builds ran one style step before the committed text (0xfefor-2, and state 0's enemy in a local); the instruction stream is the same line for line, and the committed text reads 100.0% with no diff entries in a build of the file with its callee closure.updateBuildingPlacementRotationPreview46.2%,playUnitSelectSpeech94.1%,InitBlendFilterArraysUnk97.9%,lightUpBuilding99.3% andparseHlp99.7%. That run does not build every file, and we did not look further into them. The full build above is the one to go by.openshc-pr-fixfinds nothing to change, andopenshc-pr-checkgives 0 ERROR and 37 CHECK (all explained below).What got it to 100%
Apart from the dropped
unitIDcopy, the draft's statements are all still there; what changed is how they are arranged, one compare, and what the compiler can see. The numbers are reccmp raw on our dev branch, on the/GLlist (the table above has the normalised ones)./GLlist, and every access reads the unit throughDAT_CurrentUnitSlotID::instance. The draft took a copy of the slot at the top (int unitID). The original keeps the global itself in a register and loads it again only behind a call that may change it; a local copy is never loaded again (73.7% to 88.4%).>= 25(thecmpat 0x0056FB6C, thejgeat 0x0056FB73). The draft had> 25. This was in the 73.7% already.US_MOVE_TO_DESTINATIONandUS_UNKNOWN_120). The frame table of its animation is chosen withfield_0x3c2 != 0first. The animation handles the last frame (animationFrame <= 0) first and has the twogfxNumberstores behind it, again with!= 0first. The predator test has the predator arm first (88.4% to 95.3%).shortfor the test against 0 and anintcopy, taken ahead of the test, for the index. The original tests the 16-bit value and sign-extends it ahead of the branch (movzx ecx,cmp cx, bx,movsx eax, cx,je, from 0x0056FD11). With oneshortthe extension came behind the branch (95.3% to 96.0%).UpdateLion's chase state (Map::Units::UpdateLion at 100% #425) has the same shape.seatedtest. Each arm stores its frame and then holds the whole if / else that adds togfxNumber; the draft had one if / else behind the two arms.eaxin the last-frame arm (0x00570063) and intoedxin the other (0x0057007B).eaxis the register the frame was in, free again in the last-frame arm.eaxfor it through the last-frame arm, takesedxfor the facing in both arms, and moves the two identical loads above the branch.=for+=, other operand orders, the idle test inside the else arm.UnitsState::tryAttackUnitIDhas to be visible (next section). quick_diff 0.9949 to 1.0000; with both, 96.0% to 100.0%.The states stay blocks that return, as in the draft: this function matches so. quick_diff, with its
/GLcallees in the unit, reads 1.0000: 589 instructions against 589.Lines added to
cmake/compiler-flags-gl.txtUpdateCageDog.cpp.UnitsState/tryAttackUnitID.cpp. The original loads the slot intoesiin front of the call (0x0056FACC), calls it (0x0056FAD9) and still usesesibehind it (0x0056FAE6). So its compiler knew that the call leavesDAT_CurrentUnitSlotIDalone, and ours knows that only when it sees the body.tryAttackUnitIDis a leaf function, at 100.0% on main and not on main's/GLlist; it reads 100.0% on the list as off it.Leaving one callee out at a time (quick_diff, 1.0000 with all of them),
UpdateCageDogneeds four:checkIfCitizenUnitIsAliveBasedOnStatehasUnitReachedDestinationmakeUnitStopWalkingByClearingPathProgressStatetryAttackUnitIDThree more of its callees are on main's list and make no difference to it either way (1.0000 without each):
buildingIsAccessible,commitUnitLocationandresumeMovementIfNoAttackTarget.One callee must stay off the list for this function: with
playSFXAtLocationvisible it reads 0.9527. It is on no/GLlist today. Five more finished callees that are on no list make no difference (1.0000 with each):resetUnitMovementState,setupUnitSharingCurrentTilePosition,commitPendingUnitPosition,resumeMovementAfterInterruptionandchangeDestinationByLeftover.What the two lines do to other functions: nothing that our narrower run can see: with and without them, each of the 145 other functions reads the same,
tryAttackUnitIDamong them (100.0% on the list as off it).Kept as they are (the 37 CHECKs)
(char)casts (17 CHECKs): the eleven frame-table reads intoanimationFrame, the four compares ofdisappearFadeAlphaCountdownand the two ofengineerManningSiegeStateRef_checkType. The headers have these asbyte, and the original reads them signed (see the proposal below).Unit::field_0x3c2three times,MapAndTimeState::field3172_0x27ac, and tenFrames_Shared_UnitClimbingUp.field_0x…tables. What they hold is in the next section.(UnitState)206,207,208,210and211(6 CHECKs, 211 twice): the enum on main has names up to 205 and for 209, and none for these (see the proposal below).Not flagged, but match-driven or worth knowing:
TRUEorFALSE.BOOLEnumresults and theBOOLEnumfieldanimationCycleNumberHasJustIncrementedare tested by truthiness.!= 0,== 0and<= 0stay onshortfields, onDAT_UnitHasBecomeIdle, onpredatorNearbyand onbuildingIsAccessible, which returnsint.-2where the draft had0xfe, as main'sUpdateMerchantwrites the same field. The field is abytein the header and the original reads it signed. The dog fades in when it is let out (disappearFadeAlphaCountdownstarts at 31 and goes down by 2) and fades out when it is back at the kennel (from 0, up by 2).tryAttackUnitID; the draft had a local that was used once. The code is the same either way.// waiting in the kennel until enough enemies are close,// chasing an enemy, ...) are gynt's, from his draft.What some placeholders hold
seatedset, and 208 stops the dog and goes to state 0 oncemovementRelatedis 8 or more.US_MOVE_TO_DESTINATIONandUS_UNKNOWN_120are the walking state.US_MELEE_ATTACKis the bite (FX_WOLF_ATTACK),US_UNKNOWN_117a wait of 51 updates between the death animation andUS_DISAPPEAR, andUS_JESTER_ROAM_TO(109) the fade in or out that ends incachedState, as for the fire watch.updateTickTrackerruns fromcagedogReleaseCheckMoment * 8to 32 above it, so about every 33 updates the dog counts the enemy units around its tile (computeTotalUnitsWithinDistancewith a distance of 16) and is let out whenALGO_TotalTroopValue >= 25orALGO_TotalTroopCount > 5.cagedogReleaseCheckMomentonly shifts where the counter starts.MapAndTimeState::field3172_0x27acis incremented once on every update of a cage dog.Unit::field_0x3c2: in the walking state it picks one of two frame tables and one of twogfxNumberbases (624 or 688); state 206 sets it to 1.Unit::unknownBool01is counted down to 0, one step per update.Unit::workplaceBuildingID_1andworkplaceBuildingUID: the kennel. When itsuidno longer matches, the dog goes toUS_DISAPPEAR.Unit::engineerManningSiegeStateRef_checkType: the signed step of the fade in state 109, as for the fire watch and the miller: -2 fades the dog in, 2 fades it out.Frames_Shared_UnitClimbingUp:field_0x6d98andfield_0x6db8are the two tables of the walking state's animation (byfield_0x3c2),field_0x6d7candfield_0x7134those of state 206 (byseated),field_0x7060,0x7078,0x7098,0x70c0and0x70e0the five of the bite (bysubstate), andfield_0x6c64the second death;ANIM_Worker_Shared1is the first.FX_WOLF_ATTACKandFX_WOLF_DIEare the cage dog's sounds too, so the names are narrower than their use.tryAttackUnitID,resumeMovementIfNoAttackTargetandfindClosestEnemyByAreaAndRangeare what the dog attacks with.Proposed, not changed here
charfor byte fields read as signed:Unit::disappearFadeAlphaCountdown,Unit::engineerManningSiegeStateRef_checkTypeand the AnimationFrameData frame tables, as on Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416, Three more Map::Units functions at 100%: UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher #421 and Two more Map::Units functions at 100%: UpdateHunterDog and UpdateFireWatch #433. Ascharthe(char)casts above could go.UnitState206, 207, 208, 210 and 211 (US_UNKNOWN_206and so on, next toUS_UNKNOWN_205andUS_UNKNOWN_209), through the Ghidra sync. The six casts would then go. We did not add them here: Map::Units::UpdateLion at 100% #425 adds the first three and found that new enumerators inUnitState.hppmove one function on main (see below).Other PRs
UpdateCageDog.cppand one more hunk in the status file, where it conflicts with main already: 0x0056F9B0 (100.0% here against his 54.7%). Once this lands, his branch can take main's version.cmake/compiler-flags-gl.txt, so whichever goes in second gets a small conflict there: keep the lines of both. Apart from Reimpl/map units updatefunctions #204, those four are the only PRs this one gives a new conflict (checked withgit merge-tree). Reimpl/TileMapState 100% batch 2 #396 conflicts with main in that file already, and this PR adds nothing to that.tryAttackUnitID.cppon the/GLlist as well, so with both in, the line is there twice; main's list has such pairs already. On a local merge of main with Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 and Three more Map::Units functions at 100%: UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher #421, with their/GLlines (16 of its 17 callee files are then on the list),UpdateCageDogstill reads 100.0% raw and normalised, with no diff entries.UpdateLion) merges cleanly, and overlaps in two ways.US_UNKNOWN_206,US_UNKNOWN_207andUS_UNKNOWN_208toUnitState.hppand asks whether names or casts are wanted there. This PR follows the answer: if the names land, three of its six casts become names.UpdateLioncallstryAttackUnitIDtoo and is on Map::Units::UpdateLion at 100% #425's/GLlist, so with both PRs in it sees the body. On a local merge of this branch with Map::Units::UpdateLion at 100% #425, a/GLbuild ofUpdateLionwith its callee closure still reads 100.0% raw and normalised, with no diff entries.🤖 Generated with Claude Code