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()