forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stocks.cpp
1493 lines (1264 loc) · 42.7 KB
/
stocks.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 "uicommon.h"
#include "listcolumn.h"
#include <functional>
// DF data structure definition headers
#include "DataDefs.h"
#include "Types.h"
#include "df/item.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/viewscreen_storesst.h"
#include "df/items_other_id.h"
#include "df/job.h"
#include "df/unit.h"
#include "df/world.h"
#include "df/item_quality.h"
#include "df/caravan_state.h"
#include "df/mandate.h"
#include "df/general_ref_building_holderst.h"
#include "modules/Gui.h"
#include "modules/Items.h"
#include "modules/Job.h"
#include "modules/World.h"
#include "modules/Screen.h"
#include "modules/Maps.h"
#include "modules/Units.h"
#include "df/building_cagest.h"
#include "df/adventurest.h"
DFHACK_PLUGIN("stocks");
#define PLUGIN_VERSION 0.13
REQUIRE_GLOBAL(world);
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
#define MAX_NAME 30
#define SIDEBAR_WIDTH 30
/*
* Utility
*/
static string get_quality_name(const df::item_quality quality)
{
if (gps->dimx - SIDEBAR_WIDTH < 60)
return int_to_string(quality);
else
return ENUM_KEY_STR(item_quality, quality);
}
static df::item *get_container_of(df::item *item)
{
auto container = Items::getContainer(item);
return (container) ? container : item;
}
static df::item *get_container_of(df::unit *unit)
{
auto ref = Units::getGeneralRef(unit, general_ref_type::CONTAINED_IN_ITEM);
return (ref) ? ref->getItem() : nullptr;
}
/*
* Trade Info
*/
class TradeDepotInfo
{
public:
TradeDepotInfo()
{
reset();
}
void prepareTradeVariables()
{
reset();
for(auto bld_it = world->buildings.all.begin(); bld_it != world->buildings.all.end(); bld_it++)
{
auto bld = *bld_it;
if (!isUsableDepot(bld))
continue;
depot = bld;
id = depot->id;
trade_possible = can_trade();
break;
}
}
bool assignItem(vector<df::item *> &entries)
{
for (auto it = entries.begin(); it != entries.end(); it++)
{
auto item = *it;
item = get_container_of(item);
if (!Items::canTradeWithContents(item))
return false;
auto href = df::allocate<df::general_ref_building_holderst>();
if (!href)
return false;
auto job = new df::job();
df::coord tpos(depot->centerx, depot->centery, depot->z);
job->pos = tpos;
job->job_type = job_type::BringItemToDepot;
// job <-> item link
if (!Job::attachJobItem(job, item, df::job_item_ref::Hauled))
{
delete job;
delete href;
return false;
}
// job <-> building link
href->building_id = id;
depot->jobs.push_back(job);
job->general_refs.push_back(href);
// add to job list
Job::linkIntoWorld(job);
}
return true;
}
void reset()
{
depot = 0;
trade_possible = false;
}
bool canTrade()
{
return trade_possible;
}
private:
int32_t id;
df::building *depot;
bool trade_possible;
bool isUsableDepot(df::building* bld)
{
if (bld->getType() != building_type::TradeDepot)
return false;
if (bld->getBuildStage() < bld->getMaxBuildStage())
return false;
if (bld->jobs.size() == 1 && bld->jobs[0]->job_type == job_type::DestroyBuilding)
return false;
return true;
}
};
static TradeDepotInfo depot_info;
/*
* Item manipulation
*/
static map<df::item *, bool> items_in_cages;
static df::job *get_item_job(df::item *item)
{
auto ref = Items::getSpecificRef(item, specific_ref_type::JOB);
if (ref && ref->data.job)
return ref->data.job;
return nullptr;
}
static bool is_marked_for_trade(df::item *item, df::item *container = nullptr)
{
item = (container) ? container : get_container_of(item);
auto job = get_item_job(item);
if (!job)
return false;
return job->job_type == job_type::BringItemToDepot;
}
static bool is_in_inventory(df::item *item)
{
item = get_container_of(item);
return item->flags.bits.in_inventory;
}
static bool is_item_in_cage_cache(df::item *item)
{
return items_in_cages.find(item) != items_in_cages.end();
}
static string get_item_label(df::item *item, bool truncate, bool trim)
{
auto label = Items::getBookTitle(item);
if (label == "")
{
label = Items::getDescription(item, 0, false);
}
if (trim && item->getType() == item_type::BIN)
{
auto pos = label.find("<#");
if (pos != string::npos)
{
label = label.substr(0, pos-1);
}
}
auto wear = item->getWear();
if (wear > 0)
{
string wearX;
switch (wear)
{
case 1:
wearX = "x";
break;
case 2:
wearX = "X";
break;
case 3:
wearX = "xX";
break;
default:
wearX = "XX";
break;
}
label = wearX + label + wearX;
}
label = pad_string(label, MAX_NAME, false, truncate);
return label;
}
static string get_keywords(df::item *item)
{
string keywords;
if (item->flags.bits.in_job)
keywords += "job ";
if (item->flags.bits.rotten)
keywords += "rotten ";
if (item->flags.bits.owned)
keywords += "owned ";
if (item->flags.bits.forbid)
keywords += "forbid ";
if (item->flags.bits.dump)
keywords += "dump ";
if (item->flags.bits.on_fire)
keywords += "fire ";
if (item->flags.bits.melt)
keywords += "melt ";
if (is_item_in_cage_cache(item))
keywords += "caged ";
if (is_in_inventory(item))
keywords += "inventory ";
if (depot_info.canTrade())
{
if (is_marked_for_trade(item))
keywords += "trade ";
}
return keywords + get_item_label(item, false, false);
}
struct item_grouped_entry
{
std::vector<df::item *> entries;
string getLabel(bool grouped) const
{
if (entries.size() == 0)
return "";
return get_item_label(entries[0], true, grouped);
}
string getKeywords() const
{
return get_keywords(entries[0]);
}
df::item *getFirstItem() const
{
if (entries.size() == 0)
return nullptr;
return entries[0];
}
bool canMelt() const
{
df::item *item = getFirstItem();
if (!item)
return false;
return can_melt(item);
}
bool isSetToMelt() const
{
df::item *item = getFirstItem();
if (!item)
return false;
return is_set_to_melt(item);
}
bool contains(df::item *item) const
{
return std::find(entries.begin(), entries.end(), item) != entries.end();
}
void setFlags(const df::item_flags flags, const bool state)
{
for (auto it = entries.begin(); it != entries.end(); it++)
{
if (state)
(*it)->flags.whole |= flags.whole;
else
(*it)->flags.whole &= ~flags.whole;
}
}
bool isSingleItem()
{
return entries.size() == 1;
}
};
struct extra_filters
{
bool hide_trade_marked, hide_in_inventory, hide_in_cages;
extra_filters()
{
reset();
}
void reset()
{
hide_in_inventory = false;
hide_trade_marked = false;
}
};
static bool cages_populated = false;
static vector<df::building_cagest *> cages;
static void find_cages()
{
if (cages_populated)
return;
for (size_t b=0; b < world->buildings.all.size(); b++)
{
df::building* building = world->buildings.all[b];
if (building->getType() == building_type::Cage)
{
cages.push_back(static_cast<df::building_cagest *>(building));
}
}
cages_populated = true;
}
static df::building_cagest *is_in_cage(df::unit *unit)
{
find_cages();
for (auto it = cages.begin(); it != cages.end(); it++)
{
auto cage = *it;
for (size_t c = 0; c < cage->assigned_units.size(); c++)
{
if(cage->assigned_units[c] == unit->id)
return cage;
}
}
return nullptr;
}
template <class T>
class StockListColumn : public ListColumn<T>
{
virtual void display_extras(const T &item_group, int32_t &x, int32_t &y) const
{
auto item = item_group->getFirstItem();
if (item->flags.bits.in_job)
OutputString(COLOR_LIGHTBLUE, x, y, "J");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (item->flags.bits.rotten)
OutputString(COLOR_CYAN, x, y, "X");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (item->flags.bits.owned)
OutputString(COLOR_GREEN, x, y, "O");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (item->flags.bits.forbid)
OutputString(COLOR_RED, x, y, "F");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (item->flags.bits.dump)
OutputString(COLOR_LIGHTMAGENTA, x, y, "D");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (item->flags.bits.on_fire)
OutputString(COLOR_LIGHTRED, x, y, "R");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (item->flags.bits.melt)
OutputString(COLOR_BLUE, x, y, "M");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (is_in_inventory(item))
OutputString(COLOR_WHITE, x, y, "I");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (is_item_in_cage_cache(item))
OutputString(COLOR_LIGHTRED, x, y, "C");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
if (depot_info.canTrade())
{
if (is_marked_for_trade(item))
OutputString(COLOR_LIGHTGREEN, x, y, "T");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
}
if (item->isImproved())
OutputString(COLOR_BLUE, x, y, "* ");
else
OutputString(COLOR_LIGHTBLUE, x, y, " ");
auto quality = static_cast<df::item_quality>(item->getQuality());
if (quality > item_quality::Ordinary)
{
auto color = COLOR_BROWN;
switch(quality)
{
case item_quality::FinelyCrafted:
color = COLOR_CYAN;
break;
case item_quality::Superior:
color = COLOR_LIGHTBLUE;
break;
case item_quality::Exceptional:
color = COLOR_GREEN;
break;
case item_quality::Masterful:
color = COLOR_LIGHTGREEN;
break;
case item_quality::Artifact:
color = COLOR_BLUE;
break;
default:
break;
}
OutputString(color, x, y, get_quality_name(quality));
}
}
virtual bool validSearchInput (unsigned char c)
{
switch (c)
{
case '(':
case ')':
return true;
break;
default:
break;
}
string &search_string = ListColumn<T>::search_string;
if (c == '^' && !search_string.size())
return true;
else if (c == '$' && search_string.size())
{
if (search_string == "^")
return false;
if (search_string[search_string.size() - 1] != '$')
return true;
}
return ListColumn<T>::validSearchInput(c);
}
std::string getRawSearch(const std::string s)
{
string raw_search = s;
if (raw_search.size() && raw_search[0] == '^')
raw_search.erase(0, 1);
if (raw_search.size() && raw_search[raw_search.size() - 1] == '$')
raw_search.erase(raw_search.size() - 1, 1);
return toLower(raw_search);
}
virtual void tokenizeSearch (vector<string> *dest, const string search)
{
string raw_search = getRawSearch(search);
ListColumn<T>::tokenizeSearch(dest, raw_search);
}
virtual bool showEntry (const ListEntry<T> *entry, const vector<string> &search_tokens)
{
string &search_string = ListColumn<T>::search_string;
if (!search_string.size())
return true;
bool match_start = false, match_end = false;
string raw_search = getRawSearch(search_string);
if (search_string.size() && search_string[0] == '^')
match_start = true;
if (search_string.size() && search_string[search_string.size() - 1] == '$')
match_end = true;
if (!ListColumn<T>::showEntry(entry, search_tokens))
return false;
string item_name = toLower(Items::getBookTitle(entry->elem->entries[0]));
if (item_name == "")
{
item_name = toLower(Items::getDescription(entry->elem->entries[0], 0, false));
}
if ((match_start || match_end) && raw_search.size() > item_name.size())
return false;
if (match_start && item_name.compare(0, raw_search.size(), raw_search) != 0)
return false;
if (match_end && item_name.compare(item_name.size() - raw_search.size(), raw_search.size(), raw_search) != 0)
return false;
return true;
}
};
class search_help : public dfhack_viewscreen
{
public:
void feed (std::set<df::interface_key> *input)
{
if (input->count(interface_key::HELP))
return;
if (Screen::isDismissed(this))
return;
Screen::dismiss(this);
if (!input->count(interface_key::LEAVESCREEN) && !input->count(interface_key::SELECT))
parent->feed(input);
}
void render()
{
static std::string text =
"\7 Flag names can be\n"
" searched for - e.g. job,\n"
" inventory, dump, forbid\n"
"\n"
"\7 Use ^ to match the start\n"
" of a name, and/or $ to\n"
" match the end of a name";
if (Screen::isDismissed(this))
return;
parent->render();
int left_margin = gps->dimx - SIDEBAR_WIDTH;
int x = left_margin, y = 2;
Screen::fillRect(Screen::Pen(' ', 0, 0), left_margin - 1, 1, gps->dimx - 2, gps->dimy - 4);
Screen::fillRect(Screen::Pen(' ', 0, 0), left_margin - 1, 1, left_margin - 1, gps->dimy - 2);
OutputString(COLOR_WHITE, x, y, "Search help", true, left_margin);
++y;
vector<string> lines;
split_string(&lines, text, "\n");
for (auto line = lines.begin(); line != lines.end(); ++line)
OutputString(COLOR_WHITE, x, y, line->c_str(), true, left_margin);
}
std::string getFocusString() { return "stocks_view/search_help"; }
};
class ViewscreenStocks : public dfhack_viewscreen
{
public:
static df::item_flags hide_flags;
static extra_filters extra_hide_flags;
ViewscreenStocks(df::building_stockpilest *sp = NULL) : sp(sp)
{
is_grouped = true;
selected_column = 0;
items_column.multiselect = false;
items_column.auto_select = true;
items_column.allow_search = true;
items_column.left_margin = 2;
items_column.bottom_margin = 1;
items_column.search_margin = gps->dimx - SIDEBAR_WIDTH;
items_column.changeHighlight(0);
apply_to_all = false;
hide_unflagged = false;
checked_flags.bits.in_job = true;
checked_flags.bits.rotten = true;
checked_flags.bits.owned = true;
checked_flags.bits.forbid = true;
checked_flags.bits.dump = true;
checked_flags.bits.on_fire = true;
checked_flags.bits.melt = true;
checked_flags.bits.on_fire = true;
min_quality = item_quality::Ordinary;
max_quality = item_quality::Artifact;
min_wear = 0;
cages.clear();
items_in_cages.clear();
cages_populated = false;
last_selected_item = nullptr;
populateItems();
items_column.selectDefaultEntry();
}
static void reset()
{
hide_flags.whole = 0;
extra_hide_flags.reset();
depot_info.reset();
}
void feed(set<df::interface_key> *input)
{
if (input->count(interface_key::LEAVESCREEN))
{
input->clear();
Screen::dismiss(this);
return;
}
else if (input->count(interface_key::HELP))
{
Screen::show(dts::make_unique<search_help>(), plugin_self);
}
bool key_processed = false;
switch (selected_column)
{
case 0:
key_processed = items_column.feed(input);
break;
}
if (key_processed)
return;
if (input->count(interface_key::CUSTOM_CTRL_J))
{
hide_flags.bits.in_job = !hide_flags.bits.in_job;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_X))
{
hide_flags.bits.rotten = !hide_flags.bits.rotten;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_O))
{
hide_flags.bits.owned = !hide_flags.bits.owned;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_F))
{
hide_flags.bits.forbid = !hide_flags.bits.forbid;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_D))
{
hide_flags.bits.dump = !hide_flags.bits.dump;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_E))
{
hide_flags.bits.on_fire = !hide_flags.bits.on_fire;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_M))
{
hide_flags.bits.melt = !hide_flags.bits.melt;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_I))
{
extra_hide_flags.hide_in_inventory = !extra_hide_flags.hide_in_inventory;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_C))
{
extra_hide_flags.hide_in_cages = !extra_hide_flags.hide_in_cages;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_T))
{
extra_hide_flags.hide_trade_marked = !extra_hide_flags.hide_trade_marked;
populateItems();
}
else if (input->count(interface_key::CUSTOM_CTRL_N))
{
hide_unflagged = !hide_unflagged;
populateItems();
}
else if (input->count(interface_key::CUSTOM_SHIFT_C))
{
setAllFlags(true);
populateItems();
}
else if (input->count(interface_key::CUSTOM_SHIFT_E))
{
setAllFlags(false);
populateItems();
}
else if (input->count(interface_key::CHANGETAB))
{
is_grouped = !is_grouped;
populateItems();
items_column.centerSelection();
}
else if (input->count(interface_key::SECONDSCROLL_UP))
{
if (min_quality > item_quality::Ordinary)
{
min_quality = static_cast<df::item_quality>(static_cast<int16_t>(min_quality) - 1);
populateItems();
}
}
else if (input->count(interface_key::SECONDSCROLL_DOWN))
{
if (min_quality < max_quality && min_quality < item_quality::Artifact)
{
min_quality = static_cast<df::item_quality>(static_cast<int16_t>(min_quality) + 1);
populateItems();
}
}
else if (input->count(interface_key::SECONDSCROLL_PAGEUP))
{
if (max_quality > min_quality && max_quality > item_quality::Ordinary)
{
max_quality = static_cast<df::item_quality>(static_cast<int16_t>(max_quality) - 1);
populateItems();
}
}
else if (input->count(interface_key::SECONDSCROLL_PAGEDOWN))
{
if (max_quality < item_quality::Artifact)
{
max_quality = static_cast<df::item_quality>(static_cast<int16_t>(max_quality) + 1);
populateItems();
}
}
else if (input->count(interface_key::CUSTOM_SHIFT_W))
{
++min_wear;
if (min_wear > 3)
min_wear = 0;
populateItems();
}
else if (input->count(interface_key::CUSTOM_SHIFT_Z))
{
input->clear();
auto item_group = items_column.getFirstSelectedElem();
if (!item_group)
return;
if (is_grouped && !item_group->isSingleItem())
return;
auto item = item_group->getFirstItem();
auto pos = getRealPos(item);
if (!isRealPos(pos))
return;
Screen::dismiss(this);
auto vs = Gui::getCurViewscreen(true);
while (vs && !virtual_cast<df::viewscreen_dwarfmodest>(vs))
{
Screen::dismiss(vs);
vs = vs->parent;
}
// Could be clever here, if item is in a container, to look inside the container.
// But that's different for built containers vs bags/pots in stockpiles.
send_key(interface_key::D_LOOK);
move_cursor(pos);
}
else if (input->count(interface_key::CUSTOM_SHIFT_A))
{
apply_to_all = !apply_to_all;
}
else if (input->count(interface_key::CUSTOM_SHIFT_D))
{
df::item_flags flags;
flags.bits.dump = true;
toggleFlag(flags);
populateItems();
}
else if (input->count(interface_key::CUSTOM_SHIFT_F))
{
df::item_flags flags;
flags.bits.forbid = true;
toggleFlag(flags);
populateItems();
}
else if (input->count(interface_key::CUSTOM_SHIFT_M))
{
toggleMelt();
populateItems();
}
else if (input->count(interface_key::CUSTOM_SHIFT_T))
{
if (depot_info.canTrade())
{
auto selected = getSelectedItems();
for (auto it = selected.begin(); it != selected.end(); it++)
{
depot_info.assignItem((*it)->entries);
}
}
}
else if (input->count(interface_key::CURSOR_LEFT))
{
--selected_column;
validateColumn();
}
else if (input->count(interface_key::CURSOR_RIGHT))
{
selected_column++;
validateColumn();
}
else if (enabler->tracking_on && enabler->mouse_lbut)
{
if (items_column.setHighlightByMouse())
selected_column = 0;
enabler->mouse_lbut = enabler->mouse_rbut = 0;
}
}
void move_cursor(const df::coord &pos)
{
Gui::setCursorCoords(pos.x, pos.y, pos.z);
Gui::refreshSidebar();
}
void send_key(const df::interface_key &key)
{
set< df::interface_key > keys;
keys.insert(key);
Gui::getCurViewscreen(true)->feed(&keys);
}
void render()
{
if (Screen::isDismissed(this))
return;
dfhack_viewscreen::render();
Screen::clear();
Screen::drawBorder(" Stocks ");
items_column.display(selected_column == 0);
int32_t y = 1;
auto left_margin = gps->dimx - SIDEBAR_WIDTH;
int32_t x = left_margin - 2;
Screen::Pen border('\xDB', 8);
for (; y < gps->dimy - 1; y++)
{
paintTile(border, x, y);
}
y = 2;
x = left_margin;
OutputString(COLOR_BROWN, x, y, "Filters ", false, left_margin);
OutputString(COLOR_LIGHTRED, x, y, "(Ctrl+Key toggles)", true, left_margin);
OutputFilterString(x, y, "In Job ", "J", !hide_flags.bits.in_job, false, left_margin, COLOR_LIGHTBLUE);
OutputFilterString(x, y, "Rotten", "X", !hide_flags.bits.rotten, true, left_margin, COLOR_CYAN);
OutputFilterString(x, y, "Owned ", "O", !hide_flags.bits.owned, false, left_margin, COLOR_GREEN);
OutputFilterString(x, y, "Forbidden", "F", !hide_flags.bits.forbid, true, left_margin, COLOR_RED);
OutputFilterString(x, y, "Dump ", "D", !hide_flags.bits.dump, false, left_margin, COLOR_LIGHTMAGENTA);
OutputFilterString(x, y, "On Fire", "E", !hide_flags.bits.on_fire, true, left_margin, COLOR_LIGHTRED);
OutputFilterString(x, y, "Melt ", "M", !hide_flags.bits.melt, false, left_margin, COLOR_BLUE);
OutputFilterString(x, y, "In Inventory", "I", !extra_hide_flags.hide_in_inventory, true, left_margin, COLOR_WHITE);
OutputFilterString(x, y, "Caged ", "C", !extra_hide_flags.hide_in_cages, false, left_margin, COLOR_LIGHTRED);
OutputFilterString(x, y, "Trade", "T", !extra_hide_flags.hide_trade_marked, true, left_margin, COLOR_LIGHTGREEN);
OutputFilterString(x, y, "No Flags", "N", !hide_unflagged, true, left_margin, COLOR_GREY);
if (gps->dimy > 26)
++y;
OutputHotkeyString(x, y, "Clear All", "Shift-C", true, left_margin);
OutputHotkeyString(x, y, "Enable All", "Shift-E", true, left_margin);
OutputHotkeyString(x, y, "Toggle Grouping", interface_key::CHANGETAB, true, left_margin);
++y;
OutputHotkeyString(x, y, "Min Qual: ", "-+");
OutputString(COLOR_BROWN, x, y, get_quality_name(min_quality), true, left_margin);
OutputHotkeyString(x, y, "Max Qual: ", "/*");
OutputString(COLOR_BROWN, x, y, get_quality_name(max_quality), true, left_margin);
OutputHotkeyString(x, y, "Min Wear: ", "Shift-W");
OutputString(COLOR_BROWN, x, y, int_to_string(min_wear), true, left_margin);
if (gps->dimy > 27)
++y;
OutputString(COLOR_BROWN, x, y, "Actions (");
OutputString(COLOR_LIGHTGREEN, x, y, int_to_string(items_column.getDisplayedListSize()));
OutputString(COLOR_BROWN, x, y, " Items)", true, left_margin);
OutputHotkeyString(x, y, "Zoom ", "Shift-Z", false, left_margin);
OutputHotkeyString(x, y, "Dump", "-D", true, left_margin);
OutputHotkeyString(x, y, "Forbid ", "Shift-F", false, left_margin);
OutputHotkeyString(x, y, "Melt", "-M", true, left_margin);
OutputHotkeyString(x, y, "Mark for Trade", "Shift-T", true, left_margin,
depot_info.canTrade() ? COLOR_WHITE : COLOR_DARKGREY);
OutputHotkeyString(x, y, "Apply to: ", "Shift-A");
OutputString(COLOR_BROWN, x, y, (apply_to_all) ? "Listed" : "Selected", true, left_margin);
y = gps->dimy - 4;
OutputHotkeyString(x, y, "Search help", interface_key::HELP, true, left_margin);
}
std::string getFocusString() { return "stocks_view"; }
df::item *getSelectedItem() override
{
if (is_grouped)
return nullptr;
vector<item_grouped_entry*> items = getSelectedItems();
if (items.size() != 1)
return nullptr;
if (items[0]->entries.size() != 1)
return nullptr;
return items[0]->entries[0];
}
private:
StockListColumn<item_grouped_entry *> items_column;
int selected_column;
bool apply_to_all, hide_unflagged;
df::item_flags checked_flags;
df::item_quality min_quality, max_quality;
int16_t min_wear;
bool is_grouped;
std::list<item_grouped_entry> grouped_items_store;
df::item *last_selected_item;
string last_selected_hash;