Scriptname _00E_MQ02_Functions extends Quest Conditional

Import Utility
Import Game
Import _00E_QuestFunctions

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

Function MovePlayerAndJesparToStart()

	ShowFirstPersonGeometry(true)
	Timescale.SetValue(10)
	PlayerREF.MoveTo(MQ02_D0_PlayersWakeUpmarker02)
	_00E_MC_JesparREF.Disable()
	FadeToBlackHoldImod.Apply()
	ForceFirstPerson()
	DisablePlayerControls(true, true, true, true, true, true, true)
	Levelsystem.SkipTimeToHour(5.5)
	_00E_MC_JesparREF.Enable()
	_00E_MC_JesparREF.MoveTo(MQ02_D0_JesparSpawnMarker)
	Wait(0.3)
	Self.SetStage(15)
	
EndFunction

Function WakeUpPlayer()

	FadeOutGame(false, true, 0.4, 1)
	PlayerRef.PlayIdle(Idle_1stPersonWoozyGetUpFromBed)
	FadeToBlackHoldImod.Remove()
	_00E_MQ02_WakeUp.Apply()
	Self.SetStage(20)
	RequestAutoSave()
	EnablePlayerControls()
	_00E_SilenceLongTransitionHighPriority.Remove()
	_00E_Music_Special_Character_Jespar.Add()

EndFunction

Function StartSC01()

	MQ02_SC1_JesparDialogeInterlude.ForceStart()

EndFunction

Function FadeOutAndBack()

	DisablePlayerControls()
	_00E_MC_JesparREF.EnableAI(false)
	FadeToBlackAndBackIMOD.Apply()
	SkyrimClear.SetActive()
	Wait(3.3)
	_00E_MC_JesparREF.Disable()
	Levelsystem.SkipTimeToHour(6.5)
	Wait(0.5)
	; Jespar may snap to player's position after EnableAI, so we ensure he is in the correct position.
	; Plus, he acts weird and wants to go somewhere without this.
	_00E_MC_JesparREF.EnableAI(true)
	_00E_MC_JesparREF.Enable()
	_00E_MC_JesparREF.MoveTo(MQ02_D0_JesparSpawnMarker)
	EnablePlayerControls()
	SetStage(30)
	ShowFirstPersonGeometry(true)
	
EndFunction

Function DialogueInterlude()

	ShowFirstPersonGeometry(true)
	SetPlayerAIDriven(false)

EndFunction

Function ShowQuestDifficultyTutorial()

	MQ02_CampBanditREF.Enable()
	If _00E_DisableQuestTutorials.GetValueInt() == 0
		_00E_Tutorial_QuestDifficulty.Show()
	EndIf

EndFunction

Function MoveJesparToHouseAndUnlockDoor()

	MQ02_D5_YeroFarmTrapdoor.Lock(false, true)
	_00E_MC_JesparREF.MoveTo(MQ02_D5_JesparYeroMarker01)
	Self.SetObjectiveCompleted(10)
	Self.SetObjectiveDisplayed(15)
	
EndFunction

Function CheckIfObjectivesAreCompleted()

	if (Self.IsObjectiveCompleted(15)) && (Self.IsObjectiveCompleted(25))
		Self.SetStage(55)
	Else
		Return
	EndIf

EndFunction

Function StartSC06()
	
	MQ02_D4_AlfridBench.BlockActivation(false)
	MQ02_SC6_AlfriedTravelToBench.ForceStart()

EndFunction

Function ReadBook()
	
	while bReadingBooks == true
		Wait(0.1)
	endwhile
	bReadingBooks = True
	iReadCounter += 1
	if iReadCounter == 3
		UpdateObjective()
	Endif
	bReadingBooks = false
	
EndFunction

Function CountBook()
	
	while bCountingBooks == true
		Wait(0.1)
	endwhile
	bCountingBooks = True
	iBookCounter += 1
	if iBookCounter == 3
		UpdateObjective()
	Endif
	bCountingBooks = false
	
EndFunction

