function OnLily01SpellStart(keys)
	local caster = keys.caster
	local ability = keys.ability
	local targetPoint = keys.target_points[1]

	if caster:GetMaxMana() ~= (caster.lily_max_mana or 100) then 
		caster:SetMaxMana(caster.lily_max_mana or 100)
	end

	if SpawnSystem.IsUnLimited == false and GameRules:GetCustomGameDifficulty() < FUNNY_MODE then 
		local targets = 
				FindUnitsInRadius(
					caster:GetTeamNumber(), 
					targetPoint, 
					nil, 
					keys.radius, 
					ability:GetAbilityTargetTeam(), 
					ability:GetAbilityTargetType(), 
					ability:GetAbilityTargetFlags(), 
					FIND_CLOSEST, 
					false
				)
		
		local exp = caster:THTD_GetPower() * keys.power_factor

		for k,v in pairs(targets) do
			if v ~= nil and v:IsNull() == false and v:THTD_IsTower() and v ~= caster and v:GetOwner() == caster:GetOwner() and v:THTD_GetLevel() < THTD_MAX_LEVEL then 
				v:THTD_AddExp(exp)
			end
		end
	end

	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.radius)
	local damage = caster:THTD_GetAbilityPowerDamage(ability)
	local maxDamage = caster:GetKillDamage()
	for k,v in pairs(targets) do		
		local DamageTable = {
			ability = ability,
			victim = v, 
			attacker = caster, 
			damage = math.min(maxDamage, damage + v:GetHealth() * keys.hp_damage/100), 
			damage_type = keys.ability:GetAbilityDamageType(), 
			damage_flags = DOTA_DAMAGE_FLAG_NONE
		}							
		UnitDamageTarget(DamageTable)	
	end

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/lily/ability_lily_01_a.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end

function OnLily01EffectThink(keys)
	local caster = keys.caster
	local ability = keys.ability

	if SpawnSystem.IsUnLimited and caster.is_unlimited_mana_buff ~= true then
		caster.is_unlimited_mana_buff = true
		caster:AddManaCostReducePercent(keys.mana_down, "thtd_lily_01_unlimited_mana_buff")
		caster.lily_max_mana = 100 - caster:GetManaCostReducePercent()	
	end

	if caster:GetMaxMana() ~= (caster.lily_max_mana or 100) then 
		caster:SetMaxMana(caster.lily_max_mana or 100)		
	end

	if ability:IsFullyCastable() then		
		if caster.thtd_lily_01_effectIndex == nil then
			caster.thtd_lily_01_effectIndex = ParticleManager:CreateParticle("particles/heroes/lily/ability_lily_01_ready.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControlEnt(caster.thtd_lily_01_effectIndex , 0, caster, 5, "follow_origin", Vector(0,0,0), true)
		end
	elseif caster.thtd_lily_01_effectIndex ~= nil then
		ParticleManager:DestroyParticleSystem(caster.thtd_lily_01_effectIndex,true)
		caster.thtd_lily_01_effectIndex = nil
	end
end

function OnLily02SpellStart(keys)
	local caster = keys.caster

	if caster:GetMaxMana() ~= (caster.lily_max_mana or 100) then 
		caster:SetMaxMana(caster.lily_max_mana or 100)
	end

	local powerRange = 0
	local pv = caster:GetAbilityPowerValue("thtd_lily_02")
	if pv ~= nil then 
		powerRange = pv[1] 
	end	
	local range = powerRange + keys.radius

	local targets = THTD_FindFriendlyUnitsInRadius(caster,caster:GetAbsOrigin(),range)	
	for k,v in pairs(targets) do
		keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_lily_02_buff", nil)	
	end	

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/lily/ability_lily_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 1, Vector(range, range, range))
	ParticleManager:DestroyParticleSystemTime(effectIndex,keys.duration)
end

function OnCreatedLily02Buff(keys)
	local factor = 1
	if keys.target:GetUnitName() == "lily" then 
		local date = string.sub(GameRules.GameData.server_time,6,10)
		if date >= "02-05" and date <= "05-05" then 
			factor = keys.self_crit
		end
	end
	keys.target:AddDamageOutgoingAll(keys.damage_up * factor, "thtd_lily_02_damage_up")	
end

function OnDestroyLily02Buff(keys)	
	keys.target:AddDamageOutgoingAll("thtd_lily_02_damage_up")
end

