mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-23 08:30:39 +00:00
Implement arbitrarily sized UserDefinedGrids
This commit is contained in:
1 parent
f53e9d5112
commit
d0e7f535bb
6 files changed
+162
-55
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,34 +1,64 @@
|
||||
import com.dfsek.terra.biome.BiomeZone;
|
||||
import org.polydev.gaea.math.FastNoise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class DistributionTest {
|
||||
private static final double[] normalMap = new double[] {-0.7074090838432312D, -0.6983937621116638D, -0.6916067600250244D, -0.6815563440322876D, -0.6738069653511047D, -0.6669901609420776D, -0.6638160943984985D, -0.6613113880157471D, -0.6590566039085388D, -0.6569026112556458D, -0.6553084850311279D, -0.6537241339683533D, -0.6522396802902222D, -0.6510595679283142D, -0.6501344442367554D, -0.6492011547088623D, -0.6481583118438721D, -0.6471081376075745D, -0.6462589502334595D, -0.6451671719551086D, -0.6443428993225098D, -0.6434608101844788D, -0.6426240801811218D, -0.6418614387512207D, -0.6410216093063354D, -0.6403073668479919D, -0.6395679712295532D, -0.6387998461723328D, -0.638111412525177D, -0.6372985243797302D, -0.6365064978599548D, -0.6358203887939453D, -0.6349064707756042D, -0.6342090368270874D, -0.6337069272994995D, -0.633001446723938D, -0.6322312951087952D, -0.631661057472229D, -0.6309065818786621D, -0.6301361322402954D, -0.6294490694999695D, -0.6287845969200134D, -0.6280625462532043D, -0.627465009689331D, -0.6267663240432739D, -0.626183271408081D, -0.6255864500999451D, -0.6248782277107239D, -0.624212384223938D, -0.6238000392913818D, -0.6231371164321899D, -0.6224943995475769D, -0.6218792200088501D, -0.6213060021400452D, -0.6206783652305603D, -0.6201399564743042D, -0.6195181608200073D, -0.6189191937446594D, -0.6180287003517151D, -0.6174584627151489D, -0.6170166730880737D, -0.616350531578064D, -0.6157816648483276D, -0.6152390241622925D, -0.6145324110984802D, -0.6139620542526245D, -0.61328125D, -0.6127347946166992D, -0.6120567917823792D, -0.6113621592521667D, -0.6104971766471863D, -0.6099048256874084D, -0.608817994594574D, -0.6080660223960876D, -0.6072586178779602D, -0.606625497341156D, -0.6057839393615723D, -0.6050985455513D, -0.6045429706573486D, -0.6039659380912781D, -0.6032459735870361D, -0.6026276350021362D, -0.6018943786621094D, -0.6011484861373901D, -0.6003972887992859D, -0.5997307300567627D, -0.5989946722984314D, -0.5982387065887451D, -0.597582995891571D, -0.5968650579452515D, -0.5961800217628479D, -0.5956499576568604D, -0.594971776008606D, -0.5942410230636597D, -0.5937290191650391D, -0.593068540096283D, -0.592410147190094D, -0.5917299389839172D, -0.5910217761993408D, -0.5904621481895447D, -0.5899747610092163D, -0.589218020439148D, -0.5883771777153015D, -0.5875868201255798D, -0.586907148361206D, -0.5861385464668274D, -0.5854381918907166D, -0.5847938656806946D, -0.5841156244277954D, -0.5834392309188843D, -0.582630455493927D, -0.5819268226623535D, -0.5814274549484253D, -0.580765426158905D, -0.5801209807395935D, -0.5796810388565063D, -0.5792078375816345D, -0.5786181092262268D, -0.5780342221260071D, -0.5774702429771423D, -0.5770033001899719D, -0.5764638185501099D, -0.5759979486465454D, -0.5754085183143616D, -0.5747255682945251D, -0.5740239024162292D, -0.57340008020401D, -0.572874128818512D, -0.5724111795425415D, -0.5719996690750122D, -0.5714352130889893D, -0.5707415342330933D, -0.5702087879180908D, -0.5696454644203186D, -0.5691488981246948D, -0.5685044527053833D, -0.567990243434906D, -0.5674160122871399D, -0.5668932795524597D, -0.5664599537849426D, -0.5656421780586243D, -0.5650233030319214D, -0.5643867254257202D, -0.5638323426246643D, -0.5633259415626526D, -0.5626614093780518D, -0.5620179772377014D, -0.56131911277771D, -0.5606859922409058D, -0.5602015852928162D, -0.5595278739929199D, -0.5589155554771423D, -0.5583369135856628D, -0.5578821897506714D, -0.5571556091308594D, -0.5566734075546265D, -0.5561076998710632D, -0.5553616881370544D, -0.5547254681587219D, -0.5540726780891418D, -0.5533741116523743D, -0.5527831315994263D, -0.552133321762085D, -0.5515923500061035D, -0.5509361028671265D, -0.5503506660461426D, -0.5498544573783875D, -0.5491971373558044D, -0.5486406087875366D, -0.5478317737579346D, -0.5473202466964722D, -0.546663224697113D, -0.5460997223854065D, -0.5455703139305115D, -0.5450489521026611D, -0.5445272922515869D, -0.5440360903739929D, -0.5435668230056763D, -0.5429897308349609D, -0.5424480438232422D, -0.5420375466346741D, -0.5414220690727234D, -0.5409082770347595D, -0.5403460264205933D, -0.5397695302963257D, -0.5392237305641174D, -0.5386609435081482D, -0.5380395650863647D, -0.5375149846076965D, -0.5369104146957397D, -0.5363602042198181D, -0.5358449816703796D, -0.5351901054382324D, -0.5346441864967346D, -0.5339838862419128D, -0.5334790349006653D, -0.5328581929206848D, -0.53231281042099D, -0.531755268573761D, -0.5311923623085022D, -0.5305578112602234D, -0.5300067067146301D, -0.5294909477233887D, -0.5288727283477783D, -0.5283600687980652D, -0.5278205275535583D, -0.527280867099762D, -0.5266819596290588D, -0.5259708762168884D, -0.5254105925559998D, -0.5246822237968445D, -0.5241648554801941D, -0.5237182378768921D, -0.5232840180397034D, -0.5226284861564636D, -0.5220814347267151D, -0.5215300917625427D, -0.5210031270980835D, -0.5204776525497437D, -0.5198733806610107D, -0.5193755626678467D, -0.5187353491783142D, -0.5182331800460815D, -0.5176404118537903D, -0.5171401500701904D, -0.516610324382782D, -0.51608Line truncated
|
||||
//private static final List<Double> m = Arrays.asList(normalMap);
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
FastNoise noise = new FastNoise(new Random().nextInt());
|
||||
noise.setFrequency(0.01f);
|
||||
noise.setNoiseType(FastNoise.NoiseType.SimplexFractal);
|
||||
noise.setFractalOctaves(3);
|
||||
int[] numbers = new int[32];
|
||||
|
||||
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);
|
||||
System.out.println(noise);
|
||||
int attempts = 16;
|
||||
int[] numbers = new int[attempts];
|
||||
double min = Integer.MAX_VALUE;
|
||||
double max = Integer.MIN_VALUE;
|
||||
for(int i = 0; i < 32; i++) {
|
||||
|
||||
|
||||
|
||||
for(int i = 0; i < attempts; i++) {
|
||||
numbers[i] = 0;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 10000000; i++) {
|
||||
double n = noise.getSimplexFractal(0, i);
|
||||
max = Math.max(max, n);
|
||||
min = Math.min(min, n);
|
||||
numbers[BiomeZone.normalize(n)]++;
|
||||
long l = System.nanoTime();
|
||||
for(int i = 0; i < 1000000; i++) {
|
||||
double n = noise.getNoise(0, i);
|
||||
max = Math.max(max, n-1);
|
||||
min = Math.min(min, n-1);
|
||||
numbers[normalize(n, attempts)]++;
|
||||
}
|
||||
long l2 = System.nanoTime()-l;
|
||||
System.out.println("Took " + (double)l2/1000000 + "ms (" + ((double)l2/1000000) + "ns per.");
|
||||
|
||||
for(int i = 0; i < 32; i++) {
|
||||
l = System.nanoTime();
|
||||
for(int i = 0; i < 1000000; i++) {
|
||||
double n = noise.getNoise(0, i);
|
||||
max = Math.max(max, n - 1);
|
||||
min = Math.min(min, n - 1);
|
||||
}
|
||||
l2 = System.nanoTime()-l;
|
||||
System.out.println("Took " + (double)l2/1000000 + "ms (" + ((double)l2/1000000) + "ns per.");
|
||||
|
||||
for(int i = 0; i < attempts; i++) {
|
||||
System.out.println(i + ": " + numbers[i]);
|
||||
}
|
||||
for(int i = 0; i < 32; i++) {
|
||||
for(int i = 0; i < attempts; i++) {
|
||||
System.out.print(i + " |");
|
||||
for(int j = 0; j < numbers[i]/2500; j++) {
|
||||
for(int j = 0; j < numbers[i]/250; j++) {
|
||||
System.out.print("-");
|
||||
}
|
||||
System.out.println("|");
|
||||
@@ -36,4 +66,22 @@ public class DistributionTest {
|
||||
System.out.println("max: " + max);
|
||||
System.out.println("min: " + min);
|
||||
}
|
||||
public static int normalize(double d, int num) {
|
||||
int start = 0;
|
||||
int end = normalMap.length - 1;
|
||||
while (start + 1 < end) {
|
||||
int mid = start + (end - start) / 2;
|
||||
if (normalMap[mid] <= d) {
|
||||
start = mid;
|
||||
} else {
|
||||
end = mid;
|
||||
}
|
||||
}
|
||||
double left = Math.abs(normalMap[start] - d);
|
||||
double right = Math.abs(normalMap[end] - d);
|
||||
if (left <= right) {
|
||||
return start * (num) / (normalMap.length);
|
||||
}
|
||||
return end * (num) / (normalMap.length);
|
||||
}
|
||||
}
|
||||
@@ -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 + "%)";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user