Implement arbitrarily sized UserDefinedGrids

This commit is contained in:
dfsek 2020-09-20 03:31:59 -07:00
parent f53e9d5112
commit d0e7f535bb
6 changed files with 162 additions and 55 deletions

10
pom.xml
View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<groupId>com.dfsek</groupId>
<artifactId>Terra</artifactId>
<version>1.0-SNAPSHOT</version>
@ -83,7 +83,13 @@
<dependency>
<groupId>org.polydev</groupId>
<artifactId>gaea</artifactId>
<version>1.7.5</version>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>javax.vecmath</groupId>
<artifactId>vecmath</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -3,6 +3,7 @@ package com.dfsek.terra.biome;
import com.dfsek.terra.config.WorldConfig;
import org.bukkit.World;
import org.polydev.gaea.biome.BiomeGrid;
import org.polydev.gaea.biome.NormalizationUtil;
import org.polydev.gaea.math.FastNoise;
import java.util.HashMap;
@ -13,15 +14,17 @@ public class BiomeZone {
private final World w;
private final FastNoise noise;
private static final Map<World, BiomeZone> zones = new HashMap<>();
private static final double[] normalMap = new double[] {-0.35662081837654114D, -0.30661869049072266D, -0.27095329761505127D, -0.24149227142333984D, -0.21537694334983826D, -0.19166918098926544D, -0.16956785321235657D, -0.14864568412303925D, -0.12845154106616974D, -0.10894706845283508D, -0.08996972441673279D, -0.0715663805603981D, -0.053535036742687225D, -0.03580872714519501D, -0.01817353256046772D, -7.577221258543432E-4D, 0.016616813838481903D, 0.03416096046566963D, 0.05187138542532921D, 0.06989025324583054D, 0.08827653527259827D, 0.10723070055246353D, 0.12675245106220245D, 0.14694781601428986D, 0.16793397068977356D, 0.18999846279621124D, 0.2138010412454605D, 0.24002985656261444D, 0.2696261405944824D, 0.30540621280670166D, 0.35551881790161133D, 0.653269350528717D};
private static final double[] normalMap = new double[] {-0.572874128818512D, -0.5007192492485046D, -0.4495924413204193D, -0.41612040996551514D, -0.3814384937286377D, -0.3477869927883148D, -0.31369876861572266D, -0.28042978048324585D, -0.24612723290920258D, -0.21002958714962006D, -0.17449893057346344D, -0.1394101232290268D, -0.10480091720819473D, -0.0714595764875412D, -0.03575916960835457D, -0.0017036114586517215D, 0.03202686831355095D, 0.06717526167631149D, 0.10201185941696167D, 0.13758908212184906D, 0.17380206286907196D, 0.20863550901412964D, 0.24430148303508759D, 0.2795235514640808D, 0.31312644481658936D, 0.3475150465965271D, 0.38061848282814026D, 0.415109783411026D, 0.44838231801986694D, 0.4965132176876068D, 0.5715073347091675D, 0.7126374840736389D};
private BiomeZone(World w, float freq) {
this.w = w;
this.noise = new FastNoise((int) w.getSeed()+2);
noise.setNoiseType(FastNoise.NoiseType.SimplexFractal);
noise.setFractalOctaves(5);
noise.setFrequency(freq);
FastNoise base = new FastNoise((int) (w.getSeed()+2));
base.setNoiseType(FastNoise.NoiseType.Simplex);
base.setFrequency(freq);
this.noise.setCellularDistanceFunction(FastNoise.CellularDistanceFunction.Natural);
this.noise.setCellularNoiseLookup(base);
this.noise.setFrequency(freq);
setZones(WorldConfig.fromWorld(w).definedGrids);
zones.put(w, this);
}
@ -32,24 +35,11 @@ public class BiomeZone {
}
protected BiomeGrid getGrid(int x, int z) {
return grids[normalize(noise.getSimplexFractal(x, z))];
return grids[NormalizationUtil.normalize(noise.getNoise(x, z), 32)];
}
protected static BiomeZone fromWorld(World w) {
if(zones.containsKey(w)) return zones.get(w);
else return new BiomeZone(w, WorldConfig.fromWorld(w).zoneFreq);
}
/**
* Takes a noise input and normalizes it to a value between 0 and 31 inclusive.
*
* @param d - The noise value to normalize.
* @return int - The normalized value.
*/
public static int normalize(double d) {
for(int i = 0; i < normalMap.length; i++) {
if(d < normalMap[i]) return i;
}
return normalMap.length-1;
}
}

View File

@ -6,11 +6,13 @@ import org.polydev.gaea.biome.BiomeGrid;
public class UserDefinedGrid extends BiomeGrid {
public UserDefinedGrid(World w, float freq1, float freq2, BiomeGridConfig config) {
super(w, freq1, freq2);
super(w, freq1, freq2, config.getBiomeGrid().length, config.getBiomeGrid()[0].length);
super.setNormalType(NormalType.CELLULAR);
super.setGrid(config.getBiomeGrid());
}
public UserDefinedGrid(World w, float freq1, float freq2, UserDefinedBiome[][] b) {
super(w, freq1, freq2);
super(w, freq1, freq2, b.length, b[0].length);
super.setNormalType(NormalType.CELLULAR);
super.setGrid(b);
}
}

View File

@ -25,7 +25,9 @@ public class BiomeGridConfig extends YamlConfiguration {
private String gridID;
private String friendlyName;
private boolean isEnabled = false;
private final UserDefinedBiome[][] gridRaw = new UserDefinedBiome[16][16];
private UserDefinedBiome[][] gridRaw;
private int sizeX;
private int sizeZ;
public BiomeGridConfig(File file) throws IOException, InvalidConfigurationException {
super();
@ -40,9 +42,12 @@ public class BiomeGridConfig extends YamlConfiguration {
if(!contains("name")) throw new InvalidConfigurationException("Grid Name unspecified!");
this.friendlyName = getString("name");
if(!contains("grid")) throw new InvalidConfigurationException("Grid not found!");
this.sizeX = ((List<List<String>>) getList("grid")).size();
this.sizeZ = ((List<List<String>>) getList("grid")).get(0).size();
gridRaw = new UserDefinedBiome[sizeX][sizeZ];
try {
for(int x = 0; x < 16; x++) {
for(int z = 0; z < 16; z++) {
for(int x = 0; x < sizeX; x++) {
for(int z = 0; z < sizeZ; z++) {
try {
gridRaw[x][z] = BiomeConfig.fromID(((List<List<String>>) getList("grid")).get(x).get(z)).getBiome();
} catch(NullPointerException e) {
@ -56,6 +61,14 @@ public class BiomeGridConfig extends YamlConfiguration {
isEnabled = true;
}
public int getSizeX() {
return sizeX;
}
public int getSizeZ() {
return sizeZ;
}
public String getFriendlyName() {
return friendlyName;
}
@ -87,7 +100,7 @@ public class BiomeGridConfig extends YamlConfiguration {
try {
BiomeGridConfig grid = new BiomeGridConfig(path.toFile());
biomeGrids.put(grid.getGridID(), grid);
main.getLogger().info("Loaded BiomeGrid with name " + grid.getFriendlyName() + ", ID " + grid.getGridID() + " from " + path.toString());
main.getLogger().info("Loaded BiomeGrid with name " + grid.getFriendlyName() + ", ID " + grid.getGridID() + " Size: " + grid.getSizeX() + ", " + grid.getSizeZ() + " from " + path.toString());
} catch(IOException e) {
e.printStackTrace();
} catch(InvalidConfigurationException | IllegalArgumentException e) {

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,5 @@
import com.dfsek.terra.biome.BiomeZone;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import org.polydev.gaea.math.FastNoise;
import java.util.ArrayList;
@ -9,14 +10,19 @@ import java.util.Random;
public class LookupGenerator {
private static double[] lookup;
public static void main(String[] args) {
int dist = 16;
public static void main(String[] args) throws InterruptedException {
int dist = 4096;
List<Double> vals = new ArrayList<>();
FastNoise noise = new FastNoise(new Random().nextInt());
noise.setFrequency(0.01f);
noise.setNoiseType(FastNoise.NoiseType.SimplexFractal);
noise.setFractalOctaves(5);
FastNoise noise = new FastNoise();
FastNoise noiseLookup = new FastNoise();
noiseLookup.setNoiseType(FastNoise.NoiseType.Simplex);
noiseLookup.setFrequency(0.02f);
noise.setFrequency(0.02f);
noise.setNoiseType(FastNoise.NoiseType.Cellular);
noise.setCellularDistanceFunction(FastNoise.CellularDistanceFunction.Natural);
noise.setCellularReturnType(FastNoise.CellularReturnType.NoiseLookup);
noise.setCellularNoiseLookup(noiseLookup);
int[] numbers = new int[dist];
double min = Integer.MAX_VALUE;
double max = Integer.MIN_VALUE;
@ -24,14 +30,29 @@ public class LookupGenerator {
numbers[i] = 0;
}
for(int i = 0; i < 10000000; i++) {
double n = noise.getNoise(0, i);
max = Math.max(max, n);
min = Math.min(min, n);
vals.add(n);
numbers[normalize(n, dist)]++;
int workerAmount = 16;
List<Worker> workers = new ArrayList<>();
for(int i = 0; i < workerAmount; i++) {
workers.add(new Worker(new ArrayList<>(), 10000000, noise));
}
for(Worker w : workers) {
w.start();
}
for(Worker w : workers) {
System.out.println("Waiting for Worker " + workers.indexOf(w));
w.join();
}
for(Worker w : workers) {
vals.addAll(w.getResult());
}
System.out.println("Generated " + vals.size() + " values.");
for(int i = 0; i < dist; i++) {
System.out.println(i + ": " + numbers[i]);
}
@ -58,7 +79,7 @@ public class LookupGenerator {
s.append("}");
numbers = new int[dist];
vals = new ArrayList<>();
for(int i = 0; i < 10000000; i++) {
for(int i = 0; i < 50000000; i++) {
double n = noise.getNoise(0, i);
vals.add(n);
numbers[normalizeNew(n)]++;
@ -69,7 +90,7 @@ public class LookupGenerator {
}
for(int i = 0; i < dist; i++) {
System.out.print(i + (String.valueOf(i).length() ==1 ? " " : "") + " |");
for(int j = 0; j < numbers[i]/3000; j++) {
for(int j = 0; j < numbers[i]/100; j++) {
System.out.print("-");
}
System.out.println("|");
@ -89,4 +110,31 @@ public class LookupGenerator {
}
return lookup.length-1;
}
private static class Worker extends Thread {
private final List<Double> l;
private final int searches;
private final FastNoise noise;
public Worker(List<Double> l, int searches, FastNoise noise) {
this.l = l;
this.searches = searches;
this.noise = noise;
}
@Override
public void run() {
for(int i = 0; i < searches; i++) {
double n = noise.getNoise(0, i);
l.add(n);
}
}
public List<Double> getResult() {
return l;
}
public String getStatus() {
return "Generating values. " + l.size() + "/" + searches + " (" + ((long)l.size()*100L)/searches + "%)";
}
}
}