--[[ Ѩ: [][ѩ][][][][Ԩ][][лת][ٽ][][][·]
ؼ:
Ǵ	1 3˺
	2Ϣ 2˺
ź	3˺ 1
	2Ϣ 2˺
	1 3˺
ն޳  2Ϣ 2˺
Ѫ	2Ϣ 2
ڤ	1 2˺ 1

1μ, 3.5
ҪĿ3.5, Ϊº8߶, ̫Զ򲻵
º, Ŀû, ǻù, 6-12
ľ׮, ʵս鲻Ǻܺ
--]]

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

--ѡ
addopt("", false)

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

--
local f = {}

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

	--¼
	if casting("ź") and castleft() < 0.13 then
		settimer("źĶ")
	end
	if casting("") and castleft() < 0.13 then
		settimer("Ƕ")
	end

	--ʼ
	local tSpeedXY, tSpeedZ = speed(tid())
	v["Ŀ꾲ֹ"] = rela("ж") and tSpeedXY <= 0 and tSpeedZ <= 0
	v["GCD"] = cdinterval(16)
	v["GCD"] = cdleft(16)
	v["CD"] = scdtime("")
	v["źĳܴ"] = cn("ź")
	v["źĳʱ"] = cntime("ź", true)
	v["źCD"] = scdtime("ź")
	v["CD"] = scdtime("")
	v["CD"] = scdtime("")
	v["ն޳CD"] = scdtime("ն޳")
	v["Ѫܴ"] = cn("ѪȪ")
	v["Ѫʱ"] = cntime("ѪȪ", true)
	v["CD"] = scdtime("⹳")
	v["CD"] = scdtime("͹")
	v["CD"] = scdtime("лת")
	v["źĶʱ"] = casttime("ź")
	v["Ƕʱ"] = casttime("")
	v["ն޳ʱ"] = casttime("ն޳")

	v["Ŀʱ"] = tbufftime("", id())
	v["ǲ"] = buffsn("26215")			--8, 3ʱźĴһ˺Ȼɾ
	v["ʱ"] = bufftime("26215")
	v["ʱ"] = bufftime("͹")	--buff 16596 ĻЧ+15% 6 4ͽɾ, buff 24244 5
	v["ٽڲ"] = buffsn("ٽ")			--6 3 ʵЧ 15927 15928 15929, Ӽն4˺ 10% 20% 30%
	v["ٽʱ"] = bufftime("ٽ")
	v["ʱ"] = bufftime("")		--10, +25%
	v["ʱ"] = bufftime("")		--10, ӷ+50%
	v["ʱ"] = bufftime("24744")			--·


	--Ŀ겻ǵ ֱӽ
	if not rela("ж") then return end

	--
	if getopt("") and dungeon() and nofight() then return end

	--ȴ״̬ͬ
	if gettimer("źĶ") < 0.3 then
		--if cdleft(16) <= 0 then print("----------ȴźͷ") end
		return
	end

	--жϼ
	v["ɷż"] = false
	v["Ҫʱ"] = 0
	if v["ٽʱ"] < v["źĶʱ"] then	--ˣûаٽ
		if v["źĳܴ"] >= 3 and v["CD"] <= v["źĶʱ"] * 3 then	--3 + 
			v["ɷż"] = "3 + "
			v["Ҫʱ"] = v["źĶʱ"] * 3 + casttime("")
		end
	else
		if v["źĳܴ"] + v["ٽڲ"] >= 3 then	--ܴ3ٽ
			if v["CD"] <= (3 - v["ٽڲ"]) * v["źĶʱ"] then	--CDСڴ3ٽʱ
				v["ɷż"] = format("%d + ", 3 - v["ٽڲ"])
				v["Ҫʱ"] = v["źĶʱ"] * (3 - v["ٽڲ"]) + casttime("")
			end
		end
	end

	--˫ն޳
	v["ն޳"] = false
	if not v["ɷż"] and dis() < 4 and v["ն޳CD"] <= v["GCD"] then
		if v["źĳʱ"] > v["ն޳ʱ"] and v["ٽʱ"] > v["ն޳ʱ"] + v["źĶʱ"] + 0.25 then
			v["ն޳"] = true
		end
	end

	--ն޳
	f["ն"]()

	--⹳,  0.5 8.75, п
	v["źĶʣ"] = 0.625
	v["źĶѶ"] = v["źĶʱ"] - v["źĶʣ"]
	if casting("ź") and castleft() <= v["źĶʣ"] and dis() < 3.5 then
		--if v["ٽʱ"] > castleft() + 0.1 and v["ٽڲ"] + v["źĳܴ"] >= 3 and v["CD"] <= (3 - v["ٽڲ"]) * v["GCD"] then
		if not v["ն޳"] then
			if v["ʱ"] > 3 then
				aCastX("⹳")
			end
		end
	end

	--
	v[""] = false
	if (v["Ŀ꾲ֹ"] or ttid() == id()) and dis() < 12 and face() < 90 then
		if v["GCD"] < 0.5 and v["CD"] <= v["GCD"] then
			if v["ٽڲ"] >= 3 and v["ٽʱ"] > v["źĶʱ"] + v["Ƕʱ"] + v["GCD"] + 0.3 then
				v[""] = true
			end
			
		end
	end

	if buff("") or gettimer("ѪȪ") < 0.3 then
		if gettimer("ڤ") > 0.3 then
			f[""]()
		end
	end

	if nobuff("") or gettimer("ڤ") < 0.3 then
		if gettimer("ѪȪ") > 0.3 then
			f["˫"]()
		end
	end

	--ûżܼ¼Ϣ
	if fight() and rela("ж") and dis() < 12 and face() < 60 and cdleft(16) <= 0 and castleft() <= 0  and state("վ|·|ܲ|Ծ") then
		if gettimer("ڤ") > 0.3 and gettimer("ѪȪ") > 0.3 and gettimer("ź") > 0.3 and gettimer("źĶ") > 0.3 and gettimer("") > 0.3 and gettimer("Ƕ") > 0.3 and gettimer("ն޳") > 0.3 then
			PrintInfo("---------- ûż")
		end
	end
end

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

f[""] = function()
	--
	if v[""] and dis() > 6 then
		if f[""]("") then
			return
		end
	end

	--ն޳
	v["ƶ"] = keydown("MOVEFORWARD") or keydown("MOVEBACKWARD") or keydown("STRAFELEFT") or keydown("STRAFERIGHT") or keydown("JUMP")
	if not v["ƶ"] and dis() < 4 and cdtime("ն޳") <= 0 then
		if v["CD"] <= v["ն޳ʱ"] / 8 * 5 + v["źĶѶ"] then
			if v["ٽʱ"] > v["ն޳ʱ"] / 8 * 5 + v["źĶʱ"] + 0.3 then
				if f[""]("ն޳") then
					CastX("ն޳")
				end
			end
		end
	end
	
	--n + 
	if v["ɷż"] then
		if v["CD"] > 0 or v["ʱ"] > v["Ҫʱ"] then
			if dis() < 12 and cdtime("ź") <= 0 and f[""](v["ɷż"]) then
				CastX("ź")
				return
			end
		end
	end

	--żźıٽ
	if v["ٽʱ"] > v["źĶʱ"] and v["ٽʱ"] < 2.5 then
		if dis() < 12 and cdtime("ź") <= 0 then
			if f[""]("żźıٽ") then
				CastX("ź")
			end
		end
	end

	-- + Ǵ + ź + 
	if v["CD"] < v["GCD"] * 2 + v["źĶѶ"] then
		if v["Ŀʱ"] > 0 then
			CastX("")
		end
	end

	-- + ź + ն޳ + ź + 
	if v["ն޳CD"] <= v["GCD"] + v["źĶʱ"] then
		if v["CD"] < v["GCD"] + v["źĶʱ"] + v["ն޳ʱ"] + v["źĶѶ"] then
			if v["Ŀʱ"] > 0 then
				CastX("")
			end
		end
	end

	--Ǵ
	if v["CD"] > 0 and dis() < 6 and face() < 90 then
		if v["źĳʱ"] > v["GCD"] + v["źĶʱ"] and v["ٽʱ"] > v["GCD"] + v["źĶʱ"] + 0.3 then
			CastX("ǴƽҰ")
		end
	end

	--ź
	if cdleft(16) <= 0 and dis() < 12 and cdtime("ź") <= 0 then
		if v["CD"] > 0 or v["Ŀʱ"] > 0 then
			if f[""]("żź") then
				CastX("ź")
			end
		end
	end
end


f["˫"] = function()
	--
	if dis() > 6 and dis() < 12 and face() < 90 and v["CD"] > 20 then
		 if v["ٽڲ"] >= 3 and v["ٽʱ"] > 0 and v["ʱ"] >= 0 and v["ʱ"] >= 0 then
			aCastX("")
		 end
	end
	v[""] = npc("ϵ:Լ", "ģID:107305")
	if v[""] ~= 0 and xdis(v[""]) < 12 then
		aCastX("", 0, v[""])
	end

	--лת
	if dis() < 12 and v["ٽʱ"] >= v["GCD"] + v["źĶʱ"] + 0.5 then
		if v["ʱ"] >= 0 and v["ʱ"] > 7 then
			if CastX("лת") then
				aCastX("")
			end
		end
	end

	--͹
	v["+Ҷʱ"] = v["źĶʱ"] + v["Ƕʱ"] + 0.3
	if (v["Ŀ꾲ֹ"] or ttid() == id()) and dis() > 6 and dis() < 12 then
		if v["ٽڲ"] >= 2 and v["ٽʱ"] > casttime("ź") and v["ʱ"] > v["+Ҷʱ"] and v["ʱ"] > v["+Ҷʱ"] then
			if cdtime("ź") <= 0 and cdtime("͹") <= 0 then
				CastX("͹")
				CastX("ź")
			end
		end

		if v["ʱ"] > v["Ƕʱ"] and v["ʱ"] > v["Ƕʱ"] then
			if v["ٽڲ"] >= 3 and v["ٽʱ"] > v["źĶʱ"] + v["Ƕʱ"] + v["GCD"] then
				if v["GCD"] < 0.5 and v["źĳʱ"] > 5 then
					CastX("͹")
				end
			end
		end
	end

	--
	if v[""] then
		if v["ʱ"] > v["Ƕʱ"] or gettimer("ڤ") < 0.3 then
			aCastX("")
		end
		if v["ʱ"] <= v["Ƕʱ"] and gettimer("ڤ") > 0.3 then
			f[""]("")
		end
	end

	--ն޳
	if v["ն޳"] then
		CastX("ն޳")
	end

	--
	if v["GCD"] < 0.5 and v["CD"] <= v["GCD"] and v["CD"] <= v["GCD"] + v["GCD"] * 2 + v["źĶѶ"] then
		if v["ٽʱ"] > v["GCD"] + v["GCD"] * 2 + v["źĶʱ"] then
			f[""](" + Ǵ + ź + ")
		end
	end
	
	if v["GCD"] < 0.5 and v["CD"] <= v["GCD"] then
		if v["ն޳CD"] <= v["GCD"] + v["GCD"] + v["źĶʱ"] then
			if v["CD"] <= v["GCD"] + v["GCD"] + v["źĶʱ"] + v["ն޳ʱ"] + v["źĶѶ"] then
				if v["ٽʱ"] > v["GCD"] + v["GCD"] + v["źĶʱ"] then
					f[""](" + ź + ն޳ + ź + ")
				end
			end
		end
	end

	--ź
	if v["źĳܴ"] >= 3 then	--
		if CastX("ź") then
			print("ּź")
		end
	end

	if dis() < 12 and v["GCD"] < 0.5 and v["źCD"] <= v["GCD"] then
		if v["ʱ"] <= v["źĶʱ"] then
			f[""]("ź")
		end
		CastX("ź")
	end
end

f["ն"] = function()
	local szBreakReason = nil
	if casting("ն޳") then
		v["6ڵ"] = npc("ϵ:ж", "ѡ", "<6")
		if v["6ڵ"] == 0 then
			szBreakReason = "6û"
		end
		if v["ʱ"] >= 0 then
			if v["źĳܴ"] >= 1 and v["ٽʱ"] > v["źĶʱ"] and v["ٽʱ"] < v["źĶʱ"] + 0.3 then
				szBreakReason = "ٽ"
			end
			if v["CD"] <= v["źĶʱ"] and v["źĳܴ"] >= 2 then
				szBreakReason = "ŷ"
			end
		end
	end

	if szBreakReason then
		stopcasting()
		PrintInfo("--ն޳:"..szBreakReason)
	end
end

f[""] = function(szReason)
	if gettimer("ѪȪ") > 0.3 and CastX("ѪȪ") then
		print("-------------------- :"..szReason, frame())
		exit()
	end
end

f[""] = function(szReason)
	if gettimer("ڤ") > 0.3 and CastX("ڤ") then
		print("-------------------- :"..szReason, frame())
		return true
	end
	return false
end

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

--¼Ϣ
function PrintInfo(s)
	local t = {}
	if s then t[#t+1] = s end
	
	--t[#t+1] = "Ŀ:"..v["Ŀʱ"]
	t[#t+1] = "ٽ:"..v["ٽڲ"]..", "..v["ٽʱ"]
	t[#t+1] = ":"..v["ʱ"]
	t[#t+1] = ":"..v["ʱ"]
	t[#t+1] = ":"..v["ʱ"]
	t[#t+1] = ":"..v["ǲ"]..", "..v["ʱ"]

	t[#t+1] = "źCD:"..v["źĳܴ"]..", "..v["źĳʱ"]
	t[#t+1] = "CD:"..v["CD"]
	t[#t+1] = "CD:"..v["CD"]
	t[#t+1] = "CD:"..v["CD"]
	t[#t+1] = "CD:"..v["CD"]
	t[#t+1] = "ն޳CD:"..v["ն޳CD"]
	t[#t+1] = "CD:"..v["CD"]
	t[#t+1] = "CD:"..v["CD"]
	--t[#t+1] = "ѪCD:"..v["Ѫܴ"]..", "..v["Ѫʱ"]
	t[#t+1] = ":"..v["ʱ"]
	t[#t+1] = ":"..format("%.1f", dis())
	t[#t+1] = "GCD:"..v["GCD"]
	
	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 aCastX(szSkill)
	if acast(szSkill) then
		settimer(szSkill)
		if v["¼Ϣ"] then PrintInfo() end
		return true
	end
	return false
end

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

--buff
function OnBuff(CharacterID, BuffName, BuffID, BuffLevel, StackNum, SkillSrcID, StartFrame, EndFrame, FrameCount)
	if CharacterID == id() then
		if BuffID == 15524 then		--
			deltimer("ѪȪ")
			deltimer("ڤ")
		end
		if BuffID == 26215 then	--źķǼ
			if StackNum  > 0 then
				deltimer("źĶ")
			end
		end
	end
end

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