Repository navigation
One more Map::Units function at 100%: UpdateKnight - #444
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::UpdateKnight, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (974c32f, after bb5f008 at 55.10%), through versions on our dev branch at 84.6% and 96.6%. Kept forms, each with quick_diff of the form without it (the /GL callees in the unit; measured before the file's last pass over its spelling, when the final form read 0.9964, the rest being quick_diff's spelling of three instructions): - the clash sound reads the attacked unit's type once into an int, behind the frame test: the original compares it as an int (as a short 0.8985 against 0.9094 at that step); - every unit access goes through the slot global, and there is no local copy of the unit ID; - `building` is one of the function's locals and is set to 0 by a statement in front of the switch. This is what gives the original's frame, 4 bytes that are reserved (push ecx, a pop ecx at each of the 18 exits) and never touched, and its `mov esi, ebp` for a wall: the variable is live across the calls and gets the slot, and the compiler takes its value from the zero register and drops the store. With `building = 0;` in the wall arm there is no frame (0.9798 against 0.9927); as an initialiser on the declaration `isWall` and `building` come out in each other's registers (0.9806); - the wall attack in the shape of the original: the destroyed and the AI cases are an if / else with the frame reset behind both, the AI's danger and tolerance are read in each test, and the wall case comes first; - in the destroyed case the if behind moveUnitToBehaviorTarget only sets the state and falls through to the case's last statement. The original has that statement's code a second time behind the state store, with the unit's offset computed again; with `animationCycleNumber = 0; return;` repeated in the if the offset is computed once (0.9927 against 0.9964). Numbers and pointer arithmetic that have names in main's headers are written with those names. The two tests in which an AI player gives up on a wall, gate or tower read TileMapState::SEC_PathfindingCostTileMap1105 and attackInfo.playerInfo[attacker - 1], as recomputeAttackAIZone does. The states 3 and 108, the wall bit of the logic layer, the hit sound's material and the knight's body graphic use their enum values. State 105 has no name in UnitState yet and keeps its cast. compiler-flags-gl.txt gets UpdateKnight.cpp. The callee it needs visible is on the list already: hasUnitReachedDestination (quick_diff without it 0.5043, the same as with no callee bodies at all, i.e. off the list). resumeMovementIfNoAttackTarget, which it also calls and which is on the list, makes no difference to it. reccmp in a build of main's whole /GL list (the 133 files that exist) with every resolver of those files on: UpdateKnight 100.0% raw and normalised, no differing line. Each of the other 143 functions in that build reads the same as in the same build of main (3458fea) without it. 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::UpdateKnightis from gynt's units branchreimpl/map-units-updatefunctions(his draft #204), and the commit credits him as co-author.Units::UpdateKnight/GLMeasured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie.tools/).UpdateKnightreads 100.0% raw and normalised there, with no diff entry, and each of main's 2,439 functions reads exactly what it read in main's own build.++,const, truthiness, an if / else for a ternary, two casts dropped and two comments reworded. The function's code is the same before and after: both files compile to the same 826 instructions, line for line. The one thing quick_diff prints differently is the address of the tile-cost table in two operands: 0x01EE2998 as a number before,DAT_TileMapState+ 0x44F790 now, the same address (0.9964 becomes 0.9939 for that spelling). So the full build's result stands for this head./GLlist that exist on main, andUpdateKnight.cpp: 144 functions, average 99.56.UpdateKnightreads 100.0% raw and normalised, with no diff entries. The same build without the file has 143 functions, and each of them reads the same in both./GLlist andUpdateKnight.cpp: 294 functions, average 99.60.UpdateKnightreads 100.0% raw and normalised again, with no diff entries, and each of the 293 other functions reads the same as in that build without the file.openshc-pr-fixfinds nothing to change, andopenshc-pr-checkgives 0 ERROR and 33 CHECK (explained below).What got it to 100%
The numbers in this paragraph are reccmp raw / normalised with the file on the
/GLlist (below). On our dev branch (not public), our first version read 84.6 / 78.2. With the first three points below, and one use of a local copy of the slot still left, it read 96.6 / 86.0. The last two points made it 100.0 / 100.0.Every access reads the unit through
DAT_CurrentUnitSlotID::instance. There is no local copy of the slot any more.The clash sound reads the attacked unit's type once into an
int, behind the frame test. The original compares it as an int (movsx, thencmp ecx, 24and so on), not as a short.The wall attack in the original's shape. The "building destroyed" case and the AI's give-up case are an if / else with the frame reset behind both; the AI's danger and tolerance are read in each of the two tests; the wall case comes first.
buildingis one of the function's locals and is set to 0 by a statement in front of theswitch. The original reserves 4 bytes of frame (push ecx, apop ecxat each of its 18 exits) and never reads or writes them, and for a wall it setsbuildingfrom the zero register (mov esi, ebpat 0x00562FA1). Both come from this one statement: the variable is live across every call, so it gets a home in the frame, and the compiler then takes its value from the zero register where it is read and drops the store. Other places for the same zero, in quick_diff (the/GLcallees in the unit):building = 0;as a statement between the top's firstifand theswitch(this PR)mov esi, ebpint building = 0;on the declaration at the topmov edi, ebp:isWallandbuildingin each other's registersbuilding = 0;in the wall arm, and no local copy of the slotxor esi, esiint building = 0;at the top of the case(0.9927 is with the next point still open.)
Behind a failed
moveUnitToBehaviorTargetthe if only stores the state and falls through to the case's last statement. The original computes the unit's offset twice from one load there (mov edx, [slot]; mov eax, edx; imul eax, eax, 1168; imul edx, edx, 1168at 0x0056305E), once for the state store and once foranimationCycleNumber = 0: the second half is the case's shared end (animationCycleNumber = 0; break;), which the compiler copied behind the if's body. WithanimationCycleNumber = 0; return;repeated inside the if, as before, the offset is computed once (0.9927 against 0.9964).The quick_diff numbers are from before the last pass. Its last 0.0036 were three spellings of the same bytes (
0xffffagainst-0x1twice, the operand order of onetest).Lines added to
cmake/compiler-flags-gl.txtUpdateKnight.cpp. The one callee it has to see is on main's list already:hasUnitReachedDestination. quick_diff reads 0.9964 with it visible and 0.5043 without it, the same as with the file off the list. Its calleeresumeMovementIfNoAttackTargetis on the list too and makes no difference (0.9964 without it).Kept as they are
int building;without a value, and its one-line why in the code (one of the twodeclared without a valueCHECKs): the table above. The other isint damage;, which each arm of the wall test assigns.(char)casts: the eleven frame-table reads intoanimationFrame, four tests ofdisappearFadeAlphaCountdownand two ofengineerManningSiegeStateRef_checkType. The headers have these asbyte, and the original reads them signed (see the proposal below).(int)(mapTimeInTicks + slot) % 40: the original divides signed (cdq,idivat 0x00562C33).(UnitState)105(gathering at the rally point): 105 has no name in main's enum; gynt's Reimplement 17 navigation and map-property functions #293 names itUS_RALLYING. States 3 and 108 use main'sUS_STATE_3UnkandUS_UNKNOWN_108.units[slot].ownerread again for the damage call's argument and forattacker, where a localownerexists: the original reads it again there too (movsxat 0x00562FDE and 0x00563093).What some placeholders hold
The knight is drawn in two parts, the horse through
imageIDUnkand the rider throughgfxNumber, each with its own frame table per state.Frames_Shared_UnitClimbingUp.field_0x2264and.field_0x2290: the horse's frames in melee, for odd and for even slots;.field_0x2290is also the table of the wall attack..field_0x22dc: the rider's frames in melee..field_0x2210and.field_0x223c: horse and rider while cheering (state 3)..field_0x2104and.field_0x2084: the horse standing idle, for odd and for even slots;.field_0x2184: the rider's idle frames.Unit::field_0x2ac: 10 while the knight cheers or stands idle. Nothing more is known from this function.Proposed, not changed here
charfor byte fields read as signed:Unit::disappearFadeAlphaCountdown,Unit::engineerManningSiegeStateRef_checkType(main'sUpdateMerchantstores -2 into it) and the AnimationFrameData frame tables, as on Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416 and Three more Map::Units functions at 100%: UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher #421. Ascharthe seventeen casts above could go.US_RALLYING = 105toUnitState; once it is on main, the two casts can use it.Other PRs
UpdateKnight.cpptoo: an add/add conflict on that file, and the status line at 0x00562AA0 differs (100.0% here against his 55.1%). Once this lands, his branch can take main's version of it.cmake/compiler-flags-gl.txt. The conflict is line-level only: whichever lands second needs a trivial merge that keeps the lines of both. Reimplement 47 UnitsState functions at 100% and restyle 28 more #378, cmake: optional wrapper units built from function-visibility.txt #399 and Map::Units::UpdateLion at 100% #425 merge cleanly.🤖 Generated with Claude Code