From 42de53b4656c7807eb33b9abd0597bfebc402c6d Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Mon, 23 Nov 2026 06:57:59 +0100 Subject: [PATCH] fix(AI): someChatMessageSelection at 100%, the first test as an early return AICState::someChatMessageSelection (0x004D10B0) read 99.2% (95.8% normalised) in a full build of main: the original pushes edi in front of the player loop's set-up (0x004D1107), ours behind it. Written as an early return that clears aiTauntResponseTimeWindow itself, the first test (in the in-game menu, in a single-player skirmish) moves the push to the original's place: the clear is written in both exits instead of once at the end. The rest is gynt's code from #261, one indentation level out, with the checker's style rules applied: the menu test by truthiness, prefix steps, `%=`, a loop for the eight receiver flags, both loops bounded by `countof`, the locals declared where they are first set, the loop comment cut to one line, and the unused `using` lines and the BOOLEnum include dropped. These compile to the same listing. The status line drops "one reordered push edi". Measured on main 0e4a83b5d with both resolver sets on: 100.0% raw and normalised in a narrow build of this text and in a full build of it. A plain 1..8 player loop reads 61.0% (40.9% normalised) in a narrow build. Not run in game. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- .../AI/AICState/someChatMessageSelection.cpp | 129 ++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 57 insertions(+), 74 deletions(-) diff --git a/src/OpenSHC/AI/AICState/someChatMessageSelection.cpp b/src/OpenSHC/AI/AICState/someChatMessageSelection.cpp index efcd33cacd..e1634341d9 100644 --- a/src/OpenSHC/AI/AICState/someChatMessageSelection.cpp +++ b/src/OpenSHC/AI/AICState/someChatMessageSelection.cpp @@ -6,7 +6,6 @@ #include "OpenSHC/AI/AIType.hpp" #include "OpenSHC/Commands/GameCommandType.hpp" #include "OpenSHC/Game/GameMode.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_GameCore.hpp" #include "OpenSHC/Globals/DAT_GameState.hpp" @@ -17,90 +16,74 @@ namespace OpenSHC { namespace AI { - using OpenSHC::AI::AIType; - using OpenSHC::Commands::GameCommandType; - using OpenSHC::Game::GameMode; - using OpenSHC::WindowsHelper::Enums::BOOLEnum; - // FUNCTION: STRONGHOLDCRUSADER 0x004D10B0 void AICState::someChatMessageSelection() { - int candidates[9]; - int candidateCount; - int total; - int i; - int playerID; - - if ((MACRO_CALL_MEMBER(OpenSHC::Game::GameCore_Func::getAreWeInAInGameMenu, DAT_GameCore::ptr)() != FALSE) - && (DAT_GameSynchronyState::instance.currentGameMode == OpenSHC::Game::GM_SKIRMISH_SINGLE_PLAYER)) { + if (!MACRO_CALL_MEMBER(OpenSHC::Game::GameCore_Func::getAreWeInAInGameMenu, DAT_GameCore::ptr)() + || (DAT_GameSynchronyState::instance.currentGameMode != OpenSHC::Game::GM_SKIRMISH_SINGLE_PLAYER)) { + this->aiTauntResponseTimeWindow = 0; + return; + } - if (this->aiTauntResponseTimeWindow == 0) { - return; - } + if (this->aiTauntResponseTimeWindow == 0) { + return; + } - if (timeGetTime() - this->aiTauntResponseTimeWindow <= 2000) { - return; - } + if (timeGetTime() - this->aiTauntResponseTimeWindow <= 2000) { + return; + } - candidateCount = 0; - // Players 1..8, unrolled 4 at a time. The outer loop has to step by 4 and the inner 4 can be - // left to the compiler, which unrolls them into the same instructions as four written-out - // copies. A plain 1..8 loop does not work (0.92 -> 0.67, and the same for every spelling of - // it): MSVC unrolls all 8 and, with the trip count known, constant-folds every index into an - // absolute address and an immediate. The original keeps the index in a register, so its - // source had an induction variable surviving the unroll - which is what stepping by 4 gives. - for (playerID = 1; playerID < 9; playerID += 4) { - for (int offset = 0; offset < 4; offset++) { - if (DAT_GameSynchronyState::instance.currentAIArray[playerID + offset] != 0 - && DAT_GameState::instance.mapAndTime.playerTeams[playerID + offset] - != DAT_GameState::instance.mapAndTime - .playerTeams[DAT_GameSynchronyState::instance.currentPlayerSlotID]) { - candidates[candidateCount] = playerID + offset; - candidateCount++; - } + int candidates[9]; + int candidateCount = 0; + // four players a step, the inner four left to the compiler (a plain 1..8 loop loses the match: 61%) + for (int playerID = 1; playerID < countof(DAT_GameSynchronyState::instance.currentAIArray); playerID += 4) { + for (int offset = 0; offset < 4; ++offset) { + if (DAT_GameSynchronyState::instance.currentAIArray[playerID + offset] != 0 + && DAT_GameState::instance.mapAndTime.playerTeams[playerID + offset] + != DAT_GameState::instance.mapAndTime + .playerTeams[DAT_GameSynchronyState::instance.currentPlayerSlotID]) { + candidates[candidateCount] = playerID + offset; + candidateCount++; } } + } - if (candidateCount > 0) { - total = 0; - // NOTE: 'i = total' keeps the (redundant) loop entry check of the original. - for (i = total; i < candidateCount; i++) { - total += DAT_SkirmishDefinedData::instance - .MaxBreachTribes[DAT_GameState::instance.playerDataArray[candidates[i]].aiType - + OpenSHC::AI::AIT_FREDERICK] - .tribeType; - } + if (candidateCount > 0) { + int total = 0; + // from total, which keeps the original's redundant entry test + for (int i = total; i < candidateCount; ++i) { + total += DAT_SkirmishDefinedData::instance + .MaxBreachTribes[DAT_GameState::instance.playerDataArray[candidates[i]].aiType + + OpenSHC::AI::AIT_FREDERICK] + .tribeType; + } - if (total != 0) { - int roll = (int)SEC_RNG::instance.currentNumber1; - MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber1, SEC_RNG::ptr)(); - roll = roll % total; + if (total != 0) { + int roll = (int)SEC_RNG::instance.currentNumber1; + MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber1, SEC_RNG::ptr)(); + roll %= total; - for (i = 0; i < candidateCount; i++) { - playerID = candidates[i]; - roll -= DAT_SkirmishDefinedData::instance - .MaxBreachTribes[DAT_GameState::instance.playerDataArray[playerID].aiType - + OpenSHC::AI::AIT_FREDERICK] - .tribeType; - if (roll < 0) { - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[1] = 1; - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[2] = 1; - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[3] = 1; - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[4] = 1; - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[5] = 1; - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[6] = 1; - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[7] = 1; - DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[8] = 1; - DAT_GameSynchronyState::instance.DAT_ChatTauntOrMessage = -2 - - DAT_SkirmishDefinedData::instance - .MaxBreachTribes[DAT_GameState::instance.playerDataArray[playerID].aiType - + OpenSHC::AI::AIT_FREDERICK] - .tribeCount; - DAT_GameSynchronyState::instance.DAT_GameCommandParam0 = playerID; - MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::queueCommand, - DAT_GameSynchronyState::ptr)(OpenSHC::Commands::GCT_TAUNT_OR_CHAT); - break; + for (int i = 0; i < candidateCount; ++i) { + int const playerID = candidates[i]; + roll -= DAT_SkirmishDefinedData::instance + .MaxBreachTribes[DAT_GameState::instance.playerDataArray[playerID].aiType + + OpenSHC::AI::AIT_FREDERICK] + .tribeType; + if (roll < 0) { + for (int receiver = 1; + receiver < countof(DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray); + ++receiver) { + DAT_GameSynchronyState::instance.DAT_ChatMessageReceiverArray[receiver] = 1; } + DAT_GameSynchronyState::instance.DAT_ChatTauntOrMessage = -2 + - DAT_SkirmishDefinedData::instance + .MaxBreachTribes[DAT_GameState::instance.playerDataArray[playerID].aiType + + OpenSHC::AI::AIT_FREDERICK] + .tribeCount; + DAT_GameSynchronyState::instance.DAT_GameCommandParam0 = playerID; + MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::queueCommand, + DAT_GameSynchronyState::ptr)(OpenSHC::Commands::GCT_TAUNT_OR_CHAT); + break; } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e8a6864d03..a8aab21bf0 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -35457,7 +35457,7 @@ SHC_3BB0A8C1_0x004D1050 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D1090 | 0.0% | Pending -SHC_3BB0A8C1_0x004D10B0 | 100.0% | Reimplemented, one reordered push edi +SHC_3BB0A8C1_0x004D10B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004D11C0 | 0.0% | Pending