Skip to content

TroopValueState leftovers: 4 functions, 2 at 100% - #528

Closed
gynt wants to merge 13 commits into
mainfrom
reimpl/TroopValueState-leftovers
Closed

gynt wants to merge 13 commits into
mainfrom
reimpl/TroopValueState-leftovers

Conversation

@gynt

@gynt gynt commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Two TroopValueState functions rebuilt from the binary, plus two callees. A separate types commit gives the siege-tent tables at 0xB4C7CC and 0xB4D0F0 their own globals (DAT_ARRAY_00b4c7cc, INT_ARRAY_00b4d0f0); the two ranges become padding in AttackInfoDefinedData, so nothing in memory has two meanings. Those two data entries show as Pending in the status file. The functions were ported from gynt's catch-all branches: this branch is origin/main plus the copied files, compile fixes, matching work and status.

Measured with reccmp, with both the struct and the function resolvers enabled and main's /GL callees on.

2 of 4 functions at 100%. The others hit a wall: the blocker named for each was measured, and the source forms tried against it tied or scored worse.

At 100%

Function Address Ported at
TroopValueState::getPlayerNot1AndHasKeep 0x0051BDE0 new
TribesState::addUnitToTribe 0x00522590 new

Hit a wall

Function Address Match Ported at Why it stops there
TroopValueState::placeSiegeTentsAndAssignEngineers 0x0051B710 97.4% 25.6% Prologue register order, and the constant 1 is built with lea. Was 91.0 before the types commit split the siege-tent tables into their own globals.
TroopValueState::assignMacemenAndKnightsNotFromPlayer1ToTribes 0x0051CCB0 87.3% 55.3% Register naming of the tribe-ID multiply and the unit-owner push.

Behaviour fixes

These were wrong in the decompiler output and are fixed against the binary:

  • assignMacemenAndKnightsNotFromPlayer1ToTribes: the unit scan advanced a Unit* by 0x248 units instead of one; the knight limit is 6 above 18 knights (not at 18 and above); the assignment loop returns once the last knight is placed.
  • placeSiegeTentsAndAssignEngineers: the engineer table has five entries, not four; the queue stops at 250 entries, not 251; the tribe scan advanced a Tribe* by 0xcd tribes; the unit loop ends the function when the units run out instead of looping forever.

Type and header changes are in their own types: commits. Resolver flags are not committed.


🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ

gynt and others added 9 commits November 28, 2026 13:56
…tate-leftovers

Their best match lives only on gynt's catch-all branches, which are ~2080 cpp
files ahead of their merge base and 412 commits behind main. The cpp files are
copied unchanged, without the headers of those branches:
- gynt/reimpl/UI-DisplayElements-pr-development@fbc092e15 (2)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
Renamed fields, globals and resolvers by address, enum-typed arguments,
array fields indexed directly, clang-format.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
SHC_3BB0A8C1_0x0051B710 25.6% placeSiegeTentsAndAssignEngineers
SHC_3BB0A8C1_0x0051CCB0 55.3% assignMacemenAndKnightsNotFromPlayer1ToTribes

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
…binary

Both fix behaviour bugs carried over from the decompiler output:

- assignMacemenAndKnightsNotFromPlayer1ToTribes 55.3 -> 68.6: the unit scan
  advanced a Unit* by 0x248 units instead of one, the knight limit is 6 above
  18 knights (not at 18 and above), and the knight assignment loop returns
  once the last knight is placed (Ghidra left an empty `if`). Left: the knight
  count lives in a stack slot rather than edx.
- placeSiegeTentsAndAssignEngineers 25.6 -> 72.1: the engineer table has five
  entries (field_0xc8[5]), not four; the queue stops at 250 entries, not 251;
  the tribe scan advanced a Tribe* by 0xcd tribes; and the unit loop now ends
  the function when the units run out instead of looping forever. Left: MSVC
  bases both AttackInfoDefinedData arrays on one pointer where the original
  indexes each by absolute address.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
… knights pass

assignMacemenAndKnightsNotFromPlayer1ToTribes 68.6 -> 87.3.

- getPlayerNot1AndHasKeep (100%) taken from gynt's TroopValueState-100-batch-1.
- With it and its caller in compiler-flags-gl.txt, LTCG knows the callee
  leaves edx alone, so the knight count stays in edx across that call and
  is only reloaded after removeUnitFromTribe, as in the original.
- Both tribe loops share one tribeIndex, which gives the stack layout of
  the original.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
…al's loops, addUnitToTribe

placeSiegeTentsAndAssignEngineers 72.1 -> 90.1, addUnitToTribe new at 100.

- The engineer scan tests unitID once on entry and returns from the
  bottom of the loop, so only the path that filled the tribe reaches
  the unitID + 1 test after it; the old trailing `unitID >= max` test
  ran on that path too.
- The tribe scan counter and the engineer tribe read from tribeIDArray
  are one variable, as in Ghidra's output: the original keeps both in esi.
- addUnitToTribe and its leaf setWhetherTribeContainsAnyUnits go in
  compiler-flags-gl.txt with the caller.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
…unt last

Reading the two table entries before the per-player count moves the loads into
the original's order (90.1 -> 91.0). The remaining gap is the table loop: the
original indexes both AttackInfoDefinedData arrays off one register stepping by
4, which our single resolver struct lets MSVC turn into a pointer anchor.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
The original addresses the siege building types at 0xB4C7CC and the
engineers per building at 0xB4D0F0 as two separate globals, indexed from
two absolute bases. Both sat inside AttackInfoDefinedData, which let the
compiler walk them with one pointer at a fixed distance. Each table has
nine entries: the field_0xe8 and field_0xa0c fields the function read
separately are entry 8.

