Skip to content

Reimplement 78 TroopValueState functions at 100% from #275, plus 6 with register allocation differences - #554

Open
gynt wants to merge 101 commits into
sourcehold:mainfrom
gynt:reimpl/pr275-matched
Open

gynt wants to merge 101 commits into
sourcehold:mainfrom
gynt:reimpl/pr275-matched

Conversation

@gynt

@gynt gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

This PR takes the finished part of draft #275 (reimpl/TroopValueState). It has 78 functions that reccmp puts at normalized 100%: 77 in TroopValueState and TribesState::addUnitToTribe. It also has six functions where only register allocation differs: computeSiegeSpotScores, updateTribeBehaviorBasedOnBehaviorType, initializeAttackWaveSlot and pruneStaleArcherPoints / pruneStaleSupportPoints / pruneStaleTentPoints.

The files come from the draft's last commit (4ae047f) with git checkout, on top of origin/main. Nothing else from the draft is in here. The draft also carries:

  • the functions that are not finished yet;
  • style edits to existing main functions;
  • tool changes.

Measured on origin/main 55fa231 (merged into this branch; it includes the ghidra changes in 0701799, c1b50c4 and 87e40b6), after the goto-free rewrite of assignBehaviorTypeAndLinkSupportTribe. Earlier measurements were on 592a17d, 40cbb1c, 06cdd81 and c1b50c4. I built the DLL with a full build (build_parallel.py -j 3) and then ran reccmp. The build list held:

  • main's reimplemented files;
  • these 84 files.

Both resolver sets were switched on: the function resolvers through .local, and the struct resolvers through enable_reimplemented_data.py. None of the 78 dropped on main's headers. I measured again after the style pass (below), after merging 592a17d, 06cdd81 and c1b50c4, with the same result each time. The table shows the numbers from the last run.

Register allocation only. For each of these, I compared the reccmp diff with registers, stack slots and jump displacements normalized. All remaining lines are of these kinds, and none changes behaviour:

  • register or slot choice;
  • push placement and order;
  • load order;
  • an un-coalesced reg-reg copy;
  • base/index order;
  • a constant held in a register vs an immediate;
  • alignment padding and jump displacements that follow from the code size.

Block layout, store order, instruction selection and computation all match the original. Each has main's status remark <pct>% | Register allocation differs.

  • computeSiegeSpotScores (96.4 / 92.0): register swaps and base/index order.
  • updateTribeBehaviorBasedOnBehaviorType (97.5 / 82.8):
    • The unknownAttackRelatedUpdateCounter increment and reload are scheduled two instructions earlier, with an extra movzx ecx, ax copy.
    • The x/y destination loads use [eax+eax+X] before the final add eax, eax instead of after it. The argument pushes are the same values in the same order.
    • The 5-byte jmp $+5; lea ecx,[ecx] loop-alignment pad becomes a 1-byte nop, and the jump-table entries shift accordingly.
  • initializeAttackWaveSlot (96.9 / 91.3): one argument push esi and the mov ecx, LowLevelMemory load swap places with the attackWaveTicker store. The order of the memory stores is unchanged.
  • pruneStaleArcherPoints / pruneStaleSupportPoints / pruneStaleTentPoints (80.5 / 78.4 each):
    • The original pushes eax, which is known to be 0 at that point, where ours pushes the immediate 0, and there is a register swap.
    • Our code is one byte longer, so the two loop jumps are one byte longer.
