From 3b362294b95241511adfe221c968fa8a92515753 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Tue, 8 Sep 2020 17:34:11 -0700 Subject: [PATCH 1/2] move quickfort logic from Planner to buildingplan where all the other gui-related logic is. --- plugins/buildingplan-planner.cpp | 27 +++------------------ plugins/buildingplan-planner.h | 10 +------- plugins/buildingplan.cpp | 41 ++++++++++++++++---------------- 3 files changed, 25 insertions(+), 53 deletions(-) diff --git a/plugins/buildingplan-planner.cpp b/plugins/buildingplan-planner.cpp index 31caf995e..6923570c2 100644 --- a/plugins/buildingplan-planner.cpp +++ b/plugins/buildingplan-planner.cpp @@ -291,11 +291,9 @@ void PlannedBuilding::remove() * Planner */ -Planner::Planner() : in_dummmy_screen(false), quickfort_mode(false) { } +Planner::Planner() { } -void enable_quickfort_fn(pair& pair) { pair.second = true; } - -bool Planner::isPlanableBuilding(const df::building_type type) const +bool Planner::isPlannableBuilding(const df::building_type type) const { return item_for_building_type.find(type) != item_for_building_type.end(); } @@ -321,8 +319,6 @@ void Planner::initialize() default_item_filters[df::building_type::btype] = ItemFilter(); \ available_item_vectors[df::item_type::itype] = std::vector(); \ is_relevant_item_type[df::item_type::itype] = true; \ - if (planmode_enabled.find(df::building_type::btype) == planmode_enabled.end()) \ - planmode_enabled[df::building_type::btype] = false FOR_ENUM_ITEMS(item_type, it) is_relevant_item_type[it] = false; @@ -474,22 +470,6 @@ void Planner::adjustMaxQuality(df::building_type type, int amount) (*min_quality) = *max_quality; } -void Planner::enableQuickfortMode() -{ - saved_planmodes = planmode_enabled; - for_each_(planmode_enabled, enable_quickfort_fn); - - quickfort_mode = true; -} - -void Planner::disableQuickfortMode() -{ - planmode_enabled = saved_planmodes; - quickfort_mode = false; -} - -bool Planner::inQuickFortMode() { return quickfort_mode; } - void Planner::boundsCheckItemQuality(item_quality::item_quality *quality) { *quality = static_cast(*quality); @@ -517,7 +497,7 @@ void Planner::gather_available_items() F(in_building); F(construction); F(artifact); #undef F - std::vector &items = df::global::world->items.other[df::items_other_id::IN_PLAY]; + std::vector &items = df::global::world->items.other[df::items_other_id::IN_PLAY]; for (size_t i = 0; i < items.size(); i++) { @@ -548,5 +528,4 @@ void Planner::gather_available_items() } } -std::map planmode_enabled, saved_planmodes; Planner planner; diff --git a/plugins/buildingplan-planner.h b/plugins/buildingplan-planner.h index b073e96b8..67eeb9bff 100644 --- a/plugins/buildingplan-planner.h +++ b/plugins/buildingplan-planner.h @@ -61,11 +61,9 @@ private: class Planner { public: - bool in_dummmy_screen; - Planner(); - bool isPlanableBuilding(const df::building_type type) const; + bool isPlannableBuilding(const df::building_type type) const; void reset(DFHack::color_ostream &out); @@ -86,16 +84,11 @@ public: void adjustMinQuality(df::building_type type, int amount); void adjustMaxQuality(df::building_type type, int amount); - void enableQuickfortMode(); - void disableQuickfortMode(); - bool inQuickFortMode(); - private: std::map item_for_building_type; std::map default_item_filters; std::map> available_item_vectors; std::map is_relevant_item_type; //Needed for fast check when looping over all items - bool quickfort_mode; std::vector planned_buildings; @@ -104,5 +97,4 @@ private: void gather_available_items(); }; -extern std::map planmode_enabled, saved_planmodes; extern Planner planner; diff --git a/plugins/buildingplan.cpp b/plugins/buildingplan.cpp index ae8ce8c8d..2ba8f982e 100644 --- a/plugins/buildingplan.cpp +++ b/plugins/buildingplan.cpp @@ -28,6 +28,9 @@ using namespace DFHack; using namespace df::enums; bool show_help = false; +bool quickfort_mode = false; +bool in_dummy_screen = false; +std::map planmode_enabled; class ViewscreenChooseMaterial : public dfhack_viewscreen { @@ -316,7 +319,7 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest return ui->main.mode == ui_sidebar_mode::Build && df::global::ui_build_selector && df::global::ui_build_selector->stage < 2 && - planner.isPlanableBuilding(ui_build_selector->building_type); + planner.isPlannableBuilding(ui_build_selector->building_type); } std::vector getNoblePositionOfSelectedBuildingOwner() @@ -357,11 +360,16 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest auto type = ui_build_selector->building_type; if (input->count(interface_key::CUSTOM_SHIFT_P)) { + if (planmode_enabled.find(type) == planmode_enabled.end()) + { + planmode_enabled[type] = false; + } + planmode_enabled[type] = !planmode_enabled[type]; if (!planmode_enabled[type]) { Gui::refreshSidebar(); - planner.in_dummmy_screen = false; + in_dummy_screen = false; } return true; } @@ -375,12 +383,12 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest if (is_planmode_enabled(type)) { - if (planner.inQuickFortMode() && planner.in_dummmy_screen) + if (quickfort_mode && in_dummy_screen) { if (input->count(interface_key::SELECT) || input->count(interface_key::SEC_SELECT) || input->count(interface_key::LEAVESCREEN)) { - planner.in_dummmy_screen = false; + in_dummy_screen = false; send_key(interface_key::LEAVESCREEN); } @@ -392,9 +400,9 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest if (ui_build_selector->errors.size() == 0 && planner.allocatePlannedBuilding(type)) { Gui::refreshSidebar(); - if (planner.inQuickFortMode()) + if (quickfort_mode) { - planner.in_dummmy_screen = true; + in_dummy_screen = true; } } @@ -402,14 +410,7 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest } else if (input->count(interface_key::CUSTOM_SHIFT_F)) { - if (!planner.inQuickFortMode()) - { - planner.enableQuickfortMode(); - } - else - { - planner.disableQuickfortMode(); - } + quickfort_mode = !quickfort_mode; } else if (input->count(interface_key::CUSTOM_SHIFT_M)) { @@ -509,7 +510,7 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest auto type = ui_build_selector->building_type; if (plannable) { - if (planner.inQuickFortMode() && planner.in_dummmy_screen) + if (quickfort_mode && in_dummy_screen) { Screen::Pen pen(' ',COLOR_BLACK); int y = dims.y1 + 1; @@ -533,7 +534,7 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest if (is_planmode_enabled(type)) { - OutputToggleString(x, y, "Quickfort Mode", "F", planner.inQuickFortMode(), true, left_margin); + OutputToggleString(x, y, "Quickfort Mode", "F", quickfort_mode, true, left_margin); auto filter = planner.getDefaultItemFilterForType(type); @@ -552,13 +553,13 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest } else { - planner.in_dummmy_screen = false; + in_dummy_screen = false; } } } else if (isInPlannedBuildingQueryMode()) { - planner.in_dummmy_screen = false; + in_dummy_screen = false; // Hide suspend toggle option int y = 20; @@ -597,7 +598,7 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest } else { - planner.in_dummmy_screen = false; + in_dummy_screen = false; show_help = false; } } @@ -690,7 +691,7 @@ DFhackCExport command_result plugin_shutdown(color_ostream &) // Lua API section static bool isPlannableBuilding(df::building_type type) { - return planner.isPlanableBuilding(type); + return planner.isPlannableBuilding(type); } static void addPlannedBuilding(df::building *bld) { From 1c2761f90384e4eeedd4364cc4a012be2512fce1 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 23 Sep 2020 20:00:04 -0700 Subject: [PATCH 2/2] enable all in quickfort mode and simplify logic since new bool map values are already initialized to false --- plugins/buildingplan.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/plugins/buildingplan.cpp b/plugins/buildingplan.cpp index 2ba8f982e..93d5ff4e4 100644 --- a/plugins/buildingplan.cpp +++ b/plugins/buildingplan.cpp @@ -287,12 +287,7 @@ void ViewscreenChooseMaterial::render() //START Viewscreen Hook static bool is_planmode_enabled(df::building_type type) { - if (planmode_enabled.find(type) == planmode_enabled.end()) - { - return false; - } - - return planmode_enabled[type]; + return planmode_enabled[type] || quickfort_mode; } struct buildingplan_hook : public df::viewscreen_dwarfmodest @@ -360,13 +355,8 @@ struct buildingplan_hook : public df::viewscreen_dwarfmodest auto type = ui_build_selector->building_type; if (input->count(interface_key::CUSTOM_SHIFT_P)) { - if (planmode_enabled.find(type) == planmode_enabled.end()) - { - planmode_enabled[type] = false; - } - planmode_enabled[type] = !planmode_enabled[type]; - if (!planmode_enabled[type]) + if (!is_planmode_enabled(type)) { Gui::refreshSidebar(); in_dummy_screen = false;