

local g_szNameSence = {
	[ 2 ] = "苏州"
}


local g_szNameMission = "爱与生命"

local g_objPosMain = { nX , nY }

g_objPosMain[ 1 ] = { nX = 158 , nY = 114 }
g_objPosMain[ 2 ] = { nX = 162 , nY = 114 }
g_objPosMain[ 3 ] = { nX = 166 , nY = 114 }
g_objPosMain[ 4 ] = { nX = 170 , nY = 114 }
g_objPosMain[ 5 ] = { nX = 174 , nY = 114 }
g_objPosMain[ 6 ] = { nX = 178 , nY = 114 }
g_objPosMain[ 7 ] = { nX = 182 , nY = 114 }
g_objPosMain[ 8 ] = { nX = 186 , nY = 114 }
g_objPosMain[ 9 ] = { nX = 190 , nY = 114 }
g_objPosMain[ 10 ] = { nX = 194 , nY = 114 }
g_objPosMain[ 11 ] = { nX = 198 , nY = 114 }
g_objPosMain[ 12 ] = { nX = 202 , nY = 114 }
g_objPosMain[ 13 ] = { nX = 206 , nY = 114 }
g_objPosMain[ 14 ] = { nX = 210 , nY = 114 }
g_objPosMain[ 15 ] = { nX = 214 , nY = 114 }
g_objPosMain[ 16 ] = { nX = 218 , nY = 114 }
g_objPosMain[ 17 ] = { nX = 222 , nY = 114 }
g_objPosMain[ 18 ] = { nX = 226 , nY = 114 }
g_objPosMain[ 19 ] = { nX = 230 , nY = 114 }
g_objPosMain[ 20 ] = { nX = 234 , nY = 114 }
g_objPosMain[ 21 ] = { nX = 238 , nY = 114 }
g_objPosMain[ 22 ] = { nX = 242 , nY = 114 }
g_objPosMain[ 23 ] = { nX = 246 , nY = 114 }
g_objPosMain[ 24 ] = { nX = 250 , nY = 114 }
g_objPosMain[ 25 ] = { nX = 254 , nY = 114 }
g_objPosMain[ 26 ] = { nX = 258 , nY = 114 }
g_objPosMain[ 27 ] = { nX = 262 , nY = 114 }
g_objPosMain[ 28 ] = { nX = 266 , nY = 114 }
g_objPosMain[ 29 ] = { nX = 270 , nY = 114 }
g_objPosMain[ 30 ] = { nX = 274 , nY = 114 }
g_objPosMain[ 31 ] = { nX = 186 , nY = 125 }
g_objPosMain[ 32 ] = { nX = 186 , nY = 129 }
g_objPosMain[ 33 ] = { nX = 186 , nY = 133 }
g_objPosMain[ 34 ] = { nX = 186 , nY = 137 }
g_objPosMain[ 35 ] = { nX = 186 , nY = 141 }
g_objPosMain[ 36 ] = { nX = 186 , nY = 145 }
g_objPosMain[ 37 ] = { nX = 186 , nY = 149 }
g_objPosMain[ 38 ] = { nX = 186 , nY = 153 }
g_objPosMain[ 39 ] = { nX = 186 , nY = 157 }
g_objPosMain[ 40 ] = { nX = 186 , nY = 161 }
g_objPosMain[ 41 ] = { nX = 186 , nY = 165 }
g_objPosMain[ 42 ] = { nX = 186 , nY = 169 }
g_objPosMain[ 43 ] = { nX = 186 , nY = 173 }
g_objPosMain[ 44 ] = { nX = 186 , nY = 177 }
g_objPosMain[ 45 ] = { nX = 186 , nY = 181 }
g_objPosMain[ 46 ] = { nX = 186 , nY = 185 }
g_objPosMain[ 47 ] = { nX = 186 , nY = 189 }
g_objPosMain[ 48 ] = { nX = 186 , nY = 193 }
g_objPosMain[ 49 ] = { nX = 186 , nY = 197 }
g_objPosMain[ 50 ] = { nX = 186 , nY = 201 }
g_objPosMain[ 51 ] = { nX = 186 , nY = 205 }
g_objPosMain[ 52 ] = { nX = 186 , nY = 209 }
g_objPosMain[ 53 ] = { nX = 186 , nY = 213 }
g_objPosMain[ 54 ] = { nX = 186 , nY = 217 }
g_objPosMain[ 55 ] = { nX = 186 , nY = 221 }
g_objPosMain[ 56 ] = { nX = 186 , nY = 225 }
g_objPosMain[ 57 ] = { nX = 186 , nY = 229 }
g_objPosMain[ 58 ] = { nX = 186 , nY = 233 }
g_objPosMain[ 59 ] = { nX = 186 , nY = 237 }
g_objPosMain[ 60 ] = { nX = 186 , nY = 241 }
g_objPosMain[ 61 ] = { nX = 186 , nY = 245 }
g_objPosMain[ 62 ] = { nX = 186 , nY = 249 }
g_objPosMain[ 63 ] = { nX = 186 , nY = 253 }
g_objPosMain[ 64 ] = { nX = 186 , nY = 257 }
g_objPosMain[ 65 ] = { nX = 186 , nY = 261 }
g_objPosMain[ 66 ] = { nX = 186 , nY = 265 }

g_objPosMain[ 67 ] = { nX = 123 , nY = 78 }
g_objPosMain[ 68 ] = { nX = 123 , nY = 82 }
g_objPosMain[ 69 ] = { nX = 123 , nY = 86 }
g_objPosMain[ 70 ] = { nX = 123 , nY = 90 }
g_objPosMain[ 71 ] = { nX = 123 , nY = 94 }
g_objPosMain[ 72 ] = { nX = 123 , nY = 98 }
g_objPosMain[ 73 ] = { nX = 123 , nY = 102 }
g_objPosMain[ 74 ] = { nX = 123 , nY = 106 }
g_objPosMain[ 75 ] = { nX = 123 , nY = 110 }
g_objPosMain[ 76 ] = { nX = 123 , nY = 114 }
g_objPosMain[ 77 ] = { nX = 123 , nY = 118 }
g_objPosMain[ 78 ] = { nX = 123 , nY = 122 }
g_objPosMain[ 79 ] = { nX = 123 , nY = 126 }
g_objPosMain[ 80 ] = { nX = 123 , nY = 130 }
g_objPosMain[ 81 ] = { nX = 123 , nY = 134 }
g_objPosMain[ 82 ] = { nX = 123 , nY = 138 }


if G_bFinish1 == nil then
	G_bFinish1 = 0
end

function getTargetPos( szNameObj )

	local n = EnumObject(true)
	if n == 0 then
		return -1 , -1 , -1
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( nameObj ) == tostring( szNameObj ) then
			return idObj , xObj , yObj
		end
	end
	return -1 , -1 , -1
	
end

local g_objDiff = { nID }

function isDiffID( nID )
	local nNum = table.getn( g_objDiff ) 	
	if nNum <= 0 then
		return false
	end
	for i = 1 , nNum do
		if tonumber( g_objDiff[ i ].nID )  == tonumber( nID ) then
			return false
		end
	end
	return true
end

function getTargetDiff()

	local n = EnumObject(true)
	if n == 0 then
		return -1 , -1 , -1
	end

	local nNum = table.getn( g_objDiff ) 
	if nNum <= 0 then

		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( classObj ) ~= "PLAYER" then
				continue
			end
			if idObj ~= -1 and xObj ~= -1 and yObj ~= -1 then
				return idObj , xObj , yObj
			end
		end
	else
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( classObj ) ~= "PLAYER" then
				continue
			end
			if idObj ~= -1 and xObj ~= -1 and yObj ~= -1 then
				if isDiffID( idObj ) == false then
					continue
				end
				return idObj , xObj , yObj
			end
		end


	end

	return -1 , -1 , -1
	
end


function isMsgItem( szSub )
	local strType,_,_,_,szText = DataPool:GetNPCEventList_Item( 0 )	
	if strType == "text" then				
		if string.find(szText, szSub ) then
			return true
		end	
	end
	return false
end

function receiveMission()

	MoveToNPC( 226 , 326 , 0x01 , "左桐" )
	System:Sleep( 500 )
	QuestFrameOptionClicked( tostring( g_szNameMission ) , CMP_PART_MATCH)	
	System:Sleep( 500 )

	if isMsgItem( "你今天已经参与过本任务，请明天再来" ) == true then
		Player:EnableTeamFollow( false )
		QuestFrameMissionClose()
		return false
	end

	QuestFrameAcceptClicked()
	QuestFrameMissionClose()
	return true
end



function missionComplete()

	MoveToNPC( 226 , 326 , 0x01 , "左桐" )
	System:Sleep( 500 )
	QuestFrameOptionClicked( tostring( g_szNameMission ) , CMP_PART_MATCH)	
	System:Sleep( 500 )
	if isMsgItem( "背包道具栏剩余空位不足" ) == true then
		ShowMessage( "背包已满" )
		System:Sleep( 1000 )
	end
	QuestFrameMissionComplete()
	QuestFrameMissionClose()
end



function cancelMission()
	local idScript = getIdScriptMission( tostring( g_szNameMission ) )
	if idScript ~= -1 then
		MissionBlockProcess( idScript )
	else
		return
	end
end



function getIdScriptMission( szNameMission )

	local nIndex , idMission , idScript
	for i = 1 , 3 do
		nIndex , idMission , idScript = DataPool:GetPlayerMission( szNameMission )
		if nIndex ~= -1 then
			break
		else
			System:Sleep( 1500 )
		end

	end

	if nIndex == -1 then
		
		System:Sleep( 500 )
		return -1
	end


	return idScript

end


function isMissionComplete( szNameMiss )

	local nIndex, idMission, idScript = DataPool:GetPlayerMission( szNameMiss )
	local g_nParam = {}

	if nIndex ~= -1 then

		for i = 0 , 7 do
			nParam[i] = DataPool:GetPlayerMission_Variable( nIndex , i )
		end
		
		return tonumber( nParam[0] )
	end

	return -1
end

function offRide()
	if GetMount() ~= -1 then
		Player:EnableTeamFollow( false )
		Dismount()
	end
end


function addObjDiff( idObj )

	local nNum = table.getn( g_objDiff )

	local nIndex = 1

	if nNum > 0 then
		nIndex = nNum + 1
	end
	g_objDiff[ nIndex ] = { nID = idObj }

end


function getMsgCheck()
	local channel, data = GetDebugMessage()
	while channel == 1 do

		if string.find( data , "已收集快乐感悟" ) then
			if string.find( data , "已收集快乐感悟：9/9" ) then
				return 1
			else
				return 0
			end
		end

		if string.find( data , "距离对方过远，行为终止" ) then
			return 2
		end


		if string.find( data , "该玩家已有“爱与生命”状态，无法向其收集快乐感悟" ) then
			return 3
		end

		if string.find( data , "快乐感悟已收集完成，无法继续收集。请传达给夜西湖" ) then
			return 4
		end

		channel, data = GetDebugMessage()
	end
	return -1
end

function isMsgNear()
	local channel, data = GetDebugMessage()
	while channel == 1 do

		if string.find( data , "您的周围已经有放下的烛台，请在空旷一点的地方再尝试一下" ) then
			return true
		end

		channel, data = GetDebugMessage()
	end
	return false
end


function mySleep( nSleep )
	local nCount = 1
	while true do
		if nCount > nSleep then
			break
		end
		nCount = nCount + 1
		System:Sleep( 1000 )
	end
end

function exeMission()

	MoveTo( 189 , 275 , 0x01 )

	while true do

		if getMsgCheck() == 1 then
			return true
		end

		local idObj , xObj , yObj = getTargetDiff()
		if idObj ~= -1 then
			addObjDiff( idObj )
			MoveTo( xObj , yObj , -1 )
			offRide()
			LockTarget( idObj )
			System:Sleep( 500 )

			while true do

				UseItemById_Target( 0x1D77F7D , idObj )

				local nResult = getMsgCheck()
				if nResult == 1 or nResult == 4 then
					return true
				elseif nResult == 0 or nResult == 2 or nResult == 3 then
					break
				end
				System:Sleep( 1000 )
			end
		end 
	
	end
end


local g_objCoordinate = { nX , nY , nAngle }

function getRadian( nAngle )
	local nResult = ( nAngle * math.pi ) / 180
	return nResult
end

function setPointCircle( x , y , nAngle , nR )
	local nCount = 360 / nAngle
	for i = 1 , nCount do
		local xResult = x + nR * math.cos( getRadian( nAngle*i ) )
		local yResult = y + nR * math.sin( getRadian( nAngle*i ) )

		

		g_objCoordinate[ i ] = { nX = xResult , nY = yResult , nAngle = nAngle*i }
	end
end

function useItemPos()

	for i = 1 , table.getn( g_objPosMain ) do
		if g_objPosMain[ i ].nX == nil or g_objPosMain[ i ].nY == nil then
			continue
		end

		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( tonumber( g_objPosMain[ i ].nX ) , tonumber( g_objPosMain[ i ].nY ) , 45 , 2 )

		for j = 1 , 8 do
			MoveTo( tonumber( g_objCoordinate[ j ].nX ) , tonumber( g_objCoordinate[ j ].nY ) , -1 )
			while true do

				if isMsgNear() == true then
					break
				end
				if DataPool:UserBag_CountItemByIDTable( 0x1D77F7A ) <= 0 then
					return true
				end
				offRide()
				UseItemById( 0x1D77F7A )
				System:Sleep( 1000 )
			end			
		end
	end

	return false
end

function exeStep2()

	MoveTo( 154 , 108 , 0xa4 , "风间雅" )
	while true do
		if DataPool:UserBag_CountItemByIDTable( 0x1D77F7A ) > 0 then
			break
		end

		MoveToNPC( 154 , 108 , 0xa4 , "风间雅" )
		System:Sleep( 500 )
		QuestFrameOptionClicked( "爱与生命" , CMP_PART_MATCH)
		QuestFrameMissionComplete()
		QuestFrameMissionClose()
		offRide()
		UseItemById( 0x1D77F7A )
		System:Sleep( 100 )
	end
	return useItemPos()
end

function _SMain(...)


	if Player:GetData( "LEVEL" ) < 20 then
		ShowMessage( "等级>= 20 " )
		System:Sleep( 1000 )
		Player:EnableTeamFollow( false )
		return
	end

	LogChatMsg( 0 )
	LogChatMsg( 1 )



	while true do

		if GetQuestTimesInfo( "爱与生命" , 10000 ) == 0 then 
			Player:EnableTeamFollow( false )
			ShowMessage( "今天的任务已经完成了！" )
			System:UpdateRecord( "爱与生命" )
			System:Sleep( 1000 )
			return
		end
		
		local nPerform = isMissionComplete( tostring( g_szNameMission ) )

			if nPerform == -1 then

				if DataPool:UserBag_CountItemByIDTable( 0x1D77F7A ) > 0 then
					if exeStep2() == false then
						ShowMessage( "这里没有位置， 请您手动使用物品！" )
						System:Sleep( 1000 )
						ShowMessage( "这里没有位置， 请您手动使用物品！" )
						System:Sleep( 1000 )
						return
					else
						ShowMessage( "今天的任务已经完成！" )
						System:Sleep( 1000 )
						System:UpdateRecord( tostring( g_szNameMission ) )
						return
					end
					continue
				end
			
				if receiveMission() == false then
					ShowMessage( "今天的任务已经完成！" )
					System:Sleep( 1000 )
					System:UpdateRecord( tostring( g_szNameMission ) )
					return

				end

			elseif nPerform == 1 then

				if DataPool:UserBag_CountItemByIDTable( 0x2626bf9 ) >= 9 then

					if exeStep2() == false then
						ShowMessage( "这里没有位置， 请您手动使用物品！" )
						System:Sleep( 1000 )
						ShowMessage( "这里没有位置， 请您手动使用物品！" )
						System:Sleep( 1000 )
						return
					else
						ShowMessage( "今天的任务已经完成！" )
						System:Sleep( 1000 )
						System:UpdateRecord( tostring( g_szNameMission ) )
						return
					end

					continue
				end
				missionComplete()

			elseif nPerform == 2 then
				cancelMission()
				continue
			elseif nPerform == 0 then
				if G_bFinish1 == 0 then
					if exeMission() == true then
						G_bFinish1 = 1
					end
				else
					exeStep2()
				end
			end
	end

	LogChatMsg( -1 )
end

_SMain()
