forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildingplan_cycle.cpp
More file actions
445 lines (381 loc) · 16.5 KB
/
Copy pathbuildingplan_cycle.cpp
File metadata and controls
445 lines (381 loc) · 16.5 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "plannedbuilding.h"
#include "buildingplan.h"
#include "Debug.h"
#include "modules/Burrows.h"
#include "modules/Items.h"
#include "modules/Job.h"
#include "modules/Maps.h"
#include "modules/Materials.h"
#include "modules/Units.h"
#include "df/building_actual.h"
#include "df/building_design.h"
#include "df/burrow.h"
#include "df/general_ref.h"
#include "df/item.h"
#include "df/item_slabst.h"
#include "df/job.h"
#include "df/world.h"
#include <unordered_map>
using std::map;
using std::string;
using std::unordered_map;
namespace DFHack {
DBG_EXTERN(buildingplan, cycle);
}
using namespace DFHack;
struct BadFlags {
uint32_t whole;
BadFlags() {
df::item_flags flags;
#define F(x) flags.bits.x = true;
F(dump); F(forbid); F(garbage_collect);
F(hostile); F(on_fire); F(rotten); F(trader);
F(in_building); F(construction); F(in_job);
F(owned); F(removed); F(encased); F(spider_web);
#undef F
whole = flags.whole;
}
};
// cache of walkability groups that have citizens in them
static std::unordered_set<uint16_t> accessible_walkability_groups;
void update_walkability_groups(){
// ensure that we update at most once per tick
auto frame_counter = df::global::world->frame_counter;
if (frame_counter == walkability_timestamp)
return;
else
walkability_timestamp = frame_counter;
accessible_walkability_groups.clear();
auto get_group = [](df::unit *unit){
auto group = Maps::getWalkableGroup(Units::getPosition(unit));
if (group != 0)
{
accessible_walkability_groups.insert(group);
}
};
Units::forCitizens(get_group);
}
// This is tricky. we want to choose an item that can be brought to the job site, but that's not
// necessarily the same as job->pos. it could be many tiles off in any direction (e.g. for bridges), or
// up or down (e.g. for stairs). For now, just return if the item is in the same walkability group
// as a citizen or resident
static bool isAccessible(color_ostream& out, df::item* item) {
df::coord item_pos = Items::getPosition(item);
uint16_t walkability_group = Maps::getWalkableGroup(item_pos);
bool is_walkable = accessible_walkability_groups.contains(walkability_group);
TRACE(cycle, out).print("item {} in walkability_group {} at ({},{},{}) is {}accessible from job site\n",
item->id, walkability_group, item_pos.x, item_pos.y, item_pos.z, is_walkable ? "(probably) " : "not ");
return is_walkable;
}
// as of v50, soap, coal, and ash are no longer valid building materials
static bool isUnusableBar(color_ostream& out, df::item* item) {
if (item->getType() != df::item_type::BAR)
return false;
MaterialInfo minfo(item);
string token = minfo.getToken();
if (token.starts_with("COAL:") || token == "ASH")
return true;
df::job_item_flags2 ok;
df::job_item_flags2 mask;
minfo.getMatchBits(ok, mask);
return ok.bits.soap;
}
static bool isInWheelbarrow(color_ostream& out, df::item* item) {
auto container = Items::getContainer(item);
if (!container || container->getType() != df::item_type::TOOL)
return false;
return container->hasToolUse(df::tool_uses::HEAVY_OBJECT_HAULING);
}
bool isInIgnoreBurrow(df::item *item){
auto ignore_burrow = getIgnoreBurrow();
return ignore_burrow && Burrows::isAssignedTile(ignore_burrow, Items::getPosition(item));
}
bool itemPassesScreen(color_ostream& out, df::item* item) {
static const BadFlags bad_flags;
return !(item->flags.whole & bad_flags.whole)
&& !item->isAssignedToStockpile()
&& isAccessible(out, item)
&& !isUnusableBar(out, item)
&& !isInWheelbarrow(out, item)
&& !isInIgnoreBurrow(item);
}
bool matchesHeatSafety(int16_t mat_type, int32_t mat_index, HeatSafety heat) {
if (heat == HEAT_SAFETY_ANY)
return true;
MaterialInfo minfo(mat_type, mat_index);
df::job_item_flags2 ok;
df::job_item_flags2 mask;
minfo.getMatchBits(ok, mask);
if (heat >= HEAT_SAFETY_MAGMA)
return ok.bits.magma_safe;
if (heat == HEAT_SAFETY_FIRE)
return ok.bits.fire_safe || ok.bits.magma_safe;
return false;
}
bool matchesFilters(df::item * item, const df::job_item * jitem, HeatSafety heat, const ItemFilter &item_filter, const std::set<string> &specials) {
// check the properties that are not checked by Job::isSuitableItem()
if (jitem->item_type > -1 && jitem->item_type != item->getType())
return false;
if (jitem->item_subtype > -1 &&
jitem->item_subtype != item->getSubtype())
return false;
if (jitem->flags2.bits.building_material && !item->isBuildMat())
return false;
if ((jitem->flags1.bits.empty || jitem->flags2.bits.lye_milk_free)) {
auto gref = Items::getGeneralRef(item, df::general_ref_type::CONTAINS_ITEM);
if (gref) {
if (jitem->flags1.bits.empty)
return false;
if (auto contained_item = gref->getItem(); contained_item) {
MaterialInfo mi;
mi.decode(contained_item);
if (mi.getToken() != "WATER")
return false;
}
}
}
if (jitem->metal_ore > -1 && !item->isMetalOre(jitem->metal_ore))
return false;
if (jitem->has_tool_use > df::tool_uses::NONE
&& !item->hasToolUse(jitem->has_tool_use))
return false;
auto itype = item->getType();
if (itype == df::item_type::SLAB && specials.count("engraved")
&& static_cast<df::item_slabst *>(item)->engraving_type != df::slab_engraving_type::Memorial)
return false;
if (itype == df::item_type::CAGE && specials.count("empty")
&& (Items::getGeneralRef(item, df::general_ref_type::CONTAINS_UNIT)
|| Items::getGeneralRef(item, df::general_ref_type::CONTAINS_ITEM)))
return false;
if (!matchesHeatSafety(item->getMaterial(), item->getMaterialIndex(), heat))
return false;
return Job::isSuitableItem(jitem, itype, item->getSubtype())
&& Job::isSuitableMaterial(jitem, item->getMaterial(), item->getMaterialIndex(), itype)
&& item_filter.matches(item);
}
bool isJobReady(color_ostream &out, const std::vector<df::job_item *> &jitems) {
int needed_items = 0;
for (auto job_item : jitems) { needed_items += job_item->quantity; }
if (needed_items) {
DEBUG(cycle,out).print("building needs {} more item(s)\n", needed_items);
return false;
}
return true;
}
static bool job_item_idx_lt(df::job_item_ref *a, df::job_item_ref *b) {
// we want the items in the opposite order of the filters
return a->job_item_idx > b->job_item_idx;
}
// this function does not remove the job_items since their quantity fields are
// now all at 0, so there is no risk of having extra items attached. we don't
// remove them to keep the "finalize with buildingplan active" path as similar
// as possible to the "finalize with buildingplan disabled" path.
void finalizeBuilding(color_ostream &out, df::building *bld, bool unsuspend_on_finalize) {
DEBUG(cycle,out).print("finalizing building {}\n", bld->id);
auto job = bld->jobs[0];
// sort the items so they get added to the structure in the correct order
std::sort(job->items.begin(), job->items.end(), job_item_idx_lt);
// derive the material properties of the building and job from the first
// applicable item. if any boulders are involved, it makes the whole
// structure "rough".
bool rough = false;
for (auto attached_item : job->items) {
df::item *item = attached_item->item;
rough = rough || item->getType() == df::item_type::BOULDER;
if (bld->mat_type == -1) {
bld->mat_type = item->getMaterial();
job->mat_type = bld->mat_type;
}
if (bld->mat_index == -1) {
bld->mat_index = item->getMaterialIndex();
job->mat_index = bld->mat_index;
}
}
if (bld->needsDesign()) {
auto act = (df::building_actual *)bld;
if (!act->design)
act->design = new df::building_design();
act->design->flags.bits.rough = rough;
}
// we're good to go!
if (unsuspend_on_finalize) {
job->flags.bits.suspend = false;
Job::checkBuildingsNow();
}
}
static df::building * popInvalidTasks(color_ostream &out, Bucket &task_queue,
unordered_map<int32_t, PlannedBuilding> &planned_buildings) {
while (!task_queue.empty()) {
auto & task = task_queue.front();
auto id = task.first;
if (planned_buildings.count(id) > 0) {
auto bld = planned_buildings.at(id).getBuildingIfValidOrRemoveIfNot(out);
if (bld && bld->jobs[0]->job_items.elements[task.second]->quantity)
return bld;
}
DEBUG(cycle,out).print("discarding invalid task: bld={}, job_item_idx={}\n", id, task.second);
task_queue.pop_front();
}
return NULL;
}
static int distance(df::coord pos1, df::coord pos2) {
return std::max(abs(pos1.x - pos2.x), abs(pos1.y - pos2.y)) + abs(pos1.z - pos2.z);
}
static void doVector(color_ostream &out, df::job_item_vector_id vector_id,
map<string, Bucket> &buckets,
unordered_map<int32_t, PlannedBuilding> &planned_buildings,
bool unsuspend_on_finalize) {
auto other_id = ENUM_ATTR(job_item_vector_id, other, vector_id);
const auto item_vector = df::global::world->items.other[other_id];
DEBUG(cycle,out).print("matching {} item(s) in vector {} against {} filter bucket(s)\n",
item_vector.size(),
ENUM_KEY_STR(job_item_vector_id, vector_id),
buckets.size());
// items we might want to attach (and their positions)
std::vector<df::item*> available;
for (auto item : item_vector) {
if (itemPassesScreen(out, item))
available.emplace_back(item);
}
std::vector<std::pair<df::coord, df::item*>> matching;
size_t num_matching = 0;
DEBUG(cycle,out).print("{} items available for assignment\n", available.size());
for (auto bucket_it = buckets.begin(); bucket_it != buckets.end(); ) {
TRACE(cycle,out).print("scanning bucket: {}/{}\n",
ENUM_KEY_STR(job_item_vector_id, vector_id), bucket_it->first);
auto & task_queue = bucket_it->second;
bool first_task = true;
while (auto bld = popInvalidTasks(out, task_queue, planned_buildings)){
auto & task = task_queue.front();
auto id = task.first;
auto job = bld->jobs[0];
auto &jitems = job->job_items.elements;
const size_t num_filters = jitems.size();
const int filter_idx = task.second;
const int rev_filter_idx = num_filters - (filter_idx+1);
auto &pb = planned_buildings.at(id);
// first task of the bucket: filter/count available items
if (first_task) {
matching.clear();
for(auto item : available)
if (!item->flags.bits.in_job &&
matchesFilters(item,
jitems[filter_idx],
pb.heat_safety,
pb.item_filters[rev_filter_idx],
pb.specials))
matching.emplace_back(Items::getPosition(item),item);
num_matching = matching.size();
first_task = false;
TRACE(cycle,out).print("first task in bucket: found {} matching items\n",
num_matching);
}
// every task: find and attach closest matching item (if any)
if (num_matching == 0)
break; // no more items for this bucket, go to next bucket.
auto jpos = job->pos;
std::pair<df::coord, df::item*> *closest = nullptr;
for (auto &p : matching) {
if (p.second && (closest == nullptr || distance(jpos, p.first) < distance(jpos, closest->first)))
closest = &p;
}
auto item = closest->second; // some item must be closest.
if (Job::attachJobItem(job, item, df::job_role_type::Hauled, filter_idx)) {
MaterialInfo material;
material.decode(item);
ItemTypeInfo item_type;
item_type.decode(item);
DEBUG(cycle,out).print("attached {} {} (distance {}) to filter {} for {}({}): {}/{}\n",
material.toString(),
item_type.toString(),
distance(closest->first, jpos),
filter_idx,
ENUM_KEY_STR(building_type, bld->getType()),
id,
ENUM_KEY_STR(job_item_vector_id, vector_id),
bucket_it->first);
// clean up fulfilled task
task_queue.pop_front();
// keep quantity aligned with the actual number of remaining
// items so if buildingplan is turned off, the building will
// be completed with the correct number of items.
--jitems[filter_idx]->quantity;
// null the item* component in vector of matching items
// ensures we don't try to attach this item to another job
closest->second = nullptr;
--num_matching;
// try to finalize building
if (isJobReady(out, jitems)) {
finalizeBuilding(out, bld, unsuspend_on_finalize);
planned_buildings.at(id).remove(out);
}
} else {
WARN(cycle,out).print("failed to attach job item");
}
} // no more items or no more tasks
if (task_queue.empty()) {
DEBUG(cycle,out).print(
"removing empty item bucket: {}/{}; {} left\n",
ENUM_KEY_STR(job_item_vector_id, vector_id),
bucket_it->first,
buckets.size() - 1);
bucket_it = buckets.erase(bucket_it);
} else {
++bucket_it;
}
}
}
struct VectorsToScanLast {
std::vector<df::job_item_vector_id> vectors;
VectorsToScanLast() {
// order is important here. we want to match boulders before wood and
// everything before bars. blocks are not listed here since we'll have
// already scanned them when we did the first pass through the buckets.
vectors.push_back(df::job_item_vector_id::BOULDER);
vectors.push_back(df::job_item_vector_id::WOOD);
vectors.push_back(df::job_item_vector_id::BAR);
vectors.push_back(df::job_item_vector_id::IN_PLAY);
}
};
void buildingplan_cycle(color_ostream &out, Tasks &tasks,
unordered_map<int32_t, PlannedBuilding> &planned_buildings, bool unsuspend_on_finalize) {
static const VectorsToScanLast vectors_to_scan_last;
DEBUG(cycle,out).print(
"running buildingplan cycle for %zu registered buildings\n",
planned_buildings.size());
update_walkability_groups();
for (auto it = tasks.begin(); it != tasks.end(); ) {
auto vector_id = it->first;
// we could make this a set, but it's only a few elements
if (std::find(vectors_to_scan_last.vectors.begin(),
vectors_to_scan_last.vectors.end(),
vector_id) != vectors_to_scan_last.vectors.end()) {
++it;
continue;
}
auto & buckets = it->second;
doVector(out, vector_id, buckets, planned_buildings, unsuspend_on_finalize);
if (buckets.empty()) {
DEBUG(cycle,out).print("removing empty vector: {}; {} vector(s) left\n",
ENUM_KEY_STR(job_item_vector_id, vector_id),
tasks.size() - 1);
it = tasks.erase(it);
}
else
++it;
}
for (auto vector_id : vectors_to_scan_last.vectors) {
if (tasks.count(vector_id) == 0)
continue;
auto & buckets = tasks[vector_id];
doVector(out, vector_id, buckets, planned_buildings, unsuspend_on_finalize);
if (buckets.empty()) {
DEBUG(cycle,out).print("removing empty vector: {}; {} vector(s) left\n",
ENUM_KEY_STR(job_item_vector_id, vector_id),
tasks.size() - 1);
tasks.erase(vector_id);
}
}
DEBUG(cycle,out).print("cycle done; {} registered building(s) left\n",
planned_buildings.size());
}