-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathwork-now.cpp
127 lines (101 loc) · 3.8 KB
/
work-now.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "Debug.h"
#include "PluginManager.h"
#include "modules/Job.h"
#include "modules/EventManager.h"
#include "modules/Persistence.h"
#include "modules/World.h"
using std::string;
using std::vector;
using namespace DFHack;
DFHACK_PLUGIN("work-now");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
namespace DFHack {
DBG_DECLARE(worknow, log, DebugCategory::LINFO);
}
static const string CONFIG_KEY = string(plugin_name) + "/config";
static PersistentDataItem config;
enum ConfigValues {
CONFIG_IS_ENABLED = 0,
};
static command_result work_now(color_ostream& out, vector<string>& parameters);
static void jobCompletedHandler(color_ostream& out, void* ptr);
EventManager::EventHandler handler(plugin_self,jobCompletedHandler,1);
DFhackCExport command_result plugin_init(color_ostream& out, std::vector<PluginCommand> &commands) {
commands.push_back(PluginCommand(
"work-now",
"Reduce the time that dwarves idle after completing a job.",
work_now));
return CR_OK;
}
static void cleanup() {
EventManager::unregister(EventManager::EventType::JOB_COMPLETED, handler);
}
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
out.printerr("Cannot enable %s without a loaded fort.\n", plugin_name);
return CR_FAILURE;
}
if (enable != is_enabled) {
is_enabled = enable;
DEBUG(log,out).print("%s from the API; persisting\n",
is_enabled ? "enabled" : "disabled");
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
if (enable)
EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, handler);
else
cleanup();
} else {
DEBUG(log,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled",
is_enabled ? "enabled" : "disabled");
}
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) {
cleanup();
return CR_OK;
}
DFhackCExport command_result plugin_load_site_data (color_ostream &out) {
config = World::GetPersistentSiteData(CONFIG_KEY);
if (!config.isValid()) {
DEBUG(log,out).print("no config found in this save; initializing\n");
config = World::AddPersistentSiteData(CONFIG_KEY);
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
}
is_enabled = config.get_bool(CONFIG_IS_ENABLED);
DEBUG(log,out).print("loading persisted enabled state: %s\n",
is_enabled ? "true" : "false");
return CR_OK;
}
static void poke_idlers() {
Job::checkBuildingsNow();
Job::checkDesignationsNow();
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event e) {
if (e == SC_PAUSED) {
DEBUG(log,out).print("game paused; poking idlers\n");
poke_idlers();
} else if (e == DFHack::SC_WORLD_UNLOADED) {
if (is_enabled) {
DEBUG(log,out).print("world unloaded; disabling %s\n",
plugin_name);
is_enabled = false;
}
}
return CR_OK;
}
static command_result work_now(color_ostream& out, vector<string>& parameters) {
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
out.printerr("Cannot run %s without a loaded fort.\n", plugin_name);
return CR_FAILURE;
}
if (parameters.empty() || parameters[0] == "status") {
out.print("work_now is %sactively poking idle dwarves.\n", is_enabled ? "" : "not ");
return CR_OK;
}
return CR_WRONG_USAGE;
}
static void jobCompletedHandler(color_ostream& out, void* ptr) {
DEBUG(log,out).print("job completed; poking idlers\n");
poke_idlers();
}