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

ѭ   - ɽ - 乬 -  - 
--]]

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

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

--
local f = {}

--ѭ
function Main(g_player)
	if casting("乬") and castleft() < 0.13 then
		settimer("乬")
	end
	if casting("|") and castleft() < 0.13 then
		settimer("")
	end

	--ʼ
	v[""] = buffsn("24327")
	v["Ӱ"] = 0
	for i = 3, 8 do
		if buff("999"..i) then
			v["Ӱ"] = v["Ӱ"] + 1
		end
	end
	v["Ŀʱ"] = tbufftime("", id())
	v["Ŀʱ"] = tbufftime("", id())
	v["CD"] = scdtime("ɺ")
	v["ӰCD"] = cdtime(14081)	--ͻӰ, ID
	v["ĿѪ϶"] = rela("ж") and tlifevalue() > lifemax() * 5	--Сֲ뿪ϵ

	--Ӱ
	f["Ӱ˫"]()

	--Ӱ
	f["Ӱб"]()

	--ȴbuffͬ, buffûͬж
	if gettimer("乬") < 0.5 then
		--print("---------ȴͬ")
		return
	end
	if gettimer("ѩ") < 0.5 or gettimer("ɽˮ") < 0.5 then
		print("---------ȴл")
		return
	end

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

	--ûսƽA
	if nofight() then
		CastX("")
	end

	-- +  + 乬 ʱ
	local nTime = casttime("") + cdinterval(16) + casttime("乬") + 0.5

	--------------------ѩ--------------------
	if buff("9320") then
		if v[""] == 0 then
			if v["Ŀʱ"] < nTime or v["Ŀʱ"] < nTime then
				f["ɽˮ"]()
			end
		end

		if v[""] == 0 or v[""] == 4 then
			if CastX("") then
				settimer("")
			end
		end

		if v[""] == 0 or v[""] == 5 then
			CastX("")
		end
	end

	--------------------ɽˮ--------------------
	if buff("9319") then
		--
		if buff("ɺ") then
			f["ɺ"]()
			return
		end

		--ɺ
		if fight() and v["ĿѪ϶"] and dis() < 20 and nobuff("ɺ|֪") then
			if cdtime("ɺ") < 0.5 then
				f["Ӱ˫"]()
				if v["ӰCD"] > 1 then
					f["ɽˮ"]()
				end
				if v["ӰCD"] > 1 and scdtime(14229) > 1 then 
					if CastX("ɺ") then
						settimer("ɺ")
					end
				end
				return
			end
		end

		--л
		if v["Ŀʱ"] > nTime and v["Ŀʱ"] > nTime then
			if gettimer("ɺ") > 0.5 and gettimer("Ӱ˫") > 0.5 then
				if v[""] == 0 or v[""] == 4 or v[""] == 5 then
					f["ѩ"]()
				end
			end
		end

		if v["乬Ŀ"] ~= 0 and v["乬Ŀ"] == tid() then
			--print("----------乬ȴĿbuffˢ")
			return
		end
		
		if fight() and gettimer("Ӱ˫") > 0.5 then
			--if v["Ŀʱ"] <= casttime("乬") or v[""] == 2 or v[""] == 6 then
			if v["Ŀʱ"] <= casttime("乬") or v[""] == 2 then
				CastX("")
			end

			--if v["Ŀʱ"] <= casttime("乬") or v[""] == 3 or v[""] == 7 then
			if v["Ŀʱ"] <= casttime("乬") or v[""] == 3 then
				CastX("")
			end

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

	f["ûż"]()
end

--żϢ
f["ûż"] = function()
	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 state("վ") then
		PrintInfo("----------û, ")
	end
end

f["ɺ"] = function()
	if v[""] == 6 then
		CastX("")
	end
	if v[""] == 5 then
		if CastX("乬") then
			settimer("乬")
		end
	end
	if v[""] == 4 then
		if buffsn("֪") == 4 then	--һκ
			f["ѩ"]()
		else
			CastX("")
		end
	end
	if v[""] == 3 then
		if CastX("") then
			settimer("")
		end
	end
	if v[""] == 2 then
		CastX("")
	end

	f["ûż"]()
end

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

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

f["ɽˮ"] = function()
	cast(14229)
end

f["Ӱ˫"] = function()
	if cast(14081) then
		settimer("Ӱ˫")
	end
end

f["Ӱ˫"] = function()
	if casting("|") and bufftime("Ӱ˫") < 0.5 then	--ʱ쵽ˣ
		fcast(14162)
		return
	end
	if bufftime("Ӱ˫") <= casttime("乬") then	--Сһ乬
		cast(14162)
		return
	end
	if scdtime(14229) > 1 and v["CD"] > 1 then		--ɽù
		if v["Ӱ"] >= 6 or cn("Ӱб") < 1 then	--Ӱ˻Ӱб
			cast(14162)
		end
	end
end

f["Ӱб"] = function()
	if v["Ӱ"] < 6 then
		if buff("ɺ|Ӱ˫") then
			cast("Ӱб")
		end
	end
end

--Ϣ
function PrintInfo(s)
	local szinfo = ":"..v[""]..", Ŀ:"..v["Ŀʱ"]..", Ŀ:"..v["Ŀʱ"]..", Ӱ:"..cn("Ӱб")..", "..cntime("Ӱб", true)..", :"..cn("")..", "..cn("")..", CD:"..v["CD"]
	if s then
		szinfo = s..szinfo
	end
	print(szinfo)
end

--ʹü
function CastX(szSkill)
	if cast(szSkill) then
		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 == 14298 then	--乬
			v["乬Ŀ"] = TargetID	--¼ĿID
			return
		end
		if SkillID == 34676 then	--֪˾, ȼ21˺
			print("OnCast->ͷ:"..SkillName..", ID:"..SkillID..", ܵȼ:"..SkillLevel..", ĿID:"..TargetID..", ʼ֡:"..StartFrame..", ǰ֡:"..FrameCount)
			return
		end
	end
end

--ָbuffƳ
local tBuff = {
--[24247] = "ԭ",	--ҷ
--[24754] = "ԭ",	--
--[24327] = "",
--[9495] = "ԭ",	--
--[12576] = "ƺ",
--[9430] = "ѩ",
}

--buff»ص
function OnBuff(CharacterID, BuffName, BuffID, BuffLevel, StackNum, SkillSrcID, StartFrame, EndFrame, FrameCount)
	if CharacterID == id() then
		--[[
		local szName = tBuff[BuffID]
		if szName then
			if szName ~= "ԭ" then
				BuffName = szName
			end
			if StackNum  > 0 then
				print("OnBuff->buff: ".. BuffName..", ID: "..BuffID..", ȼ: "..BuffLevel..", : "..StackNum..", ԴID: "..SkillSrcID..", ʼ֡: "..StartFrame..", ǰ֡: "..FrameCount..", ֡: "..EndFrame..", ʣʱ: "..(EndFrame-FrameCount)/16)
			else
				print("OnBuff->Ƴbuff: ".. BuffName..", ID: "..BuffID..", ȼ: "..BuffLevel..", : "..StackNum..", ԴID: "..SkillSrcID..", ʼ֡: "..StartFrame..", ǰ֡: "..FrameCount)
			end
		end
		--]]
		if StackNum  > 0 then
			if BuffID == 9319 then
				deltimer("ɽˮ")
				return
			end
			if BuffID == 9320 then
				deltimer("ѩ")
				return
			end
			if BuffID == 24327 then	--
				deltimer("乬")
				return
			end
		end
	end
end

--buffбص
function OnBuffList(CharacterID)
	if CharacterID == v["乬Ŀ"] then
		v["乬Ŀ"] = 0
	end
end

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

--[[
		
	5	6
	4	5
	3	4
	2	3
	1	2
--]]
