forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embark-tools.cpp
860 lines (825 loc) · 27.7 KB
/
embark-tools.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
#include "Console.h"
#include "Core.h"
#include "DataDefs.h"
#include "Export.h"
#include "PluginManager.h"
#include "MiscUtils.h"
#include "modules/Screen.h"
#include "modules/Gui.h"
#include <algorithm>
#include <map>
#include <set>
#include <VTableInterpose.h>
#include "ColorText.h"
#include "uicommon.h"
#include "df/viewscreen_choose_start_sitest.h"
#include "df/interface_key.h"
using namespace DFHack;
using df::global::enabler;
using df::global::gps;
DFHACK_PLUGIN("embark-tools");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
#define FOR_ITER_TOOLS(iter) for(auto iter = tools.begin(); iter != tools.end(); iter++)
void update_embark_sidebar (df::viewscreen_choose_start_sitest * screen)
{
bool is_top = false;
if (screen->location.embark_pos_min.y == 0)
is_top = true;
std::set<df::interface_key> keys;
keys.insert(df::interface_key::SETUP_LOCAL_Y_MUP);
screen->feed(&keys);
if (!is_top)
{
keys.insert(df::interface_key::SETUP_LOCAL_Y_MDOWN);
screen->feed(&keys);
}
}
void get_embark_pos (df::viewscreen_choose_start_sitest * screen,
int& x1, int& x2, int& y1, int& y2, int& w, int& h)
{
x1 = screen->location.embark_pos_min.x,
x2 = screen->location.embark_pos_max.x,
y1 = screen->location.embark_pos_min.y,
y2 = screen->location.embark_pos_max.y,
w = x2 - x1 + 1,
h = y2 - y1 + 1;
}
void set_embark_pos (df::viewscreen_choose_start_sitest * screen,
int x1, int x2, int y1, int y2)
{
screen->location.embark_pos_min.x = x1;
screen->location.embark_pos_max.x = x2;
screen->location.embark_pos_min.y = y1;
screen->location.embark_pos_max.y = y2;
}
#define GET_EMBARK_POS(screen, a, b, c, d, e, f) \
int a, b, c, d, e, f; \
get_embark_pos(screen, a, b, c, d, e, f);
typedef df::viewscreen_choose_start_sitest start_sitest;
typedef std::set<df::interface_key> ikey_set;
class EmbarkTool
{
protected:
bool enabled;
public:
EmbarkTool()
:enabled(false)
{ }
virtual bool getEnabled() { return enabled; }
virtual void setEnabled(bool state) { enabled = state; }
virtual void toggleEnabled() { setEnabled(!enabled); }
virtual std::string getId() = 0;
virtual std::string getName() = 0;
virtual std::string getDesc() = 0;
virtual df::interface_key getToggleKey() = 0;
virtual void before_render(start_sitest* screen) { };
virtual void after_render(start_sitest* screen) { };
virtual void before_feed(start_sitest* screen, ikey_set* input, bool &cancel) { };
virtual void after_feed(start_sitest* screen, ikey_set* input) { };
virtual void after_mouse_event(start_sitest* screen) { };
};
std::map<std::string, EmbarkTool*> tools;
/*
class SampleTool : public EmbarkTool
{
virtual std::string getId() { return "id"; }
virtual std::string getName() { return "Name"; }
virtual std::string getDesc() { return "Description"; }
virtual df::interface_key getToggleKey() { return df::interface_key::KEY; }
virtual void before_render(start_sitest* screen) { }
virtual void after_render(start_sitest* screen) { }
virtual void before_feed(start_sitest* screen, ikey_set* input, bool &cancel) { };
virtual void after_feed(start_sitest* screen, ikey_set* input) { };
};
*/
class EmbarkAnywhere : public EmbarkTool
{
public:
virtual std::string getId() { return "anywhere"; }
virtual std::string getName() { return "Embark anywhere"; }
virtual std::string getDesc() { return "Allows embarking anywhere on the world map"; }
virtual df::interface_key getToggleKey() { return df::interface_key::CUSTOM_A; }
virtual void after_render(start_sitest* screen)
{
auto dim = Screen::getWindowSize();
int x = 20, y = dim.y - 2;
if (screen->page >= 0 && screen->page <= 4)
{
OutputString(COLOR_WHITE, x, y, ": Embark!");
}
}
virtual void before_feed(start_sitest* screen, ikey_set *input, bool &cancel)
{
if (input->count(df::interface_key::SETUP_EMBARK))
{
cancel = true;
screen->in_embark_normal = 1;
}
};
};
class SandIndicator : public EmbarkTool
{
protected:
bool dirty;
std::string indicator;
void update_indicator()
{
CoreSuspendClaimer suspend;
buffered_color_ostream out;
Core::getInstance().runCommand(out, "prospect");
auto fragments = out.fragments();
indicator = "";
for (auto iter = fragments.begin(); iter != fragments.end(); iter++)
{
std::string fragment = iter->second;
if (fragment.find("SAND_") != std::string::npos)
{
indicator = "Sand";
break;
}
}
dirty = false;
}
public:
SandIndicator()
:EmbarkTool(),
dirty(true),
indicator("")
{ }
virtual void setEnabled(bool state)
{
EmbarkTool::setEnabled(state);
dirty = true;
}
virtual std::string getId() { return "sand"; }
virtual std::string getName() { return "Sand indicator"; }
virtual std::string getDesc() { return "Displays an indicator when sand is present on the given embark site"; }
virtual df::interface_key getToggleKey() { return df::interface_key::CUSTOM_S; }
virtual void after_render(start_sitest* screen)
{
if (dirty)
update_indicator();
auto dim = Screen::getWindowSize();
int x = dim.x - 28,
y = 13;
if (screen->page == 0)
{
OutputString(COLOR_YELLOW, x, y, indicator);
}
}
virtual void after_feed(start_sitest* screen, ikey_set* input)
{
dirty = true;
};
};
class StablePosition : public EmbarkTool
{
protected:
int prev_position[4];
bool moved_position;
void save_position(start_sitest* screen)
{
prev_position[0] = screen->location.embark_pos_min.x;
prev_position[1] = screen->location.embark_pos_max.x;
prev_position[2] = screen->location.embark_pos_min.y;
prev_position[3] = screen->location.embark_pos_max.y;
}
void restore_position(start_sitest* screen)
{
if (screen->finder.finder_state != -1)
{
// Site finder is active - don't override default local position
return;
}
screen->location.embark_pos_min.x = prev_position[0];
screen->location.embark_pos_max.x = prev_position[1];
screen->location.embark_pos_min.y = prev_position[2];
screen->location.embark_pos_max.y = prev_position[3];
update_embark_sidebar(screen);
}
public:
StablePosition()
:EmbarkTool(),
moved_position(false)
{
prev_position[0] = 0;
prev_position[1] = 0;
prev_position[2] = 3;
prev_position[3] = 3;
}
virtual std::string getId() { return "sticky"; }
virtual std::string getName() { return "Stable position"; }
virtual std::string getDesc() { return "Maintains the selected local area while navigating the world map"; }
virtual df::interface_key getToggleKey() { return df::interface_key::CUSTOM_P; }
virtual void before_render(start_sitest* screen) {
if (moved_position)
{
restore_position(screen);
moved_position = false;
}
}
virtual void before_feed(start_sitest* screen, ikey_set* input, bool &cancel) {
for (auto iter = input->begin(); iter != input->end(); iter++)
{
df::interface_key key = *iter;
bool is_motion = false;
int dx = 0, dy = 0;
switch (key)
{
case df::interface_key::CURSOR_UP:
case df::interface_key::CURSOR_DOWN:
case df::interface_key::CURSOR_LEFT:
case df::interface_key::CURSOR_RIGHT:
case df::interface_key::CURSOR_UPLEFT:
case df::interface_key::CURSOR_UPRIGHT:
case df::interface_key::CURSOR_DOWNLEFT:
case df::interface_key::CURSOR_DOWNRIGHT:
case df::interface_key::CURSOR_UP_FAST:
case df::interface_key::CURSOR_DOWN_FAST:
case df::interface_key::CURSOR_LEFT_FAST:
case df::interface_key::CURSOR_RIGHT_FAST:
case df::interface_key::CURSOR_UPLEFT_FAST:
case df::interface_key::CURSOR_UPRIGHT_FAST:
case df::interface_key::CURSOR_DOWNLEFT_FAST:
case df::interface_key::CURSOR_DOWNRIGHT_FAST:
is_motion = true;
break;
}
if (is_motion && !moved_position)
{
save_position(screen);
moved_position = true;
}
}
};
};
class MouseControl : public EmbarkTool
{
protected:
// Used for event handling
int prev_x;
int prev_y;
bool prev_lbut;
// Used for controls
bool base_max_x;
bool base_max_y;
bool in_local_move;
bool in_local_edge_resize_x;
bool in_local_edge_resize_y;
bool in_local_corner_resize;
// These keep track of where the embark location would be (i.e. if
// the mouse is dragged out of the local embark area), to prevent
// the mouse from moving the actual area when it's 20+ tiles away
int local_overshoot_x1;
int local_overshoot_x2;
int local_overshoot_y1;
int local_overshoot_y2;
inline bool in_local_adjust()
{
return in_local_move || in_local_edge_resize_x || in_local_edge_resize_y ||
in_local_corner_resize;
}
void lbut_press(start_sitest* screen, bool pressed, int x, int y)
{
GET_EMBARK_POS(screen, x1, x2, y1, y2, width, height);
in_local_move = in_local_edge_resize_x = in_local_edge_resize_y =
in_local_corner_resize = false;
if (pressed)
{
if (x >= 1 && x <= 16 && y >= 2 && y <= 17)
{
// Local embark - translate to local map coordinates
x -= 1;
y -= 2;
if ((x == x1 || x == x2) && (y == y1 || y == y2))
{
in_local_corner_resize = true;
base_max_x = (x == x2);
base_max_y = (y == y2);
}
else if (x == x1 || x == x2)
{
in_local_edge_resize_x = true;
base_max_x = (x == x2);
base_max_y = false;
}
else if (y == y1 || y == y2)
{
in_local_edge_resize_y = true;
base_max_x = false;
base_max_y = (y == y2);
}
else if (x > x1 && x < x2 && y > y1 && y < y2)
{
in_local_move = true;
base_max_x = base_max_y = false;
local_overshoot_x1 = x1;
local_overshoot_x2 = x2;
local_overshoot_y1 = y1;
local_overshoot_y2 = y2;
}
}
}
update_embark_sidebar(screen);
}
void mouse_move(start_sitest* screen, int x, int y)
{
GET_EMBARK_POS(screen, x1, x2, y1, y2, width, height);
if (x == -1 && prev_x > (2 + 16))
{
x = gps->dimx;
gps->mouse_x = x - 1;
}
if (y == -1 && prev_y > (1 + 16))
{
y = gps->dimy;
gps->mouse_y = y - 1;
}
if (in_local_corner_resize || in_local_edge_resize_x || in_local_edge_resize_y)
{
x -= 1;
y -= 2;
}
if (in_local_corner_resize)
{
x = std::max(0, std::min(15, x));
y = std::max(0, std::min(15, y));
if (base_max_x)
x2 = x;
else
x1 = x;
if (base_max_y)
y2 = y;
else
y1 = y;
if (x1 > x2)
{
std::swap(x1, x2);
base_max_x = !base_max_x;
}
if (y1 > y2)
{
std::swap(y1, y2);
base_max_y = !base_max_y;
}
}
else if (in_local_edge_resize_x)
{
x = std::max(0, std::min(15, x));
if (base_max_x)
x2 = x;
else
x1 = x;
if (x1 > x2)
{
std::swap(x1, x2);
base_max_x = !base_max_x;
}
}
else if (in_local_edge_resize_y)
{
y = std::max(0, std::min(15, y));
if (base_max_y)
y2 = y;
else
y1 = y;
if (y1 > y2)
{
std::swap(y1, y2);
base_max_y = !base_max_y;
}
}
else if (in_local_move)
{
int dx = x - prev_x;
int dy = y - prev_y;
local_overshoot_x1 += dx;
local_overshoot_x2 += dx;
local_overshoot_y1 += dy;
local_overshoot_y2 += dy;
if (local_overshoot_x1 < 0)
{
x1 = 0;
x2 = width - 1;
}
else if (local_overshoot_x2 > 15)
{
x1 = 15 - (width - 1);
x2 = 15;
}
else
{
x1 = local_overshoot_x1;
x2 = local_overshoot_x2;
}
if (local_overshoot_y1 < 0)
{
y1 = 0;
y2 = height - 1;
}
else if (local_overshoot_y2 > 15)
{
y1 = 15 - (height - 1);
y2 = 15;
}
else
{
y1 = local_overshoot_y1;
y2 = local_overshoot_y2;
}
}
set_embark_pos(screen, x1, x2, y1, y2);
}
public:
MouseControl()
:EmbarkTool(),
prev_x(0),
prev_y(0),
prev_lbut(false),
base_max_x(false),
base_max_y(false),
in_local_move(false),
in_local_edge_resize_x(false),
in_local_edge_resize_y(false),
in_local_corner_resize(false),
local_overshoot_x1(0),
local_overshoot_x2(0),
local_overshoot_y1(0),
local_overshoot_y2(0)
{ }
virtual std::string getId() { return "mouse"; }
virtual std::string getName() { return "Mouse control"; }
virtual std::string getDesc() { return "Implements mouse controls on the embark screen"; }
virtual df::interface_key getToggleKey() { return df::interface_key::CUSTOM_M; }
virtual void after_render(start_sitest* screen)
{
GET_EMBARK_POS(screen, x1, x2, y1, y2, width, height);
int mouse_x = gps->mouse_x, mouse_y = gps->mouse_y;
int local_x = prev_x - 1;
int local_y = prev_y - 2;
if (local_x >= x1 && local_x <= x2 && local_y >= y1 && local_y <= y2)
{
int screen_x1 = x1 + 1;
int screen_x2 = x2 + 1;
int screen_y1 = y1 + 2;
int screen_y2 = y2 + 2;
UIColor fg = in_local_adjust() ? COLOR_GREY : COLOR_DARKGREY;
Screen::Pen corner_ul = Screen::Pen((char)201, fg, COLOR_BLACK);
Screen::Pen corner_ur = Screen::Pen((char)187, fg, COLOR_BLACK);
Screen::Pen corner_dl = Screen::Pen((char)200, fg, COLOR_BLACK);
Screen::Pen corner_dr = Screen::Pen((char)188, fg, COLOR_BLACK);
Screen::Pen border_ud = Screen::Pen((char)205, fg, COLOR_BLACK);
Screen::Pen border_lr = Screen::Pen((char)186, fg, COLOR_BLACK);
if (in_local_corner_resize ||
((local_x == x1 || local_x == x2) && (local_y == y1 || local_y == y2)))
{
if (local_x == x1 && local_y == y1)
Screen::paintTile(corner_ul, screen_x1, screen_y1);
else if (local_x == x2 && local_y == y1)
Screen::paintTile(corner_ur, screen_x2, screen_y1);
else if (local_x == x1 && local_y == y2)
Screen::paintTile(corner_dl, screen_x1, screen_y2);
else if (local_x == x2 && local_y == y2)
Screen::paintTile(corner_dr, screen_x2, screen_y2);
}
else if (in_local_edge_resize_x || local_x == x1 || local_x == x2)
{
if ((in_local_edge_resize_x && !base_max_x) || local_x == x1)
{
Screen::paintTile(corner_ul, screen_x1, screen_y1);
for (int i = screen_y1 + 1; i <= screen_y2 - 1; ++i)
Screen::paintTile(border_lr, screen_x1, i);
Screen::paintTile(corner_dl, screen_x1, screen_y2);
}
else
{
Screen::paintTile(corner_ur, screen_x2, screen_y1);
for (int i = screen_y1 + 1; i <= screen_y2 - 1; ++i)
Screen::paintTile(border_lr, screen_x2, i);
Screen::paintTile(corner_dr, screen_x2, screen_y2);
}
}
else if (in_local_edge_resize_y || local_y == y1 || local_y == y2)
{
if ((in_local_edge_resize_y && !base_max_y) || local_y == y1)
{
Screen::paintTile(corner_ul, screen_x1, screen_y1);
for (int i = screen_x1 + 1; i <= screen_x2 - 1; ++i)
Screen::paintTile(border_ud, i, screen_y1);
Screen::paintTile(corner_ur, screen_x2, screen_y1);
}
else
{
Screen::paintTile(corner_dl, screen_x1, screen_y2);
for (int i = screen_x1 + 1; i <= screen_x2 - 1; ++i)
Screen::paintTile(border_ud, i, screen_y2);
Screen::paintTile(corner_dr, screen_x2, screen_y2);
}
}
else
{
Screen::paintTile(corner_ul, screen_x1, screen_y1);
Screen::paintTile(corner_ur, screen_x2, screen_y1);
Screen::paintTile(corner_dl, screen_x1, screen_y2);
Screen::paintTile(corner_dr, screen_x2, screen_y2);
}
}
}
virtual void after_mouse_event(start_sitest* screen)
{
if (enabler->mouse_lbut != prev_lbut)
{
lbut_press(screen, enabler->mouse_lbut, gps->mouse_x, gps->mouse_y);
}
if (gps->mouse_x != prev_x || gps->mouse_y != prev_y)
{
mouse_move(screen, gps->mouse_x, gps->mouse_y);
}
prev_lbut = enabler->mouse_lbut;
prev_x = gps->mouse_x;
prev_y = gps->mouse_y;
};
};
class embark_tools_settings : public dfhack_viewscreen
{
public:
embark_tools_settings () { };
~embark_tools_settings () { };
void help () { };
std::string getFocusString () { return "embark-tools/options"; };
void render ()
{
parent->render();
int x, y;
auto dim = Screen::getWindowSize();
int width = 50,
height = 4 + 1 + tools.size(), // Padding + lower row
min_x = (dim.x - width) / 2,
max_x = (dim.x + width) / 2,
min_y = (dim.y - height) / 2,
max_y = min_y + height;
Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_DARKGREY), min_x, min_y, max_x, max_y);
Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_BLACK), min_x + 1, min_y + 1, max_x - 1, max_y - 1);
std::string title = " Embark tools (DFHack) ";
Screen::paintString(Screen::Pen(' ', COLOR_BLACK, COLOR_GREY), min_x + ((max_x - min_x - title.size()) / 2), min_y, title);
x = min_x + 2;
y = max_y - 2;
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::SELECT));
OutputString(COLOR_WHITE, x, y, "/");
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::LEAVESCREEN));
OutputString(COLOR_WHITE, x, y, ": Done");
y = min_y + 2;
FOR_ITER_TOOLS(iter)
{
EmbarkTool* t = iter->second;
x = min_x + 2;
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(t->getToggleKey()));
OutputString(COLOR_WHITE, x, y, ": " + t->getName() + ": ");
OutputString(t->getEnabled() ? COLOR_GREEN : COLOR_RED, x, y,
t->getEnabled() ? "Enabled" : "Disabled");
y++;
}
};
void feed (std::set<df::interface_key> * input)
{
if (input->count(df::interface_key::SELECT) || input->count(df::interface_key::LEAVESCREEN))
{
Screen::dismiss(this);
return;
}
for (auto iter = input->begin(); iter != input->end(); iter++)
{
df::interface_key key = *iter;
FOR_ITER_TOOLS(iter)
{
EmbarkTool* t = iter->second;
if (t->getToggleKey() == key)
{
t->toggleEnabled();
}
}
}
};
};
void add_tool (EmbarkTool *t)
{
tools[t->getId()] = t;
}
bool tool_exists (std::string tool_name)
{
return tools.find(tool_name) != tools.end();
}
bool tool_enabled (std::string tool_name)
{
if (tool_exists(tool_name))
return tools[tool_name]->getEnabled();
return false;
}
bool tool_enable (std::string tool_name, bool enable_state)
{
int n = 0;
FOR_ITER_TOOLS(iter)
{
EmbarkTool* tool = iter->second;
if (tool->getId() == tool_name || tool_name == "all")
{
tool->setEnabled(enable_state);
n++;
}
}
return (bool)n;
}
struct choose_start_site_hook : df::viewscreen_choose_start_sitest
{
typedef df::viewscreen_choose_start_sitest interpose_base;
void display_tool_status()
{
auto dim = Screen::getWindowSize();
int x = 1,
y = dim.y - 5;
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::CUSTOM_S));
OutputString(COLOR_WHITE, x, y, ": Enabled: ");
std::vector<std::string> parts;
FOR_ITER_TOOLS(it)
{
EmbarkTool *t = it->second;
if (t->getEnabled())
parts.push_back(t->getName());
}
if (parts.size())
{
std::string label = join_strings(", ", parts);
if (label.size() > dim.x - x - 1)
{
label.resize(dim.x - x - 1 - 3);
label.append("...");
}
OutputString(COLOR_LIGHTMAGENTA, x, y, label);
}
else
OutputString(COLOR_LIGHTMAGENTA, x, y, "(none)");
}
void display_settings()
{
Screen::show(new embark_tools_settings, plugin_self);
}
inline bool is_valid_page()
{
return (page >= 0 && page <= 4);
}
DEFINE_VMETHOD_INTERPOSE(void, feed, (std::set<df::interface_key> *input))
{
bool cancel = false;
FOR_ITER_TOOLS(iter)
{
EmbarkTool* tool = iter->second;
if (tool->getEnabled())
tool->before_feed(this, input, cancel);
}
if (cancel)
return;
INTERPOSE_NEXT(feed)(input);
if (input->count(df::interface_key::CUSTOM_S) && is_valid_page())
display_settings();
FOR_ITER_TOOLS(iter)
{
EmbarkTool* tool = iter->second;
if (tool->getEnabled())
tool->after_feed(this, input);
}
}
DEFINE_VMETHOD_INTERPOSE(void, render, ())
{
FOR_ITER_TOOLS(iter)
{
EmbarkTool* tool = iter->second;
if (tool->getEnabled())
tool->before_render(this);
}
INTERPOSE_NEXT(render)();
display_tool_status();
FOR_ITER_TOOLS(iter)
{
EmbarkTool* tool = iter->second;
if (tool->getEnabled())
tool->after_render(this);
}
}
};
IMPLEMENT_VMETHOD_INTERPOSE(choose_start_site_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(choose_start_site_hook, render);
command_result embark_tools_cmd (color_ostream &out, std::vector <std::string> & parameters);
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
add_tool(new EmbarkAnywhere);
add_tool(new MouseControl);
add_tool(new SandIndicator);
add_tool(new StablePosition);
std::string help = "";
help += "embark-tools (enable/disable) tool [tool...]\n"
"Tools:\n";
FOR_ITER_TOOLS(iter)
{
help += (" " + iter->second->getId() + ": " + iter->second->getDesc() + "\n");
}
commands.push_back(PluginCommand(
"embark-tools",
"A collection of embark tools",
embark_tools_cmd,
false,
help.c_str()
));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown (color_ostream &out)
{
INTERPOSE_HOOK(choose_start_site_hook, feed).remove();
INTERPOSE_HOOK(choose_start_site_hook, render).remove();
return CR_OK;
}
DFhackCExport command_result plugin_enable (color_ostream &out, bool enable)
{
if (is_enabled != enable)
{
if (!INTERPOSE_HOOK(choose_start_site_hook, feed).apply(enable) ||
!INTERPOSE_HOOK(choose_start_site_hook, render).apply(enable))
return CR_FAILURE;
is_enabled = enable;
}
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange (color_ostream &out, state_change_event evt)
{
return CR_OK;
}
DFhackCExport command_result plugin_onupdate (color_ostream &out)
{
static int8_t mask = 0;
static decltype(gps->mouse_x) prev_x = -1;
static decltype(gps->mouse_y) prev_y = -1;
df::viewscreen* parent = DFHack::Gui::getCurViewscreen();
VIRTUAL_CAST_VAR(screen, df::viewscreen_choose_start_sitest, parent);
if (!screen)
return CR_OK;
int8_t new_mask = (enabler->mouse_lbut << 1) |
(enabler->mouse_rbut << 2) |
(enabler->mouse_lbut_down << 3) |
(enabler->mouse_rbut_down << 4) |
(enabler->mouse_lbut_lift << 5) |
(enabler->mouse_rbut_lift << 6);
if (mask != new_mask || prev_x != gps->mouse_x || prev_y != gps->mouse_y)
{
FOR_ITER_TOOLS(iter)
{
if (iter->second->getEnabled())
iter->second->after_mouse_event(screen);
}
}
mask = new_mask;
prev_x = gps->mouse_x;
prev_y = gps->mouse_y;
return CR_OK;
}
command_result embark_tools_cmd (color_ostream &out, std::vector <std::string> & parameters)
{
CoreSuspender suspend;
if (parameters.size())
{
// Set by "enable"/"disable" - allows for multiple commands, e.g. "enable nano disable anywhere"
bool enable_state = true;
for (size_t i = 0; i < parameters.size(); i++)
{
if (parameters[i] == "enable")
{
enable_state = true;
plugin_enable(out, true); // Enable plugin
}
else if (parameters[i] == "disable")
enable_state = false;
else if (tool_exists(parameters[i]) || parameters[i] == "all")
{
tool_enable(parameters[i], enable_state);
}
else
return CR_WRONG_USAGE;
}
}
else
{
if (is_enabled)
{
out << "Tool status:" << std::endl;
FOR_ITER_TOOLS(iter)
{
EmbarkTool* t = iter->second;
out << " " << t->getName() << " (" << t->getId() << "): "
<< (t->getEnabled() ? "Enabled" : "Disabled") << std::endl;
}
}
else
{
out << "Plugin not enabled" << std::endl;
}
}
return CR_OK;
}