Repository navigation
Reimplement 4 Map::Units functions and RenderAnimatedTutorialFloatOverlay, with UnitState names (100%) - #409
Open
kotijekriv wants to merge 7 commits into
Open
kotijekriv wants to merge 7 commits into
kotijekriv wants to merge 7 commits into
Conversation
Units::UpdateDeer (0x00541DE0) and Units::UpdateRabbit (0x00542630) dispatch on the unit's state through jump tables that run to 209 (cmp ecx, 0xd1 at 0x00541E5F with tables at 0x00542558 and 0x00542528; cmp eax, 0xd1 at 0x0054276E with tables at 0x00542C34 and 0x00542C0C). Deer's table has its own entries for 117, 202 to 205 and 209, Rabbit's for 117, 203, 204 and 209 (202 and 205 go to its default). They store these states (mov word ptr [reg + 0x138880C]: the units array at 0x0138854C plus state at 0x2C0): Deer 205, 203 and 204 at 0x00541EBC, 0x00541ED5 and 0x00541EE0 and 117 at 0x0054244B and 0x0054249C, Rabbit 203, 204 and 117 at 0x005427C6, 0x005427D3 and 0x00542B0E, and TribesState::scatterTribeUnitsRandomly stores 209 at 0x00523A0A. The enum goes from US_STONE_DEATH_03 (116) to US_UNKNOWN_120 and ends at US_ASSASSIN_CLIMBING_DOWN (129); the six are named in the US_UNKNOWN_<n> form. Used in place of (UnitState)N and the numeric case labels; code generation is the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::CurrentUnitHasHealer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions, the branch of PR sourcehold#204 (6d8ad88, 89.29%, "loop precheck scheduling"; the file last changed in 974c32f; 92.9 on main's headers in the 43a17df baseline). The current player is read into a const local, currentPlayer, before the loop, as the original loads currentPlayerSlotID before the loop's precheck, the way PlayArmoryIsFullWarning and WarnIfPlayersGranaryIsFull read theirs (read in the condition: quick_diff 0.9259 -> 0.8519). maxUnitCount is an int, so the (int) cast goes, and the loop counts unitID as main's unit loops do. reccmp 100.0%. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateRabbit, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (209f6d4 at 69.32%, 49bd9c9 at 79.00%): - units[] is indexed through unitID or through DAT_CurrentUnitSlotID::instance per statement, as a greedy quick_diff search picked them one statement per step; - the US_UNKNOWN_203 path sits inside the `> 0` test, the slowdown is an int compare with (fixedRng & 0xf), and the death cases test animationFrame <= 0 first, the original's branch directions (structural_diff.py); - tribe is an int local, so its load is movsx as in the original; it is read at the top, as reading it where it is used loses the match (quick_diff 1.0000 -> 0.5216); - when hasUnitReachedDestination returns TRUE in the movement case, animationCycleNumber and substate are written through a local Unit pointer. The scheduler (Schedule, called from dop2_PreEncode) cannot tell those two stores apart from the state.generic store through units[unitID] and gives each a memory edge to it, so they come before pop edi and pop edi before the third store, as in the original; with all three through units[unitID] the stores are independent and pop edi went first. Found with cdb: tuple_dump.py shows the block in source order at dop2_PreEncode entry and reordered when it returns, and a breakpoint on DagMakeDep lists the edges; - states 117, 203, 204 and 209 use the US_UNKNOWN_<n> names of the header commit. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateDeer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (f7d126f at 36.18%, 6458bca at 64.64%, cases in jump-table order): - units[] is indexed through unitID or through DAT_CurrentUnitSlotID::instance per statement, as a greedy quick_diff search picked them one statement per step; in states 204 and 205 the two forms were picked over every combination: 204 tests substate through the slot, as 203 does, so its substate is not kept in a register for the increment and the two states share one idle tail as in the original; - when hasUnitReachedDestination returns TRUE, animationCycleNumber and substate are written through a local Unit pointer, so the two stores stay ahead of the epilogue's pop edi as in the original (the same lever as in UpdateRabbit); - in both seated branches of state 205 the frame-0 path sets DAT_UnitHasBecomeIdle and falls through to the shared idle block instead of repeating it and returning: the original has that block copied into each path; - frameOffset is declared at the top: the original holds its 1 in edx from the prologue, where it also adds it to deerCount (declared in the movement case: quick_diff 0.8300); tribe is read at the top too (just before the switch: 0.9122); - states 117, 202 to 205 and 209 use the US_UNKNOWN_<n> names of the header commit. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateMerchant, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (0adb264 at 53.39%, 8372c08 at 63.80%), under /GL: - every call that takes the unit passes DAT_CurrentUnitSlotID::instance, and units[] is indexed through unitID or through the slot per statement, as a greedy quick_diff search picked them one statement per step; - in state 3 the three stores after the destination test go through the slot, which the original reads again after setDestinationForUnit, and gfxNumber and field_0x30_animRelated are written through a local Unit pointer, so they come after the stateBasedSpeed store as in the original; - states 2 and 3 are US_STATE_2Unk and US_STATE_3Unk, and state 2's fade step is written as -2 (the byte the original stores, 0xFE; same code); - compiler-flags-gl.txt lists UpdateMerchant.cpp: 100% with the line, 78.0% without it. The original keeps values in registers across checkIfCitizenUnitIsAliveBasedOnState and hasUnitReachedDestination, which main's list already has, and reads DAT_CurrentUnitSlotID again after activateTraderState, which stays off the list: with its body visible the slot stays in esi across the call (90.0% instead of 100%). Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UI::Rendering::RenderAnimatedTutorialFloatOverlay, from 0287fe5 on reimpl/batch1-development, in the form TheRedDaemon worked out in his review: x and y step down by 8 and 32 before the per-resolution offset, and by 28 and 69 after the draw buffer is saved, and the offset comes from a chain of plain ifs. The original pushes the trailing 0 before it computes the other arguments, and this form gives that order; one subtraction of 36 and 101 loses the match (quick_diff 0.8035 in the first place, 0.6356 in the second), so the two steps stay, with a comment. The 1680x1050 offset is the original's 4050, where every other offset is the screen height - 600; a fixme says so. The function is Gynt's: 5236e98 (60.79%) and 63cc49f on his fork's reimpl/UI-Rendering-development, and e8e27aa (60.8%) on reimpl/UI-Rendering (his sourcehold#255); ce6bd1e took it to 67.2% on reimpl/batch1-development. Co-authored-by: TheRedDaemon <66257843+TheRedDaemon@users.noreply.github.com> Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main's Ghidra sync 1b36c0a has this PR's six UnitState values, so the PR no longer changes a header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Main is merged in now (e086ba7). Its Ghidra sync 1b36c0a has this PR's six Because of the symbol-count effect gynt found on #366, we measured again after the merge. This was a full build of main e086ba7 with this PR and #410 merged on it, against a full build of main alone.
|
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.
Five more functions at 100%, and one header commit that names six unit states. Four of the functions are Map::Units functions from gynt's units branch
reimpl/map-units-updatefunctions(his draft #204). The fifth is UI::Rendering's tutorial arrow, from our draft #313, in the form TheRedDaemon worked out in his review there. Each commit cites the drafts it builds on and credits gynt as co-author; the arrow's commit credits TheRedDaemon too.Units::CurrentUnitHasHealerUnits::UpdateRabbitUnits::UpdateDeerUnits::UpdateMerchant/GL(see below)UI::Rendering::RenderAnimatedTutorialFloatOverlayMeasured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp (cce67bcab) with--resolve-wrapped-calls --ignore-security-cookie.compiler-flags-gl.txt. All five read 100.0% raw and normalised, and so do UpdateMerchant's nine reimplemented callees. Of the 2,376 functions measured on main, 2,374 read exactly the same with this branch and none read lower. Two read higher,UI::Menu::Constructor_Menu(98.4% to 100.0%) andUI::Menu::handleMenuItems(97.9% to 100.0%). Those are the same two as in Reimplement 5 UI functions from gynt's drafts and #328 (100%) #407's measurement, for the same reason: both compare with the constant0x11000000, a symbol in main's DLL happens to sit at that address, and reccmp names the constant after it until the DLL's layout moves. Neither file changes.openshc-pr-fixfinds nothing to change, andopenshc-pr-checkgives no errors.What got them to 100%
currentPlayerSlotIDbefore the loop's first test, the wayPlayArmoryIsFullWarningandWarnIfPlayersGranaryIsFullread theirs.maxUnitCountis an int, so the draft's(int)cast goes.DAT_CurrentUnitSlotIDagain instead of keepingunitID. So the statements where it does that indexunits[DAT_CurrentUnitSlotID::instance], and the rest keepunitID. They were picked by a quick_diff search, one statement per step. In UpdateMerchant every call also passes the slot.Unit* constlocal. This is the compiler's instruction scheduler, not the order of the statements. In UpdateRabbit and UpdateDeer, whenhasUnitReachedDestinationreturns TRUE,animationCycleNumberandsubstateare written through the pointer. With every store throughunits[unitID], MSVC sees them as independent and puts the epilogue'spop edibefore them (UpdateRabbit read 99.8%). Through a pointer it can't tell them apart from the state store, so they keep their place before it, as in the original. We found this by running the compiler under cdb and listing the scheduler's dependency edges. UpdateMerchant's state 3 uses the same lever to keepgfxNumberandfield_0x30_animRelatedafter thestateBasedSpeedstore. Each pointer has a one-line comment saying why.> 0test, the slowdown is an int compare withfixedRng & 0xf, and the death cases testanimationFrame <= 0first. These are the original's branch directions.tribeis an int local read at the top, so its load is amovsxas in the original.substatethrough the slot, as state 203 does, so its substate isn't kept in a register for the increment, and the two states share one idle tail as in the original. In both seated branches of state 205, the frame-0 path setsDAT_UnitHasBecomeIdleand falls into the shared idle block; the original has that block copied into each path.frameOffsetis declared at the top, because the original holds its 1 inedxfrom the start.setDestinationForUnit. State 2's fade step is written as -2, the byte the original stores (0xFE). The rest is the/GLlist change below.Header change (a commit of its own)
UnitStategetsUS_UNKNOWN_117,US_UNKNOWN_202,US_UNKNOWN_203,US_UNKNOWN_204,US_UNKNOWN_205andUS_UNKNOWN_209(7724c54), in theUS_UNKNOWN_<n>form, in place of(UnitState)Nand numeric case labels in UpdateRabbit and UpdateDeer. Code generation is the same.cmp ecx, 0xd1, tables at 0x00542558 and 0x00542528). UpdateRabbit's has its own entries for 117, 203, 204 and 209, and sends 202 and 205 to its default (0x0054276E,cmp eax, 0xd1, tables at 0x00542C34 and 0x00542C0C).mov word ptr [reg + 0x138880C]: the units array at 0x0138854C plusstateat 0x2C0): UpdateDeer stores 205, 203 and 204 at 0x00541EBC, 0x00541ED5 and 0x00541EE0, and 117 at 0x0054244B and 0x0054249C. UpdateRabbit stores 203, 204 and 117 at 0x005427C6, 0x005427D3 and 0x00542B0E.scatterTribeUnitsRandomlystores 209 at 0x00523A0A; main'sscatterTribeUnitsRandomly.cppwrites it asstate.generic = 209and can use the name once this is in.US_STONE_DEATH_03(116) toUS_UNKNOWN_120, and ends atUS_ASSASSIN_CLIMBING_DOWN(129).Main's declaration check will fail on this PR and label it until the six values are synced to main. Then we merge main in, and the check passes.
One line added to
cmake/compiler-flags-gl.txtUpdateMerchant.cppgoes on the list. It reads 100.0% with the line and 78.0% without it.The original keeps values in registers across
checkIfCitizenUnitIsAliveBasedOnStateandhasUnitReachedDestination, so UpdateMerchant has to see their bodies. Both are on main's list already. It also readsDAT_CurrentUnitSlotIDagain afteractivateTraderState, so that callee must stay off the list: with its body visible, the slot stays inesiacross the call and UpdateMerchant reads 90.0%. UpdateMerchant isactivateTraderState's only caller.We checked each of these numbers again in small builds of UpdateMerchant and its nine reimplemented callees: 100.0% as the branch has it, 78.0% without the line, and 90.0% with
activateTraderStateon the list too. gynt's #378 puts two more of its callees on the list,clearHiddenFlagAndUpdatePositionandresetUnitMovementState. With those two added, UpdateMerchant still reads 100.0%, and so do they, so the two PRs can go in in either order.Kept as they are, measured with quick_diff
calculatedOwnerPlayerIndexmapped by an else-if chain (9 to 2, 10 and 11 to 3, 12 to 4, 13 to 6, 14 to 8, 15 to 3)triberead at the toptriberead at the topframeOffsetdeclared at the topMost of these have a one-line comment in the code that says why.
One odd number kept: RenderAnimatedTutorialFloatOverlay's offset for 1680x1050 is 4050. Every other resolution gets its height minus 600, so 450 was probably meant, but the original has 4050 (
mov ebx, 0xfd2at 0x004BC862), so it stays, with a comment.What some placeholders hold
INT_00df563c: the time (timeGetTime) when the tutorial arrow's current cycle of 10 frames started.INT_00df5640: bit 0 says that start time has been set.DAT_00df5540: the tutorial step transition (0, 1 or 2;InitTutorialStepTransitionstores 1 or 2). While it is nonzero, the arrow isn't drawn.Proposed, not changed here
(char)casts onUnit::disappearFadeAlphaCountdown(+0x32E),Unit::engineerManningSiegeStateRef_checkType(+0x32F) and fields of the AnimationFrameData tables, as in Reimplement 5 Map::Units update functions from gynt's units branch, under /GL (100%) #393. These fields arebytein the headers, but the original compares them as signed (< 0,> 32). Retyping them tocharin Ghidra would let the casts go here and in the unit functions on main.DAT_UnitHasBecomeIdleisundefined4, but it is only ever set to 1 or tested against 0, so it could beBOOLEnum(as Reimplement 11 Map::Units functions from gynt's units branch (100%) #390 and Reimplement 5 Map::Units update functions from gynt's units branch, under /GL (100%) #393 said too).One function leaves #313
UI::Rendering::RenderAnimatedTutorialFloatOverlay(0x004BC790)#313 gets one commit that removes its
.cppfile and puts its status line back to main's (21b73bc). Main has no file for it, and #313 changed no header for it. #313 still compiles as before without it: its one file that doesn't compile, RenderGreatestLordScreen, failed before this change too.Other PRs
cmake/compiler-flags-gl.txt, so whichever goes in second gets a one-line conflict there: keep both. gynt's Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 also changes that list; it merges cleanly with this branch (see above).🤖 Generated with Claude Code