diff --git a/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java b/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java index 884271c4..756c5df7 100644 --- a/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java +++ b/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java @@ -20,6 +20,8 @@ import com.sk89q.worldedit.*; import com.sk89q.worldguard.protection.UnsupportedIntersectionException; + +import java.util.ArrayList; import java.util.List; /** @@ -128,7 +130,125 @@ public boolean intersectsWith(ProtectedRegion region) throws UnsupportedIntersec */ public List getIntersectingRegions(List regions) throws UnsupportedIntersectionException { - throw new UnsupportedOperationException("Not supported yet."); + int numRegions = regions.size(); + List intersectingRegions = new ArrayList(); + int i, i2, i3; + + for (i = 0; i < numRegions; i++) { + ProtectedRegion region = regions.get(i); + BlockVector rMinPoint = region.getMinimumPoint(); + BlockVector rMaxPoint = region.getMaximumPoint(); + + // Check whether the region is outside the min and max vector + if ((rMinPoint.getBlockX() < min.getBlockX() && rMaxPoint.getBlockX() < min.getBlockX()) + || (rMinPoint.getBlockX() > max.getBlockX() && rMaxPoint.getBlockX() > max.getBlockX()) + && ((rMinPoint.getBlockY() < min.getBlockY() && rMaxPoint.getBlockY() < min.getBlockY()) + || (rMinPoint.getBlockY() > max.getBlockY() && rMaxPoint.getBlockY() > max.getBlockY())) + && ((rMinPoint.getBlockZ() < min.getBlockZ() && rMaxPoint.getBlockZ() < min.getBlockZ()) + || (rMinPoint.getBlockZ() > max.getBlockZ() && rMaxPoint.getBlockZ() > max.getBlockZ())) ) { + intersectingRegions.add(regions.get(i)); + continue; + } + + // Check whether the regions points are inside the other region + if (region.contains(new Vector(min.getBlockX(), min.getBlockY(), min.getBlockZ())) + || region.contains(new Vector(min.getBlockX(), min.getBlockY(), max.getBlockZ())) + || region.contains(new Vector(min.getBlockX(), max.getBlockY(), max.getBlockZ())) + || region.contains(new Vector(min.getBlockX(), max.getBlockY(), min.getBlockZ())) + || region.contains(new Vector(max.getBlockX(), max.getBlockY(), max.getBlockZ())) + || region.contains(new Vector(max.getBlockX(), max.getBlockY(), min.getBlockZ())) + || region.contains(new Vector(max.getBlockX(), min.getBlockY(), min.getBlockZ())) + || region.contains(new Vector(max.getBlockX(), min.getBlockY(), max.getBlockZ())) ) { + intersectingRegions.add(regions.get(i)); + continue; + } + + // Check whether the other regions points are inside the current region + if (region.getTypeName() == "polygon") { + for (i2 = 0; i < ((ProtectedPolygonalRegion)region).getPoints().size(); i++) { + BlockVector2D pt2Dr = ((ProtectedPolygonalRegion)region).getPoints().get(i2); + int minYr = ((ProtectedPolygonalRegion)region).minY; + int maxYr = ((ProtectedPolygonalRegion)region).maxY; + Vector ptr = new Vector(pt2Dr.getBlockX(), minYr, pt2Dr.getBlockZ()); + Vector ptr2 = new Vector(pt2Dr.getBlockX(), maxYr, pt2Dr.getBlockZ()); + + if (this.contains(ptr) || this.contains(ptr2)) { + intersectingRegions.add(regions.get(i)); + continue; + } + } + } else if (region.getTypeName() == "cuboid") { + BlockVector ptcMin = region.getMinimumPoint(); + BlockVector ptcMax = region.getMaximumPoint(); + + if (this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ())) + || this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ())) + || this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ())) ) { + intersectingRegions.add(regions.get(i)); + continue; + } + } else { + throw new UnsupportedOperationException("Not supported yet."); + } + + // Check whether the current regions edges collide with the regions edges + boolean regionIsIntersecting = false; + List points = new ArrayList(); + points.add(new BlockVector2D(min.getBlockX(), min.getBlockZ())); + points.add(new BlockVector2D(min.getBlockX(), max.getBlockZ())); + points.add(new BlockVector2D(max.getBlockX(), max.getBlockZ())); + points.add(new BlockVector2D(max.getBlockX(), min.getBlockZ())); + + for (i2 = 0; i2 < points.size(); i2++) { + boolean checkNextPoint = false; + BlockVector2D currPoint = points.get(i2); + BlockVector2D nextPoint; + + if (i2 == (points.size() - 1)) { + nextPoint = points.get(0); + } else { + nextPoint = points.get(i2 + 1); + } + + int currX = currPoint.getBlockX(); + int currZ = currPoint.getBlockZ(); + while (!checkNextPoint) { + for(i3 = min.getBlockY(); i3 <= max.getBlockY(); i3++) { + if (region.contains(new Vector(currX, i3, currZ))) { + intersectingRegions.add(regions.get(i)); + regionIsIntersecting = true; + break; + } + } + + if (currX == nextPoint.getBlockX() || currZ == nextPoint.getBlockZ() || regionIsIntersecting) { + checkNextPoint = true; + } + + if (nextPoint.getBlockX() > currPoint.getBlockX()) { + currX++; + } else { + currX--; + } + if (nextPoint.getBlockZ() > currPoint.getBlockZ()) { + currZ++; + } else { + currZ--; + } + } + + if (regionIsIntersecting) { + break; + } + } + } + + return intersectingRegions; } diff --git a/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java b/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java index a493bdc0..7418c6bf 100644 --- a/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java +++ b/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java @@ -19,6 +19,7 @@ package com.sk89q.worldguard.protection.regions; import java.util.List; +import java.util.ArrayList; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.Vector; @@ -64,6 +65,10 @@ public ProtectedPolygonalRegion(String id, List points, int minY, max = new BlockVector(maxX, maxY, maxZ); } + public List getPoints() { + return points; + } + @Override public BlockVector getMinimumPoint() { return min; @@ -134,7 +139,118 @@ public boolean contains(Vector pt) { public List getIntersectingRegions(List regions) throws UnsupportedIntersectionException { - throw new UnsupportedOperationException("Not supported yet."); + int numRegions = regions.size(); + int numPoints = points.size(); + List intersectingRegions = new ArrayList(); + int i, i2, i3; + + for (i = 0; i < numRegions; i++) { + ProtectedRegion region = regions.get(i); + BlockVector rMinPoint = region.getMinimumPoint(); + BlockVector rMaxPoint = region.getMaximumPoint(); + + // Check whether the region is outside the min and max vector + if ((rMinPoint.getBlockX() < min.getBlockX() && rMaxPoint.getBlockX() < min.getBlockX()) + || (rMinPoint.getBlockX() > max.getBlockX() && rMaxPoint.getBlockX() > max.getBlockX()) + && ((rMinPoint.getBlockY() < min.getBlockY() && rMaxPoint.getBlockY() < min.getBlockY()) + || (rMinPoint.getBlockY() > max.getBlockY() && rMaxPoint.getBlockY() > max.getBlockY())) + && ((rMinPoint.getBlockZ() < min.getBlockZ() && rMaxPoint.getBlockZ() < min.getBlockZ()) + || (rMinPoint.getBlockZ() > max.getBlockZ() && rMaxPoint.getBlockZ() > max.getBlockZ())) ) { + intersectingRegions.add(regions.get(i)); + continue; + } + + // Check whether the regions points are inside the other region + for (i2 = 0; i < numPoints; i++) { + Vector pt = new Vector(points.get(i2).getBlockX(), minY, points.get(i2).getBlockZ()); + Vector pt2 = new Vector(points.get(i2).getBlockX(), maxY, points.get(i2).getBlockZ()); + if (region.contains(pt) || region.contains(pt2)) { + intersectingRegions.add(regions.get(i)); + continue; + } + } + + // Check whether the other regions points are inside the current region + if (region.getTypeName() == "polygon") { + for (i2 = 0; i < ((ProtectedPolygonalRegion)region).getPoints().size(); i++) { + BlockVector2D pt2Dr = ((ProtectedPolygonalRegion)region).getPoints().get(i2); + int minYr = ((ProtectedPolygonalRegion)region).minY; + int maxYr = ((ProtectedPolygonalRegion)region).maxY; + Vector ptr = new Vector(pt2Dr.getBlockX(), minYr, pt2Dr.getBlockZ()); + Vector ptr2 = new Vector(pt2Dr.getBlockX(), maxYr, pt2Dr.getBlockZ()); + + if (this.contains(ptr) || this.contains(ptr2)) { + intersectingRegions.add(regions.get(i)); + continue; + } + } + } else if (region.getTypeName() == "cuboid") { + BlockVector ptcMin = region.getMinimumPoint(); + BlockVector ptcMax = region.getMaximumPoint(); + + if (this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ())) + || this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ())) + || this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ())) + || this.contains(new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ())) ) { + intersectingRegions.add(regions.get(i)); + continue; + } + } else { + throw new UnsupportedOperationException("Not supported yet."); + } + + // Check whether the current regions edges collide with the regions edges + boolean regionIsIntersecting = false; + for (i2 = 0; i2 < numPoints; i2++) { + boolean checkNextPoint = false; + BlockVector2D currPoint = points.get(i2); + BlockVector2D nextPoint; + + if (i2 == (numPoints - 1)) { + nextPoint = points.get(0); + } else { + nextPoint = points.get(i2 + 1); + } + + int currX = currPoint.getBlockX(); + int currZ = currPoint.getBlockZ(); + + while (!checkNextPoint) { + for(i3 = this.minY; i3 <= this.maxY; i3++) { + if (region.contains(new Vector(currX, i3, currZ))) { + intersectingRegions.add(regions.get(i)); + regionIsIntersecting = true; + break; + } + } + + if (currX == nextPoint.getBlockX() || currZ == nextPoint.getBlockZ() || regionIsIntersecting) { + checkNextPoint = true; + } + + if (nextPoint.getBlockX() > currPoint.getBlockX()) { + currX++; + } else { + currX--; + } + if (nextPoint.getBlockZ() > currPoint.getBlockZ()) { + currZ++; + } else { + currZ--; + } + } + + if (regionIsIntersecting) { + break; + } + } + } + + return intersectingRegions; }