From baf9d172e5e598d85ed1c0d2a74a30889fed6c7c Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Mon, 23 Nov 2026 01:00:05 +0100 Subject: [PATCH] reimplement: SHC_3BB0A8C1_0x00575820 100.0% Reimplemented Units::UpdateHorseArcher_DoShooting, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his draft PR 204; his commits on the file: 9fde7f237a at 70.33%, then 974c32fcf3's clang-format), with this tree's field names (43a17dfac5). It reads a true 100.0 / 100.0 in main's visibility (see the end). The statements are his except the building index local, the goto with its label and the second `= 6` store (the facing direction is now an int local); what the text needed is how they are arranged. 1. Shooting variation 5's failure path (the target is gone and no new one is found) ends in `goto resetShooting;` into the function's last lines, which reset the state and the variation: the original shares them there (0x00575C21, jmp at 0x00575A6E; variation 6's failure keeps its own copy, which goes on to the frame test). Written out, the copy counts as more uses of the constant 4 (the reload state), and the compiler then keeps 4 in a register through the function where the original compares with an immediate 4. 2. `horseArcherShootingVariation = 6;` is written on both paths of variation 5 (behind the copied target position and for no target). The code has one store (0x00575A4B, `mov word ptr [esi + ...], di`); with the two written uses the constant 6 gets its register (`mov edi, 6` at 0x00575935), as in the original. 3. Variation 4: the facing direction (savedFacingDirection) and the target building's index (targetBuildingID) are int locals in front of the targeting-type test (both loaded before the branch, `movsx` at 0x00575865 and 0x0057586C); the index is used for the building's x and y. 4. Variation 6: the acquireShootTarget test the original's way round, the failure (reset) first, so that it is the fall-through (0x00575B87). 5. The three height sums terrainOrClimbHeight first (the target position copy and both arrowShootingRelated heights): the original loads it in front of buildingHeight (0x00575A35, 0x00575AAF, 0x00575AD0). 6. The calls that return BOOLEnum, the BOOLEnum field animationCycleNumberHasJustIncremented and `dying` tested by truthiness; the gfx offsets in decimal and `(EntityType)24` (EntityType has no entry for 24); the usings and the include nobody names dropped. `target > -1` stays (the original compares with -1; `>= 0` differs). Measured with real builds of the units stack's head 6e3de74062 (every header this file includes is the same there as on main 0e4a83b5d2), the whole build list plus this file and its /GL line, BUILD_OK: with the /GL lines of its two callees (setFacingDirectionTowardCoords / -TowardUnit, on the stack's list only through PR 378) taken off, as main has them, UpdateHorseArcher_DoShooting reads 100.0 raw / 100.0 normalised, no diff entry, 345 functions, none of the others reading differently. With the two callees on the list (a build of that stack with this function, UpdateFireEater and UpdateLady, 350 functions), the text before the goto's comment was reworded read the same. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- cmake/compiler-flags-gl.txt | 1 + .../Units/UpdateHorseArcher_DoShooting.cpp | 200 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/UpdateHorseArcher_DoShooting.cpp diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 18dce0ff70..3a736aa2bb 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -137,3 +137,4 @@ src/OpenSHC/Map/Units/UpdateQuarryMason.cpp src/OpenSHC/Map/Units/UpdateDrunkard.cpp src/OpenSHC/Map/Units/UpdateInnkeeper.cpp src/OpenSHC/Map/Units/UpdateMerchant.cpp +src/OpenSHC/Map/Units/UpdateHorseArcher_DoShooting.cpp diff --git a/src/OpenSHC/Map/Units/UpdateHorseArcher_DoShooting.cpp b/src/OpenSHC/Map/Units/UpdateHorseArcher_DoShooting.cpp new file mode 100644 index 0000000000..af27fabe9e --- /dev/null +++ b/src/OpenSHC/Map/Units/UpdateHorseArcher_DoShooting.cpp @@ -0,0 +1,200 @@ +#include "../Units.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.hpp" +#include "OpenSHC/Map/Entities/EntityType.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_CurrentUnitSlotID.hpp" +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitPropertiesDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + + using OpenSHC::Map::Entities::EntityType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00575820 + void Units::UpdateHorseArcher_DoShooting(int unitID) + { + short variation = DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation; + if (variation == 0) { + return; + } + DAT_UnitsState::instance.units[unitID].animationSpeed = 1; + DAT_UnitsState::instance.units[unitID].field_0x58 = 0; + if (variation == 4) { + // both as int locals before the test: the original loads them before it branches + int savedFacingDirection = DAT_UnitsState::instance.units[unitID].facingDirection; + int targetBuildingID = DAT_UnitsState::instance.units[unitID].targetID_OR_targetBuildingID; + if (DAT_UnitsState::instance.units[unitID].targetingType == OpenSHC::Map::Units::UIT_ATTACK_BUILDING) { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardCoords, + DAT_UnitsState::ptr)(DAT_CurrentUnitSlotID::instance, + DAT_BuildingsState::instance.buildings[targetBuildingID].x, + DAT_BuildingsState::instance.buildings[targetBuildingID].y); + } else if (DAT_UnitsState::instance.units[unitID].targetingType == OpenSHC::Map::Units::UIT_ATTACK_LAND + || DAT_UnitsState::instance.units[unitID].shootTargetedUnit == -2) { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardCoords, + DAT_UnitsState::ptr)(DAT_CurrentUnitSlotID::instance, + DAT_UnitsState::instance.units[unitID].shootTargetMicroX / 8, + DAT_UnitsState::instance.units[unitID].shootTargetMicroY / 8); + } else { + MACRO_CALL_MEMBER( + OpenSHC::Map::Units::UnitsState_Func::setFacingDirectionTowardUnit, DAT_UnitsState::ptr)( + DAT_CurrentUnitSlotID::instance, DAT_UnitsState::instance.units[unitID].shootTargetedUnit); + } + DAT_UnitsState::instance.units[unitID].assassinClimbingUpUnk_OR_previousFacingDirection + = DAT_UnitsState::instance.units[unitID].facingDirection; + DAT_UnitsState::instance.units[unitID].facingDirection = savedFacingDirection; + } + int direction = (DAT_UnitsState::instance.units[unitID].assassinClimbingUpUnk_OR_previousFacingDirection + - DAT_TileMapState::instance.mapOrientation + 2) + % 8; + if (DAT_UnitsState::instance.units[unitID].animationCycleNumber >= 100) { + DAT_UnitsState::instance.units[unitID].animationCycleNumber = 0; + } + variation = DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation; + if (variation == 4) { + DAT_UnitsState::instance.units[unitID].animationFrame + = (char)DAT_UnitPropertiesDefinedData::instance.Frames_Shared_UnitClimbingUp + .ANIM_Frames_HorseArcher_Shooting_1[DAT_UnitsState::instance.units[unitID].animationCycleNumber]; + } else if (variation == 5) { + DAT_UnitsState::instance.units[unitID].animationFrame + = (char)DAT_UnitPropertiesDefinedData::instance.Frames_Shared_UnitClimbingUp + .ANIM_Frames_HorseArcher_Shooting_2[DAT_UnitsState::instance.units[unitID].animationCycleNumber]; + } else if (variation == 6) { + DAT_UnitsState::instance.units[unitID].animationFrame + = (char)DAT_UnitPropertiesDefinedData::instance.Frames_Shared_UnitClimbingUp + .ANIM_Frames_HorseArcher_Shooting_3[DAT_UnitsState::instance.units[unitID].animationCycleNumber]; + } + if (DAT_UnitsState::instance.units[unitID].animationFrame > 0) { + if (DAT_UnitsState::instance.units[unitID].state.generic + == OpenSHC::Map::Units::States::US_RELOAD_WEAPONUnk) { + DAT_UnitsState::instance.units[unitID].gfxNumber + = DAT_UnitsState::instance.units[unitID].facingDirectionMapOrientationCorrected + 337; + } + DAT_UnitsState::instance.units[unitID].imageIDUnk + = direction + 329 + DAT_UnitsState::instance.units[unitID].animationFrame * 8; + } + if (variation == 4) { + if (DAT_UnitsState::instance.units[unitID].animationFrame > 0) { + return; + } + DAT_UnitsState::instance.units[unitID].animationCycleNumber = 0; + DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation = 5; + return; + } + if (variation == 5) { + if (DAT_UnitsState::instance.units[unitID].animationFrame > 0) { + return; + } + int target = DAT_UnitsState::instance.units[unitID].shootTargetedUnit; + DAT_UnitsState::instance.units[unitID].animationCycleNumber = 0; + if (target >= 0) { + if (DAT_UnitsState::instance.units[target].logicalState != OpenSHC::Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[unitID].targetUID != DAT_UnitsState::instance.units[target].uid + || DAT_UnitsState::instance.units[target].dying) { + if (MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::acquireShootTarget, + DAT_UnitsState::ptr)(DAT_CurrentUnitSlotID::instance)) { + return; + } + // shares the reset at the end, as the original does: written out, 4 takes a register + goto resetShooting; + } + DAT_UnitsState::instance.units[unitID].shootTargetMicroX + = DAT_UnitsState::instance.units[target].microXPosition; + DAT_UnitsState::instance.units[unitID].shootTargetMicroY + = DAT_UnitsState::instance.units[target].microYPosition; + DAT_UnitsState::instance.units[unitID].shootTargetZ + = DAT_UnitsState::instance.units[target].terrainOrClimbHeight + + DAT_UnitsState::instance.units[target].buildingHeight; + // stored on both paths: with one store behind the if, the 6 loses its register + DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation = 6; + return; + } + DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation = 6; + return; + } + if (variation != 6) { + return; + } + if (DAT_UnitsState::instance.units[unitID].animationFrame == 27 + && DAT_UnitsState::instance.units[unitID].animationCycleNumberHasJustIncremented) { + int target = DAT_UnitsState::instance.units[unitID].shootTargetedUnit; + DAT_UnitsState::instance.units[unitID].field_0x40e = false; + // `> -1`, not `>= 0`: the original compares with -1 + if (target > -1) { + int distance = MACRO_CALL_MEMBER(OpenSHC::Map::Entities::EntityState_Func::arrowShootingRelated, + DAT_EntityState::ptr)(DAT_UnitsState::instance.units[unitID].microXPosition, + DAT_UnitsState::instance.units[unitID].microYPosition, + DAT_UnitsState::instance.units[unitID].terrainOrClimbHeight + 45 + + DAT_UnitsState::instance.units[unitID].buildingHeight, + DAT_UnitsState::instance.units[target].microXPosition, + DAT_UnitsState::instance.units[target].microYPosition, + DAT_UnitsState::instance.units[target].terrainOrClimbHeight + 26 + + DAT_UnitsState::instance.units[target].buildingHeight); + if (distance <= 0 + || DAT_UnitsState::instance.units[target].uid != DAT_UnitsState::instance.units[unitID].targetUID + || DAT_UnitsState::instance.units[target].logicalState != OpenSHC::Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[target].dying) { + // the failure arm first: the original falls through into it + if (!MACRO_CALL_MEMBER( + OpenSHC::Map::Units::UnitsState_Func::acquireShootTarget, DAT_UnitsState::ptr)(unitID)) { + if (DAT_UnitsState::instance.units[unitID].state.generic + == OpenSHC::Map::Units::States::US_RELOAD_WEAPONUnk) { + DAT_UnitsState::instance.units[unitID].state.generic + = OpenSHC::Map::Units::States::US_DETERMINE_NEXT_STATEUnk; + } + DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation = 0; + } else { + DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation = 5; + } + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::prepareProjectileTarget, + DAT_UnitsState::ptr)(unitID, target, distance); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::shootProjectile, DAT_UnitsState::ptr)( + unitID, OpenSHC::Map::Entities::ET_ARROW_AND_DEFAULT, + DAT_UnitsState::instance.units[unitID].shootTargetMicroX, + DAT_UnitsState::instance.units[unitID].shootTargetMicroY, + DAT_UnitsState::instance.units[unitID].shootTargetZ + 30); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + OpenSHC::DE::SHCDE::FX_ARROW_FIRE); + DAT_UnitsState::instance.units[unitID].field_0x3f8 = 0; + } + } else { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::prepareProjectileTarget, DAT_UnitsState::ptr)( + DAT_CurrentUnitSlotID::instance, -1, 200); + MACRO_CALL_MEMBER(OpenSHC::Map::Units::UnitsState_Func::shootProjectile, DAT_UnitsState::ptr)( + DAT_CurrentUnitSlotID::instance, (EntityType)24, + DAT_UnitsState::instance.units[unitID].shootTargetMicroX, + DAT_UnitsState::instance.units[unitID].shootTargetMicroY, + DAT_UnitsState::instance.units[unitID].shootTargetZ + 30); + MACRO_CALL_MEMBER(OpenSHC::Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + OpenSHC::DE::SHCDE::FX_ARROW_FIRE); + DAT_UnitsState::instance.units[unitID].field_0x3f8 = 0; + } + } + if (DAT_UnitsState::instance.units[unitID].animationFrame > 0) { + return; + } + DAT_UnitsState::instance.units[unitID].animationCycleNumber = 0; + resetShooting: + if (DAT_UnitsState::instance.units[unitID].state.generic == OpenSHC::Map::Units::States::US_RELOAD_WEAPONUnk) { + DAT_UnitsState::instance.units[unitID].state.generic + = OpenSHC::Map::Units::States::US_DETERMINE_NEXT_STATEUnk; + } + DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation = 0; + } + +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e8a6864d03..1843d74f1d 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -66399,7 +66399,7 @@ SHC_3BB0A8C1_0x00575818 | 0.0% | Pending SHC_3BB0A8C1_0x00575819 | 0.0% | Pending -SHC_3BB0A8C1_0x00575820 | 0.0% | Pending +SHC_3BB0A8C1_0x00575820 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00575C40 | 0.0% | Pending