42 lines
1.5 KiB
Java
42 lines
1.5 KiB
Java
package games.dmg.spigotheights;
|
|
|
|
import java.util.function.IntUnaryOperator;
|
|
|
|
public final class HeightMath {
|
|
private HeightMath() {
|
|
}
|
|
|
|
public static double randomScale(HeightSettings settings, IntUnaryOperator randomIndex) {
|
|
int outcomes = settings.randomStepCount() + 1;
|
|
int index = randomIndex.applyAsInt(outcomes);
|
|
if (index < 0 || index >= outcomes) {
|
|
throw new IllegalArgumentException("random index is outside the requested bound");
|
|
}
|
|
return normalize(settings.minimum() + index * settings.adjustmentStep());
|
|
}
|
|
|
|
public static double grow(double current, HeightSettings settings) {
|
|
return clamp(current + settings.adjustmentStep(), settings);
|
|
}
|
|
|
|
public static double shrink(double current, HeightSettings settings) {
|
|
return clamp(current - settings.adjustmentStep(), settings);
|
|
}
|
|
|
|
public static double safeStoredScale(Double stored, HeightSettings settings) {
|
|
if (stored == null || !Double.isFinite(stored)) {
|
|
return clamp(1.0, settings);
|
|
}
|
|
// Restoration is an explicit escape from configured stature limits.
|
|
return stored == 1.0 ? 1.0 : clamp(stored, settings);
|
|
}
|
|
|
|
public static double clamp(double value, HeightSettings settings) {
|
|
return normalize(Math.max(settings.minimum(), Math.min(settings.maximum(), value)));
|
|
}
|
|
|
|
private static double normalize(double value) {
|
|
return Math.rint(value * 1_000_000.0) / 1_000_000.0;
|
|
}
|
|
}
|