Skip to content

Commit 12b6694

Browse files
committed
Clean up and finalize draft of new placement logic
1 parent 67a2d72 commit 12b6694

File tree

2 files changed

+53
-202
lines changed

2 files changed

+53
-202
lines changed

src/TerrainGen/Terrain_Gen.cpp

Lines changed: 53 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,9 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
135135
for (int x = 0; x < width; ++x) {
136136
for (int y = 0; y < height; ++y) {
137137
// Godot GridMap Tile Rotation
138-
//
139-
// North = 0; // No rotation
140-
// South = 16;
141-
// East = 10;
142-
// West = 22;
143-
//
144-
int tilesRotation = 0;
138+
int tilesRotation = NORTH;
145139

146-
//
140+
// Neighbor's of Data Grid Map
147141
// +----+----+
148142
// | n1 | n2 |
149143
// +----+----+
@@ -156,22 +150,21 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
156150
int n3 = elevationMap[y + 1][x];
157151
int n4 = elevationMap[y + 1][x + 1];
158152

159-
// Determine Elevation Changes
153+
// Determine the Elevation Value
154+
//
155+
// Elevation of tile is the max of the neighbors elevation,
156+
// assuming that the random noise is consistent in its spread
157+
//
158+
int elevation = max({ n1, n2, n3, n4 });
159+
160+
// Determine Elevation Changes & Rotations & Tile Choice
160161
//
161162
// Elevation change needs to be known in Left, Right, Up, & Down
162-
// due to the determination of a tiles type and rotation
163+
// due to the determination of a tiles type and rotation.
163164
//
164-
// ! Unneeded due to the simpler way of just using neighbor values
165+
// The tiles determine their type in relation to elevation due to
166+
// their characteristics & setting water to elevation 0.
165167
//
166-
// bool change1right = n1 < n2; // n1 -> n2
167-
// bool change3down = n1 < n3; // n1 -> n3
168-
// bool change3right = n3 < n4; // n3 -> n4
169-
// bool change4down = n2 < n4; // n2 -> n4
170-
171-
// bool change1left = n1 > n2; // n1 -> n2
172-
// bool change2up = n1 > n3; // n1 -> n3
173-
// bool change3left = n3 > n4; // n3 -> n4
174-
// bool change4up = n2 > n4; // n2 -> n4
175168

176169
//-------------------------//
177170
// Basic Tiles
@@ -195,7 +188,7 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
195188
// | n3 | n4 | | 1 | 1 | | 2 | 2 |
196189
// +----+----+ +---+---+ +---+---+
197190
//
198-
if (n1 == n2 && n2 == n3 && n3 == 4 && n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0) {
191+
if (n1 == n2 && n2 == n3 && n3 == n4 && n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0) {
199192
tileMap[x][y] = GROUND;
200193
}
201194

@@ -216,7 +209,7 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
216209
}
217210
// Water's Edge North
218211
// +----+----+ +---+---+
219-
// | n1 | n2 | | 1 |1 |
212+
// | n1 | n2 | | 1 | 1 |
220213
// +----+----+ +---+---+
221214
// | n3 | n4 | | 0 | 0 |
222215
// +----+----+ +---+---+
@@ -311,6 +304,7 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
311304
// | n3 | n4 | | 1 | 2 |
312305
// +----+----+ +---+---+
313306
//
307+
// TODO : Determine the way of choosing between cliffs and ramps
314308
if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n4 > n1 && n4 > n2 && n4 > n3) {
315309
tileMap[x][y] = CLIFF_CORNER;
316310
tilesRotation = NORTH;
@@ -322,6 +316,7 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
322316
// | n3 | n4 | | 1 | 1 |
323317
// +----+----+ +---+---+
324318
//
319+
// TODO : Determine the way of choosing between cliffs and ramps
325320
else if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n1 > n2 && n1 > n3 && n1 > n4) {
326321
tileMap[x][y] = CLIFF_CORNER;
327322
tilesRotation = SOUTH;
@@ -333,6 +328,7 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
333328
// | n3 | n4 | | 1 | 1 |
334329
// +----+----+ +---+---+
335330
//
331+
// TODO : Determine the way of choosing between cliffs and ramps
336332
else if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n2 > n1 && n2 > n3 && n2 > n4) {
337333
tileMap[x][y] = CLIFF_CORNER;
338334
tilesRotation = EAST;
@@ -344,6 +340,7 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
344340
// | n3 | n4 | | 1 | 0 |
345341
// +----+----+ +---+---+
346342
//
343+
// TODO : Determine the way of choosing between cliffs and ramps
347344
else if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n3 > n1 && n3 > n2 && n3 > n4) {
348345
tileMap[x][y] = CLIFF_CORNER;
349346
tilesRotation = WEST;
@@ -353,211 +350,68 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
353350
// Cliff's & Ramp's Edges
354351
//-------------------------//
355352

356-
// Cliff's Edge
353+
// Cliff or Ramp Edge North
357354
// +----+----+ +---+---+
358-
// | n1 | n2 | | 2 | 1 |
355+
// | n1 | n2 | | 1 | 1 |
359356
// +----+----+ +---+---+
360-
// | n3 | n4 | | 2 | 1 |
357+
// | n3 | n4 | | 2 | 2 |
361358
// +----+----+ +---+---+
362359
//
363360
// TODO : Determine the way of choosing between cliffs and ramps
364-
if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0) {
361+
if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n1 < n3 && n1 < n4 && n2 < n4 && n2 < n3) {
365362
tileMap[x][y] = CLIFF;
363+
tilesRotation = NORTH;
366364
}
367-
368-
// Cliff's Edge
365+
// Cliff's Edge South
366+
// +----+----+ +---+---+
367+
// | n1 | n2 | | 2 | 2 |
368+
// +----+----+ +---+---+
369+
// | n3 | n4 | | 1 | 1 |
370+
// +----+----+ +---+---+
371+
//
372+
// TODO : Determine the way of choosing between cliffs and ramps
373+
else if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n1 > n3 && n1 > n4 && n2 > n4 && n2 > n3) {
374+
tileMap[x][y] = CLIFF;
375+
tilesRotation = SOUTH;
376+
}
377+
// Cliff's Edge East
369378
// +----+----+ +---+---+
370379
// | n1 | n2 | | 2 | 1 |
371380
// +----+----+ +---+---+
372381
// | n3 | n4 | | 2 | 1 |
373382
// +----+----+ +---+---+
374383
//
375384
// TODO : Determine the way of choosing between cliffs and ramps
376-
if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n1 > n2 && n3 > n4) {
385+
else if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n1 < n2 && n1 < n4 && n3 < n2 && n3 < n4) {
377386
tileMap[x][y] = CLIFF;
387+
tilesRotation = EAST;
378388
}
379-
380-
// TODO : Add rest of the logic for all tile types
381-
382-
// TODO : Remove unneeded Code below
383-
384-
// Setup booleans & Ints
385-
bool needsRamp = false, needsCliff = false, adjacentToWater = false;
386-
387-
int rotationOrientation = 0;
388-
int numElevationChanges = 0;
389-
int numDirectionChanges = 0;
390-
int maxElevationChange = 0;
391-
int waterEdgeNeighbors = 0;
392-
int waterNeighbors[4] = { 0, 0, 0, 0 };
393-
394-
// If Elevation is 0, we get water tiles
395-
if (elevation == 0) {
396-
myGridMap->set_cell_item(Vector3i(x, elevation, y), WATER, 0);
397-
continue;
398-
}
399-
400-
// Get the cardinal neighbors
401-
int neighborElevations[4] = { elevation, elevation, elevation, elevation };
402-
403-
// Loop through and assign value to the cardinal neighbors
404-
for (int d = 0; d < 4; ++d) {
405-
int nx = x + dx[d];
406-
int ny = y + dy[d];
407-
408-
if (nx >= 0 && ny >= 0 && nx < width && ny < height) {
409-
// Get Neighbors Elevation
410-
neighborElevations[d] = elevationMap[nx][ny];
411-
if (neighborElevations[d] == 0) {
412-
// If neighbor is water, store that info
413-
adjacentToWater = true;
414-
waterNeighbors[d] = 1;
415-
}
416-
}
417-
}
418-
419-
// Loop through the grid cells cardinal neighbors again
420-
for (int d = 0; d < 4; ++d) {
421-
// Find the Elevation diff between neighbors
422-
int diff = neighborElevations[d] - elevation;
423-
424-
// If not 0, then there is a change
425-
if (diff != 0)
426-
numDirectionChanges++;
427-
428-
// If an elevation diff of 1
429-
if (diff == 1 || diff == -1) {
430-
//We have elevation change
431-
432-
numElevationChanges++;
433-
maxElevationChange = max(maxElevationChange, abs(diff));
434-
435-
float randomFactor = (float)rand() / RAND_MAX;
436-
437-
// Set Cliffs or Ramps since Elevation has changed
438-
if (rawNoise[x][y] < 0.4) {
439-
tileMap[x][y] = CLIFF;
440-
} else {
441-
tileMap[x][y] = RAMP;
442-
}
443-
}
444-
}
445-
446-
// If grid cell is adjacent to water
447-
if (adjacentToWater) {
448-
// Get the cardinal neighbors
449-
waterEdgeNeighbors = waterNeighbors[0] + waterNeighbors[1] + waterNeighbors[2] + waterNeighbors[3];
450-
451-
// Decide if it needs to be a water edge or a water corner
452-
if (waterEdgeNeighbors == 1) {
453-
tileMap[x][y] = WATER_EDGE;
454-
} else if (
455-
(waterNeighbors[0] && waterNeighbors[1]) ||
456-
(waterNeighbors[1] && waterNeighbors[2]) ||
457-
(waterNeighbors[2] && waterNeighbors[3]) ||
458-
(waterNeighbors[3] && waterNeighbors[0])) {
459-
tileMap[x][y] = WATER_CORNER; // Two adjacent water edges form a corner
460-
}
461-
}
462-
463-
if (tileMap[x][y] == RAMP || tileMap[x][y] == CLIFF || tileMap[x][y] == RAMP_CORNER || tileMap[x][y] == CLIFF_CORNER) {
464-
int highestNeighborIndex = -1;
465-
int secondaryNeighborIndex = -1;
466-
int highestNeighborElevation = elevation;
467-
int secondaryNeighborElevation = elevation;
468-
469-
for (int d = 0; d < 4; ++d) {
470-
int diff = neighborElevations[d] - elevation;
471-
if (diff > 0) {
472-
if (diff > highestNeighborElevation - elevation) {
473-
secondaryNeighborIndex = highestNeighborIndex;
474-
secondaryNeighborElevation = highestNeighborElevation;
475-
highestNeighborElevation = neighborElevations[d];
476-
highestNeighborIndex = d;
477-
} else if (diff > secondaryNeighborElevation - elevation) {
478-
secondaryNeighborElevation = neighborElevations[d];
479-
secondaryNeighborIndex = d;
480-
}
481-
}
482-
}
483-
484-
// Determine if we have a corner case (two adjacent neighbors at higher elevation)
485-
bool isCorner = (secondaryNeighborIndex != -1 && abs(neighborElevations[secondaryNeighborIndex] - elevation) > 0);
486-
487-
// Apply rotation based on elevation direction
488-
if (highestNeighborIndex != -1) {
489-
if (!isCorner) {
490-
switch (highestNeighborIndex) {
491-
case 0:
492-
rotationOrientation = 0;
493-
break; // North
494-
case 1:
495-
rotationOrientation = 10;
496-
break; // East
497-
case 2:
498-
rotationOrientation = 16;
499-
break; // South
500-
case 3:
501-
rotationOrientation = 22;
502-
break; // West
503-
}
504-
} else {
505-
// Handle corner rotation logic
506-
if ((highestNeighborIndex == 0 && secondaryNeighborIndex == 1) ||
507-
(highestNeighborIndex == 1 && secondaryNeighborIndex == 0)) {
508-
rotationOrientation = 0; // Top-right corner
509-
} else if ((highestNeighborIndex == 2 && secondaryNeighborIndex == 3) ||
510-
(highestNeighborIndex == 3 && secondaryNeighborIndex == 2)) {
511-
rotationOrientation = 10; // Bottom-left corner
512-
} else if ((highestNeighborIndex == 0 && secondaryNeighborIndex == 3) ||
513-
(highestNeighborIndex == 3 && secondaryNeighborIndex == 0)) {
514-
rotationOrientation = 16; // Top-left corner
515-
} else if ((highestNeighborIndex == 1 && secondaryNeighborIndex == 2) ||
516-
(highestNeighborIndex == 2 && secondaryNeighborIndex == 1)) {
517-
rotationOrientation = 22; // Bottom-right corner
518-
}
519-
}
520-
}
389+
// Cliff's Edge West
390+
// +----+----+ +---+---+
391+
// | n1 | n2 | | 1 | 2 |
392+
// +----+----+ +---+---+
393+
// | n3 | n4 | | 1 | 2 |
394+
// +----+----+ +---+---+
395+
//
396+
// TODO : Determine the way of choosing between cliffs and ramps
397+
else if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n1 > n2 && n1 > n4 && n3 > n2 && n3 > n4) {
398+
tileMap[x][y] = CLIFF;
399+
tilesRotation = WEST;
521400
}
522401

