Scriptname _00E_MQ10a_Functions extends Quest  

Import Utility

int function _GetScriptVersion() Global
    return 1
endFunction

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

Function SetUpScene()

    ; Prepare for scene MQ10a_SC1_DreamSequencePart01, part 1 of 3.
    ; This is called from the result script of MQ10a stage 5.
	_FS_Phasmalist_ControlQuest.UnsummonApparitionIfExists()
	MQ10a_SC1_HeartParentREF.Enable()
	_00E_PlayerFunctions.GetSoundControl().RemoveSilence()
	PlayerREF.MoveTo(PlayerStartMarkerNew)
	fPlayerScale = _00E_SetActorScale.Change(PlayerREF, 0.85)
	_00E_TimeControl.SkipTimeToHour(18.4)
	Game.ForceFirstPerson()
	_00E_Func_AIWalk.PlayerAIWalkStop() ; workaround for the "uncompiled scripts bug" of patch 1.5.8.0
	;Game.EnablePlayerControls()
	Game.DisablePlayerControls(false, true, true, false, true, true, true, true, 0)
	Game.RequestAutoSave()
	PlayerREF.PlayIdle(TG05_GetUp)
	if GetCurrentStageID() >= 5
		FadeToBlackBackImod2SecPause.Apply()
	endif
	Game.ShowFirstPersonGeometry(false)
	Utility.Wait(1)
	FadeToBlackHoldImod.Remove()
	
	SetUpSceneForReal()

EndFunction

Function SetUpSceneForReal()

    ; Prepare for scene MQ10a_SC1_DreamSequencePart01, part 2 of 3.
    ; ; This is called when the player enters triggerbox
    ; ; _00E_MQ10a_SetUpTGBX, on their way to Father.
    ; This is called from function SetUpScene().
    ; ; This used to be in the SetUpScene() function, but it overcrowded
    ; ; the function, leading to difficulties starting the quest
    ; ; (might be part of the reason for the infamous MQ09 blackscreen
    ; ; bug).

	int iIndex = _00E_Dreams_ObjectList01.GetSize()
	
	while iIndex > 0
		iIndex -= 1
		ObjectReference objToDelete = _00E_Dreams_ObjectList01.GetAt(iIndex) as ObjectReference
		objToDelete.Disable()
	EndWhile
	MQ10a_SC1_FatherREF.Enable()
	MQ10a_SC1_FatherREF.SetOutfit(_00E_Father_Outfit_Decayed)
	MQP01_DreamDoorREF.Lock(True)
	MQP01_DreamDoorREF.SetLockLevel(255)
	;MQ10a_SC1_DreamSequencePart01.ForceStart() starts with the begin of the quest anyway, this only causes a papyrus error
	EnableSisterAndMother()
	_00E_PlayerFunctions.GetSoundControl().RemoveSilence()

EndFunction

Function UnlockDoor()

	MQP01_DreamDoorREF.Lock(False)
	MQP01_DreamDoorREF.SetOpen(True)

EndFunction

Function EnableSisterAndMother()

    ; Prepare for scene MQ10a_SC1_DreamSequencePart01, part 3 of 3.
    ; This is called from function SetUpSceneForReal().
    ; Apparently this function is also called again when the player
    ; talks to father.

	MQ10a_SC1_SisterREF.Enable()
	MQ10a_SC1_MotherREF.Enable()
	MQ10a_SC1_SisterREF.SetAlpha(1.0)
	MQ10a_SC1_MotherREF.SetAlpha(1.0)
	MQ10a_SC1_MotherREF.MoveTo(MQ10a_SC1_MotherChairREF)
	MQ10a_SC1_SisterREF.MoveTo(MQ10a_SC1_SisterChairREF)

EndFunction

Function SetTime()

	MQ10a_SC1_MotherREF.Enable()
	MQ10a_SC1_SisterREF.Enable()
	TimeScale.SetValue(450)
	RegisterForSingleUpdate(1)
	
EndFunction

Function AdvanceTime()

	FXCameraAttachEmbersEffect.Play(PlayerREF)
	_00E_MAGFXFireOilIgniteLow.Play(MQ10a_SC1_FatherREF)
	_00E_MAGFXFireOilIgniteLow.Play(MQ10a_SC1_SisterREF)
	_00E_MAGFXFireOilIgniteLow.Play(MQ10a_SC1_MotherREF)
	FireFXShader.Play(MQ10a_SC1_MotherREF)
	FireFXShader.Play(MQ10a_SC1_SisterREF)
	FireFXShader.Play(MQ10a_SC1_FatherREF)
	_00E_Music_Special_MQP01_BuildUp.Add()

EndFunction

Function CollapseDream()
	
	_00E_MQ10a_HeartbeatM.Play(PlayerREF)
	_00E_MQ10a_DreamCollapseIMOD.Apply()
	
EndFunction

Function FadeToBlack()
	
	FadeToBlackHoldImod.ApplyCrossFade(0.5)

EndFunction

Function AddSilence()

	_00E_SilenceAbruptHighPriority.Add()
	SetCurrentStageID(25)
	
EndFunction

Function FireStage01()

	MAGFireBallExpImod.Apply()
	MQ10a_SC1_FireREF_01.EnableNoWait()
	MQ10a_SC1_FireREF_02.EnableNoWait()
	Game.ShakeCamera(afStrength = 0.3)
	MAGDestructionFireStormExplosionM.Play(PlayerREF)
	
EndFunction

Function FireStage02()

	MAGFireBallExpImod.Apply()
	MQ10a_SC1_FireREF_03.EnableNoWait()
	MQ10a_SC1_FireREF_04.EnableNoWait()
	Game.ShakeCamera(afStrength = 0.6)
	MAGDestructionFireStormExplosionM.Play(PlayerREF)
	
EndFunction

Function CleanUp()

	_00E_SilenceAbruptHighPriority.Remove()
	Timescale.SetValue(10)
	PlayerREF.SetScale(fPlayerScale)
	Game.ShowFirstPersonGeometry(true)
	MQ10a_SC1_HeartParentREF.Disable()
	FXCameraAttachEmbersEffect.Stop(PlayerREF)
	MQ10a_SC1_FireREF_01.Disable()
	MQ10a_SC1_FireREF_02.Disable()
	MQ10a_SC1_FireREF_03.Disable()
	MQ10a_SC1_FireREF_04.Disable()
	MQ10a_SC1_MotherREF.Disable()
	MQ10a_SC1_SisterREF.Disable()
	MQ10a_SC1_FatherREF.Disable()
	
	If MQ10a_SC1_DreamSequencePart01.IsPlaying()
	
		MQ10a_SC1_DreamSequencePart01.Stop()
		
	EndIf
	
	MQ10b.SetCurrentStageID(5)
	
EndFunction

;=====================================================================================
;              							STATES                  					 
;=====================================================================================

Event OnUpdate()

	If GetCurrentStageID() < 25
	
		If GameHour.GetValue() >= 22
			Timescale.SetValue(0.01)
		Else
			RegisterForSingleUpdate(1)
		EndIf
		
	EndIf
	
EndEvent

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

float fPlayerScale

Actor Property MQ10a_SC1_FatherREF Auto
Actor Property MQ10a_SC1_MotherREF Auto
Actor Property MQ10a_SC1_SisterREF Auto
Actor Property PlayerREF Auto

Formlist Property _00E_Dreams_ObjectList01 Auto

ImageSpaceModifier Property _00E_MQ10a_DreamCollapseIMOD Auto

MusicType Property _00E_SilenceAbruptHighPriority Auto
MusicType Property _00E_Music_Special_MQP01_BuildUp Auto

EffectShader Property FireFXShader Auto

GlobalVariable Property Timescale Auto
GlobalVariable Property GameHour Auto

Outfit Property _00E_Father_Outfit_Decayed Auto

VisualEffect Property FXCameraAttachEmbersEffect Auto

Quest Property MQ10b Auto

ObjectReference Property PlayerStartMarkerNew Auto
ObjectReference Property MQP01_DreamDoorREF Auto
ObjectReference Property MQ10a_SC1_FireREF_01 Auto
ObjectReference Property MQ10a_SC1_FireREF_02 Auto
ObjectReference Property MQ10a_SC1_FireREF_03 Auto
ObjectReference Property MQ10a_SC1_FireREF_04 Auto
ObjectReference Property MQ10a_SC1_HeartParentREF Auto
ObjectReference Property MQ10a_SC1_MotherChairREF Auto
ObjectReference Property MQ10a_SC1_SisterChairREF Auto

Scene Property MQ10a_SC1_DreamSequencePart01 Auto

Sound Property _00E_MQ10a_HeartbeatM Auto
Sound Property _00E_MAGFXFireOilIgniteLow Auto
Sound Property MAGDestructionFireStormExplosionM Auto

Float TimescaleBefore

Idle Property TG05_GetUp Auto

ImageSpaceModifier Property FadeToBlackHoldImod Auto
ImageSpaceModifier Property FadeToBlackBackImod Auto
ImageSpaceModifier Property FadeToBlackBackImod2SecPause Auto
ImageSpaceModifier Property MAGFireBallExpImod Auto