1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Common; |
4
|
|
|
|
5
|
|
|
use classConfig; |
6
|
|
|
|
7
|
|
|
class Vector { |
8
|
|
|
|
9
|
|
|
const READ_VECTOR = 'readVector'; |
10
|
|
|
const READ_PARAMS_FLEET = 'readParamsFleet'; |
11
|
|
|
|
12
|
|
|
public static $knownGalaxies = 0; |
13
|
|
|
public static $knownSystems = 0; |
14
|
|
|
public static $knownPlanets = 0; |
15
|
|
|
public static $galaxyDistance = 20000; |
16
|
|
|
protected static $_isStaticInit = false; |
17
|
|
|
|
18
|
|
|
public $galaxy = 0; |
19
|
|
|
public $system = 0; |
20
|
|
|
public $planet = 0; |
21
|
|
|
public $type = PT_NONE; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param classConfig $config |
25
|
|
|
*/ |
26
|
82 |
|
public static function _staticInit($config) { |
27
|
82 |
|
if (static::$_isStaticInit) { |
28
|
81 |
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
static::$knownGalaxies = intval($config->game_maxGalaxy); |
32
|
1 |
|
static::$knownSystems = intval($config->game_maxSystem); |
33
|
1 |
|
static::$knownPlanets = intval($config->game_maxPlanet); |
34
|
1 |
|
static::$galaxyDistance = intval($config->uni_galaxy_distance); |
|
|
|
|
35
|
1 |
|
static::$_isStaticInit = true; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Vector $vector |
40
|
|
|
*/ |
41
|
62 |
|
public function readFromVector($vector) { |
42
|
62 |
|
$this->galaxy = $vector->galaxy; |
43
|
62 |
|
$this->system = $vector->system; |
44
|
62 |
|
$this->planet = $vector->planet; |
45
|
62 |
|
$this->type = $vector->type; |
46
|
62 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $paramName |
50
|
|
|
* @param array $planetRow |
51
|
|
|
* |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
protected function getParamInt($paramName, $planetRow) { |
55
|
|
|
$default = empty($planetRow[$paramName]) ? 0 : $planetRow[$paramName]; |
56
|
|
|
|
57
|
|
|
return sys_get_param_int($paramName, $default); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $planetRow |
62
|
|
|
*/ |
63
|
|
|
public function readFromParamFleets($planetRow = array()) { |
64
|
|
|
$this->galaxy = $this->getParamInt('galaxy', $planetRow); |
65
|
|
|
$this->system = $this->getParamInt('system', $planetRow); |
66
|
|
|
$this->planet = $this->getParamInt('planet', $planetRow); |
67
|
|
|
$this->type = $this->getParamInt('planet_type', $planetRow); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Vector constructor. |
72
|
|
|
* |
73
|
|
|
* @param int|string $galaxy |
74
|
|
|
* @param int|Vector|array $system |
75
|
|
|
* @param int $planet |
76
|
|
|
* @param int $type |
77
|
|
|
*/ |
78
|
82 |
|
public function __construct($galaxy = 0, $system = 0, $planet = 0, $type = PT_NONE) { |
79
|
|
|
// static::_staticInit(); |
|
|
|
|
80
|
|
|
|
81
|
82 |
|
if (is_string($galaxy) && $galaxy == Vector::READ_VECTOR && is_object($system) && $system instanceof Vector) { |
82
|
1 |
|
$this->readFromVector($system); |
83
|
82 |
|
} elseif (is_string($galaxy) && $galaxy == Vector::READ_PARAMS_FLEET && is_array($system)) { |
84
|
|
|
$this->readFromParamFleets($system); |
85
|
|
|
} else { |
86
|
82 |
|
$this->galaxy = intval($galaxy); |
87
|
82 |
|
$this->system = intval($system); |
88
|
82 |
|
$this->planet = intval($planet); |
89
|
82 |
|
$this->type = intval($type); |
90
|
|
|
} |
91
|
82 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Vector $vector |
95
|
|
|
* @param bool $returnZero |
96
|
|
|
* |
97
|
|
|
* @return int|number |
98
|
|
|
*/ |
99
|
68 |
|
public function distance($vector, $returnZero = false) { |
100
|
68 |
|
if ($this->galaxy != $vector->galaxy) { |
101
|
22 |
|
$distance = abs($this->galaxy - $vector->galaxy) * static::$galaxyDistance; |
102
|
68 |
|
} elseif ($this->system != $vector->system) { |
103
|
19 |
|
$distance = abs($this->system - $vector->system) * 5 * 19 + 2700; |
104
|
46 |
|
} elseif ($this->planet != $vector->planet) { |
105
|
19 |
|
$distance = abs($this->planet - $vector->planet) * 5 + 1000; |
106
|
27 |
|
} elseif ($returnZero && $this->type == $vector->type) { |
107
|
|
|
// && $this->type != PT_NONE && $vector->type != PT_NONE |
|
|
|
|
108
|
1 |
|
$distance = 0; |
109
|
1 |
|
} else { |
110
|
7 |
|
$distance = 5; |
111
|
|
|
} |
112
|
|
|
|
113
|
68 |
|
return $distance; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $coordinates |
118
|
|
|
* @param string $prefix |
119
|
|
|
* |
120
|
|
|
* @return static |
121
|
|
|
*/ |
122
|
16 |
|
public static function convertToVector($coordinates, $prefix = '') { |
123
|
16 |
|
$vector = new static(); |
124
|
16 |
|
$vector->convertToVectorDynamic($coordinates, $prefix); |
125
|
|
|
|
126
|
16 |
|
return $vector; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $coordinates |
131
|
|
|
* @param string $prefix |
132
|
|
|
* |
133
|
|
|
* @return static |
134
|
|
|
*/ |
135
|
16 |
|
public function convertToVectorDynamic($coordinates, $prefix = '') { |
136
|
16 |
|
$this->galaxy = isset($coordinates[$prefix . 'galaxy']) ? intval($coordinates[$prefix . 'galaxy']) : 0; |
137
|
16 |
|
$this->system = isset($coordinates[$prefix . 'system']) ? intval($coordinates[$prefix . 'system']) : 0; |
138
|
16 |
|
$this->planet = isset($coordinates[$prefix . 'planet']) ? intval($coordinates[$prefix . 'planet']) : 0; |
139
|
16 |
|
$this->type = isset($coordinates[$prefix . 'type']) |
140
|
16 |
|
? isset($coordinates[$prefix . 'type']) |
141
|
16 |
|
: (isset($coordinates[$prefix . 'planet_type']) ? intval($coordinates[$prefix . 'planet_type']) : 0); |
142
|
|
|
|
143
|
16 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $prefix |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function toArray($prefix = '') { |
152
|
|
|
$array = array( |
153
|
|
|
$prefix . 'galaxy' => $this->galaxy, |
154
|
|
|
$prefix . 'system' => $this->system, |
155
|
|
|
$prefix . 'planet' => $this->planet, |
156
|
|
|
$prefix . 'type' => $this->type, |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
return $array; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $coordinates |
164
|
|
|
* @param bool $returnZero |
165
|
|
|
* |
166
|
|
|
* @return int|number |
167
|
|
|
*/ |
168
|
8 |
|
public function distanceFromCoordinates($coordinates, $returnZero = false) { |
169
|
8 |
|
return $this->distance(static::convertToVector($coordinates), $returnZero); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $from |
174
|
|
|
* @param array $to |
175
|
|
|
* @param bool $returnZero |
176
|
|
|
* |
177
|
|
|
* @return int|number |
178
|
|
|
*/ |
179
|
|
|
public static function distanceBetweenCoordinates($from, $to, $returnZero = false) { |
180
|
|
|
return static::convertToVector($from)->distanceFromCoordinates($to, $returnZero); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array $planetRow |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function isSameLocation($planetRow) { |
189
|
|
|
return $this->distanceFromCoordinates($planetRow, true) == 0; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function isInUniverse() { |
196
|
|
|
return |
197
|
|
|
$this->galaxy >= 1 && $this->galaxy <= static::$knownGalaxies && |
198
|
|
|
$this->system >= 1 && $this->system <= static::$knownSystems; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function isInSystem() { |
205
|
|
|
return $this->planet >= 1 && $this->planet <= static::$knownPlanets; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function isInKnownSpace() { |
212
|
|
|
return $this->isInUniverse() && $this->isInSystem(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|