forked from Whales/Cataclysm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldmap_generate.cpp
600 lines (574 loc) · 20.6 KB
/
worldmap_generate.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
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
#include "worldmap.h"
#include "biome.h"
#include "rng.h"
#include "globals.h"
#include "window.h"
#include <math.h>
void Worldmap::generate()
{
// Points_live is used below to track which points to update
std::vector<Point> points_live;
// Used below when deciding when to turn lakes into ocean
Lake_status lake[WORLDMAP_SIZE][WORLDMAP_SIZE];
City_status city[WORLDMAP_SIZE][WORLDMAP_SIZE];
std::vector<Point> lake_seeds;
std::vector<Point> city_seeds;
for (int x = 0; x < WORLDMAP_SIZE; x++) {
for (int y = 0; y < WORLDMAP_SIZE; y++) {
biomes[x][y] = NULL;
lake[x][y] = LAKE_NOTLAKE;
city[x][y] = CITY_NOTCITY;
}
}
// Randomly seed biomes
for (std::list<Biome*>::iterator it = BIOMES.instances.begin();
it != BIOMES.instances.end();
it++) {
for (int n = 0; n < WORLDMAP_SIZE / 10; n++) {
Point p( rng(0, WORLDMAP_SIZE - 1), rng(0, WORLDMAP_SIZE - 1) );
points_live.push_back(p);
if ((*it)->has_flag(BIOME_FLAG_LAKE)) {
lake_seeds.push_back(p);
lake[p.x][p.y] = LAKE_UNCHECKED;
}
if ((*it)->has_flag(BIOME_FLAG_CITY)) {
city_seeds.push_back(p);
city[p.x][p.y] = CITY_HUB;
}
biomes[p.x][p.y] = (*it);
}
}
while (!points_live.empty()) {
std::vector<Point> new_points;
//std::vector<Point> points_copy = points_live;
int i = rng(0, points_live.size() - 1);
std::vector<Point> valid_growth;
Point p = points_live[i];
if (p.x > 0 && biomes[p.x - 1][p.y] == NULL) {
valid_growth.push_back( Point(p.x - 1, p.y) );
}
if (p.y > 0 && biomes[p.x][p.y - 1] == NULL) {
valid_growth.push_back( Point(p.x, p.y - 1) );
}
if (p.x < WORLDMAP_SIZE - 1 && biomes[p.x + 1][p.y] == NULL) {
valid_growth.push_back( Point(p.x + 1, p.y) );
}
if (p.y < WORLDMAP_SIZE - 1 && biomes[p.x][p.y + 1] == NULL) {
valid_growth.push_back( Point(p.x, p.y + 1) );
}
if (valid_growth.empty()) { // No valid points - this point is dead!
points_live.erase(points_live.begin() + i);
i--;
} else {
Point growth = valid_growth[rng(0, valid_growth.size() - 1)];
biomes[growth.x][growth.y] = biomes[p.x][p.y];
lake[growth.x][growth.y] = lake[p.x][p.y];
if (city[p.x][p.y] == CITY_HUB) {
city[growth.x][growth.y] = CITY_RAW;
} else {
city[growth.x][growth.y] = city[p.x][p.y];
}
points_live.push_back( growth );
}
}
// Now look at the biomes and randomly select a terrain for each
for (int x = 0; x < WORLDMAP_SIZE; x++) {
for (int y = 0; y < WORLDMAP_SIZE; y++) {
if (biomes[x][y]) {
tiles[x][y].terrain = biomes[x][y]->pick_terrain();
} else {
tiles[x][y].set_terrain("ocean");
}
}
}
/* At this point, we have a lot of blobs of terrain, but no ocean!
* The draw_island function sets altitude to 100 at its center and randomly
* slopes down in a way that introduces penisulas &c
*/
int center = WORLDMAP_SIZE / 2, shift = WORLDMAP_SIZE / 10;
Point island_center( rng(center - shift, center + shift),
rng(center - shift, center + shift) );
std::vector<std::vector<int> > altitude;
std::vector<int> tmpvec;
for (int x = 0; x < WORLDMAP_SIZE; x++) {
tmpvec.push_back(0);
}
for (int x = 0; x < WORLDMAP_SIZE; x++) {
altitude.push_back(tmpvec);
}
draw_island(altitude, island_center, 400, 20, 0);
// Now draw several (8) more, small islands
for (int i = 0; i < 8; i++) {
Point islet;
switch (rng(1, 4)) { // Which side to place it along?
case 1:
islet.x = rng(0, WORLDMAP_SIZE - 1);
islet.y = rng(15, 40);
break;
case 2:
islet.x = rng(WORLDMAP_SIZE - 41, WORLDMAP_SIZE - 16);
islet.y = rng(0, WORLDMAP_SIZE - 1);
break;
case 3:
islet.x = rng(0, WORLDMAP_SIZE - 1);
islet.y = rng(WORLDMAP_SIZE - 41, WORLDMAP_SIZE - 16);
break;
case 4:
islet.x = rng(15, 40);
islet.y = rng(0, WORLDMAP_SIZE - 1);
break;
}
int size = 80;
draw_island(altitude, islet, size, 2, i + 1);
while (one_in(3)) { // island chain
if (one_in(2)) {
islet.x -= rng(size / 5, size / 3);
} else {
islet.x += rng(size / 5, size / 3);
}
if (one_in(2)) {
islet.y -= rng(size / 5, size / 3);
} else {
islet.y += rng(size / 5, size / 3);
}
size -= rng(0, 20);
/* Using "i + 1" as the ID means that all islands in the same chain are
* considered to be the "same" island - for whatever purposes the island ID is
* ultimately used for (e.g. random_tile_with_terrain()). TODO: change???
*/
draw_island(altitude, islet, size, 2, i + 1);
}
}
// Now find all lake biomes that are ocean-adjacent and make them shallows.
// Also, all the surviving lakes should become a river seed
std::vector<Point> river_seeds;
for (int i = 0; i < lake_seeds.size(); i++) {
std::vector<Point> lake_points;
std::vector<Point> live_points;
lake_points.push_back( lake_seeds[i] );
live_points.push_back( lake_seeds[i] );
bool ocean = false;
while (!live_points.empty()) {
Point p = live_points[0];
for (int x = p.x - 1; x <= p.x + 1; x++) {
for (int y = p.y - 1; y <= p.y + 1; y++) {
if (x >= 0 && x < WORLDMAP_SIZE && y >= 0 && y < WORLDMAP_SIZE) {
if (lake[x][y] == LAKE_UNCHECKED) {
lake_points.push_back( Point(x, y) );
live_points.push_back( Point(x, y) );
lake[x][y] = LAKE_CHECKED;
} else if (!ocean && altitude[x][y] <= 0) {
ocean = true;
}
}
}
}
live_points.erase(live_points.begin());
}
if (ocean) {
for (int i = 0; i < lake_points.size(); i++) {
Point p = lake_points[i];
altitude[p.x][p.y] = 0;
//set_terrain(p.x, p.y, "tester");
}
} else {
river_seeds.push_back( lake_seeds[i] );
}
}
// For each river seed, draw a river that *tends* to slope down until it hits
// ocean.
for (int i = 0; i < river_seeds.size(); i++) {
Point rp = river_seeds[i];
bool done = false;
// TODO: This occasionally fails to terminate.
while (!done) {
if (!tiles[rp.x][rp.y].terrain->has_flag(WTF_NO_RIVER) &&
!tiles[rp.x][rp.y].terrain->has_flag(WTF_WATER) ) {
tiles[rp.x][rp.y].set_terrain("river");
}
std::vector<Point> next;
std::vector<int> chances;
int total_chance = 0;
for (int n = 1; n <= 4; n++) {
int x, y;
switch (n) {
case 1: x = rp.x - 1; y = rp.y ; break;
case 2: x = rp.x + 1; y = rp.y ; break;
case 3: x = rp.x ; y = rp.y - 1; break;
case 4: x = rp.x ; y = rp.y + 1; break;
}
if (x < 0 || x >= WORLDMAP_SIZE || y < 0 || y >= WORLDMAP_SIZE ||
tiles[x][y].terrain->has_flag(WTF_SALTY) || altitude[x][y] <= 0) {
done = true;
// no_river tiles are only acceptable if it has the water flag too
} else if (!tiles[x][y].terrain->has_flag(WTF_NO_RIVER) ||
tiles[x][y].terrain->has_flag(WTF_WATER)) {
next.push_back( Point(x, y) );
int chance;
if (altitude[x][y] > altitude[rp.x][rp.y]) {
//chance = 100 + altitude[rp.x][rp.y] - altitude[x][y];
chance = 5;
} else { // Better chance for places we slope down to
//chance = 150 + altitude[rp.x][rp.y] - altitude[x][y];
chance = 20;
}
if (tiles[x][y].terrain->has_flag(WTF_WATER)) {
chance += 7;
}
chances.push_back(chance);
total_chance += chance;
}
}
if (chances.empty()) {
done = true;
}
// Now pick from among those options.
if (!done) {
int index = rng(1, total_chance);
bool pick_done = false;
for (int i = 0; !pick_done && i < chances.size(); i++) {
index -= chances[i];
if (index <= 0) {
rp = next[i];
pick_done = true;
}
}
}
}
}
// Take everything with altitude <= 0 and set it to be ocean.
for (int x = 0; x < WORLDMAP_SIZE; x++) {
for (int y = 0; y < WORLDMAP_SIZE; y++) {
if (altitude[x][y] <= 0 && !biomes[x][y]->has_flag(BIOME_FLAG_NO_OCEAN)) {
tiles[x][y].set_terrain("ocean");
biomes[x][y] = BIOMES.lookup_name("ocean");
// If it's a hub, i.e. a city_seed, try to reposition it within 5 tiles
if (city[x][y] == CITY_HUB) {
for (int i = 0; i < city_seeds.size(); i++) {
if (city_seeds[i].x == x && city_seeds[i].y == y) {
i = city_seeds.size();
bool done = false;
for (int rad = 1; !done && rad <= 5; rad++) {
for (int rx = x - rad; !done && rx <= x + rad; rx++) {
int ry = y - rad;
if (rx >= 0 && rx < WORLDMAP_SIZE &&
ry >= 0 && ry < WORLDMAP_SIZE &&
altitude[rx][ry] > 0 && city[rx][ry] != CITY_NOTCITY &&
!done) {
city[rx][ry] = CITY_RAW;
city_seeds.push_back( Point(rx, ry) );
done = true;
}
ry = y + rad;
if (rx >= 0 && rx < WORLDMAP_SIZE &&
ry >= 0 && ry < WORLDMAP_SIZE &&
altitude[rx][ry] > 0 && city[rx][ry] != CITY_NOTCITY &&
!done) {
city[rx][ry] = CITY_RAW;
city_seeds.push_back( Point(rx, ry) );
done = true;
}
}
for (int ry = y - rad; !done && ry <= y + rad; ry++) {
int rx = x - rad;
if (rx >= 0 && rx < WORLDMAP_SIZE &&
ry >= 0 && ry < WORLDMAP_SIZE &&
altitude[rx][ry] > 0 && city[rx][ry] != CITY_NOTCITY &&
!done) {
city[rx][ry] = CITY_RAW;
city_seeds.push_back( Point(rx, ry) );
done = true;
}
rx = x + rad;
if (rx >= 0 && rx < WORLDMAP_SIZE &&
ry >= 0 && ry < WORLDMAP_SIZE &&
altitude[rx][ry] > 0 && city[rx][ry] != CITY_NOTCITY &&
!done) {
city[rx][ry] = CITY_RAW;
city_seeds.push_back( Point(rx, ry) );
done = true;
}
}
}
}
}
}
city[x][y] = CITY_NOTCITY;
} else {
if (city[x][y] == CITY_HUB) {
city[x][y] = CITY_RAW;
}
int range = tiles[x][y].terrain->beach_range;
if (range != -1) {
for (int xn = x - range; xn <= x + range; xn++) {
for (int yn = y - range; yn <= y + range; yn++) {
if (xn >= 0 && xn < WORLDMAP_SIZE &&
yn >= 0 && yn < WORLDMAP_SIZE && altitude[xn][yn] <= 0) {
tiles[x][y].terrain = make_into_beach(tiles[x][y].terrain);
}
}
}
}
}
}
}
// Erase any remaining below-water cities
for (int i = 0; i < city_seeds.size(); i++) {
Point p = city_seeds[i];
if (tiles[p.x][p.y].terrain->road_cost <= 0 ||
altitude[p.x][p.y] <= 0) {
city_seeds.erase(city_seeds.begin() + i);
i--;
}
}
// Draw some roads between cities.
if (city_seeds.size() > 1) {
for (int i = 0; i < city_seeds.size(); i++) {
Generic_map gmap = get_generic_map();
Pathfinder pf(gmap);
pf.set_allow_diagonal(false);
pf.set_bounds(20);
Point from = city_seeds[i];
// This is a roll to get any index EXCEPT the current one;
// If we roll the current one, use the last one (which the roll skips)
int index = rng(0, city_seeds.size() - 2);
if (index == i) {
index = city_seeds.size() - 1;
}
Point to = city_seeds[index];
Path path = pf.get_path(PATH_A_STAR, from, to);
if (path.get_cost() <= 150000) {
for (int n = 0; n < path.size(); n++) {
Tripoint p = path[n];
if (!tiles[p.x][p.y].terrain->has_flag(WTF_NO_ROAD)) {
if (tiles[p.x][p.y].terrain->has_flag(WTF_BRIDGE)) {
tiles[p.x][p.y].set_terrain("bridge");
} else {
tiles[p.x][p.y].set_terrain("road");
if (city[p.x][p.y] != CITY_NOTCITY) {
city[p.x][p.y] = CITY_ROAD;
}
}
}
}
}
}
}
// Now fill in cities!
for (int i = 0; i < city_seeds.size(); i++) {
std::vector<Point> active;
active.push_back( city_seeds[i] );
bool vertical_blocks = one_in(2);
int block_size = rng(4, 8);
while (!active.empty()) {
int index = 0;
Point p = active[index];
if (( vertical_blocks && (p.x % 3 != 0 && p.y % block_size != 0)) ||
(!vertical_blocks && (p.y % 3 != 0 && p.x % block_size != 0)) ) {
index = rng(0, active.size() - 1);
p = active[index];
}
active.erase(active.begin() + index);
bool expansions = false;
int roads = 0;
City_status stat = city[p.x][p.y];
for (int n = 0; n < 4; n++) {
Point expand;
switch (n) {
case 0: expand = Point(p.x - 1, p.y ); break;
case 1: expand = Point(p.x + 1, p.y ); break;
case 2: expand = Point(p.x , p.y - 1); break;
case 3: expand = Point(p.x , p.y + 1); break;
}
if (expand.x >= 0 && expand.x < WORLDMAP_SIZE &&
expand.y >= 0 && expand.y < WORLDMAP_SIZE) {
if (city[expand.x][expand.y] == CITY_RAW &&
stat != CITY_BUILDING_CLOSED) {
if (( vertical_blocks &&
(expand.x % 3 == 0 || expand.y % block_size == 0)) ||
(!vertical_blocks &&
(expand.y % 3 == 0 || expand.x % block_size == 0)) ) {
active.insert(active.begin(), expand);
} else {
active.push_back( expand );
}
city[expand.x][expand.y] = CITY_BUILDING;
if (rl_dist(expand.x, expand.y, city_seeds[i].x, city_seeds[i].y) <=
rng(1, 6)) {
tiles[expand.x][expand.y].terrain = random_shop();
} else {
tiles[expand.x][expand.y].set_terrain("house");
}
expansions = true;
} else if (city[expand.x][expand.y] == CITY_ROAD) {
if (stat != CITY_BUILDING_CLOSED) {
active.push_back( expand );
}
roads++;
} else if (city[expand.x][expand.y] == CITY_ROAD_CLOSED) {
roads++;
} else if (city[expand.x][expand.y] == CITY_BUILDING &&
stat != CITY_BUILDING_CLOSED) {
city[expand.x][expand.y] = CITY_BUILDING_CLOSED;
}
}
if (!expansions) {
bool block_closer = (( vertical_blocks && p.x % 3 == 0) ||
(!vertical_blocks && p.y % 3 == 0) );
if (block_closer) {
if (vertical_blocks && p.y % block_size != 1 &&
p.y % block_size != block_size - 1) {
block_closer = false;
} else if (!vertical_blocks && p.x % block_size != 1 &&
p.x % block_size != block_size - 1) {
block_closer = false;
}
}
if (roads == 2 && block_closer) {
city[p.x][p.y] = CITY_ROAD_CLOSED;
tiles[p.x][p.y].set_terrain("road");
} else if (city[p.x][p.y] == CITY_BUILDING) {
city[p.x][p.y] = CITY_BUILDING_CLOSED;
} else if (city[p.x][p.y] == CITY_ROAD) {
city[p.x][p.y] = CITY_ROAD_CLOSED;
}
} else {
city[p.x][p.y] = CITY_ROAD_CLOSED;
tiles[p.x][p.y].set_terrain("road");
}
}
}
}
// Take any usued city tiles and make them field
for (int x = 0; x < WORLDMAP_SIZE; x++) {
for (int y = 0; y < WORLDMAP_SIZE; y++) {
if (city[x][y] == CITY_RAW) {
tiles[x][y].set_terrain("field");
biomes[x][y] = BIOMES.lookup_name("grassland");
int range = tiles[x][y].terrain->beach_range;
for (int xn = x - range; xn <= x + range; xn++) {
for (int yn = y - range; yn <= y + range; yn++) {
if (xn >= 0 && xn < WORLDMAP_SIZE &&
yn >= 0 && yn < WORLDMAP_SIZE && altitude[xn][yn] <= 0) {
tiles[x][y].terrain = make_into_beach(tiles[x][y].terrain);
}
}
}
}
}
}
// Finally, place mosnters!
place_monsters();
}
void Worldmap::place_monsters()
{
for (int x = 0; x < WORLDMAP_SIZE; x++) {
for (int y = 0; y < WORLDMAP_SIZE; y++) {
if (biomes[x][y]) {
Variable_monster_genus var = biomes[x][y]->monsters;
// Decide how many genera to place here.
int placed = var.pick_number();
if (placed > 0) {
std::vector<Monster_genus*> gens = var.pick(placed);
for (int i = 0; i < gens.size(); i++) {
Monster_genus* genus = gens[i];
int population = biomes[x][y]->monster_population.roll() / placed;
if (genus && population > 0) {
Monster_spawn tmpspawn;
tmpspawn.genus = genus;
tmpspawn.population = population;
tiles[x][y].monsters.push_back(tmpspawn);
}
}
}
}
}
}
}
void Worldmap::draw_island(std::vector<std::vector<int> > &altitude,
Point center, int height, int edge_dist, int id)
{
if (center.x < 0 || center.x >= WORLDMAP_SIZE ||
center.y < 0 || center.y >= WORLDMAP_SIZE ) {
return;
}
altitude[center.x][center.y] = height;
add_point_to_island(center, id);
std::vector<Point> points_active;
points_active.push_back(center);
int center_point = WORLDMAP_SIZE / 2;
int shift = WORLDMAP_SIZE / 10;
// As long as we have active points...
while (!points_active.empty()) {
// We're going to empty out points_active and replace it with new_points.
std::vector<Point> new_points;
while (!points_active.empty()) {
// Pick a random point...
int index = rng(0, points_active.size() - 1);
Point p = points_active[index];
// Cycle through all adjacent points (no diagonals!)
for (int i = 0; i < 4; i++) {
int x, y;
switch (i) {
case 0: x = p.x - 1; y = p.y; break;
case 1: x = p.x + 1; y = p.y; break;
case 2: x = p.x; y = p.y - 1; break;
case 3: x = p.x; y = p.y + 1; break;
}
if (x > 0 && x < WORLDMAP_SIZE && y > 0 && y < WORLDMAP_SIZE &&
altitude[x][y] == 0) {
// Figure out how far we are from the edge - either vertically or horizontally
int dist_from_edge = (x > center_point ? WORLDMAP_SIZE - x : x);
int y_dist = (y > center_point ? WORLDMAP_SIZE - y : y);
if (y_dist < dist_from_edge) {
dist_from_edge = y_dist;
}
// Add that adjacent point to new_points & copy altitude
new_points.push_back( Point(x, y) );
altitude[x][y] = altitude[p.x][p.y];
// Reduce the altitude in one of many ways:
if (dist_from_edge < rng(0, edge_dist)) {
// Slope down rapidly if we're close to the edge, to avoid straight lines
altitude[x][y] -= rng(20, 100);
} else if (one_in(30)) {
// Rarely, slope down rapidly. This promotes gulfs and fjords.
altitude[x][y] -= rng(0, 100);
} else if (!one_in(10)) {
/* Otherwise, 90% of the time slope slowly; 10% don't slope. The latter
* promotes peninsulae.
*/
altitude[x][y] -= rng(0, shift);
}
// If the altitude winds up greater than 0, add us to the island ID
if (altitude[x][y] > 0) {
add_point_to_island( Point(x, y), id );
}
} // if (is in bounds & altitude == 0)
} // for (int i = 0; i < 4; i++)
// Erase the point we were working on
points_active.erase(points_active.begin() + index);
} // while (!active_points.empty())
points_active = new_points;
} // while (!active_points.empty())
// Set anything < 0 to 0.
for (int x = 0; x < WORLDMAP_SIZE; x++) {
for (int y = 0; y < WORLDMAP_SIZE; y++) {
if (altitude[x][y] < 0) {
altitude[x][y] = 0;
}
}
}
}
void Worldmap::add_point_to_island(Point p, int id)
{
if (id < 0) {
return;
}
if (islands.count(id) == 0) {
std::vector<Point> tmp;
tmp.push_back(p);
islands[id] = tmp;
} else {
islands[id].push_back(p);
}
}