Skip to content

Commit b1cba2d

Browse files
committed
Start to new tile placement logic
Based on the wonderful advice of a fellow godot developer, I am integrating a dual grid system to make the tile placement logic work in a more correct manner.
1 parent eab7d51 commit b1cba2d

File tree

4 files changed

+163
-16
lines changed

4 files changed

+163
-16
lines changed
364 KB
Loading
51.6 KB
Binary file not shown.

src/TerrainGen/Terrain_Gen.cpp

Lines changed: 156 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
2626
2727
*****************************************************/
2828

29-
vector<vector<vector<int>>> gridMap(
30-
depth, vector<vector<int>>(width, vector<int>(height, 0)));
31-
29+
// Raw Noise -> Stored for possible random value use
3230
vector<vector<float>> rawNoise(width, vector<float>(height, 0));
33-
vector<vector<int>> elevationMap(width, vector<int>(height, 0));
31+
// Data Grid -> Simply an Elevation Map double the size of our Render Grid
32+
vector<vector<int>> elevationMap(width * 2, vector<int>(height * 2, 0));
33+
// Render Grid
3434
vector<vector<TileType>> tileMap(width, vector<TileType>(height, GROUND));
3535

36+
// Produce a 2x Grid for the Data Grid
37+
int widthx2 = width * 2;
38+
int heightx2 = height * 2;
39+
3640
int blockSize = 4;
37-
int reducedX = floor(width / blockSize);
38-
int reducedY = floor(height / blockSize);
41+
int reducedX = floor(widthx2 / blockSize);
42+
int reducedY = floor(heightx2 / blockSize);
3943
vector<vector<int>> lowResMap(reducedX, vector<int>(reducedY, 0));
4044

4145
int dx[4] = { 1, -1, 0, 0 };
@@ -48,8 +52,8 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
4852
*****************************************************/
4953

