tracking and tracing

This commit is contained in:
gnembon committed 2019-07-20 18:53:17 -03:00
1 parent eee23e4ba6
commit 3c96345a60
20 files changed
+404 -128

No files matched your search

@@ -679,7 +679,6 @@ public class CarpetExpression
this.expr.addLazyFunction("blast_resistance", -1, (c, t, lv) ->
genericStateTest(c, "blast_resistance", lv, (s, p, w) -> new NumericValue(s.getBlock().getBlastResistance())));
// Deprecated
this.expr.addLazyFunction("top", -1, (c, t, lv) ->
{
String type = lv.get(0).evalValue(c).getString().toLowerCase(Locale.ROOT);
@@ -693,20 +692,10 @@ public class CarpetExpression
case "surface": htype = Heightmap.Type.WORLD_SURFACE; break;
default: throw new InternalExpressionException("Unknown heightmap type: "+type);
}
int x;
int z;
Value v1 = lv.get(1).evalValue(c);
if (v1 instanceof BlockValue)
{
BlockPos inpos = ((BlockValue)v1).getPos();
x = inpos.getX();
z = inpos.getZ();
}
else
{
x = (int) NumericValue.asNumber(lv.get(1).evalValue(c)).getLong();
z = (int) NumericValue.asNumber(lv.get(2).evalValue(c)).getLong();
}
BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext)c, lv, 1);
BlockPos pos = locator.block.getPos();
int x = pos.getX();
int z = pos.getZ();
Value retval = new NumericValue(((CarpetContext)c).s.getWorld().getChunk(x >> 4, z >> 4).sampleHeightmap(htype, x & 15, z & 15) + 1);
return (c_, t_) -> retval;
//BlockPos pos = new BlockPos(x,y,z);
@@ -965,24 +954,6 @@ public class CarpetExpression
return (_c, _t) -> res;
});
/*this.expr.addLazyFunction("trace_block", -1, (c, t, lv) -> {
Value entityValue = lv.get(0).evalValue(c);
if (!(entityValue instanceof EntityValue))
throw new InternalExpressionException("First argument of trace_block should be an entity");
Entity e = ((EntityValue) entityValue).getEntity();
HitResult hitres = Tracer.traceEntityLook(e, 20);
switch (hitres.getType())
{
case MISS:
return (_c, _t) -> Value.NULL;
case BLOCK:
return (_c, _t) -> new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() );
case ENTITY:
return (_c, _t) -> new EntityValue(((EntityHitResult)hitres).getEntity());
}
return LazyValue.NULL;
});*/
this.expr.addLazyFunction("set_biome", -1, (c, t, lv) -> {
CarpetContext cc = (CarpetContext)c;
BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0);
@@ -1736,7 +1707,6 @@ public class CarpetExpression
return (c_, t_) -> ListValue.wrap(retlist);
});
this.expr.addLazyFunction("query", -1, (c, t, lv) -> {
if (lv.size()<2)
{
@@ -2516,15 +2486,16 @@ public class CarpetExpression
this.expr.addLazyFunction("print", 1, (c, t, lv) ->
{
ServerCommandSource s = ((CarpetContext)c).s;
Value res = lv.get(0).evalValue(c);
if (s.getEntity() instanceof PlayerEntity)
{
Messenger.m((PlayerEntity) s.getEntity(), "w "+ lv.get(0).evalValue(c).getString());
Messenger.m((PlayerEntity) s.getEntity(), "w "+ res.getString());
}
else
{
Messenger.m(s, "w "+ lv.get(0).evalValue(c).getString());
Messenger.m(s, "w "+ res.getString());
}
return lv.get(0); // pass through for variables
return (_c, _t) -> res; // pass through for variables
});
+25 -60
View File
@@ -498,21 +498,9 @@ public class Expression implements Cloneable
Value.assertNotNull(v);
return lazyfun.apply(c, t, v);
}
catch (InternalExpressionException exc)
catch (RuntimeException exc)
{
throw new ExpressionException(e, token, exc.getMessage());
}
catch (ArithmeticException exc)
{
throw new ExpressionException(e, token, "Your math is wrong, "+exc.getMessage());
}
catch (ExitStatement exit)
{
throw exit;
}
catch (Exception exc)
{
throw new ExpressionException(e, token, "Error while evaluating expression: "+exc.getMessage());
throw handleCodeException(exc, e, token);
}
}
});
@@ -532,21 +520,9 @@ public class Expression implements Cloneable
Value.assertNotNull(v1, v2);
return lazyfun.apply(c, type, e, t, v1, v2);
}
catch (InternalExpressionException exc) // might not actually throw it
catch (RuntimeException exc)
{
throw new ExpressionException(e, t, exc.getMessage());
}
catch (ArithmeticException exc)
{
throw new ExpressionException(e, t, "Your math is wrong, "+exc.getMessage());
}
catch (ExitStatement exit)
{
throw exit;
}
catch (Exception exc)
{
throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage());
throw handleCodeException(exc, e, t);
}
}
});
@@ -565,26 +541,27 @@ public class Expression implements Cloneable
Value.assertNotNull(v1, v2);
return lazyfun.apply(c, t, v1, v2);
}
catch (InternalExpressionException exc)
catch (RuntimeException exc)
{
throw new ExpressionException(e, token, exc.getMessage());
}
catch (ArithmeticException exc)
{
throw new ExpressionException(e, token, "Your math is wrong, "+exc.getMessage());
}
catch (ExitStatement exit)
{
throw exit;
}
catch (Exception exc)
{
throw new ExpressionException(e, token, "Error while evaluating expression: "+exc.getMessage());
throw handleCodeException(exc, e, token);
}
}
});
}
private RuntimeException handleCodeException(RuntimeException exc, Expression e, Tokenizer.Token token)
{
if (exc instanceof ExitStatement)
return exc;
if (exc instanceof InternalExpressionException)
return new ExpressionException(e, token, exc.getMessage());
if (exc instanceof ArithmeticException)
return new ExpressionException(e, token, "Your math is wrong, "+exc.getMessage());
if (exc instanceof ExpressionException)
return exc;
return new ExpressionException(e, token, "Error while evaluating expression: "+exc.getMessage());
}
private void addUnaryOperator(String surface, boolean leftAssoc, Function<Value, Value> fun)
{
operators.put(surface+"u", new AbstractUnaryOperator(precedence.get("unary+-!"), leftAssoc)
@@ -679,22 +656,10 @@ public class Expression implements Cloneable
{
return fun.apply(c, i, lazyParams);
}
catch (InternalExpressionException exc)
catch (RuntimeException exc)
{
throw new ExpressionException(e, t, exc.getMessage());
throw handleCodeException(exc, e, t);
}
catch (ArithmeticException exc)
{
throw new ExpressionException(e, t, "Your math is wrong, "+exc.getMessage());
}
catch (ExitStatement exit)
{
throw exit;
}
//catch (Exception exc)
//{
// throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage());
//}
}
});
}
@@ -770,7 +735,7 @@ public class Expression implements Cloneable
{
throw new ExpressionException(function_context, t, "Your math is wrong, "+exc.getMessage());
}
catch (ExitStatement exit)
catch (ExitStatement | ExpressionException exit)
{
throw exit;
}
@@ -1060,7 +1025,7 @@ public class Expression implements Cloneable
Value ret = lv.get(lv.size() - 1).evalValue(c);
return (cc, tt) -> ret;
}
return (cc, tt) -> Value.ZERO;
return (cc, tt) -> Value.NULL;
});
}
@@ -1892,8 +1857,6 @@ public class Expression implements Cloneable
return LazyListValue.range(from, to, step);
});
addUnaryFunction("is_list", value -> (value instanceof ListValue)? Value.TRUE : Value.FALSE);
addBinaryFunction("get", (v1, v2) -> v1.getElementAt(v2));
//Deprecated, use "get" instead
@@ -2512,6 +2475,8 @@ public class Expression implements Cloneable
}
});
addUnaryFunction("type", v -> new StringValue(v.getTypeString()));
addUnaryFunction("length", v -> new NumericValue(v.length()));
addLazyFunction("rand", 1, (c, t, lv) -> {
Value argument = lv.get(0).evalValue(c);
@@ -73,12 +73,10 @@ public class ScriptHost
try
{
ServerPlayerEntity player = source.getPlayer();
CarpetSettings.LOG.error("Retrieving "+player.getName().getString()+" host for "+getName());
return retrieveForExecution(player.getName().getString());
}
catch (CommandSyntaxException e)
{
CarpetSettings.LOG.error("Retrieving global host for "+getName());
return retrieveForExecution((String)null);
}
}
@@ -241,6 +241,12 @@ public class BlockValue extends Value
return this != NULL && !getBlockState().isAir();
}
@Override
public String getTypeString()
{
return "block";
}
@Override
public Value clone()
{
@@ -1,6 +1,7 @@
package carpet.script.value;
import carpet.fakes.MobEntityInterface;
import carpet.helpers.Tracer;
import carpet.script.exception.InternalExpressionException;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -30,6 +31,9 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
@@ -124,6 +128,12 @@ public class EntityValue extends Value
return this.get(what, null);
}
@Override
public String getTypeString()
{
return "entity";
}
public static Pair<EntityType<?>, Predicate<? super Entity>> getPredicate(String who)
{
Pair<EntityType<?>, Predicate<? super Entity>> res = entityPredicates.get(who);
@@ -310,20 +320,65 @@ public class EntityValue extends Value
return new StringValue(Direction.getEntityFacingOrder(e)[index].asString());
});
put("trace", (e, a) ->
{
float reach = 4.5f;
boolean entities = true;
boolean liquids = false;
if (a!=null)
{
if (!(a instanceof ListValue))
{
reach = (float) NumericValue.asNumber(a).getDouble();
}
else
{
List<Value> args = ((ListValue) a).getItems();
if (args.size()==0)
throw new InternalExpressionException("trace needs more arguments");
reach = (float) NumericValue.asNumber(args.get(0)).getDouble();
if (args.size() > 1)
{
entities = false;
for (int i = 1; i < args.size(); i++)
{
String what = args.get(i).getString();
if (what.equalsIgnoreCase("entities"))
entities = true;
else if (what.equalsIgnoreCase("blocks")) {}
else if (what.equalsIgnoreCase("liquids"))
liquids = true;
else throw new InternalExpressionException("Incorrect tracing: "+what);
}
}
}
}
else if (e instanceof ServerPlayerEntity && ((ServerPlayerEntity) e).interactionManager.isCreative())
{
reach = 5.0f;
}
HitResult hitres;
if (entities)
hitres = Tracer.rayTrace(e, 1, reach, liquids);
else
hitres = Tracer.rayTraceBlocks(e, 1, reach, liquids);
switch (hitres.getType())
{
case MISS: return Value.NULL;
case BLOCK: return new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() );
case ENTITY: return new EntityValue(((EntityHitResult)hitres).getEntity());
}
return Value.NULL;
});
put("nbt",(e, a) -> {
CompoundTag nbttagcompound = e.toTag((new CompoundTag()));
if (a==null)
return new NBTSerializableValue(nbttagcompound);//StringValue(nbttagcompound.toString());
NbtPathArgumentType.NbtPath path;
try
{
path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(a.getString()));
}
catch (CommandSyntaxException exc)
{
throw new InternalExpressionException("Incorrect path: "+a.getString());
}
String res = null;
NbtPathArgumentType.NbtPath path = NBTSerializableValue.cachePath(a.getString());
try
{
List<Tag> tags = path.get(nbttagcompound);
@@ -331,7 +386,7 @@ public class EntityValue extends Value
return Value.NULL;
if (tags.size()==1)
return NBTSerializableValue.decodeTag(tags.get(0));
return ListValue.wrap(tags.stream().map(t -> NBTSerializableValue.decodeTag(t)).collect(Collectors.toList()));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList()));
}
catch (CommandSyntaxException ignored) { }
return Value.NULL;
@@ -504,9 +559,13 @@ public class EntityValue extends Value
});
put("custom_name", (e, v) -> {
String name = v.getString();
if (name.isEmpty())
if (v instanceof NullValue)
{
e.setCustomNameVisible(false);
e.setCustomName(null);
return;
}
e.setCustomNameVisible(true);
e.setCustomName(new LiteralText(v.getString()));
});
put("dismount", (e, v) -> e.stopRiding() );
@@ -1,7 +1,7 @@
package carpet.script.value;
import carpet.script.exception.ExpressionException;
import carpet.script.value.Value;
import carpet.script.exception.InternalExpressionException;
import java.util.List;
@@ -23,6 +23,12 @@ public class FunctionSignatureValue extends Value
throw new ExpressionException("Function "+identifier+" is not defined yet");
}
@Override
public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct.");
}
@Override
public Value clone()
{
@@ -1,6 +1,6 @@
package carpet.script.value;
import carpet.script.value.Value;
import carpet.script.exception.InternalExpressionException;
public class GlobalValue extends Value
{
@@ -21,4 +21,10 @@ public class GlobalValue extends Value
{
return false;
}
@Override
public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct.");
}
}
@@ -111,5 +111,10 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
{
throw new InternalExpressionException("get element can only be obtained for regular lists");
}
@Override
public String getTypeString()
{
return "iterator";
}
}
@@ -326,6 +326,11 @@ public class ListValue extends Value
index = index % numitems;
return items.get((int)index);
}
@Override
public String getTypeString()
{
return "list";
}
@@ -26,8 +26,6 @@ import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static java.lang.Math.abs;
public class NBTSerializableValue extends Value
{
private String nbtString = null;
@@ -224,31 +222,38 @@ public class NBTSerializableValue extends Value
}
}
@Override
public Value getElementAt(Value value)
private static Map<String, NbtPathArgumentType.NbtPath> pathCache = new HashMap<>();
public static NbtPathArgumentType.NbtPath cachePath(String arg)
{
String pathString = value.getString();
//return Value.NULL;
NbtPathArgumentType.NbtPath path;
NbtPathArgumentType.NbtPath res = pathCache.get(arg);
if (res != null)
return res;
try
{
path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(pathString));
res = NbtPathArgumentType.nbtPath().method_9362(new StringReader(arg));
}
catch (CommandSyntaxException exc)
{
throw new InternalExpressionException("Incorrect nbt path: "+pathString);
throw new InternalExpressionException("Incorrect nbt path: "+arg);
}
String res = null;
if (pathCache.size() > 1024)
pathCache.clear();
pathCache.put(arg, res);
return res;
}
@Override
public Value getElementAt(Value value)
{
NbtPathArgumentType.NbtPath path = cachePath(value.getString());
try
{
List<Tag> tags = path.get(getTag());
if (tags.size()==0)
return Value.NULL;
if (tags.size()==1)
return new NBTSerializableValue(tags.get(0));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList()));
return NBTSerializableValue.decodeTag(tags.get(0));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList()));
}
catch (CommandSyntaxException ignored) { }
return Value.NULL;
@@ -259,4 +264,10 @@ public class NBTSerializableValue extends Value
index = index % numitems;
return items.get((int)index); */
}
@Override
public String getTypeString()
{
return "nbt";
}
}
@@ -38,4 +38,10 @@ public class NullValue extends NumericValue // TODO check nonsingleton code
{
return o instanceof NullValue ? 0 : -1;
}
@Override
public String getTypeString()
{
return "null";
}
}
@@ -167,4 +167,10 @@ public class NumericValue extends Value
return getLong();
}
@Override
public String getTypeString()
{
return "number";
}
}
@@ -24,4 +24,10 @@ public class StringValue extends Value
{
this.str = str;
}
@Override
public String getTypeString()
{
return "number";
}
}
@@ -177,4 +177,6 @@ public abstract class Value implements Comparable<Value>, Cloneable
{
throw new InternalExpressionException("get element can only be obtained for lists and nbt values");
}
public abstract String getTypeString();
}