<?php
include_once ('./core/base.php');
falseauth();

$header = 'Локация';

include_once ('./core/head.php');

echo '<center><div style="width: 350px; position: relative;">';

//Указатель
if($u['training_step'] == 12) echo '<div style="position: absolute; bottom: -1%; right: 0%; z-index: 9999;"><img src="/images/cursor.gif"></div>';
if($u['training_step'] == 13) echo '<div style="position: absolute; bottom: 71%; right: 39%; z-index: 9999;"><a href="/training/next/"><img src="/images/cursor.gif"></a></div>';
if($u['training_step'] == 16) echo '<div style="position: absolute; bottom: 71%; right: 39%; z-index: 9999;"><a href="/training/next/"><img src="/images/cursor.gif"></a></div>';

if($u['training_step'] == 14 AND $u['x'] > 5) echo '<div style="position: absolute; bottom: 39%; right: 52%; z-index: 9999;"><img src="/images/cursor.gif"></div>';
if($u['training_step'] == 14 AND $u['x'] < 8) echo '<div style="position: absolute; bottom: 39%; right: 25%; z-index: 9999;"><img src="/images/cursor.gif"></div>';

$loc_up = $base -> query("SELECT * FROM `locations` WHERE `x` = '".$u['x']."' AND `y` = '".($u['y'] + 1)."' AND `world` = '".$u['world']."' LIMIT 1") -> fetch_assoc();
$loc_down = $base -> query("SELECT * FROM `locations` WHERE `x` = '".$u['x']."' AND `y` = '".($u['y'] - 1)."' AND `world` = '".$u['world']."' LIMIT 1") -> fetch_assoc();
$loc_left = $base -> query("SELECT * FROM `locations` WHERE `x` = '".($u['x'] - 1)."' AND `y` = '".$u['y']."' AND `world` = '".$u['world']."' LIMIT 1") -> fetch_assoc();
$loc_right = $base -> query("SELECT * FROM `locations` WHERE `x` = '".($u['x'] + 1)."' AND `y` = '".$u['y']."' AND `world` = '".$u['world']."' LIMIT 1") -> fetch_assoc();

$_GET['move'] = fl($_GET['move']);
$_GET['pve'] = fl($_GET['pve']);
$_GET['pvb'] = fl($_GET['pvb']);
$_GET['pvp'] = fl($_GET['pvp']);

if($_GET['pvb']){
	if($uloc['boss'] > 0){
		$boss_q = $base -> query('SELECT * FROM `mobs` WHERE `boss` = "1" AND `id` = "'.$uloc['boss'].'" LIMIT 1');
		$boss_nr = $boss_q -> num_rows;
		if($boss_nr > 0){
			$boss = $boss_q -> fetch_assoc();
			if((time() - $boss['boss_time_dead']) >= $boss['boss_respawn_time']){
				if(!$uparty){
					$base -> query('INSERT INTO `pvb` SET
					`user` = "'.$u['id'].'",
					`boss` = "'.$boss['id'].'",
					`boss_time_attack` = "'.time().'"');
					$pvb_id = mysqli_insert_id($base);
					$base -> query('INSERT INTO `pvb_log` SET
						`pvb_id` = "'.$pvb_id.'",
						`text` = "'.$u['login'].' напал на '.$boss['name'].', битва началась!"');
					$base -> query('UPDATE `users` SET `battle` = "pvb" WHERE `id` = "'.$u['id'].'" LIMIT 1');
					header('Location: /pvb/');
					exit;
				}else{
					if($uparty['first_user'] == $u['id']){
						if($uparty['first_user']){
							$base -> query('INSERT INTO `pvb` SET
							`party` = "'.$uparty['id'].'",
							`user` = "'.$uparty['first_user'].'",
							`boss` = "'.$boss['id'].'",
							`boss_time_attack` = "'.time().'"');
							$base -> query('UPDATE `users` SET `battle` = "pvb" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
						}
						if($uparty['second_user']){
							$base -> query('INSERT INTO `pvb` SET
							`party` = "'.$uparty['id'].'",
							`user` = "'.$uparty['second_user'].'",
							`boss` = "'.$boss['id'].'",
							`boss_time_attack` = "'.time().'"');
							$base -> query('UPDATE `users` SET `battle` = "pvb" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
						}
						if($uparty['third_user']){
							$base -> query('INSERT INTO `pvb` SET
							`party` = "'.$uparty['id'].'",
							`user` = "'.$uparty['third_user'].'",
							`boss` = "'.$boss['id'].'",
							`boss_time_attack` = "'.time().'"');
							$base -> query('UPDATE `users` SET `battle` = "pvb" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
						}
						$base -> query('INSERT INTO `pvb_log` SET
							`party` = "'.$uparty['id'].'",
							`text` = "'.$u['login'].' напал на '.$boss['name'].', битва началась!"');
						header('Location: /pvb/');
						exit;
					}else{
						$_SESSION['message'] = 'Начать бой может только лидер команды.';
						header('Location: /location/');
						exit;
					}
				}
			}else{
				header('Location: /location/');
				exit;
			}
		}else{
			header('Location: /location/');
			exit;
		}
	}else{
		header('Location: /location/');
		exit;
	}
}

if($_GET['pvp']){
	if($uloc['arena'] > 0){
		$user_q = $base -> query('SELECT * FROM `users` WHERE `world` = "'.$u['world'].'" AND `x` = "'.$u['x'].'" AND `y` = "'.$u['y'].'" AND `battle` = "no" AND `id` = "'.$_GET['pvp'].'" LIMIT 1');
		$user_nr = $user_q -> num_rows;
		if($user_nr > 0){
			$user = $user_q -> fetch_assoc();
			$usparty_q = $base -> query("SELECT * FROM `party` WHERE `first_user` = '".$user['id']."' OR `second_user` = '".$user['id']."' OR `third_user` = '".$user['id']."' LIMIT 1");
			$usparty_nr = $usparty_q -> num_rows;
			if($usparty_nr > 0){
				$usparty = $usparty_q -> fetch_assoc();
				if($usparty['first_user'] == $user['id']){
					$base -> query('DELETE FROM `party` WHERE `id` = "'.$usparty['id'].'"');
					$base -> query('DELETE FROM `party_chat` WHERE `party_id` = "'.$usparty['id'].'"');
					$base -> query('DELETE FROM `pve_log` WHERE `party` = "'.$usparty['id'].'"');
					$base -> query('DELETE FROM `pvb_log` WHERE `party` = "'.$usparty['id'].'"');
				}elseif($usparty['second_user'] == $user['id']){
					if($usparty['third_user'] != 0) $base -> query('UPDATE `party` SET `second_user` = "'.$usparty['third_user'].'", `third_user` = "0" WHERE `id` = "'.$usparty['id'].'" LIMIT 1');
					else $base -> query('UPDATE `party` SET `second_user` = "0" WHERE `id` = "'.$usparty['id'].'" LIMIT 1');
				}elseif($usparty['third_user'] == $user['id']){
					$base -> query('UPDATE `party` SET `third_user` = "0" WHERE `id` = "'.$usparty['id'].'" LIMIT 1');
				}
			}
			if($uparty){
				if($uparty['first_user'] == $u['id']){
					$base -> query('DELETE FROM `party` WHERE `id` = "'.$uparty['id'].'"');
					$base -> query('DELETE FROM `party_chat` WHERE `party_id` = "'.$uparty['id'].'"');
					$base -> query('DELETE FROM `pve_log` WHERE `party` = "'.$uparty['id'].'"');
					$base -> query('DELETE FROM `pvb_log` WHERE `party` = "'.$uparty['id'].'"');
				}elseif($uparty['second_user'] == $u['id']){
					if($uparty['third_user'] != 0) $base -> query('UPDATE `party` SET `second_user` = "'.$uparty['third_user'].'", `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
					else $base -> query('UPDATE `party` SET `second_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
				}elseif($uparty['third_user'] == $u['id']){
					$base -> query('UPDATE `party` SET `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
				}
			}
			$base -> query('INSERT INTO `pvp` SET
			`first_user` = "'.$u['id'].'",
			`second_user` = "'.$user['id'].'"');
			$pvp_id = mysqli_insert_id($base);
			$base -> query('INSERT INTO `pvp_log` SET
				`pvp_id` = "'.$pvp_id.'",
				`text` = "'.$u['login'].' атаковал '.$user['login'].', битва началась!"');
			$base -> query('UPDATE `users` SET `battle` = "pvp" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			$base -> query('UPDATE `users` SET `battle` = "pvp" WHERE `id` = "'.$user['id'].'" LIMIT 1');
			header('Location: /pvp/');
			exit;
		}else{
			$_SESSION['message'] = 'Игрок находится в битве.';
			header('Location: /location/');
			exit;
		}
	}else{
		header('Location: /location/');
		exit;
	}
}

//Если игрок в команде, и не является лидером, то делаем так, чтобы он не мог ходить
if($_GET['move']){
	if($uparty){
		if($uparty['first_user'] != $u['id']){
			$_SESSION['message'] = 'Направлять команду может только лидер команды.';
			header('Location: /location/');
			exit;
		}
	}
}
if($_GET['move'] == 'up' AND $loc_up != 0 AND $uloc['block_up'] != 1 AND $u['training_step'] == 0){
	$mobs_q = $base -> query('SELECT * FROM `mobs` WHERE `location_type` = "'.$loc_up['location_type'].'" ORDER BY RAND()');
	$mobs_nr = $mobs_q -> num_rows;
	if($mobs_nr > 0){
		$chance_battle = mt_rand(0,100);
		if($chance_battle < 30){
			$mob = $mobs_q -> fetch_assoc();
			if(!$uparty){
				$base -> query('INSERT INTO `pve` SET
				`user` = "'.$u['id'].'",
				`mob` = "'.$mob['id'].'",
				`mob_hp` = "'.$mob['hp'].'",
				`mob_max_hp` = "'.$mob['hp'].'",
				`mob_time_attack` = "'.time().'"');
				$pve_id = mysqli_insert_id($base);
				$base -> query('INSERT INTO `pve_log` SET
					`pve_id` = "'.$pve_id.'",
					`text` = "'.$mob['name'].' напал на '.$u['login'].', битва началась!"');
				$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			}else{
				if($uparty['first_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['first_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
				}
				if($uparty['second_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['second_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
				}
				if($uparty['third_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['third_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
				}
				$leader = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1') -> fetch_assoc();
				$base -> query('INSERT INTO `pve_log` SET
				`party_id` = "'.$uparty['id'].'",
				`text` = "'.$mob['name'].' напал на команду '.$leader['login'].', битва началась!"');
			}
		}
	}
	if(!$uparty){
		$base -> query('UPDATE `users` SET `y` = `y` + "1", `direction` = "_up" WHERE `id` = "'.$u['id'].'" LIMIT 1');
	}else{
		if($uparty['first_user']) $base -> query('UPDATE `users` SET `y` = `y` + "1", `direction` = "_up" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
		if($uparty['second_user']) $base -> query('UPDATE `users` SET `y` = `y` + "1", `direction` = "_up" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
		if($uparty['third_user']) $base -> query('UPDATE `users` SET `y` = `y` + "1", `direction` = "_up" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
	}
	header('Location: /location/');
	exit;
}
if($_GET['move'] == 'down' AND $loc_down != 0 AND $uloc['block_down'] != 1 AND $u['training_step'] == 0){
	$mobs_q = $base -> query('SELECT * FROM `mobs` WHERE `location_type` = "'.$loc_down['location_type'].'" ORDER BY RAND()');
	$mobs_nr = $mobs_q -> num_rows;
	if($mobs_nr > 0){
		$chance_battle = mt_rand(0,100);
		if($chance_battle < 30){
			$mob = $mobs_q -> fetch_assoc();
			if(!$uparty){
				$base -> query('INSERT INTO `pve` SET
				`user` = "'.$u['id'].'",
				`mob` = "'.$mob['id'].'",
				`mob_hp` = "'.$mob['hp'].'",
				`mob_max_hp` = "'.$mob['hp'].'",
				`mob_time_attack` = "'.time().'"');
				$pve_id = mysqli_insert_id($base);
				$base -> query('INSERT INTO `pve_log` SET
					`pve_id` = "'.$pve_id.'",
					`text` = "'.$mob['name'].' напал на '.$u['login'].', битва началась!"');
				$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			}else{
				if($uparty['first_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['first_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
				}
				if($uparty['second_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['second_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
				}
				if($uparty['third_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['third_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
				}
				$leader = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1') -> fetch_assoc();
				$base -> query('INSERT INTO `pve_log` SET
				`party_id` = "'.$uparty['id'].'",
				`text` = "'.$mob['name'].' напал на команду '.$leader['login'].', битва началась!"');
			}
		}
	}
	if(!$uparty){
		$base -> query('UPDATE `users` SET `y` = `y` - "1", `direction` = "" WHERE `id` = "'.$u['id'].'" LIMIT 1');
	}else{
		if($uparty['first_user']) $base -> query('UPDATE `users` SET `y` = `y` - "1", `direction` = "" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
		if($uparty['second_user']) $base -> query('UPDATE `users` SET `y` = `y` - "1", `direction` = "" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
		if($uparty['third_user']) $base -> query('UPDATE `users` SET `y` = `y` - "1", `direction` = "" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
	}
	header('Location: /location/');
	exit;
}
if($u['training_step'] == 0 OR $u['training_step'] == 14){
if($u['training_step'] == 14 AND $u['x'] == 5){
	//Не пускаем
}else{
if($_GET['move'] == 'left' AND $loc_left != 0 AND $uloc['block_left'] != 1){
	$mobs_q = $base -> query('SELECT * FROM `mobs` WHERE `location_type` = "'.$loc_left['location_type'].'" ORDER BY RAND()');
	$mobs_nr = $mobs_q -> num_rows;
	if($mobs_nr > 0){
		$chance_battle = mt_rand(0,100);
		if($chance_battle < 30){
			$mob = $mobs_q -> fetch_assoc();
			if(!$uparty){
				$base -> query('INSERT INTO `pve` SET
				`user` = "'.$u['id'].'",
				`mob` = "'.$mob['id'].'",
				`mob_hp` = "'.$mob['hp'].'",
				`mob_max_hp` = "'.$mob['hp'].'",
				`mob_time_attack` = "'.time().'"');
				$pve_id = mysqli_insert_id($base);
				$base -> query('INSERT INTO `pve_log` SET
					`pve_id` = "'.$pve_id.'",
					`text` = "'.$mob['name'].' напал на '.$u['login'].', битва началась!"');
				$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			}else{
				if($uparty['first_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['first_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
				}
				if($uparty['second_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['second_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
				}
				if($uparty['third_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['third_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
				}
				$leader = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1') -> fetch_assoc();
				$base -> query('INSERT INTO `pve_log` SET
				`party_id` = "'.$uparty['id'].'",
				`text` = "'.$mob['name'].' напал на команду '.$leader['login'].', битва началась!"');
			}
		}
	}
	if(!$uparty){
		$base -> query('UPDATE `users` SET `x` = `x` - "1", `direction` = "_left" WHERE `id` = "'.$u['id'].'" LIMIT 1');
	}else{
		if($uparty['first_user']) $base -> query('UPDATE `users` SET `x` = `x` - "1", `direction` = "_left" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
		if($uparty['second_user']) $base -> query('UPDATE `users` SET `x` = `x` - "1", `direction` = "_left" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
		if($uparty['third_user']) $base -> query('UPDATE `users` SET `x` = `x` - "1", `direction` = "_left" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
	}
	header('Location: /location/');
	exit;
}
}
}
if($u['training_step'] == 0 OR $u['training_step'] == 14){
if($u['training_step'] == 14 AND $u['x'] == 8){
	//Не пускаем
}else{
if($_GET['move'] == 'right' AND $loc_right != 0 AND $uloc['block_right'] != 1){
	$mobs_q = $base -> query('SELECT * FROM `mobs` WHERE `location_type` = "'.$loc_right['location_type'].'" ORDER BY RAND()');
	$mobs_nr = $mobs_q -> num_rows;
	if($mobs_nr > 0){
		$chance_battle = mt_rand(0,100);
		if($chance_battle < 30){
			$mob = $mobs_q -> fetch_assoc();
			if(!$uparty){
				$base -> query('INSERT INTO `pve` SET
				`user` = "'.$u['id'].'",
				`mob` = "'.$mob['id'].'",
				`mob_hp` = "'.$mob['hp'].'",
				`mob_max_hp` = "'.$mob['hp'].'",
				`mob_time_attack` = "'.time().'"');
				$pve_id = mysqli_insert_id($base);
				$base -> query('INSERT INTO `pve_log` SET
					`pve_id` = "'.$pve_id.'",
					`text` = "'.$mob['name'].' напал на '.$u['login'].', битва началась!"');
				$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			}else{
				if($uparty['first_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['first_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
				}
				if($uparty['second_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['second_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
				}
				if($uparty['third_user']){
					$base -> query('INSERT INTO `pve` SET
					`party` = "'.$uparty['id'].'",
					`user` = "'.$uparty['third_user'].'",
					`mob` = "'.$mob['id'].'",
					`mob_hp` = "'.$mob['hp'].'",
					`mob_max_hp` = "'.$mob['hp'].'",
					`mob_time_attack` = "'.time().'"');
					$base -> query('UPDATE `users` SET `battle` = "pve" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
				}
				$leader = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1') -> fetch_assoc();
				$base -> query('INSERT INTO `pve_log` SET
				`party_id` = "'.$uparty['id'].'",
				`text` = "'.$mob['name'].' напал на команду '.$leader['login'].', битва началась!"');
			}
		}
	}
	if(!$uparty){
		$base -> query('UPDATE `users` SET `x` = `x` + "1", `direction` = "_right" WHERE `id` = "'.$u['id'].'" LIMIT 1');
	}else{
		if($uparty['first_user']) $base -> query('UPDATE `users` SET `x` = `x` + "1", `direction` = "_right" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
		if($uparty['second_user']) $base -> query('UPDATE `users` SET `x` = `x` + "1", `direction` = "_right" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
		if($uparty['third_user']) $base -> query('UPDATE `users` SET `x` = `x` + "1", `direction` = "_right" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
	}
	header('Location: /location/');
	exit;
}
}
}
if($_GET['move'] == 'teleport' AND $uloc['teleport_to_world'] > 0){
	if(!$uparty){
		$base -> query('UPDATE `users` SET `world` = "'.$uloc['teleport_to_world'].'", `x` = "'.$uloc['teleport_to_x'].'", `y` = "'.$uloc['teleport_to_y'].'", `direction` = "" WHERE `id` = "'.$u['id'].'" LIMIT 1');
	}else{
		if($uparty['first_user'] == $u['id']){
			if($uparty['first_user']) $base -> query('UPDATE `users` SET `world` = "'.$uloc['teleport_to_world'].'", `x` = "'.$uloc['teleport_to_x'].'", `y` = "'.$uloc['teleport_to_y'].'", `direction` = "" WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1');
			if($uparty['second_user']) $base -> query('UPDATE `users` SET `world` = "'.$uloc['teleport_to_world'].'", `x` = "'.$uloc['teleport_to_x'].'", `y` = "'.$uloc['teleport_to_y'].'", `direction` = "" WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1');
			if($uparty['third_user']) $base -> query('UPDATE `users` SET `world` = "'.$uloc['teleport_to_world'].'", `x` = "'.$uloc['teleport_to_x'].'", `y` = "'.$uloc['teleport_to_y'].'", `direction` = "" WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1');
		}else{
			$_SESSION['message'] = 'Направлять команду может только лидер команды.';
		}
	}
	header('Location: /location/');
	exit;
}


$wave = 1;
$table = 1;
$x = $u['x'] - 3;
$y = $u['y'] + 3;


echo '<div class="border" style="margin-top: 5px; width: 100%; max-width: 343px; height: 344px; box-sizing: border-box;"><div style="background-image: url(/images/locations/worlds/'.$uloc['world'].'/world_out.png); width: 100%; max-width: 336px; height: 336px; background-color: #101010; position: relative; word-wrap: break-word;"><table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse;" width="100%" height="336px">';

//Повышение уровня
if($u['level'] != 50 && $u['exp'] >= $lexp['exp'] && $u['training_step'] == 0){
	$exp = $u['exp'] - $lexp['exp'];
	$g = $u['level'] * 5;
	$base -> query("UPDATE `users` SET
		`level` = `level` + '1',
		`exp` = '".$exp."',
		`gold` = `gold` + '".$g."',
		`hp` = '".$u['max_hp']."',
		`mp` = '".$u['max_mp']."',
		`character_points` = `character_points` + '5',
		`skill_points` = `skill_points` + '1'
		WHERE `id` = '".$u['id']."' LIMIT 1");
	echo '<div style="position: absolute; top: 0.5%; left: 0.5%; width: 48%; font-size: 11px; text-shadow: black 1px 1px 1px; color: white;">';
	echo '<div class="fast_log">';
	echo '<div style="padding: 3px;"><b>Уровень повышен!</b><br/><img src="/images/icons/gold.png" width="16px"><font color="gold">'.$g.'</font> <img src="/images/icons/character_points.png" width="16px">5 <img src="/images/icons/skill_points.png" width="16px">1';
	echo '</div></div></div>';
}

//Уведомления
if($_SESSION['message'] != '' OR $u['last_log'] != ''){
	if($u['training_step'] == 0){
		if($u['exp'] < $lexp['exp']){
			echo '<div style="position: absolute; top: 1%; left: 1%; width: 48%; font-size: 11px; text-shadow: black 1px 1px 1px; color: white;">';
			echo '<div class="fast_log">';
			if($u['last_log'] != ''){
				echo '<div style="text-align: left; padding-left: 5px;">'.$u['last_log'].'</div>';
				$base -> query('UPDATE `users` SET `last_log` = "" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			}elseif($_SESSION['message'] != ''){
				echo '<div style="text-align: left; padding-left: 5px;">'.$_SESSION['message'].'</div>';
				unset($_SESSION['message']);
			}
		echo '</div></div>';
	}
	}
}

//Приглашения в клан
$clan_invitations_q = $base -> query("SELECT * FROM `clan_invitation` WHERE `invitee_user` = '".$u['id']."' ORDER BY `id` DESC LIMIT 1");
$clan_invitations_nr = $clan_invitations_q -> num_rows;
if($clan_invitations_nr > 0 AND $u['training_step'] == 0){
	$clan_invitation = $clan_invitations_q -> fetch_assoc();
	$who = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$clan_invitation['user'].'"') -> fetch_assoc();
	$clan = $base -> query('SELECT * FROM `clans` WHERE `id` = "'.$who['clan'].'"') -> fetch_assoc();
	echo '<div style="position: absolute; top: 1%; right: 1%; width: 48%; font-size: 11px; text-shadow: black 1px 1px 1px; color: white;">';
	echo '<div class="fast_log">';
	echo '<div style="padding-left: 1.5%;"><b>'.$who['login'].'</b> приглашает вас в клан <b>'.$clan['name'].'</b>.<br/>
	<a class="btn_mini" href="/clan/invite/'.$clan_invitation['id'].'/yes/">Принять</a> <a class="btn_mini" href="/clan/invite/'.$clan_invitation['id'].'/no/">Отказать</a></div>';
	echo '</div></div>';
}

//Приглашения в команду
$party_invitations_q = $base -> query("SELECT * FROM `party_invitation` WHERE `invitee_user` = '".$u['id']."' ORDER BY `id` DESC LIMIT 1");
$party_invitations_nr = $party_invitations_q -> num_rows;
if($party_invitations_nr > 0 AND $clan_invitations_nr <= 0 AND $u['training_step'] == 0){
	$party = $party_invitations_q -> fetch_assoc();
	$who = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$party['user'].'"') -> fetch_assoc();
	echo '<div style="position: absolute; top: 1%; right: 1%; width: 48%; font-size: 11px; text-shadow: black 1px 1px 1px; color: white;">';
	echo '<div class="fast_log">';
	echo '<div style="padding-left: 1.5%;"><b>'.$who['login'].'</b> приглашает в команду.<br/>
	<a class="btn_mini" href="/party/'.$party['id'].'/yes/">Принять</a> <a class="btn_mini" href="/party/'.$party['id'].'/no/">Отказать</a></div>';
	echo '</div></div>';
}

//Название локации
if($party_invitations_nr <= 0 AND $clan_invitations_nr <= 0 AND $u['training_step'] == 0){
	if($uloc['boss'] > 0 OR $uloc['city'] > 0 OR $uloc['teleport_to_world'] > 0){
		echo '<div style="position: absolute; top: 1%; right: 1%; width: 48%; font-size: 11px; text-shadow: black 1px 1px 1px; color: white; z-index: 9997;">';
		echo '<div class="fast_log">';
		echo '<div style="padding-left: 1.5%;">'.$uloc['name'].'</div>';
		echo '</div></div>';
	}
}

//Речь наставника
if($u['training_step'] > 0){
	if($u['training_step'] == 10) $say = 'Для начала перейди в инвентарь и надень Нож с Рубахой.';
	if($u['training_step'] == 11) $say = 'Так-то лучше! Теперь давай подготовим зелья к бою. Перейди в меню персонажа.';
	if($u['sex'] == 1) $a = '';
	else $a = 'а';
	if($u['training_step'] == 12) $say = 'Отлично! Таким же образом ты сможешь выставлять свои умения. Нам нужна помощь с Пчелами, это хорошее задание для новичка! Для начала посети Бэрри.';
	if($u['training_step'] == 13) $say = 'Задание принято! Тебе потребуется убить 10 Пчел для его выполнения, а теперь давай попробуем найти тех самых Пчел о которых говорится в задании.<br/>
	<center><a class="btn_mini" href="/training/next/">Давай!</a></center>';
	if($u['training_step'] == 14) $say = 'В траве водятся Пчелы, в лесу - Прыголапы, а в роще - Волки. На тропинках и в городах на тебя никто не нападёт. Давай попробуем побродить по траве, пока не найдём Пчелу.';
	if($u['training_step'] == 15) $say = 'Ловко ты её! Нужно заполнить кое-какие бумаги чтобы продолжить. За это так же предусмотрен бонус!';
	if($u['training_step'] == 16) $say = 'Так-то лучше! Теперь таким же образом найди и убей ещё Пчел, а потом подойдёшь ко мне, и сдашь задание. Удачи тебе, и невероятных приключений!<br/>
	<center><a class="btn_mini" href="/training/next/">До встречи!</a></center>';
	if($u['training_step'] == 10
		OR $u['training_step'] == 11
		OR $u['training_step'] == 12
		OR $u['training_step'] == 13
		OR $u['training_step'] == 14
		OR $u['training_step'] == 15
		OR $u['training_step'] == 16){
		echo '<div style="position: absolute; top: 1%; right: 1%; width: 98%; font-size: 11px; text-shadow: black 1px 1px 1px; color: white; z-index: 9997;">';
		echo '<div class="fast_log">';
		echo '<div style="text-align: left; padding: 4px; min-height: 36px;"><img style="float: left; margin-right: 3px;" src="/images/npcs/ernest.png" width="36px"><div style=""><font color="#E0E0E0">'.$say.'</font></div></div>';
		echo '</div></div>';
	}
}

//Чат
if($u['chat_on_location'] == 'on'){
$_GET['reply'] = fl($_GET['reply']);
$_POST['msg'] = fl($_POST['msg']);
if(isset($_REQUEST['add'])){
	if(!$ban){
		if((time() - $u['last_chat']) <= 15){
			$_SESSION['message'] = 'Сообщения можно писать не чаще одного в 15 секунд.';
			header('Location: /location/');
			exit;
		}
		if($u['last_chat_message'] == $_POST['msg']){
			$_SESSION['message'] = 'Ваше последнее сообщение имеет такое же содержимое.';
			header('Location: /location/');
			exit;
		}
		if(mb_strlen($_POST['msg']) > 2 AND mb_strlen($_POST['msg']) < 76 AND $_POST['msg'] != ''){
			if($uparty){
				$base -> query('INSERT INTO `party_chat` SET
				`party_id` = "'.$uparty['id'].'",
				`user` = "'.$u['id'].'",
				`text` = "'.$_POST['msg'].'",
				`time` = "'.time().'"');
			}else{
				$base -> query('INSERT INTO `chat` SET
				`user` = "'.$u['id'].'",
				`text` = "'.$_POST['msg'].'",
				`time` = "'.time().'"');
			}
			$base -> query('UPDATE `users` SET `last_chat` = "'.time().'", `last_chat_message` = "'.$_POST['msg'].'" WHERE `id` = "'.$u['id'].'"');
			header('Location: /location/');
			exit;
		}else{
			$_SESSION['message'] = 'Сообщение должно содержать не менее 3 символов и не более 75.';
			header('Location: /location/');
			exit;
		}
	}else{
		$_SESSION['message'] = 'Вы сможете писать в чате по истечению срока бана.';
		header('Location: /location/');
		exit;
	}
}
if($uparty) $msg_nr = $base -> query("SELECT * FROM `party_chat`") -> num_rows;
else $msg_nr = $base -> query("SELECT * FROM `chat`") -> num_rows;
if($msg_nr == 2){
	$limit = 2;
}elseif($msg_nr == 1){
	$limit = 1;
}elseif($msg_nr >= 3){
	$limit = ''.($msg_nr - 3).',3';
}
if($uparty) $sql = $base -> query("SELECT * FROM `party_chat` ORDER BY `id` ASC LIMIT $limit");
else $sql = $base -> query("SELECT * FROM `chat` ORDER BY `id` ASC LIMIT $limit");
echo '<div style="position: absolute; bottom: 1%; left: 1%; width: 65%; font-size: 10px; text-shadow: black 1px 1px 1px; color: white; text-align: left; z-index: 9997;">';
echo '<a href="/chat/"><div class="fast_log" style="padding-left: 1.5%;">';
if($msg_nr > 0){
	while($msg = $sql -> fetch_assoc()){
		$msg_user = $base -> query("SELECT * FROM `users` WHERE `id` = '".$msg['user']."' LIMIT 1") -> fetch_assoc();
		$msg['text'] = str_replace(array($u['login'].','), '<font style="text-decoration: underline;">'.$u['login'].'</font>, ', $msg['text']);
		echo '<font color="'.$msg_user['login_color'].'"><b>'.$msg_user['login'].'</b></font>: <font color="#E0E0E0">'.nl2br(smiles($msg['text'])).'</font><br/>';
	}
}else{
	if($uparty) echo 'Командный чат пока пуст...';
	else echo 'Чат пока пуст...';
}
echo '</div></a></div></div>';
}

//Карта
while($wave <= 49){

if($table == 8){
	$table = 1;
	$x = $x - 7;
	$y = $y - 1;
}

$locs = $base -> query("SELECT * FROM `locations` WHERE `world` = '".$u['world']."' AND `x` = '".$x."' AND `y` = '".$y."' LIMIT 1") -> fetch_assoc();

$loc_img = 'background-image: url(/images/locations/worlds/'.$locs['world'].'/'.$locs['image'].'.png);';

if($locs['boss'] > 0){
	$boss = $base -> query('SELECT * FROM `mobs` WHERE `id` = "'.$locs['boss'].'" LIMIT 1') -> fetch_assoc();
	if((time() - $boss['boss_time_dead']) >= $boss['boss_respawn_time']){
		$loc_img = 'background-image: url(/images/locations/worlds/'.$locs['world'].'/'.$locs['boss_image'].'.png);';
	}else{
		$loc_img = 'background-image: url(/images/locations/worlds/'.$locs['world'].'/'.$locs['image'].'.png);';
	}
}

if($locs == 0) $loc_img = 'background-image: url(/images/locations/worlds/'.$uloc['world'].'/world_out.png);';

if($table == 1) echo '<tr height="14.285%">';

echo '<td width="14.285%"  style="'.$loc_img.' background-size: cover;" align="center">';
//echo ''.$locs['x'].','.$locs['y'].'';

//echo''.$locs['id'].'';
$locs_users = $base -> query("SELECT * FROM `users` WHERE `x` = '".$locs['x']."' AND `y` = '".$locs['y']."' AND `id` != '".$u['id']."' AND `world` = '".$locs['world']."' AND `date_online` > '".(time()-600)."' LIMIT 1") -> fetch_assoc();
if(isset($locs_users)){
	echo '<div style="width: 100%; height: 100%; background: url(/images/skins/'.$locs_users['skin'].''.$locs_users['direction'].'.gif) no-repeat center;">';
	if($wave > 0 AND $wave < 8) $log_pos_y = 98;
	if($wave > 7 AND $wave < 15) $log_pos_y = 86;
	if($wave > 14 AND $wave < 22) $log_pos_y = 71.5;
	if($wave > 21 AND $wave < 29) $log_pos_y = 57;
	if($wave > 28 AND $wave < 36) $log_pos_y = 43;
	if($wave > 35 AND $wave < 43) $log_pos_y = 28.5;
	if($wave > 42 AND $wave < 50) $log_pos_y = 14.285;
	//$log_pos_y = $log_pos_y - 1;
		if($table == 7 OR $table == 1){
			if($table == 7){
				$cent = 0;
				$log_pos_x = 0;
			}
			if($table == 1){
				$cent = 2;
				$log_pos_x = 0;
			}
			$dots = 'white-space: nowrap; overflow: hidden; text-overflow: ellipsis;';
			$width = 14.285;
		}else{
			$cent = 7.142;
			$width = 28.5;
			$dots = '';
		}
		if($table == 6) $log_pos_x = 14.285;
		if($table == 5) $log_pos_x = 28.5;
		if($table == 4) $log_pos_x = 43;
		if($table == 3) $log_pos_x = 57;
		if($table == 2) $log_pos_x = 71.5;
		if($table == 1) $log_pos_x = 88;
		$log_pos_x = $log_pos_x - $cent;
		echo '<div style="position: absolute; bottom: '.$log_pos_y.'%; right: '.$log_pos_x.'%; font-size: 10px; text-shadow: black 1px 1px 1px; color: '.$locs_users['login_color'].'; width: '.$width.'%; height: 11px; text-align: center; '.$dots.'">'.$locs_users['login'].'</div>';
}

if($u['x'] == $x AND $u['y'] == $y){
	echo '<div style="width: 100%; height: 100%; background: url('.$skin.') no-repeat center;">';
	//echo '<img style="padding-top: 28px; padding-right: 28px;" src="/images/icons/vip.png" width="20px">';
	echo '<div style="position: absolute; bottom: 57%; right: 35.7%; font-size: 10px; text-shadow: black 1px 1px 1px; color: '.$u['login_color'].'; width: 28.5%; height: 11px;text-align: center;">'.$u['login'].'</div>';
}


//Передвижение
if($u['training_step'] == 0){
if($locs['x'] == $loc_up['x'] AND $locs['y'] == $loc_up['y'] AND $uloc['block_up'] != 1){
	if(!$uparty) echo '<a href="/location/move/up/"><img src="/images/move.png"></a>';
	elseif($uparty['first_user'] == $u['id']) echo '<a href="/location/move/up/"><img src="/images/move.png"></a>';
}
if($locs['x'] == $loc_down['x'] AND $locs['y'] == $loc_down['y'] AND $uloc['block_down'] != 1){
	echo '<div style="z-index: 9999;">';
	if(!$uparty) echo '<a href="/location/move/down/"><img src="/images/move.png"></a>';
	elseif($uparty['first_user'] == $u['id']) echo '<a href="/location/move/down/"><img src="/images/move.png"></a>';
	echo '</div>';
}
}
if($u['training_step'] == 0 OR $u['training_step'] == 14){
if($u['training_step'] == 14 AND $u['x'] == 5){
	//Не показывать
}else{
if($locs['x'] == $loc_left['x'] AND $locs['y'] == $loc_left['y'] AND $uloc['block_left'] != 1){
	if(!$uparty) echo '<a href="/location/move/left/"><img src="/images/move.png"></a>';
	elseif($uparty['first_user'] == $u['id']) echo '<a href="/location/move/left/"><img src="/images/move.png"></a>';
}
}
if($u['training_step'] == 14 AND $u['x'] == 8){
	//Не показывать
}else{
if($locs['x'] == $loc_right['x'] AND $locs['y'] == $loc_right['y'] AND $uloc['block_right'] != 1){
	if(!$uparty) echo '<a href="/location/move/right/"><img src="/images/move.png"></a>';
	elseif($uparty['first_user'] == $u['id']) echo '<a href="/location/move/right/"><img src="/images/move.png"></a>';
}
}
}
echo '</div></td>';

if($table == 7) echo '</tr>';

$wave ++;
$table ++;
$x ++;

}

//Ищем игроков
$loc_users_q = $base -> query('SELECT * FROM `users` WHERE `world` = "'.$u['world'].'" AND `x` = "'.$u['x'].'" AND `y` = "'.$u['y'].'" AND `id` != "'.$u['id'].'" AND `date_online` > "'.(time()-600).'" ORDER BY `level` DESC');
$loc_users_nr = $loc_users_q -> num_rows;
//Ищем предметы
$loc_items = $base -> query('SELECT * FROM `locations_items` WHERE `world` = "'.$u['world'].'" AND `x` = "'.$u['x'].'" AND `y` = "'.$u['y'].'" ORDER BY `id` DESC');
$loc_items_nr = $loc_items -> num_rows;

if($loc_users_nr > 0 OR $loc_items_nr > 0 OR $uloc['boss'] > 0 OR $uloc['city'] > 0 OR $uloc['teleport_to_world']){
	echo '<div style="position: absolute; bottom: 1%; right: 1%; width: 40%;" align="right">';
	if($loc_users_nr > 0 OR $loc_items_nr > 0){
		echo '<a href="/explore/"><div class="btn_mini"><center><img src="/images/icons/old_scroll.png"><br/><font size="1">Осмотр</font></center></div></a>';
	}
	//Босс
	if($uloc['boss'] > 0){
		$boss = $base -> query('SELECT * FROM `mobs` WHERE `id` = "'.$uloc['boss'].'" LIMIT 1') -> fetch_assoc();
		if((time() - $boss['boss_time_dead']) >= $boss['boss_respawn_time']){
		echo '<a href="/location/pvb/'.$uloc['boss'].'/"><div class="btn_mini"><center><img src="/images/icons/boss.png" width="24px"><br/><b><font size="1">'.$boss['name'].'</font></b></center></div></a>';
		}else{
			$time_to_reload = intval((60 * 60) * 12);
			$time_boss_respawn = intval(($boss['boss_time_dead'] + $boss['boss_respawn_time']) - time());
			echo '<div class="btn_mini"><center><img src="/images/icons/skeleton.png" width="24px"><br/><font size="1">'.tl($time_boss_respawn).'</font></center></div>';
		}
	}
	//Города
	elseif($uloc['city'] > 0 AND $_SERVER['PHP_SELF'] != '/city.php' AND $_SERVER['PHP_SELF'] == '/location.php') echo '<a href="/city/"><div class="btn_mini"><center><img src="/images/icons/location.png" width="24px"><br/><font size="1">Посетить</font></center></a>';
	//Пещеры, порталы и т.д.
	elseif($uloc['teleport_to_world'] > 0 AND $_SERVER['PHP_SELF'] == '/location.php') echo '<a href="/location/move/teleport"><div class="btn_mini"><center><img src="/images/icons/location.png" width="24px"><br/><font size="1">Перейти</a></center></a>';

	echo '</div>';
}

echo '</table></div></div></center>';




//ЧАТ

if($u['chat_on_location'] == 'on'){
echo '<center>';
echo '<form action="" method="POST">';
echo '<input style="padding: 5px; width: 60%;" type="text" placeholder="Введите сообщение." name="msg"/> <input style="padding: 4px;" type="submit" name="add" value="Сказать">';
echo '</form>';
echo '</center>';
}

echo '</center>';

if($u['admin'] == 3){
	echo '<div class="event">';
	echo ''.$uloc['name'].' X: '.$uloc['x'].' | Y: '.$uloc['y'].'<br/>';
	echo 'Тип локации: '.$uloc['location_type'].'<br/>';
	echo '<center><a class="btn_small" href="/panel/locations/'.$uloc['id'].'/">Изменить</a></center>';
	echo '</div>';
}

include_once ('./core/foot.php');
?>