Explorar el Código

Fixes in Zone::getInternalBorder and Rectangle::getExternalBorder

smelc hace 7 años
padre
commit
d483f070c7

+ 1 - 0
squidlib-util/src/main/java/squidpony/squidgrid/mapping/DungeonUtility.java

@@ -1537,6 +1537,7 @@ public class DungeonUtility {
         return result;
     }
 
+    // FIXME CH Use Grids instead
     private static boolean hasANeighborNotIn(Coord c, Collection<Coord> others) {
         for (Direction dir : Direction.OUTWARDS) {
             if (!others.contains(c.translate(dir)))

+ 5 - 3
squidlib-util/src/main/java/squidpony/squidgrid/mapping/Rectangle.java

@@ -363,7 +363,7 @@ public interface Rectangle extends Zone {
 
 		@Override
 		public String toString() {
-			return "Room at " + bottomLeft + ", width:" + width + ", height:" + height;
+			return "Rectangle at " + bottomLeft + ", width:" + width + ", height:" + height;
 		}
 
 		// Implementation of Zone:
@@ -464,9 +464,11 @@ public interface Rectangle extends Zone {
 
 		@Override
 		public Collection<Coord> getExternalBorder() {
-			final List<Coord> result = new ArrayList<Coord>((width + height) * 2);
+			final Rectangle extension = extend();
+			final List<Coord> result = new ArrayList<Coord>(
+					(extension.getWidth() + extension.getHeight()) * 2);
 			for (Direction dir : Direction.CARDINALS)
-				Utils.getBorder(this, dir, result);
+				Utils.getBorder(extension, dir, result);
 			return result;
 		}
 

+ 13 - 18
squidlib-util/src/main/java/squidpony/squidgrid/zone/Zone.java

@@ -309,29 +309,24 @@ public interface Zone extends Serializable, Iterable<Coord> {
 		@Override
 		/* Convenience implementation, feel free to override. */
 		public List<Coord> getInternalBorder() {
-			final int sz = size();
-			if (sz <= 1)
-				return getAll();
-			final List<Coord> result = new ArrayList<Coord>(sz);
-			final List<Coord> all = getAll();
-			assert sz == all.size();
-			nextCell: for (int i = 0; i < sz; i++) {
-				final Coord c = all.get(i);
-				for (Direction out : Direction.OUTWARDS) {
-					final Coord neighbor = c.translate(out);
-					if (!contains(neighbor)) {
-						result.add(c);
-						continue nextCell;
-					}
-				}
-			}
-			return result;
+			return size() <= 1 ? getAll() : DungeonUtility.border(getAll(), null);
 		}
 
 		@Override
 		/* Convenience implementation, feel free to override. */
 		public Collection<Coord> getExternalBorder() {
-			return DungeonUtility.border(getAll(), null);
+			final List<Coord> result = new ArrayList<Coord>(size());
+			final List<Coord> internalBorder = getInternalBorder();
+			final int ibsz = internalBorder.size();
+			for (int i = 0; i < ibsz; i++) {
+				final Coord b = internalBorder.get(i);
+				for (Direction dir : Direction.OUTWARDS) {
+					final Coord borderNeighbor = b.translate(dir);
+					if (!contains(borderNeighbor))
+						result.add(borderNeighbor);
+				}
+			}
+			return result;
 		}
 
 		@Override