ShowMessage("每日帮会事务任务脚本 $Rev: 1 $")

--[[
[author]
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()


