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
288
289
290
291
292
293
294
295
296
297
|
package ch.asynk.tankontank.engine;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.LinkedList;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
public class PossiblePaths implements Iterable<Vector3>
{
private final Board board;
private Pawn pawn;
private Tile to;
private Orientation lastO;
private List<Tile> stack;
private List<Tile> ctrlTiles;
private List<ArrayList<Tile>> paths;
private List<ArrayList<Tile>> filteredPaths;
private Board.TileCollection tiles;
public PossiblePaths(Board board, int tSize, int stSize, int ftSize, int vectSize)
{
this.board = board;
this.tiles = new TileSet(board, tSize);
this.stack = new ArrayList<Tile>(stSize);
this.ctrlTiles = new ArrayList<Tile>(ftSize);
this.paths = new LinkedList<ArrayList<Tile>>();
this.filteredPaths = new LinkedList<ArrayList<Tile>>();
this.lastO = Orientation.KEEP;
}
public void setLastOrientation(Orientation lastO)
{
this.lastO = lastO;
}
public int init(Pawn pawn, Tile to)
{
this.pawn = pawn;
this.to = to;
clear();
return build();
}
public void clear()
{
for (List<Tile> tiles : this.paths) tiles.clear();
for (List<Tile> tiles : this.filteredPaths) tiles.clear();
this.stack.clear();
this.paths.clear();
this.ctrlTiles.clear();
this.filteredPaths.clear();
this.tiles.clear();
}
public int size()
{
if (ctrlTiles.size() == 0)
return paths.size();
return filteredPaths.size();
}
public boolean contains(Tile tile)
{
return tiles.contains(tile);
}
public void enable(int i, boolean enable)
{
for (Tile tile : tiles)
board.enableOverlayOn(tile, i, enable);
}
private int build()
{
// from and to are not part of the path
Tile from = pawn.getTile();
if (board.distance(from, to) == 1) {
ArrayList<Tile> temp = new ArrayList<Tile>(0);
// temp.add(from);
// temp.add(to);
paths.add(temp);
for (Tile tile : temp) tiles.add(tile);
} else {
// stack.add(from);
findAllPaths(from, pawn.getMovementPoints(), true);
}
// printToErr("paths", paths);
stack.clear();
return paths.size();
}
private void findAllPaths(Tile from, int mvtLeft, boolean roadMarch)
{
Tile moves[] = new Tile[6];
board.setAdjacentTiles(from, moves);
for(int i = 0; i < 6; i++) {
Tile next = moves[i];
if (next == null) continue;
boolean road = next.road(board.getSide(i));
int cost = next.costFrom(pawn, board.getSide(i), road);
int r = (mvtLeft - cost);
if (roadMarch & road) r += pawn.getRoadMarchBonus();
if ((board.distance(next, to) <= r)) {
if (next == to) {
ArrayList<Tile> temp = new ArrayList<Tile>(stack.size() + 1);
for (Tile t: stack)
temp.add(t);
// temp.add(next);
paths.add(temp);
for (Tile tile : temp) tiles.add(tile);
} else {
stack.add(next);
findAllPaths(next, (mvtLeft - cost), (roadMarch & road));
stack.remove(stack.size() - 1);
}
}
}
}
public int toggleCtrlTile(Tile tile)
{
if (ctrlTiles.contains(tile))
ctrlTiles.remove(tile);
else
ctrlTiles.add(tile);
return filterPaths();
}
private int filterPaths()
{
int s = ctrlTiles.size();
tiles.clear();
filteredPaths.clear();
for (ArrayList<Tile> path : paths) {
int ok = 0;
for (Tile filter : ctrlTiles) {
if (path.contains(filter))
ok += 1;
}
if (ok == s) {
if (path.size() == (s + 0)) { // from and to are not part of the path
filteredPaths.clear();
filteredPaths.add(path);
tiles.clear();
for (Tile tile : path) tiles.add(tile);
break;
} else {
filteredPaths.add(path);
for (Tile tile : path) tiles.add(tile);
}
}
}
// printToErr("filteredPaths", filteredPaths);
return filteredPaths.size();
}
public int pathCost(int i)
{
int cost = 0;
boolean roadMarch = true;
Tile prev = null;
for (Tile next : paths.get(i)) {
if (prev != null) {
Orientation o = Orientation.fromMove(next.col, next.row, prev.col, prev.row);
boolean road = next.road(o);
cost += next.costFrom(pawn, o, road);
roadMarch &= road;
}
prev = next;
}
if (roadMarch)
cost -= pawn.getRoadMarchBonus();
if (cost < 1)
cost = 1;
return cost;
}
public int pathSteps(int idx)
{
int steps = 0;
Tile tile = pawn.getTile();
Orientation o = pawn.getOrientation();
for (Tile next : filteredPaths.get(idx)) {
Orientation nextO = Orientation.fromMove(tile.col, tile.row, next.col, next.row);
if (nextO != o) {
steps += 2;
o = nextO;
} else
steps += 1;
tile = next;
}
if (lastO != Orientation.fromMove(tile.col, tile.row, to.col, to.row))
steps += 2;
else
steps +=1;
return steps;
}
@Override
public Iterator<Vector3> iterator()
{
return new Vector3Iterator(pawn, to, lastO, filteredPaths.get(0));
}
private void printToErr(String what, List<ArrayList<Tile>> paths)
{
System.err.println(what + " ("+paths.size()+")");
for (ArrayList<Tile> path : paths) {
System.err.println(" - path");
for(Tile tile : path)
System.err.println(" " + tile);
}
System.err.println();
}
}
class Vector3Iterator implements Iterator<Vector3>
{
private Pawn pawn;
private Tile to;
private Orientation o;
private Orientation lastO;
private Tile tile;
private Vector2 pos = new Vector2();
private Vector3 v = new Vector3();
private int i;
private List<Tile> path;
public Vector3Iterator(Pawn pawn, Tile to, Orientation lastO, List<Tile> path)
{
this.pawn = pawn;
this.to = to;
this.lastO = lastO;
this.path = path;
this.tile = pawn.getTile();
this.o = pawn.getOrientation();
this.v.set(pawn.getPosition().x, pawn.getPosition().y, o.r());
this.i = 0;
}
@Override
public boolean hasNext()
{
if ((tile == to) && (o == lastO))
return false;
return true;
}
@Override
public Vector3 next()
{
if (tile == to) {
v.z = lastO.r();
o = lastO;
return v;
}
Tile nextTile;
if (i < path.size())
nextTile = path.get(i);
else
nextTile = to;
Orientation nextO = Orientation.fromMove(tile.col, tile.row, nextTile.col, nextTile.row);
if (nextO != o) {
v.z = nextO.r();
o = nextO;
return v;
}
pawn.getPosAt(nextTile, pos);
v.x = pos.x;
v.y = pos.y;
tile = nextTile;
i += 1;
return v;
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}
|