Scriptname _00E_PlayerSetUpScript extends ObjectReference  
{Initializes all the necessary Quests, maintains the player, contains various OnPlayerLoadGame() failsafes}

Float Property CURRENT_PATCH_VERSION = 2.08 AutoReadOnly


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

Event OnInit()
	
	; needs the IsInMenuMode() check to prevent stuck messages when FS is not loaded
	While Utility.IsInMenuMode()
		Utility.WaitMenuMode(0.1)
	EndWhile
	
	CheckIfForgottenStoriesIsLoaded()
	
	If self == (PlayerREF as ObjectReference)
		
		If fPatchVersion == 0.00
			fPatchVersion = CURRENT_PATCH_VERSION
		EndIf
		
		GoToState("RealPlayer")
		PlayerREF.SetActorValue("speedMult", 95)
		PlayerREF.SetActorValue("Healrate", 0)
		
		SetAmountAutosaves()
		
		EnableDisableKillmove()
		
		AddPerks()
	
		DisableRegularWaiting()
		StartQuests(QuestsToStart)
		
	EndIf

EndEvent

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

State RealPlayer

	;/ Event OnBeginState()
		Debug.Trace(self + ": OnBeginState RealPlayer")
	EndEvent /;

EndState


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

Function CheckIfForgottenStoriesIsLoaded()
	
	; function that quits back to the main menu when the Enderal - Forgotten Stories.esm is not not being added to the plugins.txt
	; and hence not being loaded in-game. Unknown reason, probably write protection issues	
	
	If Game.GetModByName("Enderal - Forgotten Stories.esm") == 255
		bFSWasNotLoadedBefore = true
		_00E_Game_ForgottenStoriesNotLoaded.Show()
		Game.QuitToMainMenu()
	Else
		If bFSWasNotLoadedBefore == true
			AddPerks()
			If FSQuestsToStartFailsafePatch1560 == None
				FSQuestsToStartFailsafePatch1560 = Game.GetFormFromFile(0x0102F4B0, "Enderal - Forgotten Stories.esm") as FormList
			EndIf
			StartQuests(FSQuestsToStartFailsafePatch1560)
			bFSWasNotLoadedBefore = false
		EndIf
	EndIf

EndFunction

;=====================================================================================
;              							 1.5.6.0                   					 
;=====================================================================================

Function ResetFSNQR05Temple()
	
	; resets the cells which is needed due to the fixes on enemies and ambushes in patch 1.5.6.0
	Quest FS_NQR05 = Game.GetFormFromFile(0x000438D2,"Skyrim.esm") as Quest
	Cell FS_NQR05_Temple_02_New = Game.GetFormFromFile(0x00045101,"Skyrim.esm") as Cell
	If FS_NQR05.GetStage() < 165
		FS_NQR05_Temple_02_New.Reset()
	EndIf

EndFunction

;=====================================================================================
;              							 1.5.7.0                   					 
;=====================================================================================

Function CleanMQ12bGhostFlags()
	
	; cleans old ghost flags on certain NPCs after the siege when patch 1.5.7.0 is applied
	; only needed once, since it won't happen anymore for everyone with a save before the siege
	_00E_MQ12b_Functions MQ12bFunctions = Game.GetFormFromFile(0x0002EBAD,"Skyrim.esm") as _00E_MQ12b_Functions
	MQ12bFunctions.OldRemoveGhostFlag()
	
EndFunction

;=====================================================================================
;              							 1.5.8.0                   					 
;=====================================================================================

Function ResetAuroraOnLoad()
	; resets Aurora if she was stuck during the ritual scene due to the gamehour change while she was in the smoking animation
	_00E_FS_NQ07_Functions FS_NQ07Functions = Game.GetFormFromFile(0x0101CA09,"Enderal - Forgotten Stories.esm") as _00E_FS_NQ07_Functions
	FS_NQ07Functions.ResetAurora()
EndFunction

Function DisableMQ12bPanicTriggerbox()
	; disables MQ12b_SC3_PanicTriggerboxREF when patch 1.5.8.0 is applied
	_00E_MQ12b_Functions MQ12bFunctions = Game.GetFormFromFile(0x0002EBAD,"Skyrim.esm") as _00E_MQ12b_Functions
	If MQ12bFunctions.IsCompleted()
		MQ12bFunctions.DisablePanicTriggerbox()
	EndIf
