--[[ Ѩ: [][][][龣][][][ʹ][][][Ϸ][][]
ؼ:
½  2 2˺
  2 2˺
  2 1˺ 1
Ҵ  2Ϣ 2˺
  1 1Ч(Χͷ) 2

պ, 15ڴ, ³龣ʱҪϴںҴ
5, սΧûһ, մ´5㺮, 2Ͳ
--]]

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

--ѡ
addopt("", false)
addopt("С60%ǧ֦", false)

--
local v = {}
v["¼Ϣ"] = true
v[""] = 0
v["Ҵ"] = 0
v["մ"] = 0
v["ǧ֦"] = false

--
local f = {}

--ѭ
function Main()
	--Զݼ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
	if casting("Ҵʱ") and castleft() < 0.13 then
		settimer("Ҵ")
	end

	--ʼ
	v["ҩ"] = yaoxing()	--´0 С0 -55
	v["ҩԾֵ"] = math.abs(v["ҩ"])
	local tSpeedXY, tSpeedZ = speed(tid())
	v["Ŀ꾲ֹ"] = rela("ж") and tSpeedXY <= 0 and tSpeedZ <= 0

	v["GCD"] = cdinterval(16)
	v["GCD"] = cdleft(16)
	v["͸֧"] = od("")
	v["͸֧ȴʱ"] = odtime("")
	v["CD"] = scdtime("")
	v["մCD"] = scdtime("մδ")
	v["ҴCD"] = scdtime("Ҵʱ")
	v["CD"] = scdtime("ѩ")
	v["CD"] = cdleft(2021)
	v["ԼCD"] = scdtime("Լ")
	v["ҶCD"] = scdtime("Ҷ")
	v["ҰCD"] = scdtime("Ұ")
	v["ӦCD"] = scdtime("Ӧҩ")

	v["Ҳ"] = tbuffsn("", id())
	v["ʱ"] = tbufftime("", id())
	v["ʹʱ"] = bufftime("ʹ")
	v["ʱ"] = bufftime("")
	v["½˲"] = buff("21106")
	v["Ұ"] = buff("24458")
	v["Ӧд"] = buffsn("24469")

	v["Լ"] = npc("ϵ:Լ", "ģID:106107")
	v["Ҷ"] = npc("ϵ:Լ", "ģID:106632")
	v["Ҷʱ"] = bufftime("20702")
	v["ƶ"] = keydown("MOVEFORWARD") or keydown("MOVEBACKWARD") or keydown("STRAFELEFT") or keydown("STRAFERIGHT") or keydown("JUMP")

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

	--ûս մ´5㺮
	if nofight() and v["ҩ"] > -5 then 
		if not rela("ж") or dis() > 35 and  v["GCD"] <= 0 then
			if npc("ϵ:ж", "<11") == 0 then	--11û
				CastX("մδ", true)
			end
		end
	end

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

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

	v["Ҷ"] = false
	if rela("ж") and buff("20071") then
		if (dis() < 10 and face() < 90) or (v["Լ"] ~= 0 and xxdis(v["Լ"], tid()) < 10) then	--ԼԼ10
			if v["Ҳ"] >= 8 and v["ʱ"] > 7 then
				v["Ҷ"] = true
				CastX("Ҷ")
			end
		end
	end

	--Ҷ
	if v["Ҷ"] then
		CastX("Ҷ")
	end

	--ѩ
	if rela("ж") then
		if dis() < 6 or (v["Լ"] ~= 0 and xxdis(v["Լ"], tid()) < 6) then	--ԼԼ6
			--if v["ʱ"] > 0 and v["ʹʱ"] > 0 and buff("ǧ֦") and gettimer("ǧ֦") > 0.3 then
			if v["ʱ"] > 0 and v["ʹʱ"] > 0 then
				if cdtime("ѩ") <= 0 then
					if CastX("Ұ") then	--Ұ, Ҷϵ
						CastX("ѩ")
					end
					if v["ҰCD"] > 6 then
						CastX("ѩ")
					end
				end
			end
		end
	end

	--
	if bufftime("21796") > casttime("") then
		f["ǧ֦"]("Ŵ", 0.25)
		CastX("")
	end

	--Լ
	if rela("ж") then
		CastX("Լ")
	end

	--Ҷ
	if rela("ж") then
		if CastX("Ҷ") then
			CastX("մδ")
		end
	end

	--ȴ½ͷ ҩͬ
	if gettimer("½") < 0.3 then
		--print("----------ȴ½ͷ")
		return
	end

	--
	if rela("ж") and dis() < 20 and gettimer("ڶ") > 0.5 and not v["ƶ"] then
		if v["ʱ"] < 12 then
			if v["ҩ"] <= -3 or (gettimer("ͷմδ") < 4 and v["ҩ"] - (5 -v["Ҵ"]) <= -3) then
				if cdtime("") <= 0 then
					f["ǧ֦"]("Ŵ", 0.25)
					if CastX("") then
						v[""] = 0
						stopmove()
						return
					end
				end
			end
		end
	end

	--Ҵʱ
	if rela("ж") and (v["Ŀ꾲ֹ"] or ttid() == id()) and dis() < 15 and not v["ƶ"] then
		if v["Ҳ"] >= 4 and v["ʱ"] > casttime("Ҵʱ") + v["GCD"] * 2 + 0.5 and not v["Ҷ"] then
			if v["CD"] > casttime("Ҵʱ") and v["ʹʱ"] > casttime("Ҵʱ") + casttime("") + 0.5 then
				if cdtime("Ҵʱ") <= 0 then
					f["ǧ֦"]("Ҵ", 0.4)
					if CastX("Ҵʱ") then
						v["Ҵ"] = 0
						stopmove()
						return
					end
				end
			end
		end
	end

	--
	if rela("ж") then
		if (dis() < 6 and face() < 90) or (v["Լ"] ~= 0 and xxdis(v["Լ"], tid()) < 6) then	--ԼԼ6
			if v["ҩ"] <= -3 or (gettimer("ͷմδ") < 4 and v["ҩ"] - (5 -v["Ҵ"]) <= -3) or v["CD"] > v["GCD"] * 2 then
				if v["Ҳ"] >= 8 and v["ʱ"] > 11 then
					if v["ʱ"] > 1 and v["ʹʱ"] > 1 then
						if cdtime("") <= 0 then
							f["ǧ֦"]("ź", 0.4)
							CastX("")
						end
					end
				end
			end
		end
	end

	--
	if v["ʹʱ"] < 3 or v["ҩ"] < -3 --[[or v["ҩ"] - (5 -v["Ҵ"]) < -3--]] then
		CastX("Ƕϳ")
	end

	--½
	CastX("½׺")

	f["ǧ֦"]()

	--ûżܼ¼Ϣ
	if fight() and rela("ж") and dis() < 20 and state("վ") and cdleft(16) <= 0 and castleft() <= 0 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 then
			PrintInfo("-- ûż")
		end
	end
end

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

f["ǧ֦"] = function(szReason, nMana)
	v["ǧ֦"] = false
	if buff("ǧ֦") and gettimer("ǧ֦") > 0.3 then
		return true
	end

	if mana() > nMana then
		if cast("ǧ֦") then
			print("-------------------- ǧ֦:"..szReason)
			return true
		end
	end
	return false
end

f["ǧ֦"] = function()
	if gettimer("") < 0.3 or casting("") or gettimer("ڶ") < 0.3 then return end
	if gettimer("Ҵʱ") < 0.3 or casting("Ҵʱ") or gettimer("Ҵ") < 0.3 then return end
	if gettimer("") < 0.3 or v["Ҷ"] or gettimer("ͷŷҶ") <= 0.5 then return end

	--  ҴCD
	if v["CD"] > 3 and v["CD"] > 3 and v["ҴCD"] > 3 then
		if not getopt("С60%ǧ֦") or mana() < 0.6 then
			if cbuff("ǧ֦") then
				settimer("ǧ֦")
			end
		end
	end

	--Ŀ겻ǵж
	if not rela("ж") then
		if cbuff("ǧ֦") then
			settimer("ǧ֦")
		end
	end
end

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

	t[#t+1] = ":"..v["Ҳ"]..", "..v["ʱ"]
	t[#t+1] = "ʹ:"..v["ʹʱ"]
	t[#t+1] = ":"..v["ʱ"]

	t[#t+1] = "CD:"..v["͸֧"]..", "..v["͸֧ȴʱ"]..", "..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["ҰCD"]
	t[#t+1] = "ǧ֦CD:"..scdtime("ǧ֦")
	
	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

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

--buff
function OnBuff(CharacterID, BuffName, BuffID, BuffLevel, StackNum, SkillSrcID, StartFrame, EndFrame, FrameCount)
	if CharacterID == id() then
		if BuffID == 20075 then		--ǧ֦
			deltimer("ǧ֦")
			if StackNum > 0 then
				print("-------------------- ǧ֦")
			else
				print("-------------------- ǧ֦")
			end
		end

		if StackNum <= 0 and BuffID == 20071 then
			v["ǧ֦"] = "buffƳ"
		end

		--[[buff
		if StackNum > 0 then
			print("buff->"..BuffName, BuffID, BuffLevel, SkillSrcID, StartFrame, (EndFrame -StartFrame) / 16, EndFrame, FrameCount)
		else
			print("Ƴbuff->"..BuffName, BuffID, BuffLevel)
		end
		--]]
	end
end

--ҩԸ
function OnYaoxingUpdate()
	deltimer("½")
end

--ж
function OnBreak(CharacterID)
	if CharacterID == id() then
		v["ǧ֦"] = "ж"
	end
end

--ͷż
function OnCast(CasterID, SkillName, SkillID, SkillLevel, TargetType, TargetID, PosY, PosZ, StartFrame, FrameCount)
	if CasterID == id() then
		--[[Լͷ
		if TargetType == 2 then
			print("OnCast->"..SkillName, "ID:"..SkillID, "ȼ:"..SkillLevel, "Ŀ:"..TargetID.."|"..PosY.."|"..PosZ, "ʼ֡:"..StartFrame, "ǰ֡:"..FrameCount)
		else
			print("OnCast->"..SkillName, "ID:"..SkillID, "ȼ:"..SkillLevel, "Ŀ:"..TargetID, "ʼ֡:"..StartFrame, "ǰ֡:"..FrameCount)
		end
		--]]

		if SkillID == 27578 then
			v["մ"] = 0
			settimer("ͷմδ")
		end

		if SkillID == 28114 then
			v["մ"] = v["մ"] + 1
			if v["մ"] >= 5 then
				deltimer("ͷմδ")
			end
		end

		if SkillID == 27556 then	--ÿ
			v[""] = v[""] + 1
			if v[""] >= 4 then
				v["ǧ֦"] = "ڶ"
			end
		end

		if SkillID == 27584 then	--Ҵÿ
			v["Ҵ"] = v["Ҵ"] + 1
			if v["Ҵ"] >= 5 then
				v["ǧ֦"] = "Ҵ"
			end
		end

		if SkillID == 28372 then
			v["ǧ֦"] = "ҶͷŽ"
		end

		if SkillID == 27637 then	--Ҷ
			settimer("ͷŷҶ")
		end
	end
end

local tNpcName = {
[106107] = "Լ",
[106632] = "Ҷ",
}

--NPC볡
function OnNpcEnter(NpcID, NpcName, NpcTemplateID, NpcModelID, EmployerID)
	if EmployerID == id() then
		local szName = tNpcName[NpcTemplateID]
		if szName then
			print("-------------------- OnNpcEnter->"..szName, frame())
		end
	end
end

--NPC뿪
function OnNpcLeave(NpcID, NpcName, NpcTemplateID, NpcModelID, EmployerID)
	if EmployerID == id() then
		local szName = tNpcName[NpcTemplateID]
		if szName then
			print("-------------------- OnNpcLeave->"..szName, frame())
		end
	end
end

--ս״̬ı
function OnFight(bFight)
	if bFight then
		print("--------------------ս")
	else
		--սǧ֦
		v["ǧ֦"] = "ս"

		--սѴ
		if dungeon() then
			bigtext("ǵô")
		end
		
		print("--------------------뿪ս")
	end
end
