-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathconfig.cpp
More file actions
411 lines (334 loc) · 11.8 KB
/
config.cpp
File metadata and controls
411 lines (334 loc) · 11.8 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
#include "config.h"
namespace gen {
namespace config {
unsigned int seed = 0;
double resolution = 0.08;
std::string outfileExt = ".png";
std::string outfile = "output";
double erosionAmount = -1.0;
int erosionIterations = 3;
int numCities = -1;
int numTowns = -1;
int imageWidth = 1920;
int imageHeight = 1080;
double defaultExtentsHeight = 20.0;
double drawScale = 1.0;
bool enableSlopes = true;
bool enableRivers = true;
bool enableContour = true;
bool enableBorders = true;
bool enableCities = true;
bool enableTowns = true;
bool enableLabels = true;
bool enableAreaLabels = true;
bool withSvgOutput = false;
bool enableSvgColors = false;
bool verbose = false;
void print(std::string msg) {
if (gen::config::verbose) {
std::cout << msg << std::endl;
}
}
bool parseOptions(int argc, char **argv) {
OptionArgs opts;
void *argtable[] = {
opts.help = arg_litn("h", "help", 0, 1, "display this help and exit"),
opts.seed = arg_strn("s", "seed", "<uint>", 0, 1, "set random generator seed"),
opts.timeseed = arg_litn(NULL, "timeseed", 0, 1, "set seed from system time"),
opts.resolution = arg_dbln("r", "resolution", "<float>", 0, 1, "level of map detail"),
opts.outfile = arg_filen("o", "output", "filename", 0, 1, "output file"),
opts.output = arg_filen(NULL, NULL, "<file>", 0, 1, "output file"),
opts.eroamount = arg_dbln("e", "erosion-amount", "<float>", 0, 1, "erosion amount"),
opts.erosteps = arg_intn(NULL, "erosion-steps", "<int>", 0, 1, "number of erosion iterations"),
opts.ncities = arg_intn("c", "cities", "<int>", 0, 1, "number of generated cities"),
opts.ntowns = arg_intn("t", "towns", "<int>", 0, 1, "number of generated towns"),
opts.size = arg_strn(NULL, "size", "<widthpx:heightpx>", 0, 1, "set output image size"),
opts.drawscale = arg_dbln(NULL, "draw-scale", "<float>", 0, 1, "set scale of drawn lines/points"),
opts.noslopes = arg_litn(NULL, "no-slopes", 0, 1, "disable slope drawing"),
opts.norivers = arg_litn(NULL, "no-rivers", 0, 1, "disable river drawing"),
opts.nocontour = arg_litn(NULL, "no-contour", 0, 1, "disable contour drawing"),
opts.noborders = arg_litn(NULL, "no-borders", 0, 1, "disable border drawing"),
opts.nocities = arg_litn(NULL, "no-cities", 0, 1, "disable city drawing"),
opts.notowns = arg_litn(NULL, "no-towns", 0, 1, "disable town drawing"),
opts.nolabels = arg_litn(NULL, "no-labels", 0, 1, "disable label drawing"),
opts.noarealabels = arg_litn(NULL, "no-arealabels", 0, 1, "disable area label drawing"),
opts.drawinfo = arg_litn(NULL, "drawing-supported", 0, 1, "display whether drawing is supported and exit"),
opts.withsvg = arg_litn(NULL, "with-svg", 0, 1, "additional output in svg format"),
opts.svgcolors = arg_litn(NULL, "svg-colors", 0, 1, "enable colors in svg output"),
opts.verbose = arg_litn("v", "verbose", 0, 1, "output additional information to stdout"),
opts.end = arg_end(20)
};
if (arg_nullcheck(argtable) != 0) {
std::cout << "error: insufficient memory." << std::endl;
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return false;
}
int nerrors = arg_parse(argc,argv,argtable);
std::string progname("map_generation");
if (opts.help->count > 0) {
std::cout << "Usage: " << progname;
arg_print_syntax(stdout, argtable, "\n");
std::cout << "\nOptions:\n" << std::endl;
arg_print_glossary(stdout, argtable, " %-30s %s\n");
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return false;
}
if (nerrors > 0) {
arg_print_errors(stdout, opts.end, progname.c_str());
std::cout << "Try '" << progname << " --help' for more information." << std::endl;
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return false;
}
if (!_displayInfo(opts)) {
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return false;
}
if (!_setOptions(opts)) {
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return false;
}
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return true;
}
bool _displayInfo(OptionArgs opts) {
bool isInfoDisplayed = _displayDrawSupportInfo(opts.drawinfo);
return !isInfoDisplayed;
}
bool _displayDrawSupportInfo(arg_lit *drawinfo) {
if (drawinfo->count == 0) {
return false;
}
#ifdef PYTHON_RENDERING_SUPPORTED
std::string result = "True";
#else
std::string result = "False";
#endif
std::cout << "--drawing-supported=" << result << std::endl;
return true;
}
bool _setOptions(OptionArgs opts) {
if (!_setSeed(opts.timeseed, opts.seed)) { return false; }
if (!_setResolution(opts.resolution)) { return false; }
if (!_setOutputFile(opts.outfile, opts.output)) { return false; }
if (!_setErosionAmount(opts.eroamount)) { return false; }
if (!_setErosionIterations(opts.erosteps)) { return false; }
if (!_setNumCities(opts.ncities)) { return false; }
if (!_setNumTowns(opts.ntowns)) { return false; }
if (!_setImageSize(opts.size)) { return false; }
if (!_setDrawScale(opts.drawscale)) { return false; }
if (!_disableSlopes(opts.noslopes)) { return false; }
if (!_disableRivers(opts.norivers)) { return false; }
if (!_disableContour(opts.nocontour)) { return false; }
if (!_disableBorders(opts.noborders)) { return false; }
if (!_disableCities(opts.nocities)) { return false; }
if (!_disableTowns(opts.notowns)) { return false; }
if (!_disableLabels(opts.nolabels)) { return false; }
if (!_disableAreaLabels(opts.noarealabels)) { return false; }
if (!_enableWithSvg(opts.withsvg)) { return false; }
if (!_enableSvgColors(opts.svgcolors)) { return false; }
if (!_setVerbosity(opts.verbose)) { return false; }
return true;
}
bool _setSeed(arg_lit *timeseed, arg_str *seed) {
if (timeseed->count > 0) {
gen::config::seed = (unsigned int)time(NULL);
} else if (seed->count > 0) {
std::istringstream istr(seed->sval[0]);
istr >> gen::config::seed;
}
return true;
}
bool _setResolution(arg_dbl *res) {
if (res->count == 0) {
return true;
}
double r = res->dval[0];
if (r <= 0) {
std::cout << "error: resolution must be greater than zero." << std::endl;
std::cout << "resolution: " << r << std::endl;
return false;
}
gen::config::resolution = r;
return true;
}
bool _setOutputFile(arg_file *outfile1, arg_file *outfile2) {
if (outfile1->count > 0) {
gen::config::outfile = outfile1->filename[0];
gen::config::outfileExt = outfile1->extension[0];
} else if (outfile2->count > 0) {
gen::config::outfile = outfile2->filename[0];
gen::config::outfileExt = outfile2->extension[0];
}
return true;
}
bool _setErosionAmount(arg_dbl *amount) {
if (amount->count == 0) {
return true;
}
double e = amount->dval[0];
if (e <= 0) {
std::cout << "error: erosion amount must be greater than zero." << std::endl;
std::cout << "erosion amount: " << e << std::endl;
return false;
}
gen::config::erosionAmount = e;
return true;
}
bool _setErosionIterations(arg_int *iterations) {
if (iterations->count == 0) {
return true;
}
int n = iterations->ival[0];
if (n < 0) {
std::cout << "error: erosion iterations must be greater than or equal to zero." << std::endl;
std::cout << "erosion iterations: " << n << std::endl;
return false;
}
gen::config::erosionIterations = n;
return true;
}
bool _setNumCities(arg_int *ncities) {
if (ncities->count == 0) {
return true;
}
int n = ncities->ival[0];
if (n < 0) {
std::cout << "error: number of cities must be greater than or equal to zero." << std::endl;
std::cout << "number of cities: " << n << std::endl;
return false;
}
gen::config::numCities = n;
return true;
}
bool _setNumTowns(arg_int *ntowns) {
if (ntowns->count == 0) {
return true;
}
int n = ntowns->ival[0];
if (n < 0) {
std::cout << "error: number of towns must be greater than or equal to zero." << std::endl;
std::cout << "number of towns: " << n << std::endl;
return false;
}
gen::config::numTowns = n;
return true;
}
bool _setImageSize(arg_str *size) {
if (size->count == 0) {
return true;
}
std::string sizestr(size->sval[0]);
std::string delimiter = ":";
size_t dpos = sizestr.find(delimiter);
std::string invalidmsg("error: image size must be in form <widthpx:heighpx>.");
if (dpos == std::string::npos) {
std::cout << invalidmsg << std::endl;
return false;
}
std::string widthstr = sizestr.substr(0, dpos);
std::string heightstr = sizestr.substr(dpos + 1, sizestr.size());
if (widthstr.size() == 0 || heightstr.size() == 0) {
std::cout << invalidmsg << std::endl;
return false;
}
int widthpx;
std::istringstream istreamWidth(widthstr);
istreamWidth >> widthpx;
if (istreamWidth.fail() || widthpx <= 0) {
std::cout << "error: invalid image width value." << std::endl;
std::cout << "width value: " << widthstr << std::endl;
return false;
}
int heightpx;
std::istringstream istreamHeight(heightstr);
istreamHeight >> heightpx;
if (istreamHeight.fail() || heightpx <= 0) {
std::cout << "error: invalid image height value." << std::endl;
std::cout << "height value: " << heightstr << std::endl;
return false;
}
gen::config::imageWidth = widthpx;
gen::config::imageHeight = heightpx;
return true;
}
bool _setDrawScale(arg_dbl *drawscale) {
if (drawscale->count == 0) {
return true;
}
double s = drawscale->dval[0];
if (s <= 0) {
std::cout << "error: draw scale must be greater than zero." << std::endl;
std::cout << "draw size: " << s << std::endl;
return false;
}
gen::config::drawScale = s;
return true;
}
bool _disableSlopes(arg_lit *noslopes) {
if (noslopes->count > 0) {
gen::config::enableSlopes = false;
}
return true;
}
bool _disableRivers(arg_lit *norivers) {
if (norivers->count > 0) {
gen::config::enableRivers = false;
}
return true;
}
bool _disableContour(arg_lit *nocontour) {
if (nocontour->count > 0) {
gen::config::enableContour = false;
}
return true;
}
bool _disableBorders(arg_lit *noborders) {
if (noborders->count > 0) {
gen::config::enableBorders = false;
}
return true;
}
bool _disableCities(arg_lit *nocities) {
if (nocities->count > 0) {
gen::config::enableCities = false;
}
return true;
}
bool _disableTowns(arg_lit *notowns) {
if (notowns->count > 0) {
gen::config::enableTowns = false;
}
return true;
}
bool _disableLabels(arg_lit *nolabels) {
if (nolabels->count > 0) {
gen::config::enableLabels = false;
}
return true;
}
bool _disableAreaLabels(arg_lit *noarealabels) {
if (noarealabels->count > 0) {
gen::config::enableAreaLabels = false;
}
return true;
}
bool _enableWithSvg(arg_lit *withsvg) {
if (withsvg->count > 0) {
gen::config::withSvgOutput = true;
}
return true;
}
bool _enableSvgColors(arg_lit *svgcolors) {
if (svgcolors->count > 0) {
gen::config::enableSvgColors = true;
}
return true;
}
bool _setVerbosity(arg_lit *verbose) {
if (verbose->count > 0) {
gen::config::verbose = true;
}
return true;
}
}
}