From 368a5f00bdd31c14f161e28cf27ebfc24d468b8d Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:25:32 +0100 Subject: [PATCH 1/8] reimplement: SHC_3BB0A8C1_0x004D3320 97.2% choice in ebx, not ebp AICState::updateAIStrengthState as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his 06102f215 and 49ee0959c (2026-11-07, his newest commit on the file), with his status line. The next commit records what the remaining difference is. Co-Authored-By: Claude Fable 5.2 --- .../AI/AICState/updateAIStrengthState.cpp | 217 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/updateAIStrengthState.cpp diff --git a/src/OpenSHC/AI/AICState/updateAIStrengthState.cpp b/src/OpenSHC/AI/AICState/updateAIStrengthState.cpp new file mode 100644 index 0000000000..ccc4ed64aa --- /dev/null +++ b/src/OpenSHC/AI/AICState/updateAIStrengthState.cpp @@ -0,0 +1,217 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Random/RNG.func.hpp" +#include "OpenSHC/AI/AINervousDestroyBuildingCategory.hpp" +#include "OpenSHC/AI/AINervousDestroyBuildingCategoryInt.hpp" +#include "OpenSHC/AI/AIRecruitUnitChoice.hpp" +#include "OpenSHC/AI/AIStrengthType.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Resources/ResourceType.hpp" +#include "OpenSHC/Game/TrailType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace AI { + + using OpenSHC::AI::AINervousDestroyBuildingCategory; + using OpenSHC::AI::AINervousDestroyBuildingCategoryInt; + using OpenSHC::AI::AIRecruitUnitChoice; + using OpenSHC::AI::AIStrengthType; + using OpenSHC::Game::GameMode2; + using OpenSHC::Game::TrailType; + using OpenSHC::WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x004D3320 + void AICState::updateAIStrengthState(int playerID, int aiRecruitUnitChoicePreference) + { + int _aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; + if (_aiType == 0) + return; + + if ((char)aiRecruitUnitChoicePreference == 0) { + if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SKIRMISH_AND_MULTIPLAYER) + return; + if (DAT_GameCore::instance.isSkirmishTrail != TRUE) + return; + if (DAT_GameCore::instance.currentTrailType != OpenSHC::Game::TT_EXTREME) + return; + } + + int _aicIndex = _aiType - 1; + + int _currentGold + = DAT_GameState::instance.playerDataArray[playerID].currentResources[OpenSHC::Game::Resources::RT_GOLD]; + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState = 0; + if (_currentGold < 200) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState = 0; + else if (_currentGold < 2000) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState = 1; + else + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState = 2; + + int _currentPopulation = DAT_GameState::instance.playerDataArray[playerID].currentPopulation; + if (_currentPopulation >= 15) { + if (_currentPopulation < 40) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState += 1; + else + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState += 2; + } + + int _totalFood = DAT_GameState::instance.playerDataArray[playerID].totalFood; + if (_totalFood >= 20) { + if (_totalFood < 200) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState += 1; + else + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState += 2; + } + + int _totalTroopCount = DAT_GameState::instance.playerDataArray[playerID].totalAttackTroops + + DAT_GameState::instance.playerDataArray[playerID].totalRaidingTroopsUnk + + DAT_GameState::instance.playerDataArray[playerID].totalDefensiveTroopsUnk; + if (_totalTroopCount >= 8) { + if (_totalTroopCount < 40) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState += 2; + else + DAT_GameState::instance.playerDataArray[playerID].aiStrengthState += 4; + } + + if (DAT_GameState::instance.playerDataArray[playerID].aiStrengthState <= 2) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling = OpenSHC::AI::AIST_WEAK; + else if (DAT_GameState::instance.playerDataArray[playerID].aiStrengthState <= 7) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling = OpenSHC::AI::AIST_DEFAULT; + else + DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling = OpenSHC::AI::AIST_STRONG; + + // while the first AIV pause is still running, the AI always feels weak + if (DAT_GameState::instance.playerDataArray[playerID].aivCurrentPauseIndex == 1 + && DAT_GameState::instance.playerDataArray[playerID] + .aivPauses[DAT_GameState::instance.playerDataArray[playerID].aivCurrentPauseIndex] + > 0) + DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling = OpenSHC::AI::AIST_WEAK; + + // the three recruitProb* triples are laid out as Default, Weak, Strong, matching AIStrengthType + int _recruitProbDefensive = (&this->aics[_aicIndex] + .recruitProbDefDefault)[DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling]; + int _recruitProbAttack = (&this->aics[_aicIndex] + .recruitProbAttackDefault)[DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling]; + int _recruitProbRaid = (&this->aics[_aicIndex] + .recruitProbRaidDefault)[DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling]; + int _totalRecruitProb = _recruitProbAttack + _recruitProbRaid + _recruitProbDefensive; + + int _aiRecruitUnitChoice; + if (_totalRecruitProb != 0) { + int _random = SEC_RNG::instance.currentNumber2 % _totalRecruitProb; + MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + + if ((int)DAT_GameCore::instance.mapTimeInTicks < 4800) + _aiRecruitUnitChoice = OpenSHC::AI::AIRUC_DEFENSIVE; + else if (_random < _recruitProbDefensive) + _aiRecruitUnitChoice = OpenSHC::AI::AIRUC_DEFENSIVE; + else if (_random < _recruitProbRaid + _recruitProbDefensive) + _aiRecruitUnitChoice = OpenSHC::AI::AIRUC_RAIDING; + else + _aiRecruitUnitChoice = OpenSHC::AI::AIRUC_ATTACKING; + } else { + _aiRecruitUnitChoice = aiRecruitUnitChoicePreference; + } + + if (_aiRecruitUnitChoice == OpenSHC::AI::AIRUC_RAIDING) { + if (DAT_GameState::instance.playerDataArray[playerID].totalRaidingTroopsUnk >= MACRO_CALL_MEMBER( + OpenSHC::AI::AICState_Func::getCurrentDesiredAttackRaidUnitCount, this)(_aicIndex, playerID)) + _aiRecruitUnitChoice = OpenSHC::AI::AIRUC_ATTACKING; + } else if (_aiRecruitUnitChoice == OpenSHC::AI::AIRUC_DEFENSIVE) { + if (DAT_GameState::instance.playerDataArray[playerID].totalDefensiveTroopsUnk + >= this->aics[_aicIndex].DefTotal) + _aiRecruitUnitChoice = OpenSHC::AI::AIRUC_ATTACKING; + } + + if (DAT_GameState::instance.playerDataArray[playerID].aiRecruitUnitChoiceState != _aiRecruitUnitChoice) + DAT_GameState::instance.playerDataArray[playerID].aiRecruitUnitChoiceState = _aiRecruitUnitChoice; + + if ((char)aiRecruitUnitChoicePreference == 0) + return; + + if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker > 0) { + if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::teamIsWeakRelativeToEnemy, this)(playerID, TRUE)) { + DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker = 0; + return; + } + + int _gold + = DAT_GameState::instance.playerDataArray[playerID].currentResources[OpenSHC::Game::Resources::RT_GOLD]; + DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling = OpenSHC::AI::AIST_WEAK; + DAT_GameState::instance.playerDataArray[playerID].aiRecruitUnitChoiceState = OpenSHC::AI::AIRUC_DEFENSIVE; + + if (_gold > 500) { + if (_gold > 2500) { + DAT_GameState::instance.playerDataArray[playerID].aiStateCounter++; + if ((DAT_GameState::instance.playerDataArray[playerID].aiStateCounter & 1) + && DAT_GameState::instance.playerDataArray[playerID].relativeStrengthOfTeamComparedToEnemy + <= 20) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::shareGoldAmongTeamMembers, this)( + playerID, _gold / 10); + } + } + } else if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker <= 7) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_FEAR_FACTOR_BUILDINGS); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_WORKSHOPS_AND_RELIGIOUS_BUILDINGS); + } else if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker <= 5) { + // unreachable in the original as well: anything <= 5 is already handled by the <= 7 branch + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_RESOURCE_BUILDINGS); + } + + if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker > 1) + DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker--; + return; + } + + int _destroyChoice = DAT_GameState::instance.playerDataArray[playerID].aiBuildingDestroyChoiceTracker; + if (_destroyChoice > 0) { + if (!MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::hasNotEnoughSupplies, this)(playerID)) { + DAT_GameState::instance.playerDataArray[playerID].aiBuildingDestroyChoiceTracker = 0; + return; + } + + DAT_GameState::instance.playerDataArray[playerID].resourcesToAcquireArray[2] = 20; + DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling = OpenSHC::AI::AIST_WEAK; + DAT_GameState::instance.playerDataArray[playerID].aiRecruitUnitChoiceState = OpenSHC::AI::AIRUC_DEFENSIVE; + + if (_destroyChoice == 8) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_FEAR_FACTOR_BUILDINGS); + } else if (_destroyChoice == 7) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_WORKSHOPS_AND_RELIGIOUS_BUILDINGS); + } else if (_destroyChoice == 6) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_GATEHOUSES_TOWERS_GUILDS); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_BARRACKS_MERCPOST_STABLES); + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_WATER_BUILDINGS); + } else if (_destroyChoice == 5) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_RESOURCE_BUILDINGS); + } else if (_destroyChoice == 4) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::destroyAnyBuildingInCategory, this)( + playerID, OpenSHC::AI::AINDBC_FARMS); + } + + DAT_GameState::instance.playerDataArray[playerID].aiBuildingDestroyChoiceTracker--; + } else if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::teamIsWeakRelativeToEnemy, this)(playerID, FALSE)) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::playNervous2BikFromPlayer, this)(playerID); + DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker = 8; + DAT_GameState::instance.playerDataArray[playerID].aiStateCounter = 0; + } else if (MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::hasNotEnoughSupplies, this)(playerID)) { + DAT_GameState::instance.playerDataArray[playerID].aiBuildingDestroyChoiceTracker = 8; + } + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b62540e14f..7970ebb054 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35511,7 +35511,7 @@ SHC_3BB0A8C1_0x004D2F80 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D30E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D3320 | 0.0% | Pending +SHC_3BB0A8C1_0x004D3320 | 97.2% | choice in ebx, not ebp SHC_3BB0A8C1_0x004D3780 | 100.0% | Reimplemented From 61981119eb5807a82f5589df34d5a5ffa056aca8 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:25:32 +0100 Subject: [PATCH 2/8] reimplement: SHC_3BB0A8C1_0x004D3320 97.2% one variable's register: ebp in the original, ebx in ours AICState::updateAIStrengthState, gynt's version from the commit before this one (368a5f00bd: his 49ee0959c on upstream's reimpl/AICState, unchanged). reccmp reads 97.2, as for his file: the 326 instructions of both sides are the same instructions in the same order with the same memory operands and jump targets, and they differ only in the register of one variable, the recruit choice between the random draw and its store: ebp in the original, ebx in ours, on nine lines (0x004D34BB to 0x004D3524). It is recorded at reccmp's number, not as 100: whether a difference in register names alone counts as a match (#414) is not settled. quick_diff reads 0.9724 with five hunks, all of them register names only. On top of gynt's file, measured the same: the seven using lines that nothing uses and the two includes that nothing uses (AINervousDestroyBuildingCategoryInt.hpp, BOOLEnum.hpp) are gone, aiType is tested against AITA_NULL, resourcesToAcquireArray is indexed with RT_WOOD, and three comments say why a line has its shape (the low byte of the preference, the comparison with TRUE). Co-authored-by: Gynt Co-Authored-By: Claude Fable 5.2 --- .../AI/AICState/updateAIStrengthState.cpp | 19 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/OpenSHC/AI/AICState/updateAIStrengthState.cpp b/src/OpenSHC/AI/AICState/updateAIStrengthState.cpp index ccc4ed64aa..197fb86bc6 100644 --- a/src/OpenSHC/AI/AICState/updateAIStrengthState.cpp +++ b/src/OpenSHC/AI/AICState/updateAIStrengthState.cpp @@ -2,13 +2,12 @@ #include "OpenSHC/Random/RNG.func.hpp" #include "OpenSHC/AI/AINervousDestroyBuildingCategory.hpp" -#include "OpenSHC/AI/AINervousDestroyBuildingCategoryInt.hpp" #include "OpenSHC/AI/AIRecruitUnitChoice.hpp" #include "OpenSHC/AI/AIStrengthType.hpp" +#include "OpenSHC/AI/AITypeA.hpp" #include "OpenSHC/Game/GameMode2.hpp" #include "OpenSHC/Game/Resources/ResourceType.hpp" #include "OpenSHC/Game/TrailType.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_GameCore.hpp" #include "OpenSHC/Globals/DAT_GameState.hpp" @@ -17,24 +16,18 @@ namespace OpenSHC { namespace AI { - using OpenSHC::AI::AINervousDestroyBuildingCategory; - using OpenSHC::AI::AINervousDestroyBuildingCategoryInt; - using OpenSHC::AI::AIRecruitUnitChoice; - using OpenSHC::AI::AIStrengthType; - using OpenSHC::Game::GameMode2; - using OpenSHC::Game::TrailType; - using OpenSHC::WindowsHelper::Enums::BOOLEnum; - // FUNCTION: STRONGHOLDCRUSADER 0x004D3320 void AICState::updateAIStrengthState(int playerID, int aiRecruitUnitChoicePreference) { int _aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; - if (_aiType == 0) + if (_aiType == AITA_NULL) return; + // the original tests only the low byte of this parameter if ((char)aiRecruitUnitChoicePreference == 0) { if (DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_SKIRMISH_AND_MULTIPLAYER) return; + // compared with TRUE: the original tests isSkirmishTrail == 1 if (DAT_GameCore::instance.isSkirmishTrail != TRUE) return; if (DAT_GameCore::instance.currentTrailType != OpenSHC::Game::TT_EXTREME) @@ -132,6 +125,7 @@ namespace AI { if (DAT_GameState::instance.playerDataArray[playerID].aiRecruitUnitChoiceState != _aiRecruitUnitChoice) DAT_GameState::instance.playerDataArray[playerID].aiRecruitUnitChoiceState = _aiRecruitUnitChoice; + // the original tests only the low byte of this parameter if ((char)aiRecruitUnitChoicePreference == 0) return; @@ -179,7 +173,8 @@ namespace AI { return; } - DAT_GameState::instance.playerDataArray[playerID].resourcesToAcquireArray[2] = 20; + DAT_GameState::instance.playerDataArray[playerID].resourcesToAcquireArray[OpenSHC::Game::Resources::RT_WOOD] + = 20; DAT_GameState::instance.playerDataArray[playerID].aiStrengthFeeling = OpenSHC::AI::AIST_WEAK; DAT_GameState::instance.playerDataArray[playerID].aiRecruitUnitChoiceState = OpenSHC::AI::AIRUC_DEFENSIVE; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 7970ebb054..c960dba4bf 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35511,7 +35511,7 @@ SHC_3BB0A8C1_0x004D2F80 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D30E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D3320 | 97.2% | choice in ebx, not ebp +SHC_3BB0A8C1_0x004D3320 | 97.2% | one variable's register: ebp in the original, ebx in ours SHC_3BB0A8C1_0x004D3780 | 100.0% | Reimplemented From da9605a4c119e3844312152f9d3e05161fcacac6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:25:32 +0100 Subject: [PATCH 3/8] reimplement: SHC_3BB0A8C1_0x004D2000 99.7% Loop latch reload order AICState::aiCreateSiegeUnits as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his 31aa9391d2 (2026-11-03, 99.69%), d76377dbc2 and his style commit 5e329403da (2026-11-07, his newest on the file), with his status line. The next commit records what the remaining difference is. Co-Authored-By: Claude Fable 5.2 --- .../AI/AICState/aiCreateSiegeUnits.cpp | 251 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp diff --git a/src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp b/src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp new file mode 100644 index 0000000000..f83f07a093 --- /dev/null +++ b/src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp @@ -0,0 +1,251 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/AI/AIType.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_SkirmishDefinedData.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" + +namespace OpenSHC { +namespace AI { + + using OpenSHC::Commands::MappersEnum; + using OpenSHC::Map::Buildings::BuildingType; + using OpenSHC::Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x004D2000 + void AICState::aiCreateSiegeUnits(int playerID) + { + if (DAT_GameState::instance.playerDataArray[playerID].aiType == OpenSHC::AI::AIT_NULL) + return; + int aicIndex = DAT_GameState::instance.playerDataArray[playerID].aiType - 1; + int guildID = DAT_GameState::instance.playerDataArray[playerID] + .buildingEntryInfos[OpenSHC::Game::Player::Enums::BEIE_ENGINEERSGUILD] + .id; + if (guildID <= 0) + return; + if (DAT_BuildingsState::instance.buildings[guildID].buildingType != OpenSHC::Map::Buildings::BT_ENGINEERSGUILD) + return; + if (DAT_GameState::instance.playerDataArray[playerID].canStartSpending == 0) + return; + + for (int siegeIndex = 0; siegeIndex < 4; siegeIndex++) { + int siegeUnitType = DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex].unitType; + int locationCount = DAT_GameState::instance.playerDataArray[playerID].aivUnitLocationSlotLocationCount + [DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex].slot]; + int i = 0; + if (locationCount > 3) + locationCount = 3; + else if (locationCount <= 0) + continue; + + // Measured: the for-loop form of rule 9, `for (; i < locationCount; i++)` with the `i++` moved to + // the step, costs quick_diff 0.988 -> 0.696. The while stays, as in generateSiegeCreationInformation. + while (i < locationCount) { + int index = i; + i++; + int location = DAT_GameState::instance.playerDataArray[playerID].aivUnitLocationSlots + [DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex].slot][index]; + if (location <= 0) + continue; + + int building = DAT_TileMapState::instance.BuildingLayer[location]; + int unitID = DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .unitID; + int uid = DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .uid; + int buildingID = DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .buildingID; + + if (unitID != 0 && DAT_UnitsState::instance.units[unitID].uid != uid) { + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .unitID = 0; + DAT_GameState::instance.playerDataArray[playerID].aiSiegeCreationInformation[siegeIndex][index].uid + = 0; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .delay = 0; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .unknown = 0; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .buildingID = 0; + continue; + } + if (buildingID != 0 && DAT_BuildingsState::instance.buildings[buildingID].uid != uid) { + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .unitID = 0; + DAT_GameState::instance.playerDataArray[playerID].aiSiegeCreationInformation[siegeIndex][index].uid + = 0; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .delay = 0; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .unknown = 0; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .buildingID = 0; + continue; + } + + if (unitID == 0 && buildingID == 0) { + if (building != 0) { + if (siegeIndex >= 2) + continue; + if (DAT_BuildingsState::instance.buildings[building].buildingType + != OpenSHC::Map::Buildings::BT_TOWER4 + && DAT_BuildingsState::instance.buildings[building].buildingType + != OpenSHC::Map::Buildings::BT_TOWER5) + continue; + } else { + if (siegeIndex < 2) + continue; + } + + int requiredGold + = DAT_BuildingsState::instance + .buildingCosts[DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex] + .buildingType] + .requiredGold; + if (requiredGold + > DAT_GameState::instance.playerDataArray[playerID].currentResources[Game::Resources::RT_GOLD] + + this->aics[aicIndex].defSiegeEngineGoldThreshold) + continue; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .delay++; + if (DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .delay + < this->aics[aicIndex].defSiegeEngineBuildDelay) + continue; + + int y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[location]; + int x = location - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + if (siegeIndex < 2) { + int spawnedUnitID = MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::spawnUnit, + DAT_UnitsState::ptr)(playerID, playerID, x * 8 + 4, y * 8 + 4, + DAT_BuildingsState::instance.buildings[building].terrainHeightUnk, (UnitType)siegeUnitType); + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .unitID = spawnedUnitID; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .uid = DAT_UnitsState::instance.units[spawnedUnitID].uid; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .buildingID = 0; + MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::processPlacementResourceLossForBuildingType, + DAT_BuildingsState::ptr)(playerID, + (BuildingType)DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex] + .buildingType, + 0); + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::TileMapState_Func::placeBuilding, DAT_TileMapState::ptr)( + playerID, x, y, + (MappersEnum)(ushort)DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex] + .commandBuildingType, + 3, 15); + if (!DAT_TileMapState::instance.buildingPlacementFail) { + DAT_GameState::instance.playerDataArray[playerID].currentResources[Game::Resources::RT_GOLD] + -= requiredGold; + int placedBuildingID = DAT_TileMapState::instance.placedBuildingID; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .unitID = 0; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .uid = DAT_BuildingsState::instance.buildings[placedBuildingID].uid; + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .buildingID = placedBuildingID; + } + } + DAT_GameState::instance.playerDataArray[playerID] + .aiSiegeCreationInformation[siegeIndex][index] + .delay = 0; + return; + } + if (buildingID != 0) { + int requiredEngineers = MACRO_CALL_MEMBER( + OpenSHC::Map::Buildings::BuildingsState_Func::getRequiredEngineerCountForSiegeBuilding, + DAT_BuildingsState::ptr)(buildingID); + if (DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount == requiredEngineers) + continue; + if (DAT_GameState::instance.playerDataArray[playerID].engineerCountRelated >= requiredEngineers) { + int tribeID + = MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::assignRequiredIdleEngineersToNewTribe, + this)(playerID, requiredEngineers); + DAT_TribesState::instance.tribes[tribeID].tribeType = OpenSHC::AI::Tribes::AITT_ENGINEERS; + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType + = OpenSHC::Map::Units::STBT_0x410_SIEGE_EQUIPMENT_CONSTRUCTION; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::giveTribeAnInstruction, + DAT_TribesState::ptr)(tribeID, + OpenSHC::Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED, buildingID, + DAT_BuildingsState::instance.buildings[buildingID].uid, 0); + return; + } + DAT_GameState::instance.playerDataArray[playerID].isEngineerRequired = TRUE; + continue; + } + + int remainingEngineers = MACRO_CALL_MEMBER( + OpenSHC::Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(unitID); + if (remainingEngineers <= 0) + continue; + if (DAT_GameState::instance.playerDataArray[playerID].engineerCountRelated < remainingEngineers) { + DAT_GameState::instance.playerDataArray[playerID].isEngineerRequired = TRUE; + continue; + } + int tribeID = MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::assignRequiredIdleEngineersToNewTribe, + this)(playerID, remainingEngineers); + int unitArea + = (short) + DAT_TileMapState::instance.PathConnectionLayer[DAT_UnitsState::instance.units[unitID].tile]; + int targetUnitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + if (targetUnitID == 0) + continue; + int targetArea = (short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[targetUnitID].tile]; + if (MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, unitArea, targetArea, 0) + == 0) + continue; + DAT_TribesState::instance.tribes[tribeID].tribeType = OpenSHC::AI::Tribes::AITT_ENGINEERS; + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = OpenSHC::Map::Units::STBT_0x41c; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::relayTribeInstruction, DAT_UnitsState::ptr)( + tribeID, OpenSHC::Map::Units::UIT_MAN_SIEGE_EQUIPMENT, unitID, + DAT_UnitsState::instance.units[unitID].uid, 0); + return; + } + } + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index c960dba4bf..d2117f369b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35479,7 +35479,7 @@ SHC_3BB0A8C1_0x004D1D60 | 0.0% | Pending SHC_3BB0A8C1_0x004D1F00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D2000 | 0.0% | Pending +SHC_3BB0A8C1_0x004D2000 | 99.7% | Loop latch reload order SHC_3BB0A8C1_0x004D2080 | 0.0% | Pending From 6a0a71217c9d1b7cc4b303b52145be7d3d5c204a Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:25:33 +0100 Subject: [PATCH 4/8] reimplement: SHC_3BB0A8C1_0x004D2000 99.7% matches modulo TU symbol count AICState::aiCreateSiegeUnits, gynt's version from the commit before this one (da9605a4c1: his 31aa9391d2 at 99.69% on upstream's reimpl/AICState, with d76377dbc2 and 5e329403da). The source is his and it is right: the function matches modulo the translation unit's symbol count. reccmp reads 99.7 (normalized 95.5) as committed, and 100.0 (100.0) with 30 dummy `typedef short` lines after the last #include and nothing else changed; both numbers are from builds of this commit's file. Over the 64 possible shifts (0 to 63 such lines; quick_diff, one compile of this commit's file per shift) the function compiles to two code streams: shifts 20 to 31 and 36 to 44 give the original's code, the other 43, shift 0 among them, give the committed one. On gynt's file, before the countof bound and ++siegeIndex went in, the same two streams sit six shifts higher (26 to 37 and 42 to 50). The whole difference is the order of three reloads at the inner loop's latch: the original loads eax, esi, ecx (0x004D23AF, 0x004D23B3, 0x004D23B7), the other stream eax, ecx, esi. This is the effect gynt describes on #366 in tools/compiler-internals/msvc-backend.md ("The TU symbol count decides some register choices"): c2 files address constants into 64 buckets by a symbol number counted over the whole unit, and passes that walk the candidates in that order (here probably the one that places the reloads) come out differently when a header above the function gains or loses a declaration. As that note asks, nothing is padded here and the count is not steered: the next header edit above the function moves it again. The note is not on main yet, so the function is recorded at the 99.7 that reccmp reads there, not as 100. On top of gynt's file: the outer loop's bound is countof(SiegeEngineMetaInfoArray) with ++siegeIndex, which leaves both streams as they are and only moves the shifts at which each appears, as said above; and the comment on the while loop is one line without the scores (with the step in a for header the function reads 0.696 in quick_diff, his measurement). Co-authored-by: Gynt Co-Authored-By: Claude Fable 5.2 --- src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp | 6 +++--- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp b/src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp index f83f07a093..78442088d8 100644 --- a/src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp +++ b/src/OpenSHC/AI/AICState/aiCreateSiegeUnits.cpp @@ -46,7 +46,8 @@ namespace AI { if (DAT_GameState::instance.playerDataArray[playerID].canStartSpending == 0) return; - for (int siegeIndex = 0; siegeIndex < 4; siegeIndex++) { + for (int siegeIndex = 0; siegeIndex < countof(DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray); + ++siegeIndex) { int siegeUnitType = DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex].unitType; int locationCount = DAT_GameState::instance.playerDataArray[playerID].aivUnitLocationSlotLocationCount [DAT_SkirmishDefinedData::instance.SiegeEngineMetaInfoArray[siegeIndex].slot]; @@ -56,8 +57,7 @@ namespace AI { else if (locationCount <= 0) continue; - // Measured: the for-loop form of rule 9, `for (; i < locationCount; i++)` with the `i++` moved to - // the step, costs quick_diff 0.988 -> 0.696. The while stays, as in generateSiegeCreationInformation. + // a while: with the step in a for header the function loses the match while (i < locationCount) { int index = i; i++; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index d2117f369b..ee3a46b502 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35479,7 +35479,7 @@ SHC_3BB0A8C1_0x004D1D60 | 0.0% | Pending SHC_3BB0A8C1_0x004D1F00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004D2000 | 99.7% | Loop latch reload order +SHC_3BB0A8C1_0x004D2000 | 99.7% | matches modulo TU symbol count SHC_3BB0A8C1_0x004D2080 | 0.0% | Pending From c2cffa4b0adca1bd2b629d05be23fafbc33bc889 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 16:25:33 +0100 Subject: [PATCH 5/8] reimplement: SHC_3BB0A8C1_0x004D1D60 62.4% register permutation AICState::sellExcessGoods as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his 124830b114 (2026-11-03, 62.41%) and his style commit 5e329403da (2026-11-07, his newest on the file), with his status line. The next commit records what the remaining difference is. Co-Authored-By: Claude Fable 5.2 --- src/OpenSHC/AI/AICState/sellExcessGoods.cpp | 116 ++++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/sellExcessGoods.cpp diff --git a/src/OpenSHC/AI/AICState/sellExcessGoods.cpp b/src/OpenSHC/AI/AICState/sellExcessGoods.cpp new file mode 100644 index 0000000000..555af875a5 --- /dev/null +++ b/src/OpenSHC/AI/AICState/sellExcessGoods.cpp @@ -0,0 +1,116 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/AI/AIType.hpp" +#include "OpenSHC/AI/AITypeInt.hpp" +#include "OpenSHC/AI/Trading/AIResourceTradeCategory.hpp" +#include "OpenSHC/AI/Trading/AIResourceTradeCategoryInt.hpp" +#include "OpenSHC/AI/Trading/ResourceAICResourceMappingEntry.hpp" +#include "OpenSHC/Game/Player/PlayerData.hpp" +#include "OpenSHC/Game/Resources/ResourceType.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_SkirmishDefinedData.hpp" + +namespace OpenSHC { +namespace AI { + + using OpenSHC::AI::AIType; + using OpenSHC::AI::AITypeInt; + using OpenSHC::AI::AIC::AICSpecification; + using OpenSHC::AI::Trading::AIResourceTradeCategory; + using OpenSHC::AI::Trading::AIResourceTradeCategoryInt; + using OpenSHC::AI::Trading::ResourceAICResourceMappingEntry; + using OpenSHC::Game::Player::PlayerData; + using OpenSHC::Game::Resources::ResourceType; + + // FUNCTION: STRONGHOLDCRUSADER 0x004D1D60 + void AICState::sellExcessGoods(int playerID) + { + int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; + if (aiType == OpenSHC::AI::AIT_NULL) { + return; + } + + int aicIndex = aiType - 1; + // Measured: indexing currentResources and resourcesToAcquireArray through playerDataArray[playerID] + // instead of one flat offset from playerDataArray[0] costs quick_diff 0.659 -> 0.644. The original + // computes the offset once and reuses it for both arrays. + int playerResourceBase = playerID * (sizeof(PlayerData) / sizeof(int)); + for (int i = 0; i < 20; i++) { + ResourceType resourceType + = (ResourceType)DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].game; + int variance = this->aics[aicIndex].maxResourceVariance; + int resourceOffset = (playerResourceBase + resourceType) * sizeof(int); + + int amount; + if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_WOOD) { + amount = this->aics[aicIndex].maxWood; + } else if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_FOOD) { + amount = this->aics[aicIndex].maxFood; + } else if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_STONE) { + amount = this->aics[aicIndex].maxStone; + } else if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_WEAPONS) { + amount = this->aics[aicIndex].maxEquipment; + } else if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_ALE) { + amount = this->aics[aicIndex].maxBeer; + } else { + amount = this->aics[aicIndex].maxResourceOther; + } + amount += variance; + + if (DAT_GameState::instance.playerDataArray[playerID].aiNervousActionsTracker > 0 + && DAT_GameState::instance.playerDataArray[playerID].currentResources[Game::Resources::RT_GOLD] < 500) { + // Nervous and low on gold: keep a quarter of the food and up to 10000 weapons, sell everything else + if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_FOOD) { + amount = amount / 4; + } else if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_WEAPONS) { + amount = 10000; + } else { + amount = 0; + } + variance = 0; + } else if (DAT_GameState::instance.playerDataArray[playerID].popularity + < this->aics[aicIndex].criticalPopularity) { + // Critical popularity: keep all food, sell all weapons, keep a quarter of everything else + if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + != OpenSHC::AI::Trading::AIRTC_FOOD) { + if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic + == OpenSHC::AI::Trading::AIRTC_WEAPONS) { + amount = 0; + } else { + amount = amount / 4; + } + } + variance = 0; + } else { + amount += *( + int*)((char*)DAT_GameState::instance.playerDataArray[0].resourcesToAcquireArray + resourceOffset); + } + + // Resources the AIC always sells are kept at zero + for (int j = 0; j < 15; j++) { + if (resourceType == (&this->aics[aicIndex].sellResource01)[j]) { + amount = 0; + variance = 0; + break; + } + } + + if (*(int*)((char*)DAT_GameState::instance.playerDataArray[0].currentResources + resourceOffset) > amount) { + MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sellGoods, this)(playerID, resourceType, + ((int*)DAT_GameState::instance.playerDataArray[0] + .currentResources)[playerResourceBase + resourceType] + - amount + variance); + return; + } + } + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index ee3a46b502..8ec611b4de 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35475,7 +35475,7 @@ SHC_3BB0A8C1_0x004D1950 | 0.0% | Pending SHC_3BB0A8C1_0x004D1A00 | 0.0% | Pending -SHC_3BB0A8C1_0x004D1D60 | 0.0% | Pending +SHC_3BB0A8C1_0x004D1D60 | 62.4% | register permutation SHC_3BB0A8C1_0x004D1F00 | 100.0% | Reimplemented From f47fa7e7bd8c720a5acada719291769f2350936b Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 16:25:33 +0100 Subject: [PATCH 6/8] reimplement: SHC_3BB0A8C1_0x004D1D60 66.9% register names only: ebp/ebx and edi/esi swapped AICState::sellExcessGoods, from gynt's draft: c2cffa4b0a, the commit before this one, is his file from upstream's reimpl/AICState (124830b114 at 62.41%, "register permutation"). The function's structure, the flat offset for the two resource arrays and everything else are his. Only register names differ: reccmp reads 66.9, and the 132 instructions of both sides are the same instructions in the same order with the same memory operands, the stack slots included. What differs is two pairs of callee-saved registers, each swapped through the whole function (ebp with ebx, edi with esi): the player's int count, later the flat offset, is in ebp in the original and in ebx in ours; the player's offset, later the pointer into the sell list, in edi and esi; the AIC's address in ebx and ebp; the variance in esi and edi. The four pushes and pops stand at the same places under those names, and the jump targets are the same. It is recorded at reccmp's number, not as 100: whether a difference in register names alone counts as a match (#414) is not settled. What changed against the draft: the player's product is written inside the offset's expression instead of in a local declared before the loop. With the local, the loop head stores the resource type and forms the offset in the resource type's register, in another order than the original (one hunk of the diff is more than register names: quick_diff 0.6515); written inline, the offset is formed in the product's register as in the original and only register names are left (0.6667). Tried for the registers, on the draft's form (quick_diff): the sum's operands the other way round, the offset before the variance, the variance before the resource type, the base before aicIndex, a signed product (all 0.6515), the nervous count in a local (0.6107, 130 instructions), the call's amount read through the offset (0.6336, 130) and plain array indexing (0.6054, 129). Style, measured the same: the loop's bound is countof(AIResourceTradeAICMapping), the steps are ++i and ++j, the two quarterings are amount /= 4, the six using lines and three includes that nothing uses are gone, and the comment on the flat offset says what the original does without the scores. gynt's draft reads 62.4 in reccmp. Co-authored-by: Gynt Co-Authored-By: Claude Fable 5.2 --- src/OpenSHC/AI/AICState/sellExcessGoods.cpp | 27 ++++++--------------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/OpenSHC/AI/AICState/sellExcessGoods.cpp b/src/OpenSHC/AI/AICState/sellExcessGoods.cpp index 555af875a5..d3ed83ffff 100644 --- a/src/OpenSHC/AI/AICState/sellExcessGoods.cpp +++ b/src/OpenSHC/AI/AICState/sellExcessGoods.cpp @@ -1,10 +1,7 @@ #include "../AICState.func.hpp" #include "OpenSHC/AI/AIType.hpp" -#include "OpenSHC/AI/AITypeInt.hpp" #include "OpenSHC/AI/Trading/AIResourceTradeCategory.hpp" -#include "OpenSHC/AI/Trading/AIResourceTradeCategoryInt.hpp" -#include "OpenSHC/AI/Trading/ResourceAICResourceMappingEntry.hpp" #include "OpenSHC/Game/Player/PlayerData.hpp" #include "OpenSHC/Game/Resources/ResourceType.hpp" @@ -14,12 +11,6 @@ namespace OpenSHC { namespace AI { - using OpenSHC::AI::AIType; - using OpenSHC::AI::AITypeInt; - using OpenSHC::AI::AIC::AICSpecification; - using OpenSHC::AI::Trading::AIResourceTradeCategory; - using OpenSHC::AI::Trading::AIResourceTradeCategoryInt; - using OpenSHC::AI::Trading::ResourceAICResourceMappingEntry; using OpenSHC::Game::Player::PlayerData; using OpenSHC::Game::Resources::ResourceType; @@ -32,15 +23,13 @@ namespace AI { } int aicIndex = aiType - 1; - // Measured: indexing currentResources and resourcesToAcquireArray through playerDataArray[playerID] - // instead of one flat offset from playerDataArray[0] costs quick_diff 0.659 -> 0.644. The original - // computes the offset once and reuses it for both arrays. - int playerResourceBase = playerID * (sizeof(PlayerData) / sizeof(int)); - for (int i = 0; i < 20; i++) { + for (int i = 0; i < countof(DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping); ++i) { ResourceType resourceType = (ResourceType)DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].game; int variance = this->aics[aicIndex].maxResourceVariance; - int resourceOffset = (playerResourceBase + resourceType) * sizeof(int); + // one flat offset from playerDataArray[0] for currentResources and resourcesToAcquireArray: the + // original computes (the player's ints + the resource) * 4 once and uses it for both arrays + int resourceOffset = (playerID * (sizeof(PlayerData) / sizeof(int)) + resourceType) * sizeof(int); int amount; if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic @@ -68,7 +57,7 @@ namespace AI { // Nervous and low on gold: keep a quarter of the food and up to 10000 weapons, sell everything else if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic == OpenSHC::AI::Trading::AIRTC_FOOD) { - amount = amount / 4; + amount /= 4; } else if (DAT_SkirmishDefinedData::instance.AIResourceTradeAICMapping[i].aic == OpenSHC::AI::Trading::AIRTC_WEAPONS) { amount = 10000; @@ -85,7 +74,7 @@ namespace AI { == OpenSHC::AI::Trading::AIRTC_WEAPONS) { amount = 0; } else { - amount = amount / 4; + amount /= 4; } } variance = 0; @@ -95,7 +84,7 @@ namespace AI { } // Resources the AIC always sells are kept at zero - for (int j = 0; j < 15; j++) { + for (int j = 0; j < 15; ++j) { if (resourceType == (&this->aics[aicIndex].sellResource01)[j]) { amount = 0; variance = 0; @@ -106,7 +95,7 @@ namespace AI { if (*(int*)((char*)DAT_GameState::instance.playerDataArray[0].currentResources + resourceOffset) > amount) { MACRO_CALL_MEMBER(OpenSHC::AI::AICState_Func::sellGoods, this)(playerID, resourceType, ((int*)DAT_GameState::instance.playerDataArray[0] - .currentResources)[playerResourceBase + resourceType] + .currentResources)[playerID * (sizeof(PlayerData) / sizeof(int)) + resourceType] - amount + variance); return; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 8ec611b4de..94e983333c 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35475,7 +35475,7 @@ SHC_3BB0A8C1_0x004D1950 | 0.0% | Pending SHC_3BB0A8C1_0x004D1A00 | 0.0% | Pending -SHC_3BB0A8C1_0x004D1D60 | 62.4% | register permutation +SHC_3BB0A8C1_0x004D1D60 | 66.9% | register names only: ebp/ebx and edi/esi swapped SHC_3BB0A8C1_0x004D1F00 | 100.0% | Reimplemented From 5452f1bc651683aaaa986429c813b654994b7924 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 18 Nov 2026 17:56:13 +0100 Subject: [PATCH 7/8] reimplement: SHC_3BB0A8C1_0x004CDCF0 83.2% Pools 1 in a register AICState::aiShouldAttackOrWaitForTeamCoordination as gynt has it on upstream's reimpl/AICState (5e329403d), unchanged: his f4967de51a (2026-11-03, 83.25%) and his style commit 5e329403da (his newest on the file), with his status line. The next commit works on it. Co-Authored-By: Claude Fable 5.2 --- ...iShouldAttackOrWaitForTeamCoordination.cpp | 79 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp diff --git a/src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp b/src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp new file mode 100644 index 0000000000..c91b872ae1 --- /dev/null +++ b/src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp @@ -0,0 +1,79 @@ +#include "../AICState.func.hpp" + +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Synchrony/GameSynchronyState.func.hpp" +#include "OpenSHC/AI/AIType.hpp" +#include "OpenSHC/Game/GameMode2.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace AI { + + // FUNCTION: STRONGHOLDCRUSADER 0x004CDCF0 + BOOLEnum AICState::aiShouldAttackOrWaitForTeamCoordination(int playerID) + { + int otherPlayerID = 1; + int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; + if (aiType == OpenSHC::AI::AIT_NULL) + return FALSE; + if (DAT_GameState::instance.playerDataArray[playerID].requestStateUnk == 1) + return TRUE; + int aicIndex = aiType - 1; + + if (DAT_GameState::instance.playerDataArray[DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID] + .totalTroopValue + <= 100) + DAT_GameState::instance.playerDataArray[playerID].currentWaveRandomAttackingStrength = 0; + + if (DAT_GameState::instance.playerDataArray[playerID].totalAttackTroops + - DAT_GameState::instance.playerDataArray[playerID].totalAttackingEngineerTroops + < this->aics[aicIndex].AttForceBase + + DAT_GameState::instance.playerDataArray[playerID].currentWaveRandomAttackingStrength) + return FALSE; + if (DAT_GameState::instance.playerDataArray[playerID].aiAttackCoordinationLevel == 0) + return TRUE; + + int readyTeamMembers = 0; + int unreadyTeamMembers = 0; + for (; otherPlayerID < 9; otherPlayerID++) { + if (otherPlayerID == playerID) + continue; + if (MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, DAT_UnitsState::ptr)( + otherPlayerID) + == 0) + continue; + if (!MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::isAIPlayer, + DAT_GameSynchronyState::ptr)(otherPlayerID)) + continue; + if (DAT_GameState::instance.mapAndTime.playerTeams[otherPlayerID] + != DAT_GameState::instance.mapAndTime.playerTeams[playerID]) + continue; + + if (DAT_GameState::instance.playerDataArray[otherPlayerID].aiPlayerState != 0) + return TRUE; + if (DAT_GameState::instance.playerDataArray[otherPlayerID].totalAttackTroops + - DAT_GameState::instance.playerDataArray[otherPlayerID].totalAttackingEngineerTroops + >= this->aics[DAT_GameState::instance.playerDataArray[otherPlayerID].aiType - 1].AttForceBase + + DAT_GameState::instance.playerDataArray[otherPlayerID].currentWaveRandomAttackingStrength) + readyTeamMembers++; + else + unreadyTeamMembers++; + } + + if (readyTeamMembers >= DAT_GameState::instance.playerDataArray[playerID].aiAttackCoordinationLevel + || unreadyTeamMembers == 0) + return TRUE; + + DAT_GameState::instance.playerDataArray[playerID].aiCoordinatedAttackPatience++; + if (DAT_GameState::instance.playerDataArray[playerID].aiCoordinatedAttackPatience > 48 + && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_CAMPAIGN_MISSION) + return TRUE; + return FALSE; + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 94e983333c..b6e233f2d3 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35293,7 +35293,7 @@ SHC_3BB0A8C1_0x004CDBC0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CDCA0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004CDCF0 | 0.0% | Pending +SHC_3BB0A8C1_0x004CDCF0 | 83.2% | Pools 1 in a register SHC_3BB0A8C1_0x004CDE60 | 100.0% | Reimplemented From 305c962d88f8daa03e989fbe58479fd972888571 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Wed, 18 Nov 2026 17:56:15 +0100 Subject: [PATCH 8/8] reimplement: SHC_3BB0A8C1_0x004CDCF0 99.0% operand order of one compare (cmp ebp, [level]) AICState::aiShouldAttackOrWaitForTeamCoordination, from gynt's draft: 5452f1bc65, the commit before this one, is his file from upstream's reimpl/AICState (f4967de51a at 83.25%, "Pools 1 in a register"). reccmp reads 99.0 (normalized 95.2) where the draft reads 83.2 (78.1); quick_diff 0.9899, 99 instructions on both sides, every jump lands on the same instruction as in the original, and one instruction differs. Not 100. What changed: - The tests nest so that the returns are shared. The coordination level test wraps the loop and the patience test, and the force test wraps that: one `return TRUE` serves "no coordination", "enough ready members" and "out of patience", and one `return FALSE` serves the force test. With an early `return TRUE` at the level test the constant 1 is used four times before the loop (the requestStateUnk compare, two returns and the loop counter's start), the compiler keeps it in edi from the requestStateUnk compare on, and the counter's `mov edi, 1` moves up there: gynt's remark. With three uses it stays an immediate. (The allocator's probe shows the four-use piece with a benefit of 1, the lowest that still gets a register.) - The two counts are declared above the force test, where the original stores unreadyTeamMembers (0x004CDD6C). - otherPlayerID is declared in the loop's header. The draft declared it at the top of the function, which kept the pooled 1 in place there. What is left: 0x004CDD74 is `cmp ebp, dword ptr [esi + level]` in the original and `cmp dword ptr [esi + level], ebp` in ours; ebp is the zero that readyTeamMembers starts with. Same length, same jump. The compiler folds the test to "level against 0" and then puts the memory operand first; the original compared a register value that was still a variable when the compare was built. Spellings that all give our order: level != 0, 0 != level, plain truth value, readyTeamMembers != level and the reverse, unreadyTeamMembers != level, the sum of both counts, the counts declared in the other order or set by a chained assignment, short, char and unsigned counts. (readyTeamMembers < level gives a different jump, jle.) Measured (quick_diff): the draft 0.8900; the nested form with an early return for the level test 0.8293 (106 instructions); with the counts declared at the top of the function 0.7839; as committed 0.9899. Besides that: the loop's bound is countof(playerTeams) and the increments are prefix ones. Neither changes the code. The behaviour is the draft's. Co-authored-by: Gynt Co-Authored-By: Claude Fable 5.2 --- ...iShouldAttackOrWaitForTeamCoordination.cpp | 81 +++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp b/src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp index c91b872ae1..0449e3eb7e 100644 --- a/src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp +++ b/src/OpenSHC/AI/AICState/aiShouldAttackOrWaitForTeamCoordination.cpp @@ -16,64 +16,59 @@ namespace AI { // FUNCTION: STRONGHOLDCRUSADER 0x004CDCF0 BOOLEnum AICState::aiShouldAttackOrWaitForTeamCoordination(int playerID) { - int otherPlayerID = 1; int aiType = DAT_GameState::instance.playerDataArray[playerID].aiType; if (aiType == OpenSHC::AI::AIT_NULL) return FALSE; if (DAT_GameState::instance.playerDataArray[playerID].requestStateUnk == 1) return TRUE; int aicIndex = aiType - 1; - if (DAT_GameState::instance.playerDataArray[DAT_GameState::instance.playerDataArray[playerID].attackedPlayerID] .totalTroopValue <= 100) DAT_GameState::instance.playerDataArray[playerID].currentWaveRandomAttackingStrength = 0; - + int readyTeamMembers = 0; + int unreadyTeamMembers = 0; if (DAT_GameState::instance.playerDataArray[playerID].totalAttackTroops - DAT_GameState::instance.playerDataArray[playerID].totalAttackingEngineerTroops - < this->aics[aicIndex].AttForceBase - + DAT_GameState::instance.playerDataArray[playerID].currentWaveRandomAttackingStrength) - return FALSE; - if (DAT_GameState::instance.playerDataArray[playerID].aiAttackCoordinationLevel == 0) + >= this->aics[aicIndex].AttForceBase + + DAT_GameState::instance.playerDataArray[playerID].currentWaveRandomAttackingStrength) { + // nested so that the three ways to attack share one return: an early return here loses the match + if (DAT_GameState::instance.playerDataArray[playerID].aiAttackCoordinationLevel != 0) { + for (int otherPlayerID = 1; otherPlayerID < countof(DAT_GameState::instance.mapAndTime.playerTeams); + ++otherPlayerID) { + if (otherPlayerID == playerID) + continue; + if (MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, + DAT_UnitsState::ptr)(otherPlayerID) + == 0) + continue; + if (!MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::isAIPlayer, + DAT_GameSynchronyState::ptr)(otherPlayerID)) + continue; + if (DAT_GameState::instance.mapAndTime.playerTeams[otherPlayerID] + != DAT_GameState::instance.mapAndTime.playerTeams[playerID]) + continue; + if (DAT_GameState::instance.playerDataArray[otherPlayerID].aiPlayerState != 0) + return TRUE; + if (DAT_GameState::instance.playerDataArray[otherPlayerID].totalAttackTroops + - DAT_GameState::instance.playerDataArray[otherPlayerID].totalAttackingEngineerTroops + >= this->aics[DAT_GameState::instance.playerDataArray[otherPlayerID].aiType - 1].AttForceBase + + DAT_GameState::instance.playerDataArray[otherPlayerID].currentWaveRandomAttackingStrength) + ++readyTeamMembers; + else + ++unreadyTeamMembers; + } + if (readyTeamMembers < DAT_GameState::instance.playerDataArray[playerID].aiAttackCoordinationLevel + && unreadyTeamMembers != 0) { + ++DAT_GameState::instance.playerDataArray[playerID].aiCoordinatedAttackPatience; + if (DAT_GameState::instance.playerDataArray[playerID].aiCoordinatedAttackPatience <= 48 + || DAT_GameCore::instance.gameMode_2 == OpenSHC::Game::GM_CAMPAIGN_MISSION) + return FALSE; + } + } return TRUE; - - int readyTeamMembers = 0; - int unreadyTeamMembers = 0; - for (; otherPlayerID < 9; otherPlayerID++) { - if (otherPlayerID == playerID) - continue; - if (MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::getAliveLordForPlayer, DAT_UnitsState::ptr)( - otherPlayerID) - == 0) - continue; - if (!MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::isAIPlayer, - DAT_GameSynchronyState::ptr)(otherPlayerID)) - continue; - if (DAT_GameState::instance.mapAndTime.playerTeams[otherPlayerID] - != DAT_GameState::instance.mapAndTime.playerTeams[playerID]) - continue; - - if (DAT_GameState::instance.playerDataArray[otherPlayerID].aiPlayerState != 0) - return TRUE; - if (DAT_GameState::instance.playerDataArray[otherPlayerID].totalAttackTroops - - DAT_GameState::instance.playerDataArray[otherPlayerID].totalAttackingEngineerTroops - >= this->aics[DAT_GameState::instance.playerDataArray[otherPlayerID].aiType - 1].AttForceBase - + DAT_GameState::instance.playerDataArray[otherPlayerID].currentWaveRandomAttackingStrength) - readyTeamMembers++; - else - unreadyTeamMembers++; } - - if (readyTeamMembers >= DAT_GameState::instance.playerDataArray[playerID].aiAttackCoordinationLevel - || unreadyTeamMembers == 0) - return TRUE; - - DAT_GameState::instance.playerDataArray[playerID].aiCoordinatedAttackPatience++; - if (DAT_GameState::instance.playerDataArray[playerID].aiCoordinatedAttackPatience > 48 - && DAT_GameCore::instance.gameMode_2 != OpenSHC::Game::GM_CAMPAIGN_MISSION) - return TRUE; return FALSE; } - } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b6e233f2d3..fec78aee5f 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35293,7 +35293,7 @@ SHC_3BB0A8C1_0x004CDBC0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004CDCA0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004CDCF0 | 83.2% | Pools 1 in a register +SHC_3BB0A8C1_0x004CDCF0 | 99.0% | operand order of one compare (cmp ebp, [level]) SHC_3BB0A8C1_0x004CDE60 | 100.0% | Reimplemented