From e841bfad68d385d2082f77a0b94a7f861bb66255 Mon Sep 17 00:00:00 2001 From: Daniel Mills Date: Sat, 24 Oct 2020 00:29:20 -0400 Subject: [PATCH] Hunk Views --- .../java/com/volmit/iris/gen/v2/HunkView.java | 175 ++++++++++++++++++ .../java/com/volmit/iris/gen/v2/TestGen.java | 48 +++++ 2 files changed, 223 insertions(+) create mode 100644 src/main/java/com/volmit/iris/gen/v2/HunkView.java create mode 100644 src/main/java/com/volmit/iris/gen/v2/TestGen.java diff --git a/src/main/java/com/volmit/iris/gen/v2/HunkView.java b/src/main/java/com/volmit/iris/gen/v2/HunkView.java new file mode 100644 index 000000000..96443949c --- /dev/null +++ b/src/main/java/com/volmit/iris/gen/v2/HunkView.java @@ -0,0 +1,175 @@ +package com.volmit.iris.gen.v2; + +import com.volmit.iris.gen.v2.scaffold.ArrayHunk; +import com.volmit.iris.gen.v2.scaffold.Hunk; +import com.volmit.iris.gen.v2.scaffold.HunkFace; + +public class HunkView implements Hunk +{ + private final int ox; + private final int oy; + private final int oz; + private final int w; + private final int h; + private final int d; + private final Hunk src; + + public HunkView(Hunk src) + { + this(src, src.getWidth(), src.getHeight(), src.getDepth()); + } + + public HunkView(Hunk src, int w, int h, int d) + { + this(src, w, h, d, 0, 0, 0); + } + + public HunkView(Hunk src, int w, int h, int d, int ox, int oy, int oz) + { + this.src = src; + this.w = w; + this.h = h; + this.d = d; + this.ox = ox; + this.oy = oy; + this.oz = oz; + } + + @Override + public Hunk croppedView(int x1, int y1, int z1, int x2, int y2, int z2) + { + return new HunkView(this, x2 - x1, y2 - y1, z2 - z1, x1 + ox, y1 + oy, z1 + oz); + } + + @Override + public ArrayHunk crop(int x1, int y1, int z1, int x2, int y2, int z2) + { + ArrayHunk h = new ArrayHunk(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; + } + + @Override + public void insert(int offX, int offY, int offZ, Hunk hunk, boolean invertY) + { + if(offX + (hunk.getWidth() - 1) >= w || offY + (hunk.getHeight() - 1) >= h || offZ + (hunk.getDepth() - 1) >= d || offX < 0 || offY < 0 || offZ < 0) + { + throw new RuntimeException("Cannot insert hunk " + hunk.getWidth() + "," + hunk.getHeight() + "," + hunk.getDepth() + " into Hunk " + w + "," + h + "," + d + " with offset " + offZ + "," + offY + "," + offZ); + } + + for(int i = offX; i < offX + hunk.getWidth(); i++) + { + for(int j = offY; j < offY + hunk.getHeight(); j++) + { + for(int k = offZ; k < offZ + hunk.getDepth(); k++) + { + set(i, j, k, hunk.get(i - offX, j - offY, k - offZ)); + } + } + } + } + + @Override + public void set(int x1, int y1, int z1, int x2, int y2, int z2, T t) + { + if(x1 >= w || y1 >= h || z1 >= d || x2 >= w || y2 >= h || z2 >= d) + { + throw new RuntimeException(x1 + "-" + x2 + " " + y1 + "-" + y2 + " " + z1 + "-" + z2 + " is out of the bounds 0,0,0 - " + (w - 1) + "," + (h - 1) + "," + (d - 1)); + } + + src.set(x1, y1, z1, x2, y2, z2, t); + } + + @Override + public void set(int x, int y, int z, T t) + { + 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)); + } + + src.set(x + ox, y + oy, z + oz, t); + } + + @Override + public T get(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 src.get(x + ox, y + oy, z + oz); + } + + @Override + public T getClosest(int x, int y, int z) + { + return src.get(x >= w ? w + ox - 1 : x + ox, y >= h ? h + oy - 1 : y + oy, z >= d ? d + oz - 1 : z + oz); + } + + @Override + public void fill(T t) + { + set(0 + ox, 0 + oy, 0 + oz, w - 1 + ox, h - 1 + oy, d - 1 + oz, t); + } + + @Override + public int getWidth() + { + return w; + } + + @Override + public int getDepth() + { + return d; + } + + @Override + public int getHeight() + { + return h; + } + + @Override + public Hunk getFace(HunkFace f) + { + switch(f) + { + case BOTTOM: + return croppedView(0, 0, 0, getWidth() - 1, 0, getDepth() - 1); + case EAST: + return croppedView(getWidth() - 1, 0, 0, getWidth() - 1, getHeight() - 1, getDepth() - 1); + case NORTH: + return croppedView(0, 0, 0, getWidth() - 1, getHeight() - 1, 0); + case SOUTH: + return croppedView(0, 0, 0, 0, getHeight() - 1, getDepth() - 1); + case TOP: + return croppedView(0, getHeight() - 1, 0, getWidth() - 1, getHeight() - 1, getDepth() - 1); + case WEST: + return croppedView(0, 0, getDepth() - 1, getWidth() - 1, getHeight() - 1, getDepth() - 1); + default: + break; + } + + return null; + } + + @Override + public Hunk getSource() + { + return src; + } +} diff --git a/src/main/java/com/volmit/iris/gen/v2/TestGen.java b/src/main/java/com/volmit/iris/gen/v2/TestGen.java new file mode 100644 index 000000000..46a884c86 --- /dev/null +++ b/src/main/java/com/volmit/iris/gen/v2/TestGen.java @@ -0,0 +1,48 @@ +package com.volmit.iris.gen.v2; + +import java.util.Random; +import java.util.UUID; + +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.WorldCreator; +import org.bukkit.block.data.BlockData; +import org.bukkit.entity.Player; +import org.bukkit.generator.ChunkGenerator; + +import com.volmit.iris.gen.v2.scaffold.Hunk; +import com.volmit.iris.gen.v2.scaffold.layer.ProceduralStream; +import com.volmit.iris.object.NoiseStyle; + +public class TestGen +{ + public static void gen(Player p) + { + p.teleport(new WorldCreator("t/" + UUID.randomUUID().toString()).generator(new ChunkGenerator() + { + //@builder + ProceduralStream rock = NoiseStyle.STATIC.stream(1337) + .select(new Material[] { + Material.STONE, + Material.ANDESITE + }).convertCached((m) -> m.createBlockData()); + ProceduralStream terrain = NoiseStyle.CELLULAR.stream(1337) + .fit(1, 32) + .offset(1000, 1000) + .zoom(2.5) + .interpolate().starcast(8, 9) + .into().bilinear(8); + //@done + + @Override + public ChunkData generateChunkData(World world, Random random, int x, int z, BiomeGrid biome) + { + ChunkData c = createChunkData(world); + Hunk data = Hunk.view(c); + terrain.fillUp2D(data, x * 16, z * 16, rock); + + return c; + } + }).createWorld().getSpawnLocation()); + } +}