--[[ Ѩ: [][][][][Q][Ԩ][ɾ][][ƶ][][ɫ][]
ؼ:
˹  2Ϣ 2Ч
׼  2Ϣ 2Ч
  3Ч 1
  3Ч 1Χ
ʥ  2Ϣ 2Ч
ǧ  3Ϣ 1Ч
Ů  3Ϣ 1
  2 1Ч 1

ٴ غ ->  -> ׼ -> Ů ->  ̵
ûǰĿһֱѡboss
Ŀ10ڷŶ
˹ Ƴ׼ ֶ
--]]

--ѡ
addopt("", false)

--
local v = {}
v["¼Ϣ"] = true

--
local f = {}

--ѭ
function Main()
	--Զݼ1ҷҡ, עǿݼ趨Ŀݼ1ָİǼϵ1
	if keydown(1) then
		fcast("ҡֱ")
	end

	--ǧ
	if gettimer("ǧ") < 0.3 or casting("ǧ") then
		return
	end

	--ȴʼ
	if gettimer("") < 0.3 then
		return
	end

	if casting("ƶ") and castleft() < 0.13 then
		settimer("")
	end

	--
	if fight() and pet() and life() < 0.6 then
		fcast("ˮ", true)
	end

	--
	if getopt("") and tbuffstate("ɴ") then
		fcast("")
	end

	--ʼ
	v[""] = charinfo("")
	v["Ŀ"] = f["ȡĿ"]()
	v["ĿѪ"] = xlife(v["Ŀ"])
	v["boss"] = npc("ϵ:ж", "ǿ=6", "ѡ", "<30") ~= 0
	v["Ŀboss"] = rela("ж") and target("boss")
	local tSpeedXY, tSpeedZ = speed(tid())
	v["Ŀ꾲ֹ"] = rela("ж") and tSpeedXY <= 0 and tSpeedZ <= 0		--Ŀûƶ
	v[""] = buffsn("")
	v["ʱ"] = bufftime("")

	---------------------------------------------

	--ٺ
	if not pet("̵") then
		cast("̵")
	end

	--ɢ
	if pet("̵") then
		v["Ҫɢ"] = xparty(petid(), "û״̬:", "<40", "߿ɴ", "buffʱ:ԲЧ|ԪԲЧ|ԲЧ|ԲЧ>1")		--Χ40, ߿ɴ, ָЧ
		if v["Ҫɢ"] ~= 0  then
			if settar(v["Ҫɢ"]) then	--ֻڵǰĿ
				cast("")
			end
		end
	end

	--ƻ
	if fight() and v["Ŀboss"] and ttid() ~= 0 and ttid() ~= id() then	--Ŀboss ĿĿ Լ
		if xbufftime("ƻ", ttid(), id()) < 5 then
			xcast("ƻ", ttid())	--ĿĿ
		end
	end

	--ǧ
	if fight() then
		_, v["Ѫ"] = party("û״̬:", "<20", "߿ɴ", "ûؾ", "Ѫ<0.6")	--Լ20
		if qixue("Q") and pet("̵") then
			_, v["Ѫ"] = xparty(petid(), "û״̬:", "<20", "߿ɴ", "ûؾ", "Ѫ<0.6")	--20
		end
		
		if v["Ѫ"] >= 3 then
			if fcast("ǧ") then
				settimer("ǧ")
				return
			end
		end
	end

	--ʥ
	if fight() and v["ĿѪ"] < 0.6 then
		CastX("ʥ֯", true)
	end

	--Ů
	if fight() and v["boss"] then
		cast("Ů洲")
	end

	--ֲ
	if fight() and v["Ŀboss"] and v["Ŀ꾲ֹ"] then
		cast("")
	end

	--
	if v["ĿѪ"] < 0.85 then
		CastX("ǣ˿", true)
	end

	--Ŷ
	if fight() and v["Ŀboss"] and v["Ŀ꾲ֹ"] and dis() < 10 and gettimer("") > 0.5 then
		cast("ƶ")
	end

	--Զ
	if gettimer("") > 0.5 and nobuff("ʱ") and life() < 0.7 and cdleft(16) > 0.5 then
		interact("ƶ")
	end

	--
	if v["ĿѪ"] < 0.95 then
		if casting("") then
			v["NPC"] = npc("ϵ:Լ", "ģID:107819")
			if v["NPC"] ~= 0 and xxdis(v["NPC"], v["Ŀ"]) > range("", true) then	--ڶ, Ŀ겻跶Χ, ͷ
				if CastX("", true) then
					settimer("")
					return
				end
			end
		else
			if CastX("") then
				settimer("")
				return
			end
		end
	end

	--ս
	if casting("") and castleft() < 0.13 then
		settimer("")
	end
	if nofight() and gettimer("") > 0.5 then
		xcast("", party("״̬:", "<20", "߿ɴ"))
	end
end

-------------------------------------------------------------------------------

f["ȡĿ"] = function()
	local targetID = id()	--ĿΪԼ, ڶŶʱ party  0
	local partyID = party("û״̬:", "Լ", "<20", "߿ɴ", "ûؾ", "Ѫ")	--ȡѪٶ
	if partyID ~= 0 and partyID ~= targetID and xlife(partyID) < life() then	--ѪٶұԼѪ
		targetID = partyID	--ָΪĿ
	end
	return targetID
end

--¼Ϣ
function PrintInfo(s)
	local t = {}
	if s then t[#t+1] = s end
	t[#t+1] = "Ŀ:"..v["Ŀ"]
	t[#t+1] = "ĿѪ:"..format("%0.2f", v["ĿѪ"])
	print(table.concat(t, ", "))
end

--Ŀʹü
function CastX(szSkill, bf)
	if xcast(szSkill, v["Ŀ"], bf) then
		if v["¼Ϣ"] then PrintInfo() end
		return true
	end
	return false
end

-------------------------------------------------------------------------------
--ʼ
function OnChannel(CasterID, SkillName, SkillID, SkillLevel, TargetType, TargetID, PosY, PosZ, Frame, StartFrame, FrameCount)
	if CasterID == id() then
		if SkillID == 33444 then	--
			deltimer("")
		end
	end
end
