Implement linear patterns using RandomPattern + SimpleRandom (#2630)

This commit is contained in:
Hannes Greule 2024-03-17 13:17:00 +01:00 committed by GitHub
parent 2d0ea9adf8
commit dc61efe11c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 141 additions and 13 deletions

View File

@ -2,7 +2,7 @@ package com.fastasyncworldedit.core.extension.factory.parser.pattern;
import com.fastasyncworldedit.core.configuration.Caption; import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.extension.factory.parser.RichParser; import com.fastasyncworldedit.core.extension.factory.parser.RichParser;
import com.fastasyncworldedit.core.function.pattern.Linear2DBlockPattern; import com.fastasyncworldedit.core.math.random.Linear2DRandom;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.command.util.SuggestionHelper; import com.sk89q.worldedit.command.util.SuggestionHelper;
@ -14,7 +14,6 @@ import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockStateHolder;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
public class Linear2DPatternParser extends RichParser<Pattern> { public class Linear2DPatternParser extends RichParser<Pattern> {
@ -59,9 +58,8 @@ public class Linear2DPatternParser extends RichParser<Pattern> {
zScale = Integer.parseInt(arguments[2]); zScale = Integer.parseInt(arguments[2]);
Preconditions.checkArgument(zScale != 0); Preconditions.checkArgument(zScale != 0);
} }
if (inner instanceof RandomPattern) { if (inner instanceof RandomPattern rp) {
Set<Pattern> patterns = ((RandomPattern) inner).getPatterns(); return new RandomPattern(new Linear2DRandom(xScale, zScale), rp);
return new Linear2DBlockPattern(patterns.toArray(new Pattern[0]), xScale, zScale);
} }
throw new InputParseException(TextComponent.of("Pattern " + inner.getClass().getSimpleName() throw new InputParseException(TextComponent.of("Pattern " + inner.getClass().getSimpleName()
+ " cannot be used with " + getPrefix())); + " cannot be used with " + getPrefix()));

View File

@ -2,7 +2,7 @@ package com.fastasyncworldedit.core.extension.factory.parser.pattern;
import com.fastasyncworldedit.core.configuration.Caption; import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.extension.factory.parser.RichParser; import com.fastasyncworldedit.core.extension.factory.parser.RichParser;
import com.fastasyncworldedit.core.function.pattern.Linear3DBlockPattern; import com.fastasyncworldedit.core.math.random.Linear3DRandom;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.command.util.SuggestionHelper; import com.sk89q.worldedit.command.util.SuggestionHelper;
@ -14,7 +14,6 @@ import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockStateHolder;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
public class Linear3DPatternParser extends RichParser<Pattern> { public class Linear3DPatternParser extends RichParser<Pattern> {
@ -64,9 +63,8 @@ public class Linear3DPatternParser extends RichParser<Pattern> {
zScale = Integer.parseInt(arguments[3]); zScale = Integer.parseInt(arguments[3]);
Preconditions.checkArgument(zScale != 0); Preconditions.checkArgument(zScale != 0);
} }
if (inner instanceof RandomPattern) { if (inner instanceof RandomPattern rp) {
Set<Pattern> patterns = ((RandomPattern) inner).getPatterns(); return new RandomPattern(new Linear3DRandom(xScale, yScale, zScale), rp);
return new Linear3DBlockPattern(patterns.toArray(new Pattern[0]), xScale, yScale, zScale);
} }
throw new InputParseException(TextComponent.of("Pattern " + inner.getClass().getSimpleName() throw new InputParseException(TextComponent.of("Pattern " + inner.getClass().getSimpleName()
+ " cannot be used with " + getPrefix())); + " cannot be used with " + getPrefix()));

View File

@ -9,6 +9,11 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import static java.lang.Math.floorDiv; import static java.lang.Math.floorDiv;
/**
* @deprecated replaced by {@link com.sk89q.worldedit.function.pattern.RandomPattern}
* combined with {@link com.fastasyncworldedit.core.math.random.Linear2DRandom}.
*/
@Deprecated(forRemoval = true, since = "TODO")
public class Linear2DBlockPattern extends AbstractPattern { public class Linear2DBlockPattern extends AbstractPattern {
private final Pattern[] patternsArray; private final Pattern[] patternsArray;

View File

@ -9,6 +9,11 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import static java.lang.Math.floorDiv; import static java.lang.Math.floorDiv;
/**
* @deprecated replaced by {@link com.sk89q.worldedit.function.pattern.RandomPattern}
* combined with {@link com.fastasyncworldedit.core.math.random.Linear3DRandom}.
*/
@Deprecated(forRemoval = true, since = "TODO")
public class Linear3DBlockPattern extends AbstractPattern { public class Linear3DBlockPattern extends AbstractPattern {
private final Pattern[] patternsArray; private final Pattern[] patternsArray;

View File

@ -0,0 +1,50 @@
package com.fastasyncworldedit.core.math.random;
import static com.fastasyncworldedit.core.math.random.Linear3DRandom.doubleDiv;
import static java.lang.Math.floorDiv;
/**
* A {@link SimpleRandom} that deterministically maps coordinates
* to values.
* @since TODO
*/
public class Linear2DRandom implements SimpleRandom {
private final int xScale;
private final int zScale;
/**
* Creates a new {@link Linear2DRandom} instance
*
* @param xScale the scale applied to the x component of a coordinate
* @param zScale the scale applied to the z component of a coordinate
*/
public Linear2DRandom(final int xScale, final int zScale) {
this.xScale = xScale;
this.zScale = zScale;
}
@Override
public double nextDouble(final int x, final int y, final int z) {
return nextDouble(x, y, z, 1d);
}
@Override
public double nextDouble(final int x, final int y, final int z, double bound) {
double index = (doubleDiv(x, this.xScale) + doubleDiv(z, this.zScale)) % bound;
if (index < 0) {
index += bound;
}
return index;
}
@Override
public int nextInt(final int x, final int y, final int z, final int bound) {
int index = (floorDiv(x, this.xScale) + floorDiv(z, this.zScale)) % bound;
if (index < 0) {
index += bound;
}
return index;
}
}

View File

@ -0,0 +1,58 @@
package com.fastasyncworldedit.core.math.random;
import static java.lang.Math.floorDiv;
/**
* A {@link SimpleRandom} that deterministically maps coordinates
* to values.
* @since TODO
*/
public class Linear3DRandom implements SimpleRandom {
private final int xScale;
private final int yScale;
private final int zScale;
/**
* Creates a new {@link Linear3DRandom} instance
*
* @param xScale the scale applied to the x component of a coordinate
* @param yScale the scale applied to the y component of a coordinate
* @param zScale the scale applied to the z component of a coordinate
*/
public Linear3DRandom(final int xScale, final int yScale, final int zScale) {
this.xScale = xScale;
this.yScale = yScale;
this.zScale = zScale;
}
@Override
public double nextDouble(final int x, final int y, final int z) {
return nextDouble(x, y, z, 1d);
}
@Override
public double nextDouble(final int x, final int y, final int z, double bound) {
double index = (doubleDiv(x, this.xScale) + doubleDiv(y, this.yScale) + doubleDiv(z, this.zScale)) % bound;
if (index < 0) {
index += bound;
}
return index;
}
// used to avoid explicit conversion at call site
static double doubleDiv(double dividend, double divisor) {
// add a minimal value to avoid too many integral values hitting the exact weight of an entry in SimpleRandomCollection
return Math.nextUp(dividend) / divisor;
}
@Override
public int nextInt(final int x, final int y, final int z, final int bound) {
int index = (floorDiv(x, this.xScale) + floorDiv(y, this.yScale) + floorDiv(z, this.zScale)) % bound;
if (index < 0) {
index += bound;
}
return index;
}
}

View File

@ -13,6 +13,20 @@ public interface SimpleRandom {
*/ */
double nextDouble(int x, int y, int z); double nextDouble(int x, int y, int z);
/**
* Generate a random double from three integer components.
* The generated value is between 0 (inclusive) and {@code bound} (exclusive).
*
* @param x the first component
* @param y the second component
* @param z the third component
* @param bound upper bound (exclusive)
* @return a double between 0 (inclusive) and {@code bound} (exclusive)
*/
default double nextDouble(int x, int y, int z, double bound) {
return nextDouble(x, y, z) * bound;
}
/** /**
* Generate a random integer from three integer components. * Generate a random integer from three integer components.
* The generated value is between 0 (inclusive) and 1 (exclusive) * The generated value is between 0 (inclusive) and 1 (exclusive)
@ -24,8 +38,8 @@ public interface SimpleRandom {
* @return a random integer between 0 (inclusive) and {@code bound} (exclusive) * @return a random integer between 0 (inclusive) and {@code bound} (exclusive)
*/ */
default int nextInt(int x, int y, int z, int bound) { default int nextInt(int x, int y, int z, int bound) {
double val = nextDouble(x, y, z); double val = nextDouble(x, y, z, bound);
return (int) (val * bound); return (int) val;
} }
} }

View File

@ -36,7 +36,7 @@ public class SimpleRandomCollection<E> extends RandomCollection<E> {
@Override @Override
public E next(int x, int y, int z) { public E next(int x, int y, int z) {
return map.ceilingEntry(getRandom().nextDouble(x, y, z) * this.total).getValue(); return map.ceilingEntry(getRandom().nextDouble(x, y, z, this.total)).getValue();
} }
} }