DFS instantiation in biome generator is now correct.

Performed refactoring

Began work on world height shader (untested).
This commit is contained in:
Harrison Deng 2020-04-21 20:59:03 -05:00
parent e2c3e46849
commit 6d552820ee
16 changed files with 431 additions and 46 deletions

21
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch) - Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 25566
}
]
}

View File

@ -41,7 +41,7 @@
</build> </build>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
</properties> </properties>
</project> </project>

View File

@ -4,7 +4,6 @@ import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import ca.recrown.islandsurvivalcraft.world.IslandSurvivalCraftChunkGenerator; import ca.recrown.islandsurvivalcraft.world.IslandSurvivalCraftChunkGenerator;
import ca.recrown.islandsurvivalcraft.world.IslandWorldGenerator;
import ca.recrown.islandsurvivalcraft.world.generation.BiomePerIslandGenerator; import ca.recrown.islandsurvivalcraft.world.generation.BiomePerIslandGenerator;
public class IslandSurvivalCraft extends JavaPlugin { public class IslandSurvivalCraft extends JavaPlugin {

View File

@ -6,6 +6,8 @@ import java.util.Objects;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Queue; import java.util.Queue;
import org.apache.commons.lang.NullArgumentException;
public class DepthFirstSearch { public class DepthFirstSearch {
private Queue<Node> queue; private Queue<Node> queue;
private CoordinateValidatable validatable; private CoordinateValidatable validatable;
@ -48,6 +50,9 @@ public class DepthFirstSearch {
} }
public boolean buildPathToEndNode() { public boolean buildPathToEndNode() {
if (validatable == null) throw new IllegalStateException("Need to set a validator.");
if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
queue = new PriorityQueue<>(); queue = new PriorityQueue<>();
checkedNodes.clear(); checkedNodes.clear();
Node begin = startNode; Node begin = startNode;
@ -83,6 +88,10 @@ public class DepthFirstSearch {
* @return * @return
*/ */
public boolean findTarget(CoordinateTargetValidatable targetValidator) { public boolean findTarget(CoordinateTargetValidatable targetValidator) {
if (validatable == null) throw new IllegalStateException("Need to set a validator.");
if (targetValidator == null) throw new NullArgumentException("targetValidator");
if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
queue = new LinkedList<>(); queue = new LinkedList<>();
checkedNodes.clear(); checkedNodes.clear();
Node begin = startNode; Node begin = startNode;
@ -127,6 +136,7 @@ public class DepthFirstSearch {
public int compareTo(Node o) { public int compareTo(Node o) {
return Math.round(distanceToGoal(endNode) - o.distanceToGoal(endNode)); return Math.round(distanceToGoal(endNode) - o.distanceToGoal(endNode));
} }
public float distanceToGoal(Node goal) { public float distanceToGoal(Node goal) {
float distanceX = goal.x - this.x; float distanceX = goal.x - this.x;

View File

@ -4,11 +4,13 @@ import java.util.Random;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.generator.ChunkGenerator.BiomeGrid; import org.bukkit.generator.ChunkGenerator.BiomeGrid;
import org.bukkit.generator.ChunkGenerator.ChunkData; import org.bukkit.generator.ChunkGenerator.ChunkData;
import ca.recrown.islandsurvivalcraft.world.generation.BedrockGenerator; import ca.recrown.islandsurvivalcraft.world.generation.BedrockGenerator;
import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator; import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator;
import ca.recrown.islandsurvivalcraft.world.shaders.WorldHeightShader;
/** /**
* A world generator. * A world generator.
@ -18,31 +20,45 @@ public class IslandWorldGenerator {
private final BedrockGenerator bedrockGenerator; private final BedrockGenerator bedrockGenerator;
private final BiomeSelector biomeSelector; private final BiomeSelector biomeSelector;
private final IslandBiomeGenerator biomeGenerator; private final IslandBiomeGenerator biomeGenerator;
private final WorldHeightShader heightShader;
public final World world; public final World world;
public final IslandWorldBaseTerrainMap islandMapGenerator; public final IslandWorldBaseTerrainMap islandMapGenerator;
public final Random random; public final Random random;
public IslandWorldGenerator(World world, IslandBiomeGenerator generator, Random random) { public IslandWorldGenerator(World world, IslandBiomeGenerator islandBiomeGenerator, Random random) {
this.world = world; this.world = world;
this.maxHeight = world.getMaxHeight(); this.maxHeight = world.getMaxHeight();
this.random = random; this.random = random;
this.biomeSelector = new BiomeSelector(random); this.biomeSelector = new BiomeSelector(random);
this.bedrockGenerator = new BedrockGenerator(random); this.bedrockGenerator = new BedrockGenerator(random);
this.islandMapGenerator = new IslandWorldBaseTerrainMap(random, world.getSeaLevel(), 4); this.islandMapGenerator = new IslandWorldBaseTerrainMap(random, world.getSeaLevel(), 4);
this.biomeGenerator = generator; this.biomeGenerator = islandBiomeGenerator;
this.heightShader = new WorldHeightShader(world.getSeed(), islandMapGenerator);
biomeSelector.initialize(); biomeSelector.initialize();
islandBiomeGenerator.initialize(world, islandMapGenerator, biomeSelector);
} }
public void GenerateChunk(int chunkX, int chunkZ, int localX, int localZ, ChunkData chunk, BiomeGrid biomeGrid) { public void GenerateChunk(int chunkX, int chunkZ, int localX, int localZ, ChunkData chunk, BiomeGrid biomeGrid) {
int worldX = 16 * chunkX + localX; int worldX = 16 * chunkX + localX;
int worldZ = 16 * chunkZ + localZ; int worldZ = 16 * chunkZ + localZ;
//gets the bedrock.
int bedrockHeight = bedrockGenerator.getBedrockHeight(worldX, worldZ);
//Sets the biome. //Sets the biome.
biomeGenerator.GenerateBiome(chunkX, chunkZ, localX, localZ, biomeGrid); Biome currentBiome = biomeGenerator.GenerateBiome(chunkX, chunkZ, localX, localZ);
for (int y = 0; y < maxHeight; y++) {
biomeGrid.setBiome(localX, y, localZ, currentBiome);
}
//Sets the bedrock. //get height shader.
int bedrockHeight = bedrockGenerator.getBedrockHeight(worldX, worldZ); int height = heightShader.getAltitude(worldX, worldZ, currentBiome);
//set general shape
chunk.setRegion(localX, 0, localZ, localX, height, localZ, Material.BEDROCK);
//set bedrock last
chunk.setRegion(localX, 0, localZ, localX, bedrockHeight, localZ, Material.BEDROCK); chunk.setRegion(localX, 0, localZ, localX, bedrockHeight, localZ, Material.BEDROCK);
} }
} }

View File

@ -5,13 +5,11 @@ import java.util.Random;
import org.bukkit.util.noise.SimplexOctaveGenerator; import org.bukkit.util.noise.SimplexOctaveGenerator;
public class BedrockGenerator { public class BedrockGenerator {
private Random random;
private SimplexOctaveGenerator noiseGenerator; private SimplexOctaveGenerator noiseGenerator;
private final int maxBedrockHeight = 5; private final int maxBedrockHeight = 5;
private final int minBedrockHeight = 1; private final int minBedrockHeight = 1;
public BedrockGenerator(Random random) { public BedrockGenerator(Random random) {
this.random = random;
noiseGenerator = new SimplexOctaveGenerator(random, 8); noiseGenerator = new SimplexOctaveGenerator(random, 8);
noiseGenerator.setScale(0.1D); noiseGenerator.setScale(0.1D);
} }

View File

@ -2,7 +2,6 @@ package ca.recrown.islandsurvivalcraft.world.generation;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
import ca.recrown.islandsurvivalcraft.pathfinding.CoordinateTargetValidatable; import ca.recrown.islandsurvivalcraft.pathfinding.CoordinateTargetValidatable;
import ca.recrown.islandsurvivalcraft.pathfinding.DepthFirstSearch; import ca.recrown.islandsurvivalcraft.pathfinding.DepthFirstSearch;
@ -14,19 +13,14 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator, Coordinate
private IslandWorldBaseTerrainMap islandLocator; private IslandWorldBaseTerrainMap islandLocator;
private BiomeSelector biomeSelector; private BiomeSelector biomeSelector;
private TemperatureMapGenerator temperatureMapGenerator; private TemperatureMapGenerator temperatureMapGenerator;
private int buildHeight;
private World world; private World world;
private DepthFirstSearch dfs; private DepthFirstSearch dfs;
private Biome mainBiome; private Biome mainBiome;
private Biome shoreBiome; private Biome shoreBiome;
private float temperature; private float temperature;
private BiomeGrid currentBiomeGrid;
private int currentChunkX, currentChunkZ;
public BiomePerIslandGenerator() { public BiomePerIslandGenerator() {
this.temperatureMapGenerator = new TemperatureMapGenerator(); this.temperatureMapGenerator = new TemperatureMapGenerator();
dfs = new DepthFirstSearch(islandLocator);
} }
@Override @Override
@ -37,7 +31,7 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator, Coordinate
this.biomeSelector = biomeSelector; this.biomeSelector = biomeSelector;
this.world = world; this.world = world;
this.temperatureMapGenerator.setSeed(world.getSeed()); this.temperatureMapGenerator.setSeed(world.getSeed());
this.buildHeight = world.getMaxHeight(); dfs = new DepthFirstSearch(islandLocator);
} }
@Override @Override
@ -52,32 +46,18 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator, Coordinate
if (mainBiome != null && shoreBiome != null) { if (mainBiome != null && shoreBiome != null) {
return true; return true;
} }
} else if (x/16 == currentChunkX && y/16 == currentChunkZ) {
int localX = x - (currentChunkX * 16);
int localZ = y - (currentChunkZ * 16);
Biome foundBiome = currentBiomeGrid.getBiome(localX, 0, localZ);
if (islandLocator.isShore(x, y)) {
shoreBiome = foundBiome;
} else {
mainBiome = foundBiome;
}
if (mainBiome != null && shoreBiome != null) {
return true;
}
} }
return false; return false;
} }
@Override @Override
public void GenerateBiome(int chunkX, int chunkZ, int localX, int localZ, BiomeGrid biomeGrid) { public Biome GenerateBiome(int chunkX, int chunkZ, int localX, int localZ) {
int worldX = chunkX * 16 + localX; int worldX = chunkX * 16 + localX;
int worldZ = chunkZ * 16 + localZ; int worldZ = chunkZ * 16 + localZ;
if (islandLocator.isIsland(worldX, worldZ)) { if (islandLocator.isIsland(worldX, worldZ)) {
if (mainBiome == null && shoreBiome == null) { if (mainBiome == null && shoreBiome == null) {
this.currentBiomeGrid = biomeGrid; dfs.setStartPosition(worldX, worldZ);
this.currentChunkX = chunkX;
this.currentChunkZ = chunkZ;
if (!dfs.findTarget(this)) { if (!dfs.findTarget(this)) {
temperature = temperatureMapGenerator.getTemperature(worldX, worldZ); temperature = temperatureMapGenerator.getTemperature(worldX, worldZ);
mainBiome = biomeSelector.getLandBiome(temperature); mainBiome = biomeSelector.getLandBiome(temperature);
@ -96,10 +76,7 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator, Coordinate
} else if (islandLocator.isShore(worldX, worldZ)) { } else if (islandLocator.isShore(worldX, worldZ)) {
designatedBiome = shoreBiome; designatedBiome = shoreBiome;
} }
return designatedBiome;
for (int y = 0; y < buildHeight; y++) {
biomeGrid.setBiome(localX, y, localZ, designatedBiome);
}
} }
@Override @Override

View File

@ -2,13 +2,12 @@ package ca.recrown.islandsurvivalcraft.world.generation;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
import ca.recrown.islandsurvivalcraft.world.BiomeSelector; import ca.recrown.islandsurvivalcraft.world.BiomeSelector;
import ca.recrown.islandsurvivalcraft.world.IslandWorldBaseTerrainMap; import ca.recrown.islandsurvivalcraft.world.IslandWorldBaseTerrainMap;
public interface IslandBiomeGenerator { public interface IslandBiomeGenerator {
public void initialize(World world, IslandWorldBaseTerrainMap mapGenerator, BiomeSelector biome); public void initialize(World world, IslandWorldBaseTerrainMap mapGenerator, BiomeSelector biomeSelector);
public void GenerateBiome(int chunkX, int chunkZ, int localX, int localZ, BiomeGrid biomeGrid); public Biome GenerateBiome(int chunkX, int chunkZ, int localX, int localZ);
public IslandBiomeGenerator getInstance(); public IslandBiomeGenerator getInstance();
} }

View File

@ -1,4 +1,4 @@
package ca.recrown.islandsurvivalcraft.world.shapers; package ca.recrown.islandsurvivalcraft.world.shaders;
import java.util.Random; import java.util.Random;

View File

@ -139,9 +139,9 @@ public class DepthFirstSearchTest {
@Test @Test
public void testDFSFindEndNodeMapDValid() { public void testDFSFindEndNodeMapDValid() {
Validator validator = new Validator(mapD); Validator validator = new Validator(mapD);
dfs = new DepthFirstSearch(validator);
dfs.setValidatable(validator); dfs.setValidatable(validator);
dfs.setStartPosition(0, 0); dfs.setStartPosition(0, 0);
assertTrue(dfs.findTarget(validator)); assertTrue(dfs.findTarget(validator));
assertEquals(0, dfs.getEndX()); assertEquals(0, dfs.getEndX());
assertEquals(3, dfs.getEndY()); assertEquals(3, dfs.getEndY());
@ -150,6 +150,7 @@ public class DepthFirstSearchTest {
@Test @Test
public void testDFSFindEndNodeMapEInvalid() { public void testDFSFindEndNodeMapEInvalid() {
Validator validator = new Validator(mapE); Validator validator = new Validator(mapE);
dfs = new DepthFirstSearch();
dfs.setValidatable(validator); dfs.setValidatable(validator);
dfs.setStartPosition(0, 0); dfs.setStartPosition(0, 0);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
#Minecraft server properties #Minecraft server properties
#Sun Apr 19 03:21:17 CDT 2020 #Tue Apr 21 20:37:39 CDT 2020
spawn-protection=16 spawn-protection=16
max-tick-time=60000 max-tick-time=60000
query.port=25565 query.port=25565

View File

@ -4,5 +4,5 @@ del /s /f /q world >nul 2>&1
ECHO "Attempting to copy plugin jar to plugins folder" ECHO "Attempting to copy plugin jar to plugins folder"
COPY "..\target\IslandSurvivalCraft-1.0.0.jar" "plugins\IslandSurvivalCraft-1.0.0.jar" >nul COPY "..\target\IslandSurvivalCraft-1.0.0.jar" "plugins\IslandSurvivalCraft-1.0.0.jar" >nul
ECHO "Starting server..." ECHO "Starting server..."
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=4711 -Xms512M -Xmx1G -jar paper-195.jar nogui java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=25566 -Xms512M -Xmx1G -jar paper-195.jar nogui
pause pause

View File

@ -3,5 +3,5 @@ rm -rf world
echo "Attempting to copy plugin jar to plugins folder" echo "Attempting to copy plugin jar to plugins folder"
cp "..\target\IslandSurvivalCraft-1.0.0.jar" "plugins\IslandSurvivalCraft-1.0.0.jar" cp "..\target\IslandSurvivalCraft-1.0.0.jar" "plugins\IslandSurvivalCraft-1.0.0.jar"
echo "Starting server..." echo "Starting server..."
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=4711 -Xms512M -Xmx1G -jar paper-195.jar nogui java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=25566 -Xms512M -Xmx1G -jar paper-195.jar nogui
pause pause