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

	caster:EmitSound("Sound_THTD.thtd_tenshi_01")	
	
	local targets = THTD_FindUnitsInRadius(caster,target:GetOrigin(),keys.range)	

	local powerBonus = #targets * keys.bonus_attack_power
	if powerBonus > 0 then 
		caster:THTD_AddBasePower(powerBonus)
		caster:THTD_AddBaseAttack(powerBonus)
		caster:SetContextThink(DoUniqueString("dota_timer"), 
			function() 
				if GameRules:IsGamePaused() then return 0.1 end
				caster:THTD_AddBasePower(-powerBonus)
				caster:THTD_AddBaseAttack(-powerBonus)
				return nil 
			end, 
		keys.duration_time)
	end
	
	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)
	for k,v in pairs(targets) do
		local DamageTable_aoe = {
   			ability = keys.ability,
            victim = v, 
            attacker = caster, 
            damage = damage, 
            damage_type = keys.ability:GetAbilityDamageType(), 
            damage_flags = DOTA_DAMAGE_FLAG_NONE
	   	}
	   	UnitDamageTarget(DamageTable_aoe)	   	
	end
	
	local effectIndex = ParticleManager:CreateParticle("particles/econ/items/earthshaker/egteam_set/hero_earthshaker_egset/earthshaker_echoslam_start_fallback_low_egset.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 1, target:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end

function OnTenshi02AttackLanded(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target	
	
	local targets = THTD_FindUnitsInRadius(caster,target:GetOrigin(),keys.range)	
	local damage = caster:THTD_GetAttack()
	for k,v in pairs(targets) do
		local DamageTable_aoe = {
   			ability = keys.ability,
            victim = v, 
            attacker = caster, 
            damage = damage, 
            damage_type = keys.ability:GetAbilityDamageType(), 
            damage_flags = DOTA_DAMAGE_FLAG_NONE
	   	}
	   	UnitDamageTarget(DamageTable_aoe)	   	
	end

	if caster.thtd_attack_time == nil then
		caster.thtd_attack_time = GameRules:GetGameTime() - 10
	end
	if caster.thtd_attack_time < GameRules:GetGameTime() - 1 then
		caster.thtd_attack_time = GameRules:GetGameTime()
		local effectIndex = ParticleManager:CreateParticle("particles/econ/items/earthshaker/egteam_set/hero_earthshaker_egset/earthshaker_echoslam_start_fallback_low_egset.vpcf", PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
		ParticleManager:SetParticleControl(effectIndex, 1, target:GetOrigin())
		ParticleManager:DestroyParticleSystem(effectIndex,false)
	end	
end

-- 被动modifier会在所有自定变量前生效，造成不起作用，不要用Passive
function OnThinkTenshi02Buff(keys)
	local caster = keys.caster	

	local powerChance = 0
	local pv = caster:GetAbilityPowerValue("thtd_tenshi_02")
	if pv ~= nil then 
		powerChance = pv[1]			
	end
	local bonus = keys.chance + powerChance
	local add = bonus - (caster.thtd_tenshi_02_crit_chance or 0)
	if add ~= 0 then 
		caster:THTD_AddCritChance(add)
		caster.thtd_tenshi_02_crit_chance = bonus	
	end

	local bonus = keys.crit
	if caster:IsPower999() then 
		bonus = bonus * 2	
	end
	local add = bonus - (caster.thtd_tenshi_02_crit_damage or 0)
	if add ~= 0 then 
		caster:THTD_AddCritDamage(add)
		caster.thtd_tenshi_02_crit_damage = bonus
	end
end

function OnDestroyTenshi02Buff(keys)
	local caster = keys.caster	
	
	if caster.thtd_tenshi_02_crit_chance ~= nil then 
		caster:THTD_AddCritChance(-caster.thtd_tenshi_02_crit_chance)
		caster.thtd_tenshi_02_crit_chance = nil
	end	
	if caster.thtd_tenshi_02_crit_damage ~= nil then 
		caster:THTD_AddCritDamage(-caster.thtd_tenshi_02_crit_damage)
		caster.thtd_tenshi_02_crit_damage = nil
	end	
end

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

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

	caster.thtd_tenshi_03_attack_count = caster.thtd_tenshi_03_attack_count + 1

	if caster.thtd_tenshi_03_attack_count >= keys.max_count then 
		caster.thtd_tenshi_03_attack_count = 0

		local powerDamage = 0
		local pv = caster:GetAbilityPowerValue("thtd_tenshi_03")
		if pv ~= nil then 					
			powerDamage = pv[1]	
		end
		caster.thtd_tenshi_03_damage = caster:THTD_GetAbilityPowerDamage(keys.ability) + powerDamage * caster:THTD_GetStarDamage()
		
		caster:EmitSound("Hero_Magnataur.ShockWave.Particle")

		local info = 
		{
				Ability = keys.ability,
				EffectName = "particles/heroes/tenshi/ability_tenshi_03.vpcf",
				vSpawnOrigin = caster:GetAbsOrigin(),
				fDistance = 1500,
				fStartRadius = 380,
				fEndRadius = 380,
				Source = caster,
				bHasFrontalCone = false,
				bReplaceExisting = false,
				iUnitTargetTeam = DOTA_UNIT_TARGET_TEAM_ENEMY,
				iUnitTargetFlags = DOTA_UNIT_TARGET_FLAG_NONE,
				iUnitTargetType = DOTA_UNIT_TARGET_HERO + DOTA_UNIT_TARGET_BASIC,
				fExpireTime = GameRules:GetGameTime() + 10.0,
				bDeleteOnHit = false,
				vVelocity = caster:GetForwardVector() * 1800,
				bProvidesVision = true,
				iVisionRadius = 1000,
				iVisionTeamNumber = caster:GetTeamNumber()
		}
		local projectile = ProjectileManager:CreateLinearProjectile(info)
		ParticleManager:DestroyLinearProjectileSystem(projectile,false)
	end
	
	if caster:IsPower666() then 
		if caster.thtd_tenshi_power666_cast ~= true then 
			caster.thtd_tenshi_power666_cast = true
			local time = 1.5	
			local forward = caster:GetForwardVector()
			local effectIndex = ParticleManager:CreateParticle("particles/units/heroes/hero_elder_titan/elder_titan_earth_splitter.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin())
			ParticleManager:SetParticleControl(effectIndex, 1, forward * 900) --方向
			ParticleManager:SetParticleControl(effectIndex, 3, Vector(0, time, 0))  --时间
			ParticleManager:DestroyParticleSystem(effectIndex,false)

			caster:SetContextThink(DoUniqueString("thtd_suwako_03_unit_up"), 
				function()
					caster:EmitSound("Hero_ElderTitan.EarthSplitter.Cast")
					local targets = FindUnitsInLine(
						caster:GetTeamNumber(), 
						caster:GetOrigin(), 
						caster:GetOrigin() + forward * 1000,
						nil, 
						300,
						keys.ability:GetAbilityTargetTeam(), 
						keys.ability:GetAbilityTargetType(), 
						keys.ability:GetAbilityTargetFlags()
					)	
					local damage = 666 * caster:THTD_GetStarDamage()
					for k,v in pairs(targets) do
						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

					caster.thtd_tenshi_power666_cast = false
					return nil			
				end,
			time)
		end
	end
end

function OnTenshiProjectileHit(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	
	local DamageTable = {
			ability = keys.ability,
	        victim = target, 
	        attacker = caster, 
	        damage = caster.thtd_tenshi_03_damage, 
	        damage_type = keys.ability:GetAbilityDamageType(), 
	        damage_flags = DOTA_DAMAGE_FLAG_NONE
   	}
   	UnitDamageTarget(DamageTable)
end

function OnThinkTenshi03Buff(keys)
	local target = keys.target
	
	local targets = THTD_FindFriendlyUnitsInRadius(target,target:GetAbsOrigin(),keys.radius)	
	for k,v in pairs(targets) do
		local modifier = v:FindModifierByName("modifier_tenshi_03_attack_speed_buff")
		-- 持续时间比think大一点
		if modifier == nil then			
			keys.ability:ApplyDataDrivenModifier(target, v, "modifier_tenshi_03_attack_speed_buff", {Duration = 0.3})
		else
			modifier:SetDuration(0.3, false)
		end
	end
end

function OnCreatedTenshi03Buff(keys)
	keys.target:THTD_AddAttackSpeed(keys.attack_speed, "thtd_tenshi_03_bonus")	
end

function OnDestroyTenshi03Buff(keys)
	keys.target:THTD_AddAttackSpeed("thtd_tenshi_03_bonus")
end