@@ -10,8 +10,6 @@ TerrainGen::~TerrainGen() {
1010}
1111
1212void 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-
1513 /* ****************************************************
1614
1715 Setup the Noise Function
@@ -36,23 +34,26 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
3634 vector<vector<vector<int >>> gridMap (
3735 depth, vector<vector<int >>(width, vector<int >(height, 0 )));
3836
39- // Noise Map may be used later, not sure yet
40- // vector<vector<float>> noiseMap(width, vector<float>(height, 0.0f));
41-
4237 vector<vector<int >> elevationMap (width, vector<int >(height, 0 ));
4338 vector<vector<int >> elevationMapSmooth (width, vector<int >(height, 0 ));
4439
40+ int dx[4 ] = { 1 , -1 , 0 , 0 };
41+ int dy[4 ] = { 0 , 0 , 1 , -1 };
42+
4543 /* ****************************************************
4644
4745 Noise Map Generation & Smoothing
4846
4947 *****************************************************/
5048
49+ // Generate the Elevation Map
5150 for (int x = 0 ; x < width; x++) {
5251 for (int y = 0 ; y < height; y++) {
5352 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;
5657 }
5758 }
5859
@@ -85,10 +86,35 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
8586
8687 /* ****************************************************
8788
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
8993
9094 *****************************************************/
9195
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+
92118 for (int x = 0 ; x < width; ++x) {
93119 for (int y = 0 ; y < height; ++y) {
94120 int elevation = elevationMap[x][y];
@@ -116,8 +142,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
116142 *****************************************************/
117143
118144 int neighborElevations[4 ] = { elevation, elevation, elevation, elevation };
119- int dx[4 ] = { 1 , -1 , 0 , 0 };
120- int dy[4 ] = { 0 , 0 , 1 , -1 };
121145
122146 for (int d = 0 ; d < 4 ; ++d) {
123147 int nx = x + dx[d];
@@ -128,7 +152,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
128152 }
129153 }
130154
131- // Elevation analysis
132155 TileType tileType = GROUND;
133156 int rotationOrientation = 0 ;
134157 bool needsRamp = false , needsCliff = false , needsCorner = false ;
@@ -150,7 +173,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
150173 }
151174 }
152175
153- // Assign terrain type
154176 if (numDirectionChanges == 0 ) {
155177 tileType = GROUND;
156178 } else if (needsCliff) {
@@ -159,7 +181,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
159181 tileType = RAMP;
160182 }
161183
162- // Corner detection: Two direction elevation change
163184 if (numDirectionChanges >= 2 ) {
164185 needsCorner = true ;
165186 if (tileType == RAMP)
@@ -168,7 +189,6 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
168189 tileType = CLIFF_CORNER;
169190 }
170191
171- // Water Edge & Corner detection
172192 if (tileType == WATER) {
173193 if (numDirectionChanges == 1 ) {
174194 tileType = WATER_EDGE;
@@ -177,40 +197,58 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
177197 }
178198 }
179199
180- // Assign rotation using **0, 90, 180, 270** only
181200 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;
196210 }
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+ }
207245 }
208246 }
209247 }
210248
211249 /* ****************************************************
212250
213- Grid Cell Setter
251+ Grid Map Cell Setter
214252
215253 *****************************************************/
216254
0 commit comments