@@ -10,8 +10,6 @@ TerrainGen::~TerrainGen() {
10
10
}
11
11
12
12
void TerrainGen::generate (GridMap *myGridMap, int height, int width, int depth, int seed, int noiseType, int noiseOctaves, float jitter, float noiseFreq) {
13
- UtilityFunctions::print (" Begin Terrain Generation!" );
14
-
15
13
/* ****************************************************
16
14
17
15
Setup the Noise Function
@@ -36,23 +34,26 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
36
34
vector<vector<vector<int >>> gridMap (
37
35
depth, vector<vector<int >>(width, vector<int >(height, 0 )));
38
36
39
- // Noise Map may be used later, not sure yet
40
- // vector<vector<float>> noiseMap(width, vector<float>(height, 0.0f));
41
-
42
37
vector<vector<int >> elevationMap (width, vector<int >(height, 0 ));
43
38
vector<vector<int >> elevationMapSmooth (width, vector<int >(height, 0 ));
44
39
40
+ int dx[4 ] = { 1 , -1 , 0 , 0 };
41
+ int dy[4 ] = { 0 , 0 , 1 , -1 };
42
+
45
43
/* ****************************************************
46
44
47
45
Noise Map Generation & Smoothing
48
46
49
47
*****************************************************/
50
48
49
+ // Generate the Elevation Map
51
50
for (int x = 0 ; x < width; x++) {
52
51
for (int y = 0 ; y < height; y++) {
53
52
float currentNoise = noise->get_noise_2d ((float )x, (float )y);
54
- // noiseMap[x][y] = currentNoise;
55
- elevationMap[x][y] = round (((currentNoise + 1 .0f ) / 2 .0f ) * depth);
53
+
54
+ float bias = 0.1 , stepSize = 4 ;
55
+ int rawNoise = round (pow (((currentNoise + 1 .0f ) / 2 .0f ) + bias, 1.5 ) * depth);
56
+ elevationMap[x][y] = round (rawNoise / stepSize) * stepSize;
56
57
}
57
58
}
58
59
@@ -85,10 +86,35 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
85
86
86
87
/* ****************************************************
87
88
88
- Tile Placement
89
+ Elevation Spacing
90
+
91
+ The elevation map generates elevation changes to close together
92
+ create a buffer that smooths out double edges
89
93
90
94
*****************************************************/
91
95
96
+ for (int x = 1 ; x < width - 1 ; x++) {
97
+ for (int y = 1 ; y < height - 1 ; y++) {
98
+ int elevation = elevationMap[x][y];
99
+
100
+ bool hasElevationChange = false ;
101
+ for (int d = 0 ; d < 4 ; ++d) {
102
+ int nx = x + dx[d];
103
+ int ny = y + dy[d];
104
+
105
+ if (nx >= 0 && ny >= 0 && nx < width && ny < height) {
106
+ if (abs (elevationMap[nx][ny] - elevation) > 1 ) {
107
+ hasElevationChange = true ;
108
+ }
109
+ }
110
+ }
111
+
112
+ if (hasElevationChange) {
113
+ elevationMap[x][y] = elevationMap[x - 1 ][y]; // Force buffer using prior elevation
114
+ }
115
+ }
116
+ }
117
+
92
118
for (int x = 0 ; x < width; ++x) {
93
119
for (int y = 0 ; y < height; ++y) {
94
120
int elevation = elevationMap[x][y];
@@ -116,8 +142,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
116
142
*****************************************************/
117
143
118
144
int neighborElevations[4 ] = { elevation, elevation, elevation, elevation };
119
- int dx[4 ] = { 1 , -1 , 0 , 0 };
120
- int dy[4 ] = { 0 , 0 , 1 , -1 };
121
145
122
146
for (int d = 0 ; d < 4 ; ++d) {
123
147
int nx = x + dx[d];
@@ -128,7 +152,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
128
152
}
129
153
}
130
154
131
- // Elevation analysis
132
155
TileType tileType = GROUND;
133
156
int rotationOrientation = 0 ;
134
157
bool needsRamp = false , needsCliff = false , needsCorner = false ;
@@ -150,7 +173,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
150
173
}
151
174
}
152
175
153
- // Assign terrain type
154
176
if (numDirectionChanges == 0 ) {
155
177
tileType = GROUND;
156
178
} else if (needsCliff) {
@@ -159,7 +181,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
159
181
tileType = RAMP;
160
182
}
161
183
162
- // Corner detection: Two direction elevation change
163
184
if (numDirectionChanges >= 2 ) {
164
185
needsCorner = true ;
165
186
if (tileType == RAMP)
@@ -168,7 +189,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
168
189
tileType = CLIFF_CORNER;
169
190
}
170
191
171
- // Water Edge & Corner detection
172
192
if (tileType == WATER) {
173
193
if (numDirectionChanges == 1 ) {
174
194
tileType = WATER_EDGE;
@@ -177,40 +197,58 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
177
197
}
178
198
}
179
199
180
- // Assign rotation using **0, 90, 180, 270** only
181
200
if (tileType == RAMP || tileType == CLIFF || tileType == RAMP_CORNER || tileType == CLIFF_CORNER) {
182
- if (!needsCorner) {
183
- switch (dominantDirection) {
184
- case 0 :
185
- rotationOrientation = 0 ; // No Rotation
186
- break ; // North
187
- case 1 :
188
- rotationOrientation = 1 ; // 90 Degrees Clockwise
189
- break ; // East
190
- case 2 :
191
- rotationOrientation = 2 ; // 180 Degrees Clockwise
192
- break ; // South
193
- case 3 :
194
- rotationOrientation = 3 ; // 270 Degrees Clockwise
195
- break ; // West
201
+ int highestNeighborIndex = -1 ;
202
+ int highestNeighborElevation = elevation;
203
+ int secondaryNeighborIndex = -1 ;
204
+
205
+ for (int d = 0 ; d < 4 ; ++d) {
206
+ if (neighborElevations[d] > highestNeighborElevation) {
207
+ secondaryNeighborIndex = highestNeighborIndex;
208
+ highestNeighborElevation = neighborElevations[d];
209
+ highestNeighborIndex = d;
196
210
}
197
- } else {
198
- // Corner cases use the same **0, 90, 180, 270** logic.
199
- if (neighborElevations[0 ] != elevation && neighborElevations[2 ] != elevation) {
200
- rotationOrientation = 1 ; // Vertical corner (North-South elevation change)
201
- } else if (neighborElevations[1 ] != elevation && neighborElevations[3 ] != elevation) {
202
- rotationOrientation = 2 ; // Horizontal corner (East-West elevation change)
203
- } else if (neighborElevations[0 ] != elevation && neighborElevations[1 ] != elevation) {
204
- rotationOrientation = 0 ; // Corner facing North-East
205
- } else if (neighborElevations[2 ] != elevation && neighborElevations[3 ] != elevation) {
206
- rotationOrientation = 3 ; // Corner facing South-West
211
+ }
212
+
213
+ bool isCorner = (secondaryNeighborIndex != -1 && abs (neighborElevations[secondaryNeighborIndex] - elevation) > 0 );
214
+
215
+ if (highestNeighborIndex != -1 ) {
216
+ if (!isCorner) {
217
+ switch (highestNeighborIndex) {
218
+ case 0 :
219
+ rotationOrientation = 2 ;
220
+ break ; // North
221
+ case 1 :
222
+ rotationOrientation = 3 ;
223
+ break ; // East
224
+ case 2 :
225
+ rotationOrientation = 0 ;
226
+ break ; // South
227
+ case 3 :
228
+ rotationOrientation = 1 ;
229
+ break ; // West
230
+ }
231
+ } else {
232
+ if ((highestNeighborIndex == 0 && secondaryNeighborIndex == 1 ) ||
233
+ (highestNeighborIndex == 1 && secondaryNeighborIndex == 0 )) {
234
+ rotationOrientation = 0 ;
235
+ } else if ((highestNeighborIndex == 2 && secondaryNeighborIndex == 3 ) ||
236
+ (highestNeighborIndex == 3 && secondaryNeighborIndex == 2 )) {
237
+ rotationOrientation = 3 ;
238
+ } else if ((highestNeighborIndex == 0 && secondaryNeighborIndex == 3 ) ||
239
+ (highestNeighborIndex == 3 && secondaryNeighborIndex == 0 )) {
240
+ rotationOrientation = 1 ;
241
+ } else if ((highestNeighborIndex == 1 && secondaryNeighborIndex == 2 ) ||
242
+ (highestNeighborIndex == 2 && secondaryNeighborIndex == 1 )) {
243
+ rotationOrientation = 2 ;
244
+ }
207
245
}
208
246
}
209
247
}
210
248
211
249
/* ****************************************************
212
250
213
- Grid Cell Setter
251
+ Grid Map Cell Setter
214
252
215
253
*****************************************************/
216
254
0 commit comments