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

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;
}
}