function OnYuyuko01SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local casterPoint = caster:GetAbsOrigin()
	local range = caster:Script_GetAttackRange()

	local powerChance = 0
	local pv = caster:GetAbilityPowerValue("thtd_yuyuko_02")
	if pv ~= nil then 					
		powerChance = pv[1]	
	end
		
	caster.thtd_yuyuko_kill_chance = caster:GetAbilityValue("thtd_yuyuko_02", "chance") + powerChance	

	caster:EmitSound("Sound_THTD.thtd_yuyuka_01")

	local count = keys.max_count
	local tick = keys.tick_time
	
	if caster:IsPower666() then 
		count = count + 3 
	end

	caster:SetContextThink(DoUniqueString("ability_yuyuko_01_casted"), 
		function()
			if GameRules:IsGamePaused() then return 0.1 end
			if not THTD_IsValid(caster) then 
				return nil
			end
			if count <= 0 then 
				return nil 
			end		

			local targets = THTD_FindUnitsInRadius(caster,casterPoint,range)
			for k,v in pairs(targets) do
				local info = 
				{
					Target = v,
					Source = caster,
					Ability = keys.ability,	
					EffectName = "particles/heroes/thtd_yuyuko/ability_yuyuko_01.vpcf",
					iMoveSpeed = 750,
					vSourceLoc= caster:GetAbsOrigin(),                -- Optional (HOW)
					bDrawsOnMinimap = false,                          -- Optional
					bDodgeable = true,                                -- Optional
					bIsAttack = false,                                -- Optional
					bVisibleToEnemies = true,                         -- Optional
					bReplaceExisting = false,                         -- Optional
					flExpireTime = GameRules:GetGameTime() + 10,      -- Optional but recommended
					bProvidesVision = true,                           -- Optional
					iVisionRadius = 400,                              -- Optional
					iVisionTeamNumber = caster:GetTeamNumber()        -- Optional
				}
				local projectile = ProjectileManager:CreateTrackingProjectile(info)
				ParticleManager:DestroyLinearProjectileSystem(projectile,false)
			end

			count = count - 1
			return tick
		end, 
	0)	
end

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

	caster:EmitSound("Hero_Necrolyte.ProjectileImpact")

	if RollPercentage(caster.thtd_yuyuko_kill_chance) then 
		caster:AbilityKill(target, keys.ability)		
	else
		local DamageTable = {
			ability = keys.ability,
			victim = target, 
			attacker = caster, 
			damage = caster:THTD_GetAbilityPowerDamage(keys.ability), 
			damage_type = keys.ability:GetAbilityDamageType(), 
			damage_flags = DOTA_DAMAGE_FLAG_NONE
		}
		UnitDamageTarget(DamageTable)
	end	
end

function OnSpellStartYuyuko03(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local vecCaster = caster:GetOrigin()
	local vecForward = caster:GetForwardVector() 
	local targetPoint = keys.target_points[1]
	caster.thtd_yuyuko_03_target_point = targetPoint	
	caster.thtd_yuyuko_03_think_count = 0

	caster:EmitSound("Voice_Thdots_Yuyuko.AbilityYuyuko04")

	local unit = CreateUnitByName(
		"npc_dota2x_unit_yuyuko_04"
		,caster:GetOrigin() - vecForward * 100
		,false
		,caster
		,caster
		,caster:GetTeam()
	)
	local ability_dummy_unit = unit:FindAbilityByName("ability_dummy_unit")
	ability_dummy_unit:SetLevel(1)
	
	unit:StartGesture(ACT_DOTA_IDLE)
	local forwardRad = GetRadBetweenTwoVec2D(caster:GetOrigin(),unit:GetOrigin())
	unit:SetForwardVector(Vector(math.cos(forwardRad+math.pi/2),math.sin(forwardRad+math.pi/2),0))

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_yuyuko/ability_yuyuko_04.vpcf", PATTACH_CUSTOMORIGIN, unit)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local effectIndex2 = ParticleManager:CreateParticle("particles/heroes/yuyuko/ability_yuyuko_04_effect_d.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex2, 0, caster:GetOrigin())
	ParticleManager:DestroyParticleSystemTime(effectIndex2,2.0)

	unit:SetContextThink(DoUniqueString("thtd_yuyuko_03_unit_remove"), 
		function () 
			if GameRules:IsGamePaused() then return 0.1 end
			unit:RemoveSelf()
			return nil
		end, 
	5.0)


	local powerChance = 0
	local pv = caster:GetAbilityPowerValue("thtd_yuyuko_02")
	if pv ~= nil then 					
		powerChance = pv[1]	
	end
		
	caster.thtd_yuyuko_kill_chance = caster:GetAbilityValue("thtd_yuyuko_02", "chance") + powerChance	
	local ability = caster:FindAbilityByName("thtd_yuyuko_01")

	caster:SetContextThink(DoUniqueString("ability_yuyuko_01_casted"), 
		function()
			if GameRules:IsGamePaused() then return 0.1 end
			if not THTD_IsValid(caster) then 
				return nil
			end
			if not caster:HasModifier("modifier_yuyuko_03_think") then 
				return nil 
			end

			local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.damage_radius)
			for k,v in pairs(targets) do
				local info = 
				{
					Target = v,
					Source = caster,
					Ability = ability,	
					EffectName = "particles/heroes/thtd_yuyuko/ability_yuyuko_01.vpcf",
					iMoveSpeed = 750,
					vSourceLoc= caster:GetAbsOrigin(),                -- Optional (HOW)
					bDrawsOnMinimap = false,                          -- Optional
					bDodgeable = true,                                -- Optional
					bIsAttack = false,                                -- Optional
					bVisibleToEnemies = true,                         -- Optional
					bReplaceExisting = false,                         -- Optional
					flExpireTime = GameRules:GetGameTime() + 10,      -- Optional but recommended
					bProvidesVision = true,                           -- Optional
					iVisionRadius = 400,                              -- Optional
					iVisionTeamNumber = caster:GetTeamNumber()        -- Optional
				}
				local projectile = ProjectileManager:CreateTrackingProjectile(info)
				ParticleManager:DestroyLinearProjectileSystem(projectile,false)
			end

			return 0.25
		end, 
	0)
end

function OnThinkYuyuko03(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = caster.thtd_yuyuko_03_target_point
	

	local powerDamage = 0
	local pv = caster:GetAbilityPowerValue("thtd_yuyuko_03")
	if pv ~= nil then 					
		powerDamage = pv[1]	
	end	

	caster.thtd_yuyuko_03_health_kill = keys.health_kill + powerDamage	
	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.damage_radius) 
	for _,v in pairs(targets) do  
		if caster.thtd_yuyuko_03_think_count%4 == 0 then			
			local effectIndex = ParticleManager:CreateParticle("particles/thd2/heroes/yuyuko/ability_yuyuko_04_effect.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, v:GetOrigin())
			ParticleManager:DestroyParticleSystem(effectIndex,false)
		end
		   
		if v.thtd_damage_lock ~= true and v:HasModifier("modifier_yuyuko04_killed") ~= true and v:GetHealthPercent() <= caster.thtd_yuyuko_03_health_kill then 
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_yuyuko04_killed", {})

			local effectIndex = ParticleManager:CreateParticle("particles/thd2/heroes/yuyuko/ability_yuyuko_04_effect_a.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, v:GetOrigin())
			ParticleManager:SetParticleControl(effectIndex, 5, v:GetOrigin())
			ParticleManager:DestroyParticleSystem(effectIndex,false)

	   		local effectIndex2 = ParticleManager:CreateParticle("particles/heroes/yuyuko/ability_yuyuko_04_effect_d.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex2, 0, v:GetOrigin())
			ParticleManager:DestroyParticleSystemTime(effectIndex2,2.0)

			caster:AbilityKill(v, keys.ability)
		end
	end

	caster.thtd_yuyuko_03_think_count = caster.thtd_yuyuko_03_think_count + 1
end
