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

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_remilia/ability_remilia_03_end.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 3, caster:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	effectIndex = ParticleManager:CreateParticle("particles/heroes/moluo/abiilty_moluo_014.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)
	local damage = caster:THTD_GetAbilityPowerDamage(ability)

	local powerBonus = #targets * keys.power_bonus
	local duration_time = math.floor(keys.duration_time * 100 + 0.5) / 100

	for k,target in pairs(targets) do
		local effectIndex = ParticleManager:CreateParticle("particles/heroes/moluo/ability_moluo01_explosion_vip.vpcf",PATTACH_CUSTOMORIGIN,caster)
		ParticleManager:SetParticleControlEnt(effectIndex , 0, caster, 5, "follow_origin", Vector(0,0,0), true)
		ParticleManager:SetParticleControlEnt(effectIndex , 1, caster, 5, "follow_origin", Vector(0,0,0), true)
		ParticleManager:SetParticleControlEnt(effectIndex , 2, target, 5, "follow_origin", Vector(0,0,0), true)
		ParticleManager:SetParticleControlForward(effectIndex, 2, caster:GetForwardVector())
		ParticleManager:SetParticleControlEnt(effectIndex , 5, caster, 5, "follow_origin", Vector(0,0,0), true)
		ParticleManager:SetParticleControlEnt(effectIndex , 7, caster, 5, "follow_origin", Vector(0,0,0), true)
		ParticleManager:SetParticleControlEnt(effectIndex , 10, caster, 5, "follow_origin", Vector(0,0,0), true)

		local crit = 1
		if target:HasModifier("modifier_remilia_03_debuff") then
			crit = 10
		end

		local damage_table={
			ability = ability,
			victim=target, 
			attacker=caster, 
			damage=damage * crit,
			damage_type=ability:GetAbilityDamageType(),
			damage_flags=ability:GetAbilityTargetFlags()
		}
		UnitDamageTarget(damage_table)
		OnRemilia02SpellStart(caster)
	end

	if powerBonus > 0 then 
		caster:THTD_AddBasePower(powerBonus)
		caster:SetContextThink(DoUniqueString("dota_timer"), 
			function() 
				if GameRules:IsGamePaused() then return 0.1 end
				caster:THTD_AddBasePower(-powerBonus)
				return nil 
			end, 
		duration_time)
	end
end

function OnRemilia02SpellStart(caster)	
	local step = caster:GetAbilityValue("thtd_remilia_02", "damage_up")
	local maxUp = caster:GetAbilityValue("thtd_remilia_02", "max_up")
	local duration = caster:GetAbilityValue("thtd_remilia_02", "duration_time")	
	
	if caster.thtd_remilia_02_outgoing == nil then
		caster.thtd_remilia_02_outgoing = 0
	end
	
	local count = math.min(step, maxUp - caster.thtd_remilia_02_outgoing)
	if count > 0 then
		caster:AddDamageOutgoingMagical(count)
		caster.thtd_remilia_02_outgoing = caster.thtd_remilia_02_outgoing + count	
		caster:SetContextThink(DoUniqueString("modifier_reimilia_02_buff_count"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end				
				caster:AddDamageOutgoingMagical(-count)
				caster.thtd_remilia_02_outgoing = caster.thtd_remilia_02_outgoing - count				
				return nil
			end, 
		duration)
	end
end

function OnRemilia03SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local targetPoint = keys.target_points[1]
	local forward = (keys.target_points[1] - caster:GetOrigin()):Normalized()

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/remilia/ability_remilia_03_spark.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin() + Vector(forward.x * 92,forward.y * 92,150))
	ParticleManager:SetParticleControl(effectIndex, 8, Vector(forward.x,forward.y,0))
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_remilia/ability_remilia_03_end.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 3, caster:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local info = 
	{
			Ability = keys.ability,
        	EffectName = "particles/heroes/remilia/ability_remilia_01.vpcf",
        	vSpawnOrigin = caster:GetOrigin()+Vector(0,0,30),
        	fDistance = keys.distance,
        	fStartRadius = 300,
        	fEndRadius = 300,
        	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 = forward * 4000,
			bProvidesVision = true,
			iVisionRadius = 1000,
			iVisionTeamNumber = caster:GetTeamNumber()
	}
	local projectile = ProjectileManager:CreateLinearProjectile(info)
	ParticleManager:DestroyLinearProjectileSystem(projectile,false)
end

function OnRemilia03SpellHit(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local targetPoint = target:GetOrigin()
	
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/remilia/ability_remilia_01_explosion.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:SetParticleControl(effectIndex, 1, targetPoint)
	ParticleManager:SetParticleControl(effectIndex, 3, targetPoint)
	ParticleManager:SetParticleControl(effectIndex, 5, targetPoint)
	ParticleManager:DestroyParticleSystem(effectIndex,false)
	
	if caster.thtd_remilia_03_chance_count == nil then
		caster.thtd_remilia_03_chance_count = 0
	end

	local chance = caster:GetAbilityValue("thtd_remilia_04", "chance")
	local success = RollPercentage(chance)
	if caster.thtd_remilia_03_chance_count >= 100/chance then
		success = true
	end
	if success then
		caster.thtd_remilia_03_chance_count = 0
	else
		caster.thtd_remilia_03_chance_count = caster.thtd_remilia_03_chance_count + 1
	end

	local damage = 0
	local ability4 = caster:FindAbilityByName("thtd_remilia_04")
	if ability4:GetLevel() > 0 and success then
		damage = ability4:GetSpecialValueFor("power_damage_03") * caster:THTD_GetStarDamage()
		if not target:HasModifier("modifier_remilia_03_debuff") then
			keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_remilia_03_debuff",nil)			
		end	
	else
		damage = caster:THTD_GetAbilityPowerDamage(keys.ability)
	end

	local crit = 1
	if target:HasModifier("modifier_remilia_03_debuff") then
		crit = 10
	end

	local damage_table = {
			ability = keys.ability,
			victim = target,
			attacker = caster,
			damage = damage * crit,
			damage_type = keys.ability:GetAbilityDamageType(), 
			amage_flags = keys.ability:GetAbilityTargetFlags()
	}
	UnitDamageTarget(damage_table)
	OnRemilia02SpellStart(caster)
end

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

	target:RemoveMagicalResist()
	target:RemovePhysicalArmor()

	local bonus = caster:GetAbilityValue("thtd_remilia_04", "damage_up")
	target:AddDamageIncomingPhysical(bonus, "thtd_remilia_03_damage_up")
	target:AddDamageIncomingMagical(bonus, "thtd_remilia_03_damage_up")	
end

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

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/remilia/ability_remilia_04_laser.vpcf",PATTACH_CUSTOMORIGIN,caster)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint+Vector(0,0,700))
	ParticleManager:SetParticleControl(effectIndex, 1, targetPoint+Vector(800,0,800))
	ParticleManager:SetParticleControl(effectIndex, 2, targetPoint+Vector(0,0,0))
	ParticleManager:SetParticleControl(effectIndex, 3, targetPoint+Vector(-800,0,800))
	ParticleManager:SetParticleControl(effectIndex, 4, targetPoint+Vector(0,0,1600))
	ParticleManager:SetParticleControl(effectIndex, 6, targetPoint)
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local effectIndexSmoke = ParticleManager:CreateParticle("particles/heroes/remilia/ability_remilia_04_laser_rocket.vpcf",PATTACH_CUSTOMORIGIN,caster)
	ParticleManager:SetParticleControl(effectIndexSmoke, 0, targetPoint+Vector(0,0,700))
	ParticleManager:SetParticleControl(effectIndexSmoke, 1, Vector(1,0,0))
	ParticleManager:SetParticleControl(effectIndexSmoke, 2, Vector(-1,0,0))
	ParticleManager:SetParticleControl(effectIndexSmoke, 3, Vector(0,0,0.5))
	ParticleManager:SetParticleControl(effectIndexSmoke, 4, Vector(0,0,-1))
	ParticleManager:DestroyParticleSystem(effectIndexSmoke,false)
	
	local ability = caster:FindAbilityByName("thtd_remilia_04")
	local time = math.floor(ability:GetSpecialValueFor("duration_time") * 100 + 0.5) / 100	
	local tick = math.floor(ability:GetSpecialValueFor("tick_time") * 100 + 0.5) / 100 
	local range = ability:GetSpecialValueFor("range")

	caster:EmitSound("Sound_THTD.thtd_remilia_04")
	
	caster:SetContextThink(DoUniqueString("ability_remilia_03_effect_destroy"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if time <= 0 then return nil end

			if not THTD_IsValid(caster) then
				ParticleManager:DestroyParticleSystem(effectIndex,true)
				ParticleManager:DestroyParticleSystem(effectIndexSmoke,true)
				return nil 
			end

			local damage = caster:THTD_GetAbilityPowerDamage(ability)
			local targets = THTD_FindUnitsInRadius(caster,targetPoint,range)
			for k,v in pairs(targets) do
				local crit = 1
				if v:HasModifier("modifier_remilia_03_debuff") then
					crit = 10
				end
				local damage_table={
					ability = ability,
					victim = v, 
					attacker = caster, 
					damage = damage * crit,
					damage_type = ability:GetAbilityDamageType(),
					damage_flags = ability:GetAbilityTargetFlags()
				}
				UnitDamageTarget(damage_table)
			end

			OnRemilia02SpellStart(caster)			

			time = time - tick
			return tick		
		end, 
	0)
end