EndFunction

Function ReAddDarkKeeperAffinity()
	
	; Dark Keeper Affinity Update
	Spell _00E_Affinity_AbDarkKeeper = Game.GetFormFromFile(0x00143340,"Skyrim.esm") as Spell
	If PlayerREF.HasSpell(_00E_Affinity_AbDarkKeeper)
		PlayerREF.RemoveSpell(_00E_Affinity_AbDarkKeeper)
		Utility.Wait(1)
		PlayerREF.AddSpell(_00E_Affinity_AbDarkKeeper, false)
	EndIf

EndFunction

Function ReAddFuryDrivenPerk()

	; Fury Driven ability update
	Spell abFuryDriven = Game.GetFormFromFile(0x000855CE, "Skyrim.esm") as Spell
	If PlayerREF.HasSpell(abFuryDriven)
		PlayerREF.RemoveSpell(abFuryDriven)
		Utility.Wait(1)
		PlayerREF.AddSpell(abFuryDriven, False)
	EndIf

EndFunction

Function UpdateLivingTempleTrigger()

	; Living Temple's ambush trigger update
	ObjectReference triggerRef = Game.GetFormFromFile(0x000C6766, "Skyrim.esm") as ObjectReference
	(triggerRef as defaultActivateSelf).playerAndAlliesOnly = True

EndFunction

Function UpdateMQ11a()

	; MQ11a update
	ObjectReference portcullisRef = Game.GetFormFromFile(0x000E1D1F, "Skyrim.esm") as ObjectReference
	Quest MQ11a = Game.GetFormFromFile(0x0002EBA9, "Skyrim.esm") as Quest
	If (MQ11a.GetStage() <= 125) && (portcullisRef.Is3DLoaded() == False)
		(portcullisRef as _00E_DUNGate01Script).isOpen = False
	EndIf

	If MQ11a.IsRunning() && (MQ11a.IsCompleted() == False)
		(MQ11a as _00E_MQ11a_Functions).UpdateCaliaAliasScriptState()
	EndIf
	
	ObjectReference NQ34_EnableMarkerREF = Game.GetFormFromFile(0x000480E8, "Skyrim.esm") as ObjectReference
	If MQ11a.GetStage() >= 45
		NQ34_EnableMarkerREF.Enable()
	EndIf
	
	Actor NQ34_WildMageREF = Game.GetFormFromFile(0x00141F00, "Skyrim.esm") as Actor
	Quest NQ34 = Game.GetFormFromFile(0x00020A12, "Skyrim.esm") as Quest
	If NQ34.GetStage() == 15 || NQ34.GetStage() == 20
		NQ34_WildMageREF.Enable()
	EndIf
	
	If NQ34_WildMageREF.IsDead() && NQ34.GetStage() == 15
		NQ34.SetStage(20)
	EndIf

EndFunction

Function AddPerks()
	
	; adds the new Balancing perks for alchemy potions to the player, this is needed because of the changes with the Lycantroph
	PlayerREF.AddPerk(_00E_FS_Alchemy_BalancingPerk)
	PlayerREF.AddPerk(_00E_FS_Alchemy_BalancingPerk_RestorePotions)
	
EndFunction

Function DisableRegularWaiting()
{Disables regular waiting by adding the no-wait ability}

	PlayerREF.AddSpell(_00E_AbBlockWaiting, False)

EndFunction

Function StartQuests(Formlist QuestList)
	
	; starts all quests that are in the formlist
	Int iIndex = QuestList.GetSize() 
	While iIndex
		iIndex -= 1
		Quest  kQuest = QuestList.GetAt(iIndex) as Quest
		kQuest.Start()
	endwhile
	
EndFunction

;=====================================================================================
;              							 1.6.0.0                   					 
;=====================================================================================

