Scriptname _00E_MQ09_QuestFunctions Extends Quest Conditional

Import Utility

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

Event OnUpdateGameTime()

	Timescale.SetValue(0.001)

EndEvent

Event OnUpdate()

	If GetStage() >= 60 && (MQ10a.GetStage() < 5 || !MQ10a.IsRunning())
	
		MQ10a.SetStage(5)
        
        Wait(1)
        
        if MQ10a.GetStage() < 5
        
           _00E_MQ09_MQ10a_Broken_Message.Show()
            RegisterForSingleUpdate(6)

        endif
	ElseIf GetStage() == 15 && _00E_MC_JesparREF.GetParentCell() != MQ09bSkelett.GetParentCell()
		
		_00E_MC_JesparREF.MoveTo(MQ09_SC1_JesparTravelToREF)
		
	EndIf
	
EndEvent

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

Function RemoveSilences()

	Levelsystem.RemoveSilence()

EndFunction

Function AdjustGamehour()

	Timescale.SetValue(20)
	GameHour.SetValue(18.3) ; Minor correction of time after InitQuest
	RegisterForSingleUpdateGameTime(3)

EndFunction

Function InitQuest()

	; Restore player's health if it's damaged because we just "slept"
	PlayerREF.RestoreAV("Health", 10000.0)

	; Force the player to exit any furniture (the benches from MQ09a's scenes) before putting them to the bed
	; Without this exiting the bed is glitchy
	PlayerREF.MoveTo(MQ09JesparPortMarker)
	Utility.Wait(1.0)

	Timescale.SetValue(0.1)
	Levelsystem.SkipTimeToHour(18.3)
	MQ09_LightParentREF.Disable()
	FadeToBlackHoldIMOD.Apply()
	PlayerREF.MoveTo(MQ09_SC01_PlayerBedREF)
	_00E_MC_JesparREF.MoveTo(MQ09JesparPortMarker)
	bBedFailsave = True
	MQ09StaticDoor.disable()
	MQ08_SoultrainDoorREF.SetOpen(True)
	
	_00E_QuestFunctions.PlayerAIWalk(True)
	Game.ForceThirdPerson()
	
	If MQ09_D1_SceneIsle == None
		MQ09_D1_SceneIsle = Game.GetFormFromFile(0x000EA957, "Skyrim.esm") as Scene
	EndIf
	If !MQ09_D1_SceneIsle.IsPlaying()
		MQ09_D1_SceneIsle.ForceStart()
	EndIf
	
	MQ09a.StopTrainSound()
	Self.SetStage(10)
	
EndFunction

Function StartSC01Failsave()

	If MQ09_D1_SceneIsle == None
		MQ09_D1_SceneIsle = Game.GetFormFromFile(0x000EA957, "Skyrim.esm") as Scene
	EndIf
	MQ09_D1_SceneIsle.ForceStart()
	
EndFunction

Function SC01JesparFailsafe()

	RegisterForSingleUpdate(10)
	
EndFunction

Function PlayerStandUp()

	UnlockDoor()
	String facegen = "bUseFaceGenPreprocessedHeads:General"
	SetINIBool(facegen, False)
	Game.GetPlayer().QueueNiNodeUpdate()
	SetINIBool(facegen, True)
	ImagespaceModifier.RemoveCrossFade()
	FadeToBlackHoldIMOD.Remove()
	_00E_WakeUp.ApplyCrossFade(afFadeDuration = 0.5)
	MQ09_SC01_PlayerBedREF.Activate(PlayerREF, true)
	_00E_QuestFunctions.PlayerAiWalkStop()

EndFunction

Function UnlockDoor()

	String facegen = "bUseFaceGenPreprocessedHeads:General"
	SetINIBool(facegen, False)
	Game.GetPlayer().QueueNiNodeUpdate()
	SetINIBool(facegen, True)

	MQ08TrainDoorRef.DisableNoWait()
	MQ09TrainDoorRef001.Lock(0)

	if TimescaleContainer == 0
		TimescaleContainer = Timescale.GetValue()
	endif
	wait(0.4)
	
EndFunction

Function PlayFX()

	_FS_TheriantrophistControlQuest.TransformBackIfTransformed()

	Actor Spinne = HoheSpinne.GetActorReference()
	Actor Bear = HoherBear.GetActorReference()
	Actor Tiger = HoherTiger.GetActorReference()
	
	MS04MemoryFXBody01VFX.Play(Bear)
	MS04MemoryFXBody01VFX.Play(Spinne)
	MS04MemoryFXBody01VFX.Play(Tiger)

	Bear.SetAlpha(0.5, True)
	Spinne.SetAlpha(0.5, True)
	Tiger.SetAlpha(0.5, True)
	
EndFunction

Function PlayZyklus()

	AudioCategoryAMB.SetVolume(0.5)
	AudioCategoryAMBr.SetVolume(0.5)
	_00E_Music_Special_Zyklus_Looped.Add()
	_00E_SilenceAbruptHighPriority.Add()
	
EndFunction

Function RemoveZyklus()
	_00E_Music_Special_Zyklus_Looped.Remove()
EndFunction

Function PlayTealorTheme()

	_00E_Music_Special_Character_Tealor.Add()
	_00E_SilenceAbruptHighPriority.Add()
	
EndFunction

Function RemoveTealorTheme()

	_00E_Music_Special_Character_Tealor.Remove()

EndFunction

Function HohenAufloesen()

	ObjectReference Bear = HoherBear.GetReference()
	ObjectReference Tiger = HoherTiger.GetReference()
	ObjectReference Spinne = HoheSpinne.GetReference()
	
	_00E_HighOnesDisintegrate.Play(Bear)
	_00E_HighOnesDisintegrate.Play(Tiger)
	_00E_HighOnesDisintegrate.Play(Spinne)
	_00E_A2_Ghostwalk_TeleportDoneSound.Play(playerREF)

	Game.DisablePlayerControls(1, 1, 1, 1, 1, 1, 1)

EndFunction

Function HohenDisable()

	Actor Spinne = HoheSpinne.GetActorReference()
	Actor Bear = HoherBear.GetActorReference()
	Actor Tiger = HoherTiger.GetActorReference()
	
	Spinne.SetAlpha(0.0)
	Bear.SetAlpha(0.0)
	Tiger.SetAlpha(0.0)

	_00E_HighOnesDisintegrate.Stop(Spinne)
	_00E_HighOnesDisintegrate.Stop(Bear)
	_00E_HighOnesDisintegrate.Stop(Tiger)

 	; Move the hohen to the NPC dumpster instead of disabling because they are attached to an enable marker
	Spinne.MoveTo(_00E_NPCDumpMarker)
	Bear.MoveTo(_00E_NPCDumpMarker)
	Tiger.MoveTo(_00E_NPCDumpMarker)

EndFunction

Function GameFadeOut()

	Game.DisablePlayerControls()
	Game.ForceFirstPerson()
	WPNSwingUnarmedM.Play(PlayerREF)
	HohenDisable()
	AlmaRef.MoveTo(AlmaPortRef)
	Wait(0.15)
	PlayerREF.PlayIdle(TG05_KnockOut)
	Game.ShakeCamera(None,8, 1)
	
	if PlayerREF.GetActorBase().GetSex() == 0
		_00E_Male_PlayerHitM.Play(PlayerREF)
	Else
		_00E_Female_PlayerHitM.Play(PlayerREF)
	EndIf
	
	WPNImpactBladeVsFlesh.Play(PlayerREF)
	Game.TriggerScreenBlood(10)	
	Wait(3.2)
	_00E_NPCKillBodyfall.Play(PlayerREF)
	FadeToBlackHoldImod.Apply()
	PlayerREF.RemoveItem(_00E_MQ09_Silverplate, 1, true, none)
	SkyrimClear.ForceActive()
	RemoveZyklus()
	Wait(4)

	Game.EnablePlayerControls()
	Game.DisablePlayerControls(true, true, true, false, true, true, true)
	
	_00E_MQ09_SamaelRef.Enable()
	_00E_SC_AlmaREF.Enable()
	_00E_MC_TaranorREF.Enable()
	
	MQ09_TaranorScene_LinkMarkerREF.Enable()
	_00E_MC_TaranorREF.MoveTo(MQ09_SC04_TaranorMarker)
	_00E_MQ09_SamaelRef.MoveTo(MQ09_SC4_SamaelMarker)
	If _00E_MC_TaranorREF.GetParentCell() != PlayerREF.GetParentCell()
		_00E_MQ09_SamaelRef.MoveTo(MQ09_SC4_SamaelMarker)
	EndIf
	_00E_SC_AlmaREF.MoveTo(MQ09_SC04_AlmaMarkerREF)
	If MQ09_MC_TealorREF.IsDisabled()
		MQ09_MC_TealorREF.Enable()
	EndIf
	MQ09_MC_TealorREF.MoveTo(MQ09_SC06_TealorTurnAround)
	_00E_SC_AlmaREF.SetOutfit(_00E_NehrimeseOutfit01NoHelmetNoShield)
	PlayerRef.MoveTo(MQ09_SC04_ChairMarkerPlayer)
	_00E_QuestFunctions.PlayerAIWalk(True)

	Wait(1)
	MQ09_D4_TaranorScene.ForceStart()
	GhostFXShader.Stop(MQ09_MC_TealorREF)
	MQ09_MC_TealorREF.SetAlpha(0.0)

EndFunction

Function WakeUp()
	
	GhostFXShader.Stop(MQ09_MC_TealorREF)
	MQ09_MC_TealorREF.SetAlpha(0.0)
	MQ09_SC06_SilverPlateRef.Enable()
	; Move Samael to the player to "take blindfold off". The packages will return him to his actual place in the scene.
	_00E_MQ09_SamaelRef.Disable()
	_00E_MQ09_SamaelRef.MoveTo(MQ09_SC04_SamaelStunplayer)
	_00E_MQ09_SamaelRef.Enable()
	_00E_WakeUp.Apply()
	FadeToBlackHoldImod.Remove()
	Wait(2.5)
	
EndFunction

Function MQ09_D5_RepeatForceGreetSceneStart()

	MQ09_D5_RepeatForceGreetScene.ForceStart()
	
EndFunction
	
Function MQ09_D6_SceneForceStart()

	MQ09_D6_Scene.ForceStart()
	
EndFunction
	
Function KillPlayer()

	WerewolfExtractVFX.Play(PlayerREF)
	WerewolfExtractVFX.Play(_00E_MQ09_SamaelRef)
	_00E_AMB_Mystic_Whispers02M.Play(PlayerREF)
	PoisonDeath.Apply()
	Wait(2.9)
	FadeToBlackHoldIMOD.ApplyCrossFade(afFadeDuration = 0.5)
	PlayerREF.DamageAV("Health", 1000)
	PlayerREF.KillEssential(_00E_MQ09_SamaelRef)
	Wait(3)
	WerewolfExtractVFX.Stop(PlayerREF)
	WerewolfExtractVFX.Stop(_00E_MQ09_SamaelRef)
	Game.QuitToMainMenu()
	
EndFunction

Function DisablePlate()

	MQ09_SC06_SilverPlateRef.Disable()

EndFunction

Function MovePlate()

	MQ09_SC06_SilverPlateListenREF.Enable()

EndFunction

Function SamaelSalute()

	_00E_MQ09_SamaelRef.PlayIdle(IdleSalute)

EndFunction

Function EnableSilverPlate()

	SilberplatteRef.Enable()
	SilberplatteRef.SetMotionType(4)
	
EndFunction

Function AppearTealor()

	MAGShockExpImod.Apply()
	_00E_MQ04_CrystalTouch.Play(PlayerREF)
	MQ09_SC06_SilverPlateListenREF.PlaceAtMe(_00E_NPCOorbayaTeleportExplosionBlue, 1)
	_00E_A2_Ghostwalk_TeleportDoneSound.Play(MQ09_MC_TealorREF)
	GhostFXShader.Play(MQ09_MC_TealorREF)

EndFunction

Function TealorSetAlpha()

	MQ09_MC_TealorREF.SetAlpha(0.0)
	TimeFadeIn01FXS.Stop(MQ09_MC_TealorREF)

EndFunction

Function FadeInTealor()

	MQ09_MC_TealorREF.MoveTo(MQ09_SC06_SilverPlateListenREF)
	TimeFadeIn01FXS.Play(MQ09_MC_TealorREF)
	Wait(1.5)
	GhostFXShader.Play(MQ09_MC_TealorREF)
	MQ09_MC_TealorREF.SetAlpha(0.5)
	_00E_MC_TaranorREF.SetActorValue("speedMult", 65)

EndFunction

Function DisappearTealor()

	ObjectReference TealorRef = Tealor.GetReference()
	_00E_A2_Ghostwalk_TeleportDoneSound.Play(TealorRef)
	TimeFadeOut01FXS.Play(TealorRef)
	Wait(2)
	TealorRef.Disable()
	GhostFXShader.Stop(MQ09_MC_TealorREF)
	Game.RequestAutoSave()
		
EndFunction

Function EndQuest()

	_00E_Music_Special_Zyklus_Looped.Remove()
	_00E_Music_Special_Zyklus_OneSelection.Remove() ; A remnant from 1.5.7.0 and older versions
	_00E_Music_Special_Character_Tealor.Remove()
	AudioCategoryAMB.SetVolume(1.0)
	AudioCategoryAMBr.SetVolume(1.0)
	Timescale.SetValue(0.1)
	SpieluhrSwitcher.Enable()
	MAGConjureReanimate.Play(PlayerREF)
	Game.DisablePlayerControls(1, 1, 1, 1, 1, 1, 1)
	_00E_MQ09KnockOutLong.Apply()
	Wait(4.0)
	FadeToBlackHoldImod.ApplyCrossFade()
	Game.ShowFirstPersonGeometry(true)
	_00E_MC_TaranorREF.SetActorValue("speedMult", 100)
	RegisterForSingleUpdate(3)
	MQ10a.SetStage(5)
	
EndFunction

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

Float TimescaleContainer = 0.00
Bool Done= False

Bool Property bBedFailsave Auto Conditional Hidden

_00E_QuestFunctions Property Levelsystem Auto
_00E_MQ08_Questfunctions Property MQ09a Auto

Actor Property _00E_MC_JesparREF Auto
Actor Property PlayerREF Auto
Actor Property _00E_MC_TaranorREF Auto
Actor Property _00E_SC_AlmaREF Auto
Actor Property _00E_MQ09_SamaelRef Auto
Actor Property MQ09_MC_TealorREF Auto
Actor Property MQ09bSkelett Auto

MusicType Property _00E_Music_Special_Zyklus_OneSelection Auto
MusicType Property _00E_Music_Special_Zyklus_Looped Auto
MusicType Property _00E_SilenceTransitionLowPriority Auto
Explosion Property _00E_NPCOorbayaTeleportExplosionBlue Auto
MusicType Property _00E_SilenceAbruptHighPriority Auto
MusicType Property _00E_Music_Special_Character_Tealor Auto

Sound Property WPNSwingUnarmedM Auto
Sound Property _00E_A2_Ghostwalk_TeleportDoneSound Auto
Sound Property WPNImpactBladeVsFlesh Auto
Sound Property MAGConjureReanimate Auto
Sound Property _00E_Male_PlayerHitM Auto
Sound Property _00E_Female_PlayerHitM Auto
Sound Property _00E_NPCKillBodyfall Auto
Sound Property _00E_MQ04_CrystalTouch Auto
Sound Property _00E_AMB_Mystic_Whispers02M Auto

Outfit Property _00E_NehrimeseOutfit01NoHelmetNoShield Auto

EffectShader Property TimeFadeOut01FXS Auto
EffectShader Property TimeFadeIn01FXS Auto
EffectShader Property _00E_HighOnesDisintegrate Auto
EffectShader Property GhostFXShader Auto

VisualEffect Property MS04MemoryFXBody01VFX Auto
VisualEffect Property WerewolfExtractVFX Auto

ReferenceAlias Property HoherBear Auto
ReferenceAlias Property HoherTiger Auto
ReferenceAlias Property HoheSpinne Auto
ReferenceAlias Property Tealor Auto

Idle Property TG05_KnockOut Auto
Idle Property IdleSalute Auto

ObjectReference Property MQ09StaticDoor Auto
ObjectReference Property SpieluhrSwitcher Auto 
ObjectReference Property MQ09JesparPortMarker Auto
ObjectReference Property MQ08TrainDoorRef Auto
ObjectReference Property MQ09TrainDoorRef001 Auto 
ObjectReference Property MQ09_SC01_PlayerBedREF Auto
ObjectReference Property AlmaRef Auto
ObjectReference Property AlmaPortRef Auto
ObjectReference Property SilberplatteRef Auto
ObjectReference Property MQ09_LightParentREF Auto
ObjectReference Property MQ09_SC4_SamaelMarker Auto
ObjectReference Property MQ09_SC04_AlmaMarkerREF Auto
ObjectReference Property MQ09_SC04_TaranorMarker Auto
ObjectReference Property MQ09_SC04_ChairMarkerPlayer Auto
ObjectReference Property MQ09_TaranorScene_LinkMarkerREF Auto
ObjectReference Property MQ09_SC06_SilverPlateRef Auto
ObjectReference Property MQ09_SC06_SilverPlateListenREF Auto
ObjectReference Property MQ09_SC06_TealorTurnAround Auto
ObjectReference Property MQ08_SoultrainDoorREF Auto
ObjectReference Property MQ09_SC1_JesparTravelToREF Auto
ObjectReference Property MQ09_SC04_SamaelStunplayer Auto
ObjectReference Property _00E_NPCDumpMarker Auto

Quest Property MQ10a Auto

ImagespaceModifier Property _00E_MQ09KnockOutLong Auto
ImageSpaceModifier Property FadeToBlackHoldImod Auto
ImageSpaceModifier Property _00E_WakeUp Auto 
ImageSpaceModifier Property MAGShockExpImod Auto 
ImageSpaceModifier Property PoisonDeath Auto 

Weather Property SkyrimClear Auto

GlobalVariable Property Gamehour Auto
GlobalVariable Property Timescale Auto

SoundCategory Property AudioCategoryAMB Auto
SoundCategory Property AudioCategoryAMBr Auto

Scene Property MQ09_D1_SceneIsle Auto
Scene Property MQ09_D4_TaranorScene Auto
Scene Property MQ09_D5_RepeatForceGreetScene Auto
Scene Property MQ09_D6_Scene Auto

MiscObject Property _00E_MQ09_Silverplate Auto

Message Property _00E_MQ09_MQ10a_Broken_Message Auto