Skip to content

Commit

Permalink
Merge branch 'release/0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-t committed Feb 27, 2018
2 parents 7155de7 + d65e303 commit 983aeaf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>com.github.fabio-t</groupId>
<artifactId>terrain-generator</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<packaging>jar</packaging>

<name>terrain-generator</name>
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/com/github/fabioticconi/tergen/HeightMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class HeightMap
private float sourceThreshold;
private float riverThreshold;
private float riverFraction;
private int maxSameDirection;

final public FractalNoise fractalNoise;

Expand All @@ -48,6 +49,7 @@ public HeightMap()
this.sourceThreshold = 0f;
this.riverThreshold = 0f;
this.riverFraction = 0f;
this.maxSameDirection = 0;

this.fractalNoise = new FractalNoise();
}
Expand All @@ -73,7 +75,7 @@ public HeightMap island(final float islandFraction)
return this;
}

public HeightMap rivers(final float sourceThreshold, final float riverThreshold, final float riverFraction)
public HeightMap rivers(final float sourceThreshold, final float riverThreshold, final float riverFraction, final int maxSameDirection)
{
if (sourceThreshold <= 0f || sourceThreshold > 1f)
throw new IllegalArgumentException("source threshold must be in range (0,1]");
Expand All @@ -87,9 +89,13 @@ public HeightMap rivers(final float sourceThreshold, final float riverThreshold,
if (riverFraction <= 0f || riverFraction > 1f)
throw new IllegalArgumentException("river fraction must be in range (0,1]");

if (maxSameDirection < 0)
throw new IllegalArgumentException("max same direction must be a non-negative integer");

this.sourceThreshold = sourceThreshold;
this.riverThreshold = riverThreshold;
this.riverFraction = riverFraction;
this.maxSameDirection = maxSameDirection;

return this;
}
Expand Down Expand Up @@ -161,7 +167,7 @@ public float[][] build()
{
if (heightmap[x][y] >= sourceThreshold && r.nextFloat() < riverFraction)
{
makeRiver(heightmap, x, y, -1);
makeRiver(heightmap, x, y, -1, 0, -1);

// return heightmap; // FIXME remove this later
}
Expand All @@ -172,7 +178,7 @@ public float[][] build()
return heightmap;
}

private void makeRiver(final float heightmap[][], final int x, final int y, final int skipNeighbour)
private void makeRiver(final float heightmap[][], final int x, final int y, final int skipNeighbour, final int same, final int prev)
{
// the "source" cannot be lower than the "river depth"
if (heightmap[x][y] <= riverThreshold)
Expand Down Expand Up @@ -201,9 +207,9 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina

final int y1 = y + z;

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

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

System.out.println(x_t + " " + y2 + " " + heightmap[x_t][y2]);
// System.out.println(x_t + " " + y2 + " " + heightmap[x_t][y2]);

if (neighbour != skipNeighbour &&
heightmap[x_t][y2] > riverThreshold &&
heightmap[x_t][y2] < minHeight)
heightmap[x_t][y2] < minHeight &&
(same <= maxSameDirection || prev != neighbour))
{
newX = x_t;
newY = y2;
Expand All @@ -240,8 +247,10 @@ private void makeRiver(final float heightmap[][], final int x, final int y, fina

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

final int sameChosen = chosen == prev ? same + 1 : 0;

// we continue recursively
if (newX >= 0)
makeRiver(heightmap, newX, newY, chosen >= 0 ? 3-chosen : chosen);
makeRiver(heightmap, newX, newY, skipNeighbour < 0 && chosen >= 0 ? 3-chosen : skipNeighbour, sameChosen, chosen);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/github/fabioticconi/tergen/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void main(final String[] args)
final HeightMap heightMap = new HeightMap()
.size(width, height)
.island(0.8f)
.rivers(0.8f, 0.03f, 0.001f);
.rivers(0.8f, 0.03f, 0.001f, 1);

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

0 comments on commit 983aeaf

Please sign in to comment.