2010-10-11 08:22:47 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
|
2010-10-11 08:22:47 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
package com.sk89q.worldedit.regions;
|
2010-10-11 08:22:47 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
import com.sk89q.worldedit.BlockVector;
|
2011-12-13 03:20:31 +00:00
|
|
|
import com.sk89q.worldedit.LocalWorld;
|
2010-10-14 08:31:05 +00:00
|
|
|
import com.sk89q.worldedit.Vector;
|
2010-10-28 17:27:30 +00:00
|
|
|
import com.sk89q.worldedit.Vector2D;
|
|
|
|
import com.sk89q.worldedit.data.ChunkStore;
|
2010-10-11 08:22:47 +00:00
|
|
|
import java.util.Iterator;
|
2010-10-28 17:27:30 +00:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.HashSet;
|
2010-10-11 08:22:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @author sk89q
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
2012-01-03 14:57:29 +00:00
|
|
|
public class CuboidRegion extends AbstractRegion {
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* Store the first point.
|
|
|
|
*/
|
2010-10-13 01:03:56 +00:00
|
|
|
private Vector pos1;
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* Store the second point.
|
|
|
|
*/
|
2010-10-13 01:03:56 +00:00
|
|
|
private Vector pos2;
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* Construct a new instance of this cuboid region.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-11 08:22:47 +00:00
|
|
|
* @param pos1
|
|
|
|
* @param pos2
|
|
|
|
*/
|
2010-10-13 01:03:56 +00:00
|
|
|
public CuboidRegion(Vector pos1, Vector pos2) {
|
2011-12-13 03:20:31 +00:00
|
|
|
this(null, pos1, pos2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new instance of this cuboid region.
|
|
|
|
*
|
|
|
|
* @param world
|
|
|
|
* @param pos1
|
|
|
|
* @param pos2
|
|
|
|
*/
|
|
|
|
public CuboidRegion(LocalWorld world, Vector pos1, Vector pos2) {
|
2012-01-03 14:57:29 +00:00
|
|
|
super(world);
|
2010-10-11 08:22:47 +00:00
|
|
|
this.pos1 = pos1;
|
|
|
|
this.pos2 = pos2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the lower point of the cuboid.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return min point
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
2010-10-13 01:03:56 +00:00
|
|
|
public Vector getMinimumPoint() {
|
|
|
|
return new Vector(Math.min(pos1.getX(), pos2.getX()),
|
2010-10-11 08:22:47 +00:00
|
|
|
Math.min(pos1.getY(), pos2.getY()),
|
|
|
|
Math.min(pos1.getZ(), pos2.getZ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the upper point of the cuboid.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return max point
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
2010-10-13 01:03:56 +00:00
|
|
|
public Vector getMaximumPoint() {
|
|
|
|
return new Vector(Math.max(pos1.getX(), pos2.getX()),
|
2010-10-11 08:22:47 +00:00
|
|
|
Math.max(pos1.getY(), pos2.getY()),
|
|
|
|
Math.max(pos1.getZ(), pos2.getZ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of blocks in the region.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return number of blocks
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
2011-02-20 01:44:39 +00:00
|
|
|
public int getArea() {
|
2010-10-13 01:03:56 +00:00
|
|
|
Vector min = getMinimumPoint();
|
|
|
|
Vector max = getMaximumPoint();
|
2010-10-11 08:22:47 +00:00
|
|
|
|
|
|
|
return (int)((max.getX() - min.getX() + 1) *
|
|
|
|
(max.getY() - min.getY() + 1) *
|
|
|
|
(max.getZ() - min.getZ() + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get X-size.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return width
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
|
|
|
public int getWidth() {
|
2010-10-13 01:03:56 +00:00
|
|
|
Vector min = getMinimumPoint();
|
|
|
|
Vector max = getMaximumPoint();
|
2010-10-11 08:22:47 +00:00
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
return (int) (max.getX() - min.getX() + 1);
|
2010-10-11 08:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Y-size.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return height
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
|
|
|
public int getHeight() {
|
2010-10-13 01:03:56 +00:00
|
|
|
Vector min = getMinimumPoint();
|
|
|
|
Vector max = getMaximumPoint();
|
2010-10-11 08:22:47 +00:00
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
return (int) (max.getY() - min.getY() + 1);
|
2010-10-11 08:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Z-size.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return length
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
|
|
|
public int getLength() {
|
2010-10-13 01:03:56 +00:00
|
|
|
Vector min = getMinimumPoint();
|
|
|
|
Vector max = getMaximumPoint();
|
2010-10-11 08:22:47 +00:00
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
return (int) (max.getZ() - min.getZ() + 1);
|
2010-10-11 08:22:47 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 04:41:06 +00:00
|
|
|
/**
|
|
|
|
* Expands the cuboid in a direction.
|
|
|
|
*
|
|
|
|
* @param change
|
|
|
|
*/
|
|
|
|
public void expand(Vector change) {
|
|
|
|
if (change.getX() > 0) {
|
|
|
|
if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) {
|
|
|
|
pos1 = pos1.add(new Vector(change.getX(), 0, 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(change.getX(), 0, 0));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Math.min(pos1.getX(), pos2.getX()) == pos1.getX()) {
|
|
|
|
pos1 = pos1.add(new Vector(change.getX(), 0, 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(change.getX(), 0, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change.getY() > 0) {
|
|
|
|
if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, change.getY(), 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, change.getY(), 0));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Math.min(pos1.getY(), pos2.getY()) == pos1.getY()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, change.getY(), 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, change.getY(), 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change.getZ() > 0) {
|
|
|
|
if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, 0, change.getZ()));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, 0, change.getZ()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, 0, change.getZ()));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, 0, change.getZ()));
|
|
|
|
}
|
|
|
|
}
|
2010-10-25 08:07:10 +00:00
|
|
|
|
2012-01-03 15:13:50 +00:00
|
|
|
recalculate();
|
2010-10-13 04:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contracts the cuboid in a direction.
|
|
|
|
*
|
|
|
|
* @param change
|
|
|
|
*/
|
|
|
|
public void contract(Vector change) {
|
|
|
|
if (change.getX() < 0) {
|
|
|
|
if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) {
|
|
|
|
pos1 = pos1.add(new Vector(change.getX(), 0, 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(change.getX(), 0, 0));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Math.min(pos1.getX(), pos2.getX()) == pos1.getX()) {
|
|
|
|
pos1 = pos1.add(new Vector(change.getX(), 0, 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(change.getX(), 0, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change.getY() < 0) {
|
|
|
|
if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, change.getY(), 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, change.getY(), 0));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Math.min(pos1.getY(), pos2.getY()) == pos1.getY()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, change.getY(), 0));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, change.getY(), 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change.getZ() < 0) {
|
|
|
|
if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, 0, change.getZ()));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, 0, change.getZ()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) {
|
|
|
|
pos1 = pos1.add(new Vector(0, 0, change.getZ()));
|
|
|
|
} else {
|
|
|
|
pos2 = pos2.add(new Vector(0, 0, change.getZ()));
|
|
|
|
}
|
|
|
|
}
|
2010-10-25 08:07:10 +00:00
|
|
|
|
2012-01-03 15:13:50 +00:00
|
|
|
recalculate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recalculate() {
|
2011-12-13 03:20:31 +00:00
|
|
|
pos1 = pos1.clampY(0, world == null ? 127 : world.getMaxY());
|
|
|
|
pos2 = pos2.clampY(0, world == null ? 127 : world.getMaxY());
|
2010-10-13 04:41:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:13:50 +00:00
|
|
|
@Override
|
|
|
|
public void shift(Vector change) throws RegionOperationException {
|
|
|
|
pos1 = pos1.add(change);
|
|
|
|
pos2 = pos2.add(change);
|
|
|
|
|
|
|
|
recalculate();
|
|
|
|
}
|
|
|
|
|
2010-10-13 04:41:06 +00:00
|
|
|
/**
|
|
|
|
* Get position 1.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-13 04:41:06 +00:00
|
|
|
* @return position 1
|
|
|
|
*/
|
|
|
|
public Vector getPos1() {
|
|
|
|
return pos1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set position 1.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-13 04:41:06 +00:00
|
|
|
* @param pos1
|
|
|
|
*/
|
|
|
|
public void setPos1(Vector pos1) {
|
|
|
|
this.pos1 = pos1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get position 2.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-13 04:41:06 +00:00
|
|
|
* @return position 2
|
|
|
|
*/
|
|
|
|
public Vector getPos2() {
|
|
|
|
return pos2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set position 2.
|
|
|
|
*
|
|
|
|
* @param pos2
|
|
|
|
*/
|
|
|
|
public void setPos2(Vector pos2) {
|
|
|
|
this.pos2 = pos2;
|
|
|
|
}
|
|
|
|
|
2010-10-28 17:27:30 +00:00
|
|
|
/**
|
|
|
|
* Get a list of chunks that this region is within.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2010-10-28 17:27:30 +00:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Set<Vector2D> getChunks() {
|
|
|
|
Set<Vector2D> chunks = new HashSet<Vector2D>();
|
|
|
|
|
|
|
|
Vector min = getMinimumPoint();
|
|
|
|
Vector max = getMaximumPoint();
|
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
|
|
|
|
for (int y = min.getBlockY(); y <= max.getBlockY(); ++y) {
|
|
|
|
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
|
2010-10-28 17:27:30 +00:00
|
|
|
Vector pt = new Vector(x, y, z);
|
|
|
|
chunks.add(ChunkStore.toChunk(pt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks;
|
|
|
|
}
|
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Returns true based on whether the region contains the point,
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
*/
|
|
|
|
public boolean contains(Vector pt) {
|
|
|
|
double x = pt.getX();
|
|
|
|
double y = pt.getY();
|
|
|
|
double z = pt.getZ();
|
|
|
|
|
|
|
|
Vector min = getMinimumPoint();
|
|
|
|
Vector max = getMaximumPoint();
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
return x >= min.getBlockX() && x <= max.getBlockX()
|
|
|
|
&& y >= min.getBlockY() && y <= max.getBlockY()
|
|
|
|
&& z >= min.getBlockZ() && z <= max.getBlockZ();
|
|
|
|
}
|
|
|
|
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* Get the iterator.
|
2011-09-24 19:32:03 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return iterator of points inside the region
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
2012-01-03 14:57:29 +00:00
|
|
|
@Override
|
2011-02-20 01:44:39 +00:00
|
|
|
public Iterator<BlockVector> iterator() {
|
2011-02-27 04:30:53 +00:00
|
|
|
return new Iterator<BlockVector>() {
|
|
|
|
private Vector min = getMinimumPoint();
|
|
|
|
private Vector max = getMaximumPoint();
|
|
|
|
private int nextX = min.getBlockX();
|
|
|
|
private int nextY = min.getBlockY();
|
|
|
|
private int nextZ = min.getBlockZ();
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-02-27 04:30:53 +00:00
|
|
|
public boolean hasNext() {
|
|
|
|
return (nextX != Integer.MIN_VALUE);
|
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-02-27 04:30:53 +00:00
|
|
|
public BlockVector next() {
|
|
|
|
if (!hasNext()) throw new java.util.NoSuchElementException();
|
|
|
|
BlockVector answer = new BlockVector(nextX, nextY, nextZ);
|
2011-07-15 07:00:48 +00:00
|
|
|
if (++nextX > max.getBlockX()) {
|
2011-02-27 04:30:53 +00:00
|
|
|
nextX = min.getBlockX();
|
2011-07-15 07:00:48 +00:00
|
|
|
if (++nextY > max.getBlockY()) {
|
2011-02-27 04:30:53 +00:00
|
|
|
nextY = min.getBlockY();
|
2011-07-15 07:00:48 +00:00
|
|
|
if (++nextZ > max.getBlockZ()) {
|
2011-02-27 04:30:53 +00:00
|
|
|
nextX = Integer.MIN_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return answer;
|
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-02-27 04:30:53 +00:00
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
};
|
2010-10-11 08:22:47 +00:00
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2011-08-08 12:39:16 +00:00
|
|
|
/**
|
2011-08-24 22:27:18 +00:00
|
|
|
* Returns string representation in the format
|
|
|
|
* "(minX, minY, minZ) - (maxX, maxY, maxZ)".
|
2011-08-08 12:39:16 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2011-08-24 22:27:18 +00:00
|
|
|
return getMinimumPoint() + " - " + getMaximumPoint();
|
2011-08-08 12:39:16 +00:00
|
|
|
}
|
2010-10-11 08:22:47 +00:00
|
|
|
}
|