<?php

include($phpbb_root_path . 'rpg/function_rpg.'.$phpEx);
include($phpbb_root_path . 'rpg/function_richer.'.$phpEx);

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
	exit;
}
// 不同層級的房子, 有不同的顏色
$color_array = array(	'ff00ff', 'ff00ee', 'ff00dd', 'ff00cc', 'ff00bb', 'ff00aa', 'ff0099', 'ff0088',
			'ff0077', 'ff0066', 'ff0055', 'ff0044', 'ff0033', 'ff0022', 'ff0011', 'ff0000',
			'ee0000', 'dd0000', 'cc0000', 'bb0000', 'aa0000', '990000', '880000', '770000',
			'660000', '550000', '440000', '330000', '220000', '110000', '000000'	);
// 房子層級
$house_names = array(	'建地', '睡袋', '帳篷', '洞穴', '草房', '木房', '貨櫃屋', '鐵皮屋', '磚房',
			'平房', '二樓平房', '三樓平房', '鋼筋透天', '二樓透天', '三樓透天', '四樓透天',
			'五樓透天', '別墅', '二樓別墅', '三樓別墅', '四樓別墅', '五樓別墅', '別墅大廈',
			'科技大廈', '磁浮大廈', '磁浮之城', '天空之城', '太空之城', '奈米城堡', '黑暗都市',
			'水怪大陸', '水怪惑星', '水怪星系', '<img src="images/rob7.gif">');

$returnurl = '<br /><br /><a href="rpg.php?action=richer">返回上一頁</a>';

$template->set_filenames(array(
	'menu' => 'rpg_header.tpl',
	'body' => 'rpg_richer.tpl')
);

srand((double)microtime()*1000000);

// 取得個人資料
$profiledata = get_userdata(intval($userdata['uid']));
// 第一次產生地圖時才要使用
// CreateRicherMap();
// exit;
// 0000000000
//        === Y座標
//     === X座標
//    = 目前面對的方向 0-右 1-下 2-左 3-上
//   = 目前的動作 0-All Done 1-詢問視窗
// == 可以擁有的房子數
$house_limit = floor($profiledata['rpg_richer_pos'] / 100000000);
$profiledata['rpg_richer_pos'] -= $house_limit      * 100000000;
$now_a       = floor($profiledata['rpg_richer_pos'] /  10000000);
$profiledata['rpg_richer_pos'] -= $now_a            *  10000000;
$now_d       = floor($profiledata['rpg_richer_pos'] /   1000000);
$profiledata['rpg_richer_pos'] -= $now_d            *   1000000;
$now_x       = floor($profiledata['rpg_richer_pos'] /      1000);
$now_y       = floor($profiledata['rpg_richer_pos'] %      1000);

$around_mark = array(); // 記錄繳交過路費的連鎖反應(框線用)
$road_line = array();   // 記錄走路的軌跡

$road_name = '';
$result_msg = '';

if ( ( isset($HTTP_POST_VARS['go2next']) ) &&
     (!((($profiledata['rpg_richer_pos'] % 1000000) == 5024) && ((time() - $profiledata['rpg_richer_old']) < (86400 /  8)))) &&
     (!((($profiledata['rpg_richer_pos'] % 1000000) == 2018) && ((time() - $profiledata['rpg_richer_old']) < (86400 / 24)))) &&
     (!((($profiledata['rpg_richer_pos'] % 1000000) == 9018) && ((time() - $profiledata['rpg_richer_old']) < (86400 / 12)))) )
{
	if ( CheckRefresh(9, intval($userdata['uid']), 10) )
	{
		message_die(GENERAL_MESSAGE, '為了避免系統負荷過重，請間隔 10 秒，再擲一次骰子!!' . $returnurl);
	}
	$max_dice = intval($HTTP_POST_VARS['dice_tool']);
	define("MAX_POINT", ($max_dice + 1) * 6);
	$next_point = rand($max_dice + 1, MAX_POINT);

	$count_point = $next_point;
	// 產生方圓 MAX_POINT 的陣列
	$around_map = array();
	for ($i = 0; $i < (MAX_POINT * 2 + 1); $i++)
	{
		$around_map[] = array();
		for ($j = 0; $j < (MAX_POINT * 2 + 1); $j++)
		{
			$around_map[$i][] = 0;
		}
	}
	// 將周圍的地圖填入
	$sql = "select map_x, map_y from xoops_pbb_rpg_map where map_x >= $now_x - " . MAX_POINT . " and map_y >= $now_y - " . MAX_POINT .
		" and map_x <= $now_x + " . MAX_POINT . " and map_y <= $now_y + " . MAX_POINT;
	$result = $db->sql_query($sql);
	if ($row = $db->sql_fetchrow($result))
	{
		do
		{
			$around_map[$row['map_y'] - $now_y + MAX_POINT][$row['map_x'] - $now_x + MAX_POINT] = 1;
		}
		while ( $row = $db->sql_fetchrow($result) );
	}
	$new_x = MAX_POINT;
	$new_y = MAX_POINT;
	do
	{
		// 先判斷哪幾個方向有路
		$d_count = array();
		if ($around_map[$new_y][$new_x + 1]) $d_count[] = 0; // 右
		if ($around_map[$new_y + 1][$new_x]) $d_count[] = 1; // 下
		if ($around_map[$new_y][$new_x - 1]) $d_count[] = 2; // 左
		if ($around_map[$new_y - 1][$new_x]) $d_count[] = 3; // 上
		if (count($d_count) <= 1)
		{
			$now_d = $d_count[0];
			switch ($now_d)
			{
				case 0: $new_x++;
					break;
				case 1: $new_y++;
					break;
				case 2: $new_x--;
					break;
				case 3: $new_y--;
					break;
			}
		}
		elseif (count($d_count) == 2) // 只有一條路，直走
		{
			// 右邊有路，且目前面對的方向不是往左 ( <-- 表示不是回頭路 )
			if (($around_map[$new_y][$new_x + 1]) && ($now_d != 2))
			{
				$new_x++;
				$now_d = 0;
			}
			elseif (($around_map[$new_y + 1][$new_x]) && ($now_d != 3))
			{
				$new_y++;
				$now_d = 1;
			}
			elseif (($around_map[$new_y][$new_x - 1]) && ($now_d != 0))
			{
				$new_x--;
				$now_d = 2;
			}
			elseif (($around_map[$new_y - 1][$new_x]) && ($now_d != 1))
			{
				$new_y--;
				$now_d = 3;
			}
		}
		elseif (count($d_count) > 2) // 只有兩條路以上，亂數決定
		{
			do
			{
				$next_d = $d_count[rand(0, count($d_count) - 1)];
				if ( ( ($now_d == 0) && ($next_d != 2) ) || ( ($now_d == 1) && ($next_d != 3) ) ||
					( ($now_d == 2) && ($next_d != 0) ) || ( ($now_d == 3) && ($next_d != 1) ) )
					break;
			}
			while (true);
			$now_d = $next_d;
			switch ($now_d)
			{
				case 0: $new_x++;
					break;
				case 1: $new_y++;
					break;
				case 2: $new_x--;
					break;
				case 3: $new_y--;
					break;
			}
		}
		if ( ($profiledata['user_money'] + $profiledata['user_bank']) < 100000 )
		{
			if ( ($now_y + $new_y - MAX_POINT) == 20 ) // 進入中級區，倒頭往上
			   $now_d = 3;
		}
		else if ( ($profiledata['user_money'] + $profiledata['user_bank']) < 5000000 )
		{
			if ( ($now_y + $new_y - MAX_POINT) == 29 ) // 進入地獄區，倒頭往上
			   $now_d = 3;
		}
		$road_line['x' . (($now_x + $new_x - MAX_POINT) * 1000 + ($now_y + $new_y - MAX_POINT))] = 1;
		$count_point--;
	}
	while ($count_point > 0);
	$now_x += $new_x - MAX_POINT;
	$now_y += $new_y - MAX_POINT;
	$sql = "update xoops_users set rpg_richer_old = " . time() .
		", rpg_richer_pos = $house_limit * 100000000 + $now_d * 1000000 + $now_x * 1000 + $now_y where uid = " . intval($userdata['uid']);
	$db->sql_query($sql);

	// 判斷會發生什麼事
	$sql = "select m.*, u.uname, u.rpg_richer_old, u.rpg_richer_pos from xoops_pbb_rpg_map m left join xoops_users u on
		(m.owner_id = u.uid) where m.map_x = $now_x and m.map_y = $now_y";
	$result = $db->sql_query($sql);
	if ($row = $db->sql_fetchrow($result))
	{
		$road_name = $row['road_name'];
		if ($row['house_level'] < 0) // 機會，命運
		{
			$result_msg = Chance_Destiny($row['house_level']);
		}
		else if ($row['owner_id'] == 0) // 空地
		{
			$result_msg = '目前這塊地是';
			if ( $row['house_level'] == 0 )
			{
				$result_msg .= '空地';
			}
			else
			{
				if ( $row['house_level'] > count($house_names) - 1 )
				{
					$result_msg .= $house_names[count($house_names) - 1] . '*' . ($row['house_level'] - count($house_names) + 4);
				}
				else
				{
					$result_msg .= $house_names[$row['house_level']];
				}
			}
			$result_msg .= ', 價格: ' . $row['base_rental'] . ', 係數: ' . $row['rental_factor'] . ', 請問您願意買下它嗎?? <input type="submit" name="buyit" class="liteoption" value="確定"><br />若不想買下的話，則無須理會，請繼續遊戲。';
		}
		else if ( $row['owner_id'] == $profiledata['uid'] )
		{
			$result_msg = '目前這' . ( ($row['house_level'] == 0) ? '塊地' : '棟建築物' ) . '是您的私有財產，是否花費 $' . floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] / 4) .
					' 升級成 ' .
					( ( $row['house_level'] + 1 > count($house_names) - 1 ) ? $house_names[count($house_names) - 1] . '* ' . ($row['house_level'] - count($house_names) + 3) : $house_names[$row['house_level'] + 1] ) .
					'?? <input type="submit" name="upgradeit" class="liteoption" value="升級"><br />若不想升級的話，則無須理會，請繼續遊戲。';
		}
		else if ( $row['owner_id'] != $profiledata['uid'] )
		{

			$around_owner = array();
			$around_road = array();
			$around_fee = array();

			$sql = 'select * from xoops_pbb_rpg_map where owner_id = ' . $row['owner_id'];
			$result = $db->sql_query($sql);
			if ( $row_fee = $db->sql_fetchrow($result) )
			{
				do
				{
					$around_owner['x' . ($row_fee['map_x'] * 1000 + $row_fee['map_y'])] = $row_fee['owner_id'];
					$around_road['x' . ($row_fee['map_x'] * 1000 + $row_fee['map_y'])] = $row_fee['road_name'];
					$around_fee['x' . ($row_fee['map_x'] * 1000 + $row_fee['map_y'])] =	floor($row_fee['base_rental'] * ($row_fee['house_level'] + 1) * $row_fee['rental_factor'] / 100);
				}
				while ( $row_fee = $db->sql_fetchrow($result) );
			}

			$idle_percent = floor( (time() - $row['rpg_richer_old']) / (86400 / 12) );
			if ( $idle_percent > 100 ) $idle_percent = 100;

			$idle_percent2 = floor( (time() - $row['rpg_richer_old']) / (86400 / 6) );
			if ( $idle_percent2 > 100 ) $idle_percent2 = 100;
			$idle_percent2 = 100 - $idle_percent2;

			$fee_detail = '';
			$total_fee = FeeLoop($row['map_x'], $row['map_y'], 1, 5);
			$fee_detail = substr($fee_detail, 0, strlen($fee_detail) - 2) . "
合計: $total_fee 地主閒置 " . 	floor( (time() - $row['rpg_richer_old']) / (86400 / 24) ) . " 小時，實付 $" .
				floor( $total_fee * (100 - $idle_percent) / 100 );

			if ( ( ($row['rpg_richer_pos'] % 1000000) == 5024 ) && ( (time() - $row['rpg_richer_old']) < (86400 / 8) ) )
			{
				$fee_detail .= "
但....地主正在快樂地(?)享受笙美訓練中，所以不用付費。";
				$total_fee = 0;
			}
			else if ( ( ($row['rpg_richer_pos'] % 1000000) == 2018 ) && ( (time() - $row['rpg_richer_old']) < (86400 / 24) ) )
			{
				$fee_detail .= "
但....地主正在醫院住院中，所以不用付費。";
				$total_fee = 0;
			}
			else if ( ( ($row['rpg_richer_pos'] % 1000000) == 9018 ) && ( (time() - $row['rpg_richer_old']) < (86400 / 12) ) )
			{
				$fee_detail .= "
但....地主正在監獄服刑中，所以不用付費。";
				$total_fee = 0;
			}
			else
			{
				$total_fee = floor( $total_fee * (100 - $idle_percent) / 100 );
			}
			$result_msg = "您必須付給 " . $row['uname'] . " $" . $total_fee . "元的過路費!! --&gt; <a title='$fee_detail'>滑鼠移到這裡顯示詳細資料</a>" .
					DoFeeDeduct($row['owner_id'], $row['uname'], $total_fee);
			if ( $row['map_y'] < 20 )
			{
				$total_fee = floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] * 30);
			}
			else if ( $row['map_y'] < 30 )
			{
				$total_fee = floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] * 10);
			}
			else
			{
				$total_fee = floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] * 2.5);
			}
			$total_fee = floor($total_fee * $idle_percent2 / 100);
			if ( $row['uname'] == '' )
			{
				$result_msg .= '<br />既然這塊地的地主已經掛了，那這塊地就直接給您好了....';
				$sql = 'update xoops_pbb_rpg_map set owner_id = ' . $profiledata['uid'] . ' where map_id = ' . $row['map_id'];
				$db->sql_query($sql);
			}
			else if ( $total_fee <= $profiledata['user_money'] )
			{
				$result_msg .= '<br />口胡，竟然敢收我過路費，看我用 $' . $total_fee .
						' 把你這座資產處理得一乾兩淨，順便....過戶!! <input type="submit" name="cancelit" class="liteoption" value="出手">';
			}
		}
		if (($row['house_level'] >= 0) && ($profiledata['user_money'] > 0)) // 除了機會、命運以外的乞丐
		{
			if ( $now_y < 20)
			{
				$sql = 'select count(*) as no_money from xoops_users where user_money + user_bank <= 1000 and mod(rpg_richer_pos, 1000000) = ' . ($now_x * 1000 + $now_y) . ' and uid <> ' . $profiledata['uid'];
				$per_give = rand(1, 100) * 100;
				$sql2 = 'update xoops_users set user_money = user_money + ' . $per_give . ' where user_money + user_bank <= 1000 and mod(rpg_richer_pos, 1000000) = ' . ($now_x * 1000 + $now_y) . ' and uid <> ' . $profiledata['uid'];
			}
			else if ( $now_y < 30)
			{
				$sql = 'select count(*) as no_money from xoops_users where user_money + user_bank <= 10000 and mod(rpg_richer_pos, 1000000) = ' . ($now_x * 1000 + $now_y) . ' and uid <> ' . $profiledata['uid'];
				$per_give = rand(1, 100) * 1000;
				$sql2 = 'update xoops_users set user_money = user_money + ' . $per_give . ' where user_money + user_bank <= 10000 and mod(rpg_richer_pos, 1000000) = ' . ($now_x * 1000 + $now_y) . ' and uid <> ' . $profiledata['uid'];
			}
			else
			{
				$sql = 'select count(*) as no_money from xoops_users where user_money + user_bank <= 100000 and mod(rpg_richer_pos, 1000000) = ' . ($now_x * 1000 + $now_y) . ' and uid <> ' . $profiledata['uid'];
				$per_give = rand(1, 100) * 10000;
				$sql2 = 'update xoops_users set user_money = user_money + ' . $per_give . ' where user_money + user_bank <= 100000 and mod(rpg_richer_pos, 1000000) = ' . ($now_x * 1000 + $now_y) . ' and uid <> ' . $profiledata['uid'];
			}
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			if ( $row['no_money'] > 0 )
			{
				if ($profiledata['user_money'] < $per_give * $row['no_money'])
				{
					$result_msg .= '<br />您目前所在的地方，一共有 ' . $row['no_money'] .
						' 位小乞丐，您必須施捨每位 $' . $per_give . ' ，共計 $' . $profiledata['user_money'] .
						' (身上的現金全給了....)';
					$sql = 'update xoops_users set user_money = 0 where uid = ' . $profiledata['uid'];
				}
				else
				{
					$result_msg .= '<br />您目前所在的地方，一共有 ' . $row['no_money'] .
						' 位小乞丐，您必須施捨每位 $' . $per_give . ' ，共計 $' . ($per_give * $row['no_money']);
					$sql = 'update xoops_users set user_money = user_money - ' . ($per_give * $row['no_money']) . ' where uid = ' . $profiledata['uid'];
				}
				$db->sql_query($sql);
				$db->sql_query($sql2);
			}
		}

	}
}

// 買空地
if ( isset($HTTP_POST_VARS['buyit']) )
{
	if ($now_a == 1)
	{
		$result_msg = '嘿嘿嘿....按重新整理是沒用的!!';
	}
	else
	{
		$sql = 'select count(*) as house_count from xoops_pbb_rpg_map where owner_id = ' . $profiledata['uid'];
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		if ( $row['house_count'] >= ($house_limit + 4) )
		{
			$result_msg = '您已經達到買地的上限了，若欲再買地，請先賣掉您原來擁有的土地；或者看看神願不願意賞賜您....';
		}
		else
		{
			$sql = "select * from xoops_pbb_rpg_map where map_x = $now_x and map_y = $now_y";
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			if ( $row['owner_id'] > 0 )
			{
				$result_msg = '您慢了一步，這塊地已經售出!!';
			}
			else if ( $row['base_rental'] > $profiledata['user_money'] )
			{
				$result_msg = '現金不足，無法購買!!';
			}
			else
			{
				$sql = "update xoops_users set user_money = user_money - " . $row['base_rental'] . " where uid = " . $profiledata['uid'];
				$db->sql_query($sql);
				$sql = "update xoops_pbb_rpg_map set owner_id = " . $profiledata['uid'] . " where map_x = $now_x and map_y = $now_y";
				if ( $db->sql_query($sql) )
				{
					$result_msg = '您已經成功買下這塊地!!';
				}
				$sql = "update xoops_users set rpg_richer_pos = $house_limit * 100000000 + 10000000 + $now_d * 1000000 + $now_x * 1000 + $now_y where uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
			}
		}
	}
}

// 升級
if ( isset($HTTP_POST_VARS['upgradeit']) )
{
	if ($now_a == 1)
	{
		$result_msg = '嘿嘿嘿....按重新整理是沒用的!!';
	}
	else
	{
		$sql = "select * from xoops_pbb_rpg_map where map_x = $now_x and map_y = $now_y";
		$result = $db->sql_query($sql);
		if ($row = $db->sql_fetchrow($result))
		{
			$road_name = $row['road_name'];
			if ( $row['owner_id'] != $profiledata['uid'] )
			{
				$result_msg = '這塊地不是您的喲!!無法升級!!';
			}
			else if ( $profiledata['user_money'] < floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] / 4) )
			{
				$result_msg = '現金不足，無法升級!!';
			}
			else
			{
				$sql = "update xoops_pbb_rpg_map set house_level = house_level + 1 where map_x = $now_x and map_y = $now_y";
				$db->sql_query($sql);
				$sql = "update xoops_users set user_money = user_money - " . floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] / 4) . " where uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
				$profiledata['user_money'] -= floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] / 4);
			}
		}
		$sql = "update xoops_users set rpg_richer_pos = $house_limit * 100000000 + 10000000 + $now_d * 1000000 + $now_x * 1000 + $now_y where uid = " . intval($userdata['uid']);
		$db->sql_query($sql);
	}
}

// 鏟平併購
if ( isset($HTTP_POST_VARS['cancelit']) )
{
	if ($now_a == 1)
	{
		$result_msg = '嘿嘿嘿....按重新整理是沒用的!!';
	}
	else
	{
		$sql = 'select count(*) as house_count from xoops_pbb_rpg_map where owner_id = ' . $profiledata['uid'];
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		if ( $row['house_count'] >= ($house_limit + 4) )
		{
			$result_msg = '您已經達到買地的上限了，若欲過戶，請先賣掉您原來擁有的土地，或者看看神願不願意賞賜您....';
			$target_owner_id = 0;
		}
		else
		{
			$target_owner_id = $profiledata['uid'];
		}
		$sql = "select m.*, u.rpg_richer_old from xoops_pbb_rpg_map m left join xoops_users u on (m.owner_id = u.uid) where m.map_x = $now_x and m.map_y = $now_y";
		$result = $db->sql_query($sql);
		if ($row = $db->sql_fetchrow($result))
		{
			$road_name = $row['road_name'];
			if ( $now_y < 20 )
			{
				$total_fee = floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] * 30);
			}
			else if ( $now_y < 30 )
			{
				$total_fee = floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] * 10);
			}
			else
			{
				$total_fee = floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] * 2.5);
			}
			$idle_percent2 = floor( (time() - $row['rpg_richer_old']) / (86400 / 6) );
			if ( $idle_percent2 > 100 ) $idle_percent2 = 100;
			$idle_percent2 = 100 - $idle_percent2;
			$total_fee = floor($total_fee * $idle_percent2 / 100);

			if ( $total_fee > $profiledata['user_money'] )
			{
				$result_msg = '現金不足，無法處理!!';
			}
			else
			{
				$sql = "update xoops_users set user_money = user_money + ($total_fee * 0.8) where uid = " . $row['owner_id'];
				$db->sql_query($sql);
				$sql = "update xoops_pbb_rpg_map set house_level = 0, owner_id = $target_owner_id where map_x = $now_x and map_y = $now_y";
				$db->sql_query($sql);
				$sql = "update xoops_users set user_money = user_money - $total_fee where uid = " . intval($userdata['uid']);
				$db->sql_query($sql);
				$profiledata['user_money'] -= $total_fee;
			}
		}
		$sql = "update xoops_users set rpg_richer_pos = $house_limit * 100000000 + 10000000 + $now_d * 1000000 + $now_x * 1000 + $now_y where uid = " . intval($userdata['uid']);
		$db->sql_query($sql);
	}
}

// 出售
if ( isset($HTTP_POST_VARS['go2sell']) )
{
	$map_id = intval($HTTP_POST_VARS['sell_id']);
	$sql = "select * from xoops_pbb_rpg_map where map_id = $map_id";
	$result = $db->sql_query($sql);
	if ($row = $db->sql_fetchrow($result))
	{
		$road_name = $row['road_name'];
		if ( $row['owner_id'] != $profiledata['uid'] )
		{
			$result_msg = '這塊地不是您的喲!!無法出售!!';
		}
		else
		{
			$sql = "update xoops_pbb_rpg_map set owner_id = 0 where map_id = $map_id";
			$db->sql_query($sql);
			$sql = "update xoops_users set user_money = user_money + " . floor($row['base_rental'] * 0.9) .
				" where uid = " . intval($userdata['uid']);
			$db->sql_query($sql);
			$profiledata['user_money'] += floor($row['base_rental'] * 0.9);
		}
	}
}

	// 產生地圖
	// 空架構
	$sql = 'SELECT max(map_x) max_x, max(map_y) max_y from xoops_pbb_rpg_map';
	if ( $result = $db->sql_query($sql) )
	{
		if ( $row = $db->sql_fetchrow($result) )
		{
			$map_html = '';
			$max_x = $row['max_x'];
			$max_y = $row['max_y'];
			for ($i = 0; $i <= $max_y; $i++)
			{
				$map_html .= '<tr>';
				for ($j = 0; $j <= $max_x; $j++)
				{
					$map_html .= "<td align=\"center\"" . (($i == 0) ? " width=\"" . floor(100 / ($max_x + 1)) . "%\"" : "") . " MAP($j,$i)</td>";
				}
				$map_html .= '</tr>';
			}
		}
	}

$whoincell = array();
$other_info = '';
if ( isset($HTTP_POST_VARS['exec_func']) )
{
	$func_id = intval($HTTP_POST_VARS['function_id']);
	switch ($func_id)
	{
		case 1: // 查看所有人的位置
			$sql = 'select uname, rpg_richer_pos from xoops_users where rpg_richer_pos > 0';
			if ( $result = $db->sql_query($sql) )
			{
				if ( $row = $db->sql_fetchrow($result) )
				{
					do
					{
						$whoincell['x' . ($row['rpg_richer_pos'] % 1000000)] .= '<br />' . $row['uname'];
					}
					while ( $row = $db->sql_fetchrow($result) );
				}
			}
			break;
		case 2: // 取得路費收支情形
			$sql = 'SELECT * FROM xoops_pbb_rpg_event WHERE event_class = 4 AND uid = ' . intval($userdata['uid']) . ' ORDER BY event_time DESC LIMIT 0, 50';
			$result = $db->sql_query($sql);
			if ( $row = $db->sql_fetchrow($result) )
			{
				$i = 1;
				$other_info = '<span class="genmed">路費收支表(只保留三天):</span><table class="forumline" width="100%" cellspacing="1" cellpadding="3" border="0">
						<tr><td align="center" class="catLeft"><b><span class="gen">#</span></b></td>
						<td align="center" class="catLeft"><b><span class="gen">日期</span></b></td>
						<td align="center" class="catLeft"><b><span class="gen">金額</span></b></td>
						<td align="center" class="catLeft"><b><span class="gen">備註</span></b></td></tr>';
				do
				{
					$other_info .= '<tr><td class="row3" align="center"><span class="gensmall">' . $i .
							'</span></td><td align="center"><span class="gensmall">' .
							($row['event_time'] ? create_date($userdata['user_dateformat'], $row['event_time'], $board_config['board_timezone']) : $lang['Never_last_logon']) .
							'</span></td><td align="right"><span class="gensmall">' . number_format($row['event_money']) .
							'</span></td><td><span class="gensmall">' . $row['event_note'] . '</span></td></tr>';
					$i++;
				}
				while ( $row = $db->sql_fetchrow($result) );
				$other_info .= '</table>';
				$sql = 'delete from xoops_pbb_rpg_event where uid = ' . intval($userdata['uid']) . ' and event_time < ' . (time() - 86400 * 3);
				$db->sql_query($sql);
			}
			else
			{
				$other_info = '<span class="genmed">目前尚無資料</span>';
			}
			break;
	}
}

	// 是否有房產
	$AssetCount = 0;
	$AssetCombo = '';

	// 有空地、房屋的
	$sql = 'select m.*, u.uname from xoops_pbb_rpg_map m left join xoops_users u on m.owner_id = u.uid';
	if ( $result = $db->sql_query($sql) )
	{
		if ( $row = $db->sql_fetchrow($result) )
		{
			do
			{
				if ($road_line['x' . ( $row['map_x'] * 1000 + $row['map_y'])] )
					$road_line['x' . ($row['map_x'] * 1000 + $row['map_y'])] = $row['road_name'];
				if ( $row['owner_id'] == $profiledata['uid'] )
				{
					$AssetCount++;
					$AssetCombo .= '<option value="' . $row['map_id'] . '"' . ($AssetCount == 1 ? ' selected' : '') . '>' . $row['road_name'] . '</option>';
				}

				if ( $row['house_level'] < 0 )
				{
					$cell_value = '<span class="gen"><b>' . $row['road_name'] .
					     '</b></span><font color=white size=1>' . $whoincell['x' . ($row['map_x'] * 1000 + $row['map_y'])] . '</font>';
				}
				else
				{
					if ($row['owner_id'] > 0)
					{
						if ( ($row['map_x'] == $now_x) && ($row['map_y'] == $now_y) )
						{
							$cell_value = '<span class="gensmall"><font color="#ffffff"><b>' . substr($row['uname'], 0, 8) . '<br />';
						}
						else
						{
							$cell_value = '<span class="gensmall">' . ($row['house_level'] > 20 ? '<font color="#ffffff">' : '') . substr($row['uname'], 0, 8) . '<br />';
						}
						if ( $row['house_level'] > count($house_names) - 1 )
						{
							$cell_value .= $house_names[count($house_names) - 1] . '*' . ($row['house_level'] - count($house_names) + 2);
						}
						else
						{
							$cell_value .= $house_names[$row['house_level']];
						}
						if ( ($row['map_x'] == $now_x) && ($row['map_y'] == $now_y) )
						{
							$cell_value .= '</b></font></span>';
						}
						else
						{
							$cell_value .= ($row['house_level'] > 20 ? '</font>' : '') . '</span>';
						}
						$cell_value .= '<font color=white size=1>' . $whoincell['x' . ($row['map_x'] * 1000 + $row['map_y'])] . '</font>';
					}
					else
					{
						$cell_value = '&nbsp;<font color=white size=1>' . $whoincell['x' . ($row['map_x'] * 1000 + $row['map_y'])] . '</font>';
					}
				}
				$cell_hint = $row['road_name'] . '　價值: ' . ( ($row['house_level'] == 0) ? $row['base_rental'] : $row['base_rental'] * $row['rental_factor'] * ($row['house_level'] + 1) ) .
						'　係數: ' . $row['rental_factor'] . 
						'　路費: ' . floor( $row['base_rental'] * $row['rental_factor'] * ($row['house_level'] + 1) / 100 );
				if ( ($row['map_x'] == $now_x) && ($row['map_y'] == $now_y) )
				{
					$map_html = str_replace('MAP(' . $row['map_x'] . ',' . $row['map_y'] . ')', 'bgcolor="blue" Title="' . $cell_hint .
							'"' . ( $around_mark['x' . ($row['map_x'] * 1000 + $row['map_y'])] ? ' style="border=\'2 solid blue\'"' : '') . '>' . $cell_value, $map_html);
				}
				else
				{
					if ($row['owner_id'] > 0)
					{
						if ( $row['house_level'] > count($color_array) - 1 )
						{
							$house_color = $color_array[count($color_array) - 1];
						}
						else
						{
							$house_color = $color_array[$row['house_level']];
						}
						$map_html = str_replace('MAP(' . $row['map_x'] . ',' . $row['map_y'] . ')', 'bgcolor="' . $house_color . '" Title="' . $cell_hint .
							'"' . ( $around_mark['x' . ($row['map_x'] * 1000 + $row['map_y'])] ? ' style="border=\'2 solid blue\'"' : '') . '>' . $cell_value, $map_html);
					}
					else
					{
						$map_html = str_replace('MAP(' . $row['map_x'] . ',' . $row['map_y'] . ')', 'bgcolor="#CCCCCC" Title="' . $cell_hint .
							'"' . ( $around_mark['x' . ($row['map_x'] * 1000 + $row['map_y'])] ? ' style="border=\'2 solid blue\'"' : '') . '>' . $cell_value, $map_html);
					}
				}
			}
			while ( $row = $db->sql_fetchrow($result) );
		}
	}
	$map_html = str_replace("MAP($now_x,$now_y)", 'bgcolor="blue">' . $cell_value, $map_html);

	// 空格子
	for ($i = 0; $i <= $max_y; $i++)
	{
		for ($j = 0; $j <= $max_x; $j++)
		{
			$map_html = str_replace('MAP(' . $j . ',' . $i . ')', '>&nbsp;', $map_html);
		}
	}

if ( $roadname == '' )
{
	$sql = "select * from xoops_pbb_rpg_map where map_x = $now_x and map_y = $now_y";
	$result = $db->sql_query($sql);
	if ($row = $db->sql_fetchrow($result)) $road_name = $row['road_name'];
}

if ( ($profiledata['user_money'] <= 0) && ($profiledata['user_bank'] <= 0) )
{
	$button_list = '<input type="button" name="nonext" class="liteoption" value="乞丐只能乖乖待在原地喲!!">';
}
else if ( ($now_x == 5) && ($now_y == 24) && ( (time() - $profiledata['rpg_richer_old']) < (86400 / 8) ) )
{
	$button_list = '<input type="button" name="nonext" class="liteoption" value="您還要在笙美島繼續享受 ' .
	floor( ($profiledata['rpg_richer_old'] + 86400 / 8 - time()) / 60) . ' 分鐘!!">';
}
else if ( ($now_x == 2) && ($now_y == 18) && ( (time() - $profiledata['rpg_richer_old']) < (86400 / 24) ) )
{
	$button_list = '<input type="button" name="nonext" class="liteoption" value="您還要在醫院繼續住院 ' .
	floor( ($profiledata['rpg_richer_old'] + 86400 / 24 - time()) / 60) . ' 分鐘!!">';
}
else if ( ($now_x == 9) && ($now_y == 18) && ( (time() - $profiledata['rpg_richer_old']) < (86400 / 12) ) )
{
	$button_list = '<input type="button" name="nonext" class="liteoption" value="您還要在監獄繼續服刑 ' .
	floor( ($profiledata['rpg_richer_old'] + 86400 / 12 - time()) / 60) . ' 分鐘!!">';
}
else
{
	$dice_tool = '';
	// 是否有殺手腳踏車
	$sql = 'select * from xoops_pbb_rpg_owns where item_id = 107 and uid = ' . $profiledata['uid'];
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
		$dice_tool = '<option value="1"' . ($max_dice == 1 ? ' selected' : '') . '>殺手腳踏車</option>';
	// 是否有阿丁鳳凰號
	$sql = 'select * from xoops_pbb_rpg_owns where item_id = 108 and uid = ' . $profiledata['uid'];
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
		$dice_tool .= '<option value="2"' . ($max_dice == 2 ? ' selected' : '') . '>阿丁鳳凰號</option>';

	$button_list = '<input type="submit" name="go2next" class="liteoption" value="擲骰子">';
	if ($dice_tool  != '')
		$button_list = '<select name="dice_tool"><option value="0"' . ($max_dice == 0 ? ' selected' : '') . '>步行</option>' . $dice_tool . '</select>&nbsp;' . $button_list;
	if ($AssetCombo != '')
		$button_list .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="sell_id">' . $AssetCombo . '</select>&nbsp;<input type="submit" name="go2sell" class="liteoption" value="出售">';
}

$play_info = '<b>現　　金:&nbsp;</b>' . $profiledata['user_money'] . '<br /><b>存　　款:&nbsp;</b>' . $profiledata['user_bank'] . '<br /><b>房　　產:&nbsp;</b>' . $AssetCount . ' / ' . ($house_limit + 4);

if ( count($road_line) > 0 )
{
	$road_name .= '(<a title="';
	for ( reset($road_line); $key = key($road_line); next($road_line) )
	{
		$road_name .= $road_line[$key] . '
';
	}
	$road_name .= '">行進路線</a>)';
}

// 產生資料頁面
$page_title = "水怪大陸 RPG 首頁";
include($phpbb_root_path . 'includes/page_header.'.$phpEx);

$template->assign_vars(array(
	'USERNAME' => $profiledata['uname'],
	'AVATAR_IMG' => GetAvatar($profiledata),
	'RICHER_MAP' => $map_html,
	'DICE_RESULT' => ( isset($HTTP_POST_VARS['go2next']) ? $next_point . ' 點' : '尚未擲出' ),
	'CURRENT_ROAD' => $road_name,
	'RESULT_MSG' => $result_msg,
	'BUTTON_CONTROL' => $button_list,
	'PLAYER_INFO' => $play_info,
	'OTHER_INFORMATION' => $other_info,
	'L_NOWMENU' => '大大房屋仲介') );

$template->pparse('menu');
$template->pparse('body');

include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

?>