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

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)
	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 OnRumia01Kill(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)

	if caster.rumia_01_bonus == nil then
		caster.rumia_01_bonus = 0
	end
	
	if caster:IsPower999() then 
		if caster.rumia_01_bonus < keys.max_bonus * 10 then
			caster.rumia_01_bonus = caster.rumia_01_bonus + 1
			caster:THTD_AddBasePower(1)		
		end
	else
		if caster.rumia_01_bonus < keys.max_bonus then 
			caster.rumia_01_bonus = caster.rumia_01_bonus + 1
			caster:THTD_AddBasePower(1)	
		elseif caster.rumia_01_bonus > keys.max_bonus then 
			caster:THTD_AddBasePower(-(caster.rumia_01_bonus - keys.max_bonus))	
			caster.rumia_01_bonus = keys.max_bonus
		end
	end
end

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

	if keys.ability:GetLevel() < 1 then return end

	if caster.rumia_02_attack_count == nil then
		caster.rumia_02_attack_count = 0
	end

	local powerCount = 0
	local pv = caster:GetAbilityPowerValue("thtd_rumia_02")
	if pv ~= nil then 					
		powerCount = pv[1]	
	end

	caster.rumia_02_attack_count = caster.rumia_02_attack_count + 1
	
	if caster.rumia_02_attack_count >= (keys.max_count - powerCount) then
		RumiaProjectileStart(keys)
		caster.rumia_02_attack_count = 0
	end
end

function RumiaProjectileStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)

	caster:EmitSound("Sound_THTD.thtd_rumia_01")

	for i=1,12 do
		local forwardCos = caster:GetForwardVector().x
		local forwardSin = caster:GetForwardVector().y
		local angle = (39 - 6.5 * i) / 180 * math.pi
		local forward = Vector(	math.cos(angle)*forwardCos - math.sin(angle)*forwardSin,
								forwardSin*math.cos(angle) + forwardCos*math.sin(angle),0)
		local info = 
		{
				Ability = keys.ability,
	        	EffectName = "particles/heroes/rumia/ability_rumia_02_projectile.vpcf",
	        	vSpawnOrigin = caster:GetOrigin() + forward * 500 - caster:GetForwardVector() * 500 + Vector(0,0,128),
	        	fDistance = 800,
	        	fStartRadius = 150,
	        	fEndRadius = 150,
	        	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 = true,
				vVelocity = caster:GetForwardVector() * 1800,
				bProvidesVision = true,
				iVisionRadius = 1000,
				iVisionTeamNumber = caster:GetTeamNumber()
		}
		if caster:THTD_IsTowerEx() == true then
			info.EffectName = "particles/heroes/rumia/ability_rumia_02_ex_projectile.vpcf"
		end
		local projectile = ProjectileManager:CreateLinearProjectile(info)
		ParticleManager:DestroyLinearProjectileSystem(projectile,false)
	end
