Implement chunk serialization

This commit is contained in:
dfsek
2020-09-26 01:17:46 -07:00
parent f727e9d297
commit c8e564c2d3
19 changed files with 166 additions and 36 deletions

View File

@@ -5,6 +5,7 @@ import com.dfsek.terra.config.WorldConfig;
import com.dfsek.terra.population.CavePopulator;
import com.dfsek.terra.population.FloraPopulator;
import com.dfsek.terra.population.OrePopulator;
import com.dfsek.terra.population.StructurePopulator;
import com.dfsek.terra.population.TreePopulator;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@@ -18,17 +19,25 @@ import org.polydev.gaea.math.ChunkInterpolator;
import org.polydev.gaea.math.FastNoise;
import org.polydev.gaea.population.PopulationManager;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class TerraChunkGenerator extends GaeaChunkGenerator {
private static final BlockData STONE = Material.STONE.createBlockData();
private static final BlockData WATER = Material.WATER.createBlockData();
private final PopulationManager popMan = new PopulationManager();
private boolean needsLoad = true;
private static final Map<World, PopulationManager> popMap = new HashMap<>();
public TerraChunkGenerator() {
super(ChunkInterpolator.InterpolationType.TRILINEAR);
popMan.attach(new StructurePopulator());
popMan.attach(new TreePopulator());
popMan.attach(new FloraPopulator());
popMan.attach(new OrePopulator());
@@ -36,6 +45,7 @@ public class TerraChunkGenerator extends GaeaChunkGenerator {
@Override
public ChunkData generateBase(@NotNull World world, @NotNull Random random, int chunkX, int chunkZ, FastNoise fastNoise) {
if(needsLoad) load(world);
ChunkData chunk = createChunkData(world);
int sea = WorldConfig.fromWorld(world).seaLevel;
for(byte x = 0; x < 16; x++) {
@@ -49,6 +59,31 @@ public class TerraChunkGenerator extends GaeaChunkGenerator {
return chunk;
}
private void load(World w) {
try {
popMan.loadBlocks(w);
} catch(IOException e) {
if(e instanceof FileNotFoundException) {
Bukkit.getLogger().warning("[Terra] No population chunks were loaded. If this is your first time starting your server with Terra, or if you are creating a new world, this is normal.");
} else e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
popMap.put(w, popMan);
needsLoad = false;
}
public static void saveAll() {
for(Map.Entry<World, PopulationManager> e : popMap.entrySet()) {
try {
e.getValue().saveBlocks(e.getKey());
Bukkit.getLogger().info("[Terra] Saved data for world " + e.getKey().getName());
} catch(IOException ioException) {
ioException.printStackTrace();
}
}
}
@Override
public int getNoiseOctaves(World world) {
return 4;

View File

@@ -0,0 +1,59 @@
package com.dfsek.terra.generation;
import org.bukkit.block.Biome;
import org.polydev.gaea.biome.Decorator;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.world.Flora;
public class UserDefinedDecorator extends Decorator {
private final ProbabilityCollection<Flora> flora;
private final ProbabilityCollection<Tree> trees;
private final int floraChance;
private final int treeChance;
private final int treeDensity;
public UserDefinedDecorator(ProbabilityCollection<Flora> flora, ProbabilityCollection<Tree> trees, int floraChance, int treeChance, int treeDensity) {
this.flora = flora;
this.trees = trees;
this.floraChance = floraChance;
this.treeChance = treeChance;
this.treeDensity = treeDensity;
}
@Override
public ProbabilityCollection<Tree> getTrees() {
return trees;
}
public int getTreeChance() {
return treeChance;
}
@Override
public int getTreeDensity() {
return treeDensity;
}
@Override
public boolean overrideStructureChance() {
return false;
}
@Override
public Biome getVanillaBiome() {
return null;
}
@Override
public ProbabilityCollection<Flora> getFlora() {
return flora;
}
@Override
public int getFloraChance() {
return floraChance;
}
}

View File

@@ -0,0 +1,94 @@
package com.dfsek.terra.generation;
import com.dfsek.terra.math.NoiseFunction2;
import com.dfsek.terra.math.NoiseFunction3;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.polydev.gaea.biome.Generator;
import org.polydev.gaea.math.FastNoise;
import org.polydev.gaea.math.parsii.eval.Expression;
import org.polydev.gaea.math.parsii.eval.Parser;
import org.polydev.gaea.math.parsii.eval.Scope;
import org.polydev.gaea.math.parsii.eval.Variable;
import org.polydev.gaea.math.parsii.tokenizer.ParseException;
import org.polydev.gaea.world.palette.Palette;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class UserDefinedGenerator extends Generator {
private final Expression noiseExp;
private final Scope s = new Scope();
private final Variable xVar = s.getVariable("x");;
private final Variable yVar = s.getVariable("y");
private final Variable zVar = s.getVariable("z");;
private final TreeMap<Integer, Palette<BlockData>> paletteMap;
private final NoiseFunction2 n2 = new NoiseFunction2();
private final NoiseFunction3 n3 = new NoiseFunction3();
private static final Object noiseLock = new Object();
public UserDefinedGenerator(String e, List<Variable> v, TreeMap<Integer, Palette<BlockData>> pa) throws ParseException {
Parser p = new Parser();
p.registerFunction("noise2", n2);
p.registerFunction("noise3", n3);
this.paletteMap = pa;
this.noiseExp = p.parse(e, s);
}
/**
* Gets the 2D noise at a pair of coordinates using the provided FastNoise instance.
*
* @param gen - The FastNoise instance to use.
* @param x - The x coordinate.
* @param z - The z coordinate.
* @return double - Noise value at the specified coordinates.
*/
@Override
public double getNoise(FastNoise gen, World w, int x, int z) {
synchronized(noiseLock) {
xVar.setValue(x);
yVar.setValue(0);
zVar.setValue(z);
n2.setNoise(gen, false);
n3.setNoise(gen, false);
return noiseExp.evaluate();
}
}
/**
* Gets the 3D noise at a pair of coordinates using the provided FastNoise instance.
*
* @param gen - The FastNoise instance to use.
* @param x - The x coordinate.
* @param y - The y coordinate.
* @param z - The z coordinate.
* @return double - Noise value at the specified coordinates.
*/
@Override
public double getNoise(FastNoise gen, World w, int x, int y, int z) {
synchronized(noiseLock) {
xVar.setValue(x);
yVar.setValue(y);
zVar.setValue(z);
n2.setNoise(gen, false);
n3.setNoise(gen, false);
return noiseExp.evaluate();
}
}
/**
* Gets the BlocPalette to generate the biome with.
*
* @return BlocPalette - The biome's palette.
*/
@Override
public Palette<BlockData> getPalette(int y) {
for(Map.Entry<Integer, Palette<BlockData>> e : paletteMap.entrySet()) {
if(e.getKey() >= y ) return e.getValue();
}
return null;
}
}