-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathpreserve-tombs.cpp
256 lines (215 loc) · 8.79 KB
/
preserve-tombs.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
#include "Debug.h"
#include "PluginManager.h"
#include "modules/Units.h"
#include "modules/Buildings.h"
#include "modules/Persistence.h"
#include "modules/EventManager.h"
#include "modules/World.h"
#include "df/world.h"
#include "df/unit.h"
#include "df/building.h"
#include "df/building_civzonest.h"
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("preserve-tombs");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(world);
static const std::string CONFIG_KEY = std::string(plugin_name) + "/config";
static PersistentDataItem config;
static int32_t cycle_timestamp;
static constexpr int32_t CYCLE_TICKS = 107;
enum ConfigValues {
CONFIG_IS_ENABLED = 0,
};
static std::unordered_map<int32_t, int32_t> tomb_assignments;
namespace DFHack {
DBG_DECLARE(preservetombs, control, DebugCategory::LINFO);
DBG_DECLARE(preservetombs, cycle, DebugCategory::LINFO);
DBG_DECLARE(preservetombs, event, DebugCategory::LINFO);
}
static bool assign_to_tomb(df::unit * unit, int32_t building_id);
static void update_tomb_assignments(color_ostream& out);
void onUnitDeath(color_ostream& out, void* ptr);
static command_result do_command(color_ostream& out, std::vector<std::string>& params);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
commands.push_back(PluginCommand(
plugin_name,
"Preserve tomb assignments when assigned units die.",
do_command));
return CR_OK;
}
static command_result do_command(color_ostream& out, std::vector<std::string>& params) {
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
out.printerr("Cannot use %s without a loaded fort.\n", plugin_name);
return CR_FAILURE;
}
if (params.size() == 0 || params[0] == "status") {
out.print("%s is currently %s\n", plugin_name, is_enabled ? "enabled" : "disabled");
if (is_enabled) {
out.print("tracked tomb assignments:\n");
std::for_each(tomb_assignments.begin(), tomb_assignments.end(), [&out](const auto& p){
auto& [unit_id, building_id] = p;
auto* unit = df::unit::find(unit_id);
std::string name = unit ? DF2CONSOLE(Units::getReadableName(unit)) : "UNKNOWN UNIT" ;
out.print("%s (id %d) -> building %d\n", name.c_str(), unit_id, building_id);
});
}
return CR_OK;
}
if (params[0] == "now") {
if (!is_enabled) {
out.printerr("Cannot update %s when not enabled", plugin_name);
return CR_FAILURE;
}
update_tomb_assignments(out);
out.print("Updated tomb assignments\n");
return CR_OK;
}
return CR_WRONG_USAGE;
}
// event listener
EventManager::EventHandler assign_tomb_handler(plugin_self, onUnitDeath, 0);
static void do_enable(color_ostream &out) {
EventManager::registerListener(EventManager::EventType::UNIT_DEATH, assign_tomb_handler);
update_tomb_assignments(out);
}
static void do_disable() {
tomb_assignments.clear();
EventManager::unregisterAll(plugin_self);
}
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)
do_enable(out);
else
do_disable();
} 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_shutdown (color_ostream &out) {
DEBUG(control,out).print("shutting down %s\n", plugin_name);
// PluginManager handles unregistering our handler from EventManager,
// so we don't have to do that here
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);
}
is_enabled = config.get_bool(CONFIG_IS_ENABLED);
DEBUG(control,out).print("loading persisted enabled state: %s\n",
is_enabled ? "true" : "false");
if (is_enabled)
do_enable(out);
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_WORLD_UNLOADED && is_enabled) {
DEBUG(control,out).print("world unloaded; disabling %s\n",
plugin_name);
is_enabled = false;
do_disable();
}
return CR_OK;
}
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (world->frame_counter - cycle_timestamp >= CYCLE_TICKS)
update_tomb_assignments(out);
return CR_OK;
}
// On unit death - check if we assigned them a tomb
//
//
void onUnitDeath(color_ostream& out, void* ptr) {
// input is void* that contains the unit id
int32_t unit_id = reinterpret_cast<std::intptr_t>(ptr);
// check if unit was assigned a tomb in life
auto it = tomb_assignments.find(unit_id);
if (it == tomb_assignments.end()) return;
// assign that unit to their previously assigned tomb in life
auto unit = df::unit::find(unit_id);
int32_t building_id = it->second;
if (!assign_to_tomb(unit, building_id)) {
if (unit) {
WARN(event, out).print("%s died - but failed to assign them back to their tomb %d\n",
DF2CONSOLE(Units::getReadableName(unit)).c_str(), building_id);
} else {
WARN(event, out).print("Unit %d died - but failed to assign them back to their tomb %d\n", unit_id, building_id);
}
return;
}
// success, print status update and remove assignment from our memo-list
INFO(event, out).print("%s died - assigning them back to their tomb\n",
DF2CONSOLE(Units::getReadableName(unit)).c_str());
tomb_assignments.erase(it);
}
// Update tomb assignments
//
//
static void update_tomb_assignments(color_ostream &out) {
cycle_timestamp = world->frame_counter;
// check tomb civzones for assigned units
for (auto* tomb : world->buildings.other.ZONE_TOMB) {
if (!tomb || !tomb->flags.bits.exists) continue;
if (tomb->assigned_unit_id == -1) continue;
auto unit = Buildings::getOwner(tomb);
if (!unit || Units::isDead(unit)) continue; // we only care about living units
auto it = tomb_assignments.find(tomb->assigned_unit_id);
if (it == tomb_assignments.end()) {
tomb_assignments.emplace(tomb->assigned_unit_id, tomb->id);
DEBUG(cycle, out).print("%s new tomb assignment, unit %d to tomb %d\n",
plugin_name, tomb->assigned_unit_id, tomb->id);
} else if (it->second != tomb->id) {
DEBUG(cycle, out).print("%s tomb assignment to %d changed, (old: %d, new: %d)\n",
plugin_name, tomb->assigned_unit_id, it->second, tomb->id);
it->second = tomb->id;
}
}
// now check our civzones for unassignment / deleted zone
std::erase_if(tomb_assignments,[&](const auto& p){
auto &[unit_id, building_id] = p;
auto bld = df::building::find(building_id);
if (!bld) {
DEBUG(cycle, out).print("%s tomb missing: %d - removing\n", plugin_name, building_id);
return true;
}
auto tomb = virtual_cast<df::building_civzonest>(bld);
if (!tomb || !tomb->flags.bits.exists) {
DEBUG(cycle, out).print("%s tomb missing: %d - removing\n", plugin_name, building_id);
return true;
}
if (tomb->assigned_unit_id != unit_id) {
DEBUG(cycle, out).print("%s unit %d unassigned from tomb %d - removing\n", plugin_name, unit_id, building_id);
return true;
}
return false;
});
}
// ASSIGN UNIT TO TOMB
//
//
static bool assign_to_tomb(df::unit * unit, int32_t building_id) {
if (!unit || !Units::isDead(unit)) return false;
auto bld = df::building::find(building_id);
if (!bld) return false;
auto tomb = virtual_cast<df::building_civzonest>(bld);
if (!tomb || tomb->assigned_unit_id != -1) return false;
Buildings::setOwner(tomb, unit);
return true;
}