forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiletypes.cpp
More file actions
635 lines (568 loc) · 17 KB
/
Copy pathtiletypes.cpp
File metadata and controls
635 lines (568 loc) · 17 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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <sstream>
using std::vector;
using std::string;
using std::endl;
using std::set;
#include <dfhack/Core.h>
#include <dfhack/Console.h>
#include <dfhack/Export.h>
#include <dfhack/PluginManager.h>
#include <dfhack/modules/Vegetation.h>
#include <dfhack/modules/Maps.h>
#include <dfhack/modules/Gui.h>
#include <dfhack/TileTypes.h>
#include <dfhack/extra/MapExtras.h>
using namespace MapExtras;
using namespace DFHack;
//zilpin: These two functions were giving me compile errors in VS2008, so I cheated with the C style loop below, just to get it to build.
//Original code is commented out.
void tolower(std::string &str)
{
//The C++ way...
//std::transform(str.begin(), str.end(), str.begin(), std::bind2nd(std::ptr_fun(&std::tolower<char> ), std::locale("")));
//The C way...
for(char *c=(char *)str.c_str(); *c; ++c)
{
*c = tolower(*c);
}
}
void toupper(std::string &str)
{
//std::transform(str.begin(), str.end(), str.begin(), std::bind2nd(std::ptr_fun(&std::toupper<char>), std::locale("")));
for(char *c=(char *)str.c_str(); *c; ++c)
{
*c = toupper(*c);
}
}
int toint(const std::string &str, int failValue = 0)
{
std::istringstream ss(str);
int valInt;
ss >> valInt;
if (ss.fail())
{
return failValue;
}
return valInt;
}
struct TileType
{
DFHack::TileShape shape;
DFHack::TileMaterial material;
DFHack::TileSpecial special;
TileType()
{
shape = DFHack::tileshape_invalid;
material = DFHack::tilematerial_invalid;
special = DFHack::tilespecial_invalid;
}
bool empty()
{
return shape == -1 && material == -1 && special == -1;
}
};
std::ostream &operator<<(std::ostream &stream, const TileType &paint)
{
bool used = false;
bool needSpace = false;
if (paint.special >= 0)
{
stream << DFHack::TileSpecialString[paint.special];
used = true;
needSpace = true;
}
if (paint.material >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << DFHack::TileMaterialString[paint.material];
used = true;
needSpace = true;
}
if (paint.shape >= 0)
{
if (needSpace)
{
stream << " ";
needSpace = false;
}
stream << DFHack::TileShapeString[paint.shape];
used = true;
}
if (!used)
{
stream << "any";
}
return stream;
}
bool processTileType(TileType &paint, const std::string &option, const std::string &value)
{
std::string val = value;
toupper(val);
int valInt;
if (val == "ANY")
{
valInt = -1;
}
else
{
valInt = toint(value, -2);
}
bool found = false;
if (option == "shape" || option == "sh" || option == "s")
{
if (valInt >= -1 && valInt < DFHack::tileshape_count)
{
paint.shape = (DFHack::TileShape) valInt;
found = true;
}
else
{
for (int i = 0; i < DFHack::tileshape_count; i++)
{
if (val == DFHack::TileShapeString[i])
{
paint.shape = (DFHack::TileShape) i;
found = true;
break;
}
}
if (!found)
{
std::cout << "Unknown tile shape: " << value << std::endl;
}
}
}
else if (option == "material" || option == "mat" || option == "m")
{
if (valInt >= -1 && valInt < DFHack::tilematerial_count)
{
paint.material = (DFHack::TileMaterial) valInt;
found = true;
}
else
{
for (int i = 0; i < DFHack::tilematerial_count; i++)
{
if (val == DFHack::TileMaterialString[i])
{
paint.material = (DFHack::TileMaterial) i;
found = true;
break;
}
}
if (!found)
{
std::cout << "Unknown tile material: " << value << std::endl;
}
}
}
else if (option == "special" || option == "sp")
{
if (valInt >= -1 && valInt < DFHack::tilespecial_count)
{
paint.special = (DFHack::TileSpecial) valInt;
found = true;
}
else
{
for (int i = 0; i < DFHack::tilespecial_count; i++)
{
if (val == DFHack::TileSpecialString[i])
{
paint.special = (DFHack::TileSpecial) i;
found = true;
break;
}
}
if (!found)
{
std::cout << "Unknown tile special: " << value << std::endl;
}
}
}
else
{
std::cout << "Unknown option: '" << option << "'" << std::endl;
}
return found;
}
void help( std::ostream & out, const std::string &option)
{
if (option.empty())
{
out << "Commands:" << std::endl
<< " quit / q : quit" << std::endl
<< " filter / f [options] : change filter options" << std::endl
<< " paint / p [options] : change paint options" << std::endl
<< " point / p : set point brush" << std::endl
<< " range / r : set range brush" << std::endl
<< " block : set block brush" << std::endl
<< " column : set column brush" << std::endl
<< std::endl
<< "Filter/paint options:" << std::endl
<< " Shape / sh / s: set tile shape information" << std::endl
<< " Material / mat / m: set tile material information" << std::endl
<< " Special / s: set special tile information" << std::endl
<< "See help [option] for more information" << std::endl;
}
else if (option == "shape" || option == "s" ||option == "sh")
{
out << "Available shapes:" << std::endl
<< " ANY" << std::endl;
for (int i = 0; i < DFHack::tileshape_count; i++)
{
out << " " << DFHack::TileShapeString[i] << std::endl;
}
}
else if (option == "material"|| option == "mat" ||option == "m")
{
out << "Available materials:" << std::endl
<< " ANY" << std::endl;
for (int i = 0; i < DFHack::tilematerial_count; i++)
{
out << " " << DFHack::TileMaterialString[i] << std::endl;
}
}
else if (option == "special")
{
out << "Available specials:" << std::endl
<< " ANY" << std::endl;
for (int i = 0; i < DFHack::tilespecial_count; i++)
{
out << " " << DFHack::TileSpecialString[i] << std::endl;
}
}
}
typedef std::vector<DFHack::DFCoord> coord_vec;
class Brush
{
public:
virtual ~Brush() {};
virtual coord_vec points(MapExtras::MapCache &mc, DFHack::DFCoord start) = 0;
};
/**
* generic 3D rectangle brush. you can specify the dimensions of
* the rectangle and optionally which tile is its 'center'
*/
class RectangleBrush : public Brush
{
int x_, y_, z_;
int cx_, cy_, cz_;
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(){};
};
/**
* 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;
for (int xi = 0; xi < 16; xi++)
{
for (int yi = 0; yi < 16; yi++)
{
v.push_back(iterc);
iterc.y++;
}
iterc.x++;
}
return v;
};
};
/**
* 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))
{
uint16_t tt = mc.tiletypeAt(start);
if(DFHack::LowPassable(tt) || juststarted && DFHack::HighPassable(tt))
{
v.push_back(start);
juststarted = false;
start.z++;
}
else break;
}
return v;
};
};
CommandHistory tiletypes_hist;
DFhackCExport command_result df_tiletypes (Core * c, vector <string> & parameters);
DFhackCExport const char * plugin_name ( void )
{
return "tiletypes";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
tiletypes_hist.load("tiletypes.history");
commands.clear();
commands.push_back(PluginCommand("tiletypes", "Paint map tiles freely, similar to liquids.", df_tiletypes, true));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
tiletypes_hist.save("tiletypes.history");
return CR_OK;
}
DFhackCExport command_result df_tiletypes (Core * c, vector <string> & parameters)
{
uint32_t x_max = 0, y_max = 0, z_max = 0;
int32_t x = 0, y = 0, z = 0;
DFHack::Maps *maps;
DFHack::Gui *gui;
for(int i = 0; i < parameters.size();i++)
{
if(parameters[i] == "help" || parameters[i] == "?")
{
c->con.print("This tool allows painting tiles types with a brush, using an optional filter.\n"
"The tool is interactive, similarly to the liquids tool.\n"
"Further help is available inside.\n"
);
return CR_OK;
}
}
TileType filter, paint;
Brush *brush = new RectangleBrush(1,1);
bool end = false;
std::string brushname = "point";
int width = 1, height = 1, z_levels = 1;
c->con << "Welcome to the tiletype tool.\nType 'help' or '?' for a list of available commands, 'q' to quit.\nPress return after a command to confirm." << std::endl;
c->con.printerr("THIS TOOL CAN BE DANGEROUS. YOU'VE BEEN WARNED.\n");
while (!end)
{
c->con << "Filter: " << filter << std::endl
<< "Paint: " << paint << std::endl
<< "Brush: " << brushname << std::endl;
std::string input = "";
std::string command = "";
std::string option = "";
std::string value = "";
c->con.lineedit("tiletypes> ",input,tiletypes_hist);
tiletypes_hist.add(input);
std::istringstream ss(input);
ss >> command >> option >> value;
tolower(command);
tolower(option);
if (command == "help" || command == "?")
{
help(c->con,option);
}
else if (command == "quit" || command == "q")
{
end = true;
}
else if (command == "filter" || command == "f")
{
processTileType(filter, option, value);
}
else if (command == "paint" || (command == "p" && !option.empty()))
{
processTileType(paint, option, value);
}
else if (command == "point" || command == "p")
{
delete brush;
brushname = "point";
brush = new RectangleBrush(1,1);
}
else if (command == "range" || command == "r")
{
std::stringstream ss;
CommandHistory hist;
ss << "Set range width <" << width << "> ";
c->con.lineedit(ss.str(),command,hist);
width = command == "" ? width : toint(command);
if (width < 1) width = 1;
ss.str("");
ss << "Set range height <" << height << "> ";
c->con.lineedit(ss.str(),command,hist);
height = command == "" ? height : toint(command);
if (height < 1) height = 1;
ss.str("");
ss << "Set range z-levels <" << z_levels << "> ";
c->con.lineedit(ss.str(),command,hist);
z_levels = command == "" ? z_levels : toint(command);
if (z_levels < 1) z_levels = 1;
delete brush;
if (width == 1 && height == 1 && z_levels == 1)
{
brushname = "point";
}
else
{
brushname = "range";
}
brush = new RectangleBrush(width, height, z_levels, 0, 0, 0);
}
else if (command == "block")
{
delete brush;
brushname = "block";
brush = new BlockBrush();
}
else if (command == "column")
{
delete brush;
brushname = "column";
brush = new ColumnBrush();
}
else if (command.empty())
{
if (paint.empty())
{
c->con.printerr("Set the paint first.\n");
continue;
}
c->Suspend();
maps = c->getMaps();
gui = c->getGui();
if (!maps->Start())
{
c->con.printerr("Cannot get map info!\n");
c->Resume();
return CR_FAILURE;
}
maps->getSize(x_max, y_max, z_max);
if (!(gui->Start() && gui->getCursorCoords(x,y,z)))
{
c->con.printerr("Can't get cursor coords! Make sure you have a cursor active in DF.\n");
c->Resume();
return CR_FAILURE;
}
c->con.print("Cursor coords: (%d, %d, %d)\n",x,y,z);
DFHack::DFCoord cursor(x,y,z);
MapExtras::MapCache map(maps);
coord_vec all_tiles = brush->points(map, cursor);
c->con.print("working...\n");
for (coord_vec::iterator iter = all_tiles.begin(); iter != all_tiles.end(); ++iter)
{
const DFHack::TileRow *source = DFHack::getTileRow(map.tiletypeAt(*iter));
if ((filter.shape > -1 && filter.shape != source->shape)
|| (filter.material > -1 && filter.material != source->material)
|| (filter.special > -1 && filter.special != source->special))
{
continue;
}
DFHack::TileShape shape = paint.shape;
if (shape < 0)
{
shape = source->shape;
}
DFHack::TileMaterial material = paint.material;
if (material < 0)
{
material = source->material;
}
DFHack::TileSpecial special = paint.special;
if (special < 0)
{
special = source->special;
}
int32_t type = DFHack::findTileType(shape, material, source->variant, special, source->direction);
// make sure it's not invalid
if(type != -1)
map.setTiletypeAt(*iter, type);
// Remove liquid from walls, etc
if (!DFHack::FlowPassable(shape))
{
DFHack::t_designation des = map.designationAt(*iter);
des.bits.flow_size = 0;
map.setDesignationAt(*iter, des);
}
}
if (map.WriteAll())
{
c->con.print("OK\n");
}
else
{
c->con.printerr("Something failed horribly! RUN!\n");
}
maps->Finish();
c->Resume();
}
}
return CR_OK;
}