Merge branch 'gui-autogems' into develop

develop
lethosor 2018-05-05 15:45:06 -04:00
commit 0904e8aa93
5 changed files with 72 additions and 3 deletions

@ -1057,6 +1057,9 @@ autogems
Creates a new Workshop Order setting, automatically cutting rough gems Creates a new Workshop Order setting, automatically cutting rough gems
when `enabled <enable>`. when `enabled <enable>`.
See `gui/autogems` for a configuration UI. If necessary, the ``autogems-reload``
command reloads the configuration file produced by that script.
.. _stockflow: .. _stockflow:
stockflow stockflow

@ -37,12 +37,16 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
================================================================================ ================================================================================
# Future # Future
## New Scripts
- `gui/autogems`: a configuration UI for the `autogems` plugin
## Fixes ## Fixes
- `liquids`: fixed "range" command to default to 1 for dimensions consistently - `liquids`: fixed "range" command to default to 1 for dimensions consistently
- `search`: fixed 4/6 keys in unit screen search - `search`: fixed 4/6 keys in unit screen search
- `view-item-info`: fixed an error with some armor - `view-item-info`: fixed an error with some armor
## Misc Improvements ## Misc Improvements
- `autogems`: can now blacklist arbitrary gem types (see `gui/autogems`)
- `exterminate`: added more words for current unit, removed warning - `exterminate`: added more words for current unit, removed warning
================================================================================ ================================================================================

@ -85,7 +85,7 @@ if (BUILD_SUPPORTED)
#DFHACK_PLUGIN(advtools advtools.cpp) #DFHACK_PLUGIN(advtools advtools.cpp)
DFHACK_PLUGIN(autochop autochop.cpp) DFHACK_PLUGIN(autochop autochop.cpp)
DFHACK_PLUGIN(autodump autodump.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(autohauler autohauler.cpp)
DFHACK_PLUGIN(autolabor autolabor.cpp) DFHACK_PLUGIN(autolabor autolabor.cpp)
DFHACK_PLUGIN(automaterial automaterial.cpp) DFHACK_PLUGIN(automaterial automaterial.cpp)

@ -4,9 +4,12 @@
* For best effect, include "enable autogems" in your dfhack.init configuration. * For best effect, include "enable autogems" in your dfhack.init configuration.
*/ */
#include <fstream>
#include "uicommon.h" #include "uicommon.h"
#include "modules/Buildings.h" #include "modules/Buildings.h"
#include "modules/Filesystem.h"
#include "modules/Gui.h" #include "modules/Gui.h"
#include "modules/Job.h" #include "modules/Job.h"
#include "modules/World.h" #include "modules/World.h"
@ -19,6 +22,8 @@
#include "df/job_item.h" #include "df/job_item.h"
#include "df/viewscreen_dwarfmodest.h" #include "df/viewscreen_dwarfmodest.h"
#include "jsoncpp-ex.h"
#define CONFIG_KEY "autogems/config" #define CONFIG_KEY "autogems/config"
#define DELTA_TICKS 1200 #define DELTA_TICKS 1200
#define MAX_WORKSHOP_JOBS 10 #define MAX_WORKSHOP_JOBS 10
@ -50,6 +55,7 @@ const char *usage = (
"While this option is enabled, jobs will be created in Jeweler's Workshops\n" "While this option is enabled, jobs will be created in Jeweler's Workshops\n"
"to cut any accessible rough gems.\n" "to cut any accessible rough gems.\n"
); );
std::set<mat_index> blacklist;
void add_task(mat_index gem_type, df::building_workshopst *workshop) { void add_task(mat_index gem_type, df::building_workshopst *workshop) {
// Create a single task in the specified workshop. // Create a single task in the specified workshop.
@ -121,6 +127,7 @@ bool valid_gem(df::item* item) {
if (item->flags.bits.construction) return false; if (item->flags.bits.construction) return false;
if (item->flags.bits.garbage_collect) return false; if (item->flags.bits.garbage_collect) return false;
if (item->flags.bits.in_building) return false; if (item->flags.bits.in_building) return false;
if (blacklist.count(item->getMaterialIndex())) return false;
return true; return true;
} }
@ -245,6 +252,8 @@ struct autogem_hook : public df::viewscreen_dwarfmodest {
running = !running; running = !running;
return true; return true;
} else if (input->count(interface_key::CUSTOM_SHIFT_G)) {
Core::getInstance().setHotkeyCmd("gui/autogems");
} }
return false; return false;
@ -269,7 +278,11 @@ struct autogem_hook : public df::viewscreen_dwarfmodest {
} }
if (pen.valid()) { 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);
} }
} }
} }
@ -278,6 +291,44 @@ struct autogem_hook : public df::viewscreen_dwarfmodest {
IMPLEMENT_VMETHOD_INTERPOSE(autogem_hook, feed); IMPLEMENT_VMETHOD_INTERPOSE(autogem_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(autogem_hook, render); 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<std::string>&) {
return read_config(out) ? CR_OK : CR_FAILURE;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_MAP_LOADED) { if (event == DFHack::SC_MAP_LOADED) {
@ -286,6 +337,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
auto config = World::GetPersistentData(CONFIG_KEY); auto config = World::GetPersistentData(CONFIG_KEY);
running = config.isValid() && !config.ival(0); running = config.isValid() && !config.ival(0);
last_frame_count = world->frame_counter; last_frame_count = world->frame_counter;
read_config(out);
} }
} else if (event == DFHack::SC_MAP_UNLOADED) { } else if (event == DFHack::SC_MAP_UNLOADED) {
running = false; running = false;
@ -305,10 +357,20 @@ DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) {
} }
running = enabled && World::isFortressMode(); running = enabled && World::isFortressMode();
if (running) {
read_config(out);
}
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
commands.push_back(PluginCommand(
"autogems-reload",
"Reload autogems config file",
cmd_reload_config,
false,
"Reload autogems config file"
));
return CR_OK; return CR_OK;
} }

@ -1 +1 @@
Subproject commit 53abecc969936ad20c372737e1187dac4a54ef9a Subproject commit 6295e5ac6a93cf460d4721510335a15541f6efbf