--[[ Ѩ: [ɷ][][ҷ][][][ʦ][ֹ֪][][][ƺ][][ɺ]
ؼ:
 2 1˺ 1
 2 2˺
 2 2˺
 2 2˺
--]]

--رԶ
setglobal("Զ", false)

--ѡ
addopt("", false)

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

--
local f = {}

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

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

	--ʼ
	v["ܴ"] = cn(14067)		--ID
	v["ʱ"] = cntime(14067, true)
	v["ܴ"] = cn("")
	v["ʱ"] = cntime("", true)
	v["Ӱܴ"] = cn("Ӱб")
	v["Ӱʱ"] = cntime("Ӱб", true)
	v["ӰCD"] = scdtime(14081)		--ͻӰ, ID
	v["CD"] = scdtime("ɺ")
	v["ʱ"] = casttime(14067)
	v["ʱ"] = casttime(14064)
	v["GCD"] = cdinterval(16)

	v["ɽˮ"] = buff("9319")	--buffID
	v["ѩ"] = buff("9320")
	v["ƽɳ"] = buff("9322")
	v["Ŀʱ"] = tbufftime("", id())
	v["Ŀʱ"] = tbufftime("", id())
	v[""] = buffsn("24327")
	v["ʱ"] = bufftime("")
	v["ҷ"] = buffsn("ҷ")		--24247 5
	v["ҷʱ"] = bufftime("ҷ")
	v["ղ"] = buffsn("")		--24754 32
	v["ʱ"] = bufftime("")
	v["ƺ"] = buffsn("12576")
	v["ƺʱ"] = bufftime("12576")
	v["֪ʱ"] = bufftime("֪")
	v["Ӱ"] = 0
	for i = 3, 8 do
		if buff("999"..i) then
			v["Ӱ"] = v["Ӱ"] + 1
		end
	end
	v["ĿѪ϶"] = rela("ж") and tlifevalue() > lifemax() * 10	--Ŀ굱ǰѪԼѪ10, Сֲ뿪ϵ

	
	--ָɽ
	if nofight() and not v["ɽˮ"] and v[""] == 0 and v["CD"] <= 0 then
		f["лɽˮ"]()
	end

	--
	if getopt("") and dungeon() and nofight() then return end
	
	f["Ӱ˫"]()

	f["Ӱб"]()

	--ȴͷ, ͬ
	if gettimer("") < 0.5 then
		print("---------ȴ乬ͷ", frame())
		return
	end

	--ȴл
	if gettimer("л") < 0.3 then
		print("---------ȴл", frame())
		return
	end

	--ƽAս, սbuffɾ
	if nofight() then
		CastX("")
	end

	-- +  + 
	v["dotжʱ"] = v["ʱ"] + v["GCD"] + v["ʱ"] + 0.5

	if v["ѩ"] then
		f["ѩ"]()
	
	elseif bufftime("ɺ") > 0 then
		f["ɺ"]()
	
	elseif v["ɽˮ"] then
		f["ɽˮ"]()
	
	elseif v["ƽɳ"] then
		f["ƽɳ"]()
	end

	--ûżܼ¼ϢŲ
	if fight() and rela("ж") and dis() < 20 and face() < 90 and cdleft(16) <= 0 and castleft() <= 0 and gettimer("") > 0.5 and gettimer("乬") > 0.5 and gettimer("") > 0.5 and gettimer("") > 0.5 and gettimer("") > 0.5 and state("վ") then
		PrintInfo("---------- ûż")
	end
end

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

f["ɺ"] = function()	--ǰ4 2, 3, 5, 6 , һαΪ4
	if v["CD"] > 59 then
		CastX(14229, true)	--ɽ, ͸ɽˮ0.5GCD, ǰûų
	end

	if v[""] == 6 then
		CastX("")		-- +3
	end

	if v[""] == 5 then
		CastX("乬")	-- +4
	end

	if v[""] == 4 then
		if buff("25957") then
			f["лѩ"]()
		else
			CastX("")	-- +5
		end
	end

	if v[""] == 3 then
		CastX("")	-- +6
	end

	if v[""] == 2 then
		CastX("")		-- +2
	end

	--ߵ, ·;
	CastX("乬")

end

f["ɽˮ"] = function()
	--ɺ, Ӱ -> ɽ -> 
	if fight() and v["ĿѪ϶"] and dis() < 20 and nobuff("ɺ") then	--v[""] <= 0
		if v["ܴ"] > 0 or cntime(14067) <= v["GCD"] then
			if v["CD"] < 0.5 then
				CastX(14081)		--Ӱ˫
				if v["ӰCD"] > 1 then	--Ӱ
					CastX(14229, true)	--ɽ
					CastX("ɺ")
				end
				return	--ȴ˾ͱ
			end
		end
	end

	--лѩ
	if v["Ŀʱ"] > v["dotжʱ"] and v["Ŀʱ"] > v["dotжʱ"] then
		if gettimer("ɺ") > 0.5 and gettimer("Ӱ˫") > 0.5 then
			if v[""] == 0 or v[""] == 4 or v[""] == 5 then
				f["лѩ"]()
			end
		end
	end
	
	--ȴ乬ͷźĿbuffˢ
	if gettimer("ͷű乬") < 0.5 and v["乬Ŀ"] ~= 0 and v["乬Ŀ"] == tid() then
		print("----------乬ȴĿbuffˢ", frame())
		return
	end
		
	if fight() and gettimer("ɺ") > 0.5 and gettimer("Ӱ˫") > 0.5 then
		if v["Ŀʱ"] <= v["ʱ"] or v[""] == 2 then	--dot 
			CastX("")		-- +3
		end
		if v["Ŀʱ"] <= v["ʱ"] or v[""] == 3 then	--dot 
			CastX("")		-- +2
		end

		if v[""] == 0 or v[""] == 1 or v[""] >= 5 then	--5ñ乬
			CastX("乬")	-- +4
		end

		if v[""] == 4 then
			CastX("")		-- +5
		end
	end
end

f["ѩ"] = function()
	if v["ĿѪ϶"] and dis() < 20 and v["CD"] < v["GCD"] and nobuff("ɺ") and v["ܴ"] > 0  then	--иɽ, 
		f["лɽˮ"]()
	end

	if v[""] == 0 then
		if v["Ŀʱ"] < v["dotжʱ"] or v["Ŀʱ"] < v["dotжʱ"] then	--иɽ, 乬ˢdot
			f["лɽˮ"]()
		end

		if v["CD"] <= v["ʱ"] + v["GCD"] and v["ʱ"] > 13 then	--ɿȴܲ㣬ȴ
			CastX("")
		end
		CastX("")
		CastX("")
	end

	if v[""] == 4 then
		CastX("")		-- +5
	end

	if v[""] == 5 then
		CastX("")		-- +4
	end

	--Ӧþ 0 4 5 , ֹӳ󴥵¼Ӹֹⲻ
	CastX("")		--+3
end

f["ƽɳ"] = function()
	f["лɽˮ"]()		--PVEƽɳ, ֶ󴥽ƽɳֱиɽ
end

f["лѩ"] = function()
	if CastX(14070) then	--ID
		settimer("л")
		exit()
	end
end

f["лɽˮ"] = function()
	if CastX(14069) then	--ID
		settimer("л")
		exit()
	end
end

f["Ӱ˫"] = function()
	if casting("|") and bufftime("Ӱ˫") < 0.5 then	-- ʱ쵽ˣ
		if fcast(14162) then
			settimer("Ӱ˫")
		end
		return
	end

	if bufftime("Ӱ˫") <= v["ʱ"] then	--Сһ乬
		if cast(14162) then
			settimer("Ӱ˫")
		end
		return
	end

	if scdtime(14229) > bufftime("Ӱ˫") and v["CD"] > bufftime("Ӱ˫") then		--ɽù
		if v["Ӱ"] >= 6 or cn("Ӱб") < 1 then	--Ӱ˻Ӱб
			if cast(14162) then
				settimer("Ӱ˫")
			end
		end
	end
end

f["Ӱб"] = function()
	if rela("ж") and v["Ӱ"] < 6 then
		local bCast = false
		if gettimer("Ӱ˫") > 0.5 and bufftime("Ӱ˫") > 0 then	--Ӱ
			bCast = true
		end
		if buff("ɺ") then	--
			bCast = true
		end
		if v["Ӱܴ"] >= 3 then	--
			bCast = true
		end
		if bCast then
			CastX("Ӱб")
		end
	end
end

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

--¼Ϣ
function PrintInfo(s)
	local t = {}
	if s then t[#t+1] = s end
	t[#t+1] = "Ŀ:"..v["Ŀʱ"]
	t[#t+1] = "Ŀ:"..v["Ŀʱ"]
	t[#t+1] = ":"..v[""]
	t[#t+1] = "ʱ:"..bufftime("ɺ")
	--t[#t+1] = "ҷ:"..v["ҷ"]..", "..v["ҷʱ"]
	--t[#t+1] = ":"..v["ղ"]..", "..v["ʱ"]
	t[#t+1] = "֪:"..v["֪ʱ"]
	t[#t+1] = "Ӱ:"..v["Ӱ"]
	
	t[#t+1] = "CD:"..v["ܴ"]..", "..v["ʱ"]
	t[#t+1] = "CD:"..v["ܴ"]..", "..v["ʱ"]
	t[#t+1] = "ӰCD:"..v["Ӱܴ"]..", "..v["Ӱʱ"]
	t[#t+1] = "CD:"..v["CD"]
	t[#t+1] = "ӰCD:"..v["ӰCD"]

	print(table.concat(t, ", "))
end

--ʹüܲ¼Ϣ
function CastX(szSkill, bSelf)
	if cast(szSkill, bSelf) then
		settimer(szSkill)
		if v["¼Ϣ"] then PrintInfo() end
		return true
	end
	return false
end

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

--ͷżܻص
function OnCast(CasterID, SkillName, SkillID, SkillLevel, TargetType, TargetID, PosY, PosZ, StartFrame, FrameCount)
	if CasterID == id() then
		if SkillID == 18860 then	--乬Ӽ
			v["乬Ŀ"] = TargetID	--ͷʱĿbuffûˢ, ¼ĿID
			settimer("ͷű乬")
			deltimer("")
		end

		if SkillID == 14474 then	--Ӽ
			deltimer("")
		end

		if SkillID == 34676 then	--֪˾, ¼ͷϢ, ڲ鿴ǲߵȼ21˺
			print("--------------------"..SkillName..", ID:"..SkillID..", ܵȼ:"..SkillLevel..", ĿID:"..TargetID..", ʼ֡:"..StartFrame..", ǰ֡:"..FrameCount)
		end
	end
end

--buff»ص
function OnBuff(CharacterID, BuffName, BuffID, BuffLevel, StackNum, SkillSrcID, StartFrame, EndFrame, FrameCount)
	if CharacterID == id() and StackNum  > 0 then	--Լbuff
		if BuffID == 9319 or BuffID == 9320 or BuffID == 9322 then	--3buff
			deltimer("л")
		end

		if BuffID == 26001 then
			print("--------------------")
		end
	end
end

--buffб»ص
function OnBuffList(CharacterID)
	if CharacterID == v["乬Ŀ"] then
		v["乬Ŀ"] = 0	--ͷű乬Ŀbuffˢ, 0ȴ
	end
end

--¼ս״̬
function OnFight(bFight)
	if bFight then
		print("--------------------ս")
	else
		print("--------------------뿪ս")
	end
end

--[[ ѭ
ɺ ȷ
Ӱб [ƺ]˸֪
Ŀ̽ǲ
ɺʱѭ   - иɽ - 乬 -  - 
--]]
