ShowMessage("ÿհű $Rev: 1 $")

--[[
[author]
class=3
perday_max=1
]]--
script_name=""

local g_NpcName = ""			-- NPC
local g_Npc_X = 134
local g_Npc_Y = 49
local g_LimitPlayerLevel = 40			-- Ƶȼ
local g_MissionName = "ÿհ"	-- 
local g_MaxMissionCount = 3
local g_MissionParam_FinishedFlag = 0
local g_MissionType = {			-- 
[ 1 ] = { misName = "ӢЯ־˻", buff = "", misFun = InteractiveMission1 },
[ 2 ] = { misName = "Э׽", buff = "ҹоװ", misFun = InteractiveMission2 },
[ 3 ] = { misName = "ȴ۲", buff = "", misFun = InteractiveMission3 },
[ 4 ] = { misName = "ɳ԰溦", buff = "԰ũ", misFun = InteractiveMission4 },
[ 5 ] = { misName = "֧Ԯ", buff = "ӳ", misFun = InteractiveMission5 },
}

local g_nParam = {}
local g_CurrentMissionID = 0 

-------------------------------------------------------------------------------------------------------------------
local x610013_g_RandomPosition = {								-- ˵İˢµ
	{83,43}, {84,45}, {88,41}, {88,48}, {92,46}, 			-- Group1
	{43,62},{45,66}, {42,71}, {47,68},{47,60},  			-- Group2
	{79,95}, {74,100},{81,103}, {84,105}, {88,96},  		-- Group3
	{81,140}, {80,145}, {77,144}, {82,143}, {84,149},  		-- Group4
	{120,139}, {122,143}, {115,146}, {120,148}, {118,143},		-- Group5
	{140,128}, {144,130}, {146,133}, {141,134}, {138,140}, 		-- Group6
}

function InteractiveMission1()

	local Index = g_nParam[1] + g_nParam[2]
	MoveTo(x610013_g_RandomPosition[Index][1], x610013_g_RandomPosition[Index][2], -1, "", 2, false)

	local dwFlags = CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_FREE|CHA_OCCUPANT_FREE|CHA_OCCUPANT_MYSELF|CHA_OCCUPANT_TEAM|CHA_STATE_ALIVE

	local Id = LockNearestTarget("˵İ", dwFlags, "")

	if Id ~= INVALID_ID then
		while Character:IsPresent(Id) do
			if Character:GetData(Id, "HP_PERCENT") == 100 then
				break
			end
			Player_UseSkill_Collect(3092, Id)
			System:Sleep(500)
		end
	end
end

-------------------------------------------------------------------------------------------------------------------
x610014_g_EscapedThiefPosition = {					-- ܵķ·
{151,64, 91,69, 92,82, 98,86, 100,103, 100,118, 85,121, 70,123, 76,136, 68,147, 86,156},
{125,66, 66,67, 57,61, 55,99, 79,100, 100,104, 99,124, 113,131, 113,145, 84,150},
{142,58, 152,70, 145,100, 115,101, 100,104, 99,120, 59,121, 39,122, 38,141, 82,152},
{129,53, 129,57, 57,56, 56,85, 58,95, 99,103, 99,127, 84,138, 86,158},
{134,57, 74,57, 66,63, 79,69, 99,97, 113,121, 118,134, 79,142, 77,152},
}

local g_nMiddleIndex = 0

function KillFeizei( fX , fY , nScene )

	local idObj = g_nParam[3]

	if DataPool:IsMissionCanCommit(g_CurrentMissionID) then
		return false
	end

	if Character:IsPresent(idObj) then
		local x, y = Character:GetPos(idObj)
		local xMy, yMy = Player:GetPos()
		local dist = GetDist(xMy, yMy, x, y)
		if dist <= 3 then
			Player_UseSkill_Collect(3112, idObj)
			Player_UseSkill_Collect(3093, idObj)
		else 
			if x ~= fX or y ~= fY then
				return false
			end
		end
	end

	return true
end
	

