mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-16 21:30:08 +00:00
WIP Seismic Integration
This commit is contained in:
@@ -7,9 +7,10 @@
|
||||
|
||||
package com.dfsek.terra.api.block.entity;
|
||||
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
|
||||
import com.dfsek.terra.api.Handle;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
|
||||
public interface BlockEntity extends Handle {
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
|
||||
package com.dfsek.terra.api.block.state.properties.enums;
|
||||
|
||||
import com.dfsek.terra.api.util.Rotation;
|
||||
import com.dfsek.seismic.type.Rotation;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.Construct;
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
package com.dfsek.terra.api.entity;
|
||||
|
||||
import com.dfsek.terra.api.Handle;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import com.dfsek.terra.api.event.events.AbstractCancellable;
|
||||
import com.dfsek.terra.api.event.events.PackEvent;
|
||||
import com.dfsek.terra.api.structure.LootTable;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
|
||||
package com.dfsek.terra.api.inventory;
|
||||
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
|
||||
|
||||
public interface BlockInventoryHolder extends InventoryHolder {
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.dfsek.terra.api.noise;
|
||||
|
||||
/**
|
||||
* A NoiseSampler which additionally may provide a 1st directional derivative
|
||||
*/
|
||||
public interface DerivativeNoiseSampler extends NoiseSampler {
|
||||
|
||||
static boolean isDifferentiable(NoiseSampler sampler) {
|
||||
return sampler instanceof DerivativeNoiseSampler dSampler && dSampler.isDifferentiable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Samplers may or may not be able to provide a derivative depending on what
|
||||
* inputs they take, this method signals whether this is the case.
|
||||
*
|
||||
* @return If the noise sampler provides a derivative or not
|
||||
*/
|
||||
boolean isDifferentiable();
|
||||
|
||||
/**
|
||||
* Derivative return version of standard 2D noise evaluation
|
||||
*
|
||||
* @param seed
|
||||
* @param x
|
||||
* @param y
|
||||
*
|
||||
* @return 3 element array, in index order: noise value, partial x derivative, partial y derivative
|
||||
*/
|
||||
double[] noised(long seed, double x, double y);
|
||||
|
||||
/**
|
||||
* Derivative return version of standard 3D noise evaluation
|
||||
*
|
||||
* @param seed
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
*
|
||||
* @return 4 element array, in index order: noise value, partial x derivative, partial y derivative, partial z derivative
|
||||
*/
|
||||
double[] noised(long seed, double x, double y, double z);
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.noise;
|
||||
|
||||
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
import com.dfsek.terra.api.util.vector.Vector2Int;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
|
||||
|
||||
public interface NoiseSampler {
|
||||
static NoiseSampler zero() {
|
||||
return new NoiseSampler() {
|
||||
@Override
|
||||
public double noise(long seed, double x, double y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double noise(long seed, double x, double y, double z) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
default double noise(Vector3 vector3, long seed) {
|
||||
return noise(seed, vector3.getX(), vector3.getY(), vector3.getZ());
|
||||
}
|
||||
|
||||
default double noise(Vector3Int vector3, long seed) {
|
||||
return noise(seed, vector3.getX(), vector3.getY(), vector3.getZ());
|
||||
}
|
||||
|
||||
|
||||
default double noise(Vector2 vector2, long seed) {
|
||||
return noise(seed, vector2.getX(), vector2.getZ());
|
||||
}
|
||||
|
||||
default double noise(Vector2Int vector2, long seed) {
|
||||
return noise(seed, vector2.getX(), vector2.getZ());
|
||||
}
|
||||
|
||||
double noise(long seed, double x, double y);
|
||||
|
||||
default double noise(long seed, int x, int y) {
|
||||
return noise(seed, (double) x, y);
|
||||
}
|
||||
|
||||
double noise(long seed, double x, double y, double z);
|
||||
|
||||
default double noise(long seed, int x, int y, int z) {
|
||||
return noise(seed, (double) x, y, z);
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ package com.dfsek.terra.api.structure;
|
||||
|
||||
import java.util.random.RandomGenerator;
|
||||
|
||||
import com.dfsek.terra.api.util.Rotation;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
import com.dfsek.seismic.type.Rotation;
|
||||
import com.dfsek.seismic.type.vector.Vector3Int;
|
||||
import com.dfsek.terra.api.world.WritableWorld;
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ package com.dfsek.terra.api.structure;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus.Experimental;
|
||||
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
|
||||
|
||||
@Experimental
|
||||
|
||||
@@ -12,7 +12,7 @@ import org.jetbrains.annotations.ApiStatus.Experimental;
|
||||
import com.dfsek.terra.api.registry.key.StringIdentifiable;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.structure.StructureSpawn;
|
||||
import com.dfsek.terra.api.util.Range;
|
||||
import com.dfsek.terra.api.util.range.Range;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ package com.dfsek.terra.api.structure.feature;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.IntConsumer;
|
||||
|
||||
import com.dfsek.terra.api.util.Range;
|
||||
import com.dfsek.terra.api.util.range.Range;
|
||||
import com.dfsek.terra.api.util.function.IntToBooleanFunction;
|
||||
import com.dfsek.terra.api.util.generic.Lazy;
|
||||
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.dfsek.terra.api.util;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
|
||||
|
||||
public final class GeometryUtil {
|
||||
private GeometryUtil() {
|
||||
|
||||
}
|
||||
|
||||
public static void sphere(Vector3Int origin, int radius, Consumer<Vector3Int> action) {
|
||||
for(int x = -radius; x <= radius; x++) {
|
||||
for(int y = -radius; y <= radius; y++) {
|
||||
for(int z = -radius; z <= radius; z++) {
|
||||
if(x * x + y * y + z * z <= radius * radius) {
|
||||
action.accept(Vector3Int.of(origin, x, y, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cube(Vector3Int origin, int radius, Consumer<Vector3Int> action) {
|
||||
for(int x = -radius; x <= radius; x++) {
|
||||
for(int y = -radius; y <= radius; y++) {
|
||||
for(int z = -radius; z <= radius; z++) {
|
||||
action.accept(Vector3Int.of(origin, x, y, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Utility class for mathematical functions.
|
||||
*/
|
||||
public final class MathUtil {
|
||||
/**
|
||||
* Epsilon for fuzzy floating point comparisons.
|
||||
*/
|
||||
public static final double EPSILON = 1.0E-5;
|
||||
private static final int SIN_BITS, SIN_MASK, SIN_COUNT;
|
||||
private static final double radFull, radToIndex;
|
||||
private static final double degFull, degToIndex;
|
||||
private static final double[] sin, cos;
|
||||
static {
|
||||
SIN_BITS = 12;
|
||||
SIN_MASK = ~(-1 << SIN_BITS);
|
||||
SIN_COUNT = SIN_MASK + 1;
|
||||
|
||||
radFull = Math.PI * 2.0;
|
||||
degFull = 360.0;
|
||||
radToIndex = SIN_COUNT / radFull;
|
||||
degToIndex = SIN_COUNT / degFull;
|
||||
|
||||
sin = new double[SIN_COUNT];
|
||||
cos = new double[SIN_COUNT];
|
||||
|
||||
for(int i = 0; i < SIN_COUNT; i++) {
|
||||
sin[i] = Math.sin((i + 0.5f) / SIN_COUNT * radFull);
|
||||
cos[i] = Math.cos((i + 0.5f) / SIN_COUNT * radFull);
|
||||
}
|
||||
|
||||
// Four cardinal directions (credits: Nate)
|
||||
for(int i = 0; i < 360; i += 90) {
|
||||
sin[(int) (i * degToIndex) & SIN_MASK] = Math.sin(i * Math.PI / 180.0);
|
||||
cos[(int) (i * degToIndex) & SIN_MASK] = Math.cos(i * Math.PI / 180.0);
|
||||
}
|
||||
}
|
||||
|
||||
public static double sin(double rad) {
|
||||
return sin[(int) (rad * radToIndex) & SIN_MASK];
|
||||
}
|
||||
|
||||
public static double cos(double rad) {
|
||||
return cos[(int) (rad * radToIndex) & SIN_MASK];
|
||||
}
|
||||
|
||||
public static double tan(double rad) {
|
||||
return sin(rad) / cos(rad);
|
||||
}
|
||||
|
||||
public static double invSqrt(double x) {
|
||||
double halfX = 0.5d * x;
|
||||
long i = Double.doubleToLongBits(x); // evil floating point bit level hacking
|
||||
i = 0x5FE6EC85E7DE30DAL - (i >> 1); // what the fuck?
|
||||
double y = Double.longBitsToDouble(i);
|
||||
y *= (1.5d - halfX * y * y); // 1st newtonian iteration
|
||||
// y *= (1.5d - halfX * y * y); // 2nd newtonian iteration, this can be removed
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the standard deviation of an array of doubles.
|
||||
*
|
||||
* @param numArray The array of numbers to calculate the standard deviation of.
|
||||
*
|
||||
* @return double - The standard deviation.
|
||||
*/
|
||||
public static double standardDeviation(List<Number> numArray) {
|
||||
double sum = 0.0, standardDeviation = 0.0;
|
||||
int length = numArray.size();
|
||||
|
||||
for(Number num : numArray) {
|
||||
sum += num.doubleValue();
|
||||
}
|
||||
|
||||
double mean = sum / length;
|
||||
|
||||
for(Number num : numArray) {
|
||||
standardDeviation += Math.pow(num.doubleValue() - mean, 2);
|
||||
}
|
||||
|
||||
return Math.sqrt(standardDeviation / length);
|
||||
}
|
||||
|
||||
public static long hashToLong(String s) {
|
||||
if(s == null) {
|
||||
return 0;
|
||||
}
|
||||
long hash = 0;
|
||||
for(char c : s.toCharArray()) {
|
||||
hash = 31L * hash + c;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare 2 floating-point values with epsilon to account for rounding errors
|
||||
*
|
||||
* @param a Value 1
|
||||
* @param b Value 2
|
||||
*
|
||||
* @return Whether these values are equal
|
||||
*/
|
||||
public static boolean equals(double a, double b) {
|
||||
return a == b || Math.abs(a - b) < EPSILON;
|
||||
}
|
||||
|
||||
public static int normalizeIndex(double val, int size) {
|
||||
return Math.max(Math.min((int) Math.floor(((val + 1D) / 2D) * size), size - 1), 0);
|
||||
}
|
||||
|
||||
public static long squash(int first, int last) {
|
||||
return (((long) first) << 32) | (last & 0xffffffffL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamp value to range of [-1, 1]
|
||||
*
|
||||
* @param in Value to clamp
|
||||
*
|
||||
* @return Clamped value
|
||||
*/
|
||||
public static double clamp(double in) {
|
||||
return Math.min(Math.max(in, -1), 1);
|
||||
}
|
||||
|
||||
public static int clamp(int min, int i, int max) {
|
||||
return Math.max(Math.min(i, max), min);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the value in a normally distributed data set that has probability p.
|
||||
*
|
||||
* @param p Probability of value
|
||||
* @param mu Mean of data
|
||||
* @param sigma Standard deviation of data
|
||||
*
|
||||
* @return Value corresponding to input probability
|
||||
*/
|
||||
public static double normalInverse(double p, double mu, double sigma) {
|
||||
if(p < 0 || p > 1)
|
||||
throw new IllegalArgumentException("Probability must be in range [0, 1]");
|
||||
if(sigma < 0)
|
||||
throw new IllegalArgumentException("Standard deviation must be positive.");
|
||||
if(p == 0)
|
||||
return Double.NEGATIVE_INFINITY;
|
||||
if(p == 1)
|
||||
return Double.POSITIVE_INFINITY;
|
||||
if(sigma == 0)
|
||||
return mu;
|
||||
double q, r, val;
|
||||
|
||||
q = p - 0.5;
|
||||
|
||||
if(Math.abs(q) <= .425) {
|
||||
r = .180625 - q * q;
|
||||
val =
|
||||
q * (((((((r * 2509.0809287301226727 +
|
||||
33430.575583588128105) * r + 67265.770927008700853) * r +
|
||||
45921.953931549871457) * r + 13731.693765509461125) * r +
|
||||
1971.5909503065514427) * r + 133.14166789178437745) * r +
|
||||
3.387132872796366608)
|
||||
/ (((((((r * 5226.495278852854561 +
|
||||
28729.085735721942674) * r + 39307.89580009271061) * r +
|
||||
21213.794301586595867) * r + 5394.1960214247511077) * r +
|
||||
687.1870074920579083) * r + 42.313330701600911252) * r + 1);
|
||||
} else {
|
||||
if(q > 0) {
|
||||
r = 1 - p;
|
||||
} else {
|
||||
r = p;
|
||||
}
|
||||
|
||||
r = Math.sqrt(-Math.log(r));
|
||||
|
||||
if(r <= 5) {
|
||||
r -= 1.6;
|
||||
val = (((((((r * 7.7454501427834140764e-4 +
|
||||
.0227238449892691845833) * r + .24178072517745061177) *
|
||||
r + 1.27045825245236838258) * r +
|
||||
3.64784832476320460504) * r + 5.7694972214606914055) *
|
||||
r + 4.6303378461565452959) * r +
|
||||
1.42343711074968357734)
|
||||
/ (((((((r *
|
||||
1.05075007164441684324e-9 + 5.475938084995344946e-4) *
|
||||
r + .0151986665636164571966) * r +
|
||||
.14810397642748007459) * r + .68976733498510000455) *
|
||||
r + 1.6763848301838038494) * r +
|
||||
2.05319162663775882187) * r + 1);
|
||||
} else {
|
||||
r -= 5;
|
||||
val = (((((((r * 2.01033439929228813265e-7 +
|
||||
2.71155556874348757815e-5) * r +
|
||||
.0012426609473880784386) * r + .026532189526576123093) *
|
||||
r + .29656057182850489123) * r +
|
||||
1.7848265399172913358) * r + 5.4637849111641143699) *
|
||||
r + 6.6579046435011037772)
|
||||
/ (((((((r *
|
||||
2.04426310338993978564e-15 + 1.4215117583164458887e-7) *
|
||||
r + 1.8463183175100546818e-5) * r +
|
||||
7.868691311456132591e-4) * r + .0148753612908506148525)
|
||||
* r + .13692988092273580531) * r +
|
||||
.59983220655588793769) * r + 1);
|
||||
}
|
||||
|
||||
if(q < 0.0) {
|
||||
val = -val;
|
||||
}
|
||||
}
|
||||
|
||||
return mu + sigma * val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Murmur64 hashing function
|
||||
*
|
||||
* @param h Input value
|
||||
*
|
||||
* @return Hashed value
|
||||
*/
|
||||
public static long murmur64(long h) {
|
||||
h ^= h >>> 33;
|
||||
h *= 0xff51afd7ed558ccdL;
|
||||
h ^= h >>> 33;
|
||||
h *= 0xc4ceb9fe1a85ec53L;
|
||||
h ^= h >>> 33;
|
||||
return h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1D Linear interpolation between 2 points 1 unit apart.
|
||||
*
|
||||
* @param t - Distance from v0. Total distance between v0 and v1 is 1 unit.
|
||||
* @param v0 - Value at v0.
|
||||
* @param v1 - Value at v1.
|
||||
*
|
||||
* @return double - The interpolated value.
|
||||
*/
|
||||
public static double lerp(double t, double v0, double v1) {
|
||||
return v0 + t * (v1 - v0);
|
||||
}
|
||||
|
||||
public static double cubicLerp(double a, double b, double c, double d, double t) {
|
||||
double p = (d - c) - (a - b);
|
||||
return t * t * t * p + t * t * ((a - b) - p) + t * (c - a) + b;
|
||||
}
|
||||
|
||||
public static double interpHermite(double t) {
|
||||
return t * t * (3 - 2 * t);
|
||||
}
|
||||
|
||||
public static double interpQuintic(double t) {
|
||||
return t * t * t * (t * (t * 6 - 15) + 10);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util;
|
||||
|
||||
import java.util.random.RandomGenerator;
|
||||
import java.util.random.RandomGeneratorFactory;
|
||||
|
||||
import com.dfsek.terra.api.world.chunk.Chunk;
|
||||
|
||||
|
||||
public final class PopulationUtil {
|
||||
public static RandomGenerator getRandom(Chunk c) {
|
||||
return getRandom(c, 0);
|
||||
}
|
||||
|
||||
public static RandomGenerator getRandom(Chunk c, long salt) {
|
||||
return RandomGeneratorFactory.<RandomGenerator.SplittableGenerator>of("Xoroshiro128PlusPlus").create(
|
||||
getCarverChunkSeed(c.getX(), c.getZ(), c.getWorld().getSeed() + salt));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the carver seed for a chunk.
|
||||
*
|
||||
* @param chunkX Chunk's X coordinate
|
||||
* @param chunkZ Chunk's Z coordinate
|
||||
* @param seed World seed
|
||||
*
|
||||
* @return long - The carver seed.
|
||||
*/
|
||||
public static long getCarverChunkSeed(int chunkX, int chunkZ, long seed) {
|
||||
RandomGenerator r = RandomGeneratorFactory.<RandomGenerator.SplittableGenerator>of("Xoroshiro128PlusPlus").create(seed);
|
||||
return chunkX * r.nextLong() ^ chunkZ * r.nextLong() ^ seed;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util;
|
||||
|
||||
public enum Rotation {
|
||||
|
||||
CW_90(90),
|
||||
CW_180(180),
|
||||
CCW_90(270),
|
||||
NONE(0);
|
||||
private final int degrees;
|
||||
|
||||
Rotation(int degrees) {
|
||||
this.degrees = degrees;
|
||||
}
|
||||
|
||||
public static Rotation fromDegrees(int deg) {
|
||||
return switch(Math.floorMod(deg, 360)) {
|
||||
case 0 -> Rotation.NONE;
|
||||
case 90 -> Rotation.CW_90;
|
||||
case 180 -> Rotation.CW_180;
|
||||
case 270 -> Rotation.CCW_90;
|
||||
default -> throw new IllegalArgumentException();
|
||||
};
|
||||
}
|
||||
|
||||
public Rotation inverse() {
|
||||
return switch(this) {
|
||||
case NONE -> NONE;
|
||||
case CCW_90 -> CW_90;
|
||||
case CW_90 -> CCW_90;
|
||||
case CW_180 -> CW_180;
|
||||
};
|
||||
}
|
||||
|
||||
public Rotation rotate(Rotation rotation) {
|
||||
return fromDegrees(this.getDegrees() + rotation.getDegrees());
|
||||
}
|
||||
|
||||
public int getDegrees() {
|
||||
return degrees;
|
||||
}
|
||||
|
||||
public enum Axis {
|
||||
X,
|
||||
Y,
|
||||
Z
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util;
|
||||
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
import com.dfsek.terra.api.util.vector.Vector2.Mutable;
|
||||
|
||||
|
||||
public final class RotationUtil {
|
||||
/**
|
||||
* Rotate and mirror a coordinate pair.
|
||||
*
|
||||
* @param orig Vector to rotate.
|
||||
* @param r Rotation
|
||||
*
|
||||
* @return Rotated vector
|
||||
*/
|
||||
public static Vector2 rotateVector(Vector2 orig, Rotation r) {
|
||||
Mutable copy = orig.mutable();
|
||||
switch(r) {
|
||||
case CW_90 -> copy.setX(orig.getZ()).setZ(-orig.getX());
|
||||
case CCW_90 -> copy.setX(-orig.getZ()).setZ(orig.getX());
|
||||
case CW_180 -> copy.multiply(-1);
|
||||
}
|
||||
return copy.immutable();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
package com.dfsek.terra.api.util.collection;
|
||||
|
||||
import com.dfsek.seismic.math.normalization.NormalizationFunctions;
|
||||
import com.dfsek.seismic.type.sampler.Sampler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -19,11 +21,9 @@ import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.random.RandomGenerator;
|
||||
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.util.MathUtil;
|
||||
import com.dfsek.terra.api.util.mutable.MutableInteger;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3Int;
|
||||
|
||||
|
||||
public class ProbabilityCollection<E> implements Collection<E> {
|
||||
@@ -49,29 +49,29 @@ public class ProbabilityCollection<E> implements Collection<E> {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public E get(NoiseSampler n, double x, double y, double z, long seed) {
|
||||
public E get(Sampler n, double x, double y, double z, long seed) {
|
||||
if(array.length == 0) return null;
|
||||
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, x, y, z), array.length)];
|
||||
return (E) array[NormalizationFunctions.normalizeIndex(n.getSample(seed, x, y, z), array.length)];
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public E get(NoiseSampler n, Vector3Int vector3Int, long seed) {
|
||||
public E get(Sampler n, Vector3Int vector3Int, long seed) {
|
||||
if(array.length == 0) return null;
|
||||
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()),
|
||||
return (E) array[(int) NormalizationFunctions.normalizeIndex(n.getSample(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()),
|
||||
array.length)];
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public E get(NoiseSampler n, Vector3 vector3Int, long seed) {
|
||||
public E get(Sampler n, Vector3 vector3Int, long seed) {
|
||||
if(array.length == 0) return null;
|
||||
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()),
|
||||
return (E) array[(int) NormalizationFunctions.normalizeIndex(n.getSample(seed, vector3Int.getX(), vector3Int.getY(), vector3Int.getZ()),
|
||||
array.length)];
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public E get(NoiseSampler n, double x, double z, long seed) {
|
||||
public E get(Sampler n, double x, double z, long seed) {
|
||||
if(array.length == 0) return null;
|
||||
return (E) array[(int) MathUtil.normalizeIndex(n.noise(seed, x, z), array.length)];
|
||||
return (E) array[(int) NormalizationFunctions.normalizeIndex(n.getSample(seed, x, z), array.length)];
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -202,12 +202,12 @@ public class ProbabilityCollection<E> implements Collection<E> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(NoiseSampler n, double x, double y, double z, long seed) {
|
||||
public T get(Sampler n, double x, double y, double z, long seed) {
|
||||
return single;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(NoiseSampler n, double x, double z, long seed) {
|
||||
public T get(Sampler n, double x, double z, long seed) {
|
||||
return single;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util;
|
||||
package com.dfsek.terra.api.util.range;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util;
|
||||
package com.dfsek.terra.api.util.range;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -1,256 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util.vector;
|
||||
|
||||
import com.dfsek.terra.api.util.MathUtil;
|
||||
|
||||
|
||||
/**
|
||||
* oh yeah
|
||||
*/
|
||||
public class Vector2 {
|
||||
private static final Vector2 ZERO = new Vector2(0, 0);
|
||||
private static final Vector2 UNIT = new Vector2(0, 1);
|
||||
protected double x, z;
|
||||
|
||||
/**
|
||||
* Create a vector with a given X and Z component
|
||||
*
|
||||
* @param x X component
|
||||
* @param z Z component
|
||||
*/
|
||||
private Vector2(double x, double z) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static Vector2 zero() {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
public static Vector2 unit() {
|
||||
return UNIT;
|
||||
}
|
||||
|
||||
public static Vector2 of(double x, double z) {
|
||||
return new Vector2(x, z);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the length of this Vector
|
||||
*
|
||||
* @return length
|
||||
*/
|
||||
public double length() {
|
||||
return Math.sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the squared length of this Vector
|
||||
*
|
||||
* @return squared length
|
||||
*/
|
||||
public double lengthSquared() {
|
||||
return x * x + z * z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the distance from this vector to another.
|
||||
*
|
||||
* @param other Another vector
|
||||
*
|
||||
* @return Distance between vectors
|
||||
*/
|
||||
public double distance(Vector2 other) {
|
||||
return Math.sqrt(distanceSquared(other));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the squared distance between 2 vectors.
|
||||
*
|
||||
* @param other Another vector
|
||||
*
|
||||
* @return Squared distance
|
||||
*/
|
||||
public double distanceSquared(Vector2 other) {
|
||||
double dx = other.getX() - x;
|
||||
double dz = other.getZ() - z;
|
||||
return dx * dx + dz * dz;
|
||||
}
|
||||
|
||||
public Vector3 extrude(double y) {
|
||||
return Vector3.of(this.x, y, this.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get X component
|
||||
*
|
||||
* @return X component
|
||||
*/
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Z component
|
||||
*
|
||||
* @return Z component
|
||||
*/
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
|
||||
public int getBlockX() {
|
||||
return (int) Math.floor(x);
|
||||
}
|
||||
|
||||
public int getBlockZ() {
|
||||
return (int) Math.floor(z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 17;
|
||||
hash = 31 * hash + Double.hashCode(x);
|
||||
hash = 31 * hash + Double.hashCode(z);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if(!(obj instanceof Vector2 other)) return false;
|
||||
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
|
||||
}
|
||||
|
||||
public Mutable mutable() {
|
||||
return new Mutable(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + x + ", " + z + ")";
|
||||
}
|
||||
|
||||
|
||||
public static class Mutable extends Vector2 {
|
||||
|
||||
private Mutable(double x, double z) {
|
||||
super(x, z);
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Mutable setX(double x) {
|
||||
this.x = x;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public Mutable setZ(double z) {
|
||||
this.z = z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector2 immutable() {
|
||||
return Vector2.of(x, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the length of this Vector
|
||||
*
|
||||
* @return length
|
||||
*/
|
||||
public double length() {
|
||||
return Math.sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the squared length of this Vector
|
||||
*
|
||||
* @return squared length
|
||||
*/
|
||||
public double lengthSquared() {
|
||||
return x * x + z * z;
|
||||
}
|
||||
|
||||
public Mutable add(double x, double z) {
|
||||
this.x += x;
|
||||
this.z += z;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply X and Z components by a value.
|
||||
*
|
||||
* @param m Value to multiply
|
||||
*
|
||||
* @return Mutated vector, for chaining.
|
||||
*/
|
||||
public Mutable multiply(double m) {
|
||||
x *= m;
|
||||
z *= m;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add this vector to another.
|
||||
*
|
||||
* @param other Vector to add
|
||||
*
|
||||
* @return Mutated vector, for chaining.
|
||||
*/
|
||||
public Mutable add(Vector2 other) {
|
||||
x += other.getX();
|
||||
z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract a vector from this vector,
|
||||
*
|
||||
* @param other Vector to subtract
|
||||
*
|
||||
* @return Mutated vector, for chaining.
|
||||
*/
|
||||
public Mutable subtract(Vector2 other) {
|
||||
x -= other.getX();
|
||||
z -= other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize this vector to length 1
|
||||
*
|
||||
* @return Mutated vector, for chaining.
|
||||
*/
|
||||
public Mutable normalize() {
|
||||
divide(length());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide X and Z components by a value.
|
||||
*
|
||||
* @param d Divisor
|
||||
*
|
||||
* @return Mutated vector, for chaining.
|
||||
*/
|
||||
public Mutable divide(double d) {
|
||||
x /= d;
|
||||
z /= d;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package com.dfsek.terra.api.util.vector;
|
||||
|
||||
import com.dfsek.terra.api.util.Rotation;
|
||||
|
||||
|
||||
/**
|
||||
* oh yeah
|
||||
*/
|
||||
public class Vector2Int {
|
||||
private static final Vector2Int ZERO = new Vector2Int(0, 0);
|
||||
private static final Vector2Int UNIT = new Vector2Int(0, 1);
|
||||
protected int x, z;
|
||||
|
||||
protected Vector2Int(int x, int z) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static Vector2Int zero() {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
public static Vector2Int unit() {
|
||||
return UNIT;
|
||||
}
|
||||
|
||||
public static Vector2Int of(int x, int z) {
|
||||
return new Vector2Int(x, z);
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public Vector3Int toVector3(int y) {
|
||||
return new Vector3Int(x, y, z);
|
||||
}
|
||||
|
||||
public Mutable mutable() {
|
||||
return new Mutable(x, z);
|
||||
}
|
||||
|
||||
public Vector2Int rotate(Rotation rotation) {
|
||||
return switch(rotation) {
|
||||
case CW_90 -> of(z, -x);
|
||||
case CCW_90 -> of(-z, x);
|
||||
case CW_180 -> of(-x, -z);
|
||||
default -> this;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (31 * x) + z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof Vector2Int that) {
|
||||
return this.x == that.x && this.z == that.z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static class Mutable extends Vector2Int {
|
||||
|
||||
protected Mutable(int x, int z) {
|
||||
super(x, z);
|
||||
}
|
||||
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setZ(int z) {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public Vector2Int immutable() {
|
||||
return new Vector2Int(x, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,441 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2025 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.util.vector;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.dfsek.terra.api.util.MathUtil;
|
||||
|
||||
|
||||
public class Vector3 {
|
||||
private static final Vector3 ZERO = new Vector3(0, 0, 0);
|
||||
private static final Vector3 UNIT = new Vector3(0, 1, 0);
|
||||
protected double x, y, z;
|
||||
|
||||
private Vector3(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static Vector3 zero() {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
public static Vector3 unit() {
|
||||
return UNIT;
|
||||
}
|
||||
|
||||
public static Vector3 of(double x, double y, double z) {
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
public double lengthSquared() {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
public double length() {
|
||||
return Math.sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
public double inverseLength() {
|
||||
return MathUtil.invSqrt(lengthSquared());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the distance between this vector and another. The value of this
|
||||
* method is not cached and uses a costly square-root function, so do not
|
||||
* repeatedly call this method to get the vector's magnitude. NaN will be
|
||||
* returned if the inner result of the sqrt() function overflows, which
|
||||
* will be caused if the distance is too long.
|
||||
*
|
||||
* @param o The other vector
|
||||
*
|
||||
* @return the distance
|
||||
*/
|
||||
public double distance(@NotNull Vector3 o) {
|
||||
return Math.sqrt(Math.pow(x - o.getX(), 2) + Math.pow(y - o.getY(), 2) + Math.pow(z - o.getZ(), 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the squared distance between this vector and another.
|
||||
*
|
||||
* @param o The other vector
|
||||
*
|
||||
* @return the distance
|
||||
*/
|
||||
public double distanceSquared(@NotNull Vector3 o) {
|
||||
return Math.pow(x - o.getX(), 2) + Math.pow(y - o.getY(), 2) + Math.pow(z - o.getZ(), 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of this vector with another. The dot product
|
||||
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
|
||||
*
|
||||
* @param other The other vector
|
||||
*
|
||||
* @return dot product
|
||||
*/
|
||||
public double dot(@NotNull Vector3 other) {
|
||||
return x * other.getX() + y * other.getY() + z * other.getZ();
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
|
||||
public int getBlockX() {
|
||||
return (int) Math.floor(x);
|
||||
}
|
||||
|
||||
public int getBlockY() {
|
||||
return (int) Math.floor(y);
|
||||
}
|
||||
|
||||
public int getBlockZ() {
|
||||
return (int) Math.floor(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if a vector is normalized
|
||||
*
|
||||
* @return whether the vector is normalised
|
||||
*/
|
||||
public boolean isNormalized() {
|
||||
return MathUtil.equals(this.lengthSquared(), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hash code for this vector
|
||||
*
|
||||
* @return hash code
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 7;
|
||||
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
|
||||
return hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if two objects are equal.
|
||||
* <p>
|
||||
* Only two Vectors can ever return true. This method uses a fuzzy match
|
||||
* to account for floating point errors. The epsilon can be retrieved
|
||||
* with epsilon.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(!(obj instanceof Vector3 other)) return false;
|
||||
return MathUtil.equals(x, other.getX()) && MathUtil.equals(y, other.getY()) && MathUtil.equals(z, other.getZ());
|
||||
}
|
||||
|
||||
public Vector3Int toInt() {
|
||||
return Vector3Int.of(getBlockX(), getBlockY(), getBlockZ());
|
||||
}
|
||||
|
||||
public Mutable mutable() {
|
||||
return new Mutable(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + getX() + ", " + getY() + ", " + getZ() + ")";
|
||||
}
|
||||
|
||||
|
||||
public static class Mutable extends Vector3 {
|
||||
private Mutable(double x, double y, double z) {
|
||||
super(x, y, z);
|
||||
}
|
||||
|
||||
public static Mutable of(double x, double y, double z) {
|
||||
return new Mutable(x, y, z);
|
||||
}
|
||||
|
||||
public Vector3 immutable() {
|
||||
return Vector3.of(x, y, z);
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public Mutable setZ(double z) {
|
||||
this.z = z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Mutable setX(double x) {
|
||||
this.x = x;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public Mutable setY(double y) {
|
||||
this.y = y;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double lengthSquared() {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
public double length() {
|
||||
return Math.sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
public double inverseLength() {
|
||||
return MathUtil.invSqrt(lengthSquared());
|
||||
}
|
||||
|
||||
public Mutable normalize() {
|
||||
return this.multiply(this.inverseLength());
|
||||
}
|
||||
|
||||
public Mutable subtract(int x, int y, int z) {
|
||||
this.x -= x;
|
||||
this.y -= y;
|
||||
this.z -= z;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of this vector with another. The dot product
|
||||
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
|
||||
*
|
||||
* @param other The other vector
|
||||
*
|
||||
* @return dot product
|
||||
*/
|
||||
public double dot(@NotNull Vector3 other) {
|
||||
return x * other.getX() + y * other.getY() + z * other.getZ();
|
||||
}
|
||||
|
||||
public Mutable subtract(Vector3 end) {
|
||||
x -= end.getX();
|
||||
y -= end.getY();
|
||||
z -= end.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable multiply(double m) {
|
||||
x *= m;
|
||||
y *= m;
|
||||
z *= m;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(double x, double y, double z) {
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
this.z += z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(Vector3 other) {
|
||||
this.x += other.getX();
|
||||
this.y += other.getY();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(Vector3Int other) {
|
||||
this.x += other.getX();
|
||||
this.y += other.getY();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(Vector2 other) {
|
||||
this.x += other.getX();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
||||
*
|
||||
* <p>
|
||||
* Rotation will follow the general Right-Hand-Rule, which means rotation
|
||||
* will be counterclockwise when the axis is pointing towards the observer.
|
||||
* <p>
|
||||
* This method will always make sure the provided axis is a unit vector, to
|
||||
* not modify the length of the vector when rotating.
|
||||
*
|
||||
* @param axis the axis to rotate the vector around. If the passed vector is
|
||||
* not of length 1, it gets copied and normalized before using it for the
|
||||
* rotation. Please use {@link Mutable#normalize()} on the instance before
|
||||
* passing it to this method
|
||||
* @param angle the angle to rotate the vector around the axis
|
||||
*
|
||||
* @return the same vector
|
||||
*
|
||||
* @throws IllegalArgumentException if the provided axis vector instance is
|
||||
* null
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
|
||||
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.mutable().normalize().immutable(), angle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
||||
*
|
||||
* <p>
|
||||
* Rotation will follow the general Right-Hand-Rule, which means rotation
|
||||
* will be counterclockwise when the axis is pointing towards the observer.
|
||||
* <p>
|
||||
* Note that the vector length will change accordingly to the axis vector
|
||||
* length. If the provided axis is not a unit vector, the rotated vector
|
||||
* will not have its previous length. The scaled length of the resulting
|
||||
* vector will be related to the axis vector.
|
||||
*
|
||||
* @param axis the axis to rotate the vector around.
|
||||
* @param angle the angle to rotate the vector around the axis
|
||||
*
|
||||
* @return the same vector
|
||||
*
|
||||
* @throws IllegalArgumentException if the provided axis vector instance is
|
||||
* null
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundNonUnitAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
|
||||
double x = getX(), y = getY(), z = getZ();
|
||||
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
|
||||
|
||||
double cosTheta = MathUtil.cos(angle);
|
||||
double sinTheta = MathUtil.sin(angle);
|
||||
double dotProduct = this.dot(axis);
|
||||
|
||||
double xPrime = x2 * dotProduct * (1d - cosTheta)
|
||||
+ x * cosTheta
|
||||
+ (-z2 * y + y2 * z) * sinTheta;
|
||||
double yPrime = y2 * dotProduct * (1d - cosTheta)
|
||||
+ y * cosTheta
|
||||
+ (z2 * x - x2 * z) * sinTheta;
|
||||
double zPrime = z2 * dotProduct * (1d - cosTheta)
|
||||
+ z * cosTheta
|
||||
+ (-y2 * x + x2 * y) * sinTheta;
|
||||
|
||||
return setX(xPrime).setY(yPrime).setZ(zPrime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around the x axis.
|
||||
* <p>
|
||||
* This piece of math is based on the standard rotation matrix for vectors
|
||||
* in three dimensional space. This matrix can be found here:
|
||||
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
|
||||
* Matrix</a>.
|
||||
*
|
||||
* @param angle the angle to rotate the vector about. This angle is passed
|
||||
* in radians
|
||||
*
|
||||
* @return the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundX(double angle) {
|
||||
double angleCos = MathUtil.cos(angle);
|
||||
double angleSin = MathUtil.sin(angle);
|
||||
|
||||
double y = angleCos * getY() - angleSin * getZ();
|
||||
double z = angleSin * getY() + angleCos * getZ();
|
||||
return setY(y).setZ(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around the y axis.
|
||||
* <p>
|
||||
* This piece of math is based on the standard rotation matrix for vectors
|
||||
* in three dimensional space. This matrix can be found here:
|
||||
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
|
||||
* Matrix</a>.
|
||||
*
|
||||
* @param angle the angle to rotate the vector about. This angle is passed
|
||||
* in radians
|
||||
*
|
||||
* @return the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundY(double angle) {
|
||||
double angleCos = MathUtil.cos(angle);
|
||||
double angleSin = MathUtil.sin(angle);
|
||||
|
||||
double x = angleCos * getX() + angleSin * getZ();
|
||||
double z = -angleSin * getX() + angleCos * getZ();
|
||||
return setX(x).setZ(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around the z axis
|
||||
* <p>
|
||||
* This piece of math is based on the standard rotation matrix for vectors
|
||||
* in three dimensional space. This matrix can be found here:
|
||||
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
|
||||
* Matrix</a>.
|
||||
*
|
||||
* @param angle the angle to rotate the vector about. This angle is passed
|
||||
* in radians
|
||||
*
|
||||
* @return the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundZ(double angle) {
|
||||
double angleCos = MathUtil.cos(angle);
|
||||
double angleSin = MathUtil.sin(angle);
|
||||
|
||||
double x = angleCos * getX() - angleSin * getY();
|
||||
double y = angleSin * getX() + angleCos * getY();
|
||||
return setX(x).setY(y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 13;
|
||||
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
|
||||
return hash;
|
||||
}
|
||||
|
||||
public int getBlockX() {
|
||||
return (int) Math.floor(x);
|
||||
}
|
||||
|
||||
public int getBlockY() {
|
||||
return (int) Math.floor(y);
|
||||
}
|
||||
|
||||
public int getBlockZ() {
|
||||
return (int) Math.floor(z);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package com.dfsek.terra.api.util.vector;
|
||||
|
||||
|
||||
public class Vector3Int {
|
||||
private static final Vector3Int ZERO = new Vector3Int(0, 0, 0);
|
||||
private static final Vector3Int UNIT = new Vector3Int(0, 1, 0);
|
||||
protected int x, y, z;
|
||||
|
||||
protected Vector3Int(int x, int y, int z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static Vector3Int unit() {
|
||||
return UNIT;
|
||||
}
|
||||
|
||||
public static Vector3Int zero() {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
public static Vector3Int of(int x, int y, int z) {
|
||||
return new Vector3Int(x, y, z);
|
||||
}
|
||||
|
||||
public static Vector3Int of(Vector3Int origin, int x, int y, int z) {
|
||||
return new Vector3Int(origin.x + x, origin.y + y, origin.z + z);
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public Mutable mutable() {
|
||||
return new Mutable(x, y, z);
|
||||
}
|
||||
|
||||
public Vector3 toVector3() {
|
||||
return Vector3.of(x, y, z);
|
||||
}
|
||||
|
||||
public Vector3.Mutable toVector3Mutable() {
|
||||
return Vector3.Mutable.of(x, y, z);
|
||||
}
|
||||
|
||||
public static class Mutable extends Vector3Int {
|
||||
protected Mutable(int x, int y, int z) {
|
||||
super(x, y, z);
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setZ(int z) {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public Vector3Int immutable() {
|
||||
return Vector3Int.of(x, y, z);
|
||||
}
|
||||
|
||||
public Mutable add(int x, int y, int z) {
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
this.z += z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3 toVector3() {
|
||||
return Vector3.of(x, y, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.EntityType;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
import com.dfsek.seismic.type.vector.Vector3Int;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
import com.dfsek.terra.api.world.util.Interceptors;
|
||||
|
||||
@@ -2,8 +2,8 @@ package com.dfsek.terra.api.world;
|
||||
|
||||
import com.dfsek.terra.api.block.entity.BlockEntity;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3Int;
|
||||
|
||||
|
||||
/**
|
||||
@@ -29,7 +29,7 @@ public interface ReadableWorld extends World {
|
||||
* @return {@link BlockState} at coordinates.
|
||||
*/
|
||||
default BlockState getBlockState(Vector3 position) {
|
||||
return getBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ());
|
||||
return getBlockState(position.getFloorX(), position.getFloorY(), position.getFloorZ());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,7 +46,7 @@ public interface ReadableWorld extends World {
|
||||
BlockEntity getBlockEntity(int x, int y, int z);
|
||||
|
||||
default BlockEntity getBlockEntity(Vector3 position) {
|
||||
return getBlockEntity(position.getBlockX(), position.getBlockY(), position.getBlockZ());
|
||||
return getBlockEntity(position.getFloorX(), position.getFloorY(), position.getFloorZ());
|
||||
}
|
||||
|
||||
default BlockEntity getBlockEntity(Vector3Int position) {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
package com.dfsek.terra.api.world;
|
||||
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.chunk.Chunk;
|
||||
|
||||
|
||||
@@ -15,6 +15,6 @@ public interface ServerWorld extends WritableWorld {
|
||||
Chunk getChunkAt(int x, int z);
|
||||
|
||||
default Chunk getChunkAt(Vector3 location) {
|
||||
return getChunkAt(location.getBlockX() >> 4, location.getBlockZ() >> 4);
|
||||
return getChunkAt(location.getFloorX() >> 4, location.getFloorZ() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,18 @@ package com.dfsek.terra.api.world;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.EntityType;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3Int;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.Column;
|
||||
|
||||
|
||||
public interface WritableWorld extends ReadableWorld {
|
||||
default void setBlockState(Vector3 position, BlockState data, boolean physics) {
|
||||
setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics);
|
||||
setBlockState(position.getFloorX(), position.getFloorY(), position.getFloorZ(), data, physics);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3.Mutable position, BlockState data, boolean physics) {
|
||||
setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics);
|
||||
setBlockState(position.getFloorX(), position.getFloorY(), position.getFloorZ(), data, physics);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3Int position, BlockState data, boolean physics) {
|
||||
|
||||
@@ -16,8 +16,8 @@ import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import com.dfsek.terra.api.util.Column;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3Int;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
|
||||
@@ -49,7 +49,7 @@ public interface BiomeProvider {
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
default Biome getBiome(Vector3 vector3, long seed) {
|
||||
return getBiome(vector3.getBlockX(), vector3.getBlockY(), vector3.getBlockZ(), seed);
|
||||
return getBiome(vector3.getFloorX(), vector3.getFloorY(), vector3.getFloorZ(), seed);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,8 +10,8 @@ package com.dfsek.terra.api.world.chunk.generation;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.util.vector.Vector3Int;
|
||||
import com.dfsek.seismic.type.vector.Vector3;
|
||||
import com.dfsek.seismic.type.vector.Vector3Int;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.Palette;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
@@ -24,7 +24,7 @@ public interface ChunkGenerator {
|
||||
BlockState getBlock(WorldProperties world, int x, int y, int z, BiomeProvider biomeProvider);
|
||||
|
||||
default BlockState getBlock(WorldProperties world, Vector3 vector3, BiomeProvider biomeProvider) {
|
||||
return getBlock(world, vector3.getBlockX(), vector3.getBlockY(), vector3.getBlockZ(), biomeProvider);
|
||||
return getBlock(world, vector3.getFloorX(), vector3.getFloorY(), vector3.getFloorZ(), biomeProvider);
|
||||
}
|
||||
|
||||
default BlockState getBlock(WorldProperties world, Vector3Int vector3, BiomeProvider biomeProvider) {
|
||||
|
||||
Reference in New Issue
Block a user