workNow: added option to look for jobs every time a dwarf finishes or cancels a job.

develop
expwnent 2013-03-16 22:43:57 -04:00
parent 6a31abc60f
commit 16b64b9fc3
1 changed files with 39 additions and 16 deletions

@ -3,39 +3,51 @@
#include "Export.h" #include "Export.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "DataDefs.h" #include "DataDefs.h"
#include "modules/EventManager.h"
#include "modules/World.h" #include "modules/World.h"
#include "df/global_objects.h" #include "df/global_objects.h"
#include <vector> #include <vector>
using namespace std; using namespace std;
using namespace DFHack; using namespace DFHack;
DFHACK_PLUGIN("workNow"); DFHACK_PLUGIN("workNow");
static bool active = false; static int mode = 0;
DFhackCExport command_result workNow(color_ostream& out, vector<string>& parameters); DFhackCExport command_result workNow(color_ostream& out, vector<string>& parameters);
void jobCompletedHandler(color_ostream& out, void* ptr);
EventManager::EventHandler handler(jobCompletedHandler,1);
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("workNow", "makes dwarves look for jobs every time you pause", workNow, false, "When workNow is active, every time the game pauses, DF will make dwarves perform any appropriate available jobs. This includes when you one step through the game using the pause menu.\n" commands.push_back(PluginCommand("workNow", "makes dwarves look for jobs whever they finish one, or every time you pause", workNow, false, "When workNow is active, every time the game pauses, DF will make dwarves perform any appropriate available jobs. This includes when you one step through the game using the pause menu. When workNow is in mode 2, it will make dwarves look for jobs every time a job completes (or is cancelled).\n"
"workNow 1\n" "workNow\n"
" activate workNow\n" " print workNow status\n"
"workNow 0\n" "workNow 0\n"
" deactivate workNow\n")); " deactivate workNow\n"
"workNow 1\n"
" activate workNow (look for jobs on pause, and only then)\n"
"workNow 2\n"
" make dwarves look for jobs whenever a job completes\n"
));
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) { DFhackCExport command_result plugin_shutdown ( color_ostream &out ) {
active = false; mode = 0;
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event e) { DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event e) {
if ( !active ) if ( !mode )
return CR_OK; return CR_OK;
if ( e == DFHack::SC_WORLD_UNLOADED ) { if ( e == DFHack::SC_WORLD_UNLOADED ) {
active = false; mode = 0;
return CR_OK; return CR_OK;
} }
if ( e != DFHack::SC_PAUSED ) if ( e != DFHack::SC_PAUSED )
@ -47,11 +59,9 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
return CR_OK; return CR_OK;
} }
DFhackCExport command_result workNow(color_ostream& out, vector<string>& parameters) { DFhackCExport command_result workNow(color_ostream& out, vector<string>& parameters) {
if ( parameters.size() == 0 ) { if ( parameters.size() == 0 ) {
out.print("workNow status = %s\n", active ? "active" : "inactive"); out.print("workNow status = %d\n", mode);
return CR_OK; return CR_OK;
} }
if ( parameters.size() > 1 ) { if ( parameters.size() > 1 ) {
@ -59,12 +69,25 @@ DFhackCExport command_result workNow(color_ostream& out, vector<string>& paramet
} }
int32_t a = atoi(parameters[0].c_str()); int32_t a = atoi(parameters[0].c_str());
if (a < 0 || a > 1) if (a < 0 || a > 2)
return CR_WRONG_USAGE; return CR_WRONG_USAGE;
active = (bool)a; if ( a == 2 && mode != 2 ) {
EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, handler, plugin_self);
} else if ( mode == 2 && a != 2 ) {
EventManager::unregister(EventManager::EventType::JOB_COMPLETED, handler, plugin_self);
}
mode = a;
out.print("workNow status = %d\n", mode);
return CR_OK; return CR_OK;
} }
void jobCompletedHandler(color_ostream& out, void* ptr) {
if ( mode < 2 )
return;
*df::global::process_jobs = true;
*df::global::process_dig = true;
}