-
Notifications
You must be signed in to change notification settings - Fork 26
/
plan_task.cpp
737 lines (646 loc) · 20.9 KB
/
plan_task.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#include "ai.h"
#include "debug.h"
#include "plan.h"
#include "modules/Buildings.h"
#include "modules/Maps.h"
#include "df/abstract_building.h"
#include "df/building_cagest.h"
#include "df/building_trapst.h"
#include "df/item_cagest.h"
#include "df/tile_occupancy.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/viewscreen_locationsst.h"
REQUIRE_GLOBAL(cur_year);
REQUIRE_GLOBAL(cur_year_tick);
static bool want_reupdate = false;
void Plan::update(color_ostream &)
{
last_update_year = *cur_year;
last_update_tick = *cur_year_tick;
if (bg_idx_generic == tasks_generic.end())
{
bg_idx_generic = tasks_generic.begin();
nrdig.clear();
for (auto it = tasks_generic.begin(); it != tasks_generic.end(); it++)
{
task *t = *it;
if ((t->type != task_type::dig_room && t->type != task_type::dig_room_immediate) || (t->r->type == room_type::corridor && (t->r->corridor_type == corridor_type::veinshaft || t->r->corridor_type == corridor_type::outpost)))
continue;
df::coord size = t->r->size();
if (t->r->type != room_type::corridor || size.z > 1)
nrdig[t->r->queue]++;
if (t->r->type != room_type::corridor && size.x * size.y * size.z >= 10)
nrdig[t->r->queue]++;
}
want_reupdate = false;
events.onupdate_register_once("df-ai plan bg generic", [this](color_ostream & out) -> bool
{
if (!Core::getInstance().isMapLoaded())
{
return true;
}
if (bg_idx_generic == tasks_generic.end())
{
if (want_reupdate)
{
update(out);
}
return true;
}
std::ostringstream reason;
task & t = **bg_idx_generic;
auto any_immediate = [this]() -> bool
{
for (auto t : tasks_generic)
{
if (t->type == task_type::dig_room_immediate)
{
return true;
}
}
return false;
};
bool del = false;
switch (t.type)
{
case task_type::want_dig:
{
size_t wantdig_max = ai.stocks.count_total.count(stock_item::pick) ? std::max(ai.stocks.count_total.at(stock_item::pick), 2) : 2;
if (any_immediate())
{
reason << "waiting for more important room to be dug";
}
else if (t.r->is_dug() || nrdig[t.r->queue] < wantdig_max)
{
digroom(out, t.r);
del = true;
}
else
{
reason << "dig queue " << t.r->queue << " has " << nrdig[t.r->queue] << " of " << wantdig_max << " slots already filled";
}
break;
}
case task_type::dig_room:
case task_type::dig_room_immediate:
fixup_open(out, t.r);
if (t.r->is_dug(reason))
{
t.r->status = room_status::dug;
construct_room(out, t.r);
want_reupdate = true; // wantdig asap
del = true;
}
else
{
t.r->dig();
}
break;
case task_type::construct_tradedepot:
del = try_construct_tradedepot(out, t.r, reason);
break;
case task_type::construct_workshop:
del = try_construct_workshop(out, t.r, reason);
break;
case task_type::construct_farmplot:
del = try_construct_farmplot(out, t.r, reason);
break;
case task_type::construct_furnace:
del = try_construct_furnace(out, t.r, reason);
break;
case task_type::construct_stockpile:
del = try_construct_stockpile(out, t.r, reason);
break;
case task_type::construct_activityzone:
del = try_construct_activityzone(out, t.r, reason);
break;
case task_type::construct_windmill:
del = try_construct_windmill(out, t.r, reason);
break;
case task_type::monitor_farm_irrigation:
del = monitor_farm_irrigation(out, t.r, reason);
break;
case task_type::setup_farmplot:
del = try_setup_farmplot(out, t.r, reason);
break;
case task_type::furnish:
break;
case task_type::check_furnish:
break;
case task_type::check_construct:
del = try_endconstruct(out, t.r, reason);
break;
case task_type::dig_cistern:
del = try_digcistern(out, t.r);
break;
case task_type::dig_garbage:
del = true;
break;
case task_type::check_idle:
del = checkidle(out, reason);
break;
case task_type::check_rooms:
checkrooms(out);
break;
case task_type::monitor_cistern:
monitor_cistern(out, reason);
break;
case task_type::monitor_room_value:
del = monitor_room_value(out, t.r, reason);
break;
case task_type::rescue_caged:
del = rescue_caged(out, t.r, t.f, t.item_id, reason);
break;
case task_type::_task_type_count:
break;
}
if (del)
{
delete *bg_idx_generic;
tasks_generic.erase(bg_idx_generic++);
}
else
{
t.last_status = reason.str();
bg_idx_generic++;
}
return false;
});
}
if (bg_idx_furniture == tasks_furniture.end())
{
bg_idx_furniture = tasks_furniture.begin();
cache_nofurnish.clear();
events.onupdate_register_once("df-ai plan bg furniture", [this](color_ostream & out) -> bool
{
if (!Core::getInstance().isMapLoaded())
{
return true;
}
if (bg_idx_furniture == tasks_furniture.end())
{
return true;
}
std::ostringstream reason;
task & t = **bg_idx_furniture;
bool del = false;
switch (t.type)
{
case task_type::furnish:
del = try_furnish(out, t.r, t.f, reason);
break;
case task_type::check_furnish:
del = try_endfurnish(out, t.r, t.f, reason);
break;
default:
break;
}
if (del)
{
delete *bg_idx_furniture;
tasks_furniture.erase(bg_idx_furniture++);
}
else
{
t.last_status = reason.str();
bg_idx_furniture++;
}
return false;
});
}
}
task *Plan::is_digging()
{
for (auto t : tasks_generic)
{
if ((t->type == task_type::want_dig || t->type == task_type::dig_room || t->type == task_type::dig_room_immediate) && t->r->type != room_type::corridor)
{
return t;
}
}
return nullptr;
}
bool Plan::is_idle()
{
if (!tasks_furniture.empty())
{
return false;
}
for (auto it = tasks_generic.begin(); it != tasks_generic.end(); it++)
{
task *t = *it;
if (t->type != task_type::monitor_cistern && t->type != task_type::check_rooms && t->type != task_type::check_idle)
return false;
}
return true;
}
bool Plan::checkidle(color_ostream & out, std::ostream & reason)
{
for (auto t : tasks_generic)
{
if ((t->type == task_type::want_dig || t->type == task_type::dig_room_immediate) && t->r->type != room_type::corridor && t->r->queue == 0)
{
reason << "already have queued room: " << AI::describe_room(t->r);
return false;
}
}
if (!priorities.empty())
{
for (auto & priority : priorities)
{
if (priority.working)
{
bool any_active_task = false;
for (auto t : tasks_generic)
{
if (priority.match_task(t))
{
any_active_task = true;
break;
}
}
if (!any_active_task)
{
for (auto t : tasks_furniture)
{
if (priority.match_task(t))
{
any_active_task = true;
break;
}
}
}
if (!any_active_task)
{
priority.working = false;
}
}
if (priority.act(ai, out, reason))
{
break;
}
}
return false;
}
reason << "no priority list!";
return false;
}
void Plan::checkrooms(color_ostream & out)
{
size_t ncheck = 8;
for (size_t i = ncheck * 4; i > 0; i--)
{
if (checkroom_idx < rooms_and_corridors.size())
{
room* r = rooms_and_corridors.at(checkroom_idx);
if (r->status == room_status::plan && r->build_when_accessible)
{
bool all_ready = true;
for (auto ap : r->accesspath)
{
if (ap->status < room_status::dug)
{
all_ready = false;
}
}
if (all_ready)
{
wantdig(out, r, 2);
}
}
if (r->status != room_status::plan)
{
checkroom(out, r);
ncheck--;
}
}
checkroom_idx++;
if (ncheck <= 0)
break;
}
if (checkroom_idx >= rooms_and_corridors.size())
checkroom_idx = 0;
}
// ensure room was not tantrumed etc
void Plan::checkroom(color_ostream & out, room *r)
{
if (r->status == room_status::plan)
{
return;
}
// fix missing walls/staircases
fixup_open(out, r);
// designation cancelled: damp stone, cave-in, or tree
r->dig();
if (r->required_value > 0 && r->compute_value() < r->required_value)
{
add_task(task_type::monitor_room_value, r);
}
if (r->status == room_status::dug || r->status == room_status::finished)
{
// tantrumed furniture
for (auto it = r->layout.begin(); it != r->layout.end(); it++)
{
furniture *f = *it;
if (f->ignore)
continue;
df::coord t = r->min + f->pos;
if (f->bld_id != -1 && !df::building::find(f->bld_id))
{
std::ostringstream str;
str << "fix furniture " << f->type << " in " << AI::describe_room(r);
ai.debug(out, str.str());
f->bld_id = -1;
add_task(task_type::furnish, r, f);
}
if (f->construction != construction_type::NONE)
{
std::ostringstream discard;
try_furnish_construction(out, f->construction, t, discard);
}
}
// tantrumed building
if (r->bld_id != -1 && !r->dfbuilding())
{
ai.debug(out, "rebuild " + AI::describe_room(r));
r->bld_id = -1;
construct_room(out, r);
}
}
}
// queue a room for digging when other dig jobs are finished
bool Plan::wantdig(color_ostream & out, room *r, int32_t queue)
{
if (r->queue_dig || r->status != room_status::plan)
{
return false;
}
if (queue != 0)
{
r->queue = queue;
}
ai.debug(out, "wantdig " + AI::describe_room(r));
r->queue_dig = true;
r->dig(true);
add_task(task_type::want_dig, r);
return true;
}
bool Plan::digroom(color_ostream & out, room *r, bool immediate)
{
if (r->status != room_status::plan)
return false;
ai.debug(out, "digroom " + AI::describe_room(r));
r->queue_dig = false;
r->status = room_status::dig;
fixup_open(out, r);
r->dig();
add_task(immediate ? task_type::dig_room_immediate : task_type::dig_room, r);
for (auto it = r->accesspath.begin(); it != r->accesspath.end(); it++)
{
digroom(out, *it, immediate);
}
for (auto f : r->layout)
{
if (f->type == layout_type::floodgate)
{
continue;
}
add_task(task_type::furnish, r, f);
}
ai.find_room(room_type::stockpile, [this, &out, r, immediate](room *r_) -> bool
{
if (r_->workshop != r)
{
return false;
}
// dig associated stockpiles
digroom(out, r_, immediate);
// search all stockpiles in case there's more than one
return false;
});
df::coord size = r->size();
if (r->type != room_type::corridor || size.z > 1)
{
nrdig[r->queue]++;
if (size.x * size.y * size.z >= 10)
nrdig[r->queue]++;
}
return true;
}
class FinalizeLocationExclusive : public ExclusiveCallback
{
AI & ai;
room *r;
public:
FinalizeLocationExclusive(AI & ai, room *r) :
ExclusiveCallback("finalize location " + AI::describe_room(r)),
ai(ai),
r(r)
{}
virtual void Run(color_ostream & out)
{
if (r->location_type == location_type::temple && r->data2 != -1)
{
FinalizeTemple(out);
}
}
void FinalizeTemple(color_ostream & out)
{
ExpectScreen<df::viewscreen_dwarfmodest>("dwarfmode/Default");
Key(interface_key::D_LOCATIONS);
ExpectedScreen<df::viewscreen_locationsst> view(this);
ExpectScreen<df::viewscreen_locationsst>("locations/Locations");
auto bld = r->dfbuilding();
bool found = false;
for (auto loc : view->locations)
{
if (loc && loc->id == bld->location_id)
{
found = true;
break;
}
}
if (!found)
{
ai.debug(out, "[ERROR] could not find " + AI::describe_room(r) + " on the list");
Key(interface_key::LEAVESCREEN);
ExpectScreen<df::viewscreen_dwarfmodest>("dwarfmode/Default");
return;
}
while (!view->locations.at(view->location_idx) || view->locations.at(view->location_idx)->id != bld->location_id)
{
Key(interface_key::STANDARDSCROLL_DOWN);
}
Key(interface_key::STANDARDSCROLL_RIGHT);
ExpectScreen<df::viewscreen_locationsst>("locations/Occupations");
Key(interface_key::LOCATION_RECOGNIZE_PRIESTHOOD);
Key(interface_key::SELECT);
ExpectScreen<df::viewscreen_locationsst>("locations/AssignOccupation");
Key(interface_key::STANDARDSCROLL_DOWN);
Key(interface_key::SELECT);
ExpectScreen<df::viewscreen_locationsst>("locations/Occupations");
Key(interface_key::LEAVESCREEN);
ExpectScreen<df::viewscreen_dwarfmodest>("dwarfmode/Default");
}
};
bool Plan::monitor_room_value(color_ostream & out, room *r, std::ostream & reason)
{
if (r->required_value <= 0)
{
return true;
}
int32_t value = r->compute_value();
if (value < 0)
{
reason << "cannot compute room value";
for (auto f : r->layout)
{
if (f->makeroom)
{
reason << " (waiting for " << ai.describe_furniture(f) << " to be built)";
break;
}
}
return false;
}
if (value >= r->required_value)
{
if (r->type == room_type::location)
{
events.queue_exclusive(std::make_unique<FinalizeLocationExclusive>(ai, r));
}
return true;
}
reason << "room value is " << (r->required_value - value) << " too low";
if (!smooth_room(out, r, false))
{
reason << " (smoothing)";
}
else if (!smooth_room(out, r, true))
{
reason << " (engraving)";
}
else
{
// TODO: build statues, etc.
reason << " (TODO)";
}
return false;
}
bool Plan::rescue_caged(color_ostream & out, room *r, furniture *f, int32_t item_id, std::ostream & reason)
{
auto cage = virtual_cast<df::item_cagest>(df::item::find(item_id));
if (!cage)
{
// cage no longer exists.
return true;
}
df::unit *prisoner = nullptr;
for (auto ref : cage->general_refs)
{
if (ref->getType() == general_ref_type::CONTAINS_UNIT)
{
prisoner = ref->getUnit();
break;
}
}
df::building_cagest *cage_bld = nullptr;
for (auto ref : cage->general_refs)
{
if (ref->getType() == general_ref_type::BUILDING_HOLDER)
{
cage_bld = virtual_cast<df::building_cagest>(ref->getBuilding());
break;
}
}
if (prisoner == nullptr)
{
// there's no longer someone in the cage.
// if we secured the cage to the floor, free it up.
if (cage_bld)
{
Buildings::deconstruct(cage_bld);
}
return true;
}
auto lever = virtual_cast<df::building_trapst>(df::building::find(f->bld_id));
if (!lever || lever->getBuildStage() != lever->getMaxBuildStage())
{
reason << "cannot free " << AI::describe_unit(prisoner) << ": missing lever";
return false;
}
DFAI_ASSERT(lever->trap_type == trap_type::Lever, "lever is trap_type::" + enum_item_key(lever->trap_type));
reason << "freeing " << AI::describe_unit(prisoner) << ": ";
// step 1: someone's trapped in a cage. check for a free tile in the "release zone"
// and create a job to secure the cage to that spot.
if (!cage_bld)
{
for (auto ref : cage->specific_refs)
{
if (ref->type == specific_ref_type::JOB)
{
reason << "waiting for cage to be secured to floor";
return false;
}
}
df::coord free_tile;
for (int16_t x = r->min.x; x <= r->max.x; x++)
{
for (int16_t y = r->min.y; y <= r->max.y; y++)
{
df::coord c(x, y, r->min.z);
auto tt = *Maps::getTileType(c);
if (ENUM_ATTR(tiletype_shape, basic_shape, ENUM_ATTR(tiletype, shape, tt)) != tiletype_shape_basic::Floor)
{
continue;
}
if (Maps::getTileOccupancy(c)->bits.building != tile_building_occ::None)
{
continue;
}
free_tile = c;
break;
}
}
if (free_tile.isValid())
{
auto bld = Buildings::allocInstance(free_tile, building_type::Cage);
Buildings::setSize(bld, df::coord(1, 1, 1));
std::vector<df::item*> cage_item;
cage_item.push_back(cage);
Buildings::constructWithItems(bld, cage_item);
reason << "waiting for cage to be secured to floor";
return false;
}
reason << "waiting for space in release zone";
return false;
}
// step 2: the cage has been secured in the "release zone". make sure we have two
// mechanisms and tell the dwarves to link the lever to the cage.
furniture cage_f;
cage_f.bld_id = cage_bld->id;
if (!link_lever(out, f, &cage_f, reason))
{
return false;
}
// step 3: the cage is linked to the lever. give it a pull. if all goes well,
// we'll end up freeing the task on the next iteration as the cage will be empty
// and left on the floor as an item.
pull_lever(out, f, reason);
// we could deconstruct the lever periodically to reclaim mechanisms, but the
// potential humor of having a lever with hundreds of mechanisms outweighs the
// cost of a few extra boulders being needed in most forts.
return false;
}
void Plan::add_task(task_type::type type, room *r, furniture *f, int32_t item_id)
{
auto & tasks = (type == task_type::furnish || type == task_type::check_furnish) ? tasks_furniture : tasks_generic;
for (auto t : tasks)
{
if (t->type == type && t->r == r && t->f == f && t->item_id == item_id)
{
return;
}
}
tasks.push_back(new task(type, r, f, item_id));
}