2014-07-09 21:14:17 +00:00
|
|
|
package com.sk89q.worldedit.extent.transform;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.boydti.fawe.object.extent.ResettableExtent;
|
|
|
|
import com.boydti.fawe.util.ReflectionUtils;
|
|
|
|
import com.sk89q.jnbt.ByteTag;
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.internal.helper.MCDirections;
|
|
|
|
import com.sk89q.worldedit.math.transform.AffineTransform;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.AbstractProperty;
|
2018-12-23 16:19:33 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
|
|
|
import com.google.common.collect.Sets;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2019-01-09 07:13:44 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector2;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.math.Vector3;
|
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
|
|
|
import com.sk89q.worldedit.registry.state.BooleanProperty;
|
2018-07-05 23:16:52 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.DirectionalProperty;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.util.Direction;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.world.biome.BaseBiome;
|
2018-08-21 17:22:37 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
2018-08-21 17:22:37 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-21 17:22:37 +00:00
|
|
|
import javax.annotation.Nullable;
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2019-01-09 07:13:44 +00:00
|
|
|
import java.util.Set;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
/**
|
|
|
|
* Transforms blocks themselves (but not their position) according to a
|
|
|
|
* given transform.
|
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
public class BlockTransformExtent extends ResettableExtent {
|
|
|
|
private Transform transform;
|
|
|
|
private Transform transformInverse;
|
|
|
|
private int[] BLOCK_ROTATION_BITMASK;
|
|
|
|
private int[][] BLOCK_TRANSFORM;
|
|
|
|
private int[][] BLOCK_TRANSFORM_INVERSE;
|
|
|
|
private int[] ALL = new int[0];
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
public BlockTransformExtent(Extent parent) {
|
|
|
|
this(parent, new AffineTransform());
|
|
|
|
}
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
public BlockTransformExtent(Extent parent, Transform transform) {
|
|
|
|
super(parent);
|
2014-07-09 21:14:17 +00:00
|
|
|
this.transform = transform;
|
2018-08-12 14:03:07 +00:00
|
|
|
this.transformInverse = this.transform.inverse();
|
|
|
|
cache();
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
private List<Direction> getDirections(AbstractProperty property) {
|
|
|
|
if (property instanceof DirectionalProperty) {
|
|
|
|
DirectionalProperty directional = (DirectionalProperty) property;
|
|
|
|
directional.getValues();
|
|
|
|
} else {
|
|
|
|
switch (property.getKey()) {
|
|
|
|
case HALF:
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
case ROTATION:
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
case AXIS:
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
case FACING:
|
2018-06-18 12:51:21 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
case SHAPE:
|
2019-01-09 07:13:44 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
case NORTH:
|
|
|
|
case EAST:
|
|
|
|
case SOUTH:
|
|
|
|
case WEST:
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
return null;
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
2019-01-09 07:13:44 +00:00
|
|
|
// @Override
|
|
|
|
// public BlockState getBlock(BlockVector3 position) {
|
|
|
|
// return transformBlock(super.getBlock(position), false);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public BaseBlock getFullBlock(BlockVector3 position) {
|
|
|
|
// return transformBlock(super.getFullBlock(position), false);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
// public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws WorldEditException {
|
|
|
|
// return super.setBlock(location, transformBlock(block, true));
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * Transform the given block using the given transform.
|
|
|
|
// *
|
|
|
|
// * <p>The provided block is modified.</p>
|
|
|
|
// *
|
|
|
|
// * @param block the block
|
|
|
|
// * @param transform the transform
|
|
|
|
// * @return the same block
|
|
|
|
// */
|
|
|
|
// public static <T extends BlockStateHolder> T transform(T block, Transform transform) {
|
|
|
|
// return transform(block, transform, block);
|
|
|
|
// }
|
|
|
|
|
|
|
|
private static final Set<String> directionNames = Sets.newHashSet("north", "south", "east", "west");
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * Transform the given block using the given transform.
|
|
|
|
// *
|
|
|
|
// * @param block the block
|
|
|
|
// * @param transform the transform
|
|
|
|
// * @param changedBlock the block to change
|
|
|
|
// * @return the changed block
|
|
|
|
// */
|
|
|
|
// private static <T extends BlockStateHolder> T transform(T block, Transform transform, T changedBlock) {
|
|
|
|
// checkNotNull(block);
|
|
|
|
// checkNotNull(transform);
|
|
|
|
//
|
|
|
|
// List<? extends Property> properties = block.getBlockType().getProperties();
|
|
|
|
//
|
|
|
|
// for (Property property : properties) {
|
|
|
|
// if (property instanceof DirectionalProperty) {
|
|
|
|
// Direction value = (Direction) block.getState(property);
|
|
|
|
// if (value != null) {
|
|
|
|
// Vector3 newValue = getNewStateValue((DirectionalProperty) property, transform, value.toVector());
|
|
|
|
// if (newValue != null) {
|
|
|
|
// changedBlock = (T) changedBlock.with(property, Direction.findClosest(newValue, Direction.Flag.ALL));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
|
|
|
|
@Nullable
|
2019-01-09 07:13:44 +00:00
|
|
|
//<<<<<<< HEAD
|
2018-08-12 14:03:07 +00:00
|
|
|
private static Integer getNewStateIndex(Transform transform, List<Direction> directions, int oldIndex) {
|
|
|
|
Direction oldDirection = directions.get(oldIndex);
|
2019-01-09 07:13:44 +00:00
|
|
|
Vector3 oldVector = oldDirection.toVector();
|
|
|
|
Vector3 newVector = transform.apply(oldVector).subtract(transform.apply(Vector3.ZERO)).normalize();
|
2018-08-12 14:03:07 +00:00
|
|
|
int newIndex = oldIndex;
|
2019-01-09 07:13:44 +00:00
|
|
|
double closest = oldVector.normalize().dot(newVector);
|
|
|
|
//=======
|
|
|
|
// private static Vector3 getNewStateValue(DirectionalProperty state, Transform transform, Vector3 oldDirection) {
|
|
|
|
// Vector3 newDirection = transform.apply(oldDirection).subtract(transform.apply(Vector3.ZERO)).normalize();
|
|
|
|
// Vector3 newValue = null;
|
|
|
|
// double closest = -2;
|
|
|
|
//>>>>>>> 399e0ad5... Refactor vector system to be cleaner
|
2014-07-09 21:14:17 +00:00
|
|
|
boolean found = false;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
for (int i = 0; i < directions.size(); i++) {
|
|
|
|
Direction v = directions.get(i);
|
|
|
|
double dot = v.toVector().normalize().dot(newVector);
|
|
|
|
if (dot > closest) {
|
2018-07-17 07:31:07 +00:00
|
|
|
closest = dot;
|
2018-08-12 14:03:07 +00:00
|
|
|
newIndex = i;
|
2018-07-17 07:31:07 +00:00
|
|
|
found = true;
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return newIndex;
|
2014-07-09 21:14:17 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
private void cache() {
|
|
|
|
BLOCK_ROTATION_BITMASK = new int[BlockTypes.size()];
|
|
|
|
BLOCK_TRANSFORM = new int[BlockTypes.size()][];
|
|
|
|
BLOCK_TRANSFORM_INVERSE = new int[BlockTypes.size()][];
|
|
|
|
outer:
|
|
|
|
for (int i = 0; i < BLOCK_TRANSFORM.length; i++) {
|
|
|
|
BLOCK_TRANSFORM[i] = ALL;
|
|
|
|
BLOCK_TRANSFORM_INVERSE[i] = ALL;
|
|
|
|
BlockTypes type = BlockTypes.get(i);
|
|
|
|
int bitMask = 0;
|
|
|
|
for (AbstractProperty property : (Collection<AbstractProperty>) type.getProperties()) {
|
|
|
|
Collection<Direction> directions = getDirections(property);
|
|
|
|
if (directions != null) {
|
|
|
|
BLOCK_TRANSFORM[i] = null;
|
|
|
|
BLOCK_TRANSFORM_INVERSE[i] = null;
|
|
|
|
bitMask |= property.getBitMask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bitMask != 0) {
|
|
|
|
BLOCK_ROTATION_BITMASK[i] = bitMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResettableExtent setExtent(Extent extent) {
|
|
|
|
return super.setExtent(extent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Transform getTransform() {
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTransform(Transform affine) {
|
|
|
|
this.transform = affine;
|
|
|
|
this.transformInverse = this.transform.inverse();
|
|
|
|
cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
private final BlockState transform(BlockState state, int[][] transformArray, Transform transform) {
|
|
|
|
int typeId = state.getInternalBlockTypeId();
|
|
|
|
int[] arr = transformArray[typeId];
|
|
|
|
if (arr == ALL) return state;
|
|
|
|
if (arr == null) {
|
|
|
|
arr = transformArray[typeId] = new int[state.getBlockType().getMaxStateId() + 1];
|
|
|
|
Arrays.fill(arr, -1);
|
|
|
|
}
|
|
|
|
int mask = BLOCK_ROTATION_BITMASK[typeId];
|
|
|
|
int internalId = state.getInternalId();
|
|
|
|
|
|
|
|
int maskedId = internalId & mask;
|
|
|
|
int newMaskedId = arr[maskedId];
|
|
|
|
if (newMaskedId != -1) {
|
2018-08-27 16:56:28 +00:00
|
|
|
return BlockState.getFromInternalId(newMaskedId | (internalId & (~mask)));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
newMaskedId = state.getInternalId();
|
|
|
|
|
|
|
|
BlockTypes type = state.getBlockType();
|
|
|
|
for (AbstractProperty property : (Collection<AbstractProperty>) type.getProperties()) {
|
|
|
|
List<Direction> directions = getDirections(property);
|
|
|
|
if (directions != null) {
|
|
|
|
Integer newIndex = getNewStateIndex(transform, directions, property.getIndex(state.getInternalId()));
|
|
|
|
if (newIndex != null) {
|
|
|
|
newMaskedId = property.modifyIndex(newMaskedId, newIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arr[maskedId] = newMaskedId & mask;
|
2018-08-27 16:56:28 +00:00
|
|
|
return BlockState.getFromInternalId(newMaskedId);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public final BlockState transformFast(BlockState block) {
|
|
|
|
BlockState transformed = transform(block, BLOCK_TRANSFORM, transform);
|
|
|
|
if (block.hasNbtData()) {
|
|
|
|
CompoundTag tag = block.getNbtData();
|
|
|
|
if (tag.containsKey("Rot")) {
|
|
|
|
int rot = tag.asInt("Rot");
|
|
|
|
|
|
|
|
Direction direction = MCDirections.fromRotation(rot);
|
|
|
|
|
|
|
|
if (direction != null) {
|
2019-01-09 07:13:44 +00:00
|
|
|
Vector3 applyAbsolute = transform.apply(direction.toVector());
|
|
|
|
Vector3 applyOrigin = transform.apply(Vector3.ZERO);
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-09 07:13:44 +00:00
|
|
|
Direction newDirection = Direction.findClosest(applyAbsolute.subtract(applyOrigin), Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL);
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
if (newDirection != null) {
|
|
|
|
Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
|
|
|
|
values.put("Rot", new ByteTag((byte) MCDirections.toRotation(newDirection)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
transformed = new BaseBlock(transformed, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final BlockState transformFastInverse(BlockState block) {
|
|
|
|
BlockState transformed = transform(block, BLOCK_TRANSFORM_INVERSE, transformInverse);
|
|
|
|
if (block.hasNbtData()) {
|
|
|
|
CompoundTag tag = block.getNbtData();
|
|
|
|
if (tag.containsKey("Rot")) {
|
|
|
|
int rot = tag.asInt("Rot");
|
|
|
|
|
|
|
|
Direction direction = MCDirections.fromRotation(rot);
|
|
|
|
|
|
|
|
if (direction != null) {
|
2019-01-09 07:13:44 +00:00
|
|
|
Vector3 applyAbsolute = transformInverse.apply(direction.toVector());
|
|
|
|
Vector3 applyOrigin = transformInverse.apply(Vector3.ZERO);
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-09 07:13:44 +00:00
|
|
|
Direction newDirection = Direction.findClosest(applyAbsolute.subtract(applyOrigin), Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL);
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
if (newDirection != null) {
|
|
|
|
Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
|
|
|
|
values.put("Rot", new ByteTag((byte) MCDirections.toRotation(newDirection)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
transformed = new BaseBlock(transformed, tag);
|
|
|
|
}
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getLazyBlock(int x, int y, int z) {
|
|
|
|
return transformFast(super.getLazyBlock(x, y, z));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-09 07:13:44 +00:00
|
|
|
public BlockState getLazyBlock(BlockVector3 position) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return transformFast(super.getLazyBlock(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-09 07:13:44 +00:00
|
|
|
public BlockState getBlock(BlockVector3 position) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return transformFast(super.getBlock(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-09 07:13:44 +00:00
|
|
|
public BaseBiome getBiome(BlockVector2 position) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return super.getBiome(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean setBlock(int x, int y, int z, BlockStateHolder block) throws WorldEditException {
|
|
|
|
return super.setBlock(x, y, z, transformFastInverse((BlockState) block));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-01-09 07:13:44 +00:00
|
|
|
public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws WorldEditException {
|
2018-08-12 14:03:07 +00:00
|
|
|
return super.setBlock(location, transformFastInverse((BlockState) block));
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:36:39 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|