forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brushes.h
290 lines (265 loc) · 7.49 KB
/
Brushes.h
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
#pragma once
#include <llimits.h>
#include <sstream>
#include <string>
#include <stack>
#include <set>
typedef vector <df::coord> coord_vec;
class Brush
{
public:
virtual ~Brush(){};
virtual coord_vec points(MapExtras::MapCache & mc,DFHack::DFCoord start) = 0;
virtual std::string str() const {
return "unknown";
}
};
/**
* generic 3D rectangle brush. you can specify the dimensions of
* the rectangle and optionally which tile is its 'center'
*/
class RectangleBrush : public Brush
{
public:
RectangleBrush(int x, int y, int z = 1, int centerx = -1, int centery = -1, int centerz = -1)
{
if(centerx == -1)
cx_ = x/2;
else
cx_ = centerx;
if(centery == -1)
cy_ = y/2;
else
cy_ = centery;
if(centerz == -1)
cz_ = z/2;
else
cz_ = centerz;
x_ = x;
y_ = y;
z_ = z;
};
coord_vec points(MapExtras::MapCache & mc, DFHack::DFCoord start)
{
coord_vec v;
DFHack::DFCoord iterstart(start.x - cx_, start.y - cy_, start.z - cz_);
DFHack::DFCoord iter = iterstart;
for(int xi = 0; xi < x_; xi++)
{
for(int yi = 0; yi < y_; yi++)
{
for(int zi = 0; zi < z_; zi++)
{
if(mc.testCoord(iter))
v.push_back(iter);
iter.z++;
}
iter.z = iterstart.z;
iter.y++;
}
iter.y = iterstart.y;
iter.x ++;
}
return v;
};
~RectangleBrush(){};
std::string str() const {
if (x_ == 1 && y_ == 1 && z_ == 1)
{
return "point";
}
else
{
std::ostringstream ss;
ss << "rect: " << x_ << "/" << y_ << "/" << z_ << std::endl;
return ss.str();
}
}
private:
int x_, y_, z_;
int cx_, cy_, cz_;
};
/**
* stupid block brush, legacy. use when you want to apply something to a whole DF map block.
*/
class BlockBrush : public Brush
{
public:
BlockBrush(){};
~BlockBrush(){};
coord_vec points(MapExtras::MapCache & mc, DFHack::DFCoord start)
{
coord_vec v;
DFHack::DFCoord blockc = start / 16;
DFHack::DFCoord iterc = blockc * 16;
if( !mc.testCoord(start) )
return v;
auto starty = iterc.y;
for(int xi = 0; xi < 16; xi++)
{
for(int yi = 0; yi < 16; yi++)
{
v.push_back(iterc);
iterc.y++;
}
iterc.y = starty;
iterc.x ++;
}
return v;
};
std::string str() const {
return "block";
}
};
/**
* Column from a position through open space tiles
* example: create a column of magma
*/
class ColumnBrush : public Brush
{
public:
ColumnBrush(){};
~ColumnBrush(){};
coord_vec points(MapExtras::MapCache & mc, DFHack::DFCoord start)
{
coord_vec v;
bool juststarted = true;
while (mc.testCoord(start))
{
df::tiletype tt = mc.tiletypeAt(start);
if(DFHack::LowPassable(tt) || juststarted && DFHack::HighPassable(tt))
{
v.push_back(start);
juststarted = false;
start.z++;
}
else break;
}
return v;
};
std::string str() const {
return "column";
}
};
/**
* Flood-fill water tiles from cursor (for wclean)
* example: remove salt flag from a river
*/
class FloodBrush : public Brush
{
public:
FloodBrush(Core *c){c_ = c;};
~FloodBrush(){};
coord_vec points(MapExtras::MapCache & mc, DFHack::DFCoord start)
{
coord_vec v;
std::stack<DFCoord> to_flood;
to_flood.push(start);
std::set<DFCoord> seen;
while (!to_flood.empty()) {
DFCoord xy = to_flood.top();
to_flood.pop();
df::tile_designation des = mc.designationAt(xy);
if (seen.find(xy) == seen.end()
&& des.bits.flow_size
&& des.bits.liquid_type == tile_liquid::Water) {
v.push_back(xy);
seen.insert(xy);
maybeFlood(DFCoord(xy.x - 1, xy.y, xy.z), to_flood, mc);
maybeFlood(DFCoord(xy.x + 1, xy.y, xy.z), to_flood, mc);
maybeFlood(DFCoord(xy.x, xy.y - 1, xy.z), to_flood, mc);
maybeFlood(DFCoord(xy.x, xy.y + 1, xy.z), to_flood, mc);
df::tiletype tt = mc.tiletypeAt(xy);
if (LowPassable(tt))
{
maybeFlood(DFCoord(xy.x, xy.y, xy.z - 1), to_flood, mc);
}
if (HighPassable(tt))
{
maybeFlood(DFCoord(xy.x, xy.y, xy.z + 1), to_flood, mc);
}
}
}
return v;
}
std::string str() const {
return "flood";
}
private:
void maybeFlood(DFCoord c, std::stack<DFCoord> &to_flood, MapExtras::MapCache &mc) {
if (mc.testCoord(c)) {
to_flood.push(c);
}
}
Core *c_;
};
command_result parseRectangle(color_ostream & out,
vector<string> & input, int start, int end,
int & width, int & height, int & zLevels,
bool hasConsole = true)
{
int newWidth = 0, newHeight = 0, newZLevels = 0;
if (end > start + 1)
{
newWidth = atoi(input[start++].c_str());
newHeight = atoi(input[start++].c_str());
if (end > start) {
newZLevels = atoi(input[start++].c_str());
} else {
newZLevels = 1; // So 'range w h' won't ask for it.
}
}
string command = "";
std::stringstream str;
CommandHistory hist;
if (newWidth < 1) {
if (hasConsole) {
Console &con = static_cast<Console&>(out);
str.str("");
str << "Set range width <" << width << "> ";
con.lineedit(str.str(), command, hist);
hist.add(command);
newWidth = command.empty() ? width : atoi(command.c_str());
} else {
return CR_WRONG_USAGE;
}
}
if (newHeight < 1) {
if (hasConsole) {
Console &con = static_cast<Console&>(out);
str.str("");
str << "Set range height <" << height << "> ";
con.lineedit(str.str(), command, hist);
hist.add(command);
newHeight = command.empty() ? height : atoi(command.c_str());
} else {
return CR_WRONG_USAGE;
}
}
if (newZLevels < 1) {
if (hasConsole) {
Console &con = static_cast<Console&>(out);
str.str("");
str << "Set range z-levels <" << zLevels << "> ";
con.lineedit(str.str(), command, hist);
hist.add(command);
newZLevels = command.empty() ? zLevels : atoi(command.c_str());
} else {
return CR_WRONG_USAGE;
}
}
width = newWidth < 1? 1 : newWidth;
height = newHeight < 1? 1 : newHeight;
zLevels = newZLevels < 1? 1 : newZLevels;
return CR_OK;
}
inline std::ostream &operator<<(std::ostream &stream, const Brush& brush)
{
stream << brush.str();
return stream;
}
inline std::ostream &operator<<(std::ostream &stream, const Brush* brush)
{
stream << brush->str();
return stream;
}