ScriptName footprintsAddFootstepsScript Extends ActiveMagicEffect

Race[] 	Property FootprintsRacesV1 Auto
Spell[] Property FootprintsSpellsV1 Auto

Spell 	Property footprintsIgnoreAbility Auto
Spell 	Property footprintsFootstepsAbilityHuman Auto

Keyword Property ActorTypeNPC Auto
Keyword Property footprintsFootstepsEffect Auto

; CUSTOM
FormList Property footprintsCustomSpellsFLST Auto
GlobalVariable Property fp_UseCustomFootprints Auto

Event OnEffectStart( Actor target, Actor caster )

	If ( !target )
		return
	EndIf
	
	If ( fp_UseCustomFootprints.GetValue() )
		;Debug.Trace( "footprintsAddFootstepsScript - Checking for custom race..." )
		int iCustomFLSTSize = footprintsCustomSpellsFLST.GetSize()
		;Debug.Trace( "footprintsCustomSpellsFLST size: " + iCustomFLSTSize )
		While ( iCustomFLSTSize )
			iCustomFLSTSize -= 1
			Spell CustomSpell = footprintsCustomSpellsFLST.GetAt( iCustomFLSTSize ) as Spell
			;Debug.Trace( "footprintsAddFootstepsScript - Attempting to add " + CustomSpell + " to " + target )
			If ( CustomSpell && target.AddSpell( CustomSpell, false ) )
				If ( target.HasMagicEffectWithKeyword( footprintsFootstepsEffect ) )
					;Debug.Trace( "footprintsAddFootstepsScript - Added CUSTOM spell to " + target + target.GetRace() )
					return
				Else
					target.RemoveSpell( CustomSpell )
				EndIf
			EndIf
		EndWhile
	EndIf
	
	If ( target.HasKeyword( ActorTypeNPC ) && target.AddSpell( footprintsFootstepsAbilityHuman ) )
		;Debug.Trace( "footprintsAddFootstepsScript - Adding NPC spell to " + target + target.GetRace() )
		return
	EndIf
	
	;Debug.Trace( "footprintsAddFootstepsScript - Finding Creature spell to " + target + target.GetRace() )
	int i = FootprintsRacesV1.Find( target.GetRace() )
	If ( i >= 0 )
		;Debug.Trace( "footprintsAddFootstepsScript - Found Race at Index " + i )
		If ( target.AddSpell( FootprintsSpellsV1[i] ) )
			;Debug.Trace( "footprintsAddFootstepsScript - Adding " + FootprintsSpellsV1[i] + " to " + target + target.GetRace() )
			return
		EndIf
	EndIf
	
	; Ignore
	target.AddSpell( footprintsIgnoreAbility )

EndEvent

bool Function HasMagicEffectInList( Actor akTarget, MagicEffect[] akMagicEffectArray )
	int aiIndex = 0
	While ( aiIndex < akMagicEffectArray.Length )
		If ( akTarget.HasMagicEffect(akMagicEffectArray[aiIndex]) )
			;Debug.Trace( akTarget + " has " + akMagicEffectArray[aiIndex] )
			return True
		EndIf
		aiIndex += 1
	EndWhile
	return False
EndFunction