Function Address reccmp normalized Status
TribesState::addUnitToTribe 0x00522590 100.0 100.0 100.0% | Reimplemented
TroopValueState::addUnitToNewTribe 0x0051BC70 100.0 100.0 100.0% | Reimplemented
TroopValueState::advanceAttackWaveStaging 0x0051D690 100.0 100.0 100.0% | Reimplemented
TroopValueState::aiControlNonSkirmishUnitMovement 0x00520F70 100.0 100.0 100.0% | Reimplemented
TroopValueState::aiRecomputeAttacks 0x005202B0 100.0 100.0 100.0% | Reimplemented
TroopValueState::aiRecomputeAttacks2 0x005200A0 100.0 100.0 100.0% | Reimplemented
TroopValueState::applyTribeBehaviorTypes 0x00518870 100.0 100.0 100.0% | Reimplemented
TroopValueState::assignBehaviorTypeAndLinkSupportTribe 0x00518A10 100.0 100.0 100.0% | Reimplemented
TroopValueState::assignMacemenAndKnightsNotFromPlayer1ToTribes 0x0051CCB0 100.0 100.0 100.0% | Reimplemented
TroopValueState::assignTribeToSupportPoint 0x0051B190 100.0 100.0 100.0% | Reimplemented
TroopValueState::buildRallyPointsFromSiegeUnits 0x00519690 100.0 100.0 100.0% | Reimplemented
TroopValueState::calculateArch2ClosestToTribeTargetUnit 0x0051AB60 100.0 100.0 100.0% | Reimplemented
TroopValueState::calculateHigh2ClostestToTribeTargetUnit 0x0051A9F0 100.0 100.0 100.0% | Reimplemented
TroopValueState::calculateTile2MoatValueClosestToUnit 0x0051A780 100.0 100.0 100.0% | Reimplemented
TroopValueState::calculateTile2PeoplValueClosestToUnit 0x0051A140 100.0 100.0 100.0% | Reimplemented
TroopValueState::claimArcherAttackPoint 0x0051AFF0 100.0 100.0 100.0% | Reimplemented
TroopValueState::collectArcherUnitsByLocation 0x0051CA50 100.0 100.0 100.0% | Reimplemented
TroopValueState::commandUnitsToMoveToKeep 0x0051C800 100.0 100.0 100.0% | Reimplemented
TroopValueState::computeAttackWaveTroopComposition 0x00519310 100.0 100.0 100.0% | Reimplemented
TroopValueState::countAvailablePeopleValueSlots 0x0051A0C0 100.0 100.0 100.0% | Reimplemented
TroopValueState::decideAndExecuteTribeAttackAction 0x0051D2B0 100.0 100.0 100.0% | Reimplemented
TroopValueState::decrementTileMap1104 0x0051D5D0 100.0 100.0 100.0% | Reimplemented
TroopValueState::executeDelayedAttackWaveTargetAssignment 0x00520CD0 100.0 100.0 100.0% | Reimplemented
TroopValueState::exitSiegeEquipmentForWave 0x0051BD10 100.0 100.0 100.0% | Reimplemented
TroopValueState::expandAIZoneLayerStage1 0x0051ACE0 100.0 100.0 100.0% | Reimplemented
TroopValueState::expandAIZoneLayerStage2 0x0051AE00 100.0 100.0 100.0% | Reimplemented
TroopValueState::expandAIZoneLayerStage3 0x0051AE90 100.0 100.0 100.0% | Reimplemented
TroopValueState::findEnemyBuildingsClosestToUnit 0x00519FB0 100.0 100.0 100.0% | Reimplemented
TroopValueState::findEnemyLord 0x0051A220 100.0 100.0 100.0% | Reimplemented
TroopValueState::findEnemyTowersOrGates 0x0051A330 100.0 100.0 100.0% | Reimplemented
TroopValueState::findEnemyWalls 0x00519CC0 100.0 100.0 100.0% | Reimplemented
TroopValueState::findNearestAvailableScalePoint 0x00519E40 100.0 100.0 100.0% | Reimplemented
TroopValueState::findNearestDiggableMoatPoint 0x0051A8A0 100.0 100.0 100.0% | Reimplemented
TroopValueState::findOrReserveArcherPointSlot 0x0051AF20 100.0 100.0 100.0% | Reimplemented
TroopValueState::findTribeWithMoatAttackBehavior 0x0051A920 100.0 100.0 100.0% | Reimplemented
TroopValueState::getClosestWideValueBasedOnPlayer 0x0051A5B0 100.0 100.0 100.0% | Reimplemented
TroopValueState::getPlayerNot1AndHasKeep 0x0051BDE0 100.0 100.0 100.0% | Reimplemented
TroopValueState::getSiegeIndexForTile 0x0051B240 100.0 100.0 100.0% | Reimplemented
TroopValueState::getSupportPointIndex 0x0051B0C0 100.0 100.0 100.0% | Reimplemented
TroopValueState::getValueOfTroopType 0x0051C360 100.0 100.0 100.0% | Reimplemented
TroopValueState::giveLightPitchInstructionToUnitClosestToPitch 0x0051BF70 100.0 100.0 100.0% | Reimplemented
TroopValueState::ifNecessaryThrowOilAutomatically 0x0051C570 100.0 100.0 100.0% | Reimplemented
TroopValueState::initializeAttackZoneSearch 0x00519850 100.0 100.0 100.0% | Reimplemented
TroopValueState::initializeOrAdvanceAttackWave 0x0051FF90 100.0 100.0 100.0% | Reimplemented
TroopValueState::isLessThanPercentageOfTribesInAttackDying 0x00518BB0 100.0 100.0 100.0% | Reimplemented
TroopValueState::moveTowardsParticularUnits 0x0051D510 100.0 100.0 100.0% | Reimplemented
TroopValueState::moveTribeToNearbyGatehouse 0x0051D450 100.0 100.0 100.0% | Reimplemented
TroopValueState::moveTribeToReachableNearbyTile 0x0051D1C0 100.0 100.0 100.0% | Reimplemented
TroopValueState::placeSiegeTentAtAttackAngle 0x0051B310 100.0 100.0 100.0% | Reimplemented
TroopValueState::placeSiegeTentsAndAssignEngineers 0x0051B710 100.0 100.0 100.0% | Reimplemented
TroopValueState::placeTunnelEntrances 0x0051B570 100.0 100.0 100.0% | Reimplemented
TroopValueState::placeTunnelForEveryTunneler 0x0051BA30 100.0 100.0 100.0% | Reimplemented
TroopValueState::playAttackAlarmSound 0x0051B6C0 100.0 100.0 100.0% | Reimplemented
TroopValueState::processSpottedEnemyTiles 0x0051F950 100.0 100.0 100.0% | Reimplemented
TroopValueState::queueOilThrowForIdleArchers 0x0051C470 100.0 100.0 100.0% | Reimplemented
TroopValueState::recomputeTargetedBuildingTilesArray 0x005198C0 100.0 100.0 100.0% | Reimplemented
TroopValueState::recomputeTotalTroopValueOfTroopsNearKeep 0x0051FA70 100.0 100.0 100.0% | Reimplemented
TroopValueState::recountTotalTroopValue 0x0051F9C0 100.0 100.0 100.0% | Reimplemented
TroopValueState::redeployArchersToDefensivePositions 0x0051CBD0 100.0 100.0 100.0% | Reimplemented
TroopValueState::registerSpottedEnemyTile 0x0051BE60 100.0 100.0 100.0% | Reimplemented
TroopValueState::renderAttackInfoDebugOverlay 0x00518C50 100.0 100.0 100.0% | Reimplemented
TroopValueState::scanForArcherPoints 0x0051F340 100.0 100.0 100.0% | Reimplemented
TroopValueState::scanForSupportPoints 0x0051F4B0 100.0 100.0 100.0% | Reimplemented
TroopValueState::searchTribeWithProperties 0x005182A0 100.0 100.0 100.0% | Reimplemented
TroopValueState::sendAttackingPatrolTribeToComputedDestination 0x0051D0D0 100.0 100.0 100.0% | Reimplemented
TroopValueState::setGate2 0x0051A2D0 100.0 100.0 100.0% | Reimplemented
TroopValueState::setMoat3 0x0051A720 100.0 100.0 100.0% | Reimplemented
TroopValueState::setRallyPointForLaddermenTribe 0x00519790 100.0 100.0 100.0% | Reimplemented
TroopValueState::setScale3 0x00519DE0 100.0 100.0 100.0% | Reimplemented
TroopValueState::setTown2 0x00519F50 100.0 100.0 100.0% | Reimplemented
TroopValueState::setWide3 0x0051A550 100.0 100.0 100.0% | Reimplemented
TroopValueState::shouldLightPitchBasedOnTroopValue 0x0051BF20 100.0 100.0 100.0% | Reimplemented
TroopValueState::trySendKnightTribeToAttack 0x0051D020 100.0 100.0 100.0% | Reimplemented
TroopValueState::trySendMacemenTribeToAttack 0x0051CF90 100.0 100.0 100.0% | Reimplemented
TroopValueState::updateActiveAttackWaveState 0x0051D730 100.0 100.0 100.0% | Reimplemented
TroopValueState::updateArcherBrazierProximityFlags 0x0051C2D0 100.0 100.0 100.0% | Reimplemented
TroopValueState::updateAttackInfoTick 0x0051FE80 100.0 100.0 100.0% | Reimplemented
TroopValueState::updateInProgressAttackWave 0x00520450 100.0 100.0 100.0% | Reimplemented
TroopValueState::computeSiegeSpotScores 0x00519990 96.4 92.0 96.4% | Register allocation differs
TroopValueState::updateTribeBehaviorBasedOnBehaviorType 0x0051D7F0 97.5 82.8 97.5% | Register allocation differs
TroopValueState::initializeAttackWaveSlot 0x00518180 96.9 91.3 96.9% | Register allocation differs
TroopValueState::pruneStaleArcherPoints 0x0051AF70 80.5 78.4 80.5% | Register allocation differs
TroopValueState::pruneStaleSupportPoints 0x0051B110 80.5 78.4 80.5% | Register allocation differs
TroopValueState::pruneStaleTentPoints 0x0051B290 80.5 78.4 80.5% | Register allocation differs

