Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a6a0fc1
TribesState: moveTribeToIndexedNearbyTile returns void
gynt Dec 4, 2026
4d3a96f
reimplement: SHC_3BB0A8C1_0x00522590 100.0% Reimplemented
gynt Dec 4, 2026
e4da26b
reimplement: SHC_3BB0A8C1_0x00522890 100.0% Reimplemented
gynt Dec 4, 2026
a21fd85
reimplement: SHC_3BB0A8C1_0x005227E0 100.0% Reimplemented
gynt Dec 4, 2026
0790e91
reimplement: SHC_3BB0A8C1_0x00522720 100.0% Reimplemented
gynt Dec 4, 2026
2725417
reimplement: SHC_3BB0A8C1_0x00522390 100.0% Reimplemented
gynt Dec 4, 2026
5a0a411
reimplement: SHC_3BB0A8C1_0x00521280 100.0% Reimplemented
gynt Dec 4, 2026
33d9309
reimplement: SHC_3BB0A8C1_0x0052AD50 100.0% Reimplemented
gynt Dec 4, 2026
32b0b77
reimplement: SHC_3BB0A8C1_0x00522630 100.0% Reimplemented
gynt Dec 4, 2026
4283f7b
reimplement: SHC_3BB0A8C1_0x00525A70 100.0% Reimplemented
gynt Dec 4, 2026
98f4e7c
reimplement: SHC_3BB0A8C1_0x00522CA0 100.0% Reimplemented
gynt Dec 4, 2026
e584cc8
reimplement: SHC_3BB0A8C1_0x00525F20 100.0% Reimplemented
gynt Dec 4, 2026
090816d
reimplement: SHC_3BB0A8C1_0x00522EF0 100.0% Reimplemented
gynt Dec 4, 2026
49d284f
reimplement: SHC_3BB0A8C1_0x0052B630 100.0% Reimplemented
gynt Dec 4, 2026
7e829bd
reimplement: SHC_3BB0A8C1_0x0052B110 100.0% Reimplemented
gynt Dec 4, 2026
edac618
reimplement: SHC_3BB0A8C1_0x0052B390 100.0% Reimplemented
gynt Dec 4, 2026
815c4c7
reimplement: SHC_3BB0A8C1_0x00523EF0 100.0% Reimplemented
gynt Dec 4, 2026
ebbf72e
reimplement: SHC_3BB0A8C1_0x00521EF0 100.0% Reimplemented
gynt Dec 4, 2026
5ed5535
reimplement: SHC_3BB0A8C1_0x0052A700 100.0% Reimplemented
gynt Dec 4, 2026
8aa856e
style: addUnitToTribe uses the same file as #554
gynt Dec 4, 2026
6e3903e
Merge origin/main into reimpl/pr279-matched
gynt Dec 4, 2026
7b4ebc8
reimplement: SHC_3BB0A8C1_0x00525BA0 96.1% Register allocation differs
gynt Dec 4, 2026
ff1cbb4
reimplement: SHC_3BB0A8C1_0x00522090 98.0% Register allocation differs
gynt Dec 4, 2026
ccb6b0d
reimplement: SHC_3BB0A8C1_0x00526F00 90.3% Register allocation differs
gynt Dec 4, 2026
3e6c25a
reimplement: SHC_3BB0A8C1_0x005217E0 92.4% Register allocation differs
gynt Dec 4, 2026
b904c4a
reimplement: SHC_3BB0A8C1_0x00521A90 94.4% Register allocation differs
gynt Dec 4, 2026
77df1a5
Add the TribeState enum
gynt Dec 4, 2026
cd7adbd
Add the TribeStateShort alias
gynt Dec 4, 2026
a82ad19
review: TheRedDaemon's checklist for the TribesState functions
gynt Dec 4, 2026
83a4930
TribeState: use the TRS_ prefix
gynt Dec 4, 2026
333dc3b
TribeState: back to the TS_ prefix
gynt Dec 4, 2026
0756508
TribeState: move to Map/Enums with TS_UNUSED, TS_ACTIVE, TS_DISBANDED
gynt Dec 4, 2026
76be8cd
Merge origin/main into reimpl/pr279-matched
gynt Dec 4, 2026
5d77c7e
Merge origin/main into reimpl/pr279-matched
gynt Dec 4, 2026
821844c
Merge origin/main into reimpl/pr279-matched
gynt Dec 4, 2026
27cb7cd
playUnitSelectionSound: replace the two gotos with a siege flag
gynt Dec 4, 2026
bebb523
commandUnitsToLocation: replace the goto with a do/while(0) block
gynt Dec 4, 2026
ba6725a
commandUnitsToLocation: use the noneCommanded flag instead of do/whil…
gynt Dec 4, 2026
239e03d
Merge origin/main into reimpl/pr279-matched
gynt Dec 4, 2026
12a8768
review: drop redundant casts
gynt Dec 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
}
103 changes: 103 additions & 0 deletions src/OpenSHC/Map/Units/TribesState/commandUnitsToLocation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#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;
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);
++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) {
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;
}

}
}
}
141 changes: 141 additions & 0 deletions src/OpenSHC/Map/Units/TribesState/createAnimal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#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/LogicHelpers/Logic1.hpp"
#include "OpenSHC/Map/MapConstants.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;

