Repository navigation
Conversation
…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))) |
There was a problem hiding this comment.
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.
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.
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; |
There was a problem hiding this comment.
Is stuff like this with strange access patterns not usually a sign that a ptr was shifted from the compiler inside a structure?
There was a problem hiding this comment.
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.790PlayerData* 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) { |
There was a problem hiding this comment.
Are there enums for the food types?
There was a problem hiding this comment.
Check for foodTypeToBeEatenNext
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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))) |
There was a problem hiding this comment.
Same here: Does this not point us to the fact, that we use the wrong structure for this or that we access it wrong?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Was a while with external counter and internal pass++ tried? Did not read it in the tried list.
There was a problem hiding this comment.
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:
passdeclared 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;++passat the end;pass = pass + 1at 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.
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
…umption Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
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.
updateSection1034Infoneeds #549 for its three callees (see below).What is in it
3 functions, in two groups.
One that needs a
volatileread:WildlifeState::updateSection1034Info(0x0052E020, from #526).mov eax, [esp+N]; add eax, 1; cmp eax, 20; mov [esp+N], eax; jl.sub [esp+N], 1; jne.pass = *(int volatile*)&pass + 1keeps the up-counting memory counter, and the rest of the function is plain.for (...; ++pass): 0.943;forfrom 20: 0.945;do { } while (++pass < 20): 0.943.Two that walk an
int*throughPlayerDatawith a local field macro:GameStateStructures::processFoodConsumption(0x00458AD0) andGameStateStructures::updateCrowding(0x00458E60), both from #243.The original runs a pointer to one field of
playerDataArray[1]through the 8 players:foodClockSpeedin the first function,currentPopulationin the second. It reaches every otherPlayerDatafield 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
updateCrowdingloop advances the pointer in both exits rather than in theforheader. With the advance in the header, two registers swap.Trick-free forms tried:
processFoodConsumption(quick_gl)updateCrowding(quick_diff)this->playerDataArray[playerID].fieldPlayerData* playerwithplayer->fieldMain 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/main592a17d, with the struct and function resolvers on. The build list held these 3 files, main'supdateFoodTypesInStockForAllPlayers, and #549'shasAdjacentCellWithField24Or25,isAttackStagingCandidateCellandfloodFillZoneProximityFromCell:WildlifeState::updateSection1034Infovolatileread of the pass counterGameStateStructures::processFoodConsumptionPLAYER_FIELD#defineGameStateStructures::updateCrowdingPLAYER_FIELD#define/GL entries
All of these are in one separate commit:
processFoodConsumption: the original keepsthisin edx across the call toupdateFoodTypesInStockForAllPlayers, which is already /GL on main. Without the entry the function scores 96.5%.updateSection1034Info, plus its calleeshasAdjacentCellWithField24Or25,isAttackStagingCandidateCellandfloodFillZoneProximityFromCell: LTCG has to see the callees. Without them the function scores 70.9%..cppfiles are in Reimplement 10 WildlifeState and LandscapeState functions at 100% from #526 #549, which has to be merged first for this function to match.No header changes. There is one commit per function, each setting its line in
status/addresses-SHC-3BB0A8C1.txtto100.0% | Reimplemented.🤖 Generated with Claude Code
https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp