ShowMessage("ֻ뽽汾 $Rev: 100 $")

--[[
[author]
descript=ֲʱһֱ 3 , Ϊ3. ֻ뽽ͬʱʱ,Ƚ!
perday_max=1
cmd0str=ֻ(ĬϺѺͶ)
cmd0={zh=1,jh=1,jh_hy=1}
cmd1str=ֻֻ
cmd1={zh=1,jh=0,jh_hy=0}
cmd2str=ֻ(ĬϺѺͶ)
cmd2={zh=0,jh=1,jh_hy=1}
cmd3str=ֻ()
cmd3={zh=0,jh=1,jh_hy=0}
cmd_default=0
]]--
script_name="ֻ뽽"

local cmd={zh=1,jh=1,jh_hy=1}
local chk_nZhongzi = 0
local chk_nHuafei = 0
local gMap = 2

local pos = {
[0]={x=0,y=0,chktime=0,chkother=0},
[1]={x=0,y=0,chktime=0,chkother=0},
[2]={x=0,y=0,chktime=0,chkother=0}
}

local ObjectList={}

function UpdatePos()

	pos[1].x,pos[1].y = Player:GetPos()

	pos[0].x = pos[1].x
	pos[0].y = pos[1].y - 3
	pos[2].x = pos[1].x
	pos[2].y = pos[1].y + 3

end

function UpdateBagData()
	chk_nHuafei = DataPool:UserBag_CountItemByName("")
	chk_nZhongzi = 	DataPool:UserBag_CountItemByName("ʻ")
end

function GetFlowerLevel( name )
	local nFlower = 0 
	if name == "ʻ1" then
		nFlower = 1
	elseif name == "ʻ2" then
		nFlower = 2
	elseif name == "ʻ3" then
		nFlower = 3
	elseif name == "ʻ" then
		nFlower = 4
	end
	return nFlower
end

function UpdateObjectData()

	--ػid
	--˳ ԼѳĻ > ԼδĻ > δĻ 	
	local selectID = INVALID_ID
	local selectMyCount = 0 
	local selectDis = 999999

	local myName = Player:GetName() 
	local myX,myY = Player:GetPos()

	--Ч
	for k,v in pairs(ObjectList) do
		v.del = true
	end

	pos[0].chkother = 0 
	pos[1].chkother = 0 
	pos[2].chkother = 0 

	local nMax = EnumObject(true)

	for i = 0 , nMax do 
		local id,type,name,nX,nY,title = EnumObject(false,i)
		--DbgPrintf("%s %s" , name , title)
		if type == "NPC" then
			local nFlower = GetFlowerLevel( name )
			if nFlower == 0 then
				continue
			end

			if GetDist(nX,nY,pos[0].x,pos[0].y) <= 2 then
				pos[0].chkother = pos[0].chkother + 1 
			end


			if GetDist(nX,nY,pos[1].x,pos[1].y) <= 2 then
				pos[1].chkother = pos[1].chkother + 1 
			end

			if GetDist(nX,nY,pos[2].x,pos[2].y) <= 2 then
				pos[2].chkother = pos[2].chkother + 1 
			end

			local playerName = ""
			if nFlower == 4 then
				local _,_,tName = string.find( title, "(.+)#{SDJZH_33}")
				playerName =  tName 
			else
				local _,_,tName = string.find( title, "(.+)#{SDJZH_32}")
				--DbgPrintf("Player name:%s , title :%s" , tostring(tName) , tostring(title))
				playerName =  tName 		
			end

			if playerName == nil or playerName == "" then
				continue 
			end

			local idFlower = nX * 10000 + nY 
			local isUpdate = false
			local isMine = false
			if playerName == myName then
				isUpdate = true
				isMine = true
				selectMyCount = selectMyCount + 1
			else
				local tDis = GetDist(nX,nY,pos[1].x,pos[1].y)
				if cmd.jh == 1 and tDis < 12 then
					if cmd.jh_hy == 1 then
						local nTeam = DataPool:GetFriendByName(playerName)
						if nTeam > 0 and nTeam < 5 then
							isUpdate = true
						elseif GetTeamMemberIndex(playerName) ~= -1 then
							isUpdate = true 
						end
					else
						isUpdate = true
					end
				end
			end	

			if isUpdate == true then
				if ObjectList[idFlower] == nil then
					ObjectList[idFlower] = {}
					ObjectList[idFlower].jh = false
				end

				ObjectList[idFlower].id = id 
				ObjectList[idFlower].x = nX 
				ObjectList[idFlower].y = nY 
				ObjectList[idFlower].lv = nFlower
				ObjectList[idFlower].self = isMine
				ObjectList[idFlower].del = false

				if nFlower == 1 or nFlower == 4 then
					ObjectList[idFlower].jh = false
				end

				--޸ķֵ
				if ObjectList[idFlower].jh == false then
					if ObjectList[idFlower].self == true then
						local newDis = GetDistSq(nX,nY,myX,myY)
						if ObjectList[selectID] == nil then
							selectID = idFlower
							selectDis = newDis
						else
							if nFlower == 4 then
								if ObjectList[selectID].lv == 4 then
									if newDis < selectDis then
										selectID = idFlower
										selectDis = newDis
									end
								else
									selectID = idFlower
									selectDis = newDis
								end
							else
								if ObjectList[selectID].lv ~= 4 then
									if newDis < selectDis then
										selectID = idFlower
										selectDis = newDis
									end
								end
							end
						end
					elseif ObjectList[idFlower].lv > 0  and ObjectList[idFlower].lv < 4 then
						--δĻ
						local newDis = GetDistSq(nX,nY,myX,myY)
						if ObjectList[selectID] == nil then
							selectID = idFlower
							selectDis = newDis
						else
							if ObjectList[selectID].self == false then
								if newDis < selectDis then
									selectID = idFlower
									selectDis = newDis
								end
							end
						end
					end
				end
			end
		end
	end

	for k,v in pairs(ObjectList) do
		if v.del == true then
			ObjectList[k] = nil
		end
	end

	return selectID , selectMyCount
end


function GetPlantPos()

	local iResult = -1

	for i=0,2 do

		if pos[i].x == 0 and pos[i].y == 0 then
			--DbgPrintf("ʧЧ")
			continue 
		end

		local now = System:timeGetTime()
		if pos[i].chktime ~= 0 and pos[i].chktime > now then
			--DbgPrintf("ȴʱ")
			continue
		end

		if pos[i].chkother > 0 then
			--DbgPrintf("Χж")
			continue
		end
		
		local nID = pos[i].x * 10000 + pos[i].y 
		if ObjectList[nID] ~= nil then
			--DbgPrintf("Ѵ")
			continue
		end

		iResult = i 
		break
	end

	return iResult
end

function _SMain(...)

	if _CommandLine ~= nil then 
		cmd = _CommandLine
	end

	UpdatePos()

	local nWait = 0 
	
	while true do
		
		UpdateBagData()

		local selectID,myFlowerCount = UpdateObjectData()
		selectID = tonumber(selectID)
		myFlowerCount = tonumber(myFlowerCount)

		DbgPrintf("ֻ : %d,%d , : %d,%d" , selectID,myFlowerCount , chk_nZhongzi, chk_nHuafei )

		if ObjectList[selectID] ~= nil then
			--ѡıȻδĻԼѳĻ
			--DbgPrintf("ѡĻ %d , ȼ %d" , selectID , ObjectList[selectID].lv )
			if ObjectList[selectID].lv < 4 and chk_nHuafei > 0 then
				MoveTo( ObjectList[selectID].x , ObjectList[selectID].y , 2 , "" ) 
				Dismount()
				local nowHuafei = chk_nHuafei
				System:Sleep(1000)
				UseItem_Target("" ,ObjectList[selectID].id)
				System:Sleep(1000)
				UpdateBagData()

				local isNext,szMsg = GetDebugMessage()
				while isNext == 1 do
					if string.find(szMsg ,"Ѿʻʩ") then
						ObjectList[selectID].jh = true 
					end
					isNext,szMsg = GetDebugMessage()
				end
				local tCount = chk_nHuafei + 1
				if ObjectList[selectID].jh == false and nowHuafei == tCount then
					
					ObjectList[selectID].jh = true 
				end
				nWait = 0 
				continue
			elseif ObjectList[selectID].lv == 4 then
				MoveTo( ObjectList[selectID].x , ObjectList[selectID].y , 2 , "" ) 
				Dismount()
				System:Sleep(1000)
				Player_UseSkill_Collect(3 ,ObjectList[selectID].id)
				System:Sleep(1000)
				nWait = 0 
				continue
			end
		end

		--ûҪĻѳĻ,ѡֻ
		if cmd.zh == 1 and chk_nZhongzi > 0 then
			local selectPos = GetPlantPos()
			if selectPos ~= -1 then
				MoveTo( pos[selectPos].x , pos[selectPos].y , gMap , "" , 2 , false )
				Dismount()
				System:Sleep(1000)
				UseItem("ʻ")

				System:Sleep(1000)

				local isNext,szMsg = GetDebugMessage()
				while isNext == 1 do
					if string.find(szMsg ,"ǰ򲻿ֲ") then
						pos[selectPos].x = 0 
						pos[selectPos].y = 0 
					elseif string.find(szMsg,"ǰѾûֲռ") then
						local now = System:timeGetTime()
						pos[selectPos].chktime = now + 60*1
					end
					isNext,szMsg = GetDebugMessage()
				end

				nWait = 0 
				continue
			end
		end

		--ûջͽ
		if myFlowerCount == 0 then
			if cmd.jh == 1 and cmd.zh == 1 then
				if chk_nZhongzi == 0 and chk_nHuafei == 0 then
					ShowMessage("ʺѾ.")
					return 
				end
			elseif cmd.jh == 1 then
				if chk_nHuafei == 0 then
					ShowMessage("Ѿ.")
					return 
				end
			elseif cmd.zh == 1 then
				if chk_nZhongzi == 0 then
					ShowMessage("Ѿ.")
					return 
				else
					local nCheckPos = 0
					for i=0,2 do
						if pos[i].x == 0 and pos[i].y == 0 then
							nCheckPos = nCheckPos + 1
						end
					end

					if nCheckPos >= 3 then
						ShowMessage("ǰλ޷ֻ,λ.")
						return
					end
				end
			else
				ShowMessage("д.")
				return
			end
		end

		System:Sleep(1000)

		nWait = nWait + 1

		if nWait > 5 then
			MoveTo(pos[1].x , pos[1].y , 2 , "" )
			nWait = 0 
		end
	end

end

_SMain()