Scriptname Tsc_ThrowingWeapons_StaminaScript extends activemagiceffect

Actor player
Actor Property PlayerRef  Auto
SPELL Property NPCAmmoSpell  Auto
Ammo Property AmmoItem  Auto
Scroll Property ScrollItem  Auto
Tsc_ThrowingWeapons_QuestScript Property QuestScript Auto

int modCRC_old
int magidle_old
int magatk_old

Event OnEffectStart(Actor akTarget, Actor akCaster)
	player = akTarget
	if (player == PlayerRef)
		RegisterForAnimationEvent(akTarget, "MRh_SpellFire_Event")
	elseIf (!player.HasSpell(NPCAmmoSpell))
		player.AddSpell(NPCAmmoSpell, false)
	endIf
	modCRC_old = QuestScript.getModCRC()
	if (modCRC_old != 0)
		magidle_old = player.GetAnimationVariableInt("FNISaa_magidle")
		magatk_old = player.GetAnimationVariableInt("FNISaa_magatk")
		if (player == PlayerRef && Game.GetCameraState() == 0)
			RegisterforCameraState()
		else
			bool result = FNIS_aa.SetAnimGroup(player, "_magidle", QuestScript.getMagidle_base(), 0, "Spears", true)
			result = result && FNIS_aa.SetAnimGroup(player, "_magatk", QuestScript.getMagatk_base(), 0, "Spears", true)
			if !result
				Debug.Trace("FNIS AA could not be activated for Throwing Weapons Lite for following actor:" + player)
			endIf
		endIf
	endIf
endEvent

Event OnPlayerCameraState(int oldState, int newState)
	if (newState != 0)
		bool result = FNIS_aa.SetAnimGroup(player, "_magidle", QuestScript.getMagidle_base(), 0, "Spears", true)
		result = result && FNIS_aa.SetAnimGroup(player, "_magatk", QuestScript.getMagatk_base(), 0, "Spears", true)
		if !result
			Debug.Trace("FNIS AA could not be activated for Throwing Weapons Lite for following actor:" + player)
		endIf
		UnregisterforCameraState()
	EndIf	
EndEvent

Event OnAnimationEvent(ObjectReference akSource, string asEventName)
	player.DamageAV("Stamina", 20.0)
endEvent

Event OnSpellCast(Form akSpell)
	if (player == PlayerRef)
		int ammoCount = player.GetItemCount(AmmoItem)
		int scrollCount = player.GetItemCount(ScrollItem)
		if (scrollCount < ammoCount)
			ammoCount = ammoCount - scrollCount
			player.removeItem(AmmoItem, ammoCount, true)
		endIf
	endIf
endEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	int ammoCount = akTarget.GetItemCount(AmmoItem)
	int scrollCount = akTarget.GetItemCount(ScrollItem)
	if (scrollCount < ammoCount)
		ammoCount = ammoCount - scrollCount
		akTarget.removeItem(AmmoItem, ammoCount, true)
	endIf
	if (akTarget == PlayerRef)
		UnregisterForAnimationEvent(akTarget, "MRh_SpellFire_Event")
		UnregisterforCameraState()
	endIf
	int current_crc = QuestScript.getModCRC()
	if (current_crc != 0)
		if (current_crc != modCRC_old)
			magidle_old = 0
			magatk_old = 0
		endIf
		akTarget.SetAnimationVariableInt("FNISaa_magidle", magidle_old)
		akTarget.SetAnimationVariableInt("FNISaa_magatk", magatk_old)
	endIf
endEvent