Scriptname _00E_Class_PerkScript extends ObjectReference  

Import Game

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

Event OnActivate(ObjectReference akActionRef)
	If akActionRef != PlayerREF
		Return
	EndIf

	GoToState("DoNothing")
	
	Int iButton = __Config_DisplayMessage.Show()

	If __Config_MultipleLevelsConfirm
		__Config_MultipleLevels = True
	EndIf

	If __Config_MultipleLevels

		If __Config_MultipleLevelsTwoLevels
			If iButton < 2
				TeachPerk()
				FinalizeUnlock()
				Return
			EndIf
		Else ; __Config_MultipleLevelsTwoLevels == False
			If iButton < 3 
				If __Config_TeachesTalent
					TeachTalent()
				Else
					TeachPerk()
				EndIf
				FinalizeUnlock()
				Return
			EndIf
		EndIf

	Else ; __Config_MultipleLevels == False

		If iButton == 0
			TeachFirstPerk()
			FinalizeUnlock()
			Return
		EndIf

	EndIf

	GoToState("")
EndEvent

State DoNothing
	Event OnActivate(ObjectReference akActionRef)
		; Do nothing
	EndEvent	
EndState

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

Function TeachPerk()
	If PlayerREF.HasPerk(__Config_PerkToTeach1) == False
		TeachFirstPerk()
	ElseIf PlayerREF.HasPerk(__Config_PerkToTeach2) == False
		PlayerREF.AddPerk(__Config_PerkToTeach2)
	Else
		PlayerREF.AddPerk(__Config_PerkToTeach3)
	EndIf
EndFunction
	
Function TeachTalent()
	If Game.IsWordUnlocked(__Config_WordToTeach1) == False
		PlayerREF.AddShout(__Config_TalentToTeach)
		WordToTeach = __Config_WordToTeach1
		TeachFirstPerk(False)
	ElseIf Game.IsWordUnlocked(__Config_WordToTeach2) == False
		WordToTeach = __Config_WordToTeach2
		PlayerREF.AddPerk(__Config_PerkToTeach2)
	Else
		WordToTeach = __Config_WordToTeach3
		PlayerREF.AddPerk(__Config_PerkToTeach3)
	EndIf

	Game.UnlockWord(WordToTeach)
	Game.TeachWord(WordToTeach)
	SetRecoveryTime(WordToTeach)
EndFunction

Function TeachFirstPerk(Bool bTeachAdditionalPerk = True)
	PlayerREF.AddPerk(__Config_PerkToTeach1)
	If __Config_QuestToStart
		__Config_QuestToStart.Start()
	EndIf
	If AdditionalPerk && bTeachAdditionalPerk
		PlayerREF.AddPerk(AdditionalPerk)
	EndIf
	If sendModEventOnUnlock
		SendModEvent(modEventName)
	EndIf
EndFunction

Function SetRecoveryTime(WordOfPower WordToTeach)
{This function handles the issue of cooldowns not depending on the value in the CK but on the duration the player presses the [Shout] key on his keyboard.}

	If WordToTeach == __Config_WordToTeach2
		__Config_TalentToTeach.SetNthRecoveryTime(0, fRecoveryTime02)
		__Config_TalentToTeach.SetNthRecoveryTime(1, fRecoveryTime02)
	ElseIf	WordToTeach == __Config_WordToTeach3
		__Config_TalentToTeach.SetNthRecoveryTime(0, fRecoveryTime03)
		__Config_TalentToTeach.SetNthRecoveryTime(1, fRecoveryTime03)
		__Config_TalentToTeach.SetNthRecoveryTime(2, fRecoveryTime03)
	EndIf
EndFunction

