Rarity System

This commit is contained in:
Daniel Mills 2020-07-22 15:04:28 -04:00
parent 6c28e270d2
commit a9ce316d28
4 changed files with 68 additions and 7 deletions

View File

@ -11,15 +11,16 @@ import ninja.bytecode.iris.util.BiomeResult;
import ninja.bytecode.iris.util.CellGenerator;
import ninja.bytecode.iris.util.GenLayer;
import ninja.bytecode.iris.util.RNG;
import ninja.bytecode.iris.util.RarityCellGenerator;
import ninja.bytecode.shuriken.collections.KList;
public class GenLayerBiome extends GenLayer
{
private CellGenerator region;
private CellGenerator bridge;
private CellGenerator land;
private CellGenerator shore;
private CellGenerator sea;
private RarityCellGenerator<IrisBiome> land;
private RarityCellGenerator<IrisBiome> shore;
private RarityCellGenerator<IrisBiome> sea;
private DimensionChunkGenerator iris;
public GenLayerBiome(DimensionChunkGenerator iris, RNG rng)
@ -28,9 +29,9 @@ public class GenLayerBiome extends GenLayer
this.iris = iris;
region = new CellGenerator(rng.nextParallelRNG(1188519));
bridge = new CellGenerator(rng.nextParallelRNG(1541462));
land = new CellGenerator(rng.nextParallelRNG(9045162));
shore = new CellGenerator(rng.nextParallelRNG(2342812));
sea = new CellGenerator(rng.nextParallelRNG(6135621));
land = new RarityCellGenerator<>(rng.nextParallelRNG(9045162));
shore = new RarityCellGenerator<>(rng.nextParallelRNG(2342812));
sea = new RarityCellGenerator<>(rng.nextParallelRNG(6135621));
}
public IrisRegion getRegion(double bx, double bz)

View File

@ -10,6 +10,7 @@ import lombok.EqualsAndHashCode;
import ninja.bytecode.iris.util.CNG;
import ninja.bytecode.iris.util.CellGenerator;
import ninja.bytecode.iris.util.Desc;
import ninja.bytecode.iris.util.IRare;
import ninja.bytecode.iris.util.RNG;
import ninja.bytecode.shuriken.collections.KList;
import ninja.bytecode.shuriken.logging.L;
@ -17,11 +18,14 @@ import ninja.bytecode.shuriken.logging.L;
@Desc("Represents a biome in iris.")
@Data
@EqualsAndHashCode(callSuper = false)
public class IrisBiome extends IrisRegistrant
public class IrisBiome extends IrisRegistrant implements IRare
{
@Desc("This is the human readable name for this biome. This can and should be different than the file name. This is not used for loading biomes in other objects.")
private String name = "A Biome";
@Desc("The weight of this biome. Higher than 1 is more common, less than 1 (above 0) is rarer")
private double weight = 1D;
@Desc("This changes the dispersion of the biome colors if multiple derivatives are chosen")
private Dispersion biomeDispersion = Dispersion.SCATTER;

View File

@ -0,0 +1,6 @@
package ninja.bytecode.iris.util;
public interface IRare
{
public double getWeight();
}

View File

@ -0,0 +1,50 @@
package ninja.bytecode.iris.util;
import org.apache.commons.lang.math.DoubleRange;
import ninja.bytecode.shuriken.collections.KList;
import ninja.bytecode.shuriken.collections.KMap;
public class RarityCellGenerator<T extends IRare> extends CellGenerator
{
public RarityCellGenerator(RNG rng)
{
super(rng);
}
public T get(double x, double z, KList<T> t)
{
int totalWeight = 0;
KMap<DoubleRange, T> ranges = new KMap<>();
for(T i : t)
{
int weight = (int) Math.round(1000 * i.getWeight());
if(weight < 1)
{
continue;
}
ranges.put(new DoubleRange(totalWeight, totalWeight + weight), i);
totalWeight += weight;
}
int r = getIndex(x, z, totalWeight);
for(DoubleRange i : ranges.keySet())
{
if(i.containsDouble(r))
{
return ranges.get(i);
}
}
if(!t.isEmpty())
{
return t.get(0);
}
return null;
}
}