Skip to content

Reimplement 6 AI::AICState functions at 100%, from gynt's drafts - #418

Open
kotijekriv wants to merge 12 commits into
sourcehold:mainfrom
kotijekriv:reimpl/AICState-100
Open

kotijekriv wants to merge 12 commits into
sourcehold:mainfrom
kotijekriv:reimpl/AICState-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Six AI::AICState functions at 100%, with no header changes. All six start from gynt's drafts on upstream's reimpl/AICState, and gynt agreed to us finishing them ("Go ahead! I am not working on those branches"). Each function is two commits: one under his name that carries his file from 5e32940 unchanged, with his status line, and one of ours on top that says what changed and credits him as co-author.

Function Address gynt's draft Draft here, reccmp (normalised) Now, reccmp (normalised)
AICState::aiGiveRaidInstructions 0x004D2A70 6a03e7d, 62.04% 62.0 (63.3) 100.0 (100.0)
AICState::addUnitToSmallestBehaviourTypeTribe 0x004CCD20 3334240, 93.19% 93.2 (81.7) 100.0 (100.0)
AICState::updateAIPlayerState 0x004D49E0 dc584e2, 81.73% 81.5 (79.9) 100.0 (100.0)
AICState::decideOnTribeAttackLocation 0x004D1A00 59f74d1, 93.20% 93.2 (87.5) 100.0 (100.0)
AICState::chooseRandomBuildingIDFromPlayersArrayOfBuildings 0x004CDB20 2521fe3, 65.96% 66.0 (66.0) 100.0 (100.0)
AICState::sendWallPatrolUnitTribesToAIVLocations 0x004D4340 207c302, 81.22% 81.2 (74.9) 100.0 (100.0)

