-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpopulation_justice.cpp
302 lines (287 loc) · 12.1 KB
/
population_justice.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
#include "ai.h"
#include "population.h"
#include "modules/Gui.h"
#include "modules/Units.h"
#include "df/building_civzonest.h"
#include "df/crime.h"
#include "df/general_ref_contains_itemst.h"
#include "df/general_ref_contains_unitst.h"
#include "df/item.h"
#include "df/items_other_id.h"
#include "df/punishment.h"
#include "df/ui.h"
#include "df/viewscreen_justicest.h"
#include "df/viewscreen_overallstatusst.h"
#include "df/world.h"
REQUIRE_GLOBAL(cur_year);
REQUIRE_GLOBAL(cur_year_tick);
REQUIRE_GLOBAL(ui);
REQUIRE_GLOBAL(world);
void Population::update_caged(color_ostream & out)
{
int32_t count = 0;
for (auto it = world->items.other[items_other_id::CAGE].begin(); it != world->items.other[items_other_id::CAGE].end(); it++)
{
df::item *cage = *it;
if (!cage->flags.bits.on_ground)
{
bool ignore = true;
if (cage->flags.bits.forbid && !cage->flags.bits.trader)
{
for (auto ref : cage->general_refs)
{
auto bld = ref->getBuilding();
if (ref->getType() == general_ref_type::BUILDING_HOLDER && bld && bld->getType() == building_type::TradeDepot)
{
ignore = false;
break;
}
}
}
if (ignore)
{
continue;
}
}
for (auto ref = cage->general_refs.begin(); ref != cage->general_refs.end(); ref++)
{
if (virtual_cast<df::general_ref_contains_itemst>(*ref))
{
df::item *i = (*ref)->getItem();
if (i->flags.bits.dump && !i->flags.bits.forbid)
{
continue;
}
count++;
i->flags.bits.dump = 1;
i->flags.bits.forbid = 0;
}
else if (virtual_cast<df::general_ref_contains_unitst>(*ref))
{
df::unit *u = (*ref)->getUnit();
if (Units::isOwnCiv(u))
{
room *r = ai.find_room(room_type::releasecage, [](room *r) -> bool { return r->status >= room_status::finished; });
if (r)
{
for (auto f : r->layout)
{
if (f->type == layout_type::lever && f->bld_id != -1)
{
ai.plan.add_task(task_type::rescue_caged, r, f, cage->id);
break;
}
}
}
}
else
{
size_t waiting_items = 0;
for (auto ii : u->inventory)
{
if (Items::getOwner(ii->item))
{
continue;
}
waiting_items++;
if (ii->item->flags.bits.dump && !ii->item->flags.bits.forbid)
{
continue;
}
count++;
ii->item->flags.bits.dump = 1;
ii->item->flags.bits.forbid = 0;
ai.debug(out, "pop: marked item " + AI::describe_item(ii->item) + " for dumping");
}
if (!waiting_items)
{
room *r = ai.find_room(room_type::pitcage, [](room *r) -> bool { return r->dfbuilding(); });
if (r && AI::spiral_search(r->pos(), 1, 1, [cage](df::coord t) -> bool { return t == cage->pos; }).isValid())
{
assign_unit_to_zone(u, virtual_cast<df::building_civzonest>(r->dfbuilding()));
ai.debug(out, "pop: marked " + AI::describe_unit(u) + " for pitting");
military_random_squad_attack_unit(out, u, "just in case pitting fails");
}
else
{
military_cancel_attack_order(out, u, "caged, but not in place for pitting");
}
}
else
{
ai.debug(out, stl_sprintf("pop: waiting for %s to be stripped for pitting (%zu items remain)", AI::describe_unit(u).c_str(), waiting_items));
military_cancel_attack_order(out, u, "caged, but not ready for pitting");
}
}
}
}
}
if (count > 0)
{
ai.debug(out, stl_sprintf("pop: dumped %d items from cages", count));
}
}
void Population::update_crimes(color_ostream & out)
{
// check for criminals, log justice updates
int32_t not_before_year = last_checked_crime_year;
int32_t not_before_tick = last_checked_crime_tick;
last_checked_crime_year = *cur_year;
last_checked_crime_tick = *cur_year_tick;
for (auto crime : world->crimes.all)
{
if (!crime->flags.bits.discovered || crime->site != ui->site_id)
{
continue;
}
std::string accusation;
switch (crime->mode)
{
case df::crime::ProductionOrderViolation:
accusation = "violation of a production order";
break;
case df::crime::ExportViolation:
accusation = "violation of an export ban";
break;
case df::crime::JobOrderViolation:
accusation = "violation of a job order";
break;
case df::crime::ConspiracyToSlowLabor:
accusation = "conspiracy to slow labor";
break;
case df::crime::Murder:
accusation = "murder";
break;
case df::crime::DisorderlyBehavior:
accusation = "disorderly conduct";
break;
case df::crime::BuildingDestruction:
accusation = "building destruction";
break;
case df::crime::Vandalism:
accusation = "vandalism";
break;
case df::crime::Theft:
accusation = "theft";
break;
case df::crime::Robbery:
accusation = "robbery";
break;
case df::crime::BloodDrinking:
accusation = "vampirism";
break;
case df::crime::Embezzlement:
accusation = "embezzlement";
break;
case df::crime::AttemptedMurder:
accusation = "attempted murder";
break;
case df::crime::Kidnapping:
accusation = "kidnapping";
break;
case df::crime::AttemptedKidnapping:
accusation = "attempted kidnapping";
break;
case df::crime::AttemptedTheft:
accusation = "attempted theft";
break;
case df::crime::Treason:
accusation = "treason";
break;
case df::crime::Espionage:
accusation = "espionage";
break;
case df::crime::Bribery:
accusation = "bribery";
break;
}
if (accusation.empty())
{
accusation = enum_item_key(crime->mode);
}
df::unit *criminal = df::unit::find(crime->criminal);
df::unit *convicted = df::unit::find(crime->convict_data.convicted);
df::unit *victim = df::unit::find(crime->victim_data.victim);
std::string with_victim;
if (victim)
{
with_victim = " with " + AI::describe_unit(victim) + " as the victim";
}
if (crime->discovered_year > not_before_year || (crime->discovered_year == not_before_year && crime->discovered_time >= not_before_tick))
{
ai.debug(out, "[Crime] New crime discovered: " + AI::describe_unit(criminal) + " is accused of " + accusation + with_victim + ".");
}
if (crime->flags.bits.needs_trial && criminal && !convicted && AI::is_dwarfmode_viewscreen())
{
// FIXME: this should be an ExclusiveCallback
ai.debug(out, "[Crime] Convicting " + AI::describe_unit(criminal) + " of " + accusation + with_victim + ".");
Gui::getCurViewscreen(true)->feed_key(interface_key::D_STATUS);
if (auto screen = virtual_cast<df::viewscreen_overallstatusst>(Gui::getCurViewscreen(true)))
{
auto page = std::find(screen->visible_pages.begin(), screen->visible_pages.end(), df::viewscreen_overallstatusst::Justice);
if (page == screen->visible_pages.end())
{
ai.debug(out, "[Crime] [ERROR] Could not find justice tab on status screen.");
}
else
{
while (screen->visible_pages.at(screen->page_cursor) != df::viewscreen_overallstatusst::Justice)
{
Gui::getCurViewscreen(true)->feed_key(interface_key::STANDARDSCROLL_RIGHT);
}
Gui::getCurViewscreen(true)->feed_key(interface_key::SELECT);
if (auto justice = virtual_cast<df::viewscreen_justicest>(Gui::getCurViewscreen(true)))
{
auto it = std::find(justice->cases.begin(), justice->cases.end(), crime);
if (it == justice->cases.end())
{
ai.debug(out, "[Crime] Could not find case. Checking " + std::string(justice->cold_cases ? "recent crimes" : "cold cases"));
Gui::getCurViewscreen(true)->feed_key(interface_key::CHANGETAB);
it = std::find(justice->cases.begin(), justice->cases.end(), crime);
}
if (it == justice->cases.end())
{
ai.debug(out, "[Crime] [ERROR] Could not find case.");
}
else
{
while (justice->cases.size() <= size_t(justice->sel_idx_current) || justice->cases.at(size_t(justice->sel_idx_current)) != crime)
{
Gui::getCurViewscreen(true)->feed_key(interface_key::STANDARDSCROLL_DOWN);
}
Gui::getCurViewscreen(true)->feed_key(interface_key::SELECT);
auto convict = std::find(justice->convict_choices.begin(), justice->convict_choices.end(), criminal);
if (convict == justice->convict_choices.end())
{
ai.debug(out, "[Crime] [ERROR] Criminal is not on list of suspects.");
Gui::getCurViewscreen(true)->feed_key(interface_key::LEAVESCREEN);
}
else
{
while (justice->convict_choices.at(justice->cursor_right) != criminal)
{
Gui::getCurViewscreen(true)->feed_key(interface_key::STANDARDSCROLL_DOWN);
}
Gui::getCurViewscreen(true)->feed_key(interface_key::SELECT);
}
}
}
else
{
ai.debug(out, "[Crime] [ERROR] Could not open justice tab on status screen.");
}
Gui::getCurViewscreen(true)->feed_key(interface_key::LEAVESCREEN);
}
}
else
{
ai.debug(out, "[Crime] [ERROR] Could not open status screen.");
}
Gui::getCurViewscreen(true)->feed_key(interface_key::LEAVESCREEN);
}
if (convicted && !crime->flags.bits.sentenced)
{
ai.debug(out, "[Crime] Waiting for sentencing for " + AI::describe_unit(convicted) + ", who was convicted of the crime of " + accusation + with_victim + ".");
}
}
}