From 45331604fc96b915ef5fe126ccc816fdb3a67a77 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:15:54 +0100 Subject: [PATCH 1/8] build: compile processFoodConsumption with /GL As in #243: the original keeps this in edx across the call to updateFoodTypesInStockForAllPlayers, which is already /GL on main. LTCG can only reproduce that with both sides compiled /GL (96.5% without the entry, 100% with it). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- cmake/compiler-flags-gl.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 5430b694de..bc5fb935fb 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -20,6 +20,7 @@ src/OpenSHC/Input/MouseState/updateRightDragCameraControl.cpp src/OpenSHC/Text/FontSizeClass/setupFontSizeClassObject.cpp src/OpenSHC/Text/TextManager/setupFontSizeClassObjects.cpp src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp +src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp src/OpenSHC/Game/GameStateStructures/clearEnemyRelatedStructures.cpp src/OpenSHC/Game/GameStateStructures/fillWith0xFF.cpp src/OpenSHC/Game/GameStateStructures/computeSignPostEntryData.cpp From d212e713dc78d5b68950d08034c8634e320218c1 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:15:55 +0100 Subject: [PATCH 2/8] reimplement: SHC_3BB0A8C1_0x0045AF30 100.00% Reimplemented Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../computePopulationStatistics.cpp | 117 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp new file mode 100644 index 0000000000..c2558fca71 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp @@ -0,0 +1,117 @@ +#include "../GameStateStructures.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Resources/ResourceType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_UIDragDropDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace Game { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0045AF30 + void GameStateStructures::computePopulationStatistics() + { + for (int playerID = 1; playerID < 9; ++playerID) { + this->playerDataArray[playerID].currentPopulation + = ((this->playerDataArray[playerID].countEntities + - this->playerDataArray[playerID].nonInteractiveCitizenCountUnk) + - this->playerDataArray[playerID].field_0x2174) + - this->playerDataArray[playerID].armySize; + if (this->playerDataArray[playerID].lordID != 0 + && DAT_UnitsState::instance.units[this->playerDataArray[playerID].lordID].uid + == this->playerDataArray[playerID].lordUID) { + --this->playerDataArray[playerID].currentPopulation; + } + if (this->playerDataArray[playerID].ladyIDUnk != 0 + && DAT_UnitsState::instance.units[this->playerDataArray[playerID].ladyIDUnk].uid + == this->playerDataArray[playerID].someUnitIDSelfRef) { + --this->playerDataArray[playerID].currentPopulation; + } + if (this->playerDataArray[playerID].jesterIDUnk != 0 + && DAT_UnitsState::instance.units[this->playerDataArray[playerID].jesterIDUnk].uid + == this->playerDataArray[playerID].someUnitIDSelfRef_2) { + --this->playerDataArray[playerID].currentPopulation; + } + this->playerDataArray[playerID].populationRelatedCrowdingCountUnk + = this->playerDataArray[playerID].currentPopulation_2 + - this->playerDataArray[playerID].availablePeasantsOrHousedPeasants; + if (this->playerDataArray[playerID].blessedPeopleCountUnk <= 0) { + this->playerDataArray[playerID].blessedPeoplePercentage = 0; + } else { + int const total = this->playerDataArray[playerID].unblessedPeopleCountUnk + + this->playerDataArray[playerID].blessedPeopleCountUnk; + int blessedPercentage; + if (total == 0) { + blessedPercentage = 100; + } else { + blessedPercentage = (this->playerDataArray[playerID].blessedPeopleCountUnk * 100) / total; + } + this->playerDataArray[playerID].blessedPeoplePercentage = blessedPercentage; + } + if (this->mapAndTime.startOfDay != FALSE) { + this->playerDataArray[playerID] + .populationGrowth[this->playerDataArray[playerID].populationGrowthStatisticCounter] + = this->playerDataArray[playerID].availablePeasantsOrHousedPeasants; + ++this->playerDataArray[playerID].populationGrowthStatisticCounter; + if (this->playerDataArray[playerID].populationGrowthStatisticCounter >= 8) { + this->playerDataArray[playerID].populationGrowthStatisticCounter = 0; + } + int growthSum = 0; + for (int sample = 0; sample < 8; ++sample) { + growthSum += this->playerDataArray[playerID].populationGrowth[sample]; + } + this->playerDataArray[playerID].averagePopulationGrowthUnk = growthSum / 8; + } + if (this->mapAndTime.monthChanged != 0) { + if (this->mapAndTime.populationIndex < 300) { + this->mapAndTime.playerPopulationStatistics[playerID][this->mapAndTime.populationIndex] + = (short)this->playerDataArray[playerID].currentPopulation; + } else { + // Running average move values + for (int sample = 0; sample < 299; ++sample) { + this->mapAndTime.playerPopulationStatistics[playerID][sample] + = this->mapAndTime.playerPopulationStatistics[playerID][sample + 1]; + } + this->mapAndTime.playerPopulationStatistics[playerID][299] + = (short)this->playerDataArray[playerID].currentPopulation; + } + } + if (this->playerDataArray[playerID].currentPopulation + > DAT_GameSynchronyState::instance.finalResults.finalMaxPopulation[playerID]) { + DAT_GameSynchronyState::instance.finalResults.finalMaxPopulation[playerID] + = (short)this->playerDataArray[playerID].currentPopulation; + } + if (DAT_GameCore::instance.gameMode_2 != GM_EDITOR) { + if (this->playerDataArray[playerID].tacticalPowersBarLevel < 7000) { + ++this->playerDataArray[playerID].tacticalPowersBarLevel; + } + if (this->playerDataArray[playerID].goldDonation > 0) { + int goldIncrement = this->playerDataArray[playerID].goldDonation; + if (goldIncrement > 10) { + goldIncrement = 10; + } + this->playerDataArray[playerID].currentResources[Resources::RT_GOLD] += goldIncrement; + this->playerDataArray[playerID].goldDonation -= goldIncrement; + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + DAT_UIDragDropDefinedData::instance.MenuView_TriggerInitial = TRUE; + DAT_WindowAndDirectDraw::instance.unk_resetViewportRelated = 1; + } + } + } + } + if (this->mapAndTime.monthChanged != 0) { + ++this->mapAndTime.populationIndex; + } + MACRO_CALL_MEMBER(GameStateStructures_Func::updateFoodTypesInStockForAllPlayers, this)(); + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 766545d302..dcbeb30ba8 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -18027,7 +18027,7 @@ SHC_3BB0A8C1_0x0045AEB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045AF00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045AF30 | 0.0% | Pending +SHC_3BB0A8C1_0x0045AF30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045B1C0 | 100.0% | Reimplemented From 5ace9de955a979213e76123aba9c72f69b9edf7e Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:15:56 +0100 Subject: [PATCH 3/8] reimplement: SHC_3BB0A8C1_0x00458AD0 100.00% Reimplemented Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../processFoodConsumption.cpp | 166 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp new file mode 100644 index 0000000000..e16c68505e --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp @@ -0,0 +1,166 @@ +#include "../GameStateStructures.func.hpp" + +#include "OpenSHC/Audio/MSS/SoundSystem.func.hpp" +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Synchrony/GameSynchronyState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Game/Resources/ResourceType.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" +#include "OpenSHC/string-literals.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_CounterFoodWarningInterval.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_SoundSystemState.hpp" + +namespace OpenSHC { +namespace Game { + + using Map::MapType2; + using Resources::ResourceType; + using WindowsHelper::Enums::BOOLEnum; + + // The original walks a pointer to foodClockSpeed and reaches every other PlayerData field by its distance from it. + // Indexing playerDataArray[playerID] instead makes MSVC base the loop on another field, which shifts every + // displacement, and reading foodStorageLevel back from the field (as the original does) then costs 20 points. +#define PLAYER_FIELD(type, field) \ + (*(type*)((char*)foodClockSpeed + offsetof(PlayerData, field) - offsetof(PlayerData, foodClockSpeed))) + + // FUNCTION: STRONGHOLDCRUSADER 0x00458AD0 + void GameStateStructures::processFoodConsumption() + { + if (DAT_GameCore::instance.gameMode_2 == GM_EDITOR) { + return; + } + if (DAT_GameCore::instance.gameMode_2 == GM_SIEGE_THAT) { + return; + } + if (DAT_GameCore::instance.gameMode_2 == GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + return; + } + MACRO_CALL_MEMBER(GameStateStructures_Func::updateFoodTypesInStockForAllPlayers, this)(); + int playerID = 1; + int* foodClockSpeed = &this->playerDataArray[1].foodClockSpeed; + for (; playerID < 9; ++playerID, foodClockSpeed += sizeof(PlayerData) / sizeof(int)) { + if (PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_KEEP].id) <= 0 + || PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id) <= 0) { + continue; + } + PLAYER_FIELD(int, foodStorageLevel) = -1; + if (PLAYER_FIELD(int, foodTypesInStock) <= 0) { + PLAYER_FIELD(int, foodTypesCurrentlyEaten) = 0; + continue; + } + int clockSpeed = PLAYER_FIELD(int, currentPopulation) * 3; + PLAYER_FIELD(int, foodClockSpeed) = clockSpeed; + if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY + && MACRO_CALL_MEMBER(Synchrony::GameSynchronyState_Func::isAIPlayer, DAT_GameSynchronyState::ptr)( + playerID) + != FALSE) { + PLAYER_FIELD(int, foodClockSpeed) = (clockSpeed * 60) / 100; + } + switch (PLAYER_FIELD(int, rationsSetting)) { + case 0: + PLAYER_FIELD(int, foodClockSpeed) = 0; + PLAYER_FIELD(int, foodTypesCurrentlyEaten) = 0; + break; + case 1: + PLAYER_FIELD(int, foodClockSpeed) = PLAYER_FIELD(int, foodClockSpeed) / 2; + break; + case 3: + PLAYER_FIELD(int, foodClockSpeed) = (PLAYER_FIELD(int, foodClockSpeed) * 3) / 2; + break; + case 4: + PLAYER_FIELD(int, foodClockSpeed) = PLAYER_FIELD(int, foodClockSpeed) * 2; + } + if (PLAYER_FIELD(int, foodClockSpeed) != 0) { + PLAYER_FIELD(int, foodStorageLevel) + = ((15000 / PLAYER_FIELD(int, foodClockSpeed)) * PLAYER_FIELD(int, totalFood)) / 800; + } + if (DAT_GameState::instance.mapAndTime.monthChanged != 0) { + short leftover = (short)PLAYER_FIELD(int, foodStorageLevel); + PLAYER_FIELD(short, foodStorageLevelLastLastMonth) = PLAYER_FIELD(short, foodStorageLevelLastMonth); + PLAYER_FIELD(short, foodStorageLevelLastMonth) = leftover; + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_GameState::instance.mapAndTime.singlePlayerHasKeepAndGranary != FALSE) { + if (PLAYER_FIELD(int, totalFood) == 0) { + if (DAT_CounterFoodWarningInterval::instance == 0 + && MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() + == FALSE) { + // "No food distributed this month" + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + s_food_warning4_wav_005a509c); + } + ++DAT_CounterFoodWarningInterval::instance; + if (DAT_CounterFoodWarningInterval::instance > 2) { + DAT_CounterFoodWarningInterval::instance = 0; + } + } else if (leftover < 2 && leftover != PLAYER_FIELD(short, foodStorageLevelLastLastMonth) + && PLAYER_FIELD(int, rationsSetting) != 0) { + if (leftover == 1) { + if (MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() + == FALSE) { + // "Granary stocks are very low sire" + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + s_food_warning2_wav_005a5088); + } + } else if (MACRO_CALL_MEMBER(Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, + DAT_SoundSystemState::ptr)() + == FALSE) { + // "We are almost out of food sire" + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( + s_food_warning3_wav_005a5074); + } + } + } + } + PLAYER_FIELD(int, foodClock) += PLAYER_FIELD(int, foodClockSpeed); + if (PLAYER_FIELD(int, foodClock) > 15000) { + PLAYER_FIELD(int, foodClock) = 0; + PLAYER_FIELD(int, foodTypesCurrentlyEaten) = PLAYER_FIELD(int, foodTypesInStock); + for (;;) { + ++PLAYER_FIELD(int, foodTypeToBeEatenNext); + if (PLAYER_FIELD(int, foodTypeToBeEatenNext) > 3) { + PLAYER_FIELD(int, foodTypeToBeEatenNext) = 0; + } + if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 0 && PLAYER_FIELD(int, breadCount) > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_BREAD, 1, 0); + break; + } + if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 1 && PLAYER_FIELD(int, cheeseCount) > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_CHEESE, 1, 0); + break; + } + if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 2 && PLAYER_FIELD(int, meatCount) > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_MEAT, 1, 0); + break; + } + if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 3 && PLAYER_FIELD(int, appleCount) > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_APPLE, 1, 0); + break; + } + } + } + } + } + +#undef PLAYER_FIELD + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index dcbeb30ba8..2a8f33e378 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17733,7 +17733,7 @@ SHC_3BB0A8C1_0x00458A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458A80 | 0.0% | Pending -SHC_3BB0A8C1_0x00458AD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00458AD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458D38 | 0.0% | Pending From 8a1c78fce5169f33105e66765c33f004be926e25 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:15:56 +0100 Subject: [PATCH 4/8] reimplement: SHC_3BB0A8C1_0x00459A20 100.00% Reimplemented Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../spawnPoisonCloudsAroundBuilding.cpp | 59 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp new file mode 100644 index 0000000000..100374bee4 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp @@ -0,0 +1,59 @@ +#include "../GameStateStructures.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Random/RNG.func.hpp" +#include "OpenSHC/UI/MinimapViewState.func.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.hpp" +#include "OpenSHC/Map/Entities/EntityType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_MinimapViewState.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Game { + + using DE::SHCDE::eSFX; + using Map::Entities::EntityType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00459A20 + void GameStateStructures::spawnPoisonCloudsAroundBuilding(int buildingID) + { + // The decrement sits in each failure branch rather than in the loop header: that is what makes MSVC emit the + // original's read-modify-write on the stack slot. + for (int attemptsLeft = 20; attemptsLeft > 0;) { + uint xPosition + = SEC_RNG::instance.currentNumber2 % 30 - 15 + DAT_BuildingsState::instance.buildings[buildingID].x; + uint yPosition = (SEC_RNG::instance.currentNumber2 >> 8) % 30 - 15 + + DAT_BuildingsState::instance.buildings[buildingID].y; + MACRO_CALL_MEMBER(Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + if (xPosition > 399 || yPosition > 399 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[yPosition * 400 + xPosition] == 0) { + attemptsLeft--; + continue; + } + int const tile = DAT_ViewportRenderState::instance.translationMatrix[yPosition].addXgetTile + xPosition; + if ((DAT_TileMapState::instance.LogicLayer[tile] & 0x4a5014b1U) != 0) { + attemptsLeft--; + continue; + } + for (int cloudIndex = SEC_RNG::instance.currentNumber2 % 5 - 2; cloudIndex < 8; ++cloudIndex) { + MACRO_CALL_MEMBER(Map::Entities::EntityState_Func::spawnProjectileEntity, DAT_EntityState::ptr)(0, 0, 0, + xPosition * 8, yPosition * 8, DAT_TileMapState::instance.HeightLayer[tile], 0, 0, 0, + Map::Entities::ET_COW_POISON_CLOUD, 0); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + xPosition, yPosition, DE::SHCDE::FX_FLIES); + MACRO_CALL_MEMBER(UI::MinimapViewState_Func::setSpawnMoment, DAT_MinimapViewState::ptr)( + xPosition, yPosition); + return; + } + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 2a8f33e378..611f745260 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17901,7 +17901,7 @@ SHC_3BB0A8C1_0x004598B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00459930 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00459A20 | 0.0% | Pending +SHC_3BB0A8C1_0x00459A20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00459A40 | 0.0% | Pending From 25587790e9e3e4d756f5144953ece155e16306f2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:15:57 +0100 Subject: [PATCH 5/8] reimplement: SHC_3BB0A8C1_0x00458E60 100.00% Reimplemented Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../GameStateStructures/updateCrowding.cpp | 38 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp new file mode 100644 index 0000000000..fa9e0f688d --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp @@ -0,0 +1,38 @@ +#include "../GameStateStructures.func.hpp" + +namespace OpenSHC { +namespace Game { + + // The original walks a pointer to currentPopulation and reaches the other PlayerData fields by their distance + // from it. Indexing playerDataArray[playerID] instead makes MSVC base the loop on populationCap. +#define PLAYER_FIELD(type, field) \ + (*(type*)((char*)currentPopulation + offsetof(PlayerData, field) - offsetof(PlayerData, currentPopulation))) + + // FUNCTION: STRONGHOLDCRUSADER 0x00458E60 + void GameStateStructures::updateCrowding() + { + int* currentPopulation = &this->playerDataArray[1].currentPopulation; + for (int playerID = 1; playerID != 9; ++playerID) { + int populationCap = PLAYER_FIELD(int, populationCap); + int population = PLAYER_FIELD(int, currentPopulation); + // The pointer advance is written in both exits: with it in the loop header MSVC swaps two registers. + if (population <= populationCap) { + PLAYER_FIELD(int, crowding) = 0; + currentPopulation += sizeof(PlayerData) / sizeof(int); + continue; + } + if (population <= 0) { + PLAYER_FIELD(int, crowding) = 0; + } else if (populationCap <= 0) { + PLAYER_FIELD(int, crowding) = 150; + } else { + PLAYER_FIELD(int, crowding) = (population * 100) / populationCap; + } + currentPopulation += sizeof(PlayerData) / sizeof(int); + } + } + +#undef PLAYER_FIELD + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 611f745260..252a35e66d 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17749,7 +17749,7 @@ SHC_3BB0A8C1_0x00458D50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458E20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00458E60 | 0.0% | Pending +SHC_3BB0A8C1_0x00458E60 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458EC0 | 100.0% | Reimplemented From ab8078e4b1ee40d633e20adc7da03781393fff4f Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:15:58 +0100 Subject: [PATCH 6/8] reimplement: SHC_3BB0A8C1_0x00455E10 92.88% Register allocation differs Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- ...AndStatisticsAndStartGoodsAndResources.cpp | 128 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp new file mode 100644 index 0000000000..2c3054f1b5 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp @@ -0,0 +1,128 @@ +#include "../GameStateStructures.func.hpp" + +#include "OpenSHC/AI/AICState.func.hpp" +#include "OpenSHC/OS.func.hpp" +#include "OpenSHC/Game/GameMode.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_AICState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_RenderingDefinedData.hpp" + +namespace OpenSHC { +namespace Game { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00455E10 + void GameStateStructures::resetVariousCountsAndStatisticsAndStartGoodsAndResources() + { + for (int playerID = 1; playerID < 9; ++playerID) { + this->playerDataArray[playerID].weeksWithoutFood = 0; + this->playerDataArray[playerID].foodTypesCurrentlyEaten = 0; + this->playerDataArray[playerID].largestPopularityChangeReason = 0; + this->playerDataArray[playerID].foodStorageLevelLastLastMonth = 0; + this->playerDataArray[playerID].foodStorageLevelLastMonth = 0; + this->playerDataArray[playerID].vclock = 2000; + this->playerDataArray[playerID].someCount43 = 0; + int startingPopularity = this->mapAndTime.startingPopularity * 10; + this->playerDataArray[playerID].popularity = startingPopularity; + this->playerDataArray[playerID].storedPopularityPercent = startingPopularity; + this->playerDataArray[playerID].someCount44 = (short)startingPopularity; + this->playerDataArray[playerID].taxesSetting = 3; + this->playerDataArray[playerID].rationsSetting = 2; + int currentPlayerRations + = DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .rationsSetting; + this->playerDataArray[playerID].rationsSetting2 = currentPlayerRations; + this->playerDataArray[playerID].rationsSetting3 = currentPlayerRations; + this->playerDataArray[playerID].someCount45 = 1; + this->playerDataArray[playerID].someCount46 = 1; + this->playerDataArray[playerID].someCount47 = 0; + this->playerDataArray[playerID].textYOffset = 60; + this->playerDataArray[playerID].someCountdown01 = 0; + this->playerDataArray[playerID].playerDeathRelated = 0; + this->playerDataArray[playerID].previousBlessedPeoplePercentage = 0; + this->playerDataArray[playerID].popularityReligionBasedDiv25 = 0; + this->playerDataArray[playerID].eventPlagueCountdown = 0; + this->playerDataArray[playerID].eventLionsCountdown = 0; + this->playerDataArray[playerID].eventBanditsCountdown = 0; + this->playerDataArray[playerID].eventFireCountdown = 0; + this->playerDataArray[playerID].eventMarriageCountdown = 0; + this->playerDataArray[playerID].eventJesterCountdown = 0; + this->playerDataArray[playerID].currentPlaguePenalty = 0; + this->playerDataArray[playerID].currentLionsPenalty = 0; + this->playerDataArray[playerID].currentBanditsPenalty = 0; + this->playerDataArray[playerID].currentFirePenalty = 0; + this->playerDataArray[playerID].currentMarriageBoost = 0; + this->playerDataArray[playerID].currentJesterBoost = 0; + this->playerDataArray[playerID].eventPlagueHealerActivity = 0; + this->playerDataArray[playerID].keepEnclosementRelatedCountdown = 0; + this->playerDataArray[playerID].defensesDamagedByPlayer = 0; + for (int resourceType = 0; resourceType < countof(this->playerDataArray[playerID].startResources); + ++resourceType) { + this->playerDataArray[playerID].startResources[resourceType] + = this->mapAndTime.startGoods[resourceType]; + } + MACRO_CALL(OS_Func::_memset)(this->playerDataArray[playerID].snoozedBuildings, 0, + sizeof(this->playerDataArray[playerID].snoozedBuildings)); + this->playerDataArray[playerID].troopsKilled = 0; + this->playerDataArray[playerID].troopsLost = 0; + this->playerDataArray[playerID].weightedLosses = 0; + this->playerDataArray[playerID].field_0x40 = 0; + this->playerDataArray[playerID].field_0x42 = 0; + if (this->mapAndTime.editScenarioExtraOptions != 0) { + this->playerDataArray[playerID].taxesSetting = this->mapAndTime.scenarioTaxesSetting; + this->playerDataArray[playerID].rationsSetting = this->playerDataArray[playerID].rationsSetting3 + = this->playerDataArray[playerID].rationsSetting2 = this->mapAndTime.scenarioRationsSetting; + this->playerDataArray[playerID].currentResources[Resources::RT_GOLD] = this->mapAndTime.scenarioGold; + } + if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY) { + int gameTypeMultiplier = 1; + if (DAT_GameCore::instance.gameMode_2 == GM_SKIRMISH_AND_MULTIPLAYER + && DAT_GameCore::instance.isSkirmishTrail == TRUE + && DAT_GameCore::instance.currentTrailType == TT_EXTREME) { + gameTypeMultiplier = 3; + } + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[playerID] != -1) { + this->playerDataArray[playerID].startResources[Resources::RT_GOLD] + = DAT_RenderingDefinedData::instance + .field_0x53bb4[(DAT_GameSynchronyState::instance.skirmishCurrentAdvantageBalance + + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) + * 2 + + 12] + * gameTypeMultiplier; + } else if (DAT_GameSynchronyState::instance.currentAIArray[playerID] != 0) { + this->playerDataArray[playerID].startResources[Resources::RT_GOLD] + = DAT_RenderingDefinedData::instance + .field_0x53bb4[(DAT_GameSynchronyState::instance.skirmishCurrentAdvantageBalance + + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) + * 2 + + 13] + * gameTypeMultiplier; + } + } + DAT_GameState::instance.mapAndTime.populationIndex = 0; + this->playerDataArray[playerID].beforeLastMonthsGold + = (short)this->playerDataArray[playerID].currentResources[Resources::RT_GOLD]; + this->playerDataArray[playerID].lastMonthsGold + = (short)this->playerDataArray[playerID].currentResources[Resources::RT_GOLD]; + this->playerDataArray[playerID].aiHoldBackTicks = -1000; + for (int foodType = 0; foodType < countof(this->playerDataArray[playerID].isFoodTypeBanned); ++foodType) { + this->playerDataArray[playerID].isFoodTypeBanned[foodType] = 0; + } + MACRO_CALL_MEMBER(AI::AICState_Func::setCurrentAttackStrength, DAT_AICState::ptr)(playerID); + MACRO_CALL_MEMBER(AI::AICState_Func::setCurrentAttackRaidParameter, DAT_AICState::ptr)(playerID); + DAT_GameState::instance.playerDataArray[playerID].nervousBikCountdown = 48; + this->playerDataArray[playerID].tacticalPowersBarLevel = 0; + this->playerDataArray[playerID].goldDonation = 0; + } + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 252a35e66d..c5f2d55ad6 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17039,7 +17039,7 @@ SHC_3BB0A8C1_0x00455D40 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00455D90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00455E10 | 0.0% | Pending +SHC_3BB0A8C1_0x00455E10 | 92.9% | Register allocation differs SHC_3BB0A8C1_0x00455E40 | 0.0% | Pending From f7c95a071b0186739270b988512c70b582f4dc5f Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:25:05 +0100 Subject: [PATCH 7/8] style: apply the style guide to the five GameStateStructures functions countof instead of the loop bounds 9, 8, 300 and 299; the logic-layer mask in spawnPoisonCloudsAroundBuilding as Logic1 flags (L_SEA | L_BORDER | ... | L_MOAT == 0x4a5014b1); BOOLEnum fields and returns tested directly instead of against FALSE; const on the two assign-once locals of resetVariousCounts...; duplicate own .func.hpp include and unused usings dropped; namespace closing comments. Re-measured on 40cbb1cb7 with a full build and reccmp: the four 100% functions stay 100.0/100.0, resetVariousCountsAndStatisticsAndStartGoodsAndResources stays 92.9/85.5 (register-blind identical). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../computePopulationStatistics.cpp | 28 ++++++++-------- .../processFoodConsumption.cpp | 32 +++++++------------ ...AndStatisticsAndStartGoodsAndResources.cpp | 12 +++---- .../spawnPoisonCloudsAroundBuilding.cpp | 13 +++++--- .../GameStateStructures/updateCrowding.cpp | 6 ++-- 5 files changed, 43 insertions(+), 48 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp index c2558fca71..f380c181db 100644 --- a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp +++ b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp @@ -1,6 +1,5 @@ #include "../GameStateStructures.func.hpp" -#include "OpenSHC/Game/GameStateStructures.func.hpp" #include "OpenSHC/Game/GameMode2.hpp" #include "OpenSHC/Game/Resources/ResourceType.hpp" #include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" @@ -14,12 +13,10 @@ namespace OpenSHC { namespace Game { - using WindowsHelper::Enums::BOOLEnum; - // FUNCTION: STRONGHOLDCRUSADER 0x0045AF30 void GameStateStructures::computePopulationStatistics() { - for (int playerID = 1; playerID < 9; ++playerID) { + for (int playerID = 1; playerID < countof(this->playerDataArray); ++playerID) { this->playerDataArray[playerID].currentPopulation = ((this->playerDataArray[playerID].countEntities - this->playerDataArray[playerID].nonInteractiveCitizenCountUnk) @@ -56,31 +53,36 @@ namespace Game { } this->playerDataArray[playerID].blessedPeoplePercentage = blessedPercentage; } - if (this->mapAndTime.startOfDay != FALSE) { + if (this->mapAndTime.startOfDay) { this->playerDataArray[playerID] .populationGrowth[this->playerDataArray[playerID].populationGrowthStatisticCounter] = this->playerDataArray[playerID].availablePeasantsOrHousedPeasants; ++this->playerDataArray[playerID].populationGrowthStatisticCounter; - if (this->playerDataArray[playerID].populationGrowthStatisticCounter >= 8) { + if (this->playerDataArray[playerID].populationGrowthStatisticCounter + >= countof(this->playerDataArray[playerID].populationGrowth)) { this->playerDataArray[playerID].populationGrowthStatisticCounter = 0; } int growthSum = 0; - for (int sample = 0; sample < 8; ++sample) { + for (int sample = 0; sample < countof(this->playerDataArray[playerID].populationGrowth); ++sample) { growthSum += this->playerDataArray[playerID].populationGrowth[sample]; } - this->playerDataArray[playerID].averagePopulationGrowthUnk = growthSum / 8; + this->playerDataArray[playerID].averagePopulationGrowthUnk + = growthSum / countof(this->playerDataArray[playerID].populationGrowth); } if (this->mapAndTime.monthChanged != 0) { - if (this->mapAndTime.populationIndex < 300) { + if (this->mapAndTime.populationIndex < countof(this->mapAndTime.playerPopulationStatistics[playerID])) { this->mapAndTime.playerPopulationStatistics[playerID][this->mapAndTime.populationIndex] = (short)this->playerDataArray[playerID].currentPopulation; } else { // Running average move values - for (int sample = 0; sample < 299; ++sample) { + for (int sample = 0; sample < countof(this->mapAndTime.playerPopulationStatistics[playerID]) - 1; + ++sample) { this->mapAndTime.playerPopulationStatistics[playerID][sample] = this->mapAndTime.playerPopulationStatistics[playerID][sample + 1]; } - this->mapAndTime.playerPopulationStatistics[playerID][299] + this->mapAndTime + .playerPopulationStatistics[playerID] + [countof(this->mapAndTime.playerPopulationStatistics[playerID]) - 1] = (short)this->playerDataArray[playerID].currentPopulation; } } @@ -113,5 +115,5 @@ namespace Game { MACRO_CALL_MEMBER(GameStateStructures_Func::updateFoodTypesInStockForAllPlayers, this)(); } -} -} +} // namespace Game +} // namespace OpenSHC diff --git a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp index e16c68505e..7678441b80 100644 --- a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp +++ b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp @@ -2,7 +2,6 @@ #include "OpenSHC/Audio/MSS/SoundSystem.func.hpp" #include "OpenSHC/Audio/SFX/SFXState.func.hpp" -#include "OpenSHC/Game/GameStateStructures.func.hpp" #include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" #include "OpenSHC/Synchrony/GameSynchronyState.func.hpp" #include "OpenSHC/Game/GameMode.hpp" @@ -25,10 +24,6 @@ namespace OpenSHC { namespace Game { - using Map::MapType2; - using Resources::ResourceType; - using WindowsHelper::Enums::BOOLEnum; - // The original walks a pointer to foodClockSpeed and reaches every other PlayerData field by its distance from it. // Indexing playerDataArray[playerID] instead makes MSVC base the loop on another field, which shifts every // displacement, and reading foodStorageLevel back from the field (as the original does) then costs 20 points. @@ -51,7 +46,8 @@ namespace Game { MACRO_CALL_MEMBER(GameStateStructures_Func::updateFoodTypesInStockForAllPlayers, this)(); int playerID = 1; int* foodClockSpeed = &this->playerDataArray[1].foodClockSpeed; - for (; playerID < 9; ++playerID, foodClockSpeed += sizeof(PlayerData) / sizeof(int)) { + for (; playerID < countof(this->playerDataArray); + ++playerID, foodClockSpeed += sizeof(PlayerData) / sizeof(int)) { if (PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_KEEP].id) <= 0 || PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id) <= 0) { continue; @@ -65,8 +61,7 @@ namespace Game { PLAYER_FIELD(int, foodClockSpeed) = clockSpeed; if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY && MACRO_CALL_MEMBER(Synchrony::GameSynchronyState_Func::isAIPlayer, DAT_GameSynchronyState::ptr)( - playerID) - != FALSE) { + playerID)) { PLAYER_FIELD(int, foodClockSpeed) = (clockSpeed * 60) / 100; } switch (PLAYER_FIELD(int, rationsSetting)) { @@ -92,12 +87,11 @@ namespace Game { PLAYER_FIELD(short, foodStorageLevelLastLastMonth) = PLAYER_FIELD(short, foodStorageLevelLastMonth); PLAYER_FIELD(short, foodStorageLevelLastMonth) = leftover; if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID - && DAT_GameState::instance.mapAndTime.singlePlayerHasKeepAndGranary != FALSE) { + && DAT_GameState::instance.mapAndTime.singlePlayerHasKeepAndGranary) { if (PLAYER_FIELD(int, totalFood) == 0) { if (DAT_CounterFoodWarningInterval::instance == 0 - && MACRO_CALL_MEMBER( - Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() - == FALSE) { + && !MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)()) { // "No food distributed this month" MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( s_food_warning4_wav_005a509c); @@ -109,16 +103,14 @@ namespace Game { } else if (leftover < 2 && leftover != PLAYER_FIELD(short, foodStorageLevelLastLastMonth) && PLAYER_FIELD(int, rationsSetting) != 0) { if (leftover == 1) { - if (MACRO_CALL_MEMBER( - Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() - == FALSE) { + if (!MACRO_CALL_MEMBER(Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, + DAT_SoundSystemState::ptr)()) { // "Granary stocks are very low sire" MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( s_food_warning2_wav_005a5088); } - } else if (MACRO_CALL_MEMBER(Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, - DAT_SoundSystemState::ptr)() - == FALSE) { + } else if (!MACRO_CALL_MEMBER(Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, + DAT_SoundSystemState::ptr)()) { // "We are almost out of food sire" MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( s_food_warning3_wav_005a5074); @@ -162,5 +154,5 @@ namespace Game { #undef PLAYER_FIELD -} -} +} // namespace Game +} // namespace OpenSHC diff --git a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp index 2c3054f1b5..b8e8dc24ec 100644 --- a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp +++ b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp @@ -17,12 +17,10 @@ namespace OpenSHC { namespace Game { - using WindowsHelper::Enums::BOOLEnum; - // FUNCTION: STRONGHOLDCRUSADER 0x00455E10 void GameStateStructures::resetVariousCountsAndStatisticsAndStartGoodsAndResources() { - for (int playerID = 1; playerID < 9; ++playerID) { + for (int playerID = 1; playerID < countof(this->playerDataArray); ++playerID) { this->playerDataArray[playerID].weeksWithoutFood = 0; this->playerDataArray[playerID].foodTypesCurrentlyEaten = 0; this->playerDataArray[playerID].largestPopularityChangeReason = 0; @@ -30,13 +28,13 @@ namespace Game { this->playerDataArray[playerID].foodStorageLevelLastMonth = 0; this->playerDataArray[playerID].vclock = 2000; this->playerDataArray[playerID].someCount43 = 0; - int startingPopularity = this->mapAndTime.startingPopularity * 10; + int const startingPopularity = this->mapAndTime.startingPopularity * 10; this->playerDataArray[playerID].popularity = startingPopularity; this->playerDataArray[playerID].storedPopularityPercent = startingPopularity; this->playerDataArray[playerID].someCount44 = (short)startingPopularity; this->playerDataArray[playerID].taxesSetting = 3; this->playerDataArray[playerID].rationsSetting = 2; - int currentPlayerRations + int const currentPlayerRations = DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] .rationsSetting; this->playerDataArray[playerID].rationsSetting2 = currentPlayerRations; @@ -124,5 +122,5 @@ namespace Game { } } -} -} +} // namespace Game +} // namespace OpenSHC diff --git a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp index 100374bee4..939bd66b22 100644 --- a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp @@ -6,6 +6,7 @@ #include "OpenSHC/UI/MinimapViewState.func.hpp" #include "OpenSHC/DE/SHCDE/eSFX.hpp" #include "OpenSHC/Map/Entities/EntityType.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" #include "OpenSHC/Globals/DAT_BuildingsState.hpp" #include "OpenSHC/Globals/DAT_EntityState.hpp" @@ -18,8 +19,7 @@ namespace OpenSHC { namespace Game { - using DE::SHCDE::eSFX; - using Map::Entities::EntityType; + using namespace Map::LogicHelpers; // FUNCTION: STRONGHOLDCRUSADER 0x00459A20 void GameStateStructures::spawnPoisonCloudsAroundBuilding(int buildingID) @@ -38,7 +38,10 @@ namespace Game { continue; } int const tile = DAT_ViewportRenderState::instance.translationMatrix[yPosition].addXgetTile + xPosition; - if ((DAT_TileMapState::instance.LogicLayer[tile] & 0x4a5014b1U) != 0) { + if ((DAT_TileMapState::instance.LogicLayer[tile] + & (L_SEA | L_BORDER | L_BORDER_EDGE | L_ROCKY | L_BUILDING | L_TREE | L_RIVER + | L_CRENEL_VARIATIONUnk | L_FARM_FIELD_HOP | L_FARM_FIELD_DAIRY | L_MOAT)) + != 0) { attemptsLeft--; continue; } @@ -55,5 +58,5 @@ namespace Game { } } -} -} +} // namespace Game +} // namespace OpenSHC diff --git a/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp index fa9e0f688d..811f5b089a 100644 --- a/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp @@ -12,7 +12,7 @@ namespace Game { void GameStateStructures::updateCrowding() { int* currentPopulation = &this->playerDataArray[1].currentPopulation; - for (int playerID = 1; playerID != 9; ++playerID) { + for (int playerID = 1; playerID != countof(this->playerDataArray); ++playerID) { int populationCap = PLAYER_FIELD(int, populationCap); int population = PLAYER_FIELD(int, currentPopulation); // The pointer advance is written in both exits: with it in the loop header MSVC swaps two registers. @@ -34,5 +34,5 @@ namespace Game { #undef PLAYER_FIELD -} -} +} // namespace Game +} // namespace OpenSHC From 91a2b213923f384a347f6a3eb155f2211c5e4ebd Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 16:48:35 +0100 Subject: [PATCH 8/8] review: pre-review pass for TheRedDaemon - processFoodConsumption and updateCrowding leave this PR: they only match through a local PLAYER_FIELD #define that walks an int pointer through PlayerData (a PlayerData* walk measures 0.48 in quick_diff). Their status lines go back to main's, and so does the /GL entry processFoodConsumption needed. - spawnPoisonCloudsAroundBuilding: map bounds as MC_MAP_XY_LIMIT_MIN_1 / MC_MAP_XY_LIMIT, the logic mask tested by truthiness, const on the two positions, and the match-explaining comment removed (moved into the PR thread). Re-measured on 592a17d94 with a full build and reccmp: computePopulationStatistics and spawnPoisonCloudsAroundBuilding 100.0/100.0, resetVariousCountsAndStatisticsAndStartGoodsAndResources 92.9/85.5 (register-blind identical). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- cmake/compiler-flags-gl.txt | 1 - .../processFoodConsumption.cpp | 158 ------------------ .../spawnPoisonCloudsAroundBuilding.cpp | 20 +-- .../GameStateStructures/updateCrowding.cpp | 38 ----- status/addresses-SHC-3BB0A8C1.txt | 4 +- 5 files changed, 12 insertions(+), 209 deletions(-) delete mode 100644 src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp delete mode 100644 src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 802df1e5a2..cc2e343848 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -20,7 +20,6 @@ src/OpenSHC/Input/MouseState/updateRightDragCameraControl.cpp src/OpenSHC/Text/FontSizeClass/setupFontSizeClassObject.cpp src/OpenSHC/Text/TextManager/setupFontSizeClassObjects.cpp src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp -src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp src/OpenSHC/Game/GameStateStructures/clearEnemyRelatedStructures.cpp src/OpenSHC/Game/GameStateStructures/fillWith0xFF.cpp src/OpenSHC/Game/GameStateStructures/computeSignPostEntryData.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp deleted file mode 100644 index 7678441b80..0000000000 --- a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp +++ /dev/null @@ -1,158 +0,0 @@ -#include "../GameStateStructures.func.hpp" - -#include "OpenSHC/Audio/MSS/SoundSystem.func.hpp" -#include "OpenSHC/Audio/SFX/SFXState.func.hpp" -#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" -#include "OpenSHC/Synchrony/GameSynchronyState.func.hpp" -#include "OpenSHC/Game/GameMode.hpp" -#include "OpenSHC/Game/GameMode2.hpp" -#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" -#include "OpenSHC/Game/Resources/ResourceType.hpp" -#include "OpenSHC/Map/MapType2.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" -#include "OpenSHC/string-literals.hpp" - -#include "OpenSHC/Globals/DAT_BuildingsState.hpp" -#include "OpenSHC/Globals/DAT_CounterFoodWarningInterval.hpp" -#include "OpenSHC/Globals/DAT_GameCore.hpp" -#include "OpenSHC/Globals/DAT_GameState.hpp" -#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" -#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" -#include "OpenSHC/Globals/DAT_SFXState.hpp" -#include "OpenSHC/Globals/DAT_SoundSystemState.hpp" - -namespace OpenSHC { -namespace Game { - - // The original walks a pointer to foodClockSpeed and reaches every other PlayerData field by its distance from it. - // Indexing playerDataArray[playerID] instead makes MSVC base the loop on another field, which shifts every - // displacement, and reading foodStorageLevel back from the field (as the original does) then costs 20 points. -#define PLAYER_FIELD(type, field) \ - (*(type*)((char*)foodClockSpeed + offsetof(PlayerData, field) - offsetof(PlayerData, foodClockSpeed))) - - // FUNCTION: STRONGHOLDCRUSADER 0x00458AD0 - void GameStateStructures::processFoodConsumption() - { - if (DAT_GameCore::instance.gameMode_2 == GM_EDITOR) { - return; - } - if (DAT_GameCore::instance.gameMode_2 == GM_SIEGE_THAT) { - return; - } - if (DAT_GameCore::instance.gameMode_2 == GM_BUILDERUnk - && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { - return; - } - MACRO_CALL_MEMBER(GameStateStructures_Func::updateFoodTypesInStockForAllPlayers, this)(); - int playerID = 1; - int* foodClockSpeed = &this->playerDataArray[1].foodClockSpeed; - for (; playerID < countof(this->playerDataArray); - ++playerID, foodClockSpeed += sizeof(PlayerData) / sizeof(int)) { - if (PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_KEEP].id) <= 0 - || PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id) <= 0) { - continue; - } - PLAYER_FIELD(int, foodStorageLevel) = -1; - if (PLAYER_FIELD(int, foodTypesInStock) <= 0) { - PLAYER_FIELD(int, foodTypesCurrentlyEaten) = 0; - continue; - } - int clockSpeed = PLAYER_FIELD(int, currentPopulation) * 3; - PLAYER_FIELD(int, foodClockSpeed) = clockSpeed; - if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY - && MACRO_CALL_MEMBER(Synchrony::GameSynchronyState_Func::isAIPlayer, DAT_GameSynchronyState::ptr)( - playerID)) { - PLAYER_FIELD(int, foodClockSpeed) = (clockSpeed * 60) / 100; - } - switch (PLAYER_FIELD(int, rationsSetting)) { - case 0: - PLAYER_FIELD(int, foodClockSpeed) = 0; - PLAYER_FIELD(int, foodTypesCurrentlyEaten) = 0; - break; - case 1: - PLAYER_FIELD(int, foodClockSpeed) = PLAYER_FIELD(int, foodClockSpeed) / 2; - break; - case 3: - PLAYER_FIELD(int, foodClockSpeed) = (PLAYER_FIELD(int, foodClockSpeed) * 3) / 2; - break; - case 4: - PLAYER_FIELD(int, foodClockSpeed) = PLAYER_FIELD(int, foodClockSpeed) * 2; - } - if (PLAYER_FIELD(int, foodClockSpeed) != 0) { - PLAYER_FIELD(int, foodStorageLevel) - = ((15000 / PLAYER_FIELD(int, foodClockSpeed)) * PLAYER_FIELD(int, totalFood)) / 800; - } - if (DAT_GameState::instance.mapAndTime.monthChanged != 0) { - short leftover = (short)PLAYER_FIELD(int, foodStorageLevel); - PLAYER_FIELD(short, foodStorageLevelLastLastMonth) = PLAYER_FIELD(short, foodStorageLevelLastMonth); - PLAYER_FIELD(short, foodStorageLevelLastMonth) = leftover; - if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID - && DAT_GameState::instance.mapAndTime.singlePlayerHasKeepAndGranary) { - if (PLAYER_FIELD(int, totalFood) == 0) { - if (DAT_CounterFoodWarningInterval::instance == 0 - && !MACRO_CALL_MEMBER( - Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)()) { - // "No food distributed this month" - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( - s_food_warning4_wav_005a509c); - } - ++DAT_CounterFoodWarningInterval::instance; - if (DAT_CounterFoodWarningInterval::instance > 2) { - DAT_CounterFoodWarningInterval::instance = 0; - } - } else if (leftover < 2 && leftover != PLAYER_FIELD(short, foodStorageLevelLastLastMonth) - && PLAYER_FIELD(int, rationsSetting) != 0) { - if (leftover == 1) { - if (!MACRO_CALL_MEMBER(Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, - DAT_SoundSystemState::ptr)()) { - // "Granary stocks are very low sire" - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( - s_food_warning2_wav_005a5088); - } - } else if (!MACRO_CALL_MEMBER(Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, - DAT_SoundSystemState::ptr)()) { - // "We are almost out of food sire" - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( - s_food_warning3_wav_005a5074); - } - } - } - } - PLAYER_FIELD(int, foodClock) += PLAYER_FIELD(int, foodClockSpeed); - if (PLAYER_FIELD(int, foodClock) > 15000) { - PLAYER_FIELD(int, foodClock) = 0; - PLAYER_FIELD(int, foodTypesCurrentlyEaten) = PLAYER_FIELD(int, foodTypesInStock); - for (;;) { - ++PLAYER_FIELD(int, foodTypeToBeEatenNext); - if (PLAYER_FIELD(int, foodTypeToBeEatenNext) > 3) { - PLAYER_FIELD(int, foodTypeToBeEatenNext) = 0; - } - if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 0 && PLAYER_FIELD(int, breadCount) > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, - DAT_BuildingsState::ptr)(playerID, Resources::RT_BREAD, 1, 0); - break; - } - if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 1 && PLAYER_FIELD(int, cheeseCount) > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, - DAT_BuildingsState::ptr)(playerID, Resources::RT_CHEESE, 1, 0); - break; - } - if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 2 && PLAYER_FIELD(int, meatCount) > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, - DAT_BuildingsState::ptr)(playerID, Resources::RT_MEAT, 1, 0); - break; - } - if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 3 && PLAYER_FIELD(int, appleCount) > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, - DAT_BuildingsState::ptr)(playerID, Resources::RT_APPLE, 1, 0); - break; - } - } - } - } - } - -#undef PLAYER_FIELD - -} // namespace Game -} // namespace OpenSHC diff --git a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp index 939bd66b22..7ef3cecee8 100644 --- a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp @@ -7,6 +7,7 @@ #include "OpenSHC/DE/SHCDE/eSFX.hpp" #include "OpenSHC/Map/Entities/EntityType.hpp" #include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/MapConstants.hpp" #include "OpenSHC/Globals/DAT_BuildingsState.hpp" #include "OpenSHC/Globals/DAT_EntityState.hpp" @@ -24,24 +25,23 @@ namespace Game { // FUNCTION: STRONGHOLDCRUSADER 0x00459A20 void GameStateStructures::spawnPoisonCloudsAroundBuilding(int buildingID) { - // The decrement sits in each failure branch rather than in the loop header: that is what makes MSVC emit the - // original's read-modify-write on the stack slot. for (int attemptsLeft = 20; attemptsLeft > 0;) { - uint xPosition + uint const xPosition = SEC_RNG::instance.currentNumber2 % 30 - 15 + DAT_BuildingsState::instance.buildings[buildingID].x; - uint yPosition = (SEC_RNG::instance.currentNumber2 >> 8) % 30 - 15 + uint const yPosition = (SEC_RNG::instance.currentNumber2 >> 8) % 30 - 15 + DAT_BuildingsState::instance.buildings[buildingID].y; MACRO_CALL_MEMBER(Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); - if (xPosition > 399 || yPosition > 399 - || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[yPosition * 400 + xPosition] == 0) { + if (xPosition > Map::MC_MAP_XY_LIMIT_MIN_1 || yPosition > Map::MC_MAP_XY_LIMIT_MIN_1 + || DAT_ViewportRenderState::instance + .DAT_BinaryTileMap400x400[yPosition * Map::MC_MAP_XY_LIMIT + xPosition] + == 0) { attemptsLeft--; continue; } int const tile = DAT_ViewportRenderState::instance.translationMatrix[yPosition].addXgetTile + xPosition; - if ((DAT_TileMapState::instance.LogicLayer[tile] - & (L_SEA | L_BORDER | L_BORDER_EDGE | L_ROCKY | L_BUILDING | L_TREE | L_RIVER - | L_CRENEL_VARIATIONUnk | L_FARM_FIELD_HOP | L_FARM_FIELD_DAIRY | L_MOAT)) - != 0) { + if (DAT_TileMapState::instance.LogicLayer[tile] + & (L_SEA | L_BORDER | L_BORDER_EDGE | L_ROCKY | L_BUILDING | L_TREE | L_RIVER | L_CRENEL_VARIATIONUnk + | L_FARM_FIELD_HOP | L_FARM_FIELD_DAIRY | L_MOAT)) { attemptsLeft--; continue; } diff --git a/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp deleted file mode 100644 index 811f5b089a..0000000000 --- a/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "../GameStateStructures.func.hpp" - -namespace OpenSHC { -namespace Game { - - // The original walks a pointer to currentPopulation and reaches the other PlayerData fields by their distance - // from it. Indexing playerDataArray[playerID] instead makes MSVC base the loop on populationCap. -#define PLAYER_FIELD(type, field) \ - (*(type*)((char*)currentPopulation + offsetof(PlayerData, field) - offsetof(PlayerData, currentPopulation))) - - // FUNCTION: STRONGHOLDCRUSADER 0x00458E60 - void GameStateStructures::updateCrowding() - { - int* currentPopulation = &this->playerDataArray[1].currentPopulation; - for (int playerID = 1; playerID != countof(this->playerDataArray); ++playerID) { - int populationCap = PLAYER_FIELD(int, populationCap); - int population = PLAYER_FIELD(int, currentPopulation); - // The pointer advance is written in both exits: with it in the loop header MSVC swaps two registers. - if (population <= populationCap) { - PLAYER_FIELD(int, crowding) = 0; - currentPopulation += sizeof(PlayerData) / sizeof(int); - continue; - } - if (population <= 0) { - PLAYER_FIELD(int, crowding) = 0; - } else if (populationCap <= 0) { - PLAYER_FIELD(int, crowding) = 150; - } else { - PLAYER_FIELD(int, crowding) = (population * 100) / populationCap; - } - currentPopulation += sizeof(PlayerData) / sizeof(int); - } - } - -#undef PLAYER_FIELD - -} // namespace Game -} // namespace OpenSHC diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 871431c9b9..a0cc9bb908 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17733,7 +17733,7 @@ SHC_3BB0A8C1_0x00458A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458A80 | 0.0% | Pending -SHC_3BB0A8C1_0x00458AD0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00458AD0 | 0.0% | Pending SHC_3BB0A8C1_0x00458D38 | 0.0% | Pending @@ -17749,7 +17749,7 @@ SHC_3BB0A8C1_0x00458D50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458E20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00458E60 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00458E60 | 0.0% | Pending SHC_3BB0A8C1_0x00458EC0 | 100.0% | Reimplemented