Scriptname _00E_Game_TalentControlSC extends Actor
{Controls animation behaviour for all talents}


;=====================================================================================
;              							FUNCTIONS                  					 
;=====================================================================================

Function RegisterForActions()
	RegisterForActorAction(3)
	RegisterForActorAction(4)
EndFunction

Function UnregisterForActions()
	UnregisterForActorAction(3)
	UnregisterForActorAction(4)
EndFunction

; Called by the magic effect (_00E_Game_TalentCooldownSC)
Function RegisterCooldownMagicEffect(_00E_Game_TalentCooldownSC newCooldownMagicEffect)
	CooldownMagicEffect = newCooldownMagicEffect
EndFunction

Function RefreshCooldownSpell()
	; The order of operation below is probably the most race-condition-safe one (see the comment in OnUpdate)
	RegisterForSingleUpdate(2)
	RemoveSpell(_00E_Game_AbTalentCooldown)
	_00E_Game_TalentControl_CooldownResetDesired.SetValue(1)
	CooldownMagicEffect = None
EndFunction

Function SetTalentRecoveryTime(Shout talent, Float newRecoveryTime)
	If CooldownMagicEffect
		CooldownMagicEffect.SetShoutRecoveryTime(talent, newRecoveryTime)
	Else
		SetVoiceRecoveryTime(newRecoveryTime)
	EndIf
EndFunction


;=====================================================================================
;              							EVENTS                  					 
;=====================================================================================

Event OnInit()
	If self == PlayerREF
		GoToState("RealPlayer")
		RegisterForActions()
	EndIf
EndEvent

Event OnPlayerLoadGame()
	If self == PlayerREF ; A check just in case. Most likely this condition is always True
		If GetState() != "RealPlayer" ; Post-1.2.5.0 version update
			GoToState("RealPlayer")

			; In the previous version the player could end with a permament QuietCasting perk by mistake.
			If HasPerk(QuietCasting)
				RemovePerk(QuietCasting)
				Debug.Trace(self + ": Removed QuietCasting perk on version update")
			EndIf

			RefreshCooldownSpell()
		EndIf

		RegisterForActions()
	EndIf
EndEvent

State RealPlayer

	Event OnUpdate()
		; This OnUpdate is triggered by RegisterForUpdates in other scripts attached to the player, e.g. in _00E_EPUpdateFunctions.
		; So we must be cautious here.

		If (CooldownMagicEffect == None && _00E_Game_TalentControl_CooldownResetDesired.GetValue() != 0)
			_00E_Game_TalentControl_CooldownResetDesired.SetValue(0)
			AddSpell(_00E_Game_AbTalentCooldown, False)
		EndIf
	EndEvent

	Event OnCellLoad()
		RegisterForActions()
	EndEvent

	Event OnActorAction(int actionType, Actor akActor, Form source, int slot)
		Shout shoutUsed = source as Shout
		if(!shoutUsed)
			return
		EndIf

		If actionType == 3
			LastShoutUsed = shoutUsed
		EndIf

		If shoutUsed == _00E_A1_Onslaught
			If ActionType == 4 
				_00E_A1_OnslaughtCloakSP.Cast(self, self)
			;How was this suppsosed to work?
			;ElseIf ActionType == 3
			;	_00E_A1_Tutorial_Onslaught.ShowAsHelpMessage("Onslaught", 5, 1, 1)
			EndIf
		EndIf

		If actionType == 3 && _00E_Game_SilentShouts.HasForm(shoutUsed)
			QuietCastCounter += 1
			Int curQuietCastCounter = QuietCastCounter
			AddPerk(QuietCasting)

			; The real RemovePerk(QuietCasting) should happen in OnSpellCast.
			; But in case it won't for any reason, we'll wait for fShoutTime2 setting + extra padding, and force it.
			Float waitTime = Game.GetGameSettingFloat("fShoutTime2") + 1.5
			Utility.Wait(waitTime)

			If QuietCastCounter == curQuietCastCounter
				RemovePerk(QuietCasting)
			EndIf
		EndIf
	EndEvent

	Event OnSpellCast(Form maybeSpell)
		If(!LastShoutUsed)
			return
		EndIf

		Spell castSpell = maybeSpell as Spell	
		If castSpell
			If (castSpell == LastShoutUsed.GetNthSpell(0) || castSpell == LastShoutUsed.GetNthSpell(1) || castSpell == LastShoutUsed.GetNthSpell(2))
				LastShoutUsed = None
				CooldownMagicEffect.OnShoutCast()
				RemovePerk(QuietCasting)
			EndIf
		EndIf
		
	EndEvent

EndState


Event OnActorAction(int actionType, Actor akActor, Form source, int slot)
	; If we get here, most likely it's not the real player, it's a copy.
	; So clear its pre-1.2.5.0 action registrations to not waste resources on this ever again.
	If self != PlayerREF
		UnregisterForActions()
	EndIf
EndEvent

;=====================================================================================
;              							PROPERTIES                  					 
;=====================================================================================

Formlist Property _00E_Game_SilentShouts Auto

Actor Property PlayerREF Auto

;Message Property _00E_A1_Tutorial_Onslaught Auto

Spell Property _00E_A1_OnslaughtCloakSP Auto
Spell Property _00E_Game_AbTalentCooldown Auto 

GlobalVariable Property _00E_Game_TalentControl_CooldownResetDesired Auto

Perk Property QuietCasting Auto

Shout Property _00E_A1_Onslaught Auto

_00E_Game_TalentCooldownSC CooldownMagicEffect = None
Shout LastShoutUsed = None

Int QuietCastCounter = 0