Changed biome cache to cache biomes instead of arrays.

This commit is contained in:
Harrison Deng 2020-04-26 17:40:18 -05:00
parent df10a2c298
commit b63c39b380

View File

@ -16,7 +16,7 @@ import ca.recrown.islandsurvivalcraft.world.IslandWorldMapper;
public class BiomePerIslandGenerator implements IslandBiomeGenerator {
private volatile boolean initialized;
private final TemperatureMapGenerator temperatureMapGenerator;
private final Cache<Point2, Biome[][]> chunkBiomesCache;
private final Cache<Point2, Biome> chunkBiomesCache;
private final Cache<Point2, Boolean> chunkGenStatusCache;
private volatile IslandWorldMapper worldIslandMap;
private volatile BiomeSelector biomeSelector;
@ -28,7 +28,7 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
public BiomePerIslandGenerator() {
this.temperatureMapGenerator = new TemperatureMapGenerator();
chunkBiomesCache = new Cache<>(8192);
chunkBiomesCache = new Cache<>(65536);
chunkGenStatusCache = new Cache<>(4096);
localChunkCache = new Biome[16][16];
}
@ -56,8 +56,10 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
}
int worldX = Utilities.addMagnitude(16 * chunkX, localX);
int worldZ = Utilities.addMagnitude(16 * chunkZ, localZ);
Point2 worldCoords = new Point2(worldX, worldZ);
Biome cachedBiome = getBiome(worldX, worldZ);
Biome cachedBiome = getBiome(worldCoords);
if (cachedBiome != null) return cachedBiome;
temperature = temperatureMapGenerator.getTemperature(worldX, worldZ);
@ -77,25 +79,24 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
DepthFirstSearch prop = new DepthFirstSearch(propInfo);
prop.setStartPosition(worldX, worldZ);
prop.findTarget(propInfo);
return getBiome(worldX, worldZ);
return getBiome(worldCoords);
}
private Biome getBiome(int worldX, int worldZ) {
int localX = Math.abs(worldX % 16);
int localZ = Math.abs(worldZ % 16);
Point2 chunkCoords = new Point2(worldX / 16, worldZ / 16);
private Biome getBiome(Point2 worldCoords) {
int localX = Math.abs(worldCoords.x % 16);
int localZ = Math.abs(worldCoords.y % 16);
Point2 chunkCoords = new Point2(worldCoords.x / 16, worldCoords.y / 16);
//chunk local
if (chunkCoords.fastEquals(this.currChunkCoords) && localChunkCache[localX][localZ] != null) {
return localChunkCache[localX][localZ];
}
Biome[][] biomes = chunkBiomesCache.getValue(chunkCoords);
if (biomes != null) {
Biome biome = biomes[localX][localZ];
if (biome != null) return biome;
}
//cache local
Biome biome = chunkBiomesCache.getValue(worldCoords);
if (biome != null) return biome;
//files local
Boolean chunkGenStat = chunkGenStatusCache.getValue(chunkCoords);
if (chunkGenStat == null) {
chunkGenStat = world.isChunkGenerated(chunkCoords.x, chunkCoords.y);
@ -103,25 +104,23 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
}
if (chunkGenStat) {
if (biomes == null) biomes = new Biome[16][16];
biomes[localX][localZ] = world.getBiome(worldX, 0, worldZ);
chunkBiomesCache.setValue(chunkCoords, biomes);
return biomes[localX][localZ];
biome = world.getBiome(worldCoords.x, 0, worldCoords.y);
chunkBiomesCache.setValue(worldCoords, biome);
return biome;
}
return null;
}
private void setCacheBiome(int worldX, int worldZ, Biome biome) {
int localX = Math.abs(worldX % 16);
int localZ = Math.abs(worldZ % 16);
Point2 chunkCoords = new Point2(worldX / 16, worldZ / 16);
private void setCacheBiome(Point2 worldCoords, Biome biome) {
int localX = Math.abs(worldCoords.x % 16);
int localZ = Math.abs(worldCoords.y % 16);
Point2 chunkCoords = new Point2(worldCoords.x / 16, worldCoords.y / 16);
//If local, set it locally
if (chunkCoords.fastEquals(this.currChunkCoords)) localChunkCache[localX][localZ] = biome;
Biome[][] chunkBiomes = chunkBiomesCache.getValue(chunkCoords);
if (chunkBiomes == null) chunkBiomes = new Biome[16][16];
chunkBiomes[localX][localZ] = biome;
chunkBiomesCache.setValue(chunkCoords, chunkBiomes);
//Set it cache wide.
chunkBiomesCache.setValue(worldCoords, biome);
}
private class FreshCachePropagationInfo implements CoordinateTargetValidatable, CoordinateValidatable {
@ -129,14 +128,15 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
@Override
public boolean isCoordinateTarget(int x, int y) {
Point2 worldCoords = new Point2(x, y);
if (worldIslandMap.isLand(x, y)) {
if (worldIslandMap.isShore(x, y)) {
setCacheBiome(x, y, shore);
setCacheBiome(worldCoords, shore);
} else {
setCacheBiome(x, y, main);
setCacheBiome(worldCoords, main);
}
} else {
setCacheBiome(x, y, shallow);
setCacheBiome(worldCoords, shallow);
}
return false;
}
@ -168,6 +168,8 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
@Override
public boolean isCoordinateTarget(int x, int y) {
Point2 worldCoords = new Point2(x, y);
if ((isLand = worldIslandMap.isLand(x, y))) {
isShore = worldIslandMap.isShore(x, y);
} else {
@ -175,13 +177,13 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
}
if (main == null && isLand && !isShore) {
main = getBiome(x, y);
main = getBiome(worldCoords);
} else
if (shore == null && isShore) {
shore = getBiome(x, y);
shore = getBiome(worldCoords);
} else
if (shallow == null && isShallow) {
shallow = getBiome(x, y);
shallow = getBiome(worldCoords);
}
return allBiomesAcquired();
}