The hashes are his last measured commit on each file. 5e32940 is his style pass over four of them; addUnitToSmallestBehaviourTypeTribe and chooseRandomBuildingIDFromPlayersArrayOfBuildings are as 3334240 and 2521fe3 left them.

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.
  • Each function: the reccmp numbers above, and quick_diff 1.0000 with the same instruction count on both sides: aiGiveRaidInstructions 241, addUnitToSmallestBehaviourTypeTribe 122, updateAIPlayerState 483, decideOnTribeAttackLocation 250, chooseRandomBuildingIDFromPlayersArrayOfBuildings 47 and sendWallPatrolUnitTribesToAIVLocations 114 (quick_diff doesn't count nop and mov edi, edi).
  • Full build: an all-on build of this head's tree (024e2d0182), on main 3458fea, every function resolver that has a .cpp switched on: 0 failed of 2,429 compile steps, 99.58 over 2,445 functions. All six read 100.0 raw and normalised.
  • Against main: compared with a full build of main alone (f489b72; 3458fea only adds files under tools/), three of the 2,439 functions on main read lower in that build:
    • UpdateFireBallistaBuilding reads 99.6; see its section below.
    • UI::Menu::handleMenuItems reads 97.9 and UI::Menu::Constructor_Menu 98.4. Their constant 0x11000000 is also a function's address in some of our builds, so reccmp prints it as a symbol. In the builds where I compared bytes, they were the same as the original's, but I did not compare them in this build.
  • With the two new functions' resolvers off, as committed: the same build with every file in it reads all six at 100.0 raw and normalised, and none of main's functions lower or higher.
  • Checks: openshc-pr-fix finds nothing to change, and openshc-pr-check gives 0 ERROR and 8 CHECK, each explained below. clang-format changes nothing in the six files.
  • Not run in game.

What got them to 100%

The behaviour of all six is the draft's.

  • aiGiveRaidInstructions: in each of the three arms that look for a building, the target tile is computed right after each of the two lookups, instead of once after both. The original has two copies of that computation (0x004D2C00 and 0x004D2D6F, one register apart), and its arms jump into each other's copies. With one computation per arm the compiler merges them into a single copy: the function comes out seven instructions short (234), with this and the player's offset in each other's registers. It takes all three arms: written twice in one arm reads 0.6320 to 0.7261, in two arms 0.7193 to 0.8050, in all three 1.0000. tools/compiler-internals/finding-mismatches.md on main lists this function as unsolved (section "Where aiGiveRaidInstructions currently stands"); this form solves it.
  • addUnitToSmallestBehaviourTypeTribe: the tribe the function returns is a variable of its own (resultTribeID). The single-tribe path sets it from createTribeForPlayer, and the loop path copies the loop's choice into it after the loop, next to the copy of the slot the draft already had. The original reads both back from the stack on the loop's exit (0x004CCE77 and 0x004CCE7B). With one variable the compiler keeps the tribe in eax through the loop, and the function is two instructions longer (124).
  • updateAIPlayerState: two things.
    • State 9 sets the state back to 0 at each of its three exits, instead of once after one combined test. The block's control flow comes out the same, with one merged store at 0x004D4FF7, but the two extra stores of 0 make the compiler keep 0 in ebp from the first if/else on (xor ebp, ebp at 0x004D4AF3), and 1 only in ecx at the dispatch, as the original does. With one store it keeps 1 in ebp, which is what gynt's remark on the draft says, and the function has 486 instructions. This change alone reads 0.9420, with the original's 483 instructions.
    • The attacked player's ID is read into a local right before the counter of attacks on that player is stepped (0x004D4A93). Without the local, the scratch registers of the argument loads from there to the end of the function are one step rotated.
  • decideOnTribeAttackLocation: two locals and the last test turned round.
    • The campground tile is a local (campgroundTile) before it is passed to canNavigateUnitsFromTileToTargetTile, which lets this be loaded first (0x004D1C1D), gynt's "this reload".
    • The target unit's ID is an int local instead of a reference to the short field: the original reads the field once with movsx and tests the register (0x004D1C81). The draft's fixme explains why it had the reference: with the int local alone the compiler merges the two campground sums, which the original keeps apart (lea eax at 0x004D1CC6, lea edx at 0x004D1D07). With the tile local in place they stay apart. The int local alone reads 0.9555, the tile local alone 0.9636 (244 instructions each), both 1.0000.
    • The last test is written so that sendUnitsToCampfire is the last statement. The instructions are the same either way, but with commandUnitsToLocation last, four conditional returns (0x004D1B70, 0x004D1BB6, 0x004D1C53 and 0x004D1D19) aim at the copy of the return sequence after that call, and reccmp reads 97.2 (normalised 93.1).
  • chooseRandomBuildingIDFromPlayersArrayOfBuildings: two things together.
    • The buildings are read by index instead of through a pointer that walks the array. The compiler makes the walking pointer itself (lea ebp at 0x004CDB47) and turns the counter into the count kept on the stack, as the original has it.
    • Each of the three priority sets tests for equality and does the two stores in its own arm. The compiler merges the three pairs into the one copy the original has (0x004CDB95 and 0x004CDB97).
    • Measured: the draft reads 0.8298, the index alone 0.4742 (50 instructions), the arms alone 0.8298, both 1.0000.
  • sendWallPatrolUnitTribesToAIVLocations: the patrol slot is computed from j * stepSize instead of from a second variable that the for loop advances by stepSize. The compiler turns the product into that running sum itself and starts it where the original does, behind the loop's entry test (xor ebp, ebp at 0x004D43FB). Until there ebp is free, so playerID stays in it outside the inner loop. The draft reads 0.8634 (113 instructions), the variable zeroed right before the loop or in the for's init 0.8282, and j * stepSize 1.0000.

UpdateFireBallistaBuilding with the two new functions' resolvers on

UpdateFireBallistaBuilding (0x0041E240) reads 100% on main. In a full all-on build of this branch it reads 99.6 (normalised 95.5): one mov edx from an int global sits at a different place. With the resolvers of chooseRandomBuildingIDFromPlayersArrayOfBuildings and sendWallPatrolUnitTribesToAIVLocations off, as committed, it reads 100.0. Switching on either of the two is enough to move it.

What we know:

  • Its source and the headers it includes are unchanged by this branch.
  • Reimplement 3 UI::MenuItems functions from gynt's drafts (not yet 100%) #328's base e086ba7 reads the same 99.6 without these functions. The file's own input is identical there to main's: the same header text, with the same function resolvers on.
  • With the two resolvers on, padding the file's symbol count with dummy typedefs changed nothing: N = 0 to 63 after the AICState.func.hpp include, N = 0 to 31 after the last include. With them off, every N from 0 to 63 after the AICState.func.hpp include reads 100.
  • A link of the /GL objects alone reads 100 with the two resolvers on, while the whole link reads 99.6.

What we think, not proven: the function is built with /GL, so its code is generated at link time, and the whole link decides it rather than its own source. A small unrelated change elsewhere can move it either way.

The checker's 8 CHECKs

File and line Finding Where it is answered
addUnitToSmallestBehaviourTypeTribe.cpp:17 undefined type in code "Proposed": the undefined4 parameters
addUnitToSmallestBehaviourTypeTribe.cpp:29 declared without a value "Kept": int groupsCount;
aiGiveRaidInstructions.cpp:40 for loop without its counter in the header "Kept": for (; i < ...)
aiGiveRaidInstructions.cpp:54 declared without a value "Kept": int tile;
decideOnTribeAttackLocation.cpp:57 number cast to UnitInstructionType "Proposed": (UnitInstructionType)35
decideOnTribeAttackLocation.cpp:117 declared without a value "Kept": int destinationTile;
decideOnTribeAttackLocation.cpp:123 cast on a global "Kept": the (short) on the PathConnectionLayer read
sendWallPatrolUnitTribesToAIVLocations.cpp:49 for loop without its counter in the header "Kept": for (; j < groups; ++j)

Kept as they are

  • sendWallPatrolUnitTribesToAIVLocations: for (; j < groups; ++j). j starts at the top of the outer loop's body: started in the for header the function reads 0.9561, as the group limit then sits in edi for the division.
  • aiGiveRaidInstructions: for (; i < countof(DAT_SkirmishDefinedData::instance.TribeTypeID); ++i). i is declared at the top, before aicIndex: declared in the for header the function reads 0.8737 (242 instructions).
  • int groupsCount;, int tile; and int destinationTile; (addUnitToSmallestBehaviourTypeTribe, aiGiveRaidInstructions, decideOnTribeAttackLocation): each is assigned in several arms and read after them.
  • updateAIPlayerState: the != 1 next to the == 2 on requestStateUnk has a fixme: the original makes both compares (0x004D4A0C and 0x004D4A11).
  • decideOnTribeAttackLocation: the (short) on the PathConnectionLayer read is from gynt's draft, and several of main's readers of that layer write it the same way.

Proposed, not changed here

  • addUnitToSmallestBehaviourTypeTribe's undefined4 parameters: playerID is an int: it is multiplied by the size of PlayerData (imul eax, eax, 0x39f4 at 0x004CCD2A) and passed to createTribeForPlayer (0x004CCDFB). aiUnitBehaviourType is an AIUnitBehaviourType: it indexes AIUnitBehaviourTypeMapping counted from AIUBT_ENGINEERING (loaded at 0x004CCD45, used at 0x004CCD4A). The second parameter is never read.
  • pathFindingToAttackCastleIncludingMoat returns a bool: PathFindingState.hpp and PathFindingState.func.hpp declare it as returning BOOLEnum, but its four returns set only al (0x004A423C, 0x004A4416, 0x004A4433 and 0x004A4462), and the original tests only al after the call (0x004D1AED).
  • 182 and 185 in decideOnTribeAttackLocation: it compares tribes[].tribeSubtype1 with these numbers (0x004D1B4C and 0x004D1B56). They equal AITI_DEF_WALKING_RANGED and AITI_DEF_SIEGE, but the field isn't shown to hold an AITribeIndex, so the numbers stay.
  • (UnitInstructionType)35 in decideOnTribeAttackLocation (push 0x23 at 0x004D1B0C): the enum has no name for 35 yet.

Other PRs

🤖 Generated with Claude Code

gynt and others added 12 commits November 18, 2026 16:25
AICState::aiGiveRaidInstructions as gynt has it on upstream's reimpl/AICState (5e32940), unchanged: his 6a03e7d
(2026-11-03) and his style commit 5e32940 (2026-11-07, his newest on the file), with his status line. The next
commit takes it to 100.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::aiGiveRaidInstructions, from gynt's draft: ae6823b, the commit before this one, is his file from
upstream's reimpl/AICState (6a03e7d at 62.04%; before it 718b754 at 60.3%, 5eb6821 at 58.57%, efe657d
at 30.02% and e88be70 at 29.89%). reccmp reads 100.0 (normalized 100.0) where the draft reads 62.0 (63.3);
quick_diff 1.0000, 241 instructions on both sides. The function's structure, its names and everything outside
the three lookups are his.

What the match needed: in each of the three arms that look for a building (the ally's request, the raid, the ranged
tribes), the target tile is computed right after each of the two lookups: once when the first lookup finds a
building, and once after the fallback lookup. The draft computed it once per arm, after both lookups.

The original has two copies of that computation, at 0x004D2C00 and 0x004D2D6F, which differ in one register (edx
against ecx), and the arms jump into each other's copies. That is what the compiler leaves of the six computations
written here. With one computation per arm it merges the three into a single copy: the function comes out seven
instructions short (234), and from the prologue on `this` and the player's offset sit in each other's registers.

Measured one arm at a time (quick_diff; the arms in the order request, raid, ranged): written twice in the first
only 0.6320, the second only 0.7261, the third only 0.7110, the first two 0.7344, the first and third 0.7193, the
last two 0.8050, all three 1.0000. A nested shape with one computation per arm reads 0.7284, like the draft, and
the fallback lookup first with the computation in both branches reads 0.7625.

Style, measured the same: aiType is tested against AITA_NULL, the loop bound is countof(TribeTypeID) and the step
is ++i. The counter stays declared at the top, before aicIndex, as in the draft: declared in the for header the
function reads 0.8737 (242 instructions).

The behaviour is the draft's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…latc

AICState::addUnitToSmallestBehaviourTypeTribe as gynt has it on upstream's reimpl/AICState (5e32940), unchanged:
his 7985988 (2026-11-03) and 3334240 (2026-11-07, his newest on the file), with his status line. The next
commit takes it to 100.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::addUnitToSmallestBehaviourTypeTribe, from gynt's draft: 0ce3fee, the commit before this one, is his
file from upstream's reimpl/AICState (3334240 at 93.19%; before it 7985988 and a013994 at 80.13%,
fc674a0 at 66.03% and 61e841a at 63.91%). reccmp reads 100.0 (normalized 100.0) where the draft reads 93.2
(81.7); quick_diff 1.0000, 122 instructions on both sides. The switch, the two paths, the loop with its end slot
in groupsCount and the copy of the slot after the loop are his.

