Issues (1369)

includes/general/general_unitFunctions.php (3 issues)

1
<?php
2
3
use Unit\DBStaticUnit;
4
5
/**
6
 * Created by Gorlum 04.12.2017 4:32
7
 */
8
9
function eco_get_total_cost($unit_id, $unit_level) {
10
  static $rate, $sn_group_resources_all, $sn_group_resources_loot;
11
  if (!$rate) {
12
    $sn_group_resources_all = sn_get_groups('resources_all');
13
    $sn_group_resources_loot = sn_get_groups('resources_loot');
14
15
    $rate[RES_METAL] = SN::$config->rpg_exchange_metal;
16
    $rate[RES_CRYSTAL] = SN::$config->rpg_exchange_crystal / SN::$config->rpg_exchange_metal;
17
    $rate[RES_DEUTERIUM] = SN::$config->rpg_exchange_deuterium / SN::$config->rpg_exchange_metal;
18
  }
19
20
  $unit_cost_data = get_unit_param($unit_id, 'cost');
21
  if (!is_array($unit_cost_data)) {
22
    return array('total' => 0);
23
  }
24
  $factor = isset($unit_cost_data['factor']) ? $unit_cost_data['factor'] : 1;
25
  $cost_array = array(BUILD_CREATE => array(), 'total' => 0);
26
  $unit_level = $unit_level > 0 ? $unit_level : 0;
27
  foreach ($unit_cost_data as $resource_id => $resource_amount) {
28
    if (!in_array($resource_id, $sn_group_resources_all)) {
29
      continue;
30
    }
31
    $cost_array[BUILD_CREATE][$resource_id] = round($resource_amount * ($factor == 1 ? $unit_level : ((1 - pow($factor, $unit_level)) / (1 - $factor))));
32
    if (in_array($resource_id, $sn_group_resources_loot)) {
33
      $cost_array['total'] += $cost_array[BUILD_CREATE][$resource_id] * $rate[$resource_id];
34
    }
35
  }
36
37
  return $cost_array;
38
}
39
40
function sn_unit_purchase($unit_id) { }
41
42
function sn_unit_relocate($unit_id, $from, $to) { }
43
44
/**
45
 * @param array $user
46
 * @param array $planet     ([])
47
 * @param int   $unit_id
48
 * @param bool  $for_update (false)
49
 * @param bool  $plain      (false)
50
 *
51
 * @return int|float|bool
52
 *
53
 * @see sn_mrc_get_level()
54
 */
55
function mrc_get_level(&$user, $planet = [], $unit_id, $for_update = false, $plain = false) {
56
  $result = null;
57
58
  return sn_function_call(__FUNCTION__, [&$user, $planet, $unit_id, $for_update, $plain, &$result]);
59
}
60
61
/**
62
 * @param $user
63
 * @param $planet
64
 * @param $unit_id
65
 * @param $for_update
66
 * @param $plain
67
 * @param $result
68
 *
69
 * @return int|mixed
70
 *
71
 * @see mrc_get_level()
72
 */
