added in_slime_chunk, seed, add seed option to rand

This commit is contained in:
gnembon committed 2020-01-25 11:08:11 -04:00
1 parent 9914dd992c
commit 0d62fdc980
3 files changed
+45 -3

No files matched your search

@@ -102,6 +102,7 @@ import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.gen.ChunkRandom;
import net.minecraft.world.loot.context.LootContext;
import net.minecraft.world.loot.context.LootContextParameters;
import org.apache.commons.lang3.tuple.Pair;
@@ -607,6 +608,8 @@ public class CarpetExpression
* <p>Numeric function, indicating hardness of a block.</p>
* <h3><code>blast_resistance(pos)</code></h3>
* <p>Numeric function, indicating blast_resistance of a block.</p>
* <h3><code>in_slime_chunk(pos)</code></h3>
* <p>Boolean indicating if the given block position is in a slime chunk.</p>
* <h3><code>top(type, pos)</code></h3>
* <p>Returns the Y value of the topmost block at given x, z coords (y value of a block is not important), according to the
@@ -1018,6 +1021,19 @@ 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())));
this.expr.addLazyFunction("in_slime_chunk", -1, (c, t, lv) ->
{
BlockPos pos = BlockValue.fromParams((CarpetContext)c, lv, 0).block.getPos();
ChunkPos chunkPos = new ChunkPos(pos);
Value ret = new NumericValue(ChunkRandom.create(
chunkPos.x, chunkPos.z,
((CarpetContext)c).s.getWorld().getSeed(),
987234911L
).nextInt(10) == 0);
return (_c, _t) -> ret;
});
this.expr.addLazyFunction("top", -1, (c, t, lv) ->
{
String type = lv.get(0).evalValue(c).getString().toLowerCase(Locale.ROOT);
@@ -2957,6 +2973,8 @@ public class CarpetExpression
* loop(1000,game_tick()) // runs the game as fast as it can for 1000 ticks
* loop(1000,game_tick(100)) // runs the game twice as slow for 1000 ticks
* </pre>
* <h3><code>seed()</code></h3>
* <p>Returns current world seed.</p>
* <h3><code>current_dimension()</code></h3>
* <p>Returns current dimension that the script runs in.</p>
* <h3><code>in_dimension(smth, expr)</code></h3>
@@ -3399,6 +3417,12 @@ public class CarpetExpression
return (cc, tt) -> Value.TRUE;
});
this.expr.addLazyFunction("seed", -1, (c, t, lv) -> {
ServerCommandSource s = ((CarpetContext)c).s;
Value ret = new NumericValue(s.getWorld().getSeed());
return (cc, tt) -> ret;
});
this.expr.addLazyFunction("current_dimension", 0, (c, t, lv) -> {
ServerCommandSource s = ((CarpetContext)c).s;
Value retval = new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.DIMENSION.getId(s.getWorld().dimension.getType())));
+11 -3
View File
@@ -2720,12 +2720,15 @@ public class Expression
* length('foo') =&gt; 3
* </pre>
*
* <h3><code>rand(expr)</code></h3>
* <h3><code>rand(expr), rand(expr, seed)</code></h3>
* <p>returns a random number from <code>0.0</code>
* (inclusive) to <code>expr</code> (exclusive).
* In boolean context (in conditions, boolean functions, or <code>bool</code>), returns
* false if the randomly selected value is less than 1. This means that <code>rand(2)</code> returns true half
* of the time and <code>rand(5)</code> returns true for 80% (4/5) of the time</p>
* of the time and <code>rand(5)</code> returns true for 80% (4/5) of the time. If seed is not provided, uses a random seed.
* If seed is provided, each consecutive call to rand() will act like 'next' call to the same random object.
* Scarpet keeps track of up to 1024 custom random number generators, so if you exceed this number (per app), then your
* random sequence will revert to the beginning.</p>
* <pre>
* map(range(10), floor(rand(10))) =&gt; [5, 8, 0, 6, 9, 3, 9, 9, 1, 8]
* map(range(10), bool(rand(2))) =&gt; [1, 1, 1, 0, 0, 1, 1, 0, 0, 0]
@@ -2982,7 +2985,12 @@ public class Expression
addUnaryFunction("type", v -> new StringValue(v.getTypeString()));
addUnaryFunction("length", v -> new NumericValue(v.length()));
addLazyFunction("rand", 1, (c, t, lv) -> {
addLazyFunction("rand", -1, (c, t, lv) -> {
int argsize = lv.size();
Random randomizer = Expression.randomizer;
if (argsize != 1 && argsize != 2)
throw new InternalExpressionException("'rand' takes one (range) or two arguments (range and seed)");
if (argsize == 2) randomizer = c.host.getRandom(NumericValue.asNumber(lv.get(1).evalValue(c)).getLong());
Value argument = lv.get(0).evalValue(c);
if (argument instanceof ListValue)
{
@@ -4,11 +4,13 @@ import carpet.script.bundled.Module;
import carpet.script.exception.InternalExpressionException;
import carpet.script.value.FunctionValue;
import carpet.script.value.Value;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Stream;
@@ -16,6 +18,14 @@ import java.util.stream.Stream;
public abstract class ScriptHost
{
public static Map<Value, Value> systemGlobals = new ConcurrentHashMap<>();
private static Map<Long, Random> randomizers = new Long2ObjectOpenHashMap<>();
public Random getRandom(long aLong)
{
if (randomizers.size() > 1024)
randomizers.clear();
return randomizers.computeIfAbsent(aLong, Random::new);
}
public static class ModuleData