Skip to content

Commit 17e28bc

Browse files
committed
Use ScaleAndTranslateTransform in everything using WorldEditExpressionEnvironment
1 parent 9f4fae4 commit 17e28bc

6 files changed

Lines changed: 202 additions & 127 deletions

File tree

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

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
import com.sk89q.worldedit.math.interpolation.Node;
9595
import com.sk89q.worldedit.math.noise.RandomNoise;
9696
import com.sk89q.worldedit.math.transform.AffineTransform;
97+
import com.sk89q.worldedit.math.transform.ScaleAndTranslateTransform;
98+
import com.sk89q.worldedit.math.transform.Transform;
9799
import com.sk89q.worldedit.regions.CuboidRegion;
98100
import com.sk89q.worldedit.regions.CylinderRegion;
99101
import com.sk89q.worldedit.regions.EllipsoidRegion;
@@ -2267,6 +2269,7 @@ public List<Countable<BlockState>> getBlockDistribution(Region region, boolean s
22672269
* @throws ExpressionException if there is a problem with the expression
22682270
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
22692271
*/
2272+
@Deprecated
22702273
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
22712274
final Pattern pattern, final String expressionString, final boolean hollow)
22722275
throws ExpressionException, MaxChangedBlocksException {
@@ -2287,12 +2290,32 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
22872290
* @throws ExpressionException if there is a problem with the expression
22882291
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
22892292
*/
2293+
@Deprecated
22902294
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
22912295
final Pattern pattern, final String expressionString, final boolean hollow, final int timeout)
22922296
throws ExpressionException, MaxChangedBlocksException {
2297+
return makeShape(region, new ScaleAndTranslateTransform(zero, unit), pattern, expressionString, hollow, timeout);
2298+
}
2299+
2300+
/**
2301+
* Generate a shape for the given expression.
2302+
*
2303+
* @param region the region to generate the shape in
2304+
* @param transform the transformation for x/y/z variables
2305+
* @param pattern the default material to make the shape from
2306+
* @param expressionString the expression defining the shape
2307+
* @param hollow whether the shape should be hollow
2308+
* @param timeout the time, in milliseconds, to wait for each expression evaluation before halting it. -1 to disable
2309+
* @return number of blocks changed
2310+
* @throws ExpressionException if there is a problem with the expression
2311+
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
2312+
*/
2313+
public int makeShape(final Region region,
2314+
Transform transform, final Pattern pattern, final String expressionString, final boolean hollow, final int timeout)
2315+
throws ExpressionException, MaxChangedBlocksException {
22932316
final Expression expression = Expression.compile(expressionString, "x", "y", "z", "type", "data");
22942317
expression.optimize();
2295-
return makeShape(region, zero, unit, pattern, expression, hollow, timeout);
2318+
return makeShape(region, transform, pattern, expression, hollow, timeout);
22962319
}
22972320

22982321
/**
@@ -2302,9 +2325,23 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
23022325
* The Expression class is subject to change. Expressions should be provided via the string overload.
23032326
* </p>
23042327
*/
2328+
@Deprecated
23052329
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
23062330
final Pattern pattern, final Expression expression, final boolean hollow, final int timeout)
23072331
throws ExpressionException, MaxChangedBlocksException {
2332+
return makeShape(region, new ScaleAndTranslateTransform(zero, unit), pattern, expression, hollow, timeout);
2333+
}
2334+
2335+
/**
2336+
* Internal version of {@link EditSession#makeShape(Region, Vector3, Vector3, Pattern, String, boolean, int)}.
2337+
*
2338+
* <p>
2339+
* The Expression class is subject to change. Expressions should be provided via the string overload.
2340+
* </p>
2341+
*/
2342+
public int makeShape(final Region region, Transform transform,
2343+
final Pattern pattern, final Expression expression, final boolean hollow, final int timeout)
2344+
throws ExpressionException, MaxChangedBlocksException {
23082345

23092346
expression.getSlots().getVariable("x")
23102347
.orElseThrow(IllegalStateException::new);
@@ -2318,16 +2355,17 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
23182355
final Variable dataVariable = expression.getSlots().getVariable("data")
23192356
.orElseThrow(IllegalStateException::new);
23202357

2321-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero);
2358+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
23222359
expression.setEnvironment(environment);
23232360

23242361
final int[] timedOut = {0};
2362+
final Transform transformInverse = transform.inverse();
23252363
final ArbitraryShape shape = new ArbitraryShape(region) {
23262364
@Override
23272365
protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial) {
23282366
final Vector3 current = Vector3.at(x, y, z);
23292367
environment.setCurrentBlock(current);
2330-
final Vector3 inputPosition = current.subtract(zero).divide(unit);
2368+
final Vector3 inputPosition = transformInverse.apply(current);
23312369

23322370
try {
23332371
int[] legacy = LegacyMapper.getInstance().getLegacyFromBlock(defaultMaterial.toImmutableState());
@@ -2384,9 +2422,10 @@ protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial)
23842422
* @throws ExpressionException thrown on invalid expression input
23852423
* @throws MaxChangedBlocksException thrown if too many blocks are changed
23862424
*/
2425+
@Deprecated
23872426
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString)
23882427
throws ExpressionException, MaxChangedBlocksException {
2389-
return deformRegion(region, zero, unit, expressionString, WorldEdit.getInstance().getConfiguration().calculationTimeout);
2428+
return deformRegion(region, new ScaleAndTranslateTransform(zero, unit), expressionString, WorldEdit.getInstance().getConfiguration().calculationTimeout);
23902429
}
23912430

23922431
/**
@@ -2405,11 +2444,31 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24052444
* @throws ExpressionException thrown on invalid expression input
24062445
* @throws MaxChangedBlocksException thrown if too many blocks are changed
24072446
*/
2447+
@Deprecated
24082448
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString,
24092449
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2450+
final Transform transform = new ScaleAndTranslateTransform(zero, unit);
2451+
return deformRegion(region, transform, expressionString, timeout);
2452+
}
2453+
2454+
/**
2455+
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
2456+
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
2457+
* have changed.
2458+
*
2459+
* @param region the region to deform
2460+
* @param transform the coordinate system
2461+
* @param expressionString the expression to evaluate for each block
2462+
* @param timeout maximum time for the expression to evaluate for each block. -1 for unlimited.
2463+
* @return number of blocks changed
2464+
* @throws ExpressionException thrown on invalid expression input
2465+
* @throws MaxChangedBlocksException thrown if too many blocks are changed
2466+
*/
2467+
public int deformRegion(final Region region, final Transform transform, final String expressionString,
2468+
final int timeout) throws ExpressionException, MaxChangedBlocksException {
24102469
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
24112470
expression.optimize();
2412-
return deformRegion(region, zero, unit, expression, timeout);
2471+
return deformRegion(region, transform, expression, timeout);
24132472
}
24142473

24152474
/**
@@ -2419,32 +2478,47 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24192478
* The Expression class is subject to change. Expressions should be provided via the string overload.
24202479
* </p>
24212480
*/
2481+
@Deprecated
24222482
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final Expression expression,
24232483
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2484+
return deformRegion(region, new ScaleAndTranslateTransform(zero, unit), expression, timeout);
2485+
}
2486+
2487+
/**
2488+
* Internal version of {@link EditSession#deformRegion(Region, Vector3, Vector3, String, int)}.
2489+
*
2490+
* <p>
2491+
* The Expression class is subject to change. Expressions should be provided via the string overload.
2492+
* </p>
2493+
*/
2494+
public int deformRegion(final Region region, final Transform transform, final Expression expression,
2495+
final int timeout) throws ExpressionException, MaxChangedBlocksException {
24242496
final Variable x = expression.getSlots().getVariable("x")
24252497
.orElseThrow(IllegalStateException::new);
24262498
final Variable y = expression.getSlots().getVariable("y")
24272499
.orElseThrow(IllegalStateException::new);
24282500
final Variable z = expression.getSlots().getVariable("z")
24292501
.orElseThrow(IllegalStateException::new);
24302502

2431-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero);
2503+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
24322504
expression.setEnvironment(environment);
24332505