Function UpdatePhasmalistContainer()

	ObjectReference Phasmalist_ApparationInventory_Bandit_Tier4 = Game.GetFormFromFile(0x0101E99B, "Enderal - Forgotten Stories.esm") as ObjectReference
	ObjectReference Phasmalist_ApparationInventory_Archer_Tier5 = Game.GetFormFromFile(0x0101E9CA, "Enderal - Forgotten Stories.esm") as ObjectReference
	Armor _00E_Phasmalist_Trinket_Bandit_Tier4 = Game.GetFormFromFile(0x0101E9A6, "Enderal - Forgotten Stories.esm") as Armor
	Armor _00E_Phasmalist_Trinket_Archer_Tier5 = Game.GetFormFromFile(0x0101E9D4, "Enderal - Forgotten Stories.esm") as Armor
	
	_00E_Phasmalist_ApparationSC _36E_Phasmalist_Apparation_Hybrid_Tier4 = Game.GetFormFromFile(0x01025309, "Enderal - Forgotten Stories.esm") as _00E_Phasmalist_ApparationSC
	_36E_Phasmalist_Apparation_Hybrid_Tier4.inventoryContainer = Phasmalist_ApparationInventory_Bandit_Tier4
	
	If (PlayerREF.GetItemCount(_00E_Phasmalist_Trinket_Bandit_Tier4) > 0 && PlayerREF.GetItemCount(_00E_Phasmalist_Trinket_Archer_Tier5) == 0)
		Phasmalist_ApparationInventory_Archer_Tier5.RemoveAllItems(Phasmalist_ApparationInventory_Bandit_Tier4, false, true)
	EndIf

EndFunction

Function UpdatePassengerBookAtShipwreck()
	
	_00E_FS_NQ02_Functions FS_NQ02_Functions = (Game.GetFormFromFile(0x000438AC, "Skyrim.esm") as Quest) as _00E_FS_NQ02_Functions
	FS_NQ02_Functions.UpdatePassengerBook()
	
EndFunction

Function ResetMQ02Riddle()
	
	GlobalVariable _00E_MQ02Riddle = Game.GetFormFromFile(0x00074B07, "Skyrim.esm") as GlobalVariable
	
	If _00E_MQ02Riddle.GetValueInt() > 0
		_00E_MQ02_Functions MQ02Functions = Game.GetFormFromFile(0x0002EBA1, "Skyrim.esm") as _00E_MQ02_Functions
		MQ02Functions.ResetRiddle()
	EndIf
	
EndFunction


;=====================================================================================
;              							 1.6.1.0                   					 
;=====================================================================================


Function UpdateMQ02()
	_00E_MQ02_Functions MQ02Functions = Game.GetFormFromFile(0x0002EBA1, "Skyrim.esm") as _00E_MQ02_Functions
	Int mq02stage = MQ02Functions.GetStage()
	If mq02stage >= 138 && mq02stage < 170
		MQ02Functions.MQ02_TripToArkEnableMarker.EnableNoWait()
	EndIf
EndFunction


;=====================================================================================
;              							 1.6.2.0                   					 
;=====================================================================================


Function UpdateItemHandler()

	Cell ClassMenue = Game.GetFormFromFile(0x000A7FBC, "Skyrim.esm") as Cell
	
	If PlayerREF.GetParentCell() == ClassMenue
		Quest ClassMenuItemHandler = Game.GetFormFromFile(0x00048442, "Skyrim.esm") as Quest
		ReferenceAlias Player = ClassMenuItemHandler.GetAliasByName("Player") as ReferenceAlias
		Player.ForceRefTo(PlayerREF)
	EndIf

EndFunction


;=====================================================================================
;              							 2.0.6
;=====================================================================================

Function UpdateMQ12b_206()
	_00E_MQ12b_Functions MQ12bFunctions = Game.GetFormFromFile(0x0002EBAD, "Skyrim.esm") as _00E_MQ12b_Functions
	Quest MQ15 = Game.GetFormFromFile(0x0002EBB0, "Skyrim.esm") as Quest
	If MQ12bFunctions.GetStage() >= 135 && MQ15.GetStage() < 5
		MQ12bFunctions.OldReenableDisabledNPC()
	EndIf
EndFunction


;=====================================================================================
;              							 2.0.8
;=====================================================================================

