From 2a83d9229094eb38628c422ede9d1b28495cd8ec Mon Sep 17 00:00:00 2001 From: Anuradha Dissanayake Date: Wed, 21 Nov 2012 23:45:26 +1300 Subject: [PATCH] Code cleanup --- plugins/automaterial.cpp | 124 ++++++++++++++------------------------- 1 file changed, 45 insertions(+), 79 deletions(-) diff --git a/plugins/automaterial.cpp b/plugins/automaterial.cpp index d05e13a3e..ac5a4ae22 100644 --- a/plugins/automaterial.cpp +++ b/plugins/automaterial.cpp @@ -14,13 +14,14 @@ // DF data structure definition headers #include "DataDefs.h" #include "MiscUtils.h" -#include "df/viewscreen_dwarfmodest.h" -#include "df/ui_build_selector.h" #include "df/build_req_choice_genst.h" #include "df/build_req_choice_specst.h" +#include "df/construction_type.h" #include "df/item.h" - #include "df/ui.h" +#include "df/ui_build_selector.h" +#include "df/viewscreen_dwarfmodest.h" + #include "modules/Gui.h" #include "modules/Screen.h" @@ -34,7 +35,6 @@ using df::global::gps; using df::global::ui; using df::global::ui_build_selector; - DFHACK_PLUGIN("automaterial"); struct MaterialDescriptor @@ -55,12 +55,10 @@ struct MaterialDescriptor } }; -typedef int16_t construction_type; - -static map last_used_material; -static map last_moved_material; -static map< construction_type, vector > preferred_materials; -static map< construction_type, df::interface_key > hotkeys; +static map last_used_material; +static map last_moved_material; +static map< int16_t, vector > preferred_materials; +static map< int16_t, df::interface_key > hotkeys; static bool last_used_moved = false; static bool auto_choose_materials = true; static bool auto_choose_attempted = true; @@ -71,7 +69,6 @@ static command_result automaterial_cmd(color_ostream &out, vector & par return CR_OK; } - DFhackCExport command_result plugin_shutdown ( color_ostream &out ) { return CR_OK; @@ -183,7 +180,6 @@ static MaterialDescriptor get_material_in_list(size_t i) return result; } - static bool is_material_in_autoselect(size_t &i, MaterialDescriptor &material) { for (i = 0; i < get_curr_constr_prefs().size(); i++) @@ -225,27 +221,6 @@ static bool move_material_to_top(MaterialDescriptor &material) return false; } -static bool choose_materials() -{ - size_t size = ui_build_selector->choices.size(); - for (size_t i = 0; i < size; i++) - { - MaterialDescriptor material = get_material_in_list(i); - size_t j; - if (is_material_in_autoselect(j, material)) - { - ui_build_selector->sel_index = i; - std::set< df::interface_key > keys; - keys.insert(df::interface_key::SELECT_ALL); - Core::getTopViewscreen()->feed(&keys); - if (!in_material_choice_stage()) - return true; - } - } - - return false; -} - static bool check_autoselect(MaterialDescriptor &material, bool toggle) { size_t idx; @@ -269,6 +244,27 @@ struct jobutils_hook : public df::viewscreen_dwarfmodest { typedef df::viewscreen_dwarfmodest interpose_base; + bool choose_materials() + { + size_t size = ui_build_selector->choices.size(); + for (size_t i = 0; i < size; i++) + { + MaterialDescriptor material = get_material_in_list(i); + size_t j; + if (is_material_in_autoselect(j, material)) + { + ui_build_selector->sel_index = i; + std::set< df::interface_key > keys; + keys.insert(df::interface_key::SELECT_ALL); + this->feed(&keys); + if (!in_material_choice_stage()) + return true; + } + } + + return false; + } + DEFINE_VMETHOD_INTERPOSE(void, feed, (set *input)) { if (in_material_choice_stage()) @@ -302,15 +298,17 @@ struct jobutils_hook : public df::viewscreen_dwarfmodest } } - construction_type last_used_constr_subtype = (in_material_choice_stage()) ? ui_build_selector->building_subtype : -1; + int16_t last_used_constr_subtype = (in_material_choice_stage()) ? ui_build_selector->building_subtype : -1; INTERPOSE_NEXT(feed)(input); - if (revert_to_last_used_type && last_used_constr_subtype >= 0 && !in_material_choice_stage()) + if (revert_to_last_used_type && + last_used_constr_subtype >= 0 && + !in_material_choice_stage() && + hotkeys.find(last_used_constr_subtype) != hotkeys.end()) { input->clear(); - std::set< df::interface_key > keys; - keys.insert(hotkeys[last_used_constr_subtype]); - Core::getTopViewscreen()->feed(&keys); + input->insert(hotkeys[last_used_constr_subtype]); + this->feed(input); } } @@ -394,46 +392,14 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector