-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathfastdwarf.cpp
210 lines (164 loc) · 6.32 KB
/
fastdwarf.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
#include "Debug.h"
#include "PluginManager.h"
#include "modules/Maps.h"
#include "modules/Persistence.h"
#include "modules/Units.h"
#include "modules/World.h"
#include "df/unit.h"
#include "df/unit_relationship_type.h"
using std::string;
using std::vector;
using namespace DFHack;
DFHACK_PLUGIN("fastdwarf");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(world);
using df::global::debug_turbospeed; // soft dependency, so not REQUIRE_GLOBAL
namespace DFHack {
// for configuration-related logging
DBG_DECLARE(fastdwarf, control, DebugCategory::LINFO);
// for logging during the update cycle
DBG_DECLARE(fastdwarf, cycle, DebugCategory::LINFO);
}
static const string CONFIG_KEY = string(plugin_name) + "/config";
static PersistentDataItem config;
enum ConfigValues {
CONFIG_FAST = 0,
CONFIG_TELE = 1,
};
static command_result do_command(color_ostream &out, vector<string> & parameters);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> & commands) {
DEBUG(control,out).print("initializing %s\n", plugin_name);
commands.push_back(PluginCommand(
"fastdwarf",
"Citizens walk fast and and finish jobs instantly.",
do_command));
return CR_OK;
}
static void set_state(color_ostream &out, int fast, int tele) {
DEBUG(control,out).print("setting state: fast=%d, tele=%d\n", fast, tele);
is_enabled = fast || tele;
config.set_int(CONFIG_FAST, fast);
config.set_int(CONFIG_TELE, tele);
if (debug_turbospeed)
*debug_turbospeed = fast == 2;
else if (fast == 2)
out.printerr("Speed level 2 not available.\n");
}
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
if (!Core::getInstance().isMapLoaded() || !World::IsSiteLoaded()) {
out.printerr("Cannot enable %s without a loaded fort.\n", plugin_name);
return CR_FAILURE;
}
if (enable != is_enabled) {
set_state(out, enable ? 1 : 0, 0);
} else {
DEBUG(control,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) {
DEBUG(control,out).print("shutting down %s\n", plugin_name);
// make sure the debug flag doesn't get left on
if (debug_turbospeed)
*debug_turbospeed = false;
return CR_OK;
}
DFhackCExport command_result plugin_load_site_data (color_ostream &out) {
config = World::GetPersistentSiteData(CONFIG_KEY);
if (!config.isValid()) {
DEBUG(control,out).print("no config found in this save; initializing\n");
config = World::AddPersistentSiteData(CONFIG_KEY);
set_state(out, false, false);
} else {
is_enabled = config.get_int(CONFIG_FAST) || config.get_int(CONFIG_TELE);
DEBUG(control,out).print("loading persisted state: fast=%d, tele=%d\n",
config.get_int(CONFIG_FAST),
config.get_int(CONFIG_TELE));
}
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_WORLD_UNLOADED) {
if (is_enabled) {
DEBUG(control,out).print("world unloaded; disabling %s\n",
plugin_name);
is_enabled = false;
}
}
return CR_OK;
}
static void do_tele(color_ostream &out, df::unit * unit) {
// skip dwarves that are dragging creatures or being dragged
if ((unit->relationship_ids[df::unit_relationship_type::Draggee] != -1) ||
(unit->relationship_ids[df::unit_relationship_type::Dragger] != -1))
return;
// skip dwarves that are following other units
if (unit->following != 0)
return;
// skip unconscious units
if (unit->counters.unconscious > 0)
return;
// don't do anything if the dwarf isn't going anywhere
if (!unit->pos.isValid() || !unit->path.dest.isValid() || unit->pos == unit->path.dest)
return;
// skip units with no current job so they can still meander and fulfill needs
if (!unit->job.current_job)
return;
// don't do anything if the destination is in a different pathability group or is unrevealed
if (!Maps::canWalkBetween(unit->pos, unit->path.dest))
return;
if (!Maps::isTileVisible(unit->path.dest))
return;
DEBUG(cycle,out).print("teleporting unit %d\n", unit->id);
if (!Units::teleport(unit, unit->path.dest))
return;
unit->path.path.clear();
}
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
DEBUG(cycle,out).print("running %s cycle\n", plugin_name);
// fast mode 2 is handled by DF itself
bool is_fast = config.get_int(CONFIG_FAST) == 1;
bool is_tele = config.get_int(CONFIG_TELE) == 1;
Units::forCitizens([&](auto unit) {
if (is_tele)
do_tele(out, unit);
if (is_fast) {
DEBUG(cycle,out).print("fastifying unit %d\n", unit->id);
Units::setGroupActionTimers(out, unit, 1, df::unit_action_type_group::All);
}
});
return CR_OK;
}
/////////////////////////////////////////////////////
// CLI logic
//
static command_result do_command(color_ostream &out, vector <string> & parameters)
{
if (!Core::getInstance().isMapLoaded() || !World::IsSiteLoaded()) {
out.printerr("Cannot run %s without a loaded fort.\n", plugin_name);
return CR_FAILURE;
}
const size_t num_params = parameters.size();
if (num_params > 2 || (num_params > 1 && parameters[0] == "help"))
return CR_WRONG_USAGE;
if (num_params == 0 || parameters[0] == "status") {
out.print("Current state: fast = %d, teleport = %d.\n",
config.get_int(CONFIG_FAST),
config.get_int(CONFIG_TELE));
return CR_OK;
}
int fast = string_to_int(parameters[0]);
int tele = num_params >= 2 ? string_to_int(parameters[1]) : 0;
if (fast < 0 || fast > 2) {
out.printerr("Invalid value for fast: '%s'", parameters[0].c_str());
return CR_WRONG_USAGE;
}
if (tele < 0 || tele > 1) {
out.printerr("Invalid value for tele: '%s'", parameters[1].c_str());
return CR_WRONG_USAGE;
}
set_state(out, fast, tele);
return CR_OK;
}