function OnReisen01AttackLanded(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target

	ReisenRepelUnit(caster, target)
end

function ReisenRepelUnit(caster, target)
	if target.thtd_is_fearing == true then return end
	if target.next_move_point ~= nil and target.thtd_is_feared_by_reisen_01 ~= true then		
		
		target.thtd_is_feared_by_reisen_01 = true
		target.thtd_is_fearing = true
		local current_next_move_point = target.next_move_point	

		target.next_move_point = target:GetOrigin() - target:GetForwardVector() * 500

		target:EmitSound("Hero_Sniper.ProjectileImpact")

		local count = 20	
		target:SetContextThink(DoUniqueString("thtd_reisen01_move_next_point"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				if not IsValidAlive(target) then 
					return nil 
				end
				count = count - 1
				if count <= 0 or THTD_IsValid(caster) == false then
					target.next_move_point = current_next_move_point																
					target.thtd_is_fearing = false
					return nil
				end				
				return 0.1
			end, 
		0)
	end
end

function OnReisen02AttackLanded(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target

	if caster.thtd_reisen_02_illusion_count == nil then
		caster.thtd_reisen_02_illusion_count = 0
	end	

	if caster.thtd_reisen_02_illusion_count < keys.max_count and RollPercentage(keys.chance) then
		caster:EmitSound("Sound_THTD.thtd_reisen_02")
		local illusion = CreateUnitByName(
				"reisen_illusion", 
				caster:GetOrigin() + RandomVector(150), 
				false, 
				caster:GetOwner(), 
				caster:GetOwner(), 
				caster:GetTeam() 
			)
		illusion.thtd_spawn_unit_owner = caster
		illusion:SetControllableByPlayer(caster:GetPlayerOwnerID(), true) 
		local count = 0
		keys.ability:ApplyDataDrivenModifier(caster, illusion, "modifier_reisen_02_illusion", nil)
		illusion:SetBaseDamageMax(caster:THTD_GetAttack())
		illusion:SetBaseDamageMin(caster:THTD_GetAttack())
		illusion:MoveToPositionAggressive(illusion:GetOrigin() + illusion:GetForwardVector() * 100)
		illusion:SetContextThink(DoUniqueString("thtd_reisen02_illusion"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				if count > 20 then 
					illusion:AddNoDraw()
					illusion:ForceKill(true)
					caster.thtd_reisen_02_illusion_count = caster.thtd_reisen_02_illusion_count - 1
					return nil				
				end
				count = count + 1
				return 0.5
			end, 
		0)
		caster.thtd_reisen_02_illusion_count = caster.thtd_reisen_02_illusion_count + 1
	end
end

function OnReisen03SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target

	caster:EmitSound("Sound_THTD.thtd_reisen_03")

	if caster.thtd_reisen_02_illusion_count == nil then
		caster.thtd_reisen_02_illusion_count = 0
	end	

	local targets = THTD_FindUnitsInRadius(caster,target:GetOrigin(),keys.range)
	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * (1 + caster.thtd_reisen_02_illusion_count * keys.damage_up/100)
	local playerid = caster:GetPlayerOwnerID()
	if GameRules.player_bb_buff[playerid]["item_3030"] > 0 then
		damage = damage * (1 + GameRules.player_bb_buff[playerid]["item_3030"]/100)
	end
	for k,v in pairs(targets) do
		Reisen03RepelUnit(caster, v)
		local DamageTable = {
   			ability = keys.ability,
            victim = v, 
            attacker = caster, 
            damage = damage, 
            damage_type = keys.ability:GetAbilityDamageType(), 
            damage_flags = DOTA_DAMAGE_FLAG_NONE
	   	}
	   	UnitDamageTarget(DamageTable)
	end

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_reisen/ability_reisen_03.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 1, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 3, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 9, caster:GetOrigin())
	ParticleManager:DestroyParticleSystemTime(effectIndex,2.0)	
end

function Reisen03RepelUnit(caster, target)
	if target.thtd_is_fearing == true then return end
	if target.next_move_point ~= nil then	
		target.thtd_is_feared_by_reisen_01 = true
		target.thtd_is_fearing = true
		local current_next_move_point = target.next_move_point	

		target.next_move_point = target:GetOrigin() - target:GetForwardVector() * 500		

		target:EmitSound("Hero_Sniper.ProjectileImpact")

		local count = 20	
		target:SetContextThink(DoUniqueString("thtd_reisen01_move_next_point"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				if not IsValidAlive(target) then 
					return nil 
				end
				count = count - 1
				if count <= 0 or THTD_IsValid(caster) == false then
					target.next_move_point = current_next_move_point																
					target.thtd_is_fearing = false
					return nil
				end				
				return 0.1
			end, 
		0)		
	end
end