What the match needed: the tribe the function returns is a variable of its own (resultTribeID). The single-tribe
path sets it from createTribeForPlayer, and the loop path copies the loop's choice into it after the loop, next to
the copy of the slot that the draft already had there.

The original reads both back from the stack on the loop's exit (0x004CCE77 and 0x004CCE7B) and keeps neither in a
register inside the loop. With one variable for the tribe, the compiler keeps it in eax through the loop: it loads
it at the end of every pass, the function is two instructions longer (124), and the two stack slots trade places.

Also tried on the draft (quick_diff): the loop's three stores in two other orders and the size in a local (0.9431,
as the draft), the two declarations the other way round (0.9350), and the slot set before the call on the break
path (0.8421).

Style, measured the same: the four case labels are AITribeIndex names, groupsCount += firstSlot, the step is
++slot, and the mapping is indexed from AIUBT_ENGINEERING.

The behaviour is the draft's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::updateAIPlayerState as gynt has it on upstream's reimpl/AICState (5e32940), unchanged: his dc584e2
(2026-11-03, 81.73%) and his style commit 5e32940 (2026-11-07, his newest on the file), with his status line.
An older version by the sourcehold account, a150748 (2026-09-17, 52.84%), is on upstream's
reimpl/AICState-backup. The next commit takes up the difference gynt's remark names.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::updateAIPlayerState, from gynt's draft: 1497254, the commit before this one, is his file from upstream's
reimpl/AICState (dc584e2 at 81.73%, "Pools 1 instead of 0 in ebp"). reccmp reads 100.0 (normalized 100.0)
where the draft reads 81.5 (79.9); quick_diff 1.0000, 483 instructions on both sides (the draft had 486). The state
machine and everything outside the two places below are his (an older version by the sourcehold account,
a150748 at 52.84%, is on upstream's reimpl/AICState-backup).

What the match needed, two things:

- State 9 sets the state back to 0 at each of its three exits (the ticker reaching 20, the tribes standing still,
  no troops left) instead of once after one combined test. This block's control flow comes out the same either
  way, with one merged store at 0x004D4FF7. The merge comes after register allocation, though, and the two extra
  stores of 0 are what makes the compiler keep 0 in ebp from the first if/else on (xor ebp, ebp at 0x004D4AF3)
  and 1 only in ecx at the dispatch, as the original does. With one store it keeps 1 in ebp, which is what gynt's
  remark says, and the function has 486 instructions. That alone reads 0.9420 in quick_diff, with the original's
  483 instructions.
- The attacked player's ID is read into a local right before the counter of attacks on that player is stepped
  (0x004D4A93). Without the local, the scratch registers of the one-off argument loads are one step rotated from
  there to the end of the function: the original has the attacked player's product in eax and the player's shift
  in edx, the draft the other way round. Six other spellings of that increment compile like the draft.

Read out of the compiler with color_probe.py: both constants are register candidates; in the draft 1 has key -25
and 0 has key -27, and the higher key is served first and takes ebp. A store of the constant raises its key by
about 6, a push of it lowers it by 2, an add by 1.

Style, measured the same: the two using lines and the BOOLEnum.hpp include that nothing uses are gone; the two
places above carry a one-line why each, and the test that compares requestStateUnk with 1 and then with 2 has
a fixme (the original makes both compares, 0x004D4A0C and 0x004D4A11).

The behaviour is the draft's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::decideOnTribeAttackLocation as gynt has it on upstream's reimpl/AICState (5e32940), unchanged: his
59f74d1 (2026-11-07, 93.20%; before it 92ea87c at 93.01%, eb33d1d and 123ac02 at 91.60%, 6a93c2c
at 68.80% and f156123 at 58.00%), d76377d and his style commit 5e32940 (his newest on the file), with his
status line. The next commit takes it to 100.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::decideOnTribeAttackLocation, from gynt's draft: be4686a, the commit before this one, is his file from
upstream's reimpl/AICState (59f74d1 at 93.20%, "this reload, regalloc differs"). reccmp reads 100.0 (normalized
100.0) where the draft reads 93.2 (87.5); quick_diff 1.0000, 250 instructions on both sides. The function's
structure and everything outside the two places below are his.

