From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samsuik Date: Fri, 19 Apr 2024 22:20:03 +0100 Subject: [PATCH] Optimise paper explosions diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java index ba20e87d2105ce53cdaf4049de2388d05fcd1b56..6e3a429bc165d8473ea50ee2ae1548270db599d1 100644 --- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java +++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java @@ -297,6 +297,12 @@ public final class ChunkEntitySlices { return true; } + // Sakura start - optimise paper explosions; expose slice entity list + public Entity[] getSectionEntities(int sectionY) { + return this.allEntities.getSectionEntities(sectionY); + } + // Sakura end - optimise paper explosions; expose slice entity list + public void getHardCollidingEntities(final Entity except, final AABB box, final List into, final Predicate predicate) { this.hardCollidingEntities.getEntities(except, box, into, predicate); } @@ -495,6 +501,17 @@ public final class ChunkEntitySlices { } } + // Sakura start - optimise paper explosions; expose slice entity list + public Entity[] getSectionEntities(int sectionY) { + final BasicEntityList list = this.entitiesBySection[sectionY - this.slices.minSection]; + if (list != null) { + return list.storage; + } else { + return new Entity[0]; + } + } + // Sakura end - optimise paper explosions; expose slice entity list + public void getEntities(final Entity except, final AABB box, final List into, final Predicate predicate) { if (this.count == 0) { return; diff --git a/net/minecraft/world/level/ServerExplosion.java b/net/minecraft/world/level/ServerExplosion.java index 340d50be3cc94e47affddd4ad916e1c02e620636..5c029776df58976df6791f66618c97980fbfa4dc 100644 --- a/net/minecraft/world/level/ServerExplosion.java +++ b/net/minecraft/world/level/ServerExplosion.java @@ -86,7 +86,7 @@ public class ServerExplosion implements Explosion { } } - CACHED_RAYS = rayCoords.toDoubleArray(); + CACHED_RAYS = sortExplosionRays(rayCoords); // Sakura - optimise paper explosions } private static final int CHUNK_CACHE_SHIFT = 2; @@ -304,6 +304,39 @@ public class ServerExplosion implements Explosion { return (float)missedRays / (float)totalRays; } // Paper end - collisions optimisations + // 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(it.unimi.dsi.fastutil.doubles.DoubleArrayList rayCoords) { + List 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 ServerExplosion( ServerLevel level, @@ -395,6 +428,12 @@ public class ServerExplosion implements Explosion { initialCache = this.getOrCacheExplosionBlock(blockX, blockY, blockZ, key, true); } + // Sakura start - optimise paper explosions + if (!this.interactsWithBlocks() || initialCache.resistance > (this.radius * 1.3f)) { + return ret; + } + // Sakura end - optimise paper explosions + // only ~1/3rd of the loop iterations in vanilla will result in a ray, as it is iterating the perimeter of // a 16x16x16 cube // we can cache the rays and their normals as well, so that we eliminate the excess iterations / checks and @@ -473,16 +512,55 @@ public class ServerExplosion implements Explosion { // Paper end - collision optimisations } - private void hurtEntities() { - float f = this.radius * 2.0F; + // Sakura start - optimise paper explosions + protected final AABB getExplosionBounds(float f) { int floor = Mth.floor(this.center.x - f - 1.0); int floor1 = Mth.floor(this.center.x + f + 1.0); int floor2 = Mth.floor(this.center.y - f - 1.0); int floor3 = Mth.floor(this.center.y + f + 1.0); int floor4 = Mth.floor(this.center.z - f - 1.0); int floor5 = Mth.floor(this.center.z + f + 1.0); - List list = this.level.getEntities(this.excludeSourceFromDamage ? this.source : null, new AABB(floor, floor2, floor4, floor1, floor3, floor5), entity -> entity.isAlive() && !entity.isSpectator()); // Paper - Fix lag from explosions processing dead entities, Allow explosions to damage source - for (Entity entity : list) { // Paper - used in loop + return new AABB(floor, floor2, floor4, floor1, floor3, floor5); + } + + private void hurtEntities() { + float f = this.radius * 2.0F; + + int minSection = ca.spottedleaf.moonrise.common.util.WorldUtil.getMinSection(this.level); + int maxSection = ca.spottedleaf.moonrise.common.util.WorldUtil.getMaxSection(this.level); + + int minChunkX = Mth.floor(this.center.x - f) >> 4; + int maxChunkX = Mth.floor(this.center.x + f) >> 4; + int minChunkY = Mth.clamp(Mth.floor(this.center.y - f) >> 4, minSection, maxSection); + int maxChunkY = Mth.clamp(Mth.floor(this.center.y + f) >> 4, minSection, maxSection); + int minChunkZ = Mth.floor(this.center.z - f) >> 4; + int maxChunkZ = Mth.floor(this.center.z + f) >> 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(f, chunk.getSectionEntities(chunkY)); + } + } + } + } + + protected final void impactEntities(float f, Entity[] entities) { + for (int i = 0; i < entities.length; i++) { + Entity entity = entities[i]; + if (entity == null) break; // end of entity section + this.impactEntity(f, entity); + if (entity != entities[i]) i--; // entities can be removed mid-explosion + } + } + + protected final void impactEntity(float f, Entity entity) { + if (entity.isAlive() && !entity.isSpectator() && (!this.excludeSourceFromDamage || entity != this.source)) { // Paper - Fix lag from explosions processing dead entities, Allow explosions to damage source + // Sakura end - optimise paper explosions if (!entity.ignoreExplosion(this)) { double d = Math.sqrt(entity.distanceToSqr(this.center)) / f; if (d <= 1.0) { @@ -507,15 +585,16 @@ public class ServerExplosion implements Explosion { // - Damaging EnderDragon does nothing // - EnderDragon hitbox always covers the other parts and is therefore always present if (entity instanceof EnderDragonPart) { - continue; + return; // Sakura - optimise paper explosions } entity.lastDamageCancelled = false; if (entity instanceof EnderDragon) { + final AABB bounds = this.getExplosionBounds(f); // Sakura - optimise paper explosions for (EnderDragonPart dragonPart : ((EnderDragon) entity).getSubEntities()) { // Calculate damage separately for each EntityComplexPart - if (list.contains(dragonPart)) { + if (dragonPart.getBoundingBox().intersects(bounds)) { // Sakura - optimise paper explosions dragonPart.hurtServer(this.level, this.damageSource, this.damageCalculator.getEntityDamageAmount(this, dragonPart, f1)); } } @@ -524,7 +603,7 @@ public class ServerExplosion implements 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 }