forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelayer.cpp
391 lines (343 loc) · 13.6 KB
/
changelayer.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
// changelayer plugin
// allows changing the material type of geological layers
#include "Console.h"
#include "Core.h"
#include "DataDefs.h"
#include "Export.h"
#include "PluginManager.h"
#include "TileTypes.h"
#include "modules/Gui.h"
#include "modules/MapCache.h"
#include "modules/Maps.h"
#include "modules/Materials.h"
// DF data structure definition headers
#include "df/region_map_entry.h"
#include "df/world.h"
#include "df/world_data.h"
#include "df/world_geo_biome.h"
#include "df/world_geo_layer.h"
using namespace DFHack;
using namespace df::enums;
using namespace std;
using std::vector;
using std::string;
DFHACK_PLUGIN("changelayer");
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(cursor);
const string changelayer_help =
" Allows to change the material of whole geology layers.\n"
" Can have impact on all surrounding regions, not only your embark!\n"
" By default changing stone to soil and vice versa is not allowed.\n"
" By default changes only the layer at the cursor position.\n"
" Note that one layer can stretch across lots of z levels.\n"
" By default changes only the geology which is linked to the biome under the\n"
" cursor. That geology might be linked to other biomes as well, though.\n"
" Mineral veins and gem clusters will stay on the map.\n"
" Use 'changevein' for them.\n\n"
" tl;dr: You will end up with changing quite big areas in one go.\n\n"
"Options (first parameter MUST be the material id):\n"
" all_biomes - Change layer for all biomes on your map.\n"
" Result may be undesirable since the same layer\n"
" can AND WILL be on different z-levels for different biomes.\n"
" Use the tool 'probe' to get an idea how layers and biomes\n"
" are distributed on your map.\n"
" all_layers - Change all layers on your map.\n"
" Candy mountain, anyone?\n"
" Will make your map quite boring, but tidy.\n"
" force - Allow changing stone to soil and vice versa.\n"
" !!THIS CAN HAVE WEIRD EFFECTS, USE WITH CARE!!\n"
" Note that soil will not be magically replaced with stone.\n"
" You will, however, get a stone floor after digging so it\n"
" will allow the floor to be engraved.\n"
" Note that stone will not be magically replaced with soil.\n"
" You will, however, get a soil floor after digging so it\n"
" could be helpful for creating farm plots on maps with no soil.\n"
" verbose - Give some more details about what is being changed.\n"
" trouble - Give some advice for known problems.\n"
"Example:\n"
" changelayer GRANITE\n"
" Convert layer at cursor position into granite.\n"
" changelayer SILTY_CLAY force\n"
" Convert layer at cursor position into clay even if it's stone.\n"
" changelayer MARBLE allbiomes alllayers\n"
" Convert all layers of all biomes into marble.\n";
const string changelayer_trouble =
"Known problems with changelayer:\n\n"
" Nothing happens, the material stays the old.\n"
" Pause/unpause the game and/or move the cursor a bit. Then retry.\n"
" Try changing another layer, undo the changes and try again.\n"
" Try saving and loading the game.\n\n"
" Weird stuff happening after using the 'force' option.\n"
" Change former stone layers back to stone, soil back to soil.\n"
" If in doubt, use the 'probe' tool to find tiles with soil walls\n"
" and stone layer type or the other way round.\n";
command_result changelayer (color_ostream &out, std::vector <std::string> & parameters);
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
commands.push_back(PluginCommand(
"changelayer", "Change a whole geology layer.",
changelayer, false, /* true means that the command can't be used from non-interactive user interface */
// Extended help string. Used by CR_WRONG_USAGE and the help command:
changelayer_help.c_str()
));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
bool conversionAllowed(color_ostream &out, MaterialInfo mi, MaterialInfo ml, bool force);
// no need to spam the "stone to soil" warning more than once
// in case multiple biomes and/or layers are going to be changed
static bool warned = false;
command_result changelayer (color_ostream &out, std::vector <std::string> & parameters)
{
CoreSuspender suspend;
string material;
bool force = false;
bool all_biomes = false;
bool all_layers = false;
bool verbose = false;
warned = false;
for(size_t i = 0; i < parameters.size();i++)
{
if(parameters[i] == "help" || parameters[i] == "?")
{
out.print("%s",changelayer_help.c_str());
return CR_OK;
}
if(parameters[i] == "trouble")
{
out.print("%s",changelayer_trouble.c_str());
return CR_OK;
}
if(parameters[i] == "force")
force = true;
if(parameters[i] == "all_biomes")
all_biomes = true;
if(parameters[i] == "all_layers")
all_layers = true;
if(parameters[i] == "verbose")
verbose = true;
}
if (!Maps::IsValid())
{
out.printerr("Map is not available!\n");
return CR_FAILURE;
}
if (parameters.empty())
{
out.printerr("You need to specify a material!\n");
return CR_WRONG_USAGE;
}
material = parameters[0];
MaterialInfo mat_new;
if (!mat_new.findInorganic(material))
{
out.printerr("No such material!\n");
return CR_FAILURE;
}
// check if specified material is stone or gem or soil
if (mat_new.inorganic->material.flags.is_set(material_flags::IS_METAL) ||
mat_new.inorganic->material.flags.is_set(material_flags::NO_STONE_STOCKPILE))
{
out.printerr("Invalid material - you must select a type of stone or gem or soil.\n");
return CR_FAILURE;
}
MapExtras::MapCache mc;
int32_t regionX, regionY, regionZ;
Maps::getPosition(regionX,regionY,regionZ);
int32_t cursorX, cursorY, cursorZ;
Gui::getCursorCoords(cursorX,cursorY,cursorZ);
if(cursorX == -30000)
{
out.printerr("No cursor; place cursor over tile.\n");
return CR_FAILURE;
}
DFCoord cursor (cursorX,cursorY,cursorZ);
MapExtras::Block * b = mc.BlockAt(cursor/16);
if(!b || !b->is_valid())
{
out.printerr("No data.\n");
return CR_OK;
}
df::tile_designation des = b->DesignationAt(cursor%16);
// get biome and geolayer at cursor position
uint32_t biome = des.bits.biome;
uint32_t layer = des.bits.geolayer_index;
if(verbose)
{
out << "biome: " << biome << endl
<< "geolayer: " << layer << endl;
}
// there is no Maps::WriteGeology or whatever, and I didn't want to mess with the library and add it
// so I copied the stuff which reads the geology information and modified it to be able to change it
//
// a more elegant solution would probably look like this:
// 1) modify Maps::ReadGeology to accept and fill one more optional vector
// where the geolayer ids of the 9 biomes are stored
// 2) call ReadGeology here, modify the data in the vectors without having to do all that map stuff
// 3) write Maps::WriteGeology, pass the vectors, let it do it's work
// Step 1) is optional, but it would make implementing 3) easier.
// Otherwise that "check which geo_index is used by biome X" loop would need to be done again.
// no need to touch the same geology more than once
// though it wouldn't matter much since there is not much data to be processed
vector<uint16_t> v_geoprocessed;
v_geoprocessed.clear();
// iterate over 8 surrounding regions + local region
for (int i = eNorthWest; i < eBiomeCount; i++)
{
if(verbose)
out << "---Biome: " << i;
if(!all_biomes && uint32_t(i)!=biome)
{
if(verbose)
out << "-skipping" << endl;
continue;
}
else
{
if(verbose)
out << "-checking" << endl;
}
// check against worldmap boundaries, fix if needed
// regionX is in embark squares
// regionX/16 is in 16x16 embark square regions
// i provides -1 .. +1 offset from the current region
int bioRX = world->map.region_x / 16 + ((i % 3) - 1);
if (bioRX < 0) bioRX = 0;
if (bioRX >= world->world_data->world_width) bioRX = world->world_data->world_width - 1;
int bioRY = world->map.region_y / 16 + ((i / 3) - 1);
if (bioRY < 0) bioRY = 0;
if (bioRY >= world->world_data->world_height) bioRY = world->world_data->world_height - 1;
// get index into geoblock vector
uint16_t geoindex = world->world_data->region_map[bioRX][bioRY].geo_index;
if(verbose)
out << "geoindex: " << geoindex << endl;
bool skip = false;
for(int g=0; size_t(g)<v_geoprocessed.size(); g++)
{
if(v_geoprocessed.at(g)==geoindex)
{
if(verbose)
out << "already processed" << endl;
skip = true;
break;
}
}
if(skip)
continue;
v_geoprocessed.push_back(geoindex);
/// geology blocks have a vector of layer descriptors
// get the vector with pointer to layers
df::world_geo_biome *geo_biome = df::world_geo_biome::find(geoindex);
if (!geo_biome)
{
if(verbose)
out << "no geology found here." << endl;
continue;
}
vector <df::world_geo_layer*> &geolayers = geo_biome->layers;
// complain if layer is out of range
// geology has up to 16 layers currently, but can have less!
if(layer >= geolayers.size() || layer < 0)
{
if(verbose)
out << "layer out of range!";
continue;
}
// now let's actually write the new mat id to the layer(s)
if(all_layers)
{
for (size_t j = 0; j < geolayers.size(); j++)
{
MaterialInfo mat_old;
mat_old.decode(0, geolayers[j]->mat_index);
if(conversionAllowed(out, mat_new, mat_old, force))
{
if(verbose)
out << "changing geolayer " << j
<< " from " << mat_old.getToken()
<< " to " << mat_new.getToken()
<< endl;
geolayers[j]->mat_index = mat_new.index;
}
}
}
else
{
MaterialInfo mat_old;
mat_old.decode(0, geolayers[layer]->mat_index);
if(conversionAllowed(out, mat_new, mat_old, force))
{
if(verbose)
out << "changing geolayer " << layer
<< " from " << mat_old.getToken()
<< " to " << mat_new.getToken()
<< endl;
geolayers[layer]->mat_index = mat_new.index;
}
}
}
out.print("Done.\n");
// Give control back to DF.
return CR_OK;
}
// check if user tries to convert soil <-> stone
// throw some warning if he does
bool conversionAllowed(color_ostream &out, MaterialInfo mat_new, MaterialInfo mat_old, bool force)
{
// check if current layer mat is stone or soil:
// by default don't allow to turn stone to soil and vice versa
bool unsafe = false;
bool allowed = true;
// throw warning if user wants to change soil to stone or vice versa
// while it does work it might be unsafe and can have weird results
// you can't simply convert stone to soil, the rock walls will remain (same the other way round)
// the floor will turn into soil, though, so it might be useful for creating farm plots
// therefore it's not completely forbidden and can be enabled by the 'force' option
if ( mat_new.inorganic->flags.is_set(inorganic_flags::SOIL_ANY)
&& !mat_old.inorganic->flags.is_set(inorganic_flags::SOIL_ANY))
{
if(!warned)
{
out << "Changing a stone layer into soil is probably not wise." << endl
<< "The stone will remain and you get a soil floor after digging." << endl;
}
unsafe = true;
}
else if ( !mat_new.inorganic->flags.is_set(inorganic_flags::SOIL_ANY)
&& mat_old.inorganic->flags.is_set(inorganic_flags::SOIL_ANY))
{
if(!warned)
{
out << "Changing a soil layer into stone is probably not wise." << endl
<< "The soil will remain and you only get a stone floor after digging." << endl;
}
unsafe = true;
}
if(unsafe)
{
if(force)
{
if(!warned)
{
out << "You've been warned, good luck." << endl;
}
allowed = true;
}
else
{
if(!warned)
{
out << "Use the option 'force' if you REALLY want to do that." << endl
<< "Weird things can happen with your map, so save your game before trying!" << endl
<< "Example: 'changelayer GRANITE force'" << endl;
}
allowed = false;
}
// avoid multiple warnings for the same stuff
warned = true;
}
return allowed;
}