1: package siege.core;
2:
3: import java.awt.Dimension;
4: import java.awt.Point;
5: import java.util.HashSet;
6: import java.util.LinkedList;
7:
8: 9: 10: 11:
12: public class TerritoryMap
13: {
14: public PlayboardPoint[][] map;
15: public Territory territory;
16:
17: public Point minimum;
18: public Point maximum;
19: public Dimension mapSize;
20:
21: HashSet<Point> stackToExplore = new HashSet<Point>();
22: HashSet<Point> stackExplored = new HashSet<Point>();
23:
24: public TerritoryMap(Territory territory)
25: {
26: this.territory = territory;
27: createMap();
28: }
29:
30: 31: 32:
33: public void createMap()
34: {
35: computeLengths();
36:
37: map = new PlayboardPoint[mapSize.height][mapSize.width];
38:
39: 40: PlayboardPoint[][] grid = territory.getPlayboard().getGrid();
41: for(int y = 0; y < map.length; y++) {
42: for(int x = 0; x < map[0].length; x++) {
43: PlayboardPoint gp = grid[minimum.y+y][minimum.x+x];
44: map[y][x] = gp;
45: }
46: }
47: }
48:
49: 50: 51: 52:
53: public void computeLengths()
54: {
55: LinkedList<PlayboardPoint> borderLine = territory.getBorderLine();
56:
57: int widthMax = borderLine.getFirst().getX();
58: int widthMin = widthMax;
59: int heightMax = borderLine.getFirst().getY();
60: int heightMin = heightMax;
61:
62: for (PlayboardPoint point : borderLine) {
63: int x = point.getX();
64: if (widthMax < x) {
65: widthMax = x;
66: } else if (widthMin > x) {
67: widthMin = x;
68: }
69:
70: int y = point.getY();
71: if (heightMax < y) {
72: heightMax = y;
73: } else if (heightMin > y) {
74: heightMin = y;
75: }
76: }
77:
78: maximum = new Point(widthMax, heightMax);
79: minimum = new Point(widthMin, heightMin);
80: mapSize = new Dimension(maximum.x-minimum.x+1, maximum.y-minimum.y+1);
81: }
82:
83: 84: 85:
86: public void removeOuterPoints()
87: {
88: stackToExplore = new HashSet<Point>(mapSize.width*mapSize.height/2);
89: stackExplored = new HashSet<Point>(stackToExplore.size());
90:
91: 92: for (int y = 0; y < map.length; y++) {
93:
94: if( (y == 0) || (y == (map.length-1)) ) {
95: 96:
97: 98: for(int x = 0; x < map[0].length; x++) {
99: Point point = new Point(x,y);
100: if(isExplorable(point)) {
101: stackToExplore.add(point);
102: }
103: }
104:
105: } else {
106: 107:
108: 109: Point point = new Point(0,y);
110: if(isExplorable(point)) {
111: stackToExplore.add(point);
112: }
113: 114: point = new Point(map[0].length-1,y);
115: if(isExplorable(point)) {
116: stackToExplore.add(point);
117: }
118:
119: }
120:
121: }
122:
123: 124: while(stackToExplore.size() > 0) {
125: Point current = stackToExplore.iterator().next();
126:
127: addSurroundingPointsToSearchStack(current);
128:
129: stackToExplore.remove(current);
130: stackExplored.add(current);
131: removePoint(current);
132: }
133: }
134:
135: public boolean isExplorable(Point point)
136: {
137: boolean result = true;
138:
139: PlayboardPoint playboardPoint = getPlayboardPoint(point);
140: if(playboardPoint == null) {
141: result = false;
142: } else if(playboardPoint.hasParentTerritory(territory, PointStatus.BORDER)) {
143: result = false;
144: } else if(stackExplored.contains(playboardPoint)) {
145: result = false;
146: }
147:
148: return result;
149: }
150:
151: public void removePoint(Point point)
152: {
153: map[point.y][point.x] = null;
154: }
155:
156: public PlayboardPoint getPlayboardPoint(Point point)
157: {
158: if(isValidCoordinate(point) == false) {
159: throw new IllegalArgumentException("Given point has invalid coordinates (within map)");
160: }
161: return map[point.y][point.x];
162: }
163:
164: public boolean isValidCoordinate(Point point)
165: {
166: boolean result = false;
167: if( (point.y >= 0 && point.y < map.length) &&
168: (point.x >= 0 && point.x < map[0].length) ) {
169: result = true;
170: }
171: return result;
172: }
173:
174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189:
190: public boolean hasTerritoryObstacle(Point surrounding, Point current)
191: {
192: Point offsetFromCurrent = new Point(-1*(current.x-surrounding.x), -1*(current.y-surrounding.y));
193: if(offsetFromCurrent.x == 0 || offsetFromCurrent.y == 0) {
194: throw new IllegalArgumentException("Given point must be surrounding corner point");
195: } else if (Math.abs(offsetFromCurrent.x) > 1 || Math.abs(offsetFromCurrent.y) > 1) {
196: throw new IllegalArgumentException("Given point must be surrounding point (absolute distance is 1 in maximum)");
197: } else if (getPlayboardPoint(current).hasParentTerritory(territory, PointStatus.BORDER)) {
198: throw new IllegalArgumentException("Given point must not be part of territory borderline (of territory for which is map created)");
199: }
200: this.dump();
201: PlayboardPoint p1 = getPlayboardPoint(new Point(surrounding.x-offsetFromCurrent.x, surrounding.y));
202: PlayboardPoint p2 = getPlayboardPoint(new Point(surrounding.x, surrounding.y-offsetFromCurrent.y));
203: if(p1 == null || p2 == null) {
204: return false;
205: }
206: return (p1.hasParentTerritory(territory, PointStatus.BORDER) && p2.hasParentTerritory(territory, PointStatus.BORDER));
207: }
208:
209: public void addSurroundingPointsToSearchStack(Point current)
210: {
211: for(int y = -1; y <= 1; y++) {
212: for(int x = -1; x <= 1; x++) {
213:
214: 215: if(x == 0 && y == 0) {
216: continue;
217: }
218:
219: Point offset = new Point(x,y);
220: Point surrounding = new Point(current.x+offset.x, current.y+offset.y);
221:
222: 223: if(isValidCoordinate(surrounding) == false ) {
224: continue;
225: }
226:
227: if(isExplorable(surrounding) == false) {
228: continue;
229: }
230:
231: 232: 233: 234: 235: 236: 237: 238: 239: 240:
241: if(offset.x == 0 || offset.y == 0) {
242: stackToExplore.add(surrounding);
243: } else {
244: if (hasTerritoryObstacle(surrounding, current) == false) {
245: stackToExplore.add(surrounding);
246: }
247: }
248: }
249: }
250: }
251:
252: public HashSet<PlayboardPoint> getTerritoryPoints()
253: {
254: HashSet<PlayboardPoint> points = new HashSet<PlayboardPoint>(map[0].length*map.length);
255: for (int y = 0; y < map.length; y++) {
256: for (int x = 0; x < map[0].length; x++) {
257: PlayboardPoint point = getPlayboardPoint(new Point(x,y));
258: if(point != null) {
259: points.add(point);
260: }
261: }
262: }
263: return points;
264: }
265:
266: public void dump()
267: {
268: System.out.println("---");
269: for (int y = 0; y < map.length; y++) {
270: for (int x = 0; x < map[0].length; x++) {
271: PlayboardPoint playboardPoint = getPlayboardPoint(new Point(x,y));
272: if(playboardPoint == null) {
273: System.out.print(" N ");
274: } else if (playboardPoint.hasParentTerritory(territory, PointStatus.BORDER)) {
275: System.out.print(" T ");
276: } else if (playboardPoint.hasParentTerritory(territory)) {
277: System.out.print(" t ");
278: } else {
279: System.out.print(" . ");
280: }
281: }
282: System.out.println("\n");
283: }
284: }
285: }
286: