Scriptname FXWitchlightAttachSCRIPT extends ActiveMagicEffect conditional
{Attaches and manages witchlights's fx}

VisualEffect Property WitchlightFXAttachEffect Auto
Spell Property WispDrainAttack Auto
Sound Property NPCWitchlightAttackM Auto
Actor Property PlayerREF Auto

ObjectReference selfRef

Event OnEffectStart(Actor Target, Actor Caster)
	selfRef = caster		

	; Small version update.
	; The new script properties do not get auto-filled if the NPC ref has been already inited
	If PlayerREF == None
		PlayerREF = Game.GetPlayer()
	EndIf
	If WispDrainAttack == None
		WispDrainAttack = Game.GetFormFromFile(0x00091F78, "Skyrim.esm") as Spell
	EndIf
	If NPCWitchlightAttackM == None
		NPCWitchlightAttackM = Game.GetFormFromFile(0x00036E7E, "GavrantFixes_210510.esp") as Sound
	EndIf
	
	;USKP 2.0.1 - Stop this from attaching to the player.
	If selfRef == PlayerREF
		Dispel()
		Return
	EndIf
	 
	; only attack FX once the 3D is loaded
	Int iLoadCountdown = 50
	While selfRef.Is3DLoaded() == False && iLoadCountdown > 0
		iLoadCountdown -= 1
	EndWhile
	;USKP 2.0.2 - So despite ALL THAT above, we still need to check this. Stupid.
	If selfRef.Is3DLoaded()
		WitchlightFXAttachEffect.Play(selfRef, -1)
	EndIf
EndEvent

Event OnSpellCast(Form akSpell)
	If akSpell == WispDrainAttack
		NPCWitchlightAttackM.Play(selfRef)
	EndIf
EndEvent

Event OnEffectFinish(Actor Target, Actor Caster)
	WitchlightFXAttachEffect.Stop(selfRef)
EndEvent