From a9e7a273f65404e9d99601c519f17df4a1384e8a Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Thu, 3 Dec 2026 17:22:09 +0100 Subject: [PATCH] reimplement: SHC_3BB0A8C1_0x0053D3D0 100.0% Reimplemented UnitsState::setDestinationForUnit from 71.1% / 68.9% (option off; 71.3% / 67.3% on) to 100%. The text is gynt's, on main since #238 (8fa4bea964). His commits on it, newest first: f3c6879a07 (71.3%), eb81a28387, 86bd3a0c28, 154d3022a0, f601aa4dd0, 305507f883. The status line still had 64.2% from 154d3022a0; his later commits did not update it. Five edits: - The map test is a flag (`_onMap`), tested once. The original falls through into its exit after the third test and goes on with `jne 0x0053D439`; with one `||` condition MSVC jumps to the exit with `je` and puts it out of line. - In the two climb branches (under `climbIsIllegal != 0`, and under `allAssassinsUnk == 0 && reusePathingInfo == 0`) the register call and its -1 exit stand inside `if (climbDataID != 0)`, with the `climbDataID == 0` exit as its `else`. The original has one despawn block, at the first branch's -1 exit (0x0053D635), and the other exits jump into it; with the `climbDataID == 0` exits first in the text MSVC puts that block at the end of the function. - The last test is inverted (`<= 0` becomes `> 0`): the success stores come first, inside `if (_pathPlanSize > 0)`, and the despawn exit after them. With the exit first in the text its unitID load took edx and stayed a stub of its own (`push edx; jmp`); the original's exit jumps into the first branch's block (`jle 0x0053D635`). In the success block the ladder exit's x and y are stored after the destination tile, as in the first success block. - All seven despawn exits clear climbIsIllegal before allAssassinsUnk. Main's text had the other order in all seven; the original's shared block stores climbIsIllegal first (0x0053D63E). quick_diff can't see the order of two global stores, so only the real build showed it. - The file is off the /GL list: the original loads ecx again before every call, and with /GL MSVC drops the load before fillMemory_ByteValue (0x0053D729) because it sees that copyData keeps ecx. No behaviour change against main's text: the map test is the same condition by De Morgan, the inverted tests keep each branch's code, and the swapped stores write different fields before the same call or return. quick_diff: 0.7747 on main's text, 1.0000 with the edits (270 instructions as the original). Fewer edits stay below: the inverted test alone 0.8191, with the flag 0.8885, with the if / else but no flag 0.9234. After the builds: gynt's three block comments are // lines, and one // line each says why the flag, the two else exits and the inverted last test are needed. The file compiled alone gives the same object as the built text's (every section but .debug$*: same bytes and relocations). Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- cmake/compiler-flags-gl.txt | 1 - .../UnitsState/setDestinationForUnit.cpp | 116 ++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 3 files changed, 63 insertions(+), 56 deletions(-) diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 5430b694de..cc2e343848 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -306,7 +306,6 @@ src/OpenSHC/Map/Units/UnitsState/processUnitMove.cpp src/OpenSHC/Map/Units/UnitsState/recomputeTroopValuesForPlayer.cpp src/OpenSHC/Map/Units/UnitsState/removeUnitsSameTileLinkageIfNoLongerApplicable.cpp src/OpenSHC/Map/Units/UnitsState/selectWorkerSpeechOrMoraleStateID.cpp -src/OpenSHC/Map/Units/UnitsState/setDestinationForUnit.cpp src/OpenSHC/Map/Units/UnitsState/shootProjectile.cpp src/OpenSHC/Map/Units/UnitsState/shouldUnitsEngageInMelee.cpp src/OpenSHC/Map/Units/UnitsState/spawnUnit.cpp diff --git a/src/OpenSHC/Map/Units/UnitsState/setDestinationForUnit.cpp b/src/OpenSHC/Map/Units/UnitsState/setDestinationForUnit.cpp index f1f3800ab4..cd63567c93 100644 --- a/src/OpenSHC/Map/Units/UnitsState/setDestinationForUnit.cpp +++ b/src/OpenSHC/Map/Units/UnitsState/setDestinationForUnit.cpp @@ -21,11 +21,13 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x0053D3D0 BOOLEnum UnitsState::setDestinationForUnit(int unitID, uint x, uint y, int reusePathingInfo) { - /* check by unit type */ + // check by unit type int _ableToClimbTowers = DAT_UnitPropertiesDefinedData::instance.ABLE_TO_CLIMB_TOWERS[this->units[unitID].unitType]; - if (x > MC_MAP_XY_LIMIT_INCLUSIVE || y > MC_MAP_XY_LIMIT_INCLUSIVE - || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] == 0) { + // a flag, not the whole test in the if (inline, the exit moves out of line and loses the match) + bool const _onMap = x <= MC_MAP_XY_LIMIT_INCLUSIVE && y <= MC_MAP_XY_LIMIT_INCLUSIVE + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] != 0; + if (!_onMap) { DAT_PathFindingState::instance.allAssassinsUnk = 0; return FALSE; } @@ -86,8 +88,8 @@ namespace Map { DAT_PathFindingState::ptr)( this->units[unitID].owner, _fromArea, _toArea, this->units[unitID].unitCanClimb); if (this->units[unitID].field_0x2a8 == 0) { - DAT_PathFindingState::instance.allAssassinsUnk = 0; DAT_PathFindingState::instance.climbIsIllegal = 0; + DAT_PathFindingState::instance.allAssassinsUnk = 0; this->units[unitID].movementCooldown = 0; MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)( unitID); @@ -96,24 +98,26 @@ namespace Map { this->units[unitID].climbDataID = MACRO_CALL_MEMBER( OpenSHC::Map::Navigation::PathFindingState_Func::setClimbBasedOnClosestClimbData, DAT_PathFindingState::ptr)(unitID, _fromArea, this->units[unitID].field_0x2a8); - if (this->units[unitID].climbDataID == 0) { - DAT_PathFindingState::instance.allAssassinsUnk = 0; + // the no-climb exit as the else (written first, the shared despawn exit moves to the end) + if (this->units[unitID].climbDataID != 0) { + int const _registerResult = MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::registerUnitOnClimbData, + DAT_PathFindingState::ptr)(this->units[unitID].climbDataID, unitID); + if (_registerResult == 1) { + // walk to the climb first + x = DAT_PathFindingState::instance.climbX; + y = DAT_PathFindingState::instance.climbY; + } else if (_registerResult == -1) { + DAT_PathFindingState::instance.climbIsIllegal = 0; + DAT_PathFindingState::instance.allAssassinsUnk = 0; + this->units[unitID].movementCooldown = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)( + unitID); + return FALSE; + } + } else { DAT_PathFindingState::instance.climbIsIllegal = 0; - this->units[unitID].movementCooldown = 0; - MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)( - unitID); - return FALSE; - } - int const _registerResult = MACRO_CALL_MEMBER( - OpenSHC::Map::Navigation::PathFindingState_Func::registerUnitOnClimbData, - DAT_PathFindingState::ptr)(this->units[unitID].climbDataID, unitID); - if (_registerResult == 1) { - /* walk to the climb first */ - x = DAT_PathFindingState::instance.climbX; - y = DAT_PathFindingState::instance.climbY; - } else if (_registerResult == -1) { DAT_PathFindingState::instance.allAssassinsUnk = 0; - DAT_PathFindingState::instance.climbIsIllegal = 0; this->units[unitID].movementCooldown = 0; MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)( unitID); @@ -126,8 +130,8 @@ namespace Map { DAT_PathFindingState::ptr)( this->units[unitID].owner, _fromArea, _toArea, this->units[unitID].unitCanClimb); if (this->units[unitID].field_0x2a8 == 0) { - DAT_PathFindingState::instance.allAssassinsUnk = 0; DAT_PathFindingState::instance.climbIsIllegal = 0; + DAT_PathFindingState::instance.allAssassinsUnk = 0; this->units[unitID].movementCooldown = 0; MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)(unitID); return FALSE; @@ -135,23 +139,26 @@ namespace Map { this->units[unitID].climbDataID = MACRO_CALL_MEMBER( OpenSHC::Map::Navigation::PathFindingState_Func::setClimbBasedOnClosestClimbData, DAT_PathFindingState::ptr)(unitID, _fromArea, this->units[unitID].field_0x2a8); - if (this->units[unitID].climbDataID == 0) { - DAT_PathFindingState::instance.allAssassinsUnk = 0; - DAT_PathFindingState::instance.climbIsIllegal = 0; - this->units[unitID].movementCooldown = 0; - MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)(unitID); - return FALSE; - } - int const _registerResult - = MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::registerUnitOnClimbData, + // the no-climb exit as the else (written first, the shared despawn exit moves to the end) + if (this->units[unitID].climbDataID != 0) { + int const _registerResult = MACRO_CALL_MEMBER( + OpenSHC::Map::Navigation::PathFindingState_Func::registerUnitOnClimbData, DAT_PathFindingState::ptr)(this->units[unitID].climbDataID, unitID); - if (_registerResult == 1) { - /* walk to the climb first */ - x = DAT_PathFindingState::instance.climbX; - y = DAT_PathFindingState::instance.climbY; - } else if (_registerResult == -1) { - DAT_PathFindingState::instance.allAssassinsUnk = 0; + if (_registerResult == 1) { + // walk to the climb first + x = DAT_PathFindingState::instance.climbX; + y = DAT_PathFindingState::instance.climbY; + } else if (_registerResult == -1) { + DAT_PathFindingState::instance.climbIsIllegal = 0; + DAT_PathFindingState::instance.allAssassinsUnk = 0; + this->units[unitID].movementCooldown = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)( + unitID); + return FALSE; + } + } else { DAT_PathFindingState::instance.climbIsIllegal = 0; + DAT_PathFindingState::instance.allAssassinsUnk = 0; this->units[unitID].movementCooldown = 0; MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)(unitID); return FALSE; @@ -184,28 +191,29 @@ namespace Map { DAT_PathFindingState::ptr)(this->units[unitID].owner, _ableToClimbTowers); } DAT_PathFindingState::instance.notAllAssassinsUnk = 0; - if (_pathPlanSize <= 0) { - DAT_PathFindingState::instance.allAssassinsUnk = 0; + // the success path first (with the early exit first, its despawn exit is a copy, not the shared one) + if (_pathPlanSize > 0) { + this->units[unitID].totalSizeOfPathPlan = _pathPlanSize; + this->units[unitID].tunnelerFinishedDigging = 2; + this->units[unitID].currentIndexInPathPlan = 0; + this->units[unitID].destinationXPosition = x; + this->units[unitID].destinationYPosition = y; + this->units[unitID].destinationTilePosition + = (short)x + DAT_ViewportRenderState::instance.translationMatrix[(short)y].addXgetTile; + this->units[unitID].ladderExitXPosition = this->units[unitID].x; + this->units[unitID].ladderExitYPosition = this->units[unitID].y; + this->units[unitID].previousTilePosition = this->units[unitID].x + + DAT_ViewportRenderState::instance.translationMatrix[this->units[unitID].y].addXgetTile; + this->units[unitID].cannotClimb = DAT_PathFindingState::instance.climbIsIllegal != 0; DAT_PathFindingState::instance.climbIsIllegal = 0; - this->units[unitID].movementCooldown = 0; - MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)(unitID); - return FALSE; + DAT_PathFindingState::instance.allAssassinsUnk = 0; + return TRUE; } - this->units[unitID].totalSizeOfPathPlan = _pathPlanSize; - this->units[unitID].tunnelerFinishedDigging = 2; - this->units[unitID].currentIndexInPathPlan = 0; - this->units[unitID].destinationXPosition = x; - this->units[unitID].destinationYPosition = y; - this->units[unitID].ladderExitYPosition = this->units[unitID].y; - this->units[unitID].destinationTilePosition - = (short)x + DAT_ViewportRenderState::instance.translationMatrix[(short)y].addXgetTile; - this->units[unitID].ladderExitXPosition = this->units[unitID].x; - this->units[unitID].previousTilePosition = this->units[unitID].x - + DAT_ViewportRenderState::instance.translationMatrix[this->units[unitID].y].addXgetTile; - this->units[unitID].cannotClimb = DAT_PathFindingState::instance.climbIsIllegal != 0; DAT_PathFindingState::instance.climbIsIllegal = 0; DAT_PathFindingState::instance.allAssassinsUnk = 0; - return TRUE; + this->units[unitID].movementCooldown = 0; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::despawnUnreachableUnit, this)(unitID); + return FALSE; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 766545d302..b31ea5fc2b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -60013,7 +60013,7 @@ SHC_3BB0A8C1_0x0053D3C1 | 0.0% | Pending SHC_3BB0A8C1_0x0053D3C2 | 0.0% | Pending -SHC_3BB0A8C1_0x0053D3D0 | 64.2% | Asm differs +SHC_3BB0A8C1_0x0053D3D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0053D850 | 100.0% | Reimplemented