From 55e2de5c6e68d61e1ba372079d93ac9c0a722f43 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Thu, 23 Jul 2020 11:10:46 -0700 Subject: [PATCH] Improve per player mob spawning (#3971) Co-authored-by: Spottedleaf --- ...ement-optional-per-player-mob-spawns.patch | 32 +++++++++++-------- ...-isOutsideRange-to-use-distance-maps.patch | 9 +++--- .../0505-Optimize-Light-Engine.patch | 4 +-- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Spigot-Server-Patches/0383-implement-optional-per-player-mob-spawns.patch b/Spigot-Server-Patches/0383-implement-optional-per-player-mob-spawns.patch index d297b2719..df68198ea 100644 --- a/Spigot-Server-Patches/0383-implement-optional-per-player-mob-spawns.patch +++ b/Spigot-Server-Patches/0383-implement-optional-per-player-mob-spawns.patch @@ -656,7 +656,7 @@ index cacf60563826da0219754a67500fdc239b13f0cd..f06c41d06f853b625dbd46822126810d private static double a(ChunkCoordIntPair chunkcoordintpair, Entity entity) { diff --git a/src/main/java/net/minecraft/server/SpawnerCreature.java b/src/main/java/net/minecraft/server/SpawnerCreature.java -index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb9e8767cd 100644 +index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..b255ccb4501d85f1507b105d2793c7fb6a919f62 100644 --- a/src/main/java/net/minecraft/server/SpawnerCreature.java +++ b/src/main/java/net/minecraft/server/SpawnerCreature.java @@ -29,6 +29,11 @@ public final class SpawnerCreature { @@ -683,10 +683,11 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb }); } } -@@ -125,13 +135,33 @@ public final class SpawnerCreature { +@@ -125,13 +135,36 @@ public final class SpawnerCreature { continue; } +- if ((flag || !enumcreaturetype.d()) && (flag1 || enumcreaturetype.d()) && (flag2 || !enumcreaturetype.e()) && spawnercreature_d.a(enumcreaturetype, limit)) { + // Paper start - only allow spawns upto the limit per chunk and update count afterwards + int currEntityCount = spawnercreature_d.getEntityCountsByType().getInt(enumcreaturetype); + int k1 = limit * spawnercreature_d.getSpawnerChunks() / SpawnerCreature.b; @@ -695,14 +696,17 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb + if (worldserver.paperConfig.perPlayerMobSpawns) { + int minDiff = Integer.MAX_VALUE; + for (EntityPlayer entityplayer : worldserver.getChunkProvider().playerChunkMap.playerMobDistanceMap.getPlayersInRange(chunk.getPos())) { -+ minDiff = Math.min(limit - worldserver.getChunkProvider().playerChunkMap.getMobCountNear(entityplayer, enumcreaturetype), minDiff); ++ int chunkRange = entityplayer.playerNaturallySpawnedEvent.getSpawnRadius(); ++ double rangeScale = (double)((chunkRange * 2 + 1) * (chunkRange * 2 + 1)) / SpawnerCreature.b; ++ int scaledLimit = (int)Math.round(rangeScale * limit); ++ minDiff = Math.min(scaledLimit - worldserver.getChunkProvider().playerChunkMap.getMobCountNear(entityplayer, enumcreaturetype), minDiff); + } + difference = (minDiff == Integer.MAX_VALUE) ? 0 : minDiff; + } + // Paper end + -+ if (difference > 0) { // Paper - if ((flag || !enumcreaturetype.d()) && (flag1 || enumcreaturetype.d()) && (flag2 || !enumcreaturetype.e()) && spawnercreature_d.a(enumcreaturetype, limit)) { ++ // Paper start - per player mob spawning ++ if ((flag || !enumcreaturetype.d()) && (flag1 || enumcreaturetype.d()) && (flag2 || !enumcreaturetype.e()) && difference > 0) { // CraftBukkit end - a(enumcreaturetype, worldserver, chunk, (entitytypes, blockposition, ichunkaccess) -> { + int spawnCount = spawnMobs(enumcreaturetype, worldserver, chunk, (entitytypes, blockposition, ichunkaccess) -> { @@ -710,15 +714,15 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb }, (entityinsentient, ichunkaccess) -> { spawnercreature_d.a(entityinsentient, ichunkaccess); + }, -+ limit, worldserver.paperConfig.perPlayerMobSpawns ? worldserver.getChunkProvider().playerChunkMap::updatePlayerMobTypeMap : null); -+ spawnercreature_d.getEntityCountsByType().mergeInt(enumcreaturetype, 0, (keyInMap, valueInMap) -> { ++ difference, worldserver.paperConfig.perPlayerMobSpawns ? worldserver.getChunkProvider().playerChunkMap::updatePlayerMobTypeMap : null); ++ spawnercreature_d.getEntityCountsByType().mergeInt(enumcreaturetype, spawnCount, (keyInMap, valueInMap) -> { + return Integer.valueOf(spawnCount + valueInMap.intValue()); }); -+ } // Paper ++ // Paper end - per player mob spawning } } -@@ -140,22 +170,34 @@ public final class SpawnerCreature { +@@ -140,22 +173,34 @@ public final class SpawnerCreature { } public static void a(EnumCreatureType enumcreaturetype, WorldServer worldserver, Chunk chunk, SpawnerCreature.c spawnercreature_c, SpawnerCreature.a spawnercreature_a) { @@ -755,7 +759,7 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb int k = 0; while (k < 3) { -@@ -195,13 +237,13 @@ public final class SpawnerCreature { +@@ -195,13 +240,13 @@ public final class SpawnerCreature { // Paper start Boolean doSpawning = a(worldserver, enumcreaturetype, structuremanager, chunkgenerator, biomebase_biomemeta, blockposition_mutableblockposition, d2); if (doSpawning == null) { @@ -771,7 +775,7 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb } entityinsentient.setPositionRotation(d0, (double) i, d1, worldserver.random.nextFloat() * 360.0F, 0.0F); -@@ -209,13 +251,18 @@ public final class SpawnerCreature { +@@ -209,13 +254,18 @@ public final class SpawnerCreature { groupdataentity = entityinsentient.prepare(worldserver, worldserver.getDamageScaler(entityinsentient.getChunkCoordinates()), EnumMobSpawn.NATURAL, groupdataentity, (NBTTagCompound) null); // CraftBukkit start if (worldserver.addEntity(entityinsentient, SpawnReason.NATURAL)) { @@ -793,7 +797,7 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb } if (entityinsentient.c(k1)) { -@@ -237,6 +284,7 @@ public final class SpawnerCreature { +@@ -237,6 +287,7 @@ public final class SpawnerCreature { } } @@ -801,7 +805,7 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb } private static boolean a(WorldServer worldserver, IChunkAccess ichunkaccess, BlockPosition.MutableBlockPosition blockposition_mutableblockposition, double d0) { -@@ -476,8 +524,8 @@ public final class SpawnerCreature { +@@ -476,8 +527,8 @@ public final class SpawnerCreature { public static class d { @@ -812,7 +816,7 @@ index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb private final SpawnerCreatureProbabilities c; private final Object2IntMap d; @Nullable -@@ -540,7 +588,7 @@ public final class SpawnerCreature { +@@ -540,7 +591,7 @@ public final class SpawnerCreature { // CraftBukkit start private boolean a(EnumCreatureType enumcreaturetype, int limit) { diff --git a/Spigot-Server-Patches/0479-Optimize-isOutsideRange-to-use-distance-maps.patch b/Spigot-Server-Patches/0479-Optimize-isOutsideRange-to-use-distance-maps.patch index 0c436066a..e1b33d52b 100644 --- a/Spigot-Server-Patches/0479-Optimize-isOutsideRange-to-use-distance-maps.patch +++ b/Spigot-Server-Patches/0479-Optimize-isOutsideRange-to-use-distance-maps.patch @@ -77,10 +77,10 @@ index 32d3887e2542c4ebba4a7498167fbe4b497a71ce..7e57a53ec614a2f7d2672edff9d7c0e0 public String c() { diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java -index eebd4c50a7324250d3ebe7060739a71af4243f72..a6363b73522f9d27534b6e80f4b3789e84316c49 100644 +index eebd4c50a7324250d3ebe7060739a71af4243f72..319059ba31f3614cc59cd4c4e4fa9242f6e4fe99 100644 --- a/src/main/java/net/minecraft/server/ChunkProviderServer.java +++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java -@@ -728,6 +728,36 @@ public class ChunkProviderServer extends IChunkProvider { +@@ -728,6 +728,37 @@ public class ChunkProviderServer extends IChunkProvider { boolean flag1 = this.world.getGameRules().getBoolean(GameRules.DO_MOB_SPAWNING) && !world.getPlayers().isEmpty(); // CraftBukkit if (!flag) { @@ -112,12 +112,13 @@ index eebd4c50a7324250d3ebe7060739a71af4243f72..a6363b73522f9d27534b6e80f4b3789e + + playerChunkMap.playerMobSpawnMap.addOrUpdate(player, chunkX, chunkZ, range); + player.lastEntitySpawnRadiusSquared = (double)((range << 4) * (range << 4)); // used in isOutsideRange ++ player.playerNaturallySpawnedEvent = event; + } + // Paper end - optimize isOutisdeRange this.world.getMethodProfiler().enter("pollingChunks"); int k = this.world.getGameRules().getInt(GameRules.RANDOM_TICK_SPEED); boolean flag2 = world.ticksPerAnimalSpawns != 0L && worlddata.getTime() % world.ticksPerAnimalSpawns == 0L; // CraftBukkit -@@ -757,15 +787,7 @@ public class ChunkProviderServer extends IChunkProvider { +@@ -757,15 +788,7 @@ public class ChunkProviderServer extends IChunkProvider { this.world.getMethodProfiler().exit(); //List list = Lists.newArrayList(this.playerChunkMap.f()); // Paper //Collections.shuffle(list); // Paper @@ -134,7 +135,7 @@ index eebd4c50a7324250d3ebe7060739a71af4243f72..a6363b73522f9d27534b6e80f4b3789e final int[] chunksTicked = {0}; this.playerChunkMap.forEachVisibleChunk((playerchunk) -> { // Paper - safe iterator incase chunk loads, also no wrapping Optional optional = ((Either) playerchunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left(); -@@ -781,9 +803,9 @@ public class ChunkProviderServer extends IChunkProvider { +@@ -781,9 +804,9 @@ public class ChunkProviderServer extends IChunkProvider { Chunk chunk = (Chunk) optional1.get(); ChunkCoordIntPair chunkcoordintpair = playerchunk.i(); diff --git a/Spigot-Server-Patches/0505-Optimize-Light-Engine.patch b/Spigot-Server-Patches/0505-Optimize-Light-Engine.patch index 65edad281..31bf1a582 100644 --- a/Spigot-Server-Patches/0505-Optimize-Light-Engine.patch +++ b/Spigot-Server-Patches/0505-Optimize-Light-Engine.patch @@ -25,10 +25,10 @@ Massive update to light to improve performance and chunk loading/generation. 8) Fix NPE risk that crashes server in getting nibble data diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java -index 604e7004b659daed2844ba1a76bf09288ec549e5..ef980f9859d1d7d0d5e13d0d70e998055f92135e 100644 +index 7c87ef638d538093e944341525a1027be5d15a0e..3a5a9aba111fed8b68818b95332b723ee2bce2ac 100644 --- a/src/main/java/net/minecraft/server/ChunkProviderServer.java +++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java -@@ -1044,7 +1044,7 @@ public class ChunkProviderServer extends IChunkProvider { +@@ -1045,7 +1045,7 @@ public class ChunkProviderServer extends IChunkProvider { if (ChunkProviderServer.this.tickDistanceManager()) { return true; } else {