-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerrainGen.cpp
782 lines (662 loc) · 19.9 KB
/
TerrainGen.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
#include "TerrainGen.h"
//
// FUNCTION: TerrainGen() (Constructor)
//
// PURPOSE: Creates a terrain map
//
// COMMENTS:
// This constructor performs the following jobs to create a fully functional terrain map:
//
// Allocates the required memory for terrain
// Initialize terrain heights to zero
// Initialize some terrain types to random values
// Fills in the rest of the terrain based on the random values
// Creates a Perlin Noise height map
// Uses the height map values to add heights to the terrain
// Uses an iteration of the height map generation to create the camera track
// Smooths the terrain
// Adjusts terrain types to include water and snow if necessary
//
TerrainGen::TerrainGen(Vector2 l)
{
location = l;
dX = (int)(MAX_WORLD_SIZE/pow(2.0, 2));
dZ = (int)(MAX_WORLD_SIZE/pow(2.0, 2));
//Allocate memory space for the height map
terrain = (tile **) malloc ((MAX_WORLD_SIZE) * sizeof(tile *));
if(!terrain)
{
log("Memory allocation error while making terrain\n");
exit(1);
}
for(int k = 0; k <= MAX_WORLD_SIZE; k++)
{
terrain[k] = (tile *) malloc ((MAX_WORLD_SIZE) * sizeof(tile));
if(!terrain[k])
{
log("Memory allocation error while making terrain\n");
exit(1);
}
}
//Allocate memory for camera Track data
int delta = 32;
cTrack = (double **) malloc (delta * sizeof(double *));
if(!cTrack)
{
log("Memory allocation error while making camera Track\n");
exit(1);
}
for(int k = 0; k <= delta; k++)
{
cTrack[k] = (double *) malloc (delta * sizeof(double));
if(!cTrack[k])
{
log("Memory allocation error while making camera Track\n");
exit(1);
}
}
nodeSpread = 32;
//Allocate memory for the node path list
nodes = (nodePath **) malloc (MAX_WORLD_SIZE/nodeSpread * sizeof(nodePath *));
if(!cTrack)
{
log("Memory allocation error while making camera Track\n");
exit(1);
}
for(int k = 0; k <= MAX_WORLD_SIZE/nodeSpread; k++)
{
nodes[k] = (nodePath *) malloc (MAX_WORLD_SIZE/nodeSpread * sizeof(nodePath));
if(!nodes[k])
{
log("Memory allocation error while making camera Track\n");
exit(1);
}
}
waterHeight = 3; //Default height for water to be generated
deepWater = waterHeight-3;
snowHeight = 20; //Height for snow to be generated
biome = LAKES;//getRandomAsI(BIOME_COUNT);
switch(biome)
{
//Fields with sparse trees
case PLAINS: pField = 65, pForest = 35, pDesert = 0; //Probabilities of each terrain type being generated (Must sum to 100)
persistence = .5 + getRandomAsD(8)/100;
//avgHeight = 20+getRandomAsD(20);
waterHeight = 0;
//snowHeight = 0;
featurePoints = 100;
log("Terrain type 0 was picked.\n");
break;
//Mountainous with lots of trees
case MOUNTAINS: pField = 55, pForest = 45, pDesert = 0;
persistence = .9;// + getRandomAsD(5)/100;
waterHeight = -100;
//avgHeight = 100+getRandomAsD(100);
featurePoints = 50;
log("Terrain type 1 was picked.\n");
break;
//Flat deserts with some brush
case DESERT: pField = 20, pForest = 0, pDesert = 80;
persistence = .30 + getRandomAsD(5)/100;
waterHeight = 0;
//avgHeight = 20+getRandomAsD(20);
featurePoints = 60;
log("Terrain type 2 was picked.\n");
break;
//Average mix of fields and trees with extra water
case FOREST_MIX: pField = 40, pForest = 60, pDesert = 0;
persistence = .52 + getRandomAsD(10)/100;
featurePoints = 60;
log("Terrain type 3 was picked.\n");
break;
case LAKES: pField = 85, pForest = 15, pDesert = 0; //Probabilities of each terrain type being generated (Must sum to 100)
persistence = .4 + getRandomAsD(8)/100;
//avgHeight = 20+getRandomAsD(20);
waterHeight = 1;
//snowHeight = 0;
featurePoints = 100;
log("Terrain type 4 was picked.\n");
break;
default:
log("Invalid biome "+itos(biome)+"value picked.\n");
}
sandHeight = waterHeight + 1;
log("persistence: "+dtos(persistence)+"\n");
//Initialize the terrain with a few random points and initial height values
for(int z = 0; z < MAX_WORLD_SIZE; z++)
for(int x = 0; x < MAX_WORLD_SIZE; x++)
{
terrain[x][z].x = x;
terrain[x][z].y = 0;
terrain[x][z].z = z;
if(x%dX == 0 && z%dX == 0)
terrain[x][z].type = terrainTypeDistribution();
}
//Create Terrain data
generateTerrain(dX, dZ);
//Generate terrain height
modifyTerrainHeight();
//Make the track for the camera to move along
makeCamTrack();
}
//
// FUNCTION: modifyTerrainHeight()
//
// PURPOSE: Creates the terrain by combining all the terrain generating algorithms
//
// COMMENTS:
//
void TerrainGen::modifyTerrainHeight()
{
//Initialize the Perlin Noise map data
HeightMap * pMap = new PerlinNoise(persistence, avgHeight, biome);
for(int i = 0; i <= 5; i++)
{
pMap->create();
combineHeightMapWithTerrain(pMap, i);
}
if(biome == DESERT) {
HeightMap * vMap = new VoronoiGraph(featurePoints);
vMap->create();
combineHeightMapWithTerrain(vMap, 10);
}
//Smooth the terrain heights
for(int z = 0; z < MAX_WORLD_SIZE; z++)
for(int x = 0; x < MAX_WORLD_SIZE; x++)
{
//Smooth terrain
for(int i = 5; i >= 0; i--)
smooth(x, z, i);
//Finds the lowest and the highest point in the terrain
setTerrainData(x, z);
}
//If the slope of a terrain location is over a specified amount, make that type a cliff
//Make sure snow is generated above the specified height and
//that water is generated below the specified height
//Draw shore for spaces just above water
for(int z = 0; z < MAX_WORLD_SIZE; z++)
for(int x = 0; x < MAX_WORLD_SIZE; x++)
{
//findSlope(x, z);
//Calculate sand before water
if(getTerrainHeight(x,z) < sandHeight)
terrain[x][z].type = SAND;
//Water will override the sand if it's low enough
if(terrain[x][z].y < waterHeight) {
//if(!(terrain[x][z].y < deepWater))
// terrain[x][z].y = waterHeight;
terrain[x][z].type = WATER;
}
//Snow is above a certain height
//int heightDiff = terrain[x][z].y-snowHeight;
if(terrain[x][z].y > snowHeight) {
terrain[x][z].type = SNOW;
}
else if(terrain[x][z].y > snowHeight-2) {
if(choose(getDecimalRandom(terrain[x][z].y-snowHeight+3)-1))
terrain[x][z].type = SNOW;
}
//Initialize the nodes for pathfinding
if(x%nodeSpread == 0 && z%nodeSpread == 0)
{
setCoord(nodes[x/nodeSpread][z/nodeSpread].nodeData, x, z);
if(terrain[x][z].gradient < .7 && terrain[x][z].type != 4)
nodes[x/nodeSpread][z/nodeSpread].incentive = -1;
else
nodes[x/nodeSpread][z/nodeSpread].incentive = -2;
}
Vector3 n1, n2;
//Get normal vector for the first triangle
setVector(n1, 0, terrain[x+1][z+1].y-terrain[x+1][z].y, 1);
setVector(n2, -1, terrain[x][z].y-terrain[x+1][z].y, 0);
terrain[x][z].normal1 = crossProduct(n2, n1);
normalize(terrain[x][z].normal1);
//Get normal vector for the second triangle
setVector(n1, 1, terrain[x+1][z+1].y-terrain[x][z+1].y, 0);
setVector(n2, 0, terrain[x][z].y-terrain[x][z+1].y, -1);
terrain[x][z].normal2 = crossProduct(n1, n2);
normalize(terrain[x][z].normal2);
}
}
//
// FUNCTION: generateTerrain()
//
// PURPOSE: Uses the Diamond-Square algorithm to generate the base terrain type structure
//
// COMMENTS: This randomizes the placement of terrain types like forests, plains
//
void TerrainGen::generateTerrain(int dX, int dZ)
{
for(int z = 0; z < MAX_WORLD_SIZE; z += dZ)
for(int x = 0; x < MAX_WORLD_SIZE; x += dX)
{
tile *child;
tile *parents[4];
//Current tile being referenced
parents[0] = &terrain[x][z];
double pSum = 0;
//If there is room in both directions then use four parents
if(x < MAX_WORLD_SIZE-1 && z < MAX_WORLD_SIZE-1)
{
child = &terrain[x+dX/2][z+dZ/2]; //Find child tile
parents[1] = &terrain[x+dX][z+dZ]; //Find other parent tile
parents[2] = &terrain[x+dX][z]; //Find other parent tile
parents[3] = &terrain[x][z+dZ]; //Find other parent tile
pSum += getProbabilityByType(parents[0]->type);
pSum += getProbabilityByType(parents[1]->type);
pSum += getProbabilityByType(parents[2]->type);
pSum += getProbabilityByType(parents[3]->type);
double choice = getRandomAsD(100)/100.0;
//double choice = getRandomAsD(100);
if(choice < getProbabilityByType(parents[0]->type)/pSum)
//if(choice < 25)
child->type = parents[0]->type;
else if(choice < (getProbabilityByType(parents[0]->type)+getProbabilityByType(parents[1]->type))/pSum)
child->type = parents[1]->type;
else if(choice < (getProbabilityByType(parents[0]->type)+getProbabilityByType(parents[1]->type)+getProbabilityByType(parents[2]->type))/pSum)
child->type = parents[2]->type;
else
child->type = parents[3]->type;
}
//Check if there will be a second parent in the Z direction
if(z < MAX_WORLD_SIZE-1)
{
child = &terrain[x][z+dZ/2]; //Find child tile
parents[1] = &terrain[x][z+dZ]; //Find other parent tile
if(x > 0)
parents[2] = &terrain[x-dX/2][z+dZ/2]; //Find other parent tile
else
{
Vector2 adjLoc;
setCoord(adjLoc, location.x-1, location.y);
getTileDataFromFile(*parents[2], adjLoc, MAX_WORLD_SIZE+(x-dX/2), z+dZ/2);
parents[2]->y = 0;
}
if(x < MAX_WORLD_SIZE-1)
parents[3] = &terrain[x+dX/2][z+dZ/2]; //Find other parent tile
//If #2 spot is null then move #3 to #2
if(!parents[2])
parents[2] = parents[3];
int randBound = 100;
//If #3 is null then make sure the randomizer doesn't pick the null spot
if(!parents[3])
randBound = 75;
pSum += getProbabilityByType(parents[0]->type);
pSum += getProbabilityByType(parents[1]->type);
pSum += getProbabilityByType(parents[2]->type);
pSum += getProbabilityByType(parents[3]->type);
double choice = getRandomAsD(100)/100.0;
//double choice = getRandomAsD(100);
if(choice < getProbabilityByType(parents[0]->type)/pSum)
//if(choice < 25)
child->type = parents[0]->type;
else if(choice < (getProbabilityByType(parents[0]->type)+getProbabilityByType(parents[1]->type))/pSum)
child->type = parents[1]->type;
else if(choice < (getProbabilityByType(parents[0]->type)+getProbabilityByType(parents[1]->type)+getProbabilityByType(parents[2]->type))/pSum)
child->type = parents[2]->type;
else
child->type = parents[3]->type;
}
pSum = 0;
//Check if there will be a second parent in the X direction
if(x < MAX_WORLD_SIZE-1)
{
child = &terrain[x+dX/2][z]; //Find child tile
parents[1] = &terrain[x+dX][z]; //Find other parent tile
if(z > 0)
parents[2] = &terrain[x+dX/2][z-dZ/2]; //Find other parent tile
if(z < MAX_WORLD_SIZE-1)
parents[3] = &terrain[x+dX/2][z+dZ/2]; //Find other parent tile
//If #2 spot is null then move #3 to #2
if(!parents[2])
parents[2] = parents[3];
int randBound = 100;
//If #3 is null then make sure the randomizer doesn't pick the null spot
if(!parents[3])
randBound = 75;
pSum += getProbabilityByType(parents[0]->type);
pSum += getProbabilityByType(parents[1]->type);
pSum += getProbabilityByType(parents[2]->type);
pSum += getProbabilityByType(parents[3]->type);
double choice = getRandomAsD(100)/100.0;
//double choice = getRandomAsD(100);
if(choice < getProbabilityByType(parents[0]->type)/pSum)
//if(choice < 25)
child->type = parents[0]->type;
else if(choice < (getProbabilityByType(parents[0]->type)+getProbabilityByType(parents[1]->type))/pSum)
child->type = parents[1]->type;
else if(choice < (getProbabilityByType(parents[0]->type)+getProbabilityByType(parents[1]->type)+getProbabilityByType(parents[2]->type))/pSum)
child->type = parents[2]->type;
else
child->type = parents[3]->type;
}
pSum = 0;
}
int newDX = 0, newDZ = 0;
if(dX > 1)
newDX = dX/2;
if(dZ > 1)
newDZ = dZ/2;
if(dX <= 1 && dZ <= 1)
return;
generateTerrain(newDX, newDZ);
}
//
// FUNCTION: perturbance()
//
// PURPOSE: Terrain modifying algorithm that skews the terrain formations with perlin noise
//
// COMMENTS:
//
void TerrainGen::perturbance(int variation)
{
variation *= 100;
int xMin, xMax = 0, zMin, zMax = 0;
int frequency = (int)pow(2.0, 6); //Number of points being used on each axis direction
int delta = MAX_WORLD_SIZE/frequency; //Distance between the points being used
double xVal[64], zVal[64], dZ, dX;
for(int i = 0; i < frequency; i++)
xVal[i] = (rand() % variation)/100;
for(int i = 0; i < frequency; i++)
zVal[i] = (rand() % variation)/100;
for(int z = 0; z < MAX_WORLD_SIZE; z++)
{
if(z%delta == 0)
{
zMin = zMax;
zMax++;
}
dZ = cosineInterpolate(zVal[zMin], zVal[zMax], (z%delta)/(double)delta);
for(int x = 0; x < MAX_WORLD_SIZE; x++)
{
if(x % delta == 0)
{
xMin = xMax;
xMax++;
}
dX = cosineInterpolate(xVal[xMin], xVal[xMax], (x%delta)/(double)delta);
//double dummy = dX;
//getTerrainHeight(x, z) = getTerrainHeight(x+(int)dZ, z+(int)dX);
}
xMax = 0;
}
}
//
// FUNCTION: terrainTypeDistribution()
//
// PURPOSE: Picks a random number based on the types of terrain used in the generator and the
// probability for each type to appear
//
// COMMENTS:
//
int TerrainGen::terrainTypeDistribution()
{
//Chooses a random terrain type based on the given probability distributions
//of each terrain type
int choice = rand() % 100;
if(choice <= pField)
return FIELD; //Field
if(choice <= pField + pForest)
return FOREST; //Forest
return SAND; //Desert
}
//
// FUNCTION: combineHeightMapWithTerrain()
//
// PURPOSE: Combines a height map onto the current terrain
//
// COMMENTS:
//
void TerrainGen::combineHeightMapWithTerrain(HeightMap *hMap, int iter)
{
for(int z = 0; z < MAX_WORLD_SIZE; z++)
for(int x = 0; x < MAX_WORLD_SIZE; x++)
{
//Combine only the high height values of the large features
if(biome == MOUNTAINS && iter < 3)
terrain[x][z].y += *hMap->getMap(x, z);
//Combine only the low values of the small features
if(biome == MOUNTAINS && iter == 5)
terrain[x][z].y += *hMap->getMap(x, z)/2;
if(biome != MOUNTAINS)
terrain[x][z].y += *hMap->getMap(x, z);
}
}
//
// FUNCTION: thermalErosion()
//
// PURPOSE: Uses thermal erosion algorithms to modify the terrain
//
// COMMENTS: Moves terrain around based on nearby slopes
// Currently not a complete algorithm
//
void TerrainGen::thermalErosion()
{
/*double erodeThreshold, heightDiff;
//Iterate over the map
for(int z = 0; z < MAX_WORLD_SIZE; z++)
for(int x = 0; x < MAX_WORLD_SIZE; x++)
{
//Iterate over the eight cells surrounding the current cell
for(int b = -1; b <= 1; b++)
for(int a = -1; a <= 1; a++)
{
if(a == 0 && b == 0)
continue;
heightDiff = getTerrainHeight(x, z) - getTerrainHeight(x+a, z+b);
if(abs(heightDiff) > erodeThreshold)
{
}
}
}*/
}
//
// FUNCTION: makeCamTrack()
//
// PURPOSE: Sets the height values for the camera so it floats above the terrain
//
// COMMENTS:
//
void TerrainGen::makeCamTrack()
{
double delta = (MAX_WORLD_SIZE-1)/32.0; //Distance between the inital points of the track
for(int z = 0; z*delta <= MAX_WORLD_SIZE; z++)
for(int x = 0; x*delta <= MAX_WORLD_SIZE; x++)
{
if(getTerrainType(x*delta, z*delta) == WATER)
cTrack[x][z] = waterHeight+2;
else
cTrack[x][z] = getTerrainHeight(x*delta, z*delta) + 2;
}
}
//
// FUNCTION: smooth()
//
// PURPOSE: Smooths the terrain height values by averaging the height of a tile with it's neighbor tiles
//
// COMMENTS:
//
void TerrainGen::smooth(int x, int z, int spread)
{
double corners, sides, center;
if(x <= spread-1 || z <= spread-1 || x >= MAX_WORLD_SIZE-spread || z >= MAX_WORLD_SIZE-spread){}
else
{
corners = (getTerrainHeight(x-spread, z-spread) + getTerrainHeight(x+spread, z+spread) + getTerrainHeight(x+spread, z-spread) + getTerrainHeight(x-spread, z+spread))/16;
sides = (getTerrainHeight(x-spread, z) + getTerrainHeight(x+spread, z) + getTerrainHeight(x, z-spread) + getTerrainHeight(x, z+spread))/8;
center = getTerrainHeight(x, z)/4;
terrain[x][z].y = corners + sides + center;
}
}
void TerrainGen::findSlope(int x, int z)
{
double h[4], max;
h[0] = abs(getTerrainHeight(x, z)-getTerrainHeight(x-1, z));
h[1] = abs(getTerrainHeight(x, z)-getTerrainHeight(x+1, z));
h[2] = abs(getTerrainHeight(x, z)-getTerrainHeight(x, z-1));
h[3] = abs(getTerrainHeight(x, z)-getTerrainHeight(x, z+1));
max = h[0];
for(int i = 1; i < 4; i++)
{
if(max < h[i])
max = h[i];
}
//log(dtos(max)+"\n");
terrain[x][z].gradient = max;
}
//
// FUNCTION: resetNodes()
//
// PURPOSE: Clears the node path overlay of all values and returns them to their default values
//
// COMMENTS:
//
void TerrainGen::resetNodes()
{
for(int z = 0; z < MAX_WORLD_SIZE/nodeSpread; z++)
for(int x = 0; x < MAX_WORLD_SIZE/nodeSpread; x++)
{
if(nodes[x][z].incentive != -2)
nodes[x][z].incentive = -1;
}
}
//
// FUNCTION: setTerrainData()
//
// PURPOSE: Saves stat values of the terrain after the generation is complete
//
// COMMENTS:
//
void TerrainGen::setTerrainData(int x, int z)
{
if(getTerrainHeight(x, z) < minHeight)
minHeight = getTerrainHeight(x, z);
else if(getTerrainHeight(x, z) > maxHeight)
maxHeight = getTerrainHeight(x, z);
}
void TerrainGen::getColor(int x, int z)
{
switch(terrain[x][z].type)
{
case FIELD: glColor3f(0.1f,1,0.1f); //Fields
break;
case FOREST: glColor3f(0,0.5f,0); //Forest
break;
case SAND: glColor3f(0.7f,0.7f,0); //Desert
break;
case SNOW: glColor3f(0.8f,0.8f,0.8f); //Snow
break;
case WATER: glColor3f(0,0,.6f); //Water
break;
}
}
double ** TerrainGen::getCamTrack()
{
return cTrack;
}
double TerrainGen::getTerrainHeight(int x, int z)
{
//double val = 0;
if(x < 0 || z < 0 || x > MAX_WORLD_SIZE-1 || z > MAX_WORLD_SIZE-1)
return 0;
else
return terrain[x][z].y;
}
int TerrainGen::getTerrainType(int x, int z)
{
return terrain[x][z].type;
}
double TerrainGen::getTerrainGradient(int x, int z)
{
return terrain[x][z].gradient;
}
Vector3 TerrainGen::getN1(int x, int z)
{
return terrain[x][z].normal1;
}
Vector3 TerrainGen::getN2(int x, int z)
{
return terrain[x][z].normal2;
}
//
// FUNCTION: getSpecificTerainHeight()
//
// PURPOSE: Returns the height of the terrain between the tile data points by checking for the interestion of the plane
//
// COMMENTS:
//
double TerrainGen::getSpecificTerrainHeight(double x, double z)
{
Vector3 target;
Vector3 p1, p2, p3, p4, l1, l2;
setVector(l1, x, -500, z);
setVector(l2, x, 700, z);
setVector(p1, (int)x, getTerrainHeight((int)x,(int)z), (int)z);
setVector(p2, (int)x+1, getTerrainHeight((int)x+1,(int)z), (int)z);
setVector(p3, (int)x+1, getTerrainHeight((int)x+1,(int)z+1), (int)z+1);
setVector(p4, (int)x, getTerrainHeight((int)x,(int)z+1), (int)z+1);
bool tri1 = checkLineIntersect(p2, p3, p1, l1, l2, target);
bool tri2 = checkLineIntersect(p4, p3, p1, l1, l2, target);
if(tri1 || tri2)
return target.y;
else
return -1;
}
int TerrainGen::interpolateType(int type1, int type2, double x)
{
double t = (1-cos(x * PI)) * .5;
double p = rand() % 100;
if(p > t*100)
return type1;
else
return type2;
}
double TerrainGen::getProbabilityByType(int terrainType)
{
switch(terrainType)
{
case 0:
return pField;
case 1:
return pForest;
case 2:
return pDesert;
default:
return -1;
}
}
tile TerrainGen::getTileData(int x, int z) {
if(x < 0 || z < 0 || x > MAX_WORLD_SIZE-1 || z > MAX_WORLD_SIZE-1)
return terrain[0][0];
else
return terrain[x][z];
}
bool TerrainGen::getTileDataFromFile(tile &t, Vector2 chunkLocation, int tileX, int tileZ)
{
string fileName = "terrain/"+itos(chunkLocation.x)+""+itos(chunkLocation.y)+".txt";
string test = "terrain/00.txt";
if(fileName != test)
return false;
//string fileName = "00.txt";
//string fileName2 = "00.txt";
const char * f = fileName.c_str();
FILE * file = fopen(f, "r");
if(file == NULL){
log("Couldn't open file "+fileName);
return false;
}
int n = 0;
float f1=0, f2=0;
for(int i = 0; i < tileX*(MAX_WORLD_SIZE-1)+tileZ; i++) {
fscanf(file, "%d", &n);
fscanf(file, "%E", &f1);
fscanf(file, "%E", &f2);
}
t.type = n;
t.y = f1;
t.gradient = f2;
return true;
}
TerrainGen::~TerrainGen(void)
{
}