Skip to content

Commit

Permalink
Added Rule: optimizedBiomeAccess
Browse files Browse the repository at this point in the history
Now allowing client-side rules for carpet-fixes
  • Loading branch information
FxMorin committed Jan 7, 2022
1 parent bdfec6a commit 03b7966
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!

Expand All @@ -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*
Expand All @@ -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


---

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/carpetfixes/CarpetFixesSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Biome> 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));
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/carpet-fixes.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 03b7966

Please sign in to comment.