forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembark-tools.cpp
524 lines (480 loc) · 15.3 KB
/
embark-tools.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include "Console.h"
#include "Core.h"
#include "DataDefs.h"
#include "Export.h"
#include "PluginManager.h"
#include "modules/Screen.h"
#include "modules/Gui.h"
#include <algorithm>
#include <set>
#include <VTableInterpose.h>
#include "ColorText.h"
#include "df/viewscreen_choose_start_sitest.h"
#include "df/interface_key.h"
using namespace DFHack;
struct EmbarkTool
{
std::string id;
std::string name;
std::string desc;
bool enabled;
df::interface_key toggle_key;
};
EmbarkTool embark_tools[] = {
{"anywhere", "Embark anywhere", "Allows embarking anywhere on the world map",
false, df::interface_key::CUSTOM_A},
{"nano", "Nano embark", "Allows the embark size to be decreased below 2x2",
false, df::interface_key::CUSTOM_N},
{"sand", "Sand indicator", "Displays an indicator when sand is present on the given embark site",
false, df::interface_key::CUSTOM_S},
{"sticky", "Stable position", "Maintains the selected local area while navigating the world map",
false, df::interface_key::CUSTOM_P},
};
#define NUM_TOOLS sizeof(embark_tools) / sizeof(EmbarkTool)
command_result embark_tools_cmd (color_ostream &out, std::vector <std::string> & parameters);
void OutputString (int8_t color, int &x, int y, const std::string &text);
bool tool_exists (std::string tool_name);
bool tool_enabled (std::string tool_name);
bool tool_enable (std::string tool_name, bool enable_state);
void tool_update (std::string tool_name);
class embark_tools_settings : public dfhack_viewscreen
{
public:
embark_tools_settings () { };
~embark_tools_settings () { };
void help () { };
std::string getFocusString () { return "embark-tools/options"; };
void render ()
{
int x;
auto dim = Screen::getWindowSize();
int width = 50,
height = 4 + 1 + NUM_TOOLS, // Padding + lower row
min_x = (dim.x - width) / 2,
max_x = (dim.x + width) / 2,
min_y = (dim.y - height) / 2,
max_y = min_y + height;
Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_DARKGREY), min_x, min_y, max_x, max_y);
Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_BLACK), min_x + 1, min_y + 1, max_x - 1, max_y - 1);
x = min_x + 2;
OutputString(COLOR_LIGHTRED, x, max_y - 2, Screen::getKeyDisplay(df::interface_key::SELECT));
OutputString(COLOR_WHITE, x, max_y - 2, "/");
OutputString(COLOR_LIGHTRED, x, max_y - 2, Screen::getKeyDisplay(df::interface_key::LEAVESCREEN));
OutputString(COLOR_WHITE, x, max_y - 2, ": Done");
for (int i = 0, y = min_y + 2; i < NUM_TOOLS; i++, y++)
{
EmbarkTool t = embark_tools[i];
x = min_x + 2;
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(t.toggle_key));
OutputString(COLOR_WHITE, x, y, ": " + t.name + (t.enabled ? ": Enabled" : ": Disabled"));
}
};
void feed (std::set<df::interface_key> * input)
{
if (input->count(df::interface_key::SELECT) || input->count(df::interface_key::LEAVESCREEN))
{
Screen::dismiss(this);
return;
}
for (auto iter = input->begin(); iter != input->end(); iter++)
{
df::interface_key key = *iter;
for (int i = 0; i < NUM_TOOLS; i++)
{
if (embark_tools[i].toggle_key == key)
{
embark_tools[i].enabled = !embark_tools[i].enabled;
}
}
}
};
};
/*
* Logic
*/
void update_embark_sidebar (df::viewscreen_choose_start_sitest * screen)
{
bool is_top = false;
if (screen->embark_pos_min.y == 0)
is_top = true;
std::set<df::interface_key> keys;
keys.insert(df::interface_key::SETUP_LOCAL_Y_MUP);
screen->feed(&keys);
if (!is_top)
{
keys.insert(df::interface_key::SETUP_LOCAL_Y_MDOWN);
screen->feed(&keys);
}
}
void resize_embark (df::viewscreen_choose_start_sitest * screen, int dx, int dy)
{
/* Reproduces DF's embark resizing functionality
* Local area resizes up and to the right, unless it's already touching the edge
*/
int x1 = screen->embark_pos_min.x,
x2 = screen->embark_pos_max.x,
y1 = screen->embark_pos_min.y,
y2 = screen->embark_pos_max.y,
width = x2 - x1 + dx,
height = y2 - y1 + dy;
if (x1 == x2 && dx == -1)
dx = 0;
if (y1 == y2 && dy == -1)
dy = 0;
x2 += dx; // Resize right
while (x2 > 15)
{
x2--;
x1--;
}
x1 = std::max(0, x1);
y1 -= dy; // Resize up
while (y1 < 0)
{
y1++;
y2++;
}
y2 = std::min(15, y2);
screen->embark_pos_min.x = x1;
screen->embark_pos_max.x = x2;
screen->embark_pos_min.y = y1;
screen->embark_pos_max.y = y2;
update_embark_sidebar(screen);
}
std::string sand_indicator = "";
bool sand_dirty = true; // Flag set when update is needed
void sand_update ()
{
CoreSuspendClaimer suspend;
df::viewscreen * top = Gui::getCurViewscreen();
VIRTUAL_CAST_VAR(screen, df::viewscreen_choose_start_sitest, top);
buffered_color_ostream out;
Core::getInstance().runCommand(out, "prospect");
auto fragments = out.fragments();
sand_indicator = "";
for (auto iter = fragments.begin(); iter != fragments.end(); iter++)
{
std::string fragment = iter->second;
if (fragment.find("SAND_") != std::string::npos)
{
sand_indicator = "Sand";
break;
}
}
sand_dirty = false;
}
int sticky_pos[] = {0, 0, 3, 3};
bool sticky_moved = false;
void sticky_save (df::viewscreen_choose_start_sitest * screen)
{
sticky_pos = {
screen->embark_pos_min.x,
screen->embark_pos_max.x,
screen->embark_pos_min.y,
screen->embark_pos_max.y,
};
}
void sticky_apply (df::viewscreen_choose_start_sitest * screen)
{
screen->embark_pos_min.x = sticky_pos[0];
screen->embark_pos_max.x = sticky_pos[1];
screen->embark_pos_min.y = sticky_pos[2];
screen->embark_pos_max.y = sticky_pos[3];
update_embark_sidebar(screen);
}
/*
* Viewscreen hooks
*/
void OutputString (int8_t color, int &x, int y, const std::string &text)
{
Screen::paintString(Screen::Pen(' ', color, 0), x, y, text);
x += text.length();
}
struct choose_start_site_hook : df::viewscreen_choose_start_sitest
{
typedef df::viewscreen_choose_start_sitest interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, feed, (std::set<df::interface_key> *input))
{
bool prevent_default = false;
df::viewscreen * top = Gui::getCurViewscreen();
VIRTUAL_CAST_VAR(screen, df::viewscreen_choose_start_sitest, top);
if (tool_enabled("anywhere"))
{
for (auto iter = input->begin(); iter != input->end(); iter++)
{
df::interface_key key = *iter;
if (key == df::interface_key::SETUP_EMBARK)
{
prevent_default = true;
screen->in_embark_normal = 1;
}
}
}
if (input->count(df::interface_key::CUSTOM_S))
{
Screen::show(new embark_tools_settings);
return;
}
if (tool_enabled("nano"))
{
for (auto iter = input->begin(); iter != input->end(); iter++)
{
df::interface_key key = *iter;
bool is_resize = true;
int dx = 0, dy = 0;
switch (key)
{
case df::interface_key::SETUP_LOCAL_Y_UP:
dy = 1;
break;
case df::interface_key::SETUP_LOCAL_Y_DOWN:
dy = -1;
break;
case df::interface_key::SETUP_LOCAL_X_UP:
dx = 1;
break;
case df::interface_key::SETUP_LOCAL_X_DOWN:
dx = -1;
break;
default:
is_resize = false;
}
if (is_resize)
{
prevent_default = true;
resize_embark(screen, dx, dy);
}
}
}
if (tool_enabled("sticky"))
{
for (auto iter = input->begin(); iter != input->end(); iter++)
{
df::interface_key key = *iter;
bool is_motion = false;
int dx = 0, dy = 0;
switch (key)
{
case df::interface_key::CURSOR_UP:
case df::interface_key::CURSOR_DOWN:
case df::interface_key::CURSOR_LEFT:
case df::interface_key::CURSOR_RIGHT:
case df::interface_key::CURSOR_UPLEFT:
case df::interface_key::CURSOR_UPRIGHT:
case df::interface_key::CURSOR_DOWNLEFT:
case df::interface_key::CURSOR_DOWNRIGHT:
case df::interface_key::CURSOR_UP_FAST:
case df::interface_key::CURSOR_DOWN_FAST:
case df::interface_key::CURSOR_LEFT_FAST:
case df::interface_key::CURSOR_RIGHT_FAST:
case df::interface_key::CURSOR_UPLEFT_FAST:
case df::interface_key::CURSOR_UPRIGHT_FAST:
case df::interface_key::CURSOR_DOWNLEFT_FAST:
case df::interface_key::CURSOR_DOWNRIGHT_FAST:
is_motion = true;
break;
default:
is_motion = false;
}
if (is_motion && !sticky_moved)
{
sticky_save(screen);
sticky_moved = true;
}
}
}
if (tool_enabled("sand"))
{
sand_dirty = true;
}
if (!prevent_default)
INTERPOSE_NEXT(feed)(input);
}
DEFINE_VMETHOD_INTERPOSE(void, render, ())
{
df::viewscreen * top = Gui::getCurViewscreen();
VIRTUAL_CAST_VAR(screen, df::viewscreen_choose_start_sitest, top);
if (!screen)
return;
if (tool_enabled("sticky") && sticky_moved)
{
sticky_apply(screen);
sticky_moved = false;
}
INTERPOSE_NEXT(render)();
auto dim = Screen::getWindowSize();
int x = 1,
y = dim.y - 5;
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::CUSTOM_S));
OutputString(COLOR_WHITE, x, y, ": Enabled: ");
std::list<std::string> parts;
for (int i = 0; i < NUM_TOOLS; i++)
{
if (embark_tools[i].enabled)
{
parts.push_back(embark_tools[i].name);
parts.push_back(", ");
}
}
if (parts.size())
{
parts.pop_back(); // Remove trailing comma
for (auto iter = parts.begin(); iter != parts.end(); iter++)
{
OutputString(COLOR_LIGHTMAGENTA, x, y, *iter);
}
}
else
{
OutputString(COLOR_LIGHTMAGENTA, x, y, "(none)");
}
if (tool_enabled("anywhere"))
{
x = 20; y = dim.y - 2;
if (screen->page >= 0 && screen->page <= 4)
{
// Only display on five map pages, not on site finder or notes
OutputString(COLOR_WHITE, x, y, ": Embark!");
}
}
if (tool_enabled("sand"))
{
if (sand_dirty)
{
sand_update();
}
x = dim.x - 28; y = 13;
if (screen->page == 0)
{
OutputString(COLOR_YELLOW, x, y, sand_indicator);
}
}
}
};
IMPLEMENT_VMETHOD_INTERPOSE(choose_start_site_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(choose_start_site_hook, render);
/*
* Tool management
*/
bool tool_exists (std::string tool_name)
{
for (int i = 0; i < NUM_TOOLS; i++)
{
if (embark_tools[i].id == tool_name)
return true;
}
return false;
}
bool tool_enabled (std::string tool_name)
{
for (int i = 0; i < NUM_TOOLS; i++)
{
if (embark_tools[i].id == tool_name)
return embark_tools[i].enabled;
}
return false;
}
bool tool_enable (std::string tool_name, bool enable_state)
{
int n = 0;
for (int i = 0; i < NUM_TOOLS; i++)
{
if (embark_tools[i].id == tool_name || tool_name == "all")
{
embark_tools[i].enabled = enable_state;
tool_update(tool_name);
n++;
}
}
return (bool)n;
}
void tool_update (std::string tool_name)
{
// Called whenever a tool is enabled/disabled
if (tool_name == "sand")
{
sand_dirty = true;
}
}
/*
* Plugin management
*/
DFHACK_PLUGIN("embark-tools");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
std::string help = "";
help += "embark-tools (enable/disable) tool [tool...]\n"
"Tools:\n";
for (int i = 0; i < NUM_TOOLS; i++)
{
help += (" " + embark_tools[i].id + ": " + embark_tools[i].desc + "\n");
}
commands.push_back(PluginCommand(
"embark-tools",
"A collection of embark tools",
embark_tools_cmd,
false,
help.c_str()
));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown (color_ostream &out)
{
INTERPOSE_HOOK(choose_start_site_hook, feed).remove();
INTERPOSE_HOOK(choose_start_site_hook, render).remove();
return CR_OK;
}
DFhackCExport command_result plugin_enable (color_ostream &out, bool enable)
{
if (is_enabled != enable)
{
if (!INTERPOSE_HOOK(choose_start_site_hook, feed).apply(enable) ||
!INTERPOSE_HOOK(choose_start_site_hook, render).apply(enable))
return CR_FAILURE;
is_enabled = enable;
}
return CR_OK;
}
command_result embark_tools_cmd (color_ostream &out, std::vector <std::string> & parameters)
{
CoreSuspender suspend;
if (parameters.size())
{
// Set by "enable"/"disable" - allows for multiple commands, e.g. "enable nano disable anywhere"
bool enable_state = true;
for (size_t i = 0; i < parameters.size(); i++)
{
if (parameters[i] == "enable")
{
enable_state = true;
plugin_enable(out, true); // Enable plugin
}
else if (parameters[i] == "disable")
enable_state = false;
else if (tool_exists(parameters[i]) || parameters[i] == "all")
{
tool_enable(parameters[i], enable_state);
}
else
return CR_WRONG_USAGE;
}
}
else
{
if (is_enabled)
{
out << "Tool status:" << std::endl;
for (int i = 0; i < NUM_TOOLS; i++)
{
EmbarkTool t = embark_tools[i];
out << t.name << " (" << t.id << "): " << (t.enabled ? "Enabled" : "Disabled") << std::endl;
}
}
else
{
out << "Plugin not enabled" << std::endl;
}
}
return CR_OK;
}