| Conditions | 11 |
| Paths | 18 |
| Total Lines | 78 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 11 | function COE_missileAttack($defenceTech, $attackerTech, $MIPs, $structures, $targetedStructure = '0') { |
||
| 12 | // Here we select which part of defense should take damage: structure or shield |
||
| 13 | // $damageTo = P_SHIELD; |
||
| 14 | // $damageTo = P_STRUCTURE; |
||
| 15 | $damageTo = P_DEFENSE; |
||
| 16 | |||
| 17 | $mip_data = get_unit_param(UNIT_DEF_MISSILE_INTERPLANET); |
||
| 18 | $MIPDamage = floor(mrc_modify_value($attackerTech, false, TECH_WEAPON, $MIPs * $mip_data[P_ATTACK] * mt_rand(80, 120) / 100)); |
||
|
|
|||
| 19 | foreach ($structures as $key => $structure) { |
||
| 20 | $unit_info = get_unit_param($key); |
||
| 21 | $amplify = isset($mip_data[P_AMPLIFY][$key]) ? $mip_data[P_AMPLIFY][$key] : 1; |
||
| 22 | $structures[$key][P_SHIELD] = floor(mrc_modify_value($defenceTech, false, TECH_SHIELD, $unit_info[P_SHIELD]) / $amplify); |
||
| 23 | $structures[$key][P_STRUCTURE] = floor(mrc_modify_value($defenceTech, false, TECH_ARMOR, $unit_info[P_ARMOR]) / $amplify); |
||
| 24 | $structures[$key][P_DEFENSE] = floor(( |
||
| 25 | mrc_modify_value($defenceTech, false, TECH_ARMOR, $unit_info[P_ARMOR]) + |
||
| 26 | mrc_modify_value($defenceTech, false, TECH_SHIELD, $unit_info[P_SHIELD]) |
||
| 27 | ) / $amplify * mt_rand(80, 120) / 100); |
||
| 28 | } |
||
| 29 | |||
| 30 | $startStructs = $structures; |
||
| 31 | |||
| 32 | if ($targetedStructure) { |
||
| 33 | //attacking only selected structure |
||
| 34 | $damageDone = $structures[$targetedStructure][$damageTo]; |
||
| 35 | $structsDestroyed = min(floor($MIPDamage / $damageDone), $structures[$targetedStructure][0]); |
||
| 36 | $structures[$targetedStructure][0] -= $structsDestroyed; |
||
| 37 | $MIPDamage -= $structsDestroyed * $damageDone; |
||
| 38 | } else { |
||
| 39 | // REALLY random attack |
||
| 40 | $can_be_damaged = sn_get_groups('defense_active'); |
||
| 41 | //debug($structures); |
||
| 42 | //debug($can_be_damaged); |
||
| 43 | do { |
||
| 44 | // finding is there any structure that can be damaged with leftovers of $MIPDamage |
||
| 45 | foreach ($can_be_damaged as $key => $unit_id) { |
||
| 46 | //debug($structures[$unit_id][0]); |
||
| 47 | //debug($structures[$unit_id][$damageTo], $MIPDamage); |
||
| 48 | if ($structures[$unit_id][0] <= 0 || $structures[$unit_id][$damageTo] > $MIPDamage) { |
||
| 49 | unset($can_be_damaged[$key]); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | if (empty($can_be_damaged)) { |
||
| 53 | break; |
||
| 54 | } |
||
| 55 | sort($can_be_damaged); |
||
| 56 | //debug($can_be_damaged, 'can be damaged'); |
||
| 57 | $random_defense = mt_rand(0, count($can_be_damaged) - 1); |
||
| 58 | //debug($can_be_damaged[$random_defense], 'Target'); |
||
| 59 | $current_target = &$structures[$can_be_damaged[$random_defense]]; |
||
| 60 | //debug($current_target[0], 'Amount was'); |
||
| 61 | $can_be_destroyed = min($current_target[0], floor($MIPDamage / $current_target[$damageTo])); |
||
| 62 | //debug($MIPDamage, 'MIPDamage'); |
||
| 63 | //debug($can_be_destroyed, 'Can be destroyed'); |
||
| 64 | $destroyed = mt_rand(1, $can_be_destroyed); |
||
| 65 | $MIPDamage -= $current_target[$damageTo] * $destroyed; |
||
| 66 | $current_target[0] -= $destroyed; |
||
| 67 | //debug($destroyed, 'Actually destroyed'); |
||
| 68 | |||
| 69 | //print('<hr>'); |
||
| 70 | } while ($MIPDamage > 0 && !empty($can_be_damaged)); |
||
| 71 | //debug($MIPDamage, 'MIPDamage left'); |
||
| 72 | } |
||
| 73 | //debug($structures);//die(); |
||
| 74 | // 1/2 of metal and 1/4 of crystal of destroyed structures returns to planet |
||
| 75 | $metal = 0; |
||
| 76 | $crystal = 0; |
||
| 77 | foreach ($structures as $key => $structure) { |
||
| 78 | $unit_info = get_unit_param($key); |
||
| 79 | $destroyed = $startStructs[$key][0] - $structure[0]; |
||
| 80 | $metal += $destroyed * $unit_info[P_COST][RES_METAL] / 2; |
||
| 81 | $crystal += $destroyed * $unit_info[P_COST][RES_CRYSTAL] / 4; |
||
| 82 | } |
||
| 83 | |||
| 84 | $return['structures'] = $structures; // Structures left after attack |
||
| 85 | $return['metal'] = floor($metal); // Metal scraps |
||
| 86 | $return['crystal'] = floor($crystal); // Crystal scraps |
||
| 87 | |||
| 88 | return $return; |
||
| 89 | } |
||
| 202 |