1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 13.10.2017 2:34 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Meta\Economic; |
7
|
|
|
|
8
|
|
|
use \classSupernova; |
9
|
|
|
|
10
|
|
|
class ResourceCalculations { |
11
|
|
|
/** |
12
|
|
|
* @var float[][] $storageMatrix - [resourceId => [unitId => storageSize]] |
13
|
|
|
*/ |
14
|
|
|
public $storageMatrix = null; |
15
|
|
|
/** |
16
|
|
|
* @var float[][] $productionFullMatrix - [resourceId => [productionUnitId => productionAmount]] |
17
|
|
|
*/ |
18
|
|
|
public $productionFullMatrix = []; |
19
|
|
|
public $productionCurrentMatrix = []; |
20
|
|
|
public $efficiency = 0; |
21
|
|
|
public $energy = []; |
22
|
|
|
|
23
|
|
|
protected $groupModifiers = []; |
24
|
|
|
protected $groupPlanetDensities = []; |
25
|
|
|
|
26
|
|
|
protected static $groupFactories = []; |
27
|
|
|
|
28
|
|
|
protected static $storageCapacityFuncs = []; |
29
|
|
|
|
30
|
|
|
protected static $staticInitialized = false; |
31
|
|
|
protected static $mineSpeedCurrent = 1; |
32
|
|
|
protected static $mineSpeedNormal = 1; |
33
|
|
|
protected static $storageScaling = 1; |
34
|
|
|
|
35
|
|
|
protected static $basicPlanetIncomeTable = []; |
36
|
|
|
protected static $basicPlanetStorageTable = []; |
37
|
|
|
|
38
|
|
|
public static function initStatic() { |
39
|
|
|
if (static::$staticInitialized) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
static::$mineSpeedNormal = game_resource_multiplier(true); |
44
|
|
|
static::$mineSpeedCurrent = game_resource_multiplier(); |
45
|
|
|
static::$storageScaling = classSupernova::$config->eco_scale_storage ? static::$mineSpeedNormal : 1; |
|
|
|
|
46
|
|
|
|
47
|
|
|
static::$groupFactories = sn_get_groups('factories'); |
48
|
|
|
|
49
|
|
|
// Filling capacity functions list |
50
|
|
|
foreach (sn_get_groups('storages') as $unit_id) { |
51
|
|
|
foreach (get_unit_param($unit_id, P_STORAGE) as $resource_id => $function) { |
52
|
|
|
static::$storageCapacityFuncs[$unit_id][$resource_id] = $function; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
static::$basicPlanetIncomeTable[RES_METAL][0] = floatval(classSupernova::$config->metal_basic_income); |
|
|
|
|
57
|
|
|
static::$basicPlanetIncomeTable[RES_CRYSTAL][0] = floatval(classSupernova::$config->crystal_basic_income); |
|
|
|
|
58
|
|
|
static::$basicPlanetIncomeTable[RES_DEUTERIUM][0] = floatval(classSupernova::$config->deuterium_basic_income); |
|
|
|
|
59
|
|
|
static::$basicPlanetIncomeTable[RES_ENERGY][0] = floatval(classSupernova::$config->energy_basic_income); |
|
|
|
|
60
|
|
|
|
61
|
|
|
static::$basicPlanetStorageTable[RES_METAL][0] = floatval(classSupernova::$config->eco_planet_storage_metal); |
|
|
|
|
62
|
|
|
static::$basicPlanetStorageTable[RES_CRYSTAL][0] = floatval(classSupernova::$config->eco_planet_storage_crystal); |
|
|
|
|
63
|
|
|
static::$basicPlanetStorageTable[RES_DEUTERIUM][0] = floatval(classSupernova::$config->eco_planet_storage_deuterium); |
|
|
|
|
64
|
|
|
static::$basicPlanetStorageTable[RES_ENERGY][0] = 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function __construct() { |
68
|
|
|
static::initStatic(); |
69
|
|
|
|
70
|
|
|
$this->groupModifiers = sn_get_groups(GROUP_MODIFIERS_NAME); |
71
|
|
|
$this->groupPlanetDensities = sn_get_groups('planet_density'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function eco_get_planet_caps(&$user, &$planet_row, $production_time = 0) { |
|
|
|
|
75
|
|
|
// TODO Считать $production_time для термоядерной электростанции |
76
|
|
|
$this->storageMatrix = static::$basicPlanetStorageTable; |
77
|
|
|
foreach (static::$storageCapacityFuncs as $unit_id => $capacityFuncList) { |
78
|
|
|
foreach ($capacityFuncList as $resource_id => $function) { |
79
|
|
|
$this->storageMatrix[$resource_id][$unit_id] = floor(static::$storageScaling * |
80
|
|
|
mrc_modify_value($user, $planet_row, $this->groupModifiers[MODIFIER_RESOURCE_CAPACITY], $function(mrc_get_level($user, $planet_row, $unit_id))) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$planet_row['metal_max'] = $this->getStorage(RES_METAL); |
86
|
|
|
$planet_row['crystal_max'] = $this->getStorage(RES_CRYSTAL); |
87
|
|
|
$planet_row['deuterium_max'] = $this->getStorage(RES_DEUTERIUM); |
88
|
|
|
|
89
|
|
|
if ($planet_row['planet_type'] == PT_MOON) { |
90
|
|
|
$planet_row['metal_perhour'] = 0; |
91
|
|
|
$planet_row['crystal_perhour'] = 0; |
92
|
|
|
$planet_row['deuterium_perhour'] = 0; |
93
|
|
|
$planet_row['energy_used'] = 0; |
94
|
|
|
$planet_row['energy_max'] = 0; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->fillProductionMatrix($user, $planet_row); |
100
|
|
|
$this->applyDensityModifiers($planet_row['density_index']); |
101
|
|
|
$this->applyProductionSpeed(); |
102
|
|
|
$this->applyCapitalRates($user['id_planet'] == $planet_row['id']); |
103
|
|
|
$this->applyProductionModifiers($user, $planet_row); |
104
|
|
|
|
105
|
|
|
$this->productionCurrentMatrix = $this->productionFullMatrix; |
106
|
|
|
|
107
|
|
|
$this->calculateEnergyBalance(); |
108
|
|
|
$this->applyEfficiency(); |
109
|
|
|
|
110
|
|
|
$planet_row['metal_perhour'] = $this->getProduction(RES_METAL); |
111
|
|
|
$planet_row['crystal_perhour'] = $this->getProduction(RES_CRYSTAL); |
112
|
|
|
$planet_row['deuterium_perhour'] = $this->getProduction(RES_DEUTERIUM); |
113
|
|
|
|
114
|
|
|
$planet_row['energy_max'] = $this->energy[BUILD_CREATE]; |
115
|
|
|
$planet_row['energy_used'] = $this->energy[BUILD_DESTROY]; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getStorage($resourceId) { |
121
|
|
|
return |
122
|
|
|
is_array($this->storageMatrix[$resourceId]) && !empty($this->storageMatrix[$resourceId]) |
123
|
|
|
? array_sum($this->storageMatrix[$resourceId]) |
124
|
|
|
: 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getProductionFull($resourceId) { |
128
|
|
|
return |
129
|
|
|
is_array($this->productionFullMatrix[$resourceId]) && !empty($this->productionFullMatrix[$resourceId]) |
130
|
|
|
? array_sum($this->productionFullMatrix[$resourceId]) |
131
|
|
|
: 0; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getProduction($resourceId) { |
135
|
|
|
$production = is_array($this->productionCurrentMatrix[$resourceId]) && !empty($this->productionCurrentMatrix[$resourceId]) |
136
|
|
|
? array_sum($this->productionCurrentMatrix[$resourceId]) |
137
|
|
|
: 0; |
138
|
|
|
|
139
|
|
|
return $production >= 0 ? floor($production) : ceil($production); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Applying core resource production multipliers to all mining info, including basic mining |
144
|
|
|
* |
145
|
|
|
* @param $planetDensity |
146
|
|
|
*/ |
147
|
|
|
protected function applyDensityModifiers($planetDensity) { |
148
|
|
|
if (!empty($densityInfo = $this->groupPlanetDensities[$planetDensity][UNIT_RESOURCES])) { |
149
|
|
|
foreach ($densityInfo as $resourceId => $densityMultiplier) { |
150
|
|
|
if (!empty($this->productionFullMatrix[$resourceId])) { |
151
|
|
|
foreach ($this->productionFullMatrix[$resourceId] as $miningUnitId => &$miningAmount) { |
152
|
|
|
$miningAmount *= $densityMultiplier; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Applying game speed to mining rates |
161
|
|
|
* |
162
|
|
|
* @param bool $isCapital |
163
|
|
|
*/ |
164
|
|
|
protected function applyCapitalRates($isCapital) { |
165
|
|
|
if ( |
166
|
|
|
!$isCapital |
167
|
|
|
|| |
168
|
|
|
empty(classSupernova::$gc->config->planet_capital_mining_rate) |
169
|
|
|
|| |
170
|
|
|
classSupernova::$gc->config->planet_capital_mining_rate == 1 |
171
|
|
|
) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
foreach ($this->productionFullMatrix as $resourceId => &$miningData) { |
176
|
|
|
foreach ($miningData as $miningUnitId => &$miningAmount) { |
177
|
|
|
$miningAmount *= classSupernova::$gc->config->planet_capital_mining_rate; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Applying game speed to mining rates |
184
|
|
|
*/ |
185
|
|
|
protected function applyProductionSpeed() { |
186
|
|
|
foreach ($this->productionFullMatrix as $resourceId => &$miningData) { |
187
|
|
|
foreach ($miningData as $miningUnitId => &$miningAmount) { |
188
|
|
|
$miningAmount *= ($resourceId == RES_ENERGY ? static::$mineSpeedNormal : static::$mineSpeedCurrent); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Applying resource production modifiers |
195
|
|
|
* |
196
|
|
|
* @param $user |
197
|
|
|
* @param $planet_row |
198
|
|
|
*/ |
199
|
|
|
protected function applyProductionModifiers(&$user, &$planet_row) { |
200
|
|
|
foreach ($this->productionFullMatrix as &$resourceProductionTable) { |
201
|
|
|
foreach ($resourceProductionTable as $mineId => &$mineProduction) { |
202
|
|
|
$mineProduction = floor(mrc_modify_value($user, $planet_row, $this->groupModifiers[MODIFIER_RESOURCE_PRODUCTION], $mineProduction)); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
protected function calculateEnergyBalance() { |
208
|
|
|
if ($this->productionCurrentMatrix[RES_ENERGY][STRUC_MINE_FUSION]) { |
209
|
|
|
$deuterium_balance = array_sum($this->productionCurrentMatrix[RES_DEUTERIUM]); |
210
|
|
|
$energy_balance = array_sum($this->productionCurrentMatrix[RES_ENERGY]); |
211
|
|
|
if ($deuterium_balance < 0 || $energy_balance < 0) { |
212
|
|
|
$this->productionCurrentMatrix[RES_DEUTERIUM][STRUC_MINE_FUSION] = $this->productionCurrentMatrix[RES_ENERGY][STRUC_MINE_FUSION] = 0; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
foreach ($this->productionCurrentMatrix[RES_ENERGY] as $energy) { |
217
|
|
|
$this->energy[$energy >= 0 ? BUILD_CREATE : BUILD_DESTROY] += $energy; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->energy[BUILD_DESTROY] = -$this->energy[BUILD_DESTROY]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Calculate total efficiency and applying to production |
225
|
|
|
*/ |
226
|
|
|
protected function applyEfficiency() { |
227
|
|
|
$this->efficiency = $this->energy[BUILD_DESTROY] > $this->energy[BUILD_CREATE] |
228
|
|
|
? $this->energy[BUILD_CREATE] / $this->energy[BUILD_DESTROY] |
229
|
|
|
: 1; |
230
|
|
|
|
231
|
|
|
// If mines on 100% efficiency - doing nothing |
232
|
|
|
if ($this->efficiency == 1) { |
233
|
|
|
return; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Adjusting current production with efficiency |
237
|
|
|
foreach ($this->productionCurrentMatrix as $resource_id => &$resource_data) { |
238
|
|
|
foreach ($resource_data as $unit_id => &$resource_production) { |
239
|
|
|
if (!($unit_id == STRUC_MINE_FUSION && $resource_id == RES_DEUTERIUM) && $unit_id != 0 && !($resource_id == RES_ENERGY && $resource_production >= 0)) { |
240
|
|
|
$resource_production = $resource_production * $this->efficiency; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param $user |
248
|
|
|
* @param $planet_row |
249
|
|
|
*/ |
250
|
|
|
protected function fillProductionMatrix(&$user, &$planet_row) { |
251
|
|
|
$this->productionFullMatrix = static::$basicPlanetIncomeTable; |
252
|
|
|
foreach (static::$groupFactories as $unit_id) { |
253
|
|
|
$unit_data_production = get_unit_param($unit_id, P_UNIT_PRODUCTION); |
254
|
|
|
$unit_level = mrc_get_level($user, $planet_row, $unit_id); |
255
|
|
|
$unit_load = $planet_row[pname_factory_production_field_name($unit_id)]; |
256
|
|
|
|
257
|
|
|
foreach ($unit_data_production as $resource_id => $function) { |
258
|
|
|
$this->productionFullMatrix[$resource_id][$unit_id] = $function($unit_level, $unit_load, $user, $planet_row); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
|