|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class used to keep track of paths between two sectors. |
|
5
|
|
|
* Used by the Plotter class to store the state of a plotted course. |
|
6
|
|
|
*/ |
|
7
|
|
|
class Distance { |
|
8
|
|
|
private $gameID; |
|
9
|
|
|
private $distance = -1; //First sector added will be the start and a distance of 0 |
|
10
|
|
|
private $numWarps = 0; |
|
11
|
|
|
private $path = array(); |
|
12
|
|
|
private $warpMap = array(); |
|
13
|
|
|
|
|
14
|
|
|
public function __construct($gameID,$_startSectorId) { |
|
15
|
|
|
$this->gameID = $gameID; |
|
16
|
|
|
$this->addToPath($_startSectorId); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function incrementDistance() { |
|
20
|
|
|
$this->distance++; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function incrementNumWarps() { |
|
24
|
|
|
$this->numWarps++; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getDistance() { |
|
28
|
|
|
return $this->distance; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getTotalSectors() { |
|
32
|
|
|
return $this->getDistance()+$this->getNumWarps(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getNumWarps() { |
|
36
|
|
|
return $this->numWarps; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getTurns() { |
|
40
|
|
|
return $this->distance * TURNS_PER_SECTOR + $this->numWarps * TURNS_PER_WARP; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getRelativeDistance() { |
|
44
|
|
|
return $this->distance + $this->numWarps * TURNS_WARP_SECTOR_EQUIVALENCE; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return integer |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getEndSectorID() { |
|
51
|
|
|
return $this->path[count($this->path)-1]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function &getEndSector() { |
|
55
|
|
|
return SmrSector::getSector($this->gameID,$this->getEndSectorID()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getPath() { |
|
62
|
|
|
return $this->path; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// NOTE: this assumes 2-way warps |
|
66
|
|
|
public function reversePath() { |
|
67
|
|
|
$this->path = array_reverse($this->path); |
|
68
|
|
|
$this->warpMap = array_flip($this->warpMap); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param integer $nextSector |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addToPath($nextSector) { |
|
75
|
|
|
$this->incrementDistance(); |
|
76
|
|
|
$this->path[] = $nextSector; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function addWarpToPath($sectorAfterWarp, $sectorBeforeWarp) { |
|
80
|
|
|
$this->incrementNumWarps(); |
|
81
|
|
|
$this->path[] = $sectorAfterWarp; |
|
82
|
|
|
$this->warpMap[$sectorBeforeWarp] = $sectorAfterWarp; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getNextOnPath() { |
|
86
|
|
|
return $this->path[0]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function followPath() { |
|
90
|
|
|
$nextSectorID = array_shift($this->path); |
|
91
|
|
|
if (in_array($nextSectorID, $this->warpMap)) { |
|
92
|
|
|
$this->numWarps--; |
|
93
|
|
|
} else { |
|
94
|
|
|
$this->distance--; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function removeStart() { |
|
99
|
|
|
return array_shift($this->path); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function isInPath($sectorID) { |
|
103
|
|
|
return in_array($sectorID,$this->getPath()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* If the given sector is in the path, then return the segment |
|
108
|
|
|
* of the path that comes after the given sector. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function skipToSector($sectorID) { |
|
111
|
|
|
$position = array_search($sectorID, $this->path); |
|
112
|
|
|
if ($position !== false) { |
|
113
|
|
|
// The resulting path does not include sectorID, i.e. (sectorID,end] |
|
114
|
|
|
$this->path = array_slice($this->path, $position + 1); |
|
115
|
|
|
$this->numWarps = count(array_intersect($this->path, array_values($this->warpMap))); |
|
116
|
|
|
$this->distance = count($this->path) - $this->numWarps; |
|
117
|
|
|
return $this; |
|
118
|
|
|
} else { |
|
119
|
|
|
throw new Exception('Cannot skip to sector not in path!'); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|