Skip to content

One more Map::Units function at 100%: UpdateHorseArcher_DoShooting - #475

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

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-D-doshooting-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateHorseArcher_DoShooting (the horse archer's
shooting step, called from UpdateHorseArcher, which is in #449) 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::UpdateHorseArcher_DoShooting 0x00575820 gynt's draft 70.33% (9fde7f2) 100.0%, under /GL

What got it to 100%. Five arrangements, each measured:

  • Variation 5's failure path (the target is gone and no new one is found) ends in goto resetShooting; into the
    function's last lines: the original shares them there (0x00575C21, jumped to at 0x00575A6E). Written out, the
    copy counts as more uses of the constant 4, and the compiler then keeps 4 in a register through the function
    where the original compares with an immediate 4. Variation 6's failure keeps its own copy, as in the original.
  • horseArcherShootingVariation = 6; is written on both paths of variation 5: the code has one store
    (0x00575A4B), but with the two written uses the 6 gets its register (mov edi, 6 at 0x00575935), as in the
    original.
  • Variation 4 reads the facing direction and the target building's index into int locals in front of the
    targeting-type test (the original loads both before it branches, 0x00575865 / 0x0057586C).
  • Variation 6's acquireShootTarget test has the failure arm first, so that it is the fall-through.
  • The three height sums read terrainOrClimbHeight first (0x00575A35, 0x00575AAF, 0x00575AD0). Our quick
    compile could not see this (it folds the field offsets); the real build showed it.

(EntityType)24 stays a cast: EntityType has no entry for 24 (an ET_UNKNOWN_24 would be a header change of its
own). target > -1 stays as the draft wrote it: the original compares with -1, and >= 0 compiles differently.

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 this branch on main 0e4a83b, with both resolver sets on and the branch's own /GL list (main's plus this file), and 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.58 over 2,442 functions. UpdateHorseArcher_DoShooting reads 100.0 raw and normalised there too, with no diff entry, and its two callees, setFacingDirectionTowardCoords and setFacingDirectionTowardUnit (main's, not on main's /GL list), read 100.0 as before. No other main function reads differently: against main's own build (made at f489b72, 2,439 functions) the only other changes are main's since then (One more UI::MenuModals function at 100%: ProgressBarBox #467's MenuModalRenderFunction_ProgressBarBox and its constructor, UI: the single-player game launch at 100% #466's LaunchSinglePlayerGameUnk), and every other function reads exactly what it read before (unrounded 99.5790 raw and 99.5529 normalised, 2,326 functions at 100.0 raw; main 0e4a83b alone: 99.5788 and 99.5528 over 2,441, 2,325 at 100.0). UpdateHorseArcher, which calls this function, is in Three more Map::Units functions at 100%: UpdateSwordsman, UpdateArabianSwordsman and UpdateHorseArcher #449 and so not in this build; on main no reimplemented function calls it.
  • Our own narrower run: real builds of our units stack's head (Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 and the units on it; every header this file
    includes is the same there as on main 0e4a83b). Its two callees, setFacingDirectionTowardCoords and
    setFacingDirectionTowardUnit, are on that stack's /GL list only through Reimplement 47 UnitsState functions at 100% and restyle 28 more #378, so the build for this PR takes
    their two lines off locally, as main has them: UpdateHorseArcher_DoShooting 100.0% raw and normalised, no diff
    entries, 345 functions, none of the other 344 reading differently from the build without it. With the two
    callees on the list it reads the same.
  • Not run in game.

Checks: openshc-pr-check gives 0 ERROR and 12 CHECK:

  • the placeholder names field_0x58, field_0x40e and field_0x3f8 (no better name in this tree); its CHECK that
    field_0x58 is a BOOLEnum field matches a field of that name in SoundEffectsHelperData1.hpp: the unit's
    field_0x58 is a short;
  • the (char) casts on the three ANIM_Frames_HorseArcher_Shooting_* tables, whose elements are byte: the cast
    makes the frame signed for the > 0 tests, as in the other unit files;
  • (EntityType)24 (see above);
  • the goto (two CHECKs, and main's style-lint bot will post it once): its target has two predecessors, the end of
    the function and variation 5's failure path, and the comment says what writing it out does.

Other PRs

🤖 Generated with Claude Code

Units::UpdateHorseArcher_DoShooting, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his draft
PR 204; his commits on the file: 9fde7f2 at 70.33%, then 974c32f's clang-format), with this tree's field
names (43a17df). It reads a true 100.0 / 100.0 in main's visibility (see the end). The
statements are his except the building index local, the goto with its label and the second `= 6` store (the
facing direction is now an int local); what the text needed is how they are arranged.

1. Shooting variation 5's failure path (the target is gone and no new one is found) ends in `goto resetShooting;`
   into the function's last lines, which reset the state and the variation: the original shares them there
   (0x00575C21, jmp at 0x00575A6E; variation 6's failure keeps its own copy, which goes on to the frame test).
   Written out, the copy counts as more uses of the constant 4 (the reload state), and the compiler then keeps 4
   in a register through the function where the original compares with an immediate 4.
2. `horseArcherShootingVariation = 6;` is written on both paths of variation 5 (behind the copied target position
   and for no target). The code has one store (0x00575A4B, `mov word ptr [esi + ...], di`); with the two written
   uses the constant 6 gets its register (`mov edi, 6` at 0x00575935), as in the original.
3. Variation 4: the facing direction (savedFacingDirection) and the target building's index (targetBuildingID)
   are int locals in front of the targeting-type test (both loaded before the branch, `movsx` at 0x00575865 and
   0x0057586C); the index is used for the building's x and y.
4. Variation 6: the acquireShootTarget test the original's way round, the failure (reset) first, so that it is
   the fall-through (0x00575B87).
5. The three height sums terrainOrClimbHeight first (the target position copy and both arrowShootingRelated
   heights): the original loads it in front of buildingHeight (0x00575A35, 0x00575AAF, 0x00575AD0).
6. The calls that return BOOLEnum, the BOOLEnum field animationCycleNumberHasJustIncremented and `dying` tested
   by truthiness; the gfx offsets in decimal and `(EntityType)24` (EntityType has no entry for 24); the usings
   and the include nobody names dropped. `target > -1` stays (the original compares with -1; `>= 0` differs).

Measured with real builds of the units stack's head 6e3de74062 (every header this file includes is the same there
as on main 0e4a83b), the whole build list plus this file and its /GL line, BUILD_OK: with the /GL lines of its
two callees (setFacingDirectionTowardCoords / -TowardUnit, on the stack's list only through PR 378) taken off,
as main has them, UpdateHorseArcher_DoShooting reads 100.0 raw / 100.0 normalised, no diff entry, 345 functions,
none of the others reading differently. With the two callees on the list (a build of that stack with this
function, UpdateFireEater and UpdateLady, 350 functions), the text before the goto's comment was reworded read the
same.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

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
20 Keep nesting low; no goto 1
Where

Rule 20 — Keep nesting low; no goto

  • src/OpenSHC/Map/Units/UpdateHorseArcher_DoShooting.cpp:110 — goto; check how many predecessors the target has first

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