<?
define('PROTECTOR', 1);

$head = 'boss';//фикс. места

if($_GET[mod]=='ataka'){
$header=TRUE;
}else{
$textl='Битва';
}

@include('files/db.php');
@include('files/auth.php');
@include('files/func.php');
@include('files/core.php');
@include('files/head.php');
@include('files/zag.php');

$timer=time()+300;
////////////////////////
$req = mysql_query("SELECT * FROM `tmp` WHERE `usr` = '$log' LIMIT 1");
////////////////////////////
$mags = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' LIMIT 1");
$umen = mysql_fetch_array($mags);
if (mysql_num_rows($req)==0){echo"Вы ненаходитесь в бою!";
include('files/down.php');exit;}
$m = mysql_fetch_array($req);
$req = mysql_query("SELECT * FROM `temp` WHERE `id` = '$m[mob]' and `user`='$log' LIMIT 1");
$mob = mysql_fetch_array($req);
$req = mysql_query("SELECT * FROM `pit` WHERE `usr` = '$log' and `status` = 'on'");
$avto=mysql_num_rows($req);
if($avto==1){
$pit = mysql_fetch_array($req);}

function timer($log){
$time=time();
mysql_query("UPDATE `tmp` SET `ltime` = '$time' WHERE `usr` = '$log'");}
///////////////////////
function lose($log,$pas,$m,$pit,$ass,$a,$mob,$udata){
$time=time();
$losetime=$m[ltime]+600;
if($udata[hp]<=0 or $time>$losetime){
$nloses=$udata[loses]+1;
if($mob[hp]<=0){
mysql_query("UPDATE `temp` SET `hp` = '0',`status`='die' WHERE `id` = '$m[mob]'");
}else{
mysql_query("UPDATE `temp` SET `hp` = '$mob[hp]',`status`='on',`oponent`='not' WHERE `id` = '$m[mob]'");}

mysql_query("UPDATE `mesto` SET `place` = 'gorod',`city` = '0',`x` = '0',`y` = '0' WHERE `usr` = '$log'");     
mysql_query("UPDATE users SET loses = '$nloses',hp='0' WHERE usr = '$log'");//пишем данные в базу
mysql_query("UPDATE regenerator SET last = '$time' WHERE usr = '$log'");//сбиваем регенерацию
mysql_query("DELETE FROM `tmp` WHERE usr='$log'");//чистим логи
//питы
if($pit[status]==on){
if($pit[hp]<=0){
mysql_query("UPDATE pit SET status = 'die' WHERE `usr` = '$log'");}}
$req=mysql_query("SELECT * FROM log WHERE usr = '$log' and place='battle' and kto='enemy' ORDER BY id DESC LIMIT 1");
$ilok = mysql_fetch_assoc($req);

echo"<img src='pic/game/lose.jpg' alt='*'/><br>
$ilok[text]<br/> Ты проиграл!<br/>";
echo"<a href=\"start.php?\">Продолжить</a>";
include('files/down.php');
exit;}}

