-
Notifications
You must be signed in to change notification settings - Fork 11
/
random_costs.cpp
337 lines (286 loc) · 12.1 KB
/
random_costs.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "random_costs.h"
#include <algorithm>
#include <random>
#include <string>
#include <vector>
#include <list>
#include "genie/dat/DatFile.h"
#include "ids.h"
typedef genie::ResourceUsage<int16_t, int16_t, int16_t> ResourceCost;
typedef genie::ResourceUsage<int16_t, int16_t, uint8_t> ResearchResourceCost;
bool isNaturalResourceCost(const ResourceCost &cost) { return cost.Type != -1 && cost.Type < 4 && cost.Amount > 0 && cost.Flag == 1; }
bool isNaturalResearchResourceCost(const ResearchResourceCost &cost) {
return cost.Type != -1 && cost.Type < 4 && cost.Amount > 0 && cost.Flag == 1;
}
bool hasNaturalResourceCost(const genie::Unit &unit) {
std::vector<ResourceCost> costs = unit.Creatable.ResourceCosts;
return std::any_of(costs.begin(), costs.end(), isNaturalResourceCost);
}
bool hasNaturalResearchResourceCost(std::vector<ResearchResourceCost> costs) {
return std::any_of(costs.begin(), costs.end(), isNaturalResearchResourceCost);
}
int getSumOfNaturalResourceCosts(const std::vector<ResourceCost> &resourceCosts) {
int16_t sum = 0;
for (const ResourceCost &cost : resourceCosts) {
if (cost.Type > -1 && cost.Type < 4) {
sum += cost.Amount;
}
}
return sum;
}
int getSumOfNaturalResearchResourceCosts(const std::vector<ResearchResourceCost > &resourceCosts) {
int sum = 0;
for (const ResearchResourceCost &cost : resourceCosts) {
if (cost.Type > -1 && cost.Type < 4) {
sum += cost.Amount;
}
}
return sum;
}
ResearchResourceCost toResearchResourceCost(const ResourceCost &resourceCost) {
ResearchResourceCost researchResourceCost;
researchResourceCost.Type = resourceCost.Type;
researchResourceCost.Amount = resourceCost.Amount;
researchResourceCost.Flag = (bool)resourceCost.Flag;
return researchResourceCost;
}
ResourceCost toResourceCost(const ResearchResourceCost &researchResourceCost) {
ResourceCost resourceCost;
resourceCost.Type = researchResourceCost.Type;
resourceCost.Amount = researchResourceCost.Amount;
resourceCost.Flag = (bool)researchResourceCost.Flag;
return resourceCost;
}
std::vector<ResearchResourceCost> toResearchResourceCosts(const std::vector<ResourceCost> &resourceCosts) {
std::vector<ResearchResourceCost> researchResourceCosts;
researchResourceCosts.reserve(resourceCosts.size());
for (const ResourceCost &resourceCost : resourceCosts) {
if (resourceCost.Type < TYPE_POPULATION_HEADROOM) {
researchResourceCosts.push_back(toResearchResourceCost(resourceCost));
} else {
ResearchResourceCost researchResourceCost;
researchResourceCost.Type = -1;
researchResourceCost.Amount = 0;
researchResourceCost.Flag = false;
researchResourceCosts.push_back(researchResourceCost);
}
}
return researchResourceCosts;
}
std::vector<ResourceCost> toResourceCosts(const std::vector<ResearchResourceCost> &researchResourceCosts) {
std::vector<ResourceCost> resourceCosts;
resourceCosts.reserve(researchResourceCosts.size());
for (const ResearchResourceCost &researchResourceCost : researchResourceCosts) {
resourceCosts.push_back(toResourceCost(researchResourceCost));
}
return resourceCosts;
}
std::string costToString(const std::vector<ResourceCost> &costs) {
std::string s;
for (const ResourceCost &cost : costs) {
if (cost.Flag == 1) {
s += std::to_string(cost.Amount);
s += " ";
switch (cost.Type) {
case TYPE_FOOD:
s += "Food ";
break;
case TYPE_WOOD:
s += "Wood ";
break;
case TYPE_GOLD:
s += "Gold ";
break;
case TYPE_STONE:
s += "Stone ";
break;
case TYPE_POPULATION_HEADROOM:
s += "Pop ";
break;
default:
s += "vat?";
}
}
}
return s;
}
std::string costToString(const std::vector<ResearchResourceCost> &costs) {
std::string s;
for (const ResearchResourceCost &cost : costs) {
if (cost.Flag == 1) {
s += std::to_string(cost.Amount);
s += " ";
switch (cost.Type) {
case TYPE_FOOD:
s += "Food ";
break;
case TYPE_WOOD:
s += "Wood ";
break;
case TYPE_GOLD:
s += "Gold ";
break;
case TYPE_STONE:
s += "Stone ";
break;
case TYPE_POPULATION_HEADROOM:
s += "Pop ";
break;
default:
s += "vat?";
}
}
}
return s;
}
bool bothRequirePopulationHeadroom(int unitId, std::vector<ResourceCost> &resourceCosts, const genie::Civ &civ) {
return ((civ.Units.at(unitId).Creatable.ResourceCosts.at(2).Type == TYPE_POPULATION_HEADROOM &&
resourceCosts.at(2).Type == TYPE_POPULATION_HEADROOM) ||
(civ.Units.at(unitId).Creatable.ResourceCosts.at(2).Type != TYPE_POPULATION_HEADROOM &&
resourceCosts.at(2).Type != TYPE_POPULATION_HEADROOM));
}
void copyResourceCostAt(int unitId, int index, std::vector<ResourceCost> &target, const genie::Civ &civ) {
genie::ResourceUsage<int16_t, int16_t, int16_t> source = civ.Units.at(unitId).Creatable.ResourceCosts.at(index);
target.at(index).Type = source.Type;
target.at(index).Amount = source.Amount;
target.at(index).Flag = source.Flag;
}
void enableStablesForMesoCivs(genie::DatFile *df) {
const std::list<int> civBonusEffects = {3, 447, 449};
for (const auto &civBonusId : civBonusEffects) {
int position = 0;
for (const auto &item : df->Effects.at(civBonusId).EffectCommands) {
if (item.Type == 102 && item.D == 25) {
std::cout << "Patching civ bonus effect " << civBonusId << ": re-enabling stable" << std::endl;
df->Effects.at(civBonusId).EffectCommands.erase(
df->Effects.at(civBonusId).EffectCommands.begin() + position);
break;
}
position++;
}
}
}
void jumbleCosts(genie::DatFile *df, int limit) {
std::vector<int> unitIds;
for (genie::Unit unit : df->Civs.at(0).Units) {
if (hasNaturalResourceCost(unit) && (getSumOfNaturalResourceCosts(unit.Creatable.ResourceCosts) > limit)) {
unitIds.push_back(unit.ID);
}
}
std::vector<int> techIds;
size_t index = 0;
for (const genie::Tech &tech : df->Techs) {
std::vector<ResearchResourceCost> resourceCopy = tech.ResourceCosts;
if (hasNaturalResearchResourceCost(resourceCopy) && (getSumOfNaturalResearchResourceCosts(resourceCopy) > limit)) {
techIds.push_back(index);
}
index++;
}
std::vector<std::vector<ResourceCost>> allTheCosts;
for (int unitId : unitIds) {
std::vector<ResourceCost> resourceCopy = df->Civs.at(0).Units.at(unitId).Creatable.ResourceCosts;
allTheCosts.push_back(resourceCopy);
}
for (int techId : techIds) {
std::vector<ResourceCost> resourceCopy = toResourceCosts(df->Techs.at(techId).ResourceCosts);
allTheCosts.push_back(resourceCopy);
}
for (int i = 0; i < 10; i++) {
unsigned int seed = std::random_device()();
std::cout << "Seed for shuffling unit and tech costs is: " << std::to_string(seed) << std::endl;
shuffle(allTheCosts.begin(), allTheCosts.end(), std::mt19937(seed));
size_t costIndex = 0;
for (int techId : techIds) {
std::vector<ResearchResourceCost> researchResourceCosts =
toResearchResourceCosts(allTheCosts.at(costIndex));
std::cout << "Setting cost of tech with id " << techId << " to " << costToString(researchResourceCosts)
<< std::endl;
df->Techs.at(techId).ResourceCosts = researchResourceCosts;
costIndex++;
}
for (int unitId : unitIds) {
std::vector<ResourceCost> &resourceCosts = allTheCosts.at(costIndex);
std::cout << "Setting cost of unit with id " << unitId << " to " << costToString(resourceCosts)
<< std::endl;
if (!bothRequirePopulationHeadroom(unitId, resourceCosts, df->Civs.at(0))) {
copyResourceCostAt(unitId, 2, resourceCosts, df->Civs.at(0));
}
for (genie::Civ &civ : df->Civs) {
civ.Units.at(unitId).Creatable.ResourceCosts = resourceCosts;
}
costIndex++;
}
int maleVillagerCost =
getSumOfNaturalResourceCosts(df->Civs.at(0).Units.at(ID_VILLAGER_BASE_M).Creatable.ResourceCosts);
int femaleVillagerCost =
getSumOfNaturalResourceCosts(df->Civs.at(0).Units.at(ID_VILLAGER_BASE_F).Creatable.ResourceCosts);
if (maleVillagerCost <= 200 && femaleVillagerCost <= 200) {
return;
}
std::cout << "Villagers are too expensive, reshuffling…" << std::endl;
}
std::cout << "Giving up, villagers stay expensive, sorry." << std::endl;
}
void jumbleUnitCosts(genie::DatFile *df) {
std::vector<int> unitIds;
for (genie::Unit unit : df->Civs.at(0).Units) {
if (hasNaturalResourceCost(unit)) {
unitIds.push_back(unit.ID);
}
}
std::vector<std::vector<ResourceCost>> allTheCosts;
for (int unitId : unitIds) {
std::vector<ResourceCost> resourceCopy = df->Civs.at(0).Units.at(unitId).Creatable.ResourceCosts;
allTheCosts.push_back(resourceCopy);
}
for (int i = 0; i < 10; i++) {
unsigned int seed = std::random_device()();
std::cout << "Seed for shuffling unit costs is: " << std::to_string(seed) << std::endl;
std::shuffle(allTheCosts.begin(), allTheCosts.end(), std::mt19937(seed));
size_t index = 0;
for (int unitId : unitIds) {
std::vector<ResourceCost> &resourceCosts = allTheCosts.at(index);
std::cout << "Setting cost of unit with id " << unitId << " to " << costToString(resourceCosts)
<< std::endl;
if (!bothRequirePopulationHeadroom(unitId, resourceCosts, df->Civs.at(0))) {
copyResourceCostAt(unitId, 2, resourceCosts, df->Civs.at(0));
}
for (genie::Civ &civ : df->Civs) {
civ.Units.at(unitId).Creatable.ResourceCosts = resourceCosts;
}
index++;
}
int maleVillagerCost =
getSumOfNaturalResourceCosts(df->Civs.at(0).Units.at(ID_VILLAGER_BASE_M).Creatable.ResourceCosts);
int femaleVillagerCost =
getSumOfNaturalResourceCosts(df->Civs.at(0).Units.at(ID_VILLAGER_BASE_F).Creatable.ResourceCosts);
if (maleVillagerCost <= 200 && femaleVillagerCost <= 200) {
return;
}
std::cout << "Villagers are too expensive, reshuffling…" << std::endl;
}
std::cout << "Giving up, villagers stay expensive, sorry." << std::endl;
}
void jumbleTechCosts(genie::DatFile *df) {
std::vector<int> techIds;
std::vector<std::vector<ResearchResourceCost>> allTheCosts;
size_t index = 0;
for (const genie::Tech &tech : df->Techs) {
std::vector<ResearchResourceCost> resourceCopy = tech.ResourceCosts;
if (hasNaturalResearchResourceCost(resourceCopy)) {
allTheCosts.push_back(resourceCopy);
techIds.push_back(index);
}
index++;
}
unsigned int seed = std::random_device()();
std::cout << "Seed for shuffling tech costs is: " << std::to_string(seed) << std::endl;
shuffle(allTheCosts.begin(), allTheCosts.end(), std::mt19937(seed));
index = 0;
for (size_t techId : techIds) {
std::vector<ResearchResourceCost> &resourceCosts = allTheCosts.at(index);
std::cout << "Setting cost of tech with id " << techId << " to " << costToString(resourceCosts) << std::endl;
df->Techs.at(techId).ResourceCosts = resourceCosts;
index++;
}
}