<?php
function calculateAttack (&$attackers, &$defenders) {
global $pricelist, $CombatCaps, $game_config, $resource;
$totalResourcePoints = array('attacker' => 0, 'defender' => 0);
$resourcePointsAttacker = array('metal' => 0, 'crystal' => 0);
foreach ($attackers as $fleetID => $attacker) {
foreach ($attacker['detail'] as $element => $amount) {
$resourcePointsAttacker['metal'] += $amount * $pricelist[$element]['metal'];
$resourcePointsAttacker['crystal'] += $amount * $pricelist[$element]['crystal'];
$totalResourcePoints['attacker'] += $amount * $pricelist[$element]['metal'];
$totalResourcePoints['attacker'] += $amount * $pricelist[$element]['crystal'];
}
}
$resourcePointsDefender = array('metal' => 0, 'crystal' => 0);
foreach ($defenders as $fleetID => $defender) {
foreach ($defender['def'] as $element => $amount) { //Line20
if ($element < 300) {
$resourcePointsDefender['metal'] += $amount * $pricelist[$element]['metal'];
$resourcePointsDefender['crystal'] += $amount * $pricelist[$element]['crystal'];
$totalResourcePoints['defender'] += $amount * $pricelist[$element]['metal'];
$totalResourcePoints['defender'] += $amount * $pricelist[$element]['crystal'];
} else {
if (!isset($originalDef[$element])) $originalDef[$element] = 0;
$originalDef[$element] += $amount;
}
}
}
$max_rounds = MAX_ATTACK_ROUNDS;
$max_rounds = 10;
for ($round = 0, $rounds = array(); $round < $max_rounds; $round++) {
$attackDamage = array('total' => 0);
$attackShield = array('total' => 0);
$attackAmount = array('total' => 0);
$defenseDamage = array('total' => 0);
$defenseShield = array('total' => 0);
$defenseAmount = array('total' => 0);
$attArray = array();
$defArray = array();
foreach ($attackers as $fleetID => $attacker) {
$attackDamage[$fleetID] = 0;
$attackShield[$fleetID] = 0;
$attackAmount[$fleetID] = 0;
foreach ($attacker['detail'] as $element => $amount) {
$defTech = (1 + (0.1 * ($attacker['user']['defence_tech']) + (0.05 * $attacker['rpg_amiral'])));
$shieldTech = (1 + (0.1 * ($attacker['user']['shield_tech']) + (0.05 * $attacker['rpg_amiral'])));
$attTech = (1 + (0.1 * ($attacker['user']['military_tech']) + (0.05 * $attacker['rpg_amiral'])));
$attackers[$fleetID]['techs'] = array($defTech, $shieldTech, $attTech);
$thisDef = $amount * ($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10 * $defTech * (rand(80, 120) / 100);
$thisShield = $amount * ($CombatCaps[$element]['shield']) * $shieldTech * (rand(80, 120) / 100);
$thisAtt = $amount * ($CombatCaps[$element]['attack']) * $attTech * (rand(80, 120) / 100);
$attArray[$fleetID][$element] = array('def' => $thisDef, 'shield' => $thisShield, 'att' => $thisAtt);
$attackDamage[$fleetID] += $thisAtt;
$attackDamage['total'] += $thisAtt;
$attackShield[$fleetID] += $thisDef;
$attackShield['total'] += $thisDef;
$attackAmount[$fleetID] += $amount;
$attackAmount['total'] += $amount;
}
}
foreach ($defenders as $fleetID => $defender) {
$defenseDamage[$fleetID] = 0;
$defenseShield[$fleetID] = 0;
$defenseAmount[$fleetID] = 0;
foreach ($defender['def'] as $element => $amount) { //Line80
$defTech = (1 + (0.1 * ($defender['user']['defence_tech']) + (0.05 * $defender['rpg_amiral'])));
$shieldTech = (1 + (0.1 * ($defender['user']['shield_tech']) + (0.05 * $defender['rpg_amiral'])));
$attTech = (1 + (0.1 * ($defender['user']['military_tech']) + (0.05 * $defender['rpg_amiral'])));
$defenders[$fleetID]['techs'] = array($defTech, $shieldTech, $attTech);
$thisDef = $amount * ($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10 * $defTech;
$thisShield = $amount * ($CombatCaps[$element]['shield']) * $shieldTech * (rand(80, 120) / 100);
$thisAtt = $amount * ($CombatCaps[$element]['attack']) * $attTech * (rand(80, 120) / 100);
if ($element == 407 || $element == 408) $thisAtt = 0;
$defArray[$fleetID][$element] = array('def' => $thisDef, 'shield' => $thisShield, 'att' => $thisAtt);
$defenseDamage[$fleetID] += $thisAtt;
$defenseDamage['total'] += $thisAtt;
$defenseShield[$fleetID] += $thisDef;
$defenseShield['total'] += $thisDef;
$defenseAmount[$fleetID] += $amount;
$defenseAmount['total'] += $amount;
}
}
$rounds[$round] = array('attackers' => $attackers, 'defenders' => $defenders, 'attack' => $attackDamage, 'defense' => $defenseDamage, 'attackA' => $attackAmount, 'defenseA' => $defenseAmount, 'infoA' => $attArray, 'infoD' => $defArray);
if ($defenseAmount['total'] <= 0 || $attackAmount['total'] <= 0) {
break;
}
// Calculate hit percentages (ACS only but ok)
$attackPct = array();
foreach ($attackAmount as $fleetID => $amount) {
if (!is_numeric($fleetID)) continue;
$attackPct[$fleetID] = $amount / $attackAmount['total'];
}
$defensePct = array();
foreach ($defenseAmount as $fleetID => $amount) {
if (!is_numeric($fleetID)) continue;
$defensePct[$fleetID] = $amount / $defenseAmount['total'];
}
$attacker_n = array();
$attacker_shield = 0;
foreach ($attackers as $fleetID => $attacker) {
$attacker_n[$fleetID] = array();
foreach($attacker['detail'] as $element => $amount) {
$defender_moc = $amount * ($defenseDamage['total'] * $attackPct[$fleetID]) / $attackAmount[$fleetID];
if ($amount > 0) {
if ($attArray[$fleetID][$element]['shield']/$amount < $defender_moc) {
$max_removePoints = floor($amount * $defenseAmount['total'] / $attackAmount[$fleetID] * $attackPct[$fleetID]);
$defender_moc -= $attArray[$fleetID][$element]['shield'];
$attacker_shield += $attArray[$fleetID][$element]['shield'];
$ile_removePoints = floor($defender_moc / (($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10));
if ($max_removePoints < 0) $max_removePoints = 0;
if ($ile_removePoints < 0) $ile_removePoints = 0;
if ($ile_removePoints > $max_removePoints) {
$ile_removePoints = $max_removePoints;
}
$attacker_n[$fleetID][$element] = ceil($amount - $ile_removePoints);
if ($attacker_n[$fleetID][$element] <= 0) {
$attacker_n[$fleetID][$element] = 0;
}
} else {
$attacker_n[$fleetID][$element] = round($amount);
$attacker_shield += $defender_moc;
}
} else {
$attacker_n[$fleetID][$element] = round($amount);
$attacker_shield += $defender_moc;
}
}
}
$defender_n = array();
$defender_shield = 0;
foreach ($defenders as $fleetID => $defender) {
$defender_n[$fleetID] = array();
foreach($defender['def'] as $element => $amount) {
$attacker_moc = $amount * ($attackDamage['total'] * $defensePct[$fleetID]) / $defenseAmount[$fleetID];
if ($amount > 0) {
if ($defArray[$fleetID][$element]['shield']/$amount < $attacker_moc) {
$max_removePoints = floor($amount * $attackAmount['total'] / $defenseAmount[$fleetID] * $defensePct[$fleetID]);
$attacker_moc -= $defArray[$fleetID][$element]['shield'];
$defender_shield += $defArray[$fleetID][$element]['shield'];
$ile_removePoints = floor(($attacker_moc / ((($pricelist[$element]['metal'] + $pricelist[$element]['crystal']) / 10) )));
if ($max_removePoints < 0) $max_removePoints = 0;
if ($ile_removePoints < 0) $ile_removePoints = 0;
if ($ile_removePoints > $max_removePoints) {
$ile_removePoints = $max_removePoints;
}
$defender_n[$fleetID][$element] = ceil($amount - $ile_removePoints);
if ($defender_n[$fleetID][$element] <= 0) {
$defender_n[$fleetID][$element] = 0;
}
} else {
$defender_n[$fleetID][$element] = round($amount);
$defender_shield += $attacker_moc;
}
} else {
$defender_n[$fleetID][$element] = round($amount);
$defender_shield += $attacker_moc;
}
}
}
$rounds[$round]['attackShield'] = $attacker_shield;
$rounds[$round]['defShield'] = $defender_shield;
foreach ($attackers as $fleetID => $attacker) {
$attackers[$fleetID]['detail'] = array_map('round', $attacker_n[$fleetID]);
}
foreach ($defenders as $fleetID => $defender) {
$defenders[$fleetID]['def'] = array_map('round', $defender_n[$fleetID]);
}
}
if ($attackAmount['total'] <= 0) {
$won = 2; // defender
} elseif ($defenseAmount['total'] <= 0) {
$won = 1; // attacker
} else {
$won = 0; // draw
$rounds[count($rounds)] = array('attackers' => $attackers, 'defenders' => $defenders, 'attack' => $attackDamage, 'defense' => $defenseDamage, 'attackA' => $attackAmount, 'defenseA' => $defenseAmount);
}
foreach ($attackers as $fleetID => $attacker) {
foreach ($attacker['detail'] as $element => $amount) {
$totalResourcePoints['attacker'] -= $pricelist[$element]['metal'] * $amount;
$totalResourcePoints['attacker'] -= $pricelist[$element]['crystal'] * $amount;
$resourcePointsAttacker['metal'] -= $pricelist[$element]['metal'] * $amount;
$resourcePointsAttacker['crystal'] -= $pricelist[$element]['crystal'] * $amount;
}
}
foreach ($defenders as $fleetID => $defender) {
foreach ($defender['def'] as $element => $amount) { //Line271
if ($element < 300) {
$resourcePointsDefender['metal'] -= $pricelist[$element]['metal'] * $amount;
$resourcePointsDefender['crystal'] -= $pricelist[$element]['crystal'] * $amount;
$totalResourcePoints['defender'] -= $pricelist[$element]['metal'] * $amount;
$totalResourcePoints['defender'] -= $pricelist[$element]['crystal'] * $amount;
} else {
$lost = $originalDef[$element] - $amount;
$giveback = $lost * (rand(70*0.8, 70*1.2) / 100);
$defenders[$fleetID]['def'][$element] += $giveback;
}
}
}
$totalLost = array('att' => $totalResourcePoints['attacker'], 'def' => $totalResourcePoints['defender']);
$debAttMet = ($resourcePointsAttacker['metal'] * 0.6);
$debAttCry = ($resourcePointsAttacker['crystal'] * 0.6);
$debDefMet = ($resourcePointsDefender['metal'] * 0.6);
$debDefCry = ($resourcePointsDefender['crystal'] * 0.6);
return array('won' => $won, 'debree' => array('att' => array($debAttMet, $debAttCry), 'def' => array($debDefMet, $debDefCry)), 'rw' => $rounds, 'lost' => $totalLost);
}
?>