From 03f7d4ecfb272b23519a2d10e8ab5dafe4bdd923 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Thu, 29 Mar 2012 21:06:43 -0700 Subject: [PATCH] Added getChunkCubes() to Region to get 16^3 chunks a region overlaps with --- .../sk89q/worldedit/regions/CuboidRegion.java | 49 +++++++++++- .../worldedit/regions/CylinderRegion.java | 75 ++++++++++++------- .../worldedit/regions/EllipsoidRegion.java | 35 +++++++-- .../worldedit/regions/Polygonal2DRegion.java | 29 +++++-- .../com/sk89q/worldedit/regions/Region.java | 15 +++- 5 files changed, 161 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index 692408e1b..c46de4ae6 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.regions; import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D; @@ -302,11 +303,27 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { Vector min = getMinimumPoint(); Vector max = getMaximumPoint(); + for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) { + for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) { + chunks.add(new BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS, + z >> ChunkStore.CHUNK_SHIFTS)); + } + } + + return chunks; + } + + public Set getChunkCubes() { + Set chunks = new HashSet(); + + Vector min = getMinimumPoint(); + Vector max = getMaximumPoint(); + 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) { - Vector pt = new Vector(x, y, z); - chunks.add(ChunkStore.toChunk(pt)); + chunks.add(new BlockVector(x >> ChunkStore.CHUNK_SHIFTS, + y >> ChunkStore.CHUNK_SHIFTS, z >> ChunkStore.CHUNK_SHIFTS)); } } } @@ -376,7 +393,33 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { return new Iterable() { @Override public Iterator iterator() { - return new FlatRegionIterator(CuboidRegion.this); + return new Iterator() { + private Vector min = getMinimumPoint(); + private Vector max = getMaximumPoint(); + private int nextX = min.getBlockX(); + private int nextZ = min.getBlockZ(); + + public boolean hasNext() { + return (nextX != Integer.MIN_VALUE); + } + + public Vector2D next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + Vector2D answer = new Vector2D(nextX, nextZ); + if (++nextX > max.getBlockX()) { + nextX = min.getBlockX(); + if (++nextZ > max.getBlockZ()) { + nextX = Integer.MIN_VALUE; + } + } + return answer; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; } }; } diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index decb82f65..3c21db4b5 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -22,6 +22,9 @@ package com.sk89q.worldedit.regions; import java.util.HashSet; import java.util.Iterator; import java.util.Set; + +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D; @@ -49,7 +52,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Construct the region. - * + * * @param world */ public CylinderRegion(LocalWorld world) { @@ -59,9 +62,10 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Construct the region. - * + * * @param world - * @param points + * @param center + * @param radius * @param minY * @param maxY */ @@ -81,8 +85,8 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Returns the main center point of the cylinder - * - * @return + * + * @return */ public Vector getCenter() { return center; @@ -90,7 +94,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Sets the main center point of the region - * + * */ public void setCenter(Vector center) { this.center = center; @@ -99,8 +103,8 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Returns the radius of the cylinder - * - * @return + * + * @return */ public Vector2D getRadius() { return radius.subtract(0.5, 0.5); @@ -108,8 +112,8 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Sets the radius of the cylinder - * - * @param radius + * + * @param radius */ public void setRadius(Vector2D radius) { this.radius = radius.add(0.5, 0.5); @@ -117,7 +121,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Extends the radius to be at least the given radius - * + * * @param minRadius */ public void extendRadius(Vector2D minRadius) { @@ -126,7 +130,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Set the minimum Y. - * + * * @param y */ public void setMinimumY(int y) { @@ -136,7 +140,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Se the maximum Y. - * + * * @param y */ public void setMaximumY(int y) { @@ -146,7 +150,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Get the lower point of a region. - * + * * @return min. point */ public Vector getMinimumPoint() { @@ -155,7 +159,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Get the upper point of a region. - * + * * @return max. point */ public Vector getMaximumPoint() { @@ -164,7 +168,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Gets the maximum Y value - * @return + * @return */ public int getMaximumY() { return maxY; @@ -172,7 +176,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Gets the minimum Y value - * @return + * @return */ public int getMinimumY() { return minY; @@ -180,7 +184,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Get the number of blocks in the region. - * + * * @return number of blocks */ public int getArea() { @@ -299,7 +303,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Get a list of chunks. - * + * * @return */ public Set getChunks() { @@ -310,9 +314,30 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) { for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) { - Vector pt = new Vector(x, minY, z); - if (contains(pt)) { - chunks.add(ChunkStore.toChunk(pt)); + if (contains(new BlockVector(x, minY, z))) { + chunks.add(new BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS, + z >> ChunkStore.CHUNK_SHIFTS)); + } + } + } + + return chunks; + } + + @Override + public Set getChunkCubes() { + Set chunks = new HashSet(); + + Vector min = getMinimumPoint(); + Vector max = getMaximumPoint(); + + 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) { + if (contains(new BlockVector(x, y, z))) { + chunks.add(new BlockVector(x >> ChunkStore.CHUNK_SHIFTS, + y >> ChunkStore.CHUNK_SHIFTS, z >> ChunkStore.CHUNK_SHIFTS)); + } } } } @@ -322,8 +347,8 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Sets the height of the cylinder to fit the specified Y. - * - * @param y + * + * @param y * @return true if the area was expanded */ public boolean setY(int y) { @@ -356,7 +381,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { /** * Returns string representation in the format * "(centerX, centerY, centerZ) - (radiusX, radiusZ)" - * + * * @return string */ @Override diff --git a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java index 02181874a..f212f778b 100644 --- a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.regions; +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D; @@ -48,10 +50,10 @@ public class EllipsoidRegion extends AbstractRegion { public EllipsoidRegion(Vector pos1, Vector pos2) { this(null, pos1, pos2); } - + /** * Construct a new instance of this ellipsoid region. - * + * * @param world * @param center * @param radius @@ -198,7 +200,7 @@ public class EllipsoidRegion extends AbstractRegion { /** * Set radiuses. * - * @param radiuses + * @param radius */ public void setRadius(Vector radius) { this.radius = radius.add(0.5, 0.5, 0.5); @@ -218,8 +220,31 @@ public class EllipsoidRegion extends AbstractRegion { 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) { - Vector pt = new Vector(x, y, z); - chunks.add(ChunkStore.toChunk(pt)); + if (contains(new BlockVector(x, y, z))) { + chunks.add(new BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS, + z >> ChunkStore.CHUNK_SHIFTS)); + } + } + } + } + + return chunks; + } + + @Override + public Set getChunkCubes() { + Set chunks = new HashSet(); + + Vector min = getMinimumPoint(); + Vector max = getMaximumPoint(); + + 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) { + if (contains(new BlockVector(x, y, z))) { + chunks.add(new BlockVector(x >> ChunkStore.CHUNK_SHIFTS, + y >> ChunkStore.CHUNK_SHIFTS, z >> ChunkStore.CHUNK_SHIFTS)); + } } } } diff --git a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java index a779f2b68..9dcaa5d41 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -269,7 +269,7 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion { /** * Expand the region. * - * @param change + * @param changes */ public void expand(Vector... changes) throws RegionOperationException { for (Vector change : changes) { @@ -292,7 +292,7 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion { /** * Contract the region. * - * @param change + * @param changes */ public void contract(Vector... changes) throws RegionOperationException { for (Vector change : changes) { @@ -415,12 +415,31 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion { Vector min = getMinimumPoint(); Vector max = getMaximumPoint(); + for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) { + for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) { + if (contains(new BlockVector(x, minY, z))) { // Not the best + chunks.add(new BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS, + z >> ChunkStore.CHUNK_SHIFTS)); + } + } + } + + return chunks; + } + + @Override + public Set getChunkCubes() { + Set chunks = new HashSet(); + + Vector min = getMinimumPoint(); + Vector max = getMaximumPoint(); + 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) { - Vector pt = new Vector(x, y, z); - if (contains(pt)) { // Not the best - chunks.add(ChunkStore.toChunk(pt)); + if (contains(new BlockVector(x, y, z))) { // Not the best + chunks.add(new BlockVector(x >> ChunkStore.CHUNK_SHIFTS, + y >> ChunkStore.CHUNK_SHIFTS, z >> ChunkStore.CHUNK_SHIFTS)); } } } diff --git a/src/main/java/com/sk89q/worldedit/regions/Region.java b/src/main/java/com/sk89q/worldedit/regions/Region.java index 82d7eff74..78c055f0f 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Region.java +++ b/src/main/java/com/sk89q/worldedit/regions/Region.java @@ -130,17 +130,24 @@ public interface Region extends Iterable { * @return */ public Set getChunks(); - + + /** + * Return a list of 16*16*16 chunks in a region + * + * @return The chunk cubes this region overlaps with + */ + public Set getChunkCubes(); + /** * Get the world the selection is in - * + * * @return */ public LocalWorld getWorld(); - + /** * Sets the world the selection is in - * + * * @return */ public void setWorld(LocalWorld world);