--[[ Ѩ: [ָ][][][̤][][ѩ][][][][ѩ][][]
ؼ:
  3Ϣ 1
¥  2Ϣ 1
  2 2˺
  1 1 2
  3 1
ܽ  1 2˺ 1Ч
ѩ  3˺ 1
  2Ϣ 1 1
--]]

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

--
local v = {}
v["Ŀ"] = 0
v["Ŀ"] = 0
v["Ŀ"] = 0
v["̺Ŀ"] = 0

--
local f = {}

--ѭ
function Main()
	--
	if fight() and life() < 0.6 then
		cast("໤", true)
	end

	--ȴָ
	if casting("ָ") and castleft() < 0.13 then
		settimer("ָ")
	end
	if gettimer("ָ") < 0.3 then
		return
	end

	--ֶ¼Ŀ
	if casting("ع") and castleft() < 0.13 then
		v["Ŀ"] = casttarget()
		settimer("ֶ")
	end

	--ݶ¼Ŀ
	if casting("") and castleft() < 0.13 then
		v["Ŀ"] = casttarget()
		settimer("ݶ")
	end

	--ʼ
	v["Ŀʱ"] = tbufftime("ع", id())
	v["Ŀʱ"] = tbufftime("ָ", id())
	v["Ŀʱ"] = tbufftime("", id())
	v["Ŀѩʱ"] = tbufftime("ѩʱ", id())
	v["Ŀ"] = f["Ŀ"]()
	v["Ŀ"] = f["Ŀ"]()
	v["Ŀ"] = f["Ŀ"]()
	v["Ŀпѩ"] = f["Ŀпѩ"]()
	v["Ŀdot"] = f["Ŀdot"]()
	v["ܽCD"] = scdtime("ܽز")
	v["ʯCD"] = scdtime("ʯ")
	v[""] = buff("")
	v["вɢ"] = buff("ɢ")
	v["̤"] = buff("26416")
	v["ȫbuffҪʱ"] = f["ȫbuffҪʱ"]()


	--
	if rela("ж") and dis() < 20 and face() < 60 and v["ʯCD"] > 10 and cdleft(16) >= 0.5 and cdleft(16) <= 1 and not v["̤"] then
		cast("")
	end

	--ܽ
	--if v["Ŀdot"] >= 4 and v["ʯCD"] < cdinterval(16) and v["ʯCD"] > 0 then
	if v["Ŀdot"] >= 4 and v["ʯCD"] < cdinterval(16) then
		cast("ܽز")
	end

	--ʯ
	if not v["вɢ"] and v["Ŀdot"] >= 3 and scdtime("") < 0.1 and not v["̤"] then
		cast("ʯ")
	end
	if v["Ŀdot"] >= 4 then	
		cast("ʯ")
	end
	
	--
	if v[""] and not v["Źָ"] then

	else
		if not v["Ŀ"] then
			cast("")
		end
	end
	
	--ѩ
	if not v["Ŀпѩ"] or buffsn("") < 3 then
		cast("ѩʱ")
	end

	--ָ
	if v[""] and not v["Źָ"] then
		cast("ָ")
	end
	if v["ʯCD"] - v["ȫbuffҪʱ"] > 1 then
		cast("ָ")
	end

	--
	if not v["Ŀ"] then
		cast("ع")
	end

	--
	if not v["Ŀ"] then
		cast("ָ")
	end

	--ˮ
	if fight() and mana() < 0.5 then
		cast("ˮ", true)
	end
end


f["Ŀ"] = function()
	if v["̺Ŀ"] ~= 0 and v["̺Ŀ"] == tid() and gettimer("̺1") < 0.5 then return false end		--û
	if v["Ŀ"] ~= 0 and v["Ŀ"] == tid() then
		if gettimer("ͷع") < 0.5 or gettimer("ֶ") < 0.5 then return true end	--շŹ
	end
	return v["Ŀʱ"] > 0
end

f["Ŀ"] = function()
	if v["̺Ŀ"] ~= 0 and v["̺Ŀ"] == tid() and gettimer("̺2") < 0.5 then return false end
	if v["Ŀ"] ~= 0 and v["Ŀ"] == tid() and gettimer("ͷָ") < 0.5 then return true end
	return v["Ŀʱ"] > 0
end

f["Ŀ"] = function()
	if v["̺Ŀ"] ~= 0 and v["̺Ŀ"] == tid() and gettimer("̺3") < 0.5 then return false end
	if v["Ŀ"] ~= 0 and v["Ŀ"] == tid() then
		if gettimer("ͷ") < 0.5 or gettimer("ݶ") < 0.5 then return true end
	end
	return v["Ŀʱ"] > 0
end

f["Ŀпѩ"] = function()
	if v["̺Ŀ"] ~= 0 and v["̺Ŀ"] == tid() and gettimer("̺0") < 0.5 then return false end
	return v["Ŀѩʱ"] > 0
end

f["Ŀdot"] = function()
	local nDot = 0
	if v["Ŀ"] then nDot = nDot + 1 end
	if v["Ŀ"] then nDot = nDot + 1 end
	if v["Ŀ"] then nDot = nDot + 1 end
	if v["Ŀпѩ"] then nDot = nDot + 1 end
	return nDot
end

f["ȫbuffҪʱ"] = function()
	local nTime = 0
	if not v["Ŀ"] then
		nTime = nTime + cdinterval(16)
	end
	if not v["Ŀ"] then
		nTime = nTime + cdinterval(16)
	end
	if v["ܽCD"] < v["ʯCD"] then
		nTime = nTime + cdinterval(16)
	end
	return nTime
end

--ͷż
function OnCast(CasterID, SkillName, SkillID, SkillLevel, TargetType, TargetID, PosY, PosZ, StartFrame, FrameCount)
	--ͷżܵԼ
	if CasterID == id() then
		--ָ
		if SkillID == 179 then
			deltimer("ָ")
			v["Źָ"] = true
			return
		end

		--ָ
		if SkillID == 180 then
			v["Ŀ"] = TargetID
			settimer("ͷָ")
			return
		end

		--ع
		if SkillID == 189 then
			v["Ŀ"] = TargetID
			settimer("ͷع")
			return
		end

		--
		if SkillID == 190 then
			v["Ŀ"] = TargetID
			settimer("ͷ")
			return
		end

		--̺, ָ  ̤ dot, ȡ ȼ 1 , 2 , 3 , 0 ѩ
		if SkillID == 601 then
			local level = SkillLevel % 4
			settimer("̺"..level)		--öӦܼʱ
			v["̺Ŀ"] = TargetID

			--ˮ
			if rela("ж") and dis() < 20 and face() < 60 then
				cast("ˮ޼")
			end
		end

		--
		if SkillID == 2645 then
			v["Źָ"] = false
		end

		-- ָ 
		if SkillID == 13847 then
			v["Ŀ"] = TargetID
			settimer("ͷع")
			return
		end

		-- ָ 
		if SkillID == 13848 then
			v["Ŀ"] = TargetID
			settimer("ͷ")
			return
		end
	end
end
