forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwarfmonitor.cpp
2050 lines (1749 loc) · 62.4 KB
/
dwarfmonitor.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 "DataDefs.h"
#include "df/job.h"
#include "df/ui.h"
#include "df/unit.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/world.h"
#include "df/misc_trait_type.h"
#include "df/unit_misc_trait.h"
#include "LuaTools.h"
#include "LuaWrapper.h"
#include "modules/Gui.h"
#include "modules/Units.h"
#include "modules/Translation.h"
#include "modules/World.h"
#include "modules/Maps.h"
#include "df/activity_entry.h"
#include "df/activity_event.h"
#include "df/creature_raw.h"
#include "df/dance_form.h"
#include "df/descriptor_color.h"
#include "df/descriptor_shape.h"
#include "df/item_type.h"
#include "df/itemdef_ammost.h"
#include "df/itemdef_armorst.h"
#include "df/itemdef_foodst.h"
#include "df/itemdef_glovesst.h"
#include "df/itemdef_helmst.h"
#include "df/itemdef_instrumentst.h"
#include "df/itemdef_pantsst.h"
#include "df/itemdef_shieldst.h"
#include "df/itemdef_shoesst.h"
#include "df/itemdef_siegeammost.h"
#include "df/itemdef_toolst.h"
#include "df/itemdef_toyst.h"
#include "df/itemdef_trapcompst.h"
#include "df/itemdef_weaponst.h"
#include "df/musical_form.h"
#include "df/poetic_form.h"
#include "df/trapcomp_flags.h"
#include "df/unit_preference.h"
#include "df/unit_soul.h"
#include "df/viewscreen_unitst.h"
#include "df/world_raws.h"
using std::deque;
DFHACK_PLUGIN("dwarfmonitor");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(current_weather);
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(ui);
typedef int16_t activity_type;
#define PLUGIN_VERSION 0.9
#define DAY_TICKS 1200
#define DELTA_TICKS 100
const int min_window = 28;
const int max_history_days = 3 * min_window;
const int ticks_per_day = DAY_TICKS / DELTA_TICKS;
template <typename T1, typename T2>
struct less_second {
typedef pair<T1, T2> type;
bool operator ()(type const& a, type const& b) const {
return a.second > b.second;
}
};
struct dwarfmonitor_configst {
std::string date_format;
};
static dwarfmonitor_configst dwarfmonitor_config;
static bool monitor_jobs = false;
static bool monitor_misery = true;
static bool monitor_date = true;
static bool monitor_weather = true;
static map<df::unit *, deque<activity_type>> work_history;
static int misery[] = { 0, 0, 0, 0, 0, 0, 0 };
static bool misery_upto_date = false;
static color_value monitor_colors[] =
{
COLOR_LIGHTRED,
COLOR_RED,
COLOR_YELLOW,
COLOR_WHITE,
COLOR_CYAN,
COLOR_LIGHTBLUE,
COLOR_LIGHTGREEN
};
static int get_happiness_cat(df::unit *unit)
{
int level = Units::getStressCategory(unit);
if (level < 0) level = 0;
if (level > 6) level = 6;
return level;
}
static int get_max_history()
{
return ticks_per_day * max_history_days;
}
static int getPercentage(const int n, const int d)
{
return static_cast<int>(
static_cast<float>(n) / static_cast<float>(d) * 100.0);
}
static string getUnitName(df::unit * unit)
{
string label = "";
auto name = Units::getVisibleName(unit);
if (name->has_name)
label = Translation::TranslateName(name, false);
return label;
}
template<typename T>
static string getFormName(int32_t id, const string &default_ = "?") {
T *form = T::find(id);
if (form)
return Translation::TranslateName(&form->name);
return default_;
}
static void send_key(const df::interface_key &key)
{
set< df::interface_key > keys;
keys.insert(key);
Gui::getCurViewscreen(true)->feed(&keys);
}
static void move_cursor(df::coord &pos)
{
Gui::setCursorCoords(pos.x, pos.y, pos.z);
Gui::refreshSidebar();
}
static void open_stats_screen();
namespace dm_lua {
static color_ostream_proxy *out;
static lua_State *state;
typedef int(*initializer)(lua_State*);
int no_args (lua_State *L) { return 0; }
void cleanup()
{
if (out)
{
delete out;
out = NULL;
}
lua_close(state);
}
bool init_call (const char *func)
{
if (!out)
out = new color_ostream_proxy(Core::getInstance().getConsole());
return Lua::PushModulePublic(*out, state, "plugins.dwarfmonitor", func);
}
bool safe_call (int nargs)
{
return Lua::SafeCall(*out, state, nargs, 0);
}
bool call (const char *func, initializer init = no_args)
{
Lua::StackUnwinder top(state);
if (!init_call(func))
return false;
int nargs = init(state);
return safe_call(nargs);
}
namespace api {
int monitor_state (lua_State *L)
{
std::string type = luaL_checkstring(L, 1);
if (type == "weather")
lua_pushboolean(L, monitor_weather);
else if (type == "misery")
lua_pushboolean(L, monitor_misery);
else if (type == "date")
lua_pushboolean(L, monitor_date);
else
lua_pushnil(L);
return 1;
}
int get_weather_counts (lua_State *L)
{
#define WEATHER_TYPES WTYPE(clear, None); WTYPE(rain, Rain); WTYPE(snow, Snow);
#define WTYPE(type, name) int type = 0;
WEATHER_TYPES
#undef WTYPE
int i, j;
for (i = 0; i < 5; ++i)
{
for (j = 0; j < 5; ++j)
{
switch ((*current_weather)[i][j])
{
#define WTYPE(type, name) case weather_type::name: type++; break;
WEATHER_TYPES
#undef WTYPE
}
}
}
lua_newtable(L);
#define WTYPE(type, name) Lua::TableInsert(L, #type, type);
WEATHER_TYPES
#undef WTYPE
#undef WEATHER_TYPES
return 1;
}
int get_misery_data (lua_State *L)
{
lua_newtable(L);
for (int i = 0; i < 7; i++)
{
Lua::Push(L, i);
lua_newtable(L);
Lua::TableInsert(L, "value", misery[i]);
Lua::TableInsert(L, "color", monitor_colors[i]);
Lua::TableInsert(L, "last", (i == 6));
lua_settable(L, -3);
}
return 1;
}
}
}
#define DM_LUA_FUNC(name) { #name, df::wrap_function(dm_lua::api::name, true) }
#define DM_LUA_CMD(name) { #name, dm_lua::api::name }
DFHACK_PLUGIN_LUA_COMMANDS {
DM_LUA_CMD(monitor_state),
DM_LUA_CMD(get_weather_counts),
DM_LUA_CMD(get_misery_data),
DFHACK_LUA_END
};
#define JOB_IDLE -1
#define JOB_UNKNOWN -2
#define JOB_MILITARY -3
#define JOB_LEISURE -4
#define JOB_UNPRODUCTIVE -5
#define JOB_DESIGNATE -6
#define JOB_STORE_ITEM -7
#define JOB_MANUFACTURE -8
#define JOB_DETAILING -9
#define JOB_HUNTING -10
#define JOB_MEDICAL -14
#define JOB_COLLECT -15
#define JOB_CONSTRUCTION -16
#define JOB_AGRICULTURE -17
#define JOB_FOOD_PROD -18
#define JOB_MECHANICAL -19
#define JOB_ANIMALS -20
#define JOB_PRODUCTIVE -21
static map<activity_type, string> activity_labels;
static string getActivityLabel(const activity_type activity)
{
string label;
if (activity_labels.find(activity) != activity_labels.end())
{
label = activity_labels[activity];
}
else
{
string raw_label = enum_item_key_str(static_cast<df::job_type>(activity));
for (auto c = raw_label.begin(); c != raw_label.end(); c++)
{
if (label.length() > 0 && *c >= 'A' && *c <= 'Z')
label += ' ';
label += *c;
}
}
return label;
}
class ViewscreenDwarfStats : public dfhack_viewscreen
{
public:
ViewscreenDwarfStats(df::unit *starting_selection) : selected_column(0)
{
dwarves_column.multiselect = false;
dwarves_column.auto_select = true;
dwarves_column.setTitle("Dwarves");
dwarf_activity_column.multiselect = false;
dwarf_activity_column.auto_select = true;
dwarf_activity_column.setTitle("Dwarf Activity");
window_days = min_window;
populateDwarfColumn(starting_selection);
}
void populateDwarfColumn(df::unit *starting_selection = NULL)
{
selected_column = 0;
auto last_selected_index = dwarf_activity_column.highlighted_index;
dwarves_column.clear();
dwarf_activity_values.clear();
for (auto it = work_history.begin(); it != work_history.end();)
{
auto unit = it->first;
if (!Units::isActive(unit))
{
work_history.erase(it++);
continue;
}
deque<activity_type> *work_list = &it->second;
++it;
size_t dwarf_total = 0;
dwarf_activity_values[unit] = map<activity_type, size_t>();
size_t count = window_days * ticks_per_day;
for (auto entry = work_list->rbegin(); entry != work_list->rend() && count > 0; entry++, count--)
{
if (*entry == JOB_UNKNOWN || *entry == job_type::DrinkBlood)
continue;
++dwarf_total;
addDwarfActivity(unit, *entry);
}
auto &values = dwarf_activity_values[unit];
for (auto it = values.begin(); it != values.end(); ++it)
it->second = getPercentage(it->second, dwarf_total);
dwarves_column.add(getUnitName(unit), unit);
}
dwarf_activity_column.left_margin = dwarves_column.fixWidth() + 2;
dwarves_column.filterDisplay();
if (starting_selection)
dwarves_column.selectItem(starting_selection);
else
dwarves_column.setHighlight(last_selected_index);
populateActivityColumn();
}
void populateActivityColumn()
{
dwarf_activity_column.clear();
if (dwarves_column.getDisplayedListSize() == 0)
return;
auto unit = dwarves_column.getFirstSelectedElem();
if (dwarf_activity_values.find(unit) == dwarf_activity_values.end())
return;
auto dwarf_activities = &dwarf_activity_values[unit];
if (dwarf_activities)
{
vector<pair<activity_type, size_t>> rev_vec(dwarf_activities->begin(), dwarf_activities->end());
sort(rev_vec.begin(), rev_vec.end(), less_second<activity_type, size_t>());
for (auto it = rev_vec.begin(); it != rev_vec.end(); ++it)
dwarf_activity_column.add(getActivityItem(it->first, it->second), it->first);
}
dwarf_activity_column.fixWidth();
dwarf_activity_column.clearSearch();
dwarf_activity_column.setHighlight(0);
}
void addDwarfActivity(df::unit *unit, const activity_type &activity)
{
if (dwarf_activity_values[unit].find(activity) == dwarf_activity_values[unit].end())
dwarf_activity_values[unit][activity] = 0;
dwarf_activity_values[unit][activity]++;
}
string getActivityItem(activity_type activity, size_t value)
{
return pad_string(int_to_string(value), 3) + " " + getActivityLabel(activity);
}
void feed(set<df::interface_key> *input)
{
bool key_processed = false;
switch (selected_column)
{
case 0:
key_processed = dwarves_column.feed(input);
break;
case 1:
key_processed = dwarf_activity_column.feed(input);
break;
}
if (key_processed)
{
if (selected_column == 0 && dwarves_column.feed_changed_highlight)
populateActivityColumn();
return;
}
if (input->count(interface_key::LEAVESCREEN))
{
input->clear();
Screen::dismiss(this);
return;
}
else if (input->count(interface_key::CUSTOM_SHIFT_D))
{
Screen::dismiss(this);
open_stats_screen();
}
else if (input->count(interface_key::CUSTOM_SHIFT_Z))
{
df::unit *selected_unit = (selected_column == 0) ? dwarves_column.getFirstSelectedElem() : nullptr;
if (selected_unit)
{
input->clear();
Screen::dismiss(this);
Gui::resetDwarfmodeView(true);
send_key(interface_key::D_VIEWUNIT);
move_cursor(selected_unit->pos);
}
}
else if (input->count(interface_key::SECONDSCROLL_PAGEDOWN))
{
window_days += min_window;
if (window_days > max_history_days)
window_days = min_window;
populateDwarfColumn();
}
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 (dwarves_column.setHighlightByMouse())
{
selected_column = 0;
populateActivityColumn();
}
else if (dwarf_activity_column.setHighlightByMouse())
selected_column = 1;
enabler->mouse_lbut = enabler->mouse_rbut = 0;
}
}
void render()
{
using namespace df::enums::interface_key;
if (Screen::isDismissed(this))
return;
dfhack_viewscreen::render();
Screen::clear();
Screen::drawBorder(" Dwarf Activity ");
dwarves_column.display(selected_column == 0);
dwarf_activity_column.display(selected_column == 1);
int32_t y = gps->dimy - 4;
int32_t x = 2;
OutputHotkeyString(x, y, "Leave", LEAVESCREEN);
x += 13;
string window_label = "Window Months: " + int_to_string(window_days / min_window);
OutputHotkeyString(x, y, window_label.c_str(), SECONDSCROLL_PAGEDOWN);
++y;
x = 2;
OutputHotkeyString(x, y, "Fort Stats", CUSTOM_SHIFT_D);
x += 3;
OutputHotkeyString(x, y, "Zoom Unit", CUSTOM_SHIFT_Z);
}
std::string getFocusString() { return "dwarfmonitor_dwarfstats"; }
private:
ListColumn<df::unit *> dwarves_column;
ListColumn<activity_type> dwarf_activity_column;
int selected_column;
size_t window_days;
map<df::unit *, map<activity_type, size_t>> dwarf_activity_values;
void validateColumn()
{
set_to_limit(selected_column, 1);
}
void resize(int32_t x, int32_t y)
{
dfhack_viewscreen::resize(x, y);
dwarves_column.resize();
dwarf_activity_column.resize();
}
};
class ViewscreenFortStats : public dfhack_viewscreen
{
public:
ViewscreenFortStats()
{
fort_activity_column.multiselect = false;
fort_activity_column.auto_select = true;
fort_activity_column.setTitle("Fort Activities");
fort_activity_column.bottom_margin = 4;
dwarf_activity_column.multiselect = false;
dwarf_activity_column.auto_select = true;
dwarf_activity_column.setTitle("Units on Activity");
dwarf_activity_column.bottom_margin = 4;
dwarf_activity_column.text_clip_at = 25;
category_breakdown_column.setTitle("Category Breakdown");
category_breakdown_column.bottom_margin = 4;
window_days = min_window;
populateFortColumn();
}
void populateFortColumn()
{
selected_column = 0;
fort_activity_count = 0;
auto last_selected_index = fort_activity_column.highlighted_index;
fort_activity_column.clear();
fort_activity_totals.clear();
dwarf_activity_values.clear();
category_breakdown.clear();
for (auto it = work_history.begin(); it != work_history.end();)
{
auto unit = it->first;
if (!Units::isActive(unit))
{
work_history.erase(it++);
continue;
}
deque<activity_type> *work_list = &it->second;
++it;
size_t count = window_days * ticks_per_day;
for (auto entry = work_list->rbegin(); entry != work_list->rend() && count > 0; entry++, count--)
{
if (*entry == JOB_UNKNOWN)
continue;
++fort_activity_count;
auto real_activity = *entry;
if (real_activity < 0)
{
addFortActivity(real_activity);
}
else
{
auto activity = static_cast<df::job_type>(real_activity);
switch (activity)
{
case job_type::Eat:
case job_type::Drink:
case job_type::Drink2:
case job_type::Sleep:
case job_type::AttendParty:
case job_type::Rest:
case job_type::CleanSelf:
case job_type::DrinkBlood:
real_activity = JOB_LEISURE;
break;
case job_type::Kidnap:
case job_type::StartingFistFight:
case job_type::SeekInfant:
case job_type::SeekArtifact:
case job_type::GoShopping:
case job_type::GoShopping2:
case job_type::RecoverPet:
case job_type::CauseTrouble:
case job_type::ReportCrime:
case job_type::BeatCriminal:
case job_type::ExecuteCriminal:
real_activity = JOB_UNPRODUCTIVE;
break;
case job_type::CarveUpwardStaircase:
case job_type::CarveDownwardStaircase:
case job_type::CarveUpDownStaircase:
case job_type::CarveRamp:
case job_type::DigChannel:
case job_type::Dig:
case job_type::CarveTrack:
case job_type::CarveFortification:
real_activity = JOB_DESIGNATE;
break;
case job_type::StoreOwnedItem:
case job_type::PlaceItemInTomb:
case job_type::StoreItemInStockpile:
case job_type::StoreItemInBag:
case job_type::StoreItemInHospital:
case job_type::StoreWeapon:
case job_type::StoreArmor:
case job_type::StoreItemInBarrel:
case job_type::StoreItemInBin:
case job_type::BringItemToDepot:
case job_type::BringItemToShop:
case job_type::GetProvisions:
case job_type::FillWaterskin:
case job_type::FillWaterskin2:
case job_type::CheckChest:
case job_type::PickupEquipment:
case job_type::DumpItem:
case job_type::PushTrackVehicle:
case job_type::PlaceTrackVehicle:
case job_type::StoreItemInVehicle:
real_activity = JOB_STORE_ITEM;
break;
case job_type::ConstructDoor:
case job_type::ConstructFloodgate:
case job_type::ConstructBed:
case job_type::ConstructThrone:
case job_type::ConstructCoffin:
case job_type::ConstructTable:
case job_type::ConstructChest:
case job_type::ConstructBin:
case job_type::ConstructArmorStand:
case job_type::ConstructWeaponRack:
case job_type::ConstructCabinet:
case job_type::ConstructStatue:
case job_type::ConstructBlocks:
case job_type::MakeRawGlass:
case job_type::MakeCrafts:
case job_type::MintCoins:
case job_type::CutGems:
case job_type::CutGlass:
case job_type::EncrustWithGems:
case job_type::EncrustWithGlass:
case job_type::SmeltOre:
case job_type::MeltMetalObject:
case job_type::ExtractMetalStrands:
case job_type::MakeWeapon:
case job_type::ForgeAnvil:
case job_type::ConstructCatapultParts:
case job_type::ConstructBallistaParts:
case job_type::MakeArmor:
case job_type::MakeHelm:
case job_type::MakePants:
case job_type::StudWith:
case job_type::ProcessPlantsVial:
case job_type::ProcessPlantsBarrel:
case job_type::WeaveCloth:
case job_type::MakeGloves:
case job_type::MakeShoes:
case job_type::MakeShield:
case job_type::MakeCage:
case job_type::MakeChain:
case job_type::MakeFlask:
case job_type::MakeGoblet:
case job_type::MakeToy:
case job_type::MakeAnimalTrap:
case job_type::MakeBarrel:
case job_type::MakeBucket:
case job_type::MakeWindow:
case job_type::MakeTotem:
case job_type::MakeAmmo:
case job_type::DecorateWith:
case job_type::MakeBackpack:
case job_type::MakeQuiver:
case job_type::MakeBallistaArrowHead:
case job_type::AssembleSiegeAmmo:
case job_type::ConstructMechanisms:
case job_type::MakeTrapComponent:
case job_type::ExtractFromPlants:
case job_type::ExtractFromRawFish:
case job_type::ExtractFromLandAnimal:
case job_type::MakeCharcoal:
case job_type::MakeAsh:
case job_type::MakeLye:
case job_type::MakePotashFromLye:
case job_type::MakePotashFromAsh:
case job_type::DyeThread:
case job_type::DyeCloth:
case job_type::SewImage:
case job_type::MakePipeSection:
case job_type::ConstructHatchCover:
case job_type::ConstructGrate:
case job_type::ConstructQuern:
case job_type::ConstructMillstone:
case job_type::ConstructSplint:
case job_type::ConstructCrutch:
case job_type::ConstructTractionBench:
case job_type::CustomReaction:
case job_type::ConstructSlab:
case job_type::EngraveSlab:
case job_type::SpinThread:
case job_type::MakeTool:
real_activity = JOB_MANUFACTURE;
break;
case job_type::DetailFloor:
case job_type::DetailWall:
real_activity = JOB_DETAILING;
break;
case job_type::Hunt:
case job_type::ReturnKill:
case job_type::HuntVermin:
case job_type::GatherPlants:
case job_type::Fish:
case job_type::CatchLiveFish:
case job_type::BaitTrap:
case job_type::InstallColonyInHive:
real_activity = JOB_HUNTING;
break;
case job_type::RemoveConstruction:
case job_type::DestroyBuilding:
case job_type::RemoveStairs:
case job_type::ConstructBuilding:
real_activity = JOB_CONSTRUCTION;
break;
case job_type::FellTree:
case job_type::CollectWebs:
case job_type::CollectSand:
case job_type::DrainAquarium:
case job_type::FillAquarium:
case job_type::FillPond:
case job_type::CollectClay:
real_activity = JOB_COLLECT;
break;
case job_type::TrainHuntingAnimal:
case job_type::TrainWarAnimal:
case job_type::CatchLiveLandAnimal:
case job_type::TameVermin:
case job_type::TameAnimal:
case job_type::ChainAnimal:
case job_type::UnchainAnimal:
case job_type::UnchainPet:
case job_type::ReleaseLargeCreature:
case job_type::ReleasePet:
case job_type::ReleaseSmallCreature:
case job_type::HandleSmallCreature:
case job_type::HandleLargeCreature:
case job_type::CageLargeCreature:
case job_type::CageSmallCreature:
case job_type::PitLargeAnimal:
case job_type::PitSmallAnimal:
case job_type::SlaughterAnimal:
case job_type::ShearCreature:
case job_type::PenLargeAnimal:
case job_type::PenSmallAnimal:
case job_type::TrainAnimal:
real_activity = JOB_ANIMALS;
break;
case job_type::PlantSeeds:
case job_type::HarvestPlants:
case job_type::FertilizeField:
real_activity = JOB_AGRICULTURE;
break;
case job_type::ButcherAnimal:
case job_type::PrepareRawFish:
case job_type::MillPlants:
case job_type::MilkCreature:
case job_type::MakeCheese:
case job_type::PrepareMeal:
case job_type::ProcessPlants:
case job_type::CollectHiveProducts:
real_activity = JOB_FOOD_PROD;
break;
case job_type::LoadCatapult:
case job_type::LoadBallista:
case job_type::FireCatapult:
case job_type::FireBallista:
real_activity = JOB_MILITARY;
break;
case job_type::LoadCageTrap:
case job_type::LoadStoneTrap:
case job_type::LoadWeaponTrap:
case job_type::CleanTrap:
case job_type::LinkBuildingToTrigger:
case job_type::PullLever:
real_activity = JOB_MECHANICAL;
break;
case job_type::RecoverWounded:
case job_type::DiagnosePatient:
case job_type::ImmobilizeBreak:
case job_type::DressWound:
case job_type::CleanPatient:
case job_type::Surgery:
case job_type::Suture:
case job_type::SetBone:
case job_type::PlaceInTraction:
case job_type::GiveWater:
case job_type::GiveFood:
case job_type::GiveWater2:
case job_type::GiveFood2:
case job_type::BringCrutch:
case job_type::ApplyCast:
real_activity = JOB_MEDICAL;
break;
case job_type::OperatePump:
case job_type::ManageWorkOrders:
case job_type::UpdateStockpileRecords:
case job_type::TradeAtDepot:
real_activity = JOB_PRODUCTIVE;
break;
default:
break;
}
addFortActivity(real_activity);
addCategoryActivity(real_activity, *entry);
}
if (dwarf_activity_values.find(real_activity) == dwarf_activity_values.end())
dwarf_activity_values[real_activity] = map<df::unit *, size_t>();
map<df::unit *, size_t> &activity_for_dwarf = dwarf_activity_values[real_activity];
if (activity_for_dwarf.find(unit) == activity_for_dwarf.end())
activity_for_dwarf[unit] = 0;
++activity_for_dwarf[unit];
}
}
vector<pair<activity_type, size_t>> rev_vec(fort_activity_totals.begin(), fort_activity_totals.end());
sort(rev_vec.begin(), rev_vec.end(), less_second<activity_type, size_t>());
for (auto rev_it = rev_vec.begin(); rev_it != rev_vec.end(); rev_it++)
{
auto activity = rev_it->first;
addToFortAverageColumn(activity);
for (auto it = dwarf_activity_values[activity].begin(); it != dwarf_activity_values[activity].end(); it++)
{
auto avg = getPercentage(it->second, getFortActivityCount(activity));
dwarf_activity_values[activity][it->first] = avg;
}
}
for (auto cat_it = category_breakdown.begin(); cat_it != category_breakdown.end(); cat_it++)
{
auto cat_total = fort_activity_totals[cat_it->first];
for (auto val_it = cat_it->second.begin(); val_it != cat_it->second.end(); val_it++)
{
category_breakdown[cat_it->first][val_it->first] = getPercentage(val_it->second, cat_total);
}
}
dwarf_activity_column.left_margin = fort_activity_column.fixWidth() + 2;
fort_activity_column.filterDisplay();
fort_activity_column.setHighlight(last_selected_index);
populateDwarfColumn();
populateCategoryBreakdownColumn();
}
void populateDwarfColumn()
{
dwarf_activity_column.clear();
if (fort_activity_column.getDisplayListSize() > 0)
{
activity_type selected_activity = fort_activity_column.getFirstSelectedElem();
auto dwarf_activities = &dwarf_activity_values[selected_activity];
if (dwarf_activities)
{
vector<pair<df::unit *, size_t>> rev_vec(dwarf_activities->begin(), dwarf_activities->end());
sort(rev_vec.begin(), rev_vec.end(), less_second<df::unit *, size_t>());
for (auto it = rev_vec.begin(); it != rev_vec.end(); ++it)
dwarf_activity_column.add(getDwarfAverage(it->first, it->second), it->first);
}
}
category_breakdown_column.left_margin = dwarf_activity_column.fixWidth() + 2;
dwarf_activity_column.clearSearch();
dwarf_activity_column.setHighlight(0);
}
void populateCategoryBreakdownColumn()
{
category_breakdown_column.clear();
if (fort_activity_column.getDisplayListSize() == 0)
return;
auto selected_activity = fort_activity_column.getFirstSelectedElem();
auto category_activities = &category_breakdown[selected_activity];
if (category_activities)
{
vector<pair<activity_type, size_t>> rev_vec(category_activities->begin(), category_activities->end());
sort(rev_vec.begin(), rev_vec.end(), less_second<activity_type, size_t>());
for (auto it = rev_vec.begin(); it != rev_vec.end(); ++it)
category_breakdown_column.add(getBreakdownAverage(it->first, it->second), it->first);
}
category_breakdown_column.fixWidth();
category_breakdown_column.clearSearch();
category_breakdown_column.setHighlight(0);
}
void addToFortAverageColumn(activity_type &type)
{
if (getFortActivityCount(type))
fort_activity_column.add(getFortAverage(type), type);
}
string getFortAverage(const activity_type &activity)
{
auto average = getPercentage(getFortActivityCount(activity), fort_activity_count);
auto label = getActivityLabel(activity);
auto result = pad_string(int_to_string(average), 3) + " " + label;
return result;
}
string getDwarfAverage(df::unit *unit, const size_t value)
{
auto label = getUnitName(unit);
auto result = pad_string(int_to_string(value), 3) + " " + label;
return result;
}
string getBreakdownAverage(activity_type activity, const size_t value)
{
auto label = getActivityLabel(activity);
auto result = pad_string(int_to_string(value), 3) + " " + label;
return result;
}
size_t getFortActivityCount(const activity_type activity)
{
if (fort_activity_totals.find(activity) == fort_activity_totals.end())
return 0;
return fort_activity_totals[activity];
}
void addFortActivity(const activity_type activity)
{
if (fort_activity_totals.find(activity) == fort_activity_totals.end())
fort_activity_totals[activity] = 0;
fort_activity_totals[activity]++;
}
void addCategoryActivity(const int category, const activity_type activity)
{