Style pass. Every file follows REIMPLEMENTATION_STYLE_GUIDE.md as it is on main now. The style changes are folded into the per-function commits:

  • Unused using declarations are dropped in 14 files.
  • Self-operations are used instead of x = x + 1 / x = x | 0x10 (++, --, +=, |=).
  • param_N names became real names in four functions:
    • assignBehaviorTypeAndLinkSupportTribe: attackWave, playerID, behaviorType, updateLimitBase, updateLimitStep;
    • initializeOrAdvanceAttackWave: attackWave;
    • placeSiegeTentsAndAssignEngineers: attackWave, useAttackAngle;
    • shouldLightPitchBasedOnTroopValue: unusedUnk.
  • Enums instead of numbers:
    • AITT_CATAPULT / AITT_TREBUCHET / AITT_FIRE_BALLISTA replace (AITribeType)0x16..0x18.
    • In exitSiegeEquipmentForWave, tribe types 19 to 21 have no name yet, so they stay as decimal literals with a comment.
  • countof(DAT_TribesState::instance.tribes) and countof(DAT_TileMapState::instance.AIInfoLayer) replace 1250 and 80400.
  • Arguments with no enum are written in decimal: 50, 20, 30, and field_0x3c5 = 34.
  • scanForArcherPoints compared against a raw '\x02' control character in the source. It now compares against 2.
  • for loops replace the guarded do-while in giveLightPitchInstructionToUnitClosestToPitch and the plain while loops in placeSiegeTentsAndAssignEngineers and placeTunnelForEveryTunneler.
  • Two do-whiles stay as they are; the PR comment with the pre-review notes says why:
    • In findEnemyLord, the for form measured 92.1% instead of 100%, so I reverted it.
    • In ifNecessaryThrowOilAutomatically, the loop body sets the count it runs to.
  • assignMacemenAndKnightsNotFromPlayer1ToTribes and placeSiegeTentsAndAssignEngineers now include their own ../TroopValueState.func.hpp first.
  • The five register-only functions added later got the same pass:
    • unused usings dropped;
    • initializeAttackWaveSlot's parameters named attackWave / signpostChoice;
    • 0 instead of '', 16 / -1 / 96 instead of hex;
    • countof(...playerInfo) instead of 8.
    • Their match did not change.
  • clang-format is applied to all files.
  • Pre-review pass (review: commit):
    • named constants:
      • TribeState (TS_*) for tribeState;
      • AIInfoFlag for the AI info layer bits;
      • SSS_UNUSABLE for the siege spot scores;
      • the Logic1 keep/wall flags;
      • STBT_0x410_SIEGE_EQUIPMENT_CONSTRUCTION;
      • countof() for the archer point, support point, engineer queue and attack vector tables;
    • redundant casts dropped, TRUE/FALSE and truthiness;
    • const locals, +=, and "Mace_s4.wav" from string-literals.hpp;
    • a dead security-cookie read removed;
    • unused includes removed;
    • the comments that only explained a form kept for the match moved into a PR comment.
    • Every function keeps its score in a full reccmp build.

