Skip to content
Navigation Menu
{{ message }}
forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-map.cpp
More file actions
400 lines (334 loc) · 14.5 KB
/
Copy pathexport-map.cpp
File metadata and controls
400 lines (334 loc) · 14.5 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
#include "Debug.h"
#include "Error.h"
#include "PluginManager.h"
#include "MiscUtils.h"
#include "modules/Maps.h"
#include "modules/Translation.h"
#include "df/entity_raw.h"
#include "df/creature_raw.h"
#include "df/historical_entity.h"
#include "df/world.h"
#include "df/map_block.h"
#include "df/world_data.h"
#include "df/world_site.h"
#include "df/region_map_entry.h"
#include "df/world_region.h"
#include "df/world_landmass.h"
#include "df/world_region_details.h"
#include "gdal/ogrsf_frmts.h"
#include <string>
#include <vector>
#include <chrono>
using std::string;
using std::vector;
using namespace DFHack;
DFHACK_PLUGIN("export-map");
REQUIRE_GLOBAL(world);
namespace DFHack {
DBG_DECLARE(exportmap, log);
}
static command_result do_command(color_ostream &out, vector<string> ¶meters);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
DEBUG(log,out).print("initializing %s\n", plugin_name);
commands.push_back(PluginCommand(
plugin_name,
"Export the world map.",
do_command));
return CR_OK;
}
auto setGeometry(OGRFeature *feature, double x, double y, double dimx, double dimy = 0) {
if (dimy == 0) { dimy = dimx; }
auto poly = new OGRPolygon();
auto boundary = new OGRLinearRing();
y = -y; // in GIS negative y-coordinates mean further south
boundary->addPoint(x,y);
boundary->addPoint(x,y-dimy);
boundary->addPoint(x+dimx,y-dimy);
boundary->addPoint(x+dimx,y);
boundary->closeRings();
//the "Directly" variants assume ownership of the objects created above
poly->addRingDirectly(boundary);
feature->SetGeometryDirectly( poly );
}
auto get_world_index(int world_x, int world_y, int8_t dir) {
switch (dir) {
case 1: world_x-- ; world_y++; break;
case 2: ; world_y++; break;
case 3: world_x++ ; world_y++; break;
case 4: world_x-- ; ; break;
// case 5 induces no change
case 6: world_x++ ; ; break;
case 7: world_x-- ; world_y--; break;
case 8: ; world_y--; break;
case 9: world_x++ ; world_y--; break;
}
world_x = std::min(std::max(0,world_x),world->world_data->world_width - 1);
world_y = std::min(std::max(0,world_y),world->world_data->world_height - 1);
return std::pair(world_x,world_y);
}
auto create_field(OGRLayer *layer, std::string name, OGRFieldType type, int width = 0, OGRFieldSubType subtype = OFSTNone) {
OGRFieldDefn field( name.c_str() , type );
if (subtype != OFSTNone) {
field.SetSubType(subtype);
}
if (width != 0) {
field.SetWidth(width);
}
// this should create a copy internally
if( layer->CreateField( &field ) != OGRERR_NONE ){
throw CR_FAILURE;
}
}
// PROJ.4 description of EPSG:3857 (https://epsg.io/3857)
static const char* EPSG_3857 = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs";
const char* describe_surroundings(int savagery, int evilness) {
constexpr std::array<const char*,9>surroundings{
"Serene", "Mirthful", "Joyous Wilds",
"Calm", "Wilderness", "Untamed Wilds",
"Sinister", "Haunted", "Terrifying"
};
auto savagery_index = savagery < 33 ? 0 : (savagery > 65 ? 2 : 1);
auto evilness_index = evilness < 33 ? 0 : (evilness > 65 ? 2 : 1);
return surroundings[3 * evilness_index + savagery_index];
}
static int wdim = 768; // dimension of a world tile
static int rdim = 48; // dimension of a region tile
static command_result export_region_tiles(color_ostream &out);
static command_result export_sites(color_ostream &out);
static command_result do_command(color_ostream &out, vector<string> ¶meters)
{
CoreSuspender suspend;
if (!Core::getInstance().isWorldLoaded()){
out.printerr("This command requires a world to be loaded\n");
return CR_WRONG_USAGE;
}
if (parameters.size() && parameters[0] == "sites")
{
return export_sites(out);
}
else
{
return export_region_tiles(out);
}
}
static command_result export_sites(color_ostream &out)
{
out.print("exporting sites... ");
out.flush();
const auto start{std::chrono::steady_clock::now()};
// set up coordinate system
OGRSpatialReference CRS;
if (CRS.importFromProj4(EPSG_3857) != OGRERR_NONE) {
out.printerr("could not set up coordinate system");
return CR_FAILURE;
}
// set up output driver
GDALAllRegister();
const char *driver_name = "SQLite";
const char *extension = "sqlite";
auto driver = GetGDALDriverManager()->GetDriverByName(driver_name);
if (!driver) {
out.printerr("could not find sqlite driver");
return CR_FAILURE;
}
// create a dataset and associate it to a file
std::string sites("sites.");
sites.append(extension);
const char* options[] = { "SPATIALITE=YES", nullptr };
auto dataset = driver->Create( sites.c_str(), 0, 0, 0, GDT_Unknown, options);
if (!dataset) {
out.printerr("could not create dataset");
return CR_FAILURE;
}
// create a layer for the biome data
// const char* format[] = { "FORMAT=WKT", nullptr };
auto layer = dataset->CreateLayer( "world_sites", &CRS, wkbPolygon, nullptr );
if (!layer) {
out.printerr("could not create layer");
return CR_FAILURE;
}
try {
create_field(layer, "site_id", OFTInteger);
create_field(layer, "civ_id", OFTInteger);
create_field(layer, "created_year", OFTInteger);
create_field(layer, "cur_owner_id", OFTInteger);
create_field(layer, "type", OFTString, 15);
create_field(layer, "site_name_df", OFTString, 100);
create_field(layer, "site_name_en", OFTString, 100);
create_field(layer, "civ_name_df", OFTString, 100);
create_field(layer, "civ_name_en", OFTString, 100);
create_field(layer, "site_government_df", OFTString, 100);
create_field(layer, "site_government_en", OFTString, 100);
create_field(layer, "owner_race", OFTString, 15);
// create_field(layer, "local_ruler", OFTString, 100);
}
catch (const DFHack::command_result& r) {
out.printerr("could not create fields for output layer");
return r;
}
if (dataset->StartTransaction() != OGRERR_NONE) {
out.printerr("could not start a transaction\n");
}
for (auto const site : world->world_data->sites)
{
auto feature = OGRFeature::CreateFeature( layer->GetLayerDefn() );
setGeometry(
feature,
site->global_min_x * rdim,
site->global_min_y * rdim,
(site->global_max_x - site->global_min_x + 1) * rdim,
(site->global_max_y - site->global_min_y + 1) * rdim
);
feature->SetField( "site_id", site->id );
feature->SetField( "type", ENUM_KEY_STR(world_site_type, site->type).c_str() );
#define SET_FIELD(name) feature->SetField( #name, site->name)
SET_FIELD(civ_id);
SET_FIELD(created_year);
SET_FIELD(cur_owner_id);
#undef SET_FIELD
#define TRANSLATE_NAME(field_name, name_object)\
feature->SetField((#field_name"_df"), DF2UTF(Translation::translateName(&name_object, false)).c_str());\
feature->SetField((#field_name"_en"), DF2UTF(Translation::translateName(&name_object, true)).c_str());
TRANSLATE_NAME(site_name, site->name)
auto civ = df::historical_entity::find(site->civ_id);
if (civ) { TRANSLATE_NAME(civ_name,civ->name) }
auto owner = df::historical_entity::find(site->cur_owner_id);
if (owner) {
TRANSLATE_NAME(site_government,owner->name)
auto race = df::creature_raw::find(owner->race);
if (!race){
race = df::creature_raw::find(civ->race);
}
if (race) {
feature->SetField( "owner_race", race->name[2].c_str() );
}
}
// this updates the feature with the id it receives in the layer
if( layer->CreateFeature( feature ) != OGRERR_NONE )
return CR_FAILURE;
// but we don't care and destroy the feature
OGRFeature::DestroyFeature( feature );
}
dataset->CommitTransaction();
GDALClose( dataset );
const auto finish{std::chrono::steady_clock::now()};
const std::chrono::duration<double> elapsed_seconds{finish - start};
out.print("done in %f ms !\n", elapsed_seconds.count());
return CR_OK;
}
static command_result export_region_tiles(color_ostream &out)
{
out.print("%lu / %d region map tiles loaded\n",
world->world_data->midmap_data.region_details.size(),
world->world_data->world_width * world->world_data->world_height
);
out.print("exporting map... ");
out.flush();
const auto start{std::chrono::steady_clock::now()};
// set up coordinate system
OGRSpatialReference CRS;
if (CRS.importFromProj4(EPSG_3857) != OGRERR_NONE) {
out.printerr("could not set up coordinate system");
return CR_FAILURE;
}
// set up output driver
GDALAllRegister();
const char *driver_name = "Parquet";
const char *extension = "parquet";
auto driver = GetGDALDriverManager()->GetDriverByName(driver_name);
CHECK_NULL_POINTER(driver);
// create a data set and associate it to a file
std::string map("map.");
map.append(extension);
auto dataset = driver->Create( map.c_str(), 0, 0, 0, GDT_Unknown, NULL );
// create a layer for the biome data
auto layer = dataset->CreateLayer( "world_biomes", &CRS, wkbPolygon, NULL );
try {
create_field(layer, "region_id", OFTInteger);
create_field(layer, "region_name_en", OFTString, 100);
create_field(layer, "region_name_df", OFTString, 100);
create_field(layer, "landmass_id", OFTInteger);
create_field(layer, "landmass_name_en", OFTString, 100);
create_field(layer, "landmass_name_df", OFTString, 100);
create_field(layer, "biome_type", OFTString, 32);
create_field(layer, "surroundings", OFTString, 16);
create_field(layer, "elevation", OFTInteger);
create_field(layer, "evilness", OFTInteger);
create_field(layer, "savagery", OFTInteger);
create_field(layer, "volcanism", OFTInteger);
create_field(layer, "drainage", OFTInteger);
create_field(layer, "temperature", OFTInteger);
create_field(layer, "vegetation", OFTInteger);
create_field(layer, "rainfall", OFTInteger);
create_field(layer, "snowfall", OFTInteger);
create_field(layer, "salinity", OFTInteger);
create_field(layer, "reanimating", OFTInteger, 0, OFSTBoolean);
create_field(layer, "has_bogeymen", OFTInteger, 0, OFSTBoolean);
} catch (const DFHack::command_result& r) {
out.printerr("could not create fields for output layer");
return r;
}
// iterating over the region details allows the user to do partial map exports
// by manually scrolling on the embark site selection
for (auto const region_details : world->world_data->midmap_data.region_details) {
auto world_x = region_details->pos.x;
auto world_y = region_details->pos.y;
for (int region_x = 0; region_x < 16; ++region_x) {
for (int region_y = 0; region_y < 16; ++region_y) {
auto feature = OGRFeature::CreateFeature( layer->GetLayerDefn() );
setGeometry(
feature,
(double)(world_x * wdim + region_x * rdim),
(double)(world_y * wdim + region_y * rdim),
rdim
);
// get information from the region details
auto [biome_x,biome_y] = get_world_index(world_x, world_y, region_details->biome[region_x][region_y]);
feature->SetField( "biome_type", ENUM_KEY_STR(biome_type,Maps::getBiomeType(biome_x,biome_y)).c_str() );
feature->SetField( "elevation", region_details->elevation[region_x][region_y]);
// gets supplementary information from the world tile
auto& region_map_entry = world->world_data->region_map[biome_x][biome_y];
#define SET_FIELD(name) feature->SetField( #name, region_map_entry.name)
SET_FIELD(region_id);
SET_FIELD(landmass_id);
SET_FIELD(evilness);
SET_FIELD(savagery);
SET_FIELD(volcanism);
SET_FIELD(drainage);
SET_FIELD(temperature);
SET_FIELD(vegetation);
SET_FIELD(rainfall);
SET_FIELD(snowfall);
SET_FIELD(salinity);
#undef SET_FIELD
feature->SetField( "surroundings", describe_surroundings(region_map_entry.savagery, region_map_entry.evilness));
auto region = df::world_region::find(region_map_entry.region_id);
if (region) {
auto region_name_en = DF2UTF(Translation::translateName(®ion->name, true));
feature->SetField( "region_name_en", region_name_en.c_str());
auto region_name_df = DF2UTF(Translation::translateName(®ion->name, false));
feature->SetField( "region_name_df", region_name_df.c_str());
feature->SetField("reanimating", region->reanimating);
feature->SetField("has_bogeymen", region->has_bogeymen);
}
auto landmass = df::world_landmass::find(region_map_entry.landmass_id);
if (landmass) {
auto landmass_name_en = DF2UTF(Translation::translateName(&landmass->name, true));
feature->SetField( "landmass_name_en", landmass_name_en.c_str());
auto landmass_name_df = DF2UTF(Translation::translateName(&landmass->name, false));
feature->SetField( "landmass_name_df", landmass_name_df.c_str());
}
// this updates the feature with the id it receives in the layer
if( layer->CreateFeature( feature ) != OGRERR_NONE )
return CR_FAILURE;
// but we don't care and destroy the feature
OGRFeature::DestroyFeature( feature );
}
}
}
GDALClose( dataset );
const auto finish{std::chrono::steady_clock::now()};
const std::chrono::duration<double> elapsed_seconds{finish - start};
out.print("done in %f ms !\n", elapsed_seconds.count());
return CR_OK;
}
You can’t perform that action at this time.
