Fix despawn ranges by defaulting to an ellipsoid shape (#11312)

This commit is contained in:
Jake Potrebic 2024-08-25 11:26:33 -07:00
parent 4a0660bd59
commit 43f2d628d5
2 changed files with 86 additions and 22 deletions

View file

@ -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 {

View file

@ -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<MobCategory> spawnLimits = Util.make(new Reference2IntOpenHashMap<>(NaturalSpawner.SPAWNING_CATEGORIES.length), map -> Arrays.stream(NaturalSpawner.SPAWNING_CATEGORIES).forEach(mobCategory -> map.put(mobCategory, -1)));
+ @MergeMap
+ public Map<MobCategory, DespawnRangePair> despawnRanges = Arrays.stream(MobCategory.values()).collect(Collectors.toMap(Function.identity(), category -> DespawnRangePair.createDefault()));
+ public DespawnRange.Shape despawnRangeShape = DespawnRange.Shape.ELLIPSOID;
+ @MergeMap
+ public Reference2IntMap<MobCategory> 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<MobCategory, DespawnRangePair> 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<DespawnRange> 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<DespawnRange> 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<DespawnRange> {
+
@ -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();
+ }