Repository navigation
Three more Map::Units functions at 100%: UpdateSwordsman, UpdateArabianSwordsman and UpdateHorseArcher - #449
Open
kotijekriv wants to merge 3 commits into
Open
kotijekriv wants to merge 3 commits into
kotijekriv wants to merge 3 commits into
Conversation
Units::UpdateSwordsman, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (b9c82dc at 51.51%, after 7e5729a at 44.04%), through versions on our dev branch from 81.0% to 97.1%. The forms that matter, with quick_diff where one was measured against its alternative (the /GL callees in the unit): - the unit is read through the slot global, with no local copy of the unit ID; the attacked unit's ID is read once into a local in front of playHurtSFXForUnit, and its type once into an int behind it, as in UpdateKnight; - `building` is one of the function's locals and starts at 0. The original keeps 4 bytes of frame for it that it never touches (push ecx, a pop ecx at each exit) and takes its value from the zero register for a wall. Zeroed in the wall arm instead there is no frame (822 instructions against 845); - the fade countdown is decremented as a signed char (`--(char&)...`), the form in which the compiler keeps -1 in a register across it as the original does; - the wall attack gives up in three places. Where the building is destroyed and where the AI gives up on a gate or tower the code only sets the state and falls to the case's end, and the compiler gives each a copy of that end with the unit's offset computed twice from one load. The AI's give-up on a wall is the then-arm of its test with the return in the else-arm: the compiler jumps from it into the first place's block, as the original does (0x00562224 to 0x0056216F). With the first two places written out (`state; animationCycleNumber = 0; return;`) the block has the offset once, 9 bytes short (0.9941 against 0.9976); with both only setting the state the block is laid out at the second place (0.9816). Numbers and pointer arithmetic that have names in main's headers are written with those names, as in UpdateKnight. State 105 has no name in UnitState yet and keeps its cast. compiler-flags-gl.txt gets UpdateSwordsman.cpp. No other line is needed: of its callees, hasUnitReachedDestination and resumeMovementIfNoAttackTarget are on main's list already, and the function reads 100.0% with the list as it is. reccmp in a build of main's whole /GL list (the 133 files that exist) and the three units of this branch, with every resolver of those files on: UpdateSwordsman 100.0% raw and normalised, no differing line. Each of the 143 functions of main's list reads the same as in the same build of main (3458fea) without the three units. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateArabianSwordsman, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (5ae2ff3 at 54.61%, after dc09474 at 53.81% and 4812579 at 43.50%), through versions on our dev branch from 82.9% to 97.0%. It is UpdateSwordsman's twin and takes the same forms: - the unit is read through the slot global, with no local copy of the unit ID; the attacked unit's ID is read once into a local in front of playHurtSFXForUnit, and its type once into an int behind it; - `building` is one of the function's locals and starts at 0, which gives the original's 4 bytes of frame that are never touched and the zero register for a wall (zeroed in the wall arm: 772 instructions against 795); - the fade countdown is decremented as a signed char (`--(char&)...`); - of the wall attack's three give-up places the first and the third only set the state and fall to the case's end, and the second, the AI's give-up on a wall, is the then-arm of its test with the return in the else-arm. The compiler keeps a block with its own copy of the case's end at the first place and jumps into it from the second, as the original does (quick_diff with the /GL callees in the unit 0.9937 -> 0.9975, the 9 bytes). Numbers and pointer arithmetic that have names in main's headers are written with those names, as in UpdateKnight. State 105 has no name in UnitState yet and keeps its cast. compiler-flags-gl.txt gets UpdateArabianSwordsman.cpp. No other line is needed: of its callees, hasUnitReachedDestination and resumeMovementIfNoAttackTarget are on main's list already, and the function reads 100.0% with the list as it is. reccmp in a build of main's whole /GL list (the 133 files that exist) and the three units of this branch, with every resolver of those files on: UpdateArabianSwordsman 100.0% raw and normalised, no differing line. Each of the 143 functions of main's list reads the same as in the same build of main (3458fea) without the three units. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateHorseArcher, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (a7ad6f9 at 44.66%, after ccbca4c at 44.56% and f567a48 at 40.84%), through versions on our dev branch from 85.1% to 99.9%. The forms that matter: - the melee and wall-attack states have UpdateKnight's shapes: the unit is read through the slot global with no local copy of the unit ID, the clash sound reads the attacked unit's type once into an int, and the place where the building is destroyed only sets the state and falls to the case's end; - `building` is one of the function's locals and starts at 0, which gives the original's 4 bytes of frame that are never touched and the zero register for a wall (zeroed in the wall arm: 830 instructions against 851); - the horse's place in the stables is freed through a pointer to the building, the count of animals decremented first: `Building* const stables = &buildings[...]; if (stables->numberOfAnimals > 0) { stables->numberOfAnimals--; stables->randomOutpostField--; }`. The original decrements the count in the register it loaded it into for the test, decrements randomOutpostField in memory and stores the count last (`sub cl, 1; add byte [...], 0xff; mov [...], cl` at the function's end). Of twenty forms of the block only this one, and the same with a reference, gives that order; with the building indexed in each access the store never sinks behind the second decrement. Main has the same kind of local in UpdateGoodBuildingDancingBear. Numbers and pointer arithmetic that have names in main's headers are written with those names, as in UpdateKnight. State 105 has no name in UnitState yet and keeps its cast. compiler-flags-gl.txt gets UpdateHorseArcher.cpp. No other line is needed: of its callees, hasUnitReachedDestination and resumeMovementIfNoAttackTarget are on main's list already, and the function reads 100.0% with the list as it is. reccmp in a build of main's whole /GL list (the 133 files that exist) and the three units of this branch, with every resolver of those files on: UpdateHorseArcher 100.0% raw and normalised, no differing line. Each of the 143 functions of main's list reads the same as in the same build of main (3458fea) without the three units. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <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.
Three more functions at 100%, with no header changes. All three are Map::Units functions from gynt's units branch
reimpl/map-units-updatefunctions(his draft #204), and all three commits credit him as co-author. They are the melee units next toUpdateKnight(#444) and share its wall attack.Units::UpdateSwordsman/GLUnits::UpdateArabianSwordsman/GLUnits::UpdateHorseArcher/GLMeasured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie.MapEditorLandscaping_GeneralButtonshandler) left out and its resolver off, as in main's baseline: 0 failed of 2,426 compile steps, 99.57 over 2,442 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files undertools/).UpdateSwordsman,UpdateArabianSwordsmanandUpdateHorseArcherread 100.0 raw and normalised there too, with no diff entry. Two functions on main read lower, and it is how reccmp prints a constant, not their code:Menu::handleMenuItems(0x004F6280) reads 97.9 (91.4 normalised) andMenu::Constructor_Menu(0x004F4100) reads 98.4 (95.2), both 100.0 before. Their only differing lines are five compares with the constant 0x11000000, which reccmp prints on our side as the name of a symbol of our DLL that lies at exactly that address in this build's layout; UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435 and UI: the credits scroll's fade and the win/defeat window at 100% #437 describe the same two readings. Unrounded the average is 99.5749 against 99.5759 for main; with those two at 100.0 it would be 99.5765. Every other main function reads exactly what it read before./GLlist that exist on main, and the three files: 146 functions, average 99.57. Each of the three reads 100.0% raw and normalised, with no diff entries. The same build without the three files has 143 functions, and each of them reads the same in both.openshc-pr-fixfinds nothing to change, andopenshc-pr-checkgives 0 ERROR and 84 CHECK (33, 24 and 27 on the three commits; explained below).What got them to 100%
Every number here is with the file on the
/GLlist (below). On our dev branch (not public) the three went from 81.0%, 82.9% and 85.1% to 97.1%, 97.0% and 99.9%, mostly with the formsUpdateKnighthas: the unit read throughDAT_CurrentUnitSlotID::instancewith no local copy of the slot, the clash sound reading the attacked unit's type once into anint, andbuildingas one of the function's locals that starts at 0. That last one gives the original's 4 bytes of frame that are never touched and its zero register for a wall. In these three an initialiser on the declaration does it (int building = 0;); zeroed in the wall arm instead there is no frame (822 instructions against 845 inUpdateSwordsman).In the two swordsmen the fade countdown is also decremented as a signed char (
--(char&)…disappearFadeAlphaCountdown): in that form the compiler keeps -1 in a register across the decrement, as the original does.Two things were left, and they are what this PR is about.
The wall attack gives up in three places, and the second must be a then-arm (
UpdateSwordsman,UpdateArabianSwordsman). The first place is where the building is destroyed andmoveUnitToBehaviorTargetfails, the second where an AI player gives up on a wall, the third where it gives up on a gate or tower. In the original the first and the third are each a state store with a copy of the case's end behind it (animationCycleNumber = 0, the pops,ret), the unit's offset computed twice from one load, and the second is ajmpinto the first one's block (0x00562224 to 0x0056216F inUpdateSwordsman). The forms of the first two places, in quick_diff with the/GLcallees in the unit, which lays these blocks out as the real build does:state = …; animationCycleNumber = 0; return;if (setDestination…) return;state = …; animationCycleNumber = 0; return;if (!setDestination…) { state = …; } else { return; }(this PR)if (setDestination…) { return; } else { state = …; }The last 0.0024 are two spellings of the same bytes (
0xffffagainst-0x1). AnanimationCycleNumber = 0; break;of its own in the destroyed branch or in the wall arm does not give it either (0.9775, 0.9912).The horse's place in the stables is freed through a pointer to the building (
UpdateHorseArcher, its last two lines). The original loadsnumberOfAnimalsfor the test, decrements it in the register, decrementsrandomOutpostFieldin memory and stores the count last:mov cl, [animals]; test cl, cl; jle; sub cl, 1; add byte [outposts], 0xff; mov [animals], cl. With the building indexed in each access the store never ends up behind the second decrement, whatever the order of the two statements, with or without a local for the count, and with-= 1,--orx = x - 1(18 forms; most givesub; mov; addoradd; sub; mov, none the original's order). With both fields reached through oneBuilding* const stables, the count decremented first, it does; a reference gives the same code. Main has the same kind of local inUpdateGoodBuildingDancingBear.Lines added to
cmake/compiler-flags-gl.txtUpdateSwordsman.cpp,UpdateArabianSwordsman.cppandUpdateHorseArcher.cpp. No callee needs a line:hasUnitReachedDestinationandresumeMovementIfNoAttackTarget, which all three call, are on main's list already, and with main's list as it is the three compile to the same code as with every finished callee of our dev list visible.Kept as they are
int damage;without a value (threedeclared without a valueCHECKs): each arm of the wall test assigns it.(char)casts (18, 16 and 14): 30 frame-table reads intoanimationFrame, and in each function four tests ofdisappearFadeAlphaCountdownand two ofengineerManningSiegeStateRef_checkType. The headers have these asbyte, and the original reads them signed (see the proposal below). The two--(char&)decrements belong with them.(int)(mapTimeInTicks + slot) % 40in each function: the original divides signed (cdq,idivat 0x00561D89, 0x00575DC2 and 0x0057AD72).units[slot].ownerread again in two places thoughowneris a local: the argument ofprocessDamageToBuildingThunkandattacker. The original reads the field again there (movsxat 0x005620EF and 0x005621A4 inUpdateSwordsman, 0x00576131 and 0x005761E6 inUpdateArabianSwordsman, 0x0057B1E8 and 0x0057B29D inUpdateHorseArcher), as inUpdateKnight.(UnitState)105in each function (gathering at the rally point, or at the keep): 105 isUS_RALLYINGin gynt's Reimplement 17 navigation and map-property functions #293, and the casts go when that lands. The states 3 and 108 use main'sUS_STATE_3UnkandUS_UNKNOWN_108.What some placeholders hold
Unit::field_0x2ac: 10 while the unit cheers or stands idle, as inUpdateKnight(the horse archer has no cheering state: only idle).Frames_Shared_UnitClimbingUp:.field_0x1e7cis the wall attack's table and the melee table for substate 0,.field_0x1ea0the melee table for the other substate;.field_0x2058is the cheering table;.field_0x1ec4,.field_0x1f3c,.field_0x1f84and.field_0x1fecare the four idle animations, picked by the slot's low two bits..field_0x9fcis the frame table ofUS_DEATH_03,.field_0x99cthat ofUS_DEATH_02and the three stone deaths..field_0x2fc0holds the horse's frames and.field_0x300cthe rider's, in melee and in the wall attack..field_0x9fcis the table ofUS_DEATH_01.Unit::field_0x430is set to 1 on every update.Unit::field_0x3f8is a flag the idle state sets once, counting the unit as idle at once, when its instruction is to attack a unit, a building or land, or to light pitch.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(char)and(char&)casts above could go.US_RALLYINGin gynt's Reimplement 17 navigation and map-property functions #293: the six casts go when that lands (a second definition here would break the build).Other PRs
UpdateHorseArcherstate 0 asks for a target only below 432 (the original'sjgeat 0x0057ACB7; Reimpl/map units updatefunctions #204 has> 432, so at exactly 432 it callsacquireShootTarget);% 40divides signed (Reimpl/map units updatefunctions #204 divides theuintmapTimeInTicksunsigned); and the swordsmen keep the attacked unit's ID acrossplayHurtSFXForUnit(ediat 0x0056233C), where Reimpl/map units updatefunctions #204 reads it again.cmake/compiler-flags-gl.txt. The conflict is line-level only: whichever lands second needs a trivial merge that keeps the lines of both. Reimpl/TileMapState 100% batch 2 #396 already conflicts with main on its own. Map::Units::UpdateLion at 100% #425 merges cleanly, and cmake: optional wrapper units built from function-visibility.txt #399 doesn't touch the list./GLlist. We have not measured the three with Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 in the tree.🤖 Generated with Claude Code