Skip to content
5 changes: 5 additions & 0 deletions cmake/compiler-flags-gl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -280,6 +281,10 @@ src/OpenSHC/Map/Units/UpdateMinerIronFetcher.cpp
src/OpenSHC/Map/Units/UpdateWorker_ConsiderTakingABreakUnk.cpp
src/OpenSHC/Map/Units/UnitsState/isWorkplaceBuildingOnAdjacentTile.cpp
src/OpenSHC/Map/Units/ConsiderHavingABreakNowUnk.cpp
src/OpenSHC/Map/WildlifeState/hasAdjacentCellWithField24Or25.cpp
src/OpenSHC/Map/WildlifeState/isAttackStagingCandidateCell.cpp
src/OpenSHC/Map/WildlifeState/floodFillZoneProximityFromCell.cpp
src/OpenSHC/Map/WildlifeState/updateSection1034Info.cpp
src/OpenSHC/Map/Units/UnitsState/acquireShootTarget.cpp
src/OpenSHC/Map/Units/UnitsState/calculateUnitMovementSpeed.cpp
src/OpenSHC/Map/Units/UnitsState/chooseHusbandAndWife.cpp
Expand Down
161 changes: 161 additions & 0 deletions src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp
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)))

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor Author

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] at this + 0x30D40, stride sizeof(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 imul because 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:

lea esi, [edx + 0x367e4]  ; 0x30D40 + 1*0x39F4 + 0x20B0 = &playerDataArray[1].foodClockSpeed
cmp [esi - 0x201c], ebp   ; 0x094  buildingEntryInfos[BEIE_KEEP].id
cmp [esi - 0x1edc], ebp   ; 0x1D4  buildingEntryInfos[BEIE_CAMPGROUND].id
mov [esi + 0xe0], -1      ; 0x2190 foodStorageLevel
mov eax, [esi + 0xd0]     ; 0x2180 currentPopulation
mov ecx, [esi + 0xdc]     ; 0x218C rationsSetting
...
add ebx, 1 / add esi, 0x39f4 / cmp ebx, 9 / jl

So the header is correct. With this->playerDataArray[playerID].field, MSVC builds the same loop, but it points the pointer at foodStorageLevel (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.


// 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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 &playerDataArray[1] plus one field offset out of the loop, and that is where the negative displacements come from. I tested the trick-free forms (quick_gl, with the callee linked):

  • this->playerDataArray[playerID].x: 0.790
  • PlayerData* player = &this->playerDataArray[1] with ++player: 0.790

Both give the original's instruction stream with two exceptions: the pointer points at foodStorageLevel (edx + 0x368c4) instead of foodClockSpeed (edx + 0x367e4), and there is a jmp where the original has an alignment nop. So a correctly typed struct pointer is the right model. But I found no declaration or access that makes MSVC choose foodClockSpeed as the base. PLAYER_FIELD only forces that choice.

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
35 changes: 35 additions & 0 deletions src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp
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)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer as in processFoodConsumption: the structure and the indexing are correct. The only difference is which field MSVC points the pointer at:

add ecx, 0x368b4          ; 0x30D40 + 0x39F4 + 0x2180 = &playerDataArray[1].currentPopulation
mov esi, [ecx - 0x210c]   ; 0x74   populationCap
mov eax, [ecx]            ; 0x2180 currentPopulation
mov [ecx - 0x2100], ...   ; 0x80   crowding
add ecx, 0x39f4 / sub ebx, 1 / jne

Plain this->playerDataArray[playerID].x with an if / else-if chain gives the same 31 instructions, but with add ecx, 0x347a8 (pointing at populationCap) and ebx/edi swapped. That scores 0.613 in quick_diff. None of the other forms I tried put it on currentPopulation either:

Form quick_diff Pointer field
reading the population first 0.576 crowding
PlayerData* walk 0.478 / 0.438 crowding / struct start
PlayerData& 0.353 populationCap
nested or inverted conditions 0.39–0.52 populationCap

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
46 changes: 46 additions & 0 deletions src/OpenSHC/Map/WildlifeState/updateSection1034Info.cpp
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was a while with external counter and internal pass++ tried? Did not read it in the tried list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 while form compiles to the same code as the for:

Form quick_gl reccmp raw / norm
int pass = 0; while (pass < 20) { pass++; ... } 0.943 94.0 / 88.4
int pass = 0; while (pass < 20) { ...; pass++; } 0.943 94.0 / 88.4
while (pass++ < 20) { ... } 0.943 94.0 / 88.4
while (++pass <= 20) { ... } 0.943 94.0 / 88.4

These forms also scored 0.943 in quick_gl:

  • pass declared at the top of the function, before the game-mode return, with the increment at the start, at the end or in the condition;
  • != 20;
  • ++pass at the end;
  • pass = pass + 1 at the end.

In every case MSVC sees that pass is never read and reverses the counter: mov [esp+N], 20 ... sub [esp+N], 1; jne. The original has mov eax, [esp+N]; add eax, 1; cmp eax, 20; mov [esp+N], eax; jl. So the volatile stays.

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
6 changes: 3 additions & 3 deletions status/addresses-SHC-3BB0A8C1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -49297,7 +49297,7 @@ SHC_3BB0A8C1_0x0052DF50 | 0.0% | Pending

SHC_3BB0A8C1_0x0052DF60 | 0.0% | Pending

SHC_3BB0A8C1_0x0052E020 | 0.0% | Pending
SHC_3BB0A8C1_0x0052E020 | 100.0% | Reimplemented

SHC_3BB0A8C1_0x0052E120 | 0.0% | Pending

Expand Down
Loading