Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions cmake/compiler-flags-gl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,10 @@ src/OpenSHC/Map/Units/UpdateHunter.cpp
src/OpenSHC/Map/Units/UpdateAppleFarmer.cpp
src/OpenSHC/Map/Units/UpdateJester.cpp
src/OpenSHC/Map/Units/UpdateSlave.cpp
src/OpenSHC/Map/Units/TroopValueState/assignMacemenAndKnightsNotFromPlayer1ToTribes.cpp
src/OpenSHC/Map/Units/TroopValueState/getPlayerNot1AndHasKeep.cpp
src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp
src/OpenSHC/Map/Units/TribesState/setWhetherTribeContainsAnyUnits.cpp
src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentsAndAssignEngineers.cpp
src/OpenSHC/Map/Units/UpdateHealer.cpp
src/OpenSHC/Map/Units/UpdateSpearman.cpp
23 changes: 23 additions & 0 deletions src/OpenSHC/Globals/DAT_ARRAY_00b4c7cc.hpp
Original file line number Diff line number Diff line change
@@ -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/Globals/DAT_ARRAY_00b4c7cc.hpp'
*/

#pragma once

#include "OpenSHC/Commands/MappersEnum.hpp"

// GLOBAL: STRONGHOLDCRUSADER 0x00B4C7CC
// StructResolver::Instance<enum OpenSHC::Commands::MappersEnum [9],11847628>::instance

namespace OpenSHC {

using OpenSHC::Commands::MappersEnum;

MACRO_STRUCT_RESOLVER(MappersEnum[9], false, Address::SHC_3BB0A8C1_0x00B4C7CC) DAT_ARRAY_00b4c7cc;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new hpp file really necsesary, why is this not part of AttackInfoDefinedData?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it, and yes, the separate global is necessary for the match.

Both tables do sit inside AttackInfoDefinedData (0xB4C704): this one is field_0xc8, and INT_ARRAY_00b4d0f0 is field_0x9ec. So I removed both headers and read DAT_AttackInfoDefinedData::instance.field_0xc8[k] and ((int*)...field_0x9ec)[k] instead. With both resolver sets on, placeSiegeTentsAndAssignEngineers went from 97.4% to 91.0% (normalized 88.7% → 82.4%). addUnitToTribe and getPlayerNot1AndHasKeep stayed at 100%.

The reason is visible in the diff. The original indexes both tables from one zero-based counter:

xor edx, edx
mov esi, [edx + AttackInfoDefinedData+0xC8]
mov ecx, [edx + AttackInfoDefinedData+0x9EC]
add edx, 4 / cmp edx, 0x10

Once both are fields of one struct, MSVC knows they are 0x924 apart. It walks one pointer instead, and reaches the other table at a fixed offset from it:

mov edx, AttackInfoDefinedData+0x9EC
mov esi, [edx - 0x924]
mov ecx, [edx]

The original compiler did not do that, so it saw two unrelated objects. In the original source these are separate globals, not members of AttackInfoDefinedData. That is why I am keeping DAT_ARRAY_00b4c7cc and INT_ARRAY_00b4d0f0 as they are.

One follow-up: the merge with main took main's AttackInfoDefinedData.hpp, which still declares field_0xc8..field_0xe8 and field_0x9ec[72] over the same memory. The real fix is in Ghidra: shrink AttackInfoDefinedData (or mark those ranges as gaps) so the two tables are standalone globals there too. That has no effect on codegen.

} // namespace OpenSHC

MACRO_STRUCT_INSTANCE(Address::SHC_3BB0A8C1_0x00B4C7CC);
19 changes: 19 additions & 0 deletions src/OpenSHC/Globals/INT_ARRAY_00b4d0f0.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
THIS FILE IS AUTO GENERATED
Communicate changes to the dev team (e.g. via a Pull Request).
Changes get lost otherwise.

path: 'OpenSHC/Globals/INT_ARRAY_00b4d0f0.hpp'
*/

#pragma once

// GLOBAL: STRONGHOLDCRUSADER 0x00B4D0F0
// StructResolver::Instance<int [9],11849968>::instance

namespace OpenSHC {

MACRO_STRUCT_RESOLVER(int[9], false, Address::SHC_3BB0A8C1_0x00B4D0F0) INT_ARRAY_00b4d0f0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer as for DAT_ARRAY_00b4c7cc, see #528 (comment). Folding this table into AttackInfoDefinedData::field_0x9ec lets MSVC address both tables from one pointer ([edx - 0x924] / [edx]). The original indexes them independently from a zero-based counter. That drops placeSiegeTentsAndAssignEngineers from 97.4% to 91.0%, so it has to stay a global of its own.

} // namespace OpenSHC

MACRO_STRUCT_INSTANCE(Address::SHC_3BB0A8C1_0x00B4D0F0);
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 = this->tribes[tribeID].size + 1;
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);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include "../../../Map.func.hpp"

#include "OpenSHC/Map/Units/TribesState.func.hpp"
#include "OpenSHC/Map/Units/TroopValueState.func.hpp"
#include "OpenSHC/AI/Tribes/AITribeType.hpp"
#include "OpenSHC/Map/Units/UnitLogicState.hpp"
#include "OpenSHC/Map/Units/UnitType.hpp"
#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp"

#include "OpenSHC/Globals/DAT_TribesState.hpp"
#include "OpenSHC/Globals/DAT_TroopValueState.hpp"
#include "OpenSHC/Globals/DAT_UnitsState.hpp"