Header changes. None. Since 0701799, main declares both of the per-player arrays that placeSiegeTentsAndAssignEngineers reads:

  • AttackInfoDefinedData::field_0xc8[9], the MappersEnum[9] at 0x00B4C7CC. The PR's own DAT_ARRAY_00b4c7cc.hpp is removed.
  • INT_ARRAY_00b4d0f0, the same header the PR had added.

All headers and the address table are main's, including the TribeState enum below. The PR carries no header changes.

The 5999 limits use main's new SSS_UNUSABLE_THRESHOLD.

After the 0701799 merge, initializeOrAdvanceAttackWave dropped to 46.8%, because main's smaller AttackInfoDefinedData changes how the compiler assigns its registers. Including Map/Enums/TribeState.hpp brings it back to 100%; the PR comment explains this.

TribeState enum: Map/Enums/TribeState.hpp and TribeStateShort.hpp now come from main (87e40b6), byte-identical to this PR's own enum commit:

/GL entries (cmake/compiler-flags-gl.txt) are the 13 the draft uses:

  • collectArcherUnitsByLocation
  • getPlayerNot1AndHasKeep
  • getValueOfTroopType
  • recountTotalTroopValue
  • ifNecessaryThrowOilAutomatically
  • assignMacemenAndKnightsNotFromPlayer1ToTribes
  • placeSiegeTentsAndAssignEngineers
  • scanForArcherPoints
  • placeTunnelForEveryTunneler
  • giveLightPitchInstructionToUnitClosestToPitch
  • computeSiegeSpotScores
  • TribesState::addUnitToTribe
  • TribesState::setWhetherTribeContainsAnyUnits: already on main; listed because addUnitToTribe calls it.

