From 60d1c270c2173633f4d74473084bd541a5441d2f Mon Sep 17 00:00:00 2001 From: lethosor Date: Wed, 12 Jul 2017 16:07:44 -0400 Subject: [PATCH 1/3] Display autogems config option --- plugins/autogems.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/autogems.cpp b/plugins/autogems.cpp index 013038834..b77c5d249 100644 --- a/plugins/autogems.cpp +++ b/plugins/autogems.cpp @@ -50,6 +50,7 @@ const char *usage = ( "While this option is enabled, jobs will be created in Jeweler's Workshops\n" "to cut any accessible rough gems.\n" ); +std::set blacklist; void add_task(mat_index gem_type, df::building_workshopst *workshop) { // Create a single task in the specified workshop. @@ -121,6 +122,7 @@ bool valid_gem(df::item* item) { if (item->flags.bits.construction) return false; if (item->flags.bits.garbage_collect) return false; if (item->flags.bits.in_building) return false; + if (blacklist.count(item->getMaterialIndex())) return false; return true; } @@ -245,6 +247,8 @@ struct autogem_hook : public df::viewscreen_dwarfmodest { running = !running; return true; + } else if (input->count(interface_key::CUSTOM_SHIFT_G)) { + Core::getInstance().setHotkeyCmd("gui/autogems"); } return false; @@ -269,7 +273,11 @@ struct autogem_hook : public df::viewscreen_dwarfmodest { } if (pen.valid()) { - OutputHotkeyString(x, y, (running? "Auto Cut Gems": "No Auto Cut Gems"), "g", false, x, COLOR_WHITE, COLOR_LIGHTRED); + OutputHotkeyString(x, y, (running? "Auto Cut Gems": "No Auto Cut Gems"), + interface_key::CUSTOM_G, false, x, COLOR_WHITE, COLOR_LIGHTRED); + x += running ? 5 : 2; + OutputHotkeyString(x, y, "Opts", interface_key::CUSTOM_SHIFT_G, + false, 0, COLOR_WHITE, COLOR_LIGHTRED); } } } From 9d7feaf39df2afec1794a1278339d77524bd2ebb Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 5 May 2018 12:46:10 -0400 Subject: [PATCH 2/3] autogems: load blacklist from autogems.json Closes #1027 --- plugins/CMakeLists.txt | 2 +- plugins/autogems.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++ scripts | 2 +- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index e85e0aaf4..231ff656d 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -85,7 +85,7 @@ if (BUILD_SUPPORTED) #DFHACK_PLUGIN(advtools advtools.cpp) DFHACK_PLUGIN(autochop autochop.cpp) DFHACK_PLUGIN(autodump autodump.cpp) - DFHACK_PLUGIN(autogems autogems.cpp) + DFHACK_PLUGIN(autogems autogems.cpp LINK_LIBRARIES jsoncpp) DFHACK_PLUGIN(autohauler autohauler.cpp) DFHACK_PLUGIN(autolabor autolabor.cpp) DFHACK_PLUGIN(automaterial automaterial.cpp) diff --git a/plugins/autogems.cpp b/plugins/autogems.cpp index b77c5d249..ee255846d 100644 --- a/plugins/autogems.cpp +++ b/plugins/autogems.cpp @@ -4,9 +4,12 @@ * For best effect, include "enable autogems" in your dfhack.init configuration. */ +#include + #include "uicommon.h" #include "modules/Buildings.h" +#include "modules/Filesystem.h" #include "modules/Gui.h" #include "modules/Job.h" #include "modules/World.h" @@ -19,6 +22,8 @@ #include "df/job_item.h" #include "df/viewscreen_dwarfmodest.h" +#include "jsoncpp-ex.h" + #define CONFIG_KEY "autogems/config" #define DELTA_TICKS 1200 #define MAX_WORKSHOP_JOBS 10 @@ -286,6 +291,44 @@ struct autogem_hook : public df::viewscreen_dwarfmodest { IMPLEMENT_VMETHOD_INTERPOSE(autogem_hook, feed); IMPLEMENT_VMETHOD_INTERPOSE(autogem_hook, render); +bool read_config(color_ostream &out) { + std::string path = "data/save/" + World::ReadWorldFolder() + "/autogems.json"; + if (!Filesystem::isfile(path)) { + // no need to require the config file to exist + return true; + } + + std::ifstream f(path); + Json::Value config; + try { + if (!f.good() || !(f >> config)) { + out.printerr("autogems: failed to read autogems.json\n"); + return false; + } + } + catch (Json::Exception &e) { + out.printerr("autogems: failed to read autogems.json: %s\n", e.what()); + return false; + } + + if (config["blacklist"].isArray()) { + blacklist.clear(); + for (int i = 0; i < int(config["blacklist"].size()); i++) { + Json::Value item = config["blacklist"][i]; + if (item.isInt()) { + blacklist.insert(mat_index(item.asInt())); + } + else { + out.printerr("autogems: illegal item at position %i in blacklist\n", i); + } + } + } + return true; +} + +command_result cmd_reload_config(color_ostream &out, std::vector&) { + return read_config(out) ? CR_OK : CR_FAILURE; +} DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { if (event == DFHack::SC_MAP_LOADED) { @@ -294,6 +337,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan auto config = World::GetPersistentData(CONFIG_KEY); running = config.isValid() && !config.ival(0); last_frame_count = world->frame_counter; + read_config(out); } } else if (event == DFHack::SC_MAP_UNLOADED) { running = false; @@ -313,10 +357,20 @@ DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) { } running = enabled && World::isFortressMode(); + if (running) { + read_config(out); + } return CR_OK; } DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { + commands.push_back(PluginCommand( + "autogems-reload", + "Reload autogems config file", + cmd_reload_config, + false, + "Reload autogems config file" + )); return CR_OK; } diff --git a/scripts b/scripts index 53abecc96..65b102ed6 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 53abecc969936ad20c372737e1187dac4a54ef9a +Subproject commit 65b102ed6e0fe5b56b20f0c157f5a000e750432b From c9d7f433a9a0e28f2586d9a1cd377de572fdabb2 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 5 May 2018 15:39:16 -0400 Subject: [PATCH 3/3] Update docs for autogems UI --- docs/Plugins.rst | 3 +++ docs/changelog.txt | 4 ++++ scripts | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/Plugins.rst b/docs/Plugins.rst index 030aa9de1..d8cfdd270 100644 --- a/docs/Plugins.rst +++ b/docs/Plugins.rst @@ -1057,6 +1057,9 @@ autogems Creates a new Workshop Order setting, automatically cutting rough gems when `enabled `. +See `gui/autogems` for a configuration UI. If necessary, the ``autogems-reload`` +command reloads the configuration file produced by that script. + .. _stockflow: stockflow diff --git a/docs/changelog.txt b/docs/changelog.txt index db2027698..e0b26afee 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -37,12 +37,16 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ================================================================================ # Future +## New Scripts +- `gui/autogems`: a configuration UI for the `autogems` plugin + ## Fixes - `liquids`: fixed "range" command to default to 1 for dimensions consistently - `search`: fixed 4/6 keys in unit screen search - `view-item-info`: fixed an error with some armor ## Misc Improvements +- `autogems`: can now blacklist arbitrary gem types (see `gui/autogems`) - `exterminate`: added more words for current unit, removed warning ================================================================================ diff --git a/scripts b/scripts index 65b102ed6..6295e5ac6 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 65b102ed6e0fe5b56b20f0c157f5a000e750432b +Subproject commit 6295e5ac6a93cf460d4721510335a15541f6efbf