put vector3 in class in API

This commit is contained in:
dfsek
2021-06-30 10:39:03 -07:00
parent abd3683a49
commit bf5e8d903c
107 changed files with 552 additions and 544 deletions

View File

@@ -10,6 +10,8 @@ import com.dfsek.terra.api.lang.Language;
import com.dfsek.terra.api.profiler.Profiler;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.tectonic.LoaderHolder;
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
import com.dfsek.terra.api.util.JarUtil;
import com.dfsek.terra.api.world.TerraWorld;
import com.dfsek.terra.api.world.World;
@@ -22,7 +24,7 @@ import java.util.jar.JarFile;
/**
* Represents a Terra mod/plugin instance.
*/
public interface TerraPlugin extends LoaderRegistrar {
public interface TerraPlugin extends LoaderRegistrar, LoaderHolder {
WorldHandle getWorldHandle();
TerraWorld getWorld(World world);

View File

@@ -1,6 +1,7 @@
package com.dfsek.terra.api.config;
import com.dfsek.terra.api.LoaderRegistrar;
import com.dfsek.terra.api.tectonic.LoaderHolder;
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
import com.dfsek.terra.api.addon.TerraAddon;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.world.TerraWorld;
@@ -9,8 +10,7 @@ import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import java.util.Map;
import java.util.Set;
public interface ConfigPack extends LoaderRegistrar {
@SuppressWarnings("unchecked")
public interface ConfigPack extends LoaderRegistrar, LoaderHolder {
<T> CheckedRegistry<T> getRegistry(Class<T> clazz);
BiomeProvider.BiomeProviderBuilder getBiomeProviderBuilder();

View File

@@ -30,4 +30,28 @@ public interface NoiseSampler {
double getNoiseSeeded(int seed, double x, double y);
double getNoiseSeeded(int seed, double x, double y, double z);
static NoiseSampler zero() {
return new NoiseSampler() {
@Override
public double getNoise(double x, double y) {
return 0;
}
@Override
public double getNoise(double x, double y, double z) {
return 0;
}
@Override
public double getNoiseSeeded(int seed, double x, double y) {
return 0;
}
@Override
public double getNoiseSeeded(int seed, double x, double y, double z) {
return 0;
}
};
}
}

View File

@@ -0,0 +1,20 @@
package com.dfsek.terra.api.tectonic;
import com.dfsek.tectonic.abstraction.TemplateProvider;
import com.dfsek.tectonic.loading.TypeLoader;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import java.lang.reflect.Type;
import java.util.function.Supplier;
public interface LoaderHolder {
<T> LoaderHolder applyLoader(Type type, TypeLoader<T> loader);
default <T> LoaderHolder applyLoader(Class<? extends T> type, TypeLoader<T> loader) {
return applyLoader((Type) type, loader);
}
<T> LoaderHolder applyLoader(Type type, TemplateProvider<ObjectTemplate<T>> loader);
default <T> LoaderHolder applyLoader(Class<? extends T> type, TemplateProvider<ObjectTemplate<T>> loader) {
return applyLoader((Type) type, loader);
}
}

View File

@@ -1,4 +1,4 @@
package com.dfsek.terra.api;
package com.dfsek.terra.api.tectonic;
import com.dfsek.tectonic.loading.TypeRegistry;

View File

@@ -0,0 +1,9 @@
package com.dfsek.terra.api.util.seeded;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import java.util.function.Supplier;
@FunctionalInterface
public interface NoiseProvider extends Supplier<ObjectTemplate<NoiseSeeded>> {
}

View File

@@ -0,0 +1,10 @@
package com.dfsek.terra.api.util.seeded;
import com.dfsek.terra.api.noise.NoiseSampler;
public interface NoiseSeeded extends SeededBuilder<NoiseSampler> {
@Override
NoiseSampler apply(Long seed);
int getDimensions();
}

View File

@@ -0,0 +1,8 @@
package com.dfsek.terra.api.util.seeded;
import java.util.function.Function;
@FunctionalInterface
public interface SeededBuilder<T> extends Function<Long, T> {
}

View File

@@ -0,0 +1,7 @@
package com.dfsek.terra.api.util.seeded;
import com.dfsek.terra.api.world.biome.generation.pipeline.BiomeSource;
@FunctionalInterface
public interface SourceSeeded extends SeededBuilder<BiomeSource> {
}

View File

@@ -0,0 +1,7 @@
package com.dfsek.terra.api.util.seeded;
import com.dfsek.terra.api.world.biome.generation.pipeline.Stage;
@FunctionalInterface
public interface StageSeeded extends SeededBuilder<Stage> {
}

View File

@@ -1,47 +1,115 @@
package com.dfsek.terra.api.vector;
import com.dfsek.terra.api.util.MathUtil;
import com.dfsek.terra.api.world.World;
import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
public interface Vector3 extends Cloneable {
double getZ();
public class Vector3 implements Cloneable {
private double x;
private double y;
private double z;
Vector3 setZ(double z);
public Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
double getX();
public double getZ() {
return z;
}
Vector3 setX(double x);
public Vector3 setZ(double z) {
this.z = z;
return this;
}
double getY();
public double getX() {
return x;
}
Vector3 setY(double y);
public Vector3 setX(double x) {
this.x = x;
return this;
}
int getBlockX();
public double getY() {
return y;
}
int getBlockY();
public Vector3 setY(double y) {
this.y = y;
return this;
}
int getBlockZ();
public int getBlockX() {
return FastMath.floorToInt(x);
}
Vector3 multiply(double m);
public int getBlockY() {
return FastMath.floorToInt(y);
}
Vector3 add(double x, double y, double z);
public int getBlockZ() {
return FastMath.floorToInt(z);
}
Vector3 add(Vector3 other);
public Vector3 multiply(double m) {
x *= m;
y *= m;
z *= m;
return this;
}
Vector3 add(Vector2 other);
public Vector3 add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
double lengthSquared();
public Vector3 add(Vector3 other) {
this.x += other.getX();
this.y += other.getY();
this.z += other.getZ();
return this;
}
double length();
public Vector3 add(Vector2 other) {
this.x += other.getX();
this.z += other.getZ();
return this;
}
double inverseLength();
public double lengthSquared() {
return x * x + y * y + z * z;
}
public Vector3 clone() {
try {
return (Vector3) super.clone();
} catch(CloneNotSupportedException e) {
throw new Error(e);
}
}
public double length() {
return FastMath.sqrt(lengthSquared());
}
public double inverseLength() {
return FastMath.invSqrtQuick(lengthSquared());
}
/**
* Returns if a vector is normalized
*
* @return whether the vector is normalised
*/
boolean isNormalized();
public boolean isNormalized() {
return MathUtil.equals(this.lengthSquared(), 1);
}
/**
* Rotates the vector around the x axis.
@@ -55,7 +123,15 @@ public interface Vector3 extends Cloneable {
* in radians
* @return the same vector
*/
@NotNull Vector3 rotateAroundX(double angle);
@NotNull
public Vector3 rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.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.
@@ -69,7 +145,15 @@ public interface Vector3 extends Cloneable {
* in radians
* @return the same vector
*/
@NotNull Vector3 rotateAroundY(double angle);
@NotNull
public Vector3 rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.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
@@ -83,7 +167,15 @@ public interface Vector3 extends Cloneable {
* in radians
* @return the same vector
*/
@NotNull Vector3 rotateAroundZ(double angle);
@NotNull
public Vector3 rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double x = angleCos * getX() - angleSin * getY();
double y = angleSin * getX() + angleCos * getY();
return setX(x).setY(y);
}
/**
* Get the distance between this vector and another. The value of this
@@ -95,7 +187,9 @@ public interface Vector3 extends Cloneable {
* @param o The other vector
* @return the distance
*/
double distance(@NotNull Vector3 o);
public double distance(@NotNull Vector3 o) {
return FastMath.sqrt(FastMath.pow2(x - o.getX()) + FastMath.pow2(y - o.getY()) + FastMath.pow2(z - o.getZ()));
}
/**
* Get the squared distance between this vector and another.
@@ -103,7 +197,9 @@ public interface Vector3 extends Cloneable {
* @param o The other vector
* @return the distance
*/
double distanceSquared(@NotNull Vector3 o);
public double distanceSquared(@NotNull Vector3 o) {
return FastMath.pow2(x - o.getX()) + FastMath.pow2(y - o.getY()) + FastMath.pow2(z - o.getZ());
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
@@ -124,7 +220,10 @@ public interface Vector3 extends Cloneable {
* @throws IllegalArgumentException if the provided axis vector instance is
* null
*/
@NotNull Vector3 rotateAroundAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException;
@NotNull
public Vector3 rotateAroundAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.clone().normalize(), angle);
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
@@ -144,7 +243,27 @@ public interface Vector3 extends Cloneable {
* @throws IllegalArgumentException if the provided axis vector instance is
* null
*/
@NotNull Vector3 rotateAroundNonUnitAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException;
@NotNull
public Vector3 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 = Math.cos(angle);
double sinTheta = Math.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);
}
/**
* Calculates the dot product of this vector with another. The dot product
@@ -153,13 +272,59 @@ public interface Vector3 extends Cloneable {
* @param other The other vector
* @return dot product
*/
double dot(@NotNull Vector3 other);
public double dot(@NotNull Vector3 other) {
return x * other.getX() + y * other.getY() + z * other.getZ();
}
Vector3 normalize();
public Vector3 normalize() {
return this.multiply(this.inverseLength());
}
Vector3 subtract(int x, int y, int z);
public Vector3 subtract(int x, int y, int z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
Vector3 subtract(Vector3 end);
public Vector3 subtract(Vector3 end) {
x -= end.getX();
y -= end.getY();
z -= end.getZ();
return this;
}
Vector3 clone();
/**
* 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)) return false;
Vector3 other = (Vector3) obj;
return MathUtil.equals(x, other.getX()) && MathUtil.equals(y, other.getY()) && MathUtil.equals(z, other.getZ());
}
@Override
public String toString() {
return "(" + getX() + ", " + getY() + ", " + getZ() + ")";
}
}