From 93ce3f21f796877c0875990094daefd23c217fa7 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sun, 18 Oct 2026 18:00:38 +0200 Subject: [PATCH 01/18] reimplement: the seven GameStateStructures functions still below 100% Split out of the previous reimpl/GameStateStructures tip: the 92 byte-identical functions moved to reimpl/GameStateStructures-100. What stays here is the work still open - computeBuildingCategoryEntryPointAndDestroyEarlierBuilding (33.5%), spawnMotherOrChild (54.6%), processPeasantSpawnAndDespawnCycle (84.0%), resetVariousCountsAndStatisticsAndStartGoodsAndResources (92.2%), spawnPoisonCloudsAroundBuilding (92.4%), processFoodConsumption (96.8%) and computePopulationStatistics (98.0%) - plus their status lines, the /GL entries they use, and the AGENTS.md / cheat-sheet notes and the two diff tools that record their blockers. Co-Authored-By: Claude Opus 5 (1M context) --- AGENTS.md | 47 ++ IMPLEMENTATION_CHEAT_SHEET.md | 404 ++++++++++++++++++ cmake/compiler-flags-gl.txt | 5 +- ...oryEntryPointAndDestroyEarlierBuilding.cpp | 155 +++++++ .../computePopulationStatistics.cpp | 125 ++++++ .../processFoodConsumption.cpp | 165 +++++++ .../processPeasantSpawnAndDespawnCycle.cpp | 137 ++++++ ...AndStatisticsAndStartGoodsAndResources.cpp | 127 ++++++ .../spawnMotherOrChild.cpp | 145 +++++++ .../spawnPoisonCloudsAroundBuilding.cpp | 55 +++ status/addresses-SHC-3BB0A8C1.txt | 96 ++--- tools/reimplementation-helper/batch/README.md | 1 + .../batch/diff_anchor.py | 120 ++++++ .../batch/diff_falsesym.py | 99 +++++ 14 files changed, 1632 insertions(+), 49 deletions(-) create mode 100644 src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp create mode 100644 src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp create mode 100644 src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp create mode 100644 src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp create mode 100644 src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp create mode 100644 src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp create mode 100644 src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp create mode 100644 tools/reimplementation-helper/batch/diff_anchor.py create mode 100644 tools/reimplementation-helper/batch/diff_falsesym.py diff --git a/AGENTS.md b/AGENTS.md index 44d25ab291..4806ade6b6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -160,6 +160,19 @@ restyling anything large: - a jump table - Ghidra usually renders the dispatch as a call through an unnamed pointer array and drops the arms, so rebuild the `switch` from the byte and jump tables instead of restyling the decompiler output. +`quick_diff.py` is a triage tool and it misranks two things. It prints the **wrong immediates for the +original side** (`0x51eb851f` where the binary holds `0x66666667` or `0x10624dd3`), so never read a constant +out of its output - use `orig_asm.py` or Ghidra. And its difflib ratio can invert the reccmp ranking even well +above the 0.6 the README warns about, because a changed base anchor makes every displacement differ textually +while the opcodes still line up: on `updateCrowding` it preferred a variant that measured 25.5% over one that +measured 33.3%. Rank with it, then settle small functions with `try_styles.py`, which narrows the build list to +one file and therefore costs about a minute per variant. + +reccmp symbolizes an immediate that happens to coincide with a data address, and it does so per binary. The +thirty `push 0xffffff` of `renderDebugDataCurrentPlayerData` come out as +`push I::instance+424671` on the original side and as the plain immediate on ours, which caps +that function at ~96% with no source change possible. Check for that before chasing a stable few percent. + `diff_triage.py` gives one line per function in the build list and says whether more source work can pay at all: it flags a `sub esp` frame-size mismatch (`FRAME`), byte-sized stack slots we have and the original does not (`BYTE`), a cold return block we inline where the original outlines it (`RET`), and the case where only register and stack-slot @@ -176,6 +189,40 @@ dropped because every hit it produced was a loop-induction stride rather than a which is the only lever left once a function is `alloc-only`; it pays about one move in ten, and mostly where the statements sit between two calls or on a loop back-edge. +### Switching the reimplementation on before measuring + +There are **two** switches, and both are deliberately uncommitted, so a rebase, a fresh clone or a +`git checkout -- src/OpenSHC` turns both off at once: + +- `tools/reimplementation-control/enable_reimplemented_data.py` - the `MACRO_STRUCT_RESOLVER` flags in + `src/OpenSHC/Globals/*.hpp`. +- `tools/reimplementation-helper/batch/enable_local_functions.py` - the `MACRO_FUNCTION_RESOLVER` flags for + exactly the files in `cmake/openshc-sources.txt.local`. The repository-wide + `tools/reimplementation-control/Enable-Reimplemented-Functions.ps1` flips every resolver that has a .cpp + anywhere in the tree, which does not link while the build list is a subset: each enabled resolver whose + .cpp is not compiled becomes an unresolved external. + +With the function resolvers off, every call leaves the module for the original game address, so **`/GL` +buys nothing**: LTCG cannot see a callee it does not compile. `getSalesPrice` sat at 68.4% for as long as +`getSellResourceAmount`'s resolver was `false`, and reached 100% the moment it was `true` - the original +keeps `this` in the volatile EDX across that call, which is exactly what LTCG provides. `clearEnemyRelatedStructures` +went 97.7% -> 100% the same way. So to chase a "volatile register survives a call" diff: put the callee's .cpp in +the build list, enable its resolver, and add **both** files to `cmake/compiler-flags-gl.txt`. + +**`/GL` can also make a function worse, and not only through the LTCG unit.** LTCG lets MSVC see that a +callee does not touch a given struct, so it starts hoisting loads the original's compiler had to repeat: +`spawnPoisonCloudsAroundBuilding` fell 92.4% -> 69.6% because LTCG hoisted +`buildings[buildingID].x`/`.y` out of the retry loop across `nextRandomNumber2`, which the original +re-reads every iteration. So add `/GL` for a pair only when the *original* shows the cross-call knowledge - +a volatile register still live after a `call`, or a missing `mov ecx, ` reload. A reimplemented +callee is not a reason on its own. + +`/GL` is whole-program, so the list is not a free-for-all. Adding files that are not part of the pair under +test re-codegens everything in the LTCG unit: one such round cost `processFoodConsumption` 20 points and +`spawnPoisonCloudsAroundBuilding` 23 while gaining 1.3 elsewhere. Add one caller/callee pair at a time and +keep only what measures better. A callee that is not itself at 100% is worse than useless here, because +LTCG then propagates *our* register use rather than the original's. + ### Binary Comparison (`reccmp`) Compares generated binaries against the original executable. diff --git a/IMPLEMENTATION_CHEAT_SHEET.md b/IMPLEMENTATION_CHEAT_SHEET.md index 12991e55dd..1c9e0313aa 100644 --- a/IMPLEMENTATION_CHEAT_SHEET.md +++ b/IMPLEMENTATION_CHEAT_SHEET.md @@ -642,3 +642,407 @@ Naturally, the stack reserved for the function indicated by `sub esp, ` At best, not only the stack fits, but variables are also at the correct positions and sizes within. If the stack usage shows a proper order in the function (i.e. the initial array occupies the first part in the allocated function stack, which means it requires the highest `[esp + ]` to reach), it might be sometimes good to fit one "side" of the stack first and then experiment with the rest of the function that has a non-fitting stack usage. + +### A constant that reaches a field through a register was a local in the source + +Where several arms of a chain each store the *same* literal both into a field and into an +arithmetic expression, the original loads the literal into a register first: + +```asm +mov eax, -0x96 ; the constant, in a register +add ebp, eax ; popularity = popularityAfterFear + penalty +mov dword ptr [ebx], ebp +mov word ptr [esi + 0x32ff8], ax ; someCount54 = penalty, from the SAME register +``` + +Writing the constants out per arm (`popularity = snapshot - 150; someCount54 = -150;`) gives +`mov word ptr [...], -0x96` with an *immediate* instead, because the two uses then share nothing. +The source assigned one local in each arm and used it after the chain; MSVC then duplicated the +two statements into every arm, which is why the stores look per-arm in the asm: + +```cpp +int siegePenalty; +if (c <= 0) { siegePenalty = -150; } else if (c <= 5) { siegePenalty = -125; } /* ... */ +this->...popularity = popularityAfterFear + siegePenalty; +this->...someCount54 = (short)siegePenalty; +``` + +This single change took `updatePopularity` from 68.3% to 93.2%; it also fixed the register +allocation of the whole surrounding function, which had looked like an unrelated allocator gap. +The tell is an *immediate* on our side where the original uses a byte/word register (`ax`, `cx`) +that was just loaded with the same constant. + +### The struct/loop base anchor moves when a field loses its stores + +MSVC picks one address as the base for a run of accesses into the same object +(`lea esi, [ecx + 0x367e4]`, then `[esi - 0x10]`, `[esi + 0xe0]`, ...). Which field lands at +offset 0 is not the most-used one and is not controllable from the source, but it *does* change +when the source changes: introducing a local for a field that was previously stored in several +arms moved the anchor by 0xe0 in `processFoodConsumption` and by 0x10 in +`processPeasantSpawnAndDespawnCycle`, and every displacement in the function then differs. + +Both of those locals are provably present in the original (a value survives a call in a +callee-saved register), yet the anchor shift cost more than the local gained: +81.7% -> 61.7% and 85.3% -> 69.0%. So when a `FRAME`-free function regresses by a large, +suspiciously uniform amount after a one-line change, compare the `lea`/`add` that sets up the +base before looking anywhere else, and keep the form with the matching anchor. + +### A function that looks huge may be a tail call + +`nof_fpoints_locationFinder` had been reimplemented with a 40-line `do/while` body; the original +is eleven instructions ending in `mov ecx, ; jmp 0x4a6940`, a tail call to +`PathFindingState::searchCertainPathLayer`, whose body Ghidra had folded into the caller's +decompilation. Check the original's last instruction for a `jmp` to another function's entry +before reproducing a long body: 35.6% -> normalized 100% here. + +### The join block of a long if/else-if chain moves with the size of the first arm + +In `computeBuildingCategoryEntryPointAndDestroyEarlierBuilding` the chain order, every arm's body and the +shared tail all match the original, yet the original ends +`[prologue][first arm][else chain][tail][epilogue]` and this toolchain emits +`[prologue][first arm][tail][epilogue][else chain]` - the `ret` lands at 51% of the function instead of 99%, +and because reccmp aligns instruction streams, almost the whole function then counts as differing (33.5%). + +The flip is a threshold on the **first arm**, not a source shape. Thirteen source forms (switch, fully +nested `else`, combined guard, empty trailing `else`, nested early returns, `__forceinline` helpers for the +tail and for the chain, parameter copies, casts, declaration order) all produced byte-identical code. +Shrinking the first arm does move the tail to the end, and three unrelated shrinks did it: dropping one arm +of its inner `orientation` chain, dropping its three early returns, or dropping its nested +`currentMenuViewType` block. The threshold sits between 168 and 175 instructions for that function, and the +original is 178, so it is not reachable by trimming anything the original actually contains. + +`#pragma optimize("s", on)` also produces the original's layout, which is a useful diagnosis - but it is not +the explanation: measured over the thirteen functions still short of 100% in `Game::GameStateStructures`, +`/Os` lost ground on **every single one** (e.g. `showPopAndGoldPopup` 0.93 -> 0.20, +`renderDebugDataCurrentPlayerData` 0.96 -> 0.17). The original was built for speed; only its block layout +differs, which points at a different compiler build rather than a different flag. `/G5`, `/G6` and `/G7` +change nothing in this toolchain (they are deprecated no-ops in VS2005). + +### The struct base anchor is coupled to the transformation you are trying to make + +MSVC bases a run of accesses into one object on a single address (`lea esi, [ecx + 0x367e4]`, then +`[esi - 0x10]`, `[esi + 0xe0]`, ...). Which field lands at offset 0 is not a size or frequency decision - +with the right anchor `processFoodConsumption` gets 30 byte-sized displacements and with the wrong one 3, +and MSVC still picks the wrong one - and it cannot be steered from the source: the global vs `this->` +spelling of a field, an extra read of it, the index's cast, the statement order and the number of stores to +other fields all leave it unchanged (probed with a `/FA` compile per variant, about ten seconds each). + +What *does* move it is the very change you want: + +- `processFoodConsumption`: the original keeps `currentPopulation * 3` in a callee-saved register across the + `isAIPlayer` call, so a local is provably right. Writing that local - or getting the same CSE for free by + enabling LTCG over the callee - moves the anchor by 0xe0 and costs 20 points, more than the local gains. +- `processPeasantSpawnAndDespawnCycle`: the original stores `vclockIncrement` once after the chain rather + than in each arm. Hoisting the store moves the anchor by 0x10 and costs 15 points. + +Both originals have the local *and* the matching anchor, so the combination is simply not reachable here. +When a one-line change costs a large and suspiciously uniform amount, read the `lea`/`add` that sets up the +base before looking anywhere else. + +One narrow rule did hold, for a condition that reads two arrays of the same struct: MSVC anchors the +induction variable on the **second** array mentioned in the source, while the comparison order follows the +source. `showPopAndGoldPopup` and `createStatsPopUpEntities` need `currentPlayerFullIDArray` tested first +*and* used as the anchor, so the two requirements conflict and the faithful form (the one the asm's +comparison order shows) is the one that loses the anchor. + +### Check whether the original shares a tail before duplicating a call + +Ghidra renders a cross-jumped tail as a `goto` into the middle of another branch, and the shared part can be +a single instruction. In `processPeasantSpawnAndDespawnCycle` the two `playSpeechSFX` sites are one call +reached from both paths with the speech ID in a register (`speechID = SEID_POP_EMIGRATE; goto LAB_0045b684;`), +and in `updatePopularity` the six siege arms share `popularity = snapshot + penalty; someCount54 = penalty;`. + +The two need opposite treatment, so read the asm rather than the decompiler's shape: + +- `updatePopularity` had to *gain* the variable: MSVC then duplicated the two statements into each arm by + itself, which is what the original does, and the function went 68.3% -> 93.2%. +- `processPeasantSpawnAndDespawnCycle` already emits one call site - MSVC cross-jumped the two identical + tails on its own - so routing the ID through a `SpeechEffectID` local changed nothing (MSVC propagates the + constant straight back into the push). Count the call sites in a `/FA` compile before restructuring. + +### Declaration order decides stack slots, and two swapped locals cost a whole function + +MSVC assigns stack slots in declaration order. Two locals declared the wrong way round therefore sit +in each other's slots and, because the slot drives the spill and reload, usually end up in each +other's registers as well. The damage spreads over every instruction that touches either of them, +while `diff_triage.py` still calls the function register-naming only - `assignSelectionToKey` read +95.9% with a `norm` of 1.000. + +`diff_slots.py` is the detector: the same number of slots on both sides with a different use count +per slot. For `assignSelectionToKey` the original spills +`mov [esp+0x18], edi; mov [esp+0x1c], ebx; mov [esp+0x20], esi` and reloads in the same order, so the +local in the lowest slot is declared first. Our source had + +```cpp +int slotID = 0; // -> [esp+0x20] +int tribeIndex = 0; // -> [esp+0x18] +``` + +and swapping the two declarations put both the slots and the registers back: **95.9% -> 100%**, one +line moved. `perm_decls.py` automates the search over a short run of declarations. It also found a +field store that belonged after the declarations in `updatePopularity` (93.2% -> 93.9%), and nothing +on seven other functions - so run it on anything `diff_slots.py` flags, but do not expect a hit +everywhere. + +### `lea` where the original has `add` means a snapshot local the original did not keep + +`field = snapshotLocal + delta` has to preserve the snapshot, so MSVC emits +`lea ecx, [eax + ebp]; mov [field], ecx`. `field = field + delta` lets it update the register that +already caches the field read: `add ebp, eax; mov [field], ebp`. The two differ by one byte per site +and by the register the result lands in, which looks exactly like allocator noise in the diff. + +`updatePopularity` spent the whole pass at 93% on six such sites: + +```cpp +int popularityAfterFear = this->playerDataArray[playerID].popularity; // the original has no such local +... +this->playerDataArray[playerID].popularity = popularityAfterFear + siegePenalty; // -> lea +this->playerDataArray[playerID].popularity = this->playerDataArray[playerID].popularity + siegePenalty; // -> add +``` + +Dropping the local took it to **100%** (and the six `lea`s were also why every jump offset after them +was 6 bytes off). The snapshot was redundant anyway: nothing between the read and the use writes +`popularity`, which is the condition to check before removing one. The original still loads the field +into a register early - that is MSVC hoisting the read, not evidence of a local. + +Note that the sibling line in the same function, `popularity = popularityAfterTax + popularityChange`, +*does* match with its local, because there the original really does keep a separate snapshot. Decide +per site from the `lea`/`add` in the original, not from the shape of the neighbouring code. + +### Two arrays of one struct in a condition: the original reaches the second off a pointer to the first + +`showPopAndGoldPopup` and `createStatsPopUpEntities` both guard their player loop with + +```cpp +if ((currentPlayerFullIDArray[playerID] == -1) && (currentAIArray[playerID] == 0)) { continue; } +``` + +and both sat at 87-89% because MSVC based the loop's induction variable on `currentAIArray` while the +original based it on `currentPlayerFullIDArray` - four instructions, but they are the pointer set-up, +both guard reads and the loop's end compare, so they carry the whole loop with them. No spelling of +the condition moves it: swapping the operands fixes the base and breaks the comparison order, and +nine other forms (nested `if`, De Morgan, casts, `ptr->`, a local for either read) compile identically. + +The decompilation says why. The original walks one pointer and reaches the second array off it: + +```c +_currentPlayer = DAT_GameSynchronyState.currentPlayerFullIDArray + 1; +... +if (*_currentPlayer != -1 || _currentPlayer[0x1b] != 0) { ... } +_currentPlayer = _currentPlayer + 1; +``` + +`0x1b` is the distance between the two arrays in ints. Writing that - a pointer local for the first +array with the second read as `currentPlayerFullID[0x1b]` - took **both functions to 100%**. A pointer +local for the first read *alone*, with the second still by name, changes nothing: both reads have to +go through the one pointer. Keep the magic index, with a comment; it is the only form that matches. + +### Three shared-tail rewrites that the decompilation justifies and that still measure worse + +The decompilation shows a shared tail for all three of these, and reproducing it without `goto` lost +every time. Read them before spending a day on the same idea: + +- `processFoodConsumption` has **one** `processResourceLoss` call with the type in a variable where we + write four calls with literal types. Setting a `ResourceType` and calling once after the loop is + exactly the original's shape and measured **83.7% -> 59.7%**: the extra local swaps the zero + constant and the loop counter between `ebp` and `ebx`, renaming twenty lines. +- `computeBuildingCategoryEntryPointAndDestroyEarlierBuilding` has one `destroyBuilding` call shared by + five arms and one `buildingEntry` read shared by nine. Hoisting both (an `existingBuildingID` + variable plus one `category != CAMPGROUND` re-test) lands within one instruction of the original's + length, 177 against 178, and still loses: the join block stays in the middle, so the alignment does + not recover. +- `resetVariousCountsAndStatisticsAndStartGoodsAndResources` has one `startResources[0xf] = gold * + multiplier` store shared by the human and AI arms. Writing the table value into a local in each arm + so the tails are identical text does not get MSVC to cross-jump them (92.4% -> 91.0%). + +A pointer local is not a general fix for the base anchor either: `PlayerData*` locals leave +`processPeasantSpawnAndDespawnCycle`'s anchor to move with the shared store exactly as before, and in +`spawnMotherOrChild` they remove the three `global - this` slots the original has, taking the frame +further from `sub esp, 0x18` rather than closer. + +### Read the decompilation, not only the disassembly, before calling a function allocator-bound + +Four of the finds above came from `decompile_function`, not from the interleaved reccmp diff, and two +of them were worth 100%. The decompiler names the original's locals and pointers, which is exactly the +information the asm hides: `processFoodConsumption`'s AI branch is +`(currentPopulation * 180) / 100` with `currentPopulation` as the local, not +`(foodClockSpeed * 60) / 100` - the same value, but MSVC reaches the original's instruction sequence +only from the first spelling. `updateCrowding`'s pointer is anchored at `currentPopulation`, which is +why its base is 0x2100 away from the field it stores to. + +## reccmp can score a byte-identical function below 100% + +`renderDebugDataCurrentPlayerData` scores 96.0% and every single one of the 41 diff lines is this: + + -push I::instance+424671 (OFFSET) + +push 0xffffff + +Both sides are the same four bytes. `0xffffff` is opaque white, a colour argument to the text calls, +and reccmp renders it as an address because in the *original* image that value lands inside +BuildingsState; in ours it does not. Verified with `disassemble_function 0x0045A060`, which shows +`PUSH 0xffffff` throughout. The function is byte-identical to the original and no source change can +raise its score. `tools/reimplementation-helper/batch/diff_falsesym.py` finds this class; it is the +only function in GameStateStructures affected, so nothing else below 100% is a tooling artifact. + +The reverse mistake looks the same in the diff and *is* a real bug: an operand that should be an +address, written as a literal in our source. Always confirm against the original disassembly. + +## spawnMotherOrChild: ebx goes to the anchor instead of the loop counter (54.6%, closed) + +The whole gap is one allocator decision. The original keeps `spawnBudget` in `ebx` and the +`playerDataArray` anchor in scratch `edx`, reloading it after calls. Ours does the opposite: the +anchor takes `ebx` (callee-saved, so it survives `spawnUnit` without a reload - arguably better +code) and `spawnBudget` spills, which is the 7th stack slot and the `sub esp, 0x1c` against the +original's `0x18`. Use that frame size as a free oracle: `0x18` means the counter stayed in a +register. + +Measured and ineffective - all four produce a *byte-identical* instruction stream to the current +form, so the shape is canonicalised before allocation: + +* `int buildingID = 0;` before the budget, as the decompilation has it, with an empty `for` init +* `spawnBudget` declared outside the `playerID` loop (a longer live range does not buy a register) +* both of the above +* both, with `buildingID` also at function scope + +Spelling the inner-loop `popularity` read as `this->` instead of `DAT_GameState::instance` *does* +reach the original's `sub esp, 0x18`, because it drops the `0xfffcf0ec` base to one register instead +of two - but that base exists in the original, so the frame matches by compensation and reccmp +measured 54.5%, a slight regression. quick_diff ranked it 0.751 over 0.739; trust reccmp. + +## spawnPoisonCloudsAroundBuilding: the memory decrement (92.4%, closed) + +The only gap is the loop tail. The original folds it to a read-modify-write plus a fresh re-read: + + sub dword ptr [esp + 0x10], 1 + cmp dword ptr [esp + 0x10], 0 + +while we keep the value in a register (`mov eax, mem; sub eax, 1; test eax, eax; mov mem, eax`). The +peephole needs the load/sub/store contiguous, and our scheduler puts the `test` between the sub and +the store. The two `continue`s force the decrement into the `for` header, so the obvious forms were +already known to canonicalise; rewriting the `continue`s as nested `if`s to put the decrement at the +bottom of the body - the decompilation's own shape, and the one the peephole wants - measured +*worse* (0.905 against 0.950) and still did not produce `sub dword ptr`. + +## processPeasantSpawnAndDespawnCycle: do not hoist the popularity read (84.0%, closed) + +The original computes `popularity / 500` twice, once per arm. Hoisting it into one local and taking +the shared `vclockIncrement` store - the `processFoodConsumption` fix, applied here - moves the base +anchor from `0x347a4` to `0x36978` and drops the score from 0.92 to 0.68. The duplicated division is +load-bearing: it is what keeps the anchor on `vclockIncrement`. + +## A per-file /Os does not reach the block layout (disproven) + +`cmake/compiler-flags-gl.txt` sets `/GL` per file, so the same mechanism can carry any flag: + + file_dependent_read_list(".../compiler-flags-os.txt" COMPILER_FLAG_OS_FILES) + set_source_files_properties(${COMPILER_FLAG_OS_FILES} PROPERTIES COMPILE_OPTIONS /Os) + +That looked like the way to give `computeBuildingCategoryEntryPointAndDestroyEarlierBuilding` the +original's basic-block layout without the cost a global `/Os` was measured to have. It does not +work. With `/O2 /Os`, and then with `/O2 /O1`, the reccmp diff for that function is identical to +`/O2` alone - all 297 lines, 33.5% / 32.8% every time. The `.obj` hash does change, which is +misleading; it is the command line recorded in the debug section, not the code. MSVC resolves the +trailing size flag to nothing more than `/Os` over `/O2`'s `/Ot`, and that bit does not move this +function's layout. + +So the earlier "`/Os` reproduces the layout" note was a whole-build effect (different inlining +elsewhere), not something a per-file flag can reach. The layout difference - the common tail of the +if/else-if chain laid out after the first arm, which turns the leading `jne` into +0x136 where the +original has +0xb4 and so changes every displacement after it - remains unreachable from the source +or from per-file flags. + +## /GL does nothing for a function that makes no reimplemented member call + +`computeBuildingCategoryEntryPointAndDestroyEarlierBuilding`, +`processPeasantSpawnAndDespawnCycle`, `spawnMotherOrChild`, `spawnPoisonCloudsAroundBuilding` and +`updateCrowding` were never in `cmake/compiler-flags-gl.txt`. Adding all five changes nothing: every +score in the build list is identical to the digit and the average stays at 97.76%. `/GL` earns its +place by removing the redundant `mov ecx, offset` before a call into another reimplemented method, +so it can only pay where such a call exists and the callee's resolver is on. `updateCrowding` has no +calls at all, and the others call out to methods that are not in the build list. It does not +re-roll register allocation inside a function, which is what these five actually need. Do not add +entries to that list speculatively - measure, and leave it out when the number does not move. + +## resetVariousCountsAndStatisticsAndStartGoodsAndResources: a one-byte encoding (92.2%, closed) + +The original loads the current player's slot into `edx`, we use `eax`: + + -mov edx, dword ptr [GameSynchronyState::instance+1089140] imul edx, edx, 0x39f4 + +mov eax, dword ptr [GameSynchronyState::instance+1089140] imul eax, eax, 0x39f4 + +`mov eax, [abs]` is the one-byte-shorter `A1` form, so our version of that line is a byte shorter, +which moves the alignment padding before the `startResources` copy loop (the original pads with a +three-byte `lea esp, [esp]`, we emit `jmp 0x3` over a `lea ecx, [ecx]`) and renames a register in +the `_memset` call set-up. One encoding choice, three diff hunks. Spelling the index as its own +local, taking a `const PlayerData*` to the source player, and reading through `this->` instead of +the global all produce a byte-identical function (the `this->` form measures worse, 0.819 against +0.924). MSVC picks the destination register for a load it is about to overwrite, and the source +cannot name it. + +## The /GS security cookie is a permanent ~4% floor + +`playWAVSFX` (96.2%) and `playSpeechSFX` (96.4%) are finished. Their entire remaining diff is the +`/GS` stack-cookie code that MSVC adds around a function with a large local buffer: + + -mov eax, dword ptr [StructResolver::Instance::instance (DATA)] + +mov eax, dword ptr [__security_cookie (DATA)] + -call + +call __security_check_cookie (FUNCTION) + +Each module gets its own cookie, so the original reads the game's at 0x00B94220 and we read our +DLL's. The `__security_check_cookie` call normalizes away; the cookie *load* does not, which is why +both sit at normalized 96.0% and cannot go higher. Any reimplemented function with a stack buffer +big enough to trigger `/GS` carries the same floor. + +The project already declares the game's cookie - `Globals/MSVC_SecurityCookie.hpp`, address +0x00B94220 - and `src/symbols/data.asm` can publish absolute symbols (`SYM EQU 00B94220h`), so +redirecting the compiler's reference looks like it should work. It does not: + + PUBLIC ___security_cookie + ___security_cookie EQU 00B94220h + + LIBCMT.lib(gs_cookie.obj) : error LNK2005: ___security_cookie already defined in data.asm.obj + +The CRT owns that symbol. Getting past it needs `/FORCE:MULTIPLE` on the whole link, with undefined +symbol selection, and the `OpenSHC.exe` target would then read a cookie at an address that only +exists inside the game's process. Not worth 4% on two functions - but note `/GS-` is *not* the +answer either, because the original has the cookie code and dropping it loses more than it gains. + +`playSFXAtLocation` is likewise done at 98.1% / normalized 100%: its only diff is the call targets +of the CRT helpers `_CIsqrt` and `_ftol2_sse`. + +## updateCrowding: the anchor and the store count cannot both be had (37.7%) + +Correcting an earlier note that said the anchor was wrong here - in the current one-store form the +anchor is **right**. The original (`disassemble_function 0x00458E60`) is: + + add ecx, 0x368b4 ; anchor on currentPopulation, displacement 0 + mov ebx, 8 ; a countdown, no playerID + xor edi, edi ; a zero register, before the loop + mov esi, dword ptr [ecx - 0x210c] ; populationCap + mov eax, dword ptr [ecx] ; currentPopulation + ... + mov dword ptr [ecx - 0x2100], edi ; crowding = 0, four stores, one per arm + ... + add ecx, 0x39f4 + sub ebx, 1 + jnz + +We match the anchor, the countdown, the pointer stride and both reads. The gap is that the original +assigns the *field* in each of the four arms, where we compute into a local and store once. The two +forms measure: + +* one store, value in `eax` -> **37.7%**, anchor `0x368b4` correct +* four field stores -> 33.3%, anchor moves to `0x347b4`, i.e. onto `crowding` itself + +The four stores drag the anchor because our zeros are immediates (`mov dword ptr [ecx], 0`), and an +immediate store is shortest at displacement 0, so MSVC bases on the field being stored. The original +pays nothing for a displacement because its zero is in `edi`, which is also why it can spell +`currentPopulation <= 0` as `cmp eax, edi` instead of our `test eax, eax`. Giving the zero a name to +force it into a register does not work - MSVC constant-propagates it straight back to an immediate: + +* `int noCrowding = 0;` inside the loop, used as the store source -> 33.3% +* the same declared before the loop, live range spanning it -> 33.3% + +So the choice is the correct anchor or the correct store count, and the anchor is worth 4.4 points +more. Keep the one-store form. diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index d0c81b8d5d..d646b7f7dc 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -16,4 +16,7 @@ src/OpenSHC/Audio/SFX/SFXState/playUnitSpeech.cpp src/OpenSHC/Game/GameStateStructures/getSellResourceAmount.cpp src/OpenSHC/Map/TileMapState/determineBuildingPlacementRotation.cpp src/OpenSHC/Map/Navigation/DirectionAlgorithmState/getMouseVectorLengthBasedOnDirection.cpp -src/OpenSHC/Input/MouseState/updateRightDragCameraControl.cpp \ No newline at end of file +src/OpenSHC/Input/MouseState/updateRightDragCameraControl.cpp +src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp +src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp +src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp new file mode 100644 index 0000000000..291cc7847f --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp @@ -0,0 +1,155 @@ +#include "../GameStateStructures.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" +#include "OpenSHC/UI/Enums/MenuViewType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Game { + + using Player::PlayerDataBuildingCategoryEnum; + using UI::Enums::MenuViewType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00456FD0 + void GameStateStructures::computeBuildingCategoryEntryPointAndDestroyEarlierBuilding( + uint buildingID, int playerID, PlayerDataBuildingCategoryEnum category) + { + int entryX = 0; + int entryY = 0; + DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; + if (category == Player::PDBCE_KEEP) { + if ((int)this->playerDataArray[playerID].keep.id > 0 + && this->playerDataArray[playerID].keep.id != buildingID) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + this->playerDataArray[playerID].keep.id); + } + short orientation = DAT_BuildingsState::instance.buildings[buildingID].orientation; + entryX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + entryY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + if (orientation == 4) { + entryX = entryX + 3; + --entryY; + } else if (orientation == 2) { + --entryX; + entryY = entryY + 3; + } else if (orientation == 6) { + entryX = entryX + 7; + entryY = entryY + 3; + } else { + entryX = entryX + 3; + entryY = entryY + 7; + } + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; + if (this->playerDataArray[playerID].stockpile.id != 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + this->playerDataArray[playerID].stockpile.id); + } + } + } else if (category == Player::PDBCE_MARKETPLACE) { + if (this->playerDataArray[playerID].marketplace.id > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + this->playerDataArray[playerID].marketplace.id); + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_ENGINEERSGUILD) { + if (this->playerDataArray[playerID].engineersGuild.id > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + this->playerDataArray[playerID].engineersGuild.id); + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_TUNNELERSGUILD) { + if (this->playerDataArray[playerID].tunnelersGuild.id > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + this->playerDataArray[playerID].tunnelersGuild.id); + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_STOCKPILE) { + // a storage building of this kind already exists, keep the old entry + if (this->playerDataArray[playerID].stockpile.id > 0) { + return; + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_ARMORY) { + // a storage building of this kind already exists, keep the old entry + if (this->playerDataArray[playerID].armory.id > 0) { + return; + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_GRANARY) { + // a storage building of this kind already exists, keep the old entry + if (this->playerDataArray[playerID].granary.id > 0) { + return; + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_BARRACKS) { + if (this->playerDataArray[playerID].barracks.id > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + this->playerDataArray[playerID].barracks.id); + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_MERCENARYPOST) { + if (this->playerDataArray[playerID].mercenaryPost.id > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + this->playerDataArray[playerID].mercenaryPost.id); + } + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_OILSMELTER) { + entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + } else if (category == Player::PDBCE_CAMPGROUND) { + short orientation = DAT_BuildingsState::instance.buildings[buildingID].orientation; + entryX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; + entryY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + if (orientation == 4) { + entryX = entryX + 3; + entryY = entryY + 6; + } else if (orientation == 2) { + entryY = entryY + 3; + } else if (orientation == 6) { + entryX = entryX + 6; + entryY = entryY + 3; + } else { + entryX = entryX + 3; + } + } + if (entryX < 0) { + return; + } + if (entryX >= 400) { + return; + } + if (entryY < 0) { + return; + } + if (entryY >= 400) { + return; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[entryY * 400 + entryX] != 0) { + (&this->playerDataArray[playerID].keep)[category].id = buildingID; + (&this->playerDataArray[playerID].keep)[category].yEntry = entryY; + (&this->playerDataArray[playerID].keep)[category].xEntry = entryX; + (&this->playerDataArray[playerID].keep)[category].tileEntry + = DAT_ViewportRenderState::instance.translationMatrix[entryY].addXgetTile + entryX; + (&this->playerDataArray[playerID].keep)[category].areaEntry + = (short)DAT_TileMapState::instance + .PathConnectionLayer[(&this->playerDataArray[playerID].keep)[category].tileEntry]; + DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 0; + } + } + +} +} diff --git a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp new file mode 100644 index 0000000000..f9df446adc --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp @@ -0,0 +1,125 @@ +#include "../GameStateStructures.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Game/GameMode2.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].field639_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 + = this->playerDataArray[playerID].currentPopulation - 1; + } + if (this->playerDataArray[playerID].ladyIDUnk != 0 + && DAT_UnitsState::instance.units[this->playerDataArray[playerID].ladyIDUnk].uid + == this->playerDataArray[playerID].someUnitIDSelfRef) { + this->playerDataArray[playerID].currentPopulation + = this->playerDataArray[playerID].currentPopulation - 1; + } + 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].currentPopulation - 1; + } + 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 blessedPercentage; + if (this->playerDataArray[playerID].unblessedPeopleCountUnk + + this->playerDataArray[playerID].blessedPeopleCountUnk + == 0) { + blessedPercentage = 100; + } else { + blessedPercentage = (this->playerDataArray[playerID].blessedPeopleCountUnk * 100) + / (this->playerDataArray[playerID].unblessedPeopleCountUnk + + this->playerDataArray[playerID].blessedPeopleCountUnk); + } + 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 + = this->playerDataArray[playerID].populationGrowthStatisticCounter + 1; + 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 + = this->playerDataArray[playerID].tacticalPowersBarLevel + 1; + } + if (this->playerDataArray[playerID].goldDonation > 0) { + int goldIncrement = this->playerDataArray[playerID].goldDonation; + if (goldIncrement > 10) { + goldIncrement = 10; + } + this->playerDataArray[playerID].currentResources[15] + = this->playerDataArray[playerID].currentResources[15] + goldIncrement; + this->playerDataArray[playerID].goldDonation + = 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/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp new file mode 100644 index 0000000000..3e16c8e440 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp @@ -0,0 +1,165 @@ +#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/Resources/ResourceType.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.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; + + // 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)(); + for (int playerID = 1; playerID < 9; ++playerID) { + if (this->playerDataArray[playerID].keep.id <= 0 || this->playerDataArray[playerID].campground.id <= 0) { + continue; + } + this->playerDataArray[playerID].foodStorageLevel = -1; + if (this->playerDataArray[playerID].foodTypesInStock <= 0) { + this->playerDataArray[playerID].foodTypesCurrentlyEaten = 0; + continue; + } + int foodClockSpeed = this->playerDataArray[playerID].currentPopulation * 3; + this->playerDataArray[playerID].foodClockSpeed = foodClockSpeed; + if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY + && MACRO_CALL_MEMBER(Synchrony::GameSynchronyState_Func::isAIPlayer, DAT_GameSynchronyState::ptr)( + playerID) + != FALSE) { + this->playerDataArray[playerID].foodClockSpeed = (foodClockSpeed * 60) / 100; + } + switch (this->playerDataArray[playerID].rationsSetting) { + case 0: + this->playerDataArray[playerID].foodClockSpeed = 0; + this->playerDataArray[playerID].foodTypesCurrentlyEaten = 0; + break; + case 1: + this->playerDataArray[playerID].foodClockSpeed = this->playerDataArray[playerID].foodClockSpeed / 2; + break; + case 3: + this->playerDataArray[playerID].foodClockSpeed + = (this->playerDataArray[playerID].foodClockSpeed * 3) / 2; + break; + case 4: + this->playerDataArray[playerID].foodClockSpeed = this->playerDataArray[playerID].foodClockSpeed * 2; + } + int storageLevel = -1; + if (this->playerDataArray[playerID].foodClockSpeed != 0) { + storageLevel = ((15000 / this->playerDataArray[playerID].foodClockSpeed) + * this->playerDataArray[playerID].totalFood) + / 800; + this->playerDataArray[playerID].foodStorageLevel = storageLevel; + } + if (DAT_GameState::instance.mapAndTime.monthChanged != 0) { + short leftover = (short)storageLevel; + short lastMonthLeftover = this->playerDataArray[playerID].foodStorageLevelLastMonth; + this->playerDataArray[playerID].foodStorageLevelLastLastMonth = lastMonthLeftover; + this->playerDataArray[playerID].foodStorageLevelLastMonth = leftover; + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && DAT_GameState::instance.mapAndTime.singlePlayerHasKeepAndGranary != FALSE) { + if (this->playerDataArray[playerID].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)( + "food_warning4.wav"); + } + ++DAT_CounterFoodWarningInterval::instance; + if ((int)DAT_CounterFoodWarningInterval::instance > 2) { + DAT_CounterFoodWarningInterval::instance = 0; + } + } else if (leftover < 2 && leftover != lastMonthLeftover + && this->playerDataArray[playerID].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)( + "food_warning2.wav"); + } + } 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)( + "food_warning3.wav"); + } + } + } + } + this->playerDataArray[playerID].foodClock + = this->playerDataArray[playerID].foodClock + this->playerDataArray[playerID].foodClockSpeed; + if (this->playerDataArray[playerID].foodClock > 15000) { + this->playerDataArray[playerID].foodClock = 0; + this->playerDataArray[playerID].foodTypesCurrentlyEaten + = this->playerDataArray[playerID].foodTypesInStock; + while (true) { + this->playerDataArray[playerID].foodTypeToBeEatenNext + = this->playerDataArray[playerID].foodTypeToBeEatenNext + 1; + if (this->playerDataArray[playerID].foodTypeToBeEatenNext > 3) { + this->playerDataArray[playerID].foodTypeToBeEatenNext = 0; + } + if (this->playerDataArray[playerID].foodTypeToBeEatenNext == 0 + && this->playerDataArray[playerID].breadCount > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_BREAD, 1, 0); + break; + } + if (this->playerDataArray[playerID].foodTypeToBeEatenNext == 1 + && this->playerDataArray[playerID].cheeseCount > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_CHEESE, 1, 0); + break; + } + if (this->playerDataArray[playerID].foodTypeToBeEatenNext == 2 + && this->playerDataArray[playerID].meatCount > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_MEAT, 1, 0); + break; + } + if (this->playerDataArray[playerID].foodTypeToBeEatenNext == 3 + && this->playerDataArray[playerID].appleCount > 0) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::processResourceLoss, + DAT_BuildingsState::ptr)(playerID, Resources::RT_APPLE, 1, 0); + break; + } + } + } + } + } + +} +} diff --git a/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp new file mode 100644 index 0000000000..2dfd626ec9 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp @@ -0,0 +1,137 @@ +#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/Units/UnitsState.func.hpp" +#include "OpenSHC/UI/Helpers.func.hpp" +#include "OpenSHC/Audio/SFX/SpeechEffectID.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.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" +#include "OpenSHC/Globals/DAT_TroopDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Game { + + using Audio::SFX::SpeechEffectID; + using Map::MapType2; + using Map::Buildings::BuildingType; + using Map::Units::UnitType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0045B4A0 + void GameStateStructures::processPeasantSpawnAndDespawnCycle() + { + // handle peasant (de)spawning + if (DAT_GameCore::instance.gameMode_2 == GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + return; + } + for (int playerID = 1; playerID < 9; ++playerID) { + if (this->playerDataArray[playerID].lordKilledByPlayerID != 0) { + continue; + } + if (this->playerDataArray[playerID].playerDeathRelated != 0 + && DAT_GameSynchronyState::instance.currentGameMode == GM_SOLITARY) { + continue; + } + if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY) { + this->playerDataArray[playerID].vclockIncrement + = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSkirmish[this->playerDataArray[playerID].popularity / 500]; + } else { + int popularityIndex = this->playerDataArray[playerID].popularity / 500; + if (this->playerDataArray[playerID].currentPopulation > 100) { + this->playerDataArray[playerID].vclockIncrement + = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSolitaryPopMoreThan100[popularityIndex]; + } else { + this->playerDataArray[playerID].vclockIncrement + = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSolitaryPopLessThan101[popularityIndex]; + } + } + this->playerDataArray[playerID].vclock + = this->playerDataArray[playerID].vclockIncrement + this->playerDataArray[playerID].vclock; + if (this->playerDataArray[playerID].vclock >= 4000) { + this->playerDataArray[playerID].vclock = 4000; + } else if (this->playerDataArray[playerID].vclock < 0) { + this->playerDataArray[playerID].vclock = 0; + } + if (this->playerDataArray[playerID].vclockIncrement == 0) { + this->playerDataArray[playerID].vclock = 2000; + } + if (this->playerDataArray[playerID].keep.id <= 0) { + continue; + } + int campgroundID = this->playerDataArray[playerID].campground.id; + if (campgroundID <= 0) { + continue; + } + if (this->playerDataArray[playerID].vclock >= 4000) { + if (this->playerDataArray[playerID].currentPopulation < this->playerDataArray[playerID].populationCap + && this->playerDataArray[playerID].availablePeasantsOrHousedPeasants < 24) { + // Havent reached peasant limit. + int x = this->playerDataArray[playerID].campground.xEntry; + int y = this->playerDataArray[playerID].campground.yEntry; + this->playerDataArray[playerID].vclock = 2000; + int peasantUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + playerID, playerID, x * 8, y * 8, + DAT_BuildingsState::instance.buildings[campgroundID].terrainHeightUnk, Map::Units::UT_PEASANT); + if (peasantUnitID != 0) { + // Successfully spawned peasant. + DAT_UnitsState::instance.units[peasantUnitID].animationCycleNumber + = DAT_UnitsState::instance.units[peasantUnitID].fixedRng & 0xf; + DAT_UnitsState::instance.units[peasantUnitID].isDisappearingUnk = 1; + if (DAT_GameCore::instance.gameMode_2 == GM_CRUSADER_TUTORIAL) { + MACRO_CALL(UI::Helpers_Func::SetTutorialBuildingActionState)(9, Map::Buildings::BT_HOVEL); + } + } + if (this->playerDataArray[playerID].field26_0x42 == 1) { + this->playerDataArray[playerID].field26_0x42 = 0; + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() + == FALSE) { + // people are coming to the castle + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( + Audio::SFX::SEID_POP_IMMIGRATE); + } + } + } + } else if (this->playerDataArray[playerID].vclock <= 0 + && this->playerDataArray[playerID].availablePeasantsOrHousedPeasants + + this->playerDataArray[playerID].populationRelatedCrowdingCountUnk + > 4) { + this->playerDataArray[playerID].vclock = 2000; + // let a peasant disappear? + MACRO_CALL_MEMBER(GameStateStructures_Func::despawnPeasantOrWorker, this)(playerID); + if (this->playerDataArray[playerID].field26_0x42 == 0) { + this->playerDataArray[playerID].field26_0x42 = 1; + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID + && MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() + == FALSE) { + // sound is probably about peasants leaving the castle + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( + Audio::SFX::SEID_POP_EMIGRATE); + } + } + } + } + } + +} +} diff --git a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp new file mode 100644 index 0000000000..0971363964 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp @@ -0,0 +1,127 @@ +#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/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].someCount40 = 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 = 0x3c; + this->playerDataArray[playerID].someCountdown01 = 0; + this->playerDataArray[playerID].playerDeathRelated = 0; + this->playerDataArray[playerID].previousBlessedPeoplePercentage = 0; + this->playerDataArray[playerID].popularityReligionBasedDiv25 = 0; + this->playerDataArray[playerID].someCount48 = 0; + this->playerDataArray[playerID].someCount49 = 0; + this->playerDataArray[playerID].someCount50 = 0; + this->playerDataArray[playerID].someCount51 = 0; + this->playerDataArray[playerID].someCount52 = 0; + this->playerDataArray[playerID].someCount53 = 0; + this->playerDataArray[playerID].someCount54 = 0; + this->playerDataArray[playerID].someCount55 = 0; + this->playerDataArray[playerID].someCount56 = 0; + this->playerDataArray[playerID].someCount57 = 0; + this->playerDataArray[playerID].someCount58 = 0; + this->playerDataArray[playerID].someCount59 = 0; + this->playerDataArray[playerID].someCount60 = 0; + this->playerDataArray[playerID].keepEnclosementRelatedCountdown = 0; + this->playerDataArray[playerID].defensesDamagedByPlayer = 0; + for (int resourceType = 0; resourceType < 25; ++resourceType) { + this->playerDataArray[playerID].startResources[resourceType] + = this->mapAndTime.startGoods[resourceType]; + } + MACRO_CALL(OS_Func::_memset)(this->playerDataArray[playerID].snoozedBuildings, 0, 100); + this->playerDataArray[playerID].troopsKilled = 0; + this->playerDataArray[playerID].troopsLost = 0; + this->playerDataArray[playerID].weightedLosses = 0; + this->playerDataArray[playerID].field25_0x40 = 0; + this->playerDataArray[playerID].field26_0x42 = 0; + if (this->mapAndTime.editScenarioExtraOptions != 0) { + this->playerDataArray[playerID].taxesSetting = this->mapAndTime.scenarioTaxesSetting; + int scenarioRations = this->mapAndTime.scenarioRationsSetting; + this->playerDataArray[playerID].rationsSetting2 = scenarioRations; + this->playerDataArray[playerID].rationsSetting3 = scenarioRations; + this->playerDataArray[playerID].rationsSetting = scenarioRations; + this->playerDataArray[playerID].currentResources[0xf] = 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[0xf] + = DAT_RenderingDefinedData::instance + .field451_0x53bb4[(DAT_GameSynchronyState::instance.skirmishCurrentAdvantageBalance + + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) + * 2 + + 0xc] + * gameTypeMultiplier; + } else if (DAT_GameSynchronyState::instance.currentAIArray[playerID] != 0) { + this->playerDataArray[playerID].startResources[0xf] + = DAT_RenderingDefinedData::instance + .field451_0x53bb4[(DAT_GameSynchronyState::instance.skirmishCurrentAdvantageBalance + + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) + * 2 + + 0xd] + * gameTypeMultiplier; + } + } + DAT_GameState::instance.mapAndTime.populationIndex = 0; + this->playerDataArray[playerID].beforeLastMonthsGold + = (short)this->playerDataArray[playerID].currentResources[0xf]; + this->playerDataArray[playerID].lastMonthsGold + = (short)this->playerDataArray[playerID].currentResources[0xf]; + this->playerDataArray[playerID].aiControlStatusRelated = -1000; + for (int foodType = 0; foodType < 4; ++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 = 0x30; + this->playerDataArray[playerID].tacticalPowersBarLevel = 0; + this->playerDataArray[playerID].goldDonation = 0; + } + } + +} +} diff --git a/src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp b/src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp new file mode 100644 index 0000000000..d523af8c19 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp @@ -0,0 +1,145 @@ +#include "../GameStateStructures.func.hpp" + +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingLogicalState.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Game { + + using Map::Buildings::BuildingLogicalState; + using Map::Buildings::BuildingType; + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00458280 + void GameStateStructures::spawnMotherOrChild() + { + if (DAT_GameCore::instance.gameMode_2 == GM_EDITOR) { + return; + } + if (DAT_GameCore::instance.gameMode_2 == GM_SIEGE_THAT) { + return; + } + for (int playerID = 1; playerID < 9; ++playerID) { + // if has keep and campground + if (this->playerDataArray[playerID].keep.id <= 0 || this->playerDataArray[playerID].campground.id <= 0) { + continue; + } + // Only spawn in non-multiplayer + if (DAT_GameSynchronyState::instance.currentGameMode == GM_SKIRMISH_SINGLE_PLAYER) { + if (DAT_GameState::instance.playerDataArray[playerID].someCount46 != 0) { + // if total enemy troop value < 101 + if (DAT_GameState::instance.playerDataArray[playerID].totalEnemyTroopValue > 100) { + DAT_GameState::instance.playerDataArray[playerID].someCount46 = 0; + continue; + } + } else { + // if total enemy troop value < 41 + if (DAT_GameState::instance.playerDataArray[playerID].totalEnemyTroopValue > 40) { + continue; + } + DAT_GameState::instance.playerDataArray[playerID].someCount46 = 1; + } + } else if (this->playerDataArray[playerID].sumOfTotalEnemyUnitsCount > 0) { + continue; + } + int spawnBudget + = (75 - this->playerDataArray[playerID].someCount03) - this->playerDataArray[playerID].someCount02; + for (int buildingID = 0; buildingID < DAT_BuildingsState::instance.maxBuildingsCount; ++buildingID) { + if (DAT_BuildingsState::instance.buildings[buildingID].logicalState == (BuildingLogicalState)0 + || DAT_BuildingsState::instance.buildings[buildingID].logicalState == Map::Buildings::BLS_REMOVE + || DAT_BuildingsState::instance.buildings[buildingID].owner != playerID + || DAT_BuildingsState::instance.buildings[buildingID].fireDuration != 0 + || (DAT_BuildingsState::instance.buildings[buildingID].buildingType != Map::Buildings::BT_HOVEL + && DAT_BuildingsState::instance.buildings[buildingID].buildingType + != Map::Buildings::BT_HOUSE)) { + continue; + } + if (spawnBudget <= 0) { + break; + } + // if building has no unit + if (DAT_BuildingsState::instance.buildings[buildingID].unitRefID == 0) { + short motherEntryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + if (motherEntryX > 0) { + short motherEntryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + if (motherEntryY > 0) { + int motherTile + = DAT_ViewportRenderState::instance.translationMatrix[motherEntryY].addXgetTile + + motherEntryX; + int motherUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, + DAT_UnitsState::ptr)(playerID, playerID, motherEntryX * 8, motherEntryY * 8, + DAT_TileMapState::instance.HeightLayer[motherTile], Map::Units::UT_MOTHER); + if (motherUnitID != 0) { + // store in unitRef + DAT_BuildingsState::instance.buildings[buildingID].unitRefID = (short)motherUnitID; + DAT_BuildingsState::instance.buildings[buildingID].unitRefUID + = DAT_UnitsState::instance.units[motherUnitID].uid; + DAT_UnitsState::instance.units[motherUnitID].workplaceBuildingUID + = DAT_BuildingsState::instance.buildings[buildingID].uid; + DAT_UnitsState::instance.units[motherUnitID].workplaceBuildingID_1 = (short)buildingID; + --spawnBudget; + DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown + = (char)((DAT_BuildingsState::instance.buildings[buildingID].fireRelatedRNG1 >> 10) + % 3) + + 1; + } + } + } + } else if (DAT_GameState::instance.playerDataArray[playerID].popularity / 100 >= 50) { + // if popularity is higher than 49 + DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown + = DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown - 1; + if ((char)DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown <= 0 + && (short)(char)DAT_BuildingsState::instance.buildings[buildingID].maxOccupants + != DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount) { + short childEntryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; + DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown + = (char)((DAT_BuildingsState::instance.buildings[buildingID].fireRelatedRNG1 >> 10) % 3) + + 1; + if (childEntryX > 0) { + short childEntryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; + if (childEntryY > 0) { + int childTile + = DAT_ViewportRenderState::instance.translationMatrix[childEntryY].addXgetTile + + childEntryX; + int childUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, + DAT_UnitsState::ptr)(playerID, playerID, childEntryX * 8, childEntryY * 8, + DAT_TileMapState::instance.HeightLayer[childTile], Map::Units::UT_CHILD); + if (childUnitID != 0) { + --spawnBudget; + DAT_BuildingsState::instance.buildings[buildingID] + .workerID[DAT_BuildingsState::instance.buildings[buildingID] + .currentEmployeeCount] = (short)childUnitID; + DAT_BuildingsState::instance.buildings[buildingID].workerUID + [DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount] + = DAT_UnitsState::instance.units[childUnitID].uid; + int workplaceUID = DAT_BuildingsState::instance.buildings[buildingID].uid; + // increment currentEmployeeCount of building + DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount + = DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount + 1; + DAT_UnitsState::instance.units[childUnitID].workplaceBuildingID_1 + = (short)buildingID; + DAT_UnitsState::instance.units[childUnitID].workplaceBuildingUID = workplaceUID; + } + } + } + } + } + } + } + } + +} +} diff --git a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp new file mode 100644 index 0000000000..9e31db7ced --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp @@ -0,0 +1,55 @@ +#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) + { + for (int attemptsLeft = 20; attemptsLeft > 0; attemptsLeft--) { + uint xPosition = SEC_RNG::instance.currentNumber2 % 30 - 15 + + (short)DAT_BuildingsState::instance.buildings[buildingID].x; + uint yPosition = (SEC_RNG::instance.currentNumber2 >> 8) % 30 - 15 + + (short)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) { + continue; + } + int tile = DAT_ViewportRenderState::instance.translationMatrix[yPosition].addXgetTile + xPosition; + if ((DAT_TileMapState::instance.LogicLayer[tile] & 0x4a5014b1U) != 0) { + 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, (byte)DAT_TileMapState::instance.EntityLayerLT25[tile + 0x13a10], 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 c51eb419db..4d394d7fff 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17039,7 +17039,7 @@ SHC_3BB0A8C1_0x00455D40 | 0.0% | Pending SHC_3BB0A8C1_0x00455D90 | 0.0% | Pending -SHC_3BB0A8C1_0x00455E10 | 0.0% | Pending +SHC_3BB0A8C1_0x00455E10 | 92.2% | Volatile reg naming differs SHC_3BB0A8C1_0x00455E40 | 0.0% | Pending @@ -17155,7 +17155,7 @@ SHC_3BB0A8C1_0x00456C50 | 0.0% | Pending SHC_3BB0A8C1_0x00456EF0 | 0.0% | Pending -SHC_3BB0A8C1_0x00456FD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00456FD0 | 33.5% | Join block placed after arm 1 SHC_3BB0A8C1_0x004572A0 | 0.0% | Pending @@ -17685,7 +17685,7 @@ SHC_3BB0A8C1_0x004580C0 | 0.0% | Pending SHC_3BB0A8C1_0x004580F0 | 0.0% | Pending -SHC_3BB0A8C1_0x00458280 | 0.0% | Pending +SHC_3BB0A8C1_0x00458280 | 54.6% | Budget spilled, base in EBX SHC_3BB0A8C1_0x00458360 | 0.0% | Pending @@ -17733,7 +17733,7 @@ SHC_3BB0A8C1_0x00458A70 | 0.0% | Pending SHC_3BB0A8C1_0x00458A80 | 0.0% | Pending -SHC_3BB0A8C1_0x00458AD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00458AD0 | 96.8% | Field read would move the anch SHC_3BB0A8C1_0x00458D38 | 0.0% | Pending @@ -17901,7 +17901,7 @@ SHC_3BB0A8C1_0x004598B0 | 0.0% | Pending SHC_3BB0A8C1_0x00459930 | 0.0% | Pending -SHC_3BB0A8C1_0x00459A20 | 0.0% | Pending +SHC_3BB0A8C1_0x00459A20 | 92.4% | loop counter kept in a reg SHC_3BB0A8C1_0x00459A40 | 0.0% | Pending @@ -18027,7 +18027,7 @@ SHC_3BB0A8C1_0x0045AEB0 | 0.0% | Pending SHC_3BB0A8C1_0x0045AF00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045AF30 | 0.0% | Pending +SHC_3BB0A8C1_0x0045AF30 | 98.0% | Reg alloc differs SHC_3BB0A8C1_0x0045B1C0 | 0.0% | Pending @@ -18037,7 +18037,7 @@ SHC_3BB0A8C1_0x0045B3E0 | 0.0% | Pending SHC_3BB0A8C1_0x0045B460 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045B4A0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045B4A0 | 84.0% | Shared store moves the anchor SHC_3BB0A8C1_0x0045B6B0 | 0.0% | Pending @@ -18147,17 +18147,17 @@ SHC_3BB0A8C1_0x0045CD10 | 0.0% | Pending SHC_3BB0A8C1_0x0045D060 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D080 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D080 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045D0B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D0C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D0C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D140 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D140 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D1A0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D1A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D200 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D200 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045D300 | 0.0% | Pending @@ -18327,41 +18327,41 @@ SHC_3BB0A8C1_0x0045D364 | 0.0% | Pending SHC_3BB0A8C1_0x0045D365 | 0.0% | Pending -SHC_3BB0A8C1_0x0045D370 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D3C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D3C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D430 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D430 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045D460 | 0.0% | Pending -SHC_3BB0A8C1_0x0045D690 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D690 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D6C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D6C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D740 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D740 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D890 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D890 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D8F0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D8F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D950 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D950 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D990 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D990 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045D9E0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045D9E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045DA20 | 0.0% | Pending +SHC_3BB0A8C1_0x0045DA20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045DA40 | 0.0% | Pending +SHC_3BB0A8C1_0x0045DA40 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045DA60 | 0.0% | Pending -SHC_3BB0A8C1_0x0045DA80 | 0.0% | Pending +SHC_3BB0A8C1_0x0045DA80 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045DAE0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045DAE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045DB40 | 0.0% | Pending +SHC_3BB0A8C1_0x0045DB40 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045DDF4 | 0.0% | Pending @@ -18709,7 +18709,7 @@ SHC_3BB0A8C1_0x0045EF84 | 0.0% | Pending SHC_3BB0A8C1_0x0045EF90 | 0.0% | Pending -SHC_3BB0A8C1_0x0045F080 | 0.0% | Pending +SHC_3BB0A8C1_0x0045F080 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045F0B4 | 0.0% | Pending @@ -18745,21 +18745,21 @@ SHC_3BB0A8C1_0x0045F0CC | 0.0% | Pending SHC_3BB0A8C1_0x0045F0CD | 0.0% | Pending -SHC_3BB0A8C1_0x0045F0D0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045F0D0 | 100.0% | Reimplemented, requires SHC_3BB0A8C1_0x0045F130 active SHC_3BB0A8C1_0x0045F120 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045F130 | 0.0% | Pending +SHC_3BB0A8C1_0x0045F130 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045F240 | 0.0% | Pending +SHC_3BB0A8C1_0x0045F240 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045F470 | 0.0% | Pending +SHC_3BB0A8C1_0x0045F470 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045F580 | 0.0% | Pending +SHC_3BB0A8C1_0x0045F580 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045F5B0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045F5B0 | 99.0% | Reimplemented, two cases where an "edx + ecx" is flipped to "ecx + edx" -SHC_3BB0A8C1_0x0045FDC0 | 0.0% | Pending +SHC_3BB0A8C1_0x0045FDC0 | 70.1% | Reimplemented, but heavy structural mismatch, seems functional SHC_3BB0A8C1_0x0045FEC0 | 0.0% | Pending @@ -18825,15 +18825,15 @@ SHC_3BB0A8C1_0x004613D0 | 0.0% | Pending SHC_3BB0A8C1_0x004613D4 | 0.0% | Pending -SHC_3BB0A8C1_0x004613E0 | 0.0% | Pending +SHC_3BB0A8C1_0x004613E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004614D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004614D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x004614F0 | 0.0% | Pending SHC_3BB0A8C1_0x00461520 | 0.0% | Pending -SHC_3BB0A8C1_0x00461550 | 0.0% | Pending +SHC_3BB0A8C1_0x00461550 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00461570 | 0.0% | Pending @@ -18879,23 +18879,23 @@ SHC_3BB0A8C1_0x004619C4 | 0.0% | Pending SHC_3BB0A8C1_0x004619C8 | 0.0% | Pending -SHC_3BB0A8C1_0x004619D0 | 0.0% | Pending +SHC_3BB0A8C1_0x004619D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00461A20 | 0.0% | Pending +SHC_3BB0A8C1_0x00461A20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00461B90 | 0.0% | Pending +SHC_3BB0A8C1_0x00461B90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00461CF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00461CF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00461E50 | 0.0% | Pending +SHC_3BB0A8C1_0x00461E50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00461F90 | 0.0% | Pending +SHC_3BB0A8C1_0x00461F90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x004620F0 | 0.0% | Pending +SHC_3BB0A8C1_0x004620F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00462150 | 0.0% | Pending +SHC_3BB0A8C1_0x00462150 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00462190 | 0.0% | Pending +SHC_3BB0A8C1_0x00462190 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00462340 | 0.0% | Pending diff --git a/tools/reimplementation-helper/batch/README.md b/tools/reimplementation-helper/batch/README.md index be86d74140..08f5ad1e02 100644 --- a/tools/reimplementation-helper/batch/README.md +++ b/tools/reimplementation-helper/batch/README.md @@ -31,6 +31,7 @@ Analysis helpers, for deciding *what* to change when a function is stuck below 1 | `diff_slots.py NAME` | Every `[esp + N]` slot with its access count on each side, and the slots only we use. Run after `diff_triage.py` reports `FRAME`. | | `diff_jcc.py [NAME]` | Comparisons whose operator is one strictness step off the original (`jl`/`jle`, `jg`/`jge`, `ja`/`jae`) or whose `cmp` operands are the other way round, with context. | | `diff_types.py [--run] [NAME]` | The non-comparison counterpart of `diff_jcc.py`: `movsx` against `movzx` for the same field (its signedness is wrong in the generated header), `add` against `sub` with the same magnitude (a sign error in a formula - how `2400/n + 40` was caught being `- 40`). It pairs only equal-length runs inside one reccmp block, so it stays quiet once the streams drift: it reports nothing across the 101 functions of `Game::GameStateStructures` and fires on both bugs when they are reintroduced. | +| `diff_anchor.py [--run] [NAME]` | Functions whose struct/loop base anchor differs from the original's (`lea esi, [ecx + 0x367e4]` against a different constant). Every displacement in the function then differs while the opcodes still line up, which reads as allocator noise - it is the whole gap between 83.7% and 61.7% on `processFoodConsumption`. The delta is the distance between the two fields MSVC chose; the decompilation names the original's as the pointer it starts the loop with. | | `diff_lea.py [--run] [NAME]` | `lea reg, [a + b]` on our side where the original has the destructive `add a, b`: `field = snapshotLocal + delta` has to keep the snapshot, `field = field + delta` updates the cached read in place. Drop the local when nothing between the read and the use writes the field - that was `updatePopularity`'s last 6.1%. `RLEA` is the reverse, a local the original keeps and we do not. | | `diff_signed.py [NAME-SUBSTRING]` | Comparisons the original makes signed and we make unsigned (or the reverse): `jg/jge/jl/jle` against our `ja/jae/jb/jbe` over the same `cmp`. Fix it by comparing through `(int)` in the source, not by editing the generated header, and re-run the tool to confirm - one instruction barely moves the percentage. | | `quick_diff.py FILE.cpp... [-q]` | Compile one file with `/FA` and diff it against the original function disassembled straight from `_original/` - about 6 seconds, no link and no reccmp, so a dozen source variants can be tried in the time of one build. Shows the whole original function, which reccmp's diff does not when ours is shorter. A triage loop: confirm a 1.0 with `reccmp_report.py --run`, and do not trust a *rise* of its ratio below about 0.6 - on a badly matching function difflib rewards a different alignment, and one such "gain" was a 10-point loss in reccmp. | diff --git a/tools/reimplementation-helper/batch/diff_anchor.py b/tools/reimplementation-helper/batch/diff_anchor.py new file mode 100644 index 0000000000..7ce6bcc8ea --- /dev/null +++ b/tools/reimplementation-helper/batch/diff_anchor.py @@ -0,0 +1,120 @@ +"""Find functions whose struct/loop base anchor differs from the original's. + +usage: diff_anchor.py [--run] [NAME-SUBSTRING] + +MSVC bases a run of accesses into one object on a single address and expresses every field as a +displacement off it: + + lea esi, [ecx + 0x367e4] ; the anchor + cmp dword ptr [esi - 0x201c], ebp + mov dword ptr [esi + 0xe0], -1 + +Which field lands at offset 0 is MSVC's choice, and when it picks a different one than the original +did, *every* displacement in the function differs while the opcodes still line up. That reads as +allocator noise in the diff and is the single most expensive mismatch in this codebase: the anchor +alone is the whole gap between 83.7% and 61.7% on `processFoodConsumption`. + +ANCHOR reports the two base constants and their difference, which is the distance between the two +fields MSVC chose. Look that offset up in the struct header to see which field the original anchored +on; the decompilation names it directly, as the pointer it starts the loop with +(`_playerData = &DAT_GameState.playerDataArray[1].foodClockSpeed`). + +What to do about a hit, in the order that has paid: + +1. Check whether a *recent source change* caused it. The anchor moves when a field loses stores - a + local for a repeatedly stored field, or an LTCG-enabled CSE, both do it. Such a change can be + correct and still cost 20 points; keep the form with the matching anchor and record why. +2. Otherwise the anchor is probably not reachable. Tested and ineffective on `updateCrowding`, + `processFoodConsumption` and `processPeasantSpawnAndDespawnCycle`: the global vs `this->` spelling + of any field, an extra read of the anchor field, casts on the index, declaration and statement + order, `PlayerData*` pointer locals, and even an `int*` local pointing straight at the field the + original anchors on - MSVC re-bases to the store target regardless. + +An exception worth knowing: where a condition reads two arrays of the *same* struct, reaching the +second off a pointer to the first does move the base, and took `showPopAndGoldPopup` and +`createStatsPopUpEntities` from 87-89% to 100%. See the cheat sheet. + +--run re-runs reccmp first (otherwise reccmp/dll/diff.json from the last run is used). +""" + +import re +import sys + +import common + +# `lea esi, [ecx + 0x367e4]`, `add esi, 0x367e4`, `lea ebx, [eax + ebp]` is not an anchor +ANCHOR_RE = re.compile(r"^(?:lea (e[a-z]{2}), \[(e[a-z]{2}) \+ (0x[0-9a-f]{4,}|\d{4,})\]" + r"|add (e[a-z]{2}), (0x[0-9a-f]{4,}|\d{4,}))$") +MIN_ANCHOR = 0x1000 + + +def op(line): + return line.split("\t")[0].strip() + + +def anchors(lines): + """[(register, constant)] for every base set-up in order.""" + out = [] + for line in lines: + m = ANCHOR_RE.match(line) + if not m: + continue + reg = m.group(1) or m.group(4) + value = int(m.group(3) or m.group(5), 0) + if value >= MIN_ANCHOR: + out.append((reg, value)) + return out + + +def blocks(entry): + pairs = [] + for hunk in entry.get("diff") or []: + for block in hunk[1]: + o = [op(r[1]) for r in block.get("orig", [])] + r = [op(r[1]) for r in block.get("recomp", [])] + if o and r: + pairs.append((o, r)) + return pairs + + +def hints_for(entry): + hits = [] + for o, r in blocks(entry): + a, b = anchors(o), anchors(r) + if not a or len(a) != len(b): + continue + for (_, x), (_, y) in zip(a, b): + if x != y and (x, y) not in [(h[0], h[1]) for h in hits]: + hits.append((x, y, y - x)) + return hits + + +def main(): + args = sys.argv[1:] + if args and args[0] == "--run": + common.run_reccmp() + args = args[1:] + want = args[0] if args else "" + byaddr = common.load_diff() + shown = total = 0 + for f, e, a in common.entries(byaddr): + if e is None or float(e["matching"]) >= 1 or want not in e["name"]: + continue + total += 1 + hits = hints_for(e) + if not hits: + continue + shown += 1 + print("### %s %.1f%% norm %.1f%%" + % (e["name"], float(e["matching"]) * 100, common.normalized_ratio(e) * 100)) + for x, y, delta in hits: + print(" ANCHOR orig: 0x%-8x ours: 0x%-8x ours is 0x%x %s - look that offset up in the " + "struct header" % (x, y, abs(delta), "higher" if delta > 0 else "lower")) + if not total: + print("nothing below 100% in the current list") + else: + print("\n%d of %d function(s) below 100%% anchor differently" % (shown, total)) + + +if __name__ == "__main__": + main() diff --git a/tools/reimplementation-helper/batch/diff_falsesym.py b/tools/reimplementation-helper/batch/diff_falsesym.py new file mode 100644 index 0000000000..590611a384 --- /dev/null +++ b/tools/reimplementation-helper/batch/diff_falsesym.py @@ -0,0 +1,99 @@ +"""Find diff lines that are only a reccmp *symbolization* difference, not a codegen difference. + +usage: diff_falsesym.py [--run] [NAME-SUBSTRING] + +reccmp compares the symbolized disassembly text, so it renders an operand as `SYMBOL+N (OFFSET)` +whenever the value happens to land inside a known object. A plain integer constant that is large +enough can land inside one by coincidence - and because the two images lay their data out +differently, it lands inside an object in the original and not in ours. The same instruction then +reads as a mismatch: + + -push I::instance+424671 (OFFSET) + +push 0xffffff + +Those two lines are the same four bytes. `0xffffff` is opaque white, a colour argument to a text +call, and it sits inside BuildingsState in the original image only. This was the *entire* remaining +gap of `renderDebugDataCurrentPlayerData`: 41 lines, all of them that one constant, scoring 96.0% +for code that is byte-identical to the original. + +FALSESYM pairs an orig line carrying a symbol with a recomp line carrying a bare immediate when the +mnemonic and the rest of the operands are identical. It cannot prove the bytes match - it does not +know the object's base address - so confirm each hit by disassembling the original: + + ghidra-mcp disassemble_function # or tools/.../orig_asm.py + +and checking that the original really is a plain immediate. If it is, the function is finished and +no source change can raise its score; record it rather than chasing it. Beware the reverse mistake +too: a constant that *should* be an address, spelled as a literal in our source, produces the same +diff and is a real bug. + +--run re-runs reccmp first (otherwise reccmp/dll/diff.json from the last run is used). +""" + +import re +import sys + +import common + +IMM_RE = re.compile(r"^(0x[0-9a-f]+|\d+)$") +SYM_RE = re.compile(r"[A-Za-z_<>:]") + + +def op(line): + return line.split("\t")[0].strip() + + +def split(line): + parts = line.split(None, 1) + return (parts[0], parts[1].strip() if len(parts) > 1 else "") + + +def hints_for(entry): + hits = {} + for hunk in entry.get("diff") or []: + for block in hunk[1]: + o = [op(r[1]) for r in block.get("orig", [])] + r = [op(r[1]) for r in block.get("recomp", [])] + if not o or len(o) != len(r): + continue + for a, b in zip(o, r): + if a == b: + continue + ma, oa = split(a) + mb, ob = split(b) + # same mnemonic, ours a bare immediate, theirs carrying a symbol + if ma == mb and IMM_RE.match(ob) and SYM_RE.search(oa) and int(ob, 0) >= 0x10000: + hits[(a, b)] = hits.get((a, b), 0) + 1 + return sorted(hits.items(), key=lambda kv: -kv[1]) + + +def main(): + args = sys.argv[1:] + if args and args[0] == "--run": + common.run_reccmp() + args = args[1:] + want = args[0] if args else "" + byaddr = common.load_diff() + shown = total = 0 + for f, e, a in common.entries(byaddr): + if e is None or float(e["matching"]) >= 1 or want not in e["name"]: + continue + total += 1 + hits = hints_for(e) + if not hits: + continue + shown += 1 + lines = sum(n for _, n in hits) + print("### %s %.1f%% norm %.1f%% (%d line(s) look like symbolization only)" + % (e["name"], float(e["matching"]) * 100, common.normalized_ratio(e) * 100, lines)) + for (a, b), n in hits[:10]: + print(" FALSESYM x%-4d orig: %s" % (n, a)) + print(" %14s ours: %s - confirm the original is a plain immediate" % ("", b)) + if not total: + print("nothing below 100% in the current list") + else: + print("\n%d of %d function(s) below 100%% have a symbolization-only hint" % (shown, total)) + + +if __name__ == "__main__": + main() From 5bf899660889466b555eb40646de65f9a332a61f Mon Sep 17 00:00:00 2001 From: Gynt Date: Sun, 18 Oct 2026 18:20:08 +0200 Subject: [PATCH 02/18] fix(split): take updateCrowding, which is at 37.7% It was misfiled onto reimpl/GameStateStructures-100. The cheat-sheet section recording its blocker is already here. Co-Authored-By: Claude Opus 5 (1M context) --- .../GameStateStructures/updateCrowding.cpp | 27 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 28 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..367580c3e4 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp @@ -0,0 +1,27 @@ +#include "../GameStateStructures.func.hpp" + +namespace OpenSHC { +namespace Game { + + // FUNCTION: STRONGHOLDCRUSADER 0x00458E60 + void GameStateStructures::updateCrowding() + { + for (int playerID = 1; playerID != 9; ++playerID) { + int populationCap = this->playerDataArray[playerID].populationCap; + int currentPopulation = this->playerDataArray[playerID].currentPopulation; + int crowding; + if (currentPopulation <= populationCap) { + crowding = 0; + } else if (currentPopulation <= 0) { + crowding = 0; + } else if (populationCap <= 0) { + crowding = 150; + } else { + crowding = (currentPopulation * 100) / populationCap; + } + this->playerDataArray[playerID].crowding = crowding; + } + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 4d394d7fff..bbf516a341 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17749,7 +17749,7 @@ SHC_3BB0A8C1_0x00458D50 | 0.0% | Pending SHC_3BB0A8C1_0x00458E20 | 0.0% | Pending -SHC_3BB0A8C1_0x00458E60 | 0.0% | Pending +SHC_3BB0A8C1_0x00458E60 | 37.7% | 4 stores anchor on crowding SHC_3BB0A8C1_0x00458EC0 | 0.0% | Pending From 2ead03bb06d3d38b08c531e22b91590718e38f15 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:06:15 +0100 Subject: [PATCH 03/18] GameStateStructures: bring five functions to 100% match - updateCrowding, processFoodConsumption: walk a pointer to the field the original anchors its loop on, reaching other PlayerData fields by offset - spawnPoisonCloudsAroundBuilding: decrement the retry counter in each failure branch to get the original's memory decrement - computePopulationStatistics: name the blessed + unblessed total - updateFoodTypesInStockForAllPlayers (0x00458A70): new, needed so /GL can keep `this` live across the call in processFoodConsumption Co-Authored-By: Claude Opus 5.5 (1M context) --- .../computePopulationStatistics.cpp | 10 +-- .../processFoodConsumption.cpp | 90 +++++++++---------- .../spawnPoisonCloudsAroundBuilding.cpp | 5 +- .../GameStateStructures/updateCrowding.cpp | 31 ++++--- .../updateFoodTypesInStockForAllPlayers.cpp | 30 +++++++ 5 files changed, 103 insertions(+), 63 deletions(-) create mode 100644 src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp diff --git a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp index f9df446adc..da8127bbe0 100644 --- a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp +++ b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp @@ -48,15 +48,13 @@ namespace Game { 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 (this->playerDataArray[playerID].unblessedPeopleCountUnk - + this->playerDataArray[playerID].blessedPeopleCountUnk - == 0) { + if (total == 0) { blessedPercentage = 100; } else { - blessedPercentage = (this->playerDataArray[playerID].blessedPeopleCountUnk * 100) - / (this->playerDataArray[playerID].unblessedPeopleCountUnk - + this->playerDataArray[playerID].blessedPeopleCountUnk); + blessedPercentage = (this->playerDataArray[playerID].blessedPeopleCountUnk * 100) / total; } this->playerDataArray[playerID].blessedPeoplePercentage = blessedPercentage; } diff --git a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp index 3e16c8e440..5061e0bade 100644 --- a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp +++ b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp @@ -27,6 +27,12 @@ namespace Game { 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() { @@ -41,53 +47,50 @@ namespace Game { return; } MACRO_CALL_MEMBER(GameStateStructures_Func::updateFoodTypesInStockForAllPlayers, this)(); - for (int playerID = 1; playerID < 9; ++playerID) { - if (this->playerDataArray[playerID].keep.id <= 0 || this->playerDataArray[playerID].campground.id <= 0) { + int playerID = 1; + int* foodClockSpeed = &this->playerDataArray[1].foodClockSpeed; + for (; playerID < 9; ++playerID, foodClockSpeed += sizeof(PlayerData) / sizeof(int)) { + if (PLAYER_FIELD(int, keep.id) <= 0 || PLAYER_FIELD(int, campground.id) <= 0) { continue; } - this->playerDataArray[playerID].foodStorageLevel = -1; - if (this->playerDataArray[playerID].foodTypesInStock <= 0) { - this->playerDataArray[playerID].foodTypesCurrentlyEaten = 0; + PLAYER_FIELD(int, foodStorageLevel) = -1; + if (PLAYER_FIELD(int, foodTypesInStock) <= 0) { + PLAYER_FIELD(int, foodTypesCurrentlyEaten) = 0; continue; } - int foodClockSpeed = this->playerDataArray[playerID].currentPopulation * 3; - this->playerDataArray[playerID].foodClockSpeed = foodClockSpeed; + 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) { - this->playerDataArray[playerID].foodClockSpeed = (foodClockSpeed * 60) / 100; + PLAYER_FIELD(int, foodClockSpeed) = (clockSpeed * 60) / 100; } - switch (this->playerDataArray[playerID].rationsSetting) { + switch (PLAYER_FIELD(int, rationsSetting)) { case 0: - this->playerDataArray[playerID].foodClockSpeed = 0; - this->playerDataArray[playerID].foodTypesCurrentlyEaten = 0; + PLAYER_FIELD(int, foodClockSpeed) = 0; + PLAYER_FIELD(int, foodTypesCurrentlyEaten) = 0; break; case 1: - this->playerDataArray[playerID].foodClockSpeed = this->playerDataArray[playerID].foodClockSpeed / 2; + PLAYER_FIELD(int, foodClockSpeed) = PLAYER_FIELD(int, foodClockSpeed) / 2; break; case 3: - this->playerDataArray[playerID].foodClockSpeed - = (this->playerDataArray[playerID].foodClockSpeed * 3) / 2; + PLAYER_FIELD(int, foodClockSpeed) = (PLAYER_FIELD(int, foodClockSpeed) * 3) / 2; break; case 4: - this->playerDataArray[playerID].foodClockSpeed = this->playerDataArray[playerID].foodClockSpeed * 2; + PLAYER_FIELD(int, foodClockSpeed) = PLAYER_FIELD(int, foodClockSpeed) * 2; } - int storageLevel = -1; - if (this->playerDataArray[playerID].foodClockSpeed != 0) { - storageLevel = ((15000 / this->playerDataArray[playerID].foodClockSpeed) - * this->playerDataArray[playerID].totalFood) - / 800; - this->playerDataArray[playerID].foodStorageLevel = storageLevel; + 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)storageLevel; - short lastMonthLeftover = this->playerDataArray[playerID].foodStorageLevelLastMonth; - this->playerDataArray[playerID].foodStorageLevelLastLastMonth = lastMonthLeftover; - this->playerDataArray[playerID].foodStorageLevelLastMonth = leftover; + 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 (this->playerDataArray[playerID].totalFood == 0) { + if (PLAYER_FIELD(int, totalFood) == 0) { if (DAT_CounterFoodWarningInterval::instance == 0 && MACRO_CALL_MEMBER( Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() @@ -100,8 +103,8 @@ namespace Game { if ((int)DAT_CounterFoodWarningInterval::instance > 2) { DAT_CounterFoodWarningInterval::instance = 0; } - } else if (leftover < 2 && leftover != lastMonthLeftover - && this->playerDataArray[playerID].rationsSetting != 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)() @@ -120,38 +123,31 @@ namespace Game { } } } - this->playerDataArray[playerID].foodClock - = this->playerDataArray[playerID].foodClock + this->playerDataArray[playerID].foodClockSpeed; - if (this->playerDataArray[playerID].foodClock > 15000) { - this->playerDataArray[playerID].foodClock = 0; - this->playerDataArray[playerID].foodTypesCurrentlyEaten - = this->playerDataArray[playerID].foodTypesInStock; + PLAYER_FIELD(int, foodClock) = 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); while (true) { - this->playerDataArray[playerID].foodTypeToBeEatenNext - = this->playerDataArray[playerID].foodTypeToBeEatenNext + 1; - if (this->playerDataArray[playerID].foodTypeToBeEatenNext > 3) { - this->playerDataArray[playerID].foodTypeToBeEatenNext = 0; + PLAYER_FIELD(int, foodTypeToBeEatenNext) = PLAYER_FIELD(int, foodTypeToBeEatenNext) + 1; + if (PLAYER_FIELD(int, foodTypeToBeEatenNext) > 3) { + PLAYER_FIELD(int, foodTypeToBeEatenNext) = 0; } - if (this->playerDataArray[playerID].foodTypeToBeEatenNext == 0 - && this->playerDataArray[playerID].breadCount > 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 (this->playerDataArray[playerID].foodTypeToBeEatenNext == 1 - && this->playerDataArray[playerID].cheeseCount > 0) { + 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 (this->playerDataArray[playerID].foodTypeToBeEatenNext == 2 - && this->playerDataArray[playerID].meatCount > 0) { + 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 (this->playerDataArray[playerID].foodTypeToBeEatenNext == 3 - && this->playerDataArray[playerID].appleCount > 0) { + 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; @@ -161,5 +157,7 @@ namespace Game { } } +#undef PLAYER_FIELD + } } diff --git a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp index 9e31db7ced..dd3845adde 100644 --- a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp @@ -24,7 +24,8 @@ namespace Game { // FUNCTION: STRONGHOLDCRUSADER 0x00459A20 void GameStateStructures::spawnPoisonCloudsAroundBuilding(int buildingID) { - for (int attemptsLeft = 20; attemptsLeft > 0; attemptsLeft--) { + int attemptsLeft = 20; + while (attemptsLeft > 0) { uint xPosition = SEC_RNG::instance.currentNumber2 % 30 - 15 + (short)DAT_BuildingsState::instance.buildings[buildingID].x; uint yPosition = (SEC_RNG::instance.currentNumber2 >> 8) % 30 - 15 @@ -32,10 +33,12 @@ namespace Game { 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 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) { diff --git a/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp index 367580c3e4..fa9e0f688d 100644 --- a/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/updateCrowding.cpp @@ -3,25 +3,36 @@ 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 = this->playerDataArray[playerID].populationCap; - int currentPopulation = this->playerDataArray[playerID].currentPopulation; - int crowding; - if (currentPopulation <= populationCap) { - crowding = 0; - } else if (currentPopulation <= 0) { - crowding = 0; + 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) { - crowding = 150; + PLAYER_FIELD(int, crowding) = 150; } else { - crowding = (currentPopulation * 100) / populationCap; + PLAYER_FIELD(int, crowding) = (population * 100) / populationCap; } - this->playerDataArray[playerID].crowding = crowding; + currentPopulation += sizeof(PlayerData) / sizeof(int); } } +#undef PLAYER_FIELD + } } diff --git a/src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp b/src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp new file mode 100644 index 0000000000..53b48ee582 --- /dev/null +++ b/src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp @@ -0,0 +1,30 @@ +#include "../GameStateStructures.func.hpp" + +namespace OpenSHC { +namespace Game { + + // FUNCTION: STRONGHOLDCRUSADER 0x00458A70 + void GameStateStructures::updateFoodTypesInStockForAllPlayers() + { + for (int playerID = 1; playerID != 9; ++playerID) { + if (this->playerDataArray[playerID].keep.id <= 0 || this->playerDataArray[playerID].campground.id <= 0) { + continue; + } + this->playerDataArray[playerID].foodTypesInStock = 0; + if (this->playerDataArray[playerID].breadCount > 0) { + this->playerDataArray[playerID].foodTypesInStock = 1; + } + if (this->playerDataArray[playerID].cheeseCount > 0) { + this->playerDataArray[playerID].foodTypesInStock = this->playerDataArray[playerID].foodTypesInStock + 1; + } + if (this->playerDataArray[playerID].meatCount > 0) { + this->playerDataArray[playerID].foodTypesInStock = this->playerDataArray[playerID].foodTypesInStock + 1; + } + if (this->playerDataArray[playerID].appleCount > 0) { + this->playerDataArray[playerID].foodTypesInStock = this->playerDataArray[playerID].foodTypesInStock + 1; + } + } + } + +} +} From 8bc46ff174aa6930c1b1527f16d2121719498fb7 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:06:58 +0100 Subject: [PATCH 04/18] GameStateStructures: /GL for processFoodConsumption pair, status update Co-Authored-By: Claude Opus 5.5 (1M context) --- cmake/compiler-flags-gl.txt | 2 ++ status/addresses-SHC-3BB0A8C1.txt | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 162955856e..406eab6a31 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -20,3 +20,5 @@ src/OpenSHC/Input/MouseState/updateRightDragCameraControl.cpp src/OpenSHC/AI/AIVState/executeDefaultCastleAIV.cpp src/OpenSHC/AI/AIVState/findAppropriateGridLocationForBuilding.cpp +src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp +src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 2f6373d4e3..44da7faced 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17729,11 +17729,11 @@ SHC_3BB0A8C1_0x00458990 | 0.0% | Pending SHC_3BB0A8C1_0x004589E0 | 0.0% | Pending -SHC_3BB0A8C1_0x00458A70 | 0.0% | Pending +SHC_3BB0A8C1_0x00458A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458A80 | 0.0% | Pending -SHC_3BB0A8C1_0x00458AD0 | 96.8% | Field read would move the anch +SHC_3BB0A8C1_0x00458AD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458D38 | 0.0% | Pending @@ -17749,7 +17749,7 @@ SHC_3BB0A8C1_0x00458D50 | 0.0% | Pending SHC_3BB0A8C1_0x00458E20 | 0.0% | Pending -SHC_3BB0A8C1_0x00458E60 | 37.7% | 4 stores anchor on crowding +SHC_3BB0A8C1_0x00458E60 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00458EC0 | 0.0% | Pending @@ -17901,7 +17901,7 @@ SHC_3BB0A8C1_0x004598B0 | 0.0% | Pending SHC_3BB0A8C1_0x00459930 | 0.0% | Pending -SHC_3BB0A8C1_0x00459A20 | 92.4% | loop counter kept in a reg +SHC_3BB0A8C1_0x00459A20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00459A40 | 0.0% | Pending @@ -18027,7 +18027,7 @@ SHC_3BB0A8C1_0x0045AEB0 | 0.0% | Pending SHC_3BB0A8C1_0x0045AF00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045AF30 | 98.0% | Reg alloc differs +SHC_3BB0A8C1_0x0045AF30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045B1C0 | 0.0% | Pending From 8e64add587190e9d6904338aa9369fb07311ece3 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:06:58 +0100 Subject: [PATCH 05/18] Cheat sheet: field-pointer loop anchor, memory decrement loop Co-Authored-By: Claude Opus 5.5 (1M context) --- IMPLEMENTATION_CHEAT_SHEET.md | 100 +++++++++++++++++----------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/IMPLEMENTATION_CHEAT_SHEET.md b/IMPLEMENTATION_CHEAT_SHEET.md index 5d1684e3b0..60dfccceb4 100644 --- a/IMPLEMENTATION_CHEAT_SHEET.md +++ b/IMPLEMENTATION_CHEAT_SHEET.md @@ -718,7 +718,8 @@ change nothing in this toolchain (they are deprecated no-ops in VS2005). MSVC bases a run of accesses into one object on a single address (`lea esi, [ecx + 0x367e4]`, then `[esi - 0x10]`, `[esi + 0xe0]`, ...). Which field lands at offset 0 is not a size or frequency decision - with the right anchor `processFoodConsumption` gets 30 byte-sized displacements and with the wrong one 3, -and MSVC still picks the wrong one - and it cannot be steered from the source: the global vs `this->` +and MSVC still picks the wrong one - and it cannot be steered from an *indexed* source (an explicit pointer to the +anchor field does pin it, see the section near the end): the global vs `this->` spelling of a field, an extra read of it, the index's cast, the statement order and the number of stores to other fields all leave it unchanged (probed with a `/FA` compile per variant, about ten seconds each). @@ -859,11 +860,12 @@ further from `sub esp, 0x18` rather than closer. Four of the finds above came from `decompile_function`, not from the interleaved reccmp diff, and two of them were worth 100%. The decompiler names the original's locals and pointers, which is exactly the -information the asm hides: `processFoodConsumption`'s AI branch is -`(currentPopulation * 180) / 100` with `currentPopulation` as the local, not -`(foodClockSpeed * 60) / 100` - the same value, but MSVC reaches the original's instruction sequence -only from the first spelling. `updateCrowding`'s pointer is anchored at `currentPopulation`, which is -why its base is 0x2100 away from the field it stores to. +information the asm hides. `updateCrowding`'s pointer is anchored at `currentPopulation`, which is +why its base is 0x2100 away from the field it stores to - and writing that pointer out is what took it +to 100% (see *A loop base the original anchors on one field* below). Do not over-read the decompiler's +arithmetic, though: it renders `processFoodConsumption`'s AI branch as `(currentPopulation * 0xb4) / 100`, +but the binary keeps `currentPopulation * 3` in `edi` and multiplies that by 60 (`shl 4; sub; add; add`), +so `(clockSpeed * 60) / 100` with `clockSpeed` as the local is the form that matches. ## reccmp can score a byte-identical function below 100% @@ -904,19 +906,20 @@ reach the original's `sub esp, 0x18`, because it drops the `0xfffcf0ec` base to of two - but that base exists in the original, so the frame matches by compensation and reccmp measured 54.5%, a slight regression. quick_diff ranked it 0.751 over 0.739; trust reccmp. -## spawnPoisonCloudsAroundBuilding: the memory decrement (92.4%, closed) +## spawnPoisonCloudsAroundBuilding: the memory decrement is a decrement in each failure branch (100%) -The only gap is the loop tail. The original folds it to a read-modify-write plus a fresh re-read: +The original ends its retry loop with a read-modify-write and a fresh compare: sub dword ptr [esp + 0x10], 1 cmp dword ptr [esp + 0x10], 0 + jg -while we keep the value in a register (`mov eax, mem; sub eax, 1; test eax, eax; mov mem, eax`). The -peephole needs the load/sub/store contiguous, and our scheduler puts the `test` between the sub and -the store. The two `continue`s force the decrement into the `for` header, so the obvious forms were -already known to canonicalise; rewriting the `continue`s as nested `if`s to put the decrement at the -bottom of the body - the decompilation's own shape, and the one the peephole wants - measured -*worse* (0.905 against 0.950) and still did not produce `sub dword ptr`. +A `for (...; attemptsLeft > 0; attemptsLeft--)` with two `continue`s compiles to a register load, `sub`, `test` and +store instead, and every spelling of the header (`--x`, `x -= 1`, `0 < x`, `x >= 1`, `short`, a count-up loop) +gives the same or worse. `!= 0` and `>= 0` do give `sub dword ptr`, but fused with its own flags (`jne`/`jns`), not +the separate `cmp`. What matches is a `while (attemptsLeft > 0)` with `attemptsLeft--;` written before **each** +`continue`: the two decrements are cross-jumped into one block, which is the shape the peephole folds into +`sub [mem], 1`, and the loop test then re-reads memory. 92.4% -> 100%. ## processPeasantSpawnAndDespawnCycle: do not hoist the popularity read (84.0%, closed) @@ -1006,38 +1009,37 @@ answer either, because the original has the cookie code and dropping it loses mo `playSFXAtLocation` is likewise done at 98.1% / normalized 100%: its only diff is the call targets of the CRT helpers `_CIsqrt` and `_ftol2_sse`. -## updateCrowding: the anchor and the store count cannot both be had (37.7%) - -Correcting an earlier note that said the anchor was wrong here - in the current one-store form the -anchor is **right**. The original (`disassemble_function 0x00458E60`) is: - - add ecx, 0x368b4 ; anchor on currentPopulation, displacement 0 - mov ebx, 8 ; a countdown, no playerID - xor edi, edi ; a zero register, before the loop - mov esi, dword ptr [ecx - 0x210c] ; populationCap - mov eax, dword ptr [ecx] ; currentPopulation - ... - mov dword ptr [ecx - 0x2100], edi ; crowding = 0, four stores, one per arm - ... - add ecx, 0x39f4 - sub ebx, 1 - jnz - -We match the anchor, the countdown, the pointer stride and both reads. The gap is that the original -assigns the *field* in each of the four arms, where we compute into a local and store once. The two -forms measure: - -* one store, value in `eax` -> **37.7%**, anchor `0x368b4` correct -* four field stores -> 33.3%, anchor moves to `0x347b4`, i.e. onto `crowding` itself - -The four stores drag the anchor because our zeros are immediates (`mov dword ptr [ecx], 0`), and an -immediate store is shortest at displacement 0, so MSVC bases on the field being stored. The original -pays nothing for a displacement because its zero is in `edi`, which is also why it can spell -`currentPopulation <= 0` as `cmp eax, edi` instead of our `test eax, eax`. Giving the zero a name to -force it into a register does not work - MSVC constant-propagates it straight back to an immediate: - -* `int noCrowding = 0;` inside the loop, used as the store source -> 33.3% -* the same declared before the loop, live range spanning it -> 33.3% - -So the choice is the correct anchor or the correct store count, and the anchor is worth 4.4 points -more. Keep the one-store form. +## A loop base the original anchors on one field: walk a pointer to that field (100%, three functions) + +MSVC strength-reduces a `playerDataArray[playerID].field` loop into one base register plus displacements, and +which field sits at displacement 0 is the anchor discussed above. It cannot be steered while the source indexes +the array, but it **can** be pinned by writing the induction pointer yourself, aimed at the field the decompiler +names (`_currentPopulationAddr = &DAT_GameState.playerDataArray[1].currentPopulation`, `ADJ(...)->field`), and +reaching the other fields by their offset from it: + +```cpp +#define PLAYER_FIELD(type, field) (*(type*)((char*)currentPopulation + offsetof(PlayerData, field) - offsetof(PlayerData, currentPopulation))) + +int* currentPopulation = &this->playerDataArray[1].currentPopulation; +for (...; currentPopulation += sizeof(PlayerData) / sizeof(int)) { PLAYER_FIELD(int, crowding) = 0; ... } +``` + +A `PlayerData*` (or a `PlayerData&` per iteration) is canonicalised back to the indexed form and does not help; +the pointer has to point at the field itself. Once the anchor is pinned, the changes that used to "move the +anchor" become free and the function matches: + +- `updateCrowding` 37.7% -> 100%: with the anchor right, the four per-arm `crowding` stores take their zero + from a register as the original does. It also needed the pointer advance written in both exits (an early + `continue` arm and the end of the body) rather than once in the `for` header, which swapped two registers. +- `processFoodConsumption` 96.8% -> 100%: anchor on `foodClockSpeed`; `leftover` read back from + `foodStorageLevel` instead of a local; `LastLastMonth = LastMonth` copied field to field and compared through + the field (that gives the original's 16-bit `mov cx`); `int playerID = 1;` declared before the pointer, for + the order of the two loop initialisers. The `this` it keeps in `edx` across `updateFoodTypesInStockForAllPlayers` + is LTCG over that callee, which therefore had to be reimplemented (0x00458A70, matched first try) and put in + `cmake/compiler-flags-gl.txt` next to its caller. +- `processPeasantSpawnAndDespawnCycle`: anchored on `vclockIncrement`, the shared store after the increment + chain no longer moves the base, but what is left (two register names, and `x * 8` as `lea` against three + `add`s) still measures below the indexed form in reccmp, so it was not kept. + +Keep the macro local to the .cpp and `#undef` it at the end of the file. + From 47865ced6de5fa4890f4d60fa63b6cc1a766dd0d Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:21:29 +0100 Subject: [PATCH 06/18] GameStateStructures: apply the reimplementation style guide Compound assignments and ++/--, imported string literals, RT_GOLD instead of 15, countof/sizeof for array bounds, decimal constants, no redundant casts, for instead of while, HeightLayer[tile] instead of an offset into EntityLayerLT25. All nine functions measure unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../computePopulationStatistics.cpp | 22 +++++-------- .../processFoodConsumption.cpp | 13 ++++---- .../processPeasantSpawnAndDespawnCycle.cpp | 3 +- ...AndStatisticsAndStartGoodsAndResources.cpp | 31 ++++++++++--------- .../spawnMotherOrChild.cpp | 10 +++--- .../spawnPoisonCloudsAroundBuilding.cpp | 17 +++++----- 6 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp index afe3eac40b..c2558fca71 100644 --- a/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp +++ b/src/OpenSHC/Game/GameStateStructures/computePopulationStatistics.cpp @@ -2,6 +2,7 @@ #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" @@ -27,20 +28,17 @@ namespace Game { if (this->playerDataArray[playerID].lordID != 0 && DAT_UnitsState::instance.units[this->playerDataArray[playerID].lordID].uid == this->playerDataArray[playerID].lordUID) { - this->playerDataArray[playerID].currentPopulation - = this->playerDataArray[playerID].currentPopulation - 1; + --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 - = this->playerDataArray[playerID].currentPopulation - 1; + --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].currentPopulation - 1; + --this->playerDataArray[playerID].currentPopulation; } this->playerDataArray[playerID].populationRelatedCrowdingCountUnk = this->playerDataArray[playerID].currentPopulation_2 @@ -62,8 +60,7 @@ namespace Game { this->playerDataArray[playerID] .populationGrowth[this->playerDataArray[playerID].populationGrowthStatisticCounter] = this->playerDataArray[playerID].availablePeasantsOrHousedPeasants; - this->playerDataArray[playerID].populationGrowthStatisticCounter - = this->playerDataArray[playerID].populationGrowthStatisticCounter + 1; + ++this->playerDataArray[playerID].populationGrowthStatisticCounter; if (this->playerDataArray[playerID].populationGrowthStatisticCounter >= 8) { this->playerDataArray[playerID].populationGrowthStatisticCounter = 0; } @@ -94,18 +91,15 @@ namespace Game { } if (DAT_GameCore::instance.gameMode_2 != GM_EDITOR) { if (this->playerDataArray[playerID].tacticalPowersBarLevel < 7000) { - this->playerDataArray[playerID].tacticalPowersBarLevel - = this->playerDataArray[playerID].tacticalPowersBarLevel + 1; + ++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[15] - = this->playerDataArray[playerID].currentResources[15] + goldIncrement; - this->playerDataArray[playerID].goldDonation - = this->playerDataArray[playerID].goldDonation - goldIncrement; + 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; diff --git a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp index 85f4d72cad..0f4ef17654 100644 --- a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp +++ b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp @@ -11,6 +11,7 @@ #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" @@ -99,7 +100,7 @@ namespace Game { == FALSE) { // "No food distributed this month" MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( - "food_warning4.wav"); + s_food_warning4_wav_005a509c); } ++DAT_CounterFoodWarningInterval::instance; if ((int)DAT_CounterFoodWarningInterval::instance > 2) { @@ -113,24 +114,24 @@ namespace Game { == FALSE) { // "Granary stocks are very low sire" MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)( - "food_warning2.wav"); + 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)( - "food_warning3.wav"); + s_food_warning3_wav_005a5074); } } } } - PLAYER_FIELD(int, foodClock) = PLAYER_FIELD(int, foodClock) + PLAYER_FIELD(int, foodClockSpeed); + 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); - while (true) { - PLAYER_FIELD(int, foodTypeToBeEatenNext) = PLAYER_FIELD(int, foodTypeToBeEatenNext) + 1; + for (;;) { + ++PLAYER_FIELD(int, foodTypeToBeEatenNext); if (PLAYER_FIELD(int, foodTypeToBeEatenNext) > 3) { PLAYER_FIELD(int, foodTypeToBeEatenNext) = 0; } diff --git a/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp index 0547275559..057c04e368 100644 --- a/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp +++ b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp @@ -64,8 +64,7 @@ namespace Game { .PeasantSpawnClockIncrementSolitaryPopLessThan101[popularityIndex]; } } - this->playerDataArray[playerID].vclock - = this->playerDataArray[playerID].vclockIncrement + this->playerDataArray[playerID].vclock; + this->playerDataArray[playerID].vclock += this->playerDataArray[playerID].vclockIncrement; if (this->playerDataArray[playerID].vclock >= 4000) { this->playerDataArray[playerID].vclock = 4000; } else if (this->playerDataArray[playerID].vclock < 0) { diff --git a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp index 3c3bfed7b6..7d7cf2868b 100644 --- a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp +++ b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp @@ -4,6 +4,7 @@ #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" @@ -43,7 +44,7 @@ namespace Game { this->playerDataArray[playerID].someCount45 = 1; this->playerDataArray[playerID].someCount46 = 1; this->playerDataArray[playerID].someCount47 = 0; - this->playerDataArray[playerID].textYOffset = 0x3c; + this->playerDataArray[playerID].textYOffset = 60; this->playerDataArray[playerID].someCountdown01 = 0; this->playerDataArray[playerID].playerDeathRelated = 0; this->playerDataArray[playerID].previousBlessedPeoplePercentage = 0; @@ -63,11 +64,13 @@ namespace Game { this->playerDataArray[playerID].eventPlagueHealerActivity = 0; this->playerDataArray[playerID].keepEnclosementRelatedCountdown = 0; this->playerDataArray[playerID].defensesDamagedByPlayer = 0; - for (int resourceType = 0; resourceType < 25; ++resourceType) { + 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, 100); + 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; @@ -79,7 +82,7 @@ namespace Game { this->playerDataArray[playerID].rationsSetting2 = scenarioRations; this->playerDataArray[playerID].rationsSetting3 = scenarioRations; this->playerDataArray[playerID].rationsSetting = scenarioRations; - this->playerDataArray[playerID].currentResources[0xf] = this->mapAndTime.scenarioGold; + this->playerDataArray[playerID].currentResources[Resources::RT_GOLD] = this->mapAndTime.scenarioGold; } if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY) { int gameTypeMultiplier = 1; @@ -89,35 +92,35 @@ namespace Game { gameTypeMultiplier = 3; } if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[playerID] != -1) { - this->playerDataArray[playerID].startResources[0xf] + this->playerDataArray[playerID].startResources[Resources::RT_GOLD] = DAT_RenderingDefinedData::instance .field_0x53bb4[(DAT_GameSynchronyState::instance.skirmishCurrentAdvantageBalance - + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) + + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) * 2 - + 0xc] + + 12] * gameTypeMultiplier; } else if (DAT_GameSynchronyState::instance.currentAIArray[playerID] != 0) { - this->playerDataArray[playerID].startResources[0xf] + this->playerDataArray[playerID].startResources[Resources::RT_GOLD] = DAT_RenderingDefinedData::instance .field_0x53bb4[(DAT_GameSynchronyState::instance.skirmishCurrentAdvantageBalance - + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) + + DAT_GameSynchronyState::instance.skirmishGameIntensityType * 5) * 2 - + 0xd] + + 13] * gameTypeMultiplier; } } DAT_GameState::instance.mapAndTime.populationIndex = 0; this->playerDataArray[playerID].beforeLastMonthsGold - = (short)this->playerDataArray[playerID].currentResources[0xf]; + = (short)this->playerDataArray[playerID].currentResources[Resources::RT_GOLD]; this->playerDataArray[playerID].lastMonthsGold - = (short)this->playerDataArray[playerID].currentResources[0xf]; + = (short)this->playerDataArray[playerID].currentResources[Resources::RT_GOLD]; this->playerDataArray[playerID].aiControlStatusRelated = -1000; - for (int foodType = 0; foodType < 4; ++foodType) { + 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 = 0x30; + DAT_GameState::instance.playerDataArray[playerID].nervousBikCountdown = 48; this->playerDataArray[playerID].tacticalPowersBarLevel = 0; this->playerDataArray[playerID].goldDonation = 0; } diff --git a/src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp b/src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp index d5001add3c..62adf01142 100644 --- a/src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp +++ b/src/OpenSHC/Game/GameStateStructures/spawnMotherOrChild.cpp @@ -101,10 +101,9 @@ namespace Game { } } else if (DAT_GameState::instance.playerDataArray[playerID].popularity / 100 >= 50) { // if popularity is higher than 49 - DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown - = DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown - 1; - if ((char)DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown <= 0 - && (short)(char)DAT_BuildingsState::instance.buildings[buildingID].maxOccupants + --DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown; + if (DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown <= 0 + && DAT_BuildingsState::instance.buildings[buildingID].maxOccupants != DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount) { short childEntryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; DAT_BuildingsState::instance.buildings[buildingID].childSpawnCountdown @@ -129,8 +128,7 @@ namespace Game { = DAT_UnitsState::instance.units[childUnitID].uid; int workplaceUID = DAT_BuildingsState::instance.buildings[buildingID].uid; // increment currentEmployeeCount of building - DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount - = DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount + 1; + ++DAT_BuildingsState::instance.buildings[buildingID].currentEmployeeCount; DAT_UnitsState::instance.units[childUnitID].workplaceBuildingID_1 = (short)buildingID; DAT_UnitsState::instance.units[childUnitID].workplaceBuildingUID = workplaceUID; diff --git a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp index dd3845adde..100374bee4 100644 --- a/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/spawnPoisonCloudsAroundBuilding.cpp @@ -24,27 +24,28 @@ namespace Game { // FUNCTION: STRONGHOLDCRUSADER 0x00459A20 void GameStateStructures::spawnPoisonCloudsAroundBuilding(int buildingID) { - int attemptsLeft = 20; - while (attemptsLeft > 0) { - uint xPosition = SEC_RNG::instance.currentNumber2 % 30 - 15 - + (short)DAT_BuildingsState::instance.buildings[buildingID].x; + // 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 - + (short)DAT_BuildingsState::instance.buildings[buildingID].y; + + 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 tile = DAT_ViewportRenderState::instance.translationMatrix[yPosition].addXgetTile + xPosition; + 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, (byte)DAT_TileMapState::instance.EntityLayerLT25[tile + 0x13a10], 0, - 0, 0, Map::Entities::ET_COW_POISON_CLOUD, 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); From 1654987f1882370f23ffa97895f4b8a5800645ae Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:44:05 +0100 Subject: [PATCH 07/18] computeBuildingCategoryEntryPointAndDestroyEarlierBuilding: share the entry read (33.5% -> 64.0%) The original reads the building entrance once for nine categories and jumps to it. Written out per arm, MSVC merges the copies only after register allocation, so buildingID scored high enough to take ebx. Kept as goto with a comment; a goto-free outer || test measured 52.1 quick against 71.8. Co-Authored-By: Claude Opus 5.5 (1M context) --- ...oryEntryPointAndDestroyEarlierBuilding.cpp | 146 ++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 68 insertions(+), 80 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp index 445864b6bb..eca90d9b0f 100644 --- a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp @@ -24,26 +24,26 @@ namespace Game { int entryY = 0; DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; if (category == Player::PDBCE_KEEP) { - if ((int)this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id > 0 + if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id > 0 && this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id != buildingID) { MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id); } short orientation = DAT_BuildingsState::instance.buildings[buildingID].orientation; - entryX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; - entryY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; + entryX = DAT_BuildingsState::instance.buildings[buildingID].x; + entryY = DAT_BuildingsState::instance.buildings[buildingID].y; if (orientation == 4) { - entryX = entryX + 3; + entryX += 3; --entryY; } else if (orientation == 2) { --entryX; - entryY = entryY + 3; + entryY += 3; } else if (orientation == 6) { - entryX = entryX + 7; - entryY = entryY + 3; + entryX += 7; + entryY += 3; } else { - entryX = entryX + 3; - entryY = entryY + 7; + entryX += 3; + entryY += 7; } if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; @@ -52,81 +52,69 @@ namespace Game { this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_STOCKPILE].id); } } - } else if (category == Player::PDBCE_MARKETPLACE) { - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MARKETPLACE].id > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( - this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MARKETPLACE].id); - } - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_ENGINEERSGUILD) { - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ENGINEERSGUILD].id > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( - this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ENGINEERSGUILD].id); - } - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_TUNNELERSGUILD) { - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_TUNNELERSGUILD].id > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( - this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_TUNNELERSGUILD].id); - } - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_STOCKPILE) { - // a storage building of this kind already exists, keep the old entry - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_STOCKPILE].id > 0) { - return; - } - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_ARMORY) { - // a storage building of this kind already exists, keep the old entry - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ARMORY].id > 0) { - return; - } - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_GRANARY) { - // a storage building of this kind already exists, keep the old entry - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_GRANARY].id > 0) { - return; - } - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_BARRACKS) { - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_BARRACKS].id > 0) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( - this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_BARRACKS].id); + } else { + // The original reads the building entrance once for all of these categories and jumps to it, the + // same goes for the destroy call. Writing the read out in every arm (MSVC merges the copies only after + // register allocation) makes buildingID look hot enough to keep in a register: 33.5% instead of 64.0%. + int existingID; + if (category == Player::PDBCE_MARKETPLACE) { + existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MARKETPLACE].id; + } else if (category == Player::PDBCE_ENGINEERSGUILD) { + existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ENGINEERSGUILD].id; + } else if (category == Player::PDBCE_TUNNELERSGUILD) { + existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_TUNNELERSGUILD].id; + } else if (category == Player::PDBCE_STOCKPILE) { + // a storage building of this kind already exists, keep the old entry + if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_STOCKPILE].id > 0) { + return; + } + goto readEntry; + } else if (category == Player::PDBCE_ARMORY) { + // a storage building of this kind already exists, keep the old entry + if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ARMORY].id > 0) { + return; + } + goto readEntry; + } else if (category == Player::PDBCE_GRANARY) { + // a storage building of this kind already exists, keep the old entry + if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_GRANARY].id > 0) { + return; + } + goto readEntry; + } else if (category == Player::PDBCE_BARRACKS) { + existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_BARRACKS].id; + } else if (category == Player::PDBCE_MERCENARYPOST) { + existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MERCENARYPOST].id; + } else if (category == Player::PDBCE_OILSMELTER) { + goto readEntry; + } else if (category == Player::PDBCE_CAMPGROUND) { + short orientation = DAT_BuildingsState::instance.buildings[buildingID].orientation; + entryX = DAT_BuildingsState::instance.buildings[buildingID].x; + entryY = DAT_BuildingsState::instance.buildings[buildingID].y; + if (orientation == 4) { + entryX += 3; + entryY += 6; + } else if (orientation == 2) { + entryY += 3; + } else if (orientation == 6) { + entryX += 6; + entryY += 3; + } else { + entryX += 3; + } + goto checkEntry; + } else { + goto checkEntry; } - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_MERCENARYPOST) { - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MERCENARYPOST].id > 0) { + if (existingID > 0) { MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( - this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MERCENARYPOST].id); + existingID); } + readEntry: entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_OILSMELTER) { - entryX = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryX; - entryY = DAT_BuildingsState::instance.buildings[buildingID].buildingEntryY; - } else if (category == Player::PDBCE_CAMPGROUND) { - short orientation = DAT_BuildingsState::instance.buildings[buildingID].orientation; - entryX = (short)DAT_BuildingsState::instance.buildings[buildingID].x; - entryY = (short)DAT_BuildingsState::instance.buildings[buildingID].y; - if (orientation == 4) { - entryX = entryX + 3; - entryY = entryY + 6; - } else if (orientation == 2) { - entryY = entryY + 3; - } else if (orientation == 6) { - entryX = entryX + 6; - entryY = entryY + 3; - } else { - entryX = entryX + 3; - } } + checkEntry: if (entryX < 0) { return; } @@ -146,7 +134,7 @@ namespace Game { this->playerDataArray[playerID].buildingEntryInfos[category].tileEntry = DAT_ViewportRenderState::instance.translationMatrix[entryY].addXgetTile + entryX; this->playerDataArray[playerID].buildingEntryInfos[category].areaEntry - = (short)DAT_TileMapState::instance + = DAT_TileMapState::instance .PathConnectionLayer[this->playerDataArray[playerID].buildingEntryInfos[category].tileEntry]; DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 0; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index fe08adbafa..d3818dbf80 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17155,7 +17155,7 @@ SHC_3BB0A8C1_0x00456C50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00456EF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00456FD0 | 33.5% | Join block placed after arm 1 +SHC_3BB0A8C1_0x00456FD0 | 64.0% | category held in a callee-saved reg SHC_3BB0A8C1_0x004572A0 | 100.0% | Reimplemented From 5f97b817c8a4860bb47c56f6191f13902eed9e25 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:47:26 +0100 Subject: [PATCH 08/18] types(Globals): DAT_CounterFoodWarningInterval is int Its only users are the four accesses in processFoodConsumption, which increment it, compare it signed against 2 (add eax, 1; cmp eax, 2; jle) and reset it to 0. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/OpenSHC/Globals/DAT_CounterFoodWarningInterval.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Globals/DAT_CounterFoodWarningInterval.hpp b/src/OpenSHC/Globals/DAT_CounterFoodWarningInterval.hpp index 0ad0b27792..a061de7cd1 100644 --- a/src/OpenSHC/Globals/DAT_CounterFoodWarningInterval.hpp +++ b/src/OpenSHC/Globals/DAT_CounterFoodWarningInterval.hpp @@ -9,11 +9,11 @@ #pragma once // GLOBAL: STRONGHOLDCRUSADER 0x00DF2960 -// StructResolver::Instance::instance +// StructResolver::Instance::instance namespace OpenSHC { -MACRO_STRUCT_RESOLVER(undefined4, false, Address::SHC_3BB0A8C1_0x00DF2960) DAT_CounterFoodWarningInterval; +MACRO_STRUCT_RESOLVER(int, false, Address::SHC_3BB0A8C1_0x00DF2960) DAT_CounterFoodWarningInterval; } // namespace OpenSHC MACRO_STRUCT_INSTANCE(Address::SHC_3BB0A8C1_0x00DF2960); From 90a08002d35db2dcf57f17c3b6ca14d70dbf9f20 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:49:43 +0100 Subject: [PATCH 09/18] types: computeBuildingCategoryEntryPointAndDestroyEarlierBuilding takes a BuildingEntryInfoEnum The category indexes PlayerData::buildingEntryInfos[], whose index enum is Player::Enums::BuildingEntryInfoEnum; PlayerDataBuildingCategoryEnum is the older duplicate of it with the same values. The .cpp follows the signature and its constants. No code change: the function measures 64.0% before and after. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/OpenSHC/Game/GameStateStructures.func.hpp | 6 ++--- src/OpenSHC/Game/GameStateStructures.hpp | 6 ++--- ...oryEntryPointAndDestroyEarlierBuilding.cpp | 27 +++++++++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures.func.hpp b/src/OpenSHC/Game/GameStateStructures.func.hpp index 7da2fbd967..473946ddcc 100644 --- a/src/OpenSHC/Game/GameStateStructures.func.hpp +++ b/src/OpenSHC/Game/GameStateStructures.func.hpp @@ -4,7 +4,7 @@ #include "OpenSHC/Commands/MappersEnum.hpp" #include "OpenSHC/Game/GameStateStructures.hpp" -#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" #include "OpenSHC/Game/Resources/ResourceType.hpp" #include "OpenSHC/IO/PackagedFileMagicNum.hpp" #include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" @@ -13,7 +13,7 @@ namespace Game { namespace GameStateStructures_Func { using OpenSHC::Commands::MappersEnum; - using OpenSHC::Game::Player::PlayerDataBuildingCategoryEnum; + using OpenSHC::Game::Player::Enums::BuildingEntryInfoEnum; using OpenSHC::Game::Resources::ResourceType; using OpenSHC::IO::PackagedFileMagicNum; using OpenSHC::WindowsHelper::Enums::BOOLEnum; @@ -94,7 +94,7 @@ namespace Game { &GameStateStructures::addSignpostToBuildingEntryData) addSignpostToBuildingEntryData; - MACRO_FUNCTION_RESOLVER(void (GameStateStructures::*)(uint, int, PlayerDataBuildingCategoryEnum), false, + MACRO_FUNCTION_RESOLVER(void (GameStateStructures::*)(uint, int, BuildingEntryInfoEnum), false, Address::SHC_3BB0A8C1_0x00456FD0, &GameStateStructures::computeBuildingCategoryEntryPointAndDestroyEarlierBuilding) computeBuildingCategoryEntryPointAndDestroyEarlierBuilding; diff --git a/src/OpenSHC/Game/GameStateStructures.hpp b/src/OpenSHC/Game/GameStateStructures.hpp index f7a6cecd2d..85f531ded4 100644 --- a/src/OpenSHC/Game/GameStateStructures.hpp +++ b/src/OpenSHC/Game/GameStateStructures.hpp @@ -9,8 +9,8 @@ #pragma once #include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" #include "OpenSHC/Game/Player/PlayerData.hpp" -#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" #include "OpenSHC/Game/Resources/ResourceType.hpp" #include "OpenSHC/Game/State/MapAndTimeState.hpp" #include "OpenSHC/Game/State/UnitSelectionHotKeyEntry.hpp" @@ -22,7 +22,7 @@ namespace Game { using OpenSHC::Commands::MappersEnum; using OpenSHC::Game::Player::PlayerData; - using OpenSHC::Game::Player::PlayerDataBuildingCategoryEnum; + using OpenSHC::Game::Player::Enums::BuildingEntryInfoEnum; using OpenSHC::Game::Resources::ResourceType; using OpenSHC::Game::State::MapAndTimeState; using OpenSHC::Game::State::UnitSelectionHotKeyEntry; @@ -88,7 +88,7 @@ namespace Game { void addSignpostToBuildingEntryData(int buildingID); void computeBuildingCategoryEntryPointAndDestroyEarlierBuilding( - uint buildingID, int playerID, PlayerDataBuildingCategoryEnum category); + uint buildingID, int playerID, BuildingEntryInfoEnum category); void validateBuildingCategoryReference(int playerID, int param_2); diff --git a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp index eca90d9b0f..225fe63e72 100644 --- a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp @@ -2,7 +2,6 @@ #include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" #include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" -#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" #include "OpenSHC/UI/Enums/MenuViewType.hpp" #include "OpenSHC/Globals/DAT_BuildingsState.hpp" @@ -13,17 +12,17 @@ namespace OpenSHC { namespace Game { - using Player::PlayerDataBuildingCategoryEnum; + using Player::Enums::BuildingEntryInfoEnum; using UI::Enums::MenuViewType; // FUNCTION: STRONGHOLDCRUSADER 0x00456FD0 void GameStateStructures::computeBuildingCategoryEntryPointAndDestroyEarlierBuilding( - uint buildingID, int playerID, PlayerDataBuildingCategoryEnum category) + uint buildingID, int playerID, BuildingEntryInfoEnum category) { int entryX = 0; int entryY = 0; DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; - if (category == Player::PDBCE_KEEP) { + if (category == Player::Enums::BEIE_KEEP) { if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id > 0 && this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id != buildingID) { MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( @@ -57,37 +56,37 @@ namespace Game { // same goes for the destroy call. Writing the read out in every arm (MSVC merges the copies only after // register allocation) makes buildingID look hot enough to keep in a register: 33.5% instead of 64.0%. int existingID; - if (category == Player::PDBCE_MARKETPLACE) { + if (category == Player::Enums::BEIE_MARKETPLACE) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MARKETPLACE].id; - } else if (category == Player::PDBCE_ENGINEERSGUILD) { + } else if (category == Player::Enums::BEIE_ENGINEERSGUILD) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ENGINEERSGUILD].id; - } else if (category == Player::PDBCE_TUNNELERSGUILD) { + } else if (category == Player::Enums::BEIE_TUNNELERSGUILD) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_TUNNELERSGUILD].id; - } else if (category == Player::PDBCE_STOCKPILE) { + } else if (category == Player::Enums::BEIE_STOCKPILE) { // a storage building of this kind already exists, keep the old entry if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_STOCKPILE].id > 0) { return; } goto readEntry; - } else if (category == Player::PDBCE_ARMORY) { + } else if (category == Player::Enums::BEIE_ARMORY) { // a storage building of this kind already exists, keep the old entry if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ARMORY].id > 0) { return; } goto readEntry; - } else if (category == Player::PDBCE_GRANARY) { + } else if (category == Player::Enums::BEIE_GRANARY) { // a storage building of this kind already exists, keep the old entry if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_GRANARY].id > 0) { return; } goto readEntry; - } else if (category == Player::PDBCE_BARRACKS) { + } else if (category == Player::Enums::BEIE_BARRACKS) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_BARRACKS].id; - } else if (category == Player::PDBCE_MERCENARYPOST) { + } else if (category == Player::Enums::BEIE_MERCENARYPOST) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MERCENARYPOST].id; - } else if (category == Player::PDBCE_OILSMELTER) { + } else if (category == Player::Enums::BEIE_OILSMELTER) { goto readEntry; - } else if (category == Player::PDBCE_CAMPGROUND) { + } else if (category == Player::Enums::BEIE_CAMPGROUND) { short orientation = DAT_BuildingsState::instance.buildings[buildingID].orientation; entryX = DAT_BuildingsState::instance.buildings[buildingID].x; entryY = DAT_BuildingsState::instance.buildings[buildingID].y; From 5fab371f5668e48f58cdbbf7a00e214062c09bca Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:50:15 +0100 Subject: [PATCH 10/18] processFoodConsumption: drop the int cast on the now-int warning counter Co-Authored-By: Claude Opus 5.5 (1M context) --- src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp index 0f4ef17654..e16c68505e 100644 --- a/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp +++ b/src/OpenSHC/Game/GameStateStructures/processFoodConsumption.cpp @@ -103,7 +103,7 @@ namespace Game { s_food_warning4_wav_005a509c); } ++DAT_CounterFoodWarningInterval::instance; - if ((int)DAT_CounterFoodWarningInterval::instance > 2) { + if (DAT_CounterFoodWarningInterval::instance > 2) { DAT_CounterFoodWarningInterval::instance = 0; } } else if (leftover < 2 && leftover != PLAYER_FIELD(short, foodStorageLevelLastLastMonth) From 8e448364fb92d13aefd8449dc671c78053d3d06f Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 27 Nov 2026 18:50:45 +0100 Subject: [PATCH 11/18] Cheat sheet: write a cross-jumped tail once, the allocator counts the copies Co-Authored-By: Claude Opus 5.5 (1M context) --- IMPLEMENTATION_CHEAT_SHEET.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/IMPLEMENTATION_CHEAT_SHEET.md b/IMPLEMENTATION_CHEAT_SHEET.md index 970280695e..990c7bccfa 100644 --- a/IMPLEMENTATION_CHEAT_SHEET.md +++ b/IMPLEMENTATION_CHEAT_SHEET.md @@ -784,6 +784,15 @@ The two need opposite treatment, so read the asm rather than the decompiler's sh tails on its own - so routing the ID through a `SpeechEffectID` local changed nothing (MSVC propagates the constant straight back into the push). Count the call sites in a `/FA` compile before restructuring. +A merged tail can still be worth writing once even when our `/FA` already shows one copy, because MSVC +cross-jumps *after* register allocation: the allocator still counts every source copy as a use. In +`computeBuildingCategoryEntryPointAndDestroyEarlierBuilding` nine arms each read +`buildings[buildingID].buildingEntryX/Y` (and five call `destroyBuilding`). Both compiles end with one shared +block, but with nine source copies `buildingID` scored high enough to take `ebx`, where the original reloads it +from the stack every time. Writing the read once, behind `goto readEntry` from the arms that share it, took +the function 33.5% -> 64.0%. A goto-free form (an outer `||` over the nine categories around the inner +chain) measured worse, 52.1 against 71.8 quick, because the categories are then tested twice. + ### Declaration order decides stack slots, and two swapped locals cost a whole function MSVC assigns stack slots in declaration order. Two locals declared the wrong way round therefore sit From d7af0ef35bc8563ceeca1a1a8be627e691b8c870 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 28 Nov 2026 11:01:30 +0100 Subject: [PATCH 12/18] processPeasantSpawnAndDespawnCycle: store the increment once, scale the entry in locals Structure over raw match: kind 0.956 -> 0.973, mismatched mnemonics 7 -> 2. The original picks the clock increment into one register in every arm and stores it once, walks a pointer to vclockIncrement, and scales the campground entry with three adds. Raw match falls 85.3% -> 78.4% because the increment and the sum now trade eax/ecx with the original; reg_scores shows that pick comes down to a one-point eax affinity, so no source form tried reaches it. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../processPeasantSpawnAndDespawnCycle.cpp | 89 +++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp index 057c04e368..f93cefacea 100644 --- a/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp +++ b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp @@ -32,6 +32,11 @@ namespace Game { using Map::Units::UnitType; using WindowsHelper::Enums::BOOLEnum; +// The original walks a pointer to vclockIncrement and reaches the other PlayerData fields by their distance from +// it. Indexing playerDataArray[playerID] instead makes MSVC base the loop on popularity. +#define PLAYER_FIELD(type, field) \ + (*(type*)((char*)vclockIncrement + offsetof(PlayerData, field) - offsetof(PlayerData, vclockIncrement))) + // FUNCTION: STRONGHOLDCRUSADER 0x0045B4A0 void GameStateStructures::processPeasantSpawnAndDespawnCycle() { @@ -40,56 +45,60 @@ namespace Game { && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { return; } - for (int playerID = 1; playerID < 9; ++playerID) { - if (this->playerDataArray[playerID].lordKilledByPlayerID != 0) { + int playerID = 1; + int* vclockIncrement = &this->playerDataArray[1].vclockIncrement; + for (; playerID < 9; ++playerID, vclockIncrement += sizeof(PlayerData) / sizeof(int)) { + if (PLAYER_FIELD(int, lordKilledByPlayerID) != 0) { continue; } - if (this->playerDataArray[playerID].playerDeathRelated != 0 + if (PLAYER_FIELD(int, playerDeathRelated) != 0 && DAT_GameSynchronyState::instance.currentGameMode == GM_SOLITARY) { continue; } + // The original picks the increment into a register and stores it once, then reuses that register for + // the sum and for the zero test below. + int increment; if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY) { - this->playerDataArray[playerID].vclockIncrement - = DAT_TroopDefinedData::instance - .PeasantSpawnClockIncrementSkirmish[this->playerDataArray[playerID].popularity / 500]; + increment = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSkirmish[PLAYER_FIELD(int, popularity) / 500]; } else { - int popularityIndex = this->playerDataArray[playerID].popularity / 500; - if (this->playerDataArray[playerID].currentPopulation > 100) { - this->playerDataArray[playerID].vclockIncrement - = DAT_TroopDefinedData::instance - .PeasantSpawnClockIncrementSolitaryPopMoreThan100[popularityIndex]; + int popularityIndex = PLAYER_FIELD(int, popularity) / 500; + if (PLAYER_FIELD(int, currentPopulation) > 100) { + increment = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSolitaryPopMoreThan100[popularityIndex]; } else { - this->playerDataArray[playerID].vclockIncrement - = DAT_TroopDefinedData::instance - .PeasantSpawnClockIncrementSolitaryPopLessThan101[popularityIndex]; + increment = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSolitaryPopLessThan101[popularityIndex]; } } - this->playerDataArray[playerID].vclock += this->playerDataArray[playerID].vclockIncrement; - if (this->playerDataArray[playerID].vclock >= 4000) { - this->playerDataArray[playerID].vclock = 4000; - } else if (this->playerDataArray[playerID].vclock < 0) { - this->playerDataArray[playerID].vclock = 0; + PLAYER_FIELD(int, vclockIncrement) = increment; + int const vclock = PLAYER_FIELD(int, vclock) + increment; + PLAYER_FIELD(int, vclock) = vclock; + if (vclock >= 4000) { + PLAYER_FIELD(int, vclock) = 4000; + } else if (vclock < 0) { + PLAYER_FIELD(int, vclock) = 0; } - if (this->playerDataArray[playerID].vclockIncrement == 0) { - this->playerDataArray[playerID].vclock = 2000; + if (increment == 0) { + PLAYER_FIELD(int, vclock) = 2000; } - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id <= 0) { + if (PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_KEEP].id) <= 0) { continue; } - int campgroundID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id; + int campgroundID = PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id); if (campgroundID <= 0) { continue; } - if (this->playerDataArray[playerID].vclock >= 4000) { - if (this->playerDataArray[playerID].currentPopulation < this->playerDataArray[playerID].populationCap - && this->playerDataArray[playerID].availablePeasantsOrHousedPeasants < 24) { + if (PLAYER_FIELD(int, vclock) >= 4000) { + if (PLAYER_FIELD(int, currentPopulation) < PLAYER_FIELD(int, populationCap) + && PLAYER_FIELD(int, availablePeasantsOrHousedPeasants) < 24) { // Havent reached peasant limit. - int x = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].xEntry; - int y = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].yEntry; - this->playerDataArray[playerID].vclock = 2000; + int x = PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].xEntry) * 8; + int y = PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].yEntry) * 8; + PLAYER_FIELD(int, vclock) = 2000; int peasantUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( - playerID, playerID, x * 8, y * 8, - DAT_BuildingsState::instance.buildings[campgroundID].terrainHeightUnk, Map::Units::UT_PEASANT); + playerID, playerID, x, y, DAT_BuildingsState::instance.buildings[campgroundID].terrainHeightUnk, + Map::Units::UT_PEASANT); if (peasantUnitID != 0) { // Successfully spawned peasant. DAT_UnitsState::instance.units[peasantUnitID].animationCycleNumber @@ -99,8 +108,8 @@ namespace Game { MACRO_CALL(UI::Helpers_Func::SetTutorialBuildingActionState)(9, Map::Buildings::BT_HOVEL); } } - if (this->playerDataArray[playerID].field_0x42 == 1) { - this->playerDataArray[playerID].field_0x42 = 0; + if (PLAYER_FIELD(short, field_0x42) == 1) { + PLAYER_FIELD(short, field_0x42) = 0; if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID && MACRO_CALL_MEMBER( Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() @@ -111,15 +120,15 @@ namespace Game { } } } - } else if (this->playerDataArray[playerID].vclock <= 0 - && this->playerDataArray[playerID].availablePeasantsOrHousedPeasants - + this->playerDataArray[playerID].populationRelatedCrowdingCountUnk + } else if (PLAYER_FIELD(int, vclock) <= 0 + && PLAYER_FIELD(int, availablePeasantsOrHousedPeasants) + + PLAYER_FIELD(int, populationRelatedCrowdingCountUnk) > 4) { - this->playerDataArray[playerID].vclock = 2000; + PLAYER_FIELD(int, vclock) = 2000; // let a peasant disappear? MACRO_CALL_MEMBER(GameStateStructures_Func::despawnPeasantOrWorker, this)(playerID); - if (this->playerDataArray[playerID].field_0x42 == 0) { - this->playerDataArray[playerID].field_0x42 = 1; + if (PLAYER_FIELD(short, field_0x42) == 0) { + PLAYER_FIELD(short, field_0x42) = 1; if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID && MACRO_CALL_MEMBER( Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() @@ -133,5 +142,7 @@ namespace Game { } } +#undef PLAYER_FIELD + } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index d3818dbf80..64e534cfa9 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -18037,7 +18037,7 @@ SHC_3BB0A8C1_0x0045B3E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045B460 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045B4A0 | 84.0% | Shared store moves the anchor +SHC_3BB0A8C1_0x0045B4A0 | 78.4% | structure matches but for one lea, rest is allocation SHC_3BB0A8C1_0x0045B6B0 | 100.0% | Reimplemented From 01744ce85dc6813100e3f00dc78beca30a83fb33 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 30 Nov 2026 00:08:54 +0100 Subject: [PATCH 13/18] quick_diff/quick_styles: --structural scores kind, shape and delta structural_diff.py only scores files in the build list, so a source variant could not be judged by structure before it replaced the source. quick_diff.py --structural reports kind/shape/delta for any file from its one compile and diffs the register-anonymised lines; quick_styles.py --structural ranks the variants by delta, then kind, then ratio. Co-Authored-By: Claude Opus 5.5 (1M context) --- tools/reimplementation-helper/batch/README.md | 4 +- .../batch/quick_diff.py | 20 ++++++++- .../batch/quick_styles.py | 44 ++++++++++++++----- .../batch/structural_diff.py | 5 ++- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/tools/reimplementation-helper/batch/README.md b/tools/reimplementation-helper/batch/README.md index b6e5ef8793..daafe756e2 100644 --- a/tools/reimplementation-helper/batch/README.md +++ b/tools/reimplementation-helper/batch/README.md @@ -44,7 +44,7 @@ functions and the traps - is in [../../compiler-internals/finding-mismatches.md] | `diff_anchor.py [--run] [NAME]` | Functions whose struct/loop base anchor differs from the original's (`lea esi, [ecx + 0x367e4]` against a different constant). Every displacement in the function then differs while the opcodes still line up, which reads as allocator noise - it is the whole gap between 83.7% and 61.7% on `processFoodConsumption`. The delta is the distance between the two fields MSVC chose; the decompilation names the original's as the pointer it starts the loop with. | | `diff_lea.py [--run] [NAME]` | `lea reg, [a + b]` on our side where the original has the destructive `add a, b`: `field = snapshotLocal + delta` has to keep the snapshot, `field = field + delta` updates the cached read in place. Drop the local when nothing between the read and the use writes the field - that was `updatePopularity`'s last 6.1%. `RLEA` is the reverse, a local the original keeps and we do not. | | `diff_signed.py [NAME-SUBSTRING]` | Comparisons the original makes signed and we make unsigned (or the reverse): `jg/jge/jl/jle` against our `ja/jae/jb/jbe` over the same `cmp`. Fix it by comparing through `(int)` in the source, not by editing the generated header, and re-run the tool to confirm - one instruction barely moves the percentage. | -| `quick_diff.py FILE.cpp... [-q] [--name FUNCTION]` | Compile one file with `/FA` and diff it against the original function disassembled straight from `_original/` - about 6 seconds, no link and no reccmp, so a dozen source variants can be tried in the time of one build. Handles free functions (the mangled scope is the innermost namespace), x87 operand spellings (`fmulp ST(2), ST(0)` vs `fmulp st(2)`, `__real@` constants) and indexed local-array slots (`_arr$[esp+edi*8+112]`); before those three fixes every floating-point instruction and every local-array element access on our side mismatched and `structural_diff.py` reported duplicates that did not exist. Shows the whole original function, which reccmp's diff does not when ours is shorter. A triage loop: confirm a 1.0 with `reccmp_report.py --run`, and do not trust a *rise* of its ratio below about 0.6 - on a badly matching function difflib rewards a different alignment, and one such "gain" was a 10-point loss in reccmp. | +| `quick_diff.py FILE.cpp... [-q] [--structural] [--name FUNCTION]` | Compile one file with `/FA` and diff it against the original function disassembled straight from `_original/` - about 6 seconds, no link and no reccmp, so a dozen source variants can be tried in the time of one build. Handles free functions (the mangled scope is the innermost namespace), x87 operand spellings (`fmulp ST(2), ST(0)` vs `fmulp st(2)`, `__real@` constants) and indexed local-array slots (`_arr$[esp+edi*8+112]`); before those three fixes every floating-point instruction and every local-array element access on our side mismatched and `structural_diff.py` reported duplicates that did not exist. Shows the whole original function, which reccmp's diff does not when ours is shorter. A triage loop: confirm a 1.0 with `reccmp_report.py --run`, and do not trust a *rise* of its ratio below about 0.6 - on a badly matching function difflib rewards a different alignment, and one such "gain" was a 10-point loss in reccmp. `--structural` adds `structural_diff.py`'s `kind`/`shape`/`delta` for the same compile, lists the mismatched mnemonics (`add 12/13` is original/ours), and prints the diff on register- and constant-anonymised lines; unlike `structural_diff.py` it takes any file, not only the build list. | | `quick_gl.py FILE.cpp [CALLEE.cpp...] [-q] [-s]` | `quick_diff.py` for a file in `cmake/compiler-flags-gl.txt`: compiles it with `/GL` and LTCG-links it into a throw-away DLL with the `/GL` objects of the callees it calls through `X_Func::` (found in the build list and the GL list), then diffs the function against the original. quick_diff strips `/GL`, so it cannot show an `ecx`/`edx` kept across a call; this does, in about 6 seconds. Needs a full build first (for the callee objects) and the callees' resolvers enabled (`enable_local_functions.py`). `aiFindBuildingToAttack` reads 0.7027 in quick_diff and 1.0000 here, as in reccmp. | | `push_pattern.py FILE.cpp...` | Per call, the argument-push run as `pushes/interleaved` for both sides. A neat run (`0` interleaved) means every argument was already in a register or slot, i.e. the source had a **local**; a mixed run is what writing the expression inline at the call produces. A starred row is a call where the two sides disagree - add the local, or drop it, accordingly. Counts only calls whose set-up it can see, so a differing call *total* means our source has an extra call (a duplicated block), not that the tool failed. | | `perm_decls.py FILE FIRST LAST` | Permute one short run of declarations and keep the order that matches best, scored by `quick_diff.py -s`. Declaration order does **not** decide stack slots - the colorer sorts by reference weight per byte and leaves only *ties* to the order the symbols arrive in, so this pays exactly when two locals of equal weight are declared the wrong way round (`assignSelectionToKey` 95.9% -> 100% from one swapped line) and does nothing otherwise. Run `slot_weight.py` first to see whether the weights tie at all. For initialised declarations the order does decide which *register* each one gets, which is a separate effect worth permuting for. | @@ -58,7 +58,7 @@ functions and the traps - is in [../../compiler-internals/finding-mismatches.md] | `rank_functions.py [filter] [--top N]` | Rank by source lines x (1 - match), so the big badly-matching functions come first instead of the small ones with a low percentage. | | `scan_dispatch.py [filter]` | Functions whose dispatch form disagrees with the original (jump table on one side, if/else-if chain on the other), and which way to fix each. | | `scan_chains.py [filter] [--min N]` | `if/else if` chains comparing one expression against several constants: the candidates to convert when `scan_dispatch.py` says the original has a table we do not. | -| `quick_styles.py FUNC [-s] [style...]` | `try_styles.py` scored by `quick_diff.py` instead of a build: about a second per variant against roughly half a minute for an incremental build plus a reccmp run, so a dozen source forms cost less than one build cycle. Same variant directory (`build-RelWithDebInfo/batch/variants/FUNC/*.cpp`), same table, same winner-restore; `-s` keeps `[esp + N]` offsets, for declaration order. It never touches the source list or the DLL, so it leaves no stale `diff.json` behind. The score is difflib's ratio, not the reccmp percentage - rank the forms here, then confirm the winner with `reccmp_report.py --run`, and mind `quick_diff`'s warning about ratio rises below about 0.6. | +| `quick_styles.py FUNC [-s] [--structural] [style...]` | `try_styles.py` scored by `quick_diff.py` instead of a build: about a second per variant against roughly half a minute for an incremental build plus a reccmp run, so a dozen source forms cost less than one build cycle. Same variant directory (`build-RelWithDebInfo/batch/variants/FUNC/*.cpp`), same table, same winner-restore; `-s` keeps `[esp + N]` offsets, for declaration order. It never touches the source list or the DLL, so it leaves no stale `diff.json` behind. The score is difflib's ratio, not the reccmp percentage - rank the forms here, then confirm the winner with `reccmp_report.py --run`, and mind `quick_diff`'s warning about ratio rises below about 0.6. `--structural` ranks by fewest mismatched mnemonics, then `kind`, then the ratio: on `processPeasantSpawnAndDespawnCycle` the plain ratio preferred the old form (94.2, delta 7) over the committed one (82.9, delta 2), which is the wrong way round once structure is what you are fixing. | | `try_styles.py FUNC [style...]` | Compile and measure several hand-written variants of one function, restore the winner, print the table. Variants live in `build-RelWithDebInfo/batch/variants/FUNC/*.cpp`. | ### Readability passes diff --git a/tools/reimplementation-helper/batch/quick_diff.py b/tools/reimplementation-helper/batch/quick_diff.py index d5b5ab3ebf..46bd8c3e87 100644 --- a/tools/reimplementation-helper/batch/quick_diff.py +++ b/tools/reimplementation-helper/batch/quick_diff.py @@ -1,6 +1,6 @@ """Compile ONE source file and diff its code against the original function, in seconds. -usage: quick_diff.py FILE.cpp... [-q] [-s] [--name FUNCTION] +usage: quick_diff.py FILE.cpp... [-q] [-s] [--structural] [--name FUNCTION] Compiles each file with a DLL compile command plus /FA (no link, no reccmp), disassembles the original function at the file's `// FUNCTION:` address straight from `_original/` with capstone, normalizes both streams (globals, @@ -8,6 +8,13 @@ `-q` prints the ratio only; `-s` keeps `[esp + N]` offsets instead of collapsing them to one token, for chasing stack-slot assignment (declaration order) once everything else matches. +`--structural` adds `structural_diff.py`'s tiers for the same compile - `kind` (mnemonics only), `shape` +(registers and constants anonymised) and `delta` (per-mnemonic count mismatches, listed) - and prints the diff on +the anonymised `shape` lines, so what is left reads as instructions rather than as register names. Unlike +`structural_diff.py`, which only scores files in the build list, this takes any file, so it scores a variant +before it replaces the source. Rank by delta first and kind second: the ratio is dominated by register naming and +can rise while the structure gets worse. + This is a triage loop, not the score: it is stricter than reccmp about register names and looser about stack offsets and constants, so confirm a 1.0000 with `reccmp_report.py --run`. Unlike reccmp it shows the *whole* original function - reccmp cuts the original stream at the length of our function, which hides the original's @@ -28,9 +35,10 @@ quiet = '-q' in sys.argv keep_slots = '-s' in sys.argv +structural = '--structural' in sys.argv # --name NAME: diff that function of a file holding several `// FUNCTION:` markers (default: the first) want = sys.argv[sys.argv.index('--name') + 1] if '--name' in sys.argv else None -_skip = {'-q', '-s', '--name'} | ({want} if want else set()) +_skip = {'-q', '-s', '--structural', '--name'} | ({want} if want else set()) files = [os.path.abspath(f) for f in sys.argv[1:] if f not in _skip] if not files and __name__ == "__main__": sys.exit(__doc__) @@ -221,6 +229,14 @@ def main(): continue ratio = difflib.SequenceMatcher(None, a, b, autojunk=False).ratio() print('##### %s ratio %.4f orig %d ours %d' % (os.path.basename(f), ratio, len(a), len(b))) + if structural: + # imported here: structural_diff imports this module in turn, through asm_streams + import structural_diff + m = structural_diff.measure(f, os.path.basename(f), None, pair=(a, b)) + print('kind %.4f shape %.4f delta %d %s' % ( + m['kind'], m['shape'], m['delta'], + ' '.join('%s %d/%d' % (k, n, o) for k, n, o in m['deltas']))) + a, b = [structural_diff.shape(l) for l in a], [structural_diff.shape(l) for l in b] if not quiet: for l in difflib.unified_diff(a, b, lineterm='', n=2): if not l.startswith(('---', '+++')): diff --git a/tools/reimplementation-helper/batch/quick_styles.py b/tools/reimplementation-helper/batch/quick_styles.py index be2ac35fc4..7b714bd000 100644 --- a/tools/reimplementation-helper/batch/quick_styles.py +++ b/tools/reimplementation-helper/batch/quick_styles.py @@ -12,11 +12,16 @@ variants. The winner is left in the source tree; on a tie with the original, the original stays. usage: - quick_styles.py FunctionName [-s] [style ...] + quick_styles.py FunctionName [-s] [--structural] [style ...] `-s` scores with `[esp + N]` offsets kept (quick_diff -s), which is what makes declaration order and slot assignment visible; use it once everything else matches. +`--structural` ranks by structure instead (quick_diff --structural): fewest mismatched mnemonics +(`delta`) first, then the mnemonic-sequence ratio (`kind`), and the plain ratio only to break what +is left. The ratio is mostly register naming, so it can prefer a variant with the wrong instruction +mix; on processPeasantSpawnAndDespawnCycle the structurally better form (delta 7 -> 2) scored lower. + The score is quick_diff's difflib ratio, which is NOT the reccmp percentage: it is stricter about register names and looser about stack offsets and constants. Use it to rank the forms, then confirm the winner with `reccmp_report.py --run`. Ties are printed rather than broken for you - prefer the @@ -38,20 +43,32 @@ BATCH = Path(__file__).resolve().parent VARIANTS = common.TMP / "variants" RATIO = re.compile(r"ratio\s+([0-9.]+)") +STRUCTURE = re.compile(r"kind ([0-9.]+) shape ([0-9.]+) delta (\d+) *(.*)") -def measure(target, keep_slots, out_dir): +def measure(target, keep_slots, out_dir, structural=False): + """(score, label, error output). Scores compare with `>`; a structural one is (-delta, kind, ratio).""" env = dict(os.environ) env["QD_OUT"] = out_dir argv = [sys.executable, str(BATCH / "quick_diff.py"), str(target), "-q"] if keep_slots: argv.append("-s") + if structural: + argv.append("--structural") proc = subprocess.run(argv, capture_output=True, text=True, env=env) out = proc.stdout + proc.stderr found = RATIO.search(out) if not found: - return None, out - return float(found.group(1)) * 100, None + return None, None, out + ratio = float(found.group(1)) * 100 + if not structural: + return ratio, "%.4f%%" % ratio, None + st = STRUCTURE.search(out) + if not st: + return None, None, out + kind, delta = float(st.group(1)), int(st.group(3)) + label = "delta %-3d kind %.4f ratio %.4f%% %s" % (delta, kind, ratio, st.group(4)) + return (-delta, kind, ratio), label, None def main(): @@ -60,7 +77,8 @@ def main(): sys.exit(__doc__) func = argv.pop(0) keep_slots = "-s" in argv - styles = [a for a in argv if a != "-s"] + structural = "--structural" in argv + styles = [a for a in argv if a not in ("-s", "--structural")] vdir = VARIANTS / func if not vdir.is_dir(): @@ -85,17 +103,17 @@ def main(): # the committed source is a candidate too: a variant that merely ties it must not # replace it (that happened: an identical-scoring variant was left in the tree) shutil.copy(backup, target) - ratio, err = measure(target, keep_slots, out_dir) + ratio, label, err = measure(target, keep_slots, out_dir, structural) results.append((ratio, '_original')) - print("%-32s %s" % ('_original', "COMPILE FAIL" if ratio is None else "%.4f%%" % ratio)) + print("%-32s %s" % ('_original', "COMPILE FAIL" if ratio is None else label)) for style in styles: shutil.copy(vdir / (style + ".cpp"), target) common.format_file(target) - ratio, err = measure(target, keep_slots, out_dir) + ratio, label, err = measure(target, keep_slots, out_dir, structural) results.append((ratio, style)) # four decimals: a variant printing the same first decimal as the baseline has # repeatedly turned out to be slightly below it, which is one instruction - print("%-32s %s" % (style, "COMPILE FAIL" if ratio is None else "%.4f%%" % ratio)) + print("%-32s %s" % (style, "COMPILE FAIL" if ratio is None else label)) if err: for line in err.splitlines(): if "error" in line: @@ -112,8 +130,12 @@ def main(): sys.exit("every variant failed to compile; original restored") best = ranked[0][0] - tied = [s for r, s in ranked if abs(r - best) < 1e-9] - print("\nbest %.4f%% achieved by: %s" % (best, ", ".join(tied))) + tied = [s for r, s in ranked if r == best] + if structural: + print("\nbest delta %d, kind %.4f, ratio %.4f%% achieved by: %s" + % (-best[0], best[1], best[2], ", ".join(tied))) + else: + print("\nbest %.4f%% achieved by: %s" % (best, ", ".join(tied))) keep = '_original' if '_original' in tied else tied[0] if len(tied) > 1: print("(tied -- keeping %s; pick whichever reads best)" % keep) diff --git a/tools/reimplementation-helper/batch/structural_diff.py b/tools/reimplementation-helper/batch/structural_diff.py index a216a2d6e5..e1d9db79a6 100644 --- a/tools/reimplementation-helper/batch/structural_diff.py +++ b/tools/reimplementation-helper/batch/structural_diff.py @@ -121,8 +121,9 @@ def ratio(a, b): return difflib.SequenceMatcher(None, a, b, autojunk=False).ratio() -def measure(path, name, raw): - orig, ours = asm_streams.streams(path) +def measure(path, name, raw, pair=None): + # `pair` takes streams the caller already has, so `quick_diff.py --structural` does not compile twice + orig, ours = pair or asm_streams.streams(path) if ours is None: return None mo, mu = asm_streams.mnemonics(orig), asm_streams.mnemonics(ours) From 8fde231133643b2684323c610b2a9f20c4fedd6e Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 2 Dec 2026 16:15:18 +0100 Subject: [PATCH 14/18] resetVariousCountsAndStatisticsAndStartGoodsAndResources: use main's aiHoldBackTicks PlayerData::aiControlStatusRelated was renamed to aiHoldBackTicks on main. Co-Authored-By: Claude Opus 5.5 (1M context) --- ...resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp index 7d7cf2868b..0bfc0a5645 100644 --- a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp +++ b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp @@ -114,7 +114,7 @@ namespace Game { = (short)this->playerDataArray[playerID].currentResources[Resources::RT_GOLD]; this->playerDataArray[playerID].lastMonthsGold = (short)this->playerDataArray[playerID].currentResources[Resources::RT_GOLD]; - this->playerDataArray[playerID].aiControlStatusRelated = -1000; + this->playerDataArray[playerID].aiHoldBackTicks = -1000; for (int foodType = 0; foodType < countof(this->playerDataArray[playerID].isFoodTypeBanned); ++foodType) { this->playerDataArray[playerID].isFoodTypeBanned[foodType] = 0; } From 7562d8a7bc7a20122e80478e2afb3008b4be61ec Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 22:49:24 +0100 Subject: [PATCH 15/18] GameStateStructures: keep main's headers and main's updateFoodTypesInStockForAllPlayers The branch changed computeBuildingCategoryEntryPointAndDestroyEarlierBuilding's category parameter to BuildingEntryInfoEnum. Main's headers lead, and the enum type does not change code generation, so the header is back to main's PlayerDataBuildingCategoryEnum and the .cpp compares against PDBCE_* instead. updateFoodTypesInStockForAllPlayers is 100% on main as well, so the PR no longer changes it. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Game/GameStateStructures.func.hpp | 6 ++-- src/OpenSHC/Game/GameStateStructures.hpp | 6 ++-- ...oryEntryPointAndDestroyEarlierBuilding.cpp | 27 ++++++++------- .../updateFoodTypesInStockForAllPlayers.cpp | 34 ++++++++++--------- 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures.func.hpp b/src/OpenSHC/Game/GameStateStructures.func.hpp index 473946ddcc..7da2fbd967 100644 --- a/src/OpenSHC/Game/GameStateStructures.func.hpp +++ b/src/OpenSHC/Game/GameStateStructures.func.hpp @@ -4,7 +4,7 @@ #include "OpenSHC/Commands/MappersEnum.hpp" #include "OpenSHC/Game/GameStateStructures.hpp" -#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" #include "OpenSHC/Game/Resources/ResourceType.hpp" #include "OpenSHC/IO/PackagedFileMagicNum.hpp" #include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" @@ -13,7 +13,7 @@ namespace Game { namespace GameStateStructures_Func { using OpenSHC::Commands::MappersEnum; - using OpenSHC::Game::Player::Enums::BuildingEntryInfoEnum; + using OpenSHC::Game::Player::PlayerDataBuildingCategoryEnum; using OpenSHC::Game::Resources::ResourceType; using OpenSHC::IO::PackagedFileMagicNum; using OpenSHC::WindowsHelper::Enums::BOOLEnum; @@ -94,7 +94,7 @@ namespace Game { &GameStateStructures::addSignpostToBuildingEntryData) addSignpostToBuildingEntryData; - MACRO_FUNCTION_RESOLVER(void (GameStateStructures::*)(uint, int, BuildingEntryInfoEnum), false, + MACRO_FUNCTION_RESOLVER(void (GameStateStructures::*)(uint, int, PlayerDataBuildingCategoryEnum), false, Address::SHC_3BB0A8C1_0x00456FD0, &GameStateStructures::computeBuildingCategoryEntryPointAndDestroyEarlierBuilding) computeBuildingCategoryEntryPointAndDestroyEarlierBuilding; diff --git a/src/OpenSHC/Game/GameStateStructures.hpp b/src/OpenSHC/Game/GameStateStructures.hpp index 85f531ded4..f7a6cecd2d 100644 --- a/src/OpenSHC/Game/GameStateStructures.hpp +++ b/src/OpenSHC/Game/GameStateStructures.hpp @@ -9,8 +9,8 @@ #pragma once #include "OpenSHC/Commands/MappersEnum.hpp" -#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" #include "OpenSHC/Game/Player/PlayerData.hpp" +#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" #include "OpenSHC/Game/Resources/ResourceType.hpp" #include "OpenSHC/Game/State/MapAndTimeState.hpp" #include "OpenSHC/Game/State/UnitSelectionHotKeyEntry.hpp" @@ -22,7 +22,7 @@ namespace Game { using OpenSHC::Commands::MappersEnum; using OpenSHC::Game::Player::PlayerData; - using OpenSHC::Game::Player::Enums::BuildingEntryInfoEnum; + using OpenSHC::Game::Player::PlayerDataBuildingCategoryEnum; using OpenSHC::Game::Resources::ResourceType; using OpenSHC::Game::State::MapAndTimeState; using OpenSHC::Game::State::UnitSelectionHotKeyEntry; @@ -88,7 +88,7 @@ namespace Game { void addSignpostToBuildingEntryData(int buildingID); void computeBuildingCategoryEntryPointAndDestroyEarlierBuilding( - uint buildingID, int playerID, BuildingEntryInfoEnum category); + uint buildingID, int playerID, PlayerDataBuildingCategoryEnum category); void validateBuildingCategoryReference(int playerID, int param_2); diff --git a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp index 225fe63e72..eca90d9b0f 100644 --- a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp @@ -2,6 +2,7 @@ #include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" #include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Game/Player/PlayerDataBuildingCategoryEnum.hpp" #include "OpenSHC/UI/Enums/MenuViewType.hpp" #include "OpenSHC/Globals/DAT_BuildingsState.hpp" @@ -12,17 +13,17 @@ namespace OpenSHC { namespace Game { - using Player::Enums::BuildingEntryInfoEnum; + using Player::PlayerDataBuildingCategoryEnum; using UI::Enums::MenuViewType; // FUNCTION: STRONGHOLDCRUSADER 0x00456FD0 void GameStateStructures::computeBuildingCategoryEntryPointAndDestroyEarlierBuilding( - uint buildingID, int playerID, BuildingEntryInfoEnum category) + uint buildingID, int playerID, PlayerDataBuildingCategoryEnum category) { int entryX = 0; int entryY = 0; DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; - if (category == Player::Enums::BEIE_KEEP) { + if (category == Player::PDBCE_KEEP) { if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id > 0 && this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id != buildingID) { MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( @@ -56,37 +57,37 @@ namespace Game { // same goes for the destroy call. Writing the read out in every arm (MSVC merges the copies only after // register allocation) makes buildingID look hot enough to keep in a register: 33.5% instead of 64.0%. int existingID; - if (category == Player::Enums::BEIE_MARKETPLACE) { + if (category == Player::PDBCE_MARKETPLACE) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MARKETPLACE].id; - } else if (category == Player::Enums::BEIE_ENGINEERSGUILD) { + } else if (category == Player::PDBCE_ENGINEERSGUILD) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ENGINEERSGUILD].id; - } else if (category == Player::Enums::BEIE_TUNNELERSGUILD) { + } else if (category == Player::PDBCE_TUNNELERSGUILD) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_TUNNELERSGUILD].id; - } else if (category == Player::Enums::BEIE_STOCKPILE) { + } else if (category == Player::PDBCE_STOCKPILE) { // a storage building of this kind already exists, keep the old entry if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_STOCKPILE].id > 0) { return; } goto readEntry; - } else if (category == Player::Enums::BEIE_ARMORY) { + } else if (category == Player::PDBCE_ARMORY) { // a storage building of this kind already exists, keep the old entry if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_ARMORY].id > 0) { return; } goto readEntry; - } else if (category == Player::Enums::BEIE_GRANARY) { + } else if (category == Player::PDBCE_GRANARY) { // a storage building of this kind already exists, keep the old entry if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_GRANARY].id > 0) { return; } goto readEntry; - } else if (category == Player::Enums::BEIE_BARRACKS) { + } else if (category == Player::PDBCE_BARRACKS) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_BARRACKS].id; - } else if (category == Player::Enums::BEIE_MERCENARYPOST) { + } else if (category == Player::PDBCE_MERCENARYPOST) { existingID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_MERCENARYPOST].id; - } else if (category == Player::Enums::BEIE_OILSMELTER) { + } else if (category == Player::PDBCE_OILSMELTER) { goto readEntry; - } else if (category == Player::Enums::BEIE_CAMPGROUND) { + } else if (category == Player::PDBCE_CAMPGROUND) { short orientation = DAT_BuildingsState::instance.buildings[buildingID].orientation; entryX = DAT_BuildingsState::instance.buildings[buildingID].x; entryY = DAT_BuildingsState::instance.buildings[buildingID].y; diff --git a/src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp b/src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp index 9135bdd1a0..e2bb9ec433 100644 --- a/src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp +++ b/src/OpenSHC/Game/GameStateStructures/updateFoodTypesInStockForAllPlayers.cpp @@ -9,22 +9,24 @@ namespace Game { void GameStateStructures::updateFoodTypesInStockForAllPlayers() { for (int playerID = 1; playerID < 9; ++playerID) { - if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id <= 0 - || this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id <= 0) { - continue; - } - this->playerDataArray[playerID].foodTypesInStock = 0; - if (this->playerDataArray[playerID].breadCount > 0) { - this->playerDataArray[playerID].foodTypesInStock = 1; - } - if (this->playerDataArray[playerID].cheeseCount > 0) { - ++this->playerDataArray[playerID].foodTypesInStock; - } - if (this->playerDataArray[playerID].meatCount > 0) { - ++this->playerDataArray[playerID].foodTypesInStock; - } - if (this->playerDataArray[playerID].appleCount > 0) { - ++this->playerDataArray[playerID].foodTypesInStock; + if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id > 0 + && this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id > 0) { + this->playerDataArray[playerID].foodTypesInStock = 0; + if (this->playerDataArray[playerID].breadCount > 0) { + this->playerDataArray[playerID].foodTypesInStock = 1; + } + if (this->playerDataArray[playerID].cheeseCount > 0) { + this->playerDataArray[playerID].foodTypesInStock + = this->playerDataArray[playerID].foodTypesInStock + 1; + } + if (this->playerDataArray[playerID].meatCount > 0) { + this->playerDataArray[playerID].foodTypesInStock + = this->playerDataArray[playerID].foodTypesInStock + 1; + } + if (this->playerDataArray[playerID].appleCount > 0) { + this->playerDataArray[playerID].foodTypesInStock + = this->playerDataArray[playerID].foodTypesInStock + 1; + } } } } From 176f4a342d3726000588c246843f947620a9ea0e Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:08:38 +0100 Subject: [PATCH 16/18] reimplement: SHC_3BB0A8C1_0x0045B4A0 85.30% orig keeps increment in a reg Take the plain-indexing version from 6and8/reimpl/GameStateStructures (gynt, October), adapted to main's PlayerData field names: 78.4% -> 85.3%. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../processPeasantSpawnAndDespawnCycle.cpp | 90 +++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 41 insertions(+), 51 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp index f93cefacea..0547275559 100644 --- a/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp +++ b/src/OpenSHC/Game/GameStateStructures/processPeasantSpawnAndDespawnCycle.cpp @@ -32,11 +32,6 @@ namespace Game { using Map::Units::UnitType; using WindowsHelper::Enums::BOOLEnum; -// The original walks a pointer to vclockIncrement and reaches the other PlayerData fields by their distance from -// it. Indexing playerDataArray[playerID] instead makes MSVC base the loop on popularity. -#define PLAYER_FIELD(type, field) \ - (*(type*)((char*)vclockIncrement + offsetof(PlayerData, field) - offsetof(PlayerData, vclockIncrement))) - // FUNCTION: STRONGHOLDCRUSADER 0x0045B4A0 void GameStateStructures::processPeasantSpawnAndDespawnCycle() { @@ -45,60 +40,57 @@ namespace Game { && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { return; } - int playerID = 1; - int* vclockIncrement = &this->playerDataArray[1].vclockIncrement; - for (; playerID < 9; ++playerID, vclockIncrement += sizeof(PlayerData) / sizeof(int)) { - if (PLAYER_FIELD(int, lordKilledByPlayerID) != 0) { + for (int playerID = 1; playerID < 9; ++playerID) { + if (this->playerDataArray[playerID].lordKilledByPlayerID != 0) { continue; } - if (PLAYER_FIELD(int, playerDeathRelated) != 0 + if (this->playerDataArray[playerID].playerDeathRelated != 0 && DAT_GameSynchronyState::instance.currentGameMode == GM_SOLITARY) { continue; } - // The original picks the increment into a register and stores it once, then reuses that register for - // the sum and for the zero test below. - int increment; if (DAT_GameSynchronyState::instance.currentGameMode != GM_SOLITARY) { - increment = DAT_TroopDefinedData::instance - .PeasantSpawnClockIncrementSkirmish[PLAYER_FIELD(int, popularity) / 500]; + this->playerDataArray[playerID].vclockIncrement + = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSkirmish[this->playerDataArray[playerID].popularity / 500]; } else { - int popularityIndex = PLAYER_FIELD(int, popularity) / 500; - if (PLAYER_FIELD(int, currentPopulation) > 100) { - increment = DAT_TroopDefinedData::instance - .PeasantSpawnClockIncrementSolitaryPopMoreThan100[popularityIndex]; + int popularityIndex = this->playerDataArray[playerID].popularity / 500; + if (this->playerDataArray[playerID].currentPopulation > 100) { + this->playerDataArray[playerID].vclockIncrement + = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSolitaryPopMoreThan100[popularityIndex]; } else { - increment = DAT_TroopDefinedData::instance - .PeasantSpawnClockIncrementSolitaryPopLessThan101[popularityIndex]; + this->playerDataArray[playerID].vclockIncrement + = DAT_TroopDefinedData::instance + .PeasantSpawnClockIncrementSolitaryPopLessThan101[popularityIndex]; } } - PLAYER_FIELD(int, vclockIncrement) = increment; - int const vclock = PLAYER_FIELD(int, vclock) + increment; - PLAYER_FIELD(int, vclock) = vclock; - if (vclock >= 4000) { - PLAYER_FIELD(int, vclock) = 4000; - } else if (vclock < 0) { - PLAYER_FIELD(int, vclock) = 0; + this->playerDataArray[playerID].vclock + = this->playerDataArray[playerID].vclockIncrement + this->playerDataArray[playerID].vclock; + if (this->playerDataArray[playerID].vclock >= 4000) { + this->playerDataArray[playerID].vclock = 4000; + } else if (this->playerDataArray[playerID].vclock < 0) { + this->playerDataArray[playerID].vclock = 0; } - if (increment == 0) { - PLAYER_FIELD(int, vclock) = 2000; + if (this->playerDataArray[playerID].vclockIncrement == 0) { + this->playerDataArray[playerID].vclock = 2000; } - if (PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_KEEP].id) <= 0) { + if (this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_KEEP].id <= 0) { continue; } - int campgroundID = PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id); + int campgroundID = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].id; if (campgroundID <= 0) { continue; } - if (PLAYER_FIELD(int, vclock) >= 4000) { - if (PLAYER_FIELD(int, currentPopulation) < PLAYER_FIELD(int, populationCap) - && PLAYER_FIELD(int, availablePeasantsOrHousedPeasants) < 24) { + if (this->playerDataArray[playerID].vclock >= 4000) { + if (this->playerDataArray[playerID].currentPopulation < this->playerDataArray[playerID].populationCap + && this->playerDataArray[playerID].availablePeasantsOrHousedPeasants < 24) { // Havent reached peasant limit. - int x = PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].xEntry) * 8; - int y = PLAYER_FIELD(int, buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].yEntry) * 8; - PLAYER_FIELD(int, vclock) = 2000; + int x = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].xEntry; + int y = this->playerDataArray[playerID].buildingEntryInfos[Player::Enums::BEIE_CAMPGROUND].yEntry; + this->playerDataArray[playerID].vclock = 2000; int peasantUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( - playerID, playerID, x, y, DAT_BuildingsState::instance.buildings[campgroundID].terrainHeightUnk, - Map::Units::UT_PEASANT); + playerID, playerID, x * 8, y * 8, + DAT_BuildingsState::instance.buildings[campgroundID].terrainHeightUnk, Map::Units::UT_PEASANT); if (peasantUnitID != 0) { // Successfully spawned peasant. DAT_UnitsState::instance.units[peasantUnitID].animationCycleNumber @@ -108,8 +100,8 @@ namespace Game { MACRO_CALL(UI::Helpers_Func::SetTutorialBuildingActionState)(9, Map::Buildings::BT_HOVEL); } } - if (PLAYER_FIELD(short, field_0x42) == 1) { - PLAYER_FIELD(short, field_0x42) = 0; + if (this->playerDataArray[playerID].field_0x42 == 1) { + this->playerDataArray[playerID].field_0x42 = 0; if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID && MACRO_CALL_MEMBER( Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() @@ -120,15 +112,15 @@ namespace Game { } } } - } else if (PLAYER_FIELD(int, vclock) <= 0 - && PLAYER_FIELD(int, availablePeasantsOrHousedPeasants) - + PLAYER_FIELD(int, populationRelatedCrowdingCountUnk) + } else if (this->playerDataArray[playerID].vclock <= 0 + && this->playerDataArray[playerID].availablePeasantsOrHousedPeasants + + this->playerDataArray[playerID].populationRelatedCrowdingCountUnk > 4) { - PLAYER_FIELD(int, vclock) = 2000; + this->playerDataArray[playerID].vclock = 2000; // let a peasant disappear? MACRO_CALL_MEMBER(GameStateStructures_Func::despawnPeasantOrWorker, this)(playerID); - if (PLAYER_FIELD(short, field_0x42) == 0) { - PLAYER_FIELD(short, field_0x42) = 1; + if (this->playerDataArray[playerID].field_0x42 == 0) { + this->playerDataArray[playerID].field_0x42 = 1; if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID && MACRO_CALL_MEMBER( Audio::MSS::SoundSystem_Func::shouldSoundXNotBePlaying, DAT_SoundSystemState::ptr)() @@ -142,7 +134,5 @@ namespace Game { } } -#undef PLAYER_FIELD - } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 70407e3eb9..3fde2712f1 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -18037,7 +18037,7 @@ SHC_3BB0A8C1_0x0045B3E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0045B460 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0045B4A0 | 78.4% | structure matches but for one lea, rest is allocation +SHC_3BB0A8C1_0x0045B4A0 | 85.3% | orig keeps increment in a reg SHC_3BB0A8C1_0x0045B6B0 | 100.0% | Reimplemented From 1ca37b03480d0cb7662b80a7f197741b6a973e51 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:08:39 +0100 Subject: [PATCH 17/18] reimplement: SHC_3BB0A8C1_0x00455E10 92.88% volatile reg naming differs Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- ...tVariousCountsAndStatisticsAndStartGoodsAndResources.cpp | 6 ++---- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp index 0bfc0a5645..2c3054f1b5 100644 --- a/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp +++ b/src/OpenSHC/Game/GameStateStructures/resetVariousCountsAndStatisticsAndStartGoodsAndResources.cpp @@ -78,10 +78,8 @@ namespace Game { this->playerDataArray[playerID].field_0x42 = 0; if (this->mapAndTime.editScenarioExtraOptions != 0) { this->playerDataArray[playerID].taxesSetting = this->mapAndTime.scenarioTaxesSetting; - int scenarioRations = this->mapAndTime.scenarioRationsSetting; - this->playerDataArray[playerID].rationsSetting2 = scenarioRations; - this->playerDataArray[playerID].rationsSetting3 = scenarioRations; - this->playerDataArray[playerID].rationsSetting = scenarioRations; + 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) { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3fde2712f1..45b218da78 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 | 92.2% | Volatile reg naming differs +SHC_3BB0A8C1_0x00455E10 | 92.9% | volatile reg naming differs SHC_3BB0A8C1_0x00455E40 | 0.0% | Pending From e442cb02775856e6741678340cf827c7ca4d01bd Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:02:38 +0100 Subject: [PATCH 18/18] reimplement: SHC_3BB0A8C1_0x00456FD0 84.83% this/entry ptr ebx-ebp swap The tile index of the new entry is computed into a local before the stores and the area is read through it. With the area read back from the entry's tileEntry field the compiler kept category in a callee-saved register; now it stays in edx and is reloaded from its home after the calls, as in the original (64.0% -> 84.8%). What is left is the swap of this (ebx) and the keep branch's player pointer (ebp): reg_scores.py shows this (benefit 38) served before the player pointer temp, the original serves them the other way round. Build mode: plain. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- ...BuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp | 7 +++---- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp index eca90d9b0f..c6173066f1 100644 --- a/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp +++ b/src/OpenSHC/Game/GameStateStructures/computeBuildingCategoryEntryPointAndDestroyEarlierBuilding.cpp @@ -128,14 +128,13 @@ namespace Game { return; } if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[entryY * 400 + entryX] != 0) { + int const tile = DAT_ViewportRenderState::instance.translationMatrix[entryY].addXgetTile + entryX; this->playerDataArray[playerID].buildingEntryInfos[category].id = buildingID; this->playerDataArray[playerID].buildingEntryInfos[category].yEntry = entryY; this->playerDataArray[playerID].buildingEntryInfos[category].xEntry = entryX; - this->playerDataArray[playerID].buildingEntryInfos[category].tileEntry - = DAT_ViewportRenderState::instance.translationMatrix[entryY].addXgetTile + entryX; + this->playerDataArray[playerID].buildingEntryInfos[category].tileEntry = tile; this->playerDataArray[playerID].buildingEntryInfos[category].areaEntry - = DAT_TileMapState::instance - .PathConnectionLayer[this->playerDataArray[playerID].buildingEntryInfos[category].tileEntry]; + = DAT_TileMapState::instance.PathConnectionLayer[tile]; DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 0; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 45b218da78..aea814268a 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17155,7 +17155,7 @@ SHC_3BB0A8C1_0x00456C50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00456EF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00456FD0 | 64.0% | category held in a callee-saved reg +SHC_3BB0A8C1_0x00456FD0 | 84.8% | this/entry ptr ebx-ebp swap SHC_3BB0A8C1_0x004572A0 | 100.0% | Reimplemented