<?php

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
	exit;
}

function CheckMyDead($uid)
{
	global $db;

	$sql = "select rpg_cur_hp from xoops_users where uid = $uid";
	$result = $db->sql_query($sql);
	if ( $row2 = $db->sql_fetchrow($result) )
	{
		return ( $row2['rpg_cur_hp'] <= 0 );
	}
	else
	{
		return false;
	}
}

function GetButtonList($uid, $itemid = 0)
{
	global $db;

	$btn_list = '<input type="submit" name="go2move" class="liteoption" value="移動">&nbsp;<input type="submit" name="go2attack" class="liteoption" value="攻擊">';
	$sql = "select x.setting_value2, i.item_id, i.item_name from xoops_pbb_rpg_owns o, xoops_pbb_rpg_items i, xoops_pbb_rpg_medals m, xoops_pbb_rpg_setting x
		where o.uid = $uid and i.in_which_store = 3 and m.position_order = 0 and m.uid = $uid and x.setting_id = 1 and
		i.item_id = o.item_id and i.need_class = m.position_class and m.position_class = x.setting_value1
		order by x.setting_value1, i.need_level";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		$btn_list .= '&nbsp;<select name="sp_skill">';
		do
		{
			$btn_list .= '<option value="' . $row['item_id'] . '"' . ($row['item_id'] == $itemid ? ' selected' : '') . '>技能 : ' . $row['setting_value2'] . ' : ' . $row['item_name'] . '</option>';
		}
		while ( $row = $db->sql_fetchrow($result) );
		$btn_list .= '</select>&nbsp;<input type="submit" name="go2skill" class="liteoption" value="使用">';
	}
	$btn_list .= '&nbsp;<input type="submit" name="go2position" class="liteoption" value="全員位置">&nbsp;<input type="submit" name="go2disp" class="liteoption" value="陣亡列表">&nbsp;<input type="submit" name="go2escape" class="liteoption" value="我是肉腳">';

	unset($uid);
	unset($itemid);
	unset($sql);
	unset($row);
	unset($result);
	return $btn_list;
}
function GetSkillName($itemid)
{
	global $db;

	$sql = "select item_name from xoops_pbb_rpg_items where in_which_store = 3 and item_id = $itemid";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		return $row['item_name'];
	}
	else
	{
		return "";
	}
}

function CheckSkill($uid, $itemid)
{
	global $db;

	$sql = "select i.item_id from xoops_pbb_rpg_owns o, xoops_pbb_rpg_items i, xoops_pbb_rpg_medals m
		where o.uid = $uid and m.uid = $uid and i.in_which_store = 3 and i.item_id = $itemid and
		o.item_id = $itemid and i.need_class = m.position_class and i.need_level = m.position_order + 1";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ($row['item_id'] > 0)
		{
			return "";
		}
		else
		{
			return "您沒有這項技能喲!!";
		}
	}
	else
	{
		return "您沒有這項技能喲!!";
	}
}

function GetMaxPoint($uid, $next_act)
{
	global $db;

	$max_point = 1;

	if ($next_act == 1 || $next_act == 2)
	{
		$sql = "select max(position_order) as max_point from xoops_pbb_rpg_medals where uid = $uid";
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
			$max_point = $row['max_point'] + 1;
	}
	else
	{
		$sql = "select max(m.position_class) as now_class, max(m.position_order) as max_point from xoops_pbb_rpg_medals m, xoops_pbb_rpg_items i where i.item_id = $next_act and m.uid = $uid and i.need_class = m.position_class";
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ( $row['now_class'] != 1 ) // 戰士系範圍為 1
				$max_point = $row['max_point'] + 1;

		}
	}

	unset($row);
	unset($result);
	return $max_point;
}

function GenerateSamePlayer($pfile, $now_x, $now_y, $max_point)
{
	global $db;

	$sql = "select count(*) as scope_count from xoops_pbb_rpg_eol
		where (abs(map_x - $now_x) + abs(map_y - $now_y)) <= $max_point and map_x <> $now_x and map_y <> $now_y and can_not_pass = 0";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ( $row['scope_count'] > 0 )
		{
			srand((double)microtime()*1000000);
			for ($i = 1; $i <= $max_point; $i++)
			{
				$sql = "insert into xoops_pbb_rpg_npc
					(map_id, now_x, now_y, now_icon, now_dir, rpg_level, rpg_cur_hp, rpg_max_hp,
					rpg_attack, rpg_defense, rpg_magic, rpg_speed, generate_time, npc_name, rpg_cur_mp, rpg_max_mp) " .
					" select map_id, map_x, map_y, '" .
					substr($pfile['rpg_eol_data'], -11, 2) . "' as now_icon, " .
					rand(0, 3) . " as now_dir, " .
					$pfile['rpg_level'] . " as now_level, " .
					$pfile['rpg_cur_hp'] . " as rpg_cur_hp, " .
					$pfile['rpg_max_hp'] . " as rpg_max_hp, " .
					$pfile['rpg_attack'] . " as rpg_attack, " .
					$pfile['rpg_defense'] . " as rpg_defense, " .
					$pfile['rpg_magic'] . " as rpg_magic, " .
					$pfile['rpg_speed'] . " as rpg_speed, " .
					($pfile['rpg_cur_hp'] == $pfile['rpg_max_hp'] ? (time() - 21600 + 60 * $max_point) : (time() - 43200 + 60 * $max_point)) . " as generate_time, '" .
					$pfile['uname'] . "' as npc_name, " .
					$pfile['rpg_cur_mp'] . " as rpg_cur_mp, " .
					$pfile['rpg_max_mp'] . " as rpg_max_mp from xoops_pbb_rpg_eol " .
					" where (abs(map_x - $now_x) + abs(map_y - $now_y)) <= $max_point and map_x <> $now_x and map_y <> $now_y and can_not_pass = 0 limit " . rand(0, $row['scope_count'] - 1) . ", 1";
				$db->sql_query($sql);
			}
		}
	}
	unset($i);
	unset($sql);
	unset($row);
	unset($result);
}

function GenerateNPC()
{
	global $db;

	// 自爆
	$sql = "delete from xoops_pbb_rpg_npc where (rpg_cur_hp = rpg_max_hp and generate_time <= " . (time() - 21600) .
		") or generate_time is null or generate_time <= " . (time() - 43200);
	$db->sql_query($sql);

	$rpg_lv = array();
	$npc_lv = array();
	$sql = "select distinct rpg_level from xoops_pbb_rpg_npc where npc_name is null";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		do
		{
			$npc_lv[] = $row['rpg_level'] - 5;
		}
		while ( $row = $db->sql_fetchrow($result) );
	}
	$sql = "select rpg_level from xoops_users where rpg_eol_data is not null and right(rpg_eol_data, 6) <> '000086' and rpg_level not in (" .
		implode(', ', $npc_lv) . ")";

	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		do
		{
			$rpg_lv[] = $row['rpg_level'] + 5;
		}
		while ( $row = $db->sql_fetchrow($result) );
	}
	$npc_count = 10;
	$npc_level = 1;

	// 普通路人
	$sql = "select avg(rpg_level) as eol_level from xoops_users where rpg_level > 10 and rpg_eol_data is not null";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		$npc_level = $row['eol_level'];
	}

	$sql = "select count(*) as now_count from xoops_pbb_rpg_npc where (npc_name is null or npc_name = '') and now_x < 24 and now_y > 47";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ( $row['now_count'] < $npc_count )
		{
			for ($i = $row['now_count']; $i < $npc_count; $i++)
			{
				$rpg_lv[] = rand(($npc_level - 10 > 0 ? $npc_level - 10 : 1), $npc_level + 100);
			}
		}
	}

	if ( count($rpg_lv) > 0 )
	{
		srand((double)microtime()*1000000);
		$sql = "select count(*) as map_count from xoops_pbb_rpg_eol where map_id = 0 and can_not_pass = 0 and map_x < 24 and map_y > 47";
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$map_count = $row['map_count'] - 1;
		for ($i = 0; $i < count($rpg_lv); $i++)
		{
			$now_level = $rpg_lv[$i];
			$rpg_max_hp = floor( $now_level * (100 + $now_level));
			$rpg_attack = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
			$rpg_defense = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
			$rpg_magic = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
			$rpg_speed = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
			$sql = "insert into xoops_pbb_rpg_npc (map_id, now_x, now_y, now_icon, now_dir, rpg_level,
				rpg_cur_hp, rpg_max_hp, rpg_attack, rpg_defense, rpg_magic, rpg_speed, generate_time)
				select map_id, map_x as now_x, map_y as now_y, '" . sprintf("%02d", rand(0, 22)) . "' as now_icon, " .
				rand(0, 3) . " as now_dir, $now_level as now_level, $rpg_max_hp as rpg_cur_hp, $rpg_max_hp as rpg_max_hp,
				$rpg_attack as rpg_attack, $rpg_defense as rpg_defense, $rpg_magic as rpg_magic, $rpg_speed as rpg_speed, " . time() .
				" as generate_time from xoops_pbb_rpg_eol where map_id = 0 and can_not_pass = 0 and map_x < 24 and map_y > 47 limit " . rand(0, $map_count) . ", 1";
			$db->sql_query($sql);
		}
	}

	// 進階路人
	$npc_level = 0;
	$sql = "select rpg_level from xoops_users order by rpg_level desc limit 0, 20";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		do
		{
			$npc_level += $row['rpg_level'];
		}
		while ( $row = $db->sql_fetchrow($result) );
	}
	$npc_level = floor($npc_level / 20);

	$sql = "select count(*) as eol_count from xoops_users
		where rpg_eol_data is not null and right(rpg_eol_data, 6) <> '000086'";
	$result = $db->sql_query($sql);
	$npc_count = 10;
	if ( $row = $db->sql_fetchrow($result) )
	{
		$npc_count = ($row['eol_count'] * 2 > 10 ? $row['eol_count'] * 2 : 10);
	}

	$sql = "select count(*) as now_count from xoops_pbb_rpg_npc where (npc_name is null or npc_name = '') and rpg_level >= $npc_level";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ( $row['now_count'] < $npc_count )
		{
			$now_count = $row['now_count'];
			srand((double)microtime()*1000000);
			$sql = "select count(*) as map_count from xoops_pbb_rpg_eol where map_id = 0 and can_not_pass = 0";
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			$map_count = $row['map_count'] - 1;
			for ($i = $now_count; $i < $npc_count; $i++)
			{
				$now_level = rand($npc_level, $npc_level + 50);
				$rpg_max_hp = floor( $now_level * (100 + $now_level));
				$rpg_attack = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
				$rpg_defense = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
				$rpg_magic = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
				$rpg_speed = floor($now_level * rand((floor($now_level / 10) + 1) * 10, (floor($now_level / 10) * 2 + 5) * 10) / 10);
				$sql = "insert into xoops_pbb_rpg_npc (map_id, now_x, now_y, now_icon, now_dir, rpg_level,
					rpg_cur_hp, rpg_max_hp, rpg_attack, rpg_defense, rpg_magic, rpg_speed, generate_time)
					select map_id, map_x as now_x, map_y as now_y, '" . sprintf("%02d", rand(0, 22)) . "' as now_icon, " .
					rand(0, 3) . " as now_dir, $now_level as now_level, $rpg_max_hp as rpg_cur_hp, $rpg_max_hp as rpg_max_hp,
					$rpg_attack as rpg_attack, $rpg_defense as rpg_defense, $rpg_magic as rpg_magic, $rpg_speed as rpg_speed, " . time() .
					" as generate_time from xoops_pbb_rpg_eol where map_id = 0 and can_not_pass = 0 limit " . rand(0, $map_count) . ", 1";
				$db->sql_query($sql);
			}
		}
	}

	unset($i);
	unset($npc_count);
	unset($npc_level);
	unset($now_level);
	unset($rpg_max_hp);
	unset($rpg_attack);
	unset($rpg_defense);
	unset($rpg_magic);
	unset($rpg_speed);
	unset($map_count);
	unset($now_count);
	unset($sql);
	unset($row);
	unset($result);
	unset($rpg_lv);
	unset($npc_lv);
}

function UserIsDead($wsql, $wsql2, $uid, $uname, $ulv, $deadtype, $is_npc = false)
{
	global $db;

	$ret_msg = '';
	if ( $is_npc )
	{
		$sql = "select count(*) as dead_count, sum(rpg_level) as dead_level, sum(rpg_max_hp) as dead_money from xoops_pbb_rpg_npc where rpg_cur_hp <= 0 and $wsql";
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ( $row['dead_count'] > 0 )
			{
				$sql = "select npc_id from xoops_pbb_rpg_npc where rpg_cur_hp <= 0 and $wsql";
				$result = $db->sql_query($sql);
				if ( $row2 = $db->sql_fetchrow($result) )
				{
					$ret_msg = ' 砍死: ';
					do
					{
						$ret_msg .= ($row2['npc_name'] == '' ? '路人' . $row2['npc_id'] : $row2['npc_name']) . ' ';
					}
					while ( $row2 = $db->sql_fetchrow($result) );
					$ret_msg .= "死因: $deadtype 並獲得若干道具: ";
				}
				$sql = "select count(*) as item_count from xoops_pbb_rpg_items where in_which_store <= 2 and need_level <= $ulv";
				$result = $db->sql_query($sql);
				$row2 = $db->sql_fetchrow($result);
				$item_count = $row2['item_count'] - 1;
				srand((double)microtime()*1000000);
				for ($i = 1; $i <= $row['dead_count']; $i++)
				{
					$sql = "select item_id, item_name from xoops_pbb_rpg_items where in_which_store <= 2 and need_level <= $ulv order by item_id limit " . rand(0, $item_count) . " , 1";
					$result = $db->sql_query($sql);
					$row2 = $db->sql_fetchrow($result);
					$ret_msg .= $row2['item_name'] . ' ';
					$sql = "insert into xoops_pbb_rpg_owns (uid, item_id) values ($uid, " . $row2['item_id'] . ")";
					$db->sql_query($sql);
				}

				// 超出 50 項，砍掉
				$sql = "select own_id from xoops_pbb_rpg_owns where uid = $uid order by own_id desc limit 50, 1";
				$result = $db->sql_query($sql);
				if ( $row2 = $db->sql_fetchrow($result) )
				{
					$sql = "delete from xoops_pbb_rpg_owns where uid = $uid and own_id <= " . $row2['own_id'];
					$db->sql_query($sql);
				}

				// 寫入砍殺記錄
				$sql = "insert into xoops_pbb_rpg_event (uid, event_time, event_class, event_type, event_monty, event_note)
					values ($uid, " . time() . ", 5, 0, " . $row['dead_money'] . ", '$deadtype: 路人 " . $row['dead_count'] . " 隻')";
				$db->sql_query($sql);
				// 砍死人嘉獎
				$sql = "update xoops_users set user_money = user_money + " . $row['dead_money'] .
					", rpg_cur_exp = rpg_cur_exp + " . $row['dead_level'] . " where uid = $uid";
				$db->sql_query($sql);

				$sql = "delete from xoops_pbb_rpg_npc where rpg_cur_hp <= 0";
				$db->sql_query($sql);

				GenerateNPC(); // 補充生力軍
				LevelUp($uid, true); // 升級
			}
		}
	}
	else
	{
		$sql = "select count(*) as dead_count, sum(floor((user_money + user_bank) * 0.2)) as dead_money, sum(rpg_level) as dead_level from xoops_users
			where rpg_cur_hp <= 0 and $wsql";
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ( $row['dead_count'] > 0 )
			{
				// 取得被害者的道具
				$sql = "select u.uid, u.uname, min(o.own_id) as own_id from xoops_users u, xoops_pbb_rpg_owns o
					where u.uid = o.uid and u.rpg_cur_hp <= 0 and $wsql2 group by u.uid, u.uname";
				$result = $db->sql_query($sql);
				if ( $row2 = $db->sql_fetchrow($result) )
				{
					$ownlist = array();
					$ret_msg = ' 砍死: ';
					do
					{
						$ownlist[] = $row2['own_id'];
						$ret_msg .= $row2['uname'] . ' ';
					}
					while ( $row2 = $db->sql_fetchrow($result) );
					$ret_msg .= "死因: $deadtype 並獲得若干道具: ";
					$sql = "select i.item_name from xoops_pbb_rpg_owns o, xoops_pbb_rpg_items i where o.own_id in (" .
						implode(', ', $ownlist) . ") and o.item_id = i.item_id";
					$result = $db->sql_query($sql);
					if ( $row2 = $db->sql_fetchrow($result) )
					{
						do
						{
							$ret_msg .= $row2['item_name'] . ' ';
						}
						while ( $row2 = $db->sql_fetchrow($result) );
					}
					$sql = "update xoops_pbb_rpg_owns set uid = $uid where own_id in (" . implode(', ', $ownlist) . ")";
					$db->sql_query($sql);
					// 超出 50 項，砍掉
					$sql = "select own_id from xoops_pbb_rpg_owns where uid = $uid order by own_id desc limit 50, 1";
					$result = $db->sql_query($sql);
					if ( $row2 = $db->sql_fetchrow($result) )
					{
						$sql = "delete from xoops_pbb_rpg_owns where uid = $uid and own_id <= " . $row2['own_id'];
						$db->sql_query($sql);
					}
					unset($ownlist);
				}

				// 寫入砍殺記錄
				$sql = "insert into xoops_pbb_rpg_event select $uid as uid, " . time() .
					" as event_time, 5 as event_class, 0 as event_type, floor(user_money * 0.2) as event_money, " .
					"concat('$deadtype: ', uname) as event_note from xoops_users where rpg_cur_hp <= 0 and $wsql";
				$db->sql_query($sql);
				// 寫入被砍記錄
				$sql = "insert into xoops_pbb_rpg_event select uid, " . time() .
					" as event_time, 5 as event_class, 1 as event_type, floor(user_money * -0.2) as event_money, " .
					"concat('被$deadtype: $uname($ulv) LV: ', rpg_level) as event_note from xoops_users where rpg_cur_hp <= 0 and $wsql";
				$db->sql_query($sql);
				// 被砍死處罰
				$sql = "update xoops_users set rpg_level = rpg_level - if(rpg_level <= 1, 0, if(rpg_level >= $ulv, 1, 0)),
					user_money = floor(user_money * 0.8), user_bank = floor(user_bank * 0.8), rpg_eol_killed = rpg_eol_killed + 1,
					rpg_cur_hp = rpg_max_hp, rpg_eol_data = concat('0', substring(rpg_eol_data, 2, 2), '000000086'),
					rpg_cur_mp = rpg_max_mp where rpg_cur_hp <= 0 and $wsql";
				$db->sql_query($sql);
				// 砍死人嘉獎
				$sql = "update xoops_users set rpg_eol_kill = rpg_eol_kill + 1, user_money = user_money + " . $row['dead_money'] .
					", rpg_cur_exp = rpg_cur_exp + " . $row['dead_level'] . " where uid = $uid";
				$db->sql_query($sql);
				// 升級
				LevelUp($uid, true);
			}
		}
		$sql = "update xoops_users set rpg_cur_hp = rpg_max_hp, rpg_eol_data = concat('0', substring(rpg_eol_data, 2, 2), '000000086'),
			rpg_cur_mp = rpg_max_mp where rpg_cur_hp <= 0";
		$db->sql_query($sql);
	}

	unset($sql);
	unset($row);
	unset($row2);
	unset($result);
	return $ret_msg;
}

function AttackPercent($self, $target)
{
	if ( $self == 0 ) return 95;
	if ( $target == $self ) return 80;
	if ( $target > $self )
	{
		if ( ($target - $self) / $self * 8 > 75 )
		{
			return 5;
		}
		else
		{
			return 80 - floor( ($target - $self) / $self * 8 );
		}
	}
	else
	{
		if ( ($self - $target) / $target * 2 > 15 )
		{
			return 95;
		}
		else
		{
			return 80 + floor( ($self - $target) / $target * 2 );
		}
	}
}

function GetNeedMP($itemid, $ulv, $plv, $max_mp)
{
	global $db;

	$sql = "select need_mp1, need_mp2 from xoops_pbb_rpg_items where in_which_store = 3 and item_id = $itemid";
	$result = $db->sql_query($sql);
	if ( $row = $db->sql_fetchrow($result) )
	{
		if ( $row['need_mp1'] == $row['need_mp2'] && intval($row['need_mp1']) != 0 && intval($row['need_mp2']) != 0 )
		{
			if ( $row['need_mp1'] * $row['need_mp2'] > $max_mp )
			{
				return $row['need_mp1'] * $row['need_mp2'];
			}
			else
			{
				return $max_mp;
			}
		}
		else if ( $row['need_mp1'] == '0' || $row['need_mp2'] == '0' )
		{
			return $max_mp;
		}
		else
		{
			return ($row['need_mp1'] == 'LV' ? $ulv : ($row['need_mp1'] == 'PL' ? $plv : intval($row['need_mp1']))) *
				($row['need_mp2'] == 'LV' ? $ulv : ($row['need_mp2'] == 'PL' ? $plv : intval($row['need_mp2'])));
		}
	}
	else
	{
		return 0;
	}
}

function CounterAttack(&$am_i_dead, $pfile, $row, $is_npc)
{
	global $db;

	if ( $row['rpg_cur_hp'] <= 0 ) return '';

	srand((double)microtime()*1000000);
	$ret_msg = '';
	if ( rand(1, 100) <= AttackPercent($row['rpg_speed'], $pfile['rpg_speed']) )
	{
		if ( $row['rpg_attack'] > $pfile['rpg_defense'] + 10 )
		{
			$hp_deduct = rand(10, $row['rpg_attack'] - $pfile['rpg_defense']);
		}
		else
		{
			$hp_deduct = 10;
		}
		$hp_deduct = floor($hp_deduct / rand(2, 10));
		$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp - $hp_deduct where uid = " . $pfile['uid'];
		$db->sql_query($sql);

		$ret_msg = " 對手 " . ($is_npc ? ($row['npc_name'] == '' ? '路人' . $row['npc_id'] : $row['npc_name']) : $row['uname']) . " 反擊，損傷 $hp_deduct 點。";
		$am_i_dead = CheckMyDead($pfile['uid']);
		if ( $am_i_dead )
		{
			if ( $is_npc )
			{
				$sql = "update xoops_users set user_money = floor(user_money * 0.8), user_bank = floor(user_bank * 0.8), rpg_eol_killed = rpg_eol_killed + 1,
					rpg_cur_hp = rpg_max_hp, rpg_eol_data = concat('0', substring(rpg_eol_data, 2, 2), '000000086'), rpg_cur_mp = rpg_max_mp
					where rpg_cur_hp <= 0 and uid = " . $pfile['uid'];
				$db->sql_query($sql);
				$sql = "select min(own_id) as own_id from xoops_pbb_rpg_owns where uid = " . $pfile['uid'];
				$result = $db->sql_query($sql);
				$row2 = $db->sql_fetchrow($result);
				$sql = "delete from xoops_pbb_rpg_owns where own_id = " . $row2['own_id'];
				$db->sql_query($sql);
				unset($result);
				unset($row2);
			}
			else
			{
				UserIsDead("uid = " . $pfile['uid'], "u.uid = " . $pfile['uid'], $row['uid'], $row['uname'], $row['rpg_level'], "反擊($hp_deduct)");
			}
		}
		unset($hp_deduct);
		unset($sql);
	}
	return $ret_msg;
}

function SubmitNext($next_x, $next_y, $next_act, $item_id)
{
	global $db, $now_x, $now_y, $now_dir, $now_act, $profiledata;

	$am_i_dead = false;
	$ret_msg = '';
	$max_point = GetMaxPoint($profiledata['uid'], ($next_act == 1 || $next_act == 2 ? $next_act : $item_id) );
	if ( $next_act >= 3 )
	{
		$need_mp = GetNeedMP($item_id, $profiledata['rpg_level'], $max_point, $profiledata['rpg_max_mp']);
	}
	else
	{
		$need_mp = 0;
	}

	if (( (abs($next_x - $now_x) + abs($next_y - $now_y) > $max_point) || $next_x < 0 || $next_y < 0 || $next_x > 47 || $next_y > 95 ) &&
		$next_act != 4 ) // 去除隱身，分身，補血
	{
		$ret_msg = '媽媽說作弊是不行的喲!!或者....您已經陣亡了。';
	}
	else if ( $profiledata['rpg_cur_mp'] < $need_mp )
	{
		$ret_msg = " MP 不足，所需 MP : $need_mp";
	}
	else if ( $next_act == 1 ) // 移動
	{
		// 決定面對的方向
		if ($next_y == $now_y)
		{
			if ($next_x < $now_x)
			{
				$now_dir = 1;
			}
			else if ($next_x > $now_x)
			{
				$now_dir = 3;
			}
		}
		else if ($next_x == $now_x)
		{
			if ($next_y < $now_y)
			{
				$now_dir = 2;
			}
			else
			{
				$now_dir = 0;
			}
		}
		else if ($next_y <= $now_y + $max_point / 3 && $next_y >= $now_y - $max_point / 3)
		{
			if ($next_x < $now_x)
			{
				$now_dir = 1;
			}
			else if ($next_x > $now_x)
			{
				$now_dir = 3;
			}
		}
		else if ($next_y < $now_y)
		{
			$now_dir = 2;
		}
		else
		{
			$now_dir = 0;
		}
		$sql = "select count(*) as already_in from xoops_users where right(rpg_eol_data, 6) = '" . sprintf("%03d%03d", $next_x, $next_y) . "'";
		$result = $db->sql_query($sql);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if ( $row['already_in'] > 0 )
			{
				$ret_msg = '這個位置已經有人佔了喲!!';
			}
			else
			{
				$sql = "select count(*) as already_in from xoops_pbb_rpg_npc where now_x = $next_x and now_y = $next_y";
				$result = $db->sql_query($sql);
				if ( $row = $db->sql_fetchrow($result) )
				{
					if ( $row['already_in'] > 0 )
					{
						$ret_msg = '這個位置已經有人佔了喲!!';
					}
					else
					{
						$now_x = $next_x;
						$now_y = $next_y;
					}
				}
			}
		}
	}
	else
	{
		if ( $need_mp > 0 )
		{
			$sql = "update xoops_users set rpg_cur_mp = rpg_cur_mp - $need_mp where uid = " . $profiledata['uid'];
			$db->sql_query($sql);
		}

		if ( $next_act == 2 || $next_act == 3 )
		{
			$is_npc = false;
			$sql = "select uid, uname, rpg_cur_hp, rpg_level, rpg_attack, rpg_defense, rpg_magic, rpg_speed from xoops_users where right(rpg_eol_data, 6) = '" . sprintf("%03d%03d", $next_x, $next_y) . "'";
			$result = $db->sql_query($sql);
			if ( !( $row = $db->sql_fetchrow($result) ) )
			{
				$is_npc = true;
				$sql = "select * from xoops_pbb_rpg_npc where now_x = $next_x and now_y = $next_y";
				$result = $db->sql_query($sql);
				if ( $row = $db->sql_fetchrow($result) )
				{
					if ( $profiledata['rpg_level'] - 10 >= $row['rpg_level'] )
						$ret_msg = "媽媽說不能欺負弱小，尤其是無辜的路人....";
					$npc_id = $row['npc_id'];
					$npc_scope = floor(($row['rpg_level'] - 1) / 10) + 1;
					if ($npc_scope > 9) $npc_scope = 9;
					$is_normal_npc = $row['npc_name'] == '';
				}
				else
				{
					$npc_id = 0;
					$ret_msg = "目標人物溜了....";
				}
			}
			else
			{
				if ( $profiledata['rpg_level'] - 10 >= $row['rpg_level'] )
					$ret_msg = "媽媽說不能欺負弱小....";
			}
		}

		if ( $ret_msg == '' )
		{
			$sql = "select count(*) as weapon_count, max(need_level) as weapon_level from xoops_pbb_rpg_items i, xoops_pbb_rpg_owns o where o.uid = " . $profiledata['uid'] .
				" and i.in_which_store = 1 and i.item_id = o.item_id";
			$result = $db->sql_query($sql);
			if ( $row2 = $db->sql_fetchrow($result) )
			{
				if ( $row2['weapon_count'] > 2 )
				{
					$ret_msg = "您沒有多出來的手可以拿其它武器了!!";
				}
				else if ( $row2['weapon_level'] > $profiledata['rpg_level'] )
				{
					$ret_msg = "您現在的等級無法使用這支武器喲!!";
				}
			}
		}

		if ( $ret_msg == '' && $next_act >= 3 ) $ret_msg = CheckSkill($profiledata['uid'], $item_id);

		if ( $ret_msg == '' )
		{
			srand((double)microtime()*1000000);
			if ( $next_act >= 3 ) $next_act = $item_id;
			switch ( $next_act )
			{
				case 2: // 攻擊
					$rid = rand(1, 100);
					switch ( $rid )
					{
						case 1:
							$double = 5;
							$ret_msg = $profiledata['uname'] . '使出 黑暗攻擊 ';
							$dead_cause = '黑暗攻擊';
							break;
						case $rid <= 3:
							$double = 4;
							$ret_msg = $profiledata['uname'] . '使出 全力攻擊 ';
							$dead_cause = '全力攻擊';
							break;
						case $rid <= 8:
							$double = 3;
							$ret_msg = $profiledata['uname'] . '使出 用力攻擊 ';
							$dead_cause = '用力攻擊';
							break;
						case $rid <= 18:
							$double = 2;
							$ret_msg = $profiledata['uname'] . '使出 會心攻擊 ';
							$dead_cause = '會心攻擊';
							break;
						default:
							$double = 1;
							$ret_msg = $profiledata['uname'] . '使出 普通攻擊 ';
							$dead_cause = '普通攻擊';
							break;
					}
					if ( $profiledata['rpg_attack'] > $row['rpg_defense'] )
					{
						$hp_deduct = rand( ( ($profiledata['rpg_attack'] / 2 - $row['rpg_defense']) > 0 ? floor($profiledata['rpg_attack'] / 2 - $row['rpg_defense']) : 1) , $profiledata['rpg_attack'] - $row['rpg_defense']);
					}
					else
					{
						$hp_deduct = 10;
					}
					if ( $hp_deduct < $max_point * 10 )
						$hp_deduct = rand($max_point * 10, $max_point * 20);
					$hp_deduct *= $double;

					if ( rand(1, 100) > AttackPercent($profiledata['rpg_speed'], $row['rpg_speed']) )
					{
						$ret_msg .= '....遜，沒打中!!';
					}
					else
					{
						$sql = "update " . ($is_npc ? "phpbb_rpg_npc" : "xoops_users") .
							" set rpg_cur_hp = rpg_cur_hp - $hp_deduct where " . ($is_npc ? "npc_id = " . $row['npc_id'] : "uid = " . $row['uid']);
						$db->sql_query($sql);
						$dead_cause .= "($hp_deduct)";
						$ret_msg .= "擊中 " . ($is_npc ? ($row['npc_name'] == '' ? "路人" . $row['npc_id'] : $row['npc_name']) : $row['uname']) . " 損傷 $hp_deduct 點 ";
					}
					// 對手反擊
					$row['rpg_cur_hp'] -= $hp_deduct;
					$ret_msg .= CounterAttack($am_i_dead, $profiledata, $row, $is_npc);
					if ( $is_npc )
					{
						if ( $is_normal_npc )
						$ret_msg .= UserIsDead("npc_id = " . $row['npc_id'], '', $profiledata['uid'], '', 0, $dead_cause, true);
					}
					else
					{
						$ret_msg .= UserIsDead("uid = " . $row['uid'], "u.uid = " . $row['uid'], $profiledata['uid'], $profiledata['uname'], $profiledata['rpg_level'], $dead_cause);
					}
					break;
				case 109: // HP 10%
					$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp + floor(rpg_max_hp * 0.1) where uid = " . $profiledata['uid'];
					$db->sql_query($sql);
					$sql = "update xoops_users set rpg_cur_hp = rpg_max_hp where rpg_cur_hp > rpg_max_hp and uid = " . $profiledata['uid'];
					$db->sql_query($sql);
					$ret_msg = "施咒完成， HP 恢復 10% :: " . floor($profiledata['rpg_max_hp'] * 0.1) . "點";
					break;
				case 110: // HP 20%
					$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp + floor(rpg_max_hp * 0.2) where uid = " . $profiledata['uid'];
					$db->sql_query($sql);
					$sql = "update xoops_users set rpg_cur_hp = rpg_max_hp where rpg_cur_hp > rpg_max_hp and uid = " . $profiledata['uid'];
					$db->sql_query($sql);
					$ret_msg = "施咒完成， HP 恢復 20% :: " . floor($profiledata['rpg_max_hp'] * 0.2) . "點";
					break;
				case 111: // HP 100%
					$sql = "update xoops_users set rpg_cur_hp = rpg_max_hp where uid = " . $profiledata['uid'];
					$db->sql_query($sql);
					$ret_msg = "施咒完成， HP 完全恢復";
					break;
				case 112: // 蘿莉吸血式
					$hp_deduct = floor($row['rpg_cur_hp'] * 0.05);
					if ( $hp_deduct > 0 || rand(1, 100) <= AttackPercent($row['rpg_magic'], $profiledata['rpg_magic']) )
					{
						if ( $is_npc )
						{
							$sql = "update xoops_pbb_rpg_npc set rpg_cur_hp = rpg_cur_hp - $hp_deduct where npc_id = " . $row['npc_id'];
							$db->sql_query($sql);
							$ret_msg = "吸血完成，增加 $hp_deduct 點 HP ，受害者: 路人" . $row['npc_id'];
						}
						else
						{
							$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp - $hp_deduct where uid = " . $row['uid'];
							$db->sql_query($sql);
							$ret_msg = "吸血完成，增加 $hp_deduct 點 HP ，受害者: " . $row['uname'];
						}
						$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp + $hp_deduct where uid = " . $profiledata['uid'];
						$db->sql_query($sql);
						$sql = "update xoops_users set rpg_cur_hp = rpg_max_hp where rpg_cur_hp > rpg_max_hp and uid = " . $profiledata['uid'];
						$db->sql_query($sql);
					}
					else
					{
						$ret_msg = "遜....什麼也沒吸到!!";
					}
					break;
				case 113: // 歐巴吸血式
				case 114: // 美夕吸血式
					$hp_deduct = 0;
					if ( rand(1, 100) <= AttackPercent($row['rpg_magic'], $profiledata['rpg_magic']) )
					{
						$ret_msg = "";
						$sql = "select uname, rpg_cur_hp from xoops_users where left(right(rpg_eol_data, 6), 3) >= $next_x - ($next_act - 112) and
							left(right(rpg_eol_data, 6), 3) <= $next_x + ($next_act - 112) and right(rpg_eol_data, 3) >= $next_y - ($next_act - 112) and
							right(rpg_eol_data, 3) <= $next_y + ($next_act - 112) and rpg_eol_data is not null and right(rpg_eol_data, 6) <> '000086' and uid <> " . $profiledata['uid'];
						$result = $db->sql_query($sql);
						if ( $row2 = $db->sql_fetchrow($result) )
						{
							do
							{
								$hp_deduct += floor($row2['rpg_cur_hp'] * 0.05 * ($next_act - 111));
								$ret_msg .= " " . $row2['uname'] . "(" . floor($row2['rpg_cur_hp'] * 0.05 * ($next_act - 111)) . ")";
							}
							while ( $row2 = $db->sql_fetchrow($result) );
						}
						$sql = "select npc_id, npc_name, rpg_cur_hp from xoops_pbb_rpg_npc where now_x >= $next_x - ($next_act - 112) and
							now_x <= $next_x + ($next_act - 112) and now_y >= $next_y - ($next_act - 112) and
							now_y <= $next_y + ($next_act - 112) and (npc_name <> '" . $profiledata['uname'] . "' or npc_name is null)";
						$result = $db->sql_query($sql);
						if ( $row2 = $db->sql_fetchrow($result) )
						{
							do
							{
								$hp_deduct += floor($row2['rpg_cur_hp'] * 0.05 * ($next_act - 111));
								$ret_msg .= " " . ($row2['npc_name'] == "" ? "路人" . $row2['npc_id'] : $row2['npc_name']) . "(" . floor($row2['rpg_cur_hp'] * 0.05 * ($next_act - 111)) . ")";
							}
							while ( $row2 = $db->sql_fetchrow($result) );
						}
						$sql = "update xoops_users set rpg_cur_hp = floor(rpg_cur_hp * (1 - 0.05 * ($next_act - 111)))
							where left(right(rpg_eol_data, 6), 3) >= $next_x - ($next_act - 112) and
							left(right(rpg_eol_data, 6), 3) <= $next_x + ($next_act - 112) and right(rpg_eol_data, 3) >= $next_y - ($next_act - 112) and
							right(rpg_eol_data, 3) <= $next_y + ($next_act - 112) and rpg_eol_data is not null and right(rpg_eol_data, 6) <> '000086' and uid <> " . $profiledata['uid'];
						$db->sql_query($sql);
						$sql = "update xoops_pbb_rpg_npc set rpg_cur_hp = floor(rpg_cur_hp * (1 - 0.05 * ($next_act - 111)))
							where now_x >= $next_x - ($next_act - 112) and now_x <= $next_x + ($next_act - 112) and now_y >= $next_y - ($next_act - 112) and
							now_y <= $next_y + ($next_act - 112) and (npc_name <> '" . $profiledata['npc_name'] . "' or npc_name is null)";
						$db->sql_query($sql);
						$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp + $hp_deduct where uid = " . $profiledata['uid'];
						$db->sql_query($sql);
						$sql = "update xoops_users set rpg_cur_hp = rpg_max_hp where rpg_cur_hp > rpg_max_hp and uid = " . $profiledata['uid'];
						$db->sql_query($sql);
						$ret_msg = "吸血完成，增加 $hp_deduct 點 HP 。受害者:" . $ret_msg;
					}
					else
					{
						$ret_msg = "遜....什麼也沒吸到!!";
					}
					break;
				case 115: // 心眼
					$sql = "delete from xoops_pbb_rpg_npc where (abs(now_x - $now_x) + abs(now_y - $now_y)) <= $max_point and npc_name is not null";
					$db->sql_query($sql);
					$ret_msg = "心眼打開，分身消失";
					break;
				case 116: // 天眼
					$sql = "update xoops_users set rpg_eol_data = concat('0', right(rpg_eol_data, 11))
						where (abs(left(right(rpg_eol_data, 6), 3) - $now_x) + abs(right(rpg_eol_data, 3) - $now_y)) <= $max_point";
					$db->sql_query($sql);
					$ret_msg = "天眼打開，隱身忍者現身。";
					break;
				case 117: // 笙美夕
					$sql = "select min(own_id) as tima_id, count(*) as tima_count from xoops_pbb_rpg_owns where item_id = 105 and uid = " . $profiledata['uid'];
					$result = $db->sql_query($sql);
					if ( $row2 = $db->sql_fetchrow($result) )
					{
						if ( $row2['tima_count'] > 0 )
						{
							$sql = "delete from xoops_pbb_rpg_owns where own_id = " . $row2['tima_id'];
							$db->sql_query($sql);
							$sql = "update xoops_users set rpg_cur_hp = floor(rpg_max_hp * 0.2), rpg_cur_mp = floor(rpg_max_mp * 0.2)
								where rpg_eol_data is not null and right(rpg_eol_data, 6) <> '000086' and uid <> " . $profiledata['uid'];
							$db->sql_query($sql);
							$sql = "update xoops_pbb_rpg_npc set rpg_cur_hp = floor(rpg_max_hp * 0.2), rpg_cur_mp = floor(rpg_max_mp * 0.2)";
							$db->sql_query($sql);
							$ret_msg = "呼.... <b>奧義 - 笙美夕</b> 施展完畢!!";
						}
						else
						{
							$ret_msg = "您沒有 <b>tima的祝福(?)</b> 可以輔助施展這個技巧!!";
						}
					}
					else
					{
						$ret_msg = "您沒有 <b>tima的祝福(?)</b> 可以輔助施展這個技巧!!";
					}
					break;
				case 164: // 隱身術
					$now_act = $max_point;
					$ret_msg = "隱身完成，共可隱身 $now_act 回合。";
					break;
				case 165: // 影分身術
					GenerateSamePlayer($profiledata, $now_x, $now_y, $max_point);
					$ret_msg = "分身完成，共產生 $max_point 隻分身。";
					break;
				case 166: // 射擊
				case 172: // 弩弓
					$sql1 = '';
					$sql2 = '';
					$sql3 = '';
					if ( $next_x == $now_x ) // 直線射擊
					{
						if ($next_y > $now_y + 1)
						{
							$sql1 = "left(right(rpg_eol_data, 6), 3) = $now_x and right(rpg_eol_data, 3) > $now_y and right(rpg_eol_data, 3) < $next_y and right(rpg_eol_data, 6) <> '000086'";
							$sql2 = "now_x = $now_x and now_y > $now_y and now_y < $next_y and (now_x <> 0 or now_y <> 86)";
							$sql3 = "left(right(u.rpg_eol_data, 6), 3) = $now_x and right(u.rpg_eol_data, 3) > $now_y and right(u.rpg_eol_data, 3) < $next_y and right(u.rpg_eol_data, 6) <> '000086'";
						}
						else if ($next_y < $now_y - 1)
						{
							$sql1 = "left(right(rpg_eol_data, 6), 3) = $now_x and right(rpg_eol_data, 3) > $next_y and right(rpg_eol_data, 3) < $now_y and right(rpg_eol_data, 6) <> '000086'";
							$sql2 = "now_x = $now_x and now_y > $next_y and now_y < $now_y and (now_x <> 0 or now_y <> 86)";
							$sql3 = "left(right(u.rpg_eol_data, 6), 3) = $now_x and right(u.rpg_eol_data, 3) > $next_y and right(u.rpg_eol_data, 3) < $now_y and right(u.rpg_eol_data, 6) <> '000086'";
						}
						}
							else if ( $next_y == $now_y )
						{
						if ($next_x > $now_x + 1)
						{
							$sql1 = "right(rpg_eol_data, 3) = $now_y and left(right(rpg_eol_data, 6), 3) > $now_x and left(right(rpg_eol_data, 6), 3) < $next_x and right(rpg_eol_data, 6) <> '000086'";
							$sql2 = "now_y = $now_y and now_x > $now_x and now_x < $next_x and (now_x <> 0 or now_y <> 86)";
							$sql3 = "right(u.rpg_eol_data, 3) = $now_y and left(right(u.rpg_eol_data, 6), 3) > $now_x and left(right(u.rpg_eol_data, 6), 3) < $next_x and right(u.rpg_eol_data, 6) <> '000086'";
						}
						else if ($next_x < $now_x - 1)
						{
							$sql1 = "right(rpg_eol_data, 3) = $now_y and left(right(rpg_eol_data, 6), 3) > $next_x and left(right(rpg_eol_data, 6), 3) < $now_x and right(rpg_eol_data, 6) <> '000086'";
							$sql2 = "now_y = $now_y and now_x > $next_x and now_x < $now_x and (now_x <> 0 or now_y <> 86)";
							$sql3 = "right(u.rpg_eol_data, 3) = $now_y and left(right(u.rpg_eol_data, 6), 3) > $now_x and left(right(u.rpg_eol_data, 6), 3) < $next_x and right(u.rpg_eol_data, 6) <> '000086'";
						}
					}

					if ( $sql1 != '' )
					{
						$dead_cause = '';
						$hp_deduct = rand(10, 50) * $max_point;
						if ($next_act == 172) $hp_deduct *= 3;
						$sql = "select uname from xoops_users where $sql1";
						$result = $db->sql_query($sql);
						if ( $row2 = $db->sql_fetchrow($result) )
						{
							do
							{
								$dead_cause .= $row2['uname'] . ' ';
							}
							while ( $row2 = $db->sql_fetchrow($result) );
						}
						$sql = "select npc_id, npc_name from xoops_pbb_rpg_npc where $sql2";
						$result = $db->sql_query($sql);
						if ( $row2 = $db->sql_fetchrow($result) )
						{
							do
							{
								$dead_cause .= ($row2['npc_name'] == '' ? '路人' . $row2['npc_id'] : $row2['npc_name']) . ' ';
							}
							while ( $row2 = $db->sql_fetchrow($result) );
						}
						$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp - $hp_deduct where $sql1";
						$db->sql_query($sql);
						$sql = "update xoops_pbb_rpg_npc set rpg_cur_hp = rpg_cur_hp - $hp_deduct where $sql2";
						$db->sql_query($sql);
						if ( $dead_cause != '' )
						{
							$ret_msg .= " 流彈受傷點數: $hp_deduct 流彈受傷人員: " . $dead_cause;
							$ret_msg .= UserIsDead($sql1, $sql3, $profiledata['uid'], $profiledata['uname'], $profiledata['rpg_level'], ($next_act == 172 ? "弩" : "射" ) . "擊流彈($hp_deduct)");
							if ( $is_normal_npc && $is_npc )
							$ret_msg .= UserIsDead($sql2, "", $profiledata['uid'], $profiledata['uname'], $profiledata['rpg_level'], ($next_act == 172 ? "弩" : "射" ) . "擊流彈($hp_deduct)", true);
						}
					}
					unset($sql1);
					unset($sql2);
					unset($sql3);

					if ( rand(1, 100) > AttackPercent($profiledata['rpg_speed'] * 2, $row['rpg_speed']) )
					{
						$ret_msg = "太遜了，沒射中目標人物...." . $ret_msg;
					}
					else
					{
						if ( $profiledata['rpg_attack'] > $row['rpg_defense'] )
						{
							$hp_deduct = rand( ( ($profiledata['rpg_attack'] / 2 - $row['rpg_defense']) > 0 ? floor($profiledata['rpg_attack'] / 2 - $row['rpg_defense']) : 1) , $profiledata['rpg_attack'] - $row['rpg_defense']);
						}
						else
						{
							$hp_deduct = 10;
						}
						if ( $hp_deduct < $max_point * 10 )
							$hp_deduct = rand($max_point * 10, $max_point * 20);
						if ($next_act == 172) $hp_deduct *= 3;

						$sql = "update " . ($is_npc ? "phpbb_rpg_npc" : "xoops_users") .
							" set rpg_cur_hp = rpg_cur_hp - $hp_deduct where " .
							($is_npc ? "npc_id = " . $row['npc_id'] : "uid = " . $row['uid']);
						$db->sql_query($sql);

						$ret_msg = "射中 " . ($is_npc ? ($row['npc_name'] == '' ? "路人" . $row['npc_id'] : $row['npc_name']) : $row['uname']) . " 損傷 $hp_deduct 點 " . $ret_msg;
						$dead_cause = ($next_act == 172 ? "弩" : "射" ) . "擊($hp_deduct)";
					}
					// 反擊
					$row['rpg_cur_hp'] -= $hp_deduct;
					if ( $row['rpg_cur_hp'] > 0 )
					{
						if ( $is_npc )
						{
							if ( rand(1, 100) <= 40 )
							{
								if ( $row['rpg_attack'] - $profiledata['rpg_defense'] > 100 )
								{
									$hp_deduct = rand(100, $row['rpg_attack'] - $profiledata['rpg_defense']);
								}
								else
								{
									$hp_deduct = 100;
								}
								$hp_deduct = floor($hp_deduct / rand(2, 10));
								$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp - $hp_deduct where uid = " . $profiledata['uid'];
								$db->sql_query($sql);
								$ret_msg .= " 對手 " . ($row['npc_name'] == '' ? "路人" . $row['npc_id'] : $row['npc_name']) . " 反擊，損傷 $hp_deduct 點。";
								$am_i_dead = CheckMyDead($profiledata['uid']);
							}
						}
						else
						{
							$sql = "select max(position_order) as max_point from xoops_pbb_rpg_medals where position_class = 2 and uid = " . $row['uid'];
							$result = $db->sql_query($sql);
							if ( $row2 = $db->sql_fetchrow($result) )
							{
								if ( (abs($next_x - $now_x) + abs($next_y - $now_y) <= $row2['max_point'] + 1) && (rand(1, 100) <= 50) )
								{
									if ( $row['rpg_attack'] - $profiledata['rpg_defense'] > 10 * ($row2['max_point'] + 1) )
									{
										$hp_deduct = rand(10 * ($row2['max_point'] + 1), $row['rpg_attack'] - $profiledata['rpg_defense']);
									}
									else
									{
										$hp_deduct = 10 * ($row2['max_point'] + 1);
									}
									$hp_deduct = floor($hp_deduct / rand(2, 10));
									$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp - $hp_deduct where uid = " . $profiledata['uid'];
									$db->sql_query($sql);
									$ret_msg .= " 對手 " . $row['uname'] . " 反擊，損傷 $hp_deduct 點。";
									$am_i_dead = CheckMyDead($profiledata['uid']);
									UserIsDead("uid = " . $profiledata['uid'], "u.uid = " . $profiledata['uid'], $row['uid'], $row['uname'], $profiledata['rpg_level'], "射擊反擊($hp_deduct)");
								}
							}
						}
					}

					if ( $is_npc )
					{
						if ( $is_normal_npc )
						$ret_msg .= UserIsDead("npc_id = " . $row['npc_id'], '', $profiledata['uid'], '', 0, $dead_cause, true);
					}
					else
					{
						$ret_msg .= UserIsDead("uid = " . $row['uid'], "u.uid = " . $row['uid'], $profiledata['uid'], $profiledata['uname'], $profiledata['rpg_level'], $dead_cause);
					}
					break;
				case 167: // 疾風斬
				case 168: // 龍擊破
				case 169: // 霸皇神擊
				case 170: // 爆炸鐵金剛
					switch ( $next_act )
					{
						case 167:
							$hp_deduct = $profiledata['rpg_attack'] - $row['rpg_defense'];
							$ret_msg = $profiledata['uname'] . '使出 疾風斬 ';
							$dead_cause = '疾風斬';
							break;
						case 168:
							$hp_deduct = $profiledata['rpg_attack'] * 2 - $row['rpg_defense'];
							$ret_msg = $profiledata['uname'] . '使出 龍擊破 ';
							$dead_cause = '龍擊破';
							break;
						case 169:
							$hp_deduct = $profiledata['rpg_attack'] * 3 - $row['rpg_defense'];
							$ret_msg = $profiledata['uname'] . '使出 霸皇神擊 ';
							$dead_cause = '霸皇神擊';
							break;
						case 170:
							$hp_deduct = $profiledata['rpg_cur_hp'] - 1;
							$ret_msg = $profiledata['uname'] . '使出 爆炸鐵金剛(淚) ';
							$dead_cause = '爆炸鐵金剛(淚\)';
							$sql = "update xoops_users set rpg_cur_hp = 1 where uid = " . $profiledata['uid'];
							$db->sql_query($sql);
							$profiledata['rpg_cur_hp'] = 1;
							break;
					}
					if ( $hp_deduct < 0 ) $hp_deduct = 0;
					$sql = "update " . ($is_npc ? "phpbb_rpg_npc" : "xoops_users") .
						" set rpg_cur_hp = rpg_cur_hp - $hp_deduct where " . ($is_npc ? "npc_id = " . $row['npc_id'] : "uid = " . $row['uid']);
					$db->sql_query($sql);
					$dead_cause .= "($hp_deduct)";
					$ret_msg .= "擊中 " . ($is_npc ? ($row['npc_name'] == '' ? "路人" . $row['npc_id'] : $row['npc_name']) : $row['uname']) . " 損傷 $hp_deduct 點。";
					// 對手反擊
					// $row['rpg_cur_hp'] -= $hp_deduct;
					$ret_msg .= CounterAttack($am_i_dead, $profiledata, $row, $is_npc);
					if ( $is_npc )
					{
						if ( $is_normal_npc )
						$ret_msg .= UserIsDead("npc_id = " . $row['npc_id'], '', $profiledata['uid'], '', 0, $dead_cause, true);
					}
					else
					{
						$ret_msg .= UserIsDead("uid = " . $row['uid'], "u.uid = " . $row['uid'], $profiledata['uid'], $profiledata['uname'], $profiledata['rpg_level'], $dead_cause);
					}
					break;
				case 171: // 萬物歸零究級無限血火紅
				case 173: // 亂飛矢
					if ( $next_act == 171 )
					{
						$sql = "select min(own_id) as wing_id, count(*) as wing_count from xoops_pbb_rpg_owns where item_id = 104 and uid = " . $profiledata['uid'];
						$result = $db->sql_query($sql);
						if ( $row2 = $db->sql_fetchrow($result) )
						{
							if ( $row2['wing_count'] == 0 )
							{
								$ret_msg = "您沒有 <b>翼翼的羽毛</b> 可以輔助施展這個技巧!!";
							}
							else
							{
								$sql = "delete from xoops_pbb_rpg_owns where uid = " . $profiledata['uid'] . " and own_id = " . $row2['wing_id'];
								$db->sql_query($sql);
							}
						}
					}
					if ( $ret_msg == '' )
					{
						$dead_cause = '';
						$sql1 = "rpg_eol_data is not null and right(rpg_eol_data, 6) <> '000086' and rpg_level >= " . ($profiledata['rpg_level'] - 10) .
							" and (abs(left(right(rpg_eol_data, 6), 3) - $now_x) + abs(right(rpg_eol_data, 3) - $now_y)) <= 9 " .
							($next_act == 171 ? "" : " and uid <> " . $profiledata['uid']);
						$sql2 = "(abs(now_x - $now_x) + abs(now_y - $now_y)) <= 9 and rpg_level >= " . ($profiledata['rpg_level'] - 10);
						$sql3 = "u.rpg_eol_data is not null and right(u.rpg_eol_data, 6) <> '000086' and u.rpg_level >= " . ($profiledata['rpg_level'] - 10) .
							" and (abs(left(right(u.rpg_eol_data, 6), 3) - $now_x) + abs(right(u.rpg_eol_data, 3) - $now_y)) <= 9 " .
							($next_act == 171 ? "" : " and u.uid <> " . $profiledata['uid']);

						$sql = "delete from xoops_pbb_rpg_npc where npc_name is not null and npc_name <> '' and $sql2";
						$db->sql_query($sql);

						$sql = "select uname from xoops_users where $sql1";
						$result = $db->sql_query($sql);
						if ( $row2 = $db->sql_fetchrow($result) )
						{
							do
							{
								$dead_cause .= $row2['uname'] . ' ';
							}
							while ( $row2 = $db->sql_fetchrow($result) );
						}

						$sql = "select npc_id, npc_name from xoops_pbb_rpg_npc where $sql2";
						$result = $db->sql_query($sql);
						if ( $row2 = $db->sql_fetchrow($result) )
						{
							do
							{
								$dead_cause .= ($row2['npc_name'] == '' ? '路人' . $row2['npc_id'] : $row2['npc_name']) . ' ';
							}
							while ( $row2 = $db->sql_fetchrow($result) );
						}
						if ( $next_act == 171 )
						{
							$hp_deduct = floor($profiledata['rpg_max_mp'] / 3);
							$skill_name = "萬物歸零究級無限血火紅";
						}
						else
						{
							$hp_deduct = rand(floor($profiledata['rpg_attack'] / 10), floor($profiledata['rpg_attack'] / 2));
							$skill_name = "亂飛矢";
						}
						$sql = "update xoops_users set rpg_cur_hp = rpg_cur_hp - $hp_deduct where $sql1";
						$db->sql_query($sql);
						$sql = "update xoops_pbb_rpg_npc set rpg_cur_hp = rpg_cur_hp - $hp_deduct where $sql2";
						$db->sql_query($sql);
						$ret_msg = $profiledata['uname'] . "使出 $skill_name ，擊中: $dead_cause ，受傷點數: $hp_deduct ";
						$ret_msg .= UserIsDead($sql1, $sql3, $profiledata['uid'], $profiledata['uname'], $profiledata['rpg_level'], "$skill_name($hp_deduct)");
						$ret_msg .= UserIsDead($sql2, "", $profiledata['uid'], $profiledata['uname'], $profiledata['rpg_level'], "$skill_name($hp_deduct)", true);
						unset($skill_name);
					}
					break;
			}

			// 路人逃跑機制
			if ( $is_npc && $npc_id > 0 )
			{
				if (! $is_normal_npc )
				{
					$sql = "delete from xoops_pbb_rpg_npc where npc_id = $npc_id";
					$db->sql_query($sql);
				}
				else if ( rand(1, 100) <= 10 )
				{
					$scope = rand(1, $npc_scope);
					$sql = "select count(*) as scope_count from xoops_pbb_rpg_eol
						where (abs(map_x - $next_x) + abs(map_y - $next_y)) <= $scope and
						map_x <> $next_x and map_y <> $next_y and can_not_pass = 0";
					$result = $db->sql_query($sql);
					if ( $row2 = $db->sql_fetchrow($result) )
					{
						$rand_next = rand(0, $row2['scope_count'] - 1);
						if ( $rand_next >= 0 )
						{
							$sql = "select * from xoops_pbb_rpg_eol
								where (abs(map_x - $next_x) + abs(map_y - $next_y)) <= $scope and
								map_x <> $next_x and map_y <> $next_y and can_not_pass = 0 limit $rand_next, 1";
							$result = $db->sql_query($sql);
							if ( $row2 = $db->sql_fetchrow($result) )
							{
								$sql = "update xoops_pbb_rpg_npc set now_x = " . $row2['map_x'] . ", now_y = " .
									$row2['map_y'] . " where npc_id = $npc_id";
								$db->sql_query($sql);
							}
						}
					}
				}
			}
		}
	}

	unset($sql);
	unset($row);
	unset($row2);
	unset($result);
	unset($hp_deduct);
	unset($rand_next);
	unset($scope);
	unset($is_npc);
	unset($dead_cause);
	unset($npc_id);
	unset($npc_scope);
	unset($is_normal_npc);

	if ( $am_i_dead )
	{
		return 'xxxxx';
	}
	else
	{
		return $ret_msg;
	}
}
?>