From 3a7416462714a0432f345bda3838d81e9fa61b67 Mon Sep 17 00:00:00 2001 From: Julian Krings Date: Wed, 8 Jan 2025 13:53:33 +0100 Subject: [PATCH] implement priority to static placement component --- .../components/MantleObjectComponent.java | 111 ++++++++---------- .../components/MantleStaticComponent.java | 33 +++++- 2 files changed, 80 insertions(+), 64 deletions(-) diff --git a/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleObjectComponent.java b/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleObjectComponent.java index 8a0514500..18b711446 100644 --- a/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleObjectComponent.java +++ b/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleObjectComponent.java @@ -19,6 +19,7 @@ package com.volmit.iris.engine.mantle.components; import com.volmit.iris.Iris; +import com.volmit.iris.core.loader.IrisData; import com.volmit.iris.engine.data.cache.Cache; import com.volmit.iris.engine.mantle.EngineMantle; import com.volmit.iris.engine.mantle.IrisMantleComponent; @@ -39,11 +40,13 @@ import com.volmit.iris.util.matter.MatterStructurePOI; import com.volmit.iris.util.noise.CNG; import com.volmit.iris.util.noise.NoiseType; import com.volmit.iris.util.parallel.BurstExecutor; +import com.volmit.iris.util.parallel.MultiBurst; import lombok.Getter; import org.bukkit.util.BlockVector; import java.io.IOException; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; @@ -162,99 +165,67 @@ public class MantleObjectComponent extends IrisMantleComponent { private int computeRadius() { var dimension = getDimension(); - AtomicInteger xg = new AtomicInteger(); - AtomicInteger zg = new AtomicInteger(); - KSet objects = new KSet<>(); KMap> scalars = new KMap<>(); for (var region : dimension.getAllRegions(this::getData)) { - for (var j : region.getObjects()) { - if (j.getScale().canScaleBeyond()) { - scalars.put(j.getScale(), j.getPlace()); + for (var placement : region.getObjects()) { + if (placement.getScale().canScaleBeyond()) { + scalars.put(placement.getScale(), placement.getPlace()); } else { - objects.addAll(j.getPlace()); + objects.addAll(placement.getPlace()); } } - } - for (var biome : dimension.getAllBiomes(this::getData)) { - for (var j : biome.getObjects()) { - if (j.getScale().canScaleBeyond()) { - scalars.put(j.getScale(), j.getPlace()); - } else { - objects.addAll(j.getPlace()); + + for (var biome : region.getAllBiomes(this::getData)) { + for (var placement : biome.getObjects()) { + if (placement.getScale().canScaleBeyond()) { + scalars.put(placement.getScale(), placement.getPlace()); + } else { + objects.addAll(placement.getPlace()); + } } } } - BurstExecutor e = getEngineMantle().getTarget().getBurster().burst(objects.size()); + return computeObjectRadius(objects, scalars, getEngineMantle().getTarget().getBurster(), getData()); + } + + static int computeObjectRadius(KSet objects, KMap> scalars, MultiBurst burst, IrisData data) { + AtomicInteger x = new AtomicInteger(); + AtomicInteger z = new AtomicInteger(); + + BurstExecutor e = burst.burst(objects.size()); KMap sizeCache = new KMap<>(); - for (String i : objects) { + for (String loadKey : objects) { e.queue(() -> { try { - BlockVector bv = sizeCache.computeIfAbsent(i, (k) -> { - try { - return IrisObject.sampleSize(getData().getObjectLoader().findFile(i)); - } catch (IOException ex) { - Iris.reportError(ex); - ex.printStackTrace(); - } - - return null; - }); - - if (bv == null) { - throw new RuntimeException(); - } + BlockVector bv = sampleSize(sizeCache, data, loadKey); if (Math.max(bv.getBlockX(), bv.getBlockZ()) > 128) { - Iris.warn("Object " + i + " has a large size (" + bv + ") and may increase memory usage!"); + Iris.warn("Object " + loadKey + " has a large size (" + bv + ") and may increase memory usage!"); } - synchronized (xg) { - xg.getAndSet(Math.max(bv.getBlockX(), xg.get())); - } - - synchronized (zg) { - zg.getAndSet(Math.max(bv.getBlockZ(), zg.get())); - } + x.getAndUpdate(i -> Math.max(bv.getBlockX(), i)); + z.getAndUpdate(i -> Math.max(bv.getBlockZ(), i)); } catch (Throwable ed) { Iris.reportError(ed); - } }); } for (Map.Entry> entry : scalars.entrySet()) { double ms = entry.getKey().getMaximumScale(); - for (String j : entry.getValue()) { + for (String loadKey : entry.getValue()) { e.queue(() -> { try { - BlockVector bv = sizeCache.computeIfAbsent(j, (k) -> { - try { - return IrisObject.sampleSize(getData().getObjectLoader().findFile(j)); - } catch (IOException ioException) { - Iris.reportError(ioException); - ioException.printStackTrace(); - } - - return null; - }); - - if (bv == null) { - throw new RuntimeException(); - } + BlockVector bv = sampleSize(sizeCache, data, loadKey); if (Math.max(bv.getBlockX(), bv.getBlockZ()) > 128) { - Iris.warn("Object " + j + " has a large size (" + bv + ") and may increase memory usage! (Object scaled up to " + Form.pc(ms, 2) + ")"); + Iris.warn("Object " + loadKey + " has a large size (" + bv + ") and may increase memory usage! (Object scaled up to " + Form.pc(ms, 2) + ")"); } - synchronized (xg) { - xg.getAndSet((int) Math.max(Math.ceil(bv.getBlockX() * ms), xg.get())); - } - - synchronized (zg) { - zg.getAndSet((int) Math.max(Math.ceil(bv.getBlockZ() * ms), zg.get())); - } + x.getAndUpdate(i -> (int) Math.max(Math.ceil(bv.getBlockX() * ms), i)); + x.getAndUpdate(i -> (int) Math.max(Math.ceil(bv.getBlockZ() * ms), i)); } catch (Throwable ee) { Iris.reportError(ee); @@ -264,6 +235,20 @@ public class MantleObjectComponent extends IrisMantleComponent { } e.complete(); - return Math.max(xg.get(), zg.get()); + return Math.max(x.get(), z.get()); + } + + private static BlockVector sampleSize(KMap sizeCache, IrisData data, String loadKey) { + BlockVector bv = sizeCache.computeIfAbsent(loadKey, (k) -> { + try { + return IrisObject.sampleSize(data.getObjectLoader().findFile(loadKey)); + } catch (IOException ioException) { + Iris.reportError(ioException); + ioException.printStackTrace(); + } + + return null; + }); + return Objects.requireNonNull(bv, "sampleSize returned a null block vector"); } } diff --git a/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleStaticComponent.java b/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleStaticComponent.java index cc0ba6e42..dfa38bf8a 100644 --- a/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleStaticComponent.java +++ b/core/src/main/java/com/volmit/iris/engine/mantle/components/MantleStaticComponent.java @@ -3,18 +3,25 @@ package com.volmit.iris.engine.mantle.components; import com.volmit.iris.engine.mantle.EngineMantle; import com.volmit.iris.engine.mantle.IrisMantleComponent; import com.volmit.iris.engine.mantle.MantleWriter; +import com.volmit.iris.engine.object.IrisObjectScale; import com.volmit.iris.engine.object.IrisStaticPlacement; import com.volmit.iris.engine.object.NoiseStyle; +import com.volmit.iris.util.collection.KList; +import com.volmit.iris.util.collection.KMap; +import com.volmit.iris.util.collection.KSet; import com.volmit.iris.util.context.ChunkContext; import com.volmit.iris.util.mantle.MantleFlag; import com.volmit.iris.util.math.RNG; import com.volmit.iris.util.noise.CNG; +import lombok.Getter; public class MantleStaticComponent extends IrisMantleComponent { private final CNG cng; + @Getter + private final int radius = computeRadius(); public MantleStaticComponent(EngineMantle engineMantle) { - super(engineMantle, MantleFlag.STATIC); + super(engineMantle, MantleFlag.STATIC, 1); cng = NoiseStyle.STATIC.create(new RNG(seed())); } @@ -25,4 +32,28 @@ public class MantleStaticComponent extends IrisMantleComponent { placement.place(writer, rng, getData()); } } + + private int computeRadius() { + var placements = getDimension().getStaticPlacements(); + + KSet objects = new KSet<>(); + KMap> scalars = new KMap<>(); + for (var staticPlacement : placements.getObjects()) { + var placement = staticPlacement.placement(); + if (placement.getScale().canScaleBeyond()) { + scalars.put(placement.getScale(), placement.getPlace()); + } else { + objects.addAll(placement.getPlace()); + } + } + + int jigsaw = placements.getStructures() + .stream() + .mapToInt(staticPlacement -> staticPlacement.maxDimension(getData())) + .max() + .orElse(0); + int object = MantleObjectComponent.computeObjectRadius(objects, scalars, getEngineMantle().getTarget().getBurster(), getData()); + + return Math.max(jigsaw, object); + } }