forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stockflow.cpp
420 lines (331 loc) · 12 KB
/
stockflow.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Stockflow plugin.
* For best effect, place "stockflow enable" in your dfhack.init configuration,
* or set AUTOENABLE to true.
*/
#include "uicommon.h"
#include "LuaTools.h"
#include "df/building_stockpilest.h"
#include "df/job.h"
#include "df/viewscreen_dwarfmodest.h"
#include "modules/Gui.h"
#include "modules/Maps.h"
#include "modules/World.h"
using namespace DFHack;
using namespace std;
using df::building_stockpilest;
DFHACK_PLUGIN("stockflow");
#define AUTOENABLE false
#ifdef DFHACK_PLUGIN_IS_ENABLED
DFHACK_PLUGIN_IS_ENABLED(enabled);
#else
bool enabled = false;
#endif
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(ui);
bool fast = false;
const char *tagline = "Allows the fortress bookkeeper to queue jobs through the manager.";
const char *usage = (
" stockflow enable\n"
" Enable the plugin.\n"
" stockflow disable\n"
" Disable the plugin.\n"
" stockflow fast\n"
" Enable the plugin in fast mode.\n"
" stockflow list\n"
" List any work order settings for your stockpiles.\n"
" stockflow status\n"
" Display whether the plugin is enabled.\n"
"\n"
"While enabled, the 'q' menu of each stockpile will have two new options:\n"
" j: Select a job to order, from an interface like the manager's screen.\n"
" J: Cycle between several options for how many such jobs to order.\n"
"\n"
"Whenever the bookkeeper updates stockpile records, new work orders will\n"
"be placed on the manager's queue for each such selection, reduced by the\n"
"number of identical orders already in the queue.\n"
"\n"
"In fast mode, new work orders will be enqueued once per day, instead of\n"
"waiting for the bookkeeper.\n"
);
/*
* Lua interface.
* Currently calls out to Lua functions, but never back in.
*/
class LuaHelper {
public:
void cycle(color_ostream &out) {
bool found = false;
if (fast) {
// Ignore the bookkeeper; either gather or enqueue orders every cycle.
found = !bookkeeping;
} else {
// Gather orders when the bookkeeper starts updating stockpile records,
// and enqueue them when the job is done.
for (df::job_list_link* link = &world->job_list; link != NULL; link = link->next) {
if (link->item == NULL) continue;
if (link->item->job_type == job_type::UpdateStockpileRecords) {
found = true;
break;
}
}
}
if (found && !bookkeeping) {
command_method("start_bookkeeping", out);
bookkeeping = true;
} else if (bookkeeping && !found) {
command_method("finish_bookkeeping", out);
bookkeeping = false;
}
}
void init() {
stockpile_id = -1;
initialized = false;
bookkeeping = false;
}
bool reset(color_ostream &out, bool load) {
stockpile_id = -1;
bookkeeping = false;
if (load) {
return initialized = command_method("initialize_world", out);
} else if (initialized) {
initialized = false;
return command_method("clear_caches", out);
}
return true;
}
bool command_method(const char *method, color_ostream &out) {
// Calls a lua function with no parameters.
// Suspension is required for "stockflow enable" from the command line,
// but may be overkill for other situations.
CoreSuspender suspend;
auto L = Lua::Core::State;
Lua::StackUnwinder top(L);
if (!lua_checkstack(L, 1))
return false;
if (!Lua::PushModulePublic(out, L, "plugins.stockflow", method))
return false;
if (!Lua::SafeCall(out, L, 0, 0))
return false;
return true;
}
bool stockpile_method(const char *method, building_stockpilest *sp) {
// Combines the select_order and toggle_trigger method calls,
// because they share the same signature.
CoreSuspendClaimer suspend;
auto L = Lua::Core::State;
color_ostream_proxy out(Core::getInstance().getConsole());
Lua::StackUnwinder top(L);
if (!lua_checkstack(L, 2))
return false;
if (!Lua::PushModulePublic(out, L, "plugins.stockflow", method))
return false;
Lua::Push(L, sp);
if (!Lua::SafeCall(out, L, 1, 0))
return false;
// Invalidate the string cache.
stockpile_id = -1;
return true;
}
bool collect_settings(building_stockpilest *sp) {
// Find strings representing the job to order, and the trigger condition.
// There might be a memory leak here; C++ is odd like that.
auto L = Lua::Core::State;
color_ostream_proxy out(Core::getInstance().getConsole());
CoreSuspendClaimer suspend;
Lua::StackUnwinder top(L);
if (!lua_checkstack(L, 2))
return false;
if (!Lua::PushModulePublic(out, L, "plugins.stockflow", "stockpile_settings"))
return false;
Lua::Push(L, sp);
if (!Lua::SafeCall(out, L, 1, 2))
return false;
if (!lua_isstring(L, -1))
return false;
current_trigger = lua_tostring(L, -1);
lua_pop(L, 1);
if (!lua_isstring(L, -1))
return false;
current_job = lua_tostring(L, -1);
lua_pop(L, 1);
stockpile_id = sp->id;
return true;
}
void draw(building_stockpilest *sp) {
if (sp->id != stockpile_id) {
if (!collect_settings(sp)) {
Core::printerr("Stockflow job collection failed!\n");
return;
}
}
auto dims = Gui::getDwarfmodeViewDims();
int left_margin = dims.menu_x1 + 1;
int x = left_margin;
int y = dims.y2 - 3;
int links = 0;
links += sp->links.give_to_pile.size();
links += sp->links.take_from_pile.size();
links += sp->links.give_to_workshop.size();
links += sp->links.take_from_workshop.size();
if (links + 12 >= y)
y += 1;
OutputHotkeyString(x, y, current_job, "j", true, left_margin, COLOR_WHITE, COLOR_LIGHTRED);
if (*current_trigger)
OutputHotkeyString(x, y, current_trigger, " J", true, left_margin, COLOR_WHITE, COLOR_LIGHTRED);
}
private:
long stockpile_id;
bool initialized;
bool bookkeeping;
const char *current_job;
const char *current_trigger;
};
static LuaHelper helper;
#define DELTA_TICKS 600
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (!enabled)
return CR_OK;
if (!Maps::IsValid())
return CR_OK;
static decltype(world->frame_counter) last_frame_count = 0;
if (DFHack::World::ReadPauseState())
return CR_OK;
if (world->frame_counter - last_frame_count < DELTA_TICKS)
return CR_OK;
last_frame_count = world->frame_counter;
helper.cycle(out);
return CR_OK;
}
/*
* Interface hooks
*/
struct stockflow_hook : public df::viewscreen_dwarfmodest {
typedef df::viewscreen_dwarfmodest interpose_base;
bool handleInput(set<df::interface_key> *input) {
building_stockpilest *sp = get_selected_stockpile();
if (!sp)
return false;
if (input->count(interface_key::CUSTOM_J)) {
// Select a new order for this stockpile.
if (!helper.stockpile_method("select_order", sp)) {
Core::printerr("Stockflow order selection failed!\n");
}
return true;
} else if (input->count(interface_key::CUSTOM_SHIFT_J)) {
// Toggle the order trigger for this stockpile.
if (!helper.stockpile_method("toggle_trigger", sp)) {
Core::printerr("Stockflow trigger toggle failed!\n");
}
return true;
}
return false;
}
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input)) {
if (!handleInput(input))
INTERPOSE_NEXT(feed)(input);
}
DEFINE_VMETHOD_INTERPOSE(void, render, ()) {
INTERPOSE_NEXT(render)();
building_stockpilest *sp = get_selected_stockpile();
if (sp)
helper.draw(sp);
}
};
IMPLEMENT_VMETHOD_INTERPOSE(stockflow_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(stockflow_hook, render);
static bool apply_hooks(color_ostream &out, bool enabling) {
if (enabling && !gps) {
out.printerr("Stockflow needs graphics.\n");
return false;
}
if (!INTERPOSE_HOOK(stockflow_hook, feed).apply(enabling) || !INTERPOSE_HOOK(stockflow_hook, render).apply(enabling)) {
out.printerr("Could not %s stockflow hooks!\n", enabling? "insert": "remove");
return false;
}
if (!helper.reset(out, enabling && Maps::IsValid())) {
out.printerr("Could not reset stockflow world data!\n");
return false;
}
return true;
}
static command_result stockflow_cmd(color_ostream &out, vector <string> & parameters) {
bool desired = enabled;
if (parameters.size() == 1) {
if (parameters[0] == "enable" || parameters[0] == "on" || parameters[0] == "1") {
desired = true;
fast = false;
} else if (parameters[0] == "disable" || parameters[0] == "off" || parameters[0] == "0") {
desired = false;
fast = false;
} else if (parameters[0] == "fast" || parameters[0] == "always" || parameters[0] == "2") {
desired = true;
fast = true;
} else if (parameters[0] == "usage" || parameters[0] == "help" || parameters[0] == "?") {
out.print("%s: %s\nUsage:\n%s", name, tagline, usage);
return CR_OK;
} else if (parameters[0] == "list") {
if (!enabled) {
out.printerr("Stockflow is not currently enabled.\n");
return CR_FAILURE;
}
if (!Maps::IsValid()) {
out.printerr("You haven't loaded a map yet.\n");
return CR_FAILURE;
}
// Tell Lua to list any saved stockpile orders.
return helper.command_method("list_orders", out)? CR_OK: CR_FAILURE;
} else if (parameters[0] != "status") {
return CR_WRONG_USAGE;
}
} else if (parameters.size() > 1) {
return CR_WRONG_USAGE;
}
if (desired != enabled) {
if (!apply_hooks(out, desired)) {
return CR_FAILURE;
}
}
out.print("Stockflow is %s %s%s.\n", (desired == enabled)? "currently": "now", desired? "enabled": "disabled", fast? ", in fast mode": "");
enabled = desired;
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_MAP_LOADED) {
if (!helper.reset(out, enabled)) {
out.printerr("Could not load stockflow world data!\n");
return CR_FAILURE;
}
} else if (event == DFHack::SC_MAP_UNLOADED) {
if (!helper.reset(out, false)) {
out.printerr("Could not unload stockflow world data!\n");
return CR_FAILURE;
}
}
return CR_OK;
}
DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) {
/* Accept the "enable stockflow"/"disable stockflow" syntax, where available. */
/* Same as "stockflow enable"/"stockflow disable" except without the status line. */
if (enable != enabled) {
if (!apply_hooks(out, enable)) {
return CR_FAILURE;
}
enabled = enable;
}
return CR_OK;
}
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
helper.init();
if (AUTOENABLE) {
if (!apply_hooks(out, true)) {
return CR_FAILURE;
}
enabled = true;
}
commands.push_back(PluginCommand(name, tagline, stockflow_cmd, false, usage));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown(color_ostream &out) {
return plugin_enable(out, false);
}