2023-04-27 15:22:45 -06:00
|
|
|
#include "Debug.h"
|
2012-11-19 18:17:05 -07:00
|
|
|
#include "PluginManager.h"
|
2013-03-16 20:43:57 -06:00
|
|
|
|
|
|
|
#include "modules/EventManager.h"
|
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2012-11-19 18:17:05 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
DFHACK_PLUGIN("work-now");
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
2014-12-06 16:47:35 -07:00
|
|
|
REQUIRE_GLOBAL(process_jobs);
|
|
|
|
REQUIRE_GLOBAL(process_dig);
|
2012-11-19 18:17:05 -07:00
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
namespace DFHack {
|
|
|
|
DBG_DECLARE(worknow, log, DebugCategory::LINFO);
|
|
|
|
}
|
2012-11-19 18:17:05 -07:00
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
DFhackCExport command_result work_now(color_ostream& out, vector<string>& parameters);
|
2012-12-08 10:50:33 -07:00
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
static void jobCompletedHandler(color_ostream& out, void* ptr);
|
2013-03-16 20:43:57 -06:00
|
|
|
EventManager::EventHandler handler(jobCompletedHandler,1);
|
|
|
|
|
2012-12-08 10:50:33 -07:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream& out, std::vector<PluginCommand> &commands) {
|
2022-07-31 14:32:06 -06:00
|
|
|
commands.push_back(PluginCommand(
|
2023-04-27 15:22:45 -06:00
|
|
|
"work-now",
|
2022-07-31 14:32:06 -06:00
|
|
|
"Reduce the time that dwarves idle after completing a job.",
|
2023-04-27 15:22:45 -06:00
|
|
|
work_now));
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-12-08 10:50:33 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
|
|
|
|
if (enable != is_enabled)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, handler, plugin_self);
|
|
|
|
else
|
|
|
|
EventManager::unregister(EventManager::EventType::JOB_COMPLETED, handler, plugin_self);
|
|
|
|
|
|
|
|
is_enabled = enable;
|
|
|
|
}
|
|
|
|
|
2012-11-19 18:17:05 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) {
|
|
|
|
return plugin_enable(out, false);
|
|
|
|
}
|
|
|
|
|
2012-12-08 10:50:33 -07:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event e) {
|
2023-04-27 15:22:45 -06:00
|
|
|
if (e == SC_PAUSED) {
|
|
|
|
DEBUG(log,out).print("game paused; poking idlers\n");
|
|
|
|
*process_jobs = true;
|
|
|
|
*process_dig = true;
|
2012-12-08 10:50:33 -07:00
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-11-19 18:17:05 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
DFhackCExport command_result work_now(color_ostream& out, vector<string>& parameters) {
|
|
|
|
if (parameters.empty() || parameters[0] == "status") {
|
|
|
|
out.print("work_now is %sactively poking idle dwarves.\n", is_enabled ? "" : "not ");
|
2012-12-08 10:50:33 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2012-11-19 18:17:05 -07:00
|
|
|
}
|
|
|
|
|
2023-04-27 15:22:45 -06:00
|
|
|
static void jobCompletedHandler(color_ostream& out, void* ptr) {
|
|
|
|
DEBUG(log,out).print("job completed; poking idlers\n");
|
2014-08-11 14:07:52 -06:00
|
|
|
*process_jobs = true;
|
|
|
|
*process_dig = true;
|
2013-03-16 20:43:57 -06:00
|
|
|
}
|