Skip to content

One more Map::Units function at 100%: UpdateKnight - #444

Open
kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-knight-100
Open

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-knight-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateKnight is from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and the commit credits him as co-author.

Function Address Before After
Units::UpdateKnight 0x00562AA0 gynt's draft 55.10% (bb5f008; 49.02% at 968bbc1) 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, every struct resolver and every function resolver on: 0 failed of 2,424 compile steps, 99.58 (99.5761) over 2,440 functions, against 99.58 (99.5759) over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). UpdateKnight reads 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.
  • That build had the file before its last pass, which changed spelling only: names from main's headers in place of numbers and pointer arithmetic, ++, 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.
  • Our own narrower run, with the final file: the 133 files of main's /GL list that exist on main, and UpdateKnight.cpp: 144 functions, average 99.56. UpdateKnight reads 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.
  • With Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 in the tree: a local merge of main with Reimplement 47 UnitsState functions at 100% and restyle 28 more #378, its own /GL list and UpdateKnight.cpp: 294 functions, average 99.60. UpdateKnight reads 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.
  • 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 33 CHECK (explained below).
  • Not run in game.

What got it to 100%

The numbers in this paragraph are reccmp raw / normalised with the file on the /GL list (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, then cmp ecx, 24 and 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.

  • building is one of the function's locals and is set to 0 by a statement in front of the switch. The original reserves 4 bytes of frame (push ecx, a pop ecx at each of its 18 exits) and never reads or writes them, and for a wall it sets building from the zero register (mov esi, ebp at 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 /GL callees in the unit):

    Form Frame Wall arm quick_diff
    building = 0; as a statement between the top's first if and the switch (this PR) 4 bytes, untouched mov esi, ebp 0.9927
    int building = 0; on the declaration at the top 4 bytes, untouched mov edi, ebp: isWall and building in each other's registers 0.9806
    building = 0; in the wall arm, and no local copy of the slot none xor esi, esi 0.9798
    int building = 0; at the top of the case none nothing 0.9687

    (0.9927 is with the next point still open.)

  • Behind a failed moveUnitToBehaviorTarget the 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, 1168 at 0x0056305E), once for the state store and once for animationCycleNumber = 0: the second half is the case's shared end (animationCycleNumber = 0; break;), which the compiler copied behind the if's body. With animationCycleNumber = 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 (0xffff against -0x1 twice, the operand order of one test).

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

  • UpdateKnight.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 callee resumeMovementIfNoAttackTarget is on the list too and makes no difference (0.9964 without it).
  • What the line does to other functions: nothing that our narrower run can see (above).

Kept as they are

  • int building; without a value, and its one-line why in the code (one of the two declared without a value CHECKs): the table above. The other is int damage;, which each arm of the wall test assigns.
  • Seventeen (char) casts: the eleven frame-table reads into animationFrame, 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).
  • (int)(mapTimeInTicks + slot) % 40: the original divides signed (cdq, idiv at 0x00562C33).
  • Two (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 it US_RALLYING. States 3 and 108 use main's US_STATE_3Unk and US_UNKNOWN_108.
  • units[slot].owner read again for the damage call's argument and for attacker, where a local owner exists: the original reads it again there too (movsx at 0x00562FDE and 0x00563093).
  • Placeholder names (eleven CHECKs): below.

What some placeholders hold

The knight is drawn in two parts, the horse through imageIDUnk and the rider through gfxNumber, each with its own frame table per state.

  • Frames_Shared_UnitClimbingUp.field_0x2264 and .field_0x2290: the horse's frames in melee, for odd and for even slots; .field_0x2290 is also the table of the wall attack. .field_0x22dc: the rider's frames in melee.
  • .field_0x2210 and .field_0x223c: horse and rider while cheering (state 3).
  • .field_0x2104 and .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

Other PRs

🤖 Generated with Claude Code

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 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