Scriptname _00E_Class_PerkScript extends ObjectReference  

Import Game

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

Event onActivate(ObjectReference akActionRef)

	int iButton
	
	iButton = __Config_DisplayMessage.Show()
	If __Config_MultipleLevelsConfirm
		__Config_MultipleLevels=true
	EndIf
	If (__Config_MultipleLevels) && !(__Config_MultipleLevelsTwoLevels)
		If iButton < 3 
			If (__Config_TeachesTalent)
				TeachTalent()
			Elseif !(__Config_TeachesTalent)
				TeachPerk()
			EndIf
			TalentPoints.SetValue(TalentPoints.GetValueInt() - 1)	
			PlayerREF.SetAV("dragonsouls", TalentPoints.GetValueInt())
			PlayVisuals()
		EndIf

	ElseIf (__Config_MultipleLevels) && (__Config_MultipleLevelsTwoLevels)
		If iButton < 2
			TeachPerk()
			TalentPoints.SetValue(TalentPoints.GetValueInt() - 1)	
			PlayerREF.SetAV("dragonsouls", TalentPoints.GetValueInt())
			PlayVisuals()
		EndIf
		
	Elseif !__Config_MultipleLevels
		If iButton == 0
			PlayerREF.AddPerk(__Config_PerkToTeach1)
			If 	__Config_QuestToStart != None
				__Config_QuestToStart.Start()
			EndIf
            If ( AdditionalPerk )
                PlayerREF.AddPerk(AdditionalPerk)
            EndIf
			If sendModEventOnUnlock
				SendModEvent(modEventName)
			Endif

			TalentPoints.SetValue(TalentPoints.GetValueInt() - 1)	
			PlayerREF.SetAV("dragonsouls", TalentPoints.GetValueInt())
			PlayVisuals()
		EndIf

	EndIf
	
EndEvent

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

Function TeachPerk()

	if !(PlayerREF.HasPerk(__Config_PerkToTeach1))
		PlayerREF.AddPerk(__Config_PerkToTeach1)
		If 	__Config_QuestToStart != None
			__Config_QuestToStart.start()
		EndIf
		if sendModEventOnUnlock
			SendModEvent(modEventName)
		Endif
		if ( AdditionalPerk )
			PlayerREF.AddPerk(AdditionalPerk)
		endif
	elseif (PlayerREF.HasPerk(__Config_PerkToTeach1)) && !(PlayerREF.HasPerk(__Config_PerkToTeach2))
		PlayerREF.AddPerk(__Config_PerkToTeach2)
	else
		PlayerREF.AddPerk(__Config_PerkToTeach3)
	endif
EndFunction
	
Function TeachTalent()

	if !(Game.IsWordUnlocked(__Config_WordToTeach1))
		PlayerREF.AddShout(__Config_TalentToTeach)
		WordToTeach = __Config_WordToTeach1
		PlayerREF.AddPerk(__Config_PerkToTeach1)
		If 	__Config_QuestToStart != None
			__Config_QuestToStart.start()
		EndIf
		if sendModEventOnUnlock
			SendModEvent(modEventName)
		Endif
	elseif (Game.IsWordUnlocked(__Config_WordToTeach1)) && !(Game.IsWordUnlocked(__Config_WordToTeach2))
		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 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() as Int)

	ObjectReference MemoryPoint = Self.GetLinkedRef()
	MAGConjureImpact.Play(PlayerREF)
	MemoryPoint.PlayAnimation("PlayAnim02")
	if( MemoryPoint.GetLinkedRef(TestLine) )
		MemoryPoint.GetLinkedRef(TestLine).PlayAnimation("Unlock")
	endif
	if( MemoryPoint.GetLinkedRef(__PerkLine01) )
		MemoryPoint.GetLinkedRef(__PerkLine01).PlayAnimation("Unlock")
	endif
	if( MemoryPoint.GetLinkedRef(__PerkLine02) )
		MemoryPoint.GetLinkedRef(__PerkLine02).PlayAnimation("Unlock")
	endif
	if( MemoryPoint.GetLinkedRef(__PerkLine03) )
		MemoryPoint.GetLinkedRef(__PerkLine03).PlayAnimation("Unlock")
	endif
	if( MemoryPoint.GetLinkedRef(__PerkLine04) )
		MemoryPoint.GetLinkedRef(__PerkLine04).PlayAnimation("Unlock")
	endif
	if( MemoryPoint.GetLinkedRef(__PerkLine05) )
		MemoryPoint.GetLinkedRef(__PerkLine05).PlayAnimation("Unlock")
	endif
	if( MemoryPoint.GetLinkedRef(__PerkLine06) )
		MemoryPoint.GetLinkedRef(__PerkLine06).PlayAnimation("Unlock")
	endif

	Sound unlockSound = Game.GetForm(0x0003A1E7) 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


;=====================================================================================
;              							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