mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-04 00:45:57 +00:00
implement RecursionsFunction
This commit is contained in:
parent
63e59692e2
commit
7fe7dac57a
@ -21,9 +21,9 @@ public class Block implements Item<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReturnLevel apply(Location location, Rotation rotation) {
|
public ReturnLevel apply(Location location, Rotation rotation, int recursions) {
|
||||||
for(Item<?> item : items) {
|
for(Item<?> item : items) {
|
||||||
Object result = item.apply(location, rotation);
|
Object result = item.apply(location, 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;
|
||||||
@ -33,9 +33,9 @@ public class Block implements Item<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReturnLevel apply(Location location, Chunk chunk, Rotation rotation) {
|
public ReturnLevel apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
for(Item<?> item : items) {
|
for(Item<?> item : items) {
|
||||||
Object result = item.apply(location, chunk, rotation);
|
Object result = item.apply(location, chunk, 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;
|
||||||
|
@ -6,9 +6,9 @@ import com.dfsek.terra.api.structures.structure.Rotation;
|
|||||||
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);
|
T apply(Location location, Rotation rotation, int recursions);
|
||||||
|
|
||||||
T apply(Location location, Chunk chunk, Rotation rotation);
|
T apply(Location location, Chunk chunk, Rotation rotation, int recursions);
|
||||||
|
|
||||||
Position getPosition();
|
Position getPosition();
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,13 @@ public abstract class ConstantExpression<T> implements Returnable<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T apply(Location location, Rotation rotation) {
|
public T apply(Location location, Rotation rotation, int recursions) {
|
||||||
return constant;
|
return constant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T apply(Location location, Chunk chunk, Rotation rotation) {
|
public T apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return constant;
|
return constant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,12 +15,12 @@ public class BreakKeyword implements Keyword<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
|
||||||
return Block.ReturnLevel.BREAK;
|
return Block.ReturnLevel.BREAK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return Block.ReturnLevel.BREAK;
|
return Block.ReturnLevel.BREAK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,12 +15,12 @@ public class ContinueKeyword implements Keyword<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
|
||||||
return Block.ReturnLevel.CONTINUE;
|
return Block.ReturnLevel.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return Block.ReturnLevel.CONTINUE;
|
return Block.ReturnLevel.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,14 +20,14 @@ public class IfKeyword implements Keyword<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
|
||||||
if(statement.apply(location, rotation)) return conditional.apply(location, rotation);
|
if(statement.apply(location, rotation, recursions)) return conditional.apply(location, rotation, recursions);
|
||||||
return Block.ReturnLevel.NONE;
|
return Block.ReturnLevel.NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
if(statement.apply(location, chunk, rotation)) return conditional.apply(location, chunk, rotation);
|
if(statement.apply(location, chunk, rotation, recursions)) return conditional.apply(location, chunk, rotation, recursions);
|
||||||
return Block.ReturnLevel.NONE;
|
return Block.ReturnLevel.NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,12 +15,12 @@ public class ReturnKeyword implements Keyword<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
|
||||||
return Block.ReturnLevel.RETURN;
|
return Block.ReturnLevel.RETURN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return Block.ReturnLevel.RETURN;
|
return Block.ReturnLevel.RETURN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,9 +20,9 @@ public class WhileKeyword implements Keyword<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
|
||||||
while(statement.apply(location, rotation)) {
|
while(statement.apply(location, rotation, recursions)) {
|
||||||
Block.ReturnLevel level = conditional.apply(location, rotation);
|
Block.ReturnLevel level = conditional.apply(location, 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;
|
||||||
}
|
}
|
||||||
@ -30,9 +30,9 @@ public class WhileKeyword implements Keyword<Block.ReturnLevel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation) {
|
public Block.ReturnLevel apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
while(statement.apply(location, chunk, rotation)) {
|
while(statement.apply(location, chunk, rotation, recursions)) {
|
||||||
Block.ReturnLevel level = conditional.apply(location, chunk, rotation);
|
Block.ReturnLevel level = conditional.apply(location, chunk, 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;
|
||||||
}
|
}
|
||||||
|
@ -25,12 +25,12 @@ public abstract class BinaryOperation<I, O> implements Returnable<O> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public O apply(Location location, Rotation rotation) {
|
public O apply(Location location, Rotation rotation, int recursions) {
|
||||||
return apply(left.apply(location, rotation), right.apply(location, rotation));
|
return apply(left.apply(location, rotation, recursions), right.apply(location, rotation, recursions));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public O apply(Location location, Chunk chunk, Rotation rotation) {
|
public O apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return apply(left.apply(location, chunk, rotation), right.apply(location, chunk, rotation));
|
return apply(left.apply(location, chunk, rotation, recursions), right.apply(location, chunk, rotation, recursions));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,13 @@ 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) {
|
public T apply(Location location, Rotation rotation, int recursions) {
|
||||||
return apply(input.apply(location, rotation));
|
return apply(input.apply(location, rotation, recursions));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T apply(Location location, Chunk chunk, Rotation rotation) {
|
public T apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return apply(input.apply(location, chunk, rotation));
|
return apply(input.apply(location, chunk, rotation, recursions));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -19,15 +19,15 @@ public class Assignment<T> implements Item<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T apply(Location location, Rotation rotation) {
|
public T apply(Location location, Rotation rotation, int recursions) {
|
||||||
T val = value.apply(location, rotation);
|
T val = value.apply(location, rotation, recursions);
|
||||||
delegate.setValue(val);
|
delegate.setValue(val);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T apply(Location location, Chunk chunk, Rotation rotation) {
|
public T apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
T val = value.apply(location, chunk, rotation);
|
T val = value.apply(location, chunk, rotation, recursions);
|
||||||
delegate.setValue(val);
|
delegate.setValue(val);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,12 @@ public class Getter implements Returnable<Object> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object apply(Location location, Rotation rotation) {
|
public Object apply(Location location, Rotation rotation, int recursions) {
|
||||||
return delegate.getValue();
|
return delegate.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object apply(Location location, Chunk chunk, Rotation rotation) {
|
public Object apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return delegate.getValue();
|
return delegate.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import com.dfsek.terra.api.structures.parser.lang.Block;
|
|||||||
import com.dfsek.terra.api.structures.script.builders.BlockFunctionBuilder;
|
import com.dfsek.terra.api.structures.script.builders.BlockFunctionBuilder;
|
||||||
import com.dfsek.terra.api.structures.script.builders.CheckFunctionBuilder;
|
import com.dfsek.terra.api.structures.script.builders.CheckFunctionBuilder;
|
||||||
import com.dfsek.terra.api.structures.script.builders.RandomFunctionBuilder;
|
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.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.registry.ScriptRegistry;
|
import com.dfsek.terra.registry.ScriptRegistry;
|
||||||
@ -31,7 +32,8 @@ public class StructureScript {
|
|||||||
parser.addFunction("block", new BlockFunctionBuilder(main))
|
parser.addFunction("block", new BlockFunctionBuilder(main))
|
||||||
.addFunction("check", new CheckFunctionBuilder(main))
|
.addFunction("check", new CheckFunctionBuilder(main))
|
||||||
.addFunction("structure", new StructureFunctionBuilder(registry, main))
|
.addFunction("structure", new StructureFunctionBuilder(registry, main))
|
||||||
.addFunction("randomInt", new RandomFunctionBuilder());
|
.addFunction("randomInt", new RandomFunctionBuilder())
|
||||||
|
.addFunction("recursions", new RecursionsFunctionBuilder());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
block = parser.parse();
|
block = parser.parse();
|
||||||
@ -41,12 +43,12 @@ public class StructureScript {
|
|||||||
this.id = parser.getID();
|
this.id = parser.getID();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Location location, Rotation rotation) {
|
public void execute(Location location, Rotation rotation, int recursions) {
|
||||||
block.apply(location, rotation);
|
block.apply(location, rotation, recursions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Location location, Chunk chunk, Rotation rotation) {
|
public void execute(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
block.apply(location, chunk, rotation);
|
block.apply(location, chunk, rotation, recursions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.dfsek.terra.api.structures.script.builders;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||||
|
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||||
|
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
|
||||||
|
import com.dfsek.terra.api.structures.script.functions.RecursionsFunction;
|
||||||
|
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class RecursionsFunctionBuilder implements FunctionBuilder<RecursionsFunction> {
|
||||||
|
@Override
|
||||||
|
public RecursionsFunction build(List<Returnable<?>> argumentList, Position position) throws ParseException {
|
||||||
|
return new RecursionsFunction(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int argNumber() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Returnable.ReturnType getArgument(int position) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
|
|||||||
import com.dfsek.terra.registry.ScriptRegistry;
|
import com.dfsek.terra.registry.ScriptRegistry;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class StructureFunctionBuilder implements FunctionBuilder<StructureFunction> {
|
public class StructureFunctionBuilder implements FunctionBuilder<StructureFunction> {
|
||||||
private final ScriptRegistry registry;
|
private final ScriptRegistry registry;
|
||||||
@ -22,12 +23,15 @@ public class StructureFunctionBuilder implements FunctionBuilder<StructureFuncti
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public StructureFunction build(List<Returnable<?>> argumentList, Position position) throws ParseException {
|
public StructureFunction build(List<Returnable<?>> argumentList, Position position) throws ParseException {
|
||||||
return new StructureFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1), (Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), registry, position, main);
|
if(argumentList.size() < 5) throw new ParseException("Expected rotations: " + position);
|
||||||
|
|
||||||
|
return new StructureFunction((Returnable<Number>) argumentList.remove(0), (Returnable<Number>) argumentList.remove(0), (Returnable<Number>) argumentList.remove(0), (Returnable<String>) argumentList.remove(0),
|
||||||
|
argumentList.stream().map(item -> ((Returnable<String>) item)).collect(Collectors.toList()), registry, position, main);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int argNumber() {
|
public int argNumber() {
|
||||||
return 4;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,10 +41,8 @@ public class StructureFunctionBuilder implements FunctionBuilder<StructureFuncti
|
|||||||
case 1:
|
case 1:
|
||||||
case 2:
|
case 2:
|
||||||
return Returnable.ReturnType.NUMBER;
|
return Returnable.ReturnType.NUMBER;
|
||||||
case 3:
|
|
||||||
return Returnable.ReturnType.STRING;
|
|
||||||
default:
|
default:
|
||||||
return null;
|
return Returnable.ReturnType.STRING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,17 +35,17 @@ public class BlockFunction implements Function<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Location location, Rotation rotation) {
|
public Void apply(Location location, Rotation rotation, int recursions) {
|
||||||
Vector2 xz = new Vector2(x.apply(location, rotation).doubleValue(), z.apply(location, rotation).doubleValue());
|
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, rotation);
|
RotationUtil.rotateVector(xz, rotation);
|
||||||
|
|
||||||
location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation).intValue(), FastMath.roundToInt(xz.getZ())).getBlock().setBlockData(data, false);
|
location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())).getBlock().setBlockData(data, false);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Location location, Chunk chunk, Rotation rotation) {
|
public Void apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
//TODO: do
|
//TODO: do
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -33,19 +33,19 @@ public class CheckFunction implements Function<String> {
|
|||||||
return "check";
|
return "check";
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector3 getVector(Location location, Chunk chunk, Rotation rotation) {
|
private Vector3 getVector(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
Vector2 xz = chunk == null ? new Vector2(x.apply(location, rotation).doubleValue(), z.apply(location, rotation).doubleValue())
|
Vector2 xz = chunk == null ? new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue())
|
||||||
: new Vector2(x.apply(location, chunk, rotation).doubleValue(), z.apply(location, chunk, rotation).doubleValue());
|
: new Vector2(x.apply(location, chunk, rotation, recursions).doubleValue(), z.apply(location, chunk, rotation, recursions).doubleValue());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, rotation);
|
RotationUtil.rotateVector(xz, rotation);
|
||||||
|
|
||||||
return location.clone().add(chunk == null ? new Vector3(FastMath.roundToInt(xz.getX()), y.apply(location, rotation).intValue(), FastMath.roundToInt(xz.getZ()))
|
return location.clone().add(chunk == null ? new Vector3(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ()))
|
||||||
: new Vector3(FastMath.roundToInt(xz.getX()), y.apply(location, chunk, rotation).intValue(), FastMath.roundToInt(xz.getZ()))).toVector();
|
: new Vector3(FastMath.roundToInt(xz.getX()), y.apply(location, chunk, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ()))).toVector();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apply(Location location, Rotation rotation) {
|
public String apply(Location location, Rotation rotation, int recursions) {
|
||||||
return apply(getVector(location, null, rotation), location.getWorld());
|
return apply(getVector(location, null, rotation, recursions), location.getWorld());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String apply(Vector3 vector, World world) {
|
private String apply(Vector3 vector, World world) {
|
||||||
@ -57,8 +57,8 @@ public class CheckFunction implements Function<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apply(Location location, Chunk chunk, Rotation rotation) {
|
public String apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return apply(getVector(location, chunk, rotation), location.getWorld());
|
return apply(getVector(location, chunk, rotation, recursions), location.getWorld());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -30,13 +30,13 @@ public class RandomFunction implements Function<Integer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer apply(Location location, Rotation rotation) {
|
public Integer apply(Location location, Rotation rotation, int recursions) {
|
||||||
return ThreadLocalRandom.current().nextInt(numberReturnable.apply(location, rotation).intValue()); // TODO: deterministic random
|
return ThreadLocalRandom.current().nextInt(numberReturnable.apply(location, rotation, recursions).intValue()); // TODO: deterministic random
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer apply(Location location, Chunk chunk, Rotation rotation) {
|
public Integer apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return ThreadLocalRandom.current().nextInt(numberReturnable.apply(location, chunk, rotation).intValue());
|
return ThreadLocalRandom.current().nextInt(numberReturnable.apply(location, chunk, rotation, recursions).intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.dfsek.terra.api.structures.script.functions;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.math.vector.Location;
|
||||||
|
import com.dfsek.terra.api.platform.world.Chunk;
|
||||||
|
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
|
||||||
|
import com.dfsek.terra.api.structures.structure.Rotation;
|
||||||
|
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||||
|
|
||||||
|
public class RecursionsFunction implements Function<Number> {
|
||||||
|
private final Position position;
|
||||||
|
|
||||||
|
public RecursionsFunction(Position position) {
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String name() {
|
||||||
|
return "recursions";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReturnType returnType() {
|
||||||
|
return ReturnType.NUMBER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Number apply(Location location, Rotation rotation, int recursions) {
|
||||||
|
return recursions;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Number apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
|
return recursions;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
}
|
@ -13,14 +13,18 @@ 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;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class StructureFunction implements Function<Void> {
|
public class StructureFunction implements Function<Void> {
|
||||||
private final ScriptRegistry registry;
|
private final ScriptRegistry registry;
|
||||||
private final Returnable<String> id;
|
private final Returnable<String> id;
|
||||||
private final Returnable<Number> x, y, z;
|
private final Returnable<Number> x, y, z;
|
||||||
private final Position position;
|
private final Position position;
|
||||||
private final TerraPlugin main;
|
private final TerraPlugin main;
|
||||||
|
private final List<Returnable<String>> rotations;
|
||||||
|
|
||||||
public StructureFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> id, ScriptRegistry registry, Position position, TerraPlugin main) {
|
public StructureFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> id, List<Returnable<String>> rotations, ScriptRegistry registry, Position position, TerraPlugin main) {
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.position = position;
|
this.position = position;
|
||||||
@ -28,6 +32,7 @@ public class StructureFunction implements Function<Void> {
|
|||||||
this.y = y;
|
this.y = y;
|
||||||
this.z = z;
|
this.z = z;
|
||||||
this.main = main;
|
this.main = main;
|
||||||
|
this.rotations = rotations;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -41,28 +46,37 @@ public class StructureFunction implements Function<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Location location, Rotation rotation) {
|
public Void apply(Location location, Rotation rotation, int recursions) {
|
||||||
System.out.println("executing structure function");
|
System.out.println("executing structure function");
|
||||||
|
|
||||||
Vector2 xz = new Vector2(x.apply(location, rotation).doubleValue(), z.apply(location, rotation).doubleValue());
|
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue());
|
||||||
|
|
||||||
RotationUtil.rotateVector(xz, rotation);
|
RotationUtil.rotateVector(xz, rotation);
|
||||||
|
|
||||||
StructureScript script = registry.get(id.apply(location, rotation));
|
StructureScript script = registry.get(id.apply(location, rotation, recursions));
|
||||||
if(script == null) {
|
if(script == null) {
|
||||||
main.getLogger().severe("No such structure " + id.apply(location, rotation));
|
main.getLogger().severe("No such structure " + id.apply(location, rotation, recursions));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Location location1 = location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation).intValue(), FastMath.roundToInt(xz.getZ()));
|
Rotation rotation1;
|
||||||
|
String rotString = rotations.get(ThreadLocalRandom.current().nextInt(rotations.size())).apply(location, rotation, recursions);
|
||||||
|
try {
|
||||||
|
rotation1 = Rotation.valueOf(rotString);
|
||||||
|
} catch(IllegalArgumentException e) {
|
||||||
|
main.getLogger().severe("Invalid rotation " + rotString);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
script.execute(location1, rotation);
|
Location location1 = location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ()));
|
||||||
|
|
||||||
|
script.execute(location1, rotation.rotate(rotation1), recursions + 1);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Location location, Chunk chunk, Rotation rotation) {
|
public Void apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,10 @@ public enum Rotation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Rotation rotate(Rotation rotation) {
|
||||||
|
return fromDegrees(this.getDegrees() + rotation.getDegrees());
|
||||||
|
}
|
||||||
|
|
||||||
public enum Axis {
|
public enum Axis {
|
||||||
X, Y, Z
|
X, Y, Z
|
||||||
}
|
}
|
||||||
|
@ -51,9 +51,9 @@ public class ParserTest {
|
|||||||
long t = System.nanoTime() - l;
|
long t = System.nanoTime() - l;
|
||||||
System.out.println("Took " + (double) t / 1000000);
|
System.out.println("Took " + (double) t / 1000000);
|
||||||
|
|
||||||
block.apply(null, Rotation.NONE);
|
block.apply(null, Rotation.NONE, recursions);
|
||||||
|
|
||||||
block.apply(null, Rotation.NONE);
|
block.apply(null, Rotation.NONE, recursions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Test1 implements Function<Void> {
|
private static class Test1 implements Function<Void> {
|
||||||
@ -68,13 +68,13 @@ public class ParserTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Location location, Rotation rotation) {
|
public Void apply(Location location, Rotation rotation, int recursions) {
|
||||||
System.out.println("string: " + a.apply(location, rotation) + ", double: " + b.apply(location, rotation));
|
System.out.println("string: " + a.apply(location, rotation, recursions) + ", double: " + b.apply(location, rotation, recursions));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Location location, Chunk chunk, Rotation rotation) {
|
public Void apply(Location location, Chunk chunk, Rotation rotation, int recursions) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,9 +32,14 @@ public class LoadRawCommand extends LoadCommand implements DebugCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
|
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
|
||||||
TerraWorld terraWorld = getMain().getWorld(new BukkitWorld(sender.getWorld()));
|
|
||||||
|
|
||||||
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)));
|
|
||||||
|
TerraWorld terraWorld = getMain().getWorld(new BukkitWorld(sender.getWorld()));
|
||||||
|
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);
|
||||||
|
long l = System.nanoTime() - t;
|
||||||
|
|
||||||
|
sender.sendMessage("Took " + ((double) l) / 1000000 + "ms");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user