-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1814 lines (1654 loc) · 48.7 KB
/
main.c
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <conio.h>
#endif
#include <time.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <pthread.h>
// Utility macros
#define CHECK_ERROR(test, message) \
do { \
if((test)) { \
fprintf(stderr, "%s\n", (message)); \
exit(1); \
} \
} while(0)
// Wave Function Collapse
// 0 Empty
// 1 Trees
// 2 Bushes
// 3 Grass
// 4 Sand
// 5 Lake
// 6 Water
// 7 Stone
#define biomeSize 16
#define landmassSize 16
#define PI 3.14159265359
#define tectonicPlateBit 1
#define tectonicLandmassBit 2
#define landmassBit 4
#define polygonIslandsBit 8
#define biomeBit 16
#define biomeFinderBit 32
#define finalMapRender 64
#define nothingYet 128
#define numberOfOldRandomValues 20
struct biomeStruct {
unsigned char biomeID;
unsigned char biomeCenterOffsetX;
unsigned char biomeCenterOffsetY;
};
typedef struct biomeStruct biomeInfo;
struct color {
unsigned char red;
unsigned char green;
unsigned char blue;
};
typedef struct color color;
struct position {
int x,y;
};
typedef struct position position;
position* tectonicPlatesOrigin;
int* tectonicPlatesSize;
unsigned char** map;
int** heightMap;
biomeInfo** biomeMap;
unsigned char** landmassMap;
color** finalMap;
char textMode = 0;
char visual = tectonicPlateBit | tectonicLandmassBit | polygonIslandsBit |biomeBit | finalMapRender;
unsigned char r,g,b = 0;
int tectonicPlates = 0;
int mapSizeX, mapSizeY;
int maximumVerticies;
int initialSeed;
char animate = 0;
int heighestHeight = 0;
int deepestDepth = 0;
double lightAngle = 3.5;
// Export Options
char bmpMode = 0;
char heightmapExport = 0;
char albedoExport = 0;
float biomeMapSizeX, biomeMapSizeY = 0;
// Threading
int numberOfThreads = 16;
pthread_t *tids = NULL;
int *threadProgress = NULL;
int finishedThreads = 0;
// enums
typedef enum {
emptylandmass,
ocean,
islands,
archipelago,
continent,
mainland,
numlandmass,
} landmass;
// Biomes
typedef enum {
emptybiome,
mountains,
desert,
beach,
forest,
grasslands,
tundra,
numBiomes,
} biome;
typedef enum {
emptyTile,
treeTile,
bushTile,
grassTile,
oceanTile,
sandTile,
riverTile,
stoneTile,
snowTile,
iceTile,
errorTile,
numTiles,
} tilename;
// NOTE: THIS ONLY ACCOUNT FOR A SINGLE WRAP AROUND, NOT MULTIPLE!!!!
int getWrappedAround(int location, int maxValue) {
if (location < 0) {
location += maxValue;
} else if (location >= maxValue) {
location = location%maxValue;
}
return location;//%maxValue;
}
int getWrappedAroundAlt(int location, int maxValue) {
if (location < 0) {
location = location*-1;
} else if (location >= maxValue) {
location = location%maxValue;
}
return location;//%maxValue;
}
// Get a random Tile ID
int getRandomTileID() {
return rand() % numTiles;
}
// Get a random Biome ID
int getRandomBiomeID() {
return rand() % numBiomes;
}
// Get a random landmass ID
int getRandomlandmassID() {
return rand() % numlandmass;
}
//" "Empty can be anything
// T Trees can be next to bushes and Trees
// m Bushes can be next to grass and Trees
// , Grass can be next to bushes and Grass
// # Sand can be next to Grass
// ~ Water can be next to Sand and Water
int x,y = 0;
int i = 0;
char id = emptyTile;
char surrounding[4] = {emptyTile,emptyTile,emptyTile,emptyTile};
int invalidTile = emptyTile;
int previousRandomValues[numberOfOldRandomValues] = { 0 };
SDL_Renderer *renderer;
int updateProgressBar(int progress, int mode) {
// Adjust
progress+=1;
// Fake event to keep SDL from freezing
SDL_Event event;
SDL_PollEvent(&event);
// Draw outlined
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect outlineRect = {mapSizeX/2-53,mapSizeY/2-13,106,26};
SDL_RenderFillRect(renderer, &outlineRect);
// Draw bg
SDL_SetRenderDrawColor(renderer, 64, 64, 64, 255);
SDL_Rect bgRect = {mapSizeX/2-50,mapSizeY/2-10,100,20};
SDL_RenderFillRect(renderer, &bgRect);
// Get color
switch(mode) {
case 0: // Tectonic Plates
SDL_SetRenderDrawColor(renderer, 255, 66, 66, 255);
break;
case 1: // Tectonic Landmass
SDL_SetRenderDrawColor(renderer, 255, 141, 83, 255);
break;
case 2: // Landmass
SDL_SetRenderDrawColor(renderer, 255, 216, 100, 255);
break;
case 3: // Biome
SDL_SetRenderDrawColor(renderer, 211, 236, 90, 255);
break;
case 4: // Blended Biomes
SDL_SetRenderDrawColor(renderer, 167, 255, 80, 255);
break;
default:
SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
break;
}
// Render Progress
SDL_Rect barRect = {mapSizeX/2-50,mapSizeY/2-10,progress,20};
SDL_RenderFillRect(renderer, &barRect);
SDL_RenderPresent(renderer);
return 0;
}
int find_max_along_line(int** arr, int m, int n, int x1, int y1, int x2, int y2) {
int dx = x2 - x1, dy = y2 - y1;
#if defined(_WIN32)
int max_value = INT_MIN;
#else
int max_value = -999999999;
#endif
for (float t = 0; t <= 1; t += 0.5) { // Adjust step size based on desired precision
int x = getWrappedAround((int)round(x1 + t * dx),mapSizeX);
int y = getWrappedAround((int)round(y1 + t * dy),mapSizeY);
// Check if x and y are within array bounds and update max_value if necessary
if (x >= 0 && x < m && y >= 0 && y < n && arr[x][y] > max_value) {
max_value = arr[x][y];
}
}
return max_value;
}
int getCoordinatesRelativeToCenter(double angle, double distance, double center_x, double center_y, char xOrY) {
double x_double = center_x + distance * cos(angle);
double y_double = center_y + distance * sin(angle);
// Round coordinates to nearest integer
if (xOrY) {
return (int)round(x_double);
} else {
return (int)round(y_double);
}
}
float getAliasedShade(int x, int y, int heightAtCoordinate, unsigned char currentTile) {
float shade = 0.0f;
float stepSize = 0.2;
int numberOfSteps = 0;
for (float yIntern = -0.5; yIntern < 0.5; yIntern += stepSize) {
for (float xIntern = -0.5; xIntern < 0.5; xIntern += stepSize) {
int distantX = getCoordinatesRelativeToCenter(lightAngle,5.0,(double)x+xIntern,(double)y+yIntern, 1);
int distantY = getCoordinatesRelativeToCenter(lightAngle,5.0,(double)x+xIntern,(double)y+yIntern, 0);
int maxAlongLine = find_max_along_line(heightMap, mapSizeX, mapSizeY, x, y, distantX, distantY);
if ((maxAlongLine > heightAtCoordinate)) {
float untilPeak = ((float)heightAtCoordinate)/((float)255.0f);
float heightDifference = ((float)heightAtCoordinate)/((float)maxAlongLine);
shade += (1.0f-untilPeak)*heightDifference;
} else {
shade += 1.0f;
}
numberOfSteps++;
}
}
shade /= (float)numberOfSteps;
if (currentTile == oceanTile) {
if (heightAtCoordinate < 128) {
shade *= ((float)heightAtCoordinate)/255.0f;
}
}
return shade;
}
// Render a tile
int printTile(int x, int y) {
int currentTile = map[x][y];
switch(currentTile) {
case emptyTile: // Empty
r = 0;
g = 0;
b = 0;
break;
case treeTile: // Tree
r = 17;
g = 95;
b = 66;
break;
case bushTile: // Bush
r = 94;
g = 187;
b = 32;
break;
case grassTile: // Grass
r = 182;
g = 236;
b = 101;
break;
case sandTile: // Sand
r = 250;
g = 222;
b = 168;
break;
case riverTile: // River Water
r = 160;
g = 199;
b = 244;
break;
case oceanTile: // Ocean Water
r = 47;
g = 112;
b = 196;
break;
case stoneTile: // Stone
r = 114;
g = 121;
b = 130;
break;
case snowTile: // Snow
r = 223;
g = 248;
b = 255;
break;
case iceTile: // Ice
r = 179;
g = 214;
b = 241;
break;
default: // Error
r = 255;
g = 0;
b = 255;
break;
};
// Shade
if (!albedoExport) {
int heightAtCoordinate = heightMap[x][y];
// TODO: Jitter this to smooth pixels
float shade = getAliasedShade(x, y, heightAtCoordinate, currentTile);
//float height = (((float)heightMap[x][y])/255.0f);
float finalR = (float)r/255.0f;
float finalG = (float)g/255.0f;
float finalB = (float)b/255.0f;
finalR *= shade;
finalG *= shade;
finalB *= shade;
r = ((int)(finalR*255.0f))&0xFF;
g = ((int)(finalG*255.0f))&0xFF;
b = ((int)(finalB*255.0f))&0xFF;
}
if (heightmapExport) {
r = heightMap[x][y];
g = heightMap[x][y];
b = heightMap[x][y];
}
if (!bmpMode) {
finalMap[x][y].red = r;
finalMap[x][y].green = g;
finalMap[x][y].blue = b;
}
return 0;
}
int normalizeHeightmap() {
int heightmax = 0;
int depthmax = 0;
for (int y = 0; y < mapSizeY; y++) {
for (int x = 0; x < mapSizeX; x++) {
int height = heightMap[x][y];
if (height > heightmax) {
heightmax = height;
}
if (height < depthmax) {
depthmax = height;
}
}
}
for (int y = 0; y < mapSizeY; y++) {
for (int x = 0; x < mapSizeX; x++) {
int height = heightMap[x][y];
float relativeHeight = 0;
if (height >= 0) {
relativeHeight = (float)height/(float)heightmax;
} else {
relativeHeight = ((float)height/(float)depthmax)*-1;
}
relativeHeight = (relativeHeight/2)+0.5f;
heightMap[x][y] = (int)(relativeHeight*255.0f);
SDL_SetRenderDrawColor(renderer, heightMap[x][y],heightMap[x][y],heightMap[x][y],255);
SDL_RenderDrawPoint(renderer, x, y);
}
}
SDL_RenderPresent(renderer);
}
int addNoiseToHeightmap(int noiseIntensity) {
for (int y = 0; y < mapSizeY; y++) {
for (int x = 0; x < mapSizeX; x++) {
heightMap[x][y] += (rand()%noiseIntensity)-(noiseIntensity/2);
if (heightMap[x][y] > heighestHeight) {
heighestHeight = heightMap[x][y];
}
if (heightMap[x][y] < deepestDepth) {
deepestDepth = heightMap[x][y];
}
}
}
}
// This'll be a lazy Box blur
int blurHeightmap(int blurRadius) {
int** blurredHeightMap;
blurredHeightMap = (int**)malloc(mapSizeX * sizeof(int*));
for (int i = 0; i < mapSizeX; i++) {
blurredHeightMap[i] = (int*)malloc(mapSizeY * sizeof(int));
}
// Iterate over every pixel
for (int y = 0; y < mapSizeY; y++) {
for (int x = 0; x < mapSizeX; x++) {
// Internal iteration
int total = 0;
int i = 0;
for (int a = blurRadius*-1; a <= blurRadius; a++) {
for (int b = blurRadius*-1; b <= blurRadius; b++) {
int internalX = getWrappedAround(x+a,mapSizeX);
int internalY = getWrappedAround(y+b,mapSizeY);
//printf("%d,%d\n", internalX, internalY);
total += heightMap[internalX][internalY];
//printf("%d: %d,%d: %d\n", i, a,b,total);
i++;
}
}
total = total/i;
/*
if (total > heighestHeight) {
total = heighestHeight;
}
if (total < deepestDepth) {
total = deepestDepth;
}*/
blurredHeightMap[x][y] = total;
SDL_SetRenderDrawColor(renderer, total,total,total,255);
SDL_RenderDrawPoint(renderer, x, y);
}
//if (y%biomeSize) {
//}
}
SDL_RenderPresent(renderer);
// Copy to main heightmap
for (int y = 0; y < mapSizeY; y++) {
for (int x = 0; x < mapSizeX; x++) {
heightMap[x][y] = blurredHeightMap[x][y];
}
}
free(blurredHeightMap);
return 0;
}
void *printSectionOfMap(void *vargp) {
int index = (int)vargp;
int minSegment = mapSizeX/numberOfThreads;
int start = index*minSegment;
int end = index*minSegment+minSegment-1;
//printf("%d: %d -> %d\n", index, start, end);
for (x = start; x < end; x++) {
for (y = 0; y < mapSizeY; y++) {
printTile(x,y);
//printf("%d: %d,%d\n", index, x,y);
}
}
finishedThreads++;
}
// Render the map
int printMap() {
// Clear screen
if (textMode) {
for (y = 0; y < mapSizeY; y++) {
for (x = 0; x < mapSizeX; x++) {
printTile(x,y);
printTile(x,y);
}
printf("\n");
}
printf("\x1b[0m");
} else {
for (x = 0; x < mapSizeX; x++) {
for (y = 0; y < mapSizeY; y++) {
printTile(x,y);
//printf("%d: %d,%d\n", index, x,y);
}
}
/*
tids = (pthread_t *)malloc(numberOfThreads * sizeof(pthread_t));
if (tids == NULL) {
// handle memory allocation failure
printf("Could not allocate memory to threads!\n");
return 1;
}
// Create Threads
finishedThreads = 0;
for (int i = 0; i < numberOfThreads; i++) {
int ret = pthread_create(&tids[i], NULL, printSectionOfMap, (void *)i);
if (ret != 0) {
printf("Thread %d failed to create! Error code %d\n", i, ret);
return 1;
}
}
// Supervise
//while (finishedThreads < numberOfThreads) {
//printf("%d/%d\n", finishedThreads, numberOfThreads);
//}
// Rejoin them
for (int i = 0; i < numberOfThreads; i++) {
pthread_join(tids[i], NULL);
}
free(tids);*/
}
}
// Place a tile on the map
void placeTile(int x, int y, int tile) {
x = getWrappedAround(x,mapSizeX);
y = getWrappedAround(y,mapSizeY);
map[x][y] = tile;
}
void zeroHeightMapIfLessThanZero(int x, int y) {
x = getWrappedAround(x,mapSizeX);
y = getWrappedAround(y,mapSizeY);
if (heightMap[x][y] < 0) {
heightMap[x][y] = 0;
}
}
void increaseHeightMap(int x, int y) {
x = getWrappedAround(x,mapSizeX);
y = getWrappedAround(y,mapSizeY);
heightMap[x][y]+=1;
if (heightMap[x][y] > heighestHeight) {
heighestHeight = heightMap[x][y];
}
}
// Place a tile on the map
int getTile(int x, int y) {
x = getWrappedAround(x,mapSizeX);
y = getWrappedAround(y,mapSizeY);
return map[x][y];
}
// Calculate Distance between two points as integer
int getIntDistance(int x1, int y1, int x2, int y2) {
return (int) sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) );
}
int wrapped_distance(int x1, int y1, int x2, int y2, int width, int height) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
// Wrap the distances around the torus
int wrapped_dx = (dx < width - dx) ? dx : width - dx;
int wrapped_dy = (dy < height - dy) ? dy : height - dy;
// Calculate the wrapped distance
int wrapped_dist = sqrt(wrapped_dx * wrapped_dx + wrapped_dy * wrapped_dy);
return wrapped_dist;
}
// Random with a maximum
// Max included
int getRandomLimited(int max) {
max = max+1;
return rand() % max;
}
// Random with a minimum and maximum
int getRandomLimitedMinMax(int min, int max) {
max = max + 1; // Include the upper bound (max) in the range
int result = (rand() % (max - min)) + min;
return result;
}
// Smoothed Random Generator with a maximum value
// Used to make random values less... random?
float getSmoothedRandomLimited(int max, int smoothSteps) {
float smoothedValue = 0.0f;
for (int i = 1; i < numberOfOldRandomValues-1; i++) {
//printf("%d -> %d\n",previousRandomValues[i],previousRandomValues[i+1]);
previousRandomValues[i] = previousRandomValues[i+1];
}
previousRandomValues[numberOfOldRandomValues] = rand() % max;
for (int i = numberOfOldRandomValues; i > numberOfOldRandomValues-smoothSteps; i--) {
smoothedValue+=(float)previousRandomValues[i];
}
//printf("%f\n",smoothedValue/(float)smoothSteps);
return smoothedValue/(float)smoothSteps;
}
// Both of these are broken!
// Remove surrounding array!
// Checks if another tile is surrounding the current tile at any other side
int isNeighboring(int tile) {
if ((surrounding[0] == tile) || (surrounding[1] == tile) || (surrounding[2] == tile) || (surrounding[3] == tile)) {
return 1;
} else {
return 0;
}
}
// Checks to see if a tile is surrounded by another type of tile
int surroundedBy(int tile) {
if ((surrounding[0] == tile) && (surrounding[1] == tile) && (surrounding[2] == tile) && (surrounding[3] == tile)) {
return 1;
} else {
return 0;
}
}
// The random island
// aka the algorithm I used to shape islands previously
// leaving it here in case I ever find a use for it!
void randomIsland(int x1, int y1, float islandSize) {
islandSize *= (float)(getRandomLimited(10))/7.0f;
for (y = 0; y < mapSizeY; y++) {
for (x = 0; x < mapSizeX; x++) {
int x2 = x;
int y2 = y;
int distanceToIslandCenter = getIntDistance(x1,y1,x2,y2);
// Generate Water Circle
if (distanceToIslandCenter < islandSize*(getRandomLimited(5))) {
map[x2][y2] = emptyTile;
SDL_RenderDrawPoint(renderer, x, y);
}
}
}
}
// Line drawing
// Just using Bresenham's line algorithm
void plotLineHigh(int x0, int y0, int x1, int y1, int tile) {
int dx = x1 - x0;
int dy = y1 - y0;
int xi = 1;
if (dx < 0) {
xi = -1;
dx = -dx;
}
int D = (2 * dx) - dy;
int x = x0;
for (int y = y0; y <= y1; y++) {
placeTile(x,y,tile);
if (D > 0) {
x = x + xi;
D = D + (2 * (dx - dy));
} else {
D = D + (2 * dx);
}
}
}
void plotLineLow(int x0, int y0, int x1, int y1, int tile) {
int dx = x1 - x0;
int dy = y1 - y0;
int yi = 1;
if (dy < 0) {
yi = -1;
dy = -dy;
}
int D = (2 * dy) - dx;
int y = y0;
for (int x = x0; x <= x1; x++) {
placeTile(x,y,tile);
if (D > 0) {
y = y + yi;
D = D + (2 * (dy - dx));
} else {
D = D + (2 * dy);
}
}
}
void plotLine(int x0, int y0, int x1, int y1, int tile) {
if (abs(y1 - y0) < abs(x1 - x0)) {
if (x0 > x1) {
plotLineLow(x1, y1, x0, y0, tile);
} else {
plotLineLow(x0, y0, x1, y1, tile);
}
} else {
if (y0 > y1) {
plotLineHigh(x1, y1, x0, y0, tile);
} else {
plotLineHigh(x0, y0, x1, y1, tile);
}
}
}
/*
The Polygon island!
These create points around a circle,
then randomly push that out to create islands of various sizes
*/
void polygonIsland(int x1, int y1, float islandSize, float smoothness) {
// Set number of points along circle
float* polygonX = (float*)malloc(maximumVerticies * sizeof(float));;
float* polygonY = (float*)malloc(maximumVerticies * sizeof(float));;
float angleIncrement = 2 * M_PI / maximumVerticies; // Calculate the angle increment
float currentAngle = (float)getRandomLimited(360)/360.0f; // Starting angle
// Set random displacements of points
for (int pointID = 0; pointID < maximumVerticies; pointID++) {
//printf("%d/%d\n", pointID, maximumVerticies);
// Interpolate continent outline between points
float x2 = cos(currentAngle) * islandSize * getSmoothedRandomLimited(5, smoothness) + (float)x1;
float y2 = sin(currentAngle) * islandSize * getSmoothedRandomLimited(5, smoothness) + (float)y1;
polygonX[pointID] = x2;
polygonY[pointID] = y2;
currentAngle += angleIncrement; // Increment the angle for the next point
}
// Draw Polygon on map
if (visual & polygonIslandsBit) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for (int i = 0; i < maximumVerticies - 1; ++i) {
SDL_RenderDrawLine(renderer, polygonX[i], polygonY[i], polygonX[i + 1], polygonY[i + 1]);
}
}
// Fill out tiles inside polygon
// Note: Probably better to do later once all islands have been placed!!!
for (int mapY = -mapSizeY; mapY < mapSizeY+mapSizeY; mapY++) {
for (int mapX = -mapSizeX; mapX < mapSizeX+mapSizeX; mapX++) {
int i, j;
float pointX = (float)mapX;
float pointY = (float)mapY;
char isInside = 0;
for (i = 0, j = maximumVerticies - 1; i < maximumVerticies; j = i++) {
if (((polygonY[i] >= pointY && polygonY[j] < pointY) || (polygonY[j] >= pointY && polygonY[i] < pointY)) &&
(pointX < (polygonX[j] - polygonX[i]) * (pointY - polygonY[i]) / (polygonY[j] - polygonY[i]) + polygonX[i])) {
isInside = !isInside;
}
}
if (isInside) {
placeTile(mapX, mapY, emptyTile);
zeroHeightMapIfLessThanZero(mapX, mapY);
increaseHeightMap(mapX, mapY);
}
}
}
// Generate rivers riverTile
// Figure out how to draw a nice line
// FIX RIVERS!!!
/*
int numberOfRivers = getRandomLimited(islandSize/2);
int numberOfRiverBends = 10;
float lineX[10];
float lineY[10];
for (int i = 0; i < numberOfRivers; i++) {
// Choose two nodes
int pointA = getRandomLimited(maximumVerticies);
int pointB = getRandomLimited(maximumVerticies);
// Get their positions
float x0 = (float)polygonX[pointA];
float y0 = (float)polygonY[pointA];
float x1 = (float)polygonX[pointB];
float y1 = (float)polygonY[pointB];
// Calculate the size of each step
float stepX = (x1-x0)/numberOfRiverBends;
float stepY = (y1-y0)/numberOfRiverBends;
// Store the resulting line and randomize it's positions slightly
float prevStepX = x0;
float prevStepY = y0;
for (int i = 0; i < numberOfRiverBends; i++) {
prevStepX = x0+stepX*i;
prevStepY = y0+stepY*i;
plotLine(
prevStepX,
prevStepY,
x0+stepX*(i+1)+(getRandomLimited(3)),
y0+stepY*(i+1)+(getRandomLimited(3)),
riverTile);
}
}*/
}
// Checks the current Biome Square for a certain tile, usually oceanwater
// Used to prevent deserts from spawning near water
int checkBiomeForTile(int x, int y,int tile) {
x = getWrappedAround(x*biomeSize,mapSizeX);
y = getWrappedAround(y*biomeSize,mapSizeY);
int numberOfTiles = 0;
for (int tileY = y; tileY < y+biomeSize; tileY++) {
for (int tileX = x; tileX < x+biomeSize; tileX++) {
if (map[tileX][tileY]==tile) {
numberOfTiles++;
}
}
}
return numberOfTiles;
}
// Checks if it's neighboring a biome
int isNeighboringBiome(int x, int y, int biome) {
x = getWrappedAround(x,((int)biomeMapSizeX));
y = getWrappedAround(y,((int)biomeMapSizeY));
int neighboringOnSides;
if (biomeMap[x+1][y].biomeID == biome) {
neighboringOnSides++;
}
if (biomeMap[x][y+1].biomeID == biome) {
neighboringOnSides++;
}
if (biomeMap[x-1][y].biomeID == biome) {
neighboringOnSides++;
}
if (biomeMap[x][y-1].biomeID == biome) {
neighboringOnSides++;
}
return neighboringOnSides;
}
// Check which landmass the tile belongs to
int checklandmass(int x, int y) {
x = x/landmassSize;
y = y/landmassSize;
x = getWrappedAround(x,mapSizeX/landmassSize);
y = getWrappedAround(y,mapSizeY/landmassSize);
return landmassMap[x][y];
}
// This segfaults a lot...
int getNeighboringBiomes(int x, int y, int direction) {
//printf("x: %d, y: %d, d: %d\n", x, y, direction);
switch(direction) {
case 0:
// North
y-=1;
break;
case 1:
// East
x+=1;
break;
case 2:
// South
y+=1;
break;
case 3:
// West
x-=1;
break;
default:
printf("Invalid Direction\n");
break;
}
x = getWrappedAround(x,mapSizeX/biomeSize);
y = getWrappedAround(y,mapSizeY/biomeSize);
return biomeMap[x][y].biomeID;
}
int renderBiome(int biomeX,int biomeY, int biomeFinder) {
int alpha = 128;
if (biomeFinder) {
alpha = 4;
}
switch(biomeMap[biomeX][biomeY].biomeID) {
case emptybiome:
SDL_SetRenderDrawColor(renderer, 25, 25, 25, alpha);
break;
case mountains:
SDL_SetRenderDrawColor(renderer, 128, 128, 128, alpha);
break;
case desert:
SDL_SetRenderDrawColor(renderer, 255, 255, 0, alpha);
break;
case beach:
SDL_SetRenderDrawColor(renderer, 192, 255, 0, alpha);
break;
case forest:
SDL_SetRenderDrawColor(renderer, 0, 128, 0, alpha);
break;
case grasslands:
SDL_SetRenderDrawColor(renderer, 0, 255, 0, alpha);
break;
case tundra:
SDL_SetRenderDrawColor(renderer, 255, 255, 255, alpha);
break;
default:
SDL_SetRenderDrawColor(renderer, 255, 0, 255, alpha);
break;
}
if (!biomeFinder) {
SDL_Rect rect = {biomeX*biomeSize,biomeY*biomeSize,biomeSize,biomeSize};
SDL_RenderFillRect(renderer, &rect);
}
//SDL_RenderPresent(renderer);
return 0;
}
int scaleToValue(int value, int maxValue, int desiredMax) {
float goal = (float)desiredMax;
float max = (float)maxValue;
float scaler = goal/max;
return (int)((float)value*scaler);
}
// NOTE: Breaks with non-square images
// Function to create and save as BMP file
void saveBMP() {
char outputFilename[256];
char str0[32];
char str1[32];
char str2[64];
char str3[64];
sprintf(str0, "map_%d-", mapSizeX);
sprintf(str1, "%d_", mapSizeY);
sprintf(str2, "%d_", initialSeed);
sprintf(str3, "%d", time(NULL));
strcpy(outputFilename, str0);
strcat(outputFilename, str1);
strcat(outputFilename, str2);
strcat(outputFilename, str3);
if (heightmapExport) {
strcat(outputFilename, "_height");
}
if (albedoExport) {
strcat(outputFilename, "_height");
}
strcat(outputFilename, ".bmp");
FILE* file = fopen(outputFilename, "wb");
if (!file) {
printf("Error opening file\n");
return;
}
uint32_t imageSize = mapSizeX * mapSizeY * 3;
uint32_t headerSize = 54;
uint32_t fileSize = imageSize + headerSize;+
printf("Filesize: %d", fileSize);
// BMP file header
uint8_t header[54] = {
'B', 'M', // BMP signature
(uint8_t)(fileSize & 0xFF), // File size
(uint8_t)(fileSize >> 8 & 0xFF),
(uint8_t)(fileSize >> 16 & 0xFF),
(uint8_t)(fileSize >> 24 & 0xFF),
0, 0, 0, 0, // Reserved
headerSize, 0, 0, 0, // Image data offset ; 14 bytes up to this point
40, 0, 0, 0, // DIB header size
(uint8_t)(mapSizeX & 0xFF), // Image width
(uint8_t)(mapSizeX >> 8 & 0xFF),
(uint8_t)(mapSizeX >> 16 & 0xFF),
(uint8_t)(mapSizeX >> 24 & 0xFF),
(uint8_t)(mapSizeY & 0xFF), // Image height
(uint8_t)(mapSizeY >> 8 & 0xFF),
(uint8_t)(mapSizeY >> 16 & 0xFF),
(uint8_t)(mapSizeY >> 24 & 0xFF),
1, 0, // Number of color planes
24, 0, // Bits per pixel (24-bit color)
0, 0, 0, 0, // Compression method