From 701b7b7d9b1589824c1f077133f58207b4f1f5a8 Mon Sep 17 00:00:00 2001 From: Anuradha Dissanayake Date: Fri, 1 Mar 2013 21:29:16 +1300 Subject: [PATCH] Multiple data averaging windows and gcc fixes. --- plugins/buildingplan.cpp | 2 - plugins/dwarfmonitor.cpp | 110 ++++++++++++++++++++++++--------------- plugins/uicommon.h | 4 +- 3 files changed, 72 insertions(+), 44 deletions(-) diff --git a/plugins/buildingplan.cpp b/plugins/buildingplan.cpp index ad00490b4..cb6bcdc73 100644 --- a/plugins/buildingplan.cpp +++ b/plugins/buildingplan.cpp @@ -4,7 +4,6 @@ // DF data structure definition headers #include "DataDefs.h" -#include "MiscUtils.h" #include "Types.h" #include "df/build_req_choice_genst.h" #include "df/build_req_choice_specst.h" @@ -33,7 +32,6 @@ #include "modules/World.h" #include "df/building.h" -using df::global::gps; using df::global::ui; using df::global::ui_build_selector; using df::global::world; diff --git a/plugins/dwarfmonitor.cpp b/plugins/dwarfmonitor.cpp index 95b576828..6a4e3c2ea 100644 --- a/plugins/dwarfmonitor.cpp +++ b/plugins/dwarfmonitor.cpp @@ -1,7 +1,6 @@ #include "uicommon.h" #include "DataDefs.h" -#include "MiscUtils.h" #include "df/job.h" #include "df/ui.h" @@ -19,17 +18,18 @@ using std::deque; -using df::global::gps; using df::global::world; using df::global::ui; typedef int16_t activity_type; -#define PLUGIN_VERSION 0.2 +#define PLUGIN_VERSION 0.4 #define DAY_TICKS 1200 #define DELTA_TICKS 100 -int max_history_days = 7; +const int min_window = 28; +const int max_history_days = 3 * min_window; +const int ticks_per_day = DAY_TICKS / DELTA_TICKS; template struct less_second { @@ -40,7 +40,7 @@ struct less_second { }; static bool monitor_jobs = false; -static bool monitor_misery = false; +static bool monitor_misery = true; static map> work_history; static int misery[] = { 0, 0, 0, 0, 0, 0, 0 }; @@ -48,7 +48,7 @@ static bool misery_upto_date = false; static int get_max_history() { - return (DAY_TICKS / DELTA_TICKS) * max_history_days; + return ticks_per_day * max_history_days; } static int getPercentage(const int n, const int d) @@ -363,15 +363,32 @@ private: class ViewscreenFortStats : public dfhack_viewscreen { public: - ViewscreenFortStats() : selected_column(0), fort_activity_count(0) + 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; + + 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(); for (auto it = work_history.begin(); it != work_history.end();) { @@ -385,7 +402,8 @@ public: deque *work_list = &it->second; ++it; - for (auto entry = work_list->begin(); entry != work_list->end(); entry++) + 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; @@ -495,7 +513,7 @@ public: vector> rev_vec(fort_activity_totals.begin(), fort_activity_totals.end()); sort(rev_vec.begin(), rev_vec.end(), less_second()); - + for (auto rev_it = rev_vec.begin(); rev_it != rev_vec.end(); rev_it++) { auto activity = rev_it->first; @@ -510,9 +528,32 @@ public: dwarf_activity_column.left_margin = fort_activity_column.fixWidth() + 2; fort_activity_column.filterDisplay(); + fort_activity_column.setHighlight(last_selected_index); populateDwarfColumn(); } + void populateDwarfColumn() + { + dwarf_activity_column.clear(); + if (fort_activity_column.getDisplayListSize() == 0) + return; + + auto dwarf_activities = fort_activity_column.getFirstSelectedElem(); + if (dwarf_activities) + { + vector> rev_vec(dwarf_activities->begin(), dwarf_activities->end()); + sort(rev_vec.begin(), rev_vec.end(), less_second()); + + for_each_(rev_vec, + [&] (pair x) + { dwarf_activity_column.add(getDwarfAverage(x.first, x.second), *x.first); }); + } + + dwarf_activity_column.fixWidth(); + dwarf_activity_column.clearSearch(); + dwarf_activity_column.setHighlight(0); + } + void addToFortAverageColumn(activity_type type) { if (getFortActivityCount(type)) @@ -552,28 +593,6 @@ public: fort_activity_totals[activity]++; } - void populateDwarfColumn() - { - dwarf_activity_column.clear(); - if (fort_activity_column.getDisplayListSize() == 0) - return; - - auto dwarf_activities = fort_activity_column.getFirstSelectedElem(); - if (dwarf_activities) - { - vector> rev_vec(dwarf_activities->begin(), dwarf_activities->end()); - sort(rev_vec.begin(), rev_vec.end(), less_second()); - - for_each_(rev_vec, - [&] (pair x) - { dwarf_activity_column.add(getDwarfAverage(x.first, x.second), *x.first); }); - } - - dwarf_activity_column.fixWidth(); - dwarf_activity_column.clearSearch(); - dwarf_activity_column.setHighlight(0); - } - void feed(set *input) { bool key_processed = false; @@ -601,6 +620,14 @@ public: Screen::dismiss(this); return; } + else if (input->count(interface_key::SECONDSCROLL_PAGEDOWN)) + { + window_days += min_window; + if (window_days > max_history_days) + window_days = min_window; + + populateFortColumn(); + } else if (input->count(interface_key::CUSTOM_SHIFT_D)) { df::unit *selected_unit = (selected_column == 1) ? dwarf_activity_column.getFirstSelectedElem() : nullptr; @@ -656,11 +683,18 @@ public: fort_activity_column.display(selected_column == 0); dwarf_activity_column.display(selected_column == 1); - int32_t y = gps->dimy - 3; + int32_t y = gps->dimy - 4; int32_t x = 2; OutputHotkeyString(x, y, "Leave", "Esc"); - x += 3; + + x += 13; + string window_label = "Window Months: " + int_to_string(window_days / min_window); + OutputHotkeyString(x, y, window_label.c_str(), "*"); + + ++y; + x = 2; OutputHotkeyString(x, y, "Dwarf Stats", "Shift-D"); + x += 3; OutputHotkeyString(x, y, "Zoom Unit", "Shift-Z"); } @@ -675,6 +709,7 @@ private: map fort_activity_totals; map> dwarf_activity_values; size_t fort_activity_count; + size_t window_days; void validateColumn() { @@ -916,12 +951,6 @@ static command_result dwarfmonitor_cmd(color_ostream &out, vector & par if (set_monitoring_mode(mode, true)) { out << "Monitoring enabled: " << mode << endl; - if (parameters.size() > 2) - { - int new_window = atoi(parameters[2].c_str()); - if (new_window > 0) - max_history_days = new_window; - } } else { @@ -962,9 +991,8 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector \n" - " Start monitoring [days]\n" + " Start monitoring \n" " can be \"work\", \"misery\", or \"all\"\n" - " [days] number of days to track (default is 7)\n\n" "dwarfmonitor disable \n" " as above\n\n" "dwarfmonitor stats\n" diff --git a/plugins/uicommon.h b/plugins/uicommon.h index d4d52df4a..a936aecfe 100644 --- a/plugins/uicommon.h +++ b/plugins/uicommon.h @@ -4,6 +4,7 @@ #include #include "Core.h" +#include "MiscUtils.h" #include #include #include @@ -24,6 +25,7 @@ using namespace DFHack; using namespace df::enums; using df::global::enabler; +using df::global::gps; #ifndef HAVE_NULLPTR @@ -184,7 +186,7 @@ public: list.clear(); display_list.clear(); display_start_offset = 0; - max_item_width = 0; + max_item_width = title.length(); resize(); }