From dcb964fdc2e138d08d9806d30b61e3f83ad9ffa4 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Tue, 1 Dec 2026 22:31:36 +0100 Subject: [PATCH 1/2] UnitsState: prepareProjectileTarget returns nothing prepareProjectileTarget 0x0053CBD0 is `void`: the header has `int`, from Ghidra. The original leaves something different in eax on each path: the unit type less 22 (the switch index, 0x0053CDD3), a quotient, the 16-bit value it just stored. None of the seventeen calls on main reads a result, and the code matches with `void` only (the next commit): with `int`, the values have to be named and held, and scatter and the random number swap registers (85.7% / 79.2%). UnitsState.hpp and UnitsState.func.hpp, one line each. The declaration in UnitsState.hpp also names the third parameter leadDistanceUnk, as the definition does. Co-Authored-By: Claude Opus 5.5 --- src/OpenSHC/Map/Units/UnitsState.func.hpp | 2 +- src/OpenSHC/Map/Units/UnitsState.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/UnitsState.func.hpp b/src/OpenSHC/Map/Units/UnitsState.func.hpp index b5346f6f34..207445e069 100644 --- a/src/OpenSHC/Map/Units/UnitsState.func.hpp +++ b/src/OpenSHC/Map/Units/UnitsState.func.hpp @@ -677,7 +677,7 @@ namespace Map { &UnitsState::getEnemyUnitIDNearby) getEnemyUnitIDNearby; - MACRO_FUNCTION_RESOLVER(int (UnitsState::*)(int, int, int), false, Address::SHC_3BB0A8C1_0x0053CBD0, + MACRO_FUNCTION_RESOLVER(void (UnitsState::*)(int, int, int), false, Address::SHC_3BB0A8C1_0x0053CBD0, &UnitsState::prepareProjectileTarget) prepareProjectileTarget; diff --git a/src/OpenSHC/Map/Units/UnitsState.hpp b/src/OpenSHC/Map/Units/UnitsState.hpp index cc2d8c7abc..430b42cf76 100644 --- a/src/OpenSHC/Map/Units/UnitsState.hpp +++ b/src/OpenSHC/Map/Units/UnitsState.hpp @@ -410,7 +410,7 @@ namespace Map { int getEnemyUnitIDNearby(int unitID, int tileUnk, int unitHeight); - int prepareProjectileTarget(int shooterID, int targetID, int param_3); + void prepareProjectileTarget(int shooterID, int targetID, int leadDistanceUnk); BOOLEnum harassBuildingsWithSiegeAI(int unitID); From 6a800c052d22ec1b4f22f502466491eac567f790 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Tue, 1 Dec 2026 22:31:37 +0100 Subject: [PATCH 2/2] reimplement: SHC_3BB0A8C1_0x0053CBD0 100.0% Reimplemented UnitsState::prepareProjectileTarget from 85.7% / 79.2% (gynt's text, on main since #238) to 100%. With the function `void` (the commit before), the returned values go (quick_diff 0.8566 -> 0.9928). The random adjustment of the target's height is written on the field itself, `+=` / `-=` in two arms, instead of through an unsigned copy (1.0000), and the three kinds of scatter are one if / else-if chain (the same listing). The random numbers that fed the returned quotients are passed straight on, the copy of the target's fixedRng is an int, and _scatter and _adjustment start at 0 (the same object code). _archerScatter is never read but stays: without it the archer cases go and both jump tables start at the catapult (quick_diff 0.9857). The target's position is stored in both arms, as before: once above the if reads 0.9162. The order of the tests and every constant are gynt's (e595955379 81.5%, 0c208807ee 85.75%). Measured with real builds of this branch's tree on main 4591b6fe72 (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp raw / normalised): 100.0 / 100.0 in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS off, in one with it on, and in a full build with it off (of this branch merged with the one for six other UnitsState functions). Passing the random numbers straight on, the int copy of fixedRng, the first values of _scatter and _adjustment and the two comments came after those builds. A small build with it off of the final text (this branch merged with the one for six other UnitsState functions, 550 files, 560 functions) reads every function as the earlier small build with it off did. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- .../UnitsState/prepareProjectileTarget.cpp | 63 ++++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/OpenSHC/Map/Units/UnitsState/prepareProjectileTarget.cpp b/src/OpenSHC/Map/Units/UnitsState/prepareProjectileTarget.cpp index 74da8ad0e6..159df72497 100644 --- a/src/OpenSHC/Map/Units/UnitsState/prepareProjectileTarget.cpp +++ b/src/OpenSHC/Map/Units/UnitsState/prepareProjectileTarget.cpp @@ -10,15 +10,16 @@ namespace Map { namespace Units { // FUNCTION: STRONGHOLDCRUSADER 0x0053CBD0 - int UnitsState::prepareProjectileTarget(int shooterID, int targetID, int leadDistanceUnk) + void UnitsState::prepareProjectileTarget(int shooterID, int targetID, int leadDistanceUnk) { - uint _targetFixedRng = 0; + int _targetFixedRng = 0; if (targetID == -1) { MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::setRandomShootLocation, this)(shooterID, this->units[shooterID].shootTargetMicroX, this->units[shooterID].shootTargetMicroY, this->units[shooterID].shootTargetZ); } else { _targetFixedRng = this->units[targetID].fixedRng; + // the three stores stand in both arms (once above the if loses the match) if (this->units[targetID].tunnelerFinishedDigging != 2) { this->units[shooterID].shootTargetMicroX = this->units[targetID].x * 8 + 4; this->units[shooterID].shootTargetMicroY = this->units[targetID].y * 8 + 4; @@ -26,7 +27,7 @@ namespace Map { = this->units[targetID].buildingHeight + this->units[targetID].terrainOrClimbHeight; } else { int _lead = (leadDistanceUnk * 133) / 100; - int _adjustment; + int _adjustment = 0; /* todo:fixme:ucp: this if-else should be improved to also trigger when the targetID has negative height (walking in pitch or moat) */ if (this->units[targetID].stateBasedSpeed != 0) { @@ -64,12 +65,8 @@ namespace Map { } } } - /* the original returns whatever the switch below left in the return register on - the paths that adjust nothing, which is the type's index into it; the value is - not a meaningful result, but callers of those paths are given it */ - int _scatter; - /* The original's jump tables carry entries for the archers that end up in the same place as - every other unhandled type, so their arms did something that was optimised away. */ + int _scatter = 0; + // a dead store: it keeps the archer entries in both jump tables (without it they start at the catapult) BOOLEnum _archerScatter; if (this->units[shooterID].terrainOrClimbHeight > this->units[shooterID].shootTargetZ) { switch (this->units[shooterID].unitType) { @@ -89,9 +86,9 @@ namespace Map { case OpenSHC::Map::Units::UT_E_ARCHER: case OpenSHC::Map::Units::UT_E_ARCHER_DEBUG: _archerScatter = FALSE; - return this->units[shooterID].unitType - 22; + return; default: - return this->units[shooterID].unitType - 22; + return; } } else { _scatter = this->units[shooterID].shootTargetZ - this->units[shooterID].terrainOrClimbHeight; @@ -118,7 +115,7 @@ namespace Map { break; } if (_scatter <= 0) { - return this->units[shooterID].unitType - 22; + return; } if (_scatter > 280) { _scatter = 280; @@ -126,45 +123,35 @@ namespace Map { } int _rng = SEC_RNG::instance.currentNumber2; MACRO_CALL_MEMBER(OpenSHC::Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); - int _unhandledTypeResult = (_rng / 300) * 300; if (_rng % 300 >= _scatter) { - return _unhandledTypeResult; + return; } if ((_rng & 7) < 2) { _scatter /= 4; if (_scatter == 0) { _scatter = 1; } - int _horizontalRng = abs((_rng / 512) + this->units[shooterID].fixedRng); - this->units[shooterID].shootTargetMicroX += ((_horizontalRng % _scatter) - (_scatter / 2)); - int _verticalRng = abs(_rng + _targetFixedRng); - this->units[shooterID].shootTargetMicroY += ((_verticalRng % _scatter) - (_scatter / 2)); - return _verticalRng / _scatter; - } - if ((_rng & 7) < 6) { + this->units[shooterID].shootTargetMicroX + += ((abs((_rng / 512) + this->units[shooterID].fixedRng) % _scatter) - (_scatter / 2)); + this->units[shooterID].shootTargetMicroY += ((abs(_rng + _targetFixedRng) % _scatter) - (_scatter / 2)); + } else if ((_rng & 7) < 6) { _scatter /= 6; if (_scatter == 0) { _scatter = 1; } - int _heightRng = abs(this->units[shooterID].fixedRng + _rng); - int _heightOffset = _heightRng % _scatter + 20; - int const _z = (ushort)this->units[shooterID].shootTargetZ; - if ((short)_z < _heightOffset + 8) { - _unhandledTypeResult = _z + _heightOffset; - this->units[shooterID].shootTargetZ = _unhandledTypeResult; - return _unhandledTypeResult; + int _heightOffset = abs(this->units[shooterID].fixedRng + _rng) % _scatter + 20; + if (this->units[shooterID].shootTargetZ < _heightOffset + 8) { + this->units[shooterID].shootTargetZ += _heightOffset; + } else { + this->units[shooterID].shootTargetZ -= _heightOffset; } - _unhandledTypeResult = _z - _heightOffset; - this->units[shooterID].shootTargetZ = _unhandledTypeResult; - return _unhandledTypeResult; - } - _scatter /= 6; - if (_scatter == 0) { - _scatter = 1; + } else { + _scatter /= 6; + if (_scatter == 0) { + _scatter = 1; + } + this->units[shooterID].shootTargetZ += (abs(_rng + _targetFixedRng) % _scatter) + 40; } - int _heightRng = abs(_rng + _targetFixedRng); - this->units[shooterID].shootTargetZ = this->units[shooterID].shootTargetZ + (_heightRng % _scatter) + 40; - return _heightRng / _scatter; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 98c4514966..4385283746 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -59673,7 +59673,7 @@ SHC_3BB0A8C1_0x0053CAC0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0053CAE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0053CBD0 | 85.8% | Style pass, asm differs +SHC_3BB0A8C1_0x0053CBD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0053CF84 | 0.0% | Pending