-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAI.cpp
More file actions
197 lines (155 loc) · 8.77 KB
/
AI.cpp
File metadata and controls
197 lines (155 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "AI.h"
#include "Runtime.h"
#include "EventSystems.h"
#include "Data.h"
#include <functional>
#include <string>
namespace AI::CongressSupport {
// TODO: Find a way to optimize target choosers. Currently they are ran for every player (even minor civs), every turn, and for both types of outcomes.
// This is a problem with the game's base code, and could prove problematic for performance if we hook these functions by calling lua code.
static bool HandleTargetChooser(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType,
const std::string& name, const std::string& decisionKey, Types::TargetChooser baseChooser, void* modifierAnalysis, unsigned int fieldOffset)
{
using namespace EventSystems;
using namespace Data;
if (DoesProcessorExist(name)) {
auto variantMap = LuaVariantMap();
variantMap.emplace("OutcomeType", LuaVariant(outcomeType));
variantMap.emplace("PlayerId", LuaVariant(*(int*)((uintptr_t)player + 0xd8)));
variantMap.emplace(decisionKey, LuaVariant(*(int*)((uintptr_t)modifierAnalysis + fieldOffset)));
std::cout << "Calling Target Chooser: " << name << "!\n";
if (CallCustomProcessor(name, variantMap)) {
int decisionType = std::get<int>(variantMap.at(decisionKey));
std::cout << "Decision type: " << decisionType << '\n';
if (decisionType == -1) {
return false;
}
*(int*)((uintptr_t)modifierAnalysis + fieldOffset) = decisionType;
return true;
}
}
return baseChooser(congressSupport, player, outcomeType, modifierAnalysis);
}
Types::TargetChooser orig_District;
Types::TargetChooser base_District;
bool District(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"DistrictTargetChooser", "DistrictIndex", base_District, modifierAnalysis, 0x1c);
}
Types::TargetChooser orig_UnitPromotionClass;
Types::TargetChooser base_UnitPromotionClass;
bool UnitPromotionClass(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"UnitPromotionClassTargetChooser", "UnitPromotionClassIndex", base_UnitPromotionClass, modifierAnalysis, 0x58);
}
Types::TargetChooser orig_UnitBuildYield;
Types::TargetChooser base_UnitBuildYield;
bool UnitBuildYield(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"UnitBuildYieldTargetChooser", "YieldIndex", base_UnitBuildYield, modifierAnalysis, 0x64);
}
Types::TargetChooser orig_TradingPartners;
Types::TargetChooser base_TradingPartners;
bool TradingPartners(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"TradingPartnersTargetChooser", "TargetPlayerId", base_TradingPartners, modifierAnalysis, 0x40);
}
Types::TargetChooser orig_PlayerOrDiploLeader;
Types::TargetChooser base_PlayerOrDiploLeader;
bool PlayerOrDiploLeader(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"PlayerOrDiploLeaderTargetChooser", "TargetPlayerId", base_PlayerOrDiploLeader, modifierAnalysis, 0x40);
}
// Unused normally
Types::TargetChooser orig_GreatPersonClass;
Types::TargetChooser base_GreatPersonClass;
bool GreatPersonClass(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"GreatPersonClassTargetChooser", "GreatPersonClassIndex", base_GreatPersonClass, modifierAnalysis, 0x6c);
}
Types::TargetChooser orig_GreatPersonPatronage;
Types::TargetChooser base_GreatPersonPatronage;
bool GreatPersonPatronage(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"GreatPersonPatronageTargetChooser", "GreatPersonClassIndex", base_GreatPersonClass, modifierAnalysis, 0x6c);
}
Types::TargetChooser orig_SpyOperation;
Types::TargetChooser base_SpyOperation;
bool SpyOperation(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"SpyOperationTargetChooser", "UnitOperationIndex", base_SpyOperation, modifierAnalysis, 0x54);
}
Types::TargetChooser orig_MostCommonLuxury;
Types::TargetChooser base_MostCommonLuxury;
bool MostCommonLuxury(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"MostCommonLuxuryTargetChooser", "ResourceIndex", base_MostCommonLuxury, modifierAnalysis, 0x4c);
}
Types::TargetChooser orig_MinorCivBonus;
Types::TargetChooser base_MinorCivBonus;
bool MinorCivBonus(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"MinorCivBonusTargetChooser", "MinorCivBonusIndex", base_MinorCivBonus, modifierAnalysis, 0x3c);
}
Types::TargetChooser orig_GrievancesType;
Types::TargetChooser base_GrievancesType;
bool GrievancesType(Types::Class* congressSupport, Player::Instance* player, OutcomeType outcomeType, void* modifierAnalysis) {
return HandleTargetChooser(congressSupport, player, outcomeType,
"GrievancesTypeTargetChooser", "TargetPlayerId", base_GrievancesType, modifierAnalysis, 0x40);
}
int RegisterOutcomeTypes(hks::lua_State* L) {
hks::createtable(L, 0, 2);
hks::pushinteger(L, OutcomeType::A);
hks::setfield(L, -2, "A");
hks::pushinteger(L, OutcomeType::B);
hks::setfield(L, -2, "B");
hks::setfield(L, hks::LUA_GLOBAL, "OutcomeTypes");
return 0;
}
void Create() {
using namespace Runtime;
orig_District = GetGameCoreGlobalAt<Types::TargetChooser>(DISTRICT_OFFSET);
CreateHook(orig_District, &District, &base_District);
orig_UnitPromotionClass = GetGameCoreGlobalAt<Types::TargetChooser>(UNIT_PROMOTION_CLASS_OFFSET);
CreateHook(orig_UnitPromotionClass, &UnitPromotionClass, &base_UnitPromotionClass);
orig_UnitBuildYield = GetGameCoreGlobalAt<Types::TargetChooser>(UNIT_BUILD_YIELD_OFFSET);
CreateHook(orig_UnitBuildYield, &UnitBuildYield, &base_UnitBuildYield);
orig_TradingPartners = GetGameCoreGlobalAt<Types::TargetChooser>(TRADING_PARTNERS_OFFSET);
CreateHook(orig_TradingPartners, &TradingPartners, &base_TradingPartners);
orig_PlayerOrDiploLeader = GetGameCoreGlobalAt<Types::TargetChooser>(PLAYER_OR_DIPLO_LEADER_OFFSET);
CreateHook(orig_PlayerOrDiploLeader, &PlayerOrDiploLeader, &base_PlayerOrDiploLeader);
orig_GreatPersonClass = GetGameCoreGlobalAt<Types::TargetChooser>(GREAT_PERSON_CLASS_OFFSET);
CreateHook(orig_GreatPersonClass, &GreatPersonClass, &base_GreatPersonClass);
orig_GreatPersonPatronage = GetGameCoreGlobalAt<Types::TargetChooser>(GREAT_PERSON_PATRONAGE_OFFSET);
CreateHook(orig_GreatPersonPatronage, &GreatPersonPatronage, &base_GreatPersonPatronage);
orig_SpyOperation = GetGameCoreGlobalAt<Types::TargetChooser>(SPY_OPERATION_OFFSET);
CreateHook(orig_SpyOperation, &SpyOperation, &base_SpyOperation);
orig_MostCommonLuxury = GetGameCoreGlobalAt<Types::TargetChooser>(MOST_COMMON_LUXURY_OFFSET);
CreateHook(orig_MostCommonLuxury, &MostCommonLuxury, &base_MostCommonLuxury);
orig_MinorCivBonus = GetGameCoreGlobalAt<Types::TargetChooser>(MINOR_CIV_BONUS_OFFSET);
CreateHook(orig_MinorCivBonus, &MinorCivBonus, &base_MinorCivBonus);
orig_GrievancesType = GetGameCoreGlobalAt<Types::TargetChooser>(GRIEVANCES_TYPE_OFFSET);
CreateHook(orig_GrievancesType, &GrievancesType, &base_GrievancesType);
}
}
namespace AI::Espionage {
Types::GetAIEspionage GetAIEspionage;
Types::GetMostUsedSpyMission GetMostUsedSpyMission;
static int lGetMostUsedSpyMission(hks::lua_State* L) {
Player::Instance* player = Player::GetPlayerInstance(L);
hks::pushinteger(L, GetMostUsedSpyMission(GetAIEspionage(player)));
return 1;
}
int RegisterAIEspionageManager(hks::lua_State* L) {
std::cout << "Registering AIEspionageManager!\n";
hks::createtable(L, 0, 1);
PushLuaMethod(L, lGetMostUsedSpyMission, "lGetMostUsedSpyMission", -2, "GetMostUsedSpyMission");
hks::setfield(L, hks::LUA_GLOBAL, "AIEspionageManager");
return 0;
}
void Create() {
using namespace Runtime;
GetAIEspionage = GetGameCoreGlobalAt<Types::GetAIEspionage>(GET_AI_ESPIONAGE_OFFSET);
GetMostUsedSpyMission = GetGameCoreGlobalAt<Types::GetMostUsedSpyMission>(GET_MOST_USED_SPY_MISSION_OFFSET);
}
}