-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasePathFinder.ts
150 lines (123 loc) · 3.94 KB
/
BasePathFinder.ts
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
import { Air, Land as LandUnit, Naval } from '@civ-clone/library-unit/Types';
import { PathFinder, IPathFinder } from '@civ-clone/core-world-path/PathFinder';
import {
RuleRegistry,
instance as ruleRegistryInstance,
} from '@civ-clone/core-rule/RuleRegistry';
import Action from '@civ-clone/core-unit/Action';
import { Move } from '@civ-clone/library-unit/Actions';
import MovementCost from '@civ-clone/core-unit/Rules/MovementCost';
import Path from '@civ-clone/core-world-path/Path';
import Tile from '@civ-clone/core-world/Tile';
import Unit from '@civ-clone/core-unit/Unit';
export type Node = {
tile: Tile;
parent: Node | null;
cost: number;
};
interface IBasePathFinder extends IPathFinder {
createNode(tile: Tile, parent: Node | null, cost: number): Node;
createPath(node: Node): Path;
}
export class BasePathFinder extends PathFinder implements IBasePathFinder {
#candidates: Path[] = [];
#heap: Node[] = [this.createNode(this.start())];
#ruleRegistry: RuleRegistry;
#seen: Tile[] = [this.start()];
constructor(
unit: Unit,
start: Tile,
end: Tile,
ruleRegistry: RuleRegistry = ruleRegistryInstance
) {
super(unit, start, end);
this.#ruleRegistry = ruleRegistry;
}
private canMoveTo(tile: Tile): boolean {
if (this.unit() instanceof Air) {
return true;
}
if (this.unit() instanceof LandUnit) {
return tile.isLand();
}
if (this.unit() instanceof Naval) {
return tile.isWater();
}
return false;
}
createNode(tile: Tile, parent: Node | null = null, cost: number = 0): Node {
return {
tile,
parent,
cost,
};
}
createPath(node: Node): Path {
const tiles: Tile[] = [];
let movementCost = 0;
while (node.parent) {
tiles.unshift(node.tile);
movementCost += node.cost;
node = node.parent;
}
tiles.unshift(node.tile);
const path = new Path(...tiles);
path.setMovementCost(movementCost);
return path;
}
generate(): Path {
while (this.#heap.length) {
const currentNode = this.#heap.shift(),
{ tile } = currentNode as Node;
tile
.getNeighbours()
.sort(
(neighbourA, neighbourB) =>
neighbourA.distanceFrom(tile) - neighbourB.distanceFrom(tile)
)
// TODO: is this needed to make it fair?
// .filter((tile: Tile): boolean => this.#playerWorldRegistry.getByPlayer(this.unit().player()).includes(tile))
.forEach((target: Tile): void => {
if (this.canMoveTo(target)) {
const [movementCost] = this.#ruleRegistry
.process(
MovementCost,
this.unit(),
new Move(
tile,
target,
this.unit(),
this.#ruleRegistry
) as Action
)
.sort((costA, costB) => costA - costB),
targetNode = this.createNode(target, currentNode, 1);
if (target === this.end()) {
this.#candidates.push(this.createPath(targetNode));
// if this path is "good enough" (<10% longer than direct), skip out here...
if (
this.#candidates[this.#candidates.length - 1].length <
this.start().distanceFrom(this.end()) * 1.1
) {
this.#heap.splice(0, this.#heap.length);
}
return;
}
if (
!this.#heap.some((node: Node): boolean => node.tile === target) &&
!this.#seen.includes(target)
) {
this.#heap.push(targetNode);
this.#seen.push(target);
}
}
});
}
// TODO: This might get REALLY expensive...
const [cheapest] = this.#candidates.sort(
(a: Path, b: Path): number => a.movementCost() - b.movementCost()
);
return cheapest;
}
}
export default BasePathFinder;