end

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

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability, 1)

	if caster:THTD_IsTowerEx() == true then
		damage = caster:THTD_GetAbilityPowerDamage(keys.ability, 2)
	end

	if caster:IsPower666() then damage = damage * 10 end

	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 OnRumia03AttackLanded(keys)
	if keys.ability:GetLevel()<1 then return end
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	if target.thtd_damage_lock == true then return end
	
	if target:GetHealthPercent() > 70 and RollPercentage(keys.chance) then	
		local targetPoint = target:GetOrigin()
		keys.ability:ApplyDataDrivenModifier(caster,target,"modifier_rumia_03_pause",{})
		local pointRad = GetRadBetweenTwoVec2D(caster:GetOrigin(),targetPoint)
		local randomPi = -2*math.pi

		if RandomInt(0,1) == 0 then
			randomPi = -2*math.pi
		else
			randomPi = 2*math.pi
		end

		local forwardVec = Vector(math.cos(pointRad+randomPi), math.sin(pointRad+randomPi),RandomFloat(0,1))
		local projectileTable = {
		    Ability        	 	=   keys.ability,
			EffectName			=	"particles/heroes/thtd_rumia/ability_rumia_03.vpcf",
			vSpawnOrigin		=	caster:GetOrigin(),
			vSpawnOriginNew		=	caster:GetOrigin(),
			fDistance			=	5000,
			fStartRadius		=	60,
			fEndRadius			=	60,
			Source         	 	=   caster,
			bHasFrontalCone		=	false,
			bRepalceExisting 	=  	false,
			iUnitTargetTeams	=	"DOTA_UNIT_TARGET_TEAM_ENEMY",
			iUnitTargetTypes	=	"DOTA_UNIT_TARGET_HERO | DOTA_UNIT_TARGET_CREEP",
			iUnitTargetFlags	=	"DOTA_UNIT_TARGET_FLAG_NONE",
			fExpireTime     	=   GameRules:GetGameTime() + 10.0,
			bDeleteOnHit    	=   true,
			vVelocity       	=   forwardVec,
			bProvidesVision		=	true,
			iVisionRadius		=	400,
			iVisionTeamNumber 	= 	caster:GetTeamNumber(),
		}

		local speed = 4000
		local acceleration = -400
		local iVelocity = 1000
		local ishit = false

	    local effectIndex = ParticleManager:CreateParticle(projectileTable.EffectName, PATTACH_CUSTOMORIGIN, nil) 
	    ParticleManager:SetParticleControlForward(effectIndex,3,(projectileTable.vVelocity*iVelocity/50 + speed/50 * (targetPoint - caster:GetOrigin()):Normalized()):Normalized())

	    local ability = projectileTable.Ability
	    local targets = {}
	    local targets_remove = {}
	    local totalDistance = 0
	    local time = 0
	    local high = 0

	    caster:SetContextThink(DoUniqueString("ability_caster_projectile"), 
	        function()
	            if GameRules:IsGamePaused() then return 0.03 end

	            -- 向心力单位向量
	            local vecCentripetal = (projectileTable.vSpawnOriginNew - targetPoint):Normalized()

	            -- 向心力
	            local forceCentripetal = speed/50

	            -- 初速度单位向量
	            local vecInVelocity = projectileTable.vVelocity

	            -- 初始力
	            local forceIn = iVelocity/50

	            -- 投射物矢量
	            local vecProjectile = vecInVelocity * forceIn + forceCentripetal * vecCentripetal

	            local vec = projectileTable.vSpawnOriginNew + vecProjectile + Vector(0,0,high)

	            -- 投射物单位向量
	            local particleForward = vecProjectile:Normalized()

	            -- 目标和投射物距离
	            local dis = GetDistanceBetweenTwoVec2D(targetPoint,vec)

	            ParticleManager:SetParticleControlForward(effectIndex,3,particleForward)

	            totalDistance = totalDistance + math.sqrt(forceIn*forceIn + forceCentripetal*forceCentripetal)

	            if(dis<400)then
	            	if ishit == false then
	            		ishit = true
	            		target:StartGesture(ACT_DOTA_FLAIL)
	            	end
	            	high = high + 25
	            	target:SetAbsOrigin(projectileTable.vSpawnOriginNew+400*Vector(forwardVec.x,forwardVec.y,0)-Vector(0,0,50))
	            end

	            if(dis<projectileTable.fEndRadius)then
	                if(projectileTable.bDeleteOnHit)then
	                    OnRumia03AbilityEnd(caster,target,keys.ability,effectIndex,time)
	                    return nil
	                end
	            end

	            if(totalDistance<projectileTable.fDistance and dis>=projectileTable.fEndRadius)then
	                ParticleManager:SetParticleControl(effectIndex,3,vec)
	                projectileTable.vSpawnOriginNew = vec
	                speed = speed + acceleration
	                time = time + 0.02
	                return 0.02
	            else
	                if func then func(projectileTable.vSpawnOriginNew) end
	                ParticleManager:DestroyParticleSystem(effectIndex,true)
	                return nil
	            end
	        end, 
	    0.02)
	end
end