Function UpdateObjective()

	SetObjectiveCompleted(20)
	SetObjectiveDisplayed(25)
	MQ02_D5_YeroFarmTrapdoor.Lock(false, true)
	_00E_MC_JesparREF.MoveTo(MQ02_D5_JesparYeroMarker01)
	
EndFunction

Function StartPuzzleScene()

    ; Failsave for http://sureai.net:9898/browse/ERB-1192 :
    if MQ02_SC1_JesparDialogeInterlude.IsPlaying()
        MQ02_SC1_JesparDialogeInterlude.Stop()
    endif
	MQ02_SC2_PuzzleScene.Start()
	
EndFunction

Function EnableYeroBook()

	_00E_MQ02_SC3_YeroBook.Enable()
	
EndFunction

Function PlayerDrinkWine()
	
	;Debug.SendAnimationEvent(PlayerREF, "IdleForceDefaultState")
	Game.DisablePlayerControls()
	ForceThirdPerson()
	Debug.SendAnimationEvent(PlayerREF, "IdleDrinkPotion")
	PlayerREF.RestoreActorValue("Health", PlayerREF.GetAV("Health"))
	_00E_NPCUncorkPotionAndDrinkM.Play(PlayerREF)

EndFunction

Function TeleportJesparAway()

	PlayerREF.AddItem(_01E_TeleportrolleFlusshaim, 1, false)
	Levelsystem.TeleportNPC(_00E_MC_JesparREF, MQ02_D5_JesparTeleportMarker)
	Self.SetStage(135)
	SetFlusshaimTavernBarStoolsOwner(PlayerREF.GetActorBase())
	
EndFunction

Function YeroBookFailsave()

	If (_00E_MQ02_SC3_YeroBook != None) && (_00E_MQ02_SC3_YeroBook.GetParentCell() == PlayerREF.GetParentCell())

		_00E_MQ02_SC3_YeroBook.SetPosition(3569.7229, -5481.8379, -429.7459)
		
	EndIf

EndFunction


Function GivePlayerSkillbook()

	If iPlayerClass == 3
	
		PlayerREF.AddItem(_00E_LehrbuchBlockTo25)
		PlayerREF.AddItem(_00E_LehrbuchHeavyArmorTo25)
		PlayerREF.AddItem(_00E_LehrbuchOnehandedTo25)
		PlayerREF.AddItem(_00E_LehrbuchTwohandedTo25)
		PlayerREF.AddItem(_00E_HandwerksbuchSmithingTo25)
		
	ElseIf iPlayerClass == 2
	
		PlayerREF.AddItem(_00E_LehrbuchRestorationTo25)
		PlayerREF.AddItem(_00E_LehrbuchIllusionTo25)
		PlayerREF.AddItem(_00E_LehrbuchDestructionTo25)
		PlayerREF.AddItem(_00E_LehrbuchAlterationTo25)
		PlayerREF.AddItem(_00E_LehrbuchConjurationTo25)
	
	ElseIf iPlayerClass == 1
	
		PlayerREF.AddItem(_00E_LehrbuchSneakTo25)
		PlayerREF.AddItem(_00E_LehrbuchOnehandedTo25)
		PlayerREF.AddItem(_00E_LehrbuchMarksmanTo25)
		PlayerREF.AddItem(_00E_LehrbuchLightArmorTo25)
		PlayerREF.AddItem(_00E_HandwerksbuchLockpickingTo25)
	
	EndIf
	If _00E_DisableQuestTutorials.GetValueInt() == 0
		_00E_Tutorial_Skillbooks.Show()
	EndIf

EndFunction

Function WaitForNightfall()

	Wait(0.2)
	DisablePlayerControls()
	FadeToBlackAndBackIMOD02.Apply()
	Wait(3)
	Levelsystem.SkipTimeToHour(3)
	SetStage(140)
	
EndFunction

