Skip to content

Reimplement 67 Map::TileMapState functions (batch 2) - #396

Open
gynt wants to merge 584 commits into
mainfrom
reimpl/TileMapState-100-2
Open

gynt wants to merge 584 commits into
mainfrom
reimpl/TileMapState-100-2

Conversation

@gynt

@gynt gynt commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Second batch of Map::TileMapState: 67 functions, mostly the building placers (place*), the placement checks, the footprint clearers, wall and moat handling, heights and the misc display layer.

At review, the 16 functions that need a volatile read or the __forceinline change to FunctionResolver.h moved to #519, which has since been merged into this branch. Two functions that only match through tricks the review objects to moved to #560: getHeightAtTileIncludingOwnersBuildings (a local noinline forwarder) and updateGfxLayer (a volatile read in a local #define).

Match

The status lines give reccmp's raw match. Measured on 5ff7919fd (main 33cdd1f3e merged in), with the struct and function resolvers on and with every TileMapState source, calculateHoveredTile.cpp and xyAreValid.cpp in the build list: 186 functions, 167 at 100% in the default build.

This PR's 78 status lines: 60 at 100%. Five brush functions reach 100% only with OPENSHC_FUNCTION_VISIBILITY_UNITS on. Following main's convention, their line gives the default-build % and the remark gives the figure with the option on. The others below record their blocker in status/addresses-SHC-3BB0A8C1.txt:

Function Raw Normalized
processEntityDamageToBuildingCollateral 99.9 95.8
placeStockpile 99.3 95.2
demolishBuildingsInKeepsConstructionFootprint 98.6 90.9
placeTree 98.1 92.0
placeOilsmelter 98.0 92.1
checkBuildingCanBePlacedHere 97.3 90.3
placeBuilding 97.3 91.1
evaluateBuildingPlacementAtCursor 94.2 87.5
placeRock 93.5 88.1
processDamageToBuilding 89.5 82.5
setConstructionGFXLayerBasedOnPlacementChecks 78.0 77.5
determineBuildingPlacementRotation 68.7 68.7
useTerrainHeightBrush 62.7 (76.4 with the option on) 50.0
useEraserBrush, createPlateau, useLevelBrush, setLand, isValidCastleSiteLocation 95.0, 83.9, 53.0, 29.9, 61.6 (100 with the option on)
  • xyAreValid.cpp (main) has to be in the build list: checkBuildingCanBePlacedHere is a /GL caller, and without our xyAreValid it reloads buildingY + y after every call (61.7%).
  • determineBuildingPlacementRotation keeps its volatile and stays here, because main's updateBuildingPlacementRotationPreview calls it under /GL and drops from 100% to 46.2% without it.

Review round

All review comments are answered in their threads. The changes keep every function in the build list at its previous score or better, and none of them alters the generated code except where a function improved:

  • MapConstants, WO_OWNER_MASK, MO_*/CD_*, MD_*, L_*/L2_* and the other existing enums replace the numbers they stand for.
  • Parameter types and names are settled from the original's reads and the callers' pushes, so most casts on parameters are gone.
  • Code comments are removed.
  • Merging main moved placeDefensiveStructureTile (100 → 90.2), which the parameter changes brought back to 100.

Header changes (to mirror in Ghidra)

New enums:

  • Map/Buildings: BuildingPlacementFailState (BPFS_*), BuildingPlacementWarning (BPW_*), BuildingTilePlacementResult (BTPR_*)
  • Map/Enums: MoatStage (MS_PLANNED, MS_DUG), OrganismLayerConstants (OLC_ROCK_ID_OFFSET), NeighbourMask (NM_*), EditorAffectType (EAT_*), TerrainHeightBrushMode (THBM_*)
  • Map/Rocks/Enums/RockType (RT_NONE, RT_1X1_A … RT_4X4_D)
  • Map/LogicHelpers: RiverFall (RF_*), MacroLayerTile (MLT_*)
  • added MiscDisplay::MD_PILLAR_Y_OFFSETUnk (0x800) and MapConstants::MC_MAP_SECTOR_SIZE (10)
  • added BuildingLogicalState::BLS_NONE (0, an unused building slot)

