Properly randomize trigonometry

This commit is contained in:
JFronny 2021-10-19 21:29:57 +02:00
parent fc327f1871
commit ac30b4388c
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
11 changed files with 48 additions and 72 deletions

View File

@ -6,7 +6,7 @@ minecraft_version=1.17.1
yarn_mappings=build.61
loader_version=0.12.1
# Mod Properties
mod_version=1.4.0
mod_version=1.5.0
maven_group=io.jfronny.gitlab
archives_base_name=quickmeth
# Dependencies

View File

@ -7,6 +7,8 @@ public class Cfg implements JfConfig {
@Entry
public static Boolean corruptGenericMath = true;
@Entry
public static Boolean corruptTrigonometry = true;
@Entry
public static Boolean corruptGenericMath2 = false;
@Entry
public static Boolean corruptPerlinNoise = true;

View File

@ -2,7 +2,18 @@ package io.gitlab.jfronny.quickmeth;
import net.minecraft.util.math.MathHelper;
import java.util.Random;
public class MathUtil {
private static final float[] SIN_TABLE_FAST = new float[4096];
private static final float radToIndex = 651.8986469044033F;
static {
Random rng = new Random(0);
for (int j = 0; j < SIN_TABLE_FAST.length; ++j) {
SIN_TABLE_FAST[j] = rng.nextFloat() * 2 - 1;
}
}
public static double boxedInvert(double min, double max, double value) {
return max - value + min;
}
@ -34,4 +45,8 @@ public class MathUtil {
public static int boxedInvert(int value) {
return boxedInvert(MathHelper.floor(value), MathHelper.ceil(value), value);
}
public static float sin(float value) {
return SIN_TABLE_FAST[(int) (value * radToIndex) & 4095];
}
}

View File

@ -25,6 +25,8 @@ public class MixinPlugin implements IMixinConfigPlugin {
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if (Objects.equals(mixinClassName, MathHelperMixin.class.getName()))
return Cfg.corruptGenericMath;
else if (Objects.equals(mixinClassName, MathHelperTrigonometryMixin.class.getName()))
return Cfg.corruptTrigonometry;
else if (Objects.equals(mixinClassName, MathHelperMixin2.class.getName()))
return Cfg.corruptGenericMath2;
else if (Objects.equals(mixinClassName, MixinPerlinNoiseSampler.class.getName()))
@ -54,5 +56,4 @@ public class MixinPlugin implements IMixinConfigPlugin {
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
}
}

View File

@ -1,32 +0,0 @@
package io.gitlab.jfronny.quickmeth;
import net.fabricmc.api.ModInitializer;
import net.minecraft.util.math.noise.OctavePerlinNoiseSampler;
import net.minecraft.util.math.noise.OctaveSimplexNoiseSampler;
import net.minecraft.util.math.noise.PerlinNoiseSampler;
import net.minecraft.util.math.noise.SimplexNoiseSampler;
import net.minecraft.world.gen.SimpleRandom;
import java.util.List;
public class QuickMethMain implements ModInitializer {
@Override
public void onInitialize() {
// Random mess to test most noise functions
SimpleRandom sr = new SimpleRandom(1024);
PerlinNoiseSampler pns = new PerlinNoiseSampler(sr);
SimplexNoiseSampler sns = new SimplexNoiseSampler(sr);
List<Integer> octaves = List.of(-1, -2, -3);
OctavePerlinNoiseSampler opns = new OctavePerlinNoiseSampler(sr, octaves);
OctaveSimplexNoiseSampler osns = new OctaveSimplexNoiseSampler(sr, octaves);
System.out.println(sns.sample(sns.sample(sr.nextDouble(), sr.nextDouble()),
pns.sample(opns.sample(sr.nextDouble(), sr.nextDouble(), sr.nextDouble()),
opns.sample(sr.nextDouble(), sr.nextDouble(), sr.nextDouble(), sr.nextDouble()), sr.nextDouble()),
pns.sampleDerivative(osns.sample(sr.nextDouble(), sr.nextDouble(), true),
osns.sample(osns.sample(sr.nextDouble(), sr.nextDouble(), false),
sr.nextDouble(), sr.nextDouble(), sr.nextDouble()),
sr.nextDouble(),
new double[] {sr.nextDouble(), sr.nextGaussian(), sr.nextDouble()})));
}
}

View File

@ -1,21 +0,0 @@
package io.gitlab.jfronny.quickmeth;
// https://github.com/FwuffyPetsOwO/FastMathFabric/blob/main/src/main/java/net/FwuffyPetsOwO/FastMath/FastMath.java
public class SinUtil {
private static final float[] SIN_TABLE_FAST = new float[4096];
private static final float radToIndex = roundToFloat(651.8986469044033D);
static {
for (int j = 0; j < SIN_TABLE_FAST.length; ++j) {
SIN_TABLE_FAST[j] = roundToFloat(Math.sin((double) j * Math.PI * 2.0D / 4096.0D));
}
}
public static float sin(float value) {
return SIN_TABLE_FAST[(int) (value * radToIndex) & 4095];
}
public static float roundToFloat(double d) {
return (float) ((double) Math.round(d * 1.0E8D) / 1.0E8D);
}
}

View File

@ -1,7 +1,6 @@
package io.gitlab.jfronny.quickmeth.mixin;
import io.gitlab.jfronny.quickmeth.MathUtil;
import io.gitlab.jfronny.quickmeth.SinUtil;
import net.minecraft.util.math.MathHelper;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
@ -20,19 +19,6 @@ public abstract class MathHelperMixin {
@Shadow public static int floor(double value) { return 0; }
@Shadow public static double nextDouble(Random random, double min, double max) { return 0; }
@Inject(method = "sin(F)F", at = @At("TAIL"), cancellable = true)
private static void invertSin(CallbackInfoReturnable<Float> ci) {
ci.setReturnValue(MathUtil.boxedInvert(ci.getReturnValue()));
}
@Overwrite
public static float sin(float f) {
return SinUtil.sin(f);
}
@Overwrite
public static float cos(float f) { return sin(f); }
@Overwrite
public static float sqrt(float f) {
return (float)Math.pow(f, 0.6);

View File

@ -0,0 +1,25 @@
package io.gitlab.jfronny.quickmeth.mixin;
import io.gitlab.jfronny.quickmeth.MathUtil;
import net.minecraft.util.math.MathHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(MathHelper.class)
public class MathHelperTrigonometryMixin {
@Inject(method = "sin(F)F", at = @At("TAIL"), cancellable = true)
private static void invertSin(CallbackInfoReturnable<Float> ci) {
ci.setReturnValue(MathUtil.boxedInvert(ci.getReturnValue()));
}
@Overwrite
public static float sin(float f) {
return MathUtil.sin(f);
}
@Overwrite
public static float cos(float f) { return sin(f); }
}

View File

@ -2,6 +2,8 @@
"quickmeth.jfconfig.title": "QuickMäth",
"quickmeth.jfconfig.corruptGenericMath": "Corrupt generic math",
"quickmeth.jfconfig.corruptGenericMath.tooltip": "Corrupts methods in MathHelper. This will impact many things but most notably movement",
"quickmeth.jfconfig.corruptTrigonometry": "Corrupt Trigonometry",
"quickmeth.jfconfig.corruptTrigonometry.tooltip": "Corrupts trigonometry methods in MathHelper. Effects vary",
"quickmeth.jfconfig.corruptGenericMath2": "Corrupt more generic math",
"quickmeth.jfconfig.corruptGenericMath2.tooltip": "Corrupts more methods in MathHelper. These WILL break a lot of things so they are separate",
"quickmeth.jfconfig.corruptPerlinNoise": "Corrupt perlin noise",

View File

@ -17,9 +17,6 @@
"entrypoints": {
"libjf:config": [
"io.gitlab.jfronny.quickmeth.Cfg"
],
"main": [
"io.gitlab.jfronny.quickmeth.QuickMethMain"
]
},
"mixins": [

View File

@ -7,6 +7,7 @@
"mixins": [
"MathHelperMixin",
"MathHelperMixin2",
"MathHelperTrigonometryMixin",
"MixinPerlinNoiseSampler",
"MixinSimplexNoiseSampler"
],