-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworld.cpp
2541 lines (2307 loc) · 71.5 KB
/
world.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "world.h"
#include "rng.h"
#include "geometry.h"
#include "window.h"
#include "cuss.h"
#include "keys.h" // for input_direction()
#include "kingdom.h" // To color map based on Kingdom.
#include "stringfunc.h" // For capitalize()
#include "animal.h"
#include "ai_city.h"
#include "pathfind.h" // For road building, trade route finding, and more!
#include "globals.h"
#include <sstream>
#include <vector>
#include <math.h> // for pow() and sqrt()
#include <fstream>
Map_seen::Map_seen()
{
}
Map_seen::Map_seen(int S)
{
init(S);
}
Map_seen::~Map_seen()
{
}
void Map_seen::init(int S)
{
size = S;
std::vector<bool> tmp;
for (int i = 0; i < size; i++) {
tmp.push_back(false);
}
for (int i = 0; i < size; i++) {
seen.push_back(tmp);
}
}
std::string Map_seen::save_data()
{
std::stringstream ret;
ret << size << std::endl;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
ret << seen[x][y] << " ";
}
}
return ret.str();
}
bool Map_seen::load_data(std::istream& data)
{
data >> size;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
int tmp_seen;
data >> tmp_seen;
seen[x][y] = tmp_seen;
}
}
return true;
}
bool Map_seen::OOB(Point p)
{
return OOB(p.x, p.y);
}
bool Map_seen::OOB(int x, int y)
{
return (x < 0 || x >= size ||
y < 0 || y >= size );
}
bool Map_seen::is_seen(Point p)
{
return is_seen(p.x, p.y);
}
bool Map_seen::is_seen(int x, int y)
{
if (OOB(x, y)) {
return false;
}
return seen[x][y];
}
void Map_seen::mark_seen(Point p)
{
mark_seen(p.x, p.y);
}
void Map_seen::mark_seen(int x, int y)
{
if (!OOB(x, y)) {
seen[x][y] = true;
}
}
World_design::World_design()
{
size = WORLD_SIZE_MEDIUM;
temperature = WORLD_TEMP_TEMPERATE;
rainfall = WORLD_RAIN_MODERATE;
mountain = WORLD_MOUNTAIN_VARIED;
}
World_design::~World_design()
{
}
World_map::World_map()
{
}
World_map::~World_map()
{
}
bool World_map::generate(World_design design)
{
// Some sanity checks
if (design.size == WORLD_SIZE_MAX) {
debugmsg("World_map generated with size of WORLD_SIZE_MAX!");
return false;
}
if (design.temperature == WORLD_TEMP_MAX) {
debugmsg("World_map generated with temperature of WORLD_TEMP_MAX!");
return false;
}
if (design.rainfall == WORLD_RAIN_MAX) {
debugmsg("World_map generated with rainfall of WORLD_RAIN_MAX!");
return false;
}
if (design.mountain == WORLD_MOUNTAIN_MAX) {
debugmsg("World_map generated with mountain of WORLD_MOUNTAIN_MAX!");
return false;
}
if (!set_size(design.size)) {
return false;
}
name = design.name;
int equator_temp = 100;
switch (design.temperature) {
case WORLD_TEMP_ICE_AGE: equator_temp = 25; break;
case WORLD_TEMP_COLD: equator_temp = 50; break;
case WORLD_TEMP_TEMPERATE: equator_temp = 100; break;
case WORLD_TEMP_HOT: equator_temp = 120; break;
case WORLD_TEMP_FURNACE: equator_temp = 175; break;
}
int temp_shift = 0;
for (int x = 0; x < size; x++) {
if (!one_in(3)) {
temp_shift += rng(-1, 1);
}
if (temp_shift < -15) {
temp_shift = -15;
} else if (temp_shift > 15) {
temp_shift = 15;
}
for (int y = 0; y < size; y++) {
tiles[x][y] = MAP_OCEAN;
altitude[x][y] = -1;
rainfall[x][y] = -1;
if (y < size / 2) {
temperature[x][y] = (equator_temp * (temp_shift + y)) / (size / 2);
} else {
int fy = size - 1 - y;
temperature[x][y] = (equator_temp * (temp_shift + fy)) / (size / 2);
}
//temperature[x][y] += rng(-1, 1);
//temperature[x][y] += rng(-3, 3);
continent_id[x][y] = -1;
kingdom_id [x][y] = -1;
city [x][y] = NULL;
river [x][y] = false;
road [x][y] = false;
}
}
continents.clear();
// Pick a bunch of points to base continents off of.
int num_continents = size / rng(6, 10);
for (int i = 0; i < num_continents; i++) {
Point continent(rng(20, size - 21), rng(20, size - 21));
continents.push_back(continent);
}
// Build continent, and rivers there
/* We want to figure out the min/max size for continents, given the size of the
* world map. But continents are built by starting with a seed altitude, and
* stepping down by some amount each tile (usually). So the size (usually) is
* about equal to the seed altitude divided by that step. We don't want to
* change our seed altitude, so we change the step instead. csize_min/max is
* the ratio of heigh-to-step.
*/
int csize_min = size / 50, csize_max = size / 10;
for (int i = 0; i < continents.size(); i++) {
int height;
int step; // Alt decreases by this each tile (usually).
if (one_in(8)) { // Island
if (one_in(2)) { // Low-altitude island
height = rng(20, 50);
step = rng(16 / csize_min, 240 / csize_max);
} else { // Mountainous island
height = rng(100, 150);
step = rng(200 / csize_min, 1400 / csize_max);
}
} else { // Normal continent!
height = rng(70, 125);
step = rng(24 / csize_min, 320 / csize_max);
}
if (step > height / 3) {
step = height / 3; // Let's be reasonable.
}
add_continent(continents[i], height, step, i);
} // for (int i = 0; i < continents.size(); i++)
// Add some mountain ranges.
int num_ranges = 0, range_width = 0;
int alt_foothill = 55, alt_mountain = 80;
switch (design.mountain) {
case WORLD_MOUNTAIN_FLAT:
num_ranges = 0;
range_width = 0;
alt_foothill = 80;
alt_mountain = 100;
break;
case WORLD_MOUNTAIN_LOW:
num_ranges = 2;
range_width = 1;
alt_foothill = 75;
alt_mountain = 95;
break;
case WORLD_MOUNTAIN_VARIED:
num_ranges = 4;
range_width = 1;
alt_foothill = 55;
alt_mountain = 80;
break;
case WORLD_MOUNTAIN_HIGH:
num_ranges = 8;
range_width = 2;
alt_foothill = 30;
alt_mountain = 65;
break;
case WORLD_MOUNTAIN_COVERED:
num_ranges = 15;
range_width = 4;
alt_foothill = 14;
alt_mountain = 40;
break;
}
int range_length_max = size / 3, range_length_min = size / 8;
for (int i = 0; i < num_ranges; i++) {
// Pick a starting point - anywhere in the world (but far enough from the edges
// to guarantee an in-bound end point).
Point start( rng(range_length_max, size - 1 - range_length_max),
rng(range_length_max, size - 1 - range_length_max) );
// Pick an ending point - fairly close to the starting point.
int min_x, max_x, min_y, max_y;
if (one_in(2)) { // Go west
min_x = start.x - range_length_max;
max_x = start.x - range_length_min;
} else { // Go east
min_x = start.x + range_length_min;
max_x = start.x + range_length_max;
}
if (one_in(2)) { // Go north
min_y = start.y - range_length_max;
max_y = start.y - range_length_min;
} else { // Go south
min_y = start.y + range_length_max;
max_y = start.y + range_length_min;
}
Point end(rng(min_x, max_x), rng(min_y, max_y));
// Generate a line between the two points.
std::vector<Point> range_line = line_to(start, end);
// Iterate over the line.
for (int n = 0; n < range_line.size(); n++) {
Point p = range_line[n];
// Affect all tiles within range_width. If we're close to the start or end of
// the range, use a shorter range_width and lower mountains.
int range_dist = range_width;
int alt_min = alt_foothill, alt_max = alt_mountain * 2;
if (rl_dist(start, p) <= 3 || rl_dist(end, p) <= 3) {
range_dist = 1;
alt_min = alt_min / 2;
alt_max = alt_mountain;
} else if (rl_dist(start, p) <= 6 || rl_dist(end, p) <= 6) {
alt_max = alt_mountain;
}
for (int x = p.x - range_dist; x <= p.x + range_dist; x++) {
for (int y = p.y - range_dist; y <= p.y + range_dist; y++) {
if (!OOB(x, y) && altitude[x][y] > 0) {
altitude[x][y] = rng(alt_min, alt_max);
}
}
}
}
} // for (int i = 0; i < num_ranges; i++)
// All continents get some rivers!
for (int i = 0; i < continents.size(); i++) {
Point river(continents[i].x + rng(-5, 5), continents[i].y + rng(-5, 5));
add_river(river);
int num = 3;
while (one_in(num)) {
river.x += rng(-4, 4);
river.y += rng(-4, 4);
add_river(river);
num++;
}
}
/* Set rainfall! Start at the western edge of the map, then proceed eastward.
* TODO: It'd be cool if the northern hemisphere used east-blowing winds (as
* below), but the southern hemisphere used west-blowing winds. And if
* wind strength were low at the equator and poles, and high in between.
*/
int base_rainfall = 35, rain_shift_down = -2, rain_shift_up = 4,
river_rain_min = 1, river_rain_max = 15, ocean_rain_chance = 4;
switch (design.rainfall) {
case WORLD_RAIN_DRY:
base_rainfall = 0;
rain_shift_down = -5;
rain_shift_up = 1;
river_rain_min = 0;
river_rain_max = 4;
ocean_rain_chance = 10;
break;
case WORLD_RAIN_LIGHT:
base_rainfall = 15;
rain_shift_down = -3;
rain_shift_up = 2;
river_rain_min = 1;
river_rain_max = 8;
ocean_rain_chance = 7;
break;
case WORLD_RAIN_MODERATE:
base_rainfall = 35;
rain_shift_down = -2;
rain_shift_up = 4;
river_rain_min = 1;
river_rain_max = 15;
ocean_rain_chance = 4;
break;
case WORLD_RAIN_HEAVY:
base_rainfall = 50;
rain_shift_down = -2;
rain_shift_up = 5;
river_rain_min = 2;
river_rain_max = 20;
ocean_rain_chance = 3;
break;
case WORLD_RAIN_UNENDING:
base_rainfall = 100;
rain_shift_down = -1;
rain_shift_up = 8;
river_rain_min = 5;
river_rain_max = 30;
ocean_rain_chance = 2;
break;
}
for (int y = 0; y < size; y++) {
rainfall[0][y] = base_rainfall;
}
for (int x = 1; x < size; x++) {
for (int y = 0; y < size; y++) {
// Copy the value of rainfall from the tile to the left (doubled to weight it).
int past = rainfall[x - 1][y] * 2;
// Sources is the number of inputs we've used - later on we'll divide by this
// number to get an average. Since inputs are weighted, sources starts as 2.
int sources = 2;
// Increase rainfall if we're over the ocean.
if (altitude[x][y] <= 0) {
past += rng(0, 1);
if (one_in(ocean_rain_chance)) {
past += rng(1, 2); // Sometimes it increases by a lot!
}
// If we're not over the ocean, occasionally decrease rainfall.
} else if (one_in(15)) {
past -= rng(0, 1);
}
// Use the tile to the northwest as a source - weighted higher!
if (y > 0 && rainfall[x - 1][y - 1] > 0) {
sources += 5;
past += rainfall[x - 1][y - 1] * 5;
if (rainfall[x - 1][y - 1] > rainfall[x - 1][y]) {
sources += 1;
past += rainfall[x - 1][y - 1];
}
}
// Ditto for the tile to the southwest.
if (y < size - 1 && rainfall[x - 1][y + 1] > 0) {
sources += 5;
past += rainfall[x - 1][y + 1] * 5;
if (rainfall[x - 1][y + 1] > rainfall[x - 1][y]) {
sources += 1;
past += rainfall[x - 1][y + 1];
}
}
past /= sources; // Grab the average.
// Lose a lot of rainfall if a mountain is blocking these moisture-laden winds.
if (altitude[x][y] >= 85) {
past = rng(0, past / 2);
// High hills sometimes block some moisture too.
} else if (altitude[x][y] >= 60 && one_in(6)) {
past -= rng(0, 4);
// Low altitudes (and low rainfall) give us a chance to alter the rainfall -
// usually increasing it a bit.
} else if (altitude[x][y] < 85 - past && one_in(3)) {
past = rng(past + rain_shift_down, past + rain_shift_up);
}
// Rivers add rainfall, resulting in forests and swamps to the east.
if (river[x][y]) {
if (one_in(10)) {
past += rng(river_rain_min * 10, river_rain_max * 3);
} else {
past += rng(river_rain_min, river_rain_max);
}
}
// High temperatures tend to decrease rainfall.
if (past > temperature[x][y]) {
past = rng(temperature[x][y], past);
}
// Finally, copy it over!
rainfall[x][y] = past;
if (rainfall[x][y] < 0) {
rainfall[x][y] = 0;
}
}
}
// Finally, set terrain based on altitude, temperature, rainfall and river
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
// First category: high-altitude stuff.
if (altitude[x][y] >= alt_mountain) {
if (river[x][y]) {
if (temperature[x][y] <= 20) {
tiles[x][y] = MAP_GLACIER;
} else {
tiles[x][y] = MAP_CANYON;
}
} else if (temperature[x][y] <= 20) {
tiles[x][y] = MAP_ICY_MOUNTAIN;
} else {
tiles[x][y] = MAP_MOUNTAINOUS;
}
// Next category: foothills.
} else if (altitude[x][y] >= alt_foothill) {
// What kind of river is it? We MIGHT have a canyon for high altitude.
if (river[x][y]) {
if (temperature[x][y] <= 20) {
tiles[x][y] = MAP_GLACIER;
} else if (altitude[x][y] >= rng(60, 85)) {
tiles[x][y] = MAP_CANYON;
} else {
tiles[x][y] = MAP_BASIN;
}
// High rainfall will override the hilly altitude and create jungle/forest.
} else if (rainfall[x][y] - 55 > altitude[x][y] - alt_foothill) {
if (temperature[x][y] > altitude[x][y] + 80 - alt_foothill) {
tiles[x][y] = MAP_JUNGLE;
} else {
tiles[x][y] = MAP_FOREST;
}
// Otherwise, we're just some kind of foothills.
} else if (temperature[x][y] <= 20) {
tiles[x][y] = MAP_ICY_FOOTHILLS;
} else {
tiles[x][y] = MAP_FOOTHILLS;
}
// Next category: the ocean (or frozen ocean (aka icecap)).
} else if (altitude[x][y] <= 0) {
if (altitude[x][y] == 0 && temperature[x][y] <= 12) {
tiles[x][y] = MAP_ICECAP;
} else {
tiles[x][y] = MAP_OCEAN;
}
// Finally, all other altitudes are considered "flat."
} else {
// Rivers are glaciers if it's cold, swamps if it's very rainy, or just rivers
if (river[x][y]) {
if (temperature[x][y] <= 20) {
tiles[x][y] = MAP_GLACIER;
} else if (rainfall[x][y] >= 50) { // "50" was "55", shld we revert?
tiles[x][y] = MAP_SWAMP;
} else {
tiles[x][y] = MAP_BASIN;
}
// Cold temps are always tundra
} else if (temperature[x][y] <= 20) {
tiles[x][y] = MAP_TUNDRA;
// High rainfall is always swamp
} else if (rainfall[x][y] >= 55) {
tiles[x][y] = MAP_SWAMP;
// Medium rainfall is forest or jungle, depending on temp
} else if (rainfall[x][y] >= 45) {
if (temperature[x][y] >= 75) {
tiles[x][y] = MAP_JUNGLE;
} else {
tiles[x][y] = MAP_FOREST;
}
// Medium-low rainfall is plains
} else if (rainfall[x][y] >= 30) {
tiles[x][y] = MAP_PLAINS;
// Low rainfall is wasteland
} else if (rainfall[x][y] >= 12) {
tiles[x][y] = MAP_WASTELAND;
// Very low rainfall is wasteland if cold, desert if warm
} else if (temperature[x][y] <= 32) {
tiles[x][y] = MAP_WASTELAND;
} else {
tiles[x][y] = MAP_DESERT;
}
}
}
}
// ... aaaand set up crops/minerals.
// Crops
for (int i = 1; i < CROP_MAX; i++) {
Crop crop = Crop(i);
Crop_datum* crop_dat = Crop_data[crop];
int min_radius = 5 + (crop_dat->percentage / 6);
int max_radius = 10 + (crop_dat->percentage / 2);
int avg_radius = (min_radius + max_radius) / 2;
int avg_size = avg_radius * avg_radius;
// Calculate the total number of blobs of the given size that'd fit in the world
int total_blobs = size * size;
total_blobs /= avg_size;
if (crop_dat->percentage < 20) {
total_blobs *= .6;
} else if (crop_dat->percentage > 90) {
total_blobs *= 1.2;
}
// Place an appropriate percentage of the total blobs
int num_blobs = total_blobs * crop_dat->percentage;
num_blobs /= 100;
if (crop_dat->percentage >= 90) {
max_radius += 8;
}
// Now place the blobs.
for (int n = 0; n < num_blobs; n++) {
int percent = (100 * n) / num_blobs;
if (percent >= 10 && percent % 5 == 0) {
popup_nowait("\
Generating World...\n\
Crop %d / %d (%s) \n\
Placing %d blobs [%d%%%%%%%%]",
i, CROP_MAX - 1, Crop_data[crop]->name.c_str(),
num_blobs, (100 * n) / num_blobs);
}
int radius = rng(min_radius, max_radius);
Point p;
int tries = 0;
do {
tries++;
p = Point( rng(0, size - 1), rng(0, size - 1) );
} while (tries < 20 && !tile_okay_for_crop(p, crop));
add_crop(p, crop, radius);
}
}
// Exact same thing, but for minerals.
for (int i = 1; i < MINERAL_MAX; i++) {
Mineral mineral = Mineral(i);
Mineral_datum* mineral_dat = Mineral_data[mineral];
int min_radius = 5 + (mineral_dat->percentage / 6);
int max_radius = 10 + (mineral_dat->percentage / 2);
int avg_radius = (min_radius + max_radius) / 2;
int avg_size = avg_radius * avg_radius;
// Calculate the total number of blobs of the given size that'd fit in the world
int total_blobs = size * size;
total_blobs /= avg_size * 1.2;
if (mineral_dat->percentage < 20) {
total_blobs *= .6;
} else if (mineral_dat->percentage > 90) {
total_blobs *= 1.2;
}
// Place an appropriate percentage of the total blobs
int num_blobs = total_blobs * mineral_dat->percentage;
num_blobs /= 100;
if (mineral_dat->percentage >= 90) {
max_radius += 8;
}
// Now place the blobs.
for (int n = 0; n < num_blobs; n++) {
int percent = (100 * n) / num_blobs;
if (percent >= 10 && percent % 5 == 0) {
popup_nowait("\
Generating World...\n\
Minerals: %d / %d (%s) \n\
Placing %d blobs [%d%%%%%%%%]",
i, MINERAL_MAX - 1, Mineral_data[mineral]->name.c_str(),
num_blobs, (100 * n) / num_blobs);
}
int radius = rng(min_radius, max_radius);
Point p( rng(0, size - 1), rng(0, size - 1) );
add_mineral(p, mineral, radius);
}
}
// Slightly different for animals, but still very similar
for (int i = 1; i < ANIMAL_MAX; i++) {
Animal animal = Animal(i);
Animal_datum* animal_dat = Animal_data[animal];
int num_blobs = 5 + animal_dat->percentage / 2;
// We want those blobs to cover <animal_dat->percentage> percent of the world.
if (num_blobs < 2) {
num_blobs = 2;
}
// So figure out a size that will do that.
int needed_coverage = (animal_dat->percentage * size *
size) / 100;
int needed_area = needed_coverage / num_blobs;
int needed_radius = sqrt( double(needed_area) );
int min_radius = needed_radius * 0.6;
int max_radius = needed_radius * 1.4;
// Now place the blobs.
for (int n = 0; n < num_blobs; n++) {
int percent = (100 * n) / num_blobs;
if (percent >= 10 && percent % 5 == 0) {
popup_nowait("\
Generating World...\n\
Animal %d / %d (%s) \n\
Placing %d blobs [%d%%%%%%%%]",
i, ANIMAL_MAX - 1, Animal_data[animal]->name.c_str(),
num_blobs, (100 * n) / num_blobs);
}
int radius = rng(min_radius, max_radius);
Point p;
int tries = 0;
do {
tries++;
p = Point( rng(0, size - 1), rng(0, size - 1) );
} while (tries < 20 && !tile_okay_for_animal(p, animal));
add_animal(p, animal, radius);
}
}
// Fix clustered rivers
/*
std::vector<Point> swamps;
for (int x = 0; x < size - 2; x++) {
for (int y = 0; y < size - 2; y++) {
int count = 0;
for (int rx = x; rx <= x + 10; rx++) {
for (int ry = y; ry <= y + 10; ry++) {
if (tiles[rx][ry] == MAP_BASIN) {
count++;
}
}
}
if (count >= 35) {
for (int rx = x; rx <= x + 10; rx++) {
for (int ry = y; ry <= y + 10; ry++) {
if (tiles[rx][ry] == MAP_BASIN || one_in(6)) {
swamps.push_back( Point(rx, ry) );
}
}
}
}
}
}
for (int i = 0; i < swamps.size(); i++) {
tiles[swamps[i].x][swamps[i].y] = MAP_SWAMP;
}
*/
// Finally, generate our road map and travel maps.
update_road_map();
update_travel_map(); // By not passing a parameter, we update ALL races' maps
// Set the name.
/*
Window w_name(24, 9, 32, 6);
cuss::interface i_name;
if (!i_name.load_from_file("cuss/set_world_name.cuss")) {
set_random_name();
debugmsg("Name set to '%s'.", name.c_str());
return false;
}
i_name.select("entry_name");
i_name.ref_data("entry_name", &name);
bool done = false;
while (!done) {
i_name.draw(&w_name);
w_name.refresh();
long ch = input();
if (ch == '!') {
set_random_name();
} else if (ch == '\n') {
done = true;
} else {
i_name.handle_keypress(ch);
}
}
*/
return true;
}
bool World_map::set_size(World_size Size)
{
if (size <= 0) {
debugmsg("World_map::set_size(%d) called!", Size);
return false;
}
size = world_size_size(Size);
// Prep all our data vectors.
std::vector<Map_type> tmp_map_type;
std::vector<City*> tmp_ptr;
std::vector<bool> tmp_bool;
std::vector<int> tmp_int;
for (int i = 0; i < size; i++) {
tmp_map_type.push_back( MAP_NULL );
tmp_ptr.push_back ( NULL );
tmp_bool.push_back ( false );
tmp_int.push_back ( 0 );
}
tiles.clear();
city.clear();
river.clear();
road.clear();
altitude.clear();
rainfall.clear();
temperature.clear();
continent_id.clear();
kingdom_id.clear();
crops.clear();
minerals.clear();
animals.clear();
for (int i = 0; i < size; i++) {
tiles.push_back ( tmp_map_type );
city.push_back ( tmp_ptr );
river.push_back ( tmp_bool );
road.push_back ( tmp_bool );
altitude.push_back ( tmp_int );
rainfall.push_back ( tmp_int );
temperature.push_back ( tmp_int );
continent_id.push_back ( tmp_int );
kingdom_id.push_back ( tmp_int );
crops.push_back ( tmp_int );
minerals.push_back ( tmp_int );
animals.push_back ( tmp_int );
}
return true;
}
bool World_map::save_to_file(std::string filename)
{
if (filename.empty()) {
return false;
}
std::ofstream fout;
fout.open(filename.c_str());
if (!fout.is_open()) {
return false;
}
fout << name << std::endl;
fout << size << " " << continents.size() << " ";
for (int i = 0; i < continents.size(); i++) {
fout << continents[i].x << " " << continents[i].y << " ";
}
fout << std::endl;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
fout << tiles [x][y] << " " <<
continent_id[x][y] << " " << // Not sure if we really need this
kingdom_id [x][y] << " " <<
road [x][y] << " " <<
crops [x][y] << " " <<
minerals [x][y] << " " <<
animals [x][y] << " ";
}
fout << std::endl;
}
fout << road_map.save_data() << std::endl;
// Start at 1 cause we don't do RACE_NULL
for (int i = 1; i < RACE_MAX; i++) {
fout << travel_map[ Race(i) ].save_data() << std::endl;
}
fout.close();
return true;
}
bool World_map::load_from_file(std::string filename)
{
if (filename.empty()) {
debugmsg("World_map attempted to load an empty filename.");
return false;
}
std::ifstream fin;
fin.open(filename.c_str());
if (!fin.is_open()) {
debugmsg("World_map couldn't open '%s' for loading.", filename.c_str());
return false;
}
std::getline(fin, name);
int tmp_size, num_continents;
fin >> tmp_size >> num_continents;
set_size( World_size(tmp_size) );
for (int i = 0; i < num_continents; i++) {
Point p;
fin >> p.x >> p.y;
continents.push_back(p);
}
int tmp_map_type;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
bool tmp_road;
fin >> tmp_map_type >>
continent_id[x][y] >>
kingdom_id [x][y] >>
tmp_road >>
crops [x][y] >>
minerals [x][y] >>
animals [x][y];
tiles[x][y] = Map_type(tmp_map_type);
road[x][y] = tmp_road;
}
}
if (!road_map.load_data(fin)) {
debugmsg("World_map failed to load road_map.");
return false;
}
// Start at 1 cause we don't do RACE_NULL
for (int i = 1; i < RACE_MAX; i++) {
Generic_map tmpmap;
if (!tmpmap.load_data(fin)) {
debugmsg("World_map failed to load travel_map for %s.",
Race_data[i]->name.c_str());
return false;
}
travel_map[Race(i)] = tmpmap;
}
fin.close();
return true;
}
void World_map::add_continent(Point origin, int height, int step, int id)
{
std::vector<Point> active;
active.push_back(origin);
altitude [origin.x][origin.y] = height;
continent_id[origin.x][origin.y] = id;
/* Continent-building algorithm:
* 1) Add the origin point to our active points list.
* 2) Pick any point from the active points list, and remove it from the list.
* 3) For all adjacent (non-diagonal) points, if it's ocean (altitude <= 0)
* copy our current point's altitude, then reduce it by some amount.
* 4) If the reduction doesn't put us <= 0, add that point to the active points
* list, and set its continent_id to this continent's id (if we've attached
* to an existing continent, note that in the joined_continents vector).
*/
while (!active.empty()) {
std::vector<Point> new_points;
while (!active.empty()) {
int index = rng(0, active.size() - 1);
Point p = active[index];
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 < size && y > 0 && y < size && altitude[x][y] <= 0) {
altitude[x][y] = altitude[p.x][p.y];
// Find the distance to the edge of the map.
int dist_to_edge = (x < y ? x : y);
if (size - 1 - x < dist_to_edge) {
dist_to_edge = size - 1 - x;
}
if (size - 1 - y < dist_to_edge) {
dist_to_edge = size - 1 - y;
}
// Slope off quickly if we're close to the edge of the map.
if (dist_to_edge < 20) {
altitude[x][y] -= rng(0, 2 * (20 - dist_to_edge));
// Occasionally slope off very quickly.
} else if (one_in(30)) {
altitude[x][y] -= rng(0, 100);
// Almost always slope off steadily; 1 time in 10 we don't slope off at all!
} else if (!one_in(10)) {
altitude[x][y] -= rng(0, step);
}
if (altitude[x][y] > 0) {
new_points.push_back(Point(x, y));
if (continent_id[x][y] != -1) {
// Joined continents!
int other_id = continent_id[x][y];
if (joined_continents.count(id) == 0) {
std::vector<int> tmp;
tmp.push_back(other_id);
joined_continents[id] = tmp;
} else {
// Check if we've already noted this
bool found = false;
for (int i = 0; !found && i<joined_continents[id].size(); i++) {
if (joined_continents[id][i] == other_id) {
found = true;
}
}
if (!found) {
joined_continents[id].push_back(other_id);
if (joined_continents.count(other_id) == 0) {
std::vector<int> tmp;
tmp.push_back(id);
joined_continents[other_id] = tmp;
} else {
joined_continents[other_id].push_back(id);
}
}
}
} else { // if (continent_id[x][y] != -1)
continent_id[x][y] = id;
}
} else { // if (altitude[x][y] > 0)
altitude[x][y] = 0; // "False" ocean
}
} // if (inbounds & altitude <= 0)
} // for (int i = 0; i < 4; i++)
active.erase(active.begin() + index);
} // while (!active.empty())
active = new_points;
} // while (!active.empty())
}
void World_map::add_river(Point origin)
{
if (OOB(origin)) {
return;
}