Skip to content

Commit 983aeaf

Browse files
committed
Merge branch 'release/0.1.1'
2 parents 7155de7 + d65e303 commit 983aeaf

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<groupId>com.github.fabio-t</groupId>
2222
<artifactId>terrain-generator</artifactId>
23-
<version>0.1.0</version>
23+
<version>0.1.1</version>
2424
<packaging>jar</packaging>
2525

2626
<name>terrain-generator</name>

src/main/java/com/github/fabioticconi/tergen/HeightMap.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class HeightMap
3535
private float sourceThreshold;
3636
private float riverThreshold;
3737
private float riverFraction;
38+
private int maxSameDirection;
3839

3940
final public FractalNoise fractalNoise;
4041

@@ -48,6 +49,7 @@ public HeightMap()
4849
this.sourceThreshold = 0f;
4950
this.riverThreshold = 0f;
5051
this.riverFraction = 0f;
52+
this.maxSameDirection = 0;
5153

5254
this.fractalNoise = new FractalNoise();
5355
}
@@ -73,7 +75,7 @@ public HeightMap island(final float islandFraction)
7375
return this;
7476
}
7577

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)
7779
{
7880
if (sourceThreshold <= 0f || sourceThreshold > 1f)
7981
throw new IllegalArgumentException("source threshold must be in range (0,1]");
@@ -87,9 +89,13 @@ public HeightMap rivers(final float sourceThreshold, final float riverThreshold,
8789
if (riverFraction <= 0f || riverFraction > 1f)
8890
throw new IllegalArgumentException("river fraction must be in range (0,1]");
8991

92+
if (maxSameDirection < 0)
93+
throw new IllegalArgumentException("max same direction must be a non-negative integer");
94+
9095
this.sourceThreshold = sourceThreshold;
9196
this.riverThreshold = riverThreshold;
9297
this.riverFraction = riverFraction;
98+
this.maxSameDirection = maxSameDirection;
9399

94100
return this;
95101
}
@@ -161,7 +167,7 @@ public float[][] build()
161167
{
162168
if (heightmap[x][y] >= sourceThreshold && r.nextFloat() < riverFraction)
163169
{
164-
makeRiver(heightmap, x, y, -1);
170+
makeRiver(heightmap, x, y, -1, 0, -1);
165171

166172
// return heightmap; // FIXME remove this later
167173
}
@@ -172,7 +178,7 @@ public float[][] build()
172178
return heightmap;
173179
}
174180

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)
176182
{
177183
// the "source" cannot be lower than the "river depth"
178184
if (heightmap[x][y] <= riverThreshold)
@@ -201,9 +207,9 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina
201207

202208
final int y1 = y + z;
203209

204-
if (y1 < height && neighbour != skipNeighbour)
210+
if (y1 < height && neighbour != skipNeighbour && (same <= maxSameDirection || prev != neighbour))
205211
{
206-
System.out.println(x_t + " " + y1 + " " + heightmap[x_t][y1]);
212+
// System.out.println(x_t + " " + y1 + " " + heightmap[x_t][y1]);
207213

208214
if (heightmap[x_t][y1] > riverThreshold && heightmap[x_t][y1] < minHeight)
209215
{
@@ -223,11 +229,12 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina
223229
continue;
224230
}
225231

226-
System.out.println(x_t + " " + y2 + " " + heightmap[x_t][y2]);
232+
// System.out.println(x_t + " " + y2 + " " + heightmap[x_t][y2]);
227233

228234
if (neighbour != skipNeighbour &&
229235
heightmap[x_t][y2] > riverThreshold &&
230-
heightmap[x_t][y2] < minHeight)
236+
heightmap[x_t][y2] < minHeight &&
237+
(same <= maxSameDirection || prev != neighbour))
231238
{
232239
newX = x_t;
233240
newY = y2;
@@ -240,8 +247,10 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina
240247

241248
// System.out.println("chosen: " + newX + " " + newY + " " + minHeight + ", #" + chosen);
242249

250+
final int sameChosen = chosen == prev ? same + 1 : 0;
251+
243252
// we continue recursively
244253
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);
246255
}
247256
}

src/main/java/com/github/fabioticconi/tergen/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void main(final String[] args)
3333
final HeightMap heightMap = new HeightMap()
3434
.size(width, height)
3535
.island(0.8f)
36-
.rivers(0.8f, 0.03f, 0.001f);
36+
.rivers(0.8f, 0.03f, 0.001f, 1);
3737

3838
heightMap.fractalNoise
3939
.set(16, 0.5f, 3f / Math.max(width, height), 1f);

0 commit comments

Comments
 (0)