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

	if caster.__koishi_lock ~= true then 
		caster.__koishi_lock = true
		local targets = THTD_FindUnitsInRadius(caster,caster:GetOrigin(),keys.range)

		for i=1,#targets do
			local unit = targets[i]
			if unit~=nil and unit:IsNull()==false and unit~=target and unit:IsAlive() then
				caster:PerformAttack(unit,true,false,true,false,true,false,true)
				if RollPercentage(keys.chance) then
					local DamageTable = {
				   			ability = keys.ability,
				            victim = unit, 
				            attacker = caster, 
				            damage = caster:THTD_GetAbilityPowerDamage(keys.ability), 
				            damage_type = keys.ability:GetAbilityDamageType(), 
				            damage_flags = DOTA_DAMAGE_FLAG_NONE
				   	}
				   	UnitDamageTarget(DamageTable)

				   	local effectIndex = ParticleManager:CreateParticle("particles/units/heroes/hero_dark_willow/dark_willow_bramble.vpcf", PATTACH_CUSTOMORIGIN, caster)
					ParticleManager:SetParticleControlEnt(effectIndex , 0, unit, 5, "follow_origin", Vector(0,0,0), true)
					ParticleManager:DestroyParticleSystemTimeFalse(effectIndex,1.0)
				end
			end
		end
		caster.__koishi_lock = false
	end
end

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

	local modifer = caster:FindModifierByName("modifier_koishi_02_attack_speed") or keys.ability:ApplyDataDrivenModifier(caster, caster, "modifier_koishi_02_attack_speed", nil)

	local max_count = keys.max_count
	if caster:HasModifier("passive_koishi_04_attack") then 
		max_count = keys.count_bonus
	end

	if modifer:GetStackCount() < max_count then			
		modifer:IncrementStackCount()
	end	
	modifer:SetDuration(keys.duration_time, false)
end

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

	if target:THTD_IsTower() and target:HasModifier("modifier_koishi_03_buff") == false then
		caster.thtd_last_cast_unit = target

		local time = keys.duration_time
		if caster:FindAbilityByName("thtd_koishi_01"):GetLevel() > 1 then
			time = keys.combo_time
		end

		local bonus = keys.power_bonus	
		if target:GetUnitName() == "satori" then 
			if target:IsPower999() then 
				bonus = bonus * (keys.self_crit	+ 10)
			else
				bonus = bonus * keys.self_crit	
			end
		elseif target:GetUnitName() == "koishi" then 
			bonus = bonus * keys.self_crit	
		end		
		target.koishi_03_power_bonus = bonus

		keys.ability:ApplyDataDrivenModifier(caster,target,"modifier_koishi_03_buff", {Duration = time})			
		target:EmitSound("Hero_OgreMagi.Bloodlust.Target")
	else
		keys.ability:EndCooldown()
	end
end

function OnCreatedKoishi03Buff(keys)
	local target = keys.target	
	local bonus = target.koishi_03_power_bonus
	target:THTD_AddBasePower(bonus, "thtd_koishi_03_bonus")
	target:THTD_AddBaseAttack(bonus, "thtd_koishi_03_bonus")	
end

function OnDestroyKoishi03Buff(keys)
	local target = keys.target	
	target:THTD_AddBasePower("thtd_koishi_03_bonus")
	target:THTD_AddBaseAttack("thtd_koishi_03_bonus")
