ShowMessage("公会建设任务脚本 $Rev: 187 $")

--[[
[author]
class=3
]]--

local NEED_ACTIVEPOINT = 10

local g_szMission = "建设任务"
local g_nParam = {}
local g_nIndex = 0
local g_idMission = 0
local g_idScript = 0
local g_dwNextTime = 0

local g_NpcInfo = {
	[0] = {szNpcName="郑无名", fX=99, fY=54},
	[1] = {szNpcName="杜子庞", fX=45, fY=73},
	[2] = {szNpcName="制药操作台", fX=48, fY=98},
	[3] = {szNpcName="苟写", fX=73, fY=93},
	[4] = {szNpcName="余满仓", fX=165, fY=141},
}

function OnMission_600036()

	local nNpcID = 0
	
	if g_nParam[2] == 5 then
		nNpcID = tonumber(g_nParam[5])
	else
		nNpcID = tonumber(g_nParam[3])
	end

	local szNpcName, fX, fY, nScene = DataBase:GetMissionNPCInfo(nNpcID)

	if fX == 0 then
		ShowMessage("建设任务：无法取得NPC信息。")
		return
	end

	GuildToCity(nScene)

	if g_nParam[2] == 5 then

		MoveTo(fX, fY, nScene)
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("目标", "添加", szNpcName)
		AI:SetParameter("地图", nScene)
		AI:Start(false)
		while DataPool:IsMissionCanCommit(g_idMission) == false do
			System:Sleep(200)
		end
		AI:Stop()

	elseif g_nParam[2] == 3 then

		local szSceneName = GetActiveSceneName()

		if szSceneName ~= "市集" then

			--CityToGuild()
			MoveToNPC(fX, fY, nScene, szNpcName)
			QuestFrameOptionClicked(g_szMission, CMP_FULL_MATCH)	-- 选择选项
			QuestFrameOptionClicked("进入市集", CMP_FULL_MATCH)	-- 选择选项

		else

			AI:SetAI("杀怪")
			AI:SetParameter("初始化")

			AI:SetParameter("地图", -1)
			AI:SetParameter("坐标", "添加", 43, 41)
			AI:SetParameter("坐标", "添加", 30, 42)
			AI:SetParameter("坐标", "添加", 13, 51)
			AI:SetParameter("坐标", "添加", 20, 30)
			AI:SetParameter("坐标", "添加", 14, 15)
			AI:SetParameter("坐标", "添加", 30, 20)
			AI:SetParameter("坐标", "添加", 49, 12)
			AI:SetParameter("坐标", "添加", 46, 32)

			AI:Start(false)
			while DataPool:GetPlayerMission_Variable(g_nIndex, 2) == 3 do
				System:Sleep(200)
			end
			AI:Stop()

			while GetActiveSceneName() == "市集" do
				System:Sleep(1000)
			end
		end
	else
		MoveToNPC(fX, fY, nScene, szNpcName)
	end


end

function OnMission_600038()

	local nNpcID = 0
	
	if g_nParam[2] == 1 then
		nNpcID = tonumber(g_nParam[3])
	elseif g_nParam[2] == 2 then
		nNpcID = tonumber(g_nParam[5])
	end

	local szNpcName, fX, fY, nScene = DataBase:GetMissionNPCInfo(nNpcID)

	if fX == 0 then
		ShowMessage("建设任务：无法取得NPC信息。")
		return
	end

	GuildToCity(nScene)

	if g_nParam[2] == 1 then

		MoveToNPC(fX, fY, nScene, szNpcName)

	elseif g_nParam[2] == 2 then

		MoveTo(fX, fY, nScene)
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("目标", "添加", szNpcName)
		AI:SetParameter("地图", nScene)
		AI:Start(false)
		while DataPool:GetPlayerMission_Variable(g_nIndex, 2) == 2 do
			System:Sleep(200)
		end
		AI:Stop()
	end
end

function OnMission_600039()

	local szSceneName = GetActiveSceneName()

	if szSceneName ~= "工地" then

		CityToGuild()
		MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
		QuestFrameOptionClicked(g_szMission, CMP_FULL_MATCH)	-- 选择选项
		QuestFrameOptionClicked("进入工地", CMP_FULL_MATCH)	-- 选择选项
	else
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")

		AI:SetParameter("地图", -1)
		AI:SetParameter("坐标", "添加", 30, 44)
		AI:SetParameter("坐标", "添加", 48, 32)
		AI:SetParameter("坐标", "添加", 32, 32)
		AI:SetParameter("坐标", "添加", 12, 32)
		AI:SetParameter("坐标", "添加", 12, 22)
		AI:SetParameter("坐标", "添加", 30, 15)
		AI:SetParameter("坐标", "添加", 30, 26)
		AI:SetParameter("坐标", "添加", 48, 22)

		AI:Start(false)
		while DataPool:IsMissionCanCommit(g_idMission) == false do
			System:Sleep(200)
		end
		AI:Stop()

		while GetActiveSceneName() == "工地" do
			System:Sleep(1000)
		end
	end
end

------------------------------------------------------------------------------------------------
function _SMain(...)

	while true do

		g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(g_szMission)

		if g_nIndex ~= -1 then

			DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", g_szMission, g_nIndex, g_idMission, g_idScript)

			-- 取得任务参数
			for i=0,7 do
				g_nParam[i] = DataPool:GetPlayerMission_Variable(g_nIndex, i)
				DbgPrintf("[%d]%d", i, g_nParam[i])
			end

			if DataPool:IsMissionCanCommit(g_idMission) then

				-- 完成任务
				DbgPrintf("任务已完成")
				
				CityToGuild()
				MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
				QuestFrameOptionClicked(g_szMission, CMP_PART_MATCH)	-- 选择选项
				QuestFrameMissionContinue()	-- 继续
				QuestFrameMissionComplete()	-- 完成
				
				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0);

				if strType == "text" then
					if string.find(szText, "已经全部完成") then
						ShowMessage("建设任务：已经全部完成")
						break	-- 退出循环，结束脚本
					end
				end

				QuestFrameMissionClose()
				
			else
				-- 已接任务，未完成
				DbgPrintf("任务进行中")

				if g_idScript == 600036 then
					OnMission_600036()
				elseif g_idScript == 600038 then
					OnMission_600038()
				elseif g_idScript == 600039 then
					OnMission_600039()
				else
					ShowMessage("建设任务：未知的任务脚本。")
					break
				end

			end


		else
			-------------------------------------------------------------------------------------
			-- 领取任务


			-- 进入帮派城市
			if CityToGuild() == false then
				ShowMessage("建设任务：没有帮派城市。")
				break 
			end
			
			MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)

			QuestFrameOptionClicked(g_szMission, CMP_FULL_MATCH)	-- 选择选项

			local bContinue = true
			local isNext,szMsg = GetDebugMessage()
			while isNext == 1 do
				if string.find(szMsg, "无法接取建设任务") then
					ShowMessage("建设任务：未开启建设任务。")
					bContinue = false
					break
				end
				isNext,szMsg = GetDebugMessage()
			end

			if bContinue == false then 
				break
			end

			QuestFrameAcceptClicked()

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

			if strType == "text" then
				if string.find(szText, "没有建设") then
					ShowMessage("建设任务：帮派没有建设任务。")
					break	-- 退出循环，结束脚本
				elseif string.find(szText, "交付完毕") then
					ShowMessage("建设任务：帮派建设任务已交付完毕，需待帮主或副帮主新发布。")
					break	-- 退出循环，结束脚本
				end
			end

			System:Sleep(1500)
			QuestFrameOptionClicked("传送", CMP_PART_MATCH)
			QuestFrameMissionClose()

		end

		System:Sleep(500)
	end

	GuildToCity(0)
end

_SMain()