From b1663c040f6cbd49f4c5d7629b9c2eb79c226714 Mon Sep 17 00:00:00 2001 From: Dan Macbook Date: Thu, 13 Aug 2020 02:49:35 -0400 Subject: [PATCH] Noise fix --- .../iris/gen/TerrainChunkGenerator.java | 370 +++++++----------- .../volmit/iris/noise/CellHeightNoise.java | 2 +- .../java/com/volmit/iris/noise/Research.java | 29 ++ .../com/volmit/iris/noise/SimplexNoise.java | 45 ++- .../com/volmit/iris/noise/VascularNoise.java | 2 +- .../com/volmit/iris/object/IrisBiome.java | 215 ++++------ .../com/volmit/iris/object/NoiseStyle.java | 20 +- .../com/volmit/iris/util/ResourceLoader.java | 102 ++--- 8 files changed, 339 insertions(+), 446 deletions(-) create mode 100644 src/main/java/com/volmit/iris/noise/Research.java diff --git a/src/main/java/com/volmit/iris/gen/TerrainChunkGenerator.java b/src/main/java/com/volmit/iris/gen/TerrainChunkGenerator.java index 2e07f4ac2..b3d5327d8 100644 --- a/src/main/java/com/volmit/iris/gen/TerrainChunkGenerator.java +++ b/src/main/java/com/volmit/iris/gen/TerrainChunkGenerator.java @@ -31,8 +31,7 @@ import lombok.EqualsAndHashCode; @Data @EqualsAndHashCode(callSuper = false) -public abstract class TerrainChunkGenerator extends ParallelChunkGenerator -{ +public abstract class TerrainChunkGenerator extends ParallelChunkGenerator { private long lastUpdateRequest = M.ms(); private long lastChunkLoad = M.ms(); private GenLayerCave glCave; @@ -42,8 +41,7 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator private BiomeResult[] cacheTrueBiome; private IrisLock cacheLock; - public TerrainChunkGenerator(String dimensionName, int threads) - { + public TerrainChunkGenerator(String dimensionName, int threads) { super(dimensionName, threads); cacheHeightMap = new int[256]; cacheTrueBiome = new BiomeResult[256]; @@ -51,29 +49,25 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator cacheLock = new IrisLock("TerrainCacheLock"); } - public void onInit(World world, RNG rng) - { + public void onInit(World world, RNG rng) { super.onInit(world, rng); rockRandom = getMasterRandom().nextParallelRNG(2858678); glCave = new GenLayerCave(this, rng.nextParallelRNG(238948)); glCarve = new GenLayerCarve(this, rng.nextParallelRNG(968346576)); } - public KList getCaves(int x, int z) - { + public KList getCaves(int x, int z) { return glCave.genCaves(x, z, x & 15, z & 15, null); } @Override - protected void onGenerateColumn(int cx, int cz, int rx, int rz, int x, int z, AtomicSliver sliver, BiomeMap biomeMap, boolean sampled) - { - if(x > 15 || x < 0 || z > 15 || z < 0) - { + protected void onGenerateColumn(int cx, int cz, int rx, int rz, int x, int z, AtomicSliver sliver, + BiomeMap biomeMap, boolean sampled) { + if (x > 15 || x < 0 || z > 15 || z < 0) { throw new RuntimeException("Invalid OnGenerate call: x:" + x + " z:" + z); } - try - { + try { int highestPlaced = 0; BlockData block; int fluidHeight = getDimension().getFluidHeight(); @@ -89,29 +83,27 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator BiomeResult biomeResult = sampleTrueBiome(rx, rz, noise); IrisBiome biome = biomeResult.getBiome(); - if(biome == null) - { + if (biome == null) { throw new RuntimeException("Null Biome!"); } cacheBiome(cachingAllowed && !sampled, x, z, biomeResult, height); cacheInternalBiome(cachingAllowed && !sampled, x, z, biome); - KList layers = biome.generateLayers(wx, wz, masterRandom, height, height - getFluidHeight()); - KList seaLayers = biome.isSea() ? biome.generateSeaLayers(wx, wz, masterRandom, fluidHeight - height) : new KList<>(); + KList layers = biome.generateLayers(rx, rz, masterRandom, height, height - getFluidHeight()); + KList seaLayers = biome.isSea() + ? biome.generateSeaLayers(rx, rz, masterRandom, fluidHeight - height) + : new KList<>(); boolean caverning = false; KList cavernHeights = new KList<>(); int lastCavernHeight = -1; // From Height to Bedrock - for(int k = Math.max(height, fluidHeight); k >= 0; k--) - { + for (int k = Math.max(height, fluidHeight); k >= 0; k--) { boolean cavernSurface = false; // Bedrock - if(k == 0) - { - if(biomeMap != null) - { + if (k == 0) { + if (biomeMap != null) { sliver.set(k, biome.getDerivative()); biomeMap.setBiome(x, z, biome); } @@ -121,10 +113,8 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator } // Carving - if(carvable && glCarve.isCarved(rx, k, rz)) - { - if(biomeMap != null) - { + if (carvable && glCarve.isCarved(rx, k, rz)) { + if (biomeMap != null) { sliver.set(k, biome.getDerivative()); biomeMap.setBiome(x, z, biome); } @@ -134,8 +124,7 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator continue; } - else if(carvable && caverning) - { + else if (carvable && caverning) { lastCavernHeight = k; cavernSurface = true; cavernHeights.add(k); @@ -145,27 +134,24 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator boolean underwater = k > height && k <= fluidHeight; // Set Biome - if(biomeMap != null) - { + if (biomeMap != null) { sliver.set(k, biome.getGroundBiome(masterRandom, rz, k, rx)); biomeMap.setBiome(x, z, biome); } // Set Sea Material (water/lava) - if(underwater) - { - block = seaLayers.hasIndex(fluidHeight - k) ? layers.get(depth) : getDimension().getFluid(rockRandom, wx, k, wz); + if (underwater) { + block = seaLayers.hasIndex(fluidHeight - k) ? layers.get(depth) + : getDimension().getFluid(rockRandom, wx, k, wz); } // Set Surface Material for cavern layer surfaces - else if(layers.hasIndex(lastCavernHeight - k)) - { + else if (layers.hasIndex(lastCavernHeight - k)) { block = layers.get(lastCavernHeight - k); } // Set Surface Material for true surface - else - { + else { block = layers.hasIndex(depth) ? layers.get(depth) : getDimension().getRock(rockRandom, wx, k, wz); depth++; } @@ -175,14 +161,13 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator highestPlaced = Math.max(highestPlaced, k); // Decorate underwater surface - if(!cavernSurface && (k == height && block.getMaterial().isSolid() && k < fluidHeight)) - { + if (!cavernSurface && (k == height && block.getMaterial().isSolid() && k < fluidHeight)) { decorateUnderwater(biome, sliver, wx, k, wz, rx, rz, block); } // Decorate Cavern surfaces, but not the true surface - if((carvable && cavernSurface) && !(k == Math.max(height, fluidHeight) && block.getMaterial().isSolid() && k < 255 && k >= fluidHeight)) - { + if ((carvable && cavernSurface) && !(k == Math.max(height, fluidHeight) && block.getMaterial().isSolid() + && k < 255 && k >= fluidHeight)) { decorateLand(biome, sliver, wx, k, wz, rx, rz, block); } } @@ -192,36 +177,31 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator IrisBiome caveBiome = glBiome.generateData(InferredType.CAVE, wx, wz, rx, rz, region).getBiome(); // Decorate Cave Biome Height Sections - if(caveBiome != null) - { - for(CaveResult i : caveResults) - { - for(int j = i.getFloor(); j <= i.getCeiling(); j++) - { + if (caveBiome != null) { + for (CaveResult i : caveResults) { + for (int j = i.getFloor(); j <= i.getCeiling(); j++) { sliver.set(j, caveBiome); sliver.set(j, caveBiome.getGroundBiome(masterRandom, rz, j, rx)); } - KList floor = caveBiome.generateLayers(wx, wz, rockRandom, i.getFloor() - 2, i.getFloor() - 2); - KList ceiling = caveBiome.generateLayers(wx + 256, wz + 256, rockRandom, height - i.getCeiling() - 2, height - i.getCeiling() - 2); + KList floor = caveBiome.generateLayers(wx, wz, rockRandom, i.getFloor() - 2, + i.getFloor() - 2); + KList ceiling = caveBiome.generateLayers(wx + 256, wz + 256, rockRandom, + height - i.getCeiling() - 2, height - i.getCeiling() - 2); BlockData blockc = null; - for(int j = 0; j < floor.size(); j++) - { - if(j == 0) - { + for (int j = 0; j < floor.size(); j++) { + if (j == 0) { blockc = floor.get(j); } sliver.set(i.getFloor() - j, floor.get(j)); } - for(int j = ceiling.size() - 1; j > 0; j--) - { + for (int j = ceiling.size() - 1; j > 0; j--) { sliver.set(i.getCeiling() + j, ceiling.get(j)); } - if(blockc != null && !sliver.isSolid(i.getFloor() + 1)) - { + if (blockc != null && !sliver.isSolid(i.getFloor() + 1)) { decorateCave(caveBiome, sliver, wx, i.getFloor(), wz, rx, rz, blockc); } } @@ -230,122 +210,100 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator block = sliver.get(Math.max(height, fluidHeight)); // Decorate True Surface - if(block.getMaterial().isSolid()) - { + if (block.getMaterial().isSolid()) { decorateLand(biome, sliver, wx, Math.max(height, fluidHeight), wz, rx, rz, block); } // Update Height Map - if(!sampled && cachingAllowed && highestPlaced < height) - { + if (!sampled && cachingAllowed && highestPlaced < height) { cacheHeightMap[(z << 4) | x] = highestPlaced; } } - catch(Throwable e) - { + catch (Throwable e) { fail(e); } } - protected void cacheInternalBiome(boolean b, int x, int z, IrisBiome bv) - { - if(b) - { + protected void cacheInternalBiome(boolean b, int x, int z, IrisBiome bv) { + if (b) { cacheInternalBiome(x, z, bv); } } - private void cacheBiome(boolean b, int x, int z, BiomeResult biomeResult, int height) - { - if(b) - { - try - { + private void cacheBiome(boolean b, int x, int z, BiomeResult biomeResult, int height) { + if (b) { + try { cacheTrueBiome[(z << 4) | x] = biomeResult; cacheHeightMap[(z << 4) | x] = height; } - catch(Throwable e) - { + catch (Throwable e) { Iris.error("Failed to write cache at " + x + " " + z); } } } @Override - protected void onGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid) - { + protected void onGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid) { super.onGenerate(random, x, z, data, grid); RNG ro = random.nextParallelRNG((x * x * x) - z); IrisRegion region = sampleRegion((x * 16) + 7, (z * 16) + 7); IrisBiome biome = sampleTrueBiome((x * 16) + 7, (z * 16) + 7).getBiome(); - for(IrisDepositGenerator k : getDimension().getDeposits()) - { + for (IrisDepositGenerator k : getDimension().getDeposits()) { k.generate(data, ro, this); } - for(IrisDepositGenerator k : region.getDeposits()) - { - for(int l = 0; l < ro.i(k.getMinPerChunk(), k.getMaxPerChunk()); l++) - { + for (IrisDepositGenerator k : region.getDeposits()) { + for (int l = 0; l < ro.i(k.getMinPerChunk(), k.getMaxPerChunk()); l++) { k.generate(data, ro, this); } } - for(IrisDepositGenerator k : biome.getDeposits()) - { - for(int l = 0; l < ro.i(k.getMinPerChunk(), k.getMaxPerChunk()); l++) - { + for (IrisDepositGenerator k : biome.getDeposits()) { + for (int l = 0; l < ro.i(k.getMinPerChunk(), k.getMaxPerChunk()); l++) { k.generate(data, ro, this); } } } - private void decorateLand(IrisBiome biome, AtomicSliver sliver, double wx, int k, double wz, int rx, int rz, BlockData block) - { - if(!getDimension().isDecorate()) - { + private void decorateLand(IrisBiome biome, AtomicSliver sliver, double wx, int k, double wz, int rx, int rz, + BlockData block) { + if (!getDimension().isDecorate()) { return; } int j = 0; - for(IrisBiomeDecorator i : biome.getDecorators()) - { - if(i.getPartOf().equals(DecorationPart.SHORE_LINE) && (!touchesSea(rx, rz) || k != getFluidHeight())) - { + for (IrisBiomeDecorator i : biome.getDecorators()) { + if (i.getPartOf().equals(DecorationPart.SHORE_LINE) && (!touchesSea(rx, rz) || k != getFluidHeight())) { continue; } - BlockData d = i.getBlockData(getMasterRandom().nextParallelRNG((int) (38888 + biome.getRarity() + biome.getName().length() + j++)), wx, wz); + BlockData d = i.getBlockData(getMasterRandom() + .nextParallelRNG((int) (38888 + biome.getRarity() + biome.getName().length() + j++)), rx, rz); - if(d != null) - { - if(!B.canPlaceOnto(d.getMaterial(), block.getMaterial())) - { + if (d != null) { + if (!B.canPlaceOnto(d.getMaterial(), block.getMaterial())) { continue; } - if(d.getMaterial().equals(Material.CACTUS)) - { - if(!block.getMaterial().equals(Material.SAND) && !block.getMaterial().equals(Material.RED_SAND)) - { + if (d.getMaterial().equals(Material.CACTUS)) { + if (!block.getMaterial().equals(Material.SAND) && !block.getMaterial().equals(Material.RED_SAND)) { sliver.set(k, B.getBlockData("RED_SAND")); } } - if(d.getMaterial().equals(Material.WHEAT) || d.getMaterial().equals(Material.CARROTS) || d.getMaterial().equals(Material.POTATOES) || d.getMaterial().equals(Material.MELON_STEM) || d.getMaterial().equals(Material.PUMPKIN_STEM)) - { - if(!block.getMaterial().equals(Material.FARMLAND)) - { + if (d.getMaterial().equals(Material.WHEAT) || d.getMaterial().equals(Material.CARROTS) + || d.getMaterial().equals(Material.POTATOES) || d.getMaterial().equals(Material.MELON_STEM) + || d.getMaterial().equals(Material.PUMPKIN_STEM)) { + if (!block.getMaterial().equals(Material.FARMLAND)) { sliver.set(k, B.getBlockData("FARMLAND")); } } - if(d instanceof Bisected && k < 254) - { + if (d instanceof Bisected && k < 254) { Bisected t = ((Bisected) d.clone()); t.setHalf(Half.TOP); Bisected b = ((Bisected) d.clone()); @@ -354,19 +312,17 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator sliver.set(k + 2, t); } - else - { - int stack = i.getHeight(getMasterRandom().nextParallelRNG((int) (39456 + (10000 * i.getChance()) + i.getStackMax() + i.getStackMin() + i.getZoom())), wx, wz); + else { + int stack = i.getHeight(getMasterRandom().nextParallelRNG( + (int) (39456 + (10000 * i.getChance()) + i.getStackMax() + i.getStackMin() + i.getZoom())), + rx, rz); - if(stack == 1) - { + if (stack == 1) { sliver.set(k + 1, d); } - else if(k < 255 - stack) - { - for(int l = 0; l < stack; l++) - { + else if (k < 255 - stack) { + for (int l = 0; l < stack; l++) { sliver.set(k + l + 1, d); } } @@ -377,36 +333,31 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator } } - private void decorateCave(IrisBiome biome, AtomicSliver sliver, double wx, int k, double wz, int rx, int rz, BlockData block) - { - if(!getDimension().isDecorate()) - { + private void decorateCave(IrisBiome biome, AtomicSliver sliver, double wx, int k, double wz, int rx, int rz, + BlockData block) { + if (!getDimension().isDecorate()) { return; } int j = 0; - for(IrisBiomeDecorator i : biome.getDecorators()) - { - BlockData d = i.getBlockData(getMasterRandom().nextParallelRNG(2333877 + biome.getRarity() + biome.getName().length() + +j++), wx, wz); + for (IrisBiomeDecorator i : biome.getDecorators()) { + BlockData d = i.getBlockData( + getMasterRandom().nextParallelRNG(2333877 + biome.getRarity() + biome.getName().length() + +j++), + rx, rz); - if(d != null) - { - if(!B.canPlaceOnto(d.getMaterial(), block.getMaterial())) - { + if (d != null) { + if (!B.canPlaceOnto(d.getMaterial(), block.getMaterial())) { continue; } - if(d.getMaterial().equals(Material.CACTUS)) - { - if(!block.getMaterial().equals(Material.SAND) && !block.getMaterial().equals(Material.RED_SAND)) - { + if (d.getMaterial().equals(Material.CACTUS)) { + if (!block.getMaterial().equals(Material.SAND) && !block.getMaterial().equals(Material.RED_SAND)) { sliver.set(k, B.getBlockData("SAND")); } } - if(d instanceof Bisected && k < 254) - { + if (d instanceof Bisected && k < 254) { Bisected t = ((Bisected) d.clone()); t.setHalf(Half.TOP); Bisected b = ((Bisected) d.clone()); @@ -415,21 +366,17 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator sliver.set(k + 2, t); } - else - { - int stack = i.getHeight(getMasterRandom().nextParallelRNG((int) (39456 + (1000 * i.getChance()) + i.getZoom() * 10)), wx, wz); + else { + int stack = i.getHeight(getMasterRandom() + .nextParallelRNG((int) (39456 + (1000 * i.getChance()) + i.getZoom() * 10)), rx, rz); - if(stack == 1) - { + if (stack == 1) { sliver.set(k + 1, d); } - else if(k < 255 - stack) - { - for(int l = 0; l < stack; l++) - { - if(sliver.isSolid(k + l + 1)) - { + else if (k < 255 - stack) { + for (int l = 0; l < stack; l++) { + if (sliver.isSolid(k + l + 1)) { break; } @@ -443,38 +390,35 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator } } - private void decorateUnderwater(IrisBiome biome, AtomicSliver sliver, double wx, int y, double wz, int rx, int rz, BlockData block) - { - if(!getDimension().isDecorate()) - { + private void decorateUnderwater(IrisBiome biome, AtomicSliver sliver, double wx, int y, double wz, int rx, int rz, + BlockData block) { + if (!getDimension().isDecorate()) { return; } int j = 0; - for(IrisBiomeDecorator i : biome.getDecorators()) - { - if(biome.getInferredType().equals(InferredType.SHORE)) - { + for (IrisBiomeDecorator i : biome.getDecorators()) { + if (biome.getInferredType().equals(InferredType.SHORE)) { continue; } - BlockData d = i.getBlockData(getMasterRandom().nextParallelRNG(2555 + biome.getRarity() + biome.getName().length() + j++), wx, wz); + BlockData d = i.getBlockData( + getMasterRandom().nextParallelRNG(2555 + biome.getRarity() + biome.getName().length() + j++), rx, + rz); - if(d != null) - { - int stack = i.getHeight(getMasterRandom().nextParallelRNG((int) (239456 + i.getStackMax() + i.getStackMin() + i.getVerticalZoom() + i.getZoom() + i.getBlockData().size() + j)), wx, wz); + if (d != null) { + int stack = i.getHeight(getMasterRandom().nextParallelRNG((int) (239456 + i.getStackMax() + + i.getStackMin() + i.getVerticalZoom() + i.getZoom() + i.getBlockData().size() + j)), rx, rz); - if(stack == 1) - { + if (stack == 1) { sliver.set(i.getPartOf().equals(DecorationPart.SEA_SURFACE) ? (getFluidHeight() + 1) : (y + 1), d); } - else if(y < getFluidHeight() - stack) - { - for(int l = 0; l < stack; l++) - { - sliver.set(i.getPartOf().equals(DecorationPart.SEA_SURFACE) ? (getFluidHeight() + 1 + l) : (y + l + 1), d); + else if (y < getFluidHeight() - stack) { + for (int l = 0; l < stack; l++) { + sliver.set(i.getPartOf().equals(DecorationPart.SEA_SURFACE) ? (getFluidHeight() + 1 + l) + : (y + l + 1), d); } } @@ -484,33 +428,30 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator } @Override - protected void onPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, BiomeMap biomeMap) - { + protected void onPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, + BiomeMap biomeMap) { onPreParallaxPostGenerate(random, x, z, data, grid, height, biomeMap); } - protected void onPreParallaxPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, BiomeMap biomeMap) - { + protected void onPreParallaxPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, + BiomeMap biomeMap) { } - protected void onPostParallaxPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, BiomeMap biomeMap) - { + protected void onPostParallaxPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, + HeightMap height, BiomeMap biomeMap) { } - protected double getNoiseHeight(int rx, int rz) - { + protected double getNoiseHeight(int rx, int rz) { double wx = getZoomed(rx); double wz = getZoomed(rz); return getBiomeHeight(wx, wz, rx, rz); } - public BiomeResult sampleTrueBiomeBase(int x, int z, int height) - { - if(!getDimension().getFocus().equals("")) - { + public BiomeResult sampleTrueBiomeBase(int x, int z, int height) { + if (!getDimension().getFocus().equals("")) { return focus(); } @@ -520,51 +461,42 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator double sh = region.getShoreHeight(wx, wz); IrisBiome current = sampleBiome(x, z).getBiome(); - if(current.isShore() && height > sh) - { + if (current.isShore() && height > sh) { return glBiome.generateData(InferredType.LAND, wx, wz, x, z, region); } - if(current.isShore() || current.isLand() && height <= getDimension().getFluidHeight()) - { + if (current.isShore() || current.isLand() && height <= getDimension().getFluidHeight()) { return glBiome.generateData(InferredType.SEA, wx, wz, x, z, region); } - if(current.isSea() && height > getDimension().getFluidHeight()) - { + if (current.isSea() && height > getDimension().getFluidHeight()) { return glBiome.generateData(InferredType.LAND, wx, wz, x, z, region); } - if(height <= getDimension().getFluidHeight()) - { + if (height <= getDimension().getFluidHeight()) { return glBiome.generateData(InferredType.SEA, wx, wz, x, z, region); } - if(height <= getDimension().getFluidHeight() + sh) - { + if (height <= getDimension().getFluidHeight() + sh) { return glBiome.generateData(InferredType.SHORE, wx, wz, x, z, region); } return glBiome.generateRegionData(wx, wz, x, z, region); } - public BiomeResult sampleCaveBiome(int x, int z) - { + public BiomeResult sampleCaveBiome(int x, int z) { double wx = getModifiedX(x, z); double wz = getModifiedZ(x, z); return glBiome.generateData(InferredType.CAVE, wx, wz, x, z, sampleRegion(x, z)); } - public BiomeResult sampleTrueBiome(int x, int y, int z) - { - if(y < getTerrainHeight(x, z)) - { + public BiomeResult sampleTrueBiome(int x, int y, int z) { + if (y < getTerrainHeight(x, z)) { double wx = getModifiedX(x, z); double wz = getModifiedZ(x, z); BiomeResult r = glBiome.generateData(InferredType.CAVE, wx, wz, x, z, sampleRegion(x, z)); - if(r.getBiome() != null) - { + if (r.getBiome() != null) { return r; } } @@ -572,20 +504,16 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator return sampleTrueBiome(x, z); } - public BiomeResult sampleTrueBiome(int x, int z) - { + public BiomeResult sampleTrueBiome(int x, int z) { return sampleTrueBiome(x, z, getTerrainHeight(x, z)); } - public BiomeResult sampleTrueBiome(int x, int z, double noise) - { - if(!getDimension().getFocus().equals("")) - { + public BiomeResult sampleTrueBiome(int x, int z, double noise) { + if (!getDimension().getFocus().equals("")) { return focus(); } - if(isSafe() && x >> 4 == cacheX && z >> 4 == cacheZ) - { + if (isSafe() && x >> 4 == cacheX && z >> 4 == cacheZ) { return cacheTrueBiome[((z & 15) << 4) | (x & 15)]; } @@ -597,8 +525,7 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator BiomeResult res = sampleTrueBiomeBase(x, z, height); IrisBiome current = res.getBiome(); - if(current.isSea() && height > getDimension().getFluidHeight() - sh) - { + if (current.isSea() && height > getDimension().getFluidHeight() - sh) { return glBiome.generateData(InferredType.SHORE, wx, wz, x, z, region); } @@ -606,46 +533,39 @@ public abstract class TerrainChunkGenerator extends ParallelChunkGenerator } @Override - protected int onSampleColumnHeight(int cx, int cz, int rx, int rz, int x, int z) - { + protected int onSampleColumnHeight(int cx, int cz, int rx, int rz, int x, int z) { int fluidHeight = getDimension().getFluidHeight(); double noise = getNoiseHeight(rx, rz); return (int) Math.round(noise) + fluidHeight; } - private boolean touchesSea(int rx, int rz) - { - return isFluidAtHeight(rx + 1, rz) || isFluidAtHeight(rx - 1, rz) || isFluidAtHeight(rx, rz - 1) || isFluidAtHeight(rx, rz + 1); + private boolean touchesSea(int rx, int rz) { + return isFluidAtHeight(rx + 1, rz) || isFluidAtHeight(rx - 1, rz) || isFluidAtHeight(rx, rz - 1) + || isFluidAtHeight(rx, rz + 1); } - public boolean isUnderwater(int x, int z) - { + public boolean isUnderwater(int x, int z) { return isFluidAtHeight(x, z); } - public boolean isFluidAtHeight(int x, int z) - { + public boolean isFluidAtHeight(int x, int z) { return Math.round(getTerrainHeight(x, z)) < getFluidHeight(); } - public int getFluidHeight() - { + public int getFluidHeight() { return getDimension().getFluidHeight(); } - public double getTerrainHeight(int x, int z) - { - if(isSafe() && x >> 4 == cacheX && z >> 4 == cacheZ) - { + public double getTerrainHeight(int x, int z) { + if (isSafe() && x >> 4 == cacheX && z >> 4 == cacheZ) { return cacheHeightMap[((z & 15) << 4) | (x & 15)]; } return getNoiseHeight(x, z) + getFluidHeight(); } - public double getTerrainWaterHeight(int x, int z) - { + public double getTerrainWaterHeight(int x, int z) { return Math.max(getTerrainHeight(x, z), getFluidHeight()); } } diff --git a/src/main/java/com/volmit/iris/noise/CellHeightNoise.java b/src/main/java/com/volmit/iris/noise/CellHeightNoise.java index 004b2a6ae..5938de989 100644 --- a/src/main/java/com/volmit/iris/noise/CellHeightNoise.java +++ b/src/main/java/com/volmit/iris/noise/CellHeightNoise.java @@ -13,7 +13,7 @@ public class CellHeightNoise implements NoiseGenerator { } private double filter(double noise) { - return M.clip((noise / 2D) + 0.5D, 0D, 1D); + return M.clip(1D - ((noise / 2D) + 0.5D), 0D, 1D); } @Override diff --git a/src/main/java/com/volmit/iris/noise/Research.java b/src/main/java/com/volmit/iris/noise/Research.java new file mode 100644 index 000000000..a74b3230b --- /dev/null +++ b/src/main/java/com/volmit/iris/noise/Research.java @@ -0,0 +1,29 @@ +package com.volmit.iris.noise; + +import com.volmit.iris.object.NoiseStyle; +import com.volmit.iris.util.RNG; + +public class Research { + + public static void main(String[] args) { + CNG cng = NoiseStyle.VIGOCTAVE_SIMPLEX.create(new RNG(RNG.r.nextLong())); + + double max = -1; + double min = 2; + + for (int i = 0; i < 999999; i++) { + double n = cng.noise(i, i * 2, i * 4); + + if (n < min) { + min = n; + } + + if (n > max) { + max = n; + } + } + + System.out.println(min + " - " + max); + } + +} diff --git a/src/main/java/com/volmit/iris/noise/SimplexNoise.java b/src/main/java/com/volmit/iris/noise/SimplexNoise.java index 888d5f873..0f3513d40 100644 --- a/src/main/java/com/volmit/iris/noise/SimplexNoise.java +++ b/src/main/java/com/volmit/iris/noise/SimplexNoise.java @@ -3,11 +3,11 @@ package com.volmit.iris.noise; import com.volmit.iris.util.RNG; public class SimplexNoise implements NoiseGenerator, OctaveNoise { - private final SNG n; + private final FastNoise n; private int octaves; public SimplexNoise(long seed) { - this.n = new SNG(new RNG(seed)); + this.n = new FastNoise(new RNG(seed).imax()); octaves = 1; } @@ -18,26 +18,55 @@ public class SimplexNoise implements NoiseGenerator, OctaveNoise { @Override public double noise(double x) { if (octaves <= 1) { - return f(n.noise(x)); + return f(n.GetNoise((float) x, 0f)); } - return f(n.noise(x, octaves, 1D, 1D, false)); + double f = 1; + double m = 0; + double v = 0; + + for (int i = 0; i < octaves; i++) { + v += n.GetNoise((float) (x * (f == 1 ? f++ : (f *= 2))), 0f) * f; + m += f; + } + + return f(v / m); } @Override public double noise(double x, double z) { if (octaves <= 1) { - return f(n.noise(x, z)); + return f(n.GetNoise((float) x, (float) z)); } - return f(n.noise(x, z, octaves, 1D, 1D, true)); + double f = 1; + double m = 0; + double v = 0; + + for (int i = 0; i < octaves; i++) { + f = f == 1 ? f + 1 : f * 2; + v += n.GetNoise((float) (x * f), (float) (z * f)) * f; + m += f; + } + + return f(v / m); } @Override public double noise(double x, double y, double z) { if (octaves <= 1) { - return f(n.noise(x, y, z)); + return f(n.GetNoise((float) x, (float) y, (float) z)); } - return f(n.noise(x, y, z, octaves, 1D, 1D, true)); + double f = 1; + double m = 0; + double v = 0; + + for (int i = 0; i < octaves; i++) { + f = f == 1 ? f + 1 : f * 2; + v += n.GetNoise((float) (x * f), (float) (y * f), (float) (z * f)) * f; + m += f; + } + + return f(v / m); } @Override diff --git a/src/main/java/com/volmit/iris/noise/VascularNoise.java b/src/main/java/com/volmit/iris/noise/VascularNoise.java index da3b3c203..e64cd4085 100644 --- a/src/main/java/com/volmit/iris/noise/VascularNoise.java +++ b/src/main/java/com/volmit/iris/noise/VascularNoise.java @@ -13,7 +13,7 @@ public class VascularNoise implements NoiseGenerator { } private double filter(double noise) { - return M.clip(1D - ((noise / 2D) + 0.5D), 0D, 1D); + return M.clip((noise / 2D) + 0.5D, 0D, 1D); } @Override diff --git a/src/main/java/com/volmit/iris/object/IrisBiome.java b/src/main/java/com/volmit/iris/object/IrisBiome.java index 2ca446564..41901cb6d 100644 --- a/src/main/java/com/volmit/iris/object/IrisBiome.java +++ b/src/main/java/com/volmit/iris/object/IrisBiome.java @@ -26,8 +26,7 @@ import lombok.EqualsAndHashCode; @Desc("Represents a biome in iris. Biomes are placed inside of regions and hold objects.") @Data @EqualsAndHashCode(callSuper = false) -public class IrisBiome extends IrisRegistrant implements IRare -{ +public class IrisBiome extends IrisRegistrant implements IRare { @MinNumber(2) @Required @DontObfuscate @@ -115,7 +114,8 @@ public class IrisBiome extends IrisRegistrant implements IRare @ArrayType(min = 1, type = IrisBiomeGeneratorLink.class) @DontObfuscate @Desc("Generators for this biome. Multiple generators with different interpolation sizes will mix with other biomes how you would expect. This defines your biome height relative to the fluid height. Use negative for oceans.") - private KList generators = new KList().qadd(new IrisBiomeGeneratorLink()); + private KList generators = new KList() + .qadd(new IrisBiomeGeneratorLink()); @ArrayType(min = 1, type = IrisStructurePlacement.class) @DontObfuscate @@ -135,85 +135,71 @@ public class IrisBiome extends IrisRegistrant implements IRare private transient AtomicCache> layerHeightGenerators = new AtomicCache<>(); private transient AtomicCache> layerSeaHeightGenerators = new AtomicCache<>(); - public IrisBiome() - { + public IrisBiome() { } - public double getHeight(ContextualChunkGenerator xg, double x, double z, long seed) - { + public double getHeight(ContextualChunkGenerator xg, double x, double z, long seed) { double height = 0; - for(IrisBiomeGeneratorLink i : generators) - { + for (IrisBiomeGeneratorLink i : generators) { height += i.getHeight(xg, x, z, seed); } return Math.max(0, Math.min(height, 255)); } - public CNG getBiomeGenerator(RNG random) - { - return biomeGenerator.aquire(() -> - { + public CNG getBiomeGenerator(RNG random) { + return biomeGenerator.aquire(() -> { return biomeStyle.create(random.nextParallelRNG(213949 + 228888 + getRarity() + getName().length())); }); } - public RarityCellGenerator getChildrenGenerator(RNG random, int sig, double scale) - { - return childrenCell.aquire(() -> - { - RarityCellGenerator childrenCell = new RarityCellGenerator(random.nextParallelRNG(sig * 2137)); + public RarityCellGenerator getChildrenGenerator(RNG random, int sig, double scale) { + return childrenCell.aquire(() -> { + RarityCellGenerator childrenCell = new RarityCellGenerator( + random.nextParallelRNG(sig * 2137)); childrenCell.setCellScale(scale); return childrenCell; }); } - public KList generateLayers(double wx, double wz, RNG random, int maxDepth, int height) - { - if(isLockLayers()) - { + public KList generateLayers(double wx, double wz, RNG random, int maxDepth, int height) { + if (isLockLayers()) { return generateLockedLayers(wx, wz, random, maxDepth, height); } KList data = new KList<>(); - if(maxDepth <= 0) - { + if (maxDepth <= 0) { return data; } - for(int i = 0; i < layers.size(); i++) - { + for (int i = 0; i < layers.size(); i++) { CNG hgen = getLayerHeightGenerators(random).get(i); - int d = hgen.fit(layers.get(i).getMinHeight(), layers.get(i).getMaxHeight(), wx / layers.get(i).getZoom(), wz / layers.get(i).getZoom()); + int d = hgen.fit(layers.get(i).getMinHeight(), layers.get(i).getMaxHeight(), wx / layers.get(i).getZoom(), + wz / layers.get(i).getZoom()); - if(d < 0) - { + if (d < 0) { continue; } - for(int j = 0; j < d; j++) - { - if(data.size() >= maxDepth) - { + for (int j = 0; j < d; j++) { + if (data.size() >= maxDepth) { break; } - try - { - data.add(getLayers().get(i).get(random.nextParallelRNG(i + j), (wx + j) / layers.get(i).getZoom(), j, (wz - j) / layers.get(i).getZoom())); + try { + data.add(getLayers().get(i).get(random.nextParallelRNG(i + j), (wx + j) / layers.get(i).getZoom(), + j, (wz - j) / layers.get(i).getZoom())); } - catch(Throwable e) - { + catch (Throwable e) { e.printStackTrace(); } } - if(data.size() >= maxDepth) - { + if (data.size() >= maxDepth) { break; } } @@ -221,47 +207,40 @@ public class IrisBiome extends IrisRegistrant implements IRare return data; } - public KList generateLockedLayers(double wx, double wz, RNG random, int maxDepth, int height) - { + public KList generateLockedLayers(double wx, double wz, RNG random, int maxDepth, int height) { KList data = new KList<>(); KList real = new KList<>(); - if(maxDepth <= 0) - { + if (maxDepth <= 0) { return data; } - for(int i = 0; i < layers.size(); i++) - { + for (int i = 0; i < layers.size(); i++) { CNG hgen = getLayerHeightGenerators(random).get(i); - int d = hgen.fit(layers.get(i).getMinHeight(), layers.get(i).getMaxHeight(), wx / layers.get(i).getZoom(), wz / layers.get(i).getZoom()); + int d = hgen.fit(layers.get(i).getMinHeight(), layers.get(i).getMaxHeight(), wx / layers.get(i).getZoom(), + wz / layers.get(i).getZoom()); - if(d < 0) - { + if (d < 0) { continue; } - for(int j = 0; j < d; j++) - { - try - { - data.add(getLayers().get(i).get(random.nextParallelRNG(i + j), (wx + j) / layers.get(i).getZoom(), j, (wz - j) / layers.get(i).getZoom())); + for (int j = 0; j < d; j++) { + try { + data.add(getLayers().get(i).get(random.nextParallelRNG(i + j), (wx + j) / layers.get(i).getZoom(), + j, (wz - j) / layers.get(i).getZoom())); } - catch(Throwable e) - { + catch (Throwable e) { e.printStackTrace(); } } } - if(data.isEmpty()) - { + if (data.isEmpty()) { return real; } - for(int i = 0; i < maxDepth; i++) - { + for (int i = 0; i < maxDepth; i++) { int offset = (getMaxHeight() - height) - i; int index = offset % data.size(); real.add(data.get(index < 0 ? 0 : index)); @@ -270,14 +249,11 @@ public class IrisBiome extends IrisRegistrant implements IRare return real; } - private int getMaxHeight() - { - return maxHeight.aquire(() -> - { + private int getMaxHeight() { + return maxHeight.aquire(() -> { int maxHeight = 0; - for(IrisBiomeGeneratorLink i : getGenerators()) - { + for (IrisBiomeGeneratorLink i : getGenerators()) { maxHeight += i.getMax(); } @@ -285,46 +261,39 @@ public class IrisBiome extends IrisRegistrant implements IRare }); } - public IrisBiome infer(InferredType t, InferredType type) - { + public IrisBiome infer(InferredType t, InferredType type) { setInferredType(t.equals(InferredType.DEFER) ? type : t); return this; } - public KList generateSeaLayers(double wx, double wz, RNG random, int maxDepth) - { + public KList generateSeaLayers(double wx, double wz, RNG random, int maxDepth) { KList data = new KList<>(); - for(int i = 0; i < seaLayers.size(); i++) - { + for (int i = 0; i < seaLayers.size(); i++) { CNG hgen = getLayerSeaHeightGenerators(random).get(i); - int d = hgen.fit(seaLayers.get(i).getMinHeight(), seaLayers.get(i).getMaxHeight(), wx / seaLayers.get(i).getZoom(), wz / seaLayers.get(i).getZoom()); + int d = hgen.fit(seaLayers.get(i).getMinHeight(), seaLayers.get(i).getMaxHeight(), + wx / seaLayers.get(i).getZoom(), wz / seaLayers.get(i).getZoom()); - if(d < 0) - { + if (d < 0) { continue; } - for(int j = 0; j < d; j++) - { - if(data.size() >= maxDepth) - { + for (int j = 0; j < d; j++) { + if (data.size() >= maxDepth) { break; } - try - { - data.add(getSeaLayers().get(i).get(random.nextParallelRNG(i + j), (wx + j) / seaLayers.get(i).getZoom(), j, (wz - j) / seaLayers.get(i).getZoom())); + try { + data.add(getSeaLayers().get(i).get(random.nextParallelRNG(i + j), + (wx + j) / seaLayers.get(i).getZoom(), j, (wz - j) / seaLayers.get(i).getZoom())); } - catch(Throwable e) - { + catch (Throwable e) { e.printStackTrace(); } } - if(data.size() >= maxDepth) - { + if (data.size() >= maxDepth) { break; } } @@ -332,16 +301,13 @@ public class IrisBiome extends IrisRegistrant implements IRare return data; } - public KList getLayerHeightGenerators(RNG rng) - { - return layerHeightGenerators.aquire(() -> - { + public KList getLayerHeightGenerators(RNG rng) { + return layerHeightGenerators.aquire(() -> { KList layerHeightGenerators = new KList<>(); int m = 7235; - for(IrisBiomePaletteLayer i : getLayers()) - { + for (IrisBiomePaletteLayer i : getLayers()) { layerHeightGenerators.add(i.getHeightGenerator(rng.nextParallelRNG((m++) * m * m * m))); } @@ -349,16 +315,13 @@ public class IrisBiome extends IrisRegistrant implements IRare }); } - public KList getLayerSeaHeightGenerators(RNG rng) - { - return layerSeaHeightGenerators.aquire(() -> - { + public KList getLayerSeaHeightGenerators(RNG rng) { + return layerSeaHeightGenerators.aquire(() -> { KList layerSeaHeightGenerators = new KList<>(); int m = 7735; - for(IrisBiomePaletteLayer i : getSeaLayers()) - { + for (IrisBiomePaletteLayer i : getSeaLayers()) { layerSeaHeightGenerators.add(i.getHeightGenerator(rng.nextParallelRNG((m++) * m * m * m))); } @@ -366,57 +329,45 @@ public class IrisBiome extends IrisRegistrant implements IRare }); } - public boolean isLand() - { - if(inferredType == null) - { + public boolean isLand() { + if (inferredType == null) { return true; } return inferredType.equals(InferredType.LAND); } - public boolean isSea() - { - if(inferredType == null) - { + public boolean isSea() { + if (inferredType == null) { return false; } return inferredType.equals(InferredType.SEA); } - public boolean isShore() - { - if(inferredType == null) - { + public boolean isShore() { + if (inferredType == null) { return false; } return inferredType.equals(InferredType.SHORE); } - public Biome getSkyBiome(RNG rng, double x, double y, double z) - { - if(biomeSkyScatter.size() == 1) - { + public Biome getSkyBiome(RNG rng, double x, double y, double z) { + if (biomeSkyScatter.size() == 1) { return biomeSkyScatter.get(0); } - if(biomeSkyScatter.isEmpty()) - { + if (biomeSkyScatter.isEmpty()) { return getGroundBiome(rng, x, y, z); } return biomeSkyScatter.get(getBiomeGenerator(rng).fit(0, biomeSkyScatter.size() - 1, x, y, z)); } - public KList getRealChildren(ContextualChunkGenerator g) - { - return realChildren.aquire(() -> - { + public KList getRealChildren(ContextualChunkGenerator g) { + return realChildren.aquire(() -> { KList realChildren = new KList<>(); - for(String i : getChildren()) - { + for (String i : getChildren()) { realChildren.add(g != null ? g.loadBiome(i) : Iris.globaldata.getBiomeLoader().load(i)); } @@ -424,16 +375,13 @@ public class IrisBiome extends IrisRegistrant implements IRare }); } - public KList getAllChildren(ContextualChunkGenerator g, int limit) - { + public KList getAllChildren(ContextualChunkGenerator g, int limit) { KSet m = new KSet<>(); m.addAll(getChildren()); limit--; - if(limit > 0) - { - for(String i : getChildren()) - { + if (limit > 0) { + for (String i : getChildren()) { IrisBiome b = g != null ? g.loadBiome(i) : Iris.globaldata.getBiomeLoader().load(i); int l = limit; m.addAll(b.getAllChildren(g, l)); @@ -443,18 +391,15 @@ public class IrisBiome extends IrisRegistrant implements IRare return new KList(m); } - public Biome getGroundBiome(RNG rng, double x, double y, double z) - { - if(biomeSkyScatter.isEmpty()) - { + public Biome getGroundBiome(RNG rng, double x, double y, double z) { + if (biomeSkyScatter.isEmpty()) { return getDerivative(); } - if(biomeScatter.size() == 1) - { + if (biomeScatter.size() == 1) { return biomeScatter.get(0); } - return biomeScatter.get(getBiomeGenerator(rng).fit(0, biomeScatter.size() - 1, x, y, z)); + return getBiomeGenerator(rng).fit(biomeScatter, x, y, z); } } diff --git a/src/main/java/com/volmit/iris/object/NoiseStyle.java b/src/main/java/com/volmit/iris/object/NoiseStyle.java index 5600fd2eb..6d8ff3b37 100644 --- a/src/main/java/com/volmit/iris/object/NoiseStyle.java +++ b/src/main/java/com/volmit/iris/object/NoiseStyle.java @@ -28,43 +28,43 @@ public enum NoiseStyle { @Desc("Basic, Smooth & Fast Simplex noise.") @DontObfuscate - SIMPLEX(rng -> new CNG(rng, 1D, 1)), + SIMPLEX(rng -> new CNG(rng, 1D, 1).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 2 octaves") @DontObfuscate - BIOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 2)), + BIOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 2).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 3 octaves") @DontObfuscate - TRIOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 3)), + TRIOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 3).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 4 octaves") @DontObfuscate - QUADOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 4)), + QUADOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 4).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 5 octaves") @DontObfuscate - QUINTOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 5)), + QUINTOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 5).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 6 octaves") @DontObfuscate - SEXOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 6)), + SEXOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 6).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 7 octaves") @DontObfuscate - SEPTOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 7)), + SEPTOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 7).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 8 octaves") @DontObfuscate - OCTOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 8)), + OCTOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 8).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 9 octaves") @DontObfuscate - NONOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 9)), + NONOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 9).scale(0.04)), @Desc("Basic, Smooth & Fast Simplex noise. Uses 10 octaves") @DontObfuscate - VIGOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 10)), + VIGOCTAVE_SIMPLEX(rng -> new CNG(rng, 1D, 10).scale(0.04)), @Desc("Cellular noise creates the same noise level for cells, changes noise level on cell borders.") @DontObfuscate diff --git a/src/main/java/com/volmit/iris/util/ResourceLoader.java b/src/main/java/com/volmit/iris/util/ResourceLoader.java index aa6600721..2df6da8a2 100644 --- a/src/main/java/com/volmit/iris/util/ResourceLoader.java +++ b/src/main/java/com/volmit/iris/util/ResourceLoader.java @@ -9,8 +9,7 @@ import com.volmit.iris.object.IrisRegistrant; import lombok.Data; @Data -public class ResourceLoader -{ +public class ResourceLoader { protected File root; protected String folderName; protected String resourceTypeName; @@ -22,8 +21,7 @@ public class ResourceLoader protected IrisLock lock; protected String preferredFolder = null; - public ResourceLoader(File root, String folderName, String resourceTypeName, Class objectClass) - { + public ResourceLoader(File root, String folderName, String resourceTypeName, Class objectClass) { lock = new IrisLock("Res"); folderMapCache = new KMap<>(); this.objectClass = objectClass; @@ -34,15 +32,12 @@ public class ResourceLoader loadCache = new KMap<>(); } - public long count() - { + public long count() { return loadCache.size(); } - protected T loadFile(File j, String key, String name) - { - try - { + protected T loadFile(File j, String key, String name) { + try { T t = new Gson().fromJson(IO.readAll(j), objectClass); loadCache.put(key, t); Iris.hotloader.track(j); @@ -53,39 +48,32 @@ public class ResourceLoader return t; } - catch(Throwable e) - { + catch (Throwable e) { lock.unlock(); Iris.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() + ": " + e.getMessage()); return null; } } - public T load(String name) - { + public T load(String name) { String key = name + "-" + cname; - if(loadCache.containsKey(key)) - { + if (loadCache.containsKey(key)) { T t = loadCache.get(key); return t; } lock.lock(); - for(File i : getFolders(name)) - { - for(File j : i.listFiles()) - { - if(j.isFile() && j.getName().endsWith(".json") && j.getName().split("\\Q.\\E")[0].equals(name)) - { + for (File i : getFolders(name)) { + for (File j : i.listFiles()) { + if (j.isFile() && j.getName().endsWith(".json") && j.getName().split("\\Q.\\E")[0].equals(name)) { return loadFile(j, key, name); } } File file = new File(i, name + ".json"); - if(file.exists()) - { + if (file.exists()) { return loadFile(file, key, name); } } @@ -96,20 +84,15 @@ public class ResourceLoader return null; } - public KList getFolders() - { - if(folderCache == null) - { + public KList getFolders() { + lock.lock(); + if (folderCache == null) { folderCache = new KList<>(); - for(File i : root.listFiles()) - { - if(i.isDirectory()) - { - for(File j : i.listFiles()) - { - if(j.isDirectory() && j.getName().equals(folderName)) - { + for (File i : root.listFiles()) { + if (i.isDirectory()) { + for (File j : i.listFiles()) { + if (j.isDirectory() && j.getName().equals(folderName)) { folderCache.add(j); break; } @@ -117,32 +100,26 @@ public class ResourceLoader } } - if(preferredFolder != null) - { - for(File i : folderCache.copy()) - { - if(i.getParentFile().getName().equals(preferredFolder)) - { + if (preferredFolder != null) { + for (File i : folderCache.copy()) { + if (i.getParentFile().getName().equals(preferredFolder)) { folderCache.remove(i); folderCache.add(0, i); } } } } + lock.unlock(); return folderCache; } - public KList getFolders(String rc) - { + public KList getFolders(String rc) { KList folders = getFolders().copy(); - if(rc.contains(":")) - { - for(File i : folders.copy()) - { - if(!rc.startsWith(i.getName() + ":")) - { + if (rc.contains(":")) { + for (File i : folders.copy()) { + if (!rc.startsWith(i.getName() + ":")) { folders.remove(i); } } @@ -151,28 +128,23 @@ public class ResourceLoader return folders; } - public void clearCache() - { + public void clearCache() { loadCache.clear(); folderCache = null; } - public File fileFor(T b) - { - for(File i : getFolders()) - { - for(File j : i.listFiles()) - { - if(j.isFile() && j.getName().endsWith(".json") && j.getName().split("\\Q.\\E")[0].equals(b.getLoadKey())) - { + public File fileFor(T b) { + for (File i : getFolders()) { + for (File j : i.listFiles()) { + if (j.isFile() && j.getName().endsWith(".json") + && j.getName().split("\\Q.\\E")[0].equals(b.getLoadKey())) { return j; } } File file = new File(i, b.getLoadKey() + ".json"); - if(file.exists()) - { + if (file.exists()) { return file; } } @@ -180,13 +152,11 @@ public class ResourceLoader return null; } - public boolean isLoaded(String next) - { + public boolean isLoaded(String next) { return loadCache.containsKey(next); } - public void preferFolder(String name) - { + public void preferFolder(String name) { preferredFolder = name; } }