1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
class SmrGalaxy { |
3
|
|
|
protected static $CACHE_GALAXIES = array(); |
4
|
|
|
protected static $CACHE_GAME_GALAXIES = array(); |
5
|
|
|
|
6
|
|
|
const TYPES = ['Racial', 'Neutral', 'Planet']; |
7
|
|
|
|
8
|
|
|
protected $db; |
9
|
|
|
protected $SQL; |
10
|
|
|
|
11
|
|
|
protected $gameID; |
12
|
|
|
protected $galaxyID; |
13
|
|
|
protected $name; |
14
|
|
|
protected $width; |
15
|
|
|
protected $height; |
16
|
|
|
protected $galaxyType; |
17
|
|
|
protected $maxForceTime; |
18
|
|
|
|
19
|
|
|
protected $startSector = false; |
20
|
|
|
|
21
|
|
|
protected $hasChanged = false; |
22
|
|
|
protected $isNew = false; |
23
|
|
|
|
24
|
|
|
public static function getGameGalaxies($gameID, $forceUpdate = false) { |
25
|
|
|
if ($forceUpdate || !isset(self::$CACHE_GAME_GALAXIES[$gameID])) { |
26
|
|
|
$db = new SmrMySqlDatabase(); |
27
|
|
|
$db->query('SELECT * FROM game_galaxy WHERE game_id = ' . $db->escapeNumber($gameID) . ' ORDER BY galaxy_id ASC'); |
28
|
|
|
$galaxies = array(); |
29
|
|
|
while ($db->nextRecord()) { |
30
|
|
|
$galaxyID = $db->getInt('galaxy_id'); |
31
|
|
|
$galaxies[$galaxyID] = self::getGalaxy($gameID, $galaxyID, $forceUpdate, $db); |
32
|
|
|
} |
33
|
|
|
self::$CACHE_GAME_GALAXIES[$gameID] = $galaxies; |
34
|
|
|
} |
35
|
|
|
return self::$CACHE_GAME_GALAXIES[$gameID]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function getGalaxy($gameID, $galaxyID, $forceUpdate = false, $db = null) { |
39
|
|
|
if ($forceUpdate || !isset(self::$CACHE_GALAXIES[$gameID][$galaxyID])) { |
40
|
|
|
$g = new SmrGalaxy($gameID, $galaxyID, false, $db); |
41
|
|
|
self::$CACHE_GALAXIES[$gameID][$galaxyID] = $g; |
42
|
|
|
} |
43
|
|
|
return self::$CACHE_GALAXIES[$gameID][$galaxyID]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function saveGalaxies() { |
47
|
|
|
foreach (self::$CACHE_GALAXIES as $gameGalaxies) { |
48
|
|
|
foreach ($gameGalaxies as $galaxy) { |
49
|
|
|
$galaxy->save(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function createGalaxy($gameID, $galaxyID) { |
55
|
|
|
if (!isset(self::$CACHE_GALAXIES[$gameID][$galaxyID])) { |
56
|
|
|
$g = new SmrGalaxy($gameID, $galaxyID, true); |
57
|
|
|
self::$CACHE_GALAXIES[$gameID][$galaxyID] = $g; |
58
|
|
|
} |
59
|
|
|
return self::$CACHE_GALAXIES[$gameID][$galaxyID]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function __construct($gameID, $galaxyID, $create = false, $db = null) { |
63
|
|
|
$this->db = new SmrMySqlDatabase(); |
64
|
|
|
$this->SQL = 'game_id = ' . $this->db->escapeNumber($gameID) . ' |
65
|
|
|
AND galaxy_id = ' . $this->db->escapeNumber($galaxyID); |
66
|
|
|
|
67
|
|
|
if (isset($db)) { |
68
|
|
|
$this->isNew = false; |
69
|
|
|
} else { |
70
|
|
|
$db = $this->db; |
71
|
|
|
$db->query('SELECT * FROM game_galaxy WHERE ' . $this->SQL); |
72
|
|
|
$this->isNew = !$db->nextRecord(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->gameID = (int)$gameID; |
76
|
|
|
$this->galaxyID = (int)$galaxyID; |
77
|
|
|
if (!$this->isNew) { |
78
|
|
|
$this->name = $db->getField('galaxy_name'); |
79
|
|
|
$this->width = $db->getInt('width'); |
80
|
|
|
$this->height = $db->getInt('height'); |
81
|
|
|
$this->galaxyType = $db->getField('galaxy_type'); |
82
|
|
|
$this->maxForceTime = $db->getInt('max_force_time'); |
83
|
|
|
} elseif ($create === false) { |
84
|
|
|
throw new Exception('No such galaxy: ' . $gameID . '-' . $galaxyID); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function save() { |
89
|
|
|
if ($this->isNew) { |
90
|
|
|
$this->db->query('INSERT INTO game_galaxy (game_id,galaxy_id,galaxy_name,width,height,galaxy_type,max_force_time) |
91
|
|
|
values |
92
|
|
|
(' . $this->db->escapeNumber($this->getGameID()) . |
93
|
|
|
',' . $this->db->escapeNumber($this->getGalaxyID()) . |
94
|
|
|
',' . $this->db->escapeString($this->getName()) . |
95
|
|
|
',' . $this->db->escapeNumber($this->getWidth()) . |
96
|
|
|
',' . $this->db->escapeNumber($this->getHeight()) . |
97
|
|
|
',' . $this->db->escapeString($this->getGalaxyType()) . |
98
|
|
|
',' . $this->db->escapeNumber($this->getMaxForceTime()) . ')'); |
99
|
|
|
} elseif ($this->hasChanged) { |
100
|
|
|
$this->db->query('UPDATE game_galaxy SET galaxy_name = ' . $this->db->escapeString($this->getName()) . |
101
|
|
|
', width = ' . $this->db->escapeNumber($this->getWidth()) . |
102
|
|
|
', height = ' . $this->db->escapeNumber($this->getHeight()) . |
103
|
|
|
', galaxy_type = ' . $this->db->escapeString($this->getGalaxyType()) . |
104
|
|
|
', max_force_time = ' . $this->db->escapeNumber($this->getMaxForceTime()) . |
105
|
|
|
' WHERE ' . $this->SQL); |
106
|
|
|
} |
107
|
|
|
$this->isNew = false; |
108
|
|
|
$this->hasChanged = false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getGameID() { |
112
|
|
|
return $this->gameID; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getGalaxyID() { |
116
|
|
|
return $this->galaxyID; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getGalaxyMapHREF() { |
120
|
|
|
return 'map_galaxy.php?galaxy_id=' . $this->getGalaxyID(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns the galaxy name. |
125
|
|
|
* Use getDisplayName for an HTML-safe version. |
126
|
|
|
*/ |
127
|
|
|
public function getName() { |
128
|
|
|
return $this->name; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the galaxy name, suitable for HTML display. |
133
|
|
|
*/ |
134
|
|
|
public function getDisplayName() : string { |
135
|
|
|
return htmlentities($this->name); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function setName($name) { |
139
|
|
|
if ($this->name == $name) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
$this->name = $name; |
143
|
|
|
$this->hasChanged = true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getWidth() { |
147
|
|
|
return $this->width; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function setWidth($width) { |
151
|
|
|
if ($this->width == $width) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
$this->width = $width; |
155
|
|
|
$this->hasChanged = true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getHeight() { |
159
|
|
|
return $this->height; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setHeight($height) { |
163
|
|
|
if ($this->height == $height) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
$this->height = $height; |
167
|
|
|
$this->hasChanged = true; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getStartSector() { |
171
|
|
|
if ($this->startSector === false) { |
172
|
|
|
$this->startSector = 1; |
173
|
|
|
if ($this->getGalaxyID() != 1) { |
174
|
|
|
$galaxies = SmrGalaxy::getGameGalaxies($this->getGameID()); |
175
|
|
|
for ($i = 1; $i < $this->getGalaxyID(); $i++) { |
176
|
|
|
$this->startSector += $galaxies[$i]->getSize(); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
return $this->startSector; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getEndSector() { |
184
|
|
|
return $this->getStartSector() + $this->getSize() - 1; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function getSize() { |
188
|
|
|
return $this->getHeight() * $this->getWidth(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getSectors() { |
192
|
|
|
return SmrSector::getGalaxySectors($this->getGameID(), $this->getGalaxyID()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getPorts() { |
196
|
|
|
return SmrPort::getGalaxyPorts($this->getGameID(), $this->getGalaxyID()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getLocations() { |
200
|
|
|
return SmrLocation::getGalaxyLocations($this->getGameID(), $this->getGalaxyID()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getPlanets() { |
204
|
|
|
return SmrPlanet::getGalaxyPlanets($this->getGameID(), $this->getGalaxyID()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function getForces() { |
208
|
|
|
return SmrForce::getGalaxyForces($this->getGameID(), $this->getGalaxyID()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getPlayers() { |
212
|
|
|
return SmrPlayer::getGalaxyPlayers($this->getGameID(), $this->getGalaxyID()); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns a 2D array of sectors in the galaxy. |
217
|
|
|
* If $centerSectorID is specified, it will be in the center of the array. |
218
|
|
|
* If $dist is also specified, only include sectors $dist away from center. |
219
|
|
|
* |
220
|
|
|
* NOTE: This routine queries sectors inefficiently. You may want to |
221
|
|
|
* construct the cache efficiently before calling this. |
222
|
|
|
*/ |
223
|
|
|
public function getMapSectors($centerSectorID = false, $dist = false) { |
224
|
|
|
if ($centerSectorID === false) { |
225
|
|
|
$topLeft = SmrSector::getSector($this->getGameID(), $this->getStartSector()); |
226
|
|
|
} else { |
227
|
|
|
$topLeft = SmrSector::getSector($this->getGameID(), $centerSectorID); |
228
|
|
|
// go left then up |
229
|
|
|
for ($i = 0; ($dist === false || $i < $dist) && $i < floor($this->getWidth() / 2); $i++) { |
230
|
|
|
$topLeft = $topLeft->getNeighbourSector('Left'); |
231
|
|
|
} |
232
|
|
|
for ($i = 0; ($dist === false || $i < $dist) && $i < floor($this->getHeight() / 2); $i++) { |
233
|
|
|
$topLeft = $topLeft->getNeighbourSector('Up'); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$mapSectors = array(); |
238
|
|
|
for ($i = 0; ($dist === false || $i < 2 * $dist + 1) && $i < $this->getHeight(); $i++) { |
239
|
|
|
$mapSectors[$i] = array(); |
240
|
|
|
// get left most sector for this row |
241
|
|
|
$rowLeft = $i == 0 ? $topLeft : $rowLeft->getNeighbourSector('Down'); |
|
|
|
|
242
|
|
|
|
243
|
|
|
// iterate through the columns |
244
|
|
|
for ($j = 0; ($dist === false || $j < 2 * $dist + 1) && $j < $this->getWidth(); $j++) { |
245
|
|
|
$nextSec = $j == 0 ? $rowLeft : $nextSec->getNeighbourSector('Right'); |
|
|
|
|
246
|
|
|
$mapSectors[$i][$j] = $nextSec; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
return $mapSectors; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function getGalaxyType() { |
253
|
|
|
return $this->galaxyType; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function setGalaxyType($galaxyType) { |
257
|
|
|
if ($this->galaxyType == $galaxyType) { |
258
|
|
|
return; |
259
|
|
|
} |
260
|
|
|
$this->galaxyType = $galaxyType; |
261
|
|
|
$this->hasChanged = true; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function getMaxForceTime() { |
265
|
|
|
return $this->maxForceTime; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function setMaxForceTime($maxForceTime) { |
269
|
|
|
if ($this->maxForceTime == $maxForceTime) { |
270
|
|
|
return; |
271
|
|
|
} |
272
|
|
|
$this->maxForceTime = $maxForceTime; |
273
|
|
|
$this->hasChanged = true; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function generateSectors() { |
277
|
|
|
$sectorID = $this->getStartSector(); |
278
|
|
|
for ($i = 0; $i < $this->getSize(); $i++) { |
279
|
|
|
$sector = SmrSector::createSector($this->gameID, $sectorID); |
280
|
|
|
$sector->setGalaxyID($this->getGalaxyID()); |
281
|
|
|
$sector->update(); //Have to save sectors after creating them |
282
|
|
|
$sectorID++; |
283
|
|
|
} |
284
|
|
|
$this->setConnectivity(100); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Randomly set the connections between all galaxy sectors. |
289
|
|
|
* $connectivity = (average) percent of connections to enable. |
290
|
|
|
*/ |
291
|
|
|
public function setConnectivity($connectivity) { |
292
|
|
|
// Only set down/right, otherwise we double-hit every link |
293
|
|
|
$linkDirs = array('Down', 'Right'); |
294
|
|
|
|
295
|
|
|
$problem = true; |
296
|
|
|
$problemTimes = 0; |
297
|
|
|
while ($problem) { |
298
|
|
|
$problem = false; |
299
|
|
|
|
300
|
|
|
foreach ($this->getSectors() as $galSector) { |
301
|
|
|
foreach ($linkDirs as $linkDir) { |
302
|
|
|
if (mt_rand(1, 100) <= $connectivity) { |
303
|
|
|
$galSector->enableLink($linkDir); |
304
|
|
|
} else { |
305
|
|
|
$galSector->disableLink($linkDir); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
// Try again if any sector has 0 connections (except 1-sector gals) |
311
|
|
|
if ($this->getSize() > 1) { |
312
|
|
|
foreach ($this->getSectors() as $galSector) { |
313
|
|
|
if ($galSector->getNumberOfConnections() == 0) { |
314
|
|
|
$problem = true; |
315
|
|
|
break; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if ($problem && $problemTimes++ > 350) { |
321
|
|
|
$connectivity = 100; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
return $problemTimes <= 350; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Returns the sector connectivity of the galaxy as a percent. |
329
|
|
|
*/ |
330
|
|
|
public function getConnectivity() { |
331
|
|
|
$totalLinks = 0; |
332
|
|
|
foreach ($this->getSectors() as $galSector) { |
333
|
|
|
$totalLinks += $galSector->getNumberOfLinks(); |
334
|
|
|
} |
335
|
|
|
// There are 4 possible links per sector |
336
|
|
|
$connectivity = 100 * $totalLinks / (4 * $this->getSize()); |
337
|
|
|
return $connectivity; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function contains($sectorID) { |
341
|
|
|
if ($sectorID instanceof SmrSector) { |
342
|
|
|
return $sectorID->getGalaxyID() == $this->getGalaxyID(); |
343
|
|
|
} |
344
|
|
|
return $sectorID >= $this->getStartSector() && $sectorID <= $this->getEndSector(); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public static function getGalaxyContaining($gameID, $sectorID) { |
348
|
|
|
return SmrSector::getSector($gameID, $sectorID)->getGalaxy(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function equals(SmrGalaxy $otherGalaxy) { |
352
|
|
|
return $otherGalaxy->getGalaxyID() == $this->getGalaxyID() && $otherGalaxy->getGameID() == $this->getGameID(); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|