function OnRumia03AbilityEnd(caster,target,ability,effectIndex,time)
	caster:SetContextThink(DoUniqueString("ability_caster_projectile_END"), 
        function()
            if GameRules:IsGamePaused() then return 0.03 end
            target:RemoveModifierByName("modifier_rumia_03_pause")
			ParticleManager:DestroyParticleSystem(effectIndex,true)	
			local effectIndexEnd = ParticleManager:CreateParticle("particles/heroes/thtd_rumia/ability_rumia_04_explosion.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndexEnd, 0, target:GetOrigin())
			ParticleManager:SetParticleControl(effectIndexEnd, 3, target:GetOrigin())
			ParticleManager:DestroyParticleSystem(effectIndexEnd,false)
			target:SetContextThink(DoUniqueString("ability_caster_projectile_END"), 
				function()
					if GameRules:IsGamePaused() then return 0.03 end
					if THTD_IsValid(target) then 
						FindClearSpaceForUnit(target, target:GetOrigin(), false)
					end				
					return nil
				end, 
			0.2)			
			caster:AbilityKill(target, ability)
			if caster.rumia_01_bonus == nil then
				caster.rumia_01_bonus = 0
			end			
			if caster.rumia_01_bonus < caster:GetAbilityValue("thtd_rumia_01", "max_bonus") then
				caster.rumia_01_bonus = caster.rumia_01_bonus + 1
				caster:THTD_AddBasePower(1)
			end
            return nil
        end, 
   	1.5 - time)
end

function Rumia04AttackTargetPoint(keys)
	if keys.ability:GetLevel() < 1 then return end

	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local vecCaster = caster:GetOrigin()
	local pointRad = GetRadBetweenTwoVec2D(vecCaster,target:GetOrigin())

	local randomPi = RandomFloat(-2*math.pi,2*math.pi)
	local forwardVec = Vector(math.cos(pointRad+randomPi), math.sin(pointRad+randomPi),RandomFloat(0,1))

	local BulletTable = {
	    Ability        	 	=   keys.ability,
		EffectName			=	"particles/heroes/thtd_rumia/ability_rumia_04.vpcf",
		vSpawnOrigin		=	vecCaster,
		vSpawnOriginNew		=	vecCaster,
		fDistance			=	5000,
		fStartRadius		=	60,
		fEndRadius			=	60,
		Source         	 	=   caster,
		bHasFrontalCone		=	false,
		bRepalceExisting 	=  	false,
		iUnitTargetTeams	=	"DOTA_UNIT_TARGET_TEAM_ENEMY",
		iUnitTargetTypes	=	"DOTA_UNIT_TARGET_HERO | DOTA_UNIT_TARGET_CREEP",
		iUnitTargetFlags	=	"DOTA_UNIT_TARGET_FLAG_NONE",
		fExpireTime     	=   GameRules:GetGameTime() + 10.0,
		bDeleteOnHit    	=   true,
		vVelocity       	=   forwardVec,
		bProvidesVision		=	true,
		iVisionRadius		=	400,
		iVisionTeamNumber 	= 	caster:GetTeamNumber(),
	}

	local speed = 2000
	local acc = 200
	local iVelo = 1000

	CreateProjectileMoveToPoint(BulletTable,caster,target:GetOrigin(),speed,iVelo,-acc,
		function(vec)
			local powerDamage = 0
			local pv = caster:GetAbilityPowerValue("thtd_rumia_04")
			if pv ~= nil then 					
				powerDamage = pv[1]	
			end
			local targetpoint = Vector(vec.x,vec.y,caster:GetOrigin().z)
			local targets = THTD_FindUnitsInRadius(caster,targetpoint,keys.range)
			local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) + powerDamage * caster:THTD_GetStarDamage()
			for k,v in pairs(targets) do
				local damage_table = {
					victim = v,
					attacker = caster,
					damage = damage,
					ability = keys.ability,
					damage_type = keys.ability:GetAbilityDamageType(), 
					damage_flags = DOTA_DAMAGE_FLAG_NONE,
				}
				UnitDamageTarget(damage_table)
			end
			
			local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_rumia/ability_rumia_04_explosion.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, targetpoint)
			ParticleManager:SetParticleControl(effectIndex, 3, targetpoint)
			ParticleManager:DestroyParticleSystem(effectIndex,false)
		end
	)
end