Added value caching to world mapper.

Largely untested.
This commit is contained in:
Harrison Deng 2020-04-24 00:01:03 -05:00
parent be19db1f9b
commit 1b5a03d72a

View File

@ -4,23 +4,32 @@ import java.util.Random;
import org.bukkit.util.noise.SimplexOctaveGenerator;
import ca.recrown.islandsurvivalcraft.caching.Cache;
import ca.recrown.islandsurvivalcraft.caching.CacheValue;
import ca.recrown.islandsurvivalcraft.caching.CoordinateIdentifier;
import ca.recrown.islandsurvivalcraft.pathfinding.CoordinateValidatable;
import ca.recrown.islandsurvivalcraft.pathfinding.DepthFirstSearch;
public class IslandWorldMapper implements CoordinateValidatable {
private Cache<Double> blockValueCache;
private SimplexOctaveGenerator noiseGenerator;
private final int noiseOctaves = 3;
private final float islandBlockGenerationPercent = 15f;
private final float exaggerationFactor = 0.15f;
private final double noiseFrequency = 0.6D;
private final double noiseAmplitude = 0.4D;
private final int noiseOctaves = 4;
private final float islandBlockGenerationPercent = 16;
private final float exaggerationFactor = 1.2f;
private final float islandValueExaggerationFactor = 0.2f;
private final double noiseFrequency = 1.95D;
private final double noiseAmplitude = 0.5D;
private final double scale = 0.03D;
private final float shoreFactor = 0.035f;
private final float shallowPortion = 0.01f;
private final float shallowPortion = 0.015f;
private final DepthFirstSearch dfs;
public IslandWorldMapper(Random random) {
dfs = new DepthFirstSearch(this);
this.noiseGenerator = new SimplexOctaveGenerator(random, noiseOctaves);
noiseGenerator.setScale(scale);
blockValueCache = new Cache<>(1024);
}
/**
@ -74,22 +83,20 @@ public class IslandWorldMapper implements CoordinateValidatable {
* @return true if these coordinates represent an island.
*/
public boolean isIsland(int worldX, int worldZ) {
if (isLand(worldX, worldZ) || isShallowPortion(worldX, worldZ)) {
return true;
}
return false;
return isLand(worldX, worldZ) || isShallowPortion(worldX, worldZ);
}
/**
* This block is considered a shallow portion of the island:
* The block height is less than the sea level,
* and that the block height is greater than the sea level - the shallow depth.
* and that the block height is greater than the shallow portion.
* If it is land, it is not shallow.
* @param worldX the X coordinate of location in world.
* @param worldZ the Z coordinate of location in world.
* @return true if it is considered the shallow portion.
*/
public boolean isShallowPortion(int worldX, int worldZ) {
if (getWorldBlockValue(worldX, worldZ) >= -shallowPortion) {
if (!isLand(worldX, worldZ) && getWorldBlockValue(worldX, worldZ) >= -shallowPortion) {
return true;
}
return false;
@ -106,21 +113,29 @@ public class IslandWorldMapper implements CoordinateValidatable {
* @return a value representing the island at the given point.
*/
public double getWorldBlockValue(int worldX, int worldZ) {
CoordinateIdentifier coords = new CoordinateIdentifier(worldX, worldZ);
CacheValue<Double> cacheVal = blockValueCache.retrieveCache(coords);
if (!cacheVal.isEmpty()) return cacheVal.getValue();
double portionSea = 1f - (this.islandBlockGenerationPercent / 100f);
double shift = 1f - 2 * portionSea;
double rawNoise = noiseGenerator.noise(worldX, worldZ, noiseFrequency, noiseAmplitude, true);
double noise = 0;
if (rawNoise < 0) {
noise = - Math.pow(rawNoise, exaggerationFactor) + shift;
noise = ( - Math.pow(- rawNoise, exaggerationFactor) + shift);
} else {
noise = Math.pow(rawNoise, exaggerationFactor) + shift;
noise = Math.pow(rawNoise, islandValueExaggerationFactor) + shift;
}
double maxNeg = -1 + shift;
double maxPos = 1 + shift;
double res = 0;
if (noise < 0) {
return - noise / maxNeg;
res = - noise / maxNeg;
}
return noise / maxPos;
res = noise / maxPos;
cacheVal.setValue(res);
return res;
}
/**