semi-auto cleanup

This commit is contained in:
gnembon committed 2023-02-04 13:26:21 +01:00
1 parent cad80f82c6
commit 8f61271606
99 files changed
+11463 -8007

No files matched your search

@@ -15,9 +15,11 @@ import carpet.script.value.LazyListValue;
import carpet.script.value.ListValue;
import carpet.script.value.NumericValue;
import carpet.script.value.Value;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Vec3i;
import net.minecraft.server.level.ServerLevel;
@@ -27,19 +29,23 @@ import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class BlockIterators {
public static void apply(Expression expression)
public class BlockIterators
{
public static void apply(final Expression expression)
{
// lazy cause of lazy expression
expression.addLazyFunction("scan", (c, t, llv) ->
{
if (llv.size() < 3) throw new InternalExpressionException("'scan' needs many more arguments");
List<Value> lv = Fluff.AbstractFunction.unpackLazy(llv.subList(0, llv.size()-1), c, Context.NONE);
CarpetContext cc = (CarpetContext)c;
BlockArgument centerLocator = BlockArgument.findIn(cc, lv, 0);
if (llv.size() < 3)
{
throw new InternalExpressionException("'scan' needs many more arguments");
}
final List<Value> lv = Fluff.AbstractFunction.unpackLazy(llv.subList(0, llv.size() - 1), c, Context.NONE);
final CarpetContext cc = (CarpetContext) c;
final BlockArgument centerLocator = BlockArgument.findIn(cc, lv, 0);
Vector3Argument rangeLocator = Vector3Argument.findIn(lv, centerLocator.offset);
BlockPos center = centerLocator.block.getPos();
Vec3i range;
final BlockPos center = centerLocator.block.getPos();
final Vec3i range;
if (rangeLocator.fromBlock)
{
@@ -48,14 +54,13 @@ public class BlockIterators {
abs(rangeLocator.vec.y - center.getY()),
abs(rangeLocator.vec.z - center.getZ())
);
//if (lv.size() > rangeLocator.offset+1) throw new InternalExpressionException("'scan' takes two block positions, and the expression")
}
else
{
range = new Vec3i(abs(rangeLocator.vec.x), abs(rangeLocator.vec.y), abs(rangeLocator.vec.z));
}
Vec3i upperRange = range;
if (lv.size() > rangeLocator.offset+1) // +1 cause we still need the expression
if (lv.size() > rangeLocator.offset + 1) // +1 cause we still need the expression
{
rangeLocator = Vector3Argument.findIn(lv, rangeLocator.offset);
if (rangeLocator.fromBlock)
@@ -65,60 +70,60 @@ public class BlockIterators {
abs(rangeLocator.vec.y - center.getY()),
abs(rangeLocator.vec.z - center.getZ())
);
//if (lv.size() > rangeLocator.offset+1) throw new InternalExpressionException("'scan' takes two block positions, and the expression")
}
else
{
upperRange = new Vec3i(abs(rangeLocator.vec.x), abs(rangeLocator.vec.y), abs(rangeLocator.vec.z));
}
}
if (llv.size() != rangeLocator.offset+1)
if (llv.size() != rangeLocator.offset + 1)
{
throw new InternalExpressionException("'scan' takes two, or three block positions, and an expression: "+lv.size()+" "+rangeLocator.offset);
throw new InternalExpressionException("'scan' takes two, or three block positions, and an expression: " + lv.size() + " " + rangeLocator.offset);
}
LazyValue expr = llv.get(rangeLocator.offset);
final LazyValue expr = llv.get(rangeLocator.offset);
int cx = center.getX();
int cy = center.getY();
int cz = center.getZ();
int xrange = range.getX();
int yrange = range.getY();
int zrange = range.getZ();
int xprange = upperRange.getX();
int yprange = upperRange.getY();
int zprange = upperRange.getZ();
final int cx = center.getX();
final int cy = center.getY();
final int cz = center.getZ();
final int xrange = range.getX();
final int yrange = range.getY();
final int zrange = range.getZ();
final int xprange = upperRange.getX();
final int yprange = upperRange.getY();
final int zprange = upperRange.getZ();
//saving outer scope
LazyValue _x = c.getVariable("_x");
LazyValue _y = c.getVariable("_y");
LazyValue _z = c.getVariable("_z");
LazyValue __ = c.getVariable("_");
final LazyValue _x = c.getVariable("_x");
final LazyValue _y = c.getVariable("_y");
final LazyValue _z = c.getVariable("_z");
final LazyValue __ = c.getVariable("_");
int sCount = 0;
outer:for (int y=cy-yrange; y <= cy+yprange; y++)
outer:
for (int y = cy - yrange; y <= cy + yprange; y++)
{
int yFinal = y;
final int yFinal = y;
c.setVariable("_y", (c_, t_) -> new NumericValue(yFinal).bindTo("_y"));
for (int x=cx-xrange; x <= cx+xprange; x++)
for (int x = cx - xrange; x <= cx + xprange; x++)
{
int xFinal = x;
final int xFinal = x;
c.setVariable("_x", (c_, t_) -> new NumericValue(xFinal).bindTo("_x"));
for (int z=cz-zrange; z <= cz+zprange; z++)
for (int z = cz - zrange; z <= cz + zprange; z++)
{
int zFinal = z;
final int zFinal = z;
c.setVariable("_z", (c_, t_) -> new NumericValue(zFinal).bindTo("_z"));
Value blockValue = BlockValue.fromCoords(((CarpetContext)c), xFinal,yFinal,zFinal).bindTo("_");
c.setVariable( "_", (cc_, t_c) -> blockValue);
final Value blockValue = BlockValue.fromCoords(((CarpetContext) c), xFinal, yFinal, zFinal).bindTo("_");
c.setVariable("_", (cc_, t_c) -> blockValue);
Value result;
try
{
result = expr.evalValue(c, t);
}
catch (ContinueStatement notIgnored)
catch (final ContinueStatement notIgnored)
{
result = notIgnored.retval;
}
catch (BreakStatement notIgnored)
catch (final BreakStatement notIgnored)
{
break outer;
}
@@ -134,66 +139,70 @@ public class BlockIterators {
c.setVariable("_y", _y);
c.setVariable("_z", _z);
c.setVariable("_", __);
int finalSCount = sCount;
final int finalSCount = sCount;
return (c_, t_) -> new NumericValue(finalSCount);
});
// must be lazy
expression.addLazyFunction("volume", (c, t, llv) ->
{
CarpetContext cc = (CarpetContext)c;
if (llv.size() < 3) throw new InternalExpressionException("'volume' needs many more arguments");
List<Value> lv = Fluff.AbstractFunction.unpackLazy(llv.subList(0, llv.size()-1), c, Context.NONE);
final CarpetContext cc = (CarpetContext) c;
if (llv.size() < 3)
{
throw new InternalExpressionException("'volume' needs many more arguments");
}
final List<Value> lv = Fluff.AbstractFunction.unpackLazy(llv.subList(0, llv.size() - 1), c, Context.NONE);
BlockArgument pos1Locator = BlockArgument.findIn(cc, lv, 0);
BlockArgument pos2Locator = BlockArgument.findIn(cc, lv, pos1Locator.offset);
BlockPos pos1 = pos1Locator.block.getPos();
BlockPos pos2 = pos2Locator.block.getPos();
final BlockArgument pos1Locator = BlockArgument.findIn(cc, lv, 0);
final BlockArgument pos2Locator = BlockArgument.findIn(cc, lv, pos1Locator.offset);
final BlockPos pos1 = pos1Locator.block.getPos();
final BlockPos pos2 = pos2Locator.block.getPos();
int x1 = pos1.getX();
int y1 = pos1.getY();
int z1 = pos1.getZ();
int x2 = pos2.getX();
int y2 = pos2.getY();
int z2 = pos2.getZ();
int minx = min(x1, x2);
int miny = min(y1, y2);
int minz = min(z1, z2);
int maxx = max(x1, x2);
int maxy = max(y1, y2);
int maxz = max(z1, z2);
LazyValue expr = llv.get(pos2Locator.offset);
final int x1 = pos1.getX();
final int y1 = pos1.getY();
final int z1 = pos1.getZ();
final int x2 = pos2.getX();
final int y2 = pos2.getY();
final int z2 = pos2.getZ();
final int minx = min(x1, x2);
final int miny = min(y1, y2);
final int minz = min(z1, z2);
final int maxx = max(x1, x2);
final int maxy = max(y1, y2);
final int maxz = max(z1, z2);
final LazyValue expr = llv.get(pos2Locator.offset);
//saving outer scope
LazyValue _x = c.getVariable("_x");
LazyValue _y = c.getVariable("_y");
LazyValue _z = c.getVariable("_z");
LazyValue __ = c.getVariable("_");
final LazyValue _x = c.getVariable("_x");
final LazyValue _y = c.getVariable("_y");
final LazyValue _z = c.getVariable("_z");
final LazyValue __ = c.getVariable("_");
int sCount = 0;
outer:for (int y=miny; y <= maxy; y++)
outer:
for (int y = miny; y <= maxy; y++)
{
int yFinal = y;
final int yFinal = y;
c.setVariable("_y", (c_, t_) -> new NumericValue(yFinal).bindTo("_y"));
for (int x=minx; x <= maxx; x++)
for (int x = minx; x <= maxx; x++)
{
int xFinal = x;
final int xFinal = x;
c.setVariable("_x", (c_, t_) -> new NumericValue(xFinal).bindTo("_x"));
for (int z=minz; z <= maxz; z++)
for (int z = minz; z <= maxz; z++)
{
int zFinal = z;
final int zFinal = z;
c.setVariable("_z", (c_, t_) -> new NumericValue(zFinal).bindTo("_z"));
Value blockValue = BlockValue.fromCoords(((CarpetContext)c), xFinal,yFinal,zFinal).bindTo("_");
c.setVariable( "_", (cc_, t_c) -> blockValue);
final Value blockValue = BlockValue.fromCoords(((CarpetContext) c), xFinal, yFinal, zFinal).bindTo("_");
c.setVariable("_", (cc_, t_c) -> blockValue);
Value result;
try
{
result = expr.evalValue(c, t);
}
catch (ContinueStatement notIgnored)
catch (final ContinueStatement notIgnored)
{
result = notIgnored.retval;
}
catch (BreakStatement notIgnored)
catch (final BreakStatement notIgnored)
{
break outer;
}
@@ -209,16 +218,16 @@ public class BlockIterators {
c.setVariable("_y", _y);
c.setVariable("_z", _z);
c.setVariable("_", __);
int finalSCount = sCount;
final int finalSCount = sCount;
return (c_, t_) -> new NumericValue(finalSCount);
});
expression.addContextFunction("neighbours", -1, (c, t, lv)->
expression.addContextFunction("neighbours", -1, (c, t, lv) ->
{
BlockPos center = BlockArgument.findIn((CarpetContext) c, lv,0).block.getPos();
ServerLevel world = ((CarpetContext) c).level();
final BlockPos center = BlockArgument.findIn((CarpetContext) c, lv, 0).block.getPos();
final ServerLevel world = ((CarpetContext) c).level();
List<Value> neighbours = new ArrayList<>();
final List<Value> neighbours = new ArrayList<>();
neighbours.add(new BlockValue(null, world, center.above()));
neighbours.add(new BlockValue(null, world, center.below()));
neighbours.add(new BlockValue(null, world, center.north()));
@@ -228,20 +237,26 @@ public class BlockIterators {
return ListValue.wrap(neighbours);
});
expression.addContextFunction("rect", -1, (c, t, lv)->
expression.addContextFunction("rect", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c;
int cx, cy, cz;
int sminx, sminy, sminz;
int smaxx, smaxy, smaxz;
BlockArgument cposLocator = BlockArgument.findIn(cc, lv, 0);
BlockPos cpos = cposLocator.block.getPos();
final CarpetContext cc = (CarpetContext) c;
final int cx;
final int cy;
final int cz;
final int sminx;
final int sminy;
final int sminz;
final int smaxx;
final int smaxy;
final int smaxz;
final BlockArgument cposLocator = BlockArgument.findIn(cc, lv, 0);
final BlockPos cpos = cposLocator.block.getPos();
cx = cpos.getX();
cy = cpos.getY();
cz = cpos.getZ();
if (lv.size() > cposLocator.offset)
{
Vector3Argument diffLocator = Vector3Argument.findIn(lv, cposLocator.offset);
final Vector3Argument diffLocator = Vector3Argument.findIn(lv, cposLocator.offset);
if (diffLocator.fromBlock)
{
sminx = Mth.floor(abs(diffLocator.vec.x - cx));
@@ -256,7 +271,7 @@ public class BlockIterators {
}
if (lv.size() > diffLocator.offset)
{
Vector3Argument posDiff = Vector3Argument.findIn(lv, diffLocator.offset);
final Vector3Argument posDiff = Vector3Argument.findIn(lv, diffLocator.offset);
if (posDiff.fromBlock)
{
smaxx = Mth.floor(abs(posDiff.vec.x - cx));
@@ -289,19 +304,21 @@ public class BlockIterators {
return new LazyListValue()
{
final int minx = cx-sminx;
final int miny = cy-sminy;
final int minz = cz-sminz;
final int maxx = cx+smaxx;
final int maxy = cy+smaxy;
final int maxz = cz+smaxz;
final int minx = cx - sminx;
final int miny = cy - sminy;
final int minz = cz - sminz;
final int maxx = cx + smaxx;
final int maxy = cy + smaxy;
final int maxz = cz + smaxz;
int x;
int y;
int z;
{
reset();
}
@Override
public boolean hasNext()
{
@@ -311,7 +328,7 @@ public class BlockIterators {
@Override
public Value next()
{
Value r = BlockValue.fromCoords(cc, x,y,z);
final Value r = BlockValue.fromCoords(cc, x, y, z);
//possibly reroll context
x++;
if (x > maxx)
@@ -347,56 +364,57 @@ public class BlockIterators {
@Override
public String getString()
{
return String.format(Locale.ROOT, "rect[(%d,%d,%d),..,(%d,%d,%d)]",minx, miny, minz, maxx, maxy, maxz);
return String.format(Locale.ROOT, "rect[(%d,%d,%d),..,(%d,%d,%d)]", minx, miny, minz, maxx, maxy, maxz);
}
};
});
expression.addContextFunction("diamond", -1, (c, t, lv)->
expression.addContextFunction("diamond", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
final CarpetContext cc = (CarpetContext) c;
BlockArgument cposLocator=BlockArgument.findIn((CarpetContext)c,lv,0);
BlockPos cpos = cposLocator.block.getPos();
final BlockArgument cposLocator = BlockArgument.findIn((CarpetContext) c, lv, 0);
final BlockPos cpos = cposLocator.block.getPos();
int cx;
int cy;
int cz;
int width;
int height;
final int cx;
final int cy;
final int cz;
final int width;
final int height;
try
{
cx = cpos.getX();
cy = cpos.getY();
cz = cpos.getZ();
if (lv.size()==cposLocator.offset)
if (lv.size() == cposLocator.offset)
{
Value retval = ListValue.of(
BlockValue.fromCoords(cc, cx, cy-1, cz),
return ListValue.of(
BlockValue.fromCoords(cc, cx, cy - 1, cz),
BlockValue.fromCoords(cc, cx, cy, cz),
BlockValue.fromCoords(cc, cx-1, cy, cz),
BlockValue.fromCoords(cc, cx, cy, cz-1),
BlockValue.fromCoords(cc, cx+1, cy, cz),
BlockValue.fromCoords(cc, cx, cy, cz+1),
BlockValue.fromCoords(cc, cx, cy+1, cz)
BlockValue.fromCoords(cc, cx - 1, cy, cz),
BlockValue.fromCoords(cc, cx, cy, cz - 1),
BlockValue.fromCoords(cc, cx + 1, cy, cz),
BlockValue.fromCoords(cc, cx, cy, cz + 1),
BlockValue.fromCoords(cc, cx, cy + 1, cz)
);
return retval;
}
else if (lv.size()==1+cposLocator.offset)
else if (lv.size() == 1 + cposLocator.offset)
{
width = (int) ((NumericValue) lv.get(cposLocator.offset)).getLong();
height = 0;
}
else if(lv.size()==2+cposLocator.offset)
else if (lv.size() == 2 + cposLocator.offset)
{
width = (int) ((NumericValue) lv.get(cposLocator.offset)).getLong();
height = (int) ((NumericValue) lv.get(cposLocator.offset+1)).getLong();
} else{
height = (int) ((NumericValue) lv.get(cposLocator.offset + 1)).getLong();
}
else
{
throw new InternalExpressionException("Incorrect number of arguments for 'diamond'");
}
}
catch (ClassCastException exc)
catch (final ClassCastException ignored)
{
throw new InternalExpressionException("Attempted to pass a non-number to 'diamond'");
}
@@ -406,9 +424,11 @@ public class BlockIterators {
{
int curradius;
int curpos;
{
reset();
}
@Override
public boolean hasNext()
{
@@ -425,9 +445,9 @@ public class BlockIterators {
}
// x = 3-|i-6|
// z = |( (i-3)%12-6|-3
Value block = BlockValue.fromCoords(cc, cx+(curradius-abs(curpos-2*curradius)), cy, cz-curradius+abs( abs(curpos-curradius)%(4*curradius) -2*curradius ));
final Value block = BlockValue.fromCoords(cc, cx + (curradius - abs(curpos - 2 * curradius)), cy, cz - curradius + abs(abs(curpos - curradius) % (4 * curradius) - 2 * curradius));
curpos++;
if (curpos>=curradius*4)
if (curpos >= curradius * 4)
{
curradius++;
curpos = 0;
@@ -446,7 +466,7 @@ public class BlockIterators {
@Override
public String getString()
{
return String.format(Locale.ROOT, "diamond[(%d,%d,%d),%d,0]",cx, cy, cz, width);
return String.format(Locale.ROOT, "diamond[(%d,%d,%d),%d,0]", cx, cy, cz, width);
}
};
}
@@ -457,9 +477,11 @@ public class BlockIterators {
int curradius;
int curpos;
int curheight;
{
reset();
}
@Override
public boolean hasNext()
{
@@ -471,27 +493,26 @@ public class BlockIterators {
{
if (curheight == -height || curheight == height)
{
return BlockValue.fromCoords(cc, cx, cy+curheight++, cz);
return BlockValue.fromCoords(cc, cx, cy + curheight++, cz);
}
if (curradius == 0)
{
curradius++;
return BlockValue.fromCoords(cc, cx, cy+curheight, cz);
return BlockValue.fromCoords(cc, cx, cy + curheight, cz);
}
// x = 3-|i-6|
// z = |( (i-3)%12-6|-3
Value block = BlockValue.fromCoords(cc, cx+(curradius-abs(curpos-2*curradius)), cy+curheight, cz-curradius+abs( abs(curpos-curradius)%(4*curradius) -2*curradius ));
final Value block = BlockValue.fromCoords(cc, cx + (curradius - abs(curpos - 2 * curradius)), cy + curheight, cz - curradius + abs(abs(curpos - curradius) % (4 * curradius) - 2 * curradius));
curpos++;
if (curpos>=curradius*4)
if (curpos >= curradius * 4)
{
curradius++;
curpos = 0;
if (curradius>width -abs(width*curheight/height))
if (curradius > width - abs(width * curheight / height))
{
curheight++;
curradius = 0;
curpos = 0;
}
}
return block;
@@ -508,7 +529,7 @@ public class BlockIterators {
@Override
public String getString()
{
return String.format(Locale.ROOT, "diamond[(%d,%d,%d),%d,%d]",cx, cy, cz, width, height);
return String.format(Locale.ROOT, "diamond[(%d,%d,%d),%d,%d]", cx, cy, cz, width, height);
}
};
}