forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaterial.cpp
1220 lines (1040 loc) · 39.1 KB
/
automaterial.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
// Auto Material Select
#include <map>
#include <string>
#include <vector>
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <VTableInterpose.h>
// DF data structure definition headers
#include "DataDefs.h"
#include "MiscUtils.h"
#include "df/build_req_choice_genst.h"
#include "df/build_req_choice_specst.h"
#include "df/construction_type.h"
#include "df/item.h"
#include "df/ui.h"
#include "df/ui_build_selector.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/items_other_id.h"
#include "df/job.h"
#include "df/world.h"
#include "df/building_constructionst.h"
#include "modules/Gui.h"
#include "modules/Screen.h"
#include "modules/Items.h"
#include "modules/Constructions.h"
#include "modules/Buildings.h"
#include "modules/Maps.h"
#include "TileTypes.h"
#include "df/job_item.h"
using namespace std;
using std::map;
using std::string;
using std::vector;
using namespace DFHack;
using namespace df::enums;
using df::global::gps;
using df::global::ui;
using df::global::ui_build_selector;
DFHACK_PLUGIN("automaterial");
struct MaterialDescriptor
{
df::item_type item_type;
int16_t item_subtype;
int16_t type;
int32_t index;
bool valid;
bool matches(const MaterialDescriptor &a) const
{
return a.valid && valid &&
a.type == type &&
a.index == index &&
a.item_type == item_type &&
a.item_subtype == item_subtype;
}
};
static command_result automaterial_cmd(color_ostream &out, vector <string> & parameters)
{
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
void OutputString(int8_t color, int &x, int &y, const std::string &text, bool newline = false, int left_margin = 0)
{
Screen::paintString(Screen::Pen(' ', color, 0), x, y, text);
if (newline)
{
++y;
x = left_margin;
}
else
x += text.length();
}
void OutputHotkeyString(int &x, int &y, const char *text, const char *hotkey, bool newline = false, int left_margin = 0, int8_t color = COLOR_WHITE)
{
OutputString(10, x, y, hotkey);
string display(": ");
display.append(text);
OutputString(color, x, y, display, newline, left_margin);
}
void OutputToggleString(int &x, int &y, const char *text, const char *hotkey, bool state, bool newline = true, int left_margin = 0, int8_t color = COLOR_WHITE)
{
OutputHotkeyString(x, y, text, hotkey);
OutputString(COLOR_WHITE, x, y, ": ");
if (state)
OutputString(COLOR_GREEN, x, y, "Enabled", newline, left_margin);
else
OutputString(COLOR_GREY, x, y, "Disabled", newline, left_margin);
}
static string int_to_string(int i)
{
return static_cast<ostringstream*>( &(ostringstream() << i))->str();
}
//START UI Functions
struct coord32_t
{
int32_t x, y, z;
};
static enum t_box_select_mode {SELECT_FIRST, SELECT_SECOND, SELECT_MATERIALS, AUTOSELECT_MATERIALS} box_select_mode = SELECT_FIRST;
static coord32_t box_first, box_second;
static bool box_select_enabled = false;
static bool show_box_selection = true;
static bool hollow_selection = false;
static deque<df::item*> box_select_materials;
#define SELECTION_IGNORE_TICKS 10
static int ignore_selection = SELECTION_IGNORE_TICKS;
static map<int16_t, MaterialDescriptor> last_used_material;
static map<int16_t, MaterialDescriptor> last_moved_material;
static map< int16_t, vector<MaterialDescriptor> > preferred_materials;
static map< int16_t, df::interface_key > hotkeys;
static bool last_used_moved = false;
static bool auto_choose_materials = true;
static bool auto_choose_attempted = true;
static bool revert_to_last_used_type = false;
static bool allow_future_placement = false;
static inline bool in_material_choice_stage()
{
return Gui::build_selector_hotkey(Core::getTopViewscreen()) &&
ui_build_selector->building_type == df::building_type::Construction &&
ui->main.mode == ui_sidebar_mode::Build &&
ui_build_selector->stage == 2;
}
static inline bool in_placement_stage()
{
return Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
ui->main.mode == ui_sidebar_mode::Build &&
ui_build_selector &&
ui_build_selector->building_type == df::building_type::Construction &&
ui_build_selector->stage == 1;
}
static inline bool in_type_choice_stage()
{
return Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
ui->main.mode == ui_sidebar_mode::Build &&
ui_build_selector &&
ui_build_selector->building_type < 0;
}
static inline vector<MaterialDescriptor> &get_curr_constr_prefs()
{
if (preferred_materials.find(ui_build_selector->building_subtype) == preferred_materials.end())
preferred_materials[ui_build_selector->building_subtype] = vector<MaterialDescriptor>();
return preferred_materials[ui_build_selector->building_subtype];
}
static inline MaterialDescriptor &get_last_used_material()
{
if (last_used_material.find(ui_build_selector->building_subtype) == last_used_material.end())
last_used_material[ui_build_selector->building_subtype] = MaterialDescriptor();
return last_used_material[ui_build_selector->building_subtype];
}
static void set_last_used_material(const MaterialDescriptor &matetial)
{
last_used_material[ui_build_selector->building_subtype] = matetial;
}
static MaterialDescriptor &get_last_moved_material()
{
if (last_moved_material.find(ui_build_selector->building_subtype) == last_moved_material.end())
last_moved_material[ui_build_selector->building_subtype] = MaterialDescriptor();
return last_moved_material[ui_build_selector->building_subtype];
}
static void set_last_moved_material(const MaterialDescriptor &matetial)
{
last_moved_material[ui_build_selector->building_subtype] = matetial;
}
static MaterialDescriptor get_material_in_list(size_t i)
{
MaterialDescriptor result;
result.valid = false;
if (VIRTUAL_CAST_VAR(gen, df::build_req_choice_genst, ui_build_selector->choices[i]))
{
result.item_type = gen->item_type;
result.item_subtype = gen->item_subtype;
result.type = gen->mat_type;
result.index = gen->mat_index;
result.valid = true;
}
else if (VIRTUAL_CAST_VAR(spec, df::build_req_choice_specst, ui_build_selector->choices[i]))
{
result.item_type = spec->candidate->getType();
result.item_subtype = spec->candidate->getSubtype();
result.type = spec->candidate->getActualMaterial();
result.index = spec->candidate->getActualMaterialIndex();
result.valid = true;
}
return result;
}
static bool is_material_in_autoselect(size_t &i, MaterialDescriptor &material)
{
for (i = 0; i < get_curr_constr_prefs().size(); i++)
{
if (get_curr_constr_prefs()[i].matches(material))
return true;
}
return false;
}
static bool is_material_in_list(size_t &i, MaterialDescriptor &material)
{
const size_t size = ui_build_selector->choices.size(); //Just because material list could be very big
for (i = 0; i < size; i++)
{
if (get_material_in_list(i).matches(material))
return true;
}
return false;
}
static bool move_material_to_top(MaterialDescriptor &material)
{
size_t i;
if (is_material_in_list(i, material))
{
auto sel_item = ui_build_selector->choices[i];
ui_build_selector->choices.erase(ui_build_selector->choices.begin() + i);
ui_build_selector->choices.insert(ui_build_selector->choices.begin(), sel_item);
ui_build_selector->sel_index = 0;
set_last_moved_material(material);
return true;
}
set_last_moved_material(MaterialDescriptor());
return false;
}
static bool check_autoselect(MaterialDescriptor &material, bool toggle)
{
size_t idx;
if (is_material_in_autoselect(idx, material))
{
if (toggle)
vector_erase_at(get_curr_constr_prefs(), idx);
return true;
}
else
{
if (toggle)
get_curr_constr_prefs().push_back(material);
return false;
}
}
static void cancel_box_selection()
{
if (box_select_mode == SELECT_FIRST)
return;
box_select_mode = SELECT_FIRST;
box_select_materials.clear();
if (!show_box_selection)
Gui::setDesignationCoords(-1, -1, -1);
}
//END UI Functions
//START Building and Verification
struct building_site
{
df::coord pos;
bool in_open_air;
building_site(df::coord pos, bool in_open_air)
{
this->pos = pos;
this->in_open_air = in_open_air;
}
building_site() {}
};
static deque<building_site> valid_building_sites;
static deque<building_site> open_air_sites;
static building_site anchor;
static bool is_orthogonal_to_pending_construction(building_site &site)
{
for (deque<building_site>::iterator it = valid_building_sites.begin(); it != valid_building_sites.end(); it++)
{
if ((it->pos.x == site.pos.x && abs(it->pos.y - site.pos.y) == 1) || (it->pos.y == site.pos.y && abs(it->pos.x - site.pos.x) == 1))
{
site.in_open_air = true;
return true;
}
}
return false;
}
static df::building_constructionst *get_construction_on_tile(const df::coord &pos)
{
auto current = Buildings::findAtTile(pos);
if (current)
return strict_virtual_cast<df::building_constructionst>(current);
return NULL;
}
static df::tiletype *read_tile_shapes(const df::coord &pos, df::tiletype_shape &shape, df::tiletype_shape_basic &shape_basic)
{
if (!Maps::isValidTilePos(pos))
return NULL;
auto ttype = Maps::getTileType(pos);
if (!ttype)
return NULL;
shape = tileShape(*ttype);
shape_basic = tileShapeBasic(shape);
return ttype;
}
static bool is_valid_building_site(building_site &site, bool orthogonal_check, bool check_placed_constructions, bool in_future_placement_mode)
{
df::tiletype_shape shape;
df::tiletype_shape_basic shape_basic;
auto ttype = read_tile_shapes(site.pos, shape, shape_basic);
if (!ttype)
return false;
if (shape_basic == tiletype_shape_basic::Open)
{
if (orthogonal_check)
{
// Check if this is a valid tile to have a construction placed orthogonally to it
if (!in_future_placement_mode)
return false;
df::building_constructionst *cons = get_construction_on_tile(site.pos);
if (cons && cons->type == construction_type::Floor)
{
site.in_open_air = true;
return true;
}
return false;
}
// Stairs can be placed in open space, if they can connect to other stairs
df::tiletype_shape shape_s;
df::tiletype_shape_basic shape_basic_s;
if (ui_build_selector->building_subtype == construction_type::DownStair ||
ui_build_selector->building_subtype == construction_type::UpDownStair)
{
df::coord below(site.pos.x, site.pos.y, site.pos.z - 1);
auto ttype_s = read_tile_shapes(below, shape_s, shape_basic_s);
if (ttype_s)
{
if (shape_s == tiletype_shape::STAIR_UP || shape_s == tiletype_shape::STAIR_UPDOWN)
return true;
}
}
if (ui_build_selector->building_subtype == construction_type::UpStair ||
ui_build_selector->building_subtype == construction_type::UpDownStair)
{
df::coord above(site.pos.x, site.pos.y, site.pos.z + 1);
auto ttype_s = read_tile_shapes(above, shape_s, shape_basic_s);
if (ttype_s)
{
if (shape_s == tiletype_shape::STAIR_DOWN || shape_s == tiletype_shape::STAIR_UPDOWN)
return true;
}
}
// Check if there is a valid tile orthogonally adjacent
bool valid_orthogonal_tile_found = false;
df::coord orthagonal_pos;
orthagonal_pos.z = site.pos.z;
for (orthagonal_pos.x = site.pos.x-1; orthagonal_pos.x <= site.pos.x+1 && !valid_orthogonal_tile_found; orthagonal_pos.x++)
{
for (orthagonal_pos.y = site.pos.y-1; orthagonal_pos.y <= site.pos.y+1; orthagonal_pos.y++)
{
if ((site.pos.x == orthagonal_pos.x) == (site.pos.y == orthagonal_pos.y))
continue;
building_site orthogonal_site(orthagonal_pos, false);
if (is_valid_building_site(orthogonal_site, true, check_placed_constructions, in_future_placement_mode))
{
valid_orthogonal_tile_found = true;
if (orthogonal_site.in_open_air)
site.in_open_air = true;
break;
}
}
}
if (!(valid_orthogonal_tile_found || (check_placed_constructions && is_orthogonal_to_pending_construction(site))))
{
site.in_open_air = true;
return false;
}
}
else if (orthogonal_check)
{
if (shape != tiletype_shape::RAMP &&
shape_basic != tiletype_shape_basic::Floor &&
shape_basic != tiletype_shape_basic::Stair)
return false;
}
else
{
auto material = tileMaterial(*ttype);
if (shape == tiletype_shape::RAMP)
{
if (material == tiletype_material::CONSTRUCTION)
return false;
}
else
{
if (shape_basic != tiletype_shape_basic::Floor)
return false;
if (material == tiletype_material::CONSTRUCTION)
{
// Can build on top of a wall, but not on a constructed floor
df::coord pos_below = site.pos;
pos_below.z--;
if (!Maps::isValidTilePos(pos_below))
return false;
auto ttype = Maps::getTileType(pos_below);
if (!ttype)
return false;
auto shape = tileShape(*ttype);
auto shapeBasic = tileShapeBasic(shape);
if (tileShapeBasic(shape) != tiletype_shape_basic::Wall)
return false;
}
if (material == tiletype_material::FIRE ||
material == tiletype_material::POOL ||
material == tiletype_material::BROOK ||
material == tiletype_material::RIVER ||
material == tiletype_material::MAGMA ||
material == tiletype_material::DRIFTWOOD ||
material == tiletype_material::CAMPFIRE
)
return false;
}
}
if (orthogonal_check)
return true;
auto designation = Maps::getTileDesignation(site.pos);
if (designation->bits.flow_size > 2)
return false;
auto current = Buildings::findAtTile(site.pos);
if (current)
return false;
df::coord2d size(1,1);
return Buildings::checkFreeTiles(site.pos, size, NULL, false, false);
}
static bool find_anchor_in_spiral(const df::coord &start)
{
bool found = false;
for (anchor.pos.z = start.z; anchor.pos.z > start.z - 4; anchor.pos.z--)
{
int x, y, dx, dy;
x = y = dx = 0;
dy = -1;
const int side = 11;
const int maxI = side*side;
for (int i = 0; i < maxI; i++)
{
if (-side/2 < x && x <= side/2 && -side/2 < y && y <= side/2)
{
anchor.pos.x = start.x + x;
anchor.pos.y = start.y + y;
if (is_valid_building_site(anchor, false, false, false))
{
found = true;
break;
}
}
if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1-y)))
{
int tmp = dx;
dx = -dy;
dy = tmp;
}
x += dx;
y += dy;
}
if (found)
break;
}
return found;
}
static bool find_valid_building_sites(bool in_future_placement_mode)
{
valid_building_sites.clear();
open_air_sites.clear();
int xD = (box_second.x > box_first.x) ? 1 : -1;
int yD = (box_second.y > box_first.y) ? 1 : -1;
for (int32_t xB = box_first.x; (xD > 0) ? (xB <= box_second.x) : (xB >= box_second.x); xB += xD)
{
for (int32_t yB = box_first.y; (yD > 0) ? (yB <= box_second.y) : (yB >= box_second.y); yB += yD)
{
if (hollow_selection && !(xB == box_first.x || xB == box_second.x || yB == box_first.y || yB == box_second.y))
continue;
building_site site(df::coord(xB, yB, box_second.z), false);
if (is_valid_building_site(site, false, true, in_future_placement_mode))
valid_building_sites.push_back(site);
else if (site.in_open_air)
{
if (in_future_placement_mode)
valid_building_sites.push_back(site);
else
open_air_sites.push_back(site);
}
}
}
size_t last_open_air_count = 0;
while (valid_building_sites.size() > 0 && open_air_sites.size() != last_open_air_count)
{
last_open_air_count = open_air_sites.size();
deque<building_site> current_open_air_list = open_air_sites;
open_air_sites.clear();
for (deque<building_site>::iterator it = current_open_air_list.begin(); it != current_open_air_list.end(); it++)
{
if (is_orthogonal_to_pending_construction(*it))
valid_building_sites.push_back(*it);
else
open_air_sites.push_back(*it);
}
}
return valid_building_sites.size() > 0;
}
static bool designate_new_construction(df::coord &pos, df::construction_type &type, df::item *item)
{
auto newinst = Buildings::allocInstance(pos, building_type::Construction, type);
if (!newinst)
return false;
vector<df::item*> items;
items.push_back(item);
Maps::ensureTileBlock(pos);
if (!Buildings::constructWithItems(newinst, items))
{
delete newinst;
return false;
}
return true;
}
//END Building and Verification
//START Viewscreen Hook
struct jobutils_hook : public df::viewscreen_dwarfmodest
{
//START UI Methods
typedef df::viewscreen_dwarfmodest interpose_base;
void send_key(const df::interface_key &key)
{
set< df::interface_key > keys;
keys.insert(key);
this->feed(&keys);
}
bool select_material_at_index(size_t i)
{
ui_build_selector->sel_index = i;
std::set< df::interface_key > keys;
keys.insert(df::interface_key::SELECT_ALL);
this->feed(&keys);
return !in_material_choice_stage();
}
bool choose_materials()
{
if (!auto_choose_materials || get_curr_constr_prefs().size() == 0)
return false;
size_t size = ui_build_selector->choices.size();
for (size_t i = 0; i < size; i++)
{
MaterialDescriptor material = get_material_in_list(i);
size_t j;
if (is_material_in_autoselect(j, material))
{
return select_material_at_index(i);
}
}
return false;
}
void draw_box_selection()
{
if (!box_select_enabled)
return;
df::coord vport = Gui::getViewportPos();
//Even if selection drawing is disabled, paint a green cursor as we can place box selection anywhere
if (box_select_mode == SELECT_FIRST || (!show_box_selection && box_select_mode == SELECT_SECOND))
{
int32_t x, y, z;
if (!Gui::getCursorCoords(x, y, z))
return;
x = x - vport.x + 1;
y = y - vport.y + 1;
OutputString(COLOR_GREEN, x, y, "X");
}
else if (show_box_selection && box_select_mode == SELECT_SECOND)
{
if (!Gui::getCursorCoords(box_second.x, box_second.y, box_second.z))
return;
int32_t xD = (box_second.x > box_first.x) ? 1 : -1;
int32_t yD = (box_second.y > box_first.y) ? 1 : -1;
for (int32_t xB = box_first.x; (xD > 0) ? (xB <= box_second.x) : (xB >= box_second.x); xB += xD)
{
for (int32_t yB = box_first.y; (yD > 0) ? (yB <= box_second.y) : (yB >= box_second.y); yB += yD)
{
if (hollow_selection && !(xB == box_first.x || xB == box_second.x || yB == box_first.y || yB == box_second.y))
continue;
int8_t color = (xB == box_second.x && yB == box_second.y) ? COLOR_GREEN : COLOR_BROWN;
int32_t x = xB - vport.x + 1;
int32_t y = yB - vport.y + 1;
OutputString(color, x, y, "X");
}
}
}
else if (show_box_selection && box_select_mode == SELECT_MATERIALS)
{
for (deque<building_site>::iterator it = valid_building_sites.begin(); it != valid_building_sites.end(); it++)
{
int32_t x = it->pos.x - vport.x + 1;
int32_t y = it->pos.y - vport.y + 1;
OutputString(COLOR_GREEN, x, y, "X");
}
}
}
void reset_existing_selection()
{
for (int i = 0; i < 10; i++)
{
send_key(df::interface_key::BUILDING_DIM_Y_DOWN);
send_key(df::interface_key::BUILDING_DIM_X_DOWN);
}
}
void handle_input(set<df::interface_key> *input)
{
if (ui_build_selector->building_subtype >= 7)
return;
if (in_material_choice_stage())
{
if (input->count(interface_key::LEAVESCREEN))
{
box_select_mode = SELECT_FIRST;
}
MaterialDescriptor material = get_material_in_list(ui_build_selector->sel_index);
if (material.valid)
{
if (input->count(interface_key::SELECT) || input->count(interface_key::SELECT_ALL))
{
if (get_last_moved_material().matches(material))
last_used_moved = false; //Keep selected material on top
set_last_used_material(material);
if (box_select_enabled)
{
auto curr_index = ui_build_selector->sel_index;
vector<MaterialDescriptor> gen_material;
gen_material.push_back(get_material_in_list(curr_index));
box_select_materials.clear();
// Populate material list with selected material
populate_box_materials(gen_material, ((input->count(interface_key::SELECT_ALL) && ui_build_selector->is_grouped) ? -1 : 1));
input->clear(); // Let the apply_box_selection routine allocate the construction
input->insert(interface_key::LEAVESCREEN);
}
}
else if (input->count(interface_key::CUSTOM_A))
{
check_autoselect(material, true);
input->clear();
}
}
}
else if (in_placement_stage())
{
if (input->count(interface_key::CUSTOM_A))
{
auto_choose_materials = !auto_choose_materials;
}
else if (input->count(interface_key::CUSTOM_T))
{
revert_to_last_used_type = !revert_to_last_used_type;
}
else if (input->count(interface_key::CUSTOM_B))
{
reset_existing_selection();
box_select_enabled = !box_select_enabled;
if (!box_select_enabled)
cancel_box_selection();
return;
}
else if (input->count(interface_key::CUSTOM_O))
{
allow_future_placement = !allow_future_placement;
}
else if (input->count(interface_key::LEAVESCREEN))
{
switch (box_select_mode)
{
case SELECT_FIRST:
case SELECT_SECOND:
cancel_box_selection();
default:
break;
}
}
else if (box_select_enabled)
{
if (input->count(interface_key::SELECT))
{
switch (box_select_mode)
{
case SELECT_FIRST:
if (!Gui::getCursorCoords(box_first.x, box_first.y, box_first.z))
{
cancel_box_selection();
return;
}
box_select_mode = SELECT_SECOND;
if (!show_box_selection)
Gui::setDesignationCoords(box_first.x, box_first.y, box_first.z);
input->clear();
return;
case SELECT_SECOND:
if (!Gui::getCursorCoords(box_second.x, box_second.y, box_second.z))
{
cancel_box_selection();
return;
}
cancel_box_selection();
input->clear();
apply_box_selection(true);
return;
default:
break;
}
}
else if (input->count(interface_key::CUSTOM_X))
{
show_box_selection = !show_box_selection;
if (box_select_mode == SELECT_SECOND)
{
if (show_box_selection)
{
Gui::setDesignationCoords(-1, -1, -1);
}
else
{
Gui::setDesignationCoords(box_first.x, box_first.y, box_first.z);
}
}
}
else if (input->count(interface_key::CUSTOM_H))
{
hollow_selection = !hollow_selection;
}
else if (input->count(interface_key::BUILDING_DIM_Y_UP) ||
input->count(interface_key::BUILDING_DIM_Y_DOWN) ||
input->count(interface_key::BUILDING_DIM_X_UP) ||
input->count(interface_key::BUILDING_DIM_X_DOWN))
{
input->clear();
return;
}
}
}
}
//END UI Methods
//START Building Application
bool populate_box_materials(vector<MaterialDescriptor> &gen_materials, int32_t count = -1)
{
bool result = false;
if (gen_materials.size() == 0)
return result;
if (ui_build_selector->is_grouped)
send_key(interface_key::BUILDING_EXPAND_CONTRACT);
size_t size = ui_build_selector->choices.size();
vector<MaterialDescriptor>::iterator gen_material;
for (size_t i = 0; i < size; i++)
{
if (VIRTUAL_CAST_VAR(spec, df::build_req_choice_specst, ui_build_selector->choices[i]))
{
for (gen_material = gen_materials.begin(); gen_material != gen_materials.end(); gen_material++)
{
if (gen_material->item_type == spec->candidate->getType() &&
gen_material->item_subtype == spec->candidate->getSubtype() &&
gen_material->type == spec->candidate->getActualMaterial() &&
gen_material->index == spec->candidate->getActualMaterialIndex())
{
box_select_materials.push_back(spec->candidate);
if (count > -1)
return true; // Right now we only support 1 or all materials
result = true;
break;
}
}
}
}
send_key(interface_key::BUILDING_EXPAND_CONTRACT);
return result;
}
void move_cursor(df::coord &pos)
{
Gui::setCursorCoords(pos.x, pos.y, pos.z);
send_key(interface_key::CURSOR_DOWN_Z);
send_key(interface_key::CURSOR_UP_Z);
}
void move_cursor(coord32_t &pos)
{
df::coord c((int16_t) pos.x, (int16_t) pos.y, (int16_t) pos.z);
move_cursor(c);
}
void apply_box_selection(bool new_start)
{
static bool saved_revert_setting = false;
static bool auto_select_applied = false;
box_select_mode = SELECT_MATERIALS;
if (new_start)
{
bool ok_to_continue = false;
bool in_future_placement_mode = false;
if (!find_valid_building_sites(false))
{
if (allow_future_placement)
{
in_future_placement_mode = find_valid_building_sites(true);
}
}
else
{
ok_to_continue = true;
}
if (in_future_placement_mode)
{
ok_to_continue = find_anchor_in_spiral(valid_building_sites[0].pos);
}
else if (ok_to_continue)
{
// First valid site is guaranteed to be anchored, either on a tile or against a valid orthogonal tile
// Use it as an anchor point to generate materials list
anchor = valid_building_sites.front();
valid_building_sites.pop_front();
valid_building_sites.push_back(anchor);
}
if (!ok_to_continue)
{
cancel_box_selection();
hollow_selection = false;
return;
}
saved_revert_setting = revert_to_last_used_type;
revert_to_last_used_type = true;
auto_select_applied = false;
box_select_materials.clear();
}
while (valid_building_sites.size() > 0)
{
building_site site = valid_building_sites.front();
valid_building_sites.pop_front();
if (box_select_materials.size() > 0)
{
df::construction_type type = (df::construction_type) ui_build_selector->building_subtype;
df::item *item = NULL;
while (box_select_materials.size() > 0)
{
item = box_select_materials.front();
if (!item->flags.bits.in_job)
break;
box_select_materials.pop_front();
item = NULL;
}
if (item != NULL)
{
if (designate_new_construction(site.pos, type, item))
{
box_select_materials.pop_front();
box_select_mode = AUTOSELECT_MATERIALS;
send_key(interface_key::LEAVESCREEN); //Must do this to register items in use
send_key(hotkeys[type]);
box_select_mode = SELECT_MATERIALS;
}
continue;
}
}
// Generate material list using regular construction placement routine
if (site.in_open_air)
{
// Cannot invoke material selection on an unconnected tile, use anchor instead
move_cursor(anchor.pos);
send_key(df::interface_key::SELECT);