store structures in Buffer

This commit is contained in:
dfsek committed 2020-12-24 02:40:28 -07:00
1 parent 7127943298
commit 058ec9f24d
29 files changed
+173 -70

No files matched your search

@@ -1,7 +1,7 @@
package com.dfsek.terra.api.structures.parser.lang; package com.dfsek.terra.api.structures.parser.lang;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import java.util.List; import java.util.List;
@@ -20,9 +20,9 @@ public class Block implements Item<Block.ReturnLevel> {
} }
@Override @Override
public synchronized ReturnLevel apply(Location location, Rotation rotation, int recursions) { public synchronized ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
for(Item<?> item : items) { for(Item<?> item : items) {
Object result = item.apply(location, rotation, recursions); Object result = item.apply(buffer, rotation, recursions);
if(result instanceof ReturnLevel) { if(result instanceof ReturnLevel) {
ReturnLevel level = (ReturnLevel) result; ReturnLevel level = (ReturnLevel) result;
if(!level.equals(ReturnLevel.NONE)) return level; if(!level.equals(ReturnLevel.NONE)) return level;
@@ -1,11 +1,11 @@
package com.dfsek.terra.api.structures.parser.lang; package com.dfsek.terra.api.structures.parser.lang;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public interface Item<T> { public interface Item<T> {
T apply(Location location, Rotation rotation, int recursions); T apply(Buffer buffer, Rotation rotation, int recursions);
Position getPosition(); Position getPosition();
} }
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.constants; package com.dfsek.terra.api.structures.parser.lang.constants;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public abstract class ConstantExpression<T> implements Returnable<T> { public abstract class ConstantExpression<T> implements Returnable<T> {
@@ -15,7 +15,7 @@ public abstract class ConstantExpression<T> implements Returnable<T> {
} }
@Override @Override
public T apply(Location location, Rotation rotation, int recursions) { public T apply(Buffer buffer, Rotation rotation, int recursions) {
return constant; return constant;
} }
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.functions.builtin; package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath; import net.jafama.FastMath;
@@ -20,7 +20,7 @@ public class AbsFunction extends MathFunction {
} }
@Override @Override
public Number apply(Location location, Rotation rotation, int recursions) { public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return FastMath.abs(returnable.apply(location, rotation, recursions).doubleValue()); return FastMath.abs(returnable.apply(buffer, rotation, recursions).doubleValue());
} }
} }
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.functions.builtin; package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath; import net.jafama.FastMath;
@@ -22,7 +22,7 @@ public class PowFunction extends MathFunction {
} }
@Override @Override
public Number apply(Location location, Rotation rotation, int recursions) { public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return FastMath.pow(base.apply(location, rotation, recursions).doubleValue(), power.apply(location, rotation, recursions).doubleValue()); return FastMath.pow(base.apply(buffer, rotation, recursions).doubleValue(), power.apply(buffer, rotation, recursions).doubleValue());
} }
} }
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.functions.builtin; package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath; import net.jafama.FastMath;
@@ -20,7 +20,7 @@ public class SqrtFunction extends MathFunction {
} }
@Override @Override
public Number apply(Location location, Rotation rotation, int recursions) { public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return FastMath.sqrt(returnable.apply(location, rotation, recursions).doubleValue()); return FastMath.sqrt(returnable.apply(buffer, rotation, recursions).doubleValue());
} }
} }
@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.keywords; package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block; import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword; import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class BreakKeyword implements Keyword<Block.ReturnLevel> { public class BreakKeyword implements Keyword<Block.ReturnLevel> {
@@ -14,7 +14,7 @@ public class BreakKeyword implements Keyword<Block.ReturnLevel> {
} }
@Override @Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) { public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
return Block.ReturnLevel.BREAK; return Block.ReturnLevel.BREAK;
} }
@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.keywords; package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block; import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword; import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class ContinueKeyword implements Keyword<Block.ReturnLevel> { public class ContinueKeyword implements Keyword<Block.ReturnLevel> {
@@ -14,7 +14,7 @@ public class ContinueKeyword implements Keyword<Block.ReturnLevel> {
} }
@Override @Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) { public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
return Block.ReturnLevel.CONTINUE; return Block.ReturnLevel.CONTINUE;
} }
@@ -1,10 +1,10 @@
package com.dfsek.terra.api.structures.parser.lang.keywords; package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block; import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword; import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class IfKeyword implements Keyword<Block.ReturnLevel> { public class IfKeyword implements Keyword<Block.ReturnLevel> {
@@ -19,8 +19,8 @@ public class IfKeyword implements Keyword<Block.ReturnLevel> {
} }
@Override @Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) { public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
if(statement.apply(location, rotation, recursions)) return conditional.apply(location, rotation, recursions); if(statement.apply(buffer, rotation, recursions)) return conditional.apply(buffer, rotation, recursions);
return Block.ReturnLevel.NONE; return Block.ReturnLevel.NONE;
} }
@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.keywords; package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block; import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword; import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class ReturnKeyword implements Keyword<Block.ReturnLevel> { public class ReturnKeyword implements Keyword<Block.ReturnLevel> {
@@ -14,7 +14,7 @@ public class ReturnKeyword implements Keyword<Block.ReturnLevel> {
} }
@Override @Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) { public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
return Block.ReturnLevel.RETURN; return Block.ReturnLevel.RETURN;
} }
@@ -1,10 +1,10 @@
package com.dfsek.terra.api.structures.parser.lang.keywords; package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block; import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword; import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class WhileKeyword implements Keyword<Block.ReturnLevel> { public class WhileKeyword implements Keyword<Block.ReturnLevel> {
@@ -19,9 +19,9 @@ public class WhileKeyword implements Keyword<Block.ReturnLevel> {
} }
@Override @Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) { public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
while(statement.apply(location, rotation, recursions)) { while(statement.apply(buffer, rotation, recursions)) {
Block.ReturnLevel level = conditional.apply(location, rotation, recursions); Block.ReturnLevel level = conditional.apply(buffer, rotation, recursions);
if(level.equals(Block.ReturnLevel.BREAK)) break; if(level.equals(Block.ReturnLevel.BREAK)) break;
if(level.equals(Block.ReturnLevel.RETURN)) return Block.ReturnLevel.RETURN; if(level.equals(Block.ReturnLevel.RETURN)) return Block.ReturnLevel.RETURN;
} }
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.operations; package com.dfsek.terra.api.structures.parser.lang.operations;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public abstract class BinaryOperation<I, O> implements Returnable<O> { public abstract class BinaryOperation<I, O> implements Returnable<O> {
@@ -24,7 +24,7 @@ public abstract class BinaryOperation<I, O> implements Returnable<O> {
} }
@Override @Override
public O apply(Location location, Rotation rotation, int recursions) { public O apply(Buffer buffer, Rotation rotation, int recursions) {
return apply(left.apply(location, rotation, recursions), right.apply(location, rotation, recursions)); return apply(left.apply(buffer, rotation, recursions), right.apply(buffer, rotation, recursions));
} }
} }
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.operations; package com.dfsek.terra.api.structures.parser.lang.operations;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public abstract class UnaryOperation<T> implements Returnable<T> { public abstract class UnaryOperation<T> implements Returnable<T> {
@@ -17,8 +17,8 @@ public abstract class UnaryOperation<T> implements Returnable<T> {
public abstract T apply(T input); public abstract T apply(T input);
@Override @Override
public T apply(Location location, Rotation rotation, int recursions) { public T apply(Buffer buffer, Rotation rotation, int recursions) {
return apply(input.apply(location, rotation, recursions)); return apply(input.apply(buffer, rotation, recursions));
} }
@Override @Override
@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.variables; package com.dfsek.terra.api.structures.parser.lang.variables;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Item; import com.dfsek.terra.api.structures.parser.lang.Item;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class Assignment<T> implements Item<T> { public class Assignment<T> implements Item<T> {
@@ -18,8 +18,8 @@ public class Assignment<T> implements Item<T> {
} }
@Override @Override
public synchronized T apply(Location location, Rotation rotation, int recursions) { public synchronized T apply(Buffer buffer, Rotation rotation, int recursions) {
T val = value.apply(location, rotation, recursions); T val = value.apply(buffer, rotation, recursions);
delegate.setValue(val); delegate.setValue(val);
return val; return val;
} }
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.variables; package com.dfsek.terra.api.structures.parser.lang.variables;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class Getter implements Returnable<Object> { public class Getter implements Returnable<Object> {
@@ -18,7 +18,7 @@ public class Getter implements Returnable<Object> {
} }
@Override @Override
public synchronized Object apply(Location location, Rotation rotation, int recursions) { public synchronized Object apply(Buffer buffer, Rotation rotation, int recursions) {
return delegate.getValue(); return delegate.getValue();
} }
@@ -11,6 +11,8 @@ import com.dfsek.terra.api.structures.script.builders.RandomFunctionBuilder;
import com.dfsek.terra.api.structures.script.builders.RecursionsFunctionBuilder; import com.dfsek.terra.api.structures.script.builders.RecursionsFunctionBuilder;
import com.dfsek.terra.api.structures.script.builders.StructureFunctionBuilder; import com.dfsek.terra.api.structures.script.builders.StructureFunctionBuilder;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.structure.buffer.StructureBuffer;
import com.dfsek.terra.registry.ScriptRegistry; import com.dfsek.terra.registry.ScriptRegistry;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@@ -42,8 +44,14 @@ public class StructureScript {
this.id = parser.getID(); this.id = parser.getID();
} }
public void execute(Location location, Rotation rotation, int recursions) { public void execute(Location location, Rotation rotation) {
block.apply(location, rotation, recursions); StructureBuffer buffer = new StructureBuffer(location);
block.apply(buffer, rotation, 0);
buffer.paste();
}
public void executeInBuffer(Buffer buffer, Rotation rotation, int recursions) {
block.apply(buffer, rotation, recursions);
} }
public String getId() { public String getId() {
@@ -1,7 +1,7 @@
package com.dfsek.terra.api.structures.script.functions; package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector2; import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.TerraPlugin; import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.block.BlockData; import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.structures.parser.exceptions.ParseException; import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
@@ -10,6 +10,8 @@ import com.dfsek.terra.api.structures.parser.lang.constants.ConstantExpression;
import com.dfsek.terra.api.structures.parser.lang.functions.Function; import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.RotationUtil; import com.dfsek.terra.api.structures.structure.RotationUtil;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.structure.buffer.BufferedBlock;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath; import net.jafama.FastMath;
@@ -34,12 +36,12 @@ public class BlockFunction implements Function<Void> {
} }
@Override @Override
public Void apply(Location location, Rotation rotation, int recursions) { public Void apply(Buffer buffer, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue()); Vector2 xz = new Vector2(x.apply(buffer, rotation, recursions).doubleValue(), z.apply(buffer, rotation, recursions).doubleValue());
RotationUtil.rotateVector(xz, rotation); RotationUtil.rotateVector(xz, rotation);
location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())).getBlock().setBlockData(data, false); buffer.addItem(new BufferedBlock(data), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
return null; return null;
} }
@@ -1,6 +1,5 @@
package com.dfsek.terra.api.structures.script.functions; package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector2; import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.math.vector.Vector3; import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.TerraPlugin; import com.dfsek.terra.api.platform.TerraPlugin;
@@ -9,6 +8,7 @@ import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function; import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.RotationUtil; import com.dfsek.terra.api.structures.structure.RotationUtil;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.structures.world.LandCheck; import com.dfsek.terra.api.structures.world.LandCheck;
import com.dfsek.terra.api.structures.world.OceanCheck; import com.dfsek.terra.api.structures.world.OceanCheck;
@@ -32,17 +32,17 @@ public class CheckFunction implements Function<String> {
return "check"; return "check";
} }
private Vector3 getVector(Location location, Rotation rotation, int recursions) { private Vector3 getVector(Buffer buffer, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue()); Vector2 xz = new Vector2(x.apply(buffer, rotation, recursions).doubleValue(), z.apply(buffer, rotation, recursions).doubleValue());
RotationUtil.rotateVector(xz, rotation); RotationUtil.rotateVector(xz, rotation);
return new Vector3(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())); return new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())).add(buffer.getOrigin().toVector());
} }
@Override @Override
public String apply(Location location, Rotation rotation, int recursions) { public String apply(Buffer buffer, Rotation rotation, int recursions) {
return apply(getVector(location, rotation, recursions), location.getWorld()); return apply(getVector(buffer, rotation, recursions), buffer.getOrigin().getWorld());
} }
private String apply(Vector3 vector, World world) { private String apply(Vector3 vector, World world) {
@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.script.functions; package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function; import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@@ -29,8 +29,8 @@ public class RandomFunction implements Function<Integer> {
} }
@Override @Override
public Integer apply(Location location, Rotation rotation, int recursions) { public Integer apply(Buffer buffer, Rotation rotation, int recursions) {
return ThreadLocalRandom.current().nextInt(numberReturnable.apply(location, rotation, recursions).intValue()); // TODO: deterministic random return ThreadLocalRandom.current().nextInt(numberReturnable.apply(buffer, rotation, recursions).intValue()); // TODO: deterministic random
} }
@Override @Override
@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.script.functions; package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.functions.Function; import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
public class RecursionsFunction implements Function<Number> { public class RecursionsFunction implements Function<Number> {
@@ -23,7 +23,7 @@ public class RecursionsFunction implements Function<Number> {
} }
@Override @Override
public Number apply(Location location, Rotation rotation, int recursions) { public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return recursions; return recursions;
} }
@@ -1,13 +1,15 @@
package com.dfsek.terra.api.structures.script.functions; package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector2; import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.TerraPlugin; import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.structures.parser.lang.Returnable; import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function; import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.script.StructureScript; import com.dfsek.terra.api.structures.script.StructureScript;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.RotationUtil; import com.dfsek.terra.api.structures.structure.RotationUtil;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.structure.buffer.IntermediateBuffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.registry.ScriptRegistry; import com.dfsek.terra.registry.ScriptRegistry;
import net.jafama.FastMath; import net.jafama.FastMath;
@@ -45,20 +47,20 @@ public class StructureFunction implements Function<Void> {
} }
@Override @Override
public Void apply(Location location, Rotation rotation, int recursions) { public Void apply(Buffer buffer, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue()); Vector2 xz = new Vector2(x.apply(buffer, rotation, recursions).doubleValue(), z.apply(buffer, rotation, recursions).doubleValue());
RotationUtil.rotateVector(xz, rotation); RotationUtil.rotateVector(xz, rotation);
StructureScript script = registry.get(id.apply(location, rotation, recursions)); StructureScript script = registry.get(id.apply(buffer, rotation, recursions));
if(script == null) { if(script == null) {
main.getLogger().severe("No such structure " + id.apply(location, rotation, recursions)); main.getLogger().severe("No such structure " + id.apply(buffer, rotation, recursions));
return null; return null;
} }
Rotation rotation1; Rotation rotation1;
String rotString = rotations.get(ThreadLocalRandom.current().nextInt(rotations.size())).apply(location, rotation, recursions); String rotString = rotations.get(ThreadLocalRandom.current().nextInt(rotations.size())).apply(buffer, rotation, recursions);
try { try {
rotation1 = Rotation.valueOf(rotString); rotation1 = Rotation.valueOf(rotString);
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
@@ -66,9 +68,9 @@ public class StructureFunction implements Function<Void> {
return null; return null;
} }
Location location1 = location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())); Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ()));
script.execute(location1, rotation.rotate(rotation1), recursions + 1); script.executeInBuffer(new IntermediateBuffer(buffer, offset), rotation.rotate(rotation1), recursions + 1);
return null; return null;
} }
@@ -0,0 +1,10 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
public interface Buffer {
Buffer addItem(BufferedItem item, Vector3 location);
Location getOrigin();
}
@@ -0,0 +1,17 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.platform.block.BlockData;
public class BufferedBlock implements BufferedItem {
private final BlockData data;
public BufferedBlock(BlockData data) {
this.data = data;
}
@Override
public void paste(Location origin) {
origin.getBlock().setBlockData(data, false);
}
}
@@ -0,0 +1,7 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
public interface BufferedItem {
void paste(Location origin);
}
@@ -0,0 +1,24 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
public class IntermediateBuffer implements Buffer {
private final Buffer original;
private final Vector3 offset;
public IntermediateBuffer(Buffer original, Vector3 offset) {
this.original = original;
this.offset = offset;
}
@Override
public Buffer addItem(BufferedItem item, Vector3 location) {
return original.addItem(item, location.add(offset));
}
@Override
public Location getOrigin() {
return original.getOrigin().add(offset);
}
}
@@ -0,0 +1,33 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
import java.util.HashMap;
import java.util.Map;
public class StructureBuffer implements Buffer {
private final Map<Vector3, BufferedItem> bufferedItemMap = new HashMap<>();
private final Location origin;
public StructureBuffer(Location origin) {
this.origin = origin;
}
public void paste() {
bufferedItemMap.forEach(((vector3, item) -> {
item.paste(origin.clone().add(vector3));
}));
}
@Override
public Buffer addItem(BufferedItem item, Vector3 location) {
bufferedItemMap.put(location, item);
return this;
}
@Override
public Location getOrigin() {
return origin;
}
}
@@ -21,7 +21,7 @@ public class TerraTree implements Tree {
@Override @Override
public synchronized boolean plant(Location location, Random random) { public synchronized boolean plant(Location location, Random random) {
structure.execute(location.clone().add(0, yOffset, 0), Rotation.fromDegrees(90 * random.nextInt(4)), 0); structure.execute(location.clone().add(0, yOffset, 0), Rotation.fromDegrees(90 * random.nextInt(4)));
return true; return true;
} }
@@ -1,6 +1,5 @@
package structure; package structure;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.Parser; import com.dfsek.terra.api.structures.parser.Parser;
import com.dfsek.terra.api.structures.parser.exceptions.ParseException; import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
import com.dfsek.terra.api.structures.parser.lang.Block; import com.dfsek.terra.api.structures.parser.lang.Block;
@@ -8,6 +7,7 @@ import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function; import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder; import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
import com.dfsek.terra.api.structures.structure.Rotation; import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Position;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -67,8 +67,8 @@ public class ParserTest {
} }
@Override @Override
public Void apply(Location location, Rotation rotation, int recursions) { public Void apply(Buffer buffer, Rotation rotation, int recursions) {
System.out.println("string: " + a.apply(location, rotation, recursions) + ", double: " + b.apply(location, rotation, recursions)); System.out.println("string: " + a.apply(buffer, rotation, recursions) + ", double: " + b.apply(buffer, rotation, recursions));
return null; return null;
} }
@@ -36,7 +36,7 @@ public class LoadRawCommand extends LoadCommand implements DebugCommand {
TerraWorld terraWorld = getMain().getWorld(new BukkitWorld(sender.getWorld())); TerraWorld terraWorld = getMain().getWorld(new BukkitWorld(sender.getWorld()));
long t = System.nanoTime(); long t = System.nanoTime();
terraWorld.getConfig().getScriptRegistry().get(args[0]).execute(new Location(new BukkitWorld(sender.getWorld()), sender.getLocation().getX(), sender.getLocation().getY(), sender.getLocation().getZ()), Rotation.fromDegrees(90 * ThreadLocalRandom.current().nextInt(4)), 0); terraWorld.getConfig().getScriptRegistry().get(args[0]).execute(new Location(new BukkitWorld(sender.getWorld()), sender.getLocation().getX(), sender.getLocation().getY(), sender.getLocation().getZ()), Rotation.fromDegrees(90 * ThreadLocalRandom.current().nextInt(4)));
long l = System.nanoTime() - t; long l = System.nanoTime() - t;
sender.sendMessage("Took " + ((double) l) / 1000000 + "ms"); sender.sendMessage("Took " + ((double) l) / 1000000 + "ms");