@@ -35,6 +35,7 @@ public class HeightMap
35
35
private float sourceThreshold ;
36
36
private float riverThreshold ;
37
37
private float riverFraction ;
38
+ private int maxSameDirection ;
38
39
39
40
final public FractalNoise fractalNoise ;
40
41
@@ -48,6 +49,7 @@ public HeightMap()
48
49
this .sourceThreshold = 0f ;
49
50
this .riverThreshold = 0f ;
50
51
this .riverFraction = 0f ;
52
+ this .maxSameDirection = 0 ;
51
53
52
54
this .fractalNoise = new FractalNoise ();
53
55
}
@@ -73,7 +75,7 @@ public HeightMap island(final float islandFraction)
73
75
return this ;
74
76
}
75
77
76
- public HeightMap rivers (final float sourceThreshold , final float riverThreshold , final float riverFraction )
78
+ public HeightMap rivers (final float sourceThreshold , final float riverThreshold , final float riverFraction , final int maxSameDirection )
77
79
{
78
80
if (sourceThreshold <= 0f || sourceThreshold > 1f )
79
81
throw new IllegalArgumentException ("source threshold must be in range (0,1]" );
@@ -87,9 +89,13 @@ public HeightMap rivers(final float sourceThreshold, final float riverThreshold,
87
89
if (riverFraction <= 0f || riverFraction > 1f )
88
90
throw new IllegalArgumentException ("river fraction must be in range (0,1]" );
89
91
92
+ if (maxSameDirection < 0 )
93
+ throw new IllegalArgumentException ("max same direction must be a non-negative integer" );
94
+
90
95
this .sourceThreshold = sourceThreshold ;
91
96
this .riverThreshold = riverThreshold ;
92
97
this .riverFraction = riverFraction ;
98
+ this .maxSameDirection = maxSameDirection ;
93
99
94
100
return this ;
95
101
}
@@ -161,7 +167,7 @@ public float[][] build()
161
167
{
162
168
if (heightmap [x ][y ] >= sourceThreshold && r .nextFloat () < riverFraction )
163
169
{
164
- makeRiver (heightmap , x , y , -1 );
170
+ makeRiver (heightmap , x , y , -1 , 0 , - 1 );
165
171
166
172
// return heightmap; // FIXME remove this later
167
173
}
@@ -172,7 +178,7 @@ public float[][] build()
172
178
return heightmap ;
173
179
}
174
180
175
- private void makeRiver (final float heightmap [][], final int x , final int y , final int skipNeighbour )
181
+ private void makeRiver (final float heightmap [][], final int x , final int y , final int skipNeighbour , final int same , final int prev )
176
182
{
177
183
// the "source" cannot be lower than the "river depth"
178
184
if (heightmap [x ][y ] <= riverThreshold )
@@ -201,9 +207,9 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina
201
207
202
208
final int y1 = y + z ;
203
209
204
- if (y1 < height && neighbour != skipNeighbour )
210
+ if (y1 < height && neighbour != skipNeighbour && ( same <= maxSameDirection || prev != neighbour ) )
205
211
{
206
- System .out .println (x_t + " " + y1 + " " + heightmap [x_t ][y1 ]);
212
+ // System.out.println(x_t + " " + y1 + " " + heightmap[x_t][y1]);
207
213
208
214
if (heightmap [x_t ][y1 ] > riverThreshold && heightmap [x_t ][y1 ] < minHeight )
209
215
{
@@ -223,11 +229,12 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina
223
229
continue ;
224
230
}
225
231
226
- System .out .println (x_t + " " + y2 + " " + heightmap [x_t ][y2 ]);
232
+ // System.out.println(x_t + " " + y2 + " " + heightmap[x_t][y2]);
227
233
228
234
if (neighbour != skipNeighbour &&
229
235
heightmap [x_t ][y2 ] > riverThreshold &&
230
- heightmap [x_t ][y2 ] < minHeight )
236
+ heightmap [x_t ][y2 ] < minHeight &&
237
+ (same <= maxSameDirection || prev != neighbour ))
231
238
{
232
239
newX = x_t ;
233
240
newY = y2 ;
@@ -240,8 +247,10 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina
240
247
241
248
// System.out.println("chosen: " + newX + " " + newY + " " + minHeight + ", #" + chosen);
242
249
250
+ final int sameChosen = chosen == prev ? same + 1 : 0 ;
251
+
243
252
// we continue recursively
244
253
if (newX >= 0 )
245
- makeRiver (heightmap , newX , newY , chosen >= 0 ? 3 -chosen : chosen );
254
+ makeRiver (heightmap , newX , newY , skipNeighbour < 0 && chosen >= 0 ? 3 -chosen : skipNeighbour , sameChosen , chosen );
246
255
}
247
256
}
0 commit comments