-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Radius.tsx
417 lines (379 loc) · 11 KB
/
Radius.tsx
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import FastPriorityQueue from 'fastpriorityqueue';
import { Skill } from './info/Skill.tsx';
import { Bridge, TileInfo, TileTypes, TransitionCost } from './info/Tile.tsx';
import { UnitInfo } from './info/Unit.tsx';
import canAccessBridge from './lib/canAccessBridge.tsx';
import canLoad from './lib/canLoad.tsx';
import getVectorRadius from './lib/getVectorRadius.tsx';
import { EntityType } from './map/Entity.tsx';
import Unit from './map/Unit.tsx';
import vec from './map/vec.tsx';
import Vector from './map/Vector.tsx';
import MapData from './MapData.tsx';
type RadiusConfiguration = {
getCost(map: MapData, unit: Unit, vector: Vector): number;
getResourceValue(unit: Unit): number;
getTransitionCost(
info: UnitInfo,
current: TileInfo,
parent: TileInfo,
): number;
isAccessible(map: MapData, unit: Unit, vector: Vector): boolean;
};
export type RadiusItem = Readonly<{
cost: number;
parent: Vector | null;
vector: Vector;
}>;
export const RadiusItem = (
vector: Vector,
cost: number = 0,
parent?: Vector | null,
) => ({
cost,
parent: parent && !vector.equals(parent) ? parent : null,
vector,
});
function isAccessibleBase(map: MapData, unit: Unit, vector: Vector) {
if (!map.contains(vector)) {
return false;
}
const unitB = map.units.get(vector);
if (unitB && map.isOpponent(unitB, unit)) {
return false;
}
const building = map.buildings.get(vector);
if (building && !building.info.isAccessibleBy(unit.info)) {
return false;
}
return true;
}
export const MoveConfiguration = {
getCost: (map: MapData, unit: Unit, vector: Vector) => {
const tile = map.maybeGetTileInfo(vector);
const movementCost = tile?.getMovementCost(unit.info);
return tile &&
movementCost != null &&
movementCost !== -1 &&
canAccessBridge(map, unit.info, vector, tile)
? movementCost
: -1;
},
getResourceValue: (unit: Unit) => unit.fuel,
getTransitionCost: (
info: UnitInfo,
current: TileInfo,
parent: TileInfo,
): number => {
if (current.group === parent.group) {
return 0;
}
const parentCost = parent.getTransitionCost(info);
const currentCost = current.getTransitionCost(info);
const maybeCancelTransitionCost =
parentCost === TransitionCost.Cancel ||
currentCost === TransitionCost.Cancel;
if (maybeCancelTransitionCost) {
return (parentCost === TransitionCost.Cancel &&
currentCost === TransitionCost.Cancel) ||
parent.group === Bridge.group ||
current.group === Bridge.group
? 0
: Number.POSITIVE_INFINITY;
}
return parentCost + currentCost;
},
isAccessible: isAccessibleBase,
} as const;
const VisionConfiguration = {
getCost: (map: MapData, unit: Unit, vector: Vector) =>
map.maybeGetTileInfo(vector)?.configuration.vision || -1,
getResourceValue: () => Number.POSITIVE_INFINITY,
getTransitionCost: () => 0,
isAccessible: (map: MapData, unit: Unit, vector: Vector) =>
map.contains(vector),
} as const;
function calculateRadius(
map: MapData,
unit: Unit,
start: Vector,
radius: number,
{
getCost,
getResourceValue,
getTransitionCost,
isAccessible,
}: RadiusConfiguration = MoveConfiguration,
): Map<Vector, RadiusItem> {
const { info } = unit;
const closed = new Array(map.size.width * map.size.height);
const paths = new Map<Vector, RadiusItem>();
const queue = new FastPriorityQueue<RadiusItem>((a, b) => a.cost < b.cost);
queue.add(RadiusItem(start));
while (!queue.isEmpty()) {
const { cost: parentCost, vector } = queue.poll()!;
const index = map.getTileIndex(vector);
if (closed[index]) {
continue;
}
closed[index] = true;
const vectors = vector.adjacent();
for (let i = 0; i < vectors.length; i++) {
const currentVector = vectors[i];
if (!map.contains(currentVector)) {
continue;
}
const currentIndex = map.getTileIndex(currentVector);
if (closed[currentIndex]) {
continue;
}
const cost = getCost(map, unit, currentVector);
if (cost < 0 || !isAccessible(map, unit, currentVector)) {
closed[currentIndex] = true;
continue;
}
const nextCost =
parentCost +
cost +
getTransitionCost(
info,
map.getTileInfo(vector),
map.getTileInfo(currentVector),
);
const previousPath = paths.get(currentVector);
if (
nextCost <= radius &&
(!previousPath || nextCost < previousPath.cost) &&
nextCost <= getResourceValue(unit)
) {
const item = {
cost: nextCost,
parent: vector,
vector: currentVector,
};
paths.set(currentVector, item);
if (nextCost < radius) {
queue.add(item);
}
}
}
}
return paths;
}
export function moveable(
map: MapData,
unit: Unit,
start: Vector,
radius: number = unit.info.getRadiusFor(map.getPlayer(unit)),
configuration: RadiusConfiguration = MoveConfiguration,
withStart = false,
): ReadonlyMap<Vector, RadiusItem> {
const moveable = calculateRadius(map, unit, start, radius, configuration);
if (withStart) {
moveable.set(start, RadiusItem(start));
}
return moveable;
}
export function getPathCost(
map: MapData,
unit: Unit,
start: Vector,
path: ReadonlyArray<Vector>,
radius: number = unit.info.getRadiusFor(map.getPlayer(unit)),
{
getCost,
getResourceValue,
getTransitionCost,
isAccessible,
}: RadiusConfiguration = MoveConfiguration,
) {
const { info } = unit;
const seen = new Set([start]);
let previousVector = start;
let totalCost = 0;
for (const vector of path) {
if (seen.has(vector) || !map.contains(vector)) {
return -1;
}
seen.add(vector);
if (previousVector.distance(vector) > 1) {
return -1;
}
const cost = getCost(map, unit, vector);
if (cost < 0 || !isAccessible(map, unit, vector)) {
return -1;
}
totalCost +=
cost +
getTransitionCost(
info,
map.getTileInfo(vector),
map.getTileInfo(previousVector),
);
if (totalCost > radius || totalCost > getResourceValue(unit)) {
return -1;
}
previousVector = vector;
}
const unitB = map.units.get(previousVector);
return !unitB || canLoad(map, unitB, unit, previousVector) ? totalCost : -1;
}
export function visible(
map: MapData,
unit: Unit,
start: Vector,
radius: number = unit.info.configuration.vision,
): ReadonlyMap<Vector, RadiusItem> {
const vision =
radius +
(unit.isUnfolded() ? 2 : 0) +
(unit.info.type === EntityType.Soldier &&
map.getTileInfo(start).type & TileTypes.Mountain
? 1
: 0);
const visible = calculateRadius(
map,
unit,
start,
vision,
VisionConfiguration,
);
const player = map.getPlayer(unit);
const canSeeHiddenFields =
player.activeSkills.size &&
player.activeSkills.has(Skill.UnitInfantryForestAttackAndDefenseIncrease);
for (const [vector] of visible) {
if (
!canSeeHiddenFields &&
vector.distance(start) > 1 &&
map.getTileInfo(vector).style.hidden
) {
visible.delete(vector);
}
}
for (const vector of start.expand()) {
if (map.contains(vector)) {
visible.set(vector, RadiusItem(vector));
}
}
return visible;
}
export function attackable(
map: MapData,
unit: Unit,
start: Vector,
optimize: 'cost' | 'cover',
radius?: number,
): ReadonlyMap<Vector, RadiusItem> {
const player = map.getPlayer(unit);
if (radius == null) {
radius = unit.info.getRadiusFor(player);
}
const { info } = unit;
const attackable = new Map<Vector, RadiusItem>();
if (!info.hasAttack()) {
return attackable;
}
const range = info.getRangeFor(player);
if (info.isLongRange() && range) {
const [low, high] = range;
for (let x = 0; x <= high; x++) {
for (let y = 0; y <= high - x; y++) {
const v1 = vec(start.x + x, start.y + y);
if (start.distance(v1) >= low) {
const s2 = { x: start.x + x, y: start.y - y };
const v2 = map.contains(s2) && vec(s2.x, s2.y);
const s3 = { x: start.x - x, y: start.y + y };
const v3 = map.contains(s3) && vec(s3.x, s3.y);
const s4 = { x: start.x - x, y: start.y - y };
const v4 = map.contains(s4) && vec(s4.x, s4.y);
if (map.contains(v1)) {
attackable.set(v1, RadiusItem(v1));
}
if (v2) {
attackable.set(v2, RadiusItem(v2));
}
if (v3) {
attackable.set(v3, RadiusItem(v3));
}
if (v4) {
attackable.set(v4, RadiusItem(v4));
}
}
}
}
}
if (info.isShortRange() || info.canAttackAt(1, range)) {
for (const currentVector of start.adjacent()) {
if (map.contains(currentVector)) {
attackable.set(
currentVector,
RadiusItem(
currentVector,
map.getTileInfo(currentVector).configuration.cover,
start,
),
);
}
}
if (unit.canMove()) {
const moveable = calculateRadius(map, unit, start, radius);
for (const [, parent] of moveable) {
const parentCost =
optimize === 'cover'
? -map.getTileInfo(parent.vector).configuration.cover
: parent.cost;
const unitB = map.units.get(parent.vector);
// If there is a unit that you own, and it hasn't moved yet,
// you can move it out of the way to make the fields around it attackable.
if (
unitB &&
((unitB.hasMoved() && map.matchesPlayer(unitB, unit)) ||
!map.isOpponent(unitB, unit))
) {
continue;
}
const vectors = parent.vector.adjacent();
for (let i = 0; i < vectors.length; i++) {
const vector = vectors[i];
if (map.contains(vector)) {
const itemB = attackable.get(vector);
if (
!itemB ||
(parentCost < itemB.cost && vector.distance(start) > 1)
) {
attackable.set(
vector,
RadiusItem(vector, parentCost, parent.vector),
);
}
}
}
}
if (info.isLongRange() && info.canAttackAt(2, range)) {
const canAttackLargeArea = info.canAttackAt(3, range);
for (const [, item] of Array.from(moveable)) {
const list = canAttackLargeArea
? getVectorRadius(map, item.vector, 3)
: item.vector.adjacentStar();
for (const vector of list) {
const currentAttackable = attackable.get(vector);
if (
map.contains(vector) &&
(!currentAttackable ||
(currentAttackable.parent &&
map.units.has(currentAttackable.parent) &&
!map.units.has(item.vector)))
) {
attackable.set(
vector,
RadiusItem(vector, item.cost, item.vector),
);
}
}
}
}
}
}
return attackable;
}