Repository navigation
Reimplement 6 AI::AICState functions at 100%, from gynt's drafts - #418
Open
kotijekriv wants to merge 12 commits into
Open
kotijekriv wants to merge 12 commits into
kotijekriv wants to merge 12 commits into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.AICState::aiGiveRaidInstructionsAICState::addUnitToSmallestBehaviourTypeTribeAICState::updateAIPlayerStateAICState::decideOnTribeAttackLocationAICState::chooseRandomBuildingIDFromPlayersArrayOfBuildingsAICState::sendWallPatrolUnitTribesToAIVLocationsThe hashes are his last measured commit on each file. 5e32940 is his style pass over four of them;
addUnitToSmallestBehaviourTypeTribeandchooseRandomBuildingIDFromPlayersArrayOfBuildingsare as 3334240 and 2521fe3 left them.Measured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie.aiGiveRaidInstructions241,addUnitToSmallestBehaviourTypeTribe122,updateAIPlayerState483,decideOnTribeAttackLocation250,chooseRandomBuildingIDFromPlayersArrayOfBuildings47 andsendWallPatrolUnitTribesToAIVLocations114 (quick_diff doesn't countnopandmov edi, edi)..cppswitched on: 0 failed of 2,429 compile steps, 99.58 over 2,445 functions. All six read 100.0 raw and normalised.tools/), three of the 2,439 functions on main read lower in that build:UpdateFireBallistaBuildingreads 99.6; see its section below.UI::Menu::handleMenuItemsreads 97.9 andUI::Menu::Constructor_Menu98.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.openshc-pr-fixfinds nothing to change, andopenshc-pr-checkgives 0 ERROR and 8 CHECK, each explained below. clang-format changes nothing in the six files.What got them to 100%
The behaviour of all six is the draft's.
thisand 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.mdon main lists this function as unsolved (section "WhereaiGiveRaidInstructionscurrently stands"); this form solves it.resultTribeID). The single-tribe path sets it fromcreateTribeForPlayer, 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).xor ebp, ebpat 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.campgroundTile) before it is passed tocanNavigateUnitsFromTileToTargetTile, which letsthisbe loaded first (0x004D1C1D), gynt's "this reload".lea eaxat 0x004D1CC6,lea edxat 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.sendUnitsToCampfireis the last statement. The instructions are the same either way, but withcommandUnitsToLocationlast, 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).lea ebpat 0x004CDB47) and turns the counter into the count kept on the stack, as the original has it.j * stepSizeinstead of from a second variable that the for loop advances bystepSize. 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, ebpat 0x004D43FB). Until there ebp is free, soplayerIDstays 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, andj * stepSize1.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): onemov edxfrom an int global sits at a different place. With the resolvers ofchooseRandomBuildingIDFromPlayersArrayOfBuildingsandsendWallPatrolUnitTribesToAIVLocationsoff, as committed, it reads 100.0. Switching on either of the two is enough to move it.What we know:
AICState.func.hppinclude, N = 0 to 31 after the last include. With them off, every N from 0 to 63 after theAICState.func.hppinclude reads 100./GLobjects 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
addUnitToSmallestBehaviourTypeTribe.cpp:17undefined4parametersaddUnitToSmallestBehaviourTypeTribe.cpp:29int groupsCount;aiGiveRaidInstructions.cpp:40for (; i < ...)aiGiveRaidInstructions.cpp:54int tile;decideOnTribeAttackLocation.cpp:57UnitInstructionType(UnitInstructionType)35decideOnTribeAttackLocation.cpp:117int destinationTile;decideOnTribeAttackLocation.cpp:123(short)on thePathConnectionLayerreadsendWallPatrolUnitTribesToAIVLocations.cpp:49for (; j < groups; ++j)Kept as they are
for (; j < groups; ++j).jstarts 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.for (; i < countof(DAT_SkirmishDefinedData::instance.TribeTypeID); ++i).iis declared at the top, beforeaicIndex: declared in the for header the function reads 0.8737 (242 instructions).int groupsCount;,int tile;andint destinationTile;(addUnitToSmallestBehaviourTypeTribe,aiGiveRaidInstructions,decideOnTribeAttackLocation): each is assigned in several arms and read after them.!= 1next to the== 2onrequestStateUnkhas afixme: the original makes both compares (0x004D4A0C and 0x004D4A11).(short)on thePathConnectionLayerread is from gynt's draft, and several of main's readers of that layer write it the same way.Proposed, not changed here
addUnitToSmallestBehaviourTypeTribe'sundefined4parameters:playerIDis an int: it is multiplied by the size ofPlayerData(imul eax, eax, 0x39f4at 0x004CCD2A) and passed tocreateTribeForPlayer(0x004CCDFB).aiUnitBehaviourTypeis anAIUnitBehaviourType: it indexesAIUnitBehaviourTypeMappingcounted fromAIUBT_ENGINEERING(loaded at 0x004CCD45, used at 0x004CCD4A). The second parameter is never read.pathFindingToAttackCastleIncludingMoatreturns a bool:PathFindingState.hppandPathFindingState.func.hppdeclare it as returningBOOLEnum, but its four returns set only al (0x004A423C, 0x004A4416, 0x004A4433 and 0x004A4462), and the original tests only al after the call (0x004D1AED).decideOnTribeAttackLocation: it comparestribes[].tribeSubtype1with these numbers (0x004D1B4C and 0x004D1B56). They equalAITI_DEF_WALKING_RANGEDandAITI_DEF_SIEGE, but the field isn't shown to hold anAITribeIndex, so the numbers stay.(UnitInstructionType)35indecideOnTribeAttackLocation(push 0x23at 0x004D1B0C): the enum has no name for 35 yet.Other PRs
cmake/compiler-flags-gl.txt, which this branch doesn't touch, and conflict with each other there. Map::Units::UpdateLion at 100% #425 adds a line to that file too, and merges cleanly with each of the three. With cmake: function-visibility.txt, which callers must see which callees' bodies #398, UI: more functions at 100% from our drafts #313, #327 and #328, with two header changes #417, UI: barracks and mercenary post click handlers, with the voice-line tables as globals #420, UI: three more functions at 100%: the lobby's start button, the save list's header and the mission rows #423, Map::Units::UpdateLion at 100% #425 and any one of those three together, it merges cleanly.reimpl/AICStatecan take main's version of these six files once this lands. Seven more of his drafts remain on that branch; four of them are in our draft AICState: four functions from gynt's drafts that stop short of 100%, with what is left on each #424, below 100%.🤖 Generated with Claude Code