-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathautohauler.cpp
1173 lines (1037 loc) · 36.5 KB
/
autohauler.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
/**
* All includes copied from autolabor for now
*/
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <vector>
#include <algorithm>
#include "modules/Units.h"
#include "modules/World.h"
// DF data structure definition headers
#include "DataDefs.h"
#include <df/ui.h>
#include <df/world.h>
#include <df/unit.h>
#include <df/unit_soul.h>
#include <df/unit_labor.h>
#include <df/unit_skill.h>
#include <df/job.h>
#include <df/building.h>
#include <df/workshop_type.h>
#include <df/unit_misc_trait.h>
#include <df/entity_position_responsibility.h>
#include <df/historical_figure.h>
#include <df/historical_entity.h>
#include <df/histfig_entity_link.h>
#include <df/histfig_entity_link_positionst.h>
#include <df/entity_position_assignment.h>
#include <df/entity_position.h>
#include <df/building_tradedepotst.h>
#include <df/building_stockpilest.h>
#include <df/items_other_id.h>
#include <df/ui.h>
#include <df/activity_info.h>
#include <MiscUtils.h>
#include "modules/MapCache.h"
#include "modules/Items.h"
#include "modules/Units.h"
// Not sure what this does, but may have to figure it out later
#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
// I can see a reason for having all of these
using std::string;
using std::endl;
using std::vector;
using namespace DFHack;
using namespace df::enums;
// idk what this does
DFHACK_PLUGIN("autohauler");
REQUIRE_GLOBAL(ui);
REQUIRE_GLOBAL(world);
/*
* Autohauler module for dfhack
* Fork of autolabor, DFHack version 0.40.24-r2
*
* Rather than the all-of-the-above means of autolabor, autohauler will instead
* only manage hauling labors and leave skilled labors entirely to the user, who
* will probably use Dwarf Therapist to do so.
* Idle dwarves will be assigned the hauling labors; everyone else (including
* those currently hauling) will have the hauling labors removed. This is to
* encourage every dwarf to do their assigned skilled labors whenever possible,
* but resort to hauling when those jobs are not available. This also implies
* that the user will have a very tight skill assignment, with most skilled
* labors only being assigned to just one dwarf, no dwarf having more than two
* active skilled labors, and almost every non-military dwarf having at least
* one skilled labor assigned.
* Autohauler allows skills to be flagged as to prevent hauling labors from
* being assigned when the skill is present. By default this is the unused
* ALCHEMIST labor but can be changed by the user.
* It is noteworthy that, as stated in autolabor.cpp, "for almost all labors,
* once a dwarf begins a job it will finish that job even if the associated
* labor is removed." This is why we can remove hauling labors by default to try
* to force dwarves to do "real" jobs whenever they can.
* This is a standalone plugin. However, it would be wise to delete
* autolabor.plug.dll as this plugin is mutually exclusive with it.
*/
// Yep...don't know what it does
DFHACK_PLUGIN_IS_ENABLED(enable_autohauler);
// This is the configuration saved into the world save file
static PersistentDataItem config;
// There is a possibility I will add extensive, line-by-line debug capability
// later
static bool print_debug = false;
// Default number of frames between autohauler updates
const static int DEFAULT_FRAME_SKIP = 30;
// Number of frames between autohauler updates
static int frame_skip;
// Don't know what this does
command_result autohauler (color_ostream &out, std::vector <std::string> & parameters);
// Don't know what this does either
static bool isOptionEnabled(unsigned flag)
{
return config.isValid() && (config.ival(0) & flag) != 0;
}
// Not sure about the purpose of this
enum ConfigFlags {
CF_ENABLED = 1,
};
// Don't know what this does
static void setOptionEnabled(ConfigFlags flag, bool on)
{
if (!config.isValid())
return;
if (on)
config.ival(0) |= flag;
else
config.ival(0) &= ~flag;
}
// This is a vector of states and number of dwarves in that state
static std::vector<int> state_count(5);
// Employment status of dwarves
enum dwarf_state {
// Ready for a new task
IDLE,
// Busy with a useful task
BUSY,
// In the military, can't work
MILITARY,
// Baby or Child, can't work
CHILD,
// Doing something that precludes working, may be busy for a while
OTHER
};
// I presume this is the number of states in the following enumeration.
static const int NUM_STATE = 5;
// This is a list of strings to be associated with aforementioned dwarf_state
// struct
static const char *state_names[] = {
"IDLE",
"BUSY",
"MILITARY",
"CHILD",
"OTHER"
};
// List of possible activites of a dwarf that will be further narrowed to states
// IDLE - Specifically waiting to be assigned a task (No Job)
// BUSY - Performing a toggleable labor, or a support action for that labor.
// OTHER - Doing something else
static const dwarf_state dwarf_states[] = {
BUSY /* CarveFortification */,
BUSY /* DetailWall */,
BUSY /* DetailFloor */,
BUSY /* Dig */,
BUSY /* CarveUpwardStaircase */,
BUSY /* CarveDownwardStaircase */,
BUSY /* CarveUpDownStaircase */,
BUSY /* CarveRamp */,
BUSY /* DigChannel */,
BUSY /* FellTree */,
BUSY /* GatherPlants */,
BUSY /* RemoveConstruction */,
BUSY /* CollectWebs */,
BUSY /* BringItemToDepot */,
BUSY /* BringItemToShop */,
OTHER /* Eat */,
OTHER /* GetProvisions */,
OTHER /* Drink */,
OTHER /* Drink2 */,
OTHER /* FillWaterskin */,
OTHER /* FillWaterskin2 */,
OTHER /* Sleep */,
BUSY /* CollectSand */,
BUSY /* Fish */,
BUSY /* Hunt */,
BUSY /* HuntVermin */,
OTHER /* Kidnap */,
OTHER /* BeatCriminal */,
OTHER /* StartingFistFight */,
OTHER /* CollectTaxes */,
OTHER /* GuardTaxCollector */,
BUSY /* CatchLiveLandAnimal */,
BUSY /* CatchLiveFish */,
OTHER /* ReturnKill */,
OTHER /* CheckChest */,
OTHER /* StoreOwnedItem */,
BUSY /* PlaceItemInTomb */,
BUSY /* StoreItemInStockpile */,
BUSY /* StoreItemInBag */,
BUSY /* StoreItemInHospital */,
BUSY /* StoreItemInChest */,
BUSY /* StoreItemInCabinet */,
BUSY /* StoreWeapon */,
BUSY /* StoreArmor */,
BUSY /* StoreItemInBarrel */,
BUSY /* StoreItemInBin */,
OTHER /* SeekArtifact */,
OTHER /* SeekInfant */,
OTHER /* AttendParty */,
OTHER /* GoShopping */,
OTHER /* GoShopping2 */,
OTHER /* Clean */,
OTHER /* Rest */,
BUSY /* PickupEquipment */,
BUSY /* DumpItem */,
OTHER /* StrangeMoodCrafter */,
OTHER /* StrangeMoodJeweller */,
OTHER /* StrangeMoodForge */,
OTHER /* StrangeMoodMagmaForge */,
OTHER /* StrangeMoodBrooding */,
OTHER /* StrangeMoodFell */,
OTHER /* StrangeMoodCarpenter */,
OTHER /* StrangeMoodMason */,
OTHER /* StrangeMoodBowyer */,
OTHER /* StrangeMoodTanner */,
OTHER /* StrangeMoodWeaver */,
OTHER /* StrangeMoodGlassmaker */,
OTHER /* StrangeMoodMechanics */,
BUSY /* ConstructBuilding */,
BUSY /* ConstructDoor */,
BUSY /* ConstructFloodgate */,
BUSY /* ConstructBed */,
BUSY /* ConstructThrone */,
BUSY /* ConstructCoffin */,
BUSY /* ConstructTable */,
BUSY /* ConstructChest */,
BUSY /* ConstructBin */,
BUSY /* ConstructArmorStand */,
BUSY /* ConstructWeaponRack */,
BUSY /* ConstructCabinet */,
BUSY /* ConstructStatue */,
BUSY /* ConstructBlocks */,
BUSY /* MakeRawGlass */,
BUSY /* MakeCrafts */,
BUSY /* MintCoins */,
BUSY /* CutGems */,
BUSY /* CutGlass */,
BUSY /* EncrustWithGems */,
BUSY /* EncrustWithGlass */,
BUSY /* DestroyBuilding */,
BUSY /* SmeltOre */,
BUSY /* MeltMetalObject */,
BUSY /* ExtractMetalStrands */,
BUSY /* PlantSeeds */,
BUSY /* HarvestPlants */,
BUSY /* TrainHuntingAnimal */,
BUSY /* TrainWarAnimal */,
BUSY /* MakeWeapon */,
BUSY /* ForgeAnvil */,
BUSY /* ConstructCatapultParts */,
BUSY /* ConstructBallistaParts */,
BUSY /* MakeArmor */,
BUSY /* MakeHelm */,
BUSY /* MakePants */,
BUSY /* StudWith */,
BUSY /* ButcherAnimal */,
BUSY /* PrepareRawFish */,
BUSY /* MillPlants */,
BUSY /* BaitTrap */,
BUSY /* MilkCreature */,
BUSY /* MakeCheese */,
BUSY /* ProcessPlants */,
BUSY /* ProcessPlantsBag */,
BUSY /* ProcessPlantsVial */,
BUSY /* ProcessPlantsBarrel */,
BUSY /* PrepareMeal */,
BUSY /* WeaveCloth */,
BUSY /* MakeGloves */,
BUSY /* MakeShoes */,
BUSY /* MakeShield */,
BUSY /* MakeCage */,
BUSY /* MakeChain */,
BUSY /* MakeFlask */,
BUSY /* MakeGoblet */,
BUSY /* MakeInstrument */,
BUSY /* MakeToy */,
BUSY /* MakeAnimalTrap */,
BUSY /* MakeBarrel */,
BUSY /* MakeBucket */,
BUSY /* MakeWindow */,
BUSY /* MakeTotem */,
BUSY /* MakeAmmo */,
BUSY /* DecorateWith */,
BUSY /* MakeBackpack */,
BUSY /* MakeQuiver */,
BUSY /* MakeBallistaArrowHead */,
BUSY /* AssembleSiegeAmmo */,
BUSY /* LoadCatapult */,
BUSY /* LoadBallista */,
BUSY /* FireCatapult */,
BUSY /* FireBallista */,
BUSY /* ConstructMechanisms */,
BUSY /* MakeTrapComponent */,
BUSY /* LoadCageTrap */,
BUSY /* LoadStoneTrap */,
BUSY /* LoadWeaponTrap */,
BUSY /* CleanTrap */,
OTHER /* CastSpell */,
BUSY /* LinkBuildingToTrigger */,
BUSY /* PullLever */,
BUSY /* BrewDrink */,
BUSY /* ExtractFromPlants */,
BUSY /* ExtractFromRawFish */,
BUSY /* ExtractFromLandAnimal */,
BUSY /* TameVermin */,
BUSY /* TameAnimal */,
BUSY /* ChainAnimal */,
BUSY /* UnchainAnimal */,
BUSY /* UnchainPet */,
BUSY /* ReleaseLargeCreature */,
BUSY /* ReleasePet */,
BUSY /* ReleaseSmallCreature */,
BUSY /* HandleSmallCreature */,
BUSY /* HandleLargeCreature */,
BUSY /* CageLargeCreature */,
BUSY /* CageSmallCreature */,
BUSY /* RecoverWounded */,
BUSY /* DiagnosePatient */,
BUSY /* ImmobilizeBreak */,
BUSY /* DressWound */,
BUSY /* CleanPatient */,
BUSY /* Surgery */,
BUSY /* Suture */,
BUSY /* SetBone */,
BUSY /* PlaceInTraction */,
BUSY /* DrainAquarium */,
BUSY /* FillAquarium */,
BUSY /* FillPond */,
BUSY /* GiveWater */,
BUSY /* GiveFood */,
BUSY /* GiveWater2 */,
BUSY /* GiveFood2 */,
BUSY /* RecoverPet */,
BUSY /* PitLargeAnimal */,
BUSY /* PitSmallAnimal */,
BUSY /* SlaughterAnimal */,
BUSY /* MakeCharcoal */,
BUSY /* MakeAsh */,
BUSY /* MakeLye */,
BUSY /* MakePotashFromLye */,
BUSY /* FertilizeField */,
BUSY /* MakePotashFromAsh */,
BUSY /* DyeThread */,
BUSY /* DyeCloth */,
BUSY /* SewImage */,
BUSY /* MakePipeSection */,
BUSY /* OperatePump */,
OTHER /* ManageWorkOrders */,
OTHER /* UpdateStockpileRecords */,
OTHER /* TradeAtDepot */,
BUSY /* ConstructHatchCover */,
BUSY /* ConstructGrate */,
BUSY /* RemoveStairs */,
BUSY /* ConstructQuern */,
BUSY /* ConstructMillstone */,
BUSY /* ConstructSplint */,
BUSY /* ConstructCrutch */,
BUSY /* ConstructTractionBench */,
OTHER /* CleanSelf */,
BUSY /* BringCrutch */,
BUSY /* ApplyCast */,
BUSY /* CustomReaction */,
BUSY /* ConstructSlab */,
BUSY /* EngraveSlab */,
BUSY /* ShearCreature */,
BUSY /* SpinThread */,
BUSY /* PenLargeAnimal */,
BUSY /* PenSmallAnimal */,
BUSY /* MakeTool */,
BUSY /* CollectClay */,
BUSY /* InstallColonyInHive */,
BUSY /* CollectHiveProducts */,
OTHER /* CauseTrouble */,
OTHER /* DrinkBlood */,
OTHER /* ReportCrime */,
OTHER /* ExecuteCriminal */,
BUSY /* TrainAnimal */,
BUSY /* CarveTrack */,
BUSY /* PushTrackVehicle */,
BUSY /* PlaceTrackVehicle */,
BUSY /* StoreItemInVehicle */,
BUSY /* GeldAnimal */,
BUSY /* MakeFigurine */,
BUSY /* MakeAmulet */,
BUSY /* MakeScepter */,
BUSY /* MakeCrown */,
BUSY /* MakeRing */,
BUSY /* MakeEarring */,
BUSY /* MakeBracelet */,
BUSY /* MakeGem */,
BUSY /* PutItemOnDisplay */,
};
// Mode assigned to labors. Either it's a hauling job, or it's not.
enum labor_mode {
ALLOW,
HAULERS,
FORBID
};
// This is the default treatment of a particular labor.
struct labor_default
{
labor_mode mode;
int active_dwarfs;
};
// This is the current treatment of a particular labor.
// This would have been more cleanly presented as a class
struct labor_info
{
// It seems as if this is the means of accessing the world data
PersistentDataItem config;
// Number of dwarves assigned this labor
int active_dwarfs;
// Set the persistent data item associated with this labor treatment
// We Java folk hate pointers, but that's what the parameter will be
void set_config(PersistentDataItem a) { config = a; }
// Return the labor_mode associated with this labor
labor_mode mode() { return (labor_mode) config.ival(0); }
// Set the labor_mode associated with this labor
void set_mode(labor_mode mode) { config.ival(0) = mode; }
};
// This is a vector of all the current labor treatments
static std::vector<struct labor_info> labor_infos;
// This is just an array of all the labors, whether it should be untouched
// (DISABLE) or treated as a last-resort job (HAULERS).
static const struct labor_default default_labor_infos[] = {
/* MINE */ {ALLOW, 0},
/* HAUL_STONE */ {HAULERS, 0},
/* HAUL_WOOD */ {HAULERS, 0},
/* HAUL_BODY */ {HAULERS, 0},
/* HAUL_FOOD */ {HAULERS, 0},
/* HAUL_REFUSE */ {HAULERS, 0},
/* HAUL_ITEM */ {HAULERS, 0},
/* HAUL_FURNITURE */ {HAULERS, 0},
/* HAUL_ANIMAL */ {HAULERS, 0},
/* CLEAN */ {HAULERS, 0},
/* CUTWOOD */ {ALLOW, 0},
/* CARPENTER */ {ALLOW, 0},
/* DETAIL */ {ALLOW, 0},
/* MASON */ {ALLOW, 0},
/* ARCHITECT */ {ALLOW, 0},
/* ANIMALTRAIN */ {ALLOW, 0},
/* ANIMALCARE */ {ALLOW, 0},
/* DIAGNOSE */ {ALLOW, 0},
/* SURGERY */ {ALLOW, 0},
/* BONE_SETTING */ {ALLOW, 0},
/* SUTURING */ {ALLOW, 0},
/* DRESSING_WOUNDS */ {ALLOW, 0},
/* FEED_WATER_CIVILIANS */ {HAULERS, 0}, // This could also be ALLOW
/* RECOVER_WOUNDED */ {HAULERS, 0},
/* BUTCHER */ {ALLOW, 0},
/* TRAPPER */ {ALLOW, 0},
/* DISSECT_VERMIN */ {ALLOW, 0},
/* LEATHER */ {ALLOW, 0},
/* TANNER */ {ALLOW, 0},
/* BREWER */ {ALLOW, 0},
/* ALCHEMIST */ {FORBID, 0},
/* SOAP_MAKER */ {ALLOW, 0},
/* WEAVER */ {ALLOW, 0},
/* CLOTHESMAKER */ {ALLOW, 0},
/* MILLER */ {ALLOW, 0},
/* PROCESS_PLANT */ {ALLOW, 0},
/* MAKE_CHEESE */ {ALLOW, 0},
/* MILK */ {ALLOW, 0},
/* COOK */ {ALLOW, 0},
/* PLANT */ {ALLOW, 0},
/* HERBALIST */ {ALLOW, 0},
/* FISH */ {ALLOW, 0},
/* CLEAN_FISH */ {ALLOW, 0},
/* DISSECT_FISH */ {ALLOW, 0},
/* HUNT */ {ALLOW, 0},
/* SMELT */ {ALLOW, 0},
/* FORGE_WEAPON */ {ALLOW, 0},
/* FORGE_ARMOR */ {ALLOW, 0},
/* FORGE_FURNITURE */ {ALLOW, 0},
/* METAL_CRAFT */ {ALLOW, 0},
/* CUT_GEM */ {ALLOW, 0},
/* ENCRUST_GEM */ {ALLOW, 0},
/* WOOD_CRAFT */ {ALLOW, 0},
/* STONE_CRAFT */ {ALLOW, 0},
/* BONE_CARVE */ {ALLOW, 0},
/* GLASSMAKER */ {ALLOW, 0},
/* EXTRACT_STRAND */ {ALLOW, 0},
/* SIEGECRAFT */ {ALLOW, 0},
/* SIEGEOPERATE */ {ALLOW, 0},
/* BOWYER */ {ALLOW, 0},
/* MECHANIC */ {ALLOW, 0},
/* POTASH_MAKING */ {ALLOW, 0},
/* LYE_MAKING */ {ALLOW, 0},
/* DYER */ {ALLOW, 0},
/* BURN_WOOD */ {ALLOW, 0},
/* OPERATE_PUMP */ {ALLOW, 0},
/* SHEARER */ {ALLOW, 0},
/* SPINNER */ {ALLOW, 0},
/* POTTERY */ {ALLOW, 0},
/* GLAZING */ {ALLOW, 0},
/* PRESSING */ {ALLOW, 0},
/* BEEKEEPING */ {ALLOW, 0},
/* WAX_WORKING */ {ALLOW, 0},
/* HANDLE_VEHICLES */ {HAULERS, 0},
/* HAUL_TRADE */ {HAULERS, 0},
/* PULL_LEVER */ {HAULERS, 0},
/* REMOVE_CONSTRUCTION */ {HAULERS, 0},
/* HAUL_WATER */ {HAULERS, 0},
/* GELD */ {ALLOW, 0},
/* BUILD_ROAD */ {HAULERS, 0},
/* BUILD_CONSTRUCTION */ {HAULERS, 0},
/* PAPERMAKING */ {ALLOW, 0},
/* BOOKBINDING */ {ALLOW, 0}
};
/**
* Reset labor to default treatment
*/
static void reset_labor(df::unit_labor labor)
{
labor_infos[labor].set_mode(default_labor_infos[labor].mode);
}
/**
* This is individualized dwarf info populated in plugin_onupdate
*/
struct dwarf_info_t
{
// Current simplified employment status of dwarf
dwarf_state state;
// Set to true if for whatever reason we are exempting this dwarf
// from hauling
bool haul_exempt;
};
/**
* Disable autohauler labor lists
*/
static void cleanup_state()
{
enable_autohauler = false;
labor_infos.clear();
}
static void enable_alchemist(color_ostream &out)
{
if (!Units::setLaborValidity(unit_labor::ALCHEMIST, true))
{
// informational only; this is a non-fatal error
out.printerr("%s: Could not flag Alchemist as a valid skill; Alchemist will not"
" be settable from DF or DFHack labor management screens.\n", plugin_name);
}
}
/**
* Initialize the plugin labor lists
*/
static void init_state(color_ostream &out)
{
// This obtains the persistent data from the world save file
config = World::GetPersistentData("autohauler/config");
// Check to ensure that the persistent data item actually exists and that
// the first item in the array of ints isn't -1 (implies disabled)
if (config.isValid() && config.ival(0) == -1)
config.ival(0) = 0;
// Check to see if the plugin is enabled in the persistent data, if so then
// enable the local flag for autohauler being enabled
enable_autohauler = isOptionEnabled(CF_ENABLED);
// If autohauler is not enabled then it's pretty pointless to do the rest
if (!enable_autohauler)
return;
// First get the frame skip from persistent data, or create the item
// if not present
auto cfg_frameskip = World::GetPersistentData("autohauler/frameskip");
if (cfg_frameskip.isValid())
{
frame_skip = cfg_frameskip.ival(0);
}
else
{
// Add to persistent data then get it to assert it's actually there
cfg_frameskip = World::AddPersistentData("autohauler/frameskip");
cfg_frameskip.ival(0) = DEFAULT_FRAME_SKIP;
frame_skip = cfg_frameskip.ival(0);
}
/* Here we are going to populate the labor list by loading persistent data
* from the world save */
// This is a vector of all the persistent data items from config
std::vector<PersistentDataItem> items;
// This populates the aforementioned vector
World::GetPersistentData(&items, "autohauler/labors/", true);
// Resize the list of current labor treatments to size of list of default
// labor treatments
labor_infos.resize(ARRAY_COUNT(default_labor_infos));
// For every persistent data item...
for (auto p = items.begin(); p != items.end(); p++)
{
// Load as a string the key associated with the persistent data item
string key = p->key();
// Translate the string into a labor defined by global dfhack constants
df::unit_labor labor = (df::unit_labor) atoi(key.substr(strlen("autohauler/labors/")).c_str());
// Ensure that the labor is defined in the existing list
if (labor >= 0 && size_t(labor) < labor_infos.size())
{
// Link the labor treatment with the associated persistent data item
labor_infos[labor].set_config(*p);
// Set the number of dwarves associated with labor to zero
labor_infos[labor].active_dwarfs = 0;
}
}
// Add default labors for those not in save
for (size_t i = 0; i < ARRAY_COUNT(default_labor_infos); i++) {
// Determine if the labor is already present. If so, exit the for loop
if (labor_infos[i].config.isValid())
continue;
// Not sure of the mechanics, but it seems to give an output stream
// giving a string for the new persistent data item
std::stringstream name;
name << "autohauler/labors/" << i;
// Add a new persistent data item as it is not currently in the save
labor_infos[i].set_config(World::AddPersistentData(name.str()));
// Set the active counter to zero
labor_infos[i].active_dwarfs = 0;
// Reset labor to default treatment
reset_labor((df::unit_labor) i);
}
// Allow Alchemist to be set in the labor-related UI screens so the player
// can actually use it as a flag without having to run Dwarf Therapist
enable_alchemist(out);
}
/**
* Call this method to enable the plugin.
*/
static void enable_plugin(color_ostream &out)
{
// If there is no config persistent item, make one
if (!config.isValid())
{
config = World::AddPersistentData("autohauler/config");
config.ival(0) = 0;
}
// I think this is already done in init_state(), but it can't hurt
setOptionEnabled(CF_ENABLED, true);
enable_autohauler = true;
// Output to console that the plugin is enabled
out << "Enabling the plugin." << endl;
// Disable autohauler and clear the labor list
cleanup_state();
// Initialize the plugin
init_state(out);
}
/**
* Initialize the plugin
*/
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
// This seems to verify that the default labor list and the current labor
// list are the same size
if(ARRAY_COUNT(default_labor_infos) != ENUM_LAST_ITEM(unit_labor) + 1)
{
out.printerr("autohauler: labor size mismatch\n");
return CR_FAILURE;
}
// Essentially an introduction dumped to the console
commands.push_back(PluginCommand(
"autohauler",
"Automatically manage hauling labors.",
autohauler));
// Initialize plugin labor lists
init_state(out);
return CR_OK;
}
/**
* Shut down the plugin
*/
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
cleanup_state();
return CR_OK;
}
/**
* This method responds to the map being loaded, or unloaded.
*/
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
switch (event) {
case SC_MAP_LOADED:
cleanup_state();
init_state(out);
break;
case SC_MAP_UNLOADED:
cleanup_state();
break;
default:
break;
}
return CR_OK;
}
/**
* This method is called every frame in Dwarf Fortress from my understanding.
*/
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{
// This makes it so that the plugin is only run every 60 steps, in order to
// save FPS. Since it is static, this is declared before this method is called
static int step_count = 0;
// Cancel run if the world doesn't exist or plugin isn't enabled
if(!world || !world->map.block_index || !enable_autohauler) { return CR_OK; }
// Increment step count
step_count++;
// Run aforementioned step count and return unless threshold is reached.
if (step_count < frame_skip) return CR_OK;
// Reset step count since at this point it has reached 60
step_count = 0;
// Create a vector of units. This will be populated in the following for loop.
std::vector<df::unit *> dwarfs;
// Scan the world and look for any citizens in the player's civilization.
// Add these to the list of dwarves.
// xxx Does it need to be ++i?
for (size_t i = 0; i < world->units.active.size(); ++i)
{
df::unit* cre = world->units.active[i];
if (Units::isCitizen(cre))
{
dwarfs.push_back(cre);
}
}
// This just keeps track of the number of civilians from the previous for loop.
int n_dwarfs = dwarfs.size();
// This will return if there are no civilians. Otherwise could call
// nonexistent elements of array.
if (n_dwarfs == 0)
return CR_OK;
// This is a matching of assigned jobs with a dwarf's state
// xxx but wouldn't it be better if this and "dwarfs" were in the same object?
std::vector<dwarf_info_t> dwarf_info(n_dwarfs);
// Reset the counter for number of dwarves in states to zero
state_count.clear();
state_count.resize(NUM_STATE);
// Find the activity state for each dwarf
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
{
/* Before determining how to handle employment status, handle
* hauling exemptions first */
// Default deny condition of on break for later else-if series
bool is_migrant = false;
// Scan every labor. If a labor that disallows hauling is present
// for the dwarf, the dwarf is hauling exempt
FOR_ENUM_ITEMS(unit_labor, labor)
{
if (!(labor == unit_labor::NONE))
{
bool test1 = labor_infos[labor].mode() == FORBID;
bool test2 = dwarfs[dwarf]->status.labors[labor];
if(test1 && test2) dwarf_info[dwarf].haul_exempt = true;
}
}
// Scan a dwarf's miscellaneous traits for on break or migrant status.
// If either of these are present, disable hauling because we want them
// to try to find real jobs first
for (auto p = dwarfs[dwarf]->status.misc_traits.begin(); p < dwarfs[dwarf]->status.misc_traits.end(); p++)
{
if ((*p)->id == misc_trait_type::Migrant)
is_migrant = true;
}
/* Now determine a dwarf's employment status and decide whether
* to assign hauling */
// I don't think you can set the labors for babies and children, but let's
// ignore them anyway
if (Units::isBaby(dwarfs[dwarf]) || Units::isChild(dwarfs[dwarf]))
{
dwarf_info[dwarf].state = CHILD;
}
// Account for any hauling exemptions here
else if (dwarf_info[dwarf].haul_exempt)
{
dwarf_info[dwarf].state = BUSY;
}
// Account for the military
else if (ENUM_ATTR(profession, military, dwarfs[dwarf]->profession))
dwarf_info[dwarf].state = MILITARY;
// Account for incoming migrants
else if (is_migrant)
{
dwarf_info[dwarf].state = OTHER;
}
else if (dwarfs[dwarf]->job.current_job == NULL)
{
dwarf_info[dwarf].state = IDLE;
}
// If it gets to this point we look at the task and assign either BUSY or OTHER
else
{
int job = dwarfs[dwarf]->job.current_job->job_type;
if (job >= 0 && size_t(job) < ARRAY_COUNT(dwarf_states))
dwarf_info[dwarf].state = dwarf_states[job];
else
{
// Warn the console that the dwarf has an unregistered labor, default to BUSY
out.print("Dwarf %i \"%s\" has unknown job %i\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), job);
dwarf_info[dwarf].state = BUSY;
}
}
// Debug: Output dwarf job and state data
if(print_debug)
out.print("Dwarf %i %s State: %i\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(),
dwarf_info[dwarf].state);
// Increment corresponding labor in default_labor_infos struct
state_count[dwarf_info[dwarf].state]++;
}
// At this point the debug if present has been completed
print_debug = false;
// This is a vector of all the labors
std::vector<df::unit_labor> labors;
// For every labor...
FOR_ENUM_ITEMS(unit_labor, labor)
{
// Ignore all nonexistent labors
if (labor == unit_labor::NONE)
continue;
// Set number of active dwarves for this job to zero
labor_infos[labor].active_dwarfs = 0;
// And add the labor to the aforementioned vector of labors
labors.push_back(labor);
}
// This is a different algorithm than Autolabor. Instead, the intent is to
// have "real" jobs filled first, then if nothing is available the dwarf
// instead resorts to hauling.
// IDLE - Enable hauling
// BUSY - Disable hauling
// OTHER - Enable hauling
// MILITARY - Enable hauling
// There was no reason to put potential haulers in an array. All of them are
// covered in the following for loop.
// Equivalent of Java for(unit_labor : labor)
// For every labor...
FOR_ENUM_ITEMS(unit_labor, labor)
{
// If this is a non-labor continue to next item
if (labor == unit_labor::NONE)
continue;
// If this is not a hauling labor continue to next item
if (labor_infos[labor].mode() != HAULERS)
continue;
// For every dwarf...
for(size_t dwarf = 0; dwarf < dwarfs.size(); dwarf++)
{
if (!Units::isValidLabor(dwarfs[dwarf], labor))
continue;
// Set hauling labors based on employment states
if(dwarf_info[dwarf].state == IDLE) {
dwarfs[dwarf]->status.labors[labor] = true;
}
else if(dwarf_info[dwarf].state == MILITARY) {
dwarfs[dwarf]->status.labors[labor] = true;
}
else if(dwarf_info[dwarf].state == OTHER) {
dwarfs[dwarf]->status.labors[labor] = true;
}
else if(dwarf_info[dwarf].state == BUSY) {
dwarfs[dwarf]->status.labors[labor] = false;
}
// If at the end of this the dwarf has the hauling labor, increment the
// counter
if(dwarfs[dwarf]->status.labors[labor])
{
labor_infos[labor].active_dwarfs++;
}
// CHILD ignored
}
// Let's play a game of "find the missing bracket!" I hope this is correct.
}
// This would be the last statement of the method
return CR_OK;
}
/**
* xxx Isn't this a repeat of enable_plugin? If it is separately called, then
* passing a constructor should suffice.
*/
DFhackCExport command_result plugin_enable ( color_ostream &out, bool enable )
{
if (!Core::getInstance().isWorldLoaded()) {
out.printerr("World is not loaded: please load a game first.\n");
return CR_FAILURE;
}
if (enable && !enable_autohauler)
{
enable_plugin(out);
}
else if(!enable && enable_autohauler)
{
enable_autohauler = false;
setOptionEnabled(CF_ENABLED, false);
out << "Autohauler is disabled." << endl;
}
return CR_OK;
}
/**
* Print the aggregate labor status to the console.
*/
void print_labor (df::unit_labor labor, color_ostream &out)
{
string labor_name = ENUM_KEY_STR(unit_labor, labor);
out << labor_name << ": ";