The two ranges become padding in the struct, so its other users keep
their layout and nothing in memory has two meanings.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
With the two tables as separate globals the table loop indexes both from
absolute bases as the original does. What is left is the prologue's
register order and the constant 1 built with lea.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXNCM9MzSpvzeakUrQ
@github-actions

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

3 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 2 of the 4 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
9 Prefer for over while/do 1
14 The shape of a file 2
Where

Rule 9 — Prefer for over while/do

  • src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentsAndAssignEngineers.cpp:98 — while-loop; the guide asks for a for

Rule 14 — The shape of a file

  • src/OpenSHC/Map/Units/TroopValueState/assignMacemenAndKnightsNotFromPlayer1ToTribes.cpp:1 — first include is not TroopValueState; the folder is the class
  • src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentsAndAssignEngineers.cpp:1 — first include is not TroopValueState; the folder is the class

- AttackInfoDefinedData.hpp: take main's version (field_0x9ec[72]). The
  branch's siege-tent globals DAT_ARRAY_00b4c7cc and INT_ARRAY_00b4d0f0 are
  kept; they now overlap main's field_0xc8 and field_0x9ec ranges.
- compiler-flags-gl.txt: keep main's list and append the branch's entries.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

using OpenSHC::Commands::MappersEnum;

MACRO_STRUCT_RESOLVER(MappersEnum[9], false, Address::SHC_3BB0A8C1_0x00B4C7CC) DAT_ARRAY_00b4c7cc;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is this new hpp file really necsesary, why is this not part of AttackInfoDefinedData?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried it, and yes, the separate global is necessary for the match.

Both tables do sit inside AttackInfoDefinedData (0xB4C704): this one is field_0xc8, and INT_ARRAY_00b4d0f0 is field_0x9ec. So I removed both headers and read DAT_AttackInfoDefinedData::instance.field_0xc8[k] and ((int*)...field_0x9ec)[k] instead. With both resolver sets on, placeSiegeTentsAndAssignEngineers went from 97.4% to 91.0% (normalized 88.7% → 82.4%). addUnitToTribe and getPlayerNot1AndHasKeep stayed at 100%.

The reason is visible in the diff. The original indexes both tables from one zero-based counter:

xor edx, edx
mov esi, [edx + AttackInfoDefinedData+0xC8]
mov ecx, [edx + AttackInfoDefinedData+0x9EC]
add edx, 4 / cmp edx, 0x10

Once both are fields of one struct, MSVC knows they are 0x924 apart. It walks one pointer instead, and reaches the other table at a fixed offset from it:

mov edx, AttackInfoDefinedData+0x9EC
mov esi, [edx - 0x924]
mov ecx, [edx]

The original compiler did not do that, so it saw two unrelated objects. In the original source these are separate globals, not members of AttackInfoDefinedData. That is why I am keeping DAT_ARRAY_00b4c7cc and INT_ARRAY_00b4d0f0 as they are.

One follow-up: the merge with main took main's AttackInfoDefinedData.hpp, which still declares field_0xc8..field_0xe8 and field_0x9ec[72] over the same memory. The real fix is in Ghidra: shrink AttackInfoDefinedData (or mark those ranges as gaps) so the two tables are standalone globals there too. That has no effect on codegen.


namespace OpenSHC {

MACRO_STRUCT_RESOLVER(int[9], false, Address::SHC_3BB0A8C1_0x00B4D0F0) INT_ARRAY_00b4d0f0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same comment here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same answer as for DAT_ARRAY_00b4c7cc, see #528 (comment). Folding this table into AttackInfoDefinedData::field_0x9ec lets MSVC address both tables from one pointer ([edx - 0x924] / [edx]). The original indexes them independently from a zero-based counter. That drops placeSiegeTentsAndAssignEngineers from 97.4% to 91.0%, so it has to stay a global of its own.

gynt and others added 3 commits December 3, 2026 22:46
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Declare tribeID = 1 before the tribeIDArraySize reset so the 1 is a mov, not lea from the zero. Measured in the normal /GL build.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
knightLimit as an if/else-if chain (4 or 2) instead of (knightCount > 12) * 2 + 2; that settles the register naming of the setg/lea and both tribe loops. Measured in the normal /GL build (caller and callees in compiler-flags-gl.txt).

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

Its own index and mask locals before the compound |= (as on PR sourcehold#528); measured
100% here in the normal build, no /GL needed.

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

placeSiegeTentsAndAssignEngineers, taken from reimpl/TroopValueState-leftovers
(sourcehold#528), reads the siege building types at 0xB4C7CC and the engineers per
building at 0xB4D0F0 as two separate nine-entry globals indexed from two
absolute bases, as the original does. Reading them through
AttackInfoDefinedData (field_0xc8 and field_0x9ec, five entries each plus
loose fields) lets the compiler walk one pointer at a fixed distance, which
the original does not. This adds the two struct resolvers and their address
entries only; AttackInfoDefinedData is left as on main.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit to gynt/OpenSHC that referenced this pull request Dec 3, 2026
…egeTentsAndAssignEngineers and their TribesState callees

As on reimpl/TroopValueState-leftovers (sourcehold#528), whose versions of the two
functions this branch now takes. Measured in the /GL build mode (no
function-visibility units).

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

The style pass dropped sourcehold#528's comment, but the statement order it explains
is still load-bearing for the 100% match. Comment-only change.

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

gynt commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #554

@gynt gynt closed this Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

declaration/type change This requires changes to the generated/exported types or definitions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant