Merge pull request #115 from gnembon/threads

Threads
This commit is contained in:
gnembon authored and GitHub committed 2019-12-25 03:12:26 -04:00
commit 8a4ad0f353
22 files changed
+955 -310

No files matched your search

@@ -11,7 +11,7 @@ import java.util.Random;
@Mixin(targets = "net.minecraft.entity.mob.BlazeEntity$ShootFireballGoal")
public class BlazeEntityShootFireballGoal_extremeMixin
{
@Redirect(method = "tick", expect = 2, at = @At(
@Redirect(method = "tick()V", expect = 2, at = @At(
value = "INVOKE",
target = "Ljava/util/Random;nextGaussian()D"
))
@@ -26,26 +26,20 @@ public abstract class ContainerMixin
@Shadow public abstract void sendContentUpdates();
@Inject( method = "onSlotClick", at = @At(value = "HEAD"), cancellable = true)
private void onThrowClick(
int int_1,
int int_2,
SlotActionType slotActionType_1,
PlayerEntity playerEntity_1,
CallbackInfoReturnable<ItemStack> cir
)
private void onThrowClick( int slotId, int clickData, SlotActionType actionType, PlayerEntity playerEntity, CallbackInfoReturnable<ItemStack> cir)
{
if (slotActionType_1 == SlotActionType.THROW && CarpetSettings.ctrlQCraftingFix && playerEntity_1.inventory.getCursorStack().isEmpty() && int_1 >= 0)
if (actionType == SlotActionType.THROW && CarpetSettings.ctrlQCraftingFix && playerEntity.inventory.getCursorStack().isEmpty() && slotId >= 0)
{
ItemStack itemStack_1 = ItemStack.EMPTY;
Slot slot_4 = slotList.get(int_1);
if (slot_4 != null && slot_4.hasStack() && slot_4.canTakeItems(playerEntity_1))
Slot slot_4 = slotList.get(slotId);
if (slot_4 != null && slot_4.hasStack() && slot_4.canTakeItems(playerEntity))
{
if(int_1 == 0 && int_2 == 1)
if(slotId == 0 && clickData == 1)
{
Item craftedItem = slot_4.getStack().getItem();
while(slot_4.hasStack() && slot_4.getStack().getItem() == craftedItem)
{
this.onSlotClick(int_1, 0, SlotActionType.THROW, playerEntity_1);
this.onSlotClick(slotId, 0, SlotActionType.THROW, playerEntity);
}
this.sendContentUpdates();
cir.setReturnValue(itemStack_1);
@@ -4,14 +4,18 @@ import com.mojang.authlib.GameProfile;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.stat.Stat;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static carpet.script.CarpetEventServer.Event.PLAYER_FINISHED_USING_ITEM;
import static carpet.script.CarpetEventServer.Event.STATISTICS;
@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntity_scarpetEventMixin extends PlayerEntity
@@ -43,4 +47,10 @@ public abstract class ServerPlayerEntity_scarpetEventMixin extends PlayerEntity
super.method_6040();
}
}
@Inject(method = "increaseStat", at = @At("HEAD"))
private void grabStat(Stat<?> stat, int amount, CallbackInfo ci)
{
STATISTICS.onPlayerStatistic((ServerPlayerEntity) (Object)this, stat, amount);
}
}
@@ -21,14 +21,4 @@ public class WorldChunk_fillUpdatesMixin
if (!CarpetSettings.impendingFillSkipUpdates)
blockState.onBlockAdded(world_1, blockPos_1, blockState_1, boolean_1);
}
@Redirect(method = "setBlockState", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/block/BlockState;onBlockRemoved(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Z)V"
))
private void onRemoved(BlockState blockState, World world_1, BlockPos blockPos_1, BlockState blockState_1, boolean boolean_1)
{
if (!CarpetSettings.impendingFillSkipUpdates)
blockState.onBlockRemoved(world_1, blockPos_1, blockState_1, boolean_1);
}
}
@@ -5,6 +5,7 @@ import carpet.script.value.BlockValue;
import carpet.script.value.EntityValue;
import carpet.script.value.FunctionValue;
import carpet.script.value.ListValue;
import carpet.script.value.NBTSerializableValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
@@ -13,11 +14,15 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.entity.Entity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.stat.Stat;
import net.minecraft.stat.Stats;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.dimension.DimensionType;
import org.apache.commons.lang3.tuple.Pair;
@@ -25,10 +30,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -403,7 +410,32 @@ public class CarpetEventServer
{
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource);
}
};
},
STATISTICS("statistic", new CallbackList(4))
{
private <T> Identifier getStatId(Stat<T> stat)
{
return stat.getType().getRegistry().getId(stat.getValue());
}
private Set<Identifier> skippedStats = new HashSet<Identifier>(){{
add(Stats.TIME_SINCE_DEATH);
add(Stats.TIME_SINCE_REST);
add(Stats.PLAY_ONE_MINUTE);
}};
@Override
public void onPlayerStatistic(ServerPlayerEntity player, Stat<?> stat, int amount)
{
Identifier id = getStatId(stat);
if (skippedStats.contains(id)) return;
handler.call( () -> Arrays.asList(
((c, t) -> new EntityValue(player)),
((c, t) -> new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.STAT_TYPE.getId(stat.getType())))),
((c, t) -> new StringValue(NBTSerializableValue.nameFromRegistryId(id))),
((c, t) -> new NumericValue(amount))
), player::getCommandSource);
}
}
;
// on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls
@@ -427,6 +459,7 @@ public class CarpetEventServer
//stubs for calls just to ease calls in vanilla code so they don't need to deal with scarpet value types
public void onTick() { }
public void onPlayerEvent(ServerPlayerEntity player) { }
public void onPlayerStatistic(ServerPlayerEntity player, Stat<?> stat, int amount) { }
public void onMountControls(ServerPlayerEntity player, float strafeSpeed, float forwardSpeed, boolean jumping, boolean sneaking) { }
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) { }
public void onBlockAction(ServerPlayerEntity player, BlockPos blockpos, Direction facing) { }
@@ -10,6 +10,7 @@ import carpet.script.exception.CarpetExpressionException;
import carpet.script.exception.ExitStatement;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException;
import carpet.CarpetSettings;
import carpet.script.value.BlockValue;
import carpet.script.value.EntityValue;
import carpet.script.value.FunctionValue;
@@ -19,8 +20,8 @@ import carpet.script.value.NBTSerializableValue;
import carpet.script.value.NullValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.ThreadValue;
import carpet.script.value.Value;
import carpet.CarpetSettings;
import carpet.utils.BlockInfo;
import carpet.utils.Messenger;
import com.google.common.collect.Sets;
@@ -105,7 +106,6 @@ import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static carpet.script.value.NBTSerializableValue.nameFromRegistryId;
import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
@@ -126,7 +126,7 @@ import static java.lang.Math.sqrt;
* players using <code>player('*')</code>, which only returns players in current dimension, or use
* <code>in_dimension(expr)</code> function.</p>
*/
public class CarpetExpression
public class CarpetExpression
{
private ServerCommandSource source;
@@ -770,7 +770,7 @@ public class CarpetExpression
booleanStateTest(c, "liquid", lv, (s, p) -> !s.getFluidState().isEmpty()));
this.expr.addLazyFunction("flammable", -1, (c, t, lv) ->
booleanStateTest(c, "flammable", lv, (s, p) -> !s.getMaterial().isBurnable()));
booleanStateTest(c, "flammable", lv, (s, p) -> s.getMaterial().isBurnable()));
this.expr.addLazyFunction("transparent", -1, (c, t, lv) ->
booleanStateTest(c, "transparent", lv, (s, p) -> !s.getMaterial().isSolid()));
@@ -969,27 +969,29 @@ public class CarpetExpression
if (sourceBlockState == targetBlockState && data == null)
return (c_, t_) -> Value.FALSE;
CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates;
BlockState finalSourceBlockState = sourceBlockState;
BlockPos targetPos = targetLocator.block.getPos();
Clearable.clear(world.getBlockEntity(targetPos));
world.setBlockState(targetPos, sourceBlockState, 2);
if ( data != null)
cc.s.getMinecraftServer().executeSync( () ->
{
BlockEntity be = world.getBlockEntity(targetPos);
if (be != null)
CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates;
Clearable.clear(world.getBlockEntity(targetPos));
world.setBlockState(targetPos, finalSourceBlockState, 2);
if (data != null)
{
CompoundTag destTag = data.method_10553();
destTag.putInt("x", targetPos.getX());
destTag.putInt("y", targetPos.getY());
destTag.putInt("z", targetPos.getZ());
be.fromTag(destTag);
be.markDirty();
BlockEntity be = world.getBlockEntity(targetPos);
if (be != null)
{
CompoundTag destTag = data.method_10553();
destTag.putInt("x", targetPos.getX());
destTag.putInt("y", targetPos.getY());
destTag.putInt("z", targetPos.getZ());
be.fromTag(destTag);
be.markDirty();
}
}
}
CarpetSettings.impendingFillSkipUpdates = false;
Value retval = new BlockValue(sourceBlockState, world, targetLocator.block.getPos());
CarpetSettings.impendingFillSkipUpdates = false;
});
Value retval = new BlockValue(finalSourceBlockState, world, targetLocator.block.getPos());
return (c_, t_) -> retval;
});
@@ -2862,6 +2864,7 @@ public class CarpetExpression
CarpetContext cc = (CarpetContext)c;
BlockState targetBlock = null;
BlockValue.VectorLocator pointLocator;
boolean interactable = true;
String name;
try
{
@@ -2869,8 +2872,14 @@ public class CarpetExpression
name = nameValue instanceof NullValue ? "" : nameValue.getString();
pointLocator = BlockValue.locateVec(cc, lv, 1, true);
if (lv.size()>pointLocator.offset)
targetBlock = BlockValue.fromParams(cc, lv, pointLocator.offset, true).block.getBlockState();
{
BlockValue.LocatorResult blockLocator = BlockValue.fromParams(cc, lv, pointLocator.offset, true, true);
if (blockLocator.block != null) targetBlock = blockLocator.block.getBlockState();
if (lv.size() > blockLocator.offset)
{
interactable = lv.get(blockLocator.offset).evalValue(c, Context.BOOLEAN).getBoolean();
}
}
}
catch (IndexOutOfBoundsException e)
{
@@ -2898,6 +2907,7 @@ public class CarpetExpression
armorstand.setNoGravity(true);
armorstand.setInvisible(true);
armorstand.setInvulnerable(true);
armorstand.getDataTracker().set(ArmorStandEntity.ARMOR_STAND_FLAGS, (byte)(interactable?8 : 16|8));
cc.s.getWorld().spawnEntity(armorstand);
EntityValue result = new EntityValue(armorstand);
return (_c, _t) -> result;
@@ -2949,6 +2959,17 @@ public class CarpetExpression
return (_c, _t) -> res; // pass through for variables
});
//"overidden" native call to cancel if on main thread
this.expr.addLazyFunction("join_task", 1, (c, t, lv) -> {
if (((CarpetContext)c).s.getMinecraftServer().isOnThread())
throw new InternalExpressionException("'join_task' cannot be called from main thread to avoid deadlocks");
Value v = lv.get(0).evalValue(c);
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'join_task' could only be used with a task value");
Value ret = ((ThreadValue) v).join();
return (_c, _t) -> ret;
});
this.expr.addLazyFunction("logger", 1, (c, t, lv) ->
{
Value res = lv.get(0).evalValue(c);
@@ -2982,6 +3003,7 @@ public class CarpetExpression
this.expr.addLazyFunction("game_tick", -1, (c, t, lv) -> {
ServerCommandSource s = ((CarpetContext)c).s;
if (!s.getMinecraftServer().isOnThread()) throw new InternalExpressionException("Unable to run ticks from threads");
((MinecraftServerInterface)s.getMinecraftServer()).forceTick( () -> System.nanoTime()- CarpetServer.scriptServer.tickStart<50000000L);
if (lv.size()>0)
{
@@ -3008,7 +3030,7 @@ public class CarpetExpression
this.expr.addLazyFunction("current_dimension", 0, (c, t, lv) -> {
ServerCommandSource s = ((CarpetContext)c).s;
Value retval = new StringValue(nameFromRegistryId(Registry.DIMENSION.getId(s.getWorld().dimension.getType())));
Value retval = new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.DIMENSION.getId(s.getWorld().dimension.getType())));
return (cc, tt) -> retval;
});
@@ -3064,12 +3086,19 @@ public class CarpetExpression
if (lv.size() <= locator.offset)
throw new InternalExpressionException("'plop' needs extra argument indicating what to plop");
String what = lv.get(locator.offset).evalValue(c).getString();
Boolean res = FeatureGenerator.spawn(what, ((CarpetContext)c).s.getWorld(), locator.block.getPos());
if (res == null)
return (c_, t_) -> Value.NULL;
if (what.equalsIgnoreCase("boulder")) // there might be more of those
this.forceChunkUpdate(locator.block.getPos(), ((CarpetContext)c).s.getWorld());
return (c_, t_) -> new NumericValue(res);
Value [] result = new Value[]{Value.NULL};
((CarpetContext)c).s.getMinecraftServer().executeSync( () ->
{
Boolean res = FeatureGenerator.spawn(what, ((CarpetContext) c).s.getWorld(), locator.block.getPos());
if (res == null)
return;
if (what.equalsIgnoreCase("boulder")) // there might be more of those
this.forceChunkUpdate(locator.block.getPos(), ((CarpetContext) c).s.getWorld());
result[0] = new NumericValue(res);
});
Value ret = result[0]; // preventing from lazy evaluating of the result in case a future completes later
return (_c, _t) -> ret;
});
this.expr.addLazyFunction("schedule", -1, (c, t, lv) -> {
@@ -3128,7 +3157,7 @@ public class CarpetExpression
String file = null;
if (lv.size()>1)
{
String origfile = lv.get(0).evalValue(c).getString();
String origfile = lv.get(1).evalValue(c).getString();
file = origfile.toLowerCase(Locale.ROOT).replaceAll("[^A-Za-z0-9]", "");
if (file.isEmpty())
{
@@ -20,6 +20,7 @@ import net.minecraft.util.math.BlockPos;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@@ -217,6 +218,35 @@ public class CarpetScriptHost extends ScriptHost
super.onClose();
if (this.saveTimeout > 0)
dumpState();
FunctionValue closing = globalFunctions.get("__on_close");
if (closing != null)
{
try
{
callUDF(BlockPos.ORIGIN, CarpetServer.minecraft_server.getCommandSource(), closing, Collections.emptyList());
}
catch (InvalidCallbackException ignored)
{
}
}
userHosts.forEach((key, value) ->
{
FunctionValue userClosing = value.globalFunctions.get("__on_close");
if (userClosing != null)
{
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(key);
ServerCommandSource source = (player != null)?player.getCommandSource():CarpetServer.minecraft_server.getCommandSource();
try
{
((CarpetScriptHost) value).callUDF(BlockPos.ORIGIN, source, userClosing, Collections.emptyList());
}
catch (InvalidCallbackException ignored)
{
}
}
});
String markerName = ExpressionInspector.MARKER_STRING+"_"+((getName()==null)?"":getName());
for (ServerWorld world : CarpetServer.minecraft_server.getWorlds())
{
@@ -225,6 +255,7 @@ public class CarpetScriptHost extends ScriptHost
e.remove();
}
}
}
private void dumpState()
@@ -239,9 +270,9 @@ public class CarpetScriptHost extends ScriptHost
public Tag getGlobalState(String file)
{
if (getName() == null || myCode.isInternal()) return null;
if (getName() == null ) return null;
if (file != null)
myCode.getData(file);
return myCode.getData(file);
if (parent == null)
return globalState;
return ((CarpetScriptHost)parent).globalState;
@@ -249,7 +280,7 @@ public class CarpetScriptHost extends ScriptHost
public void setGlobalState(Tag tag, String file)
{
if (getName() == null || myCode.isInternal()) return;
if (getName() == null ) return;
if (file!= null)
{
@@ -38,12 +38,12 @@ public class CarpetScriptServer
//make static for now, but will change that later:
public CarpetScriptHost globalHost;
public Map<String, CarpetScriptHost> modules;
long tickStart;
public long tickStart;
public boolean stopAll;
Set<String> holyMoly;
private Set<String> holyMoly;
public CarpetEventServer events;
public static List<ModuleInterface> bundledModuleData = new ArrayList<ModuleInterface>(){{
private static List<ModuleInterface> bundledModuleData = new ArrayList<ModuleInterface>(){{
add(new BundledModule("camera"));
add(new BundledModule("event_test"));
}};
@@ -72,7 +72,7 @@ public class CarpetScriptServer
}
ModuleInterface getModule(String name)
private ModuleInterface getModule(String name)
{
File folder = CarpetServer.minecraft_server.getLevelStorage().resolveFile(
CarpetServer.minecraft_server.getLevelName(), "scripts");
@@ -218,25 +218,6 @@ public class CarpetScriptServer
}
return false;
}
private void addEvents(ServerCommandSource source, String hostName)
{
if (1+2 == 3) throw new RuntimeException("This should run when code is evaluated");
ScriptHost host = modules.get(hostName);
if (host == null)
{
return;
}
for (String fun : host.globalFunctions.keySet())
{
if (!fun.startsWith("__on_"))
continue;
String event = fun.replaceFirst("__on_","");
if (!CarpetEventServer.Event.byName.containsKey(event))
continue;
events.addEvent(event, hostName, fun);
}
}
private void addCommand(ServerCommandSource source, String hostName)
{
@@ -261,7 +242,9 @@ public class CarpetScriptServer
requires((player) -> modules.containsKey(hostName)).
executes( (c) ->
{
Messenger.m(c.getSource(), "gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call(c.getSource(),"__command", null, ""));
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(),"__command", null, "");
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
return 1;
});
@@ -271,28 +254,16 @@ public class CarpetScriptServer
then(literal(function).
requires((player) -> modules.containsKey(hostName) && modules.get(hostName).getPublicFunctions().contains(function)).
executes( (c) -> {
Messenger.m(
c.getSource(),
"gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call(
c.getSource(),
function,
null,
""
)
);
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(), function,null,"");
if (!response.isEmpty()) Messenger.m(c.getSource(),"gi "+response);
return 1;
}).
then(argument("args...", StringArgumentType.greedyString()).
executes( (c) -> {
Messenger.m(
c.getSource(),
"gi "+modules.get(hostName).retrieveForExecution(c.getSource()).call(
c.getSource(),
function,
null,
StringArgumentType.getString(c, "args...")
)
);
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(), function,null, StringArgumentType.getString(c, "args..."));
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
return 1;
})));
}
+10 -10
View File
@@ -6,16 +6,16 @@ import java.util.Set;
public class Context
{
static final int NONE = 0;
static final int VOID = 1;
static final int BOOLEAN = 2;
static final int NUMBER = 3;
static final int STRING = 4;
static final int LIST = 5;
static final int ITERATOR = 6;
static final int SIGNATURE = 7;
static final int LOCALIZATION = 8;
static final int LVALUE = 9;
public static final int NONE = 0;
public static final int VOID = 1;
public static final int BOOLEAN = 2;
public static final int NUMBER = 3;
public static final int STRING = 4;
public static final int LIST = 5;
public static final int ITERATOR = 6;
public static final int SIGNATURE = 7;
public static final int LOCALIZATION = 8;
public static final int LVALUE = 9;
public Map<String, LazyValue> variables = new HashMap<>();
+68 -8
View File
@@ -11,7 +11,13 @@ import carpet.script.Fluff.QuadFunction;
import carpet.script.Fluff.QuinnFunction;
import carpet.script.Fluff.SexFunction;
import carpet.script.Fluff.TriFunction;
import carpet.script.exception.*;
import carpet.script.exception.BreakStatement;
import carpet.script.exception.ContinueStatement;
import carpet.script.exception.ExitStatement;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ReturnStatement;
import carpet.script.exception.ThrowStatement;
import carpet.script.value.AbstractListValue;
import carpet.script.value.ContainerValueInterface;
import carpet.script.value.FunctionSignatureValue;
@@ -23,6 +29,7 @@ import carpet.script.value.ListValue;
import carpet.script.value.MapValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.ThreadValue;
import carpet.script.value.Value;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.apache.commons.lang3.text.WordUtils;
@@ -1191,16 +1198,14 @@ public class Expression implements Cloneable
{
Value v1 = lv1.evalValue(c, Context.BOOLEAN);
if (!v1.getBoolean()) return (cc, tt) -> v1;
Value v2 = lv2.evalValue(c, Context.BOOLEAN);
return v2.getBoolean() ? ((cc, tt) -> v2) : LazyValue.FALSE;
return lv2;
});
addLazyBinaryOperator("||", precedence.get("or||"), false, (c, t, lv1, lv2) ->
{
Value v1 = lv1.evalValue(c, Context.BOOLEAN);
if (v1.getBoolean()) return (cc, tt) -> v1;
Value v2 = lv2.evalValue(c, Context.BOOLEAN);
return v2.getBoolean() ? ((cc, tt) -> v2) : LazyValue.FALSE;
return lv2;
});
addBinaryOperator("~", precedence.get("attribute~:"), true, Value::in);
@@ -1921,7 +1926,7 @@ public class Expression implements Cloneable
LazyValue expr = lv.get(1);
//scoping
LazyValue _val = c.getVariable("_");
LazyValue _iter = c.getVariable("_i");
LazyValue _ite = c.getVariable("_i");
int successCount = 0;
for (int i=0; iterator.hasNext(); i++)
{
@@ -1952,7 +1957,7 @@ public class Expression implements Cloneable
//revering scope
((AbstractListValue) rval).fatality();
c.setVariable("_", _val);
c.setVariable("_i", _iter);
c.setVariable("_i", _ite);
long promiseWontChange = successCount;
return (cc, tt) -> new NumericValue(promiseWontChange);
});
@@ -1981,15 +1986,18 @@ public class Expression implements Cloneable
//scoping
LazyValue _val = c.getVariable("_");
LazyValue _acc = c.getVariable("_a");
LazyValue _ite = c.getVariable("_i");
while (iterator.hasNext())
for (int i=0; iterator.hasNext(); i++)
{
Value next = iterator.next();
String var = next.boundVariable;
next.bindTo("_");
Value promiseWontChangeYou = acc;
int seriously = i;
c.setVariable("_a", (cc, tt) -> promiseWontChangeYou.bindTo("_a"));
c.setVariable("_", (cc, tt) -> next);
c.setVariable("_i", (cc, tt) -> new NumericValue(seriously).bindTo("_i"));
try
{
acc = expr.evalValue(c, t);
@@ -2009,6 +2017,7 @@ public class Expression implements Cloneable
((AbstractListValue) rval).fatality();
c.setVariable("_a", _acc);
c.setVariable("_", _val);
c.setVariable("_i", _ite);
Value hopeItsEnoughPromise = acc;
return (cc, tt) -> hopeItsEnoughPromise;
@@ -2992,6 +3001,57 @@ public class Expression implements Cloneable
return (cc, tt) -> retval;
});
addLazyFunctionWithDelegation("task", -1, (c, t, expr, tok, lv) -> {
if (lv.size() == 0)
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
Value funcDesc = lv.get(0).evalValue(c);
if (!(funcDesc instanceof FunctionValue))
{
String name = funcDesc.getString();
funcDesc = c.host.globalFunctions.get(name);
if (funcDesc == null)
throw new InternalExpressionException("Function "+name+" is not defined yet");
}
FunctionValue fun = (FunctionValue)funcDesc;
int extraargs = lv.size() - fun.getArguments().size();
if (extraargs != 1 && extraargs != 2)
{
throw new InternalExpressionException("Function takes "+fun.getArguments().size()+" arguments.");
}
List<LazyValue> lvargs = new ArrayList<>();
for (int i=0; i< fun.getArguments().size(); i++)
{
lvargs.add(lv.get(i+1));
}
Value queue = Value.NULL;
if (extraargs == 2) queue = lv.get(lv.size()-1).evalValue(c);
ThreadValue thread = new ThreadValue(queue, fun, expr, tok, c, lvargs);
Thread.yield();
return (cc, tt) -> thread;
});
addFunction("task_count", (lv) ->
{
if (lv.size() > 0)
{
return new NumericValue(ThreadValue.taskCount(lv.get(0)));
}
return new NumericValue(ThreadValue.taskCount());
});
addUnaryFunction("join_task", (v) -> {
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'join_task' could only be used with a task value");
return ((ThreadValue) v).join();
});
addUnaryFunction("task_completed", (v) -> {
if (!(v instanceof ThreadValue))
throw new InternalExpressionException("'task_completed' could only be used with a task value");
return new NumericValue(((ThreadValue) v).isFinished());
});
}
static Expression none = new Expression("null");
+1 -1
View File
@@ -11,7 +11,7 @@ import java.util.stream.Collectors;
public class ScriptHost
{
private final Map<String, ScriptHost> userHosts = new Object2ObjectOpenHashMap<>();
protected final Map<String, ScriptHost> userHosts = new Object2ObjectOpenHashMap<>();
public Map<String, FunctionValue> globalFunctions = new Object2ObjectOpenHashMap<>();
public Map<String, LazyValue> globalVariables = new Object2ObjectOpenHashMap<>();
@@ -56,7 +56,7 @@ public class FileModule implements ModuleInterface
}
//copied private method from net.minecraft.nbt.NbtIo.read()
private static Tag read(File file)
public static Tag read(File file)
{
try (DataInputStream dataInput_1 = new DataInputStream(new FileInputStream(file)))
{
@@ -80,7 +80,7 @@ public class FileModule implements ModuleInterface
}
//copied private method from net.minecraft.nbt.NbtIo.write() and client method safe_write
private static void write(Tag tag_1, File file)
public static void write(Tag tag_1, File file)
{
File file_2 = new File(file.getAbsolutePath() + "_tmp");
if (file_2.exists()) file_2.delete();
@@ -101,23 +101,4 @@ public class FileModule implements ModuleInterface
if (file.exists()) file.delete();
if (!file.exists()) file_2.renameTo(file);
}
@Override
public Tag getData(String file)
{
File dataFile =CarpetServer.minecraft_server.getLevelStorage().resolveFile(
CarpetServer.minecraft_server.getLevelName(), "scripts/"+name+".data"+(file==null?"":"."+file)+".nbt");
if (!(dataFile.isFile()))
return null;
return read(dataFile);
}
@Override
public void saveData(String file, Tag globalState)
{
File dataFile =CarpetServer.minecraft_server.getLevelStorage().resolveFile(
CarpetServer.minecraft_server.getLevelName(), "scripts/"+name+".data"+(file==null?"":"."+file)+".nbt");
write(globalState, dataFile);
}
}
@@ -1,12 +1,44 @@
package carpet.script.bundled;
import carpet.CarpetServer;
import net.minecraft.nbt.Tag;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import static carpet.script.bundled.FileModule.read;
import static carpet.script.bundled.FileModule.write;
public interface ModuleInterface
{
String getName();
String getCode();
default boolean isInternal() {return true; }
default Tag getData(String file) {return null; }
default void saveData(String file, Tag globalState) {}
default Tag getData(String file)
{
File dataFile = CarpetServer.minecraft_server.getLevelStorage().resolveFile(
CarpetServer.minecraft_server.getLevelName(), "scripts/"+getName()+".data"+(file==null?"":"."+file)+".nbt");
if (!Files.exists(dataFile.toPath()) || !(dataFile.isFile())) return null;
return read(dataFile);
}
default void saveData(String file, Tag globalState)
{
File dataFile =CarpetServer.minecraft_server.getLevelStorage().resolveFile(
CarpetServer.minecraft_server.getLevelName(), "scripts/"+getName()+".data"+(file==null?"":"."+file)+".nbt");
if (!Files.exists(dataFile.toPath().getParent()))
{
try
{
Files.createDirectory(dataFile.toPath().getParent());
}
catch (IOException ignored)
{
}
}
write(globalState, dataFile);
}
}
@@ -21,6 +21,7 @@ import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import net.minecraft.world.chunk.WorldChunk;
import java.util.Arrays;
import java.util.HashMap;
@@ -129,15 +130,24 @@ public class BlockValue extends Value
public static LocatorResult fromParams(CarpetContext c, List<LazyValue> params, int offset)
{
return fromParams(c, params,offset, false);
return fromParams(c, params,offset, false, false);
}
public static LocatorResult fromParams(CarpetContext c, List<LazyValue> params, int offset, boolean acceptString)
{
return fromParams(c, params,offset, acceptString, false);
}
public static LocatorResult fromParams(CarpetContext c, List<LazyValue> params, int offset, boolean acceptString, boolean optional)
{
try
{
Value v1 = params.get(0 + offset).evalValue(c);
//add conditional from string name
if (optional && v1 instanceof NullValue)
{
return new LocatorResult(null, 1+offset);
}
if (acceptString && v1 instanceof StringValue)
{
return new LocatorResult(fromString(v1.getString()), 1+offset);
@@ -192,6 +202,15 @@ public class BlockValue extends Value
throw new InternalExpressionException("Attempted to fetch block state without world or stored block state");
}
public static BlockEntity getBlockEntity(ServerWorld world, BlockPos pos)
{
if (world.getServer().isOnThread())
return world.getBlockEntity(pos);
else
return world.getWorldChunk(pos).getBlockEntity(pos, WorldChunk.CreationType.IMMEDIATE);
}
public CompoundTag getData()
{
if (data != null)
@@ -202,7 +221,7 @@ public class BlockValue extends Value
}
if (world != null && pos != null)
{
BlockEntity be = world.getBlockEntity(pos);
BlockEntity be = getBlockEntity(world, pos);
CompoundTag tag = new CompoundTag();
if (be == null)
{
@@ -223,7 +223,7 @@ public class EntityValue extends Value
put("dimension", (e, a) -> new StringValue(nameFromRegistryId(Registry.DIMENSION.getId(e.dimension))));
put("height", (e, a) -> new NumericValue(e.getDimensions(EntityPose.STANDING).height));
put("width", (e, a) -> new NumericValue(e.getDimensions(EntityPose.STANDING).width));
put("eye_height", (e, a) -> new NumericValue(e.getEyeHeight(EntityPose.STANDING)));
put("eye_height", (e, a) -> new NumericValue(e.getStandingEyeHeight()));
put("age", (e, a) -> new NumericValue(e.age));
put("despawn_timer", (e, a) -> e instanceof LivingEntity?new NumericValue(((LivingEntity) e).getDespawnCounter()):Value.NULL);
put("item", (e, a) -> (e instanceof ItemEntity)?ListValue.fromItemStack(((ItemEntity) e).getStack()):Value.NULL);
@@ -273,6 +273,22 @@ public class EntityValue extends Value
}
return Value.NULL;
});
put("permission_level", (e, a) -> {
if (e instanceof ServerPlayerEntity)
{
ServerPlayerEntity spe = (ServerPlayerEntity) e;
for (int i=4; i>=0; i--)
{
if (spe.allowsPermissionLevel(i))
return new NumericValue(i);
}
return new NumericValue(0);
}
return Value.NULL;
});
//spectating_entity
// isGlowing
put("effect", (e, a) ->
@@ -756,6 +772,39 @@ public class EntityValue extends Value
else
e.noClip = v.getBoolean();
});
put("effect", (e, v) ->
{
if (!(e instanceof LivingEntity)) return;
LivingEntity le = (LivingEntity)e;
if (v == null)
le.clearPotionEffects();
else if (v instanceof ListValue)
{
List<Value> lv = ((ListValue) v).getItems();
if (lv.size() >= 2 && lv.size() <= 5)
{
String effectName = lv.get(0).getString();
if (effectName.startsWith("minecraft:"))
effectName = "minecraft:"+effectName;
StatusEffect effect = Registry.STATUS_EFFECT.get(new Identifier(effectName));
if (effect == null)
throw new InternalExpressionException("Wrong effect name: "+effectName);
int duration = (int)NumericValue.asNumber(lv.get(1)).getLong();
int amplifier = 0;
if (lv.size() > 2)
amplifier = (int)NumericValue.asNumber(lv.get(2)).getLong();
boolean showParticles = true;
if (lv.size() > 3)
showParticles = lv.get(3).getBoolean();
boolean showIcon = true;
if (lv.size() > 4)
showIcon = lv.get(4).getBoolean();
le.addPotionEffect(new StatusEffectInstance(effect, duration, amplifier, showParticles, showIcon));
return;
}
}
throw new InternalExpressionException("'effect' needs either no arguments (clear) or effect name, duration, and optional amplifier, show particles and show icon");
});
// gamemode
// spectate
@@ -246,7 +246,7 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
return items;
}
public Iterator<Value> iterator() { return new ArrayList<>(items).iterator(); }
public Iterator<Value> iterator() { return new ArrayList<>(items).iterator(); } // should be thread safe
public void extend(List<Value> subList)
{
@@ -5,6 +5,12 @@ import carpet.script.LazyValue;
import carpet.script.exception.InternalExpressionException;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.InventoryProvider;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.ChestBlockEntity;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.command.arguments.ItemStackArgument;
import net.minecraft.command.arguments.ItemStringReader;
@@ -21,8 +27,12 @@ import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringNbtReader;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;
import java.util.Collection;
import java.util.HashMap;
@@ -116,6 +126,41 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
return copy;
}
// stolen from HopperBlockEntity, adjusted for threaded operation
public static Inventory getInventoryAt(ServerWorld world, BlockPos blockPos)
{
Inventory inventory = null;
BlockState blockState = world.getBlockState(blockPos);
Block block = blockState.getBlock();
if (block instanceof InventoryProvider) {
inventory = ((InventoryProvider)block).getInventory(blockState, world, blockPos);
} else if (block.hasBlockEntity()) {
BlockEntity blockEntity = BlockValue.getBlockEntity(world, blockPos);
if (blockEntity instanceof Inventory) {
inventory = (Inventory)blockEntity;
if (inventory instanceof ChestBlockEntity && block instanceof ChestBlock) {
inventory = ChestBlock.getInventory(blockState, world, blockPos, true);
}
}
}
if (inventory == null) {
List<Entity> list = world.getEntities(
(Entity)null,
new Box(
blockPos.getX() - 0.5D, blockPos.getY() - 0.5D, blockPos.getZ() - 0.5D,
blockPos.getX() + 0.5D, blockPos.getY() + 0.5D, blockPos.getZ() + 0.5D),
EntityPredicates.VALID_INVENTORIES
);
if (!list.isEmpty()) {
inventory = (Inventory)list.get(world.random.nextInt(list.size()));
}
}
return inventory;
}
public static InventoryLocator locateInventory(CarpetContext c, List<LazyValue> params, int offset)
{
try
@@ -139,7 +184,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
BlockPos pos = ((BlockValue) v1).getPos();
if (pos == null)
throw new InternalExpressionException("Block to access inventory needs to be positioned in the world");
inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos);
inv = getInventoryAt(c.s.getWorld(), pos);
if (inv == null)
return null;
return new InventoryLocator(pos, pos, inv, offset + 1);
@@ -151,7 +196,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
NumericValue.asNumber(args.get(0)).getDouble(),
NumericValue.asNumber(args.get(1)).getDouble(),
NumericValue.asNumber(args.get(2)).getDouble());
inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos);
inv = getInventoryAt(c.s.getWorld(), pos);
if (inv == null)
return null;
return new InventoryLocator(pos, pos, inv, offset + 1);
@@ -160,7 +205,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
NumericValue.asNumber(v1).getDouble(),
NumericValue.asNumber(params.get(1 + offset).evalValue(c)).getDouble(),
NumericValue.asNumber(params.get(2 + offset).evalValue(c)).getDouble());
inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos);
inv = getInventoryAt(c.s.getWorld(), pos);
if (inv == null)
return null;
return new InventoryLocator(pos, pos, inv, offset + 3);
@@ -1,29 +1,34 @@
package carpet.script.value;
import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.LazyValue;
import carpet.script.Tokenizer;
import carpet.script.exception.InternalExpressionException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadValue extends Value
{
CompletableFuture<Value> taskFuture;
private CompletableFuture<Value> taskFuture;
private long id;
private static long sequence = 0L;
private static final Map<Value,ThreadPoolExecutor> executorServices = new HashMap<>();
private static final ExecutorService executorService = Executors.newCachedThreadPool();
public ThreadValue(LazyValue expression, Context ctx)
public ThreadValue(Value pool, FunctionValue function, Expression expr, Tokenizer.Token token, Context ctx, List<LazyValue> args)
{
id = sequence++;
taskFuture = CompletableFuture.supplyAsync(
() -> expression.evalValue(ctx),
executorService
() -> function.lazyEval(ctx, Context.NONE, expr, token, args).evalValue(ctx),
executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool())
);
Thread.yield();
}
@Override
@@ -55,11 +60,6 @@ public class ThreadValue extends Value
return taskFuture.isDone();
}
public void stop()
{
taskFuture.complete(Value.NULL);
}
@Override
public boolean equals(Object o)
{
@@ -72,7 +72,27 @@ public class ThreadValue extends Value
public int compareTo(Value o)
{
if (!(o instanceof ThreadValue))
throw new InternalExpressionException("Cannot compare ");
throw new InternalExpressionException("Cannot compare tasks to other types");
return (int) (this.id - ((ThreadValue) o).id);
}
@Override
public int hashCode()
{
return Long.hashCode(id);
}
public static int taskCount()
{
return executorServices.values().stream().map(ThreadPoolExecutor::getActiveCount).reduce(0, Integer::sum);
}
public static int taskCount(Value pool)
{
if (executorServices.containsKey(pool))
{
return executorServices.get(pool).getActiveCount();
}
return 0;
}
}
+1 -1
View File
@@ -110,7 +110,7 @@ public abstract class Value implements Comparable<Value>, Cloneable
@Override
public int compareTo(final Value o)
{
if (o instanceof NumericValue || o instanceof ListValue)
if (o instanceof NumericValue || o instanceof ListValue || o instanceof ThreadValue)
{
return -o.compareTo(this);
}
+522 -147
View File
@@ -1,66 +1,357 @@
__command() ->
(
print('camera_path module.');
print('camera: scarpet app.');
print('-------------------');
print(' "/camera_path start" - set the starting point');
print(' "/camera start" - set the starting point, resets the path');
print('');
print(' "/camera_path add <N>" - add another point <N> frames later');
print(' "/camera add <N>" - add point to the end <N> seconds later');
print(' "/camera prepend <N>" - prepend point <N> seconds before');
print(' "/camera clear" - removes entire path');
print(' "/camera select" - or punch a point, to select it');
print(' "/camera move" - moves selected point toplayer location');
print(' "/camera duration <X>" - resizes current selected path');
print(' "/camera split_point>" - splits current selected path in half');
print(' "/camera delete_point" - removes current keypoint');
print(' "/camera trim_path" - removes all keypoints from selected');
print('');
print(' "/camera_path select_interpolation < linear | gauss_? >"');
print(' Select interpolation between points:');
print(' "/camera store <name>"');
print(' "/camera load <name>"');
print(' - stores and loads paths from files in /scripts folder');
print('');
print(' "/camera interpolation < linear | gauss | cr | gauss_<num> >"');
print(' Selects interpolation between points:');
print(' - cr: Catmull-Rom interpolation (default).');
print(' smooth path that goes through all points.');
print(' - linear: straight paths between points.');
print(' - gauss_auto: automatic smooth transitions.');
print(' - gauss_<number>: custom fixed variance ');
print(' (in points) for special effects.');
print(' - gauss: automatic smooth transitions.');
print(' - gauss_<N>: custom fixed variance ');
print(' (in seconds) for special effects.');
print(' use "_" for decimal separator, e.g. gauss_2_5');
print(' gauss makes the smoothest path,');
print(' but treating points as suggestions only');
print('');
print(' "/camera_path repeat <N> <last_delay>" - ');
print(' "/camera repeat <N> <last_delay>" - ');
print(' Repeat existing points configuration n-times');
print(' using <last_delay> points to link path ends');
print(' using <last_delay> seconds to link path ends');
print('');
print(' "/camera_path speed <factor> - ');
print(' "/camera stretch <factor>" - ');
print(' Change number of frames between points');
print(' from 25 -> 4 times faster (less points),');
print(' to 400 -> 4 times slower (more points)');
print(' from 25 -> 4x faster, to 400 -> 4x slower,');
print('');
print(' "/camera_path play <fps>: run a path with a player');
print(' <fps> needs to be multiples of 20, (20 tps)');
print(' "/camera play : run a path with a player');
print(' use "sneak" to stop it prematurely');
print(' run /camera hide and F1 to clear the view');
print('');
print(' "/camera_path show": shows current path for a moment');
print(' "/camera show": shows current path');
print(' color of particles used is different for different players');
print(' "/camera hide": hides path display');
print('');
print(' "/script in camera_path invoke _show_path_tick <particle> <ppt>":');
print(' place this in a repeating commandblock');
print(' to display path continuously');
print(' with set particle and number of particles per tick');
print(' example: \'dust 0.9 0.1 0.1 1\' 100 ');
''
);
start() ->
global_points = null;
global_dimension = null;
global_player = null;
global_player_eye_offset = map(range(5), 0);
global_showing_path = false;
global_playing_path = false;
global_needs_updating = false;
global_selected_point = null;
global_markers = null;
global_particle_density = 100;
global_color_a = null;
global_color_b = null;
global_path_precalculated = null;
// starts the path with current player location
start() -> __start_with( _() -> l(l(__camera_position(), 0,'sharp')) );
// start path with customized initial points selection
__start_with(points_supplier) ->
(
p = player();
global_points = l(l(p ~ 'location', 0,'sharp'));
undef('global_path_precalculated');
str('Started path at %.1f %.1f %.1f', p~'x', p~'y', p~'z')
p = player();
global_player = str(p);
global_player_eye_offset = l(0, p~'eye_height',0,0,0);
global_dimension = p~'dimension';
code = abs(hash_code(str(p))-123456);
global_color_a = str('dust %.1f %.1f %.1f 1',(code%10)/10,(((code/10)%10)/10), (((code/100)%10)/10) ); // 'dust 0.1 0.9 0.1 1',
global_color_b = str('dust %.1f %.1f %.1f 1',(((code/1000)%10)/10),(((code/10000)%10)/10), (((code/100000)%10)/10) );// 'dust 0.6 0.6 0.6 1'
global_points = call(points_supplier);
__update();
show();
str('Started path at %.1f %.1f %.1f', p~'x', p~'y', p~'z');
);
// gets current player controlling the path, or fails
__get_player() ->
(
if (!global_player, exit('No player selected'));
p = player(global_player);
if (p == null, exit('Player logged out'));
p;
);
// clears current path
clear() ->
(
global_points = l();
global_dimension = null;
global_player = null;
global_showing_path = false;
global_playing_path = false;
__update();
'Path cleared';
);
// camera position for the player sticks out of their eyes
__camera_position() -> (__get_player()~'location' + global_player_eye_offset);
// path changed, now notify all processes that they need to update;
__update() ->
(
global_path_precalculated = null;
global_playing_path = false;
global_needs_updating = true;
);
// ensures that the current execution context can modify the path
__assert_can_modify_path() ->
(
if (!global_points, exit('Path is not setup for current player'));
if (!global_showing_path, exit('Turn on path showing to edit key points'));
if(!global_player || !global_dimension || __get_player()~'dimension' != global_dimension,
exit('Player not in dimension'));
);
// make sure selected point is correct
__assert_point_selected(validator) ->
(
__assert_can_modify_path();
if (!call(validator, global_selected_point), exit('No appropriate point selected'));
);
//select a custom interpolation method
interpolation(method) -> __interpolation(method, true);
__interpolation(method, verbose) ->
(
__prepare_path_if_needed() -> __prepare_path_if_needed_generic();
// each supported method needs to specify its __find_position_for_point to trace the path
// accepting segment number, and position in the segment
// or optionally __prepare_path_if_needed, if path is inefficient to compute point by point
global_interpolator = if (
method == 'linear', '__interpolator_linear',
method == 'cr', '__interpolator_cr',
method == 'gauss', _(s, p) -> __interpolator_gauB(s, p, 0),
method ~ '^gauss_',
(
type = method - 'gauss_';
type = replace(type,'_','.');
variance = round(60*number(type));
_(s, p, outer(variance)) -> __interpolator_gauB(s, p, variance);
),
exit('Choose one of the following methods: linear, gauss, gauss_<deviation>, cr')
);
__update();
if(verbose, 'Interpolation changed to '+method, '');
);
__interpolation('cr', false);
// adds a point to the end of the path with delay in seconds
add(delay) ->
(
p = player();
mode = 'sharp';
__assert_can_modify_path();
//mode is currently unused, run_path does always sharp, gauss interpolator is always smooth
// but this option could be used could be used at some point by more robust interpolators
__add_path_segment(p ~ 'location', delay, mode);
str('Added point %d: %.1f %.1f %.1f', length(global_points), p~'x', p~'y', p~'z')
__add_path_segment(__camera_position(), round(60*delay), 'smooth', true);
__update();
__get_path_size_string();
);
__add_path_segment(vector, duration, mode) ->
// prepends the path with a new starting point, with a segment of specified delay
prepend(delay) ->
(
undef('global_path_precalculated');
if ( (l('sharp','smooth') ~ mode) == null, exit('use smooth or sharp point'));
if(!global_points, exit('Cannot add point to path that didn\'t started yet!'));
l(v, end_time, m) = global_points:(-1);
vector:(-2) = __adjusted_rot(v:(-2) , vector:(-2) );
global_points += l(vector, end_time+duration, mode)
__assert_can_modify_path();
__add_path_segment(__camera_position(), round(60*delay), 'smooth', false);
__update();
__get_path_size_string();
);
// adjusts current rotation so we don\'t spin around like crazy
// repeats existing points seveal times, using last section delay (seconds) to join points
repeat(times, last_section_delay) ->
(
if (err = __is_not_valid_for_motion(), exit(err));
positions = map(global_points, _:0);
modes = map(global_points, _:(-1));
durations = map(global_points, global_points:(_i+1):1 - _:1 );
durations:(-1) = round(60*last_section_duration);
loop(times,
loop( length(positions),
__add_path_segment(positions:_, durations:_, modes:_, true)
)
);
__update();
__get_path_size_string();
);
//stretches or shrinks current path to X percent of what it was before
stretch(percentage) ->
(
if (err = __is_not_valid_for_motion(), exit(err));
if (percentage < 25 || percentage > 400,
exit('path speed can only be speed, or slowed down 4 times. Re-call command for larger changes')
);
ratio = percentage/100;
previous_path_length = global_points:(-1):1;
for(global_points, _:1 = _:1*ratio );
__update();
str('path %s from %.2f to %.2f seconds',
if(ratio<1,'shortened','extended'),
previous_path_length/60,
global_points:(-1):1/60
)
);
// moves current selected point to player location
move() ->
(
__assert_point_selected(_(p) -> p != null);
global_points:global_selected_point:0 = __camera_position();
__update();
'';
);
// chenges duration of the current selected segment to X seconds
duration(amount) ->
(
__assert_point_selected(_(p) -> p); // skips nulls and 0 - starting point
duration = number(amount);
new_ticks = round(duration * 60);
if (new_ticks < 10, return());
previous_ticks = global_points:global_selected_point:1-global_points:(global_selected_point-1):1;
delta = new_ticks - previous_ticks;
// adjust duration of all points after that.
for (range(global_selected_point, length(global_points)),
global_points:_:1 += delta;
);
__update();
__get_path_size_string();
);
// deletes current keypoint without changing the path length
delete_point() ->
(
__assert_point_selected(_(p) -> p != null);
if (length(global_points) < 2, clear(); return());
if (global_selected_point == 0, global_points:1:1 = 0);
global_points = filter(global_points, _i != global_selected_point);
if (global_selected_point >= length(global_points), global_selected_point = null);
__update();
__get_path_size_string();
);
// splits current selected segment in half by adding a keypoint in between
split_point() ->
(
__assert_point_selected(_(p) -> p); // skips nulls and 0 - starting point
current_time = global_points:global_selected_point:1;
previous_time = global_points:(global_selected_point-1):1;
segment_duration = current_time-previous_time;
put(
global_points,
global_selected_point,
l(
__get_path_at(global_selected_point-1, previous_time, segment_duration/2),
previous_time+segment_duration/2,
global_points:global_selected_point:2
),
'insert'
);
__update();
__get_path_size_string();
);
// removes all points in the path from the current point
trim_path() ->
(
__assert_point_selected(_(p) -> p != null);
global_points = slice(global_points, 0, global_selected_point);
global_selected_point = null;
__update();
__get_path_size_string();
);
// moves entire camera path keeping the angles to player position being in the starting point
reposition() ->
(
__assert_can_modify_path();
shift = pos(__get_player())-slice(global_points:0:0, 0, 3);
shift += 0; shift += 0;
shift = shift + global_player_eye_offset;
for(global_points, _:0 = _:0 + shift);
__update();
__get_path_size_string();
);
// selects either a point of certain number (starting from 1), or closest point
select(num) ->
(
if (!global_points, return());
if (!global_showing_path, return());
p = __get_player();
num = (num+1000*(length(global_points)+1)) % (length(global_points)+1);
selected_point = if (num, num-1,
__closest_point_to_center(
p~'pos'+l(0,p~'eye_height',0),
map(global_points, slice(_:0, 0, 3))
)
);
global_selected_point = if (global_selected_point == selected_point, null, selected_point);
global_needs_updating = true;
// no need to _update since path is still valid
);
// player can also punch the mannequin to select/deselect it
__on_player_attacks_entity(p, e) ->
(
if (e~'type' == 'armor_stand' && query(e, 'has_tag', '__scarpet_marker_camera') && global_markers,
for (global_markers,
if(_==e,
global_selected_point = if (global_selected_point == _i, null, _i);
global_needs_updating = true;
return();
)
);
);
);
// adds new segment to the path
__add_path_segment(vector, duration, mode, append) ->
(
if ( (l('sharp','smooth') ~ mode) == null, exit('use smooth or sharp point'));
l(v, segment_time, m) = global_points:(if(append, -1, 0));
vector:(-2) = __adjusted_rot(v:(-2), vector:(-2));
if (append,
global_points += l(vector, segment_time+duration, mode);
,
new_points = l(l(vector, 0, mode));
for (global_points,
_:1 += duration;
new_points += _;
);
global_points = new_points;
);
null;
);
// adjusts current rotation so we don't spin around like crazy
__adjusted_rot(previous_rot, current_rot) ->
(
while( abs(previous_rot-current_rot) > 180, 1000,
@@ -69,132 +360,168 @@ __adjusted_rot(previous_rot, current_rot) ->
current_rot
);
// returns current path size blurb
__get_path_size_string() ->
(
if (!__is_not_valid_for_motion(),
str('%d points, %.1f secs', length(global_points), global_points:(-1):1/60);
,
'Path too small to run';
);
);
repeat(times, last_section_duration) ->
// checks if the current path is valid for motion
__is_not_valid_for_motion() ->
(
undef('global_path_precalculated');
positions = map(global_points, _:0);
modes = map(global_points, _:(-1));
durations = map(global_points, global_points:(_i+1):1 - _:1 );
durations:(-1) = last_section_duration;
loop(times,
loop( length(positions),
__add_path_segment(positions:_, durations:_, modes:_)
)
);
str('Add %d points %d times', length(positions), times)
if(!global_points, return('Path not defined yet'));
if(length(global_points)<2, return('Path not complete - add more points'));
if(!global_dimension || __get_player()~'dimension' != global_dimension, return('Wrong dimension'));
false
);
speed(percentage) ->
(
undef('global_path_precalculated');
if (percentage < 25 || percentage > 400,
exit('path speed can only be speed, or slowed down 4 times. Recall command for larger changes')
);
ratio = percentage/100;
previous_path_length = global_points:(-1):1;
for(global_points, _:1 = _:1*ratio );
undef('global_path_precalculated');
str('path %s from %d to %d ticks',
if(ratio<1,'shortened','extended'),
previous_path_length,
global_points:(-1):1
)
);
select_interpolation(method) ->
(
undef('global_path_precalculated');
__prepare_path_if_needed() -> __prepare_path_if_needed_generic();
// each supported method needs to specify its __find_position_for_point to trace the path
// accepting segment number, and position in the segment
// or optionally __prepare_path_if_needed, if path is inefficient to compute point by point
if (
method == 'linear',
(
__find_position_for_point(s, p) -> __find_position_for_linear(s, p)
),
method ~ '^gauss_',
(
type = method - 'gauss_';
global_interpol_option = if(type=='auto',0,number(type));
__find_position_for_point(s, p) -> __find_position_for_gauss(s, p)
),
exit('Choose one of the following methods: linear, gauss:auto, gauss:<deviation>')
);
'Ok'
);
select_interpolation('gauss_auto');
__assert_valid_for_motion() ->
(
if(!global_points, exit('Path not defined yet'));
if(length(global_points)<2, exit('Path not complete - add more points'));
null
);
// grabs position of a player for a given segment, which segment starts at start point, and offset by index points
__get_path_at(segment, start, index) ->
(
v = global_path_precalculated:(start+index);
if(v == null,
v = __find_position_for_point(segment, index);
v = call(global_interpolator, segment, index);
global_path_precalculated:(start+index) = v
);
v
);
__invalidate_points_cache() -> global_path_precalculated = map(range(global_points:(-1):1), null);
// squared euclidean distance between two points
__distsq(vec1, vec2) -> reduce(vec1 - vec2, _a + _*_, 0);
//finds index of the closest point from a list to the center point
__closest_point_to_center(center, points) ->
(
reduce(points,
d = __distsq(_, center); if( d<dd, dd=d; _i, _a),
dd = __distsq(points:0, center); 0
);
);
// displays path particles and key point markers on its own thread
show() ->
(
loop(100,
_show_path_tick('dust 0.9 0.1 0.1 1', 100);
game_tick(50)
__get_path_size_string();
if (global_showing_path, return ());
global_showing_path = true;
global_needs_updating= false;
__create_markers() ->
(
map(global_points || l(),
is_selected = global_selected_point != null && _i == global_selected_point;
caption = if (_i == 0, '1: Start', str('%d: %.1fs', _i+1, global_points:_i:1/60); );
if (is_selected && _i > 0,
caption += str(' (%.1fs current segment)', (global_points:_i:1 - global_points:(_i-1):1)/60);
);
m = create_marker(caption, _:0, 'observer');
if (is_selected,
modify(m,'effect','glowing',72000, 0, false, false);
);
m
);
);
'Done!'
__show_path_tick() ->
(
if (__is_not_valid_for_motion(), return());
__prepare_path_if_needed();
loop(global_particle_density,
segment = floor(rand(length(global_points)-1));
particle_type = if ((segment+1) == global_selected_point, global_color_a, global_color_b); //'dust 0.1 0.9 0.1 1', 'dust 0.6 0.6 0.6 1');
start = global_points:segment:1;
end = global_points:(segment+1):1;
index = floor(rand(end-start));
l(x, y, z) = slice(__get_path_at(segment, start, index), 0, 3);
particle(particle_type, x, y, z, 1, 0, 0)
);
null
);
task( _() -> (
global_markers = __create_markers();
loop(7200,
if(!global_showing_path, break());
__get_player();
if (global_needs_updating,
global_needs_updating = false;
for(global_markers, modify(_,'remove'));
global_markers = __create_markers();
);
__show_path_tick();
sleep(100);
);
for(global_markers, modify(_,'remove'));
global_markers = null;
global_showing_path = false;
));
'';
);
play(fps) ->
// hides path display
hide() ->
(
p = player();
__assert_valid_for_motion();
__prepare_path_if_needed();
if ((fps % 20 != 0) || fps < 20, exit('FPS needs to be multiples of 20') );
tpt = round(fps / 20);
mspt = 50 / tpt;
start_time = time();
point = 0;
loop( length(global_points)-1,
segment = _;
start = global_points:segment:1;
end = global_points:(segment+1):1;
loop(end-start,
v = __get_path_at(segment, start, _);
modify(p, 'location', v);
point += 1;
if ((point % tpt == 0), game_tick());
end_time = time();
took = end_time - start_time;
if (took < mspt, sleep(mspt-took));
start_time = time()
)
if (global_showing_path,
global_showing_path = false;
'Stopped showing path';
,
''
);
game_tick(1000);
'Done!'
);
_show_path_tick(particle_type, total) ->
// runs the player on the path
play() ->
(
__assert_valid_for_motion();
if (err = __is_not_valid_for_motion(), exit(err));
__prepare_path_if_needed();
loop(total,
segment = floor(rand(length(global_points)-1));
start = global_points:segment:1;
end = global_points:(segment+1):1;
index = floor(rand(end-start));
l(x, y, z) = slice(__get_path_at(segment, start, index), 0, 3);
particle(particle_type, x, y, z, 1, 0, 0)
);
null
if (!__get_player() || __get_player()~'dimension' != global_dimension, exit('No player in dimension'));
task( _() -> (
if (global_playing_path, // we don't want to join_task not to lock it just in case. No need to panic here
global_playing_path = false; // cancel current path rendering
sleep(1500); // in case we interrupt previous playbacks
);
showing_path = global_showing_path;
hide();
sleep(2000); // so particles can discipate
global_playing_path = true;
mspt = 1000 / 60;
start_time = time();
point = 0;
p = __get_player();
try (
loop( length(global_points)-1, segment = _;
start = global_points:segment:1;
end = global_points:(segment+1):1;
loop(end-start,
if (p~'sneaking', global_playing_path = false);
if (!global_playing_path, throw());
v = __get_path_at(segment, start, _)-global_player_eye_offset;
modify(p, 'location', v);
point += 1;
end_time = time();
took = end_time - start_time;
if (took < mspt, sleep(mspt-took));
start_time = time()
)
);
);
sleep(1000);
global_playing_path = false;
if (showing_path, show());
));
'';
);
// prepares empty path to fit new points
__prepare_path_if_needed_generic() ->
(
if(!global_path_precalculated, __invalidate_points_cache())
if(!global_path_precalculated, global_path_precalculated = map(range(global_points:(-1):1), null))
);
__find_position_for_linear(segment, point) ->
// linear interpolator
__interpolator_linear(segment, point) ->
(
l(va, start, mode_a) = global_points:segment;
l(vb, end, mode_b) = global_points:(segment+1);
@@ -202,26 +529,28 @@ __find_position_for_linear(segment, point) ->
dt = point/section;
dt*vb+(1-dt)*va
);
// normal distribution should look like that
//(1/sqrt(2*pi*d*d))*euler^(-((x-miu)^2)/(2*d*d))
// but we will be normalizing anyways, so who cares
__norm_prob(x, miu, d) -> euler^(-((x-miu)^2)/(2*d*d));
__find_position_for_gauss(from_index, point) ->
//gauB interpolator
__interpolator_gauB(from_index, point, deviation) ->
(
dev = global_interpol_option;
components = l();
path_point = global_points:from_index:1;
try(
for(range(from_index+1, length(global_points)),
l(v,ptime,mode) = global_points:_;
dev = if (global_interpol_option > 0, global_interpol_option,
dev = if (deviation > 0, deviation,
devs = l();
if (_+1 < length(global_points), devs += global_points:(_+1):1-ptime);
if (_-1 >= 0, devs += ptime-global_points:(_-1):1);
0.6*reduce(devs, _a+_, 0)/length(devs)
);
impact = __norm_prob(path_point+point, ptime, dev);
if(rtotal && impact < 0.000001*rtotal, throw());
//if(rtotal && impact < 0.000001*rtotal, throw()); // can work badly on segments with vastly diff lengths
components += l(v, impact);
rtotal += impact
)
@@ -229,18 +558,64 @@ __find_position_for_gauss(from_index, point) ->
try(
for(range(from_index, -1, -1),
l(v,ptime,mode) = global_points:_;
dev = if (global_interpol_option > 0, global_interpol_option,
dev = if (deviation > 0, deviation,
devs = l();
if (_+1 < length(global_points), devs += global_points:(_+1):1-ptime);
if (_-1 >= 0, devs += ptime-global_points:(_-1):1);
0.6*reduce(devs, _a+_, 0)/length(devs)
);
impact = __norm_prob(path_point+point, ptime, dev);
if(ltotal && impact < 0.000001*ltotal, throw());
//if(ltotal && impact < 0.000001*ltotal, throw());
components += l(v, impact);
ltotal += impact
)
);
total = rtotal+ltotal;
reduce(components, _a+_:0*(_:1/total), l(0,0,0,0,0))
)
);
// Catmull-Rom spline
__interpolator_cr(from_index, point) ->
(
total = global_points:(from_index+1):1 - global_points:from_index:1;
p__1 = global_points:(if(from_index == 0, 0, from_index-1)):0;
p_0 = global_points:from_index:0;
p_1 = global_points:(from_index+1):0;
p_2 = global_points:(if(from_index == (length(global_points)-2), -1, from_index+2)):0;
r = point/total; // ratio within segment
(r*((2-r)*r-1) * p__1 + (r*r*(3*r-5)+2) * p_0 + r*((4 - 3*r)*r + 1) * p_1 + (r-1)*r*r * p_2) / 2
);
// store current path in a world file
store(file) ->
(
if (!global_points, exit('No path to save'));
path_nbt = nbt('{}');
for (global_points,
point_nbt = nbt('{}');
point_nbt:'duration' = _:1;
point_nbt:'type' = _:2;
for(_:0, put(point_nbt:'pos',_,_i));
put(path_nbt:'points', point_nbt, _i);
);
store_app_data(path_nbt, file);
);
// loads path under the local file that
load(file) ->
(
path_nbt = load_app_data(file);
if (!path_nbt, exit('No path to load: '+file));
new_points = map(get(path_nbt, 'points[]'), l(_:'pos[]', _:'duration', _:'type'));
if (!new_points || first(new_points, length(_:0) != 5),
exit('Incorrect data for :'+file);
);
__start_with(_(outer(new_points)) -> new_points);
);
// when closing - shut-down visualization and playback threads
__on_close() ->
(
global_playing_path = false;
global_showing_path = false;
);
@@ -158,4 +158,9 @@ __on_player_drops_stack(player) -> (
print('');
print('__on_player_drops_stack(player)');
print('Player '+player+' drops current stack.')
);
__on_statistic(player, category, event, value) ->
(
print('__on_statistic(player, category, event, value) -> ['+join(', ',l(player, category, event, value))+']');
);
+1
View File
@@ -141,6 +141,7 @@
"DamageTracker_scarpetEventMixin",
"ServerPlayerEntity_scarpetEventMixin",
"FallingBlockEntity_scarpetEventsMixin",
"ChunkTicketManager_scarpetMixin",
"ServerChunkManager_scarpetMixin",