<?
# Автор движка: InstalScript
# vk.com/k.karamin  - Я Вк
# bymas.ru/id3741  - Я на бумасе
# Специально для обитателей bymas.ru
# Ток на бумасе можно найти моды для данного движка
require_once($_SERVER['DOCUMENT_ROOT']."/instalscript/game/inc/function.php");

/*
$i = '0';
while($i <= '300'){
copy("http://naemniki.mobi/maneken?g=m&s=b&m=2-".$i."","полный путь куда копировать/".$i.".jpg")
$i++;
}
# */





$myt = mysql_fetch_assoc(mysql_query("SELECT * FROM `utank` WHERE `act` = '1' AND `uid` = '".$uid."' "));
if($myt == null){logi("Для боёв необходим танк");header("Location:/angar/");exit();}
$myb = mysql_fetch_assoc(mysql_query("SELECT * FROM `battle` WHERE `uid` = '".$uid."' "));



if($is['1'] == 'clear'){mysql_query("DELETE FROM `log` WHERE `pid` = '".$uid."' "); /*or die(mysql_error());*/ header("Location:".$_SERVER['HTTP_REFERER']."");}



head("Бой",$uid);

switch($is['1']){
default:
echo "
<div class='trnt-block'>
<div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='wrap4'><div class='wrap5'><div class='wrap6'><div class='wrap7'><div class='wrap8'>
<div class='wrap-content'>

<a class='simple-but border mb2' href='/battle/pve/'><span><span>PVE</span></span></a>
<div class = 'bot'><a class='simple-but border mb2' href='/battle/pvp/'><span><span>PVP</span></span></a></div>

</div>
</div></div></div></div></div></div></div></div>
</div>
";
if($_SESSION['protiv_pve'] != null){$_SESSION['protiv_pve'] = null;}
if($_SESSION['map_pve'] != null){$_SESSION['map_pve'] = null;}

if($myb != null){mysql_query("DELETE  FROM `battle` WHERE `uid` = '".$uid."' ");}

break;

case'pve':

if($_SESSION['map_pve'] == null){
$randmap = mysql_fetch_assoc(mysql_query("SELECT * FROM `map` ORDER BY RAND() "));
$_SESSION['map_pve'] = $randmap['id'];
}else{
$randmap = mysql_fetch_assoc(mysql_query("SELECT * FROM `map` WHERE `id` = '".abs($_SESSION['map_pve'])."' "));
}

echo "
<div class='trnt-block'>
<div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='wrap4'><div class='wrap5'><div class='wrap6'><div class='wrap7'><div class='wrap8'>
<div class='wrap-content cntr'>";
echo "<a href = '/infomap/info/".$randmap['id']."/'><div class = 'simple-but'>".$randmap['name']."</div></a>";
echo "<img src = '".str_replace("[type]","logo",$randmap['img'])."'>";
echo "<div class = 'bot'><a class='simple-but border mb2' href='/battle/b/pve/'><span><span>К бою</span></span></a></div>";
echo "
</div>
</div></div></div></div></div></div></div></div>
</div>
";
break;
case'b':

if($_SESSION['map_pve'] == null){
header("Location:/battle");
exit();
}

echo "
<div class='trnt-block'>
<div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='wrap4'><div class='wrap5'><div class='wrap6'><div class='wrap7'><div class='wrap8'>
<div class='wrap-content cntr'>";
if(isset($_SESSION['map_pve'])){
$map = mysql_fetch_assoc(mysql_query("SELECT * FROM `map` WHERE `id` = '".abs($_SESSION['map_pve'])."' "));
if($map == null){
$_SESSION['map_pve'] = null;
header("Location:/battle");
exit();
}else{
if($_SESSION['protiv_pve'] == null){
echo "<img src = '".str_replace("[type]","mini",$map['img'])."' width = '50%'>";
}
if(mt_rand(1,4) == '1' && $_SESSION['protiv_pve'] == null){
$protivn = mysql_fetch_assoc(mysql_query("SELECT * FROM `tank` WHERE `type` = '".tank($myt['tid'],'type')."' ORDER BY RAND() "));
$_SESSION['protiv_pve'] = $protivn['id'];
}
if($_SESSION['protiv_pve'] == null){
$title = "Продвигаться вперёд";
$href = "/battle/b/pve/";

$rndfuel = mt_rand(1,10);

if($rndfuel >= $user['fuel']){
mysql_query("UPDATE `users` SET `fuel` = '0' WHERE `id` = '".$uid."' "); # or die(mysql_error());
logi("Кончилось топливо");
header("Location:/angar/");
exit();
}


mysql_query("UPDATE `users` SET `fuel` = `fuel` - '".($rndfuel)."' WHERE `id` = '".$uid."' "); # or die(mysql_error());
}else{
$protivn = mysql_fetch_assoc(mysql_query("SELECT * FROM `tank` WHERE `id` = '".abs($_SESSION['protiv_pve'])."' "));
$title = "В бой";
$href = "/battle/upd/";
if($protivn == null){
$_SESSION['protiv_pve'] = null;
header("Location:/battle/b/pve/");
exit();
}else{
# echo $_SESSION['protiv_pve']; id танка противника
echo "<table><td><img src = '/tankimage_".$myt['tid']."_1.jpg' width = '95%'></td><td></br></br>Против</td><td><img src = '/tankimage_".$protivn['id']."_1.jpg' width = '95%'></td></table>";
}
}
}
}


echo "<div class = 'bot'><a class='simple-but border mb2' href='".$href."'><span><span>".$title."</span></span></a></div>";


echo "
</div>
</div></div></div></div></div></div></div></div>
</div>
";
break;
case'upd':
$pv = mysql_fetch_assoc(mysql_query("SELECT * FROM `tank` WHERE `id` = '".abs($_SESSION['protiv_pve'])."' "));
if($pv == null){header("Location:/battle/");exit();}

if($myb == null){
mysql_query("INSERT INTO `battle` SET
`uid` = '".$uid."',
`tid` = '".$pv['id']."',
`ha` = '".round(($myt['ha']/100)*(mt_rand(90,110)))."',
`hb` = '".round(($myt['hb']/100)*(mt_rand(90,110)))."',
`hc` = '".round(($myt['hc']/100)*(mt_rand(90,110)))."',
`ho` = '".round(($myt['ho']/100)*(mt_rand(90,110)))."',
`mha` = '".$myt['ha']."',
`mhb` = '".$myt['hb']."',
`mhc` = '".$myt['hc']."',
`mho` = '".$myt['ho']."',
`time` = '".(time())."'
"); #or die(mysql_error());
}else{
mysql_query("UPDATE `battle` SET
`tid` = '".$pv['id']."',
`ha` = '".round(($myt['ha']/100)*(mt_rand(90,110)))."',
`hb` = '".round(($myt['hb']/100)*(mt_rand(90,110)))."',
`hc` = '".round(($myt['hc']/100)*(mt_rand(90,110)))."',
`ho` = '".round(($myt['ho']/100)*(mt_rand(90,110)))."',
`mha` = '".$myt['ha']."',
`mhb` = '".$myt['hb']."',
`mhc` = '".$myt['hc']."',
`mho` = '".$myt['ho']."',
`time` = '".(time())."'
WHERE `uid` = '".$uid."' ");# or die(mysql_error());
}

header("Location:/battle/menu/atack");
exit();
break;
case'menu':
if($myb == null){header("Location:/battle/");exit();}

$BB = mysql_fetch_assoc(mysql_query("SELECT * FROM `uamunition` WHERE `uid` = '".$uid."'  AND `aid` = '19' "));
$OF = mysql_fetch_assoc(mysql_query("SELECT * FROM `uamunition` WHERE `uid` = '".$uid."'  AND `aid` = '20' "));
$KS = mysql_fetch_assoc(mysql_query("SELECT * FROM `uamunition` WHERE `uid` = '".$uid."'  AND `aid` = '21' "));
if($BB == null){$BB['kol'] = '0';}
if($OF == null){$OF['kol'] = '0';}
if($KS == null){$KS['kol'] = '0';}


if($is['2'] == 'atack'){

$uta = round(((5 + (abs(round(mt_rand((($myt['ha']/100)*75),(($myt['ha']/100)*100)) - (mt_rand((($myb['hb']/100)*60),(($myb['hb']/100 )*90)))))))/100)*50); # Моя атака
$bta = round(((5 + (abs(round(mt_rand((($myb['ha']/100)*75),(($myb['ha']/100)*100)) - (mt_rand((($myt['hb']/100)*60),(($myt['hb']/100 )*90)))))))/100)*30); # round(($uta/100)*mt_rand(50,100)); #round(abs((($uta/100)*70) -  (5 + (round(mt_rand((($myb['ha']/100)*75),(($myb['ha']/100)*100)) - (mt_rand((($myt['hb']/100)*60),(($myt['hb']/100 )*90)))))))); # Атака бота


trofei_battle($uid,'battle-pve');


/* - Использование снаряда -*/
if($is['3'] == 'bb' or $is['3'] == 'of' or $is['3'] == 'ks'){


if($is['3'] == 'bb'){
$tsnar = "snaryadBB"; # Тип снаряда
if($BB['kol'] > '0'){
$dpur = round(($myb['mha']/100)*40); # Дополнительный урон 40 %
$uta = round($uta + $dpur);

/* -Достижение: Использованно снарядов BB- */
$at_us_bb = mysql_fetch_assoc(mysql_query("SELECT * FROM `at_us` WHERE `uid` = '".$uid."' AND `type` = 'bb' "));
if(isset($at_us_bb)){
if($at_us_bb == null){
mysql_query("INSERT INTO `at_us` SET `uid` = '".$uid."',`type` = 'bb',`amount` = '1',`time` = '".(time())."' ");
}
else{
mysql_query("UPDATE `at_us` SET `amount` = `amount` + '1' WHERE `uid` = '".$uid."' AND `type` = 'bb' ");
}
}
/* -Достижение: Использованно снарядов BB- */
mysql_query("UPDATE `uamunition` SET `kol` = `kol` - '1' WHERE `uid` = '".$uid."' AND `aid` = '19' ");

}else{
$utext = "Снаряда нет";
$snaryad = 'no';
}
}
elseif($is['3'] == 'of'){
$tsnar = "snaryadOF"; # Тип снаряда
if($OF['kol'] > '0'){
$dpur = round(($myb['mha']/100)*35); # Дополнительный урон 35 %
$uta = round($uta + $dpur);

/* -Достижение: Использованно снарядов OF- */
$at_us_of = mysql_fetch_assoc(mysql_query("SELECT * FROM `at_us` WHERE `uid` = '".$uid."' AND `type` = 'of' "));
if(isset($at_us_of)){
if($at_us_of == null){
mysql_query("INSERT INTO `at_us` SET `uid` = '".$uid."',`type` = 'of',`amount` = '1',`time` = '".(time())."' ");
}
else{
mysql_query("UPDATE `at_us` SET `amount` = `amount` + '1' WHERE `uid` = '".$uid."' AND `type` = 'of' ");
}
}
/* -Достижение: Использованно снарядов OF- */
mysql_query("UPDATE `uamunition` SET `kol` = `kol` - '1' WHERE `uid` = '".$uid."' AND `aid` = '20' ");


}else{
$utext = "Снаряда нет";
$snaryad = 'no';
}

}
elseif($is['3'] == 'ks'){
$tsnar = "snaryadKS"; # Тип снаряда

if($KS['kol'] > '0'){
$dpur = round(($myb['mha']/100)*30); # Дополнительный урон 30 %
$uta = round($uta + $dpur);

/* -Достижение: Использованно снарядов KS- */
$at_us_ks = mysql_fetch_assoc(mysql_query("SELECT * FROM `at_us` WHERE `uid` = '".$uid."' AND `type` = 'ks' "));
if(isset($at_us_ks)){
if($at_us_ks == null){
mysql_query("INSERT INTO `at_us` SET `uid` = '".$uid."',`type` = 'ks',`amount` = '1',`time` = '".(time())."' ");
}
else{
mysql_query("UPDATE `at_us` SET `amount` = `amount` + '1' WHERE `uid` = '".$uid."' AND `type` = 'ks' ");
}
}
/* -Достижение: Использованно снарядов KS- */
mysql_query("UPDATE `uamunition` SET `kol` = `kol` - '1' WHERE `uid` = '".$uid."' AND `aid` = '21' ");#or die(mysql_error());

}else{
$utext = "Снаряда нет";
$snaryad = 'no';
}
}

if($snaryad != 'no'){
$snaryad = 'yes';
$utext = "Вы попали в танк ".tank($myb['tid'],'name').", ".snaryad($tsnar,'1')." снарядом (дополнительный урон $dpur) прочность -  $uta";
}

}
/* - Использование снаряда -*/
/* - Для достижения:урон -*/
$at_us_uron= mysql_fetch_assoc(mysql_query("SELECT * FROM `at_us` WHERE `type` = 'uron' AND `uid` = '".$uid."' "));# or die(mysql_error());
if(isset($at_us_uron)){
if($at_us_uron == null){
mysql_query("INSERT  INTO `at_us` SET `type` = 'uron',`amount` = '".$uta."',`uid` = '".$uid."',`time` = '".(time())."'");# or die(mysql_error());
}else{
mysql_query("UPDATE `at_us` SET `amount` =  `amount` + '".$uta."' WHERE `uid` = '".$uid."' AND `type` = 'uron'  ");#or die(mysql_error());
}
}
/* - Для достижения:урон -*/




if($uta >= $myb['hc']){
$snaryad = '';

$utext = "Вы уничтожили танк ".tank($myb['tid'],'name')." ";


/* - Для достижения: уничтоженно танков,стран -*/
$at_tank = mysql_fetch_assoc(mysql_query("SELECT * FROM `at_tank` WHERE `country` = '".tank($myb['tid'],'country')."' AND `type` = '".tank($myb['tid'],'type')."' ")); #or die(mysql_error());
if(isset($at_tank)){
if($at_tank == null){
mysql_query("INSERT INTO `at_tank` SET `uid` = '".$uid."',`country` = '".tank($myb['tid'],'country')."',`type` = '".tank($myb['tid'],'type')."',`amount` = '1' ")or die(mysql_error());
}else{
mysql_query("UPDATE `at_tank` SET `amount` = `amount` + '1' WHERE `uid` = '".$uid."' AND `id` = '".$at_tank['id']."' AND `country` = '".tank($myb['tid'],'country')."' AND `type` = '".tank($myb['tid'],'type')."' ")or die(mysql_error());
}
}
/* - Для достижения: уничтоженно танков,стран -*/

mysql_query("INSERT INTO `log` SET `text` = '".$utext."',`pid` = '".$uid."',`who` = '0',`time` = '".(time())."' ");
mysql_query("UPDATE `users` SET `exp` = `exp` + '".$uta."' WHERE `id` = '".$uid."' ");
mysql_query("DELETE FROM `battle` WHERE `uid` = '".$uid."' ");


if($_SESSION['protiv_pve'] != null){$_SESSION['protiv_pve'] = null;}

header("Location:/battle/b/pve/win");
exit();
}
else{
$btext = "В вас попали, прочность - $bta";
if($snaryad != 'yes' && $snaryad != 'no'){
$utext = "Вы попали в танк ".tank($myb['tid'],'name')." прочность -  $uta";
}

mysql_query("UPDATE `users` SET `exp` = `exp` + '".$uta."' WHERE `id` = '".$uid."' ");
mysql_query("UPDATE `battle` SET `hc` = `hc` - '".$uta."',`mhc` = `mhc` - '".$bta."' WHERE `id` = ".$myb['id']." ");
if($bta > '0'){mysql_query("INSERT INTO `log` SET `text` = '".$btext."',`pid` = '".$uid."',`who` = '1',`time` = '".(time())."' ");}
}


# -Уничтожение моего танка- #
if($bta >= $myb['mhc']){
$btext = "Ваш танк подорвали";
mysql_query("INSERT INTO `log` SET `text` = '".$btext."',`pid` = '".$uid."',`who` = '1',`time` = '".(time())."' ");
if($_SESSION['protiv_pve'] != null){$_SESSION['protiv_pve'] = null;}
header("Location:/battle/b/pve/");
exit();
}
# -Уничтожение моего танка- #


mysql_query("INSERT INTO `log` SET `text` = '".$utext."',`pid` = '".$uid."',`who` = '0',`time` = '".(time())."' ");



header("Location:/battle/menu/");
exit();
}





echo "
<div class='trnt-block'>
<div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='wrap4'><div class='wrap5'><div class='wrap6'><div class='wrap7'><div class='wrap8'>
<div class='wrap-content cntr'>
";


echo "<table>";
echo "<tr>";
echo "<td class = 'cntr'>
Мой танк
<div class = 'ta_l'>
<img src = '/instalscript/images/icons/damage.jpg'><span class='green2'>".$myb['mha']."</span><br>
<img src = '/instalscript/images/icons/armor.jpg'><span class='green2'>".$myb['mhb']."</span><br>
<img src = '/instalscript/images/icons/durability.jpg'><span class='green2'>".$myb['mhc']."</span><br>
<img src = '/instalscript/images/icons/accuracy.jpg'><span class='green2'>".$myb['mho']."</span><br>
</div>
</td>";

echo "<td class = 'cntr'><img src = '/tankimage_".$myb['tid']."_1.jpg' width = '80%'></td>";

echo "<td>
Противник
<div class = 'ta_l'>
<img src = '/instalscript/images/icons/damage.jpg'><span class='green2'>".$myb['ha']."</span><br>
<img src = '/instalscript/images/icons/armor.jpg'><span class='green2'>".$myb['hb']."</span><br>
<img src = '/instalscript/images/icons/durability.jpg'><span class='green2'>".$myb['hc']."</span><br>
<img src = '/instalscript/images/icons/accuracy.jpg'><span class='green2'>".$myb['ho']."</span><br>
</div>
</td>";
echo "</tr>";


echo "
<tr>
<td class = 'cntr'><a title = 'Атака бронебойным снарядом' class = 'inbl thumb' href = '/battle/menu/atack/bb/'><span class='digit esmall'><span class='l'>&nbsp;</span><span class='m'>".$BB['kol']."</span><span class='r'>&nbsp;</span></span><span class = 'mask1'><img src = '/instalscript/images/amunition/snaryadBB.png'></span></a></td>
<td class = 'cntr'><a title = 'Атака осколочно-фугасным снарядом' class = 'inbl thumb' href = '/battle/menu/atack/of/'><span class='digit esmall'><span class='l'>&nbsp;</span><span class='m'>".$OF['kol']."</span><span class='r'>&nbsp;</span></span><span class = 'mask1'><img src = '/instalscript/images/amunition/snaryadOF.png'></span></a></td>
<td class = 'cntr'><a title = 'Атака кумулятивным снарядом' class = 'inbl thumb' href = '/battle/menu/atack/ks/'><span class='digit esmall'><span class='l'>&nbsp;</span><span class='m'>".$KS['kol']."</span><span class='r'>&nbsp;</span></span><span class = 'mask1'><img src = '/instalscript/images/amunition/snaryadKS.png'></span></a></td>
</tr>
";
echo "</table>";


echo "<div class = 'bot'><a class='simple-but border mb2' href = '/battle/menu/atack/'><span><span>Атаковать</span></span></a></div>";

echo "
</div>
</div></div></div></div></div></div></div></div>
</div>
";


break;
case'pvp':
# $pvp = mysql_fetch_assoc(mysql_query("SELECT * FROM `utank` WHERE `uid` != '".$uid."' ORDER BY RAND() "));

echo "
<div class='trnt-block'>
<div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='wrap4'><div class='wrap5'><div class='wrap6'><div class='wrap7'><div class='wrap8'>
<div class='wrap-content'>";
/*
if($pvp == null){
echo "<div class='mt5 mb5 small green1 cntr'>Подходящих противников нет</div>";
}
# */

echo "Выберите карту<br />";



$count = mysql_result(mysql_query("SELECT COUNT(*) FROM  `map` "),0);


$str = '2'; # кол-во записей для вывода
$url = "/battle/pvp"; # ссылка
$num = abs($is['2']); # текущая страница
$result = $count; # подсчёт



$q = mysql_query("SELECT * FROM `map` LIMIT ".page($num,$str,$result).",$str ");
while($map = mysql_fetch_assoc($q)){
# $usercount = mysql_result(mysql_query("SELECT COUNT(*) FROM `users` WHERE `idm` = '".$map['id']."' "),0)or die(mysql_error());
echo "<img src = '".str_replace("[type]","logo",$map['img'])."' width = '50%' >";
}
ppage($url,$num,$str,$result);



/*
$map_query = mysql_query("SELECT * FROM `map` ");
while($map = mysql_fetch_assoc($map_query)){
echo "<img src = '".str_replace("[type]","logo",$map['img'])."' width = '30%' >";
}
# */




echo "<div class = 'bot'><a class='simple-but border mb2' href='/battle/'><span><span>Вернуться в бои</span></span></a></div>";

echo "
</div>
</div></div></div></div></div></div></div></div>
</div>
";

break;
}

$mylog = mysql_fetch_assoc(mysql_query("SELECT * FROM `log` WHERE `pid` = '".$uid."'"));
if($mylog != null){
echo "
<div class='trnt-block'>
<div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='wrap4'><div class='wrap5'><div class='wrap6'><div class='wrap7'><div class='wrap8'>
<div class='wrap-content'>
";
if($is['1'] != 'menu' && $is['1'] != 'b'){
echo "<a class='simple-but border mb2' href = '/battle/clear/'><span><span>Очистить лог боя</span></span></a>";
}

$qlog = mysql_query("SELECT * FROM `log` WHERE `pid` = '".$uid."' ORDER BY `time` DESC LIMIT 10");
while($log = mysql_fetch_assoc($qlog)){
if($log['who'] == '0'){echo "<span class = 'esmall required'>".bbcode($log['text'],'1')."</span>";echo"<span class = 'esmall'>".clock($log['time'])."</span></br>";}
if($log['who'] == '1'){echo "<span class = 'esmall error'>".bbcode($log['text'],'1')."</span>";echo"<span class = 'esmall'>".clock($log['time'])."</span></br>";}
}
echo "
</div>
</div></div></div></div></div></div></div></div>
</div>
";
}


foot($uid);
?>