9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2026-01-02 22:02:23 +00:00

Fixes & merge

This commit is contained in:
Daniel Mills
2020-10-23 13:05:16 -04:00
parent 99022055cf
commit 5a6f6fba2e
6 changed files with 86 additions and 161 deletions

View File

@@ -0,0 +1,64 @@
package com.volmit.iris.gen.atomics;
public class HeightHunk extends Hunk<Byte>
{
public HeightHunk(int w, int d)
{
super(w, 1, d);
}
public void setHeight(int x, int y, int z)
{
set(x, 0, z, (byte) (y + Byte.MIN_VALUE));
}
public int getHeight(int x, int z)
{
return get(x, 0, z) - Byte.MIN_VALUE;
}
@SafeVarargs
public static HeightHunk combined(Byte defaultNode, HeightHunk... hunks)
{
int w = 0;
int d = 0;
for(HeightHunk i : hunks)
{
w = Math.max(w, i.getW());
d = Math.max(d, i.getD());
}
HeightHunk b = new HeightHunk(w, d);
b.fill((byte) (defaultNode + Byte.MIN_VALUE));
for(HeightHunk i : hunks)
{
b.insert(i);
}
return b;
}
@SafeVarargs
public static HeightHunk combined(HeightHunk... hunks)
{
int w = 0;
int d = 0;
for(HeightHunk i : hunks)
{
w = Math.max(w, i.getW());
d = Math.max(d, i.getD());
}
HeightHunk b = new HeightHunk(w, d);
for(HeightHunk i : hunks)
{
b.insert(i);
}
return b;
}
}

View File

@@ -0,0 +1,317 @@
package com.volmit.iris.gen.atomics;
import org.bouncycastle.util.Arrays;
import com.volmit.iris.Iris;
import com.volmit.iris.util.Function3;
import com.volmit.iris.util.Supplier2;
import com.volmit.iris.util.Supplier3;
import lombok.Data;
@Data
public class Hunk<T>
{
protected final int w;
protected final int h;
protected final int d;
protected final T[] data;
@SuppressWarnings("unchecked")
public Hunk(int w, int h, int d)
{
if(w * h * d < 0)
{
Iris.error(w + " " + h + " " + d + " is not valid!");
}
this.w = w;
this.h = h;
this.d = d;
data = (T[]) new Object[w * h * d];
}
/**
* Create a new hunk from a section of this hunk.
*
*
* @param x1
* The min x (inclusive)
* @param y1
* The min y (inclusive)
* @param z1
* The min z (inclusive)
* @param x2
* The max x (exclusive)
* @param y2
* The max y (exclusive)
* @param z2
* The max z (exclusive)
* @return the new hunk (x2-x1, y2-y1, z2-z1)
*/
public Hunk<T> crop(int x1, int y1, int z1, int x2, int y2, int z2)
{
Hunk<T> h = new Hunk<T>(x2 - x1, y2 - y1, z2 - z1);
for(int i = x1; i < x2; i++)
{
for(int j = y1; j < y2; j++)
{
for(int k = z1; k < z2; k++)
{
h.set(i - x1, j - y1, k - z1, get(i, j, k));
}
}
}
return h;
}
/**
* Insert a hunk into this one with an offset the inserted hunk
*
* @param offX
* the offset from zero for x
* @param offY
* the offset from zero for y
* @param offZ
* the offset from zero for z
* @param hunk
* the hunk to insert
*/
public void insert(int offX, int offY, int offZ, Hunk<T> hunk)
{
insert(offX, offY, offZ, hunk, false);
}
/**
* Insert a hunk into this one
*
* @param hunk
* the hunk to insert
*/
public void insert(Hunk<T> hunk)
{
insert(0, 0, 0, hunk, false);
}
/**
* Insert a hunk into this one
*
* @param hunk
* the hunk to insert
* @param inverted
* invert the inserted hunk or not
*/
public void insert(Hunk<T> hunk, boolean inverted)
{
insert(0, 0, 0, hunk, inverted);
}
/**
* Insert a hunk into this one with an offset and possibly inverting the y of
* the inserted hunk
*
* @param offX
* the offset from zero for x
* @param offY
* the offset from zero for y
* @param offZ
* the offset from zero for z
* @param hunk
* the hunk to insert
* @param invertY
* should the inserted hunk be inverted
*/
public void insert(int offX, int offY, int offZ, Hunk<T> hunk, boolean invertY)
{
if(offX + (hunk.getW() - 1) >= w || offY + (hunk.getH() - 1) >= h || offZ + (hunk.getD() - 1) >= d || offX < 0 || offY < 0 || offZ < 0)
{
throw new RuntimeException("Cannot insert hunk " + hunk.getW() + "," + hunk.getH() + "," + hunk.getD() + " into Hunk " + w + "," + h + "," + d + " with offset " + offZ + "," + offY + "," + offZ);
}
for(int i = offX; i < offX + hunk.getW(); i++)
{
for(int j = offY; j < offY + hunk.getH(); j++)
{
for(int k = offZ; k < offZ + hunk.getD(); k++)
{
set(i, j, k, hunk.get(i - offX, j - offY, k - offZ));
}
}
}
}
public void set(int x, int z, int y1, int y2, T t)
{
for(int i = y1; i <= y2; i++)
{
set(x, i, z, t);
}
}
public void set(int x1, int y1, int z1, int x2, int y2, int z2, T t)
{
for(int i = x1; i <= x2; i++)
{
for(int j = y1; j <= y2; j++)
{
for(int k = z1; k <= z2; k++)
{
set(i, j, k, t);
}
}
}
}
public void set(int x, int y, int z, T t)
{
data[index(x, y, z)] = t;
}
public T get(int x, int y, int z)
{
return data[index(x, y, z)];
}
public T get(int x, int y, int z, T oob)
{
if(x >= w || y >= h || z >= d)
{
return oob;
}
return data[index(x, y, z)];
}
public T getClosest(int x, int y, int z)
{
return data[index(x >= w ? w - 1 : x, y >= h ? h - 1 : y, z >= d ? d - 1 : z)];
}
public void setInvertedY(int x, int y, int z, T t)
{
data[index(x, h - y, z)] = t;
}
public T getInvertedY(int x, int y, int z)
{
return data[index(x, h - y, z)];
}
protected int index(int x, int y, int z)
{
if(x >= w || y >= h || z >= d)
{
throw new RuntimeException(x + " " + y + " " + z + " is out of the bounds 0,0,0 - " + (w - 1) + "," + (h - 1) + "," + (d - 1));
}
return (z * w * h) + (y * w) + x;
}
public void fill(int ox, int oy, int oz, Function3<Integer, Integer, Integer, T> f)
{
for(int i = ox; i < ox + getW(); i++)
{
for(int j = oy; j < oy + getH(); j++)
{
for(int k = oz; k < oz + getD(); k++)
{
set(i - ox, j - oy, k - oz, f.apply(i, j, k));
}
}
}
}
public void forEach(Supplier3<Integer, Integer, Integer> t)
{
for(int i = 0; i < getW(); i++)
{
for(int j = 0; j < getH(); j++)
{
for(int k = 0; k < getD(); k++)
{
t.get(i, j, k);
}
}
}
}
public void forEachXZ(Supplier2<Integer, Integer> t)
{
for(int i = 0; i < getW(); i++)
{
for(int k = 0; k < getD(); k++)
{
t.get(i, k);
}
}
}
public void fill(T t)
{
Arrays.fill(data, t);
}
@SafeVarargs
public static <T> Hunk<T> combined(T defaultNode, Hunk<T>... hunks)
{
int w = 0;
int h = 0;
int d = 0;
for(Hunk<T> i : hunks)
{
w = Math.max(w, i.getW());
h = Math.max(h, i.getH());
d = Math.max(d, i.getD());
}
Hunk<T> b = new Hunk<T>(w, h, d);
b.fill(defaultNode);
for(Hunk<T> i : hunks)
{
b.insert(i);
}
return b;
}
@SafeVarargs
public static <T> Hunk<T> combined(Hunk<T>... hunks)
{
int w = 0;
int h = 0;
int d = 0;
for(Hunk<T> i : hunks)
{
w = Math.max(w, i.getW());
h = Math.max(h, i.getH());
d = Math.max(d, i.getD());
}
Hunk<T> b = new Hunk<T>(w, h, d);
for(Hunk<T> i : hunks)
{
b.insert(i);
}
return b;
}
public int highestNonNull(int x, int z)
{
for(int i = h - 1; i >= 0; i--)
{
if(get(x, i, z) != null)
{
return i;
}
}
return 0;
}
}

View File

@@ -0,0 +1,283 @@
package com.volmit.iris.gen.atomics;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
import org.bukkit.generator.ChunkGenerator.ChunkData;
import org.bukkit.material.MaterialData;
import com.volmit.iris.object.IrisBiome;
import com.volmit.iris.util.KList;
import lombok.Getter;
@SuppressWarnings("deprecation")
public class TerrainHunk extends Hunk<TerrainNode> implements BiomeGrid, ChunkData
{
@Getter
private HeightHunk height;
@Getter
private Hunk<IrisBiome> biome;
public TerrainHunk(int w, int h, int d)
{
super(w, h, d);
this.height = new HeightHunk(w, d);
this.biome = new Hunk<IrisBiome>(w, h, d);
}
public TerrainHunk(int w, int h, int d, Hunk<Double> noise)
{
super(w, h, d);
this.height = new HeightHunk(w, d);
this.biome = new Hunk<IrisBiome>(w, h, d);
for(int i = 0; i < w; i++)
{
for(int j = 0; j < d; j++)
{
height.setHeight(i, noise.highestNonNull(i, j), j);
}
}
}
@Override
public int getMaxHeight()
{
return getH();
}
private void set(int x, int y, int z, BlockData block)
{
TerrainNode n = get(x, y, z);
if(n == null)
{
n = new TerrainNode(Biome.THE_VOID, block);
}
else
{
n = n.setBlockData(block);
}
set(x, y, z, n);
}
private void set(int x, int y, int z, Biome biome)
{
TerrainNode n = get(x, y, z);
if(n == null)
{
n = new TerrainNode(biome, Material.AIR.createBlockData());
}
else
{
n = n.setBiome(biome);
}
set(x, y, z, n);
}
@Override
public void setBlock(int x, int y, int z, Material material)
{
set(x, y, z, material.createBlockData());
}
@Override
public void setBlock(int x, int y, int z, MaterialData material)
{
set(x, y, z, material.getItemType().createBlockData());
}
@Override
public void setBlock(int x, int y, int z, BlockData blockData)
{
set(x, y, z, blockData);
}
@Override
public void setRegion(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, Material material)
{
throw new RuntimeException("Not Supported");
}
@Override
public void setRegion(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, MaterialData material)
{
throw new RuntimeException("Not Supported");
}
@Override
public void setRegion(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, BlockData blockData)
{
throw new RuntimeException("Not Supported");
}
@Override
public Material getType(int x, int y, int z)
{
return getBlockData(x, y, z).getMaterial();
}
@Override
public MaterialData getTypeAndData(int x, int y, int z)
{
return new MaterialData(getBlockData(x, y, z).getMaterial());
}
@Override
public BlockData getBlockData(int x, int y, int z)
{
TerrainNode n = get(x, y, z);
if(n == null)
{
return Material.VOID_AIR.createBlockData();
}
return n.getBlockData();
}
public BlockData getBlockDataOrNull(int x, int y, int z)
{
TerrainNode n = get(x, y, z);
if(n == null)
{
return null;
}
return n.getBlockData();
}
@Override
public byte getData(int x, int y, int z)
{
throw new RuntimeException("Not Supported");
}
@Override
public Biome getBiome(int x, int z)
{
throw new RuntimeException("Not Supported");
}
@Override
public Biome getBiome(int x, int y, int z)
{
TerrainNode n = get(x, y, z);
if(n == null)
{
return Biome.THE_VOID;
}
return n.getBiome();
}
@Override
public void setBiome(int x, int z, Biome bio)
{
throw new RuntimeException("Not Supported");
}
@Override
public void setBiome(int x, int y, int z, Biome bio)
{
set(x, y, z, bio);
}
@SuppressWarnings("unchecked")
@SafeVarargs
public static TerrainHunk combined(TerrainNode defaultNode, TerrainHunk... hunks)
{
KList<HeightHunk> hhunks = new KList<>();
KList<Hunk<IrisBiome>> bhunks = new KList<>();
int w = 0;
int h = 0;
int d = 0;
for(TerrainHunk i : hunks)
{
hhunks.add(i.getHeight());
bhunks.add(i.getBiome());
w = Math.max(w, i.getW());
h = Math.max(h, i.getH());
d = Math.max(d, i.getD());
}
TerrainHunk b = new TerrainHunk(w, h, d);
b.fill(defaultNode);
for(TerrainHunk i : hunks)
{
b.insert(i);
}
b.height = HeightHunk.combined((byte) 0, hhunks.toArray(new HeightHunk[hhunks.size()]));
b.biome = Hunk.combined(null, hhunks.toArray(new Hunk[hhunks.size()]));
return b;
}
@SuppressWarnings("unchecked")
@SafeVarargs
public static TerrainHunk combined(TerrainHunk... hunks)
{
KList<HeightHunk> hhunks = new KList<>();
KList<Hunk<IrisBiome>> bhunks = new KList<>();
int w = 0;
int h = 0;
int d = 0;
for(TerrainHunk i : hunks)
{
hhunks.add(i.getHeight());
bhunks.add(i.getBiome());
w = Math.max(w, i.getW());
h = Math.max(h, i.getH());
d = Math.max(d, i.getD());
}
TerrainHunk b = new TerrainHunk(w, h, d);
for(TerrainHunk i : hunks)
{
b.insert(i);
}
b.height = HeightHunk.combined((byte) 0, hhunks.toArray(new HeightHunk[hhunks.size()]));
b.biome = Hunk.combined(null, hhunks.toArray(new Hunk[hhunks.size()]));
return b;
}
public void into(ChunkData c)
{
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
for(int k = 0; k < d; k++)
{
BlockData n = getBlockData(i, j, k);
if(n == null)
{
continue;
}
c.setBlock(i, j, k, n);
}
}
}
}
}

View File

@@ -0,0 +1,61 @@
package com.volmit.iris.gen.atomics;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import com.volmit.iris.util.KList;
public class TerrainNode
{
private static final KList<BlockData> blockDataPalette = new KList<BlockData>();
private final byte biome;
private final short block;
private TerrainNode(byte biome, short block)
{
this.biome = biome;
this.block = block;
}
public TerrainNode(Biome biome, BlockData block)
{
this((byte) (biome.ordinal()), (short) (paletteOf(block)));
}
public TerrainNode setBlockData(BlockData block)
{
return new TerrainNode(biome, (short) (paletteOf(block)));
}
public TerrainNode setBiome(Biome biome)
{
return new TerrainNode((byte) (biome.ordinal()), block);
}
public BlockData getBlockData()
{
return blockDataPalette.get(block);
}
public Biome getBiome()
{
return Biome.values()[biome];
}
private static int paletteOf(BlockData b)
{
synchronized(blockDataPalette)
{
int v = blockDataPalette.indexOf(b);
if(v >= 0)
{
return v;
}
blockDataPalette.add(b);
return blockDataPalette.size() - 1;
}
}
}