Function PlayVisuals()
	;Fix for ERB-1355, somehow _00E_TalentpointsRemaining was not filled in that save game on all perk activators
	If _00E_TalentpointsRemaining == None
		_00E_TalentpointsRemaining  = Game.GetFormFromFile(0x000431FB, "Skyrim.esm") as Message
	EndIf
	_00E_TalentpointsRemaining.Show(TalentPoints.GetValueInt())

	ObjectReference MemoryPoint = Self.GetLinkedRef()

	MAGConjureImpact.Play(PlayerREF)

	MemoryPoint.PlayAnimation("PlayAnim02")
	PlayUnlockAnimationOnLinkedRef(MemoryPoint, TestLine)
	PlayUnlockAnimationOnLinkedRef(MemoryPoint, __PerkLine01)
	PlayUnlockAnimationOnLinkedRef(MemoryPoint, __PerkLine02)
	PlayUnlockAnimationOnLinkedRef(MemoryPoint, __PerkLine03)
	PlayUnlockAnimationOnLinkedRef(MemoryPoint, __PerkLine04)
	PlayUnlockAnimationOnLinkedRef(MemoryPoint, __PerkLine05)
	PlayUnlockAnimationOnLinkedRef(MemoryPoint, __PerkLine06)

	GoToState("") ; Time to unlock OnActivate

	Sound unlockSound = Game.GetFormFromFile(0x0003A1E7, "Skyrim.esm") as Sound
	unlockSound.Play(PlayerREF)

	MAGDragonPowerAbsorbManEffect.Play(PlayerREF, 8, MemoryPoint)
	_00E_UnlockClassIMOD.Apply()
	Utility.Wait(0.1)
	_00E_Class_MemoryAbsorbFXS.Play(PlayerREF)
	Utility.Wait(3)
	_00E_Class_MemoryAbsorbFXS.Stop(PlayerREF)
	Utility.Wait(2)
	MAGDragonPowerAbsorbManEffect.Stop(PlayerREF)
EndFunction

Function PlayUnlockAnimationOnLinkedRef(ObjectReference MemoryPoint, Keyword linkedKwd)
	ObjectReference ref = MemoryPoint.GetLinkedRef(linkedKwd)
	If ref
		ref.PlayAnimation("Unlock")
	EndIf
EndFunction

Function FinalizeUnlock()
	TalentPoints.Mod(-1)
	PlayerREF.SetAV("dragonsouls", TalentPoints.GetValueInt())
	PlayVisuals()
EndFunction


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

bool Property __Config_bValue01 = False Auto
{Whether the talent this perk teaches needs one value (for example, magnitude). Usually true.}

bool Property __Config_bValue02 = False Auto
{Whether the talent this perk teaches needs a second value (for example, duration).}

bool Property __Config_bValue03 = False Auto
{Whether the talent this perk teaches needs third value (for example, distance).}

bool Property sendModEventOnUnlock = False Auto
String Property modEventName = "" Auto

bool Property __Config_MultipleLevels Auto
{Does this perk have multiple levels to teach? ALWAYS true for Talents! Max: 3}
bool Property __Config_MultipleLevelsTwoLevels Auto
{Does this multiple level perk only have two two ranks?}
bool Property __Config_TeachesTalent Auto
{Does this perk/memory teach a talent?}
float Property fRecoveryTime02 Auto
{Set this to the cooldown of the second talent.}
float Property fRecoveryTime03 Auto
{Set this to the cooldown of the third talent.}
Message Property __Config_DisplayMessage Auto
{The corresponding MessageBox to display when this perk is selected}
Perk Property __Config_PerkToTeach1 Auto
{Perk/First rank perk this memory teaches. Always fill!}
Perk Property __Config_PerkToTeach2 Auto
{Second rank of the perk this memory star teaches}
Perk Property __Config_PerkToTeach3 Auto
{Third rank of the perk this memory star teaches}
Perk Property AdditionalPerk Auto
Shout Property __Config_TalentToTeach Auto
{Talent this perk teaches. Leave empty if this perk doesn't teach a talent.}
Quest Property __Config_QuestToStart = None Auto
{When the first perk is added to the player, this quest is started, too}

WordOfPower Property __Config_WordToTeach1 Auto
{1st level of Talent this perk/memory teaches. Leave empty if this perk doesn't teach a talent.}
WordOfPower Property __Config_WordToTeach2 Auto
{2nd level of Talent this perk/memory teaches. Leave empty if this perk doesn't teach a talent.}
WordOfPower Property __Config_WordToTeach3 Auto
{3rd of Talent this perk/memory teaches. Leave empty if this perk doesn't teach a talent.}
WordOfPower Property WordToTeach Auto Hidden

Keyword Property TestLine Auto
Keyword Property __PerkLine01 Auto
Keyword Property __PerkLine02 Auto
Keyword Property __PerkLine03 Auto
Keyword Property __PerkLine04 Auto
Keyword Property __PerkLine05 Auto
Keyword Property __PerkLine06 Auto

Message Property _00E_TalentpointsRemaining Auto

GlobalVariable Property TalentPoints Auto
VisualEffect Property MAGDragonPowerAbsorbManEffect Auto
EffectShader Property _00E_Class_MemoryAbsorbFXS Auto

ImagespaceModifier Property _00E_UnlockClassIMOD Auto

Sound Property MAGConjureImpact Auto
 
Actor Property PlayerREF Auto

bool Property __Config_MultipleLevelsConfirm Auto