523402
/*****************************************************
524403
525-
Grid Map Cell Setter
404+
Grid Map Cell Setter
526405
527-
*****************************************************/
406+
*****************************************************/
528407

529-
myGridMap->set_cell_item(Vector3i(x, elevation, y), tileMap[x][y], rotationOrientation);
408+
myGridMap->set_cell_item(Vector3i(x, elevation, y), tileMap[x][y], tilesRotation);
530409
}
531410
}
532411

533412
//TODO : Another run through required to check adjacent tiles, especially tiles touching corner tiles. TO ensure a cliff corner connects to cliffs
534413
}
535414

536-
TerrainGen::TileType TerrainGen::isCornerTile(int x, int y, vector<vector<TileType>> &tileMap) {
537-
if (x < 1 || y < 1 || x >= tileMap.size() - 1 || y >= tileMap[0].size() - 1)
538-
return GROUND; // Bounds check
539-
540-
TileType TL = tileMap[x - 1][y - 1];
541-
TileType TR = tileMap[x + 1][y - 1];
542-
TileType BL = tileMap[x - 1][y + 1];
543-
TileType BR = tileMap[x + 1][y + 1];
544-
545-
// Check diagonal patterns for L-shapes
546-
if ((TL == WATER && BR == WATER) || (TR == WATER && BL == WATER)) {
547-
return WATER_CORNER; // Water Corner
548-
}
549-
550-
if ((TL == CLIFF && BR == CLIFF) || (TR == CLIFF && BL == CLIFF)) {
551-
return CLIFF_CORNER; // Cliff Corner
552-
}
553-
554-
if ((TL == RAMP && BR == RAMP) || (TR == RAMP && BL == RAMP)) {
555-
return RAMP_CORNER; // Ramp Corner
556-
}
557-
558-
return GROUND;
559-
}
560-
561415
void TerrainGen::_bind_methods() {
562416
ClassDB::bind_method(D_METHOD("generate", "GridMap", "height", "width", "depth", "seed", "noiseType", "waterRemoval", "noiseFreq"), &TerrainGen::generate);
563417
}

src/TerrainGen/Terrain_Gen.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ class TerrainGen : public Node {
4343

4444
static void _bind_methods();
4545

46-
private:
47-
TileType isCornerTile(int x, int y, vector<vector<TileType>> &tileMap);
48-
4946
public:
5047
TerrainGen();
5148
~TerrainGen();

0 commit comments

Comments
 (0)