Function StartJourneyScene()
	
	EnablePlayerControls()
	SetNPCAsCompanion(_00E_MC_JesparREF, True, 200, 150, 100)
	MQ02_TripToArkEnableMarker.EnableNoWait()
	Weather.ReleaseOverride()
	If _00E_DisableQuestTutorials.GetValueInt() == 0
		_00E_MQ02_TutorialCompanionFollow.Show()
	EndIf
	CompanionIsTalking.SetValueInt(0)
	MQ02_SC3_FollowJesparToArk.ForceStart()
	_00E_MC_JesparREF.EvaluatePackage()
	
EndFunction

Function StartPanoramaScene()

	CompanionIsTalking.SetValueInt(0)
	MQ02_SC4_JesparPanoramaScene.ForceStart()

	SetFlusshaimTavernBarStoolsOwner(None) ; Don't need the stools to be reserved anymore
	
EndFunction

Function MoveJesparToPlayer()
	
    DisablePlayerControls()
	
	SetNPCAsCompanion(_00E_MC_JesparREF, False)
	
	_00E_MC_JesparREF.MoveTo(MQ02_D7_JesparTeleportMarker)
	Wait(1)
    _00E_MC_JesparREF.StopCombat()
	MQ02_SC5_JesparArrivalInArk.ForceStart()

EndFunction

Function PuzzleLeverFailsave()
	
	; Ugly failsave to prevent the arbitrary disappearing of one/more puzzle levers
	
	MQ02_Puzzle_Lever_01.SetPosition(3435.8838, -2531.2515, -92.0632)
	MQ02_Puzzle_Lever_02.SetPosition(2135.6963, -2862.6311, -323.6631)
	MQ02_Puzzle_Lever_03.SetPosition(2137.8079, -2908.3262, -323.6631)
	MQ02_Puzzle_Lever_04.SetPosition(2248.0068, -3263.7217, -113.5741)
	MQ02_Puzzle_Lever_05.SetPosition(2166.4260, -2775.4336, -133.4723)

EndFunction

Function SprintHelpMessage()
	
	If _00E_DisableQuestTutorials.GetValueint() == 0
		if _00E_Tutorial_Sprint == None
			_00E_Tutorial_Sprint = Game.GetForm(0x00042B0A) as Message
		endif
    _00E_Tutorial_Sprint.Show()
	EndIf

EndFunction

Function ClearBookAlias()
	
	YeroBook.Clear()
	
EndFunction

Function FinishQuest()

	Levelsystem.RemoveSilence()
	Levelsystem.RemoveCombatSoundtracks()
	EnablePlayerControls()
	MQ03.SetStage(5)
	; SE: No such objective.
	; Self.SetObjectiveCompleted(70)
	Self.SetStage(175)
	_00E_MC_JesparREF.EvaluatePackage()
	MyradWardenFlusshaim.EnableNoWait()
	PlayerREF.RemoveItem(_00E_MQ02YeroBook)
	MQ02_TripToArkEnableMarker.DisableNoWait()
	SetFlusshaimTavernBarStoolsOwner(None) ; Failsafe
	Self.Stop()
	Cleanup()
	
EndFunction

Function Cleanup()

	; Clear reference properties to not keep them persistent
	FlusshaimTavernBarStool1   = None
	FlusshaimTavernBarStool3   = None
	MQ02_TripToArkEnableMarker = None
EndFunction

;=====================================================================================
;              							RIDDLE                  					 
;=====================================================================================

Event OnUpdate()
	
	ResetRiddle()

EndEvent

