-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathpet-uncapper.cpp
222 lines (188 loc) · 7.33 KB
/
pet-uncapper.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
#include "Debug.h"
#include "PluginManager.h"
#include "modules/Maps.h"
#include "modules/Units.h"
#include "modules/World.h"
#include "df/unit.h"
#include "df/world.h"
#include <random>
using namespace DFHack;
using std::map;
using std::string;
using std::vector;
DFHACK_PLUGIN("pet-uncapper");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(world);
namespace DFHack {
// for configuration-related logging
DBG_DECLARE(petuncapper, control, DebugCategory::LINFO);
// for logging during the periodic scan
DBG_DECLARE(petuncapper, cycle, DebugCategory::LINFO);
}
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
static const string CONFIG_KEY = string(plugin_name) + "/config";
static PersistentDataItem config;
enum ConfigValues {
CONFIG_IS_ENABLED = 0,
CONFIG_FREQ = 1,
CONFIG_POP_CAP = 2,
CONFIG_PREG_TIME = 3,
};
bool impregnate(df::unit* female, df::unit* male) {
if (!female || !male)
return false;
if (female->pregnancy_genes)
return false;
df::unit_genes* preg = new df::unit_genes;
*preg = male->appearance.genes;
female->pregnancy_genes = preg;
female->pregnancy_timer = config.get_int(CONFIG_PREG_TIME);
female->pregnancy_caste = male->caste;
return true;
}
void impregnateMany(color_ostream &out, bool verbose = false) {
// mark that we have recently run
cycle_timestamp = world->frame_counter;
map<int32_t, vector<df::unit *>> males;
map<int32_t, vector<df::unit *>> females;
map<int32_t, int32_t> popcount;
std::random_device seed;
std::mt19937 gen{seed()};
const int popcap = config.get_int(CONFIG_POP_CAP);
int pregnancies = 0;
for (auto unit : world->units.active) {
// not restricted to fort pets since merchant/wild animals can participate
if (!Units::isActive(unit) || unit->flags1.bits.active_invader || unit->flags2.bits.underworld || unit->flags2.bits.visitor_uninvited || unit->flags2.bits.visitor)
continue;
popcount[unit->race]++;
if (unit->pregnancy_genes) {
// already pregnant -- if remaining time is less than the current setting, speed it up
if ((int)unit->pregnancy_timer > config.get_int(CONFIG_PREG_TIME))
unit->pregnancy_timer = config.get_int(CONFIG_PREG_TIME);
// for player convenience and population stability, count the fetus toward the population cap
popcount[unit->race]++;
continue;
}
if (unit->flags1.bits.caged)
continue;
// must have PET or PET_EXOTIC
if (!Units::isTamable(unit))
continue;
// check for adulthood
if (Units::isBaby(unit) || Units::isChild(unit))
continue;
if (Units::isMale(unit))
males[unit->race].push_back(unit);
else if (Units::isFemale(unit))
females[unit->race].push_back(unit);
}
for (auto [race, femalesList] : females) {
if (!males.contains(race))
continue;
for (auto female : femalesList) {
if (popcap > 0 && popcount[race] >= popcap)
break;
vector<df::unit *> compatibles;
for (auto male : males[race]) {
if (Maps::canWalkBetween(female->pos, male->pos) )
compatibles.push_back(male);
}
if (compatibles.empty())
continue;
std::uniform_int_distribution<> dist{0, (int)compatibles.size() - 1};
if (impregnate(female, compatibles[dist(gen)])) {
pregnancies++;
popcount[race]++;
}
}
}
if (pregnancies || verbose) {
INFO(cycle, out).print("%d pet pregnanc%s initiated\n",
pregnancies, pregnancies == 1 ? "y" : "ies");
}
}
command_result do_command(color_ostream &out, vector<string> & parameters) {
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
out.printerr("Cannot run %s without a loaded fort.\n", plugin_name);
return CR_FAILURE;
}
if (parameters.size() == 0 || parameters[0] == "status") {
out.print("%s is %s\n\n", plugin_name, is_enabled ? "enabled" : "not enabled");
out.print("population cap per species: %d\n", config.get_int(CONFIG_POP_CAP));
out.print("updating pregnancies every %d ticks\n", config.get_int(CONFIG_FREQ));
out.print("pregancies last %d ticks\n", config.get_int(CONFIG_PREG_TIME));
} else if (parameters[0] == "now") {
impregnateMany(out, true);
} else {
if (parameters.size() < 2)
return CR_WRONG_USAGE;
string command = parameters[0];
int val = std::max(0, string_to_int(parameters[1]));
if (command == "cap")
config.set_int(CONFIG_POP_CAP, val);
else if (command == "every")
config.set_int(CONFIG_FREQ, val);
else if (command == "pregtime")
config.set_int(CONFIG_PREG_TIME, val);
else
return CR_WRONG_USAGE;
}
return CR_OK;
}
DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginCommand> &commands) {
commands.push_back(PluginCommand(
"pet-uncapper",
"Modify the pet population cap.",
do_command));
return CR_OK;
}
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
out.printerr("Cannot enable %s without a loaded fort.\n", plugin_name);
return CR_FAILURE;
}
if (enable != is_enabled) {
is_enabled = enable;
DEBUG(control,out).print("%s from the API; persisting\n",
is_enabled ? "enabled" : "disabled");
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
if (enable)
impregnateMany(out);
} else {
DEBUG(control,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled",
is_enabled ? "enabled" : "disabled");
}
return CR_OK;
}
DFhackCExport command_result plugin_load_site_data (color_ostream &out) {
cycle_timestamp = 0;
config = World::GetPersistentSiteData(CONFIG_KEY);
if (!config.isValid()) {
DEBUG(control,out).print("no config found in this save; initializing\n");
config = World::AddPersistentSiteData(CONFIG_KEY);
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
config.set_int(CONFIG_FREQ, 10000);
config.set_int(CONFIG_POP_CAP, 100);
config.set_int(CONFIG_PREG_TIME, 200000);
}
is_enabled = config.get_bool(CONFIG_IS_ENABLED);
DEBUG(control,out).print("loading persisted enabled state: %s\n",
is_enabled ? "true" : "false");
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_WORLD_UNLOADED) {
if (is_enabled) {
DEBUG(control,out).print("world unloaded; disabling %s\n",
plugin_name);
is_enabled = false;
}
}
return CR_OK;
}
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (world->frame_counter - cycle_timestamp >= config.get_int(CONFIG_FREQ))
impregnateMany(out);
return CR_OK;
}