diff --git a/plugins/automaterial.cpp b/plugins/automaterial.cpp index dc86e9c3a..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,13 +35,12 @@ using df::global::gps; using df::global::ui; using df::global::ui_build_selector; - DFHACK_PLUGIN("automaterial"); struct MaterialDescriptor { - df::item_type item_type; - int16_t item_subtype; + df::item_type item_type; + int16_t item_subtype; int16_t type; int32_t index; bool valid; @@ -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; @@ -102,19 +99,28 @@ void OutputHotkeyString(int &x, int &y, const char *text, const char *hotkey, bo static inline bool in_material_choice_stage() { return Gui::build_selector_hotkey(Core::getTopViewscreen()) && + ui_build_selector->building_type == df::building_type::Construction && ui->main.mode == ui_sidebar_mode::Build && ui_build_selector->stage == 2; } -static inline bool in_type_choice_stage() +static inline bool in_placement_stage() { return Gui::dwarfmode_hotkey(Core::getTopViewscreen()) && ui->main.mode == ui_sidebar_mode::Build && ui_build_selector && - ui_build_selector->building_type >= 0 && + ui_build_selector->building_type == df::building_type::Construction && ui_build_selector->stage == 1; } +static inline bool in_type_choice_stage() +{ + return Gui::dwarfmode_hotkey(Core::getTopViewscreen()) && + ui->main.mode == ui_sidebar_mode::Build && + ui_build_selector && + ui_build_selector->building_type < 0; +} + static inline vector &get_curr_constr_prefs() { if (preferred_materials.find(ui_build_selector->building_subtype) == preferred_materials.end()) @@ -174,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++) @@ -216,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; @@ -260,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()) @@ -281,7 +286,7 @@ struct jobutils_hook : public df::viewscreen_dwarfmodest } } } - else if (in_type_choice_stage()) + else if (in_placement_stage()) { if (input->count(interface_key::CUSTOM_A)) { @@ -293,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); } } @@ -358,7 +365,7 @@ struct jobutils_hook : public df::viewscreen_dwarfmodest OutputHotkeyString(x, y, toggle_string.c_str(), "a", true, left_margin); } } - else if (in_type_choice_stage() && ui_build_selector->building_subtype != 7) + else if (in_placement_stage() && ui_build_selector->building_subtype != 7) { int left_margin = gps->dimx - 30; int x = left_margin; @@ -385,46 +392,14 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector