<?php

function CreateRicherMap()
{
	global $db;

	// 產生地圖用的，只有第一次才需使用
	$mapdata = array(	array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
				array( 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1),
				array( 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1),
				array( 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1),
				array( 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1),
				array( 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1),
				array( 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1),
				array( 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1),
				array( 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1),
				array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
				array( 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1),
				array( 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1),
				array( 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1),
				array( 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1),
				array( 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1),
				array( 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1),
				array( 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1),
				array( 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1),
				array( 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1),
				array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
				// 開始進入中級區
				array( 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0),
				array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
				array( 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1),
				array( 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1),
				array( 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0),
				array( 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0),
				array( 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0),
				array( 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0),
				array( 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0),
				array( 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0),
				// 開始進入地獄區
				array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
				array( 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1),
				array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
				 );
	// 坐標說明
	$roaddata = array(	array( '機會', '黑北路1號', '黑北路2號', '黑北路3號', '黑北路4號', '黑北路5號', '黑北路6號', '黑北路7號', '黑北路8號', '黑北路9號', '黑北路最後一號', '黑暗'),
				array( '水西路1號', '', '', '蘿莉小路1號', '', '', '', '', '正太小徑1號', '', '', '怪東路1號'),
				array( '水西路2號', '', '', '蘿莉小路1號', '', '', '', '', '正太小徑2號', '', '', '怪東路2號'),
				array( '水西路3號', '', '', '蘿莉小路1號', '', '', '', '', '正太小徑3號', '', '', '怪東路3號'),
				array( '水西路4號', '', '', '笙美大街1號', '笙美大街2號', '笙美大街3號', '笙美大街4號', '笙美大街5號', '笙美大街6號', '', '', '怪東路4號'),
				array( '水西路5號', '', '', '', '黑暗路1號', '', '', '水怪街1號', '', '', '', '怪東路5號'),
				array( '水西路6號', '', '', '', '黑暗路2號', '', '', '水怪街2號', '', '', '', '怪東路6號'),
				array( '水西路7號', '', '', '', '黑暗路3號', '', '', '水怪街3號', '', '', '', '怪東路7號'),
				array( '水西路8號', '', '', '', '黑暗路4號', '', '', '水怪街4號', '', '', '', '怪東路8號'),
				array( '水西路9號', '殺手大道1號', '殺手大道2號', '殺手大道3號', '殺手大道4號', '殺手大道5號', '九龍街79號', '殺手大道6號', '殺手大道7號', '殺手大道8號', '殺手大道9號', '怪東路9號'),
				array( '水西路10號', '', '遊俠街1號', '', '', '', '', '', '', '丐幫下水道1號', '', '怪東路10號'),
				array( '水西路11號', '', '遊俠街2號', '', '', '', '', '', '', '丐幫下水道2號', '', '怪東路11號'),
				array( '水西路12號', '', '遊俠街3號', '', '', '', '', '', '', '丐幫下水道3號', '', '怪東路12號'),
				array( '水西路13號', '', '遊俠街4號', '', '', '', '', '', '', '丐幫下水道4號', '', '怪東路13號'),
				array( '水西路14號', '', '魔王大道1號', '魔王大道2號', '魔王大道3號', '魔王大道4號', '魔王大道5號', '魔王大道6號', '魔王大道7號', '魔王大道8號', '', '怪東路14號'),
				array( '水西路15號', '', '', '', '蘿莉控國道1號', '', '', '歐巴洞穴1號', '', '', '', '怪東路15號'),
				array( '水西路16號', '', '', '', '蘿莉控國道2號', '', '', '歐巴洞穴2號', '', '', '', '怪東路16號'),
				array( '水西路17號', '', '', '', '蘿莉控國道3號', '', '', '歐巴洞穴3號', '', '', '', '怪東路17號'),
				array( '水西路18號', '', '醫院', '', '蘿莉控國道4號', '', '', '歐巴洞穴4號', '', '監獄', '', '怪東路18號'),
				array( '神蹟', '暗南路1號', '暗南路2號', '暗南路3號', '暗南路4號', '暗南路5號', '暗南路6號', '暗南路7號', '暗南路8號', '暗南路9號', '暗南路最後一號', '命運'),
				// 開始進入中級區
				array( '', 'Ayu狹谷', '', '馬毛關', '', '', '', '', '邪翼門', '', 'Miyu隘口', ''),
				array( '灌水大(王)道1號', '灌水大(王)道2號', '灌水大(王)道3號', '灌水大(王)道4號', '灌水大(王)道5號', '灌水大(王)道6號', '灌水大(王)道7號', '灌水大(王)道8號', '灌水大(王)道9號', '灌水大(王)道10號', '灌水大(王)道11號', '灌水大(王)道12號'),
				array( '通吃小巷1號', '', '通吃小巷5號', '', '大馬(毛)路1號', '', '滾蛋密道1號', '', '色色小徑1號', '', '', '色色小徑6號'),
				array( '通吃小巷2號', '通吃小巷3號', '通吃小巷4號', '', '大馬(毛)路2號', '', '滾蛋密道2號', '', '色色小徑2號', '色色小徑3號', '色色小徑4號', '色色小徑5號'),
				array( '', '搶匪棧道1號', '', '', '大馬(毛)路3號', '笙美島', '滾蛋密道3號', '', '', '', '滾蛋密道9號', ''),
				array( '', '搶匪棧道2號', '搶匪棧道3號', '搶匪棧道4號', '搶匪棧道5號', '', '滾蛋密道4號', '滾蛋密道5號', '滾蛋密道6號', '滾蛋密道7號', '滾蛋密道8號', ''),
				array( '', '地獄隧道1號', '', '倒霉路1號', '', '', '', '', '便便街1號', '', '奈何通道1號', ''),
				array( '', '地獄隧道2號', '', '倒霉路2號', '草人吊橋1號', '草人吊橋2號', '草人吊橋3號', '草人吊橋4號', '便便街2號', '', '奈何通道2號', ''),
				array( '', '地獄隧道3號', '', '倒霉路3號', '', '', '', '', '便便街3號', '', '奈何通道3號', ''),
				array( '', '地獄隧道4號', '', '倒霉路4號', '', '', '', '', '便便街4號', '', '奈何通道4號', ''),
				// 開始進入地獄區
				array( '肥羊末路1號', '肥羊末路2號', '肥羊末路3號', '肥羊末路4號', '肥羊末路5號', '肥羊末路6號', '肥羊末路7號', '肥羊末路8號', '肥羊末路9號', '肥羊末路10號', '肥羊末路11號', '肥羊末路12號'),
				array( 'BgBone的家', '', 'pony的喵喵樂園', '', 'Gavin_hk的巢', '', '', 'YuY的窩', '', 'Hyrami小屋', '', 'proxcel543山寨'),
				array( '神賞', '不歸路1號', '不歸路2號', '不歸路3號', '國慶路178號', '不歸路5號', '不歸路6號', '不歸路7號', '不歸路8號', '不歸路9號', '不歸路10號', '魔罰'));

	// 空地的價格 (單位: 萬)
	$moneydata = array(	array(  -1,   1,   1,   4,   1,   1,   1,   1,   4,   1,   1,  -1),
				array(   1,   0,   0,   2,   0,   0,   0,   0,   2,   0,   0,   1),
				array(   1,   0,   0,   2,   0,   0,   0,   0,   2,   0,   0,   1),
				array(   1,   0,   0,   2,   0,   0,   0,   0,   2,   0,   0,   1),
				array(   1,   0,   0,   5,   8,   5,   5,   8,   5,   0,   0,   1),
				array(   2,   0,   0,   0,   3,   0,   0,   3,   0,   0,   0,   2),
				array(   2,   0,   0,   0,   3,   0,   0,   3,   0,   0,   0,   2),
				array(   2,   0,   0,   0,   4,   0,   0,   4,   0,   0,   0,   2),
				array(   2,   0,   0,   0,   5,   0,   0,   5,   0,   0,   0,   2),
				array(   4,   2,   5,   4,  12,   5,  16,  12,   4,   5,   2,   4),
				array(   2,   0,   3,   0,   0,   0,   0,   0,   0,   3,   0,   2),
				array(   2,   0,   3,   0,   0,   0,   0,   0,   0,   3,   0,   2),
				array(   2,   0,   3,   0,   0,   0,   0,   0,   0,   3,   0,   2),
				array(   2,   0,   3,   0,   0,   0,   0,   0,   0,   3,   0,   2),
				array(   1,   0,   3,   3,   8,  10,   5,   8,   3,   3,   0,   1),
				array(   1,   0,   0,   0,   3,   0,   0,   3,   0,   0,   0,   1),
				array(   1,   0,   0,   0,   3,   0,   0,   3,   0,   0,   0,   1),
				array(   1,   0,   0,   0,   2,   0,   0,   2,   0,   0,   0,   1),
				array(   1,   0,  -1,   0,   2,   0,   0,   2,   0,  -1,   0,   1),
				array(  -1,   1,   1,   2,   4,   2,   2,   4,   2,   1,   1,  -1),
				// 開始進入中級區
				array(   0,  10,   0,  12,   0,   0,   0,   0,  12,   0,  10,   0),
				array(  15,  40,  20,  24,  30,  40,  50,  30,  24,  20,  40,  15),
				array(  20,   0,  25,   0,  35,   0,  40,   0,  30,   0,   0,  25),
				array(  15,  35,  30,   0,  40,   0,  30,   0,  40,  36,  36,  30),
				array(   0,  30,   0,   0,  45,  -1,  40,   0,   0,   0,  32,   0),
				array(   0,  36,  40,  48,  50,   0,  45,  45,  50,  30,  30,   0),
				array(   0,  40,   0,  40,   0,   0,   0,   0,  30,   0,  33,   0),
				array(   0,  60,   0,  60,  70,  80,  80,  70,  60,   0,  66,   0),
				array(   0,  80,   0,  80,   0,   0,   0,   0,  90,   0,  99,   0),
				array(   0, 100,   0, 100,   0,   0,   0,   0, 120,   0, 119,   0),
				// 開始進入地獄區
				array( 800, 600, 400, 200, 400, 500, 500, 400, 200, 400, 600, 800),
				array(4800,   0,4000,   0,3600,   0,   0,3200,   0,2800,   0,2400),
				array(  -1,1600,1200,4000, 800, 400, 200, 400, 800,1200,1600,  -1)
				 );

	// 欲加蓋時的費用係(乘)數
	$plusdata = array(	array(   0, 1.5, 2.4, 4.8, 3.6, 3.2, 3.2, 3.6, 4.8, 2.4, 1.5,   0),
				array( 1.5,   0,   0, 3.5,   0,   0,   0,   0, 3.5,   0,   0, 1.5),
				array( 1.6,   0,   0, 4.0,   0,   0,   0,   0, 4.0,   0,   0, 1.6),
				array( 1.7,   0,   0, 4.5,   0,   0,   0,   0, 4.5,   0,   0, 1.7),
				array( 1.8,   0,   0, 5.4, 6.4, 6.2, 6.2, 6.4, 5.4,   0,   0, 1.8),
				array( 2.0,   0,   0,   0, 3.6,   0,   0, 3.6,   0,   0,   0, 2.0),
				array( 2.4,   0,   0,   0, 4.2,   0,   0, 4.2,   0,   0,   0, 2.4),
				array( 2.8,   0,   0,   0, 4.8,   0,   0, 4.8,   0,   0,   0, 2.8),
				array( 3.2,   0,   0,   0, 5.2,   0,   0, 5.2,   0,   0,   0, 3.2),
				array( 4.8, 4.0, 5.4, 4.8, 6.0, 6.4,  10, 6.0, 4.8, 5.4, 4.0, 4.8),
				array( 3.2,   0, 3.8,   0,   0,   0,   0,   0,   0, 3.8,   0, 3.2),
				array( 2.8,   0, 4.2,   0,   0,   0,   0,   0,   0, 4.2,   0, 2.8),
				array( 2.4,   0, 4.5,   0,   0,   0,   0,   0,   0, 4.5,   0, 2.4),
				array( 2.0,   0, 4.8,   0,   0,   0,   0,   0,   0, 4.8,   0, 2.0),
				array( 1.8,   0, 5.2, 4.8, 5.4, 8.0, 4.8, 5.4, 4.8, 5.2,   0, 1.8),
				array( 1.7,   0,   0,   0, 4.5,   0,   0, 4.5,   0,   0,   0, 1.7),
				array( 1.6,   0,   0,   0, 3.2,   0,   0, 3.2,   0,   0,   0, 1.6),
				array( 1.5,   0,   0,   0, 2.8,   0,   0, 2.8,   0,   0,   0, 1.5),
				array( 1.5,   0,   0,   0, 2.4,   0,   0, 2.4,   0,   0,   0, 1.5),
				array(   0, 1.5, 1.8, 2.5, 5.0, 3.6, 3.6, 5.0, 2.5, 1.8, 1.5,   0),
				// 開始進入中級區
				array(   0,   2,   0, 2.1,   0,   0,   0,   0, 2.1,   0,   2,   0),
				array( 3.2,   3,   4, 3.6, 4.8, 3.2, 4.8, 3.2, 3.6, 3.2,   3,   3),
				array( 3.6,   0, 3.6,   0,   4,   0, 3.6,   0, 3.8,   0,   0, 3.2),
				array( 3.2, 3.8, 3.2,   0, 4.2,   0,   4,   0, 4.2, 4.4, 5.4, 3.6),
				array(   0, 4.2,   0,   0, 4.6,   0, 4.2,   0,   0,   0, 4.8,   0),
				array(   0, 4.8, 5.2, 6.4,   5,   0, 4.8,   5, 5.6,   4, 4.4,   0),
				array(   0, 5.4,   0,   6,   0,   0,   0,   0, 6.4,   0, 5.4,   0),
				array(   0, 6.6,   0, 8.2, 7.2, 6.4, 7.2, 7.8, 8.2,   0, 6.6,   0),
				array(   0, 7.2,   0,   7,   0,   0,   0,   0,   7,   0, 7.2,   0),
				array(   0,   8,   0,   6,   0,   0,   0,   0,   6,   0,   8,   0),
				// 開始進入地獄區
				array( 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.6, 2.5, 2.4, 2.3, 2.2, 2.1),
				array(   2,   0,   2,   0,   2,   0,   0,   2,   0,   2,   0,   2),
				array(   0, 1.9, 1.8, 1.7, 1.6, 1.5, 1.5, 1.6, 1.7, 1.8, 1.9,   0)
				 );

	$sql = 'delete from xoops_pbb_rpg_map';
	$db->sql_query($sql);
	for ($i = 0; $i < sizeof($mapdata); $i++)
	{
		for ($j = 0; $j < sizeof($mapdata[0]); $j++)
		{
			if ($mapdata[$i][$j])
			{
				$sql = "insert into xoops_pbb_rpg_map (map_x, map_y, road_name, base_rental, rental_factor) values ($j, $i, '" .
					$roaddata[$i][$j] . "', " . $moneydata[$i][$j] . " * 10000," . $plusdata[$i][$j] . ")";
				$db->sql_query($sql);
			}
		}
	}
	$sql = "update xoops_pbb_rpg_map set house_level = -1, base_rental = -1 where road_name = '機會'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -2, base_rental = -1 where road_name = '命運'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -3, base_rental = -1 where road_name = '神蹟'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -4, base_rental = -1 where road_name = '黑暗'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -5, base_rental = -1 where road_name = '神賞'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -6, base_rental = -1 where road_name = '魔罰'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -7, base_rental = -1 where road_name = '醫院'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -8, base_rental = -1 where road_name = '監獄'";
	$db->sql_query($sql);
	$sql = "update xoops_pbb_rpg_map set house_level = -9, base_rental = -1 where road_name = '笙美島'";
	$db->sql_query($sql);
	echo "Generate OK!!";
}

function GetRpgItem($ItemCount)
{
	global $db, $profiledata;

	$sql = 'select count(*) as item_count from xoops_pbb_rpg_items';
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$max_rand = $row['item_count'];
	switch ($ItemCount)
	{
		case 1:
			$sql = 'select * from xoops_pbb_rpg_items where item_id not in (104, 105)';
			$max_rand -= 2;
			break;
		case 2:
			$sql = 'select * from xoops_pbb_rpg_items where item_id <> 105';
			$max_rand--;
			break;
		case 3:
			$sql = 'select * from xoops_pbb_rpg_items';
			break;
	}
	$result = $db->sql_query($sql);

	srand((double)microtime()*1000000);
	$loop_count = rand(1, $max_rand);

	for ($i = 1; $i <= $loop_count; $i++)
	{
		$row = $db->sql_fetchrow($result);
	}
	$sql = 'insert into xoops_pbb_rpg_owns (uid, item_id) values (' . $profiledata['uid'] . ', ' . $row['item_id'] . ')';
	$db->sql_query($sql);

	return '獲得道具: ' . $row['item_name'];
}

function DropRpgItem()
{
	global $db, $profiledata;

	$sql = 'select count(*) as item_count from xoops_pbb_rpg_owns where uid = ' . $profiledata['uid'];
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$max_rand = $row['item_count'];
	$sql = 'select o.*, i.item_name from xoops_pbb_rpg_owns o, xoops_pbb_rpg_items i where o.item_id = i.item_id and o.uid = ' . $profiledata['uid'];
	$result = $db->sql_query($sql);

	srand((double)microtime()*1000000);
	$loop_count = rand(1, $max_rand);

	for ($i = 1; $i <= $loop_count; $i++)
	{
		$row = $db->sql_fetchrow($result);
	}
	$sql = 'delete from xoops_pbb_rpg_owns where own_id = ' . $row['own_id'];
	$db->sql_query($sql);

	return '失去道具: ' . $row['item_name'];
}

function SellRpgItem()
{
	global $db, $profiledata;

	$sql = 'select count(*) as item_count from xoops_pbb_rpg_owns where uid = ' . $profiledata['uid'];
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$max_rand = $row['item_count'];
	$sql = 'select o.*, i.item_name, i.item_price from xoops_pbb_rpg_owns o, xoops_pbb_rpg_items i where o.item_id = i.item_id and o.uid = ' . $profiledata['uid'];
	$result = $db->sql_query($sql);

	srand((double)microtime()*1000000);
	$loop_count = rand(1, $max_rand);

	for ($i = 1; $i <= $loop_count; $i++)
	{
		$row = $db->sql_fetchrow($result);
	}
	$sql = 'delete from xoops_pbb_rpg_owns where own_id = ' . $row['own_id'];
	$db->sql_query($sql);
	$sql = 'update xoops_users set user_money = user_money + ' . floor($row['item_price'] * 0.6) . ' where uid = ' . $profiledata['uid'];
	$db->sql_query($sql);

	return '強制變賣道具: ' . $row['item_name'] . ' 得款 $' . floor($row['item_price'] * 0.6);
}

function GetSpecialItem($uid, $iid, $inote)
{
	global $db;

	$sql = "insert into xoops_pbb_rpg_owns (uid, item_id) values ($uid, $iid)";
	$db->sql_query($sql);

	return '獲得 ' . $inote ;
}

function ModifyHouseLevel($uid, $mlv)
{
	global $db;

	if ($mlv > 0)
	{
		$ret_msg = '房子等級加 ' . $mlv;
	}
	else
	{
		$ret_msg = '房子等級減 ' . ($mlv * -1);
	}
	$sql = 'select count(*) as house_count from xoops_pbb_rpg_map where owner_id = ' . $uid;
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$max_rand = $row['house_count'];
	if ($max_rand > 0)
	{
		$sql = 'select * from xoops_pbb_rpg_map where owner_id = ' . $uid;
		$result = $db->sql_query($sql);

		srand((double)microtime()*1000000);
		$loop_count = rand(1, $max_rand);

		for ($i = 1; $i <= $loop_count; $i++)
		{
			$row = $db->sql_fetchrow($result);
		}
		if ( ($row['house_level'] + $mlv) < 0 )
		{
			$sql = "update xoops_pbb_rpg_map set house_level = 0 where map_id = " . $row['map_id'];
			$db->sql_query($sql);
		}
		else
		{
			$sql = "update xoops_pbb_rpg_map set house_level = house_level + $mlv where map_id = " . $row['map_id'];
			$db->sql_query($sql);
		}
		$ret_msg = $row['road_name'] . ' ' . $ret_msg;
	}
	else
	{
		$ret_msg .= ' ....可是您沒有房子耶!!';
	}
	return $ret_msg;
}

function Chance_Destiny($type)
{
	global $db, $profiledata, $house_limit;

	srand((double)microtime()*1000000);
	$msg = '';
	$rid = rand(1, 100);
	switch ($type)
	{
		case -1: // 機會
			switch ($rid)
			{
				case $rid <= 12:
					$msg = '機會: 草人數減 ' . floor($rid / 4 + 1);
					if ( $profiledata['user_robfail'] >= floor($rid / 4 + 1) )
					{
						$sql = 'update xoops_users set user_robfail = user_robfail - ' . floor($rid / 4 + 1) . ' where uid = ' . $profiledata['uid'];
					}
					else
					{
						$sql = 'update xoops_users set user_robfail = 0 where uid = ' . $profiledata['uid'];
					}
					$db->sql_query($sql);
					break;
				case $rid <= 15:
					$msg = '機會: ' . GetRpgItem(1);
					break;
				case $rid <= 17:
					$msg = '機會: 強制當上道具店的工讀生';
					$sql = "update xoops_pbb_rpg_jobs set worker_id = " . $profiledata['uid'] . ", worker_name = '" . $profiledata['uname'] . "' where job_id = 3";
					$db->sql_query($sql);
					break;
				case 18:
					$msg = '機會: ' . GetSpecialItem($profiledata['uid'], 101, '水怪的便便 一坨');
					break;
				case 19:
					$msg = '機會: ' . ModifyHouseLevel($profiledata['uid'], 1);
					break;
				default:
					$msg = '機會 - 銘謝惠顧';
			}
			break;
		case -2: // 命運
			switch ($rid)
			{
				case $rid <= 12:
					$msg = '命運: 草人數加 ' . floor($rid / 4 + 1);
					$sql = 'update xoops_users set user_robfail = user_robfail + ' . floor($rid / 4 + 1) . ' where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case $rid <= 15:
					$msg = '命運: ' . DropRpgItem();
					break;
				case $rid <= 18:
					$msg = '命運: ' . SellRpgItem();
					break;
				case 19:
					$msg = '命運: 到笙美島接受笙美訓練';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 5024 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case 20:
					$msg = '命運: 被笙美過頭以致住院觀查';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 2018 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case 21:
					$msg = '命運: 笙美別人過頭到監獄服刑管束';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 9018 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case 22:
					$msg = '命運: ' . ModifyHouseLevel($profiledata['uid'], -1);
					break;
				default:
					$msg = '命運 - 銘謝惠顧';
			}
			break;
		case -3: // 神蹟
			switch ($rid)
			{
				case $rid <= 12:
					$msg = '神蹟: 草人數減 ' . floor($rid / 4 + 1) * 2;
					if ( $profiledata['user_robfail'] >= floor($rid / 4 + 1) * 2 )
					{
						$sql = 'update xoops_users set user_robfail = user_robfail - ' . ( floor($rid / 4 + 1) * 2 ) . ' where uid = ' . $profiledata['uid'];
					}
					else
					{
						$sql = 'update xoops_users set user_robfail = 0 where uid = ' . $profiledata['uid'];
					}
					$db->sql_query($sql);
					break;
				case $rid <= 14:
					$msg = '神蹟: ' . GetRpgItem(2);
					break;
				case $rid <= 17:
					$msg = '神蹟: 可擁有的土地數加一';
					$house_limit++;
					$sql = "update xoops_users set rpg_richer_pos = $house_limit * 100000000 + mod(rpg_richer_pos, 100000000) where uid = " . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case $rid <= 19:
					$msg = '神蹟: 強制當上道具店的工讀生';
					$sql = "update xoops_pbb_rpg_jobs set worker_id = " . $profiledata['uid'] . ", worker_name = '" . $profiledata['uname'] . "' where job_id = 3";
					$db->sql_query($sql);
					break;
				case $rid <= 21:
					$msg = '神蹟: 強制當上道具店的店小二';
					$sql = "update xoops_pbb_rpg_jobs set worker_id = " . $profiledata['uid'] . ", worker_name = '" . $profiledata['uname'] . "' where job_id = 2";
					$db->sql_query($sql);
					break;
				case 22:
					$msg = '神蹟: ' . GetSpecialItem($profiledata['uid'], 101, '水怪的便便 一坨');
					break;
				case 23:
					$msg = '神蹟: ' . GetSpecialItem($profiledata['uid'], 104, '翼翼的羽毛 一支');
					break;
				case 24:
					$msg = '神蹟: ' . ModifyHouseLevel($profiledata['uid'], 1);
					break;
				case 25:
					$msg = '神蹟: ' . ModifyHouseLevel($profiledata['uid'], 2);
					break;
				default:
					$msg = '神蹟 - 銘謝惠顧';
			}
			break;
		case -4: // 黑暗
			switch ($rid)
			{
				case $rid <= 12:
					$msg = '黑暗: 草人數加 ' . (floor($rid / 4 + 1) * 2);
					$sql = 'update xoops_users set user_robfail = user_robfail + ' . (floor($rid / 4 + 1) * 2) . ' where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case $rid <= 14:
					$msg = '黑暗: 到笙美島接受笙美訓練';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 5024 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case $rid <= 18:
					$msg = '黑暗: ' . DropRpgItem();
					break;
				case $rid <= 22:
					$msg = '黑暗: ' . SellRpgItem();
					break;
				case 23:
					$msg = '黑暗: 可擁有的土地數減一';
					if ($house_limit > 0)
					{
						$house_limit--;
						$sql = "update xoops_users set rpg_richer_pos = $house_limit * 100000000 + mod(rpg_richer_pos, 100000000) where uid = " . $profiledata['uid'];
						$db->sql_query($sql);
					}
					break;
				case 24:
					$msg = '黑暗: ' . ModifyHouseLevel($profiledata['uid'], -1);
					break;
				case 25:
					$msg = '黑暗: ' . ModifyHouseLevel($profiledata['uid'], -2);
					break;
				case 26:
					$msg = '黑暗: 被笙美過頭以致住院觀查';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 2018 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case 27:
					$msg = '黑暗: 笙美別人過頭到監獄服刑管束';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 9018 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				default:
					$msg = '黑暗 - 銘謝惠顧';
			}
			break;
		case -5: // 神賞
			switch ($rid)
			{
				case $rid <= 12:
					$msg = '神賞: 草人數減 ' . floor($rid / 4 + 1) * 4;
					if ( $profiledata['user_robfail'] >= floor($rid / 4 + 1) * 4 )
					{
						$sql = 'update xoops_users set user_robfail = user_robfail - ' . ( floor($rid / 4 + 1) * 4 ) . ' where uid = ' . $profiledata['uid'];
					}
					else
					{
						$sql = 'update xoops_users set user_robfail = 0 where uid = ' . $profiledata['uid'];
					}
					$db->sql_query($sql);
					break;
				case $rid <= 17:
					$msg = '神賞: ' . GetRpgItem(2);
					break;
				case $rid <= 20:
					$msg = '神賞: 可擁有的土地數加一';
					$house_limit++;
					$sql = "update xoops_users set rpg_richer_pos = $house_limit * 100000000 + mod(rpg_richer_pos, 100000000) where uid = " . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case $rid <= 22:
					$msg = '神賞: 強制當上道具店的工讀生';
					$sql = "update xoops_pbb_rpg_jobs set worker_id = " . $profiledata['uid'] . ", worker_name = '" . $profiledata['uname'] . "' where job_id = 3";
					$db->sql_query($sql);
					break;
				case $rid <= 24:
					$msg = '神賞: 強制當上道具店的店小二';
					$sql = "update xoops_pbb_rpg_jobs set worker_id = " . $profiledata['uid'] . ", worker_name = '" . $profiledata['uname'] . "' where job_id = 2";
					$db->sql_query($sql);
					break;
				case $rid <= 26:
					$msg = '神賞: 強制當上道具店的老板';
					$sql = "update xoops_pbb_rpg_jobs set worker_id = " . $profiledata['uid'] . ", worker_name = '" . $profiledata['uname'] . "' where job_id = 1";
					$db->sql_query($sql);
					break;
				case 27:
					$msg = '神賞: ' . GetSpecialItem($profiledata['uid'], 101, '水怪的便便 一坨');
					break;
				case 28:
					$msg = '神賞: ' . GetSpecialItem($profiledata['uid'], 104, '翼翼的羽毛 一支');
					break;
				case 29:
					$msg = '神賞: ' . GetSpecialItem($profiledata['uid'], 105, 'tima的祝福(?) 一次');
					break;
				case 30:
					$msg = '神賞: ' . ModifyHouseLevel($profiledata['uid'], 1);
					break;
				case 31:
					$msg = '神賞: ' . ModifyHouseLevel($profiledata['uid'], 2);
					break;
				case 32:
					$msg = '神賞: ' . ModifyHouseLevel($profiledata['uid'], 3);
					break;
				default:
					$msg = '神賞 - 銘謝惠顧';
			}
			break;
		case -6: // 魔罰
			switch ($rid)
			{
				case $rid <= 12:
					$msg = '魔罰: 草人數加 ' . (floor($rid / 4 + 1) * 4);
					$sql = 'update xoops_users set user_robfail = user_robfail + ' . (floor($rid / 4 + 1) * 4) . ' where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case $rid <= 14:
					$msg = '魔罰: 可擁有的土地數減一';
					if ($house_limit > 0)
					{
						$house_limit--;
						$sql = "update xoops_users set rpg_richer_pos = $house_limit * 100000000 + mod(rpg_richer_pos, 100000000) where uid = " . $profiledata['uid'];
						$db->sql_query($sql);
					}
					break;
				case $rid <= 18:
					$msg = '魔罰: ' . DropRpgItem();
					break;
				case $rid <= 22:
					$msg = '魔罰: ' . SellRpgItem();
					break;
				case 23:
					$msg = '魔罰: ' . ModifyHouseLevel($profiledata['uid'], -1);
					break;
				case 24:
					$msg = '魔罰: ' . ModifyHouseLevel($profiledata['uid'], -2);
					break;
				case 25:
					$msg = '魔罰: ' . ModifyHouseLevel($profiledata['uid'], -3);
					break;
				case 26:
					$msg = '魔罰: 到笙美島接受笙美訓練';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 5024 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case 27:
					$msg = '魔罰: 被笙美過頭以致住院觀查';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 2018 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				case 28:
					$msg = '魔罰: 笙美別人過頭到監獄服刑管束';
					$sql = 'update xoops_users set rpg_richer_pos = floor( rpg_richer_pos / 1000000) * 1000000 + 9018 where uid = ' . $profiledata['uid'];
					$db->sql_query($sql);
					break;
				default:
					$msg = '魔罰 - 銘謝惠顧';
			}
			break;
		case -7: // 醫院
			$msg = '醫院 - 既然都已經來了，那就住 1 個小時再走嘛....';
			break;
		case -8: // 監獄
			$msg = '監獄 - 既然都已經來了，那就坐 2 個小時再走嘛....';
			break;
		case -9: // 笙美島
			$msg = '笙美島 - 既然都已經來了，那就享受(?) 3 個小時再走嘛....';
			break;
	}
	return $rid . '. ' . $msg;
}

function FeeLoop($fee_x, $fee_y, $fee_level, $fee_dir)
{
	global $row, $fee_detail, $around_owner, $around_fee, $around_road, $around_mark;

	$this_fee = 0;
	if ( !( ($fee_x == $row['map_x']) && ($fee_y == $row['map_y']) && ($fee_level > 1) ) )
	{
		$this_fee = floor($around_fee['x' . ($fee_x * 1000 + $fee_y)] / $fee_level);
		$around_mark['x' . ($fee_x * 1000 + $fee_y)] = 1;
		$fee_detail .= $around_road['x' . ($fee_x * 1000 + $fee_y)] . ': ' . $this_fee . '
';
		if (( $around_owner['x' . (($fee_x + 1) * 1000 + $fee_y)] == $row['owner_id'] ) && ($fee_dir != 2))
			$this_fee += FeeLoop($fee_x + 1, $fee_y, $fee_level + 1, 0);

		if (( $around_owner['x' . (($fee_x - 1) * 1000 + $fee_y)] == $row['owner_id'] ) && ($fee_dir != 0))
			$this_fee += FeeLoop($fee_x - 1, $fee_y, $fee_level + 1, 2);

		if (( $around_owner['x' . ($fee_x * 1000 + $fee_y + 1)] == $row['owner_id'] ) && ($fee_dir != 3))
			$this_fee += FeeLoop($fee_x, $fee_y + 1, $fee_level + 1, 1);

		if (( $around_owner['x' . ($fee_x * 1000 + $fee_y - 1)] == $row['owner_id'] ) && ($fee_dir != 1))
			$this_fee += FeeLoop($fee_x, $fee_y - 1, $fee_level + 1, 3);
	}
	return $this_fee;
}

function DoFeeDeduct($owner_id, $owner_name, $fee_amount)
{
	global $db, $profiledata;

	$ret_msg = '';
	if ($fee_amount <= $profiledata['user_money'])
	{
		// 現金夠，扣現金
		$sql = "update xoops_users set user_money = user_money - $fee_amount where uid = " . $profiledata['uid'];
		$db->sql_query($sql);
		$profiledata['user_money'] -= $fee_amount;
		$sql = "update xoops_users set user_money = user_money + $fee_amount where uid = $owner_id";
		$db->sql_query($sql);
	}
	else if ($fee_amount <= $profiledata['user_bank'])
	{
		$sql = "update xoops_users set user_bank = user_bank - $fee_amount where uid = " . $profiledata['uid'];
		$db->sql_query($sql);
		$profiledata['user_bank'] -= $fee_amount;
		$sql = "update xoops_users set user_money = user_money + $fee_amount where uid = $owner_id";
		$db->sql_query($sql);
		$ret_msg = '現金不足，已從銀行存款中扣款。';
	}
	else if ($fee_amount <= ($profiledata['user_money'] + $profiledata['user_bank']) )
	{
		$sql = "update xoops_users set user_bank = 0, user_money = user_money - ($fee_amount - " . $profiledata['user_bank'] .
			") where uid = " . $profiledata['uid'];
		$db->sql_query($sql);
		$profiledata['user_money'] -= $fee_amount - $profiledata['user_bank'];
		$profiledata['user_bank'] = 0;
		$sql = "update xoops_users set user_money = user_money + $fee_amount where uid = $owner_id";
		$db->sql_query($sql);
		$ret_msg = '銀行存款已全部扣款，不足部份以現金給付。';
	}
	else
	{
		$ret_msg = '';
		$sell_money = $profiledata['user_money'] + $profiledata['user_bank'];
		// 處分道具
		$sql = 'select o.own_id, i.item_name, i.item_price from xoops_pbb_rpg_owns o, xoops_pbb_rpg_items i where o.uid = ' .
			$profiledata['uid'] . ' and o.item_id = i.item_id order by i.item_price desc';
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			$sell_item = array();
			do
			{
				$sell_money += floor($row['item_price'] * 0.6);
				$sell_item[] = $row['own_id'];
				$ret_msg .= $row['item_name'] . '&nbsp;';
				if ($sell_money >= $fee_amount)
				{
					break;
				}
			}
			while ( $row = $db->sql_fetchrow($result) );
			$sql = 'delete from xoops_pbb_rpg_owns where own_id in (' . implode(', ', $sell_item) . ')';
			$db->sql_query($sql);
		}

		if ($sell_money < $fee_amount)
		{
			// 開始抄家
			$sql = 'select * from xoops_pbb_rpg_map where owner_id = ' . $profiledata['uid'] . ' order by base_rental';
			$result = $db->sql_query($sql);
			if ( $row = $db->sql_fetchrow($result) )
			{
				$sell_house = array();
				do
				{
					$sell_money += floor($row['base_rental'] * ($row['house_level'] + 1) * $row['rental_factor'] * 0.9);
					$sell_house[] = $row['map_id'];
					$ret_msg .= $row['road_name'] . '&nbsp;';
					if ($sell_money >= $fee_amount)
					{
						break;
					}
				}
				while ( $row = $db->sql_fetchrow($result) );
				$sql = 'update xoops_pbb_rpg_map set owner_id = 0 where map_id in (' . implode(', ', $sell_house) . ')';
				$db->sql_query($sql);
			}
		}

		if ( $sell_money >= $fee_amount )
		{
			$sql = "update xoops_users set user_money = $sell_money - $fee_amount, user_bank = 0 where uid = " . $profiledata['uid'];
			$db->sql_query($sql);
			$sql = "update xoops_users set user_money = user_money + $fee_amount where uid = $owner_id";
			$db->sql_query($sql);
			$ret_msg = '因現金存款不足，所以您的道具和資產 ( ' . $ret_msg . ') 已遭到法院拍賣，得現金 $' .
					($sell_money - $profiledata['user_money'] - $profiledata['user_bank']) .
					'，加上目前現金 $' . $profiledata['user_money'] . ' 及銀行存款 $' . $profiledata['user_bank'] .
					'，付完過路費後，剩 $' . ($sell_money - $fee_amount);
			$profiledata['user_money'] = $sell_money - $fee_amount;
			$profiledata['user_bank'] = 0;
		}
		else
		{
			$sql = "update xoops_users set user_bank = 0, user_money = 0 where uid = " . $profiledata['uid'];
			$db->sql_query($sql);
			$sql = "update xoops_users set user_money = user_money + $sell_amount where uid = $owner_id";
			$db->sql_query($sql);
			$profiledata['user_money'] = 0;
			$profiledata['user_bank'] = 0;
			$ret_msg = '恭禧您!!您已經破產了!! ' .
					($ret_msg == '' ? '' : '您的道具和資產 ( ' . $ret_msg . ') 已遭到法院拍賣。 ') .
					'乖乖在原地當小乞丐，等好心的大爺賞您銀子吧!';
			$fee_amount = $sell_amount;
		}
	}
	$sql = 'insert into xoops_pbb_rpg_event values (' . $profiledata['uid'] . ', ' . time() . ', 4, 0, ' .
		$fee_amount . ', "支出: ' . $owner_name . '")';
	$db->sql_query($sql);
	$sql = 'insert into xoops_pbb_rpg_event values (' . $owner_id . ', ' . time() . ', 4, 1, ' .
		$fee_amount . ', "收入: ' . $profiledata['uname'] . '")';
	$db->sql_query($sql);
	if ($ret_msg != '') $ret_msg = '<br />處理結果: ' . $ret_msg;
	return $ret_msg;
}
?>