From 43f2d628d5d1ac7222f29487ae157834f595c32f Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sun, 25 Aug 2024 11:26:33 -0700 Subject: [PATCH] Fix despawn ranges by defaulting to an ellipsoid shape (#11312) --- ...onfigurable-entity-despawn-distances.patch | 21 ++--- patches/server/Paper-config-files.patch | 87 ++++++++++++++++--- 2 files changed, 86 insertions(+), 22 deletions(-) diff --git a/patches/server/Add-configurable-entity-despawn-distances.patch b/patches/server/Add-configurable-entity-despawn-distances.patch index 36c1d816cf..15401f5913 100644 --- a/patches/server/Add-configurable-entity-despawn-distances.patch +++ b/patches/server/Add-configurable-entity-despawn-distances.patch @@ -18,18 +18,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - - if (d0 > (double) j && this.removeWhenFarAway(d0)) { + // Paper start - Configurable despawn distances -+ // Read configration data and square it for later comparison -+ final MobCategory category = this.getType().getCategory(); + final io.papermc.paper.configuration.WorldConfiguration.Entities.Spawning.DespawnRangePair despawnRangePair = this.level().paperConfig().entities.spawning.despawnRanges.get(this.getType().getCategory()); -+ final int hardHorizontalLimitSquared = (int) Math.pow(despawnRangePair.hard().horizontalLimit().or(category.getDespawnDistance()), 2); -+ final int softHorizontalLimitSquared = (int) Math.pow(despawnRangePair.soft().horizontalLimit().or(category.getNoDespawnDistance()), 2); -+ final int hardVerticalLimit = despawnRangePair.hard().verticalLimit().or(category.getDespawnDistance()); -+ final int softVerticalLimit = despawnRangePair.soft().verticalLimit().or(category.getNoDespawnDistance()); -+ // Compute vertical/horizontal distances -+ final double horizontalDistanceSquared = entityhuman.distanceToSqr(this.getX(), entityhuman.getY(), this.getZ()); -+ final double verticalDistance = Math.abs(entityhuman.getY() - this.getY()); ++ final io.papermc.paper.configuration.type.DespawnRange.Shape shape = this.level().paperConfig().entities.spawning.despawnRangeShape; ++ final double dy = Math.abs(entityhuman.getY() - this.getY()); ++ final double dySqr = Math.pow(dy, 2); ++ final double dxSqr = Math.pow(entityhuman.getX() - this.getX(), 2); ++ final double dzSqr = Math.pow(entityhuman.getZ() - this.getZ(), 2); ++ final double distanceSquared = dxSqr + dzSqr + dySqr; + // Despawn if hard/soft limit is exceeded -+ if ((horizontalDistanceSquared > hardHorizontalLimitSquared || verticalDistance > hardVerticalLimit) && this.removeWhenFarAway(horizontalDistanceSquared)) { ++ if (despawnRangePair.hard().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy) && this.removeWhenFarAway(distanceSquared)) { this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause } - @@ -39,8 +36,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && d0 > (double) l && this.removeWhenFarAway(d0)) { - this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause - } else if (d0 < (double) l) { -+ if (horizontalDistanceSquared > softHorizontalLimitSquared || verticalDistance > softVerticalLimit) { -+ if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && this.removeWhenFarAway(horizontalDistanceSquared)) { ++ if (despawnRangePair.soft().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy)) { ++ if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && this.removeWhenFarAway(distanceSquared)) { + this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause + } + } else { diff --git a/patches/server/Paper-config-files.patch b/patches/server/Paper-config-files.patch index fd2677be57..90a92bdcd2 100644 --- a/patches/server/Paper-config-files.patch +++ b/patches/server/Paper-config-files.patch @@ -1483,6 +1483,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import org.spongepowered.configurate.objectmapping.meta.PostProcess; +import org.spongepowered.configurate.objectmapping.meta.Required; +import org.spongepowered.configurate.objectmapping.meta.Setting; ++import org.spongepowered.configurate.serialize.SerializationException; + +@SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal", "NotNullFieldNotInitialized", "InnerClassMayBeStatic"}) +public class WorldConfiguration extends ConfigurationPart { @@ -1609,6 +1610,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + public Reference2IntMap spawnLimits = Util.make(new Reference2IntOpenHashMap<>(NaturalSpawner.SPAWNING_CATEGORIES.length), map -> Arrays.stream(NaturalSpawner.SPAWNING_CATEGORIES).forEach(mobCategory -> map.put(mobCategory, -1))); + @MergeMap + public Map despawnRanges = Arrays.stream(MobCategory.values()).collect(Collectors.toMap(Function.identity(), category -> DespawnRangePair.createDefault())); ++ public DespawnRange.Shape despawnRangeShape = DespawnRange.Shape.ELLIPSOID; + @MergeMap + public Reference2IntMap ticksPerSpawn = Util.make(new Reference2IntOpenHashMap<>(NaturalSpawner.SPAWNING_CATEGORIES.length), map -> Arrays.stream(NaturalSpawner.SPAWNING_CATEGORIES).forEach(mobCategory -> map.put(mobCategory, -1))); + @@ -1622,6 +1624,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + } + ++ @PostProcess ++ public void precomputeDespawnDistances() throws SerializationException { ++ for (Map.Entry entry : this.despawnRanges.entrySet()) { ++ final MobCategory category = entry.getKey(); ++ final DespawnRangePair range = entry.getValue(); ++ range.hard().preComputed(category.getDespawnDistance(), category.getSerializedName()); ++ range.soft().preComputed(category.getNoDespawnDistance(), category.getSerializedName()); ++ } ++ } ++ + public WaterAnimalSpawnHeight wateranimalSpawnHeight; + + public class WaterAnimalSpawnHeight extends ConfigurationPart { @@ -4213,21 +4225,72 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import java.lang.reflect.Type; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.spongepowered.configurate.ConfigurationNode; -+import org.spongepowered.configurate.objectmapping.meta.Required; +import org.spongepowered.configurate.serialize.SerializationException; +import org.spongepowered.configurate.serialize.TypeSerializer; + -+public record DespawnRange( -+ @Required IntOr.Default horizontalLimit, -+ @Required IntOr.Default verticalLimit, -+ @Required boolean wasDefinedViaLongSyntax -+) { ++/* ++(x/a)^2 + (y/b)^2 + (z/c)^2 < 1 ++a == c ++ac = horizontal limit ++b = vertical limit ++x^2/ac^2 + y^2/b^2 + z^2/ac^2 < 1 ++(x^2 + z^2)/ac^2 + y^2/b^2 < 1 ++x^2 + z^2 + (y^2 * (ac^2/b^2)) < ac^2 ++ */ ++public final class DespawnRange { ++ ++ public static final TypeSerializer SERIALIZER = new Serializer(); ++ ++ private final IntOr.Default horizontalLimit; ++ private final IntOr.Default verticalLimit; ++ private final boolean wasDefinedViaLongSyntax; ++ ++ // cached values ++ private double preComputedHorizontalLimitSquared; // ac^2 ++ private double preComputedHorizontalLimitSquaredOverVerticalLimitSquared; // ac^2/b^2 ++ private int preComputedVanillaDefaultLimit; + + public DespawnRange(final IntOr.Default generalLimit) { + this(generalLimit, generalLimit, false); + } + -+ public static final TypeSerializer SERIALIZER = new Serializer(); ++ public DespawnRange(final IntOr.Default horizontalLimit, final IntOr.Default verticalLimit, final boolean wasDefinedViaLongSyntax) { ++ this.horizontalLimit = horizontalLimit; ++ this.verticalLimit = verticalLimit; ++ this.wasDefinedViaLongSyntax = wasDefinedViaLongSyntax; ++ } ++ ++ public void preComputed(int defaultDistanceLimit, String identifier) throws SerializationException { ++ if (this.verticalLimit.or(defaultDistanceLimit) <= 0) { ++ throw new SerializationException("Vertical limit must be greater than 0 for " + identifier); ++ } ++ if (this.horizontalLimit.or(defaultDistanceLimit) <= 0) { ++ throw new SerializationException("Horizontal limit must be greater than 0 for " + identifier); ++ } ++ this.preComputedVanillaDefaultLimit = defaultDistanceLimit; ++ this.preComputedHorizontalLimitSquared = Math.pow(this.horizontalLimit.or(defaultDistanceLimit), 2); ++ if (!this.horizontalLimit.isDefined() && !this.verticalLimit.isDefined()) { ++ this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared = 1.0; ++ } else { ++ this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared = this.preComputedHorizontalLimitSquared / Math.pow(this.verticalLimit.or(defaultDistanceLimit), 2); ++ } ++ } ++ ++ public boolean shouldDespawn(final Shape shape, final double dxSqr, final double dySqr, final double dzSqr, final double dy) { ++ if (shape == Shape.ELLIPSOID) { ++ return dxSqr + dzSqr + (dySqr * this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared) > this.preComputedHorizontalLimitSquared; ++ } else { ++ return dxSqr + dzSqr > this.preComputedHorizontalLimitSquared || dy > this.verticalLimit.or(this.preComputedVanillaDefaultLimit); ++ } ++ } ++ ++ public boolean wasDefinedViaLongSyntax() { ++ return this.wasDefinedViaLongSyntax; ++ } ++ ++ public enum Shape { ++ CYLINDER, ELLIPSOID ++ } + + static final class Serializer implements TypeSerializer { + @@ -4257,10 +4320,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + if (despawnRange.wasDefinedViaLongSyntax()) { -+ node.node(HORIZONTAL).set(despawnRange.horizontalLimit()); -+ node.node(VERTICAL).set(despawnRange.verticalLimit()); ++ node.node(HORIZONTAL).set(despawnRange.horizontalLimit); ++ node.node(VERTICAL).set(despawnRange.verticalLimit); + } else { -+ node.set(despawnRange.verticalLimit()); ++ node.set(despawnRange.verticalLimit); + } + } + } @@ -4854,6 +4917,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + OptionalInt value(); + ++ default boolean isDefined() { ++ return this.value().isPresent(); ++ } ++ + default int intValue() { + return this.value().orElseThrow(); + }