mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
completely redo internal TerraScript args
This commit is contained in:
@@ -11,9 +11,6 @@ import com.dfsek.terra.api.structures.parser.lang.constants.NumericConstant;
|
||||
import com.dfsek.terra.api.structures.parser.lang.constants.StringConstant;
|
||||
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.builtin.AbsFunction;
|
||||
import com.dfsek.terra.api.structures.parser.lang.functions.builtin.PowFunction;
|
||||
import com.dfsek.terra.api.structures.parser.lang.functions.builtin.SqrtFunction;
|
||||
import com.dfsek.terra.api.structures.parser.lang.keywords.flow.BreakKeyword;
|
||||
import com.dfsek.terra.api.structures.parser.lang.keywords.flow.ContinueKeyword;
|
||||
import com.dfsek.terra.api.structures.parser.lang.keywords.flow.FailKeyword;
|
||||
@@ -47,19 +44,16 @@ import com.dfsek.terra.api.structures.tokenizer.Token;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Tokenizer;
|
||||
import com.dfsek.terra.api.structures.tokenizer.exceptions.TokenizerException;
|
||||
import com.dfsek.terra.api.util.GlueList;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class Parser {
|
||||
private final String data;
|
||||
private final Map<String, FunctionBuilder<? extends Function<?>>> functions = new HashMap<>();
|
||||
private final Set<String> builtinFunctions = Sets.newHashSet("abs", "sqrt", "pow");
|
||||
|
||||
private String id;
|
||||
|
||||
@@ -67,7 +61,7 @@ public class Parser {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Parser addFunction(String name, FunctionBuilder<? extends Function<?>> functionBuilder) {
|
||||
public Parser registerFunction(String name, FunctionBuilder<? extends Function<?>> functionBuilder) {
|
||||
functions.put(name, functionBuilder);
|
||||
return this;
|
||||
}
|
||||
@@ -197,7 +191,7 @@ public class Parser {
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE, Token.Type.NUMBER_VARIABLE);
|
||||
Token name = tokens.get();
|
||||
|
||||
if(functions.containsKey(name.getContent()) || variableMap.containsKey(name.getContent()) || builtinFunctions.contains(name.getContent()))
|
||||
if(functions.containsKey(name.getContent()) || variableMap.containsKey(name.getContent()))
|
||||
throw new ParseException(name.getContent() + " is already defined in this scope", name.getPosition());
|
||||
|
||||
initializer = parseAssignment(forVar, tokens, variableMap);
|
||||
@@ -237,7 +231,7 @@ public class Parser {
|
||||
} else if(id.getType().equals(Token.Type.GROUP_BEGIN)) { // Parse grouped expression
|
||||
expression = parseGroup(tokens, variableMap);
|
||||
} else {
|
||||
if(functions.containsKey(id.getContent()) || builtinFunctions.contains(id.getContent()))
|
||||
if(functions.containsKey(id.getContent()))
|
||||
expression = parseFunction(tokens, false, variableMap);
|
||||
else if(variableMap.containsKey(id.getContent())) {
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.IDENTIFIER);
|
||||
@@ -382,7 +376,7 @@ public class Parser {
|
||||
Token name = tokens.get();
|
||||
ParserUtil.checkType(name, Token.Type.IDENTIFIER); // Name must be an identifier.
|
||||
|
||||
if(functions.containsKey(name.getContent()) || variableMap.containsKey(name.getContent()) || builtinFunctions.contains(name.getContent()))
|
||||
if(functions.containsKey(name.getContent()) || variableMap.containsKey(name.getContent()))
|
||||
throw new ParseException(name.getContent() + " is already defined in this scope", name.getPosition());
|
||||
|
||||
variableMap.put(name.getContent(), temp);
|
||||
@@ -414,7 +408,7 @@ public class Parser {
|
||||
Token identifier = tokens.consume();
|
||||
ParserUtil.checkType(identifier, Token.Type.IDENTIFIER); // First token must be identifier
|
||||
|
||||
if(!functions.containsKey(identifier.getContent()) && !builtinFunctions.contains(identifier.getContent()))
|
||||
if(!functions.containsKey(identifier.getContent()))
|
||||
throw new ParseException("No such function \"" + identifier.getContent() + "\"", identifier.getPosition());
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_BEGIN); // Second is body begin
|
||||
@@ -439,28 +433,8 @@ public class Parser {
|
||||
ParserUtil.checkReturnType(argument, builder.getArgument(i));
|
||||
}
|
||||
return builder.build(args, identifier.getPosition());
|
||||
} else {
|
||||
switch(identifier.getContent()) {
|
||||
case "abs":
|
||||
ParserUtil.checkReturnType(args.get(0), Returnable.ReturnType.NUMBER);
|
||||
if(args.size() != 1)
|
||||
throw new ParseException("Expected 1 argument; found " + args.size(), identifier.getPosition());
|
||||
return new AbsFunction(identifier.getPosition(), (Returnable<Number>) args.get(0));
|
||||
case "sqrt":
|
||||
ParserUtil.checkReturnType(args.get(0), Returnable.ReturnType.NUMBER);
|
||||
if(args.size() != 1)
|
||||
throw new ParseException("Expected 1 argument; found " + args.size(), identifier.getPosition());
|
||||
return new SqrtFunction(identifier.getPosition(), (Returnable<Number>) args.get(0));
|
||||
case "pow":
|
||||
ParserUtil.checkReturnType(args.get(0), Returnable.ReturnType.NUMBER);
|
||||
ParserUtil.checkReturnType(args.get(1), Returnable.ReturnType.NUMBER);
|
||||
if(args.size() != 2)
|
||||
throw new ParseException("Expected 1 argument; found " + args.size(), identifier.getPosition());
|
||||
return new PowFunction(identifier.getPosition(), (Returnable<Number>) args.get(0), (Returnable<Number>) args.get(1));
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unsupported function: " + identifier.getContent());
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException("Unsupported function: " + identifier.getContent());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang;
|
||||
|
||||
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 java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class Block implements Item<Block.ReturnInfo<?>> {
|
||||
private final List<Item<?>> items;
|
||||
@@ -21,9 +18,9 @@ public class Block implements Item<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public synchronized ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
for(Item<?> item : items) {
|
||||
Object result = item.apply(buffer, rotation, random, recursions);
|
||||
Object result = item.apply(implementationArguments);
|
||||
if(result instanceof ReturnInfo) {
|
||||
ReturnInfo<?> level = (ReturnInfo<?>) result;
|
||||
if(!level.getLevel().equals(ReturnLevel.NONE)) return level;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang;
|
||||
|
||||
/**
|
||||
* Arguments passed to {@link Item}s by the implementation
|
||||
*/
|
||||
public interface ImplementationArguments {
|
||||
}
|
||||
@@ -1,13 +1,9 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang;
|
||||
|
||||
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 java.util.Random;
|
||||
|
||||
public interface Item<T> {
|
||||
T apply(Buffer buffer, Rotation rotation, Random random, int recursions);
|
||||
T apply(ImplementationArguments implementationArguments);
|
||||
|
||||
Position getPosition();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.constants;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 java.util.Random;
|
||||
|
||||
public abstract class ConstantExpression<T> implements Returnable<T> {
|
||||
private final T constant;
|
||||
private final Position position;
|
||||
@@ -17,7 +14,7 @@ public abstract class ConstantExpression<T> implements Returnable<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public T apply(ImplementationArguments implementationArguments) {
|
||||
return constant;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class AbsFunction extends MathFunction {
|
||||
private final Returnable<Number> returnable;
|
||||
|
||||
public AbsFunction(Position position, Returnable<Number> returnable) {
|
||||
super(position);
|
||||
this.returnable = returnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return FastMath.abs(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
public abstract class MathFunction implements Function<Number> {
|
||||
private final Position position;
|
||||
|
||||
protected MathFunction(Position position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class PowFunction extends MathFunction {
|
||||
private final Returnable<Number> base;
|
||||
private final Returnable<Number> power;
|
||||
|
||||
public PowFunction(Position position, Returnable<Number> base, Returnable<Number> power) {
|
||||
super(position);
|
||||
this.base = base;
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return FastMath.pow(base.apply(buffer, rotation, random, recursions).doubleValue(), power.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class SqrtFunction extends MathFunction {
|
||||
private final Returnable<Number> returnable;
|
||||
|
||||
public SqrtFunction(Position position, Returnable<Number> returnable) {
|
||||
super(position);
|
||||
this.returnable = returnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return FastMath.sqrt(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.functions.def;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.buffer.Buffer;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class DefinedFunction<T> implements Function<T> {
|
||||
private final FunctionBlock<T> block;
|
||||
private final String name;
|
||||
@@ -19,8 +16,8 @@ public abstract class DefinedFunction<T> implements Function<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return block.apply(buffer, rotation, random, recursions);
|
||||
public T apply(ImplementationArguments implementationArguments) {
|
||||
return block.apply(implementationArguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.functions.def;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Item;
|
||||
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 java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class FunctionBlock<T> implements Item<T> {
|
||||
private final List<Item<?>> items;
|
||||
@@ -26,9 +24,9 @@ public class FunctionBlock<T> implements Item<T> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public synchronized T apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public synchronized T apply(ImplementationArguments implementationArguments) {
|
||||
for(Item<?> item : items) {
|
||||
Object result = item.apply(buffer, rotation, random, recursions);
|
||||
Object result = item.apply(implementationArguments);
|
||||
if(result instanceof Block.ReturnInfo) {
|
||||
Block.ReturnInfo<T> level = (Block.ReturnInfo<T>) result;
|
||||
if(level.getLevel().equals(Block.ReturnLevel.RETURN)) return level.getData();
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Keyword;
|
||||
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 java.util.Random;
|
||||
|
||||
public class BreakKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
|
||||
@@ -16,7 +13,7 @@ public class BreakKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.BREAK, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Keyword;
|
||||
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 java.util.Random;
|
||||
|
||||
public class ContinueKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
|
||||
@@ -16,7 +13,7 @@ public class ContinueKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.CONTINUE, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Keyword;
|
||||
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 java.util.Random;
|
||||
|
||||
public class FailKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
|
||||
@@ -16,7 +13,7 @@ public class FailKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.FAIL, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Keyword;
|
||||
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 java.util.Random;
|
||||
|
||||
public class ReturnKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
|
||||
@@ -16,7 +13,7 @@ public class ReturnKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.RETURN, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.keywords.looplike;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Item;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Keyword;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 java.util.Random;
|
||||
|
||||
public class ForKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Block conditional;
|
||||
private final Item<?> initializer;
|
||||
@@ -26,9 +23,9 @@ public class ForKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
for(initializer.apply(buffer, rotation, random, recursions); statement.apply(buffer, rotation, random, recursions); incrementer.apply(buffer, rotation, random, recursions)) {
|
||||
Block.ReturnInfo<?> level = conditional.apply(buffer, rotation, random, recursions);
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
for(initializer.apply(implementationArguments); statement.apply(implementationArguments); incrementer.apply(implementationArguments)) {
|
||||
Block.ReturnInfo<?> level = conditional.apply(implementationArguments);
|
||||
if(level.getLevel().equals(Block.ReturnLevel.BREAK)) break;
|
||||
if(level.getLevel().isReturnFast()) return level;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.keywords.looplike;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Keyword;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class IfKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Block conditional;
|
||||
@@ -27,15 +25,15 @@ public class IfKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
if(statement.apply(buffer, rotation, random, recursions)) return conditional.apply(buffer, rotation, random, recursions);
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
if(statement.apply(implementationArguments)) return conditional.apply(implementationArguments);
|
||||
else {
|
||||
for(Pair<Returnable<Boolean>, Block> pair : elseIf) {
|
||||
if(pair.getLeft().apply(buffer, rotation, random, recursions)) {
|
||||
return pair.getRight().apply(buffer, rotation, random, recursions);
|
||||
if(pair.getLeft().apply(implementationArguments)) {
|
||||
return pair.getRight().apply(implementationArguments);
|
||||
}
|
||||
}
|
||||
if(elseBlock != null) return elseBlock.apply(buffer, rotation, random, recursions);
|
||||
if(elseBlock != null) return elseBlock.apply(implementationArguments);
|
||||
}
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.NONE, null);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.keywords.looplike;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Keyword;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 java.util.Random;
|
||||
|
||||
public class WhileKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Block conditional;
|
||||
private final Returnable<Boolean> statement;
|
||||
@@ -21,9 +18,9 @@ public class WhileKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
while(statement.apply(buffer, rotation, random, recursions)) {
|
||||
Block.ReturnInfo<?> level = conditional.apply(buffer, rotation, random, recursions);
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
|
||||
while(statement.apply(implementationArguments)) {
|
||||
Block.ReturnInfo<?> level = conditional.apply(implementationArguments);
|
||||
if(level.getLevel().equals(Block.ReturnLevel.BREAK)) break;
|
||||
if(level.getLevel().isReturnFast()) return level;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 java.util.Random;
|
||||
|
||||
public abstract class BinaryOperation<I, O> implements Returnable<O> {
|
||||
private final Returnable<I> left;
|
||||
private final Returnable<I> right;
|
||||
@@ -26,7 +23,7 @@ public abstract class BinaryOperation<I, O> implements Returnable<O> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public O apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return apply(left.apply(buffer, rotation, random, recursions), right.apply(buffer, rotation, random, recursions));
|
||||
public O apply(ImplementationArguments implementationArguments) {
|
||||
return apply(left.apply(implementationArguments), right.apply(implementationArguments));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 java.util.Random;
|
||||
|
||||
public abstract class UnaryOperation<T> implements Returnable<T> {
|
||||
private final Returnable<T> input;
|
||||
private final Position position;
|
||||
@@ -19,8 +16,8 @@ public abstract class UnaryOperation<T> implements Returnable<T> {
|
||||
public abstract T apply(T input);
|
||||
|
||||
@Override
|
||||
public T apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return apply(input.apply(buffer, rotation, random, recursions));
|
||||
public T apply(ImplementationArguments implementationArguments) {
|
||||
return apply(input.apply(implementationArguments));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.variables;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Item;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 java.util.Random;
|
||||
|
||||
public class Assignment<T> implements Item<T> {
|
||||
private final Variable<T> delegate;
|
||||
private final Returnable<T> value;
|
||||
@@ -20,8 +17,8 @@ public class Assignment<T> implements Item<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized T apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
T val = value.apply(buffer, rotation, random, recursions);
|
||||
public synchronized T apply(ImplementationArguments implementationArguments) {
|
||||
T val = value.apply(implementationArguments);
|
||||
delegate.setValue(val);
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.dfsek.terra.api.structures.parser.lang.variables;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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 java.util.Random;
|
||||
|
||||
public class Getter implements Returnable<Object> {
|
||||
private final Variable<?> delegate;
|
||||
|
||||
@@ -20,7 +17,7 @@ public class Getter implements Returnable<Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Object apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public synchronized Object apply(ImplementationArguments implementationArguments) {
|
||||
return delegate.getValue();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.dfsek.terra.api.structures.script;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class TerraImplementationArguments implements ImplementationArguments {
|
||||
private final Buffer buffer;
|
||||
private final Rotation rotation;
|
||||
private final Random random;
|
||||
private final int recursions;
|
||||
|
||||
public TerraImplementationArguments(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
this.buffer = buffer;
|
||||
this.rotation = rotation;
|
||||
this.random = random;
|
||||
this.recursions = recursions;
|
||||
}
|
||||
|
||||
public Buffer getBuffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public int getRecursions() {
|
||||
return recursions;
|
||||
}
|
||||
|
||||
public Random getRandom() {
|
||||
return random;
|
||||
}
|
||||
|
||||
public Rotation getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.dfsek.terra.api.structures.script.builders;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.FunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class BinaryNumberFunctionBuilder implements FunctionBuilder<Function<Number>> {
|
||||
|
||||
private final BiFunction<Number, Number, Number> function;
|
||||
|
||||
public BinaryNumberFunctionBuilder(BiFunction<Number, Number, Number> function) {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<Number> build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new Function<Number>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments) {
|
||||
return function.apply(((Returnable<Number>) argumentList.get(0)).apply(implementationArguments), ((Returnable<Number>) argumentList.get(1)).apply(implementationArguments));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int argNumber() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
if(position == 0 || position == 1) return Returnable.ReturnType.NUMBER;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.dfsek.terra.api.structures.script.builders;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.FunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UnaryNumberFunctionBuilder implements FunctionBuilder<Function<Number>> {
|
||||
|
||||
private final java.util.function.Function<Number, Number> function;
|
||||
|
||||
public UnaryNumberFunctionBuilder(java.util.function.Function<Number, Number> function) {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<Number> build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new Function<Number>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments) {
|
||||
return function.apply(((Returnable<Number>) argumentList.get(0)).apply(implementationArguments));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int argNumber() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Returnable.ReturnType.NUMBER;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -5,18 +5,16 @@ import com.dfsek.terra.api.math.vector.Vector3;
|
||||
import com.dfsek.terra.api.platform.TerraPlugin;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
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.items.BufferedBlock;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class BlockFunction implements Function<Void> {
|
||||
private final BlockData data;
|
||||
private final Returnable<Number> x, y, z;
|
||||
@@ -33,13 +31,14 @@ public class BlockFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
public Void apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
BlockData rot = data.clone();
|
||||
RotationUtil.rotateBlockData(rot, rotation.inverse());
|
||||
buffer.addItem(new BufferedBlock(rot), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
||||
arguments.getBuffer().addItem(new BufferedBlock(rot), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,11 @@ 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.world.World;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
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.world.CheckCache;
|
||||
import com.dfsek.terra.api.world.generation.GenerationPhase;
|
||||
@@ -19,8 +19,6 @@ import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
|
||||
import com.dfsek.terra.config.templates.BiomeTemplate;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class CheckFunction implements Function<String> {
|
||||
private final TerraPlugin main;
|
||||
private final Returnable<Number> x, y, z;
|
||||
@@ -38,15 +36,16 @@ public class CheckFunction implements Function<String> {
|
||||
|
||||
|
||||
@Override
|
||||
public String apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public String apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
Location location = buffer.getOrigin().clone().add(new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
Location location = arguments.getBuffer().getOrigin().clone().add(new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
|
||||
return apply(location, buffer.getOrigin().getWorld());
|
||||
return apply(location, arguments.getBuffer().getOrigin().getWorld());
|
||||
}
|
||||
|
||||
private String apply(Location vector, World world) {
|
||||
|
||||
@@ -5,18 +5,16 @@ import com.dfsek.terra.api.math.vector.Vector3;
|
||||
import com.dfsek.terra.api.platform.TerraPlugin;
|
||||
import com.dfsek.terra.api.platform.world.entity.EntityType;
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
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.items.BufferedEntity;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityFunction implements Function<Void> {
|
||||
private final EntityType data;
|
||||
private final Returnable<Number> x, y, z;
|
||||
@@ -33,12 +31,13 @@ public class EntityFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
public Void apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
buffer.addItem(new BufferedEntity(data), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
arguments.getBuffer().addItem(new BufferedEntity(data), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,23 +2,20 @@ package com.dfsek.terra.api.structures.script.functions;
|
||||
|
||||
import com.dfsek.terra.api.math.vector.Vector2;
|
||||
import com.dfsek.terra.api.math.vector.Vector3;
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
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.items.Mark;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class GetMarkFunction implements Function<String> {
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
|
||||
public GetMarkFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Position position) throws ParseException {
|
||||
public GetMarkFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Position position) {
|
||||
this.position = position;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
@@ -26,11 +23,12 @@ public class GetMarkFunction implements Function<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
public String apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
Mark mark = buffer.getMark(new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
Mark mark = arguments.getBuffer().getMark(new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
return mark == null ? "" : mark.getContent();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,18 +4,16 @@ 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.structures.loot.LootTable;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
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.items.BufferedLootApplication;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import com.dfsek.terra.registry.LootRegistry;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class LootFunction implements Function<Void> {
|
||||
private final LootRegistry registry;
|
||||
private final Returnable<String> data;
|
||||
@@ -34,12 +32,13 @@ public class LootFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
public Void apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
String id = data.apply(buffer, rotation, random, recursions);
|
||||
String id = data.apply(implementationArguments);
|
||||
LootTable table = registry.get(id);
|
||||
|
||||
if(table == null) {
|
||||
@@ -47,7 +46,7 @@ public class LootFunction implements Function<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
buffer.addItem(new BufferedLootApplication(table, main), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
arguments.getBuffer().addItem(new BufferedLootApplication(table, main), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,17 +3,15 @@ package com.dfsek.terra.api.structures.script.functions;
|
||||
import com.dfsek.terra.api.math.vector.Vector2;
|
||||
import com.dfsek.terra.api.math.vector.Vector3;
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
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.items.Mark;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class MarkFunction implements Function<Void> {
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
@@ -28,12 +26,13 @@ public class MarkFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
public Void apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
buffer.setMark(new Mark(mark.apply(buffer, rotation, random, recursions)), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
arguments.getBuffer().setMark(new Mark(mark.apply(implementationArguments)), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,18 +5,16 @@ import com.dfsek.terra.api.math.vector.Vector3;
|
||||
import com.dfsek.terra.api.platform.TerraPlugin;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
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.items.BufferedPulledBlock;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class PullFunction implements Function<Void> {
|
||||
private final BlockData data;
|
||||
private final Returnable<Number> x, y, z;
|
||||
@@ -33,13 +31,14 @@ public class PullFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
public Void apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
BlockData rot = data.clone();
|
||||
RotationUtil.rotateBlockData(rot, rotation.inverse());
|
||||
buffer.addItem(new BufferedPulledBlock(rot), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
||||
arguments.getBuffer().addItem(new BufferedPulledBlock(rot), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.dfsek.terra.api.structures.script.functions;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomFunction implements Function<Integer> {
|
||||
private final Returnable<Number> numberReturnable;
|
||||
private final Position position;
|
||||
@@ -24,8 +22,8 @@ public class RandomFunction implements Function<Integer> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return random.nextInt(numberReturnable.apply(buffer, rotation, random, recursions).intValue()); // TODO: deterministic random
|
||||
public Integer apply(ImplementationArguments implementationArguments) {
|
||||
return ((TerraImplementationArguments) implementationArguments).getRandom().nextInt(numberReturnable.apply(implementationArguments).intValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.dfsek.terra.api.structures.script.functions;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
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.buffer.Buffer;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RecursionsFunction implements Function<Number> {
|
||||
private final Position position;
|
||||
|
||||
@@ -20,8 +18,8 @@ public class RecursionsFunction implements Function<Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return recursions;
|
||||
public Number apply(ImplementationArguments implementationArguments) {
|
||||
return ((TerraImplementationArguments) implementationArguments).getRecursions();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,19 +3,19 @@ package com.dfsek.terra.api.structures.script.functions;
|
||||
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.structures.parser.lang.ImplementationArguments;
|
||||
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.script.StructureScript;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.structure.Rotation;
|
||||
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.registry.ScriptRegistry;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class StructureFunction implements Function<Boolean> {
|
||||
private final ScriptRegistry registry;
|
||||
@@ -42,13 +42,14 @@ public class StructureFunction implements Function<Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
public Boolean apply(ImplementationArguments implementationArguments) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
|
||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
Vector2 xz = new Vector2(x.apply(implementationArguments).doubleValue(), z.apply(implementationArguments).doubleValue());
|
||||
|
||||
RotationUtil.rotateVector(xz, rotation);
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
String app = id.apply(buffer, rotation, random, recursions);
|
||||
String app = id.apply(implementationArguments);
|
||||
StructureScript script = registry.get(app);
|
||||
if(script == null) {
|
||||
main.getLogger().severe("No such structure " + app);
|
||||
@@ -56,7 +57,7 @@ public class StructureFunction implements Function<Boolean> {
|
||||
}
|
||||
|
||||
Rotation rotation1;
|
||||
String rotString = rotations.get(random.nextInt(rotations.size())).apply(buffer, rotation, random, recursions);
|
||||
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).apply(implementationArguments);
|
||||
try {
|
||||
rotation1 = Rotation.valueOf(rotString);
|
||||
} catch(IllegalArgumentException e) {
|
||||
@@ -64,9 +65,9 @@ public class StructureFunction implements Function<Boolean> {
|
||||
return null;
|
||||
}
|
||||
|
||||
Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ()));
|
||||
Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments).intValue(), FastMath.roundToInt(xz.getZ()));
|
||||
|
||||
return script.executeInBuffer(new IntermediateBuffer(buffer, offset), random, rotation.rotate(rotation1), recursions + 1);
|
||||
return script.executeInBuffer(new IntermediateBuffer(arguments.getBuffer(), offset), arguments.getRandom(), arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,27 +3,25 @@ package structure;
|
||||
import com.dfsek.terra.api.structures.parser.Parser;
|
||||
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.ImplementationArguments;
|
||||
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.FunctionBuilder;
|
||||
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 org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class ParserTest {
|
||||
@Test
|
||||
public void parse() throws IOException, ParseException {
|
||||
Parser parser = new Parser(IOUtils.toString(getClass().getResourceAsStream("/test.tesf")));
|
||||
|
||||
parser.addFunction("test", new FunctionBuilder<Test1>() {
|
||||
parser.registerFunction("test", new FunctionBuilder<Test1>() {
|
||||
@Override
|
||||
public Test1 build(List<Returnable<?>> argumentList, Position position) throws ParseException {
|
||||
public Test1 build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new Test1(argumentList.get(0), argumentList.get(1), position);
|
||||
}
|
||||
|
||||
@@ -51,9 +49,9 @@ public class ParserTest {
|
||||
long t = System.nanoTime() - l;
|
||||
System.out.println("Took " + (double) t / 1000000);
|
||||
|
||||
block.apply(null, Rotation.NONE, new Random(), 0);
|
||||
block.apply(null);
|
||||
|
||||
block.apply(null, Rotation.NONE, new Random(), 0);
|
||||
block.apply(null);
|
||||
}
|
||||
|
||||
private static class Test1 implements Function<Void> {
|
||||
@@ -68,8 +66,8 @@ public class ParserTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
System.out.println("string: " + a.apply(buffer, rotation, random, recursions) + ", double: " + b.apply(buffer, rotation, random, recursions));
|
||||
public Void apply(ImplementationArguments implementationArguments) {
|
||||
System.out.println("string: " + a.apply(implementationArguments) + ", double: " + b.apply(implementationArguments));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.dfsek.terra.bukkit.command.command.structure;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.lang.constants.NumericConstant;
|
||||
import com.dfsek.terra.api.structures.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.script.functions.CheckFunction;
|
||||
import com.dfsek.terra.api.structures.structure.Rotation;
|
||||
import com.dfsek.terra.api.structures.structure.buffer.StructureBuffer;
|
||||
@@ -32,9 +33,9 @@ public class SpawnCommand extends WorldCommand implements DebugCommand {
|
||||
int z = p.getBlockZ();
|
||||
Position dummy = new Position(0, 0);
|
||||
com.dfsek.terra.api.platform.world.World w = BukkitAdapter.adapt(world);
|
||||
String check = new CheckFunction(getMain(), new NumericConstant(0, dummy), new NumericConstant(0, dummy), new NumericConstant(0, dummy), getMain().getWorld(w).getConfig().getCheckCache(), dummy).apply(new StructureBuffer(
|
||||
String check = new CheckFunction(getMain(), new NumericConstant(0, dummy), new NumericConstant(0, dummy), new NumericConstant(0, dummy), getMain().getWorld(w).getConfig().getCheckCache(), dummy).apply(new TerraImplementationArguments(new StructureBuffer(
|
||||
new com.dfsek.terra.api.math.vector.Location(w, x, y, z)
|
||||
), Rotation.NONE, new FastRandom(), 0);
|
||||
), Rotation.NONE, new FastRandom(), 0));
|
||||
|
||||
sender.sendMessage("Found: " + check);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user