24342506
final DoubleArrayList<BlockVector3, BaseBlock> queue = new DoubleArrayList<>(false);
24352507

2508+
final Transform transformInverse = transform.inverse();
24362509
for (BlockVector3 targetBlockPosition : region) {
24372510
final Vector3 targetPosition = targetBlockPosition.toVector3();
24382511
environment.setCurrentBlock(targetPosition);
24392512

24402513
// transform from target coordinates
2441-
final Vector3 inputPosition = targetPosition.subtract(zero).divide(unit);
2514+
final Vector3 inputPosition = transformInverse.apply(targetPosition);
24422515

24432516
// deform
24442517
expression.evaluate(new double[]{ inputPosition.x(), inputPosition.y(), inputPosition.z() }, timeout);
2518+
final Vector3 outputPosition = Vector3.at(x.value(), y.value(), z.value());
24452519

24462520
// transform to source coordinates, round-nearest
2447-
final BlockVector3 sourcePosition = environment.toWorld(x.value(), y.value(), z.value());
2521+
final BlockVector3 sourcePosition = transform.apply(outputPosition).add(0.5, 0.5, 0.5).toBlockPoint();
24482522

24492523
// read block from world
24502524
final BaseBlock material = world.getFullBlock(sourcePosition);
@@ -2752,28 +2826,36 @@ private void recurseHollow(Region region, BlockVector3 origin, Set<BlockVector3>
27522826
}
27532827
}
27542828

2829+
@Deprecated
27552830
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BiomeType biomeType,
27562831
final String expressionString, final boolean hollow) throws ExpressionException {
27572832
return makeBiomeShape(region, zero, unit, biomeType, expressionString, hollow, WorldEdit.getInstance().getConfiguration().calculationTimeout);
27582833
}
27592834

2835+
@Deprecated
27602836
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BiomeType biomeType,
27612837
final String expressionString, final boolean hollow, final int timeout) throws ExpressionException {
2838+
return makeBiomeShape(region, new ScaleAndTranslateTransform(zero, unit), biomeType, expressionString, hollow, timeout);
2839+
}
2840+
2841+
public int makeBiomeShape(final Region region, Transform transform, final BiomeType biomeType,
2842+
final String expressionString, final boolean hollow, final int timeout) throws ExpressionException {
27622843

27632844
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
27642845
expression.optimize();
27652846

27662847
final EditSession editSession = this;
2767-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, unit, zero);
2848+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, transform);
27682849
expression.setEnvironment(environment);
27692850