There are no function-visibility entries.

Overlap with open PRs:

  • Reimplement 66 TroopValueState functions at 100% #501 (reimpl/TroopValueState-100-batch-1) has 66 of these files. The GL list also differs from Reimplement 66 TroopValueState functions at 100% #501's.
    • 56 files are identical.
    • 10 differ: expandAIZoneLayerStage1/2/3, and the style pass here touched seven more: aiRecomputeAttacks2, collectArcherUnitsByLocation, executeDelayedAttackWaveTargetAssignment, findEnemyLord, recountTotalTroopValue, setRallyPointForLaddermenTribe, shouldLightPitchBasedOnTroopValue.
    • Whichever PR is merged second will conflict on those files.
  • TroopValueState leftovers: 4 functions, 2 at 100% #528 (draft, TroopValueState leftovers) has four of these files and the same two Globals headers: addUnitToTribe, assignMacemenAndKnightsNotFromPlayer1ToTribes, getPlayerNot1AndHasKeep, placeSiegeTentsAndAssignEngineers.
  • Reimpl/TribesState development #279 (draft, TribesState) has addUnitToTribe.
  • The five register-only functions added later are in no other open PR.
  • 12 of the 100% functions are in no other ready PR:
    • addUnitToTribe
    • assignBehaviorTypeAndLinkSupportTribe
    • assignMacemenAndKnightsNotFromPlayer1ToTribes
    • commandUnitsToMoveToKeep
    • exitSiegeEquipmentForWave
    • giveLightPitchInstructionToUnitClosestToPitch
    • ifNecessaryThrowOilAutomatically
    • initializeOrAdvanceAttackWave
    • placeSiegeTentsAndAssignEngineers
    • placeTunnelForEveryTunneler
    • scanForArcherPoints
    • updateInProgressAttackWave

Commits. First the header commit, then the GL commit, then one commit per function. Then come:

  • a comment-only commit that restores TroopValueState leftovers: 4 functions, 2 at 100% #528's tribeID comment;
  • the merge of origin/main 592a17d;
  • one commit per added register-only function. Each function commit updates its line in status/addresses-SHC-3BB0A8C1.txt;
  • the TribeState enum commit, the review: commit, and the switch to the TS_ prefix;
  • the merges of origin/main 0701799 and 06cdd81, and the adaptation to them (931d540 + 43b1738);
  • the merges of origin/main c1b50c4, 87e40b6 and 55fa231 (no adaptation needed), and the goto-free assignBehaviorTypeAndLinkSupportTribe.

The draft #275 stays open for the functions that are not finished. These do not qualify as register-only:

  • recountAttackTroopValue: the fill call moves across stores.
  • assignBehaviorTypeToNearbyTribes: store order, a folded load, and an extra ret.
  • executeAttackWaveTargetAssignment: setg arithmetic instead of branches.
  • placeSiegeTentOrTunnelAtSuitableLocationAndAssignEngineers: block layout.
  • scanForSiegeTentPoints: frame size.
  • recomputeAttackInfo: layout.
  • sortAttackInfoTribeIDArrayBasedOn: 85.1% raw and 68.0% normalized on main's struct since 0701799 (it was 76.0%). Register allocation and the instruction count still differ.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

gynt and others added 30 commits December 4, 2026 15:26
placeSiegeTentsAndAssignEngineers reads the per-player MappersEnum[9] array at
0x00B4C7CC and the int[9] array at 0x00B4D0F0. Main has no header for either,
so this adds both (resolver false, as generated) and their two Address enum
entries. Taken unchanged from the draft branch of sourcehold#275.

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

