Fix fractal trees

This commit is contained in:
dfsek 2020-12-18 16:55:36 -07:00
commit 9ac098f1ca
13 changed files with 126 additions and 215 deletions

View File

@ -1,6 +1,6 @@
import com.dfsek.terra.getGitHash import com.dfsek.terra.getGitHash
val versionObj = Version("2", "2", "0", true) val versionObj = Version("3", "0", "0", true)
allprojects { allprojects {
version = versionObj version = versionObj

View File

@ -704,7 +704,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xy = x + y; double xy = x + y;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xy * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xy * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
z *= 0.577350269189626; z *= 0.577350269189626;
x += s2 - z; x += s2 - z;
@ -717,7 +717,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xz = x + z; double xz = x + z;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xz * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xz * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
y *= 0.577350269189626; y *= 0.577350269189626;
x += s2 - y; x += s2 - y;
@ -1898,7 +1898,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xy = xs + ys; double xy = xs + ys;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xy * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xy * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
zs *= 0.577350269189626; zs *= 0.577350269189626;
xs += s2 - zs; xs += s2 - zs;
@ -1911,7 +1911,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xz = xs + zs; double xz = xs + zs;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xz * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xz * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
ys *= 0.577350269189626; ys *= 0.577350269189626;
xs += s2 - ys; xs += s2 - ys;
@ -1989,7 +1989,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xy = xs + ys; double xy = xs + ys;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xy * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xy * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
zs *= 0.577350269189626; zs *= 0.577350269189626;
xs += s2 - zs; xs += s2 - zs;
@ -2002,7 +2002,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xz = xs + zs; double xz = xs + zs;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xz * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xz * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
ys *= 0.577350269189626; ys *= 0.577350269189626;
xs += s2 - ys; xs += s2 - ys;
@ -2081,7 +2081,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xy = xs + ys; double xy = xs + ys;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xy * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xy * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
zs *= 0.577350269189626; zs *= 0.577350269189626;
xs += s2 - zs; xs += s2 - zs;
@ -2094,7 +2094,7 @@ public class FastNoiseLite {
/*FNLdouble*/ /*FNLdouble*/
double xz = xs + zs; double xz = xs + zs;
/*FNLdouble*/ /*FNLdouble*/
double s2 = xz * -(/*FNLdouble*/ double) 0.211324865405187; double s2 = xz * -0.211324865405187D;
/*FNLdouble*/ /*FNLdouble*/
ys *= 0.577350269189626; ys *= 0.577350269189626;
xs += s2 - ys; xs += s2 - ys;

View File

@ -1,70 +1,27 @@
package com.dfsek.terra.api.gaea.tree.fractal; package com.dfsek.terra.api.gaea.tree.fractal;
import com.dfsek.terra.api.gaea.util.GlueList;
import com.dfsek.terra.api.generic.Entity; import com.dfsek.terra.api.generic.Entity;
import com.dfsek.terra.api.generic.TerraPlugin; import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.api.generic.world.block.BlockData; import com.dfsek.terra.api.generic.world.block.BlockData;
import com.dfsek.terra.api.generic.world.block.MaterialData;
import com.dfsek.terra.api.generic.world.vector.Location; import com.dfsek.terra.api.generic.world.vector.Location;
import com.dfsek.terra.util.MaterialSet; import com.dfsek.terra.util.MaterialSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random; import java.util.Random;
import java.util.function.Consumer; import java.util.function.Consumer;
public abstract class FractalTree { public abstract class FractalTree {
private final Map<Location, BlockData> treeAssembler = new HashMap<>();
private final List<EntitySpawnHolder> entities = new GlueList<>();
protected final Location origin;
protected final Random random;
protected final TerraPlugin main; protected final TerraPlugin main;
public abstract MaterialSet getSpawnable(); public abstract MaterialSet getSpawnable();
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public FractalTree(Location origin, Random random, TerraPlugin plugin) { public FractalTree(TerraPlugin plugin) {
this.origin = origin.add(0, 1, 0);
this.random = random;
this.main = plugin; this.main = plugin;
} }
/**
* Gets the raw tree map.
*
* @return HashMap&lt;Location, BlockData&gt; - The raw dictionary representation of the tree.
*/
public Map<Location, BlockData> getTree() {
return treeAssembler;
}
/**
* Fetches the Random object used to generate the tree.
*
* @return Random - The Random object.
*/
public Random getRandom() {
return random;
}
/**
* Fetches the origin location.
*
* @return Location - The origin location specified upon instantiation.
*/
public Location getOrigin() {
return origin;
}
/** /**
* Sets a block in the tree's storage map to a material. * Sets a block in the tree's storage map to a material.
* *
@ -72,7 +29,7 @@ public abstract class FractalTree {
* @param m - The material to which it will be set. * @param m - The material to which it will be set.
*/ */
public void setBlock(Location l, BlockData m) { public void setBlock(Location l, BlockData m) {
treeAssembler.put(l, m); l.getBlock().setBlockData(m, false);
} }
public TerraPlugin getMain() { public TerraPlugin getMain() {
@ -82,34 +39,9 @@ public abstract class FractalTree {
/** /**
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
*/ */
public abstract void grow(); public abstract void grow(Location origin, Random random);
/** public void spawnEntity(Location spawn, Class<Entity> clazz, Consumer<Entity> consumer) {
* Pastes the tree in the world. Must be invoked from main thread. spawn.getWorld().spawn(spawn, clazz, consumer);
*/
@SuppressWarnings("unchecked")
public void plant() {
for(Map.Entry<Location, BlockData> entry : treeAssembler.entrySet()) {
entry.getKey().getBlock().setBlockData(entry.getValue(), false);
}
for(EntitySpawnHolder e : entities) {
Objects.requireNonNull(e.getLocation().getWorld()).spawn(e.getLocation(), e.getEntity(), e.getConsumer());
}
}
@SuppressWarnings("rawtypes, unchecked")
public void spawnEntity(Location spawn, Class clazz, Consumer<Entity> consumer) {
entities.add(new EntitySpawnHolder(spawn, clazz, consumer));
}
/**
* Gets the material at the specified block.
* Returns air if no material has been set.
*
* @param l - The location at which to check.
* @return Material - The material at the specified block.
*/
public MaterialData getMaterial(Location l) {
return treeAssembler.getOrDefault(l, main.getWorldHandle().createBlockData("minecraft:air")).getMaterial();
} }
} }

View File

@ -5,6 +5,8 @@ import com.dfsek.terra.api.generic.world.block.BlockData;
import com.dfsek.terra.api.generic.world.vector.Location; import com.dfsek.terra.api.generic.world.vector.Location;
import com.dfsek.terra.api.generic.world.vector.Vector3; import com.dfsek.terra.api.generic.world.vector.Vector3;
import java.util.Random;
public class TreeGeometry { public class TreeGeometry {
private final FractalTree tree; private final FractalTree tree;
@ -33,19 +35,19 @@ public class TreeGeometry {
for(int y = - radius; y <= radius; y++) { for(int y = - radius; y <= radius; y++) {
for(int z = - radius; z <= radius; z++) { for(int z = - radius; z <= radius; z++) {
Vector3 position = l.toVector().clone().add(new Vector3(x, y, z)); Vector3 position = l.toVector().clone().add(new Vector3(x, y, z));
if(l.toVector().distance(position) <= radius + 0.5 && (overwrite || tree.getMaterial(position.toLocation(l.getWorld())).isAir())) if(l.toVector().distance(position) <= radius + 0.5 && (overwrite || position.toLocation(l.getWorld()).getBlock().isEmpty()))
tree.setBlock(position.toLocation(l.getWorld()), m.get()); tree.setBlock(position.toLocation(l.getWorld()), m.get());
} }
} }
} }
} }
public void generateSponge(Location l, ProbabilityCollection<BlockData> m, int radius, boolean overwrite, int sponginess) { public void generateSponge(Location l, ProbabilityCollection<BlockData> m, int radius, boolean overwrite, int sponginess, Random r) {
for(int x = - radius; x <= radius; x++) { for(int x = -radius; x <= radius; x++) {
for(int y = - radius; y <= radius; y++) { for(int y = -radius; y <= radius; y++) {
for(int z = - radius; z <= radius; z++) { for(int z = -radius; z <= radius; z++) {
Vector3 position = l.toVector().clone().add(new Vector3(x, y, z)); Vector3 position = l.toVector().clone().add(new Vector3(x, y, z));
if(tree.getRandom().nextInt(100) < sponginess && l.toVector().distance(position) <= radius + 0.5 && (overwrite || tree.getMaterial(position.toLocation(l.getWorld())).isAir())) if(r.nextInt(100) < sponginess && l.toVector().distance(position) <= radius + 0.5 && (overwrite || position.toLocation(l.getWorld()).getBlock().isEmpty()))
tree.setBlock(position.toLocation(l.getWorld()), m.get()); tree.setBlock(position.toLocation(l.getWorld()), m.get());
} }
} }
@ -57,7 +59,7 @@ public class TreeGeometry {
for(int y = 0; y <= height; y++) { for(int y = 0; y <= height; y++) {
for(int z = - radius; z <= radius; z++) { for(int z = - radius; z <= radius; z++) {
Vector3 position = l.toVector().clone().add(new Vector3(x, 0, z)); Vector3 position = l.toVector().clone().add(new Vector3(x, 0, z));
if(l.toVector().distance(position) <= radius + 0.5 && (overwrite || tree.getMaterial(position.toLocation(l.getWorld())).isAir())) if(l.toVector().distance(position) <= radius + 0.5 && (overwrite || position.toLocation(l.getWorld()).getBlock().isEmpty()))
tree.setBlock(position.toLocation(l.getWorld()), m.get()); tree.setBlock(position.toLocation(l.getWorld()), m.get());
} }
} }

View File

@ -18,21 +18,18 @@ public class Cactus extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public Cactus(Location origin, Random random, TerraPlugin main) { public Cactus(TerraPlugin main) {
super(origin, random, main); super(main);
} }
/** /**
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
BlockData cactus = getMain().getWorldHandle().createBlockData("minecraft:cactus"); BlockData cactus = getMain().getWorldHandle().createBlockData("minecraft:cactus");
int h = super.getRandom().nextInt(4) + 1; int h = random.nextInt(4) + 1;
for(int i = 0; i < h; i++) setBlock(super.getOrigin().clone().add(0, i, 0), cactus); for(int i = 0; i < h; i++) setBlock(origin.clone().add(0, i, 0), cactus);
} }
} }

View File

@ -27,34 +27,31 @@ public class IceSpike extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public IceSpike(Location origin, Random random, TerraPlugin main) { public IceSpike(TerraPlugin main) {
super(origin, random, main); super(main);
geo = new TreeGeometry(this); geo = new TreeGeometry(this);
WorldHandle handle = main.getWorldHandle(); WorldHandle handle = main.getWorldHandle();
ice = new ProbabilityCollection<BlockData>().add(handle.createBlockData("minecraft:packed_ice"), 95).add(handle.createBlockData("minecraft:blue_ice"), 5); ice = new ProbabilityCollection<BlockData>().add(handle.createBlockData("minecraft:packed_ice"), 95).add(handle.createBlockData("minecraft:blue_ice"), 5);
} }
private double getOffset() { private double getOffset(Random r) {
return (getRandom().nextDouble() - 0.5D); return (r.nextDouble() - 0.5D);
} }
/** /**
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
Vector3 direction = new Vector3(getOffset(), 0, getOffset()); Vector3 direction = new Vector3(getOffset(random), 0, getOffset(random));
Location l1 = super.getOrigin().clone(); Location l1 = origin.clone();
int h = super.getRandom().nextInt(16) + 8; int h = random.nextInt(16) + 8;
for(int i = 0; i < h; i++) { for(int i = 0; i < h; i++) {
geo.generateSponge(l1.clone().add(0, i, 0).add(direction.clone().multiply(i)), ice, (int) ((1 - ((double) i / h)) * 2 + 1), true, 80); geo.generateSponge(l1.clone().add(0, i, 0).add(direction.clone().multiply(i)), ice, (int) ((1 - ((double) i / h)) * 2 + 1), true, 80, random);
} }
for(int i = 0; i < h/3; i++) { for(int i = 0; i < h / 3; i++) {
setBlock(l1.clone().add(0, h + i, 0).add(direction.clone().multiply(h + i)), ice.get(super.getRandom())); setBlock(l1.clone().add(0, h + i, 0).add(direction.clone().multiply(h + i)), ice.get(random));
} }
} }
} }

View File

@ -23,12 +23,9 @@ public class OakTree extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public OakTree(Location origin, Random random, TerraPlugin main) { public OakTree(TerraPlugin main) {
super(origin, random, main); super(main);
geo = new TreeGeometry(this); geo = new TreeGeometry(this);
} }
@ -36,15 +33,15 @@ public class OakTree extends FractalTree {
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
growBranch(super.getOrigin().clone(), new Vector3(super.getRandom().nextInt(5) - 2, super.getRandom().nextInt(4) + 6, super.getRandom().nextInt(5) - 2), 2, 0); growBranch(origin.clone(), new Vector3(random.nextInt(5) - 2, random.nextInt(4) + 6, random.nextInt(5) - 2), 2, 0, random);
} }
private void growBranch(Location l1, Vector3 diff, double d1, int recursions) { private void growBranch(Location l1, Vector3 diff, double d1, int recursions, Random r) {
BlockData wood = getMain().getWorldHandle().createBlockData("minecraft:oak_wood"); BlockData wood = getMain().getWorldHandle().createBlockData("minecraft:oak_wood");
BlockData leaves = getMain().getWorldHandle().createBlockData("minecraft:oak_leave"); BlockData leaves = getMain().getWorldHandle().createBlockData("minecraft:oak_leave");
if(recursions > 1) { if(recursions > 1) {
geo.generateSphere(l1, leaves, 1 + super.getRandom().nextInt(2) + (3 - recursions), false); geo.generateSphere(l1, leaves, 1 + r.nextInt(2) + (3 - recursions), false);
if(recursions > 2) return; if(recursions > 2) return;
} }
if(diff.getY() < 0) diff.rotateAroundAxis(TreeGeometry.getPerpendicular(diff.clone()).normalize(), FastMath.PI); if(diff.getY() < 0) diff.rotateAroundAxis(TreeGeometry.getPerpendicular(diff.clone()).normalize(), FastMath.PI);
@ -53,17 +50,17 @@ public class OakTree extends FractalTree {
geo.generateSphere(l1.clone().add(diff.clone().multiply((double) i / d)), wood, FastMath.max((int) d1, 0), true); geo.generateSphere(l1.clone().add(diff.clone().multiply((double) i / d)), wood, FastMath.max((int) d1, 0), true);
} }
double runningAngle = (double) 45 / (recursions + 1); double runningAngle = (double) 45 / (recursions + 1);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundX(FastMath.toRadians(runningAngle + getNoise())).rotateAroundZ(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundX(FastMath.toRadians(runningAngle + getNoise(r))).rotateAroundZ(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundX(FastMath.toRadians(- runningAngle + getNoise())).rotateAroundZ(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundX(FastMath.toRadians(-runningAngle + getNoise(r))).rotateAroundZ(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundZ(FastMath.toRadians(runningAngle + getNoise())).rotateAroundX(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundZ(FastMath.toRadians(runningAngle + getNoise(r))).rotateAroundX(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundZ(FastMath.toRadians(- runningAngle + getNoise())).rotateAroundX(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.75).rotateAroundZ(FastMath.toRadians(-runningAngle + getNoise(r))).rotateAroundX(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
} }
private int getNoise() { private int getNoise(Random r) {
return super.getRandom().nextInt(60) - 30; return r.nextInt(60) - 30;
} }
} }

View File

@ -17,41 +17,40 @@ public class ShatteredPillar extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public ShatteredPillar(Location origin, Random random, TerraPlugin main) { public ShatteredPillar(TerraPlugin main) {
super(origin, random, main); super(main);
} }
/** /**
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
* @param origin
* @param random
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
BlockData obsidian = getMain().getWorldHandle().createBlockData("minecraft:obsidian"); BlockData obsidian = getMain().getWorldHandle().createBlockData("minecraft:obsidian");
int h = super.getRandom().nextInt(5) + 8; int h = random.nextInt(5) + 8;
int max = h; int max = h;
for(int i = - h; i < h; i++) setBlock(super.getOrigin().clone().add(0, i, 0), obsidian); for(int i = -h; i < h; i++) setBlock(origin.clone().add(0, i, 0), obsidian);
h = h + (getRandom().nextBoolean() ? getRandom().nextInt(3) + 1 : - (getRandom().nextInt(3) + 1)); h = h + (random.nextBoolean() ? random.nextInt(3) + 1 : -(random.nextInt(3) + 1));
int[] crystalLoc = new int[] {0, 0}; int[] crystalLoc = new int[] {0, 0};
if(h > max) { if(h > max) {
max = h; max = h;
crystalLoc = new int[] {1, 0}; crystalLoc = new int[] {1, 0};
} }
for(int i = - h; i < h; i++) setBlock(super.getOrigin().clone().add(1, i, 0), obsidian); for(int i = -h; i < h; i++) setBlock(origin.clone().add(1, i, 0), obsidian);
h = h + (getRandom().nextBoolean() ? getRandom().nextInt(3) + 1 : - (getRandom().nextInt(3) + 1)); h = h + (random.nextBoolean() ? random.nextInt(3) + 1 : -(random.nextInt(3) + 1));
if(h > max) { if(h > max) {
max = h; max = h;
crystalLoc = new int[] {0, 1}; crystalLoc = new int[] {0, 1};
} }
for(int i = - h; i < h; i++) setBlock(super.getOrigin().clone().add(0, i, 1), obsidian); for(int i = -h; i < h; i++) setBlock(origin.clone().add(0, i, 1), obsidian);
h = h + (getRandom().nextBoolean() ? getRandom().nextInt(3) + 1 : - (getRandom().nextInt(3) + 1)); h = h + (random.nextBoolean() ? random.nextInt(3) + 1 : -(random.nextInt(3) + 1));
if(h > max) { if(h > max) {
max = h; max = h;
crystalLoc = new int[] {1, 1}; crystalLoc = new int[] {1, 1};
} }
for(int i = - h; i < h; i++) setBlock(super.getOrigin().clone().add(1, i, 1), obsidian); for(int i = -h; i < h; i++) setBlock(origin.clone().add(1, i, 1), obsidian);
} }
} }

View File

@ -25,12 +25,9 @@ public class ShatteredTree extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public ShatteredTree(Location origin, Random random, TerraPlugin main) { public ShatteredTree(TerraPlugin main) {
super(origin, random, main); super(main);
geo = new TreeGeometry(this); geo = new TreeGeometry(this);
WorldHandle handle = main.getWorldHandle(); WorldHandle handle = main.getWorldHandle();
@ -46,14 +43,14 @@ public class ShatteredTree extends FractalTree {
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
growBranch(super.getOrigin().clone(), new Vector3(super.getRandom().nextInt(5) - 2, super.getRandom().nextInt(4) + 6, super.getRandom().nextInt(5) - 2), 1, 0); growBranch(origin.clone(), new Vector3(random.nextInt(5) - 2, random.nextInt(4) + 6, random.nextInt(5) - 2), 1, 0, random);
} }
private void growBranch(Location l1, Vector3 diff, double d1, int recursions) { private void growBranch(Location l1, Vector3 diff, double d1, int recursions, Random r) {
if(recursions > 2) { if(recursions > 2) {
geo.generateSphere(l1, leaves, 1 + super.getRandom().nextInt(2), false); geo.generateSphere(l1, leaves, 1 + r.nextInt(2), false);
return; return;
} }
if(diff.getY() < 0) diff.rotateAroundAxis(TreeGeometry.getPerpendicular(diff.clone()).normalize(), FastMath.PI); if(diff.getY() < 0) diff.rotateAroundAxis(TreeGeometry.getPerpendicular(diff.clone()).normalize(), FastMath.PI);
@ -62,17 +59,17 @@ public class ShatteredTree extends FractalTree {
geo.generateSphere(l1.clone().add(diff.clone().multiply((double) i / d)), bark, FastMath.max((int) d1, 0), true); geo.generateSphere(l1.clone().add(diff.clone().multiply((double) i / d)), bark, FastMath.max((int) d1, 0), true);
} }
double runningAngle = (double) 45 / (recursions + 1); double runningAngle = (double) 45 / (recursions + 1);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundX(FastMath.toRadians(runningAngle + getNoise())).rotateAroundZ(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundX(FastMath.toRadians(runningAngle + getNoise(r))).rotateAroundZ(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundX(FastMath.toRadians(- runningAngle + getNoise())).rotateAroundZ(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundX(FastMath.toRadians(-runningAngle + getNoise(r))).rotateAroundZ(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundZ(FastMath.toRadians(runningAngle + getNoise())).rotateAroundX(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundZ(FastMath.toRadians(runningAngle + getNoise(r))).rotateAroundX(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundZ(FastMath.toRadians(- runningAngle + getNoise())).rotateAroundX(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.9).rotateAroundZ(FastMath.toRadians(-runningAngle + getNoise(r))).rotateAroundX(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
} }
private int getNoise() { private int getNoise(Random r) {
return super.getRandom().nextInt(90) - 45; return r.nextInt(90) - 45;
} }
} }

View File

@ -17,21 +17,20 @@ public class SmallShatteredPillar extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public SmallShatteredPillar(Location origin, Random random, TerraPlugin main) { public SmallShatteredPillar(TerraPlugin main) {
super(origin, random, main); super(main);
} }
/** /**
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
* @param origin
* @param random
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
int h = super.getRandom().nextInt(5) + 5; int h = random.nextInt(5) + 5;
BlockData obsidian = getMain().getWorldHandle().createBlockData("minecraft:obsidian"); BlockData obsidian = getMain().getWorldHandle().createBlockData("minecraft:obsidian");
for(int i = - h; i < h; i++) setBlock(super.getOrigin().clone().add(0, i, 0), obsidian); for(int i = -h; i < h; i++) setBlock(origin.clone().add(0, i, 0), obsidian);
} }
} }

View File

@ -25,12 +25,9 @@ public class SmallShatteredTree extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public SmallShatteredTree(Location origin, Random random, TerraPlugin main) { public SmallShatteredTree(TerraPlugin main) {
super(origin, random, main); super(main);
geo = new TreeGeometry(this); geo = new TreeGeometry(this);
WorldHandle handle = main.getWorldHandle(); WorldHandle handle = main.getWorldHandle();
@ -46,14 +43,14 @@ public class SmallShatteredTree extends FractalTree {
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
growBranch(super.getOrigin().clone(), new Vector3(super.getRandom().nextInt(5) - 2, super.getRandom().nextInt(3) + 4, super.getRandom().nextInt(5) - 2), 1.5, 0); growBranch(origin.clone(), new Vector3(random.nextInt(5) - 2, random.nextInt(3) + 4, random.nextInt(5) - 2), 1.5, 0, random);
} }
private void growBranch(Location l1, Vector3 diff, double d1, int recursions) { private void growBranch(Location l1, Vector3 diff, double d1, int recursions, Random r) {
if(recursions > 2) { if(recursions > 2) {
geo.generateSphere(l1, leaves, 1 + super.getRandom().nextInt(2) + (3 - recursions), false); geo.generateSphere(l1, leaves, 1 + r.nextInt(2) + (3 - recursions), false);
return; return;
} }
if(diff.getY() < 0) diff.rotateAroundAxis(TreeGeometry.getPerpendicular(diff.clone()).normalize(), FastMath.PI); if(diff.getY() < 0) diff.rotateAroundAxis(TreeGeometry.getPerpendicular(diff.clone()).normalize(), FastMath.PI);
@ -62,17 +59,17 @@ public class SmallShatteredTree extends FractalTree {
geo.generateSphere(l1.clone().add(diff.clone().multiply((double) i / d)), bark, FastMath.max((int) d1, 0), true); geo.generateSphere(l1.clone().add(diff.clone().multiply((double) i / d)), bark, FastMath.max((int) d1, 0), true);
} }
double runningAngle = (double) 45 / (recursions + 1); double runningAngle = (double) 45 / (recursions + 1);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundX(FastMath.toRadians(runningAngle + getNoise())).rotateAroundZ(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundX(FastMath.toRadians(runningAngle + getNoise(r))).rotateAroundZ(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundX(FastMath.toRadians(- runningAngle + getNoise())).rotateAroundZ(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundX(FastMath.toRadians(-runningAngle + getNoise(r))).rotateAroundZ(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundZ(FastMath.toRadians(runningAngle + getNoise())).rotateAroundX(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundZ(FastMath.toRadians(runningAngle + getNoise(r))).rotateAroundX(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundZ(FastMath.toRadians(- runningAngle + getNoise())).rotateAroundX(FastMath.toRadians(getNoise())), growBranch(l1.clone().add(diff), diff.clone().multiply(0.7).rotateAroundZ(FastMath.toRadians(-runningAngle + getNoise(r))).rotateAroundX(FastMath.toRadians(getNoise(r))),
d1 - 1, recursions + 1); d1 - 1, recursions + 1, r);
} }
private int getNoise() { private int getNoise(Random r) {
return super.getRandom().nextInt(90) - 45; return r.nextInt(90) - 45;
} }
} }

View File

@ -22,12 +22,9 @@ public class SpruceTree extends FractalTree {
/** /**
* Instantiates a TreeGrower at an origin location. * Instantiates a TreeGrower at an origin location.
*
* @param origin - The origin location.
* @param random - The random object to use whilst generating the tree.
*/ */
public SpruceTree(Location origin, Random random, TerraPlugin main) { public SpruceTree(TerraPlugin main) {
super(origin, random, main); super(main);
geo = new TreeGeometry(this); geo = new TreeGeometry(this);
} }
@ -35,8 +32,8 @@ public class SpruceTree extends FractalTree {
* Grows the tree in memory. Intended to be invoked from an async thread. * Grows the tree in memory. Intended to be invoked from an async thread.
*/ */
@Override @Override
public void grow() { public void grow(Location origin, Random random) {
growTrunk(super.getOrigin().clone(), new Vector3(0, 16 + super.getRandom().nextInt(5), 0)); growTrunk(origin.clone(), new Vector3(0, 16 + random.nextInt(5), 0));
} }
private void growTrunk(Location l1, Vector3 diff) { private void growTrunk(Location l1, Vector3 diff) {

View File

@ -11,10 +11,9 @@ import com.dfsek.terra.api.gaea.tree.fractal.trees.SmallShatteredPillar;
import com.dfsek.terra.api.gaea.tree.fractal.trees.SmallShatteredTree; import com.dfsek.terra.api.gaea.tree.fractal.trees.SmallShatteredTree;
import com.dfsek.terra.api.gaea.tree.fractal.trees.SpruceTree; import com.dfsek.terra.api.gaea.tree.fractal.trees.SpruceTree;
import com.dfsek.terra.api.generic.TerraPlugin; import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.api.generic.world.WorldHandle; import com.dfsek.terra.api.generic.world.block.BlockFace;
import com.dfsek.terra.api.generic.world.block.MaterialData; import com.dfsek.terra.api.generic.world.block.MaterialData;
import com.dfsek.terra.api.generic.world.vector.Location; import com.dfsek.terra.api.generic.world.vector.Location;
import com.dfsek.terra.util.MaterialSet;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -69,32 +68,30 @@ public class TreeRegistry extends TerraRegistry<Tree> {
} }
} }
private final class FractalTreeHolder implements Tree { // TODO: this is jank and should be replaced later. private final class FractalTreeHolder implements Tree {
private final Constructor<? extends FractalTree> constructor; private final FractalTree tree;
private final MaterialSet set;
private FractalTreeHolder(Class<? extends FractalTree> clazz) throws NoSuchMethodException { private FractalTreeHolder(Class<? extends FractalTree> clazz) throws NoSuchMethodException {
constructor = clazz.getConstructor(Location.class, Random.class, TerraPlugin.class); Constructor<? extends FractalTree> constructor = clazz.getConstructor(TerraPlugin.class);
WorldHandle h = main.getWorldHandle();
set = MaterialSet.get(h.createMaterialData("minecraft:grass_block"), h.createMaterialData("minecraft:snow_block")); // TODO: actually implement
}
@Override
public boolean plant(Location l, Random r) {
try { try {
FractalTree tree = constructor.newInstance(l, r, main); tree = constructor.newInstance(main);
tree.grow();
tree.plant();
return true;
} catch(InstantiationException | IllegalAccessException | InvocationTargetException e) { } catch(InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
return false; throw new IllegalArgumentException("Unable to load tree: " + clazz);
} }
} }
@Override
public boolean plant(Location l, Random r) {
if(!getSpawnable().contains(l.getBlock().getType())) return false;
if(!l.getBlock().getRelative(BlockFace.UP).isEmpty()) return false;
tree.grow(l.add(0, 1, 0), r);
return true;
}
@Override @Override
public Set<MaterialData> getSpawnable() { public Set<MaterialData> getSpawnable() {
return set; return tree.getSpawnable();
} }
} }
} }