population order/invocation is now per-platform

This commit is contained in:
dfsek committed 2020-12-14 12:44:16 -07:00
1 parent 5f5504100b
commit 875e1feafe
5 files changed
+66 -74

No files matched your search

@@ -21,7 +21,6 @@ public class WorldProfiler {
.addMeasurement(new Measurement(2000000, DataType.PERIOD_MILLISECONDS), "PopulationManagerTime"); .addMeasurement(new Measurement(2000000, DataType.PERIOD_MILLISECONDS), "PopulationManagerTime");
isProfiling = false; isProfiling = false;
this.world = w; this.world = w;
w.getGenerator().getTerraGenerator().attachProfiler(this);
} }
public String getResultsFormatted() { public String getResultsFormatted() {
@@ -1,13 +1,11 @@
package com.dfsek.terra.api.generic.generator; package com.dfsek.terra.api.generic.generator;
import com.dfsek.terra.api.gaea.profiler.WorldProfiler;
import com.dfsek.terra.api.generic.TerraPlugin; import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.api.generic.world.BiomeGrid; import com.dfsek.terra.api.generic.world.BiomeGrid;
import com.dfsek.terra.api.generic.world.World; import com.dfsek.terra.api.generic.world.World;
import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.config.base.ConfigPack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Random; import java.util.Random;
public interface TerraChunkGenerator { public interface TerraChunkGenerator {
@@ -15,8 +13,6 @@ public interface TerraChunkGenerator {
void generateBiomes(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome); void generateBiomes(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome);
void attachProfiler(WorldProfiler profiler);
boolean isParallelCapable(); boolean isParallelCapable();
boolean shouldGenerateCaves(); boolean shouldGenerateCaves();
@@ -29,7 +25,5 @@ public interface TerraChunkGenerator {
ConfigPack getConfigPack(); ConfigPack getConfigPack();
List<TerraBlockPopulator> getPopulators();
TerraPlugin getMain(); TerraPlugin getMain();
} }
@@ -4,71 +4,33 @@ import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.api.gaea.biome.Biome; import com.dfsek.terra.api.gaea.biome.Biome;
import com.dfsek.terra.api.gaea.generation.GenerationPhase; import com.dfsek.terra.api.gaea.generation.GenerationPhase;
import com.dfsek.terra.api.gaea.math.ChunkInterpolator3; import com.dfsek.terra.api.gaea.math.ChunkInterpolator3;
import com.dfsek.terra.api.gaea.population.PopulationManager;
import com.dfsek.terra.api.gaea.profiler.ProfileFuture; import com.dfsek.terra.api.gaea.profiler.ProfileFuture;
import com.dfsek.terra.api.gaea.profiler.WorldProfiler;
import com.dfsek.terra.api.gaea.world.palette.Palette; import com.dfsek.terra.api.gaea.world.palette.Palette;
import com.dfsek.terra.api.generic.TerraPlugin; import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.api.generic.generator.ChunkGenerator; import com.dfsek.terra.api.generic.generator.ChunkGenerator;
import com.dfsek.terra.api.generic.generator.TerraBlockPopulator;
import com.dfsek.terra.api.generic.world.BiomeGrid; import com.dfsek.terra.api.generic.world.BiomeGrid;
import com.dfsek.terra.api.generic.world.Chunk;
import com.dfsek.terra.api.generic.world.World; import com.dfsek.terra.api.generic.world.World;
import com.dfsek.terra.api.generic.world.block.BlockData; import com.dfsek.terra.api.generic.world.block.BlockData;
import com.dfsek.terra.api.generic.world.vector.Vector3; import com.dfsek.terra.api.generic.world.vector.Vector3;
import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.templates.BiomeTemplate; import com.dfsek.terra.config.templates.BiomeTemplate;
import com.dfsek.terra.debug.Debug;
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 com.dfsek.terra.util.PaletteUtil; import com.dfsek.terra.util.PaletteUtil;
import com.dfsek.terra.util.SlabUtil; import com.dfsek.terra.util.SlabUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
public class TerraChunkGenerator implements com.dfsek.terra.api.generic.generator.TerraChunkGenerator { public class TerraChunkGenerator implements com.dfsek.terra.api.generic.generator.TerraChunkGenerator {
private static final Map<World, PopulationManager> popMap = new HashMap<>();
private final PopulationManager popMan;
private final ConfigPack configPack; private final ConfigPack configPack;
private final TerraPlugin main; private final TerraPlugin main;
private boolean needsLoad = true;
public TerraChunkGenerator(ConfigPack c, TerraPlugin main) { public TerraChunkGenerator(ConfigPack c, TerraPlugin main) {
popMan = new PopulationManager(main);
this.configPack = c; this.configPack = c;
this.main = main; this.main = main;
popMan.attach(new OrePopulator(main));
popMan.attach(new TreePopulator(main));
popMan.attach(new FloraPopulator(main));
}
public static synchronized void saveAll() {
for(Map.Entry<World, PopulationManager> e : popMap.entrySet()) {
try {
e.getValue().saveBlocks(e.getKey());
Debug.info("Saved data for world " + e.getKey().getName());
} catch(IOException ioException) {
ioException.printStackTrace();
}
}
}
public static synchronized void fixChunk(Chunk c) {
if(!(c.getWorld().getGenerator() instanceof TerraChunkGenerator)) throw new IllegalArgumentException();
popMap.get(c.getWorld()).checkNeighbors(c.getX(), c.getZ(), c.getWorld());
} }
@Override @Override
@@ -101,17 +63,11 @@ public class TerraChunkGenerator implements com.dfsek.terra.api.generic.generato
return configPack; return configPack;
} }
@Override
public List<TerraBlockPopulator> getPopulators() {
return Arrays.asList(new CavePopulator(main), new StructurePopulator(main), popMan);
}
@Override @Override
public TerraPlugin getMain() { public TerraPlugin getMain() {
return main; return main;
} }
@Override @Override
@SuppressWarnings({"try"}) @SuppressWarnings({"try"})
public ChunkGenerator.ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int chunkX, int chunkZ, ChunkGenerator.ChunkData chunk) { public ChunkGenerator.ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int chunkX, int chunkZ, ChunkGenerator.ChunkData chunk) {
@@ -121,7 +77,6 @@ public class TerraChunkGenerator implements com.dfsek.terra.api.generic.generato
ChunkInterpolator3 interp; ChunkInterpolator3 interp;
try(ProfileFuture ignored = tw.getProfiler().measure("ChunkBaseGenTime")) { try(ProfileFuture ignored = tw.getProfiler().measure("ChunkBaseGenTime")) {
interp = new ChunkInterpolator3(world, chunkX, chunkZ, tw.getGrid()); interp = new ChunkInterpolator3(world, chunkX, chunkZ, tw.getGrid());
if(needsLoad) load(world); // Load population data for world.
if(!tw.isSafe()) return chunk; if(!tw.isSafe()) return chunk;
int xOrig = (chunkX << 4); int xOrig = (chunkX << 4);
@@ -197,20 +152,4 @@ public class TerraChunkGenerator implements com.dfsek.terra.api.generic.generato
} }
} }
} }
public void attachProfiler(WorldProfiler p) {
popMan.attachProfiler(p);
}
private void load(World w) {
try {
popMan.loadBlocks(w);
} catch(FileNotFoundException e) {
LangUtil.log("warning.no-population", Level.WARNING);
} catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}
popMap.put(w, popMan);
needsLoad = false;
}
} }
@@ -77,7 +77,7 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
TerraChunkGenerator.saveAll(); BukkitChunkGeneratorWrapper.saveAll();
} }
@Override @Override
@@ -107,7 +107,7 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
long save = config.getDataSaveInterval(); long save = config.getDataSaveInterval();
Bukkit.getScheduler().runTaskTimerAsynchronously(this, TerraChunkGenerator::saveAll, save, save); // Schedule population data saving Bukkit.getScheduler().runTaskTimerAsynchronously(this, BukkitChunkGeneratorWrapper::saveAll, save, save); // Schedule population data saving
Bukkit.getPluginManager().registerEvents(new EventListener(this), this); // Register master event listener Bukkit.getPluginManager().registerEvents(new EventListener(this), this); // Register master event listener
Bukkit.getPluginManager().registerEvents(new SpigotListener(this), this); // Register Spigot event listener, once Paper accepts StructureLocateEvent PR Spigot and Paper events will be separate. Bukkit.getPluginManager().registerEvents(new SpigotListener(this), this); // Register Spigot event listener, once Paper accepts StructureLocateEvent PR Spigot and Paper events will be separate.
@@ -1,35 +1,95 @@
package com.dfsek.terra.bukkit.generator; package com.dfsek.terra.bukkit.generator;
import com.dfsek.terra.api.gaea.population.PopulationManager;
import com.dfsek.terra.api.generic.Handle; import com.dfsek.terra.api.generic.Handle;
import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.api.generic.generator.TerraChunkGenerator; import com.dfsek.terra.api.generic.generator.TerraChunkGenerator;
import com.dfsek.terra.api.generic.world.Chunk;
import com.dfsek.terra.bukkit.BukkitBiomeGrid; import com.dfsek.terra.bukkit.BukkitBiomeGrid;
import com.dfsek.terra.bukkit.BukkitWorld; import com.dfsek.terra.bukkit.BukkitWorld;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.debug.Debug;
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.World; import org.bukkit.World;
import org.bukkit.generator.BlockPopulator; import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
public class BukkitChunkGeneratorWrapper extends ChunkGenerator implements Handle { public class BukkitChunkGeneratorWrapper extends ChunkGenerator implements Handle {
private static final Map<com.dfsek.terra.api.generic.world.World, PopulationManager> popMap = new HashMap<>();
private final PopulationManager popMan;
private final TerraChunkGenerator delegate; private final TerraChunkGenerator delegate;
private final TerraPlugin main;
private boolean needsLoad = true;
public BukkitChunkGeneratorWrapper(TerraChunkGenerator delegate) { public BukkitChunkGeneratorWrapper(TerraChunkGenerator delegate) {
this.delegate = delegate; this.delegate = delegate;
this.main = delegate.getMain();
popMan = new PopulationManager(main);
popMan.attach(new OrePopulator(main));
popMan.attach(new TreePopulator(main));
popMan.attach(new FloraPopulator(main));
}
public static synchronized void saveAll() {
for(Map.Entry<com.dfsek.terra.api.generic.world.World, PopulationManager> e : popMap.entrySet()) {
try {
e.getValue().saveBlocks(e.getKey());
Debug.info("Saved data for world " + e.getKey().getName());
} catch(IOException ioException) {
ioException.printStackTrace();
}
}
}
public static synchronized void fixChunk(Chunk c) {
if(!(c.getWorld().getGenerator() instanceof com.dfsek.terra.generation.TerraChunkGenerator)) throw new IllegalArgumentException();
popMap.get(c.getWorld()).checkNeighbors(c.getX(), c.getZ(), c.getWorld());
}
private void load(com.dfsek.terra.api.generic.world.World w) {
try {
popMan.loadBlocks(w);
} catch(FileNotFoundException e) {
LangUtil.log("warning.no-population", Level.WARNING);
} catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}
popMap.put(w, popMan);
needsLoad = false;
} }
@Override @Override
public @NotNull ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome) { public @NotNull ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome) {
BukkitWorld bukkitWorld = new BukkitWorld(world); BukkitWorld bukkitWorld = new BukkitWorld(world);
if(needsLoad) load(bukkitWorld); // Load population data for world.
delegate.generateBiomes(bukkitWorld, random, x, z, new BukkitBiomeGrid(biome)); delegate.generateBiomes(bukkitWorld, random, x, z, new BukkitBiomeGrid(biome));
return (ChunkData) delegate.generateChunkData(bukkitWorld, random, x, z, new BukkitChunkGenerator.BukkitChunkData(createChunkData(world))).getHandle(); return (ChunkData) delegate.generateChunkData(bukkitWorld, random, x, z, new BukkitChunkGenerator.BukkitChunkData(createChunkData(world))).getHandle();
} }
@Override @Override
public @NotNull List<BlockPopulator> getDefaultPopulators(@NotNull World world) { public @NotNull List<BlockPopulator> getDefaultPopulators(@NotNull World world) {
return delegate.getPopulators().stream().map(BukkitPopulatorWrapper::new).collect(Collectors.toList()); return Stream.of(new CavePopulator(main), new StructurePopulator(main), popMan).map(BukkitPopulatorWrapper::new).collect(Collectors.toList());
} }
@Override @Override