Skip to content

Commit 7cf4a09

Browse files
committed
Add inputExtent parameter and separate in/outputTransform for deformRegion
1 parent 2a70a30 commit 7cf4a09

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.sk89q.worldedit.extension.platform.Watchdog;
2929
import com.sk89q.worldedit.extent.ChangeSetExtent;
3030
import com.sk89q.worldedit.extent.Extent;
31+
import com.sk89q.worldedit.extent.InputExtent;
3132
import com.sk89q.worldedit.extent.MaskingExtent;
3233
import com.sk89q.worldedit.extent.NullExtent;
3334
import com.sk89q.worldedit.extent.TracingExtent;
@@ -2495,6 +2496,28 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24952496
return deformRegion(region, transform, expressionString, timeout);
24962497
}
24972498

2499+
/**
2500+
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
2501+
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
2502+
* have changed.
2503+
*
2504+
* @param region the region to deform
2505+
* @param targetTransform the target coordinate system
2506+
* @param expressionString the expression to evaluate for each block
2507+
* @param timeout maximum time for the expression to evaluate for each block. -1 for unlimited.
2508+
* @param sourceExtent the InputExtent to fetch blocks from, for instance a World or a Clipboard
2509+
* @param sourceTransform the source coordinate system
2510+
* @return number of blocks changed
2511+
* @throws ExpressionException thrown on invalid expression input
2512+
* @throws MaxChangedBlocksException thrown if too many blocks are changed
2513+
*/
2514+
public int deformRegion(final Region region, final Transform targetTransform, final String expressionString,
2515+
final int timeout, InputExtent sourceExtent, Transform sourceTransform) throws ExpressionException, MaxChangedBlocksException {
2516+
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
2517+
expression.optimize();
2518+
return deformRegion(region, targetTransform, expression, timeout, sourceExtent, sourceTransform);
2519+
}
2520+
24982521
/**
24992522
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
25002523
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
@@ -2508,11 +2531,10 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
25082531
* @throws ExpressionException thrown on invalid expression input
25092532
* @throws MaxChangedBlocksException thrown if too many blocks are changed
25102533
*/
2534+
@Deprecated
25112535
public int deformRegion(final Region region, final Transform transform, final String expressionString,
25122536
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2513-
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
2514-
expression.optimize();
2515-
return deformRegion(region, transform, expression, timeout);
2537+
return deformRegion(region, transform, expressionString, timeout, world, transform);
25162538
}
25172539

25182540
/**
@@ -2525,7 +2547,8 @@ public int deformRegion(final Region region, final Transform transform, final St
25252547
@Deprecated
25262548
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final Expression expression,
25272549
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2528-
return deformRegion(region, new ScaleAndTranslateTransform(zero, unit), expression, timeout);
2550+
final Transform transform = new ScaleAndTranslateTransform(zero, unit);
2551+
return deformRegion(region, transform, expression, timeout, world, transform);
25292552
}
25302553

25312554
/**
@@ -2535,37 +2558,37 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
25352558
* The Expression class is subject to change. Expressions should be provided via the string overload.
25362559
* </p>
25372560
*/
2538-
public int deformRegion(final Region region, final Transform transform, final Expression expression,
2539-
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2561+
public int deformRegion(final Region region, final Transform targetTransform, final Expression expression,
2562+
final int timeout, InputExtent sourceExtent, final Transform sourceTransform) throws ExpressionException, MaxChangedBlocksException {
25402563
final Variable x = expression.getSlots().getVariable("x")
25412564
.orElseThrow(IllegalStateException::new);
25422565
final Variable y = expression.getSlots().getVariable("y")
25432566
.orElseThrow(IllegalStateException::new);
25442567
final Variable z = expression.getSlots().getVariable("z")
25452568
.orElseThrow(IllegalStateException::new);
25462569

2547-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
2570+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, targetTransform);
25482571
expression.setEnvironment(environment);
25492572

25502573
final DoubleArrayList<BlockVector3, BaseBlock> queue = new DoubleArrayList<>(false);
25512574

2552-
final Transform transformInverse = transform.inverse();
2575+
final Transform targetTransformInverse = targetTransform.inverse();
25532576
for (BlockVector3 targetBlockPosition : region) {
25542577
final Vector3 targetPosition = targetBlockPosition.toVector3();
25552578
environment.setCurrentBlock(targetPosition);
25562579

25572580
// transform from target coordinates
2558-
final Vector3 inputPosition = transformInverse.apply(targetPosition);
2581+
final Vector3 inputPosition = targetTransformInverse.apply(targetPosition);
25592582

25602583
// deform
25612584
expression.evaluate(new double[]{ inputPosition.x(), inputPosition.y(), inputPosition.z() }, timeout);
25622585
final Vector3 outputPosition = Vector3.at(x.value(), y.value(), z.value());
25632586

25642587
// transform to source coordinates, round-nearest
2565-
final BlockVector3 sourcePosition = transform.apply(outputPosition).add(0.5, 0.5, 0.5).toBlockPoint();
2588+
final BlockVector3 sourcePosition = sourceTransform.apply(outputPosition).add(0.5, 0.5, 0.5).toBlockPoint();
25662589

2567-
// read block from world
2568-
final BaseBlock material = world.getFullBlock(sourcePosition);
2590+
// read block from source extent (e.g. world/clipboard)
2591+
final BaseBlock material = sourceExtent.getFullBlock(sourcePosition);
25692592

25702593
// queue operation
25712594
queue.put(targetBlockPosition, material);

worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.sk89q.worldedit.entity.Player;
3232
import com.sk89q.worldedit.extension.platform.Actor;
3333
import com.sk89q.worldedit.extent.Extent;
34+
import com.sk89q.worldedit.extent.InputExtent;
3435
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
3536
import com.sk89q.worldedit.function.GroundFunction;
3637
import com.sk89q.worldedit.function.RegionFunction;
@@ -504,9 +505,10 @@ public int deform(Actor actor, LocalSession session, EditSession editSession,
504505
boolean offsetCenter) throws WorldEditException {
505506

506507
final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
508+
final InputExtent inputExtent = editSession.getWorld();
507509

508510
try {
509-
final int affected = editSession.deformRegion(region, transform, String.join(" ", expression), session.getTimeout());
511+
final int affected = editSession.deformRegion(region, transform, String.join(" ", expression), session.getTimeout(), inputExtent, transform);
510512
if (actor instanceof Player) {
511513
((Player) actor).findFreePosition();
512514
}

worldedit-core/src/main/java/com/sk89q/worldedit/function/factory/Deform.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ private record DeformOperation(
170170
public Operation resume(RunContext run) throws WorldEditException {
171171
try {
172172
// TODO: Move deformation code
173-
((EditSession) destination).deformRegion(region, transform, expression, timeout);
173+
final EditSession editSession = (EditSession) destination;
174+
editSession.deformRegion(region, transform, expression, timeout, editSession.getWorld(), transform);
174175
return null;
175176
} catch (ExpressionException e) {
176177
throw new RuntimeException("Failed to execute expression", e); // TODO: Better exception to throw here?

0 commit comments

Comments
 (0)