From a6a0fc166105be999a237e921f0231e63fdbc090 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:35 +0100 Subject: [PATCH 01/35] TribesState: moveTribeToIndexedNearbyTile returns void The function 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 its only caller (updateTribeCombatStanceBehavior, 0x52AC9A) ignores it. The reimplementation in the next commits is `void`, and with main's `int` declaration it does not compile (C2440 in the resolver), so the declaration and the resolver type in TribesState.func.hpp change with it. 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 4d3a96f46fd79a5d94c324e152b3a2112242d419 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:47 +0100 Subject: [PATCH 02/35] reimplement: SHC_3BB0A8C1_0x00522590 100.0% Reimplemented TribesState::addUnitToTribe from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/addUnitToTribe.cpp | 30 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp new file mode 100644 index 0000000000..bcc81557d5 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -0,0 +1,30 @@ +#include "../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) + { + ++this->tribes[tribeID].size; + 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 = unitID; + } + DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = this->tribes[tribeID].selectionTargetUnitID; + 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(TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index b31ea5fc2b..49439ef583 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00522590 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522630 | 0.0% | Pending From e4da26b9423f3f45d5ad8030450a12fbec37fa80 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:48 +0100 Subject: [PATCH 03/35] reimplement: SHC_3BB0A8C1_0x00522890 100.0% Reimplemented TribesState::createTribe from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/createTribe.cpp | 58 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/createTribe.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp new file mode 100644 index 0000000000..be75699ee8 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -0,0 +1,58 @@ +#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; + + /* + 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) + { + int tribeID = 0; + for (DAT_CurrentTribeID::instance = 1; DAT_CurrentTribeID::instance < countof(this->tribes); + ++DAT_CurrentTribeID::instance) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0) { + tribeID = DAT_CurrentTribeID::instance; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(Tribe), 0, &this->tribes[tribeID]); + break; + } + } + 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; + 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 49439ef583..1547f576b0 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46799,7 +46799,7 @@ SHC_3BB0A8C1_0x00522720 | 0.0% | Pending SHC_3BB0A8C1_0x005227E0 | 0.0% | Pending -SHC_3BB0A8C1_0x00522890 | 0.0% | Pending +SHC_3BB0A8C1_0x00522890 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522950 | 100.0% | Reimplemented From a21fd8571d325d05702fd871c18178bcb3a4c158 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:48 +0100 Subject: [PATCH 04/35] reimplement: SHC_3BB0A8C1_0x005227E0 100.0% Reimplemented TribesState::createTribeForPlayer from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/createTribeForPlayer.cpp | 52 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp new file mode 100644 index 0000000000..0a4ba121c6 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -0,0 +1,52 @@ +#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; + + /* + 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) + { + int tribeID; + for (tribeID = countof(this->tribes) - playerID; tribeID > 0; 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; + } + } + 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; + 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 1547f576b0..35e489c043 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -46797,7 +46797,7 @@ SHC_3BB0A8C1_0x00522630 | 0.0% | Pending SHC_3BB0A8C1_0x00522720 | 0.0% | Pending -SHC_3BB0A8C1_0x005227E0 | 0.0% | Pending +SHC_3BB0A8C1_0x005227E0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522890 | 100.0% | Reimplemented From 0790e915d731a80f22621482d5590e71dbe32e4c Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:49 +0100 Subject: [PATCH 05/35] reimplement: SHC_3BB0A8C1_0x00522720 100.0% Reimplemented TribesState::getAvailableTribeID from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/getAvailableTribeID.cpp | 61 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp new file mode 100644 index 0000000000..caf8a890d7 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -0,0 +1,61 @@ +#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 { + + /* + 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 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) { + tribeID = 0; + for (DAT_CurrentTribeID::instance = countof(this->tribes) - 1; DAT_CurrentTribeID::instance > 0; + --DAT_CurrentTribeID::instance) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0 + && this->tribes[DAT_CurrentTribeID::instance].time == 0) { + tribeID = DAT_CurrentTribeID::instance; + break; + } + } + if (tribeID <= 0) { + return 0; + } + this->tribes[tribeID].time = 1; + return tribeID; + } + for (tribeID = countof(this->tribes) - playerID, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; + 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) { + return 0; + } + /* + 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; + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 35e489c043..ceb78423d7 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 | 0.0% | Pending -SHC_3BB0A8C1_0x00522720 | 0.0% | Pending +SHC_3BB0A8C1_0x00522720 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005227E0 | 100.0% | Reimplemented From 272541709e15004b9b038a958a1effec59a201f2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:50 +0100 Subject: [PATCH 06/35] reimplement: SHC_3BB0A8C1_0x00522390 100.0% Reimplemented TribesState::getUnitIDForIndexInTribe from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/getUnitIDForIndexInTribe.cpp | 42 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp new file mode 100644 index 0000000000..374a2cdbf6 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -0,0 +1,42 @@ +#include "../TribesState.func.hpp" + +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 count = 0; + if (this->tribes[tribeID].tribeState != 2) { + return 0; + } + if (unitSelectionIndex >= this->tribes[tribeID].size) { + return 0; + } + for (int wordIndex = 0; wordIndex < countof(this->tribes[tribeID].unitSelectionBitMasked); wordIndex++) { + short const bits = this->tribes[tribeID].unitSelectionBitMasked[wordIndex]; + if (bits == 0) { + continue; + } + for (int bitIndex = 0; bitIndex < 16; bitIndex++) { + if (((1 << bitIndex) & bits) == 0) { + continue; + } + if (count == unitSelectionIndex) { + return wordIndex * 16 + bitIndex; + } + count++; + } + } + return 0; + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index ceb78423d7..1eff998ba8 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00522390 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522410 | 0.0% | Pending From 5a0a4115cb1d14f8b4d08d3f72138652f56a8173 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:50 +0100 Subject: [PATCH 07/35] reimplement: SHC_3BB0A8C1_0x00521280 100.0% Reimplemented TribesState::moveUnitToBehaviorTarget from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/moveUnitToBehaviorTarget.cpp | 142 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp new file mode 100644 index 0000000000..56f11b8ee6 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -0,0 +1,142 @@ +#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 { + + /* + 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 behaviorType) + { + uint y = 0; + uint x = 0; + uint tile = 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 + || DAT_UnitsState::instance.units[unitID].field_0x413 != 0 + || DAT_GameSynchronyState::instance + .currentPlayerFullIDArray[DAT_UnitsState::instance.units[unitID].owner] + != -1) { + return FALSE; + } + if (behaviorType == 0x3f2) { + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( + unitID)) { + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } + } else if (behaviorType == 0x3f5) { + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, + DAT_TroopValueState::ptr)(unitID)) { + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } + } else if (behaviorType == 0x3fb) { + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, + DAT_TroopValueState::ptr)(unitID)) { + 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; + } + } else if (behaviorType == 0x3fd) { + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)( + unitID)) { + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } + } else if (behaviorType == 0x3f6) { + if (MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findEnemyTowersOrGates, DAT_TroopValueState::ptr)(unitID)) { + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } + } else if (behaviorType == 0x413) { + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID)) { + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } + } else if (behaviorType == 0x414) { + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, + DAT_TroopValueState::ptr)(unitID)) { + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } + } else if (behaviorType == 0x3f7) { + if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestDiggableMoatPoint, + DAT_TroopValueState::ptr)(unitID)) { + x = DAT_TroopValueState::instance.x; + y = DAT_TroopValueState::instance.y; + tile = DAT_TroopValueState::instance.tile; + found = TRUE; + } + } + DAT_UnitsState::instance.units[unitID].targetedBuildingTile = 0; + if (!found) { + return FALSE; + } + 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].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; + 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)) { + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + unitID, x, y, 0); + } + return TRUE; + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 1eff998ba8..6c3202d392 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00521280 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521500 | 0.0% | Pending From 33d9309b750cc0d811d3cba1678efa46fb1e98e8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:51 +0100 Subject: [PATCH 08/35] reimplement: SHC_3BB0A8C1_0x0052AD50 100.0% Reimplemented TribesState::processDeerMoving from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/processDeerMoving.cpp | 158 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp new file mode 100644 index 0000000000..f0763bf3bc --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -0,0 +1,158 @@ +#include "../TribesState.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.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 { + + /* + 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) + { + 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; + 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; + } + if (predatorCount != 0 && this->tribes[tribeID].predatorNearby == 0) { + this->tribes[tribeID].predatorNearby = 1; + } + if (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + tribeID, 100, Map::Units::UT_ANTELOPESHDEER)) { + 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; + if (this->tribes[tribeID].currentRallyPointIndex >= this->tribes[tribeID].rallyPointCount) { + this->tribes[tribeID].rallyPointCount = 0; + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + } + return; + } + 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::giveTribeMoveInstruction, this)(tribeID, + 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; + } + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[tribeID].unknownCounter01 = 0; + return; + } + 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 <= 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)( + 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; + 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; + 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; + 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; + 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 6c3202d392..3176bdee18 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 | 0.0% | Pending +SHC_3BB0A8C1_0x0052AD50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B110 | 0.0% | Pending From 32b0b77bc22361e2eb2e394e86251dc79708a8e3 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:51 +0100 Subject: [PATCH 09/35] reimplement: SHC_3BB0A8C1_0x00522630 100.0% Reimplemented TribesState::removeUnitFromThisTribeIfInTribe from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../removeUnitFromThisTribeIfInTribe.cpp | 53 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp new file mode 100644 index 0000000000..d3b09a30fa --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -0,0 +1,53 @@ +#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) + { + /* + 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; + } + short const size = this->tribes[tribeID].size; + if (size > 0) { + this->tribes[tribeID].size = size - 1; + } + if (this->tribes[tribeID].size <= 0) { + this->tribes[tribeID].tribeState = 3; + } + /* + 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; + DAT_UnitsState::instance.units[unitID].idInTribe = 0; + } + if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { + this->tribes[tribeID].selectionTargetUnitID = -1; + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 3176bdee18..89b7aeb8da 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00522630 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522720 | 100.0% | Reimplemented From 4283f7ba7449c03960f7840ce9cff9ca1ed020ab Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:52 +0100 Subject: [PATCH 10/35] reimplement: SHC_3BB0A8C1_0x00525A70 100.0% Reimplemented TribesState::removeUnitFromTribe from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/removeUnitFromTribe.cpp | 53 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp new file mode 100644 index 0000000000..f077c4cb22 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -0,0 +1,53 @@ +#include "../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) + { + /* + 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 (size > 0) { + this->tribes[tribeID].size = size - 1; + } + if (this->tribes[tribeID].size <= 0) { + this->tribes[tribeID].tribeState = 3; + } + /* + 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; + DAT_UnitsState::instance.units[unitID].idInTribe = 0; + } + if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(tribeID); + } + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID); + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 89b7aeb8da..55560d68b3 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00525A70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00525B70 | 100.0% | Reimplemented From 98f4e7ccdeeadf5cda28b71ba461beddecf599e5 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:53 +0100 Subject: [PATCH 11/35] reimplement: SHC_3BB0A8C1_0x00522CA0 100.0% Reimplemented TribesState::snapshotSelectionTribeAndComputeStance from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- ...snapshotSelectionTribeAndComputeStance.cpp | 60 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp new file mode 100644 index 0000000000..004ee7aca7 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -0,0 +1,60 @@ +#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 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) + { + this->tribeCopiedToSlot0 = 0; + // Three stores, not a loop: a countof loop here measured 90.9% normalized. + 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 + && 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; + } + } + } + if (lastTribeID != -1 && !multipleTribes) { + this->tribes[0] = this->tribes[lastTribeID]; + this->tribeCopiedToSlot0 = 1; + return; + } + 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/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 55560d68b3..c823621241 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00522CA0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522DD0 | 100.0% | Reimplemented From e584cc8a1a7d08e236fb359f447765f99c6d8144 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:53 +0100 Subject: [PATCH 12/35] reimplement: SHC_3BB0A8C1_0x00525F20 100.0% Reimplemented TribesState::spawnDeerLionOrRabbit from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/spawnDeerLionOrRabbit.cpp | 92 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp new file mode 100644 index 0000000000..d45924b63a --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -0,0 +1,92 @@ +#include "../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 { + + /* + 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 spawnInterval, UnitType unitType) + { + 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; + } + TribesState* const shifted = (TribesState*)((char*)this + tribeID * sizeof(Tribe)); + int const targetUnitID = shifted->tribes[0].selectionTargetUnitID; + int unitID = targetUnitID; + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getNonDyingUnit, this)(tribeID); + if (unitID == 0) { + unitID = targetUnitID; + } + } + 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 = shifted->tribes[0].herdMinSize; + short const size = shifted->tribes[0].size; + int const roll = (byte)SEC_RNG::instance.currentNumber2 & 0x7f; + if (size <= herdMinSize) { + spawnInterval /= 4; + } + ++shifted->tribes[0].field_0x284; + if ((short)shifted->tribes[0].field_0x284 < spawnInterval) { + return FALSE; + } + shifted->tribes[0].field_0x284 = 0; + if (size >= shifted->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 > roll) { + 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 TRUE; + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index c823621241..2d533a55ee 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 | 0.0% | Pending -SHC_3BB0A8C1_0x00525F20 | 0.0% | Pending +SHC_3BB0A8C1_0x00525F20 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005260B0 | 100.0% | Reimplemented From 090816dad720b05661e9722b8c82939d10c31160 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:54 +0100 Subject: [PATCH 13/35] reimplement: SHC_3BB0A8C1_0x00522EF0 100.0% Reimplemented TribesState::trimTribeToSize from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/trimTribeToSize.cpp | 47 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp new file mode 100644 index 0000000000..30179fb063 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -0,0 +1,47 @@ +#include "../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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x00522EF0 + void TribesState::trimTribeToSize(undefined4 tribeID, int smallerSize) + { + int const currentTribeID = DAT_CurrentTribeID::instance; + int unitSelectionIndex = 0; + /* + 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 shifted = (TribesState*)((char*)this + currentTribeID * sizeof(Tribe)); + if (this->tribes[currentTribeID].tribeState == 0) { + return; + } + int const size = shifted->tribes[0].size; + if (size < smallerSize || size <= 0) { + return; + } + for (; unitSelectionIndex < shifted->tribes[0].size;) { + int const unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( + currentTribeID, unitSelectionIndex); + UnitLogicStateShort const logicalState = DAT_UnitsState::instance.units[unitID].logicalState; + ++unitSelectionIndex; + if ((logicalState == (UnitLogicState)1 || logicalState == Map::Units::ULS_NORMAL + || logicalState == Map::Units::ULS_TRANSITIONING) + && unitSelectionIndex >= smallerSize) { + DAT_UnitsState::instance.units[unitID].logicalState = Map::Units::ULS_REMOVE; + } + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 2d533a55ee..ace34ce889 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00522EF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522F70 | 100.0% | Reimplemented From 49d284f7771090fe1147390cc57890221296fdb0 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:55 +0100 Subject: [PATCH 14/35] reimplement: SHC_3BB0A8C1_0x0052B630 100.0% Reimplemented TribesState::updateCamelTribeBehavior from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/updateCamelTribeBehavior.cpp | 103 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp new file mode 100644 index 0000000000..8af7aee105 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -0,0 +1,103 @@ +#include "../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 { + + /* + 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 tribeID) + { + BOOLEnum const noTunnelers = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)(tribeID, this->tribes[tribeID].uid); + ++this->tribes[tribeID].unknownCounter01; + this->tribes[tribeID].unkIsAnimalTribe = 1; + if (this->tribes[tribeID].unknownCounter01 >= 8000) { + this->tribes[tribeID].unknownCounter01 = 4000; + } + if (noTunnelers + && MACRO_CALL_MEMBER(Map::Units::TribesState_Func::trySpawnAdditionalWildlifeForTribe, this)( + tribeID, 1000, 10, Map::Units::UT_CAMELSHBEAR)) { + 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 (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[tribeID].unknownCounter01 = 0; + 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; + 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)(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); + } + } else if (noTunnelers) { + this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + this->tribes[tribeID].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; + 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 ace34ce889..d07e39e7f2 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49147,7 +49147,7 @@ SHC_3BB0A8C1_0x0052B110 | 0.0% | Pending SHC_3BB0A8C1_0x0052B390 | 0.0% | Pending -SHC_3BB0A8C1_0x0052B630 | 0.0% | Pending +SHC_3BB0A8C1_0x0052B630 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B860 | 0.0% | Pending From 7e829bd930c676f9b6f2618ae1b740c2ad89c0a3 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:55 +0100 Subject: [PATCH 15/35] reimplement: SHC_3BB0A8C1_0x0052B110 100.0% Reimplemented TribesState::updateLionWolfTribeBehavior from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../updateLionWolfTribeBehavior.cpp | 116 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp new file mode 100644 index 0000000000..3841daaa75 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -0,0 +1,116 @@ +#include "../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 { + + /* + 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 tribeID) + { + BOOLEnum const noTunnelers = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)(tribeID, this->tribes[tribeID].uid); + ++this->tribes[tribeID].unknownCounter01; + this->tribes[tribeID].unkIsAnimalTribe = 1; + if (this->tribes[tribeID].unknownCounter01 >= 8000) { + this->tribes[tribeID].unknownCounter01 = 4000; + } + ushort const countdown = this->tribes[tribeID].countdown; + if (countdown != 0) { + if (countdown == 100) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setStateForAllTribeUnits, this)( + tribeID, Map::Units::States::US_UNKNOWN_208); + this->tribes[tribeID].unknownBool01 = 1; + } + return; + } + if (noTunnelers + && MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + tribeID, 500, Map::Units::UT_LIONSHWOLF)) { + 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; + 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 (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)(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); + } + } 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)( + tribeID, this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][0], + this->tribes[tribeID].rallyPointArray[lastRallyPoint - 1][1], 0, 0); + } + } + } else if (noTunnelers) { + 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; + 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 d07e39e7f2..28158bfbe3 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 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0052B110 | 0.0% | Pending +SHC_3BB0A8C1_0x0052B110 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B390 | 0.0% | Pending From edac618704118304cf18a4db4c29f7ece6ee06c6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:56 +0100 Subject: [PATCH 16/35] reimplement: SHC_3BB0A8C1_0x0052B390 100.0% Reimplemented TribesState::updateRabbitTribeBehavior from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/updateRabbitTribeBehavior.cpp | 119 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp new file mode 100644 index 0000000000..d579f3421e --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -0,0 +1,119 @@ +#include "../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 { + + /* + 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) + { + 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) { + spawnInterval = 3; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::spawnDeerLionOrRabbit, this)( + 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, + 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; + } + if (DAT_GameCore::instance.currentMenuViewType == UI::Enums::MVT_MAP_EDITOR_LANDSCAPING) { + this->tribes[tribeID].unknownCounter01 = 0; + return; + } + 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 (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); + } + ++this->tribes[tribeID].predatorNearby; + 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)) { + 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) { + 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; + 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 28158bfbe3..d3fee26ebf 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -49145,7 +49145,7 @@ SHC_3BB0A8C1_0x0052AD50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B110 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0052B390 | 0.0% | Pending +SHC_3BB0A8C1_0x0052B390 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0052B630 | 100.0% | Reimplemented From 815c4c7f820ec889677bf37ca7e8836564f13db7 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:56 +0100 Subject: [PATCH 17/35] reimplement: SHC_3BB0A8C1_0x00523EF0 100.0% Reimplemented TribesState::upgradeTribeArrayLayoutForMapVersion from #279. Measured at 100.0 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../upgradeTribeArrayLayoutForMapVersion.cpp | 35 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp new file mode 100644 index 0000000000..8eca05561d --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -0,0 +1,35 @@ +#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), + // 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 + void TribesState::upgradeTribeArrayLayoutForMapVersion( + PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) + { + if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 0xa9) { + for (int tribeIndex = countof(this->tribes) - 1; tribeIndex >= 0; --tribeIndex) { + char* const oldRecord = (char*)this->tribes + tribeIndex * 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)(&tribe->field_0x1c8, oldRecord + 0x100, 0x16c); + for (int wordIndex = 100; wordIndex < countof(tribe->unitSelectionBitMasked); ++wordIndex) { + tribe->unitSelectionBitMasked[wordIndex] = 0; + } + } + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index d3fee26ebf..98d7e1400a 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00523EF0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00523F70 | 100.0% | Reimplemented From ebbf72e3cf13bc34bc9527d72ce9697119e984ec Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:57 +0100 Subject: [PATCH 18/35] reimplement: SHC_3BB0A8C1_0x00521EF0 100.0% Reimplemented TribesState::playUnitMoveSpeech from #279. Measured at 99.4 / normalized 100.0 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). The only difference is the target of the two tail jumps to SFXState::playSpeechSFX (0x0044A3A0), which in ours go through main's trampoline; counted as 100% by main's convention. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Units/TribesState/playUnitMoveSpeech.cpp | 114 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp new file mode 100644 index 0000000000..78f28aeef2 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp @@ -0,0 +1,114 @@ +#include "../TribesState.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.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 { + + /* + 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 tribeID) + { + int const unitID + = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + UnitType unitType; + /* + 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 `&tribeID` keeps an address-taken variable alive + and blocks the tail call. + */ + { + int selectedCount; + unitType = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( + tribeID, &selectedCount); + } + 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: + /* + Any other ranged unit + */ + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 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) { + 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)(unitType, 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) { + 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)(unitType, 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)(unitType, 0xf); + } + 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)(unitType, 0x12); + } + 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)(unitType, 0x13); + } + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 98d7e1400a..a935815f4b 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 | 82.4% | pop esi scheduling -SHC_3BB0A8C1_0x00521EF0 | 0.0% | Pending +SHC_3BB0A8C1_0x00521EF0 | 100.0% | Reimplemented (counted: only the playSpeechSFX tail-jump target differs; it goes through main's trampoline) SHC_3BB0A8C1_0x00522030 | 0.0% | Pending From 5ed5535d7d062a6b3c86bc68169b36917bcfcae0 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:31:58 +0100 Subject: [PATCH 19/35] reimplement: SHC_3BB0A8C1_0x0052A700 100.0% Reimplemented TribesState::moveTribeToIndexedNearbyTile from #279. Measured at 98.6 / normalized 95.2 on origin/main f28b77bd4 (full build, both resolver sets on, reccmp). The one differing line is the loop-end compare: both binaries compare against 0xb4df44 (&field_0xcc4[40].y, 4 bytes past UnitSelectionDefinedData), which reccmp names after the next global in ours and leaves unnamed in the original. Counted as 100% by main's convention (as 0x004DA200, 0x004DA300, 0x004DAE00). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../moveTribeToIndexedNearbyTile.cpp | 57 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp new file mode 100644 index 0000000000..57a1f6f27e --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -0,0 +1,57 @@ +#include "../TribesState.func.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 { + + /* + 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`. + + reccmp shows 98.6% only because of the loop end: MSVC turns the index into a + pointer and ends the loop at `&field_0xcc4[40].y` (0xb4df44, 4 bytes past the + struct), which reccmp names after the next global in ours and leaves unnamed in + the original. The code is identical. + */ + // FUNCTION: STRONGHOLDCRUSADER 0x0052A700 + void TribesState::moveTribeToIndexedNearbyTile(int tribeID) + { + 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 candidateCount = 40; + int index = someIndex % candidateCount; + if (index <= 0) { + index = 0; + } else if (index >= candidateCount) { + return; + } + for (; index < candidateCount; ++index) { + uint const candidateX = DAT_UnitSelectionDefinedData::instance.field_0xcc4[index].x + x; + uint const candidateY = DAT_UnitSelectionDefinedData::instance.field_0xcc4[index].y + y; + if (candidateX <= 399 && candidateY <= 399 + && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[candidateY * 400 + candidateX] != 0) { + if (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( + tribeID, x, y, candidateX, candidateY)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, candidateX, candidateY, 0, 0, Map::Units::Instructions::UMSE_0); + return; + } + } + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index a935815f4b..1e172d09ca 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 | 0.0% | Pending +SHC_3BB0A8C1_0x0052A700 | 100.0% | Reimplemented (counted: only the loop-end symbol differs) SHC_3BB0A8C1_0x0052A7D0 | 0.0% | Pending From 8aa856e191d00162896e9c90a17813edbdbef0aa Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 15:33:22 +0100 Subject: [PATCH 20/35] style: addUnitToTribe uses the same file as #554 #554 carries TribesState::addUnitToTribe too, written `size++` where this branch had `++size`. Taking its exact text makes the two PRs add an identical file, so whichever merges second has no conflict on it. Still 100.0 / normalized 100.0 (full build and reccmp on f28b77bd4). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp index bcc81557d5..6cff85f557 100644 --- a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -10,7 +10,7 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522590 void TribesState::addUnitToTribe(uint unitID, int tribeID) { - ++this->tribes[tribeID].size; + this->tribes[tribeID].size++; int const bitFlagsIndex = (int)unitID / 16; int const bitMaskIndex = (int)unitID % 16; this->tribes[tribeID].unitSelectionBitMasked[bitFlagsIndex] From 7b4ebc89827d53067822f32f20e0df5c4ebeb001 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 16:06:03 +0100 Subject: [PATCH 21/35] reimplement: SHC_3BB0A8C1_0x00525BA0 96.1% Register allocation differs TribesState::createAnimal from #279, with the style pass. Measured at 96.1 / normalized 77.6 on origin/main 592a17d94 (full build, both resolver sets on, reccmp). Remaining: the reloads of x and y from their parameter slots for the three spawn-point calls, and their pushes, use other registers and are ordered around the store to herdMaxSize; the same values are pushed in the same order. No functional difference. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/createAnimal.cpp | 146 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/createAnimal.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp new file mode 100644 index 0000000000..6da3b80a7d --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -0,0 +1,146 @@ +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/LandscapeState.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; + + /* + 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) + { + UnitType unitType = (UnitType)0; + int animalCount = 0; + dword tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(0, 0); + if ((int)tribeID <= 0) { + return 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 0; + } + if (399 < y) { + return 0; + } + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + return 0; + } + int const logicTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; + if (animalType == Commands::M_MAPPER_DEER) { + if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + return 0; + } + this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN; + unitType = Map::Units::UT_ANTELOPESHDEER; + animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 9; + } else if (animalType == Commands::M_MAPPER_LION) { + if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + return 0; + } + this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN; + unitType = Map::Units::UT_LIONSHWOLF; + animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 1) + 3; + } else if (animalType == Commands::M_MAPPER_RABBIT) { + if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + return 0; + } + this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN; + unitType = Map::Units::UT_RABBIT; + animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 14; + } else if (animalType == Commands::M_MAPPER_CAMEL) { + if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + return 0; + } + this->tribes[tribeID].tribeType = (AITribeType)16; + unitType = Map::Units::UT_CAMELSHBEAR; + animalCount = ((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, y); + return 0; + } + if (animalType == Commands::M_MAPPER_SEAGULL) { + if (!(DAT_TileMapState::instance.LogicLayer[logicTile] & 1)) { + return 0; + } + MACRO_CALL_MEMBER(Map::Entities::EntityState_Func::createSeagull, DAT_EntityState::ptr)( + x * 8, y * 8); + return 0; + } + } + for (int spawnIndex = 0; spawnIndex < animalCount; ++spawnIndex) { + uint const unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::spawnUnit, DAT_UnitsState::ptr)( + 0, 0, x * 8, y * 8, tile, unitType); + if (unitID != 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, tribeID); + DAT_UnitsState::instance.units[unitID].substate + = (short)(DAT_UnitsState::instance.units[unitID].fixedRng & 3); + if (unitType == Map::Units::UT_ANTELOPESHDEER) { + byte const rngValue = (byte)DAT_UnitsState::instance.units[unitID].fixedRng & 7; + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = rngValue; + if (rngValue == 3) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } else if (rngValue == 4) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } else if (rngValue == 5) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } else if (rngValue == 6 || rngValue == 7) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 2; + } + if (spawnIndex == 0) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 0; + } else if (spawnIndex == 1 || spawnIndex == 2) { + DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; + } + } + DAT_UnitsState::instance.units[unitID].disappearFadeAlphaCountdown = 32; + } + } + short const herdSize = this->tribes[tribeID].size; + this->tribes[tribeID].herdMinSize = herdSize / 2; + if (animalType == Commands::M_MAPPER_LION) { + this->tribes[tribeID].herdMaxSize = herdSize; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::storeLionXAndYInMapInfo, this)(x, y); + } else { + this->tribes[tribeID].herdMaxSize = herdSize * 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 tribeID; + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 1e172d09ca..101fadf76b 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00525BA0 | 96.1% | Register allocation differs SHC_3BB0A8C1_0x00525F20 | 100.0% | Reimplemented From ff1cbb496527842e0968d54d8be9c22f3974e164 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 16:06:03 +0100 Subject: [PATCH 22/35] reimplement: SHC_3BB0A8C1_0x00522090 98.0% Register allocation differs TribesState::playAttackCommandFeedback from #279, with the style pass. Measured at 98.0 / normalized 94.4 on origin/main 592a17d94 (full build, both resolver sets on, reccmp). Remaining: the tribe ID is loaded from its parameter slot into ebx before the selectionContainsCombatUnit call instead of after it; nothing writes that slot in between. No functional difference. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/playAttackCommandFeedback.cpp | 42 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp new file mode 100644 index 0000000000..9b24ce0f86 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp @@ -0,0 +1,42 @@ +#include "../TribesState.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.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 { + + /* + The parameter's home is reused for the selected count that + getMajoritySelectedUnitType writes back, as in the original. + */ + // FUNCTION: STRONGHOLDCRUSADER 0x00522090 + void TribesState::playAttackCommandFeedback(int tribeID) + { + int selectionTribeID = tribeID; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + UnitType unitType = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( + selectionTribeID, &tribeID); + 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)(selectionTribeID); + } else { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 21); + if (unitType != Map::Units::UT_E_ENGINEER) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::playUnitSelectionSound, this)(selectionTribeID); + } + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 101fadf76b..8b4852ef09 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00522090 | 98.0% | Register allocation differs SHC_3BB0A8C1_0x00522110 | 100.0% | Reimplemented From ccb6b0d61a6c6c313a0547b2816c5242ea664bad Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 16:06:04 +0100 Subject: [PATCH 23/35] reimplement: SHC_3BB0A8C1_0x00526F00 90.3% Register allocation differs TribesState::commandUnitsToLocation from #279, with the style pass. Measured at 90.3 / normalized 87.2 on origin/main 592a17d94 (full build, both resolver sets on, reccmp). Remaining: this and index swap edi and ebp (a reg_scores tie), and the original stores the 1 flag and spills the tribe offset before the empty-tribe test, ours around it; that exit returns FALSE without reading either. No functional difference. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/commandUnitsToLocation.cpp | 116 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp new file mode 100644 index 0000000000..9fc7b88fa3 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -0,0 +1,116 @@ +#include "../TribesState.func.hpp" + +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.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/WindowsHelper/Enums/BOOLEnum.hpp" + +#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 { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00526F00 + BOOLEnum TribesState::commandUnitsToLocation( + int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) + { + int index = 0; + /* + 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.3% now). Only + register allocation is left: `this` and index swap edi and ebp (reg_scores + gives both -61, so the symbol-id tiebreak decides), and the original stores + the flag and spills the offset before the size test, ours around it. The + empty-tribe exit reads neither. + */ + BOOLEnum noneCommanded; + if (this->tribes[tribeID].size <= 0) { + goto noneCommandedExit; + } + noneCommanded = TRUE; + // A do-while, not a for loop: `for (; index < size;)` reads the size with a separate + // movzx and test where the original compares the field in memory (84.0% normalized). + do { + int const 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) { + 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; + } + } + 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; + } + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 0; + noneCommanded = FALSE; + } while (index < this->tribes[tribeID].size); + if (noneCommanded) { + noneCommandedExit: + return FALSE; + } + if ((char)matchUnitSpeeds != 0) { + DAT_TribesState::instance.tribes[tribeID].freeUnitSpeeds = 0; + } + 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 8b4852ef09..6f903ada25 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00526F00 | 90.3% | Register allocation differs SHC_3BB0A8C1_0x005270F0 | 0.0% | Pending From 3e6c25a48e076761e73309724fdb24b2cb46f84e Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 16:06:05 +0100 Subject: [PATCH 24/35] reimplement: SHC_3BB0A8C1_0x005217E0 92.4% Register allocation differs TribesState::playUnitSelectionSound from #279, with the style pass. Measured at 92.4 / normalized 88.5 on origin/main 592a17d94 (full build, both resolver sets on, reccmp). Remaining: the selected count is loaded into eax and copied to edi (an un-coalesced move) where the original loads it straight into edi; the other lines are jump offsets, the jump table and the alignment filler shifted by those 2 bytes, and tail-jump targets. No functional difference. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/playUnitSelectionSound.cpp | 146 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp new file mode 100644 index 0000000000..25f97c0b6a --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -0,0 +1,146 @@ +#include "../TribesState.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.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 { + + /* + 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 tribeID) + { + int const unitID + = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + { + /* + 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. + */ + int selectedCount; + switch (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( + tribeID, &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, + DE::SHCDE::FX_SWORDWALK_1); + } 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, + DE::SHCDE::FX_SWORDWALK_2); + } 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, + DE::SHCDE::FX_SWORDWALK_3); + } + return; + 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, + DE::SHCDE::FX_HORSES_1); + } 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, DE::SHCDE::FX_HORSES_3); + } + 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, DE::SHCDE::FX_HORSES_4); + } + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DE::SHCDE::FX_HORSE_WHINNY); + return; + } + case Map::Units::UT_A_HARCHER: { + 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, + DE::SHCDE::FX_HORSE_ARCHER1); + } else if (selectedCount <= 4) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DE::SHCDE::FX_HORSE_ARCHER2); + } else { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DE::SHCDE::FX_HORSE_ARCHER3); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DE::SHCDE::FX_HORSE_WHINNY); + return; + } + 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; + } + } + 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 + || requiredEngineers > 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(14); + return; + } + if (requiredEngineers == 1) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(11); + return; + } + if (requiredEngineers == 2) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(12); + return; + } + if (requiredEngineers == 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(13); + } + return; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DE::SHCDE::FX_SIEGE_ROLL); + 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)(36); + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6f903ada25..5f472874db 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 | 0.0% | Pending +SHC_3BB0A8C1_0x005217E0 | 92.4% | Register allocation differs SHC_3BB0A8C1_0x00521A48 | 0.0% | Pending From b904c4a746e9a58cbfa37befefd9fdd088cfabdd Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 16:06:05 +0100 Subject: [PATCH 25/35] reimplement: SHC_3BB0A8C1_0x00521A90 94.4% Register allocation differs TribesState::playUnitCombatSpeechForTarget from #279, with the style pass. Measured at 94.4 / normalized 90.7 on origin/main 592a17d94 (full build, both resolver sets on, reccmp). Remaining: the unitID parameter is loaded once above the range chain and copied, where the original loads it twice at the merge; the two unit-position loads (and the imul that indexes one) swap order; the selected count is copied through eax to edi; the rest are jump offsets and one jump-table entry shifted by those bytes. No functional difference. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../playUnitCombatSpeechForTarget.cpp | 191 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp new file mode 100644 index 0000000000..295918153a --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/playUnitCombatSpeechForTarget.cpp @@ -0,0 +1,191 @@ +#include "../TribesState.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/DE/SHCDE/eSFX.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x00521A90 + void TribesState::playUnitCombatSpeechForTarget(int selectionID, int unitID) + { + int selectedCount; + int const otherUnitID + = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + 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: { + int range; + if (unitType == Map::Units::UT_E_ARCHER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; + } else if (unitType == Map::Units::UT_E_XBOW) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[7] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[7]; + } else if (unitType == Map::Units::UT_A_ARCHER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; + } else if (unitType == Map::Units::UT_A_SLINGER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[33] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[33]; + } else if (unitType == Map::Units::UT_A_HARCHER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[1]; + } else if (unitType == Map::Units::UT_A_FIRETHROWER) { + range = DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[34] + * DAT_EntityDefinedData::instance.EntityTypeArrayForProjectileRange[34]; + } else { + range = unitID; + } + 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, 30); + } else { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 25); + } + if (unitType != Map::Units::UT_A_HARCHER) { + return; + } + 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, + DE::SHCDE::FX_HORSE_ARCHER1); + } else if (selectedCount <= 4) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + DE::SHCDE::FX_HORSE_ARCHER2); + } else { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + DE::SHCDE::FX_HORSE_ARCHER3); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + DE::SHCDE::FX_HORSE_WHINNY); + return; + } + 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[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + DE::SHCDE::FX_HORSES_1); + } else { + if (count == 2 || count == 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, + DAT_UnitsState::instance.units[otherUnitID].y, DE::SHCDE::FX_HORSES_3); + } + if (count >= 4) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, + DAT_UnitsState::instance.units[otherUnitID].y, DE::SHCDE::FX_HORSES_4); + } + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + DE::SHCDE::FX_HORSE_WHINNY); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)((UnitType)unitType, 21); + if (tribeSize >= 15) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + DE::SHCDE::FX_HORSE_ARMY_CHARGE); + } + return; + } + case Map::Units::UT_E_SWORD: + 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, + DE::SHCDE::FX_SWORDWALK_1); + } 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, + DE::SHCDE::FX_SWORDWALK_2); + } 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, + DE::SHCDE::FX_SWORDWALK_3); + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)((UnitType)unitType, 21); + if (tribeSize >= 15) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 20); + } + return; + case Map::Units::UT_E_SPEAR: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)((UnitType)unitType, 21); + if (tribeSize >= 15) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 20); + } + return; + case Map::Units::UT_E_PIKE: + case Map::Units::UT_E_MACE: + 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, 17); + } else { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)( + (UnitType)unitType, 21); + } + if (tribeSize >= 15) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 20); + } + return; + } + case Map::Units::UT_A_SWORDSMAN: + if (tribeSize >= 15) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[otherUnitID].x, DAT_UnitsState::instance.units[otherUnitID].y, + 20); + } + 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, 21); + return; + } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)((UnitType)unitType, 17); + return; + } + case Map::Units::UT_E_ENGINEER: + return; + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 5f472874db..40774c069f 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 | 0.0% | Pending +SHC_3BB0A8C1_0x00521A90 | 94.4% | Register allocation differs SHC_3BB0A8C1_0x00521E50 | 0.0% | Pending From 77df1a557d9a3234aea4d594fb4936e18980509d Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 17:01:20 +0100 Subject: [PATCH 26/35] Add the TribeState enum Tribe::tribeState is a plain short holding 0, 2 or 3: 0 marks a free slot (createTribe, createTribeForPlayer and getAvailableTribeID search for it), 2 an active tribe (set when a tribe is created, required by getUnitIDForIndexInTribe), and 3 a tribe whose last unit was removed (removeUnitFromTribe, removeUnitFromThisTribeIfInTribe). The TribesState functions in this PR compare and assign these values by name. Tribe.hpp keeps its short field; it can become TribeStateShort upstream. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Map/Units/Enums/TribeState.hpp | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/OpenSHC/Map/Units/Enums/TribeState.hpp diff --git a/src/OpenSHC/Map/Units/Enums/TribeState.hpp b/src/OpenSHC/Map/Units/Enums/TribeState.hpp new file mode 100644 index 0000000000..2072b75b33 --- /dev/null +++ b/src/OpenSHC/Map/Units/Enums/TribeState.hpp @@ -0,0 +1,27 @@ +/** + THIS FILE IS AUTO GENERATED + Communicate changes to the dev team (e.g. via a Pull Request). + Changes get lost otherwise. + + path: 'OpenSHC/Map/Units/Enums/TribeState.hpp' +*/ + +#pragma once + +namespace OpenSHC { +namespace Map { + namespace Units { + namespace Enums { + typedef enum TribeState { + + TS_FREE = 0, // 0x00000000 + TS_ACTIVE = 2, // 0x00000002 + TS_EMPTY = 3 // 0x00000003 + + } TribeState; + + static_assert_cpp98_obj(sizeof(TribeState) == 4, TribeState); + } // namespace Enums + } // namespace Units +} // namespace Map +} // namespace OpenSHC From cd7adbdd4d10046d737917f15723de0ab664d8a1 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 17:01:21 +0100 Subject: [PATCH 27/35] Add the TribeStateShort alias The 16-bit alias of TribeState, following the Enums/Short.hpp convention, for Tribe::tribeState (0x18, a short) once the generated header takes it. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/Enums/TribeStateShort.hpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp diff --git a/src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp b/src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp new file mode 100644 index 0000000000..59660069c4 --- /dev/null +++ b/src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp @@ -0,0 +1,23 @@ +/** + THIS FILE IS AUTO GENERATED + Communicate changes to the dev team (e.g. via a Pull Request). + Changes get lost otherwise. + + path: 'OpenSHC/Map/Units/Enums/TribeStateShort.hpp' +*/ + +#pragma once + +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + namespace Enums { + typedef short TribeStateShort; + + static_assert_cpp98_obj(sizeof(TribeStateShort) == 2, TribeStateShort); + } // namespace Enums + } // namespace Units +} // namespace Map +} // namespace OpenSHC From a82ad19c36a739e065e054604ed7c73aea33c0c2 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 17:01:21 +0100 Subject: [PATCH 28/35] review: TheRedDaemon's checklist for the TribesState functions - Tribe states by name (Enums::TS_FREE / TS_ACTIVE / TS_EMPTY) instead of 0/2/3. - Map constants: MC_MAP_XY_LIMIT_INCLUSIVE / MC_MAP_XY_LIMIT for the 399 and 400 tile bounds, MC_MAP_SECTOR_SIZE for the wildlife grid's x / 10, y / 10. - createAnimal: the terrain mask 0x4a5014b1 spelled out as its eleven Logic1 flags, L_SEA for the seagull test, UT_NONE for the initial unit type. - moveUnitToBehaviorTarget: SomeTribeBehaviorType names (STBT_0x3f2 ...) for the behaviour cases, BOOLEnum for the found flag. - Decimal instead of hex for speech action IDs, the fade countdown, the map version and the old record sizes. - `dying` tested for truthiness. - Comments that explained forms kept for the match are removed; their content goes into a PR comment. The old-layout note in upgradeTribeArrayLayoutForMapVersion stays, and trimTribeToSize gets a fixme: for the original ignoring its tribeID parameter. Re-measured with a full build and reccmp, both resolver sets on, on main 592a17d94: every 100% function stays at 100%. createAnimal goes from 96.1 to 96.5 raw (one fewer reload-order hunk; the rest is unchanged), and the other four register-allocation functions are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/commandUnitsToLocation.cpp | 15 +------ .../Map/Units/TribesState/createAnimal.cpp | 37 +++++++--------- .../Map/Units/TribesState/createTribe.cpp | 10 ++--- .../TribesState/createTribeForPlayer.cpp | 11 ++--- .../Units/TribesState/getAvailableTribeID.cpp | 17 ++------ .../TribesState/getUnitIDForIndexInTribe.cpp | 9 ++-- .../moveTribeToIndexedNearbyTile.cpp | 20 +++------ .../TribesState/moveUnitToBehaviorTarget.cpp | 30 +++++-------- .../TribesState/playAttackCommandFeedback.cpp | 4 -- .../Units/TribesState/playUnitMoveSpeech.cpp | 42 +++---------------- .../TribesState/playUnitSelectionSound.cpp | 14 ------- .../Units/TribesState/processDeerMoving.cpp | 29 ++++++------- .../removeUnitFromThisTribeIfInTribe.cpp | 15 ++----- .../Units/TribesState/removeUnitFromTribe.cpp | 15 ++----- ...snapshotSelectionTribeAndComputeStance.cpp | 5 +-- .../TribesState/spawnDeerLionOrRabbit.cpp | 9 +--- .../Map/Units/TribesState/trimTribeToSize.cpp | 11 ++--- .../TribesState/updateCamelTribeBehavior.cpp | 6 --- .../updateLionWolfTribeBehavior.cpp | 5 --- .../TribesState/updateRabbitTribeBehavior.cpp | 14 +++---- .../upgradeTribeArrayLayoutForMapVersion.cpp | 15 +++---- status/addresses-SHC-3BB0A8C1.txt | 2 +- 22 files changed, 86 insertions(+), 249 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index 9fc7b88fa3..6af73124bb 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -21,30 +21,17 @@ namespace Map { int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) { int index = 0; - /* - 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.3% now). Only - register allocation is left: `this` and index swap edi and ebp (reg_scores - gives both -61, so the symbol-id tiebreak decides), and the original stores - the flag and spills the offset before the size test, ours around it. The - empty-tribe exit reads neither. - */ BOOLEnum noneCommanded; if (this->tribes[tribeID].size <= 0) { goto noneCommandedExit; } noneCommanded = TRUE; - // A do-while, not a for loop: `for (; index < size;)` reads the size with a separate - // movzx and test where the original compares the field in memory (84.0% normalized). do { int const 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].dying) { continue; } short const goToRallyPoint = DAT_UnitsState::instance.units[unitID].goToRallyPoint; diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp index 6da3b80a7d..0c879867eb 100644 --- a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -5,6 +5,8 @@ #include "OpenSHC/Map/Units/UnitsState.func.hpp" #include "OpenSHC/AI/Tribes/AITribeType.hpp" #include "OpenSHC/Commands/MappersEnum.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/MapConstants.hpp" #include "OpenSHC/Map/Units/UnitType.hpp" #include "OpenSHC/Globals/DAT_EntityState.hpp" @@ -21,60 +23,53 @@ namespace Map { using AI::Tribes::AITribeType; using Commands::MappersEnum; - /* - 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) { - UnitType unitType = (UnitType)0; + UnitType unitType = Map::Units::UT_NONE; int animalCount = 0; dword tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(0, 0); if ((int)tribeID <= 0) { return 0; } - /* - Three separate guards: the original gives each of them its own forward jump - rather than folding them into one chain. - */ - if (399 < x) { + if (x > Map::MC_MAP_XY_LIMIT_INCLUSIVE) { return 0; } - if (399 < y) { + if (y > Map::MC_MAP_XY_LIMIT_INCLUSIVE) { return 0; } - if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * 400 + x] == 0) { + if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * Map::MC_MAP_XY_LIMIT + x] == 0) { return 0; } + int const blockingTerrain = Map::LogicHelpers::L_SEA | Map::LogicHelpers::L_BORDER + | Map::LogicHelpers::L_BORDER_EDGE | Map::LogicHelpers::L_ROCKY | Map::LogicHelpers::L_BUILDING + | Map::LogicHelpers::L_TREE | Map::LogicHelpers::L_RIVER | Map::LogicHelpers::L_CRENEL_VARIATIONUnk + | Map::LogicHelpers::L_FARM_FIELD_HOP | Map::LogicHelpers::L_FARM_FIELD_DAIRY + | Map::LogicHelpers::L_MOAT; int const logicTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x; if (animalType == Commands::M_MAPPER_DEER) { - if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + if (DAT_TileMapState::instance.LogicLayer[logicTile] & blockingTerrain) { return 0; } this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN; unitType = Map::Units::UT_ANTELOPESHDEER; animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 9; } else if (animalType == Commands::M_MAPPER_LION) { - if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + if (DAT_TileMapState::instance.LogicLayer[logicTile] & blockingTerrain) { return 0; } this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN; unitType = Map::Units::UT_LIONSHWOLF; animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 1) + 3; } else if (animalType == Commands::M_MAPPER_RABBIT) { - if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + if (DAT_TileMapState::instance.LogicLayer[logicTile] & blockingTerrain) { return 0; } this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN; unitType = Map::Units::UT_RABBIT; animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 14; } else if (animalType == Commands::M_MAPPER_CAMEL) { - if ((DAT_TileMapState::instance.LogicLayer[logicTile] & 0x4a5014b1U)) { + if (DAT_TileMapState::instance.LogicLayer[logicTile] & blockingTerrain) { return 0; } this->tribes[tribeID].tribeType = (AITribeType)16; @@ -87,7 +82,7 @@ namespace Map { return 0; } if (animalType == Commands::M_MAPPER_SEAGULL) { - if (!(DAT_TileMapState::instance.LogicLayer[logicTile] & 1)) { + if (!(DAT_TileMapState::instance.LogicLayer[logicTile] & Map::LogicHelpers::L_SEA)) { return 0; } MACRO_CALL_MEMBER(Map::Entities::EntityState_Func::createSeagull, DAT_EntityState::ptr)( diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index be75699ee8..998d476a0b 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -2,6 +2,7 @@ #include "OpenSHC/IO/LowLevelMemory.func.hpp" #include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" #include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" @@ -15,18 +16,13 @@ namespace Map { using AI::Tribes::AITribeType; - /* - 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) { int tribeID = 0; for (DAT_CurrentTribeID::instance = 1; DAT_CurrentTribeID::instance < countof(this->tribes); ++DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE) { tribeID = DAT_CurrentTribeID::instance; MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); @@ -40,7 +36,7 @@ namespace Map { this->DAT_CurrentTribeID = tribeID; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = 2; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index 0a4ba121c6..11fb089756 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -2,6 +2,7 @@ #include "OpenSHC/IO/LowLevelMemory.func.hpp" #include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" #include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" #include "OpenSHC/Globals/DAT_GameCore.hpp" @@ -13,18 +14,12 @@ namespace Map { using AI::Tribes::AITribeType; - /* - 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) { int tribeID; for (tribeID = countof(this->tribes) - playerID; tribeID > 0; tribeID -= 8) { - if (this->tribes[tribeID].tribeState == 0) { + if (this->tribes[tribeID].tribeState == Map::Units::Enums::TS_FREE) { MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); break; @@ -34,7 +29,7 @@ namespace Map { return 0; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = 2; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index caf8a890d7..48634e32c8 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -1,6 +1,7 @@ #include "../TribesState.func.hpp" #include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" #include "OpenSHC/Globals/DAT_GameCore.hpp" @@ -10,13 +11,6 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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 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) { @@ -25,7 +19,7 @@ namespace Map { tribeID = 0; for (DAT_CurrentTribeID::instance = countof(this->tribes) - 1; DAT_CurrentTribeID::instance > 0; --DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0 + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE && this->tribes[DAT_CurrentTribeID::instance].time == 0) { tribeID = DAT_CurrentTribeID::instance; break; @@ -39,7 +33,7 @@ namespace Map { } for (tribeID = countof(this->tribes) - playerID, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; tribeID -= 8, DAT_CurrentTribeID::instance = tribeID) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == 0 + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE && this->tribes[DAT_CurrentTribeID::instance].time == 0) { break; } @@ -47,11 +41,6 @@ namespace Map { if (tribeID <= 0) { return 0; } - /* - 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; } diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp index 374a2cdbf6..d33d4d6384 100644 --- a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -1,19 +1,16 @@ #include "../TribesState.func.hpp" +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" + 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 count = 0; - if (this->tribes[tribeID].tribeState != 2) { + if (this->tribes[tribeID].tribeState != Map::Units::Enums::TS_ACTIVE) { return 0; } if (unitSelectionIndex >= this->tribes[tribeID].size) { diff --git a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp index 57a1f6f27e..73a997d438 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveTribeToIndexedNearbyTile.cpp @@ -1,5 +1,6 @@ #include "../TribesState.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" #include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" #include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" @@ -10,19 +11,6 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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`. - - reccmp shows 98.6% only because of the loop end: MSVC turns the index into a - pointer and ends the loop at `&field_0xcc4[40].y` (0xb4df44, 4 bytes past the - struct), which reccmp names after the next global in ours and leaves unnamed in - the original. The code is identical. - */ // FUNCTION: STRONGHOLDCRUSADER 0x0052A700 void TribesState::moveTribeToIndexedNearbyTile(int tribeID) { @@ -40,8 +28,10 @@ namespace Map { for (; index < candidateCount; ++index) { uint const candidateX = DAT_UnitSelectionDefinedData::instance.field_0xcc4[index].x + x; uint const candidateY = DAT_UnitSelectionDefinedData::instance.field_0xcc4[index].y + y; - if (candidateX <= 399 && candidateY <= 399 - && DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[candidateY * 400 + candidateX] != 0) { + if (candidateX <= Map::MC_MAP_XY_LIMIT_INCLUSIVE && candidateY <= Map::MC_MAP_XY_LIMIT_INCLUSIVE + && DAT_ViewportRenderState::instance + .DAT_BinaryTileMap400x400[candidateY * Map::MC_MAP_XY_LIMIT + candidateX] + != 0) { if (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribePathToDestinationClear, this)( tribeID, x, y, candidateX, candidateY)) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index 56f11b8ee6..ad868ee3a5 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -16,24 +16,16 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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 behaviorType) { uint y = 0; uint x = 0; uint tile = 0; - BOOL found = FALSE; + BOOLEnum 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].dying || DAT_UnitsState::instance.units[unitID].usingTeleport != 0 || DAT_UnitsState::instance.units[unitID].field_0x413 != 0 || DAT_GameSynchronyState::instance @@ -41,7 +33,7 @@ namespace Map { != -1) { return FALSE; } - if (behaviorType == 0x3f2) { + if (behaviorType == Map::Units::STBT_0x3f2) { if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)( unitID)) { x = DAT_TroopValueState::instance.x; @@ -49,7 +41,7 @@ namespace Map { tile = DAT_TroopValueState::instance.tile; found = TRUE; } - } else if (behaviorType == 0x3f5) { + } else if (behaviorType == Map::Units::STBT_0x3f5) { if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit, DAT_TroopValueState::ptr)(unitID)) { x = DAT_TroopValueState::instance.x; @@ -57,7 +49,7 @@ namespace Map { tile = DAT_TroopValueState::instance.tile; found = TRUE; } - } else if (behaviorType == 0x3fb) { + } else if (behaviorType == Map::Units::STBT_0x3fb) { if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit, DAT_TroopValueState::ptr)(unitID)) { x = DAT_TroopValueState::instance.x; @@ -69,13 +61,13 @@ namespace Map { if (this->tribes[tribeID].selectionTargetUnitID == unitID) { DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_0x3f2; } - DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = 0x3f2; + DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = Map::Units::STBT_0x3f2; x = DAT_TroopValueState::instance.x; y = DAT_TroopValueState::instance.y; tile = DAT_TroopValueState::instance.tile; found = TRUE; } - } else if (behaviorType == 0x3fd) { + } else if (behaviorType == Map::Units::STBT_0x3fd) { if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)( unitID)) { x = DAT_TroopValueState::instance.x; @@ -83,7 +75,7 @@ namespace Map { tile = DAT_TroopValueState::instance.tile; found = TRUE; } - } else if (behaviorType == 0x3f6) { + } else if (behaviorType == Map::Units::STBT_0x3f6) { if (MACRO_CALL_MEMBER( Map::Units::TroopValueState_Func::findEnemyTowersOrGates, DAT_TroopValueState::ptr)(unitID)) { x = DAT_TroopValueState::instance.x; @@ -91,7 +83,7 @@ namespace Map { tile = DAT_TroopValueState::instance.tile; found = TRUE; } - } else if (behaviorType == 0x413) { + } else if (behaviorType == Map::Units::STBT_0x413) { if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, DAT_TroopValueState::ptr)(unitID)) { x = DAT_TroopValueState::instance.x; @@ -99,7 +91,7 @@ namespace Map { tile = DAT_TroopValueState::instance.tile; found = TRUE; } - } else if (behaviorType == 0x414) { + } else if (behaviorType == Map::Units::STBT_0x414) { if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer, DAT_TroopValueState::ptr)(unitID)) { x = DAT_TroopValueState::instance.x; @@ -107,7 +99,7 @@ namespace Map { tile = DAT_TroopValueState::instance.tile; found = TRUE; } - } else if (behaviorType == 0x3f7) { + } else if (behaviorType == Map::Units::STBT_0x3f7) { if (MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestDiggableMoatPoint, DAT_TroopValueState::ptr)(unitID)) { x = DAT_TroopValueState::instance.x; diff --git a/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp index 9b24ce0f86..a933adb5c9 100644 --- a/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playAttackCommandFeedback.cpp @@ -12,10 +12,6 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - The parameter's home is reused for the selected count that - getMajoritySelectedUnitType writes back, as in the original. - */ // FUNCTION: STRONGHOLDCRUSADER 0x00522090 void TribesState::playAttackCommandFeedback(int tribeID) { diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp index 78f28aeef2..95c0f95d57 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitMoveSpeech.cpp @@ -11,24 +11,12 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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 tribeID) { int const unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); UnitType unitType; - /* - 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 `&tribeID` keeps an address-taken variable alive - and blocks the tail call. - */ { int selectedCount; unitType = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( @@ -41,15 +29,9 @@ namespace Map { 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)(unitType, 0x1d); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 29); return; case Map::Units::UT_S_CATAPULT: - /* - Catapult - */ if (DAT_UnitsState::instance.units[unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { @@ -58,13 +40,10 @@ namespace Map { Audio::SFX::SEID_RESOURCE_NEED25); return; } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 0x10); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 16); } break; case Map::Units::UT_S_TREBUCHET: - /* - Trebuchet - */ if (DAT_UnitsState::instance.units[unitID] .digTileX__OR__countCurrentlyManningEnginers__OR__forCowsRandomBelow300 != 0) { @@ -73,38 +52,29 @@ namespace Map { Audio::SFX::SEID_RESOURCE_NEED25); return; } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 0x11); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 17); } 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)(unitType, 0xf); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 15); } 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)(unitType, 0x12); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 18); } 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)(unitType, 0x13); + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeech, DAT_SFXState::ptr)(unitType, 19); } } } diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 25f97c0b6a..8fec0215a1 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -12,26 +12,12 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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 tribeID) { int const unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); { - /* - 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. - */ int selectedCount; switch (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( tribeID, &selectedCount)) { diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index f0763bf3bc..799b3fed08 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -3,6 +3,7 @@ #include "OpenSHC/Audio/SFX/SFXState.func.hpp" #include "OpenSHC/Map/WildlifeState.func.hpp" #include "OpenSHC/DE/SHCDE/eSFX.hpp" +#include "OpenSHC/Map/MapConstants.hpp" #include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" #include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" #include "OpenSHC/Map/Units/UnitType.hpp" @@ -19,32 +20,26 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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) { 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] + .grid[DAT_UnitsState::instance.units[targetUnitID].x / Map::MC_MAP_SECTOR_SIZE] + [DAT_UnitsState::instance.units[targetUnitID].y / Map::MC_MAP_SECTOR_SIZE] .marshTiles; int const rockyTiles = DAT_WildlifeState::instance - .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] - [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .grid[DAT_UnitsState::instance.units[targetUnitID].x / Map::MC_MAP_SECTOR_SIZE] + [DAT_UnitsState::instance.units[targetUnitID].y / Map::MC_MAP_SECTOR_SIZE] .rockyTiles; - int const predatorCount = DAT_WildlifeState::instance - .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] - [DAT_UnitsState::instance.units[targetUnitID].y / 10] - .field_0x48 + int const predatorCount + = DAT_WildlifeState::instance + .grid[DAT_UnitsState::instance.units[targetUnitID].x / Map::MC_MAP_SECTOR_SIZE] + [DAT_UnitsState::instance.units[targetUnitID].y / Map::MC_MAP_SECTOR_SIZE] + .field_0x48 + DAT_WildlifeState::instance - .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] - [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .grid[DAT_UnitsState::instance.units[targetUnitID].x / Map::MC_MAP_SECTOR_SIZE] + [DAT_UnitsState::instance.units[targetUnitID].y / Map::MC_MAP_SECTOR_SIZE] .lionCount; short const size = this->tribes[tribeID].size; this->tribes[tribeID].unkIsAnimalTribe = 1; diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index d3b09a30fa..bdd4bbc252 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -1,5 +1,7 @@ #include "../TribesState.func.hpp" +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" + #include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -10,12 +12,6 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522630 void TribesState::removeUnitFromThisTribeIfInTribe(uint unitID, int tribeID) { - /* - 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]) @@ -27,13 +23,8 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = 3; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; } - /* - 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] diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index f077c4cb22..a3b1c027c7 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -1,5 +1,7 @@ #include "../TribesState.func.hpp" +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" + #include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -10,12 +12,6 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00525A70 void TribesState::removeUnitFromTribe(uint unitID, int tribeID) { - /* - 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]) @@ -25,13 +21,8 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = 3; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; } - /* - 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] diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index 004ee7aca7..167983415c 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -12,13 +12,10 @@ namespace Map { 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) { this->tribeCopiedToSlot0 = 0; - // Three stores, not a loop: a countof loop here measured 90.9% normalized. int stanceCount[3]; stanceCount[0] = 0; stanceCount[1] = 0; @@ -27,7 +24,7 @@ namespace Map { 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].dying && DAT_UnitsState::instance.units[unitID].ifSelectedThenPlayerID == playerID) { int const tribeID = DAT_UnitsState::instance.units[unitID].tribeID; if (tribeID > 0) { diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index d45924b63a..b174a674c9 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -14,13 +14,6 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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 spawnInterval, UnitType unitType) { @@ -80,7 +73,7 @@ namespace Map { if (unitType == Map::Units::UT_ANTELOPESHDEER) { DAT_UnitsState::instance.units[spawnedUnitID].antelopeBasedRngValue = 2; } - DAT_UnitsState::instance.units[spawnedUnitID].disappearFadeAlphaCountdown = 0x20; + DAT_UnitsState::instance.units[spawnedUnitID].disappearFadeAlphaCountdown = 32; if (unitType == Map::Units::UT_ANTELOPESHDEER) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateAnimalHerdBehaviorState, this)(tribeID); } diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 30179fb063..02da89e370 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -1,5 +1,6 @@ #include "../TribesState.func.hpp" +#include "OpenSHC/Map/Units/Enums/TribeState.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" @@ -12,17 +13,11 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00522EF0 void TribesState::trimTribeToSize(undefined4 tribeID, int smallerSize) { + // fixme: the original ignores tribeID and trims the tribe in DAT_CurrentTribeID int const currentTribeID = DAT_CurrentTribeID::instance; int unitSelectionIndex = 0; - /* - 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 shifted = (TribesState*)((char*)this + currentTribeID * sizeof(Tribe)); - if (this->tribes[currentTribeID].tribeState == 0) { + if (this->tribes[currentTribeID].tribeState == Map::Units::Enums::TS_FREE) { return; } int const size = shifted->tribes[0].size; diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index 8af7aee105..09419b12ce 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -14,12 +14,6 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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 tribeID) { diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index 3841daaa75..de7579dfe6 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -15,11 +15,6 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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 tribeID) { diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index d579f3421e..cb2efe49fb 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -1,6 +1,7 @@ #include "../TribesState.func.hpp" #include "OpenSHC/Map/WildlifeState.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" #include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" #include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" #include "OpenSHC/Map/Units/UnitType.hpp" @@ -16,22 +17,17 @@ namespace OpenSHC { namespace Map { namespace Units { - /* - 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) { 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] + .grid[DAT_UnitsState::instance.units[targetUnitID].x / Map::MC_MAP_SECTOR_SIZE] + [DAT_UnitsState::instance.units[targetUnitID].y / Map::MC_MAP_SECTOR_SIZE] .rockyTiles; int const marshTiles = DAT_WildlifeState::instance - .grid[DAT_UnitsState::instance.units[targetUnitID].x / 10] - [DAT_UnitsState::instance.units[targetUnitID].y / 10] + .grid[DAT_UnitsState::instance.units[targetUnitID].x / Map::MC_MAP_SECTOR_SIZE] + [DAT_UnitsState::instance.units[targetUnitID].y / Map::MC_MAP_SECTOR_SIZE] .marshTiles; BOOLEnum const noTunnelers = MACRO_CALL_MEMBER( Map::Units::TribesState_Func::isTribeFreeOfTunnelingUnits, this)(tribeID, this->tribes[tribeID].uid); diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 8eca05561d..3d6c393b53 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -6,23 +6,20 @@ 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), - // 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. + // Map versions below 169 store each tribe in 620 bytes: a 100-word unit bitmask, and the rest of the + // record (364 bytes) at offset 256. // FUNCTION: STRONGHOLDCRUSADER 0x00523EF0 void TribesState::upgradeTribeArrayLayoutForMapVersion( PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) { - if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 0xa9) { + if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 169) { for (int tribeIndex = countof(this->tribes) - 1; tribeIndex >= 0; --tribeIndex) { - char* const oldRecord = (char*)this->tribes + tribeIndex * 0x26c; + char* const oldRecord = (char*)this->tribes + tribeIndex * 620; 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)(tribe, oldRecord, 620); } - MACRO_CALL(OpenSHC::OS_Func::_memmove)(&tribe->field_0x1c8, oldRecord + 0x100, 0x16c); + MACRO_CALL(OpenSHC::OS_Func::_memmove)(&tribe->field_0x1c8, oldRecord + 256, 364); for (int wordIndex = 100; wordIndex < countof(tribe->unitSelectionBitMasked); ++wordIndex) { tribe->unitSelectionBitMasked[wordIndex] = 0; } diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 40774c069f..d73ce50d0d 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 | 96.1% | Register allocation differs +SHC_3BB0A8C1_0x00525BA0 | 96.5% | Register allocation differs SHC_3BB0A8C1_0x00525F20 | 100.0% | Reimplemented From 83a493078bdbbcc35bd79ba0787245273bbd708c Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 17:03:30 +0100 Subject: [PATCH 29/35] TribeState: use the TRS_ prefix Map/Trees/TreeState.hpp already uses TS_ (TS_UNUSED, TS_SPAWNED, TS_ALIVE, TS_TO_BE_REMOVED, TS_STATIC). The tribe states become TRS_FREE, TRS_ACTIVE and TRS_EMPTY, in the enum and in every use. A rename only; quick_diff output for createTribe and removeUnitFromTribe is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Map/Units/Enums/TribeState.hpp | 6 +++--- src/OpenSHC/Map/Units/TribesState/createTribe.cpp | 4 ++-- src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp | 4 ++-- src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp | 4 ++-- .../Map/Units/TribesState/getUnitIDForIndexInTribe.cpp | 2 +- .../Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp | 2 +- src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp | 2 +- src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/OpenSHC/Map/Units/Enums/TribeState.hpp b/src/OpenSHC/Map/Units/Enums/TribeState.hpp index 2072b75b33..9c1f2559b4 100644 --- a/src/OpenSHC/Map/Units/Enums/TribeState.hpp +++ b/src/OpenSHC/Map/Units/Enums/TribeState.hpp @@ -14,9 +14,9 @@ namespace Map { namespace Enums { typedef enum TribeState { - TS_FREE = 0, // 0x00000000 - TS_ACTIVE = 2, // 0x00000002 - TS_EMPTY = 3 // 0x00000003 + TRS_FREE = 0, // 0x00000000 + TRS_ACTIVE = 2, // 0x00000002 + TRS_EMPTY = 3 // 0x00000003 } TribeState; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index 998d476a0b..a0480560e1 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -22,7 +22,7 @@ namespace Map { int tribeID = 0; for (DAT_CurrentTribeID::instance = 1; DAT_CurrentTribeID::instance < countof(this->tribes); ++DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TRS_FREE) { tribeID = DAT_CurrentTribeID::instance; MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); @@ -36,7 +36,7 @@ namespace Map { this->DAT_CurrentTribeID = tribeID; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; + this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index 11fb089756..7ce8caea14 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -19,7 +19,7 @@ namespace Map { { int tribeID; for (tribeID = countof(this->tribes) - playerID; tribeID > 0; tribeID -= 8) { - if (this->tribes[tribeID].tribeState == Map::Units::Enums::TS_FREE) { + if (this->tribes[tribeID].tribeState == Map::Units::Enums::TRS_FREE) { MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); break; @@ -29,7 +29,7 @@ namespace Map { return 0; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; + this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index 48634e32c8..9363a6c906 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -19,7 +19,7 @@ namespace Map { tribeID = 0; for (DAT_CurrentTribeID::instance = countof(this->tribes) - 1; DAT_CurrentTribeID::instance > 0; --DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TRS_FREE && this->tribes[DAT_CurrentTribeID::instance].time == 0) { tribeID = DAT_CurrentTribeID::instance; break; @@ -33,7 +33,7 @@ namespace Map { } for (tribeID = countof(this->tribes) - playerID, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; tribeID -= 8, DAT_CurrentTribeID::instance = tribeID) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TRS_FREE && this->tribes[DAT_CurrentTribeID::instance].time == 0) { break; } diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp index d33d4d6384..d6d8837d22 100644 --- a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -10,7 +10,7 @@ namespace Map { int TribesState::getUnitIDForIndexInTribe(int tribeID, int unitSelectionIndex) { int count = 0; - if (this->tribes[tribeID].tribeState != Map::Units::Enums::TS_ACTIVE) { + if (this->tribes[tribeID].tribeState != Map::Units::Enums::TRS_ACTIVE) { return 0; } if (unitSelectionIndex >= this->tribes[tribeID].size) { diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index bdd4bbc252..6a2332e17f 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -23,7 +23,7 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; + this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_EMPTY; } int const wordIndex = (int)unitID / 16; int const bitIndex = (int)unitID % 16; diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index a3b1c027c7..3447b92c1f 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -21,7 +21,7 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; + this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_EMPTY; } int const wordIndex = (int)unitID / 16; int const bitIndex = (int)unitID % 16; diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 02da89e370..c18a0c5cee 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -17,7 +17,7 @@ namespace Map { int const currentTribeID = DAT_CurrentTribeID::instance; int unitSelectionIndex = 0; TribesState* const shifted = (TribesState*)((char*)this + currentTribeID * sizeof(Tribe)); - if (this->tribes[currentTribeID].tribeState == Map::Units::Enums::TS_FREE) { + if (this->tribes[currentTribeID].tribeState == Map::Units::Enums::TRS_FREE) { return; } int const size = shifted->tribes[0].size; From 333dc3be107e016d5fc7c3a5837255f108606885 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 17:07:18 +0100 Subject: [PATCH 30/35] TribeState: back to the TS_ prefix Main's TreeState enum is renamed to TreeStateEnum with TSE_ values in #549, which frees TS_ again, so the tribe states return to TS_FREE, TS_ACTIVE and TS_EMPTY. A rename only; quick_diff output for createTribe and removeUnitFromTribe is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Map/Units/Enums/TribeState.hpp | 6 +++--- src/OpenSHC/Map/Units/TribesState/createTribe.cpp | 4 ++-- src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp | 4 ++-- src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp | 4 ++-- .../Map/Units/TribesState/getUnitIDForIndexInTribe.cpp | 2 +- .../Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp | 2 +- src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp | 2 +- src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/OpenSHC/Map/Units/Enums/TribeState.hpp b/src/OpenSHC/Map/Units/Enums/TribeState.hpp index 9c1f2559b4..2072b75b33 100644 --- a/src/OpenSHC/Map/Units/Enums/TribeState.hpp +++ b/src/OpenSHC/Map/Units/Enums/TribeState.hpp @@ -14,9 +14,9 @@ namespace Map { namespace Enums { typedef enum TribeState { - TRS_FREE = 0, // 0x00000000 - TRS_ACTIVE = 2, // 0x00000002 - TRS_EMPTY = 3 // 0x00000003 + TS_FREE = 0, // 0x00000000 + TS_ACTIVE = 2, // 0x00000002 + TS_EMPTY = 3 // 0x00000003 } TribeState; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index a0480560e1..998d476a0b 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -22,7 +22,7 @@ namespace Map { int tribeID = 0; for (DAT_CurrentTribeID::instance = 1; DAT_CurrentTribeID::instance < countof(this->tribes); ++DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TRS_FREE) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE) { tribeID = DAT_CurrentTribeID::instance; MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); @@ -36,7 +36,7 @@ namespace Map { this->DAT_CurrentTribeID = tribeID; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_ACTIVE; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index 7ce8caea14..11fb089756 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -19,7 +19,7 @@ namespace Map { { int tribeID; for (tribeID = countof(this->tribes) - playerID; tribeID > 0; tribeID -= 8) { - if (this->tribes[tribeID].tribeState == Map::Units::Enums::TRS_FREE) { + if (this->tribes[tribeID].tribeState == Map::Units::Enums::TS_FREE) { MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); break; @@ -29,7 +29,7 @@ namespace Map { return 0; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_ACTIVE; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index 9363a6c906..48634e32c8 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -19,7 +19,7 @@ namespace Map { tribeID = 0; for (DAT_CurrentTribeID::instance = countof(this->tribes) - 1; DAT_CurrentTribeID::instance > 0; --DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TRS_FREE + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE && this->tribes[DAT_CurrentTribeID::instance].time == 0) { tribeID = DAT_CurrentTribeID::instance; break; @@ -33,7 +33,7 @@ namespace Map { } for (tribeID = countof(this->tribes) - playerID, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; tribeID -= 8, DAT_CurrentTribeID::instance = tribeID) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TRS_FREE + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE && this->tribes[DAT_CurrentTribeID::instance].time == 0) { break; } diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp index d6d8837d22..d33d4d6384 100644 --- a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -10,7 +10,7 @@ namespace Map { int TribesState::getUnitIDForIndexInTribe(int tribeID, int unitSelectionIndex) { int count = 0; - if (this->tribes[tribeID].tribeState != Map::Units::Enums::TRS_ACTIVE) { + if (this->tribes[tribeID].tribeState != Map::Units::Enums::TS_ACTIVE) { return 0; } if (unitSelectionIndex >= this->tribes[tribeID].size) { diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index 6a2332e17f..bdd4bbc252 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -23,7 +23,7 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_EMPTY; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; } int const wordIndex = (int)unitID / 16; int const bitIndex = (int)unitID % 16; diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index 3447b92c1f..a3b1c027c7 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -21,7 +21,7 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = Map::Units::Enums::TRS_EMPTY; + this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; } int const wordIndex = (int)unitID / 16; int const bitIndex = (int)unitID % 16; diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index c18a0c5cee..02da89e370 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -17,7 +17,7 @@ namespace Map { int const currentTribeID = DAT_CurrentTribeID::instance; int unitSelectionIndex = 0; TribesState* const shifted = (TribesState*)((char*)this + currentTribeID * sizeof(Tribe)); - if (this->tribes[currentTribeID].tribeState == Map::Units::Enums::TRS_FREE) { + if (this->tribes[currentTribeID].tribeState == Map::Units::Enums::TS_FREE) { return; } int const size = shifted->tribes[0].size; From 0756508ebfda5825ce241e8901041507d1935552 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 17:35:19 +0100 Subject: [PATCH 31/35] TribeState: move to Map/Enums with TS_UNUSED, TS_ACTIVE, TS_DISBANDED This aligns the enum with #554, which carries the same two files byte for byte, so the two PRs do not conflict: - Map/Enums is main's folder for these headers; Map/Units/Enums does not exist on main. - TS_FREE becomes TS_UNUSED and TS_EMPTY becomes TS_DISBANDED. - TS_ is free once #549 renames TreeState to TreeStateEnum (TSE_). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- src/OpenSHC/Map/Enums/TribeState.hpp | 25 +++++++++++++++++ src/OpenSHC/Map/Enums/TribeStateShort.hpp | 21 +++++++++++++++ src/OpenSHC/Map/Units/Enums/TribeState.hpp | 27 ------------------- .../Map/Units/Enums/TribeStateShort.hpp | 23 ---------------- .../Map/Units/TribesState/createTribe.cpp | 6 ++--- .../TribesState/createTribeForPlayer.cpp | 6 ++--- .../Units/TribesState/getAvailableTribeID.cpp | 6 ++--- .../TribesState/getUnitIDForIndexInTribe.cpp | 4 +-- .../removeUnitFromThisTribeIfInTribe.cpp | 4 +-- .../Units/TribesState/removeUnitFromTribe.cpp | 4 +-- .../Map/Units/TribesState/trimTribeToSize.cpp | 4 +-- 11 files changed, 63 insertions(+), 67 deletions(-) create mode 100644 src/OpenSHC/Map/Enums/TribeState.hpp create mode 100644 src/OpenSHC/Map/Enums/TribeStateShort.hpp delete mode 100644 src/OpenSHC/Map/Units/Enums/TribeState.hpp delete mode 100644 src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp diff --git a/src/OpenSHC/Map/Enums/TribeState.hpp b/src/OpenSHC/Map/Enums/TribeState.hpp new file mode 100644 index 0000000000..a0233c3a14 --- /dev/null +++ b/src/OpenSHC/Map/Enums/TribeState.hpp @@ -0,0 +1,25 @@ +/** + THIS FILE IS AUTO GENERATED + Communicate changes to the dev team (e.g. via a Pull Request). + Changes get lost otherwise. + + path: 'OpenSHC/Map/Enums/TribeState.hpp' +*/ + +#pragma once + +namespace OpenSHC { +namespace Map { + namespace Enums { + typedef enum TribeState { + + TS_UNUSED = 0, // 0x00000000 + TS_ACTIVE = 2, // 0x00000002 + TS_DISBANDED = 3 // 0x00000003 + + } TribeState; + + static_assert_cpp98_obj(sizeof(TribeState) == 4, TribeState); + } // namespace Enums +} // namespace Map +} // namespace OpenSHC diff --git a/src/OpenSHC/Map/Enums/TribeStateShort.hpp b/src/OpenSHC/Map/Enums/TribeStateShort.hpp new file mode 100644 index 0000000000..3ec4c7e2fb --- /dev/null +++ b/src/OpenSHC/Map/Enums/TribeStateShort.hpp @@ -0,0 +1,21 @@ +/** + THIS FILE IS AUTO GENERATED + Communicate changes to the dev team (e.g. via a Pull Request). + Changes get lost otherwise. + + path: 'OpenSHC/Map/Enums/TribeStateShort.hpp' +*/ + +#pragma once + +#include "OpenSHC/Map/Enums/TribeState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Enums { + typedef short TribeStateShort; + + static_assert_cpp98_obj(sizeof(TribeStateShort) == 2, TribeStateShort); + } // namespace Enums +} // namespace Map +} // namespace OpenSHC diff --git a/src/OpenSHC/Map/Units/Enums/TribeState.hpp b/src/OpenSHC/Map/Units/Enums/TribeState.hpp deleted file mode 100644 index 2072b75b33..0000000000 --- a/src/OpenSHC/Map/Units/Enums/TribeState.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/** - THIS FILE IS AUTO GENERATED - Communicate changes to the dev team (e.g. via a Pull Request). - Changes get lost otherwise. - - path: 'OpenSHC/Map/Units/Enums/TribeState.hpp' -*/ - -#pragma once - -namespace OpenSHC { -namespace Map { - namespace Units { - namespace Enums { - typedef enum TribeState { - - TS_FREE = 0, // 0x00000000 - TS_ACTIVE = 2, // 0x00000002 - TS_EMPTY = 3 // 0x00000003 - - } TribeState; - - static_assert_cpp98_obj(sizeof(TribeState) == 4, TribeState); - } // namespace Enums - } // namespace Units -} // namespace Map -} // namespace OpenSHC diff --git a/src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp b/src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp deleted file mode 100644 index 59660069c4..0000000000 --- a/src/OpenSHC/Map/Units/Enums/TribeStateShort.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/** - THIS FILE IS AUTO GENERATED - Communicate changes to the dev team (e.g. via a Pull Request). - Changes get lost otherwise. - - path: 'OpenSHC/Map/Units/Enums/TribeStateShort.hpp' -*/ - -#pragma once - -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" - -namespace OpenSHC { -namespace Map { - namespace Units { - namespace Enums { - typedef short TribeStateShort; - - static_assert_cpp98_obj(sizeof(TribeStateShort) == 2, TribeStateShort); - } // namespace Enums - } // namespace Units -} // namespace Map -} // namespace OpenSHC diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index 998d476a0b..d10f88a447 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -2,7 +2,7 @@ #include "OpenSHC/IO/LowLevelMemory.func.hpp" #include "OpenSHC/AI/Tribes/AITribeType.hpp" -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" #include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" @@ -22,7 +22,7 @@ namespace Map { int tribeID = 0; for (DAT_CurrentTribeID::instance = 1; DAT_CurrentTribeID::instance < countof(this->tribes); ++DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE) { + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Enums::TS_UNUSED) { tribeID = DAT_CurrentTribeID::instance; MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); @@ -36,7 +36,7 @@ namespace Map { this->DAT_CurrentTribeID = tribeID; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; + this->tribes[tribeID].tribeState = Map::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index 11fb089756..b33c3dac60 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -2,7 +2,7 @@ #include "OpenSHC/IO/LowLevelMemory.func.hpp" #include "OpenSHC/AI/Tribes/AITribeType.hpp" -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" #include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" #include "OpenSHC/Globals/DAT_GameCore.hpp" @@ -19,7 +19,7 @@ namespace Map { { int tribeID; for (tribeID = countof(this->tribes) - playerID; tribeID > 0; tribeID -= 8) { - if (this->tribes[tribeID].tribeState == Map::Units::Enums::TS_FREE) { + if (this->tribes[tribeID].tribeState == Map::Enums::TS_UNUSED) { MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( sizeof(Tribe), 0, &this->tribes[tribeID]); break; @@ -29,7 +29,7 @@ namespace Map { return 0; } this->tribes[tribeID].owner = playerID; - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_ACTIVE; + this->tribes[tribeID].tribeState = Map::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; this->tribes[tribeID].tribeType = (AITribeType)0; diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index 48634e32c8..b5581d328a 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -1,7 +1,7 @@ #include "../TribesState.func.hpp" #include "OpenSHC/Game/GameMode.hpp" -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" #include "OpenSHC/Globals/DAT_GameCore.hpp" @@ -19,7 +19,7 @@ namespace Map { tribeID = 0; for (DAT_CurrentTribeID::instance = countof(this->tribes) - 1; DAT_CurrentTribeID::instance > 0; --DAT_CurrentTribeID::instance) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Enums::TS_UNUSED && this->tribes[DAT_CurrentTribeID::instance].time == 0) { tribeID = DAT_CurrentTribeID::instance; break; @@ -33,7 +33,7 @@ namespace Map { } for (tribeID = countof(this->tribes) - playerID, DAT_CurrentTribeID::instance = tribeID; tribeID > 0; tribeID -= 8, DAT_CurrentTribeID::instance = tribeID) { - if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Units::Enums::TS_FREE + if (this->tribes[DAT_CurrentTribeID::instance].tribeState == Map::Enums::TS_UNUSED && this->tribes[DAT_CurrentTribeID::instance].time == 0) { break; } diff --git a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp index d33d4d6384..6199f1263d 100644 --- a/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getUnitIDForIndexInTribe.cpp @@ -1,6 +1,6 @@ #include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" namespace OpenSHC { namespace Map { @@ -10,7 +10,7 @@ namespace Map { int TribesState::getUnitIDForIndexInTribe(int tribeID, int unitSelectionIndex) { int count = 0; - if (this->tribes[tribeID].tribeState != Map::Units::Enums::TS_ACTIVE) { + if (this->tribes[tribeID].tribeState != Map::Enums::TS_ACTIVE) { return 0; } if (unitSelectionIndex >= this->tribes[tribeID].size) { diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index bdd4bbc252..6bf80cc7a3 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -1,6 +1,6 @@ #include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" #include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -23,7 +23,7 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; + this->tribes[tribeID].tribeState = Map::Enums::TS_DISBANDED; } int const wordIndex = (int)unitID / 16; int const bitIndex = (int)unitID % 16; diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp index a3b1c027c7..7ab2c63547 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -1,6 +1,6 @@ #include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" #include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp" #include "OpenSHC/Globals/DAT_UnitsState.hpp" @@ -21,7 +21,7 @@ namespace Map { this->tribes[tribeID].size = size - 1; } if (this->tribes[tribeID].size <= 0) { - this->tribes[tribeID].tribeState = Map::Units::Enums::TS_EMPTY; + this->tribes[tribeID].tribeState = Map::Enums::TS_DISBANDED; } int const wordIndex = (int)unitID / 16; int const bitIndex = (int)unitID % 16; diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index 02da89e370..e0349bc9bf 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -1,6 +1,6 @@ #include "../TribesState.func.hpp" -#include "OpenSHC/Map/Units/Enums/TribeState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" #include "OpenSHC/Map/Units/UnitLogicState.hpp" #include "OpenSHC/Globals/DAT_CurrentTribeID.hpp" @@ -17,7 +17,7 @@ namespace Map { int const currentTribeID = DAT_CurrentTribeID::instance; int unitSelectionIndex = 0; TribesState* const shifted = (TribesState*)((char*)this + currentTribeID * sizeof(Tribe)); - if (this->tribes[currentTribeID].tribeState == Map::Units::Enums::TS_FREE) { + if (this->tribes[currentTribeID].tribeState == Map::Enums::TS_UNUSED) { return; } int const size = shifted->tribes[0].size; From 27cb7cd3609d6ae41d6d26eb1f27ddc02bffe1d6 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 22:41:04 +0100 Subject: [PATCH 32/35] playUnitSelectionSound: replace the two gotos with a siege flag The switch sets isSiege for the siege engines and the engineer and breaks; the siege block and the engineer block follow it as if/fall-through. Same code as the goto form. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/playUnitSelectionSound.cpp | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp index 8fec0215a1..24d2d3eae8 100644 --- a/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp +++ b/src/OpenSHC/Map/Units/TribesState/playUnitSelectionSound.cpp @@ -17,6 +17,7 @@ namespace Map { { int const unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::selectionContainsCombatUnit, DAT_UnitsState::ptr)(1); + bool isSiege; { int selectedCount; switch (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getMajoritySelectedUnitType, this)( @@ -81,43 +82,44 @@ namespace Map { case Map::Units::UT_S_CATAPULT: case Map::Units::UT_S_TOWER: case Map::Units::UT_S_BATTERINGRAM: - goto siegeEngine; + isSiege = true; + break; case Map::Units::UT_E_ENGINEER: - goto engineer; + isSiege = false; + break; default: return; } } - 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 - || requiredEngineers > 3) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(14); - return; - } - if (requiredEngineers == 1) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(11); - return; - } - if (requiredEngineers == 2) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(12); + if (isSiege) { + 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 + || requiredEngineers > 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(14); + return; + } + if (requiredEngineers == 1) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(11); + return; + } + if (requiredEngineers == 2) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(12); + return; + } + if (requiredEngineers == 3) { + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(13); + } return; } - if (requiredEngineers == 3) { - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playUnitSpeechEffect, DAT_SFXState::ptr)(13); - } + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DE::SHCDE::FX_SIEGE_ROLL); return; } - MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playSFXAtLocationFullVolume, DAT_SFXState::ptr)( - DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, - DE::SHCDE::FX_SIEGE_ROLL); - return; - } - engineer: if (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_ENGINEER) { return; } From bebb52317d4e3ca9d2e782b977be4ed40409e9d1 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 22:41:05 +0100 Subject: [PATCH 33/35] commandUnitsToLocation: replace the goto with a do/while(0) block The empty-tribe check and the none-commanded check both break out to the shared return FALSE. Same score as the goto form. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/commandUnitsToLocation.cpp | 137 +++++++++--------- 1 file changed, 69 insertions(+), 68 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index 6af73124bb..04e37a3fb6 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -21,81 +21,82 @@ namespace Map { int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) { int index = 0; - BOOLEnum noneCommanded; - if (this->tribes[tribeID].size <= 0) { - goto noneCommandedExit; - } - noneCommanded = TRUE; do { - int const 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) { - continue; + if (this->tribes[tribeID].size <= 0) { + break; } - 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) { + BOOLEnum noneCommanded = TRUE; + do { + int const 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) { + 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; + } + } + 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; } + DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 0; + noneCommanded = FALSE; + } while (index < this->tribes[tribeID].size); + if (noneCommanded) { + break; } - 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; + if ((char)matchUnitSpeeds != 0) { + DAT_TribesState::instance.tribes[tribeID].freeUnitSpeeds = 0; } - DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 0; - noneCommanded = FALSE; - } while (index < this->tribes[tribeID].size); - if (noneCommanded) { - noneCommandedExit: - return FALSE; - } - if ((char)matchUnitSpeeds != 0) { - DAT_TribesState::instance.tribes[tribeID].freeUnitSpeeds = 0; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); - return TRUE; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); + return TRUE; + } while (0); + return FALSE; } } From ba6725a1bb90407038127bc8a5f87906e90e89e8 Mon Sep 17 00:00:00 2001 From: Gynt Date: Fri, 4 Dec 2026 23:06:21 +0100 Subject: [PATCH 34/35] commandUnitsToLocation: use the noneCommanded flag instead of do/while(0) The empty tribe sets noneCommanded in its own branch, and the loop runs in the else branch; one test of the flag then returns FALSE. Same score as the do/while(0) and goto forms. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../TribesState/commandUnitsToLocation.cpp | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp index 04e37a3fb6..bd987e0ac8 100644 --- a/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp +++ b/src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp @@ -21,11 +21,11 @@ namespace Map { int tribeID, uint destinationX, uint destinationY, undefined4 matchUnitSpeeds) { int index = 0; - do { - if (this->tribes[tribeID].size <= 0) { - break; - } - BOOLEnum noneCommanded = TRUE; + BOOLEnum noneCommanded; + if (this->tribes[tribeID].size <= 0) { + noneCommanded = TRUE; + } else { + noneCommanded = TRUE; do { int const unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)( tribeID, index); @@ -86,17 +86,16 @@ namespace Map { DAT_UnitsState::instance.units[unitID].rallyRelatedFlag = 0; noneCommanded = FALSE; } while (index < this->tribes[tribeID].size); - if (noneCommanded) { - break; - } - if ((char)matchUnitSpeeds != 0) { - DAT_TribesState::instance.tribes[tribeID].freeUnitSpeeds = 0; - } - MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( - tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); - return TRUE; - } while (0); - return FALSE; + } + if (noneCommanded) { + return FALSE; + } + if ((char)matchUnitSpeeds != 0) { + DAT_TribesState::instance.tribes[tribeID].freeUnitSpeeds = 0; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, this)( + tribeID, destinationX, destinationY, 0, 0, Map::Units::Instructions::UMSE_0); + return TRUE; } } From 12a8768f3d1e7de8b503a4a50496feb6bc53b8ce Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 5 Dec 2026 00:26:00 +0100 Subject: [PATCH 35/35] review: drop redundant casts Every removed cast was checked one at a time: the file's /FA listing is identical without it and no new warning appears. createAnimal keeps the createTribe result in an int instead of casting a dword local. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp --- .../Map/Units/TribesState/createAnimal.cpp | 18 +++++++++--------- .../Map/Units/TribesState/createTribe.cpp | 4 ++-- .../Units/TribesState/createTribeForPlayer.cpp | 4 ++-- .../Units/TribesState/getAvailableTribeID.cpp | 2 +- .../TribesState/moveUnitToBehaviorTarget.cpp | 2 +- .../Units/TribesState/processDeerMoving.cpp | 4 ++-- .../removeUnitFromThisTribeIfInTribe.cpp | 6 +++--- .../Units/TribesState/removeUnitFromTribe.cpp | 6 +++--- .../snapshotSelectionTribeAndComputeStance.cpp | 2 +- .../TribesState/spawnDeerLionOrRabbit.cpp | 4 ++-- .../Map/Units/TribesState/trimTribeToSize.cpp | 2 +- .../TribesState/updateCamelTribeBehavior.cpp | 4 ++-- .../updateLionWolfTribeBehavior.cpp | 4 ++-- .../TribesState/updateRabbitTribeBehavior.cpp | 4 ++-- .../upgradeTribeArrayLayoutForMapVersion.cpp | 4 ++-- 15 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp index 0c879867eb..a52127ba56 100644 --- a/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createAnimal.cpp @@ -28,8 +28,8 @@ namespace Map { { UnitType unitType = Map::Units::UT_NONE; int animalCount = 0; - dword tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(0, 0); - if ((int)tribeID <= 0) { + int tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, this)(0, 0); + if (tribeID <= 0) { return 0; } if (x > Map::MC_MAP_XY_LIMIT_INCLUSIVE) { @@ -53,28 +53,28 @@ namespace Map { } this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN; unitType = Map::Units::UT_ANTELOPESHDEER; - animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 9; + animalCount = (SEC_RNG::instance.currentNumber2 & 3) + 9; } else if (animalType == Commands::M_MAPPER_LION) { if (DAT_TileMapState::instance.LogicLayer[logicTile] & blockingTerrain) { return 0; } this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_PIKEMEN; unitType = Map::Units::UT_LIONSHWOLF; - animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 1) + 3; + animalCount = (SEC_RNG::instance.currentNumber2 & 1) + 3; } else if (animalType == Commands::M_MAPPER_RABBIT) { if (DAT_TileMapState::instance.LogicLayer[logicTile] & blockingTerrain) { return 0; } this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_CROSSBOWMEN; unitType = Map::Units::UT_RABBIT; - animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 7) + 14; + animalCount = (SEC_RNG::instance.currentNumber2 & 7) + 14; } else if (animalType == Commands::M_MAPPER_CAMEL) { if (DAT_TileMapState::instance.LogicLayer[logicTile] & blockingTerrain) { return 0; } - this->tribes[tribeID].tribeType = (AITribeType)16; + this->tribes[tribeID].tribeType = 16; unitType = Map::Units::UT_CAMELSHBEAR; - animalCount = ((byte)SEC_RNG::instance.currentNumber2 & 3) + 2; + animalCount = (SEC_RNG::instance.currentNumber2 & 3) + 2; } else { if (animalType == Commands::M_MAPPER_CROW_SEAGULL) { MACRO_CALL_MEMBER(Map::LandscapeState_Func::markNearbyTreesAsCrowTargets, DAT_LandscapeState::ptr)( @@ -96,9 +96,9 @@ namespace Map { if (unitID != 0) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, this)(unitID, tribeID); DAT_UnitsState::instance.units[unitID].substate - = (short)(DAT_UnitsState::instance.units[unitID].fixedRng & 3); + = DAT_UnitsState::instance.units[unitID].fixedRng & 3; if (unitType == Map::Units::UT_ANTELOPESHDEER) { - byte const rngValue = (byte)DAT_UnitsState::instance.units[unitID].fixedRng & 7; + byte const rngValue = DAT_UnitsState::instance.units[unitID].fixedRng & 7; DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = rngValue; if (rngValue == 3) { DAT_UnitsState::instance.units[unitID].antelopeBasedRngValue = 1; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp index d10f88a447..f37ce12cc3 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribe.cpp @@ -39,10 +39,10 @@ namespace Map { this->tribes[tribeID].tribeState = Map::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; - this->tribes[tribeID].tribeType = (AITribeType)0; + this->tribes[tribeID].tribeType = 0; this->tribes[tribeID].field_0x1c = 0; this->tribes[tribeID].field_0x1e = 0; - this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].tribeBehaviorType = 0; this->tribes[tribeID].field_0x30 = 0; this->tribes[tribeID].selectionTargetUnitID = 0; this->tribes[tribeID].size = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp index b33c3dac60..a73c1124b7 100644 --- a/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp +++ b/src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp @@ -32,10 +32,10 @@ namespace Map { this->tribes[tribeID].tribeState = Map::Enums::TS_ACTIVE; this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker; ++DAT_GameCore::instance.uniqueGameObjectTracker; - this->tribes[tribeID].tribeType = (AITribeType)0; + this->tribes[tribeID].tribeType = 0; this->tribes[tribeID].field_0x1c = 0; this->tribes[tribeID].field_0x1e = 0; - this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].tribeBehaviorType = 0; this->tribes[tribeID].field_0x30 = 0; this->tribes[tribeID].selectionTargetUnitID = 0; this->tribes[tribeID].size = 0; diff --git a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp index b5581d328a..366813c0fb 100644 --- a/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp +++ b/src/OpenSHC/Map/Units/TribesState/getAvailableTribeID.cpp @@ -41,7 +41,7 @@ namespace Map { if (tribeID <= 0) { return 0; } - this->tribes[tribeID].time = (short)(DAT_GameCore::instance.mapTimeInTicks + 400); + this->tribes[tribeID].time = DAT_GameCore::instance.mapTimeInTicks + 400; return tribeID; } diff --git a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp index ad868ee3a5..c69a541559 100644 --- a/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp +++ b/src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp @@ -117,7 +117,7 @@ namespace Map { DAT_UnitsState::instance.units[unitID].targetedBuildingTile = tile; 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; + = tile - 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; diff --git a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp index 799b3fed08..468566f689 100644 --- a/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp +++ b/src/OpenSHC/Map/Units/TribesState/processDeerMoving.cpp @@ -111,7 +111,7 @@ namespace Map { } return; } - if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (this->tribes[tribeID].tribeBehaviorType == 0) { if (marshTiles != 0) { this->tribes[tribeID].wanderIntervalTicks = 200; } @@ -143,7 +143,7 @@ namespace Map { ++this->tribes[tribeID].unknownAttackRelatedUpdateCounter; if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 800) { this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; - this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].tribeBehaviorType = 0; } } } diff --git a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp index 6bf80cc7a3..7cb2ce3c86 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromThisTribeIfInTribe.cpp @@ -13,8 +13,8 @@ namespace Map { void TribesState::removeUnitFromThisTribeIfInTribe(uint unitID, int tribeID) { if (DAT_UnitsState::instance.units[unitID].tribeID == 0 - && ((int)this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] - & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) + && (this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] + & DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) == 0) { return; } @@ -34,7 +34,7 @@ namespace Map { DAT_UnitsState::instance.units[unitID].tribeID = 0; DAT_UnitsState::instance.units[unitID].idInTribe = 0; } - if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { + if (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 index 7ab2c63547..62ee7230fe 100644 --- a/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp +++ b/src/OpenSHC/Map/Units/TribesState/removeUnitFromTribe.cpp @@ -13,8 +13,8 @@ namespace Map { void TribesState::removeUnitFromTribe(uint unitID, int tribeID) { if (DAT_UnitsState::instance.units[unitID].tribeID != 0 - || ((int)this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] - & (int)DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) + || (this->tribes[tribeID].unitSelectionBitMasked[(int)unitID / 16] + & DAT_UnitSelectionDefinedData::instance.BitMaskHelper[(int)unitID % 16]) != 0) { short const size = this->tribes[tribeID].size; if (size > 0) { @@ -32,7 +32,7 @@ namespace Map { DAT_UnitsState::instance.units[unitID].tribeID = 0; DAT_UnitsState::instance.units[unitID].idInTribe = 0; } - if ((int)this->tribes[tribeID].selectionTargetUnitID == unitID) { + if (this->tribes[tribeID].selectionTargetUnitID == unitID) { MACRO_CALL_MEMBER(Map::Units::TribesState_Func::setTargetUnitForTribe, this)(tribeID); } } diff --git a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp index 167983415c..3655cf2fd2 100644 --- a/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp +++ b/src/OpenSHC/Map/Units/TribesState/snapshotSelectionTribeAndComputeStance.cpp @@ -22,7 +22,7 @@ namespace Map { stanceCount[2] = 0; BOOL multipleTribes = FALSE; int lastTribeID = -1; - for (int unitID = 1; unitID < (int)DAT_UnitsState::instance.maxUnitCount; ++unitID) { + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; ++unitID) { if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL && !DAT_UnitsState::instance.units[unitID].dying && DAT_UnitsState::instance.units[unitID].ifSelectedThenPlayerID == playerID) { diff --git a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp index b174a674c9..b596b0a985 100644 --- a/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp +++ b/src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp @@ -40,7 +40,7 @@ namespace Map { int const terrainHeight = DAT_UnitsState::instance.units[unitID].terrainOrClimbHeight; short const herdMinSize = shifted->tribes[0].herdMinSize; short const size = shifted->tribes[0].size; - int const roll = (byte)SEC_RNG::instance.currentNumber2 & 0x7f; + int const roll = SEC_RNG::instance.currentNumber2 & 0x7f; if (size <= herdMinSize) { spawnInterval /= 4; } @@ -69,7 +69,7 @@ namespace Map { } 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); + = DAT_UnitsState::instance.units[spawnedUnitID].fixedRng & 3; if (unitType == Map::Units::UT_ANTELOPESHDEER) { DAT_UnitsState::instance.units[spawnedUnitID].antelopeBasedRngValue = 2; } diff --git a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp index e0349bc9bf..187a5b93d6 100644 --- a/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp +++ b/src/OpenSHC/Map/Units/TribesState/trimTribeToSize.cpp @@ -29,7 +29,7 @@ namespace Map { currentTribeID, unitSelectionIndex); UnitLogicStateShort const logicalState = DAT_UnitsState::instance.units[unitID].logicalState; ++unitSelectionIndex; - if ((logicalState == (UnitLogicState)1 || logicalState == Map::Units::ULS_NORMAL + if ((logicalState == 1 || logicalState == Map::Units::ULS_NORMAL || logicalState == Map::Units::ULS_TRANSITIONING) && unitSelectionIndex >= smallerSize) { DAT_UnitsState::instance.units[unitID].logicalState = Map::Units::ULS_REMOVE; diff --git a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp index 09419b12ce..9465f0b0e4 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateCamelTribeBehavior.cpp @@ -64,7 +64,7 @@ namespace Map { this->tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; return; } - if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (this->tribes[tribeID].tribeBehaviorType == 0) { if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter++ == 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 2, 1); @@ -86,7 +86,7 @@ namespace Map { } ++this->tribes[tribeID].unknownAttackRelatedUpdateCounter; if ((short)this->tribes[tribeID].unknownAttackRelatedUpdateCounter >= timeout) { - this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].tribeBehaviorType = 0; this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp index de7579dfe6..2d3f2334d0 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateLionWolfTribeBehavior.cpp @@ -74,7 +74,7 @@ namespace Map { this->tribes[tribeID].unknownCounter01 = 0; return; } - if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (this->tribes[tribeID].tribeBehaviorType == 0) { if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter++ == 0) { MACRO_CALL_MEMBER(Map::WildlifeState_Func::findAndSetNewRallyPointForDeerAndLions, DAT_WildlifeState::ptr)(tribeID, 2, 0); @@ -100,7 +100,7 @@ namespace Map { } else if (this->tribes[tribeID].tribeBehaviorType == Map::Units::STBT_1) { ++this->tribes[tribeID].unknownAttackRelatedUpdateCounter; if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter == 300) { - this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].tribeBehaviorType = 0; this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp index cb2efe49fb..64124fd208 100644 --- a/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp +++ b/src/OpenSHC/Map/Units/TribesState/updateRabbitTribeBehavior.cpp @@ -70,7 +70,7 @@ namespace Map { } return; } - if (this->tribes[tribeID].tribeBehaviorType == (SomeTribeBehaviorType)0) { + if (this->tribes[tribeID].tribeBehaviorType == 0) { if (this->tribes[tribeID].unknownAttackRelatedUpdateCounter++ == 0) { if (!MACRO_CALL_MEMBER( Map::WildlifeState_Func::buildRallyPointPathForTribe, DAT_WildlifeState::ptr)(tribeID, 4)) { @@ -104,7 +104,7 @@ namespace Map { ++this->tribes[tribeID].unknownAttackRelatedUpdateCounter; if ((short)this->tribes[tribeID].unknownAttackRelatedUpdateCounter >= this->tribes[tribeID].wanderIntervalTicks) { - this->tribes[tribeID].tribeBehaviorType = (SomeTribeBehaviorType)0; + this->tribes[tribeID].tribeBehaviorType = 0; this->tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; } } diff --git a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp index 3d6c393b53..611e0671b6 100644 --- a/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp +++ b/src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp @@ -12,11 +12,11 @@ namespace Map { void TribesState::upgradeTribeArrayLayoutForMapVersion( PackagedFileMagicNum receivedMapVersion, PackagedFileMagicNum packagerMapVersion) { - if (receivedMapVersion != packagerMapVersion && (int)receivedMapVersion < 169) { + if (receivedMapVersion != packagerMapVersion && receivedMapVersion < 169) { for (int tribeIndex = countof(this->tribes) - 1; tribeIndex >= 0; --tribeIndex) { char* const oldRecord = (char*)this->tribes + tribeIndex * 620; Tribe* const tribe = &this->tribes[tribeIndex]; - if ((void*)tribe != (void*)oldRecord) { + if ((void*)tribe != oldRecord) { MACRO_CALL(OpenSHC::OS_Func::_memmove)(tribe, oldRecord, 620); } MACRO_CALL(OpenSHC::OS_Func::_memmove)(&tribe->field_0x1c8, oldRecord + 256, 364);