<?php
//параметры
$basedmg = 330;
$needmana = 75;
$needint = 120;
$elem = 'w';
$t_min = 1;
$t_max = 4;
$count = mt_rand($t_min, $t_max);
$mag_id = 125;
$log_text = "<strong><font color=#0d2b99>Регенерация [3]</font></strong>";
$book = 909;
$have_book = mysql_fetch_array(mysql_query("SELECT `id` FROM `inventory` WHERE `prototype`='{$book}' AND `owner`='{$user['id']}' LIMIT 1;"));


if (empty($_SESSION['uid'])) {
    header("Location: index.php");
    exit;
}

$magic = mysql_fetch_array(mysql_query("SELECT `chanse` FROM `magic` WHERE `id` = '{$mag_id}' LIMIT 1 ;"));
$bat = mysql_fetch_array(mysql_query("SELECT * FROM `battle` WHERE `id`='{$user['battle']}';"));
//mana needed
$t1 = explode(";", $bat['t1']);
$t2 = explode(";", $bat['t2']);
if (in_array($user['id'], $t1)) $e_team = $t1;
if (in_array($user['id'], $t2)) $e_team = $t2;
foreach ($e_team as $k => $l) {
    if ($l > _BOTSEPARATOR_) $e_hp = mysql_fetch_array(mysql_query("SELECT `hp` FROM `bots` WHERE `id`='{$l}' LIMIT 1;"));
    else $e_hp = mysql_fetch_array(mysql_query("SELECT `hp` FROM `users` WHERE `id`='{$l}' LIMIT 1;"));
    if ($e_hp['hp'] < 1) unset($e_team[$k]);
}
shuffle($e_team);

if ($user['battle'] == 0) {
    echo "Это боевая магия...";
} elseif (!$have_book['id']) {
    echo("У Вас нет необходимой книги магии!");
} elseif ($user['mana'] < $needmana) {
    echo "Недостаточно энергии!";
} else {
    //attack script
    if ($user['intel'] >= $needint) {
        $int = $magic['chanse'] + ($user['intel'] - $needint) * 3;
        if ($int > 98) {
            $int = 99;
        }
    } else {
        $int = 0;
    }

    if (rand(1, 100) < $int) {
        global $elem_align, $elem_bonus;
        $user_elem = element(star_sign(substr($user['borndate'], 3, 2), substr($user['borndate'], 0, 2)));

        switch ($user_elem) {
            case 'f':
                $uel = 'mfire';
                break;
            case 'a':
                $uel = 'mair';
                break;
            case 'w':
                $uel = 'mwater';
                break;
            case 'e':
                $uel = 'mearth';
                break;
        }
        switch ($elem) {
            case 'f':
                $sel = 'mfire';
                break;
            case 'a':
                $sel = 'mair';
                break;
            case 'w':
                $sel = 'mwater';
                break;
            case 'e':
                $sel = 'mearth';
                break;
        }
        $sex_action = "применил";
        if ($user['sex'] == 0) $sex_action .= "а";
        addlog($user['battle'], '<span class=date>' . date("H:i") . '</span> ' . Nick::id($user['id'])->short() . ' ' . $sex_action . $targeted . ' заклинание ' . $log_text . '<BR>');

        //mass magic
        mysql_query("UPDATE `users` SET `mana` = `mana`-'" . $needmana . "' WHERE `id` = '" . $user['id'] . "';");
        foreach ($e_team as $v)
            if ($count > 0) {
                $count--;
                $bat = mysql_fetch_array(mysql_query("SELECT * FROM `battle` WHERE `id`='{$user['battle']}';"));
                $us = mysql_fetch_array(mysql_query("SELECT *,(select `id` from `online` WHERE `real_time` >= " . (time() - 60) . " AND `id` = users.`id`) as `online`  FROM `users` WHERE `id` = '{$v}' LIMIT 1;"));
                if (!$us) {
                    $bot = mysql_fetch_array(mysql_query("SELECT * FROM `bots` WHERE `id` = '{$v}' LIMIT 1;"));
                    $b_maxhp = mysql_fetch_array(mysql_query("SELECT `maxhp` FROM `users`WHERE `id`='{$bot['prototype']}'  LIMIT 1;"));
                    $b_maxhp = $b_maxhp['maxhp'];
                }
                //dmg=base+INT/LVL+X_magic
                $dmg = $basedmg + ($user['intel'] / $user['level']) + $user[$sel];
                //MF
                $dmg *= $elem_align[$user_elem][$elem];
                if ($us) $dmg *= $elem_bonus[element(star_sign(substr($user['borndate'], 3, 2), substr($user['borndate'], 0, 2)))][element(star_sign(substr($us['borndate'], 3, 2), substr($us['borndate'], 0, 2)))];
                //rand +- 25%
                $dmg = mt_rand(0.75 * $dmg, 1.25 * $dmg);
                $dmg = round($dmg);
                if ($us) $delta = $us['maxhp'] - $us['hp'];
                else $delta = $b_maxhp - $bot['hp'];
                if ($delta < $dmg) $dmg = floor($delta);
                if ($dmg < 1) $dmg = 0;

                if ($us) $target = $us['id'];
                else $target = $bot['id'];

                $targeted = '';
                //dmg >0 - go
                $all_dmg = unserialize($bat['damage']);
                $all_exp = unserialize($bat['exp']);
                $all_dmg[$user['id']] += $dmg;
                $all_exp[$user['id']] += SolveExp($user['id'], $target, $dmg);

                //сработало
                if ($dmg > 0) $hit = '-1'; else $hit = 0;
                $action = preg_replace("/Х/", Nick::id($target)->short(), magic_log($elem, $hit, $target));
                if ($dmg > 0) {
                    if ($us)
                        mysql_query("UPDATE `users` SET `hp` = `hp`+'" . $dmg . "' WHERE `id` = '" . $target . "';");
                    else
                        mysql_query("UPDATE `bots` SET `hp` = `hp`+'" . $dmg . "' WHERE `id` = '" . $target . "';");
                    mysql_query("UPDATE `battle` SET `damage`='" . serialize($all_dmg) . "', `exp`='" . serialize($all_exp) . "' WHERE `id`='{$user['battle']}';");

                    addlog($user['battle'], '<span class=date>' . date("H:i") . '</span> ' . $action . ' <strong>+' . $dmg . '</strong><BR>');

                    $bet = 1;
                } else //не сработало
                {
                    addlog($user['battle'], '<span class=date>' . date("H:i") . '</span> ' . $action . '<BR>');
                    $bet = 1;
                }
            }

    } else {
        echo "Свиток рассыпался в ваших руках...";
        $bet = 1;
    }
}