Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class DespawnRange {
private double preComputedHorizontalLimitSquared; // ac^2
private double preComputedHorizontalLimitSquaredOverVerticalLimitSquared; // ac^2/b^2
private int preComputedVanillaDefaultLimit;
private double preComputedVanillaDefaultLimitSquared;

public DespawnRange(final IntOr.Default generalLimit) {
this(generalLimit, generalLimit, false);
Expand All @@ -47,6 +48,7 @@ public void preComputed(int defaultDistanceLimit, String identifier) throws Seri
throw new SerializationException("Horizontal limit must be greater than 0 for " + identifier);
}
this.preComputedVanillaDefaultLimit = defaultDistanceLimit;
this.preComputedVanillaDefaultLimitSquared = (double) defaultDistanceLimit * defaultDistanceLimit;
this.preComputedHorizontalLimitSquared = Math.pow(this.horizontalLimit.or(defaultDistanceLimit), 2);
if (!this.horizontalLimit.isDefined() && !this.verticalLimit.isDefined()) {
this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared = 1.0;
Expand All @@ -56,19 +58,21 @@ public void preComputed(int defaultDistanceLimit, String identifier) throws Seri
}

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);
}
final double horizontalDistanceSquared = dxSqr + dzSqr;
final double horizontalLimitSquared = this.preComputedHorizontalLimitSquared;
return switch (shape) {
case ELLIPSOID -> horizontalDistanceSquared + dySqr * this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared > horizontalLimitSquared;
case CYLINDER -> horizontalDistanceSquared > horizontalLimitSquared || dy > this.verticalLimit.or(this.preComputedVanillaDefaultLimit);
case HYBRID -> dxSqr > horizontalLimitSquared || dzSqr > horizontalLimitSquared || dy > this.verticalLimit.or(this.preComputedVanillaDefaultLimit) || horizontalDistanceSquared + dySqr > this.preComputedVanillaDefaultLimitSquared;
};
}

public boolean wasDefinedViaLongSyntax() {
return this.wasDefinedViaLongSyntax;
}

public enum Shape {
CYLINDER, ELLIPSOID
CYLINDER, ELLIPSOID, HYBRID
}

static final class Serializer implements TypeSerializer<DespawnRange> {
Expand Down