function win($log,$pas,$m,$mob,$ass,$pit,$a,$udata){
$time=time();
if($mob[hp]<=0){
$sett = mysql_query("SELECT * FROM `set` WHERE `usr` = '$log' LIMIT 1");
$set = mysql_fetch_assoc($sett);
$nwins=$udata[wins]+1;
$summ = rand($mob[exp]-$mob[exp]/10,$mob[exp]+$mob[exp]/10);
if($set[vip]==on){$nexp=round($udata[exp]+($summ+$mob[lvl])*1.5);}else{
$nexp=$udata[exp]+$summ+$mob[lvl];}//расчет опыта!
$clexp=$nexp-$udata[exp];
$req = mysql_query("SELECT * FROM `clan` where `lider` = '$udata[clan]'");
$clan = mysql_fetch_array($req);
$exep = $clexp+$clan[exp];
//питы 
if($pit[status]==on){
if($pit[hp]<=0){
mysql_query("UPDATE pit SET status = 'die' WHERE `usr` = '$log'");
}else{
$pexp1=round($mob[exp]/2);
$pexp=$pit[exp]+$pexp1;
mysql_query("UPDATE pit SET exp='$pexp' WHERE `usr` = '$log'");}}
///////РЕСЫ НАСТРОЙКА

$kolvo1=rand(0,$mob[drop]);
$kolvo2=rand(0,$mob[drop]);
$kolvo3=rand(0,$mob[drop]);

if($kolvo1>>0){
$req = mysql_query("SELECT * FROM `mdrop` WHERE `lat_name` = 'serebro' LIMIT 1");
$drop = mysql_fetch_array($req);

$req = mysql_query("SELECT * FROM `res` WHERE `usr` = '$log' and `lat_name` = 'serebro'");
$res=mysql_num_rows($req);
$rs = mysql_fetch_array($req);

if($res==0){
mysql_query("INSERT INTO `res` SET `usr` = '$log',`name` = '$drop[name]',`lat_name` = 'serebro',
`tip` = '$drop[tip]',`what` = '$drop[what]',`give` = '$drop[give]',`kol` = '$kolvo1', `cena` = '$drop[cena]'");}
else{$nk=$rs[kol]+$kolvo1;
mysql_query("UPDATE `res` SET `kol` = '$nk' WHERE `usr` = '$log' and `lat_name` = 'serebro'");}
}
if($kolvo2>>0){
$req = mysql_query("SELECT * FROM `mdrop` WHERE `lat_name` = 'nefrit' LIMIT 1");
$drop = mysql_fetch_array($req);

$req = mysql_query("SELECT * FROM `res` WHERE `usr` = '$log' and `lat_name` = 'nefrit'");
$res=mysql_num_rows($req);
$rs = mysql_fetch_array($req);

if($res==0){
mysql_query("INSERT INTO `res` SET`usr` = '$log',`name` = '$drop[name]',`lat_name` = 'nefrit',`tip` = '$drop[tip]',`what` = '$drop[what]',`give` = '$drop[give]',`kol` = '$kolvo2', `cena` = '$drop[cena]'");}
else{$nk=$rs[kol]+$kolvo2;
mysql_query("UPDATE `res` SET `kol` = '$nk' WHERE `usr` = '$log' and `lat_name` = 'nefrit'");}
}
if($kolvo3>>0){
$req = mysql_query("SELECT * FROM `mdrop` WHERE `lat_name` = 'obsidian' LIMIT 1");
$drop = mysql_fetch_array($req);

$req = mysql_query("SELECT * FROM `res` WHERE `usr` = '$log' and `lat_name` = 'obsidian'");
$res=mysql_num_rows($req);
$rs = mysql_fetch_array($req);

if($res==0){
mysql_query("INSERT INTO `res` SET`usr` = '$log',`name` = '$drop[name]',`lat_name` = 'obsidian',`tip` = '$drop[tip]',
`what` = '$drop[what]',`give` = '$drop[give]',`kol` = '$kolvo3', `cena` = '$drop[cena]'");}
else{$nk=$rs[kol]+$kolvo3;
mysql_query("UPDATE `res` SET `kol` = '$nk' WHERE `usr` = '$log' and `lat_name` = 'obsidian'");}
}

$sm=rand(1,$mob[sm]);
if($sm==1){$nmoney=$udata[money]+rand($mob[money]-$mob[money]/10,$mob[money]+$mob[money]/10);
$summa=$udata[exp]-$nexp;
$summma=$udata[money]-$nmoney;
$req=mysql_query("SELECT * FROM log WHERE usr = '$log' and place='battle' and kto='user' ORDER BY id DESC LIMIT 1");
$ilok = mysql_fetch_assoc($req);

mysql_query("UPDATE `temp` SET `hp` = '0',`status`='die', `ltime`='$time' WHERE `id` = '$mob[id]' and `user`='$log'");
if(!empty($nmoney)){
mysql_query("UPDATE `clan` SET `exp` = '$exep' WHERE `lider` = '$udata[clan]'");
mysql_query("UPDATE users SET wins = '$nwins',exp='$nexp',money='$nmoney' WHERE usr = '$log'");//пишем данные в плэера
}else{
mysql_query("UPDATE `clan` SET `exp` = '$exep' WHERE `lider` = '$udata[clan]'");
mysql_query("UPDATE users SET wins = '$nwins',exp='$nexp' WHERE usr = '$log'");
}
mysql_query("DELETE FROM `tmp` WHERE usr='$log'");//чистим логи
mysql_query("DELETE FROM `log` WHERE usr='$log'");//чистим логи
mysql_query("UPDATE regenerator SET last = '$time' WHERE usr = '$log'");}//сбиваем регенерацию
////КВЕСТЫ
$req = mysql_query("SELECT * FROM `userquests` WHERE `quest` = '$mob[quest]' and `usr`='$log' and `status`='start'");
$avto=mysql_num_rows($req);
if($avto==1){
//проверка на килл
$req = mysql_query("SELECT * FROM `user_kill` WHERE `quest` = '$mob[quest]' and `city`='$udata[city]' and `usr`='$log'");
$avto=mysql_num_rows($req);
if($avto==1){
$u_k = mysql_fetch_array($req);
$u_k[killing]=$u_k[killing]-1;
if($u_k[killing]<=0){
mysql_query("DELETE FROM `user_kill` WHERE `quest` = '$mob[quest]' and `usr`='$log'");//чистим логи
}else{
mysql_query("UPDATE `user_kill` SET `killing` = '$u_k[killing]'  WHERE `quest` = '$mob[quest]' and `usr`='$log'");}}
//проверяем на асса
$req = mysql_query("SELECT * FROM `user_assasin` WHERE `quest` = '$mob[quest]' and `city`='$udata[city]' and `usr`='$log' and `mob`='$mob[id]'");
$avto=mysql_num_rows($req);
if($avto==1){
mysql_query("DELETE FROM `user_assasin` WHERE `quest` = '$mob[quest]' and `usr`='$log' and `mob`='$mob[id]'");}}
$summa=$nexp-$udata[exp];
$summma=$nmoney-$udata[money];
echo"$ilok[text]<br><b>Ты победил!</b><br>";
if(!empty($drop[name])){
if($kolvo1>>0){echo"Выпало: <b>$kolvo1</b> Серебра<br/>";}
if($kolvo2>>0){echo"Выпало: <b>$kolvo2</b> Нефрита<br/>";}
if($kolvo3>>0){echo"Выпало: <b>$kolvo3</b> Обсидиана<br/>";}
}

echo"+<b>$summa</b> опыта<br/>";
if(!empty($nmoney)){
echo"+<b>$summma</b> монет<br/>";}
if(!empty($pexp)){
echo"+$pexp1 опыта получил $pit[name]<br/>";}
echo"<a href=\"boss.php?\">Продолжить</a>";
include('files/down.php');exit;}}

function pit($log,$pas,$pit,$udata){
if($pit[status]==on){
if($pit[hp]<=0){
mysql_query("UPDATE pit SET status = 'die' WHERE `usr` = '$log'");}}}

function ass($log,$pas,$ass,$a){
if($ass[status]=='active'){
if($ass[hp]<=0){
mysql_query("UPDATE asasins SET status = 'on',`hp`='$ass[hpall]' WHERE `id`='$a[asasin]' LIMIT 1");
mysql_query("DELETE FROM `asasins_active` WHERE usr='$log'");}}}

function mein($log,$pas,$ass,$a,$m,$pit,$mob,$udata){
timer($log);
lose($log,$pas,$m,$pit,$ass,$a,$mob,$udata);
win($log,$pas,$m,$mob,$ass,$pit,$a,$udata);
pit($log,$pas,$pit,$udata);
ass($log,$pas,$ass,$a);
$set = mysql_query("SELECT * FROM `set` WHERE `usr` = '$log' LIMIT 1");
$set = mysql_fetch_array($set);
$rep = mysql_query("SELECT * FROM `pit` WHERE `usr` = '$log' and `status` = 'on'");
$pit = mysql_fetch_array($rep);

echo"<img src='pic/game/boss/m$mob[lvl].jpg' alt='*'/> <b>$mob[name]:</b> (<img src=\"pic/up.png\"/ alt=\"*\"'> $mob[lvl])<br/>";
if($pit[status]==on){
echo"</font><font color ='white'><img src='pic/pit/$pit[ava]'  width='20' height='20'  alt='*'/> Ваш помощник: <b>$pit[name]</b> <img src=\"pic/game/mob/hp.gif\" alt=\"i\"> <b>$pit[hp]</b>";}

$proc=($udata[hp]/$udata[hpall])*100;
echo"<div class='stat_bar'><div class='progress blue' style='width:$proc%'></div></div>";
echo"Жизни: <b>$udata[hp]</b> vs <b>$mob[hp]</b><br/>
Урон: <b>$udata[sila]</b> vs <b>$mob[uron]</b><br/>
Защита: <b>$udata[prot]</b> vs <b>$mob[prot]</b><br/>
Уворот: <b>$udata[lovk]</b> vs <b>$mob[lovk]</b><br/>
</div><div class='menu'>";

$u = rand(1,3);
$b = rand(1,3);
$tex=rand(1,3);
if($tex==1){$tex="Ударить!";}
if($tex==2){$tex="Бить!";}
if($tex==3){$tex="Атаковать!";}
echo '<form action="battle.php?mod=ataka&udar='.$u.'&block='.$b.'" method="post">';
echo '<input class="button" type="submit" value="'.$tex.'" /></form>';

log_msg($tlog='battle');
$el = mysql_query("SELECT * FROM `res` WHERE `usr` = '$log' and `tip`='elexir' and `kol`>'0' ORDER by `cena` DESC LIMIT 4");
$avt=mysql_num_rows($el);
if($avt>=1){echo"</div><div class='menu'> Зелья:<br>";
While($elm = mysql_fetch_array($el)){
echo"$elm[name] ($elm[kol] шт.) [<a href=\"elexirs.php?id=$elm[id]\">исп.</a>]<br/>";
}}else{echo"<b>Нет элексиров</b><br/>";}
echo "<a href='battle.php?sdaus'>Сдаться</a>";
if(isset($_GET[sdaus])){
mysql_query("UPDATE `users` SET `hp` = '0' WHERE `usr`='$log' LIMIT 1");echo"<br>Вы сдались!";}
}

$_POST[udar]=$_GET[udar];
$_POST[block]=$_GET[block];

//////////
function ataka($log,$pas,$ass,$a,$m,$pit,$mob,$timer,$udata){
////AI
$uron=$udata[sila];

$mudar=rand(1,3);
$mblock=rand(1,3);
////AI PITA
$pudar=rand(1,3);
$pblock=rand(1,3);
/////////////

if($udata[hp]>0){
if($_POST[udar]==1){$bonus=1.25;$kuda='в голову';}
if($_POST[udar]==2){$bonus=1;$kuda='по туловищу';}
if($_POST[udar]==3){$bonus=0.85;$kuda='по ногам';}

#********************************#
if($udata[lovk]<$mob[lovk]){
$s_l=rand(1,100);
$shans_lovk=round(($mob[lovk]-$udata[lovk])*5);}
#********************************#
$shans=rand(0,1);
if($_POST[udar]!=$mblock and $shans=1 and ($udata[lovk]>=$mob[lovk] or $s_l>$shans_lovk)){

if($_POST[udar]==1){$mprotect='pgolova';}
if($_POST[udar]==2){$mprotect='pbody';}
if($_POST[udar]==3){$mprotect='pnogi';}

$uron=rand($udata[umin],$udata[umax])+$udata[sila];
if($udata[lvl]>>1){$kro=100;}
if($udata[lvl]>>15){$kro=200;}
if($udata[lvl]>>35){$kro=300;}
$krit=rand(1,$kro);
if($krit<=$udata[krit]){

$mags = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' LIMIT 1");
$umen = mysql_fetch_array($mags);
if($umen[vihr]>0)
{$texs=". Сработал Вихрь критов!";
$mno=round(($uron/100)*($udata[ukrit]+$umen[vihr]));}else{
$mno=round(($uron/100)*$udata[ukrit]);}
$uron=$uron+$mno;
$uron=$uron-$mob[prot];
$uron=round($uron*$bonus);
if($uron<=0){$uron=round(0,1);}
$newmob=$mob[hp]-$uron;


$msg=rand(0,2);
if($msg=="0"){$sms="Ты наносишь крит-удар $kuda $mob[name]! -$uron $texs";}
if($msg=="1"){$sms="Ты бьёшь крит-удар $kuda $mob[name]! -$uron $texs";}
if($msg=="2"){$sms="Ты наносишь крит-удар $kuda $mob[name]! -$uron $texs";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='user',place='battle',timer='$timer'");

}else{
$uron=round($uron*$bonus);
$aya=rand(0,100);
$mags = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' LIMIT 1");
$umen = mysql_fetch_array($mags);
if($aya<=$umen[yarost]){$text=". Сработала Ярость титана!";
$uron=$uron+round(($uron/100)*30);}
$uron=$uron-$mob[prot];
if($uron<=0){$uron=round(0,1);}
$newmob=$mob[hp]-$uron;

$msg=rand(0,2);
if($msg=="0"){$sms="Ты бьёшь $mob[name] $kuda! -$uron $text";}
if($msg=="1"){$sms="Ты атакуешь $mob[name] $kuda! -$uron $text";}
if($msg=="2"){$sms="Ты бьёшь $mob[name] $kuda! -$uron $text";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='user',place='battle',timer='$timer'");
}
}else{
$newmob=$mob[hp];

$msg=rand(0,2);
if($msg=="0"){$sms="$mob[name] пригнулся от твоего удара!";}
if($msg=="1"){$sms="$mob[name] блокировал твой удар!";}
if($msg=="2"){$sms="$mob[name] увернулся от твоего удара!";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='user',place='battle',timer='$timer'");
}
}
//////////////////

if($pit[status]==on){
///////////////////////////

#********************************#
if($pit[lovk]<$mob[lovk]){
$ps_l=rand(1,100);
$pshans_lovk=round(($mob[lovk]-$pit[lovk])*5);
}
#********************************#

if($pudar==1){$bonus=1.25;$kuda='в голову';}
if($pudar==2){$bonus=1;$kuda='по туловищу';}
if($pudar==3){$bonus=0.85;$kuda='по ногам';}
/////////////
if($pudar!=$mblock){

if($pudar==1){$mprotect='pgolova';}
if($pudar==2){$mprotect='pbody';}
if($pudar==3){$mprotect='pnogi';}

$uron=rand($pit[ataka]-$pit[ataka]/8,$pit[ataka]+$pit[ataka]/8);


$uron=$uron-$mob[$mprotect];
$uron=round($uron*$bonus);
if($uron<=0){$uron=rand(0,2);}
$newmob=$newmob-$uron;

$msg=rand(0,2);
if($msg=="0"){$sms="$pit[name] бьёт $mob[name] $kuda! -$uron";}
if($msg=="1"){$sms="$pit[name] атакует $mob[name] $kuda! -$uron";}
if($msg=="2"){$sms="$pit[name] бьёт $mob[name] $kuda! -$uron";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='user',place='battle',timer='$timer'");


if($pblock!=$mudar){
//////////////////////////////////////////////////////
$uron=1;
$newhp=$pit[hp]-$uron;

mysql_query("UPDATE `pit` SET `hp` = '$newhp' WHERE `usr` = '$log'");

$msg=rand(0,2);
if($msg=="0"){$sms="$mob[name] сильно бьёт $kuda $pit[name]! ";}
if($msg=="1"){$sms="$mob[name] атакует $kuda $pit[name]! ";}
if($msg=="2"){$sms="$mob[name] метко бьёт $kuda $pit[name]! ";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='enemy',place='battle',timer='$timer'");
}
}else{
$msg=rand(0,2);
if($msg=="0"){$sms="$pit[name] пригнулся от удара!";}
if($msg=="1"){$sms="$pit[name] блокировал удар!";}
if($msg=="2"){$sms="$pit[name] увернулся от удара!";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='enemy',place='battle',timer='$timer'");
}
}


///////////////////
if($mudar==1){$bonus=1.25;$kuda='в голову';}
if($mudar==2){$bonus=1;$kuda='по туловищу';}
if($mudar==3){$bonus=0.85;$kuda='по ногам';}


#********************************#
if($mob[lovk]<$udata[lovk]){
$ms_l=rand(1,100);
$mshans_lovk=round(($udata[lovk]-$mob[lovk])*5);
}
//$ms_l>$mshans_lovk
#********************************#
$shans=rand(0,1);
if($_POST[block]!=$mudar){

//////////////////////////////////////////////////////
$krit=rand(1,100);
$mags = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' LIMIT 1");
$umen = mysql_fetch_array($mags);
if($umen[stoika]>0 and $krit<=$mob[krit])
{$tex="Сработала Защитная стойка!";
$udata[antikrit]=$udata[antikrit]+round(($udata[antikrit]/100)*$umen[stoika]);}
$uron=rand($mob[uron]-$mob[uron]/10,$mob[uron]+$mob[uron]/10);
$protect=$udata[prot];
if($krit<=$mob[krit]){
$mno=round(($uron/100)*$mob[ukrit]);
$uron=$uron+$mno;
$aya=rand(0,100);
$mags = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' LIMIT 1");
$umen = mysql_fetch_array($mags);
if($aya<=$umen[bronya]){$tex="Сработала Крепкая броня!";
$protect=$protect+round(($protect/100)*25);}
$uron=$uron-$protect;
$uron=round($uron*$bonus);
if($uron<=0){$uron=round(0,1);}
$newhp=$udata[hp]-$uron;

mysql_query("UPDATE `users` SET `hp` = '$newhp' WHERE `usr` = '$log'");

$msg=rand(0,2);
if($msg=="0"){$sms="$mob[name] наносит крит-удар $kuda! -$uron $tex";}
if($msg=="1"){$sms="$mob[name] бьёт крит-удар $kuda! -$uron $tex";}
if($msg=="2"){$sms="$mob[name] наносит крит-удар $kuda! -$uron $tex";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='enemy',place='battle',timer='$timer'");

}else{
$aya=rand(0,100);
$mags = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' LIMIT 1");
$umen = mysql_fetch_array($mags);
if($aya<=$umen[bronya]){$tex="Сработала Крепкая броня!";
$protect=$protect+round(($protect/100)*25);}
$uron=$uron-$protect;
$uron=round($uron*$bonus);

if($uron<=0){$uron=rand(0,3);}
$newhp=$udata[hp]-$uron;

mysql_query("UPDATE `users` SET `hp` = '$newhp' WHERE `usr` = '$log'");

$msg=rand(0,2);
if($msg=="0"){$sms="$mob[name] сильно бьёт $kuda! -$uron $tex";}
if($msg=="1"){$sms="$mob[name] атакует $kuda! -$uron $tex";}
if($msg=="2"){$sms="$mob[name] метко бьёт $kuda! -$uron $tex";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='enemy',place='battle',timer='$timer'");
}
}else{
$msg=rand(0,2);
if($msg=="0"){$sms="Ты пригнулся от удара!";}
if($msg=="1"){$sms="Ты блокировал удар!";}
if($msg=="2"){$sms="Ты увернулся от удара!";}

mysql_query("INSERT INTO log SET usr='$log',text='$sms',kto='enemy',place='battle',timer='$timer'");}
///////////////////////
$rang=rand($udata[mp]/20,$udata[mp]/10);
$newmp=$udata[mp]-$rang;
mysql_query("UPDATE `users` SET `mp` = '$newmp' WHERE `usr` = '$log'");
mysql_query("UPDATE `temp` SET `hp` = '$newmob' WHERE `id` = '$mob[id]' and `user`='$log'");
/////////////////////////////////////////////////
header ("Location: battle.php?");exit; //в бой
}
/////страница!!начало
if($_GET[mod]=='ataka'){
ataka($log,$pas,$ass,$a,$m,$pit,$mob,$timer,$udata);
}else{
mein($log,$pas,$ass,$a,$m,$pit,$mob,$udata);
}

include('files/down.php');
?>