Skip to content

Reimplement 3 functions that need a volatile read or a local field-walk #define - #559

Open
gynt wants to merge 8 commits into
sourcehold:mainfrom
gynt:reimpl/match-tricks-from-drafts
Open

gynt wants to merge 8 commits into
sourcehold:mainfrom
gynt:reimpl/match-tricks-from-drafts

Conversation

@gynt

@gynt gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Split out of #549 and #551 before review: these are the functions from gynt's drafts #526 and #243 that match only through a form this project keeps apart, as #519 did. They are separated so they can be looked at on their own. updateSection1034Info needs #549 for its three callees (see below).

What is in it

3 functions, in two groups.

One that needs a volatile read: WildlifeState::updateSection1034Info (0x0052E020, from #526).

  • An outer loop runs the cell pass 20 times. Its counter is never read inside the loop.
  • The original counts the stack-slot counter up from 0: mov eax, [esp+N]; add eax, 1; cmp eax, 20; mov [esp+N], eax; jl.
  • With any plain loop, MSVC reverses the unused counter: it starts the slot at 20 and closes the loop with sub [esp+N], 1; jne.
  • The step pass = *(int volatile*)&pass + 1 keeps the up-counting memory counter, and the rest of the function is plain.
  • Plain forms tried, with the quick_gl ratio (the volatile form scores 1.000):
    • for (...; ++pass): 0.943;
    • a counting-down for from 20: 0.945;
    • do { } while (++pass < 20): 0.943.

Two that walk an int* through PlayerData with a local field macro: GameStateStructures::processFoodConsumption (0x00458AD0) and GameStateStructures::updateCrowding (0x00458E60), both from #243.

  • The original runs a pointer to one field of playerDataArray[1] through the 8 players: foodClockSpeed in the first function, currentPopulation in the second. It reaches every other PlayerData field by its displacement from that field.

  • MSVC builds the same addressing only when the source does the same. PLAYER_FIELD(type, field) is *(type*)((char*)base + offsetof(PlayerData, field) - offsetof(PlayerData, base)), #defined in each file and #undefd after the function. It keeps the field names readable at every use.

  • The updateCrowding loop advances the pointer in both exits rather than in the for header. With the advance in the header, two registers swap.

  • Trick-free forms tried:

    Form processFoodConsumption (quick_gl) updateCrowding (quick_diff)
    this->playerDataArray[playerID].field 0.790 0.576
    a typed PlayerData* player with player->field 0.790 0.478

    Main has no accessor that gives the original's base. With indexing, MSVC bases the loop on a different field, which shifts every displacement.

Match

I measured with reccmp on this branch, on origin/main 592a17d, with the struct and function resolvers on. The build list held these 3 files, main's updateFoodTypesInStockForAllPlayers, and #549's hasAdjacentCellWithField24Or25, isAttackStagingCandidateCell and floodFillZoneProximityFromCell:

Function Draft Trick Raw Normalized
WildlifeState::updateSection1034Info #526 volatile read of the pass counter 100 100
GameStateStructures::processFoodConsumption #243 local PLAYER_FIELD #define 100 100
GameStateStructures::updateCrowding #243 local PLAYER_FIELD #define 100 100

/GL entries

All of these are in one separate commit:

No header changes. There is one commit per function, each setting its line in status/addresses-SHC-3BB0A8C1.txt to 100.0% | Reimplemented.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

gynt and others added 4 commits December 4, 2026 17:09
…callees with /GL

processFoodConsumption: the original keeps this in edx across the call to updateFoodTypesInStockForAllPlayers (already /GL on main); 96.5% without the entry.
updateSection1034Info with hasAdjacentCellWithField24Or25, isAttackStagingCandidateCell and floodFillZoneProximityFromCell (those three .cpp files come with sourcehold#549): LTCG has to see the callees, 70.9% without them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
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.

}
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.

if (PLAYER_FIELD(int, foodTypeToBeEatenNext) > 3) {
PLAYER_FIELD(int, foodTypeToBeEatenNext) = 0;
}
if (PLAYER_FIELD(int, foodTypeToBeEatenNext) == 0 && PLAYER_FIELD(int, breadCount) > 0) {

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.

Are there enums for the food types?

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.

Check for foodTypeToBeEatenNext

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.

There is none on main. git grep -i food -- 'src/**/Enums/*.hpp' finds only UI-tab and scenario-event values, and PlayerData::foodTypeToBeEatenNext (0x20AC) is a plain int.

The asm fixes the values. Type 0 checks breadCount and passes RT_BREAD (0xA), 1 is cheese (0xB), 2 is meat (0xC) and 3 is apples (0xD). That is the order of breadCount..appleCount in PlayerData and in updateFoodTypesInStockForAllPlayers, i.e. RT_x - RT_BREAD. I propose this as a Ghidra enum, next to BuildingEntryInfoEnum in Game/Player/Enums:

FoodTypeEnum { FTE_BREAD = 0, FTE_CHEESE = 1, FTE_MEAT = 2, FTE_APPLE = 3 }

foodTypeToBeEatenNext would then be typed FoodTypeEnumInt, and the wrap becomes > FTE_APPLE. I have not added a header in this PR. Once the generated one exists, the code can switch to it.

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.

FoodTypeEnum is in 3b9e506 and 33cdd1f

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.

The enum is used now in 29427b4: FoodTypeEnumInt for foodTypeToBeEatenNext, the wrap at > FTE_APPLE, the reset to FTE_BREAD, and FTE_BREAD/FTE_CHEESE/FTE_MEAT/FTE_APPLE through the whole chain. processFoodConsumption is still 100.0 / 100.0 in reccmp (full build, both resolver sets on).

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.

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.

gynt and others added 2 commits December 5, 2026 00:49
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The removed cast was checked on its own: the file's /FA listing is identical
without it and no new warning appears. The volatile and macro forms stay.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit that referenced this pull request Dec 5, 2026
gynt added a commit that referenced this pull request Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants