Repository navigation
Reimplement 3 functions that need a volatile read or a local field-walk #define #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f5a8fbb
eefcf72
603a284
332ee1b
360338a
8ad6d16
b7d85b9
29427b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| #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/FoodTypeEnum.hpp" | ||
| #include "OpenSHC/Game/Resources/FoodTypeEnumInt.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 { | ||
|
|
||
| #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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is stuff like this with strange access patterns not usually a sign that a ptr was shifted from the compiler inside a structure?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, partly. The shifted pointer comes from MSVC, not from the original source. MSVC hoists
Both give the original's instruction stream with two exceptions: the pointer points at |
||
| 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 const 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 const leftover = 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(Resources::FoodTypeEnumInt, foodTypeToBeEatenNext); | ||
| if (PLAYER_FIELD(Resources::FoodTypeEnumInt, foodTypeToBeEatenNext) > Resources::FTE_APPLE) { | ||
| PLAYER_FIELD(Resources::FoodTypeEnumInt, foodTypeToBeEatenNext) = Resources::FTE_BREAD; | ||
| } | ||
| if (PLAYER_FIELD(Resources::FoodTypeEnumInt, foodTypeToBeEatenNext) == Resources::FTE_BREAD | ||
| && 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(Resources::FoodTypeEnumInt, foodTypeToBeEatenNext) == Resources::FTE_CHEESE | ||
| && 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(Resources::FoodTypeEnumInt, foodTypeToBeEatenNext) == Resources::FTE_MEAT | ||
| && 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(Resources::FoodTypeEnumInt, foodTypeToBeEatenNext) == Resources::FTE_APPLE | ||
| && 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 | ||
| Original file line number | Diff line number | Diff line change | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | |||||||||||||||||
| #include "../GameStateStructures.func.hpp" | |||||||||||||||||
|
|
|||||||||||||||||
| namespace OpenSHC { | |||||||||||||||||
| namespace Game { | |||||||||||||||||
|
|
|||||||||||||||||
| #define PLAYER_FIELD(type, field) \ | |||||||||||||||||
| (*(type*)((char*)currentPopulation + offsetof(PlayerData, field) - offsetof(PlayerData, currentPopulation))) | |||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: Does this not point us to the fact, that we use the wrong structure for this or that we access it wrong?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same answer as in Plain
A different struct cannot explain it either. The two functions use different base fields (0x20B0 and 0x2180), and each one reaches fields on both sides of its base. |
|||||||||||||||||
|
|
|||||||||||||||||
| // FUNCTION: STRONGHOLDCRUSADER 0x00458E60 | |||||||||||||||||
| void GameStateStructures::updateCrowding() | |||||||||||||||||
| { | |||||||||||||||||
| int* currentPopulation = &this->playerDataArray[1].currentPopulation; | |||||||||||||||||
| for (int playerID = 1; playerID != countof(this->playerDataArray); ++playerID) { | |||||||||||||||||
| int const populationCap = PLAYER_FIELD(int, populationCap); | |||||||||||||||||
| int const population = PLAYER_FIELD(int, currentPopulation); | |||||||||||||||||
| 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 | |||||||||||||||||
| Original file line number | Diff line number | Diff line change | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,46 @@ | |||||||||||||||||
| #include "../WildlifeState.func.hpp" | |||||||||||||||||
|
|
|||||||||||||||||
| #include "OpenSHC/Game/GameMode.hpp" | |||||||||||||||||
|
|
|||||||||||||||||
| #include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" | |||||||||||||||||
|
|
|||||||||||||||||
| namespace OpenSHC { | |||||||||||||||||
| namespace Map { | |||||||||||||||||
|
|
|||||||||||||||||
| // FUNCTION: STRONGHOLDCRUSADER 0x0052E020 | |||||||||||||||||
| void WildlifeState::updateSection1034Info() | |||||||||||||||||
| { | |||||||||||||||||
| if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) { | |||||||||||||||||
| return; | |||||||||||||||||
| } | |||||||||||||||||
| for (int pass = 0; pass < 20; pass = *(int volatile*)&pass + 1) { | |||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried it now with real builds and reccmp, with both resolver sets on and #549's callees in the build list. Every
These forms also scored 0.943 in quick_gl:
In every case MSVC sees that |
|||||||||||||||||
| for (int y = 0; y < countof(this->grid[0]); y++) { | |||||||||||||||||
| for (int x = 0; x < countof(this->grid); x++) { | |||||||||||||||||
| this->grid[x][y].unknownNonZero01 = 0; | |||||||||||||||||
| this->grid[x][y].field_0x6c = 0; | |||||||||||||||||
| if (this->grid[x][y].castlebuildings != 0 | |||||||||||||||||
| && MACRO_CALL_MEMBER(Map::WildlifeState_Func::hasAdjacentCellWithField24Or25, this)(x, y)) { | |||||||||||||||||
| this->grid[x][y].field_0x64++; | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
| for (int y = 0; y < countof(this->grid[0]); y++) { | |||||||||||||||||
| for (int x = 0; x < countof(this->grid); x++) { | |||||||||||||||||
| if (this->grid[x][y].field_0x60 != 0 || this->grid[x][y].field_0x64 != 0) { | |||||||||||||||||
| MACRO_CALL_MEMBER(Map::WildlifeState_Func::floodFillZoneProximityFromCell, this)(x, y, 6); | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
| for (int y = 0; y < countof(this->grid[0]); y++) { | |||||||||||||||||
| for (int x = 0; x < countof(this->grid); x++) { | |||||||||||||||||
| if (this->grid[x][y].unknownNonZero01 == 0 | |||||||||||||||||
| && MACRO_CALL_MEMBER(Map::WildlifeState_Func::isAttackStagingCandidateCell, this)(x, y)) { | |||||||||||||||||
| this->grid[x][y].field_0x6c++; | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| } // namespace Map | |||||||||||||||||
| } // namespace OpenSHC | |||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still can not follow. So what was the actual structure here? Everything a plain array? Or multidimensional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How I understand is that the compiler refused to take the right base field as the reference in the machine code (e.g. relative field access within a structure), so it found this macro as the solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a plain array of structs:
PlayerData playerDataArray[9]atthis + 0x30D40, stridesizeof(PlayerData)= 0x39F4. Entry 0 is skipped, so the loop covers players 1..8. There is no 2-D array and there are no parallel arrays.There is no
imulbecause MSVC strength-reduces the index into a pointer that steps 0x39F4 per player. It points that pointer at one field, not at the start of the struct:So the header is correct. With
this->playerDataArray[playerID].field, MSVC builds the same loop, but it points the pointer atfoodStorageLevel(lea esi, [edx + 0x368c4]). That puts every displacement 0xE0 off, and the score drops to 0.790 (quick_gl). The macro only fixes which field the pointer points at.