Function TryToInsertStick(ObjectReference ActivatorREF)
	
	while bInsertingStick == true
		Wait(0.1)
	endwhile
	bInsertingStick = true
	
	int CurrentValue = _00E_MQ02Riddle.GetValueInt()
	
	ObjectReference LinkedREF = ActivatorREF.GetLinkedRef()
	
	If GetStage() == 70 && LinkedREF.IsDisabled()
	
		If CurrentValue == 0
	
			iDifficulty = Utility.GetINIInt("iDifficulty:GamePlay")
			Int iUpdate = 15
		
			If iDifficulty == 0
				iUpdate = 19
			ElseIf iDifficulty == 1
				iUpdate = 17
			ElseIf iDifficulty == 4
				iUpdate = 13
			ElseIf iDifficulty == 5
				iUpdate = 11
			EndIf
		
			RegisterForSingleUpdate(iUpdate)
		EndIf
			
		QSTAstrolabeButtonPressX.Play(ActivatorREF)
		LinkedREF.Enable()
		ActivatorREF.SetDisplayName("")
		_00E_MQ02Riddle.SetValue(CurrentValue + 1)

		If _00E_MQ02Riddle.GetValue() == 5
			
			UnregisterForUpdate()
			MQ02_Puzzle_DoorREF.Activate(ActivatorREF as objectReference)
			SetStage(80)
		
			If _00E_MQ02_Riddle_IsThisTheFirstRun.GetValueInt() == 1 && _00E_AchievementsEnabled.GetValueInt() == 1
				Steam.UnlockAchievement("END_STARLING_PUZZLE_01")
				_00E_MQ02_Riddle_IsThisTheFirstRun.SetValueInt(0)
			EndIf
			
		EndIf
	
	EndIf
	
	bInsertingStick = false

EndFunction

Function ResetRiddle()

	_00E_MQ02Riddle.SetValue(0)
	QSTAstrolabeButtonOpenX.Play(PlayerREF)
	
	string _sName = MQ02_Puzzle_Lever_01.GetBaseOBject().GetName()
	
	MQ02_Puzzle_Lever_01.SetDisplayName(_sName)
	MQ02_Puzzle_Lever_02.SetDisplayName(_sName)
	MQ02_Puzzle_Lever_03.SetDisplayName(_sName)
	MQ02_Puzzle_Lever_04.SetDisplayName(_sName)
	MQ02_Puzzle_Lever_05.SetDisplayName(_sName)
	
	MQ02_Puzzle_Lever_01.GetLinkedRef().DisableNoWait()
	MQ02_Puzzle_Lever_02.GetLinkedRef().DisableNoWait()
	MQ02_Puzzle_Lever_03.GetLinkedRef().DisableNoWait()
	MQ02_Puzzle_Lever_04.GetLinkedRef().DisableNoWait()
	MQ02_Puzzle_Lever_05.GetLinkedRef().DisableNoWait()
	
	_00E_MQ02_Riddle_IsThisTheFirstRun.SetValueInt(0)
			
	If CompanionIsTalking.GetValueInt() == 0
		CompanionIsTalking.SetValueInt(1)
		_00E_MC_JesparREF.Say(MQ02_SC2_JesparDoItAgainTopic)
		CompanionIsTalking.SetValueInt(0)
	EndIf

EndFunction

Function SetFlusshaimTavernBarStoolsOwner(ActorBase abNewOwner)

	FlusshaimTavernBarStool1.SetActorOwner(abNewOwner)
	FlusshaimTavernBarStool3.SetActorOwner(abNewOwner)

EndFunction


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

int Property __Config_RewardEXP Auto
{How much exp will be given upon quest completion]}

int Property iPlayerClass Auto Conditional Hidden
; 1: Rogue 2: Mage, 3: Warrior

int iDifficulty

bool bCountingBooks = false
bool bReadingBooks = false
int iBookCounter
int iReadCounter

bool bInsertingStick = false

Actor Property PlayerREF Auto
Actor Property _00E_MC_JesparREF Auto
Actor Property MQ02_CampBanditREF Auto

_00E_Game_SkillmenuSC Property Player Auto
_00E_QuestFunctions Property Levelsystem Auto

Potion Property _00E_LehrbuchBlockTo25 Auto
Potion Property _00E_LehrbuchAlterationTo25 Auto
Potion Property _00E_LehrbuchConjurationTo25 Auto
Potion Property _00E_LehrbuchDestructionTo25 Auto
Potion Property _00E_LehrbuchHeavyArmorTo25 Auto
Potion Property _00E_LehrbuchIllusionTo25 Auto
Potion Property _00E_LehrbuchLightArmorTo25 Auto
Potion Property _00E_LehrbuchMarksmanTo25 Auto
Potion Property _00E_LehrbuchOnehandedTo25 Auto
Potion Property _00E_LehrbuchRestorationTo25 Auto
Potion Property _00E_LehrbuchSneakTo25 Auto
Potion Property _00E_LehrbuchTwohandedTo25 Auto
Potion Property _00E_HandwerksbuchLockpickingTo25 Auto
Potion Property _00E_HandwerksbuchSmithingTo25 Auto

MusicType Property _00E_Music_Special_Character_Jespar Auto
MusicType Property _00E_SilenceLongTransitionHighPriority Auto

Message Property _00E_Tutorial_Skillbooks Auto
Message Property _00E_Tutorial_QuestDifficulty Auto
Message Property _00E_MQ02_TutorialCompanionFollow Auto
Message Property _00E_Tutorial_Sprint Auto
Message Property _00E_JesparEntriggerFollow Auto

ImageSpaceModifier Property FadeToBlackHoldIMOD Auto
ImageSpaceModifier Property _00E_MQ02_WakeUp Auto
ImageSpaceModifier Property FadeToBlackAndBackIMOD Auto
ImageSpaceModifier Property FadeToBlackAndBackIMOD02 Auto

Idle Property Idle_1stPersonWoozyGetUpFromBed Auto
	
Sound Property _00E_NPCUncorkPotionAndDrinkM Auto
Sound Property QSTAstrolabeButtonOpenX Auto
Sound Property QSTAstrolabeButtonPressX Auto

ObjectReference Property MQ02_D0_PlayersWakeUpmarker02 Auto
ObjectReference Property MQ02_D0_JesparSpawnMarker Auto
ObjectReference Property MQ02_D5_JesparYeroMarker01 Auto
ObjectReference Property MQ02_D5_YeroFarmTrapdoor Auto
ObjectReference Property _00E_MQ02_SC3_YeroBook Auto
ObjectReference Property MQ02_D5_JesparTeleportMarker Auto
ObjectReference Property MQ02_D7_JesparTeleportMarker Auto
ObjectReference Property MQ02_D4_AlfridBench Auto
ObjectReference Property MyradWardenFlusshaim Auto

ObjectReference Property MQ02_Puzzle_Lever_01 Auto
ObjectReference Property MQ02_Puzzle_Lever_02 Auto
ObjectReference Property MQ02_Puzzle_Lever_03 Auto
ObjectReference Property MQ02_Puzzle_Lever_04 Auto
ObjectReference Property MQ02_Puzzle_Lever_05 Auto
ObjectReference Property MQ02_Puzzle_DoorREF Auto

ObjectReference Property FlusshaimTavernBarStool1 Auto
ObjectReference Property FlusshaimTavernBarStool3 Auto
ObjectReference Property MQ02_TripToArkEnableMarker Auto

Book Property _00E_MQ02YeroBook Auto

Scene Property MQ02_SC1_JesparDialogeInterlude Auto
Scene Property MQ02_SC2_PuzzleScene Auto
Scene Property MQ02_SC3_FollowJesparToArk Auto
Scene Property MQ02_SC4_JesparPanoramaScene Auto
Scene Property MQ02_SC5_JesparArrivalInArk Auto
Scene Property MQ02_SC6_AlfriedTravelToBench Auto

Scroll Property _01E_TeleportrolleFlusshaim Auto

Quest Property MQ03 Auto

GlobalVariable Property Timescale Auto
GlobalVariable Property CompanionIsTalking Auto
GlobalVariable Property GameHour Auto
GlobalVariable Property _00E_DisableQuestTutorials Auto
GlobalVariable Property _00E_MQ02Riddle Auto
GlobalVariable Property _00E_MQ02_Riddle_IsThisTheFirstRun Auto
GlobalVariable Property _00E_AchievementsEnabled Auto

Topic Property MQ02_SC2_JesparDoItAgainTopic Auto

Weather Property SkyrimClear Auto

ReferenceAlias Property YeroBook Auto