-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_map.cpp
431 lines (402 loc) · 20 KB
/
generate_map.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
#include "header.h"
#include "globals.h"
// 00:30 <~Vornicus> basic method afaict is to use a plasma fractal (easy to look
// up) to assign altitudes, then use the altitudes to assign
// terrains.
// 00:31 <~Vornicus> You can then do things like "rain piles up on the windward
// side of mountains", "rivers go from cell to cell in the
// steepest direction", and some other stuff like that.
// RIVERS ARE FUCKED UP GOD DAMN
bool are_coords_valid(int x, int y) {
if (x <0) return false;
if (y <0) return false;
if (x >= MAX_GAME_MAP_X) return false;
if (y >= MAX_GAME_MAP_Y) return false;
return true;
}
bool any_surrounding_tile_match(int x, int y, int tile_code) {
if (gs.gm.tiles[x+1][y+1].tile_id == tile_code && are_coords_valid(x+1,y+1)) { return true; }
if (gs.gm.tiles[x+1][y].tile_id == tile_code && are_coords_valid(x+1,y)) { return true; }
if (gs.gm.tiles[x+1][y-1].tile_id == tile_code && are_coords_valid(x+1,y-1)) { return true; }
if (gs.gm.tiles[x][y+1].tile_id == tile_code && are_coords_valid(x,y+1)) { return true; }
if (gs.gm.tiles[x][y-1].tile_id == tile_code && are_coords_valid(x,y-1)) { return true; }
if (gs.gm.tiles[x-1][y+1].tile_id == tile_code && are_coords_valid(x-1,y+1)) { return true; }
if (gs.gm.tiles[x-1][y].tile_id == tile_code && are_coords_valid(x-1,y)) { return true; }
if (gs.gm.tiles[x-1][y-1].tile_id == tile_code && are_coords_valid(x-1,y-1)) { return true; }
return false;
}
bool any_surrounding_unvisited_river(int x, int y) {
if ((gs.gm.tiles[x+1][y+1].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x+1][y+1].flags&TILE_FLAGS_ROAD) && are_coords_valid(x+1,y+1)) { return true; }
if ((gs.gm.tiles[x+1][y].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x+1][y].flags&TILE_FLAGS_ROAD) && are_coords_valid(x+1,y)) { return true; }
if ((gs.gm.tiles[x+1][y-1].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x+1][y-1].flags&TILE_FLAGS_ROAD) && are_coords_valid(x+1,y-1)) { return true; }
if ((gs.gm.tiles[x][y+1].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x][y+1].flags&TILE_FLAGS_ROAD) && are_coords_valid(x,y+1)) { return true; }
if ((gs.gm.tiles[x][y-1].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x][y-1].flags&TILE_FLAGS_ROAD) && are_coords_valid(x,y-1)) { return true; }
if ((gs.gm.tiles[x-1][y+1].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x-1][y+1].flags&TILE_FLAGS_ROAD) && are_coords_valid(x-1,y+1)) { return true; }
if ((gs.gm.tiles[x-1][y].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x-1][y].flags&TILE_FLAGS_ROAD) && are_coords_valid(x-1,y)) { return true; }
if ((gs.gm.tiles[x-1][y-1].flags&TILE_FLAGS_RIVER) && !(gs.gm.tiles[x-1][y-1].flags&TILE_FLAGS_ROAD) && are_coords_valid(x-1,y-1)) { return true; }
return false;
}
bool any_available_spaces_to_go(int x, int y) {
if (are_coords_valid(x+1,y+1) && !(gs.gm.tiles[x+1][y+1].flags&TILE_FLAGS_ROAD)) { return true; }
if (are_coords_valid(x+1,y) && !(gs.gm.tiles[x+1][y].flags&TILE_FLAGS_ROAD)) { return true; }
if (are_coords_valid(x+1,y-1) && !(gs.gm.tiles[x+1][y-1].flags&TILE_FLAGS_ROAD)) { return true; }
if (are_coords_valid(x,y+1) && !(gs.gm.tiles[x][y+1].flags&TILE_FLAGS_ROAD)) { return true; }
if (are_coords_valid(x,y-1) && !(gs.gm.tiles[x][y-1].flags&TILE_FLAGS_ROAD)) { return true; }
if (are_coords_valid(x-1,y+1) && !(gs.gm.tiles[x-1][y+1].flags&TILE_FLAGS_ROAD)) { return true; }
if (are_coords_valid(x-1,y) && !(gs.gm.tiles[x-1][y].flags&TILE_FLAGS_ROAD)) { return true; }
if (are_coords_valid(x-1,y-1) && !(gs.gm.tiles[x-1][y-1].flags&TILE_FLAGS_ROAD)) { return true; }
return false;
}
bool valid_spot(int x, int y) {
if (are_coords_valid(x,y) && !(gs.gm.tiles[x][y].flags&TILE_FLAGS_ROAD)) return true;
return false;
}
int generate_map (int seed, int params) {
int number;
// just making sure everything is initialized somehow
for (int i=0;i<MAX_GAME_MAP_X;i++) {
for (int j=0;j<MAX_GAME_MAP_Y;j++) {
gs.gm.tiles[i][j]=TILE_ARCTIC;
//gs.gm.rivers[i][j]=0;
//gs.gm.roads[i][j]=0;
}
}
// let's try beginning with water/land division
for (int i=0;i<MAX_GAME_MAP_X;i++) {
for (int j=0;j<MAX_GAME_MAP_Y;j++) {
if (rand()%100 < 5 ) {
gs.gm.tiles[i][j]=TILE_WATER;
} else {
gs.gm.tiles[i][j]=TILE_GRASSLAND;
}
}
}
// passes of adding water, assigning to flags as a placeholder
for (int k=0;k<5;k++) {
for (int i=0;i<MAX_GAME_MAP_X;i++) {
for (int j=0;j<MAX_GAME_MAP_Y;j++) {
if (gs.gm.tiles[i][j].tile_id == IMG_TILE_WATER) {
if (rand()%100 < 10 && j-1 > 0) gs.gm.tiles[i][j-1].flags = IMG_TILE_WATER;
if (rand()%100 < 10 && j-1 > 0 && i+1 < MAX_GAME_MAP_X) gs.gm.tiles[i+1][j-1].flags = IMG_TILE_WATER;
if (rand()%100 < 10 && j-1 > 0 && i-1 > 0) gs.gm.tiles[i-1][j-1].flags = IMG_TILE_WATER;
if (rand()%100 < 10 && i+1 < MAX_GAME_MAP_X) gs.gm.tiles[i+1][j].flags = IMG_TILE_WATER;
if (rand()%100 < 10 && i-1 > 0) gs.gm.tiles[i-1][j].flags = IMG_TILE_WATER;
if (rand()%100 < 10 && j+1 < MAX_GAME_MAP_Y && i+1 < MAX_GAME_MAP_X) gs.gm.tiles[i+1][j+1].flags = IMG_TILE_WATER;
if (rand()%100 < 10 && j+1 < MAX_GAME_MAP_Y) gs.gm.tiles[i][j+1].flags = IMG_TILE_WATER;
if (rand()%100 < 10 && i-1 > 0 && j+1 < MAX_GAME_MAP_Y) gs.gm.tiles[i-1][j+1].flags = IMG_TILE_WATER;
}
}
}
for (int i=0;i<MAX_GAME_MAP_X;i++) {
for (int j=0;j<MAX_GAME_MAP_Y;j++) {
if (gs.gm.tiles[i][j].flags == IMG_TILE_WATER ) {
gs.gm.tiles[i][j]=TILE_WATER;
//gs.gm.rivers[i][j] = 0;
}
}
}
}
//now filling out the ground tiles, according to zones of climate
for (int i=0;i<MAX_GAME_MAP_X;i++) {
for (int j=0;j<MAX_GAME_MAP_Y;j++) {
if (gs.gm.tiles[i][j].tile_id == IMG_TILE_WATER) continue;
if (j<MAX_GAME_MAP_Y*0.10 || j>=MAX_GAME_MAP_Y*0.90) { // extremely cold
number = rand()%10+1;
switch (number) {
case 1:
case 2:
case 3:
gs.gm.tiles[i][j]=TILE_ARCTIC;
break;
case 4:
case 5:
case 6:
gs.gm.tiles[i][j]=TILE_TUNDRA;
break;
case 7:
case 8:
case 9:
case 10:
gs.gm.tiles[i][j]=TILE_TUNDRA;
break;
default:
gs.gm.tiles[i][j]=TILE_WATER;
break;
}
} else if ((j>=MAX_GAME_MAP_Y*0.10 && j<MAX_GAME_MAP_Y*0.20) || (j>=MAX_GAME_MAP_Y*0.80 && j<MAX_GAME_MAP_Y*0.90)) { // cold
number = rand()%10+1;
switch (number) {
case 1:
case 2:
gs.gm.tiles[i][j]=TILE_TUNDRA;
break;
case 3:
case 4:
gs.gm.tiles[i][j]=TILE_FOREST;
break;
case 5:
gs.gm.tiles[i][j]=TILE_GRASSLAND;
break;
case 6:
gs.gm.tiles[i][j]=TILE_HILLS;
break;
case 7:
gs.gm.tiles[i][j]=TILE_MOUNTAIN;
break;
case 8:
gs.gm.tiles[i][j]=TILE_SWAMP;
break;
case 9:
gs.gm.tiles[i][j]=TILE_SWAMP;
break;
case 10:
gs.gm.tiles[i][j]=TILE_FOREST;
break;
default:
gs.gm.tiles[i][j]=TILE_WATER;
break;
}
} else if ((j>=MAX_GAME_MAP_Y*0.20 && j<MAX_GAME_MAP_Y*0.30) || (j>=MAX_GAME_MAP_Y*0.70 && j<MAX_GAME_MAP_Y*0.80)) { // temperate
number = rand()%10+1;
switch (number) {
case 1:
case 2:
gs.gm.tiles[i][j]=TILE_GRASSLAND;
break;
case 3:
case 4:
gs.gm.tiles[i][j]=TILE_FOREST;
break;
case 5:
gs.gm.tiles[i][j]=TILE_GRASSLAND;
break;
case 6:
gs.gm.tiles[i][j]=TILE_HILLS;
break;
case 7:
gs.gm.tiles[i][j]=TILE_MOUNTAIN;
break;
case 8:
gs.gm.tiles[i][j]=TILE_SWAMP;
break;
case 9:
case 10:
gs.gm.tiles[i][j]=TILE_FOREST;
break;
default:
gs.gm.tiles[i][j]=TILE_WATER;
break;
}
} else if ((j>=MAX_GAME_MAP_Y*0.30 && j<MAX_GAME_MAP_Y*0.40) || (j>=MAX_GAME_MAP_Y*0.60 && j<MAX_GAME_MAP_Y*0.70)) { // hot and dry
number = rand()%10+1;
switch (number) {
case 1:
case 2:
gs.gm.tiles[i][j]=TILE_DESERT;
break;
case 3:
case 4:
gs.gm.tiles[i][j]=TILE_GRASSLAND;
break;
case 5:
gs.gm.tiles[i][j]=TILE_GRASSLAND;
break;
case 6:
gs.gm.tiles[i][j]=TILE_HILLS;
break;
case 7:
gs.gm.tiles[i][j]=TILE_MOUNTAIN;
break;
case 8:
gs.gm.tiles[i][j]=TILE_DESERT;
break;
case 9:
case 10:
gs.gm.tiles[i][j]=TILE_DESERT;
break;
default:
gs.gm.tiles[i][j]=TILE_WATER;
break;
}
} else { // hot and wet
number = rand()%10+1;
switch (number) {
case 1:
case 2:
gs.gm.tiles[i][j]=TILE_JUNGLE;
break;
case 3:
case 4:
gs.gm.tiles[i][j]=TILE_JUNGLE;
break;
case 5:
gs.gm.tiles[i][j]=TILE_GRASSLAND;
break;
case 6:
gs.gm.tiles[i][j]=TILE_HILLS;
break;
case 7:
gs.gm.tiles[i][j]=TILE_MOUNTAIN;
break;
case 8:
gs.gm.tiles[i][j]=TILE_SWAMP;
break;
case 9:
case 10:
gs.gm.tiles[i][j]=TILE_JUNGLE;
break;
default:
gs.gm.tiles[i][j]=TILE_WATER;
break;
}
}
//gs.gm.tiles[i][j]=rand()%10+1;
//gs.gm.rivers[i][j]=rand()%2-rand()%3+1;
//gs.gm.roads[i][j]=rand()%2-rand()%3+1;
}
}
// at this point, it's a random map with only tiles defined
// it makes sort of sense, according to climate zones
// let's try rivers next
// starting somewhere and randomly walking towards water
int xx,yy; int oxx, oyy;
int counter;
int validopts[8];
for (int k=0;k<MAX_RIVERS;k++) {
do { // generate non-water, non-river-adjacent initial tile
xx = rand()%MAX_GAME_MAP_X;
yy = rand()%MAX_GAME_MAP_Y;
} while (gs.gm.tiles[xx][yy].tile_id == IMG_TILE_WATER || gs.gm.tiles[xx][yy].flags&TILE_FLAGS_RIVER || any_surrounding_unvisited_river(xx,yy));
gs.gm.tiles[xx][yy].flags = gs.gm.tiles[xx][yy].flags|TILE_FLAGS_RIVER; // place river here
while (!any_surrounding_tile_match(xx,yy,IMG_TILE_WATER)
&& !any_surrounding_unvisited_river(xx,yy)) {
// save old coords
oxx = xx;
oyy = yy;
// pick a random valid neighbour to go to, or call it quits if none exist
if (!any_available_spaces_to_go(xx, yy)) { break; }
counter=0;
for (int i=0;i<8;i++) validopts[i] = 0;
if (valid_spot(xx+1,yy+1)) { validopts[counter] = 0; counter++; }
if (valid_spot(xx+1,yy)) { validopts[counter] = 1; counter++; }
if (valid_spot(xx+1,yy-1)) { validopts[counter] = 2; counter++; }
if (valid_spot(xx,yy+1)) { validopts[counter] = 3; counter++; }
if (valid_spot(xx,yy-1)) { validopts[counter] = 4; counter++; }
if (valid_spot(xx-1,yy+1)) { validopts[counter] = 5; counter++; }
if (valid_spot(xx-1,yy)) { validopts[counter] = 6; counter++; }
if (valid_spot(xx-1,yy-1)) { validopts[counter] = 7; counter++; }
if (counter>0) number=validopts[rand()%counter]; else break;
if (number == 0) { xx++; yy++; }
else if (number == 1) { xx++; }
else if (number == 2) { xx++; yy--; }
else if (number == 3) { yy++; }
else if (number == 4) { yy--; }
else if (number == 5) { xx--; yy++; }
else if (number == 6) { xx--; }
else if (number == 7) { xx--; yy--; }
else { break; } // what the shit?
// add river here
gs.gm.tiles[xx][yy].flags = gs.gm.tiles[xx][yy].flags|TILE_FLAGS_RIVER;
// visited spots get tracked
if (valid_spot(oxx+1,oyy+1)) gs.gm.tiles[oxx+1][oyy+1].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx+1,oyy)) gs.gm.tiles[oxx+1][oyy].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx+1,oyy-1)) gs.gm.tiles[oxx+1][oyy-1].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx,oyy+1)) gs.gm.tiles[oxx][oyy+1].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx,oyy)) gs.gm.tiles[oxx][oyy].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx,oyy-1)) gs.gm.tiles[oxx][oyy-1].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx-1,oyy+1)) gs.gm.tiles[oxx-1][oyy+1].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx-1,oyy)) gs.gm.tiles[oxx-1][oyy].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
if (valid_spot(oxx-1,oyy-1)) gs.gm.tiles[oxx-1][oyy-1].flags = gs.gm.tiles[oxx+1][oyy+1].flags|TILE_FLAGS_ROAD;
}
// clean up visited
for (int i=0;i<MAX_GAME_MAP_X;i++) {
for (int j=0;j<MAX_GAME_MAP_Y;j++) {
if (gs.gm.tiles[i][j].flags&TILE_FLAGS_ROAD) gs.gm.tiles[i][j].flags = gs.gm.tiles[i][j].flags - TILE_FLAGS_ROAD; // remove road marker
}
}
}
// SHIT'S FUCKED
/*
int xx,yy; int oxx, oyy;
int validopts[8];
int xc, yc;
int done;
int dir;
//789
//4 6
//123
int counter;
for (int k=0;k<MAX_RIVERS;k++) {
do { // generate non-water, non-river-adjacent initial tile
xx = rand()%MAX_GAME_MAP_X;
yy = rand()%MAX_GAME_MAP_Y;
} while (gs.gm.tiles[xx][yy] == IMG_TILE_WATER || any_surrounding_unvisited_river(xx,yy));
gs.gm.rivers[xx][yy] = 1; // place river here
while (!any_surrounding_tile_match(xx,yy,IMG_TILE_WATER)
&& !any_surrounding_unvisited_river(xx,yy)) {
// save old coords
oxx = xx;
oyy = yy;
// pick a random valid neighbour to go to, or call it quits if none exist
if (!any_available_spaces_to_go(xx, yy)) { break; }
if (valid_spot(xx+1,yy+1)) { xx++; yy++;}
else if (valid_spot(xx+1,yy)) { xx++; yy;}
else if (valid_spot(xx+1,yy-1)) { xx++; yy--;}
else if (valid_spot(xx,yy+1)) { xx; yy++;}
else if (valid_spot(xx,yy-1)) { xx; yy--;}
else if (valid_spot(xx-1,yy+1)) { xx--; yy++;}
else if (valid_spot(xx-1,yy)) { xx--; yy;}
else if (valid_spot(xx-1,yy-1)) { xx--; yy--;}
else {break;}
/// *
counter=0;
for (int i=0;i<8;i++) validopts[i] = 0;
if (valid_spot(xx+1,yy+1)) { validopts[counter] = 0; counter++; }
if (valid_spot(xx+1,yy)) { validopts[counter] = 1; counter++; }
if (valid_spot(xx+1,yy-1)) { validopts[counter] = 2; counter++; }
if (valid_spot(xx,yy+1)) { validopts[counter] = 3; counter++; }
if (valid_spot(xx,yy-1)) { validopts[counter] = 4; counter++; }
if (valid_spot(xx-1,yy+1)) { validopts[counter] = 5; counter++; }
if (valid_spot(xx-1,yy)) { validopts[counter] = 6; counter++; }
if (valid_spot(xx-1,yy-1)) { validopts[counter] = 7; counter++; }
if (counter>0) number=validopts[rand()%counter]; else break;
if (number == 0) { xx++; yy++; }
else if (number == 1) { xx++; yy; }
else if (number == 2) { xx++; yy--; }
else if (number == 3) { xx; yy++; }
else if (number == 4) { xx; yy--; }
else if (number == 5) { xx--; yy++; }
else if (number == 6) { xx--; yy; }
else if (number == 7) { xx--; yy--; }
else { break; } // what the shit?
// * /
// add river here
gs.gm.rivers[xx][yy] = 1;
// visited spots get tracked
if (valid_spot(oxx+1,oyy+1)) gs.gm.roads[oxx+1][oyy+1] = 1;
if (valid_spot(oxx+1,oyy)) gs.gm.roads[oxx+1][oyy] = 1;
if (valid_spot(oxx+1,oyy-1)) gs.gm.roads[oxx+1][oyy-1] = 1;
if (valid_spot(oxx,oyy+1)) gs.gm.roads[oxx][oyy+1] = 1;
if (valid_spot(oxx,oyy)) gs.gm.roads[oxx][oyy] = 1;
if (valid_spot(oxx,oyy-1)) gs.gm.roads[oxx][oyy-1] = 1;
if (valid_spot(oxx-1,oyy+1)) gs.gm.roads[oxx-1][oyy+1] = 1;
if (valid_spot(oxx-1,oyy)) gs.gm.roads[oxx-1][oyy] = 1;
if (valid_spot(oxx-1,oyy-1)) gs.gm.roads[oxx-1][oyy-1] = 1;
}
// clean up visited
for (int i=0;i<MAX_GAME_MAP_X;i++) {
for (int j=0;j<MAX_GAME_MAP_Y;j++) {
gs.gm.roads[i][j] = 0;
}
}
}
*/
//00:39 <~Vornicus> After each step of your random walk, add all 8 tiles around
// the previous step to the visited list.
//00:42 <@AnnoDomini> Intredasting.
//00:42 <~Vornicus> This will prevent your river from looping.
//00:43 <~Vornicus> Successful termination occurs when the river reaches a sea or
// other river tile; failure occurs when you run out of tiles to
// go to that are not on the visited list.
return 0;
}