5054
// Generate the Elevation Map
51-
for (int x = 0; x < width; x++) {
52-
for (int y = 0; y < height; y++) {
55+
for (int x = 0; x < widthx2; x++) {
56+
for (int y = 0; y < heightx2; y++) {
5357
float currentNoise = noise->get_noise_2d((float)x, (float)y);
5458

5559
rawNoise[x][y] = currentNoise;
@@ -78,8 +82,8 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
7882
}
7983

8084
// Upscale back to Original Size
81-
for (int x = 0; x < width; x++) {
82-
for (int y = 0; y < height; y++) {
85+
for (int x = 0; x < widthx2; x++) {
86+
for (int y = 0; y < heightx2; y++) {
8387
int srcX = x / blockSize;
8488
int srcY = y / blockSize;
8589

@@ -99,8 +103,8 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
99103
//TODO...
100104

101105
// Water Clean Up
102-
for (int x = 1; x < width - 1; x++) {
103-
for (int y = 1; y < height - 1; y++) {
106+
for (int x = 1; x < widthx2 - 1; x++) {
107+
for (int y = 1; y < heightx2 - 1; y++) {
104108
if (elevationMap[x][y] > 0) { // Check if it's an elevated tile
105109
int waterCount = 0;
106110

@@ -130,11 +134,147 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
130134
// Loop over all the grid cells
131135
for (int x = 0; x < width; ++x) {
132136
for (int y = 0; y < height; ++y) {
133-
//Get the elevation for the current grid cell
134-
int elevation = elevationMap[x][y];
137+
// Godot GridMap Tile Rotation
138+
//
139+
// North = 0; // No rotation
140+
// South = 16;
141+
// East = 10;
142+
// West = 22;
143+
//
144+
int tilesRotation = 0;
145+
146+
//
147+
// +----+----+
148+
// | n1 | n2 |
149+
// +----+----+
150+
// | n3 | n4 |
151+
// +----+----+
152+
//
153+
// Get the surrounding DataGrid Neighbors overlap of the Render Grid
154+
int n1 = elevationMap[y][x];
155+
int n2 = elevationMap[y][x + 1];
156+
int n3 = elevationMap[y + 1][x];
157+
int n4 = elevationMap[y + 1][x + 1];
158+
159+
// Determine Elevation Changes
160+
//
161+
// Elevation change needs to be known in Left, Right, Up, & Down
162+
// due to the determination of a tiles type and rotation
163+
//
164+
// ! Unneeded due to the simpler way of just using neighbor values
165+
//
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
175+
176+
// Water's Water
177+
// +---+---+
178+
// | 0 | 0 |
179+
// +---+---+
180+
// | 0 | 0 |
181+
// +---+---+
182+
//
183+
if (n1 == 0 && n2 == 0 && n3 == 0 && n4 == 0) {
184+
tileMap[x][y] = WATER;
185+
}
186+
187+
// Ground's Ground
188+
// +----+----+ +---+---+ +---+---+
189+
// | n1 | n2 | | 1 | 1 | | 2 | 2 |
190+
// +----+----+ +---+---+ +---+---+
191+
// | n3 | n4 | | 1 | 1 | | 2 | 2 |
192+
// +----+----+ +---+---+ +---+---+
193+
//
194+
if (n1 == n2 && n2 == n3 && n3 == 4 && n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0) {
195+
tileMap[x][y] = GROUND;
196+
}
197+
198+
// Water's Edge South
199+
// +----+----+ +---+---+
200+
// | n1 | n2 | | 0 | 0 |
201+
// +----+----+ +---+---+
202+
// | n3 | n4 | | 1 | 1 |
203+
// +----+----+ +---+---+
204+
//
205+
if (n1 == 0 && n2 == 0 && n3 > 0 && n4 > 0) {
206+
tileMap[x][y] = WATER_EDGE;
207+
tilesRotation = 16; // Facing South
208+
}
209+
// Water's Edge North
210+
// +----+----+ +---+---+
211+
// | n1 | n2 | | 1 |1 |
212+
// +----+----+ +---+---+
213+
// | n3 | n4 | | 0 | 0 |
214+
// +----+----+ +---+---+
215+
//
216+
else if (n1 > 0 && n2 > 0 && n3 == 0 && n4 == 0) {
217+
tileMap[x][y] = WATER_EDGE;
218+
tilesRotation = 0; // Facing North
219+
}
220+
// Water's Edge East
221+
// +----+----+ +---+---+
222+
// | n1 | n2 | | 0 | 1 |
223+
// +----+----+ +---+---+
224+
// | n3 | n4 | | 0 | 1 |
225+
// +----+----+ +---+---+
226+
//
227+
else if (n1 == 0 && n3 == 0 && n2 > 0 && n4 > 0) {
228+
tileMap[x][y] = WATER_EDGE;
229+
tilesRotation = 10; // Facing East
230+
231+
}
232+
// Water's Edge West
233+
// +----+----+ +---+---+
234+
// | n1 | n2 | | 1 | 0 |
235+
// +----+----+ +---+---+
236+
// | n3 | n4 | | 1 | 0 |
237+
// +----+----+ +---+---+
238+
//
239+
else if (n2 == 0 && n4 == 0 && n1 > 0 && n3 > 0) {
240+
tileMap[x][y] = WATER_EDGE;
241+
tilesRotation = 22; // Facing West
242+
}
243+
244+
// Water's Corner's
245+
if (n1 == 0 && n2 == 0 && n3 == 0 && n4 > 0) {
246+
tileMap[x][y] = WATER_CORNER; // Land in SE
247+
} else if (n1 > 0 && n2 == 0 && n3 == 0 && n4 == 0) {
248+
tileMap[x][y] = WATER_CORNER; // Land in NW
249+
}
250+
251+
// Cliff's Edge
252+
// +----+----+ +---+---+
253+
// | n1 | n2 | | 2 | 1 |
254+
// +----+----+ +---+---+
255+
// | n3 | n4 | | 2 | 1 |
256+
// +----+----+ +---+---+
257+
//
258+
// TODO : Determine the way of choosing between cliffs and ramps
259+
if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0) {
260+
tileMap[x][y] = CLIFF;
261+
}
262+
263+
// Cliff's Edge
264+
// +----+----+ +---+---+
265+
// | n1 | n2 | | 2 | 1 |
266+
// +----+----+ +---+---+
267+
// | n3 | n4 | | 2 | 1 |
268+
// +----+----+ +---+---+
269+
//
270+
// TODO : Determine the way of choosing between cliffs and ramps
271+
if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n1 > n2 && n3 > n4) {
272+
tileMap[x][y] = CLIFF;
273+
}
274+
275+
// TODO : Add rest of the logic for all tile types
135276

136-
//Init the current grid cell to ground
137-
tileMap[x][y] = GROUND;
277+
// TODO : Remove unneeded Code below
138278

139279
// Setup booleans & Ints
140280
bool needsRamp = false, needsCliff = false, adjacentToWater = false;

src/TerrainGen/Terrain_Gen.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class TerrainGen : public Node {
2121
GDCLASS(TerrainGen, Node);
2222

2323
protected:
24+
enum Rotation {
25+
NORTH,
26+
EAST,
27+
SOUTH,
28+
WEST
29+
};
30+
2431
enum TileType {
2532
WATER, // Pure water tile, Connects to bottom of Water Edge tiles and other Water tiles
2633
WATER_CORNER, // Water corner with a section of ground at the top height, Connects to corners of water on one side and ground on the other

0 commit comments

Comments
 (0)