Implement structure location

This commit is contained in:
dfsek 2020-10-08 21:14:35 -07:00
parent 1a7d49ab1e
commit 6ec7ab9c72
3 changed files with 30 additions and 10 deletions

View File

@ -1,6 +1,8 @@
package com.dfsek.terra.async; package com.dfsek.terra.async;
import com.dfsek.terra.Terra; import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.TerraBiomeGrid; import com.dfsek.terra.biome.TerraBiomeGrid;
import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.genconfig.StructureConfig; import com.dfsek.terra.config.genconfig.StructureConfig;
@ -12,6 +14,7 @@ import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.polydev.gaea.generation.GenerationPhase; import org.polydev.gaea.generation.GenerationPhase;
import org.polydev.gaea.profiler.ProfileFuture;
import java.util.Random; import java.util.Random;
@ -83,14 +86,17 @@ public class AsyncStructureFinder implements Runnable {
} else if(p.isOnline()) p.sendMessage("Unable to locate structure. " + spawn); } else if(p.isOnline()) p.sendMessage("Unable to locate structure. " + spawn);
} }
private boolean isValidSpawn(int x, int z) { private boolean isValidSpawn(int x, int z) {
Location spawn = new Location(world, x, 255, z); // Probably(tm) async safe Location spawn = target.getSpawn().getNearestSpawn(x, z, world.getSeed()).toLocation(world);
UserDefinedBiome b = (UserDefinedBiome) grid.getBiome(spawn, GenerationPhase.POPULATE); if(! TerraWorld.getWorld(world).getConfig().getBiome((UserDefinedBiome) grid.getBiome(spawn)).getStructures().contains(target)) return false;
Random r2 = new Random(spawn.hashCode()); Random r2 = new Random(spawn.hashCode());
GaeaStructure struc = target.getStructure(r2); GaeaStructure struc = target.getStructure(r2);
main: for(int y = target.getSearchStart().get(r2); y > 0; y--) { GaeaStructure.Rotation rotation = GaeaStructure.Rotation.fromDegrees(r2.nextInt(4) * 90);
for(int y = target.getSearchStart().get(r2); y > 0; y--) {
if(!target.getBound().isInRange(y)) return false;
spawn.setY(y); spawn.setY(y);
if(y > target.getBound().getMax() || y < target.getBound().getMin()) return false; if(! struc.checkSpawns(spawn, rotation)) continue;
return true;
} }
return true; return false;
} }
} }

View File

@ -24,8 +24,10 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -43,6 +45,9 @@ public class ConfigPack extends YamlConfiguration {
private final Map<String, BiomeConfig> biomes; private final Map<String, BiomeConfig> biomes;
private final Map<String, BiomeGridConfig> grids; private final Map<String, BiomeGridConfig> grids;
private final Map<String, TreeConfig> trees; private final Map<String, TreeConfig> trees;
private final Set<StructureConfig> allStructures = new HashSet<>();
private final File dataFolder; private final File dataFolder;
private final String id; private final String id;
@ -106,6 +111,11 @@ public class ConfigPack extends YamlConfiguration {
biomeList = getStringList("grids"); biomeList = getStringList("grids");
configs.put(id, this); configs.put(id, this);
for(BiomeConfig b : getBiomes().values()) {
allStructures.addAll(b.getStructures());
}
LangUtil.log("config-pack.loaded", Level.INFO, getID(), String.valueOf((System.nanoTime() - l)/1000000D)); LangUtil.log("config-pack.loaded", Level.INFO, getID(), String.valueOf((System.nanoTime() - l)/1000000D));
} }
@ -121,6 +131,10 @@ public class ConfigPack extends YamlConfiguration {
return carvers; return carvers;
} }
public Set<StructureConfig> getAllStructures() {
return allStructures;
}
public static synchronized void loadAll(JavaPlugin main) { public static synchronized void loadAll(JavaPlugin main) {
configs.clear(); configs.clear();
List<Path> subfolder; List<Path> subfolder;

View File

@ -30,16 +30,16 @@ public class StructurePopulator extends BlockPopulator {
if(!tw.isSafe()) return; if(!tw.isSafe()) return;
TerraBiomeGrid grid = tw.getGrid(); TerraBiomeGrid grid = tw.getGrid();
ConfigPack config = tw.getConfig(); ConfigPack config = tw.getConfig();
UserDefinedBiome b = (UserDefinedBiome) grid.getBiome(cx+ 8, cz + 8, GenerationPhase.POPULATE); structure: for(StructureConfig conf : config.getAllStructures()) {
structure: for(StructureConfig conf : config.getBiome(b).getStructures()) {
Location spawn = conf.getSpawn().getNearestSpawn(cx + 8, cz + 8, world.getSeed()).toLocation(world); Location spawn = conf.getSpawn().getNearestSpawn(cx + 8, cz + 8, world.getSeed()).toLocation(world);
if(!config.getBiome((UserDefinedBiome) grid.getBiome(spawn)).getStructures().contains(conf)) continue;
Random r2 = new Random(spawn.hashCode()); Random r2 = new Random(spawn.hashCode());
GaeaStructure struc = conf.getStructure(r2); GaeaStructure struc = conf.getStructure(r2);
GaeaStructure.Rotation rotation = GaeaStructure.Rotation.fromDegrees(r2.nextInt(4) * 90); GaeaStructure.Rotation rotation = GaeaStructure.Rotation.fromDegrees(r2.nextInt(4) * 90);
main: for(int y = conf.getSearchStart().get(r2); y > 0; y--) { for(int y = conf.getSearchStart().get(r2); y > 0; y--) {
if(y > conf.getBound().getMax() || y < conf.getBound().getMin()) continue structure; if(!conf.getBound().isInRange(y)) continue structure;
spawn.setY(y); spawn.setY(y);
if(!struc.checkSpawns(spawn, rotation)) continue; if(! struc.checkSpawns(spawn, rotation)) continue;
double horizontal = struc.getStructureInfo().getMaxHorizontal(); double horizontal = struc.getStructureInfo().getMaxHorizontal();
if(Math.abs((cx + 8) - spawn.getBlockX()) <= horizontal && Math.abs((cz + 8) - spawn.getBlockZ()) <= horizontal) { if(Math.abs((cx + 8) - spawn.getBlockX()) <= horizontal && Math.abs((cz + 8) - spawn.getBlockZ()) <= horizontal) {
try(ProfileFuture ignore = TerraProfiler.fromWorld(world).measure("StructurePasteTime")) { try(ProfileFuture ignore = TerraProfiler.fromWorld(world).measure("StructurePasteTime")) {