function OnAya01RandomSuccess(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	OnAya01AttackEffectSuccess(keys,caster,target)	
end

function OnAya01AttackEffectSuccess(keys,caster,target)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_aya/ability_aya_01.vpcf",PATTACH_CUSTOMORIGIN,caster)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin()+caster:GetForwardVector()*60)
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local ability = caster:FindAbilityByName("thtd_aya_01")
	local range = ability:GetSpecialValueFor("range")

	if target:HasModifier("modifier_aya01_news_buff") == false then
		local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_aya/ability_aya_01_news.vpcf",PATTACH_CUSTOMORIGIN,caster)
		ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
		ParticleManager:DestroyParticleSystem(effectIndex,false)
		ability:ApplyDataDrivenModifier(caster,target,"modifier_aya01_news_buff", nil)
	end
	
	local powerDamage = 0
	local pv = caster:GetAbilityPowerValue("thtd_aya_01")
	if pv ~= nil then
		powerDamage = pv[1]		
	end
	
	local damage = caster:THTD_GetAbilityPowerDamage(ability) + powerDamage * caster:THTD_GetStarDamage()
	local targets = THTD_FindUnitsInRadius(caster, target:GetOrigin(), range)		
	for _,v in pairs(targets) do
		local damage_table = {
				ability = ability,
				victim = v,
				attacker = caster,
				damage = damage,
				damage_type = ability:GetAbilityDamageType(), 
				damage_flags = DOTA_DAMAGE_FLAG_NONE
		}
		UnitDamageTarget(damage_table)		
	end

	local effectIndex = ParticleManager:CreateParticle("particles/econ/items/windrunner/windrunner_cape_cascade/windrunner_windrun_cascade.vpcf",PATTACH_CUSTOMORIGIN,nil)
	ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 1, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 3, target:GetOrigin())
	ParticleManager:DestroyParticleSystemTime(effectIndex,2)

	if caster:FindAbilityByName("thtd_aya_03"):GetLevel() > 0 then
		caster:FindAbilityByName("thtd_aya_02"):EndCooldown()
	end
end

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

	local entities = THTD_FindUnitsAll(caster)
	for k,v in pairs(entities) do
		local modifier = v:FindModifierByName("modifier_aya01_news_buff")
		if modifier == nil then
			modifier = v:FindModifierByName("modifier_hatate01_news_buff")
		end
		if modifier ~= nil then
			local count = modifier:GetStackCount()
			if count < 999 then 
				modifier:SetStackCount(count + 1)
			end	
		end
	end

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

function GetAyaBuffCrit(caster, target)
	local powerDamage = 0
	local pv = caster:GetAbilityPowerValue("thtd_aya_01")
	if pv ~= nil then
		powerDamage = pv[2]		
	end	
	
	local damage_up = caster:GetAbilityValue("thtd_aya_01", "damage_up") + powerDamage
	if caster:IsPower999() then 
		damage_up = damage_up + 4
	end

	local crit = 1.0
	local modifier = target:FindModifierByName("modifier_aya01_news_buff")
	if modifier == nil then
		modifier = target:FindModifierByName("modifier_hatate01_news_buff")
	end
	if modifier ~= nil then
		crit = crit + modifier:GetStackCount() * damage_up / 100
	end
	if caster:IsPower666() then 
		crit = crit * 4
	end
	return crit
end

