function OnLuna01Attack(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local targetPoint = target:GetOrigin()	

	local attackedTargets = {}
	OnLuna01Damage(keys,target,1)
	table.insert(attackedTargets, target)
	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)
	local count = 1
	for k,v in pairs(targets) do
		if count > keys.max_count then
			break
		end 
		if THTD_IsValid(v) and v ~= target then
			OnLuna01Damage(keys,v,1 + keys.damage_up/100)
			table.insert(attackedTargets, v)
			count = count + 1
		end
	end
	
	local fairyArea = nil
	local hero = caster:GetOwner()
	if hero~=nil and hero:IsNull()==false then
		local fairyList = GetHeroFairyList(hero)    	
		for k,v in pairs(fairyList) do
			if v.luna == caster then
				fairyArea = v
				break
			end
		end
	end

	if fairyArea ~= nil then 
		local pos1 = fairyArea.sunny:GetAbsOrigin()
		local pos2 = fairyArea.star:GetAbsOrigin()
		local pos3 = fairyArea.luna:GetAbsOrigin()
		local center, radius = GetCircleCenterAndRadius(pos1,pos2,pos3) 
		local targetsTotal = {}
		local fairyTargets = THTD_FindUnitsInRadius(caster,center,radius)		
		for _,v in pairs(fairyTargets) do
			if v~=nil and v:IsNull()==false and v:IsAlive() and IsUnitInFairy(fairyArea,v) then					
				targetsTotal[v:GetEntityIndex()] = v
			end
		end
		for k,v in pairs(targetsTotal) do
			local isExist = false
			for k2,v2 in pairs(attackedTargets) do
				if v2 == v then 
					isExist = true
					break
				end
			end
			if isExist == false and THTD_IsValid(v) then 
				OnLuna01Damage(keys,v,1 + keys.damage_up/100)
			end
		end	
	end	
end

function OnLuna01Damage(keys,target,percentage)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_luna/ability_luna_01.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 1, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 2, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 5, target:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * percentage
	local DamageTable = {
		ability = keys.ability,
        victim = target, 
        attacker = caster, 
        damage = damage, 
        damage_type = keys.ability:GetAbilityDamageType(), 
        damage_flags = DOTA_DAMAGE_FLAG_NONE
   	}
   	UnitDamageTarget(DamageTable)
end

function OnLuna02SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = keys.target_points[1]
	local foward = (targetPoint - caster:GetAbsOrigin()):Normalized()

	local targetsTotal = {}
	local targets = 
		FindUnitsInLine(
			caster:GetTeamNumber(), 
			caster:GetOrigin(), 
			caster:GetOrigin() + foward*1000, 
			nil, 
			200,
			keys.ability:GetAbilityTargetTeam(), 
			keys.ability:GetAbilityTargetType(), 
			keys.ability:GetAbilityTargetFlags()
		)
	
	local fairyArea = nil
	local hero = caster:GetOwner()
	if hero~=nil and hero:IsNull()==false then
		local fairyList = GetHeroFairyList(hero)    	
		for k,v in pairs(fairyList) do
			if v.luna == caster then
				fairyArea = v
				break
			end
		end
	end

	if fairyArea ~= nil then 
		local pos1 = fairyArea.sunny:GetAbsOrigin()
		local pos2 = fairyArea.star:GetAbsOrigin()
		local pos3 = fairyArea.luna:GetAbsOrigin()
		local center, radius = GetCircleCenterAndRadius(pos1,pos2,pos3) 		
		local fairyTargets = THTD_FindUnitsInRadius(caster,center,radius)		
		for _,v in pairs(fairyTargets) do
			if v~=nil and v:IsNull()==false and v:IsAlive() and IsUnitInFairy(fairyArea,v) then					
				targetsTotal[v:GetEntityIndex()] = v
			end
		end		
	end

	for k,v in pairs(targets) do
		targetsTotal[v:GetEntityIndex()] = v
	end
	targets = {}
	
	if caster:HasModifier("modifier_luna_02_buff") then
		caster:RemoveModifierByName("modifier_luna_02_buff")
	end
	caster.luna_02_power_bonus = keys.bonus_power * table.count(targetsTotal)
	keys.ability:ApplyDataDrivenModifier(caster,caster, "modifier_luna_02_buff", {Duration = keys.duration_time})	
	
	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)
	for k,v in pairs(targetsTotal) 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	

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_luna/ability_luna_02_laser.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin()+Vector(0,0,128))
	ParticleManager:SetParticleControl(effectIndex, 1, caster:GetOrigin() + foward*1000 + Vector(0,0,128))
	ParticleManager:SetParticleControl(effectIndex, 3, caster:GetOrigin() + foward*1000 + Vector(0,0,128))
	ParticleManager:SetParticleControl(effectIndex, 9, caster:GetOrigin() + Vector(0,0,128))
	ParticleManager:DestroyParticleSystemTime(effectIndex,2.0)	
end

function OnCreatedLuna02Buff(keys)
	local target = keys.target
	
	local bonus = target.luna_02_power_bonus
	target:THTD_AddPowerPercentage(bonus, "thtd_luna_02_bonus")
	target:THTD_AddAttackPercentage(bonus, "thtd_luna_02_bonus")	
end

function OnDestroyLuna02Buff(keys)
	local target = keys.target	
	target:THTD_AddPowerPercentage("thtd_luna_02_bonus")
	target:THTD_AddAttackPercentage("thtd_luna_02_bonus")	
end