From ae6823b89411f32f95f3db9e1b33fb80e33c564e Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:25:30 +0100 Subject: [PATCH 01/12] reimplement: SHC_3BB0A8C1_0x004D2A70 62.0% Navigate branch polarity AICState::aiGiveRaidInstructions as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his 6a03e7d06 (2026-11-03) and his style commit 5e329403d (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 --- .../AI/AICState/aiGiveRaidInstructions.cpp | 158 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp diff --git a/src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp b/src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp new file mode 100644 index 0000000000..8dc2db3e40 --- /dev/null +++ b/src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp @@ -0,0 +1,158 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeIndex.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_SkirmishDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace AI { + + // FUNCTION: STRONGHOLDCRUSADER 0x004D2A70 + void AICState::aiGiveRaidInstructions(int playerID) + { + int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; + if (aiType == 0) + return; + int i = 0; + int aicIndex = aiType - 1; + + DAT_GameState::instance.playerDataArray[playerID].raidRetargetDelayCounter++; + if (DAT_GameState::instance.playerDataArray[playerID].raidRetargetDelayCounter + >= this->aics[aicIndex].RaidRetargetDelay) + DAT_GameState::instance.playerDataArray[playerID].raidRetargetDelayCounter = 0; + + if (DAT_GameState::instance.playerDataArray[playerID].totalRaidingTroopsUnk == 0) + DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown = 0; + else if (DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown > 0) + DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown--; + + for (; i < 6; i++) { + int tribeID = DAT_GameState::instance.playerDataArray[playerID] + .aiTribeIDs[OpenSHC::AI::Tribes::AITI_DEF_KNIGHTS + i]; + DAT_TribesState::instance.tribes[tribeID].unitStance = OpenSHC::Map::Units::Behavior::USE_AGGRESSIVE; + if (tribeID == 0) + continue; + if (DAT_TribesState::instance.tribes[tribeID].uid + != DAT_GameState::instance.playerDataArray[playerID] + .aiTribeUIDs[i + OpenSHC::AI::Tribes::AITI_DEF_KNIGHTS]) + continue; + + int buildingID = 0; + int tile; + if (DAT_GameState::instance.playerDataArray[playerID].requestStateUnk == 2 + && MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, DAT_UnitsState::ptr)( + DAT_GameState::instance.playerDataArray[playerID].playerID_askerUnk) + != 0) { + if (DAT_GameState::instance.playerDataArray[playerID].raidRetargetDelayCounter != 0) + continue; + buildingID + = MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::selectRandomBuildingIDFromListOfBuildingTypes, + this)(DAT_GameState::instance.playerDataArray[playerID].playerID_askerUnk, i); + if (buildingID == 0) + buildingID = MACRO_CALL_MEMBER( + OpenSHC::AI::AICState_Func::selectBuildingFromAListOfBuildingTypes, this)(playerID); + if (buildingID == 0) + continue; + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } else if (DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown == 0 + && DAT_GameState::instance.playerDataArray[playerID].totalRaidingTroopsUnk < MACRO_CALL_MEMBER( + OpenSHC::AI::AICState_Func::getCurrentDesiredAttackRaidUnitCount, this)(aicIndex, playerID)) { + if (DAT_GameState::instance.playerDataArray[playerID].raidRetargetDelayCounter != 0) + continue; + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::checkTribeActivityPercentages, this)( + tribeID, i, TRUE)) + continue; + buildingID = MACRO_CALL_MEMBER( + OpenSHC::AI::AICState_Func::selectRandomBuildingIDFromListOfBuildingTypes, this)(playerID, i); + if (buildingID == 0) + buildingID = MACRO_CALL_MEMBER( + OpenSHC::AI::AICState_Func::selectBuildingFromAListOfBuildingTypes, this)(playerID); + if (buildingID == 0) + continue; + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } else if (DAT_SkirmishDefinedData::instance.TribeTypeID[i] == OpenSHC::AI::Tribes::AITI_DEF_WALKING_RANGED + || DAT_SkirmishDefinedData::instance.TribeTypeID[i] == OpenSHC::AI::Tribes::AITI_DEF_HORSE_ARCHERS) { + if (DAT_GameState::instance.playerDataArray[playerID].raidRetargetDelayCounter != 0) + continue; + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::checkTribeActivityPercentages, this)( + tribeID, i, FALSE)) + continue; + buildingID + = MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::selectRandomBuildingIDFromListOfBuildingTypes, + this)(DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID_2Unk, i); + if (buildingID == 0) + buildingID = MACRO_CALL_MEMBER( + OpenSHC::AI::AICState_Func::chooseRandomBuildingIDFromPlayersArrayOfBuildings, this)( + DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID_2Unk); + if (buildingID == 0) + continue; + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } else { + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::checkTribeActivityPercentages, this)( + tribeID, i, FALSE)) + continue; + + int targetBuildingID = DAT_TribesState::instance.tribes[tribeID].targetBuildingID; + if (targetBuildingID != 0 + && DAT_BuildingsState::instance.buildings[targetBuildingID].uid + == DAT_TribesState::instance.tribes[tribeID].targetBuildingUID + && DAT_BuildingsState::instance.buildings[targetBuildingID].fireDuration == 0 + && MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::checkTribeActivityPercentages, this)( + tribeID, i, TRUE)) { + if (DAT_GameState::instance.mapAndTime.yearChanged != 0) + DAT_TribesState::instance.tribes[tribeID].targetBuildingID = 0; + DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown = 100; + continue; + } + + targetBuildingID + = MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::chooseRandomBuildingIDFromPlayersArrayOfBuildings, + this)(DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID_2Unk); + if (targetBuildingID != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::relayTribeInstruction, DAT_UnitsState::ptr)( + tribeID, OpenSHC::Map::Units::UIT_ATTACK_BUILDING, targetBuildingID, + DAT_BuildingsState::instance.buildings[targetBuildingID].uid, 0); + DAT_TribesState::instance.tribes[tribeID].targetBuildingID = targetBuildingID; + DAT_TribesState::instance.tribes[tribeID].targetBuildingUID + = DAT_BuildingsState::instance.buildings[targetBuildingID].uid; + DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown = 100; + continue; + } + DAT_TribesState::instance.tribes[tribeID].targetBuildingID = 0; + DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown = 0; + continue; + } + + if (tile == 0) + continue; + + int y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::canNavigateUnitsFromTileToTargetTile, this)( + tribeID, tile)) + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::commandUnitsToLocation, DAT_TribesState::ptr)( + tribeID, x, y, 0); + else + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToCampfire, this)(tribeID, playerID); + } + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b62540e14f..3dda967b17 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35501,7 +35501,7 @@ SHC_3BB0A8C1_0x004D2870 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D28B0 | 0.0% | Pending -SHC_3BB0A8C1_0x004D2A70 | 0.0% | Pending +SHC_3BB0A8C1_0x004D2A70 | 62.0% | Navigate branch polarity SHC_3BB0A8C1_0x004D2E00 | 100.0% | Reimplemented From bf32016cd2368083de8819760819057c0e2c526e Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:25:30 +0100 Subject: [PATCH 02/12] reimplement: SHC_3BB0A8C1_0x004D2A70 100.0% Reimplemented AICState::aiGiveRaidInstructions, from gynt's draft: ae6823b894, the commit before this one, is his file from upstream's reimpl/AICState (6a03e7d06b at 62.04%; before it 718b754100 at 60.3%, 5eb682129f at 58.57%, efe657dea6 at 30.02% and e88be70fdd 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 Co-Authored-By: Claude Fable 5.2 --- .../AI/AICState/aiGiveRaidInstructions.cpp | 68 ++++++++++++------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp b/src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp index 8dc2db3e40..73fa8d5a2e 100644 --- a/src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp +++ b/src/OpenSHC/AI/AICState/aiGiveRaidInstructions.cpp @@ -2,6 +2,7 @@ #include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/AI/AITypeA.hpp" #include "OpenSHC/AI/Tribes/AITribeIndex.hpp" #include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" #include "OpenSHC/Map/Units/UnitInstructionType.hpp" @@ -20,8 +21,9 @@ namespace AI { void AICState::aiGiveRaidInstructions(int playerID) { int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; - if (aiType == 0) + if (aiType == AITA_NULL) return; + // declared here: the original keeps this zero in a register for the aiType test and the resets below int i = 0; int aicIndex = aiType - 1; @@ -35,7 +37,7 @@ namespace AI { else if (DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown > 0) DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown--; - for (; i < 6; i++) { + for (; i < countof(DAT_SkirmishDefinedData::instance.TribeTypeID); ++i) { int tribeID = DAT_GameState::instance.playerDataArray[playerID] .aiTribeIDs[OpenSHC::AI::Tribes::AITI_DEF_KNIGHTS + i]; DAT_TribesState::instance.tribes[tribeID].unitStance = OpenSHC::Map::Units::Behavior::USE_AGGRESSIVE; @@ -47,6 +49,8 @@ namespace AI { continue; int buildingID = 0; + // the tile is computed after each lookup, not once per arm: the original keeps two copies and its arms + // jump into each other's int tile; if (DAT_GameState::instance.playerDataArray[playerID].requestStateUnk == 2 && MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, DAT_UnitsState::ptr)( @@ -57,15 +61,21 @@ namespace AI { buildingID = MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::selectRandomBuildingIDFromListOfBuildingTypes, this)(DAT_GameState::instance.playerDataArray[playerID].playerID_askerUnk, i); - if (buildingID == 0) + if (buildingID != 0) { + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } else { buildingID = MACRO_CALL_MEMBER( OpenSHC::AI::AICState_Func::selectBuildingFromAListOfBuildingTypes, this)(playerID); - if (buildingID == 0) - continue; - tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX - + DAT_ViewportRenderState::instance - .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] - .addXgetTile; + if (buildingID == 0) + continue; + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } } else if (DAT_GameState::instance.playerDataArray[playerID].someTotalDefenseTroopsRelatedCountdown == 0 && DAT_GameState::instance.playerDataArray[playerID].totalRaidingTroopsUnk < MACRO_CALL_MEMBER( OpenSHC::AI::AICState_Func::getCurrentDesiredAttackRaidUnitCount, this)(aicIndex, playerID)) { @@ -76,15 +86,21 @@ namespace AI { continue; buildingID = MACRO_CALL_MEMBER( OpenSHC::AI::AICState_Func::selectRandomBuildingIDFromListOfBuildingTypes, this)(playerID, i); - if (buildingID == 0) + if (buildingID != 0) { + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } else { buildingID = MACRO_CALL_MEMBER( OpenSHC::AI::AICState_Func::selectBuildingFromAListOfBuildingTypes, this)(playerID); - if (buildingID == 0) - continue; - tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX - + DAT_ViewportRenderState::instance - .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] - .addXgetTile; + if (buildingID == 0) + continue; + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } } else if (DAT_SkirmishDefinedData::instance.TribeTypeID[i] == OpenSHC::AI::Tribes::AITI_DEF_WALKING_RANGED || DAT_SkirmishDefinedData::instance.TribeTypeID[i] == OpenSHC::AI::Tribes::AITI_DEF_HORSE_ARCHERS) { if (DAT_GameState::instance.playerDataArray[playerID].raidRetargetDelayCounter != 0) @@ -95,16 +111,22 @@ namespace AI { buildingID = MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::selectRandomBuildingIDFromListOfBuildingTypes, this)(DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID_2Unk, i); - if (buildingID == 0) + if (buildingID != 0) { + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } else { buildingID = MACRO_CALL_MEMBER( OpenSHC::AI::AICState_Func::chooseRandomBuildingIDFromPlayersArrayOfBuildings, this)( DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID_2Unk); - if (buildingID == 0) - continue; - tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX - + DAT_ViewportRenderState::instance - .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] - .addXgetTile; + if (buildingID == 0) + continue; + tile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } } else { if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::checkTribeActivityPercentages, this)( tribeID, i, FALSE)) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3dda967b17..7d8fe10ce7 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35501,7 +35501,7 @@ SHC_3BB0A8C1_0x004D2870 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D28B0 | 0.0% | Pending -SHC_3BB0A8C1_0x004D2A70 | 62.0% | Navigate branch polarity +SHC_3BB0A8C1_0x004D2A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D2E00 | 100.0% | Reimplemented From 0ce3fee38292769baab9ff7e9d91c9d948858a6e Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:25:30 +0100 Subject: [PATCH 03/12] reimplement: SHC_3BB0A8C1_0x004CCD20 93.2% selectedTribeID reload in latc AICState::addUnitToSmallestBehaviourTypeTribe as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his 798598858 (2026-11-03) and 3334240e7 (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 --- .../addUnitToSmallestBehaviourTypeTribe.cpp | 86 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp diff --git a/src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp b/src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp new file mode 100644 index 0000000000..abe1f279c8 --- /dev/null +++ b/src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp @@ -0,0 +1,86 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/AI/AITypeA.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_SkirmishDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" + +namespace OpenSHC { +namespace AI { + + // FUNCTION: STRONGHOLDCRUSADER 0x004CCD20 + int AICState::addUnitToSmallestBehaviourTypeTribe( + undefined4 playerID, undefined4 unitID, undefined4 aiUnitBehaviourType) + { + int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; + if (aiType == AITA_NULL) + return 0; + + int aicIndex = aiType - 1; + int firstSlot = DAT_SkirmishDefinedData::instance.AIUnitBehaviourTypeMapping[aiUnitBehaviourType - 10]; + int selectedTribeID = 0; + int selectedSlot = 0; + int smallestSize = 1000; + int groupsCount; + switch (firstSlot) { + case 15: + groupsCount = this->aics[aicIndex].AttUnitPatrolGroupsCount; + break; + case 186: + groupsCount = this->aics[aicIndex].AttUnitBackupGroupsCount; + break; + case 190: + groupsCount = this->aics[aicIndex].AttUnitSiegeDefGroupsCount; + break; + case 192: + groupsCount = this->aics[aicIndex].AttMainGroupsCount; + break; + default: + groupsCount = 1; + break; + } + if (groupsCount < 1) + groupsCount = 1; + + if (groupsCount == 1) { + int tribeID = DAT_GameState::instance.playerDataArray[playerID].aiTribeIDs[firstSlot]; + if (tribeID != 0 + && DAT_TribesState::instance.tribes[tribeID].uid + == DAT_GameState::instance.playerDataArray[playerID].aiTribeUIDs[firstSlot]) + return tribeID; + selectedTribeID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeForPlayer, DAT_TribesState::ptr)(playerID); + } else { + // groupsCount becomes the end slot: the original keeps the bound in the register of groupsCount + groupsCount = groupsCount + firstSlot; + for (int slot = firstSlot; slot < groupsCount; slot++) { + int tribeID = DAT_GameState::instance.playerDataArray[playerID].aiTribeIDs[slot]; + if (tribeID == 0 + || DAT_TribesState::instance.tribes[tribeID].uid + != DAT_GameState::instance.playerDataArray[playerID].aiTribeUIDs[slot]) { + selectedTribeID = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::createTribeForPlayer, DAT_TribesState::ptr)(playerID); + selectedSlot = slot; + break; + } + if (DAT_TribesState::instance.tribes[tribeID].size < smallestSize) { + smallestSize = DAT_TribesState::instance.tribes[tribeID].size; + selectedTribeID = tribeID; + selectedSlot = slot; + } + } + // the store below indexes by firstSlot; copying here (rather than setting selectedSlot in the + // single-tribe path) keeps the empty-loop edge on the shared exit, as in the original + firstSlot = selectedSlot; + } + if (selectedTribeID <= 0) + return 0; + DAT_GameState::instance.playerDataArray[playerID].aiTribeIDs[firstSlot] = selectedTribeID; + DAT_GameState::instance.playerDataArray[playerID].aiTribeUIDs[firstSlot] + = DAT_TribesState::instance.tribes[selectedTribeID].uid; + return selectedTribeID; + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 7d8fe10ce7..c5c22c4934 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -34893,7 +34893,7 @@ SHC_3BB0A8C1_0x004CCAF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CCBE0 | 0.0% | Pending -SHC_3BB0A8C1_0x004CCD20 | 0.0% | Pending +SHC_3BB0A8C1_0x004CCD20 | 93.2% | selectedTribeID reload in latc SHC_3BB0A8C1_0x004CCEDC | 0.0% | Pending From 1a1f105901693a3c64c1e5ae1104aea0f3080dc8 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:25:30 +0100 Subject: [PATCH 04/12] reimplement: SHC_3BB0A8C1_0x004CCD20 100.0% Reimplemented AICState::addUnitToSmallestBehaviourTypeTribe, from gynt's draft: 0ce3fee382, the commit before this one, is his file from upstream's reimpl/AICState (3334240e77 at 93.19%; before it 7985988584 and a013994e97 at 80.13%, fc674a09cb at 66.03% and 61e841afd9 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 Co-Authored-By: Claude Fable 5.2 --- .../addUnitToSmallestBehaviourTypeTribe.cpp | 33 +++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp b/src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp index abe1f279c8..da32d8029e 100644 --- a/src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp +++ b/src/OpenSHC/AI/AICState/addUnitToSmallestBehaviourTypeTribe.cpp @@ -2,6 +2,8 @@ #include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/AI/AITypeA.hpp" +#include "OpenSHC/AI/AIUnitBehaviourType.hpp" +#include "OpenSHC/AI/Tribes/AITribeIndex.hpp" #include "OpenSHC/Globals/DAT_GameState.hpp" #include "OpenSHC/Globals/DAT_SkirmishDefinedData.hpp" @@ -19,22 +21,23 @@ namespace AI { return 0; int aicIndex = aiType - 1; - int firstSlot = DAT_SkirmishDefinedData::instance.AIUnitBehaviourTypeMapping[aiUnitBehaviourType - 10]; + int firstSlot + = DAT_SkirmishDefinedData::instance.AIUnitBehaviourTypeMapping[aiUnitBehaviourType - AIUBT_ENGINEERING]; int selectedTribeID = 0; int selectedSlot = 0; int smallestSize = 1000; int groupsCount; switch (firstSlot) { - case 15: + case OpenSHC::AI::Tribes::AITI_BREACH_BASIC_05_01: groupsCount = this->aics[aicIndex].AttUnitPatrolGroupsCount; break; - case 186: + case OpenSHC::AI::Tribes::AITI_BREACH_08_01: groupsCount = this->aics[aicIndex].AttUnitBackupGroupsCount; break; - case 190: + case OpenSHC::AI::Tribes::AITI_BREACH_10_01: groupsCount = this->aics[aicIndex].AttUnitSiegeDefGroupsCount; break; - case 192: + case OpenSHC::AI::Tribes::AITI_BREACH_11_01: groupsCount = this->aics[aicIndex].AttMainGroupsCount; break; default: @@ -44,18 +47,19 @@ namespace AI { if (groupsCount < 1) groupsCount = 1; + int resultTribeID = 0; if (groupsCount == 1) { int tribeID = DAT_GameState::instance.playerDataArray[playerID].aiTribeIDs[firstSlot]; if (tribeID != 0 && DAT_TribesState::instance.tribes[tribeID].uid == DAT_GameState::instance.playerDataArray[playerID].aiTribeUIDs[firstSlot]) return tribeID; - selectedTribeID + resultTribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeForPlayer, DAT_TribesState::ptr)(playerID); } else { // groupsCount becomes the end slot: the original keeps the bound in the register of groupsCount - groupsCount = groupsCount + firstSlot; - for (int slot = firstSlot; slot < groupsCount; slot++) { + groupsCount += firstSlot; + for (int slot = firstSlot; slot < groupsCount; ++slot) { int tribeID = DAT_GameState::instance.playerDataArray[playerID].aiTribeIDs[slot]; if (tribeID == 0 || DAT_TribesState::instance.tribes[tribeID].uid @@ -71,16 +75,17 @@ namespace AI { selectedSlot = slot; } } - // the store below indexes by firstSlot; copying here (rather than setting selectedSlot in the - // single-tribe path) keeps the empty-loop edge on the shared exit, as in the original + // the loop's choice is copied out here, tribe and slot alike: the original reads both back from the + // stack on the loop's exit, and the stores below index by firstSlot + resultTribeID = selectedTribeID; firstSlot = selectedSlot; } - if (selectedTribeID <= 0) + if (resultTribeID <= 0) return 0; - DAT_GameState::instance.playerDataArray[playerID].aiTribeIDs[firstSlot] = selectedTribeID; + DAT_GameState::instance.playerDataArray[playerID].aiTribeIDs[firstSlot] = resultTribeID; DAT_GameState::instance.playerDataArray[playerID].aiTribeUIDs[firstSlot] - = DAT_TribesState::instance.tribes[selectedTribeID].uid; - return selectedTribeID; + = DAT_TribesState::instance.tribes[resultTribeID].uid; + return resultTribeID; } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index c5c22c4934..7f813a0ab5 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -34893,7 +34893,7 @@ SHC_3BB0A8C1_0x004CCAF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CCBE0 | 0.0% | Pending -SHC_3BB0A8C1_0x004CCD20 | 93.2% | selectedTribeID reload in latc +SHC_3BB0A8C1_0x004CCD20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CCEDC | 0.0% | Pending From 1497254e0b27853104f542eaf20f7ff001faf749 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:25:31 +0100 Subject: [PATCH 05/12] reimplement: SHC_3BB0A8C1_0x004D49E0 81.7% Pools 1 instead of 0 in ebp AICState::updateAIPlayerState as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his dc584e2a41 (2026-11-03, 81.73%) and his style commit 5e329403da (2026-11-07, his newest on the file), with his status line. An older version by the sourcehold account, a150748b80 (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 --- .../AI/AICState/updateAIPlayerState.cpp | 283 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/updateAIPlayerState.cpp diff --git a/src/OpenSHC/AI/AICState/updateAIPlayerState.cpp b/src/OpenSHC/AI/AICState/updateAIPlayerState.cpp new file mode 100644 index 0000000000..1583571ef0 --- /dev/null +++ b/src/OpenSHC/AI/AICState/updateAIPlayerState.cpp @@ -0,0 +1,283 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/AI/AIType.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace AI { + + using OpenSHC::AI::AIType; + using OpenSHC::WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x004D49E0 + void AICState::updateAIPlayerState(int playerID) + { + if (DAT_GameState::instance.playerDataArray[playerID].aiType == OpenSHC::AI::AIT_NULL) + return; + int _aicIndex = DAT_GameState::instance.playerDataArray[playerID].aiType - 1; + + // an ally asked for help and is still alive: drop everything and go to their keep + if (DAT_GameState::instance.playerDataArray[playerID].requestStateUnk != 1 + && DAT_GameState::instance.playerDataArray[playerID].requestStateUnk == 2 + && MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, DAT_UnitsState::ptr)( + DAT_GameState::instance.playerDataArray[playerID].playerID_askerUnk)) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::clearRallyFlagOnAllAIAttackTribes, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToPlayerIDsKeepIfPossible, this)( + playerID, DAT_GameState::instance.playerDataArray[playerID].playerID_askerUnk); + return; + } + + int _aiPlayerState = DAT_GameState::instance.playerDataArray[playerID].aiPlayerState; + if (_aiPlayerState == 0) { + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiShouldAttackOrWaitForTeamCoordination, this)( + playerID)) { + // proceed with attack + DAT_GameState::instance.playerDataArray[playerID].attackTicker = 0; + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 1; + DAT_GameState::instance.playerDataArray[playerID].unitPatrolRecommandRelatedDelayValue = 0; + DAT_GameState::instance.playerDataArray[playerID].aiCoordinatedAttackPatience = 0; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setAttackCoordinationLevel, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setCurrentAttackStrength, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setCurrentAttackRaidParameter, this)(playerID); + ++DAT_GameState::instance + .playerDataArray[DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID] + .attackedByPlayerArrayUnk[playerID][0]; + } else { + // send own units back to keep or campfire + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiRetreatUnits, this)(playerID); + return; + } + } else if ((MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::hasNoTroopsOrAllAreDiggers, this)(playerID) + && _aiPlayerState != 9) + || (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker > 0 && _aiPlayerState <= 5) + || DAT_GameState::instance.playerDataArray[playerID].attackTicker >= 200) { + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 8; + } + + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::clearRallyFlagOnAllAIAttackTribes, this)(playerID); + + if (!MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, DAT_UnitsState::ptr)( + DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID)) { + // attacked player is dead + if (DAT_GameState::instance.playerDataArray[playerID].aiPlayerState <= 3) { + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 9; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::disbandAIPlayerSiegeUnits, this)(playerID); + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::removeSiegeBuildings, DAT_BuildingsState::ptr)( + DAT_GameState::instance.playerDataArray[playerID].currentAttackWave, playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::addEngineersToTribe, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiReassignTunnelersToTribe, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::findAttackTribePositionAndPathToDefenses, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::makeUnitsGoDefensiveAndBackToSomeLocation, this)( + playerID); + } else if (DAT_GameState::instance.playerDataArray[playerID].aiPlayerState < 7) { + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 7; + } + } + + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::clearOutdatedAITribes, this)(playerID); + + _aiPlayerState = DAT_GameState::instance.playerDataArray[playerID].aiPlayerState; + + if (_aiPlayerState == 1) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::recreateEngineersTribe, this)(playerID); + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 2; + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::initializeGatePathfindingForOwner, + DAT_BuildingsState::ptr)(playerID); + // computes the first siege location for units + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::computeEnemyKeepApproachTile, this)(playerID); + if (DAT_GameState::instance.playerDataArray[playerID].shortestDistanceTile > 0) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::moveAttackTribesToLocations, this)(playerID); + return; + } + // no approach tile found: back to idle + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; + return; + } + + if (_aiPlayerState == 2) { + // gathering: wait until enough of the attack force has rallied + if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiChoiceAttForceRallyPercentage, this)(playerID)) + return; + // send the attacking units to move closer + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 3; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::useAITribe_0x12_toPlaceSiegeTentsAndAssignEngineers, this)( + playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::recomputeAIPathCostToAttackedKeep, this)(playerID, 1); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::computeAttackPossibilities, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setNextMoveLocationForUnits, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::giveMoveCommandToAttackTribes, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::useAITribe_0xe_toPlaceTunnels, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::removeOrganismsAndSetMoveDestinationPairs, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::commandFourTribesToMove, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::playTauntToAttackedPlayerBikFromPlayer, this)(playerID); + return; + } + + if (_aiPlayerState == 3) { + // send the attacking units closer to the castle + DAT_GameState::instance.playerDataArray[playerID].attackTicker++; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::moveAttackingUnitsToNextRallyPoint, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendAITribe_0xf_toAttackPatrol, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandSiegeEngineTribes, this)(playerID); + if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::processAttForceRallyPercentage, this)(playerID)) + return; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::recomputeAIPathCostToAttackedKeep, this)(playerID, 0); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::computeAttackPossibilities, this)(playerID); + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 4; + DAT_GameState::instance.playerDataArray[playerID].attackAssaultDelayRelatedValue = 0; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandTribe12AggressiveApproach, this)(playerID); + return; + } + + if (_aiPlayerState == 4) { + // siege: wait AttAssaultDelay (skipped without siege engines) before the assault + DAT_GameState::instance.playerDataArray[playerID].attackTicker++; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::moveAttackingUnitsToNextRallyPoint, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendAITribe_0xf_toAttackPatrol, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandSiegeEngineTribes, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendTribeToAttack, this)(playerID); + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::countSiegeEngineUnits, this)(playerID) <= 0) + DAT_GameState::instance.playerDataArray[playerID].attackAssaultDelayRelatedValue = 100; + DAT_GameState::instance.playerDataArray[playerID].attackAssaultDelayRelatedValue++; + if (DAT_GameState::instance.playerDataArray[playerID].attackAssaultDelayRelatedValue + <= this->aics[_aicIndex].AttAssaultDelay) + return; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::recomputeAIPathCostToAttackedKeep, this)(playerID, 0); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::computeAttackPossibilities, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_GameState::instance.playerDataArray[playerID].shortestDistanceX, + DAT_GameState::instance.playerDataArray[playerID].shortestDistanceY, + OpenSHC::DE::SHCDE::FX_ARMY_CHARGE); + if (!MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::canNavigateFromKeepToKeep, + DAT_GameState::ptr)(playerID, DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID)) { + // castle is closed: attack the walls + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 5; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandTribe12AggressiveApproach, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandTribe11StandGroundToGate, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandTribe13DefensiveWave, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::recomputeAIPathCostToAttackedKeep, this)(playerID, 0); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::computeAttackPossibilities, this)(playerID); + DAT_GameState::instance.playerDataArray[playerID].unknownCounter01 = 0; + return; + } + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 6; + return; + } + + if (_aiPlayerState == 5) { + // attack walls + if (DAT_GameState::instance.playerDataArray[playerID].shortestDistanceTile <= 0) + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; + DAT_GameState::instance.playerDataArray[playerID].unknownCounter01++; + DAT_GameState::instance.playerDataArray[playerID].attackTicker++; + DAT_GameState::instance.playerDataArray[playerID].someResourceCounter = 0; + if (DAT_GameState::instance.playerDataArray[playerID].unknownCounter01 >= 4) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::recomputeAIPathCostToAttackedKeep, this)(playerID, 0); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::computeAttackPossibilities, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandWallAssaultTribes, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiCommandSiegeEngineTribes, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setTribe0x11ToAggressiveAndAttackIfConditionMet, this)( + playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setEightTribe0xc0TribesToAggressiveAndAttack, this)( + playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendTribeToAttack, this)(playerID); + DAT_GameState::instance.playerDataArray[playerID].unknownCounter01 = 0; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::removeOrganismsAndSetMoveDestinationPairs, this)( + playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::commandFourTribesToMove, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setTribe0xCtoAggressiveAndAttack, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setTribe0xbToAggressiveAndAttack, this)(playerID); + } + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendAITribe_0xf_toAttackPatrol, this)(playerID); + if (!MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::canNavigateFromKeepToKeep, + DAT_GameState::ptr)(playerID, DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID) + && !MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::canUnitsNavigateFromKeepToKeep, + DAT_GameState::ptr)(playerID, DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID)) + return; + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 6; + return; + } + + if (_aiPlayerState == 6) { + // enemy castle breached + if (DAT_GameState::instance.playerDataArray[playerID].shortestDistanceTile <= 0) + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; + DAT_GameState::instance.playerDataArray[playerID].attackTicker++; + int _attackedPlayerID = DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID; + DAT_GameState::instance.playerDataArray[playerID].someResourceCounter = 0; + MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::calculateAttackVectorsToCampFireOfPlayer, + DAT_GameState::ptr)(_attackedPlayerID); + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToAttackBreachedCastle, this)(playerID)) + return; + if (MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::canNavigateFromKeepToKeep, + DAT_GameState::ptr)(playerID, DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID)) + return; + if (MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::canUnitsNavigateFromKeepToKeep, + DAT_GameState::ptr)(playerID, DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID)) + return; + // the breach got closed again: back to attacking the walls + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 5; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::recomputeAIPathCostToAttackedKeep, this)(playerID, 0); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::computeAttackPossibilities, this)(playerID); + DAT_GameState::instance.playerDataArray[playerID].unknownCounter01 = 0; + return; + } + + if (_aiPlayerState == 7) { + // target died mid-attack: linger for AttUnitPatrolRecommandDelay, then retreat + DAT_GameState::instance.playerDataArray[playerID].unitPatrolRecommandRelatedDelayValue++; + if (DAT_GameState::instance.playerDataArray[playerID].unitPatrolRecommandRelatedDelayValue + <= this->aics[_aicIndex].AttUnitPatrolRecommandDelay) + return; + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 9; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::disbandAIPlayerSiegeUnits, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::removeSiegeBuildings, + DAT_BuildingsState::ptr)(DAT_GameState::instance.playerDataArray[playerID].currentAttackWave, playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::addEngineersToTribe, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiReassignTunnelersToTribe, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::findAttackTribePositionAndPathToDefenses, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::makeUnitsGoDefensiveAndBackToSomeLocation, this)(playerID); + return; + } + + if (_aiPlayerState == 8) { + // retreat from attack + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 9; + DAT_GameState::instance.playerDataArray[playerID].attackTicker = 0; + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::disbandAIPlayerSiegeUnits, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::Map::Buildings::BuildingsState_Func::removeSiegeBuildings, + DAT_BuildingsState::ptr)(DAT_GameState::instance.playerDataArray[playerID].currentAttackWave, playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::addEngineersToTribe, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiReassignTunnelersToTribe, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::findAttackTribePositionAndPathToDefenses, this)(playerID); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::makeUnitsGoDefensiveAndBackToSomeLocation, this)(playerID); + if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker <= 0) + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::playAnger1BikFromPlayer, this)(playerID); + return; + } + + if (_aiPlayerState == 9) { + // retreating: back to idle once the units are home (or after 20 ticks) + DAT_GameState::instance.playerDataArray[playerID].attackTicker++; + if (DAT_GameState::instance.playerDataArray[playerID].attackTicker < 20 + && !MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::percentageNonMovingTribesGTEAICSpecified, this)( + playerID) + && !MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::hasNoTroopsOrAllAreDiggers, this)(playerID)) + return; + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; + } + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 7f813a0ab5..789f007a01 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35559,7 +35559,7 @@ SHC_3BB0A8C1_0x004D48F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D4940 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D49E0 | 0.0% | Pending +SHC_3BB0A8C1_0x004D49E0 | 81.7% | Pools 1 instead of 0 in ebp SHC_3BB0A8C1_0x004D5010 | 100.0% | Reimplemented From ee55bc7df18a819a38981a9fd67445c3a3166a95 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:25:31 +0100 Subject: [PATCH 06/12] reimplement: SHC_3BB0A8C1_0x004D49E0 100.0% Reimplemented AICState::updateAIPlayerState, from gynt's draft: 1497254e0b, the commit before this one, is his file from upstream's reimpl/AICState (dc584e2a41 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, a150748b80 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 Co-Authored-By: Claude Fable 5.2 --- .../AI/AICState/updateAIPlayerState.cpp | 30 +++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/OpenSHC/AI/AICState/updateAIPlayerState.cpp b/src/OpenSHC/AI/AICState/updateAIPlayerState.cpp index 1583571ef0..e33c3afe95 100644 --- a/src/OpenSHC/AI/AICState/updateAIPlayerState.cpp +++ b/src/OpenSHC/AI/AICState/updateAIPlayerState.cpp @@ -6,7 +6,6 @@ #include "OpenSHC/Map/Units/UnitsState.func.hpp" #include "OpenSHC/AI/AIType.hpp" #include "OpenSHC/DE/SHCDE/eSFX.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_BuildingsState.hpp" #include "OpenSHC/Globals/DAT_GameState.hpp" @@ -16,9 +15,6 @@ namespace OpenSHC { namespace AI { - using OpenSHC::AI::AIType; - using OpenSHC::WindowsHelper::Enums::BOOLEnum; - // FUNCTION: STRONGHOLDCRUSADER 0x004D49E0 void AICState::updateAIPlayerState(int playerID) { @@ -27,6 +23,7 @@ namespace AI { int _aicIndex = DAT_GameState::instance.playerDataArray[playerID].aiType - 1; // an ally asked for help and is still alive: drop everything and go to their keep + // fixme: != 1 is redundant beside == 2, but the original makes both compares if (DAT_GameState::instance.playerDataArray[playerID].requestStateUnk != 1 && DAT_GameState::instance.playerDataArray[playerID].requestStateUnk == 2 && MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, DAT_UnitsState::ptr)( @@ -49,9 +46,9 @@ namespace AI { MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setAttackCoordinationLevel, this)(playerID); MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setCurrentAttackStrength, this)(playerID); MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::setCurrentAttackRaidParameter, this)(playerID); - ++DAT_GameState::instance - .playerDataArray[DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID] - .attackedByPlayerArrayUnk[playerID][0]; + // read into a local first: without it the scratch registers of this increment come out swapped + int attackedPlayerID = DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID; + ++DAT_GameState::instance.playerDataArray[attackedPlayerID].attackedByPlayerArrayUnk[playerID][0]; } else { // send own units back to keep or campfire MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::aiRetreatUnits, this)(playerID); @@ -270,12 +267,21 @@ namespace AI { if (_aiPlayerState == 9) { // retreating: back to idle once the units are home (or after 20 ticks) DAT_GameState::instance.playerDataArray[playerID].attackTicker++; - if (DAT_GameState::instance.playerDataArray[playerID].attackTicker < 20 - && !MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::percentageNonMovingTribesGTEAICSpecified, this)( - playerID) - && !MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::hasNoTroopsOrAllAreDiggers, this)(playerID)) + // reset at each exit, not after one combined test: written once, the compiler keeps 1 in a register + // instead of 0 + if (DAT_GameState::instance.playerDataArray[playerID].attackTicker >= 20) { + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; return; - DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; + } + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::percentageNonMovingTribesGTEAICSpecified, this)( + playerID)) { + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; + return; + } + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::hasNoTroopsOrAllAreDiggers, this)(playerID)) { + DAT_GameState::instance.playerDataArray[playerID].aiPlayerState = 0; + return; + } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 789f007a01..c32d06fb60 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35559,7 +35559,7 @@ SHC_3BB0A8C1_0x004D48F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D4940 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D49E0 | 81.7% | Pools 1 instead of 0 in ebp +SHC_3BB0A8C1_0x004D49E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D5010 | 100.0% | Reimplemented From be4686a2b99597bd0196eaccd4408053963d7f18 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:25:31 +0100 Subject: [PATCH 07/12] reimplement: SHC_3BB0A8C1_0x004D1A00 93.2% this reload, regalloc differs AICState::decideOnTribeAttackLocation as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his 59f74d1385 (2026-11-07, 93.20%; before it 92ea87c3f8 at 93.01%, eb33d1d625 and 123ac02a51 at 91.60%, 6a93c2c25b at 68.80% and f156123649 at 58.00%), d76377dbc2 and his style commit 5e329403da (his newest on the file), with his status line. The next commit takes it to 100. Co-Authored-By: Claude Fable 5.2 --- .../AICState/decideOnTribeAttackLocation.cpp | 169 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp diff --git a/src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp b/src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp new file mode 100644 index 0000000000..22375266b8 --- /dev/null +++ b/src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp @@ -0,0 +1,169 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Random/RNG.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace AI { + + using OpenSHC::Map::Units::UnitInstructionType; + + // FUNCTION: STRONGHOLDCRUSADER 0x004D1A00 + void AICState::decideOnTribeAttackLocation(uint tribeID) + { + if (MACRO_CALL_MEMBER(AICState_Func::checkTribeActivityPercentages, this)(tribeID, FALSE, TRUE)) { + DAT_TribesState::instance.tribes[tribeID].counter = 0; + return; + } + + int targetPlayer = DAT_TribesState::instance.tribes[tribeID].targetPlayer_outpost; + if (DAT_TribesState::instance.tribes[tribeID].randomNumber == 0) { + DAT_TribesState::instance.tribes[tribeID].randomNumber = SEC_RNG::instance.currentNumber2; + MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + } + + DAT_TribesState::instance.tribes[tribeID].counter++; + if (DAT_TribesState::instance.tribes[tribeID].counter + == DAT_TribesState::instance.tribes[tribeID].randomNumber % 6 + 7) { + DAT_TribesState::instance.tribes[tribeID].counter = 0; + byte moatWasHit = 0; + int x = 0; + int y = 0; + int unitID = MACRO_CALL_MEMBER( + OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, DAT_TribesState::ptr)(tribeID, 0); + // the callee returns a bool in al; the generated header declares BOOLEnum + if (unitID != 0 + && (byte)MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::pathFindingToAttackCastleIncludingMoat, + DAT_PathFindingState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, targetPlayer, + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, &moatWasHit, &x, + &y)) { + if (moatWasHit != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::relayTribeInstruction, DAT_UnitsState::ptr)( + tribeID, (UnitInstructionType)35, + DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x, 0, 0); + return; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::relayTribeInstruction, DAT_UnitsState::ptr)( + tribeID, OpenSHC::Map::Units::UIT_FILL_MOAT, x, y, 1000); + return; + } + } + + if (DAT_TribesState::instance.tribes[tribeID].tribeSubtype1 != 182 + && DAT_TribesState::instance.tribes[tribeID].tribeSubtype1 != 185) { + if (MACRO_CALL_MEMBER(AICState_Func::checkTribeActivityPercentages, this)(tribeID, FALSE, FALSE)) + return; + + int targetBuildingID = DAT_TribesState::instance.tribes[tribeID].targetBuildingID; + if (targetBuildingID != 0 + && DAT_BuildingsState::instance.buildings[targetBuildingID].uid + == DAT_TribesState::instance.tribes[tribeID].targetBuildingUID + && DAT_BuildingsState::instance.buildings[targetBuildingID].fireDuration == 0 + && MACRO_CALL_MEMBER(AICState_Func::checkTribeActivityPercentages, this)(tribeID, FALSE, TRUE)) { + if (DAT_GameState::instance.mapAndTime.yearChanged != 0) + DAT_TribesState::instance.tribes[tribeID].targetBuildingID = 0; + return; + } + + int buildingID + = MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::selectARandomBuildingOwnedByPlayer, + DAT_GameState::ptr)(targetPlayer); + if (buildingID != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::relayTribeInstruction, DAT_UnitsState::ptr)( + tribeID, OpenSHC::Map::Units::UIT_ATTACK_BUILDING, buildingID, + DAT_BuildingsState::instance.buildings[buildingID].uid, 0); + DAT_TribesState::instance.tribes[tribeID].targetBuildingID = buildingID; + DAT_TribesState::instance.tribes[tribeID].targetBuildingUID + = DAT_BuildingsState::instance.buildings[buildingID].uid; + return; + } + + DAT_TribesState::instance.tribes[tribeID].targetBuildingID = 0; + int destinationX = DAT_GameState::instance.playerDataArray[targetPlayer] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry; + int destinationY = DAT_GameState::instance.playerDataArray[targetPlayer] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_CAMPGROUND] + .yEntry; + if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::canNavigateUnitsFromTileToTargetTile, this)(tribeID, + DAT_ViewportRenderState::instance.translationMatrix[destinationY].addXgetTile + destinationX)) + return; + + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::commandUnitsToLocation, DAT_TribesState::ptr)( + tribeID, destinationX, destinationY, 1); + return; + } + + int buildingID = MACRO_CALL_MEMBER(OpenSHC::Game::GameStateStructures_Func::selectARandomBuildingOwnedByPlayer, + DAT_GameState::ptr)(targetPlayer); + int destinationTile; + if (buildingID != 0) { + // fixme: the original reads this once with movsx into an int; an int copy does that but lets MSVC fold + // both campground arms into one, which the original keeps apart. The reference keeps the arms apart. + short const& targetUnitIDRef = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + if (targetUnitIDRef != 0) { + MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::pathfindingForAttacksUnk, + DAT_PathFindingState::ptr)(tribeID, buildingID, 1, + (short)DAT_TileMapState::instance.PathConnectionLayer[DAT_UnitsState::instance + .units[DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID] + .tile], + targetPlayer); + if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper == 0) + destinationTile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_GameState::instance.playerDataArray[targetPlayer] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_CAMPGROUND] + .yEntry] + .addXgetTile + + DAT_GameState::instance.playerDataArray[targetPlayer] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry; + else + destinationTile = DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1; + } else { + destinationTile = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY] + .addXgetTile; + } + } else { + destinationTile = DAT_ViewportRenderState::instance + .translationMatrix[DAT_GameState::instance.playerDataArray[targetPlayer] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_CAMPGROUND] + .yEntry] + .addXgetTile + + DAT_GameState::instance.playerDataArray[targetPlayer] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry; + } + + if (destinationTile == 0) + return; + + int destinationY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[destinationTile]; + int destinationX + = destinationTile - DAT_ViewportRenderState::instance.translationMatrix[destinationY].addXgetTile; + if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::canNavigateUnitsFromTileToTargetTile, this)( + tribeID, destinationTile)) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToCampfire, this)(tribeID, targetPlayer); + return; + } + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::commandUnitsToLocation, DAT_TribesState::ptr)( + tribeID, destinationX, destinationY, 1); + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index c32d06fb60..8919d9b2c0 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35473,7 +35473,7 @@ SHC_3BB0A8C1_0x004D18B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D1950 | 0.0% | Pending -SHC_3BB0A8C1_0x004D1A00 | 0.0% | Pending +SHC_3BB0A8C1_0x004D1A00 | 93.2% | this reload, regalloc differs SHC_3BB0A8C1_0x004D1D60 | 0.0% | Pending From 2290a24d51c79bef8a9de535319c1632e399f35b Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:25:32 +0100 Subject: [PATCH 08/12] reimplement: SHC_3BB0A8C1_0x004D1A00 100.0% Reimplemented AICState::decideOnTribeAttackLocation, from gynt's draft: be4686a2b9, the commit before this one, is his file from upstream's reimpl/AICState (59f74d1385 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 Co-Authored-By: Claude Fable 5.2 --- .../AICState/decideOnTribeAttackLocation.cpp | 29 ++++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp b/src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp index 22375266b8..f34cc506b1 100644 --- a/src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp +++ b/src/OpenSHC/AI/AICState/decideOnTribeAttackLocation.cpp @@ -45,7 +45,7 @@ namespace AI { int y = 0; int unitID = MACRO_CALL_MEMBER( OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, DAT_TribesState::ptr)(tribeID, 0); - // the callee returns a bool in al; the generated header declares BOOLEnum + // the original tests only al after this call if (unitID != 0 && (byte)MACRO_CALL_MEMBER( OpenSHC::Map::Navigation::PathFindingState_Func::pathFindingToAttackCastleIncludingMoat, @@ -100,8 +100,11 @@ namespace AI { int destinationY = DAT_GameState::instance.playerDataArray[targetPlayer] .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_CAMPGROUND] .yEntry; - if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::canNavigateUnitsFromTileToTargetTile, this)(tribeID, - DAT_ViewportRenderState::instance.translationMatrix[destinationY].addXgetTile + destinationX)) + // in a local of its own: written inside the call, it loses the match + int campgroundTile + = DAT_ViewportRenderState::instance.translationMatrix[destinationY].addXgetTile + destinationX; + if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::canNavigateUnitsFromTileToTargetTile, this)( + tribeID, campgroundTile)) return; MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::commandUnitsToLocation, DAT_TribesState::ptr)( @@ -113,15 +116,12 @@ namespace AI { DAT_GameState::ptr)(targetPlayer); int destinationTile; if (buildingID != 0) { - // fixme: the original reads this once with movsx into an int; an int copy does that but lets MSVC fold - // both campground arms into one, which the original keeps apart. The reference keeps the arms apart. - short const& targetUnitIDRef = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; - if (targetUnitIDRef != 0) { + int targetUnitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + if (targetUnitID != 0) { MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::pathfindingForAttacksUnk, DAT_PathFindingState::ptr)(tribeID, buildingID, 1, - (short)DAT_TileMapState::instance.PathConnectionLayer[DAT_UnitsState::instance - .units[DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID] - .tile], + (short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[targetUnitID].tile], targetPlayer); if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper == 0) destinationTile = DAT_ViewportRenderState::instance @@ -157,13 +157,14 @@ namespace AI { int destinationY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[destinationTile]; int destinationX = destinationTile - DAT_ViewportRenderState::instance.translationMatrix[destinationY].addXgetTile; - if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::canNavigateUnitsFromTileToTargetTile, this)( + // tested this way round, so that sendUnitsToCampfire is the last statement: the other way loses the match + if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::canNavigateUnitsFromTileToTargetTile, this)( tribeID, destinationTile)) { - MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToCampfire, this)(tribeID, targetPlayer); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::commandUnitsToLocation, DAT_TribesState::ptr)( + tribeID, destinationX, destinationY, 1); return; } - MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::commandUnitsToLocation, DAT_TribesState::ptr)( - tribeID, destinationX, destinationY, 1); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToCampfire, this)(tribeID, targetPlayer); } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 8919d9b2c0..a30bce513b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35473,7 +35473,7 @@ SHC_3BB0A8C1_0x004D18B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D1950 | 0.0% | Pending -SHC_3BB0A8C1_0x004D1A00 | 93.2% | this reload, regalloc differs +SHC_3BB0A8C1_0x004D1A00 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D1D60 | 0.0% | Pending From f96142bda70517f0316d5582f579c4ce26bdfd12 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:45:48 +0100 Subject: [PATCH 09/12] reimplement: SHC_3BB0A8C1_0x004CDB20 66.0% Loop register allocation AICState::chooseRandomBuildingIDFromPlayersArrayOfBuildings as gynt has it on upstream's reimpl/AICState (5e329403da), unchanged: his 2521fe3302 (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 --- ...mBuildingIDFromPlayersArrayOfBuildings.cpp | 46 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp diff --git a/src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp b/src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp new file mode 100644 index 0000000000..d47f671845 --- /dev/null +++ b/src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp @@ -0,0 +1,46 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_SkirmishDefinedData.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace AI { + + // FUNCTION: STRONGHOLDCRUSADER 0x004CDB20 + int AICState::chooseRandomBuildingIDFromPlayersArrayOfBuildings(int playerID) + { + int rng = SEC_RNG::instance.currentNumber2 & 7; + int chosenBuildingID = 0; + int chosenPriority = 46; + short* buildings = DAT_GameState::instance.playerDataArray[playerID].top100TargetableBuildings; + for (int i = 0; i < DAT_GameState::instance.playerDataArray[playerID].top100TargetableBuildingsTracker; i++) { + int buildingID = *buildings++; + if (buildingID == 0) + continue; + + for (int j = 0; j < chosenPriority; j++) { + if (rng <= 1) { + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + != DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet3[j]) + continue; + } else if (rng <= 3) { + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + != DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet2[j]) + continue; + } else { + if (DAT_BuildingsState::instance.buildings[buildingID].buildingType + != DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet1[j]) + continue; + } + + chosenPriority = j; + chosenBuildingID = buildingID; + } + } + return chosenBuildingID; + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index a30bce513b..812cb8076e 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35287,7 +35287,7 @@ SHC_3BB0A8C1_0x004CDA50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CDAB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004CDB20 | 0.0% | Pending +SHC_3BB0A8C1_0x004CDB20 | 66.0% | Loop register allocation SHC_3BB0A8C1_0x004CDBC0 | 100.0% | Reimplemented From 7cb6d3a8f97f40f6065e969d424e68f385663e41 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:46:51 +0100 Subject: [PATCH 10/12] reimplement: SHC_3BB0A8C1_0x004CDB20 100.0% Reimplemented AICState::chooseRandomBuildingIDFromPlayersArrayOfBuildings, from gynt's draft: f96142bda7, the commit before this one, is his file from upstream's reimpl/AICState (2521fe3302 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 Co-Authored-By: Claude Fable 5.2 --- ...mBuildingIDFromPlayersArrayOfBuildings.cpp | 33 ++++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp b/src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp index d47f671845..65a872ac4e 100644 --- a/src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp +++ b/src/OpenSHC/AI/AICState/chooseRandomBuildingIDFromPlayersArrayOfBuildings.cpp @@ -11,32 +11,35 @@ namespace AI { // FUNCTION: STRONGHOLDCRUSADER 0x004CDB20 int AICState::chooseRandomBuildingIDFromPlayersArrayOfBuildings(int playerID) { - int rng = SEC_RNG::instance.currentNumber2 & 7; + int const rng = SEC_RNG::instance.currentNumber2 & 7; int chosenBuildingID = 0; - int chosenPriority = 46; - short* buildings = DAT_GameState::instance.playerDataArray[playerID].top100TargetableBuildings; - for (int i = 0; i < DAT_GameState::instance.playerDataArray[playerID].top100TargetableBuildingsTracker; i++) { - int buildingID = *buildings++; + int chosenPriority = countof(DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet1); + for (int i = 0; i < DAT_GameState::instance.playerDataArray[playerID].top100TargetableBuildingsTracker; ++i) { + int const buildingID = DAT_GameState::instance.playerDataArray[playerID].top100TargetableBuildings[i]; if (buildingID == 0) continue; - for (int j = 0; j < chosenPriority; j++) { + // The stores stay in each arm: one shared copy behind the three tests loses the match. + for (int j = 0; j < chosenPriority; ++j) { if (rng <= 1) { if (DAT_BuildingsState::instance.buildings[buildingID].buildingType - != DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet3[j]) - continue; + == DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet3[j]) { + chosenPriority = j; + chosenBuildingID = buildingID; + } } else if (rng <= 3) { if (DAT_BuildingsState::instance.buildings[buildingID].buildingType - != DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet2[j]) - continue; + == DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet2[j]) { + chosenPriority = j; + chosenBuildingID = buildingID; + } } else { if (DAT_BuildingsState::instance.buildings[buildingID].buildingType - != DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet1[j]) - continue; + == DAT_SkirmishDefinedData::instance.BuildingTargetPrioritySet1[j]) { + chosenPriority = j; + chosenBuildingID = buildingID; + } } - - chosenPriority = j; - chosenBuildingID = buildingID; } } return chosenBuildingID; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 812cb8076e..0796facbb3 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35287,7 +35287,7 @@ SHC_3BB0A8C1_0x004CDA50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CDAB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004CDB20 | 66.0% | Loop register allocation +SHC_3BB0A8C1_0x004CDB20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CDBC0 | 100.0% | Reimplemented From 475baec3368da0d576f651e54a45ac1e88853031 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:51:39 +0100 Subject: [PATCH 11/12] reimplement: SHC_3BB0A8C1_0x004D4340 81.2% playerID in edi, not ebp AICState::sendWallPatrolUnitTribesToAIVLocations as gynt has it on upstream's reimpl/AICState (5e329403da), unchanged: his 207c302ab1 (2026-11-07, 81.22%, "playerID in edi, not ebp"; before it f0e44ad3ea at 77.19%) and his style commit 5e329403da (his newest on the file), with his status line. The next commit takes it to 100. Co-Authored-By: Claude Fable 5.2 --- ...sendWallPatrolUnitTribesToAIVLocations.cpp | 95 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp diff --git a/src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp b/src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp new file mode 100644 index 0000000000..d7b801aff4 --- /dev/null +++ b/src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp @@ -0,0 +1,95 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/AI/AIType.hpp" +#include "OpenSHC/AI/AITypeInt.hpp" +#include "OpenSHC/AI/AIVUnitType.hpp" +#include "OpenSHC/AI/AIVUnitTypeInt.hpp" +#include "OpenSHC/AI/Tribes/AIVUnitTypeMaxLocationPair.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_SkirmishDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" + +namespace OpenSHC { +namespace AI { + + using OpenSHC::AI::AIType; + using OpenSHC::AI::AITypeInt; + using OpenSHC::AI::AIVUnitType; + using OpenSHC::AI::AIVUnitTypeInt; + using OpenSHC::AI::Tribes::AIVUnitTypeMaxLocationPair; + using OpenSHC::Map::Units::Behavior::UnitStanceEnum; + using OpenSHC::WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x004D4340 + void AICState::sendWallPatrolUnitTribesToAIVLocations(int playerID) + { + int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; + if (aiType == OpenSHC::AI::AIT_NULL) { + return; + } + + int aicIndex = aiType - 1; + BOOLEnum enclosed = MACRO_CALL_MEMBER( + OpenSHC::Game::GameStateStructures_Func::checkKeepEnclosed, DAT_GameState::ptr)(playerID); + + for (int i = 0; i < 3; i++) { + // declared before j: the declaration order decides which of the two gets ebp (measured) + int patrolGroupIndex = 0; + int j = 0; + int count = DAT_GameState::instance.playerDataArray[playerID].aivUnitLocationSlotLocationCount + [DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits[i].aivUnitType]; + int groups = count; + if (count > this->aics[aicIndex].defWallPatrolGroups) { + groups = this->aics[aicIndex].defWallPatrolGroups; + } + if (groups <= 0) { + continue; + } + + int stepSize = count / groups; + if (stepSize <= 0) { + stepSize = 1; + } + + for (; j < groups; j++, patrolGroupIndex += stepSize) { + int tribeID + = DAT_GameState::instance.playerDataArray[playerID] + .aiTribeIDs[DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits[i].tribeArrayOffset + + j]; + if (tribeID == 0) { + continue; + } + if (DAT_TribesState::instance.tribes[tribeID].uid + != DAT_GameState::instance.playerDataArray[playerID] + .aiTribeUIDs[DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits[i].tribeArrayOffset + + j]) { + continue; + } + + int slot + = (DAT_GameState::instance.playerDataArray[playerID].defWallPatrolRallyTimeHits + patrolGroupIndex) + % count; + if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker > 0 && !enclosed) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToCampfire, this)(tribeID, playerID); + } else { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendTribeToAIVLocationSlot, this)(tribeID, + (AIVUnitType)DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits[i].aivUnitType, + slot); + } + + if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker > 0 && !enclosed) { + DAT_TribesState::instance.tribes[tribeID].unitStance = OpenSHC::Map::Units::Behavior::USE_DEFENSIVE; + } else { + DAT_TribesState::instance.tribes[tribeID].unitStance + = OpenSHC::Map::Units::Behavior::USE_AGGRESSIVE; + } + } + } + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 0796facbb3..90b5733d47 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35539,7 +35539,7 @@ SHC_3BB0A8C1_0x004D4130 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D4220 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D4340 | 0.0% | Pending +SHC_3BB0A8C1_0x004D4340 | 81.2% | playerID in edi, not ebp SHC_3BB0A8C1_0x004D4400 | 0.0% | Pending From a07eb1c5c7589e64c4463a49932424de8b2de215 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:52:42 +0100 Subject: [PATCH 12/12] reimplement: SHC_3BB0A8C1_0x004D4340 100.0% Reimplemented AICState::sendWallPatrolUnitTribesToAIVLocations, from gynt's draft: 475baec336, the commit before this one, is his file from upstream's reimpl/AICState (5e329403da, his style pass over 207c302ab1 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 Co-Authored-By: Claude Fable 5.2 --- ...sendWallPatrolUnitTribesToAIVLocations.cpp | 31 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp b/src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp index d7b801aff4..1725cac700 100644 --- a/src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp +++ b/src/OpenSHC/AI/AICState/sendWallPatrolUnitTribesToAIVLocations.cpp @@ -2,10 +2,7 @@ #include "OpenSHC/Game/GameStateStructures.func.hpp" #include "OpenSHC/AI/AIType.hpp" -#include "OpenSHC/AI/AITypeInt.hpp" #include "OpenSHC/AI/AIVUnitType.hpp" -#include "OpenSHC/AI/AIVUnitTypeInt.hpp" -#include "OpenSHC/AI/Tribes/AIVUnitTypeMaxLocationPair.hpp" #include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" #include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" @@ -16,32 +13,26 @@ namespace OpenSHC { namespace AI { - using OpenSHC::AI::AIType; - using OpenSHC::AI::AITypeInt; using OpenSHC::AI::AIVUnitType; - using OpenSHC::AI::AIVUnitTypeInt; - using OpenSHC::AI::Tribes::AIVUnitTypeMaxLocationPair; - using OpenSHC::Map::Units::Behavior::UnitStanceEnum; using OpenSHC::WindowsHelper::Enums::BOOLEnum; // FUNCTION: STRONGHOLDCRUSADER 0x004D4340 void AICState::sendWallPatrolUnitTribesToAIVLocations(int playerID) { - int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; + int const aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; if (aiType == OpenSHC::AI::AIT_NULL) { return; } - int aicIndex = aiType - 1; - BOOLEnum enclosed = MACRO_CALL_MEMBER( + int const aicIndex = aiType - 1; + BOOLEnum const enclosed = MACRO_CALL_MEMBER( OpenSHC::Game::GameStateStructures_Func::checkKeepEnclosed, DAT_GameState::ptr)(playerID); - for (int i = 0; i < 3; i++) { - // declared before j: the declaration order decides which of the two gets ebp (measured) - int patrolGroupIndex = 0; + for (int i = 0; i < countof(DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits); ++i) { + // Starts here, not in the header of its for loop: there it loses the match. int j = 0; - int count = DAT_GameState::instance.playerDataArray[playerID].aivUnitLocationSlotLocationCount - [DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits[i].aivUnitType]; + int const count = DAT_GameState::instance.playerDataArray[playerID].aivUnitLocationSlotLocationCount + [DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits[i].aivUnitType]; int groups = count; if (count > this->aics[aicIndex].defWallPatrolGroups) { groups = this->aics[aicIndex].defWallPatrolGroups; @@ -55,8 +46,8 @@ namespace AI { stepSize = 1; } - for (; j < groups; j++, patrolGroupIndex += stepSize) { - int tribeID + for (; j < groups; ++j) { + int const tribeID = DAT_GameState::instance.playerDataArray[playerID] .aiTribeIDs[DAT_SkirmishDefinedData::instance.MaxAIVLocationForPatrolUnits[i].tribeArrayOffset + j]; @@ -70,8 +61,8 @@ namespace AI { continue; } - int slot - = (DAT_GameState::instance.playerDataArray[playerID].defWallPatrolRallyTimeHits + patrolGroupIndex) + int const slot + = (DAT_GameState::instance.playerDataArray[playerID].defWallPatrolRallyTimeHits + j * stepSize) % count; if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker > 0 && !enclosed) { MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sendUnitsToCampfire, this)(tribeID, playerID); diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 90b5733d47..0a41db46cd 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35539,7 +35539,7 @@ SHC_3BB0A8C1_0x004D4130 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D4220 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D4340 | 81.2% | playerID in edi, not ebp +SHC_3BB0A8C1_0x004D4340 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D4400 | 0.0% | Pending