What the match needed, two plain locals and the last test turned round:

- In the first branch, the campground tile is a local (campgroundTile) before it is passed to
  canNavigateUnitsFromTileToTargetTile. That puts the sum in eax and lets `this` be loaded first (0x004D1C1D), gynt's
  "this reload".
- The target unit's ID is an int local (targetUnitID) instead of a reference to the short field. The original
  reads the field once with movsx and tests the register (0x004D1C81); the reference compared the word in memory
  and read it again. The draft's fixme explains why it had the reference: with the int local alone the compiler
  makes the two campground sums of that part identical and merges them (244 instructions), where the original
  keeps them apart (lea eax at 0x004D1CC6, lea edx at 0x004D1D07, sharing only the last add). With the tile local
  of the first branch in place they stay apart on their own.

- The function's last test is written so that sendUnitsToCampfire is its last statement: units that can reach
  the tile are commanded there and the function returns, the others go to the campfire. The instructions are
  the same either way, but the conditional returns of the whole function jump to the return sequence after the
  last statement: with commandUnitsToLocation last, four of them (the original's 0x004D1B70, 0x004D1BB6,
  0x004D1C53 and 0x004D1D19) aimed at the copy after that call instead, and reccmp read 97.2 (normalized 93.1).

Measured (quick_diff): the int local alone 0.9555 (244 instructions); the tile local alone on the draft 0.9636
(244); both together 1.0000. The campground's row in a local as well, in either of the two later branches, also
reads 1.0000 and is not needed; without the tile local it does not help (0.9555).

The behaviour is the draft's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::chooseRandomBuildingIDFromPlayersArrayOfBuildings as gynt has it on upstream's reimpl/AICState
(5e32940), unchanged: his 2521fe3 (2026-11-03, 65.96%, "Loop register allocation"), his only commit on the
file on that branch, with his status line. The next commit takes it to 100.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::chooseRandomBuildingIDFromPlayersArrayOfBuildings, from gynt's draft: f96142b, the commit before this
one, is his file from upstream's reimpl/AICState (2521fe3 at 65.96%, "Loop register allocation"). reccmp reads
100.0 (normalized 100.0) where the draft reads 66.0 (66.0); quick_diff 1.0000, 47 instructions on both sides, and
the ten jumps land on the same instructions (openshc-jumps.py). The function's logic and names are his.

What the match needed, two things together:

- The buildings are read by index, top100TargetableBuildings[i], instead of through a pointer that walks the
  array. The compiler makes the walking pointer itself (lea ebp at 0x004CDB47, add ebp, 2 in the latch) and turns
  the counter into the count kept on the stack (sub dword ptr [esp + 20], 1), as the original has it.
- Each of the three priority sets tests for equality and does the two stores in its own arm, instead of three
  "not equal: continue" tests in front of one shared pair of stores. The compiler merges the three pairs into the
  one copy the original has (mov edx, ecx at 0x004CDB95, mov eax, esi at 0x004CDB97), and the building ID is then
  served before the building type's temporary, so the ID gets esi and the type edi.

Measured (quick_diff): the draft 0.8298; the index alone 0.4742 (50 instructions); the arms alone on the draft's
pointer 0.8298; both 1.0000. A second index for the array next to the counter also reads 1.0000 and is not
needed. A count-down do-while behind a guard with the arms reads 0.9787.

Also changed, none of it moves the code (quick_diff 1.0000 before and after each): the start value of
chosenPriority is countof(BuildingTargetPrioritySet1) instead of 46, rng and buildingID are const, ++i and ++j.

The behaviour is the draft's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::sendWallPatrolUnitTribesToAIVLocations as gynt has it on upstream's reimpl/AICState (5e32940),
unchanged: his 207c302 (2026-11-07, 81.22%, "playerID in edi, not ebp"; before it f0e44ad at 77.19%) and his
style commit 5e32940 (his newest on the file), with his status line. The next commit takes it to 100.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AICState::sendWallPatrolUnitTribesToAIVLocations, from gynt's draft: 475baec, the commit before this one, is his
file from upstream's reimpl/AICState (5e32940, his style pass over 207c302 at 81.22%, "playerID in edi, not
ebp"). reccmp reads 100.0 (normalized 100.0) where the draft reads 81.2 (74.9); quick_diff 1.0000, 114
instructions on both sides, and the 16 jumps land on the same instructions (openshc-jumps.py). The function's
structure and names are his.

What the match needed, one thing: the patrol slot is computed from j * stepSize instead of from a second
variable (patrolGroupIndex) that the for loop advances by stepSize. The compiler turns the product into that
running sum itself, and it starts it where the original does: behind the loop's entry test, next to the
player's row offset (xor ebp, ebp at 0x004D43FB). Until there ebp is free, so playerID stays in it through the
outer loop's body and is loaded again when the inner loop is left (0x004D44B7), `this` gets edi before the
loops, and the group limit is tested in its stack slot (0x004D43C3). With the variable zeroed in the source it
is live from the top of the outer loop's body and takes ebp there.

The group counter j keeps its start value at the top of the outer loop's body, as in the draft; the comment
says why.

Measured (quick_diff): the draft 0.8634 (113 instructions); patrolGroupIndex zeroed right before the loop or in
the for's init 0.8282; j * stepSize with j started in the for 0.9561 (the group limit sits in edi for the
division); j * stepSize with j started at the top 1.0000; stepSize * j the same.

Also changed, none of it moves the code (quick_diff 1.0000 before and after each): five using lines and three
includes that nothing uses any more are gone (AITypeInt, AIVUnitTypeInt, AIVUnitTypeMaxLocationPair), ++i and
++j, the outer loop's bound is countof of the patrol table instead of 3, and aiType, aicIndex, enclosed, count,
tribeID and slot are const.

The behaviour is the draft's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kotijekriv kotijekriv changed the title Reimplement 4 AI::AICState functions at 100%, from gynt's drafts Reimplement 6 AI::AICState functions at 100%, from gynt's drafts Nov 19, 2026

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.

2 participants