forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGui.cpp
More file actions
2227 lines (1913 loc) · 70.9 KB
/
Gui.cpp
File metadata and controls
2227 lines (1913 loc) · 70.9 KB
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
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <string>
#include <vector>
#include <map>
using namespace std;
#include "modules/Gui.h"
#include "MemAccess.h"
#include "VersionInfo.h"
#include "Types.h"
#include "Error.h"
#include "ModuleFactory.h"
#include "Core.h"
#include "Debug.h"
#include "PluginManager.h"
#include "MiscUtils.h"
using namespace DFHack;
#include "modules/Job.h"
#include "modules/Screen.h"
#include "modules/Maps.h"
#include "modules/Units.h"
#include "modules/World.h"
#include "DataDefs.h"
#include "df/announcement_flags.h"
#include "df/building_cagest.h"
#include "df/building_civzonest.h"
#include "df/building_furnacest.h"
#include "df/building_trapst.h"
#include "df/building_type.h"
#include "df/building_workshopst.h"
#include "df/cri_unitst.h"
#include "df/d_init.h"
#include "df/game_mode.h"
#include "df/general_ref.h"
#include "df/global_objects.h"
#include "df/graphic.h"
#include "df/graphic_viewportst.h"
#include "df/historical_figure.h"
#include "df/interfacest.h"
#include "df/item_corpsepiecest.h"
#include "df/item_corpsest.h"
#include "df/job.h"
#include "df/legend_pagest.h"
#include "df/occupation.h"
#include "df/plant.h"
#include "df/popup_message.h"
#include "df/report.h"
#include "df/report_zoom_type.h"
#include "df/route_stockpile_link.h"
#include "df/stop_depart_condition.h"
#include "df/adventurest.h"
#include "df/buildreq.h"
#include "df/ui_look_list.h"
#include "df/gamest.h"
#include "df/ui_unit_view_mode.h"
#include "df/unit.h"
#include "df/unit_inventory_item.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/viewscreen_legendsst.h"
#include "df/viewscreen_new_regionst.h"
#include "df/viewscreen_titlest.h"
#include "df/world.h"
const size_t MAX_REPORTS_SIZE = 3000; // DF clears old reports to maintain this vector size
const int32_t RECENT_REPORT_TICKS = 500; // used by UNIT_COMBAT_REPORT_ALL_ACTIVE
const int32_t ANNOUNCE_LINE_DURATION = 100; // time to display each line in announcement bar; 2 sec at 50 GFPS
const int16_t ANNOUNCE_DISPLAY_TIME = 2000; // DF uses this value for most announcements; 40 sec at 50 GFPS
namespace DFHack
{
DBG_DECLARE(core, gui, DebugCategory::LINFO);
}
using namespace df::enums;
using df::building_civzonest;
using df::global::game;
using df::global::gamemode;
using df::global::gps;
using df::global::gview;
using df::global::init;
using df::global::plotinfo;
using df::global::selection_rect;
using df::global::ui_menu_width;
using df::global::world;
/* TODO: understand how this changes for v50
static df::layer_object_listst *getLayerList(df::viewscreen_layer *layer, int idx)
{
return virtual_cast<df::layer_object_listst>(vector_get(layer->layer_objects,idx));
}
*/
static std::string getNameChunk(virtual_identity *id, int start, int end)
{
if (!id)
return "UNKNOWN";
const char *name = id->getName();
int len = strlen(name);
if (len > start + end)
return std::string(name+start, len-start-end);
else
return name;
}
/*
* Classifying focus context by means of a string path.
*/
typedef void (*getFocusStringsHandler)(std::string &str, std::vector<std::string> &strList, df::viewscreen *screen);
static std::map<virtual_identity*, getFocusStringsHandler> getFocusStringsHandlers;
#define VIEWSCREEN(name) df::viewscreen_##name##st
#define DEFINE_GET_FOCUS_STRING_HANDLER(screen_type) \
static void getFocusStrings_##screen_type(std::string &baseFocus, std::vector<std::string> &focusStrings, VIEWSCREEN(screen_type) *screen);\
DFHACK_STATIC_ADD_TO_MAP(\
&getFocusStringsHandlers, &VIEWSCREEN(screen_type)::_identity, \
(getFocusStringsHandler)getFocusStrings_##screen_type \
); \
static void getFocusStrings_##screen_type(std::string &baseFocus, std::vector<std::string> &focusStrings, VIEWSCREEN(screen_type) *screen)
DEFINE_GET_FOCUS_STRING_HANDLER(title)
{
if (screen->managing_mods)
focusStrings.push_back(baseFocus + "/Mods");
else if (game->main_interface.settings.open)
focusStrings.push_back(baseFocus + "/Settings");
if (focusStrings.empty())
focusStrings.push_back(baseFocus + "/Default");
}
DEFINE_GET_FOCUS_STRING_HANDLER(new_region)
{
if (screen->doing_mods)
focusStrings.push_back(baseFocus + "/Mods");
else if (screen->doing_simple_params)
focusStrings.push_back(baseFocus + "/Basic");
else if (screen->doing_params)
focusStrings.push_back(baseFocus + "/Advanced");
if (focusStrings.empty())
focusStrings.push_back(baseFocus);
}
DEFINE_GET_FOCUS_STRING_HANDLER(legends)
{
if (screen->init_stage != -1)
focusStrings.push_back(baseFocus + "/Loading");
else if (screen->page.size() <= 1)
focusStrings.push_back(baseFocus + "/Default");
else
focusStrings.push_back(baseFocus + '/' + screen->page[screen->active_page_index]->header);
}
DEFINE_GET_FOCUS_STRING_HANDLER(dwarfmode)
{
std::string newFocusString;
if(game->main_interface.main_designation_selected != -1) {
newFocusString = baseFocus;
newFocusString += "/Designate/" + enum_item_key(game->main_interface.main_designation_selected);
focusStrings.push_back(newFocusString);
}
if (game->main_interface.info.open) {
newFocusString = baseFocus;
newFocusString += "/Info";
newFocusString += '/' + enum_item_key(game->main_interface.info.current_mode);
switch(game->main_interface.info.current_mode) {
case df::enums::info_interface_mode_type::CREATURES:
newFocusString += '/' + enum_item_key(game->main_interface.info.creatures.current_mode);
break;
case df::enums::info_interface_mode_type::BUILDINGS:
newFocusString += '/' + enum_item_key(game->main_interface.info.buildings.mode);
break;
case df::enums::info_interface_mode_type::LABOR:
newFocusString += '/' + enum_item_key(game->main_interface.info.labor.mode);
break;
case df::enums::info_interface_mode_type::ARTIFACTS:
newFocusString += '/' + enum_item_key(game->main_interface.info.artifacts.mode);
break;
case df::enums::info_interface_mode_type::JUSTICE:
newFocusString += '/' + enum_item_key(game->main_interface.info.justice.current_mode);
break;
case df::enums::info_interface_mode_type::WORK_ORDERS:
if (game->main_interface.info.work_orders.conditions.open)
newFocusString += "/Conditions";
else if (game->main_interface.create_work_order.open)
newFocusString += "/Create";
else
newFocusString += "/Default";
break;
default:
break;
}
focusStrings.push_back(newFocusString);
}
if (game->main_interface.view_sheets.open) {
newFocusString = baseFocus;
newFocusString += "/ViewSheets";
newFocusString += '/' + enum_item_key(game->main_interface.view_sheets.active_sheet);
switch (game->main_interface.view_sheets.active_sheet) {
case df::view_sheet_type::UNIT:
switch (game->main_interface.view_sheets.active_sub_tab) {
case 0: newFocusString += "/Overview"; break;
case 1: newFocusString += "/Items"; break;
case 2:
newFocusString += "/Health";
switch (game->main_interface.view_sheets.unit_health_active_tab) {
case 0: newFocusString += "/Status"; break;
case 1: newFocusString += "/Wounds"; break;
case 2: newFocusString += "/Treatment"; break;
case 3: newFocusString += "/History"; break;
case 4: newFocusString += "/Description"; break;
default: break;
}
break;
case 3:
newFocusString += "/Skills";
switch (game->main_interface.view_sheets.unit_skill_active_tab) {
case 0: newFocusString += "/Labor"; break;
case 1: newFocusString += "/Combat"; break;
case 2: newFocusString += "/Social"; break;
case 3: newFocusString += "/Other"; break;
case 4:
newFocusString += "/Knowledge";
if (game->main_interface.view_sheets.skill_description_raw_str.size())
newFocusString += "/Details";
else
newFocusString += "/Default";
break;
default: break;
}
break;
case 4: newFocusString += "/Rooms"; break;
case 5:
newFocusString += "/Labor";
switch (game->main_interface.view_sheets.unit_labor_active_tab) {
case 0: newFocusString += "/WorkDetails"; break;
case 1: newFocusString += "/Workshops"; break;
case 2: newFocusString += "/Locations"; break;
case 3: newFocusString += "/WorkAnimals"; break;
default: break;
}
break;
case 6: newFocusString += "/Relations"; break;
case 7: newFocusString += "/Groups"; break;
case 8:
newFocusString += "/Military";
switch (game->main_interface.view_sheets.unit_military_active_tab) {
case 0: newFocusString += "/Squad"; break;
case 1: newFocusString += "/Uniform"; break;
case 2: newFocusString += "/Kills"; break;
default: break;
}
break;
case 9:
newFocusString += "/Thoughts";
switch (game->main_interface.view_sheets.thoughts_active_tab) {
case 0: newFocusString += "/Recent"; break;
case 1: newFocusString += "/Memories"; break;
default: break;
}
break;
case 10:
newFocusString += "/Personality";
switch (game->main_interface.view_sheets.personality_active_tab) {
case 0: newFocusString += "/Traits"; break;
case 1: newFocusString += "/Values"; break;
case 2: newFocusString += "/Preferences"; break;
case 3: newFocusString += "/Needs"; break;
default: break;
}
break;
default:
break;
}
break;
case df::view_sheet_type::BUILDING:
if (auto bld = df::building::find(game->main_interface.view_sheets.viewing_bldid))
newFocusString += '/' + enum_item_key(bld->getType());
break;
default:
break;
}
focusStrings.push_back(newFocusString);
}
if(game->main_interface.bottom_mode_selected != -1) {
newFocusString = baseFocus;
switch(game->main_interface.bottom_mode_selected) {
case df::enums::main_bottom_mode_type::STOCKPILE:
if (game->main_interface.stockpile.cur_bld) {
newFocusString += "/Some";
}
newFocusString += "/Stockpile";
break;
case df::enums::main_bottom_mode_type::STOCKPILE_PAINT:
newFocusString += "/Stockpile/Paint";
break;
case df::enums::main_bottom_mode_type::HAULING:
newFocusString += "/Hauling";
break;
case df::enums::main_bottom_mode_type::ZONE:
newFocusString += "/Zone";
if (game->main_interface.civzone.cur_bld) {
newFocusString += "/Some";
newFocusString += '/' + enum_item_key(game->main_interface.civzone.cur_bld->type);
}
break;
case df::enums::main_bottom_mode_type::ZONE_PAINT:
newFocusString += "/Zone/Paint";
// TODO: figure out why enum_item_key doesn't work on this?
switch(game->main_interface.civzone.adding_new_type) {
case df::enums::civzone_type::MeetingHall:
newFocusString += "/MeetingHall";
break;
case df::enums::civzone_type::Bedroom:
newFocusString += "/Bedroom";
break;
case df::enums::civzone_type::DiningHall:
newFocusString += "/DiningHall";
break;
case df::enums::civzone_type::Pen:
newFocusString += "/Pen";
break;
case df::enums::civzone_type::Pond:
newFocusString += "/Pond";
break;
case df::enums::civzone_type::WaterSource:
newFocusString += "/WaterSource";
break;
case df::enums::civzone_type::Dungeon:
newFocusString += "/Dungeon";
break;
case df::enums::civzone_type::FishingArea:
newFocusString += "/FishingArea";
break;
case df::enums::civzone_type::SandCollection:
newFocusString += "/SandCollection";
break;
case df::enums::civzone_type::Office:
newFocusString += "/Office";
break;
case df::enums::civzone_type::Dormitory:
newFocusString += "/Dormitory";
break;
case df::enums::civzone_type::Barracks:
newFocusString += "/Barracks";
break;
case df::enums::civzone_type::ArcheryRange:
newFocusString += "/ArcheryRange";
break;
case df::enums::civzone_type::Dump:
newFocusString += "/Dump";
break;
case df::enums::civzone_type::AnimalTraining:
newFocusString += "/AnimalTraining";
break;
case df::enums::civzone_type::Tomb:
newFocusString += "/Tomb";
break;
case df::enums::civzone_type::PlantGathering:
newFocusString += "/PlantGathering";
break;
case df::enums::civzone_type::ClayCollection:
newFocusString += "/ClayCollection";
break;
}
break;
case df::enums::main_bottom_mode_type::BURROW:
newFocusString += "/Burrow";
break;
case df::enums::main_bottom_mode_type::BURROW_PAINT:
newFocusString += "/Burrow/Paint";
break;
case df::enums::main_bottom_mode_type::BUILDING:
newFocusString += "/Building";
break;
case df::enums::main_bottom_mode_type::BUILDING_PLACEMENT:
newFocusString += "/Building/Placement";
break;
case df::enums::main_bottom_mode_type::BUILDING_PICK_MATERIALS:
newFocusString += "/Building/PickMaterials";
break;
default:
break;
}
focusStrings.push_back(newFocusString);
}
if (game->main_interface.trade.open) {
newFocusString = baseFocus;
newFocusString += "/Trade";
if (game->main_interface.trade.choosing_merchant)
newFocusString += "/ChoosingMerchant";
else
newFocusString += "/Default";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.job_details.open) {
newFocusString = baseFocus;
newFocusString += "/JobDetails/" + enum_item_key(game->main_interface.job_details.context);
focusStrings.push_back(newFocusString);
}
if (game->main_interface.assign_trade.open) {
newFocusString = baseFocus;
newFocusString += "/AssignTrade";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.diplomacy.open) {
newFocusString = baseFocus;
newFocusString += "/Diplomacy";
if (game->main_interface.diplomacy.taking_requests)
newFocusString += "/Requests";
else
newFocusString += "/Default";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.petitions.open) {
newFocusString = baseFocus;
newFocusString += "/Petitions";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.stocks.open) {
newFocusString = baseFocus;
newFocusString += "/Stocks";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.assign_display_item.open) {
newFocusString = baseFocus;
newFocusString += "/AssignDisplayItem";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.name_creator.open) {
newFocusString = baseFocus;
newFocusString += "/NameCreator";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.image_creator.open) {
newFocusString = baseFocus;
newFocusString += "/ImageCreator";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.unit_selector.open) {
newFocusString = baseFocus;
newFocusString += "/UnitSelector";
newFocusString += '/' + enum_item_key(game->main_interface.unit_selector.context);
focusStrings.push_back(newFocusString);
}
if (game->main_interface.announcement_alert.open) {
newFocusString = baseFocus;
newFocusString += "/AnnouncementAlert";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.custom_symbol.open) {
newFocusString = baseFocus;
newFocusString += "/CustomSymbol";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.patrol_routes.open) {
newFocusString = baseFocus;
newFocusString += "/PatrolRoutes";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.squad_schedule.open) {
newFocusString = baseFocus;
newFocusString += "/SquadSchedule";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.squad_selector.open) {
newFocusString = baseFocus;
newFocusString += "/SquadSelector";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.burrow_selector.open) {
newFocusString = baseFocus;
newFocusString += "/BurrowSelector";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.location_selector.open) {
newFocusString = baseFocus;
newFocusString += "/LocationSelector";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.location_details.open) {
newFocusString = baseFocus;
newFocusString += "/LocationDetails";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.hauling_stop_conditions.open) {
newFocusString = baseFocus;
newFocusString += "/HaulingStopConditions";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.assign_vehicle.open) {
newFocusString = baseFocus;
newFocusString += "/AssignVehicle";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.stockpile_link.open) {
newFocusString = baseFocus;
newFocusString += "/StockpileLink";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.stockpile_tools.open) {
newFocusString = baseFocus;
newFocusString += "/StockpileTools";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.custom_stockpile.open) {
newFocusString = baseFocus;
newFocusString += "/CustomStockpile";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.create_squad.open) {
newFocusString = baseFocus;
newFocusString += "/CreateSquad";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.squad_supplies.open) {
newFocusString = baseFocus;
newFocusString += "/SquadSupplies";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.assign_uniform.open) {
newFocusString = baseFocus;
newFocusString += "/AssignUniform";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.hotkey.open) {
newFocusString = baseFocus;
newFocusString += "/Hotkey";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.options.open) {
newFocusString = baseFocus;
newFocusString += "/Options";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.help.open) {
newFocusString = baseFocus;
newFocusString += "/Help";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.settings.open) {
newFocusString = baseFocus;
newFocusString += "/Settings";
focusStrings.push_back(newFocusString);
}
if (game->main_interface.squad_equipment.open) {
newFocusString = baseFocus;
newFocusString += "/SquadEquipment";
focusStrings.push_back(newFocusString);
}
// squads should be last because it's the only one not exclusive with the others? or something?
if (game->main_interface.squads.open) {
newFocusString = baseFocus;
newFocusString += "/Squads";
focusStrings.push_back(newFocusString);
}
if (!newFocusString.size()) {
focusStrings.push_back(baseFocus + "/Default");
}
}
/* TODO: understand how this changes for v50
DEFINE_GET_FOCUS_STRING_HANDLER(dungeonmode)
{
using df::global::adventure;
if (!adventure)
return;
focus += '/' + enum_item_key(adventure->menu);
}
*/
bool Gui::matchFocusString(std::string focus_string, df::viewscreen *top) {
focus_string = toLower(focus_string);
if (!top)
top = getCurViewscreen(true);
std::vector<std::string> currentFocusStrings = getFocusStrings(top);
return std::find_if(currentFocusStrings.begin(), currentFocusStrings.end(), [&focus_string](std::string item) {
return prefix_matches(focus_string, toLower(item));
}) != currentFocusStrings.end();
}
static void push_dfhack_focus_string(dfhack_viewscreen *vs, std::vector<std::string> &focusStrings)
{
auto name = vs->getFocusString();
if (name.empty())
name = "dfhack";
else if (string::npos == name.find("dfhack/"))
name = "dfhack/" + name;
focusStrings.push_back(name);
}
std::vector<std::string> Gui::getFocusStrings(df::viewscreen* top)
{
std::vector<std::string> focusStrings;
if (!top)
return focusStrings;
if (dfhack_viewscreen::is_instance(top))
{
dfhack_viewscreen *vs = static_cast<dfhack_viewscreen*>(top);
if (vs->isFocused())
{
push_dfhack_focus_string(vs, focusStrings);
return focusStrings;
}
top = Gui::getDFViewscreen(top);
if (dfhack_viewscreen::is_instance(top))
{
push_dfhack_focus_string(static_cast<dfhack_viewscreen*>(top), focusStrings);
return focusStrings;
}
}
if (virtual_identity *id = virtual_identity::get(top))
{
std::string name = getNameChunk(id, 11, 2);
auto handler = map_find(getFocusStringsHandlers, id);
if (handler)
handler(name, focusStrings, top);
}
if (!focusStrings.size())
{
Core &core = Core::getInstance();
std::string name = core.p->readClassName(*(void**)top);
focusStrings.push_back(name.substr(11, name.size()-11-2));
}
return focusStrings;
}
// Predefined common guard functions
bool Gui::default_hotkey(df::viewscreen *top)
{
return World::isFortressMode() || World::isAdventureMode();
}
bool Gui::anywhere_hotkey(df::viewscreen *) {
return true;
}
bool Gui::dwarfmode_hotkey(df::viewscreen *top) {
return matchFocusString("dwarfmode", top);
}
static bool has_cursor()
{
return Gui::getCursorPos().isValid();
}
bool Gui::cursor_hotkey(df::viewscreen *top)
{
if (!dwarfmode_hotkey(top))
return false;
// Also require the cursor.
if (!has_cursor())
return false;
return true;
}
bool Gui::workshop_job_hotkey(df::viewscreen *top)
{
if (!dwarfmode_hotkey(top))
return false;
df::building *selected = getAnyBuilding(top);
if (!virtual_cast<df::building_workshopst>(selected) &&
!virtual_cast<df::building_furnacest>(selected))
return false;
if (selected->jobs.empty() ||
selected->jobs[0]->job_type == job_type::DestroyBuilding)
return false;
return true;
}
bool Gui::build_selector_hotkey(df::viewscreen *top)
{
using df::global::buildreq;
if (!dwarfmode_hotkey(top))
return false;
if (buildreq->building_type < 0 ||
buildreq->stage != 2 ||
buildreq->choices.empty())
return false;
return true;
}
bool Gui::view_unit_hotkey(df::viewscreen *top)
{
if (!dwarfmode_hotkey(top))
return false;
return !!getAnyUnit(top);
}
bool Gui::any_job_hotkey(df::viewscreen *top)
{
return matchFocusString("dwarfmode/Info/JOBS", top)
|| matchFocusString("dwarfmode/Info/CREATURES/CITIZEN", top)
|| workshop_job_hotkey(top);
}
df::job *Gui::getSelectedWorkshopJob(color_ostream &out, bool quiet)
{
auto bld = getSelectedBuilding(out, true);
if (!bld)
return NULL;
// no way to select a specific job; just get the first one
return bld->jobs.size() ? bld->jobs[0] : NULL;
}
df::job *Gui::getSelectedJob(color_ostream &out, bool quiet)
{
using df::global::game;
auto top = Core::getTopViewscreen();
if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedJob();
if (matchFocusString("dwarfmode/Info/JOBS")) {
auto &cri_job = game->main_interface.info.jobs.cri_job;
// no way to select specific jobs; just get the first one
return cri_job.size() ? cri_job[0]->jb : NULL;
}
if (auto unit = getAnyUnit(top)) {
df::job *job = unit->job.current_job;
if (!job && !quiet)
out.printerr("Selected unit has no job\n");
return job;
}
return getSelectedWorkshopJob(out, quiet);
}
df::unit *Gui::getAnyUnit(df::viewscreen *top)
{
using df::global::game;
if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedUnit();
if (game->main_interface.view_sheets.open
&& game->main_interface.view_sheets.active_sheet == view_sheet_type::UNIT)
return df::unit::find(game->main_interface.view_sheets.active_id);
/* TODO: understand how this changes for v50
using namespace ui_sidebar_mode;
using df::global::ui_look_cursor;
using df::global::ui_look_list;
using df::global::ui_selected_unit;
using df::global::ui_building_in_assign;
using df::global::ui_building_assign_units;
using df::global::ui_building_item_cursor;
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_unitst, top))
{
return screen->unit;
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_joblistst, top))
{
if (auto unit = vector_get(screen->units, screen->cursor_pos))
return unit;
if (auto job = vector_get(screen->jobs, screen->cursor_pos))
return Job::getWorker(job);
return NULL;
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_unitlistst, top))
return vector_get(screen->units[screen->page], screen->cursor_pos[screen->page]);
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_dungeon_monsterstatusst, top))
return screen->unit;
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_layer_unit_relationshipst, top))
{
if (VIRTUAL_CAST_VAR(list, df::layer_object_listst, vector_get(screen->layer_objects, 0)))
return vector_get(screen->relation_unit, list->cursor);
return NULL;
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_itemst, top))
{
df::general_ref *ref = vector_get(screen->entry_ref, screen->cursor_pos);
return ref ? ref->getUnit() : NULL;
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_workshop_profilest, top))
{
if (screen->tab == df::viewscreen_workshop_profilest::Workers)
return vector_get(screen->workers, screen->worker_idx);
return NULL;
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_layer_noblelistst, top))
{
switch (screen->mode)
{
case df::viewscreen_layer_noblelistst::List:
if (auto list1 = getLayerList(screen, 0))
{
if (auto info = vector_get(screen->info, list1->cursor))
return info->unit;
}
return NULL;
case df::viewscreen_layer_noblelistst::Appoint:
if (auto list2 = getLayerList(screen, 1))
{
if (auto info = vector_get(screen->candidates, list2->cursor))
return info->unit;
}
return NULL;
default:
return NULL;
}
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_locationsst, top))
{
switch (screen->menu)
{
case df::viewscreen_locationsst::AssignOccupation:
return vector_get(screen->units, screen->unit_idx);
case df::viewscreen_locationsst::Occupations:
{
auto occ = vector_get(screen->occupations, screen->occupation_idx);
if (occ)
return df::unit::find(occ->unit_id);
return NULL;
}
default:
return NULL;
}
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_petst, top))
{
df::viewscreen_petst::T_animal animal_default;
animal_default.unit = NULL;
switch (screen->mode)
{
case df::viewscreen_petst::List:
if (!vector_get(screen->is_vermin, screen->cursor))
return vector_get(screen->animal, screen->cursor, animal_default).unit;
return NULL;
case df::viewscreen_petst::SelectTrainer:
return vector_get(screen->trainer_unit, screen->trainer_cursor);
default:
return NULL;
}
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_layer_overall_healthst, top))
{
if (auto list1 = getLayerList(screen, 0))
return vector_get(screen->unit, list1->cursor);
return NULL;
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_textviewerst, top))
{
if (screen->parent)
return getAnyUnit(screen->parent);
return NULL;
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_reportlistst, top))
return vector_get(screen->units, screen->cursor);
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_announcelistst, top))
{
if (world && screen->unit) {
// in (r)eports -> enter
auto *report = vector_get(screen->reports, screen->sel_idx);
if (report)
{
for (df::unit *unit : world->units.all)
{
if (unit && screen->report_type >= 0 && screen->report_type < 3
&& unit != screen->unit) // find 'other' unit related to this report
{
for (int32_t report_id : unit->reports.log[screen->report_type])
{
if (report_id == report->id)
return unit;
}
}
}
}
} else {
// in (a)nnouncements
return NULL; // cannot determine unit from reports
}
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_layer_militaryst, top))
{
if (screen->page == df::viewscreen_layer_militaryst::T_page::Positions) {
auto positions = getLayerList(screen, 1);
if (positions && positions->enabled && positions->active)
return vector_get(screen->positions.assigned, positions->cursor);
auto candidates = getLayerList(screen, 2);
if (candidates && candidates->enabled && candidates->active)
return vector_get(screen->positions.candidates, candidates->cursor);
}
if (screen->page == df::viewscreen_layer_militaryst::T_page::Equip) {
auto positions = getLayerList(screen, 1);
if (positions && positions->enabled && positions->active)
return vector_get(screen->equip.units, positions->cursor);
}
}
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_layer_unit_healthst, top))
return screen->unit;
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_customize_unitst, top))
return screen->unit;
if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedUnit();
if (!Gui::dwarfmode_hotkey(top))
return NULL;
if (!plotinfo)
return NULL;
// general assigning units in building, i.e. (q)uery cage -> (a)ssign
if (ui_building_in_assign && *ui_building_in_assign
&& ui_building_assign_units && ui_building_item_cursor
&& plotinfo->main.mode != Zones) // dont show for (i) zone
return vector_get(*ui_building_assign_units, *ui_building_item_cursor);
if (plotinfo->follow_unit != -1)
return df::unit::find(plotinfo->follow_unit);
switch (plotinfo->main.mode) {