<?php
//параметры
        $basedmg=110;
        $needmana=20;
        $needint=80;
        $elem='f';
        $mag_id=76;
        $log_text="<strong><font color=#df0d0d>Вспышка [1]</font></strong>";
        $book=866;$have_book=mysql_fetch_array(mysql_query("SELECT `id` FROM `inventory` WHERE `prototype`='{$book}' AND `owner`='{$user['id']}' LIMIT 1;")); 


if ($_SESSION['uid'] == null) header("Location: index.php");
$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 `login` = '{$_POST['target']}' LIMIT 1;"));
$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']}';"));		
if(!$us) $bot= mysql_fetch_array(mysql_query ("SELECT * FROM `bots` WHERE `name` = '".$_POST['target']."' LIMIT 1;"));
//mana needed
$t1 = explode(";",$bat['t1']);
$t2 = explode(";",$bat['t2']);
if($us) $e_id=$us['id'];
else $e_id=$bot['id'];
if ($user['battle'] == 0) {
	echo "Это боевая магия...";
} elseif (!$have_book['id']) {
    echo("У Вас нет необходимой книги магии!"); 
} elseif((in_array ($user['id'],$t1)&&in_array ($e_id,$t1)) || (in_array ($user['id'],$t2)&&in_array ($e_id,$t2))) {
    echo "Персонаж в Вашей команде!";
} elseif($user['login']==$_POST['target']) {
    echo "Герр Леопольд фон Захер Мазох был бы доволен Вами...";
} elseif ($us && $us['battle']!=$user['battle']) {
	echo "Персонаж не в Вашем бою!";
} elseif ($user['mana']<$needmana) {
	echo "Недостаточно энергии!";
} elseif (!$us['online']&&!$bot) {
    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;
        };
        //dmg=base+INT/LVL+X_magic
        $dmg=$basedmg+($user['intel']/4)+$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)))];
        //- MF
        if($us) {
            $def=magic_def($elem,$_POST['target'],$user_elem);$dmg*=(1-magic_def($elem,$us['login'],$user_elem,1)/100);
            $dmg-=$def;
        }
        
        //rand +- 25%
        $dmg=mt_rand(0.75*$dmg,1.25*$dmg);
        $dmg=round($dmg);
        if($us) $target=$us['id'];
        else $target=$bot['id'];
        // команды
		$t1 = explode(";",$bat['t1']);
		$t2 = explode(";",$bat['t2']);
		if (in_array ($user['id'],$t1)) {
			 $us_team = "B1";
			$tar_team = "B2";
			}
        else {
			 $us_team = "B2";
			$tar_team = "B1";
			}
            
        $targeted=' на '.nick::id($target)->short();
        //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);
         $sex_action="применил";if($user['sex']==0) $sex_action.="а";
        if(!(($us['id']>0 && $us['hp']<1) || ($bot['id']>0 && $bot['hp']<1)))
         addlog($user['battle'],'<span class=date>'.date("H:i").'</span> '.nick::id($user['id'])->short().' '.$sex_action.$targeted.' заклинание '.$log_text.'<BR>');
        else 
         echo "Не нарушайте покой мертвых!";
        //сработало
        if($dmg>0) $hit=1; else $hit=0;
            $action=preg_replace("/Х/",nick::id($target)->short(),magic_log($elem,$hit,$target));
        //проверка hp>0
        if(!(($us['id']>0 && $us['hp']<1) || ($bot['id']>0 && $bot['hp']<1)))
        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']}';");
            mysql_query ("UPDATE `users` SET `mana` = `mana`-'".$needmana."' WHERE `id` = '".$user['id']."';");
            
    		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;
	}
}