--[[
[]2 [Ĺ]3 []2 
[꼯]2 [ţ]2 []3
[׽Ӱ]3 [巽о]1 [˻]2
[]1 [⽭ɽ]1
[]1 [ȫ]2 []1 [ʵ]1
[̱]1 []1
[׺]3 []2
[]1 [ٷ]1
--]]

--****************************1μ, 1ϼ1GCDȶ3

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

--ûս, ڹؾҪر
addopt("", false)

--
local v = {}
v[""] = 0
v["ȴͬ"] = false

--ѭ
function Main()
	--
	if fight() then
		--
		if life() < 0.6 and buffstate("Ч") < 40 and nobuff("") then
			fcast("")
		end

		--ɽ
		if life() < 0.35 then
			if fcast("ɽ", true) then
				stopmove()
				bigtext("ɽ", 2, 2)
			end
		end
	end

	--
	local mapName = map()
	local func = tMapFunc[mapName]
	if func then
		func(g_player)
	end

	--Զ
	if tbuffstate("ɴ") and tcastleft() < 1 then
		fcast("Զ")
	end

	--Ϸ
	if casting("϶") then
		if castleft() < 0.13 then
			settimer("϶")
		end
		return
	end

	--ʼ
	v[""] = qidian()
	v[""] = npc(":", "ɫ<3")
	v["ʣʱ"] = bufftime("51072") - 8
	local tSpeedXY, tSpeedZ = speed(tid())
	v["Ŀ꾲ֹ"] = tSpeedXY == 0 and tSpeedZ == 0		--xyٶȺZٶȶΪ0
	v["Ʋ"], v["Ʋʱ"] = qc("Ʋ", id(), id())		--ԼΧԼƲ


	--õȴͬ
	if casting("ֻ") and castleft() < 0.13 and v[""] == 7 then
		SetWait()
	end

	--Ʋ
	if rela("ж") and dis() < 20 then
		if v["Ʋ"] > -1 or v["Ʋʱ"] < 1 then		--ûƲ߿Ȧ, ԼԵľ룬ȦȦǸ
			fcast("Ʋ", true)
		end
	end

	--ȴͬ
	if v["ȴͬ"] and gettimer("ȴͬ") <= 0.25 then
		print("ȴͬ")
		return
	end

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

	if tbuff("") then
		bigtext("Ŀ޵", 0.5)
		return
	end

	--תһ, ˺
	if cdleft(16) > 0.5 then
		cast("תһ")
	end

	--ƾ
	if fight() and rela("ж") and dis() < 20 then
		cast("ƾ")
	end

	--Ԫȱ
	if fight() and mana() < 0.45 and v[""] >= 9 then
		if fcast("Ԫȱ") then
			fcast("")
		end
	end

	--躹
	if rela("ж") and dis() < 20 and bufftime("") > 7.5 and cdleft(16) < 0.5 then
		fcast("躹")
	end

	--ǻ
	if v[""] >= 8 then
		if fcast("ǻ") then
			fcast("")
		end
	end
	
	--϶
	if rela("ж") and v[""] ~= 0 and gettimer("϶") > 0.5 then
		if v["Ʋ"] < -1 and v["Ʋʱ"] > 3 and (v["Ŀ꾲ֹ"] or v["ʣʱ"] < 5) then
			--
			if v["Ŀ꾲ֹ"] and dis2() < 19.5 and visible() and cdleft(16) <= 0 then	--
				if v[""] == 0 and v["ʣʱ"] > 3 then		--ܴ3θ
					if buffsn("") >= 5 and bufftime("") > 3 then	--5㾭, ûҪע͵
						fcast("")
					end
				end
			end

			fcast("϶")
		end
	end

	--ֻ
	if fcast("ֻ") then
		if buff("꼯") then
			fcast("")
		end
	end

	--̫޼
	if cast("̫޼") then
		cast("")
	end

	--, 1.5CDܼӰ
	if cdleft(16) > 1 then
		cast("")
	end
end

--ȴ־
function SetWait()
	v["ȴͬ"] = true
	settimer("ȴͬ")
	exit()
end

--
function OnQidianUpdate()
	v["ȴͬ"] = false
	--print("OnQidianUpdate", qidian())
end

--buff
function OnBuff(CharacterID, BuffName, BuffID, BuffLevel, StackNum, SkillSrcID, StartFrame, EndFrame, FrameCount)
	if CharacterID == id() and StackNum  > 0 then	--Լ 
		if BuffID == 51072 then	--ÿγָۻbuff
			v[""] = 0
		end
	end
end

--ͷż
function OnCast(CasterID, SkillName, SkillID, SkillLevel, TargetID, nStartFrame, nFrameCount)
	if CasterID == id() then
		if SkillID == 64144 then	--϶𡤸, ÿθ۳ֻЧ3
			v[""] = v[""] + 1
		end
		--print("OnCast -> "..": "..SkillName..", ID: "..SkillID..", ȼ: "..SkillLevel..", Ŀ: "..TargetID..", ʼ֡: "..nStartFrame..", ǰ֡: "..nFrameCount)
	end
end

--NPC볡
function OnNpcEnter(NpcID, NpcName, NpcTemplateID, NpcModelID, EmployerID)
	if EmployerID == id() and NpcTemplateID == 201578 then
		print("OnNpcEnter->"..NpcName..", NPCID: "..NpcID..", ģID: "..NpcTemplateID..", ID: "..NpcModelID)
	end
end

-------------------------------------------------
tMapFunc = {}

tMapFunc["ؾ"] = function(g_player)
	--
	if tcasting("") then
		stopcasting()
		if tcastleft() < 0.5 then
			cast("")
			cast("ӭ")
			cast("ʤ")
			cast("̨")
		end
		exit()
	end

	--
	if tcasting("") and tcastleft() < 0.5 then
		settimer("Ŀ")
	end
	if gettimer("Ŀ") < 1 or tbuff("4147") then	-- ˺
		stopcasting()
		exit()
	end
end
