forked from solcloud/Counter-Strike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder.php
More file actions
235 lines (201 loc) Β· 8.18 KB
/
PathFinder.php
File metadata and controls
235 lines (201 loc) Β· 8.18 KB
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
<?php
namespace cs\Core;
use GraphPHP\Edge\DirectedEdge;
use GraphPHP\Node\Node;
use SplQueue;
final class PathFinder
{
private Graph $graph;
/** @var array<string,null> */
private array $visited = [];
private readonly int $obstacleOvercomeHeight;
/** @var array<int,array{int,int,int}> */
private static array $moves = [
90 => [+1, +0, +0],
0 => [+0, +0, +1],
270 => [-1, +0, +0],
180 => [+0, +0, -1],
];
public function __construct(private World $world, private NavigationMesh $navigationMesh)
{
$this->graph = new Graph();
$this->obstacleOvercomeHeight = Setting::playerObstacleOvercomeHeight();
}
private function canFullyMoveTo(Point $candidate, int $angle, int $targetDistance, int $radius, int $height): bool
{
if ($angle % 90 !== 0) {
GameException::notImplementedYet(); // @codeCoverageIgnore
}
$looseFloor = false;
for ($distance = 1; $distance <= $targetDistance; $distance++) {
$candidate->addPart(...self::$moves[$angle]);
if (!$this->canMoveTo($candidate, $angle, $radius)) {
return false;
}
if (!$looseFloor && !$this->world->findFloorSquare($candidate, $radius)) {
$looseFloor = true;
}
}
if (!$looseFloor) {
return true;
}
$fallCandidate = $candidate->clone();
foreach (range(1, 3 * $height) as $i) {
$fallCandidate->addY(-1);
if ($this->world->findFloorSquare($fallCandidate, $radius)) {
$candidate->setY($fallCandidate->y); // side effect
return true;
}
}
return false;
}
private function canMoveTo(Point $start, int $angle, int $radius): bool
{
$maxWallCeiling = $start->y + $this->obstacleOvercomeHeight;
$xWallMaxHeight = 0;
if ($angle === 90 || $angle === 270) {
$baseX = $start->clone()->addX(($angle === 90) ? $radius : -$radius);
$xWallMaxHeight = $this->world->findHighestWall($baseX, $this->navigationMesh->colliderHeight, $radius, $maxWallCeiling, true);
}
$zWallMaxHeight = 0;
if ($angle === 0 || $angle === 180) {
$baseZ = $start->clone()->addZ(($angle === 0) ? $radius : -$radius);
$zWallMaxHeight = $this->world->findHighestWall($baseZ, $this->navigationMesh->colliderHeight, $radius, $maxWallCeiling, false);
}
if ($xWallMaxHeight === 0 && $zWallMaxHeight === 0) { // no walls
return true;
}
// Try step over ONE low height wall
$highestWallCeiling = null;
if ($xWallMaxHeight === 0 && $zWallMaxHeight <= $maxWallCeiling) {
$highestWallCeiling = $zWallMaxHeight;
} elseif ($zWallMaxHeight === 0 && $xWallMaxHeight <= $maxWallCeiling) {
$highestWallCeiling = $xWallMaxHeight;
}
if ($highestWallCeiling === null) {
return false;
}
$floor = $this->world->findFloorSquare($start->clone()->setY($highestWallCeiling), $radius);
if ($floor) {
$start->setY($floor->getY()); // side effect
return true;
}
throw new GameException('Wall we can step over but no floor there ' . $start);
}
public function findTile(Point $pointOnFloor, int $radius): ?Point
{
$floorNavmeshPoint = $pointOnFloor->clone();
$this->convertToNavMeshNode($floorNavmeshPoint);
if ($this->navigationMesh->has($floorNavmeshPoint->hash())) {
return $floorNavmeshPoint;
}
$maxDistance = $this->navigationMesh->tileSize + $this->navigationMesh->tileSize;
$maxY = $this->obstacleOvercomeHeight + $this->obstacleOvercomeHeight;
$checkAbove = function (Point $start, int $maxY, int $radius): ?Point {
$yCandidate = $start->clone();
$navMeshCenter = $yCandidate->clone();
$this->convertToNavMeshNode($navMeshCenter);
for ($i = 1; $i <= $maxY; $i++) {
$yCandidate->addY(1);
if ($this->world->findFloorSquare($yCandidate, $radius - 1)) {
return null;
}
if ($this->navigationMesh->has($navMeshCenter->setY($yCandidate->y)->hash())) {
return $navMeshCenter;
}
}
return null;
};
// try navmesh above
$above = $checkAbove($pointOnFloor, $maxY, $radius);
if ($above) {
return $above;
}
// try neighbour tiles
$candidate = $pointOnFloor->clone();
$navmesh = $pointOnFloor->clone();
foreach (self::$moves as $angle => $move) {
$candidate->setFrom($pointOnFloor);
for ($distance = 1; $distance <= $maxDistance; $distance++) {
$candidate->addPart(...self::$moves[$angle]);
if (!$this->canFullyMoveTo($candidate, $angle, 1, $radius, $this->navigationMesh->colliderHeight)) { // @infection-ignore-all
break;
}
$prevNavmesh = $navmesh->hash();
$navmesh->setFrom($candidate);
$this->convertToNavMeshNode($navmesh);
if ($prevNavmesh === $navmesh->hash()) {
continue;
}
if ($this->navigationMesh->has($navmesh->hash())) {
return $navmesh;
}
$above = $checkAbove($candidate, $maxY, $radius);
if ($above) {
return $above;
}
}
}
return null;
}
public function convertToNavMeshNode(Point $point): void
{
$this->navigationMesh->convertToNavMeshNode($point);
}
public function buildNavigationMesh(Point $start, int $objectHeight, int $maxNodeCount = 2_000): void
{
$startPoint = $start->clone();
$this->convertToNavMeshNode($startPoint);
if (!$this->world->findFloorSquare($startPoint, 1)) {
throw new GameException('No floor on start: ' . $start); // @codeCoverageIgnore
}
/** @var SplQueue<Point> $queue */
$queue = new SplQueue();
$queue->enqueue($startPoint);
$candidate = new Point();
$nodeCount = 0;
while (!$queue->isEmpty()) {
$current = $queue->dequeue();
$currentKey = $current->hash();
if (array_key_exists($currentKey, $this->visited)) {
continue;
}
$this->visited[$currentKey] = null;
$currentNode = $this->graph->getNodeById($currentKey);
if ($currentNode === null) {
$currentNode = new Node($currentKey, $current);
$this->graph->addNode($currentNode);
}
foreach (self::$moves as $angle => $move) {
$candidate->setFrom($current);
if (!$this->canFullyMoveTo($candidate, $angle, $this->navigationMesh->tileSize, $this->navigationMesh->tileSizeHalf, $objectHeight)) {
continue;
}
$newNeighbour = $candidate->clone();
$newNode = $this->graph->getNodeById($newNeighbour->hash());
if ($newNode === null) {
$newNode = new Node($newNeighbour->hash(), $newNeighbour);
$this->graph->addNode($newNode);
}
$this->graph->addEdge(new DirectedEdge($currentNode, $newNode, 1));
$queue->enqueue($newNeighbour);
}
if (++$nodeCount === $maxNodeCount) { // @codeCoverageIgnore
GameException::notImplementedYet('MaxNodeCount hit - new map, tileSize or bad test (no boundary box, bad starting point)?'); // @codeCoverageIgnore
}
}
}
public function saveAndClear(): void
{
$this->visited = [];
$this->navigationMesh->setData($this->getGraph()->generateNeighbors());
}
public function getGraph(): Graph
{
return $this->graph;
}
public function getNavigationMesh(): NavigationMesh
{
return $this->navigationMesh;
}
}