Function Update_208()
	; Obsolete abilities cleanup
	_00E_FS_NQ07_Functions FS_NQ07_Quest = Game.GetFormFromFile(0x0001CA09, "Enderal - Forgotten Stories.esm") as _00E_FS_NQ07_Functions
	Int iFS_NQ07Stage = FS_NQ07_Quest.GetStage()
	If iFS_NQ07Stage >= 5 && iFS_NQ07Stage < 410
		FS_NQ07_Quest.UpdateAarosCarryTraySpell()
	Else
		RemoveObsoleteSpell(0x0009A893, "Skyrim.esm", 0x000F649A, "Skyrim.esm")
	EndIf

	RemoveObsoleteSpell(0x0010D339, "Skyrim.esm", 0x000F649A, "Skyrim.esm")
	RemoveObsoleteSpell(0x0006FFAF, "Skyrim.esm", 0x000F6498, "Skyrim.esm")
	RemoveObsoleteSpell(0x000F64A0, "Skyrim.esm", 0x000F6498, "Skyrim.esm")

	; Refill _00E_AnimTest_SC package arrays where needed
	FillAnimTestPackages(0x00137889, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	FillAnimTestPackages(0x0013788A, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	FillAnimTestPackages(0x0013789F, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	FillAnimTestPackages(0x001378A2, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	FillAnimTestPackages(0x001378AA, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	FillAnimTestPackages(0x001378AB, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	FillAnimTestPackages(0x00147E12, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	FillAnimTestPackages(0x00147E14, "Skyrim.esm", 0x00070657, "Skyrim.esm")
	
	FillAnimTestPackages(0x0010D336, "Skyrim.esm", 0x00070645, "Skyrim.esm")
	FillAnimTestPackages(0x0010D33B, "Skyrim.esm", 0x00070645, "Skyrim.esm")
	FillAnimTestPackages(0x00147DEA, "Skyrim.esm", 0x00070645, "Skyrim.esm")
	FillAnimTestPackages(0x00147E37, "Skyrim.esm", 0x00070645, "Skyrim.esm")

	; "Gypsy minstrels" update
	Keyword performSpotKwd = Game.GetFormFromFile(0x000FB905, "Skyrim.esm") as Keyword
	(Game.GetFormFromFile(0x00044EBC, "Skyrim.esm") as _00E_BardPlayInstrumentScript).LinkedPlayMarkerKeyword = performSpotKwd

	(Game.GetFormFromFile(0x00046D79, "Skyrim.esm") as _00E_GypsyMinstrelsControlScript).UpdateWaypoint()
EndFunction

Function RemoveObsoleteSpell(Int idNPC, String esmNPC, Int idSpell, String esmSpell)
	Spell obsoleteSpell = Game.GetFormFromFile(idSpell, esmSpell) as Spell
	(Game.GetFormFromFile(idNPC, esmNPC) as Actor).RemoveSpell(obsoleteSpell)
EndFunction

Function FillAnimTestPackages(Int idNPC, String esmNPC, Int idPackage, String esmPackage)
	Package newPackage = Game.GetFormFromFile(idPackage, esmPackage) as Package
	(Game.GetFormFromFile(idNPC, esmNPC) as _00E_AnimTest_SC).UpdateFillPackages(newPackage)
EndFunction


;=====================================================================================
;              							 ALL UPDATES                   					 
;=====================================================================================

Function EnableDisableKillmove()
	
	; it seems like that this ini setting does not do stuff by itself (or it is bugged)
	; therefore we need to read it from the file and change the global with which the killmoves are conditioned
	If (Utility.GetINIBool("bVATSDisable:VATS") == 1)
		KillMove.SetValueInt(0)
	ElseIf (Utility.GetINIBool("bVATSDisable:VATS") == 0)
		KillMove.SetValueInt(1)
	EndIf
	
EndFunction

Function SetAmountAutosaves()

	; increases amount of autosaves created from 3 to 5 without editing the ini files
	; new default ini files already have set the int to 5
	If Utility.GetINIInt("iAutoSaveCount:SaveGame") == 3
		Utility.SetINIInt("iAutoSaveCount:SaveGame", 5)
	EndIf

EndFunction

Function SetAutoSaveInterval()
	
	; updates the autosave interval
	_00E_AutoSaveSystem_Functions AutoSaveSystem_Functions = Game.GetFormFromFile(0x00048141, "Skyrim.esm") as _00E_AutoSaveSystem_Functions
	AutoSaveSystem_Functions.UpdateAutoSaveInterval()
	
EndFunction

Function FailsafeMQ05PrologueAliases()

	; failsafe for issue 1536, fills empty aliases
	If MQ05PrologueFunctions == None
		MQ05PrologueFunctions = Game.GetFormFromFile(0x00033A5B, "Skyrim.esm") as _00E_MQ05Prologue_Functions
	EndIf
	MQ05PrologueFunctions.FillEmptyAliasesFailsafe()

EndFunction

Function RefreshMountNamesOnLoad()

	_00E_NQ06_Functions NQ06Functions = Game.GetFormFromFile(0x000725BA,"Skyrim.esm") as _00E_NQ06_Functions
	NQ06Functions.RefreshNamesOnSaveLoad()

EndFunction

Function _ResetArmorWeightPerk(Perk weightPerk)
	If PlayerREF.HasPerk(weightPerk)
		PlayerREF.RemovePerk(weightPerk)
		PlayerREF.AddPerk(weightPerk)
	EndIf
EndFunction

Function ResetArmorWeightPerks()

	; Perks modifying armor weights need some push on each game load to work.
	; The push is needed only if the player is over-encumbered on game load. 
	; If the player is not over-encumbered, the push is not needed because checking the inventory, picking up items, sheathing/drawing a weapon will reset the perks anyway.
	If PlayerREF.IsOverEncumbered()
		_ResetArmorWeightPerk(_00E_Class_Keeper_P05_C_Conditioning)
		_ResetArmorWeightPerk(_00E_Class_Vagrant_P05_B_Lightweight)
	EndIf

EndFunction

Function FixMoreAffinitiesMod()
{Fixes "More Affinites" mod missing properties in the Levelsystem scripts}

	_00E_QuestFunctions QuestFunctionsScript = (Game.GetFormFromFile(0x00010AA2, "Skyrim.esm") as Quest) as _00E_QuestFunctions
	If QuestFunctionsScript._00E_RemoveAllItems_TrashContainer == NONE

		Debug.Messagebox("Found broken scripts caused by mod >>More Affinities<<. Press OK to fix scripts.")
		_00E_ConfigMenu ConfigMenuScript = (Game.GetFormFromFile(0x00010AA2, "Skyrim.esm") as Quest) as _00E_ConfigMenu
		ConfigMenuScript.controlQuest = (Game.GetFormFromFile(0x0101EC71, "Enderal - Forgotten Stories.esm") as Quest) as _FS_Phasmalist_ControlQuest
		ConfigMenuScript._00E_ConfigMenu_DisableCraftingTutorials = Game.GetFormFromFile(0x00046D8E, "Skyrim.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_DisableMenuTutorials = Game.GetFormFromFile(0x00046D8A, "Skyrim.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_DisableOtherTutorials = Game.GetFormFromFile(0x00046D8F, "Skyrim.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_DisableQuestTutorials = Game.GetFormFromFile(0x00046D8C, "Skyrim.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_DisableReadAloudBooks = Game.GetFormFromFile(0x0102E982, "Enderal - Forgotten Stories.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_DisableSkillTutorials = Game.GetFormFromFile(0x00046D8D, "Skyrim.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_GameplayOptions = Game.GetFormFromFile(0x0102F1BC, "Enderal - Forgotten Stories.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_Meditate = Game.GetFormFromFile(0x00046D98, "Skyrim.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_OtherOptions = Game.GetFormFromFile(0x00046D89, "Skyrim.esm") as Message
		ConfigMenuScript._00E_ConfigMenu_VideoSkip = Game.GetFormFromFile(0x0102E983, "Enderal - Forgotten Stories.esm") as Message
		ConfigMenuScript._00E_DisableCraftingTutorials = Game.GetFormFromFile(0x00046D90, "Skyrim.esm") as GlobalVariable
		ConfigMenuScript._00E_DisableMenuTutorials = Game.GetFormFromFile(0x00046D8B, "Skyrim.esm") as GlobalVariable
		ConfigMenuScript._00E_DisableOtherTutorials = Game.GetFormFromFile(0x00046D93, "Skyrim.esm") as GlobalVariable
		ConfigMenuScript._00E_DisableQuestTutorials = Game.GetFormFromFile(0x00046D92, "Skyrim.esm") as GlobalVariable
		ConfigMenuScript._00E_DisableReadAloudBooks = Game.GetFormFromFile(0x0102E984, "Enderal - Forgotten Stories.esm") as GlobalVariable
		ConfigMenuScript._00E_DisableSkillTutorials = Game.GetFormFromFile(0x00046D91, "Skyrim.esm") as GlobalVariable
		ConfigMenuScript._00E_FS_ConfigMenu_Mount_Teleport = Game.GetFormFromFile(0x0102F1B9, "Enderal - Forgotten Stories.esm") as Message
		ConfigMenuScript._00E_FS_ConfigMenu_Phasmalist_Teleport = Game.GetFormFromFile(0x0101F2B6, "Enderal - Forgotten Stories.esm") as Message
		ConfigMenuScript._00E_FS_IsForgottenStoriesActivated = Game.GetFormFromFile(0x0004320E, "Skyrim.esm") as GlobalVariable
		ConfigMenuScript._00E_HorseFlute_New = ((Game.GetFormFromFile(0x000725BA, "Skyrim.esm") as Quest).GetAliasByName("HorseFlute")) as _00E_Horseflute_SC
		ConfigMenuScript._00E_VideoSkip = Game.GetFormFromFile(0x0102E985, "Enderal - Forgotten Stories.esm") as GlobalVariable
	
	
		QuestFunctionsScript.BastionPerks = Game.GetFormFromFile(0x0006686B, "Skyrim.esm") as Formlist
		QuestFunctionsScript.DerwishPerks = Game.GetFormFromFile(0x0006686D, "Skyrim.esm") as Formlist
		QuestFunctionsScript.ElementalismPerks = Game.GetFormFromFile(0x00066875, "Skyrim.esm") as Formlist
		QuestFunctionsScript.EspionagePerks = Game.GetFormFromFile(0x00066889, "Skyrim.esm") as Formlist
		QuestFunctionsScript.GameHour = Game.GetFormFromFile(0x00000038, "Skyrim.esm") as GlobalVariable
		QuestFunctionsScript.Gold001 = Game.GetFormFromFile(0x0000000f, "Skyrim.esm") as MiscObject
		QuestFunctionsScript.LifeAndDeathPerks = Game.GetFormFromFile(0x00066873, "Skyrim.esm") as Formlist
		QuestFunctionsScript.MAGIllusionCharm = Game.GetFormFromFile(0x00075805, "Skyrim.esm") as Sound
		QuestFunctionsScript.ManipulationPerks = Game.GetFormFromFile(0x0006686A, "Skyrim.esm") as Formlist
		QuestFunctionsScript.PlayerSkillMenu = ((Game.GetFormFromFile(0x00010AA2, "Skyrim.esm") as Quest).GetAliasByName("Player") as Alias)  as _00E_Game_SkillmenuSC
		QuestFunctionsScript.RagePerks = Game.GetFormFromFile(0x00066887, "Skyrim.esm") as Formlist
		QuestFunctionsScript.SympathyEsme = Game.GetFormFromFile(0x0102A498, "Enderal - Forgotten Stories.esm") as GlobalVariable
		QuestFunctionsScript.TrickeryPerks = Game.GetFormFromFile(0x0006687F, "Skyrim.esm") as Formlist
		QuestFunctionsScript.VagabondPerks = Game.GetFormFromFile(0x00066877, "Skyrim.esm") as Formlist
		QuestFunctionsScript._00E_AchievementsEnabled = Game.GetFormFromFile(0x00046F83, "Skyrim.esm") as GlobalVariable
		QuestFunctionsScript._00E_AllAmmos = Game.GetFormFromFile(0x000456C0, "Skyrim.esm") as Formlist
		QuestFunctionsScript._00E_ArmorSetScript_sSetBonusRemoved = Game.GetFormFromFile(0x00036F96, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_DisableQuestTutorials = Game.GetFormFromFile(0x00046D92, "Skyrim.esm") as GlobalVariable
		QuestFunctionsScript._00E_DisableSkillTutorials = Game.GetFormFromFile(0x00046D91, "Skyrim.esm") as GlobalVariable
		QuestFunctionsScript._00E_FS_A3_AlreadyKnowThisLevel = Game.GetFormFromFile(0x0102F533, "Enderal - Forgotten Stories.esm") as Message
		QuestFunctionsScript._00E_FS_A3_NeedToLearnOtherLevels = Game.GetFormFromFile(0x0101EC7D, "Enderal - Forgotten Stories.esm") as Message
		QuestFunctionsScript._00E_FS_A3_sIn = Game.GetFormFromFile(0x00043BE0, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_FS_A3_sMageClassName = Game.GetFormFromFile(0x00043BE4, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_FS_A3_sRogueClassName = Game.GetFormFromFile(0x00043BE3, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_FS_A3_sToUnlockThisClass = Game.GetFormFromFile(0x00043BE1, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_FS_A3_sWarriorClassName = Game.GetFormFromFile(0x00043BE2, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_FS_A3_sYouNeedSkillpoints = Game.GetFormFromFile(0x00043BDF, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_FS_NQ02_Esme = Game.GetFormFromFile(0x0101F0D0, "Enderal - Forgotten Stories.esm") as ActorBase
		QuestFunctionsScript._00E_FS_NQR05_SlowMotionSP = Game.GetFormFromFile(0x0102E2BD, "Enderal - Forgotten Stories.esm") as Spell
		QuestFunctionsScript._00E_FS_NQR05_SlowMotion_IntroM = Game.GetFormFromFile(0x0102E2BF, "Enderal - Forgotten Stories.esm") as Sound
		QuestFunctionsScript._00E_FS_NQR05_SlowMotion_OutroM = Game.GetFormFromFile(0x0102E2C1, "Enderal - Forgotten Stories.esm") as Sound
		QuestFunctionsScript._00E_Levelsystem_sAbilityStanceQyrai = Game.GetFormFromFile(0x00046D9B, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_Levelsystem_sAbilityStanceSkaragg = Game.GetFormFromFile(0x00046D9C, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_PleaseRemoveMoney = Game.GetFormFromFile(0x000480DA, "Skyrim.esm") as Message
		QuestFunctionsScript._00E_RemoveAllItems_TrashContainer = Game.GetFormFromFile(0x000469E7, "Skyrim.esm") as ObjectReference
		QuestFunctionsScript._00E_SkillbookWarning = Game.GetFormFromFile(0x00031ACF, "Skyrim.esm") as Message
	
	
		_00E_Game_SkillmenuSC SkillMenuScript = ((Game.GetFormFromFile(0x00010AA2, "Skyrim.esm") as Quest).GetAliasByName("Player") as Alias) as _00E_Game_SkillmenuSC
		SkillMenuScript._00E_FS_Game_Affinity_Spectralist = Game.GetFormFromFile(0x01029A35, "Skyrim.esm") as Message
	
	
		_00E_PlayerHousing_CellNameSC CellNameScript = ((Game.GetFormFromFile(0x00010AA2, "Skyrim.esm") as Quest).GetAliasByName("Player") as Alias) as _00E_PlayerHousing_CellNameSC
		CellNameScript.FS_NQR03 = Game.GetFormFromFile(0x0014CEA2, "Skyrim.esm") as Quest
		CellNameScript.FS_NQR03_RhalataTemple = Game.GetFormFromFile(0x0010D1C2, "Skyrim.esm") as Cell
		CellNameScript._00E_sPlayerHouseMarketQuarterName = Game.GetFormFromFile(0x00047CA5, "Skyrim.esm") as Message
		CellNameScript._00E_sPlayerHouseNobleQuarterName = Game.GetFormFromFile(0x00047CA6, "Skyrim.esm") as Message
		CellNameScript._00E_sRhalataTempleName = Game.GetFormFromFile(0x00047C29, "Skyrim.esm") as Message
	
	
		_00E_WeatherControlScriptSC WeatherControlScript = ((Game.GetFormFromFile(0x00010AA2, "Skyrim.esm") as Quest).GetAliasByName("Player") as Alias) as _00E_WeatherControlScriptSC
		WeatherControlScript.CapitalCityCastleWorld = Game.GetFormFromFile(0x00070729, "Skyrim.esm") as WorldSpace
		WeatherControlScript.CapitalCityLocation = Game.GetFormFromFile(0x000A1A12, "Skyrim.esm") as Location
		WeatherControlScript.MQ09 = Game.GetFormFromFile(0x0007FA91, "Skyrim.esm") as Quest
		WeatherControlScript.MQ12b = Game.GetFormFromFile(0x0002EBAD, "Skyrim.esm") as Quest
		WeatherControlScript.MQ17 = Game.GetFormFromFile(0x0007FA93, "Skyrim.esm") as Quest
		WeatherControlScript.SkyrimOvercastRain = Game.GetFormFromFile(0x000C821F, "Skyrim.esm") as Weather
		WeatherControlScript.Vyn = Game.GetFormFromFile(0x00001D3C, "Skyrim.esm") as WorldSpace
		WeatherControlScript._00E_EnderalOvercast = Game.GetFormFromFile(0x00072785, "Skyrim.esm") as Weather
		WeatherControlScript._00E_MQ11c_SilberhainDeadWeather = Game.GetFormFromFile(0x00096807, "Skyrim.esm") as GlobalVariable
		WeatherControlScript._00E_SuntempleLocations = Game.GetFormFromFile(0x0003BB5B, "Skyrim.esm") as FormList
	
		Utility.Wait(1)
		Debug.Messagebox("Scripts fixed. Make sure to save the game. Restart the client and reload the save that you just created.\n If you have set items equipped, unequip them all and re-equip them for potential set bonuses to apply.")
	EndIf

EndFunction

;=====================================================================================
;              							 MAINTENANCE                   					 
;=====================================================================================

Function Maintenance()
	
	If fPatchVersion <= 1.25 && !bdoOnce
		int iButton01 = _00E_Game_OldSaves_01.Show()
		If iButton01 == 0
			Game.QuitToMainMenu()
		ElseIf iButton01 == 1
			int iButton02 = _00E_Game_OldSaves_02.Show()
			If iButton02 == 0
				Game.QuitToMainMenu()
			ElseIf iButton02 == 1
				bdoOnce = true
			EndIf
		EndIf
	ElseIf fPatchVersion >= 1.5
		
		; only do this once when update 1.5.6.0 is applied
		If fPatchVersion < 1.56
			ResetFSNQR05Temple()
		EndIf
		; only do this once when update 1.5.7.0 is applied
		If fPatchVersion < 1.57
			CleanMQ12bGhostFlags()
		EndIf
		; only do this once when update 1.5.8.0 is applied
		If fPatchVersion < 1.58
			ResetAuroraOnLoad()
			DisableMQ12bPanicTriggerbox()
			ReAddDarkKeeperAffinity()
			ReAddFuryDrivenPerk()
			UpdateLivingTempleTrigger()
			UpdateMQ11a()
		EndIf
		; only do this once when update 1.6.0.0 is applied
		If fPatchVersion < 1.60
			UpdatePhasmalistContainer()
			UpdatePassengerBookAtShipwreck()
			ResetMQ02Riddle()
		EndIf
		; only do this once when update 1.6.1.0 is applied
		If fPatchVersion < 1.61
			UpdateMQ02()
		EndIf
		; only do this once when update 1.6.2.0 is applied
		If fPatchVersion < 1.62
			UpdateItemHandler()
		EndIf
		; only do this once when update 2.0.6 (SE) is applied
		If fPatchVersion < 2.06
			UpdateMQ12b_206()
		EndIf
		; only do this once when update 2.0.8 (SE) is applied
		If fPatchVersion < 2.08
			Update_208()
		EndIf
		fPatchVersion = CURRENT_PATCH_VERSION
	EndIf
	
	; changes to the actor value healrate will not persist in consecutive saves
	; every time a save gets loaded the healrate needs to be set to 0, otherwise it will go back again to default at 0.7
	; this is called before active magic effects of potions or spells are applied to the player which could also alter the value
	PlayerREF.SetActorValue("Healrate", 0)
	
	EnableDisableKillmove()
	
	ResetArmorWeightPerks()

	SetAmountAutosaves()
	
	SetAutoSaveInterval()
	
	FailsafeMQ05PrologueAliases()
	
	RefreshMountNamesOnLoad()
	
	; fixes properties that were accidentally set to NONE by the "More Affinities" mod
	If Game.GetModByName("Enderal_FS_More_Affinities.esp") != 255
		FixMoreAffinitiesMod()
	EndIf

EndFunction

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

float fPatchVersion
bool bdoOnce = false
bool bFSWasNotLoadedBefore = false

_00E_MQ05Prologue_Functions Property MQ05PrologueFunctions Auto

Perk Property _00E_FS_Alchemy_BalancingPerk Auto
Perk Property _00E_FS_Alchemy_BalancingPerk_RestorePotions Auto

Spell Property _00E_AbBlockWaiting Auto
Actor Property PlayerREF Auto

FormList Property QuestsToStart Auto
FormList Property FSQuestsToStartFailsafePatch1560 Auto

Message Property _00E_Game_OldSaves_01 Auto
Message Property _00E_Game_OldSaves_02 Auto
Message Property _00E_Game_ForgottenStoriesNotLoaded Auto

GlobalVariable Property KillMove Auto

Perk Property _00E_Class_Keeper_P05_C_Conditioning Auto
Perk Property _00E_Class_Vagrant_P05_B_Lightweight Auto