|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Smr; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class used to keep track of paths between two sectors. |
|
9
|
|
|
* Used by the Plotter class to store the state of a plotted course. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Path { |
|
12
|
|
|
|
|
13
|
|
|
/** @var array<int, int> */ |
|
14
|
|
|
private array $path; |
|
15
|
|
|
private int $numWarps = 0; |
|
16
|
|
|
/** @var array<int, int> */ |
|
17
|
|
|
private array $warpMap = []; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(int $startSectorID) { |
|
20
|
|
|
$this->path = [$startSectorID]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Returns the number of sectors to get to the end of the path |
|
25
|
|
|
* (does not include the start sector). |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getLength(): int { |
|
28
|
|
|
return count($this->path) - 1; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getNumWarps(): int { |
|
32
|
|
|
return $this->numWarps; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getTurns(): int { |
|
36
|
|
|
return $this->getLength() * TURNS_PER_SECTOR + $this->numWarps * (TURNS_PER_WARP - TURNS_PER_SECTOR); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getDistance(): int { |
|
40
|
|
|
return $this->getLength() + $this->numWarps * (TURNS_WARP_SECTOR_EQUIVALENCE - 1); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getEndSectorID(): int { |
|
44
|
|
|
return $this->path[count($this->path) - 1]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array<int, int> |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getPath(): array { |
|
51
|
|
|
return $this->path; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// NOTE: this assumes 2-way warps |
|
55
|
|
|
public function reversePath(): void { |
|
56
|
|
|
$this->path = array_reverse($this->path); |
|
57
|
|
|
$this->warpMap = array_flip($this->warpMap); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add a neighboring sector to the path. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function addLink(int $nextSector): void { |
|
64
|
|
|
$this->path[] = $nextSector; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add a warp to the path. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function addWarp(int $nextSector): void { |
|
71
|
|
|
$this->warpMap[$this->getEndSectorID()] = $nextSector; |
|
72
|
|
|
$this->numWarps++; |
|
73
|
|
|
$this->path[] = $nextSector; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getNextOnPath(): int { |
|
77
|
|
|
return $this->path[1]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getStartSectorID(): int { |
|
81
|
|
|
return $this->path[0]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function followPath(): void { |
|
85
|
|
|
$nextSectorID = $this->getNextOnPath(); |
|
86
|
|
|
if (in_array($nextSectorID, $this->warpMap)) { |
|
87
|
|
|
$this->numWarps--; |
|
88
|
|
|
} |
|
89
|
|
|
array_shift($this->path); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function isInPath(int $sectorID): bool { |
|
93
|
|
|
return in_array($sectorID, $this->getPath()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* If the given sector is in the path, then return the segment |
|
98
|
|
|
* of the path that comes after the given sector. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function skipToSector(int $sectorID): self { |
|
101
|
|
|
$position = array_search($sectorID, $this->path); |
|
102
|
|
|
if ($position === false) { |
|
103
|
|
|
throw new Exception('Cannot skip to sector not in path!'); |
|
104
|
|
|
} |
|
105
|
|
|
// The resulting path does include sectorID, i.e. [sectorID,end] |
|
106
|
|
|
$this->path = array_slice($this->path, $position); |
|
|
|
|
|
|
107
|
|
|
$this->numWarps = count(array_intersect($this->path, array_keys($this->warpMap))); |
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|