-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathstocks_queue.cpp
1197 lines (1144 loc) · 33.7 KB
/
stocks_queue.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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ai.h"
#include "stocks.h"
#include "plan.h"
#include "Error.h"
#include "modules/Units.h"
#include "df/building_furnacest.h"
#include "df/building_workshopst.h"
#include "df/buildings_other_id.h"
#include "df/caste_raw.h"
#include "df/creature_raw.h"
#include "df/itemdef_trapcompst.h"
#include "df/manager_order.h"
#include "df/manager_order_template.h"
#include "df/unit_relationship_type.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/world.h"
REQUIRE_GLOBAL(cur_year);
REQUIRE_GLOBAL(world);
class MasonChairJobExclusive : public ExclusiveCallback
{
AI & ai;
int32_t wanted_amount;
public:
MasonChairJobExclusive(AI& ai, int32_t amount) :
ExclusiveCallback("assign chair construction at mason's workshop", 2),
ai(ai),
wanted_amount(amount)
{
}
protected:
void Run(color_ostream & out)
{
int32_t start_x, start_y, start_z;
Gui::getViewCoords(start_x, start_y, start_z);
if (auto workshop = ai.find_room(room_type::workshop, [](room* r) -> bool
{
if (r->workshop_type != workshop_type::Masons)
{
return false;
}
auto bld = r->dfbuilding();
return bld && bld->getBuildStage() == bld->getMaxBuildStage();
}))
{
auto bld = virtual_cast<df::building_workshopst>(workshop->dfbuilding());
int32_t wanted = min(max(wanted_amount - int(bld->jobs.size()), 0), 10 - int(bld->jobs.size()));
if (wanted > 0)
{
Key(interface_key::D_BUILDJOB);
Gui::setCursorCoords(workshop->min.x, workshop->min.y, workshop->min.z);
Key(interface_key::CURSOR_DOWNRIGHT);
ai.debug(out, stl_sprintf("queueing %d chairs directly at ", wanted) + ai.describe_room(workshop));
while (wanted > 0)
{
Key(interface_key::BUILDJOB_ADD);
Key(interface_key::HOTKEY_MASON_CHAIR);
wanted--;
}
Key(interface_key::LEAVESCREEN);
}
}
else
{
ai.debug(out, "could not find mason's workshop");
}
ai.ignore_pause(start_x, start_y, start_z);
}
};
// make it so the stocks of 'what' rises by 'amount'
void Stocks::queue_need(color_ostream & out, stock_item::item what, int32_t amount, std::ostream & reason)
{
if (amount <= 0)
{
reason << "have enough " << what;
return;
}
df::manager_order_template tmpl;
tmpl.mat_index = -1;
std::vector<stock_item::item> input;
switch (what)
{
case stock_item::ammo_combat:
{
queue_need_ammo(out, reason);
return;
}
case stock_item::ammo_training:
{
amount = (amount + 24) / 25;
tmpl.job_type = job_type::MakeAmmo;
tmpl.item_subtype = min_subtype_for_item(stock_item::ammo_training);
tmpl.material_category.bits.wood = true;
break;
}
case stock_item::anvil:
{
queue_need_anvil(out, reason);
return;
}
case stock_item::armor_feet:
case stock_item::armor_hands:
case stock_item::armor_head:
case stock_item::armor_legs:
case stock_item::armor_shield:
case stock_item::armor_torso:
{
queue_need_armor(out, what, reason);
return;
}
case stock_item::armor_stand:
{
tmpl.job_type = job_type::ConstructArmorStand;
tmpl.mat_type = 0;
break;
}
case stock_item::ash:
{
tmpl.job_type = job_type::MakeAsh;
input.push_back(stock_item::wood);
break;
}
case stock_item::axe:
{
queue_need_weapon(out, what, num_needed(what), reason, job_skill::AXE);
return;
}
case stock_item::backpack:
{
tmpl.job_type = job_type::MakeBackpack;
tmpl.material_category.bits.leather = 1;
break;
}
case stock_item::bag:
{
tmpl.job_type = job_type::ConstructChest;
tmpl.material_category.bits.cloth = 1;
break;
}
case stock_item::barrel:
{
tmpl.job_type = job_type::MakeBarrel;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::bed:
{
tmpl.job_type = job_type::ConstructBed;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::bin:
{
tmpl.job_type = job_type::ConstructBin;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::block:
{
tmpl.job_type = job_type::ConstructBlocks;
amount = (amount + 3) / 4;
// no stone => make wooden blocks (needed for pumps for aquifer handling)
if (!count_free.count(stock_item::stone) || !count_free.at(stock_item::stone))
{
if (amount > 2)
{
amount = 2;
}
tmpl.material_category.bits.wood = 1;
}
else
{
tmpl.mat_type = 0;
}
break;
}
case stock_item::bone:
{
for (auto u : world->units.active)
{
if (u->flags2.bits.slaughter)
{
reason << "marked for slaughter: " << AI::describe_unit(u);
return;
}
}
df::unit *biggest_pet = nullptr;
int32_t biggest_pet_size = 0;
bool biggest_pet_useful = true;
for (auto & pet : ai.pop.pet)
{
if (auto u = df::unit::find(pet.first))
{
if (u->relationship_ids[unit_relationship_type::Pet] != -1)
{
continue;
}
int32_t pet_size = u->body.size_info.size_cur;
if (pet.second.bits.hunts_vermin || pet.second.bits.trainable || pet.second.bits.lays_eggs || pet.second.bits.milkable || pet.second.bits.shearable)
{
if (!biggest_pet_useful)
{
continue;
}
if (biggest_pet_size < pet_size)
{
biggest_pet = u;
biggest_pet_size = pet_size;
}
}
else
{
if (biggest_pet_size < pet_size || biggest_pet_useful)
{
biggest_pet = u;
biggest_pet_size = pet_size;
biggest_pet_useful = false;
}
}
}
}
if (biggest_pet)
{
biggest_pet->flags2.bits.slaughter = true;
reason << "marked for slaughter: " << AI::describe_unit(biggest_pet);
int32_t age = ai.pop.days_since(biggest_pet->birth_year, biggest_pet->birth_time);
auto race = df::creature_raw::find(biggest_pet->race);
auto cst = race->caste.at(biggest_pet->caste);
ai.debug(out, stl_sprintf("marked %dy%dm%dd old %s:%s for slaughter (need bones)", age / 12 / 28, (age / 28) % 12, age % 28, race->creature_id.c_str(), cst->caste_id.c_str()));
}
return;
}
case stock_item::book_binding:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::book_binding);
tmpl.mat_type = 0;
break;
}
case stock_item::bookcase:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::bookcase);
tmpl.mat_type = 0;
break;
}
case stock_item::bucket:
{
tmpl.job_type = job_type::MakeBucket;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::cabinet:
{
tmpl.job_type = job_type::ConstructCabinet;
tmpl.mat_type = 0;
break;
}
case stock_item::cage:
{
tmpl.job_type = job_type::MakeCage;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::cage_metal:
{
queue_need_cage(out, reason);
return;
}
case stock_item::chair:
{
tmpl.job_type = job_type::ConstructThrone;
tmpl.mat_type = 0;
if (ai.find_room(room_type::nobleroom, [](room* r) -> bool
{
if (r->nobleroom_type != nobleroom_type::office || r->dfbuilding())
{
return false;
}
if (auto owner = df::unit::find(r->owner))
{
std::vector<Units::NoblePosition> positions;
if (Units::getNoblePositions(&positions, owner))
{
for (auto pos : positions)
{
if (pos.position->responsibilities[entity_position_responsibility::MANAGE_PRODUCTION])
{
return true;
}
}
}
}
return false;
}))
{
// the manager doesn't have an office, which requires a chair.
// assign the job directly at the workshop.
if (ai.find_room(room_type::workshop, [](room* r) -> bool
{
if (r->workshop_type != workshop_type::Masons)
{
return false;
}
auto bld = r->dfbuilding();
return bld && bld->getBuildStage() == bld->getMaxBuildStage();
}))
{
reason << "assigning job directly at mason's workshop as the manager has no office";
events.queue_exclusive(std::make_unique<MasonChairJobExclusive>(ai, amount));
return;
}
// we're screwed. for now, at least.
reason << "manager does not have an office and no mason workshop available";
return;
}
break;
}
case stock_item::chest:
{
tmpl.job_type = job_type::ConstructChest;
tmpl.mat_type = 0;
break;
}
case stock_item::clothes_feet:
case stock_item::clothes_hands:
case stock_item::clothes_head:
case stock_item::clothes_legs:
case stock_item::clothes_torso:
{
queue_need_clothes(out, what, reason);
return;
}
case stock_item::coal:
{
tmpl.job_type = job_type::MakeCharcoal;
// dont use wood -> charcoal if we have bituminous coal
// (except for bootstraping)
if (amount > 2 - count_free.at(stock_item::coal) && count_free.at(stock_item::raw_coke) > Watch.WatchStock.at(stock_item::raw_coke))
{
amount = 2 - count_free.at(stock_item::coal);
if (amount <= 0)
{
reason << "using raw coke instead of making charcoal";
}
}
break;
}
case stock_item::coffin:
{
tmpl.job_type = job_type::ConstructCoffin;
tmpl.mat_type = 0;
break;
}
case stock_item::crafts:
{
tmpl.job_type = job_type::MakeCrafts;
if (count_free.at(stock_item::stone) > count_free.at(stock_item::wood))
{
tmpl.mat_type = 0;
}
else
{
tmpl.material_category.bits.wood = 1;
}
break;
}
case stock_item::crutch:
{
tmpl.job_type = job_type::ConstructCrutch;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::die:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::die);
tmpl.mat_type = 0;
break;
}
case stock_item::door:
{
tmpl.job_type = job_type::ConstructDoor;
tmpl.mat_type = 0;
break;
}
case stock_item::drink:
{
std::map<stock_item::item, std::string> reactions;
reactions[stock_item::drink_plant] = "BREW_DRINK_FROM_PLANT";
reactions[stock_item::drink_fruit] = "BREW_DRINK_FROM_PLANT_GROWTH";
reactions[stock_item::honey] = "MAKE_MEAD";
auto score = [this, &out](std::pair<const stock_item::item, std::string> i) -> int32_t
{
int32_t c = count_free.at(i.first);
df::manager_order_template count_tmpl;
count_tmpl.job_type = job_type::CustomReaction;
count_tmpl.reaction_name = i.second;
c -= count_manager_orders(out, count_tmpl);
return c;
};
auto max = std::max_element(reactions.begin(), reactions.end(), [score](std::pair<const stock_item::item, std::string> a, std::pair<const stock_item::item, std::string> b) -> bool { return score(a) < score(b); });
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = max->second;
input.push_back(max->first);
if (count_free[stock_item::barrel] > count_free[stock_item::food_storage])
{
input.push_back(stock_item::barrel);
}
else
{
input.push_back(stock_item::food_storage);
}
amount = (amount + 4) / 5; // accounts for brewer yield, but not for input stack size
break;
}
case stock_item::dye:
{
tmpl.job_type = job_type::MillPlants;
input.push_back(stock_item::dye_plant);
input.push_back(stock_item::bag);
break;
}
case stock_item::dye_seeds:
{
tmpl.job_type = job_type::MillPlants;
input.push_back(stock_item::dye_plant);
input.push_back(stock_item::bag);
break;
}
case stock_item::flask:
{
tmpl.job_type = job_type::MakeFlask;
tmpl.material_category.bits.leather = 1;
break;
}
case stock_item::floodgate:
{
tmpl.job_type = job_type::ConstructFloodgate;
tmpl.mat_type = 0;
break;
}
case stock_item::food_storage:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::food_storage);
tmpl.mat_type = 0;
break;
}
case stock_item::goblet:
{
tmpl.job_type = job_type::MakeGoblet;
tmpl.mat_type = 0;
break;
}
case stock_item::gypsum:
{
if (ai.plan.should_search_for_metal)
{
for (auto & vein : ai.plan.map_veins)
{
if (is_gypsum(vein.first))
{
if (ai.plan.dig_vein(out, vein.first, amount))
{
reason << "marked " << MaterialInfo(0, vein.first).toString() << " vein for excavation";
return;
}
}
}
}
reason << "could not find gypsum vein";
return;
}
case stock_item::hatch_cover:
{
tmpl.job_type = job_type::ConstructHatchCover;
tmpl.mat_type = 0;
break;
}
case stock_item::honey:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "PRESS_HONEYCOMB";
input.push_back(stock_item::honeycomb);
input.push_back(stock_item::jug);
break;
}
case stock_item::hive:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::hive);
tmpl.mat_type = 0;
break;
}
case stock_item::jug:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::jug);
tmpl.mat_type = 0;
break;
}
case stock_item::lye:
{
tmpl.job_type = job_type::MakeLye;
input.push_back(stock_item::ash);
input.push_back(stock_item::bucket);
break;
}
case stock_item::meal:
{
// XXX fish/hunt/cook ?
if (last_warn_food_year != *cur_year)
{
ai.debug(out, stl_sprintf("need %d more food", amount));
last_warn_food_year = *cur_year;
}
reason << "waiting for ingredients and cooks";
return;
}
case stock_item::mechanism:
{
tmpl.job_type = job_type::ConstructMechanisms;
tmpl.mat_type = 0;
break;
}
case stock_item::minecart:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::minecart);
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::nest_box:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::nest_box);
tmpl.mat_type = 0;
break;
}
case stock_item::offering_place:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::offering_place);
tmpl.mat_type = 0;
break;
}
case stock_item::paper:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "PRESS_PLANT_PAPER";
input.push_back(stock_item::slurry);
break;
}
case stock_item::pedestal:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::pedestal);
tmpl.mat_type = 0;
break;
}
case stock_item::pick:
{
queue_need_weapon(out, what, num_needed(what), reason, job_skill::MINING, false, false, true);
return;
}
case stock_item::pipe_section:
{
tmpl.job_type = job_type::MakePipeSection;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::plaster_powder:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "MAKE_PLASTER_POWDER";
input.push_back(stock_item::gypsum);
input.push_back(stock_item::bag);
break;
}
case stock_item::potash:
{
tmpl.job_type = job_type::MakePotashFromAsh;
input.push_back(stock_item::ash);
break;
}
case stock_item::quern:
{
tmpl.job_type = job_type::ConstructQuern;
tmpl.mat_type = 0;
break;
}
case stock_item::quire:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "MAKE_QUIRE";
input.push_back(stock_item::paper);
break;
}
case stock_item::quiver:
{
tmpl.job_type = job_type::MakeQuiver;
tmpl.material_category.bits.leather = 1;
break;
}
case stock_item::raw_coke:
{
if (ai.plan.should_search_for_metal)
{
for (auto vein : ai.plan.map_veins)
{
if (!is_raw_coke(vein.first).empty())
{
if (ai.plan.dig_vein(out, vein.first, amount))
{
reason << "marked " << MaterialInfo(0, vein.first).toString() << " vein for excavation";
return;
}
}
}
}
reason << "could not find raw coke vein";
return;
}
case stock_item::rope:
{
tmpl.job_type = job_type::MakeChain;
tmpl.material_category.bits.cloth = 1;
break;
}
case stock_item::screw:
{
tmpl.job_type = job_type::MakeTrapComponent;
tmpl.item_subtype = min_subtype_for_item(stock_item::screw);
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::slab:
{
tmpl.job_type = job_type::ConstructSlab;
tmpl.mat_type = 0;
break;
}
case stock_item::slurry:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "MAKE_SLURRY_FROM_PLANT";
input.push_back(stock_item::slurry_plant);
break;
}
case stock_item::soap:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "MAKE_SOAP_FROM_TALLOW";
input.push_back(stock_item::lye);
input.push_back(stock_item::tallow);
break;
}
case stock_item::splint:
{
tmpl.job_type = job_type::ConstructSplint;
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::stepladder:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::stepladder);
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::table:
{
tmpl.job_type = job_type::ConstructTable;
tmpl.mat_type = 0;
break;
}
case stock_item::thread_seeds:
{
// only useful at game start, with low seeds stocks
tmpl.job_type = job_type::ProcessPlants;
input.push_back(stock_item::thread_plant);
break;
}
case stock_item::toy:
{
tmpl.job_type = job_type::MakeToy;
tmpl.mat_type = 0;
break;
}
case stock_item::traction_bench:
{
tmpl.job_type = job_type::ConstructTractionBench;
input.push_back(stock_item::table);
input.push_back(stock_item::rope);
input.push_back(stock_item::mechanism);
break;
}
case stock_item::weapon_melee:
{
queue_need_weapon(out, what, num_needed(what), reason);
return;
}
case stock_item::weapon_rack:
{
tmpl.job_type = job_type::ConstructWeaponRack;
tmpl.mat_type = 0;
break;
}
case stock_item::weapon_ranged:
{
queue_need_weapon(out, what, num_needed(what), reason, job_skill::NONE, false, true);
return;
}
case stock_item::weapon_training:
{
queue_need_weapon(out, what, num_needed(what), reason, job_skill::NONE, true);
return;
}
case stock_item::wheelbarrow:
{
tmpl.job_type = job_type::MakeTool;
tmpl.item_subtype = min_subtype_for_item(stock_item::wheelbarrow);
tmpl.material_category.bits.wood = 1;
break;
}
case stock_item::wood:
{
amount *= 2;
if (amount > 30)
amount = 30;
last_cutpos = cuttrees(out, amount / 6 + 1, reason);
return;
}
default:
{
throw DFHack::Error::InvalidArgument();
}
}
if (amount > 30)
amount = 30;
int32_t i_amount = amount;
if (!input.empty())
{
for (auto i : input)
{
int32_t c = count_free.at(i);
if (c < amount)
{
reason << "not enough " << i;
i_amount = std::min(i_amount, c);
if (Watch.Needed.count(i))
{
reason << ": ";
queue_need(out, i, amount - c, reason);
}
reason << "\n";
}
}
}
if (tmpl.material_category.whole != 0)
{
stock_item::item matcat_item;
if (tmpl.material_category.bits.wood)
{
matcat_item = stock_item::wood;
if (what != stock_item::bed && need_more(stock_item::bed))
{
reason << "need more beds";
return;
}
}
else if (tmpl.material_category.bits.cloth)
{
matcat_item = stock_item::cloth;
}
else if (tmpl.material_category.bits.leather)
{
matcat_item = stock_item::leather;
}
else if (tmpl.material_category.bits.bone)
{
matcat_item = stock_item::bone;
}
else if (tmpl.material_category.bits.shell)
{
matcat_item = stock_item::shell;
}
else
{
throw DFHack::Error::InvalidArgument();
}
int32_t mat_amount = count_free.at(matcat_item) - count_manager_orders_matcat(tmpl.material_category, tmpl.job_type);
if (mat_amount < amount)
{
reason << "not enough " << matcat_item;
if (Watch.Needed.count(matcat_item))
{
reason << ": ";
queue_need(out, matcat_item, amount - mat_amount, reason);
}
reason << "\n";
i_amount = std::min(i_amount, mat_amount);
}
}
amount = i_amount;
add_manager_order(out, tmpl, amount, reason);
}
int16_t Stocks::min_subtype_for_item(stock_item::item what)
{
const auto & count = count_subtype.at(what);
auto min = std::min_element(count.begin(), count.end(), [](std::pair<const int16_t, std::pair<int32_t, int32_t>> a, std::pair<const int16_t, std::pair<int32_t, int32_t>> b) -> bool
{
return a.second.first < b.second.first;
});
return min == count.end() ? -1 : min->first;
}
// make it so the stocks of 'what' decrease by 'amount'
void Stocks::queue_use(color_ostream & out, stock_item::item what, int32_t amount, std::ostream & reason)
{
if (amount <= 0)
{
reason << "amount not above threshold";
return;
}
df::manager_order_template tmpl;
tmpl.mat_index = -1;
std::vector<stock_item::item> input;
// stuff may rot/be brewed before we can process it
auto may_rot = [&amount]()
{
if (amount > 10)
amount /= 2;
if (amount > 4)
amount /= 2;
};
switch (what)
{
case stock_item::bag_plant:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "PROCESS_PLANT_TO_BAG";
may_rot();
input.push_back(stock_item::bag);
break;
}
case stock_item::bone:
{
if (need_more(stock_item::weapon_ranged))
{
int32_t need_crossbow = num_needed(stock_item::weapon_ranged) - count_free.at(stock_item::weapon_ranged);
tmpl.job_type = job_type::MakeWeapon;
tmpl.item_subtype = min_subtype_for_item(stock_item::weapon_ranged);
tmpl.material_category.bits.bone = 1;
if (amount > need_crossbow)
{
amount = need_crossbow;
}
}
else
{
tmpl.job_type = job_type::MakeAmmo;
tmpl.item_subtype = min_subtype_for_item(stock_item::ammo_training);
tmpl.material_category.bits.bone = 1;
may_rot();
}
break;
}
case stock_item::clay:
{
bool magma_kiln = false;
for (auto kiln : world->buildings.other[buildings_other_id::FURNACE_KILN_ANY])
{
if (virtual_cast<df::building_furnacest>(kiln)->type == furnace_type::MagmaKiln)
{
magma_kiln = true;
break;
}
}
if (!magma_kiln)
{
input.push_back(stock_item::coal);
}
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "MAKE_CLAY_STATUE";
break;
}
case stock_item::cloth_nodye:
{
tmpl.job_type = job_type::DyeCloth;
input.push_back(stock_item::dye);
may_rot();
break;
}
case stock_item::drink_fruit:
case stock_item::drink_plant:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = what == stock_item::drink_plant ? "BREW_DRINK_FROM_PLANT" : "BREW_DRINK_FROM_PLANT_GROWTH";
may_rot();
if (count_free[stock_item::barrel] > count_free[stock_item::food_storage])
{
input.push_back(stock_item::barrel);
}
else
{
input.push_back(stock_item::food_storage);
}
if (!need_more(stock_item::drink) && (need_more(stock_item::meal) || (need_more(stock_item::barrel) && need_more(stock_item::food_storage))))
{
reason << "have enough drinks";
return;
}
break;
}
case stock_item::food_ingredients:
{
tmpl.job_type = job_type::PrepareMeal;
tmpl.mat_type = 4; // roasts
amount = (amount + 4) / 5;
if (!need_more(stock_item::meal) && (need_more(stock_item::drink) || (need_more(stock_item::barrel) && need_more(stock_item::food_storage))))
{
reason << "have enough meals";
return;
}
break;
}
case stock_item::goblinite:
{
// make coke from bituminous coal has priority if there isn't a magma smelter
if (world->buildings.other[buildings_other_id::FURNACE_SMELTER_MAGMA].empty() &&
count_free.at(stock_item::raw_coke) > Watch.WatchStock.at(stock_item::raw_coke) &&
count_free.at(stock_item::coal) < 100)
{
reason << "making coal instead";
return;
}
tmpl.job_type = job_type::MeltMetalObject;
if (world->buildings.other[buildings_other_id::FURNACE_SMELTER_MAGMA].empty())
{
input.push_back(stock_item::coal);
}
break;
}
case stock_item::honey:
{
tmpl.job_type = job_type::CustomReaction;
tmpl.reaction_name = "MAKE_MEAD";
if (count_free[stock_item::barrel] > count_free[stock_item::food_storage])
{
input.push_back(stock_item::barrel);
}
else
{
input.push_back(stock_item::food_storage);
}
break;
}
case stock_item::honeycomb: