diff --git a/core/src/main/java/art/arcane/iris/engine/object/IrisObjectPlacementRunner.java b/core/src/main/java/art/arcane/iris/engine/object/IrisObjectPlacementRunner.java index f8a73780e..6f4babfc8 100644 --- a/core/src/main/java/art/arcane/iris/engine/object/IrisObjectPlacementRunner.java +++ b/core/src/main/java/art/arcane/iris/engine/object/IrisObjectPlacementRunner.java @@ -66,6 +66,7 @@ final class IrisObjectPlacementRunner { Objects.requireNonNull(config, "Object placement config is required."); Objects.requireNonNull(rng, "Object placement RNG is required."); Objects.requireNonNull(rdata, "Object placement data is required."); + IObjectPlacer placer = config.getHeightmap() != null ? new HeightmapObjectPlacer(rng, x, yv, z, config, oplacer) : oplacer; boolean evaluateSlopeCondition = !config.isForcePlace() && !config.getSlopeCondition().isDefault(); @@ -75,40 +76,8 @@ final class IrisObjectPlacementRunner { !config.getSlopeCondition().isValid(placementEngine.getComplex().getSlopeStream().get(x, z))) { return -1; } - if (config.isRotateTowardsSlope()) { - int slopeRotationY = 0; - ProceduralStream heightStream = placementEngine.getComplex().getHeightStream(); - // Whichever side of the rectangle that bounds the object is lowest is the 'direction' of the slope (simply said). - double hNorth = heightStream.get(x, z + ((float) self.d) / 2); - double hEast = heightStream.get(x + ((float) self.w) / 2, z); - double hSouth = heightStream.get(x, z - ((float) self.d) / 2); - double hWest = heightStream.get(x - ((float) self.w) / 2, z); - double min = Math.min(Math.min(hNorth, hEast), Math.min(hSouth, hWest)); - if (min == hNorth) { - slopeRotationY = 0; - } else if (min == hEast) { - slopeRotationY = 90; - } else if (min == hSouth) { - slopeRotationY = 180; - } else if (min == hWest) { - slopeRotationY = 270; - } - - double newRotation = config.getRotation().getYAxis().getMin() + slopeRotationY; - IrisObjectRotation originalRotation = config.getRotation(); - IrisObjectRotation slopeRotation = new IrisObjectRotation(); - slopeRotation.setXAxis(originalRotation.getXAxis()); - slopeRotation.setZAxis(originalRotation.getZAxis()); - if (newRotation == 0) { - slopeRotation.setYAxis(new IrisAxisRotationClamp(false, false, 0, 0, 90)); - slopeRotation.setEnabled(originalRotation.canRotateX() || originalRotation.canRotateZ()); - } else { - slopeRotation.setYAxis(new IrisAxisRotationClamp(true, false, newRotation, newRotation, 90)); - slopeRotation.setEnabled(true); - } - config = config.toPlacement(config.getPlace().toArray(new String[0])); - config.setRotation(slopeRotation); + withRotateTowardsSlope(config, rdata.getEngine(), x, z); } } @@ -157,15 +126,17 @@ final class IrisObjectPlacementRunner { TransformedBounds placementBounds = transformedBounds(spin, translating, translateOffset, ceilingHang, warpMargin); boolean bail = false; - if (config.isFromBottom()) { - // todo Convert this to a dedicated mode. - y = (self.getH() + 1) + rty; - if (!config.isForcePlace()) { - if (shouldBailForCarvingAnchor(placer, config, x, y, z)) { - bail = true; - } - } - } else if (yv < 0) { +// if (config.isFromBottom()) { +// // todo Convert this to a dedicated mode. +// y = (self.getH() + 1) + rty; +// if (!config.isForcePlace()) { +// if (shouldBailForCarvingAnchor(placer, config, x, y, z)) { +// bail = true; +// } +// } +// } else + + if (yv < 0) { if (config.getMode().equals(ObjectPlaceMode.CENTER_HEIGHT) || config.getMode() == ObjectPlaceMode.CENTER_STILT || organic || vacuuming) { y = (c != null ? c.getSurface() : placer.getHighest(x, z, self.getLoader(), config.isUnderwater())) + rty; @@ -583,241 +554,6 @@ final class IrisObjectPlacementRunner { } if (stilting) { - self.readLock.lock(); - try { - VectorMap blocks = self.blocks; - IrisStiltSettings settings = config.getStiltSettings(); - - double erodeCentroidX = 0; - double erodeCentroidZ = 0; - double erodeMaxDist = 1; - if (eroding) { - int centroidCount = 0; - VectorMap.Cursor centroidCursor = blocks.cursor(); - while (centroidCursor.next()) { - IrisBlockVector rot = centroidCursor.key().clone(); - spin.rotate(rot); - if (translating) { - rot.add(translateOffset); - } - if (rot.getBlockY() == lowest) { - PlatformBlockState bd = centroidCursor.value(); - if (bd != null && IrisObjectShaping.shouldStilt(bd)) { - erodeCentroidX += rot.getX(); - erodeCentroidZ += rot.getZ(); - centroidCount++; - } - } - } - if (centroidCount > 0) { - erodeCentroidX /= centroidCount; - erodeCentroidZ /= centroidCount; - } - VectorMap.Cursor spreadCursor = blocks.cursor(); - while (spreadCursor.next()) { - IrisBlockVector rot = spreadCursor.key().clone(); - spin.rotate(rot); - if (translating) { - rot.add(translateOffset); - } - if (rot.getBlockY() == lowest) { - PlatformBlockState bd = spreadCursor.value(); - if (bd != null && IrisObjectShaping.shouldStilt(bd)) { - double dx = rot.getX() - erodeCentroidX; - double dz = rot.getZ() - erodeCentroidZ; - double dist = Math.sqrt(dx * dx + dz * dz); - if (dist > erodeMaxDist) { - erodeMaxDist = dist; - } - } - } - } - } - - VectorMap.Cursor stiltCursor = blocks.cursor(); - while (stiltCursor.next()) { - IrisBlockVector g = stiltCursor.key(); - PlatformBlockState sourceData; - try { - sourceData = stiltCursor.value(); - } catch (Throwable e) { - IrisLogging.reportError(e); - IrisLogging.warn("Failed to read block node " + g.getBlockX() + "," + g.getBlockY() + "," + g.getBlockZ() + " in object " + self.getLoadKey() + " (stilt cme)"); - sourceData = IrisObject.States.AIR; - } - - if (sourceData == null) { - IrisLogging.warn("Failed to read block node " + g.getBlockX() + "," + g.getBlockY() + "," + g.getBlockZ() + " in object " + self.getLoadKey() + " (stilt null)"); - sourceData = IrisObject.States.AIR; - } - - if (!IrisObjectShaping.shouldStilt(sourceData)) { - continue; - } - - PlatformBlockState d = sourceData; - if (settings != null && settings.getPalette() != null) { - d = config.getStiltSettings().getPalette().get(rng, x, y, z, rdata); - } else { - String mat = IrisObjectShaping.materialKey(d); - if (mat.equals("minecraft:grass_block") || mat.equals("minecraft:mycelium") || mat.equals("minecraft:podzol") || mat.equals("minecraft:dirt_path")) { - d = B.getState("minecraft:dirt"); - } - } - - IrisBlockVector i = g.clone(); - spin.rotate(i); - if (ceilingHang) { - i.setY(-i.getBlockY()); - } - if (translating) { - i.add(translateOffset); - } - d = config.getRotation().rotate(d, spinx, spiny, spinz); - - int targetLayer = ceilingHang ? topLayer : lowest; - if (i.getBlockY() != targetLayer) - continue; - - if (hasEdits) { - for (IrisObjectReplace j : config.getEdit()) { - if (rng.chance(j.getChance())) { - for (PlatformBlockState k : j.getFind(rdata)) { - if (d == null) { - continue; - } - if (j.isExact() ? k.matches(d) : IrisObjectShaping.materialKey(k).equals(IrisObjectShaping.materialKey(d))) { - PlatformBlockState newData = j.getReplace(rng, i.getX() + x, i.getY() + y, i.getZ() + z, rdata); - - if (IrisObjectShaping.materialKey(newData).equals(IrisObjectShaping.materialKey(d))) { - d = BlockDataMergeSupport.merge(d, newData); - } else { - d = newData; - } - } - } - } - } - } - - if (d == null || !d.isOccluding()) - continue; - - xx = x + (int) Math.round(i.getX()); - zz = z + (int) Math.round(i.getZ()); - - if (warped) { - xx += surfaceWarp.fitDouble(-warpHalf, warpHalf, i.getX() + x, i.getY() + y, i.getZ() + z); - zz += surfaceWarp.fitDouble(-warpHalf, warpHalf, i.getZ() + z, i.getY() + y, i.getX() + x); - } - - if (organic) { - int startY = targetLayer + y; - int maxScan = settings != null ? Math.max(1, settings.getOrganicMaxScan()) : 48; - int jitterMax = settings != null ? Math.max(0, settings.getOrganicJitter()) : 3; - double scratch = settings != null ? Math.max(0, Math.min(1, settings.getOrganicScratch())) : 0.55; - long colHash = ((long) xx * 341873128712L) ^ ((long) zz * 132897987541L); - int jitter = jitterMax > 0 ? (int) (Math.abs(colHash) % (jitterMax + 1)) : 0; - - if (ceilingHang) { - int scan = 0; - int solidY = startY + 1; - while (scan < maxScan && !placer.isSolid(xx, solidY, zz)) { - solidY++; - scan++; - } - int topBound = (scan < maxScan ? solidY - 1 : startY + Math.min(maxScan, 8)) - jitter; - int total = topBound - startY; - for (int j = startY; j <= topBound; j++) { - if (scratch > 0 && total > 0) { - double ratio = (double) (j - startY) / total; - if (ratio > (1.0 - scratch)) { - long sh = ((long) xx * 341873128712L) ^ ((long) j * 132897987541L) ^ ((long) zz * 735791245321L); - double skipChance = (ratio - (1.0 - scratch)) / scratch; - if ((Math.abs(sh) % 1000) / 1000.0 < skipChance * 0.7) { - continue; - } - } - } - placer.set(xx, j, zz, d); - } - } else { - int scan = 0; - int solidY = startY - 1; - while (scan < maxScan && !placer.isSolid(xx, solidY, zz)) { - solidY--; - scan++; - } - int bottomBound = (scan < maxScan ? solidY + 1 : startY - Math.min(maxScan, 8)) + jitter; - int total = startY - bottomBound; - for (int j = startY; j >= bottomBound; j--) { - if (scratch > 0 && total > 0) { - double ratio = (double) (startY - j) / total; - if (ratio > (1.0 - scratch)) { - long sh = ((long) xx * 341873128712L) ^ ((long) j * 132897987541L) ^ ((long) zz * 735791245321L); - double skipChance = (ratio - (1.0 - scratch)) / scratch; - if ((Math.abs(sh) % 1000) / 1000.0 < skipChance * 0.7) { - continue; - } - } - } - placer.set(xx, j, zz, d); - } - } - continue; - } - - int highest = placer.getHighest(xx, zz, self.getLoader(), true); - - if (IrisProceduralBlocks.hasProperty(d, "waterlogged") && shouldAutoWaterlogBlock(placer, config, yv, xx, highest, zz)) { - d = d.withProperty("waterlogged", "true"); - } - - int lowerBound = highest - 1; - if (settings != null) { - lowerBound -= config.getStiltSettings().getOverStilt() - rng.i(0, config.getStiltSettings().getYRand()); - if (settings.getYMax() != 0) - lowerBound -= Math.min(config.getStiltSettings().getYMax() - (lowest + y - highest), 0); - } - - if (eroding) { - double dx = i.getX() - erodeCentroidX; - double dz = i.getZ() - erodeCentroidZ; - double normalizedDist = Math.sqrt(dx * dx + dz * dz) / erodeMaxDist; - normalizedDist = Math.min(normalizedDist, 1.0); - int totalDepth = (lowest + y) - lowerBound; - int erodeDepth = (int) (totalDepth * Math.pow(1.0 - normalizedDist, 1.5)); - lowerBound = (lowest + y) - erodeDepth; - } - - for (int j = lowest + y; j > lowerBound; j--) { - PlatformBlockState fluidState = placer.get(xx, j, zz); - if (B.isFluid(fluidState)) { - break; - } - if (eroding) { - int depth = (lowest + y) - j; - int totalDepth = (lowest + y) - lowerBound; - double depthRatio = totalDepth > 0 ? (double) depth / totalDepth : 0; - if (depthRatio > 0.4) { - long hash = ((long) (xx * 341873128712L) ^ ((long) j * 132897987541L) ^ ((long) zz * 735791245321L)); - double skipChance = (depthRatio - 0.4) / 0.6; - if ((Math.abs(hash) % 1000) / 1000.0 < skipChance * 0.7) { - continue; - } - } - } - - if (B.isVineBlock(d)) { - d = attachVineFaces(placer, d, xx, j, zz); - } - placer.set(xx, j, zz, d); - } - - } - } finally { - self.readLock.unlock(); - } } if (vacuuming && vacuumLowest != Integer.MAX_VALUE && placer.getEngine() != null) { @@ -847,6 +583,42 @@ final class IrisObjectPlacementRunner { return !wouldReplace && (rawStructurePiece || !air); } + private void withRotateTowardsSlope(IrisObjectPlacement config, Engine engine, int x, int z) { + int slopeRotationY = 0; + ProceduralStream heightStream = engine.getComplex().getHeightStream(); + // Whichever side of the rectangle that bounds the object is lowest is the 'direction' of the slope (simply said). + double hNorth = heightStream.get(x, z + ((float) self.d) / 2); + double hEast = heightStream.get(x + ((float) self.w) / 2, z); + double hSouth = heightStream.get(x, z - ((float) self.d) / 2); + double hWest = heightStream.get(x - ((float) self.w) / 2, z); + double min = Math.min(Math.min(hNorth, hEast), Math.min(hSouth, hWest)); + if (min == hNorth) { + slopeRotationY = 0; + } else if (min == hEast) { + slopeRotationY = 90; + } else if (min == hSouth) { + slopeRotationY = 180; + } else if (min == hWest) { + slopeRotationY = 270; + } + + double newRotation = config.getRotation().getYAxis().getMin() + slopeRotationY; + IrisObjectRotation originalRotation = config.getRotation(); + IrisObjectRotation slopeRotation = new IrisObjectRotation(); + slopeRotation.setXAxis(originalRotation.getXAxis()); + slopeRotation.setZAxis(originalRotation.getZAxis()); + if (newRotation == 0) { + slopeRotation.setYAxis(new IrisAxisRotationClamp(false, false, 0, 0, 90)); + slopeRotation.setEnabled(originalRotation.canRotateX() || originalRotation.canRotateZ()); + } else { + slopeRotation.setYAxis(new IrisAxisRotationClamp(true, false, newRotation, newRotation, 90)); + slopeRotation.setEnabled(true); + } + config = config.toPlacement(config.getPlace().toArray(new String[0])); + config.setRotation(slopeRotation); + } + + private TransformedBounds transformedBounds(SpinKernel spin, boolean translating, IrisBlockVector translateOffset, boolean ceilingHang, int margin) { int sourceMinX = -self.getCenter().getBlockX(); @@ -1294,6 +1066,7 @@ final class IrisObjectPlacementRunner { } } + @Deprecated private record TransformedBounds(int minX, int maxX, int minY, int maxY, int minZ, int maxZ) { } diff --git a/core/src/main/java/art/arcane/iris/engine/object/ObjectPlaceMode.java b/core/src/main/java/art/arcane/iris/engine/object/ObjectPlaceMode.java index 1160425a9..5dbe977cd 100644 --- a/core/src/main/java/art/arcane/iris/engine/object/ObjectPlaceMode.java +++ b/core/src/main/java/art/arcane/iris/engine/object/ObjectPlaceMode.java @@ -19,28 +19,29 @@ package art.arcane.iris.engine.object; import art.arcane.iris.engine.object.annotations.Desc; +import art.arcane.iris.engine.object.placer.*; +import lombok.Getter; @Desc("Object Place modes are useful for positioning objects just right. The default value is CENTER_HEIGHT.") public enum ObjectPlaceMode { @Desc("The default place mode. This mode picks a center point (where the center of the object will be) and takes the height. That height is used for the whole object.") - CENTER_HEIGHT, + CENTER_HEIGHT(new PlacementBehaviorCenterHeight()), @Desc("Samples a lot of points where the object will cover (horizontally) and picks the highest height, that height is then used to place the object. This mode is useful for preventing any part of your object from being buried though it will float off of cliffs.") - MAX_HEIGHT, + MAX_HEIGHT(new PlacementBehaviorHeight(PlacementBehaviorHeight.Mode.MAX, false)), @Desc("Samples only 4 points where the object will cover (horizontally) and picks the highest height, that height is then used to place the object. This mode is useful for preventing any part of your object from being buried though it will float off of cliffs.\"") - FAST_MAX_HEIGHT, + FAST_MAX_HEIGHT(new PlacementBehaviorHeight(PlacementBehaviorHeight.Mode.MAX, true)), @Desc("Samples a lot of points where the object will cover (horizontally) and picks the lowest height, that height is then used to place the object. This mode is useful for preventing any part of your object from overhanging a cliff though it gets buried a lot") - - MIN_HEIGHT, + MIN_HEIGHT(new PlacementBehaviorHeight(PlacementBehaviorHeight.Mode.MIN, false)), @Desc("Samples only 4 points where the object will cover (horizontally) and picks the lowest height, that height is then used to place the object. This mode is useful for preventing any part of your object from overhanging a cliff though it gets buried a lot") - FAST_MIN_HEIGHT, + FAST_MIN_HEIGHT(new PlacementBehaviorHeight(PlacementBehaviorHeight.Mode.MIN, true)), @Desc("Stilting is MAX_HEIGHT but it repeats the bottom most block of your object until it hits the surface. This is expensive because it has to first sample every height value for each x,z position of your object. Avoid using this unless its structures for performance reasons.") @@ -96,13 +97,21 @@ public enum ObjectPlaceMode { @Desc("Samples the height of the terrain at every x,z position of your object and pushes it down to the surface. It's pretty much like a melt function over the terrain.") - PAINT, + PAINT(new PlacementBehaviorPaint()), @Desc("Places the object in pure air at an absolute Y driven entirely by translate.y (plus optional translate.yRandom). Terrain height, underwater, and carving anchor checks are skipped. Use this for floating islands, sky structures, clouds, or blimps where the object must not be translated to the ground.") - FLOATING, + FLOATING(new PlacementBehaviorFloating()), @Desc("Raw stamp at the caller-supplied (x, y, z). No terrain sampling, no stilting, no Y recomputation, no underwater or carving anchor guards. Used internally to route native Minecraft structure pieces (villages etc.) through the Iris object placer.") - STRUCTURE_PIECE + STRUCTURE_PIECE; + + @Getter + final PlacementBehavior behavior; + + ObjectPlaceMode(PlacementBehavior behavior) { + this.behavior = behavior; + } + } diff --git a/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehavior.java b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehavior.java new file mode 100644 index 000000000..ba77d6ad5 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehavior.java @@ -0,0 +1,9 @@ +package art.arcane.iris.engine.object.placer; + +import art.arcane.iris.engine.object.IrisObjectPlacement; + +public interface PlacementBehavior { + + int getHeight(PlacementContext context); + +} diff --git a/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorCenterHeight.java b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorCenterHeight.java new file mode 100644 index 000000000..b86fb4124 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorCenterHeight.java @@ -0,0 +1,15 @@ +package art.arcane.iris.engine.object.placer; + +public class PlacementBehaviorCenterHeight implements PlacementBehavior{ + + @Override + public int getHeight(PlacementContext context) { + context.y = (context.caveResult != null ? context.caveResult.getSurface() : context.placer.getHighest(context.x, context.z, context.self.getLoader(), context.config.isUnderwater())) + context.rty; + if (!context.config.isForcePlace()) { + if (context.shouldBailForCarvingAnchor()) { + return -1; + } + } + return context.y; + } +} diff --git a/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorFloating.java b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorFloating.java new file mode 100644 index 000000000..c7e72644e --- /dev/null +++ b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorFloating.java @@ -0,0 +1,10 @@ +package art.arcane.iris.engine.object.placer; + +public class PlacementBehaviorFloating implements PlacementBehavior { + + @Override + public int getHeight(PlacementContext context) { + return context.y = context.rty; + } + +} diff --git a/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorHeight.java b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorHeight.java new file mode 100644 index 000000000..b4087f44d --- /dev/null +++ b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorHeight.java @@ -0,0 +1,73 @@ +package art.arcane.iris.engine.object.placer; + +public class PlacementBehaviorHeight implements PlacementBehavior { + + int minX; + int maxX; + int minZ; + int maxZ; + + private boolean fast; + private Mode mode; + public enum Mode { + MIN, + MAX + } + + public PlacementBehaviorHeight(Mode mode, boolean fast) { + this.fast = fast; + this.mode = mode; + } + + @Override + public int getHeight(PlacementContext context) { + minX = context.x + context.bounds.minX(); + maxX = context.x + context.bounds.maxX(); + minZ = context.z + context.bounds.minZ(); + maxZ = context.z + context.bounds.maxZ(); + + if (fast) { + return computeFastHeight(context); + } else { + return computeHeight(context); + } + } + + private int computeFastHeight(PlacementContext context) { + int xRadius = Math.max(0, (maxX - minX) / 2); + int zRadius = Math.max(0, (maxZ - minZ) / 2); + + for (int i = minX; i <= maxX; i += Math.abs(xRadius) + 1) { + for (int ii = minZ; ii <= maxZ; ii += Math.abs(zRadius) + 1) { + if (columnHeight(context, i, ii)) return -1; + } + } + return context.y; + } + + private int computeHeight(PlacementContext context) { + for (int i = minX; i <= maxX; i++) { + for (int ii = minZ; ii <= maxZ; ii++) { + if (columnHeight(context, i, ii)) return -1; + } + } + return context.y; + } + + private boolean columnHeight(PlacementContext context, int i, int ii) { + int h = context.placer.getHighest(i, ii, context.self.getLoader(), context.config.isUnderwater()) + context.rty; + if (!context.config.isForcePlace()) { + if (context.shouldBailForCarvingAnchor(i, h, ii)) { + return true; + } + } + + if (Mode.MAX == mode && h < context.y) { + context.y = h; + } + if (Mode.MIN == mode && h > context.y) { + context.y = h; + } + return false; + } +} diff --git a/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorPaint.java b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorPaint.java new file mode 100644 index 000000000..f1a249137 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorPaint.java @@ -0,0 +1,16 @@ +package art.arcane.iris.engine.object.placer; + +public class PlacementBehaviorPaint implements PlacementBehavior { + + + @Override + public int getHeight(PlacementContext context) { + context.y = context.placer.getHighest(context.x, context.z, context.self.getLoader(), context.config.isUnderwater()) + context.rty; + if (!context.config.isForcePlace()) { + if (context.shouldBailForCarvingAnchor(context.x, context.y, context.z)) { + return -1; + } + } + return context.y; + } +} diff --git a/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorStilt.java b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorStilt.java new file mode 100644 index 000000000..322006130 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementBehaviorStilt.java @@ -0,0 +1,273 @@ +package art.arcane.iris.engine.object.placer; + +import art.arcane.iris.engine.object.*; +import art.arcane.iris.spi.IrisLogging; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.iris.util.common.data.B; +import art.arcane.iris.util.common.data.VectorMap; +import art.arcane.iris.util.common.math.IrisBlockVector; + +public class PlacementBehaviorStilt implements PlacementBehavior { + + private boolean fast; + private Mode mode; + public enum Mode { + DEFAULT, + CENTER, + MIN, + ERODE, + ORGANIC + } + + public PlacementBehaviorStilt(Mode mode, boolean fast) { + this.mode = mode; + this.fast = fast; + } + + @Override + public int getHeight(PlacementContext context) { + return 0; + } + + + private void stilt(PlacementContext context) { + context.self.readLock.lock(); + try { + VectorMap blocks = context.self.blocks; + IrisStiltSettings settings = context.config.getStiltSettings(); + + double erodeCentroidX = 0; + double erodeCentroidZ = 0; + double erodeMaxDist = 1; + if (eroding) { + int centroidCount = 0; + VectorMap.Cursor centroidCursor = blocks.cursor(); + while (centroidCursor.next()) { + IrisBlockVector rot = centroidCursor.key().clone(); + spin.rotate(rot); + if (translating) { + rot.add(translateOffset); + } + if (rot.getBlockY() == lowest) { + PlatformBlockState bd = centroidCursor.value(); + if (bd != null && IrisObjectShaping.shouldStilt(bd)) { + erodeCentroidX += rot.getX(); + erodeCentroidZ += rot.getZ(); + centroidCount++; + } + } + } + if (centroidCount > 0) { + erodeCentroidX /= centroidCount; + erodeCentroidZ /= centroidCount; + } + VectorMap.Cursor spreadCursor = blocks.cursor(); + while (spreadCursor.next()) { + IrisBlockVector rot = spreadCursor.key().clone(); + spin.rotate(rot); + if (translating) { + rot.add(translateOffset); + } + if (rot.getBlockY() == lowest) { + PlatformBlockState bd = spreadCursor.value(); + if (bd != null && IrisObjectShaping.shouldStilt(bd)) { + double dx = rot.getX() - erodeCentroidX; + double dz = rot.getZ() - erodeCentroidZ; + double dist = Math.sqrt(dx * dx + dz * dz); + if (dist > erodeMaxDist) { + erodeMaxDist = dist; + } + } + } + } + } + + VectorMap.Cursor stiltCursor = blocks.cursor(); + while (stiltCursor.next()) { + IrisBlockVector g = stiltCursor.key(); + PlatformBlockState sourceData; + try { + sourceData = stiltCursor.value(); + } catch (Throwable e) { + IrisLogging.reportError(e); + IrisLogging.warn("Failed to read block node " + g.getBlockX() + "," + g.getBlockY() + "," + g.getBlockZ() + " in object " + self.getLoadKey() + " (stilt cme)"); + sourceData = IrisObject.States.AIR; + } + + if (sourceData == null) { + IrisLogging.warn("Failed to read block node " + g.getBlockX() + "," + g.getBlockY() + "," + g.getBlockZ() + " in object " + self.getLoadKey() + " (stilt null)"); + sourceData = IrisObject.States.AIR; + } + + if (!IrisObjectShaping.shouldStilt(sourceData)) { + continue; + } + + PlatformBlockState d = sourceData; + if (settings != null && settings.getPalette() != null) { + d = context.config.getStiltSettings().getPalette().get(rng, context.x, context.y, context.z, rdata); + } else { + String mat = IrisObjectShaping.materialKey(d); + if (mat.equals("minecraft:grass_block") || mat.equals("minecraft:mycelium") || mat.equals("minecraft:podzol") || mat.equals("minecraft:dirt_path")) { + d = B.getState("minecraft:dirt"); + } + } + + IrisBlockVector i = g.clone(); + spin.rotate(i); + if (ceilingHang) { + i.setY(-i.getBlockY()); + } + if (translating) { + i.add(translateOffset); + } + d = config.getRotation().rotate(d, spinx, spiny, spinz); + + int targetLayer = ceilingHang ? topLayer : lowest; + if (i.getBlockY() != targetLayer) + continue; + + if (hasEdits) { + for (IrisObjectReplace j : context.config.getEdit()) { + if (rng.chance(j.getChance())) { + for (PlatformBlockState k : j.getFind(rdata)) { + if (d == null) { + continue; + } + if (j.isExact() ? k.matches(d) : IrisObjectShaping.materialKey(k).equals(IrisObjectShaping.materialKey(d))) { + PlatformBlockState newData = j.getReplace(rng, i.getX() + context.x, i.getY() + y, i.getZ() + context.z, rdata); + + if (IrisObjectShaping.materialKey(newData).equals(IrisObjectShaping.materialKey(d))) { + d = BlockDataMergeSupport.merge(d, newData); + } else { + d = newData; + } + } + } + } + } + } + + if (d == null || !d.isOccluding()) + continue; + + xx = context.x + (int) Math.round(i.getX()); + zz = context.z + (int) Math.round(i.getZ()); + + if (warped) { + xx += surfaceWarp.fitDouble(-warpHalf, warpHalf, i.getX() + context.x, i.getY() + context.y, i.getZ() + context.z); + zz += surfaceWarp.fitDouble(-warpHalf, warpHalf, i.getZ() + context.z, i.getY() + context.y, i.getX() + context.x); + } + + if (organic) { + int startY = targetLayer + y; + int maxScan = settings != null ? Math.max(1, settings.getOrganicMaxScan()) : 48; + int jitterMax = settings != null ? Math.max(0, settings.getOrganicJitter()) : 3; + double scratch = settings != null ? Math.max(0, Math.min(1, settings.getOrganicScratch())) : 0.55; + long colHash = ((long) xx * 341873128712L) ^ ((long) zz * 132897987541L); + int jitter = jitterMax > 0 ? (int) (Math.abs(colHash) % (jitterMax + 1)) : 0; + + if (ceilingHang) { + int scan = 0; + int solidY = startY + 1; + while (scan < maxScan && !placer.isSolid(xx, solidY, zz)) { + solidY++; + scan++; + } + int topBound = (scan < maxScan ? solidY - 1 : startY + Math.min(maxScan, 8)) - jitter; + int total = topBound - startY; + for (int j = startY; j <= topBound; j++) { + if (scratch > 0 && total > 0) { + double ratio = (double) (j - startY) / total; + if (ratio > (1.0 - scratch)) { + long sh = ((long) xx * 341873128712L) ^ ((long) j * 132897987541L) ^ ((long) zz * 735791245321L); + double skipChance = (ratio - (1.0 - scratch)) / scratch; + if ((Math.abs(sh) % 1000) / 1000.0 < skipChance * 0.7) { + continue; + } + } + } + context.placer.set(xx, j, zz, d); + } + } else { + int scan = 0; + int solidY = startY - 1; + while (scan < maxScan && !context.placer.isSolid(xx, solidY, zz)) { + solidY--; + scan++; + } + int bottomBound = (scan < maxScan ? solidY + 1 : startY - Math.min(maxScan, 8)) + jitter; + int total = startY - bottomBound; + for (int j = startY; j >= bottomBound; j--) { + if (scratch > 0 && total > 0) { + double ratio = (double) (startY - j) / total; + if (ratio > (1.0 - scratch)) { + long sh = ((long) xx * 341873128712L) ^ ((long) j * 132897987541L) ^ ((long) zz * 735791245321L); + double skipChance = (ratio - (1.0 - scratch)) / scratch; + if ((Math.abs(sh) % 1000) / 1000.0 < skipChance * 0.7) { + continue; + } + } + } + context.placer.set(xx, j, zz, d); + } + } + continue; + } + + int highest = context.placer.getHighest(xx, zz, self.getLoader(), true); + + if (IrisProceduralBlocks.hasProperty(d, "waterlogged") && shouldAutoWaterlogBlock(placer, config, yv, xx, highest, zz)) { + d = d.withProperty("waterlogged", "true"); + } + + int lowerBound = highest - 1; + if (settings != null) { + lowerBound -= context.config.getStiltSettings().getOverStilt() - rng.i(0, context.config.getStiltSettings().getYRand()); + if (settings.getYMax() != 0) + lowerBound -= Math.min(context.config.getStiltSettings().getYMax() - (lowest + y - highest), 0); + } + + if (eroding) { + double dx = i.getX() - erodeCentroidX; + double dz = i.getZ() - erodeCentroidZ; + double normalizedDist = Math.sqrt(dx * dx + dz * dz) / erodeMaxDist; + normalizedDist = Math.min(normalizedDist, 1.0); + int totalDepth = (lowest + y) - lowerBound; + int erodeDepth = (int) (totalDepth * Math.pow(1.0 - normalizedDist, 1.5)); + lowerBound = (lowest + y) - erodeDepth; + } + + for (int j = lowest + y; j > lowerBound; j--) { + PlatformBlockState fluidState = context.placer.get(xx, j, zz); + if (B.isFluid(fluidState)) { + break; + } + if (eroding) { + int depth = (lowest + context.y) - j; + int totalDepth = (lowest + context.y) - lowerBound; + double depthRatio = totalDepth > 0 ? (double) depth / totalDepth : 0; + if (depthRatio > 0.4) { + long hash = ((long) (xx * 341873128712L) ^ ((long) j * 132897987541L) ^ ((long) zz * 735791245321L)); + double skipChance = (depthRatio - 0.4) / 0.6; + if ((Math.abs(hash) % 1000) / 1000.0 < skipChance * 0.7) { + continue; + } + } + } + + if (B.isVineBlock(d)) { + d = attachVineFaces(placer, d, xx, j, zz); + } + context.placer.set(xx, j, zz, d); + } + + } + } finally { + context.self.readLock.unlock(); + } + + } + + +} diff --git a/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementContext.java b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementContext.java new file mode 100644 index 000000000..8a7e57279 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/engine/object/placer/PlacementContext.java @@ -0,0 +1,50 @@ +package art.arcane.iris.engine.object.placer; + + +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.engine.object.*; +import art.arcane.volmlib.util.math.RNG; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public final class PlacementContext { + public record TransformedBounds(int minX, int maxX, int minY, int maxY, int minZ, int maxZ) { + } + + IrisObjectPlacement config; + final IObjectPlacer placer; + final CarveResult caveResult; + final RNG rng; + IrisObject self; + TransformedBounds bounds; + + int x; + int z; + int y; + int rty; + + + public boolean shouldBailForCarvingAnchor() { + return shouldBailForCarvingAnchor(x,y,z); + } + + public boolean shouldBailForCarvingAnchor(int x, int y, int z) { + CarvingMode carvingMode = config.getCarvingSupport(); + return switch (carvingMode) { + case SURFACE_ONLY -> placer.isCarved(x, y, z); + case CARVING_ONLY -> !isCarvedCaveAnchor(x, y, z); + case ANYWHERE -> false; + }; + } + + public boolean isCarvedCaveAnchor() { + return isCarvedCaveAnchor(x, y, z); + } + + public boolean isCarvedCaveAnchor(int x, int y, int z) { + return placer.isCarved(x, y, z) + || placer.isCarved(x, y - 1, z) + || placer.isCarved(x, y - 2, z) + || placer.isCarved(x, y - 3, z); + } +} \ No newline at end of file