
scope BeastAttack initializer ini
	import Units
	import DamageType
	import Statistics
	import DamageOverTime
	import StructuredDD
	import ItemFieldConditioningKit

	constant string FX             = "Objects\\Spawnmodels\\NightElf\\EntBirthTarget\\EntBirthTarget.mdl"
	constant string BLOOD_FX       = "Objects\\Spawnmodels\\Other\\HumanBloodCinematicEffect\\HumanBloodCinematicEffect.mdl"

	constant string THUNDER_FX_BIG = "Abilities\\Weapons\\Bolt\\BoltImpact.mdl"
	constant string THUNDER_FX     = "Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl"

	constant integer DOT_EXCLUSIVITY     = 'batk'
	constant integer THUNDER_EXCLUSIVITY = 'bttk'

	constant integer STUDDED_LEATHER_ID  = 'I001'

	constant real BASE_DAMAGE              = 50.
	constant real LEATHER_DAMAGE_REDUCTION = 10.
	constant real DAMAGE_PER_SECOND        =  2.
	constant real DOT_DURATION             =  5.

	constant real THUNDER_DURATION         =  5.
	constant real THUNDER_BONUS_MULTIPLIER =   .2

	function hand takes nothing returns nothing
		unit eS = GetEventDamageSource()
		unit tU = GetTriggerUnit()
		integer id = GetUnitTypeId(eS)
		real bd = BASE_DAMAGE

		// update most recent damage timing
		if damageTimings.hasInt(tU.getHandleId()) then
			damageTimings.saveInt(tU.getHandleId(), Statistics.secondsElapsed)
		end

		if (id == Units.BEAST_ID or id == Units.LESSER_BEAST_ID) and DamageType.get() == DamageType.NULLED then
			// check for studded leather
			if UnitHasItemOfTypeBJ(tU, STUDDED_LEATHER_ID) then
				bd -= LEATHER_DAMAGE_REDUCTION
			end

			// check for thunder
			if Units.thunderClock.hasInt(eS.getHandleId()) and (Statistics.secondsElapsed - Units.thunderClock.loadInt(eS.getHandleId())) < THUNDER_DURATION then
				bd += GetUnitState(tU, UNIT_STATE_MAX_LIFE) * THUNDER_BONUS_MULTIPLIER
				Units.thunderClock.saveInt(eS.getHandleId(), 0)

				DestroyEffect(AddSpecialEffectTarget(THUNDER_FX_BIG, tU, "origin"))
				DamageOverTime.add(eS, tU, 0., 2., THUNDER_FX, DamageOverTime.FX_FLAG_COORDS, THUNDER_EXCLUSIVITY)
			end

			DestroyEffect(AddSpecialEffectTarget(FX, tU, "origin"))
			DamageType.dealCodeDamage(eS, tU, bd)
			DamageOverTime.add(eS, tU, DAMAGE_PER_SECOND, DOT_DURATION, BLOOD_FX, DamageOverTime.FX_FLAG_ATTACH, DOT_EXCLUSIVITY)
		end
	end

	function ini takes nothing returns nothing
		StructuredDD.addHandler(function hand)
	end
endscope
