mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-05 15:26:28 +00:00
Reorganize
This commit is contained in:
250
src/main/java/com/volmit/iris/generator/BiomeChunkGenerator.java
Normal file
250
src/main/java/com/volmit/iris/generator/BiomeChunkGenerator.java
Normal file
@@ -0,0 +1,250 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.bukkit.World;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.layer.GenLayerBiome;
|
||||
import ninja.bytecode.iris.object.InferredType;
|
||||
import ninja.bytecode.iris.object.IrisBiome;
|
||||
import ninja.bytecode.iris.object.IrisBiomeGeneratorLink;
|
||||
import ninja.bytecode.iris.object.IrisDimension;
|
||||
import ninja.bytecode.iris.object.IrisGenerator;
|
||||
import ninja.bytecode.iris.object.IrisRegion;
|
||||
import ninja.bytecode.iris.util.BiomeResult;
|
||||
import ninja.bytecode.iris.util.CNG;
|
||||
import ninja.bytecode.iris.util.ChronoLatch;
|
||||
import ninja.bytecode.iris.util.ChunkPosition;
|
||||
import ninja.bytecode.iris.util.IrisInterpolation;
|
||||
import ninja.bytecode.iris.util.RNG;
|
||||
import ninja.bytecode.shuriken.collections.KList;
|
||||
import ninja.bytecode.shuriken.collections.KMap;
|
||||
import ninja.bytecode.shuriken.math.M;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public abstract class BiomeChunkGenerator extends DimensionChunkGenerator
|
||||
{
|
||||
protected ReentrantLock regLock;
|
||||
private KMap<String, IrisGenerator> generators;
|
||||
private KMap<String, IrisGenerator> ceilingGenerators;
|
||||
protected GenLayerBiome glBiome;
|
||||
protected CNG masterFracture;
|
||||
private KMap<ChunkPosition, BiomeResult> biomeHitCache;
|
||||
private KMap<ChunkPosition, BiomeResult> ceilingBiomeHitCache;
|
||||
protected ChronoLatch cwarn = new ChronoLatch(1000);
|
||||
private IrisBiome[] biomeCache;
|
||||
|
||||
public BiomeChunkGenerator(String dimensionName)
|
||||
{
|
||||
super(dimensionName);
|
||||
generators = new KMap<>();
|
||||
ceilingGenerators = new KMap<>();
|
||||
regLock = new ReentrantLock();
|
||||
biomeHitCache = new KMap<>();
|
||||
ceilingBiomeHitCache = new KMap<>();
|
||||
biomeCache = new IrisBiome[256];
|
||||
}
|
||||
|
||||
public void onInit(World world, RNG rng)
|
||||
{
|
||||
loadGenerators();
|
||||
glBiome = new GenLayerBiome(this, masterRandom.nextParallelRNG(1));
|
||||
masterFracture = CNG.signature(rng.nextParallelRNG(13)).scale(0.12);
|
||||
}
|
||||
|
||||
protected IrisBiome getCachedBiome(int x, int z)
|
||||
{
|
||||
return biomeCache[(z << 4) | x];
|
||||
}
|
||||
|
||||
protected void cacheBiome(int x, int z, IrisBiome b)
|
||||
{
|
||||
biomeCache[(z << 4) | x] = b;
|
||||
}
|
||||
|
||||
public KMap<ChunkPosition, BiomeResult> getBiomeHitCache()
|
||||
{
|
||||
return getDimension().isInverted() ? ceilingBiomeHitCache : biomeHitCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHotload()
|
||||
{
|
||||
super.onHotload();
|
||||
biomeHitCache = new KMap<>();
|
||||
ceilingBiomeHitCache = new KMap<>();
|
||||
loadGenerators();
|
||||
}
|
||||
|
||||
public void registerGenerator(IrisGenerator g, IrisDimension dim)
|
||||
{
|
||||
KMap<String, IrisGenerator> generators = dim.isInverted() ? ceilingGenerators : this.generators;
|
||||
|
||||
regLock.lock();
|
||||
if(g.getLoadKey() == null || generators.containsKey(g.getLoadKey()))
|
||||
{
|
||||
regLock.unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
regLock.unlock();
|
||||
generators.put(g.getLoadKey(), g);
|
||||
}
|
||||
|
||||
protected KMap<String, IrisGenerator> getGenerators()
|
||||
{
|
||||
return getDimension().isInverted() ? ceilingGenerators : generators;
|
||||
}
|
||||
|
||||
protected double getBiomeHeight(double rx, double rz)
|
||||
{
|
||||
double h = 0;
|
||||
|
||||
for(IrisGenerator i : getGenerators().values())
|
||||
{
|
||||
h += interpolateGenerator(rx, rz, i);
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
protected double interpolateGenerator(double rx, double rz, IrisGenerator gen)
|
||||
{
|
||||
double hi = IrisInterpolation.getNoise(gen.getInterpolationFunction(), (int) Math.round(rx), (int) Math.round(rz), gen.getInterpolationScale(), (xx, zz) ->
|
||||
{
|
||||
IrisBiome b = sampleBiome((int) xx, (int) zz).getBiome();
|
||||
|
||||
for(IrisBiomeGeneratorLink i : b.getGenerators())
|
||||
{
|
||||
if(i.getGenerator().equals(gen.getLoadKey()))
|
||||
{
|
||||
return i.getMax();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
double lo = IrisInterpolation.getNoise(gen.getInterpolationFunction(), (int) Math.round(rx), (int) Math.round(rz), gen.getInterpolationScale(), (xx, zz) ->
|
||||
{
|
||||
IrisBiome b = sampleBiome((int) xx, (int) zz).getBiome();
|
||||
|
||||
for(IrisBiomeGeneratorLink i : b.getGenerators())
|
||||
{
|
||||
if(i.getGenerator().equals(gen.getLoadKey()))
|
||||
{
|
||||
return i.getMin();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
return M.lerp(lo, hi, gen.getHeight(rx, rz, world.getSeed() + 239945));
|
||||
}
|
||||
|
||||
protected void loadGenerators()
|
||||
{
|
||||
generators.clear();
|
||||
ceilingGenerators.clear();
|
||||
loadGenerators(((CeilingChunkGenerator) this).getFloorDimension());
|
||||
loadGenerators(((CeilingChunkGenerator) this).getCeilingDimension());
|
||||
}
|
||||
|
||||
protected void loadGenerators(IrisDimension dim)
|
||||
{
|
||||
if(dim == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
KList<String> touch = new KList<>();
|
||||
KList<String> loadQueue = new KList<>();
|
||||
|
||||
for(String i : dim.getRegions())
|
||||
{
|
||||
IrisRegion r = Iris.data.getRegionLoader().load(i);
|
||||
|
||||
if(r != null)
|
||||
{
|
||||
loadQueue.addAll(r.getLandBiomes());
|
||||
loadQueue.addAll(r.getSeaBiomes());
|
||||
loadQueue.addAll(r.getShoreBiomes());
|
||||
loadQueue.addAll(r.getRidgeBiomeKeys());
|
||||
loadQueue.addAll(r.getSpotBiomeKeys());
|
||||
}
|
||||
}
|
||||
|
||||
while(!loadQueue.isEmpty())
|
||||
{
|
||||
String next = loadQueue.pop();
|
||||
|
||||
if(!touch.contains(next))
|
||||
{
|
||||
touch.add(next);
|
||||
IrisBiome biome = Iris.data.getBiomeLoader().load(next);
|
||||
biome.getGenerators().forEach((i) -> registerGenerator(i.getCachedGenerator(), dim));
|
||||
loadQueue.addAll(biome.getChildren());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IrisRegion sampleRegion(int x, int z)
|
||||
{
|
||||
double wx = getModifiedX(x, z);
|
||||
double wz = getModifiedZ(x, z);
|
||||
return glBiome.getRegion(wx, wz);
|
||||
}
|
||||
|
||||
public BiomeResult sampleBiome(int x, int z)
|
||||
{
|
||||
if(!getDimension().getFocus().equals(""))
|
||||
{
|
||||
IrisBiome biome = Iris.data.getBiomeLoader().load(getDimension().getFocus());
|
||||
|
||||
for(String i : getDimension().getRegions())
|
||||
{
|
||||
IrisRegion reg = Iris.data.getRegionLoader().load(i);
|
||||
|
||||
if(reg.getLandBiomes().contains(biome.getLoadKey()))
|
||||
{
|
||||
biome.setInferredType(InferredType.LAND);
|
||||
break;
|
||||
}
|
||||
|
||||
if(reg.getSeaBiomes().contains(biome.getLoadKey()))
|
||||
{
|
||||
biome.setInferredType(InferredType.SEA);
|
||||
break;
|
||||
}
|
||||
|
||||
if(reg.getShoreBiomes().contains(biome.getLoadKey()))
|
||||
{
|
||||
biome.setInferredType(InferredType.SHORE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new BiomeResult(biome, 0);
|
||||
}
|
||||
|
||||
ChunkPosition pos = new ChunkPosition(x, z);
|
||||
|
||||
if(getBiomeHitCache().containsKey(pos))
|
||||
{
|
||||
return getBiomeHitCache().get(pos);
|
||||
}
|
||||
|
||||
double wx = getModifiedX(x, z);
|
||||
double wz = getModifiedZ(x, z);
|
||||
IrisRegion region = glBiome.getRegion(wx, wz);
|
||||
BiomeResult res = glBiome.generateRegionData(wx, wz, x, z, region);
|
||||
getBiomeHitCache().put(pos, res);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.object.IrisDimension;
|
||||
import ninja.bytecode.iris.util.InvertedBiomeGrid;
|
||||
import ninja.bytecode.iris.util.RNG;
|
||||
|
||||
public abstract class CeilingChunkGenerator extends PostBlockChunkGenerator
|
||||
{
|
||||
protected boolean generatingCeiling = false;
|
||||
protected boolean ceilingCached = false;
|
||||
protected IrisDimension cacheCeiling = null;
|
||||
protected IrisDimension cacheFloor = null;
|
||||
|
||||
public CeilingChunkGenerator(String dimensionName, int threads)
|
||||
{
|
||||
super(dimensionName, threads);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid)
|
||||
{
|
||||
targetFloor();
|
||||
generate(random, x, z, data, grid);
|
||||
|
||||
if(getFloorDimension().isMirrorCeiling())
|
||||
{
|
||||
writeInverted(copy(data), data);
|
||||
}
|
||||
|
||||
else if(getCeilingDimension() != null)
|
||||
{
|
||||
ChunkData ceiling = createChunkData(world);
|
||||
InvertedBiomeGrid ceilingGrid = new InvertedBiomeGrid(grid);
|
||||
targetCeiling();
|
||||
generate(random, x, z, ceiling, ceilingGrid);
|
||||
writeInverted(ceiling, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHotload()
|
||||
{
|
||||
super.onHotload();
|
||||
ceilingCached = false;
|
||||
cacheCeiling = null;
|
||||
cacheFloor = null;
|
||||
}
|
||||
|
||||
private void targetFloor()
|
||||
{
|
||||
generatingCeiling = false;
|
||||
}
|
||||
|
||||
private void targetCeiling()
|
||||
{
|
||||
generatingCeiling = true;
|
||||
}
|
||||
|
||||
private void generate(RNG random, int x, int z, ChunkData ceiling, BiomeGrid grid)
|
||||
{
|
||||
super.onGenerate(random, x, z, ceiling, grid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrisDimension getDimension()
|
||||
{
|
||||
return generatingCeiling ? getCeilingDimension() : getFloorDimension();
|
||||
}
|
||||
|
||||
public IrisDimension getFloorDimension()
|
||||
{
|
||||
if(cacheFloor != null)
|
||||
{
|
||||
return cacheFloor;
|
||||
}
|
||||
|
||||
return cacheFloor = super.getDimension();
|
||||
}
|
||||
|
||||
public IrisDimension getCeilingDimension()
|
||||
{
|
||||
if(ceilingCached)
|
||||
{
|
||||
return cacheCeiling;
|
||||
}
|
||||
|
||||
if(getFloorDimension().getCeiling().isEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
IrisDimension c = Iris.data.getDimensionLoader().load(getFloorDimension().getCeiling());
|
||||
|
||||
if(c != null)
|
||||
{
|
||||
c.setInverted(true);
|
||||
}
|
||||
|
||||
ceilingCached = true;
|
||||
cacheCeiling = c;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
public void writeInverted(ChunkData data, ChunkData into)
|
||||
{
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
for(int j = 0; j < data.getMaxHeight(); j++)
|
||||
{
|
||||
for(int k = 0; k < 16; k++)
|
||||
{
|
||||
BlockData b = data.getBlockData(i, j, k);
|
||||
|
||||
if(b == null || b.getMaterial().equals(Material.AIR))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
into.setBlock(i, data.getMaxHeight() - j, k, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ChunkData copy(ChunkData d)
|
||||
{
|
||||
ChunkData copy = createChunkData(world);
|
||||
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
for(int j = 0; j < d.getMaxHeight(); j++)
|
||||
{
|
||||
for(int k = 0; k < 16; k++)
|
||||
{
|
||||
BlockData b = d.getBlockData(i, j, k);
|
||||
|
||||
if(b == null || b.getMaterial().equals(Material.AIR))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
copy.setBlock(i, j, k, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.event.world.ChunkUnloadEvent;
|
||||
import org.bukkit.event.world.WorldUnloadEvent;
|
||||
import org.bukkit.generator.BlockPopulator;
|
||||
import org.bukkit.generator.ChunkGenerator;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.IrisContext;
|
||||
import ninja.bytecode.iris.IrisMetrics;
|
||||
import ninja.bytecode.iris.util.BlockDataTools;
|
||||
import ninja.bytecode.iris.util.CNG;
|
||||
import ninja.bytecode.iris.util.ChronoLatch;
|
||||
import ninja.bytecode.iris.util.RNG;
|
||||
import ninja.bytecode.shuriken.bench.PrecisionStopwatch;
|
||||
import ninja.bytecode.shuriken.logging.L;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public abstract class ContextualChunkGenerator extends ChunkGenerator implements Listener
|
||||
{
|
||||
protected boolean failing;
|
||||
protected int task;
|
||||
protected boolean initialized;
|
||||
protected RNG masterRandom;
|
||||
protected ChronoLatch perSecond;
|
||||
protected ChronoLatch tickLatch;
|
||||
protected ChronoLatch pushLatch;
|
||||
protected IrisMetrics metrics;
|
||||
protected World world;
|
||||
protected int generated;
|
||||
protected int ticks;
|
||||
protected boolean pregenDone;
|
||||
|
||||
public ContextualChunkGenerator()
|
||||
{
|
||||
pushLatch = new ChronoLatch(3000);
|
||||
tickLatch = new ChronoLatch(650);
|
||||
perSecond = new ChronoLatch(1000);
|
||||
CNG.creates = 0;
|
||||
generated = 0;
|
||||
ticks = 0;
|
||||
task = -1;
|
||||
initialized = false;
|
||||
failing = false;
|
||||
pregenDone = false;
|
||||
}
|
||||
|
||||
protected abstract void onGenerate(RNG masterRandom, int x, int z, ChunkData data, BiomeGrid grid);
|
||||
|
||||
protected abstract void onInit(World world, RNG masterRandom);
|
||||
|
||||
protected abstract void onTick(int ticks);
|
||||
|
||||
protected abstract void onClose();
|
||||
|
||||
protected abstract void onFailure(Throwable e);
|
||||
|
||||
protected abstract void onChunkLoaded(Chunk c);
|
||||
|
||||
protected abstract void onChunkUnloaded(Chunk c);
|
||||
|
||||
protected abstract void onPlayerJoin(Player p);
|
||||
|
||||
protected abstract void onPlayerLeft(Player p);
|
||||
|
||||
private void init(World world, RNG rng)
|
||||
{
|
||||
if(initialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.world = world;
|
||||
this.masterRandom = new RNG(world.getSeed());
|
||||
metrics = new IrisMetrics(128);
|
||||
initialized = true;
|
||||
Bukkit.getServer().getPluginManager().registerEvents(this, Iris.instance);
|
||||
task = Bukkit.getScheduler().scheduleSyncRepeatingTask(Iris.instance, this::tick, 0, 0);
|
||||
onInit(world, masterRandom);
|
||||
}
|
||||
|
||||
private void tick()
|
||||
{
|
||||
if(perSecond.flip())
|
||||
{
|
||||
if(generated > 770)
|
||||
{
|
||||
pregenDone = true;
|
||||
}
|
||||
|
||||
if(pregenDone)
|
||||
{
|
||||
metrics.getPerSecond().put(generated);
|
||||
generated = 0;
|
||||
}
|
||||
}
|
||||
|
||||
onTick(ticks++);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerTeleportEvent e)
|
||||
{
|
||||
if(e.getFrom().getWorld().equals(world) && !e.getTo().getWorld().equals(world))
|
||||
{
|
||||
tick();
|
||||
onPlayerLeft(e.getPlayer());
|
||||
}
|
||||
|
||||
if(!e.getFrom().getWorld().equals(world) && e.getTo().getWorld().equals(world))
|
||||
{
|
||||
tick();
|
||||
onPlayerJoin(e.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerQuitEvent e)
|
||||
{
|
||||
if(e.getPlayer().getWorld().equals(world))
|
||||
{
|
||||
tick();
|
||||
onPlayerLeft(e.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(PlayerJoinEvent e)
|
||||
{
|
||||
if(e.getPlayer().getWorld().equals(world))
|
||||
{
|
||||
tick();
|
||||
onPlayerJoin(e.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(ChunkLoadEvent e)
|
||||
{
|
||||
if(e.getWorld().equals(world))
|
||||
{
|
||||
tick();
|
||||
onChunkLoaded(e.getChunk());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(ChunkUnloadEvent e)
|
||||
{
|
||||
if(e.getWorld().equals(world))
|
||||
{
|
||||
tick();
|
||||
onChunkUnloaded(e.getChunk());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void on(WorldUnloadEvent e)
|
||||
{
|
||||
if(world != null && e.getWorld().equals(world))
|
||||
{
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
protected void close()
|
||||
{
|
||||
HandlerList.unregisterAll(this);
|
||||
Bukkit.getScheduler().cancelTask(getTask());
|
||||
onClose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSpawn(World world, int x, int z)
|
||||
{
|
||||
return super.canSpawn(world, x, z);
|
||||
}
|
||||
|
||||
protected ChunkData generateChunkDataFailure(World world, Random no, int x, int z, BiomeGrid biomeGrid)
|
||||
{
|
||||
ChunkData c = Bukkit.createChunkData(world);
|
||||
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
for(int j = 0; j < 16; j++)
|
||||
{
|
||||
int h = 0;
|
||||
|
||||
if(j == i || j + i == 16)
|
||||
{
|
||||
c.setBlock(i, h, j, BlockDataTools.getBlockData("RED_TERRACOTTA"));
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
c.setBlock(i, h, j, BlockDataTools.getBlockData("BLACK_TERRACOTTA"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChunkData generateChunkData(World world, Random no, int x, int z, BiomeGrid biomeGrid)
|
||||
{
|
||||
PrecisionStopwatch sx = PrecisionStopwatch.start();
|
||||
|
||||
if(failing)
|
||||
{
|
||||
return generateChunkDataFailure(world, no, x, z, biomeGrid);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(pushLatch.flip())
|
||||
{
|
||||
if(this.world == null)
|
||||
{
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
Iris.hotloader.check((IrisContext) this);
|
||||
|
||||
if(this instanceof IrisContext)
|
||||
{
|
||||
IrisContext.pushContext((IrisContext) this);
|
||||
}
|
||||
}
|
||||
|
||||
PrecisionStopwatch s = PrecisionStopwatch.start();
|
||||
RNG random = new RNG(world.getSeed());
|
||||
init(world, random.nextParallelRNG(0));
|
||||
ChunkData c = Bukkit.createChunkData(world);
|
||||
onGenerate(random, x, z, c, biomeGrid);
|
||||
metrics.getTotal().put(s.getMilliseconds());
|
||||
generated++;
|
||||
long hits = CNG.hits;
|
||||
CNG.hits = 0;
|
||||
Iris.instance.hit(hits);
|
||||
metrics.getLoss().put(sx.getMilliseconds() - s.getMilliseconds());
|
||||
return c;
|
||||
}
|
||||
|
||||
catch(Throwable e)
|
||||
{
|
||||
fail(e);
|
||||
}
|
||||
|
||||
return generateChunkDataFailure(world, no, x, z, biomeGrid);
|
||||
}
|
||||
|
||||
public void onHotload()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void fail(Throwable e)
|
||||
{
|
||||
failing = true;
|
||||
Iris.error("ERROR! Failed to generate chunk! Iris has entered a failed state!");
|
||||
|
||||
for(Player i : world.getPlayers())
|
||||
{
|
||||
Iris.instance.imsg(i, ChatColor.DARK_RED + "Iris Generator has entered a failed state!");
|
||||
Iris.instance.imsg(i, ChatColor.RED + "- Check the console for the error.");
|
||||
Iris.instance.imsg(i, ChatColor.RED + "- Then simply run /iris dev");
|
||||
}
|
||||
|
||||
L.ex(e);
|
||||
onFailure(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BlockPopulator> getDefaultPopulators(World world)
|
||||
{
|
||||
return super.getDefaultPopulators(world);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getFixedSpawnLocation(World world, Random random)
|
||||
{
|
||||
return super.getFixedSpawnLocation(world, random);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isParallelCapable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.object.InferredType;
|
||||
import ninja.bytecode.iris.object.IrisBiome;
|
||||
import ninja.bytecode.iris.object.IrisDimension;
|
||||
import ninja.bytecode.iris.object.IrisRegion;
|
||||
import ninja.bytecode.iris.util.BiomeResult;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public abstract class DimensionChunkGenerator extends ContextualChunkGenerator
|
||||
{
|
||||
protected final String dimensionName;
|
||||
protected static final BlockData AIR = Material.AIR.createBlockData();
|
||||
protected static final BlockData BEDROCK = Material.BEDROCK.createBlockData();
|
||||
|
||||
public DimensionChunkGenerator(String dimensionName)
|
||||
{
|
||||
super();
|
||||
this.dimensionName = dimensionName;
|
||||
}
|
||||
|
||||
public IrisDimension getDimension()
|
||||
{
|
||||
IrisDimension d = Iris.data.getDimensionLoader().load(dimensionName);
|
||||
|
||||
if(d == null)
|
||||
{
|
||||
Iris.error("Can't find dimension: " + dimensionName);
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
protected BiomeResult focus()
|
||||
{
|
||||
IrisBiome biome = Iris.data.getBiomeLoader().load(getDimension().getFocus());
|
||||
|
||||
for(String i : getDimension().getRegions())
|
||||
{
|
||||
IrisRegion reg = Iris.data.getRegionLoader().load(i);
|
||||
|
||||
if(reg.getLandBiomes().contains(biome.getLoadKey()))
|
||||
{
|
||||
biome.setInferredType(InferredType.LAND);
|
||||
break;
|
||||
}
|
||||
|
||||
if(reg.getSeaBiomes().contains(biome.getLoadKey()))
|
||||
{
|
||||
biome.setInferredType(InferredType.SEA);
|
||||
break;
|
||||
}
|
||||
|
||||
if(reg.getShoreBiomes().contains(biome.getLoadKey()))
|
||||
{
|
||||
biome.setInferredType(InferredType.SHORE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new BiomeResult(biome, 0);
|
||||
}
|
||||
|
||||
public double getModifiedX(int rx, int rz)
|
||||
{
|
||||
return (getDimension().cosRotate() * rx) + (-getDimension().sinRotate() * rz) +
|
||||
|
||||
getDimension().getCoordFracture(masterRandom, 39392).fitDoubleD(-getDimension().getCoordFractureDistance() / 2, getDimension().getCoordFractureDistance() / 2, rx, rz);
|
||||
}
|
||||
|
||||
public double getModifiedZ(int rx, int rz)
|
||||
{
|
||||
return (getDimension().sinRotate() * rx) + (getDimension().cosRotate() * rz) +
|
||||
|
||||
getDimension().getCoordFracture(masterRandom, 39392).fitDoubleD(-getDimension().getCoordFractureDistance() / 2, getDimension().getCoordFractureDistance() / 2, rx, rz);
|
||||
}
|
||||
|
||||
public double getZoomed(double modified)
|
||||
{
|
||||
return (double) (modified) / getDimension().getTerrainZoom();
|
||||
}
|
||||
|
||||
public double getUnzoomed(double modified)
|
||||
{
|
||||
return (double) (modified) * getDimension().getTerrainZoom();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.IrisContext;
|
||||
import ninja.bytecode.iris.object.IrisBiome;
|
||||
import ninja.bytecode.iris.object.IrisRegion;
|
||||
import ninja.bytecode.iris.util.BiomeResult;
|
||||
import ninja.bytecode.shuriken.collections.KMap;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class IrisChunkGenerator extends CeilingChunkGenerator implements IrisContext
|
||||
{
|
||||
private Method initLighting;
|
||||
private KMap<Player, IrisBiome> b = new KMap<>();
|
||||
|
||||
public IrisChunkGenerator(String dimensionName, int threads)
|
||||
{
|
||||
super(dimensionName, threads);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeResult getBiome(int x, int z)
|
||||
{
|
||||
return sampleBiome(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrisRegion getRegion(int x, int z)
|
||||
{
|
||||
return sampleRegion(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight(int x, int z)
|
||||
{
|
||||
return sampleHeight(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onTick(int ticks)
|
||||
{
|
||||
super.onTick(ticks);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onClose()
|
||||
{
|
||||
super.onClose();
|
||||
Iris.info("Closing Iris Dimension " + getWorld().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onFailure(Throwable e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onChunkLoaded(Chunk c)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onChunkUnloaded(Chunk c)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPlayerJoin(Player p)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPlayerLeft(Player p)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHotloaded()
|
||||
{
|
||||
onHotload();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.object.IrisBiome;
|
||||
import ninja.bytecode.iris.object.IrisDepositGenerator;
|
||||
import ninja.bytecode.iris.object.IrisObjectPlacement;
|
||||
import ninja.bytecode.iris.object.IrisRegion;
|
||||
import ninja.bytecode.iris.object.atomics.AtomicSliver;
|
||||
import ninja.bytecode.iris.object.atomics.AtomicSliverMap;
|
||||
import ninja.bytecode.iris.object.atomics.AtomicWorldData;
|
||||
import ninja.bytecode.iris.object.atomics.MasterLock;
|
||||
import ninja.bytecode.iris.util.BiomeMap;
|
||||
import ninja.bytecode.iris.util.ChunkPosition;
|
||||
import ninja.bytecode.iris.util.HeightMap;
|
||||
import ninja.bytecode.iris.util.IObjectPlacer;
|
||||
import ninja.bytecode.iris.util.RNG;
|
||||
import ninja.bytecode.shuriken.collections.KMap;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public abstract class ParallaxChunkGenerator extends TerrainChunkGenerator implements IObjectPlacer
|
||||
{
|
||||
private KMap<ChunkPosition, AtomicSliver> sliverCache;
|
||||
protected AtomicWorldData parallaxMap;
|
||||
private KMap<ChunkPosition, AtomicSliver> ceilingSliverCache;
|
||||
protected AtomicWorldData ceilingParallaxMap;
|
||||
private MasterLock masterLock;
|
||||
private ReentrantLock lock = new ReentrantLock();
|
||||
private int sliverBuffer;
|
||||
|
||||
public ParallaxChunkGenerator(String dimensionName, int threads)
|
||||
{
|
||||
super(dimensionName, threads);
|
||||
sliverCache = new KMap<>();
|
||||
ceilingSliverCache = new KMap<>();
|
||||
sliverBuffer = 0;
|
||||
masterLock = new MasterLock();
|
||||
}
|
||||
|
||||
public void onInit(World world, RNG rng)
|
||||
{
|
||||
super.onInit(world, rng);
|
||||
parallaxMap = new AtomicWorldData(world, "floor");
|
||||
ceilingParallaxMap = new AtomicWorldData(world, "ceiling");
|
||||
}
|
||||
|
||||
protected KMap<ChunkPosition, AtomicSliver> getSliverCache()
|
||||
{
|
||||
return getDimension().isInverted() ? ceilingSliverCache : sliverCache;
|
||||
}
|
||||
|
||||
protected void onClose()
|
||||
{
|
||||
super.onClose();
|
||||
|
||||
try
|
||||
{
|
||||
parallaxMap.unloadAll(true);
|
||||
ceilingParallaxMap.unloadAll(true);
|
||||
}
|
||||
|
||||
catch(IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHighest(int x, int z)
|
||||
{
|
||||
return getHighest(x, z, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHighest(int x, int z, boolean ignoreFluid)
|
||||
{
|
||||
return (int) Math.round(ignoreFluid ? getTerrainHeight(x, z) : getTerrainWaterHeight(x, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int x, int y, int z, BlockData d)
|
||||
{
|
||||
getMasterLock().lock((x >> 4) + "." + (z >> 4));
|
||||
getParallaxSliver(x, z).set(y, d);
|
||||
getMasterLock().unlock((x >> 4) + "." + (z >> 4));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData get(int x, int y, int z)
|
||||
{
|
||||
BlockData b = sampleSliver(x, z).getBlock().get(y);
|
||||
return b == null ? AIR : b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSolid(int x, int y, int z)
|
||||
{
|
||||
return get(x, y, z).getMaterial().isSolid();
|
||||
}
|
||||
|
||||
public AtomicSliver getParallaxSliver(int wx, int wz)
|
||||
{
|
||||
getMasterLock().lock("gpc");
|
||||
getMasterLock().lock((wx >> 4) + "." + (wz >> 4));
|
||||
AtomicSliverMap map = getParallaxChunk(wx >> 4, wz >> 4);
|
||||
getMasterLock().unlock("gpc");
|
||||
AtomicSliver sliver = map.getSliver(wx & 15, wz & 15);
|
||||
getMasterLock().unlock((wx >> 4) + "." + (wz >> 4));
|
||||
|
||||
return sliver;
|
||||
}
|
||||
|
||||
public boolean isParallaxGenerated(int x, int z)
|
||||
{
|
||||
return getParallaxChunk(x, z).isParallaxGenerated();
|
||||
}
|
||||
|
||||
public boolean isWorldGenerated(int x, int z)
|
||||
{
|
||||
return getParallaxChunk(x, z).isWorldGenerated();
|
||||
}
|
||||
|
||||
public AtomicWorldData getParallaxMap()
|
||||
{
|
||||
return getDimension().isInverted() ? ceilingParallaxMap : parallaxMap;
|
||||
}
|
||||
|
||||
public AtomicSliverMap getParallaxChunk(int x, int z)
|
||||
{
|
||||
try
|
||||
{
|
||||
return getParallaxMap().loadChunk(x, z);
|
||||
}
|
||||
|
||||
catch(IOException e)
|
||||
{
|
||||
fail(e);
|
||||
}
|
||||
|
||||
return new AtomicSliverMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, BiomeMap biomeMap)
|
||||
{
|
||||
super.onPostGenerate(random, x, z, data, grid, height, biomeMap);
|
||||
getBiomeHitCache().clear();
|
||||
|
||||
if(getDimension().isPlaceObjects())
|
||||
{
|
||||
onGenerateParallax(random, x, z);
|
||||
injectBiomeSky(x, z, grid);
|
||||
getParallaxChunk(x, z).inject(data);
|
||||
setSliverBuffer(getSliverCache().size());
|
||||
getParallaxChunk(x, z).setWorldGenerated(true);
|
||||
getSliverCache().clear();
|
||||
getMasterLock().clear();
|
||||
}
|
||||
|
||||
super.onPostParallaxPostGenerate(random, x, z, data, grid, height, biomeMap);
|
||||
}
|
||||
|
||||
protected void injectBiomeSky(int x, int z, BiomeGrid grid)
|
||||
{
|
||||
if(getDimension().isInverted())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int rx;
|
||||
int rz;
|
||||
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
rx = (x * 16) + i;
|
||||
for(int j = 0; j < 16; j++)
|
||||
{
|
||||
rz = (z * 16) + j;
|
||||
|
||||
int min = sampleSliver(rx, rz).getHighestBiome();
|
||||
int max = getParallaxSliver(rx, rz).getHighestBlock();
|
||||
|
||||
if(min < max)
|
||||
{
|
||||
IrisBiome biome = getCachedBiome(i, j);
|
||||
|
||||
for(int g = min; g <= max; g++)
|
||||
{
|
||||
grid.setBiome(i, g, j, biome.getSkyBiome(masterRandom, rz, g, rx));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void onGenerateParallax(RNG random, int x, int z)
|
||||
{
|
||||
String key = "par." + x + "." + "z";
|
||||
ChunkPosition rad = Iris.data.getObjectLoader().getParallaxSize();
|
||||
|
||||
for(int ii = x - (rad.getX() / 2); ii <= x + (rad.getX() / 2); ii++)
|
||||
{
|
||||
int i = ii;
|
||||
|
||||
for(int jj = z - (rad.getZ() / 2); jj <= z + (rad.getZ() / 2); jj++)
|
||||
{
|
||||
int j = jj;
|
||||
|
||||
if(isParallaxGenerated(ii, jj))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(isWorldGenerated(ii, jj))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
getAccelerant().queue(key, () ->
|
||||
{
|
||||
IrisBiome b = sampleTrueBiome((i * 16) + 7, (j * 16) + 7).getBiome();
|
||||
IrisRegion r = sampleRegion((i * 16) + 7, (j * 16) + 7);
|
||||
RNG ro = random.nextParallelRNG(496888 + i + j);
|
||||
|
||||
int g = 1;
|
||||
|
||||
for(IrisObjectPlacement k : b.getObjects())
|
||||
{
|
||||
placeObject(k, i, j, random.nextParallelRNG((34 * ((i * 30) + (j * 30) + g++) * i * j) + i - j + 3569222));
|
||||
}
|
||||
|
||||
for(IrisDepositGenerator k : getDimension().getDeposits())
|
||||
{
|
||||
for(int l = 0; l < ro.i(k.getMinPerChunk(), k.getMaxPerChunk()); l++)
|
||||
{
|
||||
k.generate((x * 16) + ro.nextInt(16), (z * 16) + ro.nextInt(16), ro, this);
|
||||
}
|
||||
}
|
||||
|
||||
for(IrisDepositGenerator k : r.getDeposits())
|
||||
{
|
||||
for(int l = 0; l < ro.i(k.getMinPerChunk(), k.getMaxPerChunk()); l++)
|
||||
{
|
||||
k.generate((x * 16) + ro.nextInt(16), (z * 16) + ro.nextInt(16), ro, this);
|
||||
}
|
||||
}
|
||||
|
||||
for(IrisDepositGenerator k : b.getDeposits())
|
||||
{
|
||||
for(int l = 0; l < ro.i(k.getMinPerChunk(), k.getMaxPerChunk()); l++)
|
||||
{
|
||||
k.generate((x * 16) + ro.nextInt(16), (z * 16) + ro.nextInt(16), ro, this);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
getParallaxChunk(ii, jj).setParallaxGenerated(true);
|
||||
}
|
||||
}
|
||||
|
||||
getAccelerant().waitFor(key);
|
||||
}
|
||||
|
||||
public void placeObject(IrisObjectPlacement o, int x, int z, RNG rng)
|
||||
{
|
||||
for(int i = 0; i < o.getTriesForChunk(rng); i++)
|
||||
{
|
||||
rng = rng.nextParallelRNG((i * 3 + 8) - 23040);
|
||||
o.getSchematic(rng).place((x * 16) + rng.nextInt(16), (z * 16) + rng.nextInt(16), this, o, rng);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onTick(int ticks)
|
||||
{
|
||||
getParallaxMap().clean(ticks);
|
||||
Iris.data.getObjectLoader().clean();
|
||||
}
|
||||
|
||||
public AtomicSliver sampleSliver(int x, int z)
|
||||
{
|
||||
ChunkPosition key = new ChunkPosition(x, z);
|
||||
|
||||
if(getSliverCache().containsKey(key))
|
||||
{
|
||||
return getSliverCache().get(key);
|
||||
}
|
||||
|
||||
AtomicSliver s = new AtomicSliver(x & 15, z & 15);
|
||||
onGenerateColumn(x >> 4, z >> 4, x, z, x & 15, z & 15, s, null);
|
||||
getSliverCache().put(key, s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPreventingDecay()
|
||||
{
|
||||
return getDimension().isPreventLeafDecay();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import org.bukkit.World;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.object.atomics.AtomicSliver;
|
||||
import ninja.bytecode.iris.object.atomics.AtomicSliverMap;
|
||||
import ninja.bytecode.iris.util.BiomeMap;
|
||||
import ninja.bytecode.iris.util.GroupedExecutor;
|
||||
import ninja.bytecode.iris.util.HeightMap;
|
||||
import ninja.bytecode.iris.util.RNG;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public abstract class ParallelChunkGenerator extends BiomeChunkGenerator
|
||||
{
|
||||
private GroupedExecutor accelerant;
|
||||
private int threads;
|
||||
|
||||
public ParallelChunkGenerator(String dimensionName, int threads)
|
||||
{
|
||||
super(dimensionName);
|
||||
this.threads = threads;
|
||||
}
|
||||
|
||||
public void changeThreadCount(int tc)
|
||||
{
|
||||
threads = tc;
|
||||
GroupedExecutor e = accelerant;
|
||||
accelerant = new GroupedExecutor(threads, Thread.NORM_PRIORITY, "Iris Generator - " + world.getName());
|
||||
Iris.executors.add(accelerant);
|
||||
|
||||
if(e != null)
|
||||
{
|
||||
e.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void onGenerateColumn(int cx, int cz, int wx, int wz, int x, int z, AtomicSliver sliver, BiomeMap biomeMap);
|
||||
|
||||
protected abstract int onSampleColumnHeight(int cx, int cz, int wx, int wz, int x, int z);
|
||||
|
||||
protected abstract void onPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, BiomeMap biomeMap);
|
||||
|
||||
protected int sampleHeight(int x, int z)
|
||||
{
|
||||
return onSampleColumnHeight(x >> 4, z >> 4, x, z, x & 15, z & 15);
|
||||
}
|
||||
|
||||
protected void onGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid)
|
||||
{
|
||||
AtomicSliverMap map = new AtomicSliverMap();
|
||||
HeightMap height = new HeightMap();
|
||||
String key = "c" + x + "," + z;
|
||||
BiomeMap biomeMap = new BiomeMap();
|
||||
int ii, jj;
|
||||
|
||||
for(ii = 0; ii < 16; ii++)
|
||||
{
|
||||
int i = ii;
|
||||
int wx = (x * 16) + i;
|
||||
|
||||
for(jj = 0; jj < 16; jj++)
|
||||
{
|
||||
int j = jj;
|
||||
int wz = (z * 16) + j;
|
||||
AtomicSliver sliver = map.getSliver(i, j);
|
||||
|
||||
accelerant.queue(key, () ->
|
||||
{
|
||||
onGenerateColumn(x, z, wx, wz, i, j, sliver, biomeMap);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
accelerant.waitFor(key);
|
||||
map.write(data, grid, height);
|
||||
onPostGenerate(random, x, z, data, grid, height, biomeMap);
|
||||
}
|
||||
|
||||
protected void onClose()
|
||||
{
|
||||
accelerant.close();
|
||||
}
|
||||
|
||||
public void onInit(World world, RNG rng)
|
||||
{
|
||||
super.onInit(world, rng);
|
||||
changeThreadCount(threads);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isParallelCapable()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import ninja.bytecode.iris.Iris;
|
||||
import ninja.bytecode.iris.layer.post.PostNippleSmoother;
|
||||
import ninja.bytecode.iris.layer.post.PostPotholeFiller;
|
||||
import ninja.bytecode.iris.layer.post.PostSlabber;
|
||||
import ninja.bytecode.iris.layer.post.PostWallPatcher;
|
||||
import ninja.bytecode.iris.object.IrisDimension;
|
||||
import ninja.bytecode.iris.util.IPostBlockAccess;
|
||||
import ninja.bytecode.iris.util.IrisPostBlockFilter;
|
||||
import ninja.bytecode.iris.util.RNG;
|
||||
import ninja.bytecode.shuriken.collections.KList;
|
||||
|
||||
public abstract class PostBlockChunkGenerator extends ParallaxChunkGenerator implements IPostBlockAccess
|
||||
{
|
||||
protected boolean generatingCeiling = false;
|
||||
protected boolean ceilingCached = false;
|
||||
protected IrisDimension cacheCeiling = null;
|
||||
protected IrisDimension cacheFloor = null;
|
||||
private int currentPostX;
|
||||
private int currentPostZ;
|
||||
private ChunkData currentData;
|
||||
private KList<IrisPostBlockFilter> filters;
|
||||
private String postKey;
|
||||
private ReentrantLock lock;
|
||||
|
||||
public PostBlockChunkGenerator(String dimensionName, int threads)
|
||||
{
|
||||
super(dimensionName, threads);
|
||||
filters = new KList<>();
|
||||
postKey = "post-" + dimensionName;
|
||||
lock = new ReentrantLock();
|
||||
}
|
||||
|
||||
public void onInit(World world, RNG rng)
|
||||
{
|
||||
super.onInit(world, rng);
|
||||
filters.add(new PostNippleSmoother(this));
|
||||
filters.add(new PostPotholeFiller(this));
|
||||
filters.add(new PostWallPatcher(this));
|
||||
filters.add(new PostSlabber(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid)
|
||||
{
|
||||
super.onGenerate(random, x, z, data, grid);
|
||||
|
||||
if(!getDimension().isPostProcess())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
currentData = data;
|
||||
currentPostX = x;
|
||||
currentPostZ = z;
|
||||
int rx, i, j;
|
||||
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
rx = (x * 16) + i;
|
||||
|
||||
for(j = 0; j < 16; j++)
|
||||
{
|
||||
int rxx = rx;
|
||||
int rzz = (z * 16) + j;
|
||||
|
||||
getAccelerant().queue(postKey, () ->
|
||||
{
|
||||
for(IrisPostBlockFilter f : filters)
|
||||
{
|
||||
int rxxx = rxx;
|
||||
int rzzx = rzz;
|
||||
|
||||
f.onPost(rxxx, rzzx);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getAccelerant().waitFor(postKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData getPostBlock(int x, int y, int z)
|
||||
{
|
||||
if(x >> 4 == currentPostX && z >> 4 == currentPostZ)
|
||||
{
|
||||
lock.lock();
|
||||
BlockData d = currentData.getBlockData(x & 15, y, z & 15);
|
||||
lock.unlock();
|
||||
return d == null ? AIR : d;
|
||||
}
|
||||
|
||||
return sampleSliver(x, z).get(y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPostBlock(int x, int y, int z, BlockData d)
|
||||
{
|
||||
if(x >> 4 == currentPostX && z >> 4 == currentPostZ)
|
||||
{
|
||||
lock.lock();
|
||||
currentData.setBlock(x & 15, y, z & 15, d);
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Iris.warn("Post Block Overdraw: " + currentPostX + "," + currentPostZ + " into " + (x >> 4) + ", " + (z >> 4));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int highestTerrainOrFluidBlock(int x, int z)
|
||||
{
|
||||
return getHighest(x, z, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int highestTerrainBlock(int x, int z)
|
||||
{
|
||||
return getHighest(x, z, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
package ninja.bytecode.iris.generator;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.Bisected;
|
||||
import org.bukkit.block.data.Bisected.Half;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import ninja.bytecode.iris.layer.GenLayerCave;
|
||||
import ninja.bytecode.iris.object.DecorationPart;
|
||||
import ninja.bytecode.iris.object.InferredType;
|
||||
import ninja.bytecode.iris.object.IrisBiome;
|
||||
import ninja.bytecode.iris.object.IrisBiomeDecorator;
|
||||
import ninja.bytecode.iris.object.IrisRegion;
|
||||
import ninja.bytecode.iris.object.atomics.AtomicSliver;
|
||||
import ninja.bytecode.iris.util.BiomeMap;
|
||||
import ninja.bytecode.iris.util.BiomeResult;
|
||||
import ninja.bytecode.iris.util.BlockDataTools;
|
||||
import ninja.bytecode.iris.util.HeightMap;
|
||||
import ninja.bytecode.iris.util.RNG;
|
||||
import ninja.bytecode.shuriken.collections.KList;
|
||||
import ninja.bytecode.shuriken.math.M;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public abstract class TerrainChunkGenerator extends ParallelChunkGenerator
|
||||
{
|
||||
private long lastUpdateRequest = M.ms();
|
||||
private long lastChunkLoad = M.ms();
|
||||
private GenLayerCave glCave;
|
||||
private RNG rockRandom;
|
||||
|
||||
public TerrainChunkGenerator(String dimensionName, int threads)
|
||||
{
|
||||
super(dimensionName, threads);
|
||||
}
|
||||
|
||||
public void onInit(World world, RNG rng)
|
||||
{
|
||||
super.onInit(world, rng);
|
||||
rockRandom = getMasterRandom().nextParallelRNG(2858678);
|
||||
glCave = new GenLayerCave(this, rng.nextParallelRNG(238948));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onGenerateColumn(int cx, int cz, int rx, int rz, int x, int z, AtomicSliver sliver, BiomeMap biomeMap)
|
||||
{
|
||||
try
|
||||
{
|
||||
BlockData block;
|
||||
int fluidHeight = getDimension().getFluidHeight();
|
||||
double ox = getModifiedX(rx, rz);
|
||||
double oz = getModifiedZ(rx, rz);
|
||||
double wx = getZoomed(ox);
|
||||
double wz = getZoomed(oz);
|
||||
int depth = 0;
|
||||
double noise = getNoiseHeight(rx, rz);
|
||||
int height = (int) Math.round(noise) + fluidHeight;
|
||||
IrisBiome biome = sampleTrueBiome(rx, rz).getBiome();
|
||||
KList<BlockData> layers = biome.generateLayers(wx, wz, masterRandom, height, height - getFluidHeight());
|
||||
KList<BlockData> seaLayers = biome.isSea() ? biome.generateSeaLayers(wx, wz, masterRandom, fluidHeight - height) : new KList<>();
|
||||
cacheBiome(x, z, biome);
|
||||
|
||||
for(int k = Math.max(height, fluidHeight); k < Math.max(height, fluidHeight) + 3; k++)
|
||||
{
|
||||
if(k < Math.max(height, fluidHeight) + 3)
|
||||
{
|
||||
if(biomeMap != null)
|
||||
{
|
||||
sliver.set(k, biome.getGroundBiome(masterRandom, rz, k, rx));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int k = Math.max(height, fluidHeight); k >= 0; k--)
|
||||
{
|
||||
if(k == 0)
|
||||
{
|
||||
sliver.set(0, BEDROCK);
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean underwater = k > height && k <= fluidHeight;
|
||||
|
||||
if(biomeMap != null)
|
||||
{
|
||||
sliver.set(k, biome.getGroundBiome(masterRandom, rz, k, rx));
|
||||
biomeMap.setBiome(x, z, biome);
|
||||
}
|
||||
|
||||
if(underwater)
|
||||
{
|
||||
block = seaLayers.hasIndex(fluidHeight - k) ? layers.get(depth) : getDimension().getFluid(rockRandom, wx, k, wz);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
block = layers.hasIndex(depth) ? layers.get(depth) : getDimension().getRock(rockRandom, wx, k, wz);
|
||||
depth++;
|
||||
}
|
||||
|
||||
sliver.set(k, block);
|
||||
|
||||
if(k == height && block.getMaterial().isSolid() && k < fluidHeight)
|
||||
{
|
||||
decorateUnderwater(biome, sliver, wx, k, wz, rx, rz, block);
|
||||
}
|
||||
|
||||
if(k == Math.max(height, fluidHeight) && block.getMaterial().isSolid() && k < 255 && k > fluidHeight)
|
||||
{
|
||||
decorateLand(biome, sliver, wx, k, wz, rx, rz, block);
|
||||
}
|
||||
}
|
||||
|
||||
glCave.genCaves(rx, rz, x, z, sliver);
|
||||
}
|
||||
|
||||
catch(Throwable e)
|
||||
{
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean canPlace(Material mat, Material onto)
|
||||
{
|
||||
if(onto.equals(Material.GRASS_PATH))
|
||||
{
|
||||
if(!mat.isSolid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(onto.equals(Material.ACACIA_LEAVES) || onto.equals(Material.BIRCH_LEAVES) || onto.equals(Material.DARK_OAK_LEAVES) || onto.equals(Material.JUNGLE_LEAVES) || onto.equals(Material.OAK_LEAVES) || onto.equals(Material.SPRUCE_LEAVES))
|
||||
{
|
||||
if(!mat.isSolid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
BlockData d = i.getBlockData(getMasterRandom().nextParallelRNG(biome.hashCode() + j++), wx, wz);
|
||||
|
||||
if(d != null)
|
||||
{
|
||||
if(!canPlace(d.getMaterial(), block.getMaterial()))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(d.getMaterial().equals(Material.CACTUS))
|
||||
{
|
||||
if(!block.getMaterial().equals(Material.SAND) && !block.getMaterial().equals(Material.RED_SAND))
|
||||
{
|
||||
sliver.set(k, BlockDataTools.getBlockData("RED_SAND"));
|
||||
}
|
||||
}
|
||||
|
||||
if(d instanceof Bisected && k < 254)
|
||||
{
|
||||
Bisected t = ((Bisected) d.clone());
|
||||
t.setHalf(Half.TOP);
|
||||
Bisected b = ((Bisected) d.clone());
|
||||
b.setHalf(Half.BOTTOM);
|
||||
sliver.set(k + 1, b);
|
||||
sliver.set(k + 2, t);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int stack = i.getHeight(getMasterRandom().nextParallelRNG(39456 + i.hashCode()), wx, wz);
|
||||
|
||||
if(stack == 1)
|
||||
{
|
||||
sliver.set(k + 1, d);
|
||||
}
|
||||
|
||||
else if(k < 255 - stack)
|
||||
{
|
||||
for(int l = 0; l < stack; l++)
|
||||
{
|
||||
sliver.set(k + l + 1, d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
BlockData d = i.getBlockData(getMasterRandom().nextParallelRNG(biome.hashCode() + j++), wx, wz);
|
||||
|
||||
if(d != null)
|
||||
{
|
||||
int stack = i.getHeight(getMasterRandom().nextParallelRNG(39456 + i.hashCode()), wx, wz);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
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 onPostParallaxPostGenerate(RNG random, int x, int z, ChunkData data, BiomeGrid grid, HeightMap height, BiomeMap biomeMap)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected double getNoiseHeight(int rx, int rz)
|
||||
{
|
||||
double wx = getZoomed(rx);
|
||||
double wz = getZoomed(rz);
|
||||
|
||||
return getBiomeHeight(wx, wz);
|
||||
}
|
||||
|
||||
public BiomeResult sampleTrueBiomeBase(int x, int z)
|
||||
{
|
||||
if(!getDimension().getFocus().equals(""))
|
||||
{
|
||||
return focus();
|
||||
}
|
||||
|
||||
double wx = getModifiedX(x, z);
|
||||
double wz = getModifiedZ(x, z);
|
||||
IrisRegion region = sampleRegion(x, z);
|
||||
int height = (int) Math.round(getTerrainHeight(x, z));
|
||||
double sh = region.getShoreHeight(wx, wz);
|
||||
IrisBiome current = sampleBiome(x, z).getBiome();
|
||||
|
||||
// Stop shores from spawning on land
|
||||
if(current.isShore() && height > sh)
|
||||
{
|
||||
return glBiome.generateLandData(wx, wz, x, z, region);
|
||||
}
|
||||
|
||||
// Stop land & shore from spawning underwater
|
||||
if(current.isShore() || current.isLand() && height <= getDimension().getFluidHeight())
|
||||
{
|
||||
return glBiome.generateSeaData(wx, wz, x, z, region);
|
||||
}
|
||||
|
||||
// Stop oceans from spawning on land
|
||||
if(current.isSea() && height > getDimension().getFluidHeight())
|
||||
{
|
||||
return glBiome.generateLandData(wx, wz, x, z, region);
|
||||
}
|
||||
|
||||
// Stop land from spawning underwater
|
||||
if(height <= getDimension().getFluidHeight())
|
||||
{
|
||||
return glBiome.generateSeaData(wx, wz, x, z, region);
|
||||
}
|
||||
|
||||
// Stop land from spawning where shores go
|
||||
if(height <= getDimension().getFluidHeight() + sh)
|
||||
{
|
||||
return glBiome.generateShoreData(wx, wz, x, z, region);
|
||||
}
|
||||
|
||||
return glBiome.generateRegionData(wx, wz, x, z, region);
|
||||
}
|
||||
|
||||
public BiomeResult sampleTrueBiome(int x, int z)
|
||||
{
|
||||
if(!getDimension().getFocus().equals(""))
|
||||
{
|
||||
return focus();
|
||||
}
|
||||
|
||||
double wx = getModifiedX(x, z);
|
||||
double wz = getModifiedZ(x, z);
|
||||
IrisRegion region = sampleRegion(x, z);
|
||||
int height = sampleHeight(x, z);
|
||||
double sh = region.getShoreHeight(wx, wz);
|
||||
BiomeResult res = sampleTrueBiomeBase(x, z);
|
||||
IrisBiome current = res.getBiome();
|
||||
|
||||
// Stop oceans from spawning on the first level of beach
|
||||
if(current.isSea() && height > getDimension().getFluidHeight() - sh)
|
||||
{
|
||||
return glBiome.generateShoreData(wx, wz, x, z, region);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
public boolean isUnderwater(int x, int z)
|
||||
{
|
||||
return isFluidAtHeight(x, z);
|
||||
}
|
||||
|
||||
public boolean isFluidAtHeight(int x, int z)
|
||||
{
|
||||
return Math.round(getTerrainHeight(x, z)) < getFluidHeight();
|
||||
}
|
||||
|
||||
public int getFluidHeight()
|
||||
{
|
||||
return getDimension().getFluidHeight();
|
||||
}
|
||||
|
||||
public double getTerrainHeight(int x, int z)
|
||||
{
|
||||
return getNoiseHeight(x, z) + getFluidHeight();
|
||||
}
|
||||
|
||||
public double getTerrainWaterHeight(int x, int z)
|
||||
{
|
||||
return Math.max(getTerrainHeight(x, z), getFluidHeight());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user