Skip to content

Reimplement UpdateSlinger from gynt's units branch (100%, with names for unit state 123 and entity type 35) - #544

Merged
gynt merged 4 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-w3-1-header-100
Dec 4, 2026
Merged

gynt merged 4 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-w3-1-header-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Units::UpdateSlinger, from gynt's units branch reimpl/map-units-updatefunctions (his draft #204). It reads 100.0% in the default build and with main's function-visibility option on. The function's commit cites the commits of gynt's that measured the file and those that only touched its text.

UpdateHealer and UpdateSpearman, which were worked on together with it, are in #545, the other PR of this round, which has no header change. Neither PR needs the other.

Function Address Before Default build Option on
Units::UpdateSlinger 0x00572E80 gynt's draft 31.60% (e61e5a5) 100.0% 100.0%

"Before" is the status of gynt's last measured commit on the file; his later commits on it (6565c1b, 974c32f) were not measured.

Header change

UnitState gets US_UNKNOWN_123 = 123 and EntityType gets ET_UNKNOWN_35 = 35: one line in each header, in a commit of its own, named as the enums' other values without a known meaning are (US_UNKNOWN_120, US_UNKNOWN_121, ET_UNKNOWN_8, ET_UNKNOWN_30). Main has had both lines since its header sync d134c77, so once this PR is merged the commit adds nothing there.

  • 123 is the highest case of UpdateSlinger's switch on the state (0x00572EF4: cmp eax, 0x7b), and the state it stores behind moveToFreeTileNearby when that call left the unit in US_MOVE_TO_DESTINATION (0x005732D3: add ecx, 0x7b, which gives 123 there and 4, US_RELOAD_WEAPONUnk, otherwise).
  • Unit::field_0x2c4 holds the same state number. It is the short right behind state and cachedState, UpdateSlinger compares it with 123 in front of that call (0x0057329D), and the block the test guards is the one that stores state 123. So that test is written with the name too.
  • 35 is the entity type of the stone the slinger throws (0x00573C8B: push 0x23, shootProjectile's second argument). ET_SLINGER is 33.
  • No object code changes with the two lines. UpdateSlinger compiles to the same object file with the casts (UnitState)123 and (EntityType)35 and with the names, and so do the other PR's two files, which include both headers and use neither name (object files compared section by section, in the option-on layout and in the default one). The builds below are of the tree with the two lines.
  • Four files on main write (UnitState)123 for the same case label and the same store: UpdateArabianArcher, UpdateCrossbowman, UpdateCrusaderArcher and UpdateFireThrower. This PR leaves them as they are; they can take the name once main has it.

Measured

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed: the data resolvers with tools/reimplementation-control/enable_reimplemented_data.py, the function resolvers of the .cpp files in the build), and sourcehold's reccmp (a86aceb03) with --resolve-wrapped-calls --ignore-security-cookie. Normalised is main's reccmp_report.py figure, which ignores call and tail-jump targets and resolver addresses.
  • Small builds, both modes, of this branch's three commits on main 0cea04d, where it was first cut: 478 files, 488 functions (every /GL file of that main; UpdateSlinger and six of main's units, UpdatePitchman, UpdateDairyFarmer, UpdateEngineer, UpdateFireBallista, UpdateCatapult and UpdateHopsFarmer, with their callee closures; every file of a function-visibility group that touches them), BUILD_OK and 0 failures in both modes. UpdateSlinger reads 100.0% / 100.0% (raw / normalised) in both, and so does hasUnitReachedDestination. With the option on, cmake/function-visibility-units.py makes 25 wrapper units, as on main; UpdateSlinger is in the one that holds hasUnitReachedDestination.
  • Main has moved since. The branch is now cut from a0dc309, which merged Reimplement six UnitsState functions at 100%, from gynt's text on main #539, Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541 and Reimplement UnitsState::processUnitAttackOtherUnit at 100%, from gynt's text on main #542: seven changed function bodies in UnitsState and three files off the /GL list. None of those files is among the 69 functions UpdateSlinger reaches through its calls, and no .cpp calls UpdateSlinger, so the builds were not repeated on the new main. The branch's own lines are the same on both, but for that (short).
  • Against the same builds without the header change (the three functions of this round built together, with the casts in place of the two names; 488 functions are in both): every function reads the same but two. Both have been seen to move with the build's file list, and the earlier builds held UpdateHealer and UpdateSpearman too:
    • Buildings::UpdateCatapultBuilding reads 99.6% / 95.5% in both modes here; there it read 99.6% / 95.7% with the option off and 100.0% / 100.0% with it on.
    • UnitsState::processEntityDamageToUnit reads 71.5% / 67.9% in both modes here; there it read the same with the option off and 77.3% / 69.0% with it on.
  • Against main: not like for like. Main's same two builds were made for Reimplement UpdateShield, UpdateHunter, UpdateAppleFarmer, UpdateJester and UpdateSlave from gynt's units branch (100%, three with OPENSHC_FUNCTION_VISIBILITY_UNITS on) #537 from a longer list (503 files, 513 functions); 478 functions are in both. Of those, three read differently. All three have been seen to move with the build's file list, and none is changed here nor calls or is called by anything that is:
    • Buildings::UpdateFireBallistaBuilding reads 99.6% / 95.5% with the option off (100.0% / 100.0% in main's build) and 100.0% / 100.0% with it on, as in main's. Its twin UpdateCatapultBuilding reads 99.6% / 95.5% in both modes (99.6% / 95.7% and 100.0% / 100.0% in main's). The two trade one early load with the build's file set, as they did in Reimplement six UnitsState functions at 100%, from gynt's text on main #539's builds.
    • UnitsState::processEntityDamageToUnit reads 71.5% / 67.9% with the option off (77.3% / 69.0% in main's) and the same with it on, as in main's.
  • No full build. Every /GL file of main is in the small builds, and only those files take part in link-time code generation. So the builds hold the new body and every function whose code it could shift: the other callers of hasUnitReachedDestination and of the other members of its group. The files outside /GL are compiled one at a time and can't see the new body, and no .cpp names the new function. The files outside the small builds that include UnitState.hpp or EntityType.hpp were not rebuilt with the two new lines; a new enumerator changes no existing value and no type's size.
  • Text edits: each edit made for review after the text first read 100% (the two names, casts that did nothing dropped, an unreachable return; and an unused using removed, one pair of parentheses, comments) was compiled both ways, the file with its callee pasted in front of it, in the option-on layout and in the default one, and the two object files were compared section by section: the same object code. A form that changed the object code was not taken; those are under "Kept as it is" with what they read. The small builds were made before the last of these edits, which put the (short) back on the tick store, so they are of the same object code.
  • Checks: the style checks (the maintainers' review asks and main's style_lint.py): nothing to fix automatically, no errors and 36 notes, all of the kinds under "Kept as it is" (the header change itself, 21 placeholder names, 4 casts, 4 declarations without a value and the bot's note on one of them, 3 hints for field_0x58, one note on the mix of truth tests, one summary line).
  • Not run in game.

What got it to 100%

The text started from his draft and stood at 96.5% with the option on on our development branches; the commit lists what changed, with the original's addresses. The last step:

  • An expression written twice in place of a local. Where the sling is loaded and the target is a building, half the building's size is written in both sums, .x + widthOrHeight / 2 and .y + widthOrHeight / 2. The original divides once and moves the quotient out of eax (0x00573770), which leaves edx free for the zero that the next test compares with and the state store uses (0x0057377F to 0x005737B6). With a local for the half size the quotient stays in eax and that zero is an immediate. quick_diff with the callee in the unit: 0.9814 -> 0.9955, where the lines left are the same bytes written two ways.

The entry

  • UpdateSlinger joins the group that holds hasUnitReachedDestination and names that one callee, the one member of the group it calls. The commit gives what it reads alone (0.4706) and that no other /GL callee changes the ratio.
  • It is on cmake/compiler-flags-gl.txt, as main's other units are; the callee it has to see is on that list already.

Kept as it is

Each ratio is quick_diff with the callee in the unit; the text as it is reads 0.9955, which is a full match in the builds.

  • int building; without a value (the wall attack), with a comment: it is read behind the if / else, and with = 0 (0.9799) or with the tile's building read in front of the test (0.9735) the match is lost. This is the one line main's style-lint bot will note. int damage; beside it, as in main's UpdatePikeman: both arms behind it assign it. int targetX; / int targetY;: both arms of the test on the target assign them.
  • target > -1 where the stone is thrown, with a comment: the original compares with the register that holds -1 (0x00573B4A: cmp ebp, edx, jle); >= 0 reads 0.9940. The state before it tests the same field with >= 0, as the original does there.
  • Two arms that store the same pair behind the test of movementType_OR_targetUnitID, with a comment: the original has the compare and one copy of the stores (0x005731BC); written as one arm they read 0.9951.
  • The owner read from the unit again. The function holds the unit's owner in owner from its first lines (0x00572E91), and the original still reads it from the unit at two later places: an argument of processDamageToBuildingThunk (0x005734FD) and the wall attack's local attacker (0x005735B2). Through owner they read 0.9810 and 0.9269.
  • An arm that can't be reached: at the rally point, the test for a distance above 432 (0x00572FFF: cmp cx, 0x1b0) sits in the else of the test for above 400 (0x00572FE9: cmp cx, 0x190), so its arm never runs. It is the original's code.
  • The casts that are left (the checker's 4 notes):
    • (short) where mapTimeInTicks is stored into isSelectable_OR_matchTime: the tick count is uint and the field is short, so the cast narrows. Main's other units write that store the same way (UpdateArabianArcher, UpdateCrossbowman, UpdateCrusaderArcher, UpdateFireThrower, UpdateLadderman).
    • (char) on engineerManningSiegeStateRef_checkType, twice: the field is byte in the header, and the fade tests it for below 0 and for above 0, so it has to be read signed.
    • (int) on mapTimeInTicks in the % 40, as in UpdatePikeman: the field is uint, and the original divides signed (0x005730B1: cdq, idiv).
    • Gone since the first text, with the same object code: (char) on disappearFadeAlphaCountdown and on the frame tables (both char in the headers), (short) on BuildingLayer[tile] (short), (int) on widthOrHeight (int) and on (fixedRng & 7) + 1 (fixedRng is int).
  • BOOLEnum calls, IsGateOrTowerArray, animationCycleNumberHasJustIncremented (both BOOLEnum) and isMatchingSpeed (a bool) tested by truthiness; int calls, the two-byte flags, field_0x3f8 and DAT_UnitHasBecomeIdle (undefined4) compared with 0, as main does. That is the mix the checker notes.
  • Placeholder names (the checker's 21 notes, and 3 hints) are Ghidra's, for which we have nothing better yet:
    • Unit::field_0x2c4: see "Header change".
    • Unit::field_0x3f8, a byte that is only ever 0 or 1 here: cleared when the stone is thrown, tested and set to 1 where the slinger reports itself idle.
    • Unit::field_0x430 (a short, set to 1 on every update), Unit::field_0x3bc (an int, set to -40 when the target is too far away to throw at), Unit::field_0x40e (a bool, cleared at the frame the stone is thrown), Unit::field_0x2ac (a short, set to 10 in one state).
    • Unit::field_0x58 is a short that takes 0 and 1; the checker's BOOLEnum hint for it comes from a field of the same name in another struct.
    • The frame tables of UnitPropertiesDefinedData that the function reads (field_0x824, field_0x2b80, field_0x2ba0, field_0x2c7c).

Differences from the draft

We compared the operators and literals of every test against a number with gynt's draft. One test differs in what it tests, as the original has it:

  • UpdateSlinger looks for a target at the rally point when closestEnemyMicroDistance <= 432 (0x005730C6: cmp ..., 0x1b0; jg); the draft had < 432.

The rest are the same tests written the other way round with their arms exchanged (<= 70 as > 70 twice, <= 484 as > 484, < 0 as >= 0, > 0 as <= 0), < 401 as <= 400, BOOLEnum calls and fields tested by truthiness where the draft compares with 0, hex constants in decimal or as their enum names, and, in the sit-down state, one test for substate 101 where the draft nested it in a test for 101, 106 and 107. We did not read the file line by line against the draft.

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 3 commits December 2, 2026 15:06
UnitState gets US_UNKNOWN_123 = 123 and EntityType gets ET_UNKNOWN_35 = 35, one line each, named as the enums' other
values without a known meaning are (US_UNKNOWN_120, US_UNKNOWN_121, ET_UNKNOWN_8, ET_UNKNOWN_30).

Both are numbers the original has in UpdateSlinger 0x00572E80, which the next commit adds:

- 123 is the highest case of its switch on the state (`cmp eax, 0x7b` at 0x00572EF4), the state it stores behind
  moveToFreeTileNearby when that call left the unit in US_MOVE_TO_DESTINATION (`add ecx, 0x7b` at 0x005732D3: 123
  there, 4 for US_RELOAD_WEAPONUnk otherwise), and the value Unit::field_0x2c4 is tested for in front of that call
  (0x0057329D). field_0x2c4 is the short behind state and cachedState, and the block it guards stores state 123, so
  the field holds a state number and the test reads with the same name.
- 35 is the entity type of the stone the slinger throws (`push 0x23` at 0x00573C8B, shootProjectile's second argument).
  ET_SLINGER is 33; 35 has no name yet.

Four files on main write `(UnitState)123` for the same case label and the same store (UpdateArabianArcher,
UpdateCrossbowman, UpdateCrusaderArcher, UpdateFireThrower). This commit leaves them as they are.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Units::UpdateSlinger at 100%, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on the
file: 30448d6 at 28.29%, a43cf47 and e61e5a5 at 31.60%, 6565c1b, and the style commit 974c32f),
through the forms on our development branches (96.5% there with OPENSHC_FUNCTION_VISIBILITY_UNITS on).

What the match needed, against that text:

- Where the sling is loaded and the target is a building, half the building's size is written in both sums
  (`.x + widthOrHeight / 2`, `.y + widthOrHeight / 2`) in place of a local for it. The original divides once
  (0x0057376D: cdq; sub eax, edx; sar edx, 1) and moves the quotient out of eax (0x00573770), so the target's y is
  in eax on this arm as on the arm for a unit (0x00573732), and edx is free for the zero that the test behind the join
  compares with and the state store uses (xor edx, edx at 0x0057377F, cmp ecx, edx at 0x00573781, the store at
  0x005737B6, an exit that five jumps of another state share). With the local the quotient stays in eax, the y takes
  edx, the test is a `test ecx, ecx` and the store an immediate with an exit of its own. quick_diff with
  hasUnitReachedDestination in the unit: 0.9814 -> 0.9955 (1346 -> 1340 instructions, the original's count), and
  what is left is the same bytes written two ways (0xffff and -1, 0xfffe and -2).

Number casts to UnitState are the enum's names. State 123 is US_UNKNOWN_123 in the case label, in the store behind
moveToFreeTileNearby and in the test of field_0x2c4, the short behind state and cachedState, which holds a state too
(0x00572EF4, 0x005732D3, 0x0057329D); the projectile is ET_UNKNOWN_35 (push 0x23 at 0x00573C8B). Both names come with
the header commit before this one. The wall and keep bits are LogicHelpers' names, calls that return BOOLEnum,
IsGateOrTowerArray and animationCycleNumberHasJustIncremented are tested by truthiness, the damage of a hit on a
building and the state behind moveToFreeTileNearby are if / else (they were ternaries), the fade step is decremented
without a pointer cast, and the casts that did nothing are gone (`(char)` on disappearFadeAlphaCountdown and the frame
tables, `(short)` on BuildingLayer, `(int)` on widthOrHeight and on the fixedRng sum): none of these changes the object
code. The `(short)` where mapTimeInTicks (uint) is stored into isSelectable_OR_matchTime (short) stays, as main's other
units write that store.

Kept, each with a comment and with what the other form reads:
- `int building;` in the wall attack without a value: it is read behind the if / else, and with `= 0` (0.9799) or
  with the tile's building read in front of the test (0.9735) the match is lost.
- `target > -1` where the stone is thrown: the original compares with the register that holds -1 (0x00573B4A);
  `>= 0` reads 0.9940.
- The two arms on movementType_OR_targetUnitID that store the same pair: the original has the compare and one copy
  of the stores (0x005731BC); one arm reads 0.9951.

The owner is read from the unit again where the original reads it again: as an argument of
processDamageToBuildingThunk (0x005734FD) and into the wall attack's `attacker` (0x005735B2). Through the function's
`owner` these read 0.9810 and 0.9269.

compiler-flags-gl.txt gets UpdateSlinger.cpp only: hasUnitReachedDestination is on main's list already (quick_diff
0.4706 alone).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
One new entry, so that UpdateSlinger reads with OPENSHC_FUNCTION_VISIBILITY_UNITS on what it reads in the default
build. It joins the group that holds hasUnitReachedDestination and names that callee, the one member it calls.

Measured as sourcehold#491's pairs were (quick_diff, the unit's file with the callee's file pasted in front of it, against the
unit alone): 0.4706 alone, 0.9955 with hasUnitReachedDestination. Each other /GL callee the unit calls was tried one
at a time on top of it, without its own callees: none changes the ratio. What 0.9955 leaves are lines quick_diff
writes two ways (0xffff and -1, 0xfffe and -2); the builds in the pull request read the unit 100.0%.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

1 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
24 Declare variables upon first use 1
Where

Rule 24 — Declare variables upon first use

  • src/OpenSHC/Map/Units/UpdateSlinger.cpp:316 — declare building where it is first assigned

…der-100

# Conflicts:
#	cmake/compiler-flags-gl.txt
@gynt
gynt disabled auto-merge December 4, 2026 17:14
@gynt
gynt merged commit 7628d29 into sourcehold:main Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

declaration/type change This requires changes to the generated/exported types or definitions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants