From a8042fedd64fd79bcbc6d982bba467883d7533d8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 19 Oct 2026 22:55:00 +0200 Subject: [PATCH 001/202] import: 93 TribesState reimplementations from development Brings in every remaining TribesState method from origin/development so the branch covers all 113 resolvers. Adapted to this branch's generated headers: - Tribe field numbering differs between the branches (field19_0x1c here is field20_0x1e there and so on for 13 fields); renamed the references rather than touching the generated header. - processDeerMoving kept a ushort* walker over unknownAttackRelatedUpdateCounter, which is short in this branch's header. Resolved the pointer into the field and unnested the comma operator. syntax_check.py: 0 errors over 113 files. build_quiet.py: BUILD_OK. Co-Authored-By: Claude Opus 5 (1M context) --- .../Map/Units/TribesState/addRallyPoint.cpp | 18 + .../Units/TribesState/addUnitToNewTribe.cpp | 105 + .../Map/Units/TribesState/addUnitToTribe.cpp | 37 + ...UnitToTribeAndUpdateTribeMovementSpeed.cpp | 60 + ...addUnitsToTribeAndComputeMovementSpeed.cpp | 68 + .../TribesState/aiAssignNewUnitToTribe.cpp | 69 + .../allUnitsReachedTheirDestination.cpp | 43 + ...ribeAreOutsideCoverageOfPathFindingAlg.cpp | 41 + .../applyLadderDestructionToTribeUnits.cpp | 51 + .../applyMoveCommandOrRallyCommandToTribe.cpp | 76 + .../applyMovementDistanceToUnitsInTribe.cpp | 36 + ...ToUnitsInTribeBasedOnUnitNumberInTribe.cpp | 35 + .../TribesState/applyTribeBehaviorType.cpp | 29 + ...applyUnitTopSpeedDelayBasedOnTribeSize.cpp | 125 ++ .../assignAttackTargetsForTribe.cpp | 207 ++ .../Map/Units/TribesState/clearAllTribes.cpp | 29 + .../clearAnimalSpawnLocationsUnk.cpp | 34 + .../TribesState/clearTribesNotOfUnitType.cpp | 60 + .../TribesState/clearTribesOfUnitType.cpp | 61 + .../TribesState/commandUnitsToLocation.cpp | 92 + .../TribesState/computeTribePercentages.cpp | 126 ++ .../consumeFlaggedTribesOfType.cpp | 29 + .../TribesState/countDeerEfficiently.cpp | 46 + .../Map/Units/TribesState/createAnimal.cpp | 141 ++ .../Units/TribesState/createPlayerTribe.cpp | 39 + .../Map/Units/TribesState/createTribe.cpp | 55 + .../TribesState/createTribeForPlayer.cpp | 61 + .../createTribeWithSpawnedUnit.cpp | 37 + .../drawFlagsAndUnitDestinations.cpp | 180 ++ .../findRecentOrSignpostSpawnLocation.cpp | 51 + .../Units/TribesState/flagTribesOfType.cpp | 26 + .../Units/TribesState/getAvailableTribeID.cpp | 63 + .../getFirstUnitInTribeThatIsOnXTerrain.cpp | 39 + ...getMajorityArcherTypeEuropeanOrArabian.cpp | 49 + .../getMajoritySelectedUnitType.cpp | 67 + .../Map/Units/TribesState/getNonDyingUnit.cpp | 38 + .../TribesState/getSpecificUnitFromTribe.cpp | 50 + .../Units/TribesState/getTribeAliveStatus.cpp | 52 + .../TribesState/getUnitIDForIndexInTribe.cpp | 38 + .../TribesState/giveTribeAnInstruction.cpp | 1835 +++++++++++++++++ .../TribesState/giveTribeMoveInstruction.cpp | 550 +++++ ...asAvailableSpawnSlotForWildlifeOrMercs.cpp | 46 + .../TribesState/importStoredInfoFromSlot0.cpp | 48 + .../Units/TribesState/isTribeAllAssassins.cpp | 41 + .../isTribeFreeOfTunnelingUnits.cpp | 39 + .../isTribePathToDestinationClear.cpp | 64 + .../isTribeUnitBlockedByOtherUnit.cpp | 58 + .../moveTribeToIndexedNearbyTile.cpp | 58 + .../moveTribeToNearbyClearTile.cpp | 47 + .../TribesState/moveUnitToBehaviorTarget.cpp | 141 ++ .../TribesState/playAttackCommandFeedback.cpp | 42 + .../playUnitCombatSpeechForTarget.cpp | 195 ++ .../Units/TribesState/playUnitMoveSpeech.cpp | 112 + .../TribesState/playUnitSelectionSound.cpp | 132 ++ .../predictUnitInterceptPosition.cpp | 123 ++ .../Units/TribesState/processDeerMoving.cpp | 164 ++ .../Units/TribesState/removeAllTribeUnits.cpp | 32 + ...oveSelectedUnitsFromTheirCurrentTribes.cpp | 64 + .../removeUnitFromThisTribeIfInTribe.cpp | 45 + .../Units/TribesState/removeUnitFromTribe.cpp | 55 + .../Map/Units/TribesState/respawnDeer.cpp | 73 + .../TribesState/scatterTribeUnitsRandomly.cpp | 65 + .../TribesState/selectionContainsHorses.cpp | 43 + .../Units/TribesState/setRabbitSpawnXY.cpp | 28 + .../TribesState/setStateForAllTribeUnits.cpp | 36 + .../TribesState/setTargetUnitForTribe.cpp | 42 + ...snapshotSelectionTribeAndComputeStance.cpp | 87 + .../sortTribePathDestinationsByCost.cpp | 200 ++ .../TribesState/spawnDeerLionOrRabbit.cpp | 96 + .../spawnQueuedReinforcementWaves.cpp | 486 +++++ .../TribesState/spawnUnitsAroundLocation.cpp | 36 + .../TribesState/spawnUnitsForAITribe.cpp | 49 + .../TribesState/spawnUnitsIntoNewTribe.cpp | 62 + .../spawnWildlifeOrMercAtAvailableSlot.cpp | 56 + .../TribesState/standUpAllTribeUnits.cpp | 39 + .../stopTribeMovementAndCheckIdle.cpp | 55 + .../Units/TribesState/swapTribeOwnership.cpp | 27 + .../tribeContainsUnitThatCanClimb.cpp | 37 + .../tribeHasActiveLaddermanUnit.cpp | 42 + .../Map/Units/TribesState/trimTribeToSize.cpp | 44 + .../trySpawnAdditionalWildlifeForTribe.cpp | 60 + .../TribesState/unitIsSelectedByPlayer.cpp | 24 + .../unsetRallyRelatedFlagOnUnits.cpp | 34 + .../updateAnimalHerdBehaviorState.cpp | 41 + .../TribesState/updateCamelTribeBehavior.cpp | 114 + .../updateLionWolfTribeBehavior.cpp | 123 ++ .../updatePeasantSeatingAtBuilding.cpp | 77 + .../TribesState/updateRabbitTribeBehavior.cpp | 136 ++ .../updateTribeCombatStanceBehavior.cpp | 243 +++ .../TribesState/updateTribeRallyFlags.cpp | 49 + .../updateTribeUnitAssignments.cpp | 73 + .../Map/Units/TribesState/updateTribes.cpp | 133 ++ .../upgradeTribeArrayLayoutForMapVersion.cpp | 45 + 93 files changed, 9109 insertions(+) create mode 100644 src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/createAnimal.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/createTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/updateTribes.cpp create mode 100644 src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp b/src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp new file mode 100644 index 0000000000..c0a9ba8eae --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp @@ -0,0 +1,18 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522C50 + void TribesState::addRallyPoint(int section1016ID, short destinationX, short destinationY, int step) + { + this->tribes[section1016ID].rallyPointArray[step][0] = destinationX; + this->tribes[section1016ID].rallyPointArray[step][1] = destinationY; + this->tribes[section1016ID].rallyPointCount = (short)step + 1; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp new file mode 100644 index 0000000000..8ffc824adf --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp @@ -0,0 +1,105 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/AI/Tribes/AIVUnitTypeTribeArrayOffset.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Game::GameMode; + using Map::Units::SomeTribeBehaviorType; + using Map::Units::UnitType; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005254C0 + BOOLEnum TribesState::addUnitToNewTribe(uint unitID) + { + int _tribeID; + if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) { + return FALSE; + } + int _playerID = (int)DAT_UnitsState::instance.units[unitID].owner; + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[_playerID] == -1 + && ((_tribeID = (int)DAT_UnitsState::instance.units[unitID].tribeID, + _tribeID < 1 || (this->tribes[_tribeID].uid != DAT_UnitsState::instance.units[unitID].tribeUID)) + || (this->tribes[_tribeID].tribeState == 0))) { + int _newTribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(_playerID, 0); + if (0 < _newTribeID) { + UnitTypeShort _unitType = DAT_UnitsState::instance.units[unitID].unitType; + if (_unitType == Map::Units::UT_E_ARCHER) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_ARCHERS; + } else if (_unitType == Map::Units::UT_E_XBOW) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_CROSSBOWMEN; + } else if (_unitType == Map::Units::UT_E_SPEAR) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_SPEARMEN; + } else if (_unitType == Map::Units::UT_E_PIKE) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_PIKEMEN; + } else if (_unitType == Map::Units::UT_E_MACE) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_MACEMEN; + } else if (_unitType == Map::Units::UT_E_SWORD) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_SWORDSMEN; + } else if (_unitType == Map::Units::UT_E_KNIGHT) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_KNIGHTS; + } else if (_unitType == Map::Units::UT_E_LADDER) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_LADDERMEN; + } else if (_unitType == Map::Units::UT_E_ENGINEER) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_ENGINEERS; + } else if (_unitType == Map::Units::UT_HOPSFARMER) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_LADDERMEN; + } else if (_unitType == Map::Units::UT_TUNNELER) { + this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_TUNNELERS; + } else if (_unitType == Map::Units::UT_A_ARCHER) { + this->tribes[_newTribeID].tribeType = ((AITribeType)0x19); + } else if (_unitType == Map::Units::UT_A_SLAVE) { + this->tribes[_newTribeID].tribeType = ((AITribeType)0x1a); + } else if (_unitType == Map::Units::UT_A_SLINGER) { + this->tribes[_newTribeID].tribeType = ((AITribeType)0x1b); + } else if (_unitType == Map::Units::UT_A_ASSASSIN) { + this->tribes[_newTribeID].tribeType = ((AITribeType)0x1c); + } else if (_unitType == Map::Units::UT_A_HARCHER) { + this->tribes[_newTribeID].tribeType = ((AITribeType)0x1d); + } else if (_unitType == Map::Units::UT_A_SWORDSMAN) { + this->tribes[_newTribeID].tribeType + = (AI::Tribes::AITribeTypeShort)AI::Tribes::OFFSET_CROSSBOWMAN; + } else if (_unitType == Map::Units::UT_A_FIRETHROWER) { + this->tribes[_newTribeID].tribeType = ((AITribeType)0x1f); + } else if (_unitType == Map::Units::UT_S_FBALLISTA) { + this->tribes[_newTribeID].tribeType = ((AITribeType)0x18); + } + this->tribes[_newTribeID].someIndex = 1; + this->tribes[_newTribeID].attackWave = (short)DAT_TroopValueState::instance.attackInfo.inv_count; + _unitType = DAT_UnitsState::instance.units[unitID].unitType; + if (_unitType == Map::Units::UT_E_ENGINEER) { + if (DAT_UnitsState::instance.units[unitID].state.generic + == Map::Units::States::US_STAND_UPUnk) { + this->tribes[_newTribeID].tribeBehaviorType + = Map::Units::STBT_0x410_SIEGE_EQUIPMENT_CONSTRUCTION; + } + } else if ((_unitType == Map::Units::UT_TUNNELER) + && (DAT_UnitsState::instance.units[unitID].state.generic + == Map::Units::States::US_LOOK_AROUNDUnk)) { + this->tribes[_newTribeID].tribeBehaviorType = Map::Units::STBT_0x415; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _newTribeID); + return TRUE; + } + } + return FALSE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp new file mode 100644 index 0000000000..d1e78d2f35 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -0,0 +1,37 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522590 + void TribesState::addUnitToTribe(uint unitID, int tribeID) + { + short* psVar1; + uint _mod16; + psVar1 = &this->tribes[tribeID].size; + *psVar1 = *psVar1 + 1; + /* + basically, it sets a bit flag for every unitID that is in the selection + */ + psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); + _mod16 = (int)unitID % 16; + *psVar1 = *psVar1 | DAT_UnitSelectionDefinedData::instance.BitMaskHelper[_mod16]; + if (this->tribes[tribeID].selectionTargetUnitID == 0) { + this->tribes[tribeID].selectionTargetUnitID = (short)unitID; + } + DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = this->tribes[tribeID].selectionTargetUnitID; + DAT_UnitsState::instance.units[unitID].tribeID = (short)tribeID; + DAT_UnitsState::instance.units[unitID].tribeUID = this->tribes[tribeID].uid; + DAT_UnitsState::instance.units[unitID].idInTribe = this->tribes[tribeID].size; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp new file mode 100644 index 0000000000..aa90d51f07 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp @@ -0,0 +1,60 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522B00 + void TribesState::addUnitToTribeAndUpdateTribeMovementSpeed(int param_1, uint unitID, int tribeID) + { + Unit* _ptrUnit; + short _maximumSpeed; + short _minimumSpeed; + int _totalHealth; + short _tribeSize; + short _movementSpeed; + int _tribeID; + _tribeID = tribeID; + _maximumSpeed = 0; + _totalHealth = 0; + _minimumSpeed = 100; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, tribeID); + if (1 < DAT_UnitsState::instance.maxUnitCount) { + tribeID = DAT_UnitsState::instance.maxUnitCount - 1; + _ptrUnit = &DAT_UnitsState::instance.units[1]; + do { + if (_ptrUnit->logicalState == Map::Units::ULS_NORMAL && !_ptrUnit->dying + && _ptrUnit->ifSelectedThenPlayerID == param_1) { + _movementSpeed = _ptrUnit->movementSpeed; + if (_movementSpeed < _minimumSpeed) { + _minimumSpeed = _movementSpeed; + } + if (_maximumSpeed < _movementSpeed) { + _maximumSpeed = _movementSpeed; + } + _totalHealth = _totalHealth + _ptrUnit->healthPercentage; + } + _ptrUnit = _ptrUnit + 0x248; + tribeID = tribeID + -1; + } while (tribeID); + } + _tribeSize = this->tribes[_tribeID].size; + this->tribes[_tribeID].size2Unk = _tribeSize; + if (0 < _tribeSize) { + this->tribes[_tribeID].minimumMovementSpeed = _minimumSpeed; + this->tribes[_tribeID].movementSpeed = _minimumSpeed; + this->tribes[_tribeID].maximumMovementSpeed = _maximumSpeed; + this->tribes[_tribeID].unitsHealthPercentage = (short)(_totalHealth / (int)_tribeSize); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp new file mode 100644 index 0000000000..da55cd304c --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp @@ -0,0 +1,68 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x005229C0 + void TribesState::addUnitsToTribeAndComputeMovementSpeed(int playerID, int tribeID) + { + Unit* _pUnit; + uint _unitID; + int local_8; + short _movementSpeed; + short _maximumSpeed; + short _targetUnit; + local_8 = 0; + short _minimumSpeed = 100; + _maximumSpeed = 0; + if (1 < DAT_UnitsState::instance.maxUnitCount) { + _pUnit = &DAT_UnitsState::instance.units[1]; + _unitID = 1; + do { + if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying + && _pUnit->ifSelectedThenPlayerID == playerID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(_unitID, tribeID); + _movementSpeed = _pUnit->movementSpeed; + if (_movementSpeed < _minimumSpeed) { + _minimumSpeed = _movementSpeed; + } + if (_maximumSpeed < _movementSpeed) { + _maximumSpeed = _movementSpeed; + } + local_8 = local_8 + _pUnit->healthPercentage; + } + _unitID = _unitID + 1; + _pUnit = _pUnit + 0x248; + } while ((int)_unitID < DAT_UnitsState::instance.maxUnitCount); + } + short sVar1 = this->tribes[tribeID].size; + this->tribes[tribeID].size2Unk = sVar1; + if (0 < sVar1) { + _targetUnit = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].facingDirection_1 = DAT_UnitsState::instance.units[_targetUnit].facingDirection; + this->tribes[tribeID].facingDirection_2 = DAT_UnitsState::instance.units[_targetUnit].facingDirection; + this->tribes[tribeID].facingDirection_3 = DAT_UnitsState::instance.units[_targetUnit].facingDirection; + this->tribes[tribeID].movementRunUpTime = DAT_UnitsState::instance.units[_targetUnit].movementRunUpTime; + this->tribes[tribeID].maximumMovementSpeed = _maximumSpeed; + this->tribes[tribeID].minimumMovementSpeed = _minimumSpeed; + this->tribes[tribeID].movementSpeed = _minimumSpeed; + this->tribes[tribeID].field61_0x200 = 0; + this->tribes[tribeID].field62_0x202 = 0; + this->tribes[tribeID].initialMoveOrderGiven = 0; + this->tribes[tribeID].field65_0x208 = 0; + this->tribes[tribeID].field67_0x20c = 0; + this->tribes[tribeID].unitsHealthPercentage = (short)(local_8 / (int)sVar1); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp new file mode 100644 index 0000000000..a7d9591379 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp @@ -0,0 +1,69 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x005237F0 + void TribesState::aiAssignNewUnitToTribe(int playerID, int unitType, int unitID) + { + short* psVar1; + int _tribe; + int _index; + _index = 0; + /* + is engineer + */ + if (unitType == 0x1e) { + _index = 1; + } else { + /* + is tunneler + */ + if (unitType == 5) { + _index = 2; + } else { + /* + is ladderman + */ + if (unitType == 0x1d) { + _index = 3; + } else { + /* + is monk + */ + if (unitType == 0x25) { + _tribe = DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk; + if (!_tribe || this->tribes[_tribe].tribeState == 0 + || this->tribes[_tribe].uid + != DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk) { + _tribe + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); + DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk = _tribe; + DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk + = this->tribes[_tribe].uid; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _tribe); + } + } + } + } + psVar1 = DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeIDs + _index; + _tribe = (int)*psVar1; + if (!_tribe || this->tribes[_tribe].tribeState == 0 + || this->tribes[_tribe].uid + != DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeUIDs[_index]) { + _tribe = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); + *psVar1 = (short)_tribe; + DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeUIDs[_index] = this->tribes[_tribe].uid; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _tribe); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp b/src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp new file mode 100644 index 0000000000..264765943d --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp @@ -0,0 +1,43 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005253F0 + BOOLEnum TribesState::allUnitsReachedTheirDestination(int tribeID) + { + int unitID; + BOOLEnum BVar1; + int unitSelectionIndex = 0; + if (0 < this->tribes[tribeID].size) { + do { + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[unitID].dying == 0)) { + BVar1 = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::hasUnitReachedDestination, DAT_UnitsState::ptr)(unitID); + if (!BVar1) { + return FALSE; + } + } + } while (unitSelectionIndex < this->tribes[tribeID].size); + } + return TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp b/src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp new file mode 100644 index 0000000000..3e0d0ead44 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp @@ -0,0 +1,41 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005250B0 + BOOLEnum TribesState::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg(int tribeID, int algTileFlag) + { + int _unitID; + int iVar1 = (int)this->tribes[tribeID].size; + int unitSelectionIndex = 0; + if (0 < iVar1) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_TileMapState::instance.WalkLayer[DAT_UnitsState::instance.units[_unitID].tile] + != algTileFlag) { + return FALSE; + } + } while (unitSelectionIndex < iVar1); + } + return TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp new file mode 100644 index 0000000000..c24ee9aae0 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp @@ -0,0 +1,51 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524E20 + void TribesState::applyLadderDestructionToTribeUnits(int param_1) + { + int unitID; + BOOLEnum BVar1; + int unitSelectionIndex = 0; + if (0 < this->tribes[param_1].size) { + do { + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[unitID].dying == 0)) { + if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0) {} + if (DAT_UnitsState::instance.units[unitID].field303_0x413 != 0) {} + if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging == 2) {} + if (DAT_UnitsState::instance.units[unitID].state.generic + == Map::Units::States::US_MELEE_ATTACK) {} + bool bVar2 = this->tribes[param_1].unknownBool02 == 0; + BVar1 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::computeLadderClimbPath, + DAT_UnitsState::ptr)(unitID, (uint)(bVar2), 0, (int)(bVar2)); + if (BVar1) { + DAT_UnitsState::instance.units[unitID].state.generic + = Map::Units::States::US_DEATH_02 | Map::Units::States::US_STAND_UPUnk; + } + } + } while (unitSelectionIndex < this->tribes[param_1].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp new file mode 100644 index 0000000000..ac159aebc3 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp @@ -0,0 +1,76 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524340 + undefined4 TribesState::applyMoveCommandOrRallyCommandToTribe( + int tribeID, undefined4 x1, undefined4 y1, undefined4 isRallying, int storeAsRallyPoint) + { + int _unitID; + int _unitSelectionIndex = 0; + BOOLEnum _allAssassins + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + this->tribes[tribeID].someUnitID = 0; + if (storeAsRallyPoint) { + int _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].isRallyingUnk = (short)isRallying; + this->tribes[tribeID].rallyPointArray[0][0] = DAT_UnitsState::instance.units[_targetUnitID].x; + this->tribes[tribeID].rallyPointArray[0][1] = DAT_UnitsState::instance.units[_targetUnitID].y; + this->tribes[tribeID].rallyPointArray[1][0] = (short)x1; + this->tribes[tribeID].rallyPointArray[1][1] = (short)y1; + this->tribes[tribeID].currentRallyPointIndex = 1; + this->tribes[tribeID].rallyPointCount = 2; + } + if (0 < this->tribes[tribeID].size) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitSelectionIndex); + _unitSelectionIndex = _unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID].state.generic != Map::Units::States::US_MELEE_ATTACK + && DAT_UnitsState::instance.units[_unitID].moveableUnk != 0 + && DAT_UnitsState::instance.units[_unitID].moveRelatedFlag != 1) { + DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)x1; + DAT_UnitsState::instance.units[_unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[_unitID].targetingType + = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; + DAT_UnitsState::instance.units[_unitID]._someX_2 = 0; + DAT_UnitsState::instance.units[_unitID]._someY_2 = 0; + DAT_UnitsState::instance.units[_unitID].plannedDestinationY = (short)y1; + DAT_UnitsState::instance.units[_unitID].moveDelay = 0; + DAT_UnitsState::instance.units[_unitID].moveInstructionSpeedDelayTracker = 0; + if (_allAssassins) { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + _unitID, (uint)((short)x1), (uint)((short)y1), 0); + } + } while (_unitSelectionIndex < this->tribes[tribeID].size); + } + return (undefined4)(1); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp new file mode 100644 index 0000000000..4ce40bf6c8 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp @@ -0,0 +1,36 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525130 + void TribesState::applyMovementDistanceToUnitsInTribe(int param_1) + { + int iVar1; + int unitSelectionIndex = 0; + if (0 < this->tribes[param_1].size) { + do { + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { + DAT_UnitsState::instance.units[iVar1].movementDistance + = DAT_TileMapState::instance.CertainPathLayer[DAT_UnitsState::instance.units[iVar1].tile]; + } + } while (unitSelectionIndex < this->tribes[param_1].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp new file mode 100644 index 0000000000..aee7cd5fc4 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp @@ -0,0 +1,35 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x005251A0 + void TribesState::applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe(int param_1) + { + int iVar1; + int unitSelectionIndex = 0; + if (0 < this->tribes[param_1].size) { + do { + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { + DAT_UnitsState::instance.units[iVar1].movementDistance + = DAT_UnitsState::instance.units[iVar1].idInTribe; + } + } while (unitSelectionIndex < this->tribes[param_1].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp b/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp new file mode 100644 index 0000000000..4ad9bac398 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp @@ -0,0 +1,29 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x005221A0 + void TribesState::applyTribeBehaviorType(int attackWave, SomeTribeBehaviorType tribeBehaviorType) + { + Tribe* psVar2; + int iVar1 = DAT_TroopValueState::instance.attackInfo.someCounter1; + psVar2 = &this->tribes[1]; + do { + if (psVar2->tribeState && psVar2->attackWave == attackWave + && (int)(short)psVar2->tribeBehaviorType != tribeBehaviorType + && psVar2->attackInfo_someCounter1 != iVar1) { + psVar2->tribeBehaviorType = (SomeTribeBehaviorTypeShort)tribeBehaviorType; + psVar2->unknownAttackRelatedUpdateCounter = 0; + } + psVar2 = psVar2 + 0x19a; + } while ((int)psVar2 < 0x176261c); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp new file mode 100644 index 0000000000..6e83886c1d --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp @@ -0,0 +1,125 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524EF0 + void TribesState::applyUnitTopSpeedDelayBasedOnTribeSize(int tribeID, BOOLEnum param_2) + { + int _unitInSelection; + int _selectionUnit; + UnitIDMovementDistancePair* _nextUnit; + int _delayIndex; + int iVar1; + UnitIDMovementDistancePair* pUVar2; + short* _ptrMovementDistance; + int _unitSelectionIndex; + short _distance; + int _foundUnsortedEntry; + short _nextUnitID; + short* _tribeSize; + short _unitID; + _tribeSize = &this->tribes[tribeID].size; + _unitSelectionIndex = 0; + if (*_tribeSize < 40) { + /* + For selections (tribes) of size 40 or less + */ + this->someUnitIDArrayCount = 0; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 4004, '\0', (void*)((int)(this->someUnitIDMovementDistancePair))); + if (0 < *_tribeSize) { + /* + Of all units in the selection store unitID and optionally their movementDistance into an array + */ + _ptrMovementDistance = &this->someUnitIDMovementDistancePair[0].movementDistance; + do { + _selectionUnit + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitSelectionIndex); + if ((DAT_UnitsState::instance.units[_selectionUnit].logicalState + == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_selectionUnit].dying == 0)) { + this->someUnitIDArrayCount = this->someUnitIDArrayCount + 1; + /* + sets unitID + */ + ((UnitIDMovementDistancePair*)(_ptrMovementDistance + -1))->unitID = (short)_selectionUnit; + if (!param_2) { + *_ptrMovementDistance = DAT_UnitsState::instance.units[_selectionUnit].movementDistance; + } + } + _unitSelectionIndex = _unitSelectionIndex + 1; + _ptrMovementDistance = _ptrMovementDistance + 2; + } while (_unitSelectionIndex < *_tribeSize); + } + do { + /* + Do a sort of the array, sort by distance value (low to high) + */ + _foundUnsortedEntry = 0; + /* + fixme:reuse: WARNING reuse of parameter + */ + tribeID = 0; + if (this->someUnitIDArrayCount == 1 || this->someUnitIDArrayCount + -1 < 0) + break; + pUVar2 = this->someUnitIDMovementDistancePair; + do { + _nextUnit = pUVar2 + 1; + _distance = pUVar2->unitID; + _unitID = pUVar2->movementDistance; + _nextUnitID = pUVar2[1].movementDistance; + if ((!param_2) && (_nextUnitID < _unitID)) { + _foundUnsortedEntry = 1; + pUVar2->unitID = _nextUnit->unitID; + _nextUnit->unitID = _distance; + pUVar2->movementDistance = _nextUnitID; + pUVar2[1].movementDistance = _unitID; + } + tribeID = tribeID + 1; + pUVar2 = _nextUnit; + } while (tribeID < this->someUnitIDArrayCount + -1); + } while (_foundUnsortedEntry); + _delayIndex = 0; + if (0 < this->someUnitIDArrayCount) { + pUVar2 = this->someUnitIDMovementDistancePair; + do { + DAT_UnitsState::instance.units[pUVar2->unitID].topSpeedDelayIndex = (short)_delayIndex; + _delayIndex = _delayIndex + 1; + pUVar2 = pUVar2 + 1; + } while (_delayIndex < this->someUnitIDArrayCount); + } + } else if (0 < *_tribeSize) { + do { + /* + Set incremental delay on first 199 units, then set same value of 199 + */ + _unitInSelection + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitSelectionIndex); + iVar1 = _unitSelectionIndex; + if (199 < _unitSelectionIndex) { + iVar1 = 199; + } + DAT_UnitsState::instance.units[_unitInSelection].topSpeedDelayIndex = (short)iVar1; + _unitSelectionIndex = _unitSelectionIndex + 1; + } while (_unitSelectionIndex < *_tribeSize); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp new file mode 100644 index 0000000000..4d3cb64f97 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -0,0 +1,207 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/TroopValueState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005244D0 + undefined4 TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType) + { + short sVar3; + uint _unitID; + int _tile2; + uint _freeTile; + int iVar5; + BOOLEnum BVar6; + bool bVar7; + uint _x; + uint _y; + short _targetTileY; + uint _tile1; + int unitSelectionIndex = 0; + BOOLEnum BVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[this->tribes[tribeID].owner] != -1) { + return (undefined4)(0); + } + if (0 < this->tribes[tribeID].size) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID].moveableUnk != 0) { + /* + fixme: this is some interesting data happening all over the unit code + */ + if (targetType == 1021) { + LAB_00524621: + if (targetType == 1010) { + _tile2 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, + DAT_TroopValueState::ptr)(_unitID); + LAB_00524638: + _tile1 = DAT_TroopValueState::instance.tile; + _y = DAT_TroopValueState::instance.y; + _x = DAT_TroopValueState::instance.x; + if (_tile2) { + DAT_UnitsState::instance.units[_unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + if (targetType != Map::Units::STBT_0x414) + goto LAB_0052471e; + _freeTile = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::findFreeTileNearby, + DAT_UnitsState::ptr)(_unitID, _tile1); + if (_freeTile) { + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID, + (uint)(_freeTile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_freeTile]] + .addXgetTile), + (uint)(DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_freeTile]), + 0); + sVar3 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_tile1]; + DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar3; + DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID + = (short)_tile1; + iVar5 = DAT_ViewportRenderState::instance.translationMatrix[sVar3].addXgetTile; + DAT_UnitsState::instance.units[_unitID].targetingType + = Map::Units::UIT_ATTACK_WALL; + DAT_UnitsState::instance.units[_unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[_unitID].attackAtTileX + = (short)_tile1 - (short)iVar5; + } + } + } else { + if (targetType != Map::Units::STBT_0x3f4) { + if (targetType == Map::Units::STBT_0x3f3) { + _tile2 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findNearestAvailableScalePoint, + DAT_TroopValueState::ptr)(_unitID); + } else if (targetType == Map::Units::STBT_0x3f5) { + _tile2 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, + DAT_TroopValueState::ptr)(_unitID); + } else if (targetType == Map::Units::STBT_0x3fb) { + _tile2 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, + DAT_TroopValueState::ptr)(_unitID); + } else if (targetType == Map::Units::STBT_0x3fd) { + _tile2 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, + DAT_TroopValueState::ptr)(_unitID); + } else if (targetType == Map::Units::STBT_0x3f6) { + _tile2 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyTowersOrGates, + DAT_TroopValueState::ptr)(_unitID); + } else if ((targetType == Map::Units::STBT_0x413) + || (targetType == Map::Units::STBT_0x414)) { + _tile2 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(_unitID); + } else { + if (targetType != Map::Units::STBT_0x3f7) + continue; + _tile2 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::calculateTile2MoatValueClosestToUnit, + DAT_TroopValueState::ptr)(_unitID); + } + goto LAB_00524638; + } + iVar5 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findNearestAvailableScalePoint, + DAT_TroopValueState::ptr)(_unitID); + _tile1 = DAT_TroopValueState::instance.tile; + _y = DAT_TroopValueState::instance.y; + _x = DAT_TroopValueState::instance.x; + if (!iVar5) + continue; + DAT_UnitsState::instance.units[_unitID].state.generic = ((UnitState)0x67); + LAB_0052471e: + _targetTileY + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile1]; + DAT_UnitsState::instance.units[_unitID].attackAtTileY = _targetTileY; + DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = _tile1; + DAT_UnitsState::instance.units[_unitID].attackAtTileX = (short)_tile1 + - (short)DAT_ViewportRenderState::instance.translationMatrix[_targetTileY] + .addXgetTile; + DAT_UnitsState::instance.units[_unitID].unknownDigMoatOrWallAttackFlag1015 + = (undefined2)targetType; + iVar5 = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(_unitID); + if (!iVar5) { + if (BVar4) { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + BVar6 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID, _x, _y, 0); + if (!BVar6) { + DAT_UnitsState::instance.units[_unitID].logicalState = Map::Units::ULS_REMOVE; + } + } + if ((int)DAT_UnitsState::instance.units[_unitID].selectionTargetUnitID == _unitID) { + this->tribes[tribeID].targetX = (short)_x; + this->tribes[tribeID].targetY = (short)_y; + } + } + } else { + UnitStateShort UVar1 = DAT_UnitsState::instance.units[_unitID].state.generic; + if (UVar1 != Map::Units::States::US_MELEE_ATTACK_WALL + && UVar1 != Map::Units::States::US_MOVE_TO_DESTINATION && UVar1 != (UnitState)0x67) { + UnitTypeShort UVar2 = DAT_UnitsState::instance.units[_unitID].unitType; + if (UVar2 == Map::Units::UT_E_SPEAR) { + bVar7 = UVar1 == Map::Units::States::US_AIM_WEAPONUnk; + } else if (UVar2 == Map::Units::UT_E_ENGINEER) { + bVar7 = UVar1 == Map::Units::States::US_RELOAD_WEAPONUnk; + } else if (UVar2 == Map::Units::UT_E_LADDER) { + bVar7 = UVar1 == ((UnitState)3); + } else { + if (UVar2 != Map::Units::UT_TUNNELER) + goto LAB_00524621; + if (UVar1 == (UnitState)2 || UVar1 == (UnitState)3 + || UVar1 == Map::Units::States::US_RELOAD_WEAPONUnk + || UVar1 == Map::Units::States::US_STAND_UPUnk) + continue; + bVar7 = UVar1 + == (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk); + } + if (!bVar7) + goto LAB_00524621; + } + } + } + } while (unitSelectionIndex < this->tribes[tribeID].size); + } + return (undefined4)(1); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp b/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp new file mode 100644 index 0000000000..2c23576941 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp @@ -0,0 +1,29 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" + +#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x005211B0 + void TribesState::clearAllTribes() + { + this->clans = 0; + this->field2_0x8 = 1; + this->field9_0x24 = 0; + DAT_CurrentTribeID::instance = 1; + do { + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 0x334, '\0', (void*)((int)(this->tribes + DAT_CurrentTribeID::instance))); + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; + } while (DAT_CurrentTribeID::instance < 1250); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp b/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp new file mode 100644 index 0000000000..ddd3278e60 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp @@ -0,0 +1,34 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Location/Point4ShortXY.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + using Map::Location::Point4ShortXY; + + // FUNCTION: STRONGHOLDCRUSADER 0x005232E0 + void TribesState::clearAnimalSpawnLocationsUnk() + { + short* psVar1; + psVar1 = &DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[0].y; + do { + ((Point4ShortXY*)(psVar1 + -1))->x = 0; + *psVar1 = 0; + psVar1[0x1d7] = 0; + psVar1[0x1d8] = 0; + psVar1[0xe9f] = 0; + psVar1[0xea0] = 0; + psVar1[0xea9] = 0; + psVar1[0xeaa] = 0; + psVar1 = psVar1 + 2; + } while ((int)psVar1 < 0x117d1de); + DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor = 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp new file mode 100644 index 0000000000..a6881f1b5e --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp @@ -0,0 +1,60 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/MSVC_SecurityCookie.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00525970 + void TribesState::clearTribesNotOfUnitType(int playerID, int unitType) + { + Unit* psVar1; + int _tribeID2; + uint unitID; + Tribe* psVar2; + undefined1 _clearedTribes[1252]; + uint local_4; + local_4 = MSVC_SecurityCookie::instance ^ (uint)_clearedTribes; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 1250, '\0', (void*)((int)(_clearedTribes))); + unitID = 1; + if (1 < DAT_UnitsState::instance.maxUnitCount) { + psVar1 = &DAT_UnitsState::instance.units[1]; + do { + if (psVar1->owner == playerID && psVar1->ifSelectedThenPlayerID + && (short)psVar1->unitType != unitType) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromThisTribeIfInTribe, this)( + unitID, (int)(psVar1->tribeID)); + psVar1->ifSelectedThenPlayerID = 0; + DAT_UnitsState::instance.unitCountOfSelection[playerID] + = DAT_UnitsState::instance.unitCountOfSelection[playerID] + -1; + _clearedTribes[psVar1->tribeID] = 1; + } + unitID = unitID + 1; + psVar1 = psVar1 + 0x248; + } while ((int)unitID < DAT_UnitsState::instance.maxUnitCount); + } + _tribeID2 = 1; + psVar2 = &this->tribes[1]; + do { + if (psVar2->tribeState == 2) { + if (psVar2->selectionTargetUnitID == -1) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(_tribeID2); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(_tribeID2); + } + _tribeID2 = _tribeID2 + 1; + psVar2 = psVar2 + 0x19a; + } while (_tribeID2 < 1250); + ; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp new file mode 100644 index 0000000000..fab7f19fd7 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp @@ -0,0 +1,61 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/MSVC_SecurityCookie.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00525870 + void TribesState::clearTribesOfUnitType(int param_1, int param_2) + { + Unit* psVar1; + int tribeID; + uint unitID; + short* psVar2; + undefined1 local_4e8[1252]; + uint local_4; + local_4 = MSVC_SecurityCookie::instance ^ (uint)local_4e8; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 1250, '\0', (void*)((int)(local_4e8))); + unitID = 1; + if (1 < DAT_UnitsState::instance.maxUnitCount) { + psVar1 = &DAT_UnitsState::instance.units[1]; + do { + if ((psVar1->owner == param_1) && (psVar1->ifSelectedThenPlayerID)) { + if ((short)psVar1->unitType == param_2) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromThisTribeIfInTribe, this)( + unitID, (int)(psVar1->tribeID)); + psVar1->ifSelectedThenPlayerID = 0; + DAT_UnitsState::instance.unitCountOfSelection[param_1] + = DAT_UnitsState::instance.unitCountOfSelection[param_1] + -1; + } + local_4e8[psVar1->tribeID] = 1; + } + unitID = unitID + 1; + psVar1 = psVar1 + 0x248; + } while ((int)unitID < DAT_UnitsState::instance.maxUnitCount); + } + tribeID = 1; + psVar2 = &this->tribes[1].selectionTargetUnitID; + do { + if (psVar2[-0xd] == 2) { + if (*psVar2 == -1) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(tribeID); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); + } + tribeID = tribeID + 1; + psVar2 = psVar2 + 0x19a; + } while (tribeID < 0x4e2); + ; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp new file mode 100644 index 0000000000..5b81fc17bd --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -0,0 +1,92 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00526F00 + undefined4 TribesState::commandUnitsToLocation( + int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) + { + int _unitID; + UnitTypeShort _unitType; + int _selectionUnitID = 0; + bool bVar1 = true; + if (0 < this->tribes[tribeID].size) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _selectionUnitID); + _selectionUnitID = _selectionUnitID + 1; + if ((DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID].dying == 0)) { + short _goToRallyPoint = DAT_UnitsState::instance.units[_unitID].goToRallyPoint; + DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; + if (!_goToRallyPoint && DAT_UnitsState::instance.units[_unitID].isSelectable_OR_matchTime != 0 + && (MACRO_CALL_MEMBER( + Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(destinationX, (int)(destinationY), + (int)(DAT_UnitsState::instance.units[_unitID].x), + (int)(DAT_UnitsState::instance.units[_unitID].y)), + 4 < DAT_DirectionAlgorithmState::instance.distanceHigh) + && (DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID == 0 + || (DAT_UnitsState::instance + .units[DAT_UnitsState::instance.units[_unitID] + .targetedUnitID__OR__engineerMannedSiegeEngineRef] + .logicalState + != Map::Units::ULS_NORMAL)) + && (((_goToRallyPoint = DAT_UnitsState::instance.units[_unitID].shootTargetedUnit, + _goToRallyPoint == 0 + || (this->tribes[DAT_CurrentTribeID::instance].countdown2 <= 0)) + || (DAT_UnitsState::instance.units[_unitID].unitType == Map::Units::UT_A_HARCHER)) + || (DAT_UnitsState::instance.units[_unitID].targetUID + != DAT_UnitsState::instance.units[_goToRallyPoint].uid)) + && DAT_UnitsState::instance.units[_unitID].state.generic + != Map::Units::States::US_MELEE_ATTACK + && ((char)matchUnitSpeeds != '\0' + || (((_unitType = DAT_UnitsState::instance.units[_unitID].unitType, + _unitType != Map::Units::UT_S_FBALLISTA + && (_unitType != Map::Units::UT_S_CATAPULT)) + && ((_unitType != Map::Units::UT_S_SHIELD + && ((_unitType != Map::Units::UT_S_TOWER + && (_unitType != Map::Units::UT_S_BATTERINGRAM)))))))) + && (DAT_UnitsState::instance.units[_unitID].engineerRelatedUnk == 0 + || ((DAT_UnitsState::instance.units[_unitID].resourceToDeposit != 0 + && (DAT_UnitsState::instance.units[_unitID].field252_0x3c4 == 0))))) { + DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; + bVar1 = false; + } else { + DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 1; + } + } + } while (_selectionUnitID < this->tribes[tribeID].size); + if (!bVar1) { + if ((char)matchUnitSpeeds != '\0') { + this->tribes[tribeID].freeUnitSpeeds = 0; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); + return (undefined4)(1); + } + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp new file mode 100644 index 0000000000..81efda3376 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -0,0 +1,126 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005270F0 + void TribesState::computeTribePercentages(int tribeID) + { + uint _unitID; + BOOLEnum BVar3; + short _percentage = this->tribes[tribeID].selectionTargetUnitID; + int iVar4 = 0; + short sVar1 = this->tribes[tribeID].size; + this->tribes[tribeID].percentageDyingUnk = 0; + this->tribes[tribeID].percentageMovingUnk = 0; + this->tribes[tribeID].percentageAttackingUnk = 0; + this->tribes[tribeID].field150_0x29c = 0; + this->tribes[tribeID].percentageShootingUnk = 0; + this->tribes[tribeID].field152_0x2a0 = 0; + this->tribes[tribeID].percentageSomething = 0; + this->tribes[tribeID].movementSpeed = DAT_UnitsState::instance.units[_percentage].movementSpeed; + int local_8 = 0; + int _countRangeUnk = 0; + int _countAttackingUnk = 0; + int _countMoving = 0; + int _countDying = 0; + this->tribes[tribeID].unitType = DAT_UnitsState::instance.units[_percentage].unitType; + if (0 < sVar1) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)( + tribeID, iVar4); + iVar4 = iVar4 + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) { + BVar3 = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::isComputerManagedNonPeasant, DAT_UnitsState::ptr)(_unitID); + if (!BVar3) { + if (DAT_UnitsState::instance.units[_unitID].dying == 0) { + if (DAT_UnitsState::instance.units[_unitID].movementRelated != 8) { + _countMoving = _countMoving + 1; + } + UnitStateShort UVar2 = DAT_UnitsState::instance.units[_unitID].state.generic; + if (UVar2 == Map::Units::States::US_MELEE_ATTACK) { + _countAttackingUnk = _countAttackingUnk + 1; + } + if (UVar2 == Map::Units::States::US_MELEE_ATTACK_WALL) { + _countAttackingUnk = _countAttackingUnk + 1; + } + if (UVar2 + == (Map::Units::States::US_DEATH_02 | Map::Units::States::US_STAND_UPUnk + | Map::Units::States::US_RELOAD_WEAPONUnk)) { + local_8 = local_8 + 1; + } + if (DAT_UnitsState::instance.units[_unitID].field316_0x430 != 0 + && ((UVar2 == Map::Units::States::US_RELOAD_WEAPONUnk + || (UVar2 == Map::Units::States::US_AIM_WEAPONUnk)) + || (UVar2 == Map::Units::States::US_FIRE_WEAPONUnk))) { + _countRangeUnk = _countRangeUnk + 1; + } + UnitTypeShort _unitType = this->tribes[tribeID].unitType; + if (_unitType != (UnitType)0 + && DAT_UnitsState::instance.units[_unitID].unitType != _unitType) { + this->tribes[tribeID].unitType = ((UnitType)0); + } + short _movementSpeed = DAT_UnitsState::instance.units[_unitID].movementSpeed; + if (this->tribes[tribeID].movementSpeed < _movementSpeed) { + this->tribes[tribeID].movementSpeed = _movementSpeed; + } + } else { + _countDying = _countDying + 1; + } + } else { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, this)( + _unitID, tribeID); + } + } + } while (iVar4 < this->tribes[tribeID].size); + } + int _tribeSize = (int)this->tribes[tribeID].size; + if (!_tribeSize) { + _percentage = 100; + } else { + _percentage = (short)((_countDying * 100) / _tribeSize); + } + this->tribes[tribeID].percentageDyingUnk = _percentage; + if (!_tribeSize) { + _percentage = 100; + } else { + _percentage = (short)((_countMoving * 100) / _tribeSize); + } + this->tribes[tribeID].percentageMovingUnk = _percentage; + if (!_tribeSize) { + _percentage = 100; + } else { + _percentage = (short)((_countAttackingUnk * 100) / _tribeSize); + } + this->tribes[tribeID].percentageAttackingUnk = _percentage; + if (!_tribeSize) { + _percentage = 100; + } else { + _percentage = (short)((_countRangeUnk * 100) / _tribeSize); + } + this->tribes[tribeID].percentageShootingUnk = _percentage; + if (_tribeSize) { + this->tribes[tribeID].percentageSomething = (short)((local_8 * 100) / _tribeSize); + } + this->tribes[tribeID].percentageSomething = 100; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp b/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp new file mode 100644 index 0000000000..04e1ddee29 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp @@ -0,0 +1,29 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00523790 + void TribesState::consumeFlaggedTribesOfType(int param_1, int param_2) + { + short* psVar1; + int iVar2; + psVar1 = &this->tribes[1].unknownBool02; + iVar2 = 1; + do { + if (param_2 <= 0) {} + if (psVar1[-0x139] != 0 && psVar1[-0x138] == param_1 && *psVar1 != 0) { + *psVar1 = 0; + psVar1[1] = 1; + param_2 = param_2 + -1; + } + iVar2 = iVar2 + 1; + psVar1 = psVar1 + 0x19a; + } while (iVar2 < 0x4e2); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp b/src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp new file mode 100644 index 0000000000..8459eac662 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp @@ -0,0 +1,46 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x005236A0 + void TribesState::countDeerEfficiently() + { + DAT_GameState::instance.mapAndTime.aliveDeerCount = 0; + /* + The original walks the unit array three at a time with the test written out once + per unit, so the three blocks below are kept separate rather than folded. + */ + for (int i = 1; i < 2500; i += 3) { + Unit* unit = &DAT_UnitsState::instance.units[i]; + if (unit[0].logicalState == Map::Units::ULS_NORMAL && unit[0].dying == 0 + && unit[0].unitType == Map::Units::UT_ANTELOPESHDEER) { + DAT_GameState::instance.mapAndTime.aliveDeerCount + = DAT_GameState::instance.mapAndTime.aliveDeerCount + 1; + } + if (unit[1].logicalState == Map::Units::ULS_NORMAL && unit[1].dying == 0 + && unit[1].unitType == Map::Units::UT_ANTELOPESHDEER) { + DAT_GameState::instance.mapAndTime.aliveDeerCount + = DAT_GameState::instance.mapAndTime.aliveDeerCount + 1; + } + if (unit[2].logicalState == Map::Units::ULS_NORMAL && unit[2].dying == 0 + && unit[2].unitType == Map::Units::UT_ANTELOPESHDEER) { + DAT_GameState::instance.mapAndTime.aliveDeerCount + = DAT_GameState::instance.mapAndTime.aliveDeerCount + 1; + } + } + } + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp new file mode 100644 index 0000000000..4452bcc59f --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -0,0 +1,141 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Commands::MappersEnum; + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525BA0 + dword TribesState::createAnimal(MappersEnum animalType, uint x, uint y, int tile) + { + uint unitID; + UnitType unitType = ((UnitType)0); + int _rng2 = 0; + dword tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(0, 0); + if ((int)tribeID <= 0) { + return (dword)(0); + } + if (399 < x || 399 < y || *(char*)(y * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x) == '\0') { + return (dword)(0); + } + int _tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + if (animalType == Commands::M_MAPPER_DEER) { + if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x4a5014b1U)) { + return (dword)(0); + } + this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN; + unitType = Map::Units::UT_ANTELOPESHDEER; + _rng2 = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 9; + } else if (animalType == Commands::M_MAPPER_LION) { + if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x4a5014b1U)) { + return (dword)(0); + } + this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN; + unitType = Map::Units::UT_LIONSHWOLF; + _rng2 = ((byte)SEC_RNG::instance.currentNumber2 & 1) + 3; + } else if (animalType == Commands::M_MAPPER_RABBIT) { + if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x4a5014b1U)) { + return (dword)(0); + } + this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN; + unitType = Map::Units::UT_RABBIT; + _rng2 = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 0xe; + } else if (animalType == Commands::M_MAPPER_CAMEL) { + if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x4a5014b1U)) { + return (dword)(0); + } + this->tribes[tribeID].tribeType = ((AITribeType)0x10); + unitType = Map::Units::UT_CAMELSHBEAR; + _rng2 = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 2; + } else { + if (animalType == Commands::M_MAPPER_CROW_SEAGULL) { + MACRO_CALL_MEMBER(Map::LandscapeState_Func::markNearbyTreesAsCrowTargets, DAT_LandscapeState::ptr)( + x, (int)(y)); + return (dword)(0); + } + if (animalType == Commands::M_MAPPER_SEAGULL) { + if (!(DAT_TileMapState::instance.LogicLayer[_tile] & 1)) { + return (dword)(0); + } + MACRO_CALL_MEMBER(Map::Entities::EntityState_Func::createSeagull, DAT_EntityState::ptr)( + x * 8, (int)(y * 8)); + return (dword)(0); + } + } + int iVar3 = 0; + if (_rng2) { + do { + unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + 0, 0, (int)(x * 8), (int)(y * 8), tile, unitType); + if (unitID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, (int)(tribeID)); + DAT_UnitsState::instance.units[unitID].substate + = (byte)DAT_UnitsState::instance.units[unitID].fixedRng & 3; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + byte bVar2 = (byte)DAT_UnitsState::instance.units[unitID].fixedRng & 7; + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = bVar2; + if (bVar2 == 3) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } else if (bVar2 == 4) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } else if (bVar2 == 5) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } else if ((bVar2 == 6) || (bVar2 == 7)) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 2; + } + if (!iVar3) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 0; + } else if ((iVar3 == 1) || (iVar3 == 2)) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } + } + DAT_UnitsState::instance.units[unitID].disappearFadeAlphaCountdown = 0x20; + } + iVar3 = iVar3 + 1; + } while (iVar3 < _rng2); + } + short sVar1 = this->tribes[tribeID].size; + this->tribes[tribeID].herdMinSize = sVar1 / 2; + if (animalType == Commands::M_MAPPER_LION) { + this->tribes[tribeID].herdMaxSize = sVar1; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::storeLionXAndYInMapInfo, this)( + x, (undefined4)((int)(y))); + return (dword)(tribeID); + } + this->tribes[tribeID].herdMaxSize = sVar1 * 2; + if (animalType == Commands::M_MAPPER_DEER) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::storeDeerXAndYinMapInfo, this)( + x, (undefined4)((int)(y))); + return (dword)(tribeID); + } + if (animalType == Commands::M_MAPPER_RABBIT) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setRabbitSpawnXY, this)(x, (undefined4)((int)(y))); + return (dword)(tribeID); + } + if (animalType == Commands::M_MAPPER_CAMEL) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setCamelSpawnXY, this)(x, (undefined4)((int)(y))); + } + return (dword)(tribeID); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp new file mode 100644 index 0000000000..f387a63f7c --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp @@ -0,0 +1,39 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Map::Units::SomeTribeBehaviorType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522950 + int TribesState::createPlayerTribe(int playerID, undefined4 one, int tribeID) + { + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 820, '\0', (void*)((int)(this->tribes + tribeID))); + this->tribes[tribeID].tribeState = 2; + this->tribes[tribeID].owner = playerID; + this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; + DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; + this->tribes[tribeID].tribeType = ((AITribeType)0); + this->tribes[tribeID].field18_0x1c = 0; + this->tribes[tribeID].field19_0x1e = 0; + this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[tribeID].field29_0x30 = 0; + this->tribes[tribeID].selectionTargetUnitID = 0; + this->tribes[tribeID].size = 0; + return tribeID; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp new file mode 100644 index 0000000000..e3ffcd3989 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -0,0 +1,55 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Map::Units::SomeTribeBehaviorType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522890 + int TribesState::createTribe(int playerID, int setAsCurrentTribeID) + { + DAT_CurrentTribeID::instance = 1; + do { + int _tribeID = DAT_CurrentTribeID::instance; + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0) { + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 820, '\0', (void*)((int)(this->tribes + DAT_CurrentTribeID::instance))); + if (_tribeID <= 0) { + return 0; + } + if ((playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) && (setAsCurrentTribeID)) { + this->DAT_CurrentTribeID = _tribeID; + } + this->tribes[_tribeID].owner = playerID; + this->tribes[_tribeID].tribeState = 2; + this->tribes[_tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; + DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; + this->tribes[_tribeID].tribeType = ((AITribeType)0); + this->tribes[_tribeID].field18_0x1c = 0; + this->tribes[_tribeID].field19_0x1e = 0; + this->tribes[_tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[_tribeID].field29_0x30 = 0; + this->tribes[_tribeID].selectionTargetUnitID = 0; + this->tribes[_tribeID].size = 0; + return _tribeID; + } + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; + } while (DAT_CurrentTribeID::instance < 0x4e2); + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp new file mode 100644 index 0000000000..304f09c361 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -0,0 +1,61 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Map::Units::SomeTribeBehaviorType; + + // FUNCTION: STRONGHOLDCRUSADER 0x005227E0 + int TribesState::createTribeForPlayer(int playerID) + { + short* psVar1; + int _selectionID; + _selectionID = 1250 - playerID; + if (0 < _selectionID) { + /* + find an empty tribe + */ + psVar1 = (short*)((int)this + _selectionID * 0x334 + 0x40); + while (*psVar1 != 0) { + _selectionID = _selectionID + -8; + psVar1 = psVar1 + -0xcd0; + if (_selectionID <= 0) { + return 0; + } + } + /* + wipe the tribe + */ + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 820, '\0', (void*)((int)(this->tribes + _selectionID))); + if (0 < _selectionID) { + this->tribes[_selectionID].owner = playerID; + this->tribes[_selectionID].tribeState = 2; + this->tribes[_selectionID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; + DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; + this->tribes[_selectionID].tribeType = ((AITribeType)0); + this->tribes[_selectionID].field18_0x1c = 0; + this->tribes[_selectionID].field19_0x1e = 0; + this->tribes[_selectionID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[_selectionID].field29_0x30 = 0; + this->tribes[_selectionID].selectionTargetUnitID = 0; + this->tribes[_selectionID].size = 0; + return _selectionID; + } + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp new file mode 100644 index 0000000000..1b055c4dcf --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp @@ -0,0 +1,37 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00523190 + dword TribesState::createTribeWithSpawnedUnit( + short someIndex, undefined4 tribeType, int x, int y, int playerID, UnitType unitType, int count) + { + if ((x <= 0) && (y <= 0)) { + return (dword)(0); + } + dword _tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); + if ((int)_tribeID <= 0) { + return (dword)(0); + } + this->tribes[_tribeID].tribeType = (AITribeTypeShort)tribeType; + this->tribes[_tribeID].someIndex = someIndex; + this->tribes[_tribeID].attackWave = (short)DAT_TroopValueState::instance.attackInfo.inv_count; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSuitableSpawnLocationUnk, + DAT_PathFindingState::ptr)(x, y, -1, -1, 2000, 0); + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::spawnUnitAndAddToTribe, + DAT_PathFindingState::ptr)(playerID, playerID, count, unitType, (undefined4)((int)(_tribeID))); + this->tribes[_tribeID].pendingScatterMove = 1; + return (dword)(_tribeID); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp new file mode 100644 index 0000000000..3643b58122 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -0,0 +1,180 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/IO/Graphics/GmID.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using IO::Graphics::GmID; + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00523A30 + void TribesState::drawFlagsAndUnitDestinations(int tribeID) + { + UnitInstructionTypeShort UVar2; + int _unitID; + int iVar4; + uint _rallyIndex; + short (*_pRallyPoints)[2]; + int tile; + short _isRallying; + _isRallying = this->tribes[tribeID].isRallyingUnk; + if (_isRallying && this->tribes[tribeID].owner == DAT_GameSynchronyState::instance.currentPlayerSlotID + && (_rallyIndex = (uint)(_isRallying < 0), + (int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount)) { + _pRallyPoints = this->tribes[tribeID].rallyPointArray + _rallyIndex; + do { + /* + spawn the rally point flag + */ + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(IO::Graphics::GID_FLOATS_NEW, + (int)(((*_pRallyPoints)[0] + DAT_TileMapState::instance.flagAnimationFrame + _rallyIndex + + (*_pRallyPoints)[1]) + % 10 + + 0x61), + 0x12, -1, + (int)(DAT_ViewportRenderState::instance.translationMatrix[(*_pRallyPoints)[1]].addXgetTile + + (*_pRallyPoints)[0]), + 0xa0022); + _rallyIndex = _rallyIndex + 1; + _pRallyPoints = _pRallyPoints + 1; + } while ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount); + } + int iVar5 = 0; + if (0 < this->tribes[tribeID].size) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)( + tribeID, iVar5); + iVar5 = iVar5 + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].unknownTestAgainst0_2 == 0 + && DAT_UnitsState::instance.units[_unitID].owner + == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + short sVar1 = DAT_UnitsState::instance.units[_unitID].unitSpeedMatchingRelatedUnk; + if (!sVar1) { + if (DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan + < DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan + && (UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType, + UVar2 != Map::Units::UIT_UNIT_ATTACK_UNIT) + && UVar2 != Map::Units::UIT_ATTACK_LAND + && UVar2 != Map::Units::UIT_MAN_SIEGE_EQUIPMENT) { + iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; + if (7 < iVar4) { + iVar4 = 0xf - iVar4; + } + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, + (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[_unitID] + .plannedDestinationY] + .addXgetTile, + 2); + } + } else { + if (sVar1 < 8) { + iVar4 = (8 - sVar1) * 4; + } else { + iVar4 = 0; + } + tile = (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[_unitID].plannedDestinationY] + .addXgetTile; + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)( + IO::Graphics::GID_CURSORS, (int)(0x6a - sVar1), 10, 6, tile, iVar4 << 0x10 | 2); + iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; + if (7 < iVar4) { + iVar4 = 0xf - iVar4; + } + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, tile, + (int)(DAT_UnitsState::instance.units[_unitID].unitSpeedMatchingRelatedUnk << 0x11 | 2)); + } + UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType; + if ((UVar2 == Map::Units::UIT_ATTACK_LAND) || (UVar2 == Map::Units::UIT_THROW_COW)) { + MACRO_CALL_MEMBER(Map::TileMapState_Func::spawnFloatingNumberAroundTile, + DAT_TileMapState::ptr)((int)DAT_UnitsState::instance.units[_unitID].attackAtTileX, + (int)(DAT_UnitsState::instance.units[_unitID].attackAtTileY), + (int)(DAT_UnitsState::instance.units[_unitID].unkAttackRelated)); + } else { + if (UVar2 == Map::Units::UIT_FILL_MOAT) { + iVar4 = (int)DAT_UnitsState::instance.units[_unitID].targetX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] + .addXgetTile; + } else { + if (UVar2 != Map::Units::UIT_DIG_MOAT) { + if (UVar2 == Map::Units::UIT_UNIT_ATTACK_UNIT) { + sVar1 = DAT_UnitsState::instance.units[_unitID] + .targetedUnitID__OR__engineerMannedSiegeEngineRef; + if ((DAT_UnitsState::instance.units[sVar1].uid + == DAT_UnitsState::instance.units[_unitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID) + && (DAT_UnitsState::instance.units[sVar1].logicalState + == Map::Units::ULS_NORMAL)) { + DAT_UnitsState::instance.units[sVar1].field45_0x6c = 1; + } + } else if (UVar2 == Map::Units::UIT_ATTACK_BUILDING) { + sVar1 = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; + if ((sVar1) + && (DAT_BuildingsState::instance.buildings[sVar1].uid + == DAT_UnitsState::instance.units[_unitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { + DAT_BuildingsState::instance.buildings[sVar1].field68_0xc2 = 1; + } + } else if (UVar2 == Map::Units::UIT_LIGHT_PITCH) { + sVar1 = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; + if ((sVar1) + && (DAT_TileMapState::instance.pitchDitches[sVar1].uid + == DAT_UnitsState::instance.units[_unitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { + DAT_TileMapState::instance + .MiscDisplayLayer[DAT_TileMapState::instance.pitchDitches[sVar1].tile] + = DAT_TileMapState::instance.MiscDisplayLayer + [DAT_TileMapState::instance.pitchDitches[sVar1].tile] + | 0x4000; + } + } else { + uint uVar3 = DAT_UnitsState::instance.units[_unitID].targetedBuildingTile; + if ((uVar3) && (DAT_TileMapState::instance.BuildingLayer[uVar3] != 0)) { + DAT_BuildingsState::instance + .buildings[DAT_TileMapState::instance.BuildingLayer[uVar3]] + .field68_0xc2 = 1; + } + } + continue; + } + iVar4 = (int)DAT_UnitsState::instance.units[_unitID].targetX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] + .addXgetTile; + } + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingTextElement, + DAT_ViewportRenderState::ptr)(0x4e, 6, -0x20, iVar4, 2); + } + } + } while (iVar5 < this->tribes[tribeID].size); + } + return; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp b/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp new file mode 100644 index 0000000000..b5522b2b92 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp @@ -0,0 +1,51 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00523410 + undefined4 TribesState::findRecentOrSignpostSpawnLocation(uint* param_1, uint* param_2) + { + int iVar4 = 0; + do { + uint uVar2 = (DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor + iVar4) % 4; + short sVar1 = DAT_GameState::instance.mapAndTime.lionLocationsXY[uVar2].x; + if (sVar1) { + *param_1 = (int)sVar1; + uVar2 = (DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor + iVar4) % 4; + uint uVar3 = (uint)DAT_GameState::instance.mapAndTime.lionLocationsXY[uVar2].y; + *param_2 = uVar3; + uVar2 = *param_1; + if (uVar2 < 400 && uVar3 < 400 && *(char*)(uVar2 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + uVar3 * 400) != '\0' + && !(DAT_TileMapState::instance + .LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[uVar3].addXgetTile + + uVar2] + & 0x4a5014b1U)) { + uVar2 = (DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor + 1 + iVar4) % 4; + DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor = (short)uVar2; + return (undefined4)(1); + } + } + iVar4 = iVar4 + 1; + if (3 < iVar4) { + for (iVar4 = 0; iVar4 < 8; iVar4++) { + if (DAT_GameState::instance.mapAndTime.signpostIDs[iVar4] != 0) { + *param_1 = DAT_GameState::instance.mapAndTime.signpostsMapEdge[iVar4][0].x; + *param_2 = DAT_GameState::instance.mapAndTime.signpostsMapEdge[iVar4][0].y; + return (undefined4)(1); + } + } + return (undefined4)(0); + } + } while (true); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp b/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp new file mode 100644 index 0000000000..201bc4a50d --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp @@ -0,0 +1,26 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00523750 + void TribesState::flagTribesOfType(int param_1) + { + Tribe* psVar1; + int iVar1; + psVar1 = &this->tribes[1]; + iVar1 = 0x4e1; + do { + if (psVar1->tribeState && (short)psVar1->tribeType == param_1) { + psVar1->unknownBool02 = 1; + } + psVar1 = psVar1 + 0x19a; + iVar1 = iVar1 + -1; + } while (iVar1); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp new file mode 100644 index 0000000000..67c84cd233 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -0,0 +1,63 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Game/GameMode.hpp" + +#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Game::GameMode; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522720 + int TribesState::getAvailableTribeID(int playerID) + { + int _tribeID; + if (DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) { + _tribeID = 1249; + while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { + _tribeID = _tribeID + -1; + DAT_CurrentTribeID::instance = _tribeID; + if (_tribeID <= 0) { + return 0; + } + } + DAT_CurrentTribeID::instance = _tribeID; + if (0 < _tribeID) { + this->tribes[_tribeID].time = 1; + return _tribeID; + } + } else { + _tribeID = 1250 - playerID; + DAT_CurrentTribeID::instance = _tribeID; + if (0 < _tribeID) { + /* + is tribe slot in use? + */ + while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { + _tribeID = _tribeID + -8; + DAT_CurrentTribeID::instance = _tribeID; + if (_tribeID <= 0) { + return 0; + } + } + DAT_CurrentTribeID::instance = _tribeID; + if (0 < _tribeID) { + /* + claim it + */ + this->tribes[_tribeID].time = (short)DAT_GameCore::instance.mapTimeInTicks + 400; + return _tribeID; + } + } + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp b/src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp new file mode 100644 index 0000000000..2087bb1a86 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp @@ -0,0 +1,39 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x005240D0 + int TribesState::getFirstUnitInTribeThatIsOnXTerrain(int selectionID) + { + int _unitID; + int iVar1 = (int)this->tribes[selectionID].size; + int _unitSelectionIndex = 0; + if (0 < iVar1) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + selectionID, _unitSelectionIndex); + _unitSelectionIndex = _unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && (DAT_TileMapState::instance.LogicLayer[DAT_UnitsState::instance.units[_unitID].tile] + & 0x40000000U)) { + return _unitID; + } + } while (_unitSelectionIndex < iVar1); + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp b/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp new file mode 100644 index 0000000000..393c04c0e5 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp @@ -0,0 +1,49 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x005242B0 + UnitType TribesState::getMajorityArcherTypeEuropeanOrArabian(int selectionID) + { + int _unitID; + int iVar2 = (int)this->tribes[selectionID].size; + int _unitSelectionIndex = 0; + int _europeanArcherCount = 0; + int _arabArcherCount = 0; + if (0 < iVar2) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + selectionID, _unitSelectionIndex); + _unitSelectionIndex = _unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID].dying == 0)) { + UnitTypeShort UVar1 = DAT_UnitsState::instance.units[_unitID].unitType; + if (UVar1 == Map::Units::UT_A_ARCHER) { + _arabArcherCount = _arabArcherCount + 1; + } + if (UVar1 == Map::Units::UT_E_ARCHER) { + _europeanArcherCount = _europeanArcherCount + 1; + } + } + } while (_unitSelectionIndex < iVar2); + if (_europeanArcherCount < _arabArcherCount) { + return Map::Units::UT_A_ARCHER; + } + } + return Map::Units::UT_E_ARCHER; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp new file mode 100644 index 0000000000..343f1fe9f9 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp @@ -0,0 +1,67 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/OS.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + + /* + fixme: there is more data on the stack than used? decompilerscript: committed: 2025-01-30 21:57:43.216000 + */ + // FUNCTION: STRONGHOLDCRUSADER 0x00521720 + UnitType TribesState::getMajoritySelectedUnitType(int tribeID, int* maximumCount) + { + Unit* _pUnit; + int _maximumTroopType; + int _index; + int _countInclusive; + int _maximumCount; + int aiStackY_20140[32764]; + int _countOfUnitTypes[80]; + MACRO_CALL(OS_Func::_memset)(_countOfUnitTypes, 0, (size_t)((int)(320))); + if (1 < DAT_UnitsState::instance.maxUnitCount) { + _pUnit = &DAT_UnitsState::instance.units[1]; + _countInclusive = DAT_UnitsState::instance.maxUnitCount - 1; + do { + /* + bug: shouldn't this check if the unit is part of the tribe !? It checks for being selected I see + */ + if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying + && _pUnit->owner == DAT_GameSynchronyState::instance.currentPlayerSlotID && _pUnit->isSelected + && (short)_pUnit->unitType < 0x50) { + *(int*)((int)_countOfUnitTypes + (short)_pUnit->unitType * 4) + = *(int*)((int)_countOfUnitTypes + (short)_pUnit->unitType * 4) + 1; + } + _pUnit = _pUnit + 0x248; + _countInclusive = _countInclusive + -1; + } while (_countInclusive); + } + _maximumTroopType = 0; + _maximumCount = -1; + _index = 1; + do { + if (_maximumCount < *(int*)((int)_countOfUnitTypes + _index * 4)) { + _maximumTroopType = _index; + _maximumCount = *(int*)((int)_countOfUnitTypes + _index * 4); + } + _index = _index + Map::Units::UT_PEASANT; + } while (_index < 80); + if (_maximumTroopType != (UnitType)0 && maximumCount != (int*)0x0) { + *maximumCount = _maximumCount; + } + return (UnitType)(_maximumTroopType); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp b/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp new file mode 100644 index 0000000000..6932203d1a --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp @@ -0,0 +1,38 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00523630 + int TribesState::getNonDyingUnit(int tribeID) + { + int iVar1; + if (tribeID <= 0) { + return 0; + } + int unitSelectionIndex = 0; + short _size = this->tribes[tribeID].size; + do { + if (_size <= unitSelectionIndex) { + return 0; + } + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + } while (((DAT_UnitsState::instance.units[iVar1].logicalState != Map::Units::ULS_NORMAL) + || (DAT_UnitsState::instance.units[iVar1].dying != 0)) + || (DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue != 1)); + return iVar1; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp new file mode 100644 index 0000000000..cabe82d2ea --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp @@ -0,0 +1,50 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_00ee0fb4.hpp" +#include "OpenSHC/Globals/DAT_Tribe_HighestID.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522410 + int TribesState::getSpecificUnitFromTribe(int tribeID, int param_2) + { + short* psVar3; + if (!param_2) { + DAT_Tribe_HighestID::instance = (int)this->tribes[tribeID].highestID; + DAT_00ee0fb4::instance = 0; + } + if (DAT_Tribe_HighestID::instance < 200) { + psVar3 = this->tribes[tribeID].unitSelectionBitMasked + DAT_Tribe_HighestID::instance; + int iVar1 = DAT_Tribe_HighestID::instance; + int iVar2 = DAT_00ee0fb4::instance; + do { + if ((*psVar3 != 0) && (iVar2 < 0x10)) { + do { + if ((int)*psVar3 & 1 << ((byte)iVar2 & 0x1f)) { + DAT_00ee0fb4::instance = iVar2 + 1; + if (iVar2 + 1 != 0x10) { + DAT_Tribe_HighestID::instance = iVar1; + return iVar1 * 0x10 + iVar2; + } + DAT_Tribe_HighestID::instance = iVar1 + 1; + DAT_00ee0fb4::instance = 0; + return iVar1 * 0x10 + iVar2; + } + iVar2 = iVar2 + 1; + } while (iVar2 < 0x10); + } + iVar2 = 0; + iVar1 = iVar1 + 1; + psVar3 = psVar3 + 1; + DAT_00ee0fb4::instance = 0; + } while (iVar1 < 200); + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp new file mode 100644 index 0000000000..75dde08432 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp @@ -0,0 +1,52 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + /* + @return 0 if all alive, 1 if majority alive, 50 if fewer alive than dead, 100 if all dead decompilerscript: + committed: 2025-01-30 21:57:43.216000 decompilerscript: committed: 2026-05-02 18:15:17.059000 + */ + // FUNCTION: STRONGHOLDCRUSADER 0x00524BF0 + int TribesState::getTribeAliveStatus(int tribeID) + { + int _unit; + int _size = (int)this->tribes[tribeID].size; + int _index = 0; + int _other = 0; + int _alive = 0; + if (0 < _size) { + do { + _unit = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _index); + _index = _index + 1; + if ((DAT_UnitsState::instance.units[_unit].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unit].dying == 0)) { + if (DAT_UnitsState::instance.units[_unit].tunnelerFinishedDigging == 2) { + _alive = _alive + 1; + } else { + _other = _other + 1; + } + } + } while (_index < _size); + if (_alive != 0) { + if (_alive <= _other) { + return (int)(50); + } + return (uint)(_other != 0); + } + } + return 100; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp new file mode 100644 index 0000000000..f8147cb5f1 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -0,0 +1,38 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522390 + int TribesState::getUnitIDForIndexInTribe(int tribeID, int unitSelectionIndex) + { + int iVar2; + short* psVar3; + int _counter; + _counter = 0; + int iVar1 = 0; + if ((this->tribes[tribeID].tribeState == 2) && (unitSelectionIndex < this->tribes[tribeID].size)) { + psVar3 = this->tribes[tribeID].unitSelectionBitMasked; + do { + if (*psVar3 != 0) { + for (iVar2 = 0; iVar2 < 16; iVar2++) { + if ((int)*psVar3 & 1 << ((byte)iVar2 & 0x1f)) { + if (_counter == unitSelectionIndex) { + return iVar1 * 0x10 + iVar2; + } + _counter = _counter + 1; + } + } + } + iVar1 = iVar1 + 1; + psVar3 = psVar3 + 1; + } while (iVar1 < 200); + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp new file mode 100644 index 0000000000..6a339abc21 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -0,0 +1,1835 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Random/RNG.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Navigation/PathHelper12.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AttackInfoDefinedData.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Game::GameMode; + using Game::GameMode2; + using Map::Buildings::BuildingType; + using Map::Navigation::PathHelper12; + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + /* + gynt: unitID and x and y and id are the target of the instruction decompilerscript: committed: 2025-01-30 + 21:57:43.216000 + */ + // FUNCTION: STRONGHOLDCRUSADER 0x00527C80 + undefined4 TribesState::giveTribeAnInstruction( + int tribeID, UnitInstructionType unitInstructionType, int id, int unitUID, int param_5) + { + short* psVar1; + byte bVar2; + UnitStateShort UVar3; + UnitTypeShort UVar4; + bool bVar5; + Unit* pUVar6; + char cVar7; + short sVar8; + int _unitID_0x03; + int _unitID_0x04; + int _targetUnitID; + uint _x_528565; + BOOL _isAllAssassins_528565; + UnitInstructionType UVar10; + int iVar11; + int _unitID_0x05_0x16; + int iVar12; + int _unitID; + short sVar9; + uint uVar13; + int iVar14; + int iVar15; + int _laddermanID; + undefined3 extraout_var; + int _unitID_0x1e; + BOOLEnum BVar16; + uint _freeTileX; + uint uVar17; + uint x; + int _unitID_0x11; + short _param_3_id_x_tile_buildingID_copy; + int _unitSelectionIndex; + int* piVar18; + uint y; + uint uVar19; + bool _gamemodeSiegeThat; + int _unitTribeIndex; + int _ramOrHorse; + uint local_8; + dword _param_4_unitUID_Y_copy; + int _tile1_528565; + short _y_528565; + int _param_1_tribeID; + int _paramUnitID; + int _paramUnitUID; + short _tribeSize; + int _freeTile; + uint _defensiveTile1; + int _defensiveTile2; + short _freeTileY; + short _y; + short _x; + uint _id; + int _param_3_id_x_tile_buildingID; + int _param_4_unitUID_Y; + ushort _targetArea; + _param_4_unitUID_Y = unitUID; + iVar11 = id; + _param_1_tribeID = tribeID; + _tribeSize = this->tribes[tribeID].size; + _param_4_unitUID_Y_copy = unitUID; + _ramOrHorse = 0; + local_8 = 0; + bVar5 = false; + this->tribes[tribeID].someUnitID = 0; + this->tribes[tribeID].containsRabbit = 0; + _param_3_id_x_tile_buildingID_copy = (short)id; + uVar13 = id; + switch (unitInstructionType) { + case Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk: + _unitSelectionIndex = 0; + if (0 < _tribeSize) { + do { + _unitID_0x03 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _param_1_tribeID, _unitSelectionIndex); + _unitSelectionIndex = _unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[_unitID_0x03].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID_0x03].dying == 0)) { + DAT_UnitsState::instance.units[_unitID_0x03].field253_0x3c5 = 3; + DAT_UnitsState::instance.units[_unitID_0x03].targetingType + = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; + DAT_UnitsState::instance.units[_unitID_0x03].field243_0x3b0 = 0; + } + } while (_unitSelectionIndex < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case Map::Units::UIT_UNIT_ATTACK_UNIT: + /* + unit attack target + */ + this->tribes[tribeID].isRallyingUnk = 0; + pUVar6 = DAT_UnitsState::instance.units + id; + _unitTribeIndex = 0; + /* + repurposed parameter: counts amount of melee units + */ + id = 0; + if (pUVar6->uid != unitUID) { + return (undefined4)(0); + } + if (0 < _tribeSize) { + do { + _unitID_0x04 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[_unitID_0x04].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[_unitID_0x04].dying != 0 + || DAT_UnitsState::instance.units[_unitID_0x04].field303_0x413 != 0) + continue; + DAT_UnitsState::instance.units[_unitID_0x04]._someX_2 = 0; + DAT_UnitsState::instance.units[_unitID_0x04]._someY_2 = 0; + switch (DAT_UnitsState::instance.units[_unitID_0x04].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + /* + melee units + */ + if (DAT_UnitsState::instance.units[iVar11].unitType != Map::Units::UT_ANTELOPESHDEER + && (param_5 != 1 + || (DAT_UnitsState::instance.units[iVar11].state.generic + != Map::Units::States::US_MELEE_ATTACK))) { + id = id + 1; + } + break; + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + /* + ranged units + */ + if (DAT_UnitsState::instance.units[_unitID_0x04].state.generic == (UnitState)0x69) { + DAT_UnitsState::instance.units[_unitID_0x04].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + DAT_UnitsState::instance.units[_unitID_0x04] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[_unitID_0x04].targetUID = unitUID; + DAT_UnitsState::instance.units[_unitID_0x04].targetingType + = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[_unitID_0x04] + .targetedUnitID__OR__engineerMannedSiegeEngineRef = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_0x04].shootTargetedUnit + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_0x04].field283_0x3f8 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(_unitID_0x04); + goto LAB_00527ee1; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + DAT_UnitsState::instance.units[_unitID_0x04].field253_0x3c5 = 5; + DAT_UnitsState::instance.units[_unitID_0x04].targetingType = Map::Units::UIT_ATTACK_LAND; + DAT_UnitsState::instance.units[_unitID_0x04].attackAtTileX + = DAT_UnitsState::instance.units[iVar11].x; + DAT_UnitsState::instance.units[_unitID_0x04].attackAtTileY + = DAT_UnitsState::instance.units[iVar11].y; + DAT_UnitsState::instance.units[_unitID_0x04].unkAttackRelated = 0xb; + DAT_UnitsState::instance.units[_unitID_0x04].shootBeforeStop = 10; + break; + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + DAT_UnitsState::instance.units[_unitID_0x04].targetingType + = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[_unitID_0x04] + .targetedUnitID__OR__engineerMannedSiegeEngineRef = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_0x04] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[_unitID_0x04].field283_0x3f8 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(_unitID_0x04); + DAT_UnitsState::instance.units[_unitID_0x04].shootBeforeStop = 10; + LAB_00527ee1: + if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { + this->tribes[_param_1_tribeID].containsRabbit = 1; + } + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + if (id | x | id != 0) { + /* + if there are melee units in the selection + */ + _targetUnitID = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; + local_8 = (uint)DAT_UnitsState::instance.units[_targetUnitID].x; + unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[_targetUnitID].y; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( + _targetUnitID, iVar11, (int*)&local_8, (int*)&unitInstructionType); + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathPlanningForTribe, + DAT_PathFindingState::ptr)(tribeID, (undefined4)((int)(iVar11)), local_8, + (uint)(unitInstructionType), id, + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[_targetUnitID].tile])), + (int)(DAT_UnitsState::instance.units[_targetUnitID].owner)); + this->tribes[_param_1_tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; + this->tribes[_param_1_tribeID].someUnitUID = unitUID; + this->tribes[_param_1_tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; + iVar12 = 0; + sVar9 = this->tribes[_param_1_tribeID].size; + this->tribes[_param_1_tribeID].someTile2 + = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] + .addXgetTile; + unitInstructionType = ((UnitInstructionType)0); + if (0 < sVar9) { + id = 0x12d5c6c; + do { + iVar11 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); + iVar12 = iVar12 + 1; + if (DAT_UnitsState::instance.units[iVar11].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar11].dying == 0 + && DAT_UnitsState::instance.units[iVar11].field303_0x413 == 0) { + UVar4 = DAT_UnitsState::instance.units[iVar11].unitType; + switch (UVar4) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + if (((param_5 != 1) + || (DAT_UnitsState::instance.units[iVar11].state.generic + != Map::Units::States::US_MELEE_ATTACK)) + && (iVar14 = *(int*)id, iVar14 != 0)) { + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar14]; + uVar13 = iVar14 + - DAT_ViewportRenderState::instance.translationMatrix[sVar9] + .addXgetTile; + if (*(int*)(id + 4) == 0) { + DAT_UnitsState::instance.units[iVar11].targetingType + = ((UnitInstructionType)0); + if (UVar4 == Map::Units::UT_LORD) + break; + } else { + DAT_UnitsState::instance.units[iVar11].targetingType + = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[iVar11] + .targetedUnitID__OR__engineerMannedSiegeEngineRef + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar11] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + } + UVar3 = DAT_UnitsState::instance.units[iVar11].state.generic; + DAT_UnitsState::instance.units[iVar11].plannedDestinationX = (short)uVar13; + DAT_UnitsState::instance.units[iVar11].plannedDestinationY = sVar9; + DAT_UnitsState::instance.units[iVar11].targetedBuildingTile = 0; + DAT_UnitsState::instance.units[iVar11].movementType_OR_targetUnitID = 0; + if (UVar3 == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[iVar11].movementCooldown + = DAT_UnitsState::instance.units[iVar11].movementSpeed * -4; + } + DAT_UnitsState::instance.units[iVar11].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + BVar16 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + if (!BVar16) { + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; + } else { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(iVar11, uVar13, (uint)(sVar9), 0); + UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[iVar11] + .totalSizeOfPathPlan + / 2); + DAT_UnitsState::instance.units[iVar11].lookForEnemy = -0x32; + if ((int)unitInstructionType < (int)UVar10) { + unitInstructionType = UVar10; + } + id = id + 0xc; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + } + } + } + } while (iVar12 < this->tribes[_param_1_tribeID].size); + } + iVar11 = unitInstructionType * 8; + if (iVar11 < 9) { + iVar11 = 8; + } + this->tribes[_param_1_tribeID].field166_0x2be = (short)iVar11; + return (undefined4)(1); + } + } + break; + case Map::Units::UIT_ATTACK_LAND: + case Map::Units::UIT_THROW_COW: + unitUID = this->tribes[tribeID].owner; + this->tribes[tribeID].isRallyingUnk = 0; + _unitTribeIndex = 0; + if (0 < _tribeSize) { + do { + _unitID_0x05_0x16 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if ((DAT_UnitsState::instance.units[_unitID_0x05_0x16].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID_0x05_0x16].dying == 0)) { + switch (DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_E_ARCHER_DEBUG: + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + case Map::Units::UT_S_FBALLISTA: + if (DAT_UnitsState::instance.units[_unitID_0x05_0x16].state.generic + == (UnitState)0x69) { + DAT_UnitsState::instance.units[_unitID_0x05_0x16].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType + = (UnitInstructionTypeShort)unitInstructionType; + LAB_00528808: + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 + = (byte)unitInstructionType; + sVar9 = (short)_param_4_unitUID_Y; + if (unitInstructionType == Map::Units::UIT_THROW_COW + && (UVar4 = DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType, + UVar4 == Map::Units::UT_S_CATAPULT || (UVar4 == Map::Units::UT_S_TREBUCHET))) { + if (DAT_GameState::instance.playerDataArray[unitUID].counter <= 0) { + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 = 5; + } else { + DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType + = Map::Units::UIT_THROW_COW; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetMicroX + = _param_3_id_x_tile_buildingID_copy * 8; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetMicroY = sVar9 * 8; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetZ = (ushort) + * (byte*)(DAT_ViewportRenderState::instance + .translationMatrix[_param_4_unitUID_Y] + .addXgetTile + + 0x1d32c38 + iVar11); + } + } + DAT_UnitsState::instance.units[_unitID_0x05_0x16].attackAtTileX + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].attackAtTileY = sVar9; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].unkAttackRelated = (short)param_5; + DAT_UnitsState::instance.units[_unitID_0x05_0x16]._someX_2 = 0; + DAT_UnitsState::instance.units[_unitID_0x05_0x16]._someY_2 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(_unitID_0x05_0x16); + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootBeforeStop = 10; + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + if (DAT_UnitsState::instance.units[_unitID_0x05_0x16] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 0) + goto LAB_00528808; + } + } + if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { + return (undefined4)(1); + } + } while (true); + } + break; + case Map::Units::UIT_FILL_MOAT: + _targetArea = DAT_TileMapState::instance.PathConnectionLayer + [DAT_UnitsState::instance.units[this->tribes[tribeID].selectionTargetUnitID].tile]; + this->tribes[tribeID].isRallyingUnk = 0; + if ((int)(short)_targetArea != 0) { + iVar11 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, + DAT_PathFindingState::ptr)( + this->tribes[tribeID].owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); + if (iVar11 == 0) { + return (undefined4)(1); + } + unitUID = DAT_PathFindingState::instance.climbY; + uVar13 = DAT_PathFindingState::instance.climbX; + } + case Map::Units::UIT_DIG_MOAT: + iVar11 = unitUID; + iVar12 = 0; + this->tribes[_param_1_tribeID].isRallyingUnk = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, uVar13, (uint)(unitUID), 0, 1); + if (0 < this->tribes[_param_1_tribeID].size) { + do { + iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar12); + iVar12 = iVar12 + 1; + if ((DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar14].dying == 0)) { + switch (DAT_UnitsState::instance.units[iVar14].unitType) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_ENGINEER: + case Map::Units::UT_A_SLAVE: + if (DAT_UnitsState::instance.units[iVar14].state.generic == (UnitState)0x69) { + DAT_UnitsState::instance.units[iVar14].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + DAT_UnitsState::instance.units[iVar14].targetingType = (undefined2)unitInstructionType; + DAT_UnitsState::instance.units[iVar14].attackAtTileX = (short)uVar13; + DAT_UnitsState::instance.units[iVar14].attackAtTileY = (short)iVar11; + DAT_UnitsState::instance.units[iVar14].targetX = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar14].targetY + = (UnitStateShort)_param_4_unitUID_Y_copy; + DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; + DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID = 0; + DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; + } + } + } while (iVar12 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case Map::Units::UIT_ATTACK_BUILDING: + case ((UnitInstructionType)0x24): + case ((UnitInstructionType)0x26): + _unitTribeIndex = 0; + id = 0; + this->tribes[tribeID].targetBuildingID = _param_3_id_x_tile_buildingID_copy; + this->tribes[tribeID].targetBuildingUID = unitUID; + this->tribes[tribeID].isRallyingUnk = 0; + if (0 < _tribeSize) { + do { + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[iVar12].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[iVar12].dying != 0 + || DAT_UnitsState::instance.units[iVar12].field303_0x413 != 0) + continue; + DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; + switch (DAT_UnitsState::instance.units[iVar12].unitType) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_FIRETHROWER: + goto switchD_00528b77_caseD_16; + case Map::Units::UT_E_KNIGHT: + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) { + _ramOrHorse = _ramOrHorse + 1; + } + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + switchD_00528b77_caseD_5: + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) { + id = id + 1; + } + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + if (unitInstructionType == (UnitInstructionType)0x26 + || DAT_BuildingsState::instance.buildings[iVar11].buildingType + == Map::Buildings::BT_PITCHDITCH) + break; + DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + goto LAB_00528c46; + case Map::Units::UT_S_BATTERINGRAM: + if ((DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) + && (DAT_UnitsState::instance.units[iVar12] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) { + id = id + 1; + _ramOrHorse = _ramOrHorse + 1; + } + break; + case Map::Units::UT_A_HARCHER: + _ramOrHorse = _ramOrHorse + 1; + switchD_00528b77_caseD_16: + if (unitInstructionType == (UnitInstructionType)0x24) { + switch (DAT_BuildingsState::instance.buildings[iVar11].buildingType) { + case Map::Buildings::BT_GATEHOUSELARGE: + case Map::Buildings::BT_GATEHOUSESMALL: + case Map::Buildings::BT_WOODGATE1: + case Map::Buildings::BT_WOODGATE2: + case Map::Buildings::BT_TOWER1: + case Map::Buildings::BT_TOWER2: + case Map::Buildings::BT_TOWER3: + case Map::Buildings::BT_TOWER4: + case Map::Buildings::BT_TOWER5: + goto switchD_00528bae_caseD_2d; + } + goto switchD_00528b77_caseD_5; + } + if (unitInstructionType != (UnitInstructionType)0x26) { + switchD_00528bae_caseD_2d: + /* + attack buildings + */ + if (DAT_UnitsState::instance.units[iVar12].state.generic == (UnitState)0x69) { + DAT_UnitsState::instance.units[iVar12].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + sVar8 = (short)((int)(DAT_BuildingsState::instance.buildings[iVar11].widthOrHeight * 8) + / 2); + DAT_UnitsState::instance.units[iVar12].shootTargetMicroX + = DAT_BuildingsState::instance.buildings[iVar11].x * 8 + sVar8; + sVar9 = DAT_BuildingsState::instance.buildings[iVar11].terrainHeightUnk; + DAT_UnitsState::instance.units[iVar12].shootTargetMicroY + = DAT_BuildingsState::instance.buildings[iVar11].y * 8 + sVar8; + DAT_UnitsState::instance.units[iVar12].shootTargetZ = sVar9; + DAT_UnitsState::instance.units[iVar12].shootTargetedUnit = -1; + DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + LAB_00528c46: + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 9; + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_BUILDING; + DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + } + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } + if ((((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) + || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) + || (4 < (int)(short)DAT_BuildingsState::instance.buildings[iVar11].buildingType - 0x4aU)) + && (id | x | id != 0)) { + sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; + if (unitInstructionType == Map::Units::UIT_ATTACK_BUILDING + || unitInstructionType == (UnitInstructionType)0x26) { + iVar12 = (int)DAT_UnitsState::instance.units[sVar9].owner; + } else { + iVar12 = 0; + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingForAttacksUnk, + DAT_PathFindingState::ptr)(tribeID, iVar11, id, + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[sVar9].tile])), + iVar12); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( + tribeID, _ramOrHorse); + if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) + && (_unitTribeIndex = 0, 0 < this->tribes[_param_1_tribeID].size)) { + id = 0x12d5c70; + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0) { + UVar4 = DAT_UnitsState::instance.units[_unitID].unitType; + switch (UVar4) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_S_BATTERINGRAM: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + switchD_00528e3a_caseD_5: + if (UVar4 != Map::Units::UT_S_BATTERINGRAM) + goto LAB_00528f32; + if (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 4) + break; + if (bVar5) { + LAB_00528f32: + iVar12 = *(int*)(id + -4); + if (iVar12 == 0) + break; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar12]; + unitUID = iVar12 + - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + DAT_UnitsState::instance.units[_unitID].targetingType + = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)unitUID; + DAT_UnitsState::instance.units[_unitID].plannedDestinationY = sVar9; + if (DAT_UnitsState::instance.units[_unitID].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[_unitID].movementCooldown + = DAT_UnitsState::instance.units[_unitID].movementSpeed * -4; + } + DAT_UnitsState::instance.units[_unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + BVar16 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + if (!BVar16) { + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; + } else { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID, (uint)(unitUID), (uint)(sVar9), 0); + DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID = 0; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + uVar13 = *(uint*)id; + DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = uVar13; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[uVar13]; + DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar9; + sVar9 = *(short*)id + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9] + .addXgetTile; + id = id + 0xc; + } else { + iVar12 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findBuildingAccessPoint, + DAT_PathFindingState::ptr)(_unitID, iVar11, ¶m_5); + bVar5 = true; + if (iVar12 == 0) + goto LAB_00528f32; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar12]; + unitUID = iVar12 + - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + BVar16 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID, (uint)(unitUID), (uint)(sVar9), 0); + if (!BVar16) + goto LAB_00528f32; + DAT_UnitsState::instance.units[_unitID].targetingType + = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)unitUID; + DAT_UnitsState::instance.units[_unitID].plannedDestinationY = sVar9; + if (DAT_UnitsState::instance.units[_unitID].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[_unitID].movementCooldown + = DAT_UnitsState::instance.units[_unitID].movementSpeed * -4; + } + DAT_UnitsState::instance.units[_unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID = 0; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[param_5]; + DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = param_5; + DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar9; + sVar9 = (short)param_5 + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9] + .addXgetTile; + } + DAT_UnitsState::instance.units[_unitID].attackAtTileX = sVar9; + break; + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + if (unitInstructionType == (UnitInstructionType)0x24) + goto switchD_00528e3a_caseD_5; + } + } + if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { + return (undefined4)(1); + } + } while (true); + } + } + break; + case Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED: + this->tribes[tribeID].isRallyingUnk = 0; + _y = DAT_BuildingsState::instance.buildings[id].buildingEntryY; + _x = DAT_BuildingsState::instance.buildings[id].buildingEntryX; + _unitTribeIndex = 0; + BVar16 = MACRO_CALL_MEMBER( + Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)((int)_x, (uint)(_y)); + if (!BVar16) { + return (undefined4)(0); + } + param_5 = (int)(short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile + + (int)_x]; + unitUID = MACRO_CALL_MEMBER( + Map::Buildings::BuildingsState_Func::getRequiredEngineersCount, DAT_BuildingsState::ptr)(iVar11); + if (((unitUID | unitUID != 0) + || (DAT_BuildingsState::instance.buildings[iVar11].buildingType + == Map::Buildings::BT_OILSMELTER)) + && 0 < this->tribes[_param_1_tribeID].size) { + do { + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + iVar12 = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[uVar13].dying == 0 + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER + && (UVar3 = DAT_UnitsState::instance.units[uVar13].state.generic, + UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk) + && (UVar3 != ((UnitState)10))) + && UVar3 != Map::Units::States::US_SIT_DOWNUnk + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_RELOAD_WEAPONUnk) + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_AIM_WEAPONUnk) + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_FIRE_WEAPONUnk) + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_LOOK_AROUNDUnk)) { + DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; + DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; + cVar7 = MACRO_CALL_MEMBER( + Map::Buildings::BuildingsState_Func::resolveBuildingEntryAccessibility, + DAT_BuildingsState::ptr)(iVar11, 1, (int)(DAT_UnitsState::instance.units[uVar13].x), + (int)(DAT_UnitsState::instance.units[uVar13].y)); + if (((((uint)(extraout_var) & 0xffffff) << 8) | (uint)(byte)(cVar7)) != 0 + && (iVar14 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, + (dword)((int)(param_5)), + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile])), + 0), + iVar14 != 0)) { + sVar9 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryY; + sVar8 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX; + iVar14 = DAT_BuildingsState::instance.buildings[iVar11].uid; + DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].targetUID = iVar14; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(uVar13, (uint)(sVar8), (uint)(sVar9), 0); + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + == Map::Buildings::BT_OILSMELTER) { + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk; + DAT_UnitsState::instance.units[uVar13].workplaceBuildingID_1 + = _param_3_id_x_tile_buildingID_copy; + } else { + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_STAND_UPUnk; + DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( + uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + unitUID = unitUID + -1; + iVar12 = _unitTribeIndex; + if (unitUID == 0) { + return (undefined4)(1); + } + } + } + } + _unitTribeIndex = iVar12; + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case Map::Units::UIT_MAN_SIEGE_EQUIPMENT: + /* + manning siege equipment? + */ + this->tribes[tribeID].isRallyingUnk = 0; + if (DAT_UnitsState::instance.units[id].uid == unitUID + && (unitUID = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(id), + unitUID | unitUID != 0) + && (iVar12 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + do { + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar12); + iVar14 = iVar12 + 1; + if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[uVar13].dying == 0 + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER) { + DAT_UnitsState::instance.units[iVar11]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar11]._someY_2 = 0; + param_5 = (int)(short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile]; + unitInstructionType = ((UnitInstructionType)0); + do { + iVar14 = (int)DAT_UnitsState::instance.units[iVar11] + .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine; + x = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][0] + + (int)DAT_UnitsState::instance.units[iVar11].x; + y = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][1] + + (int)DAT_UnitsState::instance.units[iVar11].y; + uVar19 = (iVar14 + 1) % 16; + DAT_UnitsState::instance.units[iVar11] + .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine = (short)uVar19; + BVar16 = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(x, y); + if (BVar16) { + iVar14 = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + _param_4_unitUID_Y_copy + = (dword)(short)DAT_TileMapState::instance.PathConnectionLayer[iVar14]; + uVar19 = MACRO_CALL_MEMBER( + Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)(iVar14); + uVar19 = ((int)DAT_UnitsState::instance.units[iVar11].buildingHeight + + (int)DAT_UnitsState::instance.units[iVar11].terrainOrClimbHeight) + - uVar19; + uVar17 = (int)uVar19 >> 0x1f; + if ((int)((uVar19 ^ uVar17) - uVar17) < 0x11 + && (iVar14 + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)( + (int)DAT_UnitsState::instance.units[iVar11].owner, + (dword)((int)(param_5)), (dword)((int)(_param_4_unitUID_Y_copy)), 0), + iVar14 != 0)) + break; + } + unitInstructionType = (UnitInstructionType)(unitInstructionType + 1); + } while ((int)unitInstructionType < 0x10); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + uVar13, x, y, 0); + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_MAN_SIEGE_EQUIPMENT; + DAT_UnitsState::instance.units[uVar13].targetedUnitID__OR__engineerMannedSiegeEngineRef + = (short)id; + DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + unitUID = unitUID + -1; + iVar14 = iVar12; + if (unitUID == 0) { + return (undefined4)(1); + } + } + iVar12 = iVar14; + if (this->tribes[_param_1_tribeID].size <= iVar14) { + return (undefined4)(1); + } + } while (true); + } + break; + case Map::Units::UIT_EXIT_SIEGE_EQUIPMENT: + /* + disband siege engines? + */ + _param_4_unitUID_Y_copy = -(uint)(param_5 != 0) & 0x10; + param_5 = (int)SEC_RNG::instance.currentNumber2 & 0x8000000f; + if (param_5 < 0) { + param_5 = (param_5 - 1U | 0xfffffff0) + 1; + } + MACRO_CALL_MEMBER(Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + if (DAT_UnitsState::instance.units[iVar11].uid == unitUID) { + DAT_UnitsState::instance.units[iVar11].state.generic = Map::Units::States::US_FIRE_WEAPONUnk; + unitInstructionType = ((UnitInstructionType)0); + if (0 < DAT_UnitsState::instance.units[iVar11] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300) { + tribeID = iVar11 * 0x490 + 0x1388860; + do { + _unitID_0x11 = (int)*(short*)tribeID; + *(undefined2*)tribeID = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field249_0x3c0 = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field248_0x3bc = 0; + DAT_UnitsState::instance.units[_unitID_0x11].animationCycleNumber = 0; + DAT_UnitsState::instance.units[_unitID_0x11].state.generic + = Map::Units::States::US_JESTER_ROAM_TO; + DAT_UnitsState::instance.units[_unitID_0x11].disappearFadeAlphaCountdown = 0x20; + DAT_UnitsState::instance.units[_unitID_0x11].engineerManningSiegeStateRef_checkType = 0xfe; + DAT_UnitsState::instance.units[_unitID_0x11].cachedState + = (UnitStateShort)_param_4_unitUID_Y_copy; + UVar4 = DAT_UnitsState::instance.units[iVar11].unitType; + if ((UVar4 == Map::Units::UT_S_MANGONEL) || (UVar4 == Map::Units::UT_S_BALLISTA)) { + DAT_UnitsState::instance.units[_unitID_0x11].goToRallyPoint = 1; + } + DAT_UnitsState::instance.units[_unitID_0x11].updateTickTracker = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field39_0x58 = 1; + unitUID = 0; + LAB_00529da0: + do { + uVar19 = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[param_5][0] + + (int)DAT_UnitsState::instance.units[iVar11].x; + uVar13 = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[param_5][1] + + (int)DAT_UnitsState::instance.units[iVar11].y; + param_5 = param_5 + 1U & 0x8000000f; + if (param_5 < 0) { + param_5 = (param_5 - 1U | 0xfffffff0) + 1; + } + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findWalkableTileThatDoesNotContainUnit, + DAT_PathFindingState::ptr)(id, uVar19, uVar13, 1); + if (DAT_PathFindingState::instance.ALG_ResultTile == 0) { + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findWalkableTileThatDoesNotContainUnit, + DAT_PathFindingState::ptr)(id, uVar19, uVar13, 0); + sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; + sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; + iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; + if (DAT_PathFindingState::instance.ALG_ResultTile != 0) + break; + unitUID = unitUID + 1; + if (unitUID | unitUID != 0x10) + goto LAB_00529da0; + sVar9 = DAT_UnitsState::instance.units[iVar11].x; + sVar8 = DAT_UnitsState::instance.units[iVar11].y; + iVar12 = DAT_UnitsState::instance.units[iVar11].tile; + } else { + sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; + sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; + iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; + } + } while (iVar12 == 0); + bVar2 = DAT_TileMapState::instance.HeightLayer[iVar12]; + DAT_UnitsState::instance.units[_unitID_0x11].totalSizeOfPathPlan = 0; + DAT_UnitsState::instance.units[_unitID_0x11].x = sVar9; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentXPosition = sVar9; + DAT_UnitsState::instance.units[_unitID_0x11].y = sVar8; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentYPosition = sVar8; + DAT_UnitsState::instance.units[_unitID_0x11].terrainOrClimbHeight = (ushort)bVar2; + DAT_UnitsState::instance.units[_unitID_0x11].tile = iVar12; + DAT_UnitsState::instance.units[_unitID_0x11].nextTileUnk = iVar12; + DAT_UnitsState::instance.units[_unitID_0x11].microXPosition = sVar9 * 8 + 4; + DAT_UnitsState::instance.units[_unitID_0x11].microYPosition = sVar8 * 8 + 4; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::updateMicroPosition, DAT_UnitsState::ptr)( + _unitID_0x11); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::resetUnitMovementState, DAT_UnitsState::ptr)( + _unitID_0x11); + tribeID = tribeID + 2; + DAT_UnitsState::instance.units[_unitID_0x11].animationSheetFrameOffset = 1; + DAT_UnitsState::instance.units[_unitID_0x11].field_0x30_animRelated = 0x10; + unitInstructionType = (UnitInstructionType)(unitInstructionType + 1); + } while ((int)unitInstructionType < (int)DAT_UnitsState::instance.units[iVar11] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300); + } + DAT_UnitsState::instance.units[iVar11] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 = 0; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(id); + return (undefined4)(1); + } + break; + case Map::Units::UIT_THROW_OIL: + iVar11 = 0; + this->tribes[tribeID].isRallyingUnk = 0; + if (0 < _tribeSize) { + do { + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar11); + iVar11 = iVar11 + 1; + if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[uVar13].dying == 0 + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER) { + DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_THROW_OIL; + DAT_UnitsState::instance.units[uVar13].attackAtTileX = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].attackAtTileY = (short)unitUID; + DAT_UnitsState::instance.units[uVar13].plannedDestinationX + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].plannedDestinationY = (short)unitUID; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; + DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; + } + } while (iVar11 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case ((UnitInstructionType)0x15): + this->tribes[tribeID].isRallyingUnk = 0; + uVar13 = (uint)DAT_BuildingsState::instance.buildings[id].buildingEntryY; + _unitTribeIndex = 0; + BVar16 + = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)( + (int)DAT_BuildingsState::instance.buildings[id].buildingEntryX, uVar13); + if (!BVar16) { + return (undefined4)(0); + } + param_5 + = (int)(short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[uVar13].addXgetTile + + (int)DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX]; + if (0 < _tribeSize) { + do { + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[uVar13].dying == 0 + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_TUNNELER) { + DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; + DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; + iVar12 = MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::buildingIsAccessible, + DAT_BuildingsState::ptr)(iVar11, 1); + if (iVar12 != 0 + && (iVar12 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, + (dword)((int)(param_5)), + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile])), + 0), + iVar12 != 0)) { + sVar9 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryY; + sVar8 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX; + iVar11 = DAT_BuildingsState::instance.buildings[iVar11].uid; + DAT_UnitsState::instance.units[uVar13].workplaceBuildingID_1 + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].targetUID = iVar11; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(uVar13, (uint)(sVar8), (uint)(sVar9), 0); + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_LOOK_AROUNDUnk; + DAT_UnitsState::instance.units[uVar13].targetingType = ((UnitInstructionType)0x15); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( + uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + return (undefined4)(1); + } + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } + DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + iVar11); + break; + case Map::Units::UIT_ATTACK_WALL: + case ((UnitInstructionType)0x23): + case ((UnitInstructionType)0x25): + _unitTribeIndex = 0; + id = 0; + this->tribes[tribeID].isRallyingUnk = 0; + if (0 < _tribeSize) { + do { + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[uVar13].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[uVar13].dying != 0 + || DAT_UnitsState::instance.units[uVar13].field303_0x413 != 0) + continue; + DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; + DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; + switch (DAT_UnitsState::instance.units[uVar13].unitType) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_FIRETHROWER: + goto switchD_005290c7_caseD_16; + case Map::Units::UT_E_KNIGHT: + switchD_005290c7_caseD_1c: + _ramOrHorse = _ramOrHorse + 1; + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_SWORDSMAN: + switchD_005290c7_caseD_5: + id = id + 1; + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + if (unitInstructionType != (UnitInstructionType)0x25) { + sVar9 = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[iVar11]; + DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[uVar13].attackAtTileY = sVar9; + DAT_UnitsState::instance.units[uVar13].attackAtTileX + = _param_3_id_x_tile_buildingID_copy + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + DAT_UnitsState::instance.units[uVar13].field253_0x3c5 = 0x17; + DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_ATTACK_WALL; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(uVar13); + DAT_UnitsState::instance.units[uVar13].shootBeforeStop = 10; + } + break; + case Map::Units::UT_S_TOWER: + uVar19 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::findFreeTileNearby, + DAT_UnitsState::ptr)(uVar13, (uint)(iVar11)); + if (uVar19 != 0) { + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(uVar13, + (uint)(uVar19 + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[uVar19]] + .addXgetTile), + (uint)(DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[uVar19]), + 0); + sVar9 = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[iVar11]; + DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_ATTACK_WALL; + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].targetedBuildingTile = iVar11; + DAT_UnitsState::instance.units[uVar13].digTileTarget = uVar19; + DAT_UnitsState::instance.units[uVar13].attackAtTileY = sVar9; + DAT_UnitsState::instance.units[uVar13].attackAtTileX + = _param_3_id_x_tile_buildingID_copy + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + } + break; + case Map::Units::UT_S_BATTERINGRAM: + if (DAT_UnitsState::instance.units[uVar13] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4) { + local_8 = local_8 + 1; + goto switchD_005290c7_caseD_1c; + } + break; + case Map::Units::UT_A_HARCHER: + _ramOrHorse = _ramOrHorse + 1; + switchD_005290c7_caseD_16: + if (unitInstructionType == (UnitInstructionType)0x23) + goto switchD_005290c7_caseD_5; + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } + if ((DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) + && (DAT_GameState::instance.mapAndTime.skirmishStrongWalls != 0)) { + id = local_8; + } + if (id | x | id != 0) { + sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; + if (unitInstructionType == Map::Units::UIT_ATTACK_WALL + || unitInstructionType == (UnitInstructionType)0x25) { + sVar8 = DAT_UnitsState::instance.units[sVar9].facingDirection; + iVar12 = (int)DAT_UnitsState::instance.units[sVar9].owner; + } else { + sVar8 = DAT_UnitsState::instance.units[sVar9].facingDirection; + iVar12 = 0; + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findFreeSpaceNextToEnemyDefensiveStructureInSameAreaWithinDistance, + DAT_PathFindingState::ptr)(iVar11, id, + (int*)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[sVar9].tile])), + iVar12, 0, (int)(sVar8)); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( + tribeID, _ramOrHorse); + if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) + && (iVar12 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + id = 0x12d5c70; + do { + iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar12); + iVar12 = iVar12 + 1; + if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar14].dying == 0 + && DAT_UnitsState::instance.units[iVar14].field303_0x413 == 0) { + UVar4 = DAT_UnitsState::instance.units[iVar14].unitType; + switch (UVar4) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_SWORDSMAN: + switchD_0052932a_caseD_5: + if ((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) + || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) + goto switchD_0052932a_caseD_3b; + break; + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + if (unitInstructionType == (UnitInstructionType)0x23) + goto switchD_0052932a_caseD_5; + break; + case Map::Units::UT_S_BATTERINGRAM: + switchD_0052932a_caseD_3b: + if (UVar4 == Map::Units::UT_S_BATTERINGRAM) { + if (DAT_UnitsState::instance.units[iVar14] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 4) + break; + if (!bVar5) { + bVar5 = true; + iVar15 + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findUnitPreferredOrientationBasedConnectedTile, + DAT_PathFindingState::ptr)(iVar14, iVar11); + if (iVar15 != 0) { + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar15]; + unitUID = iVar15 + - DAT_ViewportRenderState::instance.translationMatrix[sVar9] + .addXgetTile; + BVar16 = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(iVar14, (uint)(unitUID), (uint)(sVar9), 0); + if (BVar16) { + DAT_UnitsState::instance.units[iVar14].targetingType + = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[iVar14].plannedDestinationX + = (short)unitUID; + DAT_UnitsState::instance.units[iVar14].plannedDestinationY = sVar9; + if (DAT_UnitsState::instance.units[iVar14].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[iVar14].movementCooldown + = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; + } + DAT_UnitsState::instance.units[iVar14].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + = 0; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar11]; + DAT_UnitsState::instance.units[iVar14].targetedBuildingTile + = iVar11; + DAT_UnitsState::instance.units[iVar14].attackAtTileY = sVar9; + DAT_UnitsState::instance.units[iVar14].attackAtTileX + = _param_3_id_x_tile_buildingID_copy + - (short)DAT_ViewportRenderState::instance + .translationMatrix[sVar9] + .addXgetTile; + break; + } + } + } + } + iVar15 = *(int*)(id + -4); + if (iVar15 != 0) { + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar15]; + uVar13 = iVar15 + - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + DAT_UnitsState::instance.units[iVar14].targetingType = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[iVar14].plannedDestinationX = (short)uVar13; + DAT_UnitsState::instance.units[iVar14].plannedDestinationY = sVar9; + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; + if (DAT_UnitsState::instance.units[iVar14].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[iVar14].movementCooldown + = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; + } + DAT_UnitsState::instance.units[iVar14].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(iVar14, uVar13, (uint)(sVar9), 0); + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + uVar13 = *(uint*)id; + sVar9 = *(short*)id; + DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID = 0; + DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = uVar13; + sVar8 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[uVar13]; + DAT_UnitsState::instance.units[iVar14].attackAtTileY = sVar8; + id = id + 0xc; + DAT_UnitsState::instance.units[iVar14].attackAtTileX = sVar9 + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar8] + .addXgetTile; + } + } + } + if (this->tribes[_param_1_tribeID].size <= iVar12) { + return (undefined4)(1); + } + } while (true); + } + } + break; + case Map::Units::UIT_SET_LADDER: + iVar12 = 0; + id = 0; + this->tribes[tribeID].isRallyingUnk = 0; + if (0 < _tribeSize) { + do { + iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar12); + iVar12 = iVar12 + 1; + if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar14].dying == 0 + && DAT_UnitsState::instance.units[iVar14].unitType == Map::Units::UT_E_LADDER) { + id = id + 1; + DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; + } + } while (iVar12 < this->tribes[_param_1_tribeID].size); + if (id | x | id != 0) { + sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + populateDestinationsArrayWithWallTileAndFreeTilePairInSameArea, + DAT_PathFindingState::ptr)(iVar11, id, + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[sVar9].tile])), + (int)(DAT_UnitsState::instance.units[sVar9].owner)); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( + tribeID, 0); + if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) + && (iVar11 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + piVar18 = &DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper; + do { + _laddermanID = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar11); + iVar11 = iVar11 + 1; + if (DAT_UnitsState::instance.units[_laddermanID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_laddermanID].dying == 0 + && DAT_UnitsState::instance.units[_laddermanID].unitType == Map::Units::UT_E_LADDER + && (_freeTile = ((PathHelper12*)(piVar18 + -1))->tile1, _freeTile != 0)) { + /* + set ladder men destination to free id next to defensive structure + */ + _freeTileY + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_freeTile]; + _freeTileX = _freeTile + - DAT_ViewportRenderState::instance.translationMatrix[_freeTileY].addXgetTile; + DAT_UnitsState::instance.units[_laddermanID].targetingType + = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[_laddermanID].plannedDestinationX + = (short)_freeTileX; + DAT_UnitsState::instance.units[_laddermanID].plannedDestinationY = _freeTileY; + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; + if (DAT_UnitsState::instance.units[_laddermanID].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[_laddermanID].movementCooldown + = DAT_UnitsState::instance.units[_laddermanID].movementSpeed * -4; + } + DAT_UnitsState::instance.units[_laddermanID].state.generic + = (ushort)(*piVar18 != 0) * 2 + Map::Units::States::US_MOVE_TO_DESTINATION; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_laddermanID, _freeTileX, (uint)(_freeTileY), 0); + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + _defensiveTile1 = *piVar18; + _defensiveTile2 = *piVar18; + /* + set ladder man ladder target id + */ + DAT_UnitsState::instance.units[_laddermanID].targetedBuildingTile = _defensiveTile1; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_defensiveTile1]; + DAT_UnitsState::instance.units[_laddermanID].attackAtTileY = sVar9; + piVar18 = piVar18 + 3; + DAT_UnitsState::instance.units[_laddermanID].attackAtTileX = (short)_defensiveTile2 + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + } + } while (iVar11 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + } + } + break; + case Map::Units::UT_E_MACE: + iVar11 = 0; + if (0 < _tribeSize) { + do { + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _param_1_tribeID, iVar11); + iVar11 = iVar11 + 1; + if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar12].dying == 0 + && (int)(short)DAT_UnitsState::instance.units[iVar12].unitType - 0x27U < 2) { + DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 1; + } + } while (iVar11 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case Map::Units::UIT_DISBAND: + /* + disband? + */ + this->tribes[tribeID].isRallyingUnk = 0; + _unitTribeIndex = 0; + if (0 < _tribeSize) { + do { + _unitID_0x1e = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _param_1_tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if ((DAT_UnitsState::instance.units[_unitID_0x1e].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID_0x1e].dying == 0)) { + switch (DAT_UnitsState::instance.units[_unitID_0x1e].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_LADDER: + case Map::Units::UT_E_ENGINEER: + case Map::Units::UT_E_MONK: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_SWORDSMAN: + case Map::Units::UT_A_FIRETHROWER: + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::disbandUnit, DAT_UnitsState::ptr)( + _unitID_0x1e); + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_TOWER: + case Map::Units::UT_S_BATTERINGRAM: + case Map::Units::UT_S_SHIELD: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + _param_1_tribeID, Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID_0x1e, + DAT_UnitsState::instance.units[_unitID_0x1e].uid, 1); + _gamemodeSiegeThat = DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT; + DAT_UnitsState::instance.units[_unitID_0x1e].state.generic + = Map::Units::States::US_DISAPPEAR; + if (_gamemodeSiegeThat) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::getPriceForDisbandedUnitType, + DAT_BuildingsState::ptr)( + (Map::Units::UnitType)(DAT_UnitsState::instance.units[_unitID_0x1e].unitType), + &tribeID); + piVar18 = DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .startResources + + 0xf; + *piVar18 = *piVar18 + tribeID; + } + } + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectAllUnitsOneByOne, DAT_UnitsState::ptr)(); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)( + this->tribes[_param_1_tribeID].owner, 0, 0); + return (undefined4)(1); + case Map::Units::UIT_STOP: + iVar11 = 0; + this->tribes[tribeID].isRallyingUnk = 0; + this->tribes[tribeID].someUnitID = 0; + if (0 < _tribeSize) { + do { + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar11); + iVar11 = iVar11 + 1; + if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar12].dying == 0 + && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0) { + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 3; + DAT_UnitsState::instance.units[iVar12].targetingType + = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; + DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].targetedBuildingTile = 0; + DAT_UnitsState::instance.units[iVar12].movementType_OR_targetUnitID = 0; + } + } while (iVar11 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case ((UnitInstructionType)0x20): + case Map::Units::UIT_SHOOT_TARGETUnk: + local_8 = id * 0x490; + pUVar6 = DAT_UnitsState::instance.units + id; + _unitTribeIndex = 0; + id = 0; + if (pUVar6->uid != unitUID) { + return (undefined4)(0); + } + if (0 < _tribeSize) { + do { + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar12].dying == 0 + && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0 + && (UVar3 = DAT_UnitsState::instance.units[iVar12].state.generic, + UVar3 != Map::Units::States::US_MELEE_ATTACK)) { + switch (DAT_UnitsState::instance.units[iVar12].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { + id = id + 1; + } + break; + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_E_ARCHER_DEBUG: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + switchD_005282f6_caseD_16: + if (UVar3 == (UnitState)0x69) { + DAT_UnitsState::instance.units[iVar12].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[iVar12].targetedUnitID__OR__engineerMannedSiegeEngineRef + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { + this->tribes[_param_1_tribeID].containsRabbit = 1; + } + if (DAT_UnitsState::instance.units[iVar12]._someX_2 == 0) { + sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; + DAT_UnitsState::instance.units[iVar12]._someX_2 + = DAT_UnitsState::instance.units[iVar12].destinationX_2Unk; + DAT_UnitsState::instance.units[iVar12]._someY_2 = sVar9; + } + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { + sVar9 = DAT_UnitsState::instance.units[iVar12]._someX_2; + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 5; + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_LAND; + DAT_UnitsState::instance.units[iVar12].attackAtTileX + = DAT_UnitsState::instance.units[iVar11].x; + DAT_UnitsState::instance.units[iVar12].attackAtTileY + = DAT_UnitsState::instance.units[iVar11].y; + DAT_UnitsState::instance.units[iVar12].unkAttackRelated = 0xb; + if (sVar9 == 0) { + sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; + DAT_UnitsState::instance.units[iVar12]._someX_2 + = DAT_UnitsState::instance.units[iVar12].destinationX_2Unk; + DAT_UnitsState::instance.units[iVar12]._someY_2 = sVar9; + } + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + } + break; + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + goto switchD_005282f6_caseD_16; + } + } + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + if (id | x | id != 0) { + iVar12 = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; + param_5 = (int)DAT_UnitsState::instance.units[iVar12].x; + unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[iVar12].y; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( + iVar12, iVar11, ¶m_5, (int*)&unitInstructionType); + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathPlanningForTribe, + DAT_PathFindingState::ptr)(tribeID, (undefined4)((int)(iVar11)), (uint)(param_5), + (uint)(unitInstructionType), id, + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[iVar12].tile])), + (int)(DAT_UnitsState::instance.units[iVar12].owner)); + this->tribes[_param_1_tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; + this->tribes[_param_1_tribeID].someUnitUID = unitUID; + this->tribes[_param_1_tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; + iVar12 = 0; + sVar9 = this->tribes[_param_1_tribeID].size; + this->tribes[_param_1_tribeID].someTile2 + = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] + .addXgetTile; + unitInstructionType = ((UnitInstructionType)0); + if (0 < sVar9) { + id = 0x12d5c6c; + do { + iVar14 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); + iVar12 = iVar12 + 1; + if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar14].dying == 0 + && DAT_UnitsState::instance.units[iVar14].field303_0x413 == 0 + && DAT_UnitsState::instance.units[iVar14].state.generic + != Map::Units::States::US_MELEE_ATTACK) { + switch (DAT_UnitsState::instance.units[iVar14].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + _tile1_528565 = *(int*)id; + if (_tile1_528565 != 0) { + _y_528565 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_tile1_528565]; + _x_528565 = _tile1_528565 + - DAT_ViewportRenderState::instance.translationMatrix[_y_528565] + .addXgetTile; + if (*(int*)(id + 4) == 0) { + DAT_UnitsState::instance.units[iVar14].targetingType + = ((UnitInstructionType)0); + if (DAT_UnitsState::instance.units[iVar14].unitType + == Map::Units::UT_LORD) + break; + } else { + DAT_UnitsState::instance.units[iVar14].targetingType + = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[iVar14] + .targetedUnitID__OR__engineerMannedSiegeEngineRef + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar14] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + } + DAT_UnitsState::instance.units[iVar14].plannedDestinationX + = (short)_x_528565; + DAT_UnitsState::instance.units[iVar14].plannedDestinationY = _y_528565; + DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; + if (DAT_UnitsState::instance.units[iVar14].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[iVar14].movementCooldown + = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; + } + if (DAT_UnitsState::instance.units[iVar14]._someX_2 == 0) { + DAT_UnitsState::instance.units[iVar14]._someX_2 + = DAT_UnitsState::instance.units[iVar14].destinationX_2Unk; + DAT_UnitsState::instance.units[iVar14]._someY_2 + = DAT_UnitsState::instance.units[iVar14].destinationY_2Unk; + } + DAT_UnitsState::instance.units[iVar14].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + _isAllAssassins_528565 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + if (_isAllAssassins_528565 == 0) { + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; + } else { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(iVar14, _x_528565, (uint)(_y_528565), 0); + DAT_UnitsState::instance.units[iVar14].moveDelay = 0; + DAT_UnitsState::instance.units[iVar14].lookForEnemy = -0x32; + if (DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + != iVar11) { + DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + = _param_3_id_x_tile_buildingID_copy; + psVar1 = (short*)((int)&DAT_UnitsState::instance.units[0].huntedBy + + local_8); + *psVar1 = *psVar1 + 1; + } + UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[iVar14] + .totalSizeOfPathPlan + / 2); + if ((int)unitInstructionType < (int)UVar10) { + unitInstructionType = UVar10; + } + id = id + 0xc; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + } + } + } + } while (iVar12 < this->tribes[_param_1_tribeID].size); + } + iVar11 = unitInstructionType * 8; + if (iVar11 < 9) { + iVar11 = 8; + } + this->tribes[_param_1_tribeID].field166_0x2be = (short)iVar11; + return (undefined4)(1); + } + } + break; + case Map::Units::UIT_LIGHT_PITCH: + this->tribes[tribeID].isRallyingUnk = 0; + _unitTribeIndex = 0; + if ((DAT_TileMapState::instance.pitchDitches[id].uid == unitUID) && (0 < _tribeSize)) { + do { + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if ((DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar12].dying == 0)) { + DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; + UVar4 = DAT_UnitsState::instance.units[iVar12].unitType; + if ((UVar4 == Map::Units::UT_E_ARCHER) || (UVar4 == Map::Units::UT_A_ARCHER)) { + sVar9 = DAT_TileMapState::instance.pitchDitches[iVar11].y; + DAT_UnitsState::instance.units[iVar12].shootTargetMicroX + = DAT_TileMapState::instance.pitchDitches[iVar11].x * 8 + 4; + iVar14 = DAT_TileMapState::instance.pitchDitches[iVar11].tile; + DAT_UnitsState::instance.units[iVar12].shootTargetMicroY = sVar9 * 8 + 4; + DAT_UnitsState::instance.units[iVar12].shootTargetZ + = (ushort)DAT_TileMapState::instance.HeightLayer[iVar14]; + DAT_UnitsState::instance.units[iVar12].shootTargetedUnit = -1; + DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 0x22; + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_LIGHT_PITCH; + DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + } + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + } + return (undefined4)(1); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp new file mode 100644 index 0000000000..856823a268 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -0,0 +1,550 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Synchrony/GameSynchronyState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005263A0 + undefined4 TribesState::giveTribeMoveInstruction( + int tribeID, uint x1, uint y1, int rallyBool, int storeAsRallyPoint, UnitMatchSpeedEnum speedMatching) + { + UnitStateShort UVar1; + UnitTypeShort UVar2; + int _firstUnitID; + int _navArea; + uint _one; + uint _one2; + uint _someUnitCanClimb; + dword _area3; + int iVar7; + int _hasUnitInArea; + BOOLEnum BVar8; + int _unitID; + dword dVar9; + int _unitID_2; + int _modulo; + int _area_2; + UnitTypeShort _unitType; + int _targetUnitArea; + uint _y = y1; + int _bool1 = 0; + int _tribeUnitIndex = 0; + int bVar6 = 0; + int _algTile2 = 0; + int _algTile = 0; + int bVar4 = 0; + if (!x1) { + if (!y1) { + return (undefined4)(0); + } + } else { + if ((int)x1 < 0) { + return (undefined4)(0); + } + if (399 < (int)x1) { + return (undefined4)(0); + } + } + if ((399 < y1) || (*(char*)(y1 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x1) == '\0')) { + return (undefined4)(0); + } + int _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; + int _targetUnitX = (int)DAT_UnitsState::instance.units[_targetUnitID].x; + int _targetUnitY = (int)DAT_UnitsState::instance.units[_targetUnitID].y; + _targetUnitArea + = (short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[_targetUnitY].addXgetTile + + _targetUnitX]; + int _tile = DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile + x1; + int _area = (int)(short)DAT_TileMapState::instance.PathConnectionLayer[_tile]; + if (!(DAT_TileMapState::instance.LogicLayer[_tile] & 0x30)) { + DAT_PathFindingState::instance.field50_0x98 = 0; + this->fcn_mtribe = this->fcn_mtribe + 1; + _firstUnitID = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getFirstUnitInTribeThatIsOnXTerrain, this)(tribeID); + if (_targetUnitID == _firstUnitID) { + _navArea = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, + DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, (dword)((int)(_area)), + (uint)(DAT_UnitsState::instance.units[_targetUnitID].x), + (uint)(DAT_UnitsState::instance.units[_targetUnitID].y)); + if (!_navArea) { + _one = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, + this)(tribeID, (undefined4)((int)(x1)), (undefined4)((int)(y1)), + (undefined4)((int)(rallyBool)), storeAsRallyPoint); + return (undefined4)(_one); + } + _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; + _bool1 = 1; + _area_2 = _navArea; + } else { + _area_2 = _targetUnitArea; + if (!_targetUnitArea) { + _one2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, + this)(tribeID, (undefined4)((int)(x1)), (undefined4)((int)(y1)), + (undefined4)((int)(rallyBool)), storeAsRallyPoint); + return (undefined4)(_one2); + } + } + /* + WARNING: y1 now is area2 + */ + y1 = _area_2; + if (0 < _area) { + int _firstUnit + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + if (!_firstUnit) { + if (_area != y1) { + _someUnitCanClimb = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::tribeContainsUnitThatCanClimb, this)(tribeID); + _area3 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, (dword)((int)(y1)), + (dword)((int)(_area)), (int)(_someUnitCanClimb)); + if (!_area3) { + iVar7 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::someBinaryAlgFunctionPathFinding, + DAT_PathFindingState::ptr)(_targetUnitID); + if (!iVar7) { + return (undefined4)(0); + } + iVar7 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findCrossAreaBridgeTileToTarget, + DAT_PathFindingState::ptr)(_targetUnitID, x1, _y); + if (!iVar7) { + return (undefined4)(0); + } + bVar6 = 1; + _algTile2 = DAT_PathFindingState::instance.ALG_TargetTile; + _algTile = DAT_PathFindingState::instance.ALG_ResultTile; + } else { + iVar7 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::setClimbBasedOnClosestClimbData, + DAT_PathFindingState::ptr)(_targetUnitID, (int)(y1), (int)(_area3)); + if (!iVar7) { + return (undefined4)(0); + } + } + } + } else { + bVar4 = 1; + } + if (storeAsRallyPoint) { + this->tribes[tribeID].isRallyingUnk = (short)rallyBool; + this->tribes[tribeID].rallyPointArray[0][0] = DAT_UnitsState::instance.units[_targetUnitID].x; + this->tribes[tribeID].rallyPointArray[0][1] = DAT_UnitsState::instance.units[_targetUnitID].y; + this->tribes[tribeID].rallyPointArray[1][0] = (short)x1; + this->tribes[tribeID].rallyPointArray[1][1] = (short)_y; + this->tribes[tribeID].currentRallyPointIndex = 1; + this->tribes[tribeID].rallyPointCount = 2; + this->tribes[tribeID].containsRabbit = 0; + } + this->ALG_ResultTileIndex = 0; + this->field6_0x18 = 0; + int _isWallGatehouseTower = (DAT_TileMapState::instance.LogicLayer[_tile] & 0x100U) != 0; + int bVar5 = 0; + int _isKeep = (DAT_TileMapState::instance.LogicLayer[_tile] & 0x10000000U) != 0; + int _isDefensiveStructure = _isKeep || _isWallGatehouseTower; + rallyBool = 1; + if (bVar4) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, + DAT_PathFindingState::ptr)(x1, _y, 0xffffffff, 0xffffffff, 500, 0); + } else if (_bool1) { + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findPathUsingClimbingWithHeightMargin16, + DAT_PathFindingState::ptr)(x1, _y, 0xffffffff, 0xffffffff, 500, FALSE); + } else { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(x1, _y, -1, -1, 500, FALSE); + } + _hasUnitInArea = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg, this)( + tribeID, DAT_PathFindingState::instance.searchGeneration); + if (!_hasUnitInArea) { + int _searchBudget = 500; + do { + _searchBudget = _searchBudget + 500; + if (0x13a10 <= _searchBudget + || ((_bool1 || (_isDefensiveStructure)) && (8000 <= _searchBudget))) { + if (!DAT_PathFindingState::instance.field50_0x98) { + if (bVar4) { + BVar8 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, + DAT_PathFindingState::ptr)( + x1, _y, (uint)(_targetUnitX), (uint)(_targetUnitY), 100000, 0); + if (!BVar8) { + return (undefined4)(0); + } + } else if (_bool1) { + BVar8 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findPathUsingClimbingWithHeightMargin16, + DAT_PathFindingState::ptr)( + x1, _y, (uint)(_targetUnitX), (uint)(_targetUnitY), 100000, FALSE); + if (!BVar8) { + return (undefined4)(0); + } + } else { + BVar8 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)( + x1, _y, _targetUnitX, _targetUnitY, 100000, FALSE); + if (!BVar8) { + return (undefined4)(0); + } + } + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func:: + applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe, + this)(tribeID); + goto LAB_00526788; + } + rallyBool = DAT_PathFindingState::instance.searchQueue.currentDistance; + if (bVar4) { + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, + DAT_PathFindingState::ptr)(x1, _y, 0xffffffff, 0xffffffff, _searchBudget, 1); + } else if (_bool1) { + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findPathUsingClimbingWithHeightMargin16, + DAT_PathFindingState::ptr)(x1, _y, 200, 0, _searchBudget, TRUE); + } else { + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(x1, _y, -1, -1, _searchBudget, TRUE); + } + BVar8 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg, this)( + tribeID, DAT_PathFindingState::instance.searchGeneration); + } while (!BVar8); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMovementDistanceToUnitsInTribe, this)(tribeID); + LAB_00526788: + if (0 < this->tribes[tribeID].size) { + do { + /* + plan x and y for each unit in selection + */ + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _tribeUnitIndex); + _tribeUnitIndex = _tribeUnitIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID].state.generic != (UnitStateShort)0xcf + && DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag == 0 + && DAT_UnitsState::instance.units[_unitID].moveRelatedFlag != 1 + && DAT_UnitsState::instance.units[_unitID].moveableUnk != 0 + && (_unitType = DAT_UnitsState::instance.units[_unitID].unitType, + _unitType != Map::Units::UT_S_TREBUCHET) + && _unitType != Map::Units::UT_S_MANGONEL && _unitType != Map::Units::UT_S_BALLISTA + && (_unitType != Map::Units::UT_S_CATAPULT + || (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2)) + && (_unitType != Map::Units::UT_S_FBALLISTA + || (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2)) + && (_unitType != Map::Units::UT_S_TOWER + || (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) + && (_unitType != Map::Units::UT_S_BATTERINGRAM + || (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) + && (_unitType != Map::Units::UT_S_SHIELD + || (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 1))) { + if (_unitType == Map::Units::UT_E_ENGINEER) { + DAT_UnitsState::instance.units[_unitID].field252_0x3c4 = 0; + } + if (bVar4) { + if (!_isKeep && !_isWallGatehouseTower) { + if (bVar5) { + _modulo = 3; + } else { + /* + fixme:ucp:feature?: change formations + */ + dVar9 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func:: + findNextTileByModuloAmongPreviousSearchNotOnDefensiveStructure, + DAT_PathFindingState::ptr)(3, (int)(x1), (int)(_y)); + this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; + if (dVar9) + goto LAB_0052691a; + bVar5 = 1; + this->ALG_ResultTileIndex = 0; + _modulo = 3; + } + goto LAB_0052690c; + } + if (bVar5) { + LAB_00526908: + _modulo = 1; + goto LAB_0052690c; + } + dVar9 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::pathFindingResultBased, + DAT_PathFindingState::ptr)(1, (int)(x1), (int)(_y), rallyBool); + this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; + if (!dVar9) { + bVar5 = 1; + this->ALG_ResultTileIndex = 0; + goto LAB_00526908; + } + } else { + if (this->tribes[tribeID].unkIsAnimalTribe == 0) { + if ((_isDefensiveStructure) || (_bool1)) + goto LAB_00526908; + if (_unitType == Map::Units::UT_S_CATAPULT + || _unitType == Map::Units::UT_S_FBALLISTA + || _unitType == Map::Units::UT_S_TOWER + || _unitType == Map::Units::UT_S_BATTERINGRAM + || _unitType == Map::Units::UT_S_SHIELD) { + _modulo = 4; + } else { + _modulo = 2; + } + } else { + _modulo = 3; + } + LAB_0052690c: + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(_modulo, (int)(x1), (int)(_y)); + this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; + } + LAB_0052691a: + if (3999 < this->ALG_ResultTileIndex) + break; + DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)this->ALG_ResultX; + DAT_UnitsState::instance.units[_unitID].plannedDestinationY = (short)this->ALG_ResultY; + } + } while (_tribeUnitIndex < this->tribes[tribeID].size); + } + int _tribeIndex_2 = 0; + if (0 < this->tribes[tribeID].size) { + do { + _unitID_2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _tribeIndex_2); + _tribeIndex_2 = _tribeIndex_2 + 1; + if (DAT_UnitsState::instance.units[_unitID_2].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID_2].dying == 0 + && DAT_UnitsState::instance.units[_unitID_2].usingTeleport == 0 + && DAT_UnitsState::instance.units[_unitID_2].field303_0x413 == 0 + && (UVar1 = DAT_UnitsState::instance.units[_unitID_2].state.generic, + UVar1 != ((UnitStateShort)0xcf)) + && DAT_UnitsState::instance.units[_unitID_2].rallyRelatedFlag == 0 + && DAT_UnitsState::instance.units[_unitID_2].moveRelatedFlag != 1 + && DAT_UnitsState::instance.units[_unitID_2].moveableUnk != 0 + && (UVar2 = DAT_UnitsState::instance.units[_unitID_2].unitType, + UVar2 != Map::Units::UT_S_TREBUCHET) + && UVar2 != Map::Units::UT_S_MANGONEL && UVar2 != Map::Units::UT_S_BALLISTA + && (UVar2 != Map::Units::UT_S_CATAPULT + || (DAT_UnitsState::instance.units[_unitID_2] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2)) + && (UVar2 != Map::Units::UT_S_FBALLISTA + || (DAT_UnitsState::instance.units[_unitID_2] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2)) + && (UVar2 != Map::Units::UT_S_TOWER + || (DAT_UnitsState::instance.units[_unitID_2] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) + && (UVar2 != Map::Units::UT_S_BATTERINGRAM + || (DAT_UnitsState::instance.units[_unitID_2] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) + && (UVar2 != Map::Units::UT_S_SHIELD + || (DAT_UnitsState::instance.units[_unitID_2] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 1))) { + if (UVar1 == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[_unitID_2].movementCooldown + = DAT_UnitsState::instance.units[_unitID_2].movementSpeed * -4; + } + DAT_UnitsState::instance.units[_unitID_2].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[_unitID_2].movementType_OR_targetUnitID = 0; + DAT_UnitsState::instance.units[_unitID_2].targetedBuildingTile = 0; + if (bVar4) { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } else if (_bool1) { + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + } else { + DAT_PathFindingState::instance.notAllAssassinsUnk + = (int)(!DAT_EntityState::instance.fireCount); + } + DAT_UnitsState::instance.units[_unitID_2]._someX_2 = 0; + DAT_UnitsState::instance.units[_unitID_2]._someY_2 = 0; + MACRO_CALL_MEMBER(Synchrony::GameSynchronyState_Func::throttledMultiplayerSyncUpdate, + DAT_GameSynchronyState::ptr)(); + if (UVar1 == Map::Units::States::US_MELEE_ATTACK + || DAT_UnitsState::instance.units[_unitID_2].plannedDestinationX + != DAT_UnitsState::instance.units[_unitID_2].destinationXPosition + || DAT_UnitsState::instance.units[_unitID_2].plannedDestinationY + != DAT_UnitsState::instance.units[_unitID_2].destinationYPosition + || DAT_UnitsState::instance.units[_unitID_2].tunnelerFinishedDigging != 2) { + BVar8 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID_2, + (uint)(( + int)((int)DAT_UnitsState::instance.units[_unitID_2].plannedDestinationX)), + (uint)(( + int)((int)DAT_UnitsState::instance.units[_unitID_2].plannedDestinationY)), + 0); + if (!BVar8) { + if (bVar6 + && (DAT_TileMapState::instance + .LogicLayer[DAT_UnitsState::instance.units[_unitID_2].tile] + & 0x10000100U)) { + /* + teleport? + */ + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID_2, + (uint)(_algTile2 + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_algTile2]] + .addXgetTile), + (uint)(DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_algTile2]), + 0); + DAT_UnitsState::instance.units[_unitID_2].state.generic + = Map::Units::States::US_APPEAR; + DAT_UnitsState::instance.units[_unitID_2] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = _algTile; + goto LAB_00526ce4; + } + } else { + LAB_00526ce4: + if (speedMatching != Map::Units::Instructions::UMSE_0) + goto LAB_00526cea; + } + } else { + LAB_00526cea: + DAT_UnitsState::instance.units[_unitID_2].unitSpeedMatchingRelatedUnk = 0x10; + } + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + if (!DAT_PathFindingState::instance.field43_0x7c) { + this->field6_0x18 = this->field6_0x18 + 1; + } else { + this->field6_0x18 = this->field6_0x18 + -1; + } + } + } while (_tribeIndex_2 < this->tribes[tribeID].size); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyUnitTopSpeedDelayBasedOnTribeSize, this)( + tribeID, FALSE); + int _tribeIndex_4 = 0; + short _tribeSize = this->tribes[tribeID].size; + this->tribes[tribeID].someUnitID = 0; + if (0 < _tribeSize) { + do { + /* + set move delay + */ + _targetUnitID = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, _tribeIndex_4); + _tribeIndex_4 = _tribeIndex_4 + 1; + if (DAT_UnitsState::instance.units[_targetUnitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_targetUnitID].dying == 0 + && DAT_UnitsState::instance.units[_targetUnitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[_targetUnitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_targetUnitID].state.generic != (UnitStateShort)0xcf + && DAT_UnitsState::instance.units[_targetUnitID].rallyRelatedFlag == 0 + && DAT_UnitsState::instance.units[_targetUnitID].moveRelatedFlag != 1 + && DAT_UnitsState::instance.units[_targetUnitID].moveableUnk != 0 + && (UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType, + UVar2 != Map::Units::UT_S_TREBUCHET + && (( + ((UVar2 != Map::Units::UT_S_MANGONEL + && (UVar2 != Map::Units::UT_S_BALLISTA)) + && ((UVar2 != Map::Units::UT_S_CATAPULT + || (DAT_UnitsState::instance.units[_targetUnitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2)))) + && ((UVar2 != Map::Units::UT_S_FBALLISTA + || (DAT_UnitsState::instance.units[_targetUnitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2)))))) + && (UVar2 != Map::Units::UT_S_TOWER + || (DAT_UnitsState::instance.units[_targetUnitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) + && (UVar2 != Map::Units::UT_S_BATTERINGRAM + || (DAT_UnitsState::instance.units[_targetUnitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) + && (UVar2 != Map::Units::UT_S_SHIELD + || (DAT_UnitsState::instance.units[_targetUnitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 1))) { + short sVar3 = DAT_UnitsState::instance.units[_targetUnitID].topSpeedDelayIndex; + short sVar10 + = (short)DAT_UnitSelectionDefinedData::instance.UnitInstructionMoveDelay[sVar3]; + if (this->tribes[tribeID].unkIsAnimalTribe == 0) { + if (-1 < this->field6_0x18) { + sVar10 = sVar10 * 2; + } + } else { + sVar10 = sVar10 * 3; + } + DAT_UnitsState::instance.units[_targetUnitID].moveInstructionSpeedDelayTracker = sVar10; + if (this->field6_0x18 < 0) { + DAT_UnitsState::instance.units[_targetUnitID].moveDelay = 0; + } else { + DAT_UnitsState::instance.units[_targetUnitID].moveDelay + = (short)(DAT_UnitSelectionDefinedData::instance.UnitMoveDelay[sVar3] / 2); + } + if (DAT_UnitsState::instance.units[_targetUnitID].movementCooldown != 0) { + DAT_UnitsState::instance.units[_targetUnitID].moveDelay = 0; + } + DAT_UnitsState::instance.units[_targetUnitID].moveInstructionSpeedDelayTracker = 0; + DAT_UnitsState::instance.units[_targetUnitID].moveDelay = 0; + DAT_UnitsState::instance.units[_targetUnitID].horseArcherShootingVariation = 0; + DAT_UnitsState::instance.units[_targetUnitID].targetingType + = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; + } + } while (_tribeIndex_4 < this->tribes[tribeID].size); + } + return (undefined4)(1); + } + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp new file mode 100644 index 0000000000..470b07ba21 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp @@ -0,0 +1,46 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Game/GameMode.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Game::GameMode; + + // FUNCTION: STRONGHOLDCRUSADER 0x00523520 + undefined4 TribesState::hasAvailableSpawnSlotForWildlifeOrMercs() + { + int iVar1; + bool bVar4; + if ((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) + && (iVar1 = 0, DAT_GameState::instance.mapAndTime.wildlifeCount < 0xa0)) { + do { + if (iVar1 < 4) { + uint uVar3 = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar1][0]; + uint uVar2 = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar1][1]; + if ((uVar3 < 400) && (uVar2 < 400)) { + bVar4 = *(char*)(uVar2 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + uVar3) == '\0'; + goto LAB_00523574; + } + } else { + bVar4 = DAT_GameState::instance.mapAndTime.mercRecruitable[iVar1 + 4] == 0; + LAB_00523574: + if (!bVar4) { + return (undefined4)(1); + } + } + iVar1 = iVar1 + 1; + } while (iVar1 < 0xc); + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp new file mode 100644 index 0000000000..bf8f51b77c --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp @@ -0,0 +1,48 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522DD0 + void TribesState::importStoredInfoFromSlot0(undefined4 param_1, int tribeID) + { + short (*_ptrStoredRallyPoints)[2]; + int iVar1; + short (*_ptrRallyPoints)[2]; + if (this->tribeCopiedToSlot0) { + this->tribes[tribeID].field166_0x2be = this->tribes[0].field166_0x2be; + this->tribes[tribeID].someUnitID = this->tribes[0].someUnitID; + this->tribes[tribeID].someUnitUID = this->tribes[0].someUnitUID; + this->tribes[tribeID].someTile = this->tribes[0].someTile; + this->tribes[tribeID].someTile2 = this->tribes[0].someTile2; + this->tribes[tribeID].currentRallyPointIndex = this->tribes[0].currentRallyPointIndex; + this->tribes[tribeID].rallyPointCount = this->tribes[0].rallyPointCount; + this->tribes[tribeID].isRallyingUnk = this->tribes[0].isRallyingUnk; + this->tribes[tribeID].unitStance = this->tribes[0].unitStance; + _ptrRallyPoints = this->tribes[tribeID].rallyPointArray + 1; + _ptrStoredRallyPoints = this->tribes[0].rallyPointArray + 1; + iVar1 = 2; + do { + _ptrRallyPoints[-1][0] = _ptrStoredRallyPoints[-1][0]; + (*_ptrRallyPoints)[0] = (*_ptrStoredRallyPoints)[0]; + _ptrRallyPoints[1][0] = _ptrStoredRallyPoints[1][0]; + _ptrRallyPoints[2][0] = _ptrStoredRallyPoints[2][0]; + _ptrRallyPoints[3][0] = _ptrStoredRallyPoints[3][0]; + _ptrRallyPoints[4][0] = _ptrStoredRallyPoints[4][0]; + _ptrRallyPoints[5][0] = _ptrStoredRallyPoints[5][0]; + _ptrRallyPoints[6][0] = _ptrStoredRallyPoints[6][0]; + _ptrRallyPoints[7][0] = _ptrStoredRallyPoints[7][0]; + _ptrRallyPoints[8][0] = _ptrStoredRallyPoints[8][0]; + _ptrStoredRallyPoints = (short (*)[2])(*_ptrStoredRallyPoints + 1); + _ptrRallyPoints = (short (*)[2])(*_ptrRallyPoints + 1); + iVar1 = iVar1 + -1; + } while (iVar1); + } + this->tribes[tribeID].unitStance = this->tribes[0].unitStance; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp new file mode 100644 index 0000000000..01756940c2 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp @@ -0,0 +1,41 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524060 + BOOLEnum TribesState::isTribeAllAssassins(int tribeID) + { + int _unitID; + int iVar1 = (int)this->tribes[tribeID].size; + int _unitSelectionIndex = 0; + if (0 < iVar1) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitSelectionIndex); + _unitSelectionIndex = _unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].unitType != Map::Units::UT_A_ASSASSIN) { + return FALSE; + } + } while (_unitSelectionIndex < iVar1); + } + return TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp new file mode 100644 index 0000000000..b47db42884 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp @@ -0,0 +1,39 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525210 + BOOLEnum TribesState::isTribeFreeOfTunnelingUnits(int param_1, int param_2) + { + int iVar2; + int iVar1 = (int)this->tribes[param_1].size; + int unitSelectionIndex = 0; + if (0 < iVar1) { + do { + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar2].dying == 0 + && DAT_UnitsState::instance.units[iVar2].tunnelerFinishedDigging == 2) { + return FALSE; + } + } while (unitSelectionIndex < iVar1); + } + return TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp new file mode 100644 index 0000000000..95e3449b25 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -0,0 +1,64 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524CA0 + undefined4 TribesState::isTribePathToDestinationClear( + int param_1, uint param_2, uint param_3, uint param_4, uint param_5) + { + int iVar2; + int iVar3; + int local_4 = 0; + if (param_2 < 400 && param_3 < 400 && *(char*)(param_3 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + param_2) != '\0' && param_4 < 400 + && param_5 < 400 && *(char*)(param_5 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + param_4) != '\0' + && (iVar2 = DAT_ViewportRenderState::instance.translationMatrix[param_5].addXgetTile + param_4, + DAT_TileMapState::instance.PathConnectionLayer + [DAT_ViewportRenderState::instance.translationMatrix[param_3].addXgetTile + param_2] + == DAT_TileMapState::instance.PathConnectionLayer[iVar2])) { + this->ALG_ResultTileIndex = 0; + uint uVar1 = DAT_TileMapState::instance.LogicLayer[iVar2]; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(param_4, param_5, -1, -1, 500, FALSE); + while (true) { + do { + if (this->tribes[param_1].size <= local_4) { + return (undefined4)(1); + } + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, local_4); + local_4 = local_4 + 1; + } while ((DAT_UnitsState::instance.units[iVar2].logicalState != Map::Units::ULS_NORMAL) + || (DAT_UnitsState::instance.units[iVar2].dying != 0)); + iVar3 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findClosestTileToStartingTile, + DAT_PathFindingState::ptr)(2 - (uint)((uVar1 & 0x100))); + this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; + if (499 < this->ALG_ResultTileIndex) + break; + iVar3 = (int)(short)DAT_TileMapState::instance.UnitLayer[iVar3]; + if (iVar3 && iVar2 != iVar3 && DAT_UnitsState::instance.units[iVar3].tribeID != param_1 + && DAT_UnitsState::instance.units[iVar3].tunnelerFinishedDigging != 2) { + return (undefined4)(0); + } + } + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp new file mode 100644 index 0000000000..17dc494165 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp @@ -0,0 +1,58 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524B30 + undefined4 TribesState::isTribeUnitBlockedByOtherUnit(int param_1) + { + int iVar2; + int unitSelectionIndex = 0; + short sVar1 = this->tribes[param_1].size; + while (true) { + do { + if (sVar1 <= unitSelectionIndex) { + return (undefined4)(0); + } + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + } while ((DAT_UnitsState::instance.units[iVar2].logicalState != Map::Units::ULS_NORMAL) + || (DAT_UnitsState::instance.units[iVar2].dying != 0)); + if (DAT_UnitsState::instance.units[iVar2].usingTeleport != 0) { + return (undefined4)(0); + } + if (DAT_UnitsState::instance.units[iVar2].field303_0x413 != 0) { + return (undefined4)(0); + } + if (DAT_UnitsState::instance.units[iVar2].tunnelerFinishedDigging == 2) + break; + if (DAT_UnitsState::instance.units[iVar2].state.generic == Map::Units::States::US_MELEE_ATTACK) { + return (undefined4)(0); + } + if (DAT_UnitsState::instance.units[iVar2].laddermanIsInPosition != '\0') { + return (undefined4)(0); + } + int iVar3 + = (int)(short)DAT_TileMapState::instance.UnitLayer[DAT_UnitsState::instance.units[iVar2].tile]; + if (iVar3 && iVar2 != iVar3 && DAT_UnitsState::instance.units[iVar3].tunnelerFinishedDigging != 2) { + return (undefined4)(1); + } + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp new file mode 100644 index 0000000000..b7f5f2a9fd --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -0,0 +1,58 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Navigation/Algorithms/XYPair.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Navigation::Algorithms::XYPair; + using Map::Units::Instructions::UnitMatchSpeedEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052A700 + int TribesState::moveTribeToIndexedNearbyTile(int param_1) + { + uint uVar3; + int* piVar6; + uint uVar7; + uint x1; + uint y1; + short sVar1 = this->tribes[param_1].selectionTargetUnitID; + uint uVar4 = (uint)DAT_UnitsState::instance.units[sVar1].y; + uVar7 = (uint)DAT_UnitsState::instance.units[sVar1].x; + int iVar2 = (int)this->tribes[param_1].someIndex; + int iVar5 = iVar2 % 0x28; + if (iVar5 <= 0) { + iVar5 = 0; + } else if (0x27 < iVar5) { + return iVar2 / 0x28; + } + piVar6 = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[iVar5].y; + do { + x1 = ((XYPair*)(piVar6 + -1))->x + uVar7; + y1 = *piVar6 + uVar4; + uVar3 = uVar4; + if (x1 < 400 && y1 < 400 && *(char*)(y1 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x1) != '\0') { + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( + param_1, uVar7, uVar4, x1, y1); + uVar3 = 0; + if (iVar2) { + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + param_1, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + return iVar2; + } + } + piVar6 = piVar6 + 2; + } while ((int)piVar6 < 0xb4df44); + return (int)(uVar3); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp new file mode 100644 index 0000000000..9539b46b31 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp @@ -0,0 +1,47 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Navigation/Algorithms/XYPair.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Navigation::Algorithms::XYPair; + using Map::Units::Instructions::UnitMatchSpeedEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052A640 + void TribesState::moveTribeToNearbyClearTile(int param_1) + { + int iVar3; + int* piVar4; + uint uVar5; + uint x1; + uint y1; + short sVar1 = this->tribes[param_1].selectionTargetUnitID; + uVar5 = (uint)DAT_UnitsState::instance.units[sVar1].x; + uint uVar2 = (uint)DAT_UnitsState::instance.units[sVar1].y; + piVar4 = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[0].y; + while (true) { + x1 = ((XYPair*)(piVar4 + -1))->x + uVar5; + y1 = *piVar4 + uVar2; + if (x1 < 400 && y1 < 400 && *(char*)(y1 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x1) != '\0' + && (iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( + param_1, uVar5, uVar2, x1, y1), + iVar3 != 0)) + break; + piVar4 = piVar4 + 2; + if (0xb4df43 < (int)piVar4) {} + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + param_1, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp new file mode 100644 index 0000000000..cdfebc6e4d --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -0,0 +1,141 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Units/TroopValueState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + using Map::Units::UnitLogicState; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00521280 + undefined4 TribesState::moveUnitToBehaviorTarget(int unitID, int param_2) + { + int iVar4; + short sVar1 = DAT_UnitsState::instance.units[unitID].tribeID; + int bVar3 = 0; + uint x = 0; + uint uVar6 = 0; + uint local_c = 0; + if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL) { + return (undefined4)(0); + } + if (DAT_UnitsState::instance.units[unitID].dying != 0) { + return (undefined4)(0); + } + if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0) { + return (undefined4)(0); + } + if (DAT_UnitsState::instance.units[unitID].field303_0x413 != 0) { + return (undefined4)(0); + } + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[DAT_UnitsState::instance.units[unitID].owner] + != -1) { + return (undefined4)(0); + } + if (param_2 == 0x3f2) { + iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( + unitID); + LAB_0052130b: + if (!iVar4) + goto LAB_0052143b; + local_c = DAT_TroopValueState::instance.y; + } else if (param_2 == 0x3f5) { + iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, + DAT_TroopValueState::ptr)(unitID); + LAB_00521333: + if (!iVar4) + goto LAB_0052143b; + local_c = DAT_TroopValueState::instance.y; + } else { + if (param_2 == 0x3fb) { + iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, + DAT_TroopValueState::ptr)(unitID); + if (!iVar4) { + iVar4 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)(unitID); + if (!iVar4) + goto LAB_0052143b; + if (DAT_TribesState::instance.tribes[sVar1].selectionTargetUnitID == unitID) { + DAT_TribesState::instance.tribes[sVar1].tribeBehaviorType = Map::Units::STBT_0x3f2; + } + local_c = DAT_TroopValueState::instance.y; + DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = 0x3f2; + goto LAB_0052142a; + } + } else { + if (param_2 == 0x3fd) { + iVar4 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)(unitID); + goto LAB_00521333; + } + if (param_2 == 0x3f6) { + iVar4 = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyTowersOrGates, DAT_TroopValueState::ptr)(unitID); + } else { + if (param_2 == 0x413) { + iVar4 + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID); + goto LAB_0052130b; + } + if (param_2 == 0x414) { + iVar4 + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID); + goto LAB_00521333; + } + if (param_2 != 0x3f7) + goto LAB_0052143b; + iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestDiggableMoatPoint, + DAT_TroopValueState::ptr)(unitID); + } + if (!iVar4) + goto LAB_0052143b; + } + local_c = DAT_TroopValueState::instance.y; + } + LAB_0052142a: + bVar3 = 1; + x = DAT_TroopValueState::instance.x; + uVar6 = DAT_TroopValueState::instance.tile; + LAB_0052143b: + DAT_UnitsState::instance.units[unitID].targetedBuildingTile = 0; + if (!bVar3) { + return (undefined4)(0); + } + short sVar5 = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[uVar6]; + short sVar2 = DAT_UnitsState::instance.units[unitID].selectionTargetUnitID; + DAT_UnitsState::instance.units[unitID].attackAtTileY = sVar5; + DAT_UnitsState::instance.units[unitID].targetedBuildingTile = uVar6; + sVar5 = (short)uVar6 - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar5].addXgetTile; + DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[unitID].attackAtTileX = sVar5; + if (sVar2 == unitID) { + DAT_TribesState::instance.tribes[sVar1].targetX = sVar5; + DAT_TribesState::instance.tribes[sVar1].targetY = DAT_UnitsState::instance.units[unitID].attackAtTileY; + } + iVar4 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(unitID); + if (!iVar4) { + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + unitID, x, local_c, 0); + } + return (undefined4)(1); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp new file mode 100644 index 0000000000..1cb21ac631 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp @@ -0,0 +1,42 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522090 + void TribesState::playAttackCommandFeedback(int param_1) + { + UnitType unitType; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + int iVar1 = param_1; + unitType + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(param_1, ¶m_1); + if (unitType == Map::Units::UT_S_BATTERINGRAM || unitType == Map::Units::UT_S_CATAPULT + || unitType == Map::Units::UT_S_TREBUCHET || unitType == Map::Units::UT_S_MANGONEL + || unitType == Map::Units::UT_S_BALLISTA || unitType == Map::Units::UT_S_FBALLISTA) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::playUnitMoveSpeech, this)( + DAT_TribesState::instance.DAT_CurrentTribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::playUnitSelectionSound, this)(iVar1); + } else { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 0x15); + if (unitType != Map::Units::UT_E_ENGINEER) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::playUnitSelectionSound, this)(iVar1); + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp new file mode 100644 index 0000000000..b7f5f8ed2c --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -0,0 +1,195 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/Map/Units/UnitTypeInt.hpp" + +#include "OpenSHC/Globals/DAT_EntityDefinedData.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitType; + using Map::Units::UnitTypeInt; + + // FUNCTION: STRONGHOLDCRUSADER 0x00521A90 + void TribesState::playUnitCombatSpeechForTarget(int selectionID, int unitID) + { + UnitTypeShort UVar2; + int _otherUnitID; + UnitTypeInt _ptrptrCurrentSelectionID; + int _yDifference; + int _otherX; + int _xDifference; + int _otherY; + int sfxOffsetInArray; + TribesState* _ptrCurrentSelectionID; + uint _actionID; + int _distance; + TribesState* _ptrCurrentSelectionID_2; + _ptrCurrentSelectionID = this; + _otherUnitID + = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + _ptrptrCurrentSelectionID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, + this)(selectionID, (int*)&_ptrCurrentSelectionID); + _ptrCurrentSelectionID_2 = _ptrCurrentSelectionID; + short sVar1 = DAT_TribesState::instance.tribes[selectionID].size; + switch (_ptrptrCurrentSelectionID) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + if (_ptrptrCurrentSelectionID == Map::Units::UT_E_ARCHER) { + _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; + } else if (_ptrptrCurrentSelectionID == Map::Units::UT_E_XBOW) { + _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[7] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[7]; + } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_ARCHER) { + _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; + } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_SLINGER) { + _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x21] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x21]; + } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_HARCHER) { + _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; + } else { + _distance = unitID; + if (_ptrptrCurrentSelectionID == Map::Units::UT_A_FIRETHROWER) { + _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22]; + } + } + _xDifference = (int)DAT_UnitsState::instance.units[_otherUnitID].x + - (int)DAT_UnitsState::instance.units[unitID].x; + _yDifference = (int)DAT_UnitsState::instance.units[_otherUnitID].y + - (int)DAT_UnitsState::instance.units[unitID].y; + if (_distance < _xDifference * _xDifference + _yDifference * _yDifference) { + _actionID = 0x1e; + } else { + _actionID = 0x19; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, _actionID); + if (_ptrptrCurrentSelectionID != Map::Units::UT_A_HARCHER) {} + if (_ptrCurrentSelectionID == (TribesState*)0x1) { + _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; + _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; + sfxOffsetInArray = 0xea; + } else if ((int)_ptrCurrentSelectionID < 5) { + _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; + _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; + sfxOffsetInArray = 0xeb; + } else { + _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; + _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; + sfxOffsetInArray = 0xec; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + _otherX, _otherY, sfxOffsetInArray); + _otherX = 0x59; + break; + case Map::Units::UT_E_SPEAR: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); + if (sVar1 < 0xf) {} + _otherX = 0x14; + break; + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_MONK: + UVar2 = DAT_UnitsState::instance.units[unitID].unitType; + if (UVar2 == Map::Units::UT_E_KNIGHT || UVar2 == Map::Units::UT_E_SWORD + || UVar2 == Map::Units::UT_LORD) { + _otherX = 0x11; + } else { + _otherX = 0x15; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, _otherX); + if (sVar1 < 0xf) {} + _otherX = 0x14; + break; + case Map::Units::UT_E_SWORD: + if (_ptrCurrentSelectionID == (TribesState*)0x1) { + _otherX = 0xad; + } else if (_ptrCurrentSelectionID == (TribesState*)0x2) { + _otherX = 0xae; + } else { + if ((int)_ptrCurrentSelectionID < 3) + goto LAB_00521d1b; + _otherX = 0xaf; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); + LAB_00521d1b: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); + if (sVar1 < 0xf) {} + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); + return; + case Map::Units::UT_E_KNIGHT: + if (_ptrCurrentSelectionID == (TribesState*)0x1) { + _otherX = 0x56; + LAB_00521c6f: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); + } else { + if ((_ptrCurrentSelectionID == (TribesState*)0x2) + || (_ptrCurrentSelectionID == (TribesState*)0x3)) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x57); + } + if (3 < (int)_ptrCurrentSelectionID_2) { + _otherX = 0x58; + goto LAB_00521c6f; + } + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x59); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); + if (sVar1 < 0xf) {} + _otherX = 0xfa; + break; + default: + UVar2 = DAT_UnitsState::instance.units[unitID].unitType; + if (UVar2 != Map::Units::UT_E_KNIGHT && UVar2 != Map::Units::UT_E_SWORD + && UVar2 != Map::Units::UT_E_MACE && UVar2 != Map::Units::UT_E_PIKE + && UVar2 != Map::Units::UT_LORD) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x11); + case Map::Units::UT_E_ENGINEER: + return; + case Map::Units::UT_A_SWORDSMAN: + if (sVar1 < 0xf) {} + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp new file mode 100644 index 0000000000..6013942e2b --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp @@ -0,0 +1,112 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Audio/SFX/SpeechEffectID.hpp" + +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Audio::SFX::SpeechEffectID; + + // FUNCTION: STRONGHOLDCRUSADER 0x00521EF0 + void TribesState::playUnitMoveSpeech(undefined4 param_1) + { + int _unitID; + UnitType _unitTypeID; + _unitID = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + /* + bug: the second argument ("stack") does not want to be renamed, but is sfxToPlay again. + */ + _unitTypeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( + (param_1), (int*)(¶m_1)); + switch (_unitTypeID) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + /* + Any other ranged unit + */ + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + _unitTypeID, 0x1d); + return; + case Map::Units::UT_S_CATAPULT: + /* + Catapult + */ + if (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 0) { + if (DAT_UnitsState::instance.units[_unitID].stoneAmmunition < 1) { + param_1 = 0x2e; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( + Audio::SFX::SEID_RESOURCE_NEED25); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + _unitTypeID, 0x10); + } + break; + case Map::Units::UT_S_TREBUCHET: + /* + Trebuchet + */ + if (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 0) { + if (DAT_UnitsState::instance.units[_unitID].stoneAmmunition < 1) { + param_1 = 0x2e; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( + Audio::SFX::SEID_RESOURCE_NEED25); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + _unitTypeID, 0x11); + } + break; + case Map::Units::UT_S_MANGONEL: + /* + Mangonel + */ + if (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 0) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + _unitTypeID, 0xf); + } + break; + case Map::Units::UT_S_BATTERINGRAM: + /* + Battering ram + */ + if (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 0) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + _unitTypeID, 0x13); + } + break; + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + /* + Ballista and fire ballista + */ + if (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 0) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + _unitTypeID, 0x12); + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp new file mode 100644 index 0000000000..082fba9537 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -0,0 +1,132 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x005217E0 + void TribesState::playUnitSelectionSound(int param_1) + { + short* psVar1; + short* psVar2; + int iVar3; + UnitType UVar4; + int iVar5; + int sfxOffsetInArray; + iVar3 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + UVar4 + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(param_1, ¶m_1); + iVar5 = param_1; + switch (UVar4) { + case Map::Units::UT_E_SWORD: + if (param_1 != 1) { + if (param_1 == 2) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0xae); + } + if (param_1 < 3) {} + iVar5 = 0xaf; + LAB_0052188b: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + iVar5); + } + iVar5 = 0xad; + goto LAB_00521827; + case Map::Units::UT_E_KNIGHT: + if (param_1 == 1) { + iVar5 = 0x56; + LAB_005218ec: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + iVar5); + } else { + if ((param_1 == 2) || (param_1 == 3)) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0x57); + } + if (3 < iVar5) { + iVar5 = 0x58; + goto LAB_005218ec; + } + } + iVar5 = 0x59; + LAB_00521827: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + iVar5); + return; + case Map::Units::UT_E_ENGINEER: + if ((DAT_UnitsState::instance.units[iVar3].unitType == Map::Units::UT_E_ENGINEER) + && (DAT_UnitsState::instance.units[iVar3].resourceToDeposit != 0)) { + param_1 = 0x24; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0x24); + } + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TOWER: + case Map::Units::UT_S_BATTERINGRAM: + iVar5 = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(iVar3); + if (iVar5 <= 0) { + iVar5 = 0x68; + goto LAB_0052188b; + } + if ((DAT_UnitsState::instance.units[iVar3] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 0) + || (3 < iVar5)) { + param_1 = 0xe; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xe); + } + if (iVar5 == 1) { + param_1 = 0xb; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xb); + } + if (iVar5 == 2) { + param_1 = 0xc; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xc); + } + if (iVar5 == 3) { + param_1 = 0xd; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xd); + } + break; + case Map::Units::UT_A_HARCHER: + psVar1 = &DAT_UnitsState::instance.units[iVar3].y; + psVar2 = &DAT_UnitsState::instance.units[iVar3].x; + if (param_1 == 1) { + iVar3 = (int)*psVar1; + iVar5 = (int)*psVar2; + sfxOffsetInArray = 0xea; + } else if (param_1 < 5) { + iVar3 = (int)*psVar1; + iVar5 = (int)*psVar2; + sfxOffsetInArray = 0xeb; + } else { + iVar3 = (int)*psVar1; + iVar5 = (int)*psVar2; + sfxOffsetInArray = 0xec; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + iVar5, iVar3, sfxOffsetInArray); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)*psVar2, (int)(*psVar1), 0x59); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp new file mode 100644 index 0000000000..b703135830 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -0,0 +1,123 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00521500 + void TribesState::predictUnitInterceptPosition( + int targetUnitID, int unitID, int* unitCurrentX, int* unitCurrentY) + { + UnitTypeShort UVar1; + uint _currentIndexInPathPlan; + int local_8; + short _targetTribeMinSpeed; + int _unitID = unitID; + int _unitAddressOffset = unitID * 0x490; + *unitCurrentX = (int)DAT_UnitsState::instance.units[unitID].x; + int _yOffb = (int)DAT_UnitsState::instance.units[unitID].y; + *unitCurrentY = _yOffb; + if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging != 2) {} + _targetTribeMinSpeed + = this->tribes[DAT_UnitsState::instance.units[targetUnitID].tribeID].minimumMovementSpeed; + int _targetTribeMinSpeedBounded = (int)_targetTribeMinSpeed; + if (_targetTribeMinSpeed < 2) { + _targetTribeMinSpeedBounded = 1; + } + short _tribeMinSpeed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; + int iVar3 = _targetTribeMinSpeedBounded * 4; + int _tribeMinSpeedBounded = (int)_tribeMinSpeed; + if (_tribeMinSpeed < 2) { + _tribeMinSpeedBounded = 1; + } + int iVar4 = (int)DAT_UnitsState::instance.units[targetUnitID].x; + int iVar2 = (int)DAT_UnitsState::instance.units[targetUnitID].y; + if (DAT_UnitsState::instance.units[targetUnitID].stateBasedSpeed <= 0) { + UVar1 = DAT_UnitsState::instance.units[targetUnitID].unitType; + if ((UVar1 == Map::Units::UT_E_KNIGHT) + && (DAT_UnitsState::instance.units[targetUnitID].isMatchingSpeed == false)) { + iVar3 = iVar3 / 3; + } else if (UVar1 == Map::Units::UT_E_SWORD) + goto LAB_005215e9; + } else { + LAB_005215e9: + iVar3 = iVar3 / 2; + } + if (DAT_UnitsState::instance.units[unitID].stateBasedSpeed <= 0) { + UVar1 = DAT_UnitsState::instance.units[unitID].unitType; + if ((UVar1 == Map::Units::UT_E_KNIGHT) + && (DAT_UnitsState::instance.units[unitID].isMatchingSpeed == false)) { + unitID = iVar3 / 3; + goto LAB_0052163b; + } + unitID = iVar3; + if (UVar1 != Map::Units::UT_E_SWORD) + goto LAB_0052163b; + } + /* + reused parameter! + */ + unitID = iVar3 / 2; + LAB_0052163b: + int _unitCurrentX_2 = *unitCurrentX; + uint _xOff_2 = _unitCurrentX_2 - iVar4; + uint _yOff_2 = _yOffb - iVar2; + int _xOff_2b = (_xOff_2 ^ (int)_xOff_2 >> 0x1f) - ((int)_xOff_2 >> 0x1f); + int _yOff_2b = (_yOff_2 ^ (int)_yOff_2 >> 0x1f) - ((int)_yOff_2 >> 0x1f); + if (_yOff_2b < _xOff_2b) { + _yOff_2b = _xOff_2b; + } + if ((-1 < (_yOff_2b + -2) * unitID) + && (_currentIndexInPathPlan = (uint)DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan, + local_8 = _tribeMinSpeedBounded * 4, + (int)_currentIndexInPathPlan < (int)DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan)) { + do { + /* + get current value in pathPlan, each byte contains two move instructions + */ + uint _pathPlanPair + = (uint) * (char*)((int)_currentIndexInPathPlan / 2 + 0x138864a + _unitAddressOffset); + if (!(_currentIndexInPathPlan & 1)) { + /* + if index is even, retain the lowest 4 bits + */ + _pathPlanPair = _pathPlanPair & 0xf; + } else { + /* + if index is odd retain the highest 4 bits + */ + _pathPlanPair = (int)_pathPlanPair >> 4; + } + *unitCurrentX = DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[_pathPlanPair] + .int_.xOffset + + _unitCurrentX_2; + *unitCurrentY = *unitCurrentY + + *(int*)((int)DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix + + _pathPlanPair * 8 + 4); + _unitCurrentX_2 = *unitCurrentX; + uint _xOff = _unitCurrentX_2 - iVar4; + uint _yOff = *unitCurrentY - iVar2; + iVar3 = (_xOff ^ (int)_xOff >> 0x1f) - ((int)_xOff >> 0x1f); + _yOffb = (_yOff ^ (int)_yOff >> 0x1f) - ((int)_yOff >> 0x1f); + if (_yOffb < iVar3) { + _yOffb = iVar3; + } + if ((_yOffb + -2) * unitID < local_8) {} + local_8 = local_8 + _tribeMinSpeedBounded * 4; + _currentIndexInPathPlan = _currentIndexInPathPlan + 1; + } while ( + (int)_currentIndexInPathPlan < (int)DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp new file mode 100644 index 0000000000..36a62d9353 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -0,0 +1,164 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/WildlifeState.func.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/UI/Enums/MenuViewType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_WildlifeState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using DE::SHCDE::eSFX; + using Map::Units::SomeTribeBehaviorType; + using Map::Units::UnitType; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using UI::Enums::MenuViewType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052AD50 + void TribesState::processDeerMoving(int tribeID) + { + int sVar2; + int SVar3; + int iVar4; + int iVar5; + int iVar6; + BOOLEnum BVar7; + int iVar8; + int _x10; + int _y10; + sVar2 = this->tribes[tribeID].selectionTargetUnitID; + _x10 = (int)DAT_UnitsState::instance.units[sVar2].x / 10; + _y10 = (int)DAT_UnitsState::instance.units[sVar2].y / 10; + iVar8 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; + iVar4 = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount; + iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; + iVar6 = DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; + sVar2 = (ushort)this->tribes[tribeID].size; + this->tribes[tribeID].unkIsAnimalTribe = 1; + if (sVar2 < 0x1f) { + this->tribes[tribeID].herdMinSize = sVar2 / 2; + this->tribes[tribeID].herdMaxSize = sVar2 * 2; + } else { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trimTribeToSize, this)(tribeID, (int)(30)); + this->tribes[tribeID].herdMinSize = 0xf; + this->tribes[tribeID].herdMaxSize = 0x3c; + } + if ((iVar4 + iVar6 != 0) && (this->tribes[tribeID].predatorNearby == 0)) { + this->tribes[tribeID].predatorNearby = 1; + } + BVar7 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + tribeID, 100, Map::Units::UT_ANTELOPESHDEER); + if (!BVar7) { + if (this->tribes[tribeID].initialMoveOrderGiven == 0) { + sVar2 = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].initialMoveOrderGiven = 1; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + (uint)(DAT_UnitsState::instance.units[sVar2].x), + (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, 0, Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + } + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[tribeID].unknownCounter01 = 0; + } + sVar2 = this->tribes[tribeID].predatorNearby; + if (!sVar2) { + SVar3 = this->tribes[tribeID].tribeBehaviorType; + if (SVar3 == (SomeTribeBehaviorType)0) { + if (iVar5) { + this->tribes[tribeID].wanderIntervalTicks = 200; + } + if (0x14 < iVar8) { + this->tribes[tribeID].wanderIntervalTicks = 200; + } + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if (this->tribes[tribeID].wanderIntervalTicks + <= this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + sVar2 = this->tribes[tribeID].rallyPointCount; + this->tribes[tribeID].wanderIntervalTicks + = (4 - ((byte)SEC_RNG::instance.currentNumber2 & 3)) * 200; + if (sVar2 < 1) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 0); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + sVar2 = this->tribes[tribeID].currentRallyPointIndex; + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][0]), + (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0); + this->tribes[tribeID].currentRallyPointIndex + = this->tribes[tribeID].currentRallyPointIndex + 1; + if (this->tribes[tribeID].rallyPointCount <= this->tribes[tribeID].currentRallyPointIndex) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].rallyPointCount = 0; + } + } + } else if (SVar3 == Map::Units::STBT_1) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 800) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + } + } + } else { + if (sVar2 == 1) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 0); + if ((this->tribes[tribeID].rallyPointCount < 1) + && (MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 1), + this->tribes[tribeID].rallyPointCount < 1)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); + } else { + sVar2 = this->tribes[tribeID].selectionTargetUnitID; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[sVar2].x, + (int)(DAT_UnitsState::instance.units[sVar2].y), DE::SHCDE::FX_DEER_RUN); + iVar8 = (int)this->tribes[tribeID].rallyPointCount; + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][0]), + (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][1]), 0, 0); + } + } + this->tribes[tribeID].predatorNearby = this->tribes[tribeID].predatorNearby + 1; + if (799 < this->tribes[tribeID].predatorNearby) { + this->tribes[tribeID].predatorNearby = 0; + } + } + } else { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 2, 0); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + sVar2 = this->tribes[tribeID].currentRallyPointIndex; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][0]), + (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0, + Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; + if (this->tribes[tribeID].rallyPointCount <= this->tribes[tribeID].currentRallyPointIndex) { + this->tribes[tribeID].rallyPointCount = 0; + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp new file mode 100644 index 0000000000..9f6c621b9e --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp @@ -0,0 +1,32 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525470 + void TribesState::removeAllTribeUnits(int param_1) + { + int iVar2; + int iVar1 = 0; + if (0 < this->tribes[param_1].size) { + do { + iVar2 = iVar1 + 1; + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, iVar1); + DAT_UnitsState::instance.units[iVar1].logicalState = Map::Units::ULS_REMOVE; + iVar1 = iVar2; + } while (iVar2 < this->tribes[param_1].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp b/src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp new file mode 100644 index 0000000000..26ba4e796a --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp @@ -0,0 +1,64 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/MSVC_SecurityCookie.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525790 + void TribesState::removeSelectedUnitsFromTheirCurrentTribes(int playerID) + { + Unit* _pUnit; + int _unitTribeID; + int _tribeID; + uint _unitID; + bool _unitRemovedFromTribeByID[1250]; + Tribe* _pTribe; + uint uVar1 = MSVC_SecurityCookie::instance ^ (uint)_unitRemovedFromTribeByID; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 1250, '\0', (void*)((int)(_unitRemovedFromTribeByID))); + _unitID = 1; + if (1 < DAT_UnitsState::instance.maxUnitCount) { + _pUnit = &DAT_UnitsState::instance.units[1]; + do { + if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying + && _pUnit->ifSelectedThenPlayerID == playerID + && (_unitTribeID = (int)_pUnit->tribeID, 0 < _unitTribeID)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromThisTribeIfInTribe, this)( + _unitID, _unitTribeID); + _unitRemovedFromTribeByID[_unitTribeID] = true; + } + _unitID = _unitID + 1; + _pUnit = _pUnit + 0x248; + } while ((int)_unitID < DAT_UnitsState::instance.maxUnitCount); + } + _tribeID = 1; + _pTribe = &this->tribes[1]; + do { + /* + tribeState + */ + if (_pTribe->tribeState == 2) { + if (_pTribe->selectionTargetUnitID == -1) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(_tribeID); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(_tribeID); + } + _tribeID = _tribeID + 1; + _pTribe = _pTribe + 1; + } while (_tribeID < 1250); + ; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp new file mode 100644 index 0000000000..f7510778c3 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -0,0 +1,45 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522630 + void TribesState::removeUnitFromThisTribeIfInTribe(uint unitID, int tribeID) + { + short* psVar1; + uint uVar2; + short _size; + if (DAT_UnitsState::instance.units[unitID].tribeID == 0) { + uVar2 = (int)unitID % 16; + if ((ushort)(DAT_UnitSelectionDefinedData::instance.BitMaskHelper[uVar2] + & this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16]) + == 0) {} + } + _size = this->tribes[tribeID].size; + if (0 < _size) { + this->tribes[tribeID].size = _size + -1; + } + if (this->tribes[tribeID].size <= 0) { + this->tribes[tribeID].tribeState = 3; + } + psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); + uVar2 = (int)unitID % 16; + *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[uVar2]; + if (DAT_UnitsState::instance.units[unitID].tribeID == tribeID) { + DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = 0; + DAT_UnitsState::instance.units[unitID].tribeID = 0; + DAT_UnitsState::instance.units[unitID].idInTribe = 0; + } + if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { + this->tribes[tribeID].selectionTargetUnitID = -1; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp new file mode 100644 index 0000000000..371d4b1693 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -0,0 +1,55 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00525A70 + void TribesState::removeUnitFromTribe(uint unitID, int tribeID) + { + short* psVar1; + uint uVar2; + short _size; + if (DAT_UnitsState::instance.units[unitID].tribeID == 0) { + uVar2 = unitID & 0x8000000f; + if ((int)uVar2 < 0) { + uVar2 = (uVar2 - 1 | 0xfffffff0) + 1; + } + if ((ushort)(DAT_UnitSelectionDefinedData::instance.BitMaskHelper[uVar2] + & this->tribes[tribeID].unitSelectionBitMasked[unitID / 16]) + == 0) + goto LAB_00525b56; + } + _size = this->tribes[tribeID].size; + if (0 < _size) { + this->tribes[tribeID].size = _size + -1; + } + if (this->tribes[tribeID].size <= 0) { + this->tribes[tribeID].tribeState = 3; + } + psVar1 = this->tribes[tribeID].unitSelectionBitMasked + (unitID / 16); + uVar2 = unitID & 0x8000000f; + if ((int)uVar2 < 0) { + uVar2 = (uVar2 - 1 | 0xfffffff0) + 1; + } + *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[uVar2]; + if (DAT_UnitsState::instance.units[unitID].tribeID == tribeID) { + DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = 0; + DAT_UnitsState::instance.units[unitID].tribeID = 0; + DAT_UnitsState::instance.units[unitID].idInTribe = 0; + } + if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(tribeID); + } + LAB_00525b56: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp b/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp new file mode 100644 index 0000000000..ba555ede3f --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp @@ -0,0 +1,73 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/LandscapeState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LandscapeState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Commands::MappersEnum; + using Game::GameMode; + using Game::GameMode2; + using Map::MapType2; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005261B0 + void TribesState::respawnDeer() + { + BOOLEnum BVar1; + uint _x; + uint _y; + if (DAT_GameCore::instance.gameMode_2 != Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != Game::GM_SIEGE_THAT + && (DAT_GameCore::instance.gameMode_2 != Game::GM_BUILDERUnk + || (DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 != Map::MT_SIEGE)) + && DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY + && DAT_GameState::instance.mapAndTime.aliveDeerCount + && DAT_GameState::instance.mapAndTime.deerCount < 6) { + int _counter = 0; + while (true) { + _x = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].x; + _y = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].y; + if (_counter < 4) { + _x = (int)DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_counter].x; + _y = (int)DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_counter].y; + } + BVar1 = MACRO_CALL_MEMBER( + Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)(_x, _y); + if (BVar1) + break; + LAB_00526268: + _counter = _counter + 1; + if (4 < _counter) {} + } + int _tile = DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile + _x; + if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x50501581U)) { + if (DAT_TileMapState::instance.OrganismLayer[_tile] == 0) + goto LAB_00526268; + MACRO_CALL_MEMBER(Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)( + (int)DAT_TileMapState::instance.OrganismLayer[_tile]); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( + Commands::M_MAPPER_DEER, _x, _y, (int)(DAT_TileMapState::instance.HeightLayer[_tile])); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp new file mode 100644 index 0000000000..ed6e6b074a --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp @@ -0,0 +1,65 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00523920 + int TribesState::scatterTribeUnitsRandomly(int param_1) + { + int iVar2; + int iVar3; + if (!param_1) { + return 0; + } + short sVar1 = this->tribes[param_1].selectionTargetUnitID; + int unitSelectionIndex = 0; + int index = 100; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSuitableSpawnLocationUnk, + DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[sVar1].x, + (int)(DAT_UnitsState::instance.units[sVar1].y), -1, -1, 5000, 0); + iVar2 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::getComputationalTileIndex, DAT_PathFindingState::ptr)(); + int iVar4 = (int)this->tribes[param_1].size; + if (iVar4 * 0x10 + 200 <= iVar2) { + if (0 < iVar4) { + do { + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar2].dying == 0)) { + index = index + 1 + (DAT_UnitsState::instance.units[iVar2].fixedRng & 0xf); + iVar3 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::getTileInSearchQueue, + DAT_PathFindingState::ptr)(index); + iVar4 = DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar3]] + .addXgetTile; + DAT_UnitsState::instance.units[iVar2].targetY + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[iVar3]; + DAT_UnitsState::instance.units[iVar2].targetX = (short)iVar3 - (short)iVar4; + DAT_UnitsState::instance.units[iVar2].state.generic = ((UnitStateShort)0xd1); + } + } while (unitSelectionIndex < this->tribes[param_1].size); + } + return 1; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp b/src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp new file mode 100644 index 0000000000..5892abc3a4 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp @@ -0,0 +1,43 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524230 + BOOLEnum TribesState::selectionContainsHorses(int param_1) + { + UnitTypeShort UVar1; + int iVar2; + int iVar3 = (int)this->tribes[param_1].size; + int unitSelectionIndex = 0; + if (0 < iVar3) { + do { + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar2].dying == 0 + && (UVar1 = DAT_UnitsState::instance.units[iVar2].unitType, + UVar1 == Map::Units::UT_A_HARCHER || (UVar1 == Map::Units::UT_E_KNIGHT))) { + return TRUE; + } + } while (unitSelectionIndex < iVar3); + } + return FALSE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp b/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp new file mode 100644 index 0000000000..be3da1bd87 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp @@ -0,0 +1,28 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x005233A0 + void TribesState::setRabbitSpawnXY(undefined4 x, undefined4 y) + { + short (*pasVar1)[2]; + pasVar1 = DAT_GameState::instance.mapAndTime.rabbitSpawnXY; + do { + if ((*pasVar1)[0] == 0) { + (*pasVar1)[0] = (short)x; + (*pasVar1)[1] = (short)y; + } + pasVar1 = pasVar1 + 1; + } while ((int)pasVar1 < 0x117ef1c); + DAT_GameState::instance.mapAndTime.rabbitSpawnXY[0][0] = (short)x; + DAT_GameState::instance.mapAndTime.rabbitSpawnXY[0][1] = (short)y; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp new file mode 100644 index 0000000000..9970c47a0c --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp @@ -0,0 +1,36 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525300 + void TribesState::setStateForAllTribeUnits(int param_1, undefined4 param_2) + { + int iVar1; + int unitSelectionIndex = 0; + if (0 < this->tribes[param_1].size) { + do { + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { + DAT_UnitsState::instance.units[iVar1].state.generic = (short)param_2; + DAT_UnitsState::instance.units[iVar1].animationCycleNumber = 0; + DAT_UnitsState::instance.units[iVar1].seated = 0; + } + } while (unitSelectionIndex < this->tribes[param_1].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp new file mode 100644 index 0000000000..1f44a08a5d --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp @@ -0,0 +1,42 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525280 + void TribesState::setTargetUnitForTribe(int tribeID) + { + int _unitID; + int _unitSelectionIndex = 0; + short _tribeSize = this->tribes[tribeID].size; + this->tribes[tribeID].selectionTargetUnitID = 0; + if (0 < _tribeSize) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitSelectionIndex); + _unitSelectionIndex = _unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID].dying == 0)) { + short _tribeTargetUnitID = this->tribes[tribeID].selectionTargetUnitID; + if (!_tribeTargetUnitID) { + this->tribes[tribeID].selectionTargetUnitID = (short)_unitID; + DAT_UnitsState::instance.units[_unitID].selectionTargetUnitID = (short)_unitID; + } else { + DAT_UnitsState::instance.units[_unitID].selectionTargetUnitID = _tribeTargetUnitID; + } + } + } while (_unitSelectionIndex < this->tribes[tribeID].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp new file mode 100644 index 0000000000..77b328ef1e --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -0,0 +1,87 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::Behavior::UnitStanceEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522CA0 + void TribesState::snapshotSelectionTribeAndComputeStance(int playerID) + { + int _unitTribeID; + int _tribeCountdown; + int _tribeID_chosen; + Unit* _pUnit; + int _unitIDCountdown; + int _stanceCount[3]; + int _countDefensive; + int _countStandground; + int _tribeID_1; + bool _atLeastTwoTribes; + _atLeastTwoTribes = false; + this->tribeCopiedToSlot0 = 0; + int _index = 0; + _stanceCount[0] = 0; + _stanceCount[1] = 0; + _stanceCount[2] = 0; + int _countAggressive = _index; + _countDefensive = 0; + _countStandground = 0; + if (1 < DAT_UnitsState::instance.maxUnitCount) { + _pUnit = &DAT_UnitsState::instance.units[1]; + _unitIDCountdown = DAT_UnitsState::instance.maxUnitCount - 1; + _tribeID_1 = -1; + do { + _tribeID_chosen = _tribeID_1; + if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying + && _pUnit->ifSelectedThenPlayerID == playerID + && (_unitTribeID = (int)_pUnit->tribeID, + 0 < _unitTribeID + && (_stanceCount[(short)this->tribes[_unitTribeID].unitStance] + = _stanceCount[(short)this->tribes[_unitTribeID].unitStance] + 1, + _tribeID_chosen = _unitTribeID, _tribeID_1 != -1)) + && _unitTribeID != _tribeID_1) { + /* + if unit is in a tribe, count its tribes stance and if + */ + _atLeastTwoTribes = true; + } + _pUnit = _pUnit + 0x248; + _unitIDCountdown = _unitIDCountdown + -1; + _tribeID_1 = _tribeID_chosen; + } while (_unitIDCountdown); + _countAggressive = _stanceCount[2]; + _countDefensive = _stanceCount[1]; + _countStandground = _stanceCount[0]; + if ((_tribeID_chosen != -1) && (!_atLeastTwoTribes)) { + /* + this is an int-by-int sized copy function + */ + int const* _src = (int const*)&this->tribes[_tribeID_chosen]; + int* _dst = (int*)&this->tribes[0]; + for (_tribeCountdown = 0; _tribeCountdown < 205; ++_tribeCountdown) { + _dst[_tribeCountdown] = _src[_tribeCountdown]; + } + this->tribeCopiedToSlot0 = 1; + } + } + if (_countAggressive + _countDefensive + _countStandground == 0) { + this->tribes[0].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + } + if ((_countDefensive <= _countAggressive) && (_countStandground <= _countAggressive)) { + this->tribes[0].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + } + this->tribes[0].unitStance = (ushort)(_countStandground <= _countDefensive); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp new file mode 100644 index 0000000000..d40d205294 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -0,0 +1,200 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingStatePartB.hpp" +#include "OpenSHC/Map/Navigation/PathHelper12.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Navigation::PathFindingStatePartB; + using Map::Navigation::PathHelper12; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524930 + void TribesState::sortTribePathDestinationsByCost(int tribeID, int horseAndRamCount) + { + int iVar2; + int iVar3; + int iVar4; + int iVar5; + int iVar6; + bool bVar7; + int iVar9; + uint y; + PathFindingStatePartB* _p2; + uint x; + int _counter2; + int* _p2Tile2; + int _tile1; + int _offset1; + PathFindingStatePartB* _p; + int _index1; + int _index2; + BOOLEnum BVar8 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + short sVar1 = this->tribes[tribeID].selectionTargetUnitID; + x = (uint)DAT_UnitsState::instance.units[sVar1].x; + if (!horseAndRamCount) { + y = (uint)DAT_UnitsState::instance.units[sVar1].y; + if (!BVar8) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(x, y, -1, -1, 100000, FALSE); + } else { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, + DAT_PathFindingState::ptr)(x, y, 0xffffffff, 0xffffffff, 100000, 0); + } + } else { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::calculatePathKeepAndWallsGatesNotAllowed, + DAT_PathFindingState::ptr)(x, (int)(DAT_UnitsState::instance.units[sVar1].y), -1, -1, 100000); + } + if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 != 0) { + _p = &DAT_PathFindingState::instance.searchQueue; + int _nextOffset1 = 0; + _offset1 = 0; + do { + _tile1 = _p->destinationsArray[0].tile1; + if (DAT_TileMapState::instance.WalkLayer[_tile1] + == DAT_PathFindingState::instance.searchGeneration) { + *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile3OrAnotherHelper + + _nextOffset1) = (int)DAT_TileMapState::instance.CertainPathLayer[_tile1]; + } else { + *(undefined4*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile3OrAnotherHelper + + _nextOffset1) = 10000000; + } + _nextOffset1 = _offset1 * 0xc + 0xc; + _index1 = _offset1 + 1; + _p = (PathFindingStatePartB*)(((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + + 200)) + ->destinationsArray + + _offset1 + 1); + _offset1 = _offset1 + 1; + } while (((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[_index1] + .tile1 + != 0); + } + do { + tribeID = 0; + bVar7 = false; + if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 == 0) + break; + iVar9 = 0; + do { + iVar2 = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile2OrAHelper + + iVar9); + if (!iVar2 + || (iVar3 + = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[1] + .tile1 + + iVar9), + iVar3 == 0) + || (iVar4 + = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[1] + .tile2OrAHelper + + iVar9), + iVar4 == 0)) + break; + iVar5 = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile3OrAnotherHelper + + iVar9); + iVar6 = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[1] + .tile3OrAnotherHelper + + iVar9); + if (iVar6 < iVar5) { + *(undefined4*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[1] + .tile1 + + iVar9) + = *(undefined4*)((int)&DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 + + iVar9); + bVar7 = true; + *(int*)((int)&DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 + iVar9) + = iVar3; + *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile2OrAHelper + + iVar9) = iVar4; + *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[1] + .tile2OrAHelper + + iVar9) = iVar2; + *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile3OrAnotherHelper + + iVar9) = iVar6; + *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[1] + .tile3OrAnotherHelper + + iVar9) = iVar5; + } + iVar9 = tribeID * 0xc + 0xc; + iVar2 = tribeID + 1; + tribeID = tribeID + 1; + } while (((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[iVar2] + .tile1 + != 0); + } while (bVar7); + iVar9 = 0; + if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 != 0) { + _p2 = &DAT_PathFindingState::instance.searchQueue; + int _nextOffset2 = 0; + _p2Tile2 = &DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper; + _counter2 = 0; + do { + if (*(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile3OrAnotherHelper + + _nextOffset2) + != 10000000) { + if (_counter2 != iVar9) { + ((PathHelper12*)(_p2Tile2 + -1))->tile1 = _p2->destinationsArray[0].tile1; + *_p2Tile2 = *( + int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile2OrAHelper + + _nextOffset2); + _p2Tile2[1] = *( + int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[0] + .tile3OrAnotherHelper + + _nextOffset2); + } + iVar9 = iVar9 + 1; + _p2Tile2 = _p2Tile2 + 3; + } + _nextOffset2 = _counter2 * 0xc + 0xc; + _index2 = _counter2 + 1; + _p2 = (PathFindingStatePartB*)(((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + + 200)) + ->destinationsArray + + _counter2 + 1); + _counter2 = _counter2 + 1; + } while (((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) + ->destinationsArray[_index2] + .tile1 + != 0); + } + DAT_PathFindingState::instance.searchQueue.destinationsArray[iVar9].tile1 = 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp new file mode 100644 index 0000000000..adf70e38d3 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -0,0 +1,96 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/UI/Enums/MenuViewType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitType; + using UI::Enums::MenuViewType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525F20 + BOOLEnum TribesState::spawnDeerLionOrRabbit(int tribeID, int param_2, UnitType unitType) + { + short* psVar1; + int sVar2; + int _finalTargetUnitID; + int iVar3; + uint _rng2; + int _terrainHeight; + int _unknown; + int _tribeSize; + int _unitX; + int _targetUnitID; + int _unitY; + if ((DAT_GameCore::instance.currentMenuViewType != UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) + && (0 < tribeID)) { + if (this->tribes[tribeID].predatorNearby != 0) { + return FALSE; + } + _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + _finalTargetUnitID = (int)_targetUnitID; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + _finalTargetUnitID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); + if (!_finalTargetUnitID) { + _finalTargetUnitID = (int)_targetUnitID; + } + } + _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2; + _terrainHeight = DAT_UnitsState::instance.units[_finalTargetUnitID].terrainOrClimbHeight; + _unknown = this->tribes[tribeID].herdMinSize; + _tribeSize = this->tribes[tribeID].size; + _unitX = DAT_UnitsState::instance.units[_finalTargetUnitID].x; + _unitY = DAT_UnitsState::instance.units[_finalTargetUnitID].y; + if (_tribeSize <= _unknown) { + param_2 = param_2 / 4; + } + psVar1 = (short*)&this->tribes[tribeID].field138_0x284; + *psVar1 = *psVar1 + 1; + if ((param_2 <= this->tribes[tribeID].field138_0x284) + && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field138_0x284 = 0, + _tribeSize < sVar2)) { + if ((unitType == Map::Units::UT_RABBIT) + && (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation)) { + iVar3 = 110; + } else { + iVar3 = ((_unknown <= _tribeSize) - 1 & 0xffffffb5) + 100; + } + if (iVar3 <= (int)(_rng2 & 0x7f)) { + _rng2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + 0, 0, (int)(_unitX * 8), (int)(_unitY * 8), (int)(_terrainHeight), unitType); + if (_rng2) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(_rng2, tribeID); + DAT_UnitsState::instance.units[_rng2].substate + = (byte)DAT_UnitsState::instance.units[_rng2].fixedRng & 3; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + DAT_UnitsState::instance.units[_rng2].antelopeBasedRngValue = 2; + } + DAT_UnitsState::instance.units[_rng2].disappearFadeAlphaCountdown = 0x20; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateAnimalHerdBehaviorState, this)( + tribeID); + } + return TRUE; + } + } + } + return FALSE; + } + return FALSE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp new file mode 100644 index 0000000000..88d1c8b2bd --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -0,0 +1,486 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/TrailType.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/Map/Units/UnitTypeInt.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_SH1_SiegeAdvancedMode.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Game::GameMode; + using Game::GameMode2; + using Game::TrailType; + using Map::Units::UnitType; + using Map::Units::UnitTypeInt; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00527440 + void TribesState::spawnQueuedReinforcementWaves() + { + int iVar3; + dword dVar4; + dword _tribeID1; + int iVar5; + int iVar6; + int* piVar7; + SiegeUnitCounts* pSVar8; + int _siegeInfoSubIndex; + int _playerID; + int _nextDestination; + int _tribeType; + UnitTypeInt _unitType; + int _destinationIndexTracker; + int _unitType1Count; + int local_4; + int _playerID_2; + int _count; + int _destinationIndex; + int _newCount; + _tribeType = 0; + bool bVar1 = false; + _unitType = ((UnitType)0); + _destinationIndexTracker = 0; + bool bVar2 = false; + if (DAT_GameCore::instance.gameMode_2 != Game::GM_EDITOR + && DAT_GameCore::instance.gameMode_2 != Game::GM_SIEGE_THAT + && (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY + || (((DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk + || (DAT_GameCore::instance.gameMode_2 == Game::GM_CRUSADER_TUTORIAL)) + || (0 < DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .keep.id)))) + && 0 <= DAT_GameState::instance.mapAndTime.countUpTo201) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateTribeUnitAssignments, this)(); + _playerID_2 = DAT_GameSynchronyState::instance.currentPlayerSlotID; + if ((DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk) + || (DAT_GameCore::instance.gameMode_2 == Game::GM_CRUSADER_TUTORIAL)) { + pSVar8 = &DAT_GameState::instance.mapAndTime.siegeInformation; + do { + iVar3 = 100; + if (!DAT_GameState::instance.mapAndTime.difficulty) { + if (_playerID_2 == 1) { + iVar3 = 0x43; + } else if (!DAT_SH1_SiegeAdvancedMode::instance) { + LAB_005279a0: + iVar3 = 167; + } + } else if (DAT_GameState::instance.mapAndTime.difficulty == 2) { + if (_playerID_2 == 1) { + iVar3 = 133; + } else if (!DAT_SH1_SiegeAdvancedMode::instance) { + iVar3 = 75; + } + } else if (DAT_GameState::instance.mapAndTime.difficulty == 3) { + if (_playerID_2 == 1) + goto LAB_005279a0; + if (!DAT_SH1_SiegeAdvancedMode::instance) { + iVar3 = 50; + } + } + pSVar8->archers = (pSVar8->archers * iVar3) / 100; + pSVar8 = (SiegeUnitCounts*)&pSVar8->crossbowmen; + } while ((int)pSVar8 < 0x117cea0); + _siegeInfoSubIndex = 0; + do { + if (DAT_GameState::instance.mapAndTime.startGoods[_siegeInfoSubIndex + 0x19] != 0) { + switch (_siegeInfoSubIndex) { + case 0: + _tribeType = 3; + _unitType = Map::Units::UT_E_ARCHER; + break; + case 1: + _tribeType = 7; + _unitType = Map::Units::UT_E_XBOW; + break; + case 2: + _tribeType = 5; + _unitType = Map::Units::UT_E_SPEAR; + break; + case 3: + _tribeType = 6; + _unitType = Map::Units::UT_E_PIKE; + break; + case 4: + _tribeType = 9; + _unitType = Map::Units::UT_E_MACE; + break; + case 5: + _tribeType = 8; + _unitType = Map::Units::UT_E_SWORD; + break; + case 6: + _tribeType = 10; + _unitType = Map::Units::UT_E_KNIGHT; + break; + case 7: + _tribeType = 4; + _unitType = Map::Units::UT_E_LADDER; + break; + case 8: + _tribeType = 0xb; + _unitType = Map::Units::UT_E_ENGINEER; + break; + case 9: + _tribeType = 0xc; + _unitType = Map::Units::UT_E_MONK; + break; + case 10: + _tribeType = 0x19; + _unitType = Map::Units::UT_A_ARCHER; + break; + case 0xb: + _tribeType = 0x1a; + _unitType = Map::Units::UT_A_SLAVE; + break; + case 0xc: + _tribeType = 0x1b; + _unitType = Map::Units::UT_A_SLINGER; + break; + case 0xd: + _tribeType = 0x1c; + _unitType = Map::Units::UT_A_ASSASSIN; + break; + case 0xe: + _tribeType = 0x1d; + _unitType = Map::Units::UT_A_HARCHER; + break; + case 0xf: + _tribeType = 0x1e; + _unitType = Map::Units::UT_A_SWORDSMAN; + break; + case 0x10: + _tribeType = 0x1f; + _unitType = Map::Units::UT_A_FIRETHROWER; + break; + case 0x11: + _tribeType = 0x18; + _unitType = Map::Units::UT_S_FBALLISTA; + } + do { + _count = DAT_GameState::instance.mapAndTime.startGoods[_siegeInfoSubIndex + 0x19]; + int _nUnits = _count; + if (9 < _count) { + _nUnits = 9; + } + DAT_GameState::instance.mapAndTime.startGoods[_siegeInfoSubIndex + 0x19] + = _count - _nUnits; + _destinationIndex = _destinationIndexTracker; + if (39 < _destinationIndexTracker) { + _destinationIndex = 0; + } + _nextDestination = _destinationIndexTracker + 1; + _tribeID1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeWithSpawnedUnit, + this)((short)_destinationIndexTracker, (undefined4)((int)(_tribeType)), + DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][_destinationIndex] + .x, + DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][_destinationIndex] + .y, + 2, (UnitType)((int)(_unitType)), _nUnits); + _newCount = DAT_GameState::instance.mapAndTime.startGoods[_siegeInfoSubIndex + 0x19]; + this->tribes[_tribeID1].unknownAttackRelatedUpdateCounter = 0x32; + _destinationIndexTracker = _nextDestination; + } while (_newCount); + } + _siegeInfoSubIndex = _siegeInfoSubIndex + 1; + iVar3 = DAT_MapPropertiesState::instance.SEC_Section1067.tunnelersCount; + } while (_siegeInfoSubIndex < 20); + while (iVar3) { + iVar6 = iVar3; + if (9 < iVar3) { + iVar6 = 9; + } + iVar5 = _destinationIndexTracker; + if (0x27 < _destinationIndexTracker) { + iVar5 = 0; + } + dVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeWithSpawnedUnit, this)( + (short)_destinationIndexTracker, 2, + DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][iVar5].x, + DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][iVar5].y, 2, + Map::Units::UT_TUNNELER, iVar6); + this->tribes[dVar4].unknownAttackRelatedUpdateCounter = 0x32; + iVar3 = iVar3 - iVar6; + _destinationIndexTracker = _destinationIndexTracker + 1; + } + DAT_GameState::instance.mapAndTime.countUpTo201 = -1; + } else if (DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) { + DAT_GameState::instance.mapAndTime.countUpTo201 + = DAT_GameState::instance.mapAndTime.countUpTo201 + 1; + if (200 < DAT_GameState::instance.mapAndTime.countUpTo201) { + DAT_GameState::instance.mapAndTime.countUpTo201 = 0; + iVar3 = 0; + while (true) { + if (iVar3 == 7) { + iVar3 = 8; + } + if (DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] != 0) + break; + iVar3 = iVar3 + 1; + if (9 < iVar3) + goto LAB_005276b5; + } + switch (iVar3) { + case 0: + _tribeType = 3; + _unitType = Map::Units::UT_E_ARCHER; + break; + case 1: + _tribeType = 7; + _unitType = Map::Units::UT_E_XBOW; + break; + case 2: + _tribeType = 5; + _unitType = Map::Units::UT_E_SPEAR; + break; + case 3: + _tribeType = 6; + _unitType = Map::Units::UT_E_PIKE; + break; + case 4: + _tribeType = 9; + _unitType = Map::Units::UT_E_MACE; + break; + case 5: + _tribeType = 8; + _unitType = Map::Units::UT_E_SWORD; + break; + case 6: + _tribeType = 10; + _unitType = Map::Units::UT_E_KNIGHT; + break; + case 8: + _tribeType = 0xb; + _unitType = Map::Units::UT_E_ENGINEER; + break; + case 9: + _tribeType = 0xc; + _unitType = Map::Units::UT_E_MONK; + } + iVar6 = DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19]; + _unitType1Count = iVar6; + if (9 < iVar6) { + _unitType1Count = 9; + } + _playerID = 1; + DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] = iVar6 - _unitType1Count; + piVar7 = &DAT_GameState::instance.playerDataArray[1].campground.xEntry; + do { + if ((DAT_GameSynchronyState::instance.currentPlayerFullIDArray[_playerID] != -1) + || (DAT_GameSynchronyState::instance.currentAIArray[_playerID] != 0)) { + dVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnUnitsIntoNewTribe, this)( + _destinationIndexTracker, _tribeType, (int)(*piVar7), (int)(piVar7[1]), _playerID, + (UnitType)((int)(_unitType)), ((UnitType)0), _unitType1Count, 0); + this->tribes[dVar4].unknownAttackRelatedUpdateCounter = 0x32; + bVar2 = true; + _destinationIndexTracker = _destinationIndexTracker + 1; + } + _playerID = _playerID + 1; + piVar7 = piVar7 + 0xe7d; + } while (_playerID < 9); + LAB_005276b5: + iVar3 = 10; + while (DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] == 0) { + iVar3 = iVar3 + 1; + if (0x13 < iVar3) + goto LAB_005277c5; + } + switch (iVar3) { + case 10: + _tribeType = 0x19; + _unitType = Map::Units::UT_A_ARCHER; + break; + case 0xb: + _tribeType = 0x1a; + _unitType = Map::Units::UT_A_SLAVE; + break; + case 0xc: + _tribeType = 0x1b; + _unitType = Map::Units::UT_A_SLINGER; + break; + case 0xd: + _tribeType = 0x1c; + _unitType = Map::Units::UT_A_ASSASSIN; + break; + case 0xe: + _tribeType = 0x1d; + _unitType = Map::Units::UT_A_HARCHER; + break; + case 0xf: + _tribeType = 0x1e; + _unitType = Map::Units::UT_A_SWORDSMAN; + break; + case 0x10: + _tribeType = 0x1f; + _unitType = Map::Units::UT_A_FIRETHROWER; + break; + case 0x11: + _tribeType = 0x18; + _unitType = Map::Units::UT_S_FBALLISTA; + } + iVar6 = DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19]; + _unitType1Count = iVar6; + if (9 < iVar6) { + _unitType1Count = 9; + } + iVar5 = 1; + DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] = iVar6 - _unitType1Count; + piVar7 = &DAT_GameState::instance.playerDataArray[1].campground.xEntry; + do { + if ((DAT_GameSynchronyState::instance.currentPlayerFullIDArray[iVar5] != -1) + || (DAT_GameSynchronyState::instance.currentAIArray[iVar5] != 0)) { + dVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnUnitsIntoNewTribe, this)( + _destinationIndexTracker, _tribeType, (int)(*piVar7), (int)(piVar7[1]), iVar5, + (UnitType)((int)(_unitType)), ((UnitType)0), _unitType1Count, 0); + this->tribes[dVar4].unknownAttackRelatedUpdateCounter = 0x32; + bVar2 = true; + _destinationIndexTracker = _destinationIndexTracker + 1; + } + iVar5 = iVar5 + 1; + piVar7 = piVar7 + 0xe7d; + } while (iVar5 < 9); + LAB_005277c5: + if (!bVar2) { + DAT_GameState::instance.mapAndTime.countUpTo201 = -1; + } + } + } else if ((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SKIRMISH_SINGLE_PLAYER) + || (!DAT_GameState::instance.mapAndTime.skirmishNoRushTicks)) { + DAT_GameState::instance.mapAndTime.countUpTo201 + = DAT_GameState::instance.mapAndTime.countUpTo201 + 1; + iVar3 = 200; + if (DAT_GameCore::instance.gameMode_2 == Game::GM_SKIRMISH_AND_MULTIPLAYER + && DAT_GameCore::instance.isSkirmishTrail == TRUE + && DAT_GameCore::instance.currentTrailType == Game::TT_EXTREME) { + iVar3 = 0x32; + } + if (iVar3 < DAT_GameState::instance.mapAndTime.countUpTo201) { + DAT_GameState::instance.mapAndTime.countUpTo201 = 0; + local_4 = 1; + _unitType1Count = 0x14; + piVar7 = &DAT_GameState::instance.playerDataArray[1].campground.xEntry; + do { + if ((DAT_GameSynchronyState::instance.currentPlayerFullIDArray[local_4] != -1) + || (DAT_GameSynchronyState::instance.currentAIArray[local_4] != 0)) { + iVar3 = 0; + do { + if (iVar3 == 7) { + iVar3 = 8; + } + if (*(int*)(DAT_GameState::instance.mapAndTime.unused_0x3ff00 + + (_unitType1Count + iVar3) * 4) + != 0) { + switch (iVar3) { + case 0: + _tribeType = 3; + _unitType = Map::Units::UT_E_ARCHER; + break; + case 1: + _tribeType = 7; + _unitType = Map::Units::UT_E_XBOW; + break; + case 2: + _tribeType = 5; + _unitType = Map::Units::UT_E_SPEAR; + break; + case 3: + _tribeType = 6; + _unitType = Map::Units::UT_E_PIKE; + break; + case 4: + _tribeType = 9; + _unitType = Map::Units::UT_E_MACE; + break; + case 5: + _tribeType = 8; + _unitType = Map::Units::UT_E_SWORD; + break; + case 6: + _tribeType = 10; + _unitType = Map::Units::UT_E_KNIGHT; + break; + case 8: + _tribeType = 0xb; + _unitType = Map::Units::UT_E_ENGINEER; + break; + case 9: + _tribeType = 0xc; + _unitType = Map::Units::UT_E_MONK; + break; + case 10: + _tribeType = 0x19; + _unitType = Map::Units::UT_A_ARCHER; + break; + case 0xb: + _tribeType = 0x1a; + _unitType = Map::Units::UT_A_SLAVE; + break; + case 0xc: + _tribeType = 0x1b; + _unitType = Map::Units::UT_A_SLINGER; + break; + case 0xd: + _tribeType = 0x1c; + _unitType = Map::Units::UT_A_ASSASSIN; + break; + case 0xe: + _tribeType = 0x1d; + _unitType = Map::Units::UT_A_HARCHER; + break; + case 0xf: + _tribeType = 0x1e; + _unitType = Map::Units::UT_A_SWORDSMAN; + break; + case 0x10: + _tribeType = 0x1f; + _unitType = Map::Units::UT_A_FIRETHROWER; + break; + case 0x11: + _tribeType = 0x18; + _unitType = Map::Units::UT_S_FBALLISTA; + } + iVar6 = *(int*)(DAT_GameState::instance.mapAndTime.unused_0x3ff00 + + (_unitType1Count + iVar3) * 4); + iVar5 = iVar6; + if (9 < iVar6) { + iVar5 = 9; + } + *(int*)(DAT_GameState::instance.mapAndTime.unused_0x3ff00 + + (_unitType1Count + iVar3) * 4) = iVar6 - iVar5; + dVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnUnitsIntoNewTribe, + this)(_destinationIndexTracker, _tribeType, (int)(*piVar7), + (int)(piVar7[1]), local_4, (UnitType)((int)(_unitType)), ((UnitType)0), + iVar5, 0); + this->tribes[dVar4].unknownAttackRelatedUpdateCounter = 0x32; + bVar1 = true; + _destinationIndexTracker = _destinationIndexTracker + 1; + break; + } + iVar3 = iVar3 + 1; + } while (iVar3 < 0x14); + } + _unitType1Count = _unitType1Count + 0x14; + local_4 = local_4 + 1; + piVar7 = piVar7 + 0xe7d; + } while (local_4 < 9); + if (!bVar1) { + DAT_GameState::instance.mapAndTime.countUpTo201 = -1; + } + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp new file mode 100644 index 0000000000..aff326bfa7 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp @@ -0,0 +1,36 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00523240 + dword TribesState::spawnUnitsAroundLocation( + undefined4 param_1, int aroundX, int aroundY, int playerID, UnitType unitType, int count) + { + if ((aroundX <= 0) && (aroundY <= 0)) { + return (dword)(0); + } + dword _tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); + if ((int)_tribeID <= 0) { + return (dword)(0); + } + this->tribes[_tribeID].someIndex = 0; + this->tribes[_tribeID].attackWave = 0; + this->tribes[_tribeID].tribeType = (AITribeTypeShort)param_1; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSuitableSpawnLocationUnk, + DAT_PathFindingState::ptr)(aroundX, aroundY, -1, -1, 2000, 0); + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::spawnUnitAndAddToTribe, + DAT_PathFindingState::ptr)(playerID, playerID, count, unitType, (undefined4)((int)(_tribeID))); + this->tribes[_tribeID].pendingScatterMove = 1; + return (dword)(_tribeID); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp new file mode 100644 index 0000000000..1a5364a2d7 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp @@ -0,0 +1,49 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/AI/AICState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_AICState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522F70 + void TribesState::spawnUnitsForAITribe( + undefined4 param_1, int param_2, int param_3, int param_4, UnitType param_5, int param_6, int param_7) + { + int unitID; + int aiUnitBehaviourType; + if (((0 < param_2) || (0 < param_3)) && 0 < param_6) { + int microXPosition = param_2 * 8; + param_2 = param_6; + do { + unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + param_4, param_4, microXPosition, param_3 * 8, 8, param_5); + if (unitID) { + DAT_UnitsState::instance.units[unitID].calculatedOwnerPlayerIndex = param_7; + UnitTypeShort UVar1 = DAT_UnitsState::instance.units[unitID].unitType; + if (UVar1 == Map::Units::UT_E_ARCHER || UVar1 == Map::Units::UT_E_XBOW + || UVar1 == Map::Units::UT_A_ARCHER || UVar1 == Map::Units::UT_A_SLINGER + || UVar1 == Map::Units::UT_A_HARCHER || UVar1 == Map::Units::UT_A_FIRETHROWER) { + aiUnitBehaviourType = 0x11; + } else { + aiUnitBehaviourType = 0x14; + } + MACRO_CALL_MEMBER(AI::AICState_Func::addUnitToItsTribe, DAT_AICState::ptr)( + unitID, aiUnitBehaviourType); + } + param_2 = param_2 + -1; + } while (param_2); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp new file mode 100644 index 0000000000..ab2ad6cee8 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp @@ -0,0 +1,62 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00523030 + dword TribesState::spawnUnitsIntoNewTribe(undefined4 counter, int tribeType, int x, int y, int playerID, + UnitType unitType, UnitType unitType2, int unitType1Count, int unitType2Count) + { + int _unitID; + uint _unitID2; + if ((x <= 0) && (y <= 0)) { + return (dword)(0); + } + dword _tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); + if (0 < (int)_tribeID) { + this->tribes[_tribeID].tribeType = (AITribeTypeShort)tribeType; + this->tribes[_tribeID].someIndex = (short)counter; + this->tribes[_tribeID].attackWave = (short)DAT_TroopValueState::instance.attackInfo.inv_count; + if (0 < unitType1Count) { + tribeType = unitType1Count; + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + playerID, playerID, x * 8, y * 8, 8, unitType); + if (_unitID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)( + _unitID, (int)(_tribeID)); + DAT_UnitsState::instance.units[_unitID].aiUnitBehaviourType = 0; + } + tribeType = tribeType + -1; + } while (tribeType); + } + if (0 < unitType2Count) { + int microYPosition = y * 8; + y = unitType2Count; + do { + _unitID2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + playerID, playerID, x * 8, microYPosition, 8, unitType2); + if (_unitID2) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)( + _unitID2, (int)(_tribeID)); + DAT_UnitsState::instance.units[_unitID2].aiUnitBehaviourType = 0; + } + y = y + -1; + } while (y); + } + this->tribes[_tribeID].pendingScatterMove = 1; + return (dword)(_tribeID); + } + return (dword)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp b/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp new file mode 100644 index 0000000000..5892fb88fb --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp @@ -0,0 +1,56 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Game/GameMode.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Commands::MappersEnum; + using Game::GameMode; + + // FUNCTION: STRONGHOLDCRUSADER 0x005262A0 + void TribesState::spawnWildlifeOrMercAtAvailableSlot() + { + int iVar1; + short* psVar3; + uint y; + uint x; + if ((DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) + || (0x9f < DAT_GameState::instance.mapAndTime.wildlifeCount)) {} + int iVar2 = 0; + psVar3 = DAT_GameState::instance.mapAndTime.playerPopulationStatistics[6] + 0xe4; + do { + if (iVar2 < 4) { + x = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar2][0]; + y = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar2][1]; + LAB_005262f6: + if (x < 400 && y < 400 && *(char*)(y * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x) != '\0' + && (iVar1 = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x, + (DAT_TileMapState::instance.LogicLayer[iVar1] & 0x50501581U) == 0)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( + Commands::M_MAPPER_RABBIT, x, y, (int)(DAT_TileMapState::instance.HeightLayer[iVar1])); + this->unknownX_01 = x; + this->unknownY_01 = y; + } + } else if (DAT_GameState::instance.mapAndTime.mercRecruitable[iVar2 + 4] != 0) { + x = *(uint*)(psVar3 + -2); + y = *(uint*)psVar3; + goto LAB_005262f6; + } + iVar2 = iVar2 + 1; + psVar3 = psVar3 + 400; + if (0xb < iVar2) {} + } while (true); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp new file mode 100644 index 0000000000..7a18ea0a4a --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp @@ -0,0 +1,39 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00525370 + void TribesState::standUpAllTribeUnits(int param_1) + { + int iVar1; + Tribe* psVar1; + psVar1 = &this->tribes[param_1]; + int unitSelectionIndex = 0; + if (0 < psVar1->size) { + do { + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::standUpIfSeated, DAT_UnitsState::ptr)(iVar1); + DAT_UnitsState::instance.units[iVar1].animationCycleNumber = 0; + DAT_UnitsState::instance.units[iVar1].rabbitMovementSlowdown = '\0'; + } + } while (unitSelectionIndex < psVar1->size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp b/src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp new file mode 100644 index 0000000000..06cf4de4e2 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp @@ -0,0 +1,55 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524890 + undefined4 TribesState::stopTribeMovementAndCheckIdle(int param_1) + { + short* psVar1; + int unitID; + int iVar2; + int unitSelectionIndex; + psVar1 = &this->tribes[param_1].size; + unitSelectionIndex = 0; + iVar2 = 0; + if (0 < *psVar1) { + do { + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[unitID].dying == 0)) { + if ((DAT_UnitsState::instance.units[unitID].usingTeleport == 0) + && (DAT_UnitsState::instance.units[unitID].field303_0x413 == 0)) { + if (DAT_UnitsState::instance.units[unitID].state.generic + != Map::Units::States::US_MOVE_TO_DESTINATION) + continue; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(unitID); + } + iVar2 = iVar2 + 1; + } + } while (unitSelectionIndex < *psVar1); + if (iVar2) { + return (undefined4)(0); + } + } + return (undefined4)(1); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp b/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp new file mode 100644 index 0000000000..4d8a3c25e7 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp @@ -0,0 +1,27 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00521240 + void TribesState::swapTribeOwnership(int param_1, int param_2) + { + Tribe* piVar1; + piVar1 = &this->tribes[1]; + do { + if (piVar1->tribeState == 2) { + if (piVar1->owner == param_1) { + piVar1->owner = param_2; + } else if (piVar1->owner == param_2) { + piVar1->owner = param_1; + } + } + piVar1 = piVar1 + 0xcd; + } while ((int)piVar1 < 0x176238c); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp b/src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp new file mode 100644 index 0000000000..6118251442 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp @@ -0,0 +1,37 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x005241C0 + undefined4 TribesState::tribeContainsUnitThatCanClimb(int param_1) + { + int iVar1; + int iVar2 = (int)this->tribes[param_1].size; + int unitSelectionIndex = 0; + if (0 < iVar2) { + do { + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar1].dying == 0 + && DAT_UnitsState::instance.units[iVar1].unitCanClimb != 0) { + return (undefined4)(1); + } + } while (unitSelectionIndex < iVar2); + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp b/src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp new file mode 100644 index 0000000000..e99df02c5f --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp @@ -0,0 +1,42 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524140 + undefined4 TribesState::tribeHasActiveLaddermanUnit(int param_1) + { + int iVar1; + int iVar2 = (int)this->tribes[param_1].size; + int unitSelectionIndex = 0; + if (0 < iVar2) { + do { + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar1].dying == 0 + && DAT_UnitsState::instance.units[iVar1].unitType == Map::Units::UT_E_LADDER + && DAT_UnitsState::instance.units[iVar1].state.generic == (UnitState)3) { + return (undefined4)(1); + } + } while (unitSelectionIndex < iVar2); + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp new file mode 100644 index 0000000000..4fabbd1e4b --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -0,0 +1,44 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522EF0 + void TribesState::trimTribeToSize(undefined4 tribeID, int smallerSize) + { + UnitLogicStateShort* pUVar1; + UnitLogicStateShort UVar2; + int iVar3; + int unitSelectionIndex; + int _currentTribeID; + _currentTribeID = DAT_CurrentTribeID::instance; + unitSelectionIndex = 0; + if (this->tribes[DAT_CurrentTribeID::instance].tribeState != 0 + && (iVar3 = (int)this->tribes[DAT_CurrentTribeID::instance].size, smallerSize <= iVar3) && 0 < iVar3) { + do { + iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _currentTribeID, unitSelectionIndex); + pUVar1 = &DAT_UnitsState::instance.units[iVar3].logicalState; + UVar2 = *pUVar1; + unitSelectionIndex = unitSelectionIndex + 1; + if ((((UVar2 == ((UnitLogicState)1)) || (UVar2 == Map::Units::ULS_NORMAL)) + || (UVar2 == Map::Units::ULS_TRANSITIONING)) + && smallerSize <= unitSelectionIndex) { + *pUVar1 = Map::Units::ULS_REMOVE; + } + } while (unitSelectionIndex < this->tribes[_currentTribeID].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp new file mode 100644 index 0000000000..c5c9bb6c69 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp @@ -0,0 +1,60 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/UI/Enums/MenuViewType.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Commands::MappersEnum; + using UI::Enums::MenuViewType; + + // FUNCTION: STRONGHOLDCRUSADER 0x005260B0 + undefined4 TribesState::trySpawnAdditionalWildlifeForTribe(int param_1, int param_2, int param_3, int param_4) + { + if ((DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) + || (param_1 <= 0)) { + return (undefined4)(0); + } + if (this->tribes[param_1].predatorNearby == 0 + && (DAT_GameCore::instance.missionNumber1to20 != 0x26 || (param_4 != 0x2f))) { + int iVar7 = (int)this->tribes[param_1].selectionTargetUnitID; + int iVar6 = iVar7; + if (param_4 == 0x2c) { + iVar6 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(param_1); + if (!iVar6) { + iVar6 = iVar7; + } + } + byte bVar5 = (byte)SEC_RNG::instance.currentNumber2; + int sVar2 = DAT_UnitsState::instance.units[iVar6].x; + int sVar3 = DAT_UnitsState::instance.units[iVar6].y; + int sVar4 = DAT_UnitsState::instance.units[iVar6].terrainOrClimbHeight; + this->tribes[param_1].field_0x288 = this->tribes[param_1].field_0x288 + 1; + if (param_2 <= this->tribes[param_1].field_0x288 + && (this->tribes[param_1].field_0x288 = 0, 99 < (bVar5 & 0x7f))) { + iVar6 = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::countLivingNondyingUnitsForPlayer, DAT_UnitsState::ptr)(param_4); + if (iVar6 < param_3) { + if (param_4 == 0x2f) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( + Commands::M_MAPPER_CAMEL, (uint)(sVar2), (uint)(sVar3), (int)(sVar4)); + } + return (undefined4)(1); + } + } + return (undefined4)(0); + } + return (undefined4)(0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp new file mode 100644 index 0000000000..b6e7f3d1f3 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp @@ -0,0 +1,24 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522550 + BOOLEnum TribesState::unitIsSelectedByPlayer(uint tribeID) + { + uint uVar1 = tribeID & 0x8000000f; + if ((int)uVar1 < 0) { + uVar1 = (uVar1 - 1 | 0xfffffff0) + 1; + } + return (uint)((DAT_UnitSelectionDefinedData::instance.BitMaskHelper[uVar1] + & *(ushort*)(DAT_UnitsState::instance.selectedUnitsBitFlags + ((int)tribeID / 16) * 2))); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp b/src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp new file mode 100644 index 0000000000..5b4dd5991c --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp @@ -0,0 +1,34 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00524000 + void TribesState::unsetRallyRelatedFlagOnUnits(int tribeID) + { + int _unitID; + int _unitSelectionIndex = 0; + if (0 < this->tribes[tribeID].size) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitSelectionIndex); + _unitSelectionIndex = _unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID].dying == 0)) { + DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; + } + } while (_unitSelectionIndex < this->tribes[tribeID].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp b/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp new file mode 100644 index 0000000000..4ab40c4188 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp @@ -0,0 +1,41 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00523590 + void TribesState::updateAnimalHerdBehaviorState(int tribeID) + { + int iVar1; + int unitSelectionIndex; + if (0 < tribeID && this->tribes[tribeID].predatorNearby == 0 + && (unitSelectionIndex = 0, 0 < this->tribes[tribeID].size)) { + do { + iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar1].dying == 0 + && DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue == 2) { + uint uVar2 = DAT_UnitsState::instance.units[iVar1].fixedRng & 7; + if (uVar2 < 2) { + DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue = 0; + } else { + DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue = (5 < uVar2) + 1; + } + } + } while (unitSelectionIndex < this->tribes[tribeID].size); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp new file mode 100644 index 0000000000..3542c57470 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -0,0 +1,114 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/WildlifeState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/UI/Enums/MenuViewType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_WildlifeState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using UI::Enums::MenuViewType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052B630 + void TribesState::updateCamelTribeBehavior(int param_1) + { + short* psVar1; + SomeTribeBehaviorTypeShort SVar2; + BOOLEnum BVar3; + int iVar4; + int sVar5; + BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( + param_1, this->tribes[param_1].uid); + psVar1 = &this->tribes[param_1].unknownCounter01; + *psVar1 = *psVar1 + 1; + sVar5 = this->tribes[param_1].unknownCounter01; + this->tribes[param_1].unkIsAnimalTribe = 1; + if (8000 <= sVar5) { + this->tribes[param_1].unknownCounter01 = 4000; + } + if ((BVar3) + && (iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trySpawnAdditionalWildlifeForTribe, this)( + param_1, 1000, 10, 0x2f), + iVar4 != 0)) { + this->tribes[param_1].initialMoveOrderGiven = 1; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + } + sVar5 = this->tribes[param_1].initialMoveOrderGiven; + if (!sVar5) { + sVar5 = this->tribes[param_1].selectionTargetUnitID; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + this->tribes[param_1].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, + (uint)(DAT_UnitsState::instance.units[sVar5].x), (uint)(DAT_UnitsState::instance.units[sVar5].y), 0, + 0, Map::Units::Instructions::UMSE_0); + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + } + if (DAT_GameCore::instance.currentMenuViewType != UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + if (sVar5 == 1) { + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + this->tribes[param_1].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(param_1, 2, 0); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); + sVar5 = this->tribes[param_1].currentRallyPointIndex; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, + (uint)((int)this->tribes[param_1].rallyPointArray[sVar5][0]), + (uint)((int)this->tribes[param_1].rallyPointArray[sVar5][1]), 0, 0, + Map::Units::Instructions::UMSE_0); + psVar1 = &this->tribes[param_1].currentRallyPointIndex; + *psVar1 = *psVar1 + 1; + if (this->tribes[param_1].rallyPointCount <= this->tribes[param_1].currentRallyPointIndex) { + this->tribes[param_1].rallyPointCount = 0; + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + } + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + } + SVar2 = this->tribes[param_1].tribeBehaviorType; + if (SVar2 == (SomeTribeBehaviorType)0) { + sVar5 = this->tribes[param_1].unknownAttackRelatedUpdateCounter; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = sVar5 + 1; + if (!sVar5) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(param_1, 2, 1); + if (0 < this->tribes[param_1].rallyPointCount) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); + iVar4 = (int)this->tribes[param_1].rallyPointCount; + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + param_1, (uint)((int)this->tribes[param_1].rallyPointArray[iVar4 + -1][0]), + (uint)((int)this->tribes[param_1].rallyPointArray[iVar4 + -1][1]), 0, 0); + } + } else if (BVar3) { + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + } + } else if (SVar2 == Map::Units::STBT_1) { + sVar5 = 800; + if (this->tribes[param_1].unknownCounter01 < 0x65) { + sVar5 = 10; + } + psVar1 = (short*)&this->tribes[param_1].unknownAttackRelatedUpdateCounter; + *psVar1 = *psVar1 + 1; + if (sVar5 <= this->tribes[param_1].unknownAttackRelatedUpdateCounter) { + this->tribes[param_1].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + } + } + } + this->tribes[param_1].unknownCounter01 = 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp new file mode 100644 index 0000000000..aeb5b26636 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -0,0 +1,123 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/WildlifeState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/UI/Enums/MenuViewType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_WildlifeState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + using Map::Units::UnitType; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using UI::Enums::MenuViewType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052B110 + void TribesState::updateLionWolfTribeBehavior(int param_1) + { + short* psVar1; + int sVar2; + SomeTribeBehaviorTypeShort SVar3; + int sVar4; + BOOLEnum BVar5; + BOOLEnum BVar6; + BVar5 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( + param_1, this->tribes[param_1].uid); + psVar1 = &this->tribes[param_1].unknownCounter01; + *psVar1 = *psVar1 + 1; + sVar2 = this->tribes[param_1].unknownCounter01; + this->tribes[param_1].unkIsAnimalTribe = 1; + if (8000 <= sVar2) { + this->tribes[param_1].unknownCounter01 = 4000; + } + sVar2 = this->tribes[param_1].countdown; + if (!sVar2) { + if ((BVar5) + && (BVar6 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + param_1, 500, Map::Units::UT_LIONSHWOLF), + BVar6)) { + this->tribes[param_1].initialMoveOrderGiven = 1; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + } + sVar2 = this->tribes[param_1].initialMoveOrderGiven; + if (!sVar2) { + sVar2 = this->tribes[param_1].selectionTargetUnitID; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + this->tribes[param_1].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, + (uint)(DAT_UnitsState::instance.units[sVar2].x), + (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, 0, Map::Units::Instructions::UMSE_0); + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + } + if (sVar2 == 1) { + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + this->tribes[param_1].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(param_1, 2, 0); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); + sVar2 = this->tribes[param_1].currentRallyPointIndex; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, + (uint)((int)this->tribes[param_1].rallyPointArray[sVar2][0]), + (uint)((int)this->tribes[param_1].rallyPointArray[sVar2][1]), 0, 0, + Map::Units::Instructions::UMSE_0); + psVar1 = &this->tribes[param_1].currentRallyPointIndex; + *psVar1 = *psVar1 + 1; + if (this->tribes[param_1].rallyPointCount <= this->tribes[param_1].currentRallyPointIndex) { + this->tribes[param_1].rallyPointCount = 0; + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + } + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + } + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[param_1].unknownCounter01 = 0; + } + SVar3 = this->tribes[param_1].tribeBehaviorType; + if (SVar3 == (SomeTribeBehaviorType)0) { + sVar4 = this->tribes[param_1].unknownAttackRelatedUpdateCounter; + sVar2 = sVar4 + 1; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = sVar2; + if (!sVar4) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(param_1, 2, 0); + if (0 < this->tribes[param_1].rallyPointCount) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyLadderDestructionToTribeUnits, this)( + param_1); + } + } else if (sVar2 == 0x96) { + if ((this->tribes[param_1].unknownBool02 == 0) + && (sVar2 = this->tribes[param_1].rallyPointCount, 0 < sVar2)) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + param_1, (uint)((int)this->tribes[param_1].rallyPointArray[sVar2 + -1][0]), + (uint)((int)this->tribes[param_1].rallyPointArray[sVar2 + -1][1]), 0, 0); + } + } else if (BVar5) { + this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + } + } else if ((SVar3 == Map::Units::STBT_1) + && (psVar1 = (short*)&this->tribes[param_1].unknownAttackRelatedUpdateCounter, + *psVar1 = *psVar1 + 1, this->tribes[param_1].unknownAttackRelatedUpdateCounter == 300)) { + this->tribes[param_1].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + } + } else if (sVar2 == 100) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setStateForAllTribeUnits, this)(param_1, 0xd0); + this->tribes[param_1].unknownBool01 = 1; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp b/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp new file mode 100644 index 0000000000..6494e2454f --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp @@ -0,0 +1,77 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00522210 + void TribesState::updatePeasantSeatingAtBuilding(int param_1, int param_2, int param_3) + { + uint uVar2; + short* psVar3; + int iVar4; + int local_8; + short sVar1 = DAT_BuildingsState::instance.buildings[param_1].owner; + local_8 = 0; + if (param_2 < 2) { + param_2 = 1; + } else if (param_2 < 4) { + param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 1) + 1; + } else { + if (param_2 < 8) { + uVar2 = (byte)SEC_RNG::instance.currentNumber2 & 3; + } else { + if (param_2 < 0x10) { + param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 1; + goto LAB_00522291; + } + uVar2 = (byte)SEC_RNG::instance.currentNumber2 & 0xf; + } + param_2 = uVar2 + 1; + } + LAB_00522291: + iVar4 = 1; + if (1 < DAT_UnitsState::instance.maxUnitCount) { + psVar3 = &DAT_UnitsState::instance.units[1].dying; + do { + if (psVar3[-0x10a] == Map::Units::ULS_NORMAL && *psVar3 == 0 && psVar3[-0x105] == sVar1 + && psVar3[-0x109] == Map::Units::UT_PEASANT + && ((UnitStateUnion*)(psVar3 + 0x10))->generic == Map::Units::States::US_STAND_UPUnk + && psVar3[0x4c] == param_1) { + local_8 = local_8 + 1; + psVar3[0x78] = 0; + if (param_3 == 1) { + psVar3[0x77] = 0; + } + if (local_8 == param_2) { + psVar3[0x77] = 1; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::standUpIfSeated, DAT_UnitsState::ptr)(iVar4); + } else if (psVar3[0x77] == 0) { + psVar3[0x78] = 1; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::sitDownIfStanding, DAT_UnitsState::ptr)( + iVar4); + } + } + iVar4 = iVar4 + 1; + psVar3 = psVar3 + 0x248; + } while (iVar4 < DAT_UnitsState::instance.maxUnitCount); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp new file mode 100644 index 0000000000..429830d4d9 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -0,0 +1,136 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/WildlifeState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/UI/Enums/MenuViewType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_WildlifeState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + using Map::Units::UnitType; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using UI::Enums::MenuViewType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052B390 + void TribesState::updateRabbitTribeBehavior(int tribeID) + { + short* psVar1; + int sVar2; + SomeTribeBehaviorTypeShort SVar3; + int sVar4; + int iVar5; + int iVar6; + BOOLEnum BVar7; + int iVar8; + int _x10; + int _y10; + int _targetUnitID; + sVar2 = this->tribes[tribeID].selectionTargetUnitID; + _x10 = (int)DAT_UnitsState::instance.units[sVar2].x / 10; + _y10 = (int)DAT_UnitsState::instance.units[sVar2].y / 10; + iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; + iVar6 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; + BVar7 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( + tribeID, this->tribes[tribeID].uid); + this->tribes[tribeID].unkIsAnimalTribe = 1; + iVar8 = 80; + if (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation) { + iVar8 = 3; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + tribeID, iVar8, Map::Units::UT_RABBIT); + if (this->tribes[tribeID].initialMoveOrderGiven == 0) { + _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].initialMoveOrderGiven = 1; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + (uint)(DAT_UnitsState::instance.units[_targetUnitID].x), + (uint)(DAT_UnitsState::instance.units[_targetUnitID].y), 0, 0, Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + } + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[tribeID].unknownCounter01 = 0; + } + sVar2 = this->tribes[tribeID].predatorNearby; + if (!sVar2) { + SVar3 = this->tribes[tribeID].tribeBehaviorType; + if (SVar3 == (SomeTribeBehaviorType)0) { + sVar4 = this->tribes[tribeID].unknownAttackRelatedUpdateCounter; + sVar2 = sVar4 + 1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = sVar2; + if (!sVar4) { + BVar7 = MACRO_CALL_MEMBER( + Map::WildlifeState_Func::buildRallyPointPathForTribe, DAT_WildlifeState::ptr)(tribeID, 4); + if (!BVar7) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 2, 1); + } + if (0 < this->tribes[tribeID].rallyPointCount) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyLadderDestructionToTribeUnits, this)( + tribeID); + } + } else if (sVar2 == 0x96) { + sVar2 = this->tribes[tribeID].rallyPointCount; + if (0 < sVar2) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2 + -1][0]), + (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2 + -1][1]), 0, 0); + } + } else if (BVar7) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + } + } else if (SVar3 == Map::Units::STBT_1) { + if (!iVar6) { + if (iVar5 < 0x15) { + this->tribes[tribeID].wanderIntervalTicks = 400; + } else { + this->tribes[tribeID].wanderIntervalTicks = 0x14; + } + } else { + this->tribes[tribeID].wanderIntervalTicks = 0x14; + } + psVar1 = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; + *psVar1 = *psVar1 + 1; + if (this->tribes[tribeID].wanderIntervalTicks + <= this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { + this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + } + } + } else { + if (sVar2 == 1) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 3, 0); + if (this->tribes[tribeID].rallyPointCount <= 0) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 3, 1); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); + } + psVar1 = (short*)&this->tribes[tribeID].predatorNearby; + *psVar1 = *psVar1 + 1; + if (2999 < this->tribes[tribeID].predatorNearby) { + this->tribes[tribeID].predatorNearby = 0; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp new file mode 100644 index 0000000000..1fb2311a9f --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp @@ -0,0 +1,243 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::Behavior::UnitStanceEnum; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052A7D0 + void TribesState::updateTribeCombatStanceBehavior(int tribeID) + { + short* psVar1; + UnitTypeShort UVar2; + int _unitID; + BOOLEnum BVar3; + int iVar4; + int _targetUnitID; + int iVar5; + uint x1; + uint y1; + int local_c; + int _buildingID; + int _buildingIndex; + short _buildingIndex_2; + UnitInstructionType _unitInstructionType; + _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; + _buildingIndex_2 = this->tribes[tribeID].someUnitID; + if (!_buildingIndex_2) { + if (this->tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { + local_c = 0; + do { + iVar5 = ((this->tribes[tribeID].someUnitArrayIndex - local_c) + 10) % 10; + _unitID = (int)this->tribes[tribeID].someUnitArray[iVar5]; + if (_unitID) { + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].uid + == this->tribes[tribeID].someUnitUIDArray[iVar5] + && DAT_GameState::instance.mapAndTime + .playerTeams[DAT_UnitsState::instance.units[_targetUnitID].owner] + != DAT_GameState::instance.mapAndTime + .playerTeams[DAT_UnitsState::instance.units[_unitID].owner] + && DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[_targetUnitID].tile] + == DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[_unitID].tile]) { + iVar5 = (int)DAT_UnitsState::instance.units[_targetUnitID].microXPosition + - (int)DAT_UnitsState::instance.units[_unitID].microXPosition; + iVar4 = (int)DAT_UnitsState::instance.units[_targetUnitID].microYPosition + - (int)DAT_UnitsState::instance.units[_unitID].microYPosition; + if (iVar5 * iVar5 + iVar4 * iVar4 < 250000) { + if ((this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 0) + && (this->tribes[tribeID].aggressiveStanceCounter == 0x3b)) { + iVar5 = 0; + if (this->tribes[tribeID].size < 1) + goto LAB_0052aa55; + goto LAB_0052a9f2; + } + break; + } + } else { + this->tribes[tribeID].someUnitArray[iVar5] = 0; + } + } + local_c = local_c + 1; + } while (local_c < 10); + } + } else { + iVar5 = (int)_buildingIndex_2; + if (DAT_UnitsState::instance.units[iVar5].uid == this->tribes[tribeID].someUnitUID) { + if ((int)DAT_UnitsState::instance.units[iVar5].destinationX_2Unk + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[iVar5].destinationY_2Unk] + .addXgetTile + == this->tribes[tribeID].someTile2) { + _buildingIndex_2 = this->tribes[tribeID].field166_0x2be; + if (_buildingIndex_2) { + this->tribes[tribeID].field166_0x2be = _buildingIndex_2 + -1; + goto LAB_0052aa77; + } + if (DAT_UnitsState::instance.units[iVar5].tile == this->tribes[tribeID].someTile) + goto LAB_0052aa77; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + tribeID, Map::Units::UIT_UNIT_ATTACK_UNIT, iVar5, this->tribes[tribeID].someUnitUID, 1); + } else { + this->tribes[tribeID].someUnitID = 0; + } + } + goto LAB_0052aa77; + LAB_0052a9f2: + do { + iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar5); + iVar5 = iVar5 + 1; + if (DAT_UnitsState::instance.units[iVar4].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar4].dying == 0 + && DAT_UnitsState::instance.units[iVar4].state.generic != Map::Units::States::US_MELEE_ATTACK) { + switch (DAT_UnitsState::instance.units[iVar4].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_S_BATTERINGRAM: + if (DAT_UnitsState::instance.units[iVar4].SA == 0) + goto LAB_0052aa77; + } + } + } while (iVar5 < this->tribes[tribeID].size); + LAB_0052aa55: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + tribeID, ((UnitInstructionType)0x20), _unitID, DAT_UnitsState::instance.units[_unitID].uid, 0); + LAB_0052aa77: + psVar1 = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; + *psVar1 = *psVar1 + 1; + if (0x32 < this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { + if (this->tribes[tribeID].isRallyingUnk == 0) { + if (this->tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { + psVar1 = &this->tribes[tribeID].aggressiveStanceCounter; + *psVar1 = *psVar1 + 1; + if (this->tribes[tribeID].aggressiveStanceCounter < 0x3c) {} + _buildingIndex_2 = this->tribes[tribeID].size; + this->tribes[tribeID].aggressiveStanceCounter = 0; + if (0 < _buildingIndex_2) { + iVar5 = 0; + do { + iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar5); + iVar5 = iVar5 + 1; + if (DAT_UnitsState::instance.units[iVar4].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar4].dying == 0 + && DAT_UnitsState::instance.units[iVar4].state.generic + != Map::Units::States::US_MELEE_ATTACK) { + switch (DAT_UnitsState::instance.units[iVar4].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_S_BATTERINGRAM: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + if (DAT_UnitsState::instance.units[iVar4].SA == 0) + goto LAB_0052ac8d; + } + } + } while (iVar5 < this->tribes[tribeID].size); + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::aggressiveStanceTargetBuildingAtRange, + DAT_PathFindingState::ptr)(_targetUnitID, (int)(15)); + if (DAT_PathFindingState::instance.ALG_ResultTile) { + _buildingIndex_2 = DAT_TileMapState::instance + .BuildingLayer[DAT_PathFindingState::instance.ALG_ResultTile]; + if (!_buildingIndex_2) { + _buildingID = 0; + _unitInstructionType = ((UnitInstructionType)0x25); + _buildingIndex = DAT_PathFindingState::instance.ALG_ResultTile; + } else { + _buildingID = DAT_BuildingsState::instance.buildings[_buildingIndex_2].uid; + _unitInstructionType = ((UnitInstructionType)0x26); + _buildingIndex = (int)_buildingIndex_2; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + tribeID, _unitInstructionType, _buildingIndex, _buildingID, 0); + } + } + } else { + BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::allUnitsReachedTheirDestination, this)( + tribeID); + if (!BVar3) {} + _buildingIndex_2 = this->tribes[tribeID].rallyPointCount; + iVar5 = this->tribes[tribeID].currentRallyPointIndex + 1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + if (_buildingIndex_2 <= iVar5) { + if (this->tribes[tribeID].isRallyingUnk == -1) { + this->tribes[tribeID].currentRallyPointIndex = 0; + this->tribes[tribeID].isRallyingUnk = 0; + } + iVar5 = 0; + } + if (this->tribes[tribeID].isRallyingUnk != 0) { + x1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][0]; + y1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][1]; + this->tribes[tribeID].currentRallyPointIndex = (short)iVar5; + UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType; + if (UVar2 == Map::Units::UT_E_ARCHER || UVar2 == Map::Units::UT_E_XBOW + || UVar2 == Map::Units::UT_A_ARCHER || UVar2 == Map::Units::UT_A_SLINGER + || UVar2 == Map::Units::UT_A_FIRETHROWER) { + if (0x1b0 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + } + } else if (UVar2 == Map::Units::UT_A_HARCHER) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, x1, y1, 0, 0); + } else if ((0x60 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) + || (this->tribes[tribeID].unitStance != Map::Units::Behavior::USE_AGGRESSIVE)) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, x1, y1, 0, 0); + } + } + } + LAB_0052ac8d: + if (this->tribes[tribeID].pendingScatterMove != 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::moveTribeToIndexedNearbyTile, this)(tribeID); + this->tribes[tribeID].pendingScatterMove = 0; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp new file mode 100644 index 0000000000..ee1629c340 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp @@ -0,0 +1,49 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00523F70 + undefined4 TribesState::updateTribeRallyFlags(int param_1) + { + short* psVar1; + int iVar2; + undefined4 uVar3; + int unitSelectionIndex; + undefined4 local_4; + short _isRallying; + psVar1 = &this->tribes[param_1].size; + unitSelectionIndex = 0; + local_4 = 0; + uVar3 = 0; + if (0 < *psVar1) { + do { + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar2].dying == 0 + && (_isRallying = DAT_UnitsState::instance.units[iVar2].goToRallyPoint, + DAT_UnitsState::instance.units[iVar2].rallyRelatedFlag = 0, + _isRallying != 0 + || (DAT_UnitsState::instance.units[iVar2].isSelectable_OR_matchTime == 0))) { + local_4 = 1; + DAT_UnitsState::instance.units[iVar2].rallyRelatedFlag = 1; + } + uVar3 = local_4; + } while (unitSelectionIndex < *psVar1); + } + return (undefined4)(uVar3); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp new file mode 100644 index 0000000000..580e95a6de --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp @@ -0,0 +1,73 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + + // FUNCTION: STRONGHOLDCRUSADER 0x00527330 + void TribesState::updateTribeUnitAssignments() + { + uint _unitID; + this->clans = 0; + this->field2_0x8 = 1; + DAT_CurrentTribeID::instance = 1; + do { + int _state = this->tribes[DAT_CurrentTribeID::instance].tribeState; + int _tribeUnitIndex = 0; + if (_state) { + int _tribeID = DAT_CurrentTribeID::instance; + if (_state != 3) { + int _tribeSize = this->tribes[DAT_CurrentTribeID::instance].size; + if (_tribeSize <= 0) { + int _tribeTime = this->tribes[DAT_CurrentTribeID::instance].time; + if (_tribeTime && (int)DAT_GameCore::instance.mapTimeInTicks <= (int)_tribeTime + && _tribeTime) + goto LAB_00527425; + } else { + int local_4 = 0; + if (0 < _tribeSize) { + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, + this)(_tribeID, _tribeUnitIndex); + int _unitLogicalState = DAT_UnitsState::instance.units[_unitID].logicalState; + _tribeUnitIndex = _tribeUnitIndex + 1; + if ((short)_unitLogicalState < 1 + || (2 < (short)_unitLogicalState + && (_unitLogicalState != Map::Units::ULS_TRANSITIONING)) + || DAT_UnitsState::instance.units[_unitID].dying != 0 + || DAT_UnitsState::instance.units[_unitID].tribeID != _tribeID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, this)( + _unitID, _tribeID); + _tribeID = DAT_CurrentTribeID::instance; + } else { + local_4 = local_4 + 1; + } + } while (_tribeUnitIndex < _tribeSize); + if (local_4) { + this->clans = this->clans + 1; + this->field2_0x8 = DAT_CurrentTribeID::instance; + goto LAB_00527425; + } + } + } + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::clearTribe, this)(_tribeID); + } + LAB_00527425: + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; + if (0x4e1 < DAT_CurrentTribeID::instance) {} + } while (true); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp new file mode 100644 index 0000000000..17eb7a7822 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp @@ -0,0 +1,133 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/TroopValueState.func.hpp" +#include "OpenSHC/AI/Tribes/AIVUnitTypeTribeArrayOffset.hpp" +#include "OpenSHC/Game/GameMode.hpp" + +#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Game::GameMode; + + // FUNCTION: STRONGHOLDCRUSADER 0x0052B860 + void TribesState::updateTribes() + { + short* psVar1; + uint _tickBasedNumberUpTo32; + _tickBasedNumberUpTo32 = DAT_GameCore::instance.mapTimeInTicks & 0x1f; + DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation + = DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation + -1; + DAT_CurrentTribeID::instance = 1; + do { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 2) { + if (0 < this->tribes[DAT_CurrentTribeID::instance].countdown2) { + psVar1 = &this->tribes[DAT_CurrentTribeID::instance].countdown2; + *psVar1 = *psVar1 + -1; + } + if ((DAT_CurrentTribeID::instance & 0x1f) == _tickBasedNumberUpTo32) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::computeTribePercentages, this)( + DAT_CurrentTribeID::instance); + } + if ((DAT_GameSynchronyState::instance + .currentPlayerFullIDArray[this->tribes[DAT_CurrentTribeID::instance].owner] + != -1) + || (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY)) { + /* + For humans, do: + */ + switch (this->tribes[DAT_CurrentTribeID::instance].tribeType) { + case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN: + goto switchD_0052b92a_caseD_d; + case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN: + goto switchD_0052b92a_caseD_e; + case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN: + goto switchD_0052b92a_caseD_f; + case ((AITribeType)0x10): + goto switchD_0052b92a_caseD_10; + default: + goto switchD_0052b92a_caseD_4; + } + } + switch (this->tribes[DAT_CurrentTribeID::instance].tribeType) { + case AI::Tribes::AITT_TUNNELERS: + case AI::Tribes::AITT_ARCHERS: + case AI::Tribes::AITT_LADDERMEN: + case AI::Tribes::AITT_SPEARMEN: + case AI::Tribes::AITT_PIKEMEN: + case AI::Tribes::AITT_CROSSBOWMEN: + case AI::Tribes::AITT_SWORDSMEN: + case AI::Tribes::AITT_MACEMEN: + case AI::Tribes::AITT_KNIGHTS: + case AI::Tribes::AITT_ENGINEERS: + case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_LADDERMEN: + case ((AITribeType)0x13): + case ((AITribeType)0x14): + case ((AITribeType)0x15): + case ((AITribeType)0x16): + case ((AITribeType)0x17): + case ((AITribeType)0x18): + case ((AITribeType)0x19): + case ((AITribeType)0x1a): + case ((AITribeType)0x1b): + case ((AITribeType)0x1c): + case ((AITribeType)0x1d): + case AI::Tribes::OFFSET_CROSSBOWMAN: + case ((AITribeType)0x1f): + MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::updateTribeBehaviorBasedOnBehaviorType, + DAT_TroopValueState::ptr)(DAT_CurrentTribeID::instance); + break; + case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN: + switchD_0052b92a_caseD_d: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::processDeerMoving, this)( + DAT_CurrentTribeID::instance); + break; + case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN: + switchD_0052b92a_caseD_e: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateLionWolfTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN: + switchD_0052b92a_caseD_f: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateRabbitTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case ((AITribeType)0x10): + switchD_0052b92a_caseD_10: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateCamelTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case ((AITribeType)0x11): + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::markTribeAsAnimalTribe, this)( + DAT_CurrentTribeID::instance); + break; + case ((AITribeType)0x12): + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::markTribeAsAnimalTribe, this)( + DAT_CurrentTribeID::instance); + break; + default: + switchD_0052b92a_caseD_4 + : MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateTribeCombatStanceBehavior, + this)(DAT_CurrentTribeID::instance); + } + psVar1 = (short*)&this->tribes[DAT_CurrentTribeID::instance].countdown; + if (this->tribes[DAT_CurrentTribeID::instance].countdown != 0) { + *psVar1 = *psVar1 + -1; + } + } + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; + if (1250 < DAT_CurrentTribeID::instance) {} + } while (true); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp new file mode 100644 index 0000000000..708476be58 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -0,0 +1,45 @@ +#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" + +#include "string.h" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00523EF0 + void TribesState::upgradeTribeArrayLayoutForMapVersion( + PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) + { + int iVar1; + Tribe* _Dst; + int iVar2; + short* _Src; + short* psVar3; + if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 169) { + iVar2 = 1249; + _Dst = this->tribes + 0x4e1; + _Src = this->tribes[944].unitSelectionBitMasked + 0x7a; + do { + if (_Dst != (Tribe*)_Src) { + memmove(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); + } + psVar3 = _Dst->unitSelectionBitMasked; + memmove(&_Dst->field34_0x1c8, (void*)((int)(_Src + 0x80)), (size_t)((int)(364))); + iVar2 = iVar2 + -1; + _Src = _Src + -0x136; + _Dst = _Dst + -1; + psVar3 = psVar3 + 100; + for (iVar1 = 50; iVar1 != 0; iVar1 = iVar1 + -1) { + psVar3[0] = 0; + psVar3[1] = 0; + psVar3 = psVar3 + 2; + } + } while (-1 < iVar2); + } + return; + } + + } +} +} From 5387a86c57b06e5475087c391ca025ac3a9425b6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 19 Oct 2026 23:05:47 +0200 Subject: [PATCH 002/202] style: resolve four field-walking pointers in TribesState unptr.py over the namespace. updateCamelTribeBehavior 49.9% -> 50.3% and updateLionWolfTribeBehavior 59.3% -> 59.6%; addUnitToTribe and updateTribes unchanged. No function got worse. Measured and reverted, so the next reader does not retry them: - unmod.py's signed-remainder rewrite costs here. removeUnitFromTribe 43.4% -> 14.1% and unitIsSelectedByPlayer 50.0% -> 33.3%: the original keeps the bare mask, so the dividend is not signed in these two. - unptr.py plus docount.py on updateTribeCombatStanceBehavior 53.4% -> 45.5% (norm 44.5% -> 38.2%); the original compiled that counted loop as a do-while. Also fixes a deparen.py bug this pass uncovered: `<` was scanned as a comparison inside `<<`, so `1 << n` was rewritten to `1 < < n`, which does not compile. Two regression cases added to test_deparen.py (10/10 pass). Co-Authored-By: Claude Opus 5 (1M context) --- .../Map/Units/TribesState/addUnitToTribe.cpp | 3 +-- .../TribesState/updateCamelTribeBehavior.cpp | 6 ++---- .../updateLionWolfTribeBehavior.cpp | 6 ++---- .../Map/Units/TribesState/updateTribes.cpp | 19 +++++++++---------- .../reimplementation-helper/batch/deparen.py | 5 +++++ .../batch/test_deparen.py | 4 ++++ 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp index d1e78d2f35..f8f5a440d6 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -14,8 +14,7 @@ namespace Map { { short* psVar1; uint _mod16; - psVar1 = &this->tribes[tribeID].size; - *psVar1 = *psVar1 + 1; + this->tribes[tribeID].size = this->tribes[tribeID].size + 1; /* basically, it sets a bit flag for every unitID that is in the selection */ diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index 3542c57470..9b92244542 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -30,8 +30,7 @@ namespace Map { int sVar5; BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( param_1, this->tribes[param_1].uid); - psVar1 = &this->tribes[param_1].unknownCounter01; - *psVar1 = *psVar1 + 1; + this->tribes[param_1].unknownCounter01 = this->tribes[param_1].unknownCounter01 + 1; sVar5 = this->tribes[param_1].unknownCounter01; this->tribes[param_1].unkIsAnimalTribe = 1; if (8000 <= sVar5) { @@ -66,8 +65,7 @@ namespace Map { (uint)((int)this->tribes[param_1].rallyPointArray[sVar5][0]), (uint)((int)this->tribes[param_1].rallyPointArray[sVar5][1]), 0, 0, Map::Units::Instructions::UMSE_0); - psVar1 = &this->tribes[param_1].currentRallyPointIndex; - *psVar1 = *psVar1 + 1; + this->tribes[param_1].currentRallyPointIndex = this->tribes[param_1].currentRallyPointIndex + 1; if (this->tribes[param_1].rallyPointCount <= this->tribes[param_1].currentRallyPointIndex) { this->tribes[param_1].rallyPointCount = 0; this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index aeb5b26636..577cad117d 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -33,8 +33,7 @@ namespace Map { BOOLEnum BVar6; BVar5 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( param_1, this->tribes[param_1].uid); - psVar1 = &this->tribes[param_1].unknownCounter01; - *psVar1 = *psVar1 + 1; + this->tribes[param_1].unknownCounter01 = this->tribes[param_1].unknownCounter01 + 1; sVar2 = this->tribes[param_1].unknownCounter01; this->tribes[param_1].unkIsAnimalTribe = 1; if (8000 <= sVar2) { @@ -70,8 +69,7 @@ namespace Map { (uint)((int)this->tribes[param_1].rallyPointArray[sVar2][0]), (uint)((int)this->tribes[param_1].rallyPointArray[sVar2][1]), 0, 0, Map::Units::Instructions::UMSE_0); - psVar1 = &this->tribes[param_1].currentRallyPointIndex; - *psVar1 = *psVar1 + 1; + this->tribes[param_1].currentRallyPointIndex = this->tribes[param_1].currentRallyPointIndex + 1; if (this->tribes[param_1].rallyPointCount <= this->tribes[param_1].currentRallyPointIndex) { this->tribes[param_1].rallyPointCount = 0; this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp index 17eb7a7822..cf7acaf97e 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp @@ -29,8 +29,8 @@ namespace Map { do { if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 2) { if (0 < this->tribes[DAT_CurrentTribeID::instance].countdown2) { - psVar1 = &this->tribes[DAT_CurrentTribeID::instance].countdown2; - *psVar1 = *psVar1 + -1; + this->tribes[DAT_CurrentTribeID::instance].countdown2 + = this->tribes[DAT_CurrentTribeID::instance].countdown2 + -1; } if ((DAT_CurrentTribeID::instance & 0x1f) == _tickBasedNumberUpTo32) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::computeTribePercentages, this)( @@ -52,8 +52,8 @@ namespace Map { goto switchD_0052b92a_caseD_f; case ((AITribeType)0x10): goto switchD_0052b92a_caseD_10; - default: - goto switchD_0052b92a_caseD_4; + default: + goto switchD_0052b92a_caseD_4; } } switch (this->tribes[DAT_CurrentTribeID::instance].tribeType) { @@ -81,8 +81,7 @@ namespace Map { case ((AITribeType)0x1d): case AI::Tribes::OFFSET_CROSSBOWMAN: case ((AITribeType)0x1f): - MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::updateTribeBehaviorBasedOnBehaviorType, + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::updateTribeBehaviorBasedOnBehaviorType, DAT_TroopValueState::ptr)(DAT_CurrentTribeID::instance); break; case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN: @@ -113,10 +112,10 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::markTribeAsAnimalTribe, this)( DAT_CurrentTribeID::instance); break; - default: - switchD_0052b92a_caseD_4 - : MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateTribeCombatStanceBehavior, - this)(DAT_CurrentTribeID::instance); + default: + switchD_0052b92a_caseD_4: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateTribeCombatStanceBehavior, this)( + DAT_CurrentTribeID::instance); } psVar1 = (short*)&this->tribes[DAT_CurrentTribeID::instance].countdown; if (this->tribes[DAT_CurrentTribeID::instance].countdown != 0) { diff --git a/tools/reimplementation-helper/batch/deparen.py b/tools/reimplementation-helper/batch/deparen.py index 305163585d..76e0b66fbc 100644 --- a/tools/reimplementation-helper/batch/deparen.py +++ b/tools/reimplementation-helper/batch/deparen.py @@ -65,6 +65,11 @@ def scan_top(s): continue if tok in ("<", ">") and s[i - 1:i + 1] == "->": continue + # a shift is not a comparison: splitting `1 << n` at the first `<` + # rejoins it as `1 < < n`, which still parses as far as the rewriter + # is concerned and then fails to compile. + if tok in ("<", ">") and tok in (s[i - 1:i], s[i + 1:i + 2]): + continue hit = tok break if hit: diff --git a/tools/reimplementation-helper/batch/test_deparen.py b/tools/reimplementation-helper/batch/test_deparen.py index 9a62a38451..54def94862 100644 --- a/tools/reimplementation-helper/batch/test_deparen.py +++ b/tools/reimplementation-helper/batch/test_deparen.py @@ -29,6 +29,10 @@ ("(a == 1)", "a == 1"), # nested || of the same operator flattens ("((a == 1 || (b == 2)) || c == 3)", "a == 1 || b == 2 || c == 3"), + # a shift is not a comparison: an earlier version split `1 << n` at the first `<` + # and rejoined it as `1 < < n`, which does not compile + ("((int)*p & 1 << ((byte)i & 0x1f))", "(int)*p & 1 << ((byte)i & 0x1f)"), + ("(x >> 2 == 0)", "x >> 2 == 0"), ] if __name__ == "__main__": From e6a11797d5e4548b8ba6479cef21dd642715580e Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 19 Oct 2026 23:12:58 +0200 Subject: [PATCH 003/202] types: five Tribe shorts are unsigned Ports ffea6d2bd from origin/development, whose field numbering differs from this branch's. diff_types.py reports EXTEND on five fields across TribesState: the original zero-extends the 16-bit read where our `int local = short field` sign-extends it. Tribe::{unknownAttackRelatedUpdateCounter, initialMoveOrderGiven, predatorNearby, field138_0x284, countdown}. spawnDeerLionOrRabbit 66.4% -> 67.2%, updateCamelTribeBehavior 50.3% -> 50.9%, updateLionWolfTribeBehavior 59.6% -> 60.1%, updateRabbitTribeBehavior 31.8% -> 32.8%, processDeerMoving 24.1% -> 23.9%. Nothing else moved. Tribe::size and Tribe::rallyPointCount carry the same hint and are deliberately left signed: both are loop bounds, so `i < size` is a signed compare that ushort would silently flip (see the upstream commit, where that cost 35 functions). Co-Authored-By: Claude Opus 5 (1M context) --- src/OpenSHC/Map/Units/Tribe.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/Tribe.hpp b/src/OpenSHC/Map/Units/Tribe.hpp index 488c1d2f0e..1079958762 100644 --- a/src/OpenSHC/Map/Units/Tribe.hpp +++ b/src/OpenSHC/Map/Units/Tribe.hpp @@ -43,7 +43,7 @@ namespace Map { SomeTribeBehaviorTypeShort tribeBehaviorType; // 0x00000028 length: 2 undefined1 padding_0x2a[2]; // 0x0000002A length: 2 short someUpdateUpperLimit; // 0x0000002C length: 2 - short unknownAttackRelatedUpdateCounter; // 0x0000002E length: 2 + ushort unknownAttackRelatedUpdateCounter; // 0x0000002E length: 2 short field29_0x30; // 0x00000030 length: 2 short selectionTargetUnitID; // 0x00000032 length: 2 short size; // 0x00000034 length: 2 @@ -63,7 +63,7 @@ namespace Map { short maximumMovementSpeed; // 0x000001FE length: 2 short field61_0x200; // 0x00000200 length: 2 short field62_0x202; // 0x00000202 length: 2 - short initialMoveOrderGiven; // 0x00000204 length: 2 + ushort initialMoveOrderGiven; // 0x00000204 length: 2 short isRallyingUnk; // 0x00000206 length: 2 short field65_0x208; // 0x00000208 length: 2 short unitsHealthPercentage; // 0x0000020A length: 2 @@ -80,18 +80,18 @@ namespace Map { short currentRallyPointIndex; // 0x00000260 length: 2 short rallyPointCount; // 0x00000262 length: 2 undefined1 padding_0x264[20]; // 0x00000264 length: 20 - short predatorNearby; // 0x00000278 length: 2 + ushort predatorNearby; // 0x00000278 length: 2 short pendingScatterMove; // 0x0000027A length: 2 short unkIsAnimalTribe; // 0x0000027C length: 2 short wanderIntervalTicks; // 0x0000027E length: 2 short herdMinSize; // 0x00000280 length: 2 short herdMaxSize; // 0x00000282 length: 2 - short field138_0x284; // 0x00000284 length: 2 + ushort field138_0x284; // 0x00000284 length: 2 short unknownCounter01; // 0x00000286 length: 2 short field_0x288; // 0x00000288 length: 2 short unknownBool02; // 0x0000028A length: 2 short unknownBool01; // 0x0000028C length: 2 - short countdown; // 0x0000028E length: 2 + ushort countdown; // 0x0000028E length: 2 short someUnitArrayIndex; // 0x00000290 length: 2 short someIndex; // 0x00000292 length: 2 short attackWave; // 0x00000294 length: 2 From 33a5e0df4998461cf6a3d50edc1d5552fefaf0c4 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 19 Oct 2026 23:12:58 +0200 Subject: [PATCH 004/202] reimplement: clearAllTribes 100%, getMajorityArcherTypeEuropeanOrArabian 69.6% clearAllTribes 95.2% -> 100%. diff_signed.py: the original ends the loop with `jl` where we had `jb`, so the undefined4 counter is compared as a signed int. Also substituted sizeof(Tribe) for the 0x334 byte count (they are equal) and turned the do-while into a for loop, which measured an exact tie. getMajorityArcherTypeEuropeanOrArabian: diff_jcc.py ORDER. Written as `arabArcherCount > europeanArcherCount`, which is the operand order the original's `cmp`/`jg` shows; quick_diff 0.9247 -> 0.9462. reccmp does not move on it. Still off by one `jmp`: the original falls through from the A_ARCHER arm into the E_ARCHER test and we jump over it. Testing E_ARCHER first was measured and is worse (0.9032), and hoisting the UnitTypeShort local to the top of the function is an exact tie, so it stays declared at its use. Co-Authored-By: Claude Opus 5 (1M context) --- src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp | 9 ++++----- .../getMajorityArcherTypeEuropeanOrArabian.cpp | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp b/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp index 2c23576941..182663c1f5 100644 --- a/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp +++ b/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp @@ -16,12 +16,11 @@ namespace Map { this->clans = 0; this->field2_0x8 = 1; this->field9_0x24 = 0; - DAT_CurrentTribeID::instance = 1; - do { + for (DAT_CurrentTribeID::instance = 1; (int)DAT_CurrentTribeID::instance < 1250; + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1) { MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - 0x334, '\0', (void*)((int)(this->tribes + DAT_CurrentTribeID::instance))); - DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; - } while (DAT_CurrentTribeID::instance < 1250); + sizeof(Tribe), '\0', &this->tribes[DAT_CurrentTribeID::instance]); + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp b/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp index 393c04c0e5..4f209b1fc2 100644 --- a/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp @@ -37,7 +37,7 @@ namespace Map { } } } while (_unitSelectionIndex < iVar2); - if (_europeanArcherCount < _arabArcherCount) { + if (_arabArcherCount > _europeanArcherCount) { return Map::Units::UT_A_ARCHER; } } From 121a6d30479c96f1919383020a47ed79e271ed80 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 19 Oct 2026 23:31:30 +0200 Subject: [PATCH 005/202] types: UnitSelectionDefinedData::BitMaskHelper is unsigned diff_types.py reports EXTEND on it in unitIsSelectedByPlayer, and the same field is read with movzx in removeUnitFromTribe where our `(int)` on the declared short gives movsx. The evidence agrees: the array holds `1 << n` for n in 0..15, so element 15 is 0x8000, which a signed short cannot represent. Free on its own (same underlying width) and it lets the reads be widened: unitIsSelectedByPlayer 50.0% -> 56.2%. BitMaskHelper2 is left alone. It is only ever written back through a 16-bit `and`, so no read site extends it and there is no evidence either way. Co-Authored-By: Claude Opus 5 (1M context) --- src/OpenSHC/UI/UnitSelectionDefinedData.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/UI/UnitSelectionDefinedData.hpp b/src/OpenSHC/UI/UnitSelectionDefinedData.hpp index abc70d695a..0079fdef72 100644 --- a/src/OpenSHC/UI/UnitSelectionDefinedData.hpp +++ b/src/OpenSHC/UI/UnitSelectionDefinedData.hpp @@ -20,7 +20,7 @@ namespace UI { typedef struct UnitSelectionDefinedData { undefined1 padding_0x0[1604]; // 0x00000000 length: 1604 - short BitMaskHelper[16]; // 0x00000644 length: 32 + ushort BitMaskHelper[16]; // 0x00000644 length: 32 short BitMaskHelper2[16]; // 0x00000664 length: 32 int UnitInstructionMoveDelay[200]; // 0x00000684 length: 800 int UnitMoveDelay[200]; // 0x000009A4 length: 800 From a0484b2b94803ff218defd576ea63507f0a2b0d6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 19 Oct 2026 23:31:30 +0200 Subject: [PATCH 006/202] reimplement: six TribesState functions, two to 100% Namespace average 71.15% -> 73.31%, nothing worse. updatePeasantSeatingAtBuilding 36.7% -> 100%. Three changes: the `short*` walker with offsets like psVar3[-0x10a] is a `Unit*` walker with named fields (the original really does walk a pointer, so the walk stays - only the magic offsets go); the `param_2 < 2 / < 4 / < 8 / < 0x10` chain is written as `<= 1 / <= 3 / <= 7 / <= 0xf`, which is the `cmp N; jg` the original has; and the goto out of the nested chain is gone, flattened to else-if. The owner local is `int`, not `short`, because the original sign-extends the read. spawnWildlifeOrMercAtAvailableSlot 23.5% -> 100%. The import had two guards with empty bodies, `if (notSolitary || tooMuchWildlife) {}` and `if (0xb < iVar2) {}`, so the function looped forever; and it was missing the `return` after a successful spawn - the original spawns at one slot and returns, which is what the name says. hasAvailableSpawnSlotForWildlifeOrMercs 26.9% -> 96.0% (norm 93.3%). The shared tail through `bool bVar4` cost a `sete`/`test` pair; two plain `return 1` arms recover the original's flags-live `jmp` into the shared block. removeUnitFromTribe 43.4% -> 67.1%. unitID is divided and taken modulo as a *signed* int - the original biases the dividend and repairs the negative remainder, which unsigned operands never need - and the selection-bit test is a 32-bit `test` of a movsx'd field against a movzx'd mask, not the decompiler's 16-bit `(ushort)(a & b)`. The goto is gone. What is left is allocator: the original keeps &tribes[tribeID] in a register where we spill it. isTribePathToDestinationClear 13.9% -> 18.8%. `2 - (uVar1 & 0x100)` subtracts the mask; the binary's `and/neg/sbb/add 2` is 2 minus a 0/1 flag, and it is computed once before the search is seeded. Bounds are `<= 399`, the tile-index limit is a signed `>= 500`, and the one `&&` chain is the two early-return guards the original's inline cold block shows. Still low: the original keeps the unit index in a stack slot and we keep it in a register. spawnDeerLionOrRabbit 67.2% -> 68.0%. `((_unknown <= _tribeSize) - 1 & 0xffffffb5) + 100` is `_tribeSize < _unknown ? 25 : 100`; written that way round it beats the opposite polarity (quick_diff 0.7295 against 0.7049). Resolving the field-walking pointer beside it costs one instruction, so it stays, with its type corrected to ushort and the measurement recorded. Co-Authored-By: Claude Opus 5 (1M context) --- ...asAvailableSpawnSlotForWildlifeOrMercs.cpp | 34 ++++---- .../isTribePathToDestinationClear.cpp | 79 +++++++++++-------- .../Units/TribesState/removeUnitFromTribe.cpp | 59 ++++++-------- .../TribesState/spawnDeerLionOrRabbit.cpp | 11 ++- .../spawnWildlifeOrMercAtAvailableSlot.cpp | 65 +++++++++------ .../updatePeasantSeatingAtBuilding.cpp | 50 ++++++------ 6 files changed, 157 insertions(+), 141 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp index 470b07ba21..b5b2909374 100644 --- a/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp +++ b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp @@ -16,27 +16,21 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00523520 undefined4 TribesState::hasAvailableSpawnSlotForWildlifeOrMercs() { - int iVar1; - bool bVar4; - if ((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) - && (iVar1 = 0, DAT_GameState::instance.mapAndTime.wildlifeCount < 0xa0)) { - do { - if (iVar1 < 4) { - uint uVar3 = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar1][0]; - uint uVar2 = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar1][1]; - if ((uVar3 < 400) && (uVar2 < 400)) { - bVar4 = *(char*)(uVar2 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + uVar3) == '\0'; - goto LAB_00523574; - } - } else { - bVar4 = DAT_GameState::instance.mapAndTime.mercRecruitable[iVar1 + 4] == 0; - LAB_00523574: - if (!bVar4) { - return (undefined4)(1); - } + if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY + || 0xa0 <= DAT_GameState::instance.mapAndTime.wildlifeCount) { + return (undefined4)(0); + } + for (int _slot = 0; _slot < 0xc; _slot = _slot + 1) { + if (_slot < 4) { + uint const _x = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0]; + uint const _y = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1]; + if (_x <= 399 && _y <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[_y * 400 + _x] != 0) { + return (undefined4)(1); } - iVar1 = iVar1 + 1; - } while (iVar1 < 0xc); + } else if (DAT_GameState::instance.mapAndTime.mercRecruitable[_slot + 4] != 0) { + return (undefined4)(1); + } } return (undefined4)(0); } diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index 95e3449b25..c4d8c443bf 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -19,41 +19,54 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00524CA0 undefined4 TribesState::isTribePathToDestinationClear( - int param_1, uint param_2, uint param_3, uint param_4, uint param_5) + int tribeID, uint fromX, uint fromY, uint toX, uint toY) { - int iVar2; - int iVar3; - int local_4 = 0; - if (param_2 < 400 && param_3 < 400 && *(char*)(param_3 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + param_2) != '\0' && param_4 < 400 - && param_5 < 400 && *(char*)(param_5 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + param_4) != '\0' - && (iVar2 = DAT_ViewportRenderState::instance.translationMatrix[param_5].addXgetTile + param_4, - DAT_TileMapState::instance.PathConnectionLayer - [DAT_ViewportRenderState::instance.translationMatrix[param_3].addXgetTile + param_2] - == DAT_TileMapState::instance.PathConnectionLayer[iVar2])) { - this->ALG_ResultTileIndex = 0; - uint uVar1 = DAT_TileMapState::instance.LogicLayer[iVar2]; - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, - DAT_PathFindingState::ptr)(param_4, param_5, -1, -1, 500, FALSE); - while (true) { - do { - if (this->tribes[param_1].size <= local_4) { - return (undefined4)(1); - } - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, local_4); - local_4 = local_4 + 1; - } while ((DAT_UnitsState::instance.units[iVar2].logicalState != Map::Units::ULS_NORMAL) - || (DAT_UnitsState::instance.units[iVar2].dying != 0)); - iVar3 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findClosestTileToStartingTile, - DAT_PathFindingState::ptr)(2 - (uint)((uVar1 & 0x100))); - this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; - if (499 < this->ALG_ResultTileIndex) - break; - iVar3 = (int)(short)DAT_TileMapState::instance.UnitLayer[iVar3]; - if (iVar3 && iVar2 != iVar3 && DAT_UnitsState::instance.units[iVar3].tribeID != param_1 - && DAT_UnitsState::instance.units[iVar3].tunnelerFinishedDigging != 2) { - return (undefined4)(0); + int _unitIndex = 0; + if (399 < fromX || 399 < fromY + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[fromY * 400 + fromX] == 0) { + return (undefined4)(0); + } + if (399 < toX || 399 < toY + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[toY * 400 + toX] == 0) { + return (undefined4)(0); + } + int const _toTile = DAT_ViewportRenderState::instance.translationMatrix[toY].addXgetTile + toX; + if (DAT_TileMapState::instance.PathConnectionLayer + [DAT_ViewportRenderState::instance.translationMatrix[fromY].addXgetTile + fromX] + != DAT_TileMapState::instance.PathConnectionLayer[_toTile]) { + return (undefined4)(0); + } + this->ALG_ResultTileIndex = 0; + /* + The original computes this once, before the search is seeded. The + decompiler's `2 - (uVar1 & 0x100)` subtracts the mask itself, but the binary + has `and 0x100; neg; sbb; add 2`, which is 2 minus a 0/1 flag. + */ + int const _searchMode = (DAT_TileMapState::instance.LogicLayer[_toTile] & 0x100) != 0 ? 1 : 2; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(toX, toY, -1, -1, 500, FALSE); + while (true) { + int _unitID; + do { + if (_unitIndex >= this->tribes[tribeID].size) { + return (undefined4)(1); } + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitIndex); + _unitIndex = _unitIndex + 1; + } while ((DAT_UnitsState::instance.units[_unitID].logicalState != Map::Units::ULS_NORMAL) + || (DAT_UnitsState::instance.units[_unitID].dying != 0)); + int _blockingUnitID = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findClosestTileToStartingTile, DAT_PathFindingState::ptr)( + _searchMode); + this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; + if ((int)this->ALG_ResultTileIndex >= 500) + break; + _blockingUnitID = (int)(short)DAT_TileMapState::instance.UnitLayer[_blockingUnitID]; + if (_blockingUnitID && _unitID != _blockingUnitID + && DAT_UnitsState::instance.units[_blockingUnitID].tribeID != tribeID + && DAT_UnitsState::instance.units[_blockingUnitID].tunnelerFinishedDigging != 2) { + return (undefined4)(0); } } return (undefined4)(0); diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index 371d4b1693..5ea20bb3ce 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -12,41 +12,34 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00525A70 void TribesState::removeUnitFromTribe(uint unitID, int tribeID) { - short* psVar1; - uint uVar2; - short _size; - if (DAT_UnitsState::instance.units[unitID].tribeID == 0) { - uVar2 = unitID & 0x8000000f; - if ((int)uVar2 < 0) { - uVar2 = (uVar2 - 1 | 0xfffffff0) + 1; + /* + unitID is divided and taken modulo as a signed int: the original biases the + dividend (`cdq; and edx, 0xf; add; sar 4`) and repairs the negative remainder + (`and -0x7ffffff1; jns; dec; or -0x10; inc`), neither of which an unsigned + operand needs. + */ + if (DAT_UnitsState::instance.units[unitID].tribeID != 0 + || ((int)this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] + & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) + != 0) { + short const _size = this->tribes[tribeID].size; + if (0 < _size) { + this->tribes[tribeID].size = _size + -1; + } + if (this->tribes[tribeID].size <= 0) { + this->tribes[tribeID].tribeState = 3; + } + short* psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); + *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[(int)unitID % 16]; + if (DAT_UnitsState::instance.units[unitID].tribeID == tribeID) { + DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = 0; + DAT_UnitsState::instance.units[unitID].tribeID = 0; + DAT_UnitsState::instance.units[unitID].idInTribe = 0; + } + if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(tribeID); } - if ((ushort)(DAT_UnitSelectionDefinedData::instance.BitMaskHelper[uVar2] - & this->tribes[tribeID].unitSelectionBitMasked[unitID / 16]) - == 0) - goto LAB_00525b56; - } - _size = this->tribes[tribeID].size; - if (0 < _size) { - this->tribes[tribeID].size = _size + -1; - } - if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = 3; - } - psVar1 = this->tribes[tribeID].unitSelectionBitMasked + (unitID / 16); - uVar2 = unitID & 0x8000000f; - if ((int)uVar2 < 0) { - uVar2 = (uVar2 - 1 | 0xfffffff0) + 1; - } - *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[uVar2]; - if (DAT_UnitsState::instance.units[unitID].tribeID == tribeID) { - DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = 0; - DAT_UnitsState::instance.units[unitID].tribeID = 0; - DAT_UnitsState::instance.units[unitID].idInTribe = 0; - } - if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(tribeID); } - LAB_00525b56: MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); } diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index adf70e38d3..75adf3623c 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -22,7 +22,7 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00525F20 BOOLEnum TribesState::spawnDeerLionOrRabbit(int tribeID, int param_2, UnitType unitType) { - short* psVar1; + ushort* psVar1; int sVar2; int _finalTargetUnitID; int iVar3; @@ -56,7 +56,12 @@ namespace Map { if (_tribeSize <= _unknown) { param_2 = param_2 / 4; } - psVar1 = (short*)&this->tribes[tribeID].field138_0x284; + /* + Keeping the pointer rather than naming the field: resolving it costs one + instruction here (quick_diff 0.7295 -> 0.7265), because the field form + re-evaluates tribes[tribeID] while the pointer freezes the address. + */ + psVar1 = &this->tribes[tribeID].field138_0x284; *psVar1 = *psVar1 + 1; if ((param_2 <= this->tribes[tribeID].field138_0x284) && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field138_0x284 = 0, @@ -65,7 +70,7 @@ namespace Map { && (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation)) { iVar3 = 110; } else { - iVar3 = ((_unknown <= _tribeSize) - 1 & 0xffffffb5) + 100; + iVar3 = _tribeSize < _unknown ? 25 : 100; } if (iVar3 <= (int)(_rng2 & 0x7f)) { _rng2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( diff --git a/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp b/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp index 5892fb88fb..30a5a96415 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp @@ -19,36 +19,51 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005262A0 void TribesState::spawnWildlifeOrMercAtAvailableSlot() { - int iVar1; - short* psVar3; - uint y; - uint x; - if ((DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) - || (0x9f < DAT_GameState::instance.mapAndTime.wildlifeCount)) {} - int iVar2 = 0; - psVar3 = DAT_GameState::instance.mapAndTime.playerPopulationStatistics[6] + 0xe4; - do { - if (iVar2 < 4) { - x = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar2][0]; - y = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[iVar2][1]; - LAB_005262f6: - if (x < 400 && y < 400 && *(char*)(y * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x) != '\0' - && (iVar1 = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x, - (DAT_TileMapState::instance.LogicLayer[iVar1] & 0x50501581U) == 0)) { + if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY + || 0xa0 <= DAT_GameState::instance.mapAndTime.wildlifeCount) { + return; + } + /* + fixme: the merc slots' coordinate pair is reached through a pointer because + the generated header cannot express it. The original walks + mapAndTime + 0x1ddc in steps of 0x320 (400 shorts), which is a row stride + playerPopulationStatistics[9][300] does not have, and reads a dword either + side of the cursor. mercRecruitable is indexed [_slot + 4] for _slot 4..11, + which is past the end of its declared int[7] as well. Both arrays need + correcting in Ghidra before this can be written with named fields. + */ + /* + The slot counter is declared before the cursor rather than in the `for` + header: MSVC assigns registers in declaration order, and the original zeroes + the counter first (quick_diff 0.9245 -> 0.9434). + */ + int _slot = 0; + short* _mercSpawnXY = DAT_GameState::instance.mapAndTime.playerPopulationStatistics[6] + 0xe4; + for (; _slot < 0xc; _slot = _slot + 1, _mercSpawnXY = _mercSpawnXY + 400) { + uint x; + uint y; + if (_slot < 4) { + x = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0]; + y = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1]; + } else { + if (DAT_GameState::instance.mapAndTime.mercRecruitable[_slot + 4] == 0) { + continue; + } + x = *(uint*)(_mercSpawnXY + -2); + y = *(uint*)_mercSpawnXY; + } + if (x <= 399 && y <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] != 0) { + int const _tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x50501581U) == 0) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( - Commands::M_MAPPER_RABBIT, x, y, (int)(DAT_TileMapState::instance.HeightLayer[iVar1])); + Commands::M_MAPPER_RABBIT, x, y, (int)(DAT_TileMapState::instance.HeightLayer[_tile])); this->unknownX_01 = x; this->unknownY_01 = y; + return; } - } else if (DAT_GameState::instance.mapAndTime.mercRecruitable[iVar2 + 4] != 0) { - x = *(uint*)(psVar3 + -2); - y = *(uint*)psVar3; - goto LAB_005262f6; } - iVar2 = iVar2 + 1; - psVar3 = psVar3 + 400; - if (0xb < iVar2) {} - } while (true); + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp b/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp index 6494e2454f..90abfa4924 100644 --- a/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp @@ -21,53 +21,49 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522210 void TribesState::updatePeasantSeatingAtBuilding(int param_1, int param_2, int param_3) { - uint uVar2; - short* psVar3; int iVar4; int local_8; - short sVar1 = DAT_BuildingsState::instance.buildings[param_1].owner; + int sVar1 = DAT_BuildingsState::instance.buildings[param_1].owner; local_8 = 0; - if (param_2 < 2) { + if (param_2 <= 1) { param_2 = 1; - } else if (param_2 < 4) { + } else if (param_2 <= 3) { param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 1) + 1; + } else if (param_2 <= 7) { + param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 1; + } else if (param_2 <= 0xf) { + param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 1; } else { - if (param_2 < 8) { - uVar2 = (byte)SEC_RNG::instance.currentNumber2 & 3; - } else { - if (param_2 < 0x10) { - param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 1; - goto LAB_00522291; - } - uVar2 = (byte)SEC_RNG::instance.currentNumber2 & 0xf; - } - param_2 = uVar2 + 1; + param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 0xf) + 1; } - LAB_00522291: iVar4 = 1; if (1 < DAT_UnitsState::instance.maxUnitCount) { - psVar3 = &DAT_UnitsState::instance.units[1].dying; + Unit* _unit = &DAT_UnitsState::instance.units[1]; do { - if (psVar3[-0x10a] == Map::Units::ULS_NORMAL && *psVar3 == 0 && psVar3[-0x105] == sVar1 - && psVar3[-0x109] == Map::Units::UT_PEASANT - && ((UnitStateUnion*)(psVar3 + 0x10))->generic == Map::Units::States::US_STAND_UPUnk - && psVar3[0x4c] == param_1) { + if (_unit->logicalState == Map::Units::ULS_NORMAL && _unit->dying == 0 && _unit->owner == sVar1 + && _unit->unitType == Map::Units::UT_PEASANT + && _unit->state.generic == Map::Units::States::US_STAND_UPUnk + && _unit->workplaceBuildingID_1 == param_1) { local_8 = local_8 + 1; - psVar3[0x78] = 0; + /* + fixme: the short at Unit + 0x390 is declared as padding in the + generated header; it is the peasant's pending-sit flag. + */ + *(short*)_unit->padding_0x390 = 0; if (param_3 == 1) { - psVar3[0x77] = 0; + _unit->peasantAlwaysZero = 0; } if (local_8 == param_2) { - psVar3[0x77] = 1; + _unit->peasantAlwaysZero = 1; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::standUpIfSeated, DAT_UnitsState::ptr)(iVar4); - } else if (psVar3[0x77] == 0) { - psVar3[0x78] = 1; + } else if (_unit->peasantAlwaysZero == 0) { + *(short*)_unit->padding_0x390 = 1; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::sitDownIfStanding, DAT_UnitsState::ptr)( iVar4); } } iVar4 = iVar4 + 1; - psVar3 = psVar3 + 0x248; + _unit = _unit + 1; } while (iVar4 < DAT_UnitsState::instance.maxUnitCount); } } From e8a89860d8d014a8fc0cf1b989f0693389743544 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 19 Oct 2026 23:32:09 +0200 Subject: [PATCH 007/202] status: TribesState progress after the import and first two batches 94 lines updated from the current reccmp run: the 93 imported functions had no status line yet, and clearAllTribes, spawnWildlifeOrMercAtAvailableSlot and updatePeasantSeatingAtBuilding have reached 100%. Namespace average 73.31% over 113 functions, 41 of them at 100%. Co-Authored-By: Claude Opus 5 (1M context) --- status/addresses-SHC-3BB0A8C1.txt | 188 +++++++++++++++--------------- 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 5ce117a54e..367f88edfe 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46381,21 +46381,21 @@ SHC_3BB0A8C1_0x00520F70 | 0.0% | Pending SHC_3BB0A8C1_0x00521180 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005211B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005211B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521210 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00521240 | 0.0% | Pending +SHC_3BB0A8C1_0x00521240 | 80.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521280 | 0.0% | Pending +SHC_3BB0A8C1_0x00521280 | 27.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521500 | 0.0% | Pending +SHC_3BB0A8C1_0x00521500 | 22.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending -SHC_3BB0A8C1_0x00521720 | 0.0% | Pending +SHC_3BB0A8C1_0x00521720 | 69.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 0.0% | Pending +SHC_3BB0A8C1_0x005217E0 | 46.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 0.0% | Pending +SHC_3BB0A8C1_0x00521A90 | 26.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending @@ -46633,9 +46633,9 @@ SHC_3BB0A8C1_0x00521EA5 | 0.0% | Pending SHC_3BB0A8C1_0x00521EA6 | 0.0% | Pending -SHC_3BB0A8C1_0x00521EB0 | 82.4% | pop esi scheduling +SHC_3BB0A8C1_0x00521EB0 | 94.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521EF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00521EF0 | 82.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522030 | 0.0% | Pending @@ -46763,7 +46763,7 @@ SHC_3BB0A8C1_0x00522082 | 0.0% | Pending SHC_3BB0A8C1_0x00522083 | 0.0% | Pending -SHC_3BB0A8C1_0x00522090 | 0.0% | Pending +SHC_3BB0A8C1_0x00522090 | 89.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522110 | 100.0% | Reimplemented @@ -46771,41 +46771,41 @@ SHC_3BB0A8C1_0x00522150 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522160 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005221A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005221A0 | 67.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005221C0 | 0.0% | Pending -SHC_3BB0A8C1_0x00522210 | 0.0% | Pending +SHC_3BB0A8C1_0x00522210 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005222B0 | 0.0% | Pending SHC_3BB0A8C1_0x00522360 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522390 | 0.0% | Pending +SHC_3BB0A8C1_0x00522390 | 36.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522410 | 0.0% | Pending +SHC_3BB0A8C1_0x00522410 | 16.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x005224E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522520 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522550 | 0.0% | Pending +SHC_3BB0A8C1_0x00522550 | 56.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522590 | 0.0% | Pending +SHC_3BB0A8C1_0x00522590 | 86.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522630 | 0.0% | Pending +SHC_3BB0A8C1_0x00522630 | 30.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522720 | 0.0% | Pending +SHC_3BB0A8C1_0x00522720 | 60.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005227E0 | 0.0% | Pending +SHC_3BB0A8C1_0x005227E0 | 75.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522890 | 0.0% | Pending +SHC_3BB0A8C1_0x00522890 | 30.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522950 | 0.0% | Pending +SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005229C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005229C0 | 62.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522B00 | 0.0% | Pending +SHC_3BB0A8C1_0x00522B00 | 56.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522BC0 | 100.0% | Reimplemented @@ -46813,23 +46813,23 @@ SHC_3BB0A8C1_0x00522BF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522C50 | 0.0% | Pending +SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522CA0 | 0.0% | Pending +SHC_3BB0A8C1_0x00522CA0 | 14.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522DD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00522DD0 | 32.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522EF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00522EF0 | 51.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522F70 | 0.0% | Pending +SHC_3BB0A8C1_0x00522F70 | 72.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523030 | 0.0% | Pending +SHC_3BB0A8C1_0x00523030 | 89.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523190 | 0.0% | Pending +SHC_3BB0A8C1_0x00523190 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523240 | 0.0% | Pending +SHC_3BB0A8C1_0x00523240 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005232E0 | 0.0% | Pending +SHC_3BB0A8C1_0x005232E0 | 40.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x005232F0 | 0.0% | Pending @@ -46837,31 +46837,31 @@ SHC_3BB0A8C1_0x00523340 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005233A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005233A0 | 92.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x005233E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523410 | 0.0% | Pending +SHC_3BB0A8C1_0x00523410 | 92.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523520 | 0.0% | Pending +SHC_3BB0A8C1_0x00523520 | 96.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523590 | 0.0% | Pending +SHC_3BB0A8C1_0x00523590 | 78.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523630 | 0.0% | Pending +SHC_3BB0A8C1_0x00523630 | 90.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005236A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005236A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523730 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523750 | 0.0% | Pending +SHC_3BB0A8C1_0x00523750 | 72.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523790 | 0.0% | Pending +SHC_3BB0A8C1_0x00523790 | 25.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005237F0 | 0.0% | Pending +SHC_3BB0A8C1_0x005237F0 | 32.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523920 | 0.0% | Pending +SHC_3BB0A8C1_0x00523920 | 78.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523A30 | 0.0% | Pending +SHC_3BB0A8C1_0x00523A30 | 41.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523B00 | 0.0% | Pending @@ -46871,31 +46871,31 @@ SHC_3BB0A8C1_0x00523E30 | 0.0% | Pending SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523EF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00523EF0 | 46.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523F70 | 0.0% | Pending +SHC_3BB0A8C1_0x00523F70 | 74.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524000 | 0.0% | Pending +SHC_3BB0A8C1_0x00524000 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524060 | 0.0% | Pending +SHC_3BB0A8C1_0x00524060 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005240D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005240D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524140 | 0.0% | Pending +SHC_3BB0A8C1_0x00524140 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005241C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005241C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524230 | 0.0% | Pending +SHC_3BB0A8C1_0x00524230 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005242B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005242B0 | 69.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524340 | 0.0% | Pending +SHC_3BB0A8C1_0x00524340 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005244D0 | 0.0% | Pending +SHC_3BB0A8C1_0x005244D0 | 44.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524890 | 0.0% | Pending +SHC_3BB0A8C1_0x00524890 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524930 | 0.0% | Pending +SHC_3BB0A8C1_0x00524930 | 28.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005249C0 | 0.0% | Pending @@ -46903,77 +46903,77 @@ SHC_3BB0A8C1_0x00524A10 | 0.0% | Pending SHC_3BB0A8C1_0x00524A30 | 0.0% | Pending -SHC_3BB0A8C1_0x00524B30 | 0.0% | Pending +SHC_3BB0A8C1_0x00524B30 | 76.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524BF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00524BF0 | 67.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524CA0 | 0.0% | Pending +SHC_3BB0A8C1_0x00524CA0 | 18.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending -SHC_3BB0A8C1_0x00524E20 | 0.0% | Pending +SHC_3BB0A8C1_0x00524E20 | 27.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524EF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00524EF0 | 25.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525060 | 0.0% | Pending SHC_3BB0A8C1_0x00525090 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005250B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005250B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525130 | 0.0% | Pending +SHC_3BB0A8C1_0x00525130 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005251A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005251A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525210 | 0.0% | Pending +SHC_3BB0A8C1_0x00525210 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525280 | 0.0% | Pending +SHC_3BB0A8C1_0x00525280 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525300 | 0.0% | Pending +SHC_3BB0A8C1_0x00525300 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525370 | 0.0% | Pending +SHC_3BB0A8C1_0x00525370 | 86.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005253F0 | 0.0% | Pending +SHC_3BB0A8C1_0x005253F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525470 | 0.0% | Pending +SHC_3BB0A8C1_0x00525470 | 58.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005254C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005254C0 | 98.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525790 | 0.0% | Pending +SHC_3BB0A8C1_0x00525790 | 95.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525870 | 0.0% | Pending +SHC_3BB0A8C1_0x00525870 | 95.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525970 | 0.0% | Pending +SHC_3BB0A8C1_0x00525970 | 94.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525A70 | 0.0% | Pending +SHC_3BB0A8C1_0x00525A70 | 67.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525BA0 | 0.0% | Pending +SHC_3BB0A8C1_0x00525BA0 | 44.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525F20 | 0.0% | Pending +SHC_3BB0A8C1_0x00525F20 | 68.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005260B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005260B0 | 66.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005261B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005261B0 | 57.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005262A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005262A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00526370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005263A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005263A0 | 30.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x005266F0 | 0.0% | Pending SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending -SHC_3BB0A8C1_0x00526F00 | 0.0% | Pending +SHC_3BB0A8C1_0x00526F00 | 39.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005270F0 | 0.0% | Pending +SHC_3BB0A8C1_0x005270F0 | 42.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527330 | 0.0% | Pending +SHC_3BB0A8C1_0x00527330 | 60.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527440 | 0.0% | Pending +SHC_3BB0A8C1_0x00527440 | 58.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x005275A0 | 0.0% | Pending @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 0.0% | Pending +SHC_3BB0A8C1_0x00527C80 | 27.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending @@ -48875,11 +48875,11 @@ SHC_3BB0A8C1_0x0052A637 | 0.0% | Pending SHC_3BB0A8C1_0x0052A638 | 0.0% | Pending -SHC_3BB0A8C1_0x0052A640 | 0.0% | Pending +SHC_3BB0A8C1_0x0052A640 | 77.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052A700 | 0.0% | Pending +SHC_3BB0A8C1_0x0052A700 | 42.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052A7D0 | 0.0% | Pending +SHC_3BB0A8C1_0x0052A7D0 | 53.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052ACB4 | 0.0% | Pending @@ -49141,15 +49141,15 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 0.0% | Pending +SHC_3BB0A8C1_0x0052AD50 | 23.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 0.0% | Pending +SHC_3BB0A8C1_0x0052B110 | 60.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B390 | 0.0% | Pending +SHC_3BB0A8C1_0x0052B390 | 32.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B630 | 0.0% | Pending +SHC_3BB0A8C1_0x0052B630 | 50.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B860 | 0.0% | Pending +SHC_3BB0A8C1_0x0052B860 | 80.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B9A0 | 0.0% | Pending From f15fd218aca71e591cbd115331bed575bcd353f2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 13:03:51 +0200 Subject: [PATCH 008/202] reimplement: wrong pointer strides and tribe loop shape, four to 100% Namespace average 73.31% -> 74.78%, nothing worse. Thirteen pointer walks were stepped by the *element index* stride instead of one element, so they ran far past the end of their array: `Unit* + 0x248` in seven files and `Tribe* + 0x19a` (and `+ 0xcd`) in four. 0x248 is sizeof(Unit) in shorts and 0x19a sizeof(Tribe) in shorts, which is what the decompiler printed for a `short*` cursor; typing the cursor and keeping the stride made every one of them a different array walk. The tribe loops are now index loops over a reference local rather than a cursor compared against an absolute end address (style guide 10 and 11), which reproduces the original's `add 0x334` / `cmp ` without the literal: - applyTribeBehaviorType 67.8% -> 100% - consumeFlaggedTribesOfType 25.0% -> 100% (it also had an `if (param_2 <= 0) {}` with an empty body where the original leaves the loop) - flagTribesOfType 72.2% -> 100% - swapTribeOwnership 80.0% -> 100% applyTribeBehaviorType and swapTribeOwnership walk the *global* instance, not `this`: the original loads the tribe address as an absolute. Using DAT_TribesState::instance is what moved the loop anchor onto the original's field and was worth most of their gain. Partial, by the stride fix alone: clearTribesOfUnitType and clearTribesNotOfUnitType 97.1%, removeSelectedUnitsFromTheirCurrentTribes 96.8% (one `[esp + N]` displacement for the cleared-tribes array; swapping it with the cookie local is an exact tie, so the declaration order stands), getMajoritySelectedUnitType 71.8%, addUnitsToTribeAndComputeMovementSpeed 64.1%, addUnitToTribeAndUpdateTribeMovementSpeed 57.9%. snapshotSelectionTribeAndComputeStance had the same stride bug and did not move; its gap is elsewhere. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...UnitToTribeAndUpdateTribeMovementSpeed.cpp | 2 +- ...addUnitsToTribeAndComputeMovementSpeed.cpp | 2 +- .../TribesState/applyTribeBehaviorType.cpp | 24 +++++++++++-------- .../TribesState/clearTribesNotOfUnitType.cpp | 17 +++++-------- .../TribesState/clearTribesOfUnitType.cpp | 17 +++++-------- .../consumeFlaggedTribesOfType.cpp | 21 +++++++--------- .../Units/TribesState/flagTribesOfType.cpp | 15 ++++-------- .../getMajoritySelectedUnitType.cpp | 2 +- ...oveSelectedUnitsFromTheirCurrentTribes.cpp | 20 +++++----------- ...snapshotSelectionTribeAndComputeStance.cpp | 2 +- .../Units/TribesState/swapTribeOwnership.cpp | 24 +++++++++++-------- 11 files changed, 64 insertions(+), 82 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp index aa90d51f07..9fa480266b 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp @@ -41,7 +41,7 @@ namespace Map { } _totalHealth = _totalHealth + _ptrUnit->healthPercentage; } - _ptrUnit = _ptrUnit + 0x248; + _ptrUnit = _ptrUnit + 1; tribeID = tribeID + -1; } while (tribeID); } diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp index da55cd304c..5bc7c31c06 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp @@ -40,7 +40,7 @@ namespace Map { local_8 = local_8 + _pUnit->healthPercentage; } _unitID = _unitID + 1; - _pUnit = _pUnit + 0x248; + _pUnit = _pUnit + 1; } while ((int)_unitID < DAT_UnitsState::instance.maxUnitCount); } short sVar1 = this->tribes[tribeID].size; diff --git a/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp b/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp index 4ad9bac398..322226c58e 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp @@ -1,6 +1,7 @@ #include "../../../Map.func.hpp" #include "../TribesState.func.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" #include "OpenSHC/Globals/DAT_TroopValueState.hpp" namespace OpenSHC { @@ -10,18 +11,21 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005221A0 void TribesState::applyTribeBehaviorType(int attackWave, SomeTribeBehaviorType tribeBehaviorType) { - Tribe* psVar2; int iVar1 = DAT_TroopValueState::instance.attackInfo.someCounter1; - psVar2 = &this->tribes[1]; - do { - if (psVar2->tribeState && psVar2->attackWave == attackWave - && (int)(short)psVar2->tribeBehaviorType != tribeBehaviorType - && psVar2->attackInfo_someCounter1 != iVar1) { - psVar2->tribeBehaviorType = (SomeTribeBehaviorTypeShort)tribeBehaviorType; - psVar2->unknownAttackRelatedUpdateCounter = 0; + /* + The original walks the global instance, not `this`: it loads the tribe + address as an absolute (`mov eax, TribesState::instance + 1520`) and ends the + loop against another absolute rather than a `this`-relative one. + */ + for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { + Tribe& _tribe = DAT_TribesState::instance.tribes[_tribeID]; + if (_tribe.tribeState && _tribe.attackWave == attackWave + && (int)(short)_tribe.tribeBehaviorType != tribeBehaviorType + && _tribe.attackInfo_someCounter1 != iVar1) { + _tribe.tribeBehaviorType = (SomeTribeBehaviorTypeShort)tribeBehaviorType; + _tribe.unknownAttackRelatedUpdateCounter = 0; } - psVar2 = psVar2 + 0x19a; - } while ((int)psVar2 < 0x176261c); + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp index a6881f1b5e..1dcb592b13 100644 --- a/src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/clearTribesNotOfUnitType.cpp @@ -15,9 +15,7 @@ namespace Map { void TribesState::clearTribesNotOfUnitType(int playerID, int unitType) { Unit* psVar1; - int _tribeID2; uint unitID; - Tribe* psVar2; undefined1 _clearedTribes[1252]; uint local_4; local_4 = MSVC_SecurityCookie::instance ^ (uint)_clearedTribes; @@ -37,21 +35,18 @@ namespace Map { _clearedTribes[psVar1->tribeID] = 1; } unitID = unitID + 1; - psVar1 = psVar1 + 0x248; + psVar1 = psVar1 + 1; } while ((int)unitID < DAT_UnitsState::instance.maxUnitCount); } - _tribeID2 = 1; - psVar2 = &this->tribes[1]; - do { - if (psVar2->tribeState == 2) { - if (psVar2->selectionTargetUnitID == -1) { + for (int _tribeID2 = 1; _tribeID2 < 1250; _tribeID2 = _tribeID2 + 1) { + Tribe& _tribe = this->tribes[_tribeID2]; + if (_tribe.tribeState == 2) { + if (_tribe.selectionTargetUnitID == -1) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(_tribeID2); } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(_tribeID2); } - _tribeID2 = _tribeID2 + 1; - psVar2 = psVar2 + 0x19a; - } while (_tribeID2 < 1250); + } ; } diff --git a/src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp index fab7f19fd7..2eaa0ad78d 100644 --- a/src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/clearTribesOfUnitType.cpp @@ -15,9 +15,7 @@ namespace Map { void TribesState::clearTribesOfUnitType(int param_1, int param_2) { Unit* psVar1; - int tribeID; uint unitID; - short* psVar2; undefined1 local_4e8[1252]; uint local_4; local_4 = MSVC_SecurityCookie::instance ^ (uint)local_4e8; @@ -38,21 +36,18 @@ namespace Map { local_4e8[psVar1->tribeID] = 1; } unitID = unitID + 1; - psVar1 = psVar1 + 0x248; + psVar1 = psVar1 + 1; } while ((int)unitID < DAT_UnitsState::instance.maxUnitCount); } - tribeID = 1; - psVar2 = &this->tribes[1].selectionTargetUnitID; - do { - if (psVar2[-0xd] == 2) { - if (*psVar2 == -1) { + for (int tribeID = 1; tribeID < 1250; tribeID = tribeID + 1) { + Tribe& _tribe = this->tribes[tribeID]; + if (_tribe.tribeState == 2) { + if (_tribe.selectionTargetUnitID == -1) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(tribeID); } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); } - tribeID = tribeID + 1; - psVar2 = psVar2 + 0x19a; - } while (tribeID < 0x4e2); + } ; } diff --git a/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp b/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp index 04e1ddee29..e65ce8ebfc 100644 --- a/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp @@ -8,20 +8,17 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00523790 void TribesState::consumeFlaggedTribesOfType(int param_1, int param_2) { - short* psVar1; - int iVar2; - psVar1 = &this->tribes[1].unknownBool02; - iVar2 = 1; - do { - if (param_2 <= 0) {} - if (psVar1[-0x139] != 0 && psVar1[-0x138] == param_1 && *psVar1 != 0) { - *psVar1 = 0; - psVar1[1] = 1; + for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { + if (param_2 <= 0) { + return; + } + Tribe& _tribe = this->tribes[_tribeID]; + if (_tribe.tribeState != 0 && _tribe.tribeType == param_1 && _tribe.unknownBool02 != 0) { + _tribe.unknownBool02 = 0; + _tribe.unknownBool01 = 1; param_2 = param_2 + -1; } - iVar2 = iVar2 + 1; - psVar1 = psVar1 + 0x19a; - } while (iVar2 < 0x4e2); + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp b/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp index 201bc4a50d..345a54475f 100644 --- a/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp @@ -8,17 +8,12 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00523750 void TribesState::flagTribesOfType(int param_1) { - Tribe* psVar1; - int iVar1; - psVar1 = &this->tribes[1]; - iVar1 = 0x4e1; - do { - if (psVar1->tribeState && (short)psVar1->tribeType == param_1) { - psVar1->unknownBool02 = 1; + for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { + Tribe& _tribe = this->tribes[_tribeID]; + if (_tribe.tribeState && (short)_tribe.tribeType == param_1) { + _tribe.unknownBool02 = 1; } - psVar1 = psVar1 + 0x19a; - iVar1 = iVar1 + -1; - } while (iVar1); + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp index 343f1fe9f9..96b207b5c1 100644 --- a/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp @@ -42,7 +42,7 @@ namespace Map { *(int*)((int)_countOfUnitTypes + (short)_pUnit->unitType * 4) = *(int*)((int)_countOfUnitTypes + (short)_pUnit->unitType * 4) + 1; } - _pUnit = _pUnit + 0x248; + _pUnit = _pUnit + 1; _countInclusive = _countInclusive + -1; } while (_countInclusive); } diff --git a/src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp b/src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp index 26ba4e796a..6179e2b41a 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeSelectedUnitsFromTheirCurrentTribes.cpp @@ -19,10 +19,8 @@ namespace Map { { Unit* _pUnit; int _unitTribeID; - int _tribeID; uint _unitID; bool _unitRemovedFromTribeByID[1250]; - Tribe* _pTribe; uint uVar1 = MSVC_SecurityCookie::instance ^ (uint)_unitRemovedFromTribeByID; MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( 1250, '\0', (void*)((int)(_unitRemovedFromTribeByID))); @@ -38,24 +36,18 @@ namespace Map { _unitRemovedFromTribeByID[_unitTribeID] = true; } _unitID = _unitID + 1; - _pUnit = _pUnit + 0x248; + _pUnit = _pUnit + 1; } while ((int)_unitID < DAT_UnitsState::instance.maxUnitCount); } - _tribeID = 1; - _pTribe = &this->tribes[1]; - do { - /* - tribeState - */ - if (_pTribe->tribeState == 2) { - if (_pTribe->selectionTargetUnitID == -1) { + for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { + Tribe& _tribe = this->tribes[_tribeID]; + if (_tribe.tribeState == 2) { + if (_tribe.selectionTargetUnitID == -1) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(_tribeID); } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(_tribeID); } - _tribeID = _tribeID + 1; - _pTribe = _pTribe + 1; - } while (_tribeID < 1250); + } ; } diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index 77b328ef1e..3ff322883c 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -54,7 +54,7 @@ namespace Map { */ _atLeastTwoTribes = true; } - _pUnit = _pUnit + 0x248; + _pUnit = _pUnit + 1; _unitIDCountdown = _unitIDCountdown + -1; _tribeID_1 = _tribeID_chosen; } while (_unitIDCountdown); diff --git a/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp b/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp index 4d8a3c25e7..9dc3863afd 100644 --- a/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp +++ b/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp @@ -1,6 +1,8 @@ #include "../../../Map.func.hpp" #include "../TribesState.func.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" + namespace OpenSHC { namespace Map { namespace Units { @@ -8,18 +10,20 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00521240 void TribesState::swapTribeOwnership(int param_1, int param_2) { - Tribe* piVar1; - piVar1 = &this->tribes[1]; - do { - if (piVar1->tribeState == 2) { - if (piVar1->owner == param_1) { - piVar1->owner = param_2; - } else if (piVar1->owner == param_2) { - piVar1->owner = param_1; + /* + The original walks the global instance, not `this`: both the start and the + end of the loop are absolute addresses in the binary. + */ + for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { + Tribe& _tribe = DAT_TribesState::instance.tribes[_tribeID]; + if (_tribe.tribeState == 2) { + if (_tribe.owner == param_1) { + _tribe.owner = param_2; + } else if (_tribe.owner == param_2) { + _tribe.owner = param_1; } } - piVar1 = piVar1 + 0xcd; - } while ((int)piVar1 < 0x176238c); + } } } From 8bc4767b6f08e713139934c8ea6352971bf34451 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 13:16:01 +0200 Subject: [PATCH 009/202] reimplement: restore six loops the import had dropped the exit from Namespace average 74.78% -> 76.43%. Six functions carried an `if (...) {}` with an empty body where the original leaves a loop or returns, which made them either loop forever or run on with stale values: - applyLadderDestructionToTribeUnits 27.8% -> 93.1%. Four of the six unit tests had been dropped to empty bodies; the original skips the unit on each, so they belong in the one condition. The climb call is also two calls with constant arguments, not one call with `(uint)(flag == 0)`, which costs a `sete` - and the `!= 0` arm is the one the original places first (quick_diff 0.9138 with the arms the other way round, 1.0000 as written). - respawnDeer 57.1% -> 100%. One `for` over five candidates where the import had a `while (true)` that could only be left by its `break`, so a run with no valid tile fell through to the spawn with the last candidate's coordinates. The spawn and its `return` now sit inside the loop, the candidate choice is an if/else (the original branches, it does not overwrite), and the organism ID is an `int` local, which is the `movsx` the original has. - moveTribeToNearbyClearTile 77.2% -> 98.4%. Same shape: with no reachable candidate the import issued a move order anyway. 0xb4df44 is one past field1608_0xcc4[39].y, so the cursor and its magic end address are a plain 40-entry loop. The unit-ID local is `int`, not `short`. - removeUnitFromThisTribeIfInTribe 30.5% -> 79.5%, the same corrections as its twin removeUnitFromTribe. Introducing a `Tribe&` for the repeated `tribes[tribeID]` was measured and reverted: 0.7945 -> 0.6620 here and 0.6709 -> 0.6144 there. - updateTribes 80.2% -> 91.2%. The loop ran one tribe past the end: the original's `cmp 0x4e2; jl` is `< 1250`, not `<= 1250`. - updateTribeUnitAssignments 60.0% -> 57.1%, which is the one regression and is accepted. Its loop could not terminate either; the two gotos are `continue`s, so the increment has to move into a `for` header, and that entry test is the cost. The alternative, a `clearTribe` call in each arm with the do-while kept, measures worse (0.6076 against 0.6250). Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../applyLadderDestructionToTribeUnits.cpp | 34 ++++++++++----- .../moveTribeToNearbyClearTile.cpp | 42 +++++++++--------- .../removeUnitFromThisTribeIfInTribe.cpp | 26 +++++------ .../Map/Units/TribesState/respawnDeer.cpp | 43 +++++++++++-------- .../updateTribeUnitAssignments.cpp | 23 ++++++---- .../Map/Units/TribesState/updateTribes.cpp | 3 +- 6 files changed, 101 insertions(+), 70 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp index c24ee9aae0..fa87e577a5 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyLadderDestructionToTribeUnits.cpp @@ -27,16 +27,30 @@ namespace Map { unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( param_1, unitSelectionIndex); unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[unitID].dying == 0)) { - if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0) {} - if (DAT_UnitsState::instance.units[unitID].field303_0x413 != 0) {} - if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging == 2) {} - if (DAT_UnitsState::instance.units[unitID].state.generic - == Map::Units::States::US_MELEE_ATTACK) {} - bool bVar2 = this->tribes[param_1].unknownBool02 == 0; - BVar1 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::computeLadderClimbPath, - DAT_UnitsState::ptr)(unitID, (uint)(bVar2), 0, (int)(bVar2)); + /* + The import left four of these six tests as `if (...) {}` with empty + bodies; the original skips the unit on each of them, so they belong + in the one condition. + */ + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0 + && DAT_UnitsState::instance.units[unitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[unitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging != 2 + && DAT_UnitsState::instance.units[unitID].state.generic + != Map::Units::States::US_MELEE_ATTACK) { + /* + Two calls, not one with a computed flag: the original branches on + the tribe flag and pushes the constants in each arm, where + `(uint)(unknownBool02 == 0)` costs a `sete`. + */ + if (this->tribes[param_1].unknownBool02 != 0) { + BVar1 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::computeLadderClimbPath, + DAT_UnitsState::ptr)(unitID, 0, 0, 0); + } else { + BVar1 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::computeLadderClimbPath, + DAT_UnitsState::ptr)(unitID, 1, 0, 1); + } if (BVar1) { DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_DEATH_02 | Map::Units::States::US_STAND_UPUnk; diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp index 9539b46b31..d40ebe2ed6 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp @@ -18,28 +18,30 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x0052A640 void TribesState::moveTribeToNearbyClearTile(int param_1) { - int iVar3; - int* piVar4; - uint uVar5; - uint x1; - uint y1; - short sVar1 = this->tribes[param_1].selectionTargetUnitID; - uVar5 = (uint)DAT_UnitsState::instance.units[sVar1].x; + int sVar1 = this->tribes[param_1].selectionTargetUnitID; + uint uVar5 = (uint)DAT_UnitsState::instance.units[sVar1].x; uint uVar2 = (uint)DAT_UnitsState::instance.units[sVar1].y; - piVar4 = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[0].y; - while (true) { - x1 = ((XYPair*)(piVar4 + -1))->x + uVar5; - y1 = *piVar4 + uVar2; - if (x1 < 400 && y1 < 400 && *(char*)(y1 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x1) != '\0' - && (iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( - param_1, uVar5, uVar2, x1, y1), - iVar3 != 0)) - break; - piVar4 = piVar4 + 2; - if (0xb4df43 < (int)piVar4) {} + /* + The import walked a cursor over the candidate offsets and left the loop's exit + test as `if (0xb4df43 < (int)piVar4) {}`, so it fell out of the `while (true)` + only through the `break` and then issued a move order with whatever x1/y1 the + last candidate had left behind. The original returns without an order when no + candidate is reachable. 0xb4df44 is one past field1608_0xcc4[39].y, so the + loop runs over all 40 entries. + */ + for (int _candidate = 0; _candidate < 40; _candidate = _candidate + 1) { + uint const x1 = DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[_candidate].x + uVar5; + uint const y1 = DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[_candidate].y + uVar2; + if (x1 <= 399 && y1 <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] != 0 + && MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( + param_1, uVar5, uVar2, x1, y1) + != 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + param_1, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + return; + } } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - param_1, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); } } diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index f7510778c3..74b64fefd7 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -11,25 +11,27 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522630 void TribesState::removeUnitFromThisTribeIfInTribe(uint unitID, int tribeID) { - short* psVar1; - uint uVar2; - short _size; - if (DAT_UnitsState::instance.units[unitID].tribeID == 0) { - uVar2 = (int)unitID % 16; - if ((ushort)(DAT_UnitSelectionDefinedData::instance.BitMaskHelper[uVar2] - & this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16]) - == 0) {} + /* + The import had this guard with an empty body; the original returns. Like + removeUnitFromTribe, unitID is divided and taken modulo as a signed int, and + the selection-bit test is a 32-bit `test` of a movsx'd field against a + movzx'd mask. + */ + if (DAT_UnitsState::instance.units[unitID].tribeID == 0 + && ((int)this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] + & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) + == 0) { + return; } - _size = this->tribes[tribeID].size; + short const _size = this->tribes[tribeID].size; if (0 < _size) { this->tribes[tribeID].size = _size + -1; } if (this->tribes[tribeID].size <= 0) { this->tribes[tribeID].tribeState = 3; } - psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); - uVar2 = (int)unitID % 16; - *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[uVar2]; + short* psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); + *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[(int)unitID % 16]; if (DAT_UnitsState::instance.units[unitID].tribeID == tribeID) { DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = 0; DAT_UnitsState::instance.units[unitID].tribeID = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp b/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp index ba555ede3f..73509d1662 100644 --- a/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp @@ -40,31 +40,38 @@ namespace Map { && DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY && DAT_GameState::instance.mapAndTime.aliveDeerCount && DAT_GameState::instance.mapAndTime.deerCount < 6) { - int _counter = 0; - while (true) { - _x = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].x; - _y = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].y; + /* + One `for` over five spawn candidates, where the import had a `while (true)` + with an `if (4 < _counter) {}` that never left it and a goto back in from + the tile check. The original ends the loop on `cmp ebp, 5; jl`, and spawns + at most one deer before returning. + */ + for (int _counter = 0; _counter < 5; _counter = _counter + 1) { if (_counter < 4) { _x = (int)DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_counter].x; _y = (int)DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_counter].y; + } else { + _x = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].x; + _y = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].y; } BVar1 = MACRO_CALL_MEMBER( Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)(_x, _y); - if (BVar1) - break; - LAB_00526268: - _counter = _counter + 1; - if (4 < _counter) {} - } - int _tile = DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile + _x; - if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x50501581U)) { - if (DAT_TileMapState::instance.OrganismLayer[_tile] == 0) - goto LAB_00526268; - MACRO_CALL_MEMBER(Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)( - (int)DAT_TileMapState::instance.OrganismLayer[_tile]); + if (!BVar1) { + continue; + } + int _tile = DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile + _x; + if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x50501581U)) { + int const _organismID = DAT_TileMapState::instance.OrganismLayer[_tile]; + if (_organismID == 0) { + continue; + } + MACRO_CALL_MEMBER(Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)( + _organismID); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( + Commands::M_MAPPER_DEER, _x, _y, (int)(DAT_TileMapState::instance.HeightLayer[_tile])); + return; } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( - Commands::M_MAPPER_DEER, _x, _y, (int)(DAT_TileMapState::instance.HeightLayer[_tile])); } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp index 580e95a6de..4450aa3316 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp @@ -19,8 +19,18 @@ namespace Map { uint _unitID; this->clans = 0; this->field2_0x8 = 1; - DAT_CurrentTribeID::instance = 1; - do { + /* + A `for`, not the original's do-while, because the two `continue`s have to run + the increment: the label they replace sat in front of it. The first test is + always true, so the two forms are equivalent. This costs 2.9 points + (60.0% -> 57.1%) for the entry test the do-while does not have; writing the + arms out with a `clearTribe` call in each instead measures worse still + (quick_diff 0.6076 against 0.6250), so the `for` stands. The import's + `if (0x4e1 < instance) {}` had an empty body, which made this loop run + forever. + */ + for (DAT_CurrentTribeID::instance = 1; (int)DAT_CurrentTribeID::instance < 1250; + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1) { int _state = this->tribes[DAT_CurrentTribeID::instance].tribeState; int _tribeUnitIndex = 0; if (_state) { @@ -31,7 +41,7 @@ namespace Map { int _tribeTime = this->tribes[DAT_CurrentTribeID::instance].time; if (_tribeTime && (int)DAT_GameCore::instance.mapTimeInTicks <= (int)_tribeTime && _tribeTime) - goto LAB_00527425; + continue; } else { int local_4 = 0; if (0 < _tribeSize) { @@ -55,17 +65,14 @@ namespace Map { if (local_4) { this->clans = this->clans + 1; this->field2_0x8 = DAT_CurrentTribeID::instance; - goto LAB_00527425; + continue; } } } } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::clearTribe, this)(_tribeID); } - LAB_00527425: - DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; - if (0x4e1 < DAT_CurrentTribeID::instance) {} - } while (true); + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp index cf7acaf97e..3aecf07358 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp @@ -123,8 +123,7 @@ namespace Map { } } DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; - if (1250 < DAT_CurrentTribeID::instance) {} - } while (true); + } while ((int)DAT_CurrentTribeID::instance < 1250); } } From 53f8ad99d5f7ff2f5f13316ce39328683c1a8690 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 13:23:50 +0200 Subject: [PATCH 010/202] types: UnitsState::selectedUnitsBitFlags is a short array Declared `byte[400]`, but both of its users already had to cast it: one through `(short*)` and one through `*(ushort*)(.. + i * 2)`. The original reads it as `movsx edx, word ptr [eax*2 + ..]`, so it is `short[200]`. unitIsSelectedByPlayer 56.2% -> 100% once the read is written without the cast; addUnitToSelected stays at 100%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/UnitsState.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/UnitsState.hpp b/src/OpenSHC/Map/Units/UnitsState.hpp index 48baed04b0..12d547a8fd 100644 --- a/src/OpenSHC/Map/Units/UnitsState.hpp +++ b/src/OpenSHC/Map/Units/UnitsState.hpp @@ -50,7 +50,7 @@ namespace Map { int totalUnitsInSelection; // 0x00000020 length: 4 int unitCountOfSelection[9]; // 0x00000024 length: 36 byte unusedBytes01[44]; // 0x00000048 length: 44 - byte selectedUnitsBitFlags[400]; // 0x00000074 length: 400 + short selectedUnitsBitFlags[200]; // 0x00000074 length: 400 byte unusedBytes02[852]; // 0x00000204 length: 852 BOOLEnum hasEngineerSelected; // 0x00000558 length: 4 int field14_0x55c; // 0x0000055C length: 4 From 5f45ab5c93ea28eade5a110a3d42c460ff5f1cab Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 13:23:50 +0200 Subject: [PATCH 011/202] reimplement: unitIsSelectedByPlayer to 100%, two more dropped returns Namespace average 76.43% -> 76.90%. unitIsSelectedByPlayer 56.2% -> 100%. With selectedUnitsBitFlags correctly typed the whole body is one expression: the `& 0x8000000f` plus negative repair is `% 16`, the mask-and-negate tail is `!= 0` on a BOOLEnum return, and both operands widen to int, which is the movsx/movzx pair the original has. addUnitToSelected keeps its 100% and loses its `(short*)` cast. predictUnitInterceptPosition 22.9% -> 30.9%. Two guards had empty bodies. The first is load-bearing: the original jumps 0x1dd bytes to the epilogue, so the entire body runs only when the unit has finished digging. The second, inside the path-plan loop, is the loop's own exit. currentIndexInPathPlan is read as a signed int (`movsx`), not `(uint)`. updateTribeUnitAssignments 57.1% -> 58.4%. diff_types.py EXTEND: the original zero-extends its reads of tribeState and size here, so both locals take a `(ushort)` cast at the read site. The fields stay signed - size is a signed loop bound elsewhere in the namespace. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/addUnitToSelected.cpp | 2 +- .../TribesState/predictUnitInterceptPosition.cpp | 15 ++++++++++++--- .../Units/TribesState/unitIsSelectedByPlayer.cpp | 9 +++------ .../TribesState/updateTribeUnitAssignments.cpp | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp index 4268378129..05e6879029 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp @@ -12,7 +12,7 @@ namespace Map { { int const bitFlagsIndex = unitID / 16; int const bitMaskIndex = unitID % 16; - ((short*)DAT_UnitsState::instance.selectedUnitsBitFlags)[bitFlagsIndex] + DAT_UnitsState::instance.selectedUnitsBitFlags[bitFlagsIndex] |= DAT_UnitSelectionDefinedData::instance.BitMaskHelper[bitMaskIndex]; } diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index b703135830..b9a6c5c622 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -25,7 +25,14 @@ namespace Map { *unitCurrentX = (int)DAT_UnitsState::instance.units[unitID].x; int _yOffb = (int)DAT_UnitsState::instance.units[unitID].y; *unitCurrentY = _yOffb; - if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging != 2) {} + /* + The import had this guard with an empty body; the original jumps straight to + the epilogue, so everything below runs only for a unit that has finished + digging. + */ + if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging != 2) { + return; + } _targetTribeMinSpeed = this->tribes[DAT_UnitsState::instance.units[targetUnitID].tribeID].minimumMovementSpeed; int _targetTribeMinSpeedBounded = (int)_targetTribeMinSpeed; @@ -76,7 +83,7 @@ namespace Map { _yOff_2b = _xOff_2b; } if ((-1 < (_yOff_2b + -2) * unitID) - && (_currentIndexInPathPlan = (uint)DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan, + && (_currentIndexInPathPlan = (int)DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan, local_8 = _tribeMinSpeedBounded * 4, (int)_currentIndexInPathPlan < (int)DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan)) { do { @@ -110,7 +117,9 @@ namespace Map { if (_yOffb < iVar3) { _yOffb = iVar3; } - if ((_yOffb + -2) * unitID < local_8) {} + if ((_yOffb + -2) * unitID < local_8) { + return; + } local_8 = local_8 + _tribeMinSpeedBounded * 4; _currentIndexInPathPlan = _currentIndexInPathPlan + 1; } while ( diff --git a/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp index b6e7f3d1f3..f47865c1cf 100644 --- a/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp @@ -11,12 +11,9 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522550 BOOLEnum TribesState::unitIsSelectedByPlayer(uint tribeID) { - uint uVar1 = tribeID & 0x8000000f; - if ((int)uVar1 < 0) { - uVar1 = (uVar1 - 1 | 0xfffffff0) + 1; - } - return (uint)((DAT_UnitSelectionDefinedData::instance.BitMaskHelper[uVar1] - & *(ushort*)(DAT_UnitsState::instance.selectedUnitsBitFlags + ((int)tribeID / 16) * 2))); + return ((int)DAT_UnitsState::instance.selectedUnitsBitFlags[(int)tribeID / 16] + & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)tribeID % 16]) + != 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp index 4450aa3316..0ab67a5e93 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp @@ -31,12 +31,12 @@ namespace Map { */ for (DAT_CurrentTribeID::instance = 1; (int)DAT_CurrentTribeID::instance < 1250; DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1) { - int _state = this->tribes[DAT_CurrentTribeID::instance].tribeState; + int _state = (ushort)this->tribes[DAT_CurrentTribeID::instance].tribeState; int _tribeUnitIndex = 0; if (_state) { int _tribeID = DAT_CurrentTribeID::instance; if (_state != 3) { - int _tribeSize = this->tribes[DAT_CurrentTribeID::instance].size; + int _tribeSize = (ushort)this->tribes[DAT_CurrentTribeID::instance].size; if (_tribeSize <= 0) { int _tribeTime = this->tribes[DAT_CurrentTribeID::instance].time; if (_tribeTime && (int)DAT_GameCore::instance.mapTimeInTicks <= (int)_tribeTime From 960fc325b509b468dbd8373ee09fa1276f8ab0a1 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 13:39:25 +0200 Subject: [PATCH 012/202] reimplement: four more TribesState functions Namespace average 76.90% -> 77.66%, nothing worse. getSpecificUnitFromTribe 16.5% -> 53.5% (norm 55.1%). Three corrections, all from the asm: the 200 bound is a signed compare on the undefined4 global, the `param_2 == 0` test is against the immediate rather than a zeroed register, and `1 << ((byte)i & 0x1f)` is decompiler noise for `1 << i` - x86 masks the shift count anyway, and spelling it out costs three instructions. Both globals are also read before the bound test, which is the order the original loads them in. snapshotSelectionTribeAndComputeStance 14.5% -> 47.1%. The tail had three unconditional stores to unitStance, so the first two were dead; the original is a three-way chain ending in `setge`. The tribe snapshot is a whole-struct assignment, which is the original's `rep movsd` of 0xcd dwords - the import's int-by-int loop compiles to a loop. The two decompiler variables tracking the last tribe ID are one variable, and the "at least two tribes" flag is a dword, not a `bool`. Reading _stanceCount[] at the end instead of keeping the three counters was measured and is worse (0.5806 -> 0.4725). applyUnitTopSpeedDelayBasedOnTribeSize 25.6% -> 39.8%. The original puts the big-tribe branch first - it tests the size and jumps away when it is below 40. Also: a typed `UnitIDMovementDistancePair*` cursor instead of a `short*` with `((Pair*)(p - 1))->unitID`, sizeof for the 4004 byte count (they are equal), the sort's two misnamed locals (`_distance` held the unit ID and `_unitID` the distance) swapped back, and the reused `tribeID` parameter replaced by a counter. playUnitCombatSpeechForTarget 26.1% -> 28.1%. Five `if (sVar1 < 0xf) {}` with empty bodies, where the original jumps to the epilogue. Retyping its int out-parameter, which the decompiler had as `TribesState*`, was measured separately and costs 8 points, so it stays wrong for now: this function is 284 original instructions against our 244 and needs rebuilding from the jump table before the type can be fixed. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...applyUnitTopSpeedDelayBasedOnTribeSize.cpp | 161 +++++++++--------- .../TribesState/getSpecificUnitFromTribe.cpp | 16 +- .../playUnitCombatSpeechForTarget.cpp | 20 ++- ...snapshotSelectionTribeAndComputeStance.cpp | 77 ++++----- 4 files changed, 137 insertions(+), 137 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp index 6e83886c1d..2ff43fa40f 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp @@ -18,106 +18,97 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00524EF0 void TribesState::applyUnitTopSpeedDelayBasedOnTribeSize(int tribeID, BOOLEnum param_2) { - int _unitInSelection; - int _selectionUnit; - UnitIDMovementDistancePair* _nextUnit; - int _delayIndex; - int iVar1; - UnitIDMovementDistancePair* pUVar2; - short* _ptrMovementDistance; - int _unitSelectionIndex; - short _distance; - int _foundUnsortedEntry; - short _nextUnitID; - short* _tribeSize; - short _unitID; - _tribeSize = &this->tribes[tribeID].size; - _unitSelectionIndex = 0; - if (*_tribeSize < 40) { - /* - For selections (tribes) of size 40 or less - */ - this->someUnitIDArrayCount = 0; - MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - 4004, '\0', (void*)((int)(this->someUnitIDMovementDistancePair))); + short* _tribeSize = &this->tribes[tribeID].size; + int _unitSelectionIndex = 0; + /* + The original places the big-tribe branch first: it tests the size and jumps + away when it is below 40. + */ + if (40 <= *_tribeSize) { if (0 < *_tribeSize) { - /* - Of all units in the selection store unitID and optionally their movementDistance into an array - */ - _ptrMovementDistance = &this->someUnitIDMovementDistancePair[0].movementDistance; do { - _selectionUnit + /* + Set incremental delay on the first 199 units, then the same value + of 199 for the rest + */ + int const _unitInSelection = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, _unitSelectionIndex); - if ((DAT_UnitsState::instance.units[_selectionUnit].logicalState - == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[_selectionUnit].dying == 0)) { - this->someUnitIDArrayCount = this->someUnitIDArrayCount + 1; - /* - sets unitID - */ - ((UnitIDMovementDistancePair*)(_ptrMovementDistance + -1))->unitID = (short)_selectionUnit; - if (!param_2) { - *_ptrMovementDistance = DAT_UnitsState::instance.units[_selectionUnit].movementDistance; - } + int iVar1 = _unitSelectionIndex; + if (199 <= _unitSelectionIndex) { + iVar1 = 199; } + DAT_UnitsState::instance.units[_unitInSelection].topSpeedDelayIndex = (short)iVar1; _unitSelectionIndex = _unitSelectionIndex + 1; - _ptrMovementDistance = _ptrMovementDistance + 2; } while (_unitSelectionIndex < *_tribeSize); } + return; + } + /* + For selections (tribes) of size 39 or less + */ + this->someUnitIDArrayCount = 0; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->someUnitIDMovementDistancePair), '\0', this->someUnitIDMovementDistancePair); + if (0 < *_tribeSize) { + /* + Of all units in the selection store unitID and optionally their + movementDistance into an array + */ + UnitIDMovementDistancePair* _entry = this->someUnitIDMovementDistancePair; do { - /* - Do a sort of the array, sort by distance value (low to high) - */ - _foundUnsortedEntry = 0; - /* - fixme:reuse: WARNING reuse of parameter - */ - tribeID = 0; - if (this->someUnitIDArrayCount == 1 || this->someUnitIDArrayCount + -1 < 0) - break; - pUVar2 = this->someUnitIDMovementDistancePair; - do { - _nextUnit = pUVar2 + 1; - _distance = pUVar2->unitID; - _unitID = pUVar2->movementDistance; - _nextUnitID = pUVar2[1].movementDistance; - if ((!param_2) && (_nextUnitID < _unitID)) { - _foundUnsortedEntry = 1; - pUVar2->unitID = _nextUnit->unitID; - _nextUnit->unitID = _distance; - pUVar2->movementDistance = _nextUnitID; - pUVar2[1].movementDistance = _unitID; - } - tribeID = tribeID + 1; - pUVar2 = _nextUnit; - } while (tribeID < this->someUnitIDArrayCount + -1); - } while (_foundUnsortedEntry); - _delayIndex = 0; - if (0 < this->someUnitIDArrayCount) { - pUVar2 = this->someUnitIDMovementDistancePair; - do { - DAT_UnitsState::instance.units[pUVar2->unitID].topSpeedDelayIndex = (short)_delayIndex; - _delayIndex = _delayIndex + 1; - pUVar2 = pUVar2 + 1; - } while (_delayIndex < this->someUnitIDArrayCount); - } - } else if (0 < *_tribeSize) { - do { - /* - Set incremental delay on first 199 units, then set same value of 199 - */ - _unitInSelection + int const _selectionUnit = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, _unitSelectionIndex); - iVar1 = _unitSelectionIndex; - if (199 < _unitSelectionIndex) { - iVar1 = 199; + if ((DAT_UnitsState::instance.units[_selectionUnit].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_selectionUnit].dying == 0)) { + this->someUnitIDArrayCount = this->someUnitIDArrayCount + 1; + _entry->unitID = (short)_selectionUnit; + if (!param_2) { + _entry->movementDistance + = DAT_UnitsState::instance.units[_selectionUnit].movementDistance; + } } - DAT_UnitsState::instance.units[_unitInSelection].topSpeedDelayIndex = (short)iVar1; _unitSelectionIndex = _unitSelectionIndex + 1; + _entry = _entry + 1; } while (_unitSelectionIndex < *_tribeSize); } + int _foundUnsortedEntry; + do { + /* + Bubble sort of the array by movementDistance, low to high + */ + _foundUnsortedEntry = 0; + int _pairIndex = 0; + if (this->someUnitIDArrayCount == 1 || this->someUnitIDArrayCount + -1 < 0) { + break; + } + UnitIDMovementDistancePair* pUVar2 = this->someUnitIDMovementDistancePair; + do { + UnitIDMovementDistancePair* _nextUnit = pUVar2 + 1; + short const _unitID = pUVar2->unitID; + short const _distance = pUVar2->movementDistance; + short const _nextDistance = _nextUnit->movementDistance; + if ((!param_2) && (_nextDistance < _distance)) { + _foundUnsortedEntry = 1; + pUVar2->unitID = _nextUnit->unitID; + _nextUnit->unitID = _unitID; + pUVar2->movementDistance = _nextDistance; + _nextUnit->movementDistance = _distance; + } + _pairIndex = _pairIndex + 1; + pUVar2 = _nextUnit; + } while (_pairIndex < this->someUnitIDArrayCount + -1); + } while (_foundUnsortedEntry); + int _delayIndex = 0; + if (0 < this->someUnitIDArrayCount) { + UnitIDMovementDistancePair* pUVar2 = this->someUnitIDMovementDistancePair; + do { + DAT_UnitsState::instance.units[pUVar2->unitID].topSpeedDelayIndex = (short)_delayIndex; + _delayIndex = _delayIndex + 1; + pUVar2 = pUVar2 + 1; + } while (_delayIndex < this->someUnitIDArrayCount); + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp index cabe82d2ea..37f01112a9 100644 --- a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp @@ -12,18 +12,22 @@ namespace Map { int TribesState::getSpecificUnitFromTribe(int tribeID, int param_2) { short* psVar3; - if (!param_2) { + if (param_2 == 0) { DAT_Tribe_HighestID::instance = (int)this->tribes[tribeID].highestID; DAT_00ee0fb4::instance = 0; } - if (DAT_Tribe_HighestID::instance < 200) { - psVar3 = this->tribes[tribeID].unitSelectionBitMasked + DAT_Tribe_HighestID::instance; - int iVar1 = DAT_Tribe_HighestID::instance; - int iVar2 = DAT_00ee0fb4::instance; + /* + Both globals are read before the bound test, which is the order the original + loads them in. + */ + int iVar1 = DAT_Tribe_HighestID::instance; + int iVar2 = DAT_00ee0fb4::instance; + if (iVar1 < 200) { + psVar3 = this->tribes[tribeID].unitSelectionBitMasked + iVar1; do { if ((*psVar3 != 0) && (iVar2 < 0x10)) { do { - if ((int)*psVar3 & 1 << ((byte)iVar2 & 0x1f)) { + if ((int)*psVar3 & 1 << iVar2) { DAT_00ee0fb4::instance = iVar2 + 1; if (iVar2 + 1 != 0x10) { DAT_Tribe_HighestID::instance = iVar1; diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index b7f5f8ed2c..37c5ed54e5 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -101,7 +101,9 @@ namespace Map { case Map::Units::UT_E_SPEAR: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) {} + if (sVar1 < 0xf) { + return; + } _otherX = 0x14; break; case Map::Units::UT_E_PIKE: @@ -116,7 +118,9 @@ namespace Map { } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, _otherX); - if (sVar1 < 0xf) {} + if (sVar1 < 0xf) { + return; + } _otherX = 0x14; break; case Map::Units::UT_E_SWORD: @@ -135,7 +139,9 @@ namespace Map { LAB_00521d1b: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) {} + if (sVar1 < 0xf) { + return; + } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); @@ -164,7 +170,9 @@ namespace Map { (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x59); MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) {} + if (sVar1 < 0xf) { + return; + } _otherX = 0xfa; break; default: @@ -180,7 +188,9 @@ namespace Map { case Map::Units::UT_E_ENGINEER: return; case Map::Units::UT_A_SWORDSMAN: - if (sVar1 < 0xf) {} + if (sVar1 < 0xf) { + return; + } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index 3ff322883c..9d6fada89b 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -16,70 +16,65 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522CA0 void TribesState::snapshotSelectionTribeAndComputeStance(int playerID) { - int _unitTribeID; - int _tribeCountdown; - int _tribeID_chosen; - Unit* _pUnit; - int _unitIDCountdown; - int _stanceCount[3]; - int _countDefensive; - int _countStandground; - int _tribeID_1; - bool _atLeastTwoTribes; - _atLeastTwoTribes = false; + BOOLEnum _atLeastTwoTribes = FALSE; this->tribeCopiedToSlot0 = 0; - int _index = 0; + int _stanceCount[3]; _stanceCount[0] = 0; _stanceCount[1] = 0; _stanceCount[2] = 0; - int _countAggressive = _index; - _countDefensive = 0; - _countStandground = 0; + /* + Three locals rather than reading _stanceCount[] again at the end: the extra + stack slot costs nothing and reading the array directly measures worse + (quick_diff 0.5806 -> 0.4725, and it moves `this` out of ebx). + */ + int _countAggressive = 0; + int _countDefensive = 0; + int _countStandground = 0; + int _lastTribeID = -1; if (1 < DAT_UnitsState::instance.maxUnitCount) { - _pUnit = &DAT_UnitsState::instance.units[1]; - _unitIDCountdown = DAT_UnitsState::instance.maxUnitCount - 1; - _tribeID_1 = -1; + Unit* _pUnit = &DAT_UnitsState::instance.units[1]; + int _unitIDCountdown = DAT_UnitsState::instance.maxUnitCount - 1; do { - _tribeID_chosen = _tribeID_1; if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying - && _pUnit->ifSelectedThenPlayerID == playerID - && (_unitTribeID = (int)_pUnit->tribeID, - 0 < _unitTribeID - && (_stanceCount[(short)this->tribes[_unitTribeID].unitStance] - = _stanceCount[(short)this->tribes[_unitTribeID].unitStance] + 1, - _tribeID_chosen = _unitTribeID, _tribeID_1 != -1)) - && _unitTribeID != _tribeID_1) { - /* - if unit is in a tribe, count its tribes stance and if - */ - _atLeastTwoTribes = true; + && _pUnit->ifSelectedThenPlayerID == playerID) { + int const _unitTribeID = (int)_pUnit->tribeID; + if (0 < _unitTribeID) { + /* + count the stance of every tribe the selection touches, and + notice as soon as it touches two different ones + */ + int const _stance = (short)this->tribes[_unitTribeID].unitStance; + _stanceCount[_stance] = _stanceCount[_stance] + 1; + if (_lastTribeID != -1 && _unitTribeID != _lastTribeID) { + _atLeastTwoTribes = TRUE; + } + _lastTribeID = _unitTribeID; + } } _pUnit = _pUnit + 1; _unitIDCountdown = _unitIDCountdown + -1; - _tribeID_1 = _tribeID_chosen; } while (_unitIDCountdown); _countAggressive = _stanceCount[2]; _countDefensive = _stanceCount[1]; _countStandground = _stanceCount[0]; - if ((_tribeID_chosen != -1) && (!_atLeastTwoTribes)) { + if (_lastTribeID != -1 && !_atLeastTwoTribes) { /* - this is an int-by-int sized copy function + A whole-struct assignment, which is the `rep movsd` of 0xcd dwords the + original has; a hand-written int-by-int copy compiles to a loop. */ - int const* _src = (int const*)&this->tribes[_tribeID_chosen]; - int* _dst = (int*)&this->tribes[0]; - for (_tribeCountdown = 0; _tribeCountdown < 205; ++_tribeCountdown) { - _dst[_tribeCountdown] = _src[_tribeCountdown]; - } + this->tribes[0] = this->tribes[_lastTribeID]; this->tribeCopiedToSlot0 = 1; } } if (_countAggressive + _countDefensive + _countStandground == 0) { this->tribes[0].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + return; } - if ((_countDefensive <= _countAggressive) && (_countStandground <= _countAggressive)) { - this->tribes[0].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + if (_countAggressive < _countDefensive || _countAggressive < _countStandground) { + this->tribes[0].unitStance = (ushort)(_countStandground <= _countDefensive); + return; } - this->tribes[0].unitStance = (ushort)(_countStandground <= _countDefensive); + this->tribes[0].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; } } From 7d877f40cdfa5574b53c8a48a4931d2290843485 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 13:47:40 +0200 Subject: [PATCH 013/202] reimplement: the magic-address loops, two to 100% Namespace average 77.66% -> 78.35%, nothing worse. clearAnimalSpawnLocationsUnk 40.0% -> 100%. The cursor walked deerSpawnLocationsXY and reached the other three arrays through offsets from it (0x3ae to lionLocationsXY, 0x1d3e to rabbitSpawnXY, 0x1d52 to camelSpawnXY), ending on the absolute address 0x117d1de. All four hold four XY pairs, so it is one four-entry loop over named fields. setRabbitSpawnXY 92.9% -> 100%. Same shape: 0x117ef1c is one past rabbitSpawnXY[3]. importStoredInfoFromSlot0 32.2% -> 42.7%. The rally-point copy is a loop over the two coordinates with the nine points left to the compiler, which is what the original's nine stores plus `add edx, 2` outer step are. The import wrote ten stores, one past the nine-entry array. createTribe: the loop bound is a signed compare on the undefined4 global and the 820 byte count is sizeof(Tribe). The percentage does not move - we are 56 instructions against the original's 64, and the original puts the whole claim block out of line behind a shared `return 0`. Writing the claim as the cold-last arm with a `Tribe&` for the repeated subscript is an exact tie, and is kept because it reads better. Also marks the C4700 in giveTribeAnInstruction: `id | x | id != 0` reads a local nothing assigns. That function has thirteen jump tables and needs rebuilding from the original's branches; the comment is there so the warning is not mistaken for noise. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../clearAnimalSpawnLocationsUnk.cpp | 29 ++++++------ .../Map/Units/TribesState/createTribe.cpp | 45 +++++++++++-------- .../TribesState/giveTribeAnInstruction.cpp | 18 ++++++++ .../TribesState/importStoredInfoFromSlot0.cpp | 32 +++++-------- .../Units/TribesState/setRabbitSpawnXY.cpp | 17 +++---- 5 files changed, 80 insertions(+), 61 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp b/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp index ddd3278e60..915d74f001 100644 --- a/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp +++ b/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp @@ -13,19 +13,22 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005232E0 void TribesState::clearAnimalSpawnLocationsUnk() { - short* psVar1; - psVar1 = &DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[0].y; - do { - ((Point4ShortXY*)(psVar1 + -1))->x = 0; - *psVar1 = 0; - psVar1[0x1d7] = 0; - psVar1[0x1d8] = 0; - psVar1[0xe9f] = 0; - psVar1[0xea0] = 0; - psVar1[0xea9] = 0; - psVar1[0xeaa] = 0; - psVar1 = psVar1 + 2; - } while ((int)psVar1 < 0x117d1de); + /* + The import walked a cursor over deerSpawnLocationsXY and reached the other + three arrays through offsets from it (0x3ae to lionLocationsXY, 0x1d3e to + rabbitSpawnXY, 0x1d52 to camelSpawnXY), ending on the absolute address + 0x117d1de. All four hold four XY pairs. + */ + for (int _slot = 0; _slot < 4; _slot = _slot + 1) { + DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_slot].x = 0; + DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_slot].y = 0; + DAT_GameState::instance.mapAndTime.lionLocationsXY[_slot].x = 0; + DAT_GameState::instance.mapAndTime.lionLocationsXY[_slot].y = 0; + DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0] = 0; + DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1] = 0; + DAT_GameState::instance.mapAndTime.camelSpawnXY[_slot].x = 0; + DAT_GameState::instance.mapAndTime.camelSpawnXY[_slot].y = 0; + } DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor = 0; } diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index e3ffcd3989..c6f5377b73 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -25,28 +25,35 @@ namespace Map { int _tribeID = DAT_CurrentTribeID::instance; if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0) { MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - 820, '\0', (void*)((int)(this->tribes + DAT_CurrentTribeID::instance))); - if (_tribeID <= 0) { - return 0; + sizeof(Tribe), '\0', &this->tribes[DAT_CurrentTribeID::instance]); + /* + The claim is the cold-last arm, which is what puts it out of line in + the original, behind the shared `return 0` epilogue. + */ + if (0 < _tribeID) { + Tribe& _tribe = this->tribes[_tribeID]; + if ((playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) + && (setAsCurrentTribeID)) { + this->DAT_CurrentTribeID = _tribeID; + } + _tribe.owner = playerID; + _tribe.tribeState = 2; + _tribe.uid = DAT_GameCore::instance.uniqueGameObjectTracker; + DAT_GameCore::instance.uniqueGameObjectTracker + = DAT_GameCore::instance.uniqueGameObjectTracker + 1; + _tribe.tribeType = ((AITribeType)0); + _tribe.field18_0x1c = 0; + _tribe.field19_0x1e = 0; + _tribe.tribeBehaviorType = ((SomeTribeBehaviorType)0); + _tribe.field29_0x30 = 0; + _tribe.selectionTargetUnitID = 0; + _tribe.size = 0; + return _tribeID; } - if ((playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) && (setAsCurrentTribeID)) { - this->DAT_CurrentTribeID = _tribeID; - } - this->tribes[_tribeID].owner = playerID; - this->tribes[_tribeID].tribeState = 2; - this->tribes[_tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; - DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; - this->tribes[_tribeID].tribeType = ((AITribeType)0); - this->tribes[_tribeID].field18_0x1c = 0; - this->tribes[_tribeID].field19_0x1e = 0; - this->tribes[_tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); - this->tribes[_tribeID].field29_0x30 = 0; - this->tribes[_tribeID].selectionTargetUnitID = 0; - this->tribes[_tribeID].size = 0; - return _tribeID; + return 0; } DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; - } while (DAT_CurrentTribeID::instance < 0x4e2); + } while ((int)DAT_CurrentTribeID::instance < 1250); return 0; } diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 6a339abc21..fbc581466a 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -244,6 +244,12 @@ namespace Map { } } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + /* + fixme: `id | x | id != 0` reads `x`, which nothing in this function + ever assigns - MSVC reports C4700 here. The decompiler mangled this + condition and it has to be rebuilt from the original's own branches + along with the rest of the function's thirteen jump tables. + */ if (id | x | id != 0) { /* if there are melee units in the selection @@ -1387,6 +1393,12 @@ namespace Map { DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; } } while (iVar12 < this->tribes[_param_1_tribeID].size); + /* + fixme: `id | x | id != 0` reads `x`, which nothing in this function + ever assigns - MSVC reports C4700 here. The decompiler mangled this + condition and it has to be rebuilt from the original's own branches + along with the rest of the function's thirteen jump tables. + */ if (id | x | id != 0) { sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: @@ -1660,6 +1672,12 @@ namespace Map { } } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + /* + fixme: `id | x | id != 0` reads `x`, which nothing in this function + ever assigns - MSVC reports C4700 here. The decompiler mangled this + condition and it has to be rebuilt from the original's own branches + along with the rest of the function's thirteen jump tables. + */ if (id | x | id != 0) { iVar12 = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; param_5 = (int)DAT_UnitsState::instance.units[iVar12].x; diff --git a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp index bf8f51b77c..4d814c1978 100644 --- a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp +++ b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp @@ -8,9 +8,6 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522DD0 void TribesState::importStoredInfoFromSlot0(undefined4 param_1, int tribeID) { - short (*_ptrStoredRallyPoints)[2]; - int iVar1; - short (*_ptrRallyPoints)[2]; if (this->tribeCopiedToSlot0) { this->tribes[tribeID].field166_0x2be = this->tribes[0].field166_0x2be; this->tribes[tribeID].someUnitID = this->tribes[0].someUnitID; @@ -21,24 +18,17 @@ namespace Map { this->tribes[tribeID].rallyPointCount = this->tribes[0].rallyPointCount; this->tribes[tribeID].isRallyingUnk = this->tribes[0].isRallyingUnk; this->tribes[tribeID].unitStance = this->tribes[0].unitStance; - _ptrRallyPoints = this->tribes[tribeID].rallyPointArray + 1; - _ptrStoredRallyPoints = this->tribes[0].rallyPointArray + 1; - iVar1 = 2; - do { - _ptrRallyPoints[-1][0] = _ptrStoredRallyPoints[-1][0]; - (*_ptrRallyPoints)[0] = (*_ptrStoredRallyPoints)[0]; - _ptrRallyPoints[1][0] = _ptrStoredRallyPoints[1][0]; - _ptrRallyPoints[2][0] = _ptrStoredRallyPoints[2][0]; - _ptrRallyPoints[3][0] = _ptrStoredRallyPoints[3][0]; - _ptrRallyPoints[4][0] = _ptrStoredRallyPoints[4][0]; - _ptrRallyPoints[5][0] = _ptrStoredRallyPoints[5][0]; - _ptrRallyPoints[6][0] = _ptrStoredRallyPoints[6][0]; - _ptrRallyPoints[7][0] = _ptrStoredRallyPoints[7][0]; - _ptrRallyPoints[8][0] = _ptrStoredRallyPoints[8][0]; - _ptrStoredRallyPoints = (short (*)[2])(*_ptrStoredRallyPoints + 1); - _ptrRallyPoints = (short (*)[2])(*_ptrRallyPoints + 1); - iVar1 = iVar1 + -1; - } while (iVar1); + /* + The original unrolls the inner loop over the nine rally points and keeps + the outer one over the two coordinates, stepping its cursor by one short. + The import wrote the nine stores out as ten, one past the array. + */ + for (int _coordinate = 0; _coordinate < 2; _coordinate = _coordinate + 1) { + for (int _point = 0; _point < 9; _point = _point + 1) { + this->tribes[tribeID].rallyPointArray[_point][_coordinate] + = this->tribes[0].rallyPointArray[_point][_coordinate]; + } + } } this->tribes[tribeID].unitStance = this->tribes[0].unitStance; } diff --git a/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp b/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp index be3da1bd87..cf8b15d107 100644 --- a/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp +++ b/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp @@ -10,15 +10,16 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005233A0 void TribesState::setRabbitSpawnXY(undefined4 x, undefined4 y) { - short (*pasVar1)[2]; - pasVar1 = DAT_GameState::instance.mapAndTime.rabbitSpawnXY; - do { - if ((*pasVar1)[0] == 0) { - (*pasVar1)[0] = (short)x; - (*pasVar1)[1] = (short)y; + /* + 0x117ef1c is one past rabbitSpawnXY[3], so the cursor and its magic end + address are a plain four-entry loop. + */ + for (int _slot = 0; _slot < 4; _slot = _slot + 1) { + if (DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0] == 0) { + DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0] = (short)x; + DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1] = (short)y; } - pasVar1 = pasVar1 + 1; - } while ((int)pasVar1 < 0x117ef1c); + } DAT_GameState::instance.mapAndTime.rabbitSpawnXY[0][0] = (short)x; DAT_GameState::instance.mapAndTime.rabbitSpawnXY[0][1] = (short)y; } From e053a22fa82730b8ea5aee009315a991567c0875 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 13:47:46 +0200 Subject: [PATCH 014/202] status: TribesState at 78.35% average, 49 of 113 at 100% 25 lines updated from the current reccmp run. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 50 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 367f88edfe..0a1800fd0c 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46385,15 +46385,15 @@ SHC_3BB0A8C1_0x005211B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521210 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00521240 | 80.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521280 | 27.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521500 | 22.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521500 | 30.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending -SHC_3BB0A8C1_0x00521720 | 69.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521720 | 71.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005217E0 | 46.5% | Reimplemented, asm differs @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 26.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 28.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending @@ -46771,7 +46771,7 @@ SHC_3BB0A8C1_0x00522150 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522160 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005221A0 | 67.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005221A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005221C0 | 0.0% | Pending @@ -46783,17 +46783,17 @@ SHC_3BB0A8C1_0x00522360 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522390 | 36.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522410 | 16.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522410 | 53.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x005224E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522520 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522550 | 56.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522550 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522590 | 86.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522630 | 30.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522630 | 79.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522720 | 60.4% | Reimplemented, asm differs @@ -46803,9 +46803,9 @@ SHC_3BB0A8C1_0x00522890 | 30.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005229C0 | 62.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005229C0 | 64.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522B00 | 56.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522B00 | 57.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522BC0 | 100.0% | Reimplemented @@ -46815,9 +46815,9 @@ SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522CA0 | 14.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522CA0 | 47.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522DD0 | 32.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522DD0 | 42.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522EF0 | 51.8% | Reimplemented, asm differs @@ -46829,7 +46829,7 @@ SHC_3BB0A8C1_0x00523190 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523240 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005232E0 | 40.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005232E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005232F0 | 0.0% | Pending @@ -46837,7 +46837,7 @@ SHC_3BB0A8C1_0x00523340 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005233A0 | 92.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005233A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005233E0 | 100.0% | Reimplemented @@ -46853,9 +46853,9 @@ SHC_3BB0A8C1_0x005236A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523730 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523750 | 72.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523750 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523790 | 25.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523790 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005237F0 | 32.1% | Reimplemented, asm differs @@ -46911,9 +46911,9 @@ SHC_3BB0A8C1_0x00524CA0 | 18.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending -SHC_3BB0A8C1_0x00524E20 | 27.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524E20 | 93.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524EF0 | 25.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524EF0 | 39.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525060 | 0.0% | Pending @@ -46939,11 +46939,11 @@ SHC_3BB0A8C1_0x00525470 | 58.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x005254C0 | 98.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525790 | 95.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525790 | 96.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525870 | 95.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525870 | 97.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525970 | 94.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525970 | 97.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525A70 | 67.1% | Reimplemented, asm differs @@ -46955,7 +46955,7 @@ SHC_3BB0A8C1_0x00525F20 | 68.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x005260B0 | 66.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005261B0 | 57.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005261B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005262A0 | 100.0% | Reimplemented @@ -46971,7 +46971,7 @@ SHC_3BB0A8C1_0x00526F00 | 39.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005270F0 | 42.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527330 | 60.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527330 | 58.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527440 | 58.5% | Reimplemented, asm differs @@ -48875,7 +48875,7 @@ SHC_3BB0A8C1_0x0052A637 | 0.0% | Pending SHC_3BB0A8C1_0x0052A638 | 0.0% | Pending -SHC_3BB0A8C1_0x0052A640 | 77.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A640 | 98.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052A700 | 42.0% | Reimplemented, asm differs @@ -49149,7 +49149,7 @@ SHC_3BB0A8C1_0x0052B390 | 32.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B630 | 50.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B860 | 80.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B9A0 | 0.0% | Pending From 212f556232ae902e4c6c63cc021dbab868e44e13 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 14:00:18 +0200 Subject: [PATCH 015/202] reimplement: getUnitIDForIndexInTribe 58.0%, and decode playUnitSelectionSound Namespace average 78.35% -> 78.53%, nothing worse. getUnitIDForIndexInTribe 36.7% -> 58.0%. `1 << ((byte)i & 0x1f)` is decompiler noise for `1 << i` - x86 masks the shift count itself, and spelling the mask out costs three instructions. Same fix as getSpecificUnitFromTribe. What is left is register allocation: the original shrink-wraps its pushes into the branches and we push up front, and neither declaration order nor declare-at-use moves it. aiAssignNewUnitToTribe: the nested if/else chain is flattened to else-if, which compiles byte-identically, and the monk arm's tribe ID is scoped to the arm. The percentage does not move - the original keeps the array index in a register where we spill it to the frame, and sharing one tribe-ID local across the arm and the tail (which is what the original does) is an exact tie. playUnitSelectionSound carries a fixme instead of a rewrite. Its decompilation is wrong in two ways - the UT_E_SWORD arm reaches `iVar5 = 0xad` unconditionally, so a two-unit selection plays three effects where the original plays one, and every `param_1 = ` in front of a speech call is MSVC writing a tail call's argument - but correcting the arms separately costs 32 points of raw match (46.5% -> 14.5%) while the normalized match rises (34.4% -> 40.8%), because the original shares those calls through its tail calls. The comment records what the branches say so the next pass can reproduce the sharing too. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/aiAssignNewUnitToTribe.cpp | 65 ++++++++----------- .../TribesState/getUnitIDForIndexInTribe.cpp | 11 ++-- .../TribesState/playUnitSelectionSound.cpp | 17 +++++ 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp index a7d9591379..692609a7ed 100644 --- a/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp @@ -11,55 +11,46 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005237F0 void TribesState::aiAssignNewUnitToTribe(int playerID, int unitType, int unitID) { - short* psVar1; - int _tribe; - int _index; - _index = 0; - /* - is engineer - */ + int _index = 0; if (unitType == 0x1e) { + /* + is engineer + */ _index = 1; - } else { + } else if (unitType == 5) { /* is tunneler */ - if (unitType == 5) { - _index = 2; - } else { - /* - is ladderman - */ - if (unitType == 0x1d) { - _index = 3; - } else { - /* - is monk - */ - if (unitType == 0x25) { - _tribe = DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk; - if (!_tribe || this->tribes[_tribe].tribeState == 0 - || this->tribes[_tribe].uid - != DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk) { - _tribe - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); - DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk = _tribe; - DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk - = this->tribes[_tribe].uid; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _tribe); - } - } + _index = 2; + } else if (unitType == 0x1d) { + /* + is ladderman + */ + _index = 3; + } else if (unitType == 0x25) { + /* + is monk + */ + int _monkTribe = DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk; + if (!_monkTribe || this->tribes[_monkTribe].tribeState == 0 + || this->tribes[_monkTribe].uid + != DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk) { + _monkTribe = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); + DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk = _monkTribe; + DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk + = this->tribes[_monkTribe].uid; } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _monkTribe); } - psVar1 = DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeIDs + _index; - _tribe = (int)*psVar1; + short* psVar1 = DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeIDs + _index; + int _tribe = (int)*psVar1; if (!_tribe || this->tribes[_tribe].tribeState == 0 || this->tribes[_tribe].uid != DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeUIDs[_index]) { _tribe = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); *psVar1 = (short)_tribe; - DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeUIDs[_index] = this->tribes[_tribe].uid; + DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeUIDs[_index] + = this->tribes[_tribe].uid; } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _tribe); } diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp index f8147cb5f1..ef007590f9 100644 --- a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -8,17 +8,14 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522390 int TribesState::getUnitIDForIndexInTribe(int tribeID, int unitSelectionIndex) { - int iVar2; - short* psVar3; - int _counter; - _counter = 0; + int _counter = 0; int iVar1 = 0; if ((this->tribes[tribeID].tribeState == 2) && (unitSelectionIndex < this->tribes[tribeID].size)) { - psVar3 = this->tribes[tribeID].unitSelectionBitMasked; + short* psVar3 = this->tribes[tribeID].unitSelectionBitMasked; do { if (*psVar3 != 0) { - for (iVar2 = 0; iVar2 < 16; iVar2++) { - if ((int)*psVar3 & 1 << ((byte)iVar2 & 0x1f)) { + for (int iVar2 = 0; iVar2 < 16; iVar2++) { + if ((int)*psVar3 & 1 << iVar2) { if (_counter == unitSelectionIndex) { return iVar1 * 0x10 + iVar2; } diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 082fba9537..fc68b1c006 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -14,6 +14,23 @@ namespace Map { using Map::Units::UnitType; + /* + fixme: this function is a chain of tail calls in the original and the + decompilation of it is wrong in two places. The UT_E_SWORD arm reaches + `iVar5 = 0xad` unconditionally, so for a two-unit selection it plays 0xae, then + 0xaf, then 0xad where the original plays 0xae alone and returns; the original has + three separate blocks, each with its own `ret`, and a selection below three units + that is neither one nor two plays nothing. `if (param_1 < 3) {}` is that case. + Every `param_1 = ;` in front of a playUnitSpeechEffect call is MSVC writing + the argument into the incoming parameter slot for a tail call, not a real + assignment, and the siege arm's five arms each return rather than falling into + the shared block at the end. + + Rebuilding it from those branches was tried and reverted: writing the arms out + separately costs 32 points of raw match (46.5% -> 14.5%) because the original + shares the play calls through its tail calls, while the normalized match rises + (34.4% -> 40.8%). It needs the sharing reproduced, not just the arms corrected. + */ // FUNCTION: STRONGHOLDCRUSADER 0x005217E0 void TribesState::playUnitSelectionSound(int param_1) { From 716d42ebdbf16ab4af6d3abf690c3554a4490a21 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 14:00:18 +0200 Subject: [PATCH 016/202] status: TribesState at 78.53% average, 49 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 0a1800fd0c..f813b92fa0 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46781,7 +46781,7 @@ SHC_3BB0A8C1_0x005222B0 | 0.0% | Pending SHC_3BB0A8C1_0x00522360 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522390 | 36.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522390 | 58.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522410 | 53.5% | Reimplemented, asm differs From af181e1d0930159a944e2a045785b0ab9e0851de Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 14:07:42 +0200 Subject: [PATCH 017/202] reimplement: updateTribeCombatStanceBehavior 57.6% Two more guards with empty bodies, both of which changed behaviour: - The aggressive-stance counter was incremented, tested against 0x3c with an empty body, and then reset to zero unconditionally, so the arm ran on every tick and the counter never reached its limit. The original's `cmp ax, 0x3c; jl` skips the whole arm. - Nothing gated the rally-point advance on allUnitsReachedTheirDestination, so the tribe advanced its rally point before arriving. 53.4% -> 57.6% (norm 44.5% -> 47.3%), namespace average 78.53% -> 78.57%. Still open on this function: the original places the rally branch first and the aggressive branch out of line (`test eax, eax; je` jumps 0x1fb away), so the two arms are the other way round from ours, and the rallyPointCount comparison re-reads the field where we keep a local. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../updateTribeCombatStanceBehavior.cpp | 159 ++++++++++-------- 1 file changed, 85 insertions(+), 74 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp index 1fb2311a9f..9257c0ac61 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp @@ -143,90 +143,101 @@ namespace Map { if (this->tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { psVar1 = &this->tribes[tribeID].aggressiveStanceCounter; *psVar1 = *psVar1 + 1; - if (this->tribes[tribeID].aggressiveStanceCounter < 0x3c) {} - _buildingIndex_2 = this->tribes[tribeID].size; - this->tribes[tribeID].aggressiveStanceCounter = 0; - if (0 < _buildingIndex_2) { - iVar5 = 0; - do { - iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar5); - iVar5 = iVar5 + 1; - if (DAT_UnitsState::instance.units[iVar4].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar4].dying == 0 - && DAT_UnitsState::instance.units[iVar4].state.generic - != Map::Units::States::US_MELEE_ATTACK) { - switch (DAT_UnitsState::instance.units[iVar4].unitType) { - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_KNIGHT: - case Map::Units::UT_E_MONK: - case Map::Units::UT_S_BATTERINGRAM: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - if (DAT_UnitsState::instance.units[iVar4].SA == 0) - goto LAB_0052ac8d; + /* + The import had this guard with an empty body, so the counter was + reset and the arm entered on every tick. The original's + `cmp ax, 0x3c; jl` skips the whole arm. + */ + if (0x3c <= this->tribes[tribeID].aggressiveStanceCounter) { + _buildingIndex_2 = this->tribes[tribeID].size; + this->tribes[tribeID].aggressiveStanceCounter = 0; + if (0 < _buildingIndex_2) { + iVar5 = 0; + do { + iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar5); + iVar5 = iVar5 + 1; + if (DAT_UnitsState::instance.units[iVar4].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar4].dying == 0 + && DAT_UnitsState::instance.units[iVar4].state.generic + != Map::Units::States::US_MELEE_ATTACK) { + switch (DAT_UnitsState::instance.units[iVar4].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_S_BATTERINGRAM: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + if (DAT_UnitsState::instance.units[iVar4].SA == 0) + goto LAB_0052ac8d; + } } + } while (iVar5 < this->tribes[tribeID].size); + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::aggressiveStanceTargetBuildingAtRange, + DAT_PathFindingState::ptr)(_targetUnitID, (int)(15)); + if (DAT_PathFindingState::instance.ALG_ResultTile) { + _buildingIndex_2 = DAT_TileMapState::instance + .BuildingLayer[DAT_PathFindingState::instance.ALG_ResultTile]; + if (!_buildingIndex_2) { + _buildingID = 0; + _unitInstructionType = ((UnitInstructionType)0x25); + _buildingIndex = DAT_PathFindingState::instance.ALG_ResultTile; + } else { + _buildingID = DAT_BuildingsState::instance.buildings[_buildingIndex_2].uid; + _unitInstructionType = ((UnitInstructionType)0x26); + _buildingIndex = (int)_buildingIndex_2; } - } while (iVar5 < this->tribes[tribeID].size); - } - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::aggressiveStanceTargetBuildingAtRange, - DAT_PathFindingState::ptr)(_targetUnitID, (int)(15)); - if (DAT_PathFindingState::instance.ALG_ResultTile) { - _buildingIndex_2 = DAT_TileMapState::instance - .BuildingLayer[DAT_PathFindingState::instance.ALG_ResultTile]; - if (!_buildingIndex_2) { - _buildingID = 0; - _unitInstructionType = ((UnitInstructionType)0x25); - _buildingIndex = DAT_PathFindingState::instance.ALG_ResultTile; - } else { - _buildingID = DAT_BuildingsState::instance.buildings[_buildingIndex_2].uid; - _unitInstructionType = ((UnitInstructionType)0x26); - _buildingIndex = (int)_buildingIndex_2; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + tribeID, _unitInstructionType, _buildingIndex, _buildingID, 0); } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - tribeID, _unitInstructionType, _buildingIndex, _buildingID, 0); } } } else { BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::allUnitsReachedTheirDestination, this)( tribeID); - if (!BVar3) {} - _buildingIndex_2 = this->tribes[tribeID].rallyPointCount; - iVar5 = this->tribes[tribeID].currentRallyPointIndex + 1; - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - if (_buildingIndex_2 <= iVar5) { - if (this->tribes[tribeID].isRallyingUnk == -1) { - this->tribes[tribeID].currentRallyPointIndex = 0; - this->tribes[tribeID].isRallyingUnk = 0; + /* + The import had this guard with an empty body; nothing advances the + rally point until the tribe has arrived. + */ + if (BVar3) { + _buildingIndex_2 = this->tribes[tribeID].rallyPointCount; + iVar5 = this->tribes[tribeID].currentRallyPointIndex + 1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + if (_buildingIndex_2 <= iVar5) { + if (this->tribes[tribeID].isRallyingUnk == -1) { + this->tribes[tribeID].currentRallyPointIndex = 0; + this->tribes[tribeID].isRallyingUnk = 0; + } + iVar5 = 0; } - iVar5 = 0; - } - if (this->tribes[tribeID].isRallyingUnk != 0) { - x1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][0]; - y1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][1]; - this->tribes[tribeID].currentRallyPointIndex = (short)iVar5; - UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType; - if (UVar2 == Map::Units::UT_E_ARCHER || UVar2 == Map::Units::UT_E_XBOW - || UVar2 == Map::Units::UT_A_ARCHER || UVar2 == Map::Units::UT_A_SLINGER - || UVar2 == Map::Units::UT_A_FIRETHROWER) { - if (0x1b0 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - tribeID, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + if (this->tribes[tribeID].isRallyingUnk != 0) { + x1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][0]; + y1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][1]; + this->tribes[tribeID].currentRallyPointIndex = (short)iVar5; + UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType; + if (UVar2 == Map::Units::UT_E_ARCHER || UVar2 == Map::Units::UT_E_XBOW + || UVar2 == Map::Units::UT_A_ARCHER || UVar2 == Map::Units::UT_A_SLINGER + || UVar2 == Map::Units::UT_A_FIRETHROWER) { + if (0x1b0 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + } + } else if (UVar2 == Map::Units::UT_A_HARCHER) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, x1, y1, 0, 0); + } else if ((0x60 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) + || (this->tribes[tribeID].unitStance != Map::Units::Behavior::USE_AGGRESSIVE)) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, x1, y1, 0, 0); } - } else if (UVar2 == Map::Units::UT_A_HARCHER) { - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, x1, y1, 0, 0); - } else if ((0x60 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) - || (this->tribes[tribeID].unitStance != Map::Units::Behavior::USE_AGGRESSIVE)) { - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, x1, y1, 0, 0); } } } From 36228ae6789f2b31af62f6cb8174c21c9320f199 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 14:07:42 +0200 Subject: [PATCH 018/202] status: TribesState at 78.57% average, 49 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index f813b92fa0..dbada2456a 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -48879,7 +48879,7 @@ SHC_3BB0A8C1_0x0052A640 | 98.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052A700 | 42.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052A7D0 | 53.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A7D0 | 57.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052ACB4 | 0.0% | Pending From 56c1e205e1b0babb3a66cc1b965cd7a4a9d8c9a5 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:08:50 +0200 Subject: [PATCH 019/202] reimplement: three animal and behaviour-target functions Namespace average 78.57% -> 78.58%. moveUnitToBehaviorTarget 27.0% -> 27.1%: the tribe-ID local is `int`, which is the movsx the original has. Its gotos stay, against the style list, because the original really does duplicate these tails and three goto-free shapes all measure worse - merging the two `local_c = y` tails into one block gives quick_diff 0.2730 at 137 instructions against the original's 156, duplicating the success tail into all eight arms gives 0.2951 at 149, and the form that is there gives 0.4092 at 147. The measurement is recorded above the function. updateRabbitTribeBehavior 32.8% -> 33.3% and updateLionWolfTribeBehavior 60.1% -> 60.7%: diff_types.py's EXTEND at `[esi + 0x28a]` is Tribe::rallyPointCount read through the tribe base, and the original zero-extends it. A `(ushort)` at the read site rather than retyping the field, which is a signed loop bound elsewhere. Measured and reverted: reading WildlifeGridElement::rockyTiles at its use site instead of hoisting it with the other grid fields. The original does load it there, and it removes the extra stack slot diff_slots.py reports, but reccmp disagrees with quick_diff on both functions that have it - updateRabbitTribeBehavior 32.8% -> 32.5% where quick_diff read 0.4634 -> 0.4914, and processDeerMoving 23.9% -> 23.5%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/moveUnitToBehaviorTarget.cpp | 9 ++++++++- .../Units/TribesState/updateLionWolfTribeBehavior.cpp | 2 +- .../Map/Units/TribesState/updateRabbitTribeBehavior.cpp | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index cdfebc6e4d..6cedfa801f 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -21,11 +21,18 @@ namespace Map { using Map::Units::UnitLogicState; using Map::Units::States::UnitState; + /* + The gotos stay here, against the style list, because the original really does + duplicate these tails and three goto-free shapes all measure worse: merging the + two `local_c = y` tails into one `if (iVar4)` block gives quick_diff 0.2730 at 137 + instructions against the original's 156, duplicating the success tail into all + eight arms gives 0.2951 at 149, and this form gives 0.4092 at 147. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00521280 undefined4 TribesState::moveUnitToBehaviorTarget(int unitID, int param_2) { int iVar4; - short sVar1 = DAT_UnitsState::instance.units[unitID].tribeID; + int sVar1 = DAT_UnitsState::instance.units[unitID].tribeID; int bVar3 = 0; uint x = 0; uint uVar6 = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index 577cad117d..caf6e01601 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -94,7 +94,7 @@ namespace Map { } } else if (sVar2 == 0x96) { if ((this->tribes[param_1].unknownBool02 == 0) - && (sVar2 = this->tribes[param_1].rallyPointCount, 0 < sVar2)) { + && (sVar2 = (ushort)this->tribes[param_1].rallyPointCount, 0 < sVar2)) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( param_1, (uint)((int)this->tribes[param_1].rallyPointArray[sVar2 + -1][0]), diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 429830d4d9..ed9a0d97bb 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -83,7 +83,7 @@ namespace Map { tribeID); } } else if (sVar2 == 0x96) { - sVar2 = this->tribes[tribeID].rallyPointCount; + sVar2 = (ushort)this->tribes[tribeID].rallyPointCount; if (0 < sVar2) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( From 8cdb11be49cf132af125f37ed734e32aedb0b267 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:19:04 +0200 Subject: [PATCH 020/202] note: giveTribeMoveInstruction's four measured declaration orders The original declares the path search budget at function scope - its prologue stores 0x1f4 into that slot alongside the zeroed locals - and `_tribeUnitIndex` is then one slot too many, so the frame only matches with both moves. All four variants were built and measured and all of them are reverted, because raw and normalized disagree and raw is what the function is scored on: as written 30.0%/22.5%, budget hoisted 28.0%/26.1%, both moves 27.1%/27.0%, counter scoped alone 21.7%/21.9%. Recorded above the function so the next pass starts from this rather than repeating the four builds. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeMoveInstruction.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 856823a268..1cf94fbea1 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -30,6 +30,22 @@ namespace Map { using Map::Units::States::UnitState; using WindowsHelper::Enums::BOOLEnum; + /* + Two declaration moves were measured here and all of them reverted, because raw + and normalized disagree and raw is what this function is scored on: + + as written raw 30.0% norm 22.5% + `_searchBudget` declared at this level raw 28.0% norm 26.1% + plus `_tribeUnitIndex` scoped to its loop raw 27.1% norm 27.0% + only `_tribeUnitIndex` scoped raw 21.7% norm 21.9% + + The original does declare the search budget at function scope - its prologue + stores 0x1f4 into the slot alongside the zeroed locals, which is what the middle + two variants reproduce - and `_tribeUnitIndex` is a slot too many when it does, + so the frame only matches with both moves. Whatever is costing the raw match is + downstream of the stack displacements those moves shift, so this needs the + allocation understood rather than another declaration permutation. + */ // FUNCTION: STRONGHOLDCRUSADER 0x005263A0 undefined4 TribesState::giveTribeMoveInstruction( int tribeID, uint x1, uint y1, int rallyBool, int storeAsRallyPoint, UnitMatchSpeedEnum speedMatching) From d5f44904e77fd8dba462489664c77f47d248fa50 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:28:23 +0200 Subject: [PATCH 021/202] reimplement: applyUnitTopSpeedDelayBasedOnTribeSize 45.1% 39.8% -> 45.1%, namespace average 78.58% -> 78.63%. diff_anchor.py: both of its cursors anchor four bytes below the original's, and every displacement in the loop differs as a result. The original walks the *movementDistance* member in the fill loop (`lea ebx, [esi + 0xfa412]`, with the unit ID stored at [ebx - 2]) and the *second* element of each pair in the sort loop (`lea eax, [esi + 0xfa414]`, with the current entry at [eax - 4] and [eax - 2]). Writing the cursors that way costs the typed struct pointer the earlier pass introduced, so each carries a comment saying why. Also records the one hint that did not pay: diff_lea.py's RLEA on removeUnitFromThisTribeIfInTribe says the original keeps &tribes[tribeID] in a register, but a `Tribe&` for the repeated subscript is not how to get it - 79.5% -> 58.8% by reccmp, matching what quick_diff said earlier. processDeerMoving loses a pointless local: its rallyPointCount test compiles against the memory operand either way, which is what the original does. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...applyUnitTopSpeedDelayBasedOnTribeSize.cpp | 25 +++++++++++++------ .../Units/TribesState/processDeerMoving.cpp | 7 ++++-- .../removeUnitFromThisTribeIfInTribe.cpp | 6 +++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp index 2ff43fa40f..37749989f4 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp @@ -55,7 +55,12 @@ namespace Map { Of all units in the selection store unitID and optionally their movementDistance into an array */ - UnitIDMovementDistancePair* _entry = this->someUnitIDMovementDistancePair; + /* + The cursor walks the movementDistance member, not the pair: that is the + anchor the original picks (`lea ebx, [esi + 0xfa412]`, with the unit ID + stored at [ebx - 2]). + */ + short* _movementDistance = &this->someUnitIDMovementDistancePair[0].movementDistance; do { int const _selectionUnit = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -63,14 +68,13 @@ namespace Map { if ((DAT_UnitsState::instance.units[_selectionUnit].logicalState == Map::Units::ULS_NORMAL) && (DAT_UnitsState::instance.units[_selectionUnit].dying == 0)) { this->someUnitIDArrayCount = this->someUnitIDArrayCount + 1; - _entry->unitID = (short)_selectionUnit; + _movementDistance[-1] = (short)_selectionUnit; if (!param_2) { - _entry->movementDistance - = DAT_UnitsState::instance.units[_selectionUnit].movementDistance; + *_movementDistance = DAT_UnitsState::instance.units[_selectionUnit].movementDistance; } } _unitSelectionIndex = _unitSelectionIndex + 1; - _entry = _entry + 1; + _movementDistance = _movementDistance + 2; } while (_unitSelectionIndex < *_tribeSize); } int _foundUnsortedEntry; @@ -83,9 +87,14 @@ namespace Map { if (this->someUnitIDArrayCount == 1 || this->someUnitIDArrayCount + -1 < 0) { break; } - UnitIDMovementDistancePair* pUVar2 = this->someUnitIDMovementDistancePair; + /* + The cursor walks the *second* element of each pair, which is the anchor + the original picks (diff_anchor.py: `lea eax, [esi + 0xfa414]`, with the + current entry reached as [eax - 4] and [eax - 2]). + */ + UnitIDMovementDistancePair* _nextUnit = this->someUnitIDMovementDistancePair + 1; do { - UnitIDMovementDistancePair* _nextUnit = pUVar2 + 1; + UnitIDMovementDistancePair* pUVar2 = _nextUnit - 1; short const _unitID = pUVar2->unitID; short const _distance = pUVar2->movementDistance; short const _nextDistance = _nextUnit->movementDistance; @@ -97,7 +106,7 @@ namespace Map { _nextUnit->movementDistance = _distance; } _pairIndex = _pairIndex + 1; - pUVar2 = _nextUnit; + _nextUnit = _nextUnit + 1; } while (_pairIndex < this->someUnitIDArrayCount + -1); } while (_foundUnsortedEntry); int _delayIndex = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index 36a62d9353..e02cfdefb3 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -88,10 +88,13 @@ namespace Map { if (this->tribes[tribeID].wanderIntervalTicks <= this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - sVar2 = this->tribes[tribeID].rallyPointCount; this->tribes[tribeID].wanderIntervalTicks = (4 - ((byte)SEC_RNG::instance.currentNumber2 & 3)) * 200; - if (sVar2 < 1) { + /* + Compared against the memory operand, not through a local: the + original has `cmp word ptr [..], 0; jg`. + */ + if (this->tribes[tribeID].rallyPointCount <= 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 5, 0); } diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index 74b64fefd7..697b976243 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -8,6 +8,12 @@ namespace OpenSHC { namespace Map { namespace Units { + /* + diff_lea.py reports RLEA here - the original keeps &tribes[tribeID] in a register + where we recompute it - but a `Tribe&` for the repeated subscript is not the way + to get it: 79.5% -> 58.8% (norm 73.2% -> 58.8%). Measured twice, by quick_diff + and by reccmp. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00522630 void TribesState::removeUnitFromThisTribeIfInTribe(uint unitID, int tribeID) { From b40aae22000ae39b3b626001d187dfd8ccbb3f47 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:33:42 +0200 Subject: [PATCH 022/202] reimplement: commandUnitsToLocation 42.3%, moveTribeToIndexedNearbyTile 43.3% Namespace average 78.63% -> 78.66%. commandUnitsToLocation 39.8% -> 42.3%. diff_triage.py's BYTE column: the original makes two byte-sized stack accesses and we made five. The two are its `(char)matchUnitSpeeds` tests; the other three were a `bool` flag, which is a dword in the original (`mov ebx, 0x1`, stored as a dword). BOOLEnum per the style guide, since every store is 0 or 1. moveTribeToIndexedNearbyTile 42.0% -> 43.3%. The bounds are `<= 399`, the 40-modulus is written in decimal, the unit-ID local is `int` (the original sign-extends), and `((XYPair*)(piVar6 + -1))->x` is `piVar6[-1]` - the array's members are ints, so the cast was reaching the same place the long way round. The index loop that took moveTribeToNearbyClearTile to 100% does not work here, because this one starts from a computed index rather than zero: 86 instructions against the original's 72 and quick_diff 0.5063, where the cursor form gives 84 at 0.5513. Declaring the candidate coordinates in the loop body is an exact tie, so they are declared at their use. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/commandUnitsToLocation.cpp | 11 +++++++++-- .../TribesState/moveTribeToIndexedNearbyTile.cpp | 14 ++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index 5b81fc17bd..ac80cfe713 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -6,6 +6,7 @@ #include "OpenSHC/Map/Units/States/UnitState.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" #include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" #include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" @@ -17,6 +18,7 @@ namespace Map { using Map::Units::UnitLogicState; using Map::Units::UnitType; + using WindowsHelper::Enums::BOOLEnum; using Map::Units::Instructions::UnitMatchSpeedEnum; using Map::Units::States::UnitState; @@ -27,7 +29,12 @@ namespace Map { int _unitID; UnitTypeShort _unitType; int _selectionUnitID = 0; - bool bVar1 = true; + /* + A dword flag, not a `bool`: diff_triage.py's BYTE column counts the original's + two byte-sized stack accesses, which are the two `(char)matchUnitSpeeds` + tests, and a byte-sized flag adds three more. + */ + BOOLEnum bVar1 = TRUE; if (0 < this->tribes[tribeID].size) { do { _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -69,7 +76,7 @@ namespace Map { || ((DAT_UnitsState::instance.units[_unitID].resourceToDeposit != 0 && (DAT_UnitsState::instance.units[_unitID].field252_0x3c4 == 0))))) { DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; - bVar1 = false; + bVar1 = FALSE; } else { DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 1; } diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index b7f5f2a9fd..f1534624a3 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -21,22 +21,20 @@ namespace Map { uint uVar3; int* piVar6; uint uVar7; - uint x1; - uint y1; - short sVar1 = this->tribes[param_1].selectionTargetUnitID; + int sVar1 = this->tribes[param_1].selectionTargetUnitID; uint uVar4 = (uint)DAT_UnitsState::instance.units[sVar1].y; uVar7 = (uint)DAT_UnitsState::instance.units[sVar1].x; int iVar2 = (int)this->tribes[param_1].someIndex; - int iVar5 = iVar2 % 0x28; + int iVar5 = iVar2 % 40; if (iVar5 <= 0) { iVar5 = 0; - } else if (0x27 < iVar5) { - return iVar2 / 0x28; + } else if (39 < iVar5) { + return iVar2 / 40; } piVar6 = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[iVar5].y; do { - x1 = ((XYPair*)(piVar6 + -1))->x + uVar7; - y1 = *piVar6 + uVar4; + uint const x1 = piVar6[-1] + uVar7; + uint const y1 = *piVar6 + uVar4; uVar3 = uVar4; if (x1 < 400 && y1 < 400 && *(char*)(y1 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x1) != '\0') { iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( From b3f2c0e0a083f8a15192077fc459e6eca995ddfc Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:41:04 +0200 Subject: [PATCH 023/202] reimplement: four more to 100% from the comparison forms Namespace average 78.66% -> 78.95%, 49 of 113 at 100% -> 53. All four were one or two comparisons written the way the decompiler printed them rather than the way the binary does them: - standUpAllTribeUnits 86.8% -> 100%. The cursor anchors on Tribe::size, not on the tribe: the original reads it as [eax] where a `Tribe*` reads [eax + 0x34], and every displacement in the loop differed as a result. - getNonDyingUnit 90.2% -> 100%. The size local is `int` (the original does one movsx where a `short` local needs movzx plus movsx), and the loop test is `index >= size`, which is the operand order of the original's `cmp`. - findRecentOrSignpostSpawnLocation 92.4% -> 100%. Bounds are `<= 399` (`cmp 0x18f; ja`) and the slot loop ends on `4 <= iVar4` (`cmp 4; jl`), not `3 < iVar4`. - addUnitToNewTribe 98.3% -> 100%. `_tribeID < 1` is `_tribeID <= 0`: `test eax, eax; jle` against our `cmp eax, 1; jl`. Tried and reverted: the same size-field cursor on five other functions that loop over the tribe size (applyLadderDestructionToTribeUnits, updateAnimalHerdBehaviorState, scatterTribeUnitsRandomly, updateTribeRallyFlags, stopTribeMovementAndCheckIdle) is an exact tie on all five - MSVC already picks that anchor when the two reads sit close together. And hoisting hasAvailableSpawnSlotForWildlifeOrMercs' counter out of its `for` header to match the original's `xor eax, eax` placement costs 10 points (96.0% -> 85.7%), which is now recorded in the file. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/addUnitToNewTribe.cpp | 2 +- .../findRecentOrSignpostSpawnLocation.cpp | 5 +++-- .../Map/Units/TribesState/getNonDyingUnit.cpp | 4 ++-- .../hasAvailableSpawnSlotForWildlifeOrMercs.cpp | 5 +++++ .../Map/Units/TribesState/standUpAllTribeUnits.cpp | 12 ++++++++---- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp index 8ffc824adf..77144d718f 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp @@ -34,7 +34,7 @@ namespace Map { int _playerID = (int)DAT_UnitsState::instance.units[unitID].owner; if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[_playerID] == -1 && ((_tribeID = (int)DAT_UnitsState::instance.units[unitID].tribeID, - _tribeID < 1 || (this->tribes[_tribeID].uid != DAT_UnitsState::instance.units[unitID].tribeUID)) + _tribeID <= 0 || (this->tribes[_tribeID].uid != DAT_UnitsState::instance.units[unitID].tribeUID)) || (this->tribes[_tribeID].tribeState == 0))) { int _newTribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(_playerID, 0); if (0 < _newTribeID) { diff --git a/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp b/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp index b5522b2b92..3cbf42c933 100644 --- a/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp @@ -22,7 +22,8 @@ namespace Map { uint uVar3 = (uint)DAT_GameState::instance.mapAndTime.lionLocationsXY[uVar2].y; *param_2 = uVar3; uVar2 = *param_1; - if (uVar2 < 400 && uVar3 < 400 && *(char*)(uVar2 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + uVar3 * 400) != '\0' + if (uVar2 <= 399 && uVar3 <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[uVar3 * 400 + uVar2] != 0 && !(DAT_TileMapState::instance .LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[uVar3].addXgetTile + uVar2] @@ -33,7 +34,7 @@ namespace Map { } } iVar4 = iVar4 + 1; - if (3 < iVar4) { + if (4 <= iVar4) { for (iVar4 = 0; iVar4 < 8; iVar4++) { if (DAT_GameState::instance.mapAndTime.signpostIDs[iVar4] != 0) { *param_1 = DAT_GameState::instance.mapAndTime.signpostsMapEdge[iVar4][0].x; diff --git a/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp b/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp index 6932203d1a..908cd31c8c 100644 --- a/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp @@ -19,9 +19,9 @@ namespace Map { return 0; } int unitSelectionIndex = 0; - short _size = this->tribes[tribeID].size; + int _size = this->tribes[tribeID].size; do { - if (_size <= unitSelectionIndex) { + if (unitSelectionIndex >= _size) { return 0; } iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( diff --git a/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp index b5b2909374..f1d9aa924f 100644 --- a/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp +++ b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp @@ -20,6 +20,11 @@ namespace Map { || 0xa0 <= DAT_GameState::instance.mapAndTime.wildlifeCount) { return (undefined4)(0); } + /* + The last instruction of difference is the original's `xor eax, eax` sitting + before the wildlifeCount guard rather than after it. Declaring the counter + there costs 10 points (96.0% -> 85.7%), so it stays in the `for` header. + */ for (int _slot = 0; _slot < 0xc; _slot = _slot + 1) { if (_slot < 4) { uint const _x = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0]; diff --git a/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp index 7a18ea0a4a..86ba276529 100644 --- a/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp +++ b/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp @@ -16,10 +16,14 @@ namespace Map { void TribesState::standUpAllTribeUnits(int param_1) { int iVar1; - Tribe* psVar1; - psVar1 = &this->tribes[param_1]; + /* + A cursor on the size field rather than on the tribe: diff_anchor's territory - + the original's `lea eax, [eax + ebp + 0x5c]` points at Tribe::size and reads + it as [eax], where a Tribe* reads it as [eax + 0x34]. + */ + short* _tribeSize = &this->tribes[param_1].size; int unitSelectionIndex = 0; - if (0 < psVar1->size) { + if (0 < *_tribeSize) { do { iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( param_1, unitSelectionIndex); @@ -30,7 +34,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar1].animationCycleNumber = 0; DAT_UnitsState::instance.units[iVar1].rabbitMovementSlowdown = '\0'; } - } while (unitSelectionIndex < psVar1->size); + } while (unitSelectionIndex < *_tribeSize); } } From 4e95697bb5e145320018cf8873714b924c716932 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:41:04 +0200 Subject: [PATCH 024/202] status: TribesState at 78.95% average, 53 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index dbada2456a..8cc0d0ff6f 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46841,13 +46841,13 @@ SHC_3BB0A8C1_0x005233A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005233E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523410 | 92.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523410 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523520 | 96.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523590 | 78.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523630 | 90.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523630 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005236A0 | 100.0% | Reimplemented @@ -46913,7 +46913,7 @@ SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending SHC_3BB0A8C1_0x00524E20 | 93.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524EF0 | 39.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524EF0 | 45.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525060 | 0.0% | Pending @@ -46931,13 +46931,13 @@ SHC_3BB0A8C1_0x00525280 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525300 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525370 | 86.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525370 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005253F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525470 | 58.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005254C0 | 98.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005254C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525790 | 96.8% | Reimplemented, asm differs @@ -46967,7 +46967,7 @@ SHC_3BB0A8C1_0x005266F0 | 0.0% | Pending SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending -SHC_3BB0A8C1_0x00526F00 | 39.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00526F00 | 42.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x005270F0 | 42.8% | Reimplemented, asm differs @@ -48877,7 +48877,7 @@ SHC_3BB0A8C1_0x0052A638 | 0.0% | Pending SHC_3BB0A8C1_0x0052A640 | 98.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052A700 | 42.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A700 | 43.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052A7D0 | 57.6% | Reimplemented, asm differs @@ -49143,9 +49143,9 @@ SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD50 | 23.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 60.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B110 | 60.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B390 | 32.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B390 | 33.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B630 | 50.9% | Reimplemented, asm differs From b378d3c2615a9de1824fdadfc1a8c865c6515498 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:47:29 +0200 Subject: [PATCH 025/202] reimplement: isTribeUnitBlockedByOtherUnit 80.7% 76.5% -> 80.7%, namespace average 78.95% -> 78.98%. The tribe-size local is `int` - a `short` one needs movzx plus movsx where the original does one movsx - and the loop test is `index >= size`, which is the operand order of the original's `cmp`. scatterTribeUnitsRandomly takes the same widening; it is code-generation neutral there but loses the same instruction pair. Widening every other `short local = ` in the namespace was trialled file by file and not kept: only predictUnitInterceptPosition and sortTribePathDestinationsByCost improved at all, by 0.001 of quick_diff ratio at ratios below 0.4 where the tool's own guidance says not to trust a rise, and addUnitsToTribeAndComputeMovementSpeed, createAnimal, drawFlagsAndUnitDestinations and playUnitCombatSpeechForTarget all dropped. This namespace does not behave like Map::Units::UnitsState did, where 17 of 18 such widenings gained. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp | 4 ++-- .../Map/Units/TribesState/scatterTribeUnitsRandomly.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp index 17dc494165..757a472ee2 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp @@ -19,10 +19,10 @@ namespace Map { { int iVar2; int unitSelectionIndex = 0; - short sVar1 = this->tribes[param_1].size; + int sVar1 = this->tribes[param_1].size; while (true) { do { - if (sVar1 <= unitSelectionIndex) { + if (unitSelectionIndex >= sVar1) { return (undefined4)(0); } iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( diff --git a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp index ed6e6b074a..c743d1e4dc 100644 --- a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp +++ b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp @@ -24,7 +24,7 @@ namespace Map { if (!param_1) { return 0; } - short sVar1 = this->tribes[param_1].selectionTargetUnitID; + int sVar1 = this->tribes[param_1].selectionTargetUnitID; int unitSelectionIndex = 0; int index = 100; MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSuitableSpawnLocationUnk, From 74f2e1254fd33a39cbbdf7b11d6a4e01517db97e Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 17:56:17 +0200 Subject: [PATCH 026/202] reimplement: moveTribeToIndexedNearbyTile 45.9%, and two more heuristics ruled out Namespace average 78.98% -> 79.00%. moveTribeToIndexedNearbyTile 43.3% -> 45.9%. Its bounds really are `<= 399`; the earlier pass meant to write them that way and the edit silently did not apply. The three remaining `*(char*)(y * 400 + (int)DAT_BinaryTileMap400x400 + x)` casts in the namespace are now array subscripts - code-generation neutral on createAnimal and giveTribeMoveInstruction, which is where the other two were. Two heuristics that pay elsewhere and cost here, both now recorded in the files so they are not retried: - sortTribePathDestinationsByCost: widening its `short` unit-ID local, which our movzx-plus-movsx pair against the original's single movsx argues for, costs 2.2 points (28.8% -> 26.6%). - spawnQueuedReinforcementWaves: making its two `bool` flags BOOLEnum, which the original's `xor ebx, ebx` and diff_triage.py's BYTE column argue for, costs 6.4 points (58.5% -> 52.1%, norm 52.9% -> 44.3%). Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/TribesState/createAnimal.cpp | 2 +- .../Map/Units/TribesState/giveTribeMoveInstruction.cpp | 2 +- .../Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp | 3 ++- .../Units/TribesState/sortTribePathDestinationsByCost.cpp | 5 +++++ .../Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp | 6 ++++++ 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp index 4452bcc59f..ab65b02a56 100644 --- a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -33,7 +33,7 @@ namespace Map { if ((int)tribeID <= 0) { return (dword)(0); } - if (399 < x || 399 < y || *(char*)(y * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x) == '\0') { + if (399 < x || 399 < y || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { return (dword)(0); } int _tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 1cf94fbea1..8196b81616 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -87,7 +87,7 @@ namespace Map { return (undefined4)(0); } } - if ((399 < y1) || (*(char*)(y1 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x1) == '\0')) { + if ((399 < y1) || (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0)) { return (undefined4)(0); } int _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index f1534624a3..a260a644c8 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -36,7 +36,8 @@ namespace Map { uint const x1 = piVar6[-1] + uVar7; uint const y1 = *piVar6 + uVar4; uVar3 = uVar4; - if (x1 < 400 && y1 < 400 && *(char*)(y1 * 400 + (int)DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400 + x1) != '\0') { + if (x1 <= 399 && y1 <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] != 0) { iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( param_1, uVar7, uVar4, x1, y1); uVar3 = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp index d40d205294..97d8c1de16 100644 --- a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -18,6 +18,11 @@ namespace Map { using Map::Navigation::PathHelper12; using WindowsHelper::Enums::BOOLEnum; + /* + The unit-ID local stays `short`, although our movzx-plus-movsx pair against the + original's single movsx says otherwise: widening it costs 2.2 points + (28.8% -> 26.6%, norm 30.3% -> 28.0%). + */ // FUNCTION: STRONGHOLDCRUSADER 0x00524930 void TribesState::sortTribePathDestinationsByCost(int tribeID, int horseAndRamCount) { diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp index 88d1c8b2bd..3a4edf7002 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -25,6 +25,12 @@ namespace Map { using Map::Units::UnitTypeInt; using WindowsHelper::Enums::BOOLEnum; + /* + The two flags stay `bool`, although the original zeroes its own with + `xor ebx, ebx` and diff_triage.py's BYTE column counts four byte-sized slots on + our side: making them BOOLEnum costs 6.4 points (58.5% -> 52.1%, norm 52.9% -> + 44.3%). + */ // FUNCTION: STRONGHOLDCRUSADER 0x00527440 void TribesState::spawnQueuedReinforcementWaves() { From 6b2a213b013a70848a25637e99e59a3579572ac8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 18:04:05 +0200 Subject: [PATCH 027/202] reimplement: trySpawnAdditionalWildlifeForTribe 87.2% 66.7% -> 87.2% (norm 66.7% -> 76.2%), namespace average 79.00% -> 79.19%. Four separate guards, not two `||` chains. The original emits a `return 0` for the first of each pair inline and jumps back to it from the second, and its three return blocks have different `pop` sequences because they sit at different points in its register saves - which is why it cannot merge them and we did. Splitting the chains recovered 16 of the 20 points. The rest: the RNG byte is read into an `int` and masked in the initialiser, which is the original's `movzx` plus one `and`; the spawn chance is `100 <= (rng & 0x7f)` (`cmp 0x64; jl`); and the wave counter is compared with the field as the first operand. Splitting guards the same way is not a general rule - on isTribePathToDestinationClear it measures worse (quick_diff 0.6635 -> 0.6296), because that one merges every failure into a single `return 0` instead of shrink-wrapping. Recorded in both files. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../isTribePathToDestinationClear.cpp | 6 +++++ .../trySpawnAdditionalWildlifeForTribe.cpp | 27 ++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index c4d8c443bf..ea16d9bcd7 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -17,6 +17,12 @@ namespace Map { using Map::Units::UnitLogicState; using WindowsHelper::Enums::BOOLEnum; + /* + The two guards stay as `||` chains. Splitting them into six separate `if`s, which + is what recovered trySpawnAdditionalWildlifeForTribe's inline return blocks, + measures worse here (quick_diff 0.6635 -> 0.6296): the original merges every + failure into one `return 0` at 0x524cd7 rather than shrink-wrapping them. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00524CA0 undefined4 TribesState::isTribePathToDestinationClear( int tribeID, uint fromX, uint fromY, uint toX, uint toY) diff --git a/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp index c5c9bb6c69..233066fb65 100644 --- a/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp @@ -19,12 +19,25 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005260B0 undefined4 TribesState::trySpawnAdditionalWildlifeForTribe(int param_1, int param_2, int param_3, int param_4) { - if ((DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) - || (param_1 <= 0)) { + /* + Four separate guards, not two `||` chains: the original emits a `return 0` for + the first of each pair inline and jumps back to it from the second, and the + three return blocks have different `pop` sequences because they sit at + different points in its register saves. + */ + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { return (undefined4)(0); } - if (this->tribes[param_1].predatorNearby == 0 - && (DAT_GameCore::instance.missionNumber1to20 != 0x26 || (param_4 != 0x2f))) { + if (param_1 <= 0) { + return (undefined4)(0); + } + if (this->tribes[param_1].predatorNearby != 0) { + return (undefined4)(0); + } + if (DAT_GameCore::instance.missionNumber1to20 == 0x26 && param_4 == 0x2f) { + return (undefined4)(0); + } + { int iVar7 = (int)this->tribes[param_1].selectionTargetUnitID; int iVar6 = iVar7; if (param_4 == 0x2c) { @@ -33,13 +46,13 @@ namespace Map { iVar6 = iVar7; } } - byte bVar5 = (byte)SEC_RNG::instance.currentNumber2; + int bVar5 = (byte)SEC_RNG::instance.currentNumber2 & 0x7f; int sVar2 = DAT_UnitsState::instance.units[iVar6].x; int sVar3 = DAT_UnitsState::instance.units[iVar6].y; int sVar4 = DAT_UnitsState::instance.units[iVar6].terrainOrClimbHeight; this->tribes[param_1].field_0x288 = this->tribes[param_1].field_0x288 + 1; - if (param_2 <= this->tribes[param_1].field_0x288 - && (this->tribes[param_1].field_0x288 = 0, 99 < (bVar5 & 0x7f))) { + if (this->tribes[param_1].field_0x288 >= param_2 + && (this->tribes[param_1].field_0x288 = 0, 100 <= bVar5)) { iVar6 = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::countLivingNondyingUnitsForPlayer, DAT_UnitsState::ptr)(param_4); if (iVar6 < param_3) { From 1c58149699048feeb52c12f0e131b431fa0beadb Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 18:08:42 +0200 Subject: [PATCH 028/202] style: createTribeForPlayer loses its hand-built tribeState pointer `(short*)((int)this + _selectionID * 0x334 + 0x40)` is `&this->tribes[_selectionID].tribeState`, and the 820 byte count is sizeof(Tribe). Both are code-generation neutral - quick_diff 0.7759 before and after - so this is readability only; the function stays at 75.2%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index 304f09c361..ada62fbbd8 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -25,10 +25,10 @@ namespace Map { /* find an empty tribe */ - psVar1 = (short*)((int)this + _selectionID * 0x334 + 0x40); + psVar1 = &this->tribes[_selectionID].tribeState; while (*psVar1 != 0) { _selectionID = _selectionID + -8; - psVar1 = psVar1 + -0xcd0; + psVar1 = psVar1 - 0xcd0; if (_selectionID <= 0) { return 0; } @@ -37,7 +37,7 @@ namespace Map { wipe the tribe */ MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - 820, '\0', (void*)((int)(this->tribes + _selectionID))); + sizeof(Tribe), '\0', &this->tribes[_selectionID]); if (0 < _selectionID) { this->tribes[_selectionID].owner = playerID; this->tribes[_selectionID].tribeState = 2; From 8193d39694862ff8bb1f7eb90c9bce5015f5757e Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 18:14:47 +0200 Subject: [PATCH 029/202] reimplement: the initial-move-order block returns, in three animal updaters Namespace average 79.19% -> 79.30%. All three of updateCamelTribeBehavior, updateLionWolfTribeBehavior and processDeerMoving have the same block - "if no move order has been given yet, give one" - and in all three the original ends it with `ret 4` after setting the behaviour type. The import let it fall through. That is not cosmetic: the block reassigns the variable that held initialMoveOrderGiven to a unit ID (the original is free to reuse the register precisely because it returns), so the `== 1` test that follows was reading a unit ID. The original's register reuse is what gave the return away. updateCamelTribeBehavior 50.9% -> 64.5%, processDeerMoving 23.9% -> 24.1%, and updateLionWolfTribeBehavior 60.7% -> 59.9%. The last one is a regression and is kept anyway: the `ret 4` at 0x52b207 is not ambiguous, and 0.8 points of block layout is not worth leaving a wrong control flow in place. updateRabbitTribeBehavior has the same block shape but an extra standUpAllTribeUnits call in it, and its asm does not show the return inside the 73 bytes the test jumps over, so it is left alone rather than guessed at. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp | 6 ++++++ .../Map/Units/TribesState/updateCamelTribeBehavior.cpp | 6 ++++++ .../Map/Units/TribesState/updateLionWolfTribeBehavior.cpp | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index e02cfdefb3..e510f34b02 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -63,12 +63,18 @@ namespace Map { tribeID, 100, Map::Units::UT_ANTELOPESHDEER); if (!BVar7) { if (this->tribes[tribeID].initialMoveOrderGiven == 0) { + /* + The original returns here - its block ends in `ret 4` after setting + the behaviour type - which is also why it can reuse the register that + held initialMoveOrderGiven for the unit ID. + */ sVar2 = this->tribes[tribeID].selectionTargetUnitID; this->tribes[tribeID].initialMoveOrderGiven = 1; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, (uint)(DAT_UnitsState::instance.units[sVar2].x), (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + return; } if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { this->tribes[tribeID].unknownCounter01 = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index 9b92244542..855b682ace 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -45,6 +45,11 @@ namespace Map { } sVar5 = this->tribes[param_1].initialMoveOrderGiven; if (!sVar5) { + /* + The original returns here - its block ends in `ret 4` - which is also why + it can reuse the register that held initialMoveOrderGiven for the unit ID. + Without the return, the `sVar5 == 1` test below reads a unit ID. + */ sVar5 = this->tribes[param_1].selectionTargetUnitID; this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; this->tribes[param_1].initialMoveOrderGiven = 2; @@ -52,6 +57,7 @@ namespace Map { (uint)(DAT_UnitsState::instance.units[sVar5].x), (uint)(DAT_UnitsState::instance.units[sVar5].y), 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + return; } if (DAT_GameCore::instance.currentMenuViewType != UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { if (sVar5 == 1) { diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index caf6e01601..ccaebc1dcd 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -50,6 +50,12 @@ namespace Map { } sVar2 = this->tribes[param_1].initialMoveOrderGiven; if (!sVar2) { + /* + The original returns here - its block ends in `ret 4` after setting + the behaviour type - which is also why it can reuse the register that + held initialMoveOrderGiven for the unit ID. Without the return, the + `sVar2 == 1` test below reads a unit ID. + */ sVar2 = this->tribes[param_1].selectionTargetUnitID; this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; this->tribes[param_1].initialMoveOrderGiven = 2; @@ -57,6 +63,7 @@ namespace Map { (uint)(DAT_UnitsState::instance.units[sVar2].x), (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + return; } if (sVar2 == 1) { this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; From 4754dfc4549b94bf6cd4b31dd55ff0ea6489737d Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 18:14:47 +0200 Subject: [PATCH 030/202] status: TribesState at 79.30% average, 53 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 8cc0d0ff6f..82aaa6cbc0 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46903,7 +46903,7 @@ SHC_3BB0A8C1_0x00524A10 | 0.0% | Pending SHC_3BB0A8C1_0x00524A30 | 0.0% | Pending -SHC_3BB0A8C1_0x00524B30 | 76.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524B30 | 80.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524BF0 | 67.2% | Reimplemented, asm differs @@ -46953,7 +46953,7 @@ SHC_3BB0A8C1_0x00525BA0 | 44.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525F20 | 68.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005260B0 | 66.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005260B0 | 87.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x005261B0 | 100.0% | Reimplemented @@ -48877,7 +48877,7 @@ SHC_3BB0A8C1_0x0052A638 | 0.0% | Pending SHC_3BB0A8C1_0x0052A640 | 98.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052A700 | 43.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A700 | 45.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052A7D0 | 57.6% | Reimplemented, asm differs @@ -49141,13 +49141,13 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 23.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052AD50 | 24.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 60.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B110 | 60.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B390 | 33.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B630 | 50.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B630 | 64.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs From 78742bac28ca167c7d0297db019f7733edca2416 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 18:19:34 +0200 Subject: [PATCH 031/202] reimplement: createAnimal 46.8%, createTribe's zero guard as an early return Namespace average 79.30% -> 79.32%. createAnimal 44.4% -> 46.8% (norm 45.1% -> 47.6%). Its three entry bounds are three separate guards, each with its own forward jump in the original rather than one folded chain. quick_diff calls this an exact tie; reccmp does not. createTribe: the `_tribeID <= 0` test is an early return, which is where the original puts its own - `cmp edi, ebx; jg` falls straight into `xor eax, eax` with the claim following, rather than jumping over the claim to a shared epilogue. Neutral on the percentage (still 30.3%) and better style. What keeps this one low is that the original carries two full `return 0` epilogues, one for the exhausted loop and one for the guard, where MSVC merges ours into one - we are 56 instructions against its 64 and the body's ten stores already match one for one. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/TribesState/createAnimal.cpp | 12 +++++++++++- src/OpenSHC/Map/Units/TribesState/createTribe.cpp | 11 +++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp index ab65b02a56..b6ec4fa08c 100644 --- a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -33,7 +33,17 @@ namespace Map { if ((int)tribeID <= 0) { return (dword)(0); } - if (399 < x || 399 < y || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + /* + Three separate guards: the original gives each of them its own forward jump + rather than folding them into one chain. + */ + if (399 < x) { + return (dword)(0); + } + if (399 < y) { + return (dword)(0); + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { return (dword)(0); } int _tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index c6f5377b73..cb6f65c3c7 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -27,10 +27,14 @@ namespace Map { MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), '\0', &this->tribes[DAT_CurrentTribeID::instance]); /* - The claim is the cold-last arm, which is what puts it out of line in - the original, behind the shared `return 0` epilogue. + An early return, which is where the original puts its `return 0`: + `cmp edi, ebx; jg` falls straight into `xor eax, eax` with the claim + following, rather than jumping over the claim to a shared epilogue. */ - if (0 < _tribeID) { + if (_tribeID <= 0) { + return 0; + } + { Tribe& _tribe = this->tribes[_tribeID]; if ((playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) && (setAsCurrentTribeID)) { @@ -50,7 +54,6 @@ namespace Map { _tribe.size = 0; return _tribeID; } - return 0; } DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; } while ((int)DAT_CurrentTribeID::instance < 1250); From e492947767eab976e98a6b2fb8515ff0be207650 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 18:23:33 +0200 Subject: [PATCH 032/202] style: giveTribeMoveInstruction's x bound is `400 <=`, and its y guard splits The original's upper bound on x is `cmp 0x190; jge`, not `cmp 0x18f; jg`, and the y bound and the tile test are two guards rather than an `||` chain. Both are code-generation neutral by reccmp (still 30.0%); quick_diff moves 0.4694 -> 0.4718. Written the way the binary does them so the next pass is not chasing a difference that is not there. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/giveTribeMoveInstruction.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 8196b81616..b45f8d8c3d 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -83,11 +83,17 @@ namespace Map { if ((int)x1 < 0) { return (undefined4)(0); } - if (399 < (int)x1) { + /* + `cmp 0x190; jge`, not `cmp 0x18f; jg`. + */ + if (400 <= (int)x1) { return (undefined4)(0); } } - if ((399 < y1) || (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0)) { + if (399 < y1) { + return (undefined4)(0); + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0) { return (undefined4)(0); } int _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; From ec9277d08d0de0f7946dc99e9e345328fcc950fa Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 18:23:45 +0200 Subject: [PATCH 033/202] status: TribesState at 79.32% average, 53 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 82aaa6cbc0..99a968324a 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46949,7 +46949,7 @@ SHC_3BB0A8C1_0x00525A70 | 67.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525BA0 | 44.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525F20 | 68.0% | Reimplemented, asm differs From e321adff2efc9036ec313f720a5c34d3a3a73318 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 19:00:53 +0200 Subject: [PATCH 034/202] reimplement: giveTribeAnInstruction's three `id | x | id != 0` tests The namespace now builds without warnings. `id` is the instruction's target parameter, but each of the three attack cases zeroes it at the top and counts melee units up in its own arm, so the test after the unit loop asks whether the selection held any. The original is one `cmp dword ptr [esp + 0x38], 0` against that counter's slot. The decompiler rendered it as `id | x | id != 0` in all three places, which read `x` before its only assignment 600 lines later - undefined behaviour, and MSVC's C4700 reported only the first of the three. Also here: the two `bool` flags are BOOLEnum, which clears diff_triage.py's BYTE column (5 byte-sized stack slots against the original's none, at the byte-aligned offsets 0x13 and 0x1b diff_slots.py reported); three declaration-only locals are gone; and `-(uint)(param_5 != 0) & 0x10` is `param_5 != 0 ? 0x10 : 0`. 27.8% -> 27.9% (norm 36.2% -> 36.3%). The percentage is not the point of this one. What still separates it is three stack slots we have and the original does not, and thirteen jump tables whose arms the decompiler flattened. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index fbc581466a..e7ad9aff2d 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -54,7 +54,11 @@ namespace Map { byte bVar2; UnitStateShort UVar3; UnitTypeShort UVar4; - bool bVar5; + /* + Dword flags, not `bool`s: diff_slots.py shows byte-aligned stack slots at + 0x13 and 0x1b on our side and none on the original's. + */ + BOOLEnum bVar5; Unit* pUVar6; char cVar7; short sVar8; @@ -85,7 +89,7 @@ namespace Map { int* piVar18; uint y; uint uVar19; - bool _gamemodeSiegeThat; + BOOLEnum _gamemodeSiegeThat; int _unitTribeIndex; int _ramOrHorse; uint local_8; @@ -93,8 +97,6 @@ namespace Map { int _tile1_528565; short _y_528565; int _param_1_tribeID; - int _paramUnitID; - int _paramUnitUID; short _tribeSize; int _freeTile; uint _defensiveTile1; @@ -103,7 +105,6 @@ namespace Map { short _y; short _x; uint _id; - int _param_3_id_x_tile_buildingID; int _param_4_unitUID_Y; ushort _targetArea; _param_4_unitUID_Y = unitUID; @@ -113,7 +114,7 @@ namespace Map { _param_4_unitUID_Y_copy = unitUID; _ramOrHorse = 0; local_8 = 0; - bVar5 = false; + bVar5 = FALSE; this->tribes[tribeID].someUnitID = 0; this->tribes[tribeID].containsRabbit = 0; _param_3_id_x_tile_buildingID_copy = (short)id; @@ -245,12 +246,13 @@ namespace Map { } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); /* - fixme: `id | x | id != 0` reads `x`, which nothing in this function - ever assigns - MSVC reports C4700 here. The decompiler mangled this - condition and it has to be rebuilt from the original's own branches - along with the rest of the function's thirteen jump tables. + `id` is zeroed before the loop and counted up in the melee arm, so + this asks whether the selection held any melee units. The original is + one `cmp dword ptr [esp + 0x38], 0` against that counter's slot; the + decompiler rendered it as `id | x | id != 0`, which read an + uninitialised local and raised C4700. */ - if (id | x | id != 0) { + if (id != 0) { /* if there are melee units in the selection */ @@ -625,7 +627,11 @@ namespace Map { if ((((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) || (4 < (int)(short)DAT_BuildingsState::instance.buildings[iVar11].buildingType - 0x4aU)) - && (id | x | id != 0)) { + /* + Again the melee counter against zero, which the decompiler rendered + as `id | x | id != 0`. + */ + && (id != 0)) { sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; if (unitInstructionType == Map::Units::UIT_ATTACK_BUILDING || unitInstructionType == (UnitInstructionType)0x26) { @@ -715,7 +721,7 @@ namespace Map { iVar12 = MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::findBuildingAccessPoint, DAT_PathFindingState::ptr)(_unitID, iVar11, ¶m_5); - bVar5 = true; + bVar5 = TRUE; if (iVar12 == 0) goto LAB_00528f32; sVar9 = DAT_ViewportRenderState::instance @@ -934,7 +940,7 @@ namespace Map { /* disband siege engines? */ - _param_4_unitUID_Y_copy = -(uint)(param_5 != 0) & 0x10; + _param_4_unitUID_Y_copy = param_5 != 0 ? 0x10 : 0; param_5 = (int)SEC_RNG::instance.currentNumber2 & 0x8000000f; if (param_5 < 0) { param_5 = (param_5 - 1U | 0xfffffff0) + 1; @@ -1227,7 +1233,10 @@ namespace Map { && (DAT_GameState::instance.mapAndTime.skirmishStrongWalls != 0)) { id = local_8; } - if (id | x | id != 0) { + /* + The melee counter against zero again, as at the two sites above. + */ + if (id != 0) { sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; if (unitInstructionType == Map::Units::UIT_ATTACK_WALL || unitInstructionType == (UnitInstructionType)0x25) { @@ -1290,7 +1299,7 @@ namespace Map { != 4) break; if (!bVar5) { - bVar5 = true; + bVar5 = TRUE; iVar15 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: findUnitPreferredOrientationBasedConnectedTile, @@ -1394,12 +1403,9 @@ namespace Map { } } while (iVar12 < this->tribes[_param_1_tribeID].size); /* - fixme: `id | x | id != 0` reads `x`, which nothing in this function - ever assigns - MSVC reports C4700 here. The decompiler mangled this - condition and it has to be rebuilt from the original's own branches - along with the rest of the function's thirteen jump tables. + The melee counter against zero, as at the first site. */ - if (id | x | id != 0) { + if (id != 0) { sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: populateDestinationsArrayWithWallTileAndFreeTilePairInSameArea, @@ -1673,12 +1679,9 @@ namespace Map { } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); /* - fixme: `id | x | id != 0` reads `x`, which nothing in this function - ever assigns - MSVC reports C4700 here. The decompiler mangled this - condition and it has to be rebuilt from the original's own branches - along with the rest of the function's thirteen jump tables. + The melee counter against zero, as at the first site. */ - if (id | x | id != 0) { + if (id != 0) { iVar12 = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; param_5 = (int)DAT_UnitsState::instance.units[iVar12].x; unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[iVar12].y; From 4fb78798e0fdfb72a597d09a7b2207fa3224f73f Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 19:05:46 +0200 Subject: [PATCH 035/202] style: three redundant locals out of giveTribeAnInstruction `_id` was declared and never used; `_defensiveTile1` and `_defensiveTile2` were two reads of the same `*piVar18`; and `_isAllAssassins_528565` held a call result for one test, which now reads inline. All three were already coalesced by MSVC, so the frame does not move - 27.9%, norm 0.512 -> 0.515. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/giveTribeAnInstruction.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index e7ad9aff2d..3b3e2db474 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -66,7 +66,6 @@ namespace Map { int _unitID_0x04; int _targetUnitID; uint _x_528565; - BOOL _isAllAssassins_528565; UnitInstructionType UVar10; int iVar11; int _unitID_0x05_0x16; @@ -100,11 +99,9 @@ namespace Map { short _tribeSize; int _freeTile; uint _defensiveTile1; - int _defensiveTile2; short _freeTileY; short _y; short _x; - uint _id; int _param_4_unitUID_Y; ushort _targetArea; _param_4_unitUID_Y = unitUID; @@ -1449,8 +1446,11 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(_laddermanID, _freeTileX, (uint)(_freeTileY), 0); DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + /* + One local: the decompiler read the same `*piVar18` + into two. + */ _defensiveTile1 = *piVar18; - _defensiveTile2 = *piVar18; /* set ladder man ladder target id */ @@ -1459,7 +1459,7 @@ namespace Map { .tileTranslationMatrix_YComponent[_defensiveTile1]; DAT_UnitsState::instance.units[_laddermanID].attackAtTileY = sVar9; piVar18 = piVar18 + 3; - DAT_UnitsState::instance.units[_laddermanID].attackAtTileX = (short)_defensiveTile2 + DAT_UnitsState::instance.units[_laddermanID].attackAtTileX = (short)_defensiveTile1 - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; } } while (iVar11 < this->tribes[_param_1_tribeID].size); @@ -1767,9 +1767,9 @@ namespace Map { } DAT_UnitsState::instance.units[iVar14].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; - _isAllAssassins_528565 = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); - if (_isAllAssassins_528565 == 0) { + if (MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID) + == 0) { DAT_PathFindingState::instance.notAllAssassinsUnk = 1; } else { DAT_PathFindingState::instance.allAssassinsUnk = 1; From 0a80e069710d62ec4bb23cb23ff3ad8e5df69a7e Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 19:11:06 +0200 Subject: [PATCH 036/202] style: two of giveTribeAnInstruction's cross-case gotos become fall-through In both attack-unit switches the ranged types share the horse archer's arm and only the horse archer counts towards _ramOrHorse, which is why the decompiler had the ranged cases jumping into a label inside it. Stacking them under that arm says the same thing as a switch, and compiles identically - 27.9% and norm 36.3% before and after. 17 gotos down to 15. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 3b3e2db474..0ef18dab3d 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -516,12 +516,6 @@ namespace Map { DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; switch (DAT_UnitsState::instance.units[iVar12].unitType) { - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_FIRETHROWER: - goto switchD_00528b77_caseD_16; case Map::Units::UT_E_KNIGHT: if (DAT_BuildingsState::instance.buildings[iVar11].buildingType != Map::Buildings::BT_PITCHDITCH) { @@ -570,7 +564,16 @@ namespace Map { break; case Map::Units::UT_A_HARCHER: _ramOrHorse = _ramOrHorse + 1; - switchD_00528b77_caseD_16: + /* + The ranged types share this arm; only the horse archer counts + towards _ramOrHorse, which is why the decompiler had them + jumping in from their own label. + */ + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_FIRETHROWER: if (unitInstructionType == (UnitInstructionType)0x24) { switch (DAT_BuildingsState::instance.buildings[iVar11].buildingType) { case Map::Buildings::BT_GATEHOUSELARGE: @@ -1137,13 +1140,6 @@ namespace Map { DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; switch (DAT_UnitsState::instance.units[uVar13].unitType) { - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_FIRETHROWER: - goto switchD_005290c7_caseD_16; case Map::Units::UT_E_KNIGHT: switchD_005290c7_caseD_1c: _ramOrHorse = _ramOrHorse + 1; @@ -1220,7 +1216,16 @@ namespace Map { break; case Map::Units::UT_A_HARCHER: _ramOrHorse = _ramOrHorse + 1; - switchD_005290c7_caseD_16: + /* + The ranged types share this arm; only the horse archer counts + towards _ramOrHorse. + */ + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_FIRETHROWER: if (unitInstructionType == (UnitInstructionType)0x23) goto switchD_005290c7_caseD_5; } From ec347479aa1ad0ee753eb8c68ad0be7bdf55536f Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 19:16:18 +0200 Subject: [PATCH 037/202] style: two more cross-case goto webs in giveTribeAnInstruction The switch at 0x52932a chains three groups: a ranged unit under instruction 0x23 falls into the melee test, and a melee unit that passes it falls into the battering-ram arm. The switch at 0x5282f6 has its siege group fall into the ranged arm after setting shootBeforeStop. Both are written as case fall-through with the groups in that order, which is what the gotos were saying. 17 gotos down to 12. 27.9% either way, norm 36.2% -> 36.1%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 0ef18dab3d..181a44d0bf 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -1269,6 +1269,16 @@ namespace Map { UVar4 = DAT_UnitsState::instance.units[iVar14].unitType; switch (UVar4) { case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + if (unitInstructionType != (UnitInstructionType)0x23) { + break; + } case Map::Units::UT_E_SPEAR: case Map::Units::UT_E_PIKE: case Map::Units::UT_E_MACE: @@ -1278,23 +1288,17 @@ namespace Map { case Map::Units::UT_LORD: case Map::Units::UT_A_SLAVE: case Map::Units::UT_A_SWORDSMAN: - switchD_0052932a_caseD_5: - if ((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) - || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) - goto switchD_0052932a_caseD_3b; - break; - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_HARCHER: - case Map::Units::UT_A_FIRETHROWER: - if (unitInstructionType == (UnitInstructionType)0x23) - goto switchD_0052932a_caseD_5; - break; + if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY + && DAT_GameState::instance.mapAndTime.skirmishStrongWalls != 0) { + break; + } + /* + The three groups chain: a ranged unit under + instruction 0x23 falls into the melee test, and a + melee unit that passes it falls into the ram arm. + That is the web of gotos the decompiler printed. + */ case Map::Units::UT_S_BATTERINGRAM: - switchD_0052932a_caseD_3b: if (UVar4 == Map::Units::UT_S_BATTERINGRAM) { if (DAT_UnitsState::instance.units[iVar14] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 @@ -1618,6 +1622,17 @@ namespace Map { id = id + 1; } break; + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + if (unitInstructionType == Map::Units::UIT_SHOOT_TARGETUnk) { + break; + } + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + /* + and on into the ranged arm, which is what the + decompiler's goto back into it said + */ case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_XBOW: case Map::Units::UT_E_ARCHER_DEBUG: @@ -1625,7 +1640,6 @@ namespace Map { case Map::Units::UT_A_SLINGER: case Map::Units::UT_A_HARCHER: case Map::Units::UT_A_FIRETHROWER: - switchD_005282f6_caseD_16: if (UVar3 == (UnitState)0x69) { DAT_UnitsState::instance.units[iVar12].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; @@ -1673,13 +1687,6 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } break; - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_BALLISTA: - case Map::Units::UT_S_FBALLISTA: - if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { - DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; - goto switchD_005282f6_caseD_16; - } } } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); From a7b49326454b282b34116b1ef1a3c014aaaea521 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 19:20:28 +0200 Subject: [PATCH 038/202] style: the last convertible goto web in giveTribeAnInstruction The switch at 0x5290c7 has a crewed battering ram count as a horseman - a goto back into the knight arm, now that arm's fall-through predecessor - and its ranged arm jumps back to the melee counter, which is the single `id = id + 1` it now does itself. Real goto statements: 17 at the start of this work, 10 now, and the percentage has not moved on any of the conversions (27.9%, norm 36.1%). The seven that remain are `LAB_*` shared tails inside loops with several predecessors each, which AGENTS.md says to leave, plus the pair at 0x528bae where the inner building-type switch jumps into another arm's large body - removing that one needs either a duplicated body or another local, and this function already carries three stack slots the original does not. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 181a44d0bf..6ac9c68e45 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -1140,8 +1140,18 @@ namespace Map { DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; switch (DAT_UnitsState::instance.units[uVar13].unitType) { + case Map::Units::UT_S_BATTERINGRAM: + if (DAT_UnitsState::instance.units[uVar13] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 4) { + break; + } + local_8 = local_8 + 1; + /* + a crewed ram counts as a horseman too, which is the goto the + decompiler printed back into this arm + */ case Map::Units::UT_E_KNIGHT: - switchD_005290c7_caseD_1c: _ramOrHorse = _ramOrHorse + 1; case Map::Units::UT_TUNNELER: case Map::Units::UT_E_SPEAR: @@ -1152,7 +1162,6 @@ namespace Map { case Map::Units::UT_LORD: case Map::Units::UT_A_SLAVE: case Map::Units::UT_A_SWORDSMAN: - switchD_005290c7_caseD_5: id = id + 1; break; case Map::Units::UT_S_CATAPULT: @@ -1206,14 +1215,6 @@ namespace Map { - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; } break; - case Map::Units::UT_S_BATTERINGRAM: - if (DAT_UnitsState::instance.units[uVar13] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4) { - local_8 = local_8 + 1; - goto switchD_005290c7_caseD_1c; - } - break; case Map::Units::UT_A_HARCHER: _ramOrHorse = _ramOrHorse + 1; /* @@ -1226,8 +1227,9 @@ namespace Map { case Map::Units::UT_A_SLINGER: case Map::Units::UT_A_ASSASSIN: case Map::Units::UT_A_FIRETHROWER: - if (unitInstructionType == (UnitInstructionType)0x23) - goto switchD_005290c7_caseD_5; + if (unitInstructionType == (UnitInstructionType)0x23) { + id = id + 1; + } } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); } From aa32e5aef5f5a8a4780306721b7ed4e3dfa7441a Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 19:31:14 +0200 Subject: [PATCH 039/202] reimplement: spawnQueuedReinforcementWaves 59.6% 58.5% -> 59.6%, namespace average 79.32% -> 79.33%. The two `bool` flags measure in opposite directions, which is why the earlier blanket conversion looked like the heuristic failing. Both together cost 6.4 points; `bVar2` alone gains 1.1. `bVar1` keeps its byte slot at 0x13 despite the original's `xor ebx, ebx`, and the split is recorded above the function. Also drops `_nextDestination`, a decompiler temp holding `_destinationIndexTracker + 1` across the spawn call for the sole purpose of assigning it back afterwards. Code-generation neutral. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../spawnQueuedReinforcementWaves.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp index 3a4edf7002..1d4dce457a 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -26,10 +26,10 @@ namespace Map { using WindowsHelper::Enums::BOOLEnum; /* - The two flags stay `bool`, although the original zeroes its own with - `xor ebx, ebx` and diff_triage.py's BYTE column counts four byte-sized slots on - our side: making them BOOLEnum costs 6.4 points (58.5% -> 52.1%, norm 52.9% -> - 44.3%). + The two flags measure in opposite directions, so only one of them is widened. + Both together cost 6.4 points (58.5% -> 52.1%); `bVar2` alone gains 1.1 + (58.5% -> 59.6%), so `bVar1` stays `bool` despite the original's `xor ebx, ebx` + and the byte-sized slot diff_slots.py reports for it at 0x13. */ // FUNCTION: STRONGHOLDCRUSADER 0x00527440 void TribesState::spawnQueuedReinforcementWaves() @@ -43,7 +43,6 @@ namespace Map { SiegeUnitCounts* pSVar8; int _siegeInfoSubIndex; int _playerID; - int _nextDestination; int _tribeType; UnitTypeInt _unitType; int _destinationIndexTracker; @@ -55,9 +54,10 @@ namespace Map { int _newCount; _tribeType = 0; bool bVar1 = false; + _unitType = ((UnitType)0); _destinationIndexTracker = 0; - bool bVar2 = false; + BOOLEnum bVar2 = FALSE; if (DAT_GameCore::instance.gameMode_2 != Game::GM_EDITOR && DAT_GameCore::instance.gameMode_2 != Game::GM_SIEGE_THAT && (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY @@ -185,7 +185,6 @@ namespace Map { if (39 < _destinationIndexTracker) { _destinationIndex = 0; } - _nextDestination = _destinationIndexTracker + 1; _tribeID1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeWithSpawnedUnit, this)((short)_destinationIndexTracker, (undefined4)((int)(_tribeType)), DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][_destinationIndex] @@ -195,7 +194,7 @@ namespace Map { 2, (UnitType)((int)(_unitType)), _nUnits); _newCount = DAT_GameState::instance.mapAndTime.startGoods[_siegeInfoSubIndex + 0x19]; this->tribes[_tribeID1].unknownAttackRelatedUpdateCounter = 0x32; - _destinationIndexTracker = _nextDestination; + _destinationIndexTracker = _destinationIndexTracker + 1; } while (_newCount); } _siegeInfoSubIndex = _siegeInfoSubIndex + 1; @@ -288,7 +287,7 @@ namespace Map { _destinationIndexTracker, _tribeType, (int)(*piVar7), (int)(piVar7[1]), _playerID, (UnitType)((int)(_unitType)), ((UnitType)0), _unitType1Count, 0); this->tribes[dVar4].unknownAttackRelatedUpdateCounter = 0x32; - bVar2 = true; + bVar2 = TRUE; _destinationIndexTracker = _destinationIndexTracker + 1; } _playerID = _playerID + 1; @@ -349,7 +348,7 @@ namespace Map { _destinationIndexTracker, _tribeType, (int)(*piVar7), (int)(piVar7[1]), iVar5, (UnitType)((int)(_unitType)), ((UnitType)0), _unitType1Count, 0); this->tribes[dVar4].unknownAttackRelatedUpdateCounter = 0x32; - bVar2 = true; + bVar2 = TRUE; _destinationIndexTracker = _destinationIndexTracker + 1; } iVar5 = iVar5 + 1; From 82db04ecef33e0239a6bdea3bfa865df8aa13a55 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 19:31:35 +0200 Subject: [PATCH 040/202] status: TribesState at 79.33% average, 53 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 99a968324a..30eea631f8 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46973,7 +46973,7 @@ SHC_3BB0A8C1_0x005270F0 | 42.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 58.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527440 | 58.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527440 | 59.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x005275A0 | 0.0% | Pending @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 27.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 27.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From fd431a05dbca13c068fddff088fa628f0228adf8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:12:25 +0200 Subject: [PATCH 041/202] reimplement: the local-widening heuristic, measured properly this time predictUnitInterceptPosition 30.9% -> 31.1%, namespace average 79.33% -> 79.34%, and seven measurements recorded in the files that carry them. The earlier pass judged these with quick_diff, which its own guidance says not to trust at the ratios involved, and reverted all seven together. Because reccmp attributes per function, one build measures changes in different files independently, so each is now a real number: predictUnitInterceptPosition 30.9% -> 31.1% kept assignAttackTargetsForTribe (bool) 44.6% -> 44.4% createAnimal 46.8% -> 46.4% computeTribePercentages (three) 42.8% -> 41.9% playUnitCombatSpeechForTarget 28.1% -> 27.6% drawFlagsAndUnitDestinations 41.8% -> 37.9% sortTribePathDestinationsByCost(bool) 28.8% -> 23.5% addUnitsToTribeAndComputeMovementSpeed 64.1% -> 46.5% So the bulk revert was right, but for the wrong reason, and the 17.6-point loss on addUnitsToTribeAndComputeMovementSpeed was worth knowing about. The guidance in AGENTS.md that widening a `short` local "cannot add a memory read, only change the extension width of one that already happens" does not hold in this namespace; whatever these locals feed, the narrow form is what the original compiled. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp | 4 ++++ .../Map/Units/TribesState/assignAttackTargetsForTribe.cpp | 3 +++ src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp | 4 ++++ src/OpenSHC/Map/Units/TribesState/createAnimal.cpp | 4 ++++ .../Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp | 4 ++++ .../Map/Units/TribesState/predictUnitInterceptPosition.cpp | 2 +- .../Map/Units/TribesState/sortTribePathDestinationsByCost.cpp | 3 ++- 7 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp index 5bc7c31c06..f2df725b80 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp @@ -11,6 +11,10 @@ namespace Map { using Map::Units::UnitLogicState; + /* + Its `short` tribe-size local stays narrow: widening it costs 17.6 points + (64.1% -> 46.5%), the largest such loss in the namespace. + */ // FUNCTION: STRONGHOLDCRUSADER 0x005229C0 void TribesState::addUnitsToTribeAndComputeMovementSpeed(int playerID, int tribeID) { diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp index 4d3cb64f97..09590ac080 100644 --- a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -27,6 +27,9 @@ namespace Map { using Map::Units::States::UnitState; using WindowsHelper::Enums::BOOLEnum; + /* + The `bool` flag stays a byte: BOOLEnum costs 0.2 points (44.6% -> 44.4%). + */ // FUNCTION: STRONGHOLDCRUSADER 0x005244D0 undefined4 TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType) { diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 81efda3376..76e9b2cacf 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -18,6 +18,10 @@ namespace Map { using Map::Units::States::UnitState; using WindowsHelper::Enums::BOOLEnum; + /* + Its three `short` locals stay narrow: widening all three costs 0.9 points + (42.8% -> 41.9%). + */ // FUNCTION: STRONGHOLDCRUSADER 0x005270F0 void TribesState::computeTribePercentages(int tribeID) { diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp index b6ec4fa08c..c77c32ec6f 100644 --- a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -23,6 +23,10 @@ namespace Map { using Commands::MappersEnum; using Map::Units::UnitType; + /* + The `short` tribe-size local stays narrow: widening it costs 0.4 points + (46.8% -> 46.4%). + */ // FUNCTION: STRONGHOLDCRUSADER 0x00525BA0 dword TribesState::createAnimal(MappersEnum animalType, uint x, uint y, int tile) { diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index 3643b58122..c4a4d7b955 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -21,6 +21,10 @@ namespace Map { using Map::Units::UnitInstructionType; using Map::Units::UnitLogicState; + /* + The `short` local stays narrow: widening it costs 3.9 points + (41.8% -> 37.9%). + */ // FUNCTION: STRONGHOLDCRUSADER 0x00523A30 void TribesState::drawFlagsAndUnitDestinations(int tribeID) { diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index b9a6c5c622..b4bf58eae1 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -39,7 +39,7 @@ namespace Map { if (_targetTribeMinSpeed < 2) { _targetTribeMinSpeedBounded = 1; } - short _tribeMinSpeed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; + int _tribeMinSpeed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; int iVar3 = _targetTribeMinSpeedBounded * 4; int _tribeMinSpeedBounded = (int)_tribeMinSpeed; if (_tribeMinSpeed < 2) { diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp index 97d8c1de16..4c53263ca5 100644 --- a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -21,7 +21,8 @@ namespace Map { /* The unit-ID local stays `short`, although our movzx-plus-movsx pair against the original's single movsx says otherwise: widening it costs 2.2 points - (28.8% -> 26.6%, norm 30.3% -> 28.0%). + (28.8% -> 26.6%, norm 30.3% -> 28.0%). Its `bool` flag stays a byte for the same + reason: BOOLEnum costs 5.3 points (28.8% -> 23.5%). */ // FUNCTION: STRONGHOLDCRUSADER 0x00524930 void TribesState::sortTribePathDestinationsByCost(int tribeID, int horseAndRamCount) From 6671fcc7b058621a6dbad373fb19a66a4049f303 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:16:57 +0200 Subject: [PATCH 042/202] reimplement: trimTribeToSize 54.1%, spawnDeerLionOrRabbit 68.9% trimTribeToSize 51.8% -> 54.1%: the size test compares the size first, which is the operand order of the original's `cmp eax, [esp + 0x18]; jl`. spawnDeerLionOrRabbit 68.0% -> 68.9%: herdMinSize and size are both `movzx` in the original where our `int` locals gave `movsx`, so both reads take a `(ushort)`. Note this is the opposite of trimTribeToSize's size read in the same namespace, which the original sign-extends - the cast belongs to the read site, not to the field. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/spawnDeerLionOrRabbit.cpp | 7 +++++-- src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index 75adf3623c..b93a3a2001 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -49,8 +49,11 @@ namespace Map { } _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2; _terrainHeight = DAT_UnitsState::instance.units[_finalTargetUnitID].terrainOrClimbHeight; - _unknown = this->tribes[tribeID].herdMinSize; - _tribeSize = this->tribes[tribeID].size; + /* + Both of these reads are `movzx` in the original, not `movsx`. + */ + _unknown = (ushort)this->tribes[tribeID].herdMinSize; + _tribeSize = (ushort)this->tribes[tribeID].size; _unitX = DAT_UnitsState::instance.units[_finalTargetUnitID].x; _unitY = DAT_UnitsState::instance.units[_finalTargetUnitID].y; if (_tribeSize <= _unknown) { diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 4fabbd1e4b..94dbb5f469 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -23,7 +23,7 @@ namespace Map { _currentTribeID = DAT_CurrentTribeID::instance; unitSelectionIndex = 0; if (this->tribes[DAT_CurrentTribeID::instance].tribeState != 0 - && (iVar3 = (int)this->tribes[DAT_CurrentTribeID::instance].size, smallerSize <= iVar3) && 0 < iVar3) { + && (iVar3 = (int)this->tribes[DAT_CurrentTribeID::instance].size, iVar3 >= smallerSize) && 0 < iVar3) { do { iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( _currentTribeID, unitSelectionIndex); From cdd796c96c0523ba1db169eb5f5958ac0b6f9f73 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:27:51 +0200 Subject: [PATCH 043/202] style: spawnUnitsForAITribe names its second coordinate The original computes both `lea ebx, [edx*8]` and `lea ebp, [ecx*8]` into registers before the spawn call, where we had one named local and one inline expression. Naming the second is code-generation neutral and the call no longer does arithmetic in an argument. playUnitMoveSpeech carries a note instead of a change. The original tail-calls playSpeechSFX with 0x2e - it writes the argument into the incoming slot, pops and jumps - so one of its two call sites ends the function, but adding the `return` at the stone-ammunition site costs 6.2 points (82.8% -> 76.6%), and dropping the `param_1 = 0x2e` write with it costs 11.0. The write models the original's parameter-slot store and has to stay; which of the two sites is the tail call needs telling apart before this is worth another try. Namespace average 79.34% -> 79.36%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp | 8 ++++++++ .../Map/Units/TribesState/spawnUnitsForAITribe.cpp | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp index 6013942e2b..82474fe35e 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp @@ -14,6 +14,14 @@ namespace Map { using Audio::SFX::SpeechEffectID; + /* + The original tail-calls playSpeechSFX with 0x2e - it writes the argument into the + incoming slot, pops and jumps - so one of the two call sites here ends the + function. Adding the `return` to the stone-ammunition site costs 6.2 points + (82.8% -> 76.6%) and dropping the `param_1 = 0x2e` write with it costs 11.0 + (82.8% -> 71.8%), so it is the other site, or the block layout costs more than + the matched jump is worth. Needs the two sites told apart before trying again. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00521EF0 void TribesState::playUnitMoveSpeech(undefined4 param_1) { diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp index 1a5364a2d7..bc2e3cf6a4 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp @@ -22,10 +22,11 @@ namespace Map { int aiUnitBehaviourType; if (((0 < param_2) || (0 < param_3)) && 0 < param_6) { int microXPosition = param_2 * 8; + int microYPosition = param_3 * 8; param_2 = param_6; do { unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( - param_4, param_4, microXPosition, param_3 * 8, 8, param_5); + param_4, param_4, microXPosition, microYPosition, 8, param_5); if (unitID) { DAT_UnitsState::instance.units[unitID].calculatedOwnerPlayerIndex = param_7; UnitTypeShort UVar1 = DAT_UnitsState::instance.units[unitID].unitType; From ebfbbcf979f160a9c99dc55898d50afe58afb1bc Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:31:48 +0200 Subject: [PATCH 044/202] reimplement: spawnUnitsIntoNewTribe 98.3% 89.4% -> 98.3% (norm 86.0% -> 95.5%). The tribe-ID guard is an early return: the original's `jg` skips an inline `xor eax, eax` epilogue rather than jumping over the whole body to a shared one, which is the shape diff_triage.py's RET flag describes. Same restructure that was neutral on createTribe pays nine points here. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp index ab2ad6cee8..2762726020 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnUnitsIntoNewTribe.cpp @@ -20,7 +20,14 @@ namespace Map { return (dword)(0); } dword _tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); - if (0 < (int)_tribeID) { + /* + An early return: the original's `jg` skips an inline `xor eax, eax` epilogue + here rather than jumping over the whole body to a shared one. + */ + if ((int)_tribeID <= 0) { + return (dword)(0); + } + { this->tribes[_tribeID].tribeType = (AITribeTypeShort)tribeType; this->tribes[_tribeID].someIndex = (short)counter; this->tribes[_tribeID].attackWave = (short)DAT_TroopValueState::instance.attackInfo.inv_count; @@ -54,7 +61,6 @@ namespace Map { this->tribes[_tribeID].pendingScatterMove = 1; return (dword)(_tribeID); } - return (dword)(0); } } From b55016c6f1ea50d91e13a2ec81b326a816d08b11 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:35:52 +0200 Subject: [PATCH 045/202] style: commandUnitsToLocation's success flag as an early return Code-generation neutral (42.3% either way) and the early-return form the style guide asks for. createTribeForPlayer keeps its positive guard with a note: the same inversion costs 2.0 points there (75.2% -> 73.2%), where it was worth nine on spawnUnitsIntoNewTribe. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/commandUnitsToLocation.cpp | 18 +++++++++++------- .../Units/TribesState/createTribeForPlayer.cpp | 5 +++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index ac80cfe713..c10270b373 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -82,14 +82,18 @@ namespace Map { } } } while (_selectionUnitID < this->tribes[tribeID].size); - if (!bVar1) { - if ((char)matchUnitSpeeds != '\0') { - this->tribes[tribeID].freeUnitSpeeds = 0; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); - return (undefined4)(1); + /* + An early return, which is where the original puts its own `xor eax, eax`. + */ + if (bVar1) { + return (undefined4)(0); + } + if ((char)matchUnitSpeeds != '\0') { + this->tribes[tribeID].freeUnitSpeeds = 0; } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); + return (undefined4)(1); } return (undefined4)(0); } diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index ada62fbbd8..abc10f3283 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -15,6 +15,11 @@ namespace Map { using AI::Tribes::AITribeType; using Map::Units::SomeTribeBehaviorType; + /* + The inner slot-ID guard stays a positive `if` around the claim: inverting it to an + early return, which took spawnUnitsIntoNewTribe from 89.4% to 98.3%, costs 2.0 + points here (75.2% -> 73.2%). + */ // FUNCTION: STRONGHOLDCRUSADER 0x005227E0 int TribesState::createTribeForPlayer(int playerID) { From 19253e0192b5948e66ecd9768aca4450ad1d37fd Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:39:35 +0200 Subject: [PATCH 046/202] reimplement: scatterTribeUnitsRandomly 90.9% 78.3% -> 90.9% (norm 69.0% -> 84.2%), namespace average 79.43% -> 79.55%. The spawn-queue size test is an early return, which is where the original puts its own `xor eax, eax`. Third function this pattern has paid on, after spawnUnitsIntoNewTribe (+8.9) and trySpawnAdditionalWildlifeForTribe (+20.5), against two where it cost (createTribeForPlayer -2.0, createTribe neutral). What separates them is whether the diff shows the original's zero-return inline behind a `jg`/`jle` or jumped to from a distance. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/scatterTribeUnitsRandomly.cpp | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp index c743d1e4dc..2c345fb2da 100644 --- a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp +++ b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp @@ -33,31 +33,34 @@ namespace Map { iVar2 = MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::getComputationalTileIndex, DAT_PathFindingState::ptr)(); int iVar4 = (int)this->tribes[param_1].size; - if (iVar4 * 0x10 + 200 <= iVar2) { - if (0 < iVar4) { - do { - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar2].dying == 0)) { - index = index + 1 + (DAT_UnitsState::instance.units[iVar2].fixedRng & 0xf); - iVar3 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::getTileInSearchQueue, - DAT_PathFindingState::ptr)(index); - iVar4 = DAT_ViewportRenderState::instance - .translationMatrix[DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[iVar3]] - .addXgetTile; - DAT_UnitsState::instance.units[iVar2].targetY - = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[iVar3]; - DAT_UnitsState::instance.units[iVar2].targetX = (short)iVar3 - (short)iVar4; - DAT_UnitsState::instance.units[iVar2].state.generic = ((UnitStateShort)0xd1); - } - } while (unitSelectionIndex < this->tribes[param_1].size); - } - return 1; + /* + An early return, which is where the original puts its own `xor eax, eax`. + */ + if (iVar2 < iVar4 * 0x10 + 200) { + return 0; + } + if (0 < iVar4) { + do { + iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if ((DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar2].dying == 0)) { + index = index + 1 + (DAT_UnitsState::instance.units[iVar2].fixedRng & 0xf); + iVar3 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::getTileInSearchQueue, + DAT_PathFindingState::ptr)(index); + iVar4 = DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar3]] + .addXgetTile; + DAT_UnitsState::instance.units[iVar2].targetY + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[iVar3]; + DAT_UnitsState::instance.units[iVar2].targetX = (short)iVar3 - (short)iVar4; + DAT_UnitsState::instance.units[iVar2].state.generic = ((UnitStateShort)0xd1); + } + } while (unitSelectionIndex < this->tribes[param_1].size); } - return 0; + return 1; } } From 1002ca623dfbab46c6c77ff7ed1e8e683170166a Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:43:29 +0200 Subject: [PATCH 047/202] reimplement: spawnDeerLionOrRabbit 73.0% 68.9% -> 73.0%. Its one big wrapping `if` is two early-return guards, which is where the original puts its own `xor eax, eax` epilogues. Fourth function the pattern has paid on. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/spawnDeerLionOrRabbit.cpp | 120 +++++++++--------- 1 file changed, 63 insertions(+), 57 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index b93a3a2001..474251a98f 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -33,68 +33,74 @@ namespace Map { int _unitX; int _targetUnitID; int _unitY; - if ((DAT_GameCore::instance.currentMenuViewType != UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) - && (0 < tribeID)) { - if (this->tribes[tribeID].predatorNearby != 0) { - return FALSE; - } - _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; - _finalTargetUnitID = (int)_targetUnitID; - if (unitType == Map::Units::UT_ANTELOPESHDEER) { - _finalTargetUnitID - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); - if (!_finalTargetUnitID) { - _finalTargetUnitID = (int)_targetUnitID; - } + /* + Two early-return guards, which is where the original puts its own + `xor eax, eax` epilogues. + */ + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + return FALSE; + } + if (tribeID <= 0) { + return FALSE; + } + if (this->tribes[tribeID].predatorNearby != 0) { + return FALSE; + } + _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + _finalTargetUnitID = (int)_targetUnitID; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + _finalTargetUnitID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); + if (!_finalTargetUnitID) { + _finalTargetUnitID = (int)_targetUnitID; } - _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2; - _terrainHeight = DAT_UnitsState::instance.units[_finalTargetUnitID].terrainOrClimbHeight; - /* - Both of these reads are `movzx` in the original, not `movsx`. - */ - _unknown = (ushort)this->tribes[tribeID].herdMinSize; - _tribeSize = (ushort)this->tribes[tribeID].size; - _unitX = DAT_UnitsState::instance.units[_finalTargetUnitID].x; - _unitY = DAT_UnitsState::instance.units[_finalTargetUnitID].y; - if (_tribeSize <= _unknown) { - param_2 = param_2 / 4; + } + _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2; + _terrainHeight = DAT_UnitsState::instance.units[_finalTargetUnitID].terrainOrClimbHeight; + /* + Both of these reads are `movzx` in the original, not `movsx`. + */ + _unknown = (ushort)this->tribes[tribeID].herdMinSize; + _tribeSize = (ushort)this->tribes[tribeID].size; + _unitX = DAT_UnitsState::instance.units[_finalTargetUnitID].x; + _unitY = DAT_UnitsState::instance.units[_finalTargetUnitID].y; + if (_tribeSize <= _unknown) { + param_2 = param_2 / 4; + } + /* + Keeping the pointer rather than naming the field: resolving it costs one + instruction here (quick_diff 0.7295 -> 0.7265), because the field form + re-evaluates tribes[tribeID] while the pointer freezes the address. + */ + psVar1 = &this->tribes[tribeID].field138_0x284; + *psVar1 = *psVar1 + 1; + if ((param_2 <= this->tribes[tribeID].field138_0x284) + && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field138_0x284 = 0, + _tribeSize < sVar2)) { + if ((unitType == Map::Units::UT_RABBIT) + && (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation)) { + iVar3 = 110; + } else { + iVar3 = _tribeSize < _unknown ? 25 : 100; } - /* - Keeping the pointer rather than naming the field: resolving it costs one - instruction here (quick_diff 0.7295 -> 0.7265), because the field form - re-evaluates tribes[tribeID] while the pointer freezes the address. - */ - psVar1 = &this->tribes[tribeID].field138_0x284; - *psVar1 = *psVar1 + 1; - if ((param_2 <= this->tribes[tribeID].field138_0x284) - && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field138_0x284 = 0, - _tribeSize < sVar2)) { - if ((unitType == Map::Units::UT_RABBIT) - && (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation)) { - iVar3 = 110; - } else { - iVar3 = _tribeSize < _unknown ? 25 : 100; - } - if (iVar3 <= (int)(_rng2 & 0x7f)) { - _rng2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( - 0, 0, (int)(_unitX * 8), (int)(_unitY * 8), (int)(_terrainHeight), unitType); - if (_rng2) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(_rng2, tribeID); - DAT_UnitsState::instance.units[_rng2].substate - = (byte)DAT_UnitsState::instance.units[_rng2].fixedRng & 3; - if (unitType == Map::Units::UT_ANTELOPESHDEER) { - DAT_UnitsState::instance.units[_rng2].antelopeBasedRngValue = 2; - } - DAT_UnitsState::instance.units[_rng2].disappearFadeAlphaCountdown = 0x20; - if (unitType == Map::Units::UT_ANTELOPESHDEER) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateAnimalHerdBehaviorState, this)( - tribeID); - } - return TRUE; + if (iVar3 <= (int)(_rng2 & 0x7f)) { + _rng2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + 0, 0, (int)(_unitX * 8), (int)(_unitY * 8), (int)(_terrainHeight), unitType); + if (_rng2) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(_rng2, tribeID); + DAT_UnitsState::instance.units[_rng2].substate + = (byte)DAT_UnitsState::instance.units[_rng2].fixedRng & 3; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + DAT_UnitsState::instance.units[_rng2].antelopeBasedRngValue = 2; + } + DAT_UnitsState::instance.units[_rng2].disappearFadeAlphaCountdown = 0x20; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateAnimalHerdBehaviorState, this)( + tribeID); } + return TRUE; } } - return FALSE; } return FALSE; } From 715b9fcd3c7e82adc9428c2b27b5a73d5d6f27b2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 20:50:31 +0200 Subject: [PATCH 048/202] style: early returns in getAvailableTribeID and upgradeTribeArrayLayoutForMapVersion Both are code-generation neutral (60.4% and 46.5% either way) and both lose a level of nesting: getAvailableTribeID no longer carries the decompiler's four nested `if (0 < _tribeID)` arms around two near-identical search loops, and upgradeTribeArrayLayoutForMapVersion's entry is two guards rather than one wrapping `if`. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/getAvailableTribeID.cpp | 55 ++++++++++--------- .../upgradeTribeArrayLayoutForMapVersion.cpp | 48 +++++++++------- 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index 67c84cd233..d609b4a18e 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -16,6 +16,10 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522720 int TribesState::getAvailableTribeID(int playerID) { + /* + Early returns rather than the decompiler's nested `if (0 < _tribeID)` arms: + the original places each `xor eax, eax` inline behind its own guard. + */ int _tribeID; if (DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) { _tribeID = 1249; @@ -27,35 +31,36 @@ namespace Map { } } DAT_CurrentTribeID::instance = _tribeID; - if (0 < _tribeID) { - this->tribes[_tribeID].time = 1; - return _tribeID; + if (_tribeID <= 0) { + return 0; } - } else { - _tribeID = 1250 - playerID; + this->tribes[_tribeID].time = 1; + return _tribeID; + } + _tribeID = 1250 - playerID; + DAT_CurrentTribeID::instance = _tribeID; + if (_tribeID <= 0) { + return 0; + } + /* + is tribe slot in use? + */ + while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { + _tribeID = _tribeID + -8; DAT_CurrentTribeID::instance = _tribeID; - if (0 < _tribeID) { - /* - is tribe slot in use? - */ - while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { - _tribeID = _tribeID + -8; - DAT_CurrentTribeID::instance = _tribeID; - if (_tribeID <= 0) { - return 0; - } - } - DAT_CurrentTribeID::instance = _tribeID; - if (0 < _tribeID) { - /* - claim it - */ - this->tribes[_tribeID].time = (short)DAT_GameCore::instance.mapTimeInTicks + 400; - return _tribeID; - } + if (_tribeID <= 0) { + return 0; } } - return 0; + DAT_CurrentTribeID::instance = _tribeID; + if (_tribeID <= 0) { + return 0; + } + /* + claim it + */ + this->tribes[_tribeID].time = (short)DAT_GameCore::instance.mapTimeInTicks + 400; + return _tribeID; } } diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 708476be58..fb9251c64f 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -16,27 +16,35 @@ namespace Map { int iVar2; short* _Src; short* psVar3; - if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 169) { - iVar2 = 1249; - _Dst = this->tribes + 0x4e1; - _Src = this->tribes[944].unitSelectionBitMasked + 0x7a; - do { - if (_Dst != (Tribe*)_Src) { - memmove(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); - } - psVar3 = _Dst->unitSelectionBitMasked; - memmove(&_Dst->field34_0x1c8, (void*)((int)(_Src + 0x80)), (size_t)((int)(364))); - iVar2 = iVar2 + -1; - _Src = _Src + -0x136; - _Dst = _Dst + -1; - psVar3 = psVar3 + 100; - for (iVar1 = 50; iVar1 != 0; iVar1 = iVar1 + -1) { - psVar3[0] = 0; - psVar3[1] = 0; - psVar3 = psVar3 + 2; - } - } while (-1 < iVar2); + /* + Two early-return guards rather than one wrapping `if`, which is where the + original places its epilogue. + */ + if (receivedMapVersion == packagerMapVersion) { + return; } + if (169 <= (int)receivedMapVersion) { + return; + } + iVar2 = 1249; + _Dst = this->tribes + 0x4e1; + _Src = this->tribes[944].unitSelectionBitMasked + 0x7a; + do { + if (_Dst != (Tribe*)_Src) { + memmove(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); + } + psVar3 = _Dst->unitSelectionBitMasked; + memmove(&_Dst->field34_0x1c8, (void*)((int)(_Src + 0x80)), (size_t)((int)(364))); + iVar2 = iVar2 + -1; + _Src = _Src + -0x136; + _Dst = _Dst + -1; + psVar3 = psVar3 + 100; + for (iVar1 = 50; iVar1 != 0; iVar1 = iVar1 + -1) { + psVar3[0] = 0; + psVar3[1] = 0; + psVar3 = psVar3 + 2; + } + } while (-1 < iVar2); return; } From c36b01027ddd40735b3fc19016bec4549b1679ba Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:01:08 +0200 Subject: [PATCH 049/202] note: assignAttackTargetsForTribe's goto web is the original's shape Rebuilt it goto-free - the unit-state gate as `continue`s rather than a `bVar7` flag with three jumps into the arm below, the target dispatch as a flat if/else-if chain, and both arms falling through into the shared tail - and it costs 11.3 points (44.6% -> 33.3%, norm 40.7% -> 28.4%). Reverted, with the measurement above the function. Worth recording why the nesting is not a bug even though it reads like one: `if (targetType == 1010)` sits inside `if (targetType == 1021)`, so from the top of the function it can never be true, but the unit-state gate below jumps into it, which is the only path that reaches it. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/assignAttackTargetsForTribe.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp index 09590ac080..68a6918efe 100644 --- a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -28,7 +28,16 @@ namespace Map { using WindowsHelper::Enums::BOOLEnum; /* - The `bool` flag stays a byte: BOOLEnum costs 0.2 points (44.6% -> 44.4%). + The gotos and the `bVar7` flag stay, against the style list. The nesting reads + wrongly - `if (targetType == 1010)` sits inside `if (targetType == 1021)`, so from + the top of the function it can never be true - but it is reachable, because the + unit-state gate below jumps into it, and the shape is the original's: writing the + gate as `continue`s, the target dispatch as a flat chain, and both arms as + fall-through into the shared tail costs 11.3 points (44.6% -> 33.3%, norm 40.7% + -> 28.4%). + + The `bool` flag stays a byte for the same reason: BOOLEnum costs 0.2 points + (44.6% -> 44.4%). */ // FUNCTION: STRONGHOLDCRUSADER 0x005244D0 undefined4 TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType) From 0362835060361ad47b8d71bd06d6ba001e2e59c4 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:01:09 +0200 Subject: [PATCH 050/202] status: TribesState at 79.59% average, 53 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 30eea631f8..1ad99d2f95 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46389,7 +46389,7 @@ SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521280 | 27.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521500 | 30.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521500 | 31.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending @@ -46819,11 +46819,11 @@ SHC_3BB0A8C1_0x00522CA0 | 47.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522DD0 | 42.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522EF0 | 51.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522EF0 | 54.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522F70 | 72.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523030 | 89.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523030 | 98.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523190 | 100.0% | Reimplemented @@ -46859,7 +46859,7 @@ SHC_3BB0A8C1_0x00523790 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005237F0 | 32.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523920 | 78.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523920 | 90.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523A30 | 41.8% | Reimplemented, asm differs @@ -46951,7 +46951,7 @@ SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525F20 | 68.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525F20 | 73.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x005260B0 | 87.2% | Reimplemented, asm differs From c9785100ee3c56907e5ef961fac1db0594237a40 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:29:09 +0200 Subject: [PATCH 051/202] reimplement: playUnitCombatSpeechForTarget's arms, read from the disassembly Read the function's real dispatch out of Ghidra rather than its decompilation: the jump table at 0x521e50 is eight arms and the byte table at 0x521e70 maps unitType - 0x16 onto them, which confirms the case grouping we had and settles two places where the decompilation was simply wrong. - The default arm plays *one* speech. The arm at 0x521df7 is a `jz` chain over the five target types into the 0x11 block, with the 0x15 block inline and carrying its own `ret 8`. Our source played 0x15 conditionally and then 0x11 unconditionally, so a selection against most targets got two speeches. - The UT_A_SWORDSMAN arm at 0x521dc8 ends in `ret 8`. Without that return it fell out of the switch into the shared play below, which reads `_otherX` - never assigned on that path. - The firethrower range is tested before the fallback, not after: the chain at 0x521ae3 is six `cmp`/`jnz` pairs with `mov ecx, [esp + 0x1c]` only on the fall-through. Worth 1.3 points on its own. 28.1% -> 24.9%, and that is the honest cost of the two behaviour fixes: the block layout shifts when MSVC stops sharing an epilogue it should not have been sharing. Correct behaviour comes before the percentage, and both are provable from the instruction stream rather than inferred. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../playUnitCombatSpeechForTarget.cpp | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index 37c5ed54e5..8e44b2c55f 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -62,12 +62,16 @@ namespace Map { } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_HARCHER) { _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; + } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_FIRETHROWER) { + /* + The firethrower range is tested before the fallback, not after it: + the chain at 0x521ae3 is six `cmp`/`jnz` pairs with + `mov ecx, [esp + 0x1c]` only on the fall-through. + */ + _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22]; } else { _distance = unitID; - if (_ptrptrCurrentSelectionID == Map::Units::UT_A_FIRETHROWER) { - _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22] - * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22]; - } } _xDifference = (int)DAT_UnitsState::instance.units[_otherUnitID].x - (int)DAT_UnitsState::instance.units[unitID].x; @@ -177,11 +181,18 @@ namespace Map { break; default: UVar2 = DAT_UnitsState::instance.units[unitID].unitType; + /* + The two speeches are exclusive. Ghidra's disassembly of the table arm at + 0x521df7 is a `jz` chain over the five target types into the 0x11 block, + with the 0x15 block inline and its own `ret 8`; the decompilation played + 0x15 conditionally and then 0x11 unconditionally. + */ if (UVar2 != Map::Units::UT_E_KNIGHT && UVar2 != Map::Units::UT_E_SWORD && UVar2 != Map::Units::UT_E_MACE && UVar2 != Map::Units::UT_E_PIKE && UVar2 != Map::Units::UT_LORD) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); + return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x11); @@ -191,9 +202,15 @@ namespace Map { if (sVar1 < 0xf) { return; } + /* + The table arm at 0x521dc8 ends in `ret 8`. Without the return this fell + out of the switch into the shared play below with `_otherX` never + assigned on this path. + */ MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); + return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, From 90bb1088e5d9e50b91a4db22ed76dd36b4c2244a Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:34:24 +0200 Subject: [PATCH 052/202] reimplement: playUnitSelectionSound 50.4% from the disassembly 46.5% -> 50.4%, and normalized 34.4% -> 49.1%. The earlier attempt at this function worked from the decompilation, corrected the arms, and lost 32 points. Reading the instruction stream instead settles the three things it got wrong: - the horse archer arm keeps a pointer to each coordinate (`lea edi, [esi + 0x1388612]`, `lea esi, [esi + 0x1388610]`) and reads through them four times rather than re-indexing the unit, which is the pair of pointers the import had and the rewrite removed; - the knight arm plays one shared size-dependent effect and then 0x59, with 0x57 extra for two or three units - not three separate play-and-return blocks; - the siege arm's last test is for three engineers and returns without a speech otherwise, where the rewrite had added a fourth 0xe fallback. The jump table at 0x521a48 has six arms and the byte table at 0x521a60 maps unitType - 0x1b onto them; checked against UnitType.hpp, the import's case grouping was right (0x1b sword, 0x1c knight, 0x1d nothing, 0x1e engineer, 0x27/0x3a/0x3b siege, 0x4a horse archer). Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/playUnitSelectionSound.cpp | 176 +++++++++--------- 1 file changed, 92 insertions(+), 84 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index fc68b1c006..1d42c26ee3 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -15,132 +15,140 @@ namespace Map { using Map::Units::UnitType; /* - fixme: this function is a chain of tail calls in the original and the - decompilation of it is wrong in two places. The UT_E_SWORD arm reaches - `iVar5 = 0xad` unconditionally, so for a two-unit selection it plays 0xae, then - 0xaf, then 0xad where the original plays 0xae alone and returns; the original has - three separate blocks, each with its own `ret`, and a selection below three units - that is neither one nor two plays nothing. `if (param_1 < 3) {}` is that case. - Every `param_1 = ;` in front of a playUnitSpeechEffect call is MSVC writing - the argument into the incoming parameter slot for a tail call, not a real - assignment, and the siege arm's five arms each return rather than falling into - the shared block at the end. - - Rebuilding it from those branches was tried and reverted: writing the arms out - separately costs 32 points of raw match (46.5% -> 14.5%) because the original - shares the play calls through its tail calls, while the normalized match rises - (34.4% -> 40.8%). It needs the sharing reproduced, not just the arms corrected. + Rebuilt from Ghidra's disassembly of 0x5217e0 rather than its decompilation: the + jump table at 0x521a48 has six arms and the byte table at 0x521a60 maps + unitType - 0x1b onto them. The speech calls in the engineer and siege arms are + tail calls (`mov [esp + 4], ` then `jmp`), which is what the decompiler + printed as an assignment to `param_1` in front of each one. */ // FUNCTION: STRONGHOLDCRUSADER 0x005217E0 void TribesState::playUnitSelectionSound(int param_1) { - short* psVar1; - short* psVar2; - int iVar3; - UnitType UVar4; - int iVar5; - int sfxOffsetInArray; - iVar3 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); - UVar4 + int const iVar3 + = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + UnitType const UVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(param_1, ¶m_1); - iVar5 = param_1; switch (UVar4) { case Map::Units::UT_E_SWORD: - if (param_1 != 1) { - if (param_1 == 2) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0xae); - } - if (param_1 < 3) {} - iVar5 = 0xaf; - LAB_0052188b: + /* + Three separate blocks at 0x521819, each with its own `ret 4`, and a + selection below three that is neither one nor two plays nothing. + */ + if (param_1 == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - iVar5); + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0xad); + return; } - iVar5 = 0xad; - goto LAB_00521827; - case Map::Units::UT_E_KNIGHT: - if (param_1 == 1) { - iVar5 = 0x56; - LAB_005218ec: + if (param_1 == 2) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - iVar5); + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0xae); + return; + } + if (param_1 < 3) { + return; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0xaf); + return; + case Map::Units::UT_E_KNIGHT: { + /* + The arm at 0x5218aa: one shared play of the size-dependent effect, then + 0x59. A selection of two or three gets 0x57 as well, and one of four or + more gets 0x58 instead of 0x56. + */ + int _sfx; + if (param_1 == 1) { + _sfx = 0x56; } else { - if ((param_1 == 2) || (param_1 == 3)) { + if (param_1 == 2 || param_1 == 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0x57); } - if (3 < iVar5) { - iVar5 = 0x58; - goto LAB_005218ec; + if (param_1 < 4) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); + return; } + _sfx = 0x58; } - iVar5 = 0x59; - LAB_00521827: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - iVar5); + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), _sfx); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); return; + } case Map::Units::UT_E_ENGINEER: - if ((DAT_UnitsState::instance.units[iVar3].unitType == Map::Units::UT_E_ENGINEER) - && (DAT_UnitsState::instance.units[iVar3].resourceToDeposit != 0)) { - param_1 = 0x24; - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0x24); + if (DAT_UnitsState::instance.units[iVar3].unitType != Map::Units::UT_E_ENGINEER) { + return; + } + if (DAT_UnitsState::instance.units[iVar3].resourceToDeposit == 0) { + return; } - break; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0x24); + return; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TOWER: - case Map::Units::UT_S_BATTERINGRAM: - iVar5 = MACRO_CALL_MEMBER( + case Map::Units::UT_S_BATTERINGRAM: { + int const _requiredEngineers = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(iVar3); - if (iVar5 <= 0) { - iVar5 = 0x68; - goto LAB_0052188b; + if (_requiredEngineers <= 0) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0x68); + return; } - if ((DAT_UnitsState::instance.units[iVar3] + if (DAT_UnitsState::instance.units[iVar3] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 0) - || (3 < iVar5)) { - param_1 = 0xe; + == 0 + || 3 < _requiredEngineers) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xe); + return; } - if (iVar5 == 1) { - param_1 = 0xb; + if (_requiredEngineers == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xb); + return; } - if (iVar5 == 2) { - param_1 = 0xc; + if (_requiredEngineers == 2) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xc); + return; } - if (iVar5 == 3) { - param_1 = 0xd; + /* + The last arm at 0x5219d9 tests for three and returns without a speech + otherwise, which the earlier count tests already make unreachable. + */ + if (_requiredEngineers == 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xd); } - break; - case Map::Units::UT_A_HARCHER: - psVar1 = &DAT_UnitsState::instance.units[iVar3].y; - psVar2 = &DAT_UnitsState::instance.units[iVar3].x; + return; + } + case Map::Units::UT_A_HARCHER: { + /* + The arm at 0x521915 keeps a pointer to each coordinate (`lea edi, [esi + + 0x1388612]`, `lea esi, [esi + 0x1388610]`) and reads through them four + times rather than re-indexing the unit. + */ + short* const _unitY = &DAT_UnitsState::instance.units[iVar3].y; + short* const _unitX = &DAT_UnitsState::instance.units[iVar3].x; + int sfxOffsetInArray; if (param_1 == 1) { - iVar3 = (int)*psVar1; - iVar5 = (int)*psVar2; sfxOffsetInArray = 0xea; - } else if (param_1 < 5) { - iVar3 = (int)*psVar1; - iVar5 = (int)*psVar2; + } else if (param_1 <= 4) { sfxOffsetInArray = 0xeb; } else { - iVar3 = (int)*psVar1; - iVar5 = (int)*psVar2; sfxOffsetInArray = 0xec; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - iVar5, iVar3, sfxOffsetInArray); + (int)*_unitX, (int)(*_unitY), sfxOffsetInArray); MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)*psVar2, (int)(*psVar1), 0x59); + (int)*_unitX, (int)(*_unitY), 0x59); + } } } From 94459fb75ac52647a05febd84b0c5c8a8f701326 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:40:15 +0200 Subject: [PATCH 053/202] reimplement: processDeerMoving 44.2% from the disassembly 24.1% -> 44.2%, and the FRAME flag is gone. Three things the instruction stream settles that the decompilation had wrong: - The map-editor branch returns. The block at 0x52af03 ends in `ret 4` right after zeroing the counter; without it, a deer tribe in the editor went on to run the whole predator and wander dispatch. - The predator test keeps one local for the sum, not two for its terms: the original adds as it reads (`mov ebp, [eax + 0x28]`, `add ebp, [eax + 0x48]`), which is the two stack slots diff_slots.py had us carrying. - The trim is the arm the original places first - `cmp cx, 0x1e; jle` falls into it - not the else. Also confirms the four WildlifeGridElement fields are right: the base is 0xEE23D8, so the displacements 0xee2400/0x18/0x1c/0x20 are lionCount, marshTiles, rockyTiles and field18_0x48, which is what we read. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/processDeerMoving.cpp | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index e510f34b02..095b2f9391 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -32,9 +32,7 @@ namespace Map { { int sVar2; int SVar3; - int iVar4; int iVar5; - int iVar6; BOOLEnum BVar7; int iVar8; int _x10; @@ -43,20 +41,29 @@ namespace Map { _x10 = (int)DAT_UnitsState::instance.units[sVar2].x / 10; _y10 = (int)DAT_UnitsState::instance.units[sVar2].y / 10; iVar8 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; - iVar4 = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount; + /* + One local for the sum, not two for the terms: the original adds them as it + reads (`mov ebp, [eax + 0x28]` then `add ebp, [eax + 0x48]`) and keeps only + the result, which is two stack slots fewer than diff_slots.py reports for us. + */ + int const _predatorCount = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount + + DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; - iVar6 = DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; sVar2 = (ushort)this->tribes[tribeID].size; this->tribes[tribeID].unkIsAnimalTribe = 1; - if (sVar2 < 0x1f) { - this->tribes[tribeID].herdMinSize = sVar2 / 2; - this->tribes[tribeID].herdMaxSize = sVar2 * 2; - } else { + /* + The trim is the arm the original places first: `cmp cx, 0x1e; jle` falls into + it and jumps to the herd-size arm. + */ + if (30 < sVar2) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trimTribeToSize, this)(tribeID, (int)(30)); this->tribes[tribeID].herdMinSize = 0xf; this->tribes[tribeID].herdMaxSize = 0x3c; + } else { + this->tribes[tribeID].herdMinSize = sVar2 / 2; + this->tribes[tribeID].herdMaxSize = sVar2 * 2; } - if ((iVar4 + iVar6 != 0) && (this->tribes[tribeID].predatorNearby == 0)) { + if ((_predatorCount != 0) && (this->tribes[tribeID].predatorNearby == 0)) { this->tribes[tribeID].predatorNearby = 1; } BVar7 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( @@ -76,8 +83,13 @@ namespace Map { this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; return; } + /* + The original returns from this one too: the block at 0x52af03 ends in + `ret 4` right after zeroing the counter. + */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { this->tribes[tribeID].unknownCounter01 = 0; + return; } sVar2 = this->tribes[tribeID].predatorNearby; if (!sVar2) { From 767053144739d6bd5826317c77c4196c8baaf60a Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:45:04 +0200 Subject: [PATCH 054/202] reimplement: the map-editor branch returns, in two more animal updaters updateRabbitTribeBehavior 33.3% -> 54.0% and updateLionWolfTribeBehavior 59.9% -> 62.9%. Namespace average 79.78% -> 79.99%. Same shape as processDeerMoving: the block that zeroes unknownCounter01 when the map editor is open ends in `ret 4` (0x52b489 and 0x52b297), so an animal tribe in the editor was running its whole predator and wander dispatch. updateRabbitTribeBehavior also gets the initial-move-order return its three siblings already had. The earlier pass declined to add it because orig_asm's filtered listing did not show the `ret 4` inside the 73 bytes the test jumps over; Ghidra's full disassembly of 0x52b440 does. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/updateLionWolfTribeBehavior.cpp | 5 +++++ .../Units/TribesState/updateRabbitTribeBehavior.cpp | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index ccaebc1dcd..6559677a2f 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -83,8 +83,13 @@ namespace Map { } this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; } + /* + The original returns from this one: the block at 0x52b297 ends in `ret 4` + right after zeroing the counter, as in its three sibling updaters. + */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { this->tribes[param_1].unknownCounter01 = 0; + return; } SVar3 = this->tribes[param_1].tribeBehaviorType; if (SVar3 == (SomeTribeBehaviorType)0) { diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index ed9a0d97bb..7005a2a762 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -59,9 +59,19 @@ namespace Map { (uint)(DAT_UnitsState::instance.units[_targetUnitID].x), (uint)(DAT_UnitsState::instance.units[_targetUnitID].y), 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + /* + And from this one: the block at 0x52b440 ends in `ret 4` after setting + the behaviour type, like its three sibling updaters. + */ + return; } + /* + The original returns from this one: the block at 0x52b489 ends in `ret 4` + right after zeroing the counter. + */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { this->tribes[tribeID].unknownCounter01 = 0; + return; } sVar2 = this->tribes[tribeID].predatorNearby; if (!sVar2) { From f100c2c8eedb8e407dba638958b16aa199754a1a Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:56:56 +0200 Subject: [PATCH 055/202] reimplement: four dropped exits and a ten-element rally array, from the disassembly aiAssignNewUnitToTribe 32.1 -> 100%: the monk arm at 0x5238ac ends in its own `ret 0xc` and never reaches the shared path, so a monk joined one tribe, not two. importStoredInfoFromSlot0 42.7 -> 94.5%: the block at 0x522ed0 is an early return, and the copy loop covers 0x260..0x287, which makes rallyPointArray ten pairs rather than nine plus padding. removeAllTribeUnits 58.6 -> 82.1%: the size is read through the pointer the original takes once. updateTribeRallyFlags: one flag reloaded at the loop exit, and the rallyRelatedFlag clear runs for every living unit. Namespace average 79.99% -> 81.25%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/Tribe.hpp | 3 +- .../TribesState/aiAssignNewUnitToTribe.cpp | 6 +++ .../TribesState/importStoredInfoFromSlot0.cpp | 48 ++++++++++------- .../Units/TribesState/removeAllTribeUnits.cpp | 24 +++++---- .../TribesState/updateTribeRallyFlags.cpp | 52 ++++++++++--------- 5 files changed, 78 insertions(+), 55 deletions(-) diff --git a/src/OpenSHC/Map/Units/Tribe.hpp b/src/OpenSHC/Map/Units/Tribe.hpp index 1079958762..b9c00ba300 100644 --- a/src/OpenSHC/Map/Units/Tribe.hpp +++ b/src/OpenSHC/Map/Units/Tribe.hpp @@ -75,8 +75,7 @@ namespace Map { UnitTypeShort unitType; // 0x00000228 length: 2 short aggressiveStanceCounter; // 0x0000022A length: 2 undefined1 padding_0x22c[12]; // 0x0000022C length: 12 - short rallyPointArray[9][2]; // 0x00000238 length: 36 - undefined1 padding_0x25c[4]; // 0x0000025C length: 4 + short rallyPointArray[10][2]; // 0x00000238 length: 40 short currentRallyPointIndex; // 0x00000260 length: 2 short rallyPointCount; // 0x00000262 length: 2 undefined1 padding_0x264[20]; // 0x00000264 length: 20 diff --git a/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp index 692609a7ed..c55674e523 100644 --- a/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp @@ -40,7 +40,13 @@ namespace Map { DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk = this->tribes[_monkTribe].uid; } + /* + The monk arm is a block of its own in the disassembly, from 0x5238ac to + its own `ret 0xc` at 0x52390f: it never reaches the shared path below, so + a monk is added to one tribe rather than two. + */ MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _monkTribe); + return; } short* psVar1 = DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeIDs + _index; int _tribe = (int)*psVar1; diff --git a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp index 4d814c1978..e53c0665ff 100644 --- a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp +++ b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp @@ -8,29 +8,37 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522DD0 void TribesState::importStoredInfoFromSlot0(undefined4 param_1, int tribeID) { - if (this->tribeCopiedToSlot0) { - this->tribes[tribeID].field166_0x2be = this->tribes[0].field166_0x2be; - this->tribes[tribeID].someUnitID = this->tribes[0].someUnitID; - this->tribes[tribeID].someUnitUID = this->tribes[0].someUnitUID; - this->tribes[tribeID].someTile = this->tribes[0].someTile; - this->tribes[tribeID].someTile2 = this->tribes[0].someTile2; - this->tribes[tribeID].currentRallyPointIndex = this->tribes[0].currentRallyPointIndex; - this->tribes[tribeID].rallyPointCount = this->tribes[0].rallyPointCount; - this->tribes[tribeID].isRallyingUnk = this->tribes[0].isRallyingUnk; + /* + The block at 0x522ed0 copies the stance and ends in its own `ret 8`, and it + reaches the field through an unfolded `[eax + ecx + 0x2e0]` rather than the + folded base the rest of the function uses, which is what an early return out + of the top of the function compiles to. + */ + if (!this->tribeCopiedToSlot0) { this->tribes[tribeID].unitStance = this->tribes[0].unitStance; - /* - The original unrolls the inner loop over the nine rally points and keeps - the outer one over the two coordinates, stepping its cursor by one short. - The import wrote the nine stores out as ten, one past the array. - */ - for (int _coordinate = 0; _coordinate < 2; _coordinate = _coordinate + 1) { - for (int _point = 0; _point < 9; _point = _point + 1) { - this->tribes[tribeID].rallyPointArray[_point][_coordinate] - = this->tribes[0].rallyPointArray[_point][_coordinate]; - } - } + return; } + this->tribes[tribeID].field166_0x2be = this->tribes[0].field166_0x2be; + this->tribes[tribeID].someUnitID = this->tribes[0].someUnitID; + this->tribes[tribeID].someUnitUID = this->tribes[0].someUnitUID; + this->tribes[tribeID].someTile = this->tribes[0].someTile; + this->tribes[tribeID].someTile2 = this->tribes[0].someTile2; + this->tribes[tribeID].currentRallyPointIndex = this->tribes[0].currentRallyPointIndex; + this->tribes[tribeID].rallyPointCount = this->tribes[0].rallyPointCount; + this->tribes[tribeID].isRallyingUnk = this->tribes[0].isRallyingUnk; this->tribes[tribeID].unitStance = this->tribes[0].unitStance; + /* + The original unrolls the inner loop over the ten rally points and keeps + the outer one over the two coordinates, stepping its cursor by one short + between the two passes. Ten is right: the copy covers 0x260 to 0x287, + which is where `rallyPointArray` ends. + */ + for (int _coordinate = 0; _coordinate < 2; _coordinate = _coordinate + 1) { + for (int _point = 0; _point < 10; _point = _point + 1) { + this->tribes[tribeID].rallyPointArray[_point][_coordinate] + = this->tribes[0].rallyPointArray[_point][_coordinate]; + } + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp index 9f6c621b9e..b0f1c1167f 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp @@ -11,19 +11,25 @@ namespace Map { using Map::Units::UnitLogicState; + /* + The size is read through a pointer the original takes once + (`lea ebx, [eax + edi + 0x5c]`) and re-reads at the bottom of the loop; the + decompiler's shuffle through a single variable is an artefact of the index and + the unit id sharing no register. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00525470 void TribesState::removeAllTribeUnits(int param_1) { - int iVar2; - int iVar1 = 0; - if (0 < this->tribes[param_1].size) { + int _index = 0; + short* const _size = &this->tribes[param_1].size; + if (0 < *_size) { do { - iVar2 = iVar1 + 1; - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, iVar1); - DAT_UnitsState::instance.units[iVar1].logicalState = Map::Units::ULS_REMOVE; - iVar1 = iVar2; - } while (iVar2 < this->tribes[param_1].size); + int const _unitID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, _index); + _index = _index + 1; + DAT_UnitsState::instance.units[_unitID].logicalState = Map::Units::ULS_REMOVE; + } while (_index < (int)*_size); } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp index ee1629c340..e1ea0715cc 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp @@ -11,37 +11,41 @@ namespace Map { using Map::Units::UnitLogicState; + /* + One flag, not two: the original zeroes it once, writes 1 into it inside the loop + and reloads it into the return register at the loop exit (0x523fef). The + decompiler's `uVar3 = local_4` at the bottom of every iteration is that single + reload hoisted into the body. + + The `rallyRelatedFlag = 0` store at 0x523fc7 sits between the two rally tests and + runs for every living unit, so the gate has to be two nested ifs rather than one + comma-spliced condition. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00523F70 undefined4 TribesState::updateTribeRallyFlags(int param_1) { - short* psVar1; - int iVar2; - undefined4 uVar3; - int unitSelectionIndex; - undefined4 local_4; - short _isRallying; - psVar1 = &this->tribes[param_1].size; - unitSelectionIndex = 0; - local_4 = 0; - uVar3 = 0; - if (0 < *psVar1) { + short* const _size = &this->tribes[param_1].size; + int unitSelectionIndex = 0; + undefined4 _anyRallying = 0; + if (0 < *_size) { do { - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); + int const _unitID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar2].dying == 0 - && (_isRallying = DAT_UnitsState::instance.units[iVar2].goToRallyPoint, - DAT_UnitsState::instance.units[iVar2].rallyRelatedFlag = 0, - _isRallying != 0 - || (DAT_UnitsState::instance.units[iVar2].isSelectable_OR_matchTime == 0))) { - local_4 = 1; - DAT_UnitsState::instance.units[iVar2].rallyRelatedFlag = 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0) { + short const _isRallying = DAT_UnitsState::instance.units[_unitID].goToRallyPoint; + DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; + if (_isRallying != 0 + || DAT_UnitsState::instance.units[_unitID].isSelectable_OR_matchTime == 0) { + _anyRallying = 1; + DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 1; + } } - uVar3 = local_4; - } while (unitSelectionIndex < *psVar1); + } while (unitSelectionIndex < *_size); } - return (undefined4)(uVar3); + return (undefined4)(_anyRallying); } } From f7afb0b65d73fabc197f369755ff03f7b35f7fb2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 21:57:34 +0200 Subject: [PATCH 056/202] status: TribesState at 81.25% average, 54 of 113 at 100% Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 1ad99d2f95..daac9a3c67 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46395,7 +46395,7 @@ SHC_3BB0A8C1_0x00521690 | 0.0% | Pending SHC_3BB0A8C1_0x00521720 | 71.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 46.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005217E0 | 50.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 28.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 24.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending @@ -46799,7 +46799,7 @@ SHC_3BB0A8C1_0x00522720 | 60.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x005227E0 | 75.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522890 | 30.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522890 | 30.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented @@ -46817,7 +46817,7 @@ SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522CA0 | 47.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522DD0 | 42.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522DD0 | 94.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522EF0 | 54.1% | Reimplemented, asm differs @@ -46845,7 +46845,7 @@ SHC_3BB0A8C1_0x00523410 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523520 | 96.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523590 | 78.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523590 | 78.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523630 | 100.0% | Reimplemented @@ -46857,7 +46857,7 @@ SHC_3BB0A8C1_0x00523750 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523790 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005237F0 | 32.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523920 | 90.9% | Reimplemented, asm differs @@ -46935,7 +46935,7 @@ SHC_3BB0A8C1_0x00525370 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005253F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525470 | 58.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525470 | 82.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x005254C0 | 100.0% | Reimplemented @@ -49141,11 +49141,11 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 24.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052AD50 | 44.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 60.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B110 | 62.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B390 | 33.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B390 | 54.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B630 | 64.5% | Reimplemented, asm differs From 63a362cdd7cb9fed86f9935c06a3dc8ee305ed34 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 22:07:25 +0200 Subject: [PATCH 057/202] reimplement: createTribe, and a deer tribe that walks a rally array it has not got createTribe 30.3 -> 67.2%: the slot index is only copied out of the loop counter once the slot is known free, and the tribe's address is taken before the clear and reused for every store after it. processDeerMoving 44.2 -> 47.4%: the block at 0x52b064 ends in `ret 4`, so a tribe with no rally points stops after asking for one; and the halving at 0x52adfe is `cdq`/`sar`, so the size is signed. moveUnitToBehaviorTarget 27.1 -> 33.3%: the tribe is reached through `this`, which the original keeps in a stack slot across the dispatch. updateTribeUnitAssignments 58.4 -> 57.0%: the size is compared with a signed `jle` on 16 bits, which the `(ushort)` cast had turned into a test only zero could pass. Worth the 1.4 points. Also applyUnitTopSpeedDelayBasedOnTribeSize 45.1 -> 46.2% and the size pointer in isTribePathToDestinationClear (neutral). Verified against the real jump tables that updateTribeCombatStanceBehavior's and playUnitCombatSpeechForTarget's case groups are already correct. Namespace average 81.25% -> 81.66%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...applyUnitTopSpeedDelayBasedOnTribeSize.cpp | 7 ++++++- .../Map/Units/TribesState/createTribe.cpp | 12 ++++++++--- .../isTribePathToDestinationClear.cpp | 8 +++++++- .../TribesState/moveUnitToBehaviorTarget.cpp | 11 +++++++--- .../Units/TribesState/processDeerMoving.cpp | 11 ++++++++-- .../updateTribeUnitAssignments.cpp | 20 +++++++++++++------ status/addresses-SHC-3BB0A8C1.txt | 10 +++++----- 7 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp index 37749989f4..52eeeae6df 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp @@ -84,7 +84,12 @@ namespace Map { */ _foundUnsortedEntry = 0; int _pairIndex = 0; - if (this->someUnitIDArrayCount == 1 || this->someUnitIDArrayCount + -1 < 0) { + /* + One test, not two: the original subtracts one from the count and jumps on + the sign-or-zero (`sub edx, 1; test edx, edx; jle`), which the decompiler + split into an equality and a sign test. + */ + if (this->someUnitIDArrayCount - 1 <= 0) { break; } /* diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index cb6f65c3c7..8e4d806507 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -22,10 +22,17 @@ namespace Map { { DAT_CurrentTribeID::instance = 1; do { - int _tribeID = DAT_CurrentTribeID::instance; if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0) { + /* + The index is only copied out of the loop counter once the slot is + known free (`mov edi, eax` at 0x5228c9), and the tribe's address is + taken before the clear (`lea esi, [edx + ebp]`, `lea eax, [esi + + 0x28]`), then reused for every store below it. + */ + int const _tribeID = DAT_CurrentTribeID::instance; + Tribe& _tribe = this->tribes[_tribeID]; MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - sizeof(Tribe), '\0', &this->tribes[DAT_CurrentTribeID::instance]); + sizeof(Tribe), '\0', &_tribe); /* An early return, which is where the original puts its `return 0`: `cmp edi, ebx; jg` falls straight into `xor eax, eax` with the claim @@ -35,7 +42,6 @@ namespace Map { return 0; } { - Tribe& _tribe = this->tribes[_tribeID]; if ((playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) && (setAsCurrentTribeID)) { this->DAT_CurrentTribeID = _tribeID; diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index ea16d9bcd7..ad0f7d584c 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -51,10 +51,16 @@ namespace Map { int const _searchMode = (DAT_TileMapState::instance.LogicLayer[_toTile] & 0x100) != 0 ? 1 : 2; MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, DAT_PathFindingState::ptr)(toX, toY, -1, -1, 500, FALSE); + /* + The size lives behind a pointer the original takes once, after the search is + seeded (`lea edx, [ecx + edi + 0x5c]` at 0x524d6e, kept in a stack slot and + re-read at the top of every iteration). + */ + short const* const _size = &this->tribes[tribeID].size; while (true) { int _unitID; do { - if (_unitIndex >= this->tribes[tribeID].size) { + if (_unitIndex >= *_size) { return (undefined4)(1); } _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index 6cedfa801f..12bb3cf09f 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -76,7 +76,7 @@ namespace Map { Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)(unitID); if (!iVar4) goto LAB_0052143b; - if (DAT_TribesState::instance.tribes[sVar1].selectionTargetUnitID == unitID) { + if (this->tribes[sVar1].selectionTargetUnitID == unitID) { DAT_TribesState::instance.tribes[sVar1].tribeBehaviorType = Map::Units::STBT_0x3f2; } local_c = DAT_TroopValueState::instance.y; @@ -131,9 +131,14 @@ namespace Map { sVar5 = (short)uVar6 - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar5].addXgetTile; DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; DAT_UnitsState::instance.units[unitID].attackAtTileX = sVar5; + /* + Reached through `this`, which the original keeps in a stack slot + ([esp + 0x18]) across the whole dispatch; only the behaviour-type store in the + 0x3fb arm goes through the global's absolute address. + */ if (sVar2 == unitID) { - DAT_TribesState::instance.tribes[sVar1].targetX = sVar5; - DAT_TribesState::instance.tribes[sVar1].targetY = DAT_UnitsState::instance.units[unitID].attackAtTileY; + this->tribes[sVar1].targetX = sVar5; + this->tribes[sVar1].targetY = DAT_UnitsState::instance.units[unitID].attackAtTileY; } iVar4 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(unitID); if (!iVar4) { diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index 095b2f9391..916717542d 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -49,7 +49,11 @@ namespace Map { int const _predatorCount = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount + DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; - sVar2 = (ushort)this->tribes[tribeID].size; + /* + Signed: the halving at 0x52adfe is `movsx`, `cdq`, `sub`, `sar 1`, which is + what a signed `/ 2` compiles to. The `(ushort)` cast turned it into a `shr`. + */ + sVar2 = (int)this->tribes[tribeID].size; this->tribes[tribeID].unkIsAnimalTribe = 1; /* The trim is the arm the original places first: `cmp cx, 0x1e; jle` falls into @@ -110,11 +114,14 @@ namespace Map { = (4 - ((byte)SEC_RNG::instance.currentNumber2 & 3)) * 200; /* Compared against the memory operand, not through a local: the - original has `cmp word ptr [..], 0; jg`. + original has `cmp word ptr [..], 0; jg`. The block it falls + into at 0x52b064 ends in `ret 4`, so a tribe with no rally + points stops here instead of walking the array it has not got. */ if (this->tribes[tribeID].rallyPointCount <= 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 5, 0); + return; } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); sVar2 = this->tribes[tribeID].currentRallyPointIndex; diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp index 0ab67a5e93..11e765509b 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp @@ -31,18 +31,26 @@ namespace Map { */ for (DAT_CurrentTribeID::instance = 1; (int)DAT_CurrentTribeID::instance < 1250; DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1) { - int _state = (ushort)this->tribes[DAT_CurrentTribeID::instance].tribeState; + /* + The state, the size and the time are all compared as 16-bit values + (`movzx ecx, word ptr`, then `cmp cx, bx` / `cmp cx, 3` / `jle`), so they + stay `short` here; the earlier `(ushort)` casts turned the signed `jle` on + the size into a test only zero could pass. It costs 1.4 points + (58.4% -> 57.0%), which is the price of being right about negative sizes. + */ + short const _state = this->tribes[DAT_CurrentTribeID::instance].tribeState; int _tribeUnitIndex = 0; if (_state) { int _tribeID = DAT_CurrentTribeID::instance; if (_state != 3) { - int _tribeSize = (ushort)this->tribes[DAT_CurrentTribeID::instance].size; - if (_tribeSize <= 0) { - int _tribeTime = this->tribes[DAT_CurrentTribeID::instance].time; + short const _size = this->tribes[DAT_CurrentTribeID::instance].size; + if (_size <= 0) { + short const _tribeTime = this->tribes[DAT_CurrentTribeID::instance].time; if (_tribeTime && (int)DAT_GameCore::instance.mapTimeInTicks <= (int)_tribeTime && _tribeTime) continue; } else { + int const _tribeSize = (int)_size; int local_4 = 0; if (0 < _tribeSize) { do { @@ -50,8 +58,8 @@ namespace Map { this)(_tribeID, _tribeUnitIndex); int _unitLogicalState = DAT_UnitsState::instance.units[_unitID].logicalState; _tribeUnitIndex = _tribeUnitIndex + 1; - if ((short)_unitLogicalState < 1 - || (2 < (short)_unitLogicalState + if (_unitLogicalState < 1 + || (2 < _unitLogicalState && (_unitLogicalState != Map::Units::ULS_TRANSITIONING)) || DAT_UnitsState::instance.units[_unitID].dying != 0 || DAT_UnitsState::instance.units[_unitID].tribeID != _tribeID) { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index daac9a3c67..7e9288b2bd 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46387,7 +46387,7 @@ SHC_3BB0A8C1_0x00521210 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00521280 | 27.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521280 | 33.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521500 | 31.1% | Reimplemented, asm differs @@ -46799,7 +46799,7 @@ SHC_3BB0A8C1_0x00522720 | 60.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x005227E0 | 75.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522890 | 30.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522890 | 67.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented @@ -46913,7 +46913,7 @@ SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending SHC_3BB0A8C1_0x00524E20 | 93.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524EF0 | 45.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524EF0 | 46.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525060 | 0.0% | Pending @@ -46971,7 +46971,7 @@ SHC_3BB0A8C1_0x00526F00 | 42.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x005270F0 | 42.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527330 | 58.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527330 | 57.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527440 | 59.6% | Reimplemented, asm differs @@ -49141,7 +49141,7 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 44.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052AD50 | 47.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B110 | 62.9% | Reimplemented, asm differs From 06232a2e8afb1cdda9a185760700ae77e90b05e9 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 22:18:02 +0200 Subject: [PATCH 058/202] reimplement: a stance the original keeps, and a tribe shuffle that read 128 bytes low snapshotSelectionTribeAndComputeStance 47.1 -> 55.8%: the copy block at 0x522d42 ends in its own `ret 4`, so a selection that is all one tribe keeps the stance it just copied instead of having the majority written over it; that is also why the three counts are only loaded at 0x522d6a. upgradeTribeArrayLayoutForMapVersion: the source cursor starts at tribes[944] + 0x1ac, which is index 0xba into unitSelectionBitMasked, not 0x7a. Every tribe the loop moved came from 128 bytes too low. No match change either way. getAvailableTribeID 60.4 -> 62.3%: the global is stored once before each loop, and the 400 is added to the full tick count before being truncated. Measured and reverted, each recorded at the function: binding giveTribeMoveInstruction's 21 subscripts to a `Tribe&` costs 1.1 points and hoisting trimTribeToSize's size pointer costs 1.2, both against explicit stack-slot evidence in the original. Namespace average 81.66% -> 81.75%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/getAvailableTribeID.cpp | 14 ++++++++++---- .../Units/TribesState/getSpecificUnitFromTribe.cpp | 9 +++++++-- .../Units/TribesState/getUnitIDForIndexInTribe.cpp | 9 +++++++-- .../Units/TribesState/giveTribeMoveInstruction.cpp | 6 ++++++ .../TribesState/moveTribeToIndexedNearbyTile.cpp | 8 +++++++- .../snapshotSelectionTribeAndComputeStance.cpp | 13 ++++++++++--- .../Map/Units/TribesState/trimTribeToSize.cpp | 8 +++++++- .../upgradeTribeArrayLayoutForMapVersion.cpp | 8 +++++++- status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 9 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index d609b4a18e..8adb1aa94a 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -23,6 +23,12 @@ namespace Map { int _tribeID; if (DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) { _tribeID = 1249; + /* + Stored once before the loop (0x52272e) and then only after each + decrement; the decompiler's repeat of the store after the loop is not in + the instruction stream. + */ + DAT_CurrentTribeID::instance = _tribeID; while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { _tribeID = _tribeID + -1; DAT_CurrentTribeID::instance = _tribeID; @@ -30,7 +36,6 @@ namespace Map { return 0; } } - DAT_CurrentTribeID::instance = _tribeID; if (_tribeID <= 0) { return 0; } @@ -52,14 +57,15 @@ namespace Map { return 0; } } - DAT_CurrentTribeID::instance = _tribeID; if (_tribeID <= 0) { return 0; } /* - claim it + claim it. The 400 is added to the full tick count and only then truncated + (`mov edx, [..]`, `add edx, 0x190`, `mov word ptr [..], dx`), not to a value + already cut down to a short. */ - this->tribes[_tribeID].time = (short)DAT_GameCore::instance.mapTimeInTicks + 400; + this->tribes[_tribeID].time = (short)(DAT_GameCore::instance.mapTimeInTicks + 400); return _tribeID; } diff --git a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp index 37f01112a9..f55f06d3fd 100644 --- a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp @@ -25,9 +25,14 @@ namespace Map { if (iVar1 < 200) { psVar3 = this->tribes[tribeID].unitSelectionBitMasked + iVar1; do { - if ((*psVar3 != 0) && (iVar2 < 0x10)) { + /* + The word is read once: `movzx edx, word ptr [esi]` for the zero test, + then `movsx edx, dx` for the bit tests, rather than a load per bit. + */ + short const _bits = *psVar3; + if ((_bits != 0) && (iVar2 < 0x10)) { do { - if ((int)*psVar3 & 1 << iVar2) { + if ((int)_bits & 1 << iVar2) { DAT_00ee0fb4::instance = iVar2 + 1; if (iVar2 + 1 != 0x10) { DAT_Tribe_HighestID::instance = iVar1; diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp index ef007590f9..ef5192be2b 100644 --- a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -13,9 +13,14 @@ namespace Map { if ((this->tribes[tribeID].tribeState == 2) && (unitSelectionIndex < this->tribes[tribeID].size)) { short* psVar3 = this->tribes[tribeID].unitSelectionBitMasked; do { - if (*psVar3 != 0) { + /* + The word is read once: `movzx edx, word ptr [esi]` for the zero test, + then `movsx edx, dx` for the bit tests, rather than a load per bit. + */ + short const _bits = *psVar3; + if (_bits != 0) { for (int iVar2 = 0; iVar2 < 16; iVar2++) { - if ((int)*psVar3 & 1 << iVar2) { + if ((int)_bits & 1 << iVar2) { if (_counter == unitSelectionIndex) { return iVar1 * 0x10 + iVar2; } diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index b45f8d8c3d..6312e2136d 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -96,6 +96,12 @@ namespace Map { if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0) { return (undefined4)(0); } + /* + The subscript is repeated rather than bound to a `Tribe&`, although the + original does take the address once (`lea eax, [edx + ecx]` at 0x526417, kept + in a stack slot) and read every later field through it: binding all 21 uses to + a reference costs 1.1 points (30.0% -> 28.9%, norm 65.2% -> 21.8%). + */ int _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; int _targetUnitX = (int)DAT_UnitsState::instance.units[_targetUnitID].x; int _targetUnitY = (int)DAT_UnitsState::instance.units[_targetUnitID].y; diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index a260a644c8..c8cae92066 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -25,11 +25,17 @@ namespace Map { uint uVar4 = (uint)DAT_UnitsState::instance.units[sVar1].y; uVar7 = (uint)DAT_UnitsState::instance.units[sVar1].x; int iVar2 = (int)this->tribes[param_1].someIndex; + /* + One division: the original's `cdq`/`idiv` leaves the quotient in EAX and the + remainder in EDX, and the unreachable arm returns that same EAX rather than + dividing again. + */ + int const _quotient = iVar2 / 40; int iVar5 = iVar2 % 40; if (iVar5 <= 0) { iVar5 = 0; } else if (39 < iVar5) { - return iVar2 / 40; + return _quotient; } piVar6 = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[iVar5].y; do { diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index 9d6fada89b..fbbd6ee0ed 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -54,17 +54,24 @@ namespace Map { _pUnit = _pUnit + 1; _unitIDCountdown = _unitIDCountdown + -1; } while (_unitIDCountdown); - _countAggressive = _stanceCount[2]; - _countDefensive = _stanceCount[1]; - _countStandground = _stanceCount[0]; if (_lastTribeID != -1 && !_atLeastTwoTribes) { /* A whole-struct assignment, which is the `rep movsd` of 0xcd dwords the original has; a hand-written int-by-int copy compiles to a loop. + + And then it returns: the block at 0x522d42 ends in its own `ret 4`, so + a selection that is all one tribe keeps the stance it just copied + instead of having the majority written over it. That is also why the + three counts are only loaded out of the array at 0x522d6a, on the path + where the copy did not happen. */ this->tribes[0] = this->tribes[_lastTribeID]; this->tribeCopiedToSlot0 = 1; + return; } + _countAggressive = _stanceCount[2]; + _countDefensive = _stanceCount[1]; + _countStandground = _stanceCount[0]; } if (_countAggressive + _countDefensive + _countStandground == 0) { this->tribes[0].unitStance = Map::Units::Behavior::USE_STAND_GROUND; diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 94dbb5f469..874e3e7190 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -22,8 +22,14 @@ namespace Map { int _currentTribeID; _currentTribeID = DAT_CurrentTribeID::instance; unitSelectionIndex = 0; + /* + The size stays a repeated subscript. The original does read it through a base + it takes once (`lea ebp, [eax + edi]` at 0x522f0b), but hoisting a pointer for + it costs 1.2 points (54.1% -> 52.9%). + */ if (this->tribes[DAT_CurrentTribeID::instance].tribeState != 0 - && (iVar3 = (int)this->tribes[DAT_CurrentTribeID::instance].size, iVar3 >= smallerSize) && 0 < iVar3) { + && (iVar3 = (int)this->tribes[DAT_CurrentTribeID::instance].size, iVar3 >= smallerSize) + && 0 < iVar3) { do { iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( _currentTribeID, unitSelectionIndex); diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index fb9251c64f..6b0f97bcb9 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -28,7 +28,13 @@ namespace Map { } iVar2 = 1249; _Dst = this->tribes + 0x4e1; - _Src = this->tribes[944].unitSelectionBitMasked + 0x7a; + /* + The source cursor starts at `this + 0xbd114`, which is tribes[944] + 0x1ac: + `unitSelectionBitMasked` sits at 0x38, so the index is (0x1ac - 0x38) / 2 = + 0xba. The import's 0x7a put it 128 bytes short, and every tribe this loop + moved came from the wrong place. + */ + _Src = this->tribes[944].unitSelectionBitMasked + 0xba; do { if (_Dst != (Tribe*)_Src) { memmove(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 7e9288b2bd..d194cf4a44 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46795,7 +46795,7 @@ SHC_3BB0A8C1_0x00522590 | 86.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522630 | 79.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522720 | 60.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522720 | 62.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x005227E0 | 75.2% | Reimplemented, asm differs @@ -46815,7 +46815,7 @@ SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522CA0 | 47.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522CA0 | 55.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522DD0 | 94.5% | Reimplemented, asm differs From a631596ec4f7b7892052ea42cf226f1e6a60e17d Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 22:26:19 +0200 Subject: [PATCH 059/202] reimplement: four more dropped returns in the camel and lion updaters updateCamelTribeBehavior 64.5 -> 70.8%: the spawn-success block at 0x52b69c and the rally block at 0x52b794 each end in their own `ret 4`, so the reads below them never saw what they wrote. And the map-editor branch at 0x52b704 is the only path that zeroes unknownCounter01 - the import zeroed it on every call, which wiped the counter the 0x65 and 0x1f40 tests at the top of the function read. updateLionWolfTribeBehavior 62.9 -> 66.8%: the same two blocks, at 0x52b1ab and 0x52b28c. updateRabbitTribeBehavior 54.0 -> 58.9%: the rally-point count is compared with a signed `jle` and indexed through a `movsx`, and the marsh-tile arm of the wander interval comes first. Namespace average 81.75% -> 81.93%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/updateCamelTribeBehavior.cpp | 23 +++++++++++++++++-- .../updateLionWolfTribeBehavior.cpp | 13 ++++++++++- .../TribesState/updateRabbitTribeBehavior.cpp | 22 +++++++++++------- status/addresses-SHC-3BB0A8C1.txt | 6 ++--- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index 855b682ace..b92fc53e13 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -40,8 +40,13 @@ namespace Map { && (iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trySpawnAdditionalWildlifeForTribe, this)( param_1, 1000, 10, 0x2f), iVar4 != 0)) { + /* + The original returns here too: the block at 0x52b69c ends in `ret 4`, so + the read of initialMoveOrderGiven below never sees the 1 just written. + */ this->tribes[param_1].initialMoveOrderGiven = 1; this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + return; } sVar5 = this->tribes[param_1].initialMoveOrderGiven; if (!sVar5) { @@ -59,7 +64,16 @@ namespace Map { this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; return; } - if (DAT_GameCore::instance.currentMenuViewType != UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + /* + The map-editor branch at 0x52b704 ends in `ret 4` and is the only path that + zeroes the counter; the import had it zeroed on every call, which wiped the + counter the 0x65 and 0x1f40 tests at the top of the function read. + */ + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[param_1].unknownCounter01 = 0; + return; + } + { if (sVar5 == 1) { this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; this->tribes[param_1].initialMoveOrderGiven = 2; @@ -76,7 +90,13 @@ namespace Map { this->tribes[param_1].rallyPointCount = 0; this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; } + /* + And this block returns as well: 0x52b794 stores the behaviour type a + second time and falls into its own `ret 4`, so the dispatch below is + not reached on this path. + */ this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + return; } SVar2 = this->tribes[param_1].tribeBehaviorType; if (SVar2 == (SomeTribeBehaviorType)0) { @@ -110,7 +130,6 @@ namespace Map { } } } - this->tribes[param_1].unknownCounter01 = 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index 6559677a2f..f2c00de9fd 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -45,8 +45,13 @@ namespace Map { && (BVar6 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( param_1, 500, Map::Units::UT_LIONSHWOLF), BVar6)) { + /* + This block returns too: 0x52b1ab ends in `ret 4`, so the read of + initialMoveOrderGiven below never sees the 1 just written. + */ this->tribes[param_1].initialMoveOrderGiven = 1; this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + return; } sVar2 = this->tribes[param_1].initialMoveOrderGiven; if (!sVar2) { @@ -81,7 +86,13 @@ namespace Map { this->tribes[param_1].rallyPointCount = 0; this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; } + /* + And this one returns: 0x52b28c stores the behaviour type a second time + and falls into its own `ret 4`, so neither the map-editor test nor the + dispatch below is reached on this path. + */ this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + return; } /* The original returns from this one: the block at 0x52b297 ends in `ret 4` @@ -106,7 +117,7 @@ namespace Map { } } else if (sVar2 == 0x96) { if ((this->tribes[param_1].unknownBool02 == 0) - && (sVar2 = (ushort)this->tribes[param_1].rallyPointCount, 0 < sVar2)) { + && (sVar2 = (int)this->tribes[param_1].rallyPointCount, 0 < sVar2)) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( param_1, (uint)((int)this->tribes[param_1].rallyPointArray[sVar2 + -1][0]), diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 7005a2a762..40016f92c6 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -93,7 +93,11 @@ namespace Map { tribeID); } } else if (sVar2 == 0x96) { - sVar2 = (ushort)this->tribes[tribeID].rallyPointCount; + /* + Signed: `cmp ax, dx` with a `jle`, and the index that follows is + `movsx ax`, so a negative count does not wrap into a huge index. + */ + sVar2 = (int)this->tribes[tribeID].rallyPointCount; if (0 < sVar2) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( @@ -105,14 +109,16 @@ namespace Map { this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } } else if (SVar3 == Map::Units::STBT_1) { - if (!iVar6) { - if (iVar5 < 0x15) { - this->tribes[tribeID].wanderIntervalTicks = 400; - } else { - this->tribes[tribeID].wanderIntervalTicks = 0x14; - } - } else { + /* + The marsh-tile arm comes first: `cmp ebp, edx; jz` falls into the + 0x14 store and jumps away to the rocky-tile test. + */ + if (iVar6) { this->tribes[tribeID].wanderIntervalTicks = 0x14; + } else if (0x14 < iVar5) { + this->tribes[tribeID].wanderIntervalTicks = 0x14; + } else { + this->tribes[tribeID].wanderIntervalTicks = 400; } psVar1 = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; *psVar1 = *psVar1 + 1; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index d194cf4a44..6558d10145 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49143,11 +49143,11 @@ SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD50 | 47.4% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 62.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B110 | 66.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B390 | 54.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B390 | 58.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B630 | 64.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B630 | 70.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs From a0c0a3fd028010a74739e5088f233c3b2b3b732e Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 22:38:55 +0200 Subject: [PATCH 060/202] reimplement: snapshotSelectionTribeAndComputeStance 55.8 -> 85.7% The three counts are read straight out of _stanceCount[], which is where the original's own three slots are: it loads [esp+0x18], [esp+0x14] and [esp+0x10] into registers at 0x522d6a and nowhere else. The copies cost a fourth slot that diff_slots.py showed the original does not have. The earlier measurement that said otherwise was taken before the copy path got its return. Also verified all four of spawnQueuedReinforcementWaves' jump tables against the binary and recorded the result at the two 18-entry switches: the asymmetric laddermen arm is already on the right one. Which switch is which is settled by the call each feeds - 0x527c34's reaches createTribeWithSpawnedUnit and 0x527bec's reaches spawnUnitsIntoNewTribe - and moving the arm the other way costs 17.1 points. Namespace average 81.93% -> 82.15%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...snapshotSelectionTribeAndComputeStance.cpp | 23 ++++++++----------- .../spawnQueuedReinforcementWaves.cpp | 17 ++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index fbbd6ee0ed..06a4653fec 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -22,14 +22,6 @@ namespace Map { _stanceCount[0] = 0; _stanceCount[1] = 0; _stanceCount[2] = 0; - /* - Three locals rather than reading _stanceCount[] again at the end: the extra - stack slot costs nothing and reading the array directly measures worse - (quick_diff 0.5806 -> 0.4725, and it moves `this` out of ebx). - */ - int _countAggressive = 0; - int _countDefensive = 0; - int _countStandground = 0; int _lastTribeID = -1; if (1 < DAT_UnitsState::instance.maxUnitCount) { Unit* _pUnit = &DAT_UnitsState::instance.units[1]; @@ -69,16 +61,19 @@ namespace Map { this->tribeCopiedToSlot0 = 1; return; } - _countAggressive = _stanceCount[2]; - _countDefensive = _stanceCount[1]; - _countStandground = _stanceCount[0]; } - if (_countAggressive + _countDefensive + _countStandground == 0) { + /* + The three counts are read straight out of the array, which is where the + original's own three slots are: it loads [esp+0x18], [esp+0x14] and + [esp+0x10] into registers at 0x522d6a and nowhere else, and diff_slots.py + shows our extra fourth slot is a spill of a redundant copy. + */ + if (_stanceCount[2] + _stanceCount[1] + _stanceCount[0] == 0) { this->tribes[0].unitStance = Map::Units::Behavior::USE_STAND_GROUND; return; } - if (_countAggressive < _countDefensive || _countAggressive < _countStandground) { - this->tribes[0].unitStance = (ushort)(_countStandground <= _countDefensive); + if (_stanceCount[2] < _stanceCount[1] || _stanceCount[2] < _stanceCount[0]) { + this->tribes[0].unitStance = (ushort)(_stanceCount[0] <= _stanceCount[1]); return; } this->tribes[0].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp index 1d4dce457a..a2a7d84381 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -129,6 +129,17 @@ namespace Map { _tribeType = 10; _unitType = Map::Units::UT_E_KNIGHT; break; + /* + This switch is the one whose table is 0x527c34, where entry 7 + is the laddermen arm at 0x527a43 - the arm that sets 4 and + 0x1d. The other 18-entry table, 0x527bec, sends entry 7 + straight to the merge point, which is why the fourth switch + below has no case 7. Which switch is which is settled by the + call each one feeds: 0x527c34's reaches + createTribeWithSpawnedUnit (0x523190) and 0x527bec's reaches + spawnUnitsIntoNewTribe (0x523030). Moving the arm the other + way costs 17.1 points (59.6% -> 42.5%). + */ case 7: _tribeType = 4; _unitType = Map::Units::UT_E_LADDER; @@ -414,6 +425,12 @@ namespace Map { _tribeType = 10; _unitType = Map::Units::UT_E_KNIGHT; break; + /* + No case 7: entry 7 of this switch's table, + 0x527bec, is the merge point 0x5278a6, so a + laddermen wave here reuses whatever the previous + iteration left in the two variables. + */ case 8: _tribeType = 0xb; _unitType = Map::Units::UT_E_ENGINEER; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6558d10145..81023a361c 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46815,7 +46815,7 @@ SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522CA0 | 55.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522CA0 | 85.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522DD0 | 94.5% | Reimplemented, asm differs From 7da540b5e3bfe43361e19b848283b023c859f428 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 22:51:19 +0200 Subject: [PATCH 061/202] reimplement: the archer arm's dropped exit and its lost second play playUnitCombatSpeechForTarget 24.9 -> 29.6%: the import left `if (type != UT_A_HARCHER) {}` with an empty body, where the original jumps to the epilogue, so every archer type ran on into the horse-archer effects; and the arm's closing `_otherX = 0x59` was a second playSFXAtLocationFullVolume, with the coordinates read again, followed by `ret 8`. Recorded at giveTribeAnInstruction: the `extraout_var` reassembly there is a decompiler artifact - 0x421be0 returns 0, 1 or 2 in the whole of EAX - but our header declares the callee `char`, so testing it plainly tests only AL and costs 1.7 points. Fixing it properly means an `int` return on BuildingsState::resolveBuildingEntryAccessibility, outside this namespace. Namespace average 82.15% -> 82.19%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 10 ++++++++++ .../playUnitCombatSpeechForTarget.cpp | 20 ++++++++++++++++--- status/addresses-SHC-3BB0A8C1.txt | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 6ac9c68e45..4909806ba0 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -812,6 +812,16 @@ namespace Map { Map::Buildings::BuildingsState_Func::resolveBuildingEntryAccessibility, DAT_BuildingsState::ptr)(iVar11, 1, (int)(DAT_UnitsState::instance.units[uVar13].x), (int)(DAT_UnitsState::instance.units[uVar13].y)); + /* + The `extraout_var` reassembly of the upper three bytes stays, + although it is a decompiler artifact: 0x421be0 really returns + 0, 1 or 2 in the whole of EAX (0x421d4f sets ECX to 1 or 2), + and the original's test is `test eax, eax`. Our header + declares the callee `char`, so a plain `cVar7 != 0` tests only + AL and costs 1.7 points (27.9% -> 26.2%). Fixing it properly + means giving BuildingsState::resolveBuildingEntryAccessibility + an `int` return, which is outside this namespace. + */ if (((((uint)(extraout_var) & 0xffffff) << 8) | (uint)(byte)(cVar7)) != 0 && (iVar14 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: calculateCanPlayerUnitsNavigateToAreaFromArea, diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index 8e44b2c55f..81f8ddd465 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -84,7 +84,14 @@ namespace Map { } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, _actionID); - if (_ptrptrCurrentSelectionID != Map::Units::UT_A_HARCHER) {} + /* + The import left this guard with an empty body. The original jumps to the + common epilogue (`cmp dword ptr [esp + 0x18], 0x4a; jnz 0x521e48`), so + only a horse archer reaches the two plays below. + */ + if (_ptrptrCurrentSelectionID != Map::Units::UT_A_HARCHER) { + return; + } if (_ptrCurrentSelectionID == (TribesState*)0x1) { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; @@ -100,8 +107,15 @@ namespace Map { } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( _otherX, _otherY, sfxOffsetInArray); - _otherX = 0x59; - break; + /* + A second play, of 0x59, with the coordinates read again (0x521c0b, + 0x521c12) - which is what the decompiler turned into an assignment of + 0x59 to one of the coordinate locals - and then `ret 8`. + */ + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)DAT_UnitsState::instance.units[_otherUnitID].y, 0x59); + return; case Map::Units::UT_E_SPEAR: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 81023a361c..1f57d00617 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 24.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 29.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending From 9720ae0a28701642277738db4c9b5339df7a506f Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 23:00:27 +0200 Subject: [PATCH 062/202] measure: the Tribe& that made createTribe match ruins its twin createTribeForPlayer does the same thing as createTribe and the original builds it the same way - the tribe's address taken once before the clear and reused for every store - but writing that as a `Tribe&` costs 37.3 points here (75.2% -> 37.9%) where it gained 36.9 there. Recorded at the function; no code change. Namespace average unchanged at 82.19%, 56 of 113 at 100%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index abc10f3283..966ddafbe8 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -19,6 +19,13 @@ namespace Map { The inner slot-ID guard stays a positive `if` around the claim: inverting it to an early return, which took spawnUnitsIntoNewTribe from 89.4% to 98.3%, costs 2.0 points here (75.2% -> 73.2%). + + The repeated subscript stays too. The original does take the tribe's address once + before the clear (`lea esi, [edx + ecx]`, `lea eax, [esi + 0x28]` at 0x522823) and + reach every store through it, and writing that as a `Tribe&` is what took the twin + createTribe from 30.3% to 67.2% - but here the identical change costs 37.3 points + (75.2% -> 37.9%). The two functions differ only in how they find the slot, so the + shape that matches one does not match the other. */ // FUNCTION: STRONGHOLDCRUSADER 0x005227E0 int TribesState::createTribeForPlayer(int playerID) From 8fa40c42b9292d77d1619f40deb28d30bbf65ee5 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 23:06:07 +0200 Subject: [PATCH 063/202] reimplement: processDeerMoving 47.4 -> 50.7%, from the reccmp diff Three scheduling differences at the head of the function, each read off the asm diff: both unit coordinates are loaded before either reciprocal multiply; the predator sum's second term is read after the marsh tiles, not with the first (0x1c, 0x00, 0x18, then `add ebp, [..+0x20]`); and the tribe size is a `short` compared as one, `movzx` plus `cmp cx, 0x1e`, with the sign extension deferred to the halving. Namespace average 82.19% -> 82.22%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/processDeerMoving.cpp | 35 +++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index 916717542d..f03978640e 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -38,34 +38,41 @@ namespace Map { int _x10; int _y10; sVar2 = this->tribes[tribeID].selectionTargetUnitID; - _x10 = (int)DAT_UnitsState::instance.units[sVar2].x / 10; - _y10 = (int)DAT_UnitsState::instance.units[sVar2].y / 10; - iVar8 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; /* - One local for the sum, not two for the terms: the original adds them as it - reads (`mov ebp, [eax + 0x28]` then `add ebp, [eax + 0x48]`) and keeps only - the result, which is two stack slots fewer than diff_slots.py reports for us. + Both coordinates are read before either division: the original has + `movsx edx, [+0x610]`, `movsx ecx, [+0x612]` and only then the two + reciprocal multiplies. + */ + short const _unitX = DAT_UnitsState::instance.units[sVar2].x; + short const _unitY = DAT_UnitsState::instance.units[sVar2].y; + _x10 = (int)_unitX / 10; + _y10 = (int)_unitY / 10; + /* + The predator sum's second term is read after the marsh tiles, not with the + first: the original reads 0x1c, then 0x00, then 0x18, and only then + `add ebp, [.. + 0x20]`. */ - int const _predatorCount = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount - + DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; + iVar8 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; + int _predatorCount = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount; iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; + _predatorCount = _predatorCount + DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; /* - Signed: the halving at 0x52adfe is `movsx`, `cdq`, `sub`, `sar 1`, which is - what a signed `/ 2` compiles to. The `(ushort)` cast turned it into a `shr`. + A `short`, compared as one: `movzx ecx, word ptr [esi + 0x5c]` then + `cmp cx, 0x1e`, with the sign extension deferred to the halving. */ - sVar2 = (int)this->tribes[tribeID].size; + short const _size = this->tribes[tribeID].size; this->tribes[tribeID].unkIsAnimalTribe = 1; /* The trim is the arm the original places first: `cmp cx, 0x1e; jle` falls into it and jumps to the herd-size arm. */ - if (30 < sVar2) { + if (30 < _size) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trimTribeToSize, this)(tribeID, (int)(30)); this->tribes[tribeID].herdMinSize = 0xf; this->tribes[tribeID].herdMaxSize = 0x3c; } else { - this->tribes[tribeID].herdMinSize = sVar2 / 2; - this->tribes[tribeID].herdMaxSize = sVar2 * 2; + this->tribes[tribeID].herdMinSize = _size / 2; + this->tribes[tribeID].herdMaxSize = _size * 2; } if ((_predatorCount != 0) && (this->tribes[tribeID].predatorNearby == 0)) { this->tribes[tribeID].predatorNearby = 1; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 1f57d00617..de911a4dde 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49141,7 +49141,7 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 47.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052AD50 | 50.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B110 | 66.8% | Reimplemented, asm differs From 266cb702a1f5642d76c5c74a9aa2db5a59510288 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 23:12:13 +0200 Subject: [PATCH 064/202] reimplement: drawFlagsAndUnitDestinations 41.8 -> 47.0% Three nested tests rather than one `&&` chain, matching the original's three separate forward jumps; the player ID is loaded first and the tribe's owner compared to it; and the starting rally index is a branch, not a `setl`. computeTribePercentages: the unit index gets its own `int`, so it is reached with the single `movsx eax, word ptr [esi + 0x5a]` the original has rather than a `movzx` plus a `movsx`. Neutral on the score, kept for being right. Namespace average 82.22% -> 82.27%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/computeTribePercentages.cpp | 12 +++++++++--- .../drawFlagsAndUnitDestinations.cpp | 18 +++++++++++++++--- status/addresses-SHC-3BB0A8C1.txt | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 76e9b2cacf..91e58fd13b 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -27,7 +27,13 @@ namespace Map { { uint _unitID; BOOLEnum BVar3; - short _percentage = this->tribes[tribeID].selectionTargetUnitID; + /* + The unit index is its own `int`: the original reaches it with a single + `movsx eax, word ptr [esi + 0x5a]`, where sharing the `short` that later holds + the percentages costs a `movzx` plus a `movsx`. + */ + int const _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + short _percentage = 0; int iVar4 = 0; short sVar1 = this->tribes[tribeID].size; this->tribes[tribeID].percentageDyingUnk = 0; @@ -37,13 +43,13 @@ namespace Map { this->tribes[tribeID].percentageShootingUnk = 0; this->tribes[tribeID].field152_0x2a0 = 0; this->tribes[tribeID].percentageSomething = 0; - this->tribes[tribeID].movementSpeed = DAT_UnitsState::instance.units[_percentage].movementSpeed; + this->tribes[tribeID].movementSpeed = DAT_UnitsState::instance.units[_targetUnitID].movementSpeed; int local_8 = 0; int _countRangeUnk = 0; int _countAttackingUnk = 0; int _countMoving = 0; int _countDying = 0; - this->tribes[tribeID].unitType = DAT_UnitsState::instance.units[_percentage].unitType; + this->tribes[tribeID].unitType = DAT_UnitsState::instance.units[_targetUnitID].unitType; if (0 < sVar1) { do { _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)( diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index c4a4d7b955..edfc3f7d8a 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -36,9 +36,20 @@ namespace Map { int tile; short _isRallying; _isRallying = this->tribes[tribeID].isRallyingUnk; - if (_isRallying && this->tribes[tribeID].owner == DAT_GameSynchronyState::instance.currentPlayerSlotID - && (_rallyIndex = (uint)(_isRallying < 0), - (int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount)) { + /* + Three nested tests, not one chain: the original has three separate forward + jumps (0x523a53, 0x523a62, 0x523a7d). The player ID is loaded first and the + tribe's owner compared to it (`mov esi, [..]`, `cmp [ebp + 0x2c], esi`), and + the starting index is a branch rather than a `setl` + (`test ax, ax`, `jge`, `mov esi, 1`). + */ + if (_isRallying + && DAT_GameSynchronyState::instance.currentPlayerSlotID == this->tribes[tribeID].owner) { + _rallyIndex = 0; + if (_isRallying < 0) { + _rallyIndex = 1; + } + if ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount) { _pRallyPoints = this->tribes[tribeID].rallyPointArray + _rallyIndex; do { /* @@ -57,6 +68,7 @@ namespace Map { _rallyIndex = _rallyIndex + 1; _pRallyPoints = _pRallyPoints + 1; } while ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount); + } } int iVar5 = 0; if (0 < this->tribes[tribeID].size) { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index de911a4dde..3a701cab4e 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46861,7 +46861,7 @@ SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523920 | 90.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523A30 | 41.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523A30 | 47.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523B00 | 0.0% | Pending From 90900844e5438b719632391520d77794c5da3509 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 23:15:24 +0200 Subject: [PATCH 065/202] reimplement: one idiv in moveTribeToIndexedNearbyTile, not a reciprocal multiply Naming the quotient in a local of its own made MSVC compute the division by reciprocal multiply and derive the remainder from it, and cost the stack slot diff_slots.py had flagged. The original divides once - `cdq`, `mov ecx, 0x28`, `idiv ecx` - and the unreachable arm returns the quotient that instruction left in EAX. Score unchanged at 45.9%; the frame now matches. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/moveTribeToIndexedNearbyTile.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index c8cae92066..b7228e4d79 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -26,16 +26,16 @@ namespace Map { uVar7 = (uint)DAT_UnitsState::instance.units[sVar1].x; int iVar2 = (int)this->tribes[param_1].someIndex; /* - One division: the original's `cdq`/`idiv` leaves the quotient in EAX and the - remainder in EDX, and the unreachable arm returns that same EAX rather than - dividing again. + Only the remainder is named. The original divides once, with `cdq`, `mov ecx, + 0x28`, `idiv ecx`, and the unreachable arm returns the quotient that same + instruction left in EAX; naming the quotient in a local of its own turns the + division into a reciprocal multiply and costs a stack slot. */ - int const _quotient = iVar2 / 40; int iVar5 = iVar2 % 40; if (iVar5 <= 0) { iVar5 = 0; } else if (39 < iVar5) { - return _quotient; + return iVar2 / 40; } piVar6 = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[iVar5].y; do { From cee46c4ba967fab7e9371c7300b880edd2036122 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 23:44:38 +0200 Subject: [PATCH 066/202] reimplement: two more at 100%, from one movsx where we had movzx plus movsx scatterTribeUnitsRandomly 90.9 -> 100%: the tile's Y component is read once into an int and shared by the translationMatrix index and the targetY store, which is the single `movsx` the original has; reading the `short` array twice left a `movzx` and a `movsx`. moveTribeToNearbyClearTile 98.4 -> 100%: a cursor against the literal end address, as in the sibling moveTribeToIndexedNearbyTile. An index over the array makes the bound a relocated `&field1608_0xcc4[40]` where the original has a plain `cmp ebx, 0xb4df44`. addUnitToTribeAndUpdateTribeMovementSpeed 57.9 -> 67.8% and addUnitsToTribeAndComputeMovementSpeed 64.1 -> 73.5%: the three speed locals are `int`. The original reads one with a single `movsx` and compares and copies it 32 bits wide; `short` locals give a `movzx` and 16-bit `cmp cx, si`. Measured and reverted, each recorded at the function: the same widening costs giveTribeAnInstruction 0.8 points, because the decompiler folded several of the original's variables into that one local, and costs drawFlagsAndUnitDestinations 1.3 even when split down to just the three target-ID uses. Namespace average 82.27% -> 82.54%, 57 of 113 at 100%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/addUnitToTribe.cpp | 11 ++++++++--- ...addUnitToTribeAndUpdateTribeMovementSpeed.cpp | 13 ++++++++++--- .../addUnitsToTribeAndComputeMovementSpeed.cpp | 13 ++++++++++--- .../TribesState/drawFlagsAndUnitDestinations.cpp | 4 +++- .../Units/TribesState/giveTribeAnInstruction.cpp | 9 +++++++++ .../TribesState/moveTribeToNearbyClearTile.cpp | 16 ++++++++++++---- .../TribesState/scatterTribeUnitsRandomly.cpp | 13 ++++++++----- status/addresses-SHC-3BB0A8C1.txt | 8 ++++---- 8 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp index f8f5a440d6..70607ae76c 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -13,14 +13,19 @@ namespace Map { void TribesState::addUnitToTribe(uint unitID, int tribeID) { short* psVar1; - uint _mod16; this->tribes[tribeID].size = this->tribes[tribeID].size + 1; /* basically, it sets a bit flag for every unitID that is in the selection */ + /* + The remainder is not named, which is a wash on the score (86.7% either way). + The gap here is a register: the original builds the cursor in a scratch + register (`mov edx, edi`, `imul edx, edx, 0x19a`, `add edx, eax`, + `lea eax, [ecx + edx*2 + 0x60]`) where ours needs EBP and so a push and a pop, + and dropping the local was not enough to free one. + */ psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); - _mod16 = (int)unitID % 16; - *psVar1 = *psVar1 | DAT_UnitSelectionDefinedData::instance.BitMaskHelper[_mod16]; + *psVar1 = *psVar1 | DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]; if (this->tribes[tribeID].selectionTargetUnitID == 0) { this->tribes[tribeID].selectionTargetUnitID = (short)unitID; } diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp index 9fa480266b..835e1cb709 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp @@ -14,12 +14,19 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522B00 void TribesState::addUnitToTribeAndUpdateTribeMovementSpeed(int param_1, uint unitID, int tribeID) { + /* + The three speeds are `int`: the original reads one with + `movsx ecx, word ptr [eax]` and compares and copies it 32 bits wide + (`cmp ecx, esi`, `mov esi, ecx`), where `short` locals give a `movzx` and + 16-bit `cmp cx, si` / `movzx esi, cx`. The tribe size stays narrow - that one + really is `movzx` plus `test ax, ax`. + */ Unit* _ptrUnit; - short _maximumSpeed; - short _minimumSpeed; + int _maximumSpeed; + int _minimumSpeed; int _totalHealth; short _tribeSize; - short _movementSpeed; + int _movementSpeed; int _tribeID; _tribeID = tribeID; _maximumSpeed = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp index f2df725b80..1d93ea52d4 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp @@ -20,12 +20,19 @@ namespace Map { { Unit* _pUnit; uint _unitID; + /* + The three speeds are `int`, as in the sibling + addUnitToTribeAndUpdateTribeMovementSpeed, where the same change gained 9.9 + points: the original reads one with a single `movsx` and compares and copies + it 32 bits wide, where `short` locals give a `movzx` plus 16-bit compares. + The size and the target unit stay narrow. + */ int local_8; - short _movementSpeed; - short _maximumSpeed; + int _movementSpeed; + int _maximumSpeed; short _targetUnit; local_8 = 0; - short _minimumSpeed = 100; + int _minimumSpeed = 100; _maximumSpeed = 0; if (1 < DAT_UnitsState::instance.maxUnitCount) { _pUnit = &DAT_UnitsState::instance.units[1]; diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index edfc3f7d8a..14d90a18f5 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -23,7 +23,9 @@ namespace Map { /* The `short` local stays narrow: widening it costs 3.9 points - (41.8% -> 37.9%). + (41.8% -> 37.9%). Splitting it so that only the three target-ID uses get an `int` + - which is where our `movzx` plus `movsx` pairs are against the original's single + `movsx` - costs 1.3 points too (47.0% -> 45.7%). */ // FUNCTION: STRONGHOLDCRUSADER 0x00523A30 void TribesState::drawFlagsAndUnitDestinations(int tribeID) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 4909806ba0..cd53138f06 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -71,6 +71,15 @@ namespace Map { int _unitID_0x05_0x16; int iVar12; int _unitID; + /* + This one stays `short`, although nine of our `movzx` plus `movsx` pairs are + its `tileTranslationMatrix_YComponent` reads indexing `translationMatrix` - + the same shape that took scatterTribeUnitsRandomly to 100% and gained the two + movement-speed functions about ten points each. Widening it costs 0.8 points + (27.9% -> 27.1%): the decompiler folded several of the original's variables + into this one, so the change lands on all of them at once and the losses eat + the gains. + */ short sVar9; uint uVar13; int iVar14; diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp index d40ebe2ed6..583f79c470 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp @@ -29,9 +29,16 @@ namespace Map { candidate is reachable. 0xb4df44 is one past field1608_0xcc4[39].y, so the loop runs over all 40 entries. */ - for (int _candidate = 0; _candidate < 40; _candidate = _candidate + 1) { - uint const x1 = DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[_candidate].x + uVar5; - uint const y1 = DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[_candidate].y + uVar2; + /* + A cursor against the literal end address, as in the sibling + moveTribeToIndexedNearbyTile: an index over the array makes the bound a + relocated `&field1608_0xcc4[40]` where the original has a plain + `cmp ebx, 0xb4df44`. + */ + int* _pCandidate = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[0].y; + do { + uint const x1 = _pCandidate[-1] + uVar5; + uint const y1 = *_pCandidate + uVar2; if (x1 <= 399 && y1 <= 399 && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] != 0 && MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( @@ -41,7 +48,8 @@ namespace Map { param_1, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); return; } - } + _pCandidate = _pCandidate + 2; + } while ((int)_pCandidate < 0xb4df44); } } diff --git a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp index 2c345fb2da..2c473132e3 100644 --- a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp +++ b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp @@ -49,12 +49,15 @@ namespace Map { index = index + 1 + (DAT_UnitsState::instance.units[iVar2].fixedRng & 0xf); iVar3 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::getTileInSearchQueue, DAT_PathFindingState::ptr)(index); - iVar4 = DAT_ViewportRenderState::instance - .translationMatrix[DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[iVar3]] - .addXgetTile; - DAT_UnitsState::instance.units[iVar2].targetY + /* + One sign-extended read shared by both uses: the original has a + single `movsx ecx, word ptr [..]` and stores its low word, where + reading the `short` array twice leaves a `movzx` plus a `movsx`. + */ + int const _targetTileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[iVar3]; + iVar4 = DAT_ViewportRenderState::instance.translationMatrix[_targetTileY].addXgetTile; + DAT_UnitsState::instance.units[iVar2].targetY = (short)_targetTileY; DAT_UnitsState::instance.units[iVar2].targetX = (short)iVar3 - (short)iVar4; DAT_UnitsState::instance.units[iVar2].state.generic = ((UnitStateShort)0xd1); } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3a701cab4e..65aca3b8a5 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46803,9 +46803,9 @@ SHC_3BB0A8C1_0x00522890 | 67.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005229C0 | 64.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005229C0 | 73.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522B00 | 57.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522B00 | 67.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522BC0 | 100.0% | Reimplemented @@ -46859,7 +46859,7 @@ SHC_3BB0A8C1_0x00523790 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523920 | 90.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523920 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523A30 | 47.0% | Reimplemented, asm differs @@ -48875,7 +48875,7 @@ SHC_3BB0A8C1_0x0052A637 | 0.0% | Pending SHC_3BB0A8C1_0x0052A638 | 0.0% | Pending -SHC_3BB0A8C1_0x0052A640 | 98.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A640 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052A700 | 45.9% | Reimplemented, asm differs From 9881cdff36b560337e730ec7d75c9497e7e50a0e Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 23:49:45 +0200 Subject: [PATCH 067/202] reimplement: applyUnitTopSpeedDelayBasedOnTribeSize 46.2 -> 48.7% The bubble sort reads all four values up front and all four are `int`: the original has `movsx edi, [eax - 4]`, `movsx ebx, [eax]`, `movsx ecx, [eax - 2]`, `movsx edx, [eax + 2]` in that order, where `short` locals give `movzx` and leaving the next entry's ID to be read inside the swap lost the fourth load. Namespace average 82.54% -> 82.56%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...applyUnitTopSpeedDelayBasedOnTribeSize.cpp | 22 +++++++++++++------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp index 52eeeae6df..2f7d13da57 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp @@ -100,15 +100,23 @@ namespace Map { UnitIDMovementDistancePair* _nextUnit = this->someUnitIDMovementDistancePair + 1; do { UnitIDMovementDistancePair* pUVar2 = _nextUnit - 1; - short const _unitID = pUVar2->unitID; - short const _distance = pUVar2->movementDistance; - short const _nextDistance = _nextUnit->movementDistance; + /* + All four values are read up front and all four are `int`: the original + has `movsx edi, [eax - 4]`, `movsx ebx, [eax]`, `movsx ecx, [eax - 2]`, + `movsx edx, [eax + 2]` in that order, where `short` locals give + `movzx` and leaving the next entry's ID to be read inside the swap + loses the fourth load entirely. + */ + int const _unitID = pUVar2->unitID; + int const _nextUnitID = _nextUnit->unitID; + int const _distance = pUVar2->movementDistance; + int const _nextDistance = _nextUnit->movementDistance; if ((!param_2) && (_nextDistance < _distance)) { _foundUnsortedEntry = 1; - pUVar2->unitID = _nextUnit->unitID; - _nextUnit->unitID = _unitID; - pUVar2->movementDistance = _nextDistance; - _nextUnit->movementDistance = _distance; + pUVar2->unitID = (short)_nextUnitID; + _nextUnit->unitID = (short)_unitID; + pUVar2->movementDistance = (short)_nextDistance; + _nextUnit->movementDistance = (short)_distance; } _pairIndex = _pairIndex + 1; _nextUnit = _nextUnit + 1; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 65aca3b8a5..06feef720e 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46913,7 +46913,7 @@ SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending SHC_3BB0A8C1_0x00524E20 | 93.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524EF0 | 46.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524EF0 | 48.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525060 | 0.0% | Pending From f19ab579099bccda75df81826997b9b4c4bb9a05 Mon Sep 17 00:00:00 2001 From: Gynt Date: Tue, 20 Oct 2026 23:57:03 +0200 Subject: [PATCH 068/202] style: declare at first use in the two files that still needed it declare_at_use.py moved six declarations in drawFlagsAndUnitDestinations and processDeerMoving. Score-neutral on both (47.0% and 50.7% unchanged), so this is the style pass only. The tool also offered to move declarations in playUnitCombatSpeechForTarget, where the result does not compile (C2360, an initialization skipped by a case label), and in updatePeasantSeatingAtBuilding, which is already at 100% and not worth the risk of a slot reshuffle; both are left alone. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../drawFlagsAndUnitDestinations.cpp | 42 +++++++++---------- .../Units/TribesState/processDeerMoving.cpp | 15 +++---- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index 14d90a18f5..59cfada1af 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -33,7 +33,6 @@ namespace Map { UnitInstructionTypeShort UVar2; int _unitID; int iVar4; - uint _rallyIndex; short (*_pRallyPoints)[2]; int tile; short _isRallying; @@ -45,31 +44,30 @@ namespace Map { the starting index is a branch rather than a `setl` (`test ax, ax`, `jge`, `mov esi, 1`). */ - if (_isRallying - && DAT_GameSynchronyState::instance.currentPlayerSlotID == this->tribes[tribeID].owner) { - _rallyIndex = 0; + if (_isRallying && DAT_GameSynchronyState::instance.currentPlayerSlotID == this->tribes[tribeID].owner) { + uint _rallyIndex = 0; if (_isRallying < 0) { _rallyIndex = 1; } if ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount) { - _pRallyPoints = this->tribes[tribeID].rallyPointArray + _rallyIndex; - do { - /* - spawn the rally point flag - */ - MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, - DAT_ViewportRenderState::ptr)(IO::Graphics::GID_FLOATS_NEW, - (int)(((*_pRallyPoints)[0] + DAT_TileMapState::instance.flagAnimationFrame + _rallyIndex - + (*_pRallyPoints)[1]) - % 10 - + 0x61), - 0x12, -1, - (int)(DAT_ViewportRenderState::instance.translationMatrix[(*_pRallyPoints)[1]].addXgetTile - + (*_pRallyPoints)[0]), - 0xa0022); - _rallyIndex = _rallyIndex + 1; - _pRallyPoints = _pRallyPoints + 1; - } while ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount); + _pRallyPoints = this->tribes[tribeID].rallyPointArray + _rallyIndex; + do { + /* + spawn the rally point flag + */ + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(IO::Graphics::GID_FLOATS_NEW, + (int)(((*_pRallyPoints)[0] + DAT_TileMapState::instance.flagAnimationFrame + _rallyIndex + + (*_pRallyPoints)[1]) + % 10 + + 0x61), + 0x12, -1, + (int)(DAT_ViewportRenderState::instance.translationMatrix[(*_pRallyPoints)[1]].addXgetTile + + (*_pRallyPoints)[0]), + 0xa0022); + _rallyIndex = _rallyIndex + 1; + _pRallyPoints = _pRallyPoints + 1; + } while ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount); } } int iVar5 = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index f03978640e..397e3b418f 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -30,14 +30,9 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x0052AD50 void TribesState::processDeerMoving(int tribeID) { - int sVar2; - int SVar3; - int iVar5; BOOLEnum BVar7; int iVar8; - int _x10; - int _y10; - sVar2 = this->tribes[tribeID].selectionTargetUnitID; + int sVar2 = this->tribes[tribeID].selectionTargetUnitID; /* Both coordinates are read before either division: the original has `movsx edx, [+0x610]`, `movsx ecx, [+0x612]` and only then the two @@ -45,8 +40,8 @@ namespace Map { */ short const _unitX = DAT_UnitsState::instance.units[sVar2].x; short const _unitY = DAT_UnitsState::instance.units[sVar2].y; - _x10 = (int)_unitX / 10; - _y10 = (int)_unitY / 10; + int _x10 = (int)_unitX / 10; + int _y10 = (int)_unitY / 10; /* The predator sum's second term is read after the marsh tiles, not with the first: the original reads 0x1c, then 0x00, then 0x18, and only then @@ -54,7 +49,7 @@ namespace Map { */ iVar8 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; int _predatorCount = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount; - iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; + int iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; _predatorCount = _predatorCount + DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; /* A `short`, compared as one: `movzx ecx, word ptr [esi + 0x5c]` then @@ -104,7 +99,7 @@ namespace Map { } sVar2 = this->tribes[tribeID].predatorNearby; if (!sVar2) { - SVar3 = this->tribes[tribeID].tribeBehaviorType; + int SVar3 = this->tribes[tribeID].tribeBehaviorType; if (SVar3 == (SomeTribeBehaviorType)0) { if (iVar5) { this->tribes[tribeID].wanderIntervalTicks = 200; From af86208b93be128dd7c4bf74831c199a03bdd5cb Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 00:09:50 +0200 Subject: [PATCH 069/202] reimplement: giveTribeAnInstruction 27.9 -> 51.9%, by its real case order The arms were sorted ascending by instruction type; the original's are not sorted at all. Its jump table at 0x52a1e8 has 19 entries and the byte index table at 0x52a234 maps type - 3 onto them, and taking the arms in order of the address each entry points at gives the layout MSVC produced from the original source: 3, 31, 4, 32/33, 26, 5/22, 20, 34, 9/36/38, 23/35/37, 24, 7, 6, 15, 16, 17, 30, 21 Reordering ours to match recovers 23.9 points. Until now every block after the first arm sat at the wrong address, so reccmp's address-ordered diff counted almost the whole function as unmatched - 4566 of 5210 diff lines, including two runs of ~570 that turned out to be the jump tables themselves rather than code. UIT_FILL_MOAT still falls through into UIT_DIG_MOAT and the two are still adjacent in that order, which the original agrees with: its type 7 block at 0x52970c is immediately followed by its type 6 block at 0x529759. UIT_LIGHT_PITCH gained an explicit `break` because it is no longer the last arm. Also the tribe size is an `int`: the original reads it with `movsx esi, word ptr [ebp + 0x5c]` and compares it 32 bits wide (+0.1). Namespace average 82.56% -> 82.77%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 2084 +++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 1049 insertions(+), 1037 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index cd53138f06..af61452871 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -105,7 +105,13 @@ namespace Map { int _tile1_528565; short _y_528565; int _param_1_tribeID; - short _tribeSize; + /* + An `int`: the original reads the size with `movsx esi, word ptr [ebp + 0x5c]` + at 0x527c99 and compares it 32 bits wide (`cmp esi, edx`), where a `short` + gives `movzx`. It is only ever used in `0 < _tribeSize` guards, so nothing is + truncated differently. + */ + int _tribeSize; int _freeTile; uint _defensiveTile1; short _freeTileY; @@ -144,6 +150,32 @@ namespace Map { return (undefined4)(1); } break; + case Map::Units::UIT_STOP: + iVar11 = 0; + this->tribes[tribeID].isRallyingUnk = 0; + this->tribes[tribeID].someUnitID = 0; + if (0 < _tribeSize) { + do { + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar11); + iVar11 = iVar11 + 1; + if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar12].dying == 0 + && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0) { + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 3; + DAT_UnitsState::instance.units[iVar12].targetingType + = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; + DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].targetedBuildingTile = 0; + DAT_UnitsState::instance.units[iVar12].movementType_OR_targetUnitID = 0; + } + } while (iVar11 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; case Map::Units::UIT_UNIT_ATTACK_UNIT: /* unit attack target @@ -373,773 +405,680 @@ namespace Map { } } break; - case Map::Units::UIT_ATTACK_LAND: - case Map::Units::UIT_THROW_COW: - unitUID = this->tribes[tribeID].owner; - this->tribes[tribeID].isRallyingUnk = 0; + case ((UnitInstructionType)0x20): + case Map::Units::UIT_SHOOT_TARGETUnk: + local_8 = id * 0x490; + pUVar6 = DAT_UnitsState::instance.units + id; _unitTribeIndex = 0; + id = 0; + if (pUVar6->uid != unitUID) { + return (undefined4)(0); + } if (0 < _tribeSize) { do { - _unitID_0x05_0x16 = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, _unitTribeIndex); + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); _unitTribeIndex = _unitTribeIndex + 1; - if ((DAT_UnitsState::instance.units[_unitID_0x05_0x16].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[_unitID_0x05_0x16].dying == 0)) { - switch (DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType) { + if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar12].dying == 0 + && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0 + && (UVar3 = DAT_UnitsState::instance.units[iVar12].state.generic, + UVar3 != Map::Units::States::US_MELEE_ATTACK)) { + switch (DAT_UnitsState::instance.units[iVar12].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { + id = id + 1; + } + break; + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + if (unitInstructionType == Map::Units::UIT_SHOOT_TARGETUnk) { + break; + } + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + /* + and on into the ranged arm, which is what the + decompiler's goto back into it said + */ case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_XBOW: case Map::Units::UT_E_ARCHER_DEBUG: - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_BALLISTA: case Map::Units::UT_A_ARCHER: case Map::Units::UT_A_SLINGER: case Map::Units::UT_A_HARCHER: case Map::Units::UT_A_FIRETHROWER: - case Map::Units::UT_S_FBALLISTA: - if (DAT_UnitsState::instance.units[_unitID_0x05_0x16].state.generic - == (UnitState)0x69) { - DAT_UnitsState::instance.units[_unitID_0x05_0x16].state.generic + if (UVar3 == (UnitState)0x69) { + DAT_UnitsState::instance.units[iVar12].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; } - DAT_UnitsState::instance.units[_unitID_0x05_0x16].field283_0x3f8 = 0; - DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType - = (UnitInstructionTypeShort)unitInstructionType; - LAB_00528808: - DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 - = (byte)unitInstructionType; - sVar9 = (short)_param_4_unitUID_Y; - if (unitInstructionType == Map::Units::UIT_THROW_COW - && (UVar4 = DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType, - UVar4 == Map::Units::UT_S_CATAPULT || (UVar4 == Map::Units::UT_S_TREBUCHET))) { - if (DAT_GameState::instance.playerDataArray[unitUID].counter <= 0) { - DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 = 5; - } else { - DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType - = Map::Units::UIT_THROW_COW; - DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetMicroX - = _param_3_id_x_tile_buildingID_copy * 8; - DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetMicroY = sVar9 * 8; - DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetZ = (ushort) - * (byte*)(DAT_ViewportRenderState::instance - .translationMatrix[_param_4_unitUID_Y] - .addXgetTile - + 0x1d32c38 + iVar11); - } - } - DAT_UnitsState::instance.units[_unitID_0x05_0x16].attackAtTileX + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[iVar12].targetedUnitID__OR__engineerMannedSiegeEngineRef = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[_unitID_0x05_0x16].attackAtTileY = sVar9; - DAT_UnitsState::instance.units[_unitID_0x05_0x16].unkAttackRelated = (short)param_5; - DAT_UnitsState::instance.units[_unitID_0x05_0x16]._someX_2 = 0; - DAT_UnitsState::instance.units[_unitID_0x05_0x16]._someY_2 = 0; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { + this->tribes[_param_1_tribeID].containsRabbit = 1; + } + if (DAT_UnitsState::instance.units[iVar12]._someX_2 == 0) { + sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; + DAT_UnitsState::instance.units[iVar12]._someX_2 + = DAT_UnitsState::instance.units[iVar12].destinationX_2Unk; + DAT_UnitsState::instance.units[iVar12]._someY_2 = sVar9; + } MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(_unitID_0x05_0x16); - DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootBeforeStop = 10; + DAT_UnitsState::ptr)(iVar12); break; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TREBUCHET: - if (DAT_UnitsState::instance.units[_unitID_0x05_0x16] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - != 0) - goto LAB_00528808; - } - } - if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { - return (undefined4)(1); - } - } while (true); - } - break; - case Map::Units::UIT_FILL_MOAT: - _targetArea = DAT_TileMapState::instance.PathConnectionLayer - [DAT_UnitsState::instance.units[this->tribes[tribeID].selectionTargetUnitID].tile]; - this->tribes[tribeID].isRallyingUnk = 0; - if ((int)(short)_targetArea != 0) { - iVar11 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, - DAT_PathFindingState::ptr)( - this->tribes[tribeID].owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); - if (iVar11 == 0) { - return (undefined4)(1); - } - unitUID = DAT_PathFindingState::instance.climbY; - uVar13 = DAT_PathFindingState::instance.climbX; - } - case Map::Units::UIT_DIG_MOAT: - iVar11 = unitUID; - iVar12 = 0; - this->tribes[_param_1_tribeID].isRallyingUnk = 0; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, uVar13, (uint)(unitUID), 0, 1); - if (0 < this->tribes[_param_1_tribeID].size) { - do { - iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar12); - iVar12 = iVar12 + 1; - if ((DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar14].dying == 0)) { - switch (DAT_UnitsState::instance.units[iVar14].unitType) { - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_ENGINEER: - case Map::Units::UT_A_SLAVE: - if (DAT_UnitsState::instance.units[iVar14].state.generic == (UnitState)0x69) { - DAT_UnitsState::instance.units[iVar14].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; + if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { + sVar9 = DAT_UnitsState::instance.units[iVar12]._someX_2; + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 5; + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_LAND; + DAT_UnitsState::instance.units[iVar12].attackAtTileX + = DAT_UnitsState::instance.units[iVar11].x; + DAT_UnitsState::instance.units[iVar12].attackAtTileY + = DAT_UnitsState::instance.units[iVar11].y; + DAT_UnitsState::instance.units[iVar12].unkAttackRelated = 0xb; + if (sVar9 == 0) { + sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; + DAT_UnitsState::instance.units[iVar12]._someX_2 + = DAT_UnitsState::instance.units[iVar12].destinationX_2Unk; + DAT_UnitsState::instance.units[iVar12]._someY_2 = sVar9; + } + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } - DAT_UnitsState::instance.units[iVar14].targetingType = (undefined2)unitInstructionType; - DAT_UnitsState::instance.units[iVar14].attackAtTileX = (short)uVar13; - DAT_UnitsState::instance.units[iVar14].attackAtTileY = (short)iVar11; - DAT_UnitsState::instance.units[iVar14].targetX = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar14].targetY - = (UnitStateShort)_param_4_unitUID_Y_copy; - DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; - DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID = 0; - DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; - DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; - } - } - } while (iVar12 < this->tribes[_param_1_tribeID].size); - return (undefined4)(1); - } - break; - case Map::Units::UIT_ATTACK_BUILDING: - case ((UnitInstructionType)0x24): - case ((UnitInstructionType)0x26): - _unitTribeIndex = 0; - id = 0; - this->tribes[tribeID].targetBuildingID = _param_3_id_x_tile_buildingID_copy; - this->tribes[tribeID].targetBuildingUID = unitUID; - this->tribes[tribeID].isRallyingUnk = 0; - if (0 < _tribeSize) { - do { - iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitTribeIndex); - _unitTribeIndex = _unitTribeIndex + 1; - if (DAT_UnitsState::instance.units[iVar12].logicalState != Map::Units::ULS_NORMAL - || DAT_UnitsState::instance.units[iVar12].dying != 0 - || DAT_UnitsState::instance.units[iVar12].field303_0x413 != 0) - continue; - DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; - DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; - switch (DAT_UnitsState::instance.units[iVar12].unitType) { - case Map::Units::UT_E_KNIGHT: - if (DAT_BuildingsState::instance.buildings[iVar11].buildingType - != Map::Buildings::BT_PITCHDITCH) { - _ramOrHorse = _ramOrHorse + 1; - } - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - switchD_00528b77_caseD_5: - if (DAT_BuildingsState::instance.buildings[iVar11].buildingType - != Map::Buildings::BT_PITCHDITCH) { - id = id + 1; - } - break; - case Map::Units::UT_S_CATAPULT: - case Map::Units::UT_S_TREBUCHET: - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_BALLISTA: - case Map::Units::UT_S_FBALLISTA: - if (unitInstructionType == (UnitInstructionType)0x26 - || DAT_BuildingsState::instance.buildings[iVar11].buildingType - == Map::Buildings::BT_PITCHDITCH) - break; - DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar12] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = unitUID; - goto LAB_00528c46; - case Map::Units::UT_S_BATTERINGRAM: - if ((DAT_BuildingsState::instance.buildings[iVar11].buildingType - != Map::Buildings::BT_PITCHDITCH) - && (DAT_UnitsState::instance.units[iVar12] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) { - id = id + 1; - _ramOrHorse = _ramOrHorse + 1; - } - break; - case Map::Units::UT_A_HARCHER: - _ramOrHorse = _ramOrHorse + 1; - /* - The ranged types share this arm; only the horse archer counts - towards _ramOrHorse, which is why the decompiler had them - jumping in from their own label. - */ - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_FIRETHROWER: - if (unitInstructionType == (UnitInstructionType)0x24) { - switch (DAT_BuildingsState::instance.buildings[iVar11].buildingType) { - case Map::Buildings::BT_GATEHOUSELARGE: - case Map::Buildings::BT_GATEHOUSESMALL: - case Map::Buildings::BT_WOODGATE1: - case Map::Buildings::BT_WOODGATE2: - case Map::Buildings::BT_TOWER1: - case Map::Buildings::BT_TOWER2: - case Map::Buildings::BT_TOWER3: - case Map::Buildings::BT_TOWER4: - case Map::Buildings::BT_TOWER5: - goto switchD_00528bae_caseD_2d; - } - goto switchD_00528b77_caseD_5; - } - if (unitInstructionType != (UnitInstructionType)0x26) { - switchD_00528bae_caseD_2d: - /* - attack buildings - */ - if (DAT_UnitsState::instance.units[iVar12].state.generic == (UnitState)0x69) { - DAT_UnitsState::instance.units[iVar12].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - } - sVar8 = (short)((int)(DAT_BuildingsState::instance.buildings[iVar11].widthOrHeight * 8) - / 2); - DAT_UnitsState::instance.units[iVar12].shootTargetMicroX - = DAT_BuildingsState::instance.buildings[iVar11].x * 8 + sVar8; - sVar9 = DAT_BuildingsState::instance.buildings[iVar11].terrainHeightUnk; - DAT_UnitsState::instance.units[iVar12].shootTargetMicroY - = DAT_BuildingsState::instance.buildings[iVar11].y * 8 + sVar8; - DAT_UnitsState::instance.units[iVar12].shootTargetZ = sVar9; - DAT_UnitsState::instance.units[iVar12].shootTargetedUnit = -1; - DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar12] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = unitUID; - LAB_00528c46: - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 9; - DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_BUILDING; - DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; - MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(iVar12); - DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + break; } } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); - } - if ((((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) - || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) - || (4 < (int)(short)DAT_BuildingsState::instance.buildings[iVar11].buildingType - 0x4aU)) /* - Again the melee counter against zero, which the decompiler rendered - as `id | x | id != 0`. + The melee counter against zero, as at the first site. */ - && (id != 0)) { - sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; - if (unitInstructionType == Map::Units::UIT_ATTACK_BUILDING - || unitInstructionType == (UnitInstructionType)0x26) { - iVar12 = (int)DAT_UnitsState::instance.units[sVar9].owner; - } else { + if (id != 0) { + iVar12 = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; + param_5 = (int)DAT_UnitsState::instance.units[iVar12].x; + unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[iVar12].y; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( + iVar12, iVar11, ¶m_5, (int*)&unitInstructionType); + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathPlanningForTribe, + DAT_PathFindingState::ptr)(tribeID, (undefined4)((int)(iVar11)), (uint)(param_5), + (uint)(unitInstructionType), id, + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[iVar12].tile])), + (int)(DAT_UnitsState::instance.units[iVar12].owner)); + this->tribes[_param_1_tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; + this->tribes[_param_1_tribeID].someUnitUID = unitUID; + this->tribes[_param_1_tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; iVar12 = 0; + sVar9 = this->tribes[_param_1_tribeID].size; + this->tribes[_param_1_tribeID].someTile2 + = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] + .addXgetTile; + unitInstructionType = ((UnitInstructionType)0); + if (0 < sVar9) { + id = 0x12d5c6c; + do { + iVar14 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); + iVar12 = iVar12 + 1; + if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar14].dying == 0 + && DAT_UnitsState::instance.units[iVar14].field303_0x413 == 0 + && DAT_UnitsState::instance.units[iVar14].state.generic + != Map::Units::States::US_MELEE_ATTACK) { + switch (DAT_UnitsState::instance.units[iVar14].unitType) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + _tile1_528565 = *(int*)id; + if (_tile1_528565 != 0) { + _y_528565 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_tile1_528565]; + _x_528565 = _tile1_528565 + - DAT_ViewportRenderState::instance.translationMatrix[_y_528565] + .addXgetTile; + if (*(int*)(id + 4) == 0) { + DAT_UnitsState::instance.units[iVar14].targetingType + = ((UnitInstructionType)0); + if (DAT_UnitsState::instance.units[iVar14].unitType + == Map::Units::UT_LORD) + break; + } else { + DAT_UnitsState::instance.units[iVar14].targetingType + = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[iVar14] + .targetedUnitID__OR__engineerMannedSiegeEngineRef + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar14] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + } + DAT_UnitsState::instance.units[iVar14].plannedDestinationX + = (short)_x_528565; + DAT_UnitsState::instance.units[iVar14].plannedDestinationY = _y_528565; + DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; + if (DAT_UnitsState::instance.units[iVar14].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[iVar14].movementCooldown + = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; + } + if (DAT_UnitsState::instance.units[iVar14]._someX_2 == 0) { + DAT_UnitsState::instance.units[iVar14]._someX_2 + = DAT_UnitsState::instance.units[iVar14].destinationX_2Unk; + DAT_UnitsState::instance.units[iVar14]._someY_2 + = DAT_UnitsState::instance.units[iVar14].destinationY_2Unk; + } + DAT_UnitsState::instance.units[iVar14].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + if (MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID) + == 0) { + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; + } else { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(iVar14, _x_528565, (uint)(_y_528565), 0); + DAT_UnitsState::instance.units[iVar14].moveDelay = 0; + DAT_UnitsState::instance.units[iVar14].lookForEnemy = -0x32; + if (DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + != iVar11) { + DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + = _param_3_id_x_tile_buildingID_copy; + psVar1 = (short*)((int)&DAT_UnitsState::instance.units[0].huntedBy + + local_8); + *psVar1 = *psVar1 + 1; + } + UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[iVar14] + .totalSizeOfPathPlan + / 2); + if ((int)unitInstructionType < (int)UVar10) { + unitInstructionType = UVar10; + } + id = id + 0xc; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + } + } + } + } while (iVar12 < this->tribes[_param_1_tribeID].size); + } + iVar11 = unitInstructionType * 8; + if (iVar11 < 9) { + iVar11 = 8; + } + this->tribes[_param_1_tribeID].field166_0x2be = (short)iVar11; + return (undefined4)(1); } - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingForAttacksUnk, - DAT_PathFindingState::ptr)(tribeID, iVar11, id, - (dword)((int)((short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[sVar9].tile])), - iVar12); - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( - tribeID, _ramOrHorse); - if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) - && (_unitTribeIndex = 0, 0 < this->tribes[_param_1_tribeID].size)) { - id = 0x12d5c70; - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitTribeIndex); - _unitTribeIndex = _unitTribeIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0) { - UVar4 = DAT_UnitsState::instance.units[_unitID].unitType; - switch (UVar4) { - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_KNIGHT: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_S_BATTERINGRAM: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - switchD_00528e3a_caseD_5: - if (UVar4 != Map::Units::UT_S_BATTERINGRAM) - goto LAB_00528f32; - if (DAT_UnitsState::instance.units[_unitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - != 4) - break; - if (bVar5) { - LAB_00528f32: - iVar12 = *(int*)(id + -4); - if (iVar12 == 0) - break; - sVar9 = DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[iVar12]; - unitUID = iVar12 - - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; - DAT_UnitsState::instance.units[_unitID].targetingType - = ((UnitInstructionType)0); - DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)unitUID; - DAT_UnitsState::instance.units[_unitID].plannedDestinationY = sVar9; - if (DAT_UnitsState::instance.units[_unitID].state.generic - == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[_unitID].movementCooldown - = DAT_UnitsState::instance.units[_unitID].movementSpeed * -4; - } - DAT_UnitsState::instance.units[_unitID].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - BVar16 = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); - if (!BVar16) { - DAT_PathFindingState::instance.notAllAssassinsUnk = 1; - } else { - DAT_PathFindingState::instance.allAssassinsUnk = 1; - } - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(_unitID, (uint)(unitUID), (uint)(sVar9), 0); - DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID = 0; - DAT_PathFindingState::instance.notAllAssassinsUnk = 0; - uVar13 = *(uint*)id; - DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = uVar13; - sVar9 = DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[uVar13]; - DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar9; - sVar9 = *(short*)id - - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9] - .addXgetTile; - id = id + 0xc; + } + break; + case Map::Units::UT_E_MACE: + iVar11 = 0; + if (0 < _tribeSize) { + do { + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _param_1_tribeID, iVar11); + iVar11 = iVar11 + 1; + if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[iVar12].dying == 0 + && (int)(short)DAT_UnitsState::instance.units[iVar12].unitType - 0x27U < 2) { + DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 1; + } + } while (iVar11 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case Map::Units::UIT_ATTACK_LAND: + case Map::Units::UIT_THROW_COW: + unitUID = this->tribes[tribeID].owner; + this->tribes[tribeID].isRallyingUnk = 0; + _unitTribeIndex = 0; + if (0 < _tribeSize) { + do { + _unitID_0x05_0x16 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if ((DAT_UnitsState::instance.units[_unitID_0x05_0x16].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID_0x05_0x16].dying == 0)) { + switch (DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_E_ARCHER_DEBUG: + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + case Map::Units::UT_S_FBALLISTA: + if (DAT_UnitsState::instance.units[_unitID_0x05_0x16].state.generic + == (UnitState)0x69) { + DAT_UnitsState::instance.units[_unitID_0x05_0x16].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType + = (UnitInstructionTypeShort)unitInstructionType; + LAB_00528808: + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 + = (byte)unitInstructionType; + sVar9 = (short)_param_4_unitUID_Y; + if (unitInstructionType == Map::Units::UIT_THROW_COW + && (UVar4 = DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType, + UVar4 == Map::Units::UT_S_CATAPULT || (UVar4 == Map::Units::UT_S_TREBUCHET))) { + if (DAT_GameState::instance.playerDataArray[unitUID].counter <= 0) { + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 = 5; } else { - iVar12 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findBuildingAccessPoint, - DAT_PathFindingState::ptr)(_unitID, iVar11, ¶m_5); - bVar5 = TRUE; - if (iVar12 == 0) - goto LAB_00528f32; - sVar9 = DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[iVar12]; - unitUID = iVar12 - - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; - BVar16 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(_unitID, (uint)(unitUID), (uint)(sVar9), 0); - if (!BVar16) - goto LAB_00528f32; - DAT_UnitsState::instance.units[_unitID].targetingType - = ((UnitInstructionType)0); - DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)unitUID; - DAT_UnitsState::instance.units[_unitID].plannedDestinationY = sVar9; - if (DAT_UnitsState::instance.units[_unitID].state.generic - == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[_unitID].movementCooldown - = DAT_UnitsState::instance.units[_unitID].movementSpeed * -4; - } - DAT_UnitsState::instance.units[_unitID].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID = 0; - DAT_PathFindingState::instance.notAllAssassinsUnk = 0; - sVar9 = DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[param_5]; - DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = param_5; - DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar9; - sVar9 = (short)param_5 - - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9] - .addXgetTile; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType + = Map::Units::UIT_THROW_COW; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetMicroX + = _param_3_id_x_tile_buildingID_copy * 8; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetMicroY = sVar9 * 8; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetZ = (ushort) + * (byte*)(DAT_ViewportRenderState::instance + .translationMatrix[_param_4_unitUID_Y] + .addXgetTile + + 0x1d32c38 + iVar11); } - DAT_UnitsState::instance.units[_unitID].attackAtTileX = sVar9; - break; - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_HARCHER: - case Map::Units::UT_A_FIRETHROWER: - if (unitInstructionType == (UnitInstructionType)0x24) - goto switchD_00528e3a_caseD_5; } + DAT_UnitsState::instance.units[_unitID_0x05_0x16].attackAtTileX + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].attackAtTileY = sVar9; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].unkAttackRelated = (short)param_5; + DAT_UnitsState::instance.units[_unitID_0x05_0x16]._someX_2 = 0; + DAT_UnitsState::instance.units[_unitID_0x05_0x16]._someY_2 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(_unitID_0x05_0x16); + DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootBeforeStop = 10; + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + if (DAT_UnitsState::instance.units[_unitID_0x05_0x16] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 0) + goto LAB_00528808; } - if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { - return (undefined4)(1); - } - } while (true); - } + } + if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { + return (undefined4)(1); + } + } while (true); } break; - case Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED: + case Map::Units::UIT_THROW_OIL: + iVar11 = 0; this->tribes[tribeID].isRallyingUnk = 0; - _y = DAT_BuildingsState::instance.buildings[id].buildingEntryY; - _x = DAT_BuildingsState::instance.buildings[id].buildingEntryX; - _unitTribeIndex = 0; - BVar16 = MACRO_CALL_MEMBER( - Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)((int)_x, (uint)(_y)); - if (!BVar16) { - return (undefined4)(0); - } - param_5 = (int)(short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile - + (int)_x]; - unitUID = MACRO_CALL_MEMBER( - Map::Buildings::BuildingsState_Func::getRequiredEngineersCount, DAT_BuildingsState::ptr)(iVar11); - if (((unitUID | unitUID != 0) - || (DAT_BuildingsState::instance.buildings[iVar11].buildingType - == Map::Buildings::BT_OILSMELTER)) - && 0 < this->tribes[_param_1_tribeID].size) { + if (0 < _tribeSize) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitTribeIndex); - iVar12 = _unitTribeIndex + 1; + tribeID, iVar11); + iVar11 = iVar11 + 1; if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[uVar13].dying == 0 - && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER - && (UVar3 = DAT_UnitsState::instance.units[uVar13].state.generic, - UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk) - && (UVar3 != ((UnitState)10))) - && UVar3 != Map::Units::States::US_SIT_DOWNUnk - && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_RELOAD_WEAPONUnk) - && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_AIM_WEAPONUnk) - && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_FIRE_WEAPONUnk) - && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_LOOK_AROUNDUnk)) { + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER) { + DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_THROW_OIL; + DAT_UnitsState::instance.units[uVar13].attackAtTileX = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].attackAtTileY = (short)unitUID; + DAT_UnitsState::instance.units[uVar13].plannedDestinationX + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].plannedDestinationY = (short)unitUID; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; - cVar7 = MACRO_CALL_MEMBER( - Map::Buildings::BuildingsState_Func::resolveBuildingEntryAccessibility, - DAT_BuildingsState::ptr)(iVar11, 1, (int)(DAT_UnitsState::instance.units[uVar13].x), - (int)(DAT_UnitsState::instance.units[uVar13].y)); - /* - The `extraout_var` reassembly of the upper three bytes stays, - although it is a decompiler artifact: 0x421be0 really returns - 0, 1 or 2 in the whole of EAX (0x421d4f sets ECX to 1 or 2), - and the original's test is `test eax, eax`. Our header - declares the callee `char`, so a plain `cVar7 != 0` tests only - AL and costs 1.7 points (27.9% -> 26.2%). Fixing it properly - means giving BuildingsState::resolveBuildingEntryAccessibility - an `int` return, which is outside this namespace. - */ - if (((((uint)(extraout_var) & 0xffffff) << 8) | (uint)(byte)(cVar7)) != 0 - && (iVar14 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: - calculateCanPlayerUnitsNavigateToAreaFromArea, - DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, - (dword)((int)(param_5)), - (dword)((int)((short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile])), - 0), - iVar14 != 0)) { - sVar9 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryY; - sVar8 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX; - iVar14 = DAT_BuildingsState::instance.buildings[iVar11].uid; - DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[uVar13].targetUID = iVar14; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(uVar13, (uint)(sVar8), (uint)(sVar9), 0); - if (DAT_BuildingsState::instance.buildings[iVar11].buildingType - == Map::Buildings::BT_OILSMELTER) { - DAT_UnitsState::instance.units[uVar13].state.generic - = Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk; - DAT_UnitsState::instance.units[uVar13].workplaceBuildingID_1 - = _param_3_id_x_tile_buildingID_copy; - } else { - DAT_UnitsState::instance.units[uVar13].state.generic - = Map::Units::States::US_STAND_UPUnk; - DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( - uVar13); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); - unitUID = unitUID + -1; - iVar12 = _unitTribeIndex; - if (unitUID == 0) { - return (undefined4)(1); - } - } - } } - _unitTribeIndex = iVar12; - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (iVar11 < this->tribes[_param_1_tribeID].size); return (undefined4)(1); } break; - case Map::Units::UIT_MAN_SIEGE_EQUIPMENT: - /* - manning siege equipment? - */ + case Map::Units::UIT_LIGHT_PITCH: this->tribes[tribeID].isRallyingUnk = 0; - if (DAT_UnitsState::instance.units[id].uid == unitUID - && (unitUID = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(id), - unitUID | unitUID != 0) - && (iVar12 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + _unitTribeIndex = 0; + if ((DAT_TileMapState::instance.pitchDitches[id].uid == unitUID) && (0 < _tribeSize)) { do { - uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar12); - iVar14 = iVar12 + 1; - if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[uVar13].dying == 0 - && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER) { - DAT_UnitsState::instance.units[iVar11]._someX_2 = 0; - DAT_UnitsState::instance.units[iVar11]._someY_2 = 0; - param_5 = (int)(short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile]; - unitInstructionType = ((UnitInstructionType)0); - do { - iVar14 = (int)DAT_UnitsState::instance.units[iVar11] - .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine; - x = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][0] - + (int)DAT_UnitsState::instance.units[iVar11].x; - y = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][1] - + (int)DAT_UnitsState::instance.units[iVar11].y; - uVar19 = (iVar14 + 1) % 16; - DAT_UnitsState::instance.units[iVar11] - .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine = (short)uVar19; - BVar16 = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, - DAT_ViewportRenderState::ptr)(x, y); - if (BVar16) { - iVar14 = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; - _param_4_unitUID_Y_copy - = (dword)(short)DAT_TileMapState::instance.PathConnectionLayer[iVar14]; - uVar19 = MACRO_CALL_MEMBER( - Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)(iVar14); - uVar19 = ((int)DAT_UnitsState::instance.units[iVar11].buildingHeight - + (int)DAT_UnitsState::instance.units[iVar11].terrainOrClimbHeight) - - uVar19; - uVar17 = (int)uVar19 >> 0x1f; - if ((int)((uVar19 ^ uVar17) - uVar17) < 0x11 - && (iVar14 - = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: - calculateCanPlayerUnitsNavigateToAreaFromArea, - DAT_PathFindingState::ptr)( - (int)DAT_UnitsState::instance.units[iVar11].owner, - (dword)((int)(param_5)), (dword)((int)(_param_4_unitUID_Y_copy)), 0), - iVar14 != 0)) - break; - } - unitInstructionType = (UnitInstructionType)(unitInstructionType + 1); - } while ((int)unitInstructionType < 0x10); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( - uVar13, x, y, 0); - DAT_UnitsState::instance.units[uVar13].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_MAN_SIEGE_EQUIPMENT; - DAT_UnitsState::instance.units[uVar13].targetedUnitID__OR__engineerMannedSiegeEngineRef - = (short)id; - DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); - unitUID = unitUID + -1; - iVar14 = iVar12; - if (unitUID == 0) { - return (undefined4)(1); + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if ((DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar12].dying == 0)) { + DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; + UVar4 = DAT_UnitsState::instance.units[iVar12].unitType; + if ((UVar4 == Map::Units::UT_E_ARCHER) || (UVar4 == Map::Units::UT_A_ARCHER)) { + sVar9 = DAT_TileMapState::instance.pitchDitches[iVar11].y; + DAT_UnitsState::instance.units[iVar12].shootTargetMicroX + = DAT_TileMapState::instance.pitchDitches[iVar11].x * 8 + 4; + iVar14 = DAT_TileMapState::instance.pitchDitches[iVar11].tile; + DAT_UnitsState::instance.units[iVar12].shootTargetMicroY = sVar9 * 8 + 4; + DAT_UnitsState::instance.units[iVar12].shootTargetZ + = (ushort)DAT_TileMapState::instance.HeightLayer[iVar14]; + DAT_UnitsState::instance.units[iVar12].shootTargetedUnit = -1; + DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 0x22; + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_LIGHT_PITCH; + DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } } - iVar12 = iVar14; - if (this->tribes[_param_1_tribeID].size <= iVar14) { - return (undefined4)(1); - } - } while (true); + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); } - break; - case Map::Units::UIT_EXIT_SIEGE_EQUIPMENT: /* - disband siege engines? + An explicit break: this arm sat last and fell out of the switch to the + shared `return 1`, which is where the original's 0x5289a3 block jumps + (0x52a156). It is no longer last. */ - _param_4_unitUID_Y_copy = param_5 != 0 ? 0x10 : 0; - param_5 = (int)SEC_RNG::instance.currentNumber2 & 0x8000000f; - if (param_5 < 0) { - param_5 = (param_5 - 1U | 0xfffffff0) + 1; - } - MACRO_CALL_MEMBER(Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); - if (DAT_UnitsState::instance.units[iVar11].uid == unitUID) { - DAT_UnitsState::instance.units[iVar11].state.generic = Map::Units::States::US_FIRE_WEAPONUnk; - unitInstructionType = ((UnitInstructionType)0); - if (0 < DAT_UnitsState::instance.units[iVar11] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300) { - tribeID = iVar11 * 0x490 + 0x1388860; - do { - _unitID_0x11 = (int)*(short*)tribeID; - *(undefined2*)tribeID = 0; - DAT_UnitsState::instance.units[_unitID_0x11].field249_0x3c0 = 0; - DAT_UnitsState::instance.units[_unitID_0x11].field248_0x3bc = 0; - DAT_UnitsState::instance.units[_unitID_0x11].animationCycleNumber = 0; - DAT_UnitsState::instance.units[_unitID_0x11].state.generic - = Map::Units::States::US_JESTER_ROAM_TO; - DAT_UnitsState::instance.units[_unitID_0x11].disappearFadeAlphaCountdown = 0x20; - DAT_UnitsState::instance.units[_unitID_0x11].engineerManningSiegeStateRef_checkType = 0xfe; - DAT_UnitsState::instance.units[_unitID_0x11].cachedState - = (UnitStateShort)_param_4_unitUID_Y_copy; - UVar4 = DAT_UnitsState::instance.units[iVar11].unitType; - if ((UVar4 == Map::Units::UT_S_MANGONEL) || (UVar4 == Map::Units::UT_S_BALLISTA)) { - DAT_UnitsState::instance.units[_unitID_0x11].goToRallyPoint = 1; - } - DAT_UnitsState::instance.units[_unitID_0x11].updateTickTracker = 0; - DAT_UnitsState::instance.units[_unitID_0x11].field39_0x58 = 1; - unitUID = 0; - LAB_00529da0: - do { - uVar19 = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[param_5][0] - + (int)DAT_UnitsState::instance.units[iVar11].x; - uVar13 = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[param_5][1] - + (int)DAT_UnitsState::instance.units[iVar11].y; - param_5 = param_5 + 1U & 0x8000000f; - if (param_5 < 0) { - param_5 = (param_5 - 1U | 0xfffffff0) + 1; - } - MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findWalkableTileThatDoesNotContainUnit, - DAT_PathFindingState::ptr)(id, uVar19, uVar13, 1); - if (DAT_PathFindingState::instance.ALG_ResultTile == 0) { - MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findWalkableTileThatDoesNotContainUnit, - DAT_PathFindingState::ptr)(id, uVar19, uVar13, 0); - sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; - sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; - iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; - if (DAT_PathFindingState::instance.ALG_ResultTile != 0) - break; - unitUID = unitUID + 1; - if (unitUID | unitUID != 0x10) - goto LAB_00529da0; - sVar9 = DAT_UnitsState::instance.units[iVar11].x; - sVar8 = DAT_UnitsState::instance.units[iVar11].y; - iVar12 = DAT_UnitsState::instance.units[iVar11].tile; - } else { - sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; - sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; - iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; - } - } while (iVar12 == 0); - bVar2 = DAT_TileMapState::instance.HeightLayer[iVar12]; - DAT_UnitsState::instance.units[_unitID_0x11].totalSizeOfPathPlan = 0; - DAT_UnitsState::instance.units[_unitID_0x11].x = sVar9; - DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentXPosition = sVar9; - DAT_UnitsState::instance.units[_unitID_0x11].y = sVar8; - DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentYPosition = sVar8; - DAT_UnitsState::instance.units[_unitID_0x11].terrainOrClimbHeight = (ushort)bVar2; - DAT_UnitsState::instance.units[_unitID_0x11].tile = iVar12; - DAT_UnitsState::instance.units[_unitID_0x11].nextTileUnk = iVar12; - DAT_UnitsState::instance.units[_unitID_0x11].microXPosition = sVar9 * 8 + 4; - DAT_UnitsState::instance.units[_unitID_0x11].microYPosition = sVar8 * 8 + 4; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::updateMicroPosition, DAT_UnitsState::ptr)( - _unitID_0x11); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::resetUnitMovementState, DAT_UnitsState::ptr)( - _unitID_0x11); - tribeID = tribeID + 2; - DAT_UnitsState::instance.units[_unitID_0x11].animationSheetFrameOffset = 1; - DAT_UnitsState::instance.units[_unitID_0x11].field_0x30_animRelated = 0x10; - unitInstructionType = (UnitInstructionType)(unitInstructionType + 1); - } while ((int)unitInstructionType < (int)DAT_UnitsState::instance.units[iVar11] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300); - } - DAT_UnitsState::instance.units[iVar11] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 = 0; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(id); - return (undefined4)(1); - } break; - case Map::Units::UIT_THROW_OIL: - iVar11 = 0; + case Map::Units::UIT_ATTACK_BUILDING: + case ((UnitInstructionType)0x24): + case ((UnitInstructionType)0x26): + _unitTribeIndex = 0; + id = 0; + this->tribes[tribeID].targetBuildingID = _param_3_id_x_tile_buildingID_copy; + this->tribes[tribeID].targetBuildingUID = unitUID; this->tribes[tribeID].isRallyingUnk = 0; if (0 < _tribeSize) { do { - uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar11); - iVar11 = iVar11 + 1; - if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[uVar13].dying == 0 - && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER) { - DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_THROW_OIL; - DAT_UnitsState::instance.units[uVar13].attackAtTileX = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[uVar13].attackAtTileY = (short)unitUID; - DAT_UnitsState::instance.units[uVar13].plannedDestinationX - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[uVar13].plannedDestinationY = (short)unitUID; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); - DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; - DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; - } - } while (iVar11 < this->tribes[_param_1_tribeID].size); - return (undefined4)(1); - } - break; - case ((UnitInstructionType)0x15): - this->tribes[tribeID].isRallyingUnk = 0; - uVar13 = (uint)DAT_BuildingsState::instance.buildings[id].buildingEntryY; - _unitTribeIndex = 0; - BVar16 - = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)( - (int)DAT_BuildingsState::instance.buildings[id].buildingEntryX, uVar13); - if (!BVar16) { - return (undefined4)(0); - } - param_5 - = (int)(short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[uVar13].addXgetTile - + (int)DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX]; - if (0 < _tribeSize) { - do { - uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, _unitTribeIndex); _unitTribeIndex = _unitTribeIndex + 1; - if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[uVar13].dying == 0 - && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_TUNNELER) { - DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; - DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; - iVar12 = MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::buildingIsAccessible, - DAT_BuildingsState::ptr)(iVar11, 1); - if (iVar12 != 0 - && (iVar12 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: - calculateCanPlayerUnitsNavigateToAreaFromArea, - DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, - (dword)((int)(param_5)), - (dword)((int)((short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile])), - 0), - iVar12 != 0)) { - sVar9 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryY; - sVar8 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX; - iVar11 = DAT_BuildingsState::instance.buildings[iVar11].uid; - DAT_UnitsState::instance.units[uVar13].workplaceBuildingID_1 - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID + if (DAT_UnitsState::instance.units[iVar12].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[iVar12].dying != 0 + || DAT_UnitsState::instance.units[iVar12].field303_0x413 != 0) + continue; + DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; + switch (DAT_UnitsState::instance.units[iVar12].unitType) { + case Map::Units::UT_E_KNIGHT: + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) { + _ramOrHorse = _ramOrHorse + 1; + } + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + switchD_00528b77_caseD_5: + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) { + id = id + 1; + } + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + if (unitInstructionType == (UnitInstructionType)0x26 + || DAT_BuildingsState::instance.buildings[iVar11].buildingType + == Map::Buildings::BT_PITCHDITCH) + break; + DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + goto LAB_00528c46; + case Map::Units::UT_S_BATTERINGRAM: + if ((DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) + && (DAT_UnitsState::instance.units[iVar12] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) { + id = id + 1; + _ramOrHorse = _ramOrHorse + 1; + } + break; + case Map::Units::UT_A_HARCHER: + _ramOrHorse = _ramOrHorse + 1; + /* + The ranged types share this arm; only the horse archer counts + towards _ramOrHorse, which is why the decompiler had them + jumping in from their own label. + */ + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_FIRETHROWER: + if (unitInstructionType == (UnitInstructionType)0x24) { + switch (DAT_BuildingsState::instance.buildings[iVar11].buildingType) { + case Map::Buildings::BT_GATEHOUSELARGE: + case Map::Buildings::BT_GATEHOUSESMALL: + case Map::Buildings::BT_WOODGATE1: + case Map::Buildings::BT_WOODGATE2: + case Map::Buildings::BT_TOWER1: + case Map::Buildings::BT_TOWER2: + case Map::Buildings::BT_TOWER3: + case Map::Buildings::BT_TOWER4: + case Map::Buildings::BT_TOWER5: + goto switchD_00528bae_caseD_2d; + } + goto switchD_00528b77_caseD_5; + } + if (unitInstructionType != (UnitInstructionType)0x26) { + switchD_00528bae_caseD_2d: + /* + attack buildings + */ + if (DAT_UnitsState::instance.units[iVar12].state.generic == (UnitState)0x69) { + DAT_UnitsState::instance.units[iVar12].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + sVar8 = (short)((int)(DAT_BuildingsState::instance.buildings[iVar11].widthOrHeight * 8) + / 2); + DAT_UnitsState::instance.units[iVar12].shootTargetMicroX + = DAT_BuildingsState::instance.buildings[iVar11].x * 8 + sVar8; + sVar9 = DAT_BuildingsState::instance.buildings[iVar11].terrainHeightUnk; + DAT_UnitsState::instance.units[iVar12].shootTargetMicroY + = DAT_BuildingsState::instance.buildings[iVar11].y * 8 + sVar8; + DAT_UnitsState::instance.units[iVar12].shootTargetZ = sVar9; + DAT_UnitsState::instance.units[iVar12].shootTargetedUnit = -1; + DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[uVar13].targetUID = iVar11; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(uVar13, (uint)(sVar8), (uint)(sVar9), 0); - DAT_UnitsState::instance.units[uVar13].state.generic - = Map::Units::States::US_LOOK_AROUNDUnk; - DAT_UnitsState::instance.units[uVar13].targetingType = ((UnitInstructionType)0x15); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( - uVar13); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + LAB_00528c46: + DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 9; + DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_BUILDING; + DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(iVar12); + DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + } + } + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } + if ((((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) + || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) + || (4 < (int)(short)DAT_BuildingsState::instance.buildings[iVar11].buildingType - 0x4aU)) + /* + Again the melee counter against zero, which the decompiler rendered + as `id | x | id != 0`. + */ + && (id != 0)) { + sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; + if (unitInstructionType == Map::Units::UIT_ATTACK_BUILDING + || unitInstructionType == (UnitInstructionType)0x26) { + iVar12 = (int)DAT_UnitsState::instance.units[sVar9].owner; + } else { + iVar12 = 0; + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingForAttacksUnk, + DAT_PathFindingState::ptr)(tribeID, iVar11, id, + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[sVar9].tile])), + iVar12); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( + tribeID, _ramOrHorse); + if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) + && (_unitTribeIndex = 0, 0 < this->tribes[_param_1_tribeID].size)) { + id = 0x12d5c70; + do { + _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + _unitTribeIndex = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0 + && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0) { + UVar4 = DAT_UnitsState::instance.units[_unitID].unitType; + switch (UVar4) { + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_S_BATTERINGRAM: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + switchD_00528e3a_caseD_5: + if (UVar4 != Map::Units::UT_S_BATTERINGRAM) + goto LAB_00528f32; + if (DAT_UnitsState::instance.units[_unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 4) + break; + if (bVar5) { + LAB_00528f32: + iVar12 = *(int*)(id + -4); + if (iVar12 == 0) + break; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar12]; + unitUID = iVar12 + - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + DAT_UnitsState::instance.units[_unitID].targetingType + = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)unitUID; + DAT_UnitsState::instance.units[_unitID].plannedDestinationY = sVar9; + if (DAT_UnitsState::instance.units[_unitID].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[_unitID].movementCooldown + = DAT_UnitsState::instance.units[_unitID].movementSpeed * -4; + } + DAT_UnitsState::instance.units[_unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + BVar16 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + if (!BVar16) { + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; + } else { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID, (uint)(unitUID), (uint)(sVar9), 0); + DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID = 0; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + uVar13 = *(uint*)id; + DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = uVar13; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[uVar13]; + DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar9; + sVar9 = *(short*)id + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9] + .addXgetTile; + id = id + 0xc; + } else { + iVar12 = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findBuildingAccessPoint, + DAT_PathFindingState::ptr)(_unitID, iVar11, ¶m_5); + bVar5 = TRUE; + if (iVar12 == 0) + goto LAB_00528f32; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[iVar12]; + unitUID = iVar12 + - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; + BVar16 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(_unitID, (uint)(unitUID), (uint)(sVar9), 0); + if (!BVar16) + goto LAB_00528f32; + DAT_UnitsState::instance.units[_unitID].targetingType + = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)unitUID; + DAT_UnitsState::instance.units[_unitID].plannedDestinationY = sVar9; + if (DAT_UnitsState::instance.units[_unitID].state.generic + == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[_unitID].movementCooldown + = DAT_UnitsState::instance.units[_unitID].movementSpeed * -4; + } + DAT_UnitsState::instance.units[_unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID = 0; + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + sVar9 = DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[param_5]; + DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = param_5; + DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar9; + sVar9 = (short)param_5 + - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9] + .addXgetTile; + } + DAT_UnitsState::instance.units[_unitID].attackAtTileX = sVar9; + break; + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + if (unitInstructionType == (UnitInstructionType)0x24) + goto switchD_00528e3a_caseD_5; + } + } + if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { return (undefined4)(1); } - } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (true); + } } - DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( - iVar11); break; case Map::Units::UIT_ATTACK_WALL: case ((UnitInstructionType)0x23): @@ -1495,22 +1434,332 @@ namespace Map { } while (iVar11 < this->tribes[_param_1_tribeID].size); return (undefined4)(1); } - } + } + } + break; + case Map::Units::UIT_FILL_MOAT: + _targetArea = DAT_TileMapState::instance.PathConnectionLayer + [DAT_UnitsState::instance.units[this->tribes[tribeID].selectionTargetUnitID].tile]; + this->tribes[tribeID].isRallyingUnk = 0; + if ((int)(short)_targetArea != 0) { + iVar11 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, + DAT_PathFindingState::ptr)( + this->tribes[tribeID].owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); + if (iVar11 == 0) { + return (undefined4)(1); + } + unitUID = DAT_PathFindingState::instance.climbY; + uVar13 = DAT_PathFindingState::instance.climbX; + } + case Map::Units::UIT_DIG_MOAT: + iVar11 = unitUID; + iVar12 = 0; + this->tribes[_param_1_tribeID].isRallyingUnk = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, uVar13, (uint)(unitUID), 0, 1); + if (0 < this->tribes[_param_1_tribeID].size) { + do { + iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar12); + iVar12 = iVar12 + 1; + if ((DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[iVar14].dying == 0)) { + switch (DAT_UnitsState::instance.units[iVar14].unitType) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_ENGINEER: + case Map::Units::UT_A_SLAVE: + if (DAT_UnitsState::instance.units[iVar14].state.generic == (UnitState)0x69) { + DAT_UnitsState::instance.units[iVar14].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + } + DAT_UnitsState::instance.units[iVar14].targetingType = (undefined2)unitInstructionType; + DAT_UnitsState::instance.units[iVar14].attackAtTileX = (short)uVar13; + DAT_UnitsState::instance.units[iVar14].attackAtTileY = (short)iVar11; + DAT_UnitsState::instance.units[iVar14].targetX = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar14].targetY + = (UnitStateShort)_param_4_unitUID_Y_copy; + DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; + DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID = 0; + DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; + } + } + } while (iVar12 < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED: + this->tribes[tribeID].isRallyingUnk = 0; + _y = DAT_BuildingsState::instance.buildings[id].buildingEntryY; + _x = DAT_BuildingsState::instance.buildings[id].buildingEntryX; + _unitTribeIndex = 0; + BVar16 = MACRO_CALL_MEMBER( + Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)((int)_x, (uint)(_y)); + if (!BVar16) { + return (undefined4)(0); + } + param_5 = (int)(short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile + + (int)_x]; + unitUID = MACRO_CALL_MEMBER( + Map::Buildings::BuildingsState_Func::getRequiredEngineersCount, DAT_BuildingsState::ptr)(iVar11); + if (((unitUID | unitUID != 0) + || (DAT_BuildingsState::instance.buildings[iVar11].buildingType + == Map::Buildings::BT_OILSMELTER)) + && 0 < this->tribes[_param_1_tribeID].size) { + do { + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, _unitTribeIndex); + iVar12 = _unitTribeIndex + 1; + if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[uVar13].dying == 0 + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER + && (UVar3 = DAT_UnitsState::instance.units[uVar13].state.generic, + UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk) + && (UVar3 != ((UnitState)10))) + && UVar3 != Map::Units::States::US_SIT_DOWNUnk + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_RELOAD_WEAPONUnk) + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_AIM_WEAPONUnk) + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_FIRE_WEAPONUnk) + && UVar3 != (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_LOOK_AROUNDUnk)) { + DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; + DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; + cVar7 = MACRO_CALL_MEMBER( + Map::Buildings::BuildingsState_Func::resolveBuildingEntryAccessibility, + DAT_BuildingsState::ptr)(iVar11, 1, (int)(DAT_UnitsState::instance.units[uVar13].x), + (int)(DAT_UnitsState::instance.units[uVar13].y)); + /* + The `extraout_var` reassembly of the upper three bytes stays, + although it is a decompiler artifact: 0x421be0 really returns + 0, 1 or 2 in the whole of EAX (0x421d4f sets ECX to 1 or 2), + and the original's test is `test eax, eax`. Our header + declares the callee `char`, so a plain `cVar7 != 0` tests only + AL and costs 1.7 points (27.9% -> 26.2%). Fixing it properly + means giving BuildingsState::resolveBuildingEntryAccessibility + an `int` return, which is outside this namespace. + */ + if (((((uint)(extraout_var) & 0xffffff) << 8) | (uint)(byte)(cVar7)) != 0 + && (iVar14 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, + (dword)((int)(param_5)), + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile])), + 0), + iVar14 != 0)) { + sVar9 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryY; + sVar8 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX; + iVar14 = DAT_BuildingsState::instance.buildings[iVar11].uid; + DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].targetUID = iVar14; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(uVar13, (uint)(sVar8), (uint)(sVar9), 0); + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + == Map::Buildings::BT_OILSMELTER) { + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk; + DAT_UnitsState::instance.units[uVar13].workplaceBuildingID_1 + = _param_3_id_x_tile_buildingID_copy; + } else { + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_STAND_UPUnk; + DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( + uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + unitUID = unitUID + -1; + iVar12 = _unitTribeIndex; + if (unitUID == 0) { + return (undefined4)(1); + } + } + } + } + _unitTribeIndex = iVar12; + } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + return (undefined4)(1); + } + break; + case Map::Units::UIT_MAN_SIEGE_EQUIPMENT: + /* + manning siege equipment? + */ + this->tribes[tribeID].isRallyingUnk = 0; + if (DAT_UnitsState::instance.units[id].uid == unitUID + && (unitUID = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(id), + unitUID | unitUID != 0) + && (iVar12 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + do { + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, iVar12); + iVar14 = iVar12 + 1; + if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[uVar13].dying == 0 + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER) { + DAT_UnitsState::instance.units[iVar11]._someX_2 = 0; + DAT_UnitsState::instance.units[iVar11]._someY_2 = 0; + param_5 = (int)(short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile]; + unitInstructionType = ((UnitInstructionType)0); + do { + iVar14 = (int)DAT_UnitsState::instance.units[iVar11] + .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine; + x = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][0] + + (int)DAT_UnitsState::instance.units[iVar11].x; + y = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][1] + + (int)DAT_UnitsState::instance.units[iVar11].y; + uVar19 = (iVar14 + 1) % 16; + DAT_UnitsState::instance.units[iVar11] + .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine = (short)uVar19; + BVar16 = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, + DAT_ViewportRenderState::ptr)(x, y); + if (BVar16) { + iVar14 = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + _param_4_unitUID_Y_copy + = (dword)(short)DAT_TileMapState::instance.PathConnectionLayer[iVar14]; + uVar19 = MACRO_CALL_MEMBER( + Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)(iVar14); + uVar19 = ((int)DAT_UnitsState::instance.units[iVar11].buildingHeight + + (int)DAT_UnitsState::instance.units[iVar11].terrainOrClimbHeight) + - uVar19; + uVar17 = (int)uVar19 >> 0x1f; + if ((int)((uVar19 ^ uVar17) - uVar17) < 0x11 + && (iVar14 + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)( + (int)DAT_UnitsState::instance.units[iVar11].owner, + (dword)((int)(param_5)), (dword)((int)(_param_4_unitUID_Y_copy)), 0), + iVar14 != 0)) + break; + } + unitInstructionType = (UnitInstructionType)(unitInstructionType + 1); + } while ((int)unitInstructionType < 0x10); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + uVar13, x, y, 0); + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_MAN_SIEGE_EQUIPMENT; + DAT_UnitsState::instance.units[uVar13].targetedUnitID__OR__engineerMannedSiegeEngineRef + = (short)id; + DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + unitUID = unitUID + -1; + iVar14 = iVar12; + if (unitUID == 0) { + return (undefined4)(1); + } + } + iVar12 = iVar14; + if (this->tribes[_param_1_tribeID].size <= iVar14) { + return (undefined4)(1); + } + } while (true); } break; - case Map::Units::UT_E_MACE: - iVar11 = 0; - if (0 < _tribeSize) { - do { - iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - _param_1_tribeID, iVar11); - iVar11 = iVar11 + 1; - if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar12].dying == 0 - && (int)(short)DAT_UnitsState::instance.units[iVar12].unitType - 0x27U < 2) { - DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 1; - } - } while (iVar11 < this->tribes[_param_1_tribeID].size); + case Map::Units::UIT_EXIT_SIEGE_EQUIPMENT: + /* + disband siege engines? + */ + _param_4_unitUID_Y_copy = param_5 != 0 ? 0x10 : 0; + param_5 = (int)SEC_RNG::instance.currentNumber2 & 0x8000000f; + if (param_5 < 0) { + param_5 = (param_5 - 1U | 0xfffffff0) + 1; + } + MACRO_CALL_MEMBER(Random::RNG_Func::nextRandomNumber2, SEC_RNG::ptr)(); + if (DAT_UnitsState::instance.units[iVar11].uid == unitUID) { + DAT_UnitsState::instance.units[iVar11].state.generic = Map::Units::States::US_FIRE_WEAPONUnk; + unitInstructionType = ((UnitInstructionType)0); + if (0 < DAT_UnitsState::instance.units[iVar11] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300) { + tribeID = iVar11 * 0x490 + 0x1388860; + do { + _unitID_0x11 = (int)*(short*)tribeID; + *(undefined2*)tribeID = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field249_0x3c0 = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field248_0x3bc = 0; + DAT_UnitsState::instance.units[_unitID_0x11].animationCycleNumber = 0; + DAT_UnitsState::instance.units[_unitID_0x11].state.generic + = Map::Units::States::US_JESTER_ROAM_TO; + DAT_UnitsState::instance.units[_unitID_0x11].disappearFadeAlphaCountdown = 0x20; + DAT_UnitsState::instance.units[_unitID_0x11].engineerManningSiegeStateRef_checkType = 0xfe; + DAT_UnitsState::instance.units[_unitID_0x11].cachedState + = (UnitStateShort)_param_4_unitUID_Y_copy; + UVar4 = DAT_UnitsState::instance.units[iVar11].unitType; + if ((UVar4 == Map::Units::UT_S_MANGONEL) || (UVar4 == Map::Units::UT_S_BALLISTA)) { + DAT_UnitsState::instance.units[_unitID_0x11].goToRallyPoint = 1; + } + DAT_UnitsState::instance.units[_unitID_0x11].updateTickTracker = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field39_0x58 = 1; + unitUID = 0; + LAB_00529da0: + do { + uVar19 = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[param_5][0] + + (int)DAT_UnitsState::instance.units[iVar11].x; + uVar13 = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[param_5][1] + + (int)DAT_UnitsState::instance.units[iVar11].y; + param_5 = param_5 + 1U & 0x8000000f; + if (param_5 < 0) { + param_5 = (param_5 - 1U | 0xfffffff0) + 1; + } + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findWalkableTileThatDoesNotContainUnit, + DAT_PathFindingState::ptr)(id, uVar19, uVar13, 1); + if (DAT_PathFindingState::instance.ALG_ResultTile == 0) { + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findWalkableTileThatDoesNotContainUnit, + DAT_PathFindingState::ptr)(id, uVar19, uVar13, 0); + sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; + sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; + iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; + if (DAT_PathFindingState::instance.ALG_ResultTile != 0) + break; + unitUID = unitUID + 1; + if (unitUID | unitUID != 0x10) + goto LAB_00529da0; + sVar9 = DAT_UnitsState::instance.units[iVar11].x; + sVar8 = DAT_UnitsState::instance.units[iVar11].y; + iVar12 = DAT_UnitsState::instance.units[iVar11].tile; + } else { + sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; + sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; + iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; + } + } while (iVar12 == 0); + bVar2 = DAT_TileMapState::instance.HeightLayer[iVar12]; + DAT_UnitsState::instance.units[_unitID_0x11].totalSizeOfPathPlan = 0; + DAT_UnitsState::instance.units[_unitID_0x11].x = sVar9; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentXPosition = sVar9; + DAT_UnitsState::instance.units[_unitID_0x11].y = sVar8; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentYPosition = sVar8; + DAT_UnitsState::instance.units[_unitID_0x11].terrainOrClimbHeight = (ushort)bVar2; + DAT_UnitsState::instance.units[_unitID_0x11].tile = iVar12; + DAT_UnitsState::instance.units[_unitID_0x11].nextTileUnk = iVar12; + DAT_UnitsState::instance.units[_unitID_0x11].microXPosition = sVar9 * 8 + 4; + DAT_UnitsState::instance.units[_unitID_0x11].microYPosition = sVar8 * 8 + 4; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::updateMicroPosition, DAT_UnitsState::ptr)( + _unitID_0x11); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::resetUnitMovementState, DAT_UnitsState::ptr)( + _unitID_0x11); + tribeID = tribeID + 2; + DAT_UnitsState::instance.units[_unitID_0x11].animationSheetFrameOffset = 1; + DAT_UnitsState::instance.units[_unitID_0x11].field_0x30_animRelated = 0x10; + unitInstructionType = (UnitInstructionType)(unitInstructionType + 1); + } while ((int)unitInstructionType < (int)DAT_UnitsState::instance.units[iVar11] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300); + } + DAT_UnitsState::instance.units[iVar11] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 = 0; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(id); return (undefined4)(1); } break; @@ -1547,339 +1796,102 @@ namespace Map { case Map::Units::UT_A_SWORDSMAN: case Map::Units::UT_A_FIRETHROWER: MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::disbandUnit, DAT_UnitsState::ptr)( - _unitID_0x1e); - break; - case Map::Units::UT_S_CATAPULT: - case Map::Units::UT_S_TREBUCHET: - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_TOWER: - case Map::Units::UT_S_BATTERINGRAM: - case Map::Units::UT_S_SHIELD: - case Map::Units::UT_S_BALLISTA: - case Map::Units::UT_S_FBALLISTA: - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - _param_1_tribeID, Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID_0x1e, - DAT_UnitsState::instance.units[_unitID_0x1e].uid, 1); - _gamemodeSiegeThat = DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT; - DAT_UnitsState::instance.units[_unitID_0x1e].state.generic - = Map::Units::States::US_DISAPPEAR; - if (_gamemodeSiegeThat) { - MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::getPriceForDisbandedUnitType, - DAT_BuildingsState::ptr)( - (Map::Units::UnitType)(DAT_UnitsState::instance.units[_unitID_0x1e].unitType), - &tribeID); - piVar18 = DAT_GameState::instance - .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] - .startResources - + 0xf; - *piVar18 = *piVar18 + tribeID; - } - } - } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); - } - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectAllUnitsOneByOne, DAT_UnitsState::ptr)(); - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)( - this->tribes[_param_1_tribeID].owner, 0, 0); - return (undefined4)(1); - case Map::Units::UIT_STOP: - iVar11 = 0; - this->tribes[tribeID].isRallyingUnk = 0; - this->tribes[tribeID].someUnitID = 0; - if (0 < _tribeSize) { - do { - iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar11); - iVar11 = iVar11 + 1; - if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar12].dying == 0 - && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0) { - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 3; - DAT_UnitsState::instance.units[iVar12].targetingType - = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; - DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 0; - MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(iVar12); - DAT_UnitsState::instance.units[iVar12].targetedBuildingTile = 0; - DAT_UnitsState::instance.units[iVar12].movementType_OR_targetUnitID = 0; - } - } while (iVar11 < this->tribes[_param_1_tribeID].size); - return (undefined4)(1); - } - break; - case ((UnitInstructionType)0x20): - case Map::Units::UIT_SHOOT_TARGETUnk: - local_8 = id * 0x490; - pUVar6 = DAT_UnitsState::instance.units + id; - _unitTribeIndex = 0; - id = 0; - if (pUVar6->uid != unitUID) { - return (undefined4)(0); - } - if (0 < _tribeSize) { - do { - iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitTribeIndex); - _unitTribeIndex = _unitTribeIndex + 1; - if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar12].dying == 0 - && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0 - && (UVar3 = DAT_UnitsState::instance.units[iVar12].state.generic, - UVar3 != Map::Units::States::US_MELEE_ATTACK)) { - switch (DAT_UnitsState::instance.units[iVar12].unitType) { - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_KNIGHT: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { - id = id + 1; - } - break; - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_BALLISTA: - case Map::Units::UT_S_FBALLISTA: - if (unitInstructionType == Map::Units::UIT_SHOOT_TARGETUnk) { - break; - } - DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; - /* - and on into the ranged arm, which is what the - decompiler's goto back into it said - */ - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_E_ARCHER_DEBUG: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_HARCHER: - case Map::Units::UT_A_FIRETHROWER: - if (UVar3 == (UnitState)0x69) { - DAT_UnitsState::instance.units[iVar12].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - } - DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_UNIT_ATTACK_UNIT; - DAT_UnitsState::instance.units[iVar12].targetedUnitID__OR__engineerMannedSiegeEngineRef - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar12] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = unitUID; - DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; - if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { - this->tribes[_param_1_tribeID].containsRabbit = 1; - } - if (DAT_UnitsState::instance.units[iVar12]._someX_2 == 0) { - sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; - DAT_UnitsState::instance.units[iVar12]._someX_2 - = DAT_UnitsState::instance.units[iVar12].destinationX_2Unk; - DAT_UnitsState::instance.units[iVar12]._someY_2 = sVar9; - } - MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(iVar12); + _unitID_0x1e); break; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TREBUCHET: - if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { - sVar9 = DAT_UnitsState::instance.units[iVar12]._someX_2; - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 5; - DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_LAND; - DAT_UnitsState::instance.units[iVar12].attackAtTileX - = DAT_UnitsState::instance.units[iVar11].x; - DAT_UnitsState::instance.units[iVar12].attackAtTileY - = DAT_UnitsState::instance.units[iVar11].y; - DAT_UnitsState::instance.units[iVar12].unkAttackRelated = 0xb; - if (sVar9 == 0) { - sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; - DAT_UnitsState::instance.units[iVar12]._someX_2 - = DAT_UnitsState::instance.units[iVar12].destinationX_2Unk; - DAT_UnitsState::instance.units[iVar12]._someY_2 = sVar9; - } - MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(iVar12); - DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_TOWER: + case Map::Units::UT_S_BATTERINGRAM: + case Map::Units::UT_S_SHIELD: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + _param_1_tribeID, Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID_0x1e, + DAT_UnitsState::instance.units[_unitID_0x1e].uid, 1); + _gamemodeSiegeThat = DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT; + DAT_UnitsState::instance.units[_unitID_0x1e].state.generic + = Map::Units::States::US_DISAPPEAR; + if (_gamemodeSiegeThat) { + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::getPriceForDisbandedUnitType, + DAT_BuildingsState::ptr)( + (Map::Units::UnitType)(DAT_UnitsState::instance.units[_unitID_0x1e].unitType), + &tribeID); + piVar18 = DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .startResources + + 0xf; + *piVar18 = *piVar18 + tribeID; } - break; } } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); - /* - The melee counter against zero, as at the first site. - */ - if (id != 0) { - iVar12 = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; - param_5 = (int)DAT_UnitsState::instance.units[iVar12].x; - unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[iVar12].y; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( - iVar12, iVar11, ¶m_5, (int*)&unitInstructionType); - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathPlanningForTribe, - DAT_PathFindingState::ptr)(tribeID, (undefined4)((int)(iVar11)), (uint)(param_5), - (uint)(unitInstructionType), id, - (dword)((int)((short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[iVar12].tile])), - (int)(DAT_UnitsState::instance.units[iVar12].owner)); - this->tribes[_param_1_tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; - this->tribes[_param_1_tribeID].someUnitUID = unitUID; - this->tribes[_param_1_tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; - iVar12 = 0; - sVar9 = this->tribes[_param_1_tribeID].size; - this->tribes[_param_1_tribeID].someTile2 - = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk - + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] - .addXgetTile; - unitInstructionType = ((UnitInstructionType)0); - if (0 < sVar9) { - id = 0x12d5c6c; - do { - iVar14 = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); - iVar12 = iVar12 + 1; - if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar14].dying == 0 - && DAT_UnitsState::instance.units[iVar14].field303_0x413 == 0 - && DAT_UnitsState::instance.units[iVar14].state.generic - != Map::Units::States::US_MELEE_ATTACK) { - switch (DAT_UnitsState::instance.units[iVar14].unitType) { - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_KNIGHT: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - _tile1_528565 = *(int*)id; - if (_tile1_528565 != 0) { - _y_528565 = DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[_tile1_528565]; - _x_528565 = _tile1_528565 - - DAT_ViewportRenderState::instance.translationMatrix[_y_528565] - .addXgetTile; - if (*(int*)(id + 4) == 0) { - DAT_UnitsState::instance.units[iVar14].targetingType - = ((UnitInstructionType)0); - if (DAT_UnitsState::instance.units[iVar14].unitType - == Map::Units::UT_LORD) - break; - } else { - DAT_UnitsState::instance.units[iVar14].targetingType - = Map::Units::UIT_UNIT_ATTACK_UNIT; - DAT_UnitsState::instance.units[iVar14] - .targetedUnitID__OR__engineerMannedSiegeEngineRef - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar14] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = unitUID; - } - DAT_UnitsState::instance.units[iVar14].plannedDestinationX - = (short)_x_528565; - DAT_UnitsState::instance.units[iVar14].plannedDestinationY = _y_528565; - DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; - if (DAT_UnitsState::instance.units[iVar14].state.generic - == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[iVar14].movementCooldown - = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; - } - if (DAT_UnitsState::instance.units[iVar14]._someX_2 == 0) { - DAT_UnitsState::instance.units[iVar14]._someX_2 - = DAT_UnitsState::instance.units[iVar14].destinationX_2Unk; - DAT_UnitsState::instance.units[iVar14]._someY_2 - = DAT_UnitsState::instance.units[iVar14].destinationY_2Unk; - } - DAT_UnitsState::instance.units[iVar14].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - if (MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID) - == 0) { - DAT_PathFindingState::instance.notAllAssassinsUnk = 1; - } else { - DAT_PathFindingState::instance.allAssassinsUnk = 1; - } - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(iVar14, _x_528565, (uint)(_y_528565), 0); - DAT_UnitsState::instance.units[iVar14].moveDelay = 0; - DAT_UnitsState::instance.units[iVar14].lookForEnemy = -0x32; - if (DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID - != iVar11) { - DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID - = _param_3_id_x_tile_buildingID_copy; - psVar1 = (short*)((int)&DAT_UnitsState::instance.units[0].huntedBy - + local_8); - *psVar1 = *psVar1 + 1; - } - UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[iVar14] - .totalSizeOfPathPlan - / 2); - if ((int)unitInstructionType < (int)UVar10) { - unitInstructionType = UVar10; - } - id = id + 0xc; - DAT_PathFindingState::instance.notAllAssassinsUnk = 0; - } - } - } - } while (iVar12 < this->tribes[_param_1_tribeID].size); - } - iVar11 = unitInstructionType * 8; - if (iVar11 < 9) { - iVar11 = 8; - } - this->tribes[_param_1_tribeID].field166_0x2be = (short)iVar11; - return (undefined4)(1); - } } - break; - case Map::Units::UIT_LIGHT_PITCH: + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectAllUnitsOneByOne, DAT_UnitsState::ptr)(); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)( + this->tribes[_param_1_tribeID].owner, 0, 0); + return (undefined4)(1); + case ((UnitInstructionType)0x15): this->tribes[tribeID].isRallyingUnk = 0; + uVar13 = (uint)DAT_BuildingsState::instance.buildings[id].buildingEntryY; _unitTribeIndex = 0; - if ((DAT_TileMapState::instance.pitchDitches[id].uid == unitUID) && (0 < _tribeSize)) { + BVar16 + = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)( + (int)DAT_BuildingsState::instance.buildings[id].buildingEntryX, uVar13); + if (!BVar16) { + return (undefined4)(0); + } + param_5 + = (int)(short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[uVar13].addXgetTile + + (int)DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX]; + if (0 < _tribeSize) { do { - iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, _unitTribeIndex); _unitTribeIndex = _unitTribeIndex + 1; - if ((DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar12].dying == 0)) { - DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; - DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; - UVar4 = DAT_UnitsState::instance.units[iVar12].unitType; - if ((UVar4 == Map::Units::UT_E_ARCHER) || (UVar4 == Map::Units::UT_A_ARCHER)) { - sVar9 = DAT_TileMapState::instance.pitchDitches[iVar11].y; - DAT_UnitsState::instance.units[iVar12].shootTargetMicroX - = DAT_TileMapState::instance.pitchDitches[iVar11].x * 8 + 4; - iVar14 = DAT_TileMapState::instance.pitchDitches[iVar11].tile; - DAT_UnitsState::instance.units[iVar12].shootTargetMicroY = sVar9 * 8 + 4; - DAT_UnitsState::instance.units[iVar12].shootTargetZ - = (ushort)DAT_TileMapState::instance.HeightLayer[iVar14]; - DAT_UnitsState::instance.units[iVar12].shootTargetedUnit = -1; - DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID + if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[uVar13].dying == 0 + && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_TUNNELER) { + DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; + DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; + iVar12 = MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::buildingIsAccessible, + DAT_BuildingsState::ptr)(iVar11, 1); + if (iVar12 != 0 + && (iVar12 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, + (dword)((int)(param_5)), + (dword)((int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile])), + 0), + iVar12 != 0)) { + sVar9 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryY; + sVar8 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX; + iVar11 = DAT_BuildingsState::instance.buildings[iVar11].uid; + DAT_UnitsState::instance.units[uVar13].workplaceBuildingID_1 = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar12] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = unitUID; - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 0x22; - DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_LIGHT_PITCH; - DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; - MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(iVar12); - DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; + DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[uVar13].targetUID = iVar11; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, + DAT_UnitsState::ptr)(uVar13, (uint)(sVar8), (uint)(sVar9), 0); + DAT_UnitsState::instance.units[uVar13].state.generic + = Map::Units::States::US_LOOK_AROUNDUnk; + DAT_UnitsState::instance.units[uVar13].targetingType = ((UnitInstructionType)0x15); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( + uVar13); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, + DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + return (undefined4)(1); } } } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); - return (undefined4)(1); } + DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( + iVar11); + break; } return (undefined4)(1); } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 06feef720e..7575f2ef96 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 27.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 51.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From 7ae5890f0a9d14b67e5aa0fd7166d49cdc120721 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 00:23:05 +0200 Subject: [PATCH 070/202] measure: the nested switches' arm order does not pay, only the outer one Both nested switches whose jump tables I could read were reordered into their table order and measured. Putting the archer arm of 0x528e3a first, so that it falls through into the block below exactly as 0x528e41 does, costs 0.9 points; reordering the eight arms of 0x5290c7 adds 0.1 of that back. Recorded at the switch; no code change. Once the outer arms sit at the right addresses, MSVC lays out the blocks inside them to its own taste. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/giveTribeAnInstruction.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index af61452871..809dbfb21c 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -131,6 +131,15 @@ namespace Map { this->tribes[tribeID].containsRabbit = 0; _param_3_id_x_tile_buildingID_copy = (short)id; uVar13 = id; + /* + The arm order here is the original's block order and is worth 23.9 points, but + the nested switches' arm order is not. Two were tried against their own jump + tables and both measure worse or flat: putting the archer arm of 0x528e3a + first so that it falls through into the block below it, which is exactly what + 0x528e41 does, costs 0.9 points; and reordering the eight arms of 0x5290c7 + into their table order adds 0.1 back. Once the arms are at the right + addresses, MSVC lays out the blocks inside them to its own taste. + */ switch (unitInstructionType) { case Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk: _unitSelectionIndex = 0; From 2be1e008c951a3185dd9577cef9113bdd3e7160c Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 09:23:01 +0200 Subject: [PATCH 071/202] measure: why the Tribe& does not pay in giveTribeAnInstruction either The original does take the tribe's address once (`lea ebp, [eax + ecx]` at 0x527c93, spilled to [esp + 0x18]) and reach every field as `[ebp + ...]`, and recomputing tribeID * 0x334 per subscript is what puts three extra dwords in our frame - 0x28 against 0x1c - which shifts every `[esp + N]` by 0xc. 675 of the 3041 differing lines carry an `[esp + N]` operand and the dominant delta is exactly +12. But binding all 74 subscripts to a `Tribe&` removes only one of the three extra slots and costs 2.8 points (51.9% -> 49.1%), because the frame then measures 0x24 and the offsets mismatch by 8 instead of 12. Partial credit is worth nothing: the frame has to land exactly on 0x1c. The two remaining extras are one use each, which makes them argument-shuffling temporaries rather than locals this file can drop. Recorded at the function; no code change. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 809dbfb21c..d68616b801 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -122,6 +122,24 @@ namespace Map { _param_4_unitUID_Y = unitUID; iVar11 = id; _param_1_tribeID = tribeID; + /* + The subscripts are repeated rather than bound to a `Tribe&`, and the reason is + worth writing down because the evidence points the other way. The original + takes the tribe's address once before it reads anything - `lea ebp, [eax + + ecx]` at 0x527c93, spilled to [esp + 0x18] at 0x527cc4 - and reaches every + tribe field as `[ebp + ...]`: `[ebp + 0x5c]` for the size, `[ebp + 0x2e8]` and + `[ebp + 0x23a]` for the two stores below. + + Recomputing `tribeID * 0x334` per subscript is indeed what puts extra dwords + in our frame: 0x28 against the original's 0x1c, which shifts every `[esp + N]` + in the function by 0xc, and 675 of the 3041 differing lines carry an + `[esp + N]` operand. Binding all 74 subscripts to a reference does remove one + of the three extra slots - but it costs 2.8 points (51.9% -> 49.1%), because + the frame then measures 0x24 and every stack offset mismatches by 8 instead of + 12. Partial credit is worth nothing here; the frame has to land exactly on + 0x1c. The two slots still over are one use each, which makes them compiler + temporaries for argument shuffling rather than locals this file can drop. + */ _tribeSize = this->tribes[tribeID].size; _param_4_unitUID_Y_copy = unitUID; _ramOrHorse = 0; From 08f5c18de3b5bf7eddad51dce9f8522b395035ed Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 09:36:06 +0200 Subject: [PATCH 072/202] reimplement: giveTribeAnInstruction's frame is now exactly the original's 0x1c Three changes together take the frame from 0x28 to 0x1c, and diff_slots.py now reports the same sixteen slots on both sides with nothing left over: - the tribe's address is taken once, as the original does before it reads anything (`lea ebp, [eax + ecx]` at 0x527c93, spilled to [esp + 0x18]); - the tribeID copy goes with it, the subscripts having been its only real users; - the two unitUID copies become one, their live ranges being in different switch arms. The gain is 0.1 points raw (51.9% -> 52.0%) and 1.1 normalized. That contradicts what I expected and the reason is recorded at the function: 675 differing lines carry an `[esp + N]` operand and the dominant delta was exactly the 0xc the frame was over, but the offsets were never the whole story. Which value sits in which slot still differs - the original's hottest local is [esp + 0x10] with 53 accesses where ours has 32 - and that follows from declaration order. Kept anyway: the frame matches, the redundant copies are gone, and the tribe base is bound the way the original binds it. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 190 ++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 107 insertions(+), 85 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index d68616b801..31f636e6fd 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -104,7 +104,6 @@ namespace Map { dword _param_4_unitUID_Y_copy; int _tile1_528565; short _y_528565; - int _param_1_tribeID; /* An `int`: the original reads the size with `movsx esi, word ptr [ebp + 0x5c]` at 0x527c99 and compares it 32 bits wide (`cmp esi, edx`), where a `short` @@ -117,11 +116,34 @@ namespace Map { short _freeTileY; short _y; short _x; - int _param_4_unitUID_Y; ushort _targetArea; - _param_4_unitUID_Y = unitUID; iVar11 = id; - _param_1_tribeID = tribeID; + /* + Three things here together make our frame exactly the original's 0x1c, down + from 0x28, and diff_slots.py now reports the same sixteen slots on both sides + with nothing left over: + + - the tribe's address is taken once, as the original does before it reads + anything (`lea ebp, [eax + ecx]` at 0x527c93, spilled to [esp + 0x18] at + 0x527cc4), so every field below is one base plus an offset instead of a + fresh `tribeID * 0x334`; + - the tribeID copy goes with it, since the subscripts were its only real + users and the four remaining argument passes can use the parameter - it is + only reassigned inside the EXIT_SIEGE arm, and nothing that reads it runs + in that arm; + - the two unitUID copies become one. `_param_4_unitUID_Y` was read only in + the ATTACK_LAND arm and `_param_4_unitUID_Y_copy` only in the siege arms, + so the live ranges never overlap and one slot does for both. + + Worth saying plainly: getting the frame exactly right was expected to be a + large gain, because 675 of the differing lines carry an `[esp + N]` operand + and the dominant delta was exactly the 0xc the frame was over. It is not - + 52.0% against 51.9%, normalized 41.7% against 40.6%. The offsets were never + the whole story: which value sits in which slot still differs (the original's + hottest local is [esp + 0x10] with 53 accesses, ours has 32 there), and that + follows from declaration order, which would need a permutation search. + */ + Tribe& _tribe = this->tribes[tribeID]; /* The subscripts are repeated rather than bound to a `Tribe&`, and the reason is worth writing down because the evidence points the other way. The original @@ -140,13 +162,13 @@ namespace Map { 0x1c. The two slots still over are one use each, which makes them compiler temporaries for argument shuffling rather than locals this file can drop. */ - _tribeSize = this->tribes[tribeID].size; + _tribeSize = _tribe.size; _param_4_unitUID_Y_copy = unitUID; _ramOrHorse = 0; local_8 = 0; bVar5 = FALSE; - this->tribes[tribeID].someUnitID = 0; - this->tribes[tribeID].containsRabbit = 0; + _tribe.someUnitID = 0; + _tribe.containsRabbit = 0; _param_3_id_x_tile_buildingID_copy = (short)id; uVar13 = id; /* @@ -164,7 +186,7 @@ namespace Map { if (0 < _tribeSize) { do { _unitID_0x03 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - _param_1_tribeID, _unitSelectionIndex); + tribeID, _unitSelectionIndex); _unitSelectionIndex = _unitSelectionIndex + 1; if ((DAT_UnitsState::instance.units[_unitID_0x03].logicalState == Map::Units::ULS_NORMAL) && (DAT_UnitsState::instance.units[_unitID_0x03].dying == 0)) { @@ -173,14 +195,14 @@ namespace Map { = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; DAT_UnitsState::instance.units[_unitID_0x03].field243_0x3b0 = 0; } - } while (_unitSelectionIndex < this->tribes[_param_1_tribeID].size); + } while (_unitSelectionIndex < _tribe.size); return (undefined4)(1); } break; case Map::Units::UIT_STOP: iVar11 = 0; - this->tribes[tribeID].isRallyingUnk = 0; - this->tribes[tribeID].someUnitID = 0; + _tribe.isRallyingUnk = 0; + _tribe.someUnitID = 0; if (0 < _tribeSize) { do { iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -199,7 +221,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].targetedBuildingTile = 0; DAT_UnitsState::instance.units[iVar12].movementType_OR_targetUnitID = 0; } - } while (iVar11 < this->tribes[_param_1_tribeID].size); + } while (iVar11 < _tribe.size); return (undefined4)(1); } break; @@ -207,7 +229,7 @@ namespace Map { /* unit attack target */ - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; pUVar6 = DAT_UnitsState::instance.units + id; _unitTribeIndex = 0; /* @@ -306,10 +328,10 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_0x04].shootBeforeStop = 10; LAB_00527ee1: if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { - this->tribes[_param_1_tribeID].containsRabbit = 1; + _tribe.containsRabbit = 1; } } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); /* `id` is zeroed before the loop and counted up in the melee arm, so this asks whether the selection held any melee units. The original is @@ -321,7 +343,7 @@ namespace Map { /* if there are melee units in the selection */ - _targetUnitID = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; + _targetUnitID = (int)_tribe.selectionTargetUnitID; local_8 = (uint)DAT_UnitsState::instance.units[_targetUnitID].x; unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[_targetUnitID].y; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( @@ -332,12 +354,12 @@ namespace Map { (dword)((int)((short)DAT_TileMapState::instance .PathConnectionLayer[DAT_UnitsState::instance.units[_targetUnitID].tile])), (int)(DAT_UnitsState::instance.units[_targetUnitID].owner)); - this->tribes[_param_1_tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; - this->tribes[_param_1_tribeID].someUnitUID = unitUID; - this->tribes[_param_1_tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; + _tribe.someUnitID = _param_3_id_x_tile_buildingID_copy; + _tribe.someUnitUID = unitUID; + _tribe.someTile = DAT_UnitsState::instance.units[iVar11].tile; iVar12 = 0; - sVar9 = this->tribes[_param_1_tribeID].size; - this->tribes[_param_1_tribeID].someTile2 + sVar9 = _tribe.size; + _tribe.someTile2 = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + DAT_ViewportRenderState::instance .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] @@ -421,13 +443,13 @@ namespace Map { } } } - } while (iVar12 < this->tribes[_param_1_tribeID].size); + } while (iVar12 < _tribe.size); } iVar11 = unitInstructionType * 8; if (iVar11 < 9) { iVar11 = 8; } - this->tribes[_param_1_tribeID].field166_0x2be = (short)iVar11; + _tribe.field166_0x2be = (short)iVar11; return (undefined4)(1); } } @@ -497,7 +519,7 @@ namespace Map { = unitUID; DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { - this->tribes[_param_1_tribeID].containsRabbit = 1; + _tribe.containsRabbit = 1; } if (DAT_UnitsState::instance.units[iVar12]._someX_2 == 0) { sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; @@ -534,12 +556,12 @@ namespace Map { break; } } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); /* The melee counter against zero, as at the first site. */ if (id != 0) { - iVar12 = (int)this->tribes[_param_1_tribeID].selectionTargetUnitID; + iVar12 = (int)_tribe.selectionTargetUnitID; param_5 = (int)DAT_UnitsState::instance.units[iVar12].x; unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[iVar12].y; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( @@ -550,12 +572,12 @@ namespace Map { (dword)((int)((short)DAT_TileMapState::instance .PathConnectionLayer[DAT_UnitsState::instance.units[iVar12].tile])), (int)(DAT_UnitsState::instance.units[iVar12].owner)); - this->tribes[_param_1_tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; - this->tribes[_param_1_tribeID].someUnitUID = unitUID; - this->tribes[_param_1_tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; + _tribe.someUnitID = _param_3_id_x_tile_buildingID_copy; + _tribe.someUnitUID = unitUID; + _tribe.someTile = DAT_UnitsState::instance.units[iVar11].tile; iVar12 = 0; - sVar9 = this->tribes[_param_1_tribeID].size; - this->tribes[_param_1_tribeID].someTile2 + sVar9 = _tribe.size; + _tribe.someTile2 = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + DAT_ViewportRenderState::instance .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] @@ -654,13 +676,13 @@ namespace Map { } } } - } while (iVar12 < this->tribes[_param_1_tribeID].size); + } while (iVar12 < _tribe.size); } iVar11 = unitInstructionType * 8; if (iVar11 < 9) { iVar11 = 8; } - this->tribes[_param_1_tribeID].field166_0x2be = (short)iVar11; + _tribe.field166_0x2be = (short)iVar11; return (undefined4)(1); } } @@ -670,21 +692,21 @@ namespace Map { if (0 < _tribeSize) { do { iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - _param_1_tribeID, iVar11); + tribeID, iVar11); iVar11 = iVar11 + 1; if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[iVar12].dying == 0 && (int)(short)DAT_UnitsState::instance.units[iVar12].unitType - 0x27U < 2) { DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 1; } - } while (iVar11 < this->tribes[_param_1_tribeID].size); + } while (iVar11 < _tribe.size); return (undefined4)(1); } break; case Map::Units::UIT_ATTACK_LAND: case Map::Units::UIT_THROW_COW: - unitUID = this->tribes[tribeID].owner; - this->tribes[tribeID].isRallyingUnk = 0; + unitUID = _tribe.owner; + _tribe.isRallyingUnk = 0; _unitTribeIndex = 0; if (0 < _tribeSize) { do { @@ -715,7 +737,7 @@ namespace Map { LAB_00528808: DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 = (byte)unitInstructionType; - sVar9 = (short)_param_4_unitUID_Y; + sVar9 = (short)_param_4_unitUID_Y_copy; if (unitInstructionType == Map::Units::UIT_THROW_COW && (UVar4 = DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType, UVar4 == Map::Units::UT_S_CATAPULT || (UVar4 == Map::Units::UT_S_TREBUCHET))) { @@ -729,7 +751,7 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetMicroY = sVar9 * 8; DAT_UnitsState::instance.units[_unitID_0x05_0x16].shootTargetZ = (ushort) * (byte*)(DAT_ViewportRenderState::instance - .translationMatrix[_param_4_unitUID_Y] + .translationMatrix[_param_4_unitUID_Y_copy] .addXgetTile + 0x1d32c38 + iVar11); } @@ -753,7 +775,7 @@ namespace Map { goto LAB_00528808; } } - if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { + if (_tribe.size <= _unitTribeIndex) { return (undefined4)(1); } } while (true); @@ -761,7 +783,7 @@ namespace Map { break; case Map::Units::UIT_THROW_OIL: iVar11 = 0; - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; if (0 < _tribeSize) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -778,16 +800,16 @@ namespace Map { DAT_UnitsState::instance.units[uVar13].plannedDestinationY = (short)unitUID; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; } - } while (iVar11 < this->tribes[_param_1_tribeID].size); + } while (iVar11 < _tribe.size); return (undefined4)(1); } break; case Map::Units::UIT_LIGHT_PITCH: - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; _unitTribeIndex = 0; if ((DAT_TileMapState::instance.pitchDitches[id].uid == unitUID) && (0 < _tribeSize)) { do { @@ -822,7 +844,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); return (undefined4)(1); } /* @@ -836,9 +858,9 @@ namespace Map { case ((UnitInstructionType)0x26): _unitTribeIndex = 0; id = 0; - this->tribes[tribeID].targetBuildingID = _param_3_id_x_tile_buildingID_copy; - this->tribes[tribeID].targetBuildingUID = unitUID; - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.targetBuildingID = _param_3_id_x_tile_buildingID_copy; + _tribe.targetBuildingUID = unitUID; + _tribe.isRallyingUnk = 0; if (0 < _tribeSize) { do { iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -957,7 +979,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); } if ((((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) @@ -967,7 +989,7 @@ namespace Map { as `id | x | id != 0`. */ && (id != 0)) { - sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; + sVar9 = _tribe.selectionTargetUnitID; if (unitInstructionType == Map::Units::UIT_ATTACK_BUILDING || unitInstructionType == (UnitInstructionType)0x26) { iVar12 = (int)DAT_UnitsState::instance.units[sVar9].owner; @@ -982,7 +1004,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( tribeID, _ramOrHorse); if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) - && (_unitTribeIndex = 0, 0 < this->tribes[_param_1_tribeID].size)) { + && (_unitTribeIndex = 0, 0 < _tribe.size)) { id = 0x12d5c70; do { _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1100,7 +1122,7 @@ namespace Map { goto switchD_00528e3a_caseD_5; } } - if (this->tribes[_param_1_tribeID].size <= _unitTribeIndex) { + if (_tribe.size <= _unitTribeIndex) { return (undefined4)(1); } } while (true); @@ -1112,7 +1134,7 @@ namespace Map { case ((UnitInstructionType)0x25): _unitTribeIndex = 0; id = 0; - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; if (0 < _tribeSize) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1216,7 +1238,7 @@ namespace Map { id = id + 1; } } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); } if ((DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) && (DAT_GameState::instance.mapAndTime.skirmishStrongWalls != 0)) { @@ -1226,7 +1248,7 @@ namespace Map { The melee counter against zero again, as at the two sites above. */ if (id != 0) { - sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; + sVar9 = _tribe.selectionTargetUnitID; if (unitInstructionType == Map::Units::UIT_ATTACK_WALL || unitInstructionType == (UnitInstructionType)0x25) { sVar8 = DAT_UnitsState::instance.units[sVar9].facingDirection; @@ -1244,7 +1266,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( tribeID, _ramOrHorse); if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) - && (iVar12 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + && (iVar12 = 0, 0 < _tribe.size)) { id = 0x12d5c70; do { iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1371,7 +1393,7 @@ namespace Map { } } } - if (this->tribes[_param_1_tribeID].size <= iVar12) { + if (_tribe.size <= iVar12) { return (undefined4)(1); } } while (true); @@ -1381,7 +1403,7 @@ namespace Map { case Map::Units::UIT_SET_LADDER: iVar12 = 0; id = 0; - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; if (0 < _tribeSize) { do { iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1394,12 +1416,12 @@ namespace Map { DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; } - } while (iVar12 < this->tribes[_param_1_tribeID].size); + } while (iVar12 < _tribe.size); /* The melee counter against zero, as at the first site. */ if (id != 0) { - sVar9 = this->tribes[_param_1_tribeID].selectionTargetUnitID; + sVar9 = _tribe.selectionTargetUnitID; MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: populateDestinationsArrayWithWallTileAndFreeTilePairInSameArea, DAT_PathFindingState::ptr)(iVar11, id, @@ -1409,7 +1431,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( tribeID, 0); if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) - && (iVar11 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + && (iVar11 = 0, 0 < _tribe.size)) { piVar18 = &DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper; do { _laddermanID = MACRO_CALL_MEMBER( @@ -1458,7 +1480,7 @@ namespace Map { DAT_UnitsState::instance.units[_laddermanID].attackAtTileX = (short)_defensiveTile1 - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; } - } while (iVar11 < this->tribes[_param_1_tribeID].size); + } while (iVar11 < _tribe.size); return (undefined4)(1); } } @@ -1466,12 +1488,12 @@ namespace Map { break; case Map::Units::UIT_FILL_MOAT: _targetArea = DAT_TileMapState::instance.PathConnectionLayer - [DAT_UnitsState::instance.units[this->tribes[tribeID].selectionTargetUnitID].tile]; - this->tribes[tribeID].isRallyingUnk = 0; + [DAT_UnitsState::instance.units[_tribe.selectionTargetUnitID].tile]; + _tribe.isRallyingUnk = 0; if ((int)(short)_targetArea != 0) { iVar11 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, DAT_PathFindingState::ptr)( - this->tribes[tribeID].owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); + _tribe.owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); if (iVar11 == 0) { return (undefined4)(1); } @@ -1481,10 +1503,10 @@ namespace Map { case Map::Units::UIT_DIG_MOAT: iVar11 = unitUID; iVar12 = 0; - this->tribes[_param_1_tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( tribeID, uVar13, (uint)(unitUID), 0, 1); - if (0 < this->tribes[_param_1_tribeID].size) { + if (0 < _tribe.size) { do { iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); @@ -1514,12 +1536,12 @@ namespace Map { DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; } } - } while (iVar12 < this->tribes[_param_1_tribeID].size); + } while (iVar12 < _tribe.size); return (undefined4)(1); } break; case Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED: - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; _y = DAT_BuildingsState::instance.buildings[id].buildingEntryY; _x = DAT_BuildingsState::instance.buildings[id].buildingEntryX; _unitTribeIndex = 0; @@ -1536,7 +1558,7 @@ namespace Map { if (((unitUID | unitUID != 0) || (DAT_BuildingsState::instance.buildings[iVar11].buildingType == Map::Buildings::BT_OILSMELTER)) - && 0 < this->tribes[_param_1_tribeID].size) { + && 0 < _tribe.size) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, _unitTribeIndex); @@ -1598,7 +1620,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); unitUID = unitUID + -1; iVar12 = _unitTribeIndex; if (unitUID == 0) { @@ -1608,7 +1630,7 @@ namespace Map { } } _unitTribeIndex = iVar12; - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); return (undefined4)(1); } break; @@ -1616,12 +1638,12 @@ namespace Map { /* manning siege equipment? */ - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; if (DAT_UnitsState::instance.units[id].uid == unitUID && (unitUID = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(id), unitUID | unitUID != 0) - && (iVar12 = 0, 0 < this->tribes[_param_1_tribeID].size)) { + && (iVar12 = 0, 0 < _tribe.size)) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); @@ -1678,7 +1700,7 @@ namespace Map { DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); unitUID = unitUID + -1; iVar14 = iVar12; if (unitUID == 0) { @@ -1686,7 +1708,7 @@ namespace Map { } } iVar12 = iVar14; - if (this->tribes[_param_1_tribeID].size <= iVar14) { + if (_tribe.size <= iVar14) { return (undefined4)(1); } } while (true); @@ -1794,12 +1816,12 @@ namespace Map { /* disband? */ - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; _unitTribeIndex = 0; if (0 < _tribeSize) { do { _unitID_0x1e = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - _param_1_tribeID, _unitTribeIndex); + tribeID, _unitTribeIndex); _unitTribeIndex = _unitTribeIndex + 1; if ((DAT_UnitsState::instance.units[_unitID_0x1e].logicalState == Map::Units::ULS_NORMAL) && (DAT_UnitsState::instance.units[_unitID_0x1e].dying == 0)) { @@ -1834,7 +1856,7 @@ namespace Map { case Map::Units::UT_S_BALLISTA: case Map::Units::UT_S_FBALLISTA: MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - _param_1_tribeID, Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID_0x1e, + tribeID, Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID_0x1e, DAT_UnitsState::instance.units[_unitID_0x1e].uid, 1); _gamemodeSiegeThat = DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT; DAT_UnitsState::instance.units[_unitID_0x1e].state.generic @@ -1852,14 +1874,14 @@ namespace Map { } } } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); } MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectAllUnitsOneByOne, DAT_UnitsState::ptr)(); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)( - this->tribes[_param_1_tribeID].owner, 0, 0); + _tribe.owner, 0, 0); return (undefined4)(1); case ((UnitInstructionType)0x15): - this->tribes[tribeID].isRallyingUnk = 0; + _tribe.isRallyingUnk = 0; uVar13 = (uint)DAT_BuildingsState::instance.buildings[id].buildingEntryY; _unitTribeIndex = 0; BVar16 @@ -1909,11 +1931,11 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(this->tribes[_param_1_tribeID].owner, uVar13, 0); + DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); return (undefined4)(1); } } - } while (_unitTribeIndex < this->tribes[_param_1_tribeID].size); + } while (_unitTribeIndex < _tribe.size); } DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 7575f2ef96..50d1c70e2b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 51.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 52.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From cb3a4b8edcfda9164fd18572b8c1736fe5248139 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 09:42:41 +0200 Subject: [PATCH 073/202] measure: nested switch arm order never pays, tested three times 0x52a308 was the last one worth trying - 128 of the remaining differing lines were its big arm sitting first where the table puts it third. Moving it costs 0.3 points, which makes three for three: 0x528e3a archer arm first, falling through as 0x528e41 does -0.9 0x5290c7 eight arms into table order +0.1 0x52a308 big arm to third, where 0x52838a puts it -0.3 The rule is about scope. The outer switch fixes where each hundred-instruction arm begins and getting it wrong misaligns everything downstream, which is why it was worth 23.9 points. Inside an arm the blocks are small and MSVC lays them out to its own branch heuristics. Consolidated into one note at the outer switch so nobody re-runs them. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 31f636e6fd..531f06bd82 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -172,13 +172,20 @@ namespace Map { _param_3_id_x_tile_buildingID_copy = (short)id; uVar13 = id; /* - The arm order here is the original's block order and is worth 23.9 points, but - the nested switches' arm order is not. Two were tried against their own jump - tables and both measure worse or flat: putting the archer arm of 0x528e3a - first so that it falls through into the block below it, which is exactly what - 0x528e41 does, costs 0.9 points; and reordering the eight arms of 0x5290c7 - into their table order adds 0.1 back. Once the arms are at the right - addresses, MSVC lays out the blocks inside them to its own taste. + The arm order here is the original's block order and is worth 23.9 points. The + nested switches' arm order is not worth anything, and that has now been tested + three times, each against the switch's own jump table: + + - 0x528e3a: putting the archer arm first so it falls through into the block + below, which is exactly what 0x528e41 does, costs 0.9 points; + - 0x5290c7: reordering its eight arms into table order adds 0.1 back; + - 0x52a308: moving the big arm to third, where 0x52838a puts it, costs 0.3. + + So the rule is about scope, not about ordering as such. The outer switch fixes + where each hundred-instruction arm begins, and getting that wrong misaligns + everything downstream - which is why it was worth 23.9. Inside an arm the + blocks are small and MSVC lays them out to its own branch heuristics, so + forcing source order on them just fights the compiler. Do not re-run these. */ switch (unitInstructionType) { case Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk: From 53d20461186b7fd8b4e6b6fac7d4779c56bbfad5 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 10:15:30 +0200 Subject: [PATCH 074/202] measure: declaration order cannot match giveTribeAnInstruction's slots, by proof The frame now matches, which is the precondition perm_decls.py documents, so I ran it over the five independent initialisations. It is a dead end and the reason is provable rather than empirical: reordering declarations only permutes which slot carries which access count, and the two multisets are not equal. original 6, 9, 10, 14, 24, 25, 53 (total 141) ours 6, 14, 19, 21, 25, 29, 32 (total 146) What differs is the distribution. The original's hottest local takes 53 accesses - the prologue shows that slot is its `id` copy, `mov edi, [esp + 0x38]` at 0x527ca6 then `mov [esp + 0x10], edi` - where ours tops out at 32, and its three zeroed counters take 9, 10 and 14 against our 19, 21 and 29. It holds those in registers across stretches where we spill them. Nor is there a merge to be had: `iVar11` and `uVar13` looked like further copies of `id` but are the decompiler's general scratch, reassigned at fifteen and twelve sites. Search stopped and the file restored; the note at the function says not to re-run it. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 531f06bd82..2a87bf32dd 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -162,6 +162,28 @@ namespace Map { 0x1c. The two slots still over are one use each, which makes them compiler temporaries for argument shuffling rather than locals this file can drop. */ + /* + Declaration order is a dead end here, and this can be shown rather than + measured, so do not run perm_decls.py over this run. The frame matches, so + diff_slots.py reports the same seven local slots on both sides - but the + access counts per slot are + + original 6, 9, 10, 14, 24, 25, 53 (total 141) + ours 6, 14, 19, 21, 25, 29, 32 (total 146) + + and reordering declarations only permutes which slot carries which count. The + two multisets are not equal, so no order can make them agree. What differs is + the distribution: the original's hottest local takes 53 accesses - from the + prologue that slot is its `id` copy, `mov edi, [esp + 0x38]` at 0x527ca6 then + `mov [esp + 0x10], edi` at 0x527cb0 - where ours tops out at 32, and its three + zeroed counters take 9, 10 and 14 against our 19, 21 and 29. The original + holds those values in registers across stretches where we spill them, which is + register allocation and not something declaration order reaches. + + The obvious-looking repair does not exist either: our `iVar11` and `uVar13` are + not further copies of `id` that could be merged into one hot local, they are + the decompiler's general scratch, reassigned at fifteen and twelve sites. + */ _tribeSize = _tribe.size; _param_4_unitUID_Y_copy = unitUID; _ramOrHorse = 0; From 556257858c04e6e626205078e73304832d956d52 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 10:25:48 +0200 Subject: [PATCH 075/202] reimplement: giveTribeAnInstruction 52.0 -> 56.8%, by not rebasing the subscripts Reading the diff content for the worst-aligned arm, rather than reasoning about slot counts, showed our tribe fields reached as `[eax + 0x34]` where the original uses `[ebp + 0x5c]`. Same address, different encoding: the original's base is `this + tribeID * 0x334` with the 0x28 `tribes` offset folded into the displacement, which is exactly what the plain subscript emits. The `Tribe&` I had added rebased onto `&tribes[tribeID]` and shifted the displacement on all 74 tribe field accesses. Reverting it is worth 4.8 points raw and 6.3 normalized, and costs nothing on the frame: dropping the tribeID copy and merging the two unitUID copies get to 0x1c on their own, so diff_slots.py still reports the same sixteen slots on both sides. This corrects two earlier entries in this file, both of which blamed the frame: the reference cost 2.8 points standalone because of the displacements, not the frame; and the frame fix netted only 0.1 because it achieved an exact frame and broke every displacement in the same commit, so the effects cancelled. Found by measuring per-arm agreement instead of whole-function aggregates - the remaining mass is concentrated in four arms (0x20 at 10%, UNIT_ATTACK_UNIT 19%, ATTACK_BUILDING 30%, ATTACK_WALL 31%). Namespace average 82.77% -> 82.81%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 228 +++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 93 insertions(+), 137 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 2a87bf32dd..9db16ea08b 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -119,78 +119,34 @@ namespace Map { ushort _targetArea; iVar11 = id; /* - Three things here together make our frame exactly the original's 0x1c, down - from 0x28, and diff_slots.py now reports the same sixteen slots on both sides - with nothing left over: + Two things here make our frame exactly the original's 0x1c, down from 0x28, + and diff_slots.py reports the same sixteen slots on both sides with nothing + left over: the tribeID copy is gone, the subscripts having been its only real + users, and the two unitUID copies are one, because `_param_4_unitUID_Y` was + read only in the ATTACK_LAND arm and `_param_4_unitUID_Y_copy` only in the + siege arms, so their live ranges never overlap. - - the tribe's address is taken once, as the original does before it reads - anything (`lea ebp, [eax + ecx]` at 0x527c93, spilled to [esp + 0x18] at - 0x527cc4), so every field below is one base plus an offset instead of a - fresh `tribeID * 0x334`; - - the tribeID copy goes with it, since the subscripts were its only real - users and the four remaining argument passes can use the parameter - it is - only reassigned inside the EXIT_SIEGE arm, and nothing that reads it runs - in that arm; - - the two unitUID copies become one. `_param_4_unitUID_Y` was read only in - the ATTACK_LAND arm and `_param_4_unitUID_Y_copy` only in the siege arms, - so the live ranges never overlap and one slot does for both. + What must NOT be done is bind the subscripts to a `Tribe&`. The original does + take the tribe's address once (`lea ebp, [eax + ecx]` at 0x527c93, spilled to + [esp + 0x18]), which makes a reference look right, and it is not needed for + the frame anyway - these two changes get there on their own. But its base is + `this + tribeID * 0x334` with the 0x28 `tribes` offset folded into every + displacement, so the size is `[ebp + 0x5c]`. A reference rebases onto + `&tribes[tribeID]` and the same field becomes `[eax + 0x34]` - the same + address, a different encoding, on all 74 tribe field accesses. That is worth + 4.8 points (56.8% against 52.0%), and it is what the plain subscript emits. - Worth saying plainly: getting the frame exactly right was expected to be a - large gain, because 675 of the differing lines carry an `[esp + N]` operand - and the dominant delta was exactly the 0xc the frame was over. It is not - - 52.0% against 51.9%, normalized 41.7% against 40.6%. The offsets were never - the whole story: which value sits in which slot still differs (the original's - hottest local is [esp + 0x10] with 53 accesses, ours has 32 there), and that - follows from declaration order, which would need a permutation search. + This also explains two earlier measurements that had been put down to the + frame: the reference costing 2.8 points on its own, and the frame fix netting + only 0.1 when it was bundled with the reference - it got the frame exactly + right and broke every displacement at the same time, so the two cancelled. */ - Tribe& _tribe = this->tribes[tribeID]; - /* - The subscripts are repeated rather than bound to a `Tribe&`, and the reason is - worth writing down because the evidence points the other way. The original - takes the tribe's address once before it reads anything - `lea ebp, [eax + - ecx]` at 0x527c93, spilled to [esp + 0x18] at 0x527cc4 - and reaches every - tribe field as `[ebp + ...]`: `[ebp + 0x5c]` for the size, `[ebp + 0x2e8]` and - `[ebp + 0x23a]` for the two stores below. - - Recomputing `tribeID * 0x334` per subscript is indeed what puts extra dwords - in our frame: 0x28 against the original's 0x1c, which shifts every `[esp + N]` - in the function by 0xc, and 675 of the 3041 differing lines carry an - `[esp + N]` operand. Binding all 74 subscripts to a reference does remove one - of the three extra slots - but it costs 2.8 points (51.9% -> 49.1%), because - the frame then measures 0x24 and every stack offset mismatches by 8 instead of - 12. Partial credit is worth nothing here; the frame has to land exactly on - 0x1c. The two slots still over are one use each, which makes them compiler - temporaries for argument shuffling rather than locals this file can drop. - */ - /* - Declaration order is a dead end here, and this can be shown rather than - measured, so do not run perm_decls.py over this run. The frame matches, so - diff_slots.py reports the same seven local slots on both sides - but the - access counts per slot are - - original 6, 9, 10, 14, 24, 25, 53 (total 141) - ours 6, 14, 19, 21, 25, 29, 32 (total 146) - - and reordering declarations only permutes which slot carries which count. The - two multisets are not equal, so no order can make them agree. What differs is - the distribution: the original's hottest local takes 53 accesses - from the - prologue that slot is its `id` copy, `mov edi, [esp + 0x38]` at 0x527ca6 then - `mov [esp + 0x10], edi` at 0x527cb0 - where ours tops out at 32, and its three - zeroed counters take 9, 10 and 14 against our 19, 21 and 29. The original - holds those values in registers across stretches where we spill them, which is - register allocation and not something declaration order reaches. - - The obvious-looking repair does not exist either: our `iVar11` and `uVar13` are - not further copies of `id` that could be merged into one hot local, they are - the decompiler's general scratch, reassigned at fifteen and twelve sites. - */ - _tribeSize = _tribe.size; _param_4_unitUID_Y_copy = unitUID; _ramOrHorse = 0; local_8 = 0; bVar5 = FALSE; - _tribe.someUnitID = 0; - _tribe.containsRabbit = 0; + this->tribes[tribeID].someUnitID = 0; + this->tribes[tribeID].containsRabbit = 0; _param_3_id_x_tile_buildingID_copy = (short)id; uVar13 = id; /* @@ -224,14 +180,14 @@ namespace Map { = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; DAT_UnitsState::instance.units[_unitID_0x03].field243_0x3b0 = 0; } - } while (_unitSelectionIndex < _tribe.size); + } while (_unitSelectionIndex < this->tribes[tribeID].size); return (undefined4)(1); } break; case Map::Units::UIT_STOP: iVar11 = 0; - _tribe.isRallyingUnk = 0; - _tribe.someUnitID = 0; + this->tribes[tribeID].isRallyingUnk = 0; + this->tribes[tribeID].someUnitID = 0; if (0 < _tribeSize) { do { iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -250,7 +206,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].targetedBuildingTile = 0; DAT_UnitsState::instance.units[iVar12].movementType_OR_targetUnitID = 0; } - } while (iVar11 < _tribe.size); + } while (iVar11 < this->tribes[tribeID].size); return (undefined4)(1); } break; @@ -258,7 +214,7 @@ namespace Map { /* unit attack target */ - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; pUVar6 = DAT_UnitsState::instance.units + id; _unitTribeIndex = 0; /* @@ -357,10 +313,10 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_0x04].shootBeforeStop = 10; LAB_00527ee1: if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { - _tribe.containsRabbit = 1; + this->tribes[tribeID].containsRabbit = 1; } } - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); /* `id` is zeroed before the loop and counted up in the melee arm, so this asks whether the selection held any melee units. The original is @@ -372,7 +328,7 @@ namespace Map { /* if there are melee units in the selection */ - _targetUnitID = (int)_tribe.selectionTargetUnitID; + _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; local_8 = (uint)DAT_UnitsState::instance.units[_targetUnitID].x; unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[_targetUnitID].y; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( @@ -383,12 +339,12 @@ namespace Map { (dword)((int)((short)DAT_TileMapState::instance .PathConnectionLayer[DAT_UnitsState::instance.units[_targetUnitID].tile])), (int)(DAT_UnitsState::instance.units[_targetUnitID].owner)); - _tribe.someUnitID = _param_3_id_x_tile_buildingID_copy; - _tribe.someUnitUID = unitUID; - _tribe.someTile = DAT_UnitsState::instance.units[iVar11].tile; + this->tribes[tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; + this->tribes[tribeID].someUnitUID = unitUID; + this->tribes[tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; iVar12 = 0; - sVar9 = _tribe.size; - _tribe.someTile2 + sVar9 = this->tribes[tribeID].size; + this->tribes[tribeID].someTile2 = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + DAT_ViewportRenderState::instance .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] @@ -472,13 +428,13 @@ namespace Map { } } } - } while (iVar12 < _tribe.size); + } while (iVar12 < this->tribes[tribeID].size); } iVar11 = unitInstructionType * 8; if (iVar11 < 9) { iVar11 = 8; } - _tribe.field166_0x2be = (short)iVar11; + this->tribes[tribeID].field166_0x2be = (short)iVar11; return (undefined4)(1); } } @@ -548,7 +504,7 @@ namespace Map { = unitUID; DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { - _tribe.containsRabbit = 1; + this->tribes[tribeID].containsRabbit = 1; } if (DAT_UnitsState::instance.units[iVar12]._someX_2 == 0) { sVar9 = DAT_UnitsState::instance.units[iVar12].destinationY_2Unk; @@ -585,12 +541,12 @@ namespace Map { break; } } - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); /* The melee counter against zero, as at the first site. */ if (id != 0) { - iVar12 = (int)_tribe.selectionTargetUnitID; + iVar12 = (int)this->tribes[tribeID].selectionTargetUnitID; param_5 = (int)DAT_UnitsState::instance.units[iVar12].x; unitInstructionType = (UnitInstructionType)DAT_UnitsState::instance.units[iVar12].y; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::predictUnitInterceptPosition, this)( @@ -601,12 +557,12 @@ namespace Map { (dword)((int)((short)DAT_TileMapState::instance .PathConnectionLayer[DAT_UnitsState::instance.units[iVar12].tile])), (int)(DAT_UnitsState::instance.units[iVar12].owner)); - _tribe.someUnitID = _param_3_id_x_tile_buildingID_copy; - _tribe.someUnitUID = unitUID; - _tribe.someTile = DAT_UnitsState::instance.units[iVar11].tile; + this->tribes[tribeID].someUnitID = _param_3_id_x_tile_buildingID_copy; + this->tribes[tribeID].someUnitUID = unitUID; + this->tribes[tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; iVar12 = 0; - sVar9 = _tribe.size; - _tribe.someTile2 + sVar9 = this->tribes[tribeID].size; + this->tribes[tribeID].someTile2 = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + DAT_ViewportRenderState::instance .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] @@ -705,13 +661,13 @@ namespace Map { } } } - } while (iVar12 < _tribe.size); + } while (iVar12 < this->tribes[tribeID].size); } iVar11 = unitInstructionType * 8; if (iVar11 < 9) { iVar11 = 8; } - _tribe.field166_0x2be = (short)iVar11; + this->tribes[tribeID].field166_0x2be = (short)iVar11; return (undefined4)(1); } } @@ -728,14 +684,14 @@ namespace Map { && (int)(short)DAT_UnitsState::instance.units[iVar12].unitType - 0x27U < 2) { DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 1; } - } while (iVar11 < _tribe.size); + } while (iVar11 < this->tribes[tribeID].size); return (undefined4)(1); } break; case Map::Units::UIT_ATTACK_LAND: case Map::Units::UIT_THROW_COW: - unitUID = _tribe.owner; - _tribe.isRallyingUnk = 0; + unitUID = this->tribes[tribeID].owner; + this->tribes[tribeID].isRallyingUnk = 0; _unitTribeIndex = 0; if (0 < _tribeSize) { do { @@ -804,7 +760,7 @@ namespace Map { goto LAB_00528808; } } - if (_tribe.size <= _unitTribeIndex) { + if (this->tribes[tribeID].size <= _unitTribeIndex) { return (undefined4)(1); } } while (true); @@ -812,7 +768,7 @@ namespace Map { break; case Map::Units::UIT_THROW_OIL: iVar11 = 0; - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; if (0 < _tribeSize) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -829,16 +785,16 @@ namespace Map { DAT_UnitsState::instance.units[uVar13].plannedDestinationY = (short)unitUID; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); + DAT_UnitsState::ptr)(this->tribes[tribeID].owner, uVar13, 0); DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; } - } while (iVar11 < _tribe.size); + } while (iVar11 < this->tribes[tribeID].size); return (undefined4)(1); } break; case Map::Units::UIT_LIGHT_PITCH: - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; _unitTribeIndex = 0; if ((DAT_TileMapState::instance.pitchDitches[id].uid == unitUID) && (0 < _tribeSize)) { do { @@ -873,7 +829,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } } - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); return (undefined4)(1); } /* @@ -887,9 +843,9 @@ namespace Map { case ((UnitInstructionType)0x26): _unitTribeIndex = 0; id = 0; - _tribe.targetBuildingID = _param_3_id_x_tile_buildingID_copy; - _tribe.targetBuildingUID = unitUID; - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].targetBuildingID = _param_3_id_x_tile_buildingID_copy; + this->tribes[tribeID].targetBuildingUID = unitUID; + this->tribes[tribeID].isRallyingUnk = 0; if (0 < _tribeSize) { do { iVar12 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1008,7 +964,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } } - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); } if ((((DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) || (DAT_GameState::instance.mapAndTime.skirmishStrongWalls == 0)) @@ -1018,7 +974,7 @@ namespace Map { as `id | x | id != 0`. */ && (id != 0)) { - sVar9 = _tribe.selectionTargetUnitID; + sVar9 = this->tribes[tribeID].selectionTargetUnitID; if (unitInstructionType == Map::Units::UIT_ATTACK_BUILDING || unitInstructionType == (UnitInstructionType)0x26) { iVar12 = (int)DAT_UnitsState::instance.units[sVar9].owner; @@ -1033,7 +989,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( tribeID, _ramOrHorse); if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) - && (_unitTribeIndex = 0, 0 < _tribe.size)) { + && (_unitTribeIndex = 0, 0 < this->tribes[tribeID].size)) { id = 0x12d5c70; do { _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1151,7 +1107,7 @@ namespace Map { goto switchD_00528e3a_caseD_5; } } - if (_tribe.size <= _unitTribeIndex) { + if (this->tribes[tribeID].size <= _unitTribeIndex) { return (undefined4)(1); } } while (true); @@ -1163,7 +1119,7 @@ namespace Map { case ((UnitInstructionType)0x25): _unitTribeIndex = 0; id = 0; - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; if (0 < _tribeSize) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1267,7 +1223,7 @@ namespace Map { id = id + 1; } } - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); } if ((DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) && (DAT_GameState::instance.mapAndTime.skirmishStrongWalls != 0)) { @@ -1277,7 +1233,7 @@ namespace Map { The melee counter against zero again, as at the two sites above. */ if (id != 0) { - sVar9 = _tribe.selectionTargetUnitID; + sVar9 = this->tribes[tribeID].selectionTargetUnitID; if (unitInstructionType == Map::Units::UIT_ATTACK_WALL || unitInstructionType == (UnitInstructionType)0x25) { sVar8 = DAT_UnitsState::instance.units[sVar9].facingDirection; @@ -1295,7 +1251,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( tribeID, _ramOrHorse); if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) - && (iVar12 = 0, 0 < _tribe.size)) { + && (iVar12 = 0, 0 < this->tribes[tribeID].size)) { id = 0x12d5c70; do { iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1422,7 +1378,7 @@ namespace Map { } } } - if (_tribe.size <= iVar12) { + if (this->tribes[tribeID].size <= iVar12) { return (undefined4)(1); } } while (true); @@ -1432,7 +1388,7 @@ namespace Map { case Map::Units::UIT_SET_LADDER: iVar12 = 0; id = 0; - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; if (0 < _tribeSize) { do { iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1445,12 +1401,12 @@ namespace Map { DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; } - } while (iVar12 < _tribe.size); + } while (iVar12 < this->tribes[tribeID].size); /* The melee counter against zero, as at the first site. */ if (id != 0) { - sVar9 = _tribe.selectionTargetUnitID; + sVar9 = this->tribes[tribeID].selectionTargetUnitID; MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: populateDestinationsArrayWithWallTileAndFreeTilePairInSameArea, DAT_PathFindingState::ptr)(iVar11, id, @@ -1460,7 +1416,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( tribeID, 0); if ((DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper != 0) - && (iVar11 = 0, 0 < _tribe.size)) { + && (iVar11 = 0, 0 < this->tribes[tribeID].size)) { piVar18 = &DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper; do { _laddermanID = MACRO_CALL_MEMBER( @@ -1509,7 +1465,7 @@ namespace Map { DAT_UnitsState::instance.units[_laddermanID].attackAtTileX = (short)_defensiveTile1 - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; } - } while (iVar11 < _tribe.size); + } while (iVar11 < this->tribes[tribeID].size); return (undefined4)(1); } } @@ -1517,12 +1473,12 @@ namespace Map { break; case Map::Units::UIT_FILL_MOAT: _targetArea = DAT_TileMapState::instance.PathConnectionLayer - [DAT_UnitsState::instance.units[_tribe.selectionTargetUnitID].tile]; - _tribe.isRallyingUnk = 0; + [DAT_UnitsState::instance.units[this->tribes[tribeID].selectionTargetUnitID].tile]; + this->tribes[tribeID].isRallyingUnk = 0; if ((int)(short)_targetArea != 0) { iVar11 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, DAT_PathFindingState::ptr)( - _tribe.owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); + this->tribes[tribeID].owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); if (iVar11 == 0) { return (undefined4)(1); } @@ -1532,10 +1488,10 @@ namespace Map { case Map::Units::UIT_DIG_MOAT: iVar11 = unitUID; iVar12 = 0; - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( tribeID, uVar13, (uint)(unitUID), 0, 1); - if (0 < _tribe.size) { + if (0 < this->tribes[tribeID].size) { do { iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); @@ -1565,12 +1521,12 @@ namespace Map { DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; } } - } while (iVar12 < _tribe.size); + } while (iVar12 < this->tribes[tribeID].size); return (undefined4)(1); } break; case Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED: - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; _y = DAT_BuildingsState::instance.buildings[id].buildingEntryY; _x = DAT_BuildingsState::instance.buildings[id].buildingEntryX; _unitTribeIndex = 0; @@ -1587,7 +1543,7 @@ namespace Map { if (((unitUID | unitUID != 0) || (DAT_BuildingsState::instance.buildings[iVar11].buildingType == Map::Buildings::BT_OILSMELTER)) - && 0 < _tribe.size) { + && 0 < this->tribes[tribeID].size) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, _unitTribeIndex); @@ -1649,7 +1605,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); + DAT_UnitsState::ptr)(this->tribes[tribeID].owner, uVar13, 0); unitUID = unitUID + -1; iVar12 = _unitTribeIndex; if (unitUID == 0) { @@ -1659,7 +1615,7 @@ namespace Map { } } _unitTribeIndex = iVar12; - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); return (undefined4)(1); } break; @@ -1667,12 +1623,12 @@ namespace Map { /* manning siege equipment? */ - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; if (DAT_UnitsState::instance.units[id].uid == unitUID && (unitUID = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(id), unitUID | unitUID != 0) - && (iVar12 = 0, 0 < _tribe.size)) { + && (iVar12 = 0, 0 < this->tribes[tribeID].size)) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); @@ -1729,7 +1685,7 @@ namespace Map { DAT_UnitsState::instance.units[uVar13].resourceToDeposit = 0; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)(uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); + DAT_UnitsState::ptr)(this->tribes[tribeID].owner, uVar13, 0); unitUID = unitUID + -1; iVar14 = iVar12; if (unitUID == 0) { @@ -1737,7 +1693,7 @@ namespace Map { } } iVar12 = iVar14; - if (_tribe.size <= iVar14) { + if (this->tribes[tribeID].size <= iVar14) { return (undefined4)(1); } } while (true); @@ -1845,7 +1801,7 @@ namespace Map { /* disband? */ - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; _unitTribeIndex = 0; if (0 < _tribeSize) { do { @@ -1903,14 +1859,14 @@ namespace Map { } } } - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); } MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectAllUnitsOneByOne, DAT_UnitsState::ptr)(); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)( - _tribe.owner, 0, 0); + this->tribes[tribeID].owner, 0, 0); return (undefined4)(1); case ((UnitInstructionType)0x15): - _tribe.isRallyingUnk = 0; + this->tribes[tribeID].isRallyingUnk = 0; uVar13 = (uint)DAT_BuildingsState::instance.buildings[id].buildingEntryY; _unitTribeIndex = 0; BVar16 @@ -1960,11 +1916,11 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectUnit, DAT_UnitsState::ptr)( uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, - DAT_UnitsState::ptr)(_tribe.owner, uVar13, 0); + DAT_UnitsState::ptr)(this->tribes[tribeID].owner, uVar13, 0); return (undefined4)(1); } } - } while (_unitTribeIndex < _tribe.size); + } while (_unitTribeIndex < this->tribes[tribeID].size); } DAT_TileMapState::instance.showNoRubbleWhenDestroyingBuilding = 1; MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::destroyBuilding, DAT_BuildingsState::ptr)( diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 50d1c70e2b..d656c62838 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 52.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 56.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From ffed577f7a9edb8dba77d06d2cb4aa3da1e899e7 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 10:31:06 +0200 Subject: [PATCH 076/202] measure: caching the tribe base does not pay, even with the displacement kept The slot census makes it look inviting - the original touches its tribeID argument 36 times where the plain subscript touches it 90, because it holds `this + tribeID * 0x334` in EBP. Shifting a TribesState pointer by whole Tribe strides and subscripting at zero satisfies both constraints at once: the same address, the same `+0x5c` encoding, and one base for the compiler to cache. It costs 3.5 points (56.8% -> 53.3%) and puts a slot back over. Recorded at the function; reverted. It also introduced a C4700 - the blanket rewrite moved a use of _tribeSize ahead of its initialisation - so this is the second time a bulk replace in this file has needed the warning output read rather than just the score. The revert clears it. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/giveTribeAnInstruction.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 9db16ea08b..345e48c7a7 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -140,6 +140,14 @@ namespace Map { frame: the reference costing 2.8 points on its own, and the frame fix netting only 0.1 when it was bundled with the reference - it got the frame exactly right and broke every displacement at the same time, so the two cancelled. + + Caching the base while keeping the displacement does not work either, though + the slot census makes it look inviting: the original touches its tribeID + argument 36 times where the plain subscript touches it 90, because it holds + `this + tribeID * 0x334` in EBP. Shifting a TribesState pointer by whole Tribe + strides and subscripting at zero gives the right address with the right + encoding and one base to cache - and costs 3.5 points (56.8% -> 53.3%). It + also puts a slot back over. The subscript stands. */ _param_4_unitUID_Y_copy = unitUID; _ramOrHorse = 0; From a2846927c64d9c585364a9f70eaa9d0f68a2e749 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 10:39:07 +0200 Subject: [PATCH 077/202] fix: restore _tribeSize's initialisation, deleted by my own comment edit In 556257858 I replaced a comment by splicing from the comment opener to the next statement, and never checked what lay between the two anchors: the region also held `_tribeSize = ...size;` and the declaration-order proof note. _tribeSize was left declared, read in fourteen loop guards, and never assigned. The build said so every time - `C4700: uninitialized local variable '_tribeSize' used` - and I misread it twice, first blaming the shifted-base experiment and then claiming in a commit message that reverting that experiment cleared it. It did not; the warning was in the committed tree for three builds. Both the assignment and the deleted note are restored. The honest number is unchanged at 56.8% (normalized 48.0%), so the 4.8 points from dropping the `Tribe&` are real - but the 57.1% I reported from the broken build was 0.3 too high, the uninitialized read having let the compiler elide work. Treat that figure as void. Two habits to keep: read the build's warnings, not just BUILD_OK and the score; and when splicing a region, print what is being removed before removing it. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 345e48c7a7..2cf561a797 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -149,6 +149,29 @@ namespace Map { encoding and one base to cache - and costs 3.5 points (56.8% -> 53.3%). It also puts a slot back over. The subscript stands. */ + /* + Declaration order is a dead end here, and this can be shown rather than + measured, so do not run perm_decls.py over this run. The frame matches, so + diff_slots.py reports the same seven local slots on both sides - but the + access counts per slot are + + original 6, 9, 10, 14, 24, 25, 53 (total 141) + ours 6, 14, 19, 21, 25, 29, 32 (total 146) + + and reordering declarations only permutes which slot carries which count. The + two multisets are not equal, so no order can make them agree. What differs is + the distribution: the original's hottest local takes 53 accesses - from the + prologue that slot is its `id` copy, `mov edi, [esp + 0x38]` at 0x527ca6 then + `mov [esp + 0x10], edi` at 0x527cb0 - where ours tops out at 32, and its three + zeroed counters take 9, 10 and 14 against our 19, 21 and 29. The original + holds those values in registers across stretches where we spill them, which is + register allocation and not something declaration order reaches. + + The obvious-looking repair does not exist either: our `iVar11` and `uVar13` are + not further copies of `id` that could be merged into one hot local, they are + the decompiler's general scratch, reassigned at fifteen and twelve sites. + */ + _tribeSize = this->tribes[tribeID].size; _param_4_unitUID_Y_copy = unitUID; _ramOrHorse = 0; local_8 = 0; From 4e4af157fbddcfdc6f7467b64822d740b6c055d4 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 17:17:38 +0200 Subject: [PATCH 078/202] measure: caching the tribe base loses 4.8 points, retested on corrected code The earlier verdict on this stood on a build where _tribeSize had lost its initialisation, so it needed redoing. Redone: the shifted TribesState pointer - same address, same `+0x5c` encoding, one base for the compiler to hold - scores 52.0% against the plain subscript's 56.8%, which is also exactly what the `Tribe&` scored. Caching the base costs 4.8 points however the displacement is written. Worth recording because every cheap signal pointed the other way. Caching takes the function from 2312 instructions to 2187 against the original's 2157, and lifts quick_diff from 0.4296 to 0.4622: fewer instructions, closer length, better ratio, worse reccmp score. The plain subscript re-derives tribeID * 0x334 at all 74 accesses and is still the better match. So the instruction count I started tracking this round is not a proxy for the score, and neither is quick_diff below its documented range. Confirm on reccmp. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/giveTribeAnInstruction.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 2cf561a797..4ec693fec1 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -146,8 +146,18 @@ namespace Map { argument 36 times where the plain subscript touches it 90, because it holds `this + tribeID * 0x334` in EBP. Shifting a TribesState pointer by whole Tribe strides and subscripting at zero gives the right address with the right - encoding and one base to cache - and costs 3.5 points (56.8% -> 53.3%). It - also puts a slot back over. The subscript stands. + encoding and one base to cache - and it loses. Measured twice, because the + first run was on the build where _tribeSize had lost its initialisation: on + corrected code it is 52.0% against the subscript's 56.8%, which is also what + the `Tribe&` scored. Caching the base costs 4.8 points whichever way the + displacement is written. + + The instructive part is that every cheap signal said the opposite. Caching + takes the function from 2312 instructions to 2187 against the original's 2157, + and lifts quick_diff from 0.4296 to 0.4622 - fewer instructions, closer + length, better ratio, worse score. The plain subscript re-derives + `tribeID * 0x334` at all 74 accesses and is still the better match, so do not + trust instruction count or quick_diff here without confirming on reccmp. */ /* Declaration order is a dead end here, and this can be shown rather than From 97ed425d36a8514f32df419c705d67eca319a6a6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 17:22:07 +0200 Subject: [PATCH 079/202] measure: the base-caching paradox is register selection, not addressing Reading the UNIT_ATTACK_UNIT arm's diff closes the question the last two commits left open. The original does cache the base - it reads the size as `movsx eax, word ptr [ebp + 0x5c]`, no imul - so caching is not the error. But it also keeps zero in EAX where we keep it in EDI and EDX, and when the source asks for a cached base MSVC chooses its own register for it and the choice cascades. The re-derived subscript happens to leave the other registers where the original has them, which is why it wins by 4.8 despite 155 extra instructions. That plus cold-block placement is the whole of the remaining gap: MAN_SIEGE_EQUIPMENT is emitted after our epilogue and inline at 0x529a8b in the original, with the arm order already matching the jump table. Neither is reachable from source. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/giveTribeAnInstruction.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 4ec693fec1..d27a619fa8 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -158,6 +158,17 @@ namespace Map { length, better ratio, worse score. The plain subscript re-derives `tribeID * 0x334` at all 74 accesses and is still the better match, so do not trust instruction count or quick_diff here without confirming on reccmp. + + The paradox resolves into register selection, which no source form reaches. + The UNIT_ATTACK_UNIT arm shows the original reading the size as + `movsx eax, word ptr [ebp + 0x5c]` - base cached in EBP, no imul - so it + really does cache. It also materialises zero in EAX (`xor eax, eax`) where we + use EDI and EDX. Asking for a cached base gets us one, but MSVC puts it in a + register of its own choosing and that cascades through everything else; the + re-derived form happens to leave the other registers where the original has + them. That is the whole of the remaining gap in this arm, together with cold + blocks: MAN_SIEGE_EQUIPMENT is emitted after the epilogue here and inline at + 0x529a8b in the original, with the arm order already matching the table. */ /* Declaration order is a dead end here, and this can be shown rather than From 68aa69b5ae7f9510a3c4704df0711a74e28cf88a Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 17:38:00 +0200 Subject: [PATCH 080/202] reimplement: three short functions, one of them to 100% removeAllTribeUnits 82.1 -> 100%: the index argument is post-incremented. The original copies it out before bumping it - `mov eax, esi`, `push eax`, then `add esi, 1` before the call at 0x525499 - which is what the decompiler printed as a spare variable carrying the old value across the call. Note this is not the general shape: getTribeAliveStatus, updateTribeRallyFlags, trimTribeToSize and assignAttackTargetsForTribe all increment after the call, so check each one. updateAnimalHerdBehaviorState 78.4 -> 80.4%: the rng local is signed. The original compares against the literal with `cmp ecx, 1; jg` and derives the second value with `setg`, where a `uint` gives `jae` and `seta` against a register holding 1. spawnUnitsForAITribe 72.9 -> 78.0%: the Y scaling is computed first, which is the register pairing the original has - `lea ebx, [edx*8]` for param_3 and `lea ebp, [ecx*8]` for param_2. Namespace average 82.81% -> 83.03%, 57 of 113 at 100%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/removeAllTribeUnits.cpp | 9 +++++++-- .../Map/Units/TribesState/spawnUnitsForAITribe.cpp | 7 ++++++- .../Units/TribesState/updateAnimalHerdBehaviorState.cpp | 7 ++++++- status/addresses-SHC-3BB0A8C1.txt | 6 +++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp index b0f1c1167f..3798769449 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp @@ -24,10 +24,15 @@ namespace Map { short* const _size = &this->tribes[param_1].size; if (0 < *_size) { do { + /* + A post-increment: the original copies the index out before bumping it + (`mov eax, esi`, `push eax`, then `add esi, 1` before the call at + 0x525499), which is what the decompiler printed as a spare variable + carrying the old value across the call. + */ int const _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, _index); - _index = _index + 1; + param_1, _index++); DAT_UnitsState::instance.units[_unitID].logicalState = Map::Units::ULS_REMOVE; } while (_index < (int)*_size); } diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp index bc2e3cf6a4..9859ec97c8 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp @@ -21,8 +21,13 @@ namespace Map { int unitID; int aiUnitBehaviourType; if (((0 < param_2) || (0 < param_3)) && 0 < param_6) { - int microXPosition = param_2 * 8; + /* + The Y scaling is computed first: the original has `lea ebx, [edx*8]` for + param_3 and `lea ebp, [ecx*8]` for param_2, which is the opposite register + pairing from declaring X first. + */ int microYPosition = param_3 * 8; + int microXPosition = param_2 * 8; param_2 = param_6; do { unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( diff --git a/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp b/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp index 4ab40c4188..5a4ea5d1d3 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp @@ -25,7 +25,12 @@ namespace Map { if (DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[iVar1].dying == 0 && DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue == 2) { - uint uVar2 = DAT_UnitsState::instance.units[iVar1].fixedRng & 7; + /* + Signed: the original compares against the literal with + `cmp ecx, 1; jg` and derives the second value with `setg`, where a + `uint` gives `jae` and `seta` against a register holding 1. + */ + int uVar2 = DAT_UnitsState::instance.units[iVar1].fixedRng & 7; if (uVar2 < 2) { DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue = 0; } else { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index d656c62838..dff7ec8f2d 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46821,7 +46821,7 @@ SHC_3BB0A8C1_0x00522DD0 | 94.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522EF0 | 54.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522F70 | 72.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522F70 | 78.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523030 | 98.3% | Reimplemented, asm differs @@ -46845,7 +46845,7 @@ SHC_3BB0A8C1_0x00523410 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523520 | 96.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523590 | 78.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523590 | 80.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523630 | 100.0% | Reimplemented @@ -46935,7 +46935,7 @@ SHC_3BB0A8C1_0x00525370 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005253F0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525470 | 82.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525470 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005254C0 | 100.0% | Reimplemented From b6980dd0dab1e7463fd4a3a6bf61b606c3f8ea73 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 22:56:26 +0200 Subject: [PATCH 081/202] reimplement: trimTribeToSize 54.1 -> 65.9%, by caching the base after all I had declined to try this, reasoning from giveTribeAnInstruction where caching the tribe base cost 4.8 points. That extrapolation was wrong and the only way to find out was to run it. Tested on three short functions where the original visibly caches: trimTribeToSize 54.1% -> 65.9% +11.8 removeUnitFromTribe 67.1% -> 37.2% -29.9 removeUnitFromThisTribeIfInTribe 79.5% -> 45.1% -34.4 Kept the first, reverted the others, noted all three at their functions. The discriminator looks mechanical. trimTribeToSize puts only its two size reads on the cached base and has no other addressing base live. The other two put size, tribeState and selectionTargetUnitID on it while a second base stays live for the bitmask - `[ebx + ebp*2 + 0x60]`, which the original also keeps on the subscript - and the two compete for registers. So the rule is not "caching pays" or "caching does not": it pays when the cached base is the function's only addressing base. That also fits giveTribeAnInstruction's -4.8, which has many competing bases. The form matters too: a shifted TribesState pointer keeps the `+0x5c` displacement, where a `Tribe&` gives `+0x34` and a pointer to the size field gives `[ptr]` with none - which is why the earlier pointer hoist here cost 1.2. Namespace average 83.03% -> 83.14%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../removeUnitFromThisTribeIfInTribe.cpp | 10 ++++++++++ .../Map/Units/TribesState/removeUnitFromTribe.cpp | 10 ++++++++++ .../Map/Units/TribesState/trimTribeToSize.cpp | 15 ++++++++++----- status/addresses-SHC-3BB0A8C1.txt | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index 697b976243..07cd1f8848 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -29,6 +29,16 @@ namespace Map { == 0) { return; } + /* + The base is NOT taken once here, although the original does take it + (`lea ebp, [eax + ecx]`) and read the size, the tribe state and the + target unit through it as `[ebp + 0x5c]`, `[ebp + 0x40]` and `[ebp + 0x5a]`. + Mirroring that - shifted TribesState pointer, bitmask accesses left on the + subscript where the original also keeps them - costs 34.4 points + (79.5% -> 45.1%). The same change is worth +11.8 in trimTribeToSize, which + differs in having only the two size reads on the base; here a second + addressing base is live for the bitmask and the two fight over registers. + */ short const _size = this->tribes[tribeID].size; if (0 < _size) { this->tribes[tribeID].size = _size + -1; diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index 5ea20bb3ce..ec071fab1c 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -22,6 +22,16 @@ namespace Map { || ((int)this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) != 0) { + /* + The base is NOT taken once here, although the original does take it + (`lea ebp, [eax + ebx]`) and read the size, the tribe state and the + target unit through it as `[ebp + 0x5c]`, `[ebp + 0x40]` and `[ebp + 0x5a]`. + Mirroring that - shifted TribesState pointer, bitmask accesses left on the + subscript where the original also keeps them - costs 29.9 points + (67.1% -> 37.2%). The same change is worth +11.8 in trimTribeToSize, which + differs in having only the two size reads on the base; here a second + addressing base is live for the bitmask and the two fight over registers. + */ short const _size = this->tribes[tribeID].size; if (0 < _size) { this->tribes[tribeID].size = _size + -1; diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 874e3e7190..498760b9c3 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -23,12 +23,17 @@ namespace Map { _currentTribeID = DAT_CurrentTribeID::instance; unitSelectionIndex = 0; /* - The size stays a repeated subscript. The original does read it through a base - it takes once (`lea ebp, [eax + edi]` at 0x522f0b), but hoisting a pointer for - it costs 1.2 points (54.1% -> 52.9%). + The size is read through a base taken once, which is what the original does + (`lea ebp, [eax + edi]` at 0x522f0b, then `[ebp + 0x5c]`); the tribeState test + above it stays on the subscript, where the original has + `cmp word ptr [eax + edi + 0x40], si`. Hoisting a pointer to the size field + itself was tried earlier and cost 1.2 points - that gives `[ptr]` with no + displacement, not the `+0x5c` the original uses. */ + TribesState* const _tb + = (TribesState*)((char*)this + DAT_CurrentTribeID::instance * sizeof(Tribe)); if (this->tribes[DAT_CurrentTribeID::instance].tribeState != 0 - && (iVar3 = (int)this->tribes[DAT_CurrentTribeID::instance].size, iVar3 >= smallerSize) + && (iVar3 = (int)_tb->tribes[0].size, iVar3 >= smallerSize) && 0 < iVar3) { do { iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -41,7 +46,7 @@ namespace Map { && smallerSize <= unitSelectionIndex) { *pUVar1 = Map::Units::ULS_REMOVE; } - } while (unitSelectionIndex < this->tribes[_currentTribeID].size); + } while (unitSelectionIndex < _tb->tribes[0].size); } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index dff7ec8f2d..db40e10253 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46819,7 +46819,7 @@ SHC_3BB0A8C1_0x00522CA0 | 85.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522DD0 | 94.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522EF0 | 54.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522EF0 | 65.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522F70 | 78.0% | Reimplemented, asm differs From 5f75dd12608e942f40f727e1cb124f0a1d5d535b Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 23:00:46 +0200 Subject: [PATCH 082/202] measure: my discriminator for base-caching was wrong, tested and withdrawn After trimTribeToSize gained 11.8 from caching the tribe base, I wrote that the rule was whether the cached base is the function's only addressing base, and picked two candidates by it. The prediction failed: updateAnimalHerdBehaviorState 80.4% -> 39.6% -40.8 updateTribeUnitAssignments 57.0% -> 57.0% flat updateAnimalHerdBehaviorState fits that description better than trimTribeToSize does - three tribe fields, nothing else needing a base - and lost forty points. So the explanation was a rationalisation of one result, not a mechanism. Both reverted and the claim struck from all three notes where I had written it. The honest summary, now at trimTribeToSize: caching the base the way the original does is worth +11.8 there and loses everywhere else measured - removeUnitFromTribe -29.9, removeUnitFromThisTribeIfInTribe -34.4, updateAnimalHerdBehaviorState -40.8, giveTribeAnInstruction -4.8, updateTribeUnitAssignments flat. Six data points, one win, no predictor. Measure each function on its own. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/removeUnitFromThisTribeIfInTribe.cpp | 5 +++-- src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index 07cd1f8848..83db367adc 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -36,8 +36,9 @@ namespace Map { Mirroring that - shifted TribesState pointer, bitmask accesses left on the subscript where the original also keeps them - costs 34.4 points (79.5% -> 45.1%). The same change is worth +11.8 in trimTribeToSize, which - differs in having only the two size reads on the base; here a second - addressing base is live for the bitmask and the two fight over registers. + is the one function where it has ever paid. Why is not known: the + "only addressing base" explanation I first wrote here is wrong, since + updateAnimalHerdBehaviorState fits it and lost 40.8 points. */ short const _size = this->tribes[tribeID].size; if (0 < _size) { diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 498760b9c3..90c9f76044 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -29,6 +29,15 @@ namespace Map { `cmp word ptr [eax + edi + 0x40], si`. Hoisting a pointer to the size field itself was tried earlier and cost 1.2 points - that gives `[ptr]` with no displacement, not the `+0x5c` the original uses. + + Do not try to predict where this helps. Caching the base the way the original + does is worth +11.8 here and loses everywhere else it has been measured: + removeUnitFromTribe -29.9, removeUnitFromThisTribeIfInTribe -34.4, + updateAnimalHerdBehaviorState -40.8, giveTribeAnInstruction -4.8, + updateTribeUnitAssignments flat. I thought the discriminator was whether the + cached base is the function's only addressing base; updateAnimalHerdBehaviorState + fits that description better than this function does and lost forty points, so + the rule is wrong. Measure each function on its own. */ TribesState* const _tb = (TribesState*)((char*)this + DAT_CurrentTribeID::instance * sizeof(Tribe)); From 7a86c4839bf4981e35349e2689ade9faec116532 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 23:09:51 +0200 Subject: [PATCH 083/202] reimplement: createTribe 67.2 -> 79.0%, a Tribe& was shifting every displacement The original computes the base without the `tribes` offset (`lea esi, [edx + ebp]`) and takes `lea eax, [esi + 0x28]` separately for the clear's argument, reaching the fields as `[esi + 0x2c]`. Our `Tribe&` folded the 0x28 into one lea and emitted `[esi + 0x4]` - every field displacement shifted by 0x28, the same defect the reference caused in giveTribeAnInstruction. A shifted TribesState pointer gives both leas and the right displacements: +11.8. Worth noting the reference still beat the plain subscript here when it was added (30.3% -> 67.2%), so wrong displacements with a cached base were better than right displacements without one - which is why this needed measuring rather than reasoning. Second win for this form after trimTribeToSize, also +11.8. Namespace average 83.14% -> 83.24%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/createTribe.cpp | 32 ++++++++++++------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index 8e4d806507..fb5fcfb86a 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -30,9 +30,17 @@ namespace Map { 0x28]`), then reused for every store below it. */ int const _tribeID = DAT_CurrentTribeID::instance; - Tribe& _tribe = this->tribes[_tribeID]; + /* + A shifted TribesState pointer, not a `Tribe&`. The original computes + the base without the `tribes` offset (`lea esi, [edx + ebp]`) and then + takes `lea eax, [esi + 0x28]` separately for the clear's argument, + reaching the fields as `[esi + 0x2c]` and so on. A reference folds the + 0x28 into one lea and shifts every field displacement by it. + */ + TribesState* const _tb + = (TribesState*)((char*)this + _tribeID * sizeof(Tribe)); MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - sizeof(Tribe), '\0', &_tribe); + sizeof(Tribe), '\0', &_tb->tribes[0]); /* An early return, which is where the original puts its `return 0`: `cmp edi, ebx; jg` falls straight into `xor eax, eax` with the claim @@ -46,18 +54,18 @@ namespace Map { && (setAsCurrentTribeID)) { this->DAT_CurrentTribeID = _tribeID; } - _tribe.owner = playerID; - _tribe.tribeState = 2; - _tribe.uid = DAT_GameCore::instance.uniqueGameObjectTracker; + _tb->tribes[0].owner = playerID; + _tb->tribes[0].tribeState = 2; + _tb->tribes[0].uid = DAT_GameCore::instance.uniqueGameObjectTracker; DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; - _tribe.tribeType = ((AITribeType)0); - _tribe.field18_0x1c = 0; - _tribe.field19_0x1e = 0; - _tribe.tribeBehaviorType = ((SomeTribeBehaviorType)0); - _tribe.field29_0x30 = 0; - _tribe.selectionTargetUnitID = 0; - _tribe.size = 0; + _tb->tribes[0].tribeType = ((AITribeType)0); + _tb->tribes[0].field18_0x1c = 0; + _tb->tribes[0].field19_0x1e = 0; + _tb->tribes[0].tribeBehaviorType = ((SomeTribeBehaviorType)0); + _tb->tribes[0].field29_0x30 = 0; + _tb->tribes[0].selectionTargetUnitID = 0; + _tb->tribes[0].size = 0; return _tribeID; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index db40e10253..20341c1d83 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46799,7 +46799,7 @@ SHC_3BB0A8C1_0x00522720 | 62.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x005227E0 | 75.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522890 | 67.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522890 | 79.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented From d22c976689a6c124f1eecbb4c9f7643da4c4ab85 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 23:17:45 +0200 Subject: [PATCH 084/202] reimplement: updateRabbitTribeBehavior 58.9 -> 60.3%, and the base-caching tally Caching the base pays +1.4 in the rabbit updater and loses on its three nearest siblings - updateCamelTribeBehavior -14.2, updateLionWolfTribeBehavior -6.0, computeTribePercentages -2.1 - which are near-identical functions with the same shape and the same original codegen. Three kept, three reverted, all four noted. The tally over ten functions: trimTribeToSize +11.8, createTribe +11.8, updateRabbitTribeBehavior +1.4, updateTribeUnitAssignments flat, computeTribePercentages -2.1, giveTribeAnInstruction -4.8, updateLionWolfTribeBehavior -6.0, updateCamelTribeBehavior -14.2, removeUnitFromTribe -29.9, removeUnitFromThisTribeIfInTribe -34.4, updateAnimalHerdBehaviorState -40.8. The four animal updaters splitting +1.4 / -6.0 / -14.2 / -40.8 is the clearest evidence that nothing here is predictable from source shape: it is MSVC's register allocator responding to detail no rule I tried captures. I proposed two such rules during this work and both failed on their first test. Separately, the `Tribe&` lead closed rather than opened: five of its eight users are at 100% and three at 96.8-97.1% capped by the __security_cookie, so the reference is right in all of them. createTribe and giveTribeAnInstruction were the only two where the original based on `this + tribeID * 0x334` instead. Namespace average 83.24% -> 83.26%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/updateRabbitTribeBehavior.cpp | 71 +++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 40016f92c6..292a509c3d 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -26,6 +26,17 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x0052B390 void TribesState::updateRabbitTribeBehavior(int tribeID) { + /* + Base cached as the original does (`lea`, two registers, no displacement), so + the field displacements are unchanged - the subscript already matches those. + Worth +1.4 here, and that is the whole of it. The same change on the three + nearest siblings loses: updateCamelTribeBehavior -14.2, + updateLionWolfTribeBehavior -6.0, computeTribePercentages -2.1. Across the + namespace caching the base now stands at two clear wins (trimTribeToSize and + createTribe, +11.8 each, the latter also fixing displacements), this +1.4, one + flat, and five losses from -2.1 to -40.8. There is no predictor; measure. + */ + TribesState* const _tb = (TribesState*)((char*)this + tribeID * sizeof(Tribe)); short* psVar1; int sVar2; SomeTribeBehaviorTypeShort SVar3; @@ -37,28 +48,28 @@ namespace Map { int _x10; int _y10; int _targetUnitID; - sVar2 = this->tribes[tribeID].selectionTargetUnitID; + sVar2 = _tb->tribes[0].selectionTargetUnitID; _x10 = (int)DAT_UnitsState::instance.units[sVar2].x / 10; _y10 = (int)DAT_UnitsState::instance.units[sVar2].y / 10; iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; iVar6 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; BVar7 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( - tribeID, this->tribes[tribeID].uid); - this->tribes[tribeID].unkIsAnimalTribe = 1; + tribeID, _tb->tribes[0].uid); + _tb->tribes[0].unkIsAnimalTribe = 1; iVar8 = 80; if (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation) { iVar8 = 3; } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( tribeID, iVar8, Map::Units::UT_RABBIT); - if (this->tribes[tribeID].initialMoveOrderGiven == 0) { - _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; - this->tribes[tribeID].initialMoveOrderGiven = 1; + if (_tb->tribes[0].initialMoveOrderGiven == 0) { + _targetUnitID = _tb->tribes[0].selectionTargetUnitID; + _tb->tribes[0].initialMoveOrderGiven = 1; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, (uint)(DAT_UnitsState::instance.units[_targetUnitID].x), (uint)(DAT_UnitsState::instance.units[_targetUnitID].y), 0, 0, Map::Units::Instructions::UMSE_0); - this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + _tb->tribes[0].tribeBehaviorType = Map::Units::STBT_1; /* And from this one: the block at 0x52b440 ends in `ret 4` after setting the behaviour type, like its three sibling updaters. @@ -70,16 +81,16 @@ namespace Map { right after zeroing the counter. */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { - this->tribes[tribeID].unknownCounter01 = 0; + _tb->tribes[0].unknownCounter01 = 0; return; } - sVar2 = this->tribes[tribeID].predatorNearby; + sVar2 = _tb->tribes[0].predatorNearby; if (!sVar2) { - SVar3 = this->tribes[tribeID].tribeBehaviorType; + SVar3 = _tb->tribes[0].tribeBehaviorType; if (SVar3 == (SomeTribeBehaviorType)0) { - sVar4 = this->tribes[tribeID].unknownAttackRelatedUpdateCounter; + sVar4 = _tb->tribes[0].unknownAttackRelatedUpdateCounter; sVar2 = sVar4 + 1; - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = sVar2; + _tb->tribes[0].unknownAttackRelatedUpdateCounter = sVar2; if (!sVar4) { BVar7 = MACRO_CALL_MEMBER( Map::WildlifeState_Func::buildRallyPointPathForTribe, DAT_WildlifeState::ptr)(tribeID, 4); @@ -87,7 +98,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 2, 1); } - if (0 < this->tribes[tribeID].rallyPointCount) { + if (0 < _tb->tribes[0].rallyPointCount) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyLadderDestructionToTribeUnits, this)( tribeID); @@ -97,16 +108,16 @@ namespace Map { Signed: `cmp ax, dx` with a `jle`, and the index that follows is `movsx ax`, so a negative count does not wrap into a huge index. */ - sVar2 = (int)this->tribes[tribeID].rallyPointCount; + sVar2 = (int)_tb->tribes[0].rallyPointCount; if (0 < sVar2) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2 + -1][0]), - (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2 + -1][1]), 0, 0); + tribeID, (uint)((int)_tb->tribes[0].rallyPointArray[sVar2 + -1][0]), + (uint)((int)_tb->tribes[0].rallyPointArray[sVar2 + -1][1]), 0, 0); } } else if (BVar7) { - this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + _tb->tribes[0].tribeBehaviorType = Map::Units::STBT_1; + _tb->tribes[0].unknownAttackRelatedUpdateCounter = 0; } } else if (SVar3 == Map::Units::STBT_1) { /* @@ -114,35 +125,35 @@ namespace Map { 0x14 store and jumps away to the rocky-tile test. */ if (iVar6) { - this->tribes[tribeID].wanderIntervalTicks = 0x14; + _tb->tribes[0].wanderIntervalTicks = 0x14; } else if (0x14 < iVar5) { - this->tribes[tribeID].wanderIntervalTicks = 0x14; + _tb->tribes[0].wanderIntervalTicks = 0x14; } else { - this->tribes[tribeID].wanderIntervalTicks = 400; + _tb->tribes[0].wanderIntervalTicks = 400; } - psVar1 = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; + psVar1 = (short*)&_tb->tribes[0].unknownAttackRelatedUpdateCounter; *psVar1 = *psVar1 + 1; - if (this->tribes[tribeID].wanderIntervalTicks - <= this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { - this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + if (_tb->tribes[0].wanderIntervalTicks + <= _tb->tribes[0].unknownAttackRelatedUpdateCounter) { + _tb->tribes[0].tribeBehaviorType = ((SomeTribeBehaviorType)0); + _tb->tribes[0].unknownAttackRelatedUpdateCounter = 0; } } } else { if (sVar2 == 1) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 3, 0); - if (this->tribes[tribeID].rallyPointCount <= 0) { + if (_tb->tribes[0].rallyPointCount <= 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 3, 1); } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); } - psVar1 = (short*)&this->tribes[tribeID].predatorNearby; + psVar1 = (short*)&_tb->tribes[0].predatorNearby; *psVar1 = *psVar1 + 1; - if (2999 < this->tribes[tribeID].predatorNearby) { - this->tribes[tribeID].predatorNearby = 0; + if (2999 < _tb->tribes[0].predatorNearby) { + _tb->tribes[0].predatorNearby = 0; } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 20341c1d83..282e202f22 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49145,7 +49145,7 @@ SHC_3BB0A8C1_0x0052AD50 | 50.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B110 | 66.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B390 | 58.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B390 | 60.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B630 | 70.8% | Reimplemented, asm differs From 501f11fad1847e3b8c668b17d09a8a39bab323ba Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 23:39:40 +0200 Subject: [PATCH 085/202] fix: revert my own 0x80 cursor error in upgradeTribeArrayLayoutForMapVersion I changed the source cursor from 0x7a to 0xba earlier and asserted in the commit message that the import had it 128 bytes short. That was backwards. The arithmetic I used had 944 * 0x334 = 0xbcf40; the product is 0xbcfc0, and 0x28 + 944 * 0x334 + 0x38 + 0x7a * 2 == 0xbd114 which is exactly the `lea esi, [ecx + 0xbd114]` the original starts its loop with. 0xba put the cursor 0x80 too high, in a function that shuffles the whole tribe array on map load. reccmp cannot see this: 46.5% / 51.2% either way, because the immediate is treated as a resolver address. diff_anchor.py names it outright, and now reports 0 of 56 functions anchoring differently. I had not run that tool in this namespace before; its docstring says the anchor alone was worth 22 points elsewhere, and it is the only check that catches a wrong base when the opcodes still line up. Two habits, both already paid for twice in this work: run diff_anchor.py as a standard step, and do not call a change a correctness fix on hand arithmetic when a tool will verify it. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../upgradeTribeArrayLayoutForMapVersion.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 6b0f97bcb9..478af34579 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -29,12 +29,14 @@ namespace Map { iVar2 = 1249; _Dst = this->tribes + 0x4e1; /* - The source cursor starts at `this + 0xbd114`, which is tribes[944] + 0x1ac: - `unitSelectionBitMasked` sits at 0x38, so the index is (0x1ac - 0x38) / 2 = - 0xba. The import's 0x7a put it 128 bytes short, and every tribe this loop - moved came from the wrong place. + 0x7a, which puts the cursor at `this + 0xbd114` - the `lea esi, [ecx + + 0xbd114]` the original starts its loop with. I changed this to 0xba earlier on + arithmetic that used 944 * 0x334 = 0xbcf40; the product is 0xbcfc0, so the + import's index was right all along and 0xba put the cursor 0x80 too high. + 0x28 + 944 * 0x334 + 0x38 + 0x7a * 2 == 0xbd114. diff_anchor.py names this + error directly - use it rather than doing the arithmetic by hand. */ - _Src = this->tribes[944].unitSelectionBitMasked + 0xba; + _Src = this->tribes[944].unitSelectionBitMasked + 0x7a; do { if (_Dst != (Tribe*)_Src) { memmove(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); From 0c83c97720f63171af20624cf44651b9124daabe Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 23:52:02 +0200 Subject: [PATCH 086/202] reimplement: playUnitSelectionSound 50.4 -> 62.1%, three case labels were wrong The dispatch at 0x521a48 is six dword entries with a 48-entry byte index table at 0x521a60, indexed by `unitType - 0x1b`. Reading it corrects three labels the import had, two of which are behaviour bugs rather than layout: - 0x1b and 0x1c, swordsman and knight, share one arm at 0x521819. The import gave the knight the 0x56/0x59 arm at 0x5218aa instead, which belongs to 0x1d, the ladderman - a type the function did not handle at all. - the siege arm at 0x521983 covers 0x29, 0x3a and 0x3b: mangonel, tower, battering ram. The import had UT_S_CATAPULT (0x27) for the first, which the table sends to the default, so catapults played nothing and mangonels got no engineer speech. - the arm at 0x521a11 is reached only by 0x1f, the miner, yet its body compares the unit's own type against 0x1e, the engineer. Both are right: the majority type that picks the arm and the unit the sound plays on need not be the same unit. The import labelled it UT_E_ENGINEER, which inverted it - engineers got the speech, miners nothing. Physical order by address puts the archer arm between the ladderman and the siege engines, which is not the order any grouping of the enum suggests. updateTribeCombatStanceBehavior 57.6 -> 65.3%: the non-zero arm comes first. The original tests the target id sixteen bits wide and skips forward when it is zero (`test ax, ax; je 0x8d`), so the block that runs when the tribe still has a target is the fall-through. Written `if (!id)` it landed past the stance loop, displacing 25 instructions down to the UIT_UNIT_ATTACK_UNIT call. Both were found by diff_jcc.py, which flagged them as inverted branches. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/playUnitSelectionSound.cpp | 83 ++++++++++++------- .../updateTribeCombatStanceBehavior.cpp | 52 +++++++----- 2 files changed, 83 insertions(+), 52 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 1d42c26ee3..271cd39e1b 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -28,8 +28,31 @@ namespace Map { = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); UnitType const UVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(param_1, ¶m_1); + /* + Arm order and the case labels both come from the dispatch at 0x521a48: six + dword entries and a 48-entry byte index table at 0x521a60, indexed by + `unitType - 0x1b`. Three of the labels the import had were wrong. + + 0x1b and 0x1c - swordsman and knight - share one arm, 0x521819. The + 0x56/0x59 arm at 0x5218aa belongs to 0x1d, the ladderman, not the knight. + The siege arm at 0x521983 covers 0x29, 0x3a and 0x3b - mangonel, tower and + battering ram - so the first of the three is UT_S_MANGONEL, not + UT_S_CATAPULT (0x27), which falls to the default and plays nothing. + + The last one is odd rather than wrong: the arm at 0x521a11 is reached only + by 0x1f, the miner, yet its body compares the unit's own type against 0x1e, + the engineer (`cmp word ptr [esi + 0x13885da], 0x1e`). The majority type + that selects the arm and the type of the unit the sound plays on are two + different units, so this is reachable; but labelling it UT_E_ENGINEER, as + the import did, gave the speech to engineers and nothing to miners. + + Physical order by address is 0x521819, the default `ret 4` at 0x521876, + then 0x5218aa, 0x521915, 0x521983, 0x521a11 - so the archer arm sits + between the ladderman and the siege engines. + */ switch (UVar4) { case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: /* Three separate blocks at 0x521819, each with its own `ret 4`, and a selection below three that is neither one nor two plays nothing. @@ -53,7 +76,7 @@ namespace Map { (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0xaf); return; - case Map::Units::UT_E_KNIGHT: { + case Map::Units::UT_E_LADDER: { /* The arm at 0x5218aa: one shared play of the size-dependent effect, then 0x59. A selection of two or three gets 0x57 as well, and one of four or @@ -84,16 +107,28 @@ namespace Map { (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); return; } - case Map::Units::UT_E_ENGINEER: - if (DAT_UnitsState::instance.units[iVar3].unitType != Map::Units::UT_E_ENGINEER) { - return; - } - if (DAT_UnitsState::instance.units[iVar3].resourceToDeposit == 0) { - return; + case Map::Units::UT_A_HARCHER: { + /* + The arm at 0x521915 keeps a pointer to each coordinate (`lea edi, [esi + + 0x1388612]`, `lea esi, [esi + 0x1388610]`) and reads through them four + times rather than re-indexing the unit. + */ + short* const _unitY = &DAT_UnitsState::instance.units[iVar3].y; + short* const _unitX = &DAT_UnitsState::instance.units[iVar3].x; + int sfxOffsetInArray; + if (param_1 == 1) { + sfxOffsetInArray = 0xea; + } else if (param_1 <= 4) { + sfxOffsetInArray = 0xeb; + } else { + sfxOffsetInArray = 0xec; } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0x24); - return; - case Map::Units::UT_S_CATAPULT: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)*_unitX, (int)(*_unitY), sfxOffsetInArray); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)*_unitX, (int)(*_unitY), 0x59); + } + case Map::Units::UT_S_MANGONEL: case Map::Units::UT_S_TOWER: case Map::Units::UT_S_BATTERINGRAM: { int const _requiredEngineers = MACRO_CALL_MEMBER( @@ -128,27 +163,15 @@ namespace Map { } return; } - case Map::Units::UT_A_HARCHER: { - /* - The arm at 0x521915 keeps a pointer to each coordinate (`lea edi, [esi + - 0x1388612]`, `lea esi, [esi + 0x1388610]`) and reads through them four - times rather than re-indexing the unit. - */ - short* const _unitY = &DAT_UnitsState::instance.units[iVar3].y; - short* const _unitX = &DAT_UnitsState::instance.units[iVar3].x; - int sfxOffsetInArray; - if (param_1 == 1) { - sfxOffsetInArray = 0xea; - } else if (param_1 <= 4) { - sfxOffsetInArray = 0xeb; - } else { - sfxOffsetInArray = 0xec; + case Map::Units::UT_MINER: + if (DAT_UnitsState::instance.units[iVar3].unitType != Map::Units::UT_E_ENGINEER) { + return; } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)*_unitX, (int)(*_unitY), sfxOffsetInArray); - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)*_unitX, (int)(*_unitY), 0x59); - } + if (DAT_UnitsState::instance.units[iVar3].resourceToDeposit == 0) { + return; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0x24); + return; } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp index 9257c0ac61..8a5f23378a 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp @@ -48,7 +48,36 @@ namespace Map { UnitInstructionType _unitInstructionType; _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; _buildingIndex_2 = this->tribes[tribeID].someUnitID; - if (!_buildingIndex_2) { + /* + The non-zero arm comes first. The original tests the id sixteen bits wide + and skips forward when it is zero - `test ax, ax; je 0x8d` - so the block + that runs when the tribe still has a target unit is the fall-through. Written + the other way round (`if (!id)`) that whole 25-instruction block, down to the + UIT_UNIT_ATTACK_UNIT call, is emitted past the stance loop and every branch + in it is displaced. + */ + if (_buildingIndex_2) { + iVar5 = (int)_buildingIndex_2; + if (DAT_UnitsState::instance.units[iVar5].uid == this->tribes[tribeID].someUnitUID) { + if ((int)DAT_UnitsState::instance.units[iVar5].destinationX_2Unk + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[iVar5].destinationY_2Unk] + .addXgetTile + == this->tribes[tribeID].someTile2) { + _buildingIndex_2 = this->tribes[tribeID].field166_0x2be; + if (_buildingIndex_2) { + this->tribes[tribeID].field166_0x2be = _buildingIndex_2 + -1; + goto LAB_0052aa77; + } + if (DAT_UnitsState::instance.units[iVar5].tile == this->tribes[tribeID].someTile) + goto LAB_0052aa77; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + tribeID, Map::Units::UIT_UNIT_ATTACK_UNIT, iVar5, this->tribes[tribeID].someUnitUID, 1); + } else { + this->tribes[tribeID].someUnitID = 0; + } + } else { if (this->tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { local_c = 0; do { @@ -88,27 +117,6 @@ namespace Map { local_c = local_c + 1; } while (local_c < 10); } - } else { - iVar5 = (int)_buildingIndex_2; - if (DAT_UnitsState::instance.units[iVar5].uid == this->tribes[tribeID].someUnitUID) { - if ((int)DAT_UnitsState::instance.units[iVar5].destinationX_2Unk - + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[iVar5].destinationY_2Unk] - .addXgetTile - == this->tribes[tribeID].someTile2) { - _buildingIndex_2 = this->tribes[tribeID].field166_0x2be; - if (_buildingIndex_2) { - this->tribes[tribeID].field166_0x2be = _buildingIndex_2 + -1; - goto LAB_0052aa77; - } - if (DAT_UnitsState::instance.units[iVar5].tile == this->tribes[tribeID].someTile) - goto LAB_0052aa77; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - tribeID, Map::Units::UIT_UNIT_ATTACK_UNIT, iVar5, this->tribes[tribeID].someUnitUID, 1); - } else { - this->tribes[tribeID].someUnitID = 0; - } } goto LAB_0052aa77; LAB_0052a9f2: From a24905583192246dbc5a3bc75c24c05ac4e4af36 Mon Sep 17 00:00:00 2001 From: Gynt Date: Wed, 21 Oct 2026 23:59:28 +0200 Subject: [PATCH 087/202] reimplement: updateRabbitTribeBehavior 60.3 -> 63.0%, rally count is a short The original loads the count `movzx eax, word ptr [esi + 0x28a]`, compares it sixteen bits wide (`cmp ax, dx` with a signed `jle`) and widens only where it indexes, `movsx eax, ax`. An `int` local compares `cmp eax, edx` and loses the pair. The extension on the load says nothing about the field's signedness - MSVC may zero-extend because nothing above bit 15 is read, and picks the sign-extend at the one point that does - so rallyPointCount stays `short` in Tribe.hpp, where a dozen `<= 0` and `< 1` tests already rely on it being signed. Found by diff_types.py. I first bundled this with two other sign changes in the same function, splitting the decompiler's `sVar2` scratch into typed locals for the predator flag and the attack counter on the same 16-bit-compare reasoning. That measured 60.3 -> 43.3. Isolating the one change with direct asm evidence gives +2.7; the split is what cost 17 points, and it is withdrawn. playUnitCombatSpeechForTarget 29.6 -> 30.8%: arm order from the dispatch at 0x521e50 (eight dwords, 55-entry byte table at 0x521e70, index `unitType - 0x16`). All the import's labels were right, the order was not: knight and swordsman precede the spearman, and the engineer arm is last of all, past the default block at 0x521df7. It had been sitting between the knight's body and the Arabian swordsman with the knight falling through into its bare `return`, so moving it needs the knight's own `return` made explicit. Only +1.2, so the bulk of that function's gap is elsewhere. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../playUnitCombatSpeechForTarget.cpp | 116 ++++++++++-------- .../TribesState/updateRabbitTribeBehavior.cpp | 16 ++- 2 files changed, 77 insertions(+), 55 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index 81f8ddd465..a5a9b25661 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -40,6 +40,23 @@ namespace Map { this)(selectionID, (int*)&_ptrCurrentSelectionID); _ptrCurrentSelectionID_2 = _ptrCurrentSelectionID; short sVar1 = DAT_TribesState::instance.tribes[selectionID].size; + /* + Arm order comes from the dispatch at 0x521e50: eight dword entries with a + 55-entry byte index table at 0x521e70, indexed by `unitType - 0x16`. The + labels the import had are all correct; the order was not. By address the + arms run + + 0x521ae3 the archer group, 0x521c2d knight, 0x521cd3 swordsman, + 0x521d5b spearman, 0x521d7e pike/mace/monk, 0x521dc8 Arabian swordsman, + 0x521df7 the default, and 0x521e48 engineer + + so knight and swordsman come before the spearman, and the engineer arm is + last of all - past the default block. The import had the engineer arm + sitting between the knight's body and the Arabian swordsman, where the + knight fell through into its bare `return`; moved to the end it needs the + knight's own `return` made explicit, exactly as UIT_LIGHT_PITCH did in + giveTribeAnInstruction once it stopped being the final arm. + */ switch (_ptrptrCurrentSelectionID) { case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_XBOW: @@ -116,54 +133,6 @@ namespace Map { (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)DAT_UnitsState::instance.units[_otherUnitID].y, 0x59); return; - case Map::Units::UT_E_SPEAR: - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) { - return; - } - _otherX = 0x14; - break; - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_MONK: - UVar2 = DAT_UnitsState::instance.units[unitID].unitType; - if (UVar2 == Map::Units::UT_E_KNIGHT || UVar2 == Map::Units::UT_E_SWORD - || UVar2 == Map::Units::UT_LORD) { - _otherX = 0x11; - } else { - _otherX = 0x15; - } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, _otherX); - if (sVar1 < 0xf) { - return; - } - _otherX = 0x14; - break; - case Map::Units::UT_E_SWORD: - if (_ptrCurrentSelectionID == (TribesState*)0x1) { - _otherX = 0xad; - } else if (_ptrCurrentSelectionID == (TribesState*)0x2) { - _otherX = 0xae; - } else { - if ((int)_ptrCurrentSelectionID < 3) - goto LAB_00521d1b; - _otherX = 0xaf; - } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); - LAB_00521d1b: - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) { - return; - } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); - return; case Map::Units::UT_E_KNIGHT: if (_ptrCurrentSelectionID == (TribesState*)0x1) { _otherX = 0x56; @@ -210,8 +179,55 @@ namespace Map { } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x11); - case Map::Units::UT_E_ENGINEER: return; + case Map::Units::UT_E_SWORD: + if (_ptrCurrentSelectionID == (TribesState*)0x1) { + _otherX = 0xad; + } else if (_ptrCurrentSelectionID == (TribesState*)0x2) { + _otherX = 0xae; + } else { + if ((int)_ptrCurrentSelectionID < 3) + goto LAB_00521d1b; + _otherX = 0xaf; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); + LAB_00521d1b: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); + if (sVar1 < 0xf) { + return; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); + return; + case Map::Units::UT_E_SPEAR: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); + if (sVar1 < 0xf) { + return; + } + _otherX = 0x14; + break; + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_MONK: + UVar2 = DAT_UnitsState::instance.units[unitID].unitType; + if (UVar2 == Map::Units::UT_E_KNIGHT || UVar2 == Map::Units::UT_E_SWORD + || UVar2 == Map::Units::UT_LORD) { + _otherX = 0x11; + } else { + _otherX = 0x15; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (Map::Units::UnitType)_ptrptrCurrentSelectionID, _otherX); + if (sVar1 < 0xf) { + return; + } + _otherX = 0x14; + break; case Map::Units::UT_A_SWORDSMAN: if (sVar1 < 0xf) { return; @@ -225,6 +241,8 @@ namespace Map { (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); return; + case Map::Units::UT_E_ENGINEER: + return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 292a509c3d..1f3412f9a8 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -41,6 +41,7 @@ namespace Map { int sVar2; SomeTribeBehaviorTypeShort SVar3; int sVar4; + short _rallyPointCount; int iVar5; int iVar6; BOOLEnum BVar7; @@ -105,15 +106,18 @@ namespace Map { } } else if (sVar2 == 0x96) { /* - Signed: `cmp ax, dx` with a `jle`, and the index that follows is - `movsx ax`, so a negative count does not wrap into a huge index. + The count is read into a `short`: the original compares it + sixteen bits wide, `cmp ax, dx` with a signed `jle`, and widens + only for the index, `movsx eax, ax`. An `int` local compares + `cmp eax, edx` and loses the pair. The `jle` is still signed, so + a negative count does not wrap into a huge index. */ - sVar2 = (int)_tb->tribes[0].rallyPointCount; - if (0 < sVar2) { + _rallyPointCount = _tb->tribes[0].rallyPointCount; + if (0 < _rallyPointCount) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, (uint)((int)_tb->tribes[0].rallyPointArray[sVar2 + -1][0]), - (uint)((int)_tb->tribes[0].rallyPointArray[sVar2 + -1][1]), 0, 0); + tribeID, (uint)((int)_tb->tribes[0].rallyPointArray[(int)_rallyPointCount + -1][0]), + (uint)((int)_tb->tribes[0].rallyPointArray[(int)_rallyPointCount + -1][1]), 0, 0); } } else if (BVar7) { _tb->tribes[0].tribeBehaviorType = Map::Units::STBT_1; From 8ac1364d34d66c4f2a1d911f2030643e3dc9731a Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:16:01 +0200 Subject: [PATCH 088/202] fix: computeTribePercentages pinned percentageSomething at 100 The last of the five percentages was written as a guarded store followed by an unconditional `= 100`, so the field ended up 100 whatever the tally was. The original has it in exactly the same shape as the four above it - test the size once, 100 when it is zero and `local_8 * 100 / size` otherwise. It duplicates the epilogue rather than sharing one, storing to 0x2ca and returning in each arm (0x5272fc and 0x527315, each with its own `ret 4`), so it is written here as an early return instead of through the shared `_percentage`. 42.8 -> 43.4%. Also in that function: the tribe size is read in the loop guard rather than into a local. The original never loads it into a register at all - it compares the field in place against the zero it already holds in bp for the seven word stores above, `cmp word ptr [esi + 0x5c], bp`. The local was used exactly once. sortTribePathDestinationsByCost 28.8 -> 29.5%: both conditionals at the top were the wrong way round. The original computes x, then jumps away when the ram-and-horse count is zero (`je 0x1e`), so that call is the fall-through; inside the other branch `test ecx, ecx; je 0x7` falls through to the all-assassins call. While there I checked the note I had left on that function claiming the original uses clean indexed addressing where we use the decompiler's casts. That was wrong. The original holds the byte offset in a register and addresses absolutely - `lea eax,[ecx+ecx*2]; add eax,eax; add eax,eax; cmp dword ptr [eax + 0x12d5c6c], 0` - which is what the existing form already compiles to, and `(PathFindingStatePartB*)(climbData + 200)` folds to the same base: climbData is ClimbData[200] at 0x1084 with stride 516, so climbData+200 is 0x1a3a4, exactly searchQueue. Nothing to do there; the note is corrected in place. These were found with a new scratch scan for inverted branches at misaligned diff positions - diff_jcc.py only compares pairs the differ managed to line up, and both of the inversions that have paid real points sat in slipped blocks. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/computeTribePercentages.cpp | 24 +++++++++++---- .../playUnitCombatSpeechForTarget.cpp | 29 +++++++++++++++---- .../sortTribePathDestinationsByCost.cpp | 24 ++++++++++----- 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 91e58fd13b..a730b46d7a 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -35,7 +35,6 @@ namespace Map { int const _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; short _percentage = 0; int iVar4 = 0; - short sVar1 = this->tribes[tribeID].size; this->tribes[tribeID].percentageDyingUnk = 0; this->tribes[tribeID].percentageMovingUnk = 0; this->tribes[tribeID].percentageAttackingUnk = 0; @@ -50,7 +49,13 @@ namespace Map { int _countMoving = 0; int _countDying = 0; this->tribes[tribeID].unitType = DAT_UnitsState::instance.units[_targetUnitID].unitType; - if (0 < sVar1) { + /* + The size is read in the test, not into a local. The original never loads it + into a register: it compares the field in place against the zero it already + has in bp for the seven word stores above - `cmp word ptr [esi + 0x5c], bp`. + A local forces a load of its own. + */ + if (0 < this->tribes[tribeID].size) { do { _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)( tribeID, iVar4); @@ -125,10 +130,19 @@ namespace Map { _percentage = (short)((_countRangeUnk * 100) / _tribeSize); } this->tribes[tribeID].percentageShootingUnk = _percentage; - if (_tribeSize) { - this->tribes[tribeID].percentageSomething = (short)((local_8 * 100) / _tribeSize); + /* + The last percentage is the same shape as the four above it, not a guarded + store followed by an unconditional 100 - which is what the import had, and + it pinned the field at 100 whatever the tally was. The original tests the + size once and duplicates the epilogue, storing to 0x2ca and returning in + each arm (0x5272fc and 0x527315, each with its own `ret 4`), so it is + written as an early return rather than through the shared `_percentage`. + */ + if (!_tribeSize) { + this->tribes[tribeID].percentageSomething = 100; + return; } - this->tribes[tribeID].percentageSomething = 100; + this->tribes[tribeID].percentageSomething = (short)((local_8 * 100) / _tribeSize); } } diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index a5a9b25661..2b4d05b48e 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -39,7 +39,24 @@ namespace Map { _ptrptrCurrentSelectionID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(selectionID, (int*)&_ptrCurrentSelectionID); _ptrCurrentSelectionID_2 = _ptrCurrentSelectionID; - short sVar1 = DAT_TribesState::instance.tribes[selectionID].size; + /* + An `int`, not a `short`. The original reads the size once, + `movsx ebp, word ptr [edi + 0x1667fd4]` at 0x521abc, and keeps the + sign-extended value in ebp for the whole function - all five `< 0xf` tests + compare against it. A `short` local gets a `movzx` here instead. + giveTribeAnInstruction reads the same field the same way, `movsx esi, word + ptr [ebp + 0x5c]`, and wanted `int` for the same reason. + + That is as far as it generalises, and it is worth only +0.1 even here. I + took the two confirmed cases as a rule and changed the `.size` local to + `int` in the nine other functions in this namespace that read it into a + `short`. One build measured all nine: + addUnitsToTribeAndComputeMovementSpeed -21.2, processDeerMoving -1.4, + createAnimal -0.4, updateTribeUnitAssignments +0.1, and five flat. `short` + is right in almost all of them. Read the load at the site; do not carry + this across functions. + */ + int const _tribeSize = DAT_TribesState::instance.tribes[selectionID].size; /* Arm order comes from the dispatch at 0x521e50: eight dword entries with a 55-entry byte index table at 0x521e70, indexed by `unitType - 0x16`. The @@ -157,7 +174,7 @@ namespace Map { (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x59); MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) { + if (_tribeSize < 0xf) { return; } _otherX = 0xfa; @@ -196,7 +213,7 @@ namespace Map { LAB_00521d1b: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) { + if (_tribeSize < 0xf) { return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( @@ -206,7 +223,7 @@ namespace Map { case Map::Units::UT_E_SPEAR: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (sVar1 < 0xf) { + if (_tribeSize < 0xf) { return; } _otherX = 0x14; @@ -223,13 +240,13 @@ namespace Map { } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (Map::Units::UnitType)_ptrptrCurrentSelectionID, _otherX); - if (sVar1 < 0xf) { + if (_tribeSize < 0xf) { return; } _otherX = 0x14; break; case Map::Units::UT_A_SWORDSMAN: - if (sVar1 < 0xf) { + if (_tribeSize < 0xf) { return; } /* diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp index 4c53263ca5..855c882d41 100644 --- a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -47,18 +47,26 @@ namespace Map { BOOLEnum BVar8 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); short sVar1 = this->tribes[tribeID].selectionTargetUnitID; x = (uint)DAT_UnitsState::instance.units[sVar1].x; - if (!horseAndRamCount) { + /* + Both of these were the wrong way round. The original computes x before the + first test and jumps away when the count is zero - `cmp dword ptr [esp + + 0x10], 0; je 0x1e` - so the ram-and-horse call is the fall-through, and + inside the other branch `test ecx, ecx; je 0x7` falls through to the + all-assassins call. Written `if (!count)` and `if (!BVar8)` both bodies are + emitted in the far block and every branch past them shifts. + */ + if (horseAndRamCount) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::calculatePathKeepAndWallsGatesNotAllowed, + DAT_PathFindingState::ptr)(x, (int)(DAT_UnitsState::instance.units[sVar1].y), -1, -1, 100000); + } else { y = (uint)DAT_UnitsState::instance.units[sVar1].y; - if (!BVar8) { - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, - DAT_PathFindingState::ptr)(x, y, -1, -1, 100000, FALSE); - } else { + if (BVar8) { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, DAT_PathFindingState::ptr)(x, y, 0xffffffff, 0xffffffff, 100000, 0); + } else { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(x, y, -1, -1, 100000, FALSE); } - } else { - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::calculatePathKeepAndWallsGatesNotAllowed, - DAT_PathFindingState::ptr)(x, (int)(DAT_UnitsState::instance.units[sVar1].y), -1, -1, 100000); } if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 != 0) { _p = &DAT_PathFindingState::instance.searchQueue; From 5667061944535d5d85766cfdaf57299d45037b15 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:22:10 +0200 Subject: [PATCH 089/202] reimplement: trySpawnAdditionalWildlifeForTribe 87.2 -> 100%, flat guards All three of its tests already had the right sense; what differed was where the merged `return 0` landed. The original puts that block between the third guard and the success body, so the first two jump forward into it (`jl 0x23`, `jl 0x15`) and the third jumps over it (`jl 0xa`). Nested inside one `&&` chain, MSVC moves the block to the end of the function and the third test inverts to a `jge` reaching back for it. Flattening the chain into early returns puts it back, and incidentally removes a comma operator inside an `&&`. updateTribeCombatStanceBehavior 65.3 -> 75.1%, two changes: - the attack counter is tested signed and sixteen bits wide. The field is a ushort, so testing it directly gave an unsigned `jbe`; the original has `movzx eax, word ptr [esi + 0x56]`, `cmp ax, 0x32`, `jle`. A `short` local reproduces both. The field's own type is left alone - 43 places read it, most outside this namespace. - the rallying arm comes first. `cmp word ptr [esi + 0x22e], bx; je 0x109` jumps away when the flag is clear, so the tribe-is-rallying body is the fall-through. Both of this function's inversions came from diff_inv_jcc.py, added here. diff_jcc.py only compares jcc pairs the differ managed to align, and an inversion tends to misalign the block it sits in, so these never showed up there. It has now found the two changes above, sortTribePathDestinationsByCost's pair, and this function's first inversion (+7.7 earlier). It reports false positives - any orig jcc is paired against any opposite recomp jcc in the same block - so its docstring says to read the diff around each hit first. Also withdrawn, with the measurement recorded in place: making updateRabbitTribeBehavior's predator flag a `short`. diff_inv_jcc flags it and the original does test it sixteen bits wide, but it measures 63.0 -> 43.5%. Tested alone this time, so the 17 points the earlier bundle lost belong to that flag specifically, not to the attack counter beside it. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../trySpawnAdditionalWildlifeForTribe.cpp | 37 +++++-- .../TribesState/updateRabbitTribeBehavior.cpp | 9 ++ .../updateTribeCombatStanceBehavior.cpp | 103 ++++++++++-------- .../batch/diff_inv_jcc.py | 60 ++++++++++ 4 files changed, 153 insertions(+), 56 deletions(-) create mode 100644 tools/reimplementation-helper/batch/diff_inv_jcc.py diff --git a/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp index 233066fb65..43e29c72f8 100644 --- a/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp @@ -51,19 +51,32 @@ namespace Map { int sVar3 = DAT_UnitsState::instance.units[iVar6].y; int sVar4 = DAT_UnitsState::instance.units[iVar6].terrainOrClimbHeight; this->tribes[param_1].field_0x288 = this->tribes[param_1].field_0x288 + 1; - if (this->tribes[param_1].field_0x288 >= param_2 - && (this->tribes[param_1].field_0x288 = 0, 100 <= bVar5)) { - iVar6 = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::countLivingNondyingUnitsForPlayer, DAT_UnitsState::ptr)(param_4); - if (iVar6 < param_3) { - if (param_4 == 0x2f) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( - Commands::M_MAPPER_CAMEL, (uint)(sVar2), (uint)(sVar3), (int)(sVar4)); - } - return (undefined4)(1); - } + /* + Flat guards, not a nested chain. All three tests already had the right + sense; what differed was where the merged `return 0` landed. The + original puts it between the third guard and the success body, so the + first two jump forward into it (`jl 0x23`, `jl 0x15`) and the third + jumps over it (`jl 0xa`) - which is what early returns give. Nested, + MSVC moves the block to the end of the function and the third test + inverts to a `jge` reaching back for it. + */ + if (this->tribes[param_1].field_0x288 < param_2) { + return (undefined4)(0); } - return (undefined4)(0); + this->tribes[param_1].field_0x288 = 0; + if (bVar5 < 100) { + return (undefined4)(0); + } + iVar6 = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::countLivingNondyingUnitsForPlayer, DAT_UnitsState::ptr)(param_4); + if (iVar6 >= param_3) { + return (undefined4)(0); + } + if (param_4 == 0x2f) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( + Commands::M_MAPPER_CAMEL, (uint)(sVar2), (uint)(sVar3), (int)(sVar4)); + } + return (undefined4)(1); } return (undefined4)(0); } diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 1f3412f9a8..56df2b0e93 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -111,6 +111,15 @@ namespace Map { only for the index, `movsx eax, ax`. An `int` local compares `cmp eax, edx` and loses the pair. The `jle` is still signed, so a negative count does not wrap into a huge index. + + This is the only local here that wants narrowing, and the + reasoning does not carry to its neighbours. diff_inv_jcc.py + still reports `cmp ax, dx` against our `cmp eax, edx` for the + predator flag at the top of the function, and the original + really does test that one sixteen bits wide too - but making it + a `short` measures 63.0 -> 43.5%. Tested alone, so it is the + predator flag specifically, not the attack counter it was first + bundled with. Leave it as an `int` and ignore the hint. */ _rallyPointCount = _tb->tribes[0].rallyPointCount; if (0 < _rallyPointCount) { diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp index 8a5f23378a..be54debdc0 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp @@ -146,8 +146,65 @@ namespace Map { LAB_0052aa77: psVar1 = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; *psVar1 = *psVar1 + 1; - if (0x32 < this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { - if (this->tribes[tribeID].isRallyingUnk == 0) { + /* + Signed, and sixteen bits wide. The field is a ushort, so testing it directly + gives an unsigned `jbe`; the original has `movzx eax, word ptr [esi + 0x56]`, + `cmp ax, 0x32` and a signed `jle`. A `short` local reproduces both - the load + may zero-extend because nothing above bit 15 is read, and the test stays + signed. The field type is left alone: 43 places read it, most outside this + namespace. + */ + short const _attackCounter = (short)this->tribes[tribeID].unknownAttackRelatedUpdateCounter; + if (0x32 < _attackCounter) { + /* + The rallying arm comes first: `xor ebx, ebx; cmp word ptr [esi + 0x22e], + bx; je 0x109` jumps away when the flag is clear, so the tribe-is-rallying + body is the fall-through and the aggressive-stance body goes in the far + block. + */ + if (this->tribes[tribeID].isRallyingUnk) { + BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::allUnitsReachedTheirDestination, this)( + tribeID); + /* + The import had this guard with an empty body; nothing advances the + rally point until the tribe has arrived. + */ + if (BVar3) { + _buildingIndex_2 = this->tribes[tribeID].rallyPointCount; + iVar5 = this->tribes[tribeID].currentRallyPointIndex + 1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + if (_buildingIndex_2 <= iVar5) { + if (this->tribes[tribeID].isRallyingUnk == -1) { + this->tribes[tribeID].currentRallyPointIndex = 0; + this->tribes[tribeID].isRallyingUnk = 0; + } + iVar5 = 0; + } + if (this->tribes[tribeID].isRallyingUnk != 0) { + x1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][0]; + y1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][1]; + this->tribes[tribeID].currentRallyPointIndex = (short)iVar5; + UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType; + if (UVar2 == Map::Units::UT_E_ARCHER || UVar2 == Map::Units::UT_E_XBOW + || UVar2 == Map::Units::UT_A_ARCHER || UVar2 == Map::Units::UT_A_SLINGER + || UVar2 == Map::Units::UT_A_FIRETHROWER) { + if (0x1b0 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + } + } else if (UVar2 == Map::Units::UT_A_HARCHER) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, x1, y1, 0, 0); + } else if ((0x60 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) + || (this->tribes[tribeID].unitStance != Map::Units::Behavior::USE_AGGRESSIVE)) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( + tribeID, x1, y1, 0, 0); + } + } + } + } else { if (this->tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { psVar1 = &this->tribes[tribeID].aggressiveStanceCounter; *psVar1 = *psVar1 + 1; @@ -206,48 +263,6 @@ namespace Map { } } } - } else { - BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::allUnitsReachedTheirDestination, this)( - tribeID); - /* - The import had this guard with an empty body; nothing advances the - rally point until the tribe has arrived. - */ - if (BVar3) { - _buildingIndex_2 = this->tribes[tribeID].rallyPointCount; - iVar5 = this->tribes[tribeID].currentRallyPointIndex + 1; - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - if (_buildingIndex_2 <= iVar5) { - if (this->tribes[tribeID].isRallyingUnk == -1) { - this->tribes[tribeID].currentRallyPointIndex = 0; - this->tribes[tribeID].isRallyingUnk = 0; - } - iVar5 = 0; - } - if (this->tribes[tribeID].isRallyingUnk != 0) { - x1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][0]; - y1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][1]; - this->tribes[tribeID].currentRallyPointIndex = (short)iVar5; - UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType; - if (UVar2 == Map::Units::UT_E_ARCHER || UVar2 == Map::Units::UT_E_XBOW - || UVar2 == Map::Units::UT_A_ARCHER || UVar2 == Map::Units::UT_A_SLINGER - || UVar2 == Map::Units::UT_A_FIRETHROWER) { - if (0x1b0 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - tribeID, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); - } - } else if (UVar2 == Map::Units::UT_A_HARCHER) { - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, x1, y1, 0, 0); - } else if ((0x60 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) - || (this->tribes[tribeID].unitStance != Map::Units::Behavior::USE_AGGRESSIVE)) { - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, x1, y1, 0, 0); - } - } - } } LAB_0052ac8d: if (this->tribes[tribeID].pendingScatterMove != 0) { diff --git a/tools/reimplementation-helper/batch/diff_inv_jcc.py b/tools/reimplementation-helper/batch/diff_inv_jcc.py new file mode 100644 index 0000000000..be11cddab4 --- /dev/null +++ b/tools/reimplementation-helper/batch/diff_inv_jcc.py @@ -0,0 +1,60 @@ +"""Inverted conditional jumps, including where the diff has slipped out of alignment. + +An `if`/`else` whose arms are the wrong way round costs far more than the one branch +it flips: the arm the original falls through into gets emitted in the far block +instead, and every branch past it shifts. Two of these were worth +7.7 +(updateTribeCombatStanceBehavior) and +0.7 (sortTribePathDestinationsByCost, which +had two in its first ten lines). + +diff_jcc.py only compares jcc pairs the differ managed to line up, and an inversion +tends to misalign the very block it sits in - so neither of those showed up there. +This walks each diff block and reports an orig-only jcc facing a recomp-only jcc of +the opposite sense. + +Not every hit is real: any orig jcc in a block is paired against any opposite recomp +jcc in the same block, so expect false positives in blocks holding several branches. +Read the diff around each hit before changing anything. A good sign is the original +jumping a short distance where ours jumps a long one, or vice versa. + +usage: diff_inv_jcc.py (reads the last reccmp run; re-run reccmp first) +""" +import sys, os, re +sys.path.insert(0, os.path.join("tools", "reimplementation-helper", "batch")) +import common + +INV = {} +for a, b in [("je","jne"),("jz","jnz"),("jl","jge"),("jle","jg"),("jb","jae"), + ("jbe","ja"),("js","jns"),("jo","jno"),("jp","jnp")]: + INV[a] = b; INV[b] = a + +def mnem(row): + return row.split()[0].lower() if row.strip() else "" + +hits = 0 +for f, e, addr in common.entries(common.load_diff()): + if not e or float(e["matching"]) >= 1.0: + continue + found = [] + for hunk in e.get("diff") or []: + for block in hunk[1]: + o = [r[1] for r in block.get("orig", [])] + r_ = [r[1] for r in block.get("recomp", [])] + if not o or not r_: + continue + oj = [x for x in o if mnem(x) in INV] + rj = [x for x in r_ if mnem(x) in INV] + for x in oj: + for y in rj: + if INV[mnem(x)] == mnem(y): + found.append((mnem(x), mnem(y), o[0][:58], r_[0][:58])) + break + else: + continue + break + if found: + hits += 1 + print("### %s %.1f%% (%d)" % (e["name"].split("::")[-1], float(e["matching"])*100, len(found))) + for om, rm, oc, rc in found[:3]: + print(" orig %-4s / ours %-4s orig-block: %s" % (om, rm, oc)) + print(" %34s ours-block: %s" % ("", rc)) +print("\n%d function(s) with an inverted branch" % hits) From 03d62bed64e50ebf21089d2054c68080ff5d3224 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:26:37 +0200 Subject: [PATCH 090/202] reimplement: predictUnitInterceptPosition 31.1 -> 31.7%, flat guards Same shape as trySpawnAdditionalWildlifeForTribe: a loop nested inside one `&&` chain whose later terms were comma operators doing the loop's own setup. Flattened into early returns, which also drops the two comma operators. Also retried the split on isTribePathToDestinationClear, where a note said the `||` chains must stay as they are on the strength of quick_diff 0.6635 -> 0.6296 - below the ratio that tool's own docs call reliable, and never confirmed. reccmp says 18.8% -> 17.4%, so the note was right; it now records the real measurement, and what that function is actually missing: the original merges every failure into one `return 0` at 0x524cd7 placed inline after the first chain, with the second chain jumping backward into it, where ours emits the block at the end. It also allocates two proper local slots (`sub esp, 8`) where we allocate one and reuse the dead tribeID parameter slot for the second counter. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../isTribePathToDestinationClear.cpp | 20 +++++++++++++++---- .../predictUnitInterceptPosition.cpp | 20 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index ad0f7d584c..a9520c0a00 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -18,10 +18,22 @@ namespace Map { using WindowsHelper::Enums::BOOLEnum; /* - The two guards stay as `||` chains. Splitting them into six separate `if`s, which - is what recovered trySpawnAdditionalWildlifeForTribe's inline return blocks, - measures worse here (quick_diff 0.6635 -> 0.6296): the original merges every - failure into one `return 0` at 0x524cd7 rather than shrink-wrapping them. + The two guards stay as `||` chains. The original merges every failure into a + single `return 0` at 0x524cd7 and places it inline right after the first chain, + so the second chain's tests jump backward into it (`ja 0x00524cd7`, encoded + `77ea`) - a full epilogue, `pop/pop/pop/xor eax,eax/pop/pop/ret 0x14`, not a + jump to a shared one. Ours emits that block at the end of the function instead, + and that placement is what the remaining gap is mostly made of. + + Splitting the chains into six separate `if`s is what took + trySpawnAdditionalWildlifeForTribe from 87.2% to a full match, so it was worth + retrying here. Measured with reccmp: 18.8% -> 17.4%. The earlier note against + it cited quick_diff 0.6635 -> 0.6296, which is below the ratio that tool is + documented to be reliable at; the conclusion was right anyway. + + What ours also lacks is the original's second local slot: it allocates one + (`push ecx`) and reuses the dead tribeID parameter slot for the second counter, + where the original has `sub esp, 8` and two proper locals. */ // FUNCTION: STRONGHOLDCRUSADER 0x00524CA0 undefined4 TribesState::isTribePathToDestinationClear( diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index b4bf58eae1..d1ad15bb7b 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -82,10 +82,22 @@ namespace Map { if (_yOff_2b < _xOff_2b) { _yOff_2b = _xOff_2b; } - if ((-1 < (_yOff_2b + -2) * unitID) - && (_currentIndexInPathPlan = (int)DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan, - local_8 = _tribeMinSpeedBounded * 4, - (int)_currentIndexInPathPlan < (int)DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan)) { + /* + Flat guards. Nesting the loop inside one `&&` chain puts the merged exit at + the end of the function; the original reaches it directly from each test. + This is the same shape that took trySpawnAdditionalWildlifeForTribe from + 87.2% to a full match, and it drops two comma operators from inside an `&&`. + */ + if ((_yOff_2b + -2) * unitID < 0) { + return; + } + _currentIndexInPathPlan = (int)DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan; + local_8 = _tribeMinSpeedBounded * 4; + if ((int)DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan + <= (int)_currentIndexInPathPlan) { + return; + } + { do { /* get current value in pathPlan, each byte contains two move instructions From 1f44aac160c95a91b4599295411afc2be66024a2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:32:31 +0200 Subject: [PATCH 091/202] measure: flattening guard chains is not a general win createTribeForPlayer looked like the clearest case left: the original returns zero through an epilogue of its own inline (`cmp edi, ebx; jg 0x9` over `pop/pop/pop/xor eax,eax/pop/ret 4`) where ours sends that return to the shared epilogue and inverts the test to `jle`. Flattening the `if (0 < slot)` around the stores into an early return measures 75.2% -> 73.2%. Reverted, with the number recorded next to the Tribe& surprise already noted on that function. The tally for this technique across four functions: trySpawnAdditionalWildlifeForTribe +12.8 (to a full match), predictUnitInterceptPosition +0.6, isTribePathToDestinationClear -1.4, createTribeForPlayer -2.0. Same shape as the base-caching tally - two clear wins and no way to pick them in advance, so it goes in the measure-don't-reason column. By contrast the inverted-arm fix is 3 for 3 so far (+7.7 and +9.8 on updateTribeCombatStanceBehavior, +0.7 on sortTribePathDestinationsByCost), which is why diff_inv_jcc.py is worth running first. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/createTribeForPlayer.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index 966ddafbe8..42bb68977c 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -26,6 +26,15 @@ namespace Map { createTribe from 30.3% to 67.2% - but here the identical change costs 37.3 points (75.2% -> 37.9%). The two functions differ only in how they find the slot, so the shape that matches one does not match the other. + + The `if (0 < slot)` around the stores also stays. The original returns zero + through an epilogue of its own right there - `cmp edi, ebx; jg 0x9` over + `pop/pop/pop/xor eax,eax/pop/ret 4` - where ours sends that return to the shared + epilogue and inverts the test to `jle`, so a flat guard looks like the obvious + fix. It measures 75.2% -> 73.2%. Flattening guard chains is as unpredictable as + caching the base: it took trySpawnAdditionalWildlifeForTribe to a full match + (+12.8) and gained +0.6 on predictUnitInterceptPosition, but costs 1.4 on + isTribePathToDestinationClear and 2.0 here. Measure it; do not assume it. */ // FUNCTION: STRONGHOLDCRUSADER 0x005227E0 int TribesState::createTribeForPlayer(int playerID) From 9fb7cfd3bc39cebb173b4f650e16e0d29eca446a Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:37:54 +0200 Subject: [PATCH 092/202] measure: moveTribeToIndexedNearbyTile's idiv is not reachable by naming the quotient The original divides once (`cdq`, `mov ecx, 0x28`, `idiv ecx`) and the unreachable arm returns the quotient the same instruction left in EAX. We emit a reciprocal multiply: `mov eax, 0x66666667`, `imul`, `sar edx, 4`, `shr`, four `add`s and a `sub` - eleven instructions for three, which looked like an easy eight back. Naming the quotient so MSVC can see both halves are wanted measures 45.9% -> 45.9%, and the reciprocal multiply is still in the output. MSVC1400 under /O2 converts division by any literal constant that way whether or not the remainder is also needed, so no arrangement of `/ 40` and `% 40` gets an `idiv`. For the original to have one its divisor cannot have been a literal in the source. Recorded on the function, because the note there previously implied the quotient was the lever. giveTribeMoveInstruction: dropped `_one` and `_one2`, two decompiler artifacts each assigned from a call and returned on the next line. Flat at 30.0%, so the two frame slots were not what that function's note is looking for, but the source is two locals and four lines shorter and the returns now read directly. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeMoveInstruction.cpp | 18 ++++++++---------- .../moveTribeToIndexedNearbyTile.cpp | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 6312e2136d..317fa775ab 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -54,8 +54,6 @@ namespace Map { UnitTypeShort UVar2; int _firstUnitID; int _navArea; - uint _one; - uint _one2; uint _someUnitCanClimb; dword _area3; int iVar7; @@ -122,10 +120,10 @@ namespace Map { (uint)(DAT_UnitsState::instance.units[_targetUnitID].x), (uint)(DAT_UnitsState::instance.units[_targetUnitID].y)); if (!_navArea) { - _one = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, - this)(tribeID, (undefined4)((int)(x1)), (undefined4)((int)(y1)), - (undefined4)((int)(rallyBool)), storeAsRallyPoint); - return (undefined4)(_one); + return (undefined4)(MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)(tribeID, + (undefined4)((int)(x1)), (undefined4)((int)(y1)), (undefined4)((int)(rallyBool)), + storeAsRallyPoint)); } _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; _bool1 = 1; @@ -133,10 +131,10 @@ namespace Map { } else { _area_2 = _targetUnitArea; if (!_targetUnitArea) { - _one2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, - this)(tribeID, (undefined4)((int)(x1)), (undefined4)((int)(y1)), - (undefined4)((int)(rallyBool)), storeAsRallyPoint); - return (undefined4)(_one2); + return (undefined4)(MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)(tribeID, + (undefined4)((int)(x1)), (undefined4)((int)(y1)), (undefined4)((int)(rallyBool)), + storeAsRallyPoint)); } } /* diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index b7228e4d79..a25dcd0285 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -26,10 +26,21 @@ namespace Map { uVar7 = (uint)DAT_UnitsState::instance.units[sVar1].x; int iVar2 = (int)this->tribes[param_1].someIndex; /* - Only the remainder is named. The original divides once, with `cdq`, `mov ecx, - 0x28`, `idiv ecx`, and the unreachable arm returns the quotient that same - instruction left in EAX; naming the quotient in a local of its own turns the - division into a reciprocal multiply and costs a stack slot. + The division here will not match from this direction, and naming the + quotient is not the lever. The original divides once - `cdq`, `mov ecx, + 0x28`, `idiv ecx` - and the unreachable arm returns the quotient that same + instruction left in EAX. Ours emits a reciprocal multiply instead: + `mov eax, 0x66666667`, `imul`, `sar edx, 4`, `shr`, four `add`s, a `sub`. + + Naming the quotient in a local of its own, so that MSVC can see both halves + are wanted, does not help: measured 45.9% -> 45.9%, and the reciprocal + multiply is still there. MSVC1400 under /O2 turns division by any literal + constant into a reciprocal multiply regardless of whether the remainder is + also needed, so no arrangement of `/ 40` and `% 40` will produce an `idiv`. + For the original to have one, its divisor cannot have been a literal in the + source - something that reads 40 from memory, or a named value the compiler + could not fold. That is the thing to find; it is not a statement-order or + local-naming problem. */ int iVar5 = iVar2 % 40; if (iVar5 <= 0) { From e9b754da6b1182a8ffa5979c062c7b6438dfe58e Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:38:23 +0200 Subject: [PATCH 093/202] status: sync the eight TribesState percentages changed in this pass playUnitSelectionSound 50.4 -> 62.1, updateTribeCombatStanceBehavior 57.6 -> 75.1, trySpawnAdditionalWildlifeForTribe 87.2 -> 100, updateRabbitTribeBehavior 60.3 -> 63.0, computeTribePercentages 42.8 -> 43.4, predictUnitInterceptPosition 31.1 -> 31.7, playUnitCombatSpeechForTarget 29.6 -> 30.9, sortTribePathDestinationsByCost 28.8 -> 29.5. Namespace average 83.26 -> 83.68 over 113 functions, 58 of them at a full match. verify.py reports 0 mismatches between the file and the current reccmp run. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 282e202f22..eec3805f14 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46389,13 +46389,13 @@ SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521280 | 33.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521500 | 31.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521500 | 31.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending SHC_3BB0A8C1_0x00521720 | 71.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 50.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005217E0 | 62.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 29.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 30.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending @@ -46895,7 +46895,7 @@ SHC_3BB0A8C1_0x005244D0 | 44.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524890 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524930 | 28.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524930 | 29.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x005249C0 | 0.0% | Pending @@ -46953,7 +46953,7 @@ SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525F20 | 73.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005260B0 | 87.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005260B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005261B0 | 100.0% | Reimplemented @@ -46969,7 +46969,7 @@ SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending SHC_3BB0A8C1_0x00526F00 | 42.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005270F0 | 42.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005270F0 | 43.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 57.0% | Reimplemented, asm differs @@ -48879,7 +48879,7 @@ SHC_3BB0A8C1_0x0052A640 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052A700 | 45.9% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052A7D0 | 57.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A7D0 | 75.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052ACB4 | 0.0% | Pending @@ -49145,7 +49145,7 @@ SHC_3BB0A8C1_0x0052AD50 | 50.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B110 | 66.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B390 | 60.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B630 | 70.8% | Reimplemented, asm differs From 9a04d70cdf9eeca020d554a4e58c04aad2397ca2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:44:31 +0200 Subject: [PATCH 094/202] reimplement: importStoredInfoFromSlot0 94.5 -> 100%, processDeerMoving 50.7 -> 62.6% Both were arms the wrong way round, both found by diff_inv_jcc.py. importStoredInfoFromSlot0: the copy is the fall-through and the stance block goes last. The original jumps away when the flag is clear (`je 0xe9`) and the block at 0x522ed0 copies the stance and ends in its own `ret 8`. The note on that function was already right that the block reaches the field through an unfolded `[eax + ecx + 0x2e0]` rather than the folded base, and our build did produce that addressing - but written `if (!flag)` MSVC placed the two-instruction block inline at the top and sent the whole twenty-line copy into the far block instead. processDeerMoving: the spawn-succeeded arm comes first. The original jumps 0x7d forward to the did-not-spawn body (`je 0x7d`), so the arm that gives the move order is the fall-through and ends in its own `ret 4`. Written `if (!BVar7)` that whole arm - the rally-point call, standUpAllTribeUnits, giveTribeMoveInstruction, the currentRallyPointIndex bump and the rallyPointCount reset - landed in the far block. That makes the inverted-arm fix 5 for 5: +9.8 and +7.7 on updateTribeCombatStanceBehavior, +11.9 here, +5.5 on importStoredInfoFromSlot0, +0.7 on sortTribePathDestinationsByCost. It is the one technique in this namespace that has not yet cost anything, unlike base-caching and guard flattening. Two hits on that list were checked and left alone: snapshotSelectionTribeAndComputeStance, where the pair is a false positive from an eax/ebp renaming, and spawnQueuedReinforcementWaves' `xor bl, bl` against the original's `xor ebx, ebx`, where the note on the function already records widening that flag as 59.6 -> 58.5. Namespace average 83.68 -> 83.83, 59 of 113 at a full match. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/importStoredInfoFromSlot0.cpp | 54 ++--- .../Units/TribesState/processDeerMoving.cpp | 203 +++++++++--------- 2 files changed, 133 insertions(+), 124 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp index e53c0665ff..befc3a1e2c 100644 --- a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp +++ b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp @@ -9,36 +9,40 @@ namespace Map { void TribesState::importStoredInfoFromSlot0(undefined4 param_1, int tribeID) { /* - The block at 0x522ed0 copies the stance and ends in its own `ret 8`, and it - reaches the field through an unfolded `[eax + ecx + 0x2e0]` rather than the - folded base the rest of the function uses, which is what an early return out - of the top of the function compiles to. + /* + The copy is the fall-through and the stance block goes last. The original + jumps away when the flag is clear (`je 0xe9`); the block at 0x522ed0 copies + the stance and ends in its own `ret 8`, reaching the field through an + unfolded `[eax + ecx + 0x2e0]` rather than the folded base the rest of the + function uses. Written `if (!flag)` the addressing already came out + unfolded, but MSVC placed that block inline at the top and sent the whole + copy into the far block instead. */ - if (!this->tribeCopiedToSlot0) { + if (this->tribeCopiedToSlot0) { + this->tribes[tribeID].field166_0x2be = this->tribes[0].field166_0x2be; + this->tribes[tribeID].someUnitID = this->tribes[0].someUnitID; + this->tribes[tribeID].someUnitUID = this->tribes[0].someUnitUID; + this->tribes[tribeID].someTile = this->tribes[0].someTile; + this->tribes[tribeID].someTile2 = this->tribes[0].someTile2; + this->tribes[tribeID].currentRallyPointIndex = this->tribes[0].currentRallyPointIndex; + this->tribes[tribeID].rallyPointCount = this->tribes[0].rallyPointCount; + this->tribes[tribeID].isRallyingUnk = this->tribes[0].isRallyingUnk; this->tribes[tribeID].unitStance = this->tribes[0].unitStance; + /* + The original unrolls the inner loop over the ten rally points and keeps + the outer one over the two coordinates, stepping its cursor by one short + between the two passes. Ten is right: the copy covers 0x260 to 0x287, + which is where `rallyPointArray` ends. + */ + for (int _coordinate = 0; _coordinate < 2; _coordinate = _coordinate + 1) { + for (int _point = 0; _point < 10; _point = _point + 1) { + this->tribes[tribeID].rallyPointArray[_point][_coordinate] + = this->tribes[0].rallyPointArray[_point][_coordinate]; + } + } return; } - this->tribes[tribeID].field166_0x2be = this->tribes[0].field166_0x2be; - this->tribes[tribeID].someUnitID = this->tribes[0].someUnitID; - this->tribes[tribeID].someUnitUID = this->tribes[0].someUnitUID; - this->tribes[tribeID].someTile = this->tribes[0].someTile; - this->tribes[tribeID].someTile2 = this->tribes[0].someTile2; - this->tribes[tribeID].currentRallyPointIndex = this->tribes[0].currentRallyPointIndex; - this->tribes[tribeID].rallyPointCount = this->tribes[0].rallyPointCount; - this->tribes[tribeID].isRallyingUnk = this->tribes[0].isRallyingUnk; this->tribes[tribeID].unitStance = this->tribes[0].unitStance; - /* - The original unrolls the inner loop over the ten rally points and keeps - the outer one over the two coordinates, stepping its cursor by one short - between the two passes. Ten is right: the copy covers 0x260 to 0x287, - which is where `rallyPointArray` ends. - */ - for (int _coordinate = 0; _coordinate < 2; _coordinate = _coordinate + 1) { - for (int _point = 0; _point < 10; _point = _point + 1) { - this->tribes[tribeID].rallyPointArray[_point][_coordinate] - = this->tribes[0].rallyPointArray[_point][_coordinate]; - } - } } } diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index 397e3b418f..e5c6e5cf0f 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -74,105 +74,15 @@ namespace Map { } BVar7 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( tribeID, 100, Map::Units::UT_ANTELOPESHDEER); - if (!BVar7) { - if (this->tribes[tribeID].initialMoveOrderGiven == 0) { - /* - The original returns here - its block ends in `ret 4` after setting - the behaviour type - which is also why it can reuse the register that - held initialMoveOrderGiven for the unit ID. - */ - sVar2 = this->tribes[tribeID].selectionTargetUnitID; - this->tribes[tribeID].initialMoveOrderGiven = 1; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, - (uint)(DAT_UnitsState::instance.units[sVar2].x), - (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, 0, Map::Units::Instructions::UMSE_0); - this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; - return; - } - /* - The original returns from this one too: the block at 0x52af03 ends in - `ret 4` right after zeroing the counter. - */ - if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { - this->tribes[tribeID].unknownCounter01 = 0; - return; - } - sVar2 = this->tribes[tribeID].predatorNearby; - if (!sVar2) { - int SVar3 = this->tribes[tribeID].tribeBehaviorType; - if (SVar3 == (SomeTribeBehaviorType)0) { - if (iVar5) { - this->tribes[tribeID].wanderIntervalTicks = 200; - } - if (0x14 < iVar8) { - this->tribes[tribeID].wanderIntervalTicks = 200; - } - this->tribes[tribeID].unknownAttackRelatedUpdateCounter - = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; - if (this->tribes[tribeID].wanderIntervalTicks - <= this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - this->tribes[tribeID].wanderIntervalTicks - = (4 - ((byte)SEC_RNG::instance.currentNumber2 & 3)) * 200; - /* - Compared against the memory operand, not through a local: the - original has `cmp word ptr [..], 0; jg`. The block it falls - into at 0x52b064 ends in `ret 4`, so a tribe with no rally - points stops here instead of walking the array it has not got. - */ - if (this->tribes[tribeID].rallyPointCount <= 0) { - MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(tribeID, 5, 0); - return; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); - sVar2 = this->tribes[tribeID].currentRallyPointIndex; - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][0]), - (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0); - this->tribes[tribeID].currentRallyPointIndex - = this->tribes[tribeID].currentRallyPointIndex + 1; - if (this->tribes[tribeID].rallyPointCount <= this->tribes[tribeID].currentRallyPointIndex) { - this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; - this->tribes[tribeID].rallyPointCount = 0; - } - } - } else if (SVar3 == Map::Units::STBT_1) { - this->tribes[tribeID].unknownAttackRelatedUpdateCounter - = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; - if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 800) { - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); - } - } - } else { - if (sVar2 == 1) { - MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(tribeID, 5, 0); - if ((this->tribes[tribeID].rallyPointCount < 1) - && (MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(tribeID, 5, 1), - this->tribes[tribeID].rallyPointCount < 1)) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); - } else { - sVar2 = this->tribes[tribeID].selectionTargetUnitID; - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[sVar2].x, - (int)(DAT_UnitsState::instance.units[sVar2].y), DE::SHCDE::FX_DEER_RUN); - iVar8 = (int)this->tribes[tribeID].rallyPointCount; - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][0]), - (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][1]), 0, 0); - } - } - this->tribes[tribeID].predatorNearby = this->tribes[tribeID].predatorNearby + 1; - if (799 < this->tribes[tribeID].predatorNearby) { - this->tribes[tribeID].predatorNearby = 0; - } - } - } else { + /* + The spawn-succeeded arm comes first. The original tests the result and jumps + 0x7d forward to the did-not-spawn body (`je 0x7d`), so the arm that gives the + move order is the fall-through and ends in its own `ret 4`. Written + `if (!BVar7)` that whole arm - the rally-point call, standUpAllTribeUnits, + giveTribeMoveInstruction, the currentRallyPointIndex bump and the + rallyPointCount reset - lands in the far block and the test inverts to `jne`. + */ + if (BVar7) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 2, 0); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); @@ -186,6 +96,101 @@ namespace Map { this->tribes[tribeID].rallyPointCount = 0; this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; } + return; + } + if (this->tribes[tribeID].initialMoveOrderGiven == 0) { + /* + The original returns here - its block ends in `ret 4` after setting + the behaviour type - which is also why it can reuse the register that + held initialMoveOrderGiven for the unit ID. + */ + sVar2 = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].initialMoveOrderGiven = 1; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + (uint)(DAT_UnitsState::instance.units[sVar2].x), (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, + 0, Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + return; + } + /* + The original returns from this one too: the block at 0x52af03 ends in + `ret 4` right after zeroing the counter. + */ + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[tribeID].unknownCounter01 = 0; + return; + } + sVar2 = this->tribes[tribeID].predatorNearby; + if (!sVar2) { + int SVar3 = this->tribes[tribeID].tribeBehaviorType; + if (SVar3 == (SomeTribeBehaviorType)0) { + if (iVar5) { + this->tribes[tribeID].wanderIntervalTicks = 200; + } + if (0x14 < iVar8) { + this->tribes[tribeID].wanderIntervalTicks = 200; + } + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if (this->tribes[tribeID].wanderIntervalTicks + <= this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].wanderIntervalTicks + = (4 - ((byte)SEC_RNG::instance.currentNumber2 & 3)) * 200; + /* + Compared against the memory operand, not through a local: the + original has `cmp word ptr [..], 0; jg`. The block it falls + into at 0x52b064 ends in `ret 4`, so a tribe with no rally + points stops here instead of walking the array it has not got. + */ + if (this->tribes[tribeID].rallyPointCount <= 0) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 0); + return; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + sVar2 = this->tribes[tribeID].currentRallyPointIndex; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, + this)(tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][0]), + (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0); + this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; + if (this->tribes[tribeID].rallyPointCount <= this->tribes[tribeID].currentRallyPointIndex) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].rallyPointCount = 0; + } + } + } else if (SVar3 == Map::Units::STBT_1) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 800) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + } + } + } else { + if (sVar2 == 1) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 0); + if ((this->tribes[tribeID].rallyPointCount < 1) + && (MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 1), + this->tribes[tribeID].rallyPointCount < 1)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); + } else { + sVar2 = this->tribes[tribeID].selectionTargetUnitID; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[sVar2].x, + (int)(DAT_UnitsState::instance.units[sVar2].y), DE::SHCDE::FX_DEER_RUN); + iVar8 = (int)this->tribes[tribeID].rallyPointCount; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, + this)(tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][0]), + (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][1]), 0, 0); + } + } + this->tribes[tribeID].predatorNearby = this->tribes[tribeID].predatorNearby + 1; + if (799 < this->tribes[tribeID].predatorNearby) { + this->tribes[tribeID].predatorNearby = 0; + } } } From b529b331997c7dfabf650199fb0c16f9f21b1596 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:50:25 +0200 Subject: [PATCH 095/202] reimplement: getSpecificUnitFromTribe 53.5 -> 55.1%, the wrap arm is the fall-through The original stores the incremented bit index, tests it and jumps 0x1d forward when it has not reached sixteen (`cmp edx, 0x10; mov [..], edx; jne 0x1d`), so the arm that advances the tribe id and resets the index is what follows the test, not the one that returns with the index unchanged. Two other hits from the same diff_inv_jcc pass measured flat and are reverted, with the numbers recorded on the functions so they do not get retried: spawnDeerLionOrRabbit's spawn-failed return (73.0 -> 73.0) and getTribeAliveStatus's duplicated `return 100` (67.2 -> 67.2). In both the original clearly emits its own inline epilogue where ours jumps to the shared one, and writing the flat early return changed nothing either way - so the inline epilogue is MSVC's own tail duplication rather than something the source shape decides. That is now six measurements of guard flattening: +12.8, +0.6, two flat, -1.4, -2.0. Also checked and left alone: moveUnitToBehaviorTarget, whose arm order is already right - the original tests 0x3f2 at 0x5212fe, 0x3f5 at 0x521321 and 0x3fb at 0x52134a, the same order as the source - so its hit was a false pair and its gap is in the spills around `[esp+0x10]` and `[esp+0x14]`. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/getSpecificUnitFromTribe.cpp | 15 +++++++++++---- .../Map/Units/TribesState/getTribeAliveStatus.cpp | 7 +++++++ .../Units/TribesState/spawnDeerLionOrRabbit.cpp | 8 ++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp index f55f06d3fd..7af8009c80 100644 --- a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp @@ -34,12 +34,19 @@ namespace Map { do { if ((int)_bits & 1 << iVar2) { DAT_00ee0fb4::instance = iVar2 + 1; - if (iVar2 + 1 != 0x10) { - DAT_Tribe_HighestID::instance = iVar1; + /* + The wrap arm is the fall-through. The original stores the + incremented bit index, tests it and jumps 0x1d forward + when it has not reached sixteen (`cmp edx, 0x10; mov + [..], edx; jne 0x1d`), so the arm that advances the tribe + id and resets the bit index is what follows the test. + */ + if (iVar2 + 1 == 0x10) { + DAT_Tribe_HighestID::instance = iVar1 + 1; + DAT_00ee0fb4::instance = 0; return iVar1 * 0x10 + iVar2; } - DAT_Tribe_HighestID::instance = iVar1 + 1; - DAT_00ee0fb4::instance = 0; + DAT_Tribe_HighestID::instance = iVar1; return iVar1 * 0x10 + iVar2; } iVar2 = iVar2 + 1; diff --git a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp index 75dde08432..f9ec9c4373 100644 --- a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp @@ -37,6 +37,13 @@ namespace Map { } } } while (_index < _size); + /* + The nesting stays. The original returns 100 from here as well as from the + empty-tribe path, jumping 0xf forward when the tally is non-zero + (`test ecx, ecx; jne 0xf`) over its own `mov eax, 0x64` epilogue, so that + return is duplicated rather than shared. Writing it as an early + `if (!_alive) return 100;` measures 67.2% -> 67.2%. + */ if (_alive != 0) { if (_alive <= _other) { return (int)(50); diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index 474251a98f..47ba68393e 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -86,6 +86,14 @@ namespace Map { if (iVar3 <= (int)(_rng2 & 0x7f)) { _rng2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( 0, 0, (int)(_unitX * 8), (int)(_unitY * 8), (int)(_terrainHeight), unitType); + /* + The `if (_rng2)` stays. The original returns FALSE through an + epilogue of its own right here - `test esi, esi; jne 0xc` over + `pop/pop/pop/xor eax,eax/pop/add esp,0xc/ret 0xc` - where ours jumps + to the shared one at the end, so a flat `if (!_rng2) return FALSE;` + looks like the fix. Measured 73.0% -> 73.0%, raw and normalized both + unchanged. + */ if (_rng2) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(_rng2, tribeID); DAT_UnitsState::instance.units[_rng2].substate From 62ccfa0c57abc9b137048e158a50e392b29ca870 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:54:58 +0200 Subject: [PATCH 096/202] reimplement: drawFlagsAndUnitDestinations 47.0 -> 54.7%, computeTribePercentages 43.4 -> 45.3% Two more arms the wrong way round, both from diff_inv_jcc.py. drawFlagsAndUnitDestinations: the speed-matching arm is the fall-through. The original reads the field, tests it sixteen bits wide and jumps 0x91 forward when it is zero (`test dx, dx; je 0x91`), so the arm that draws the speed cursor follows the test and the planned-destination arm goes in the far block. computeTribePercentages: the dying tally is the fall-through. The original jumps 0xa forward when the flag is clear - exactly the `add dword ptr [esp + 0x14], 1` and the `jmp` that follow it - so the one-line increment sits under the test and the thirty lines of tallying go far. The import had the thirty lines first. That is 7 for 7 on this technique, and the only one in this namespace that has not yet cost a point. The pattern to look for is the original jumping a short distance where ours jumps a long one, with the short jump's displacement equal to the size of the arm the original is skipping. spawnUnitsForAITribe was checked and left alone: its top matches instruction for instruction and the hit is displacements only (`jle 0x9a` against `jle 0x9f`), so our body is five bytes larger somewhere below rather than mis-ordered. Namespace average 83.85 -> 83.93. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/computeTribePercentages.cpp | 13 +++-- .../drawFlagsAndUnitDestinations.cpp | 49 +++++++++++-------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index a730b46d7a..18a07ff99f 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -64,7 +64,16 @@ namespace Map { BVar3 = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::isComputerManagedNonPeasant, DAT_UnitsState::ptr)(_unitID); if (!BVar3) { - if (DAT_UnitsState::instance.units[_unitID].dying == 0) { + /* + The dying tally is the fall-through. The original jumps 0xa + forward when the flag is clear (`je 0xa`) - exactly the + `add dword ptr [esp + 0x14], 1` and `jmp` that follow - so + the one-line increment sits under the test and the thirty + lines of tallying go in the far block. + */ + if (DAT_UnitsState::instance.units[_unitID].dying != 0) { + _countDying = _countDying + 1; + } else { if (DAT_UnitsState::instance.units[_unitID].movementRelated != 8) { _countMoving = _countMoving + 1; } @@ -95,8 +104,6 @@ namespace Map { if (this->tribes[tribeID].movementSpeed < _movementSpeed) { this->tribes[tribeID].movementSpeed = _movementSpeed; } - } else { - _countDying = _countDying + 1; } } else { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, this)( diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index 59cfada1af..01ea6cadb8 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -82,27 +82,14 @@ namespace Map { && DAT_UnitsState::instance.units[_unitID].owner == DAT_GameSynchronyState::instance.currentPlayerSlotID) { short sVar1 = DAT_UnitsState::instance.units[_unitID].unitSpeedMatchingRelatedUnk; - if (!sVar1) { - if (DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan - < DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan - && (UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType, - UVar2 != Map::Units::UIT_UNIT_ATTACK_UNIT) - && UVar2 != Map::Units::UIT_ATTACK_LAND - && UVar2 != Map::Units::UIT_MAN_SIEGE_EQUIPMENT) { - iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; - if (7 < iVar4) { - iVar4 = 0xf - iVar4; - } - MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, - DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, - (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX - + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID] - .plannedDestinationY] - .addXgetTile, - 2); - } - } else { + /* + The speed-matching arm is the fall-through. The original reads + the field and tests it sixteen bits wide, then jumps 0x91 forward + when it is zero (`test dx, dx; je 0x91`), so the arm that draws + the speed cursor follows the test and the planned-destination arm + goes in the far block. + */ + if (sVar1) { if (sVar1 < 8) { iVar4 = (8 - sVar1) * 4; } else { @@ -122,6 +109,26 @@ namespace Map { MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, tile, (int)(DAT_UnitsState::instance.units[_unitID].unitSpeedMatchingRelatedUnk << 0x11 | 2)); + } else { + if (DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan + < DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan + && (UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType, + UVar2 != Map::Units::UIT_UNIT_ATTACK_UNIT) + && UVar2 != Map::Units::UIT_ATTACK_LAND + && UVar2 != Map::Units::UIT_MAN_SIEGE_EQUIPMENT) { + iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; + if (7 < iVar4) { + iVar4 = 0xf - iVar4; + } + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, + DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, + (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[_unitID] + .plannedDestinationY] + .addXgetTile, + 2); + } } UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType; if ((UVar2 == Map::Units::UIT_ATTACK_LAND) || (UVar2 == Map::Units::UIT_THROW_COW)) { From 2752fcdd6474bdd53de2f42a6196c16a42b3f9c8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 00:59:54 +0200 Subject: [PATCH 097/202] reimplement: updateTribeUnitAssignments 57.0 -> 75.0%, updateLionWolfTribeBehavior 66.8 -> 71.6% updateTribeUnitAssignments: the unit sweep is the fall-through. The original compares the size against the zero it is already holding in bx and jumps 0x7d forward when it is not positive (`cmp cx, bx; jle 0x7d`), so the sweep follows the test and the four-line time check goes far. The import had them the other way round, which also explains why the tribeState test above it jumped 0xbb in the original against our 0x31 - the body it skips had moved. The `<= 0` sense itself is right and stays; the note on that function already records what making it a zero-only test costs. updateLionWolfTribeBehavior: the non-zero side of the countdown is the fall-through. The original jumps 0x25 forward when the countdown is zero (`cmp ax, dx; je 0x25`) - exactly the span of the setStateForAllTribeUnits arm - then tests `cmp ax, 0x64` and leaves when it is not 100. Written `if (!countdown) ... else if (countdown == 100)` the ninety lines of wander and attack handling sat under the test and the three-line arm went far. Nine for nine on inverted arms now, and the two largest single gains in this pass (+18.0 and the +11.9 on processDeerMoving) both came from it. Namespace average 83.93 -> 84.14. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../updateLionWolfTribeBehavior.cpp | 17 +++++++++++++---- .../updateTribeUnitAssignments.cpp | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index f2c00de9fd..f841e2aae4 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -40,7 +40,19 @@ namespace Map { this->tribes[param_1].unknownCounter01 = 4000; } sVar2 = this->tribes[param_1].countdown; - if (!sVar2) { + /* + The non-zero side is the fall-through. The original reads the countdown and + jumps 0x25 forward when it is zero (`cmp ax, dx; je 0x25`) - exactly the span + of the setStateForAllTribeUnits arm - then tests `cmp ax, 0x64` and leaves + when it is not 100. So the 100 arm sits under the test and the ninety lines + of wander and attack handling go in the far block. + */ + if (sVar2) { + if (sVar2 == 100) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setStateForAllTribeUnits, this)(param_1, 0xd0); + this->tribes[param_1].unknownBool01 = 1; + } + } else { if ((BVar5) && (BVar6 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( param_1, 500, Map::Units::UT_LIONSHWOLF), @@ -133,9 +145,6 @@ namespace Map { this->tribes[param_1].tribeBehaviorType = ((SomeTribeBehaviorType)0); this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; } - } else if (sVar2 == 100) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setStateForAllTribeUnits, this)(param_1, 0xd0); - this->tribes[param_1].unknownBool01 = 1; } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp index 11e765509b..72bfdf0347 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp @@ -44,12 +44,14 @@ namespace Map { int _tribeID = DAT_CurrentTribeID::instance; if (_state != 3) { short const _size = this->tribes[DAT_CurrentTribeID::instance].size; - if (_size <= 0) { - short const _tribeTime = this->tribes[DAT_CurrentTribeID::instance].time; - if (_tribeTime && (int)DAT_GameCore::instance.mapTimeInTicks <= (int)_tribeTime - && _tribeTime) - continue; - } else { + /* + The unit sweep is the fall-through. The original compares the + size against the zero it is holding in bx and jumps 0x7d forward + when it is not positive (`cmp cx, bx; jle 0x7d`), so the sweep + follows the test and the four-line time check goes in the far + block. The `<= 0` sense itself is right and stays - see above. + */ + if (0 < _size) { int const _tribeSize = (int)_size; int local_4 = 0; if (0 < _tribeSize) { @@ -76,6 +78,11 @@ namespace Map { continue; } } + } else { + short const _tribeTime = this->tribes[DAT_CurrentTribeID::instance].time; + if (_tribeTime && (int)DAT_GameCore::instance.mapTimeInTicks <= (int)_tribeTime + && _tribeTime) + continue; } } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::clearTribe, this)(_tribeID); From 4a49c5b3bb1a1fc573fdd36e95518a9bc5cef868 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:04:46 +0200 Subject: [PATCH 098/202] reimplement: giveTribeAnInstruction 56.8 -> 56.9%, and the first inverted-arm loss In giveTribeAnInstruction the all-assassins store is the fall-through: the original jumps 0xc forward when isTribeAllAssassins returns zero (`test eax, eax; je 0xc`), so the +136 store follows the test and the +128 store goes far. Same displacement either way, just the two arms the other way round. Worth +0.1 - that function's gap is still overwhelmingly cold-block placement, not branch sense. giveTribeMoveInstruction has the same shape at its isTribeAllAssassins call, and there the fix loses: the original's fall-through is the one-line `bVar4 = 1` with the thirty lines of cross-area path handling placed far, and swapping to match measures 30.0% -> 29.6% raw while normalized rises 22.5% -> 23.4%. So the instruction sequence does move closer and the branch displacements move further; raw is what the status file tracks, so it is reverted with both numbers recorded on the function. That ends the inverted-arm run at nine wins and one loss, and the loss is in one of the two functions whose own notes already say the frame allocation has to be understood before anything else there will move. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/giveTribeAnInstruction.cpp | 15 ++++++++++++--- .../TribesState/giveTribeMoveInstruction.cpp | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index d27a619fa8..afb25f012e 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -461,10 +461,19 @@ namespace Map { = Map::Units::States::US_MOVE_TO_DESTINATION; BVar16 = MACRO_CALL_MEMBER( Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); - if (!BVar16) { - DAT_PathFindingState::instance.notAllAssassinsUnk = 1; - } else { + /* + The all-assassins store is the fall-through. + The original jumps 0xc forward when the + result is zero (`test eax, eax; je 0xc`), so + the +136 store follows the test and the +128 + store goes in the far block - the same + displacement either way, just the two arms + the other way round. + */ + if (BVar16) { DAT_PathFindingState::instance.allAssassinsUnk = 1; + } else { + DAT_PathFindingState::instance.notAllAssassinsUnk = 1; } MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(iVar11, uVar13, (uint)(sVar9), 0); diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 317fa775ab..d1c66b4d89 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -144,6 +144,18 @@ namespace Map { if (0 < _area) { int _firstUnit = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + /* + The nesting here stays, against the asm. The original tests the + all-assassins result and jumps 0x38 forward when it is zero + (`test eax, eax; je 0x38`), so the one-line `bVar4 = 1` is its + fall-through and the thirty lines of cross-area path handling go + far - ours is the other way round. Swapping the arms measures + 30.0% -> 29.6% raw, although normalized rises 22.5% -> 23.4%: the + instruction sequence does get closer and the branch displacements + get worse. Everywhere else in this namespace fixing an inverted arm + gained raw, nine times out of nine; this is the one that did not, so + it is left as it was and the measurement recorded. + */ if (!_firstUnit) { if (_area != y1) { _someUnitCanClimb = MACRO_CALL_MEMBER( From 3633fc96fb2433831efc1f31677e3bb0264f85c0 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:09:22 +0200 Subject: [PATCH 099/202] measure: spawnQueuedReinforcementWaves' difficulty chain is not a switch The original dispatches the difficulty by subtracting through the values - `test ecx, ecx; je`, `sub ecx, 2; je`, `sub ecx, 1; jne` - which is MSVC's shape for a switch over a few close-together cases, where our equality chain emits a `cmp` per arm. Rewriting those three arms as `switch (difficulty)` measures 59.6% -> 55.9%, so whatever the sub-chain buys is outweighed by the arm placement it changes. Reverted with the number recorded on the function. status: synced the eight percentages this pass changed - updateTribeUnitAssignments 57.0 -> 75.0, updateLionWolfTribeBehavior 66.8 -> 71.6, processDeerMoving 50.7 -> 62.6, drawFlagsAndUnitDestinations 47.0 -> 54.7, getSpecificUnitFromTribe 53.5 -> 55.1, computeTribePercentages 43.4 -> 45.3, importStoredInfoFromSlot0 94.5 -> 100, giveTribeAnInstruction 56.8 -> 56.9. Namespace average 83.26 -> 84.14 across this session, 59 of 113 at a full match. verify.py reports 0 mismatches against the current reccmp run. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../spawnQueuedReinforcementWaves.cpp | 10 ++++++++++ status/addresses-SHC-3BB0A8C1.txt | 16 ++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp index a2a7d84381..76edca258a 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -74,6 +74,16 @@ namespace Map { pSVar8 = &DAT_GameState::instance.mapAndTime.siegeInformation; do { iVar3 = 100; + /* + The difficulty chain stays an if/else-if, against the asm. The + original dispatches it by subtracting through the values - + `test ecx, ecx; je`, `sub ecx, 2; je`, `sub ecx, 1; jne` - which + is MSVC's shape for a switch over a few close-together cases, + where an equality chain gives a `cmp` per arm. Rewriting these + three arms as `switch (difficulty)` measures 59.6% -> 55.9%, so + whatever the sub-chain buys is less than the arm placement it + changes. Reverted. + */ if (!DAT_GameState::instance.mapAndTime.difficulty) { if (_playerID_2 == 1) { iVar3 = 0x43; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index eec3805f14..aeac0ee738 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46783,7 +46783,7 @@ SHC_3BB0A8C1_0x00522360 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522390 | 58.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522410 | 53.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522410 | 55.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x005224E0 | 100.0% | Reimplemented @@ -46817,7 +46817,7 @@ SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522CA0 | 85.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522DD0 | 94.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522EF0 | 65.9% | Reimplemented, asm differs @@ -46861,7 +46861,7 @@ SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523920 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523A30 | 47.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523A30 | 54.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523B00 | 0.0% | Pending @@ -46969,9 +46969,9 @@ SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending SHC_3BB0A8C1_0x00526F00 | 42.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005270F0 | 43.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005270F0 | 45.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527330 | 57.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527440 | 59.6% | Reimplemented, asm differs @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 56.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 56.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending @@ -49141,9 +49141,9 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 50.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052AD50 | 62.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 66.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B110 | 71.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs From a1bbf42584f8717e56985c3be25bdfe01881c287 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:14:41 +0200 Subject: [PATCH 100/202] reimplement: updateAnimalHerdBehaviorState 80.4 -> 100%, snapshot... 85.7 -> 95.8% updateAnimalHerdBehaviorState: the bound is tested against 1, not 2. Written `< 2` the literal never reaches the compare - MSVC puts 2 in a register and emits `cmp ecx, ebx; jge` - where the original has `cmp ecx, 1; jg`. `<= 1` is the same test on ints and gives that instruction. The note on the function already said the original compares against the literal; it just had the wrong literal written in the code. Full match now. snapshotSelectionTribeAndComputeStance, two changes: - the stand-ground store goes last. By address the original's three exits are the aggressive store, the computed one, then `mov word ptr [ebx + 0x2e0], 0` with an epilogue of its own, so the empty-tally arm is the far block rather than the first guard. diff_triage.py flagged this as a RET-count mismatch. - the computed store is `cmp ecx, esi; setge al`, which is `_stanceCount[1] >= _stanceCount[0]`. Writing it as `[0] <= [1]` is the same answer from `cmp esi, ecx; setle al`, with the operands the other way round. The second one is a pattern worth looking for on its own: a comparison that is arithmetically identical but written with its operands swapped costs the `cmp` and the `setcc`, and neither diff_jcc nor diff_inv_jcc reports it because the branch sense matches. Namespace average 84.14 -> 84.40, 60 of 113 at a full match. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...snapshotSelectionTribeAndComputeStance.cpp | 23 +++++++++++++------ .../updateAnimalHerdBehaviorState.cpp | 14 +++++++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index 06a4653fec..d90b7bd202 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -68,15 +68,24 @@ namespace Map { [esp+0x10] into registers at 0x522d6a and nowhere else, and diff_slots.py shows our extra fourth slot is a spill of a redundant copy. */ - if (_stanceCount[2] + _stanceCount[1] + _stanceCount[0] == 0) { - this->tribes[0].unitStance = Map::Units::Behavior::USE_STAND_GROUND; - return; - } - if (_stanceCount[2] < _stanceCount[1] || _stanceCount[2] < _stanceCount[0]) { - this->tribes[0].unitStance = (ushort)(_stanceCount[0] <= _stanceCount[1]); + /* + The stand-ground store goes last, and the two counts are compared the other + way round. By address the original's three exits are the aggressive store, + then the computed one, then `mov word ptr [ebx + 0x2e0], 0` with an epilogue + of its own - so the empty-tally arm is the far block, not the first guard. + The computed one is `cmp ecx, esi; setge al`, which is + `_stanceCount[1] >= _stanceCount[0]`; writing it as `[0] <= [1]` gives the + same answer from `cmp esi, ecx; setle al`, with the operands swapped. + */ + if (_stanceCount[2] + _stanceCount[1] + _stanceCount[0] != 0) { + if (_stanceCount[2] < _stanceCount[1] || _stanceCount[2] < _stanceCount[0]) { + this->tribes[0].unitStance = (ushort)(_stanceCount[1] >= _stanceCount[0]); + return; + } + this->tribes[0].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; return; } - this->tribes[0].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + this->tribes[0].unitStance = Map::Units::Behavior::USE_STAND_GROUND; } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp b/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp index 5a4ea5d1d3..a69d53aafb 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp @@ -26,12 +26,18 @@ namespace Map { && DAT_UnitsState::instance.units[iVar1].dying == 0 && DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue == 2) { /* - Signed: the original compares against the literal with - `cmp ecx, 1; jg` and derives the second value with `setg`, where a - `uint` gives `jae` and `seta` against a register holding 1. + Signed, and tested against 1 rather than 2. The original has + `cmp ecx, 1; jg` and derives the second value with `setg`, where + a `uint` gives `jae` and `seta`. Written `< 2` the literal does + not appear in the compare at all - MSVC puts 2 in a register and + emits `cmp ecx, ebx; jge` - so the bound is written as `<= 1`, + which is the same test on ints and the instruction the original + has. The arms are already the right way round: the original's + `jg` skips the nine bytes of the zero store, so that store is + its fall-through. */ int uVar2 = DAT_UnitsState::instance.units[iVar1].fixedRng & 7; - if (uVar2 < 2) { + if (uVar2 <= 1) { DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue = 0; } else { DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue = (5 < uVar2) + 1; From 33d8426b53ebe17b43a53414d920dc41cbe975ff Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:20:28 +0200 Subject: [PATCH 101/202] cleanup: playUnitCombatSpeechForTarget's out-param is an int count, not a TribesState* The majority lookup writes a selected-unit count back through its second argument. The decompiler typed that local `TribesState*` because the import assigned `this` to it before the call, so every use of it read `_ptrCurrentSelectionID == (TribesState*)0x1` and `(int)_ptrCurrentSelectionID < 5`. The original never does that assignment: it passes the slot's address with `lea eax, [esp + 0x10]; push eax` and stores nothing into it first. The assignment is what made us spill `this` with `mov dword ptr [esp + 0x14], edi` where the original keeps it in ebx for the whole function. Typed as `int` and renamed, which drops twelve pointer casts. Worth +0.1 (30.9 -> 31.0) - that function's gap is elsewhere - but the pointer type was wrong and the store was not in the original. Checked for the same shape across the namespace: this was the only local assigned `this`. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../playUnitCombatSpeechForTarget.cpp | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index 2b4d05b48e..dfeba8915e 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -29,16 +29,23 @@ namespace Map { int _xDifference; int _otherY; int sfxOffsetInArray; - TribesState* _ptrCurrentSelectionID; + /* + This is the selected-unit count the majority lookup writes back, not a + pointer. The decompiler typed it `TribesState*` because the import assigned + `this` to it before the call - which the original never does: it passes the + slot's address with `lea eax, [esp + 0x10]; push eax` and stores nothing into + it first. That assignment is what made us spill `this` + (`mov dword ptr [esp + 0x14], edi`) where the original just keeps it in ebx. + */ + int _selectedCount; uint _actionID; int _distance; - TribesState* _ptrCurrentSelectionID_2; - _ptrCurrentSelectionID = this; + int _selectedCount_2; _otherUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); - _ptrptrCurrentSelectionID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, - this)(selectionID, (int*)&_ptrCurrentSelectionID); - _ptrCurrentSelectionID_2 = _ptrCurrentSelectionID; + _ptrptrCurrentSelectionID = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(selectionID, &_selectedCount); + _selectedCount_2 = _selectedCount; /* An `int`, not a `short`. The original reads the size once, `movsx ebp, word ptr [edi + 0x1667fd4]` at 0x521abc, and keeps the @@ -126,11 +133,11 @@ namespace Map { if (_ptrptrCurrentSelectionID != Map::Units::UT_A_HARCHER) { return; } - if (_ptrCurrentSelectionID == (TribesState*)0x1) { + if (_selectedCount == 1) { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; sfxOffsetInArray = 0xea; - } else if ((int)_ptrCurrentSelectionID < 5) { + } else if (_selectedCount < 5) { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; sfxOffsetInArray = 0xeb; @@ -151,20 +158,19 @@ namespace Map { (int)DAT_UnitsState::instance.units[_otherUnitID].y, 0x59); return; case Map::Units::UT_E_KNIGHT: - if (_ptrCurrentSelectionID == (TribesState*)0x1) { + if (_selectedCount == 1) { _otherX = 0x56; LAB_00521c6f: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); } else { - if ((_ptrCurrentSelectionID == (TribesState*)0x2) - || (_ptrCurrentSelectionID == (TribesState*)0x3)) { + if ((_selectedCount == 2) || (_selectedCount == 3)) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x57); } - if (3 < (int)_ptrCurrentSelectionID_2) { + if (3 < _selectedCount_2) { _otherX = 0x58; goto LAB_00521c6f; } @@ -198,12 +204,12 @@ namespace Map { (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x11); return; case Map::Units::UT_E_SWORD: - if (_ptrCurrentSelectionID == (TribesState*)0x1) { + if (_selectedCount == 1) { _otherX = 0xad; - } else if (_ptrCurrentSelectionID == (TribesState*)0x2) { + } else if (_selectedCount == 2) { _otherX = 0xae; } else { - if ((int)_ptrCurrentSelectionID < 3) + if (_selectedCount < 3) goto LAB_00521d1b; _otherX = 0xaf; } From 299bba8466cbcc136bf6e49297c8c768133c8987 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:29:56 +0200 Subject: [PATCH 102/202] reimplement: giveTribeAnInstruction 56.9 -> 57.4%, second switch's arm order Its thirteen switch dispatches are at 0x52a1e8, 0x52a258, 0x52a2b8, 0x52a308, 0x52a368, 0x52a3b8, 0x52a3fc, 0x52a464, 0x52a490, 0x52a4e4, 0x52a550, 0x52a5a8 and 0x52a5e4 - found by searching the function for the indirect `jmp dword ptr [reg*4 + disp]`, which is worth doing once rather than hunting them one at a time. The second one, 0x52a258 (five dwords, 0x49-entry byte table at 0x52a26c, indexed by `unitType - 5`), runs by address as 0x527eac (mangonel, ballista, fire ballista), 0x527efd (the six ranged types), 0x527f4b (the eleven melee types), 0x527f6d (catapult, trebuchet). The import had melee, ranged, catapult, mangonel - so the first arm was last and the last was third. LAB_00527ee1 lives inside the mangonel arm's tail, which is why the ranged arm's `goto` is a backward jump in the original (0x527efd to 0x527ee1). Moving that arm to the front needs an explicit `break` after the label block, matching the original's `jmp 0x527fab` out of it, or the melee labels below would be fallen into. Also audited the label sets rather than only the order, since a wrong set is a behaviour bug - that is how the catapult/mangonel mix-up in playUnitSelectionSound turned up. Switch 2's four sets and switch 3's single arm (0x52a2b8, base 5, range 0x46: one real arm holding exactly the eleven melee types, everything else default) both match the import exactly, so its labels in this function look reliable and the remaining ten were left alone. status synced; namespace average 84.40, 60 of 113 at a full match, 0 mismatches. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 93 +++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 8 +- 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index afb25f012e..398a70a704 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -287,26 +287,41 @@ namespace Map { continue; DAT_UnitsState::instance.units[_unitID_0x04]._someX_2 = 0; DAT_UnitsState::instance.units[_unitID_0x04]._someY_2 = 0; + /* + Arm order from the dispatch at 0x52a258: five dword entries and a + 0x49-entry byte index table at 0x52a26c, indexed by `unitType - + 5`. By address the arms run 0x527eac (mangonel, ballista, + fire ballista), 0x527efd (the six ranged types), 0x527f4b (the + eleven melee types) and 0x527f6d (catapult, trebuchet), so the + siege pair the import had third is really last and the mangonel + group it had last is first. + + LAB_00527ee1 sits inside the mangonel arm's tail, which is why + the ranged arm's `goto` is a backward jump in the original + (0x527efd to 0x527ee1). With that arm moved to the front the + label block needs an explicit `break` - the original ends it with + `jmp 0x527fab`, the switch's exit - or the melee labels below + would be fallen into. + */ switch (DAT_UnitsState::instance.units[_unitID_0x04].unitType) { - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_KNIGHT: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - /* - melee units - */ - if (DAT_UnitsState::instance.units[iVar11].unitType != Map::Units::UT_ANTELOPESHDEER - && (param_5 != 1 - || (DAT_UnitsState::instance.units[iVar11].state.generic - != Map::Units::States::US_MELEE_ATTACK))) { - id = id + 1; + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + DAT_UnitsState::instance.units[_unitID_0x04].targetingType + = Map::Units::UIT_UNIT_ATTACK_UNIT; + DAT_UnitsState::instance.units[_unitID_0x04] + .targetedUnitID__OR__engineerMannedSiegeEngineRef = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_0x04] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + DAT_UnitsState::instance.units[_unitID_0x04].field283_0x3f8 = 0; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(_unitID_0x04); + DAT_UnitsState::instance.units[_unitID_0x04].shootBeforeStop = 10; + LAB_00527ee1: + if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { + this->tribes[tribeID].containsRabbit = 1; } break; case Map::Units::UT_E_ARCHER: @@ -337,6 +352,27 @@ namespace Map { Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(_unitID_0x04); goto LAB_00527ee1; + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + /* + melee units + */ + if (DAT_UnitsState::instance.units[iVar11].unitType != Map::Units::UT_ANTELOPESHDEER + && (param_5 != 1 + || (DAT_UnitsState::instance.units[iVar11].state.generic + != Map::Units::States::US_MELEE_ATTACK))) { + id = id + 1; + } + break; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TREBUCHET: DAT_UnitsState::instance.units[_unitID_0x04].field253_0x3c5 = 5; @@ -348,25 +384,6 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_0x04].unkAttackRelated = 0xb; DAT_UnitsState::instance.units[_unitID_0x04].shootBeforeStop = 10; break; - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_BALLISTA: - case Map::Units::UT_S_FBALLISTA: - DAT_UnitsState::instance.units[_unitID_0x04].targetingType - = Map::Units::UIT_UNIT_ATTACK_UNIT; - DAT_UnitsState::instance.units[_unitID_0x04] - .targetedUnitID__OR__engineerMannedSiegeEngineRef = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[_unitID_0x04] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = unitUID; - DAT_UnitsState::instance.units[_unitID_0x04].field283_0x3f8 = 0; - MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, - DAT_UnitsState::ptr)(_unitID_0x04); - DAT_UnitsState::instance.units[_unitID_0x04].shootBeforeStop = 10; - LAB_00527ee1: - if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { - this->tribes[tribeID].containsRabbit = 1; - } } } while (_unitTribeIndex < this->tribes[tribeID].size); /* diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index aeac0ee738..44d1ade81f 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 30.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 31.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending @@ -46815,7 +46815,7 @@ SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522CA0 | 85.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522CA0 | 95.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented @@ -46845,7 +46845,7 @@ SHC_3BB0A8C1_0x00523410 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523520 | 96.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523590 | 80.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523590 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523630 | 100.0% | Reimplemented @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 56.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 57.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From 70b8f84f5b905b343917cf3938dc7b00afacf291 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:35:13 +0200 Subject: [PATCH 103/202] reimplement: giveTribeAnInstruction 57.4 -> 57.5%, fourth switch's arm order Table 0x52a308: five dwords, 0x49-entry byte table at 0x52a31c, base 5. By address the arms are 0x5282fd (mangonel group), 0x528311 (ranged), 0x52838a (melee), 0x528398 (catapult pair), so the melee arm the import had first belongs third. The mangonel arm falls through into the ranged arm - the comment on it already said so - and that pair was already adjacent and in the right order, so only the melee arm moved. Three of the thirteen switches were checked and need nothing: - 0x52a2b8 (base 5, range 0x46): one real arm, the eleven melee types. - 0x52a368 (base 5, range 0x46): one real arm. No order to get wrong in either. - 0x52a3b8 (base 0x16, range 0x37): two arms, 0x5287cf (ten ranged and siege types) then 0x5287f6 (catapult, trebuchet) - already in that order, labels already right. Label sets match the import in every switch audited so far, so the catapult/mangonel class of mix-up found in playUnitSelectionSound does not recur here. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 398a70a704..eb64bc274d 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -536,22 +536,16 @@ namespace Map { && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0 && (UVar3 = DAT_UnitsState::instance.units[iVar12].state.generic, UVar3 != Map::Units::States::US_MELEE_ATTACK)) { + /* + Arm order from 0x52a308: five dwords, 0x49-entry byte table + at 0x52a31c, base 5. By address 0x5282fd (mangonel group), + 0x528311 (ranged), 0x52838a (melee), 0x528398 (catapult + pair) - so the melee arm the import had first belongs third. + The mangonel arm falls through into the ranged arm, so that + pair has to stay adjacent and in that order, which it + already was. + */ switch (DAT_UnitsState::instance.units[iVar12].unitType) { - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_KNIGHT: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { - id = id + 1; - } - break; case Map::Units::UT_S_MANGONEL: case Map::Units::UT_S_BALLISTA: case Map::Units::UT_S_FBALLISTA: @@ -594,6 +588,21 @@ namespace Map { Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(iVar12); break; + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_KNIGHT: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { + id = id + 1; + } + break; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TREBUCHET: if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { From 0a688635552c68cf883ed398e33e596f75943df7 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:40:17 +0200 Subject: [PATCH 104/202] reimplement: giveTribeAnInstruction 57.5 -> 57.8%, seventh switch's arm order Table 0x52a3fc: seven dwords, 0x49-entry byte table at 0x52a418, base 5. By address the arms are 0x528b7e (horse archer), 0x528b83 (the other five ranged types), 0x528c76 (the five siege types), 0x528ca2 (battering ram), 0x528ccd (knight) and 0x528ce4 (the ten melee types). The import had its two fall-through pairs the wrong way round - knight-into-melee first and horse-archer-into-ranged last - with the siege and ram arms between them, which were already in the right relative order. Both pairs stay adjacent; they just swap ends. LAB_00528c46 lives inside the horse archer arm's tail, which is why the siege arm's `goto` is backward in the original (0x528c76 reaching back to 0x528c46). Moving that arm to the front needs an explicit `break`, matching the `jmp 0x528cfb` the original ends it with. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 107 ++++++++++-------- 1 file changed, 61 insertions(+), 46 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index eb64bc274d..f770246c35 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -944,53 +944,21 @@ namespace Map { continue; DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; + /* + Arm order from 0x52a3fc: seven dwords, 0x49-entry byte table at + 0x52a418, base 5. By address the arms are 0x528b7e (horse + archer), 0x528b83 (the other five ranged types), 0x528c76 + (the five siege types), 0x528ca2 (battering ram), 0x528ccd + (knight) and 0x528ce4 (the ten melee types). The import had the + two fall-through pairs the wrong way round: knight-into-melee + first and horse-archer-into-ranged last. + + LAB_00528c46 is inside the horse archer arm's tail, which is why + the siege arm's `goto` is backward in the original (0x528c76 to + 0x528c46). With that arm moved to the front it needs an explicit + `break` - the original ends it `jmp 0x528cfb`, the switch's exit. + */ switch (DAT_UnitsState::instance.units[iVar12].unitType) { - case Map::Units::UT_E_KNIGHT: - if (DAT_BuildingsState::instance.buildings[iVar11].buildingType - != Map::Buildings::BT_PITCHDITCH) { - _ramOrHorse = _ramOrHorse + 1; - } - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_SWORDSMAN: - switchD_00528b77_caseD_5: - if (DAT_BuildingsState::instance.buildings[iVar11].buildingType - != Map::Buildings::BT_PITCHDITCH) { - id = id + 1; - } - break; - case Map::Units::UT_S_CATAPULT: - case Map::Units::UT_S_TREBUCHET: - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_BALLISTA: - case Map::Units::UT_S_FBALLISTA: - if (unitInstructionType == (UnitInstructionType)0x26 - || DAT_BuildingsState::instance.buildings[iVar11].buildingType - == Map::Buildings::BT_PITCHDITCH) - break; - DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID - = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar12] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = unitUID; - goto LAB_00528c46; - case Map::Units::UT_S_BATTERINGRAM: - if ((DAT_BuildingsState::instance.buildings[iVar11].buildingType - != Map::Buildings::BT_PITCHDITCH) - && (DAT_UnitsState::instance.units[iVar12] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) { - id = id + 1; - _ramOrHorse = _ramOrHorse + 1; - } - break; case Map::Units::UT_A_HARCHER: _ramOrHorse = _ramOrHorse + 1; /* @@ -1050,6 +1018,53 @@ namespace Map { DAT_UnitsState::ptr)(iVar12); DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } + break; + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TREBUCHET: + case Map::Units::UT_S_MANGONEL: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: + if (unitInstructionType == (UnitInstructionType)0x26 + || DAT_BuildingsState::instance.buildings[iVar11].buildingType + == Map::Buildings::BT_PITCHDITCH) + break; + DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID + = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[iVar12] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = unitUID; + goto LAB_00528c46; + case Map::Units::UT_S_BATTERINGRAM: + if ((DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) + && (DAT_UnitsState::instance.units[iVar12] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4)) { + id = id + 1; + _ramOrHorse = _ramOrHorse + 1; + } + break; + case Map::Units::UT_E_KNIGHT: + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) { + _ramOrHorse = _ramOrHorse + 1; + } + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_SWORDSMAN: + switchD_00528b77_caseD_5: + if (DAT_BuildingsState::instance.buildings[iVar11].buildingType + != Map::Buildings::BT_PITCHDITCH) { + id = id + 1; + } + break; } } while (_unitTribeIndex < this->tribes[tribeID].size); } From 3d97b935cb52c5d7f09b8e82db139b6f850e1b9a Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:54:35 +0200 Subject: [PATCH 105/202] fix: UT_TUNNELER was on the wrong switch arm in giveTribeAnInstruction The byte table at 0x52a560 maps index 0 - unit type 5, the tunneler - to the arm at 0x52933c, which is the melee arm; the ranged types 0x16, 0x17, 0x46, 0x48, 0x49, 0x4a and 0x4c map to 0x529331. The import had the tunneler label on the ranged arm, so a tunneler met that arm's `unitInstructionType != 0x23` test and broke out of the switch instead of running the melee body. The two arms are adjacent and fall through in the original too - 0x529331 to 0x52933c is eleven bytes, the `cmp` and the `jnz` - so the arms were already in the right order and only the label moved. 58.6 -> 58.9%. Also in this pass, both from their tables: - tenth switch, 0x52a4e4 (eight dwords, 0x49-entry byte table at 0x52a504, base 5): 58.6% from 57.8%. Ram, knight and melee are a three-way fall-through and horse archer falls into ranged; the original's spacing confirms both (0x5291d6 to 0x5291e5 is the ram body, 0x5291e5 to 0x5291ea the knight's single increment). Only which end those two chains sat at was wrong, so they swapped. - ninth switch, 0x52a490 (three dwords, base 5): the ranged arm is first at 0x528e41 with melee at 0x528e4c, eleven bytes later - the whole ranged arm being the 0x24 test and a jump into the melee body. Measured flat, kept anyway: the order is a fact from the table, and the `goto` now runs forward as it does in the original instead of reaching back past ninety lines. Switches 5, 6, 8 and 12 need nothing - 5, 8 and 12 have a single real arm each, and 6's two arms are already in address order with the right labels. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 124 ++++++++++++------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 82 insertions(+), 44 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index f770246c35..0edbc81ead 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -1101,7 +1101,25 @@ namespace Map { && DAT_UnitsState::instance.units[_unitID].dying == 0 && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0) { UVar4 = DAT_UnitsState::instance.units[_unitID].unitType; + /* + Arm order from 0x52a490: three dwords, 0x48-entry byte + table at 0x52a49c, base 5. The ranged arm is first, at + 0x528e41, and the melee arm second at 0x528e4c - eleven + bytes later, which is the whole of the ranged arm: it is + just the 0x24 test and a jump into the melee body. With + the import's order that `goto` was a backward jump and + the ranged arm sat past ninety lines of melee handling. + */ switch (UVar4) { + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_HARCHER: + case Map::Units::UT_A_FIRETHROWER: + if (unitInstructionType == (UnitInstructionType)0x24) + goto switchD_00528e3a_caseD_5; + break; case Map::Units::UT_TUNNELER: case Map::Units::UT_E_SPEAR: case Map::Units::UT_E_PIKE: @@ -1199,14 +1217,6 @@ namespace Map { } DAT_UnitsState::instance.units[_unitID].attackAtTileX = sVar9; break; - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_HARCHER: - case Map::Units::UT_A_FIRETHROWER: - if (unitInstructionType == (UnitInstructionType)0x24) - goto switchD_00528e3a_caseD_5; } } if (this->tribes[tribeID].size <= _unitTribeIndex) { @@ -1233,30 +1243,36 @@ namespace Map { continue; DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; + /* + Arm order from 0x52a4e4: eight dwords, 0x49-entry byte table at + 0x52a504, base 5. By address: 0x5290ce (horse archer), 0x5290d3 + (the other six ranged types), 0x5290e3 (the five siege types), + 0x52914f (tower), 0x5291d6 (battering ram), 0x5291e5 (knight), + 0x5291ea (the nine melee types). + + Ram, knight and melee are a three-way fall-through - the comment + below says so - and the original's spacing confirms it: 0x5291d6 + to 0x5291e5 is the ram body, 0x5291e5 to 0x5291ea is the knight's + single increment. Horse archer falls into ranged the same way. So + the only thing wrong was which end those two chains sat at: the + import had ram-knight-melee first and horse-archer-ranged last. + */ switch (DAT_UnitsState::instance.units[uVar13].unitType) { - case Map::Units::UT_S_BATTERINGRAM: - if (DAT_UnitsState::instance.units[uVar13] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - != 4) { - break; - } - local_8 = local_8 + 1; + case Map::Units::UT_A_HARCHER: + _ramOrHorse = _ramOrHorse + 1; /* - a crewed ram counts as a horseman too, which is the goto the - decompiler printed back into this arm + The ranged types share this arm; only the horse archer counts + towards _ramOrHorse. */ - case Map::Units::UT_E_KNIGHT: - _ramOrHorse = _ramOrHorse + 1; - case Map::Units::UT_TUNNELER: - case Map::Units::UT_E_SPEAR: - case Map::Units::UT_E_PIKE: - case Map::Units::UT_E_MACE: - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_MONK: - case Map::Units::UT_LORD: - case Map::Units::UT_A_SLAVE: - case Map::Units::UT_A_SWORDSMAN: - id = id + 1; + case Map::Units::UT_E_ARCHER: + case Map::Units::UT_E_XBOW: + case Map::Units::UT_A_ARCHER: + case Map::Units::UT_A_SLINGER: + case Map::Units::UT_A_ASSASSIN: + case Map::Units::UT_A_FIRETHROWER: + if (unitInstructionType == (UnitInstructionType)0x23) { + id = id + 1; + } break; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TREBUCHET: @@ -1309,21 +1325,30 @@ namespace Map { - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; } break; - case Map::Units::UT_A_HARCHER: - _ramOrHorse = _ramOrHorse + 1; + case Map::Units::UT_S_BATTERINGRAM: + if (DAT_UnitsState::instance.units[uVar13] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + != 4) { + break; + } + local_8 = local_8 + 1; /* - The ranged types share this arm; only the horse archer counts - towards _ramOrHorse. + a crewed ram counts as a horseman too, which is the goto the + decompiler printed back into this arm */ - case Map::Units::UT_E_ARCHER: - case Map::Units::UT_E_XBOW: - case Map::Units::UT_A_ARCHER: - case Map::Units::UT_A_SLINGER: - case Map::Units::UT_A_ASSASSIN: - case Map::Units::UT_A_FIRETHROWER: - if (unitInstructionType == (UnitInstructionType)0x23) { - id = id + 1; - } + case Map::Units::UT_E_KNIGHT: + _ramOrHorse = _ramOrHorse + 1; + case Map::Units::UT_TUNNELER: + case Map::Units::UT_E_SPEAR: + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + case Map::Units::UT_E_SWORD: + case Map::Units::UT_E_MONK: + case Map::Units::UT_LORD: + case Map::Units::UT_A_SLAVE: + case Map::Units::UT_A_SWORDSMAN: + id = id + 1; + break; } } while (_unitTribeIndex < this->tribes[tribeID].size); } @@ -1364,7 +1389,6 @@ namespace Map { && DAT_UnitsState::instance.units[iVar14].field303_0x413 == 0) { UVar4 = DAT_UnitsState::instance.units[iVar14].unitType; switch (UVar4) { - case Map::Units::UT_TUNNELER: case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_XBOW: case Map::Units::UT_A_ARCHER: @@ -1375,6 +1399,20 @@ namespace Map { if (unitInstructionType != (UnitInstructionType)0x23) { break; } + /* + UT_TUNNELER belongs to this arm, not the ranged one + above. The byte table at 0x52a560 maps index 0 - unit + type 5 - to arm 0x52933c, the melee arm, while the + ranged types 0x16, 0x17, 0x46, 0x48, 0x49, 0x4a and 0x4c + map to 0x529331. With the label on the ranged arm a + tunneler met the `!= 0x23` test and broke out instead of + running this body. + + The two arms are adjacent and fall through in the + original as well - 0x529331 to 0x52933c is eleven bytes, + the `cmp` and the `jnz` - so only the label moved. + */ + case Map::Units::UT_TUNNELER: case Map::Units::UT_E_SPEAR: case Map::Units::UT_E_PIKE: case Map::Units::UT_E_MACE: diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 44d1ade81f..24e5e97eae 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 57.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 58.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From e99b31d7e502cc2464b01c1723b766bba0bd3cb8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 01:56:35 +0200 Subject: [PATCH 106/202] docs: record the giveTribeAnInstruction switch audit so it is not redone All thirteen dispatches are now checked against their tables. The thirteenth, 0x52a5e4 (three dwords, base 5, range 0x48), already matches in both order and labels: 0x529f9e holds the eighteen foot types and 0x529fab the eight siege types, which is the source order. Corrected over this pass: the second, fourth, seventh, ninth and tenth switches' arm order, and one wrong label on the eleventh. Single-arm or already correct: the third, fifth, sixth, eighth, twelfth and thirteenth. giveTribeAnInstruction 56.8 -> 58.9% across the session. What remains is cold-block placement and register allocation, which the note now says explicitly. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 0edbc81ead..602822fd14 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -46,6 +46,27 @@ namespace Map { gynt: unitID and x and y and id are the target of the instruction decompilerscript: committed: 2025-01-30 21:57:43.216000 */ + /* + All thirteen switch dispatches in this function have been checked against their + tables, so this does not need redoing. They are at 0x52a1e8, 0x52a258, 0x52a2b8, + 0x52a308, 0x52a368, 0x52a3b8, 0x52a3fc, 0x52a464, 0x52a490, 0x52a4e4, 0x52a550, + 0x52a5a8 and 0x52a5e4 - found by searching the function for the indirect + `jmp dword ptr [reg*4 + disp]` rather than hunting them one at a time. + + Corrected: the outer switch's arm order (worth 23.9 earlier), then the second, + fourth, seventh, ninth and tenth, and one wrong label on the eleventh + (UT_TUNNELER, a behaviour bug). Already correct and left alone: the third, + fifth, eighth and twelfth have a single real arm each; the sixth and thirteenth + match in both order and labels. + + Every label set matched the import except the eleventh's, so the + catapult-for-mangonel class of mistake found in playUnitSelectionSound is not + widespread here. + + What is left in this function is cold-block placement and register allocation - + UIT_MAN_SIEGE_EQUIPMENT is emitted after our epilogue where the original has it + inline at 0x529a8b - which no arrangement of the source reaches. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00527C80 undefined4 TribesState::giveTribeAnInstruction( int tribeID, UnitInstructionType unitInstructionType, int id, int unitUID, int param_5) From 6ea3acc5c0568c1f8e9bb77ed247ea26251b8078 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 07:47:47 +0200 Subject: [PATCH 107/202] tools: diff_regalloc.py, splitting the gap into register naming and real structure reccmp compares instruction text, so which register MSVC chose for `this` cascades into every instruction that touches it. playUnitCombatSpeechForTarget is the clean demonstration: its prologue differs only in `mov ebx, ecx` against our `mov edi, ecx`, and from there almost nothing lines up - 31.0% raw, 65.5% once register names are replaced by a single token. That matters for where to spend effort. Ranked by total loss, rank_functions.py sends you at isTribePathToDestinationClear (18.8%) and createAnimal (46.8%) early; both are mostly register-bound (68.5% and 82.0% register-blind), which is why every structural change tried on them measured flat or worse - the split guards, the inline epilogues, the arm orders. Ranked by the instructions that differ even register-blind, they fall to 36 and 46 while spawnQueuedReinforcementWaves rises: raw 59.6 against blind 59.5, so register naming explains none of its 191-instruction gap. esp is left alone because stack references carry meaning. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../batch/diff_regalloc.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tools/reimplementation-helper/batch/diff_regalloc.py diff --git a/tools/reimplementation-helper/batch/diff_regalloc.py b/tools/reimplementation-helper/batch/diff_regalloc.py new file mode 100644 index 0000000000..35afedd663 --- /dev/null +++ b/tools/reimplementation-helper/batch/diff_regalloc.py @@ -0,0 +1,61 @@ +"""Split each function's remaining gap into register naming and real structure. + +reccmp compares instruction text, so which register MSVC picked for `this` cascades +into every instruction that touches it. playUnitCombatSpeechForTarget is the clear +case: its prologue differs only in `mov ebx, ecx` against our `mov edi, ecx`, and +from there almost nothing lines up - 31.0% raw against 65.5% once the register names +are taken out. + +This re-scores every function with the general-purpose register names replaced by one +token, leaving esp alone because stack references are meaningful. + + blind >> raw the mismatch is mostly which register holds what. Not reachable from + the source; isTribePathToDestinationClear (18.8 -> 68.5) and + createAnimal (46.8 -> 82.0) are both in this group, which is why + every structural change tried on them measured flat or worse. + blind ~= raw the instructions really differ. Worth reading the diff. + +"reach" is the instruction count that differs even register-blind, so it ranks what +is actually worth attacking - unlike rank_functions.py, which ranks by total loss and +sends you at the register-bound ones first. + +usage: diff_regalloc.py [--by-name] +""" + +import difflib +import re +import sys + +import common + +REG = re.compile(r"\b(?:e?(?:ax|bx|cx|dx|si|di|bp)|[abcd][lh]|sil|dil|bpl)\b") + + +def blind(line): + return REG.sub("R", common._normalize(line)) + + +def main(): + by_name = "--by-name" in sys.argv[1:] + rows = [] + for f, e, a in common.entries(common.load_diff()): + if not e or float(e["matching"]) >= 1.0: + continue + orig, recomp = common._asm_lines(e) + if not orig: + continue + b = difflib.SequenceMatcher( + None, [blind(x) for x in orig], [blind(x) for x in recomp], + autojunk=False).ratio() + raw = float(e["matching"]) + rows.append((len(orig) * (1 - b), len(orig) * (1 - raw), raw * 100, + b * 100, e["name"].split("::")[-1])) + rows.sort(key=(lambda r: r[4]) if by_name else (lambda r: -r[0])) + print("%7s %7s %6s %6s %s" % ("reach", "lost", "raw", "blind", "function")) + for reach, lost, raw, b, name in rows: + print("%7.0f %7.0f %6.1f %6.1f %s" % (reach, lost, raw, b, name)) + print("\n%d function(s). reach = instructions that differ even register-blind." + % len(rows)) + + +main() From 701f597ddd8d1424f908260779887d50fba059ac Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 07:52:58 +0200 Subject: [PATCH 108/202] measure: spawnQueuedReinforcementWaves addresses tribes through the global, and it costs diff_regalloc.py put this function at the top of the reachable list - raw 59.6 against 59.5 register-blind, so none of its 191-instruction gap is register naming - and the first thing the diff shows is five stores we get wrong in form. The original has all five as `mov word ptr [eax + I::instance+86], 0x32` (0x527696, 0x5277a6, 0x5278ff, 0x527b0b, 0x527b86): the instance addressed absolutely with the tribe index in eax. `this->tribes[..]` gives the two-register `[eax + + 0x56]` instead, so the original source wrote the global. Changing all five measures 59.6% -> 57.2%, and it is not that the change failed to do what it claimed: afterwards four of the five match instruction for instruction and the `+ 0x56` form has gone from the diff altogether. Materialising the global just costs more in register pressure elsewhere than the five stores gain. Reverted, because raw is what the status file tracks, with the finding written next to the first store - this is the form to return to if that function's register allocation is ever brought into line, not something to rediscover. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../spawnQueuedReinforcementWaves.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp index 76edca258a..69285f47dc 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -214,6 +214,26 @@ namespace Map { .y, 2, (UnitType)((int)(_unitType)), _nUnits); _newCount = DAT_GameState::instance.mapAndTime.startGoods[_siegeInfoSubIndex + 0x19]; + /* + The original reaches this field through the global, not + `this`. All five of these 0x32 stores are + `mov word ptr [eax + I::instance+86]` in the + binary - 0x527696, 0x5277a6, 0x5278ff, 0x527b0b and + 0x527b86 - addressing the instance absolutely with the + tribe index in eax, where `this->tribes[..]` gives the + two-register `[eax + + 0x56]`. So the original + source almost certainly wrote the global here. + + Changing all five to DAT_TribesState::instance measures + 59.6% -> 57.2%, even though it is demonstrably right: + afterwards four of the five match instruction for + instruction and the `+ 0x56` form is gone from the diff + entirely. Materialising the global costs more in + register pressure elsewhere than the five stores gain. + Left as `this->` for the score, with the finding recorded + - if this function's allocation is ever brought into line + the global is the form to go back to. + */ this->tribes[_tribeID1].unknownAttackRelatedUpdateCounter = 0x32; _destinationIndexTracker = _destinationIndexTracker + 1; } while (_newCount); From 921a3bc4dbfe61bf7bb78c66314cc3426b6ad324 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:01:52 +0200 Subject: [PATCH 109/202] reimplement: comparisons written with their operands swapped, +15.0 over 9 functions `if (a < b)` and `if (b > a)` are the same test, and MSVC emits a different `cmp` for each - the left operand goes first. Written the wrong way round a comparison costs its `cmp` and the `jcc` or `setcc` that follows, and neither diff_jcc nor diff_inv_jcc reports it: the branch sense agrees with the operands, so nothing looks inverted. The +10.1 on snapshotSelectionTribeAndComputeStance earlier was one of these found by eye, which is what prompted diff_cmp_operands.py, added here. It pairs an orig `cmp A, B` against an ours `cmp B, A` in the same block and requires the following jcc/setcc to be mirrored, which is what distinguishes a swap from an unrelated pair. getMajoritySelectedUnitType 71.8 -> 75.7 addUnitToTribeAndUpdateTribeMovementSpeed 67.8 -> 71.1 addUnitsToTribeAndComputeMovementSpeed 73.5 -> 76.1 getTribeAliveStatus 67.2 -> 68.8 updateLionWolfTribeBehavior 71.6 -> 72.7 updateCamelTribeBehavior 70.8 -> 71.9 processDeerMoving 62.6 -> 63.3 (two sites) computeTribePercentages 45.3 -> 46.0 trimTribeToSize 65.9 -> 65.9 (flat, kept: the operand order is what the original has) Four of them are the same expression: `rallyPointCount <= currentRallyPointIndex`, where the original has `cmp ax, word ptr [esi + 0x28a]` with a `jl` - index first, count second. Three more sites the tool reports are left for now - spawnDeerLionOrRabbit, predictUnitInterceptPosition and giveTribeAnInstruction - because which source expression each corresponds to is not clear from the register names alone, and guessing would risk flipping the wrong comparison. Namespace average 84.40 -> 84.55. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...UnitToTribeAndUpdateTribeMovementSpeed.cpp | 2 +- ...addUnitsToTribeAndComputeMovementSpeed.cpp | 2 +- .../TribesState/computeTribePercentages.cpp | 2 +- .../getMajoritySelectedUnitType.cpp | 2 +- .../Units/TribesState/getTribeAliveStatus.cpp | 2 +- .../Units/TribesState/processDeerMoving.cpp | 4 +- .../Map/Units/TribesState/trimTribeToSize.cpp | 2 +- .../TribesState/updateCamelTribeBehavior.cpp | 9 ++- .../updateLionWolfTribeBehavior.cpp | 2 +- .../batch/diff_cmp_operands.py | 77 +++++++++++++++++++ 10 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 tools/reimplementation-helper/batch/diff_cmp_operands.py diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp index 835e1cb709..aa22047ff2 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp @@ -43,7 +43,7 @@ namespace Map { if (_movementSpeed < _minimumSpeed) { _minimumSpeed = _movementSpeed; } - if (_maximumSpeed < _movementSpeed) { + if (_movementSpeed > _maximumSpeed) { _maximumSpeed = _movementSpeed; } _totalHealth = _totalHealth + _ptrUnit->healthPercentage; diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp index 1d93ea52d4..0caac8d154 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp @@ -45,7 +45,7 @@ namespace Map { if (_movementSpeed < _minimumSpeed) { _minimumSpeed = _movementSpeed; } - if (_maximumSpeed < _movementSpeed) { + if (_movementSpeed > _maximumSpeed) { _maximumSpeed = _movementSpeed; } local_8 = local_8 + _pUnit->healthPercentage; diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 18a07ff99f..5f5dac4848 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -101,7 +101,7 @@ namespace Map { this->tribes[tribeID].unitType = ((UnitType)0); } short _movementSpeed = DAT_UnitsState::instance.units[_unitID].movementSpeed; - if (this->tribes[tribeID].movementSpeed < _movementSpeed) { + if (_movementSpeed > this->tribes[tribeID].movementSpeed) { this->tribes[tribeID].movementSpeed = _movementSpeed; } } diff --git a/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp index 96b207b5c1..dabdc9a80c 100644 --- a/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp @@ -50,7 +50,7 @@ namespace Map { _maximumCount = -1; _index = 1; do { - if (_maximumCount < *(int*)((int)_countOfUnitTypes + _index * 4)) { + if (*(int*)((int)_countOfUnitTypes + _index * 4) > _maximumCount) { _maximumTroopType = _index; _maximumCount = *(int*)((int)_countOfUnitTypes + _index * 4); } diff --git a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp index f9ec9c4373..8028fb7da3 100644 --- a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp @@ -45,7 +45,7 @@ namespace Map { `if (!_alive) return 100;` measures 67.2% -> 67.2%. */ if (_alive != 0) { - if (_alive <= _other) { + if (_other >= _alive) { return (int)(50); } return (uint)(_other != 0); diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index e5c6e5cf0f..3786db409d 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -92,7 +92,7 @@ namespace Map { (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; - if (this->tribes[tribeID].rallyPointCount <= this->tribes[tribeID].currentRallyPointIndex) { + if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { this->tribes[tribeID].rallyPointCount = 0; this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; } @@ -154,7 +154,7 @@ namespace Map { this)(tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][0]), (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0); this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; - if (this->tribes[tribeID].rallyPointCount <= this->tribes[tribeID].currentRallyPointIndex) { + if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; this->tribes[tribeID].rallyPointCount = 0; } diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 90c9f76044..60fd278aed 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -55,7 +55,7 @@ namespace Map { && smallerSize <= unitSelectionIndex) { *pUVar1 = Map::Units::ULS_REMOVE; } - } while (unitSelectionIndex < _tb->tribes[0].size); + } while (_tb->tribes[0].size > unitSelectionIndex); } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index b92fc53e13..d5256f271b 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -86,7 +86,14 @@ namespace Map { (uint)((int)this->tribes[param_1].rallyPointArray[sVar5][1]), 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[param_1].currentRallyPointIndex = this->tribes[param_1].currentRallyPointIndex + 1; - if (this->tribes[param_1].rallyPointCount <= this->tribes[param_1].currentRallyPointIndex) { + /* + Operands this way round, not `rallyPointCount <= index`. The + original has `cmp ax, word ptr [esi + 0x28a]` with a `jl` - the + index first, the count second - where the reverse spelling gives + `cmp word ptr [esi + 0x28a], ax` and a `jg`. Same test, two + different instructions. diff_cmp_operands.py finds these. + */ + if (this->tribes[param_1].currentRallyPointIndex >= this->tribes[param_1].rallyPointCount) { this->tribes[param_1].rallyPointCount = 0; this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; } diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index f841e2aae4..e78d92f8c4 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -94,7 +94,7 @@ namespace Map { (uint)((int)this->tribes[param_1].rallyPointArray[sVar2][1]), 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[param_1].currentRallyPointIndex = this->tribes[param_1].currentRallyPointIndex + 1; - if (this->tribes[param_1].rallyPointCount <= this->tribes[param_1].currentRallyPointIndex) { + if (this->tribes[param_1].currentRallyPointIndex >= this->tribes[param_1].rallyPointCount) { this->tribes[param_1].rallyPointCount = 0; this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; } diff --git a/tools/reimplementation-helper/batch/diff_cmp_operands.py b/tools/reimplementation-helper/batch/diff_cmp_operands.py new file mode 100644 index 0000000000..b2cb37d0b8 --- /dev/null +++ b/tools/reimplementation-helper/batch/diff_cmp_operands.py @@ -0,0 +1,77 @@ +"""Comparisons written with their operands the wrong way round. + +`if (a < b)` and `if (b > a)` are the same test and MSVC emits a different `cmp` for +each: the left operand goes first. So a comparison written the other way round costs +the `cmp` and its `jcc` or `setcc`, and neither diff_jcc nor diff_inv_jcc reports it - +the branch sense is consistent with the operands, so nothing looks inverted. + +It found +10.1 on snapshotSelectionTribeAndComputeStance (`cmp ecx, esi; setge al` +against our `cmp esi, ecx; setle al`) and a two-instruction miss in +addUnitsToTribeAndComputeMovementSpeed. + +This looks for an orig `cmp A, B` facing an ours `cmp B, A` in the same diff block, +with the following jcc or setcc mirrored to match - which is what makes it a swap +rather than an unrelated pair. + +usage: diff_cmp_operands.py +""" + +import re +import sys + +import common + +MIRROR = {"jl": "jg", "jg": "jl", "jle": "jge", "jge": "jle", + "jb": "ja", "ja": "jb", "jbe": "jae", "jae": "jbe", + "setl": "setg", "setg": "setl", "setle": "setge", "setge": "setle", + "setb": "seta", "seta": "setb", "setbe": "setae", "setae": "setbe"} + +CMP = re.compile(r"^cmp\s+([^,]+),\s*(.+)$") + + +def operands(line): + m = CMP.match(common._normalize(line).strip()) + return (m.group(1).strip(), m.group(2).strip()) if m else None + + +def follower(rows, i): + for r in rows[i + 1:i + 3]: + w = r.strip().split() + if w and w[0] in MIRROR: + return w[0] + return None + + +def main(): + hits = 0 + for f, e, a in common.entries(common.load_diff()): + if not e or float(e["matching"]) >= 1.0: + continue + found = [] + for hunk in e.get("diff") or []: + for block in hunk[1]: + o = [r[1] for r in block.get("orig", [])] + r_ = [r[1] for r in block.get("recomp", [])] + for i, ol in enumerate(o): + oo = operands(ol) + if not oo: + continue + for j, rl in enumerate(r_): + ro = operands(rl) + if not ro or ro != (oo[1], oo[0]): + continue + of, rf = follower(o, i), follower(r_, j) + if of and rf and MIRROR.get(of) == rf: + found.append((ol.strip(), of, rl.strip(), rf)) + break + if found: + hits += 1 + print("### %s %.1f%%" % (e["name"].split("::")[-1], + float(e["matching"]) * 100)) + for ol, of, rl, rf in found[:4]: + print(" orig %-46s %s" % (ol[:46], of)) + print(" ours %-46s %s" % (rl[:46], rf)) + print("\n%d function(s) with a swapped comparison" % hits) + + +main() From 227168abf3e6cbd6ce8d647a117121142412bd93 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:04:16 +0200 Subject: [PATCH 110/202] reimplement: the last four swapped comparisons, +8.0; none left in the namespace trimTribeToSize 65.9 -> 70.6 predictUnitInterceptPosition 31.7 -> 34.1 (two sites) spawnDeerLionOrRabbit 73.0 -> 73.8 giveTribeAnInstruction 58.9 -> 59.0 (two sites) trimTribeToSize is the instructive one. Its loop has two comparisons and the original puts the index first in both - `cmp esi, dword ptr [esp + 0x18]` with a `jl` for the `smallerSize` test and `cmp esi, ecx` with a `jl` on the back-edge. In the first pass I swapped the back-edge, which was already right, and left the one that was wrong: that made a correct comparison wrong, the two cancelled, and the function measured flat while the tool still reported a swap. Fixing the right one is worth 4.7. The lesson is to identify which source expression a reported pair belongs to before changing anything, which is why three of these were held back from the previous commit rather than guessed at. diff_cmp_operands.py now reports 0 functions with a swapped comparison. Across the thirteen functions the pattern was worth +23.0. Namespace average 84.55 -> 84.62; status synced, 0 mismatches. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 4 ++-- .../predictUnitInterceptPosition.cpp | 4 ++-- .../TribesState/spawnDeerLionOrRabbit.cpp | 2 +- .../Map/Units/TribesState/trimTribeToSize.cpp | 13 ++++++++-- status/addresses-SHC-3BB0A8C1.txt | 24 +++++++++---------- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 602822fd14..fbfa946fbb 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -519,7 +519,7 @@ namespace Map { .totalSizeOfPathPlan / 2); DAT_UnitsState::instance.units[iVar11].lookForEnemy = -0x32; - if ((int)unitInstructionType < (int)UVar10) { + if ((int)UVar10 > (int)unitInstructionType) { unitInstructionType = UVar10; } id = id + 0xc; @@ -761,7 +761,7 @@ namespace Map { UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[iVar14] .totalSizeOfPathPlan / 2); - if ((int)unitInstructionType < (int)UVar10) { + if ((int)UVar10 > (int)unitInstructionType) { unitInstructionType = UVar10; } id = id + 0xc; diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index d1ad15bb7b..b6112674f9 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -79,7 +79,7 @@ namespace Map { uint _yOff_2 = _yOffb - iVar2; int _xOff_2b = (_xOff_2 ^ (int)_xOff_2 >> 0x1f) - ((int)_xOff_2 >> 0x1f); int _yOff_2b = (_yOff_2 ^ (int)_yOff_2 >> 0x1f) - ((int)_yOff_2 >> 0x1f); - if (_yOff_2b < _xOff_2b) { + if (_xOff_2b > _yOff_2b) { _yOff_2b = _xOff_2b; } /* @@ -126,7 +126,7 @@ namespace Map { uint _yOff = *unitCurrentY - iVar2; iVar3 = (_xOff ^ (int)_xOff >> 0x1f) - ((int)_xOff >> 0x1f); _yOffb = (_yOff ^ (int)_yOff >> 0x1f) - ((int)_yOff >> 0x1f); - if (_yOffb < iVar3) { + if (iVar3 > _yOffb) { _yOffb = iVar3; } if ((_yOffb + -2) * unitID < local_8) { diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index 47ba68393e..68d9a56887 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -74,7 +74,7 @@ namespace Map { */ psVar1 = &this->tribes[tribeID].field138_0x284; *psVar1 = *psVar1 + 1; - if ((param_2 <= this->tribes[tribeID].field138_0x284) + if ((this->tribes[tribeID].field138_0x284 >= param_2) && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field138_0x284 = 0, _tribeSize < sVar2)) { if ((unitType == Map::Units::UT_RABBIT) diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 60fd278aed..808ba86d98 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -52,10 +52,19 @@ namespace Map { unitSelectionIndex = unitSelectionIndex + 1; if ((((UVar2 == ((UnitLogicState)1)) || (UVar2 == Map::Units::ULS_NORMAL)) || (UVar2 == Map::Units::ULS_TRANSITIONING)) - && smallerSize <= unitSelectionIndex) { + /* + The index comes first in both of this loop's comparisons. The + original has `cmp esi, dword ptr [esp + 0x18]` with a `jl` here + and `cmp esi, ecx` with a `jl` on the back-edge - esi being the + index in each - where `smallerSize <= index` gives the reverse + `cmp` and a `jg`. I first swapped the back-edge instead of this + one, which made a correct comparison wrong and measured flat + overall; the two cancelled out. + */ + && unitSelectionIndex >= smallerSize) { *pUVar1 = Map::Units::ULS_REMOVE; } - } while (_tb->tribes[0].size > unitSelectionIndex); + } while (unitSelectionIndex < _tb->tribes[0].size); } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 24e5e97eae..73a2b178e7 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46389,11 +46389,11 @@ SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521280 | 33.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521500 | 31.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521500 | 34.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending -SHC_3BB0A8C1_0x00521720 | 71.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521720 | 75.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x005217E0 | 62.1% | Reimplemented, asm differs @@ -46803,9 +46803,9 @@ SHC_3BB0A8C1_0x00522890 | 79.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005229C0 | 73.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005229C0 | 76.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522B00 | 67.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522B00 | 71.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522BC0 | 100.0% | Reimplemented @@ -46819,7 +46819,7 @@ SHC_3BB0A8C1_0x00522CA0 | 95.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522EF0 | 65.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522EF0 | 70.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522F70 | 78.0% | Reimplemented, asm differs @@ -46905,7 +46905,7 @@ SHC_3BB0A8C1_0x00524A30 | 0.0% | Pending SHC_3BB0A8C1_0x00524B30 | 80.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524BF0 | 67.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524BF0 | 68.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524CA0 | 18.8% | Reimplemented, asm differs @@ -46951,7 +46951,7 @@ SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525F20 | 73.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525F20 | 73.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005260B0 | 100.0% | Reimplemented @@ -46969,7 +46969,7 @@ SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending SHC_3BB0A8C1_0x00526F00 | 42.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005270F0 | 45.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005270F0 | 46.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 58.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 59.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending @@ -49141,13 +49141,13 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 62.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052AD50 | 63.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 71.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B110 | 72.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B630 | 70.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B630 | 71.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs From 1851d6bd930a6f7d25fb8b594b9cdf50e96a8ebf Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:06:22 +0200 Subject: [PATCH 111/202] tools: diff_anchor.py missed cursors into global arrays Its ANCHOR_RE only recognised `lea reg, [reg + N]` and `add reg, N`. A cursor into a global array is set up with neither - it is `mov esi, StructResolver::Instance<..> ::instance+3568 (OFFSET)` - so every anchor of that shape went unreported, including the two in this namespace. MIN_ANCHOR also had to be relaxed for the new form: it exists to keep small `add reg, N` strides out of the way, but for a mov-into-global the constant is an offset inside the global, where a few thousand is ordinary. 3568 was being filtered out as too small. It now reports addUnitsToTribeAndComputeMovementSpeed and addUnitToTribeAndUpdateTribeMovementSpeed: the original bases their unit loop on `&units[1].movementSpeed` (unit offset 0x34c, global +3568) and reads the speed at `[esi]` with dying at `[esi - 0xac]`; we anchor on `&units[1].dying` (0x2a0, +3396), so every displacement in the loop differs by 0xac while the opcodes line up. Same addresses at run time, five different instructions an iteration - which is most of what is left in both functions, and it reads as allocator noise in the diff. Recorded on both rather than guessed at: that file's own notes list what has been tried on anchors elsewhere and found ineffective, including an int* local pointing straight at the field the original anchored on. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- ...UnitToTribeAndUpdateTribeMovementSpeed.cpp | 9 +++++++++ ...addUnitsToTribeAndComputeMovementSpeed.cpp | 16 +++++++++++++++ .../batch/diff_anchor.py | 20 ++++++++++++++----- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp index aa22047ff2..45a77ac311 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp @@ -35,6 +35,15 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, tribeID); if (1 < DAT_UnitsState::instance.maxUnitCount) { tribeID = DAT_UnitsState::instance.maxUnitCount - 1; + /* + Same anchor difference as its twin addUnitsToTribeAndComputeMovementSpeed, + and most of what is left here. The original bases the unit loop on + `mov esi, I::instance+3568` - `&units[1].movementSpeed`, unit + offset 0x34c - and reads the speed at `[esi]`; we anchor on +3396, + `&units[1].dying` (0x2a0), so every displacement in the loop differs by 0xac + while the opcodes line up. diff_anchor.py reports both; see the fuller note + on the twin. + */ _ptrUnit = &DAT_UnitsState::instance.units[1]; do { if (_ptrUnit->logicalState == Map::Units::ULS_NORMAL && !_ptrUnit->dying diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp index 0caac8d154..b692c52240 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp @@ -35,6 +35,22 @@ namespace Map { int _minimumSpeed = 100; _maximumSpeed = 0; if (1 < DAT_UnitsState::instance.maxUnitCount) { + /* + The loop cursor is anchored on the wrong field, and that is most of what is + left here. MSVC bases the whole unit loop on one address: the original uses + `mov esi, I::instance+3568`, which is `&units[1].movementSpeed` + (unit offset 0x34c), and reads the speed at `[esi]` with dying at + `[esi - 0xac]`, logicalState at `[esi - 0x2c0]`, the owner at `[esi - 0x29a]` + and the health at `[esi - 0x78]`. We get +3396, `&units[1].dying` (0x2a0), + and every displacement in the loop differs by 0xac while the opcodes line up. + Same addresses at run time; five different instructions per iteration. + + diff_anchor.py now reports this - it used to miss it, because it only + recognised `lea`/`add` anchors and a cursor into a global array is set up + with `mov reg, +N`. Its notes list what has already been tried on + anchors elsewhere and found ineffective, so this is recorded rather than + guessed at. + */ _pUnit = &DAT_UnitsState::instance.units[1]; _unitID = 1; do { diff --git a/tools/reimplementation-helper/batch/diff_anchor.py b/tools/reimplementation-helper/batch/diff_anchor.py index 7ce6bcc8ea..df4029bfc7 100644 --- a/tools/reimplementation-helper/batch/diff_anchor.py +++ b/tools/reimplementation-helper/batch/diff_anchor.py @@ -42,10 +42,17 @@ import common -# `lea esi, [ecx + 0x367e4]`, `add esi, 0x367e4`, `lea ebx, [eax + ebp]` is not an anchor +# `lea esi, [ecx + 0x367e4]`, `add esi, 0x367e4`, `lea ebx, [eax + ebp]` is not an anchor. +# A cursor into a global array is set up with neither of those - it is +# `mov esi, StructResolver::Instance<..>::instance+3568 (OFFSET)` - so that form counts too, +# with the offset into the global as the constant. Missing it hid the 0xac anchor difference in +# addUnitsToTribeAndComputeMovementSpeed and addUnitToTribeAndUpdateTribeMovementSpeed, where the +# original anchors the unit cursor on `movementSpeed` and we anchor it on `dying`. ANCHOR_RE = re.compile(r"^(?:lea (e[a-z]{2}), \[(e[a-z]{2}) \+ (0x[0-9a-f]{4,}|\d{4,})\]" - r"|add (e[a-z]{2}), (0x[0-9a-f]{4,}|\d{4,}))$") + r"|add (e[a-z]{2}), (0x[0-9a-f]{4,}|\d{4,})" + r"|mov (e[a-z]{2}), \S*::instance\+(\d{4,})(?: \(OFFSET\))?)$") MIN_ANCHOR = 0x1000 +MIN_FIELD_ANCHOR = 0x40 def op(line): @@ -59,9 +66,12 @@ def anchors(lines): m = ANCHOR_RE.match(line) if not m: continue - reg = m.group(1) or m.group(4) - value = int(m.group(3) or m.group(5), 0) - if value >= MIN_ANCHOR: + reg = m.group(1) or m.group(4) or m.group(6) + value = int(m.group(3) or m.group(5) or m.group(7), 0) + # MIN_ANCHOR exists to keep small `add reg, N` strides out of the way. It does not + # apply to the mov-into-global form: there the constant is an offset inside the + # global, so a few thousand is an ordinary anchor - 3568 for the unit cursor. + if value >= (MIN_FIELD_ANCHOR if m.group(6) else MIN_ANCHOR): out.append((reg, value)) return out From 66922a3791438105db2110e7e576979960d2649c Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:12:06 +0200 Subject: [PATCH 112/202] reimplement: drawFlagsAndUnitDestinations 54.7 -> 55.9%, the zero arm comes first `cmp dx, 8; jl 0x4` jumps over `xor eax, eax` into the scaling, so `iVar4 = 0` sits under the test and the `(8 - speed) * 4` arm follows it. Written `sVar1 < 8` first the two swap and the test inverts to `jge`. playUnitSelectionSound has the same shape in its ladderman arm - `cmp edi, 4; jl 0x24` jumping away for a smaller selection, so `push 0x58` is the original's fall-through - and there the fix loses: 62.1% -> 56.0%. Reverted with the number recorded on the function. That is the second loss for the inverted-arm fix, after giveTribeMoveInstruction, and both are functions whose remaining gap is dominated by where the shared epilogue lands rather than by arm order; the running tally is 12 wins to 2 losses. Two hits on that tool's list were checked and are false pairs, noted here so they are not re-examined: assignAttackTargetsForTribe's chain does test `cx, 3` (at 0x5245f9 - the diff window had cut it off, so its order matches ours), and both of predictUnitInterceptPosition's are same-sense or register-only. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/drawFlagsAndUnitDestinations.cpp | 13 ++++++++++--- .../Units/TribesState/playUnitSelectionSound.cpp | 10 ++++++++++ status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index 01ea6cadb8..04eba14fb7 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -90,10 +90,17 @@ namespace Map { goes in the far block. */ if (sVar1) { - if (sVar1 < 8) { - iVar4 = (8 - sVar1) * 4; - } else { + /* + The zero is the fall-through. The original has + `cmp dx, 8; jl 0x4` - jumping over `xor eax, eax` into the + scaling - so `iVar4 = 0` sits under the test and the + `(8 - speed) * 4` arm comes after it. Written `sVar1 < 8` + first they swap and the test inverts to `jge`. + */ + if (sVar1 >= 8) { iVar4 = 0; + } else { + iVar4 = (8 - sVar1) * 4; } tile = (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX + DAT_ViewportRenderState::instance diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 271cd39e1b..e1ac5bd0de 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -91,6 +91,16 @@ namespace Map { (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0x57); } + /* + This one stays, against the asm. The original has `cmp edi, 4; + jl 0x24`, jumping away for a smaller selection, so `push 0x58` is + its fall-through and the lone 0x59 play sits in the far block - + ours is the other way round. Rewriting it as + `if (param_1 >= 4) { _sfx = 0x58; } else { ...; return; }` measures + 62.1% -> 56.0%. Second loss for the inverted-arm fix after + giveTribeMoveInstruction, and both are in functions whose remaining + gap is dominated by where the shared epilogue lands. + */ if (param_1 < 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[iVar3].x, diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 73a2b178e7..5205ec49bd 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46395,7 +46395,7 @@ SHC_3BB0A8C1_0x00521690 | 0.0% | Pending SHC_3BB0A8C1_0x00521720 | 75.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 62.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005217E0 | 56.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending @@ -46861,7 +46861,7 @@ SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523920 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523A30 | 54.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523A30 | 55.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523B00 | 0.0% | Pending From 0ebc9149e1ec1a3ff4e814d6f6bd131ac629ba69 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:22:03 +0200 Subject: [PATCH 113/202] reimplement: getMajoritySelectedUnitType 75.7 -> 93.1%, widen the type once The original widens the unit type once - `movsx ecx, word ptr [eax - 0x212]` - then compares `cmp ecx, 0x50` and indexes the tally array with the same register. The import cast at each use instead: `(short)unitType < 0x50` for the bound and `(short)unitType * 4` twice for the index. That gives a `movzx` for the load, a 16-bit `cmp cx, 0x50`, and then a second `movsx ecx, cx` to get an index back - three instructions where the original has two, plus the wrong compare width. One `int const _type = _pUnit->unitType;` used for both does it, and lets the index be written as `_countOfUnitTypes[_type]` rather than `*(int*)((int)_countOfUnitTypes + (short)_pUnit->unitType * 4)`. Found via a compare-width scan: a comparison the original does 32 bits wide and we do 16 names a local whose type is wrong. Two other hits from that scan measured worse and are reverted with the numbers recorded: - giveTribeAnInstruction's UVar4 as UnitTypeInt rather than UnitTypeShort. The original does compare it 32-bit in places (`cmp ecx, 0x16`, `cmp ecx, 0x46` at the pitch-ditch arm) and dispatches on it 32-bit, so it looked right: 59.0% -> 57.6% across its sixteen uses. - a plain `(int)` cast on the bound test here, before the local was introduced: flat, and the `cmp cx, 0x50` was still in the output. The cast alone does not widen it; the value has to pass through a 32-bit local. The remaining width hits all narrow a shared decompiler scratch - sVar2 in updateLionWolfTribeBehavior and processDeerMoving - which is the shape that cost 17 and 19 points in updateRabbitTribeBehavior, so they are left alone. Namespace average 84.63 -> 84.79. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../getMajoritySelectedUnitType.cpp | 18 ++++++++++++++---- .../TribesState/giveTribeAnInstruction.cpp | 6 ++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp index dabdc9a80c..b499562f22 100644 --- a/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getMajoritySelectedUnitType.cpp @@ -37,10 +37,20 @@ namespace Map { bug: shouldn't this check if the unit is part of the tribe !? It checks for being selected I see */ if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying - && _pUnit->owner == DAT_GameSynchronyState::instance.currentPlayerSlotID && _pUnit->isSelected - && (short)_pUnit->unitType < 0x50) { - *(int*)((int)_countOfUnitTypes + (short)_pUnit->unitType * 4) - = *(int*)((int)_countOfUnitTypes + (short)_pUnit->unitType * 4) + 1; + && _pUnit->owner == DAT_GameSynchronyState::instance.currentPlayerSlotID + && _pUnit->isSelected) { + /* + One sign-extended `int`, used for both the bound test and the + index. The original widens once - `movsx ecx, word ptr + [eax - 0x212]` - then compares `cmp ecx, 0x50` and indexes with + the same register. Casting at each use instead gives a `movzx` + for the test, a 16-bit `cmp cx, 0x50`, and a second `movsx ecx, + cx` for the index: three instructions where the original has two. + */ + int const _type = _pUnit->unitType; + if (_type < 0x50) { + _countOfUnitTypes[_type] = _countOfUnitTypes[_type] + 1; + } } _pUnit = _pUnit + 1; _countInclusive = _countInclusive + -1; diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index fbfa946fbb..eca6945527 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -74,6 +74,12 @@ namespace Map { short* psVar1; byte bVar2; UnitStateShort UVar3; + /* + UVar4 stays 16 bits wide. The original compares it 32-bit in places - `cmp + ecx, 0x16` and `cmp ecx, 0x46` at the pitch-ditch arm against our + `cmp cx, ..` - and the switch dispatches on it 32-bit too, so UnitTypeInt + looks right. It measures 59.0% -> 57.6% across the sixteen uses. Reverted. + */ UnitTypeShort UVar4; /* Dword flags, not `bool`s: diff_slots.py shows byte-aligned stack slots at From 016ad73584e81deda50209733a7f1fbc5ec776ee Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:31:21 +0200 Subject: [PATCH 114/202] reimplement: computeTribePercentages 46.0 -> 47.2%, and when widening a local pays The percentages are kept in an `int`. The original holds each one in a 32-bit register and stores `ax` straight into the field - `idiv ecx` then `mov word ptr [esi + 0x2be], ax` - where a `short` local makes MSVC truncate the division result and widen it back before every store. That added a `movzx eax, ax` at all four sites; the original has none at all in that function. Found by counting register-to-register re-extensions (`movsx ecx, cx` and friends) on each side: an excess on ours is a value being truncated and widened again, which means the source cast at the use site instead of widening once. computeTribePercentages had four against zero. giveTribeAnInstruction has 33 against the original's 10, almost all `sVar9` being sign-extended afresh each time it indexes. Widening it measures 59.0% -> 58.3%, and UVar4 as UnitTypeInt measured 59.0% -> 57.6% earlier. Both are reverted, and the pair of results is now recorded on that function next to the +17.4 that the same change bought on getMajoritySelectedUnitType: the difference is single-purpose versus shared. There the local held one thing, the unit type, tested against a bound and then used as an index. `sVar9` is the decompiler's general short scratch, a tribe size in one arm and a unit id in the next, and widening it just moves the extensions around. Namespace average 84.79 -> 84.80; status synced, 0 mismatches. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/computeTribePercentages.cpp | 25 ++++++++++++------- .../TribesState/giveTribeAnInstruction.cpp | 15 +++++++++++ status/addresses-SHC-3BB0A8C1.txt | 8 +++--- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 5f5dac4848..6070068824 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -33,7 +33,14 @@ namespace Map { the percentages costs a `movzx` plus a `movsx`. */ int const _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; - short _percentage = 0; + /* + An `int`, not a `short`. The original keeps each percentage in a 32-bit + register and stores `ax` straight into the field - `idiv ecx` then + `mov word ptr [esi + 0x2be], ax`. A `short` local makes MSVC truncate the + division result and widen it back before every store, adding a + `movzx eax, ax` at all four sites; the original has none. + */ + int _percentage = 0; int iVar4 = 0; this->tribes[tribeID].percentageDyingUnk = 0; this->tribes[tribeID].percentageMovingUnk = 0; @@ -116,27 +123,27 @@ namespace Map { if (!_tribeSize) { _percentage = 100; } else { - _percentage = (short)((_countDying * 100) / _tribeSize); + _percentage = (_countDying * 100) / _tribeSize; } - this->tribes[tribeID].percentageDyingUnk = _percentage; + this->tribes[tribeID].percentageDyingUnk = (short)_percentage; if (!_tribeSize) { _percentage = 100; } else { - _percentage = (short)((_countMoving * 100) / _tribeSize); + _percentage = (_countMoving * 100) / _tribeSize; } - this->tribes[tribeID].percentageMovingUnk = _percentage; + this->tribes[tribeID].percentageMovingUnk = (short)_percentage; if (!_tribeSize) { _percentage = 100; } else { - _percentage = (short)((_countAttackingUnk * 100) / _tribeSize); + _percentage = (_countAttackingUnk * 100) / _tribeSize; } - this->tribes[tribeID].percentageAttackingUnk = _percentage; + this->tribes[tribeID].percentageAttackingUnk = (short)_percentage; if (!_tribeSize) { _percentage = 100; } else { - _percentage = (short)((_countRangeUnk * 100) / _tribeSize); + _percentage = (_countRangeUnk * 100) / _tribeSize; } - this->tribes[tribeID].percentageShootingUnk = _percentage; + this->tribes[tribeID].percentageShootingUnk = (short)_percentage; /* The last percentage is the same shape as the four above it, not a guarded store followed by an unconditional 100 - which is what the import had, and diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index eca6945527..f4aca5919c 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -75,6 +75,21 @@ namespace Map { byte bVar2; UnitStateShort UVar3; /* + Both of this function's wide scratch locals stay as they are, and the two + measurements together say when widening pays and when it does not. + + `sVar9` is a `short` used 82 times, and the diff shows 33 register-to- + register re-extensions against the original's 10 - `movsx ecx, cx` each time + it is used as an index again. Widening it to `int` to extend once measures + 59.0% -> 58.3%. + + The same change was worth +17.4 on getMajoritySelectedUnitType. The + difference is that there the local had one purpose - the unit type, used for + a bound test and then as an array index - where `sVar9` here is the + decompiler's general short scratch, holding a tribe size in one arm and a + unit id in the next. Widening a single-purpose local that is tested and then + indexed with pays; widening a shared scratch does not. + UVar4 stays 16 bits wide. The original compares it 32-bit in places - `cmp ecx, 0x16` and `cmp ecx, 0x46` at the pitch-ditch arm against our `cmp cx, ..` - and the switch dispatches on it 32-bit too, so UnitTypeInt diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 5205ec49bd..7fedccb6d6 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46393,9 +46393,9 @@ SHC_3BB0A8C1_0x00521500 | 34.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending -SHC_3BB0A8C1_0x00521720 | 75.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521720 | 93.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 56.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005217E0 | 62.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending @@ -46969,7 +46969,7 @@ SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending SHC_3BB0A8C1_0x00526F00 | 42.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005270F0 | 46.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005270F0 | 47.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 59.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 58.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From ef5a6bd42fc9d947887275b6e7b929b77eb8422d Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:38:10 +0200 Subject: [PATCH 115/202] reimplement: upgradeTribeArrayLayoutForMapVersion 46.5 -> 49.4%, memset not a loop The original clears those 200 bytes with `mov ecx, 0x32` and a single `rep stosd` - fifty dwords - which is what MSVC inlines a constant-size memset to. The decompiler rendered it as a two-shorts-per-iteration loop, which compiles to eight instructions: the pair of stores, the cursor bump, the counter decrement and the branch. Found by comparing the multiset of named call targets between the two sides, looking for a call the original makes and we do not or vice versa. Most of what that turns up is jump-target noise, but three things in it were worth reading: - this `rep stosd`, which showed as an orig-only instruction beside our loop. - updateTribes calls markTribeAsAnimalTribe twice in the original and once in ours. Not a missing call: our two switch arms for 0x11 and 0x12 are identical and MSVC merged them, where the original kept both. Same behaviour either way. - `memmove` appears twice on our side and never on the original's, which calls an unresolved target there instead - the game's own copy routine rather than the CRT's. That is a resolver question, not a source one, and it is why this function's raw trails its normalized score. __security_check_cookie on clearTribesOfUnitType, clearTribesNotOfUnitType and removeSelectedUnitsFromTheirCurrentTribes is the known cap on those three - our DLL's cookie against the game's - and accounts for the whole of their remaining ~3%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../upgradeTribeArrayLayoutForMapVersion.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 478af34579..f6df49886d 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -47,11 +47,14 @@ namespace Map { _Src = _Src + -0x136; _Dst = _Dst + -1; psVar3 = psVar3 + 100; - for (iVar1 = 50; iVar1 != 0; iVar1 = iVar1 + -1) { - psVar3[0] = 0; - psVar3[1] = 0; - psVar3 = psVar3 + 2; - } + /* + A memset, not a loop. The original clears these 200 bytes with + `mov ecx, 0x32` and a single `rep stosd` - fifty dwords - which is what + MSVC inlines a constant-size memset to. The decompiler's two-shorts-per- + iteration loop compiles to eight instructions instead: the pair of + stores, the cursor bump, the counter decrement and the branch. + */ + memset(psVar3, 0, (size_t)((int)(200))); } while (-1 < iVar2); return; } From 2c151a6dce9c780d328d260c9379f00f5602e0e8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:42:10 +0200 Subject: [PATCH 116/202] reimplement: upgradeTribeArrayLayoutForMapVersion 49.4 -> 57.5% with __stosd The original clears 200 bytes with `mov ecx, 0x32` and one `rep stosd`. Three spellings, measured: the decompiler's two-shorts-per-iteration loop 46.5% memset(psVar3, 0, 200) 49.4% __stosd((unsigned long*)psVar3, 0, 50) 57.5% memset is the idiomatic one and it is better than the loop, but this build does not inline it - it emits `push 0xc8; push 0; push ebx; call memset; add esp, 0x18`. So the original's `rep stosd` is an inlined memset and /Oi is not on for us. The intrinsic emits the instruction directly and is the only one of the three that produces what the original has, which is why it is used here in preference to memset; the reasoning is recorded next to it. That clears the last rep-instruction mismatch in the namespace. 46.5 -> 57.5 overall for this function, average 84.82 -> 84.89, status synced. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../upgradeTribeArrayLayoutForMapVersion.cpp | 22 ++++++++++++++----- status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index f6df49886d..d4e8fab2c6 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -2,6 +2,7 @@ #include "../TribesState.func.hpp" #include "string.h" +#include namespace OpenSHC { namespace Map { @@ -48,13 +49,22 @@ namespace Map { _Dst = _Dst + -1; psVar3 = psVar3 + 100; /* - A memset, not a loop. The original clears these 200 bytes with - `mov ecx, 0x32` and a single `rep stosd` - fifty dwords - which is what - MSVC inlines a constant-size memset to. The decompiler's two-shorts-per- - iteration loop compiles to eight instructions instead: the pair of - stores, the cursor bump, the counter decrement and the branch. + The original clears these 200 bytes with `mov ecx, 0x32` and a single + `rep stosd` - fifty dwords. Three spellings, measured: + + the decompiler's two-shorts-per-iteration loop 46.5% + memset(psVar3, 0, 200) 49.4% + __stosd((unsigned long*)psVar3, 0, 50) 57.5% + + The loop compiles to eight instructions. `memset` is better but this + build does not inline it - it emits `push 0xc8; push 0; push ebx; call + memset; add esp, 0x18`, so the original's `rep stosd` is an inlined + memset and /Oi is not on here. The intrinsic emits the instruction + directly, which is why it is used in preference to the more idiomatic + memset: it is the only one of the three that produces what the original + has. */ - memset(psVar3, 0, (size_t)((int)(200))); + __stosd((unsigned long*)psVar3, 0, 50); } while (-1 < iVar2); return; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 7fedccb6d6..755802c7c9 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46871,7 +46871,7 @@ SHC_3BB0A8C1_0x00523E30 | 0.0% | Pending SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523EF0 | 46.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523EF0 | 57.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523F70 | 74.7% | Reimplemented, asm differs @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 58.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 59.0% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From a995c4b2972f70a74eb27c9c1ec443f729fea5cf Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 08:53:57 +0200 Subject: [PATCH 117/202] fix: an always-true retry test in giveTribeAnInstruction walked off a 16-entry table `if (unitUID | unitUID != 0x10)` parses as `unitUID | (unitUID != 0x10)`, which is 0x10 when the counter reaches 16 and 1 when it is 0 - true in every case. So the goto was unconditional, the three lines below it were dead, and the retry loop kept indexing the 16-entry table at 0xb4c7f0 that LAB_00529da0 reads with that counter (`movsx edi, word ptr [eax*4 + 0xb4c7f0]`) past its end, instead of falling back to the unit's own position after sixteen attempts. Now `unitUID != 0x10`. Two more instances of the same decompiler artifact in that function are harmless - `unitUID | unitUID != 0` is only ever truthy when unitUID is non-zero, so it is that test written three ways - but they are simplified because all three raised C4554 and the warning is what found the real one. giveTribeAnInstruction 59.0 -> 60.4%; the namespace now builds with no warnings at all. Also upgradeTribeArrayLayoutForMapVersion 57.5 -> 62.5%, which puts its raw and normalized scores level and so leaves it with no call-target differences at all. The original calls the game's own statically-linked memmove at 0x00583760 (Ghidra names it as the function's only callee); we were calling the CRT's. OS.func.hpp had resolvers for _memset and _memcpy but none for memmove, so one is added following that file's pattern - declaration in OS.hpp, stub in OS/OS.cpp, resolver in address order - and the two call sites now go through MACRO_CALL(OS_Func::_memmove), as the rest of the codebase does for CRT functions the game carries its own copy of. 46.5 -> 62.5% for that function across the session. Namespace average 84.89 -> 84.95. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 22 ++++++++++++++++--- .../upgradeTribeArrayLayoutForMapVersion.cpp | 8 ++++--- src/OpenSHC/OS.func.hpp | 4 ++++ src/OpenSHC/OS.hpp | 2 ++ src/OpenSHC/OS/OS.cpp | 3 +++ status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index f4aca5919c..a587fe9893 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -1722,7 +1722,12 @@ namespace Map { + (int)_x]; unitUID = MACRO_CALL_MEMBER( Map::Buildings::BuildingsState_Func::getRequiredEngineersCount, DAT_BuildingsState::ptr)(iVar11); - if (((unitUID | unitUID != 0) + /* + `unitUID | unitUID != 0` is only ever truthy when unitUID is non-zero, + so this is that test written three ways at once - the same decompiler + artifact as the counter below, but harmless here. + */ + if ((unitUID != 0 || (DAT_BuildingsState::instance.buildings[iVar11].buildingType == Map::Buildings::BT_OILSMELTER)) && 0 < this->tribes[tribeID].size) { @@ -1809,7 +1814,7 @@ namespace Map { if (DAT_UnitsState::instance.units[id].uid == unitUID && (unitUID = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(id), - unitUID | unitUID != 0) + unitUID != 0) && (iVar12 = 0, 0 < this->tribes[tribeID].size)) { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( @@ -1939,7 +1944,18 @@ namespace Map { if (DAT_PathFindingState::instance.ALG_ResultTile != 0) break; unitUID = unitUID + 1; - if (unitUID | unitUID != 0x10) + /* + `unitUID | unitUID != 0x10` is always true - it + parses as `unitUID | (unitUID != 0x10)`, which is + 0x10 when the counter reaches 16 and 1 when it is 0. + So the goto was unconditional and the three lines + below it were dead, leaving this retry loop to walk + off the end of the 16-entry table at 0xb4c7f0 that + LAB_00529da0 indexes with it (`movsx edi, word ptr + [eax*4 + 0xb4c7f0]`) until the path search happened + to succeed. The counter test is what was meant. + */ + if (unitUID != 0x10) goto LAB_00529da0; sVar9 = DAT_UnitsState::instance.units[iVar11].x; sVar8 = DAT_UnitsState::instance.units[iVar11].y; diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index d4e8fab2c6..9a71c80d06 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -1,6 +1,8 @@ #include "../../../Map.func.hpp" #include "../TribesState.func.hpp" +#include "OpenSHC/OS.func.hpp" + #include "string.h" #include @@ -12,7 +14,6 @@ namespace Map { void TribesState::upgradeTribeArrayLayoutForMapVersion( PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) { - int iVar1; Tribe* _Dst; int iVar2; short* _Src; @@ -40,10 +41,11 @@ namespace Map { _Src = this->tribes[944].unitSelectionBitMasked + 0x7a; do { if (_Dst != (Tribe*)_Src) { - memmove(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); + MACRO_CALL(OS_Func::_memmove)(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); } psVar3 = _Dst->unitSelectionBitMasked; - memmove(&_Dst->field34_0x1c8, (void*)((int)(_Src + 0x80)), (size_t)((int)(364))); + MACRO_CALL(OS_Func::_memmove)( + &_Dst->field34_0x1c8, (void*)((int)(_Src + 0x80)), (size_t)((int)(364))); iVar2 = iVar2 + -1; _Src = _Src + -0x136; _Dst = _Dst + -1; diff --git a/src/OpenSHC/OS.func.hpp b/src/OpenSHC/OS.func.hpp index 4523a08354..2f85a60ee8 100644 --- a/src/OpenSHC/OS.func.hpp +++ b/src/OpenSHC/OS.func.hpp @@ -175,6 +175,10 @@ namespace OS_Func { Address::SHC_3BB0A8C1_0x005835BB, &OpenSHC::OS::__strnicmp) __strnicmp; + MACRO_FUNCTION_RESOLVER(void*(__cdecl*)(void* _Dst, void* _Src, size_t _Size), REIMPLEMENTED_CRT, + Address::SHC_3BB0A8C1_0x00583760, &OpenSHC::OS::_memmove) + _memmove; + MACRO_FUNCTION_RESOLVER( void(__cdecl*)(int _Code), REIMPLEMENTED_CRT, Address::SHC_3BB0A8C1_0x00583D55, &OpenSHC::OS::_exit) _exit; diff --git a/src/OpenSHC/OS.hpp b/src/OpenSHC/OS.hpp index b0cad4b807..94d557c5c9 100644 --- a/src/OpenSHC/OS.hpp +++ b/src/OpenSHC/OS.hpp @@ -101,6 +101,8 @@ namespace OS { int __cdecl __strnicmp(char const* _Str1, char const* _Str2, size_t _MaxCount); + void* __cdecl _memmove(void* _Dst, void* _Src, size_t _Size); + void __cdecl _exit(int _Code); } // namespace OS diff --git a/src/OpenSHC/OS/OS.cpp b/src/OpenSHC/OS/OS.cpp index 686831bf59..b17b569694 100644 --- a/src/OpenSHC/OS/OS.cpp +++ b/src/OpenSHC/OS/OS.cpp @@ -143,6 +143,9 @@ namespace OS { return _strnicmp(_Str1, _Str2, _MaxCount); } + // STUB: STRONGHOLDCRUSADER 0x00583760 + void* _memmove(void* _Dst, void* _Src, size_t _Size) { return memmove(_Dst, _Src, _Size); } + // STUB: STRONGHOLDCRUSADER 0x00583D55 void _exit(int _Code) { exit(_Code); } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 755802c7c9..58f3a4f119 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46871,7 +46871,7 @@ SHC_3BB0A8C1_0x00523E30 | 0.0% | Pending SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523EF0 | 57.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523EF0 | 62.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523F70 | 74.7% | Reimplemented, asm differs @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 59.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 60.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From 1a49468eeeadd2903d2b60350e0b5459e5185fcc Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 09:03:32 +0200 Subject: [PATCH 118/202] measure: matching instruction counts does not mean matching code A mnemonic histogram across the namespace turned up one very large outlier: giveTribeAnInstruction has 119 `imul` where the original has 52, and splitting them by multiplier shows the whole excess is the tribe stride - `imul .., 0x334` once in the original against 63 in ours, one per `this->tribes[tribeID].`. Caching that base as a shifted `TribesState*`, the form that keeps field displacements identical to the subscript's, fixes it completely: imul 0x334 63 -> 1 (original: 1) instructions 2628 -> 2514 (original: 2501) quick_diff 0.4296 -> 0.4622 reccmp scores it 60.4% -> 55.6%, which confirms the -4.8 recorded for this function in the base-caching tally. What is new is that it is not register naming: the register-blind ratio stays flat at 66.6% -> 66.4%. So the instruction sequence is no closer, with the counts and the histogram both near-exact. Three independent measures of closeness all improved and the alignment did not move. That is worth having written down next to the function, because the instruction-count and histogram diagnostics are exactly the sort of thing that invites another attempt. Also measured and reverted: hoisting `&units[_unitID_0x04]` into a `Unit*` across its 21 contiguous accesses, on the theory that the unit stride was being recomputed too. 60.4% -> 57.4%, and the imul count did not change at all - MSVC was already common- subexpressioning those, so there was nothing there to win. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index a587fe9893..43577af5b1 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -47,6 +47,27 @@ namespace Map { 21:57:43.216000 */ /* + The subscript stays, and this is the measurement that says why nothing else here + is worth trying from the instruction counts. + + The original has exactly one `imul .., 0x334` in the whole function; we have 63, + because every `this->tribes[tribeID].` recomputes the stride. Caching the base as + a shifted `TribesState*` - which keeps the field displacements identical to the + subscript's, unlike a `Tribe&` - fixes that completely and brings three separate + measures of closeness almost exactly into line: + + imul 0x334 63 -> 1 (original: 1) + instructions 2628 -> 2514 (original: 2501) + quick_diff 0.4296 -> 0.4622 + + reccmp scores it 60.4% -> 55.6%. And it is not register naming: the + register-blind ratio diff_regalloc.py computes goes 66.6% -> 66.4%, flat. So the + instruction sequence is no closer at all, even with the counts and the mnemonic + histogram matching. Three independent "looks closer" signals can all improve + while the actual alignment does not; only reccmp decides, and the 21-site pointer + hoist on _unitID_0x04 that was tried alongside this did not even change the imul + count - MSVC was already common-subexpressioning those. + All thirteen switch dispatches in this function have been checked against their tables, so this does not need redoing. They are at 0x52a1e8, 0x52a258, 0x52a2b8, 0x52a308, 0x52a368, 0x52a3b8, 0x52a3fc, 0x52a464, 0x52a490, 0x52a4e4, 0x52a550, From 681e494171eb9daf2df52c6a0a3e49876e83c017 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 13:30:09 +0200 Subject: [PATCH 119/202] reimplement: getMajorityArcherTypeEuropeanOrArabian 69.6 -> 88.2%, declaration order The size read goes after the three zero-initialised locals, not before them. MSVC assigns stack slots in declaration order, and with the size first our two counters landed in 0x10 and 0x18 - the latter being the dead parameter's slot, reused because only one proper local slot got reserved (`push ecx`) - where the original has them adjacent at 0x10 and 0x14 behind a `sub esp, 8`. perm_decls.py found it: of the 24 orders of that four-declaration run, this one scores 0.9892 against the as-written 0.8172, and the next best is 0.8602. Confirmed with reccmp at 69.6 -> 88.2. Worth noting because my own notes had declaration order written off in this namespace - "disproved by slot-count multisets, not measured". That was wrong, and wrong in the way that matters: it was reasoned rather than measured, and the tool for measuring it had been sitting unused. diff_slots.py names the candidates (orig 2 slots at 0x10 and 0x14, ours 3 at 0xc, 0x10 and 0x18). Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp b/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp index 4f209b1fc2..ea10ef0e73 100644 --- a/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getMajorityArcherTypeEuropeanOrArabian.cpp @@ -17,10 +17,10 @@ namespace Map { UnitType TribesState::getMajorityArcherTypeEuropeanOrArabian(int selectionID) { int _unitID; - int iVar2 = (int)this->tribes[selectionID].size; int _unitSelectionIndex = 0; int _europeanArcherCount = 0; int _arabArcherCount = 0; + int iVar2 = (int)this->tribes[selectionID].size; if (0 < iVar2) { do { _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( From 874639bb3e5057475ffb99ee5fe73ac943f45c08 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 13:32:26 +0200 Subject: [PATCH 120/202] reimplement: updateTribeRallyFlags 74.7 -> 100%, getTribeAliveStatus 68.8 -> 85.9% The same thing in both, and the same as getMajorityArcherTypeEuropeanOrArabian: the declaration that reads the tribe size belongs after the zero-initialised locals, not before them. MSVC assigns stack slots in declaration order, and with the size first the counters end up one slot apart with the dead parameter's slot reused for one of them, where the original has them adjacent. perm_decls.py found both. updateTribeRallyFlags scores a flat 1.0000 on quick_diff with the size pointer last against 0.7619 as written, and reccmp agrees - full match. getTribeAliveStatus 0.7344 -> 0.8750, reccmp 68.8 -> 85.9. Three for three on that shape. applyUnitTopSpeedDelayBasedOnTribeSize is the fourth function with a size-first declaration and the only one where no order helps - it has just two declarations, so there is one alternative and it is worse. Searched for more instances and there are none left: those four are the whole set. Namespace average 85.11 -> 85.49, 61 of 113 at a full match. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp | 2 +- src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp | 2 +- status/addresses-SHC-3BB0A8C1.txt | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp index 8028fb7da3..ea519e51db 100644 --- a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp @@ -19,10 +19,10 @@ namespace Map { int TribesState::getTribeAliveStatus(int tribeID) { int _unit; - int _size = (int)this->tribes[tribeID].size; int _index = 0; int _other = 0; int _alive = 0; + int _size = (int)this->tribes[tribeID].size; if (0 < _size) { do { _unit = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp index e1ea0715cc..7eb6df9ba8 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp @@ -24,9 +24,9 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00523F70 undefined4 TribesState::updateTribeRallyFlags(int param_1) { - short* const _size = &this->tribes[param_1].size; int unitSelectionIndex = 0; undefined4 _anyRallying = 0; + short* const _size = &this->tribes[param_1].size; if (0 < *_size) { do { int const _unitID diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 58f3a4f119..032d796048 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46873,7 +46873,7 @@ SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523EF0 | 62.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523F70 | 74.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523F70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00524000 | 100.0% | Reimplemented @@ -46887,7 +46887,7 @@ SHC_3BB0A8C1_0x005241C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00524230 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005242B0 | 69.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005242B0 | 88.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524340 | 100.0% | Reimplemented @@ -46905,7 +46905,7 @@ SHC_3BB0A8C1_0x00524A30 | 0.0% | Pending SHC_3BB0A8C1_0x00524B30 | 80.7% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524BF0 | 68.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524BF0 | 85.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524CA0 | 18.8% | Reimplemented, asm differs From 95d6c8d8cd70de7931c667b72023aeba0ffcf8da Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 13:36:40 +0200 Subject: [PATCH 121/202] docs: declare_at_use.py said declaration position does not affect codegen It does, and perm_decls.py in the same directory already said so. Three functions in Map/Units/TribesState turned on nothing but the order of their leading declarations: getMajorityArcherTypeEuropeanOrArabian 69.6 -> 88.2, getTribeAliveStatus 68.8 -> 85.9 and updateTribeRallyFlags 74.7 -> 100, each by declaring the local that reads the tribe size after the zero-initialised counters rather than before them. MSVC assigns stack slots in declaration order, so moving one declaration past another moves both locals between slots and usually between registers too. The old wording - "this is a readability pass; run it before measuring, not instead of" - would send the next person straight past a seam worth 61 points across those three. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- tools/reimplementation-helper/batch/declare_at_use.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/reimplementation-helper/batch/declare_at_use.py b/tools/reimplementation-helper/batch/declare_at_use.py index e64f6c68f0..4205732eb9 100644 --- a/tools/reimplementation-helper/batch/declare_at_use.py +++ b/tools/reimplementation-helper/batch/declare_at_use.py @@ -9,8 +9,14 @@ tracking brace depth from that assignment to the last use. Anything it cannot prove is left alone and reported, so the output doubles as a list of what still needs a hand. -Declaration position does not change code generation, so this is a readability pass; run -it before measuring, not instead of. +Declaration position *does* change code generation, so measure after running this. MSVC +assigns stack slots in declaration order - see perm_decls.py - and moving a declaration +past another one moves both locals between slots, which usually moves them between +registers too. Three functions in Map/Units/TribesState turned on nothing else: +getMajorityArcherTypeEuropeanOrArabian 69.6% -> 88.2%, getTribeAliveStatus +68.6% -> 85.9% and updateTribeRallyFlags 74.7% -> 100%, all three just by declaring the +local that reads the tribe size after the zero-initialised counters instead of before +them. This docstring used to say the opposite; it was wrong. usage: declare_at_use.py [path-filter] From 40392119fc2d56f7566289fd447e5cfef3be06f8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 13:43:14 +0200 Subject: [PATCH 122/202] measure: re-confirmed the shifted base on updateRabbitTribeBehavior Its three near-identical siblings all use the plain subscript and two of them score higher (updateCamelTribeBehavior 71.9, updateLionWolfTribeBehavior 72.7, processDeerMoving 63.3 against this one's 63.0), so with the namespace twenty-odd changes further on than when the +1.4 was recorded it was worth re-measuring. Reverting the base here to the subscript loses again: 63.0% -> 61.1%. The entry stands, and the note now says it was re-checked rather than inherited. Also searched the rest of the namespace for more declaration-order wins after the three that paid, and there are none among the cheap runs: commandUnitsToLocation, drawFlagsAndUnitDestinations, processDeerMoving, createTribeForPlayer, applyLadderDestructionToTribeUnits, isTribeUnitBlockedByOtherUnit, updateTribes, removeSelectedUnitsFromTheirCurrentTribes, getUnitIDForIndexInTribe, playUnitMoveSpeech, moveUnitToBehaviorTarget, predictUnitInterceptPosition and moveTribeToIndexedNearbyTile all keep their as-written order as the best of every permutation. getMajoritySelectedUnitType's slot census matches the original exactly (one slot each, at 0x14c), and its remaining 6.9% is seven lines: a register choice, a two-position scheduling shift and a `ret` the original's extent does not include. Namespace average 85.49, 61 of 113 at a full match, status in sync. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/updateRabbitTribeBehavior.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 56df2b0e93..b5e7b5101f 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -35,6 +35,12 @@ namespace Map { namespace caching the base now stands at two clear wins (trimTribeToSize and createTribe, +11.8 each, the latter also fixing displacements), this +1.4, one flat, and five losses from -2.1 to -40.8. There is no predictor; measure. + + Re-measured once the namespace had moved on by twenty-odd other changes, + because the three near-identical siblings all use the plain subscript and + two of them score higher than this one (updateCamelTribeBehavior 71.9, + updateLionWolfTribeBehavior 72.7, processDeerMoving 63.3). Reverting the + base here to the subscript still loses: 63.0% -> 61.1%. The +1.4 stands. */ TribesState* const _tb = (TribesState*)((char*)this + tribeID * sizeof(Tribe)); short* psVar1; From 3be524db9fc00c900980ca5b9b8a4033a8cca260 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 17:20:51 +0200 Subject: [PATCH 123/202] reimplement: spawnUnitsForAITribe 78.0 -> 100%, the call belongs in both arms Every branch displacement in that function was out by exactly five bytes, and the cause was one instruction: the original pushes the behaviour constant straight from each arm - `push 0x14`, `jmp 0x2`, `push 0x11` - and shares the `push eax` and the call after them. Assigning a local and calling once makes MSVC materialise the value instead, `mov ecx, 0x14` / `mov ecx, 0x11` and a `push ecx`, which is five bytes more. That shape is what MSVC produces when it tail-merges two call sites, so the original wrote the call out in both branches. Writing it that way here gives a full match. Declaring the local at its use rather than at function scope was tried first and is flat - the `mov ecx, imm` is still there - so it is the two call sites that matter, not the scope. Recorded on the function. Also spawnDeerLionOrRabbit 73.8 -> 74.1%: the RNG byte is masked where it is read. The original does `and edx, 0x7f` immediately, before it reads the herd size and the coordinates; leaving the mask on the comparison forty lines down makes MSVC do it there. The value is only ever read masked - it is overwritten by the spawn result straight after - so it is the same program. Two attempts measured and reverted with the numbers recorded on the functions: addUnitToTribe, naming the word index so the division runs before the multiply as the original schedules it (flat at 86.7%, MSVC still wants EBP), and isTribeUnitBlockedByOtherUnit, hoisting a `Unit*` for its eight accesses to remove the `imul .., 0x490` the original does not repeat inside the loop (80.7% -> 74.3%; the pointer needs a home across the call and costs more than the multiply it saves). Namespace average 85.49 -> 85.69, 62 of 113 at a full match. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/addUnitToTribe.cpp | 6 +++++- .../isTribeUnitBlockedByOtherUnit.cpp | 9 ++++++++ .../TribesState/spawnDeerLionOrRabbit.cpp | 11 ++++++++-- .../TribesState/spawnUnitsForAITribe.cpp | 21 ++++++++++++++----- status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp index 70607ae76c..4b789884fb 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -22,7 +22,11 @@ namespace Map { The gap here is a register: the original builds the cursor in a scratch register (`mov edx, edi`, `imul edx, edx, 0x19a`, `add edx, eax`, `lea eax, [ecx + edx*2 + 0x60]`) where ours needs EBP and so a push and a pop, - and dropping the local was not enough to free one. + and dropping the local was not enough to free one. Naming the word index + instead, so the division runs before the multiply as it does in the original + (`cdq`, `and edx, 0xf`, `add eax, edx`, then `imul edx, edx, 0x19a`, then + `sar eax, 4`), is flat at 86.7% - MSVC still schedules the multiply first and + still wants EBP for it. */ psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); *psVar1 = *psVar1 | DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]; diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp index 757a472ee2..fff7f98d02 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp @@ -18,6 +18,15 @@ namespace Map { undefined4 TribesState::isTribeUnitBlockedByOtherUnit(int param_1) { int iVar2; + /* + The unit is re-indexed at every test, and that is cheaper than the + alternative. The original keeps its address in a register across the body - + `cmp word ptr [ecx + ..]` for the dying test with no multiply in front of + it - where ours emits `mov edx, eax; imul edx, edx, 0x490` again inside the + loop. Hoisting a `Unit*` for all eight accesses removes those, and measures + 80.7% -> 74.3%: the pointer needs a home across the call in the loop and + costs more than the multiply it saves. + */ int unitSelectionIndex = 0; int sVar1 = this->tribes[param_1].size; while (true) { diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index 68d9a56887..4c49201e42 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -55,7 +55,14 @@ namespace Map { _finalTargetUnitID = (int)_targetUnitID; } } - _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2; + /* + Masked here, not at the use. The original does `and edx, 0x7f` right after + reading the byte, before it reads the herd size and the coordinates; leaving + the mask on the comparison forty lines down makes MSVC do it there instead. + The value is only ever read masked - it is overwritten by the spawn result + straight afterwards - so this is the same program. + */ + _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2 & 0x7f; _terrainHeight = DAT_UnitsState::instance.units[_finalTargetUnitID].terrainOrClimbHeight; /* Both of these reads are `movzx` in the original, not `movsx`. @@ -83,7 +90,7 @@ namespace Map { } else { iVar3 = _tribeSize < _unknown ? 25 : 100; } - if (iVar3 <= (int)(_rng2 & 0x7f)) { + if (iVar3 <= (int)_rng2) { _rng2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( 0, 0, (int)(_unitX * 8), (int)(_unitY * 8), (int)(_terrainHeight), unitType); /* diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp index 9859ec97c8..2805e6f35e 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp @@ -19,7 +19,6 @@ namespace Map { undefined4 param_1, int param_2, int param_3, int param_4, UnitType param_5, int param_6, int param_7) { int unitID; - int aiUnitBehaviourType; if (((0 < param_2) || (0 < param_3)) && 0 < param_6) { /* The Y scaling is computed first: the original has `lea ebx, [edx*8]` for @@ -35,15 +34,27 @@ namespace Map { if (unitID) { DAT_UnitsState::instance.units[unitID].calculatedOwnerPlayerIndex = param_7; UnitTypeShort UVar1 = DAT_UnitsState::instance.units[unitID].unitType; + /* + The call is written out in both arms. The original pushes the + behaviour constant straight from each one - `push 0x14`, + `jmp 0x2`, `push 0x11` - and shares the `push eax` and the call + after them, which is what MSVC produces when it tail-merges two + call sites. Assigning a local and calling once makes it + materialise the value instead (`mov ecx, 0x14` / `mov ecx, 0x11` + and a `push ecx`), five bytes more, and that is what every + branch displacement in this function was out by. Declaring the + local at its use rather than at function scope does not change + it; the two call sites do. + */ if (UVar1 == Map::Units::UT_E_ARCHER || UVar1 == Map::Units::UT_E_XBOW || UVar1 == Map::Units::UT_A_ARCHER || UVar1 == Map::Units::UT_A_SLINGER || UVar1 == Map::Units::UT_A_HARCHER || UVar1 == Map::Units::UT_A_FIRETHROWER) { - aiUnitBehaviourType = 0x11; + MACRO_CALL_MEMBER(AI::AICState_Func::addUnitToItsTribe, DAT_AICState::ptr)( + unitID, 0x11); } else { - aiUnitBehaviourType = 0x14; + MACRO_CALL_MEMBER(AI::AICState_Func::addUnitToItsTribe, DAT_AICState::ptr)( + unitID, 0x14); } - MACRO_CALL_MEMBER(AI::AICState_Func::addUnitToItsTribe, DAT_AICState::ptr)( - unitID, aiUnitBehaviourType); } param_2 = param_2 + -1; } while (param_2); diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 032d796048..b5a08b2538 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46821,7 +46821,7 @@ SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522EF0 | 70.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522F70 | 78.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522F70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523030 | 98.3% | Reimplemented, asm differs @@ -46951,7 +46951,7 @@ SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525F20 | 73.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525F20 | 74.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x005260B0 | 100.0% | Reimplemented From 072a306d3af9ec52b0fad9c4bdf22d0703220f1b Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 17:35:05 +0200 Subject: [PATCH 124/202] reimplement: the constant-argument call belongs in each arm, +8.2 over two functions Following spawnUnitsForAITribe to its two siblings. Where the original pushes a constant argument straight from each branch and shares the rest of the call after them - MSVC tail-merging several call sites - the import had a local assigned the constant in each branch and one shared call, which makes MSVC materialise the value into a register first. playUnitSelectionSound 62.1 -> 65.4 (0x56 and 0x58, the ladderman arm) playUnitCombatSpeechForTarget 31.0 -> 35.9 (0xea, 0xeb and 0xec, the archer arm) Found by scanning for an orig-only `push ` facing an ours-only `mov , ` with the same constant, which is what the shape looks like in the diff. It named three functions; the third is giveTribeMoveInstruction, with 1, 2 and 4. What is left in playUnitSelectionSound is the order of the constant push against the `imul esi, esi, 0x490` that computes the unit address - the original pushes first - and its shared epilogue sitting early enough for the `param_1 < 3` return to reach it backwards. Both are MSVC's scheduling and placement. Namespace average 85.69 -> 85.76. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../playUnitCombatSpeechForTarget.cpp | 19 ++++++++++++------ .../TribesState/playUnitSelectionSound.cpp | 20 +++++++++++++------ status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index dfeba8915e..ac1286dc35 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -28,7 +28,6 @@ namespace Map { int _otherX; int _xDifference; int _otherY; - int sfxOffsetInArray; /* This is the selected-unit count the majority lookup writes back, not a pointer. The decompiler typed it `TribesState*` because the import assigned @@ -133,21 +132,29 @@ namespace Map { if (_ptrptrCurrentSelectionID != Map::Units::UT_A_HARCHER) { return; } + /* + The play is written out in each arm with its constant, as in the sibling + playUnitSelectionSound. The original pushes 0xea, 0xeb and 0xec directly + and shares the coordinate pushes and the call, which is MSVC tail-merging + three call sites; a named offset and one shared call makes it materialise + the value into a register first. + */ if (_selectedCount == 1) { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; - sfxOffsetInArray = 0xea; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + _otherX, _otherY, 0xea); } else if (_selectedCount < 5) { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; - sfxOffsetInArray = 0xeb; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + _otherX, _otherY, 0xeb); } else { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; - sfxOffsetInArray = 0xec; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + _otherX, _otherY, 0xec); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - _otherX, _otherY, sfxOffsetInArray); /* A second play, of 0x59, with the coordinates read again (0x521c0b, 0x521c12) - which is what the decompiler turned into an assignment of diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index e1ac5bd0de..76e90d9015 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -82,9 +82,18 @@ namespace Map { 0x59. A selection of two or three gets 0x57 as well, and one of four or more gets 0x58 instead of 0x56. */ - int _sfx; + /* + The play is written out in each arm with its constant. The original + pushes them directly - `cmp edi, 1; jne 0x4; push 0x56; jmp 0x35` for + the first and `push 0x58` for the four-or-more case - and shares the + coordinate pushes and the call after them, which is MSVC tail-merging + two call sites. An `_sfx` local and one shared call makes it + materialise the value into a register first instead. + */ if (param_1 == 1) { - _sfx = 0x56; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0x56); } else { if (param_1 == 2 || param_1 == 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( @@ -107,11 +116,10 @@ namespace Map { (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); return; } - _sfx = 0x58; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[iVar3].x, + (int)(DAT_UnitsState::instance.units[iVar3].y), 0x58); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), _sfx); MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b5a08b2538..b99fd60638 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46395,7 +46395,7 @@ SHC_3BB0A8C1_0x00521690 | 0.0% | Pending SHC_3BB0A8C1_0x00521720 | 93.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 62.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005217E0 | 65.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 31.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 35.9% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending From 2a3fe9a47bdf9548082fcba995e93c00b562af93 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 17:51:41 +0200 Subject: [PATCH 125/202] reimplement: playUnitCombatSpeechForTarget 31.0 -> 40.6%, two more arms of call sites The knight arm's `_otherX = 0x56` / `goto` back into the first play is the decompiler's rendering of two call sites: the original pushes 0x56 and 0x58 directly and shares the coordinate pushes and the call. Writing both out, and the three archer-arm plays with 0xea/0xeb/0xec before them, takes that function from 31.0 to 40.6. Every path still reaches the 0x59 play, as it did through the goto. That is five functions for this pattern now - spawnUnitsForAITribe to a full match, playUnitSelectionSound +3.3, this one +9.6 across two arms - and one that does not take. giveTribeMoveInstruction has the same shape six times over: the original reaches the modulo search through six call sites pushing the constant and jumping back to a shared tail (`push ebp; push edi; push 2; jmp -0x19b`, and the same with 4 and 1) where the import assigns `_modulo` in six places and calls once. Writing the six out moves everything except the score: instructions 726 -> 721 (original: 720) normalized 22.6 -> 29.4 register-blind 36.7 -> 46.1 raw 30.0 -> 28.9 The cost is the frame: `_modulo` is one of the slots, and without it MSVC drops to `sub esp, 0x2c` where the original and the import both have `sub esp, 0x34`, taking the `mov dword ptr [esp + 0x38], 0x1f4` with it. So the instruction sequence comes within one instruction of the original while the frame stops matching, and raw follows the frame. The `_searchBudget` move that used to shift both figures on its own now changes nothing at all on top of this. Reverted for the raw score, with the whole picture written onto the function - it supersedes the vaguer "needs the allocation understood" note that was there. The shape is settled: that function wants the six call sites and a 0x34 frame together, which needs two locals the decompiler never named. Namespace average 85.76 -> 85.80, 62 of 113 at a full match. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeMoveInstruction.cpp | 25 +++++++++++++++++++ .../playUnitCombatSpeechForTarget.cpp | 16 ++++++++---- status/addresses-SHC-3BB0A8C1.txt | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index d1c66b4d89..19e83b9ce5 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -45,6 +45,31 @@ namespace Map { so the frame only matches with both moves. Whatever is costing the raw match is downstream of the stack displacements those moves shift, so this needs the allocation understood rather than another declaration permutation. + + The strongest evidence for that, and the closest this function has come: the + original reaches the modulo search through six separate call sites, pushing the + constant from each and jumping back to a shared tail - `push ebp; push edi; + push 2; jmp -0x19b` and the same with 4 and 1 - where the import assigns + `_modulo` in six places and calls once. Writing the six call sites out is the + change that took spawnUnitsForAITribe from 78% to a full match, and here it + moves everything except the score: + + instructions 726 -> 721 (original: 720) + normalized 22.6 -> 29.4 + register-blind 36.7 -> 46.1 + raw 30.0 -> 28.9 + + What it costs is the frame. `_modulo` is one of the slots, and without it MSVC + drops to `sub esp, 0x2c` where the original - and the import - have `sub esp, + 0x34`, and the `mov dword ptr [esp + 0x38], 0x1f4` goes with it. So the + instruction sequence gets within one instruction of the original while the frame + stops matching, and raw follows the frame. Adding the `_searchBudget` move on top + changes nothing at all now (identical numbers), where on its own it used to move + both figures. + + Reverted, because raw is what the status file tracks. But the shape is not in + doubt any more: this function wants the six call sites and a 0x34 frame at the + same time, and that needs two locals the decompiler did not name. */ // FUNCTION: STRONGHOLDCRUSADER 0x005263A0 undefined4 TribesState::giveTribeMoveInstruction( diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index ac1286dc35..a12be48634 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -165,12 +165,17 @@ namespace Map { (int)DAT_UnitsState::instance.units[_otherUnitID].y, 0x59); return; case Map::Units::UT_E_KNIGHT: + /* + The same two call sites written out, which is what the decompiler's + `_otherX = 0x56` / `goto` back into the first one was: the original + pushes 0x56 and 0x58 directly and shares the coordinate pushes and the + call. Every path still reaches the 0x59 play below, as it did through + the goto. + */ if (_selectedCount == 1) { - _otherX = 0x56; - LAB_00521c6f: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x56); } else { if ((_selectedCount == 2) || (_selectedCount == 3)) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( @@ -178,8 +183,9 @@ namespace Map { (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x57); } if (3 < _selectedCount_2) { - _otherX = 0x58; - goto LAB_00521c6f; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + (int)DAT_UnitsState::instance.units[_otherUnitID].x, + (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x58); } } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b99fd60638..e81defab35 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 35.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 40.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending From 8938eb16cc81c8c96d04678e1498d1ac087f22e5 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 18:56:55 +0200 Subject: [PATCH 126/202] reimplement: createTribeForPlayer 75.2 -> 86.2%, the loop is top-tested Read the original in Ghidra rather than inferring from the diff, and its empty-slot search is: lea eax, [eax + ecx + 0x40] ; the cursor, taken once L:cmp word ptr [eax], bx ; tested through it je sub edi, 8 sub eax, 0x19a0 cmp edi, ebx jg L The back-edge returns to the test, so the test is at the top and the cursor is the only way the field is ever read. A `while (*psVar1 != 0)` does not compile to that: MSVC rotates it, peeling the first test - and doing that one through two-register addressing, `cmp word ptr [eax + ecx + 0x40], bx`, before it has taken the cursor at all - then bottom-tests the rest. Writing the test at the top with an explicit jump back gives the original's shape. Four other functions were read in Ghidra in the same pass and left alone, because the disassembly shows the remaining gap is register allocation or frame layout rather than anything the source decides: snapshotSelectionTribeAndComputeStance (95.8) - same structure, eax and ebp swapped; the original zeroes ebp for the field store then esi/ecx/eax for the three counters, we use eax then ebp. commandUnitsToLocation (42.3) - the original keeps the tribeID*0x334 product in a slot and addresses `[eax + edi + 0x5c]`, and materialises 1 in ebx so the loop can increment by it; we cache the sum and store the flag as an immediate. applyUnitTopSpeedDelayBasedOnTribeSize (48.7) - one local slot we do not have (`push ecx`), plus `test ax, ax` where we emit `cmp ax, si` against the zeroed index. moveUnitToBehaviorTarget (33.3) - the original saves four registers and keeps three zeroed counters in them with one in a slot; we save three and spill three. Same count of zeros either way. Namespace average 85.80 -> 85.90. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/createTribeForPlayer.cpp | 26 ++++++++++++++++--- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index 42bb68977c..cc19452895 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -46,13 +46,33 @@ namespace Map { /* find an empty tribe */ + /* + Top-tested, with the back-edge returning to the test, which is what the + original has at 0x522801: + + lea eax, [eax + ecx + 0x40] ; the cursor, taken once + L:cmp word ptr [eax], bx ; tested through it + je + sub edi, 8 + sub eax, 0x19a0 + cmp edi, ebx + jg L + + A `while (*psVar1 != 0)` gets rotated: MSVC peels the first test, and + does that one through two-register addressing (`cmp word ptr [eax + ecx + + 0x40], bx`) before taking the cursor at all, then bottom-tests the + rest. Writing the test at the top with an explicit jump back keeps the + cursor as the only way the field is read. + */ psVar1 = &this->tribes[_selectionID].tribeState; - while (*psVar1 != 0) { + LAB_00522801: + if (*psVar1 != 0) { _selectionID = _selectionID + -8; psVar1 = psVar1 - 0xcd0; - if (_selectionID <= 0) { - return 0; + if (0 < _selectionID) { + goto LAB_00522801; } + return 0; } /* wipe the tribe diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e81defab35..672965e5dc 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46797,7 +46797,7 @@ SHC_3BB0A8C1_0x00522630 | 79.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522720 | 62.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005227E0 | 75.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005227E0 | 86.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522890 | 79.0% | Reimplemented, asm differs From 4bffc0aaa1e870a2ad5992e99b30ddd55f260369 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:01:42 +0200 Subject: [PATCH 127/202] measure: the loop-rotation fix needs the original to strength-reduce too getAvailableTribeID is createTribeForPlayer's twin - both walk the tribe array backwards looking for a free slot, both with two `while (tribeState != 0 || time != 0)` loops - so the +11.0 that top-testing bought on one should have bought something here. It is flat at 62.3%. Read in Ghidra, the two originals differ in one way that matters. createTribeForPlayer takes its cursor once and walks it (`lea eax, [eax + ecx + 0x40]` then `sub eax, 0x19a0`), so our only problem there was the rotation MSVC applies to a `while`: peeling the first test and doing that one through two-register addressing. getAvailableTribeID's original recomputes the address every iteration instead - `mov edx, eax; imul edx, edx, 0x334; add edx, ecx` at the top of both 0x00522733 and 0x00522790 - and does not strength-reduce at all. Ours hoists `lea edx, [ecx + 0xfa0de]` and walks it with `sub edx, 0x334`. Writing the test at the top fixes the rotation and MSVC still hoists, so the reduction is the whole gap and the source cannot reach it. Reverted, with both halves of the comparison recorded on the function. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/getAvailableTribeID.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index 8adb1aa94a..670b5dd9c3 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -27,6 +27,24 @@ namespace Map { Stored once before the loop (0x52272e) and then only after each decrement; the decompiler's repeat of the store after the loop is not in the instruction stream. + + Both of these loops stay as `while`. The original recomputes the address + every iteration and does not strength-reduce them (0x00522733 and + 0x00522790): + + L:mov edx, eax; imul edx, edx, 0x334; add edx, ecx + cmp word ptr [edx + 0x40], 0 ... jg L + + where ours hoists `lea edx, [ecx + 0xfa0de]` and walks it with + `sub edx, 0x334`. Rewriting both as a top test with an explicit jump + back - the change that took the twin createTribeForPlayer from 75.2% to + 86.2% - fixes the rotation but not the reduction: MSVC still hoists the + address, and the result is flat at 62.3%. + + The difference between the two functions is that createTribeForPlayer's + original strength-reduces as well (`sub eax, 0x19a0`), so matching its + loop shape was enough there. Here the reduction is the whole gap and it + is not reachable from the source. */ DAT_CurrentTribeID::instance = _tribeID; while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { From 91223b22e52b779c945d17a6a74cdfe0cfa2ec7a Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:14:27 +0200 Subject: [PATCH 128/202] reimplement: spawnQueuedReinforcementWaves 59.6 -> 60.4%, the startGoods loop Read in Ghidra, its loop at 0x005276c0 is L:cmp dword ptr [ecx*4 + 0x117ce50], 0 jnz add ecx, 1 cmp ecx, 0x14 jl L jmp so the test is at the top with the back-edge returning to it, the index stays scaled into the addressing rather than being walked, and the bound is compared as `< 0x14` - not the `0x13 <` the decompiler wrote, which is the same test against a different constant. Written that way it gains 0.8, the same change that was worth 11.0 on createTribeForPlayer. Also tried in the same function and reverted: moving the tunneler-count read out of the do-while body to after it, which is where the original reads it (`jl` closing the loop, then `mov esi, [MapPropertiesState + 340]; test esi, esi; je`). It is the same value either way - assigned in the loop, read nowhere until the `while` below - but it measures 60.4% -> 54.8%, so being loop-carried is worth more than the position. The number is recorded next to the read. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../spawnQueuedReinforcementWaves.cpp | 35 +++++++++++++++++-- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp index 69285f47dc..47cb911988 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -239,6 +239,15 @@ namespace Map { } while (_newCount); } _siegeInfoSubIndex = _siegeInfoSubIndex + 1; + /* + The tunneler count stays inside the loop body, where the + decompiler put it. The original reads it after the back-edge - + `jl` closing the do-while, then `mov esi, [MapPropertiesState + + 340]; test esi, esi; je` - and moving it there is the same value, + since it is assigned in the loop and read nowhere until the + while below. It measures 60.4% -> 54.8%, so the read being + loop-carried is worth more here than its position. + */ iVar3 = DAT_MapPropertiesState::instance.SEC_Section1067.tunnelersCount; } while (_siegeInfoSubIndex < 20); while (iVar3) { @@ -336,10 +345,30 @@ namespace Map { } while (_playerID < 9); LAB_005276b5: iVar3 = 10; - while (DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] == 0) { + /* + Top-tested, with the back-edge returning to the test, and the + bound written as `< 0x14` because that is the constant the + original compares against. Its loop at 0x005276c0 is + + L:cmp dword ptr [ecx*4 + 0x117ce50], 0 + jnz + add ecx, 1 + cmp ecx, 0x14 + jl L + jmp + + so the index stays scaled into the addressing rather than being + walked as a pointer, and the test is at the top. A `while` gets + rotated: MSVC peels the first test and bottom-tests the rest. + Same change as createTribeForPlayer, worth +11.0 there. + */ + LAB_005276c0: + if (DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] == 0) { iVar3 = iVar3 + 1; - if (0x13 < iVar3) - goto LAB_005277c5; + if (iVar3 < 0x14) { + goto LAB_005276c0; + } + goto LAB_005277c5; } switch (iVar3) { case 10: diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 672965e5dc..8e8bc326fe 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46973,7 +46973,7 @@ SHC_3BB0A8C1_0x005270F0 | 47.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527440 | 59.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527440 | 60.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x005275A0 | 0.0% | Pending From 87f98d876788d56c2da9a6356c84472b53175d18 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:33:11 +0200 Subject: [PATCH 129/202] reimplement: predictUnitInterceptPosition 34.1 -> 40.1%, the bound is 1 not 2 Both speed bounds are tested against 1, sixteen bits wide. The original has `cmp dx, 1` and `cmp cx, 1`, each with a `jg` over a `mov reg, 1`, where `< 2` on an `int` local gives a 32-bit `jge` against 2. Same test, different instruction - and the second read is `movzx ecx, word ptr [esi + ecx + 0x222]`, so that local wants to be `short` too, not `int`. Same shape as the `uVar2 <= 1` that finished updateAnimalHerdBehaviorState: `x <= K` and `x < K+1` are the same test on integers and MSVC emits the constant it is given. Also closed out updateTribes, whose remaining 8.8% is now known to be unreachable. Its jump table at 0x52b9a0 sends tribeType 0x11 to 0x52b90a and 0x12 to 0x52b914, ten bytes apart, and both blocks are byte-for-byte the same work - `push eax; mov ecx, esi; call 0x523730; jmp 0x52b961`. The original carries two identical blocks its compiler did not tail-merge and ours merges them, which is the 0xa every branch displacement in that function is out by. No source change distinguishes two identical arms without changing what they do. And retried the arm inversion in playUnitSelectionSound now that the play is written out per arm, in case the surrounding blocks had changed enough: still a loss, 65.4% -> 60.2%. What is left there is how far MSVC tail-merges - the original shares one coordinate-read-and-call block between the 0x56 and 0x58 arms and duplicates only the push, where ours keeps its own copy of the reads. Namespace average 85.91 -> 85.96. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/playUnitSelectionSound.cpp | 15 ++++++++++++--- .../TribesState/predictUnitInterceptPosition.cpp | 15 ++++++++++++--- .../Map/Units/TribesState/updateTribes.cpp | 13 +++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 76e90d9015..90511fca25 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -106,9 +106,18 @@ namespace Map { its fall-through and the lone 0x59 play sits in the far block - ours is the other way round. Rewriting it as `if (param_1 >= 4) { _sfx = 0x58; } else { ...; return; }` measures - 62.1% -> 56.0%. Second loss for the inverted-arm fix after - giveTribeMoveInstruction, and both are in functions whose remaining - gap is dominated by where the shared epilogue lands. + 62.1% -> 56.0%. Retried after the play was written out in each arm, + in case the surrounding blocks had changed enough to matter: still a + loss, 65.4% -> 60.2%. Second and third losses for the inverted-arm + fix after giveTribeMoveInstruction, and all of them are in functions + whose remaining gap is dominated by where the shared epilogue lands. + + What is left here is how far MSVC tail-merges. The original shares + one coordinate-read-and-call block between the 0x56 and 0x58 arms, + duplicating only the `push`, and its 0x59 plays jump back to a second + shared block. Ours pushes the constants per arm correctly but keeps + its own copy of the coordinate reads, which is the two extra reads of + units[].x and units[].y the diff shows (orig 4 and 5, ours 6 and 7). */ if (param_1 < 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index b6112674f9..250b634d7a 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -35,14 +35,23 @@ namespace Map { } _targetTribeMinSpeed = this->tribes[DAT_UnitsState::instance.units[targetUnitID].tribeID].minimumMovementSpeed; + /* + Both bounds are tested against 1, not 2, and both speeds are `short`. The + original has `cmp dx, 1` and `cmp cx, 1` - sixteen bits wide, against the + literal 1, each with a `jg` over a `mov reg, 1` - where `< 2` on an `int` + gives a 32-bit `jge` against 2. Same test either way; one is the + instruction the original has. The second read is `movzx ecx, word ptr + [esi + ecx + 0x222]`, so it is a narrow load too. + */ int _targetTribeMinSpeedBounded = (int)_targetTribeMinSpeed; - if (_targetTribeMinSpeed < 2) { + if (_targetTribeMinSpeed <= 1) { _targetTribeMinSpeedBounded = 1; } - int _tribeMinSpeed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; + short _tribeMinSpeed + = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; int iVar3 = _targetTribeMinSpeedBounded * 4; int _tribeMinSpeedBounded = (int)_tribeMinSpeed; - if (_tribeMinSpeed < 2) { + if (_tribeMinSpeed <= 1) { _tribeMinSpeedBounded = 1; } int iVar4 = (int)DAT_UnitsState::instance.units[targetUnitID].x; diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp index 3aecf07358..7c26c9603a 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp @@ -104,6 +104,19 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateCamelTribeBehavior, this)( DAT_CurrentTribeID::instance); break; + /* + These two arms are the whole of this function's remaining gap, and it + is not reachable. The original's jump table at 0x52b9a0 sends + tribeType 0x11 to 0x52b90a and 0x12 to 0x52b914, ten bytes apart, and + both blocks are byte-for-byte the same work: + + push eax; mov ecx, esi; call 0x523730; jmp 0x52b961 + + So the original carries two identical blocks that its compiler did not + tail-merge, and ours merges them - which is the 0xa every branch + displacement in this function is out by. Nothing in the source + distinguishes two identical arms without changing what they do. + */ case ((AITribeType)0x11): MACRO_CALL_MEMBER(Map::Units::TribesState_Func::markTribeAsAnimalTribe, this)( DAT_CurrentTribeID::instance); diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 8e8bc326fe..e75185bcdf 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46389,7 +46389,7 @@ SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521280 | 33.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521500 | 34.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521500 | 40.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending From 60049bf206e2d5aac22786ce9e9208f8c6b26bc4 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:39:10 +0200 Subject: [PATCH 130/202] reimplement: updateCamelTribeBehavior 71.9 -> 73.1, the constant is 0x64 not 0x65 Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/updateCamelTribeBehavior.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index d5256f271b..84ee8a08c9 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -126,7 +126,13 @@ namespace Map { } } else if (SVar2 == Map::Units::STBT_1) { sVar5 = 800; - if (this->tribes[param_1].unknownCounter01 < 0x65) { + /* + Written `<= 0x64`, which is the literal the original compares - + `cmp word ptr [esi + ..], 100` with a `jg` - where `< 0x65` gives + `cmp .., 101` and a `jge`. The same test; MSVC emits the constant + it is handed. + */ + if (this->tribes[param_1].unknownCounter01 <= 0x64) { sVar5 = 10; } psVar1 = (short*)&this->tribes[param_1].unknownAttackRelatedUpdateCounter; From 2540290f913abc66bba25a198ba25c0fce1ff557 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:40:12 +0200 Subject: [PATCH 131/202] reimplement: drawFlagsAndUnitDestinations 55.9 -> 57.6, the constant is 8 not 7 Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/drawFlagsAndUnitDestinations.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index 04eba14fb7..7ad496f1e1 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -110,7 +110,14 @@ namespace Map { DAT_ViewportRenderState::ptr)( IO::Graphics::GID_CURSORS, (int)(0x6a - sVar1), 10, 6, tile, iVar4 << 0x10 | 2); iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; - if (7 < iVar4) { + /* + Negated rather than written `7 < iVar4`: the original has + `cmp eax, 8` with a `jl` jumping over the subtraction, and + MSVC emits the constant it is handed, so the greater-than + spelling gives `cmp eax, 7; jle` instead. Same test, and + writing it this way is worth 1.7% here (55.9 -> 57.6). + */ + if (!(iVar4 < 8)) { iVar4 = 0xf - iVar4; } MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, @@ -124,7 +131,7 @@ namespace Map { && UVar2 != Map::Units::UIT_ATTACK_LAND && UVar2 != Map::Units::UIT_MAN_SIEGE_EQUIPMENT) { iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; - if (7 < iVar4) { + if (!(iVar4 < 8)) { iVar4 = 0xf - iVar4; } MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, From ed3bc6b202ce10bc9cea0e92cca51ce84a0c887e Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:40:23 +0200 Subject: [PATCH 132/202] measure: sync the status file for the last three wins Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e75185bcdf..ce70e64158 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46861,7 +46861,7 @@ SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523920 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523A30 | 55.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523A30 | 57.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523B00 | 0.0% | Pending @@ -46973,7 +46973,7 @@ SHC_3BB0A8C1_0x005270F0 | 47.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527440 | 60.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527440 | 59.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x005275A0 | 0.0% | Pending @@ -49147,7 +49147,7 @@ SHC_3BB0A8C1_0x0052B110 | 72.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B630 | 71.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B630 | 73.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs From 8afb5430190ccf5556fcf941b745c5abc44fd8a6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:42:20 +0200 Subject: [PATCH 133/202] measure: playUnitSelectionSound's two merge-candidate arms cost 5.5 when adjacent Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/playUnitSelectionSound.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 90511fca25..bd54de5e8e 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -118,6 +118,14 @@ namespace Map { shared block. Ours pushes the constants per arm correctly but keeps its own copy of the coordinate reads, which is the two extra reads of units[].x and units[].y the diff shows (orig 4 and 5, ours 6 and 7). + + Tried making those two arms adjacent so MSVC would have the chance: + the 0x57 play lifted out ahead as `if (param_1 == 2 || param_1 == 3) + { play(0x57); play(0x59); return; }`, leaving `if (param_1 == 1) + play(0x56); else play(0x58);` as the whole of its if/else. Measured + 65.4% -> 59.9%. MSVC merged nothing extra and the rearranged tests + cost more than the duplicated reads do, so the shape here stays as + the original's test order has it. */ if (param_1 < 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( From b664e880e6a95912a15f35e40a25a82ba1acfa28 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 19:47:47 +0200 Subject: [PATCH 134/202] measure: three flat results - the ID local, the zero register, the coordinate widths Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/getSpecificUnitFromTribe.cpp | 10 ++++++++++ .../TribesState/isTribePathToDestinationClear.cpp | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp index 7af8009c80..1b79b1a1d5 100644 --- a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp @@ -52,6 +52,16 @@ namespace Map { iVar2 = iVar2 + 1; } while (iVar2 < 0x10); } + /* + MSVC keeps a zero of its own in a register here - `xor edi, edi` up + front, which costs it `ebx` and turns the function's immediate tests + into register compares (`cmp dx, di` where the original has + `test dx, dx`). The original materialises its zero into the counter + and stores from there. Writing the store below as + `DAT_00ee0fb4::instance = iVar2` to say the same thing measures + 55.1% -> 55.1%: the dedicated zero register survives it, so this is + the register allocator rather than the spelling. + */ iVar2 = 0; iVar1 = iVar1 + 1; psVar3 = psVar3 + 1; diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index a9520c0a00..447e6100bb 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -34,6 +34,11 @@ namespace Map { What ours also lacks is the original's second local slot: it allocates one (`push ecx`) and reuses the dead tribeID parameter slot for the second counter, where the original has `sub esp, 8` and two proper locals. + + Copying the parameter into `int const _tribeID` and using that through the loop + does not buy the slot: measured 18.8% -> 18.8%, MSVC coalesces the copy back onto + the incoming argument. The slot comes from whatever second value the original + keeps live, not from naming the first one. */ // FUNCTION: STRONGHOLDCRUSADER 0x00524CA0 undefined4 TribesState::isTribePathToDestinationClear( From 237f625926f535554ac168a6b5defe9572b92849 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:05:03 +0200 Subject: [PATCH 135/202] reimplement: sortTribePathDestinationsByCost 29.5 -> 56.2, one array subscripted off the global The decompiler's two spellings of searchQueue.destinationsArray - the queue itself and a climbData + 200 cast - are the same address, so all three loops walk one array. Subscripting it off the global gives the original's [offset + global + N] addressing; a local base pointer gives [ptr + N] and loses 30 points. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../sortTribePathDestinationsByCost.cpp | 230 +++++++----------- 1 file changed, 92 insertions(+), 138 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp index 855c882d41..ef9827ca74 100644 --- a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -27,26 +27,9 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00524930 void TribesState::sortTribePathDestinationsByCost(int tribeID, int horseAndRamCount) { - int iVar2; - int iVar3; - int iVar4; - int iVar5; - int iVar6; - bool bVar7; - int iVar9; - uint y; - PathFindingStatePartB* _p2; - uint x; - int _counter2; - int* _p2Tile2; - int _tile1; - int _offset1; - PathFindingStatePartB* _p; - int _index1; - int _index2; BOOLEnum BVar8 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); short sVar1 = this->tribes[tribeID].selectionTargetUnitID; - x = (uint)DAT_UnitsState::instance.units[sVar1].x; + uint x = (uint)DAT_UnitsState::instance.units[sVar1].x; /* Both of these were the wrong way round. The original computes x before the first test and jumps away when the count is zero - `cmp dword ptr [esp + @@ -59,7 +42,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::calculatePathKeepAndWallsGatesNotAllowed, DAT_PathFindingState::ptr)(x, (int)(DAT_UnitsState::instance.units[sVar1].y), -1, -1, 100000); } else { - y = (uint)DAT_UnitsState::instance.units[sVar1].y; + uint y = (uint)DAT_UnitsState::instance.units[sVar1].y; if (BVar8) { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, DAT_PathFindingState::ptr)(x, y, 0xffffffff, 0xffffffff, 100000, 0); @@ -68,145 +51,116 @@ namespace Map { DAT_PathFindingState::ptr)(x, y, -1, -1, 100000, FALSE); } } + /* + All three loops walk one array. The decompiler printed them as byte offsets + into two different spellings of the same address - the search queue itself + and a `climbData + 200` cast - but climbData is at 0x1084 and the queue at + 0x1a3a4, which is 200 ClimbData further on, so both are + `searchQueue.destinationsArray` and every `+ 0xc` offset is one entry. + + The array is subscripted off the global rather than through a local base + pointer. The original addresses every field as `[offset + + N]`, + strength-reducing the subscript to a byte offset it recomputes from the + counter at the bottom of each loop (`add ecx, 1; lea eax, [ecx + ecx*2]; + add eax, eax; add eax, eax`); a `PathHelper12* const` local for the array + makes MSVC keep the base in a register and address `[ptr + N]` instead, + which is worth almost thirty points across the three loops: the same code + with a `PathHelper12* const _destinations` local measures 26.4%, and + subscripting the global measures 56.2%. + */ + int _index = 0; if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 != 0) { - _p = &DAT_PathFindingState::instance.searchQueue; - int _nextOffset1 = 0; - _offset1 = 0; + /* + The entry pointer is what the original reads the tile through + (`mov edx, [edx]`), recomputed from the counter at the bottom alongside + the offset it stores through. + */ + PathHelper12* _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray; do { - _tile1 = _p->destinationsArray[0].tile1; - if (DAT_TileMapState::instance.WalkLayer[_tile1] + /* + Ten million is the cost given to a destination the current search did + not reach, which is what the compaction loop below drops entries on. + */ + int const _tile = _entry->tile1; + if (DAT_TileMapState::instance.WalkLayer[_tile] == DAT_PathFindingState::instance.searchGeneration) { - *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile3OrAnotherHelper - + _nextOffset1) = (int)DAT_TileMapState::instance.CertainPathLayer[_tile1]; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper + = (int)DAT_TileMapState::instance.CertainPathLayer[_tile]; } else { - *(undefined4*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile3OrAnotherHelper - + _nextOffset1) = 10000000; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper = 10000000; } - _nextOffset1 = _offset1 * 0xc + 0xc; - _index1 = _offset1 + 1; - _p = (PathFindingStatePartB*)(((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData - + 200)) - ->destinationsArray - + _offset1 + 1); - _offset1 = _offset1 + 1; - } while (((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[_index1] - .tile1 - != 0); + _index = _index + 1; + _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray + _index; + } while (_entry->tile1 != 0); } + /* + Bubble sort of the reached destinations by cost, cheapest first. Both the + counter and the flag live in stack slots the original zeroes from one + register before the emptiness test (`xor ecx, ecx; cmp [..], ecx; + mov [esp + 0x14], ecx; mov [esp + 0x18], ecx`), so they are assigned at the + top of the outer loop and the test follows them. + */ + bool bVar7; do { - tribeID = 0; + _index = 0; bVar7 = false; if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 == 0) break; - iVar9 = 0; do { - iVar2 = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile2OrAHelper - + iVar9); - if (!iVar2 - || (iVar3 - = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[1] - .tile1 - + iVar9), - iVar3 == 0) - || (iVar4 - = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[1] - .tile2OrAHelper - + iVar9), - iVar4 == 0)) + /* + Three separate tests, each jumping to the flag check at the bottom of + the outer loop (`test ebp, ebp; jz 0x00524aab`). + */ + int const _tile2 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile2OrAHelper; + if (!_tile2) + break; + int const _nextTile1 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile1; + if (!_nextTile1) + break; + int const _nextTile2 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile2OrAHelper; + if (!_nextTile2) break; - iVar5 = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile3OrAnotherHelper - + iVar9); - iVar6 = *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[1] - .tile3OrAnotherHelper - + iVar9); - if (iVar6 < iVar5) { - *(undefined4*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[1] - .tile1 - + iVar9) - = *(undefined4*)((int)&DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 - + iVar9); + int const _cost = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper; + int const _nextCost = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile3OrAnotherHelper; + if (_nextCost < _cost) { + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile1 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile1; bVar7 = true; - *(int*)((int)&DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 + iVar9) - = iVar3; - *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile2OrAHelper - + iVar9) = iVar4; - *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[1] - .tile2OrAHelper - + iVar9) = iVar2; - *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile3OrAnotherHelper - + iVar9) = iVar6; - *(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[1] - .tile3OrAnotherHelper - + iVar9) = iVar5; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile1 = _nextTile1; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile2OrAHelper = _nextTile2; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile2OrAHelper = _tile2; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper = _nextCost; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile3OrAnotherHelper = _cost; } - iVar9 = tribeID * 0xc + 0xc; - iVar2 = tribeID + 1; - tribeID = tribeID + 1; - } while (((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[iVar2] - .tile1 - != 0); + _index = _index + 1; + } while (DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile1 != 0); } while (bVar7); - iVar9 = 0; + /* + Compaction: the destinations the search did not reach are dropped and the + array is terminated with a zero tile. Both counters are zeroed before the + emptiness test again, and the write cursor is a pointer to the middle field + of its entry - `lea esi, [ + 4]` advanced by 0xc, with the three + fields written as [esi - 4], [esi] and [esi + 4]. + */ + int _keptCount = 0; + _index = 0; if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 != 0) { - _p2 = &DAT_PathFindingState::instance.searchQueue; - int _nextOffset2 = 0; - _p2Tile2 = &DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper; - _counter2 = 0; + PathHelper12* _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray; + int* _write = &DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper; do { - if (*(int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile3OrAnotherHelper - + _nextOffset2) - != 10000000) { - if (_counter2 != iVar9) { - ((PathHelper12*)(_p2Tile2 + -1))->tile1 = _p2->destinationsArray[0].tile1; - *_p2Tile2 = *( - int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile2OrAHelper - + _nextOffset2); - _p2Tile2[1] = *( - int*)((int)&((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[0] - .tile3OrAnotherHelper - + _nextOffset2); + if (DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper != 10000000) { + if (_index != _keptCount) { + _write[-1] = _entry->tile1; + *_write = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile2OrAHelper; + _write[1] = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper; } - iVar9 = iVar9 + 1; - _p2Tile2 = _p2Tile2 + 3; + _keptCount = _keptCount + 1; + _write = _write + 3; } - _nextOffset2 = _counter2 * 0xc + 0xc; - _index2 = _counter2 + 1; - _p2 = (PathFindingStatePartB*)(((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData - + 200)) - ->destinationsArray - + _counter2 + 1); - _counter2 = _counter2 + 1; - } while (((PathFindingStatePartB*)(DAT_PathFindingState::instance.climbData + 200)) - ->destinationsArray[_index2] - .tile1 - != 0); + _index = _index + 1; + _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray + _index; + } while (_entry->tile1 != 0); } - DAT_PathFindingState::instance.searchQueue.destinationsArray[iVar9].tile1 = 0; + DAT_PathFindingState::instance.searchQueue.destinationsArray[_keptCount].tile1 = 0; } } From 58b8cbf138538592aeee3878b9a98666ae098ecb Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:09:52 +0200 Subject: [PATCH 136/202] measure: sortTribePathDestinationsByCost's slot widths cost 1.3, not the gap Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/sortTribePathDestinationsByCost.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp index ef9827ca74..cc6808b31e 100644 --- a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -100,6 +100,14 @@ namespace Map { mov [esp + 0x14], ecx; mov [esp + 0x18], ecx`), so they are assigned at the top of the outer loop and the test follows them. */ + /* + The flag stays a `bool` although the original stores a dword into its slot + (`mov [esp + 0x18], ecx` and `mov [esp + 0x18], 1`) where ours stores the + byte `bl`: as an `int`, together with spelling both inner path-finding calls' + arguments identically so MSVC could share their push sequence the way the + original does, it measures 56.2% -> 54.9%. Whatever the original's flag was, + its slot widths are not what the remaining gap is made of. + */ bool bVar7; do { _index = 0; From 391f1620764981531bef9db920930017269a1809 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:10:11 +0200 Subject: [PATCH 137/202] measure: sync the status file for sortTribePathDestinationsByCost Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index ce70e64158..0f9239ea4d 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46895,7 +46895,7 @@ SHC_3BB0A8C1_0x005244D0 | 44.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524890 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524930 | 29.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524930 | 56.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x005249C0 | 0.0% | Pending @@ -46973,7 +46973,7 @@ SHC_3BB0A8C1_0x005270F0 | 47.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527440 | 59.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527440 | 60.4% | Reimplemented, asm differs SHC_3BB0A8C1_0x005275A0 | 0.0% | Pending From 75fb59a084b2b9efb93001742c3d74e4ea119feb Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:16:32 +0200 Subject: [PATCH 138/202] measure: hoisting coordinate reads above their arithmetic is flat in three functions Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/playUnitCombatSpeechForTarget.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index a12be48634..175cd82aff 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -113,6 +113,16 @@ namespace Map { } else { _distance = unitID; } + /* + The original reads all four coordinates before either subtraction - + `movsx edx, [esi + 0x610]`, `movsx ebx, [eax + 0x610]`, + `movsx edi, [esi + 0x612]`, `movsx ebp, [eax + 0x612]`, then `sub edx, + ebx` and `sub eax, ebp` - where ours interleaves each pair with its + subtraction. Writing the four reads out into locals of their own first + measures 40.6% -> 40.6%: MSVC schedules these loads where it wants them, + and the same spelling is flat in updateRabbitTribeBehavior and + processDeerMoving too. + */ _xDifference = (int)DAT_UnitsState::instance.units[_otherUnitID].x - (int)DAT_UnitsState::instance.units[unitID].x; _yDifference = (int)DAT_UnitsState::instance.units[_otherUnitID].y From 8d07e45d12dce98c480b1ed3e6e5dd33ee1133ac Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:21:11 +0200 Subject: [PATCH 139/202] reimplement: predictUnitInterceptPosition's path plan is units[].pathPlanStart 0x138864a is units[0].pathPlanStart - the array starts at 0x138854c and the field is at 0xfe - so the raw pointer arithmetic the decompiler left becomes a subscript, and the translation matrix's yOffset likewise. Match unchanged at 40.1%. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../predictUnitInterceptPosition.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index 250b634d7a..f7b2b09c06 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -21,7 +21,6 @@ namespace Map { int local_8; short _targetTribeMinSpeed; int _unitID = unitID; - int _unitAddressOffset = unitID * 0x490; *unitCurrentX = (int)DAT_UnitsState::instance.units[unitID].x; int _yOffb = (int)DAT_UnitsState::instance.units[unitID].y; *unitCurrentY = _yOffb; @@ -111,8 +110,16 @@ namespace Map { /* get current value in pathPlan, each byte contains two move instructions */ - uint _pathPlanPair - = (uint) * (char*)((int)_currentIndexInPathPlan / 2 + 0x138864a + _unitAddressOffset); + /* + 0x138864a is `units[0].pathPlanStart`: the unit array starts at + 0x138854c, since the x the original reads at 0x1388610 is at 0xc4 in + a Unit, and 0x138864a - 0x138854c is 0xfe. The byte is signed - + `movsx eax, byte ptr [eax + ebp + 0x138864a]` - so the cast stays, + and the unit's own offset is still added separately because that is + how the original addresses it. + */ + uint _pathPlanPair = (uint)(char)DAT_UnitsState::instance.units[_unitID] + .pathPlanStart[(int)_currentIndexInPathPlan / 2]; if (!(_currentIndexInPathPlan & 1)) { /* if index is even, retain the lowest 4 bits @@ -128,8 +135,8 @@ namespace Map { .int_.xOffset + _unitCurrentX_2; *unitCurrentY = *unitCurrentY - + *(int*)((int)DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix - + _pathPlanPair * 8 + 4); + + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[_pathPlanPair] + .int_.yOffset; _unitCurrentX_2 = *unitCurrentX; uint _xOff = _unitCurrentX_2 - iVar4; uint _yOff = *unitCurrentY - iVar2; From 66498a681c837b6cb51b03c54968ff53f06225dd Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:26:14 +0200 Subject: [PATCH 140/202] reimplement: updateCamelTribeBehavior 73.1 -> 76.3, the behaviour timeout is a short Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/updateCamelTribeBehavior.cpp | 11 ++++++++--- .../Units/TribesState/updateRabbitTribeBehavior.cpp | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index 84ee8a08c9..e528a15733 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -125,7 +125,12 @@ namespace Map { this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; } } else if (SVar2 == Map::Units::STBT_1) { - sVar5 = 800; + /* + A `short` of its own rather than another use of `sVar5`, which is an + `int`: the original compares this against the counter sixteen bits + wide (`cmp ax, dx`), where an `int` threshold gives `cmp eax, ecx`. + */ + short _behaviourTimeout = 800; /* Written `<= 0x64`, which is the literal the original compares - `cmp word ptr [esi + ..], 100` with a `jg` - where `< 0x65` gives @@ -133,11 +138,11 @@ namespace Map { it is handed. */ if (this->tribes[param_1].unknownCounter01 <= 0x64) { - sVar5 = 10; + _behaviourTimeout = 10; } psVar1 = (short*)&this->tribes[param_1].unknownAttackRelatedUpdateCounter; *psVar1 = *psVar1 + 1; - if (sVar5 <= this->tribes[param_1].unknownAttackRelatedUpdateCounter) { + if (_behaviourTimeout <= this->tribes[param_1].unknownAttackRelatedUpdateCounter) { this->tribes[param_1].tribeBehaviorType = ((SomeTribeBehaviorType)0); this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; } diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index b5e7b5101f..406ada4456 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -152,6 +152,14 @@ namespace Map { } psVar1 = (short*)&_tb->tribes[0].unknownAttackRelatedUpdateCounter; *psVar1 = *psVar1 + 1; + /* + The original compares these two sixteen bits wide (`cmp ax, dx`) + where the fields compared in place promote to `int` and give + `cmp eax, edx`. Reading both into `short` locals first to say so + measures 63.0% -> 62.5%, so the narrowing is not worth what the two + extra loads cost here - unlike updateCamelTribeBehavior, where the + same change to a threshold local is worth 3.2. + */ if (_tb->tribes[0].wanderIntervalTicks <= _tb->tribes[0].unknownAttackRelatedUpdateCounter) { _tb->tribes[0].tribeBehaviorType = ((SomeTribeBehaviorType)0); From 4fc4ad4faf1e7c2c9ec2436b20ef91be3bc488ea Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:30:36 +0200 Subject: [PATCH 141/202] measure: two narrowing changes that cost more in block layout than they buy Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/spawnDeerLionOrRabbit.cpp | 5 ++++- .../Map/Units/TribesState/updateTribeUnitAssignments.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index 4c49201e42..7da72631af 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -65,7 +65,10 @@ namespace Map { _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2 & 0x7f; _terrainHeight = DAT_UnitsState::instance.units[_finalTargetUnitID].terrainOrClimbHeight; /* - Both of these reads are `movzx` in the original, not `movsx`. + Both of these reads are `movzx` in the original, not `movsx`. They stay + `int`s cast on the way in rather than `ushort` locals, although the original + compares them against each other sixteen bits wide (`cmp cx, di`): as + `ushort` the function measures 74.1% -> 70.2%. */ _unknown = (ushort)this->tribes[tribeID].herdMinSize; _tribeSize = (ushort)this->tribes[tribeID].size; diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp index 72bfdf0347..0ebda4a472 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp @@ -60,6 +60,14 @@ namespace Map { this)(_tribeID, _tribeUnitIndex); int _unitLogicalState = DAT_UnitsState::instance.units[_unitID].logicalState; _tribeUnitIndex = _tribeUnitIndex + 1; + /* + `< 1` stays although the original has + `test edx, edx; jle` where ours gives + `cmp edx, 1; jl`: written `<= 0` the whole chain + below it re-lays out and the function measures + 75.0% -> 55.3%. The instruction is one line; the + block order it costs is twenty. + */ if (_unitLogicalState < 1 || (2 < _unitLogicalState && (_unitLogicalState != Map::Units::ULS_TRANSITIONING)) From 192aa107bc056aa253cbc3bd1c0798c6e5f07eb7 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:31:03 +0200 Subject: [PATCH 142/202] measure: sync the status file for updateCamelTribeBehavior Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- status/addresses-SHC-3BB0A8C1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 0f9239ea4d..4e6bd46fec 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49147,7 +49147,7 @@ SHC_3BB0A8C1_0x0052B110 | 72.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B630 | 73.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B630 | 76.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs From 3cd214bc99b82f0d488fe31f735804d2bd289154 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:43:11 +0200 Subject: [PATCH 143/202] measure: moveTribeToIndexedNearbyTile's loop bound has to stay a literal Deriving 0xb4df44 from the candidate array costs 7.4 points, so the magic address stays with the arithmetic written out in a comment instead. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/moveTribeToIndexedNearbyTile.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index a25dcd0285..c34026ab17 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -65,6 +65,18 @@ namespace Map { } } piVar6 = piVar6 + 2; + /* + 0xb4df44 is one past the end of the forty-entry candidate array - it is + at 0xb4d13c + 0xcc4 and the cursor points at the second member of each + eight-byte entry, so the end is 0xb4de00 + 40 * 8 + 4. The literal stays + against the style list: written + `< (int)(&...field1608_0xcc4[39].y + 2)`, which is the same address, the + function measures 45.9% -> 38.5%. + + Carrying the return value in `iVar5`, which is dead from the line above, + to save the stack slot the original does not have (`sub esp, 8` against + our 0xc) was measured at the same time and is not what costs the points. + */ } while ((int)piVar6 < 0xb4df44); return (int)(uVar3); } From 5d0d38700f3615a893604684c5433c9cb4ca21d9 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 22 Oct 2026 20:47:48 +0200 Subject: [PATCH 144/202] measure: assignAttackTargetsForTribe's materialised flag is cheaper than branching Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/assignAttackTargetsForTribe.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp index 68a6918efe..baefb2eeec 100644 --- a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -201,6 +201,17 @@ namespace Map { || UVar1 == Map::Units::States::US_RELOAD_WEAPONUnk || UVar1 == Map::Units::States::US_STAND_UPUnk) continue; + /* + This stays a flag assignment although MSVC + materialises it - `cmp ax, 9; sete al; test al, al; + jne` where the original has the bare `cmp ax, 9` and + lets the shared `jne` below consume the flags. The + other three arms do carry their comparison in the + flags; this one is the arm that falls through to the + shared test and MSVC will not carry it across that + edge. Testing and jumping here instead, which removes + the `sete` and the `test`, measures 44.6% -> 43.7%. + */ bVar7 = UVar1 == (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk); } From f216948146b617c3a61c116cb1a966a86ea944fb Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 23 Oct 2026 01:01:14 +0200 Subject: [PATCH 145/202] reimplement: giveTribeAnInstruction 60.4 -> 60.6, per-arm locals and int spawn coordinates The decompiler folded one `int` scratch across eight disjoint switch arms; splitting it into a local per arm is exactly codegen-neutral (measured 60.4% both ways), which is what makes the arms independently typeable. The exit-siege arm then gets what 0x529e0e actually does: the two path-finding results are read as dwords and the coordinates with movsx, with the (short) on the stores rather than on the reads. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 254 ++++++++++-------- 1 file changed, 138 insertions(+), 116 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 43577af5b1..ce20205678 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -145,7 +145,6 @@ namespace Map { */ short sVar9; uint uVar13; - int iVar14; int iVar15; int _laddermanID; undefined3 extraout_var; @@ -326,6 +325,7 @@ namespace Map { } break; case Map::Units::UIT_UNIT_ATTACK_UNIT: + int _pathTile; /* unit attack target */ @@ -507,10 +507,10 @@ namespace Map { if (((param_5 != 1) || (DAT_UnitsState::instance.units[iVar11].state.generic != Map::Units::States::US_MELEE_ATTACK)) - && (iVar14 = *(int*)id, iVar14 != 0)) { + && (_pathTile = *(int*)id, _pathTile != 0)) { sVar9 = DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[iVar14]; - uVar13 = iVar14 + .tileTranslationMatrix_YComponent[_pathTile]; + uVar13 = _pathTile - DAT_ViewportRenderState::instance.translationMatrix[sVar9] .addXgetTile; if (*(int*)(id + 4) == 0) { @@ -582,6 +582,7 @@ namespace Map { break; case ((UnitInstructionType)0x20): case Map::Units::UIT_SHOOT_TARGETUnk: + int _unitID_0x20; local_8 = id * 0x490; pUVar6 = DAT_UnitsState::instance.units + id; _unitTribeIndex = 0; @@ -721,15 +722,15 @@ namespace Map { if (0 < sVar9) { id = 0x12d5c6c; do { - iVar14 = MACRO_CALL_MEMBER( + _unitID_0x20 = MACRO_CALL_MEMBER( Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); iVar12 = iVar12 + 1; - if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar14].dying == 0 - && DAT_UnitsState::instance.units[iVar14].field303_0x413 == 0 - && DAT_UnitsState::instance.units[iVar14].state.generic + if (DAT_UnitsState::instance.units[_unitID_0x20].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID_0x20].dying == 0 + && DAT_UnitsState::instance.units[_unitID_0x20].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID_0x20].state.generic != Map::Units::States::US_MELEE_ATTACK) { - switch (DAT_UnitsState::instance.units[iVar14].unitType) { + switch (DAT_UnitsState::instance.units[_unitID_0x20].unitType) { case Map::Units::UT_TUNNELER: case Map::Units::UT_E_SPEAR: case Map::Units::UT_E_PIKE: @@ -749,37 +750,37 @@ namespace Map { - DAT_ViewportRenderState::instance.translationMatrix[_y_528565] .addXgetTile; if (*(int*)(id + 4) == 0) { - DAT_UnitsState::instance.units[iVar14].targetingType + DAT_UnitsState::instance.units[_unitID_0x20].targetingType = ((UnitInstructionType)0); - if (DAT_UnitsState::instance.units[iVar14].unitType + if (DAT_UnitsState::instance.units[_unitID_0x20].unitType == Map::Units::UT_LORD) break; } else { - DAT_UnitsState::instance.units[iVar14].targetingType + DAT_UnitsState::instance.units[_unitID_0x20].targetingType = Map::Units::UIT_UNIT_ATTACK_UNIT; - DAT_UnitsState::instance.units[iVar14] + DAT_UnitsState::instance.units[_unitID_0x20] .targetedUnitID__OR__engineerMannedSiegeEngineRef = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar14] + DAT_UnitsState::instance.units[_unitID_0x20] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID = unitUID; } - DAT_UnitsState::instance.units[iVar14].plannedDestinationX + DAT_UnitsState::instance.units[_unitID_0x20].plannedDestinationX = (short)_x_528565; - DAT_UnitsState::instance.units[iVar14].plannedDestinationY = _y_528565; - DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; - if (DAT_UnitsState::instance.units[iVar14].state.generic + DAT_UnitsState::instance.units[_unitID_0x20].plannedDestinationY = _y_528565; + DAT_UnitsState::instance.units[_unitID_0x20].targetedBuildingTile = 0; + if (DAT_UnitsState::instance.units[_unitID_0x20].state.generic == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[iVar14].movementCooldown - = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; + DAT_UnitsState::instance.units[_unitID_0x20].movementCooldown + = DAT_UnitsState::instance.units[_unitID_0x20].movementSpeed * -4; } - if (DAT_UnitsState::instance.units[iVar14]._someX_2 == 0) { - DAT_UnitsState::instance.units[iVar14]._someX_2 - = DAT_UnitsState::instance.units[iVar14].destinationX_2Unk; - DAT_UnitsState::instance.units[iVar14]._someY_2 - = DAT_UnitsState::instance.units[iVar14].destinationY_2Unk; + if (DAT_UnitsState::instance.units[_unitID_0x20]._someX_2 == 0) { + DAT_UnitsState::instance.units[_unitID_0x20]._someX_2 + = DAT_UnitsState::instance.units[_unitID_0x20].destinationX_2Unk; + DAT_UnitsState::instance.units[_unitID_0x20]._someY_2 + = DAT_UnitsState::instance.units[_unitID_0x20].destinationY_2Unk; } - DAT_UnitsState::instance.units[iVar14].state.generic + DAT_UnitsState::instance.units[_unitID_0x20].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; if (MACRO_CALL_MEMBER( Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID) @@ -789,18 +790,18 @@ namespace Map { DAT_PathFindingState::instance.allAssassinsUnk = 1; } MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(iVar14, _x_528565, (uint)(_y_528565), 0); - DAT_UnitsState::instance.units[iVar14].moveDelay = 0; - DAT_UnitsState::instance.units[iVar14].lookForEnemy = -0x32; - if (DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + DAT_UnitsState::ptr)(_unitID_0x20, _x_528565, (uint)(_y_528565), 0); + DAT_UnitsState::instance.units[_unitID_0x20].moveDelay = 0; + DAT_UnitsState::instance.units[_unitID_0x20].lookForEnemy = -0x32; + if (DAT_UnitsState::instance.units[_unitID_0x20].movementType_OR_targetUnitID != iVar11) { - DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + DAT_UnitsState::instance.units[_unitID_0x20].movementType_OR_targetUnitID = _param_3_id_x_tile_buildingID_copy; psVar1 = (short*)((int)&DAT_UnitsState::instance.units[0].huntedBy + local_8); *psVar1 = *psVar1 + 1; } - UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[iVar14] + UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[_unitID_0x20] .totalSizeOfPathPlan / 2); if ((int)UVar10 > (int)unitInstructionType) { @@ -944,6 +945,7 @@ namespace Map { } break; case Map::Units::UIT_LIGHT_PITCH: + int _ditchTile; this->tribes[tribeID].isRallyingUnk = 0; _unitTribeIndex = 0; if ((DAT_TileMapState::instance.pitchDitches[id].uid == unitUID) && (0 < _tribeSize)) { @@ -960,10 +962,10 @@ namespace Map { sVar9 = DAT_TileMapState::instance.pitchDitches[iVar11].y; DAT_UnitsState::instance.units[iVar12].shootTargetMicroX = DAT_TileMapState::instance.pitchDitches[iVar11].x * 8 + 4; - iVar14 = DAT_TileMapState::instance.pitchDitches[iVar11].tile; + _ditchTile = DAT_TileMapState::instance.pitchDitches[iVar11].tile; DAT_UnitsState::instance.units[iVar12].shootTargetMicroY = sVar9 * 8 + 4; DAT_UnitsState::instance.units[iVar12].shootTargetZ - = (ushort)DAT_TileMapState::instance.HeightLayer[iVar14]; + = (ushort)DAT_TileMapState::instance.HeightLayer[_ditchTile]; DAT_UnitsState::instance.units[iVar12].shootTargetedUnit = -1; DAT_UnitsState::instance.units[iVar12].targetID_OR_targetBuildingID = _param_3_id_x_tile_buildingID_copy; @@ -1292,6 +1294,7 @@ namespace Map { case Map::Units::UIT_ATTACK_WALL: case ((UnitInstructionType)0x23): case ((UnitInstructionType)0x25): + int _unitID_attackWall; _unitTribeIndex = 0; id = 0; this->tribes[tribeID].isRallyingUnk = 0; @@ -1444,13 +1447,13 @@ namespace Map { && (iVar12 = 0, 0 < this->tribes[tribeID].size)) { id = 0x12d5c70; do { - iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _unitID_attackWall = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); iVar12 = iVar12 + 1; - if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar14].dying == 0 - && DAT_UnitsState::instance.units[iVar14].field303_0x413 == 0) { - UVar4 = DAT_UnitsState::instance.units[iVar14].unitType; + if (DAT_UnitsState::instance.units[_unitID_attackWall].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID_attackWall].dying == 0 + && DAT_UnitsState::instance.units[_unitID_attackWall].field303_0x413 == 0) { + UVar4 = DAT_UnitsState::instance.units[_unitID_attackWall].unitType; switch (UVar4) { case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_XBOW: @@ -1497,7 +1500,7 @@ namespace Map { */ case Map::Units::UT_S_BATTERINGRAM: if (UVar4 == Map::Units::UT_S_BATTERINGRAM) { - if (DAT_UnitsState::instance.units[iVar14] + if (DAT_UnitsState::instance.units[_unitID_attackWall] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 4) break; @@ -1506,7 +1509,7 @@ namespace Map { iVar15 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: findUnitPreferredOrientationBasedConnectedTile, - DAT_PathFindingState::ptr)(iVar14, iVar11); + DAT_PathFindingState::ptr)(_unitID_attackWall, iVar11); if (iVar15 != 0) { sVar9 = DAT_ViewportRenderState::instance .tileTranslationMatrix_YComponent[iVar15]; @@ -1515,29 +1518,29 @@ namespace Map { .addXgetTile; BVar16 = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(iVar14, (uint)(unitUID), (uint)(sVar9), 0); + DAT_UnitsState::ptr)(_unitID_attackWall, (uint)(unitUID), (uint)(sVar9), 0); if (BVar16) { - DAT_UnitsState::instance.units[iVar14].targetingType + DAT_UnitsState::instance.units[_unitID_attackWall].targetingType = ((UnitInstructionType)0); - DAT_UnitsState::instance.units[iVar14].plannedDestinationX + DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationX = (short)unitUID; - DAT_UnitsState::instance.units[iVar14].plannedDestinationY = sVar9; - if (DAT_UnitsState::instance.units[iVar14].state.generic + DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationY = sVar9; + if (DAT_UnitsState::instance.units[_unitID_attackWall].state.generic == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[iVar14].movementCooldown - = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; + DAT_UnitsState::instance.units[_unitID_attackWall].movementCooldown + = DAT_UnitsState::instance.units[_unitID_attackWall].movementSpeed * -4; } - DAT_UnitsState::instance.units[iVar14].state.generic + DAT_UnitsState::instance.units[_unitID_attackWall].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID + DAT_UnitsState::instance.units[_unitID_attackWall].movementType_OR_targetUnitID = 0; DAT_PathFindingState::instance.notAllAssassinsUnk = 0; sVar9 = DAT_ViewportRenderState::instance .tileTranslationMatrix_YComponent[iVar11]; - DAT_UnitsState::instance.units[iVar14].targetedBuildingTile + DAT_UnitsState::instance.units[_unitID_attackWall].targetedBuildingTile = iVar11; - DAT_UnitsState::instance.units[iVar14].attackAtTileY = sVar9; - DAT_UnitsState::instance.units[iVar14].attackAtTileX + DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileY = sVar9; + DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileX = _param_3_id_x_tile_buildingID_copy - (short)DAT_ViewportRenderState::instance .translationMatrix[sVar9] @@ -1553,29 +1556,29 @@ namespace Map { .tileTranslationMatrix_YComponent[iVar15]; uVar13 = iVar15 - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; - DAT_UnitsState::instance.units[iVar14].targetingType = ((UnitInstructionType)0); - DAT_UnitsState::instance.units[iVar14].plannedDestinationX = (short)uVar13; - DAT_UnitsState::instance.units[iVar14].plannedDestinationY = sVar9; + DAT_UnitsState::instance.units[_unitID_attackWall].targetingType = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationX = (short)uVar13; + DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationY = sVar9; DAT_PathFindingState::instance.notAllAssassinsUnk = 1; - if (DAT_UnitsState::instance.units[iVar14].state.generic + if (DAT_UnitsState::instance.units[_unitID_attackWall].state.generic == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[iVar14].movementCooldown - = DAT_UnitsState::instance.units[iVar14].movementSpeed * -4; + DAT_UnitsState::instance.units[_unitID_attackWall].movementCooldown + = DAT_UnitsState::instance.units[_unitID_attackWall].movementSpeed * -4; } - DAT_UnitsState::instance.units[iVar14].state.generic + DAT_UnitsState::instance.units[_unitID_attackWall].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(iVar14, uVar13, (uint)(sVar9), 0); + DAT_UnitsState::ptr)(_unitID_attackWall, uVar13, (uint)(sVar9), 0); DAT_PathFindingState::instance.notAllAssassinsUnk = 0; uVar13 = *(uint*)id; sVar9 = *(short*)id; - DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID = 0; - DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = uVar13; + DAT_UnitsState::instance.units[_unitID_attackWall].movementType_OR_targetUnitID = 0; + DAT_UnitsState::instance.units[_unitID_attackWall].targetedBuildingTile = uVar13; sVar8 = DAT_ViewportRenderState::instance .tileTranslationMatrix_YComponent[uVar13]; - DAT_UnitsState::instance.units[iVar14].attackAtTileY = sVar8; + DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileY = sVar8; id = id + 0xc; - DAT_UnitsState::instance.units[iVar14].attackAtTileX = sVar9 + DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileX = sVar9 - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar8] .addXgetTile; } @@ -1589,20 +1592,21 @@ namespace Map { } break; case Map::Units::UIT_SET_LADDER: + int _unitID_setLadder; iVar12 = 0; id = 0; this->tribes[tribeID].isRallyingUnk = 0; if (0 < _tribeSize) { do { - iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _unitID_setLadder = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); iVar12 = iVar12 + 1; - if (DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar14].dying == 0 - && DAT_UnitsState::instance.units[iVar14].unitType == Map::Units::UT_E_LADDER) { + if (DAT_UnitsState::instance.units[_unitID_setLadder].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID_setLadder].dying == 0 + && DAT_UnitsState::instance.units[_unitID_setLadder].unitType == Map::Units::UT_E_LADDER) { id = id + 1; - DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; - DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; + DAT_UnitsState::instance.units[_unitID_setLadder]._someX_2 = 0; + DAT_UnitsState::instance.units[_unitID_setLadder]._someY_2 = 0; } } while (iVar12 < this->tribes[tribeID].size); /* @@ -1689,6 +1693,7 @@ namespace Map { uVar13 = DAT_PathFindingState::instance.climbX; } case Map::Units::UIT_DIG_MOAT: + int _unitID_digMoat; iVar11 = unitUID; iVar12 = 0; this->tribes[tribeID].isRallyingUnk = 0; @@ -1696,32 +1701,32 @@ namespace Map { tribeID, uVar13, (uint)(unitUID), 0, 1); if (0 < this->tribes[tribeID].size) { do { - iVar14 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + _unitID_digMoat = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); iVar12 = iVar12 + 1; - if ((DAT_UnitsState::instance.units[iVar14].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar14].dying == 0)) { - switch (DAT_UnitsState::instance.units[iVar14].unitType) { + if ((DAT_UnitsState::instance.units[_unitID_digMoat].logicalState == Map::Units::ULS_NORMAL) + && (DAT_UnitsState::instance.units[_unitID_digMoat].dying == 0)) { + switch (DAT_UnitsState::instance.units[_unitID_digMoat].unitType) { case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_SPEAR: case Map::Units::UT_E_PIKE: case Map::Units::UT_E_MACE: case Map::Units::UT_E_ENGINEER: case Map::Units::UT_A_SLAVE: - if (DAT_UnitsState::instance.units[iVar14].state.generic == (UnitState)0x69) { - DAT_UnitsState::instance.units[iVar14].state.generic + if (DAT_UnitsState::instance.units[_unitID_digMoat].state.generic == (UnitState)0x69) { + DAT_UnitsState::instance.units[_unitID_digMoat].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; } - DAT_UnitsState::instance.units[iVar14].targetingType = (undefined2)unitInstructionType; - DAT_UnitsState::instance.units[iVar14].attackAtTileX = (short)uVar13; - DAT_UnitsState::instance.units[iVar14].attackAtTileY = (short)iVar11; - DAT_UnitsState::instance.units[iVar14].targetX = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[iVar14].targetY + DAT_UnitsState::instance.units[_unitID_digMoat].targetingType = (undefined2)unitInstructionType; + DAT_UnitsState::instance.units[_unitID_digMoat].attackAtTileX = (short)uVar13; + DAT_UnitsState::instance.units[_unitID_digMoat].attackAtTileY = (short)iVar11; + DAT_UnitsState::instance.units[_unitID_digMoat].targetX = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_digMoat].targetY = (UnitStateShort)_param_4_unitUID_Y_copy; - DAT_UnitsState::instance.units[iVar14].targetedBuildingTile = 0; - DAT_UnitsState::instance.units[iVar14].movementType_OR_targetUnitID = 0; - DAT_UnitsState::instance.units[iVar14]._someX_2 = 0; - DAT_UnitsState::instance.units[iVar14]._someY_2 = 0; + DAT_UnitsState::instance.units[_unitID_digMoat].targetedBuildingTile = 0; + DAT_UnitsState::instance.units[_unitID_digMoat].movementType_OR_targetUnitID = 0; + DAT_UnitsState::instance.units[_unitID_digMoat]._someX_2 = 0; + DAT_UnitsState::instance.units[_unitID_digMoat]._someY_2 = 0; } } } while (iVar12 < this->tribes[tribeID].size); @@ -1729,6 +1734,7 @@ namespace Map { } break; case Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED: + int _buildingUID; this->tribes[tribeID].isRallyingUnk = 0; _y = DAT_BuildingsState::instance.buildings[id].buildingEntryY; _x = DAT_BuildingsState::instance.buildings[id].buildingEntryX; @@ -1784,20 +1790,20 @@ namespace Map { an `int` return, which is outside this namespace. */ if (((((uint)(extraout_var) & 0xffffff) << 8) | (uint)(byte)(cVar7)) != 0 - && (iVar14 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + && (_buildingUID = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: calculateCanPlayerUnitsNavigateToAreaFromArea, DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, (dword)((int)(param_5)), (dword)((int)((short)DAT_TileMapState::instance .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile])), 0), - iVar14 != 0)) { + _buildingUID != 0)) { sVar9 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryY; sVar8 = DAT_BuildingsState::instance.buildings[iVar11].buildingEntryX; - iVar14 = DAT_BuildingsState::instance.buildings[iVar11].uid; + _buildingUID = DAT_BuildingsState::instance.buildings[iVar11].uid; DAT_UnitsState::instance.units[uVar13].targetID_OR_targetBuildingID = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[uVar13].targetUID = iVar14; + DAT_UnitsState::instance.units[uVar13].targetUID = _buildingUID; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(uVar13, (uint)(sVar8), (uint)(sVar9), 0); if (DAT_BuildingsState::instance.buildings[iVar11].buildingType @@ -1828,6 +1834,7 @@ namespace Map { } break; case Map::Units::UIT_MAN_SIEGE_EQUIPMENT: + int _siegeCrewIndex; /* manning siege equipment? */ @@ -1840,7 +1847,7 @@ namespace Map { do { uVar13 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, iVar12); - iVar14 = iVar12 + 1; + _siegeCrewIndex = iVar12 + 1; if (DAT_UnitsState::instance.units[uVar13].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[uVar13].dying == 0 && DAT_UnitsState::instance.units[uVar13].unitType == Map::Units::UT_E_ENGINEER) { @@ -1850,35 +1857,35 @@ namespace Map { .PathConnectionLayer[DAT_UnitsState::instance.units[uVar13].tile]; unitInstructionType = ((UnitInstructionType)0); do { - iVar14 = (int)DAT_UnitsState::instance.units[iVar11] + _siegeCrewIndex = (int)DAT_UnitsState::instance.units[iVar11] .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine; - x = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][0] + x = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[_siegeCrewIndex][0] + (int)DAT_UnitsState::instance.units[iVar11].x; - y = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[iVar14][1] + y = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[_siegeCrewIndex][1] + (int)DAT_UnitsState::instance.units[iVar11].y; - uVar19 = (iVar14 + 1) % 16; + uVar19 = (_siegeCrewIndex + 1) % 16; DAT_UnitsState::instance.units[iVar11] .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine = (short)uVar19; BVar16 = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)(x, y); if (BVar16) { - iVar14 = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + _siegeCrewIndex = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; _param_4_unitUID_Y_copy - = (dword)(short)DAT_TileMapState::instance.PathConnectionLayer[iVar14]; + = (dword)(short)DAT_TileMapState::instance.PathConnectionLayer[_siegeCrewIndex]; uVar19 = MACRO_CALL_MEMBER( - Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)(iVar14); + Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)(_siegeCrewIndex); uVar19 = ((int)DAT_UnitsState::instance.units[iVar11].buildingHeight + (int)DAT_UnitsState::instance.units[iVar11].terrainOrClimbHeight) - uVar19; uVar17 = (int)uVar19 >> 0x1f; if ((int)((uVar19 ^ uVar17) - uVar17) < 0x11 - && (iVar14 + && (_siegeCrewIndex = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: calculateCanPlayerUnitsNavigateToAreaFromArea, DAT_PathFindingState::ptr)( (int)DAT_UnitsState::instance.units[iVar11].owner, (dword)((int)(param_5)), (dword)((int)(_param_4_unitUID_Y_copy)), 0), - iVar14 != 0)) + _siegeCrewIndex != 0)) break; } unitInstructionType = (UnitInstructionType)(unitInstructionType + 1); @@ -1895,19 +1902,30 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)(this->tribes[tribeID].owner, uVar13, 0); unitUID = unitUID + -1; - iVar14 = iVar12; + _siegeCrewIndex = iVar12; if (unitUID == 0) { return (undefined4)(1); } } - iVar12 = iVar14; - if (this->tribes[tribeID].size <= iVar14) { + iVar12 = _siegeCrewIndex; + if (this->tribes[tribeID].size <= _siegeCrewIndex) { return (undefined4)(1); } } while (true); } break; case Map::Units::UIT_EXIT_SIEGE_EQUIPMENT: + /* + The spawn position is a pair of `int`s rather than the decompiler's + shared `short`s. 0x529e0e reads the two path-finding results as dwords + (`mov ecx, dword ptr [0x12bb90c]`, `mov edx, dword ptr [0x12bb910]`) and + 0x529e36 reads the fallback coordinates with `movsx` into full registers, + storing a word only at the end - so the `(short)` belongs on the stores, + not on the reads, where it cost a truncation and a re-extension at each + of the three places the retry loop sets them. + */ + int _spawnX; + int _spawnY; /* disband siege engines? */ @@ -1959,8 +1977,8 @@ namespace Map { MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::findWalkableTileThatDoesNotContainUnit, DAT_PathFindingState::ptr)(id, uVar19, uVar13, 0); - sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; - sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; + _spawnX = DAT_PathFindingState::instance.ALG_ResultX; + _spawnY = DAT_PathFindingState::instance.ALG_ResultY; iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; if (DAT_PathFindingState::instance.ALG_ResultTile != 0) break; @@ -1978,26 +1996,30 @@ namespace Map { */ if (unitUID != 0x10) goto LAB_00529da0; - sVar9 = DAT_UnitsState::instance.units[iVar11].x; - sVar8 = DAT_UnitsState::instance.units[iVar11].y; + _spawnX = DAT_UnitsState::instance.units[iVar11].x; + _spawnY = DAT_UnitsState::instance.units[iVar11].y; iVar12 = DAT_UnitsState::instance.units[iVar11].tile; } else { - sVar9 = (short)DAT_PathFindingState::instance.ALG_ResultX; - sVar8 = (short)DAT_PathFindingState::instance.ALG_ResultY; + _spawnX = DAT_PathFindingState::instance.ALG_ResultX; + _spawnY = DAT_PathFindingState::instance.ALG_ResultY; iVar12 = DAT_PathFindingState::instance.ALG_ResultTile; } } while (iVar12 == 0); bVar2 = DAT_TileMapState::instance.HeightLayer[iVar12]; DAT_UnitsState::instance.units[_unitID_0x11].totalSizeOfPathPlan = 0; - DAT_UnitsState::instance.units[_unitID_0x11].x = sVar9; - DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentXPosition = sVar9; - DAT_UnitsState::instance.units[_unitID_0x11].y = sVar8; - DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentYPosition = sVar8; + DAT_UnitsState::instance.units[_unitID_0x11].x = (short)_spawnX; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentXPosition + = (short)_spawnX; + DAT_UnitsState::instance.units[_unitID_0x11].y = (short)_spawnY; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentYPosition + = (short)_spawnY; DAT_UnitsState::instance.units[_unitID_0x11].terrainOrClimbHeight = (ushort)bVar2; DAT_UnitsState::instance.units[_unitID_0x11].tile = iVar12; DAT_UnitsState::instance.units[_unitID_0x11].nextTileUnk = iVar12; - DAT_UnitsState::instance.units[_unitID_0x11].microXPosition = sVar9 * 8 + 4; - DAT_UnitsState::instance.units[_unitID_0x11].microYPosition = sVar8 * 8 + 4; + DAT_UnitsState::instance.units[_unitID_0x11].microXPosition + = (short)(_spawnX * 8 + 4); + DAT_UnitsState::instance.units[_unitID_0x11].microYPosition + = (short)(_spawnY * 8 + 4); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::updateMicroPosition, DAT_UnitsState::ptr)( _unitID_0x11); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::resetUnitMovementState, DAT_UnitsState::ptr)( From 0fe4a402ecfbb244bda07f46fab699573455cbd6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 23 Oct 2026 01:11:48 +0200 Subject: [PATCH 146/202] reimplement: Tribe::minimumMovementSpeed is a ushort, and four shapes that do not pay The original loads that field with movzx at 0x521576 and sign-extends it afterwards (movsx ecx, cx at 0x52158a) before the signed test, so it is an unsigned word the code treats as signed; predictUnitInterceptPosition now says that with ushort locals and casts at the tests. Match is unchanged in all four functions that touch the field. Tribe.hpp is generated, so the generator needs the same change. A census of every tribe-field access in the original's diff lines against Tribe.hpp found no other width conflict, and the Unit fields behind the movsx/movzx mismatches in the diff are already correctly signed - those come from casts in our source, not from the headers. Measured and reverted: isTribePathToDestinationClear written as a positive first guard with the body nested, so the shared return 0 is the fall-out (18.8% -> 18.8%); moveUnitToBehaviorTarget without its success flag, with the one shared store written out in both paths (33.3% -> 23.0%); computeTribePercentages through a TribesState base of its own (47.2% -> 45.2%). Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- src/OpenSHC/Map/Units/Tribe.hpp | 2 +- .../TribesState/computeTribePercentages.cpp | 9 ++++++++ .../isTribePathToDestinationClear.cpp | 5 +++++ .../TribesState/moveUnitToBehaviorTarget.cpp | 7 +++++++ .../predictUnitInterceptPosition.cpp | 21 ++++++++++++------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/OpenSHC/Map/Units/Tribe.hpp b/src/OpenSHC/Map/Units/Tribe.hpp index b9c00ba300..75bf0b4469 100644 --- a/src/OpenSHC/Map/Units/Tribe.hpp +++ b/src/OpenSHC/Map/Units/Tribe.hpp @@ -58,7 +58,7 @@ namespace Map { short supportTribeID; // 0x000001F2 length: 2 int uid2; // 0x000001F4 length: 4 short tribeID; // 0x000001F8 length: 2 - short minimumMovementSpeed; // 0x000001FA length: 2 + ushort minimumMovementSpeed; // 0x000001FA length: 2 short movementSpeed; // 0x000001FC length: 2 short maximumMovementSpeed; // 0x000001FE length: 2 short field61_0x200; // 0x00000200 length: 2 diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 6070068824..384592fd4d 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -32,6 +32,15 @@ namespace Map { `movsx eax, word ptr [esi + 0x5a]`, where sharing the `short` that later holds the percentages costs a `movzx` plus a `movsx`. */ + /* + The tribe stays a subscript at each access. The original computes its address + once in the prologue (`lea esi, [eax + ecx]`) and then reloads `this` from a + stack slot for the call in the loop (`mov ecx, [esp + 0x24]`), where we hold + `this` in a register and spill the loop counter instead - so a + `TribesState*` base of our own looks like the way to free it. It is not: + measured 47.2% -> 45.2%. MSVC commons the base out of the subscripts anyway + and the explicit pointer only changes which register it picks. + */ int const _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; /* An `int`, not a `short`. The original keeps each percentage in a 32-bit diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index 447e6100bb..6369390af8 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -25,6 +25,11 @@ namespace Map { jump to a shared one. Ours emits that block at the end of the function instead, and that placement is what the remaining gap is mostly made of. + Writing the first chain positively with the whole rest of the function nested + inside it, so that the shared `return 0` is the fall-out rather than the taken + branch, measures 18.8% -> 18.8%. MSVC puts that epilogue at the end of the + function whichever way round the guard is written. + Splitting the chains into six separate `if`s is what took trySpawnAdditionalWildlifeForTribe from 87.2% to a full match, so it was worth retrying here. Measured with reccmp: 18.8% -> 17.4%. The earlier note against diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index 12bb3cf09f..8e2560635d 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -116,6 +116,13 @@ namespace Map { local_c = DAT_TroopValueState::instance.y; } LAB_0052142a: + /* + The flag stays, although its slot is the local our frame has and the + original's does not (`sub esp, 0xc` against our 0x10). Writing the one store + the success and failure paths share - `targetedBuildingTile = 0` - out in + each of them and dropping the flag measures 33.3% -> 23.0%: the shared tail + is what holds the block order together here. + */ bVar3 = 1; x = DAT_TroopValueState::instance.x; uVar6 = DAT_TroopValueState::instance.tile; diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index f7b2b09c06..dfcac54534 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -19,7 +19,7 @@ namespace Map { UnitTypeShort UVar1; uint _currentIndexInPathPlan; int local_8; - short _targetTribeMinSpeed; + ushort _targetTribeMinSpeed; int _unitID = unitID; *unitCurrentX = (int)DAT_UnitsState::instance.units[unitID].x; int _yOffb = (int)DAT_UnitsState::instance.units[unitID].y; @@ -39,18 +39,23 @@ namespace Map { original has `cmp dx, 1` and `cmp cx, 1` - sixteen bits wide, against the literal 1, each with a `jg` over a `mov reg, 1` - where `< 2` on an `int` gives a 32-bit `jge` against 2. Same test either way; one is the - instruction the original has. The second read is `movzx ecx, word ptr - [esi + ecx + 0x222]`, so it is a narrow load too. + instruction the original has. + + Both loads are `movzx` - `movzx ecx, word ptr [esi + ecx + 0x222]` at + 0x521576 - and both are then sign-extended (`movsx edx, dx` at 0x521566, + `movsx ecx, cx` at 0x52158a) before the test, so the field is an unsigned + word that the code treats as signed. Tribe::minimumMovementSpeed is `ushort` + for that reason and the casts here are what the sign extension is. */ - int _targetTribeMinSpeedBounded = (int)_targetTribeMinSpeed; - if (_targetTribeMinSpeed <= 1) { + int _targetTribeMinSpeedBounded = (short)_targetTribeMinSpeed; + if ((short)_targetTribeMinSpeed <= 1) { _targetTribeMinSpeedBounded = 1; } - short _tribeMinSpeed + ushort _tribeMinSpeed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; int iVar3 = _targetTribeMinSpeedBounded * 4; - int _tribeMinSpeedBounded = (int)_tribeMinSpeed; - if (_tribeMinSpeed <= 1) { + int _tribeMinSpeedBounded = (short)_tribeMinSpeed; + if ((short)_tribeMinSpeed <= 1) { _tribeMinSpeedBounded = 1; } int iVar4 = (int)DAT_UnitsState::instance.units[targetUnitID].x; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 4e6bd46fec..4401c3743f 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 60.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 60.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From 9f92dacb8d6f5c0780ae15eb5131aae0f5c92442 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 23 Oct 2026 01:30:02 +0200 Subject: [PATCH 147/202] measure: giveTribeMoveInstruction's missing frame slots do not exist 0x526676 reads the tile's logic word once and both tests store the same 1 into the same slot, so the original has one defensive-structure flag where the decompiler printed three variables. Writing that out, with and without the six modulo call sites, measures 23.4% and 28.3% against the 30.0% we keep: every local removed costs a slot, which means our 0x34 frame is a different set of locals from the original's thirteen and matching its size was a coincidence. Four measurements recorded in the file so they are not re-run. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeMoveInstruction.cpp | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 19e83b9ce5..ca8f8a2831 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -67,9 +67,26 @@ namespace Map { changes nothing at all now (identical numbers), where on its own it used to move both figures. - Reverted, because raw is what the status file tracks. But the shape is not in - doubt any more: this function wants the six call sites and a 0x34 frame at the - same time, and that needs two locals the decompiler did not name. + Reverted, because raw is what the status file tracks. + + The two missing locals do not exist, and this is now settled rather than + suspected. 0x526676 reads the tile's logic word once into a register and then has + `test eax, 0x100; jz; mov [esp + 0x20], ebx` followed by + `test eax, 0x10000000; jz; mov [esp + 0x20], ebx` - two tests storing the same 1 + into the *same* slot - so the original has one defensive-structure flag where the + decompiler printed three variables and two loads. Writing that shape out, which + is what the original demonstrably does, measures: + + as written (three flags, `_modulo`) frame 0x34 raw 30.0% norm 22.5% + six call sites (three flags, no `_modulo`) frame 0x2c raw 28.9% norm 29.4% + one flag, `_modulo` frame 0x2c raw 23.4% norm 21.5% + one flag, six call sites frame 0x2c raw 28.3% norm 28.1% + + Each local removed costs a slot and the frame drops to 0x2c, so our 0x34 is made + of a different set of locals than the original's thirteen slots and matching the + size was a coincidence. A copy of the x parameter alongside `_y` does not add one + back - MSVC coalesces it. Normalized says plainly that the six call sites are the + right shape; raw cannot see past the displacement bytes. Do not re-run these four. */ // FUNCTION: STRONGHOLDCRUSADER 0x005263A0 undefined4 TribesState::giveTribeMoveInstruction( From 89ab8c31e0326b33a312c3cc32ac6214ece8ce74 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 23 Oct 2026 09:39:54 +0200 Subject: [PATCH 148/202] measure: the switch arms were inlined helpers, and inlining them back changes nothing Ghidra has no unnamed function in 0x521180 - 0x52b860, and each gap between consecutive TribesState addresses is the preceding method's own length, so the original emitted no out-of-line static. Extracting the ladder arm into a single-call-site static gets it inlined by MSVC and measures 60.6% -> 60.5%: an inlined body gets the same slots and registers as the same statements written inline, which is why the per-arm local split is neutral too. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../TribesState/giveTribeAnInstruction.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index ce20205678..8c8e04de5f 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -88,6 +88,28 @@ namespace Map { UIT_MAN_SIEGE_EQUIPMENT is emitted after our epilogue where the original has it inline at 0x529a8b - which no arrangement of the source reaches. */ + /* + The arms were not out-of-line helpers, and this is settled rather than assumed. + Ghidra has no unnamed function anywhere in 0x521180 - 0x52b860: every address in + that span is a named TribesState method and the gap between consecutive ones is + the preceding method's own length, so the original translation unit emitted no + static function of its own. Whatever helpers the 2002 source had, the compiler + inlined all of them. + + Inlining them back does not change the code either. The ladder arm - eighty-five + lines, nine locals of its own, one call site - was extracted into + + static BOOLEnum setLaddersAgainstWallTile( + TribesState* self, int tribeID, int _tribeSize, int _wallTile) + + with the arm reduced to `if (setLaddersAgainstWallTile(...)) return 1;`. MSVC + inlined it - no call appears in the diff and the function keeps its length - and + the score moved 60.6% -> 60.5%. An inlined static body therefore gets the same + slots and the same registers as the same statements written inline, which is also + why splitting the decompiler's folded scratch into a local per arm is exactly + neutral. Writing the arms as helpers is a readability choice with a small price, + not a way into the register allocation. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00527C80 undefined4 TribesState::giveTribeAnInstruction( int tribeID, UnitInstructionType unitInstructionType, int id, int unitUID, int param_5) From b90a47b8d48dfd7eaefb1546cf26440280e8ac41 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 23 Oct 2026 12:29:14 +0200 Subject: [PATCH 149/202] reimplement: playUnitCombatSpeechForTarget 40.6 -> 41.2, the selection counts are 4 The original compares the selected count against 4 twice - `cmp .., 4` with a jg and with a jl - where `< 5` and `3 <` give cmp 5 and cmp 3. A looser off-by-one scan that pairs constants across a whole function rather than within one diff block found these; the earlier scan had missed them because the blocks are misaligned. giveTribeAnInstruction has four more instructions of the same kind - two `cmp 8; jg`, one `cmp 0x10; jg`, and a cdq-based absolute value at 0x529bd6 - and correcting them costs 0.9 in every combination, so they stay wrong with the asm written out beside them. The parameter types are not the lever either: every parameter and local slot in the original is accessed as a dword, with only the odd word-wide read at a (short) cast, so the int-width signatures we have are right. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Units/TribesState/giveTribeAnInstruction.cpp | 16 ++++++++++++++++ .../playUnitCombatSpeechForTarget.cpp | 9 +++++++-- .../TribesState/predictUnitInterceptPosition.cpp | 7 +++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 8c8e04de5f..9dbd07e940 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -594,6 +594,22 @@ namespace Map { } while (iVar12 < this->tribes[tribeID].size); } iVar11 = unitInstructionType * 8; + /* + `< 9` stays, against the asm. The original has `cmp eax, 8` with + a `jg` at 0x52822b, where this gives `cmp 9` and a `jge`, and the + same at the other copy of this block. The height test further + down is the same story: 0x529bdb is `cmp eax, 0x10; jg` where + `< 0x11` gives `cmp 0x11; jge`, and 0x529bd6 is `cdq; xor eax, + edx; sub eax, edx` - a plain `x < 0 ? -x : x` - where the + decompiler's shift-and-xor gives the `sar` it was read back from. + + All four instructions are wrong in our output and correcting them + costs the same 0.9 points whichever subset is applied: the + constants alone, the absolute value alone, and all of them + together each measure 60.6% -> 59.7%. They sit in the same two + blocks, and the jump lengths those blocks are reached by are + worth more than the instructions themselves. + */ if (iVar11 < 9) { iVar11 = 8; } diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index 175cd82aff..0397e63ce9 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -154,7 +154,12 @@ namespace Map { _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( _otherX, _otherY, 0xea); - } else if (_selectedCount < 5) { + /* + `<= 4` and `>= 4` below, which are the `cmp .., 4` the original compares + against with a `jg` and a `jl`; `< 5` and `3 <` give `cmp 5`/`cmp 3` and + the other arm of each pair. + */ + } else if (_selectedCount <= 4) { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( @@ -192,7 +197,7 @@ namespace Map { (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x57); } - if (3 < _selectedCount_2) { + if (_selectedCount_2 >= 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( (int)DAT_UnitsState::instance.units[_otherUnitID].x, (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x58); diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index dfcac54534..a9765ffdbf 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -90,6 +90,13 @@ namespace Map { int _unitCurrentX_2 = *unitCurrentX; uint _xOff_2 = _unitCurrentX_2 - iVar4; uint _yOff_2 = _yOffb - iVar2; + /* + The shift-and-xor absolute values stay, although the original has + `cdq; xor eax, edx; sub eax, edx` at 0x521645 and 0x521654 - which is what + MSVC emits for `x < 0 ? -x : x`. Written that way, at both of the two sites, + the function measures 40.1% -> 27.3%: it produces the `cdq` and re-lays out + everything around it. + */ int _xOff_2b = (_xOff_2 ^ (int)_xOff_2 >> 0x1f) - ((int)_xOff_2 >> 0x1f); int _yOff_2b = (_yOff_2 ^ (int)_yOff_2 >> 0x1f) - ((int)_yOff_2 >> 0x1f); if (_xOff_2b > _yOff_2b) { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 4401c3743f..7d26c0c1b6 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 40.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 41.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending From d7a10a21c6fef634a5b460690da4b182255e4d88 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 24 Oct 2026 16:34:13 +0200 Subject: [PATCH 150/202] split: move the 44 fully matching functions to reimpl/TribesState-100 This branch now carries only the TribesState functions still below 100%, fifty of them, plus the OS::_memmove stub that upgradeTribeArrayLayoutForMapVersion needs and the batch tooling. The three header corrections stay here as well as on the new branch, because functions on both sides depend on them. The status lines for the moved functions go back to what upstream has, and the percentages for the ones that stay are the figures measured with the whole namespace implemented - which is what main will have once both branches land. Measured on this branch alone, two of them read slightly lower (giveTribeAnInstruction 60.6 -> 58.9, spawnDeerLionOrRabbit 74.1 -> 73.2) because their calls into the split-off functions now go through the resolver instead of straight to our own code. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01HtKbXQ7VMMh4CsZ1MwVEVX --- .../Map/Units/TribesState/addRallyPoint.cpp | 18 --- .../Units/TribesState/addUnitToNewTribe.cpp | 105 ------------------ .../Units/TribesState/addUnitToSelected.cpp | 2 +- .../TribesState/aiAssignNewUnitToTribe.cpp | 66 ----------- .../allUnitsReachedTheirDestination.cpp | 43 ------- ...ribeAreOutsideCoverageOfPathFindingAlg.cpp | 41 ------- .../applyMoveCommandOrRallyCommandToTribe.cpp | 76 ------------- .../applyMovementDistanceToUnitsInTribe.cpp | 36 ------ ...ToUnitsInTribeBasedOnUnitNumberInTribe.cpp | 35 ------ .../TribesState/applyTribeBehaviorType.cpp | 33 ------ .../Map/Units/TribesState/clearAllTribes.cpp | 28 ----- .../clearAnimalSpawnLocationsUnk.cpp | 37 ------ .../consumeFlaggedTribesOfType.cpp | 26 ----- .../TribesState/countDeerEfficiently.cpp | 46 -------- .../Units/TribesState/createPlayerTribe.cpp | 39 ------- .../createTribeWithSpawnedUnit.cpp | 37 ------ .../findRecentOrSignpostSpawnLocation.cpp | 52 --------- .../Units/TribesState/flagTribesOfType.cpp | 21 ---- .../getFirstUnitInTribeThatIsOnXTerrain.cpp | 39 ------- .../Map/Units/TribesState/getNonDyingUnit.cpp | 38 ------- .../TribesState/importStoredInfoFromSlot0.cpp | 50 --------- .../Units/TribesState/isTribeAllAssassins.cpp | 41 ------- .../isTribeFreeOfTunnelingUnits.cpp | 39 ------- .../moveTribeToNearbyClearTile.cpp | 57 ---------- .../Units/TribesState/removeAllTribeUnits.cpp | 43 ------- .../Map/Units/TribesState/respawnDeer.cpp | 80 ------------- .../TribesState/scatterTribeUnitsRandomly.cpp | 71 ------------ .../TribesState/selectionContainsHorses.cpp | 43 ------- .../Units/TribesState/setRabbitSpawnXY.cpp | 29 ----- .../TribesState/setStateForAllTribeUnits.cpp | 36 ------ .../TribesState/setTargetUnitForTribe.cpp | 42 ------- .../TribesState/spawnUnitsAroundLocation.cpp | 36 ------ .../TribesState/spawnUnitsForAITribe.cpp | 66 ----------- .../spawnWildlifeOrMercAtAvailableSlot.cpp | 71 ------------ .../TribesState/standUpAllTribeUnits.cpp | 43 ------- .../Units/TribesState/swapTribeOwnership.cpp | 31 ------ .../tribeContainsUnitThatCanClimb.cpp | 37 ------ .../tribeHasActiveLaddermanUnit.cpp | 42 ------- .../trySpawnAdditionalWildlifeForTribe.cpp | 86 -------------- .../TribesState/unitIsSelectedByPlayer.cpp | 21 ---- .../unsetRallyRelatedFlagOnUnits.cpp | 34 ------ .../updateAnimalHerdBehaviorState.cpp | 52 --------- .../updatePeasantSeatingAtBuilding.cpp | 73 ------------ .../TribesState/updateTribeRallyFlags.cpp | 53 --------- status/addresses-SHC-3BB0A8C1.txt | 86 +++++++------- 45 files changed, 44 insertions(+), 2036 deletions(-) delete mode 100644 src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp delete mode 100644 src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp b/src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp deleted file mode 100644 index c0a9ba8eae..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/addRallyPoint.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00522C50 - void TribesState::addRallyPoint(int section1016ID, short destinationX, short destinationY, int step) - { - this->tribes[section1016ID].rallyPointArray[step][0] = destinationX; - this->tribes[section1016ID].rallyPointArray[step][1] = destinationY; - this->tribes[section1016ID].rallyPointCount = (short)step + 1; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp deleted file mode 100644 index 77144d718f..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToNewTribe.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/AI/Tribes/AITribeType.hpp" -#include "OpenSHC/AI/Tribes/AIVUnitTypeTribeArrayOffset.hpp" -#include "OpenSHC/Game/GameMode.hpp" -#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" -#include "OpenSHC/Map/Units/States/UnitState.hpp" -#include "OpenSHC/Map/Units/UnitType.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" -#include "OpenSHC/Globals/DAT_TroopValueState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using AI::Tribes::AITribeType; - using Game::GameMode; - using Map::Units::SomeTribeBehaviorType; - using Map::Units::UnitType; - using Map::Units::States::UnitState; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x005254C0 - BOOLEnum TribesState::addUnitToNewTribe(uint unitID) - { - int _tribeID; - if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) { - return FALSE; - } - int _playerID = (int)DAT_UnitsState::instance.units[unitID].owner; - if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[_playerID] == -1 - && ((_tribeID = (int)DAT_UnitsState::instance.units[unitID].tribeID, - _tribeID <= 0 || (this->tribes[_tribeID].uid != DAT_UnitsState::instance.units[unitID].tribeUID)) - || (this->tribes[_tribeID].tribeState == 0))) { - int _newTribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(_playerID, 0); - if (0 < _newTribeID) { - UnitTypeShort _unitType = DAT_UnitsState::instance.units[unitID].unitType; - if (_unitType == Map::Units::UT_E_ARCHER) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_ARCHERS; - } else if (_unitType == Map::Units::UT_E_XBOW) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_CROSSBOWMEN; - } else if (_unitType == Map::Units::UT_E_SPEAR) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_SPEARMEN; - } else if (_unitType == Map::Units::UT_E_PIKE) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_PIKEMEN; - } else if (_unitType == Map::Units::UT_E_MACE) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_MACEMEN; - } else if (_unitType == Map::Units::UT_E_SWORD) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_SWORDSMEN; - } else if (_unitType == Map::Units::UT_E_KNIGHT) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_KNIGHTS; - } else if (_unitType == Map::Units::UT_E_LADDER) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_LADDERMEN; - } else if (_unitType == Map::Units::UT_E_ENGINEER) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_ENGINEERS; - } else if (_unitType == Map::Units::UT_HOPSFARMER) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_LADDERMEN; - } else if (_unitType == Map::Units::UT_TUNNELER) { - this->tribes[_newTribeID].tribeType = AI::Tribes::AITT_TUNNELERS; - } else if (_unitType == Map::Units::UT_A_ARCHER) { - this->tribes[_newTribeID].tribeType = ((AITribeType)0x19); - } else if (_unitType == Map::Units::UT_A_SLAVE) { - this->tribes[_newTribeID].tribeType = ((AITribeType)0x1a); - } else if (_unitType == Map::Units::UT_A_SLINGER) { - this->tribes[_newTribeID].tribeType = ((AITribeType)0x1b); - } else if (_unitType == Map::Units::UT_A_ASSASSIN) { - this->tribes[_newTribeID].tribeType = ((AITribeType)0x1c); - } else if (_unitType == Map::Units::UT_A_HARCHER) { - this->tribes[_newTribeID].tribeType = ((AITribeType)0x1d); - } else if (_unitType == Map::Units::UT_A_SWORDSMAN) { - this->tribes[_newTribeID].tribeType - = (AI::Tribes::AITribeTypeShort)AI::Tribes::OFFSET_CROSSBOWMAN; - } else if (_unitType == Map::Units::UT_A_FIRETHROWER) { - this->tribes[_newTribeID].tribeType = ((AITribeType)0x1f); - } else if (_unitType == Map::Units::UT_S_FBALLISTA) { - this->tribes[_newTribeID].tribeType = ((AITribeType)0x18); - } - this->tribes[_newTribeID].someIndex = 1; - this->tribes[_newTribeID].attackWave = (short)DAT_TroopValueState::instance.attackInfo.inv_count; - _unitType = DAT_UnitsState::instance.units[unitID].unitType; - if (_unitType == Map::Units::UT_E_ENGINEER) { - if (DAT_UnitsState::instance.units[unitID].state.generic - == Map::Units::States::US_STAND_UPUnk) { - this->tribes[_newTribeID].tribeBehaviorType - = Map::Units::STBT_0x410_SIEGE_EQUIPMENT_CONSTRUCTION; - } - } else if ((_unitType == Map::Units::UT_TUNNELER) - && (DAT_UnitsState::instance.units[unitID].state.generic - == Map::Units::States::US_LOOK_AROUNDUnk)) { - this->tribes[_newTribeID].tribeBehaviorType = Map::Units::STBT_0x415; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _newTribeID); - return TRUE; - } - } - return FALSE; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp index 05e6879029..4268378129 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToSelected.cpp @@ -12,7 +12,7 @@ namespace Map { { int const bitFlagsIndex = unitID / 16; int const bitMaskIndex = unitID % 16; - DAT_UnitsState::instance.selectedUnitsBitFlags[bitFlagsIndex] + ((short*)DAT_UnitsState::instance.selectedUnitsBitFlags)[bitFlagsIndex] |= DAT_UnitSelectionDefinedData::instance.BitMaskHelper[bitMaskIndex]; } diff --git a/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp deleted file mode 100644 index c55674e523..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/aiAssignNewUnitToTribe.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_GameState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x005237F0 - void TribesState::aiAssignNewUnitToTribe(int playerID, int unitType, int unitID) - { - int _index = 0; - if (unitType == 0x1e) { - /* - is engineer - */ - _index = 1; - } else if (unitType == 5) { - /* - is tunneler - */ - _index = 2; - } else if (unitType == 0x1d) { - /* - is ladderman - */ - _index = 3; - } else if (unitType == 0x25) { - /* - is monk - */ - int _monkTribe = DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk; - if (!_monkTribe || this->tribes[_monkTribe].tribeState == 0 - || this->tribes[_monkTribe].uid - != DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk) { - _monkTribe = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); - DAT_GameState::instance.playerDataArray[playerID].monkTribeIDUnk = _monkTribe; - DAT_GameState::instance.playerDataArray[playerID].monkTribeUIDUnk - = this->tribes[_monkTribe].uid; - } - /* - The monk arm is a block of its own in the disassembly, from 0x5238ac to - its own `ret 0xc` at 0x52390f: it never reaches the shared path below, so - a monk is added to one tribe rather than two. - */ - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _monkTribe); - return; - } - short* psVar1 = DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeIDs + _index; - int _tribe = (int)*psVar1; - if (!_tribe || this->tribes[_tribe].tribeState == 0 - || this->tribes[_tribe].uid - != DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeUIDs[_index]) { - _tribe = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); - *psVar1 = (short)_tribe; - DAT_GameState::instance.playerDataArray[playerID].freshUnitTribeUIDs[_index] - = this->tribes[_tribe].uid; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, _tribe); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp b/src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp deleted file mode 100644 index 264765943d..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/allUnitsReachedTheirDestination.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitsState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x005253F0 - BOOLEnum TribesState::allUnitsReachedTheirDestination(int tribeID) - { - int unitID; - BOOLEnum BVar1; - int unitSelectionIndex = 0; - if (0 < this->tribes[tribeID].size) { - do { - unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[unitID].dying == 0)) { - BVar1 = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::hasUnitReachedDestination, DAT_UnitsState::ptr)(unitID); - if (!BVar1) { - return FALSE; - } - } - } while (unitSelectionIndex < this->tribes[tribeID].size); - } - return TRUE; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp b/src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp deleted file mode 100644 index 3e0d0ead44..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_TileMapState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x005250B0 - BOOLEnum TribesState::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg(int tribeID, int algTileFlag) - { - int _unitID; - int iVar1 = (int)this->tribes[tribeID].size; - int unitSelectionIndex = 0; - if (0 < iVar1) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_TileMapState::instance.WalkLayer[DAT_UnitsState::instance.units[_unitID].tile] - != algTileFlag) { - return FALSE; - } - } while (unitSelectionIndex < iVar1); - } - return TRUE; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp deleted file mode 100644 index ac159aebc3..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/applyMoveCommandOrRallyCommandToTribe.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitsState.func.hpp" -#include "OpenSHC/Map/Units/States/UnitState.hpp" -#include "OpenSHC/Map/Units/UnitInstructionType.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_PathFindingState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitInstructionType; - using Map::Units::UnitLogicState; - using Map::Units::States::UnitState; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x00524340 - undefined4 TribesState::applyMoveCommandOrRallyCommandToTribe( - int tribeID, undefined4 x1, undefined4 y1, undefined4 isRallying, int storeAsRallyPoint) - { - int _unitID; - int _unitSelectionIndex = 0; - BOOLEnum _allAssassins - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); - this->tribes[tribeID].someUnitID = 0; - if (storeAsRallyPoint) { - int _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; - this->tribes[tribeID].isRallyingUnk = (short)isRallying; - this->tribes[tribeID].rallyPointArray[0][0] = DAT_UnitsState::instance.units[_targetUnitID].x; - this->tribes[tribeID].rallyPointArray[0][1] = DAT_UnitsState::instance.units[_targetUnitID].y; - this->tribes[tribeID].rallyPointArray[1][0] = (short)x1; - this->tribes[tribeID].rallyPointArray[1][1] = (short)y1; - this->tribes[tribeID].currentRallyPointIndex = 1; - this->tribes[tribeID].rallyPointCount = 2; - } - if (0 < this->tribes[tribeID].size) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitSelectionIndex); - _unitSelectionIndex = _unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 - && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0 - && DAT_UnitsState::instance.units[_unitID].state.generic != Map::Units::States::US_MELEE_ATTACK - && DAT_UnitsState::instance.units[_unitID].moveableUnk != 0 - && DAT_UnitsState::instance.units[_unitID].moveRelatedFlag != 1) { - DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)x1; - DAT_UnitsState::instance.units[_unitID].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[_unitID].targetingType - = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; - DAT_UnitsState::instance.units[_unitID]._someX_2 = 0; - DAT_UnitsState::instance.units[_unitID]._someY_2 = 0; - DAT_UnitsState::instance.units[_unitID].plannedDestinationY = (short)y1; - DAT_UnitsState::instance.units[_unitID].moveDelay = 0; - DAT_UnitsState::instance.units[_unitID].moveInstructionSpeedDelayTracker = 0; - if (_allAssassins) { - DAT_PathFindingState::instance.allAssassinsUnk = 1; - } - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( - _unitID, (uint)((short)x1), (uint)((short)y1), 0); - } - } while (_unitSelectionIndex < this->tribes[tribeID].size); - } - return (undefined4)(1); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp deleted file mode 100644 index 4ce40bf6c8..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribe.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_TileMapState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00525130 - void TribesState::applyMovementDistanceToUnitsInTribe(int param_1) - { - int iVar1; - int unitSelectionIndex = 0; - if (0 < this->tribes[param_1].size) { - do { - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { - DAT_UnitsState::instance.units[iVar1].movementDistance - = DAT_TileMapState::instance.CertainPathLayer[DAT_UnitsState::instance.units[iVar1].tile]; - } - } while (unitSelectionIndex < this->tribes[param_1].size); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp deleted file mode 100644 index aee7cd5fc4..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x005251A0 - void TribesState::applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe(int param_1) - { - int iVar1; - int unitSelectionIndex = 0; - if (0 < this->tribes[param_1].size) { - do { - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { - DAT_UnitsState::instance.units[iVar1].movementDistance - = DAT_UnitsState::instance.units[iVar1].idInTribe; - } - } while (unitSelectionIndex < this->tribes[param_1].size); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp b/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp deleted file mode 100644 index 322226c58e..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/applyTribeBehaviorType.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_TribesState.hpp" -#include "OpenSHC/Globals/DAT_TroopValueState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x005221A0 - void TribesState::applyTribeBehaviorType(int attackWave, SomeTribeBehaviorType tribeBehaviorType) - { - int iVar1 = DAT_TroopValueState::instance.attackInfo.someCounter1; - /* - The original walks the global instance, not `this`: it loads the tribe - address as an absolute (`mov eax, TribesState::instance + 1520`) and ends the - loop against another absolute rather than a `this`-relative one. - */ - for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { - Tribe& _tribe = DAT_TribesState::instance.tribes[_tribeID]; - if (_tribe.tribeState && _tribe.attackWave == attackWave - && (int)(short)_tribe.tribeBehaviorType != tribeBehaviorType - && _tribe.attackInfo_someCounter1 != iVar1) { - _tribe.tribeBehaviorType = (SomeTribeBehaviorTypeShort)tribeBehaviorType; - _tribe.unknownAttackRelatedUpdateCounter = 0; - } - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp b/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp deleted file mode 100644 index 182663c1f5..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/clearAllTribes.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/IO/LowLevelMemory.func.hpp" - -#include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" -#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x005211B0 - void TribesState::clearAllTribes() - { - this->clans = 0; - this->field2_0x8 = 1; - this->field9_0x24 = 0; - for (DAT_CurrentTribeID::instance = 1; (int)DAT_CurrentTribeID::instance < 1250; - DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1) { - MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - sizeof(Tribe), '\0', &this->tribes[DAT_CurrentTribeID::instance]); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp b/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp deleted file mode 100644 index 915d74f001..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/clearAnimalSpawnLocationsUnk.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Map/Location/Point4ShortXY.hpp" - -#include "OpenSHC/Globals/DAT_GameState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - using Map::Location::Point4ShortXY; - - // FUNCTION: STRONGHOLDCRUSADER 0x005232E0 - void TribesState::clearAnimalSpawnLocationsUnk() - { - /* - The import walked a cursor over deerSpawnLocationsXY and reached the other - three arrays through offsets from it (0x3ae to lionLocationsXY, 0x1d3e to - rabbitSpawnXY, 0x1d52 to camelSpawnXY), ending on the absolute address - 0x117d1de. All four hold four XY pairs. - */ - for (int _slot = 0; _slot < 4; _slot = _slot + 1) { - DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_slot].x = 0; - DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_slot].y = 0; - DAT_GameState::instance.mapAndTime.lionLocationsXY[_slot].x = 0; - DAT_GameState::instance.mapAndTime.lionLocationsXY[_slot].y = 0; - DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0] = 0; - DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1] = 0; - DAT_GameState::instance.mapAndTime.camelSpawnXY[_slot].x = 0; - DAT_GameState::instance.mapAndTime.camelSpawnXY[_slot].y = 0; - } - DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor = 0; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp b/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp deleted file mode 100644 index e65ce8ebfc..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/consumeFlaggedTribesOfType.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00523790 - void TribesState::consumeFlaggedTribesOfType(int param_1, int param_2) - { - for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { - if (param_2 <= 0) { - return; - } - Tribe& _tribe = this->tribes[_tribeID]; - if (_tribe.tribeState != 0 && _tribe.tribeType == param_1 && _tribe.unknownBool02 != 0) { - _tribe.unknownBool02 = 0; - _tribe.unknownBool01 = 1; - param_2 = param_2 + -1; - } - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp b/src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp deleted file mode 100644 index 8459eac662..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/countDeerEfficiently.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/Map/Units/UnitType.hpp" - -#include "OpenSHC/Globals/DAT_GameState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using Map::Units::UnitType; - - // FUNCTION: STRONGHOLDCRUSADER 0x005236A0 - void TribesState::countDeerEfficiently() - { - DAT_GameState::instance.mapAndTime.aliveDeerCount = 0; - /* - The original walks the unit array three at a time with the test written out once - per unit, so the three blocks below are kept separate rather than folded. - */ - for (int i = 1; i < 2500; i += 3) { - Unit* unit = &DAT_UnitsState::instance.units[i]; - if (unit[0].logicalState == Map::Units::ULS_NORMAL && unit[0].dying == 0 - && unit[0].unitType == Map::Units::UT_ANTELOPESHDEER) { - DAT_GameState::instance.mapAndTime.aliveDeerCount - = DAT_GameState::instance.mapAndTime.aliveDeerCount + 1; - } - if (unit[1].logicalState == Map::Units::ULS_NORMAL && unit[1].dying == 0 - && unit[1].unitType == Map::Units::UT_ANTELOPESHDEER) { - DAT_GameState::instance.mapAndTime.aliveDeerCount - = DAT_GameState::instance.mapAndTime.aliveDeerCount + 1; - } - if (unit[2].logicalState == Map::Units::ULS_NORMAL && unit[2].dying == 0 - && unit[2].unitType == Map::Units::UT_ANTELOPESHDEER) { - DAT_GameState::instance.mapAndTime.aliveDeerCount - = DAT_GameState::instance.mapAndTime.aliveDeerCount + 1; - } - } - } - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp deleted file mode 100644 index f387a63f7c..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/createPlayerTribe.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/IO/LowLevelMemory.func.hpp" -#include "OpenSHC/AI/Tribes/AITribeType.hpp" -#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" - -#include "OpenSHC/Globals/DAT_GameCore.hpp" -#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using AI::Tribes::AITribeType; - using Map::Units::SomeTribeBehaviorType; - - // FUNCTION: STRONGHOLDCRUSADER 0x00522950 - int TribesState::createPlayerTribe(int playerID, undefined4 one, int tribeID) - { - MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - 820, '\0', (void*)((int)(this->tribes + tribeID))); - this->tribes[tribeID].tribeState = 2; - this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; - DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; - this->tribes[tribeID].tribeType = ((AITribeType)0); - this->tribes[tribeID].field18_0x1c = 0; - this->tribes[tribeID].field19_0x1e = 0; - this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); - this->tribes[tribeID].field29_0x30 = 0; - this->tribes[tribeID].selectionTargetUnitID = 0; - this->tribes[tribeID].size = 0; - return tribeID; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp deleted file mode 100644 index 1b055c4dcf..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/createTribeWithSpawnedUnit.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_PathFindingState.hpp" -#include "OpenSHC/Globals/DAT_TroopValueState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00523190 - dword TribesState::createTribeWithSpawnedUnit( - short someIndex, undefined4 tribeType, int x, int y, int playerID, UnitType unitType, int count) - { - if ((x <= 0) && (y <= 0)) { - return (dword)(0); - } - dword _tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); - if ((int)_tribeID <= 0) { - return (dword)(0); - } - this->tribes[_tribeID].tribeType = (AITribeTypeShort)tribeType; - this->tribes[_tribeID].someIndex = someIndex; - this->tribes[_tribeID].attackWave = (short)DAT_TroopValueState::instance.attackInfo.inv_count; - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSuitableSpawnLocationUnk, - DAT_PathFindingState::ptr)(x, y, -1, -1, 2000, 0); - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::spawnUnitAndAddToTribe, - DAT_PathFindingState::ptr)(playerID, playerID, count, unitType, (undefined4)((int)(_tribeID))); - this->tribes[_tribeID].pendingScatterMove = 1; - return (dword)(_tribeID); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp b/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp deleted file mode 100644 index 3cbf42c933..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/findRecentOrSignpostSpawnLocation.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_GameState.hpp" -#include "OpenSHC/Globals/DAT_TileMapState.hpp" -#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00523410 - undefined4 TribesState::findRecentOrSignpostSpawnLocation(uint* param_1, uint* param_2) - { - int iVar4 = 0; - do { - uint uVar2 = (DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor + iVar4) % 4; - short sVar1 = DAT_GameState::instance.mapAndTime.lionLocationsXY[uVar2].x; - if (sVar1) { - *param_1 = (int)sVar1; - uVar2 = (DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor + iVar4) % 4; - uint uVar3 = (uint)DAT_GameState::instance.mapAndTime.lionLocationsXY[uVar2].y; - *param_2 = uVar3; - uVar2 = *param_1; - if (uVar2 <= 399 && uVar3 <= 399 - && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[uVar3 * 400 + uVar2] != 0 - && !(DAT_TileMapState::instance - .LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[uVar3].addXgetTile - + uVar2] - & 0x4a5014b1U)) { - uVar2 = (DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor + 1 + iVar4) % 4; - DAT_GameState::instance.mapAndTime.animalSpawnSlotCursor = (short)uVar2; - return (undefined4)(1); - } - } - iVar4 = iVar4 + 1; - if (4 <= iVar4) { - for (iVar4 = 0; iVar4 < 8; iVar4++) { - if (DAT_GameState::instance.mapAndTime.signpostIDs[iVar4] != 0) { - *param_1 = DAT_GameState::instance.mapAndTime.signpostsMapEdge[iVar4][0].x; - *param_2 = DAT_GameState::instance.mapAndTime.signpostsMapEdge[iVar4][0].y; - return (undefined4)(1); - } - } - return (undefined4)(0); - } - } while (true); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp b/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp deleted file mode 100644 index 345a54475f..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/flagTribesOfType.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00523750 - void TribesState::flagTribesOfType(int param_1) - { - for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { - Tribe& _tribe = this->tribes[_tribeID]; - if (_tribe.tribeState && (short)_tribe.tribeType == param_1) { - _tribe.unknownBool02 = 1; - } - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp b/src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp deleted file mode 100644 index 2087bb1a86..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/getFirstUnitInTribeThatIsOnXTerrain.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_TileMapState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x005240D0 - int TribesState::getFirstUnitInTribeThatIsOnXTerrain(int selectionID) - { - int _unitID; - int iVar1 = (int)this->tribes[selectionID].size; - int _unitSelectionIndex = 0; - if (0 < iVar1) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - selectionID, _unitSelectionIndex); - _unitSelectionIndex = _unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && (DAT_TileMapState::instance.LogicLayer[DAT_UnitsState::instance.units[_unitID].tile] - & 0x40000000U)) { - return _unitID; - } - } while (_unitSelectionIndex < iVar1); - } - return 0; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp b/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp deleted file mode 100644 index 908cd31c8c..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/getNonDyingUnit.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00523630 - int TribesState::getNonDyingUnit(int tribeID) - { - int iVar1; - if (tribeID <= 0) { - return 0; - } - int unitSelectionIndex = 0; - int _size = this->tribes[tribeID].size; - do { - if (unitSelectionIndex >= _size) { - return 0; - } - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - } while (((DAT_UnitsState::instance.units[iVar1].logicalState != Map::Units::ULS_NORMAL) - || (DAT_UnitsState::instance.units[iVar1].dying != 0)) - || (DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue != 1)); - return iVar1; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp b/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp deleted file mode 100644 index befc3a1e2c..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/importStoredInfoFromSlot0.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00522DD0 - void TribesState::importStoredInfoFromSlot0(undefined4 param_1, int tribeID) - { - /* - /* - The copy is the fall-through and the stance block goes last. The original - jumps away when the flag is clear (`je 0xe9`); the block at 0x522ed0 copies - the stance and ends in its own `ret 8`, reaching the field through an - unfolded `[eax + ecx + 0x2e0]` rather than the folded base the rest of the - function uses. Written `if (!flag)` the addressing already came out - unfolded, but MSVC placed that block inline at the top and sent the whole - copy into the far block instead. - */ - if (this->tribeCopiedToSlot0) { - this->tribes[tribeID].field166_0x2be = this->tribes[0].field166_0x2be; - this->tribes[tribeID].someUnitID = this->tribes[0].someUnitID; - this->tribes[tribeID].someUnitUID = this->tribes[0].someUnitUID; - this->tribes[tribeID].someTile = this->tribes[0].someTile; - this->tribes[tribeID].someTile2 = this->tribes[0].someTile2; - this->tribes[tribeID].currentRallyPointIndex = this->tribes[0].currentRallyPointIndex; - this->tribes[tribeID].rallyPointCount = this->tribes[0].rallyPointCount; - this->tribes[tribeID].isRallyingUnk = this->tribes[0].isRallyingUnk; - this->tribes[tribeID].unitStance = this->tribes[0].unitStance; - /* - The original unrolls the inner loop over the ten rally points and keeps - the outer one over the two coordinates, stepping its cursor by one short - between the two passes. Ten is right: the copy covers 0x260 to 0x287, - which is where `rallyPointArray` ends. - */ - for (int _coordinate = 0; _coordinate < 2; _coordinate = _coordinate + 1) { - for (int _point = 0; _point < 10; _point = _point + 1) { - this->tribes[tribeID].rallyPointArray[_point][_coordinate] - = this->tribes[0].rallyPointArray[_point][_coordinate]; - } - } - return; - } - this->tribes[tribeID].unitStance = this->tribes[0].unitStance; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp deleted file mode 100644 index 01756940c2..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/isTribeAllAssassins.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/Map/Units/UnitType.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using Map::Units::UnitType; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x00524060 - BOOLEnum TribesState::isTribeAllAssassins(int tribeID) - { - int _unitID; - int iVar1 = (int)this->tribes[tribeID].size; - int _unitSelectionIndex = 0; - if (0 < iVar1) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitSelectionIndex); - _unitSelectionIndex = _unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].unitType != Map::Units::UT_A_ASSASSIN) { - return FALSE; - } - } while (_unitSelectionIndex < iVar1); - } - return TRUE; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp deleted file mode 100644 index b47db42884..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/isTribeFreeOfTunnelingUnits.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x00525210 - BOOLEnum TribesState::isTribeFreeOfTunnelingUnits(int param_1, int param_2) - { - int iVar2; - int iVar1 = (int)this->tribes[param_1].size; - int unitSelectionIndex = 0; - if (0 < iVar1) { - do { - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar2].dying == 0 - && DAT_UnitsState::instance.units[iVar2].tunnelerFinishedDigging == 2) { - return FALSE; - } - } while (unitSelectionIndex < iVar1); - } - return TRUE; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp deleted file mode 100644 index 583f79c470..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToNearbyClearTile.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Navigation/Algorithms/XYPair.hpp" -#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" - -#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" -#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Navigation::Algorithms::XYPair; - using Map::Units::Instructions::UnitMatchSpeedEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x0052A640 - void TribesState::moveTribeToNearbyClearTile(int param_1) - { - int sVar1 = this->tribes[param_1].selectionTargetUnitID; - uint uVar5 = (uint)DAT_UnitsState::instance.units[sVar1].x; - uint uVar2 = (uint)DAT_UnitsState::instance.units[sVar1].y; - /* - The import walked a cursor over the candidate offsets and left the loop's exit - test as `if (0xb4df43 < (int)piVar4) {}`, so it fell out of the `while (true)` - only through the `break` and then issued a move order with whatever x1/y1 the - last candidate had left behind. The original returns without an order when no - candidate is reachable. 0xb4df44 is one past field1608_0xcc4[39].y, so the - loop runs over all 40 entries. - */ - /* - A cursor against the literal end address, as in the sibling - moveTribeToIndexedNearbyTile: an index over the array makes the bound a - relocated `&field1608_0xcc4[40]` where the original has a plain - `cmp ebx, 0xb4df44`. - */ - int* _pCandidate = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[0].y; - do { - uint const x1 = _pCandidate[-1] + uVar5; - uint const y1 = *_pCandidate + uVar2; - if (x1 <= 399 && y1 <= 399 - && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] != 0 - && MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( - param_1, uVar5, uVar2, x1, y1) - != 0) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - param_1, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); - return; - } - _pCandidate = _pCandidate + 2; - } while ((int)_pCandidate < 0xb4df44); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp deleted file mode 100644 index 3798769449..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/removeAllTribeUnits.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - /* - The size is read through a pointer the original takes once - (`lea ebx, [eax + edi + 0x5c]`) and re-reads at the bottom of the loop; the - decompiler's shuffle through a single variable is an artefact of the index and - the unit id sharing no register. - */ - // FUNCTION: STRONGHOLDCRUSADER 0x00525470 - void TribesState::removeAllTribeUnits(int param_1) - { - int _index = 0; - short* const _size = &this->tribes[param_1].size; - if (0 < *_size) { - do { - /* - A post-increment: the original copies the index out before bumping it - (`mov eax, esi`, `push eax`, then `add esi, 1` before the call at - 0x525499), which is what the decompiler printed as a spare variable - carrying the old value across the call. - */ - int const _unitID - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, _index++); - DAT_UnitsState::instance.units[_unitID].logicalState = Map::Units::ULS_REMOVE; - } while (_index < (int)*_size); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp b/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp deleted file mode 100644 index 73509d1662..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/respawnDeer.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/LandscapeState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Rendering/ViewportRenderState.func.hpp" -#include "OpenSHC/Commands/MappersEnum.hpp" -#include "OpenSHC/Game/GameMode.hpp" -#include "OpenSHC/Game/GameMode2.hpp" -#include "OpenSHC/Map/MapType2.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_GameCore.hpp" -#include "OpenSHC/Globals/DAT_GameState.hpp" -#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" -#include "OpenSHC/Globals/DAT_LandscapeState.hpp" -#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" -#include "OpenSHC/Globals/DAT_TileMapState.hpp" -#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Commands::MappersEnum; - using Game::GameMode; - using Game::GameMode2; - using Map::MapType2; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x005261B0 - void TribesState::respawnDeer() - { - BOOLEnum BVar1; - uint _x; - uint _y; - if (DAT_GameCore::instance.gameMode_2 != Game::GM_EDITOR - && DAT_GameCore::instance.gameMode_2 != Game::GM_SIEGE_THAT - && (DAT_GameCore::instance.gameMode_2 != Game::GM_BUILDERUnk - || (DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 != Map::MT_SIEGE)) - && DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY - && DAT_GameState::instance.mapAndTime.aliveDeerCount - && DAT_GameState::instance.mapAndTime.deerCount < 6) { - /* - One `for` over five spawn candidates, where the import had a `while (true)` - with an `if (4 < _counter) {}` that never left it and a goto back in from - the tile check. The original ends the loop on `cmp ebp, 5; jl`, and spawns - at most one deer before returning. - */ - for (int _counter = 0; _counter < 5; _counter = _counter + 1) { - if (_counter < 4) { - _x = (int)DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_counter].x; - _y = (int)DAT_GameState::instance.mapAndTime.deerSpawnLocationsXY[_counter].y; - } else { - _x = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].x; - _y = DAT_GameState::instance.mapAndTime.signpostsMapEdge[0][0].y; - } - BVar1 = MACRO_CALL_MEMBER( - Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)(_x, _y); - if (!BVar1) { - continue; - } - int _tile = DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile + _x; - if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x50501581U)) { - int const _organismID = DAT_TileMapState::instance.OrganismLayer[_tile]; - if (_organismID == 0) { - continue; - } - MACRO_CALL_MEMBER(Map::LandscapeState_Func::removeTree, DAT_LandscapeState::ptr)( - _organismID); - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( - Commands::M_MAPPER_DEER, _x, _y, (int)(DAT_TileMapState::instance.HeightLayer[_tile])); - return; - } - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp b/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp deleted file mode 100644 index 2c473132e3..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/scatterTribeUnitsRandomly.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/States/UnitState.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_PathFindingState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" -#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using Map::Units::States::UnitState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00523920 - int TribesState::scatterTribeUnitsRandomly(int param_1) - { - int iVar2; - int iVar3; - if (!param_1) { - return 0; - } - int sVar1 = this->tribes[param_1].selectionTargetUnitID; - int unitSelectionIndex = 0; - int index = 100; - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSuitableSpawnLocationUnk, - DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[sVar1].x, - (int)(DAT_UnitsState::instance.units[sVar1].y), -1, -1, 5000, 0); - iVar2 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::getComputationalTileIndex, DAT_PathFindingState::ptr)(); - int iVar4 = (int)this->tribes[param_1].size; - /* - An early return, which is where the original puts its own `xor eax, eax`. - */ - if (iVar2 < iVar4 * 0x10 + 200) { - return 0; - } - if (0 < iVar4) { - do { - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar2].dying == 0)) { - index = index + 1 + (DAT_UnitsState::instance.units[iVar2].fixedRng & 0xf); - iVar3 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::getTileInSearchQueue, - DAT_PathFindingState::ptr)(index); - /* - One sign-extended read shared by both uses: the original has a - single `movsx ecx, word ptr [..]` and stores its low word, where - reading the `short` array twice leaves a `movzx` plus a `movsx`. - */ - int const _targetTileY - = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[iVar3]; - iVar4 = DAT_ViewportRenderState::instance.translationMatrix[_targetTileY].addXgetTile; - DAT_UnitsState::instance.units[iVar2].targetY = (short)_targetTileY; - DAT_UnitsState::instance.units[iVar2].targetX = (short)iVar3 - (short)iVar4; - DAT_UnitsState::instance.units[iVar2].state.generic = ((UnitStateShort)0xd1); - } - } while (unitSelectionIndex < this->tribes[param_1].size); - } - return 1; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp b/src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp deleted file mode 100644 index 5892abc3a4..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/selectionContainsHorses.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/Map/Units/UnitType.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using Map::Units::UnitType; - using WindowsHelper::Enums::BOOLEnum; - - // FUNCTION: STRONGHOLDCRUSADER 0x00524230 - BOOLEnum TribesState::selectionContainsHorses(int param_1) - { - UnitTypeShort UVar1; - int iVar2; - int iVar3 = (int)this->tribes[param_1].size; - int unitSelectionIndex = 0; - if (0 < iVar3) { - do { - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[iVar2].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar2].dying == 0 - && (UVar1 = DAT_UnitsState::instance.units[iVar2].unitType, - UVar1 == Map::Units::UT_A_HARCHER || (UVar1 == Map::Units::UT_E_KNIGHT))) { - return TRUE; - } - } while (unitSelectionIndex < iVar3); - } - return FALSE; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp b/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp deleted file mode 100644 index cf8b15d107..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/setRabbitSpawnXY.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_GameState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x005233A0 - void TribesState::setRabbitSpawnXY(undefined4 x, undefined4 y) - { - /* - 0x117ef1c is one past rabbitSpawnXY[3], so the cursor and its magic end - address are a plain four-entry loop. - */ - for (int _slot = 0; _slot < 4; _slot = _slot + 1) { - if (DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0] == 0) { - DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0] = (short)x; - DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1] = (short)y; - } - } - DAT_GameState::instance.mapAndTime.rabbitSpawnXY[0][0] = (short)x; - DAT_GameState::instance.mapAndTime.rabbitSpawnXY[0][1] = (short)y; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp deleted file mode 100644 index 9970c47a0c..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/setStateForAllTribeUnits.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00525300 - void TribesState::setStateForAllTribeUnits(int param_1, undefined4 param_2) - { - int iVar1; - int unitSelectionIndex = 0; - if (0 < this->tribes[param_1].size) { - do { - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { - DAT_UnitsState::instance.units[iVar1].state.generic = (short)param_2; - DAT_UnitsState::instance.units[iVar1].animationCycleNumber = 0; - DAT_UnitsState::instance.units[iVar1].seated = 0; - } - } while (unitSelectionIndex < this->tribes[param_1].size); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp deleted file mode 100644 index 1f44a08a5d..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/setTargetUnitForTribe.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00525280 - void TribesState::setTargetUnitForTribe(int tribeID) - { - int _unitID; - int _unitSelectionIndex = 0; - short _tribeSize = this->tribes[tribeID].size; - this->tribes[tribeID].selectionTargetUnitID = 0; - if (0 < _tribeSize) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitSelectionIndex); - _unitSelectionIndex = _unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[_unitID].dying == 0)) { - short _tribeTargetUnitID = this->tribes[tribeID].selectionTargetUnitID; - if (!_tribeTargetUnitID) { - this->tribes[tribeID].selectionTargetUnitID = (short)_unitID; - DAT_UnitsState::instance.units[_unitID].selectionTargetUnitID = (short)_unitID; - } else { - DAT_UnitsState::instance.units[_unitID].selectionTargetUnitID = _tribeTargetUnitID; - } - } - } while (_unitSelectionIndex < this->tribes[tribeID].size); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp deleted file mode 100644 index aff326bfa7..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/spawnUnitsAroundLocation.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_PathFindingState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00523240 - dword TribesState::spawnUnitsAroundLocation( - undefined4 param_1, int aroundX, int aroundY, int playerID, UnitType unitType, int count) - { - if ((aroundX <= 0) && (aroundY <= 0)) { - return (dword)(0); - } - dword _tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(playerID, 0); - if ((int)_tribeID <= 0) { - return (dword)(0); - } - this->tribes[_tribeID].someIndex = 0; - this->tribes[_tribeID].attackWave = 0; - this->tribes[_tribeID].tribeType = (AITribeTypeShort)param_1; - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSuitableSpawnLocationUnk, - DAT_PathFindingState::ptr)(aroundX, aroundY, -1, -1, 2000, 0); - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::spawnUnitAndAddToTribe, - DAT_PathFindingState::ptr)(playerID, playerID, count, unitType, (undefined4)((int)(_tribeID))); - this->tribes[_tribeID].pendingScatterMove = 1; - return (dword)(_tribeID); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp b/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp deleted file mode 100644 index 2805e6f35e..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/spawnUnitsForAITribe.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/AI/AICState.func.hpp" -#include "OpenSHC/Map/Units/UnitsState.func.hpp" -#include "OpenSHC/Map/Units/UnitType.hpp" - -#include "OpenSHC/Globals/DAT_AICState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitType; - - // FUNCTION: STRONGHOLDCRUSADER 0x00522F70 - void TribesState::spawnUnitsForAITribe( - undefined4 param_1, int param_2, int param_3, int param_4, UnitType param_5, int param_6, int param_7) - { - int unitID; - if (((0 < param_2) || (0 < param_3)) && 0 < param_6) { - /* - The Y scaling is computed first: the original has `lea ebx, [edx*8]` for - param_3 and `lea ebp, [ecx*8]` for param_2, which is the opposite register - pairing from declaring X first. - */ - int microYPosition = param_3 * 8; - int microXPosition = param_2 * 8; - param_2 = param_6; - do { - unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( - param_4, param_4, microXPosition, microYPosition, 8, param_5); - if (unitID) { - DAT_UnitsState::instance.units[unitID].calculatedOwnerPlayerIndex = param_7; - UnitTypeShort UVar1 = DAT_UnitsState::instance.units[unitID].unitType; - /* - The call is written out in both arms. The original pushes the - behaviour constant straight from each one - `push 0x14`, - `jmp 0x2`, `push 0x11` - and shares the `push eax` and the call - after them, which is what MSVC produces when it tail-merges two - call sites. Assigning a local and calling once makes it - materialise the value instead (`mov ecx, 0x14` / `mov ecx, 0x11` - and a `push ecx`), five bytes more, and that is what every - branch displacement in this function was out by. Declaring the - local at its use rather than at function scope does not change - it; the two call sites do. - */ - if (UVar1 == Map::Units::UT_E_ARCHER || UVar1 == Map::Units::UT_E_XBOW - || UVar1 == Map::Units::UT_A_ARCHER || UVar1 == Map::Units::UT_A_SLINGER - || UVar1 == Map::Units::UT_A_HARCHER || UVar1 == Map::Units::UT_A_FIRETHROWER) { - MACRO_CALL_MEMBER(AI::AICState_Func::addUnitToItsTribe, DAT_AICState::ptr)( - unitID, 0x11); - } else { - MACRO_CALL_MEMBER(AI::AICState_Func::addUnitToItsTribe, DAT_AICState::ptr)( - unitID, 0x14); - } - } - param_2 = param_2 + -1; - } while (param_2); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp b/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp deleted file mode 100644 index 30a5a96415..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/spawnWildlifeOrMercAtAvailableSlot.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Commands/MappersEnum.hpp" -#include "OpenSHC/Game/GameMode.hpp" - -#include "OpenSHC/Globals/DAT_GameState.hpp" -#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" -#include "OpenSHC/Globals/DAT_TileMapState.hpp" -#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Commands::MappersEnum; - using Game::GameMode; - - // FUNCTION: STRONGHOLDCRUSADER 0x005262A0 - void TribesState::spawnWildlifeOrMercAtAvailableSlot() - { - if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY - || 0xa0 <= DAT_GameState::instance.mapAndTime.wildlifeCount) { - return; - } - /* - fixme: the merc slots' coordinate pair is reached through a pointer because - the generated header cannot express it. The original walks - mapAndTime + 0x1ddc in steps of 0x320 (400 shorts), which is a row stride - playerPopulationStatistics[9][300] does not have, and reads a dword either - side of the cursor. mercRecruitable is indexed [_slot + 4] for _slot 4..11, - which is past the end of its declared int[7] as well. Both arrays need - correcting in Ghidra before this can be written with named fields. - */ - /* - The slot counter is declared before the cursor rather than in the `for` - header: MSVC assigns registers in declaration order, and the original zeroes - the counter first (quick_diff 0.9245 -> 0.9434). - */ - int _slot = 0; - short* _mercSpawnXY = DAT_GameState::instance.mapAndTime.playerPopulationStatistics[6] + 0xe4; - for (; _slot < 0xc; _slot = _slot + 1, _mercSpawnXY = _mercSpawnXY + 400) { - uint x; - uint y; - if (_slot < 4) { - x = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0]; - y = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1]; - } else { - if (DAT_GameState::instance.mapAndTime.mercRecruitable[_slot + 4] == 0) { - continue; - } - x = *(uint*)(_mercSpawnXY + -2); - y = *(uint*)_mercSpawnXY; - } - if (x <= 399 && y <= 399 - && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] != 0) { - int const _tile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; - if ((DAT_TileMapState::instance.LogicLayer[_tile] & 0x50501581U) == 0) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( - Commands::M_MAPPER_RABBIT, x, y, (int)(DAT_TileMapState::instance.HeightLayer[_tile])); - this->unknownX_01 = x; - this->unknownY_01 = y; - return; - } - } - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp b/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp deleted file mode 100644 index 86ba276529..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/standUpAllTribeUnits.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitsState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00525370 - void TribesState::standUpAllTribeUnits(int param_1) - { - int iVar1; - /* - A cursor on the size field rather than on the tribe: diff_anchor's territory - - the original's `lea eax, [eax + ebp + 0x5c]` points at Tribe::size and reads - it as [eax], where a Tribe* reads it as [eax + 0x34]. - */ - short* _tribeSize = &this->tribes[param_1].size; - int unitSelectionIndex = 0; - if (0 < *_tribeSize) { - do { - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[iVar1].dying == 0)) { - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::standUpIfSeated, DAT_UnitsState::ptr)(iVar1); - DAT_UnitsState::instance.units[iVar1].animationCycleNumber = 0; - DAT_UnitsState::instance.units[iVar1].rabbitMovementSlowdown = '\0'; - } - } while (unitSelectionIndex < *_tribeSize); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp b/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp deleted file mode 100644 index 9dc3863afd..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/swapTribeOwnership.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_TribesState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00521240 - void TribesState::swapTribeOwnership(int param_1, int param_2) - { - /* - The original walks the global instance, not `this`: both the start and the - end of the loop are absolute addresses in the binary. - */ - for (int _tribeID = 1; _tribeID < 1250; _tribeID = _tribeID + 1) { - Tribe& _tribe = DAT_TribesState::instance.tribes[_tribeID]; - if (_tribe.tribeState == 2) { - if (_tribe.owner == param_1) { - _tribe.owner = param_2; - } else if (_tribe.owner == param_2) { - _tribe.owner = param_1; - } - } - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp b/src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp deleted file mode 100644 index 6118251442..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/tribeContainsUnitThatCanClimb.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x005241C0 - undefined4 TribesState::tribeContainsUnitThatCanClimb(int param_1) - { - int iVar1; - int iVar2 = (int)this->tribes[param_1].size; - int unitSelectionIndex = 0; - if (0 < iVar2) { - do { - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar1].dying == 0 - && DAT_UnitsState::instance.units[iVar1].unitCanClimb != 0) { - return (undefined4)(1); - } - } while (unitSelectionIndex < iVar2); - } - return (undefined4)(0); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp b/src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp deleted file mode 100644 index e99df02c5f..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/tribeHasActiveLaddermanUnit.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/States/UnitState.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/Map/Units/UnitType.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using Map::Units::UnitType; - using Map::Units::States::UnitState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00524140 - undefined4 TribesState::tribeHasActiveLaddermanUnit(int param_1) - { - int iVar1; - int iVar2 = (int)this->tribes[param_1].size; - int unitSelectionIndex = 0; - if (0 < iVar2) { - do { - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar1].dying == 0 - && DAT_UnitsState::instance.units[iVar1].unitType == Map::Units::UT_E_LADDER - && DAT_UnitsState::instance.units[iVar1].state.generic == (UnitState)3) { - return (undefined4)(1); - } - } while (unitSelectionIndex < iVar2); - } - return (undefined4)(0); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp deleted file mode 100644 index 43e29c72f8..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/trySpawnAdditionalWildlifeForTribe.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitsState.func.hpp" -#include "OpenSHC/Commands/MappersEnum.hpp" -#include "OpenSHC/UI/Enums/MenuViewType.hpp" - -#include "OpenSHC/Globals/DAT_GameCore.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" -#include "OpenSHC/Globals/SEC_RNG.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Commands::MappersEnum; - using UI::Enums::MenuViewType; - - // FUNCTION: STRONGHOLDCRUSADER 0x005260B0 - undefined4 TribesState::trySpawnAdditionalWildlifeForTribe(int param_1, int param_2, int param_3, int param_4) - { - /* - Four separate guards, not two `||` chains: the original emits a `return 0` for - the first of each pair inline and jumps back to it from the second, and the - three return blocks have different `pop` sequences because they sit at - different points in its register saves. - */ - if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { - return (undefined4)(0); - } - if (param_1 <= 0) { - return (undefined4)(0); - } - if (this->tribes[param_1].predatorNearby != 0) { - return (undefined4)(0); - } - if (DAT_GameCore::instance.missionNumber1to20 == 0x26 && param_4 == 0x2f) { - return (undefined4)(0); - } - { - int iVar7 = (int)this->tribes[param_1].selectionTargetUnitID; - int iVar6 = iVar7; - if (param_4 == 0x2c) { - iVar6 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(param_1); - if (!iVar6) { - iVar6 = iVar7; - } - } - int bVar5 = (byte)SEC_RNG::instance.currentNumber2 & 0x7f; - int sVar2 = DAT_UnitsState::instance.units[iVar6].x; - int sVar3 = DAT_UnitsState::instance.units[iVar6].y; - int sVar4 = DAT_UnitsState::instance.units[iVar6].terrainOrClimbHeight; - this->tribes[param_1].field_0x288 = this->tribes[param_1].field_0x288 + 1; - /* - Flat guards, not a nested chain. All three tests already had the right - sense; what differed was where the merged `return 0` landed. The - original puts it between the third guard and the success body, so the - first two jump forward into it (`jl 0x23`, `jl 0x15`) and the third - jumps over it (`jl 0xa`) - which is what early returns give. Nested, - MSVC moves the block to the end of the function and the third test - inverts to a `jge` reaching back for it. - */ - if (this->tribes[param_1].field_0x288 < param_2) { - return (undefined4)(0); - } - this->tribes[param_1].field_0x288 = 0; - if (bVar5 < 100) { - return (undefined4)(0); - } - iVar6 = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::countLivingNondyingUnitsForPlayer, DAT_UnitsState::ptr)(param_4); - if (iVar6 >= param_3) { - return (undefined4)(0); - } - if (param_4 == 0x2f) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createAnimal, this)( - Commands::M_MAPPER_CAMEL, (uint)(sVar2), (uint)(sVar3), (int)(sVar4)); - } - return (undefined4)(1); - } - return (undefined4)(0); - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp deleted file mode 100644 index f47865c1cf..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/unitIsSelectedByPlayer.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - // FUNCTION: STRONGHOLDCRUSADER 0x00522550 - BOOLEnum TribesState::unitIsSelectedByPlayer(uint tribeID) - { - return ((int)DAT_UnitsState::instance.selectedUnitsBitFlags[(int)tribeID / 16] - & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)tribeID % 16]) - != 0; - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp b/src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp deleted file mode 100644 index 5b4dd5991c..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/unsetRallyRelatedFlagOnUnits.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00524000 - void TribesState::unsetRallyRelatedFlagOnUnits(int tribeID) - { - int _unitID; - int _unitSelectionIndex = 0; - if (0 < this->tribes[tribeID].size) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitSelectionIndex); - _unitSelectionIndex = _unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[_unitID].dying == 0)) { - DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; - } - } while (_unitSelectionIndex < this->tribes[tribeID].size); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp b/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp deleted file mode 100644 index a69d53aafb..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/updateAnimalHerdBehaviorState.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00523590 - void TribesState::updateAnimalHerdBehaviorState(int tribeID) - { - int iVar1; - int unitSelectionIndex; - if (0 < tribeID && this->tribes[tribeID].predatorNearby == 0 - && (unitSelectionIndex = 0, 0 < this->tribes[tribeID].size)) { - do { - iVar1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[iVar1].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar1].dying == 0 - && DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue == 2) { - /* - Signed, and tested against 1 rather than 2. The original has - `cmp ecx, 1; jg` and derives the second value with `setg`, where - a `uint` gives `jae` and `seta`. Written `< 2` the literal does - not appear in the compare at all - MSVC puts 2 in a register and - emits `cmp ecx, ebx; jge` - so the bound is written as `<= 1`, - which is the same test on ints and the instruction the original - has. The arms are already the right way round: the original's - `jg` skips the nine bytes of the zero store, so that store is - its fall-through. - */ - int uVar2 = DAT_UnitsState::instance.units[iVar1].fixedRng & 7; - if (uVar2 <= 1) { - DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue = 0; - } else { - DAT_UnitsState::instance.units[iVar1].antelopeBasedRngValue = (5 < uVar2) + 1; - } - } - } while (unitSelectionIndex < this->tribes[tribeID].size); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp b/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp deleted file mode 100644 index 90abfa4924..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/updatePeasantSeatingAtBuilding.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "../../../Map.func.hpp" -#include "../TribesState.func.hpp" - -#include "OpenSHC/Map/Units/UnitsState.func.hpp" -#include "OpenSHC/Map/Units/States/UnitState.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/Map/Units/UnitType.hpp" - -#include "OpenSHC/Globals/DAT_BuildingsState.hpp" -#include "OpenSHC/Globals/DAT_UnitsState.hpp" -#include "OpenSHC/Globals/SEC_RNG.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - using Map::Units::UnitType; - using Map::Units::States::UnitState; - - // FUNCTION: STRONGHOLDCRUSADER 0x00522210 - void TribesState::updatePeasantSeatingAtBuilding(int param_1, int param_2, int param_3) - { - int iVar4; - int local_8; - int sVar1 = DAT_BuildingsState::instance.buildings[param_1].owner; - local_8 = 0; - if (param_2 <= 1) { - param_2 = 1; - } else if (param_2 <= 3) { - param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 1) + 1; - } else if (param_2 <= 7) { - param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 1; - } else if (param_2 <= 0xf) { - param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 1; - } else { - param_2 = ((byte)SEC_RNG::instance.currentNumber2 & 0xf) + 1; - } - iVar4 = 1; - if (1 < DAT_UnitsState::instance.maxUnitCount) { - Unit* _unit = &DAT_UnitsState::instance.units[1]; - do { - if (_unit->logicalState == Map::Units::ULS_NORMAL && _unit->dying == 0 && _unit->owner == sVar1 - && _unit->unitType == Map::Units::UT_PEASANT - && _unit->state.generic == Map::Units::States::US_STAND_UPUnk - && _unit->workplaceBuildingID_1 == param_1) { - local_8 = local_8 + 1; - /* - fixme: the short at Unit + 0x390 is declared as padding in the - generated header; it is the peasant's pending-sit flag. - */ - *(short*)_unit->padding_0x390 = 0; - if (param_3 == 1) { - _unit->peasantAlwaysZero = 0; - } - if (local_8 == param_2) { - _unit->peasantAlwaysZero = 1; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::standUpIfSeated, DAT_UnitsState::ptr)(iVar4); - } else if (_unit->peasantAlwaysZero == 0) { - *(short*)_unit->padding_0x390 = 1; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::sitDownIfStanding, DAT_UnitsState::ptr)( - iVar4); - } - } - iVar4 = iVar4 + 1; - _unit = _unit + 1; - } while (iVar4 < DAT_UnitsState::instance.maxUnitCount); - } - } - - } -} -} diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp deleted file mode 100644 index 7eb6df9ba8..0000000000 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeRallyFlags.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Units/UnitLogicState.hpp" - -#include "OpenSHC/Globals/DAT_UnitsState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - - using Map::Units::UnitLogicState; - - /* - One flag, not two: the original zeroes it once, writes 1 into it inside the loop - and reloads it into the return register at the loop exit (0x523fef). The - decompiler's `uVar3 = local_4` at the bottom of every iteration is that single - reload hoisted into the body. - - The `rallyRelatedFlag = 0` store at 0x523fc7 sits between the two rally tests and - runs for every living unit, so the gate has to be two nested ifs rather than one - comma-spliced condition. - */ - // FUNCTION: STRONGHOLDCRUSADER 0x00523F70 - undefined4 TribesState::updateTribeRallyFlags(int param_1) - { - int unitSelectionIndex = 0; - undefined4 _anyRallying = 0; - short* const _size = &this->tribes[param_1].size; - if (0 < *_size) { - do { - int const _unitID - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0) { - short const _isRallying = DAT_UnitsState::instance.units[_unitID].goToRallyPoint; - DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; - if (_isRallying != 0 - || DAT_UnitsState::instance.units[_unitID].isSelectable_OR_matchTime == 0) { - _anyRallying = 1; - DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 1; - } - } - } while (unitSelectionIndex < *_size); - } - return (undefined4)(_anyRallying); - } - - } -} -} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 7d26c0c1b6..0f2c368dac 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46381,11 +46381,11 @@ SHC_3BB0A8C1_0x00520F70 | 0.0% | Pending SHC_3BB0A8C1_0x00521180 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005211B0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005211B0 | 0.0% | Pending SHC_3BB0A8C1_0x00521210 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00521240 | 0.0% | Pending SHC_3BB0A8C1_0x00521280 | 33.3% | Reimplemented, asm differs @@ -46771,11 +46771,11 @@ SHC_3BB0A8C1_0x00522150 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522160 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005221A0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005221A0 | 0.0% | Pending SHC_3BB0A8C1_0x005221C0 | 0.0% | Pending -SHC_3BB0A8C1_0x00522210 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00522210 | 0.0% | Pending SHC_3BB0A8C1_0x005222B0 | 0.0% | Pending @@ -46789,7 +46789,7 @@ SHC_3BB0A8C1_0x005224E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522520 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522550 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00522550 | 0.0% | Pending SHC_3BB0A8C1_0x00522590 | 86.7% | Reimplemented, asm differs @@ -46801,7 +46801,7 @@ SHC_3BB0A8C1_0x005227E0 | 86.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522890 | 79.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00522950 | 0.0% | Pending SHC_3BB0A8C1_0x005229C0 | 76.1% | Reimplemented, asm differs @@ -46813,23 +46813,23 @@ SHC_3BB0A8C1_0x00522BF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00522C50 | 0.0% | Pending SHC_3BB0A8C1_0x00522CA0 | 95.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00522DD0 | 0.0% | Pending SHC_3BB0A8C1_0x00522EF0 | 70.6% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522F70 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00522F70 | 0.0% | Pending SHC_3BB0A8C1_0x00523030 | 98.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523190 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523190 | 0.0% | Pending -SHC_3BB0A8C1_0x00523240 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523240 | 0.0% | Pending -SHC_3BB0A8C1_0x005232E0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005232E0 | 0.0% | Pending SHC_3BB0A8C1_0x005232F0 | 0.0% | Pending @@ -46837,29 +46837,29 @@ SHC_3BB0A8C1_0x00523340 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005233A0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005233A0 | 0.0% | Pending SHC_3BB0A8C1_0x005233E0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523410 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523410 | 0.0% | Pending SHC_3BB0A8C1_0x00523520 | 96.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523590 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523590 | 0.0% | Pending -SHC_3BB0A8C1_0x00523630 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523630 | 0.0% | Pending -SHC_3BB0A8C1_0x005236A0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005236A0 | 0.0% | Pending SHC_3BB0A8C1_0x00523730 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523750 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523750 | 0.0% | Pending -SHC_3BB0A8C1_0x00523790 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523790 | 0.0% | Pending -SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005237F0 | 0.0% | Pending -SHC_3BB0A8C1_0x00523920 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523920 | 0.0% | Pending SHC_3BB0A8C1_0x00523A30 | 57.6% | Reimplemented, asm differs @@ -46873,23 +46873,23 @@ SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523EF0 | 62.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00523F70 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00523F70 | 0.0% | Pending -SHC_3BB0A8C1_0x00524000 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00524000 | 0.0% | Pending -SHC_3BB0A8C1_0x00524060 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00524060 | 0.0% | Pending -SHC_3BB0A8C1_0x005240D0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005240D0 | 0.0% | Pending -SHC_3BB0A8C1_0x00524140 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00524140 | 0.0% | Pending -SHC_3BB0A8C1_0x005241C0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005241C0 | 0.0% | Pending -SHC_3BB0A8C1_0x00524230 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00524230 | 0.0% | Pending SHC_3BB0A8C1_0x005242B0 | 88.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00524340 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00524340 | 0.0% | Pending SHC_3BB0A8C1_0x005244D0 | 44.6% | Reimplemented, asm differs @@ -46919,25 +46919,25 @@ SHC_3BB0A8C1_0x00525060 | 0.0% | Pending SHC_3BB0A8C1_0x00525090 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005250B0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005250B0 | 0.0% | Pending -SHC_3BB0A8C1_0x00525130 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00525130 | 0.0% | Pending -SHC_3BB0A8C1_0x005251A0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005251A0 | 0.0% | Pending -SHC_3BB0A8C1_0x00525210 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00525210 | 0.0% | Pending -SHC_3BB0A8C1_0x00525280 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00525280 | 0.0% | Pending -SHC_3BB0A8C1_0x00525300 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00525300 | 0.0% | Pending -SHC_3BB0A8C1_0x00525370 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00525370 | 0.0% | Pending -SHC_3BB0A8C1_0x005253F0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005253F0 | 0.0% | Pending -SHC_3BB0A8C1_0x00525470 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x00525470 | 0.0% | Pending -SHC_3BB0A8C1_0x005254C0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005254C0 | 0.0% | Pending SHC_3BB0A8C1_0x00525790 | 96.8% | Reimplemented, asm differs @@ -46953,11 +46953,11 @@ SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525F20 | 74.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005260B0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005260B0 | 0.0% | Pending -SHC_3BB0A8C1_0x005261B0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005261B0 | 0.0% | Pending -SHC_3BB0A8C1_0x005262A0 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x005262A0 | 0.0% | Pending SHC_3BB0A8C1_0x00526370 | 100.0% | Reimplemented @@ -48875,7 +48875,7 @@ SHC_3BB0A8C1_0x0052A637 | 0.0% | Pending SHC_3BB0A8C1_0x0052A638 | 0.0% | Pending -SHC_3BB0A8C1_0x0052A640 | 100.0% | Reimplemented +SHC_3BB0A8C1_0x0052A640 | 0.0% | Pending SHC_3BB0A8C1_0x0052A700 | 45.9% | Reimplemented, asm differs From d24bafb9893e3ba7efc7530c87d42aa490c42177 Mon Sep 17 00:00:00 2001 From: Gynt Date: Mon, 2 Nov 2026 09:39:40 +0100 Subject: [PATCH 151/202] Adopt the Wilps implementations that measure better here (#18) Ports 11 cpp bodies from reimpl/Wilps-2026-10-18, renamed into this branch's field vocabulary. No header is changed. Both variants were compiled in a worktree of *this* branch, against this branch's own headers, and scored with quick_diff.py, so the two are measured under one condition: predictUnitInterceptPosition.cpp 0.4878 -> 0.8607 applyUnitTopSpeedDelayBasedOnTribeSize.cpp 0.6432 -> 0.8899 addUnitToTribeAndUpdateTribeMovementSpeed.cpp 0.8067 -> 1.0000 stopTribeMovementAndCheckIdle.cpp 0.8462 -> 1.0000 getTribeAliveStatus.cpp 0.8750 -> 1.0000 isTribePathToDestinationClear.cpp 0.6635 -> 0.7778 addUnitsToTribeAndComputeMovementSpeed.cpp 0.8105 -> 0.8947 upgradeTribeArrayLayoutForMapVersion.cpp 0.6500 -> 0.7000 sortTribePathDestinationsByCost.cpp 0.6388 -> 0.6794 hasAvailableSpawnSlotForWildlifeOrMercs.cpp 0.8800 -> 0.9200 snapshotSelectionTribeAndComputeStance.cpp 0.9255 -> 0.9362 These are the only 11 of 70 functions in this namespace where the Wilps body wins. syntax_check.py reports ERRORS 0 over the namespace. Claude-Session: https://claude.ai/code/session_01XmXCmaoi64pyBVhcUh2Qpg Co-authored-by: Claude Opus 5 --- ...UnitToTribeAndUpdateTribeMovementSpeed.cpp | 82 ++----- ...addUnitsToTribeAndComputeMovementSpeed.cpp | 111 +++------ ...applyUnitTopSpeedDelayBasedOnTribeSize.cpp | 155 ++++--------- .../Units/TribesState/getTribeAliveStatus.cpp | 63 ++--- ...asAvailableSpawnSlotForWildlifeOrMercs.cpp | 44 ++-- .../isTribePathToDestinationClear.cpp | 126 ++++------ .../predictUnitInterceptPosition.cpp | 200 +++++----------- ...snapshotSelectionTribeAndComputeStance.cpp | 98 +++----- .../sortTribePathDestinationsByCost.cpp | 219 ++++++------------ .../stopTribeMovementAndCheckIdle.cpp | 55 ++--- .../upgradeTribeArrayLayoutForMapVersion.cpp | 82 ++----- 11 files changed, 406 insertions(+), 829 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp index 45a77ac311..c94d6c62c7 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribeAndUpdateTribeMovementSpeed.cpp @@ -1,6 +1,5 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -9,65 +8,34 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - + // The statistics are recomputed over all selected units of playerID, not over the tribe members. + // maxUnitCount (uint) is compared signed (jle at 0x522B28). // FUNCTION: STRONGHOLDCRUSADER 0x00522B00 - void TribesState::addUnitToTribeAndUpdateTribeMovementSpeed(int param_1, uint unitID, int tribeID) + void TribesState::addUnitToTribeAndUpdateTribeMovementSpeed(int playerID, uint unitID, int tribeID) { - /* - The three speeds are `int`: the original reads one with - `movsx ecx, word ptr [eax]` and compares and copies it 32 bits wide - (`cmp ecx, esi`, `mov esi, ecx`), where `short` locals give a `movzx` and - 16-bit `cmp cx, si` / `movzx esi, cx`. The tribe size stays narrow - that one - really is `movzx` plus `test ax, ax`. - */ - Unit* _ptrUnit; - int _maximumSpeed; - int _minimumSpeed; - int _totalHealth; - short _tribeSize; - int _movementSpeed; - int _tribeID; - _tribeID = tribeID; - _maximumSpeed = 0; - _totalHealth = 0; - _minimumSpeed = 100; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, tribeID); - if (1 < DAT_UnitsState::instance.maxUnitCount) { - tribeID = DAT_UnitsState::instance.maxUnitCount - 1; - /* - Same anchor difference as its twin addUnitsToTribeAndComputeMovementSpeed, - and most of what is left here. The original bases the unit loop on - `mov esi, I::instance+3568` - `&units[1].movementSpeed`, unit - offset 0x34c - and reads the speed at `[esi]`; we anchor on +3396, - `&units[1].dying` (0x2a0), so every displacement in the loop differs by 0xac - while the opcodes line up. diff_anchor.py reports both; see the fuller note - on the twin. - */ - _ptrUnit = &DAT_UnitsState::instance.units[1]; - do { - if (_ptrUnit->logicalState == Map::Units::ULS_NORMAL && !_ptrUnit->dying - && _ptrUnit->ifSelectedThenPlayerID == param_1) { - _movementSpeed = _ptrUnit->movementSpeed; - if (_movementSpeed < _minimumSpeed) { - _minimumSpeed = _movementSpeed; - } - if (_movementSpeed > _maximumSpeed) { - _maximumSpeed = _movementSpeed; - } - _totalHealth = _totalHealth + _ptrUnit->healthPercentage; + int maxSpeed = 0; + int healthSum = 0; + int minSpeed = 100; + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, tribeID); + for (int i = 1; i < (int)DAT_UnitsState::instance.maxUnitCount; ++i) { + if (DAT_UnitsState::instance.units[i].logicalState == ULS_NORMAL + && DAT_UnitsState::instance.units[i].dying == 0 + && DAT_UnitsState::instance.units[i].ifSelectedThenPlayerID == playerID) { + if (DAT_UnitsState::instance.units[i].movementSpeed < minSpeed) { + minSpeed = DAT_UnitsState::instance.units[i].movementSpeed; + } + if (DAT_UnitsState::instance.units[i].movementSpeed > maxSpeed) { + maxSpeed = DAT_UnitsState::instance.units[i].movementSpeed; } - _ptrUnit = _ptrUnit + 1; - tribeID = tribeID + -1; - } while (tribeID); + healthSum += DAT_UnitsState::instance.units[i].healthPercentage; + } } - _tribeSize = this->tribes[_tribeID].size; - this->tribes[_tribeID].size2Unk = _tribeSize; - if (0 < _tribeSize) { - this->tribes[_tribeID].minimumMovementSpeed = _minimumSpeed; - this->tribes[_tribeID].movementSpeed = _minimumSpeed; - this->tribes[_tribeID].maximumMovementSpeed = _maximumSpeed; - this->tribes[_tribeID].unitsHealthPercentage = (short)(_totalHealth / (int)_tribeSize); + this->tribes[tribeID].size2Unk = this->tribes[tribeID].size; + if (this->tribes[tribeID].size > 0) { + this->tribes[tribeID].minimumMovementSpeed = (short)minSpeed; + this->tribes[tribeID].movementSpeed = (short)minSpeed; + this->tribes[tribeID].maximumMovementSpeed = (short)maxSpeed; + this->tribes[tribeID].unitsHealthPercentage = (short)(healthSum / this->tribes[tribeID].size); } } diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp index b692c52240..8c31485079 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitsToTribeAndComputeMovementSpeed.cpp @@ -1,6 +1,5 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -9,84 +8,46 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - - /* - Its `short` tribe-size local stays narrow: widening it costs 17.6 points - (64.1% -> 46.5%), the largest such loss in the namespace. - */ + // maxUnitCount (uint) is compared signed (jle at 0x5229E7, jl at 0x522A46). // FUNCTION: STRONGHOLDCRUSADER 0x005229C0 void TribesState::addUnitsToTribeAndComputeMovementSpeed(int playerID, int tribeID) { - Unit* _pUnit; - uint _unitID; - /* - The three speeds are `int`, as in the sibling - addUnitToTribeAndUpdateTribeMovementSpeed, where the same change gained 9.9 - points: the original reads one with a single `movsx` and compares and copies - it 32 bits wide, where `short` locals give a `movzx` plus 16-bit compares. - The size and the target unit stay narrow. - */ - int local_8; - int _movementSpeed; - int _maximumSpeed; - short _targetUnit; - local_8 = 0; - int _minimumSpeed = 100; - _maximumSpeed = 0; - if (1 < DAT_UnitsState::instance.maxUnitCount) { - /* - The loop cursor is anchored on the wrong field, and that is most of what is - left here. MSVC bases the whole unit loop on one address: the original uses - `mov esi, I::instance+3568`, which is `&units[1].movementSpeed` - (unit offset 0x34c), and reads the speed at `[esi]` with dying at - `[esi - 0xac]`, logicalState at `[esi - 0x2c0]`, the owner at `[esi - 0x29a]` - and the health at `[esi - 0x78]`. We get +3396, `&units[1].dying` (0x2a0), - and every displacement in the loop differs by 0xac while the opcodes line up. - Same addresses at run time; five different instructions per iteration. - - diff_anchor.py now reports this - it used to miss it, because it only - recognised `lea`/`add` anchors and a cursor into a global array is set up - with `mov reg, +N`. Its notes list what has already been tried on - anchors elsewhere and found ineffective, so this is recorded rather than - guessed at. - */ - _pUnit = &DAT_UnitsState::instance.units[1]; - _unitID = 1; - do { - if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying - && _pUnit->ifSelectedThenPlayerID == playerID) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(_unitID, tribeID); - _movementSpeed = _pUnit->movementSpeed; - if (_movementSpeed < _minimumSpeed) { - _minimumSpeed = _movementSpeed; - } - if (_movementSpeed > _maximumSpeed) { - _maximumSpeed = _movementSpeed; - } - local_8 = local_8 + _pUnit->healthPercentage; + int healthSum = 0; + int minSpeed = 100; + int maxSpeed = 0; + for (int unitID = 1; unitID < (int)DAT_UnitsState::instance.maxUnitCount; ++unitID) { + if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0 + && DAT_UnitsState::instance.units[unitID].ifSelectedThenPlayerID == playerID) { + MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, tribeID); + if (DAT_UnitsState::instance.units[unitID].movementSpeed < minSpeed) { + minSpeed = DAT_UnitsState::instance.units[unitID].movementSpeed; + } + if (DAT_UnitsState::instance.units[unitID].movementSpeed > maxSpeed) { + maxSpeed = DAT_UnitsState::instance.units[unitID].movementSpeed; } - _unitID = _unitID + 1; - _pUnit = _pUnit + 1; - } while ((int)_unitID < DAT_UnitsState::instance.maxUnitCount); + healthSum += DAT_UnitsState::instance.units[unitID].healthPercentage; + } } - short sVar1 = this->tribes[tribeID].size; - this->tribes[tribeID].size2Unk = sVar1; - if (0 < sVar1) { - _targetUnit = this->tribes[tribeID].selectionTargetUnitID; - this->tribes[tribeID].facingDirection_1 = DAT_UnitsState::instance.units[_targetUnit].facingDirection; - this->tribes[tribeID].facingDirection_2 = DAT_UnitsState::instance.units[_targetUnit].facingDirection; - this->tribes[tribeID].facingDirection_3 = DAT_UnitsState::instance.units[_targetUnit].facingDirection; - this->tribes[tribeID].movementRunUpTime = DAT_UnitsState::instance.units[_targetUnit].movementRunUpTime; - this->tribes[tribeID].maximumMovementSpeed = _maximumSpeed; - this->tribes[tribeID].minimumMovementSpeed = _minimumSpeed; - this->tribes[tribeID].movementSpeed = _minimumSpeed; - this->tribes[tribeID].field61_0x200 = 0; - this->tribes[tribeID].field62_0x202 = 0; - this->tribes[tribeID].initialMoveOrderGiven = 0; - this->tribes[tribeID].field65_0x208 = 0; - this->tribes[tribeID].field67_0x20c = 0; - this->tribes[tribeID].unitsHealthPercentage = (short)(local_8 / (int)sVar1); + short const size = this->tribes[tribeID].size; + this->tribes[tribeID].size2Unk = size; + if (size > 0) { + int const selectionTargetUnitID = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].facingDirection_1 = DAT_UnitsState::instance.units[selectionTargetUnitID].facingDirection; + this->tribes[tribeID].facingDirection_2 = DAT_UnitsState::instance.units[selectionTargetUnitID].facingDirection; + this->tribes[tribeID].facingDirection_3 = DAT_UnitsState::instance.units[selectionTargetUnitID].facingDirection; + this->tribes[tribeID].movementRunUpTime = DAT_UnitsState::instance.units[selectionTargetUnitID].movementRunUpTime; + this->tribes[tribeID].maximumMovementSpeed = (short)maxSpeed; + int const averageHealth = healthSum / size; + this->tribes[tribeID].minimumMovementSpeed = (short)minSpeed; + this->tribes[tribeID].movementSpeed = (short)minSpeed; + short const zero = 0; + this->tribes[tribeID].field61_0x200 = zero; + this->tribes[tribeID].field62_0x202 = zero; + this->tribes[tribeID].initialMoveOrderGiven = zero; + this->tribes[tribeID].field65_0x208 = zero; + this->tribes[tribeID].field67_0x20c = zero; + this->tribes[tribeID].unitsHealthPercentage = (short)averageHealth; } } diff --git a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp index 2f7d13da57..76a886f4fd 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp @@ -1,9 +1,7 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" #include "OpenSHC/IO/LowLevelMemory.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -12,124 +10,59 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - using WindowsHelper::Enums::BOOLEnum; - + // The pair slot advances for every member while the count only grows for living ones (0x524FCA), so + // dead members leave zero holes and trailing living members are not re-indexed. The swap test is + // signed (jge at 0x525009) and skipped entirely when skipDistanceSort is set. // FUNCTION: STRONGHOLDCRUSADER 0x00524EF0 - void TribesState::applyUnitTopSpeedDelayBasedOnTribeSize(int tribeID, BOOLEnum param_2) + void TribesState::applyUnitTopSpeedDelayBasedOnTribeSize(int tribeID, BOOLEnum skipDistanceSort) { - short* _tribeSize = &this->tribes[tribeID].size; - int _unitSelectionIndex = 0; - /* - The original places the big-tribe branch first: it tests the size and jumps - away when it is below 40. - */ - if (40 <= *_tribeSize) { - if (0 < *_tribeSize) { - do { - /* - Set incremental delay on the first 199 units, then the same value - of 199 for the rest - */ - int const _unitInSelection - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitSelectionIndex); - int iVar1 = _unitSelectionIndex; - if (199 <= _unitSelectionIndex) { - iVar1 = 199; - } - DAT_UnitsState::instance.units[_unitInSelection].topSpeedDelayIndex = (short)iVar1; - _unitSelectionIndex = _unitSelectionIndex + 1; - } while (_unitSelectionIndex < *_tribeSize); + short* const tribeSize = &this->tribes[tribeID].size; + int i = 0; + int j; + int swapped; + if (*tribeSize >= 40) { + for (; i < *tribeSize; ++i) { + int const unitID = MACRO_CALL_MEMBER( + OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, i); + DAT_UnitsState::instance.units[unitID].topSpeedDelayIndex = (short)(i < 199 ? i : 199); } return; } - /* - For selections (tribes) of size 39 or less - */ this->someUnitIDArrayCount = 0; - MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - sizeof(this->someUnitIDMovementDistancePair), '\0', this->someUnitIDMovementDistancePair); - if (0 < *_tribeSize) { - /* - Of all units in the selection store unitID and optionally their - movementDistance into an array - */ - /* - The cursor walks the movementDistance member, not the pair: that is the - anchor the original picks (`lea ebx, [esi + 0xfa412]`, with the unit ID - stored at [ebx - 2]). - */ - short* _movementDistance = &this->someUnitIDMovementDistancePair[0].movementDistance; - do { - int const _selectionUnit - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitSelectionIndex); - if ((DAT_UnitsState::instance.units[_selectionUnit].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[_selectionUnit].dying == 0)) { - this->someUnitIDArrayCount = this->someUnitIDArrayCount + 1; - _movementDistance[-1] = (short)_selectionUnit; - if (!param_2) { - *_movementDistance = DAT_UnitsState::instance.units[_selectionUnit].movementDistance; - } + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->someUnitIDMovementDistancePair), 0, this->someUnitIDMovementDistancePair); + for (; i < *tribeSize; ++i) { + int const unitID = MACRO_CALL_MEMBER( + OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, i); + if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0) { + ++this->someUnitIDArrayCount; + this->someUnitIDMovementDistancePair[i].unitID = (short)unitID; + if (skipDistanceSort == FALSE) { + this->someUnitIDMovementDistancePair[i].movementDistance + = DAT_UnitsState::instance.units[unitID].movementDistance; } - _unitSelectionIndex = _unitSelectionIndex + 1; - _movementDistance = _movementDistance + 2; - } while (_unitSelectionIndex < *_tribeSize); + } } - int _foundUnsortedEntry; do { - /* - Bubble sort of the array by movementDistance, low to high - */ - _foundUnsortedEntry = 0; - int _pairIndex = 0; - /* - One test, not two: the original subtracts one from the count and jumps on - the sign-or-zero (`sub edx, 1; test edx, edx; jle`), which the decompiler - split into an equality and a sign test. - */ - if (this->someUnitIDArrayCount - 1 <= 0) { - break; - } - /* - The cursor walks the *second* element of each pair, which is the anchor - the original picks (diff_anchor.py: `lea eax, [esi + 0xfa414]`, with the - current entry reached as [eax - 4] and [eax - 2]). - */ - UnitIDMovementDistancePair* _nextUnit = this->someUnitIDMovementDistancePair + 1; - do { - UnitIDMovementDistancePair* pUVar2 = _nextUnit - 1; - /* - All four values are read up front and all four are `int`: the original - has `movsx edi, [eax - 4]`, `movsx ebx, [eax]`, `movsx ecx, [eax - 2]`, - `movsx edx, [eax + 2]` in that order, where `short` locals give - `movzx` and leaving the next entry's ID to be read inside the swap - loses the fourth load entirely. - */ - int const _unitID = pUVar2->unitID; - int const _nextUnitID = _nextUnit->unitID; - int const _distance = pUVar2->movementDistance; - int const _nextDistance = _nextUnit->movementDistance; - if ((!param_2) && (_nextDistance < _distance)) { - _foundUnsortedEntry = 1; - pUVar2->unitID = (short)_nextUnitID; - _nextUnit->unitID = (short)_unitID; - pUVar2->movementDistance = (short)_nextDistance; - _nextUnit->movementDistance = (short)_distance; + swapped = 0; + for (j = 0; j < this->someUnitIDArrayCount - 1; ++j) { + int const unitID = this->someUnitIDMovementDistancePair[j].unitID; + int const nextUnitID = this->someUnitIDMovementDistancePair[j + 1].unitID; + int const distance = this->someUnitIDMovementDistancePair[j].movementDistance; + int const nextDistance = this->someUnitIDMovementDistancePair[j + 1].movementDistance; + if (skipDistanceSort == FALSE && nextDistance < distance) { + swapped = 1; + this->someUnitIDMovementDistancePair[j].unitID = (short)nextUnitID; + this->someUnitIDMovementDistancePair[j + 1].unitID = (short)unitID; + this->someUnitIDMovementDistancePair[j].movementDistance = (short)nextDistance; + this->someUnitIDMovementDistancePair[j + 1].movementDistance = (short)distance; } - _pairIndex = _pairIndex + 1; - _nextUnit = _nextUnit + 1; - } while (_pairIndex < this->someUnitIDArrayCount + -1); - } while (_foundUnsortedEntry); - int _delayIndex = 0; - if (0 < this->someUnitIDArrayCount) { - UnitIDMovementDistancePair* pUVar2 = this->someUnitIDMovementDistancePair; - do { - DAT_UnitsState::instance.units[pUVar2->unitID].topSpeedDelayIndex = (short)_delayIndex; - _delayIndex = _delayIndex + 1; - pUVar2 = pUVar2 + 1; - } while (_delayIndex < this->someUnitIDArrayCount); + } + } while (swapped != 0); + for (i = 0; i < this->someUnitIDArrayCount; ++i) { + DAT_UnitsState::instance.units[this->someUnitIDMovementDistancePair[i].unitID].topSpeedDelayIndex + = (short)i; } } diff --git a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp index ea519e51db..b3fd90b6a1 100644 --- a/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getTribeAliveStatus.cpp @@ -1,6 +1,5 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -9,49 +8,35 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - - /* - @return 0 if all alive, 1 if majority alive, 50 if fewer alive than dead, 100 if all dead decompilerscript: - committed: 2025-01-30 21:57:43.216000 decompilerscript: committed: 2026-05-02 18:15:17.059000 - */ + // Counts living members with tunnelerFinishedDigging == 2 against the other living members (signed jl at + // 0x524C7A): 100 if none at 2, 50 if they are not the majority, else 1 if others exist, 0 if all at 2. // FUNCTION: STRONGHOLDCRUSADER 0x00524BF0 int TribesState::getTribeAliveStatus(int tribeID) { - int _unit; - int _index = 0; - int _other = 0; - int _alive = 0; - int _size = (int)this->tribes[tribeID].size; - if (0 < _size) { - do { - _unit = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _index); - _index = _index + 1; - if ((DAT_UnitsState::instance.units[_unit].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[_unit].dying == 0)) { - if (DAT_UnitsState::instance.units[_unit].tunnelerFinishedDigging == 2) { - _alive = _alive + 1; - } else { - _other = _other + 1; - } - } - } while (_index < _size); - /* - The nesting stays. The original returns 100 from here as well as from the - empty-tribe path, jumping 0xf forward when the tally is non-zero - (`test ecx, ecx; jne 0xf`) over its own `mov eax, 0x64` epilogue, so that - return is duplicated rather than shared. Writing it as an early - `if (!_alive) return 100;` measures 67.2% -> 67.2%. - */ - if (_alive != 0) { - if (_other >= _alive) { - return (int)(50); + int const tribeSize = this->tribes[tribeID].size; + int counts[2]; // [0] tunnelerFinishedDigging == 2, [1] other + counts[1] = 0; + counts[0] = 0; + for (int unitSelectionIndex = 0; unitSelectionIndex < tribeSize;) { + int const unitID = MACRO_CALL_MEMBER( + OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, unitSelectionIndex); + ++unitSelectionIndex; + if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0) { + if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging == 2) { + ++counts[0]; + } else { + ++counts[1]; } - return (uint)(_other != 0); } } - return 100; + if (counts[0] == 0) { + return 100; + } + if (counts[1] >= counts[0]) { + return 50; + } + return counts[1] != 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp index f1d9aa924f..1efd413e32 100644 --- a/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp +++ b/src/OpenSHC/Map/Units/TribesState/hasAvailableSpawnSlotForWildlifeOrMercs.cpp @@ -1,7 +1,6 @@ -#include "../../../Map.func.hpp" #include "../TribesState.func.hpp" -#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameModeInt.hpp" #include "OpenSHC/Globals/DAT_GameState.hpp" #include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" @@ -11,33 +10,30 @@ namespace OpenSHC { namespace Map { namespace Units { - using Game::GameMode; - + // Slots 4..11 read [i*4 + 0x117F094] (machine code 0x52356C) = mapAndTime.signpostIDs[i - 4] + // (0x117F0A4..0x117F0C0), not mercRecruitable as the header doc says. // FUNCTION: STRONGHOLDCRUSADER 0x00523520 undefined4 TribesState::hasAvailableSpawnSlotForWildlifeOrMercs() { - if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY - || 0xa0 <= DAT_GameState::instance.mapAndTime.wildlifeCount) { - return (undefined4)(0); - } - /* - The last instruction of difference is the original's `xor eax, eax` sitting - before the wildlifeCount guard rather than after it. Declaring the counter - there costs 10 points (96.0% -> 85.7%), so it stays in the `for` header. - */ - for (int _slot = 0; _slot < 0xc; _slot = _slot + 1) { - if (_slot < 4) { - uint const _x = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][0]; - uint const _y = (uint)DAT_GameState::instance.mapAndTime.rabbitSpawnXY[_slot][1]; - if (_x <= 399 && _y <= 399 - && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[_y * 400 + _x] != 0) { - return (undefined4)(1); + int i; + if (DAT_GameSynchronyState::instance.currentGameMode == OpenSHC::Game::GM_SOLITARY + && (i = 0, DAT_GameState::instance.mapAndTime.wildlifeCount < 0xa0)) { + do { + if (i < 4) { + uint const spawnX = DAT_GameState::instance.mapAndTime.rabbitSpawnXY[i][0]; + uint const spawnY = DAT_GameState::instance.mapAndTime.rabbitSpawnXY[i][1]; + if (spawnX <= 399 && spawnY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[spawnY * 400 + spawnX] + != 0) { + return 1; + } + } else if (DAT_GameState::instance.mapAndTime.signpostIDs[i - 4] != 0) { + return 1; } - } else if (DAT_GameState::instance.mapAndTime.mercRecruitable[_slot + 4] != 0) { - return (undefined4)(1); - } + ++i; + } while (i < 12); } - return (undefined4)(0); + return 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index 6369390af8..65a9e166ed 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -1,9 +1,7 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" #include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_PathFindingState.hpp" #include "OpenSHC/Globals/DAT_TileMapState.hpp" @@ -14,98 +12,58 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - using WindowsHelper::Enums::BOOLEnum; - - /* - The two guards stay as `||` chains. The original merges every failure into a - single `return 0` at 0x524cd7 and places it inline right after the first chain, - so the second chain's tests jump backward into it (`ja 0x00524cd7`, encoded - `77ea`) - a full epilogue, `pop/pop/pop/xor eax,eax/pop/pop/ret 0x14`, not a - jump to a shared one. Ours emits that block at the end of the function instead, - and that placement is what the remaining gap is mostly made of. - - Writing the first chain positively with the whole rest of the function nested - inside it, so that the shared `return 0` is the fall-out rather than the taken - branch, measures 18.8% -> 18.8%. MSVC puts that epilogue at the end of the - function whichever way round the guard is written. - - Splitting the chains into six separate `if`s is what took - trySpawnAdditionalWildlifeForTribe from 87.2% to a full match, so it was worth - retrying here. Measured with reccmp: 18.8% -> 17.4%. The earlier note against - it cited quick_diff 0.6635 -> 0.6296, which is below the ratio that tool is - documented to be reliable at; the conclusion was right anyway. - - What ours also lacks is the original's second local slot: it allocates one - (`push ecx`) and reuses the dead tribeID parameter slot for the second counter, - where the original has `sub esp, 8` and two proper locals. - - Copying the parameter into `int const _tribeID` and using that through the loop - does not buy the slot: measured 18.8% -> 18.8%, MSVC coalesces the copy back onto - the incoming argument. The slot comes from whatever second value the original - keeps live, not from naming the first one. - */ + // The coordinates are tested unsigned (ja 0x18F); ALG_ResultTileIndex counts the tiles taken and is + // compared signed (jge 0x1F4 at 0x524DCF); UnitLayer is read signed (movsx, 0x524DD5). The tribe size is + // re-read every iteration (0x524D80). All failure exits share the return block at 0x524CD7 except the + // occupied-tileUnk exit (0x524E00). // FUNCTION: STRONGHOLDCRUSADER 0x00524CA0 undefined4 TribesState::isTribePathToDestinationClear( - int tribeID, uint fromX, uint fromY, uint toX, uint toY) + int tribeID, uint fromX, uint fromY, uint destX, uint destY) { - int _unitIndex = 0; - if (399 < fromX || 399 < fromY + int unitSelectionIndex = 0; + if (fromX > 399 || fromY > 399 || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[fromY * 400 + fromX] == 0) { - return (undefined4)(0); + return 0; } - if (399 < toX || 399 < toY - || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[toY * 400 + toX] == 0) { - return (undefined4)(0); + if (destX > 399 || destY > 399 + || DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[destY * 400 + destX] == 0) { + return 0; } - int const _toTile = DAT_ViewportRenderState::instance.translationMatrix[toY].addXgetTile + toX; - if (DAT_TileMapState::instance.PathConnectionLayer - [DAT_ViewportRenderState::instance.translationMatrix[fromY].addXgetTile + fromX] - != DAT_TileMapState::instance.PathConnectionLayer[_toTile]) { - return (undefined4)(0); + int const destTile = DAT_ViewportRenderState::instance.translationMatrix[destY].addXgetTile + destX; + if (DAT_TileMapState::instance + .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[fromY].addXgetTile + fromX] + != DAT_TileMapState::instance.PathConnectionLayer[destTile]) { + return 0; } this->ALG_ResultTileIndex = 0; - /* - The original computes this once, before the search is seeded. The - decompiler's `2 - (uVar1 & 0x100)` subtracts the mask itself, but the binary - has `and 0x100; neg; sbb; add 2`, which is 2 minus a 0/1 flag. - */ - int const _searchMode = (DAT_TileMapState::instance.LogicLayer[_toTile] & 0x100) != 0 ? 1 : 2; - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, - DAT_PathFindingState::ptr)(toX, toY, -1, -1, 500, FALSE); - /* - The size lives behind a pointer the original takes once, after the search is - seeded (`lea edx, [ecx + edi + 0x5c]` at 0x524d6e, kept in a stack slot and - re-read at the top of every iteration). - */ - short const* const _size = &this->tribes[tribeID].size; - while (true) { - int _unitID; - do { - if (_unitIndex >= *_size) { - return (undefined4)(1); - } - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _unitIndex); - _unitIndex = _unitIndex + 1; - } while ((DAT_UnitsState::instance.units[_unitID].logicalState != Map::Units::ULS_NORMAL) - || (DAT_UnitsState::instance.units[_unitID].dying != 0)); - int _blockingUnitID = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findClosestTileToStartingTile, DAT_PathFindingState::ptr)( - _searchMode); - this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; - if ((int)this->ALG_ResultTileIndex >= 500) - break; - _blockingUnitID = (int)(short)DAT_TileMapState::instance.UnitLayer[_blockingUnitID]; - if (_blockingUnitID && _unitID != _blockingUnitID - && DAT_UnitsState::instance.units[_blockingUnitID].tribeID != tribeID - && DAT_UnitsState::instance.units[_blockingUnitID].tunnelerFinishedDigging != 2) { - return (undefined4)(0); + int const closestTileMode = (DAT_TileMapState::instance.LogicLayer[destTile] & 0x100) != 0 ? 1 : 2; + MACRO_CALL_MEMBER(Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(destX, destY, -1, -1, 500, FALSE); + for (;;) { + if (unitSelectionIndex >= this->tribes[tribeID].size) { + return 1; + } + int const unitID = MACRO_CALL_MEMBER( + OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, unitSelectionIndex); + ++unitSelectionIndex; + if (DAT_UnitsState::instance.units[unitID].logicalState != ULS_NORMAL + || DAT_UnitsState::instance.units[unitID].dying != 0) { + continue; + } + int const tileUnk = MACRO_CALL_MEMBER(Navigation::PathFindingState_Func::findClosestTileToStartingTile, + DAT_PathFindingState::ptr)(closestTileMode); + ++this->ALG_ResultTileIndex; + if ((int)this->ALG_ResultTileIndex >= 500) { + return 0; + } + int const occupantUnitID = (short)DAT_TileMapState::instance.UnitLayer[tileUnk]; + if (occupantUnitID != 0 && unitID != occupantUnitID + && DAT_UnitsState::instance.units[occupantUnitID].tribeID != tribeID + && DAT_UnitsState::instance.units[occupantUnitID].tunnelerFinishedDigging != 2) { + return 0; } } - return (undefined4)(0); } - } } } diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index a9765ffdbf..c83337b36c 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -1,4 +1,3 @@ -#include "../../../Map.func.hpp" #include "../TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitType.hpp" @@ -6,165 +5,88 @@ #include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include + namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitType; + using OpenSHC::Map::Units::UnitType; + // The path plan byte is read signed (movsx at 0x52169E) although pathPlanStart is declared byte. // FUNCTION: STRONGHOLDCRUSADER 0x00521500 void TribesState::predictUnitInterceptPosition( int targetUnitID, int unitID, int* unitCurrentX, int* unitCurrentY) { - UnitTypeShort UVar1; - uint _currentIndexInPathPlan; - int local_8; - ushort _targetTribeMinSpeed; - int _unitID = unitID; - *unitCurrentX = (int)DAT_UnitsState::instance.units[unitID].x; - int _yOffb = (int)DAT_UnitsState::instance.units[unitID].y; - *unitCurrentY = _yOffb; - /* - The import had this guard with an empty body; the original jumps straight to - the epilogue, so everything below runs only for a unit that has finished - digging. - */ + *unitCurrentX = DAT_UnitsState::instance.units[unitID].x; + *unitCurrentY = DAT_UnitsState::instance.units[unitID].y; if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging != 2) { return; } - _targetTribeMinSpeed - = this->tribes[DAT_UnitsState::instance.units[targetUnitID].tribeID].minimumMovementSpeed; - /* - Both bounds are tested against 1, not 2, and both speeds are `short`. The - original has `cmp dx, 1` and `cmp cx, 1` - sixteen bits wide, against the - literal 1, each with a `jg` over a `mov reg, 1` - where `< 2` on an `int` - gives a 32-bit `jge` against 2. Same test either way; one is the - instruction the original has. - - Both loads are `movzx` - `movzx ecx, word ptr [esi + ecx + 0x222]` at - 0x521576 - and both are then sign-extended (`movsx edx, dx` at 0x521566, - `movsx ecx, cx` at 0x52158a) before the test, so the field is an unsigned - word that the code treats as signed. Tribe::minimumMovementSpeed is `ushort` - for that reason and the casts here are what the sign extension is. - */ - int _targetTribeMinSpeedBounded = (short)_targetTribeMinSpeed; - if ((short)_targetTribeMinSpeed <= 1) { - _targetTribeMinSpeedBounded = 1; + short speed = this->tribes[DAT_UnitsState::instance.units[targetUnitID].tribeID].minimumMovementSpeed; + int targetFactor = speed; + if (speed <= 1) { + targetFactor = 1; } - ushort _tribeMinSpeed - = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; - int iVar3 = _targetTribeMinSpeedBounded * 4; - int _tribeMinSpeedBounded = (short)_tribeMinSpeed; - if ((short)_tribeMinSpeed <= 1) { - _tribeMinSpeedBounded = 1; + targetFactor *= 4; + speed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; + int unitStep = speed; + if (speed <= 1) { + unitStep = 1; } - int iVar4 = (int)DAT_UnitsState::instance.units[targetUnitID].x; - int iVar2 = (int)DAT_UnitsState::instance.units[targetUnitID].y; - if (DAT_UnitsState::instance.units[targetUnitID].stateBasedSpeed <= 0) { - UVar1 = DAT_UnitsState::instance.units[targetUnitID].unitType; - if ((UVar1 == Map::Units::UT_E_KNIGHT) - && (DAT_UnitsState::instance.units[targetUnitID].isMatchingSpeed == false)) { - iVar3 = iVar3 / 3; - } else if (UVar1 == Map::Units::UT_E_SWORD) - goto LAB_005215e9; - } else { - LAB_005215e9: - iVar3 = iVar3 / 2; + unitStep *= 4; + int const targetX = DAT_UnitsState::instance.units[targetUnitID].x; + int const targetY = DAT_UnitsState::instance.units[targetUnitID].y; + if (DAT_UnitsState::instance.units[targetUnitID].stateBasedSpeed > 0) { + targetFactor /= 2; + } else if (DAT_UnitsState::instance.units[targetUnitID].unitType == UT_E_KNIGHT + && !DAT_UnitsState::instance.units[targetUnitID].isMatchingSpeed) { + targetFactor /= 3; + } else if (DAT_UnitsState::instance.units[targetUnitID].unitType == UT_E_SWORD) { + targetFactor /= 2; } - if (DAT_UnitsState::instance.units[unitID].stateBasedSpeed <= 0) { - UVar1 = DAT_UnitsState::instance.units[unitID].unitType; - if ((UVar1 == Map::Units::UT_E_KNIGHT) - && (DAT_UnitsState::instance.units[unitID].isMatchingSpeed == false)) { - unitID = iVar3 / 3; - goto LAB_0052163b; - } - unitID = iVar3; - if (UVar1 != Map::Units::UT_E_SWORD) - goto LAB_0052163b; + if (DAT_UnitsState::instance.units[unitID].stateBasedSpeed > 0) { + targetFactor /= 2; + } else if (DAT_UnitsState::instance.units[unitID].unitType == UT_E_KNIGHT + && !DAT_UnitsState::instance.units[unitID].isMatchingSpeed) { + targetFactor /= 3; + } else if (DAT_UnitsState::instance.units[unitID].unitType == UT_E_SWORD) { + targetFactor /= 2; } - /* - reused parameter! - */ - unitID = iVar3 / 2; - LAB_0052163b: - int _unitCurrentX_2 = *unitCurrentX; - uint _xOff_2 = _unitCurrentX_2 - iVar4; - uint _yOff_2 = _yOffb - iVar2; - /* - The shift-and-xor absolute values stay, although the original has - `cdq; xor eax, edx; sub eax, edx` at 0x521645 and 0x521654 - which is what - MSVC emits for `x < 0 ? -x : x`. Written that way, at both of the two sites, - the function measures 40.1% -> 27.3%: it produces the `cdq` and re-lays out - everything around it. - */ - int _xOff_2b = (_xOff_2 ^ (int)_xOff_2 >> 0x1f) - ((int)_xOff_2 >> 0x1f); - int _yOff_2b = (_yOff_2 ^ (int)_yOff_2 >> 0x1f) - ((int)_yOff_2 >> 0x1f); - if (_xOff_2b > _yOff_2b) { - _yOff_2b = _xOff_2b; + int distanceX = abs(*unitCurrentX - targetX); + int distance = abs(*unitCurrentY - targetY); + if (distanceX > distance) { + distance = distanceX; } - /* - Flat guards. Nesting the loop inside one `&&` chain puts the merged exit at - the end of the function; the original reaches it directly from each test. - This is the same shape that took trySpawnAdditionalWildlifeForTribe from - 87.2% to a full match, and it drops two comma operators from inside an `&&`. - */ - if ((_yOff_2b + -2) * unitID < 0) { + if ((distance - 2) * targetFactor < 0) { return; } - _currentIndexInPathPlan = (int)DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan; - local_8 = _tribeMinSpeedBounded * 4; - if ((int)DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan - <= (int)_currentIndexInPathPlan) { + int pathIndex = DAT_UnitsState::instance.units[unitID].currentIndexInPathPlan; + if (pathIndex >= DAT_UnitsState::instance.units[unitID].totalSizeOfPathPlan) { return; } - { - do { - /* - get current value in pathPlan, each byte contains two move instructions - */ - /* - 0x138864a is `units[0].pathPlanStart`: the unit array starts at - 0x138854c, since the x the original reads at 0x1388610 is at 0xc4 in - a Unit, and 0x138864a - 0x138854c is 0xfe. The byte is signed - - `movsx eax, byte ptr [eax + ebp + 0x138864a]` - so the cast stays, - and the unit's own offset is still added separately because that is - how the original addresses it. - */ - uint _pathPlanPair = (uint)(char)DAT_UnitsState::instance.units[_unitID] - .pathPlanStart[(int)_currentIndexInPathPlan / 2]; - if (!(_currentIndexInPathPlan & 1)) { - /* - if index is even, retain the lowest 4 bits - */ - _pathPlanPair = _pathPlanPair & 0xf; - } else { - /* - if index is odd retain the highest 4 bits - */ - _pathPlanPair = (int)_pathPlanPair >> 4; - } - *unitCurrentX = DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[_pathPlanPair] - .int_.xOffset - + _unitCurrentX_2; - *unitCurrentY = *unitCurrentY - + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[_pathPlanPair] - .int_.yOffset; - _unitCurrentX_2 = *unitCurrentX; - uint _xOff = _unitCurrentX_2 - iVar4; - uint _yOff = *unitCurrentY - iVar2; - iVar3 = (_xOff ^ (int)_xOff >> 0x1f) - ((int)_xOff >> 0x1f); - _yOffb = (_yOff ^ (int)_yOff >> 0x1f) - ((int)_yOff >> 0x1f); - if (iVar3 > _yOffb) { - _yOffb = iVar3; - } - if ((_yOffb + -2) * unitID < local_8) { - return; - } - local_8 = local_8 + _tribeMinSpeedBounded * 4; - _currentIndexInPathPlan = _currentIndexInPathPlan + 1; - } while ( - (int)_currentIndexInPathPlan < (int)DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan); - } + int cost = unitStep; + do { + int direction = (char)DAT_UnitsState::instance.units[unitID].pathPlanStart[pathIndex / 2]; + if (pathIndex & 1) { + direction >>= 4; + } else { + direction &= 0xf; + } + *unitCurrentX = DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.xOffset + + *unitCurrentX; + *unitCurrentY += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.yOffset; + distanceX = abs(*unitCurrentX - targetX); + distance = abs(*unitCurrentY - targetY); + if (distanceX > distance) { + distance = distanceX; + } + if ((distance - 2) * targetFactor < cost) { + return; + } + cost += unitStep; + ++pathIndex; + } while (pathIndex < DAT_UnitsState::instance.units[unitID].totalSizeOfPathPlan); } } diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index d90b7bd202..570f89174b 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -1,4 +1,3 @@ -#include "../../../Map.func.hpp" #include "../TribesState.func.hpp" #include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" @@ -10,82 +9,49 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - using Map::Units::Behavior::UnitStanceEnum; + using OpenSHC::Map::Units::Behavior::USE_AGGRESSIVE; + using OpenSHC::Map::Units::Behavior::USE_STAND_GROUND; + // maxUnitCount (uint) is compared signed (jle at 0x522CCF). The stance counter array is indexed with the + // tribe's unitStance without a range check (0x522D15). // FUNCTION: STRONGHOLDCRUSADER 0x00522CA0 void TribesState::snapshotSelectionTribeAndComputeStance(int playerID) { - BOOLEnum _atLeastTwoTribes = FALSE; this->tribeCopiedToSlot0 = 0; - int _stanceCount[3]; - _stanceCount[0] = 0; - _stanceCount[1] = 0; - _stanceCount[2] = 0; - int _lastTribeID = -1; - if (1 < DAT_UnitsState::instance.maxUnitCount) { - Unit* _pUnit = &DAT_UnitsState::instance.units[1]; - int _unitIDCountdown = DAT_UnitsState::instance.maxUnitCount - 1; - do { - if (_pUnit->logicalState == Map::Units::ULS_NORMAL && !_pUnit->dying - && _pUnit->ifSelectedThenPlayerID == playerID) { - int const _unitTribeID = (int)_pUnit->tribeID; - if (0 < _unitTribeID) { - /* - count the stance of every tribe the selection touches, and - notice as soon as it touches two different ones - */ - int const _stance = (short)this->tribes[_unitTribeID].unitStance; - _stanceCount[_stance] = _stanceCount[_stance] + 1; - if (_lastTribeID != -1 && _unitTribeID != _lastTribeID) { - _atLeastTwoTribes = TRUE; - } - _lastTribeID = _unitTribeID; + BOOL multipleTribes = FALSE; + int stanceCount[3]; + stanceCount[0] = 0; + stanceCount[1] = 0; + stanceCount[2] = 0; + int lastTribeID = -1; + for (int unitID = 1; unitID < (int)DAT_UnitsState::instance.maxUnitCount; ++unitID) { + if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0 + && DAT_UnitsState::instance.units[unitID].ifSelectedThenPlayerID == playerID) { + int const tribeID = DAT_UnitsState::instance.units[unitID].tribeID; + if (tribeID > 0) { + ++stanceCount[this->tribes[tribeID].unitStance]; + if (lastTribeID != -1 && tribeID != lastTribeID) { + multipleTribes = TRUE; } + lastTribeID = tribeID; } - _pUnit = _pUnit + 1; - _unitIDCountdown = _unitIDCountdown + -1; - } while (_unitIDCountdown); - if (_lastTribeID != -1 && !_atLeastTwoTribes) { - /* - A whole-struct assignment, which is the `rep movsd` of 0xcd dwords the - original has; a hand-written int-by-int copy compiles to a loop. - - And then it returns: the block at 0x522d42 ends in its own `ret 4`, so - a selection that is all one tribe keeps the stance it just copied - instead of having the majority written over it. That is also why the - three counts are only loaded out of the array at 0x522d6a, on the path - where the copy did not happen. - */ - this->tribes[0] = this->tribes[_lastTribeID]; - this->tribeCopiedToSlot0 = 1; - return; } } - /* - The three counts are read straight out of the array, which is where the - original's own three slots are: it loads [esp+0x18], [esp+0x14] and - [esp+0x10] into registers at 0x522d6a and nowhere else, and diff_slots.py - shows our extra fourth slot is a spill of a redundant copy. - */ - /* - The stand-ground store goes last, and the two counts are compared the other - way round. By address the original's three exits are the aggressive store, - then the computed one, then `mov word ptr [ebx + 0x2e0], 0` with an epilogue - of its own - so the empty-tally arm is the far block, not the first guard. - The computed one is `cmp ecx, esi; setge al`, which is - `_stanceCount[1] >= _stanceCount[0]`; writing it as `[0] <= [1]` gives the - same answer from `cmp esi, ecx; setle al`, with the operands swapped. - */ - if (_stanceCount[2] + _stanceCount[1] + _stanceCount[0] != 0) { - if (_stanceCount[2] < _stanceCount[1] || _stanceCount[2] < _stanceCount[0]) { - this->tribes[0].unitStance = (ushort)(_stanceCount[1] >= _stanceCount[0]); - return; - } - this->tribes[0].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + if (lastTribeID != -1 && !multipleTribes) { + this->tribes[0] = this->tribes[lastTribeID]; + this->tribeCopiedToSlot0 = 1; return; } - this->tribes[0].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + if (stanceCount[2] + stanceCount[1] + stanceCount[0] != 0) { + if (stanceCount[2] >= stanceCount[1] && stanceCount[2] >= stanceCount[0]) { + this->tribes[0].unitStance = USE_AGGRESSIVE; + } else { + this->tribes[0].unitStance = stanceCount[1] >= stanceCount[0]; + } + } else { + this->tribes[0].unitStance = USE_STAND_GROUND; + } } } diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp index cc6808b31e..54b803b535 100644 --- a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -1,10 +1,6 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" #include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Navigation/PathFindingStatePartB.hpp" -#include "OpenSHC/Map/Navigation/PathHelper12.hpp" -#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_PathFindingState.hpp" #include "OpenSHC/Globals/DAT_TileMapState.hpp" @@ -13,162 +9,99 @@ namespace OpenSHC { namespace Map { namespace Units { - - using Map::Navigation::PathFindingStatePartB; - using Map::Navigation::PathHelper12; - using WindowsHelper::Enums::BOOLEnum; - - /* - The unit-ID local stays `short`, although our movzx-plus-movsx pair against the - original's single movsx says otherwise: widening it costs 2.2 points - (28.8% -> 26.6%, norm 30.3% -> 28.0%). Its `bool` flag stays a byte for the same - reason: BOOLEnum costs 5.3 points (28.8% -> 23.5%). - */ + // A bubble-sort pass ends at the first entry whose tile2 is 0 or whose successor has tile1 or tile2 0 + // (0x524A30..0x524A4C); unreached entries (cost 10000000) are compacted out afterwards. // FUNCTION: STRONGHOLDCRUSADER 0x00524930 - void TribesState::sortTribePathDestinationsByCost(int tribeID, int horseAndRamCount) + void TribesState::sortTribePathDestinationsByCost(int tribeIDThenSortIdx, int horseAndRamCount) { - BOOLEnum BVar8 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); - short sVar1 = this->tribes[tribeID].selectionTargetUnitID; - uint x = (uint)DAT_UnitsState::instance.units[sVar1].x; - /* - Both of these were the wrong way round. The original computes x before the - first test and jumps away when the count is zero - `cmp dword ptr [esp + - 0x10], 0; je 0x1e` - so the ram-and-horse call is the fall-through, and - inside the other branch `test ecx, ecx; je 0x7` falls through to the - all-assassins call. Written `if (!count)` and `if (!BVar8)` both bodies are - emitted in the far block and every branch past them shifts. - */ - if (horseAndRamCount) { - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::calculatePathKeepAndWallsGatesNotAllowed, - DAT_PathFindingState::ptr)(x, (int)(DAT_UnitsState::instance.units[sVar1].y), -1, -1, 100000); + BOOLEnum const allAssassins = MACRO_CALL_MEMBER( + OpenSHC::Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeIDThenSortIdx); + int const selectionTargetUnitID = this->tribes[tribeIDThenSortIdx].selectionTargetUnitID; + int const x = DAT_UnitsState::instance.units[selectionTargetUnitID].x; + if (horseAndRamCount != 0) { + MACRO_CALL_MEMBER(Navigation::PathFindingState_Func::calculatePathKeepAndWallsGatesNotAllowed, + DAT_PathFindingState::ptr)(x, DAT_UnitsState::instance.units[selectionTargetUnitID].y, -1, -1, 100000); } else { - uint y = (uint)DAT_UnitsState::instance.units[sVar1].y; - if (BVar8) { - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, + int const y = DAT_UnitsState::instance.units[selectionTargetUnitID].y; + if (allAssassins != FALSE) { + MACRO_CALL_MEMBER(Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, DAT_PathFindingState::ptr)(x, y, 0xffffffff, 0xffffffff, 100000, 0); } else { - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + MACRO_CALL_MEMBER(Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, DAT_PathFindingState::ptr)(x, y, -1, -1, 100000, FALSE); } } - /* - All three loops walk one array. The decompiler printed them as byte offsets - into two different spellings of the same address - the search queue itself - and a `climbData + 200` cast - but climbData is at 0x1084 and the queue at - 0x1a3a4, which is 200 ClimbData further on, so both are - `searchQueue.destinationsArray` and every `+ 0xc` offset is one entry. - - The array is subscripted off the global rather than through a local base - pointer. The original addresses every field as `[offset + + N]`, - strength-reducing the subscript to a byte offset it recomputes from the - counter at the bottom of each loop (`add ecx, 1; lea eax, [ecx + ecx*2]; - add eax, eax; add eax, eax`); a `PathHelper12* const` local for the array - makes MSVC keep the base in a register and address `[ptr + N]` instead, - which is worth almost thirty points across the three loops: the same code - with a `PathHelper12* const _destinations` local measures 26.4%, and - subscripting the global measures 56.2%. - */ - int _index = 0; - if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 != 0) { - /* - The entry pointer is what the original reads the tile through - (`mov edx, [edx]`), recomputed from the counter at the bottom alongside - the offset it stores through. - */ - PathHelper12* _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray; - do { - /* - Ten million is the cost given to a destination the current search did - not reach, which is what the compaction loop below drops entries on. - */ - int const _tile = _entry->tile1; - if (DAT_TileMapState::instance.WalkLayer[_tile] - == DAT_PathFindingState::instance.searchGeneration) { - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper - = (int)DAT_TileMapState::instance.CertainPathLayer[_tile]; - } else { - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper = 10000000; - } - _index = _index + 1; - _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray + _index; - } while (_entry->tile1 != 0); + for (int i = 0; DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile1 != 0; ++i) { + int const tileUnk = DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile1; + if (DAT_TileMapState::instance.WalkLayer[tileUnk] + == DAT_PathFindingState::instance.searchGeneration) { + DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile3OrAnotherHelper + = DAT_TileMapState::instance.CertainPathLayer[tileUnk]; + } else { + DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile3OrAnotherHelper = 10000000; + } } - /* - Bubble sort of the reached destinations by cost, cheapest first. Both the - counter and the flag live in stack slots the original zeroes from one - register before the emptiness test (`xor ecx, ecx; cmp [..], ecx; - mov [esp + 0x14], ecx; mov [esp + 0x18], ecx`), so they are assigned at the - top of the outer loop and the test follows them. - */ - /* - The flag stays a `bool` although the original stores a dword into its slot - (`mov [esp + 0x18], ecx` and `mov [esp + 0x18], 1`) where ours stores the - byte `bl`: as an `int`, together with spelling both inner path-finding calls' - arguments identically so MSVC could share their push sequence the way the - original does, it measures 56.2% -> 54.9%. Whatever the original's flag was, - its slot widths are not what the remaining gap is made of. - */ - bool bVar7; + int swapped; do { - _index = 0; - bVar7 = false; - if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 == 0) + tribeIDThenSortIdx = 0; + swapped = 0; + if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 == 0) { break; + } do { - /* - Three separate tests, each jumping to the flag check at the bottom of - the outer loop (`test ebp, ebp; jz 0x00524aab`). - */ - int const _tile2 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile2OrAHelper; - if (!_tile2) + int const tile2 = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx] + .tile2OrAHelper; + if (tile2 == 0) { break; - int const _nextTile1 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile1; - if (!_nextTile1) + } + int const nextTile1 + = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1].tile1; + if (nextTile1 == 0) { break; - int const _nextTile2 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile2OrAHelper; - if (!_nextTile2) + } + int const nextTile2 + = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1] + .tile2OrAHelper; + if (nextTile2 == 0) { break; - int const _cost = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper; - int const _nextCost = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile3OrAnotherHelper; - if (_nextCost < _cost) { - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile1 = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile1; - bVar7 = true; - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile1 = _nextTile1; - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile2OrAHelper = _nextTile2; - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile2OrAHelper = _tile2; - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper = _nextCost; - DAT_PathFindingState::instance.searchQueue.destinationsArray[_index + 1].tile3OrAnotherHelper = _cost; } - _index = _index + 1; - } while (DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile1 != 0); - } while (bVar7); - /* - Compaction: the destinations the search did not reach are dropped and the - array is terminated with a zero tile. Both counters are zeroed before the - emptiness test again, and the write cursor is a pointer to the middle field - of its entry - `lea esi, [ + 4]` advanced by 0xc, with the three - fields written as [esi - 4], [esi] and [esi + 4]. - */ - int _keptCount = 0; - _index = 0; - if (DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile1 != 0) { - PathHelper12* _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray; - int* _write = &DAT_PathFindingState::instance.searchQueue.destinationsArray[0].tile2OrAHelper; - do { - if (DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper != 10000000) { - if (_index != _keptCount) { - _write[-1] = _entry->tile1; - *_write = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile2OrAHelper; - _write[1] = DAT_PathFindingState::instance.searchQueue.destinationsArray[_index].tile3OrAnotherHelper; - } - _keptCount = _keptCount + 1; - _write = _write + 3; + int const cost + = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile3OrAnotherHelper; + int const nextCost + = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1].tile3OrAnotherHelper; + if (cost > nextCost) { + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1].tile1 + = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile1; + swapped = 1; + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile1 + = nextTile1; + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile2OrAHelper + = nextTile2; + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1] + .tile2OrAHelper = tile2; + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile3OrAnotherHelper + = nextCost; + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1].tile3OrAnotherHelper + = cost; + } + ++tribeIDThenSortIdx; + } while (DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile1 != 0); + } while (swapped != 0); + int outCount = 0; + for (int i = 0; DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile1 != 0; ++i) { + if (DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile3OrAnotherHelper != 10000000) { + if (i != outCount) { + DAT_PathFindingState::instance.searchQueue.destinationsArray[outCount].tile1 + = DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile1; + DAT_PathFindingState::instance.searchQueue.destinationsArray[outCount].tile2OrAHelper + = DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile2OrAHelper; + DAT_PathFindingState::instance.searchQueue.destinationsArray[outCount].tile3OrAnotherHelper + = DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile3OrAnotherHelper; } - _index = _index + 1; - _entry = DAT_PathFindingState::instance.searchQueue.destinationsArray + _index; - } while (_entry->tile1 != 0); + ++outCount; + } } - DAT_PathFindingState::instance.searchQueue.destinationsArray[_keptCount].tile1 = 0; + DAT_PathFindingState::instance.searchQueue.destinationsArray[outCount].tile1 = 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp b/src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp index 06cf4de4e2..1c20651be1 100644 --- a/src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp +++ b/src/OpenSHC/Map/Units/TribesState/stopTribeMovementAndCheckIdle.cpp @@ -1,6 +1,5 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/UnitsState.func.hpp" #include "OpenSHC/Map/Units/States/UnitState.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" @@ -11,43 +10,37 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - using Map::Units::States::UnitState; - + // The tribe size is re-read every iteration (0x524906). // FUNCTION: STRONGHOLDCRUSADER 0x00524890 - undefined4 TribesState::stopTribeMovementAndCheckIdle(int param_1) + undefined4 TribesState::stopTribeMovementAndCheckIdle(int tribeID) { - short* psVar1; - int unitID; - int iVar2; - int unitSelectionIndex; - psVar1 = &this->tribes[param_1].size; - unitSelectionIndex = 0; - iVar2 = 0; - if (0 < *psVar1) { + short* const tribeSize = &this->tribes[tribeID].size; + int unitSelectionIndex = 0; + int busyUnitCount = 0; + if (*tribeSize > 0) { do { - unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if ((DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[unitID].dying == 0)) { - if ((DAT_UnitsState::instance.units[unitID].usingTeleport == 0) - && (DAT_UnitsState::instance.units[unitID].field303_0x413 == 0)) { - if (DAT_UnitsState::instance.units[unitID].state.generic - != Map::Units::States::US_MOVE_TO_DESTINATION) - continue; - MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + int const unitID + = MACRO_CALL_MEMBER(OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, unitSelectionIndex); + ++unitSelectionIndex; + if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0) { + if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0 + || DAT_UnitsState::instance.units[unitID].field303_0x413 != 0) { + ++busyUnitCount; + } else if (DAT_UnitsState::instance.units[unitID].state.generic + == States::US_MOVE_TO_DESTINATION) { + MACRO_CALL_MEMBER(UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(unitID); + ++busyUnitCount; } - iVar2 = iVar2 + 1; } - } while (unitSelectionIndex < *psVar1); - if (iVar2) { - return (undefined4)(0); + } while (unitSelectionIndex < *tribeSize); + if (busyUnitCount != 0) { + return 0; } } - return (undefined4)(1); + return 1; } } diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 9a71c80d06..9391993e87 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -1,74 +1,36 @@ -#include "../../../Map.func.hpp" #include "../TribesState.func.hpp" -#include "OpenSHC/OS.func.hpp" - -#include "string.h" -#include - namespace OpenSHC { namespace Map { namespace Units { + // Old record layout: 0x26C bytes per tribe (100-word member bitmask), tail of 0x16C bytes at old offset 0x100. + // The original calls the CRT memmove at 0x583760 twice (the first one guarded by dst != src, 0x523F16); there + // is no resolver for it, so the DLL's own CRT memmove is called (same semantics). The new second half of the + // bitmask is zeroed with rep stosd (0x32 dwords, 0x523F62): MSVC 2005 emits that for this loop but a call for + // memset(p, 0, 200) (tested). receivedMapVersion is compared signed (jge at 0x523EFF). Tribes 0x4E1 down to 0. // FUNCTION: STRONGHOLDCRUSADER 0x00523EF0 void TribesState::upgradeTribeArrayLayoutForMapVersion( PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) { - Tribe* _Dst; - int iVar2; - short* _Src; - short* psVar3; - /* - Two early-return guards rather than one wrapping `if`, which is where the - original places its epilogue. - */ - if (receivedMapVersion == packagerMapVersion) { - return; - } - if (169 <= (int)receivedMapVersion) { - return; + if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 0xa9) { + int tribeIndex = 1249; + char* src = (char*)this->tribes + 1249 * 0x26c; + Tribe* dst = &this->tribes[1249]; + do { + if ((void*)dst != (void*)src) { + memmove(dst, src, 0x26c); + } + short* maskTail = &dst->unitSelectionBitMasked[100]; + memmove(&dst->field34_0x1c8, src + 0x100, 0x16c); + --tribeIndex; + src -= 0x26c; + --dst; + for (int k = 0; k < 100; ++k) { + maskTail[k] = 0; + } + } while (tribeIndex >= 0); } - iVar2 = 1249; - _Dst = this->tribes + 0x4e1; - /* - 0x7a, which puts the cursor at `this + 0xbd114` - the `lea esi, [ecx + - 0xbd114]` the original starts its loop with. I changed this to 0xba earlier on - arithmetic that used 944 * 0x334 = 0xbcf40; the product is 0xbcfc0, so the - import's index was right all along and 0xba put the cursor 0x80 too high. - 0x28 + 944 * 0x334 + 0x38 + 0x7a * 2 == 0xbd114. diff_anchor.py names this - error directly - use it rather than doing the arithmetic by hand. - */ - _Src = this->tribes[944].unitSelectionBitMasked + 0x7a; - do { - if (_Dst != (Tribe*)_Src) { - MACRO_CALL(OS_Func::_memmove)(_Dst, (void*)((int)(_Src)), (size_t)((int)(620))); - } - psVar3 = _Dst->unitSelectionBitMasked; - MACRO_CALL(OS_Func::_memmove)( - &_Dst->field34_0x1c8, (void*)((int)(_Src + 0x80)), (size_t)((int)(364))); - iVar2 = iVar2 + -1; - _Src = _Src + -0x136; - _Dst = _Dst + -1; - psVar3 = psVar3 + 100; - /* - The original clears these 200 bytes with `mov ecx, 0x32` and a single - `rep stosd` - fifty dwords. Three spellings, measured: - - the decompiler's two-shorts-per-iteration loop 46.5% - memset(psVar3, 0, 200) 49.4% - __stosd((unsigned long*)psVar3, 0, 50) 57.5% - - The loop compiles to eight instructions. `memset` is better but this - build does not inline it - it emits `push 0xc8; push 0; push ebx; call - memset; add esp, 0x18`, so the original's `rep stosd` is an inlined - memset and /Oi is not on here. The intrinsic emits the instruction - directly, which is why it is used in preference to the more idiomatic - memset: it is the only one of the three that produces what the original - has. - */ - __stosd((unsigned long*)psVar3, 0, 50); - } while (-1 < iVar2); - return; } } From a26635218fb94f7a85c403cf04d23339ee955161 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:07:53 +0100 Subject: [PATCH 152/202] reimplement: addUnitToTribe 86.7 -> 100, taken from reimpl/TroopValueState-leftovers Its own index and mask locals before the compound |= (as on PR #528); measured 100% here in the normal build, no /GL needed. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/addUnitToTribe.cpp | 31 +++++-------------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp index 4b789884fb..b5a1a61132 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -1,6 +1,4 @@ -#include "../../../Map.func.hpp" - -#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "../TribesState.func.hpp" #include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -12,32 +10,19 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522590 void TribesState::addUnitToTribe(uint unitID, int tribeID) { - short* psVar1; this->tribes[tribeID].size = this->tribes[tribeID].size + 1; - /* - basically, it sets a bit flag for every unitID that is in the selection - */ - /* - The remainder is not named, which is a wash on the score (86.7% either way). - The gap here is a register: the original builds the cursor in a scratch - register (`mov edx, edi`, `imul edx, edx, 0x19a`, `add edx, eax`, - `lea eax, [ecx + edx*2 + 0x60]`) where ours needs EBP and so a push and a pop, - and dropping the local was not enough to free one. Naming the word index - instead, so the division runs before the multiply as it does in the original - (`cdq`, `and edx, 0xf`, `add eax, edx`, then `imul edx, edx, 0x19a`, then - `sar eax, 4`), is flat at 86.7% - MSVC still schedules the multiply first and - still wants EBP for it. - */ - psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); - *psVar1 = *psVar1 | DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]; + int const bitFlagsIndex = (int)unitID / 16; + int const bitMaskIndex = (int)unitID % 16; + this->tribes[tribeID].unitSelectionBitMasked[bitFlagsIndex] + |= DAT_UnitSelectionDefinedData::instance.BitMaskHelper[bitMaskIndex]; if (this->tribes[tribeID].selectionTargetUnitID == 0) { - this->tribes[tribeID].selectionTargetUnitID = (short)unitID; + this->tribes[tribeID].selectionTargetUnitID = unitID; } DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = this->tribes[tribeID].selectionTargetUnitID; - DAT_UnitsState::instance.units[unitID].tribeID = (short)tribeID; + DAT_UnitsState::instance.units[unitID].tribeID = tribeID; DAT_UnitsState::instance.units[unitID].tribeUID = this->tribes[tribeID].uid; DAT_UnitsState::instance.units[unitID].idInTribe = this->tribes[tribeID].size; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); + MACRO_CALL_MEMBER(TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 62255d4608..95040b75a5 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46791,7 +46791,7 @@ SHC_3BB0A8C1_0x00522520 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522550 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522590 | 86.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522590 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522630 | 79.5% | Reimplemented, asm differs From 3082c970092264169ce79d66baaf2001847696b3 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:08:12 +0100 Subject: [PATCH 153/202] TribesState: follow main's headers after the merge - Struct fields renamed upstream (fieldNNN_0xXXX -> field_0xXXX, Building field68_0xc2 -> targetMarker), via refactor_struct_fields.py. - PlayerData: keep/campground are buildingEntryInfos[BEIE_KEEP/BEIE_CAMPGROUND], counter is availableCows. - SiegeUnitCounts lives in AI::Siege; createTribeWithSpawnedUnit takes an AITribeType, so spawnQueuedReinforcementWaves keeps its tribe type as one. - Six functions return BOOLEnum in main's header (TRUE/FALSE returns). - findFreeSpaceNextToEnemyDefensiveStructureInSameAreaWithinDistance takes the area as int. - upgradeTribeArrayLayoutForMapVersion calls memmove through main's new OS_Func::_memmove resolver (62.5 -> 72.5). Status lines re-measured (both resolver sets on): applyUnitTopSpeedDelayBasedOnTribeSize 71.7, isTribePathToDestinationClear 66.1, predictUnitInterceptPosition 84.6, snapshotSelectionTribeAndComputeStance 96.8, sortTribePathDestinationsByCost 58.7. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../assignAttackTargetsForTribe.cpp | 8 +- .../TribesState/commandUnitsToLocation.cpp | 12 +- .../TribesState/computeTribePercentages.cpp | 6 +- .../Map/Units/TribesState/createTribe.cpp | 9 +- .../TribesState/createTribeForPlayer.cpp | 6 +- .../drawFlagsAndUnitDestinations.cpp | 6 +- .../TribesState/giveTribeAnInstruction.cpp | 237 +++++++++--------- .../TribesState/giveTribeMoveInstruction.cpp | 24 +- .../isTribePathToDestinationClear.cpp | 3 +- .../isTribeUnitBlockedByOtherUnit.cpp | 18 +- .../moveTribeToIndexedNearbyTile.cpp | 2 +- .../TribesState/moveUnitToBehaviorTarget.cpp | 18 +- .../playUnitCombatSpeechForTarget.cpp | 10 +- .../Units/TribesState/playUnitMoveSpeech.cpp | 22 +- .../TribesState/playUnitSelectionSound.cpp | 26 +- .../predictUnitInterceptPosition.cpp | 6 +- .../Units/TribesState/processDeerMoving.cpp | 2 +- .../Units/TribesState/removeUnitFromTribe.cpp | 2 +- .../sortTribePathDestinationsByCost.cpp | 21 +- .../TribesState/spawnDeerLionOrRabbit.cpp | 9 +- .../spawnQueuedReinforcementWaves.cpp | 132 +++++----- .../Map/Units/TribesState/trimTribeToSize.cpp | 6 +- .../TribesState/updateRabbitTribeBehavior.cpp | 3 +- .../updateTribeCombatStanceBehavior.cpp | 11 +- .../updateTribeUnitAssignments.cpp | 4 +- .../upgradeTribeArrayLayoutForMapVersion.cpp | 10 +- status/addresses-SHC-3BB0A8C1.txt | 16 +- 27 files changed, 323 insertions(+), 306 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp index baefb2eeec..b84e266831 100644 --- a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -40,7 +40,7 @@ namespace Map { (44.6% -> 44.4%). */ // FUNCTION: STRONGHOLDCRUSADER 0x005244D0 - undefined4 TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType) + BOOLEnum TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType) { short sVar3; uint _unitID; @@ -56,7 +56,7 @@ namespace Map { int unitSelectionIndex = 0; BOOLEnum BVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[this->tribes[tribeID].owner] != -1) { - return (undefined4)(0); + return FALSE; } if (0 < this->tribes[tribeID].size) { do { @@ -66,7 +66,7 @@ namespace Map { if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_unitID].dying == 0 && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 - && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID].field_0x413 == 0 && DAT_UnitsState::instance.units[_unitID].moveableUnk != 0) { /* fixme: this is some interesting data happening all over the unit code @@ -222,7 +222,7 @@ namespace Map { } } while (unitSelectionIndex < this->tribes[tribeID].size); } - return (undefined4)(1); + return TRUE; } } diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index c10270b373..c8fd7f89c7 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -18,12 +18,12 @@ namespace Map { using Map::Units::UnitLogicState; using Map::Units::UnitType; - using WindowsHelper::Enums::BOOLEnum; using Map::Units::Instructions::UnitMatchSpeedEnum; using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; // FUNCTION: STRONGHOLDCRUSADER 0x00526F00 - undefined4 TribesState::commandUnitsToLocation( + BOOLEnum TribesState::commandUnitsToLocation( int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) { int _unitID; @@ -74,7 +74,7 @@ namespace Map { && (_unitType != Map::Units::UT_S_BATTERINGRAM)))))))) && (DAT_UnitsState::instance.units[_unitID].engineerRelatedUnk == 0 || ((DAT_UnitsState::instance.units[_unitID].resourceToDeposit != 0 - && (DAT_UnitsState::instance.units[_unitID].field252_0x3c4 == 0))))) { + && (DAT_UnitsState::instance.units[_unitID].field_0x3c4 == 0))))) { DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; bVar1 = FALSE; } else { @@ -86,16 +86,16 @@ namespace Map { An early return, which is where the original puts its own `xor eax, eax`. */ if (bVar1) { - return (undefined4)(0); + return FALSE; } if ((char)matchUnitSpeeds != '\0') { this->tribes[tribeID].freeUnitSpeeds = 0; } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); - return (undefined4)(1); + return TRUE; } - return (undefined4)(0); + return FALSE; } } diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 384592fd4d..9050328e50 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -54,9 +54,9 @@ namespace Map { this->tribes[tribeID].percentageDyingUnk = 0; this->tribes[tribeID].percentageMovingUnk = 0; this->tribes[tribeID].percentageAttackingUnk = 0; - this->tribes[tribeID].field150_0x29c = 0; + this->tribes[tribeID].field_0x29c = 0; this->tribes[tribeID].percentageShootingUnk = 0; - this->tribes[tribeID].field152_0x2a0 = 0; + this->tribes[tribeID].field_0x2a0 = 0; this->tribes[tribeID].percentageSomething = 0; this->tribes[tribeID].movementSpeed = DAT_UnitsState::instance.units[_targetUnitID].movementSpeed; int local_8 = 0; @@ -105,7 +105,7 @@ namespace Map { | Map::Units::States::US_RELOAD_WEAPONUnk)) { local_8 = local_8 + 1; } - if (DAT_UnitsState::instance.units[_unitID].field316_0x430 != 0 + if (DAT_UnitsState::instance.units[_unitID].field_0x430 != 0 && ((UVar2 == Map::Units::States::US_RELOAD_WEAPONUnk || (UVar2 == Map::Units::States::US_AIM_WEAPONUnk)) || (UVar2 == Map::Units::States::US_FIRE_WEAPONUnk))) { diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index fb5fcfb86a..0b2c21d66c 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -37,8 +37,7 @@ namespace Map { reaching the fields as `[esi + 0x2c]` and so on. A reference folds the 0x28 into one lea and shifts every field displacement by it. */ - TribesState* const _tb - = (TribesState*)((char*)this + _tribeID * sizeof(Tribe)); + TribesState* const _tb = (TribesState*)((char*)this + _tribeID * sizeof(Tribe)); MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), '\0', &_tb->tribes[0]); /* @@ -60,10 +59,10 @@ namespace Map { DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; _tb->tribes[0].tribeType = ((AITribeType)0); - _tb->tribes[0].field18_0x1c = 0; - _tb->tribes[0].field19_0x1e = 0; + _tb->tribes[0].field_0x1c = 0; + _tb->tribes[0].field_0x1e = 0; _tb->tribes[0].tribeBehaviorType = ((SomeTribeBehaviorType)0); - _tb->tribes[0].field29_0x30 = 0; + _tb->tribes[0].field_0x30 = 0; _tb->tribes[0].selectionTargetUnitID = 0; _tb->tribes[0].size = 0; return _tribeID; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index cc19452895..f10511b914 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -85,10 +85,10 @@ namespace Map { this->tribes[_selectionID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; this->tribes[_selectionID].tribeType = ((AITribeType)0); - this->tribes[_selectionID].field18_0x1c = 0; - this->tribes[_selectionID].field19_0x1e = 0; + this->tribes[_selectionID].field_0x1c = 0; + this->tribes[_selectionID].field_0x1e = 0; this->tribes[_selectionID].tribeBehaviorType = ((SomeTribeBehaviorType)0); - this->tribes[_selectionID].field29_0x30 = 0; + this->tribes[_selectionID].field_0x30 = 0; this->tribes[_selectionID].selectionTargetUnitID = 0; this->tribes[_selectionID].size = 0; return _selectionID; diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index 7ad496f1e1..da28084767 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -166,7 +166,7 @@ namespace Map { .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID) && (DAT_UnitsState::instance.units[sVar1].logicalState == Map::Units::ULS_NORMAL)) { - DAT_UnitsState::instance.units[sVar1].field45_0x6c = 1; + DAT_UnitsState::instance.units[sVar1].field_0x6c = 1; } } else if (UVar2 == Map::Units::UIT_ATTACK_BUILDING) { sVar1 = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; @@ -174,7 +174,7 @@ namespace Map { && (DAT_BuildingsState::instance.buildings[sVar1].uid == DAT_UnitsState::instance.units[_unitID] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { - DAT_BuildingsState::instance.buildings[sVar1].field68_0xc2 = 1; + DAT_BuildingsState::instance.buildings[sVar1].targetMarker = 1; } } else if (UVar2 == Map::Units::UIT_LIGHT_PITCH) { sVar1 = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; @@ -193,7 +193,7 @@ namespace Map { if ((uVar3) && (DAT_TileMapState::instance.BuildingLayer[uVar3] != 0)) { DAT_BuildingsState::instance .buildings[DAT_TileMapState::instance.BuildingLayer[uVar3]] - .field68_0xc2 = 1; + .targetMarker = 1; } } continue; diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp index 9dbd07e940..fb47db9e1e 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeAnInstruction.cpp @@ -111,7 +111,7 @@ namespace Map { not a way into the register allocation. */ // FUNCTION: STRONGHOLDCRUSADER 0x00527C80 - undefined4 TribesState::giveTribeAnInstruction( + BOOLEnum TribesState::giveTribeAnInstruction( int tribeID, UnitInstructionType unitInstructionType, int id, int unitUID, int param_5) { short* psVar1; @@ -311,13 +311,13 @@ namespace Map { _unitSelectionIndex = _unitSelectionIndex + 1; if ((DAT_UnitsState::instance.units[_unitID_0x03].logicalState == Map::Units::ULS_NORMAL) && (DAT_UnitsState::instance.units[_unitID_0x03].dying == 0)) { - DAT_UnitsState::instance.units[_unitID_0x03].field253_0x3c5 = 3; + DAT_UnitsState::instance.units[_unitID_0x03].field_0x3c5 = 3; DAT_UnitsState::instance.units[_unitID_0x03].targetingType = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; - DAT_UnitsState::instance.units[_unitID_0x03].field243_0x3b0 = 0; + DAT_UnitsState::instance.units[_unitID_0x03].field_0x3b0 = 0; } } while (_unitSelectionIndex < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } break; case Map::Units::UIT_STOP: @@ -331,11 +331,11 @@ namespace Map { iVar11 = iVar11 + 1; if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[iVar12].dying == 0 - && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0) { - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 3; + && DAT_UnitsState::instance.units[iVar12].field_0x413 == 0) { + DAT_UnitsState::instance.units[iVar12].field_0x3c5 = 3; DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; - DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 0; + DAT_UnitsState::instance.units[iVar12].field_0x3b0 = 0; MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(iVar12); @@ -343,7 +343,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12].movementType_OR_targetUnitID = 0; } } while (iVar11 < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } break; case Map::Units::UIT_UNIT_ATTACK_UNIT: @@ -359,7 +359,7 @@ namespace Map { */ id = 0; if (pUVar6->uid != unitUID) { - return (undefined4)(0); + return FALSE; } if (0 < _tribeSize) { do { @@ -368,7 +368,7 @@ namespace Map { _unitTribeIndex = _unitTribeIndex + 1; if (DAT_UnitsState::instance.units[_unitID_0x04].logicalState != Map::Units::ULS_NORMAL || DAT_UnitsState::instance.units[_unitID_0x04].dying != 0 - || DAT_UnitsState::instance.units[_unitID_0x04].field303_0x413 != 0) + || DAT_UnitsState::instance.units[_unitID_0x04].field_0x413 != 0) continue; DAT_UnitsState::instance.units[_unitID_0x04]._someX_2 = 0; DAT_UnitsState::instance.units[_unitID_0x04]._someY_2 = 0; @@ -399,7 +399,7 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_0x04] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID = unitUID; - DAT_UnitsState::instance.units[_unitID_0x04].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[_unitID_0x04].field_0x3f8 = 0; MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(_unitID_0x04); @@ -432,7 +432,7 @@ namespace Map { .targetedUnitID__OR__engineerMannedSiegeEngineRef = _param_3_id_x_tile_buildingID_copy; DAT_UnitsState::instance.units[_unitID_0x04].shootTargetedUnit = _param_3_id_x_tile_buildingID_copy; - DAT_UnitsState::instance.units[_unitID_0x04].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[_unitID_0x04].field_0x3f8 = 0; MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(_unitID_0x04); @@ -460,7 +460,7 @@ namespace Map { break; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TREBUCHET: - DAT_UnitsState::instance.units[_unitID_0x04].field253_0x3c5 = 5; + DAT_UnitsState::instance.units[_unitID_0x04].field_0x3c5 = 5; DAT_UnitsState::instance.units[_unitID_0x04].targetingType = Map::Units::UIT_ATTACK_LAND; DAT_UnitsState::instance.units[_unitID_0x04].attackAtTileX = DAT_UnitsState::instance.units[iVar11].x; @@ -498,8 +498,7 @@ namespace Map { this->tribes[tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; iVar12 = 0; sVar9 = this->tribes[tribeID].size; - this->tribes[tribeID].someTile2 - = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + this->tribes[tribeID].someTile2 = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + DAT_ViewportRenderState::instance .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] .addXgetTile; @@ -512,7 +511,7 @@ namespace Map { iVar12 = iVar12 + 1; if (DAT_UnitsState::instance.units[iVar11].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[iVar11].dying == 0 - && DAT_UnitsState::instance.units[iVar11].field303_0x413 == 0) { + && DAT_UnitsState::instance.units[iVar11].field_0x413 == 0) { UVar4 = DAT_UnitsState::instance.units[iVar11].unitType; switch (UVar4) { case Map::Units::UT_TUNNELER: @@ -613,8 +612,8 @@ namespace Map { if (iVar11 < 9) { iVar11 = 8; } - this->tribes[tribeID].field166_0x2be = (short)iVar11; - return (undefined4)(1); + this->tribes[tribeID].field_0x2be = (short)iVar11; + return TRUE; } } break; @@ -626,7 +625,7 @@ namespace Map { _unitTribeIndex = 0; id = 0; if (pUVar6->uid != unitUID) { - return (undefined4)(0); + return FALSE; } if (0 < _tribeSize) { do { @@ -635,7 +634,7 @@ namespace Map { _unitTribeIndex = _unitTribeIndex + 1; if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[iVar12].dying == 0 - && DAT_UnitsState::instance.units[iVar12].field303_0x413 == 0 + && DAT_UnitsState::instance.units[iVar12].field_0x413 == 0 && (UVar3 = DAT_UnitsState::instance.units[iVar12].state.generic, UVar3 != Map::Units::States::US_MELEE_ATTACK)) { /* @@ -676,7 +675,7 @@ namespace Map { DAT_UnitsState::instance.units[iVar12] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID = unitUID; - DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[iVar12].field_0x3f8 = 0; if (DAT_UnitsState::instance.units[iVar11].unitType == Map::Units::UT_RABBIT) { this->tribes[tribeID].containsRabbit = 1; } @@ -709,7 +708,7 @@ namespace Map { case Map::Units::UT_S_TREBUCHET: if (unitInstructionType != Map::Units::UIT_SHOOT_TARGETUnk) { sVar9 = DAT_UnitsState::instance.units[iVar12]._someX_2; - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 5; + DAT_UnitsState::instance.units[iVar12].field_0x3c5 = 5; DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_LAND; DAT_UnitsState::instance.units[iVar12].attackAtTileX = DAT_UnitsState::instance.units[iVar11].x; @@ -751,8 +750,7 @@ namespace Map { this->tribes[tribeID].someTile = DAT_UnitsState::instance.units[iVar11].tile; iVar12 = 0; sVar9 = this->tribes[tribeID].size; - this->tribes[tribeID].someTile2 - = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + this->tribes[tribeID].someTile2 = (int)DAT_UnitsState::instance.units[iVar11].destinationX_2Unk + DAT_ViewportRenderState::instance .translationMatrix[DAT_UnitsState::instance.units[iVar11].destinationY_2Unk] .addXgetTile; @@ -765,7 +763,7 @@ namespace Map { iVar12 = iVar12 + 1; if (DAT_UnitsState::instance.units[_unitID_0x20].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_unitID_0x20].dying == 0 - && DAT_UnitsState::instance.units[_unitID_0x20].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID_0x20].field_0x413 == 0 && DAT_UnitsState::instance.units[_unitID_0x20].state.generic != Map::Units::States::US_MELEE_ATTACK) { switch (DAT_UnitsState::instance.units[_unitID_0x20].unitType) { @@ -805,7 +803,8 @@ namespace Map { } DAT_UnitsState::instance.units[_unitID_0x20].plannedDestinationX = (short)_x_528565; - DAT_UnitsState::instance.units[_unitID_0x20].plannedDestinationY = _y_528565; + DAT_UnitsState::instance.units[_unitID_0x20].plannedDestinationY + = _y_528565; DAT_UnitsState::instance.units[_unitID_0x20].targetedBuildingTile = 0; if (DAT_UnitsState::instance.units[_unitID_0x20].state.generic == Map::Units::States::US_MELEE_ATTACK) { @@ -831,15 +830,17 @@ namespace Map { DAT_UnitsState::ptr)(_unitID_0x20, _x_528565, (uint)(_y_528565), 0); DAT_UnitsState::instance.units[_unitID_0x20].moveDelay = 0; DAT_UnitsState::instance.units[_unitID_0x20].lookForEnemy = -0x32; - if (DAT_UnitsState::instance.units[_unitID_0x20].movementType_OR_targetUnitID + if (DAT_UnitsState::instance.units[_unitID_0x20] + .movementType_OR_targetUnitID != iVar11) { - DAT_UnitsState::instance.units[_unitID_0x20].movementType_OR_targetUnitID - = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_0x20] + .movementType_OR_targetUnitID = _param_3_id_x_tile_buildingID_copy; psVar1 = (short*)((int)&DAT_UnitsState::instance.units[0].huntedBy + local_8); *psVar1 = *psVar1 + 1; } - UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance.units[_unitID_0x20] + UVar10 = (UnitInstructionType)((int)DAT_UnitsState::instance + .units[_unitID_0x20] .totalSizeOfPathPlan / 2); if ((int)UVar10 > (int)unitInstructionType) { @@ -856,8 +857,8 @@ namespace Map { if (iVar11 < 9) { iVar11 = 8; } - this->tribes[tribeID].field166_0x2be = (short)iVar11; - return (undefined4)(1); + this->tribes[tribeID].field_0x2be = (short)iVar11; + return TRUE; } } break; @@ -871,10 +872,10 @@ namespace Map { if (DAT_UnitsState::instance.units[iVar12].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[iVar12].dying == 0 && (int)(short)DAT_UnitsState::instance.units[iVar12].unitType - 0x27U < 2) { - DAT_UnitsState::instance.units[iVar12].field243_0x3b0 = 1; + DAT_UnitsState::instance.units[iVar12].field_0x3b0 = 1; } } while (iVar11 < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } break; case Map::Units::UIT_ATTACK_LAND: @@ -905,18 +906,18 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_0x05_0x16].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; } - DAT_UnitsState::instance.units[_unitID_0x05_0x16].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field_0x3f8 = 0; DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType = (UnitInstructionTypeShort)unitInstructionType; LAB_00528808: - DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field_0x3c5 = (byte)unitInstructionType; sVar9 = (short)_param_4_unitUID_Y_copy; if (unitInstructionType == Map::Units::UIT_THROW_COW && (UVar4 = DAT_UnitsState::instance.units[_unitID_0x05_0x16].unitType, UVar4 == Map::Units::UT_S_CATAPULT || (UVar4 == Map::Units::UT_S_TREBUCHET))) { - if (DAT_GameState::instance.playerDataArray[unitUID].counter <= 0) { - DAT_UnitsState::instance.units[_unitID_0x05_0x16].field253_0x3c5 = 5; + if (DAT_GameState::instance.playerDataArray[unitUID].availableCows <= 0) { + DAT_UnitsState::instance.units[_unitID_0x05_0x16].field_0x3c5 = 5; } else { DAT_UnitsState::instance.units[_unitID_0x05_0x16].targetingType = Map::Units::UIT_THROW_COW; @@ -950,7 +951,7 @@ namespace Map { } } if (this->tribes[tribeID].size <= _unitTribeIndex) { - return (undefined4)(1); + return TRUE; } } while (true); } @@ -979,7 +980,7 @@ namespace Map { DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; } } while (iVar11 < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } break; case Map::Units::UIT_LIGHT_PITCH: @@ -1010,9 +1011,9 @@ namespace Map { DAT_UnitsState::instance.units[iVar12] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID = unitUID; - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 0x22; + DAT_UnitsState::instance.units[iVar12].field_0x3c5 = 0x22; DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_LIGHT_PITCH; - DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[iVar12].field_0x3f8 = 0; MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(iVar12); @@ -1020,7 +1021,7 @@ namespace Map { } } } while (_unitTribeIndex < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } /* An explicit break: this arm sat last and fell out of the switch to the @@ -1043,7 +1044,7 @@ namespace Map { _unitTribeIndex = _unitTribeIndex + 1; if (DAT_UnitsState::instance.units[iVar12].logicalState != Map::Units::ULS_NORMAL || DAT_UnitsState::instance.units[iVar12].dying != 0 - || DAT_UnitsState::instance.units[iVar12].field303_0x413 != 0) + || DAT_UnitsState::instance.units[iVar12].field_0x413 != 0) continue; DAT_UnitsState::instance.units[iVar12]._someX_2 = 0; DAT_UnitsState::instance.units[iVar12]._someY_2 = 0; @@ -1113,15 +1114,15 @@ namespace Map { .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID = unitUID; LAB_00528c46: - DAT_UnitsState::instance.units[iVar12].field253_0x3c5 = 9; + DAT_UnitsState::instance.units[iVar12].field_0x3c5 = 9; DAT_UnitsState::instance.units[iVar12].targetingType = Map::Units::UIT_ATTACK_BUILDING; - DAT_UnitsState::instance.units[iVar12].field283_0x3f8 = 0; + DAT_UnitsState::instance.units[iVar12].field_0x3f8 = 0; MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(iVar12); DAT_UnitsState::instance.units[iVar12].shootBeforeStop = 10; } - break; + break; case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TREBUCHET: case Map::Units::UT_S_MANGONEL: @@ -1202,7 +1203,7 @@ namespace Map { _unitTribeIndex = _unitTribeIndex + 1; if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0) { + && DAT_UnitsState::instance.units[_unitID].field_0x413 == 0) { UVar4 = DAT_UnitsState::instance.units[_unitID].unitType; /* Arm order from 0x52a490: three dwords, 0x48-entry byte @@ -1323,7 +1324,7 @@ namespace Map { } } if (this->tribes[tribeID].size <= _unitTribeIndex) { - return (undefined4)(1); + return TRUE; } } while (true); } @@ -1343,7 +1344,7 @@ namespace Map { _unitTribeIndex = _unitTribeIndex + 1; if (DAT_UnitsState::instance.units[uVar13].logicalState != Map::Units::ULS_NORMAL || DAT_UnitsState::instance.units[uVar13].dying != 0 - || DAT_UnitsState::instance.units[uVar13].field303_0x413 != 0) + || DAT_UnitsState::instance.units[uVar13].field_0x413 != 0) continue; DAT_UnitsState::instance.units[uVar13]._someX_2 = 0; DAT_UnitsState::instance.units[uVar13]._someY_2 = 0; @@ -1394,7 +1395,7 @@ namespace Map { DAT_UnitsState::instance.units[uVar13].attackAtTileX = _param_3_id_x_tile_buildingID_copy - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; - DAT_UnitsState::instance.units[uVar13].field253_0x3c5 = 0x17; + DAT_UnitsState::instance.units[uVar13].field_0x3c5 = 0x17; DAT_UnitsState::instance.units[uVar13].targetingType = Map::Units::UIT_ATTACK_WALL; MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, @@ -1476,8 +1477,8 @@ namespace Map { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: findFreeSpaceNextToEnemyDefensiveStructureInSameAreaWithinDistance, DAT_PathFindingState::ptr)(iVar11, id, - (int*)((int)((short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[sVar9].tile])), + (int)((short)DAT_TileMapState::instance + .PathConnectionLayer[DAT_UnitsState::instance.units[sVar9].tile]), iVar12, 0, (int)(sVar8)); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::sortTribePathDestinationsByCost, this)( tribeID, _ramOrHorse); @@ -1485,12 +1486,13 @@ namespace Map { && (iVar12 = 0, 0 < this->tribes[tribeID].size)) { id = 0x12d5c70; do { - _unitID_attackWall = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar12); + _unitID_attackWall = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); iVar12 = iVar12 + 1; - if (DAT_UnitsState::instance.units[_unitID_attackWall].logicalState == Map::Units::ULS_NORMAL + if (DAT_UnitsState::instance.units[_unitID_attackWall].logicalState + == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_unitID_attackWall].dying == 0 - && DAT_UnitsState::instance.units[_unitID_attackWall].field303_0x413 == 0) { + && DAT_UnitsState::instance.units[_unitID_attackWall].field_0x413 == 0) { UVar4 = DAT_UnitsState::instance.units[_unitID_attackWall].unitType; switch (UVar4) { case Map::Units::UT_E_ARCHER: @@ -1556,28 +1558,34 @@ namespace Map { .addXgetTile; BVar16 = MACRO_CALL_MEMBER( Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(_unitID_attackWall, (uint)(unitUID), (uint)(sVar9), 0); + DAT_UnitsState::ptr)( + _unitID_attackWall, (uint)(unitUID), (uint)(sVar9), 0); if (BVar16) { DAT_UnitsState::instance.units[_unitID_attackWall].targetingType = ((UnitInstructionType)0); - DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationX - = (short)unitUID; - DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationY = sVar9; + DAT_UnitsState::instance.units[_unitID_attackWall] + .plannedDestinationX = (short)unitUID; + DAT_UnitsState::instance.units[_unitID_attackWall] + .plannedDestinationY = sVar9; if (DAT_UnitsState::instance.units[_unitID_attackWall].state.generic == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[_unitID_attackWall].movementCooldown - = DAT_UnitsState::instance.units[_unitID_attackWall].movementSpeed * -4; + DAT_UnitsState::instance.units[_unitID_attackWall] + .movementCooldown + = DAT_UnitsState::instance.units[_unitID_attackWall] + .movementSpeed + * -4; } DAT_UnitsState::instance.units[_unitID_attackWall].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[_unitID_attackWall].movementType_OR_targetUnitID - = 0; + DAT_UnitsState::instance.units[_unitID_attackWall] + .movementType_OR_targetUnitID = 0; DAT_PathFindingState::instance.notAllAssassinsUnk = 0; sVar9 = DAT_ViewportRenderState::instance .tileTranslationMatrix_YComponent[iVar11]; - DAT_UnitsState::instance.units[_unitID_attackWall].targetedBuildingTile - = iVar11; - DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileY = sVar9; + DAT_UnitsState::instance.units[_unitID_attackWall] + .targetedBuildingTile = iVar11; + DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileY + = sVar9; DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileX = _param_3_id_x_tile_buildingID_copy - (short)DAT_ViewportRenderState::instance @@ -1594,8 +1602,10 @@ namespace Map { .tileTranslationMatrix_YComponent[iVar15]; uVar13 = iVar15 - DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; - DAT_UnitsState::instance.units[_unitID_attackWall].targetingType = ((UnitInstructionType)0); - DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationX = (short)uVar13; + DAT_UnitsState::instance.units[_unitID_attackWall].targetingType + = ((UnitInstructionType)0); + DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationX + = (short)uVar13; DAT_UnitsState::instance.units[_unitID_attackWall].plannedDestinationY = sVar9; DAT_PathFindingState::instance.notAllAssassinsUnk = 1; if (DAT_UnitsState::instance.units[_unitID_attackWall].state.generic @@ -1610,8 +1620,10 @@ namespace Map { DAT_PathFindingState::instance.notAllAssassinsUnk = 0; uVar13 = *(uint*)id; sVar9 = *(short*)id; - DAT_UnitsState::instance.units[_unitID_attackWall].movementType_OR_targetUnitID = 0; - DAT_UnitsState::instance.units[_unitID_attackWall].targetedBuildingTile = uVar13; + DAT_UnitsState::instance.units[_unitID_attackWall].movementType_OR_targetUnitID + = 0; + DAT_UnitsState::instance.units[_unitID_attackWall].targetedBuildingTile + = uVar13; sVar8 = DAT_ViewportRenderState::instance .tileTranslationMatrix_YComponent[uVar13]; DAT_UnitsState::instance.units[_unitID_attackWall].attackAtTileY = sVar8; @@ -1623,7 +1635,7 @@ namespace Map { } } if (this->tribes[tribeID].size <= iVar12) { - return (undefined4)(1); + return TRUE; } } while (true); } @@ -1636,8 +1648,8 @@ namespace Map { this->tribes[tribeID].isRallyingUnk = 0; if (0 < _tribeSize) { do { - _unitID_setLadder = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar12); + _unitID_setLadder = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); iVar12 = iVar12 + 1; if (DAT_UnitsState::instance.units[_unitID_setLadder].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_unitID_setLadder].dying == 0 @@ -1711,7 +1723,7 @@ namespace Map { - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar9].addXgetTile; } } while (iVar11 < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } } } @@ -1725,7 +1737,7 @@ namespace Map { DAT_PathFindingState::ptr)( this->tribes[tribeID].owner, (dword)((int)((short)_targetArea)), (uint)(id), (uint)(unitUID)); if (iVar11 == 0) { - return (undefined4)(1); + return TRUE; } unitUID = DAT_PathFindingState::instance.climbY; uVar13 = DAT_PathFindingState::instance.climbX; @@ -1739,8 +1751,8 @@ namespace Map { tribeID, uVar13, (uint)(unitUID), 0, 1); if (0 < this->tribes[tribeID].size) { do { - _unitID_digMoat = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar12); + _unitID_digMoat = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar12); iVar12 = iVar12 + 1; if ((DAT_UnitsState::instance.units[_unitID_digMoat].logicalState == Map::Units::ULS_NORMAL) && (DAT_UnitsState::instance.units[_unitID_digMoat].dying == 0)) { @@ -1755,10 +1767,12 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_digMoat].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; } - DAT_UnitsState::instance.units[_unitID_digMoat].targetingType = (undefined2)unitInstructionType; + DAT_UnitsState::instance.units[_unitID_digMoat].targetingType + = (undefined2)unitInstructionType; DAT_UnitsState::instance.units[_unitID_digMoat].attackAtTileX = (short)uVar13; DAT_UnitsState::instance.units[_unitID_digMoat].attackAtTileY = (short)iVar11; - DAT_UnitsState::instance.units[_unitID_digMoat].targetX = _param_3_id_x_tile_buildingID_copy; + DAT_UnitsState::instance.units[_unitID_digMoat].targetX + = _param_3_id_x_tile_buildingID_copy; DAT_UnitsState::instance.units[_unitID_digMoat].targetY = (UnitStateShort)_param_4_unitUID_Y_copy; DAT_UnitsState::instance.units[_unitID_digMoat].targetedBuildingTile = 0; @@ -1768,7 +1782,7 @@ namespace Map { } } } while (iVar12 < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } break; case Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED: @@ -1780,7 +1794,7 @@ namespace Map { BVar16 = MACRO_CALL_MEMBER( Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)((int)_x, (uint)(_y)); if (!BVar16) { - return (undefined4)(0); + return FALSE; } param_5 = (int)(short)DAT_TileMapState::instance .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile @@ -1829,7 +1843,7 @@ namespace Map { */ if (((((uint)(extraout_var) & 0xffffff) << 8) | (uint)(byte)(cVar7)) != 0 && (_buildingUID = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: - calculateCanPlayerUnitsNavigateToAreaFromArea, + calculateCanPlayerUnitsNavigateToAreaFromArea, DAT_PathFindingState::ptr)((int)DAT_UnitsState::instance.units[uVar13].owner, (dword)((int)(param_5)), (dword)((int)((short)DAT_TileMapState::instance @@ -1861,14 +1875,14 @@ namespace Map { unitUID = unitUID + -1; iVar12 = _unitTribeIndex; if (unitUID == 0) { - return (undefined4)(1); + return TRUE; } } } } _unitTribeIndex = iVar12; } while (_unitTribeIndex < this->tribes[tribeID].size); - return (undefined4)(1); + return TRUE; } break; case Map::Units::UIT_MAN_SIEGE_EQUIPMENT: @@ -1896,10 +1910,12 @@ namespace Map { unitInstructionType = ((UnitInstructionType)0); do { _siegeCrewIndex = (int)DAT_UnitsState::instance.units[iVar11] - .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine; - x = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[_siegeCrewIndex][0] + .digTileY__OR__countLifeCycleEngineersSentToManSiegeEngine; + x = (int) + DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[_siegeCrewIndex][0] + (int)DAT_UnitsState::instance.units[iVar11].x; - y = (int)DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[_siegeCrewIndex][1] + y = (int) + DAT_AttackInfoDefinedData::instance.SiegeCrewPositionOffsets[_siegeCrewIndex][1] + (int)DAT_UnitsState::instance.units[iVar11].y; uVar19 = (_siegeCrewIndex + 1) % 16; DAT_UnitsState::instance.units[iVar11] @@ -1907,11 +1923,12 @@ namespace Map { BVar16 = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)(x, y); if (BVar16) { - _siegeCrewIndex = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + _siegeCrewIndex + = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; _param_4_unitUID_Y_copy = (dword)(short)DAT_TileMapState::instance.PathConnectionLayer[_siegeCrewIndex]; - uVar19 = MACRO_CALL_MEMBER( - Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)(_siegeCrewIndex); + uVar19 = MACRO_CALL_MEMBER(Map::TileMapState_Func::getTotalHeightAtTile, + DAT_TileMapState::ptr)(_siegeCrewIndex); uVar19 = ((int)DAT_UnitsState::instance.units[iVar11].buildingHeight + (int)DAT_UnitsState::instance.units[iVar11].terrainOrClimbHeight) - uVar19; @@ -1942,12 +1959,12 @@ namespace Map { unitUID = unitUID + -1; _siegeCrewIndex = iVar12; if (unitUID == 0) { - return (undefined4)(1); + return TRUE; } } iVar12 = _siegeCrewIndex; if (this->tribes[tribeID].size <= _siegeCrewIndex) { - return (undefined4)(1); + return TRUE; } } while (true); } @@ -1982,8 +1999,8 @@ namespace Map { do { _unitID_0x11 = (int)*(short*)tribeID; *(undefined2*)tribeID = 0; - DAT_UnitsState::instance.units[_unitID_0x11].field249_0x3c0 = 0; - DAT_UnitsState::instance.units[_unitID_0x11].field248_0x3bc = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field_0x3c0 = 0; + DAT_UnitsState::instance.units[_unitID_0x11].field_0x3bc = 0; DAT_UnitsState::instance.units[_unitID_0x11].animationCycleNumber = 0; DAT_UnitsState::instance.units[_unitID_0x11].state.generic = Map::Units::States::US_JESTER_ROAM_TO; @@ -1996,7 +2013,7 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_0x11].goToRallyPoint = 1; } DAT_UnitsState::instance.units[_unitID_0x11].updateTickTracker = 0; - DAT_UnitsState::instance.units[_unitID_0x11].field39_0x58 = 1; + DAT_UnitsState::instance.units[_unitID_0x11].field_0x58 = 1; unitUID = 0; LAB_00529da0: do { @@ -2046,18 +2063,14 @@ namespace Map { bVar2 = DAT_TileMapState::instance.HeightLayer[iVar12]; DAT_UnitsState::instance.units[_unitID_0x11].totalSizeOfPathPlan = 0; DAT_UnitsState::instance.units[_unitID_0x11].x = (short)_spawnX; - DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentXPosition - = (short)_spawnX; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentXPosition = (short)_spawnX; DAT_UnitsState::instance.units[_unitID_0x11].y = (short)_spawnY; - DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentYPosition - = (short)_spawnY; + DAT_UnitsState::instance.units[_unitID_0x11].mimicCurrentYPosition = (short)_spawnY; DAT_UnitsState::instance.units[_unitID_0x11].terrainOrClimbHeight = (ushort)bVar2; DAT_UnitsState::instance.units[_unitID_0x11].tile = iVar12; DAT_UnitsState::instance.units[_unitID_0x11].nextTileUnk = iVar12; - DAT_UnitsState::instance.units[_unitID_0x11].microXPosition - = (short)(_spawnX * 8 + 4); - DAT_UnitsState::instance.units[_unitID_0x11].microYPosition - = (short)(_spawnY * 8 + 4); + DAT_UnitsState::instance.units[_unitID_0x11].microXPosition = (short)(_spawnX * 8 + 4); + DAT_UnitsState::instance.units[_unitID_0x11].microYPosition = (short)(_spawnY * 8 + 4); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::updateMicroPosition, DAT_UnitsState::ptr)( _unitID_0x11); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::resetUnitMovementState, DAT_UnitsState::ptr)( @@ -2073,7 +2086,7 @@ namespace Map { .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 = 0; MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, DAT_UnitsState::ptr)(id); - return (undefined4)(1); + return TRUE; } break; case Map::Units::UIT_DISBAND: @@ -2119,8 +2132,8 @@ namespace Map { case Map::Units::UT_S_SHIELD: case Map::Units::UT_S_BALLISTA: case Map::Units::UT_S_FBALLISTA: - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - tribeID, Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID_0x1e, + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)(tribeID, + Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID_0x1e, DAT_UnitsState::instance.units[_unitID_0x1e].uid, 1); _gamemodeSiegeThat = DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT; DAT_UnitsState::instance.units[_unitID_0x1e].state.generic @@ -2143,7 +2156,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::deselectAllUnitsOneByOne, DAT_UnitsState::ptr)(); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)( this->tribes[tribeID].owner, 0, 0); - return (undefined4)(1); + return TRUE; case ((UnitInstructionType)0x15): this->tribes[tribeID].isRallyingUnk = 0; uVar13 = (uint)DAT_BuildingsState::instance.buildings[id].buildingEntryY; @@ -2152,7 +2165,7 @@ namespace Map { = MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::xyAreValid, DAT_ViewportRenderState::ptr)( (int)DAT_BuildingsState::instance.buildings[id].buildingEntryX, uVar13); if (!BVar16) { - return (undefined4)(0); + return FALSE; } param_5 = (int)(short)DAT_TileMapState::instance @@ -2196,7 +2209,7 @@ namespace Map { uVar13); MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::clearOrDeselectUnitFromSelection, DAT_UnitsState::ptr)(this->tribes[tribeID].owner, uVar13, 0); - return (undefined4)(1); + return TRUE; } } } while (_unitTribeIndex < this->tribes[tribeID].size); @@ -2206,7 +2219,7 @@ namespace Map { iVar11); break; } - return (undefined4)(1); + return TRUE; } } diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index ca8f8a2831..2253d6dc9e 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -152,7 +152,7 @@ namespace Map { int _tile = DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile + x1; int _area = (int)(short)DAT_TileMapState::instance.PathConnectionLayer[_tile]; if (!(DAT_TileMapState::instance.LogicLayer[_tile] & 0x30)) { - DAT_PathFindingState::instance.field50_0x98 = 0; + DAT_PathFindingState::instance.field_0x98 = 0; this->fcn_mtribe = this->fcn_mtribe + 1; _firstUnitID = MACRO_CALL_MEMBER( Map::Units::TribesState_Func::getFirstUnitInTribeThatIsOnXTerrain, this)(tribeID); @@ -245,7 +245,7 @@ namespace Map { this->tribes[tribeID].containsRabbit = 0; } this->ALG_ResultTileIndex = 0; - this->field6_0x18 = 0; + this->field_0x18 = 0; int _isWallGatehouseTower = (DAT_TileMapState::instance.LogicLayer[_tile] & 0x100U) != 0; int bVar5 = 0; int _isKeep = (DAT_TileMapState::instance.LogicLayer[_tile] & 0x10000000U) != 0; @@ -271,7 +271,7 @@ namespace Map { _searchBudget = _searchBudget + 500; if (0x13a10 <= _searchBudget || ((_bool1 || (_isDefensiveStructure)) && (8000 <= _searchBudget))) { - if (!DAT_PathFindingState::instance.field50_0x98) { + if (!DAT_PathFindingState::instance.field_0x98) { if (bVar4) { BVar8 = MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, @@ -335,7 +335,7 @@ namespace Map { if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_unitID].dying == 0 && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 - && DAT_UnitsState::instance.units[_unitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID].field_0x413 == 0 && DAT_UnitsState::instance.units[_unitID].state.generic != (UnitStateShort)0xcf && DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag == 0 && DAT_UnitsState::instance.units[_unitID].moveRelatedFlag != 1 @@ -364,7 +364,7 @@ namespace Map { .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 == 1))) { if (_unitType == Map::Units::UT_E_ENGINEER) { - DAT_UnitsState::instance.units[_unitID].field252_0x3c4 = 0; + DAT_UnitsState::instance.units[_unitID].field_0x3c4 = 0; } if (bVar4) { if (!_isKeep && !_isWallGatehouseTower) { @@ -440,7 +440,7 @@ namespace Map { if (DAT_UnitsState::instance.units[_unitID_2].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_unitID_2].dying == 0 && DAT_UnitsState::instance.units[_unitID_2].usingTeleport == 0 - && DAT_UnitsState::instance.units[_unitID_2].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_unitID_2].field_0x413 == 0 && (UVar1 = DAT_UnitsState::instance.units[_unitID_2].state.generic, UVar1 != ((UnitStateShort)0xcf)) && DAT_UnitsState::instance.units[_unitID_2].rallyRelatedFlag == 0 @@ -537,10 +537,10 @@ namespace Map { DAT_UnitsState::instance.units[_unitID_2].unitSpeedMatchingRelatedUnk = 0x10; } DAT_PathFindingState::instance.notAllAssassinsUnk = 0; - if (!DAT_PathFindingState::instance.field43_0x7c) { - this->field6_0x18 = this->field6_0x18 + 1; + if (!DAT_PathFindingState::instance.field_0x7c) { + this->field_0x18 = this->field_0x18 + 1; } else { - this->field6_0x18 = this->field6_0x18 + -1; + this->field_0x18 = this->field_0x18 + -1; } } } while (_tribeIndex_2 < this->tribes[tribeID].size); @@ -561,7 +561,7 @@ namespace Map { if (DAT_UnitsState::instance.units[_targetUnitID].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[_targetUnitID].dying == 0 && DAT_UnitsState::instance.units[_targetUnitID].usingTeleport == 0 - && DAT_UnitsState::instance.units[_targetUnitID].field303_0x413 == 0 + && DAT_UnitsState::instance.units[_targetUnitID].field_0x413 == 0 && DAT_UnitsState::instance.units[_targetUnitID].state.generic != (UnitStateShort)0xcf && DAT_UnitsState::instance.units[_targetUnitID].rallyRelatedFlag == 0 && DAT_UnitsState::instance.units[_targetUnitID].moveRelatedFlag != 1 @@ -595,14 +595,14 @@ namespace Map { short sVar10 = (short)DAT_UnitSelectionDefinedData::instance.UnitInstructionMoveDelay[sVar3]; if (this->tribes[tribeID].unkIsAnimalTribe == 0) { - if (-1 < this->field6_0x18) { + if (-1 < this->field_0x18) { sVar10 = sVar10 * 2; } } else { sVar10 = sVar10 * 3; } DAT_UnitsState::instance.units[_targetUnitID].moveInstructionSpeedDelayTracker = sVar10; - if (this->field6_0x18 < 0) { + if (this->field_0x18 < 0) { DAT_UnitsState::instance.units[_targetUnitID].moveDelay = 0; } else { DAT_UnitsState::instance.units[_targetUnitID].moveDelay diff --git a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp index 65a9e166ed..111af323a3 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribePathToDestinationClear.cpp @@ -17,8 +17,7 @@ namespace Map { // re-read every iteration (0x524D80). All failure exits share the return block at 0x524CD7 except the // occupied-tileUnk exit (0x524E00). // FUNCTION: STRONGHOLDCRUSADER 0x00524CA0 - undefined4 TribesState::isTribePathToDestinationClear( - int tribeID, uint fromX, uint fromY, uint destX, uint destY) + BOOLEnum TribesState::isTribePathToDestinationClear(int tribeID, uint fromX, uint fromY, uint destX, uint destY) { int unitSelectionIndex = 0; if (fromX > 399 || fromY > 399 diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp index fff7f98d02..18f5c9bff6 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp @@ -15,7 +15,7 @@ namespace Map { using Map::Units::States::UnitState; // FUNCTION: STRONGHOLDCRUSADER 0x00524B30 - undefined4 TribesState::isTribeUnitBlockedByOtherUnit(int param_1) + BOOLEnum TribesState::isTribeUnitBlockedByOtherUnit(int param_1) { int iVar2; /* @@ -32,7 +32,7 @@ namespace Map { while (true) { do { if (unitSelectionIndex >= sVar1) { - return (undefined4)(0); + return FALSE; } iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( param_1, unitSelectionIndex); @@ -40,26 +40,26 @@ namespace Map { } while ((DAT_UnitsState::instance.units[iVar2].logicalState != Map::Units::ULS_NORMAL) || (DAT_UnitsState::instance.units[iVar2].dying != 0)); if (DAT_UnitsState::instance.units[iVar2].usingTeleport != 0) { - return (undefined4)(0); + return FALSE; } - if (DAT_UnitsState::instance.units[iVar2].field303_0x413 != 0) { - return (undefined4)(0); + if (DAT_UnitsState::instance.units[iVar2].field_0x413 != 0) { + return FALSE; } if (DAT_UnitsState::instance.units[iVar2].tunnelerFinishedDigging == 2) break; if (DAT_UnitsState::instance.units[iVar2].state.generic == Map::Units::States::US_MELEE_ATTACK) { - return (undefined4)(0); + return FALSE; } if (DAT_UnitsState::instance.units[iVar2].laddermanIsInPosition != '\0') { - return (undefined4)(0); + return FALSE; } int iVar3 = (int)(short)DAT_TileMapState::instance.UnitLayer[DAT_UnitsState::instance.units[iVar2].tile]; if (iVar3 && iVar2 != iVar3 && DAT_UnitsState::instance.units[iVar3].tunnelerFinishedDigging != 2) { - return (undefined4)(1); + return TRUE; } } - return (undefined4)(0); + return FALSE; } } diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index c34026ab17..5e056992da 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -48,7 +48,7 @@ namespace Map { } else if (39 < iVar5) { return iVar2 / 40; } - piVar6 = &DAT_UnitSelectionDefinedData::instance.field1608_0xcc4[iVar5].y; + piVar6 = &DAT_UnitSelectionDefinedData::instance.field_0xcc4[iVar5].y; do { uint const x1 = piVar6[-1] + uVar7; uint const y1 = *piVar6 + uVar4; diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index 8e2560635d..7ce994bab6 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -29,7 +29,7 @@ namespace Map { eight arms gives 0.2951 at 149, and this form gives 0.4092 at 147. */ // FUNCTION: STRONGHOLDCRUSADER 0x00521280 - undefined4 TribesState::moveUnitToBehaviorTarget(int unitID, int param_2) + BOOLEnum TribesState::moveUnitToBehaviorTarget(int unitID, int param_2) { int iVar4; int sVar1 = DAT_UnitsState::instance.units[unitID].tribeID; @@ -38,20 +38,20 @@ namespace Map { uint uVar6 = 0; uint local_c = 0; if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL) { - return (undefined4)(0); + return FALSE; } if (DAT_UnitsState::instance.units[unitID].dying != 0) { - return (undefined4)(0); + return FALSE; } if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0) { - return (undefined4)(0); + return FALSE; } - if (DAT_UnitsState::instance.units[unitID].field303_0x413 != 0) { - return (undefined4)(0); + if (DAT_UnitsState::instance.units[unitID].field_0x413 != 0) { + return FALSE; } if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[DAT_UnitsState::instance.units[unitID].owner] != -1) { - return (undefined4)(0); + return FALSE; } if (param_2 == 0x3f2) { iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( @@ -129,7 +129,7 @@ namespace Map { LAB_0052143b: DAT_UnitsState::instance.units[unitID].targetedBuildingTile = 0; if (!bVar3) { - return (undefined4)(0); + return FALSE; } short sVar5 = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[uVar6]; short sVar2 = DAT_UnitsState::instance.units[unitID].selectionTargetUnitID; @@ -152,7 +152,7 @@ namespace Map { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( unitID, x, local_c, 0); } - return (undefined4)(1); + return TRUE; } } diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index 0397e63ce9..a8f73e6e32 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -154,11 +154,11 @@ namespace Map { _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( _otherX, _otherY, 0xea); - /* - `<= 4` and `>= 4` below, which are the `cmp .., 4` the original compares - against with a `jg` and a `jl`; `< 5` and `3 <` give `cmp 5`/`cmp 3` and - the other arm of each pair. - */ + /* + `<= 4` and `>= 4` below, which are the `cmp .., 4` the original compares + against with a `jg` and a `jl`; `< 5` and `3 <` give `cmp 5`/`cmp 3` and + the other arm of each pair. + */ } else if (_selectedCount <= 4) { _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp index 82474fe35e..131a455018 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp @@ -27,8 +27,8 @@ namespace Map { { int _unitID; UnitType _unitTypeID; - _unitID = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + _unitID + = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); /* bug: the second argument ("stack") does not want to be renamed, but is sfxToPlay again. */ @@ -44,8 +44,7 @@ namespace Map { /* Any other ranged unit */ - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - _unitTypeID, 0x1d); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x1d); return; case Map::Units::UT_S_CATAPULT: /* @@ -59,8 +58,7 @@ namespace Map { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( Audio::SFX::SEID_RESOURCE_NEED25); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - _unitTypeID, 0x10); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x10); } break; case Map::Units::UT_S_TREBUCHET: @@ -75,8 +73,7 @@ namespace Map { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( Audio::SFX::SEID_RESOURCE_NEED25); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - _unitTypeID, 0x11); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x11); } break; case Map::Units::UT_S_MANGONEL: @@ -86,8 +83,7 @@ namespace Map { if (DAT_UnitsState::instance.units[_unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - _unitTypeID, 0xf); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0xf); } break; case Map::Units::UT_S_BATTERINGRAM: @@ -97,8 +93,7 @@ namespace Map { if (DAT_UnitsState::instance.units[_unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - _unitTypeID, 0x13); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x13); } break; case Map::Units::UT_S_BALLISTA: @@ -109,8 +104,7 @@ namespace Map { if (DAT_UnitsState::instance.units[_unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - _unitTypeID, 0x12); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x12); } } } diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index bd54de5e8e..57902e6ee6 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -59,22 +59,21 @@ namespace Map { */ if (param_1 == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0xad); + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + 0xad); return; } if (param_1 == 2) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0xae); + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + 0xae); return; } if (param_1 < 3) { return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0xaf); + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0xaf); return; case Map::Units::UT_E_LADDER: { /* @@ -92,8 +91,8 @@ namespace Map { */ if (param_1 == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0x56); + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + 0x56); } else { if (param_1 == 2 || param_1 == 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( @@ -134,12 +133,11 @@ namespace Map { return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0x58); + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + 0x58); } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); return; } case Map::Units::UT_A_HARCHER: { @@ -170,8 +168,8 @@ namespace Map { Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(iVar3); if (_requiredEngineers <= 0) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0x68); + (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), + 0x68); return; } if (DAT_UnitsState::instance.units[iVar3] diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index c83337b36c..4a021ce9ed 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -73,9 +73,11 @@ namespace Map { } else { direction &= 0xf; } - *unitCurrentX = DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.xOffset + *unitCurrentX + = DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.xOffset + *unitCurrentX; - *unitCurrentY += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.yOffset; + *unitCurrentY + += DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction].int_.yOffset; distanceX = abs(*unitCurrentX - targetX); distance = abs(*unitCurrentY - targetY); if (distanceX > distance) { diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index 3786db409d..778d8cdb89 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -50,7 +50,7 @@ namespace Map { iVar8 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; int _predatorCount = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount; int iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; - _predatorCount = _predatorCount + DAT_WildlifeState::instance.grid[_x10][_y10].field18_0x48; + _predatorCount = _predatorCount + DAT_WildlifeState::instance.grid[_x10][_y10].field_0x48; /* A `short`, compared as one: `movzx ecx, word ptr [esi + 0x5c]` then `cmp cx, 0x1e`, with the sign extension deferred to the halving. diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index ec071fab1c..f539308f12 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -20,7 +20,7 @@ namespace Map { */ if (DAT_UnitsState::instance.units[unitID].tribeID != 0 || ((int)this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] - & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) + & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) != 0) { /* The base is NOT taken once here, although the original does take it diff --git a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp index 54b803b535..82e52a587c 100644 --- a/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp +++ b/src/OpenSHC/Map/Units/TribesState/sortTribePathDestinationsByCost.cpp @@ -20,7 +20,8 @@ namespace Map { int const x = DAT_UnitsState::instance.units[selectionTargetUnitID].x; if (horseAndRamCount != 0) { MACRO_CALL_MEMBER(Navigation::PathFindingState_Func::calculatePathKeepAndWallsGatesNotAllowed, - DAT_PathFindingState::ptr)(x, DAT_UnitsState::instance.units[selectionTargetUnitID].y, -1, -1, 100000); + DAT_PathFindingState::ptr)( + x, DAT_UnitsState::instance.units[selectionTargetUnitID].y, -1, -1, 100000); } else { int const y = DAT_UnitsState::instance.units[selectionTargetUnitID].y; if (allAssassins != FALSE) { @@ -33,8 +34,7 @@ namespace Map { } for (int i = 0; DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile1 != 0; ++i) { int const tileUnk = DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile1; - if (DAT_TileMapState::instance.WalkLayer[tileUnk] - == DAT_PathFindingState::instance.searchGeneration) { + if (DAT_TileMapState::instance.WalkLayer[tileUnk] == DAT_PathFindingState::instance.searchGeneration) { DAT_PathFindingState::instance.searchQueue.destinationsArray[i].tile3OrAnotherHelper = DAT_TileMapState::instance.CertainPathLayer[tileUnk]; } else { @@ -65,10 +65,11 @@ namespace Map { if (nextTile2 == 0) { break; } - int const cost - = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile3OrAnotherHelper; + int const cost = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx] + .tile3OrAnotherHelper; int const nextCost - = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1].tile3OrAnotherHelper; + = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1] + .tile3OrAnotherHelper; if (cost > nextCost) { DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1].tile1 = DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile1; @@ -79,10 +80,10 @@ namespace Map { = nextTile2; DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1] .tile2OrAHelper = tile2; - DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile3OrAnotherHelper - = nextCost; - DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1].tile3OrAnotherHelper - = cost; + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx] + .tile3OrAnotherHelper = nextCost; + DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx + 1] + .tile3OrAnotherHelper = cost; } ++tribeIDThenSortIdx; } while (DAT_PathFindingState::instance.searchQueue.destinationsArray[tribeIDThenSortIdx].tile1 != 0); diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index 7da72631af..fe7b8be6ba 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -49,8 +49,7 @@ namespace Map { _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; _finalTargetUnitID = (int)_targetUnitID; if (unitType == Map::Units::UT_ANTELOPESHDEER) { - _finalTargetUnitID - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); + _finalTargetUnitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); if (!_finalTargetUnitID) { _finalTargetUnitID = (int)_targetUnitID; } @@ -82,10 +81,10 @@ namespace Map { instruction here (quick_diff 0.7295 -> 0.7265), because the field form re-evaluates tribes[tribeID] while the pointer freezes the address. */ - psVar1 = &this->tribes[tribeID].field138_0x284; + psVar1 = &this->tribes[tribeID].field_0x284; *psVar1 = *psVar1 + 1; - if ((this->tribes[tribeID].field138_0x284 >= param_2) - && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field138_0x284 = 0, + if ((this->tribes[tribeID].field_0x284 >= param_2) + && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field_0x284 = 0, _tribeSize < sVar2)) { if ((unitType == Map::Units::UT_RABBIT) && (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation)) { diff --git a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp index 47cb911988..0217e94fc9 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnQueuedReinforcementWaves.cpp @@ -1,8 +1,11 @@ #include "../../../Map.func.hpp" #include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/AI/Siege/SiegeUnitCounts.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" #include "OpenSHC/Game/GameMode.hpp" #include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" #include "OpenSHC/Game/TrailType.hpp" #include "OpenSHC/Map/Units/UnitType.hpp" #include "OpenSHC/Map/Units/UnitTypeInt.hpp" @@ -18,6 +21,8 @@ namespace OpenSHC { namespace Map { namespace Units { + using AI::Siege::SiegeUnitCounts; + using AI::Tribes::AITribeType; using Game::GameMode; using Game::GameMode2; using Game::TrailType; @@ -43,7 +48,7 @@ namespace Map { SiegeUnitCounts* pSVar8; int _siegeInfoSubIndex; int _playerID; - int _tribeType; + AITribeType _tribeType; UnitTypeInt _unitType; int _destinationIndexTracker; int _unitType1Count; @@ -52,7 +57,7 @@ namespace Map { int _count; int _destinationIndex; int _newCount; - _tribeType = 0; + _tribeType = (AITribeType)0; bool bVar1 = false; _unitType = ((UnitType)0); @@ -65,7 +70,8 @@ namespace Map { || (DAT_GameCore::instance.gameMode_2 == Game::GM_CRUSADER_TUTORIAL)) || (0 < DAT_GameState::instance .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] - .keep.id)))) + .buildingEntryInfos[Game::Player::Enums::BEIE_KEEP] + .id)))) && 0 <= DAT_GameState::instance.mapAndTime.countUpTo201) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateTribeUnitAssignments, this)(); _playerID_2 = DAT_GameSynchronyState::instance.currentPlayerSlotID; @@ -112,31 +118,31 @@ namespace Map { if (DAT_GameState::instance.mapAndTime.startGoods[_siegeInfoSubIndex + 0x19] != 0) { switch (_siegeInfoSubIndex) { case 0: - _tribeType = 3; + _tribeType = AI::Tribes::AITT_ARCHERS; _unitType = Map::Units::UT_E_ARCHER; break; case 1: - _tribeType = 7; + _tribeType = AI::Tribes::AITT_CROSSBOWMEN; _unitType = Map::Units::UT_E_XBOW; break; case 2: - _tribeType = 5; + _tribeType = AI::Tribes::AITT_SPEARMEN; _unitType = Map::Units::UT_E_SPEAR; break; case 3: - _tribeType = 6; + _tribeType = AI::Tribes::AITT_PIKEMEN; _unitType = Map::Units::UT_E_PIKE; break; case 4: - _tribeType = 9; + _tribeType = AI::Tribes::AITT_MACEMEN; _unitType = Map::Units::UT_E_MACE; break; case 5: - _tribeType = 8; + _tribeType = AI::Tribes::AITT_SWORDSMEN; _unitType = Map::Units::UT_E_SWORD; break; case 6: - _tribeType = 10; + _tribeType = AI::Tribes::AITT_KNIGHTS; _unitType = Map::Units::UT_E_KNIGHT; break; /* @@ -151,47 +157,47 @@ namespace Map { way costs 17.1 points (59.6% -> 42.5%). */ case 7: - _tribeType = 4; + _tribeType = AI::Tribes::AITT_LADDERMEN; _unitType = Map::Units::UT_E_LADDER; break; case 8: - _tribeType = 0xb; + _tribeType = AI::Tribes::AITT_ENGINEERS; _unitType = Map::Units::UT_E_ENGINEER; break; case 9: - _tribeType = 0xc; + _tribeType = AI::Tribes::AITT_HOPS_FARMER_UNK; _unitType = Map::Units::UT_E_MONK; break; case 10: - _tribeType = 0x19; + _tribeType = AI::Tribes::AITT_ARAB_ARCHER; _unitType = Map::Units::UT_A_ARCHER; break; case 0xb: - _tribeType = 0x1a; + _tribeType = AI::Tribes::AITT_ARAB_SLAVE; _unitType = Map::Units::UT_A_SLAVE; break; case 0xc: - _tribeType = 0x1b; + _tribeType = AI::Tribes::AITT_ARAB_SLINGER; _unitType = Map::Units::UT_A_SLINGER; break; case 0xd: - _tribeType = 0x1c; + _tribeType = AI::Tribes::AITT_ARAB_ASSASSIN; _unitType = Map::Units::UT_A_ASSASSIN; break; case 0xe: - _tribeType = 0x1d; + _tribeType = AI::Tribes::AITT_ARAB_HORSE_ARCHER; _unitType = Map::Units::UT_A_HARCHER; break; case 0xf: - _tribeType = 0x1e; + _tribeType = AI::Tribes::AITT_ARAB_SWORDSMAN; _unitType = Map::Units::UT_A_SWORDSMAN; break; case 0x10: - _tribeType = 0x1f; + _tribeType = AI::Tribes::AITT_ARAB_FIRETHROWER; _unitType = Map::Units::UT_A_FIRETHROWER; break; case 0x11: - _tribeType = 0x18; + _tribeType = AI::Tribes::AITT_FIRE_BALLISTA; _unitType = Map::Units::UT_S_FBALLISTA; } do { @@ -207,7 +213,7 @@ namespace Map { _destinationIndex = 0; } _tribeID1 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeWithSpawnedUnit, - this)((short)_destinationIndexTracker, (undefined4)((int)(_tribeType)), + this)((short)_destinationIndexTracker, _tribeType, DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][_destinationIndex] .x, DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][_destinationIndex] @@ -260,7 +266,7 @@ namespace Map { iVar5 = 0; } dVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeWithSpawnedUnit, this)( - (short)_destinationIndexTracker, 2, + (short)_destinationIndexTracker, AI::Tribes::AITT_TUNNELERS, DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][iVar5].x, DAT_GameState::instance.mapAndTime.unitMoveDestinationXYPairs[0][iVar5].y, 2, Map::Units::UT_TUNNELER, iVar6); @@ -287,39 +293,39 @@ namespace Map { } switch (iVar3) { case 0: - _tribeType = 3; + _tribeType = AI::Tribes::AITT_ARCHERS; _unitType = Map::Units::UT_E_ARCHER; break; case 1: - _tribeType = 7; + _tribeType = AI::Tribes::AITT_CROSSBOWMEN; _unitType = Map::Units::UT_E_XBOW; break; case 2: - _tribeType = 5; + _tribeType = AI::Tribes::AITT_SPEARMEN; _unitType = Map::Units::UT_E_SPEAR; break; case 3: - _tribeType = 6; + _tribeType = AI::Tribes::AITT_PIKEMEN; _unitType = Map::Units::UT_E_PIKE; break; case 4: - _tribeType = 9; + _tribeType = AI::Tribes::AITT_MACEMEN; _unitType = Map::Units::UT_E_MACE; break; case 5: - _tribeType = 8; + _tribeType = AI::Tribes::AITT_SWORDSMEN; _unitType = Map::Units::UT_E_SWORD; break; case 6: - _tribeType = 10; + _tribeType = AI::Tribes::AITT_KNIGHTS; _unitType = Map::Units::UT_E_KNIGHT; break; case 8: - _tribeType = 0xb; + _tribeType = AI::Tribes::AITT_ENGINEERS; _unitType = Map::Units::UT_E_ENGINEER; break; case 9: - _tribeType = 0xc; + _tribeType = AI::Tribes::AITT_HOPS_FARMER_UNK; _unitType = Map::Units::UT_E_MONK; } iVar6 = DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19]; @@ -329,7 +335,9 @@ namespace Map { } _playerID = 1; DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] = iVar6 - _unitType1Count; - piVar7 = &DAT_GameState::instance.playerDataArray[1].campground.xEntry; + piVar7 = &DAT_GameState::instance.playerDataArray[1] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry; do { if ((DAT_GameSynchronyState::instance.currentPlayerFullIDArray[_playerID] != -1) || (DAT_GameSynchronyState::instance.currentAIArray[_playerID] != 0)) { @@ -372,35 +380,35 @@ namespace Map { } switch (iVar3) { case 10: - _tribeType = 0x19; + _tribeType = AI::Tribes::AITT_ARAB_ARCHER; _unitType = Map::Units::UT_A_ARCHER; break; case 0xb: - _tribeType = 0x1a; + _tribeType = AI::Tribes::AITT_ARAB_SLAVE; _unitType = Map::Units::UT_A_SLAVE; break; case 0xc: - _tribeType = 0x1b; + _tribeType = AI::Tribes::AITT_ARAB_SLINGER; _unitType = Map::Units::UT_A_SLINGER; break; case 0xd: - _tribeType = 0x1c; + _tribeType = AI::Tribes::AITT_ARAB_ASSASSIN; _unitType = Map::Units::UT_A_ASSASSIN; break; case 0xe: - _tribeType = 0x1d; + _tribeType = AI::Tribes::AITT_ARAB_HORSE_ARCHER; _unitType = Map::Units::UT_A_HARCHER; break; case 0xf: - _tribeType = 0x1e; + _tribeType = AI::Tribes::AITT_ARAB_SWORDSMAN; _unitType = Map::Units::UT_A_SWORDSMAN; break; case 0x10: - _tribeType = 0x1f; + _tribeType = AI::Tribes::AITT_ARAB_FIRETHROWER; _unitType = Map::Units::UT_A_FIRETHROWER; break; case 0x11: - _tribeType = 0x18; + _tribeType = AI::Tribes::AITT_FIRE_BALLISTA; _unitType = Map::Units::UT_S_FBALLISTA; } iVar6 = DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19]; @@ -410,7 +418,9 @@ namespace Map { } iVar5 = 1; DAT_GameState::instance.mapAndTime.startGoods[iVar3 + 0x19] = iVar6 - _unitType1Count; - piVar7 = &DAT_GameState::instance.playerDataArray[1].campground.xEntry; + piVar7 = &DAT_GameState::instance.playerDataArray[1] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry; do { if ((DAT_GameSynchronyState::instance.currentPlayerFullIDArray[iVar5] != -1) || (DAT_GameSynchronyState::instance.currentAIArray[iVar5] != 0)) { @@ -443,7 +453,9 @@ namespace Map { DAT_GameState::instance.mapAndTime.countUpTo201 = 0; local_4 = 1; _unitType1Count = 0x14; - piVar7 = &DAT_GameState::instance.playerDataArray[1].campground.xEntry; + piVar7 = &DAT_GameState::instance.playerDataArray[1] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry; do { if ((DAT_GameSynchronyState::instance.currentPlayerFullIDArray[local_4] != -1) || (DAT_GameSynchronyState::instance.currentAIArray[local_4] != 0)) { @@ -457,31 +469,31 @@ namespace Map { != 0) { switch (iVar3) { case 0: - _tribeType = 3; + _tribeType = AI::Tribes::AITT_ARCHERS; _unitType = Map::Units::UT_E_ARCHER; break; case 1: - _tribeType = 7; + _tribeType = AI::Tribes::AITT_CROSSBOWMEN; _unitType = Map::Units::UT_E_XBOW; break; case 2: - _tribeType = 5; + _tribeType = AI::Tribes::AITT_SPEARMEN; _unitType = Map::Units::UT_E_SPEAR; break; case 3: - _tribeType = 6; + _tribeType = AI::Tribes::AITT_PIKEMEN; _unitType = Map::Units::UT_E_PIKE; break; case 4: - _tribeType = 9; + _tribeType = AI::Tribes::AITT_MACEMEN; _unitType = Map::Units::UT_E_MACE; break; case 5: - _tribeType = 8; + _tribeType = AI::Tribes::AITT_SWORDSMEN; _unitType = Map::Units::UT_E_SWORD; break; case 6: - _tribeType = 10; + _tribeType = AI::Tribes::AITT_KNIGHTS; _unitType = Map::Units::UT_E_KNIGHT; break; /* @@ -491,43 +503,43 @@ namespace Map { iteration left in the two variables. */ case 8: - _tribeType = 0xb; + _tribeType = AI::Tribes::AITT_ENGINEERS; _unitType = Map::Units::UT_E_ENGINEER; break; case 9: - _tribeType = 0xc; + _tribeType = AI::Tribes::AITT_HOPS_FARMER_UNK; _unitType = Map::Units::UT_E_MONK; break; case 10: - _tribeType = 0x19; + _tribeType = AI::Tribes::AITT_ARAB_ARCHER; _unitType = Map::Units::UT_A_ARCHER; break; case 0xb: - _tribeType = 0x1a; + _tribeType = AI::Tribes::AITT_ARAB_SLAVE; _unitType = Map::Units::UT_A_SLAVE; break; case 0xc: - _tribeType = 0x1b; + _tribeType = AI::Tribes::AITT_ARAB_SLINGER; _unitType = Map::Units::UT_A_SLINGER; break; case 0xd: - _tribeType = 0x1c; + _tribeType = AI::Tribes::AITT_ARAB_ASSASSIN; _unitType = Map::Units::UT_A_ASSASSIN; break; case 0xe: - _tribeType = 0x1d; + _tribeType = AI::Tribes::AITT_ARAB_HORSE_ARCHER; _unitType = Map::Units::UT_A_HARCHER; break; case 0xf: - _tribeType = 0x1e; + _tribeType = AI::Tribes::AITT_ARAB_SWORDSMAN; _unitType = Map::Units::UT_A_SWORDSMAN; break; case 0x10: - _tribeType = 0x1f; + _tribeType = AI::Tribes::AITT_ARAB_FIRETHROWER; _unitType = Map::Units::UT_A_FIRETHROWER; break; case 0x11: - _tribeType = 0x18; + _tribeType = AI::Tribes::AITT_FIRE_BALLISTA; _unitType = Map::Units::UT_S_FBALLISTA; } iVar6 = *(int*)(DAT_GameState::instance.mapAndTime.unused_0x3ff00 diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 808ba86d98..8536d034c4 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -39,11 +39,9 @@ namespace Map { fits that description better than this function does and lost forty points, so the rule is wrong. Measure each function on its own. */ - TribesState* const _tb - = (TribesState*)((char*)this + DAT_CurrentTribeID::instance * sizeof(Tribe)); + TribesState* const _tb = (TribesState*)((char*)this + DAT_CurrentTribeID::instance * sizeof(Tribe)); if (this->tribes[DAT_CurrentTribeID::instance].tribeState != 0 - && (iVar3 = (int)_tb->tribes[0].size, iVar3 >= smallerSize) - && 0 < iVar3) { + && (iVar3 = (int)_tb->tribes[0].size, iVar3 >= smallerSize) && 0 < iVar3) { do { iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( _currentTribeID, unitSelectionIndex); diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 406ada4456..007f3cf26d 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -160,8 +160,7 @@ namespace Map { extra loads cost here - unlike updateCamelTribeBehavior, where the same change to a threshold local is worth 3.2. */ - if (_tb->tribes[0].wanderIntervalTicks - <= _tb->tribes[0].unknownAttackRelatedUpdateCounter) { + if (_tb->tribes[0].wanderIntervalTicks <= _tb->tribes[0].unknownAttackRelatedUpdateCounter) { _tb->tribes[0].tribeBehaviorType = ((SomeTribeBehaviorType)0); _tb->tribes[0].unknownAttackRelatedUpdateCounter = 0; } diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp index be54debdc0..17d99f7cde 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp @@ -64,9 +64,9 @@ namespace Map { .translationMatrix[DAT_UnitsState::instance.units[iVar5].destinationY_2Unk] .addXgetTile == this->tribes[tribeID].someTile2) { - _buildingIndex_2 = this->tribes[tribeID].field166_0x2be; + _buildingIndex_2 = this->tribes[tribeID].field_0x2be; if (_buildingIndex_2) { - this->tribes[tribeID].field166_0x2be = _buildingIndex_2 + -1; + this->tribes[tribeID].field_0x2be = _buildingIndex_2 + -1; goto LAB_0052aa77; } if (DAT_UnitsState::instance.units[iVar5].tile == this->tribes[tribeID].someTile) @@ -219,8 +219,8 @@ namespace Map { if (0 < _buildingIndex_2) { iVar5 = 0; do { - iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, iVar5); + iVar4 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar5); iVar5 = iVar5 + 1; if (DAT_UnitsState::instance.units[iVar4].logicalState == Map::Units::ULS_NORMAL && DAT_UnitsState::instance.units[iVar4].dying == 0 @@ -244,7 +244,8 @@ namespace Map { } } while (iVar5 < this->tribes[tribeID].size); } - MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::aggressiveStanceTargetBuildingAtRange, + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::aggressiveStanceTargetBuildingAtRange, DAT_PathFindingState::ptr)(_targetUnitID, (int)(15)); if (DAT_PathFindingState::instance.ALG_ResultTile) { _buildingIndex_2 = DAT_TileMapState::instance diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp index 0ebda4a472..37e1a608d6 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeUnitAssignments.cpp @@ -18,7 +18,7 @@ namespace Map { { uint _unitID; this->clans = 0; - this->field2_0x8 = 1; + this->field_0x8 = 1; /* A `for`, not the original's do-while, because the two `continue`s have to run the increment: the label they replace sat in front of it. The first test is @@ -82,7 +82,7 @@ namespace Map { } while (_tribeUnitIndex < _tribeSize); if (local_4) { this->clans = this->clans + 1; - this->field2_0x8 = DAT_CurrentTribeID::instance; + this->field_0x8 = DAT_CurrentTribeID::instance; continue; } } diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 9391993e87..8e2bd9625c 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -1,12 +1,14 @@ #include "../TribesState.func.hpp" +#include "OpenSHC/OS.func.hpp" + namespace OpenSHC { namespace Map { namespace Units { // Old record layout: 0x26C bytes per tribe (100-word member bitmask), tail of 0x16C bytes at old offset 0x100. - // The original calls the CRT memmove at 0x583760 twice (the first one guarded by dst != src, 0x523F16); there - // is no resolver for it, so the DLL's own CRT memmove is called (same semantics). The new second half of the + // The original calls the CRT memmove at 0x583760 twice (the first one guarded by dst != src, 0x523F16), + // through the OS_Func::_memmove resolver. The new second half of the // bitmask is zeroed with rep stosd (0x32 dwords, 0x523F62): MSVC 2005 emits that for this loop but a call for // memset(p, 0, 200) (tested). receivedMapVersion is compared signed (jge at 0x523EFF). Tribes 0x4E1 down to 0. // FUNCTION: STRONGHOLDCRUSADER 0x00523EF0 @@ -19,10 +21,10 @@ namespace Map { Tribe* dst = &this->tribes[1249]; do { if ((void*)dst != (void*)src) { - memmove(dst, src, 0x26c); + MACRO_CALL(OpenSHC::OS_Func::_memmove)(dst, src, 0x26c); } short* maskTail = &dst->unitSelectionBitMasked[100]; - memmove(&dst->field34_0x1c8, src + 0x100, 0x16c); + MACRO_CALL(OpenSHC::OS_Func::_memmove)(&dst->field_0x1c8, src + 0x100, 0x16c); --tribeIndex; src -= 0x26c; --dst; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 95040b75a5..81e28bd6e9 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46389,7 +46389,7 @@ SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521280 | 33.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521500 | 40.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521500 | 84.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00521690 | 0.0% | Pending @@ -46815,7 +46815,7 @@ SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522CA0 | 95.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522CA0 | 96.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented @@ -46871,7 +46871,7 @@ SHC_3BB0A8C1_0x00523E30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523EF0 | 62.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523EF0 | 72.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00523F70 | 100.0% | Reimplemented @@ -46895,7 +46895,7 @@ SHC_3BB0A8C1_0x005244D0 | 44.6% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524890 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524930 | 56.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524930 | 58.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x005249C0 | 0.0% | Pending @@ -46907,13 +46907,13 @@ SHC_3BB0A8C1_0x00524B30 | 80.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524BF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524CA0 | 18.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524CA0 | 66.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending SHC_3BB0A8C1_0x00524E20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524EF0 | 48.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524EF0 | 71.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525060 | 0.0% | Pending @@ -46973,7 +46973,7 @@ SHC_3BB0A8C1_0x005270F0 | 47.2% | Reimplemented, asm differs SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527440 | 60.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527440 | 60.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x005275A0 | 0.0% | Pending @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 60.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 60.8% | Reimplemented, asm differs SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending From a86b3aaec8f8f21db1b8d04e852b99fa9dca3ec2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:25:18 +0100 Subject: [PATCH 154/202] reimplement: createTribe 79.0 -> 100.0 The search loop over DAT_CurrentTribeID clears the free slot and breaks out; the claim is guarded by a second tribeID <= 0 test after it, which the original keeps after the clear and jump-threads into the loop's exhausted exit. Replaces the shifted-TribesState-pointer form. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/createTribe.cpp | 78 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index 0b2c21d66c..e944d4bec4 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -17,60 +17,42 @@ namespace Map { using AI::Tribes::AITribeType; using Map::Units::SomeTribeBehaviorType; + /* + The same shape as createTribeForPlayer: the search loop (over the global + DAT_CurrentTribeID) clears the free slot itself and breaks out, and the claim is + guarded by a second `tribeID <= 0` test that the original keeps after the clear. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00522890 int TribesState::createTribe(int playerID, int setAsCurrentTribeID) { - DAT_CurrentTribeID::instance = 1; - do { + int tribeID = 0; + for (DAT_CurrentTribeID::instance = 1; (int)DAT_CurrentTribeID::instance < 1250; + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1) { if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0) { - /* - The index is only copied out of the loop counter once the slot is - known free (`mov edi, eax` at 0x5228c9), and the tribe's address is - taken before the clear (`lea esi, [edx + ebp]`, `lea eax, [esi + - 0x28]`), then reused for every store below it. - */ - int const _tribeID = DAT_CurrentTribeID::instance; - /* - A shifted TribesState pointer, not a `Tribe&`. The original computes - the base without the `tribes` offset (`lea esi, [edx + ebp]`) and then - takes `lea eax, [esi + 0x28]` separately for the clear's argument, - reaching the fields as `[esi + 0x2c]` and so on. A reference folds the - 0x28 into one lea and shifts every field displacement by it. - */ - TribesState* const _tb = (TribesState*)((char*)this + _tribeID * sizeof(Tribe)); + tribeID = DAT_CurrentTribeID::instance; MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - sizeof(Tribe), '\0', &_tb->tribes[0]); - /* - An early return, which is where the original puts its `return 0`: - `cmp edi, ebx; jg` falls straight into `xor eax, eax` with the claim - following, rather than jumping over the claim to a shared epilogue. - */ - if (_tribeID <= 0) { - return 0; - } - { - if ((playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID) - && (setAsCurrentTribeID)) { - this->DAT_CurrentTribeID = _tribeID; - } - _tb->tribes[0].owner = playerID; - _tb->tribes[0].tribeState = 2; - _tb->tribes[0].uid = DAT_GameCore::instance.uniqueGameObjectTracker; - DAT_GameCore::instance.uniqueGameObjectTracker - = DAT_GameCore::instance.uniqueGameObjectTracker + 1; - _tb->tribes[0].tribeType = ((AITribeType)0); - _tb->tribes[0].field_0x1c = 0; - _tb->tribes[0].field_0x1e = 0; - _tb->tribes[0].tribeBehaviorType = ((SomeTribeBehaviorType)0); - _tb->tribes[0].field_0x30 = 0; - _tb->tribes[0].selectionTargetUnitID = 0; - _tb->tribes[0].size = 0; - return _tribeID; - } + sizeof(Tribe), '\0', &this->tribes[tribeID]); + break; } - DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; - } while ((int)DAT_CurrentTribeID::instance < 1250); - return 0; + } + if (tribeID <= 0) { + return 0; + } + if (playerID == DAT_GameSynchronyState::instance.currentPlayerSlotID && setAsCurrentTribeID) { + this->DAT_CurrentTribeID = tribeID; + } + this->tribes[tribeID].owner = playerID; + this->tribes[tribeID].tribeState = 2; + this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; + DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; + this->tribes[tribeID].tribeType = ((AITribeType)0); + this->tribes[tribeID].field_0x1c = 0; + this->tribes[tribeID].field_0x1e = 0; + this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[tribeID].field_0x30 = 0; + this->tribes[tribeID].selectionTargetUnitID = 0; + this->tribes[tribeID].size = 0; + return tribeID; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 81e28bd6e9..f1790ca75b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46799,7 +46799,7 @@ SHC_3BB0A8C1_0x00522720 | 62.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x005227E0 | 86.2% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522890 | 79.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522890 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented From e7416a61d157a34a752457e062a9c277d7c820bb Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:25:19 +0100 Subject: [PATCH 155/202] reimplement: createTribeForPlayer 86.2 -> 100.0 Same shape as createTribe: a for loop that clears the slot and breaks, then the tribeID <= 0 guard. Replaces the goto-based search. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/createTribeForPlayer.cpp | 98 +++++-------------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 26 insertions(+), 74 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index f10511b914..70fa26d4ed 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -16,85 +16,37 @@ namespace Map { using Map::Units::SomeTribeBehaviorType; /* - The inner slot-ID guard stays a positive `if` around the claim: inverting it to an - early return, which took spawnUnitsIntoNewTribe from 89.4% to 98.3%, costs 2.0 - points here (75.2% -> 73.2%). - - The repeated subscript stays too. The original does take the tribe's address once - before the clear (`lea esi, [edx + ecx]`, `lea eax, [esi + 0x28]` at 0x522823) and - reach every store through it, and writing that as a `Tribe&` is what took the twin - createTribe from 30.3% to 67.2% - but here the identical change costs 37.3 points - (75.2% -> 37.9%). The two functions differ only in how they find the slot, so the - shape that matches one does not match the other. - - The `if (0 < slot)` around the stores also stays. The original returns zero - through an epilogue of its own right there - `cmp edi, ebx; jg 0x9` over - `pop/pop/pop/xor eax,eax/pop/ret 4` - where ours sends that return to the shared - epilogue and inverts the test to `jle`, so a flat guard looks like the obvious - fix. It measures 75.2% -> 73.2%. Flattening guard chains is as unpredictable as - caching the base: it took trySpawnAdditionalWildlifeForTribe to a full match - (+12.8) and gained +0.6 on predictUnitInterceptPosition, but costs 1.4 on - isTribePathToDestinationClear and 2.0 here. Measure it; do not assume it. + The search loop does the clear itself and breaks out, and the claim is guarded by + a second `tribeID <= 0` test after it: the original jump-threads that test into + the loop's exhausted exit (its own `return 0`) and into the entry test, but keeps + it after the clear, where the loop variable has gone through the call. */ // FUNCTION: STRONGHOLDCRUSADER 0x005227E0 int TribesState::createTribeForPlayer(int playerID) { - short* psVar1; - int _selectionID; - _selectionID = 1250 - playerID; - if (0 < _selectionID) { - /* - find an empty tribe - */ - /* - Top-tested, with the back-edge returning to the test, which is what the - original has at 0x522801: - - lea eax, [eax + ecx + 0x40] ; the cursor, taken once - L:cmp word ptr [eax], bx ; tested through it - je - sub edi, 8 - sub eax, 0x19a0 - cmp edi, ebx - jg L - - A `while (*psVar1 != 0)` gets rotated: MSVC peels the first test, and - does that one through two-register addressing (`cmp word ptr [eax + ecx - + 0x40], bx`) before taking the cursor at all, then bottom-tests the - rest. Writing the test at the top with an explicit jump back keeps the - cursor as the only way the field is read. - */ - psVar1 = &this->tribes[_selectionID].tribeState; - LAB_00522801: - if (*psVar1 != 0) { - _selectionID = _selectionID + -8; - psVar1 = psVar1 - 0xcd0; - if (0 < _selectionID) { - goto LAB_00522801; - } - return 0; - } - /* - wipe the tribe - */ - MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( - sizeof(Tribe), '\0', &this->tribes[_selectionID]); - if (0 < _selectionID) { - this->tribes[_selectionID].owner = playerID; - this->tribes[_selectionID].tribeState = 2; - this->tribes[_selectionID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; - DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; - this->tribes[_selectionID].tribeType = ((AITribeType)0); - this->tribes[_selectionID].field_0x1c = 0; - this->tribes[_selectionID].field_0x1e = 0; - this->tribes[_selectionID].tribeBehaviorType = ((SomeTribeBehaviorType)0); - this->tribes[_selectionID].field_0x30 = 0; - this->tribes[_selectionID].selectionTargetUnitID = 0; - this->tribes[_selectionID].size = 0; - return _selectionID; + int tribeID; + for (tribeID = 1250 - playerID; tribeID > 0; tribeID = tribeID - 8) { + if (this->tribes[tribeID].tribeState == 0) { + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(Tribe), '\0', &this->tribes[tribeID]); + break; } } - return 0; + if (tribeID <= 0) { + return 0; + } + this->tribes[tribeID].owner = playerID; + this->tribes[tribeID].tribeState = 2; + this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; + DAT_GameCore::instance.uniqueGameObjectTracker = DAT_GameCore::instance.uniqueGameObjectTracker + 1; + this->tribes[tribeID].tribeType = ((AITribeType)0); + this->tribes[tribeID].field_0x1c = 0; + this->tribes[tribeID].field_0x1e = 0; + this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); + this->tribes[tribeID].field_0x30 = 0; + this->tribes[tribeID].selectionTargetUnitID = 0; + this->tribes[tribeID].size = 0; + return tribeID; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index f1790ca75b..3fc0e0c4d8 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46797,7 +46797,7 @@ SHC_3BB0A8C1_0x00522630 | 79.5% | Reimplemented, asm differs SHC_3BB0A8C1_0x00522720 | 62.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005227E0 | 86.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005227E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522890 | 100.0% | Reimplemented From 83db82f99119ad9063f0accfefc40a00fe706e98 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:25:20 +0100 Subject: [PATCH 156/202] reimplement: getUnitIDForIndexInTribe 58.0 -> 100.0 Two separate early returns (tribe state, then index < size): the original pushes ebx/esi/ebp only after the state test. Plain for loops over word and bit index. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/getUnitIDForIndexInTribe.cpp | 41 ++++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp index ef5192be2b..cb900a3c71 100644 --- a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -5,32 +5,33 @@ namespace OpenSHC { namespace Map { namespace Units { + /* + Two separate early returns: the original pushes ebx/esi/ebp only after the + tribe-state test (one `pop edi; ret` for that exit), which a combined + `state == 2 && index < size` guard does not give. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00522390 int TribesState::getUnitIDForIndexInTribe(int tribeID, int unitSelectionIndex) { - int _counter = 0; - int iVar1 = 0; - if ((this->tribes[tribeID].tribeState == 2) && (unitSelectionIndex < this->tribes[tribeID].size)) { - short* psVar3 = this->tribes[tribeID].unitSelectionBitMasked; - do { - /* - The word is read once: `movzx edx, word ptr [esi]` for the zero test, - then `movsx edx, dx` for the bit tests, rather than a load per bit. - */ - short const _bits = *psVar3; - if (_bits != 0) { - for (int iVar2 = 0; iVar2 < 16; iVar2++) { - if ((int)_bits & 1 << iVar2) { - if (_counter == unitSelectionIndex) { - return iVar1 * 0x10 + iVar2; - } - _counter = _counter + 1; + int count = 0; + if (this->tribes[tribeID].tribeState != 2) { + return 0; + } + if (unitSelectionIndex >= this->tribes[tribeID].size) { + return 0; + } + for (int wordIndex = 0; wordIndex < 200; wordIndex++) { + short const bits = this->tribes[tribeID].unitSelectionBitMasked[wordIndex]; + if (bits != 0) { + for (int bitIndex = 0; bitIndex < 16; bitIndex++) { + if ((1 << bitIndex) & (int)bits) { + if (count == unitSelectionIndex) { + return wordIndex * 16 + bitIndex; } + count++; } } - iVar1 = iVar1 + 1; - psVar3 = psVar3 + 1; - } while (iVar1 < 200); + } } return 0; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3fc0e0c4d8..9014d1d454 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46781,7 +46781,7 @@ SHC_3BB0A8C1_0x005222B0 | 0.0% | Pending SHC_3BB0A8C1_0x00522360 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522390 | 58.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522390 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522410 | 55.1% | Reimplemented, asm differs From 81e60707e8f65ef85b6b70fd449df656e5b3ba57 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:25:20 +0100 Subject: [PATCH 157/202] reimplement: snapshotSelectionTribeAndComputeStance 96.8 -> 100.0 multipleTribes is declared after the stance counters, so its zero (ebp) is the one the tribeCopiedToSlot0 store uses, as in the original. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/snapshotSelectionTribeAndComputeStance.cpp | 2 +- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index 570f89174b..0494e3727c 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -18,11 +18,11 @@ namespace Map { void TribesState::snapshotSelectionTribeAndComputeStance(int playerID) { this->tribeCopiedToSlot0 = 0; - BOOL multipleTribes = FALSE; int stanceCount[3]; stanceCount[0] = 0; stanceCount[1] = 0; stanceCount[2] = 0; + BOOL multipleTribes = FALSE; int lastTribeID = -1; for (int unitID = 1; unitID < (int)DAT_UnitsState::instance.maxUnitCount; ++unitID) { if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 9014d1d454..5284b499a1 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46815,7 +46815,7 @@ SHC_3BB0A8C1_0x00522C20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522CA0 | 96.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522CA0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented From 1a5a397a611768191d69ce726ff428a5bf883a6d Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:25:21 +0100 Subject: [PATCH 158/202] reimplement: getAvailableTribeID 62.3 -> 85.0 Both searches are for loops that break on a free slot and re-test the ID after the loop; the slot test indexes through DAT_CurrentTribeID, which stops MSVC strength-reducing the address (the original recomputes it every iteration). The first (single-player) loop still keeps the store in a copy register. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/getAvailableTribeID.cpp | 78 ++++++------------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 24 insertions(+), 56 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index 670b5dd9c3..0e3e052cb6 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -13,78 +13,46 @@ namespace Map { using Game::GameMode; + /* + Both searches break out of their loop and test the slot ID again afterwards; the + original jump-threads that test into each loop's exhausted exit but keeps it on + the found path. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00522720 int TribesState::getAvailableTribeID(int playerID) { - /* - Early returns rather than the decompiler's nested `if (0 < _tribeID)` arms: - the original places each `xor eax, eax` inline behind its own guard. - */ - int _tribeID; + int tribeID; if (DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) { - _tribeID = 1249; - /* - Stored once before the loop (0x52272e) and then only after each - decrement; the decompiler's repeat of the store after the loop is not in - the instruction stream. - - Both of these loops stay as `while`. The original recomputes the address - every iteration and does not strength-reduce them (0x00522733 and - 0x00522790): - - L:mov edx, eax; imul edx, edx, 0x334; add edx, ecx - cmp word ptr [edx + 0x40], 0 ... jg L - - where ours hoists `lea edx, [ecx + 0xfa0de]` and walks it with - `sub edx, 0x334`. Rewriting both as a top test with an explicit jump - back - the change that took the twin createTribeForPlayer from 75.2% to - 86.2% - fixes the rotation but not the reduction: MSVC still hoists the - address, and the result is flat at 62.3%. - - The difference between the two functions is that createTribeForPlayer's - original strength-reduces as well (`sub eax, 0x19a0`), so matching its - loop shape was enough there. Here the reduction is the whole gap and it - is not reachable from the source. - */ - DAT_CurrentTribeID::instance = _tribeID; - while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { - _tribeID = _tribeID + -1; - DAT_CurrentTribeID::instance = _tribeID; - if (_tribeID <= 0) { - return 0; + for (tribeID = 1249, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; + tribeID = tribeID - 1, DAT_CurrentTribeID::instance = tribeID) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0 + && this->tribes[DAT_CurrentTribeID::instance].time == 0) { + break; } } - if (_tribeID <= 0) { + if (tribeID <= 0) { return 0; } - this->tribes[_tribeID].time = 1; - return _tribeID; + this->tribes[tribeID].time = 1; + return tribeID; } - _tribeID = 1250 - playerID; - DAT_CurrentTribeID::instance = _tribeID; - if (_tribeID <= 0) { - return 0; - } - /* - is tribe slot in use? - */ - while (this->tribes[_tribeID].tribeState != 0 || this->tribes[_tribeID].time != 0) { - _tribeID = _tribeID + -8; - DAT_CurrentTribeID::instance = _tribeID; - if (_tribeID <= 0) { - return 0; + for (tribeID = 1250 - playerID, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; + tribeID = tribeID - 8, DAT_CurrentTribeID::instance = tribeID) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0 + && this->tribes[DAT_CurrentTribeID::instance].time == 0) { + break; } } - if (_tribeID <= 0) { + if (tribeID <= 0) { return 0; } /* - claim it. The 400 is added to the full tick count and only then truncated + The 400 is added to the full tick count and only then truncated (`mov edx, [..]`, `add edx, 0x190`, `mov word ptr [..], dx`), not to a value already cut down to a short. */ - this->tribes[_tribeID].time = (short)(DAT_GameCore::instance.mapTimeInTicks + 400); - return _tribeID; + this->tribes[tribeID].time = (short)(DAT_GameCore::instance.mapTimeInTicks + 400); + return tribeID; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 5284b499a1..6e3dbf68f6 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46795,7 +46795,7 @@ SHC_3BB0A8C1_0x00522590 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522630 | 79.5% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00522720 | 62.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522720 | 85.0% | loop zero reg, asm differs SHC_3BB0A8C1_0x005227E0 | 100.0% | Reimplemented From 099b36aef463a51c36c01cbb222a41d9598c3653 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:25:22 +0100 Subject: [PATCH 159/202] reimplement: getSpecificUnitFromTribe 55.1 -> 64.1 Rewritten with locals for the word and bit index, written to the two globals from the locals. Remaining: MSVC keeps a zero in edi (the original only materialises it for the bit index), which costs it ebx for the mask. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/getSpecificUnitFromTribe.cpp | 84 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 33 insertions(+), 53 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp index 1b79b1a1d5..4304fa21da 100644 --- a/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getSpecificUnitFromTribe.cpp @@ -8,65 +8,45 @@ namespace OpenSHC { namespace Map { namespace Units { + /* + An iterator over the tribe's members kept in two globals: the word index in + DAT_Tribe_HighestID and the bit index in DAT_00ee0fb4. param_2 == 0 restarts it. + The globals are written from the locals, which is why the restart and the + per-word reset store a register (`xor ecx, ecx` then `mov [..], ecx`) while the + wrap after the last bit stores an immediate. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00522410 int TribesState::getSpecificUnitFromTribe(int tribeID, int param_2) { - short* psVar3; + int wordIndex; + int bitIndex; if (param_2 == 0) { - DAT_Tribe_HighestID::instance = (int)this->tribes[tribeID].highestID; - DAT_00ee0fb4::instance = 0; + wordIndex = this->tribes[tribeID].highestID; + bitIndex = 0; + DAT_Tribe_HighestID::instance = wordIndex; + DAT_00ee0fb4::instance = bitIndex; + } else { + wordIndex = DAT_Tribe_HighestID::instance; + bitIndex = DAT_00ee0fb4::instance; } - /* - Both globals are read before the bound test, which is the order the original - loads them in. - */ - int iVar1 = DAT_Tribe_HighestID::instance; - int iVar2 = DAT_00ee0fb4::instance; - if (iVar1 < 200) { - psVar3 = this->tribes[tribeID].unitSelectionBitMasked + iVar1; - do { - /* - The word is read once: `movzx edx, word ptr [esi]` for the zero test, - then `movsx edx, dx` for the bit tests, rather than a load per bit. - */ - short const _bits = *psVar3; - if ((_bits != 0) && (iVar2 < 0x10)) { - do { - if ((int)_bits & 1 << iVar2) { - DAT_00ee0fb4::instance = iVar2 + 1; - /* - The wrap arm is the fall-through. The original stores the - incremented bit index, tests it and jumps 0x1d forward - when it has not reached sixteen (`cmp edx, 0x10; mov - [..], edx; jne 0x1d`), so the arm that advances the tribe - id and resets the bit index is what follows the test. - */ - if (iVar2 + 1 == 0x10) { - DAT_Tribe_HighestID::instance = iVar1 + 1; - DAT_00ee0fb4::instance = 0; - return iVar1 * 0x10 + iVar2; - } - DAT_Tribe_HighestID::instance = iVar1; - return iVar1 * 0x10 + iVar2; + for (; wordIndex < 200; wordIndex++) { + short const bits = this->tribes[tribeID].unitSelectionBitMasked[wordIndex]; + if (bits != 0) { + for (; bitIndex < 16; bitIndex++) { + if ((1 << bitIndex) & (int)bits) { + DAT_00ee0fb4::instance = bitIndex + 1; + if (bitIndex + 1 == 16) { + DAT_Tribe_HighestID::instance = wordIndex + 1; + DAT_00ee0fb4::instance = 0; + return wordIndex * 16 + bitIndex; } - iVar2 = iVar2 + 1; - } while (iVar2 < 0x10); + DAT_Tribe_HighestID::instance = wordIndex; + return wordIndex * 16 + bitIndex; + } } - /* - MSVC keeps a zero of its own in a register here - `xor edi, edi` up - front, which costs it `ebx` and turns the function's immediate tests - into register compares (`cmp dx, di` where the original has - `test dx, dx`). The original materialises its zero into the counter - and stores from there. Writing the store below as - `DAT_00ee0fb4::instance = iVar2` to say the same thing measures - 55.1% -> 55.1%: the dedicated zero register survives it, so this is - the register allocator rather than the spelling. - */ - iVar2 = 0; - iVar1 = iVar1 + 1; - psVar3 = psVar3 + 1; - DAT_00ee0fb4::instance = 0; - } while (iVar1 < 200); + } + bitIndex = 0; + DAT_00ee0fb4::instance = bitIndex; } return 0; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6e3dbf68f6..607671e127 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46783,7 +46783,7 @@ SHC_3BB0A8C1_0x00522360 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522390 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522410 | 55.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522410 | 64.1% | zero register, asm differs SHC_3BB0A8C1_0x005224E0 | 100.0% | Reimplemented From e50d9286e16dcca274ac23f3efd5359938175fe9 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:25:22 +0100 Subject: [PATCH 160/202] reimplement: playAttackCommandFeedback 89.1 -> 98.0 The copy of param_1 is taken before the first call, which gives the original's ebx for it; the original still loads it after selectionContainsCombatUnit. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/playAttackCommandFeedback.cpp | 7 +++---- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp index 1cb21ac631..80d4fa03f9 100644 --- a/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp @@ -18,11 +18,10 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522090 void TribesState::playAttackCommandFeedback(int param_1) { - UnitType unitType; - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); int iVar1 = param_1; - unitType - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(param_1, ¶m_1); + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + UnitType unitType + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(iVar1, ¶m_1); if (unitType == Map::Units::UT_S_BATTERINGRAM || unitType == Map::Units::UT_S_CATAPULT || unitType == Map::Units::UT_S_TREBUCHET || unitType == Map::Units::UT_S_MANGONEL || unitType == Map::Units::UT_S_BALLISTA || unitType == Map::Units::UT_S_FBALLISTA) { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 607671e127..e56487474b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46763,7 +46763,7 @@ SHC_3BB0A8C1_0x00522082 | 0.0% | Pending SHC_3BB0A8C1_0x00522083 | 0.0% | Pending -SHC_3BB0A8C1_0x00522090 | 89.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522090 | 98.0% | param load hoisted, asm differs SHC_3BB0A8C1_0x00522110 | 100.0% | Reimplemented From 28339f82170fcfa33af16ab93a0888fd59a87cfd Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:34:19 +0100 Subject: [PATCH 161/202] reimplement: removeUnitFromThisTribeIfInTribe 79.5 -> 100.0 Word and bit index locals before the compound &= free ebp for the tribe base. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../removeUnitFromThisTribeIfInTribe.cpp | 30 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index 83db367adc..07100c0ee7 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -8,12 +8,6 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - diff_lea.py reports RLEA here - the original keeps &tribes[tribeID] in a register - where we recompute it - but a `Tribe&` for the repeated subscript is not the way - to get it: 79.5% -> 58.8% (norm 73.2% -> 58.8%). Measured twice, by quick_diff - and by reccmp. - */ // FUNCTION: STRONGHOLDCRUSADER 0x00522630 void TribesState::removeUnitFromThisTribeIfInTribe(uint unitID, int tribeID) { @@ -29,26 +23,22 @@ namespace Map { == 0) { return; } - /* - The base is NOT taken once here, although the original does take it - (`lea ebp, [eax + ecx]`) and read the size, the tribe state and the - target unit through it as `[ebp + 0x5c]`, `[ebp + 0x40]` and `[ebp + 0x5a]`. - Mirroring that - shifted TribesState pointer, bitmask accesses left on the - subscript where the original also keeps them - costs 34.4 points - (79.5% -> 45.1%). The same change is worth +11.8 in trimTribeToSize, which - is the one function where it has ever paid. Why is not known: the - "only addressing base" explanation I first wrote here is wrong, since - updateAnimalHerdBehaviorState fits it and lost 40.8 points. - */ short const _size = this->tribes[tribeID].size; if (0 < _size) { - this->tribes[tribeID].size = _size + -1; + this->tribes[tribeID].size = _size - 1; } if (this->tribes[tribeID].size <= 0) { this->tribes[tribeID].tribeState = 3; } - short* psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); - *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[(int)unitID % 16]; + /* + The word index and the bit index get a local each before the compound + assignment: the original divides before it multiplies the tribe ID, which + frees ebp to keep the tribe's base for the reads after it. + */ + int const wordIndex = (int)unitID / 16; + int const bitIndex = (int)unitID % 16; + this->tribes[tribeID].unitSelectionBitMasked[wordIndex] + &= DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[bitIndex]; if (DAT_UnitsState::instance.units[unitID].tribeID == tribeID) { DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = 0; DAT_UnitsState::instance.units[unitID].tribeID = 0; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e56487474b..3db2138cd0 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46793,7 +46793,7 @@ SHC_3BB0A8C1_0x00522550 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522590 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522630 | 79.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522630 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522720 | 85.0% | loop zero reg, asm differs From 3b9f0154c941155bbad4f2fb272a574a0489c283 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:34:20 +0100 Subject: [PATCH 162/202] reimplement: removeUnitFromTribe 67.1 -> 100.0 Word and bit index locals before the compound &=, as in removeUnitFromThisTribeIfInTribe. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/removeUnitFromTribe.cpp | 23 ++++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index f539308f12..130d335eaf 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -22,25 +22,22 @@ namespace Map { || ((int)this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) != 0) { - /* - The base is NOT taken once here, although the original does take it - (`lea ebp, [eax + ebx]`) and read the size, the tribe state and the - target unit through it as `[ebp + 0x5c]`, `[ebp + 0x40]` and `[ebp + 0x5a]`. - Mirroring that - shifted TribesState pointer, bitmask accesses left on the - subscript where the original also keeps them - costs 29.9 points - (67.1% -> 37.2%). The same change is worth +11.8 in trimTribeToSize, which - differs in having only the two size reads on the base; here a second - addressing base is live for the bitmask and the two fight over registers. - */ short const _size = this->tribes[tribeID].size; if (0 < _size) { - this->tribes[tribeID].size = _size + -1; + this->tribes[tribeID].size = _size - 1; } if (this->tribes[tribeID].size <= 0) { this->tribes[tribeID].tribeState = 3; } - short* psVar1 = this->tribes[tribeID].unitSelectionBitMasked + ((int)unitID / 16); - *psVar1 = *psVar1 & DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[(int)unitID % 16]; + /* + The word index and the bit index get a local each before the compound + assignment: the original divides before it multiplies the tribe ID, + which frees ebp to keep the tribe's base for the reads after it. + */ + int const wordIndex = (int)unitID / 16; + int const bitIndex = (int)unitID % 16; + this->tribes[tribeID].unitSelectionBitMasked[wordIndex] + &= DAT_UnitSelectionDefinedData::instance.BitMaskHelper2[bitIndex]; if (DAT_UnitsState::instance.units[unitID].tribeID == tribeID) { DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = 0; DAT_UnitsState::instance.units[unitID].tribeID = 0; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3db2138cd0..8b11748f26 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46945,7 +46945,7 @@ SHC_3BB0A8C1_0x00525870 | 97.1% | Reimplemented, asm differs SHC_3BB0A8C1_0x00525970 | 97.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525A70 | 67.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented From babf46406836e9c7cfc980d0195a983ba0591624 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:34:20 +0100 Subject: [PATCH 163/202] reimplement: trimTribeToSize 70.6 -> 100.0 DAT_CurrentTribeID read once into a local, size read once for both guards, tribe reached through a shifted TribesState pointer. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/trimTribeToSize.cpp | 71 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 26 insertions(+), 47 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 8536d034c4..015e24469b 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -15,55 +15,34 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522EF0 void TribesState::trimTribeToSize(undefined4 tribeID, int smallerSize) { - UnitLogicStateShort* pUVar1; - UnitLogicStateShort UVar2; - int iVar3; - int unitSelectionIndex; - int _currentTribeID; - _currentTribeID = DAT_CurrentTribeID::instance; - unitSelectionIndex = 0; + int const currentTribeID = DAT_CurrentTribeID::instance; + int unitSelectionIndex = 0; /* - The size is read through a base taken once, which is what the original does - (`lea ebp, [eax + edi]` at 0x522f0b, then `[ebp + 0x5c]`); the tribeState test - above it stays on the subscript, where the original has - `cmp word ptr [eax + edi + 0x40], si`. Hoisting a pointer to the size field - itself was tried earlier and cost 1.2 points - that gives `[ptr]` with no - displacement, not the `+0x5c` the original uses. - - Do not try to predict where this helps. Caching the base the way the original - does is worth +11.8 here and loses everywhere else it has been measured: - removeUnitFromTribe -29.9, removeUnitFromThisTribeIfInTribe -34.4, - updateAnimalHerdBehaviorState -40.8, giveTribeAnInstruction -4.8, - updateTribeUnitAssignments flat. I thought the discriminator was whether the - cached base is the function's only addressing base; updateAnimalHerdBehaviorState - fits that description better than this function does and lost forty points, so - the rule is wrong. Measure each function on its own. + The tribe is reached through a TribesState pointer shifted by the tribe, not a + `Tribe&`: the original takes `lea ebp, [eax + edi]` and reads the size as + `[ebp + 0x5c]`, where a reference folds the `tribes` offset into the base + (0.93 against 1.0 in quick_diff). The tribe is DAT_CurrentTribeID, read once; + the tribeID parameter is unused, as in the original. */ - TribesState* const _tb = (TribesState*)((char*)this + DAT_CurrentTribeID::instance * sizeof(Tribe)); - if (this->tribes[DAT_CurrentTribeID::instance].tribeState != 0 - && (iVar3 = (int)_tb->tribes[0].size, iVar3 >= smallerSize) && 0 < iVar3) { - do { - iVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - _currentTribeID, unitSelectionIndex); - pUVar1 = &DAT_UnitsState::instance.units[iVar3].logicalState; - UVar2 = *pUVar1; - unitSelectionIndex = unitSelectionIndex + 1; - if ((((UVar2 == ((UnitLogicState)1)) || (UVar2 == Map::Units::ULS_NORMAL)) - || (UVar2 == Map::Units::ULS_TRANSITIONING)) - /* - The index comes first in both of this loop's comparisons. The - original has `cmp esi, dword ptr [esp + 0x18]` with a `jl` here - and `cmp esi, ecx` with a `jl` on the back-edge - esi being the - index in each - where `smallerSize <= index` gives the reverse - `cmp` and a `jg`. I first swapped the back-edge instead of this - one, which made a correct comparison wrong and measured flat - overall; the two cancelled out. - */ - && unitSelectionIndex >= smallerSize) { - *pUVar1 = Map::Units::ULS_REMOVE; - } - } while (unitSelectionIndex < _tb->tribes[0].size); + TribesState* const _tb = (TribesState*)((char*)this + currentTribeID * sizeof(Tribe)); + if (this->tribes[currentTribeID].tribeState == 0) { + return; + } + int const size = _tb->tribes[0].size; + if (size < smallerSize || size <= 0) { + return; } + do { + int const unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + currentTribeID, unitSelectionIndex); + UnitLogicStateShort const value = DAT_UnitsState::instance.units[unitID].logicalState; + unitSelectionIndex = unitSelectionIndex + 1; + if ((value == (UnitLogicState)1 || value == Map::Units::ULS_NORMAL + || value == Map::Units::ULS_TRANSITIONING) + && unitSelectionIndex >= smallerSize) { + DAT_UnitsState::instance.units[unitID].logicalState = Map::Units::ULS_REMOVE; + } + } while (unitSelectionIndex < _tb->tribes[0].size); } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 8b11748f26..6b94ceb10e 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46819,7 +46819,7 @@ SHC_3BB0A8C1_0x00522CA0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522EF0 | 70.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00522EF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522F70 | 100.0% | Reimplemented From ea2aed98dcd937d0f00542de74ef9dbcf89efde7 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:34:21 +0100 Subject: [PATCH 164/202] reimplement: upgradeTribeArrayLayoutForMapVersion 72.5 -> 77.5 Index the tribe array per iteration instead of walking two pointers. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../upgradeTribeArrayLayoutForMapVersion.cpp | 23 ++++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 8e2bd9625c..0743bfc8e3 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -16,22 +16,17 @@ namespace Map { PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) { if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 0xa9) { - int tribeIndex = 1249; - char* src = (char*)this->tribes + 1249 * 0x26c; - Tribe* dst = &this->tribes[1249]; - do { - if ((void*)dst != (void*)src) { - MACRO_CALL(OpenSHC::OS_Func::_memmove)(dst, src, 0x26c); + for (int tribeIndex = 1249; tribeIndex >= 0; --tribeIndex) { + char* const oldRecord = (char*)this->tribes + tribeIndex * 0x26c; + if ((void*)&this->tribes[tribeIndex] != (void*)oldRecord) { + MACRO_CALL(OpenSHC::OS_Func::_memmove)(&this->tribes[tribeIndex], oldRecord, 0x26c); } - short* maskTail = &dst->unitSelectionBitMasked[100]; - MACRO_CALL(OpenSHC::OS_Func::_memmove)(&dst->field_0x1c8, src + 0x100, 0x16c); - --tribeIndex; - src -= 0x26c; - --dst; - for (int k = 0; k < 100; ++k) { - maskTail[k] = 0; + MACRO_CALL(OpenSHC::OS_Func::_memmove)( + &this->tribes[tribeIndex].field_0x1c8, oldRecord + 0x100, 0x16c); + for (int k = 100; k < 200; ++k) { + this->tribes[tribeIndex].unitSelectionBitMasked[k] = 0; } - } while (tribeIndex >= 0); + } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6b94ceb10e..e35e881d3e 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46871,7 +46871,7 @@ SHC_3BB0A8C1_0x00523E30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523EF0 | 72.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523EF0 | 77.5% | register assignment of src/dst swapped (ebx/esi); rep stosd placement SHC_3BB0A8C1_0x00523F70 | 100.0% | Reimplemented From d3d1648fa8f2082eaf94a582affc29c3807dd380 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:45:36 +0100 Subject: [PATCH 165/202] TribesState: moveTribeToIndexedNearbyTile returns void The original returns no value. On the early exit EAX still holds the quotient of its idiv; at the end of the search it holds the y coordinate or the path test's 0. The only caller (updateTribeCombatStanceBehavior, 0x52AC9A) ignores it. With an int return type the function has to compute that value and cannot match (74.8% at best in quick_diff). As void it matches 100%. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Map/Units/TribesState.func.hpp | 2 +- src/OpenSHC/Map/Units/TribesState.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState.func.hpp b/src/OpenSHC/Map/Units/TribesState.func.hpp index 01d4062594..30fe76b715 100644 --- a/src/OpenSHC/Map/Units/TribesState.func.hpp +++ b/src/OpenSHC/Map/Units/TribesState.func.hpp @@ -456,7 +456,7 @@ namespace Map { &TribesState::moveTribeToNearbyClearTile) moveTribeToNearbyClearTile; - MACRO_FUNCTION_RESOLVER(int (TribesState::*)(int), false, Address::SHC_3BB0A8C1_0x0052A700, + MACRO_FUNCTION_RESOLVER(void (TribesState::*)(int), false, Address::SHC_3BB0A8C1_0x0052A700, &TribesState::moveTribeToIndexedNearbyTile) moveTribeToIndexedNearbyTile; diff --git a/src/OpenSHC/Map/Units/TribesState.hpp b/src/OpenSHC/Map/Units/TribesState.hpp index 1e64889bf1..ea6a780c43 100644 --- a/src/OpenSHC/Map/Units/TribesState.hpp +++ b/src/OpenSHC/Map/Units/TribesState.hpp @@ -298,7 +298,7 @@ namespace Map { void moveTribeToNearbyClearTile(int tribeID); - int moveTribeToIndexedNearbyTile(int param_1); + void moveTribeToIndexedNearbyTile(int param_1); void updateTribeCombatStanceBehavior(int tribeID); From 0ad67a2aeb5d1203ae642b882a831512c6ca76d9 Mon Sep 17 00:00:00 2001 From: Gynt Date: Thu, 3 Dec 2026 23:54:20 +0100 Subject: [PATCH 166/202] reimplement: moveTribeToIndexedNearbyTile 45.9 -> 98.6 void return (see the previous commit), the count as a variable so MSVC emits the original's idiv, and x read before y. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../moveTribeToIndexedNearbyTile.cpp | 91 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 34 insertions(+), 59 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index 5e056992da..f084e11e78 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -1,7 +1,6 @@ #include "../../../Map.func.hpp" #include "OpenSHC/Map/Units/TribesState.func.hpp" -#include "OpenSHC/Map/Navigation/Algorithms/XYPair.hpp" #include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" #include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" @@ -12,73 +11,49 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Navigation::Algorithms::XYPair; using Map::Units::Instructions::UnitMatchSpeedEnum; + /* + Returns nothing: the value left in EAX is the quotient of the idiv on the early + exit, the y coordinate or the path test's 0 at the end of the search, and the + only caller (updateTribeCombatStanceBehavior, 0x52AC9A) ignores it. + + The candidate count is a variable, not a literal: with `% 40` MSVC emits a + reciprocal multiply, while the original divides with `mov ecx, 0x28; idiv ecx`. + + Remaining gap (98.6%): MSVC turns the index into a pointer and ends the loop at + `&field_0xcc4[40].y`, a relocation against our data, where the original has the + plain address 0xb4df44. Writing that address as a literal matches, but it is + only right while the data stays at the original address, so the index stays. + */ // FUNCTION: STRONGHOLDCRUSADER 0x0052A700 - int TribesState::moveTribeToIndexedNearbyTile(int param_1) + void TribesState::moveTribeToIndexedNearbyTile(int tribeID) { - uint uVar3; - int* piVar6; - uint uVar7; - int sVar1 = this->tribes[param_1].selectionTargetUnitID; - uint uVar4 = (uint)DAT_UnitsState::instance.units[sVar1].y; - uVar7 = (uint)DAT_UnitsState::instance.units[sVar1].x; - int iVar2 = (int)this->tribes[param_1].someIndex; - /* - The division here will not match from this direction, and naming the - quotient is not the lever. The original divides once - `cdq`, `mov ecx, - 0x28`, `idiv ecx` - and the unreachable arm returns the quotient that same - instruction left in EAX. Ours emits a reciprocal multiply instead: - `mov eax, 0x66666667`, `imul`, `sar edx, 4`, `shr`, four `add`s, a `sub`. - - Naming the quotient in a local of its own, so that MSVC can see both halves - are wanted, does not help: measured 45.9% -> 45.9%, and the reciprocal - multiply is still there. MSVC1400 under /O2 turns division by any literal - constant into a reciprocal multiply regardless of whether the remainder is - also needed, so no arrangement of `/ 40` and `% 40` will produce an `idiv`. - For the original to have one, its divisor cannot have been a literal in the - source - something that reads 40 from memory, or a named value the compiler - could not fold. That is the thing to find; it is not a statement-order or - local-naming problem. - */ - int iVar5 = iVar2 % 40; - if (iVar5 <= 0) { - iVar5 = 0; - } else if (39 < iVar5) { - return iVar2 / 40; + int const targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + int const x = DAT_UnitsState::instance.units[targetUnitID].x; + int const y = DAT_UnitsState::instance.units[targetUnitID].y; + int const someIndex = this->tribes[tribeID].someIndex; + int count = 40; + int index = someIndex % count; + if (index <= 0) { + index = 0; + } else if (index >= count) { + return; } - piVar6 = &DAT_UnitSelectionDefinedData::instance.field_0xcc4[iVar5].y; do { - uint const x1 = piVar6[-1] + uVar7; - uint const y1 = *piVar6 + uVar4; - uVar3 = uVar4; + uint const x1 = DAT_UnitSelectionDefinedData::instance.field_0xcc4[index].x + x; + uint const y1 = DAT_UnitSelectionDefinedData::instance.field_0xcc4[index].y + y; if (x1 <= 399 && y1 <= 399 && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] != 0) { - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( - param_1, uVar7, uVar4, x1, y1); - uVar3 = 0; - if (iVar2) { - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - param_1, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); - return iVar2; + if (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( + tribeID, x, y, x1, y1)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + return; } } - piVar6 = piVar6 + 2; - /* - 0xb4df44 is one past the end of the forty-entry candidate array - it is - at 0xb4d13c + 0xcc4 and the cursor points at the second member of each - eight-byte entry, so the end is 0xb4de00 + 40 * 8 + 4. The literal stays - against the style list: written - `< (int)(&...field1608_0xcc4[39].y + 2)`, which is the same address, the - function measures 45.9% -> 38.5%. - - Carrying the return value in `iVar5`, which is dead from the line above, - to save the stack slot the original does not have (`sub esp, 8` against - our 0xc) was measured at the same time and is not what costs the points. - */ - } while ((int)piVar6 < 0xb4df44); - return (int)(uVar3); + ++index; + } while (index < count); } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e35e881d3e..bb233b24da 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -48877,7 +48877,7 @@ SHC_3BB0A8C1_0x0052A638 | 0.0% | Pending SHC_3BB0A8C1_0x0052A640 | 98.4% | Reimplemented, one instruction off: MSVC anchors the strength-reduced loop pointer on XYPair::x where the original anchors on ::y, so the loop sentinel immediate differs -SHC_3BB0A8C1_0x0052A700 | 45.9% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A700 | 98.6% | loop end is a relocation (&field_0xcc4[40].y) where the original has the absolute 0xb4df44 SHC_3BB0A8C1_0x0052A7D0 | 75.1% | Reimplemented, asm differs From e7033a6b7d635ecc2e88dd2545a262b8624b1a9a Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:09:10 +0100 Subject: [PATCH 167/202] reimplement: getAvailableTribeID 85.0 -> 100.0 The solitary search runs on DAT_CurrentTribeID and takes the ID only when it finds a free slot, as createTribe does. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/getAvailableTribeID.cpp | 10 +++++++--- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index 0e3e052cb6..186050ab4f 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -16,17 +16,21 @@ namespace Map { /* Both searches break out of their loop and test the slot ID again afterwards; the original jump-threads that test into each loop's exhausted exit but keeps it on - the found path. + the found path. The solitary search runs on DAT_CurrentTribeID itself and takes + the ID only when it finds a slot, as createTribe does; counting a local down + beside the global stores the global from a copy of the register. */ // FUNCTION: STRONGHOLDCRUSADER 0x00522720 int TribesState::getAvailableTribeID(int playerID) { int tribeID; if (DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY) { - for (tribeID = 1249, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; - tribeID = tribeID - 1, DAT_CurrentTribeID::instance = tribeID) { + tribeID = 0; + for (DAT_CurrentTribeID::instance = 1249; (int)DAT_CurrentTribeID::instance > 0; + DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance - 1) { if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0 && this->tribes[DAT_CurrentTribeID::instance].time == 0) { + tribeID = DAT_CurrentTribeID::instance; break; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index bb233b24da..d6cc8cdc58 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46795,7 +46795,7 @@ SHC_3BB0A8C1_0x00522590 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522630 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522720 | 85.0% | loop zero reg, asm differs +SHC_3BB0A8C1_0x00522720 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005227E0 | 100.0% | Reimplemented From 60f52dc54cbe6feb4d2423ebbe051e146ba0659c Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:09:10 +0100 Subject: [PATCH 168/202] reimplement: spawnDeerLionOrRabbit 74.1 -> 100.0 Rewritten from the disassembly: the tribe through a shifted TribesState pointer, the herd sizes as shorts (compared 16 bits wide), straight early returns, and the reads ordered so x and y land in ebx and ebp. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/spawnDeerLionOrRabbit.cpp | 140 +++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 56 insertions(+), 86 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index fe7b8be6ba..272072713c 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -19,24 +19,16 @@ namespace Map { using UI::Enums::MenuViewType; using WindowsHelper::Enums::BOOLEnum; + /* + The tribe is reached through a TribesState pointer shifted by the tribe, as in + trimTribeToSize: the original keeps `lea esi, [eax + ecx]` and reads every field + at its offset in TribesState. The sizes are compared sixteen bits wide + (`cmp cx, di`), so they are shorts. The reads of the spawn position, terrain + height, herd sizes and RNG byte are in the order that puts x and y in ebx and ebp. + */ // FUNCTION: STRONGHOLDCRUSADER 0x00525F20 - BOOLEnum TribesState::spawnDeerLionOrRabbit(int tribeID, int param_2, UnitType unitType) + BOOLEnum TribesState::spawnDeerLionOrRabbit(int tribeID, int spawnInterval, UnitType unitType) { - ushort* psVar1; - int sVar2; - int _finalTargetUnitID; - int iVar3; - uint _rng2; - int _terrainHeight; - int _unknown; - int _tribeSize; - int _unitX; - int _targetUnitID; - int _unitY; - /* - Two early-return guards, which is where the original puts its own - `xor eax, eax` epilogues. - */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { return FALSE; } @@ -46,80 +38,58 @@ namespace Map { if (this->tribes[tribeID].predatorNearby != 0) { return FALSE; } - _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; - _finalTargetUnitID = (int)_targetUnitID; + TribesState* const _tb = (TribesState*)((char*)this + tribeID * sizeof(Tribe)); + int const targetUnitID = _tb->tribes[0].selectionTargetUnitID; + int unitID = targetUnitID; if (unitType == Map::Units::UT_ANTELOPESHDEER) { - _finalTargetUnitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); - if (!_finalTargetUnitID) { - _finalTargetUnitID = (int)_targetUnitID; + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); + if (unitID == 0) { + unitID = targetUnitID; } } - /* - Masked here, not at the use. The original does `and edx, 0x7f` right after - reading the byte, before it reads the herd size and the coordinates; leaving - the mask on the comparison forty lines down makes MSVC do it there instead. - The value is only ever read masked - it is overwritten by the spawn result - straight afterwards - so this is the same program. - */ - _rng2 = (uint)(byte)SEC_RNG::instance.currentNumber2 & 0x7f; - _terrainHeight = DAT_UnitsState::instance.units[_finalTargetUnitID].terrainOrClimbHeight; - /* - Both of these reads are `movzx` in the original, not `movsx`. They stay - `int`s cast on the way in rather than `ushort` locals, although the original - compares them against each other sixteen bits wide (`cmp cx, di`): as - `ushort` the function measures 74.1% -> 70.2%. - */ - _unknown = (ushort)this->tribes[tribeID].herdMinSize; - _tribeSize = (ushort)this->tribes[tribeID].size; - _unitX = DAT_UnitsState::instance.units[_finalTargetUnitID].x; - _unitY = DAT_UnitsState::instance.units[_finalTargetUnitID].y; - if (_tribeSize <= _unknown) { - param_2 = param_2 / 4; + int const x = DAT_UnitsState::instance.units[unitID].x; + int const y = DAT_UnitsState::instance.units[unitID].y; + int const terrainHeight = DAT_UnitsState::instance.units[unitID].terrainOrClimbHeight; + short const herdMinSize = _tb->tribes[0].herdMinSize; + short const size = _tb->tribes[0].size; + int const rng = (byte)SEC_RNG::instance.currentNumber2 & 0x7f; + if (size <= herdMinSize) { + spawnInterval = spawnInterval / 4; } - /* - Keeping the pointer rather than naming the field: resolving it costs one - instruction here (quick_diff 0.7295 -> 0.7265), because the field form - re-evaluates tribes[tribeID] while the pointer freezes the address. - */ - psVar1 = &this->tribes[tribeID].field_0x284; - *psVar1 = *psVar1 + 1; - if ((this->tribes[tribeID].field_0x284 >= param_2) - && (sVar2 = this->tribes[tribeID].herdMaxSize, this->tribes[tribeID].field_0x284 = 0, - _tribeSize < sVar2)) { - if ((unitType == Map::Units::UT_RABBIT) - && (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation)) { - iVar3 = 110; - } else { - iVar3 = _tribeSize < _unknown ? 25 : 100; - } - if (iVar3 <= (int)_rng2) { - _rng2 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( - 0, 0, (int)(_unitX * 8), (int)(_unitY * 8), (int)(_terrainHeight), unitType); - /* - The `if (_rng2)` stays. The original returns FALSE through an - epilogue of its own right here - `test esi, esi; jne 0xc` over - `pop/pop/pop/xor eax,eax/pop/add esp,0xc/ret 0xc` - where ours jumps - to the shared one at the end, so a flat `if (!_rng2) return FALSE;` - looks like the fix. Measured 73.0% -> 73.0%, raw and normalized both - unchanged. - */ - if (_rng2) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(_rng2, tribeID); - DAT_UnitsState::instance.units[_rng2].substate - = (byte)DAT_UnitsState::instance.units[_rng2].fixedRng & 3; - if (unitType == Map::Units::UT_ANTELOPESHDEER) { - DAT_UnitsState::instance.units[_rng2].antelopeBasedRngValue = 2; - } - DAT_UnitsState::instance.units[_rng2].disappearFadeAlphaCountdown = 0x20; - if (unitType == Map::Units::UT_ANTELOPESHDEER) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateAnimalHerdBehaviorState, this)( - tribeID); - } - return TRUE; - } - } + _tb->tribes[0].field_0x284 = _tb->tribes[0].field_0x284 + 1; + if ((short)_tb->tribes[0].field_0x284 < spawnInterval) { + return FALSE; + } + _tb->tribes[0].field_0x284 = 0; + if (size >= _tb->tribes[0].herdMaxSize) { + return FALSE; + } + int chance; + if (unitType == Map::Units::UT_RABBIT + && DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation) { + chance = 110; + } else { + chance = size < herdMinSize ? 25 : 100; + } + if (chance > rng) { + return FALSE; + } + int const spawnedUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + 0, 0, x * 8, y * 8, terrainHeight, unitType); + if (spawnedUnitID == 0) { + return FALSE; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(spawnedUnitID, tribeID); + DAT_UnitsState::instance.units[spawnedUnitID].substate + = (short)(DAT_UnitsState::instance.units[spawnedUnitID].fixedRng & 3); + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + DAT_UnitsState::instance.units[spawnedUnitID].antelopeBasedRngValue = 2; + } + DAT_UnitsState::instance.units[spawnedUnitID].disappearFadeAlphaCountdown = 0x20; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateAnimalHerdBehaviorState, this)(tribeID); } - return FALSE; + return TRUE; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index d6cc8cdc58..40f15eed2c 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46951,7 +46951,7 @@ SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00525F20 | 74.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525F20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005260B0 | 100.0% | Reimplemented From 9454ad5b376f7fe8ea75829cad551210ce64a3fe Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:21:43 +0100 Subject: [PATCH 169/202] reimplement: updateCamelTribeBehavior 76.3 -> 100.0 Rewritten from the disassembly: each branch above the dispatch returns on its own, the move order and counters are ushorts compared 16 bits wide, the timeout is an int, and the rally point is indexed inline. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/updateCamelTribeBehavior.cpp | 180 +++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 69 insertions(+), 113 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index e528a15733..9a7d79e7f5 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -20,132 +20,88 @@ namespace Map { using UI::Enums::MenuViewType; using WindowsHelper::Enums::BOOLEnum; + /* + Every branch above the behaviour dispatch ends in its own `ret 4` in the + original, so each returns here. The move order and the update counter are + ushorts compared sixteen bits wide (`cmp ax, dx`); the timeout is an int compared + against the counter read as a short (`movsx eax, ax; cmp eax, ecx`). + */ // FUNCTION: STRONGHOLDCRUSADER 0x0052B630 - void TribesState::updateCamelTribeBehavior(int param_1) + void TribesState::updateCamelTribeBehavior(int tribeID) { - short* psVar1; - SomeTribeBehaviorTypeShort SVar2; - BOOLEnum BVar3; - int iVar4; - int sVar5; - BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( - param_1, this->tribes[param_1].uid); - this->tribes[param_1].unknownCounter01 = this->tribes[param_1].unknownCounter01 + 1; - sVar5 = this->tribes[param_1].unknownCounter01; - this->tribes[param_1].unkIsAnimalTribe = 1; - if (8000 <= sVar5) { - this->tribes[param_1].unknownCounter01 = 4000; + BOOLEnum const noTunnelers = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)(tribeID, this->tribes[tribeID].uid); + this->tribes[tribeID].unknownCounter01 = this->tribes[tribeID].unknownCounter01 + 1; + this->tribes[tribeID].unkIsAnimalTribe = 1; + if (this->tribes[tribeID].unknownCounter01 >= 8000) { + this->tribes[tribeID].unknownCounter01 = 4000; } - if ((BVar3) - && (iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trySpawnAdditionalWildlifeForTribe, this)( - param_1, 1000, 10, 0x2f), - iVar4 != 0)) { - /* - The original returns here too: the block at 0x52b69c ends in `ret 4`, so - the read of initialMoveOrderGiven below never sees the 1 just written. - */ - this->tribes[param_1].initialMoveOrderGiven = 1; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + if (noTunnelers != FALSE + && MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trySpawnAdditionalWildlifeForTribe, this)( + tribeID, 1000, 10, 0x2f) + != 0) { + this->tribes[tribeID].initialMoveOrderGiven = 1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; return; } - sVar5 = this->tribes[param_1].initialMoveOrderGiven; - if (!sVar5) { - /* - The original returns here - its block ends in `ret 4` - which is also why - it can reuse the register that held initialMoveOrderGiven for the unit ID. - Without the return, the `sVar5 == 1` test below reads a unit ID. - */ - sVar5 = this->tribes[param_1].selectionTargetUnitID; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; - this->tribes[param_1].initialMoveOrderGiven = 2; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, - (uint)(DAT_UnitsState::instance.units[sVar5].x), (uint)(DAT_UnitsState::instance.units[sVar5].y), 0, + ushort const moveOrder = this->tribes[tribeID].initialMoveOrderGiven; + if (moveOrder == 0) { + int const targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y, 0, 0, Map::Units::Instructions::UMSE_0); - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; return; } - /* - The map-editor branch at 0x52b704 ends in `ret 4` and is the only path that - zeroes the counter; the import had it zeroed on every call, which wiped the - counter the 0x65 and 0x1f40 tests at the top of the function read. - */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { - this->tribes[param_1].unknownCounter01 = 0; + this->tribes[tribeID].unknownCounter01 = 0; return; } - { - if (sVar5 == 1) { - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; - this->tribes[param_1].initialMoveOrderGiven = 2; + if (moveOrder == 1) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 2, 0); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][0], + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][1], 0, 0, + Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; + if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { + this->tribes[tribeID].rallyPointCount = 0; + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + } + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + return; + } + if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter++ == 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(param_1, 2, 0); - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); - sVar5 = this->tribes[param_1].currentRallyPointIndex; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, - (uint)((int)this->tribes[param_1].rallyPointArray[sVar5][0]), - (uint)((int)this->tribes[param_1].rallyPointArray[sVar5][1]), 0, 0, - Map::Units::Instructions::UMSE_0); - this->tribes[param_1].currentRallyPointIndex = this->tribes[param_1].currentRallyPointIndex + 1; - /* - Operands this way round, not `rallyPointCount <= index`. The - original has `cmp ax, word ptr [esi + 0x28a]` with a `jl` - the - index first, the count second - where the reverse spelling gives - `cmp word ptr [esi + 0x28a], ax` and a `jg`. Same test, two - different instructions. diff_cmp_operands.py finds these. - */ - if (this->tribes[param_1].currentRallyPointIndex >= this->tribes[param_1].rallyPointCount) { - this->tribes[param_1].rallyPointCount = 0; - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + DAT_WildlifeState::ptr)(tribeID, 2, 1); + if (this->tribes[tribeID].rallyPointCount > 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + int const lastRallyPoint = this->tribes[tribeID].rallyPointCount; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, + this)(tribeID, this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][0], + this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][1], 0, 0); } - /* - And this block returns as well: 0x52b794 stores the behaviour type a - second time and falls into its own `ret 4`, so the dispatch below is - not reached on this path. - */ - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; - return; + } else if (noTunnelers != FALSE) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } - SVar2 = this->tribes[param_1].tribeBehaviorType; - if (SVar2 == (SomeTribeBehaviorType)0) { - sVar5 = this->tribes[param_1].unknownAttackRelatedUpdateCounter; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = sVar5 + 1; - if (!sVar5) { - MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(param_1, 2, 1); - if (0 < this->tribes[param_1].rallyPointCount) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); - iVar4 = (int)this->tribes[param_1].rallyPointCount; - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - param_1, (uint)((int)this->tribes[param_1].rallyPointArray[iVar4 + -1][0]), - (uint)((int)this->tribes[param_1].rallyPointArray[iVar4 + -1][1]), 0, 0); - } - } else if (BVar3) { - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; - } - } else if (SVar2 == Map::Units::STBT_1) { - /* - A `short` of its own rather than another use of `sVar5`, which is an - `int`: the original compares this against the counter sixteen bits - wide (`cmp ax, dx`), where an `int` threshold gives `cmp eax, ecx`. - */ - short _behaviourTimeout = 800; - /* - Written `<= 0x64`, which is the literal the original compares - - `cmp word ptr [esi + ..], 100` with a `jg` - where `< 0x65` gives - `cmp .., 101` and a `jge`. The same test; MSVC emits the constant - it is handed. - */ - if (this->tribes[param_1].unknownCounter01 <= 0x64) { - _behaviourTimeout = 10; - } - psVar1 = (short*)&this->tribes[param_1].unknownAttackRelatedUpdateCounter; - *psVar1 = *psVar1 + 1; - if (_behaviourTimeout <= this->tribes[param_1].unknownAttackRelatedUpdateCounter) { - this->tribes[param_1].tribeBehaviorType = ((SomeTribeBehaviorType)0); - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; - } + } else if (this->tribes[tribeID].tribeBehaviorType == Map::Units::STBT_1) { + int timeout = 800; + if (this->tribes[tribeID].unknownCounter01 <= 100) { + timeout = 10; + } + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if ((short)this->tribes[tribeID].unknownAttackRelatedUpdateCounter >= timeout) { + this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 40f15eed2c..e9b172a8c6 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49147,7 +49147,7 @@ SHC_3BB0A8C1_0x0052B110 | 72.7% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B630 | 76.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B630 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs From 5c14ff6010b1bae65029ea119b6cbc539992d4a4 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:21:43 +0100 Subject: [PATCH 170/202] reimplement: updateLionWolfTribeBehavior 72.7 -> 100.0 Rewritten in the same shape as updateCamelTribeBehavior. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../updateLionWolfTribeBehavior.cpp | 193 ++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 83 insertions(+), 112 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index e78d92f8c4..2dcecdc5ec 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -22,128 +22,99 @@ namespace Map { using UI::Enums::MenuViewType; using WindowsHelper::Enums::BOOLEnum; + /* + The same shape as updateCamelTribeBehavior: every branch above the behaviour + dispatch ends in its own `ret 4` in the original, and the countdown, move order + and update counter are ushorts compared sixteen bits wide. + */ // FUNCTION: STRONGHOLDCRUSADER 0x0052B110 - void TribesState::updateLionWolfTribeBehavior(int param_1) + void TribesState::updateLionWolfTribeBehavior(int tribeID) { - short* psVar1; - int sVar2; - SomeTribeBehaviorTypeShort SVar3; - int sVar4; - BOOLEnum BVar5; - BOOLEnum BVar6; - BVar5 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( - param_1, this->tribes[param_1].uid); - this->tribes[param_1].unknownCounter01 = this->tribes[param_1].unknownCounter01 + 1; - sVar2 = this->tribes[param_1].unknownCounter01; - this->tribes[param_1].unkIsAnimalTribe = 1; - if (8000 <= sVar2) { - this->tribes[param_1].unknownCounter01 = 4000; + BOOLEnum const noTunnelers = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)(tribeID, this->tribes[tribeID].uid); + this->tribes[tribeID].unknownCounter01 = this->tribes[tribeID].unknownCounter01 + 1; + this->tribes[tribeID].unkIsAnimalTribe = 1; + if (this->tribes[tribeID].unknownCounter01 >= 8000) { + this->tribes[tribeID].unknownCounter01 = 4000; } - sVar2 = this->tribes[param_1].countdown; - /* - The non-zero side is the fall-through. The original reads the countdown and - jumps 0x25 forward when it is zero (`cmp ax, dx; je 0x25`) - exactly the span - of the setStateForAllTribeUnits arm - then tests `cmp ax, 0x64` and leaves - when it is not 100. So the 100 arm sits under the test and the ninety lines - of wander and attack handling go in the far block. - */ - if (sVar2) { - if (sVar2 == 100) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setStateForAllTribeUnits, this)(param_1, 0xd0); - this->tribes[param_1].unknownBool01 = 1; + ushort const countdown = this->tribes[tribeID].countdown; + if (countdown != 0) { + if (countdown == 100) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setStateForAllTribeUnits, this)(tribeID, 0xd0); + this->tribes[tribeID].unknownBool01 = 1; } - } else { - if ((BVar5) - && (BVar6 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( - param_1, 500, Map::Units::UT_LIONSHWOLF), - BVar6)) { - /* - This block returns too: 0x52b1ab ends in `ret 4`, so the read of - initialMoveOrderGiven below never sees the 1 just written. - */ - this->tribes[param_1].initialMoveOrderGiven = 1; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; - return; - } - sVar2 = this->tribes[param_1].initialMoveOrderGiven; - if (!sVar2) { - /* - The original returns here - its block ends in `ret 4` after setting - the behaviour type - which is also why it can reuse the register that - held initialMoveOrderGiven for the unit ID. Without the return, the - `sVar2 == 1` test below reads a unit ID. - */ - sVar2 = this->tribes[param_1].selectionTargetUnitID; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; - this->tribes[param_1].initialMoveOrderGiven = 2; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, - (uint)(DAT_UnitsState::instance.units[sVar2].x), - (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, 0, Map::Units::Instructions::UMSE_0); - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; - return; + return; + } + if (noTunnelers != FALSE + && MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + tribeID, 500, Map::Units::UT_LIONSHWOLF) + != FALSE) { + this->tribes[tribeID].initialMoveOrderGiven = 1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + return; + } + ushort const moveOrder = this->tribes[tribeID].initialMoveOrderGiven; + if (moveOrder == 0) { + int const targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y, 0, + 0, Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + return; + } + if (moveOrder == 1) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].initialMoveOrderGiven = 2; + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 2, 0); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][0], + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][1], 0, 0, + Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; + if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { + this->tribes[tribeID].rallyPointCount = 0; + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; } - if (sVar2 == 1) { - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; - this->tribes[param_1].initialMoveOrderGiven = 2; + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + return; + } + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[tribeID].unknownCounter01 = 0; + return; + } + if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter++ == 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(param_1, 2, 0); - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); - sVar2 = this->tribes[param_1].currentRallyPointIndex; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(param_1, - (uint)((int)this->tribes[param_1].rallyPointArray[sVar2][0]), - (uint)((int)this->tribes[param_1].rallyPointArray[sVar2][1]), 0, 0, - Map::Units::Instructions::UMSE_0); - this->tribes[param_1].currentRallyPointIndex = this->tribes[param_1].currentRallyPointIndex + 1; - if (this->tribes[param_1].currentRallyPointIndex >= this->tribes[param_1].rallyPointCount) { - this->tribes[param_1].rallyPointCount = 0; - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; + DAT_WildlifeState::ptr)(tribeID, 2, 0); + if (this->tribes[tribeID].rallyPointCount > 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyLadderDestructionToTribeUnits, this)( + tribeID); } - /* - And this one returns: 0x52b28c stores the behaviour type a second time - and falls into its own `ret 4`, so neither the map-editor test nor the - dispatch below is reached on this path. - */ - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; - return; - } - /* - The original returns from this one: the block at 0x52b297 ends in `ret 4` - right after zeroing the counter, as in its three sibling updaters. - */ - if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { - this->tribes[param_1].unknownCounter01 = 0; - return; - } - SVar3 = this->tribes[param_1].tribeBehaviorType; - if (SVar3 == (SomeTribeBehaviorType)0) { - sVar4 = this->tribes[param_1].unknownAttackRelatedUpdateCounter; - sVar2 = sVar4 + 1; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = sVar2; - if (!sVar4) { - MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(param_1, 2, 0); - if (0 < this->tribes[param_1].rallyPointCount) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(param_1); - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyLadderDestructionToTribeUnits, this)( - param_1); - } - } else if (sVar2 == 0x96) { - if ((this->tribes[param_1].unknownBool02 == 0) - && (sVar2 = (int)this->tribes[param_1].rallyPointCount, 0 < sVar2)) { + } else if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 150) { + if (this->tribes[tribeID].unknownBool02 == 0) { + short const lastRallyPoint = this->tribes[tribeID].rallyPointCount; + if (lastRallyPoint > 0) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - param_1, (uint)((int)this->tribes[param_1].rallyPointArray[sVar2 + -1][0]), - (uint)((int)this->tribes[param_1].rallyPointArray[sVar2 + -1][1]), 0, 0); + tribeID, this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][0], + this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][1], 0, 0); } - } else if (BVar5) { - this->tribes[param_1].tribeBehaviorType = Map::Units::STBT_1; - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; } - } else if ((SVar3 == Map::Units::STBT_1) - && (psVar1 = (short*)&this->tribes[param_1].unknownAttackRelatedUpdateCounter, - *psVar1 = *psVar1 + 1, this->tribes[param_1].unknownAttackRelatedUpdateCounter == 300)) { - this->tribes[param_1].tribeBehaviorType = ((SomeTribeBehaviorType)0); - this->tribes[param_1].unknownAttackRelatedUpdateCounter = 0; + } else if (noTunnelers != FALSE) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + } + } else if (this->tribes[tribeID].tribeBehaviorType == Map::Units::STBT_1) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 300) { + this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e9b172a8c6..0af8d467bc 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49143,7 +49143,7 @@ SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD50 | 63.3% | Reimplemented, asm differs -SHC_3BB0A8C1_0x0052B110 | 72.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B110 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs From 615dc1339db63028f0a8148dd678c8e467cb7fdb Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:21:44 +0100 Subject: [PATCH 171/202] reimplement: updateRabbitTribeBehavior 63.0 -> 100.0 Rewritten in the same shape as updateCamelTribeBehavior. The grid cell is indexed in full at each read instead of through x / 10 and y / 10 locals. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/updateRabbitTribeBehavior.cpp | 211 +++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 76 insertions(+), 137 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index 007f3cf26d..effbb94ba3 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -23,163 +23,102 @@ namespace Map { using UI::Enums::MenuViewType; using WindowsHelper::Enums::BOOLEnum; + /* + The same shape as updateCamelTribeBehavior. The grid cell is indexed in full in + both reads rather than through named x / 10 and y / 10 locals, which put the + tribe ID and `this` in other registers. + */ // FUNCTION: STRONGHOLDCRUSADER 0x0052B390 void TribesState::updateRabbitTribeBehavior(int tribeID) { - /* - Base cached as the original does (`lea`, two registers, no displacement), so - the field displacements are unchanged - the subscript already matches those. - Worth +1.4 here, and that is the whole of it. The same change on the three - nearest siblings loses: updateCamelTribeBehavior -14.2, - updateLionWolfTribeBehavior -6.0, computeTribePercentages -2.1. Across the - namespace caching the base now stands at two clear wins (trimTribeToSize and - createTribe, +11.8 each, the latter also fixing displacements), this +1.4, one - flat, and five losses from -2.1 to -40.8. There is no predictor; measure. - - Re-measured once the namespace had moved on by twenty-odd other changes, - because the three near-identical siblings all use the plain subscript and - two of them score higher than this one (updateCamelTribeBehavior 71.9, - updateLionWolfTribeBehavior 72.7, processDeerMoving 63.3). Reverting the - base here to the subscript still loses: 63.0% -> 61.1%. The +1.4 stands. - */ - TribesState* const _tb = (TribesState*)((char*)this + tribeID * sizeof(Tribe)); - short* psVar1; - int sVar2; - SomeTribeBehaviorTypeShort SVar3; - int sVar4; - short _rallyPointCount; - int iVar5; - int iVar6; - BOOLEnum BVar7; - int iVar8; - int _x10; - int _y10; - int _targetUnitID; - sVar2 = _tb->tribes[0].selectionTargetUnitID; - _x10 = (int)DAT_UnitsState::instance.units[sVar2].x / 10; - _y10 = (int)DAT_UnitsState::instance.units[sVar2].y / 10; - iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; - iVar6 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; - BVar7 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)( - tribeID, _tb->tribes[0].uid); - _tb->tribes[0].unkIsAnimalTribe = 1; - iVar8 = 80; + int const targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + int const rockyTiles = DAT_WildlifeState::instance + .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] + [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .rockyTiles; + int const marshTiles = DAT_WildlifeState::instance + .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] + [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .marshTiles; + BOOLEnum const noTunnelers = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)(tribeID, this->tribes[tribeID].uid); + this->tribes[tribeID].unkIsAnimalTribe = 1; + int spawnInterval = 80; if (DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation) { - iVar8 = 3; + spawnInterval = 3; } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( - tribeID, iVar8, Map::Units::UT_RABBIT); - if (_tb->tribes[0].initialMoveOrderGiven == 0) { - _targetUnitID = _tb->tribes[0].selectionTargetUnitID; - _tb->tribes[0].initialMoveOrderGiven = 1; + tribeID, spawnInterval, Map::Units::UT_RABBIT); + if (this->tribes[tribeID].initialMoveOrderGiven == 0) { + int const unitID = this->tribes[tribeID].selectionTargetUnitID; + this->tribes[tribeID].initialMoveOrderGiven = 1; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, - (uint)(DAT_UnitsState::instance.units[_targetUnitID].x), - (uint)(DAT_UnitsState::instance.units[_targetUnitID].y), 0, 0, Map::Units::Instructions::UMSE_0); - _tb->tribes[0].tribeBehaviorType = Map::Units::STBT_1; - /* - And from this one: the block at 0x52b440 ends in `ret 4` after setting - the behaviour type, like its three sibling updaters. - */ + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0, 0, + Map::Units::Instructions::UMSE_0); + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; return; } - /* - The original returns from this one: the block at 0x52b489 ends in `ret 4` - right after zeroing the counter. - */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { - _tb->tribes[0].unknownCounter01 = 0; + this->tribes[tribeID].unknownCounter01 = 0; return; } - sVar2 = _tb->tribes[0].predatorNearby; - if (!sVar2) { - SVar3 = _tb->tribes[0].tribeBehaviorType; - if (SVar3 == (SomeTribeBehaviorType)0) { - sVar4 = _tb->tribes[0].unknownAttackRelatedUpdateCounter; - sVar2 = sVar4 + 1; - _tb->tribes[0].unknownAttackRelatedUpdateCounter = sVar2; - if (!sVar4) { - BVar7 = MACRO_CALL_MEMBER( - Map::WildlifeState_Func::buildRallyPointPathForTribe, DAT_WildlifeState::ptr)(tribeID, 4); - if (!BVar7) { - MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(tribeID, 2, 1); - } - if (0 < _tb->tribes[0].rallyPointCount) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyLadderDestructionToTribeUnits, this)( - tribeID); - } - } else if (sVar2 == 0x96) { - /* - The count is read into a `short`: the original compares it - sixteen bits wide, `cmp ax, dx` with a signed `jle`, and widens - only for the index, `movsx eax, ax`. An `int` local compares - `cmp eax, edx` and loses the pair. The `jle` is still signed, so - a negative count does not wrap into a huge index. - - This is the only local here that wants narrowing, and the - reasoning does not carry to its neighbours. diff_inv_jcc.py - still reports `cmp ax, dx` against our `cmp eax, edx` for the - predator flag at the top of the function, and the original - really does test that one sixteen bits wide too - but making it - a `short` measures 63.0 -> 43.5%. Tested alone, so it is the - predator flag specifically, not the attack counter it was first - bundled with. Leave it as an `int` and ignore the hint. - */ - _rallyPointCount = _tb->tribes[0].rallyPointCount; - if (0 < _rallyPointCount) { - MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( - tribeID, (uint)((int)_tb->tribes[0].rallyPointArray[(int)_rallyPointCount + -1][0]), - (uint)((int)_tb->tribes[0].rallyPointArray[(int)_rallyPointCount + -1][1]), 0, 0); - } - } else if (BVar7) { - _tb->tribes[0].tribeBehaviorType = Map::Units::STBT_1; - _tb->tribes[0].unknownAttackRelatedUpdateCounter = 0; - } - } else if (SVar3 == Map::Units::STBT_1) { - /* - The marsh-tile arm comes first: `cmp ebp, edx; jz` falls into the - 0x14 store and jumps away to the rocky-tile test. - */ - if (iVar6) { - _tb->tribes[0].wanderIntervalTicks = 0x14; - } else if (0x14 < iVar5) { - _tb->tribes[0].wanderIntervalTicks = 0x14; - } else { - _tb->tribes[0].wanderIntervalTicks = 400; - } - psVar1 = (short*)&_tb->tribes[0].unknownAttackRelatedUpdateCounter; - *psVar1 = *psVar1 + 1; - /* - The original compares these two sixteen bits wide (`cmp ax, dx`) - where the fields compared in place promote to `int` and give - `cmp eax, edx`. Reading both into `short` locals first to say so - measures 63.0% -> 62.5%, so the narrowing is not worth what the two - extra loads cost here - unlike updateCamelTribeBehavior, where the - same change to a threshold local is worth 3.2. - */ - if (_tb->tribes[0].wanderIntervalTicks <= _tb->tribes[0].unknownAttackRelatedUpdateCounter) { - _tb->tribes[0].tribeBehaviorType = ((SomeTribeBehaviorType)0); - _tb->tribes[0].unknownAttackRelatedUpdateCounter = 0; - } - } - } else { - if (sVar2 == 1) { + ushort const predatorNearby = this->tribes[tribeID].predatorNearby; + if (predatorNearby != 0) { + if (predatorNearby == 1) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 3, 0); - if (_tb->tribes[0].rallyPointCount <= 0) { + if (this->tribes[tribeID].rallyPointCount <= 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 3, 1); } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); } - psVar1 = (short*)&_tb->tribes[0].predatorNearby; - *psVar1 = *psVar1 + 1; - if (2999 < _tb->tribes[0].predatorNearby) { - _tb->tribes[0].predatorNearby = 0; + this->tribes[tribeID].predatorNearby = this->tribes[tribeID].predatorNearby + 1; + if ((short)this->tribes[tribeID].predatorNearby >= 3000) { + this->tribes[tribeID].predatorNearby = 0; + } + return; + } + if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter++ == 0) { + if (MACRO_CALL_MEMBER(Map::WildlifeState_Func::buildRallyPointPathForTribe, DAT_WildlifeState::ptr)( + tribeID, 4) + == FALSE) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 2, 1); + } + if (this->tribes[tribeID].rallyPointCount > 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyLadderDestructionToTribeUnits, this)( + tribeID); + } + } else if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 150) { + short const lastRallyPoint = this->tribes[tribeID].rallyPointCount; + if (lastRallyPoint > 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, + this)(tribeID, this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][0], + this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][1], 0, 0); + } + } else if (noTunnelers != FALSE) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + } + } else if (this->tribes[tribeID].tribeBehaviorType == Map::Units::STBT_1) { + if (marshTiles != 0) { + this->tribes[tribeID].wanderIntervalTicks = 20; + } else if (rockyTiles > 20) { + this->tribes[tribeID].wanderIntervalTicks = 20; + } else { + this->tribes[tribeID].wanderIntervalTicks = 400; + } + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if ((short)this->tribes[tribeID].unknownAttackRelatedUpdateCounter + >= this->tribes[tribeID].wanderIntervalTicks) { + this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 0af8d467bc..6644f140ba 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49145,7 +49145,7 @@ SHC_3BB0A8C1_0x0052AD50 | 63.3% | Reimplemented, asm differs SHC_3BB0A8C1_0x0052B110 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0052B390 | 63.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B390 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B630 | 100.0% | Reimplemented From fa4e088cfeb7c9eef8051b3938601e96ed7c7f2b Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:31:05 +0100 Subject: [PATCH 172/202] reimplement: processDeerMoving 63.3 -> 100.0 Rewritten in the same shape as updateCamelTribeBehavior; the grid reads in the order that gives the original's load order (marsh, rocky, then field_0x48 + lionCount). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/processDeerMoving.cpp | 222 ++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 97 insertions(+), 127 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index 778d8cdb89..89875cb9a1 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -27,69 +27,55 @@ namespace Map { using UI::Enums::MenuViewType; using WindowsHelper::Enums::BOOLEnum; + /* + The same family as updateCamelTribeBehavior and updateRabbitTribeBehavior: the + grid cell indexed in full at each read, each branch above the dispatch returning + on its own, and the rally-point reads indexed inline rather than through a local. + The three grid reads are in the order that gives the original's load order and + registers (rocky tiles, lion count, marsh tiles, then the add). + */ // FUNCTION: STRONGHOLDCRUSADER 0x0052AD50 void TribesState::processDeerMoving(int tribeID) { - BOOLEnum BVar7; - int iVar8; - int sVar2 = this->tribes[tribeID].selectionTargetUnitID; - /* - Both coordinates are read before either division: the original has - `movsx edx, [+0x610]`, `movsx ecx, [+0x612]` and only then the two - reciprocal multiplies. - */ - short const _unitX = DAT_UnitsState::instance.units[sVar2].x; - short const _unitY = DAT_UnitsState::instance.units[sVar2].y; - int _x10 = (int)_unitX / 10; - int _y10 = (int)_unitY / 10; - /* - The predator sum's second term is read after the marsh tiles, not with the - first: the original reads 0x1c, then 0x00, then 0x18, and only then - `add ebp, [.. + 0x20]`. - */ - iVar8 = DAT_WildlifeState::instance.grid[_x10][_y10].rockyTiles; - int _predatorCount = DAT_WildlifeState::instance.grid[_x10][_y10].lionCount; - int iVar5 = DAT_WildlifeState::instance.grid[_x10][_y10].marshTiles; - _predatorCount = _predatorCount + DAT_WildlifeState::instance.grid[_x10][_y10].field_0x48; - /* - A `short`, compared as one: `movzx ecx, word ptr [esi + 0x5c]` then - `cmp cx, 0x1e`, with the sign extension deferred to the halving. - */ - short const _size = this->tribes[tribeID].size; + int const targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + int const marshTiles = DAT_WildlifeState::instance + .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] + [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .marshTiles; + int const rockyTiles = DAT_WildlifeState::instance + .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] + [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .rockyTiles; + int const predatorCount = DAT_WildlifeState::instance + .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] + [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .field_0x48 + + DAT_WildlifeState::instance + .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] + [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .lionCount; + short const size = this->tribes[tribeID].size; this->tribes[tribeID].unkIsAnimalTribe = 1; - /* - The trim is the arm the original places first: `cmp cx, 0x1e; jle` falls into - it and jumps to the herd-size arm. - */ - if (30 < _size) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trimTribeToSize, this)(tribeID, (int)(30)); - this->tribes[tribeID].herdMinSize = 0xf; - this->tribes[tribeID].herdMaxSize = 0x3c; + if (size > 30) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trimTribeToSize, this)(tribeID, 30); + this->tribes[tribeID].herdMinSize = 15; + this->tribes[tribeID].herdMaxSize = 60; } else { - this->tribes[tribeID].herdMinSize = _size / 2; - this->tribes[tribeID].herdMaxSize = _size * 2; + this->tribes[tribeID].herdMinSize = size / 2; + this->tribes[tribeID].herdMaxSize = size * 2; } - if ((_predatorCount != 0) && (this->tribes[tribeID].predatorNearby == 0)) { + if (predatorCount != 0 && this->tribes[tribeID].predatorNearby == 0) { this->tribes[tribeID].predatorNearby = 1; } - BVar7 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( - tribeID, 100, Map::Units::UT_ANTELOPESHDEER); - /* - The spawn-succeeded arm comes first. The original tests the result and jumps - 0x7d forward to the did-not-spawn body (`je 0x7d`), so the arm that gives the - move order is the fall-through and ends in its own `ret 4`. Written - `if (!BVar7)` that whole arm - the rally-point call, standUpAllTribeUnits, - giveTribeMoveInstruction, the currentRallyPointIndex bump and the - rallyPointCount reset - lands in the far block and the test inverts to `jne`. - */ - if (BVar7) { + if (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + tribeID, 100, Map::Units::UT_ANTELOPESHDEER) + != FALSE) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 2, 0); MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); - sVar2 = this->tribes[tribeID].currentRallyPointIndex; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, - (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][0]), - (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0, + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][0], + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][1], 0, 0, Map::Units::Instructions::UMSE_0); this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { @@ -99,98 +85,82 @@ namespace Map { return; } if (this->tribes[tribeID].initialMoveOrderGiven == 0) { - /* - The original returns here - its block ends in `ret 4` after setting - the behaviour type - which is also why it can reuse the register that - held initialMoveOrderGiven for the unit ID. - */ - sVar2 = this->tribes[tribeID].selectionTargetUnitID; + int const unitID = this->tribes[tribeID].selectionTargetUnitID; this->tribes[tribeID].initialMoveOrderGiven = 1; MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)(tribeID, - (uint)(DAT_UnitsState::instance.units[sVar2].x), (uint)(DAT_UnitsState::instance.units[sVar2].y), 0, - 0, Map::Units::Instructions::UMSE_0); + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0, 0, + Map::Units::Instructions::UMSE_0); this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; return; } - /* - The original returns from this one too: the block at 0x52af03 ends in - `ret 4` right after zeroing the counter. - */ if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { this->tribes[tribeID].unknownCounter01 = 0; return; } - sVar2 = this->tribes[tribeID].predatorNearby; - if (!sVar2) { - int SVar3 = this->tribes[tribeID].tribeBehaviorType; - if (SVar3 == (SomeTribeBehaviorType)0) { - if (iVar5) { - this->tribes[tribeID].wanderIntervalTicks = 200; - } - if (0x14 < iVar8) { - this->tribes[tribeID].wanderIntervalTicks = 200; - } - this->tribes[tribeID].unknownAttackRelatedUpdateCounter - = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; - if (this->tribes[tribeID].wanderIntervalTicks - <= this->tribes[tribeID].unknownAttackRelatedUpdateCounter) { - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - this->tribes[tribeID].wanderIntervalTicks - = (4 - ((byte)SEC_RNG::instance.currentNumber2 & 3)) * 200; - /* - Compared against the memory operand, not through a local: the - original has `cmp word ptr [..], 0; jg`. The block it falls - into at 0x52b064 ends in `ret 4`, so a tribe with no rally - points stops here instead of walking the array it has not got. - */ - if (this->tribes[tribeID].rallyPointCount <= 0) { - MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(tribeID, 5, 0); - return; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); - sVar2 = this->tribes[tribeID].currentRallyPointIndex; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, - this)(tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][0]), - (uint)((int)this->tribes[tribeID].rallyPointArray[sVar2][1]), 0, 0); - this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; - if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { - this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; - this->tribes[tribeID].rallyPointCount = 0; - } - } - } else if (SVar3 == Map::Units::STBT_1) { - this->tribes[tribeID].unknownAttackRelatedUpdateCounter - = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; - if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 800) { - this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - this->tribes[tribeID].tribeBehaviorType = ((SomeTribeBehaviorType)0); - } - } - } else { - if (sVar2 == 1) { + ushort const predatorNearby = this->tribes[tribeID].predatorNearby; + if (predatorNearby != 0) { + if (predatorNearby == 1) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 5, 0); - if ((this->tribes[tribeID].rallyPointCount < 1) - && (MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, - DAT_WildlifeState::ptr)(tribeID, 5, 1), - this->tribes[tribeID].rallyPointCount < 1)) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); - } else { - sVar2 = this->tribes[tribeID].selectionTargetUnitID; + if (this->tribes[tribeID].rallyPointCount <= 0) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 1); + } + if (this->tribes[tribeID].rallyPointCount > 0) { + int const unitID = this->tribes[tribeID].selectionTargetUnitID; MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocation, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[sVar2].x, - (int)(DAT_UnitsState::instance.units[sVar2].y), DE::SHCDE::FX_DEER_RUN); - iVar8 = (int)this->tribes[tribeID].rallyPointCount; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, - this)(tribeID, (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][0]), - (uint)((int)this->tribes[tribeID].rallyPointArray[iVar8 + -1][1]), 0, 0); + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DE::SHCDE::FX_DEER_RUN); + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)(tribeID, + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].rallyPointCount - 1][0], + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].rallyPointCount - 1][1], 0, 0); + } else { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::scatterTribeUnitsRandomly, this)(tribeID); } } this->tribes[tribeID].predatorNearby = this->tribes[tribeID].predatorNearby + 1; - if (799 < this->tribes[tribeID].predatorNearby) { + if ((short)this->tribes[tribeID].predatorNearby >= 800) { this->tribes[tribeID].predatorNearby = 0; } + return; + } + if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (marshTiles != 0) { + this->tribes[tribeID].wanderIntervalTicks = 200; + } + if (rockyTiles > 20) { + this->tribes[tribeID].wanderIntervalTicks = 200; + } + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if ((short)this->tribes[tribeID].unknownAttackRelatedUpdateCounter + >= this->tribes[tribeID].wanderIntervalTicks) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].wanderIntervalTicks = (4 - (SEC_RNG::instance.currentNumber2 & 3)) * 200; + if (this->tribes[tribeID].rallyPointCount <= 0) { + MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, + DAT_WildlifeState::ptr)(tribeID, 5, 0); + return; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::standUpAllTribeUnits, this)(tribeID); + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)(tribeID, + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][0], + this->tribes[tribeID].rallyPointArray[this->tribes[tribeID].currentRallyPointIndex][1], 0, 0); + this->tribes[tribeID].currentRallyPointIndex = this->tribes[tribeID].currentRallyPointIndex + 1; + if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].rallyPointCount = 0; + } + } + } else if (this->tribes[tribeID].tribeBehaviorType == Map::Units::STBT_1) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter + = this->tribes[tribeID].unknownAttackRelatedUpdateCounter + 1; + if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 800) { + this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6644f140ba..23a0a55ba5 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49141,7 +49141,7 @@ SHC_3BB0A8C1_0x0052AD41 | 0.0% | Pending SHC_3BB0A8C1_0x0052AD42 | 0.0% | Pending -SHC_3BB0A8C1_0x0052AD50 | 63.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052AD50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B110 | 100.0% | Reimplemented From 222de88bda1297b79503576aec7853471c99d998 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:54:05 +0100 Subject: [PATCH 173/202] reimplement: commandUnitsToLocation 42.3 -> 71.4 Rewritten as one test per rejection, each setting rallyRelatedFlag and continuing. freeUnitSpeeds is written through DAT_TribesState::instance, as the original does (absolute 0x1668190). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/commandUnitsToLocation.cpp | 137 +++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 72 insertions(+), 67 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index c8fd7f89c7..b798106f1c 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -10,6 +10,7 @@ #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" #include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" namespace OpenSHC { @@ -26,76 +27,80 @@ namespace Map { BOOLEnum TribesState::commandUnitsToLocation( int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) { - int _unitID; - UnitTypeShort _unitType; - int _selectionUnitID = 0; - /* - A dword flag, not a `bool`: diff_triage.py's BYTE column counts the original's - two byte-sized stack accesses, which are the two `(char)matchUnitSpeeds` - tests, and a byte-sized flag adds three more. - */ - BOOLEnum bVar1 = TRUE; - if (0 < this->tribes[tribeID].size) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _selectionUnitID); - _selectionUnitID = _selectionUnitID + 1; - if ((DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) - && (DAT_UnitsState::instance.units[_unitID].dying == 0)) { - short _goToRallyPoint = DAT_UnitsState::instance.units[_unitID].goToRallyPoint; - DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; - if (!_goToRallyPoint && DAT_UnitsState::instance.units[_unitID].isSelectable_OR_matchTime != 0 - && (MACRO_CALL_MEMBER( - Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, - DAT_DirectionAlgorithmState::ptr)(destinationX, (int)(destinationY), - (int)(DAT_UnitsState::instance.units[_unitID].x), - (int)(DAT_UnitsState::instance.units[_unitID].y)), - 4 < DAT_DirectionAlgorithmState::instance.distanceHigh) - && (DAT_UnitsState::instance.units[_unitID].movementType_OR_targetUnitID == 0 - || (DAT_UnitsState::instance - .units[DAT_UnitsState::instance.units[_unitID] - .targetedUnitID__OR__engineerMannedSiegeEngineRef] - .logicalState - != Map::Units::ULS_NORMAL)) - && (((_goToRallyPoint = DAT_UnitsState::instance.units[_unitID].shootTargetedUnit, - _goToRallyPoint == 0 - || (this->tribes[DAT_CurrentTribeID::instance].countdown2 <= 0)) - || (DAT_UnitsState::instance.units[_unitID].unitType == Map::Units::UT_A_HARCHER)) - || (DAT_UnitsState::instance.units[_unitID].targetUID - != DAT_UnitsState::instance.units[_goToRallyPoint].uid)) - && DAT_UnitsState::instance.units[_unitID].state.generic - != Map::Units::States::US_MELEE_ATTACK - && ((char)matchUnitSpeeds != '\0' - || (((_unitType = DAT_UnitsState::instance.units[_unitID].unitType, - _unitType != Map::Units::UT_S_FBALLISTA - && (_unitType != Map::Units::UT_S_CATAPULT)) - && ((_unitType != Map::Units::UT_S_SHIELD - && ((_unitType != Map::Units::UT_S_TOWER - && (_unitType != Map::Units::UT_S_BATTERINGRAM)))))))) - && (DAT_UnitsState::instance.units[_unitID].engineerRelatedUnk == 0 - || ((DAT_UnitsState::instance.units[_unitID].resourceToDeposit != 0 - && (DAT_UnitsState::instance.units[_unitID].field_0x3c4 == 0))))) { - DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 0; - bVar1 = FALSE; - } else { - DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag = 1; - } + int index = 0; + BOOLEnum noneCommanded = TRUE; + if (this->tribes[tribeID].size <= 0) { + return FALSE; + } + do { + int const unitID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, index); + index = index + 1; + if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[unitID].dying != 0) { + continue; + } + short const goToRallyPoint = DAT_UnitsState::instance.units[unitID].goToRallyPoint; + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 0; + if (goToRallyPoint != 0 || DAT_UnitsState::instance.units[unitID].isSelectable_OR_matchTime == 0) { + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 1; + continue; + } + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(destinationX, destinationY, + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y); + if (DAT_DirectionAlgorithmState::instance.distanceHigh <= 4) { + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 1; + continue; + } + if (DAT_UnitsState::instance.units[unitID].movementType_OR_targetUnitID != 0 + && DAT_UnitsState::instance + .units[DAT_UnitsState::instance.units[unitID] + .targetedUnitID__OR__engineerMannedSiegeEngineRef] + .logicalState + == Map::Units::ULS_NORMAL) { + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 1; + continue; + } + short const shootTarget = DAT_UnitsState::instance.units[unitID].shootTargetedUnit; + if (shootTarget != 0 && this->tribes[DAT_CurrentTribeID::instance].countdown2 > 0 + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_A_HARCHER + && DAT_UnitsState::instance.units[unitID].targetUID + == DAT_UnitsState::instance.units[shootTarget].uid) { + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 1; + continue; + } + if (DAT_UnitsState::instance.units[unitID].state.generic == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 1; + continue; + } + if ((char)matchUnitSpeeds == '\0') { + UnitTypeShort const unitType = DAT_UnitsState::instance.units[unitID].unitType; + if (unitType == Map::Units::UT_S_FBALLISTA || unitType == Map::Units::UT_S_CATAPULT + || unitType == Map::Units::UT_S_SHIELD || unitType == Map::Units::UT_S_TOWER + || unitType == Map::Units::UT_S_BATTERINGRAM) { + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 1; + continue; } - } while (_selectionUnitID < this->tribes[tribeID].size); - /* - An early return, which is where the original puts its own `xor eax, eax`. - */ - if (bVar1) { - return FALSE; } - if ((char)matchUnitSpeeds != '\0') { - this->tribes[tribeID].freeUnitSpeeds = 0; + if (DAT_UnitsState::instance.units[unitID].engineerRelatedUnk != 0 + && (DAT_UnitsState::instance.units[unitID].resourceToDeposit == 0 + || DAT_UnitsState::instance.units[unitID].field_0x3c4 != 0)) { + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 1; + continue; } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); - return TRUE; + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 0; + noneCommanded = FALSE; + } while (index < this->tribes[tribeID].size); + if (noneCommanded != FALSE) { + return FALSE; + } + if ((char)matchUnitSpeeds != '\0') { + DAT_TribesState::instance.tribes[tribeID].freeUnitSpeeds = 0; } - return FALSE; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); + return TRUE; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 23a0a55ba5..f63b88b645 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46967,7 +46967,7 @@ SHC_3BB0A8C1_0x005266F0 | 0.0% | Pending SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending -SHC_3BB0A8C1_0x00526F00 | 42.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00526F00 | 71.4% | constant 1 is held in ebx in the original; ours puts the tribe offset there SHC_3BB0A8C1_0x005270F0 | 47.2% | Reimplemented, asm differs From a52759bd15526873387730bb46f35b8ec6742590 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:54:05 +0100 Subject: [PATCH 174/202] reimplement: computeTribePercentages 47.2 -> 58.6 Rewritten with early continues for the rejected units and the state read at each test instead of through a local. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/computeTribePercentages.cpp | 193 ++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 61 insertions(+), 134 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 9050328e50..0fe0a5db4c 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -25,32 +25,7 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x005270F0 void TribesState::computeTribePercentages(int tribeID) { - uint _unitID; - BOOLEnum BVar3; - /* - The unit index is its own `int`: the original reaches it with a single - `movsx eax, word ptr [esi + 0x5a]`, where sharing the `short` that later holds - the percentages costs a `movzx` plus a `movsx`. - */ - /* - The tribe stays a subscript at each access. The original computes its address - once in the prologue (`lea esi, [eax + ecx]`) and then reloads `this` from a - stack slot for the call in the loop (`mov ecx, [esp + 0x24]`), where we hold - `this` in a register and spill the loop counter instead - so a - `TribesState*` base of our own looks like the way to free it. It is not: - measured 47.2% -> 45.2%. MSVC commons the base out of the subscripts anyway - and the explicit pointer only changes which register it picks. - */ - int const _targetUnitID = this->tribes[tribeID].selectionTargetUnitID; - /* - An `int`, not a `short`. The original keeps each percentage in a 32-bit - register and stores `ax` straight into the field - `idiv ecx` then - `mov word ptr [esi + 0x2be], ax`. A `short` local makes MSVC truncate the - division result and widen it back before every store, adding a - `movzx eax, ax` at all four sites; the original has none. - */ - int _percentage = 0; - int iVar4 = 0; + int const targetUnitID = this->tribes[tribeID].selectionTargetUnitID; this->tribes[tribeID].percentageDyingUnk = 0; this->tribes[tribeID].percentageMovingUnk = 0; this->tribes[tribeID].percentageAttackingUnk = 0; @@ -58,114 +33,66 @@ namespace Map { this->tribes[tribeID].percentageShootingUnk = 0; this->tribes[tribeID].field_0x2a0 = 0; this->tribes[tribeID].percentageSomething = 0; - this->tribes[tribeID].movementSpeed = DAT_UnitsState::instance.units[_targetUnitID].movementSpeed; - int local_8 = 0; - int _countRangeUnk = 0; - int _countAttackingUnk = 0; - int _countMoving = 0; - int _countDying = 0; - this->tribes[tribeID].unitType = DAT_UnitsState::instance.units[_targetUnitID].unitType; - /* - The size is read in the test, not into a local. The original never loads it - into a register: it compares the field in place against the zero it already - has in bp for the seven word stores above - `cmp word ptr [esi + 0x5c], bp`. - A local forces a load of its own. - */ - if (0 < this->tribes[tribeID].size) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)( - tribeID, iVar4); - iVar4 = iVar4 + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL) { - BVar3 = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::isComputerManagedNonPeasant, DAT_UnitsState::ptr)(_unitID); - if (!BVar3) { - /* - The dying tally is the fall-through. The original jumps 0xa - forward when the flag is clear (`je 0xa`) - exactly the - `add dword ptr [esp + 0x14], 1` and `jmp` that follow - so - the one-line increment sits under the test and the thirty - lines of tallying go in the far block. - */ - if (DAT_UnitsState::instance.units[_unitID].dying != 0) { - _countDying = _countDying + 1; - } else { - if (DAT_UnitsState::instance.units[_unitID].movementRelated != 8) { - _countMoving = _countMoving + 1; - } - UnitStateShort UVar2 = DAT_UnitsState::instance.units[_unitID].state.generic; - if (UVar2 == Map::Units::States::US_MELEE_ATTACK) { - _countAttackingUnk = _countAttackingUnk + 1; - } - if (UVar2 == Map::Units::States::US_MELEE_ATTACK_WALL) { - _countAttackingUnk = _countAttackingUnk + 1; - } - if (UVar2 - == (Map::Units::States::US_DEATH_02 | Map::Units::States::US_STAND_UPUnk - | Map::Units::States::US_RELOAD_WEAPONUnk)) { - local_8 = local_8 + 1; - } - if (DAT_UnitsState::instance.units[_unitID].field_0x430 != 0 - && ((UVar2 == Map::Units::States::US_RELOAD_WEAPONUnk - || (UVar2 == Map::Units::States::US_AIM_WEAPONUnk)) - || (UVar2 == Map::Units::States::US_FIRE_WEAPONUnk))) { - _countRangeUnk = _countRangeUnk + 1; - } - UnitTypeShort _unitType = this->tribes[tribeID].unitType; - if (_unitType != (UnitType)0 - && DAT_UnitsState::instance.units[_unitID].unitType != _unitType) { - this->tribes[tribeID].unitType = ((UnitType)0); - } - short _movementSpeed = DAT_UnitsState::instance.units[_unitID].movementSpeed; - if (_movementSpeed > this->tribes[tribeID].movementSpeed) { - this->tribes[tribeID].movementSpeed = _movementSpeed; - } - } - } else { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, this)( - _unitID, tribeID); - } - } - } while (iVar4 < this->tribes[tribeID].size); + this->tribes[tribeID].movementSpeed = DAT_UnitsState::instance.units[targetUnitID].movementSpeed; + int countState7c = 0; + int countShooting = 0; + int countAttacking = 0; + int countMoving = 0; + int countDying = 0; + this->tribes[tribeID].unitType = DAT_UnitsState::instance.units[targetUnitID].unitType; + for (int i = 0; i < this->tribes[tribeID].size;) { + int const unitID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)(tribeID, i); + ++i; + if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL) { + continue; + } + if (MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::isComputerManagedNonPeasant, DAT_UnitsState::ptr)( + unitID) + != FALSE) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, this)(unitID, tribeID); + continue; + } + if (DAT_UnitsState::instance.units[unitID].dying != 0) { + countDying = countDying + 1; + continue; + } + if (DAT_UnitsState::instance.units[unitID].movementRelated != 8) { + countMoving = countMoving + 1; + } + if (DAT_UnitsState::instance.units[unitID].state.generic == Map::Units::States::US_MELEE_ATTACK) { + countAttacking = countAttacking + 1; + } + if (DAT_UnitsState::instance.units[unitID].state.generic == Map::Units::States::US_MELEE_ATTACK_WALL) { + countAttacking = countAttacking + 1; + } + if (DAT_UnitsState::instance.units[unitID].state.generic + == (Map::Units::States::US_DEATH_02 | Map::Units::States::US_STAND_UPUnk + | Map::Units::States::US_RELOAD_WEAPONUnk)) { + countState7c = countState7c + 1; + } + if (DAT_UnitsState::instance.units[unitID].field_0x430 != 0 + && (DAT_UnitsState::instance.units[unitID].state.generic == Map::Units::States::US_RELOAD_WEAPONUnk + || DAT_UnitsState::instance.units[unitID].state.generic == Map::Units::States::US_AIM_WEAPONUnk + || DAT_UnitsState::instance.units[unitID].state.generic + == Map::Units::States::US_FIRE_WEAPONUnk)) { + countShooting = countShooting + 1; + } + UnitTypeShort const unitType = this->tribes[tribeID].unitType; + if (unitType != (UnitType)0 && DAT_UnitsState::instance.units[unitID].unitType != unitType) { + this->tribes[tribeID].unitType = (UnitType)0; + } + short const movementSpeed = DAT_UnitsState::instance.units[unitID].movementSpeed; + if (movementSpeed > this->tribes[tribeID].movementSpeed) { + this->tribes[tribeID].movementSpeed = movementSpeed; + } } - int _tribeSize = (int)this->tribes[tribeID].size; - if (!_tribeSize) { - _percentage = 100; - } else { - _percentage = (_countDying * 100) / _tribeSize; - } - this->tribes[tribeID].percentageDyingUnk = (short)_percentage; - if (!_tribeSize) { - _percentage = 100; - } else { - _percentage = (_countMoving * 100) / _tribeSize; - } - this->tribes[tribeID].percentageMovingUnk = (short)_percentage; - if (!_tribeSize) { - _percentage = 100; - } else { - _percentage = (_countAttackingUnk * 100) / _tribeSize; - } - this->tribes[tribeID].percentageAttackingUnk = (short)_percentage; - if (!_tribeSize) { - _percentage = 100; - } else { - _percentage = (_countRangeUnk * 100) / _tribeSize; - } - this->tribes[tribeID].percentageShootingUnk = (short)_percentage; - /* - The last percentage is the same shape as the four above it, not a guarded - store followed by an unconditional 100 - which is what the import had, and - it pinned the field at 100 whatever the tally was. The original tests the - size once and duplicates the epilogue, storing to 0x2ca and returning in - each arm (0x5272fc and 0x527315, each with its own `ret 4`), so it is - written as an early return rather than through the shared `_percentage`. - */ - if (!_tribeSize) { - this->tribes[tribeID].percentageSomething = 100; - return; - } - this->tribes[tribeID].percentageSomething = (short)((local_8 * 100) / _tribeSize); + int const size = this->tribes[tribeID].size; + this->tribes[tribeID].percentageDyingUnk = (short)(size == 0 ? 100 : countDying * 100 / size); + this->tribes[tribeID].percentageMovingUnk = (short)(size == 0 ? 100 : countMoving * 100 / size); + this->tribes[tribeID].percentageAttackingUnk = (short)(size == 0 ? 100 : countAttacking * 100 / size); + this->tribes[tribeID].percentageShootingUnk = (short)(size == 0 ? 100 : countShooting * 100 / size); + this->tribes[tribeID].percentageSomething = (short)(size == 0 ? 100 : countState7c * 100 / size); } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index f63b88b645..5f07532355 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46969,7 +46969,7 @@ SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending SHC_3BB0A8C1_0x00526F00 | 71.4% | constant 1 is held in ebx in the original; ours puts the tribe offset there -SHC_3BB0A8C1_0x005270F0 | 47.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005270F0 | 58.6% | this held in a register where the original spills it and keeps the loop index in ebp SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs From f652c6875d41ab0c0866013cc6078aff49b377fb Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:54:06 +0100 Subject: [PATCH 175/202] reimplement: moveUnitToBehaviorTarget 33.3 -> 37.1 Rewritten as one dispatch with a goto to the shared targetedBuildingTile store; the early rejections are one combined test. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/moveUnitToBehaviorTarget.cpp | 183 ++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 80 insertions(+), 105 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index 7ce994bab6..16b6ba5a8a 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -31,126 +31,101 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00521280 BOOLEnum TribesState::moveUnitToBehaviorTarget(int unitID, int param_2) { - int iVar4; - int sVar1 = DAT_UnitsState::instance.units[unitID].tribeID; - int bVar3 = 0; + int const tribeID = DAT_UnitsState::instance.units[unitID].tribeID; + BOOL found = FALSE; uint x = 0; - uint uVar6 = 0; - uint local_c = 0; - if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL) { - return FALSE; - } - if (DAT_UnitsState::instance.units[unitID].dying != 0) { - return FALSE; - } - if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0) { - return FALSE; - } - if (DAT_UnitsState::instance.units[unitID].field_0x413 != 0) { - return FALSE; - } - if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[DAT_UnitsState::instance.units[unitID].owner] - != -1) { + uint tile = 0; + uint y = 0; + if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[unitID].dying != 0 + || DAT_UnitsState::instance.units[unitID].usingTeleport != 0 + || DAT_UnitsState::instance.units[unitID].field_0x413 != 0 + || DAT_GameSynchronyState::instance + .currentPlayerFullIDArray[DAT_UnitsState::instance.units[unitID].owner] + != -1) { return FALSE; } if (param_2 == 0x3f2) { - iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( - unitID); - LAB_0052130b: - if (!iVar4) - goto LAB_0052143b; - local_c = DAT_TroopValueState::instance.y; + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( + unitID)) { + goto done; + } + y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x3f5) { - iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, - DAT_TroopValueState::ptr)(unitID); - LAB_00521333: - if (!iVar4) - goto LAB_0052143b; - local_c = DAT_TroopValueState::instance.y; - } else { - if (param_2 == 0x3fb) { - iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, - DAT_TroopValueState::ptr)(unitID); - if (!iVar4) { - iVar4 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)(unitID); - if (!iVar4) - goto LAB_0052143b; - if (this->tribes[sVar1].selectionTargetUnitID == unitID) { - DAT_TribesState::instance.tribes[sVar1].tribeBehaviorType = Map::Units::STBT_0x3f2; - } - local_c = DAT_TroopValueState::instance.y; - DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = 0x3f2; - goto LAB_0052142a; - } - } else { - if (param_2 == 0x3fd) { - iVar4 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)(unitID); - goto LAB_00521333; + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, + DAT_TroopValueState::ptr)(unitID)) { + goto done; + } + y = DAT_TroopValueState::instance.y; + } else if (param_2 == 0x3fb) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, + DAT_TroopValueState::ptr)(unitID)) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( + unitID)) { + goto done; } - if (param_2 == 0x3f6) { - iVar4 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::findEnemyTowersOrGates, DAT_TroopValueState::ptr)(unitID); - } else { - if (param_2 == 0x413) { - iVar4 - = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, - DAT_TroopValueState::ptr)(unitID); - goto LAB_0052130b; - } - if (param_2 == 0x414) { - iVar4 - = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, - DAT_TroopValueState::ptr)(unitID); - goto LAB_00521333; - } - if (param_2 != 0x3f7) - goto LAB_0052143b; - iVar4 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestDiggableMoatPoint, - DAT_TroopValueState::ptr)(unitID); + if (this->tribes[tribeID].selectionTargetUnitID == unitID) { + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_0x3f2; } - if (!iVar4) - goto LAB_0052143b; + DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = 0x3f2; + } + y = DAT_TroopValueState::instance.y; + } else if (param_2 == 0x3fd) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)( + unitID)) { + goto done; + } + y = DAT_TroopValueState::instance.y; + } else if (param_2 == 0x3f6) { + if (!MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyTowersOrGates, DAT_TroopValueState::ptr)(unitID)) { + goto done; } - local_c = DAT_TroopValueState::instance.y; + y = DAT_TroopValueState::instance.y; + } else if (param_2 == 0x413) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID)) { + goto done; + } + y = DAT_TroopValueState::instance.y; + } else if (param_2 == 0x414) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID)) { + goto done; + } + y = DAT_TroopValueState::instance.y; + } else if (param_2 == 0x3f7) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestDiggableMoatPoint, + DAT_TroopValueState::ptr)(unitID)) { + goto done; + } + y = DAT_TroopValueState::instance.y; + } else { + goto done; } - LAB_0052142a: - /* - The flag stays, although its slot is the local our frame has and the - original's does not (`sub esp, 0xc` against our 0x10). Writing the one store - the success and failure paths share - `targetedBuildingTile = 0` - out in - each of them and dropping the flag measures 33.3% -> 23.0%: the shared tail - is what holds the block order together here. - */ - bVar3 = 1; x = DAT_TroopValueState::instance.x; - uVar6 = DAT_TroopValueState::instance.tile; - LAB_0052143b: + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + done: DAT_UnitsState::instance.units[unitID].targetedBuildingTile = 0; - if (!bVar3) { + if (!found) { return FALSE; } - short sVar5 = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[uVar6]; - short sVar2 = DAT_UnitsState::instance.units[unitID].selectionTargetUnitID; - DAT_UnitsState::instance.units[unitID].attackAtTileY = sVar5; - DAT_UnitsState::instance.units[unitID].targetedBuildingTile = uVar6; - sVar5 = (short)uVar6 - (short)DAT_ViewportRenderState::instance.translationMatrix[sVar5].addXgetTile; + short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + short const selectionTargetUnitID = DAT_UnitsState::instance.units[unitID].selectionTargetUnitID; + DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY; + DAT_UnitsState::instance.units[unitID].targetedBuildingTile = tile; + short const tileX + = (short)tile - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[unitID].attackAtTileX = sVar5; - /* - Reached through `this`, which the original keeps in a stack slot - ([esp + 0x18]) across the whole dispatch; only the behaviour-type store in the - 0x3fb arm goes through the global's absolute address. - */ - if (sVar2 == unitID) { - this->tribes[sVar1].targetX = sVar5; - this->tribes[sVar1].targetY = DAT_UnitsState::instance.units[unitID].attackAtTileY; + DAT_UnitsState::instance.units[unitID].attackAtTileX = tileX; + if (selectionTargetUnitID == unitID) { + this->tribes[tribeID].targetX = tileX; + this->tribes[tribeID].targetY = DAT_UnitsState::instance.units[unitID].attackAtTileY; } - iVar4 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(unitID); - if (!iVar4) { + if (!MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(unitID)) { MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( - unitID, x, local_c, 0); + unitID, x, y, 0); } return TRUE; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 5f07532355..e68b716085 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46387,7 +46387,7 @@ SHC_3BB0A8C1_0x00521210 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00521280 | 33.3% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521280 | 37.1% | register allocation: original keeps x, tile and the found flag in ebp, edi, ebx SHC_3BB0A8C1_0x00521500 | 84.6% | Reimplemented, asm differs From 1e5fcff55ef285148316ed0656b2262ac36b23fa Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 00:54:24 +0100 Subject: [PATCH 176/202] TribesState: update stale header comments for computeTribePercentages and moveUnitToBehaviorTarget Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/computeTribePercentages.cpp | 4 ++-- .../Units/TribesState/moveUnitToBehaviorTarget.cpp | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp index 0fe0a5db4c..167a124c3b 100644 --- a/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp +++ b/src/OpenSHC/Map/Units/TribesState/computeTribePercentages.cpp @@ -19,8 +19,8 @@ namespace Map { using WindowsHelper::Enums::BOOLEnum; /* - Its three `short` locals stay narrow: widening all three costs 0.9 points - (42.8% -> 41.9%). + What is left is register allocation: the original keeps the loop index in ebp + and spills `this`, where ours keeps `this` in ebp and spills the index. */ // FUNCTION: STRONGHOLDCRUSADER 0x005270F0 void TribesState::computeTribePercentages(int tribeID) diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index 16b6ba5a8a..872da03637 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -22,11 +22,12 @@ namespace Map { using Map::Units::States::UnitState; /* - The gotos stay here, against the style list, because the original really does - duplicate these tails and three goto-free shapes all measure worse: merging the - two `local_c = y` tails into one `if (iVar4)` block gives quick_diff 0.2730 at 137 - instructions against the original's 156, duplicating the success tail into all - eight arms gives 0.2951 at 149, and this form gives 0.4092 at 147. + The one goto is the original's own shared tail: every arm that finds nothing + jumps to the `targetedBuildingTile = 0` store that the success path also runs. + What is left is register allocation - the original keeps x, tile and the found + flag in ebp, edi and ebx and reloads unitID from the stack, where ours keeps + unitID in ebx. The declaration order of the locals does not change it (all 24 + orders measured the same). */ // FUNCTION: STRONGHOLDCRUSADER 0x00521280 BOOLEnum TribesState::moveUnitToBehaviorTarget(int unitID, int param_2) From 615e686cff6ea8f3fa68b54b882427d81a80a746 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 08:22:01 +0100 Subject: [PATCH 177/202] reimplement: playUnitMoveSpeech 82.8 -> 100.0 Fixes a behaviour bug: with no stone the original only plays the out-of-ammunition speech and returns (tail call), the import also played the unit speech. The selected count goes to a block-scoped local, because an address-taken parameter blocks MSVC's tail call. Ballista arm placed before the ram arm, as in the original. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/playUnitMoveSpeech.cpp | 40 ++++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp index 131a455018..3b36132aaf 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp @@ -15,12 +15,9 @@ namespace Map { using Audio::SFX::SpeechEffectID; /* - The original tail-calls playSpeechSFX with 0x2e - it writes the argument into the - incoming slot, pops and jumps - so one of the two call sites here ends the - function. Adding the `return` to the stone-ammunition site costs 6.2 points - (82.8% -> 76.6%) and dropping the `param_1 = 0x2e` write with it costs 11.0 - (82.8% -> 71.8%), so it is the other site, or the block layout costs more than - the matched jump is worth. Needs the two sites told apart before trying again. + When a catapult or trebuchet has no stone, the original plays only the + out-of-ammunition speech and returns: it tail-calls playSpeechSFX and never + reaches the unit speech. The import played both. */ // FUNCTION: STRONGHOLDCRUSADER 0x00521EF0 void TribesState::playUnitMoveSpeech(undefined4 param_1) @@ -30,10 +27,17 @@ namespace Map { _unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); /* - bug: the second argument ("stack") does not want to be renamed, but is sfxToPlay again. + The selected count is written into a block-scoped local that nothing reads + afterwards. MSVC packs it into the parameter's home, and because its scope has + ended, the two out-of-ammunition plays can be tail calls (`mov [esp + 4], 0x2e; + jmp playSpeechSFX`). Passing `¶m_1` keeps an address-taken variable alive + and blocks the tail call. */ - _unitTypeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( - (param_1), (int*)(¶m_1)); + { + int selectedCount; + _unitTypeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( + param_1, &selectedCount); + } switch (_unitTypeID) { case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_XBOW: @@ -54,9 +58,9 @@ namespace Map { .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { if (DAT_UnitsState::instance.units[_unitID].stoneAmmunition < 1) { - param_1 = 0x2e; MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( Audio::SFX::SEID_RESOURCE_NEED25); + return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x10); } @@ -69,9 +73,9 @@ namespace Map { .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { if (DAT_UnitsState::instance.units[_unitID].stoneAmmunition < 1) { - param_1 = 0x2e; MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSpeechSFX, DAT_SFXState::ptr)( Audio::SFX::SEID_RESOURCE_NEED25); + return; } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x11); } @@ -86,25 +90,25 @@ namespace Map { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0xf); } break; - case Map::Units::UT_S_BATTERINGRAM: + case Map::Units::UT_S_BALLISTA: + case Map::Units::UT_S_FBALLISTA: /* - Battering ram + Ballista and fire ballista */ if (DAT_UnitsState::instance.units[_unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x13); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x12); } break; - case Map::Units::UT_S_BALLISTA: - case Map::Units::UT_S_FBALLISTA: + case Map::Units::UT_S_BATTERINGRAM: /* - Ballista and fire ballista + Battering ram */ if (DAT_UnitsState::instance.units[_unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x12); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(_unitTypeID, 0x13); } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index e68b716085..387d80d90d 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46635,7 +46635,7 @@ SHC_3BB0A8C1_0x00521EA6 | 0.0% | Pending SHC_3BB0A8C1_0x00521EB0 | 94.1% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00521EF0 | 82.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521EF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522030 | 0.0% | Pending From bbfb3167898b3075e5950e7928bd291e223dc0c3 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 08:28:30 +0100 Subject: [PATCH 178/202] reimplement: playUnitSelectionSound 65.4 -> 78.0 Fixes the arm labels from the original's byte table (knight, engineer, catapult were wrong). The siege and engineer arms sit past the end of the scope of the address-taken selected count, so MSVC emits their tail calls as the original does. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/playUnitSelectionSound.cpp | 244 +++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 88 insertions(+), 158 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 57902e6ee6..0bfae28e45 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -15,199 +15,129 @@ namespace Map { using Map::Units::UnitType; /* - Rebuilt from Ghidra's disassembly of 0x5217e0 rather than its decompilation: the - jump table at 0x521a48 has six arms and the byte table at 0x521a60 maps - unitType - 0x1b onto them. The speech calls in the engineer and siege arms are - tail calls (`mov [esp + 4], ` then `jmp`), which is what the decompiler - printed as an assignment to `param_1` in front of each one. + Arms from the byte table at 0x521a60 (indexed by unitType - 0x1b) and the jump + table at 0x521a48: swordsman 0x521819, knight 0x5218aa, engineer 0x521a11, + catapult / tower / battering ram 0x521983, horse archer 0x521915, and every other + type the bare return at 0x521876. The import had the knight's arm on the ladderman, + the knight sharing the swordsman's arm, the miner on the engineer's arm and the + mangonel in place of the catapult. */ // FUNCTION: STRONGHOLDCRUSADER 0x005217E0 void TribesState::playUnitSelectionSound(int param_1) { - int const iVar3 + int const unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); - UnitType const UVar4 - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(param_1, ¶m_1); - /* - Arm order and the case labels both come from the dispatch at 0x521a48: six - dword entries and a 48-entry byte index table at 0x521a60, indexed by - `unitType - 0x1b`. Three of the labels the import had were wrong. - - 0x1b and 0x1c - swordsman and knight - share one arm, 0x521819. The - 0x56/0x59 arm at 0x5218aa belongs to 0x1d, the ladderman, not the knight. - The siege arm at 0x521983 covers 0x29, 0x3a and 0x3b - mangonel, tower and - battering ram - so the first of the three is UT_S_MANGONEL, not - UT_S_CATAPULT (0x27), which falls to the default and plays nothing. - - The last one is odd rather than wrong: the arm at 0x521a11 is reached only - by 0x1f, the miner, yet its body compares the unit's own type against 0x1e, - the engineer (`cmp word ptr [esi + 0x13885da], 0x1e`). The majority type - that selects the arm and the type of the unit the sound plays on are two - different units, so this is reachable; but labelling it UT_E_ENGINEER, as - the import did, gave the speech to engineers and nothing to miners. - - Physical order by address is 0x521819, the default `ret 4` at 0x521876, - then 0x5218aa, 0x521915, 0x521983, 0x521a11 - so the archer arm sits - between the ladderman and the siege engines. - */ - switch (UVar4) { - case Map::Units::UT_E_SWORD: - case Map::Units::UT_E_KNIGHT: + { /* - Three separate blocks at 0x521819, each with its own `ret 4`, and a - selection below three that is neither one nor two plays nothing. + The selected count is address-taken, and MSVC does not tail-call while an + address-taken local is in scope. The siege and engineer arms end in tail + calls in the original, so they sit past the end of this block and the + switch reaches them through a goto. */ - if (param_1 == 1) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - 0xad); + int selectedCount; + switch (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( + param_1, &selectedCount)) { + case Map::Units::UT_E_SWORD: + if (selectedCount == 1) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xad); + return; + } + if (selectedCount == 2) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xae); + return; + } + if (selectedCount >= 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xaf); + } return; - } - if (param_1 == 2) { + case Map::Units::UT_E_KNIGHT: { + int const count = selectedCount; + if (count == 1) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0x56); + } else { + if (count == 2 || count == 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, + DAT_SFXState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, 0x57); + } + if (count >= 4) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, + DAT_SFXState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, 0x58); + } + } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - 0xae); + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0x59); return; } - if (param_1 < 3) { - return; - } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0xaf); - return; - case Map::Units::UT_E_LADDER: { - /* - The arm at 0x5218aa: one shared play of the size-dependent effect, then - 0x59. A selection of two or three gets 0x57 as well, and one of four or - more gets 0x58 instead of 0x56. - */ - /* - The play is written out in each arm with its constant. The original - pushes them directly - `cmp edi, 1; jne 0x4; push 0x56; jmp 0x35` for - the first and `push 0x58` for the four-or-more case - and shares the - coordinate pushes and the call after them, which is MSVC tail-merging - two call sites. An `_sfx` local and one shared call makes it - materialise the value into a register first instead. - */ - if (param_1 == 1) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - 0x56); - } else { - if (param_1 == 2 || param_1 == 3) { + case Map::Units::UT_A_HARCHER: { + short* const unitY = &DAT_UnitsState::instance.units[unitID].y; + short* const unitX = &DAT_UnitsState::instance.units[unitID].x; + if (selectedCount == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0x57); - } - /* - This one stays, against the asm. The original has `cmp edi, 4; - jl 0x24`, jumping away for a smaller selection, so `push 0x58` is - its fall-through and the lone 0x59 play sits in the far block - - ours is the other way round. Rewriting it as - `if (param_1 >= 4) { _sfx = 0x58; } else { ...; return; }` measures - 62.1% -> 56.0%. Retried after the play was written out in each arm, - in case the surrounding blocks had changed enough to matter: still a - loss, 65.4% -> 60.2%. Second and third losses for the inverted-arm - fix after giveTribeMoveInstruction, and all of them are in functions - whose remaining gap is dominated by where the shared epilogue lands. - - What is left here is how far MSVC tail-merges. The original shares - one coordinate-read-and-call block between the 0x56 and 0x58 arms, - duplicating only the `push`, and its 0x59 plays jump back to a second - shared block. Ours pushes the constants per arm correctly but keeps - its own copy of the coordinate reads, which is the two extra reads of - units[].x and units[].y the diff shows (orig 4 and 5, ours 6 and 7). - - Tried making those two arms adjacent so MSVC would have the chance: - the 0x57 play lifted out ahead as `if (param_1 == 2 || param_1 == 3) - { play(0x57); play(0x59); return; }`, leaving `if (param_1 == 1) - play(0x56); else play(0x58);` as the whole of its if/else. Measured - 65.4% -> 59.9%. MSVC merged nothing extra and the rearranged tests - cost more than the duplicated reads do, so the shape here stays as - the original's test order has it. - */ - if (param_1 < 4) { + *unitX, *unitY, 0xea); + } else if (selectedCount <= 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, - (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); - return; + *unitX, *unitY, 0xeb); + } else { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + *unitX, *unitY, 0xec); } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - 0x58); - } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), 0x59); - return; - } - case Map::Units::UT_A_HARCHER: { - /* - The arm at 0x521915 keeps a pointer to each coordinate (`lea edi, [esi + - 0x1388612]`, `lea esi, [esi + 0x1388610]`) and reads through them four - times rather than re-indexing the unit. - */ - short* const _unitY = &DAT_UnitsState::instance.units[iVar3].y; - short* const _unitX = &DAT_UnitsState::instance.units[iVar3].x; - int sfxOffsetInArray; - if (param_1 == 1) { - sfxOffsetInArray = 0xea; - } else if (param_1 <= 4) { - sfxOffsetInArray = 0xeb; - } else { - sfxOffsetInArray = 0xec; + *unitX, *unitY, 0x59); + return; } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)*_unitX, (int)(*_unitY), sfxOffsetInArray); - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)*_unitX, (int)(*_unitY), 0x59); - } - case Map::Units::UT_S_MANGONEL: - case Map::Units::UT_S_TOWER: - case Map::Units::UT_S_BATTERINGRAM: { - int const _requiredEngineers = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(iVar3); - if (_requiredEngineers <= 0) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[iVar3].x, (int)(DAT_UnitsState::instance.units[iVar3].y), - 0x68); + case Map::Units::UT_S_CATAPULT: + case Map::Units::UT_S_TOWER: + case Map::Units::UT_S_BATTERINGRAM: + goto siegeEngine; + case Map::Units::UT_E_ENGINEER: + goto engineer; + default: return; } - if (DAT_UnitsState::instance.units[iVar3] + } + siegeEngine: { + int const requiredEngineers = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getRemainingRequiredEngineers, DAT_UnitsState::ptr)(unitID); + if (requiredEngineers > 0) { + if (DAT_UnitsState::instance.units[unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 == 0 - || 3 < _requiredEngineers) { + || requiredEngineers > 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xe); return; } - if (_requiredEngineers == 1) { + if (requiredEngineers == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xb); return; } - if (_requiredEngineers == 2) { + if (requiredEngineers == 2) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xc); return; } - /* - The last arm at 0x5219d9 tests for three and returns without a speech - otherwise, which the earlier count tests already make unreachable. - */ - if (_requiredEngineers == 3) { + if (requiredEngineers == 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0xd); } return; } - case Map::Units::UT_MINER: - if (DAT_UnitsState::instance.units[iVar3].unitType != Map::Units::UT_E_ENGINEER) { - return; - } - if (DAT_UnitsState::instance.units[iVar3].resourceToDeposit == 0) { - return; - } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0x24); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0x68); + return; + } + engineer: + if (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_ENGINEER) { return; } + if (DAT_UnitsState::instance.units[unitID].resourceToDeposit == 0) { + return; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(0x24); } - } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 387d80d90d..822f18fcf5 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46395,7 +46395,7 @@ SHC_3BB0A8C1_0x00521690 | 0.0% | Pending SHC_3BB0A8C1_0x00521720 | 98.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 65.4% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005217E0 | 78.0% | tail merging: the original keeps the 0xae and 0xaf plays as separate blocks (different registers); ours merges them SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending From ff919c317093d3dd8a05bd1fdc68a9d0d1a84eb9 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 08:32:26 +0100 Subject: [PATCH 179/202] reimplement: playUnitCombatSpeechForTarget 41.2 -> 78.2 Rewritten from the original's arm layout: each arm plays its own constants and returns, the tribe size is an int read once, and the 0x1e/0x19 speech is two calls rather than a computed id. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../playUnitCombatSpeechForTarget.cpp | 331 ++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 111 insertions(+), 222 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index a8f73e6e32..4cd8ee2865 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -21,283 +21,172 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00521A90 void TribesState::playUnitCombatSpeechForTarget(int selectionID, int unitID) { - UnitTypeShort UVar2; - int _otherUnitID; - UnitTypeInt _ptrptrCurrentSelectionID; - int _yDifference; - int _otherX; - int _xDifference; - int _otherY; - /* - This is the selected-unit count the majority lookup writes back, not a - pointer. The decompiler typed it `TribesState*` because the import assigned - `this` to it before the call - which the original never does: it passes the - slot's address with `lea eax, [esp + 0x10]; push eax` and stores nothing into - it first. That assignment is what made us spill `this` - (`mov dword ptr [esp + 0x14], edi`) where the original just keeps it in ebx. - */ - int _selectedCount; - uint _actionID; - int _distance; - int _selectedCount_2; - _otherUnitID + int selectedCount; + int const otherUnitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); - _ptrptrCurrentSelectionID = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(selectionID, &_selectedCount); - _selectedCount_2 = _selectedCount; - /* - An `int`, not a `short`. The original reads the size once, - `movsx ebp, word ptr [edi + 0x1667fd4]` at 0x521abc, and keeps the - sign-extended value in ebp for the whole function - all five `< 0xf` tests - compare against it. A `short` local gets a `movzx` here instead. - giveTribeAnInstruction reads the same field the same way, `movsx esi, word - ptr [ebp + 0x5c]`, and wanted `int` for the same reason. - - That is as far as it generalises, and it is worth only +0.1 even here. I - took the two confirmed cases as a rule and changed the `.size` local to - `int` in the nine other functions in this namespace that read it into a - `short`. One build measured all nine: - addUnitsToTribeAndComputeMovementSpeed -21.2, processDeerMoving -1.4, - createAnimal -0.4, updateTribeUnitAssignments +0.1, and five flat. `short` - is right in almost all of them. Read the load at the site; do not carry - this across functions. - */ - int const _tribeSize = DAT_TribesState::instance.tribes[selectionID].size; - /* - Arm order comes from the dispatch at 0x521e50: eight dword entries with a - 55-entry byte index table at 0x521e70, indexed by `unitType - 0x16`. The - labels the import had are all correct; the order was not. By address the - arms run - - 0x521ae3 the archer group, 0x521c2d knight, 0x521cd3 swordsman, - 0x521d5b spearman, 0x521d7e pike/mace/monk, 0x521dc8 Arabian swordsman, - 0x521df7 the default, and 0x521e48 engineer - - so knight and swordsman come before the spearman, and the engineer arm is - last of all - past the default block. The import had the engineer arm - sitting between the knight's body and the Arabian swordsman, where the - knight fell through into its bare `return`; moved to the end it needs the - knight's own `return` made explicit, exactly as UIT_LIGHT_PITCH did in - giveTribeAnInstruction once it stopped being the final arm. - */ - switch (_ptrptrCurrentSelectionID) { + UnitTypeInt const unitType = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)(selectionID, &selectedCount); + int const tribeSize = DAT_TribesState::instance.tribes[selectionID].size; + switch (unitType) { case Map::Units::UT_E_ARCHER: case Map::Units::UT_E_XBOW: case Map::Units::UT_A_ARCHER: case Map::Units::UT_A_SLINGER: case Map::Units::UT_A_HARCHER: - case Map::Units::UT_A_FIRETHROWER: - if (_ptrptrCurrentSelectionID == Map::Units::UT_E_ARCHER) { - _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + case Map::Units::UT_A_FIRETHROWER: { + int range; + if (unitType == Map::Units::UT_E_ARCHER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; - } else if (_ptrptrCurrentSelectionID == Map::Units::UT_E_XBOW) { - _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[7] + } else if (unitType == Map::Units::UT_E_XBOW) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[7] * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[7]; - } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_ARCHER) { - _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + } else if (unitType == Map::Units::UT_A_ARCHER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; - } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_SLINGER) { - _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x21] + } else if (unitType == Map::Units::UT_A_SLINGER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x21] * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x21]; - } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_HARCHER) { - _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + } else if (unitType == Map::Units::UT_A_HARCHER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; - } else if (_ptrptrCurrentSelectionID == Map::Units::UT_A_FIRETHROWER) { - /* - The firethrower range is tested before the fallback, not after it: - the chain at 0x521ae3 is six `cmp`/`jnz` pairs with - `mov ecx, [esp + 0x1c]` only on the fall-through. - */ - _distance = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22] + } else if (unitType == Map::Units::UT_A_FIRETHROWER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22] * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[0x22]; } else { - _distance = unitID; + range = unitID; } - /* - The original reads all four coordinates before either subtraction - - `movsx edx, [esi + 0x610]`, `movsx ebx, [eax + 0x610]`, - `movsx edi, [esi + 0x612]`, `movsx ebp, [eax + 0x612]`, then `sub edx, - ebx` and `sub eax, ebp` - where ours interleaves each pair with its - subtraction. Writing the four reads out into locals of their own first - measures 40.6% -> 40.6%: MSVC schedules these loads where it wants them, - and the same spelling is flat in updateRabbitTribeBehavior and - processDeerMoving too. - */ - _xDifference = (int)DAT_UnitsState::instance.units[_otherUnitID].x - - (int)DAT_UnitsState::instance.units[unitID].x; - _yDifference = (int)DAT_UnitsState::instance.units[_otherUnitID].y - - (int)DAT_UnitsState::instance.units[unitID].y; - if (_distance < _xDifference * _xDifference + _yDifference * _yDifference) { - _actionID = 0x1e; + int const dx = DAT_UnitsState::instance.units[otherUnitID].x - DAT_UnitsState::instance.units[unitID].x; + int const dy = DAT_UnitsState::instance.units[otherUnitID].y - DAT_UnitsState::instance.units[unitID].y; + if (dx * dx + dy * dy > range) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 0x1e); } else { - _actionID = 0x19; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 0x19); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, _actionID); - /* - The import left this guard with an empty body. The original jumps to the - common epilogue (`cmp dword ptr [esp + 0x18], 0x4a; jnz 0x521e48`), so - only a horse archer reaches the two plays below. - */ - if (_ptrptrCurrentSelectionID != Map::Units::UT_A_HARCHER) { + if (unitType != Map::Units::UT_A_HARCHER) { return; } - /* - The play is written out in each arm with its constant, as in the sibling - playUnitSelectionSound. The original pushes 0xea, 0xeb and 0xec directly - and shares the coordinate pushes and the call, which is MSVC tail-merging - three call sites; a named offset and one shared call makes it materialise - the value into a register first. - */ - if (_selectedCount == 1) { - _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; - _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; + if (selectedCount == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - _otherX, _otherY, 0xea); - /* - `<= 4` and `>= 4` below, which are the `cmp .., 4` the original compares - against with a `jg` and a `jl`; `< 5` and `3 <` give `cmp 5`/`cmp 3` and - the other arm of each pair. - */ - } else if (_selectedCount <= 4) { - _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; - _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0xea); + } else if (selectedCount <= 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - _otherX, _otherY, 0xeb); + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0xeb); } else { - _otherY = (int)DAT_UnitsState::instance.units[_otherUnitID].y; - _otherX = (int)DAT_UnitsState::instance.units[_otherUnitID].x; MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - _otherX, _otherY, 0xec); + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0xec); } - /* - A second play, of 0x59, with the coordinates read again (0x521c0b, - 0x521c12) - which is what the decompiler turned into an assignment of - 0x59 to one of the coordinate locals - and then `ret 8`. - */ MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)DAT_UnitsState::instance.units[_otherUnitID].y, 0x59); + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, 0x59); return; - case Map::Units::UT_E_KNIGHT: - /* - The same two call sites written out, which is what the decompiler's - `_otherX = 0x56` / `goto` back into the first one was: the original - pushes 0x56 and 0x58 directly and shares the coordinate pushes and the - call. Every path still reaches the 0x59 play below, as it did through - the goto. - */ - if (_selectedCount == 1) { + } + case Map::Units::UT_E_KNIGHT: { + int const count = selectedCount; + if (count == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x56); + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0x56); } else { - if ((_selectedCount == 2) || (_selectedCount == 3)) { + if (count == 2 || count == 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x57); + DAT_UnitsState::instance.units[otherUnitID].x, + DAT_UnitsState::instance.units[otherUnitID].y, 0x57); } - if (_selectedCount_2 >= 4) { + if (count >= 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x58); + DAT_UnitsState::instance.units[otherUnitID].x, + DAT_UnitsState::instance.units[otherUnitID].y, 0x58); } } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x59); + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, 0x59); MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (_tribeSize < 0xf) { - return; - } - _otherX = 0xfa; - break; - default: - UVar2 = DAT_UnitsState::instance.units[unitID].unitType; - /* - The two speeches are exclusive. Ghidra's disassembly of the table arm at - 0x521df7 is a `jz` chain over the five target types into the 0x11 block, - with the 0x15 block inline and its own `ret 8`; the decompilation played - 0x15 conditionally and then 0x11 unconditionally. - */ - if (UVar2 != Map::Units::UT_E_KNIGHT && UVar2 != Map::Units::UT_E_SWORD - && UVar2 != Map::Units::UT_E_MACE && UVar2 != Map::Units::UT_E_PIKE - && UVar2 != Map::Units::UT_LORD) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - return; + (UnitType)unitType, 0x15); + if (tribeSize >= 0xf) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0xfa); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x11); return; + } case Map::Units::UT_E_SWORD: - if (_selectedCount == 1) { - _otherX = 0xad; - } else if (_selectedCount == 2) { - _otherX = 0xae; - } else { - if (_selectedCount < 3) - goto LAB_00521d1b; - _otherX = 0xaf; + if (selectedCount == 1) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0xad); + } else if (selectedCount == 2) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0xae); + } else if (selectedCount >= 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0xaf); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); - LAB_00521d1b: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (_tribeSize < 0xf) { - return; + (UnitType)unitType, 0x15); + if (tribeSize >= 0xf) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0x14); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); return; case Map::Units::UT_E_SPEAR: MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, 0x15); - if (_tribeSize < 0xf) { - return; + (UnitType)unitType, 0x15); + if (tribeSize >= 0xf) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0x14); } - _otherX = 0x14; - break; + return; case Map::Units::UT_E_PIKE: case Map::Units::UT_E_MACE: - case Map::Units::UT_E_MONK: - UVar2 = DAT_UnitsState::instance.units[unitID].unitType; - if (UVar2 == Map::Units::UT_E_KNIGHT || UVar2 == Map::Units::UT_E_SWORD - || UVar2 == Map::Units::UT_LORD) { - _otherX = 0x11; + case Map::Units::UT_E_MONK: { + UnitTypeShort const targetType = DAT_UnitsState::instance.units[unitID].unitType; + if (targetType == Map::Units::UT_E_KNIGHT || targetType == Map::Units::UT_E_SWORD + || targetType == Map::Units::UT_LORD) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 0x11); } else { - _otherX = 0x15; + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 0x15); } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( - (Map::Units::UnitType)_ptrptrCurrentSelectionID, _otherX); - if (_tribeSize < 0xf) { - return; + if (tribeSize >= 0xf) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0x14); } - _otherX = 0x14; - break; + return; + } case Map::Units::UT_A_SWORDSMAN: - if (_tribeSize < 0xf) { + if (tribeSize >= 0xf) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 0x14); + } + return; + default: { + UnitTypeShort const targetType = DAT_UnitsState::instance.units[unitID].unitType; + if (targetType != Map::Units::UT_E_KNIGHT && targetType != Map::Units::UT_E_SWORD + && targetType != Map::Units::UT_E_MACE && targetType != Map::Units::UT_E_PIKE + && targetType != Map::Units::UT_LORD) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 0x15); return; } - /* - The table arm at 0x521dc8 ends in `ret 8`. Without the return this fell - out of the switch into the shared play below with `_otherX` never - assigned on this path. - */ - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), 0x14); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 0x11); return; + } case Map::Units::UT_E_ENGINEER: return; } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - (int)DAT_UnitsState::instance.units[_otherUnitID].x, - (int)(DAT_UnitsState::instance.units[_otherUnitID].y), _otherX); } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 822f18fcf5..5c5ee51e15 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 41.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521A90 | 78.2% | register allocation in the archer arm: the original spills the unit type to the parameter slot and uses ebx/ebp for the coordinates SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending From c9b10abb34d78e29d68978a8ed8cd578ed0f448f Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:16:09 +0100 Subject: [PATCH 180/202] reimplement: moveUnitToBehaviorTarget 37.1 -> 100.0 Each behaviour arm now stores y, x, tile and the found flag itself, as the original does before the compiler merges the x/tile/found stores into one shared tail. The extra stores are what give x, tile and found their registers ahead of unitID. The tile-to-x/y tail now writes attackAtTileX directly and reads it back for the tribe's target, and tribeID is read after the four locals are declared. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/moveUnitToBehaviorTarget.cpp | 107 ++++++++++-------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index 872da03637..fc2f366a67 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -22,21 +22,21 @@ namespace Map { using Map::Units::States::UnitState; /* - The one goto is the original's own shared tail: every arm that finds nothing - jumps to the `targetedBuildingTile = 0` store that the success path also runs. - What is left is register allocation - the original keeps x, tile and the found - flag in ebp, edi and ebx and reloads unitID from the stack, where ours keeps - unitID in ebx. The declaration order of the locals does not change it (all 24 - orders measured the same). + Each arm stores y, x, tile and the found flag itself. The compiler merges the + x, tile and found stores into one shared tail, which is what the original + shows. Those four stores per arm are also what puts x, tile and found in + registers ahead of unitID. When the arms jumped to one shared assignment + instead, unitID took ebx and the function measured 37.1%. tribeID is read after + the four locals are declared so that its stack store follows y's. */ // FUNCTION: STRONGHOLDCRUSADER 0x00521280 BOOLEnum TribesState::moveUnitToBehaviorTarget(int unitID, int param_2) { - int const tribeID = DAT_UnitsState::instance.units[unitID].tribeID; - BOOL found = FALSE; + uint y = 0; uint x = 0; uint tile = 0; - uint y = 0; + BOOL found = FALSE; + int const tribeID = DAT_UnitsState::instance.units[unitID].tribeID; if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL || DAT_UnitsState::instance.units[unitID].dying != 0 || DAT_UnitsState::instance.units[unitID].usingTeleport != 0 @@ -47,81 +47,92 @@ namespace Map { return FALSE; } if (param_2 == 0x3f2) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( unitID)) { - goto done; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x3f5) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, DAT_TroopValueState::ptr)(unitID)) { - goto done; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x3fb) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, DAT_TroopValueState::ptr)(unitID)) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( - unitID)) { - goto done; - } + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } else if (MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)(unitID)) { if (this->tribes[tribeID].selectionTargetUnitID == unitID) { DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_0x3f2; } DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = 0x3f2; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x3fd) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)( + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)( unitID)) { - goto done; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x3f6) { - if (!MACRO_CALL_MEMBER( + if (MACRO_CALL_MEMBER( Map::Units::TroopValueState_Func::findEnemyTowersOrGates, DAT_TroopValueState::ptr)(unitID)) { - goto done; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x413) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, DAT_TroopValueState::ptr)(unitID)) { - goto done; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x414) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, DAT_TroopValueState::ptr)(unitID)) { - goto done; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; } else if (param_2 == 0x3f7) { - if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestDiggableMoatPoint, + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestDiggableMoatPoint, DAT_TroopValueState::ptr)(unitID)) { - goto done; + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; } - y = DAT_TroopValueState::instance.y; - } else { - goto done; } - x = DAT_TroopValueState::instance.x; - tile = DAT_TroopValueState::instance.tile; - found = TRUE; - done: DAT_UnitsState::instance.units[unitID].targetedBuildingTile = 0; if (!found) { return FALSE; } short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; - short const selectionTargetUnitID = DAT_UnitsState::instance.units[unitID].selectionTargetUnitID; DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY; DAT_UnitsState::instance.units[unitID].targetedBuildingTile = tile; - short const tileX - = (short)tile - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[unitID].attackAtTileX = tileX; - if (selectionTargetUnitID == unitID) { - this->tribes[tribeID].targetX = tileX; + DAT_UnitsState::instance.units[unitID].attackAtTileX + = (short)tile - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; + if (DAT_UnitsState::instance.units[unitID].selectionTargetUnitID == unitID) { + this->tribes[tribeID].targetX = DAT_UnitsState::instance.units[unitID].attackAtTileX; this->tribes[tribeID].targetY = DAT_UnitsState::instance.units[unitID].attackAtTileY; } if (!MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(unitID)) { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 5c5ee51e15..b6a1dd75a4 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46387,7 +46387,7 @@ SHC_3BB0A8C1_0x00521210 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00521280 | 37.1% | register allocation: original keeps x, tile and the found flag in ebp, edi, ebx +SHC_3BB0A8C1_0x00521280 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521500 | 84.6% | Reimplemented, asm differs From 906f225887e70734d0a1ab1cd5855375d431e08a Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:16:10 +0100 Subject: [PATCH 181/202] reimplement: upgradeTribeArrayLayoutForMapVersion 77.5 -> 100.0 Take the new record's address once as a Tribe pointer before the first memmove. Both memmove destinations and the cleared half of the bitmask are then addressed from it, which gives the original's register assignment. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../upgradeTribeArrayLayoutForMapVersion.cpp | 10 +++++----- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 0743bfc8e3..9fed500be6 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -18,13 +18,13 @@ namespace Map { if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 0xa9) { for (int tribeIndex = 1249; tribeIndex >= 0; --tribeIndex) { char* const oldRecord = (char*)this->tribes + tribeIndex * 0x26c; - if ((void*)&this->tribes[tribeIndex] != (void*)oldRecord) { - MACRO_CALL(OpenSHC::OS_Func::_memmove)(&this->tribes[tribeIndex], oldRecord, 0x26c); + Tribe* const tribe = &this->tribes[tribeIndex]; + if ((void*)tribe != (void*)oldRecord) { + MACRO_CALL(OpenSHC::OS_Func::_memmove)(tribe, oldRecord, 0x26c); } - MACRO_CALL(OpenSHC::OS_Func::_memmove)( - &this->tribes[tribeIndex].field_0x1c8, oldRecord + 0x100, 0x16c); + MACRO_CALL(OpenSHC::OS_Func::_memmove)(&tribe->field_0x1c8, oldRecord + 0x100, 0x16c); for (int k = 100; k < 200; ++k) { - this->tribes[tribeIndex].unitSelectionBitMasked[k] = 0; + tribe->unitSelectionBitMasked[k] = 0; } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b6a1dd75a4..469bd3597c 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46871,7 +46871,7 @@ SHC_3BB0A8C1_0x00523E30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523EB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523EF0 | 77.5% | register assignment of src/dst swapped (ebx/esi); rep stosd placement +SHC_3BB0A8C1_0x00523EF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523F70 | 100.0% | Reimplemented From 0bdda4fb07a75c38ccd2a0a9f6d32f4bc263a09a Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:25:58 +0100 Subject: [PATCH 182/202] reimplement: isTribeUnitBlockedByOtherUnit 80.7 -> 91.2 Skip units through a goto to the top of the loop body instead of a do-while nested in a while(true). The size test stays at the loop head as in the original, and the unit offset is computed once for the logicalState and dying tests. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../isTribeUnitBlockedByOtherUnit.cpp | 63 ++++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp index 18f5c9bff6..a68829bc89 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp @@ -17,49 +17,52 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00524B30 BOOLEnum TribesState::isTribeUnitBlockedByOtherUnit(int param_1) { - int iVar2; + int unitSelectionIndex = 0; + int const size = this->tribes[param_1].size; /* - The unit is re-indexed at every test, and that is cheaper than the - alternative. The original keeps its address in a register across the body - - `cmp word ptr [ecx + ..]` for the dying test with no multiply in front of - it - where ours emits `mov edx, eax; imul edx, edx, 0x490` again inside the - loop. Hoisting a `Unit*` for all eight accesses removes those, and measures - 80.7% -> 74.3%: the pointer needs a home across the call in the loop and - costs more than the multiply it saves. + The skip is a goto to the top of the loop body, not a `continue`. A + `continue` (or a `while (index < size)`) lets MSVC rotate the loop: the size + test is copied in front of it as a guard and moved to the bottom. The + original keeps the one test at the top, as this form does (89.5% -> 98.3% in + quick_diff). One difference is left: ours computes `unitID * 0x490` again + after the dying test, and the original computes it once. Writing the two + tests as separate gotos, as a nested do-while, or as an else-if chain does + not remove the second multiply. */ - int unitSelectionIndex = 0; - int sVar1 = this->tribes[param_1].size; - while (true) { - do { - if (unitSelectionIndex >= sVar1) { - return FALSE; - } - iVar2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - } while ((DAT_UnitsState::instance.units[iVar2].logicalState != Map::Units::ULS_NORMAL) - || (DAT_UnitsState::instance.units[iVar2].dying != 0)); - if (DAT_UnitsState::instance.units[iVar2].usingTeleport != 0) { + for (;;) { + nextUnit: + if (unitSelectionIndex >= size) { + return FALSE; + } + int const unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + param_1, unitSelectionIndex); + unitSelectionIndex = unitSelectionIndex + 1; + if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[unitID].dying != 0) { + goto nextUnit; + } + if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0) { + return FALSE; + } + if (DAT_UnitsState::instance.units[unitID].field_0x413 != 0) { return FALSE; } - if (DAT_UnitsState::instance.units[iVar2].field_0x413 != 0) { + if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging == 2) { return FALSE; } - if (DAT_UnitsState::instance.units[iVar2].tunnelerFinishedDigging == 2) - break; - if (DAT_UnitsState::instance.units[iVar2].state.generic == Map::Units::States::US_MELEE_ATTACK) { + if (DAT_UnitsState::instance.units[unitID].state.generic == Map::Units::States::US_MELEE_ATTACK) { return FALSE; } - if (DAT_UnitsState::instance.units[iVar2].laddermanIsInPosition != '\0') { + if (DAT_UnitsState::instance.units[unitID].laddermanIsInPosition != '\0') { return FALSE; } - int iVar3 - = (int)(short)DAT_TileMapState::instance.UnitLayer[DAT_UnitsState::instance.units[iVar2].tile]; - if (iVar3 && iVar2 != iVar3 && DAT_UnitsState::instance.units[iVar3].tunnelerFinishedDigging != 2) { + int const otherUnitID + = (int)(short)DAT_TileMapState::instance.UnitLayer[DAT_UnitsState::instance.units[unitID].tile]; + if (otherUnitID != 0 && unitID != otherUnitID + && DAT_UnitsState::instance.units[otherUnitID].tunnelerFinishedDigging != 2) { return TRUE; } } - return FALSE; } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 469bd3597c..191149b8bf 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46903,7 +46903,7 @@ SHC_3BB0A8C1_0x00524A10 | 0.0% | Pending SHC_3BB0A8C1_0x00524A30 | 0.0% | Pending -SHC_3BB0A8C1_0x00524B30 | 80.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524B30 | 91.2% | Register allocation: unitID * 0x490 recomputed after the dying test SHC_3BB0A8C1_0x00524BF0 | 100.0% | Reimplemented From 48a10cd1700b57be640e2992dcf69326c90c7bf1 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:25:59 +0100 Subject: [PATCH 183/202] reimplement: applyUnitTopSpeedDelayBasedOnTribeSize 71.7 -> 72.6 Initialise the selection index in each for header instead of at its declaration. The constant then folds into the size test (test ax, ax), as in the original. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp | 6 +++--- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp index 76a886f4fd..63d6a540d0 100644 --- a/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/applyUnitTopSpeedDelayBasedOnTribeSize.cpp @@ -17,11 +17,11 @@ namespace Map { void TribesState::applyUnitTopSpeedDelayBasedOnTribeSize(int tribeID, BOOLEnum skipDistanceSort) { short* const tribeSize = &this->tribes[tribeID].size; - int i = 0; + int i; int j; int swapped; if (*tribeSize >= 40) { - for (; i < *tribeSize; ++i) { + for (i = 0; i < *tribeSize; ++i) { int const unitID = MACRO_CALL_MEMBER( OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, i); DAT_UnitsState::instance.units[unitID].topSpeedDelayIndex = (short)(i < 199 ? i : 199); @@ -31,7 +31,7 @@ namespace Map { this->someUnitIDArrayCount = 0; MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(this->someUnitIDMovementDistancePair), 0, this->someUnitIDMovementDistancePair); - for (; i < *tribeSize; ++i) { + for (i = 0; i < *tribeSize; ++i) { int const unitID = MACRO_CALL_MEMBER( OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, i); if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 191149b8bf..b70e0e2ee4 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46913,7 +46913,7 @@ SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending SHC_3BB0A8C1_0x00524E20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524EF0 | 71.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524EF0 | 72.6% | Register allocation: the bubble-sort counter takes ebx; the original keeps it in the tribeID parameter slot SHC_3BB0A8C1_0x00525060 | 0.0% | Pending From 8e78e19fe7aa4e35143562158db8f20add8d1e76 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:35:56 +0100 Subject: [PATCH 184/202] reimplement: assignAttackTargetsForTribe 44.6 -> 69.0 Rewrite the target dispatch as a flat if/else chain in which each arm calls its finder, skips the unit on failure and loads tile, y and x itself. The unit-state gate becomes nested tests that continue the loop, and the Ghidra gotos and the bVar7 flag are gone. With the per-arm loads, x, y and tile take the callee-saved registers and unitID is kept on the stack, as in the original. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../assignAttackTargetsForTribe.cpp | 368 +++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 185 insertions(+), 185 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp index b84e266831..24dea661ef 100644 --- a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -28,200 +28,200 @@ namespace Map { using WindowsHelper::Enums::BOOLEnum; /* - The gotos and the `bVar7` flag stay, against the style list. The nesting reads - wrongly - `if (targetType == 1010)` sits inside `if (targetType == 1021)`, so from - the top of the function it can never be true - but it is reachable, because the - unit-state gate below jumps into it, and the shape is the original's: writing the - gate as `continue`s, the target dispatch as a flat chain, and both arms as - fall-through into the shared tail costs 11.3 points (44.6% -> 33.3%, norm 40.7% - -> 28.4%). - - The `bool` flag stays a byte for the same reason: BOOLEnum costs 0.2 points - (44.6% -> 44.4%). + Every target arm loads tile, y and x itself. The compiler merges the identical + arm tails into one copy, which is the shape the original shows. The repeated + loads are what give x, y and tile their registers (ebx, ebp, esi) ahead of + unitID, which the original keeps on the stack. With one shared `found` flag + and the loads written once, unitID took ebx and the function measured 44.6%. + Two differences remain. The merged copy lands after the last arm (0x3f7), + where the original keeps the one after the 0x3f2 call. x and y have + swapped registers. No declaration order of x, y and tile changes that. */ // FUNCTION: STRONGHOLDCRUSADER 0x005244D0 BOOLEnum TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType) { - short sVar3; - uint _unitID; - int _tile2; - uint _freeTile; - int iVar5; - BOOLEnum BVar6; - bool bVar7; - uint _x; - uint _y; - short _targetTileY; - uint _tile1; - int unitSelectionIndex = 0; - BOOLEnum BVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); + int index = 0; + BOOLEnum const allAssassins + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[this->tribes[tribeID].owner] != -1) { return FALSE; } - if (0 < this->tribes[tribeID].size) { - do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 - && DAT_UnitsState::instance.units[_unitID].field_0x413 == 0 - && DAT_UnitsState::instance.units[_unitID].moveableUnk != 0) { - /* - fixme: this is some interesting data happening all over the unit code - */ - if (targetType == 1021) { - LAB_00524621: - if (targetType == 1010) { - _tile2 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, - DAT_TroopValueState::ptr)(_unitID); - LAB_00524638: - _tile1 = DAT_TroopValueState::instance.tile; - _y = DAT_TroopValueState::instance.y; - _x = DAT_TroopValueState::instance.x; - if (_tile2) { - DAT_UnitsState::instance.units[_unitID].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - if (targetType != Map::Units::STBT_0x414) - goto LAB_0052471e; - _freeTile = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::findFreeTileNearby, - DAT_UnitsState::ptr)(_unitID, _tile1); - if (_freeTile) { - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(_unitID, - (uint)(_freeTile - - DAT_ViewportRenderState::instance - .translationMatrix[DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[_freeTile]] - .addXgetTile), - (uint)(DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[_freeTile]), - 0); - sVar3 = DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[_tile1]; - DAT_UnitsState::instance.units[_unitID].attackAtTileY = sVar3; - DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID - = (short)_tile1; - iVar5 = DAT_ViewportRenderState::instance.translationMatrix[sVar3].addXgetTile; - DAT_UnitsState::instance.units[_unitID].targetingType - = Map::Units::UIT_ATTACK_WALL; - DAT_UnitsState::instance.units[_unitID].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[_unitID].attackAtTileX - = (short)_tile1 - (short)iVar5; - } - } - } else { - if (targetType != Map::Units::STBT_0x3f4) { - if (targetType == Map::Units::STBT_0x3f3) { - _tile2 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::findNearestAvailableScalePoint, - DAT_TroopValueState::ptr)(_unitID); - } else if (targetType == Map::Units::STBT_0x3f5) { - _tile2 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, - DAT_TroopValueState::ptr)(_unitID); - } else if (targetType == Map::Units::STBT_0x3fb) { - _tile2 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, - DAT_TroopValueState::ptr)(_unitID); - } else if (targetType == Map::Units::STBT_0x3fd) { - _tile2 = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, - DAT_TroopValueState::ptr)(_unitID); - } else if (targetType == Map::Units::STBT_0x3f6) { - _tile2 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::findEnemyTowersOrGates, - DAT_TroopValueState::ptr)(_unitID); - } else if ((targetType == Map::Units::STBT_0x413) - || (targetType == Map::Units::STBT_0x414)) { - _tile2 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, - DAT_TroopValueState::ptr)(_unitID); - } else { - if (targetType != Map::Units::STBT_0x3f7) - continue; - _tile2 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::calculateTile2MoatValueClosestToUnit, - DAT_TroopValueState::ptr)(_unitID); - } - goto LAB_00524638; - } - iVar5 = MACRO_CALL_MEMBER( - Map::Units::TroopValueState_Func::findNearestAvailableScalePoint, - DAT_TroopValueState::ptr)(_unitID); - _tile1 = DAT_TroopValueState::instance.tile; - _y = DAT_TroopValueState::instance.y; - _x = DAT_TroopValueState::instance.x; - if (!iVar5) - continue; - DAT_UnitsState::instance.units[_unitID].state.generic = ((UnitState)0x67); - LAB_0052471e: - _targetTileY - = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile1]; - DAT_UnitsState::instance.units[_unitID].attackAtTileY = _targetTileY; - DAT_UnitsState::instance.units[_unitID].targetedBuildingTile = _tile1; - DAT_UnitsState::instance.units[_unitID].attackAtTileX = (short)_tile1 - - (short)DAT_ViewportRenderState::instance.translationMatrix[_targetTileY] - .addXgetTile; - DAT_UnitsState::instance.units[_unitID].unknownDigMoatOrWallAttackFlag1015 - = (undefined2)targetType; - iVar5 = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(_unitID); - if (!iVar5) { - if (BVar4) { - DAT_PathFindingState::instance.allAssassinsUnk = 1; - } - BVar6 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(_unitID, _x, _y, 0); - if (!BVar6) { - DAT_UnitsState::instance.units[_unitID].logicalState = Map::Units::ULS_REMOVE; - } - } - if ((int)DAT_UnitsState::instance.units[_unitID].selectionTargetUnitID == _unitID) { - this->tribes[tribeID].targetX = (short)_x; - this->tribes[tribeID].targetY = (short)_y; - } - } - } else { - UnitStateShort UVar1 = DAT_UnitsState::instance.units[_unitID].state.generic; - if (UVar1 != Map::Units::States::US_MELEE_ATTACK_WALL - && UVar1 != Map::Units::States::US_MOVE_TO_DESTINATION && UVar1 != (UnitState)0x67) { - UnitTypeShort UVar2 = DAT_UnitsState::instance.units[_unitID].unitType; - if (UVar2 == Map::Units::UT_E_SPEAR) { - bVar7 = UVar1 == Map::Units::States::US_AIM_WEAPONUnk; - } else if (UVar2 == Map::Units::UT_E_ENGINEER) { - bVar7 = UVar1 == Map::Units::States::US_RELOAD_WEAPONUnk; - } else if (UVar2 == Map::Units::UT_E_LADDER) { - bVar7 = UVar1 == ((UnitState)3); - } else { - if (UVar2 != Map::Units::UT_TUNNELER) - goto LAB_00524621; - if (UVar1 == (UnitState)2 || UVar1 == (UnitState)3 - || UVar1 == Map::Units::States::US_RELOAD_WEAPONUnk - || UVar1 == Map::Units::States::US_STAND_UPUnk) - continue; - /* - This stays a flag assignment although MSVC - materialises it - `cmp ax, 9; sete al; test al, al; - jne` where the original has the bare `cmp ax, 9` and - lets the shared `jne` below consume the flags. The - other three arms do carry their comparison in the - flags; this one is the arm that falls through to the - shared test and MSVC will not carry it across that - edge. Testing and jumping here instead, which removes - the `sete` and the `test`, measures 44.6% -> 43.7%. - */ - bVar7 = UVar1 - == (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk); - } - if (!bVar7) - goto LAB_00524621; - } + if (this->tribes[tribeID].size <= 0) { + return TRUE; + } + do { + uint x; + uint y; + uint tile; + int const unitID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, index); + index = index + 1; + if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL + || DAT_UnitsState::instance.units[unitID].dying != 0 + || DAT_UnitsState::instance.units[unitID].usingTeleport != 0 + || DAT_UnitsState::instance.units[unitID].field_0x413 != 0 + || DAT_UnitsState::instance.units[unitID].moveableUnk == 0) { + continue; + } + if (targetType != Map::Units::STBT_0x3fd) { + UnitStateShort const state = DAT_UnitsState::instance.units[unitID].state.generic; + if (state == Map::Units::States::US_MELEE_ATTACK_WALL + || state == Map::Units::States::US_MOVE_TO_DESTINATION || state == (UnitState)0x67) { + continue; + } + UnitTypeShort const unitType = DAT_UnitsState::instance.units[unitID].unitType; + if (unitType == Map::Units::UT_E_SPEAR) { + if (state == Map::Units::States::US_AIM_WEAPONUnk) { + continue; + } + } else if (unitType == Map::Units::UT_E_ENGINEER) { + if (state == Map::Units::States::US_RELOAD_WEAPONUnk) { + continue; + } + } else if (unitType == Map::Units::UT_E_LADDER) { + if (state == (UnitState)3) { + continue; } + } else if (unitType == Map::Units::UT_TUNNELER) { + if (state == (UnitState)2 || state == (UnitState)3 + || state == Map::Units::States::US_RELOAD_WEAPONUnk + || state == Map::Units::States::US_STAND_UPUnk + || state == (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk)) { + continue; + } + } + } + if (targetType == Map::Units::STBT_0x3f2) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( + unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x3f4) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestAvailableScalePoint, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + DAT_UnitsState::instance.units[unitID].state.generic = (UnitState)0x67; + goto moveToTarget; + } else if (targetType == Map::Units::STBT_0x3f3) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestAvailableScalePoint, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x3f5) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x3fb) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x3fd) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)( + unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x3f6) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyTowersOrGates, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x413) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x414) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else if (targetType == Map::Units::STBT_0x3f7) { + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2MoatValueClosestToUnit, + DAT_TroopValueState::ptr)(unitID)) { + continue; + } + tile = DAT_TroopValueState::instance.tile; + y = DAT_TroopValueState::instance.y; + x = DAT_TroopValueState::instance.x; + } else { + continue; + } + DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; + if (targetType == Map::Units::STBT_0x414) { + uint const freeTile = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::findFreeTileNearby, DAT_UnitsState::ptr)(unitID, tile); + if (freeTile == 0) { + continue; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(unitID, + freeTile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[freeTile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[freeTile], 0); + short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY; + DAT_UnitsState::instance.units[unitID].targetID_OR_targetBuildingID = (short)tile; + DAT_UnitsState::instance.units[unitID].targetingType = Map::Units::UIT_ATTACK_WALL; + DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[unitID].attackAtTileX + = (short)tile - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; + continue; + } + moveToTarget: { + short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY; + DAT_UnitsState::instance.units[unitID].targetedBuildingTile = tile; + DAT_UnitsState::instance.units[unitID].attackAtTileX + = (short)tile - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; + DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = (undefined2)targetType; + if (!MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)( + unitID)) { + if (allAssassins != FALSE) { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } + if (!MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + unitID, x, y, 0)) { + DAT_UnitsState::instance.units[unitID].logicalState = Map::Units::ULS_REMOVE; } - } while (unitSelectionIndex < this->tribes[tribeID].size); + } + if (DAT_UnitsState::instance.units[unitID].selectionTargetUnitID == unitID) { + this->tribes[tribeID].targetX = (short)x; + this->tribes[tribeID].targetY = (short)y; + } } + } while (index < this->tribes[tribeID].size); return TRUE; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b70e0e2ee4..44f90ee205 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46891,7 +46891,7 @@ SHC_3BB0A8C1_0x005242B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00524340 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005244D0 | 44.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005244D0 | 69.0% | Register allocation: x/y swapped; the merged arm tail lands after 0x3f7, not after 0x3f2 SHC_3BB0A8C1_0x00524890 | 100.0% | Reimplemented From e49a2e53f7d6feff0c9eb2ab0030dbf296d5c957 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:45:53 +0100 Subject: [PATCH 185/202] reimplement: commandUnitsToLocation 71.4 -> 90.3 Set noneCommanded after the size test, and send the empty tribe to the shared return FALSE. The constant 1 then gets ebx and the tribe offset is spilled, as in the original. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/commandUnitsToLocation.cpp | 15 +++++++++++++-- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index b798106f1c..eaf7758fbe 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -28,10 +28,20 @@ namespace Map { int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) { int index = 0; - BOOLEnum noneCommanded = TRUE; + /* + noneCommanded is set after the size test, and the empty tribe jumps to the + shared `return FALSE`. The original keeps 1 in ebx (the flag, the index + step, the rally flag and the return value all use it) and spills the tribe + offset. With the flag set to TRUE in its declaration, the offset took ebx + and every 1 was an immediate (80.2% in quick_diff, 90.2% now). One + difference is left: `this` and index swap edi and ebp. reg_scores gives + both the same score (-61), so the symbol-id tiebreak decides. + */ + BOOLEnum noneCommanded; if (this->tribes[tribeID].size <= 0) { - return FALSE; + goto noneCommandedExit; } + noneCommanded = TRUE; do { int const unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, index); @@ -93,6 +103,7 @@ namespace Map { noneCommanded = FALSE; } while (index < this->tribes[tribeID].size); if (noneCommanded != FALSE) { + noneCommandedExit: return FALSE; } if ((char)matchUnitSpeeds != '\0') { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 44f90ee205..c077f59f83 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46967,7 +46967,7 @@ SHC_3BB0A8C1_0x005266F0 | 0.0% | Pending SHC_3BB0A8C1_0x00526D50 | 0.0% | Pending -SHC_3BB0A8C1_0x00526F00 | 71.4% | constant 1 is held in ebx in the original; ours puts the tribe offset there +SHC_3BB0A8C1_0x00526F00 | 90.3% | Register allocation: this and index swap edi/ebp (reg_scores tie) SHC_3BB0A8C1_0x005270F0 | 58.6% | this held in a register where the original spills it and keeps the loop index in ebp From b359e000957ade1e5c0d8a827131f6ecf4c9b9cd Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:45:54 +0100 Subject: [PATCH 186/202] assignAttackTargetsForTribe: record the measured load order The tile, x, y load order fixes the x/y register swap but lowers the normalized match (66.2% -> 63.9%), so the code is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/assignAttackTargetsForTribe.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp index 24dea661ef..845eb17093 100644 --- a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -36,6 +36,10 @@ namespace Map { Two differences remain. The merged copy lands after the last arm (0x3f7), where the original keeps the one after the 0x3f2 call. x and y have swapped registers. No declaration order of x, y and tile changes that. + Loading tile, x, y instead fixes the swap (71.3%) but costs normalized + match (66.2% -> 63.9%), so the order stays. Putting the shared code + inside the 0x3f2 arm, with the other arms jumping to it, still leaves + the last copy. */ // FUNCTION: STRONGHOLDCRUSADER 0x005244D0 BOOLEnum TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType) From f5150c4af2bcf39236fabd5007b052b42218ef96 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 09:51:49 +0100 Subject: [PATCH 187/202] reimplement: createAnimal 46.8 -> 96.1 Return tribeID once at the end of the herd tail, so the spawn counter gets ebx and tribeID stays in its stack slot, as in the original. The substate is now the low two bits of fixedRng without the byte cast, the spawn loop is guarded with 0 < count, and the casts on the x/y arguments are gone. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/createAnimal.cpp | 39 +++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp index c77c32ec6f..563e14deff 100644 --- a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -24,8 +24,12 @@ namespace Map { using Map::Units::UnitType; /* - The `short` tribe-size local stays narrow: widening it costs 0.4 points - (46.8% -> 46.4%). + The `short` tribe-size local stays narrow: widening it costs 0.4 points. + + The herd tail returns tribeID once, at the end. With a `return tribeID` in + each arm, tribeID took ebx and the spawn counter was kept on the stack + (quick_diff 0.57). The original keeps the counter in ebx and reloads + tribeID from its slot for each return (quick_diff 0.93). */ // FUNCTION: STRONGHOLDCRUSADER 0x00525BA0 dword TribesState::createAnimal(MappersEnum animalType, uint x, uint y, int tile) @@ -95,14 +99,14 @@ namespace Map { } } int iVar3 = 0; - if (_rng2) { + if (0 < _rng2) { do { unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( 0, 0, (int)(x * 8), (int)(y * 8), tile, unitType); if (unitID) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, (int)(tribeID)); DAT_UnitsState::instance.units[unitID].substate - = (byte)DAT_UnitsState::instance.units[unitID].fixedRng & 3; + = (short)(DAT_UnitsState::instance.units[unitID].fixedRng & 3); if (unitType == Map::Units::UT_ANTELOPESHDEER) { byte bVar2 = (byte)DAT_UnitsState::instance.units[unitID].fixedRng & 7; DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = bVar2; @@ -130,26 +134,19 @@ namespace Map { this->tribes[tribeID].herdMinSize = sVar1 / 2; if (animalType == Commands::M_MAPPER_LION) { this->tribes[tribeID].herdMaxSize = sVar1; - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::storeLionXAndYInMapInfo, this)( - x, (undefined4)((int)(y))); - return (dword)(tribeID); - } - this->tribes[tribeID].herdMaxSize = sVar1 * 2; - if (animalType == Commands::M_MAPPER_DEER) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::storeDeerXAndYinMapInfo, this)( - x, (undefined4)((int)(y))); - return (dword)(tribeID); - } - if (animalType == Commands::M_MAPPER_RABBIT) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setRabbitSpawnXY, this)(x, (undefined4)((int)(y))); - return (dword)(tribeID); - } - if (animalType == Commands::M_MAPPER_CAMEL) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setCamelSpawnXY, this)(x, (undefined4)((int)(y))); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::storeLionXAndYInMapInfo, this)(x, y); + } else { + this->tribes[tribeID].herdMaxSize = sVar1 * 2; + if (animalType == Commands::M_MAPPER_DEER) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::storeDeerXAndYinMapInfo, this)(x, y); + } else if (animalType == Commands::M_MAPPER_RABBIT) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setRabbitSpawnXY, this)(x, y); + } else if (animalType == Commands::M_MAPPER_CAMEL) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setCamelSpawnXY, this)(x, y); + } } return (dword)(tribeID); } - } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index c077f59f83..48df018ee4 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46949,7 +46949,7 @@ SHC_3BB0A8C1_0x00525A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00525BA0 | 46.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00525BA0 | 96.1% | Scratch-register order in the spawn and herd-tail calls SHC_3BB0A8C1_0x00525F20 | 100.0% | Reimplemented From 72c82e5c88f6821a757b32dae42f8084c48dd55a Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 10:01:01 +0100 Subject: [PATCH 188/202] reimplement: playUnitSelectionSound 78.0 -> 92.4 Write the swordsman arm as one else-if chain with a single return, and read the horse archer's x and y directly instead of through pointer locals. The three swordsman calls then keep their own epilogues, and the horse-archer calls get the field addresses in registers, as in the original. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/playUnitSelectionSound.cpp | 18 ++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 0bfae28e45..094290195f 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -41,14 +41,10 @@ namespace Map { if (selectedCount == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xad); - return; - } - if (selectedCount == 2) { + } else if (selectedCount == 2) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xae); - return; - } - if (selectedCount >= 3) { + } else if (selectedCount >= 3) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xaf); } @@ -75,20 +71,18 @@ namespace Map { return; } case Map::Units::UT_A_HARCHER: { - short* const unitY = &DAT_UnitsState::instance.units[unitID].y; - short* const unitX = &DAT_UnitsState::instance.units[unitID].x; if (selectedCount == 1) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - *unitX, *unitY, 0xea); + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xea); } else if (selectedCount <= 4) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - *unitX, *unitY, 0xeb); + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xeb); } else { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - *unitX, *unitY, 0xec); + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0xec); } MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - *unitX, *unitY, 0x59); + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0x59); return; } case Map::Units::UT_S_CATAPULT: diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 48df018ee4..b112b8f429 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46395,7 +46395,7 @@ SHC_3BB0A8C1_0x00521690 | 0.0% | Pending SHC_3BB0A8C1_0x00521720 | 98.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x005217E0 | 78.0% | tail merging: the original keeps the 0xae and 0xaf plays as separate blocks (different registers); ours merges them +SHC_3BB0A8C1_0x005217E0 | 92.4% | Knight count is copied through eax; the original loads it straight into edi SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending From 52c5480c574c4dd3d3f132f1b1a8f7e5c71803df Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 10:06:47 +0100 Subject: [PATCH 189/202] reimplement: playUnitCombatSpeechForTarget 78.2 -> 94.4 Load the two units' coordinates into four locals and square the differences inline. All four values are then live at once, which pushes unitType out of ebx onto the stack, as in the original. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/playUnitCombatSpeechForTarget.cpp | 8 +++++--- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp index 4cd8ee2865..c44cf924da 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -56,9 +56,11 @@ namespace Map { } else { range = unitID; } - int const dx = DAT_UnitsState::instance.units[otherUnitID].x - DAT_UnitsState::instance.units[unitID].x; - int const dy = DAT_UnitsState::instance.units[otherUnitID].y - DAT_UnitsState::instance.units[unitID].y; - if (dx * dx + dy * dy > range) { + int const otherX = DAT_UnitsState::instance.units[otherUnitID].x; + int const otherY = DAT_UnitsState::instance.units[otherUnitID].y; + int const unitX = DAT_UnitsState::instance.units[unitID].x; + int const unitY = DAT_UnitsState::instance.units[unitID].y; + if ((otherX - unitX) * (otherX - unitX) + (otherY - unitY) * (otherY - unitY) > range) { MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( (UnitType)unitType, 0x1e); } else { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b112b8f429..569898a9d8 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46505,7 +46505,7 @@ SHC_3BB0A8C1_0x00521A8E | 0.0% | Pending SHC_3BB0A8C1_0x00521A8F | 0.0% | Pending -SHC_3BB0A8C1_0x00521A90 | 78.2% | register allocation in the archer arm: the original spills the unit type to the parameter slot and uses ebx/ebp for the coordinates +SHC_3BB0A8C1_0x00521A90 | 94.4% | unitID load hoisted above the range chain; knight count is copied through eax SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending From 2dc099cd34d15127b830b68917bb83f1661f20be Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 10:14:53 +0100 Subject: [PATCH 190/202] reimplement: drawFlagsAndUnitDestinations 57.6 -> 78.1 Rewrite the targeting tail as one flat else-if chain on the instruction type, giving the fill-moat and dig-moat arms their own text-marker call. Also compute the planned-destination tile before the cursor frame, make the rally index and the flag frame signed so the modulo uses idiv, and read the target unit ID as an int. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../drawFlagsAndUnitDestinations.cpp | 129 +++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index da28084767..34a7ac3fad 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -45,11 +45,11 @@ namespace Map { (`test ax, ax`, `jge`, `mov esi, 1`). */ if (_isRallying && DAT_GameSynchronyState::instance.currentPlayerSlotID == this->tribes[tribeID].owner) { - uint _rallyIndex = 0; + int _rallyIndex = 0; if (_isRallying < 0) { _rallyIndex = 1; } - if ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount) { + if (_rallyIndex < (int)this->tribes[tribeID].rallyPointCount) { _pRallyPoints = this->tribes[tribeID].rallyPointArray + _rallyIndex; do { /* @@ -57,8 +57,8 @@ namespace Map { */ MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, DAT_ViewportRenderState::ptr)(IO::Graphics::GID_FLOATS_NEW, - (int)(((*_pRallyPoints)[0] + DAT_TileMapState::instance.flagAnimationFrame + _rallyIndex - + (*_pRallyPoints)[1]) + (int)(((*_pRallyPoints)[0] + (int)DAT_TileMapState::instance.flagAnimationFrame + + _rallyIndex + (*_pRallyPoints)[1]) % 10 + 0x61), 0x12, -1, @@ -67,7 +67,7 @@ namespace Map { 0xa0022); _rallyIndex = _rallyIndex + 1; _pRallyPoints = _pRallyPoints + 1; - } while ((int)_rallyIndex < (int)this->tribes[tribeID].rallyPointCount); + } while (_rallyIndex < (int)this->tribes[tribeID].rallyPointCount); } } int iVar5 = 0; @@ -130,81 +130,80 @@ namespace Map { UVar2 != Map::Units::UIT_UNIT_ATTACK_UNIT) && UVar2 != Map::Units::UIT_ATTACK_LAND && UVar2 != Map::Units::UIT_MAN_SIEGE_EQUIPMENT) { + tile = (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX + + DAT_ViewportRenderState::instance + .translationMatrix[DAT_UnitsState::instance.units[_unitID] + .plannedDestinationY] + .addXgetTile; iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; if (!(iVar4 < 8)) { iVar4 = 0xf - iVar4; } MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, - DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, - (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX - + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID] - .plannedDestinationY] - .addXgetTile, - 2); + DAT_ViewportRenderState::ptr)( + IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, tile, 2); } } UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType; - if ((UVar2 == Map::Units::UIT_ATTACK_LAND) || (UVar2 == Map::Units::UIT_THROW_COW)) { + if (UVar2 == Map::Units::UIT_ATTACK_LAND || UVar2 == Map::Units::UIT_THROW_COW) { MACRO_CALL_MEMBER(Map::TileMapState_Func::spawnFloatingNumberAroundTile, DAT_TileMapState::ptr)((int)DAT_UnitsState::instance.units[_unitID].attackAtTileX, (int)(DAT_UnitsState::instance.units[_unitID].attackAtTileY), (int)(DAT_UnitsState::instance.units[_unitID].unkAttackRelated)); - } else { - if (UVar2 == Map::Units::UIT_FILL_MOAT) { - iVar4 = (int)DAT_UnitsState::instance.units[_unitID].targetX + } else if (UVar2 == Map::Units::UIT_FILL_MOAT) { + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingTextElement, + DAT_ViewportRenderState::ptr)(0x4e, 6, -0x20, + (int)DAT_UnitsState::instance.units[_unitID].targetX + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] - .addXgetTile; - } else { - if (UVar2 != Map::Units::UIT_DIG_MOAT) { - if (UVar2 == Map::Units::UIT_UNIT_ATTACK_UNIT) { - sVar1 = DAT_UnitsState::instance.units[_unitID] - .targetedUnitID__OR__engineerMannedSiegeEngineRef; - if ((DAT_UnitsState::instance.units[sVar1].uid - == DAT_UnitsState::instance.units[_unitID] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID) - && (DAT_UnitsState::instance.units[sVar1].logicalState - == Map::Units::ULS_NORMAL)) { - DAT_UnitsState::instance.units[sVar1].field_0x6c = 1; - } - } else if (UVar2 == Map::Units::UIT_ATTACK_BUILDING) { - sVar1 = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; - if ((sVar1) - && (DAT_BuildingsState::instance.buildings[sVar1].uid - == DAT_UnitsState::instance.units[_unitID] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { - DAT_BuildingsState::instance.buildings[sVar1].targetMarker = 1; - } - } else if (UVar2 == Map::Units::UIT_LIGHT_PITCH) { - sVar1 = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; - if ((sVar1) - && (DAT_TileMapState::instance.pitchDitches[sVar1].uid - == DAT_UnitsState::instance.units[_unitID] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { - DAT_TileMapState::instance - .MiscDisplayLayer[DAT_TileMapState::instance.pitchDitches[sVar1].tile] - = DAT_TileMapState::instance.MiscDisplayLayer - [DAT_TileMapState::instance.pitchDitches[sVar1].tile] - | 0x4000; - } - } else { - uint uVar3 = DAT_UnitsState::instance.units[_unitID].targetedBuildingTile; - if ((uVar3) && (DAT_TileMapState::instance.BuildingLayer[uVar3] != 0)) { - DAT_BuildingsState::instance - .buildings[DAT_TileMapState::instance.BuildingLayer[uVar3]] - .targetMarker = 1; - } - } - continue; - } - iVar4 = (int)DAT_UnitsState::instance.units[_unitID].targetX + .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] + .addXgetTile, + 2); + } else if (UVar2 == Map::Units::UIT_DIG_MOAT) { + MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingTextElement, + DAT_ViewportRenderState::ptr)(0x4e, 6, -0x20, + (int)DAT_UnitsState::instance.units[_unitID].targetX + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] - .addXgetTile; + .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] + .addXgetTile, + 2); + } else if (UVar2 == Map::Units::UIT_UNIT_ATTACK_UNIT) { + int const targetUnitID = DAT_UnitsState::instance.units[_unitID] + .targetedUnitID__OR__engineerMannedSiegeEngineRef; + if ((DAT_UnitsState::instance.units[targetUnitID].uid + == DAT_UnitsState::instance.units[_unitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID) + && (DAT_UnitsState::instance.units[targetUnitID].logicalState + == Map::Units::ULS_NORMAL)) { + DAT_UnitsState::instance.units[targetUnitID].field_0x6c = 1; + } + } else if (UVar2 == Map::Units::UIT_ATTACK_BUILDING) { + short const buildingID + = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; + if ((buildingID) + && (DAT_BuildingsState::instance.buildings[buildingID].uid + == DAT_UnitsState::instance.units[_unitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { + DAT_BuildingsState::instance.buildings[buildingID].targetMarker = 1; + } + } else if (UVar2 == Map::Units::UIT_LIGHT_PITCH) { + short const pitchDitchID + = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; + if ((pitchDitchID) + && (DAT_TileMapState::instance.pitchDitches[pitchDitchID].uid + == DAT_UnitsState::instance.units[_unitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { + DAT_TileMapState::instance + .MiscDisplayLayer[DAT_TileMapState::instance.pitchDitches[pitchDitchID].tile] + = DAT_TileMapState::instance + .MiscDisplayLayer[DAT_TileMapState::instance.pitchDitches[pitchDitchID].tile] + | 0x4000; + } + } else { + uint uVar3 = DAT_UnitsState::instance.units[_unitID].targetedBuildingTile; + if ((uVar3) && (DAT_TileMapState::instance.BuildingLayer[uVar3] != 0)) { + DAT_BuildingsState::instance.buildings[DAT_TileMapState::instance.BuildingLayer[uVar3]] + .targetMarker = 1; } - MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingTextElement, - DAT_ViewportRenderState::ptr)(0x4e, 6, -0x20, iVar4, 2); } } } while (iVar5 < this->tribes[tribeID].size); diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 569898a9d8..1e5ab5b7f3 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46861,7 +46861,7 @@ SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523920 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523A30 | 57.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00523A30 | 78.1% | Constant 1 in edi with the unit index on the stack; operand order in the rally-flag sum SHC_3BB0A8C1_0x00523B00 | 0.0% | Pending From e9f69ef07a2b57461bd58d602205ab567b043701 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 10:20:53 +0100 Subject: [PATCH 191/202] reimplement: updateTribeCombatStanceBehavior 75.1 -> 81.2 Give the building and ground-tile targets their own giveTribeAnInstruction call, test both counters through the short pointer that incremented them, and write the rally-wrap test as index >= count. The unused short copy of the counter is gone. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../updateTribeCombatStanceBehavior.cpp | 33 ++++++++----------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp index 17d99f7cde..8b0ac6bfb7 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp @@ -147,15 +147,12 @@ namespace Map { psVar1 = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; *psVar1 = *psVar1 + 1; /* - Signed, and sixteen bits wide. The field is a ushort, so testing it directly - gives an unsigned `jbe`; the original has `movzx eax, word ptr [esi + 0x56]`, - `cmp ax, 0x32` and a signed `jle`. A `short` local reproduces both - the load - may zero-extend because nothing above bit 15 is read, and the test stays - signed. The field type is left alone: 43 places read it, most outside this - namespace. + Both counters are tested through the `short*` that incremented them. The + original re-reads each field after the add (`movzx eax, word ptr + [esi + 0x56]`, `cmp ax, 0x32`, signed `jle`). A fresh read of the ushort + field folds into `cmp word ptr [mem]` instead, and the test is unsigned. */ - short const _attackCounter = (short)this->tribes[tribeID].unknownAttackRelatedUpdateCounter; - if (0x32 < _attackCounter) { + if (0x32 < *psVar1) { /* The rallying arm comes first: `xor ebx, ebx; cmp word ptr [esi + 0x22e], bx; je 0x109` jumps away when the flag is clear, so the tribe-is-rallying @@ -173,7 +170,7 @@ namespace Map { _buildingIndex_2 = this->tribes[tribeID].rallyPointCount; iVar5 = this->tribes[tribeID].currentRallyPointIndex + 1; this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - if (_buildingIndex_2 <= iVar5) { + if (iVar5 >= _buildingIndex_2) { if (this->tribes[tribeID].isRallyingUnk == -1) { this->tribes[tribeID].currentRallyPointIndex = 0; this->tribes[tribeID].isRallyingUnk = 0; @@ -213,7 +210,7 @@ namespace Map { reset and the arm entered on every tick. The original's `cmp ax, 0x3c; jl` skips the whole arm. */ - if (0x3c <= this->tribes[tribeID].aggressiveStanceCounter) { + if (0x3c <= *psVar1) { _buildingIndex_2 = this->tribes[tribeID].size; this->tribes[tribeID].aggressiveStanceCounter = 0; if (0 < _buildingIndex_2) { @@ -250,17 +247,15 @@ namespace Map { if (DAT_PathFindingState::instance.ALG_ResultTile) { _buildingIndex_2 = DAT_TileMapState::instance .BuildingLayer[DAT_PathFindingState::instance.ALG_ResultTile]; - if (!_buildingIndex_2) { - _buildingID = 0; - _unitInstructionType = ((UnitInstructionType)0x25); - _buildingIndex = DAT_PathFindingState::instance.ALG_ResultTile; + if (_buildingIndex_2) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + tribeID, (UnitInstructionType)0x26, (int)_buildingIndex_2, + DAT_BuildingsState::instance.buildings[_buildingIndex_2].uid, 0); } else { - _buildingID = DAT_BuildingsState::instance.buildings[_buildingIndex_2].uid; - _unitInstructionType = ((UnitInstructionType)0x26); - _buildingIndex = (int)_buildingIndex_2; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( + tribeID, (UnitInstructionType)0x25, + DAT_PathFindingState::instance.ALG_ResultTile, 0, 0); } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - tribeID, _unitInstructionType, _buildingIndex, _buildingID, 0); } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 1e5ab5b7f3..6d462a7d2d 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -48879,7 +48879,7 @@ SHC_3BB0A8C1_0x0052A640 | 98.4% | Reimplemented, one instruction off: MSVC ancho SHC_3BB0A8C1_0x0052A700 | 98.6% | loop end is a relocation (&field_0xcc4[40].y) where the original has the absolute 0xb4df44 -SHC_3BB0A8C1_0x0052A7D0 | 75.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052A7D0 | 81.2% | Aggressive-stance scan: the slot address and the tribe offset are hoisted differently SHC_3BB0A8C1_0x0052ACB4 | 0.0% | Pending From 413aafe76d17509f0c1508513fdb8ede27916a4c Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 12:05:13 +0100 Subject: [PATCH 192/202] reimplement: giveTribeMoveInstruction 30.0 -> 49.7 Rewritten section by section against the original: subscripted tribe, one targetArea, one defensive flag, storeAsRallyPoint reused as the fallback-search state, unitType read from the unit in each filter (no jump threading), one index for the three loops, and the move delay stored per arm for 16-bit arithmetic. Normalized 22.6 -> 47.1. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/giveTribeMoveInstruction.cpp | 983 ++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 433 insertions(+), 552 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 2253d6dc9e..47d2584405 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -31,600 +31,481 @@ namespace Map { using WindowsHelper::Enums::BOOLEnum; /* - Two declaration moves were measured here and all of them reverted, because raw - and normalized disagree and raw is what this function is scored on: - - as written raw 30.0% norm 22.5% - `_searchBudget` declared at this level raw 28.0% norm 26.1% - plus `_tribeUnitIndex` scoped to its loop raw 27.1% norm 27.0% - only `_tribeUnitIndex` scoped raw 21.7% norm 21.9% - - The original does declare the search budget at function scope - its prologue - stores 0x1f4 into the slot alongside the zeroed locals, which is what the middle - two variants reproduce - and `_tribeUnitIndex` is a slot too many when it does, - so the frame only matches with both moves. Whatever is costing the raw match is - downstream of the stack displacements those moves shift, so this needs the - allocation understood rather than another declaration permutation. - - The strongest evidence for that, and the closest this function has come: the - original reaches the modulo search through six separate call sites, pushing the - constant from each and jumping back to a shared tail - `push ebp; push edi; - push 2; jmp -0x19b` and the same with 4 and 1 - where the import assigns - `_modulo` in six places and calls once. Writing the six call sites out is the - change that took spawnUnitsForAITribe from 78% to a full match, and here it - moves everything except the score: - - instructions 726 -> 721 (original: 720) - normalized 22.6 -> 29.4 - register-blind 36.7 -> 46.1 - raw 30.0 -> 28.9 - - What it costs is the frame. `_modulo` is one of the slots, and without it MSVC - drops to `sub esp, 0x2c` where the original - and the import - have `sub esp, - 0x34`, and the `mov dword ptr [esp + 0x38], 0x1f4` goes with it. So the - instruction sequence gets within one instruction of the original while the frame - stops matching, and raw follows the frame. Adding the `_searchBudget` move on top - changes nothing at all now (identical numbers), where on its own it used to move - both figures. - - Reverted, because raw is what the status file tracks. - - The two missing locals do not exist, and this is now settled rather than - suspected. 0x526676 reads the tile's logic word once into a register and then has - `test eax, 0x100; jz; mov [esp + 0x20], ebx` followed by - `test eax, 0x10000000; jz; mov [esp + 0x20], ebx` - two tests storing the same 1 - into the *same* slot - so the original has one defensive-structure flag where the - decompiler printed three variables and two loads. Writing that shape out, which - is what the original demonstrably does, measures: - - as written (three flags, `_modulo`) frame 0x34 raw 30.0% norm 22.5% - six call sites (three flags, no `_modulo`) frame 0x2c raw 28.9% norm 29.4% - one flag, `_modulo` frame 0x2c raw 23.4% norm 21.5% - one flag, six call sites frame 0x2c raw 28.3% norm 28.1% - - Each local removed costs a slot and the frame drops to 0x2c, so our 0x34 is made - of a different set of locals than the original's thirteen slots and matching the - size was a coincidence. A copy of the x parameter alongside `_y` does not add one - back - MSVC coalesces it. Normalized says plainly that the six call sites are the - right shape; raw cannot see past the displacement bytes. Do not re-run these four. + Written section by section against the original rather than the decompiler's + output. What each part of the shape is for: + - The tribe is always subscripted, never bound to a pointer: MSVC then keeps + `this + tribeID * 0x334` and reads the fields at +0x5a, +0x5c, ... as the + original does. A `Tribe*` makes it fold the 0x28 into the base instead. + - One `targetArea` holds the target unit's area and then the result of + canNavigateFunctionReturnsArea; one `defensive` flag is set by two separate + tests of the logic word (0x526676 stores the same 1 into one slot twice). + - `storeAsRallyPoint` is dead after the rally point is stored, and the original + keeps the "fallback search started" state in its slot, so it is reused for that. + - The filter reads `unitType` from the unit every time. Through a local, MSVC + knows the type after each passing digTile test and threads the jump past the + remaining type tests; the original tests them all in line. + - One `index` serves all three loops. + - The move delay is stored from each arm so the multiply is done in 16 bits + (`imul dx, dx, 3` / `add dx, dx`), as the original does. */ // FUNCTION: STRONGHOLDCRUSADER 0x005263A0 undefined4 TribesState::giveTribeMoveInstruction( int tribeID, uint x1, uint y1, int rallyBool, int storeAsRallyPoint, UnitMatchSpeedEnum speedMatching) { - UnitStateShort UVar1; - UnitTypeShort UVar2; - int _firstUnitID; - int _navArea; - uint _someUnitCanClimb; - dword _area3; - int iVar7; - int _hasUnitInArea; - BOOLEnum BVar8; - int _unitID; - dword dVar9; - int _unitID_2; - int _modulo; - int _area_2; - UnitTypeShort _unitType; - int _targetUnitArea; - uint _y = y1; - int _bool1 = 0; - int _tribeUnitIndex = 0; - int bVar6 = 0; - int _algTile2 = 0; - int _algTile = 0; - int bVar4 = 0; - if (!x1) { - if (!y1) { - return (undefined4)(0); + int allAssassins = 0; + int climbing = 0; + int index = 0; + int defensive = 0; + int crossArea = 0; + int crossAreaTargetTile = 0; + int crossAreaResultTile = 0; + int searchBudget = 500; + int unitID; + UnitStateShort unitState; + int delayIndex; + if (x1 == 0) { + if (y1 == 0) { + return 0; } } else { if ((int)x1 < 0) { - return (undefined4)(0); + return 0; } - /* - `cmp 0x190; jge`, not `cmp 0x18f; jg`. - */ - if (400 <= (int)x1) { - return (undefined4)(0); + if ((int)x1 >= 400) { + return 0; } } - if (399 < y1) { - return (undefined4)(0); + if (y1 > 399) { + return 0; } if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0) { - return (undefined4)(0); + return 0; } - /* - The subscript is repeated rather than bound to a `Tribe&`, although the - original does take the address once (`lea eax, [edx + ecx]` at 0x526417, kept - in a stack slot) and read every later field through it: binding all 21 uses to - a reference costs 1.1 points (30.0% -> 28.9%, norm 65.2% -> 21.8%). - */ - int _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; - int _targetUnitX = (int)DAT_UnitsState::instance.units[_targetUnitID].x; - int _targetUnitY = (int)DAT_UnitsState::instance.units[_targetUnitID].y; - _targetUnitArea + int targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + int const targetUnitX = DAT_UnitsState::instance.units[targetUnitID].x; + int const targetUnitY = DAT_UnitsState::instance.units[targetUnitID].y; + int targetArea = (short)DAT_TileMapState::instance - .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[_targetUnitY].addXgetTile - + _targetUnitX]; - int _tile = DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile + x1; - int _area = (int)(short)DAT_TileMapState::instance.PathConnectionLayer[_tile]; - if (!(DAT_TileMapState::instance.LogicLayer[_tile] & 0x30)) { - DAT_PathFindingState::instance.field_0x98 = 0; - this->fcn_mtribe = this->fcn_mtribe + 1; - _firstUnitID = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::getFirstUnitInTribeThatIsOnXTerrain, this)(tribeID); - if (_targetUnitID == _firstUnitID) { - _navArea = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, - DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, (dword)((int)(_area)), - (uint)(DAT_UnitsState::instance.units[_targetUnitID].x), - (uint)(DAT_UnitsState::instance.units[_targetUnitID].y)); - if (!_navArea) { - return (undefined4)(MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)(tribeID, - (undefined4)((int)(x1)), (undefined4)((int)(y1)), (undefined4)((int)(rallyBool)), - storeAsRallyPoint)); + .PathConnectionLayer[DAT_ViewportRenderState::instance.translationMatrix[targetUnitY].addXgetTile + + targetUnitX]; + int const tile = DAT_ViewportRenderState::instance.translationMatrix[y1].addXgetTile + x1; + int const area = (short)DAT_TileMapState::instance.PathConnectionLayer[tile]; + if (DAT_TileMapState::instance.LogicLayer[tile] & 0x30) { + return 0; + } + DAT_PathFindingState::instance.field_0x98 = 0; + ++this->fcn_mtribe; + if (targetUnitID + == MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getFirstUnitInTribeThatIsOnXTerrain, this)( + tribeID)) { + targetArea = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, + DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, area, + DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y); + if (targetArea == 0) { + return MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)( + tribeID, x1, y1, rallyBool, storeAsRallyPoint); + } + targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + climbing = 1; + } else if (targetArea == 0) { + return MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)( + tribeID, x1, y1, rallyBool, storeAsRallyPoint); + } + if (area <= 0) { + return 0; + } + if (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID)) { + allAssassins = 1; + } else if (targetArea != area) { + int const canClimb + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::tribeContainsUnitThatCanClimb, this)(tribeID); + int const climbArea = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, targetArea, area, canClimb); + if (climbArea == 0) { + if (!MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::someBinaryAlgFunctionPathFinding, + DAT_PathFindingState::ptr)(targetUnitID)) { + return 0; } - _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; - _bool1 = 1; - _area_2 = _navArea; - } else { - _area_2 = _targetUnitArea; - if (!_targetUnitArea) { - return (undefined4)(MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)(tribeID, - (undefined4)((int)(x1)), (undefined4)((int)(y1)), (undefined4)((int)(rallyBool)), - storeAsRallyPoint)); + if (!MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findCrossAreaBridgeTileToTarget, + DAT_PathFindingState::ptr)(targetUnitID, x1, y1)) { + return 0; } + crossArea = 1; + crossAreaTargetTile = DAT_PathFindingState::instance.ALG_TargetTile; + crossAreaResultTile = DAT_PathFindingState::instance.ALG_ResultTile; + } else if (!MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::setClimbBasedOnClosestClimbData, + DAT_PathFindingState::ptr)(targetUnitID, targetArea, climbArea)) { + return 0; } - /* - WARNING: y1 now is area2 - */ - y1 = _area_2; - if (0 < _area) { - int _firstUnit - = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID); - /* - The nesting here stays, against the asm. The original tests the - all-assassins result and jumps 0x38 forward when it is zero - (`test eax, eax; je 0x38`), so the one-line `bVar4 = 1` is its - fall-through and the thirty lines of cross-area path handling go - far - ours is the other way round. Swapping the arms measures - 30.0% -> 29.6% raw, although normalized rises 22.5% -> 23.4%: the - instruction sequence does get closer and the branch displacements - get worse. Everywhere else in this namespace fixing an inverted arm - gained raw, nine times out of nine; this is the one that did not, so - it is left as it was and the measurement recorded. - */ - if (!_firstUnit) { - if (_area != y1) { - _someUnitCanClimb = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::tribeContainsUnitThatCanClimb, this)(tribeID); - _area3 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::calculateCanPlayerUnitsNavigateToAreaFromArea, - DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, (dword)((int)(y1)), - (dword)((int)(_area)), (int)(_someUnitCanClimb)); - if (!_area3) { - iVar7 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::someBinaryAlgFunctionPathFinding, - DAT_PathFindingState::ptr)(_targetUnitID); - if (!iVar7) { - return (undefined4)(0); - } - iVar7 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findCrossAreaBridgeTileToTarget, - DAT_PathFindingState::ptr)(_targetUnitID, x1, _y); - if (!iVar7) { - return (undefined4)(0); + } + if (storeAsRallyPoint) { + this->tribes[tribeID].isRallyingUnk = (short)rallyBool; + this->tribes[tribeID].rallyPointArray[0][0] = DAT_UnitsState::instance.units[targetUnitID].x; + this->tribes[tribeID].rallyPointArray[0][1] = DAT_UnitsState::instance.units[targetUnitID].y; + this->tribes[tribeID].rallyPointArray[1][0] = (short)x1; + this->tribes[tribeID].rallyPointArray[1][1] = (short)y1; + this->tribes[tribeID].currentRallyPointIndex = 1; + this->tribes[tribeID].rallyPointCount = 2; + this->tribes[tribeID].containsRabbit = 0; + } + this->ALG_ResultTileIndex = 0; + this->field_0x18 = 0; + if (DAT_TileMapState::instance.LogicLayer[tile] & 0x100) { + defensive = 1; + } + storeAsRallyPoint = 0; + if (DAT_TileMapState::instance.LogicLayer[tile] & 0x10000000) { + defensive = 1; + } + rallyBool = 1; + if (allAssassins) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, + DAT_PathFindingState::ptr)(x1, y1, 0xffffffff, 0xffffffff, 500, 0); + } else if (climbing) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findPathUsingClimbingWithHeightMargin16, + DAT_PathFindingState::ptr)(x1, y1, 0xffffffff, 0xffffffff, 500, FALSE); + } else { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)(x1, y1, -1, -1, 500, FALSE); + } + if (!MACRO_CALL_MEMBER(Map::Units::TribesState_Func::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg, + this)(tribeID, DAT_PathFindingState::instance.searchGeneration)) { + do { + searchBudget += 500; + if (searchBudget >= 0x13a10 || ((climbing || defensive) && searchBudget >= 8000)) { + if (!DAT_PathFindingState::instance.field_0x98) { + if (allAssassins) { + if (!MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, + DAT_PathFindingState::ptr)(x1, y1, targetUnitX, targetUnitY, 100000, 0)) { + return 0; } - bVar6 = 1; - _algTile2 = DAT_PathFindingState::instance.ALG_TargetTile; - _algTile = DAT_PathFindingState::instance.ALG_ResultTile; - } else { - iVar7 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::setClimbBasedOnClosestClimbData, - DAT_PathFindingState::ptr)(_targetUnitID, (int)(y1), (int)(_area3)); - if (!iVar7) { - return (undefined4)(0); + } else if (climbing) { + if (!MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findPathUsingClimbingWithHeightMargin16, + DAT_PathFindingState::ptr)(x1, y1, targetUnitX, targetUnitY, 100000, FALSE)) { + return 0; } + } else if (!MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, + DAT_PathFindingState::ptr)( + x1, y1, targetUnitX, targetUnitY, 100000, FALSE)) { + return 0; } } - } else { - bVar4 = 1; - } - if (storeAsRallyPoint) { - this->tribes[tribeID].isRallyingUnk = (short)rallyBool; - this->tribes[tribeID].rallyPointArray[0][0] = DAT_UnitsState::instance.units[_targetUnitID].x; - this->tribes[tribeID].rallyPointArray[0][1] = DAT_UnitsState::instance.units[_targetUnitID].y; - this->tribes[tribeID].rallyPointArray[1][0] = (short)x1; - this->tribes[tribeID].rallyPointArray[1][1] = (short)_y; - this->tribes[tribeID].currentRallyPointIndex = 1; - this->tribes[tribeID].rallyPointCount = 2; - this->tribes[tribeID].containsRabbit = 0; + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe, + this)(tribeID); + goto planDestinations; } - this->ALG_ResultTileIndex = 0; - this->field_0x18 = 0; - int _isWallGatehouseTower = (DAT_TileMapState::instance.LogicLayer[_tile] & 0x100U) != 0; - int bVar5 = 0; - int _isKeep = (DAT_TileMapState::instance.LogicLayer[_tile] & 0x10000000U) != 0; - int _isDefensiveStructure = _isKeep || _isWallGatehouseTower; - rallyBool = 1; - if (bVar4) { + rallyBool = DAT_PathFindingState::instance.searchQueue.currentDistance; + if (allAssassins) { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, - DAT_PathFindingState::ptr)(x1, _y, 0xffffffff, 0xffffffff, 500, 0); - } else if (_bool1) { + DAT_PathFindingState::ptr)(x1, y1, 0xffffffff, 0xffffffff, searchBudget, 1); + } else if (climbing) { MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::findPathUsingClimbingWithHeightMargin16, - DAT_PathFindingState::ptr)(x1, _y, 0xffffffff, 0xffffffff, 500, FALSE); + DAT_PathFindingState::ptr)(x1, y1, 200, 0, searchBudget, TRUE); } else { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, - DAT_PathFindingState::ptr)(x1, _y, -1, -1, 500, FALSE); - } - _hasUnitInArea = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg, this)( - tribeID, DAT_PathFindingState::instance.searchGeneration); - if (!_hasUnitInArea) { - int _searchBudget = 500; - do { - _searchBudget = _searchBudget + 500; - if (0x13a10 <= _searchBudget - || ((_bool1 || (_isDefensiveStructure)) && (8000 <= _searchBudget))) { - if (!DAT_PathFindingState::instance.field_0x98) { - if (bVar4) { - BVar8 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, - DAT_PathFindingState::ptr)( - x1, _y, (uint)(_targetUnitX), (uint)(_targetUnitY), 100000, 0); - if (!BVar8) { - return (undefined4)(0); - } - } else if (_bool1) { - BVar8 = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: - findPathUsingClimbingWithHeightMargin16, - DAT_PathFindingState::ptr)( - x1, _y, (uint)(_targetUnitX), (uint)(_targetUnitY), 100000, FALSE); - if (!BVar8) { - return (undefined4)(0); - } - } else { - BVar8 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, - DAT_PathFindingState::ptr)( - x1, _y, _targetUnitX, _targetUnitY, 100000, FALSE); - if (!BVar8) { - return (undefined4)(0); - } - } - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func:: - applyMovementDistanceToUnitsInTribeBasedOnUnitNumberInTribe, - this)(tribeID); - goto LAB_00526788; - } - rallyBool = DAT_PathFindingState::instance.searchQueue.currentDistance; - if (bVar4) { - MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::pathFindingWithBuildingsIncluded, - DAT_PathFindingState::ptr)(x1, _y, 0xffffffff, 0xffffffff, _searchBudget, 1); - } else if (_bool1) { - MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findPathUsingClimbingWithHeightMargin16, - DAT_PathFindingState::ptr)(x1, _y, 200, 0, _searchBudget, TRUE); - } else { - MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func::findLinkageBasedPathOrWalkRadius, - DAT_PathFindingState::ptr)(x1, _y, -1, -1, _searchBudget, TRUE); - } - BVar8 = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg, this)( - tribeID, DAT_PathFindingState::instance.searchGeneration); - } while (!BVar8); + DAT_PathFindingState::ptr)(x1, y1, -1, -1, searchBudget, TRUE); } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMovementDistanceToUnitsInTribe, this)(tribeID); - LAB_00526788: - if (0 < this->tribes[tribeID].size) { - do { - /* - plan x and y for each unit in selection - */ - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _tribeUnitIndex); - _tribeUnitIndex = _tribeUnitIndex + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].usingTeleport == 0 - && DAT_UnitsState::instance.units[_unitID].field_0x413 == 0 - && DAT_UnitsState::instance.units[_unitID].state.generic != (UnitStateShort)0xcf - && DAT_UnitsState::instance.units[_unitID].rallyRelatedFlag == 0 - && DAT_UnitsState::instance.units[_unitID].moveRelatedFlag != 1 - && DAT_UnitsState::instance.units[_unitID].moveableUnk != 0 - && (_unitType = DAT_UnitsState::instance.units[_unitID].unitType, - _unitType != Map::Units::UT_S_TREBUCHET) - && _unitType != Map::Units::UT_S_MANGONEL && _unitType != Map::Units::UT_S_BALLISTA - && (_unitType != Map::Units::UT_S_CATAPULT - || (DAT_UnitsState::instance.units[_unitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 2)) - && (_unitType != Map::Units::UT_S_FBALLISTA - || (DAT_UnitsState::instance.units[_unitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 2)) - && (_unitType != Map::Units::UT_S_TOWER - || (DAT_UnitsState::instance.units[_unitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) - && (_unitType != Map::Units::UT_S_BATTERINGRAM - || (DAT_UnitsState::instance.units[_unitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) - && (_unitType != Map::Units::UT_S_SHIELD - || (DAT_UnitsState::instance.units[_unitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 1))) { - if (_unitType == Map::Units::UT_E_ENGINEER) { - DAT_UnitsState::instance.units[_unitID].field_0x3c4 = 0; - } - if (bVar4) { - if (!_isKeep && !_isWallGatehouseTower) { - if (bVar5) { - _modulo = 3; - } else { - /* - fixme:ucp:feature?: change formations - */ - dVar9 = MACRO_CALL_MEMBER( - Map::Navigation::PathFindingState_Func:: - findNextTileByModuloAmongPreviousSearchNotOnDefensiveStructure, - DAT_PathFindingState::ptr)(3, (int)(x1), (int)(_y)); - this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; - if (dVar9) - goto LAB_0052691a; - bVar5 = 1; - this->ALG_ResultTileIndex = 0; - _modulo = 3; - } - goto LAB_0052690c; - } - if (bVar5) { - LAB_00526908: - _modulo = 1; - goto LAB_0052690c; - } - dVar9 = MACRO_CALL_MEMBER( + } while ( + !MACRO_CALL_MEMBER(Map::Units::TribesState_Func::anyUnitsOfTribeAreOutsideCoverageOfPathFindingAlg, + this)(tribeID, DAT_PathFindingState::instance.searchGeneration)); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMovementDistanceToUnitsInTribe, this)(tribeID); + planDestinations: + if (0 < this->tribes[tribeID].size) { + do { + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, index); + ++index; + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0 + && DAT_UnitsState::instance.units[unitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[unitID].field_0x413 == 0 + && DAT_UnitsState::instance.units[unitID].state.generic != (UnitStateShort)0xcf + && DAT_UnitsState::instance.units[unitID].rallyRelatedFlag == 0 + && DAT_UnitsState::instance.units[unitID].moveRelatedFlag != 1 + && DAT_UnitsState::instance.units[unitID].moveableUnk != 0 + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_TREBUCHET + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_MANGONEL + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_BALLISTA + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_CATAPULT + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_FBALLISTA + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_TOWER + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_BATTERINGRAM + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_SHIELD + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 1)) { + if (DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_E_ENGINEER) { + DAT_UnitsState::instance.units[unitID].field_0x3c4 = 0; + } + if (allAssassins) { + if (defensive) { + if (storeAsRallyPoint == 0) { + int const found = MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::pathFindingResultBased, - DAT_PathFindingState::ptr)(1, (int)(x1), (int)(_y), rallyBool); - this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; - if (!dVar9) { - bVar5 = 1; + DAT_PathFindingState::ptr)(1, x1, y1, rallyBool); + ++this->ALG_ResultTileIndex; + if (!found) { + storeAsRallyPoint = 1; this->ALG_ResultTileIndex = 0; - goto LAB_00526908; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(1, x1, y1); + ++this->ALG_ResultTileIndex; } - } else { - if (this->tribes[tribeID].unkIsAnimalTribe == 0) { - if ((_isDefensiveStructure) || (_bool1)) - goto LAB_00526908; - if (_unitType == Map::Units::UT_S_CATAPULT - || _unitType == Map::Units::UT_S_FBALLISTA - || _unitType == Map::Units::UT_S_TOWER - || _unitType == Map::Units::UT_S_BATTERINGRAM - || _unitType == Map::Units::UT_S_SHIELD) { - _modulo = 4; - } else { - _modulo = 2; - } - } else { - _modulo = 3; - } - LAB_0052690c: + } else if (storeAsRallyPoint == 1) { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: findNextTileInExistingSearchThatIsModuloDistanceAway, - DAT_PathFindingState::ptr)(_modulo, (int)(x1), (int)(_y)); - this->ALG_ResultTileIndex = this->ALG_ResultTileIndex + 1; - } - LAB_0052691a: - if (3999 < this->ALG_ResultTileIndex) - break; - DAT_UnitsState::instance.units[_unitID].plannedDestinationX = (short)this->ALG_ResultX; - DAT_UnitsState::instance.units[_unitID].plannedDestinationY = (short)this->ALG_ResultY; - } - } while (_tribeUnitIndex < this->tribes[tribeID].size); - } - int _tribeIndex_2 = 0; - if (0 < this->tribes[tribeID].size) { - do { - _unitID_2 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - tribeID, _tribeIndex_2); - _tribeIndex_2 = _tribeIndex_2 + 1; - if (DAT_UnitsState::instance.units[_unitID_2].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID_2].dying == 0 - && DAT_UnitsState::instance.units[_unitID_2].usingTeleport == 0 - && DAT_UnitsState::instance.units[_unitID_2].field_0x413 == 0 - && (UVar1 = DAT_UnitsState::instance.units[_unitID_2].state.generic, - UVar1 != ((UnitStateShort)0xcf)) - && DAT_UnitsState::instance.units[_unitID_2].rallyRelatedFlag == 0 - && DAT_UnitsState::instance.units[_unitID_2].moveRelatedFlag != 1 - && DAT_UnitsState::instance.units[_unitID_2].moveableUnk != 0 - && (UVar2 = DAT_UnitsState::instance.units[_unitID_2].unitType, - UVar2 != Map::Units::UT_S_TREBUCHET) - && UVar2 != Map::Units::UT_S_MANGONEL && UVar2 != Map::Units::UT_S_BALLISTA - && (UVar2 != Map::Units::UT_S_CATAPULT - || (DAT_UnitsState::instance.units[_unitID_2] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 2)) - && (UVar2 != Map::Units::UT_S_FBALLISTA - || (DAT_UnitsState::instance.units[_unitID_2] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 2)) - && (UVar2 != Map::Units::UT_S_TOWER - || (DAT_UnitsState::instance.units[_unitID_2] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) - && (UVar2 != Map::Units::UT_S_BATTERINGRAM - || (DAT_UnitsState::instance.units[_unitID_2] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) - && (UVar2 != Map::Units::UT_S_SHIELD - || (DAT_UnitsState::instance.units[_unitID_2] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 1))) { - if (UVar1 == Map::Units::States::US_MELEE_ATTACK) { - DAT_UnitsState::instance.units[_unitID_2].movementCooldown - = DAT_UnitsState::instance.units[_unitID_2].movementSpeed * -4; + DAT_PathFindingState::ptr)(1, x1, y1); + ++this->ALG_ResultTileIndex; } - DAT_UnitsState::instance.units[_unitID_2].state.generic - = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[_unitID_2].movementType_OR_targetUnitID = 0; - DAT_UnitsState::instance.units[_unitID_2].targetedBuildingTile = 0; - if (bVar4) { - DAT_PathFindingState::instance.allAssassinsUnk = 1; - } else if (_bool1) { - DAT_PathFindingState::instance.notAllAssassinsUnk = 0; - } else { - DAT_PathFindingState::instance.notAllAssassinsUnk - = (int)(!DAT_EntityState::instance.fireCount); - } - DAT_UnitsState::instance.units[_unitID_2]._someX_2 = 0; - DAT_UnitsState::instance.units[_unitID_2]._someY_2 = 0; - MACRO_CALL_MEMBER(Synchrony::GameSynchronyState_Func::throttledMultiplayerSyncUpdate, - DAT_GameSynchronyState::ptr)(); - if (UVar1 == Map::Units::States::US_MELEE_ATTACK - || DAT_UnitsState::instance.units[_unitID_2].plannedDestinationX - != DAT_UnitsState::instance.units[_unitID_2].destinationXPosition - || DAT_UnitsState::instance.units[_unitID_2].plannedDestinationY - != DAT_UnitsState::instance.units[_unitID_2].destinationYPosition - || DAT_UnitsState::instance.units[_unitID_2].tunnelerFinishedDigging != 2) { - BVar8 = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(_unitID_2, - (uint)(( - int)((int)DAT_UnitsState::instance.units[_unitID_2].plannedDestinationX)), - (uint)(( - int)((int)DAT_UnitsState::instance.units[_unitID_2].plannedDestinationY)), - 0); - if (!BVar8) { - if (bVar6 - && (DAT_TileMapState::instance - .LogicLayer[DAT_UnitsState::instance.units[_unitID_2].tile] - & 0x10000100U)) { - /* - teleport? - */ - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, - DAT_UnitsState::ptr)(_unitID_2, - (uint)(_algTile2 - - DAT_ViewportRenderState::instance - .translationMatrix[DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[_algTile2]] - .addXgetTile), - (uint)(DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[_algTile2]), - 0); - DAT_UnitsState::instance.units[_unitID_2].state.generic - = Map::Units::States::US_APPEAR; - DAT_UnitsState::instance.units[_unitID_2] - .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID - = _algTile; - goto LAB_00526ce4; - } - } else { - LAB_00526ce4: - if (speedMatching != Map::Units::Instructions::UMSE_0) - goto LAB_00526cea; - } - } else { - LAB_00526cea: - DAT_UnitsState::instance.units[_unitID_2].unitSpeedMatchingRelatedUnk = 0x10; - } - DAT_PathFindingState::instance.notAllAssassinsUnk = 0; - if (!DAT_PathFindingState::instance.field_0x7c) { - this->field_0x18 = this->field_0x18 + 1; - } else { - this->field_0x18 = this->field_0x18 + -1; + } else if (storeAsRallyPoint == 0) { + int const found = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func:: + findNextTileByModuloAmongPreviousSearchNotOnDefensiveStructure, + DAT_PathFindingState::ptr)(3, x1, y1); + ++this->ALG_ResultTileIndex; + if (!found) { + storeAsRallyPoint = 1; + this->ALG_ResultTileIndex = 0; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(3, x1, y1); + ++this->ALG_ResultTileIndex; } + } else if (storeAsRallyPoint == 1) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(3, x1, y1); + ++this->ALG_ResultTileIndex; } - } while (_tribeIndex_2 < this->tribes[tribeID].size); + } else if (this->tribes[tribeID].unkIsAnimalTribe) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(3, x1, y1); + ++this->ALG_ResultTileIndex; + } else if (defensive || climbing) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(1, x1, y1); + ++this->ALG_ResultTileIndex; + } else if (DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_S_CATAPULT + || DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_S_FBALLISTA + || DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_S_TOWER + || DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_S_BATTERINGRAM + || DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_S_SHIELD) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(4, x1, y1); + ++this->ALG_ResultTileIndex; + } else { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + findNextTileInExistingSearchThatIsModuloDistanceAway, + DAT_PathFindingState::ptr)(2, x1, y1); + ++this->ALG_ResultTileIndex; + } + if ((int)this->ALG_ResultTileIndex >= 4000) { + break; + } + DAT_UnitsState::instance.units[unitID].plannedDestinationX = (short)this->ALG_ResultX; + DAT_UnitsState::instance.units[unitID].plannedDestinationY = (short)this->ALG_ResultY; } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyUnitTopSpeedDelayBasedOnTribeSize, this)( - tribeID, FALSE); - int _tribeIndex_4 = 0; - short _tribeSize = this->tribes[tribeID].size; - this->tribes[tribeID].someUnitID = 0; - if (0 < _tribeSize) { - do { - /* - set move delay - */ - _targetUnitID = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, _tribeIndex_4); - _tribeIndex_4 = _tribeIndex_4 + 1; - if (DAT_UnitsState::instance.units[_targetUnitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_targetUnitID].dying == 0 - && DAT_UnitsState::instance.units[_targetUnitID].usingTeleport == 0 - && DAT_UnitsState::instance.units[_targetUnitID].field_0x413 == 0 - && DAT_UnitsState::instance.units[_targetUnitID].state.generic != (UnitStateShort)0xcf - && DAT_UnitsState::instance.units[_targetUnitID].rallyRelatedFlag == 0 - && DAT_UnitsState::instance.units[_targetUnitID].moveRelatedFlag != 1 - && DAT_UnitsState::instance.units[_targetUnitID].moveableUnk != 0 - && (UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType, - UVar2 != Map::Units::UT_S_TREBUCHET - && (( - ((UVar2 != Map::Units::UT_S_MANGONEL - && (UVar2 != Map::Units::UT_S_BALLISTA)) - && ((UVar2 != Map::Units::UT_S_CATAPULT - || (DAT_UnitsState::instance.units[_targetUnitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 2)))) - && ((UVar2 != Map::Units::UT_S_FBALLISTA - || (DAT_UnitsState::instance.units[_targetUnitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 2)))))) - && (UVar2 != Map::Units::UT_S_TOWER - || (DAT_UnitsState::instance.units[_targetUnitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) - && (UVar2 != Map::Units::UT_S_BATTERINGRAM - || (DAT_UnitsState::instance.units[_targetUnitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 4)) - && (UVar2 != Map::Units::UT_S_SHIELD - || (DAT_UnitsState::instance.units[_targetUnitID] - .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 - == 1))) { - short sVar3 = DAT_UnitsState::instance.units[_targetUnitID].topSpeedDelayIndex; - short sVar10 - = (short)DAT_UnitSelectionDefinedData::instance.UnitInstructionMoveDelay[sVar3]; - if (this->tribes[tribeID].unkIsAnimalTribe == 0) { - if (-1 < this->field_0x18) { - sVar10 = sVar10 * 2; - } - } else { - sVar10 = sVar10 * 3; - } - DAT_UnitsState::instance.units[_targetUnitID].moveInstructionSpeedDelayTracker = sVar10; - if (this->field_0x18 < 0) { - DAT_UnitsState::instance.units[_targetUnitID].moveDelay = 0; - } else { - DAT_UnitsState::instance.units[_targetUnitID].moveDelay - = (short)(DAT_UnitSelectionDefinedData::instance.UnitMoveDelay[sVar3] / 2); - } - if (DAT_UnitsState::instance.units[_targetUnitID].movementCooldown != 0) { - DAT_UnitsState::instance.units[_targetUnitID].moveDelay = 0; + } while (index < this->tribes[tribeID].size); + } + index = 0; + if (0 < this->tribes[tribeID].size) { + do { + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, index); + ++index; + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0 + && DAT_UnitsState::instance.units[unitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[unitID].field_0x413 == 0 + && (unitState = DAT_UnitsState::instance.units[unitID].state.generic, + unitState != (UnitStateShort)0xcf) + && DAT_UnitsState::instance.units[unitID].rallyRelatedFlag == 0 + && DAT_UnitsState::instance.units[unitID].moveRelatedFlag != 1 + && DAT_UnitsState::instance.units[unitID].moveableUnk != 0 + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_TREBUCHET + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_MANGONEL + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_BALLISTA + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_CATAPULT + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_FBALLISTA + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_TOWER + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_BATTERINGRAM + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_SHIELD + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 1)) { + if (unitState == Map::Units::States::US_MELEE_ATTACK) { + DAT_UnitsState::instance.units[unitID].movementCooldown + = DAT_UnitsState::instance.units[unitID].movementSpeed * -4; + } + DAT_UnitsState::instance.units[unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[unitID].movementType_OR_targetUnitID = 0; + DAT_UnitsState::instance.units[unitID].targetedBuildingTile = 0; + if (allAssassins) { + DAT_PathFindingState::instance.allAssassinsUnk = 1; + } else if (climbing) { + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + } else { + DAT_PathFindingState::instance.notAllAssassinsUnk = !DAT_EntityState::instance.fireCount; + } + DAT_UnitsState::instance.units[unitID]._someX_2 = 0; + DAT_UnitsState::instance.units[unitID]._someY_2 = 0; + MACRO_CALL_MEMBER(Synchrony::GameSynchronyState_Func::throttledMultiplayerSyncUpdate, + DAT_GameSynchronyState::ptr)(); + if (unitState != Map::Units::States::US_MELEE_ATTACK + && DAT_UnitsState::instance.units[unitID].plannedDestinationX + == DAT_UnitsState::instance.units[unitID].destinationXPosition + && DAT_UnitsState::instance.units[unitID].plannedDestinationY + == DAT_UnitsState::instance.units[unitID].destinationYPosition + && DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging == 2) { + DAT_UnitsState::instance.units[unitID].unitSpeedMatchingRelatedUnk = 0x10; + } else { + if (!MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(unitID, + DAT_UnitsState::instance.units[unitID].plannedDestinationX, + DAT_UnitsState::instance.units[unitID].plannedDestinationY, 0)) { + if (!crossArea + || !(DAT_TileMapState::instance + .LogicLayer[DAT_UnitsState::instance.units[unitID].tile] + & 0x10000100)) { + goto unitMoved; } - DAT_UnitsState::instance.units[_targetUnitID].moveInstructionSpeedDelayTracker = 0; - DAT_UnitsState::instance.units[_targetUnitID].moveDelay = 0; - DAT_UnitsState::instance.units[_targetUnitID].horseArcherShootingVariation = 0; - DAT_UnitsState::instance.units[_targetUnitID].targetingType - = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; + MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(unitID, + crossAreaTargetTile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[crossAreaTargetTile]] + .addXgetTile, + DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[crossAreaTargetTile], + 0); + DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_APPEAR; + DAT_UnitsState::instance.units[unitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID + = crossAreaResultTile; } - } while (_tribeIndex_4 < this->tribes[tribeID].size); + if (speedMatching != Map::Units::Instructions::UMSE_0) { + DAT_UnitsState::instance.units[unitID].unitSpeedMatchingRelatedUnk = 0x10; + } + } + unitMoved: + DAT_PathFindingState::instance.notAllAssassinsUnk = 0; + if (!DAT_PathFindingState::instance.field_0x7c) { + ++this->field_0x18; + } else { + --this->field_0x18; + } } - return (undefined4)(1); - } + } while (index < this->tribes[tribeID].size); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyUnitTopSpeedDelayBasedOnTribeSize, this)( + tribeID, FALSE); + index = 0; + this->tribes[tribeID].someUnitID = 0; + if (0 < this->tribes[tribeID].size) { + do { + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + tribeID, index); + ++index; + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0 + && DAT_UnitsState::instance.units[unitID].usingTeleport == 0 + && DAT_UnitsState::instance.units[unitID].field_0x413 == 0 + && DAT_UnitsState::instance.units[unitID].state.generic != (UnitStateShort)0xcf + && DAT_UnitsState::instance.units[unitID].rallyRelatedFlag == 0 + && DAT_UnitsState::instance.units[unitID].moveRelatedFlag != 1 + && DAT_UnitsState::instance.units[unitID].moveableUnk != 0 + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_TREBUCHET + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_MANGONEL + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_BALLISTA + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_CATAPULT + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_FBALLISTA + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 2) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_TOWER + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_BATTERINGRAM + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 4) + && (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_S_SHIELD + || DAT_UnitsState::instance.units[unitID] + .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 + == 1)) { + delayIndex = DAT_UnitsState::instance.units[unitID].topSpeedDelayIndex; + if (this->tribes[tribeID].unkIsAnimalTribe) { + DAT_UnitsState::instance.units[unitID].moveInstructionSpeedDelayTracker + = (short)DAT_UnitSelectionDefinedData::instance.UnitInstructionMoveDelay[delayIndex] + * 3; + } else if ((int)this->field_0x18 >= 0) { + DAT_UnitsState::instance.units[unitID].moveInstructionSpeedDelayTracker + = (short)DAT_UnitSelectionDefinedData::instance.UnitInstructionMoveDelay[delayIndex] + * 2; + } else { + DAT_UnitsState::instance.units[unitID].moveInstructionSpeedDelayTracker + = (short)DAT_UnitSelectionDefinedData::instance.UnitInstructionMoveDelay[delayIndex]; + } + if ((int)this->field_0x18 >= 0) { + DAT_UnitsState::instance.units[unitID].moveDelay + = (short)(DAT_UnitSelectionDefinedData::instance.UnitMoveDelay[delayIndex] / 2); + } else { + DAT_UnitsState::instance.units[unitID].moveDelay = 0; + } + if (DAT_UnitsState::instance.units[unitID].movementCooldown != 0) { + DAT_UnitsState::instance.units[unitID].moveDelay = 0; + } + DAT_UnitsState::instance.units[unitID].moveInstructionSpeedDelayTracker = 0; + DAT_UnitsState::instance.units[unitID].moveDelay = 0; + DAT_UnitsState::instance.units[unitID].horseArcherShootingVariation = 0; + DAT_UnitsState::instance.units[unitID].targetingType + = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; + } + } while (index < this->tribes[tribeID].size); } - return (undefined4)(0); + return 1; } - } } } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6d462a7d2d..f48a3feb0f 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46961,7 +46961,7 @@ SHC_3BB0A8C1_0x005262A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00526370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005263A0 | 30.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x005263A0 | 49.7% | Stack slot and register assignment differ (this is kept in edi; the original spills it) SHC_3BB0A8C1_0x005266F0 | 0.0% | Pending From 101b8566d7b93aef6a1186aa2d544ae68af54720 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 05:12:58 +0100 Subject: [PATCH 193/202] Tribe::countdown: ushort -> short updateTribes decrements it as a signed word (0x52B961: add word ptr [ecx], bx with bx = -1); with the ushort type the structured updateTribes emits add word ptr [ecx], 0xffff instead. The other reimplemented users, updateLionWolfTribeBehavior and processEntityDamageToUnit, compile to the same code with either type. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Map/Units/Tribe.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/Tribe.hpp b/src/OpenSHC/Map/Units/Tribe.hpp index 6499c24a56..4f4da3fad8 100644 --- a/src/OpenSHC/Map/Units/Tribe.hpp +++ b/src/OpenSHC/Map/Units/Tribe.hpp @@ -90,7 +90,7 @@ namespace Map { short field_0x288; // 0x00000288 length: 2 short unknownBool02; // 0x0000028A length: 2 short unknownBool01; // 0x0000028C length: 2 - ushort countdown; // 0x0000028E length: 2 + short countdown; // 0x0000028E length: 2 short someUnitArrayIndex; // 0x00000290 length: 2 short someIndex; // 0x00000292 length: 2 short attackWave; // 0x00000294 length: 2 From 88e71caad725f558751a920bf0d1a1f9e5731831 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 05:12:59 +0100 Subject: [PATCH 194/202] isTribeUnitBlockedByOtherUnit: 91.2% -> 100% The skip label goes in front of the for(;;) instead of inside it: MSVC then computes unitID * 0x490 once, as the original does. A continue (or the label inside the loop) makes it rotate the loop or recompute the index. Also: class include first, named parameter, no redundant casts. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../isTribeUnitBlockedByOtherUnit.cpp | 52 +++++++------------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp index a68829bc89..3d9b2fd5d1 100644 --- a/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/isTribeUnitBlockedByOtherUnit.cpp @@ -1,6 +1,5 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/States/UnitState.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" @@ -11,53 +10,40 @@ namespace OpenSHC { namespace Map { namespace Units { - using Map::Units::UnitLogicState; - using Map::Units::States::UnitState; - // FUNCTION: STRONGHOLDCRUSADER 0x00524B30 - BOOLEnum TribesState::isTribeUnitBlockedByOtherUnit(int param_1) + BOOLEnum TribesState::isTribeUnitBlockedByOtherUnit(int tribeID) { - int unitSelectionIndex = 0; - int const size = this->tribes[param_1].size; - /* - The skip is a goto to the top of the loop body, not a `continue`. A - `continue` (or a `while (index < size)`) lets MSVC rotate the loop: the size - test is copied in front of it as a guard and moved to the bottom. The - original keeps the one test at the top, as this form does (89.5% -> 98.3% in - quick_diff). One difference is left: ours computes `unitID * 0x490` again - after the dying test, and the original computes it once. Writing the two - tests as separate gotos, as a nested do-while, or as an else-if chain does - not remove the second multiply. - */ + int index = 0; + int const size = this->tribes[tribeID].size; + // a `continue` lets MSVC rotate the loop and copy the size test; the label before the loop keeps one test + nextUnit: for (;;) { - nextUnit: - if (unitSelectionIndex >= size) { + if (index >= size) { return FALSE; } - int const unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( - param_1, unitSelectionIndex); - unitSelectionIndex = unitSelectionIndex + 1; - if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL - || DAT_UnitsState::instance.units[unitID].dying != 0) { + int const unitID = MACRO_CALL_MEMBER(TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, index); + index++; + if (DAT_UnitsState::instance.units[unitID].logicalState != ULS_NORMAL + || DAT_UnitsState::instance.units[unitID].dying) { goto nextUnit; } - if (DAT_UnitsState::instance.units[unitID].usingTeleport != 0) { + if (DAT_UnitsState::instance.units[unitID].usingTeleport) { return FALSE; } - if (DAT_UnitsState::instance.units[unitID].field_0x413 != 0) { + if (DAT_UnitsState::instance.units[unitID].field_0x413) { return FALSE; } if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging == 2) { return FALSE; } - if (DAT_UnitsState::instance.units[unitID].state.generic == Map::Units::States::US_MELEE_ATTACK) { + if (DAT_UnitsState::instance.units[unitID].state.generic == States::US_MELEE_ATTACK) { return FALSE; } - if (DAT_UnitsState::instance.units[unitID].laddermanIsInPosition != '\0') { + if (DAT_UnitsState::instance.units[unitID].laddermanIsInPosition) { return FALSE; } int const otherUnitID - = (int)(short)DAT_TileMapState::instance.UnitLayer[DAT_UnitsState::instance.units[unitID].tile]; + = DAT_TileMapState::instance.UnitLayer[DAT_UnitsState::instance.units[unitID].tile]; if (otherUnitID != 0 && unitID != otherUnitID && DAT_UnitsState::instance.units[otherUnitID].tunnelerFinishedDigging != 2) { return TRUE; @@ -65,6 +51,6 @@ namespace Map { } } - } -} -} + } // namespace Units +} // namespace Map +} // namespace OpenSHC diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 2eaf481aba..86ff09d898 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46903,7 +46903,7 @@ SHC_3BB0A8C1_0x00524A10 | 0.0% | Pending SHC_3BB0A8C1_0x00524A30 | 0.0% | Pending -SHC_3BB0A8C1_0x00524B30 | 91.2% | Register allocation: unitID * 0x490 recomputed after the dying test +SHC_3BB0A8C1_0x00524B30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00524BF0 | 100.0% | Reimplemented From bf1a4d0681b2065291a6eca318d3f2c8be5f5865 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 05:13:25 +0100 Subject: [PATCH 195/202] predictUnitInterceptPosition: 84.6% -> 91.4% Both tribe speeds are read into their own short locals before the clamps, instead of reusing one local; that puts the second load in the original's place. Below 95: the original reads units[unitID].tribeID ahead of the first clamp's branch but the tribe's speed only after it. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/predictUnitInterceptPosition.cpp | 13 +++++++------ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp index 4a021ce9ed..9045f69e59 100644 --- a/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp +++ b/src/OpenSHC/Map/Units/TribesState/predictUnitInterceptPosition.cpp @@ -23,15 +23,16 @@ namespace Map { if (DAT_UnitsState::instance.units[unitID].tunnelerFinishedDigging != 2) { return; } - short speed = this->tribes[DAT_UnitsState::instance.units[targetUnitID].tribeID].minimumMovementSpeed; - int targetFactor = speed; - if (speed <= 1) { + short const targetSpeed + = this->tribes[DAT_UnitsState::instance.units[targetUnitID].tribeID].minimumMovementSpeed; + short const unitSpeed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; + int targetFactor = targetSpeed; + if (targetSpeed <= 1) { targetFactor = 1; } targetFactor *= 4; - speed = this->tribes[DAT_UnitsState::instance.units[unitID].tribeID].minimumMovementSpeed; - int unitStep = speed; - if (speed <= 1) { + int unitStep = unitSpeed; + if (unitSpeed <= 1) { unitStep = 1; } unitStep *= 4; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 86ff09d898..977e5e3a45 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46389,7 +46389,7 @@ SHC_3BB0A8C1_0x00521240 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521280 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00521500 | 84.6% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00521500 | 91.4% | Reimplemented; the original loads the second tribe ID before the first speed clamp but the tribe field after it SHC_3BB0A8C1_0x00521690 | 0.0% | Pending From 3bbe7e04c939a00a99eae938134a2c708d5e7fa1 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 05:13:26 +0100 Subject: [PATCH 196/202] updateTribes: structured switches, no gotos (stays 91.2%) The human-player branch gets its own switch over the four animal tribe types instead of gotos into the AI switch's case labels; the compiler merges the shared calls the same way. Casts on the unnamed tribe types are plain case numbers, the countdown decrements are plain --. Still 91.2%: the original has two identical markTribeAsAnimalTribe arms (0x52B90A, 0x52B914) that MSVC tail-merges here. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/updateTribes.cpp | 201 ++++++++---------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 91 insertions(+), 112 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp index 7c26c9603a..8217bc7b59 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribes.cpp @@ -1,8 +1,7 @@ -#include "../../../Map.func.hpp" +#include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/TribesState.func.hpp" #include "OpenSHC/Map/Units/TroopValueState.func.hpp" -#include "OpenSHC/AI/Tribes/AIVUnitTypeTribeArrayOffset.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" #include "OpenSHC/Game/GameMode.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" @@ -15,130 +14,110 @@ namespace OpenSHC { namespace Map { namespace Units { - using Game::GameMode; - // FUNCTION: STRONGHOLDCRUSADER 0x0052B860 void TribesState::updateTribes() { - short* psVar1; - uint _tickBasedNumberUpTo32; - _tickBasedNumberUpTo32 = DAT_GameCore::instance.mapTimeInTicks & 0x1f; - DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation - = DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation + -1; + uint const tickSlot = DAT_GameCore::instance.mapTimeInTicks & 0x1f; + DAT_GameState::instance.mapAndTime.eventCountdownRabbitInfestation--; DAT_CurrentTribeID::instance = 1; do { if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 2) { - if (0 < this->tribes[DAT_CurrentTribeID::instance].countdown2) { - this->tribes[DAT_CurrentTribeID::instance].countdown2 - = this->tribes[DAT_CurrentTribeID::instance].countdown2 + -1; + if (this->tribes[DAT_CurrentTribeID::instance].countdown2 > 0) { + this->tribes[DAT_CurrentTribeID::instance].countdown2--; } - if ((DAT_CurrentTribeID::instance & 0x1f) == _tickBasedNumberUpTo32) { - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::computeTribePercentages, this)( + if ((DAT_CurrentTribeID::instance & 0x1f) == tickSlot) { + MACRO_CALL_MEMBER(TribesState_Func::computeTribePercentages, this)( DAT_CurrentTribeID::instance); } - if ((DAT_GameSynchronyState::instance + if (DAT_GameSynchronyState::instance .currentPlayerFullIDArray[this->tribes[DAT_CurrentTribeID::instance].owner] - != -1) - || (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY)) { - /* - For humans, do: - */ + != -1 + || DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) { + // human players: only the animal tribes keep their own behaviour switch (this->tribes[DAT_CurrentTribeID::instance].tribeType) { - case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN: - goto switchD_0052b92a_caseD_d; - case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN: - goto switchD_0052b92a_caseD_e; - case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN: - goto switchD_0052b92a_caseD_f; - case ((AITribeType)0x10): - goto switchD_0052b92a_caseD_10; + case 13: + MACRO_CALL_MEMBER(TribesState_Func::processDeerMoving, this)(DAT_CurrentTribeID::instance); + break; + case 14: + MACRO_CALL_MEMBER(TribesState_Func::updateLionWolfTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case 15: + MACRO_CALL_MEMBER(TribesState_Func::updateRabbitTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case 16: + MACRO_CALL_MEMBER(TribesState_Func::updateCamelTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; default: - goto switchD_0052b92a_caseD_4; + MACRO_CALL_MEMBER(TribesState_Func::updateTribeCombatStanceBehavior, this)( + DAT_CurrentTribeID::instance); + break; + } + } else { + switch (this->tribes[DAT_CurrentTribeID::instance].tribeType) { + case AI::Tribes::AITT_TUNNELERS: + case AI::Tribes::AITT_ARCHERS: + case AI::Tribes::AITT_LADDERMEN: + case AI::Tribes::AITT_SPEARMEN: + case AI::Tribes::AITT_PIKEMEN: + case AI::Tribes::AITT_CROSSBOWMEN: + case AI::Tribes::AITT_SWORDSMEN: + case AI::Tribes::AITT_MACEMEN: + case AI::Tribes::AITT_KNIGHTS: + case AI::Tribes::AITT_ENGINEERS: + case AI::Tribes::AITT_HOPS_FARMER_UNK: + case 19: + case 20: + case 21: + case AI::Tribes::AITT_CATAPULT: + case AI::Tribes::AITT_TREBUCHET: + case AI::Tribes::AITT_FIRE_BALLISTA: + case AI::Tribes::AITT_ARAB_ARCHER: + case AI::Tribes::AITT_ARAB_SLAVE: + case AI::Tribes::AITT_ARAB_SLINGER: + case AI::Tribes::AITT_ARAB_ASSASSIN: + case AI::Tribes::AITT_ARAB_HORSE_ARCHER: + case AI::Tribes::AITT_ARAB_SWORDSMAN: + case AI::Tribes::AITT_ARAB_FIRETHROWER: + MACRO_CALL_MEMBER(TroopValueState_Func::updateTribeBehaviorBasedOnBehaviorType, + DAT_TroopValueState::ptr)(DAT_CurrentTribeID::instance); + break; + case 13: + MACRO_CALL_MEMBER(TribesState_Func::processDeerMoving, this)(DAT_CurrentTribeID::instance); + break; + case 14: + MACRO_CALL_MEMBER(TribesState_Func::updateLionWolfTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case 15: + MACRO_CALL_MEMBER(TribesState_Func::updateRabbitTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case 16: + MACRO_CALL_MEMBER(TribesState_Func::updateCamelTribeBehavior, this)( + DAT_CurrentTribeID::instance); + break; + case 17: + case 18: + MACRO_CALL_MEMBER(TribesState_Func::markTribeAsAnimalTribe, this)( + DAT_CurrentTribeID::instance); + break; + default: + MACRO_CALL_MEMBER(TribesState_Func::updateTribeCombatStanceBehavior, this)( + DAT_CurrentTribeID::instance); + break; } } - switch (this->tribes[DAT_CurrentTribeID::instance].tribeType) { - case AI::Tribes::AITT_TUNNELERS: - case AI::Tribes::AITT_ARCHERS: - case AI::Tribes::AITT_LADDERMEN: - case AI::Tribes::AITT_SPEARMEN: - case AI::Tribes::AITT_PIKEMEN: - case AI::Tribes::AITT_CROSSBOWMEN: - case AI::Tribes::AITT_SWORDSMEN: - case AI::Tribes::AITT_MACEMEN: - case AI::Tribes::AITT_KNIGHTS: - case AI::Tribes::AITT_ENGINEERS: - case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_LADDERMEN: - case ((AITribeType)0x13): - case ((AITribeType)0x14): - case ((AITribeType)0x15): - case ((AITribeType)0x16): - case ((AITribeType)0x17): - case ((AITribeType)0x18): - case ((AITribeType)0x19): - case ((AITribeType)0x1a): - case ((AITribeType)0x1b): - case ((AITribeType)0x1c): - case ((AITribeType)0x1d): - case AI::Tribes::OFFSET_CROSSBOWMAN: - case ((AITribeType)0x1f): - MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::updateTribeBehaviorBasedOnBehaviorType, - DAT_TroopValueState::ptr)(DAT_CurrentTribeID::instance); - break; - case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN: - switchD_0052b92a_caseD_d: - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::processDeerMoving, this)( - DAT_CurrentTribeID::instance); - break; - case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN: - switchD_0052b92a_caseD_e: - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateLionWolfTribeBehavior, this)( - DAT_CurrentTribeID::instance); - break; - case AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN: - switchD_0052b92a_caseD_f: - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateRabbitTribeBehavior, this)( - DAT_CurrentTribeID::instance); - break; - case ((AITribeType)0x10): - switchD_0052b92a_caseD_10: - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateCamelTribeBehavior, this)( - DAT_CurrentTribeID::instance); - break; - /* - These two arms are the whole of this function's remaining gap, and it - is not reachable. The original's jump table at 0x52b9a0 sends - tribeType 0x11 to 0x52b90a and 0x12 to 0x52b914, ten bytes apart, and - both blocks are byte-for-byte the same work: - - push eax; mov ecx, esi; call 0x523730; jmp 0x52b961 - - So the original carries two identical blocks that its compiler did not - tail-merge, and ours merges them - which is the 0xa every branch - displacement in this function is out by. Nothing in the source - distinguishes two identical arms without changing what they do. - */ - case ((AITribeType)0x11): - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::markTribeAsAnimalTribe, this)( - DAT_CurrentTribeID::instance); - break; - case ((AITribeType)0x12): - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::markTribeAsAnimalTribe, this)( - DAT_CurrentTribeID::instance); - break; - default: - switchD_0052b92a_caseD_4: - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateTribeCombatStanceBehavior, this)( - DAT_CurrentTribeID::instance); - } - psVar1 = (short*)&this->tribes[DAT_CurrentTribeID::instance].countdown; if (this->tribes[DAT_CurrentTribeID::instance].countdown != 0) { - *psVar1 = *psVar1 + -1; + this->tribes[DAT_CurrentTribeID::instance].countdown--; } } - DAT_CurrentTribeID::instance = DAT_CurrentTribeID::instance + 1; - } while ((int)DAT_CurrentTribeID::instance < 1250); + DAT_CurrentTribeID::instance++; + } while ((int)DAT_CurrentTribeID::instance < countof(this->tribes)); } - } -} -} + } // namespace Units +} // namespace Map +} // namespace OpenSHC diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 977e5e3a45..5f4ed81185 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49149,7 +49149,7 @@ SHC_3BB0A8C1_0x0052B390 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B630 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented, asm differs +SHC_3BB0A8C1_0x0052B860 | 91.2% | Reimplemented; the original keeps two identical markTribeAsAnimalTribe arms (tribe types 17 and 18) that MSVC merges here, which shifts every branch by 10 bytes SHC_3BB0A8C1_0x0052B9A0 | 0.0% | Pending From 5757082c0774e9f76eae4531d190ee1e97d4aeda Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 05:38:03 +0100 Subject: [PATCH 197/202] TribesState: record the remaining asm differences of four draft functions Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- status/addresses-SHC-3BB0A8C1.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 5f4ed81185..55cb251e0b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46783,7 +46783,7 @@ SHC_3BB0A8C1_0x00522360 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522390 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522410 | 64.1% | zero register, asm differs +SHC_3BB0A8C1_0x00522410 | 64.1% | the original keeps zero in ecx and saves only esi/edi; ours keeps zero in edi and needs ebx SHC_3BB0A8C1_0x005224E0 | 100.0% | Reimplemented @@ -46895,7 +46895,7 @@ SHC_3BB0A8C1_0x005244D0 | 69.0% | Register allocation: x/y swapped; the merged a SHC_3BB0A8C1_0x00524890 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524930 | 58.7% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524930 | 58.7% | the original shares the argument pushes of the two non-horse path calls and keeps the sort index in ecx; ours pushes per call SHC_3BB0A8C1_0x005249C0 | 0.0% | Pending @@ -46907,7 +46907,7 @@ SHC_3BB0A8C1_0x00524B30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00524BF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00524CA0 | 66.1% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00524CA0 | 66.1% | the original keeps the unit index in a stack slot and tribeID in ebp, and inlines the FALSE epilogue after the first guard SHC_3BB0A8C1_0x00524D80 | 0.0% | Pending @@ -46973,7 +46973,7 @@ SHC_3BB0A8C1_0x005270F0 | 58.6% | this held in a register where the original spi SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs -SHC_3BB0A8C1_0x00527440 | 60.5% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527440 | 60.5% | the original lays out the game-mode checks after the first call inline; ours moves them and loads the arguments in another order SHC_3BB0A8C1_0x005275A0 | 0.0% | Pending From 650fe1392fa5b2086c3309e38f8cbe37c115c568 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 06:16:10 +0100 Subject: [PATCH 198/202] drawFlagsAndUnitDestinations: 78.1% -> 82.5% Rally point read into y then x, the flag sum in the original's order with the animation frame signed, decompiler names replaced, redundant casts dropped. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../drawFlagsAndUnitDestinations.cpp | 169 +++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 83 insertions(+), 88 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp index 34a7ac3fad..a83cb6a891 100644 --- a/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp +++ b/src/OpenSHC/Map/Units/TribesState/drawFlagsAndUnitDestinations.cpp @@ -30,13 +30,13 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00523A30 void TribesState::drawFlagsAndUnitDestinations(int tribeID) { - UnitInstructionTypeShort UVar2; - int _unitID; - int iVar4; - short (*_pRallyPoints)[2]; + UnitInstructionTypeShort targetingType; + int unitID; + int cursorValue; + short (*rallyPoint)[2]; int tile; - short _isRallying; - _isRallying = this->tribes[tribeID].isRallyingUnk; + short isRallying; + isRallying = this->tribes[tribeID].isRallyingUnk; /* Three nested tests, not one chain: the original has three separate forward jumps (0x523a53, 0x523a62, 0x523a7d). The player ID is loaded first and the @@ -44,44 +44,38 @@ namespace Map { the starting index is a branch rather than a `setl` (`test ax, ax`, `jge`, `mov esi, 1`). */ - if (_isRallying && DAT_GameSynchronyState::instance.currentPlayerSlotID == this->tribes[tribeID].owner) { - int _rallyIndex = 0; - if (_isRallying < 0) { - _rallyIndex = 1; + if (isRallying && DAT_GameSynchronyState::instance.currentPlayerSlotID == this->tribes[tribeID].owner) { + int rallyIndex = 0; + if (isRallying < 0) { + rallyIndex = 1; } - if (_rallyIndex < (int)this->tribes[tribeID].rallyPointCount) { - _pRallyPoints = this->tribes[tribeID].rallyPointArray + _rallyIndex; + if (rallyIndex < this->tribes[tribeID].rallyPointCount) { + rallyPoint = this->tribes[tribeID].rallyPointArray + rallyIndex; do { - /* - spawn the rally point flag - */ + int const y = (*rallyPoint)[1]; + int const x = (*rallyPoint)[0]; + // spawn the rally point flag MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, DAT_ViewportRenderState::ptr)(IO::Graphics::GID_FLOATS_NEW, - (int)(((*_pRallyPoints)[0] + (int)DAT_TileMapState::instance.flagAnimationFrame - + _rallyIndex + (*_pRallyPoints)[1]) - % 10 - + 0x61), - 0x12, -1, - (int)(DAT_ViewportRenderState::instance.translationMatrix[(*_pRallyPoints)[1]].addXgetTile - + (*_pRallyPoints)[0]), - 0xa0022); - _rallyIndex = _rallyIndex + 1; - _pRallyPoints = _pRallyPoints + 1; - } while (_rallyIndex < (int)this->tribes[tribeID].rallyPointCount); + (x + (int)DAT_TileMapState::instance.flagAnimationFrame + rallyIndex + y) % 10 + 0x61, 0x12, + -1, DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x, 0xa0022); + rallyIndex++; + rallyPoint++; + } while (rallyIndex < this->tribes[tribeID].rallyPointCount); } } - int iVar5 = 0; + int index = 0; if (0 < this->tribes[tribeID].size) { do { - _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)( - tribeID, iVar5); - iVar5 = iVar5 + 1; - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].unknownTestAgainst0_2 == 0 - && DAT_UnitsState::instance.units[_unitID].owner + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getSpecificUnitFromTribe, this)( + tribeID, index); + index++; + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].dying == 0 + && DAT_UnitsState::instance.units[unitID].unknownTestAgainst0_2 == 0 + && DAT_UnitsState::instance.units[unitID].owner == DAT_GameSynchronyState::instance.currentPlayerSlotID) { - short sVar1 = DAT_UnitsState::instance.units[_unitID].unitSpeedMatchingRelatedUnk; + short speedMatching = DAT_UnitsState::instance.units[unitID].unitSpeedMatchingRelatedUnk; /* The speed-matching arm is the fall-through. The original reads the field and tests it sixteen bits wide, then jumps 0x91 forward @@ -89,108 +83,108 @@ namespace Map { the speed cursor follows the test and the planned-destination arm goes in the far block. */ - if (sVar1) { + if (speedMatching) { /* The zero is the fall-through. The original has `cmp dx, 8; jl 0x4` - jumping over `xor eax, eax` into the - scaling - so `iVar4 = 0` sits under the test and the - `(8 - speed) * 4` arm comes after it. Written `sVar1 < 8` + scaling - so `cursorValue = 0` sits under the test and the + `(8 - speed) * 4` arm comes after it. Written `speedMatching < 8` first they swap and the test inverts to `jge`. */ - if (sVar1 >= 8) { - iVar4 = 0; + if (speedMatching >= 8) { + cursorValue = 0; } else { - iVar4 = (8 - sVar1) * 4; + cursorValue = (8 - speedMatching) * 4; } - tile = (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX + tile = DAT_UnitsState::instance.units[unitID].plannedDestinationX + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID].plannedDestinationY] + .translationMatrix[DAT_UnitsState::instance.units[unitID].plannedDestinationY] .addXgetTile; MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, DAT_ViewportRenderState::ptr)( - IO::Graphics::GID_CURSORS, (int)(0x6a - sVar1), 10, 6, tile, iVar4 << 0x10 | 2); - iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; + IO::Graphics::GID_CURSORS, 0x6a - speedMatching, 10, 6, tile, cursorValue << 0x10 | 2); + cursorValue = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; /* - Negated rather than written `7 < iVar4`: the original has + Negated rather than written `7 < cursorValue`: the original has `cmp eax, 8` with a `jl` jumping over the subtraction, and MSVC emits the constant it is handed, so the greater-than spelling gives `cmp eax, 7; jle` instead. Same test, and writing it this way is worth 1.7% here (55.9 -> 57.6). */ - if (!(iVar4 < 8)) { - iVar4 = 0xf - iVar4; + if (!(cursorValue < 8)) { + cursorValue = 0xf - cursorValue; } MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, - DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, tile, - (int)(DAT_UnitsState::instance.units[_unitID].unitSpeedMatchingRelatedUnk << 0x11 | 2)); + DAT_ViewportRenderState::ptr)(IO::Graphics::GID_CURSORS, cursorValue + 0x52, 0xc, 6, + tile, DAT_UnitsState::instance.units[unitID].unitSpeedMatchingRelatedUnk << 0x11 | 2); } else { - if (DAT_UnitsState::instance.units[_unitID].currentIndexInPathPlan - < DAT_UnitsState::instance.units[_unitID].totalSizeOfPathPlan - && (UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType, - UVar2 != Map::Units::UIT_UNIT_ATTACK_UNIT) - && UVar2 != Map::Units::UIT_ATTACK_LAND - && UVar2 != Map::Units::UIT_MAN_SIEGE_EQUIPMENT) { - tile = (int)DAT_UnitsState::instance.units[_unitID].plannedDestinationX + if (DAT_UnitsState::instance.units[unitID].currentIndexInPathPlan + < DAT_UnitsState::instance.units[unitID].totalSizeOfPathPlan + && (targetingType = DAT_UnitsState::instance.units[unitID].targetingType, + targetingType != Map::Units::UIT_UNIT_ATTACK_UNIT) + && targetingType != Map::Units::UIT_ATTACK_LAND + && targetingType != Map::Units::UIT_MAN_SIEGE_EQUIPMENT) { + tile = DAT_UnitsState::instance.units[unitID].plannedDestinationX + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID] - .plannedDestinationY] + .translationMatrix[DAT_UnitsState::instance.units[unitID].plannedDestinationY] .addXgetTile; - iVar4 = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; - if (!(iVar4 < 8)) { - iVar4 = 0xf - iVar4; + cursorValue = DAT_TileMapState::instance.cursorOverlayAnimationFrame + -1; + if (!(cursorValue < 8)) { + cursorValue = 0xf - cursorValue; } MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingLayerElement, DAT_ViewportRenderState::ptr)( - IO::Graphics::GID_CURSORS, iVar4 + 0x52, 0xc, 6, tile, 2); + IO::Graphics::GID_CURSORS, cursorValue + 0x52, 0xc, 6, tile, 2); } } - UVar2 = DAT_UnitsState::instance.units[_unitID].targetingType; - if (UVar2 == Map::Units::UIT_ATTACK_LAND || UVar2 == Map::Units::UIT_THROW_COW) { + targetingType = DAT_UnitsState::instance.units[unitID].targetingType; + if (targetingType == Map::Units::UIT_ATTACK_LAND + || targetingType == Map::Units::UIT_THROW_COW) { MACRO_CALL_MEMBER(Map::TileMapState_Func::spawnFloatingNumberAroundTile, - DAT_TileMapState::ptr)((int)DAT_UnitsState::instance.units[_unitID].attackAtTileX, - (int)(DAT_UnitsState::instance.units[_unitID].attackAtTileY), - (int)(DAT_UnitsState::instance.units[_unitID].unkAttackRelated)); - } else if (UVar2 == Map::Units::UIT_FILL_MOAT) { + DAT_TileMapState::ptr)(DAT_UnitsState::instance.units[unitID].attackAtTileX, + DAT_UnitsState::instance.units[unitID].attackAtTileY, + DAT_UnitsState::instance.units[unitID].unkAttackRelated); + } else if (targetingType == Map::Units::UIT_FILL_MOAT) { MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingTextElement, DAT_ViewportRenderState::ptr)(0x4e, 6, -0x20, - (int)DAT_UnitsState::instance.units[_unitID].targetX + DAT_UnitsState::instance.units[unitID].targetX + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] + .translationMatrix[DAT_UnitsState::instance.units[unitID].targetY] .addXgetTile, 2); - } else if (UVar2 == Map::Units::UIT_DIG_MOAT) { + } else if (targetingType == Map::Units::UIT_DIG_MOAT) { MACRO_CALL_MEMBER(Rendering::ViewportRenderState_Func::createFloatingTextElement, DAT_ViewportRenderState::ptr)(0x4e, 6, -0x20, - (int)DAT_UnitsState::instance.units[_unitID].targetX + DAT_UnitsState::instance.units[unitID].targetX + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[_unitID].targetY] + .translationMatrix[DAT_UnitsState::instance.units[unitID].targetY] .addXgetTile, 2); - } else if (UVar2 == Map::Units::UIT_UNIT_ATTACK_UNIT) { - int const targetUnitID = DAT_UnitsState::instance.units[_unitID] + } else if (targetingType == Map::Units::UIT_UNIT_ATTACK_UNIT) { + int const targetUnitID = DAT_UnitsState::instance.units[unitID] .targetedUnitID__OR__engineerMannedSiegeEngineRef; if ((DAT_UnitsState::instance.units[targetUnitID].uid - == DAT_UnitsState::instance.units[_unitID] + == DAT_UnitsState::instance.units[unitID] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID) && (DAT_UnitsState::instance.units[targetUnitID].logicalState == Map::Units::ULS_NORMAL)) { DAT_UnitsState::instance.units[targetUnitID].field_0x6c = 1; } - } else if (UVar2 == Map::Units::UIT_ATTACK_BUILDING) { + } else if (targetingType == Map::Units::UIT_ATTACK_BUILDING) { short const buildingID - = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; + = DAT_UnitsState::instance.units[unitID].targetID_OR_targetBuildingID; if ((buildingID) && (DAT_BuildingsState::instance.buildings[buildingID].uid - == DAT_UnitsState::instance.units[_unitID] + == DAT_UnitsState::instance.units[unitID] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { DAT_BuildingsState::instance.buildings[buildingID].targetMarker = 1; } - } else if (UVar2 == Map::Units::UIT_LIGHT_PITCH) { + } else if (targetingType == Map::Units::UIT_LIGHT_PITCH) { short const pitchDitchID - = DAT_UnitsState::instance.units[_unitID].targetID_OR_targetBuildingID; + = DAT_UnitsState::instance.units[unitID].targetID_OR_targetBuildingID; if ((pitchDitchID) && (DAT_TileMapState::instance.pitchDitches[pitchDitchID].uid - == DAT_UnitsState::instance.units[_unitID] + == DAT_UnitsState::instance.units[unitID] .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID)) { DAT_TileMapState::instance .MiscDisplayLayer[DAT_TileMapState::instance.pitchDitches[pitchDitchID].tile] @@ -199,14 +193,15 @@ namespace Map { | 0x4000; } } else { - uint uVar3 = DAT_UnitsState::instance.units[_unitID].targetedBuildingTile; - if ((uVar3) && (DAT_TileMapState::instance.BuildingLayer[uVar3] != 0)) { - DAT_BuildingsState::instance.buildings[DAT_TileMapState::instance.BuildingLayer[uVar3]] + uint buildingTile = DAT_UnitsState::instance.units[unitID].targetedBuildingTile; + if ((buildingTile) && (DAT_TileMapState::instance.BuildingLayer[buildingTile] != 0)) { + DAT_BuildingsState::instance + .buildings[DAT_TileMapState::instance.BuildingLayer[buildingTile]] .targetMarker = 1; } } } - } while (iVar5 < this->tribes[tribeID].size); + } while (index < this->tribes[tribeID].size); } return; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 55cb251e0b..162684c1b8 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46861,7 +46861,7 @@ SHC_3BB0A8C1_0x005237F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523920 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00523A30 | 78.1% | Constant 1 in edi with the unit index on the stack; operand order in the rally-flag sum +SHC_3BB0A8C1_0x00523A30 | 82.5% | Constant 1 in edi with the unit index on the stack SHC_3BB0A8C1_0x00523B00 | 0.0% | Pending From 89e18df026a75652b08fa050d850b52f42b5b3d0 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 06:16:10 +0100 Subject: [PATCH 199/202] giveTribeMoveInstruction: 49.7% -> 52.6% The tribe is reached through one Tribe reference instead of 21 subscripts. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/giveTribeMoveInstruction.cpp | 45 ++++++++++--------- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp index 47d2584405..fdab39bc54 100644 --- a/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp +++ b/src/OpenSHC/Map/Units/TribesState/giveTribeMoveInstruction.cpp @@ -81,7 +81,8 @@ namespace Map { if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y1 * 400 + x1] == 0) { return 0; } - int targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + Tribe& tribe = this->tribes[tribeID]; + int targetUnitID = tribe.selectionTargetUnitID; int const targetUnitX = DAT_UnitsState::instance.units[targetUnitID].x; int const targetUnitY = DAT_UnitsState::instance.units[targetUnitID].y; int targetArea @@ -99,13 +100,13 @@ namespace Map { == MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getFirstUnitInTribeThatIsOnXTerrain, this)( tribeID)) { targetArea = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::canNavigateFunctionReturnsArea, - DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, area, - DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y); + DAT_PathFindingState::ptr)(tribe.owner, area, DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y); if (targetArea == 0) { return MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)( tribeID, x1, y1, rallyBool, storeAsRallyPoint); } - targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + targetUnitID = tribe.selectionTargetUnitID; climbing = 1; } else if (targetArea == 0) { return MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMoveCommandOrRallyCommandToTribe, this)( @@ -121,7 +122,7 @@ namespace Map { = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::tribeContainsUnitThatCanClimb, this)(tribeID); int const climbArea = MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::calculateCanPlayerUnitsNavigateToAreaFromArea, - DAT_PathFindingState::ptr)(this->tribes[tribeID].owner, targetArea, area, canClimb); + DAT_PathFindingState::ptr)(tribe.owner, targetArea, area, canClimb); if (climbArea == 0) { if (!MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::someBinaryAlgFunctionPathFinding, DAT_PathFindingState::ptr)(targetUnitID)) { @@ -140,14 +141,14 @@ namespace Map { } } if (storeAsRallyPoint) { - this->tribes[tribeID].isRallyingUnk = (short)rallyBool; - this->tribes[tribeID].rallyPointArray[0][0] = DAT_UnitsState::instance.units[targetUnitID].x; - this->tribes[tribeID].rallyPointArray[0][1] = DAT_UnitsState::instance.units[targetUnitID].y; - this->tribes[tribeID].rallyPointArray[1][0] = (short)x1; - this->tribes[tribeID].rallyPointArray[1][1] = (short)y1; - this->tribes[tribeID].currentRallyPointIndex = 1; - this->tribes[tribeID].rallyPointCount = 2; - this->tribes[tribeID].containsRabbit = 0; + tribe.isRallyingUnk = (short)rallyBool; + tribe.rallyPointArray[0][0] = DAT_UnitsState::instance.units[targetUnitID].x; + tribe.rallyPointArray[0][1] = DAT_UnitsState::instance.units[targetUnitID].y; + tribe.rallyPointArray[1][0] = (short)x1; + tribe.rallyPointArray[1][1] = (short)y1; + tribe.currentRallyPointIndex = 1; + tribe.rallyPointCount = 2; + tribe.containsRabbit = 0; } this->ALG_ResultTileIndex = 0; this->field_0x18 = 0; @@ -217,7 +218,7 @@ namespace Map { } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyMovementDistanceToUnitsInTribe, this)(tribeID); planDestinations: - if (0 < this->tribes[tribeID].size) { + if (0 < tribe.size) { do { unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, index); @@ -297,7 +298,7 @@ namespace Map { DAT_PathFindingState::ptr)(3, x1, y1); ++this->ALG_ResultTileIndex; } - } else if (this->tribes[tribeID].unkIsAnimalTribe) { + } else if (tribe.unkIsAnimalTribe) { MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: findNextTileInExistingSearchThatIsModuloDistanceAway, DAT_PathFindingState::ptr)(3, x1, y1); @@ -328,10 +329,10 @@ namespace Map { DAT_UnitsState::instance.units[unitID].plannedDestinationX = (short)this->ALG_ResultX; DAT_UnitsState::instance.units[unitID].plannedDestinationY = (short)this->ALG_ResultY; } - } while (index < this->tribes[tribeID].size); + } while (index < tribe.size); } index = 0; - if (0 < this->tribes[tribeID].size) { + if (0 < tribe.size) { do { unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, index); @@ -432,13 +433,13 @@ namespace Map { --this->field_0x18; } } - } while (index < this->tribes[tribeID].size); + } while (index < tribe.size); } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyUnitTopSpeedDelayBasedOnTribeSize, this)( tribeID, FALSE); index = 0; - this->tribes[tribeID].someUnitID = 0; - if (0 < this->tribes[tribeID].size) { + tribe.someUnitID = 0; + if (0 < tribe.size) { do { unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, index); @@ -475,7 +476,7 @@ namespace Map { .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 == 1)) { delayIndex = DAT_UnitsState::instance.units[unitID].topSpeedDelayIndex; - if (this->tribes[tribeID].unkIsAnimalTribe) { + if (tribe.unkIsAnimalTribe) { DAT_UnitsState::instance.units[unitID].moveInstructionSpeedDelayTracker = (short)DAT_UnitSelectionDefinedData::instance.UnitInstructionMoveDelay[delayIndex] * 3; @@ -502,7 +503,7 @@ namespace Map { DAT_UnitsState::instance.units[unitID].targetingType = Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk; } - } while (index < this->tribes[tribeID].size); + } while (index < tribe.size); } return 1; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 162684c1b8..86ded3ffb7 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46961,7 +46961,7 @@ SHC_3BB0A8C1_0x005262A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00526370 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005263A0 | 49.7% | Stack slot and register assignment differ (this is kept in edi; the original spills it) +SHC_3BB0A8C1_0x005263A0 | 52.6% | Stack slot and register assignment differ (this is kept in edi; the original spills it) SHC_3BB0A8C1_0x005266F0 | 0.0% | Pending From 836dbc80266f9c30e3c33cd39b974ac24cbfc168 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 06:16:11 +0100 Subject: [PATCH 200/202] assignAttackTargetsForTribe: no goto (stays 69.0%) The scale-point arm no longer jumps over the move-to-destination store; the store and the wide-value tail are guarded by targetType != STBT_0x3f4. Same code as the goto form. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../assignAttackTargetsForTribe.cpp | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp index 845eb17093..2729690e1c 100644 --- a/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp @@ -112,7 +112,6 @@ namespace Map { y = DAT_TroopValueState::instance.y; tile = DAT_TroopValueState::instance.tile; DAT_UnitsState::instance.units[unitID].state.generic = (UnitState)0x67; - goto moveToTarget; } else if (targetType == Map::Units::STBT_0x3f3) { if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestAvailableScalePoint, DAT_TroopValueState::ptr)(unitID)) { @@ -180,30 +179,33 @@ namespace Map { } else { continue; } - DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; - if (targetType == Map::Units::STBT_0x414) { - uint const freeTile = MACRO_CALL_MEMBER( - Map::Units::UnitsState_Func::findFreeTileNearby, DAT_UnitsState::ptr)(unitID, tile); - if (freeTile == 0) { + if (targetType != Map::Units::STBT_0x3f4) { + DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; + if (targetType == Map::Units::STBT_0x414) { + uint const freeTile = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::findFreeTileNearby, DAT_UnitsState::ptr)(unitID, tile); + if (freeTile == 0) { + continue; + } + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + unitID, + freeTile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[freeTile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[freeTile], 0); + short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY; + DAT_UnitsState::instance.units[unitID].targetID_OR_targetBuildingID = (short)tile; + DAT_UnitsState::instance.units[unitID].targetingType = Map::Units::UIT_ATTACK_WALL; + DAT_UnitsState::instance.units[unitID].state.generic + = Map::Units::States::US_MOVE_TO_DESTINATION; + DAT_UnitsState::instance.units[unitID].attackAtTileX = (short)tile + - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; continue; } - MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(unitID, - freeTile - - DAT_ViewportRenderState::instance - .translationMatrix[DAT_ViewportRenderState::instance - .tileTranslationMatrix_YComponent[freeTile]] - .addXgetTile, - DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[freeTile], 0); - short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; - DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY; - DAT_UnitsState::instance.units[unitID].targetID_OR_targetBuildingID = (short)tile; - DAT_UnitsState::instance.units[unitID].targetingType = Map::Units::UIT_ATTACK_WALL; - DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; - DAT_UnitsState::instance.units[unitID].attackAtTileX - = (short)tile - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile; - continue; } - moveToTarget: { short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY; DAT_UnitsState::instance.units[unitID].targetedBuildingTile = tile; @@ -224,7 +226,6 @@ namespace Map { this->tribes[tribeID].targetX = (short)x; this->tribes[tribeID].targetY = (short)y; } - } } while (index < this->tribes[tribeID].size); return TRUE; } From af57a70499936048953b6212bcc16c16486e8321 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 06:16:11 +0100 Subject: [PATCH 201/202] updateTribeCombatStanceBehavior: names instead of decompiler locals (stays 81.2%) Dead locals removed, redundant casts dropped, labels named. A fully structured version (flags or two calls per arm) measures 0.54-0.68 against 0.85 in quick_diff, so the gotos into the shared tails stay. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../updateTribeCombatStanceBehavior.cpp | 209 +++++++++--------- 1 file changed, 104 insertions(+), 105 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp index 8b0ac6bfb7..194d200e0e 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateTribeCombatStanceBehavior.cpp @@ -32,22 +32,19 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x0052A7D0 void TribesState::updateTribeCombatStanceBehavior(int tribeID) { - short* psVar1; - UnitTypeShort UVar2; - int _unitID; - BOOLEnum BVar3; - int iVar4; - int _targetUnitID; - int iVar5; + short* counter; + UnitTypeShort targetUnitType; + int enemyUnitID; + BOOLEnum arrived; + int unitID2; + int targetUnitID; + int index; uint x1; uint y1; - int local_c; - int _buildingID; - int _buildingIndex; - short _buildingIndex_2; - UnitInstructionType _unitInstructionType; - _targetUnitID = (int)this->tribes[tribeID].selectionTargetUnitID; - _buildingIndex_2 = this->tribes[tribeID].someUnitID; + int i; + short shortValue; + targetUnitID = this->tribes[tribeID].selectionTargetUnitID; + shortValue = this->tribes[tribeID].someUnitID; /* The non-zero arm comes first. The original tests the id sixteen bits wide and skips forward when it is zero - `test ax, ax; je 0x8d` - so the block @@ -56,77 +53,78 @@ namespace Map { UIT_UNIT_ATTACK_UNIT call, is emitted past the stance loop and every branch in it is displaced. */ - if (_buildingIndex_2) { - iVar5 = (int)_buildingIndex_2; - if (DAT_UnitsState::instance.units[iVar5].uid == this->tribes[tribeID].someUnitUID) { - if ((int)DAT_UnitsState::instance.units[iVar5].destinationX_2Unk + if (shortValue) { + index = (int)shortValue; + if (DAT_UnitsState::instance.units[index].uid == this->tribes[tribeID].someUnitUID) { + if ((int)DAT_UnitsState::instance.units[index].destinationX_2Unk + DAT_ViewportRenderState::instance - .translationMatrix[DAT_UnitsState::instance.units[iVar5].destinationY_2Unk] + .translationMatrix[DAT_UnitsState::instance.units[index].destinationY_2Unk] .addXgetTile == this->tribes[tribeID].someTile2) { - _buildingIndex_2 = this->tribes[tribeID].field_0x2be; - if (_buildingIndex_2) { - this->tribes[tribeID].field_0x2be = _buildingIndex_2 + -1; - goto LAB_0052aa77; + shortValue = this->tribes[tribeID].field_0x2be; + if (shortValue) { + this->tribes[tribeID].field_0x2be = shortValue + -1; + goto updateCounters; } - if (DAT_UnitsState::instance.units[iVar5].tile == this->tribes[tribeID].someTile) - goto LAB_0052aa77; + if (DAT_UnitsState::instance.units[index].tile == this->tribes[tribeID].someTile) + goto updateCounters; } MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - tribeID, Map::Units::UIT_UNIT_ATTACK_UNIT, iVar5, this->tribes[tribeID].someUnitUID, 1); + tribeID, Map::Units::UIT_UNIT_ATTACK_UNIT, index, this->tribes[tribeID].someUnitUID, 1); } else { this->tribes[tribeID].someUnitID = 0; } } else { if (this->tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { - local_c = 0; + i = 0; do { - iVar5 = ((this->tribes[tribeID].someUnitArrayIndex - local_c) + 10) % 10; - _unitID = (int)this->tribes[tribeID].someUnitArray[iVar5]; - if (_unitID) { - if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[_unitID].dying == 0 - && DAT_UnitsState::instance.units[_unitID].uid - == this->tribes[tribeID].someUnitUIDArray[iVar5] + index = ((this->tribes[tribeID].someUnitArrayIndex - i) + 10) % 10; + enemyUnitID = (int)this->tribes[tribeID].someUnitArray[index]; + if (enemyUnitID) { + if (DAT_UnitsState::instance.units[enemyUnitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[enemyUnitID].dying == 0 + && DAT_UnitsState::instance.units[enemyUnitID].uid + == this->tribes[tribeID].someUnitUIDArray[index] && DAT_GameState::instance.mapAndTime - .playerTeams[DAT_UnitsState::instance.units[_targetUnitID].owner] + .playerTeams[DAT_UnitsState::instance.units[targetUnitID].owner] != DAT_GameState::instance.mapAndTime - .playerTeams[DAT_UnitsState::instance.units[_unitID].owner] + .playerTeams[DAT_UnitsState::instance.units[enemyUnitID].owner] && DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[_targetUnitID].tile] + .PathConnectionLayer[DAT_UnitsState::instance.units[targetUnitID].tile] == DAT_TileMapState::instance - .PathConnectionLayer[DAT_UnitsState::instance.units[_unitID].tile]) { - iVar5 = (int)DAT_UnitsState::instance.units[_targetUnitID].microXPosition - - (int)DAT_UnitsState::instance.units[_unitID].microXPosition; - iVar4 = (int)DAT_UnitsState::instance.units[_targetUnitID].microYPosition - - (int)DAT_UnitsState::instance.units[_unitID].microYPosition; - if (iVar5 * iVar5 + iVar4 * iVar4 < 250000) { + .PathConnectionLayer[DAT_UnitsState::instance.units[enemyUnitID].tile]) { + index = (int)DAT_UnitsState::instance.units[targetUnitID].microXPosition + - (int)DAT_UnitsState::instance.units[enemyUnitID].microXPosition; + unitID2 = (int)DAT_UnitsState::instance.units[targetUnitID].microYPosition + - (int)DAT_UnitsState::instance.units[enemyUnitID].microYPosition; + if (index * index + unitID2 * unitID2 < 250000) { if ((this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 0) && (this->tribes[tribeID].aggressiveStanceCounter == 0x3b)) { - iVar5 = 0; + index = 0; if (this->tribes[tribeID].size < 1) - goto LAB_0052aa55; - goto LAB_0052a9f2; + goto attackEnemyUnit; + goto checkIdleMeleeUnits; } break; } } else { - this->tribes[tribeID].someUnitArray[iVar5] = 0; + this->tribes[tribeID].someUnitArray[index] = 0; } } - local_c = local_c + 1; - } while (local_c < 10); + i = i + 1; + } while (i < 10); } } - goto LAB_0052aa77; - LAB_0052a9f2: + goto updateCounters; + checkIdleMeleeUnits: do { - iVar4 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar5); - iVar5 = iVar5 + 1; - if (DAT_UnitsState::instance.units[iVar4].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar4].dying == 0 - && DAT_UnitsState::instance.units[iVar4].state.generic != Map::Units::States::US_MELEE_ATTACK) { - switch (DAT_UnitsState::instance.units[iVar4].unitType) { + unitID2 + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, index); + index = index + 1; + if (DAT_UnitsState::instance.units[unitID2].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID2].dying == 0 + && DAT_UnitsState::instance.units[unitID2].state.generic != Map::Units::States::US_MELEE_ATTACK) { + switch (DAT_UnitsState::instance.units[unitID2].unitType) { case Map::Units::UT_TUNNELER: case Map::Units::UT_E_SPEAR: case Map::Units::UT_E_PIKE: @@ -135,24 +133,24 @@ namespace Map { case Map::Units::UT_E_KNIGHT: case Map::Units::UT_E_MONK: case Map::Units::UT_S_BATTERINGRAM: - if (DAT_UnitsState::instance.units[iVar4].SA == 0) - goto LAB_0052aa77; + if (DAT_UnitsState::instance.units[unitID2].SA == 0) + goto updateCounters; } } - } while (iVar5 < this->tribes[tribeID].size); - LAB_0052aa55: + } while (index < this->tribes[tribeID].size); + attackEnemyUnit: MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - tribeID, ((UnitInstructionType)0x20), _unitID, DAT_UnitsState::instance.units[_unitID].uid, 0); - LAB_0052aa77: - psVar1 = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; - *psVar1 = *psVar1 + 1; + tribeID, ((UnitInstructionType)0x20), enemyUnitID, DAT_UnitsState::instance.units[enemyUnitID].uid, 0); + updateCounters: + counter = (short*)&this->tribes[tribeID].unknownAttackRelatedUpdateCounter; + *counter = *counter + 1; /* Both counters are tested through the `short*` that incremented them. The original re-reads each field after the add (`movzx eax, word ptr [esi + 0x56]`, `cmp ax, 0x32`, signed `jle`). A fresh read of the ushort field folds into `cmp word ptr [mem]` instead, and the test is unsigned. */ - if (0x32 < *psVar1) { + if (0x32 < *counter) { /* The rallying arm comes first: `xor ebx, ebx; cmp word ptr [esi + 0x22e], bx; je 0x109` jumps away when the flag is clear, so the tribe-is-rallying @@ -160,40 +158,41 @@ namespace Map { block. */ if (this->tribes[tribeID].isRallyingUnk) { - BVar3 = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::allUnitsReachedTheirDestination, this)( + arrived = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::allUnitsReachedTheirDestination, this)( tribeID); /* The import had this guard with an empty body; nothing advances the rally point until the tribe has arrived. */ - if (BVar3) { - _buildingIndex_2 = this->tribes[tribeID].rallyPointCount; - iVar5 = this->tribes[tribeID].currentRallyPointIndex + 1; + if (arrived) { + shortValue = this->tribes[tribeID].rallyPointCount; + index = this->tribes[tribeID].currentRallyPointIndex + 1; this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - if (iVar5 >= _buildingIndex_2) { + if (index >= shortValue) { if (this->tribes[tribeID].isRallyingUnk == -1) { this->tribes[tribeID].currentRallyPointIndex = 0; this->tribes[tribeID].isRallyingUnk = 0; } - iVar5 = 0; + index = 0; } if (this->tribes[tribeID].isRallyingUnk != 0) { - x1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][0]; - y1 = (uint)this->tribes[tribeID].rallyPointArray[iVar5][1]; - this->tribes[tribeID].currentRallyPointIndex = (short)iVar5; - UVar2 = DAT_UnitsState::instance.units[_targetUnitID].unitType; - if (UVar2 == Map::Units::UT_E_ARCHER || UVar2 == Map::Units::UT_E_XBOW - || UVar2 == Map::Units::UT_A_ARCHER || UVar2 == Map::Units::UT_A_SLINGER - || UVar2 == Map::Units::UT_A_FIRETHROWER) { - if (0x1b0 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) { + x1 = (uint)this->tribes[tribeID].rallyPointArray[index][0]; + y1 = (uint)this->tribes[tribeID].rallyPointArray[index][1]; + this->tribes[tribeID].currentRallyPointIndex = (short)index; + targetUnitType = DAT_UnitsState::instance.units[targetUnitID].unitType; + if (targetUnitType == Map::Units::UT_E_ARCHER || targetUnitType == Map::Units::UT_E_XBOW + || targetUnitType == Map::Units::UT_A_ARCHER + || targetUnitType == Map::Units::UT_A_SLINGER + || targetUnitType == Map::Units::UT_A_FIRETHROWER) { + if (0x1b0 < DAT_UnitsState::instance.units[targetUnitID].closestEnemyMicroDistance) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( tribeID, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); } - } else if (UVar2 == Map::Units::UT_A_HARCHER) { + } else if (targetUnitType == Map::Units::UT_A_HARCHER) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( tribeID, x1, y1, 0, 0); - } else if ((0x60 < DAT_UnitsState::instance.units[_targetUnitID].closestEnemyMicroDistance) + } else if ((0x60 < DAT_UnitsState::instance.units[targetUnitID].closestEnemyMicroDistance) || (this->tribes[tribeID].unitStance != Map::Units::Behavior::USE_AGGRESSIVE)) { MACRO_CALL_MEMBER( Map::Units::TribesState_Func::giveUnitSelectionMoveInstructionNoMatchedSpeed, this)( @@ -203,27 +202,27 @@ namespace Map { } } else { if (this->tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { - psVar1 = &this->tribes[tribeID].aggressiveStanceCounter; - *psVar1 = *psVar1 + 1; + counter = &this->tribes[tribeID].aggressiveStanceCounter; + *counter = *counter + 1; /* The import had this guard with an empty body, so the counter was reset and the arm entered on every tick. The original's `cmp ax, 0x3c; jl` skips the whole arm. */ - if (0x3c <= *psVar1) { - _buildingIndex_2 = this->tribes[tribeID].size; + if (0x3c <= *counter) { + shortValue = this->tribes[tribeID].size; this->tribes[tribeID].aggressiveStanceCounter = 0; - if (0 < _buildingIndex_2) { - iVar5 = 0; + if (0 < shortValue) { + index = 0; do { - iVar4 = MACRO_CALL_MEMBER( - Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, iVar5); - iVar5 = iVar5 + 1; - if (DAT_UnitsState::instance.units[iVar4].logicalState == Map::Units::ULS_NORMAL - && DAT_UnitsState::instance.units[iVar4].dying == 0 - && DAT_UnitsState::instance.units[iVar4].state.generic + unitID2 = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, index); + index = index + 1; + if (DAT_UnitsState::instance.units[unitID2].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID2].dying == 0 + && DAT_UnitsState::instance.units[unitID2].state.generic != Map::Units::States::US_MELEE_ATTACK) { - switch (DAT_UnitsState::instance.units[iVar4].unitType) { + switch (DAT_UnitsState::instance.units[unitID2].unitType) { case Map::Units::UT_TUNNELER: case Map::Units::UT_E_SPEAR: case Map::Units::UT_E_PIKE: @@ -235,22 +234,22 @@ namespace Map { case Map::Units::UT_A_SLAVE: case Map::Units::UT_A_ASSASSIN: case Map::Units::UT_A_SWORDSMAN: - if (DAT_UnitsState::instance.units[iVar4].SA == 0) - goto LAB_0052ac8d; + if (DAT_UnitsState::instance.units[unitID2].SA == 0) + goto checkScatterMove; } } - } while (iVar5 < this->tribes[tribeID].size); + } while (index < this->tribes[tribeID].size); } MACRO_CALL_MEMBER( Map::Navigation::PathFindingState_Func::aggressiveStanceTargetBuildingAtRange, - DAT_PathFindingState::ptr)(_targetUnitID, (int)(15)); + DAT_PathFindingState::ptr)(targetUnitID, 15); if (DAT_PathFindingState::instance.ALG_ResultTile) { - _buildingIndex_2 = DAT_TileMapState::instance - .BuildingLayer[DAT_PathFindingState::instance.ALG_ResultTile]; - if (_buildingIndex_2) { + shortValue = DAT_TileMapState::instance + .BuildingLayer[DAT_PathFindingState::instance.ALG_ResultTile]; + if (shortValue) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( - tribeID, (UnitInstructionType)0x26, (int)_buildingIndex_2, - DAT_BuildingsState::instance.buildings[_buildingIndex_2].uid, 0); + tribeID, (UnitInstructionType)0x26, (int)shortValue, + DAT_BuildingsState::instance.buildings[shortValue].uid, 0); } else { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, this)( tribeID, (UnitInstructionType)0x25, @@ -260,7 +259,7 @@ namespace Map { } } } - LAB_0052ac8d: + checkScatterMove: if (this->tribes[tribeID].pendingScatterMove != 0) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::moveTribeToIndexedNearbyTile, this)(tribeID); this->tribes[tribeID].pendingScatterMove = 0; From db90886935dbd19ad7451c729937f75705bf0813 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 06:17:33 +0100 Subject: [PATCH 202/202] TribesState: record the remaining asm differences of two more draft functions Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- status/addresses-SHC-3BB0A8C1.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 86ded3ffb7..0474ace7c2 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46971,7 +46971,7 @@ SHC_3BB0A8C1_0x00526F00 | 90.3% | Register allocation: this and index swap edi/e SHC_3BB0A8C1_0x005270F0 | 58.6% | this held in a register where the original spills it and keeps the loop index in ebp -SHC_3BB0A8C1_0x00527330 | 75.0% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527330 | 75.0% | the original compares the 16-bit fields against the zero held in bx and keeps the repeated time test unfolded SHC_3BB0A8C1_0x00527440 | 60.5% | the original lays out the game-mode checks after the first call inline; ours moves them and loads the arguments in another order @@ -47085,7 +47085,7 @@ SHC_3BB0A8C1_0x00527C74 | 0.0% | Pending SHC_3BB0A8C1_0x00527C78 | 0.0% | Pending -SHC_3BB0A8C1_0x00527C80 | 60.8% | Reimplemented, asm differs +SHC_3BB0A8C1_0x00527C80 | 60.8% | register allocation; caching the tribe base (Tribe& or a shifted pointer) costs about 5 points, see the notes in the source SHC_3BB0A8C1_0x005292C0 | 0.0% | Pending