73
function sn_mrc_get_level(&$user, $planet = [], $unit_id, $for_update = false, $plain = false, &$result) {
74
  $mercenary_level = 0;
75
  $unit_db_name = pname_resource_name($unit_id);
76
77
  if (in_array($unit_id, sn_get_groups(array('plans', 'mercenaries', 'tech', 'artifacts')))) {
78
    $unit = !empty($user['id']) ? DBStaticUnit::db_unit_by_location($user['id'], LOC_USER, $user['id'], $unit_id) : 0;
79
    $mercenary_level = !empty($unit['unit_level']) ? $unit['unit_level'] : 0;
80
  } elseif (in_array($unit_id, sn_get_groups(array('structures', 'fleet', 'defense')))) {
81
    $unit = DBStaticUnit::db_unit_by_location(is_array($user) ? $user['id'] : $planet['id_owner'], LOC_PLANET, $planet['id'], $unit_id);
82
    $mercenary_level = !empty($unit['unit_level']) ? $unit['unit_level'] : 0;
83
  } elseif (in_array($unit_id, sn_get_groups('governors'))) {
84
    $mercenary_level = $unit_id == $planet['PLANET_GOVERNOR_ID'] ? $planet['PLANET_GOVERNOR_LEVEL'] : 0;
85
  } elseif ($unit_id == RES_DARK_MATTER) {
86
    $mercenary_level = $user[$unit_db_name] + ($plain || $user['user_as_ally'] ? 0 : SN::$auth->account->account_metamatter);
87
  } elseif ($unit_id == RES_METAMATTER) {
88
    $mercenary_level = SN::$auth->account->account_metamatter; //$user[$unit_db_name];
89
  } elseif (in_array($unit_id, sn_get_groups(array('resources_loot'))) || $unit_id == UNIT_SECTOR) {
90
    $mercenary_level = !empty($planet[$unit_db_name]) ? $planet[$unit_db_name] : $user[$unit_db_name];
91
  }
92
93
  return $result = $mercenary_level;
94
}
95
96
function mrc_modify_value(&$user, $planet = array(), $mercenaries, $value) { return sn_function_call('mrc_modify_value', array(&$user, $planet, $mercenaries, $value)); }
97
98
function sn_mrc_modify_value(&$user, $planet = array(), $mercenaries, $value, $base_value = null) {
99
  if (!is_array($mercenaries)) {
100
    $mercenaries = array($mercenaries);
101
  }
102
103
  $base_value = isset($base_value) ? $base_value : $value;
104
105
  foreach ($mercenaries as $mercenary_id) {
106
    $mercenary_level = mrc_get_level($user, $planet, $mercenary_id);
107
108
    $mercenary = get_unit_param($mercenary_id);
109
    $mercenary_bonus = $mercenary[P_BONUS_VALUE];
110
111
    switch ($mercenary[P_BONUS_TYPE]) {
112
      case BONUS_PERCENT:
113
        $mercenary_level = $mercenary_bonus < 0 && $mercenary_level * $mercenary_bonus < -90 ? -90 / $mercenary_bonus : $mercenary_level;
114
        $value += $base_value * $mercenary_level * $mercenary_bonus / 100;
115
      break;
116
117
      case BONUS_ADD:
118
        $value += $mercenary_level * $mercenary_bonus;
119
      break;
120
121
      case BONUS_ABILITY:
122
        $value = $mercenary_level ? $mercenary_level : 0;
123
      break;
124
125
      default:
126
      break;
127
    }
128
  }
129
130
  return $value;
131
}
132
133
function sys_unit_str2arr($fleet_string) {
134
  $fleet_array = array();
135
  if (!empty($fleet_string)) {
136
    $arrTemp = explode(';', $fleet_string);
137
    foreach ($arrTemp as $temp) {
138
      if ($temp) {
139
        $temp = explode(',', $temp);
140
        if (!empty($temp[0]) && !empty($temp[1])) {
141
          $fleet_array[$temp[0]] += $temp[1];
142
        }
143
      }
144
    }
145
  }
146
147
  return $fleet_array;
148
}
149
150
function sys_unit_arr2str($unit_list) {
151
  $fleet_string = array();
152
  if (isset($unit_list)) {
153
    if (!is_array($unit_list)) {
154
      $unit_list = array($unit_list => 1);
155
    }
156
157
    foreach ($unit_list as $unit_id => $unit_count) {
158
      if ($unit_id && $unit_count) {
159
        $fleet_string[] = "{$unit_id},{$unit_count}";
160
      }
161
    }
162
  }
163
164
  return implode(';', $fleet_string);
165
}
166
167
// TODO Для полноценного функионирования апдейтера пакет функций, включая эту должен быть вынесен раньше - или грузить general.php до апдейтера
168
function sys_get_unit_location($user, $planet, $unit_id) { return sn_function_call('sys_get_unit_location', array($user, $planet, $unit_id)); }
169
170
function sn_sys_get_unit_location($user, $planet, $unit_id) {
171
  return get_unit_param($unit_id, 'location');
172
}
173
174
175
function get_engine_data($user, $engine_info, $user_tech_level = null) {
176
  $sn_data_tech_bonus = get_unit_param($engine_info['tech'], P_BONUS_VALUE);
177
178
  $user_tech_level = $user_tech_level === null ? intval(mrc_get_level($user, false, $engine_info['tech'])) : $user_tech_level;
0 ignored issues
show
false of type false is incompatible with the type array expected by parameter $planet of mrc_get_level(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
  $user_tech_level = $user_tech_level === null ? intval(mrc_get_level($user, /** @scrutinizer ignore-type */ false, $engine_info['tech'])) : $user_tech_level;
Loading history...
179
180
  $engine_info['speed_base'] = $engine_info['speed'];
181
  $tech_bonus = ($user_tech_level - $engine_info['min_level']) * $sn_data_tech_bonus / 100;
182
  $tech_bonus = $tech_bonus < -0.9 ? -0.95 : $tech_bonus;
183
  $engine_info['speed'] = floor(mrc_modify_value($user, false, array(MRC_NAVIGATOR), $engine_info['speed']) * (1 + $tech_bonus));
184
185
  $engine_info['consumption_base'] = $engine_info['consumption'];
186
  $tech_bonus = ($user_tech_level - $engine_info['min_level']) * $sn_data_tech_bonus / 1000;
187
  $tech_bonus = $tech_bonus > 0.5 ? 0.5 : ($tech_bonus < 0 ? $tech_bonus * 2 : $tech_bonus);
188
  $engine_info['consumption'] = ceil($engine_info['consumption'] * (1 - $tech_bonus));
189
190
  return $engine_info;
191
}
192
193
function get_ship_data($ship_id, $user) {
194
  $ship_data = array();
195
  if (in_array($ship_id, sn_get_groups(array('fleet', 'missile')))) {
196
    foreach (get_unit_param($ship_id, 'engine') as $engine_info) {
197
      $tech_level = intval(mrc_get_level($user, false, $engine_info['tech']));
0 ignored issues
show
false of type false is incompatible with the type array expected by parameter $planet of mrc_get_level(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

197
      $tech_level = intval(mrc_get_level($user, /** @scrutinizer ignore-type */ false, $engine_info['tech']));
Loading history...
198
      if (empty($ship_data) || $tech_level >= $engine_info['min_level']) {
199
        $ship_data = $engine_info;
200
        $ship_data['tech_level'] = $tech_level;
201
      }
202
    }
203
    $ship_data = get_engine_data($user, $ship_data);
204
    $ship_data['capacity'] = get_unit_param($ship_id, 'capacity');
205
  }
206
207
  return $ship_data;
208
}
209
210
/**
211
 * Get unit info by unit's SN ID
212
 *
213
 * @param int $unitSnId
214
 *
215
 * @return mixed
216
 */
217
function getUnitInfo($unitSnId) {
218
  return get_unit_param($unitSnId);
219
}
220
221
/**
222
 * @param $unit_id
223
 * @param $param_name
224
 * @param $user
225
 * @param $planet
226
 *
227
 * @return mixed|null
228
 *
229
 * @see sn_get_unit_param()
230
 */
231
function get_unit_param($unit_id, $param_name = null, $user = null, $planet = null) {
232
  $result = null;
233
234
  return sn_function_call('get_unit_param', array($unit_id, $param_name, $user, $planet, &$result));
235
}
236
237
/**
238
 * @param $unit_id
239
 * @param $param_name
240
 * @param $user
241
 * @param $planet
242
 * @param $result
243
 *
244
 * @return array|mixed
245
 *
246
 * @see get_unit_param()
247
 */
248
function sn_get_unit_param($unit_id, $param_name = null, $user = null, $planet = null, &$result) {
249
  global $sn_data;
250
251
  $result = isset($sn_data[$unit_id])
252
    ? ($param_name === null
253
      ? $sn_data[$unit_id]
254
      : (isset($sn_data[$unit_id][$param_name]) ? $sn_data[$unit_id][$param_name] : $result)
255
    )
256
    : $result;
257
258
  return $result;
259
}
260
261
/**
262
 * @param string|string[] $groups
263
 *
264
 * @return array|array[]
265
 */
266
function sn_get_groups($groups) {
267
  $result = null;
268
269
  return sn_function_call('sn_get_groups', array($groups, &$result));
270
}
271
272
function sn_sn_get_groups($groups, &$result) {
273
  $result = is_array($result) ? $result : array();
274
  foreach ($groups = is_array($groups) ? $groups : array($groups) as $group_name) {
0 ignored issues
show
The assignment to $groups is dead and can be removed.
Loading history...
275
    $result += is_array($a_group = get_unit_param(UNIT_GROUP, $group_name)) ? $a_group : array();
276
  }
277
278
  return $result;
279
}
280
281
282
function unit_requirements_render($user, $planetrow, $unit_id, $field = P_REQUIRE) {
283
  $result = null;
284
285
  return sn_function_call('unit_requirements_render', array($user, $planetrow, $unit_id, $field, &$result));
286
}
287
288
function sn_unit_requirements_render($user, $planetrow, $unit_id, $field = P_REQUIRE, &$result) {
289
  global $lang, $config;
290
291
  $sn_data_unit = get_unit_param($unit_id);
292
293
  $result = is_array($result) ? $result : array();
294
  if ($sn_data_unit[$field] && !($sn_data_unit[P_UNIT_TYPE] == UNIT_MERCENARIES && SN::$config->empire_mercenary_temporary)) {
295
    foreach ($sn_data_unit[$field] as $require_id => $require_level) {
296
      $level_got = mrc_get_level($user, $planetrow, $require_id);
297
      $level_basic = mrc_get_level($user, $planetrow, $require_id, false, true);
298
      $result[] = array(
299
        'NAME'             => $lang['tech'][$require_id],
300
        //'CLASS' => $require_level > $level_got ? 'negative' : ($require_level == $level_got ? 'zero' : 'positive'),
301
        'REQUEREMENTS_MET' => intval($require_level <= $level_got ? REQUIRE_MET : REQUIRE_MET_NOT),
302
        'LEVEL_REQUIRE'    => $require_level,
303
        'LEVEL'            => $level_got,
304
        'LEVEL_BASIC'      => $level_basic,
305
        'LEVEL_BONUS'      => max(0, $level_got - $level_basic),
306
        'ID'               => $require_id,
307
      );
308
    }
309
  }
310
311
  return $result;
312
}
313
314
/**
315
 * @param array $cost        - [(int)resourceId => (float)unitAmount] => [RES_CRYSTAL => 100]
316
 * @param int   $in_resource - RES_METAL...
317
 *
318
 * @return float|int
319
 */
320
function get_unit_cost_in($cost, $in_resource = RES_METAL) {
321
  static $rates;
322
323
  if (!$rates) {
324
    $rates = SN::$gc->economicHelper->getResourcesExchange();
325
  }
326
327
  unset($cost[P_FACTOR]);
328
329
  $mainResourceExchange = !empty($rates[$in_resource]) ? $rates[$in_resource] : 1;
330
  $metal_cost = 0;
331
  foreach ($cost as $resource_id => $resource_value) {
332
    if (empty($rates[$resource_id])) {
333
      continue;
334
    }
335
336
    $metal_cost += $rates[$resource_id] / $mainResourceExchange * $resource_value;
337
  }
338
339
  return $metal_cost;
340
}
341
342
/**
343
 * Calculates cost of STACKABLE unit in specified resource
344
 *
345
 * @param int|float[] $units - unit ID or array [unitId => unitAmount]
346
 * @param int         $costResourceId
347
 *
348
 * @return float|int
349
 */
350
function getStackableUnitsCost($units, $costResourceId = RES_METAL) {
351
  static $costCache;
352
353
  $result = 0;
354
355
  if (!is_array($units)) {
356
    $units = [$units => 1];
357
  }
358
359
  foreach ($units as $unitId => $unitAmount) {
360
    if (!isset($costCache[$unitId][$costResourceId])) {
361
      $unitInfo = get_unit_param($unitId);
362
363
      $costCache[$unitId][$costResourceId] = !empty($unitInfo[P_COST]) ? get_unit_cost_in($unitInfo[P_COST], $costResourceId) : 0;
364
    }
365
366
    $result += $costCache[$unitId][$costResourceId] * $unitAmount;
367
  }
368
369
  return $result;
370
}
371