function OnAya02SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = keys.target_points[1]
	local rad = GetRadBetweenTwoVec2D(caster:GetOrigin(),targetPoint)

	caster:StartGesture(ACT_DOTA_CAST_ABILITY_2)
	caster:SetForwardVector(Vector(math.cos(rad),math.sin(rad),0))

	keys.ability:ApplyDataDrivenModifier(caster,caster,"modifier_aya_02_pause", nil)
	  
	local powerDamage = 0	
	local pv = caster:GetAbilityPowerValue("thtd_aya_02")
	if pv ~= nil then 		
		powerDamage = pv[1]		
	end

	caster.last_start_point = caster:GetAbsOrigin()
	local count = 0	
	caster:SetContextThink(DoUniqueString("ability_aya_02_move"), 
		function () 
			if GameRules:IsGamePaused() then return 0.03 end
			if GetDistanceBetweenTwoVec2D(caster:GetOrigin(), targetPoint) >= 90 and GetDistanceBetweenTwoVec2D(caster:GetOrigin(), targetPoint) < keys.ability:GetCastRange() 
			and caster:HasModifier("modifier_touhoutd_release_hidden") == false then
				local vOrgin = caster:GetOrigin()
				local vCurrent = vOrgin + Vector(math.cos(rad),math.sin(rad),0)*90
				caster:SetAbsOrigin(vCurrent)					
				count = count + 1		
				if count >= 5 then	
					count = 0
					local targets = 
						FindUnitsInLine(
							caster:GetTeamNumber(), 
							caster.last_start_point, 
							vCurrent,
							nil, 
							200,
							keys.ability:GetAbilityTargetTeam(), 
							keys.ability:GetAbilityTargetType(), 
							keys.ability:GetAbilityTargetFlags()
						)
					caster.last_start_point = vCurrent
					local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) + powerDamage * caster:THTD_GetStarDamage()
					for _,v in pairs(targets) do					
						local damage_table = {
								ability = keys.ability,
								victim = v,
								attacker = caster,
								damage = damage * GetAyaBuffCrit(caster, v),
								damage_type = keys.ability:GetAbilityDamageType(), 
								damage_flags = DOTA_DAMAGE_FLAG_NONE
						}
						UnitDamageTarget(damage_table)			

						if caster:FindAbilityByName("thtd_aya_03"):GetLevel() > 0 then
							OnAyaAttack(keys,v)							
						end
					end									
				end				
			else
				local vCurrent = caster:GetAbsOrigin()
				FindClearSpaceForUnit(caster, vCurrent, false)
				caster:THTD_DestroyLevelEffect()
				caster:THTD_CreateLevelEffect()
				caster:RemoveGesture(ACT_DOTA_CAST_ABILITY_2)
				caster:RemoveModifierByName("modifier_aya_02_pause")
				if count > 0 then					
					local targets = 
						FindUnitsInLine(
							caster:GetTeamNumber(), 
							caster.last_start_point, 
							vCurrent,
							nil, 
							200,
							keys.ability:GetAbilityTargetTeam(), 
							keys.ability:GetAbilityTargetType(), 
							keys.ability:GetAbilityTargetFlags()
						)									
					local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) + powerDamage * caster:THTD_GetStarDamage()
					for _,v in pairs(targets) do					
						local damage_table = {
								ability = keys.ability,
								victim = v,
								attacker = caster,
								damage = damage * GetAyaBuffCrit(caster, v),
								damage_type = keys.ability:GetAbilityDamageType(), 
								damage_flags = DOTA_DAMAGE_FLAG_NONE
						}
						UnitDamageTarget(damage_table)			

						if caster:FindAbilityByName("thtd_aya_03"):GetLevel() > 0 then
							OnAyaAttack(keys,v)
						end
					end				
				end
				return nil
			end
			return 0.03
		end, 
	0.03)
end

function OnAyaAttack(keys,target)
	local caster = EntIndexToHScript(keys.caster_entindex)	

	if caster:HasModifier("modifier_eirin_02_spell_buff") then
		local eirin = caster:FindModifierByName("modifier_eirin_02_spell_buff"):GetCaster()
		if eirin ~= nil then
			OnAyaLinkToEirin01(eirin,target,caster)
		end
	end

	if caster:HasModifier("modifier_item_2011_attack_stun") then
		--todo
	end

	-- 基本上可叠 300 - 700 层
	local entities = THTD_FindUnitsAll(caster)
	for k,v in pairs(entities) do
		local modifier = v:FindModifierByName("modifier_aya01_news_buff")
		if modifier == nil then
			modifier = v:FindModifierByName("modifier_hatate01_news_buff")
		end
		if modifier ~= nil then
			local count = modifier:GetStackCount()
			if count < 999 then 
				modifier:SetStackCount(count + 1)
			end				
		end
	end

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

	if RollPercentage(caster:GetAbilityValue("thtd_aya_01", "chance")) then
		OnAya01AttackEffectSuccess(keys,caster,target)		
	end	
end

