fix spawn checks at biome edge

This commit is contained in:
dfsek
2020-12-30 13:55:15 -07:00
parent d16c28aebd
commit c283f37390
5 changed files with 36 additions and 14 deletions

View File

@@ -5,8 +5,8 @@ import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.generation.GenerationPhase;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.config.templates.BiomeTemplate;
import com.dfsek.terra.generation.config.WorldGenerator;
public class AirCheck extends SpawnCheck {
public AirCheck(World world, TerraPlugin main) {
@@ -16,10 +16,10 @@ public class AirCheck extends SpawnCheck {
@Override
public boolean check(int x, int y, int z) {
TerraWorld tw = main.getWorld(world);
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
TerraBiomeGrid g = tw.getGrid();
UserDefinedBiome b = (UserDefinedBiome) g.getBiome(x, z, GenerationPhase.POPULATE);
BiomeTemplate c = b.getConfig();
if(y <= c.getSeaLevel()) return false;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation <= 0;
return sample(x, y, z, g) <= 0;
}
}

View File

@@ -3,9 +3,6 @@ package com.dfsek.terra.api.structures.world;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.generation.GenerationPhase;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.generation.config.WorldGenerator;
public class LandCheck extends SpawnCheck {
public LandCheck(World world, TerraPlugin main) {
@@ -15,8 +12,6 @@ public class LandCheck extends SpawnCheck {
@Override
public boolean check(int x, int y, int z) {
TerraWorld tw = main.getWorld(world);
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation > 0;
return sample(x, y, z, tw.getGrid()) > 0;
}
}

View File

@@ -5,8 +5,8 @@ import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.generation.GenerationPhase;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.config.templates.BiomeTemplate;
import com.dfsek.terra.generation.config.WorldGenerator;
public class OceanCheck extends SpawnCheck {
public OceanCheck(World world, TerraPlugin main) {
@@ -16,10 +16,10 @@ public class OceanCheck extends SpawnCheck {
@Override
public boolean check(int x, int y, int z) {
TerraWorld tw = main.getWorld(world);
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
TerraBiomeGrid grid = tw.getGrid();
UserDefinedBiome b = (UserDefinedBiome) grid.getBiome(x, z, GenerationPhase.POPULATE);
BiomeTemplate c = b.getConfig();
if(y > c.getSeaLevel()) return false;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation <= 0;
return sample(x, y, z, grid) <= 0;
}
}

View File

@@ -2,16 +2,35 @@ package com.dfsek.terra.api.structures.world;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.generation.math.Sampler;
import net.jafama.FastMath;
import java.util.LinkedHashMap;
import java.util.Map;
public abstract class SpawnCheck {
protected final World world;
protected final TerraPlugin main;
private final Map<Long, Sampler> cache;
protected SpawnCheck(World world, TerraPlugin main) {
this.world = world;
this.main = main;
cache = new LinkedHashMap<Long, Sampler>() {
@Override
protected boolean removeEldestEntry(Map.Entry<Long, Sampler> eldest) {
return size() > main.getTerraConfig().getCheckCache();
}
};
}
public abstract boolean check(int x, int y, int z);
protected double sample(int x, int y, int z, TerraBiomeGrid grid) {
int cx = FastMath.floorDiv(x, 16);
int cz = FastMath.floorDiv(z, 16);
long key = (((long) cx) << 32) | (cz & 0xffffffffL);
return cache.computeIfAbsent(key, k -> new Sampler(cx, cz, grid, world, 4, 8)).sample(x - (cx << 4), y, z - (cz << 4));
}
}

View File

@@ -42,6 +42,10 @@ public class PluginConfig implements ConfigTemplate {
@Default
private int structureCache = 128;
@Value("cache.checks")
@Default
private int checkCache = 128;
@Value("dump-default")
@Default
private boolean dumpDefaultConfig = true;
@@ -90,4 +94,8 @@ public class PluginConfig implements ConfigTemplate {
public int getStructureCache() {
return structureCache;
}
public int getCheckCache() {
return checkCache;
}
}