forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaquifer.cpp
More file actions
262 lines (210 loc) · 9.1 KB
/
aquifer.cpp
File metadata and controls
262 lines (210 loc) · 9.1 KB
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
#include "Debug.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "TileTypes.h"
#include "modules/Maps.h"
#include "df/map_block.h"
#include "df/world.h"
using std::string;
using std::vector;
using namespace DFHack;
DFHACK_PLUGIN("aquifer");
REQUIRE_GLOBAL(world);
namespace DFHack {
DBG_DECLARE(aquifer, log, DebugCategory::LINFO);
}
static command_result do_command(color_ostream &out, vector<string> ¶meters);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
commands.push_back(PluginCommand(
plugin_name,
"Add, remove, or modify aquifers.",
do_command));
return CR_OK;
}
static command_result do_command(color_ostream &out, vector<string> ¶meters) {
CoreSuspender suspend;
if (!Core::getInstance().isMapLoaded()) {
out.printerr("Cannot run %s without a loaded map.\n", plugin_name);
return CR_FAILURE;
}
bool show_help = false;
if (!Lua::CallLuaModuleFunction(out, "plugins.aquifer", "parse_commandline", std::make_tuple(parameters),
1, [&](lua_State *L) {
show_help = !lua_toboolean(L, 1);
})) {
return CR_FAILURE;
}
return show_help ? CR_WRONG_USAGE : CR_OK;
}
////////////////////////////////////
// Lua API
//
bool is_open(int16_t x, int16_t y, int16_t z) {
df::tiletype *tt = Maps::getTileType(x, y, z);
if (!tt) return false;
return ENUM_ATTR(tiletype_shape, passable_flow, tileShape(*tt));
}
bool is_leaky(int16_t x, int16_t y, int16_t z) {
return is_open(x, y, z-1) ||
is_open(x, y-1, z) ||
is_open(x-1, y, z) ||
is_open(x+1, y, z) ||
is_open(x, y+1, z);
}
bool is_leaky(const df::coord & pos) {
return is_leaky(pos.x, pos.y, pos.z);
}
static bool is_rough_natural_wall(int16_t x, int16_t y, int16_t z) {
df::tiletype *tt = Maps::getTileType(x, y, z);
if (!tt)
return false;
return tileShape(*tt) == df::tiletype_shape::WALL &&
(tileMaterial(*tt) == df::tiletype_material::STONE ||
tileMaterial(*tt) == df::tiletype_material::SOIL) &&
tileSpecial(*tt) != df::tiletype_special::SMOOTH;
}
static bool is_rough_natural_wall(const df::coord & pos) {
return is_rough_natural_wall(pos.x, pos.y, pos.z);
}
static auto DEFAULT_PRE_BLOCK_FN = [](df::map_block *block){return block->flags.bits.has_aquifer;};
static void for_block(int minz, int maxz, const df::coord & pos1, const df::coord & pos2,
std::function<void(const df::coord &)> pos_fn,
std::function<bool(df::map_block *)> pre_block_fn = DEFAULT_PRE_BLOCK_FN)
{
for (auto block : world->map.map_blocks) {
const df::coord & bpos = block->map_pos;
if (maxz < bpos.z || minz > bpos.z ||
bpos.x > pos2.x || bpos.x + 15 < pos1.x ||
bpos.y > pos2.y || bpos.y + 15 < pos1.y)
{
continue;
}
if (!pre_block_fn(block))
continue;
for (int xoff = 0; xoff <= 15; ++xoff) {
for (int yoff = 0; yoff <= 15; ++yoff) {
df::coord pos = bpos + df::coord(xoff, yoff, 0);
if (pos.x >= pos1.x && pos.x <= pos2.x && pos.y >= pos1.y && pos.y <= pos2.y)
pos_fn(pos);
}
}
}
}
static int get_max_ground_z(color_ostream &out, const df::coord &pos1, const df::coord &pos2)
{
int maxz = -1;
for_block(pos1.z, pos2.z, pos1, pos2, [&](const df::coord &pos){
if (maxz < pos.z && is_rough_natural_wall(pos))
maxz = pos.z;
}, [&](df::map_block *block){
return maxz < block->map_pos.z;
});
return maxz;
}
static int get_max_aq_z(color_ostream &out, const df::coord &pos1, const df::coord &pos2)
{
int maxz = -1;
for_block(pos1.z, pos2.z, pos1, pos2, [](const df::coord &pos){
}, [&](df::map_block *block){
if (block->flags.bits.has_aquifer && maxz < block->map_pos.z)
maxz = block->map_pos.z;
return false;
});
return maxz;
}
static void get_z_range(color_ostream &out, int & minz, int & maxz, const df::coord & pos1,
const df::coord & pos2, int levels, bool top_is_aq = false, int skip_top = 0)
{
DEBUG(log,out).print("get_z_range: top_is_aq=%d, skip_top=%d\n", top_is_aq, skip_top);
if (!top_is_aq)
maxz = get_max_ground_z(out, pos1, pos2) - skip_top;
else
maxz = get_max_aq_z(out, pos1, pos2) - skip_top;
minz = std::max((int)pos1.z, maxz - levels + 1);
DEBUG(log,out).print("calculated z range: minz=%d, maxz=%d\n", minz, maxz);
}
static void aquifer_list(color_ostream &out, df::coord pos1, df::coord pos2, int levels, bool leaky) {
DEBUG(log,out).print("entering aquifer_list: pos1=%d,%d,%d, pos2=%d,%d,%d, levels=%d, leaky=%d\n",
pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z, levels, leaky);
std::map<int, int, std::greater<int>> light_tiles, heavy_tiles;
int minz = 0, maxz = -1;
get_z_range(out, minz, maxz, pos1, pos2, levels);
for_block(minz, maxz, pos1, pos2, [&](const df::coord &pos){
if (Maps::isTileAquifer(pos) && (!leaky || is_leaky(pos))) {
if (Maps::isTileHeavyAquifer(pos))
++heavy_tiles[pos.z];
else
++light_tiles[pos.z];
}
});
if (light_tiles.empty() && heavy_tiles.empty()) {
out.print("No %saquifer tiles in the specified range.\n", leaky ? "leaking " : "");
} else {
int elev_off = world->map.region_z - 100;
for (int z = maxz; z >= minz; --z) {
int lcount = light_tiles.contains(z) ? light_tiles[z] : 0;
int hcount = heavy_tiles.contains(z) ? heavy_tiles[z] : 0;
if (lcount || hcount)
out.print("z-level %3d (elevation %4d) has %6d %slight aquifer tile(s) and %6d %sheavy aquifer tile(s)\n",
z, z+elev_off, lcount, leaky ? "leaking " : "", hcount, leaky ? "leaking " : "");
}
}
}
static int aquifer_drain(color_ostream &out, string aq_type,
df::coord pos1, df::coord pos2, int skip_top, int levels, bool leaky)
{
DEBUG(log,out).print("entering aquifer_drain: aq_type=%s, pos1=%d,%d,%d, pos2=%d,%d,%d,"
" skip_top=%d, levels=%d, leaky=%d\n", aq_type.c_str(),
pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z, skip_top, levels, leaky);
const bool all = aq_type == "all";
const bool heavy_state = aq_type == "heavy";
int modified = Maps::removeAreaAquifer(pos1, pos2, [&](df::coord pos, df::map_block* block) -> bool {
TRACE(log, out).print("examining tile: pos=%d,%d,%d\n", pos.x, pos.y, pos.z);
return Maps::isTileAquifer(pos)
&& (all || Maps::isTileHeavyAquifer(pos) == heavy_state)
&& (!leaky || is_leaky(pos));
});
DEBUG(log, out).print("drained aquifer tiles in area: pos1=%d,%d,%d, pos2=%d,%d,%d, heavy_state=%d, all=%d, count=%d\n",
pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z, heavy_state, all, modified);
return modified;
}
static int aquifer_convert(color_ostream &out, string aq_type,
df::coord pos1, df::coord pos2, int skip_top, int levels, bool leaky)
{
DEBUG(log,out).print("entering aquifer_convert: aq_type=%s, pos1=%d,%d,%d, pos2=%d,%d,%d,"
" skip_top=%d, levels=%d, leaky=%d\n", aq_type.c_str(),
pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z, skip_top, levels, leaky);
const bool heavy_state = aq_type == "heavy";
int modified = Maps::setAreaAquifer(pos1, pos2, heavy_state, [&](df::coord pos, df::map_block* block) -> bool {
TRACE(log, out).print("examining tile: pos=%d,%d,%d\n", pos.x, pos.y, pos.z);
return Maps::isTileAquifer(pos)
&& Maps::isTileHeavyAquifer(pos) != heavy_state
&& (!leaky || is_leaky(pos));
});
DEBUG(log, out).print("converted aquifer tiles in area: pos1=%d,%d,%d, pos2=%d,%d,%d, heavy_state=%d, count=%d\n",
pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z, heavy_state, modified);
return modified;
}
static int aquifer_add(color_ostream &out, string aq_type,
df::coord pos1, df::coord pos2, int skip_top, int levels, bool leaky)
{
DEBUG(log,out).print("entering aquifer_add: aq_type=%s, pos1=%d,%d,%d, pos2=%d,%d,%d,"
" skip_top=%d, levels=%d, leaky=%d\n", aq_type.c_str(),
pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z, skip_top, levels, leaky);
const bool heavy_state = aq_type == "heavy";
int modified = Maps::setAreaAquifer(pos1, pos2, heavy_state, [&](df::coord pos, df::map_block* block) -> bool {
TRACE(log, out).print("examining tile: pos=%d,%d,%d\n", pos.x, pos.y, pos.z);
return (leaky || !is_leaky(pos))
&& (!Maps::isTileAquifer(pos) || Maps::isTileHeavyAquifer(pos) != heavy_state);
});
DEBUG(log, out).print("added aquifer tiles in area: pos1=%d,%d,%d, pos2=%d,%d,%d, heavy_state=%d, count=%d\n",
pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z, heavy_state, modified);
return modified;
}
DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(aquifer_list),
DFHACK_LUA_FUNCTION(aquifer_drain),
DFHACK_LUA_FUNCTION(aquifer_convert),
DFHACK_LUA_FUNCTION(aquifer_add),
DFHACK_LUA_END
};