<?
define('PROTECTOR', 1);

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

$textl='Сражения!';
@include('files/db.php');
@include('files/auth.php');
@include('files/func.php');
@include('files/mobdie.php');
going();
ryd();
@include('files/core.php');
@include('files/head.php');
@include('files/zag.php');
$time=time();
mysql_query("UPDATE regenerator SET last = '$time' WHERE usr = '$log'");//сбиваем регенерацию

$req = mysql_query("SELECT * FROM `bitva`");
$avto=mysql_num_rows($req);
$mags = mysql_query("SELECT * FROM `mag` WHERE `usr` = '$log' LIMIT 1");
$umen = mysql_fetch_array($mags);
$piton = mysql_query("SELECT * FROM `pit` WHERE `usr` = '$log' and `status`='on' LIMIT 1");
$pov=mysql_num_rows($piton);
$pit = mysql_fetch_array($piton);
switch($_GET[mod]){

default:
echo"<b>Поля Сражений</b>$div";
if($avto>=1){
While($bitva = mysql_fetch_array($req)){
echo"<font color=\"#ffc22b\"> • </font>";
if($udata[lvl]>$bitva[mlvl]){$font="<font color=\"gray\">";}
if($udata[lvl]<$bitva[lvl]){$font="<font color=\"chocolate\">";}
if($udata[lvl]>=$bitva[lvl] AND $udata[lvl]<=$bitva[mlvl]){$font="<font color=\"#f4d06e\">";}
echo"<a href=\"bitva.php?mod=go&id=$bitva[id]\">$font $bitva[name] </a></font><br/>";
}}else{
echo'Нет башен!<br/>';}
break;

case 'go':
if(empty($_GET[id])){echo"Не выбрано место!";break;}else{
$id=htmlspecialchars(stripslashes(addslashes($_GET['id'])));
$req = mysql_query("SELECT * FROM `bitva`WHERE `id`='$id'");
$bitva = mysql_fetch_array($req);
if($udata[lvl]<$bitva[lvl] and $log!==Admin)
{echo"<font color=\"chocolate\"><img src='pic/main/fp.png' alt='*'/> Локацию можно открыть c $bitva[lvl] ого уровня
</br><a href=\"bitva.php?\">Назад</a>";break;}
if($udata[prava]==6){echo"<a href=\"mod_panel.php?mod=6&b=$id\">Изменить мобов</a> $bitva[lvl]/$bitva[mlvl]</br><a href=\"mobcreat.php?id=$id\">Создать моба</a></br>";}
if($udata[prava]==6){echo"<a href=\"adm_panel.php?mod=6&b=$id\">Изменить мобов</a>  $bitva[lvl]/$bitva[mlvl]</br><a href=\"mobcreat.php?id=$id\">Создать моба</a></br>";}
$exp=round((($udata[exp]-$ol)/($op-$ol))*100);
$proc=($udata[hp]/$udata[hpall])*100;
echo"<div class='a'><b>$bitva[name]</b></div>$div";
echo'<div style="background:#666666;height:1px;width:100%;padding:0;margin:6px 0;">
<div style="background:#FFDF8C; height:1px; width:'.$exp.'%;"></div>
</div>';
echo"<div class='stat_bar'><div class='progress blue' style='width:$proc%'></div></div>";
///////////////////LIMIT/////////////
$lime = mysql_query("SELECT * FROM `limit` WHERE `usr`='$log' ") or die (mysql_error());
$avtol=mysql_num_rows($lime);
if($avtol==0){
mysql_query("INSERT INTO
        `limit` SET `usr` = '$log', `mob` = '0', `mobtime` = '0' ") or die (mysql_error());}
$lim = mysql_fetch_array($lime);
if($lim[mob]>10){
$limit=$time+30;
mysql_query("UPDATE `limit` SET `mobtime` = '$limit', `mob`='$lim[mob]'-'10' WHERE `usr` = '$log' ");}
if($lim[mobtime]>$time){
$ost=$lim[mobtime]-$time;
$rand=rand(10000000,10000000000);
echo"<div class='a'><div class='dot'>Ваш воин устал, необходимо отдохнуть</br> <img src='pic/update.png' alt='*'/> <a href=\"bitva.php?mod=go&id=$id&r=$rand\">Ждать $ost сек.</a></div></div>";
}
///////////////THE END////////////////////
$temper = mysql_query("SELECT * FROM `temp` WHERE `bitva`='$id' and `user`='$log' and `status`='off'");
$av=mysql_num_rows($temper);
$avol=$av+5;
While($templar = mysql_fetch_array($temper)){}
$resen = mysql_query("SELECT * FROM `mobs` WHERE `bitva`='$id' and `id`!='$templar[id]'ORDER BY RAND() ") or die (mysql_error());
$avtos=mysql_num_rows($resen);

$res = mysql_query("SELECT * FROM `mobs` WHERE `bitva`='$id' and `id`!='$templar[id]' ORDER BY `lvl` ASC LIMIT $avol") or die (mysql_error());
$avto=mysql_num_rows($res);
}

$tem = mysql_query("SELECT * FROM `temp` WHERE `bitva`='$id' and `user`='$log' and `id`='$mobs[id]' ") or die (mysql_error());
$temp = mysql_fetch_array($tem);
While($mobs = mysql_fetch_array($res)){
$tem = mysql_query("SELECT * FROM `temp` WHERE `bitva`='$id' and `user`='$log' and `id`='$mobs[id]'");
$temp = mysql_fetch_array($tem);
echo"<font color=\"#ffc22b\">";
if($mobs[status]==on and $mobs[hp]>0){
if($udata[lvl]>$mobs[lvl]){$font="<font color=\"gray\">";}
if($udata[lvl]<$mobs[lvl]){$font="<font color=\"chocolate\">";}
if($udata[lvl]==$mobs[lvl]){$font="<font color=\"#f4d06e\">";}
if($mobs[id]!==$temp[id]){
echo"<img src=\"pic/game/mobs.png\" alt=\"*\"/> <a href=\"bitva.php?mod=go&ataka&mob=$mobs[id]&id=$id\">$font $mobs[name] </a>
(<img src=\"pic/game/mob/hp.gif\" alt=\"i\">$mobs[hp])</font><a href=\"bitva.php?mod=info&id=$mobs[id]\"><img src=\"pic/main/info.gif\" alt=\"i\"></a><br/>";
}else{
if($temp[status]==on and $temp[hp]>=0){
echo"<img src=\"pic/game/mobs.png\" alt=\"*\"/> <a href=\"bitva.php?mod=go&ataka&mob=$temp[id]&id=$id\">$font $temp[name] </a>
(<img src=\"pic/game/mob/hp.gif\" alt=\"i\">$temp[hp])</font><a href=\"bitva.php?mod=info&id=$temp[id]\">
<img src=\"pic/main/info.gif\" alt=\"i\"></a><br/>";}
}
}
}
if($avto==0){
echo'Все монстры убиты!<br/>';break;}

$temo = mysql_fetch_array($resen);
$rand=rand(10000000,10000000000);
echo"</br><div class='event'><img src=\"pic/main/red_warrior.png\" alt=\"*\"/> <a href=\"bitva.php?mod=go&ataka&mob=$temo[id]&id=$id&r=$rand\">Бить любого</a></br>";
//////////АТАКА///////////////
if(isset($_GET[ataka])){

if(empty($_GET[mob])){echo"<img src=\"pic/no.png\" alt=\"*\"/> Не выбран моб!";break;}else{
if($lim[mobtime]<$time){
$_GET[mob]=htmlspecialchars(stripslashes(addslashes($_GET['mob'])));
$req = mysql_query("SELECT * FROM `mobs` WHERE `id`='$_GET[mob]' ");
$avt=mysql_num_rows($req);
$mob = mysql_fetch_array($req);
$tem = mysql_query("SELECT * FROM `temp` WHERE `user`='$log' and `id`='$_GET[mob]'");
$avto=mysql_num_rows($tem);
if($avto==0){
mysql_query("INSERT INTO
        `temp` SET
        `id` = '$mob[id]',
        `name` = '$mob[name]',
        `lvl` = '$mob[lvl]',
        `hp` = '$mob[hp]',
        `hpall` = '$mob[hp]',
        `uron` = '$mob[uron]',
        `prot` = '$mob[prot]',
        `krit` = '$mob[krit]',
        `ukrit` = '$mob[ukrit]',
        `lovk` = '$mob[lovk]',
        `money` = '$mob[money]',
        `exp` = '$mob[exp]',
        `tip` = '$mob[tip]',
        `oponent` = '0',
        `image` = '0',
        `status` = 'on',
        `bitva` = '$mob[bitva]',
        `user` = '$log',
        `ltime` = '$time'") or die (mysql_error());
}
$tem = mysql_query("SELECT * FROM `temp` WHERE `user`='$log' and `id`='$_GET[mob]'");
$mob = mysql_fetch_array($tem);

if($avt=0){echo"<img src=\"pic/no.png\" alt=\"*\"/> Нет такого моба!";exit;}

if($udata[mp]<=0){$lov=1; $utex = "<font color='#ff3333'> Не хватает энергии!</font>";
echo"<div class='event'><font color='#ff6600'>Боевой журнал</font></br>";
echo"$utex</br>";
echo"</div>";break;
}


if($udata[lovk]<$mob[lovk]){$sl=rand(1,100);
$shans_lovk=round(($mob[lovk]-$udata[lovk])*5);}
$r=rand(1,2);
if($shans_lovk>$sl and $r==1){$mb=1;
$msg=rand(0,2);
if($msg=="0"){$utex="<font color='chocolate'> $mob[name] пригнулся от твоего удара!</font>";}
if($msg=="1"){$utex="<font color='chocolate'> $mob[name] блокировал твой удар!</font>";}
if($msg=="2"){$utex="<font color='chocolate'> $mob[name] увернулся от твоего удара!</font>";}
}

if($mob[lovk]<$udata[lovk]){$ms_l=rand(1,100);$mshans_lovk=round(($udata[lovk]-$mob[lovk])*4);}
if($ms_l>$mshans_lovk){$ub=1;
$msg=rand(0,2);
if($msg=="0"){$mtex="<font color='chocolate'> Ты пригнулся от удара!</font>";}
if($msg=="1"){$mtex="<font color='chocolate'> Ты блокировал удар!</font>";}
if($msg=="2"){$mtex="<font color='chocolate'> Ты увернулся от удара!</font>";}

}
if($ub!=1){
$m=$mob[uron];
$aya=rand(0,100);
if($aya<=$umen[bronya]){$tex="Сработала Крепкая броня!";$udata[prot]=$udata[prot]+round(($udata[prot]/100)*25);}
$udar=round(rand($m-$m/7,$m+$m/7))-$udata[prot];
if($udar<=0){$udar=rand(0,2);}
$msg=rand(0,2);
if($msg=="0"){$mtex="$mob[name] сильно бьёт! <font color='red'>-$udar $tex</font>";}
if($msg=="1"){$mtex="$mob[name] атакует ! <font color='red'>-$udar $tex</font>";}
if($msg=="2"){$mtex="$mob[name] метко бьёт ! <font color='red'>-$udar $tex</font>";}
$myhp=$udata[hp]-$udar;
$krit=rand(1,100);
$myhp=$udata[hp]-$udar;
if($krit<=$mob[krit]){
$mno=round(($udar/100)*$mob[ukrit]);
$udar=$udar+$mno;
$myhp=$udata[hp]-$udar;

$msg=rand(0,2);
if($msg=="0"){$mtex="$mob[name] наносит крит-удар! <font color='red'>-$udar $tex</font>";}
if($msg=="1"){$mtex="$mob[name] бьёт крит-удар! <font color='red'>-$udar $tex</font>";}
if($msg=="2"){$mtex="$mob[name] наносит крит-удар! <font color='red'>-$udar $tex</font>";}
}
mysql_query("UPDATE `users` SET `hp`='$myhp', `mp`='$udata[mp]'-'1' WHERE `usr`='$log'") or die (mysql_error());
}
if($mb!=1){
$aya=rand(0,100);
if($aya<=$umen[yarost]){$text="Сработала Ярость титана!";$u=$udata[sila]+round(($udata[sila]/100)*30);}else{
$u=$udata[sila];}
$uron=round(rand($u-($u/8),$u+($u/8))-$mob[prot]);
if($uron<=0){$uron=rand(0,1);}
$msg=rand(0,2);
if($msg=="0"){$utex="Ты бьёшь $mob[name] ! <font color='#71cc71'>-$uron $text</font>";}
if($msg=="1"){$utex="Ты атакуешь $mob[name] ! <font color='#71cc71'>-$uron $text</font>";}
if($msg=="2"){$utex="Ты бьёшь $mob[name] ! <font color='#71cc71'>-$uron $text</font>";}
$newmob=$mob[hp]-$uron;
if($udata[lvl]<=30){$kr=100;}
if($udata[lvl]>30){$kr=200;}
if($udata[lvl]>50){$kr=300;}
$krit=rand(1,$kr);
if($krit<=$udata[krit]){
if($umen[vihr]>0)
{$texs="Сработал Вихрь критов!";
$mno=round(($uron/100)*($udata[ukrit]+$umen[vihr]));}else{
$mno=round(($uron/100)*$udata[ukrit]);}
$uron=$uron+$mno;
if($uron<=0){$uron=rand(0,2);}
$msg=rand(0,2);
if($msg=="0"){$utex="Ты наносишь крит-удар $mob[name]! <font color='#71cc71'>-$uron $texs</font>";}
if($msg=="1"){$utex="Ты бьёшь крит-удар $mob[name]! <font color='#71cc71'>-$uron $texs</font>";}
if($msg=="2"){$utex="Ты атакуешь крит-ударом $mob[name]! <font color='#71cc71'>-$uron $texs</font>";}
$newmob=$mob[hp]-$uron;

}

mysql_query("UPDATE `temp` SET `hp`='$newmob' WHERE `id`='$mob[id]'") or die (mysql_error());
}

////////////ПОБЕДА!!!/////ПРОИГРЫШ///////

if($udata[hp]<0){
$nlos=$udata[loses]+1;
mysql_query("UPDATE users SET loses = '$nlos' , hp = '0' WHERE usr = '$log'");
$utex="<font color='red'>Вы Проиграли...</font>";
echo '<html><head><title>На главную..</title><meta http-equiv="refresh" content="1; url=index.php"></head><body>';
}//Конец//

if($mob[hp]<0){
$sett = mysql_query("SELECT * FROM `set` WHERE `usr` = '$log' LIMIT 1");
$set = mysql_fetch_array($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*1.5));}else{
$nexp=round($udata[exp]+$summ);}//расчет опыта!
if($pov==1){$pi=$nexp-$udata[exp]; $po=$pi/2+$pit[exp];
mysql_query("UPDATE `pit` SET `exp` = '$po' WHERE `usr` = '$log' and `status`='on'");
}
$clexp=$nexp-$udata[exp];
$mainexp=$clexp+$udata[clanexp];
$req = mysql_query("SELECT * FROM `clan` where `lider` = '$udata[clan]'");
$clan = mysql_fetch_array($req);
$exep = $clexp+$clan[exp];
$summa=$nexp-$udata[exp];
/////Дроп////////
$res=rand(0,60);
$per=3+$set[razb];
$shans=rand(0,$per);
if($shans>=$res){
$drops=rand(1,12);
$resd = mysql_query("SELECT * FROM `mdrop` WHERE `id`='$drops'") or die (mysql_error());
$res = mysql_fetch_array($resd);
$drop="Выпало: $res[name]";
$prov = mysql_query("SELECT * FROM `res` WHERE `usr`='$log' and `name` = '$res[name]' and `tip`='$res[tip]'") or die (mysql_error());
$avto=mysql_num_rows($prov);
if($avto==0){
mysql_query("INSERT INTO
        `res` SET
        `usr` = '$log',
        `name` = '$res[name]',
        `lat_name` = '$res[lat_name]',
        `tip` = '$res[tip]',
        `what` = '$res[what]',
        `give` = '$res[give]',
        `kol` = '1',
        `cena` = '$res[cena]'") or die (mysql_error());}else{
$dr = mysql_fetch_array($prov);
$nkol = $dr[kol]+1;
mysql_query("UPDATE `res` SET `kol` = '$nkol' WHERE `usr` = '$log' and `name`='$dr[name]'");
}
}
///////////end drops////////
$summma=$udata[money]-$nmoney;
if($set[razb]<5){$set[razb]=5;}
$sm=rand(0,$set[razb]);
$shans=rand(1,31);
if($sm>=$shans){
if($set[vip]=="on"){$koef=1.5;}else{$koef=1;}
$nmoney=round($udata[money]+(rand($mob[money]-$mob[money]/10,$mob[money]+$mob[money]/10)*$koef));
$summma=$nmoney-$udata[money];
$utex="<font color='#71cc71'>Вы победили $mob[name] ! +$summa опыта +$summma монет $drop</font>";}else{
$nmoney=$udata[money];
$utex="<font color='#71cc71'>Вы победили $mob[name] ! +$summa опыта $drop</font>";}

mysql_query("UPDATE `clan` SET `exp` = '$exep' WHERE `lider` = '$udata[clan]'");
mysql_query("UPDATE users SET wins = '$nwins',exp='$nexp',money='$nmoney', clanexp='$mainexp' WHERE usr = '$log'");
mysql_query("UPDATE `temp` SET `status`='off', `ltime`='$time', `hp`='$mob[hpall]' WHERE `id`='$mob[id]'");
mysql_query("UPDATE `limit` SET `mob`='$lim[mob]'+'1' WHERE `usr` = '$log' ");
//////////ОРУЖИЕ/////////
$rew = mysql_query("SELECT * FROM `weapon` WHERE `usr`='$log' and `image`='yes'") or die(mysql_error());
While($weap=mysql_fetch_array($rew)){
if($weap[proch]>0){
$mob=$weap[mob]+1;
mysql_query("UPDATE `weapon` SET `mob` =  '$mob' WHERE`id`= '$weap[id]'") or die(mysql_error());
if($weap[zakal]==1){$km=400;}else{$km=250;}
if($weap[mob]>=$km){
$proch=$weap[proch]-1;
if($weap[zakal]==1){$k=20;}else{$k=10;}
$sila=round($weap[sila]-($weap[sila]/$k));
$lovk=round($weap[lovk]-($weap[lovk]/$k));
$prot=round($weap[prot]-($weap[prot]/$k));
$krit=round($weap[krit]-($weap[krit]/$k));
$ukrit=round($weap[ukrit]-($weap[ukrit]/$k));
$hp=round($weap[hp]-($weap[hp]/$k));
$mp=round($weap[mp]-($weap[mp]/$k));
$cena=round($weap[cena]-($weap[cena]/$k));
/////
$usila=round($udata[sila]-($weap[sila]/$k));
$ulovk=round($udata[lovk]-($weap[lovk]/$k));
$uprot=round($udata[prot]-($weap[prot]/$k));
$upkrit=round($udata[krit]-($weap[krit]/$k));
$uukrit=round($udata[ukrit]-($weap[ukrit]/$k));
$uhp=round($udata[hpall]-($weap[hp]/$k));
$ump=round($udata[mpall]-($weap[mp]/$k));
/////
mysql_query("UPDATE `weapon` SET 
`mob` =  '$weap[mob]'-'$km',
`proch` =  '$proch',
`cena` =  '$cena',
`sila` =  '$sila',
`lovk` =  '$lovk',
`prot` =  '$prot',
`krit` =  '$krit',
`ukrit` =  '$ukrit',
`hp` =  '$hp',
`mp` =  '$mp'
 WHERE`id`= '$weap[id]'");

mysql_query("UPDATE `users` SET 
`sila` =  '$usila',
`lovk` =  '$ulovk',
`prot` =  '$uprot',
`krit` =  '$upkrit',
`ukrit` =  '$uukrit',
`hpall` =  '$uhp',
`mpall` =  '$ump'
 WHERE `usr`= '$log'");
}
}}
//////////КОНЕЦ//////////
echo"<div class='event'><font color='#ff6600'>Боевой журнал</font></br>";
echo"$utex</br>";
echo"</div>";
}else{
///////////ПИТЫ///////////
if($pov==1 and $mob[ltime]!=$time){
$pataka=rand($pit[ataka]-$pit[ataka]/8,$pit[ataka]+$pit[ataka]/8);
$ataka=round($pataka);
$uvorot=rand(0,100);
$tem = mysql_query("SELECT * FROM `temp` WHERE `user`='$log' and `id`='$mob[id]'");
$mob = mysql_fetch_array($tem);
$newmobs=$mob[hp]-$ataka;

if($pit[lovk]>=$uvorot){
$msg=rand(0,2);
if($msg=="0"){$msp="ударил";}
if($msg=="1"){$msp="укусил";}
if($msg=="2"){$msp="поцарапал";}
$ptex="<b>$pit[name]</b> $msp врага <font color='chocolate'>-$ataka!</font>";

mysql_query("UPDATE `temp` SET `hp`='$newmobs' WHERE `id`='$mob[id]'") or die (mysql_error());
}else{

$ptex="<font color='#ff3333'><b>$pit[name]</b> получил травму,но атаковал -$ataka</font>";
$hp=$pit[hp]-1;
mysql_query("UPDATE `pit` SET `hp` = '$hp' WHERE `id`='$pit[id]' and `usr`='$log'") or die (mysql_error());
mysql_query("UPDATE `temp` SET `hp`='$newmobs' WHERE `id`='$mob[id]'") or die (mysql_error());
if($pit[hp]<0){mysql_query("UPDATE `pit` SET `dies`='$pit[dies]'+'1', `status`='die' WHERE `id`='$pit[id]' and `usr`='$log'") or die (mysql_error());
$ptex="<font color='red'><b>$pit[name]</b> получил травму и погиб</font>";
}
}
}

///////////КОНЕЦ//////////
if($udata[lvl]>$mob[lvl]){$font="<font color=\"gray\">";}
if($udata[lvl]<$mob[lvl]){$font="<font color=\"chocolate\">";}
if($udata[lvl]==$mob[lvl]){$font="<font color=\"#f4d06e\">";}
$rand=rand(10000000,10000000000);
echo"<img src=\"pic/main/sword.gif\" alt=\"*\"/> <a href=\"bitva.php?mod=go&ataka&mob=$mob[id]&id=$id&r=$rand\">
Добивать $font $mob[name] </a>(<img src=\"pic/game/mob/hp.gif\" alt=\"i\">$mob[hp])</font></br>";
echo"<div class='event'></br><font color='#ff6600'>Боевой журнал</font></br>";
$rand=rand(1,2);
if($rand==1){echo"$utex</br>$mtex</br>$ptex";}
if($rand==2){echo"$mtex</br>$utex</br>$ptex";}
echo"</div></font></font>";

}
}
}




}
if($pov==1){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></div>";}

echo"<b>Зелья:</b><br>";
$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){
While($elm = mysql_fetch_array($el)){
echo"$elm[name] ($elm[kol] шт.) [<a href=\"elexirs.php?id=$elm[id]\">исп.</a>]<br/>";
}}echo"</div></font>";

break;


case 'info':
$id = htmlspecialchars(addslashes(stripslashes($_GET['id'])));
$req = mysql_query("SELECT * FROM `mobs` WHERE `id` = '$id' LIMIT 1");
////////////////////////////
if (mysql_num_rows($req)==0){echo"Такого монстра не существует.".mysql_num_rows($req);@include('files/down.php');exit;}
$mob = mysql_fetch_array($req);
echo"<b>$mob[name]</b><br/>";
echo"Уровень: $mob[lvl]<br/>";
echo"Жизни: $mob[hp]<br/>";
echo"Урон: $mob[uron]<br/>";
echo"Защита: $mob[prot]<br/>";
echo"Ловкость: $mob[lovk]<br/>";
echo"Крит: $mob[krit]%<br/>";
echo"Крит-урон: $mob[ukrit]%<br/>";
echo"Опыта: $mob[exp]<br/>";
echo"Монет: $mob[money]<br/>";
echo"<a href=\"bitva.php?\">Назад</a>";
break;
/////страница!!начало
if(isset($_GET[ataka])){
header ("Location: bitva.php?mod=go&id=$id&r=$rand");}
}
@include('files/down.php');
?>