<?php
define('PROTECTOR', 1);
include('files/path.php');
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
$textl='Битва';
include($path.'files/core.php');
include($path.'files/head.php');
include($path.'files/zag.php');
if($new_lvl=='yes'){header('Location: boss.php');exit;
}
$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'<div class="main"><div class="block_zero center">Вы не находитесь в бою!</div></div>';
include($path.'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,$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'<div class="main"><div class="block_zero center">Ты проиграл!<br/><a href="start.php?">Продолжить</a></div></div>';
include($path.'files/down.php');exit;
}
}
function win($log,$pas,$m,$mob,$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/10)+$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){
$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'");

mysql_query("UPDATE `clan` SET `exp` = '$exep' WHERE `lider` = '$udata[clan]'");
mysql_query("UPDATE `users` SET `wins` = '$nwins',`exp`=`exp`+'$mob[exp]' WHERE `usr` = '$log'");


$nagrada=mysql_fetch_array(mysql_query("SELECT * FROM `shops_drop` WHERE `usr`='$log' LIMIT 1"));
if($nagrada['kompl']==0){
mysql_query("UPDATE `drop_oz` SET `exp`=`exp`+'$mob[exp]' WHERE `usr` = '$log'");
}else{
mysql_query("UPDATE `shops_drop` SET `exp`=`exp`+'$mob[exp]' 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'");}//сбиваем регенерацию

echo'<div class="main"><div class="block_zero center">';

echo"$ilok[text]<br><b>Ты победил!</b><br>";

echo"+<b>$mob[exp]</b> опыта<br/>";

if(!empty($pexp)){
echo"+$pexp1 опыта получил $pit[name]<br/>";
}
echo"<a href=\"boss.php?\">Продолжить</a>";

echo'</div></div>';
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 mein($log,$pas,$a,$m,$pit,$mob,$udata){
timer($log);
lose($log,$pas,$m,$pit,$a,$mob,$udata);
win($log,$pas,$m,$mob,$pit,$a,$udata);
pit($log,$pas,$pit,$udata);
$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'<div class="main"><div class="block_zero">';

echo"<img class='float-left' src='image/boss/$mob[lvl].png' style='margin-left:1px; margin-right:10px;' alt='*'>$mob[name] (<img src=\"image/icon/level.png\"/ alt=\"*\"'> $mob[lvl])<br/><br/><br/></div><div class='mini-line'></div><div class='block_zero'>";

if($pit['status']=='on'){
echo"</div><div class='dot-line'></div><img class='float-left' src='image/pitomec/$pit[ava]'  width='50' height='50' style='margin-left:1px; margin-right:10px;' alt='*'>Питомец: <b>$pit[name]</b> <img src=\"image/icon/health.png\" alt=\"*\"> <b>$pit[hp]</b></div><div class='mini-line'></div>";
}

$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 '<div class="block_zero center"><form action="battle.php?mod=ataka&udar='.$u.'&block='.$b.'" method="POST">';
echo '<span class="btn"><span class="end"><input class="label" type="submit" value="'.$tex.'"></span></span></form></div></div></div>';

echo'<div class="main"><div class="block_zero center">';
log_msg($tlog='battle');

echo'</div></div>';

}

$_POST['udar']=isset($_GET['udar']) ? htmlspecialchars($_GET['udar']):null;
$_POST['block']=isset($_GET['block']) ? htmlspecialchars($_GET['block']):null;

//////////
function ataka($log,$pas,$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,$a,$m,$pit,$mob,$timer,$udata);
}else{
mein($log,$pas,$a,$m,@$pit,$mob,$udata);
}

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