Implement new parsing library, implement TerraProfiler

This commit is contained in:
dfsek 2020-09-13 02:18:26 -07:00
parent d626e8c735
commit 0eddcf429c
12 changed files with 146 additions and 123 deletions

View File

@ -83,7 +83,7 @@
<dependency> <dependency>
<groupId>org.polydev</groupId> <groupId>org.polydev</groupId>
<artifactId>gaea</artifactId> <artifactId>gaea</artifactId>
<version>1.4.0</version> <version>1.5.7</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,75 +1,13 @@
package com.dfsek.terra; package com.dfsek.terra;
import com.dfsek.terra.config.ConfigUtil; import com.dfsek.terra.config.ConfigUtil;
import com.dfsek.terra.config.WorldConfig;
import com.dfsek.terra.math.NoiseFunction2;
import com.dfsek.terra.math.NoiseFunction3;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.polydev.gaea.math.parsii.eval.Expression;
import org.polydev.gaea.math.parsii.eval.Function;
import org.polydev.gaea.math.parsii.eval.Parser;
import java.util.List;
public class Terra extends JavaPlugin { public class Terra extends JavaPlugin {
static {
Parser.registerFunction("floor", new Function() {
@Override
public int getNumberOfArguments() {
return 1;
}
@Override
public double eval(List<Expression> list) {
return Math.floor(list.get(0).evaluate());
}
@Override
public boolean isNaturalFunction() {
return true;
}
});
Parser.registerFunction("ceil", new Function() {
@Override
public int getNumberOfArguments() {
return 1;
}
@Override
public double eval(List<Expression> list) {
return Math.ceil(list.get(0).evaluate());
}
@Override
public boolean isNaturalFunction() {
return true;
}
});
Parser.registerFunction("round", new Function() {
@Override
public int getNumberOfArguments() {
return 1;
}
@Override
public double eval(List<Expression> list) {
return Math.round(list.get(0).evaluate());
}
@Override
public boolean isNaturalFunction() {
return true;
}
});
Parser.registerFunction("noise2", new NoiseFunction2());
Parser.registerFunction("noise3", new NoiseFunction3());
}
private static FileConfiguration config; private static FileConfiguration config;
private static Terra instance; private static Terra instance;

View File

@ -8,11 +8,11 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.profiler.WorldProfiler;
public class TerraCommand implements CommandExecutor { public class TerraCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(args.length == 1) {
switch(args[0]) { switch(args[0]) {
case "reload": case "reload":
ConfigUtil.loadConfig(Terra.getInstance()); ConfigUtil.loadConfig(Terra.getInstance());
@ -21,7 +21,31 @@ public class TerraCommand implements CommandExecutor {
case "biome": case "biome":
if(!(sender instanceof Player)) return false; if(!(sender instanceof Player)) return false;
sender.sendMessage("You are in " + ((UserDefinedBiome) TerraBiomeGrid.fromWorld(((Player) sender).getWorld()).getBiome(((Player) sender).getLocation())).getConfig().getFriendlyName()); sender.sendMessage("You are in " + ((UserDefinedBiome) TerraBiomeGrid.fromWorld(((Player) sender).getWorld()).getBiome(((Player) sender).getLocation())).getConfig().getFriendlyName());
case "profile":
if(! (sender instanceof Player)) {
sender.sendMessage("Command is for players only.");
return true;
} }
Player p = (Player) sender;
if(p.getWorld().getGenerator() instanceof TerraChunkGenerator) {
WorldProfiler profile = TerraProfiler.fromWorld(p.getWorld());
if(args.length > 1 && "query".equals(args[1])) {
sender.sendMessage(profile.getResultsFormatted());
return true;
} else if(args.length > 1 && "reset".equals(args[1])) {
profile.reset();
sender.sendMessage("Profiler has been reset.");
return true;
} else if(args.length > 1 && "start".equals(args[1])) {
profile.setProfiling(true);
sender.sendMessage("Profiler has started.");
return true;
} else if(args.length > 1 && "stop".equals(args[1])) {
profile.setProfiling(false);
sender.sendMessage("Profiler has stopped.");
return true;
}
} else sender.sendMessage("World is not a Terra world!");
} }
return true; return true;
} }

View File

@ -0,0 +1,30 @@
package com.dfsek.terra;
import org.bukkit.World;
import org.polydev.gaea.profiler.DataType;
import org.polydev.gaea.profiler.Measurement;
import org.polydev.gaea.profiler.WorldProfiler;
import java.util.HashMap;
import java.util.Map;
public class TerraProfiler extends WorldProfiler {
private static final Map<World, TerraProfiler> profilerMap = new HashMap<>();
public TerraProfiler(World w) {
super(w);
this.addMeasurement(new Measurement(2500000, DataType.PERIOD_MILLISECONDS), "TotalChunkGenTime")
.addMeasurement(new Measurement(2500000, DataType.PERIOD_MILLISECONDS), "ChunkBaseGenTime")
.addMeasurement(new Measurement(2000000, DataType.PERIOD_MILLISECONDS), "BiomeSetTime")
.addMeasurement(new Measurement(25000000, DataType.PERIOD_MILLISECONDS), "TreeGenTime")
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "FaunaTime");
profilerMap.put(w, this);
}
public static TerraProfiler fromWorld(World w) {
if(w.getGenerator() instanceof TerraChunkGenerator) {
if(profilerMap.containsKey(w)) return profilerMap.get(w);
TerraProfiler p = new TerraProfiler(w);
profilerMap.put(w, p);
return p;
} else throw new IllegalArgumentException("Attempted to instantiate/fetch Profiler for non-Terra world!");
}
}

View File

@ -2,14 +2,18 @@ package com.dfsek.terra.biome;
import com.dfsek.terra.config.BiomeConfig; import com.dfsek.terra.config.BiomeConfig;
import com.dfsek.terra.config.ConfigUtil; import com.dfsek.terra.config.ConfigUtil;
import org.bukkit.Bukkit;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.polydev.gaea.biome.Biome; import org.polydev.gaea.biome.Biome;
import org.polydev.gaea.biome.BiomeTerrain; import org.polydev.gaea.biome.BiomeTerrain;
import org.polydev.gaea.biome.Decorator; import org.polydev.gaea.biome.Decorator;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.parsii.eval.Parser; import org.polydev.gaea.math.parsii.eval.Parser;
import org.polydev.gaea.math.parsii.eval.Scope; import org.polydev.gaea.math.parsii.eval.Scope;
import org.polydev.gaea.math.parsii.tokenizer.ParseException; import org.polydev.gaea.math.parsii.tokenizer.ParseException;
import org.polydev.gaea.structures.features.Feature; import org.polydev.gaea.structures.features.Feature;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.world.BlockPalette; import org.polydev.gaea.world.BlockPalette;
import java.util.Collections; import java.util.Collections;
@ -24,16 +28,27 @@ public class UserDefinedBiome implements Biome {
private final UserDefinedDecorator decorator; private final UserDefinedDecorator decorator;
public UserDefinedBiome(BiomeConfig config) throws ParseException { public UserDefinedBiome(BiomeConfig config) throws ParseException {
this.config = config; this.config = config;
Scope s = Scope.create();
TreeMap<Integer, BlockPalette> paletteMap = new TreeMap<>(); TreeMap<Integer, BlockPalette> paletteMap = new TreeMap<>();
for(Map.Entry<String, Object> e : config.getConfigurationSection("palette").getValues(false).entrySet()) { for(Map<?, ?> e : config.getMapList("palette")) {
paletteMap.put((Integer) e.getValue(), ConfigUtil.getPalette(e.getKey()).getPalette()); for(Map.Entry<?, ?> entry : e.entrySet()) {
try {
if(((String) entry.getKey()).startsWith("BLOCK:")) {
try {
paletteMap.put((Integer) entry.getValue(), new BlockPalette().addBlockData(new ProbabilityCollection<BlockData>().add(Bukkit.createBlockData((String) entry.getKey()), 1), 1));
} catch(IllegalArgumentException ex) {
Bukkit.getLogger().severe("SEVERE configuration error for BlockPalettes in biome" + config.getFriendlyName() + ", ID: " + config.getBiomeID() + ". BlockData " + entry.getKey() + " is invalid!");
}
}
else paletteMap.put((Integer) entry.getValue(), ConfigUtil.getPalette((String) entry.getKey()).getPalette());
} catch(ClassCastException ex) {
Bukkit.getLogger().severe("SEVERE configuration error for BlockPalettes in biome" + config.getFriendlyName() + ", ID: " + config.getBiomeID());
}
}
} }
this.decorator = new UserDefinedDecorator(config); this.decorator = new UserDefinedDecorator(config);
gen = new UserDefinedGenerator(s, Parser.parse(Objects.requireNonNull(config.getString("noise-equation")), s), Collections.emptyList(), paletteMap); gen = new UserDefinedGenerator(Objects.requireNonNull(config.getString("noise-equation")), Collections.emptyList(), paletteMap);
} }
public BiomeConfig getConfig() { public BiomeConfig getConfig() {

View File

@ -1,12 +1,15 @@
package com.dfsek.terra.biome; package com.dfsek.terra.biome;
import com.dfsek.terra.Terra;
import com.dfsek.terra.math.NoiseFunction2; import com.dfsek.terra.math.NoiseFunction2;
import com.dfsek.terra.math.NoiseFunction3; import com.dfsek.terra.math.NoiseFunction3;
import org.polydev.gaea.biome.BiomeTerrain; import org.polydev.gaea.biome.BiomeTerrain;
import org.polydev.gaea.math.FastNoise; import org.polydev.gaea.math.FastNoise;
import org.polydev.gaea.math.parsii.eval.Expression; 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.Scope;
import org.polydev.gaea.math.parsii.eval.Variable; import org.polydev.gaea.math.parsii.eval.Variable;
import org.polydev.gaea.math.parsii.tokenizer.ParseException;
import org.polydev.gaea.world.BlockPalette; import org.polydev.gaea.world.BlockPalette;
import java.util.List; import java.util.List;
@ -16,19 +19,26 @@ import java.util.TreeMap;
public class UserDefinedGenerator extends BiomeTerrain { public class UserDefinedGenerator extends BiomeTerrain {
private final Expression noiseExp; private final Expression noiseExp;
private final List<Variable> vars; private final List<Variable> vars;
private final Variable xVar; private final Scope s = new Scope();
private final Variable yVar; private final Variable xVar = s.getVariable("x");;
private final Variable zVar; private final Variable yVar = s.getVariable("y");
private final Variable zVar = s.getVariable("z");;
private final TreeMap<Integer, BlockPalette> paletteMap; private final TreeMap<Integer, BlockPalette> paletteMap;
private final NoiseFunction2 n2 = new NoiseFunction2();
private final NoiseFunction3 n3 = new NoiseFunction3();
private final Parser p = new Parser();
{
p.registerFunction("noise2", n2);
p.registerFunction("noise3", n3);
}
private static final Object noiseLock = new Object();
public UserDefinedGenerator(Scope s, Expression e, List<Variable> v, TreeMap<Integer, BlockPalette> p) { public UserDefinedGenerator(String e, List<Variable> v, TreeMap<Integer, BlockPalette> pa) throws ParseException {
this.noiseExp = e;
this.vars = v; this.vars = v;
this.paletteMap = p; this.paletteMap = pa;
this.xVar = s.getVariable("x"); this.noiseExp = p.parse(e, s);
this.yVar = s.getVariable("y");
this.zVar = s.getVariable("z");
} }
/** /**
* Gets the 2D noise at a pair of coordinates using the provided FastNoise instance. * Gets the 2D noise at a pair of coordinates using the provided FastNoise instance.
@ -40,13 +50,15 @@ public class UserDefinedGenerator extends BiomeTerrain {
*/ */
@Override @Override
public double getNoise(FastNoise gen, int x, int z) { public double getNoise(FastNoise gen, int x, int z) {
synchronized(noiseLock) {
xVar.setValue(x); xVar.setValue(x);
yVar.setValue(0); yVar.setValue(0);
zVar.setValue(z); zVar.setValue(z);
NoiseFunction2.setNoise(gen); n2.setNoise(gen, false);
NoiseFunction3.setNoise(gen); n3.setNoise(gen, false);
return noiseExp.evaluate(); return noiseExp.evaluate();
} }
}
/** /**
* Gets the 3D noise at a pair of coordinates using the provided FastNoise instance. * Gets the 3D noise at a pair of coordinates using the provided FastNoise instance.
@ -59,13 +71,15 @@ public class UserDefinedGenerator extends BiomeTerrain {
*/ */
@Override @Override
public double getNoise(FastNoise gen, int x, int y, int z) { public double getNoise(FastNoise gen, int x, int y, int z) {
synchronized(noiseLock) {
xVar.setValue(x); xVar.setValue(x);
yVar.setValue(y); yVar.setValue(y);
zVar.setValue(z); zVar.setValue(z);
NoiseFunction2.setNoise(gen); n2.setNoise(gen, false);
NoiseFunction3.setNoise(gen); n3.setNoise(gen, false);
return noiseExp.evaluate(); return noiseExp.evaluate();
} }
}
/** /**
* Gets the BlocPalette to generate the biome with. * Gets the BlocPalette to generate the biome with.

View File

@ -39,6 +39,7 @@ public class BiomeConfig extends YamlConfiguration {
if(!contains("name")) throw new InvalidConfigurationException("Biome Name unspecified!"); if(!contains("name")) throw new InvalidConfigurationException("Biome Name unspecified!");
this.friendlyName = getString("name"); this.friendlyName = getString("name");
if(!contains("vanilla")) throw new InvalidConfigurationException("Vanila Biome unspecified!"); if(!contains("vanilla")) throw new InvalidConfigurationException("Vanila Biome unspecified!");
if(!contains("palette")) throw new InvalidConfigurationException("Palette unspecified!");
try { try {
this.vanillaBiome = org.bukkit.block.Biome.valueOf(getString("vanilla")); this.vanillaBiome = org.bukkit.block.Biome.valueOf(getString("vanilla"));
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {

View File

@ -66,9 +66,9 @@ public class WorldConfig {
} }
seaLevel = config.getInt("sea-level", 63); seaLevel = config.getInt("sea-level", 63);
zoneFreq = config.getInt("frequencies.zone", 1536); zoneFreq = 1f/config.getInt("frequencies.zone", 1536);
freq1 = config.getInt("frequencies.grid-1", 256); freq1 = 1f/config.getInt("frequencies.grid-1", 256);
freq2 = config.getInt("frequencies.grid-2", 512); freq2 = 1f/config.getInt("frequencies.grid-2", 512);
try (Stream<Path> paths = Files.walk(Paths.get(main.getDataFolder() + File.separator + "grids"))) { try (Stream<Path> paths = Files.walk(Paths.get(main.getDataFolder() + File.separator + "grids"))) {
paths paths

View File

@ -7,7 +7,7 @@ import org.polydev.gaea.math.parsii.eval.Function;
import java.util.List; import java.util.List;
public class NoiseFunction2 implements Function { public class NoiseFunction2 implements Function {
private static FastNoise gen = new FastNoise(); private FastNoise gen;
@Override @Override
public int getNumberOfArguments() { public int getNumberOfArguments() {
@ -19,16 +19,12 @@ public class NoiseFunction2 implements Function {
return gen.getSimplexFractal((float) list.get(0).evaluate(), (float) list.get(1).evaluate()); return gen.getSimplexFractal((float) list.get(0).evaluate(), (float) list.get(1).evaluate());
} }
public void setNoise(FastNoise gen, boolean override) {
if(this.gen == null || override) this.gen = gen;
}
@Override @Override
public boolean isNaturalFunction() { public boolean isNaturalFunction() {
return true; return true;
} }
public static void setNoise(FastNoise gen) {
NoiseFunction2.gen = gen;
}
public static FastNoise getNoise() {
return gen;
}
} }

View File

@ -7,8 +7,7 @@ import org.polydev.gaea.math.parsii.eval.Function;
import java.util.List; import java.util.List;
public class NoiseFunction3 implements Function { public class NoiseFunction3 implements Function {
private static FastNoise gen = new FastNoise(); private FastNoise gen;
@Override @Override
public int getNumberOfArguments() { public int getNumberOfArguments() {
return 3; return 3;
@ -19,16 +18,12 @@ public class NoiseFunction3 implements Function {
return gen.getSimplexFractal((float) list.get(0).evaluate(), (float) list.get(1).evaluate(), (float) list.get(2).evaluate()); return gen.getSimplexFractal((float) list.get(0).evaluate(), (float) list.get(1).evaluate(), (float) list.get(2).evaluate());
} }
public void setNoise(FastNoise gen, boolean override) {
if(this.gen == null || override) this.gen = gen;
}
@Override @Override
public boolean isNaturalFunction() { public boolean isNaturalFunction() {
return true; return true;
} }
public static void setNoise(FastNoise gen) {
NoiseFunction3.gen = gen;
}
public static FastNoise getNoise() {
return gen;
}
} }

View File

@ -1,5 +1,7 @@
package com.dfsek.terra.population; package com.dfsek.terra.population;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.biome.TerraBiomeGrid; import com.dfsek.terra.biome.TerraBiomeGrid;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.World; import org.bukkit.World;
@ -7,6 +9,7 @@ import org.bukkit.block.Block;
import org.bukkit.generator.BlockPopulator; import org.bukkit.generator.BlockPopulator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.biome.Biome; import org.polydev.gaea.biome.Biome;
import org.polydev.gaea.profiler.ProfileFuture;
import org.polydev.gaea.world.Fauna; import org.polydev.gaea.world.Fauna;
import java.util.Random; import java.util.Random;
@ -14,16 +17,19 @@ import java.util.Random;
public class FaunaPopulator extends BlockPopulator { public class FaunaPopulator extends BlockPopulator {
@Override @Override
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) { public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) {
ProfileFuture fauna = TerraProfiler.fromWorld(world).measure("FaunaTime");
for(int x = 0; x < 16; x++) { for(int x = 0; x < 16; x++) {
for(int z = 0; z < 16; z++) { for(int z = 0; z < 16; z++) {
Biome biome = TerraBiomeGrid.fromWorld(world).getBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z); Biome biome = TerraBiomeGrid.fromWorld(world).getBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z);
if(biome.getDecorator().getFaunaChance() <= 0 || random.nextInt(100) > biome.getDecorator().getFaunaChance()) if(biome.getDecorator().getFaunaChance() <= 0 || random.nextInt(100) > biome.getDecorator().getFaunaChance())
continue; continue;
Block highest = Fauna.getHighestValidSpawnAt(chunk, x, z); Fauna item = biome.getDecorator().getFauna().get(random);
Block highest = item.getHighestValidSpawnAt(chunk, x, z);
try { try {
if(highest != null) biome.getDecorator().getFauna().get(random).plant(highest.getLocation()); if(highest != null) item.plant(highest.getLocation());
} catch(NullPointerException ignored) {} } catch(NullPointerException ignored) {}
} }
} }
if(fauna!=null) fauna.complete();
} }
} }

View File

@ -1,6 +1,7 @@
package com.dfsek.terra.population; package com.dfsek.terra.population;
import com.dfsek.terra.Terra; import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.biome.TerraBiomeGrid; import com.dfsek.terra.biome.TerraBiomeGrid;
import com.dfsek.terra.biome.UserDefinedDecorator; import com.dfsek.terra.biome.UserDefinedDecorator;
import org.bukkit.Chunk; import org.bukkit.Chunk;
@ -10,6 +11,7 @@ import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.biome.Biome; import org.polydev.gaea.biome.Biome;
import org.polydev.gaea.population.GaeaBlockPopulator; import org.polydev.gaea.population.GaeaBlockPopulator;
import org.polydev.gaea.population.PopulationManager; import org.polydev.gaea.population.PopulationManager;
import org.polydev.gaea.profiler.ProfileFuture;
import org.polydev.gaea.util.WorldUtil; import org.polydev.gaea.util.WorldUtil;
import java.util.Random; import java.util.Random;
@ -22,6 +24,7 @@ public class TreePopulator extends GaeaBlockPopulator {
@Override @Override
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) { public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) {
ProfileFuture tree = TerraProfiler.fromWorld(world).measure("TreeGenTime");
int x = random.nextInt(16); // Decrease chances of chunk-crossing trees int x = random.nextInt(16); // Decrease chances of chunk-crossing trees
int z = random.nextInt(16); int z = random.nextInt(16);
Location origin = chunk.getBlock(x, 0, z).getLocation(); Location origin = chunk.getBlock(x, 0, z).getLocation();
@ -41,5 +44,6 @@ public class TreePopulator extends GaeaBlockPopulator {
x = random.nextInt(16); // Decrease chances of chunk-crossing trees x = random.nextInt(16); // Decrease chances of chunk-crossing trees
z = random.nextInt(16); z = random.nextInt(16);
} }
if(tree!=null) tree.complete();
} }
} }