function OnCirno02SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local damagedGroup = {}

	Cirno02PassToNextUnit(keys,target,damagedGroup)
end

function Cirno02PassToNextUnit(keys,target,damagedGroup)
	if keys.ability == nil or keys.ability:IsNull() then
		damagedGroup = {}
		return
	end
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targets = THTD_FindUnitsInRadius(caster,target:GetOrigin(),keys.radius)
	
	local powerCount = 0
	local powerDamage = 1
	local pv = caster:GetAbilityPowerValue("thtd_cirno_02")
	if pv ~= nil then 		
		powerDamage = pv[1]	
		powerCount = pv[2]
	end

	caster.thtd_cirno02_power_crit = powerDamage

	if targets[1]==nil or #damagedGroup >= keys.unit_max_count then
		damagedGroup = {}
		return
	end
	if #damagedGroup == 0 then
		table.insert(damagedGroup,target)
		local count = keys.ice_count + powerCount
		caster:SetContextThink(DoUniqueString("thtd_cirno02_projectile"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				local info = 
				{
					Target = target,
					Source = caster,
					Ability = keys.ability,	
					EffectName = "particles/heroes/thtd_cirno/ability_cirno_02.vpcf",
			    	iMoveSpeed = 1400,
					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)
				if count > 0 then
					count = count - 1
					return 0.05
				end
				return nil
			end, 
		0.05)
		caster:SetContextThink(DoUniqueString("thtd_cirno02_projectile"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				Cirno02PassToNextUnit(keys,target,damagedGroup)
				return nil
			end, 
		0.5)
	else
		for k,v in pairs(targets) do
			if v~=nil and v:IsNull()==false and THTD_IsUnitInGroup(v,damagedGroup) == false then
				table.insert(damagedGroup,v)
				local count = keys.ice_count + powerCount
				caster:SetContextThink(DoUniqueString("thtd_cirno02_projectile"), 
					function()
						if GameRules:IsGamePaused() then return 0.03 end
						local info = 
						{
							Target = v,
							Source = target,
							Ability = keys.ability,	
							EffectName = "particles/heroes/thtd_cirno/ability_cirno_02.vpcf",
							iMoveSpeed = 1400,
							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
						}
						projectile = ProjectileManager:CreateTrackingProjectile(info)
						ParticleManager:DestroyLinearProjectileSystem(projectile,false)
						if count > 0 then
							count = count - 1
							return 0.05
						end
						return nil
					end, 
				0.05)
				caster:SetContextThink(DoUniqueString("thtd_cirno02_projectile"), 
					function()
						if GameRules:IsGamePaused() then return 0.03 end
						Cirno02PassToNextUnit(keys,v,damagedGroup)
						return nil
					end, 
				0.1)
			end
		end
	end
end

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

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * caster.thtd_cirno02_power_crit
	if caster:IsPower999() then 
		damage = damage * 9
	end

	local DamageTable = {
			ability = keys.ability,
			victim = target, 
			attacker = caster, 
			damage = damage, 
			damage_type = keys.ability:GetAbilityDamageType(), 
			damage_flags = DOTA_DAMAGE_FLAG_NONE
	}
	UnitDamageTarget(DamageTable)
	
	if RollPercentage(keys.chance) then
		local stuntime = keys.StunTime
		if caster:THTD_IsTowerEx() == true then 
			stuntime = stuntime * (1 + keys.stun_up/100)
		end
		keys.ability:ApplyDataDrivenModifier(caster,target,"modifier_cirno_frozen_unit", {Duration = stuntime})		 
	end
	
  if target.thtd_ability_cirno_02_damaged ~= true then
   		target.thtd_ability_cirno_02_damaged = true

   		target:SetContextThink(DoUniqueString("thtd_ability_cirno_02_damaged"), 
   			function()
   				if GameRules:IsGamePaused() then return 0.03 end
   				target.thtd_ability_cirno_02_damaged = false
   				return nil
   			end, 
   		3.0)
	   	local effectIndex = ParticleManager:CreateParticle("particles/heroes/cirno/ability_cirno_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
			ParticleManager:DestroyParticleSystem(effectIndex,false)

			caster:EmitSound("Sound_THTD.thtd_cirno_02")
	end
end

function OnCirno03Attack(keys)
	if keys.ability:GetLevel() < 1 then return end
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/cirno/ability_cirno_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)
	if caster:IsPower666() then 
		damage = damage + 69 * caster:THTD_GetStarDamage()
	end
	if caster:IsPower999() then 
		damage = damage * 9
	end
	local targets = THTD_FindUnitsInRadius(caster,target:GetOrigin(),keys.range)	
	for k,v in pairs(targets) do
		-- caster:PerformAttack(v,true,false,true,false,false,false,true) 造成攻击但不显示弹道
		if caster:IsPower666() then 
			local effectIndex = ParticleManager:CreateParticle("particles/units/heroes/hero_crystalmaiden/maiden_freezing_field_explosion.vpcf", PATTACH_CUSTOMORIGIN, v)
			ParticleManager:SetParticleControl(effectIndex, 0, v:GetOrigin())
			ParticleManager:DestroyParticleSystem(effectIndex,false)
		end

		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
end

function OnCirno04Attack(keys)
	if keys.ability:GetLevel() < 1 then return end
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local targetPoint = target:GetOrigin()

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

	caster.thtd_cirno_04_attack_count = caster.thtd_cirno_04_attack_count + 1

	if caster.thtd_cirno_04_attack_count >= keys.max_count then
		caster.thtd_cirno_04_attack_count = 0
		local count = 1
		
		local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_cirno/ability_cirno_04_circle.vpcf", PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
		ParticleManager:DestroyParticleSystem(effectIndex,false)
	
		local powerDamage = 1
		local pv = caster:GetAbilityPowerValue("thtd_cirno_04")
		if pv ~= nil then 		
			powerDamage = pv[1]				
		end

		local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * powerDamage			
		if caster:IsPower999() then 
			damage = damage * 9
		end

		caster:SetContextThink(DoUniqueString("thtd_cirno_04_spell_start"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				if count%5 == 0 then 
					local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)					
					for k,v in pairs(targets) do
						local DamageTable = {
							ability = keys.ability,
							victim = v, 
							attacker = caster, 
							damage = damage * 5 * 0.05, 
							damage_type = keys.ability:GetAbilityDamageType(), 
							damage_flags = DOTA_DAMAGE_FLAG_NONE
						}
						UnitDamageTarget(DamageTable)
					end
				end
				if count > 40 then
					return nil
				end
				caster:EmitSound("Sound_THTD.thtd_cirno_04")
				count = count + 1
				return 0.05
			end,
		0.05)
	end
end

function OnCreatedCirnoExBuff(keys)	
	keys.target:AddDamageOutgoingMagical(keys.damage_up, "thtd_cirno_ex_damage_up")
end

function OnDestroyCirnoExBuff(keys)		
	keys.target:AddDamageOutgoingMagical("thtd_cirno_ex_damage_up")
end