-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapCache.java
267 lines (226 loc) · 6.47 KB
/
MapCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.util.HashMap;
import java.util.LinkedList;
import bc.*;
/*
* Stores the static parts of a planet map
*/
public class MapCache {
private MapInfo[][] map;
private int w; //map width
private int h; //map height
public MapCache(PlanetMap pm) {
w = (int) pm.getWidth();
h = (int) pm.getHeight();
map = new MapInfo[w][h];
Planet planet = pm.getPlanet();
/*
* Create the cache of locations
*/
for (int x = 0; x<w; x++) {
for (int y=0; y<h; y++) {
MapLocation m = new MapLocation(planet, x, y);
map[x][y] = new MapInfo(m, (pm.isPassableTerrainAt(m) > 0));
}
}
/*
* Now cache all the neighbours of each location and a subset of that (passable locations) for quick access later
*/
for (int x = 0; x<w; x++) {
for (int y=0; y<h; y++) {
MapLocation here = map[x][y].here;
map[x][y].neighbours = allNeighboursOf(here);
map[x][y].passableNeighbours = allPassableNeighbours(here);
}
}
}
public int width() {
return w;
}
public int height() {
return h;
}
public MapLocation loc(int x, int y) {
return map[x][y].here;
}
public LinkedList<MapLocation> neighbours(int x, int y) {
return map[x][y].neighbours;
}
public LinkedList<MapLocation> neighbours(MapLocation m) {
return map[m.getX()][m.getY()].neighbours;
}
public LinkedList<MapLocation> passableNeighbours(int x, int y) {
return map[x][y].passableNeighbours;
}
public LinkedList<MapLocation> passableNeighbours(MapLocation m) {
return map[m.getX()][m.getY()].passableNeighbours;
}
public boolean passable(int x, int y) {
return map[x][y].passable;
}
public boolean passable(MapLocation m) {
return map[m.getX()][m.getY()].passable;
}
public int zone(int x, int y) {
return map[x][y].zone;
}
public int zone(MapLocation m) {
return map[m.getX()][m.getY()].zone;
}
public void setZone(int x, int y, int z) {
map[x][y].zone = z;
}
public void setZone(MapLocation m, int z) {
map[m.getX()][m.getY()].zone = z;
}
private LinkedList<MapLocation> allNeighboursOf(MapLocation centre) {
LinkedList<MapLocation> result = new LinkedList<MapLocation>();
int cx = centre.getX(), cy = centre.getY();
if (cx > 0) {
result.add(map[cx-1][cy].here);
if (cy > 0)
result.add(map[cx-1][cy-1].here);
if (cy+1 < h)
result.add(map[cx-1][cy+1].here);
}
if (cy > 0)
result.add(map[cx][cy-1].here);
if (cy+1 < h)
result.add(map[cx][cy+1].here);
if (cx+1 < w) {
result.add(map[cx+1][cy].here);
if (cy > 0)
result.add(map[cx+1][cy-1].here);
if (cy+1 < h)
result.add(map[cx+1][cy+1].here);
}
return result;
}
/*
* Returns an array containing all the passable neighbours of a map location
* Passable means on the map and not water
*
* Only called by MapInfo to create and cache the results for each location on the map
*/
private LinkedList<MapLocation> allPassableNeighbours(MapLocation l) {
LinkedList<MapLocation> result = new LinkedList<MapLocation>();
for (MapLocation test:map[l.getX()][l.getY()].neighbours) {
if (map[test.getX()][test.getY()].passable)
result.add(test);
}
return result;
}
public LinkedList<MapLocation> allLocationsWithin(MapLocation centre, long min, long max) {
LinkedList<MapLocation> result = new LinkedList<MapLocation>();
int cx = centre.getX(), cy = centre.getY();
Range r = new Range((int)min, (int)max);
if (map[cx][cy].cache.containsKey(r)) {
return map[cx][cy].cache.get(r);
}
if (min < 0)
result.add(centre);
//The tiles in the resultant circle will be 4 way symmetric along the diagonals and vertices
//and 8 way symmetric in other areas
//First the horizontal/vertical
for (int x=1; x*x<=max; x++) {
if (x*x <= min)
continue;
if (cx+x < w)
result.add(map[cx+x][cy].here);
if (cy+x < h)
result.add(map[cx][cy+x].here);
if (cx-x >= 0)
result.add(map[cx-x][cy].here);
if (cy-x >= 0)
result.add(map[cx][cy-x].here);
}
//Now the diagonals
for (int x=1; 2*x*x<=max; x++) {
if (2*x*x <= min)
continue;
if (cx+x < w) {
if (cy+x < h)
result.add(map[cx+x][cy+x].here);
if (cy-x >= 0)
result.add(map[cx+x][cy-x].here);
}
if (cx-x >= 0) {
if (cy+x < h)
result.add(map[cx-x][cy+x].here);
if (cy-x >= 0)
result.add(map[cx-x][cy-x].here);
}
}
//Finally the 8 way symmetry
for (int x=2; x*x+1<=max; x++) {
if (x*x+1 <= min)
continue;
for (int y=1; y<x && x*x+y*y<=max; y++) {
if (x*x+y*x<=min)
continue;
if (cx+x < w) {
if (cy+y < h)
result.add(map[cx+x][cy+y].here);
if (cy-y >= 0)
result.add(map[cx+x][cy-y].here);
}
if (cx-y >= 0) {
if (cy+x < h)
result.add(map[cx-y][cy+x].here);
if (cy-x >= 0)
result.add(map[cx-y][cy-x].here);
}
if (cx-x >= 0) {
if (cy-y >= 0)
result.add(map[cx-x][cy-y].here);
if (cy+y < h)
result.add(map[cx-x][cy+y].here);
}
if (cx+y < w) {
if (cy-x >= 0)
result.add(map[cx+y][cy-x].here);
if (cy+x < h)
result.add(map[cx+y][cy+x].here);
}
}
}
map[cx][cy].cache.put(r, result);
return result;
}
private class MapInfo {
public MapLocation here;
public boolean passable;
public int zone; //The zone number we are part of
public LinkedList<MapLocation> neighbours;
public LinkedList<MapLocation> passableNeighbours;
public HashMap<Range, LinkedList<MapLocation>> cache;
public MapInfo(MapLocation mapLocation, boolean p) {
here = mapLocation;
passable = p;
neighbours = null;
passableNeighbours = null;
zone = 0;
cache = new HashMap<Range, LinkedList<MapLocation>>();
}
}
private class Range {
private int min;
private int max;
public Range(int a, int b) {
min = a;
max = b;
}
@Override
public int hashCode() {
return min+max*100;
}
@Override
public boolean equals(Object other) {
if (other == this)
return true;
if (other == null)
return false;
Range r = (Range)other;
return (r.min == min && r.max == max);
}
}
}