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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
package ch.asynk.tankontank.engine;
import java.util.List;
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Vector;
public class SearchBoard
{
public class Node
{
public int col;
public int row;
public int search;
public int remaining;
public Node parent;
public boolean roadMarch;
public Node(int col, int row)
{
this.col = col;
this.row = row;
}
}
private int cols;
private int rows;
private Board board;
private Node nodes[];
private int searchCount;
private LinkedList<Node> queue;
private ArrayDeque<Node> stack;
private ArrayDeque<Node> roadMarch;
private List<Node> result;
private Node adjacents[];
private Board.Orientation directions[];
public SearchBoard(Board board, int cols, int rows)
{
this.cols = cols;
this.rows = rows;
this.board = board;
this.nodes = new Node[cols * rows];
for (int j = 0; j < rows; j++) {
int dx = ((j + 1) / 2);
for (int i = 0; i < cols; i++)
nodes[i + (j * cols)] = new Node((i + dx), j);
}
this.searchCount = 0;
this.queue = new LinkedList<Node>();
this.stack = new ArrayDeque<Node>(20);
this.roadMarch = new ArrayDeque<Node>(5);
this.result = new Vector<Node>(10);
this.adjacents = new Node[6];
this.directions = new Board.Orientation[6];
directions[0] = Board.Orientation.NORTH;
directions[1] = Board.Orientation.NORTH_EAST;
directions[2] = Board.Orientation.SOUTH_EAST;
directions[3] = Board.Orientation.SOUTH;
directions[4] = Board.Orientation.SOUTH_WEST;
directions[5] = Board.Orientation.NORTH_WEST;
}
private Node getNode(int col, int row)
{
int colOffset = ((row +1) / 2);
if ((col < colOffset) || (row < 0) || (row >= rows) || ((col - colOffset) >= cols))
return null;
return nodes[((col - ((row + 1) / 2))) + (row * cols)];
}
public void adjacentMoves(Node src)
{
// move to enter dst by directions[i]
adjacents[0] = getNode((src.col - 1), src.row);
adjacents[1] = getNode(src.col, (src.row + 1));
adjacents[2] = getNode((src.col + 1), (src.row + 1));
adjacents[3] = getNode((src.col + 1), src.row);
adjacents[4] = getNode(src.col, (src.row - 1));
adjacents[5] = getNode((src.col - 1), (src.row - 1));
}
public List<Node> reachableFrom(Pawn pawn, int col, int row)
{
searchCount += 1;
result.clear();
Node start = getNode(col, row);
start.parent = null;
start.search = searchCount;
start.remaining = pawn.getMovementPoints();
start.roadMarch = true;
if (start.remaining <= 0)
return result;
int roadMarchBonus = pawn.getRoadMarchBonus();
boolean first = true;
stack.push(start);
while (stack.size() != 0) {
Node src = stack.pop();
if (src.remaining <= 0) {
if (src.roadMarch) {
src.remaining = roadMarchBonus;
roadMarch.push(src);
}
continue;
}
adjacentMoves(src);
for(int i = 0; i < 6; i++) {
Node dst = adjacents[i];
if (dst != null) {
Tile t = board.getTile(dst.col, dst.row);
boolean road = t.road(directions[i]);
int cost = t.costFrom(pawn, directions[i], road);
boolean mayMoveOne = first && t.atLeastOneMove(pawn);
int r = src.remaining - cost;
boolean roadMarch = road && src.roadMarch;
if (dst.search == searchCount) {
if ((r >= 0) && ((r > dst.remaining) || (roadMarch && ((r + roadMarchBonus) >= dst.remaining)))) {
dst.remaining = r;
dst.parent = src;
dst.roadMarch = roadMarch;
}
} else {
dst.search = searchCount;
if ((r >= 0) || mayMoveOne) {
dst.parent = src;
dst.remaining = r;
dst.roadMarch = roadMarch;
stack.push(dst);
result.add(dst);
} else {
dst.parent = null;
dst.remaining = Integer.MAX_VALUE;
}
}
}
}
first = false;
}
while(roadMarch.size() != 0) {
Node src = roadMarch.pop();
adjacentMoves(src);
for(int i = 0; i < 6; i++) {
Node dst = adjacents[i];
if (dst != null) {
Tile t = board.getTile(dst.col, dst.row);
if (!t.road(directions[i]))
continue;
int cost = t.costFrom(pawn, directions[i], true);
int r = src.remaining - cost;
if (dst.search == searchCount) {
if ((r >= 0) && (r > dst.remaining)) {
dst.remaining = r;
dst.parent = src;
dst.roadMarch = true;
}
} else {
dst.search = searchCount;
if (r >= 0) {
dst.parent = src;
dst.remaining = r;
dst.roadMarch = true;
roadMarch.push(dst);
result.add(dst);
} else {
dst.parent = null;
dst.remaining = Integer.MAX_VALUE;
}
}
}
}
}
return result;
}
private void adjacentAttacks(Node src, int angle)
{
// move in allowed directions
if (Board.Orientation.NORTH.isInSides(angle))
adjacents[0] = getNode((src.col + 1), src.row);
else
adjacents[0] = null;
if (Board.Orientation.NORTH_EAST.isInSides(angle))
adjacents[1] = getNode(src.col, (src.row - 1));
else
adjacents[1] = null;
if (Board.Orientation.SOUTH_EAST.isInSides(angle))
adjacents[2] = getNode((src.col - 1), (src.row - 1));
else
adjacents[2] = null;
if (Board.Orientation.SOUTH.isInSides(angle))
adjacents[3] = getNode((src.col - 1), src.row);
else
adjacents[3] = null;
if (Board.Orientation.SOUTH_WEST.isInSides(angle))
adjacents[4] = getNode(src.col, (src.row + 1));
else
adjacents[4] = null;
if (Board.Orientation.NORTH_WEST.isInSides(angle))
adjacents[5] = getNode((src.col + 1), (src.row + 1));
else
adjacents[5] = null;
}
private boolean hasClearLineOfSight(Tile from, Tile to)
{
// FIXME
return true;
}
public List<Node> openToAttackFrom(Pawn pawn, int col, int row)
{
searchCount += 1;
result.clear();
Tile tile = board.getTile(col, row);
int range = pawn.getAttackRangeFrom(tile);
int angle = pawn.getAngleOfAttack();
int extendedAngle = pawn.getOrientation().opposite().allBut();
Node start = getNode(col, row);
start.search = searchCount;
start.remaining = range;
if (range <= 0)
return result;
queue.add(start);
boolean first = true;
while (queue.size() != 0) {
Node src = queue.remove();
if (src.remaining <= 0)
continue;
if (!first && (((range - src.remaining) % 2) == 0))
adjacentAttacks(src, extendedAngle);
else
adjacentAttacks(src, angle);
first = false;
int rangeLeft = src.remaining - 1;
for(int i = 0; i < 6; i++) {
Node dst = adjacents[i];
if (dst != null) {
if (dst.search == searchCount) {
if ((rangeLeft > dst.remaining))
dst.remaining = rangeLeft;
} else {
dst.search = searchCount;
dst.remaining = rangeLeft;
queue.add(dst);
Tile t = board.getTile(dst.col, dst.row);
if (t.hasTargetsFor(pawn) && hasClearLineOfSight(tile, t)) result.add(dst);
}
}
}
}
return result;
}
}
|