--[[ Ѩ: [Ծ][][][][ӳ][][][][][̹][][]
ؼ:
Ϫ  2˺ 2Χ
  2˺ 2Ч()
Ʒ  2 1˺ 1
Ϧ  1 2˺ 1
  3˺ 1
  2 2˺
Х  1Ч() 3
 2Ϣ 1Ч() 1Ѫ

5⿪, 
--]]

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

--ѡ
addopt("", false)

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

--
local f = {}

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

	--
	if fight() and life() < 0.5 then
		cast("Ȫ")
	end
	
	--¼
	if casting("Ʒ") and castleft() < 0.13 then
		settimer("Ʒɶ")
	end
	if casting("ɽ") and castleft() < 0.13 then
		settimer("ɽ")
	end

	--ʼ
	v[""] = rage()

	v["GCD"] = cdinterval(16)
	v["ϪCD"] = scdtime("Ϫ")
	v["CD"] = scdtime("´")
	v["Хճܴ"] = cn("Х")
	v["Хճʱ"] = cntime("Х", true)
	v["ƷCD"] = scdtime("Ʒ")
	v["糵CD"] = scdtime("ɽ")
	v["ݺܴ"] = cn("ݺ")
	v["ݺʱ"] = cntime("ݺ", true)
	v["CD"] = scdtime("")
	v["CD"] = scdtime("")

	v[""] = buffsn("")		--к, Ϧ Ʒ &˺+15% 10 2
	v["ʱ"] = bufftime("")
	v["ʱ"] = bufftime("")	--Ϧк, ӷ60% 5
	v["ڲ"] = buffsn("")		--Ʒк, Ч+8% 15 3
	v["ʱ"] = bufftime("")
	v["Ŀ꾪ʱ"] = tbufftime("", id())	--к, 糵˺ 10
	v["̹"] = buffsn("̹")		--糵ÿ2μ1, Ʒ Ϧ ׹ ˺+30%, 9 4
	v["̹ʱ"] = bufftime("̹")
	v["ʱ"] = bufftime("")	--Хؽ, +8%, 10, ÿλӳ5
	v["ʱ"] = bufftime("")	--+10% ϳ˺+40%
	
	v["ɽ"] = npc("ϵ:Լ", "ģID:57739")
	v["зɽ"] = v["ɽ"] ~= 0 and xnpc(v["ɽ"], "ϵ:ж", "<10", "ѡ") ~= 0
	v[""] = npc("ϵ:Լ", "ģID:122728", "ɫ<8")		--ٽ
	
	local tSpeedXY, tSpeedZ = speed(tid())
	v["Ŀ꾲ֹ"] = rela("ж") and tSpeedXY <= 0 and tSpeedZ <= 0
	v["ĿѪ϶"] = rela("ж") and tlifevalue() > lifemax() * 10


	--ȴڹл
	if gettimer("Х") < 0.3 then return end

	--սὣ
	if nofight() and mount("ɽӽ") and v[""] < 30 then
		f["Х"]("սὣ")
	end

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

	--
	if getopt("") and dungeon() and nofight() then return end
	
	--˺
	if mount("ˮ") then
		f["ὣ"]()
	end
	if mount("ɽӽ") then
		f["ؽ"]()
	end

	--ûżܼ¼Ϣ
	if fight() and dis() < 4 and state("վ") and cdleft(16) <= 0 and castleft() <= 0 and gettimer("Ϧ׷") > 0.3 and gettimer("Ʒ") > 0.3 and gettimer("ɽ") > 0.3 then
		PrintInfo("----------ûż")
	end
end

f["ὣ"] = function()
	--ؽ
	if dis() < 8 then
		if v[""] >= 90 then
			f["Х"]("ؽ, ")
		end
		if v["ϪCD"] > 1 and v["CD"] > 1 then
			f["Х"]("ؽ, Ϫ")
		end
	end

	--ȴ
	if gettimer("´") < 0.3 or gettimer("") < 0.3 then
		if state("") then
			deltimer("´")
			deltimer("")
		else
			return
		end
	end

	--Ϫ
	if dis() < 4 then
		CastX("Ϫ")
	end

	--
	if v["ϪCD"] > 0 then
		CastX("")
	end

	--´
	if cdleft(16) >= 0.5 and face() < 80 then
		if aCastX("´", 180) then
			return
		end
	end

	--
	if face() < 80 then
		CastX("")
	end
end

f["ؽ"] = function()
	--Ʒ
	if gettimer("Ʒɶ") > 0.3 and v["ʱ"] > casttime("Ʒ") then
		CastX("Ʒ")
	end

	--ؽ Ʒ߽֮С30
	if gettimer("Ʒɶ") < 0.3 or gettimer("ͷƷ") < 1 or v[""] < 30 then
		f["ؽ"]()
	end

	--б̹Ϧ
	if v["зɽ"] or bufftime("̹") < casttime("Ϧ׷") + 0.5 then
		if bufftime("̹") > casttime("Ϧ׷") then
			CastX("Ϧ׷")
		end
	end

	--ײ
	if bufftime("") < casttime("Ϧ׷") then
		CastX("")
	end

	--Ϧ
	CastX("Ϧ׷")

	--̥
	CastX("")

	--[[ɽȺ, ȥ
	_, v["10ڵ"] = npc("ϵ:ж", "<9", "ѡ")
	if v["10ڵ"] >= 3 then
		CastX("ɽ")
	end
	--]]
end

f["ؽ"] = function()
	--շŹݺ
	if gettimer("ݺ") < 0.5 or gettimer("ͷݺ") < 2 then
		return
	end

	--ڽ
	if gettimer("") < 0.5 or gettimer("ͷŷ") < 1.5 or v[""] ~= 0 or buff("") then
		return
	end

	--з糵
	if gettimer("ɽ") < 0.5 or gettimer("ͷŷɽ") < 1 or v["зɽ"] then
		return
	end

	--շŹ
	if gettimer("") < 0.5 or buff("") then
		return
	end

	--
	if v["Ŀ꾲ֹ"] and dis() < 6 and v[""] < 60 then
		if CastX("") then
			return
		end
	end

	--ɽ
	if v["Ŀ꾲ֹ"] and v["ĿѪ϶"] and dis() < 6 then
		if v[""] < 60 or (v["ʱ"] > 8 and v[""] < 80) then
			if v["ڲ"] >= 3 and v["ʱ"] > 5 then
				if v["ݺʱ"] < 100 and cdtime("ɽ") <= 0 then
					CastX("ݺ")
				end
				if CastX("ɽ") then
					return
				end
			end
		end
	end

	--ὣؽ
	if v["ϪCD"] < 1 and v["Хճʱ"] < 1 and v[""] < 60 then
		f["Х"]("ὣؽ")
	end

	--ݺ
	if cdleft(16) <= 0 and v["CD"] > 0 and v["ϪCD"] > 0 and v[""] < 60 and dis() < 8 then
		if CastX("ݺ") then
			return
		end
	end

	--
	if dis() < 8 and v[""] < 60 then
		CastX("")
	end
end

f["Х"] = function(szReason)
	if CastX("Х") then
		print("------------------------------ "..szReason)	--ָ лԭ
		exit()
	end
end

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

--¼Ϣ
function PrintInfo(s)
	local t = {}
	if s then t[#t+1] = s end
	t[#t+1] = ":"..v[""]
	t[#t+1] = ":".. format("%0.1f", dis())

	t[#t+1] = ":"..v[""]..", "..v["ʱ"]
	t[#t+1] = ":"..v["ʱ"]
	t[#t+1] = ":"..v["ڲ"]..", "..v["ʱ"]
	t[#t+1] = "̹:"..v["̹"]..", "..v["̹ʱ"]
	t[#t+1] = ":"..v["Ŀ꾪ʱ"]
	t[#t+1] = ":"..v["ʱ"]

	t[#t+1] = "ϪCD:"..v["ϪCD"]
	t[#t+1] = "ХCD:"..v["Хճܴ"]..", "..v["Хճʱ"]
	t[#t+1] = "ƷCD:"..v["ƷCD"]
	t[#t+1] = "糵CD:"..v["糵CD"]
	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 aCastX(szSkill, nAngle)
	if acast(szSkill, nAngle) then
		settimer(szSkill)
		if v["¼Ϣ"] then PrintInfo() end
		return true
	end
	return false
end

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

--лڹ
function OnMountKungFu(KungFu, Level)
	deltimer("Х")
end

--ͷż
function OnCast(CasterID, SkillName, SkillID, SkillLevel, TargetType, TargetID, PosY, PosZ, StartFrame, FrameCount)
	if CasterID == id() then
		if SkillID == 1593 then		--Ʒ
			settimer("ͷƷ")
		end
		if SkillID == 1663 then		--ݺ
			settimer("ͷݺ")
		end
		if SkillID == 25070 then	--
			settimer("ͷŷ")
		end
		if SkillID == 18333 then	--ɽ
			settimer("ͷŷɽ")
		end
	end
end

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