From b9d97f2434a1f64c16af306b3fb0d8359da2ddd9 Mon Sep 17 00:00:00 2001 From: cyberpwn Date: Sat, 25 Jun 2022 15:47:34 -0400 Subject: [PATCH] Goes faster --- .../engine/object/IrisGeneratorStyle.java | 20 +++- .../engine/platform/BukkitChunkGenerator.java | 10 +- .../volmit/iris/util/cache/ArrayCache.java | 82 ++++++++++++++++ .../volmit/iris/util/cache/ByteBitCache.java | 44 +++++++++ .../com/volmit/iris/util/cache/ByteCache.java | 76 +++++++++++++++ .../volmit/iris/util/cache/DataBitCache.java | 58 ++++++++++++ .../volmit/iris/util/cache/FloatBitCache.java | 44 +++++++++ .../volmit/iris/util/cache/FloatCache.java | 93 +++++++++++++++++++ .../volmit/iris/util/cache/IntBitCache.java | 44 +++++++++ .../com/volmit/iris/util/cache/IntCache.java | 78 ++++++++++++++++ .../volmit/iris/util/cache/ShortBitCache.java | 44 +++++++++ .../volmit/iris/util/cache/ShortCache.java | 76 +++++++++++++++ .../volmit/iris/util/cache/UByteBitCache.java | 40 ++++++++ .../volmit/iris/util/cache/UByteCache.java | 40 ++++++++ .../util/hunk/view/BiomeGridHunkHolder.java | 86 +++++++++++++++++ .../util/hunk/view/BiomeGridHunkView.java | 8 ++ .../util/hunk/view/ChunkDataHunkHolder.java | 76 +++++++++++++++ .../java/com/volmit/iris/util/noise/CNG.java | 58 +++++++++++- 18 files changed, 972 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/volmit/iris/util/cache/ArrayCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/ByteBitCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/ByteCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/DataBitCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/FloatBitCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/FloatCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/IntBitCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/IntCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/ShortBitCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/ShortCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/UByteBitCache.java create mode 100644 src/main/java/com/volmit/iris/util/cache/UByteCache.java create mode 100644 src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkHolder.java create mode 100644 src/main/java/com/volmit/iris/util/hunk/view/ChunkDataHunkHolder.java diff --git a/src/main/java/com/volmit/iris/engine/object/IrisGeneratorStyle.java b/src/main/java/com/volmit/iris/engine/object/IrisGeneratorStyle.java index 1ebb2f203..123a1d247 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisGeneratorStyle.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisGeneratorStyle.java @@ -35,6 +35,8 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; +import java.util.Objects; + @Snippet("style") @Accessors(chain = true) @NoArgsConstructor @@ -70,6 +72,10 @@ public class IrisGeneratorStyle { @MaxNumber(64) @Desc("The exponent") private double exponent = 1; + @MinNumber(0) + @MaxNumber(8192) + @Desc("If the cache size is set above 0, this generator will be cached") + private int cacheSize = 0; public IrisGeneratorStyle(NoiseStyle s) { this.style = s; @@ -81,6 +87,18 @@ public class IrisGeneratorStyle { } public CNG createNoCache(RNG rng, IrisData data) { + return createNoCache(rng, data, false); + } + + + private int hash() + { + return Objects.hash(expression, imageMap, multiplier, axialFracturing, fracture != null ? fracture.hash() : 0, exponent, cacheSize, zoom, cellularZoom, cellularFrequency, style); + } + + public CNG createNoCache(RNG rng, IrisData data, boolean actuallyCached) { + String cacheKey = hash() + ""; + if(getExpression() != null) { IrisExpression e = data.getExpressionLoader().load(getExpression()); @@ -134,7 +152,7 @@ public class IrisGeneratorStyle { } public CNG create(RNG rng, IrisData data) { - return cng.aquire(() -> createNoCache(rng, data)); + return cng.aquire(() -> createNoCache(rng, data, true)); } @SuppressWarnings("BooleanMethodIsAlwaysInverted") diff --git a/src/main/java/com/volmit/iris/engine/platform/BukkitChunkGenerator.java b/src/main/java/com/volmit/iris/engine/platform/BukkitChunkGenerator.java index e9216efb9..d12028f25 100644 --- a/src/main/java/com/volmit/iris/engine/platform/BukkitChunkGenerator.java +++ b/src/main/java/com/volmit/iris/engine/platform/BukkitChunkGenerator.java @@ -31,11 +31,15 @@ import com.volmit.iris.engine.object.StudioMode; import com.volmit.iris.engine.platform.studio.StudioGenerator; import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.data.IrisBiomeStorage; +import com.volmit.iris.util.format.Form; import com.volmit.iris.util.hunk.Hunk; +import com.volmit.iris.util.hunk.view.BiomeGridHunkHolder; +import com.volmit.iris.util.hunk.view.ChunkDataHunkHolder; import com.volmit.iris.util.io.ReactiveFolder; import com.volmit.iris.util.scheduling.ChronoLatch; import com.volmit.iris.util.scheduling.J; import com.volmit.iris.util.scheduling.Looper; +import com.volmit.iris.util.scheduling.PrecisionStopwatch; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.Setter; @@ -278,9 +282,11 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun if(studioGenerator != null) { studioGenerator.generateChunk(getEngine(), tc, x, z); } else { - Hunk blocks = Hunk.view(tc); - Hunk biomes = Hunk.view(tc, tc.getMinHeight(), tc.getMaxHeight()); + ChunkDataHunkHolder blocks = new ChunkDataHunkHolder(tc); + BiomeGridHunkHolder biomes = new BiomeGridHunkHolder(tc, tc.getMinHeight(), tc.getMaxHeight()); getEngine().generate(x << 4, z << 4, blocks, biomes, true); + blocks.apply(); + biomes.apply(); } ChunkData c = tc.getRaw(); diff --git a/src/main/java/com/volmit/iris/util/cache/ArrayCache.java b/src/main/java/com/volmit/iris/util/cache/ArrayCache.java new file mode 100644 index 000000000..8b578898e --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/ArrayCache.java @@ -0,0 +1,82 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import com.volmit.iris.engine.data.cache.Cache; +import com.volmit.iris.util.hunk.bits.Writable; + +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface ArrayCache extends Writable { + T get(int i); + + void set(int i, T t); + + void iset(int i, int v); + + int getWidth(); + + int getHeight(); + + void writeCache(DataOutputStream dos) throws IOException; + + static int zigZag(int coord, int size) + { + if(coord < 0) + { + coord = Math.abs(coord); + } + + if(coord % (size * 2) >= size) + { + return (size) - (coord % size) - 1; + } + + else { + return coord % size; + } + } + + default void set(int x, int y, T v) + { + set((zigZag(y, getHeight()) * getWidth()) + zigZag(x, getWidth()), v); + } + + default T get(int x, int y) + { + try + { + return get((zigZag(y, getHeight()) * getWidth()) + zigZag(x, getWidth())); + } + + catch(Throwable e) + { + e.printStackTrace(); + throw e; + } + } + + default void iset(int x, int y, int v) + { + iset((zigZag(y, getHeight()) * getWidth()) + zigZag(x, getWidth()), v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/ByteBitCache.java b/src/main/java/com/volmit/iris/util/cache/ByteBitCache.java new file mode 100644 index 000000000..eb701973f --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/ByteBitCache.java @@ -0,0 +1,44 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ByteBitCache extends DataBitCache { + public ByteBitCache(int width, int height) { + super(width, height); + } + + @Override + public Integer readNodeData(DataInputStream din) throws IOException { + return (int) din.readByte(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Integer integer) throws IOException { + dos.writeByte(integer); + } + + @Override + public void iset(int i, int v) { + set(i, v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/ByteCache.java b/src/main/java/com/volmit/iris/util/cache/ByteCache.java new file mode 100644 index 000000000..7c9421b3a --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/ByteCache.java @@ -0,0 +1,76 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import lombok.Getter; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ByteCache implements ArrayCache { + @Getter + private final int width; + @Getter + private final int height; + private final byte[] cache; + + public ByteCache(int width, int height) + { + this.width = width; + this.height = height; + cache = new byte[width * height]; + } + + public void set(int i, Integer v) + { + cache[i] = v.byteValue(); + } + + public Integer get(int i) + { + return (int)cache[i]; + } + + @Override + public void writeCache(DataOutputStream dos) throws IOException { + dos.writeInt(width); + dos.writeInt(height); + + for(int i = 0; i < width * height; i++) + { + dos.writeByte(get(i)); + } + } + + @Override + public Integer readNodeData(DataInputStream din) throws IOException { + return (int) din.readByte(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Integer integer) throws IOException { + dos.writeByte(integer); + } + + @Override + public void iset(int i, int v) { + set(i, v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/DataBitCache.java b/src/main/java/com/volmit/iris/util/cache/DataBitCache.java new file mode 100644 index 000000000..ba8ff7ebf --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/DataBitCache.java @@ -0,0 +1,58 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import com.volmit.iris.util.hunk.bits.DataContainer; +import lombok.Getter; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public abstract class DataBitCache implements ArrayCache { + @Getter + private final int width; + @Getter + private final int height; + private final DataContainer cache; + + public DataBitCache(int width, int height) + { + this.width = width; + this.height = height; + cache = new DataContainer<>(this, width * height); + } + + public void set(int i, T v) + { + cache.set(i, v); + } + + public T get(int i) + { + return cache.get(i); + } + + @Override + public void writeCache(DataOutputStream dos) throws IOException { + dos.writeInt(width); + dos.writeInt(height); + cache.writeDos(dos); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/FloatBitCache.java b/src/main/java/com/volmit/iris/util/cache/FloatBitCache.java new file mode 100644 index 000000000..4d0473dfc --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/FloatBitCache.java @@ -0,0 +1,44 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class FloatBitCache extends DataBitCache { + public FloatBitCache(int width, int height) { + super(width, height); + } + + @Override + public Float readNodeData(DataInputStream din) throws IOException { + return din.readFloat(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Float integer) throws IOException { + dos.writeFloat(integer); + } + + @Override + public void iset(int i, int v) { + set(i, (float)v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/FloatCache.java b/src/main/java/com/volmit/iris/util/cache/FloatCache.java new file mode 100644 index 000000000..334922a52 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/FloatCache.java @@ -0,0 +1,93 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import lombok.Getter; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class FloatCache implements ArrayCache { + @Getter + private final int width; + @Getter + private final int height; + private final float[] cache; + + + public FloatCache(File file) throws IOException { + this(new DataInputStream(new FileInputStream(file))); + } + + public FloatCache(DataInputStream din) throws IOException { + this(din.readInt(), din.readInt()); + for(int i = 0; i < width * height; i++) + { + cache[i] = din.readFloat(); + } + din.close(); + } + + public FloatCache(int width, int height) + { + this.width = width; + this.height = height; + cache = new float[width * height]; + } + + public void set(int i, Float v) + { + cache[i] = v; + } + + public Float get(int i) + { + return cache[i]; + } + + @Override + public void writeCache(DataOutputStream dos) throws IOException { + dos.writeInt(width); + dos.writeInt(height); + + for(int i = 0; i < width * height; i++) + { + dos.writeFloat(get(i)); + } + } + + @Override + public Float readNodeData(DataInputStream din) throws IOException { + return din.readFloat(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Float integer) throws IOException { + dos.writeFloat(integer); + } + + @Override + public void iset(int i, int v) { + set(i, (float) v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/IntBitCache.java b/src/main/java/com/volmit/iris/util/cache/IntBitCache.java new file mode 100644 index 000000000..c06e62682 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/IntBitCache.java @@ -0,0 +1,44 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class IntBitCache extends DataBitCache { + public IntBitCache(int width, int height) { + super(width, height); + } + + @Override + public Integer readNodeData(DataInputStream din) throws IOException { + return din.readInt(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Integer integer) throws IOException { + dos.writeInt(integer); + } + + @Override + public void iset(int i, int v) { + set(i, v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/IntCache.java b/src/main/java/com/volmit/iris/util/cache/IntCache.java new file mode 100644 index 000000000..63509466e --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/IntCache.java @@ -0,0 +1,78 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import com.volmit.iris.util.hunk.bits.DataContainer; +import com.volmit.iris.util.hunk.bits.Writable; +import lombok.Getter; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class IntCache implements ArrayCache { + @Getter + private final int width; + @Getter + private final int height; + private final int[] cache; + + public IntCache(int width, int height) + { + this.width = width; + this.height = height; + cache = new int[width * height]; + } + + public void set(int i, Integer v) + { + cache[i] = v; + } + + public Integer get(int i) + { + return cache[i]; + } + + @Override + public void writeCache(DataOutputStream dos) throws IOException { + dos.writeInt(width); + dos.writeInt(height); + + for(int i = 0; i < width * height; i++) + { + dos.writeInt(get(i)); + } + } + + @Override + public Integer readNodeData(DataInputStream din) throws IOException { + return din.readInt(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Integer integer) throws IOException { + dos.writeInt(integer); + } + + @Override + public void iset(int i, int v) { + set(i, v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/ShortBitCache.java b/src/main/java/com/volmit/iris/util/cache/ShortBitCache.java new file mode 100644 index 000000000..4bb01d053 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/ShortBitCache.java @@ -0,0 +1,44 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ShortBitCache extends DataBitCache { + public ShortBitCache(int width, int height) { + super(width, height); + } + + @Override + public Short readNodeData(DataInputStream din) throws IOException { + return din.readShort(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Short integer) throws IOException { + dos.writeShort(integer); + } + + @Override + public void iset(int i, int v) { + set(i, (short) v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/ShortCache.java b/src/main/java/com/volmit/iris/util/cache/ShortCache.java new file mode 100644 index 000000000..b25f3981d --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/ShortCache.java @@ -0,0 +1,76 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +import lombok.Getter; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ShortCache implements ArrayCache { + @Getter + private final int width; + @Getter + private final int height; + private final short[] cache; + + public ShortCache(int width, int height) + { + this.width = width; + this.height = height; + cache = new short[width * height]; + } + + public void set(int i, Short v) + { + cache[i] = v; + } + + public Short get(int i) + { + return cache[i]; + } + + @Override + public void writeCache(DataOutputStream dos) throws IOException { + dos.writeInt(width); + dos.writeInt(height); + + for(int i = 0; i < width * height; i++) + { + dos.writeShort(get(i)); + } + } + + @Override + public Short readNodeData(DataInputStream din) throws IOException { + return din.readShort(); + } + + @Override + public void writeNodeData(DataOutputStream dos, Short integer) throws IOException { + dos.writeShort(integer); + } + + @Override + public void iset(int i, int v) { + set(i, (short) v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/UByteBitCache.java b/src/main/java/com/volmit/iris/util/cache/UByteBitCache.java new file mode 100644 index 000000000..4a1309f7b --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/UByteBitCache.java @@ -0,0 +1,40 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +public class UByteBitCache extends ByteBitCache { + public UByteBitCache(int width, int height) { + super(width, height); + } + + @Override + public void set(int i, Integer v) { + super.set(i, v + Byte.MIN_VALUE); + } + + @Override + public Integer get(int i) { + return super.get(i) - Byte.MIN_VALUE; + } + + @Override + public void iset(int i, int v) { + set(i, v); + } +} diff --git a/src/main/java/com/volmit/iris/util/cache/UByteCache.java b/src/main/java/com/volmit/iris/util/cache/UByteCache.java new file mode 100644 index 000000000..81f5c23cc --- /dev/null +++ b/src/main/java/com/volmit/iris/util/cache/UByteCache.java @@ -0,0 +1,40 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.cache; + +public class UByteCache extends ByteCache { + public UByteCache(int width, int height) { + super(width, height); + } + + @Override + public void set(int i, Integer v) { + super.set(i, v + Byte.MIN_VALUE); + } + + @Override + public Integer get(int i) { + return super.get(i) - Byte.MIN_VALUE; + } + + @Override + public void iset(int i, int v) { + set(i, v); + } +} diff --git a/src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkHolder.java b/src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkHolder.java new file mode 100644 index 000000000..a7b066526 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkHolder.java @@ -0,0 +1,86 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.hunk.view; + +import com.volmit.iris.core.nms.INMS; +import com.volmit.iris.engine.data.chunk.LinkedTerrainChunk; +import com.volmit.iris.util.hunk.storage.AtomicHunk; +import lombok.Getter; +import org.bukkit.block.Biome; +import org.bukkit.generator.ChunkGenerator.BiomeGrid; + +@SuppressWarnings("ClassCanBeRecord") +public class BiomeGridHunkHolder extends AtomicHunk { + @Getter + private final BiomeGrid chunk; + private final int minHeight; + private final int maxHeight; + + public BiomeGridHunkHolder(BiomeGrid chunk, int minHeight, int maxHeight) { + super(16, maxHeight - minHeight, 16); + this.chunk = chunk; + this.minHeight = minHeight; + this.maxHeight = maxHeight; + } + + @Override + public int getWidth() { + return 16; + } + + @Override + public int getDepth() { + return 16; + } + + @Override + public int getHeight() { + return maxHeight - minHeight; + } + + public void apply() { + for(int i = 0; i < getHeight(); i++) { + for(int j = 0; j < getWidth(); j++) { + for(int k = 0; k < getDepth(); k++) { + Biome b = super.getRaw(j, i, k); + + if(b != null) + { + chunk.setBiome(j, i + minHeight, k, b); + } + } + } + } + } + + @Override + public Biome getRaw(int x, int y, int z) { + Biome b = super.getRaw(x, y, z); + + return b != null ? b : Biome.PLAINS; + } + + public void forceBiomeBaseInto(int x, int y, int z, Object somethingVeryDirty) { + if(chunk instanceof LinkedTerrainChunk) { + INMS.get().forceBiomeInto(x, y + minHeight, z, somethingVeryDirty, ((LinkedTerrainChunk) chunk).getRawBiome()); + return; + } + INMS.get().forceBiomeInto(x, y + minHeight, z, somethingVeryDirty, chunk); + } +} diff --git a/src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkView.java b/src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkView.java index c205162ee..ce1654acd 100644 --- a/src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkView.java +++ b/src/main/java/com/volmit/iris/util/hunk/view/BiomeGridHunkView.java @@ -18,6 +18,7 @@ package com.volmit.iris.util.hunk.view; +import com.volmit.iris.Iris; import com.volmit.iris.core.nms.INMS; import com.volmit.iris.engine.data.chunk.LinkedTerrainChunk; import com.volmit.iris.util.hunk.Hunk; @@ -31,6 +32,7 @@ public class BiomeGridHunkView implements Hunk { private final BiomeGrid chunk; private final int minHeight; private final int maxHeight; + private int highest = -1000; public BiomeGridHunkView(BiomeGrid chunk, int minHeight, int maxHeight) { this.chunk = chunk; @@ -56,6 +58,12 @@ public class BiomeGridHunkView implements Hunk { @Override public void setRaw(int x, int y, int z, Biome t) { chunk.setBiome(x, y + minHeight, z, t); + + if(y > highest) + { + highest = y; + Iris.info("Highest = " + highest); + } } @Override diff --git a/src/main/java/com/volmit/iris/util/hunk/view/ChunkDataHunkHolder.java b/src/main/java/com/volmit/iris/util/hunk/view/ChunkDataHunkHolder.java new file mode 100644 index 000000000..cec67eb41 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/hunk/view/ChunkDataHunkHolder.java @@ -0,0 +1,76 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.hunk.view; + +import com.volmit.iris.util.hunk.Hunk; +import com.volmit.iris.util.hunk.storage.AtomicHunk; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.data.BlockData; +import org.bukkit.generator.ChunkGenerator.ChunkData; + +import java.util.concurrent.atomic.AtomicReferenceArray; + +@SuppressWarnings("ClassCanBeRecord") +public class ChunkDataHunkHolder extends AtomicHunk { + private static final BlockData AIR = Material.AIR.createBlockData(); + private final ChunkData chunk; + + public ChunkDataHunkHolder(ChunkData chunk) { + super(16, chunk.getMaxHeight() - chunk.getMinHeight(), 16); + this.chunk = chunk; + } + + @Override + public int getWidth() { + return 16; + } + + @Override + public int getDepth() { + return 16; + } + + @Override + public int getHeight() { + return chunk.getMaxHeight() - chunk.getMinHeight(); + } + + @Override + public BlockData getRaw(int x, int y, int z) { + BlockData b = super.getRaw(x, y, z); + + return b != null ? b : AIR; + } + + public void apply() { + for(int i = 0; i < getHeight(); i++) { + for(int j = 0; j < getWidth(); j++) { + for(int k = 0; k < getDepth(); k++) { + BlockData b = super.getRaw(j, i, k); + + if(b != null) + { + chunk.setBlock(j, i + chunk.getMinHeight(), k, b); + } + } + } + } + } +} diff --git a/src/main/java/com/volmit/iris/util/noise/CNG.java b/src/main/java/com/volmit/iris/util/noise/CNG.java index 44d052e53..4d3ab4b6b 100644 --- a/src/main/java/com/volmit/iris/util/noise/CNG.java +++ b/src/main/java/com/volmit/iris/util/noise/CNG.java @@ -22,6 +22,8 @@ import com.volmit.iris.Iris; import com.volmit.iris.engine.data.cache.AtomicCache; import com.volmit.iris.engine.object.IRare; import com.volmit.iris.engine.object.NoiseStyle; +import com.volmit.iris.util.cache.FloatBitCache; +import com.volmit.iris.util.cache.FloatCache; import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.format.Form; import com.volmit.iris.util.function.NoiseInjector; @@ -33,6 +35,11 @@ import com.volmit.iris.util.stream.arithmetic.FittedStream; import com.volmit.iris.util.stream.sources.CNGStream; import lombok.Data; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.List; @Data @@ -56,6 +63,7 @@ public class CNG { private boolean trueFracturing = false; private KList children; private CNG fracture; + private FloatCache cache; private NoiseGenerator generator; private NoiseInjector injector; private RNG rng; @@ -133,14 +141,55 @@ public class CNG { }, 1D, 1); } - public CNG cached(int size) + public CNG cached(int size, String key, File cacheFolder) { if(size <= 0) { return this; } - generator = new CachedNoise(generator, size); + cache = null; + + File f = new File(new File(cacheFolder, ".cache"), key + ".cnm"); + FloatCache fbc; + boolean cached = false; + if(f.exists()) + { + try { + fbc = new FloatCache(f); + cached = true; + } catch(IOException e) { + fbc = new FloatCache(size, size); + } + } + + else { + fbc = new FloatCache(size, size); + } + + if(!cached) + { + for(int i = 0; i < size; i++) + { + for(int j = 0; j < size; j++) + { + fbc.set(i, j, (float) noise(i, j)); + } + } + + try { + f.getParentFile().mkdirs(); + FileOutputStream fos = new FileOutputStream(f); + DataOutputStream dos = new DataOutputStream(fos); + fbc.writeCache(dos); + dos.close(); + Iris.info("Saved Noise Cache " + f.getName()); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + + cache = fbc; return this; } @@ -435,6 +484,11 @@ public class CNG { } public double noise(double... dim) { + if(cache != null && dim.length == 2) + { + return cache.get((int)dim[0], (int)dim[1]); + } + double n = getNoise(dim); n = power != 1D ? (n < 0 ? -Math.pow(Math.abs(n), power) : Math.pow(n, power)) : n; double m = 1;