// FUNCTION: STRONGHOLDCRUSADER 0x00525BA0
dword TribesState::createAnimal(MappersEnum animalType, uint x, uint y, int tile)
{
UnitType unitType = Map::Units::UT_NONE;
int animalCount = 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) {
return 0;
}
if (y > Map::MC_MAP_XY_LIMIT_INCLUSIVE) {
return 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] & blockingTerrain) {
return 0;
}
this->tribes[tribeID].tribeType = AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_SPEARMEN;
unitType = Map::Units::UT_ANTELOPESHDEER;
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 = (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 = (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 = 16;
unitType = Map::Units::UT_CAMELSHBEAR;
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)(
x, y);
return 0;
}
if (animalType == Commands::M_MAPPER_SEAGULL) {
if (!(DAT_TileMapState::instance.LogicLayer[logicTile] & Map::LogicHelpers::L_SEA)) {
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
= DAT_UnitsState::instance.units[unitID].fixedRng & 3;
if (unitType == Map::Units::UT_ANTELOPESHDEER) {
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;
} 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;
}

}
}
}
54 changes: 54 additions & 0 deletions src/OpenSHC/Map/Units/TribesState/createTribe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "../TribesState.func.hpp"

#include "OpenSHC/IO/LowLevelMemory.func.hpp"
#include "OpenSHC/AI/Tribes/AITribeType.hpp"
#include "OpenSHC/Map/Enums/TribeState.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;

// 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 == 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]);
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 = Map::Enums::TS_ACTIVE;
this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker;
++DAT_GameCore::instance.uniqueGameObjectTracker;
this->tribes[tribeID].tribeType = 0;
this->tribes[tribeID].field_0x1c = 0;
this->tribes[tribeID].field_0x1e = 0;
this->tribes[tribeID].tribeBehaviorType = 0;
this->tribes[tribeID].field_0x30 = 0;
this->tribes[tribeID].selectionTargetUnitID = 0;
this->tribes[tribeID].size = 0;
return tribeID;
}

}
}
}
47 changes: 47 additions & 0 deletions src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "../TribesState.func.hpp"

#include "OpenSHC/IO/LowLevelMemory.func.hpp"
#include "OpenSHC/AI/Tribes/AITribeType.hpp"
#include "OpenSHC/Map/Enums/TribeState.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;

// 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 == Map::Enums::TS_UNUSED) {
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 = Map::Enums::TS_ACTIVE;
this->tribes[tribeID].uid = DAT_GameCore::instance.uniqueGameObjectTracker;
++DAT_GameCore::instance.uniqueGameObjectTracker;
this->tribes[tribeID].tribeType = 0;
this->tribes[tribeID].field_0x1c = 0;
this->tribes[tribeID].field_0x1e = 0;
this->tribes[tribeID].tribeBehaviorType = 0;
this->tribes[tribeID].field_0x30 = 0;
this->tribes[tribeID].selectionTargetUnitID = 0;
this->tribes[tribeID].size = 0;
return tribeID;
}

}
}
}
Loading
Loading