function OnMomiji01Spawn(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	if caster.ability_momiji01_Spawn_unit == nil then
		caster.ability_momiji01_Spawn_unit = {}
	end

	count = keys.max_count

	for i=1,count do
		local unit = CreateUnitByName(
			"momiji_wolf"
			,caster:GetOrigin() + ( caster:GetForwardVector() + Vector(math.cos((i-1.5)*math.pi/3),math.sin((i-1.5)*math.pi/3),0) ) * 100
			,false
			,caster:GetOwner()
			,caster:GetOwner()
			,caster:GetTeam()
		)

		if unit == nil then return end

		unit:SetControllableByPlayer(caster:GetPlayerOwnerID(), false) 
		unit:SetBaseDamageMax(caster:THTD_GetStarDamage() * keys.wolf_attack)
		unit:SetBaseDamageMin(caster:THTD_GetStarDamage() * keys.wolf_attack)
		unit.thtd_spawn_unit_owner = caster

		keys.ability:ApplyDataDrivenModifier(caster, unit, "modifier_momiji_01_buff", nil)

		local hero = caster:GetOwner()
		if GameRules.player_bb_buff[hero.thtd_player_id]["item_3016"] > 0 then 
			unit.bb_buff_3016 = GameRules.player_bb_buff[hero.thtd_player_id]["item_3016"]
			unit:AddNewModifier(unit, nil, "modifier_bb_buff_3016_effect", nil):SetStackCount(GameRules.player_bb_buff[hero.thtd_player_id]["item_3016"])
		end

		local oldSwpanUnit = caster.ability_momiji01_Spawn_unit[i]
		if oldSwpanUnit ~=nil and oldSwpanUnit:IsNull() == false then 
			oldSwpanUnit:ForceKill(false)
		end
		caster.ability_momiji01_Spawn_unit[i] = unit

		local duration = keys.ability:GetCooldownTime()
		unit:SetContextThink(DoUniqueString("thtd_momiji_01"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				if GetDistanceBetweenTwoVec2D(caster:GetOrigin(), unit:GetOrigin()) > keys.range then
					local forward = (unit:GetAbsOrigin() - caster:GetAbsOrigin()):Normalized() 
					unit:MoveToPosition(caster:GetOrigin() + forward * keys.range)
				end
				if duration > 0 then
					duration = duration - 0.5
				else
					unit:AddNoDraw()
					unit:ForceKill(false)
					return nil
				end
				if not THTD_IsValid(caster) then
					unit:AddNoDraw()
					unit:ForceKill(false)
					return nil
				end
				return 0.5
			end,
		0.5)
	end
end

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

	if target:GetPlayerOwnerID() ~= caster:GetPlayerOwnerID() then return end

	if caster.ability_momiji_02_target == target then return end
	if caster == target then return end

	keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_momiji_02", nil)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_momiji/ability_momiji_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControlEnt(effectIndex , 0, target, 5, "follow_origin", Vector(0,0,0), true)
	caster.ability_momiji_02_target = target

	local count = 0
	caster:SetContextThink(DoUniqueString("modifier_momiji_02"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if caster.ability_momiji_02_target ~= target then
				if target~=nil and target:IsNull()==false and target:HasModifier("modifier_momiji_02") then
					target:RemoveModifierByName("modifier_momiji_02")
				end
				ParticleManager:DestroyParticleSystem(effectIndex,true)
				return nil
			end
			return 0.1
		end,
	0.1)

	caster:SetContextThink("modifier_momiji_02_remove", 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if caster:THTD_IsHidden() or keys.ability:GetLevel()<1 then
				caster.ability_momiji_02_target = nil
				return nil
			end
			return 0.1
		end,
	0.1)
end

function OnCreatedMomiji02Buff(keys)	
	local target = keys.target	
	local bonus = 0
	if target:GetUnitName() == "aya" then		
		bonus = keys.aya_damage_up
	else 
		bonus = keys.damage_up
	end
	target:AddDamageOutgoingPhysical(bonus, "thtd_momiji_02_damage_up")	
end

function OnUpgradeMomiji02Buff(keys)	
	local caster = keys.caster
	local targets = {}
	if caster.ability_momiji_02_target ~= nil then 
		table.insert(targets, caster.ability_momiji_02_target)
	end
	
	for _,target in pairs(targets) do
		local bonus = 0
		if target:GetUnitName() == "aya" then		
			bonus = keys.aya_damage_up
		else 
			bonus = keys.damage_up
		end
		target:AddDamageOutgoingPhysical(bonus, "thtd_momiji_02_damage_up")	
	end

	targets = {}
end

function OnDestroyMomiji02Buff(keys)		
	keys.target:AddDamageOutgoingPhysical("thtd_momiji_02_damage_up")
end