-
Notifications
You must be signed in to change notification settings - Fork 477
/
dig.cpp
2526 lines (2304 loc) · 85.7 KB
/
dig.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 "Debug.h"
#include "LuaTools.h"
#include "MemAccess.h"
#include "PluginManager.h"
#include "modules/EventManager.h"
#include "modules/Gui.h"
#include "modules/MapCache.h"
#include "modules/Job.h"
#include "modules/Screen.h"
#include "modules/Textures.h"
#include "modules/World.h"
#include "df/block_square_event_designation_priorityst.h"
#include "df/gamest.h"
#include "df/job_list_link.h"
#include "df/map_block.h"
#include "df/world.h"
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <string>
#include <cmath>
#include <memory>
using std::vector;
using std::string;
using std::stack;
using namespace DFHack;
using namespace df::enums;
command_result digv (color_ostream &out, vector <string> & parameters);
command_result digvx (color_ostream &out, vector <string> & parameters);
command_result digl (color_ostream &out, vector <string> & parameters);
command_result diglx (color_ostream &out, vector <string> & parameters);
command_result digexp (color_ostream &out, vector <string> & parameters);
command_result digcircle (color_ostream &out, vector <string> & parameters);
command_result digtype (color_ostream &out, vector <string> & parameters);
DFHACK_PLUGIN("dig");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(game);
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(window_x);
REQUIRE_GLOBAL(window_y);
REQUIRE_GLOBAL(window_z);
namespace DFHack {
DBG_DECLARE(dig, log, DebugCategory::LINFO);
}
static const string WARM_CONFIG_KEY = string(plugin_name) + "/warmdig";
static const string DAMP_CONFIG_KEY = string(plugin_name) + "/dampdig";
static PersistentDataItem warm_config, damp_config;
static void unhide_surrounding_tagged_tiles(color_ostream& out, void* ptr);
static const int32_t CYCLE_TICKS = 1223; // a prime number that's about a day
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
static vector<TexposHandle> textures;
static bool is_painting_warm = false;
static bool is_painting_damp = false;
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands) {
textures = Textures::loadTileset("hack/data/art/damp_dig_map.png", 32, 32, true);
commands.push_back(PluginCommand(
"digv",
"Dig a whole vein.",
digv,
Gui::cursor_hotkey));
commands.push_back(PluginCommand(
"digvx",
"Dig a whole vein, following through z-levels.",
digvx,
Gui::cursor_hotkey));
commands.push_back(PluginCommand(
"digl",
"Dig layerstone.",
digl,
Gui::cursor_hotkey));
commands.push_back(PluginCommand(
"diglx",
"Dig layerstone, following through z-levels.",
diglx,
Gui::cursor_hotkey));
commands.push_back(PluginCommand(
"digexp",
"Select or designate an exploratory pattern.",
digexp));
commands.push_back(PluginCommand(
"digcircle",
"Dig designate a circle (filled or hollow)",
digcircle));
commands.push_back(PluginCommand(
"digtype",
"Dig all veins of a given type.",
digtype,Gui::cursor_hotkey));
return CR_OK;
}
static void do_enable(bool enable) {
if (enable != is_enabled) {
is_enabled = enable;
DEBUG(log).print("%s\n", is_enabled ? "enabled" : "disabled");
if (enable) {
EventManager::registerListener(EventManager::EventType::JOB_STARTED,
EventManager::EventHandler(plugin_self, unhide_surrounding_tagged_tiles, 0));
} else {
EventManager::unregisterAll(plugin_self);
}
}
else {
DEBUG(log).print("%s, but already %s; no action\n",
is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled");
}
}
DFhackCExport command_result plugin_load_site_data(color_ostream &out) {
cycle_timestamp = 0;
is_painting_warm = false;
is_painting_damp = false;
warm_config = World::GetPersistentSiteData(WARM_CONFIG_KEY);
damp_config = World::GetPersistentSiteData(DAMP_CONFIG_KEY);
if (!warm_config.isValid()) {
DEBUG(log,out).print("no warm dig config found in this save; initializing\n");
warm_config = World::AddPersistentSiteData(WARM_CONFIG_KEY);
}
if (!damp_config.isValid()) {
DEBUG(log,out).print("no damp dig config found in this save; initializing\n");
damp_config = World::AddPersistentSiteData(DAMP_CONFIG_KEY);
}
for (auto & block : world->map.map_blocks) {
auto warm_mask = World::getPersistentTilemask(warm_config, block);
auto damp_mask = World::getPersistentTilemask(damp_config, block);
if (warm_mask || damp_mask) {
do_enable(true);
break;
}
}
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_WORLD_UNLOADED) {
is_painting_warm = false;
is_painting_damp = false;
do_enable(false);
}
return CR_OK;
}
static void fill_dig_jobs(std::unordered_map<df::coord, df::job *> &dig_jobs) {
df::job_list_link *link = world->jobs.list.next;
for (; link; link = link->next) {
auto job = link->item;
auto type = ENUM_ATTR(job_type, type, job->job_type);
if (type != job_type_class::Digging)
continue;
dig_jobs.emplace(job->pos, job);
}
}
// scrub no-longer designated tiles
static void do_cycle(color_ostream &out) {
cycle_timestamp = world->frame_counter;
std::unordered_map<df::coord, df::job *> dig_jobs;
fill_dig_jobs(dig_jobs);
bool has_assignment = false;
uint32_t scrubbed = 0;
for (auto & block : world->map.map_blocks) {
auto warm_mask = World::getPersistentTilemask(warm_config, block);
auto damp_mask = World::getPersistentTilemask(damp_config, block);
if (!warm_mask && !damp_mask)
continue;
bool used = false;
const auto & block_pos = block->map_pos;
for (uint32_t x = 0; x < 16; x++) for (uint32_t y = 0; y < 16; y++) {
if ((!warm_mask || !warm_mask->getassignment(x, y)) &&
(!damp_mask || !damp_mask->getassignment(x, y)))
{
continue;
}
if (!block->designation[x][y].bits.dig &&
!dig_jobs.contains(block_pos + df::coord(x, y, 0)))
{
if (warm_mask) warm_mask->setassignment(x, y, false);
if (damp_mask) damp_mask->setassignment(x, y, false);
++scrubbed;
} else {
has_assignment = true;
used = true;
}
}
if (!used) {
// delete the tile masks so we can skip over this block in the future
World::deletePersistentTilemask(warm_config, block);
World::deletePersistentTilemask(damp_config, block);
}
}
DEBUG(log,out).print("scrubbed %u tagged tiles\n", scrubbed);
if (!has_assignment){
DEBUG(log,out).print("no more active tagged tiles; disabling\n");
do_enable(false);
}
}
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (world->frame_counter - cycle_timestamp >= CYCLE_TICKS)
do_cycle(out);
return CR_OK;
}
/////////////////////////////////////////////////////
// tile property detectors
//
static bool is_warm(const df::coord &pos) {
auto block = Maps::getTileBlock(pos);
if (!block)
return false;
return block->temperature_1[pos.x&15][pos.y&15] >= 10075;
}
static bool is_wall(const df::coord &pos) {
df::tiletype *tt = Maps::getTileType(pos);
return tt && tileShape(*tt) == df::tiletype_shape::WALL;
}
static bool is_rough_wall(int16_t x, int16_t y, int16_t z) {
df::tiletype *tt = Maps::getTileType(x, y, z);
if (!tt)
return false;
return tileShape(*tt) == df::tiletype_shape::WALL &&
tileSpecial(*tt) != df::tiletype_special::SMOOTH;
}
static bool is_smooth_wall(const df::coord &pos) {
df::tiletype *tt = Maps::getTileType(pos);
return tt && tileSpecial(*tt) == df::tiletype_special::SMOOTH
&& tileShape(*tt) == df::tiletype_shape::WALL;
}
static bool is_aquifer(int16_t x, int16_t y, int16_t z, df::tile_designation *des = NULL) {
if (!des)
des = Maps::getTileDesignation(x, y, z);
if (!des)
return false;
return des->bits.water_table && is_rough_wall(x, y, z);
}
static bool is_aquifer(const df::coord &pos, df::tile_designation *des = NULL) {
return is_aquifer(pos.x, pos.y, pos.z, des);
}
static bool is_heavy_aquifer(int16_t x, int16_t y, int16_t z, df::map_block *block = NULL) {
if (!block)
block = Maps::getTileBlock(x, y, z);
if (!block || !block->flags.bits.has_aquifer)
return false;
auto occ = Maps::getTileOccupancy(x, y, z);
if (!occ)
return false;
return occ->bits.heavy_aquifer;
}
static bool is_heavy_aquifer(const df::coord &pos, df::map_block *block = NULL) {
return is_heavy_aquifer(pos.x, pos.y, pos.z, block);
}
static bool is_wet(int16_t x, int16_t y, int16_t z) {
auto des = Maps::getTileDesignation(x, y, z);
if (!des)
return false;
if (des->bits.liquid_type == df::tile_liquid::Water && des->bits.flow_size >= 1)
return true;
if (is_aquifer(x, y, z, des))
return true;
return false;
}
static bool is_damp(const df::coord &pos) {
return is_wet(pos.x-1, pos.y-1, pos.z) ||
is_wet(pos.x, pos.y-1, pos.z) ||
is_wet(pos.x+1, pos.y-1, pos.z) ||
is_wet(pos.x-1, pos.y, pos.z) ||
is_wet(pos.x+1, pos.y, pos.z) ||
is_wet(pos.x-1, pos.y+1, pos.z) ||
is_wet(pos.x, pos.y+1, pos.z) ||
is_wet(pos.x+1, pos.y+1, pos.z) ||
is_wet(pos.x, pos.y, pos.z+1);
}
/////////////////////////////////////////////////////
// event handlers
//
static void propagate_if_material_match(color_ostream& out, MapExtras::MapCache & mc,
int16_t mat, bool warm, bool damp, const df::coord & pos)
{
auto block = Maps::getTileBlock(pos);
if (!block)
return;
INFO(log,out).print("testing adjacent tile at (%d,%d,%d), mat:%d",
pos.x, pos.y, pos.z, mc.veinMaterialAt(pos));
if (mat != mc.veinMaterialAt(pos))
return;
auto des = Maps::getTileDesignation(pos);
auto occ = Maps::getTileOccupancy(pos);
if (!des || !occ || !is_wall(pos))
return;
if (des->bits.hidden && ((warm && is_warm(pos)) || (damp && is_damp(pos)))) {
des->bits.dig = df::tile_dig_designation::Default;
occ->bits.dig_auto = true;
}
if (warm) {
if (auto warm_mask = World::getPersistentTilemask(warm_config, block, true))
warm_mask->setassignment(pos, true);
}
if (damp) {
if (auto damp_mask = World::getPersistentTilemask(damp_config, block, true))
damp_mask->setassignment(pos, true);
}
}
static void do_autodig(color_ostream& out, bool warm, bool damp, const df::coord & pos) {
MapExtras::MapCache mc;
int16_t mat = mc.veinMaterialAt(pos);
if (mat == -1)
return;
DEBUG(log,out).print("processing autodig tile at (%d,%d,%d), warm:%d, damp:%d, mat:%d\n",
pos.x, pos.y, pos.z, warm, damp, mat);
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord(-1, -1, 0));
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord( 0, -1, 0));
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord( 1, -1, 0));
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord(-1, 0, 0));
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord( 1, 0, 0));
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord(-1, 1, 0));
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord( 0, 1, 0));
propagate_if_material_match(out, mc, mat, warm, damp, pos + df::coord( 1, 1, 0));
}
static void process_taken_dig_job(color_ostream& out, const df::coord & pos) {
auto block = Maps::getTileBlock(pos);
if (!block)
return;
bool warm = false, damp = false;
if (auto warm_mask = World::getPersistentTilemask(warm_config, block)) {
warm = warm_mask->getassignment(pos);
warm_mask->setassignment(pos, false);
}
if (auto damp_mask = World::getPersistentTilemask(damp_config, block)) {
damp = damp_mask->getassignment(pos);
damp_mask->setassignment(pos, false);
}
auto occ = Maps::getTileOccupancy(pos);
if (!occ || !occ->bits.dig_auto || (!warm && !damp))
return;
do_autodig(out, warm, damp, pos);
}
static void unhide_tagged(color_ostream& out, const df::coord & pos) {
auto block = Maps::getTileBlock(pos);
if (!block)
return;
if (auto warm_mask = World::getPersistentTilemask(warm_config, block)) {
TRACE(log,out).print("testing tile at (%d,%d,%d); mask:%d, warm:%d\n", pos.x, pos.y, pos.z,
warm_mask->getassignment(pos), is_warm(pos));
if (warm_mask->getassignment(pos) && is_warm(pos)) {
DEBUG(log,out).print("revealing warm dig tile at (%d,%d,%d)\n", pos.x, pos.y, pos.z);
block->designation[pos.x&15][pos.y&15].bits.hidden = false;
}
}
if (auto damp_mask = World::getPersistentTilemask(damp_config, block)) {
TRACE(log,out).print("testing tile at (%d,%d,%d); mask:%d, damp:%d\n", pos.x, pos.y, pos.z,
damp_mask->getassignment(pos), is_damp(pos));
if (damp_mask->getassignment(pos) && is_damp(pos)) {
DEBUG(log,out).print("revealing damp dig tile at (%d,%d,%d)\n", pos.x, pos.y, pos.z);
block->designation[pos.x&15][pos.y&15].bits.hidden = false;
}
}
}
static void unhide_surrounding_tagged_tiles(color_ostream& out, void* job_ptr) {
df::job *job = (df::job *)job_ptr;
auto type = ENUM_ATTR(job_type, type, job->job_type);
if (type != job_type_class::Digging)
return;
const auto & pos = job->pos;
TRACE(log,out).print("handing dig job at (%d,%d,%d)\n", pos.x, pos.y, pos.z);
process_taken_dig_job(out, pos);
unhide_tagged(out, pos + df::coord(-1, -1, 0));
unhide_tagged(out, pos + df::coord( 0, -1, 0));
unhide_tagged(out, pos + df::coord( 1, -1, 0));
unhide_tagged(out, pos + df::coord(-1, 0, 0));
unhide_tagged(out, pos + df::coord( 1, 0, 0));
unhide_tagged(out, pos + df::coord(-1, 1, 0));
unhide_tagged(out, pos + df::coord( 0, 1, 0));
unhide_tagged(out, pos + df::coord( 1, 1, 0));
switch (job->job_type) {
case df::job_type::CarveUpDownStaircase:
unhide_tagged(out, pos + df::coord(0, 0, -1));
unhide_tagged(out, pos + df::coord(0, 0, 1));
break;
case df::job_type::CarveDownwardStaircase:
unhide_tagged(out, pos + df::coord(0, 0, -1));
break;
case df::job_type::CarveUpwardStaircase:
unhide_tagged(out, pos + df::coord(0, 0, 1));
break;
case df::job_type::DigChannel:
unhide_tagged(out, pos + df::coord(-1, -1, -1));
unhide_tagged(out, pos + df::coord( 0, -1, -1));
unhide_tagged(out, pos + df::coord( 1, -1, -1));
unhide_tagged(out, pos + df::coord(-1, 0, -1));
unhide_tagged(out, pos + df::coord( 1, 0, -1));
unhide_tagged(out, pos + df::coord(-1, 1, -1));
unhide_tagged(out, pos + df::coord( 0, 1, -1));
unhide_tagged(out, pos + df::coord( 1, 1, -1));
break;
case df::job_type::CarveRamp:
unhide_tagged(out, pos + df::coord(-1, -1, 1));
unhide_tagged(out, pos + df::coord( 0, -1, 1));
unhide_tagged(out, pos + df::coord( 1, -1, 1));
unhide_tagged(out, pos + df::coord(-1, 0, 1));
unhide_tagged(out, pos + df::coord( 1, 0, 1));
unhide_tagged(out, pos + df::coord(-1, 1, 1));
unhide_tagged(out, pos + df::coord( 0, 1, 1));
unhide_tagged(out, pos + df::coord( 1, 1, 1));
break;
default:
break;
}
}
/////////////////////////////////////////////////////
// commands
//
template <class T>
bool from_string(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
enum circle_what
{
circle_set,
circle_unset,
circle_invert,
};
bool dig (MapExtras::MapCache & MCache,
circle_what what,
df::tile_dig_designation type,
int32_t priority,
int32_t x, int32_t y, int32_t z,
int x_max, int y_max
)
{
DFCoord at (x,y,z);
auto b = MCache.BlockAt(at/16);
if(!b || !b->is_valid())
return false;
if(x == 0 || x == x_max * 16 - 1)
{
//out.print("not digging map border\n");
return false;
}
if(y == 0 || y == y_max * 16 - 1)
{
//out.print("not digging map border\n");
return false;
}
df::tiletype tt = MCache.tiletypeAt(at);
df::tile_designation des = MCache.designationAt(at);
// could be potentially used to locate hidden constructions?
if(tileMaterial(tt) == tiletype_material::CONSTRUCTION && !des.bits.hidden)
return false;
df::tiletype_shape ts = tileShape(tt);
if (ts == tiletype_shape::EMPTY && !des.bits.hidden)
return false;
if(!des.bits.hidden)
{
do
{
df::tiletype_shape_basic tsb = ENUM_ATTR(tiletype_shape, basic_shape, ts);
if(tsb == tiletype_shape_basic::Wall)
{
//std::cerr << "allowing tt" << (int)tt << ", is wall\n";
break;
}
if (tsb == tiletype_shape_basic::Floor
&& (type == tile_dig_designation::DownStair || type == tile_dig_designation::Channel)
&& ts != tiletype_shape::BRANCH
&& ts != tiletype_shape::TRUNK_BRANCH
&& ts != tiletype_shape::TWIG
)
{
//std::cerr << "allowing tt" << (int)tt << ", is floor\n";
break;
}
if (tsb == tiletype_shape_basic::Stair && type == tile_dig_designation::Channel )
break;
return false;
}
while(0);
}
switch(what)
{
case circle_set:
if(des.bits.dig == tile_dig_designation::No)
{
des.bits.dig = type;
}
break;
case circle_unset:
if (des.bits.dig != tile_dig_designation::No)
{
des.bits.dig = tile_dig_designation::No;
}
break;
case circle_invert:
if(des.bits.dig == tile_dig_designation::No)
{
des.bits.dig = type;
}
else
{
des.bits.dig = tile_dig_designation::No;
}
break;
}
//std::cerr << "allowing tt" << (int)tt << "\n";
MCache.setDesignationAt(at,des,priority);
return true;
};
bool lineX (MapExtras::MapCache & MCache,
circle_what what,
df::tile_dig_designation type,
int32_t priority,
int32_t y1, int32_t y2, int32_t x, int32_t z,
int x_max, int y_max
)
{
for(int32_t y = y1; y <= y2; y++)
{
dig(MCache, what, type, priority, x,y,z, x_max, y_max);
}
return true;
};
bool lineY (MapExtras::MapCache & MCache,
circle_what what,
df::tile_dig_designation type,
int32_t priority,
int32_t x1, int32_t x2, int32_t y, int32_t z,
int x_max, int y_max
)
{
for(int32_t x = x1; x <= x2; x++)
{
dig(MCache, what, type, priority, x,y,z, x_max, y_max);
}
return true;
};
int32_t parse_priority(color_ostream &out, vector<string> ¶meters)
{
int32_t default_priority = game->main_interface.designation.priority;
for (auto it = parameters.begin(); it != parameters.end(); ++it)
{
const string &s = *it;
if (s.substr(0, 2) == "p=" || s.substr(0, 2) == "-p")
{
if (s.size() >= 3)
{
auto priority = int32_t(1000 * atof(s.c_str() + 2));
parameters.erase(it);
return priority;
}
else if (it + 1 != parameters.end())
{
auto priority = int32_t(1000 * atof((*(it + 1)).c_str()));
parameters.erase(it, it + 2);
return priority;
}
else
{
out.printerr("invalid priority specified; reverting to %i\n", default_priority);
break;
}
}
}
return default_priority;
}
string forward_priority(color_ostream &out, vector<string> ¶meters)
{
return string("-p") + int_to_string(parse_priority(out, parameters) / 1000);
}
command_result digcircle (color_ostream &out, vector <string> & parameters)
{
static bool filled = false;
static circle_what what = circle_set;
static df::tile_dig_designation type = tile_dig_designation::Default;
static int diameter = 0;
auto saved_d = diameter;
bool force_help = false;
int32_t priority = parse_priority(out, parameters);
for(size_t i = 0; i < parameters.size();i++)
{
if(parameters[i] == "help" || parameters[i] == "?")
{
force_help = true;
}
else if(parameters[i] == "hollow")
{
filled = false;
}
else if(parameters[i] == "filled")
{
filled = true;
}
else if(parameters[i] == "set")
{
what = circle_set;
}
else if(parameters[i] == "unset")
{
what = circle_unset;
}
else if(parameters[i] == "invert")
{
what = circle_invert;
}
else if(parameters[i] == "dig")
{
type = tile_dig_designation::Default;
}
else if(parameters[i] == "ramp")
{
type = tile_dig_designation::Ramp;
}
else if(parameters[i] == "dstair")
{
type = tile_dig_designation::DownStair;
}
else if(parameters[i] == "ustair")
{
type = tile_dig_designation::UpStair;
}
else if(parameters[i] == "xstair")
{
type = tile_dig_designation::UpDownStair;
}
else if(parameters[i] == "chan")
{
type = tile_dig_designation::Channel;
}
else if (!from_string(diameter,parameters[i], std::dec))
{
diameter = saved_d;
}
}
if(diameter < 0)
diameter = -diameter;
if(force_help || diameter == 0)
{
out.print(
"A command for easy designation of filled and hollow circles.\n"
"\n"
"Options:\n"
" hollow = Set the circle to hollow (default)\n"
" filled = Set the circle to filled\n"
"\n"
" set = set designation\n"
" unset = unset current designation\n"
" invert = invert current designation\n"
"\n"
" dig = normal digging\n"
" ramp = ramp digging\n"
" ustair = staircase up\n"
" dstair = staircase down\n"
" xstair = staircase up/down\n"
" chan = dig channel\n"
"\n"
" # = diameter in tiles (default = 0)\n"
" -p # = designation priority (default = 4)\n"
"\n"
"After you have set the options, the command called with no options\n"
"repeats with the last selected parameters:\n"
"'digcircle filled 3' = Dig a filled circle with diameter = 3.\n"
"'digcircle' = Do it again.\n"
);
return CR_OK;
}
int32_t cx, cy, cz;
CoreSuspender suspend;
if (!Maps::IsValid())
{
out.printerr("Map is not available!\n");
return CR_FAILURE;
}
uint32_t x_max, y_max, z_max;
Maps::getSize(x_max,y_max,z_max);
MapExtras::MapCache MCache;
if(!Gui::getCursorCoords(cx,cy,cz) || cx == -30000)
{
out.printerr("Can't get the cursor coords...\n");
return CR_FAILURE;
}
int r = diameter / 2;
int iter;
bool adjust;
if(diameter % 2)
{
// paint center
if(filled)
{
lineY(MCache, what, type, priority, cx - r, cx + r, cy, cz, x_max, y_max);
}
else
{
dig(MCache, what, type, priority, cx - r, cy, cz, x_max, y_max);
dig(MCache, what, type, priority, cx + r, cy, cz, x_max, y_max);
}
adjust = false;
iter = 2;
}
else
{
adjust = true;
iter = 1;
}
int lastwhole = r;
for(; iter <= diameter - 1; iter +=2)
{
// top, bottom coords
int top = cy - ((iter + 1) / 2) + adjust;
int bottom = cy + ((iter + 1) / 2);
// see where the current 'line' intersects the circle
double val = std::sqrt(double(diameter*diameter - iter*iter));
// adjust for circles with odd diameter
if(!adjust)
val -= 1;
// map the found value to the DF grid
double whole;
double fraction = std::modf(val / 2.0, & whole);
if (fraction > 0.5)
whole += 1.0;
int right = cx + whole;
int left = cx - whole + adjust;
int diff = lastwhole - whole;
// paint
if(filled || iter == diameter - 1)
{
lineY(MCache, what, type, priority, left, right, top, cz, x_max, y_max);
lineY(MCache, what, type, priority, left, right, bottom, cz, x_max, y_max);
}
else
{
dig(MCache, what, type, priority, left, top, cz, x_max, y_max);
dig(MCache, what, type, priority, left, bottom, cz, x_max, y_max);
dig(MCache, what, type, priority, right, top, cz, x_max, y_max);
dig(MCache, what, type, priority, right, bottom, cz, x_max, y_max);
}
if(!filled && diff > 1)
{
int lright = cx + lastwhole;
int lleft = cx - lastwhole + adjust;
lineY(MCache, what, type, priority, lleft + 1, left - 1, top + 1 , cz, x_max, y_max);
lineY(MCache, what, type, priority, right + 1, lright - 1, top + 1 , cz, x_max, y_max);
lineY(MCache, what, type, priority, lleft + 1, left - 1, bottom - 1 , cz, x_max, y_max);
lineY(MCache, what, type, priority, right + 1, lright - 1, bottom - 1 , cz, x_max, y_max);
}
lastwhole = whole;
}
MCache.WriteAll();
return CR_OK;
}
typedef char digmask[16][16];
static digmask diag5[5] =
{
{
{1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1},
{0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0},
{0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0},
{0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0},
{0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0},
{1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1},
{0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0},
{0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0},
{0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0},
{0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0},
{1,0,0,0,0,1,0,0,0,0,1,0,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