function OnSpellStartClownpiece01(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = keys.target_points[1]

	if caster.thtd_clown_ball == nil then
		local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_hecatia/ability_hecatia_01_moon.vpcf", PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(effectIndex, 0, targetPoint + Vector(0,0,128))
		ParticleManager:SetParticleControl(effectIndex, 1, Vector(0.3,0,0))
		ParticleManager:SetParticleControl(effectIndex, 2, Vector(150,0,0))

		caster.thtd_clown_ball = 
		{
			["Vector"] = targetPoint + Vector(0,0,128),
			["EffectIndex"] = effectIndex
		}
	else
		caster.thtd_clown_ball["Vector"] = targetPoint + Vector(0,0,128)
		ParticleManager:SetParticleControl(effectIndex, 0, caster.thtd_clown_ball["Vector"])
	end

	local powerDamage = 0
	local pv = caster:GetAbilityPowerValue(keys.ability:GetAbilityName())
	if pv ~= nil then 
		powerDamage = pv[1]/100
	end	

	local range = keys.range
	local count = 1
	caster:SetContextThink(DoUniqueString("thtd_clownpiece_01_cast"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end		
			count = count - 1
			if count < 0 then
				ClownpieceRemoveAllBall(keys)			
				return nil
			end

			for i=1,12 do
				local forward1 = Vector(math.cos(i*math.pi/6),math.sin(i*math.pi/6),0)
				local forward2 = Vector(math.cos(i*math.pi/6+math.pi/2),math.sin(i*math.pi/6+math.pi/2),0)
				local startPoint = caster.thtd_clown_ball["Vector"]
				local endPoint = caster.thtd_clown_ball["Vector"] + forward1 * range + Vector(0,0,128)
				local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_hecatia/ability_hecatia_moon_reflex.vpcf", PATTACH_CUSTOMORIGIN, caster)
				ParticleManager:SetParticleControl(effectIndex , 0, startPoint)
				ParticleManager:SetParticleControl(effectIndex , 1, endPoint)
				ParticleManager:SetParticleControl(effectIndex , 9, startPoint)
				ParticleManager:DestroyParticleSystem(effectIndex,false)
			end

			local targets = THTD_FindUnitsInRadius(caster,caster.thtd_clown_ball["Vector"],range)
			local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * (1 + powerDamage)
		
			for k,v in pairs(targets) 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

			return 0.5
		end,
	0.3)	
end

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

	if caster.thtd_clown_ball ~= nil then
		local effectIndex = caster.thtd_clown_ball["EffectIndex"]
		ParticleManager:DestroyParticleSystem(effectIndex,true)
		caster.thtd_clown_ball = nil
	end	
end

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

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_junko/ability_junko_03.vpcf", PATTACH_CUSTOMORIGIN, nil)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)			
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	local powerRange = 0	
	local powerDuration = 0
	local pv = caster:GetAbilityPowerValue(keys.ability:GetAbilityName())
	if pv ~= nil then 
		powerRange = pv[1]			
		powerDuration = pv[2]		
	end

	local radius = keys.radius + powerRange
	local duration_time = keys.duration_time + powerDuration	
	local damage_up_other = keys.damage_up_other
	
	local targets = THTD_FindUnitsInRadius(caster,targetPoint,radius)
	local count = #targets
	for k,v in pairs(targets) do	
		local modifier = v:FindModifierByName("modifier_clownpiece_debuff")		
		if modifier==nil then	
			keys.ability:ApplyDataDrivenModifier(caster,v,"modifier_clownpiece_debuff", {Duration = duration_time})			
		else
			modifier:SetDuration(duration_time,false)
		end		
	end	

	if count > 0 then
		local powerDamageUp = 0
		local pv = caster:GetAbilityPowerValue("thtd_clownpiece_02")
		if pv ~= nil then 
			powerDamageUp = pv[3]		
		end		
		local factor = powerDamageUp + keys.damage_up_self
		caster.thtd_clownpiece_02_buff_bonus = count * keys.damage_up_other
		caster.thtd_clownpiece_02_self_bonus = caster.thtd_clownpiece_02_buff_bonus * factor

		local hero = caster:GetOwner()
		for k,v in pairs(hero.thtd_hero_tower_list) do
			local unitName = v:GetUnitName()			
			if unitName == "clownpiece" or unitName == "lily" or unitName == "luna" or unitName == "star" or unitName == "sunny" or unitName == "cirno" or unitName == "daiyousei" or unitName == "mugiyousei" or unitName == "hanadayousei" or unitName == "maidyousei" then
				v:RemoveModifierByName("modifier_clownpiece_buff")				
				keys.ability:ApplyDataDrivenModifier(caster,v,"modifier_clownpiece_buff", {Duration = duration_time}):SetStackCount(count)	
			end			
		end
	end
end

function OnCreatedClownpiece02Buff(keys)	
	if keys.target:GetUnitName() == "clownpiece" then 
		keys.target:AddDamageOutgoingAll(keys.caster.thtd_clownpiece_02_self_bonus or 0, "thtd_clownpiece_02_bonus")	
	else
		keys.target:AddDamageOutgoingAll(keys.caster.thtd_clownpiece_02_buff_bonus or 0, "thtd_clownpiece_02_bonus")	
	end
end

function OnDestroyClownpiece02Buff(keys)
	keys.target:AddDamageOutgoingAll("thtd_clownpiece_02_bonus")	
end

function OnSpellStartClownpiece03(keys)
	local caster = keys.caster
	local targetPoint = keys.target_points[1]
	local casterPoint = caster:GetAbsOrigin()
	local forward = (targetPoint - casterPoint):Normalized()
	
	local powerDuration = 0
	local pv = caster:GetAbilityPowerValue(keys.ability:GetAbilityName())
	if pv ~= nil then 					
		powerDuration = pv[2]	
	end	
	
	if caster:IsPower666() then 
		for i=1,6 do
			local forward1 = Vector(math.cos(i*math.pi/3),math.sin(i*math.pi/3),0)
			local effectIndex = ParticleManager:CreateParticle("particles/econ/items/jakiro/jakiro_ti8_immortal_head/jakiro_ti8_dual_breath_fire.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, casterPoint)
			ParticleManager:SetParticleControl(effectIndex, 1, forward1 * keys.distance)
			ParticleManager:DestroyParticleSystem(effectIndex,false)
		end
		local targets = THTD_FindUnitsInRadius(caster,casterPoint,keys.distance + 100)
		for k,v in pairs(targets) do
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_clownpiece_03_debuff", {Duration = keys.duration_time + powerDuration})
		end
	else
		local effectIndex = ParticleManager:CreateParticle("particles/econ/items/jakiro/jakiro_ti8_immortal_head/jakiro_ti8_dual_breath_fire.vpcf", PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(effectIndex, 0, casterPoint)
		ParticleManager:SetParticleControl(effectIndex, 1, forward * keys.distance)
		ParticleManager:DestroyParticleSystem(effectIndex,false)
		-- local effectIndex2 = ParticleManager:CreateParticle("particles/econ/items/jakiro/jakiro_ti8_immortal_head/jakiro_ti8_dual_breath_ice.vpcf", PATTACH_CUSTOMORIGIN, caster)
		-- ParticleManager:SetParticleControl(effectIndex2, 0, casterPoint)
		-- ParticleManager:SetParticleControl(effectIndex2, 1, (targetPoint - casterPoint):Normalized() * 1000)
		-- ParticleManager:DestroyParticleSystem(effectIndex2,false)

		local targets = FindUnitsInLine(
			caster:GetTeamNumber(), 
			casterPoint, 
			casterPoint + forward * (keys.distance + 100),
			nil, 
			300,
			keys.ability:GetAbilityTargetTeam(), 
			keys.ability:GetAbilityTargetType(), 
			keys.ability:GetAbilityTargetFlags()
		)	
		
		for k,v in pairs(targets) do
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_clownpiece_03_debuff", {Duration = keys.duration_time + powerDuration})
		end
	end
end

function OnThinkClownpiece03(keys)
	local caster = keys.caster
	local target = keys.target

	local powerDamage = 0
	local pv = caster:GetAbilityPowerValue(keys.ability:GetAbilityName())
	if pv ~= nil then 					
		powerDamage = pv[1]	
	end
	
	if caster:IsPower666() then powerDamage = powerDamage + 30 end
	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) + powerDamage * caster:THTD_GetStarDamage()
	if caster:IsPower999() then damage = damage * 3 end
	damage = damage * keys.tick

	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
