PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/2.10.2/upload/includes/functions/calculateAttack.php

https://github.com/SantiHarry/XG-Proyect-v2.10.x
PHP | 324 lines | 255 code | 60 blank | 9 comment | 43 complexity | a1f189008a534c33299679d73f040e13 MD5 | raw file
  1. <?php
  2. /**
  3. * @project XG Proyect
  4. * @version 2.10.x build 0000
  5. * @copyright Copyright (C) 2008 - 2012
  6. */
  7. if(!defined('INSIDE')){ die(header("location:../../"));}
  8. function calculateAttack (&$attackers, &$defenders)
  9. {
  10. global $pricelist, $CombatCaps, $resource;
  11. $totalResourcePoints = array('attacker' => 0, 'defender' => 0);
  12. $resourcePointsAttacker = array('metal' => 0, 'crystal' => 0);
  13. foreach ($attackers as $fleetID => $attacker) {
  14. foreach ($attacker['detail'] as $element => $amount) {
  15. $resourcePointsAttacker['metal'] += $pricelist[$element]['metal'] * $amount;
  16. $resourcePointsAttacker['crystal'] += $pricelist[$element]['crystal'] * $amount ;
  17. $totalResourcePoints['attacker'] += $pricelist[$element]['metal'] * $amount ;
  18. $totalResourcePoints['attacker'] += $pricelist[$element]['crystal'] * $amount ;
  19. }
  20. }
  21. $resourcePointsDefender = array('metal' => 0, 'crystal' => 0);
  22. foreach ($defenders as $fleetID => $defender) {
  23. foreach ($defender['def'] as $element => $amount) { //Line20
  24. if ($element < 300) {
  25. $resourcePointsDefender['metal'] += $pricelist[$element]['metal'] * $amount ;
  26. $resourcePointsDefender['crystal'] += $pricelist[$element]['crystal'] * $amount ;
  27. $totalResourcePoints['defender'] += $pricelist[$element]['metal'] * $amount ;
  28. $totalResourcePoints['defender'] += $pricelist[$element]['crystal'] * $amount ;
  29. } else {
  30. if (!isset($originalDef[$element])) $originalDef[$element] = 0;
  31. $originalDef[$element] += $amount;
  32. $totalResourcePoints['defender'] += $pricelist[$element]['metal'] * $amount ;
  33. $totalResourcePoints['defender'] += $pricelist[$element]['crystal'] * $amount ;
  34. }
  35. }
  36. }
  37. $max_rounds = 6;
  38. for ($round = 0, $rounds = array(); $round < $max_rounds; $round++) {
  39. $attackDamage = array('total' => 0);
  40. $attackShield = array('total' => 0);
  41. $attackAmount = array('total' => 0);
  42. $defenseDamage = array('total' => 0);
  43. $defenseShield = array('total' => 0);
  44. $defenseAmount = array('total' => 0);
  45. $attArray = array();
  46. $defArray = array();
  47. foreach ($attackers as $fleetID => $attacker) {
  48. $attackDamage[$fleetID] = 0;
  49. $attackShield[$fleetID] = 0;
  50. $attackAmount[$fleetID] = 0;
  51. foreach ($attacker['detail'] as $element => $amount) {
  52. $attTech = (1 + (0.1 * ($attacker['user']['military_tech']))); //attaque
  53. $defTech = (1 + (0.1 * ($attacker['user']['defence_tech']))); //bouclier
  54. $shieldTech = (1 + (0.1 * ($attacker['user']['shield_tech']))); //coque
  55. $attackers[$fleetID]['techs'] = array($shieldTech, $defTech, $attTech);
  56. $thisAtt = $amount * ($CombatCaps[$element]['attack']) * $attTech * (rand(80, 120) / 100); //attaque
  57. $thisDef = $amount * ($CombatCaps[$element]['shield']) * $defTech ; //bouclier
  58. $thisShield = $amount * ($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10 * $shieldTech; //coque
  59. $attArray[$fleetID][$element] = array('def' => $thisDef, 'shield' => $thisShield, 'att' => $thisAtt);
  60. $attackDamage[$fleetID] += $thisAtt;
  61. $attackDamage['total'] += $thisAtt;
  62. $attackShield[$fleetID] += $thisDef;
  63. $attackShield['total'] += $thisDef;
  64. $attackAmount[$fleetID] += $amount;
  65. $attackAmount['total'] += $amount;
  66. }
  67. }
  68. foreach ($defenders as $fleetID => $defender) {
  69. $defenseDamage[$fleetID] = 0;
  70. $defenseShield[$fleetID] = 0;
  71. $defenseAmount[$fleetID] = 0;
  72. foreach ($defender['def'] as $element => $amount) {
  73. $attTech = (1 + (0.1 * ($defender['user']['military_tech']))); //attaquue
  74. $defTech = (1 + (0.1 * ($defender['user']['defence_tech']))); //bouclier
  75. $shieldTech = (1 + (0.1 * ($defender['user']['shield_tech']))); //coque
  76. $defenders[$fleetID]['techs'] = array($shieldTech, $defTech, $attTech);
  77. $thisAtt = $amount * ($CombatCaps[$element]['attack']) * $attTech * (rand(80, 120) / 100); //attaque
  78. $thisDef = $amount * ($CombatCaps[$element]['shield']) * $defTech ; //bouclier
  79. $thisShield = $amount * ($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10 * $shieldTech; //coque
  80. if ($element == 407 || $element == 408 ) $thisAtt = 0;
  81. $defArray[$fleetID][$element] = array('def' => $thisDef, 'shield' => $thisShield, 'att' => $thisAtt);
  82. $defenseDamage[$fleetID] += $thisAtt;
  83. $defenseDamage['total'] += $thisAtt;
  84. $defenseShield[$fleetID] += $thisDef;
  85. $defenseShield['total'] += $thisDef;
  86. $defenseAmount[$fleetID] += $amount;
  87. $defenseAmount['total'] += $amount;
  88. }
  89. }
  90. $rounds[$round] = array('attackers' => $attackers, 'defenders' => $defenders, 'attack' => $attackDamage, 'defense' => $defenseDamage, 'attackA' => $attackAmount, 'defenseA' => $defenseAmount, 'infoA' => $attArray, 'infoD' => $defArray);
  91. if ($defenseAmount['total'] <= 0 || $attackAmount['total'] <= 0) {
  92. break;
  93. }
  94. // Calculate hit percentages (ACS only but ok)
  95. $attackPct = array();
  96. foreach ($attackAmount as $fleetID => $amount) {
  97. if (!is_numeric($fleetID)) continue;
  98. $attackPct[$fleetID] = $amount / $attackAmount['total'];
  99. }
  100. $defensePct = array();
  101. foreach ($defenseAmount as $fleetID => $amount) {
  102. if (!is_numeric($fleetID)) continue;
  103. $defensePct[$fleetID] = $amount / $defenseAmount['total'];
  104. }
  105. // CALCUL DES PERTES !!!
  106. $attacker_n = array();
  107. $attacker_shield = 0;
  108. foreach ($attackers as $fleetID => $attacker) {
  109. $attacker_n[$fleetID] = array();
  110. foreach($attacker['detail'] as $element => $amount) {
  111. $defender_moc = $amount * ($defenseDamage['total'] * $attackPct[$fleetID]) / $attackAmount[$fleetID];
  112. if ($amount > 0) {
  113. if ($attArray[$fleetID][$element]['def']/$amount < $defender_moc) {
  114. $max_removePoints = floor($amount * $defenseAmount['total'] / $attackAmount[$fleetID] * $attackPct[$fleetID]);
  115. $defender_moc -= $attArray[$fleetID][$element]['def'];
  116. $attacker_shield += $attArray[$fleetID][$element]['def'];
  117. $ile_removePoints = floor($defender_moc / (($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10));
  118. if ($max_removePoints < 0) $max_removePoints = 0;
  119. if ($ile_removePoints < 0) $ile_removePoints = 0;
  120. if ($ile_removePoints > $max_removePoints) {
  121. $ile_removePoints = $max_removePoints;
  122. }
  123. $attacker_n[$fleetID][$element] = ceil($amount - $ile_removePoints);
  124. if ($attacker_n[$fleetID][$element] <= 0) {
  125. $attacker_n[$fleetID][$element] = 0;
  126. }
  127. } else {
  128. $attacker_n[$fleetID][$element] = round($amount);
  129. $attacker_shield += $defender_moc;
  130. }
  131. } else {
  132. $attacker_n[$fleetID][$element] = round($amount);
  133. $attacker_shield += $defender_moc;
  134. }
  135. }
  136. }
  137. $defender_n = array();
  138. $defender_shield = 0;
  139. foreach ($defenders as $fleetID => $defender) {
  140. $defender_n[$fleetID] = array();
  141. foreach($defender['def'] as $element => $amount) {
  142. $attacker_moc = $amount * ($attackDamage['total'] * $defensePct[$fleetID]) / $defenseAmount[$fleetID];
  143. if ($amount > 0) {
  144. if ($defArray[$fleetID][$element]['def']/$amount < $attacker_moc) {
  145. $max_removePoints = floor($amount * $attackAmount['total'] / $defenseAmount[$fleetID] * $defensePct[$fleetID]);
  146. $attacker_moc -= $defArray[$fleetID][$element]['def'];
  147. $defender_shield += $defArray[$fleetID][$element]['def'];
  148. $ile_removePoints = floor($attacker_moc / (($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10));
  149. if ($max_removePoints < 0) $max_removePoints = 0;
  150. if ($ile_removePoints < 0) $ile_removePoints = 0;
  151. if ($ile_removePoints > $max_removePoints) {
  152. $ile_removePoints = $max_removePoints;
  153. }
  154. $defender_n[$fleetID][$element] = ceil($amount - $ile_removePoints);
  155. if ($defender_n[$fleetID][$element] <= 0) {
  156. $defender_n[$fleetID][$element] = 0;
  157. }
  158. } else {
  159. $defender_n[$fleetID][$element] = round($amount);
  160. $defender_shield += $attacker_moc;
  161. }
  162. } else {
  163. $defender_n[$fleetID][$element] = round($amount);
  164. $defender_shield += $attacker_moc;
  165. }
  166. }
  167. }
  168. // "Rapidfire"
  169. foreach ($attackers as $fleetID => $attacker) {
  170. foreach ($defenders as $fleetID2 => $defender) {
  171. foreach($attacker['detail'] as $element => $amount) {
  172. if ($amount > 0) {
  173. foreach ($CombatCaps[$element]['sd'] as $c => $d) {
  174. if (isset($defender['def'][$c])) {
  175. if ($d > 0) {
  176. $e = ($d / $defender['techs'][0]) / ($defender['techs'][1] * $attacker['techs'][2]);
  177. $defender_n[$fleetID2][$c] -= ceil(($amount * $e * (rand(50,120)/ 100)/ 2) * $defensePct[$fleetID2] * ($amount / $attackAmount[$fleetID]));
  178. if ($defender_n[$fleetID2][$c] <= 0) {
  179. $defender_n[$fleetID2][$c] = 0;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. foreach($defender['def'] as $element => $amount) {
  187. if ($amount > 0) {
  188. foreach ($CombatCaps[$element]['sd'] as $c => $d) {
  189. if (isset($attacker['detail'][$c])) {
  190. if ($d > 0) {
  191. $e = ($d / $defender['techs'][0]) / ($defender['techs'][1] * $attacker['techs'][2]);
  192. $attacker_n[$fleetID][$c] -= ceil(($amount * $e * (rand(50,120)/ 100)/ 2) * $attackPct[$fleetID] * ($amount / $defenseAmount[$fleetID2]));
  193. if ($attacker_n[$fleetID][$c] <= 0) {
  194. $attacker_n[$fleetID][$c] = 0;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. $rounds[$round]['attackShield'] = $attacker_shield;
  204. $rounds[$round]['defShield'] = $defender_shield;
  205. foreach ($attackers as $fleetID => $attacker) {
  206. $attackers[$fleetID]['detail'] = array_map('round', $attacker_n[$fleetID]);
  207. }
  208. foreach ($defenders as $fleetID => $defender) {
  209. $defenders[$fleetID]['def'] = array_map('round', $defender_n[$fleetID]);
  210. }
  211. }
  212. if ($attackAmount['total'] <= 0) {
  213. $won = "r"; // defender
  214. } elseif ($defenseAmount['total'] <= 0) {
  215. $won = "a"; // attacker
  216. } else {
  217. $won = "w"; // draw
  218. $rounds[count($rounds)] = array('attackers' => $attackers, 'defenders' => $defenders, 'attack' => $attackDamage, 'defense' => $defenseDamage, 'attackA' => $attackAmount, 'defenseA' => $defenseAmount);
  219. }
  220. // CDR
  221. foreach ($attackers as $fleetID => $attacker) { // flotte attaquant en CDR
  222. foreach ($attacker['detail'] as $element => $amount) {
  223. $totalResourcePoints['attacker'] -= $pricelist[$element]['metal'] * $amount ;
  224. $totalResourcePoints['attacker'] -= $pricelist[$element]['crystal'] * $amount ;
  225. $resourcePointsAttacker['metal'] -= $pricelist[$element]['metal'] * $amount ;
  226. $resourcePointsAttacker['crystal'] -= $pricelist[$element]['crystal'] * $amount ;
  227. }
  228. }
  229. $resourcePointsDefenderDefs = array('metal' => 0, 'crystal' => 0);
  230. foreach ($defenders as $fleetID => $defender) {
  231. foreach ($defender['def'] as $element => $amount) {
  232. if ($element < 300) { // flotte defenseur en CDR
  233. $resourcePointsDefender['metal'] -= $pricelist[$element]['metal'] * $amount ;
  234. $resourcePointsDefender['crystal'] -= $pricelist[$element]['crystal'] * $amount ;
  235. $totalResourcePoints['defender'] -= $pricelist[$element]['metal'] * $amount ;
  236. $totalResourcePoints['defender'] -= $pricelist[$element]['crystal'] * $amount ;
  237. } else { // defs defenseur en CDR + reconstruction
  238. $totalResourcePoints['defender'] -= $pricelist[$element]['metal'] * $amount ;
  239. $totalResourcePoints['defender'] -= $pricelist[$element]['crystal'] * $amount ;
  240. if ( $user['rpg_technocrate'] == 1 )
  241. {
  242. $lost = floor ( ( $originalDef[$element] - $amount ) / ENGINEER_DEFENSE );
  243. }
  244. else
  245. {
  246. $lost = $originalDef[$element] - $amount;
  247. }
  248. $giveback = round ( $lost * ( rand ( 70 * 0.8 , 70 * 1.2 ) / 100 ) );
  249. $defenders[$fleetID]['def'][$element] += $giveback;
  250. $resourcePointsDefenderDefs['metal'] += $pricelist[$element]['metal'] * ($lost - $giveback) ;
  251. $resourcePointsDefenderDefs['crystal'] += $pricelist[$element]['crystal'] * ($lost - $giveback) ;
  252. }
  253. }
  254. }
  255. $game_fleet_cdr = read_config ( 'fleet_cdr' );
  256. $game_defs_cdr = read_config ( 'defs_cdr' );
  257. $totalLost = array('att' => $totalResourcePoints['attacker'], 'def' => $totalResourcePoints['defender']);
  258. $debAttMet = ($resourcePointsAttacker['metal'] * ($game_fleet_cdr / 100));
  259. $debAttCry = ($resourcePointsAttacker['crystal'] * ($game_fleet_cdr / 100));
  260. $debDefMet = ($resourcePointsDefender['metal'] * ($game_fleet_cdr / 100)) + ($resourcePointsDefenderDefs['metal'] * ($game_defs_cdr / 100));
  261. $debDefCry = ($resourcePointsDefender['crystal'] * ($game_fleet_cdr / 100)) + ($resourcePointsDefenderDefs['crystal'] * ($game_defs_cdr / 100));
  262. return array('won' => $won, 'debree' => array('att' => array($debAttMet, $debAttCry), 'def' => array($debDefMet, $debDefCry)), 'rw' => $rounds, 'lost' => $totalLost);
  263. }
  264. ?>