-
Notifications
You must be signed in to change notification settings - Fork 463
/
zone.cpp
2190 lines (1930 loc) · 69.9 KB
/
zone.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
// Intention: help with activity zone management (auto-pasture animals, auto-pit goblins, ...)
//
// the following things would be nice:
// - dump info about pastures, pastured animals, count non-pastured tame animals, print gender info
// - help finding caged dwarves? (maybe even allow to build their cages for fast release)
// - dump info about caged goblins, animals, ...
// - count grass tiles on pastures, move grazers to new pasture if old pasture is empty
// move hungry unpastured grazers to pasture with grass
//
// What is working so far:
// - print detailed info about activity zone and units under cursor (mostly for checking refs and stuff)
// - mark a zone which is used for future assignment commands
// - assign single selected creature to a zone
// - mass-assign creatures using filters
// - unassign single creature under cursor from current zone
// - pitting own dwarves :)
#include "PluginManager.h"
using namespace DFHack;
DFHACK_PLUGIN("zone");
/*
#include <functional>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include "df/building_cagest.h"
#include "df/building_chainst.h"
#include "df/building_civzonest.h"
#include "df/general_ref_building_civzone_assignedst.h"
#include "df/plotinfost.h"
#include "df/unit.h"
#include "df/unit_relationship_type.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/world.h"
#include "uicommon.h"
#include "VTableInterpose.h"
#include "modules/Buildings.h"
#include "modules/Gui.h"
#include "modules/Units.h"
#include "modules/Translation.h"
using std::function;
using std::make_pair;
using std::ostringstream;
using std::pair;
using std::runtime_error;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::vector;
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(cursor);
REQUIRE_GLOBAL(gps);
REQUIRE_GLOBAL(plotinfo);
REQUIRE_GLOBAL(ui_building_item_cursor);
REQUIRE_GLOBAL(ui_building_assign_type);
REQUIRE_GLOBAL(ui_building_assign_is_marked);
REQUIRE_GLOBAL(ui_building_assign_units);
REQUIRE_GLOBAL(ui_building_assign_items);
REQUIRE_GLOBAL(ui_building_in_assign);
REQUIRE_GLOBAL(ui_menu_width);
REQUIRE_GLOBAL(world);
static void doMarkForSlaughter(df::unit* unit)
{
unit->flags2.bits.slaughter = 1;
}
// found a unit with weird position values on one of my maps (negative and in the thousands)
// it didn't appear in the animal stocks screen, but looked completely fine otherwise (alive, tame, own, etc)
// maybe a rare bug, but better avoid assigning such units to zones or slaughter etc.
static bool hasValidMapPos(df::unit* unit)
{
if( unit->pos.x >=0 && unit->pos.y >= 0 && unit->pos.z >= 0
&& unit->pos.x < world->map.x_count
&& unit->pos.y < world->map.y_count
&& unit->pos.z < world->map.z_count)
return true;
else
return false;
}
static bool isInBuiltCageRoom(df::unit*);
// dump some unit info
static void unitInfo(color_ostream & out, df::unit* unit, bool verbose = false)
{
out.print("Unit %d ", unit->id); //race %d, civ %d,", creature->race, creature->civ_id
if(unit->name.has_name)
{
// units given a nick with the rename tool might not have a first name (animals etc)
string firstname = unit->name.first_name;
if(firstname.size() > 0)
{
firstname[0] = toupper(firstname[0]);
out << "Name: " << firstname;
}
if(unit->name.nickname.size() > 0)
out << " '" << unit->name.nickname << "'";
out << ", ";
}
if(Units::isAdult(unit))
out << "adult";
else if(Units::isBaby(unit))
out << "baby";
else if(Units::isChild(unit))
out << "child";
out << " ";
// sometimes empty even in vanilla RAWS, sometimes contains full race name (e.g. baby alpaca)
// all animals I looked at don't have babies anyways, their offspring tarts as CHILD
//out << getRaceBabyName(unit);
//out << getRaceChildName(unit);
out << Units::getRaceName(unit) << " (";
switch(unit->sex)
{
case 0:
out << "female";
break;
case 1:
out << "male";
break;
case -1:
default:
out << "n/a";
break;
}
out << ")";
out << ", age: " << Units::getAge(unit, true);
if(Units::isTame(unit))
out << ", tame";
if(Units::isOwnCiv(unit))
out << ", owned";
if(Units::isWar(unit))
out << ", war";
if(Units::isHunter(unit))
out << ", hunter";
if(Units::isMerchant(unit))
out << ", merchant";
if(Units::isForest(unit))
out << ", forest";
if(Units::isEggLayer(unit))
out << ", egglayer";
if(Units::isGrazer(unit))
out << ", grazer";
if(Units::isMilkable(unit))
out << ", milkable";
if(unit->flags2.bits.slaughter)
out << ", slaughter";
if(verbose)
{
out << ". Pos: ("<<unit->pos.x << "/"<< unit->pos.y << "/" << unit->pos.z << ") " << endl;
out << "index in units vector: " << Units::findIndexById(unit->id) << endl;
}
out << endl;
if(!verbose)
return;
//out << "number of refs: " << creature->general_refs.size() << endl;
for(size_t r = 0; r<unit->general_refs.size(); r++)
{
df::general_ref* ref = unit->general_refs.at(r);
df::general_ref_type refType = ref->getType();
out << " ref#" << r <<" refType#" << refType << " "; //endl;
switch(refType)
{
case df::general_ref_type::BUILDING_CIVZONE_ASSIGNED:
{
out << "assigned to zone";
df::building_civzonest * civAss = (df::building_civzonest *) ref->getBuilding();
out << " #" << civAss->id;
}
break;
case df::general_ref_type::CONTAINED_IN_ITEM:
out << "contained in item";
break;
case df::general_ref_type::BUILDING_CAGED:
out << "caged";
break;
case df::general_ref_type::BUILDING_CHAIN:
out << "chained";
break;
default:
//out << "unhandled reftype";
break;
}
out << endl;
}
if(isInBuiltCageRoom(unit))
{
out << "in a room." << endl;
}
}
static bool isCage(df::building * building)
{
return building && (building->getType() == df::building_type::Cage);
}
static bool isChain(df::building * building)
{
return building && (building->getType() == df::building_type::Chain);
}
static df::general_ref_building_civzone_assignedst * createCivzoneRef()
{
static bool vt_initialized = false;
df::general_ref_building_civzone_assignedst* newref = NULL;
// after having run successfully for the first time it's safe to simply create the object
if(vt_initialized)
{
newref = (df::general_ref_building_civzone_assignedst*)
df::general_ref_building_civzone_assignedst::_identity.instantiate();
return newref;
}
// being called for the first time, need to initialize the vtable
for(size_t i = 0; i < world->units.all.size(); i++)
{
df::unit * creature = world->units.all[i];
for(size_t r = 0; r<creature->general_refs.size(); r++)
{
df::general_ref* ref;
ref = creature->general_refs.at(r);
if(ref->getType() == df::general_ref_type::BUILDING_CIVZONE_ASSIGNED)
{
if (strict_virtual_cast<df::general_ref_building_civzone_assignedst>(ref))
{
// !! calling new() doesn't work, need _identity.instantiate() instead !!
newref = (df::general_ref_building_civzone_assignedst*)
df::general_ref_building_civzone_assignedst::_identity.instantiate();
vt_initialized = true;
break;
}
}
}
if(vt_initialized)
break;
}
return newref;
}
static bool isInBuiltCage(df::unit* unit);
// check if assigned to pen, pit, (built) cage or chain
// note: BUILDING_CAGED is not set for animals (maybe it's used for dwarves who get caged as sentence)
// animals in cages (no matter if built or on stockpile) get the ref CONTAINED_IN_ITEM instead
// removing them from cages on stockpiles is no problem even without clearing the ref
// and usually it will be desired behavior to do so.
static bool isAssigned(df::unit* unit)
{
bool assigned = false;
for (size_t r=0; r < unit->general_refs.size(); r++)
{
df::general_ref * ref = unit->general_refs[r];
auto rtype = ref->getType();
if( rtype == df::general_ref_type::BUILDING_CIVZONE_ASSIGNED
|| rtype == df::general_ref_type::BUILDING_CAGED
|| rtype == df::general_ref_type::BUILDING_CHAIN
|| (rtype == df::general_ref_type::CONTAINED_IN_ITEM && isInBuiltCage(unit))
)
{
assigned = true;
break;
}
}
return assigned;
}
static bool isAssignedToZone(df::unit* unit)
{
bool assigned = false;
for (size_t r=0; r < unit->general_refs.size(); r++)
{
df::general_ref * ref = unit->general_refs[r];
auto rtype = ref->getType();
if(rtype == df::general_ref_type::BUILDING_CIVZONE_ASSIGNED)
{
assigned = true;
break;
}
}
return assigned;
}
// check if contained in item (e.g. animals in cages)
static bool isContainedInItem(df::unit* unit)
{
bool contained = false;
for (size_t r=0; r < unit->general_refs.size(); r++)
{
df::general_ref * ref = unit->general_refs[r];
auto rtype = ref->getType();
if(rtype == df::general_ref_type::CONTAINED_IN_ITEM)
{
contained = true;
break;
}
}
return contained;
}
static bool isInBuiltCage(df::unit* unit)
{
bool caged = false;
for (size_t b=0; b < world->buildings.all.size(); b++)
{
df::building* building = world->buildings.all[b];
if( building->getType() == df::building_type::Cage)
{
df::building_cagest* cage = (df::building_cagest*) building;
for(size_t c=0; c<cage->assigned_units.size(); c++)
{
if(cage->assigned_units[c] == unit->id)
{
caged = true;
break;
}
}
}
if(caged)
break;
}
return caged;
}
// built cage defined as room (supposed to detect zoo cages)
static bool isInBuiltCageRoom(df::unit* unit)
{
bool caged_room = false;
for (size_t b=0; b < world->buildings.all.size(); b++)
{
df::building* building = world->buildings.all[b];
// !!! building->isRoom() returns true if the building can be made a room but currently isn't
// !!! except for coffins/tombs which always return false
// !!! using the bool is_room however gives the correct state/value
if(!building->is_room)
continue;
if(building->getType() == df::building_type::Cage)
{
df::building_cagest* cage = (df::building_cagest*) building;
for(size_t c=0; c<cage->assigned_units.size(); c++)
{
if(cage->assigned_units[c] == unit->id)
{
caged_room = true;
break;
}
}
}
if(caged_room)
break;
}
return caged_room;
}
static df::building * getBuiltCageAtPos(df::coord pos)
{
df::building* cage = NULL;
for (size_t b=0; b < world->buildings.all.size(); b++)
{
df::building* building = world->buildings.all[b];
if( building->getType() == df::building_type::Cage
&& building->x1 == pos.x
&& building->y1 == pos.y
&& building->z == pos.z )
{
// don't set pointer if not constructed yet
if(building->getBuildStage()!=building->getMaxBuildStage())
break;
cage = building;
break;
}
}
return cage;
}
// check if unit is already assigned to a zone, remove that ref from unit and old zone
// check if unit is already assigned to a cage, remove that ref from the cage
// returns false if no cage or pasture information was found
// helps as workaround for http://www.bay12games.com/dwarves/mantisbt/view.php?id=4475 by the way
// (pastured animals assigned to chains will get hauled back and forth because the pasture ref is not deleted)
static bool unassignUnitFromBuilding(df::unit* unit)
{
bool success = false;
for (std::size_t idx = 0; idx < unit->general_refs.size(); idx++)
{
df::general_ref * oldref = unit->general_refs[idx];
switch(oldref->getType())
{
case df::general_ref_type::BUILDING_CIVZONE_ASSIGNED:
{
unit->general_refs.erase(unit->general_refs.begin() + idx);
df::building_civzonest * oldciv = (df::building_civzonest *) oldref->getBuilding();
for(size_t oc=0; oc<oldciv->assigned_units.size(); oc++)
{
if(oldciv->assigned_units[oc] == unit->id)
{
oldciv->assigned_units.erase(oldciv->assigned_units.begin() + oc);
break;
}
}
delete oldref;
success = true;
break;
}
case df::general_ref_type::CONTAINED_IN_ITEM:
{
// game does not erase the ref until creature gets removed from cage
//unit->general_refs.erase(unit->general_refs.begin() + idx);
// walk through buildings, check cages for inhabitants, compare ids
for (size_t b=0; b < world->buildings.all.size(); b++)
{
bool found = false;
df::building* building = world->buildings.all[b];
if(isCage(building))
{
df::building_cagest* oldcage = (df::building_cagest*) building;
for(size_t oc=0; oc<oldcage->assigned_units.size(); oc++)
{
if(oldcage->assigned_units[oc] == unit->id)
{
oldcage->assigned_units.erase(oldcage->assigned_units.begin() + oc);
found = true;
break;
}
}
}
if(found)
break;
}
success = true;
break;
}
case df::general_ref_type::BUILDING_CHAIN:
{
// try not erasing the ref and see what happens
//unit->general_refs.erase(unit->general_refs.begin() + idx);
// probably need to delete chain reference here
//success = true;
break;
}
case df::general_ref_type::BUILDING_CAGED:
{
// not sure what to do here, doesn't seem to get used by the game
//unit->general_refs.erase(unit->general_refs.begin() + idx);
//success = true;
break;
}
default:
{
// some reference which probably shouldn't get deleted
// (animals who are historical figures and have a NEMESIS reference or whatever)
break;
}
}
}
return success;
}
// assign to pen or pit
static command_result assignUnitToZone(color_ostream& out, df::unit* unit, df::building* building, bool verbose = false)
{
// building must be a pen/pasture or pit
if(!Buildings::isPenPasture(building) && !Buildings::isPitPond(building))
{
out << "Invalid building type. This is not a pen/pasture or pit/pond." << endl;
return CR_WRONG_USAGE;
}
// try to get a fresh civzone ref
df::general_ref_building_civzone_assignedst * ref = createCivzoneRef();
if(!ref)
{
out << "Could not find a clonable activity zone reference" << endl
<< "You need to pen/pasture/pit at least one creature" << endl
<< "before using 'assign' for the first time." << endl;
return CR_WRONG_USAGE;
}
// check if unit is already pastured, remove that ref from unit and old pasture
// testing showed that removing the ref from the unit only seems to be necessary for pastured creatures
// if they are in cages on stockpiles the game unassigns them automatically
// if they are in built cages the pointer to the creature needs to be removed from the cage
// TODO: check what needs to be done for chains
bool cleared_old = unassignUnitFromBuilding(unit);
if(verbose)
{
if(cleared_old)
out << "old zone info cleared.";
else
out << "no old zone info found.";
}
ref->building_id = building->id;
unit->general_refs.push_back(ref);
df::building_civzonest * civz = (df::building_civzonest *) building;
civz->assigned_units.push_back(unit->id);
out << "Unit " << unit->id
<< "(" << Units::getRaceName(unit) << ")"
<< " assigned to zone " << building->id;
if(Buildings::isPitPond(building))
out << " (pit/pond).";
if(Buildings::isPenPasture(building))
out << " (pen/pasture).";
out << endl;
return CR_OK;
}
static command_result assignUnitToCage(color_ostream& out, df::unit* unit, df::building* building, bool verbose)
{
// building must be a pen/pasture or pit
if(!isCage(building))
{
out << "Invalid building type. This is not a cage." << endl;
return CR_WRONG_USAGE;
}
// don't assign owned pets to a cage. the owner will release them, resulting into infinite hauling (df bug)
if(unit->relationship_ids[df::unit_relationship_type::Pet] != -1)
return CR_OK;
// check if unit is already pastured or caged, remove refs where necessary
bool cleared_old = unassignUnitFromBuilding(unit);
if(verbose)
{
if(cleared_old)
out << "old zone info cleared.";
else
out << "no old zone info found.";
}
//ref->building_id = building->id;
//unit->general_refs.push_back(ref);
df::building_cagest* civz = (df::building_cagest*) building;
civz->assigned_units.push_back(unit->id);
out << "Unit " << unit->id
<< "(" << Units::getRaceName(unit) << ")"
<< " assigned to cage " << building->id;
out << endl;
return CR_OK;
}
static command_result assignUnitToChain(color_ostream& out, df::unit* unit, df::building* building, bool verbose)
{
out << "sorry. assigning to chains is not possible yet." << endl;
return CR_WRONG_USAGE;
}
static command_result assignUnitToBuilding(color_ostream& out, df::unit* unit, df::building* building, bool verbose)
{
command_result result = CR_WRONG_USAGE;
if(Buildings::isActivityZone(building))
result = assignUnitToZone(out, unit, building, verbose);
else if(isCage(building))
result = assignUnitToCage(out, unit, building, verbose);
else if(isChain(building))
result = assignUnitToChain(out, unit, building, verbose);
else
out << "Cannot assign units to this type of building!" << endl;
return result;
}
static command_result assignUnitsToCagezone(color_ostream& out, vector<df::unit*> units, df::building* building, bool verbose)
{
if(!Buildings::isPenPasture(building))
{
out << "A cage zone needs to be a pen/pasture containing at least one cage!" << endl;
return CR_WRONG_USAGE;
}
int32_t x1 = building->x1;
int32_t x2 = building->x2;
int32_t y1 = building->y1;
int32_t y2 = building->y2;
int32_t z = building->z;
vector <df::building_cagest*> cages;
for (int32_t x = x1; x<=x2; x++)
{
for (int32_t y = y1; y<=y2; y++)
{
df::building* cage = getBuiltCageAtPos(df::coord(x,y,z));
if(cage)
{
df::building_cagest* cagest = (df::building_cagest*) cage;
cages.push_back(cagest);
}
}
}
if(!cages.size())
{
out << "No cages found in this zone!" << endl;
return CR_WRONG_USAGE;
}
else
{
out << "Number of cages: " << cages.size() << endl;
}
while(units.size())
{
// hrm, better use sort() instead?
df::building_cagest * bestcage = cages[0];
size_t lowest = cages[0]->assigned_units.size();
for(size_t i=1; i<cages.size(); i++)
{
if(cages[i]->assigned_units.size()<lowest)
{
lowest = cages[i]->assigned_units.size();
bestcage = cages[i];
}
}
df::unit* unit = units.back();
units.pop_back();
command_result result = assignUnitToCage(out, unit, (df::building*) bestcage, verbose);
if(result!=CR_OK)
return result;
}
return CR_OK;
}
static command_result nickUnitsInZone(color_ostream& out, df::building* building, string nick)
{
// building must be a pen/pasture or pit
if(!Buildings::isPenPasture(building) && !Buildings::isPitPond(building))
{
out << "Invalid building type. This is not a pen/pasture or pit/pond." << endl;
return CR_WRONG_USAGE;
}
df::building_civzonest * civz = (df::building_civzonest *) building;
for(size_t i = 0; i < civz->assigned_units.size(); i++)
{
df::unit* unit = df::unit::find(civz->assigned_units[i]);
if(unit)
Units::setNickname(unit, nick);
}
return CR_OK;
}
static command_result nickUnitsInCage(color_ostream& out, df::building* building, string nick)
{
// building must be a pen/pasture or pit
if(!isCage(building))
{
out << "Invalid building type. This is not a cage." << endl;
return CR_WRONG_USAGE;
}
df::building_cagest* cage = (df::building_cagest*) building;
for(size_t i=0; i<cage->assigned_units.size(); i++)
{
df::unit* unit = df::unit::find(cage->assigned_units[i]);
if(unit)
Units::setNickname(unit, nick);
}
return CR_OK;
}
static command_result nickUnitsInChain(color_ostream& out, df::building* building, string nick)
{
out << "sorry. nicknaming chained units is not possible yet." << endl;
return CR_WRONG_USAGE;
}
// give all units inside a pasture or cage the same nickname
// (usage example: protect them from being autobutchered)
static command_result nickUnitsInBuilding(color_ostream& out, df::building* building, string nick)
{
command_result result = CR_WRONG_USAGE;
if(Buildings::isActivityZone(building))
result = nickUnitsInZone(out, building, nick);
else if(isCage(building))
result = nickUnitsInCage(out, building, nick);
else if(isChain(building))
result = nickUnitsInChain(out, building, nick);
else
out << "Cannot nickname units in this type of building!" << endl;
return result;
}
// dump some zone info
static void zoneInfo(color_ostream & out, df::building* building, bool verbose)
{
if(!Buildings::isActivityZone(building))
return;
string name;
building->getName(&name);
out.print("Building %i - \"%s\" - type %s (%i)",
building->id,
name.c_str(),
ENUM_KEY_STR(building_type, building->getType()).c_str(),
building->getType());
out.print(", subtype %s (%i)",
ENUM_KEY_STR(civzone_type, (df::civzone_type)building->getSubtype()).c_str(),
building->getSubtype());
out.print("\n");
df::building_civzonest * civ = (df::building_civzonest *) building;
if(Buildings::isActive(civ))
out << "active";
else
out << "not active";
if(civ->zone_flags.bits.pen_pasture)
out << ", pen/pasture";
else if (civ->zone_flags.bits.pit_pond)
{
out << " (pit flags:" << civ->pit_flags.whole << ")";
if(civ->pit_flags.bits.is_pond)
out << ", pond";
else
out << ", pit";
}
out << endl;
out << "x1:" <<building->x1
<< " x2:" <<building->x2
<< " y1:" <<building->y1
<< " y2:" <<building->y2
<< " z:" <<building->z
<< endl;
size_t creaturecount = civ->assigned_units.size();
out << "Creatures in this zone: " << creaturecount << endl;
for(size_t c = 0; c < creaturecount; c++)
{
int32_t cindex = civ->assigned_units.at(c);
// print list of all units assigned to that zone
for(size_t i = 0; i < world->units.all.size(); i++)
{
df::unit * creature = world->units.all[i];
if(creature->id != cindex)
continue;
unitInfo(out, creature, verbose);
}
}
}
// dump some cage info
static void cageInfo(color_ostream & out, df::building* building, bool verbose)
{
if(!isCage(building))
return;
string name;
building->getName(&name);
out.print("Building %i - \"%s\" - type %s (%i)",
building->id,
name.c_str(),
ENUM_KEY_STR(building_type, building->getType()).c_str(),
building->getType());
out.print("\n");
out << "x:" << building->x1
<< " y:" << building->y1
<< " z:" << building->z
<< endl;
df::building_cagest * cage = (df::building_cagest*) building;
size_t creaturecount = cage->assigned_units.size();
out << "Creatures in this cage: " << creaturecount << endl;
for(size_t c = 0; c < creaturecount; c++)
{
int32_t cindex = cage->assigned_units.at(c);
// print list of all units assigned to that cage
for(size_t i = 0; i < world->units.all.size(); i++)
{
df::unit * creature = world->units.all[i];
if(creature->id != cindex)
continue;
unitInfo(out, creature, verbose);
}
}
}
// dump some chain/restraint info
static void chainInfo(color_ostream & out, df::building* building, bool list_refs = false)
{
if(!isChain(building))
return;
string name;
building->getName(&name);
out.print("Building %i - \"%s\" - type %s (%i)",
building->id,
name.c_str(),
ENUM_KEY_STR(building_type, building->getType()).c_str(),
building->getType());
out.print("\n");
df::building_chainst* chain = (df::building_chainst*) building;
if(chain->assigned)
{
out << "assigned: ";
unitInfo(out, chain->assigned, true);
}
if(chain->chained)
{
out << "chained: ";
unitInfo(out, chain->chained, true);
}
}
static df::building* getAssignableBuildingAtCursor(color_ostream& out)
{
// set building at cursor position to be new target building
if (cursor->x == -30000)
{
out.printerr("No cursor; place cursor over activity zone, pen,"
" pasture, pit, pond, chain, or cage.\n");
return NULL;
}
auto building_at_tile = Buildings::findAtTile(Gui::getCursorPos());
// cagezone wants a pen/pit as starting point
if (isCage(building_at_tile))
{
out << "Target building type: cage." << endl;
return building_at_tile;
}
else
{
auto zone_at_tile = Buildings::findPenPitAt(Gui::getCursorPos());
if(!zone_at_tile)
{
out << "No pen/pasture, pit, or cage under cursor!" << endl;
return NULL;
}
else
{
out << "Target building type: pen/pasture or pit." << endl;
return zone_at_tile;
}
}
}
// ZONE FILTERS (as in, filters used by 'zone')
// Maps parameter names to filters.
static unordered_map<string, function<bool(df::unit*)>> zone_filters;
static struct zone_filters_init { zone_filters_init() {
zone_filters["caged"] = isContainedInItem;
zone_filters["egglayer"] = Units::isEggLayer;
zone_filters["female"] = Units::isFemale;
zone_filters["grazer"] = Units::isGrazer;
zone_filters["hunting"] = Units::isHunter;
zone_filters["male"] = Units::isMale;
zone_filters["milkable"] = Units::isMilkable;
zone_filters["naked"] = Units::isNaked;
zone_filters["own"] = Units::isOwnCiv;
zone_filters["tamable"] = Units::isTamable;
zone_filters["tame"] = Units::isTame;
zone_filters["trainablewar"] = [](df::unit *unit) -> bool
{
return !Units::isWar(unit) && !Units::isHunter(unit) && Units::isTrainableWar(unit);
};
zone_filters["trainablehunt"] = [](df::unit *unit) -> bool
{
return !Units::isWar(unit) && !Units::isHunter(unit) && Units::isTrainableHunting(unit);
};
zone_filters["trained"] = Units::isTrained;
// backwards compatibility
zone_filters["unassigned"] = [](df::unit *unit) -> bool
{
return !isAssigned(unit);
};
zone_filters["war"] = Units::isWar;
}} zone_filters_init_;
// Extra annotations / descriptions for parameter names.
static unordered_map<string, string> zone_filter_notes;
static struct zone_filter_notes_init { zone_filter_notes_init() {
zone_filter_notes["caged"] = "caged (ignores built cages)";
zone_filter_notes["hunting"] = "trained hunting creature";
zone_filter_notes["named"] = "has name or nickname";
zone_filter_notes["own"] = "own civilization";
zone_filter_notes["trainablehunt"] = "trainable for hunting";
zone_filter_notes["trainablewar"] = "trainable for war";
zone_filter_notes["war"] = "trained war creature";
}} zone_filter_notes_init_;
static pair<string, function<bool(df::unit*)>> createRaceFilter(vector<string> &filter_args)
{
// guaranteed to exist.
string race = filter_args[0];
return make_pair(
"race of " + race,
[race](df::unit *unit) -> bool {
return Units::getRaceName(unit) == race;
}
);
}
static pair<string, function<bool(df::unit*)>> createAgeFilter(vector<string> &filter_args)
{
int target_age;
stringstream ss(filter_args[0]);
ss >> target_age;
if (ss.fail()) {
ostringstream err;
err << "Invalid age: " << filter_args[0] << "; age must be a number!";
throw runtime_error(err.str());
}
if (target_age < 0) {
ostringstream err;
err << "Invalid age: " << target_age << "; age must be >= 0!";
throw runtime_error(err.str());
}
return make_pair(
"age of exactly " + int_to_string(target_age),
[target_age](df::unit *unit) -> bool {
return Units::getAge(unit, true) == target_age;
}
);
}
static pair<string, function<bool(df::unit*)>> createMinAgeFilter(vector<string> &filter_args)
{
double min_age;
stringstream ss(filter_args[0]);
ss >> min_age;
if (ss.fail()) {
ostringstream err;
err << "Invalid minimum age: " << filter_args[0] << "; age must be a number!";
throw runtime_error(err.str());
}
if (min_age < 0) {
ostringstream err;
err << "Invalid minimum age: " << min_age << "; age must be >= 0!";
throw runtime_error(err.str());
}
return make_pair(
"minimum age of " + int_to_string(min_age),
[min_age](df::unit *unit) -> bool {
return Units::getAge(unit, true) >= min_age;
}
);
}
static pair<string, function<bool(df::unit*)>> createMaxAgeFilter(vector<string> &filter_args)
{
double max_age;
stringstream ss(filter_args[0]);
ss >> max_age;
if (ss.fail()) {
ostringstream err;
err << "Invalid maximum age: " << filter_args[0] << "; age must be a number!";
throw runtime_error(err.str());
}