local ran_03_black_list =
{
	"lily",
	"daiyousei",	
}

function IsInRan03BlackList(unit)
	for k,v in pairs(ran_03_black_list) do
		if unit:GetUnitName() == v then
			return true
		end
	end
	return false
end

local ran_03_ability_black_list =
{
	"thtd_ran_01",
}

function IsInRan03AbilityBlackList(ability)
	for k,v in pairs(ran_03_ability_black_list) do
		if ability:GetAbilityName() == v then
			return true
		end
	end
	return false
end


function Ran01_OnSpellStart(keys)	
	local caster = keys.caster
	local target = keys.target

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/ran/ability_ran_03_laser.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControlEnt(effectIndex , 0, caster, 5, "attach_hitloc", Vector(0,0,0), true)
	ParticleManager:SetParticleControlEnt(effectIndex , 1, target, 5, "attach_hitloc", Vector(0,0,0), true)
	ParticleManager:SetParticleControlEnt(effectIndex , 9, caster, 5, "attach_hitloc", Vector(0,0,0), true)

	local target_point = target:GetOrigin()

	local damage_table={
			victim = target, 
			attacker = caster, 
			ability = keys.ability,
			damage = caster:THTD_GetAbilityPowerDamage(keys.ability),
			damage_type = keys.ability:GetAbilityDamageType(),
			damage_flags = DOTA_DAMAGE_FLAG_NONE
		}
	UnitDamageTarget(damage_table)

	local target_unit = target	
	local jump_count = 1
	local jumpAmount = keys.JumpCount

	if target_unit ~= nil and target_unit:IsNull() == false and target_unit:IsAlive() and jumpAmount > 1 then
		caster:SetContextThink("ran01_lazer_jumping",
			function ()
				if GameRules:IsGamePaused() then return 0.03 end
				local enemies = THTD_FindUnitsInRadius(caster,target_point,keys.JumpRadius)

				local next_target=nil
				for _,v in pairs(enemies) do
					if v~=nil and v:IsNull()==false and v:IsAlive() then
						if v~=target_unit then
							next_target=v
							break
						end
					end
				end
				if next_target then
					-- target_unit:EmitSound("Hero_Tinker.Laser")
					effectIndex = ParticleManager:CreateParticle("particles/heroes/ran/ability_ran_03_laser.vpcf", PATTACH_CUSTOMORIGIN, caster)
					ParticleManager:SetParticleControlEnt(effectIndex , 0, target_unit, 5, "attach_hitloc", Vector(0,0,0), true)
					ParticleManager:SetParticleControlEnt(effectIndex , 1, next_target, 5, "attach_hitloc", Vector(0,0,0), true)
					ParticleManager:SetParticleControlEnt(effectIndex , 9, target_unit, 5, "attach_hitloc", Vector(0,0,0), true)

					target_unit = next_target
					target_point = next_target:GetOrigin()

					local DamageTable={
						victim = target_unit, 
						attacker = caster, 
						ability = keys.ability,
						damage = caster:THTD_GetAbilityPowerDamage(keys.ability),
						damage_type = keys.ability:GetAbilityDamageType(),
						damage_flags = DOTA_DAMAGE_FLAG_NONE
					}
					UnitDamageTarget(DamageTable)
					-- next_target:EmitSoundParams("Hero_Tinker.LaserImpact",1.0,0.4,2.0)
				end

				jump_count=jump_count+1
				if jump_count >= jumpAmount or next_target == nil or next_target:IsNull() or next_target:IsAlive() == false then 
					return nil
				end
				return keys.JumpInterval
			end,
		keys.JumpInterval)
	end
end

function OnRan03SpellStart(keys)
	local buffCaster = EntIndexToHScript(keys.caster_entindex)  --本buff来源者
	if keys.ability:GetLevel() < 1 then  --本buff来源技能
		return
	end

	local caster = keys.unit  --本次触发的技能施放者
	local ability = keys.event_ability --本次触发的施放的技能
	local manaCost = caster:GetRealManaCost(ability)
	if manaCost <= 0 then
		return
	end
	if IsInRan03BlackList(keys.unit) or IsInRan03AbilityBlackList(keys.event_ability) then 
		return
	end

	if keys.mana_regen > 0 then
		caster:GiveMana(manaCost * keys.mana_regen/100)
	end	

	local ability01 = buffCaster:FindAbilityByName("thtd_ran_01")
	ability01:EndCooldown()
	buffCaster:GiveMana(buffCaster:GetRealManaCost(ability01))
	
	local enemies = THTD_FindUnitsInRadius(buffCaster,buffCaster:GetOrigin(),buffCaster:GetAbilityValue("thtd_ran_01", "range"))
	if #enemies > 0 then
		local unit = nil
		for _,v in pairs(enemies) do
			if v~=nil and v:IsNull()==false and v:IsAlive() then
				unit = v
				break
			end
		end
		if unit ~= nil then   
			buffCaster:CastAbilityOnTarget(unit,ability01,buffCaster:GetPlayerOwnerID())
		end
	end

	-- local effectIndex = ParticleManager:CreateParticle("particles/heroes/ran/ability_ran_04_buff.vpcf", PATTACH_CUSTOMORIGIN, caster)
	-- ParticleManager:SetParticleControlEnt(effectIndex , 0, keys.unit, 5, "follow_origin", Vector(0,0,0), true)
	-- ParticleManager:DestroyParticleSystemTimeFalse(effectIndex,2.0)
	-- 太吵，改为只有蓝显示
	if buffCaster.thtd_ran_03_effect ~= true then 
		buffCaster.thtd_ran_03_effect = true
		local effectIndex = ParticleManager:CreateParticle("particles/heroes/ran/ability_ran_04_buff.vpcf", PATTACH_CUSTOMORIGIN, buffCaster)
		ParticleManager:SetParticleControlEnt(effectIndex , 0, buffCaster, 5, "follow_origin", Vector(0,0,0), true)
		ParticleManager:DestroyParticleSystemTimeFalse(effectIndex,2.0)
		buffCaster:SetContextThink(DoUniqueString("thtd_ran_03"), 
			function()
				buffCaster.thtd_ran_03_effect = nil
				return nil
			end,
		2.0)
	end
end