namespace OpenSHC {
namespace Map {
namespace Units {

using AI::Tribes::AITribeType;
using Map::Units::UnitLogicState;
using Map::Units::UnitType;
using WindowsHelper::Enums::BOOLEnum;

// FUNCTION: STRONGHOLDCRUSADER 0x0051CCB0
void TroopValueState::assignMacemenAndKnightsNotFromPlayer1ToTribes()
{
int const playerID = DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID;
int macemenCount = 0;
int knightCount = 0;
for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; unitID++) {
if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_INVISIBLE
|| !MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)(
DAT_UnitsState::instance.units[unitID].owner)) {
continue;
}
if (DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_E_MACE) {
macemenCount = macemenCount + 1;
} else if (DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_E_KNIGHT) {
knightCount = knightCount + 1;
} else {
continue;
}
MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, DAT_TribesState::ptr)(
unitID, DAT_UnitsState::instance.units[unitID].tribeID);
}
int macemenLimit;
if (macemenCount > 40) {
macemenLimit = 10;
} else if (macemenCount > 20) {
macemenLimit = 8;
} else {
macemenLimit = 5;
}
int knightLimit;
if (knightCount > 18) {
knightLimit = 6;
} else if (knightCount > 12) {
knightLimit = 4;
} else {
knightLimit = 2;
}
if (macemenCount <= 0) {
DAT_TroopValueState::instance.attackInfo.macemenTribeCount = 0;
} else if (macemenCount <= macemenLimit) {
DAT_TroopValueState::instance.attackInfo.macemenTribeCount = 1;
} else {
DAT_TroopValueState::instance.attackInfo.macemenTribeCount = macemenCount / macemenLimit + 1;
if (DAT_TroopValueState::instance.attackInfo.macemenTribeCount > 100) {
DAT_TroopValueState::instance.attackInfo.macemenTribeCount = 100;
}
}
if (knightCount <= 0) {
DAT_TroopValueState::instance.attackInfo.knightTribeCount = 0;
} else if (knightCount <= knightLimit) {
DAT_TroopValueState::instance.attackInfo.knightTribeCount = 1;
} else {
DAT_TroopValueState::instance.attackInfo.knightTribeCount = knightCount / knightLimit + 1;
if (DAT_TroopValueState::instance.attackInfo.knightTribeCount > 100) {
DAT_TroopValueState::instance.attackInfo.knightTribeCount = 100;
}
}
int macemenUnitID = 0;
int tribeIndex;
for (tribeIndex = 0; tribeIndex < DAT_TroopValueState::instance.attackInfo.macemenTribeCount;
tribeIndex++) {
int const tribeID
= MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)(playerID, 0);
DAT_TroopValueState::instance.attackInfo.macemenTribeArray[tribeIndex] = tribeID;
DAT_TribesState::instance.tribes[tribeID].tribeType = AI::Tribes::AITT_MACEMEN;
for (int assigned = 0; assigned < macemenLimit;) {
macemenUnitID = macemenUnitID + 1;
if (DAT_UnitsState::instance.units[macemenUnitID].logicalState != Map::Units::ULS_INVISIBLE
&& MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)(
DAT_UnitsState::instance.units[macemenUnitID].owner)
&& DAT_UnitsState::instance.units[macemenUnitID].unitType == Map::Units::UT_E_MACE) {
MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)(
macemenUnitID, tribeID);
macemenCount = macemenCount - 1;
if (macemenCount <= 0) {
break;
}
assigned = assigned + 1;
}
}
if (macemenCount <= 0) {
break;
}
}
int knightUnitID = 0;
for (tribeIndex = 0; tribeIndex < DAT_TroopValueState::instance.attackInfo.knightTribeCount; tribeIndex++) {
int const tribeID
= MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)(playerID, 0);
DAT_TroopValueState::instance.attackInfo.knightTribeArray[tribeIndex] = tribeID;
DAT_TribesState::instance.tribes[tribeID].tribeType = AI::Tribes::AITT_KNIGHTS;
for (int assigned = 0; assigned < knightLimit;) {
knightUnitID = knightUnitID + 1;
if (DAT_UnitsState::instance.units[knightUnitID].logicalState != Map::Units::ULS_INVISIBLE
&& MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)(
DAT_UnitsState::instance.units[knightUnitID].owner)
&& DAT_UnitsState::instance.units[knightUnitID].unitType == Map::Units::UT_E_KNIGHT) {
MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)(
knightUnitID, tribeID);
knightCount = knightCount - 1;
if (knightCount <= 0) {
return;
}
assigned = assigned + 1;
}
}
if (knightCount <= 0) {
break;
}
}
}

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

#include "OpenSHC/Map.func.hpp"
#include "OpenSHC/Game/GameMode2.hpp"
#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp"
#include "OpenSHC/Map/MapType2.hpp"
#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp"

#include "OpenSHC/Globals/DAT_GameCore.hpp"
#include "OpenSHC/Globals/DAT_GameState.hpp"
#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp"

namespace OpenSHC {
namespace Map {
namespace Units {

using WindowsHelper::Enums::BOOLEnum;

// FUNCTION: STRONGHOLDCRUSADER 0x0051BDE0
BOOLEnum TroopValueState::getPlayerNot1AndHasKeep(int playerID)
{
if (DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk
&& DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) {
if (playerID == 1) {
return TRUE;
}
} else if (playerID != 1
&& 0 < DAT_GameState::instance.playerDataArray[playerID]
.buildingEntryInfos[Game::Player::Enums::BEIE_KEEP]
.id) {
return TRUE;
}
return FALSE;
}

}
}
}
Loading
Loading