-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathtiletypes.cpp
1519 lines (1357 loc) · 49 KB
/
tiletypes.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
// Plugin tiletypes
//
// This plugin allows fine editing of individual game tiles (expect for
// changing material subtypes).
//
// Commands:
// tiletypes - runs the interractive interpreter
// tiletypes-command - run the given command
// (intended to be mapped to a hotkey or used from dfhack-run)
// tiletypes-here - runs the execute method with the last settings from
// tiletypes(-command), including brush!
// (intended to be mapped to a hotkey)
// tiletypes-here-point - runs the execute method with the last settings from
// tiletypes(-command), except with a point brush!
// (intended to be mapped to a hotkey)
// Options (everything but tiletypes-command):
// ?, help - print some help
//
// Options (tiletypes-command):
// (anything) - run the given command
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using std::vector;
using std::string;
using std::endl;
using std::set;
#include "Console.h"
#include "DataDefs.h"
#include "DataIdentity.h"
#include "Export.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "PluginLua.h"
#include "TileTypes.h"
#include "modules/Gui.h"
#include "modules/MapCache.h"
#include "modules/Maps.h"
#include "df/tile_dig_designation.h"
#include "df/world.h"
#include "Brushes.h"
using namespace MapExtras;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("tiletypes");
REQUIRE_GLOBAL(world);
struct tiletypes_options {
// whether to display help
bool help = false;
bool quiet = false;
// if set, then use this position instead of the active game cursor
df::coord cursor;
static struct_identity _identity;
};
static const struct_field_info tiletypes_options_fields[] = {
{ struct_field_info::PRIMITIVE, "help", offsetof(tiletypes_options, help), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "quiet", offsetof(tiletypes_options, quiet), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::SUBSTRUCT, "cursor", offsetof(tiletypes_options, cursor), &df::coord::_identity, 0, 0 },
{ struct_field_info::END }
};
struct_identity tiletypes_options::_identity(sizeof(tiletypes_options), &df::allocator_fn<tiletypes_options>, NULL, "tiletypes_options", NULL, tiletypes_options_fields);
// pair<bottomShape, topShape> -> newTopShape
static const std::map<std::pair<df::tiletype_shape, df::tiletype_shape>, df::tiletype_shape> autocorrectMap = {
{ { df::tiletype_shape::WALL, df::tiletype_shape::EMPTY }, df::tiletype_shape::FLOOR },
{ { df::tiletype_shape::RAMP, df::tiletype_shape::EMPTY }, df::tiletype_shape::RAMP_TOP },
{ { df::tiletype_shape::EMPTY, df::tiletype_shape::RAMP_TOP }, df::tiletype_shape::EMPTY },
};
static const uint16_t UNDERGROUND_TEMP = 10015;
static const char * HISTORY_FILE = "dfhack-config/tiletypes.history";
CommandHistory tiletypes_hist;
command_result df_tiletypes (color_ostream &out, vector <string> & parameters);
command_result df_tiletypes_command (color_ostream &out, vector <string> & parameters);
command_result df_tiletypes_here (color_ostream &out, vector <string> & parameters);
command_result df_tiletypes_here_point (color_ostream &out, vector <string> & parameters);
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
tiletypes_hist.load(HISTORY_FILE);
commands.push_back(PluginCommand("tiletypes", "Paints tiles of specified types onto the map.", df_tiletypes, true, true));
commands.push_back(PluginCommand("tiletypes-command", "Run tiletypes commands (seperated by ' ; ')", df_tiletypes_command));
commands.push_back(PluginCommand("tiletypes-here", "Repeat tiletypes command at cursor (with brush)", df_tiletypes_here));
commands.push_back(PluginCommand("tiletypes-here-point", "Repeat tiletypes command at cursor (with single tile brush)", df_tiletypes_here_point));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
tiletypes_hist.save(HISTORY_FILE);
return CR_OK;
}
void help( color_ostream & out, std::vector<std::string> &commands, int start, int end)
{
std::string option = commands.size() > size_t(start) ? commands[start] : "";
if (option.empty())
{
out << "Commands:" << std::endl
<< " quit / q : quit" << std::endl
<< " filter / f [options] : change filter options" << std::endl
<< " paint / p [options] : change paint options" << std::endl
<< " point / p : set point brush" << std::endl
<< " range / r [w] [h] [z] : set range brush" << std::endl
<< " block : set block brush" << std::endl
<< " column : set column brush" << std::endl
<< " run / (empty) : paint!" << std::endl
<< std::endl
<< "Filter/paint options:" << std::endl
<< " Any: reset to default (no filter/paint)" << std::endl
<< " Shape / sh / s: set tile shape information" << std::endl
<< " Material / mat / m: set tile material information" << std::endl
<< " Special / sp: set special tile information" << std::endl
<< " Variant / var / v: set variant tile information" << std::endl
<< " All / a: set the four above at the same time (no ANY support)" << std::endl
<< " Designated / d: set designated flag (Only for filters)" << std::endl
<< " Hidden / h: set hidden flag" << std::endl
<< " Light / l: set light flag" << std::endl
<< " Subterranean / st: set subterranean flag" << std::endl
<< " Skyview / sv: set skyview flag" << std::endl
<< " Aquifer / aqua: set aquifer flag" << std::endl
<< " Autocorrect / ac: set autocorrect flag" << std::endl
<< " Stone: paint specific stone material" << std::endl
<< " Veintype: use specific vein type for stone" << std::endl
<< "See help [option] for more information" << std::endl;
}
else if (option == "shape" || option == "s" ||option == "sh")
{
out << "Available shapes:" << std::endl
<< " ANY" << std::endl;
FOR_ENUM_ITEMS(tiletype_shape,i)
{
out << " " << ENUM_KEY_STR(tiletype_shape,i) << std::endl;
}
}
else if (option == "material"|| option == "mat" ||option == "m")
{
out << "Available materials:" << std::endl
<< " ANY" << std::endl;
FOR_ENUM_ITEMS(tiletype_material,i)
{
out << " " << ENUM_KEY_STR(tiletype_material,i) << std::endl;
}
}
else if (option == "special" || option == "sp")
{
out << "Available specials:" << std::endl
<< " ANY" << std::endl;
FOR_ENUM_ITEMS(tiletype_special,i)
{
out << " " << ENUM_KEY_STR(tiletype_special,i) << std::endl;
}
}
else if (option == "variant" || option == "var" || option == "v")
{
out << "Available variants:" << std::endl
<< " ANY" << std::endl;
FOR_ENUM_ITEMS(tiletype_variant,i)
{
out << " " << ENUM_KEY_STR(tiletype_variant,i) << std::endl;
}
}
else if (option == "designated" || option == "d")
{
out << "Available designated flags (Only for filters):" << std::endl
<< " ANY, 0, 1" << std::endl;
}
else if (option == "hidden" || option == "h")
{
out << "Available hidden flags:" << std::endl
<< " ANY, 0, 1" << std::endl;
}
else if (option == "light" || option == "l")
{
out << "Available light flags:" << std::endl
<< " ANY, 0, 1" << std::endl;
}
else if (option == "subterranean" || option == "st")
{
out << "Available subterranean flags:" << std::endl
<< " ANY, 0, 1" << std::endl;
}
else if (option == "skyview" || option == "sv")
{
out << "Available skyview flags:" << std::endl
<< " ANY, 0, 1" << std::endl;
}
else if (option == "aquifer" || option == "aqua")
{
out << "Available aquifer flags:" << std::endl
<< " ANY, 0, 1, 2" << std::endl;
}
else if (option == "autocorrect" || option == "ac")
{
out << "Available autocorrect flags:" << std::endl
<< " 0, 1" << std::endl;
}
else if (option == "stone")
{
out << "The stone option allows painting any specific stone material." << std::endl
<< "The normal 'material' option is forced to STONE, and cannot" << std::endl
<< "be changed without cancelling the specific stone selection." << std::endl
<< "Note: this feature paints under ice and constructions," << std::endl
<< "instead of replacing them with brute force." << std::endl;
}
else if (option == "veintype")
{
out << "Specifies which vein type to use when painting specific stone." << std::endl
<< "The vein type determines stone drop rate. Available types:" << std::endl;
FOR_ENUM_ITEMS(inclusion_type,i)
out << " " << ENUM_KEY_STR(inclusion_type,i) << std::endl;
out << "Vein type other than CLUSTER forces creation of a vein." << std::endl;
}
}
struct TileType
{
df::tiletype_shape shape;
df::tiletype_material material;
df::tiletype_special special;
df::tiletype_variant variant;
int dig;
int hidden;
int light;
int subterranean;
int skyview;
int aquifer;
int autocorrect;
int stone_material;
df::inclusion_type vein_type;
TileType()
{
clear();
}
void clear()
{
shape = tiletype_shape::NONE;
material = tiletype_material::NONE;
special = tiletype_special::NONE;
variant = tiletype_variant::NONE;
dig = -1;
hidden = -1;
light = -1;
subterranean = -1;
skyview = -1;
aquifer = -1;
autocorrect = 0;
stone_material = -1;
vein_type = inclusion_type::CLUSTER;
}
bool empty()
{
return shape == -1 && material == -1 && special == -1 && variant == -1
&& dig == -1 && hidden == -1 && light == -1 && subterranean == -1
&& skyview == -1 && aquifer == -1 && autocorrect == 0
&& stone_material == -1;
}
inline bool matches(const df::tiletype source,
const df::tile_designation des,
const df::tile_occupancy occ,
const t_matpair mat) const
{
bool rv = true;
rv &= (shape == -1 || shape == tileShape(source));
if (stone_material >= 0)
rv &= isStoneMaterial(source) && mat.mat_type == 0 && mat.mat_index == stone_material;
else
rv &= (material == -1 || material == tileMaterial(source));
rv &= (special == -1 || special == tileSpecial(source));
rv &= (variant == -1 || variant == tileVariant(source));
rv &= (dig == -1 || (dig != 0) == (des.bits.dig != tile_dig_designation::No));
rv &= (hidden == -1 || (hidden != 0) == des.bits.hidden);
rv &= (light == -1 || (light != 0) == des.bits.light);
rv &= (subterranean == -1 || (subterranean != 0) == des.bits.subterranean);
rv &= (skyview == -1 || (skyview != 0) == des.bits.outside);
rv &= (aquifer == -1 || (aquifer != 0) == des.bits.water_table);
rv &= (aquifer == -1 || (aquifer == 2) == occ.bits.heavy_aquifer);
return rv;
}
};
struct PaintResult {
int paintCount = 0;
std::function<void(MapExtras::MapCache&)> postWrite = [](MapExtras::MapCache& map) {};
};
std::ostream &operator<<(std::ostream &stream, const TileType &paint)
{
bool used = false;
bool needSpace = false;
if (paint.special >= 0)
{
stream << ENUM_KEY_STR(tiletype_special,paint.special);
used = true;
needSpace = true;
}
if (paint.material >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << ENUM_KEY_STR(tiletype_material,paint.material);
used = true;
needSpace = true;
}
if (paint.shape >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << ENUM_KEY_STR(tiletype_shape,paint.shape);
used = true;
needSpace = true;
}
if (paint.variant >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << ENUM_KEY_STR(tiletype_variant,paint.variant);
used = true;
needSpace = true;
}
if (paint.dig >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << (paint.dig ? "DESIGNATED" : "UNDESIGNATED");
used = true;
needSpace = true;
}
if (paint.hidden >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << (paint.hidden ? "HIDDEN" : "VISIBLE");
used = true;
needSpace = true;
}
if (paint.light >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << (paint.light ? "LIGHT" : "DARK");
used = true;
needSpace = true;
}
if (paint.subterranean >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << (paint.subterranean ? "SUBTERRANEAN" : "ABOVE GROUND");
used = true;
needSpace = true;
}
if (paint.skyview >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << (paint.skyview ? "OUTSIDE" : "INSIDE");
used = true;
needSpace = true;
}
if (paint.aquifer >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << (paint.aquifer ? (paint.aquifer == 1 ? "LIGHT AQUIFER" : "HEAVY AQUIFER") : "NO AQUIFER");
used = true;
needSpace = true;
}
if (paint.autocorrect == 1)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << "AUTOCORRECT";
used = true;
needSpace = true;
}
if (paint.stone_material >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << MaterialInfo(0,paint.stone_material).getToken()
<< " " << ENUM_KEY_STR(inclusion_type, paint.vein_type);
used = true;
needSpace = true;
}
if (!used)
{
stream << "any";
}
return stream;
}
static TileType filter, paint;
static Brush *brush = new RectangleBrush(1,1);
void printState(color_ostream &out)
{
out << "Filter: " << filter << std::endl
<< "Paint: " << paint << std::endl
<< "Brush: " << brush << std::endl;
}
//zilpin: These two functions were giving me compile errors in VS2008, so I cheated with the C style loop below, just to get it to build.
//Original code is commented out.
void tolower(std::string &str)
{
//The C++ way...
//std::transform(str.begin(), str.end(), str.begin(), std::bind2nd(std::ptr_fun(&std::tolower<char> ), std::locale("")));
//The C way...
for(char *c=(char *)str.c_str(); *c; ++c)
{
*c = tolower(*c);
}
}
void toupper(std::string &str)
{
//std::transform(str.begin(), str.end(), str.begin(), std::bind2nd(std::ptr_fun(&std::toupper<char>), std::locale("")));
for(char *c=(char *)str.c_str(); *c; ++c)
{
*c = toupper(*c);
}
}
int toint(const std::string &str, int failValue = 0)
{
std::istringstream ss(str);
int valInt;
ss >> valInt;
if (ss.fail())
{
return failValue;
}
return valInt;
}
bool tryShape(std::string value, TileType &paint)
{
FOR_ENUM_ITEMS(tiletype_shape,i)
{
if (value == ENUM_KEY_STR(tiletype_shape,i))
{
paint.shape = i;
return true;
}
}
return false;
}
bool tryMaterial(std::string value, TileType &paint)
{
FOR_ENUM_ITEMS(tiletype_material, i)
{
if (value == ENUM_KEY_STR(tiletype_material,i))
{
paint.material = i;
return true;
}
}
return false;
}
bool trySpecial(std::string value, TileType &paint)
{
FOR_ENUM_ITEMS(tiletype_special, i)
{
if (value == ENUM_KEY_STR(tiletype_special,i))
{
paint.special = i;
return true;
}
}
return false;
}
bool tryVariant(std::string value, TileType &paint)
{
FOR_ENUM_ITEMS(tiletype_variant, i)
{
if (value == ENUM_KEY_STR(tiletype_variant,i))
{
paint.variant = i;
return true;
}
}
return false;
}
bool processTileType(color_ostream & out, TileType &paint, std::vector<std::string> ¶ms, int start, int end, bool isFilter)
{
if (start == end)
{
out << "Missing argument." << std::endl;
return false;
}
int loc = start;
std::string option = params[loc++];
std::string value = end <= loc ? "" : params[loc++];
tolower(option);
toupper(value);
int valInt;
if (value == "ANY")
{
valInt = -1;
}
else
{
valInt = toint(value, -2);
}
bool found = false;
if (option == "any")
{
paint.clear();
}
else if (option == "shape" || option == "sh" || option == "s")
{
if (is_valid_enum_item((df::tiletype_shape)valInt))
{
paint.shape = (df::tiletype_shape)valInt;
found = true;
}
else
{
if (!tryShape(value, paint))
{
out << "Unknown tile shape: " << value << std::endl;
}
}
}
else if (option == "material" || option == "mat" || option == "m")
{
// Setting the material conflicts with stone_material
paint.stone_material = -1;
if (is_valid_enum_item((df::tiletype_material)valInt))
{
paint.material = (df::tiletype_material)valInt;
found = true;
}
else
{
if (!tryMaterial(value, paint))
{
out << "Unknown tile material: " << value << std::endl;
}
}
}
else if (option == "special" || option == "sp")
{
if (is_valid_enum_item((df::tiletype_special)valInt))
{
paint.special = (df::tiletype_special)valInt;
found = true;
}
else
{
if (!trySpecial(value, paint))
{
out << "Unknown tile special: " << value << std::endl;
}
}
}
else if (option == "variant" || option == "var" || option == "v")
{
if (is_valid_enum_item((df::tiletype_variant)valInt))
{
paint.variant = (df::tiletype_variant)valInt;
found = true;
}
else
{
if (!tryVariant(value, paint))
{
out << "Unknown tile variant: " << value << std::endl;
}
}
}
else if (option == "designated" || option == "d")
{
if (isFilter)
{
if (valInt >= -1 && valInt < 2)
{
paint.dig = valInt;
found = true;
}
else
{
out << "Unknown designation flag: " << value << std::endl;
}
}
else
{
out << "Option only available in filters: '" << option << "'" << std::endl;
}
}
else if (option == "hidden" || option == "h")
{
if (valInt >= -1 && valInt < 2)
{
paint.hidden = valInt;
found = true;
}
else
{
out << "Unknown hidden flag: " << value << std::endl;
}
}
else if (option == "light" || option == "l")
{
if (valInt >= -1 && valInt < 2)
{
paint.light = valInt;
found = true;
}
else
{
out << "Unknown light flag: " << value << std::endl;
}
}
else if (option == "subterranean" || option == "st")
{
if (valInt >= -1 && valInt < 2)
{
paint.subterranean = valInt;
found = true;
}
else
{
out << "Unknown subterranean flag: " << value << std::endl;
}
}
else if (option == "skyview" || option == "sv")
{
if (valInt >= -1 && valInt < 2)
{
paint.skyview = valInt;
found = true;
}
else
{
out << "Unknown skyview flag: " << value << std::endl;
}
}
else if (option == "aquifer" || option == "aqua")
{
if (valInt >= -1 && valInt < 3)
{
paint.aquifer = valInt;
found = true;
}
else
{
out << "Unknown aquifer flag: " << value << std::endl;
}
}
else if (option == "autocorrect" || option == "ac")
{
if (valInt >= 0 && valInt < 2)
{
paint.autocorrect = valInt;
found = true;
}
else
{
out << "Unknown autocorrect flag: " << value << std::endl;
}
}
else if (option == "all" || option == "a")
{
loc--;
for (; loc < end; loc++)
{
std::string param = params[loc];
toupper(param);
if (!(tryShape(param, paint) || tryMaterial(param, paint) ||
trySpecial(param, paint) || tryVariant(param, paint)))
{
out << "Unknown description: '" << param << "'" << std::endl;
break;
}
}
found = true;
}
else if (option == "stone")
{
MaterialInfo mat;
if (!mat.findInorganic(value))
out << "Unknown inorganic material: " << value << std::endl;
else if (!isStoneInorganic(mat.index))
out << "Not a stone material: " << value << std::endl;
else
{
paint.material = tiletype_material::STONE;
paint.stone_material = mat.index;
}
}
else if (option == "veintype")
{
if (!find_enum_item(&paint.vein_type, value))
out << "Unknown vein type: " << value << std::endl;
}
else
{
out << "Unknown option: '" << option << "'" << std::endl;
}
return found;
}
static bool paintTileProcessing(MapExtras::Block* block, const df::coord2d& blockPos, const TileType& target) {
df::tiletype source = block->tiletypeAt(blockPos);
df::tile_designation des = block->DesignationAt(blockPos);
// Stone painting operates on the base layer
if (target.stone_material >= 0)
source = block->baseTiletypeAt(blockPos);
df::tiletype_shape shape = target.shape;
if (shape == tiletype_shape::NONE)
shape = tileShape(source);
df::tiletype_material material = target.material;
if (material == tiletype_material::NONE)
material = tileMaterial(source);
df::tiletype_special special = target.special;
if (special == tiletype_special::NONE)
special = tileSpecial(source);
df::tiletype_variant variant = target.variant;
/*
* FIXME: variant should be:
* 1. If user variant:
* 2. If user variant \belongs target variants
* 3. use user variant
* 4. Else
* 5. use variant 0
* 6. If the source variant \belongs target variants
* 7 use source variant
* 8 ElseIf num target shape/material variants > 1
* 9. pick one randomly
* 10.Else
* 11. use variant 0
*
* The following variant check has been disabled because it's severely limiting
* the usefullness of the tool.
*/
/*
if (variant == tiletype_variant::NONE)
{
variant = tileVariant(source);
}
*/
// Remove direction from directionless tiles
DFHack::TileDirection direction = tileDirection(source);
if (!(material == tiletype_material::RIVER || shape == tiletype_shape::BROOK_BED || special == tiletype_special::TRACK || (shape == tiletype_shape::WALL && (material == tiletype_material::CONSTRUCTION || special == tiletype_special::SMOOTH))))
direction.whole = 0;
df::tiletype type = DFHack::findTileType(shape, material, variant, special, direction);
// hack for empty space
if (shape == tiletype_shape::EMPTY && material == tiletype_material::AIR && variant == tiletype_variant::VAR_1 && special == tiletype_special::NORMAL && direction.whole == 0)
type = tiletype::OpenSpace;
// make sure it's not invalid
if (type != tiletype::Void) {
if (target.stone_material >= 0) {
if (!block->setStoneAt(blockPos, type, target.stone_material, target.vein_type, true, true))
return false;
}
else
block->setTiletypeAt(blockPos, type);
}
if (target.hidden > -1)
des.bits.hidden = target.hidden;
if (target.light > -1)
des.bits.light = target.light;
if (target.subterranean > -1) {
des.bits.subterranean = target.subterranean;
// Underground tiles have a fixed ambient temperature, though magma and fire can raise it
if (target.subterranean == 1 && block->temperature1At(blockPos) < UNDERGROUND_TEMP) {
block->setTemp1At(blockPos, UNDERGROUND_TEMP);
block->setTemp2At(blockPos, UNDERGROUND_TEMP);
}
}
if (target.skyview > -1)
des.bits.outside = target.skyview;
// Remove liquid from walls, etc
if (type != (df::tiletype)-1 && !DFHack::FlowPassable(type))
{
des.bits.flow_size = 0;
//des.bits.liquid_type = DFHack::liquid_water;
//des.bits.water_table = 0;
des.bits.flow_forbid = 0;
//des.bits.liquid_static = 0;
//des.bits.water_stagnant = 0;
//des.bits.water_salt = 0;
}
return block->setDesignationAt(blockPos, des);
}
static bool autocorrectTile(MapExtras::Block* botBlock, MapExtras::Block* topBlock, const df::coord2d& blockPos, const TileType& target) {
if (!botBlock || !topBlock)
return false;
tiletype::tiletype botTiletype = botBlock->tiletypeAt(blockPos);
tiletype::tiletype topTiletype = topBlock->tiletypeAt(blockPos);
auto iter = autocorrectMap.find(std::make_pair(tileShape(botTiletype), tileShape(topTiletype)));
if (iter == autocorrectMap.end())
return false;
df::tiletype_shape newTopShape = iter->second;
TileType tiletype = TileType(target);
tiletype.shape = newTopShape;
tiletype.material = tiletype_material::NONE;
tiletype.variant = tiletype_variant::NONE;
tiletype.special = tiletype_special::NONE;
tiletype.stone_material = -1;
tiletype.aquifer = -1;
tiletype.dig = -1;
tiletype.autocorrect = 0;
if (paintTileProcessing(topBlock, blockPos, tiletype) && tileShape(topBlock->tiletypeAt(blockPos)) == newTopShape)
return true;
tiletype_material::tiletype_material topMat = tileMaterial(topTiletype);
tiletype_material::tiletype_material botMat = tileMaterial(botTiletype);
topMat = topMat == tiletype_material::NONE ? botMat : topMat;
tiletype_variant::tiletype_variant topVariant = tileVariant(topTiletype);
tiletype_variant::tiletype_variant botVariant = tileVariant(botTiletype);
topVariant = topVariant == tiletype_variant::NONE ? botVariant : topVariant;
tiletype_special::tiletype_special topSpecial = tileSpecial(topTiletype);
tiletype_special::tiletype_special botSpecial = tileSpecial(botTiletype);
topSpecial = topSpecial == tiletype_special::NONE ? botSpecial : topSpecial;
botSpecial = botSpecial == tiletype_special::NONE ? tiletype_special::NORMAL : botSpecial;
int topLikeness = 0;
for (df::tiletype tt = (df::enum_traits<df::tiletype>::first_item); DFHack::is_valid_enum_item(tt); tt = DFHack::next_enum_item(tt, false))
{
if (newTopShape != tileShape(tt))
continue;
int tempLikeness = 0;
tiletype_material::tiletype_material mat = tileMaterial(tt);
if (mat == topMat)
tempLikeness += 16;
else if (mat == botMat)
tempLikeness += 8;
else continue;
tiletype_variant::tiletype_variant variant = tileVariant(tt);
if (variant == topVariant)
tempLikeness += 2;
else if (variant == botVariant)
tempLikeness += 1;
tiletype_special::tiletype_special special = tileSpecial(tt);
if (special == topSpecial)
tempLikeness += 8;
else if (special == botSpecial)
tempLikeness += 4;
if (tempLikeness > topLikeness) {
topLikeness = tempLikeness;
tiletype.material = mat;
tiletype.variant = variant;
tiletype.special = special;
}
}
return paintTileProcessing(topBlock, blockPos, tiletype);
}
static bool autocorrectArea(MapExtras::MapCache& map, const df::coord& pos1, const df::coord& pos2, const TileType& botType) {
df::coord minPos = df::coord(std::min(pos1.x, pos2.x), std::min(pos1.y, pos2.y), std::min(pos1.z, pos2.z));
df::coord maxPos = df::coord(std::max(pos1.x, pos2.x), std::max(pos1.y, pos2.y), std::max(pos1.z, pos2.z));
bool updated = false;
// Loop through the affected blocks
for (int16_t blockX = (minPos.x >> 4) << 4; blockX <= maxPos.x; blockX += 16) {
for (int16_t blockY = (minPos.y >> 4) << 4; blockY <= maxPos.y; blockY += 16) {
std::queue<MapExtras::Block*> blockQueue = std::queue<MapExtras::Block*>();
blockQueue.push(map.BlockAtTile(df::coord(blockX, blockY, minPos.z - 1)));
blockQueue.push(map.BlockAtTile(df::coord(blockX, blockY, minPos.z)));
for (int16_t z = minPos.z; z <= maxPos.z; z++) {
blockQueue.push(map.BlockAtTile(df::coord(blockX, blockY, minPos.z + 1)));
MapExtras::Block* belowBlock = blockQueue.front();
blockQueue.pop();
int16_t startX = std::max(minPos.x - blockX, 0);
int16_t startY = std::max(minPos.y - blockY, 0);
int16_t endX = std::min(maxPos.x - blockX, 15);
int16_t endY = std::min(maxPos.y - blockY, 15);
// Loop through all tiles in the block
for (int16_t xOffset = startX; xOffset <= endX; xOffset++) {
for (int16_t yOffset = startY; yOffset <= endY; yOffset++) {
updated |= autocorrectTile(blockQueue.front(), blockQueue.back(), df::coord2d(xOffset, yOffset), botType);
updated |= autocorrectTile(belowBlock, blockQueue.front(), df::coord2d(xOffset, yOffset), botType);
}
}
blockQueue.front()->enableBlockUpdates(true, true);
}
}
}
return updated;
}
static PaintResult paintArea(MapExtras::MapCache& map, const df::coord& pos1, const df::coord& pos2,
const TileType& target, const TileType& match = TileType()) {