diff --git a/plugin/src/main/java/com/alttd/cometskyblock/util/Mth.java b/plugin/src/main/java/com/alttd/cometskyblock/util/Mth.java new file mode 100644 index 0000000..13cf34b --- /dev/null +++ b/plugin/src/main/java/com/alttd/cometskyblock/util/Mth.java @@ -0,0 +1,133 @@ +package com.alttd.cometskyblock.util; + +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + +public class Mth { + + public static int floor(float value) { + int i = (int)value; + return value < (float)i ? i - 1 : i; + } + + public static int floor(double value) { + int i = (int)value; + return value < (double)i ? i - 1 : i; + } + + public static float abs(float value) { + return Math.abs(value); + } + + public static int abs(int value) { + return Math.abs(value); + } + + public static int ceil(float value) { + int i = (int)value; + return value > (float)i ? i + 1 : i; + } + + public static int ceil(double value) { + int i = (int)value; + return value > (double)i ? i + 1 : i; + } + + public static int clamp(int value, int min, int max) { + return Math.min(Math.max(value, min), max); + } + + public static long clamp(long value, long min, long max) { + return Math.min(Math.max(value, min), max); + } + + public static float clamp(float value, float min, float max) { + return value < min ? min : Math.min(value, max); + } + + public static double clamp(double value, double min, double max) { + return value < min ? min : Math.min(value, max); + } + + public static double absMax(double a, double b) { + if (a < 0.0D) { + a = -a; + } + + if (b < 0.0D) { + b = -b; + } + + return Math.max(a, b); + } + + public static int floorDiv(int dividend, int divisor) { + return Math.floorDiv(dividend, divisor); + } + + public static int nextInt(Random random, int min, int max) { + return min >= max ? min : random.nextInt(max - min + 1) + min; + } + + public static float nextFloat(Random random, float min, float max) { + return min >= max ? min : random.nextFloat() * (max - min) + min; + } + + public static double nextDouble(Random random, double min, double max) { + return min >= max ? min : random.nextDouble() * (max - min) + min; + } + + public static float catmullrom(float delta, float p0, float p1, float p2, float p3) { + return 0.5F * (2.0F * p1 + (p2 - p0) * delta + (2.0F * p0 - 5.0F * p1 + 4.0F * p2 - p3) * delta * delta + (3.0F * p1 - p0 - 3.0F * p2 + p3) * delta * delta * delta); + } + + public static double smoothstep(double value) { + return value * value * value * (value * (value * 6.0D - 15.0D) + 10.0D); + } + + public static double smoothstepDerivative(double value) { + return 30.0D * value * value * (value - 1.0D) * (value - 1.0D); + } + + public static int sign(double value) { + if (value == 0.0D) { + return 0; + } else { + return value > 0.0D ? 1 : -1; + } + } + + public static float triangleWave(float value, float maxDeviation) { + return (Math.abs(value % maxDeviation - maxDeviation * 0.5F) - maxDeviation * 0.25F) / (maxDeviation * 0.25F); + } + + public static float square(float n) { + return n * n; + } + + public static double square(double n) { + return n * n; + } + + public static int square(int n) { + return n * n; + } + + public static long square(long n) { + return n * n; + } + + public static int randomBetweenInclusive(Random random, int min, int max) { + return random.nextInt(min, max + 1); + } + + public static float randomBetween(Random random, float min, float max) { + return random.nextFloat() * (max - min) + min; + } + + public static float normal(Random random, float mean, float deviation) { + return mean + (float)random.nextGaussian() * deviation; + } + +} + diff --git a/plugin/src/main/java/com/alttd/cometskyblock/util/Pair.java b/plugin/src/main/java/com/alttd/cometskyblock/util/Pair.java new file mode 100644 index 0000000..38e4ec8 --- /dev/null +++ b/plugin/src/main/java/com/alttd/cometskyblock/util/Pair.java @@ -0,0 +1,17 @@ +package com.alttd.cometskyblock.util; + +import lombok.Getter; + +@Getter +public class Pair { + + private final X x; + private final Y y; + + public Pair(X x, Y y) { + this.x = x; + this.y = y; + } + +} + diff --git a/plugin/src/main/java/com/alttd/cometskyblock/util/WeightedChoice.java b/plugin/src/main/java/com/alttd/cometskyblock/util/WeightedChoice.java new file mode 100644 index 0000000..0be04cc --- /dev/null +++ b/plugin/src/main/java/com/alttd/cometskyblock/util/WeightedChoice.java @@ -0,0 +1,28 @@ +package com.alttd.cometskyblock.util; + +import org.spongepowered.configurate.objectmapping.ConfigSerializable; + +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.concurrent.ThreadLocalRandom; + +@ConfigSerializable +public class WeightedChoice { + + private final NavigableMap map = new TreeMap<>(); + private double total = 0; + + public WeightedChoice add(double weight, E result) { + if (weight <= 0) { + return this; + } + total += weight; + map.put(total, result); + return this; + } + + public E next() { + double value = ThreadLocalRandom.current().nextDouble() * total; + return map.higherEntry(value).getValue(); + } +}