Scriptname _00E_SkillControl extends Quest Hidden 

import Utility

Function _ReadSkillBook(String skillName, Int skillTopLimit, Form bookItem, Message msgSkillIncrease, GlobalVariable availablePoints, Message msgNoPoints)
	Message failMsg = None

	While _SkillBookReadLocked
		WaitMenuMode(0.1)
	EndWhile
	_SkillBookReadLocked = True

	; DO NOT FORGET TO FALSE _SkillBookReadLocked ON RETURN

	If availablePoints.GetValue() >= 1
		Int curSkill = PlayerREF.GetBaseActorValue(skillName) as Int

		If curSkill >= skillTopLimit
			failMsg = _00E_Levelsystem_sSkillTooWellDeveloped
		ElseIf curSkill >= (skillTopLimit - 25) || (_00E_SkillbookWarning.Show() == 0) ; If cur. skill is within the book's limit or the player says "Yes"
			; Do skill increase
			curSkill += 1
			PlayerREF.SetActorValue(skillName, curSkill)
			availablePoints.Mod(-1)
			_SkillBookReadLocked = False
			msgSkillIncrease.Show(1, curSkill)
			Return
		EndIf
	Else ; availablePoints < 1
		failMsg = msgNoPoints
	EndIf

	; Processing failed read 
	_SkillBookReadLocked = False

	If failMsg != None
		failMsg.Show()
	EndIf

	PlayerREF.AddItem(bookItem, 1, True)
EndFunction

Function ReadPrimarySkillBook(String skillName, Int skillTopLimit, Form bookItem, Message msgSkillIncrease)
	_ReadSkillBook(skillName, skillTopLimit, bookItem, msgSkillIncrease, Lernpunkte, _00E_Levelsystem_sNoMoreLearningPoints)
EndFunction

Function ReadCraftingSkillBook(String skillName, Int skillTopLimit, Form bookItem, Message msgSkillIncrease)
	_ReadSkillBook(skillName, skillTopLimit, bookItem, msgSkillIncrease, Handwerkspunkte, _00E_Levelsystem_sNoMoreCraftingPoints)
EndFunction

Function ReadMemorySkillBook(String sClass, Int iTier, Message talentMessage, float fRecoveryTime02, float fRecoveryTime03, Potion PotionItem, Shout TaughtTalent, WordOfPower Word01, WordOfPower Word02, WordOfPower Word03, Perk Perk01, Perk Perk02, Perk Perk03)
	While _MemoryBookReadLocked
		WaitMenuMode(0.1)
	EndWhile
	_MemoryBookReadLocked = True
	
	int iButton = talentMessage.Show()

	if iButton == 0
		
		if iTier == 1
			iRequiredPoints = iPointRequirementTier01
		Elseif iTier == 2
			iRequiredPoints = iPointRequirementTier02
		Else
			iRequiredPoints = iPointRequirementTier03
		EndIf
		
		if RequirementsMetMemoryBook(iTier, Perk01, Perk02, Perk03, sClass)
			TeachTalent(iTier, TaughtTalent, Perk01, Perk02, Perk03, Word01, Word02, Word03, fRecoveryTime02, fRecoveryTime03)
		Else
		
			if bValidSkilllevel
		
				Message messageToShow = _00E_FS_A3_sMageClassName
			
				if sClass == "Rogue"
					messageToShow = _00E_FS_A3_sRogueClassName
				Elseif sClass == "Warrior"
					messageToShow = _00E_FS_A3_sWarriorClassName
				EndIf
				Debug.Notification(_00E_FS_A3_sYouNeedSkillpoints.GetName() + " " + iRequiredPoints + _00E_FS_A3_sIn.GetName() + messageToShow.GetName() + _00E_FS_A3_sToUnlockThisClass.GetName())
			
			EndIf
				
			PlayerREF.AddItem(PotionItem, 1, true)

		EndIf
		_MemoryBookReadLocked = False
		
	Else
	
		PlayerREF.AddItem(PotionItem, 1, true)
		_MemoryBookReadLocked = False
		Return
		
	EndIf
EndFunction

bool Function RequirementsMetMemoryBook(int iTier, Perk Perk01, Perk Perk02, Perk Perk03, string sClass)
	
	Formlist ClassFormList01
	Formlist ClassFormList02
	Formlist ClassFormList03
	
	if (iTier == 3 && !PlayerREF.HasPerk(Perk02)) || (iTier == 2 && !PlayerREF.HasPerk(Perk01))
	
		_00E_FS_A3_NeedToLearnOtherLevels.Show()
		bValidSkilllevel = False
		Return False
		
	Elseif (iTier == 1 && PlayerREF.HasPerk(Perk01)) || (iTier == 2 && PlayerREF.HasPerk(Perk02)) || (iTier == 3 && PlayerREF.HasPerk(Perk03))
	
		_00E_FS_A3_AlreadyKnowThisLevel.Show()
		bValidSkilllevel = False
		Return False
		
	Else
	
		bValidSkilllevel = True

		if sClass == "Rogue"
			ClassFormList01 = EspionagePerks
			ClassFormList02 = TrickeryPerks
			ClassFormList03 = VagabondPerks

		Elseif sClass == "Warrior"
			ClassFormList01 = BastionPerks
			ClassFormList02 = DerwishPerks
			ClassFormList03 = RagePerks

		Elseif sClass == "Mage"
			ClassFormList01 = ElementalismPerks
			ClassFormList02 = LifeAndDeathPerks
			ClassFormList03 = ManipulationPerks
		EndIf
		
		int iTotalPointsInRequiredClass = (_00E_Func_GetPointsInClass.Run(ClassFormList01) + _00E_Func_GetPointsInClass.Run(ClassFormList02) + _00E_Func_GetPointsInClass.Run(ClassFormList03))
		if iTotalPointsInRequiredClass >= iRequiredPoints
			Return True
		Else
			Return False
		EndIf
		
	EndIf

