From 2e67cb90caad89e72bd78ee4da826d6262fc8076 Mon Sep 17 00:00:00 2001 From: Harrison Date: Fri, 24 Apr 2020 15:10:03 -0500 Subject: [PATCH] Increased max cache size and fixed caching on method. --- .../world/IslandWorldMapper.java | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldMapper.java b/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldMapper.java index 990f1ca..4aecf85 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldMapper.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldMapper.java @@ -29,7 +29,7 @@ public class IslandWorldMapper implements CoordinateValidatable { dfs = new DepthFirstSearch(this); this.noiseGenerator = new SimplexOctaveGenerator(random, noiseOctaves); noiseGenerator.setScale(scale); - blockValueCache = new Cache<>(1024); + blockValueCache = new Cache<>(4096); } /** @@ -113,29 +113,30 @@ 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 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); - } else { - noise = Math.pow(rawNoise, islandValueExaggerationFactor) + shift; + CacheValue cacheVal = blockValueCache.retrieveCache(new CoordinateIdentifier(worldX, worldZ)); + if (cacheVal.isEmpty()) { + 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); + } else { + noise = Math.pow(rawNoise, islandValueExaggerationFactor) + shift; + } + double maxNeg = -1 + shift; + double maxPos = 1 + shift; + + double res = 0; + if (noise < 0) { + res = - noise / maxNeg; + } else { + res = noise / maxPos; + } + cacheVal.setValue(res); } - double maxNeg = -1 + shift; - double maxPos = 1 + shift; - double res = 0; - if (noise < 0) { - res = - noise / maxNeg; - } - res = noise / maxPos; - cacheVal.setValue(res); - return res; + return cacheVal.getValue(); } /** @@ -151,7 +152,7 @@ public class IslandWorldMapper implements CoordinateValidatable { if (!isIsland(firstBlockX, firstBlockZ)) return false; if (!isIsland(secondBlockX, secondBlockZ)) return false; dfs.setStartPosition(firstBlockX, firstBlockZ); - dfs.setDirectionPosition(secondBlockX, secondBlockZ); + dfs.setEndPosition(secondBlockX, secondBlockZ); boolean res = dfs.buildPathToEndNode(); dfs.deleteTree(); return res;