The same 13 entries the draft sourcehold#275 uses: the callers and callees that need
/GL on both sides so MSVC omits the repeated mov ecx, this. setWhetherTribeContainsAnyUnits
is already reimplemented on main; it is listed because addUnitToTribe calls it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
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
sourcehold#554 carries TribesState::addUnitToTribe too, written `size++` where this branch had `++size`. Taking its exact text makes the two PRs add an identical file, so whichever merges second has no conflict on it. Still 100.0 / normalized 100.0 (full build and reccmp on f28b77b).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…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 and others added 6 commits December 4, 2026 16:05
Brings in 592a17d, which fixes findBestAttackTileByPathCost's return type.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt gynt changed the title Reimplement 78 TroopValueState functions at 100% from #275, plus 1 with register allocation differences Reimplement 78 TroopValueState functions at 100% from #275, plus 6 with register allocation differences Dec 4, 2026
gynt and others added 2 commits December 4, 2026 17:20
Tribe::tribeState takes three values in the reimplemented code:
0 for a free slot, 2 once createTribe has set the tribe up, and 3
once a tribe has been emptied or given up. 1 is never written.
TRS_ is used because TS_ already belongs to TreeState.

TribeStateShort matches the short field. Tribe.hpp still declares
the field as plain short and is left unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
- Named constants instead of magic numbers:
  - TribeState (TRS_*) for tribeState;
  - AIInfoFlag for the AI info layer bits;
  - SSS_UNUSABLE for siege spot scores;
  - the Logic1 flags for the keep/wall test;
  - STBT_0x410_SIEGE_EQUIPMENT_CONSTRUCTION;
  - countof() instead of literal table sizes.
- Removed redundant casts.
- TRUE/FALSE returns, and truthiness for the dying/tribeID tests.
- const on locals that are assigned once, and `+=` instead of a
  self-assignment.
- "Mace_s4.wav" now comes from string-literals.hpp.
- Removed a dead security-cookie read.
- Removed unused includes. initializeOrAdvanceAttackWave keeps
  Map.func.hpp because its code changes without it.
- Removed the comments that only explained a form kept for the
  match; they are now in a PR comment. Also removed a stale
  comment on the size of the tent point table.

Every function keeps its score in a full reccmp build.

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

Pre-review notes: forms kept only for the match, and header types that look wrong. The code comments that used to explain these forms have been removed and are collected here.

The percentages are from a full reccmp build where marked "reccmp". The others are quick_diff.py ratios, measured against the kept form's own ratio.

findEnemyLord

  • Expected: a for loop.
  • Kept: the do { } while loop.
  • Reason: the for form measured 92.1% (reccmp), against 100% for the kept form.

ifNecessaryThrowOilAutomatically

  • Expected: a for/while loop over the construction tiles.
  • Kept: the do { } while.
  • Reason: getBuildingSizeIndexMappingData sets constructionTileCount on its first call. A loop that tests the count first would read a stale value, so the do-while is both the original's order and the correct semantics.

placeSiegeTentsAndAssignEngineers

  • Expected: tribeIDArraySize = 0; before int tribeID = 1;.
  • Kept: tribeID is initialised first. The original loads the 1 with a mov; it does not derive it from the zero. Swapped, the ratio drops from 0.9926 to 0.9877.
    • tribeID cannot move into the for header, because it is still used after the loop.
  • Also kept: the for (;;) walk over the units.

assignBehaviorTypeAndLinkSupportTribe

  • The two goto link jumps are now a found flag: each search sets it and breaks, the second search runs only if the first found nothing, and the link block runs if it is set. This compiles to the same code (100% in a full reccmp build). int and inverted forms of the flag tie; duplicating the link block in each loop with a range check after the first loop scores lower (quick_diff 44.0%).
  • Also kept: the update limit is stored before the behaviour type, in the original's store order.

updateInProgressAttackWave

  • Expected: playerID declared where it is first used, before tribeSizeSumLimit.
  • Kept: it is read at the top of the function, before any call, as the original does. Moved to its first use, the ratio drops from 1.0000 to 0.6893.

initializeOrAdvanceAttackWave

  • Expected: the unused #include "OpenSHC/Map.func.hpp" removed. It has been removed from every other file in the PR.
  • Kept: the include stays in this one file. Without it the function compiles differently: the ratio drops from 1.0000 to 0.5248, with different register use from the first instruction.
  • Also kept: #include "OpenSHC/Map/Enums/TribeState.hpp", which the function does not use. It was added after merging 0701799. Main's AttackInfoDefinedData there has fewer members, and that alone dropped the function to 46.8% in reccmp.
    • The compiler gives &attackWaveTicker[attackWave] its own register and no longer keeps 0 and 1 in ebp/ebx.
    • reg_scores.py shows a new temporary with benefit 78 that takes uses away from attackWave.
    • Padding the struct to its old size does not help, but its old member list does. Declaring a few more names in the translation unit restores 100%, and of the includes tried, TribeState.hpp does.
    • This is the same effect as the Map.func.hpp include above. It may move again with the next header update.

