9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-22 16:29:16 +00:00
Files
SakuraMC/patches/server/0015-Optimise-paper-explosions.patch
2024-07-15 13:28:07 +01:00

191 lines
9.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <kfian294ma4@gmail.com>
Date: Fri, 19 Apr 2024 22:20:03 +0100
Subject: [PATCH] Optimise paper explosions
diff --git a/src/main/java/net/minecraft/world/level/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java
index 798b840af2dc5f58e6df55426a1802d79ff5d6cb..94636b235eac944b955944425b44b9e864a319d0 100644
--- a/src/main/java/net/minecraft/world/level/Explosion.java
+++ b/src/main/java/net/minecraft/world/level/Explosion.java
@@ -100,7 +100,7 @@ public class Explosion {
}
}
- CACHED_RAYS = rayCoords.toDoubleArray();
+ CACHED_RAYS = sortExplosionRays(rayCoords); // Sakura - optimise paper explosions
}
private static final int CHUNK_CACHE_SHIFT = 2;
@@ -315,6 +315,39 @@ public class Explosion {
return (float)missedRays / (float)totalRays;
}
// Paper end - optimise collisions
+ // Sakura start - optimise paper explosions
+ /*
+ * Sort the explosion rays to better utilise the chunk and block cache.
+ * x + Vanilla Sorted
+ * z @ z 8 5
+ * - x 6 7 6 4
+ * 4 @ 5 7 @ 3
+ * 2 3 8 2
+ * 1 1
+ */
+ private static double[] sortExplosionRays(final it.unimi.dsi.fastutil.doubles.DoubleArrayList rayCoords) {
+ List<double[]> explosionRays = new ArrayList<>();
+
+ for (int i = 0; i < rayCoords.size(); i += 3) {
+ double[] ray = new double[3];
+ rayCoords.getElements(i, ray, 0, 3);
+ explosionRays.add(ray);
+ }
+
+ rayCoords.clear();
+ explosionRays.sort(java.util.Comparator.comparingDouble(vec -> {
+ double sign = Math.signum(vec[0]);
+ double dir = (sign - 1) / 2;
+ return sign + 8 + vec[2] * dir;
+ }));
+
+ double[] rays = new double[explosionRays.size() * 3];
+ for (int i = 0; i < explosionRays.size() * 3; i++) {
+ rays[i] = explosionRays.get(i / 3)[i % 3];
+ }
+ return rays;
+ }
+ // Sakura end - optimise paper explosions
public static DamageSource getDefaultDamageSource(Level world, @Nullable Entity source) {
return world.damageSources().explosion(source, Explosion.getIndirectSourceEntityInternal(source));
@@ -438,6 +471,16 @@ public class Explosion {
int j;
// Paper start - collision optimisations
+ // Sakura start - optimise paper explosions
+ if (initialCache.resistance <= (this.radius * 1.3f) && this.interactsWithBlocks()) {
+ this.searchForBlocks(blockCache, initialCache);
+ }
+ this.locateAndImpactEntities(blockCache);
+ this.clearBlockCache();
+ }
+
+ protected final void searchForBlocks(ca.spottedleaf.moonrise.patches.collisions.ExplosionBlockCache[] blockCache, final ca.spottedleaf.moonrise.patches.collisions.ExplosionBlockCache initialCache) {
+ // Sakura end - optimise paper explosions
for (int ray = 0, len = CACHED_RAYS.length; ray < len;) {
ca.spottedleaf.moonrise.patches.collisions.ExplosionBlockCache cachedBlock = initialCache;
@@ -509,25 +552,64 @@ public class Explosion {
}
// Paper - optimise collisions
- float f2 = this.radius * 2.0F;
+ // Sakura start - optimise paper explosions
+ }
- i = Mth.floor(this.x - (double) f2 - 1.0D);
- j = Mth.floor(this.x + (double) f2 + 1.0D);
+ protected final AABB getExplosionBounds(float f2) {
+ // Sakura - moved into locateAndImpactEntities
+
+ int i = Mth.floor(this.x - (double) f2 - 1.0D);
+ int j = Mth.floor(this.x + (double) f2 + 1.0D);
int l = Mth.floor(this.y - (double) f2 - 1.0D);
int i1 = Mth.floor(this.y + (double) f2 + 1.0D);
int j1 = Mth.floor(this.z - (double) f2 - 1.0D);
int k1 = Mth.floor(this.z + (double) f2 + 1.0D);
- List<Entity> list = this.level.getEntities(this.source, new AABB((double) i, (double) l, (double) j1, (double) j, (double) i1, (double) k1), (com.google.common.base.Predicate<Entity>) entity -> entity.isAlive() && !entity.isSpectator()); // Paper - Fix lag from explosions processing dead entities
+ return new AABB((double) i, (double) l, (double) j1, (double) j, (double) i1, (double) k1);
+ }
+
+ protected final void locateAndImpactEntities(ca.spottedleaf.moonrise.patches.collisions.ExplosionBlockCache[] blockCache) {
+ float f2 = this.radius * 2.0F;
Vec3 vec3d = new Vec3(this.x, this.y, this.z);
- Iterator iterator = list.iterator();
// Paper start - optimise collisions
final BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
// Paper end - optimise collisions
- while (iterator.hasNext()) {
- Entity entity = (Entity) iterator.next();
+ int minSection = io.papermc.paper.util.WorldUtil.getMinSection(this.level);
+ int maxSection = io.papermc.paper.util.WorldUtil.getMaxSection(this.level);
+
+ int minChunkX = Mth.floor(this.x - f2) >> 4;
+ int maxChunkX = Mth.floor(this.x + f2) >> 4;
+ int minChunkY = Mth.clamp(Mth.floor(this.y - f2) >> 4, minSection, maxSection);
+ int maxChunkY = Mth.clamp(Mth.floor(this.y + f2) >> 4, minSection, maxSection);
+ int minChunkZ = Mth.floor(this.z - f2) >> 4;
+ int maxChunkZ = Mth.floor(this.z + f2) >> 4;
+ ca.spottedleaf.moonrise.patches.chunk_system.level.entity.EntityLookup entityLookup = this.level.moonrise$getEntityLookup();
+ for (int chunkX = minChunkX; chunkX <= maxChunkX; ++chunkX) {
+ for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; ++chunkZ) {
+ ca.spottedleaf.moonrise.patches.chunk_system.level.entity.ChunkEntitySlices chunk = entityLookup.getChunk(chunkX, chunkZ);
+ if (chunk == null) continue; // empty slice
+
+ for (int chunkY = minChunkY; chunkY <= maxChunkY; ++chunkY) {
+ this.impactEntities(blockCache, blockPos, f2, vec3d, chunk.getSectionEntities(chunkY));
+ }
+ }
+ }
+ }
+
+ protected final void impactEntities(ca.spottedleaf.moonrise.patches.collisions.ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos, float f2, Vec3 vec3d, Entity[] entities) {
+ for (int i = 0; i < entities.length; i++) {
+ Entity entity = entities[i];
+ if (entity == null) break; // end of entity section
+ this.impactEntity(blockCache, blockPos, f2, vec3d, entity);
+ if (entity != entities[i]) i--; // entities can be removed mid-explosion
+ }
+ }
+
+ protected final void impactEntity(ca.spottedleaf.moonrise.patches.collisions.ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos, float f2, Vec3 vec3d, Entity entity) {
+ if (entity.isAlive() && !entity.isSpectator()) { // Paper - Fix lag from explosions processing dead entities
+ // Sakura end - optimise paper explosions
if (!entity.ignoreExplosion(this)) {
double d7 = Math.sqrt(entity.distanceToSqr(vec3d)) / (double) f2;
@@ -552,7 +634,7 @@ public class Explosion {
// - Damaging EntityEnderDragon does nothing
// - EntityEnderDragon hitbock always covers the other parts and is therefore always present
if (entity instanceof EnderDragonPart) {
- continue;
+ return; // Sakura - optimise paper explosions
}
entity.lastDamageCancelled = false;
@@ -560,9 +642,10 @@ public class Explosion {
seenFraction = (double)this.getBlockDensity(vec3d, entity, blockCache, blockPos); // Paper - optimise collisions
if (entity instanceof EnderDragon) {
+ AABB bounds = this.getExplosionBounds(f2); // Sakura - optimise paper explosions
for (EnderDragonPart entityComplexPart : ((EnderDragon) entity).subEntities) {
// Calculate damage separately for each EntityComplexPart
- if (list.contains(entityComplexPart)) {
+ if (entityComplexPart.getBoundingBox().intersects(bounds)) { // Sakura - optimise paper explosions
entityComplexPart.hurt(this.damageSource, this.damageCalculator.getEntityDamageAmount(this, entity));
}
}
@@ -576,7 +659,7 @@ public class Explosion {
}
if (entity.lastDamageCancelled) { // SPIGOT-5339, SPIGOT-6252, SPIGOT-6777: Skip entity if damage event was cancelled
- continue;
+ return; // Sakura - optimise paper explosions
}
// CraftBukkit end
} else { seenFraction = (double)this.getBlockDensity(vec3d, entity, blockCache, blockPos); } // Paper - optimise collisions
@@ -625,6 +708,11 @@ public class Explosion {
}
}
}
+ // Sakura start - optimise paper explosions
+ }
+
+ protected void clearBlockCache() {
+ // Sakura end - optimise paper explosions
// Paper start - optimise collisions
this.blockCache = null;
this.chunkPosCache = null;