function OnAya03WingsSpellThink(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	if keys.ability:GetLevel() < 1 then 
		if caster.thtd_aya_03_wings~=nil and caster.thtd_aya_03_wings:IsNull()==false then
			caster.thtd_aya_03_wings:RemoveSelf()
			caster.thtd_aya_03_wings = nil
		end
		return 
	end

	if caster.thtd_aya_03_wings == nil then
		caster.thtd_aya_03_wings = CreateUnitByName(
				"npc_unit_aya_03_wings", 
				caster:GetOrigin(), 
				false, 
				caster, 
				caster, 
				caster:GetTeam() 
			)
		caster.thtd_aya_03_wings:FollowEntity( caster, true )
	end
end

function OnAyaLinkToEirin01(caster,target,attacker)
	local ability = caster:FindAbilityByName("thtd_eirin_01")
	local manaCost = ability:GetSpecialValueFor("mana_cost")
	local manaBonus = ability:GetSpecialValueFor("mana_bonus")/100
	local manaChance = ability:GetSpecialValueFor("mana_chance")
	if caster:GetMana() >= manaCost then
		if RollPercentage(caster:THTD_GetStar() * manaChance) then
			caster:SetMana(math.min(caster:GetMaxMana(), caster:GetMana() - manaCost + caster:GetMaxMana() * manaBonus))
		else
			caster:SetMana(caster:GetMana() - manaCost)
		end

		local now = GameRules:GetGameTime()
		if now - (target.thtd_eirin_01_project or 0) < 1 then 
			local delay = GetDistanceBetweenTwoVec2D(caster:GetAbsOrigin(), target:GetAbsOrigin()) / 1400			
			caster:SetContextThink(DoUniqueString("dota_timer"), 
				function() 
					if GameRules:IsGamePaused() then return 0.1 end		
					if IsValidAlive(target) then	
						local damage = caster:THTD_GetAbilityPowerDamage(ability)								
						local DamageTable = {
								ability = ability,
								victim = target, 
								attacker = caster, 
								damage = damage, 
								damage_type = ability:GetAbilityDamageType(), 
								damage_flags = DOTA_DAMAGE_FLAG_NONE
						}					
						UnitDamageTarget(DamageTable)
						Eirin01HitCount(caster)
					end
					return nil 
				end, 
			delay)
			return
		end
		target.thtd_eirin_01_project = now

		caster:StartGesture(ACT_DOTA_CAST_ABILITY_1)
		local info = 
		{
			Target = target,
			Source = caster,
			Ability = ability,	
			EffectName = "particles/heroes/thtd_eirin/ability_eirin_01.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
			ExtraData = { 									  -- 注意，只支持简单类型，不支持 table数据
				attacker_entindex = attacker:GetEntityIndex()	
			}
		}
		local projectile = ProjectileManager:CreateTrackingProjectile(info)
		ParticleManager:DestroyLinearProjectileSystem(projectile,false)
	end
end

-- 同abilityeirin.lua
function Eirin01HitCount(caster)
	if caster.thtd_eirin_01_count_lock == true then 
		return
	end

	if not caster:HasModifier("modifier_eirin_01_count") then 
		caster:FindAbilityByName("thtd_eirin_02"):ApplyDataDrivenModifier(caster, caster, "modifier_eirin_01_count", {duration = SpawnSystem.CurTime})
		local effectName = "particles/addons_gameplay/morokai_orb_overhead_counter.vpcf"
		local effectIndex = ParticleManager:CreateParticle(effectName, PATTACH_CUSTOMORIGIN, caster) 
		ParticleManager:SetParticleControl(effectIndex, 0, caster:GetAbsOrigin() + Vector(0, 0, 410))	
		ParticleManager:SetParticleControl(effectIndex, 2, Vector(1, 0, 0))
		caster.thtd_eirin_01_count_effect = effectIndex		
	end

	caster.thtd_eirin_01_count = (caster.thtd_eirin_01_count or 0) + 1
	if caster.thtd_eirin_01_count > 500 then 
		caster.thtd_eirin_01_count = 500
		return
	end

	local one = math.floor(caster.thtd_eirin_01_count/100)
	local two = math.floor((caster.thtd_eirin_01_count - one * 100)/10)
	local three = caster.thtd_eirin_01_count - one * 100 - two * 10
	if three == 0 then three = 1 end
	ParticleManager:SetParticleControl(caster.thtd_eirin_01_count_effect, 2, Vector(three, two, one))
end