Casts kept for the match (the review: drop redundant casts commit removed every cast whose removal leaves the code identical)

  • initializeOrAdvanceAttackWave: (char)unknownByteArray02[...], and placeSiegeTentsAndAssignEngineers: (char)someSinglePlayerScore[...] (twice). Both arrays are declared byte, but the original reads them with movsx. Without the cast the code zero-extends instead. Goes away once both are char in Ghidra (proposed).
  • addUnitToTribe: (int)unitID / 16 and % 16, a signed divide on a uint parameter (cdq; and edx, 0xf; add eax, edx; sar eax, 4). Goes away once unitID is int in Ghidra (proposed, see Reimplement 18 TribesState functions at 100% from #279, plus 5 with register allocation differences #555).
  • commandUnitsToMoveToKeep: 0 < (int)_keep. The local is a uint because it then indexes buildings[], and declaring it int changes that code. The header field (BuildingEntryInfo::id) is already an int.
  • updateTribeBehaviorBasedOnBehaviorType: (UnitStanceEnum)(attackWaveRetargetCount > 2). Without it the stored stance is computed differently.
  • (Map::Units::UnitType) on unitType arguments (ifNecessaryThrowOilAutomatically, recomputeTotalTroopValueOfTroopsNearKeep, recountTotalTroopValue) is needed to compile: the field is a short.

Header types (generated headers, left unchanged)

  • AITribeType has no names for 19–21.
    • exitSiegeEquipmentForWave treats 19–24 as siege equipment, and 22–24 are catapult, trebuchet and fire ballista.
    • So 19–21 are most likely the ram, the siege tower and the portable shield.
  • assignBehaviorTypeAndLinkSupportTribe takes undefined4 parameters for the update limit base and step. They are plain ints.
  • Tribe::tribeState is a plain short. Main's TribeStateShort (TS_UNUSED/TS_ACTIVE/TS_DISBANDED) would fit there.

Splitting AttackInfoDefinedData: measured, not pushed

DAT_ARRAY_00b4c7cc (MappersEnum[9], 0xB4C7CC) overlaps AttackInfoDefinedData's field_0xc8 range, and INT_ARRAY_00b4d0f0 (int[9], 0xB4D0F0) overlaps its field_0x9ec range.

I tried a split so that nothing overlaps:

  • AttackInfoDefinedData at 0xB4C704: the five int[10] arrays, 0xC8 bytes;
  • DAT_ARRAY_00b4c7cc;
  • AttackInfoDefinedDataPartB at 0xB4C7F0: SiegeCrewPositionOffsets and the 28 tribe-type priority lists, 0x900 bytes;
  • INT_ARRAY_00b4d0f0;
  • AttackInfoDefinedDataPartC at 0xB4D114: 36 bytes, still unknown.

Each new part got its own Globals header, Address entry and data.asm symbol.

Full build, both resolver sets on, raw / normalized:

function before after split
advanceAttackWaveStaging 100.0 / 100.0 100.0 / 100.0
initializeOrAdvanceAttackWave 100.0 / 100.0 46.8 / 46.8
updateInProgressAttackWave 100.0 / 100.0 74.4 / 73.3
placeSiegeTentsAndAssignEngineers (reads the two arrays) 100.0 / 100.0 100.0 / 100.0
UpdateBatteringRamBuilding (main) 77.0 / 73.5 77.0 / 73.5
UpdateShieldBuilding (main) 78.8 / 75.6 78.8 / 75.6
UpdateSiegeTowerBuilding (main) 77.0 / 73.5 77.0 / 73.5
UpdateTrebutchetBuilding (main) 67.9 / 66.8 67.9 / 66.8
sortAttackInfoTribeIDArrayBasedOn (draft #275) 76.0 / 71.5 85.1 / 68.0

Neither of the two drops comes from changed source. Both functions read only the first part (AIAttackWaveAdvanceDelay / AttackWaveDurationPerAttacker). Their code changes only because that struct shrinks from 0xA34 to 0xC8 bytes: the compiler swaps esi and edi from the first instruction onward. Putting back the old AttackInfoDefinedData.hpp, with the rest of the split still in place, brings both back to their old ratios (quick_diff 1.0000 and 0.9889).

So the overlap stays, and the split is not part of this PR. sortAttackInfoTribeIDArrayBasedOn would reach 85.1% raw with the split, but its normalized score drops, so it stays out as well.

This aligns the enum with sourcehold#555, which carries the same two files
byte for byte, so the two PRs do not conflict. TS_ becomes free once
sourcehold#549 renames TreeState to TreeStateEnum (TSE_).

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
This aligns the enum with sourcehold#554, which carries the same two files
byte for byte, so the two PRs do not conflict:
- Map/Enums is main's folder for these headers; Map/Units/Enums does
  not exist on main.
- TS_FREE becomes TS_UNUSED and TS_EMPTY becomes TS_DISBANDED.
- TS_ is free once sourcehold#549 renames TreeState to TreeStateEnum (TSE_).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt and others added 4 commits December 4, 2026 18:48
…-matched

Conflicts:
- cmake/compiler-flags-gl.txt: kept both the PR's entries and main's.
- src/precomp/addresses-SHC-3BB0A8C1.hpp: took main's version, because
  main's headers lead.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
- Main's AttackInfoDefinedData now declares `MappersEnum field_0xc8[9]`,
  which is the array at 0xB4C7CC. DAT_ARRAY_00b4c7cc.hpp is removed, and
  placeSiegeTentsAndAssignEngineers reads
  DAT_AttackInfoDefinedData::instance.field_0xc8[k].
- The 5999 comparisons use main's new SSS_UNUSABLE_THRESHOLD.
- initializeOrAdvanceAttackWave includes Map/Enums/TribeState.hpp. With
  main's smaller struct declaration the function dropped from 100% to
  46.8%: the compiler gives the &attackWaveTicker[attackWave] address
  its own register instead of attackWave. Declaring a few more names in
  the translation unit restores the original allocation, and so does
  the old header. Of the includes tried, this one restores 100%.

Every function keeps its score in a full reccmp build.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
This completes 931d540, which only removed the header.
- placeSiegeTentsAndAssignEngineers reads
  DAT_AttackInfoDefinedData::instance.field_0xc8[k] instead of
  DAT_ARRAY_00b4c7cc.
- The 5999 comparisons use main's new SSS_UNUSABLE_THRESHOLD, in
  calculateTile2MoatValueClosestToUnit, findEnemyTowersOrGates,
  findNearestAvailableScalePoint, getClosestWideValueBasedOnPlayer and
  setGate2/setMoat3/setScale3/setWide3.
- initializeOrAdvanceAttackWave includes Map/Enums/TribeState.hpp.
  With main's smaller AttackInfoDefinedData declaration, the function
  dropped from 100% to 46.8%. The compiler gave the
  &attackWaveTicker[attackWave] address its own register instead of
  attackWave. Declaring a few more names in the translation unit
  restores the original allocation, and so does the old header. Of
  the includes tried, this one restores 100%.

Every function keeps its score in a full reccmp build.

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
These files are byte-identical to the ones in sourcehold#554 and sourcehold#555:
TS_UNUSED = 0, TS_ACTIVE = 2, TS_DISBANDED = 3. The TS_ prefix relies
on sourcehold#549 renaming TreeState's values to TSE_.

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
- Main's AttackInfoDefinedData declares `MappersEnum field_0xc8[9]`,
  which is the array at 0xB4C7CC. The previous commit removed
  DAT_ARRAY_00b4c7cc.hpp together with the TribeState headers, and
  placeSiegeTentsAndAssignEngineers now reads
  DAT_AttackInfoDefinedData::instance.field_0xc8[k].
- The 5999 limit checks use main's new SSS_UNUSABLE_THRESHOLD.
- initializeOrAdvanceAttackWave dropped from 100% to 46.8% with main's
  smaller struct declaration. It now takes sourcehold#554's version, which
  includes Map/Enums/TribeState.hpp and is back at 100%.
- sortAttackInfoTribeIDArrayBasedOn: 76.0% -> 85.1%. The struct no
  longer overlaps the array labels, so the status line is updated.
  Normalized it is 68.0%, down from 71.5%.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit that referenced this pull request Dec 4, 2026
Main's 87e40b6 adds Map/Enums/TribeState.hpp and TribeStateShort.hpp,
byte-identical to this branch's copies.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot removed the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 4, 2026
gynt and others added 4 commits December 4, 2026 22:42
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…ound flag

Each support-tribe search sets found and breaks; the second search runs only
when the first found nothing, and the link block runs when found is set.
Same code as the goto form.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Every removed cast was checked one at a time: the file's /FA listing is
identical without it and no new warning appears.

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

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant