Skip to content

Three more Map::Units functions at 100%: UpdateSwordsman, UpdateArabianSwordsman and UpdateHorseArcher - #449

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

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

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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 to UpdateKnight (#444) and share its wall attack.

Function Address Before After
Units::UpdateSwordsman 0x00561C10 gynt's draft 51.51% (b9c82dc; 44.04% at 7e5729a) 100.0%, under /GL
Units::UpdateArabianSwordsman 0x00575C40 gynt's draft 54.61% (5ae2ff3; 53.81% at dc09474) 100.0%, under /GL
Units::UpdateHorseArcher 0x0057AB50 gynt's draft 44.66% (a7ad6f9; 44.56% at ccbca4c) 100.0%, under /GL

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 the previous cut of this branch on main 3458fea (the same code: the last cut only adds comments and drops one unused include), with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) 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 under tools/). UpdateSwordsman, UpdateArabianSwordsman and UpdateHorseArcher read 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) and Menu::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.
  • Our own narrower run: the 133 files of main's /GL list 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.
  • 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 84 CHECK (33, 24 and 27 on the three commits; explained below).
  • Not run in game.

What got them to 100%

Every number here is with the file on the /GL list (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 forms UpdateKnight has: the unit read through DAT_CurrentUnitSlotID::instance with no local copy of the slot, the clash sound reading the attacked unit's type once into an int, and building as 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 in UpdateSwordsman).

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 and moveUnitToBehaviorTarget fails, 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 a jmp into the first one's block (0x00562224 to 0x0056216F in UpdateSwordsman). The forms of the first two places, in quick_diff with the /GL callees in the unit, which lays these blocks out as the real build does:

    First place Second place Result quick_diff
    state = …; animationCycleNumber = 0; return; the same one block at the first place with the offset computed once, 9 bytes short; the second place jumps into it 0.9941
    only sets the state only sets the state, behind if (setDestination…) return; the compiler merges the two state stores early and keeps the block at the second place; the case's end moves up behind the first test 0.9816 (93.4% in a real build)
    only sets the state state = …; animationCycleNumber = 0; return; the first place is the original's, the second a full copy of its own 0.9912
    only sets the state if (!setDestination…) { state = …; } else { return; } (this PR) each place keeps its block until it has its copy of the end; the two are merged late and the first one stays 0.9976
    only sets the state if (setDestination…) { return; } else { state = …; } the early merge again 0.9816

    The last 0.0024 are two spellings of the same bytes (0xffff against -0x1). An animationCycleNumber = 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 loads numberOfAnimals for the test, decrements it in the register, decrements randomOutpostField in 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, -- or x = x - 1 (18 forms; most give sub; mov; add or add; sub; mov, none the original's order). With both fields reached through one Building* const stables, the count decremented first, it does; a reference gives the same code. Main has the same kind of local in UpdateGoodBuildingDancingBear.

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

  • UpdateSwordsman.cpp, UpdateArabianSwordsman.cpp and UpdateHorseArcher.cpp. No callee needs a line: hasUnitReachedDestination and resumeMovementIfNoAttackTarget, 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.
  • What the lines do to other functions: nothing that our narrower run can see (above).

Kept as they are

  • int damage; without a value (three declared without a value CHECKs): each arm of the wall test assigns it.
  • 48 (char) casts (18, 16 and 14): 30 frame-table reads into animationFrame, and in each function four tests of disappearFadeAlphaCountdown and two of engineerManningSiegeStateRef_checkType. The headers have these as byte, and the original reads them signed (see the proposal below). The two --(char&) decrements belong with them.
  • (int)(mapTimeInTicks + slot) % 40 in each function: the original divides signed (cdq, idiv at 0x00561D89, 0x00575DC2 and 0x0057AD72).
  • units[slot].owner read again in two places though owner is a local: the argument of processDamageToBuildingThunk and attacker. The original reads the field again there (movsx at 0x005620EF and 0x005621A4 in UpdateSwordsman, 0x00576131 and 0x005761E6 in UpdateArabianSwordsman, 0x0057B1E8 and 0x0057B29D in UpdateHorseArcher), as in UpdateKnight.
  • Two (UnitState)105 in each function (gathering at the rally point, or at the keep): 105 is US_RALLYING in gynt's Reimplement 17 navigation and map-property functions #293, and the casts go when that lands. The states 3 and 108 use main's US_STATE_3Unk and US_UNKNOWN_108.
  • Placeholder names (24 CHECKs: 11, 4 and 9): below.

What some placeholders hold

  • Unit::field_0x2ac: 10 while the unit cheers or stands idle, as in UpdateKnight (the horse archer has no cheering state: only idle).
  • Swordsman, frame tables in Frames_Shared_UnitClimbingUp: .field_0x1e7c is the wall attack's table and the melee table for substate 0, .field_0x1ea0 the melee table for the other substate; .field_0x2058 is the cheering table; .field_0x1ec4, .field_0x1f3c, .field_0x1f84 and .field_0x1fec are the four idle animations, picked by the slot's low two bits.
  • Arabian swordsman: .field_0x9fc is the frame table of US_DEATH_03, .field_0x99c that of US_DEATH_02 and the three stone deaths.
  • Horse archer: the unit is drawn in two parts, as the knight is; .field_0x2fc0 holds the horse's frames and .field_0x300c the rider's, in melee and in the wall attack. .field_0x9fc is the table of US_DEATH_01. Unit::field_0x430 is set to 1 on every update. Unit::field_0x3f8 is 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

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 3 commits November 21, 2026 13:41
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 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant