--[[
[]2 [ѩ]2 []3
[]2 []2 []3
[Ҽ]1 []2 []2
[˪]2 [ҹ]1 []2
[]1 []2
[]1 []1
[¶]2 []3
[]2
--]]

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

--ѡ
addopt("", false)

--
local v = {}
v["Ŀ"] = 0
v["»ǰĿ"] = 0

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

	--»
	if casting("»к") then return end

	--ָ»ǰĿ
	if v["»ǰĿ"] ~= 0 then
		settar(v["»ǰĿ"])
		v["»ǰĿ"] = 0
	end

	if nobuff("") then
		cast("ķ")
	end

	if nofight() and nobuff("") then
		cast("", true)
	end

	--
	if fight() and life() < 0.6 and buffstate("Ч") < 40 then
		cast("صͰ")
	end

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

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

	--ʼ
	v["Ŀ꼱"] = tbuffsn("", id())
	if v["Ŀ"] ~= 0 and tid() == v["Ŀ"] then	--ϼĿbuffûͬͼ1
		v["Ŀ꼱"] = v["Ŀ꼱"] + 1
	end
	local tSpeedXY, tSpeedZ = speed(tid())
	v["Ŀ꾲ֹ"] = tSpeedXY == 0 and tSpeedZ == 0		--xyٶȺZٶȶΪ0
	v["ĿѪ϶"] = rela("ж") and tlifevalue() > lifemax() * 10		--ĿѪԼѪ10

	--»к, ҪԵǰĿͷŲЧ
	if v["ĿѪ϶"] and dis() < 25 and cdtime("»к") <= 0 and v["Ŀ꼱"] < 3 then
		--ӦbuffȼҪĿڹ
		v["»buffȼ"] = 1		--й, 50%, ⹦
		v["Ҫķ"] = "ڹ:Ѫս||̫齣||ˮ|ɽӽ|Ц"
		if tlife() > 0.5 then
			v["»buffȼ"] = 2	--», 50%, ڹ
			v["Ҫķ"] = "ڹ:Ѫս||̫齣||ˮ|ɽӽ|Ц"
		end

		if not g_player.IsHaveBuff(50372, v["»buffȼ"]) then		--»йʱ䵽˲ʧʱΪ, nobuff("»") ж
			v["»Ŀ"] = party("û״̬:", "Լ", v["Ҫķ"], "<18", "߿ɴ", "")
			if v["»Ŀ"] ~= 0 then
				v["»ǰĿ"] = tid()			--¼ǰĿ, »кָ
				if settar(v["»Ŀ"]) then
					cast("»к")
				end
			end
		end
	end

	--
	if tbuffstate("ɴ") then
		cast("ͨ")
	end

	--
	if fight() and buffsn("") <= 3 then
		cast("")
	end

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

	--[[ת ɢ GCDȥ
	if tbufftype("||Ԫ|") > 0 then
		fcast("ת")
	end
	--]]

	--
	if tbufftime("", id()) < 3 then
		cast("")
	end

	--
	if v["Ŀ꼱"] >= 3 then
		if tbufftime("", id()) > 9 or (v["Ŀ"] ~= 0 and tid() == v["Ŀ"]) then
			cast("")
		end
	end

	--
	if rela("ж") and dis() < 9 and v["Ŀ꾲ֹ"] and state("վ") and nobuff("") then
		cast("")
	end

	if v["Ŀ꼱"] < 3 then
		cast("")
	end

	if v["Ŀ꼱"] >= 2 then
		--
		_, v["Ŀ10߹"] = tnpc("ϵ:ж", "<10", "ѡ")
		if v["Ŀ10߹"] >= 2 then
			cast("")
		end
		
		--
		if v["ĿѪ϶"] and dis() < 19 and v["Ŀ꾲ֹ"] and state("վ") and cdtime("") <= 0 then
			cast("")
			
		end

		cast("")
	end
	
	cast("")
	cast("Ҽ")

	--ŲҼ(ƶ)
	cast("")
	cast("")
	cast("")
end


--ͷż
function OnCast(CasterID, SkillName, SkillID, SkillLevel, TargetType, TargetID, PosY, PosZ, StartFrame, FrameCount)
	if CasterID == id() then
		if SkillID == 3009 then
			v["Ŀ"] = TargetID	--¼ϼĿ
		end

		--[[
		if SkillID ~= 2341 then
			if TargetType == 2 then		--2 ָλ,  3 4 ָNPC
				print("OnCast->["..xname(CasterID).."] ͷ:"..SkillName..", ID:"..SkillID..", ܵȼ:"..SkillLevel..", Ŀλ:"..TargetID.."|"..PosY.."|"..PosZ..", ʼ֡:"..StartFrame..", ǰ֡:"..FrameCount)
			else
				print("OnCast->["..xname(CasterID).."] ͷ:"..SkillName..", ID:"..SkillID..", ܵȼ:"..SkillLevel..", ĿID:"..TargetID..", ʼ֡:"..StartFrame..", ǰ֡:"..FrameCount)
			end
		end
		--]]
	end
end

--buffб
function OnBuffList(CharacterID)
	--µԼϼĿ꣬ĿΪ0, Ӳ
	if CharacterID == v["Ŀ"] then
		v["Ŀ"] = 0
	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("Ŀ") < 2 or tbuff("4147") then	-- ˺
		cbuff("")	--ȡ
		stopcasting()		--ֹͣ
		turn(180)			--Ŀ
		exit()
	else
		if tname("") and face() > 60 then
			turn()
		end
	end
end
