From 03b79666c17b952bc340218c9b8af13979c7e76e Mon Sep 17 00:00:00 2001 From: FX - PR0CESS Date: Fri, 7 Jan 2022 13:44:53 -0500 Subject: [PATCH] Added Rule: `optimizedBiomeAccess` Now allowing client-side rules for carpet-fixes --- README.md | 14 ++- .../java/carpetfixes/CarpetFixesSettings.java | 8 ++ .../BiomeAccess_predictionMixin.java | 111 ++++++++++++++++++ src/main/resources/carpet-fixes.mixins.json | 1 + 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 src/main/java/carpetfixes/mixins/optimizations/BiomeAccess_predictionMixin.java diff --git a/README.md b/README.md index 62bb947b..6f2fbf29 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # Carpet-Fixes -[![FX's Discord](https://img.shields.io/discord/636633673524969483?logo=discord)](https://discord.gg/vurv5pdFpa) +[![FX's Discord](https://img.shields.io/discord/636633673524969483?logo=discord&style=flat-square)](https://discord.gg/vurv5pdFpa) +[![Mod Environment](https://img.shields.io/badge/Enviroment-Server%2FClient-blue?style=flat-square)](https://github.com/fxmorin/carpet-fixes) +[![GitHub all releases](https://img.shields.io/github/downloads/fxmorin/carpet-fixes/total?style=flat-square)]() +![Latest Minecraft Version](https://img.shields.io/badge/Latest%20MC%20Support-1.18-green?style=flat-square) [![Mod License](https://img.shields.io/github/license/fxmorin/carpet-fixes?style=flat-square)](https://github.com/fxmorin/carpet-fixes/blob/master/LICENSE) -[![Mod Environment](https://img.shields.io/badge/Environment-server-blue?style=flat-square)](https://github.com/samolego/carpet-fixes) [Fabric Carpet](https://github.com/gnembon/fabric-carpet) extension mod which attempts to fix as many vanilla minecraft bugs as possible! @@ -12,6 +14,7 @@ If you enjoy the mod, help me out by giving this project a star, thanks. ## Settings See [List of all available settings](https://github.com/fxmorin/carpet-fixes/wiki/Available-Settings) +*(W.I.P. - Missing lots of rules)* ## How to Use *coming soon* @@ -20,8 +23,11 @@ See [List of all available settings](https://github.com/fxmorin/carpet-fixes/wik *coming soon* ## More detail -This mod works for both Singleplayer & Multiplayer, although it only modifies server-side functionality! -The mod does **not** fix any client-side bugs! +This mod works for both Singleplayer & Multiplayer. +This mod now supports fixing some client-side bugs! +Run `/carpet-fixes list client` to see a list of all available client-side fixes. +These fixes may not be client-side only + --- diff --git a/src/main/java/carpetfixes/CarpetFixesSettings.java b/src/main/java/carpetfixes/CarpetFixesSettings.java index c47c5f5b..511d40c7 100644 --- a/src/main/java/carpetfixes/CarpetFixesSettings.java +++ b/src/main/java/carpetfixes/CarpetFixesSettings.java @@ -892,6 +892,14 @@ public class CarpetFixesSettings { ) public static boolean villagerDiscountIgnoresOfflinePlayersFix = false; + //by FX - PR0CESS + @Rule( + desc = "Optimized the getBiome call to be 25% - 75% faster", + extra = "This is a fully vanilla optimization. Works on both client & server", + category = {OPTIMIZATION,VANILLA,RECOMMENDED,CLIENT} + ) + public static boolean optimizedBiomeAccess = false; + /* DUPE BUGS diff --git a/src/main/java/carpetfixes/mixins/optimizations/BiomeAccess_predictionMixin.java b/src/main/java/carpetfixes/mixins/optimizations/BiomeAccess_predictionMixin.java new file mode 100644 index 00000000..3ae5e083 --- /dev/null +++ b/src/main/java/carpetfixes/mixins/optimizations/BiomeAccess_predictionMixin.java @@ -0,0 +1,111 @@ +package carpetfixes.mixins.optimizations; + +import carpetfixes.CarpetFixesSettings; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; +import net.minecraft.world.biome.Biome; +import net.minecraft.world.biome.source.BiomeAccess; +import net.minecraft.world.biome.source.SeedMixer; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +/** + * Optimized getBiome call: Reduce the number of calls to the mess of + * {@link net.minecraft.world.biome.source.SeedMixer#mixSeed(long, long)} which is pretty heavy on performance. + * + * We are able to do this by skipping around 370 of 512 possible calls to getBiome() by predicting the outcome + * before doing the seed mixing. This seems to be around 25% - 75% faster depending on the use case. + * We can predict much faster than the seed mixing. + * + * @author FX - PR0CESS + */ + +@Mixin(BiomeAccess.class) +public class BiomeAccess_predictionMixin { + + @Shadow @Final private BiomeAccess.Storage storage; + + @Shadow @Final private long seed; + + @Shadow private static double method_38108(long l) {return 0;} + + private static final double maxOffset = 0.4500000001D; + + + @Inject( + method = "getBiome", + at = @At("HEAD"), + cancellable = true + ) + public void optimizedGetBiome(BlockPos pos, CallbackInfoReturnable cir) { + if (CarpetFixesSettings.optimizedBiomeAccess) { + int xMinus2 = pos.getX() - 2; + int yMinus2 = pos.getY() - 2; + int zMinus2 = pos.getZ() - 2; + int x = xMinus2 >> 2; // BlockPos to BiomePos + int y = yMinus2 >> 2; + int z = zMinus2 >> 2; + double quartX = (double) (xMinus2 & 3) / 4.0D; // quartLocal divided by 4 + double quartY = (double) (yMinus2 & 3) / 4.0D; // 0/4, 1/4, 2/4, 3/4 + double quartZ = (double) (zMinus2 & 3) / 4.0D; // [0, 0.25, 0.5, 0.75] + int smallestX = 0; + double smallestDist = Double.POSITIVE_INFINITY; + for (int biomeX = 0; biomeX < 8; ++biomeX) { + boolean everyOtherQuad = (biomeX & 4) == 0; // 1 1 1 1 0 0 0 0 + boolean everyOtherPair = (biomeX & 2) == 0; // 1 1 0 0 1 1 0 0 + boolean everyOther = (biomeX & 1) == 0; // 1 0 1 0 1 0 1 0 + double quartXX = everyOtherQuad ? quartX : quartX - 1.0D; //[-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75] + double quartYY = everyOtherPair ? quartY : quartY - 1.0D; + double quartZZ = everyOther ? quartZ : quartZ - 1.0D; + + //This code block is new + double maxQuartYY = 0.0D, maxQuartZZ = 0.0D; + if (biomeX != 0) { + maxQuartYY = MathHelper.square(Math.max(quartYY + maxOffset, Math.abs(quartYY - maxOffset))); + maxQuartZZ = MathHelper.square(Math.max(quartZZ + maxOffset, Math.abs(quartZZ - maxOffset))); + double maxQuartXX = MathHelper.square(Math.max(quartXX + maxOffset, Math.abs(quartXX - maxOffset))); + if (smallestDist < maxQuartXX + maxQuartYY + maxQuartZZ) { + continue; + } + } + + int xx = everyOtherQuad ? x : x + 1; + int yy = everyOtherPair ? y : y + 1; + int zz = everyOther ? z : z + 1; + + //I transferred the code from method_38106 to here, so I could call continue halfway through + long seed = SeedMixer.mixSeed(this.seed, xx); + seed = SeedMixer.mixSeed(seed, yy); + seed = SeedMixer.mixSeed(seed, zz); + seed = SeedMixer.mixSeed(seed, xx); + seed = SeedMixer.mixSeed(seed, yy); + seed = SeedMixer.mixSeed(seed, zz); + double offsetX = method_38108(seed); + double sqrX = MathHelper.square(quartXX + offsetX); + if (biomeX != 0 && smallestDist < sqrX + maxQuartYY + maxQuartZZ) continue; // skip the rest of the loop + seed = SeedMixer.mixSeed(seed, this.seed); + double offsetY = method_38108(seed); + double sqrY = MathHelper.square(quartYY + offsetY); + if (biomeX != 0 && smallestDist < sqrX + sqrY + maxQuartZZ) continue; // skip the rest of the loop + seed = SeedMixer.mixSeed(seed, this.seed); + double offsetZ = method_38108(seed); + double biomeDist = sqrX + sqrY + MathHelper.square(quartZZ + offsetZ); + + if (smallestDist > biomeDist) { + smallestX = biomeX; + smallestDist = biomeDist; + } + } + + //Back to the orignal code + int biomeX = (smallestX & 4) == 0 ? x : x + 1; + int biomeY = (smallestX & 2) == 0 ? y : y + 1; + int biomeZ = (smallestX & 1) == 0 ? z : z + 1; + cir.setReturnValue(this.storage.getBiomeForNoiseGen(biomeX, biomeY, biomeZ)); + } + } +} diff --git a/src/main/resources/carpet-fixes.mixins.json b/src/main/resources/carpet-fixes.mixins.json index 38a2cedb..02e27514 100644 --- a/src/main/resources/carpet-fixes.mixins.json +++ b/src/main/resources/carpet-fixes.mixins.json @@ -124,6 +124,7 @@ "itemFixes.MinecartItem_missingOcclusionMixin", "itemFixes.PickaxeItem_blackstoneButtonMixin", "itemFixes.SpawnEggItem_offsetAndOcclusionMixin", + "optimizations.BiomeAccess_predictionMixin", "optimizations.ChunkTicketManager_optimizationMixin", "optimizations.MathHelper_hypotMixin", "optimizations.PoweredRailBlock_fasterMixin",