function ExpeditionBattle($fleet_id, $pirates, $level, $when) { global $db_prefix; global $GlobalUni; $fleetmap = array(202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215); $defmap = array(401, 402, 403, 404, 405, 406, 407, 408); $a_result = array(0 => "combatreport_ididattack_iwon", 1 => "combatreport_ididattack_ilost", 2 => "combatreport_ididattack_draw"); global $db_host, $db_user, $db_pass, $db_name, $db_prefix; $a = array(); $d = array(); $unitab = LoadUniverse(); $fid = $unitab['fid']; $did = $unitab['did']; $rf = $unitab['rapid']; // *** Союзные атаки не должны вступать битву. Игнорировать их. $f = LoadFleet($fleet_id); // *** Сгенерировать исходные данные // Список атакующих $anum = 0; $a[0] = LoadUser($f['owner_id']); $a[0]['fleet'] = array(); foreach ($fleetmap as $i => $gid) { $a[0]['fleet'][$gid] = abs($f["ship{$gid}"]); } $start_planet = GetPlanet($f['start_planet']); $a[0]['g'] = $start_planet['g']; $a[0]['s'] = $start_planet['s']; $a[0]['p'] = $start_planet['p']; $a[0]['id'] = $fleet_id; $a[0]['points'] = $a[0]['fpoints'] = 0; $anum++; // Список обороняющихся $dnum = 0; $d[0] = LoadUser(99999); if ($pirates) { $d[0]['oname'] = "Piraten"; $d[0]['r109'] = max(0, $a[0]['r109'] - 3); $d[0]['r110'] = max(0, $a[0]['r110'] - 3); $d[0]['r111'] = max(0, $a[0]['r111'] - 3); } else { $d[0]['oname'] = "Aliens"; $d[0]['r109'] = $a[0]['r109'] + 3; $d[0]['r110'] = $a[0]['r110'] + 3; $d[0]['r111'] = $a[0]['r111'] + 3; } $d[0]['fleet'] = array(); $d[0]['defense'] = array(); foreach ($fleetmap as $i => $gid) { // Определить состав флота пиратов / чужих if ($pirates) { // Пиратский флот, Округление состава флота вниз. // Нормальный - 30% +/- 3% от количества кораблей вашего флота + 5 ЛИ // Сильный - 50% +/- 5% от количества кораблей вашего флота + 3 Крейсера // Оч. Сильный - 80% +/- 8% от количества кораблей вашего флота + 2 Линка if ($a[0]['fleet'][$gid] > 0) { if ($level == 0) { $ratio = mt_rand(27, 33) / 100; } else { if ($level == 1) { $ratio = mt_rand(45, 55) / 100; } else { if ($level == 2) { $ratio = mt_rand(72, 88) / 100; } } } $d[0]['fleet'][$gid] = floor($a[0]['fleet'][$gid] * $ratio); } else { $d[0]['fleet'][$gid] = 0; } } else { // Флот Чужих, Округление состава флота вверх. // Нормальный - 40% +/- 4% от количества кораблей вашего флота + 5 ТИ // Сильный - 60% +/- 6% от количества кораблей вашего флота + 3 Линейки // Оч. Сильный - 90% +/- 9% от количества кораблей вашего флота + 2 Уника if ($a[0]['fleet'][$gid] > 0) { if ($level == 0) { $ratio = mt_rand(36, 44) / 100; } else { if ($level == 1) { $ratio = mt_rand(54, 66) / 100; } else { if ($level == 2) { $ratio = mt_rand(81, 99) / 100; } } } $d[0]['fleet'][$gid] = ceil($a[0]['fleet'][$gid] * $ratio); } else { $d[0]['fleet'][$gid] = 0; } } } if ($pirates) { if ($level == 0) { $d[0]['fleet'][204] += 5; } else { if ($level == 1) { $d[0]['fleet'][206] += 3; } else { if ($level == 2) { $d[0]['fleet'][207] += 2; } } } } else { if ($level == 0) { $d[0]['fleet'][205] += 5; } else { if ($level == 1) { $d[0]['fleet'][215] += 3; } else { if ($level == 2) { $d[0]['fleet'][213] += 2; } } } } foreach ($defmap as $i => $gid) { $d[0]['defense'][$gid] = 0; } $target_planet = GetPlanet($f['target_planet']); $d[0]['g'] = $target_planet['g']; $d[0]['s'] = $target_planet['s']; $d[0]['p'] = $target_planet['p']; $d[0]['id'] = $target_planet['planet_id']; $d[0]['points'] = $d[0]['fpoints'] = 0; $dnum++; $source .= "Rapidfire = {$rf}\n"; $source .= "FID = {$fid}\n"; $source .= "DID = {$did}\n"; $source .= "Attackers = " . $anum . "\n"; $source .= "Defenders = " . $dnum . "\n"; foreach ($a as $num => $attacker) { $source .= "Attacker" . $num . " = (<" . $attacker['oname'] . "> "; $source .= $attacker['id'] . " "; $source .= $attacker['g'] . " " . $attacker['s'] . " " . $attacker['p'] . " "; $source .= $attacker['r109'] . " " . $attacker['r110'] . " " . $attacker['r111'] . " "; foreach ($fleetmap as $i => $gid) { $source .= $attacker['fleet'][$gid] . " "; } $source .= ")\n"; } foreach ($d as $num => $defender) { $source .= "Defender" . $num . " = (<" . $defender['oname'] . "> "; $source .= $defender['id'] . " "; $source .= $defender['g'] . " " . $defender['s'] . " " . $defender['p'] . " "; $source .= $defender['r109'] . " " . $defender['r110'] . " " . $defender['r111'] . " "; foreach ($fleetmap as $i => $gid) { $source .= $defender['fleet'][$gid] . " "; } foreach ($defmap as $i => $gid) { $source .= $defender['defense'][$gid] . " "; } $source .= ")\n"; } $battle = array(null, $source, "", "", $when); $battle_id = AddDBRow($battle, "battledata"); $bf = fopen("battledata/battle_" . $battle_id . ".txt", "w"); fwrite($bf, $source); fclose($bf); // *** Передать данные боевому движку $arg = "\"battle_id={$battle_id}\""; system($unitab['battle_engine'] . " {$arg}"); // *** Обработать выходные данные $battleres = file_get_contents("battleresult/battle_" . $battle_id . ".txt"); $res = unserialize($battleres); // Определить исход битвы. if ($res['result'] === "awon") { $battle_result = 0; } else { if ($res['result'] === "dwon") { $battle_result = 1; } else { $battle_result = 2; } } // Рассчитать общие потери (учитывать дейтерий и восстановленную оборону) $aloss = $dloss = 0; $loss = CalcLosses($a, $d, $res, array()); $a = $loss['a']; $d = $loss['d']; $aloss = $loss['aloss']; $dloss = $loss['dloss']; // Сгенерировать боевой доклад. loca_add("battlereport", $GlobalUni['lang']); loca_add("technames", $GlobalUni['lang']); $text = ShortBattleReport($res, $when); // Разослать сообщения $mailbox = array(); // Если флот уничтожен за 1 или 2 раунда - не показывать лог боя для атакующих. if (count($res['rounds']) <= 2 && $battle_result == 1) { $text = "Контакт с флотом потерян. <br> Это означает, что его уничтожили первым же залпом <!--A:{$aloss},W:{$dloss}-->"; } foreach ($a as $i => $user) { if ($mailbox[$user['player_id']] == true) { continue; } $bericht = SendMessage($user['player_id'], "Командование флотом", "Боевой доклад", $text, 6, $when); MarkMessage($user['player_id'], $bericht); $subj = "<a href=\"#\" onclick=\"fenster(\\'index.php?page=bericht&session={PUBLIC_SESSION}&bericht={$bericht}\\', \\'Bericht_Kampf\\');\" ><span class=\"" . $a_result[$battle_result] . "\">Боевой доклад [" . $target_planet['g'] . ":" . $target_planet['s'] . ":" . $target_planet['p'] . "] (V:" . nicenum($dloss) . ",A:" . nicenum($aloss) . ")</span></a>"; SendMessage($user['player_id'], "Командование флотом", $subj, "", 2, $when); $mailbox[$user['player_id']] = true; } // Обновить лог боевого доклада $subj = "<a href=\"#\" onclick=\"fenster(\\'index.php?page=admin&session={PUBLIC_SESSION}&mode=BattleReport&bericht={$battle_id}\\', \\'Bericht_Kampf\\');\" ><span class=\"" . $a_result[$battle_result] . "\">Боевой доклад [" . $target_planet['g'] . ":" . $target_planet['s'] . ":" . $target_planet['p'] . "] (V:" . nicenum($dloss) . ",A:" . nicenum($aloss) . ")</span></a>"; $query = "UPDATE " . $db_prefix . "battledata SET title = '" . $subj . "', report = '" . $text . "' WHERE battle_id = {$battle_id};"; dbquery($query); // Почистить старые боевые доклады $ago = $when - 2 * 7 * 24 * 60 * 60; $query = "DELETE FROM " . $db_prefix . "battledata WHERE date < {$ago};"; dbquery($query); // Модифицировать флот WritebackBattleResultsExpedition($a, $d, $res); // Изменить статистику игроков foreach ($a as $i => $user) { AdjustStats($user['player_id'], $user['points'], $user['fpoints'], 0, '-'); } RecalcRanks(); // Чистим промежуточные данные боевого движка unlink("battledata/battle_" . $battle_id . ".txt"); unlink("battleresult/battle_" . $battle_id . ".txt"); return $battle_result; }
function SimBattle($a, $d, $rf, $fid, $did, $debug, &$battle_result, &$aloss, &$dloss) { global $db_prefix; global $GlobalUni; $fleetmap = array(202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215); $defmap = array(401, 402, 403, 404, 405, 406, 407, 408); $unitab = LoadUniverse(); if ($debug) { print_r($a); echo "<br>"; print_r($d); echo "<br><hr>"; } // *** Сгенерировать исходные данные $source .= "Rapidfire = {$rf}\n"; $source .= "FID = {$fid}\n"; $source .= "DID = {$did}\n"; $anum = count($a); $dnum = count($d); $source .= "Attackers = {$anum}\n"; $source .= "Defenders = {$dnum}\n"; for ($n = 0; $n < $anum; $n++) { $source .= "Attacker{$n} = (<Attacker{$n}> " . mt_rand(1, 10000) . " " . $a[$n]['g'] . " " . $a[$n]['s'] . " " . $a[$n]['p'] . " "; $source .= $a[$n]['r109'] . " " . $a[$n]['r110'] . " " . $a[$n]['r111'] . " "; foreach ($fleetmap as $i => $gid) { $source .= $a[$n]['fleet'][$gid] . " "; } $source .= ")\n"; } for ($n = 0; $n < $dnum; $n++) { $source .= "Defender{$n} = (<Defender{$n}> " . mt_rand(1, 10000) . " " . $d[$n]['g'] . " " . $d[$n]['s'] . " " . $d[$n]['p'] . " "; $source .= $d[$n]['r109'] . " " . $d[$n]['r110'] . " " . $d[$n]['r111'] . " "; foreach ($fleetmap as $i => $gid) { $source .= $d[$n]['fleet'][$gid] . " "; } foreach ($defmap as $i => $gid) { $source .= $d[$n]['defense'][$gid] . " "; } $source .= ")\n"; } if ($debug) { echo $source . "<hr>"; } $battle = array($battle_id, $source, '', '', time()); $battle_id = AddDBRow($battle, "battledata"); $bf = fopen("battledata/battle_" . $battle_id . ".txt", "w"); fwrite($bf, $source); fclose($bf); // *** Передать данные боевому движку $arg = "\"battle_id={$battle_id}\""; system($unitab['battle_engine'] . " {$arg}"); // *** Обработать выходные данные $battleres = file_get_contents("battleresult/battle_" . $battle_id . ".txt"); $res = unserialize($battleres); if ($debug) { print_r($battle); echo "<hr>"; print_r($res); echo "<hr>"; } // Удалить уже ненужные боевые данные. $query = "DELETE FROM " . $db_prefix . "battledata WHERE battle_id = {$battle_id}"; dbquery($query); // Восстановить оборону $repaired = RepairDefense($d, $res, $unitab['defrepair'], $unitab['defrepair_delta'], false); // Рассчитать общие потери $aloss = $dloss = 0; $loss = CalcLosses($a, $d, $res, $repaired); $a = $loss['a']; $d = $loss['d']; $aloss = $loss['aloss']; $dloss = $loss['dloss']; // Создать луну $mooncreated = false; $moonchance = min(floor(($res['dm'] + $res['dk']) / 100000), 20); if (mt_rand(1, 100) <= $moonchance) { $mooncreated = true; } if ($res['result'] === "awon") { $battle_result = 0; } else { if ($res['result'] === "dwon") { $battle_result = 1; } else { $battle_result = 2; } } // Сгенерировать боевой доклад. loca_add("battlereport", $GlobalUni['lang']); loca_add("technames", $GlobalUni['lang']); return BattleReport($res, time(), $aloss, $dloss, 1, 2, 3, $moonchance, $mooncreated, $repaired); }