27702851
AtomicInteger timedOut = new AtomicInteger();
2852+
final Transform transformInverse = transform.inverse();
27712853
final ArbitraryBiomeShape shape = new ArbitraryBiomeShape(region) {
27722854
@Override
27732855
protected BiomeType getBiome(int x, int y, int z, BiomeType defaultBiomeType) {
27742856
final Vector3 current = Vector3.at(x, y, z);
27752857
environment.setCurrentBlock(current);
2776-
final Vector3 inputPosition = current.subtract(zero).divide(unit);
2858+
final Vector3 inputPosition = transformInverse.apply(current);
27772859

27782860
try {
27792861
if (expression.evaluate(new double[]{ inputPosition.x(), inputPosition.y(), inputPosition.z() }, timeout) <= 0) {

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

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
import com.sk89q.worldedit.internal.annotation.Radii;
3333
import com.sk89q.worldedit.internal.annotation.Selection;
3434
import com.sk89q.worldedit.internal.expression.ExpressionException;
35+
import com.sk89q.worldedit.internal.util.TransformUtil;
3536
import com.sk89q.worldedit.math.BlockVector3;
36-
import com.sk89q.worldedit.math.Vector3;
37+
import com.sk89q.worldedit.math.transform.Transform;
3738
import com.sk89q.worldedit.regions.Region;
3839
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
3940
import com.sk89q.worldedit.util.formatting.text.TextComponent;
@@ -373,42 +374,10 @@ public int generate(Actor actor, LocalSession session, EditSession editSession,
373374
boolean offsetPlacement,
374375
@Switch(name = 'c', desc = "Use the selection's center as origin")
375376
boolean offsetCenter) throws WorldEditException {
376-
377-
final Vector3 zero;
378-
Vector3 unit;
379-
380-
if (useRawCoords) {
381-
zero = Vector3.ZERO;
382-
unit = Vector3.ONE;
383-
} else if (offsetPlacement) {
384-
zero = session.getPlacementPosition(actor).toVector3();
385-
unit = Vector3.ONE;
386-
} else if (offsetCenter) {
387-
final Vector3 min = region.getMinimumPoint().toVector3();
388-
final Vector3 max = region.getMaximumPoint().toVector3();
389-
390-
zero = max.add(min).multiply(0.5);
391-
unit = Vector3.ONE;
392-
} else {
393-
final Vector3 min = region.getMinimumPoint().toVector3();
394-
final Vector3 max = region.getMaximumPoint().toVector3();
395-
396-
zero = max.add(min).multiply(0.5);
397-
unit = max.subtract(zero);
398-
399-
if (unit.x() == 0) {
400-
unit = unit.withX(1.0);
401-
}
402-
if (unit.y() == 0) {
403-
unit = unit.withY(1.0);
404-
}
405-
if (unit.z() == 0) {
406-
unit = unit.withZ(1.0);
407-
}
408-
}
377+
final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
409378

410379
try {
411-
final int affected = editSession.makeShape(region, zero, unit, pattern, String.join(" ", expression), hollow, session.getTimeout());
380+
final int affected = editSession.makeShape(region, transform, pattern, String.join(" ", expression), hollow, session.getTimeout());
412381
if (actor instanceof Player) {
413382
((Player) actor).findFreePosition();
414383
}
@@ -442,41 +411,10 @@ public int generateBiome(Actor actor, LocalSession session, EditSession editSess
442411
boolean offsetPlacement,
443412
@Switch(name = 'c', desc = "Use the selection's center as origin")
444413
boolean offsetCenter) throws WorldEditException {
445-
final Vector3 zero;
446-
Vector3 unit;
447-
448-
if (useRawCoords) {
449-
zero = Vector3.ZERO;
450-
unit = Vector3.ONE;
451-
} else if (offsetPlacement) {
452-
zero = session.getPlacementPosition(actor).toVector3();
453-
unit = Vector3.ONE;
454-
} else if (offsetCenter) {
455-
final Vector3 min = region.getMinimumPoint().toVector3();
456-
final Vector3 max = region.getMaximumPoint().toVector3();
457-
458-
zero = max.add(min).multiply(0.5);
459-
unit = Vector3.ONE;
460-
} else {
461-
final Vector3 min = region.getMinimumPoint().toVector3();
462-
final Vector3 max = region.getMaximumPoint().toVector3();
463-
464-
zero = max.add(min).multiply(0.5);
465-
unit = max.subtract(zero);
466-
467-
if (unit.x() == 0) {
468-
unit = unit.withX(1.0);
469-
}
470-
if (unit.y() == 0) {
471-
unit = unit.withY(1.0);
472-
}
473-
if (unit.z() == 0) {
474-
unit = unit.withZ(1.0);
475-
}
476-
}
414+
final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
477415

478416
try {
479-
final int affected = editSession.makeBiomeShape(region, zero, unit, target, String.join(" ", expression), hollow, session.getTimeout());
417+
final int affected = editSession.makeBiomeShape(region, transform, target, String.join(" ", expression), hollow, session.getTimeout());
480418
actor.printInfo(TranslatableComponent.of("worldedit.generatebiome.changed", TextComponent.of(affected)));
481419
return affected;
482420
} catch (ExpressionException e) {

0 commit comments

Comments
 (0)