New struct:

  • Game/Player/AttackedByPlayerStatistics (0x20: attacks, buildingsDestroyed, unitsLost, troopEncounters, four unknown ints); PlayerData::attackedByPlayerArrayUnk[10][8] → attackedByPlayer[10]

Fields:

  • TileMapState: buildingPlacementFail, placementWarning, placementOnWall/placementOnMoat (BOOLEnum), editorAffectType, ChangedLayer (char), gfxTileHeight (short), DAT_CardinalTilesAroundTile (int)
  • Moat::stage (MoatStageByte), Rock::type (RockTypeShort), Building 0x8C → int secondTileUnk
  • TerrainDefinedData: footprintSideOffsets[4][4], MacroLayerScanOffsets2x2/3x3/4x4, MacroLayerPartFrameOffsets[3][4][16], TerrainEdgeGraphicLookup[8], orientationRelativeBuildingTileOffset1/2 as [16][4]/[15][4], field_0x2764[8][60], field_0x264[3][12] → keepDoorOffsets[3][4][3]

Signatures in TileMapState.hpp/.func.hpp: the place* family (BuildingTypeShort buildingType, uint buildingSize, int variation/orientation, int averageHeight), the placement checks (MappersEnumShort commands, typed results), and the brush, rock, tree and damage functions (int y, named parameters; int x in eraseAreaWithBrush/useEraserBrush, int x, int y in placePitchDitch).

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

gynt and others added 30 commits November 7, 2026 11:59
Adds the callers whose original keeps this in ecx across an already
finished callee, plus those callees: clearBuildingFromTerrain and
computeWallCornerRenderRotation reach 100%, placeKeep,
setupAllMapSections and validateWallBuildPath gain a little. Tried and
left out (worse with /GL): placeEngineersguild, placeTunnelersguild,
upgradeTowerLogicLayer; neutral: the place* callers of
updatePathLinkagesForBuilding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmaNCgNNvTDAKE2qbZC
…al instance

New NeighbourFlagsGlobalAsm.hpp holds the same handwritten macros as
NeighbourFlagsAsm.hpp, but addressing [DAT_TileMapState::instance] like
the original (mov edi, [0x1fe7c70] ... mov [0x1fe7c48], dl) instead of
reloading this before each field. NeighbourFlagsAsm.hpp is unchanged.
The two brush functions' inline copies of the mark-changed macro now use
the shared one.

updateGFXLayers 87.5 -> 92.9, updateMacroLayerRelated 85.3 -> 89.2,
setBitFlagBasedOnWallTowerGatehouseOrKeep 85.2 -> 90.7, createPlateau
35.6 -> 43.7, setTerrain 92.5 -> 92.8. updateGfxLayer 90.5 -> 89.8: its
19 expansions now match exactly, the loss is reccmp realigning jump
displacements after the code got 57 instructions shorter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmaNCgNNvTDAKE2qbZC
@gynt

gynt commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

About the match regression check: it flags 1 file, processEntityDamageToBuildingCollateral.cpp (SHC_3BB0A8C1_0x00517790), and the flag is a false positive. The 100.0% it quotes for Wilps93-OpenSHC/reimpl/processEntityDamageToBuildingCollateral is that fork's status line, not a measured match on this branch. Built here, that version scores lower than ours:

version match
Ours (this PR) 99.9%
Theirs (Wilps93 fork, verbatim, with the header's y as uint as on its base) 99.7%
Theirs, adapted to this branch's int y 99.7%

Measured on 1d44f4a8a with reccmp, both resolver sets enabled and the TileMapState namespace in the build list. The fork's version uses the same two volatile reads (brush tile and HeightLayer), so it would not remove that caveat either.

…00-2

# Conflicts:
#	src/precomp/FunctionResolver.h
Comment on lines +24 to +28
static __forceinline BOOLEnum isOnMap(uint x, uint y)
{
return x <= MC_MAP_XY_LIMIT_INCLUSIVE && y <= MC_MAP_XY_LIMIT_INCLUSIVE
&& DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And no scoping trick or in-place computation of values allowed to not use this forced inline function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Check if inline is really necessary. It is the xyAreValid function from ViewportRenderState. Perhaps we can force some inlining on that function later on. Or maybe function-visbility.txt is at play here and this functin can see xyAreValid! Or perhaps a code fold happened on identical functions: one in TileMapState and one in ViewportRenderState!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The original (0x4FA2D0) has no call here: it inlines the bounds and bitmap test with the instance as a constant (cmp ebx,0x18f; ja; cmp eax,0x18f; ja; imul ecx,0x190; cmp byte ptr [ecx+ebx+0x21aec98],0), so this is neither identical-function folding nor visibility. A plain xyAreValid call measures 37.4% (it emits the call). Two int const offset locals plus a written-out range check give 100.0/100.0 with no helper and no __forceinline (b8455b3).

Comment thread status/addresses-SHC-3BB0A8C1.txt Outdated
@@ -39653,7 +39653,7 @@ SHC_3BB0A8C1_0x004F9DF0 | 100.0% | Reimplemented

SHC_3BB0A8C1_0x004F9F00 | 100.0% | Reimplemented

SHC_3BB0A8C1_0x004FA000 | 68.7% | final else 8; y reload
SHC_3BB0A8C1_0x004FA000 | 68.7% | volatile wall/count; slot layout

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some of the comments mention a higher matching degree, so I am a bit confused? Or is this due the removeal of some volatile?

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.

TODO: check which match is reported here. Should be the raw match, not normalized or anything.

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 checked this. The status lines now give reccmp's raw match (the first column of reccmp_report.py --run pct). I measured on main 592a17d merged into this branch (632d090): both resolver sets on, every TileMapState source plus calculateHoveredTile and xyAreValid in the build, default cmake options. For the brush functions that only reach 100% through cmake/function-visibility.txt, I also built with OPENSHC_FUNCTION_VISIBILITY_UNITS on. They follow main's convention: the % is the default-build figure and the remark gives the figure with the option on.

0x004FA000: 68.7% is correct. It is the raw reccmp match, and the normalized score is also 68.7%, so no volatile was removed. The higher figures came from my earlier replies on this file (0.7989 with the volatiles, 0.7100 without). Those are quick_diff.py ratios. quick_diff is a triage tool: it compiles without /GL, uses a different normalization and gives a difflib ratio, which is not the reccmp score. It still gives 0.7989 for this file today. It was wrong to quote those ratios next to a status figure. No code comment in this PR gives a match percentage, so the .cpp files did not need changes.

The remark was also too terse. It now says what differs. The other remarks now use main's wording, and the 100% lines that described a technique (forceinline get(), volatile) now just say Reimplemented.

Address Function Old New
0x004FA000 determineBuildingPlacementRotation 68.7% volatile wall/count; slot layout 68.7% Asm differs: register and stack slot assignment; wall and wallTileCount are volatile
0x004FFF10 getHeightAtTileIncludingOwnersBuildings 100.0% Reimplemented 99.3% Only the call target differs: getTotalHeightAtTile is called through a local noinline forwarder
0x005017C0 useEraserBrush 100.0% Reimplemented 95.0% 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on
0x00501F50 createPlateau 100.0% Reimplemented 83.9% 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on
0x00502110 useLevelBrush 100.0% Reimplemented 53.0% 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on
0x00502390 setLand 100.0% Reimplemented 29.9% 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on
0x005024F0 isValidCastleSiteLocation 100.0% Reimplemented 61.6% 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on
0x00502680 useTerrainHeightBrush 76.4% FgCodeMotion arm placement 62.7% 76.4 with OPENSHC_FUNCTION_VISIBILITY_UNITS on, asm differs
0x004FA760 demolishBuildingsInKeepsConstructionFootprint 98.6% offset array partition 98.6% Instruction selection differs: playerID - 40 is a lea in the original, a sub in ours
0x004FB4D0 placeRock 93.5% volatile kind reads; asm differs 93.5% Asm differs: an extra movzx and one register choice; kind is read through volatile
0x005037B0 checkBuildingCanBePlacedHere 97.3% slot weights; keep/gate slots 97.3% Register allocation differs, two stack slots swapped
0x00504A30 evaluateBuildingPlacementAtCursor 94.2% register allocation 94.2% Asm differs: ebp is saved at a different point and the bounds checks are scheduled differently
0x00504F10 setConstructionGFXLayerBasedOnPlacementChecks 78.0% register allocation 78.0% Asm differs: 0x14-byte stack frame instead of 0x18, stack slots assigned differently
0x00508030 placeOilsmelter 98.0% imul register naming 98.0% Register allocation differs
0x00508540 placeStockpile 99.3% jl loop end needs unroll 99.3% Asm differs: one loop ends on jne instead of jl
0x00515DA0 placeTree 98.1% register allocation 98.1% Instruction scheduling differs: one stack store comes before a test instead of after
0x00516B80 processDamageToBuilding 89.5% volatile destroyed; tail merge differs 89.5% Asm differs: tail merge and stack slots; destroyed is volatile
0x00517790 processEntityDamageToBuildingCollateral 99.9% volatile tile/height; one store order 99.9% Instruction scheduling differs: one byte store is placed later
0x004FC9E0, 0x004FDB00, 0x004FF870, 0x00501D90, 0x00508A00, 0x00509180, 0x00512940 updateGFXLayers, updateMacroLayerRelated, setBitFlagBasedOnWallTowerGatehouseOrKeep, raiseLand, renderPreviewMapperWithBrush, updateGfxLayer, setTerrain 100.0% Reimplemented; forceinline/volatile notes 100.0% Reimplemented

Changed percentages are in bold. The other 55 lines were already correct at 100.0% Reimplemented.

Separately, I reverted this branch's edits to clearStockpileFootprintTiles, placeDefensiveStructureTile and calculateHoveredTile to main's versions (7b244a3). Main already has those three at 100%, and the edits did not change the score.

Comment on lines +43 to +45
if ((uint)(DAT_TerrainDefinedData::instance.footprintSideOffsets[rotation][0].x + x)
<= MC_MAP_XY_LIMIT_INCLUSIVE
&& (uint)tileY <= MC_MAP_XY_LIMIT_INCLUSIVE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this be a range check with the compiler trick to merge this to one test, or is the type of footprintSideOffsets not fitting?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Check if the (uint) hides a x >= 0 && x < MC_MAP_XY_LIMIT_INCLUSIVE comparison that fits equally well, in which case it is preferred to write it out instead of a uint cast based comparison.

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.

tileY is now tileY >= 0 && tileY <= MC_MAP_XY_LIMIT_INCLUSIVE, with identical code (49edfe4). For x (footprintSideOffsets[...].x + x) every written-out form changes the code (quick_diff 0.4865 with a tileX local, 0.3979 with the expression repeated, against 0.5761), so x keeps its (uint).

{
if (x > 399 || y > 399) {
if (x > MC_MAP_XY_LIMIT_INCLUSIVE || y < 0 || y > MC_MAP_XY_LIMIT_INCLUSIVE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would assume if y is int, the x also?

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 will check!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. The original checks x with the same single cmp ebx,0x18f; ja as y, which is what x < 0 || x > 399 on an int compiles to, so x is now int with the written-out check (header change, baeb7a4). Identical code here and in ClickErase.

this->skipPlacementCheck = 0;
int pitchDitchID = 0;

BuildingType buildingType = (BuildingType)(ushort)MACRO_CALL_MEMBER(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So othe thought: Is the return of the function and BuildingType in general maybe convertCommandBuildingTypeToBuildingType unsigned short actually?

int owner = DAT_BuildingsState::instance.buildings[buildingID].owner;
DAT_GameSynchronyState::instance.finalResults.finalBuildingsBurned[playerID] += 1;
DAT_GameSynchronyState::instance.finalResults.finalBuidingsDestroyed[owner] += 1;
DAT_GameState::instance.playerDataArray[owner].attackedByPlayerArrayUnk[playerID][1] += 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would prefer a struct with unknown fields to just leaving it.

@@ -23,23 +23,22 @@ namespace OpenSHC {
namespace Map {

// FUNCTION: STRONGHOLDCRUSADER 0x005017C0
void TileMapState::useEraserBrush(uint x, uint y, int brush)
void TileMapState::useEraserBrush(uint x, int y, int brush)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How likely is it that x is not also an int?

@gynt gynt Dec 4, 2026 •

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 will check!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is now int (baeb7a4). x only appears in add ecx, ebp, so no signedness is visible, and the code is identical here and in resetAreaBasedOnLogicalLayer.

this->illegalBuild = TRUE;
this->field_0x554920 = 0;
if (x1 > 399 || y1 > 399) {
this->buildingPlacementFail = TRUE;
if ((uint)x1 > MC_MAP_XY_LIMIT_INCLUSIVE || (uint)y1 > MC_MAP_XY_LIMIT_INCLUSIVE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe it was a range check?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Check if the (uint) hides a x >= 0 && x < MC_MAP_XY_LIMIT_INCLUSIVE comparison that fits equally well, in which case it is preferred to write it out instead of a uint cast based comparison.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes: x1 < 0 || x1 > MC_MAP_XY_LIMIT_INCLUSIVE etc. folds into the same single unsigned cmp; ja, with identical code and still 100.0/100.0 (531d443).

if (commandShort == OpenSHC::Commands::M_MAPPER_KEEP1 || commandShort == OpenSHC::Commands::M_MAPPER_KEEP2
|| commandShort == OpenSHC::Commands::M_MAPPER_KEEP3) {
int const keepIndex = command - OpenSHC::Commands::M_MAPPER_KEEP1;
for (int part = 0; part < 3; part++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please leave a comment for the clean up runs.

this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 668
+ this->LuminesenceLayer[this->DAT_SomeTile] * 8
+ (this->RandomLayer[this->DAT_SomeTile] & 7);
for (local_14_f = 0; local_14_f < 7; local_14_f++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would prefer a relation to a size with comment, actually.

gynt and others added 3 commits December 4, 2026 17:34
…00-2

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

main already has these three at 100%, and this branch's edits to them score the same, so keep main's files and status lines.

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

Measured on main 592a17d merged in, with both resolver sets on and every TileMapState source plus calculateHoveredTile and xyAreValid in the build. The percentage is reccmp's raw match from the default build; the five brush functions that only reach 100% through cmake/function-visibility.txt use main's '100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on' remark. getHeightAtTileIncludingOwnersBuildings was listed at 100% but is 99.3% raw (only its normalized score is 100%). Remarks now describe the actual asm difference in main's wording.

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 2 commits December 4, 2026 18:22
…00-2

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

Both only match through tricks the review objects to: a local noinline forwarder, and a local #define that reads GMFirstPictureIndex through a volatile cast. They move to their own PR. Their status lines go back to main's Pending. No /GL or visibility entry belonged to them, and the other 186 functions in the build measure the same without them.

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 23:16
…00-2

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

The original has no call here: it inlines the bounds and DAT_BinaryTileMap400x400 test with the ViewportRenderState instance as a constant. Two int locals for the offset tile (y first, as the inlined arguments were evaluated) and a written-out range check give the same code.

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

x < 0 || x > MC_MAP_XY_LIMIT_INCLUSIVE folds into the same single unsigned compare.

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

tileY >= 0 && tileY <= MC_MAP_XY_LIMIT_INCLUSIVE folds into the same unsigned compare. The x checks keep their cast: every written-out form changes the code.

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 and others added 8 commits December 5, 2026 03:45
…rush and placePitchDitch

The original bounds-checks these coordinates with a single `cmp reg, 0x18f; ja`, which is also
what `x < 0 || x > 399` on an int compiles to; y in eraseAreaWithBrush was already an int with
that check. useEraserBrush only adds x to a tile index. The generated code of these three
functions and of their callers (ClickErase, UpgradePitchDitchBuildingIntoPitchDitchObject,
placeBuilding, resetAreaBasedOnLogicalLayer) is identical.

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

Each player keeps one row of eight ints per attacking player at 0x2BDC. Only the first four are
ever addressed in the original, each by one writer:
- +0x0 attacks: updateAIPlayerState, when an AI launches an attack (0x4D4AA7)
- +0x4 buildingsDestroyed: processDamageToBuilding and processEntityDamageToBuildingCollateral
  (0x517057, 0x517ABB)
- +0x8 unitsLost: processUnitLossStatistic (0x45653A)
- +0xC troopEncounters: setLastEncounteredTroopUnit (0x456657)
0x10-0x1C have no references and stay field_0x.. . All five users compile to identical code.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The table holds the offsets of the three keep doors (BT_KEEPDOOR_LEFT, BT_KEEPDOOR,
BT_KEEPDOOR_RIGHT) per keep type (manor house, stone keep, ...) and per rotation. placeKeep
indexed it flat as [0][(keep * 4 + rotation) * 3 + door]; it now reads
[keep][rotation][door], and keepOutbuildingOffsets_0x384/0x3e4 ([3][4]) get the same
[keep][rotation] index instead of [0][keep * 4 + rotation]. The part loops of
checkBuildingCanBePlacedHere and setConstructionGFXLayerBasedOnPlacementChecks count with
countof instead of a literal 3. All four users compile to identical code.

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

UnitLayer is a short array, so the local needs no casts at its uses. Identical code.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
processDamageToBuilding and processEntityDamageToBuildingCollateral compared logicalState with
(BuildingLogicalState)0; they now use the named value (identical code). Main's users that
compare with a plain 0 are left as they are.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
versionNumOfCurrentFileTypeUnk is already a PackagedFileMagicNumInt; identical code and warnings.

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

Same as placeWalls: placeBarracks, placeEngineersguild, placeTunnelersguild and placeOilsmelter
read the new building id into a ushort and sign-extended it with (short) at each use, and
placeTree did the same for the logical tree type. A short local needs no casts; identical code.

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 added the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 5, 2026
@gynt

gynt commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Casts kept for the match

Every cast added by this PR was removed on its own and the file recompiled. Each removal was kept only when the listing and the warnings stayed identical. Three casts went this way: prepareMap.cpp (PackagedFileMagicNum) (dc3588f), and the two (BuildingLogicalState)0, which are now the new BLS_NONE (536e6c6). The ushort locals that were cast back to (short) are now short locals (de24136, 5ff7919). Most of the remaining casts are needed to compile (an enum from an int). These are the ones that change the code:

Where Cast Why it stays
determineBuildingPlacementRotation.cpp 43/63/83/103 (uint)(footprintSideOffsets[r][k].x + x) <= MC_MAP_XY_LIMIT_INCLUSIVE tileY is written out as a range check with identical code, but every written-out form for x changes the code: quick_diff 0.4865 with a tileX local, 0.3979 with the expression repeated, against 0.5761
placeBuilding.cpp 75 (BuildingType)(ushort)convertCommandBuildingTypeToBuildingType(...) the original zero-extends the 32-bit return here only (movzx eax, ax at 0x516351); the converter's other 14 callers use all of eax, so BuildingType stays int-sized. No cast gives 0.8976, (short) 0.8969 and a ushort local 0.8073, against 0.8984
demolishBuildingsInKeepsConstructionFootprint.cpp 35 (BuildingType)((short)buildingType - BT_MANORHOUSE) the 16-bit read of the type before the subtraction; without (short) the code differs
placeKeep.cpp 78, placeKillingPit.cpp 47 (uchar) into BuildingWasLayer a byte layer; same code without it, but it raises C4244

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

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.

2 participants