From 93675d90bb45868031b4350e1ca80d8ebd9da0dd Mon Sep 17 00:00:00 2001 From: Daniel Mills Date: Mon, 2 Aug 2021 01:13:07 -0400 Subject: [PATCH] 3D Caching for procedural streams --- .../iris/engine/stream/ProceduralStream.java | 4 ++ .../engine/stream/utility/CachedStream3D.java | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/java/com/volmit/iris/engine/stream/utility/CachedStream3D.java diff --git a/src/main/java/com/volmit/iris/engine/stream/ProceduralStream.java b/src/main/java/com/volmit/iris/engine/stream/ProceduralStream.java index 1b73a219f..17ec95e08 100644 --- a/src/main/java/com/volmit/iris/engine/stream/ProceduralStream.java +++ b/src/main/java/com/volmit/iris/engine/stream/ProceduralStream.java @@ -262,6 +262,10 @@ public interface ProceduralStream extends ProceduralLayer, Interpolated { return new CachedStream2D(this, maxSize); } + default ProceduralStream cache3D(int maxSize) { + return new CachedStream3D(this, maxSize); + } + default ProceduralStream convert(Function converter) { return new ConversionStream(this, converter); } diff --git a/src/main/java/com/volmit/iris/engine/stream/utility/CachedStream3D.java b/src/main/java/com/volmit/iris/engine/stream/utility/CachedStream3D.java new file mode 100644 index 000000000..47630b7c0 --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/stream/utility/CachedStream3D.java @@ -0,0 +1,60 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 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.engine.stream.utility; + +import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; +import com.volmit.iris.engine.cache.Cache; +import com.volmit.iris.engine.stream.BasicStream; +import com.volmit.iris.engine.stream.ProceduralStream; +import com.volmit.iris.util.math.BlockPosition; + +public class CachedStream3D extends BasicStream implements ProceduralStream { + private final ProceduralStream stream; + private final ConcurrentLinkedHashMap cache; + + public CachedStream3D(ProceduralStream stream, int size) { + super(); + this.stream = stream; + cache = new ConcurrentLinkedHashMap.Builder() + .initialCapacity(size) + .maximumWeightedCapacity(size) + .concurrencyLevel(32) + .build(); + } + + @Override + public double toDouble(T t) { + return stream.toDouble(t); + } + + @Override + public T fromDouble(double d) { + return stream.fromDouble(d); + } + + @Override + public T get(double x, double z) { + return cache.compute(new BlockPosition((int)x, -1, (int)z), (k, v) -> v != null ? v : stream.get((int) x, (int) z)); + } + + @Override + public T get(double x, double y, double z) { + return cache.compute(new BlockPosition((int)x, (int)y, (int)z), (k, v) -> v != null ? v : stream.get((int) x, (int) y, (int) z)); + } +}