EndFunction

Function TeachTalent(int iLevel, Shout TaughtTalent, Perk Perk01, Perk Perk02, Perk Perk03, WordOfPower Word01, WordOfPower Word02, WordOfPower Word03, float fRecoveryTime02, float fRecoveryTime03)

	if iLevel == 1
	
		PlayerREF.AddPerk(Perk01)
		PlayerREF.AddShout(TaughtTalent)
		Game.UnlockWord(Word01)
		Game.TeachWord(Word01)
		
	Elseif iLevel == 2
	
		PlayerREF.AddPerk(Perk02)
		Game.UnlockWord(Word02)
		Game.TeachWord(Word02)
		SetRecoveryTimeMemoryBook(2, TaughtTalent, fRecoveryTime02, fRecoveryTime03)
	
	Else
		
		PlayerREF.AddPerk(Perk03)
		Game.UnlockWord(Word03)
		Game.TeachWord(Word03)
		SetRecoveryTimeMemoryBook(3, TaughtTalent, fRecoveryTime02, fRecoveryTime03)
	
	EndIf
	
	TalentPoints.Mod(-1)
	
	Steam.UnlockAchievement("END_TALENT_BOOK_01")
	
EndFunction

Function SetRecoveryTimeMemoryBook(int iWord, Shout TaughtTalent, float fRecoveryTime02, float fRecoveryTime03)
{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 iWord == 2
		TaughtTalent.SetNthRecoveryTime(0, fRecoveryTime02)
		TaughtTalent.SetNthRecoveryTime(1, fRecoveryTime02)
	Else
		TaughtTalent.SetNthRecoveryTime(0, fRecoveryTime03)
		TaughtTalent.SetNthRecoveryTime(1, fRecoveryTime03)
		TaughtTalent.SetNthRecoveryTime(2, fRecoveryTime03)
	EndIf

EndFunction

Function ShowSynergyMessage()

	_00E_Levelsystem_sSynergyDiscovered.Show()
	_00E_EPHandler.GiveEP(150)

	If !bUnlockedSynergyAchievement
		Steam.UnlockAchievement("END_SYNERGY_01")
		bUnlockedSynergyAchievement = true
	EndIf

EndFunction

int iPointRequirementTier01 = 5
int iPointRequirementTier02 = 10
int iPointRequirementTier03 = 15

int iRequiredPoints
bool bValidSkilllevel

bool bUnlockedSynergyAchievement

Bool _SkillBookReadLocked = False
Bool _MemoryBookReadLocked = False
 
actor Property PlayerRef  Auto  

Message Property _00E_SkillbookWarning  Auto 
Message Property _00E_Levelsystem_sSkillTooWellDeveloped  Auto  
Message Property _00E_FS_A3_sMageClassName  Auto  
Message Property _00E_FS_A3_sRogueClassName  Auto  
Message Property _00E_FS_A3_sWarriorClassName  Auto  
Message Property _00E_FS_A3_sYouNeedSkillpoints  Auto  
Message Property _00E_FS_A3_sIn  Auto  
Message Property _00E_FS_A3_sToUnlockThisClass  Auto  
Message Property _00E_FS_A3_NeedToLearnOtherLevels  Auto  
Message Property _00E_FS_A3_AlreadyKnowThisLevel  Auto  

FormList Property EspionagePerks  Auto  
FormList Property TrickeryPerks  Auto  
FormList Property VagabondPerks  Auto  
FormList Property BastionPerks  Auto  
FormList Property DerwishPerks  Auto  
FormList Property RagePerks  Auto  
FormList Property ElementalismPerks  Auto  
FormList Property LifeAndDeathPerks  Auto  
FormList Property ManipulationPerks  Auto  

GlobalVariable Property Handwerkspunkte  Auto  
GlobalVariable Property Lernpunkte  Auto  
GlobalVariable Property TalentPoints  Auto  

Message Property _00E_Levelsystem_sNoMoreCraftingPoints  Auto  
Message Property _00E_Levelsystem_sNoMoreLearningPoints  Auto  
Message Property _00E_Levelsystem_sSynergyDiscovered  Auto