function InteractiveMission2()

	local idObj = g_nParam[3]
	local xMy, yMy = Player:GetPos()

	DbgPrintf("ID %d", tonumber(idObj))

	if Character:IsPresent(idObj) then

		DbgPrintf("ҵ OBJ!")

		local x, y = Character:GetPos(idObj)
		local dist = GetDist(xMy, yMy, x, y)

		DbgPrintf(" %d %d,%d", dist, x, y)

		MoveTo(x, y, -1, "", 1, false , KillFeizei )

	else
		local x , y = DataPool:SearchFZ() 
		DbgPrintf(" %d,%d" , tonumber(x) , tonumber(y) )
		MoveTo( x , y , -1, "", 1, false , KillFeizei )
	end
end

-------------------------------------------------------------------------------------------------------------------
x610015_g_Skill = {
3094, 3095, 3096, 3097, 3098, 3099,
}

x610015_g_Position = {
{x = 169, y = 135},
{x = 170, y = 137},
{x = 169, y = 139},
{x = 170, y = 141},
{x = 169, y = 143},
{x = 168, y = 144},
}

function InteractiveMission3()
	local nX , nY = Player:GetPos()
	if IsDestination( 165 , 141 , -1 , 2 ) == false then
		MoveTo(165, 141, -1, "", 1, false)
	end
	
	local idObj, xObj, yObj = FindCharacterByName("")

	if idObj ~= INVALID_ID then
		for n = 1, 5 do
			if x610015_g_Position[n].x == xObj and x610015_g_Position[n].y == yObj then
				DbgPrintf("@ %d", x610015_g_Skill[n])
				Player_UseSkill_Collect(x610015_g_Skill[n],"")
			end
		end
	end
	
end

-------------------------------------------------------------------------------------------------------------------
x610016_g_RandomPosition = {
	{130,148}, {126,151}, {123,152},
	{122,150}, {125,146}, {132,153},
	{134,151}, {136,149}, {139,146},
	{133,144}, {130,142}, {125,143},
	{116,143}, {121,136}, {140,141},
}

function InteractiveMission4()

	MoveTo(x610016_g_RandomPosition[1][1], x610016_g_RandomPosition[1][2], -1, "", 20, false)

	local dwFlags = CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_FREE|CHA_OCCUPANT_FREE|CHA_OCCUPANT_MYSELF|CHA_OCCUPANT_TEAM|CHA_STATE_ALIVE

	--ѡȡһĿ
	local Id = LockNearestTarget("԰", dwFlags, CMP_FULL_MATCH , 1)

	if Id ~= INVALID_ID then

		local x, y = Character:GetPos(Id)
		MoveTo(x, y, -1, "", 2 , false)
	--[[	
		while Character:IsPresent(Id) do
			if Character:GetData(Id, "DIE") then
				DbgPrintf("Target Is Die")
				break
			end

			DbgPrintf("ʹó漼 %d" ,tonumber(Id))
	]]--
		Player_UseSkill_Collect(3104, Id)

		System:Sleep(200)
--		end

	end
end

-------------------------------------------------------------------------------------------------------------------
x610017_g_Position = {
{x = 59, y = 131, name = "ˮ"},
{x = 114, y = 97, name = "ľİ"},
{x = 129, y = 49, name = ""},
{x = 43, y = 99, name = "ҩ"},
{x = 58, y = 51, name = ""},
}
local task = -1

function InteractiveMission5()
DbgPrintf("@5")

	DbgPrintf("@1 task %d", task)

	if task == -1 then
		-- ݶλ÷
		local nMemIndex = GetTeamMemberIndex(Player:GetName())

		if nMemIndex == 0 then
			if g_nParam[1] == 0 then
				task = 1
			end
		elseif nMemIndex == 1 then
			if g_nParam[2] == 0 then
				task = 2
			elseif g_nParam[3] == 0 then
				task = 3
			end
		elseif nMemIndex == 2 then
			if g_nParam[4] == 0 then
				task = 4
			elseif g_nParam[5] == 0 then
				task = 5
			end
		elseif mMemIndex == 3 then
			if g_nParam[3] == 0 then
				task = 3
			end
		elseif mMemIndex == 4 then
			if g_nParam[5] == 0 then
				task = 5
			end
		end

		-- 
		if task == -1 then
			local buff = {}
			local n = 1
			for i = 1, 5 do
				if g_nParam[i] == 0 then
					buff[n] = i
					n = n + 1
				end
			end

			if table.getn(buff) > 0 then

				math.randomseed(os.time())
				local idx = math.random(1, table.getn(buff))

				task = buff[idx]
			end
		end

	end

	DbgPrintf("@2 task %d", task)

	if g_nParam[task] == 1 then
		task = -1
		return
	end

	if task == -1 then
		return
	end

	MoveTo(x610017_g_Position[task].x, x610017_g_Position[task].y, -1, "", 1, false)

	local dwFlags = CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_FREE|CHA_OCCUPANT_FREE|CHA_OCCUPANT_MYSELF|CHA_OCCUPANT_TEAM|CHA_STATE_ALIVE

	local Id = LockNearestTarget(x610017_g_Position[task].name, dwFlags, "")

	if Id ~= INVALID_ID then
		Player_UseSkill_Collect(3105, Id)
	end
end

-------------------------------------------------------------------------------------------------------------------
local g_MissionFun = {
[ 1 ] = InteractiveMission1,
[ 2 ] = InteractiveMission2,
[ 3 ] = InteractiveMission3,
[ 4 ] = InteractiveMission4,
[ 5 ] = InteractiveMission5
}

function OnInteractiveMission()

	for i = 1, 5 do

		-- жǷȡ
		local nIndex, idMission, idScript = DataPool:GetPlayerMission(g_MissionType[i].misName)

		if nIndex ~= -1 then

			DbgPrintf("ѽȡ")

			-- ȡ
			for i = 0, 7 do
				g_nParam[i] = DataPool:GetPlayerMission_Variable(nIndex, i)
				DbgPrintf("[%d] = %d", i, g_nParam[i])
			end

			if g_MissionFun[i] == nil then

				ShowMessage("űûҵӦ")
				return false
			end

			if g_nParam[0] == 0 then

				if Player_GetBuff(g_MissionType[i].buff) == -1 then
					DbgPrintf("ȱ״̬,½ȡ") 
					Dismount()
					MoveToNPC(g_Npc_X, g_Npc_Y, -1, g_NpcName)
					QuestFrameOptionClicked("ȡÿհ", CMP_FULL_MATCH)

					return true
				end	

				DbgPrintf(">>>>>>>>>>>>>>")
				g_CurrentMissionID = idMission
				g_MissionFun[i]()

			elseif g_nParam[0] == 1 then
				DbgPrintf("") 
				MoveToNPC(g_Npc_X, g_Npc_Y, -1, g_NpcName)
				DbgPrintf("Իnpc 2")
				QuestFrameOptionClicked(g_MissionName, CMP_FULL_MATCH)
				QuestFrameMissionComplete()	-- 

			elseif g_nParam[0] == 2 then

				QuestFrameMissionAbnegate(idScript)

			end

			return true
		end
	end

	DbgPrintf("δȡ")

	MoveTo(g_Npc_X, g_Npc_Y, -1, "", 1)

	Dismount()

	-- ӳȡ
	--if DataPool:IsTeamLeader() then
	DbgPrintf("Իnpc")

	MoveToNPC(g_Npc_X, g_Npc_Y, -1, g_NpcName)

	DbgPrintf("ѡ %s", g_MissionName)
	QuestFrameOptionClicked(g_MissionName, CMP_FULL_MATCH)
	QuestFrameAcceptClicked()
	System:Sleep(1500)

	local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)

	if strType == "text" then
		if string.find(szText, "ȡ") then
			System:Sleep(1000)
		end
	end

	local isNext,szMsg = GetDebugMessage()
	while isNext == 1 do
		if string.find(szMsg, "Ѿɹ3") then
			System:UpdateRecord( script_name )
			return false
		end
		isNext,szMsg = GetDebugMessage()
	end
	--end

	return true
end

function _SMain(...)

	if Player:GetData("LEVEL") < 40 then
		ShowMessage("ȼ40")
		return
	end

	while true do

		-- ж϶
		if DataPool:GetTeamMemCount() < g_MaxMissionCount then 
			ShowMessage("%dˣ", g_MaxMissionCount)
			break
		end

		-- ɳ
		if CityToGuild() == false then
			ShowMessage("ûаɳУ")
			break 
		end

		if OnInteractiveMission() == false then
			break
		end

		System:Sleep(200)
	end

	GuildToCity(0)

end

_SMain()