end

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

	if caster:HasModifier("passive_koishi_04_attack") then 
		keys.ability:EndCooldown()
		keys.ability:StartCooldown(1.0)
		caster:GiveMana(caster:GetRealManaCost(keys.ability))
		return
	end

	local time = keys.duration_time

	caster:StartGesture(ACT_DOTA_CAST_ABILITY_4)
	caster:SetAttackCapability(DOTA_UNIT_CAP_MELEE_ATTACK)

	keys.ability:ApplyDataDrivenModifier(caster, caster, "passive_koishi_04_attack",nil)
	caster:EmitSound("Voice_Thdots_Koishi.AbilityKoishi041")
	caster:EmitSound("Hero_VengefulSpirit.WaveOfTerror")

	local effectIndex = ParticleManager:CreateParticle("particles/econ/events/fall_major_2015/teleport_end_fallmjr_2015_lvl2_black.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin())
	ParticleManager:DestroyParticleSystemTimeFalse(effectIndex,0.8)

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

			caster:SetContextThink(DoUniqueString("thtd_koishi04_buff_remove"), 
				function()
					if GameRules:IsGamePaused() then return 0.1 end
					caster:StartGesture(ACT_DOTA_CAST_ABILITY_4_END)
					local effectIndex = ParticleManager:CreateParticle("particles/econ/events/fall_major_2015/teleport_end_fallmjr_2015_lvl2_black.vpcf", PATTACH_CUSTOMORIGIN, caster)
					ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin())
					ParticleManager:DestroyParticleSystemTimeFalse(effectIndex,0.25)
					caster:EmitSound("Voice_Thdots_Koishi.AbilityKoishi042")
					caster:EmitSound("Hero_VengefulSpirit.WaveOfTerror")

					caster:SetContextThink(DoUniqueString("thtd_koishi04_buff_remove"), 
						function()
							if GameRules:IsGamePaused() then return 0.1 end
							caster:RemoveGesture(ACT_DOTA_CAST_ABILITY_4_END)		
							
							caster:SetAttackCapability(DOTA_UNIT_CAP_RANGED_ATTACK)
							caster:RemoveModifierByName("passive_koishi_04_attack")							
							return nil
						end,
					0.25)

					return nil
				end,
			time)

			return nil
		end,
	0.8)
end

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

	local count = 3
	caster:SetContextThink(DoUniqueString("thtd_koishi03_buff_remove"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			local num = RandomInt(1,6)
			local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_koishi/ability_koishi_04_attack_0"..num..".vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex , 0, caster:GetOrigin() - Vector(0,0,20))
			ParticleManager:SetParticleControlForward(effectIndex , 0, caster:GetForwardVector()+Vector(0,0,num/12))
			ParticleManager:SetParticleControlForward(effectIndex , 1, caster:GetForwardVector())
			ParticleManager:DestroyParticleSystem(effectIndex,false)
		
		   	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_koishi/ability_koishi_04_attack_landed.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex , 0, target:GetOrigin() + Vector(0,0,-260))
			ParticleManager:SetParticleControl(effectIndex , 1, target:GetOrigin() + Vector(0,0,0))
			ParticleManager:SetParticleControl(effectIndex , 2, target:GetOrigin() + Vector(0,0,0))
			ParticleManager:SetParticleControl(effectIndex , 3, target:GetOrigin() + Vector(0,0,0))
			ParticleManager:DestroyParticleSystem(effectIndex,false)

			if count > 0 then
				count = count - 1
				return 0.1
			else
				return nil
			end
		end,
	0.1)
end

function OnKoishi04Kill(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.unit

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/moluo/ability_moluo03_explosion.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex , 0, target:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex , 3, target:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end

function OnCreatedKoishi04Buff(keys)
	local target = keys.target
	target:THTD_AddBasePower(keys.power_base, "thtd_koishi_04_bonus")
	target:THTD_AddBaseAttack(keys.power_base, "thtd_koishi_04_bonus")	
	target:THTD_AddPowerPercentage(keys.power_up, "thtd_koishi_04_bonus")
	target:THTD_AddAttackPercentage(keys.power_up, "thtd_koishi_04_bonus")	
end

function OnDestroyKoishi04Buff(keys)
	local target = keys.target
	target:THTD_AddBasePower("thtd_koishi_04_bonus")
	target:THTD_AddBaseAttack("thtd_koishi_04_bonus")	
	target:THTD_AddPowerPercentage("thtd_koishi_04_bonus")
	target:THTD_AddAttackPercentage("thtd_koishi_04_bonus")	
end