Performance improvements

This commit is contained in:
dfsek
2020-09-30 21:18:30 -07:00
parent ca5accafa2
commit 0f29a506d0
20 changed files with 281 additions and 109 deletions

View File

@@ -0,0 +1,57 @@
package com.dfsek.terra.procgen;
import org.bukkit.util.Vector;
import org.polydev.gaea.math.MathUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GridSpawn {
private final int separation;
private final int width;
public GridSpawn(int width, int separation) {
this.separation = separation;
this.width = width;
}
/**
* Get nearest spawnpoint
* @param x X coordinate
* @param z Z coordinate
* @param seed Seed for RNG
* @return Vector representing nearest spawnpoint
*/
public Vector getNearestSpawn(int x, int z, long seed) {
int structureChunkX = x / (width + 2*separation);
int structureChunkZ = z / (width + 2*separation);
List<Vector> zones = new ArrayList<>();
for(int xi = structureChunkX-1; xi <= structureChunkX+1; xi++) {
for(int zi = structureChunkZ-1; zi <= structureChunkZ+1; zi++) {
zones.add(getChunkSpawn(xi, zi, seed));
}
}
Vector shortest = zones.get(0);
Vector compare = new Vector(x, 0, z);
for(Vector v : zones) {
if(compare.distanceSquared(shortest) > compare.distanceSquared(v)) shortest = v.clone();
}
return shortest;
}
/**
* Get the X/Z coordinates of the spawnpoint in the nearest Chunk (not Minecraft chunk)
* @param structureChunkX Chunk X coordinate
* @param structureChunkZ Chunk Z coordinate
* @param seed Seed for RNG
* @return Vector representing spawnpoint
*/
private Vector getChunkSpawn(int structureChunkX, int structureChunkZ, long seed) {
Random r = new Random(MathUtil.getCarverChunkSeed(structureChunkX, structureChunkZ, seed));
int offsetX = r.nextInt(width);
int offsetZ = r.nextInt(width);
int sx = structureChunkX * (width + 2*separation) + offsetX;
int sz = structureChunkZ * (width + 2*separation) + offsetZ;
return new Vector(sx, 0, sz);
}
}

View File

@@ -0,0 +1,20 @@
package com.dfsek.terra.procgen.voxel;
import org.bukkit.block.Block;
import org.bukkit.util.Vector;
import org.polydev.gaea.math.FastNoise;
public class DeformedSphere extends VoxelGeometry {
public DeformedSphere(Vector start, int rad, double deform, FastNoise noise) {
for(int x = -rad; x <= rad; x++) {
for(int y = -rad; y <= rad; y++) {
for(int z = -rad; z <= rad; z++) {
Vector c = new Vector(x, y, z);
if(c.length() < (rad + 0.5) * ((noise.getNoise(x, y, z)+1)*deform)) {
addVector(c.add(start));
}
}
}
}
}
}

View File

@@ -0,0 +1,19 @@
package com.dfsek.terra.procgen.voxel;
import org.bukkit.util.Vector;
import org.polydev.gaea.math.FastNoise;
public class Sphere extends VoxelGeometry {
public Sphere(Vector start, int rad) {
for(int x = -rad; x <= rad; x++) {
for(int y = -rad; y <= rad; y++) {
for(int z = -rad; z <= rad; z++) {
Vector c = new Vector(x, y, z);
if(c.length() < rad + 0.5) {
addVector(c.add(start));
}
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
package com.dfsek.terra.procgen.voxel;
import org.bukkit.util.Vector;
public class Tube extends VoxelGeometry {
public Tube(Vector start, Vector end, int radius) {
Vector step = start.clone().subtract(end).normalize();
Vector run = start.clone();
int steps = (int) start.distance(end);
for(int i = 0; i < steps; i++) {
merge(new Sphere(run, radius));
run.add(step);
}
}
}

View File

@@ -0,0 +1,26 @@
package com.dfsek.terra.procgen.voxel;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.List;
public abstract class VoxelGeometry {
public List<Vector> geometry = new ArrayList<>();
public List<Vector> getGeometry() {
return geometry;
}
protected void addVector(Vector v) {
geometry.add(v);
}
public void merge(VoxelGeometry other) {
geometry.addAll(other.getGeometry());
}
public static VoxelGeometry getBlank() {
return new Blank();
}
private static class Blank extends VoxelGeometry {}
}