<?
	define('PROTECTOR', 1);
	ob_start();
	$head = 'catacomb';//фикс. места
	$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');
	if($udata[lvl]>=70){
		
		
		
		$uscr = mysql_query("SELECT * FROM `catacomb_user` WHERE id = '$udata[id]' LIMIT 1");
		$uscc=mysql_num_rows($uscr);
		$usc = mysql_fetch_array($uscr);
		
		$row = mysql_query("SELECT * FROM `catacomb_room` WHERE id = '$usc[id_room]' LIMIT 1");
		$avt=mysql_num_rows($row);
		$cat = mysql_fetch_array($row);
		
		$rowa = mysql_query("SELECT * FROM `catacomb` WHERE level = '$cat[level]' LIMIT 1");
		$avta=mysql_num_rows($rowa);
		$cata = mysql_fetch_array($rowa);
		
		$rowb = mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]' and status = 0 and type='mob'");
		$botc=mysql_num_rows($rowb);
		$bot = mysql_fetch_array($rowb);
		
		$rowbb = mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]' and status = 0 and type='boss'");
		$botcb=mysql_num_rows($rowbb);
		$botb = mysql_fetch_array($rowbb);
		
		if($uscc==0){		header("Location: index.php?");}
		if($avt==0){		header("Location: index.php?");}
		
		
		if(mysql_num_rows(mysql_query("SELECT * FROM `catacomb_user` WHERE id_room = $cat[id] and status = '0'"))){
			
			switch($_GET[mod]){
				default:
				
				if(mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]' and status = '0'")) and $cat[timer]==0){
					
					echo'<img src="pic/hp.png" alt=""/>'.$usc['hp'].' [ <img src="pic/main/naem.gif" alt=""/>'.($botc).'';
					if($botb[hp]>0){echo' <img src="pic/main/heroup.gif" alt=""/> - <img src="pic/hp.png" alt=""/>'.($botb[hp]).' ';}echo']</div><div class="menu">';
					if($usc[status]==0){
						
						
						if(!empty($_SESSION[amob]))
						{
							$rowbq = mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]' and status = 0 and id = '".sint($_SESSION[amob])."' LIMIT 1");
							$botcq=mysql_num_rows($rowbq);
							$botq = mysql_fetch_array($rowbq);
							if($botcq){
								
								echo'
								<table><tr><td>
								<img src="pic/catacomb/'.$botq[type].'/'.$botq[img].'.png" alt=""/>
								</td>
								<td>
								Тип - '.$botq[type].'<br/>
								[ <img src="pic/hp.png" alt=""/>'.($botq[hp]).' 
								- <img src="pic/pers/udar.png" alt="*"/>'.($botq[uron]).'
								- <img src="pic/pers/lovk.png" alt="*"/>'.($botq[lovk]).' ] <br/>
								<a href="?mod=atack&id='.$botq[id].'"><input type="submit" value="Атаковать" class="ibutton"></a>';
								if(!mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE agrit = '$udata[id]'"))){
									if(mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE agrit = 0 and type='mob' and id='$botq[id]'"))){
										echo' <a href="?mod=agrit&id='.$botq[id].'"><input type="submit" value="Агрить" class="ibutton"></a>';
									}}
									echo' </td>
									</tr>
									</table>
									';
							}}
							$rowbq = mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]' and status = 0 order by rand() DESC LIMIT 1");
							$botq = mysql_fetch_array($rowbq);
							echo'
							<table><tr><td>
							<img src="pic/catacomb/'.$botq[type].'/'.$botq[img].'.png" alt=""/>
							</td>
							<td>
							Тип - '.$botq[type].'<br/>
							[ <img src="pic/hp.png" alt=""/>'.($botq[hp]).' 
							- <img src="pic/pers/udar.png" alt="*"/>'.($botq[uron]).'
							- <img src="pic/pers/lovk.png" alt="*"/>'.($botq[lovk]).' ] <br/>
							<a href="?mod=atack&id='.$botq[id].'"><input type="submit" value="Атаковать" class="ibutton"></a>';
							if(!mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE agrit = '$udata[id]'"))){
								if(mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE agrit = 0 and type='mob' and id='$botq[id]'"))){
									echo' <a href="?mod=agrit&id='.$botq[id].'"><input type="submit" value="Агрить" class="ibutton"></a>';
								}}
								echo' </td>
								</tr>
								</table>
								';
								$rowlo = mysql_query("SELECT * FROM `catacomb_log` WHERE id = '$udata[id]' and room=$usc[id_room] ORDER by `id_log` DESC LIMIT 10");
								$avtlo=mysql_num_rows($rowlo);
								if($avtlo){
									echo'</div><div class="menu">';
									while($catlo = mysql_fetch_array($rowlo)){
									echo''.$catlo[text].'<br/>';}
									
								}
								
								}else{
								echo'<center>Вы убиты!Ждите окончания боя.<br/><a href="?"><input type="submit" value="Обновить" class="ibutton"></a></center>';
								$rowlo = mysql_query("SELECT * FROM `catacomb_log` WHERE room=$usc[id_room] ORDER by `id_log` DESC LIMIT 10");
								$avtlo=mysql_num_rows($rowlo);
								if($avtlo){
									echo'</div><div class="menu">';
									while($catlo = mysql_fetch_array($rowlo)){
										$rowlou = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE id = $catlo[id] LIMIT 1"));
									echo'['.$rowlou[usr].'] '.$catlo[text].'<br/>';}
									
								}
								
					}
					}else{
					$rara=rand(1,$uscc);
					$rara2=1;
					if(mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]'")) and $cat[timer]==0 and $cat[level]<=$cset_urovni){
						$roweu = mysql_query("SELECT * FROM `catacomb_user` WHERE id_room = $usc[id_room]");
						while($cateue = mysql_fetch_array($roweu)){
							$rowecatu = mysql_query("SELECT * FROM `users` WHERE id = $cateue[id]");
							$catecatu = mysql_fetch_array($rowecatu);
							
							$money=ceil($cateue[nanes]/100)*$cata[money];
							$exp=ceil($cateue[nanes]/100)*$cata[exp];
							$bon=$cateue[kill]*$cata[money];
							$resultat=$resultat."$catecatu[usr] ,нанес $cateue[nanes] урона и убил $cateue[kill] противников.<br/>Получает: $money + $bon (бонус) золота и $exp опыта";
							if($rara2=$rara){
								$rca = rand(1,100);
								if($cata[stuff_chance]>$rca and $cateue[nanes]>5000){
									$stuff= explode(",", $cata[stuff]);
									$st = rand(0,count ($stuff));
									
									$rep = mysql_query("SELECT * FROM `shops` WHERE`id`='$stuff[$st]'");
									
									$mags = mysql_fetch_array($rep);
									
									mysql_query("INSERT INTO
									`weapon` SET
									`usr` = '$catecatu[usr]',
									`tip` = '$mags[tip]',
									`kompl` = '$mags[kompl]',
									`proch` = '10',
									`name` = '$mags[name]',
									`cena` = '$mags[cena]',
									`hp` = '$mags[hp]',
									`mp` = '$mags[mp]',
									`krit` = '$mags[krit]',
									`ukrit` = '$mags[ukrit]',
									`sila` = '$mags[sila]',
									`lovk` = '$mags[lovk]',
									`prot` = '$mags[prot]',
									`nlvl` = '$mags[nlvl]',
									`image` = 'not'") or die (mysql_error());
									$resultat=$resultat.",а также вещь [$mags[name]]";
									
									
								}
							}$rara2++;
							if($cat[level]>=$cset_urovni){
								$text = 'Подземелья пройдены.'.$resultat.'';
								
								$to = $catecatu[usr];
								$time = date("H:i d.m.y");
								mysql_query("INSERT INTO `msg_r` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
								mysql_query("INSERT INTO `msg_i` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time',`mail_msg` = '$text'");
							}
							$resultat=$resultat." .<br/><br/>";
							mysql_query("UPDATE catacomb_user SET `hp` = $catecatu[hpall] ,`nanes` = '0',`status`= '0' ,`kill`= '0'   WHERE id=$catecatu[id]");
							
							mysql_query("UPDATE users SET `money` = `money` + $money+$bon , `exp`=`exp` +$exp WHERE id= $catecatu[id]");
						}
						mysql_query("DELETE FROM catacomb_log WHERE room = $usc[id_room]");
						mysql_query("DELETE FROM catacomb_bot WHERE room = $usc[id_room]");
						if($cat[level]<$cset_urovni){mysql_query("INSERT INTO catacomb_log SET room = '$usc[id_room]', id = '0', text = 'Уровень пройден!<br/>Результаты:<br/><br/> $resultat'");}else{
							mysql_query("DELETE FROM catacomb_user WHERE id_room = $usc[id_room]");
							mysql_query("DELETE FROM catacomb_room WHERE id = $usc[id_room]");
						}
						header("Location: catacomb.php?");
					}
					
					
					
					if($cat[level]==$cset_urovni){
						header("Location: index.php?");
					}
					
				if($cat[timer]==1){
				echo'</div><div class="menu"><center>Бой через '.($cat[timer_time]-time()).' сек.<br/><a href="?"><input type="submit" value="Обновить" class="ibutton"></a></center>';
				if(time()>=$cat[timer_time]){$xc = $cat[level]+1;
				mysql_query("DELETE FROM catacomb_log WHERE room = $usc[id_room]");
				mysql_query("UPDATE catacomb_room SET timer = '0', level = '$xc' WHERE id=$usc[id_room]");
				header("Location: /catacomb.php?");
				}
				}else{echo'<center>Ждите начала боя!<br/><a href="?"><input type="submit" value="Обновить" class="ibutton"></a></center>';}
				if($cat[timer]==0){
				if($udata[id]==$cat[lider]){ECHO'</div><div class="menu">';
				echo'<a href="?mod=start&id='.$id.'"><input type="submit" value="Старт" class="ibutton"></a>';}
				}
				
				
				$rowlo = mysql_query("SELECT * FROM `catacomb_log`WHERE id=0 and room=$usc[id_room] ORDER by `id_log` DESC LIMIT 1");
				$avtlo=mysql_num_rows($rowlo);
				if($avtlo){
				echo'</div><div class="menu">';
				while($catlo = mysql_fetch_array($rowlo)){
				
				echo''.$catlo[text].'<br/>';}
				
				}
				}
				break;
				
				case'start':	
				if(!mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]'"))){
				if($cat[timer]==0){
				if($udata[id]==$cat[lider]){
				
				$tt=time()+$cset_time;
				mysql_query("UPDATE catacomb_room SET timer = '1', timer_time = '$tt' WHERE id=$usc[id_room]");
				$rowecat = mysql_query("SELECT * FROM `catacomb` WHERE level =  $cat[level]+1");
				$catecat = mysql_fetch_array($rowecat);
				$ra=rand($catecat[bot_min],$catecat[bot_max]);
				for($i=0;$i<$ra;$i++){
				mysql_query("INSERT INTO catacomb_bot SET room = '$usc[id_room]', hp = '$catecat[bot_hp]', uron = '$catecat[bot_uron]', lovk = '$catecat[bot_lovk]', type = 'mob', img = '".rand(1,40)."'");
				}
				mysql_query("INSERT INTO catacomb_bot SET room = '$usc[id_room]', hp = '$catecat[boss_hp]', uron = '$catecat[boss_uron]', lovk = '$catecat[boss_lovk]', type = 'boss', img = '".rand(1,10)."'");
				}}}
				header("Location: catacomb.php?");
				break;	
				
				case'agrit':	
				if(!mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE agrit = '$udata[id]'"))){
				if(mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE agrit = 0 and type='mob' and id='".sint($_GET[id])."'"))){
				mysql_query("UPDATE catacomb_bot SET agrit = '$udata[id]' WHERE id='".sint($_GET[id])."'");
				}}
				header("Location: catacomb.php?");
				break;
				
				case'atack':
				if($usc[tatus]==0){
				$rowbba = mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]' and status = 0 and id='".sint($_GET[id])."'");
				$botcba=mysql_num_rows($rowbba);
				$botba = mysql_fetch_array($rowbba);
				if($botcba)
				{
				if($cat[level]==1){$lovk=15;$lovkm=10;}elseif($cat[level]==2){$lovk=10;$lovkm=6;}elseif($cat[level]>=$cset_urovni){$lovk=5;$lovkm=3;}
				$rca = rand(1,100);
				if($botba[type]=="mob"){$who="Стражника";$whot="Стражник";$whop="Стражнику";}else{$who="Короля";$whot="Король";$whop="Королю";}
				if($rca>$lovkm){
				
				$uron=rand(ceil($udata[sila]/1.5),$udata[sila]);
				if(($botba[hp]-$uron)>0){
				mysql_query("UPDATE catacomb_bot SET `hp` = `hp` - $uron WHERE id=$botba[id]");
				mysql_query("UPDATE catacomb_user SET `nanes` = `nanes` + $uron WHERE id=$udata[id]");
				mysql_query("INSERT INTO catacomb_log SET room = '$usc[id_room]', id = '$udata[id]', text = 'Вы бьете $who на $uron'");
				}else{
				mysql_query("UPDATE catacomb_bot SET `hp` = `hp`-$uron,status = 1 WHERE id=$botba[id]");
				mysql_query("UPDATE catacomb_user SET `nanes` = `nanes` + $uron , `kill`= `kill` + 1 WHERE id=$udata[id]");
				mysql_query("INSERT INTO catacomb_log SET `room` = '$usc[id_room]', id = '$udata[id]', text = '<font color=red>Вы убиваете $who ,забрав у него последние $botba[hp] жизней</font>'");
				}
				}else{
				mysql_query("INSERT INTO catacomb_log SET room = '$usc[id_room]', id = '$udata[id]', text = '<font color=#fa8e47>Вы промахнулись по $whop</font>'");
				}
				
				$rca = rand(1,100);
				if($botba[agrit]==0 or $botba[agrit]==$udata[id]){
				if($rca>$lovk){
				
				$uron=rand(ceil($botba[uron]/1.5),$botba[uron]);
				
				if(($usc[hp]-$uron)>0){
				mysql_query("UPDATE catacomb_user SET `hp` = `hp` - $uron WHERE id=$udata[id]");
				mysql_query("INSERT INTO catacomb_log SET room = '$usc[id_room]', id = '$udata[id]', text = '$whot бьет вас на $uron'");
				}else{
				mysql_query("UPDATE catacomb_user SET `hp` = `hp`-$uron,status = 1 WHERE id=$udata[id]");
				mysql_query("INSERT INTO catacomb_log SET `room` = '$usc[id_room]', id = '$udata[id]', text = '<font color=red>$whot убивает вас,забрав последние $usc[hp] жизней</font>'");
				}
				
				}else{
				mysql_query("INSERT INTO catacomb_log SET room = '$usc[id_room]', id = '$udata[id]', text = '<font color=#fa8e47>$whot промахнулся по вам</font>'");
				}
				}
				
				$_SESSION[amob]=sint($_GET[id]);
				}
				
				}
				header("Location: /catacomb.php?");
				break;
				}
				
				
				
				}else{
				
				if(mysql_num_rows(mysql_query("SELECT * FROM `catacomb_bot` WHERE room = '$usc[id_room]'")) and $cat[timer]==0 and $cat[level]<=$cset_urovni){
				$roweu = mysql_query("SELECT * FROM `catacomb_user` WHERE id_room = $usc[id_room]");
				while($cateue = mysql_fetch_array($roweu)){
				$rowecatu = mysql_query("SELECT * FROM `users` WHERE id = $cateue[id]");
				$catecatu = mysql_fetch_array($rowecatu);
				
				
				$text = 'Ваша команда была разбита!';
				$to = $catecatu[usr];
				$time = date("H:i d.m.y");
				mysql_query("INSERT INTO `msg_r` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
				mysql_query("INSERT INTO `msg_i` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time',`mail_msg` = '$text'");
				}
				mysql_query("DELETE FROM catacomb_log WHERE room = $usc[id_room]");
				mysql_query("DELETE FROM catacomb_bot WHERE room = $usc[id_room]");
				mysql_query("DELETE FROM catacomb_user WHERE id_room = $usc[id_room]");
				mysql_query("DELETE FROM catacomb_room WHERE id = $usc[id_room]");
				}
				header("Location: index.php?");
				
				
				}
				}else{
				echo'</div><div class="menu">
				Доступоно с 70 лвл!
				</div>';
				}
				@include('files/down.php');
				?>								