Scriptname _00E_MQP02_Functions extends Quest Conditional 

Import Utility
Import _00E_QuestFunctions

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

Event OnUpdate()

	If GetStage() < 10
		SetStage(10)
	EndIf

EndEvent

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

Function MovePlayer()

	If !(MQP02_D0_PlayerStartMarker == None)
		PlayerREF.MoveTo(MQP02_D0_PlayerStartMarker)
	Else
		PlayerREF.MoveTo(PlayerStartMarker.GetReference())
	EndIf
	
EndFunction


Function WakeUp()

	FXCameraAttachEmbersEffect.Stop(PlayerREF)
	FXCameraAttachEmbersHeavyEffect.Stop(PlayerREF)
	Game.PrecacheCharGen()
	PlayerREF.RestoreActorValue("Health", 100)
	Levelsystem.RemoveAllItemsSafeVersion(None)
	Game.ForceFirstPerson()
	Game.DisablePlayerControls()
	;Game.RequestAutoSave()
	FadeToBlackHOLDIMod.Apply()
	
	If !(MQP02_D0_PlayerStartMarker == None)
		PlayerREF.MoveTo(MQP02_D0_PlayerStartMarker)
	Else
		PlayerREF.MoveTo(PlayerStartMarker.GetReference())
	EndIf
	
	PlayerREF.PlayIdle(TG05_Knockout)
	PlayerREF.AddItem(ClothesPrisonerRags, 1, True)
	PlayerREF.EquipItem(ClothesPrisonerRags, false, true)
	Levelsystem.SkipTimeToHour(1)

	Wait(2)
	PlayerREF.PlayIdle(TG05_GetUp)
			
	MQP02_SC1_PlayerWaksUp.ForceStart()
	
	Wait(1)
	FadeToBlackHOLDIMod.PopTo(_00E_WakeUp)
	Wait(7.5)
	
	RegisterForSingleUpdate(5)
	
	Self.SetStage(10)

EndFunction

Function StartCharacterGeneration()

	Debug.SendAnimationEvent(PlayerREF, "IdleForceDefaultState")
	Game.DisablePlayerControls()
	Game.ShowRaceMenu()
	RegisterForSingleUpdate(1)
	_00E_Music_Special_WiegeDesLebens.Add()
	GoToState("Chargen")

EndFunction

Function AddRacialAbilites()

	Spell racialAb_Aeterna = Game.GetFormFromFile(0x0101EC69, "Enderal - Forgotten Stories.esm") as Spell
	Spell racialAb_Arazalean = Game.GetFormFromFile(0x0101EC68, "Enderal - Forgotten Stories.esm") as Spell
	Spell racialAb_Kilean = Game.GetFormFromFile(0x0101EC67, "Enderal - Forgotten Stories.esm") as Spell
	Spell racialAb_Qyranian = Game.GetFormFromFile(0x0101EC66, "Enderal - Forgotten Stories.esm") as Spell

	If Player.GetRace() == HighElfRace
	
		PlayerREF.AddSpell(racialAb_Aeterna)
	
	ElseIf Player.GetRace() == NordRace
	
		PlayerREF.AddSpell(racialAb_Arazalean)
	
	ElseIf Player.GetRace() == BretonRace
	
		PlayerREF.AddSpell(racialAb_Kilean)
	
	Else
	
		PlayerREF.AddSpell(racialAb_Qyranian)
	
	EndIf

EndFunction

Function StartSC2()

	Game.EnablePlayerControls()
	MQP02_SC3_Navcut.Disable()
	
	If _00E_FS_IsForgottenStoriesActivated.GetValueInt() == 1
		AddRacialAbilites()	
	EndIf
	
	FreightRoomDoor.GetReference().Lock(False)
	Riq.GetActorReference().Enable()
	Sebald.GetActorReference().Enable()
	MQP02_RiqRef.MoveTo(MQP02_SC2_RiqStartMarker)
	MQP02_SebaldRef.MoveTo(MQP02_SC2_SebaldStartMarker)
	MQP02_SC2_RiqAndSebald01.ForceStart()
	
EndFunction


Function ShowLanternMessage()
		
	Message.ResetHelpMessage("Activate")
	_00E_MQP02_Tutorial_Activate.ShowAsHelpMessage("Activate", 5, 1, 1)

EndFunction

Function StartSC03()

	MQP02_SC3_RiqAndSebald02.ForceStart()
	
EndFunction

Function OpenFreightRoomDoor()

	MQP02_SC4_JailDoor.SetOpen(True)

EndFunction

Function EnableNavcut()

	MQP02_SC3_Navcut.Enable()
	
EndFunction

Function DisableNavcut()

	MQP02_SC3_Navcut.Disable()
	
EndFunction

Function StartCombatMusic()

	Message.ResetHelpMessage("Attack")
	Game.RequestAutoSave()
	_00E_MQP02_CombatMusic.Add()
	MQP02_SC4_JailDoor.Lock(True)
	MQP02_SC4_JailDoor.SetOpen(False)
	If PlayerREF.GetEquippedSpell(0) != None
		PlayerREF.UnequipSpell(PlayerREF.GetEquippedSpell(0), 0)
	EndIf
	If PlayerREF.GetEquippedSpell(1) != None
		PLayerREF.UnequipSpell(PlayerREF.GetEquippedSpell(1), 1)
	EndIf
	PlayerREF.DrawWeapon()
	SetObjectiveDisplayed(5)
	Wait(1.5)

EndFunction

Function DisableBlockade()

	; Disable the collision plane in front of the player and enable his controls
	
	MQP02_SC1_PlayerCollisionMarker.Disable()
	Game.EnablePlayerControls()

EndFunction

Function StartActualCombat()
	
	MQP02_SebaldREF.Say(MQP02_D3_SebaldCombatCommentTopic)
	EnableNavcut()
	MQP02_SebaldREF.SetActorValue("Aggression", 2)
	MQP02_RiqRef.SetActorValue("Aggression", 2)
	MQP02_RiqRef.StartCombat(PlayerREF)
	MQP02_RiqRef.CreateDetectionEvent(PlayerREF, 100)
	MQP02_SiriusREF.StartCombat(MQP02_SebaldREF)
	MQP02_SebaldREF.StartCombat(MQP02_SiriusREF)
	_00E_MQP02_Tutorial_CombatBasic.ShowAsHelpMessage("Attack", 5, 1, 1)
	RiqHealthMax = MQP02_RiqRef.GetActorValue("Health")
	RegisterForSingleUpdate(1)
	GoToState("CombatTutorial")
	
EndFunction

Function StartSC04()

	DisableCombatNavcut()
	DisableNavcut()
	_00E_MQP02_CombatMusic.Remove()
	Game.RequestAutoSave()
	Wait(1.5)
	MQP02_SC4_VeiledWomanAppears.ForceStart()
	
EndFunction

Function EnableCombatNavcut()

	; Enables a navcut-plane to prevent the NPCs from following the player on the upper deck of the freight-room. Called from MQp02_SC4_CombatFailsaveBox
	
	MQP02_SC3_CombatNavcut.Enable()
	
EndFunction

Function DisableCombatNavcut()

	MQP02_SC3_CombatNavcut.Disable()
	
EndFunction

Function ShowTutorialMessage01()

	_00E_MQP02_Tutorial_CombatPowerAttack.ShowAsHelpMessage("HeavyAttack", 5, 1, 1)

EndFunction

Function ShowTutorialMessage02()
	
	_00E_MQP02_Tutorial_CombatDodge.ShowAsHelpMessage("Move", 5, 1, 1)

EndFunction

Function SetActivationRopeBool()

	If !bRopeActivated
		bRopeActivated = True
	EndIf

EndFunction

Function EnableVeiledWoman()

	If _00E_MC_VeiledWomanREF.IsDisabled()
		_00E_MC_VeiledWomanREF.Enable()
	EndIf
	
	_00E_MC_VeiledWomanREF.SetAlpha(0.1)
	_00E_MC_VeiledWomanREF.MoveTo(MQP02_SC4_VeiledWomanAppear)
	
EndFunction

Function OpenDoorAndStartMusic()

	_00E_QuestFunctions.PlayerAIWalk(True)
	MQP02_SC4_VeiledWomanFXBox.Enable()
	
	EffectShader _00E_FS_NQ02_VeiledWomanAppearFXS = Game.GetFormFromFile(0x0102ED3C, "Enderal - Forgotten Stories.esm") as EffectShader
	_00E_FS_NQ02_VeiledWomanAppearFXS.Play(_00E_MC_VeiledWomanREF)
	
	
	MQP02_SC4_JailDoor.Lock(False)
	MQP02_SC4_JailDoor.SetOpen(True)
	_00E_MC_VeiledWomanREF.SetAlpha(0.85)
	
	Sirius.GetActorReference().SetLookAt(VeiledWoman.GetReference())
	
	_00E_Music_Character_VeiledWomanT.Add()
	
	PlayerREF.KnockAreaEffect(0.6, 128)
		
EndFunction

Function LevitateAndCrashPlayer()
	
	MQP02_SiriusREF.SetHeadTracking(false)
	Game.ForceFirstPerson()
	Game.DisablePlayerControls(true, true, true, false, true, true, true, true)
	VeiledWomanSpellAMB =  QSTMG07MagnusStormCollegeSmallLPM.Play(PlayerREF)
	MQP02_SiriusREF.PlayIdle(IdleT02AscendMale)
	PlayerREF.SplineTranslateTo(PlayerREF.GetPositionX(), PlayerREF.GetPositionY(), 90.0, PlayerREF.GetAngleX(), PlayerREF.GetAngleY(), PlayerREF.GetAngleZ(), 5, 9)
	MQP02_SiriusREF.TranslateTo(MQP02_SiriusREF.GetPositionX(), MQP02_SiriusREF.GetPositionY(), 90.0, MQP02_SiriusREF.GetAngleX(), MQP02_SiriusREF.GetAngleY(), MQP02_SiriusREF.GetAngleZ(), 5, 9)
	Wait(1.5)
	bIsLevitating = True

EndFunction

Function DashPlayerTowardsWall()
	
	bTimeToScream = True
	Game.ShakeCamera()
	VeiledWomanSpellSound = _00E_MQP02_SpellThunderC.Play(PlayerREF)
	_00E_MQP02_CrashMotionBlur.Apply()
	PlayerREF.SplineTranslateTo(280.0 * Math.Sin(PlayerRef.GetAngleZ()), PlayerREF.GetPositionY(), PlayerREF.GetPositionZ() - 5.0, PlayerREF.GetAngleX(), PlayerREF.GetAngleY(), -90, 100, 2000)
	LevitatingCorrect = True

EndFunction

Function KnockOutPlayer()

	AudioCategoryAMB.Mute() 
	AudioCategoryAMB2x.Mute() 
	AudioCategoryAMBr.Mute() 
	_00E_Music_Character_VeiledWomanT.Remove()
	Sound.StopInstance(VeiledWomanSpellAMB)
	_00E_NPCKillBodyfall.Play(PlayerREF)
	_00E_P02_VeiledWomanSpellLoop.PopTo(FadeToBlackHoldImod)
	MQP02_SiriusREF.SetHeadTracking()
	Wait(0.8)
	Sound.StopInstance(VeiledWomanSpellSound)
	Wait(3)
	Game.PrecacheCharGenClear() 
	Self.SetStage(80) 
	
EndFunction

Function SebaldRoomFailsave()

	If MQP02_SebaldREF.GetDistance(MQP02_SC3_RiqSearchChestMarker) >= 300
		MQP02_SebaldREF.MoveTo(SebaldMoveToMarkerFinal)
	EndIf

EndFunction

Bool Function CheckLevitateState()

	If LevitatingCorrect == False
		Return False
	ElseIf LevitatingCorrect == True
		Return True
	EndIf
	
EndFunction

Function StartSC05()

	Levelsystem.SkipTimeToHour(8.25)
	PlayerREF.RestoreActorValue("Health", 100)
	Game.DisablePlayerControls(true, true, true, True, true, true, true, true)
	PlayerREF.MoveTo(MQP02_SC5_PlayerExecutionMarker)
	PlayerREF.PlayIdle(TG05_KnockOut)
	MQP02_SiriusREF.MoveTo(MQP02_SC5_SiriusKneelMarker)
	MQP02_SC5_RocioREF.MoveTo(MQP02_SC5_RocioStandMarker)
	MQP02_SiriusREF.PlayIdle(IdleForceDefaultState)
	AudioCategoryAMB.UnMute() 
	AudioCategoryAMB2x.UnMute() 
	AudioCategoryAMBr.UnMute() 
	Wait(1)
	
	Debug.SendAnimationEvent(MQP02_SiriusREF, "IdleBoundKneesEnterInstant")
	
	MQP02_SC5_RocioExecutesSirius.ForceStart()
	
	Wait(4)
	
	FadeToBlackHoldImod.PopTo(_00E_MQP02_HeadacheAnimated)

EndFunction

Function SiriusGetUp()

	Debug.SendAnimationEvent(Sirius.GetActorReference(), "IdleFurnitureExitSlow")
	
EndFunction

Function RocioKillSirius()

	Sirius.GetActorReference().GetActorBase().SetEssential(False)
	Rocio.GetActorReference().PlayIdleWithTarget(pa_1HMKillMoveShortB, Sirius.GetActorReference())
	Sirius.GetActorReference().Kill(Rocio.GetActorReference())
	Utility.Wait(1)
	Game.TriggerScreenBlood(7)
	MQP02_SC5_BloodDecal.Enable()

EndFunction

Function RocioKillPlayer()

	WPNSwingUnarmedM.Play(PlayerREF)
	Wait(0.7)
	Game.ShakeCamera(afStrength = 1)
	_00E_MQP02_MassiveClubImpact.Play(PlayerREF)
	_00E_MQP02_HeadacheKnockout.Apply()
	AudioCategoryAMB.Mute() 
	AudioCategoryAMB2x.Mute() 
	AudioCategoryAMBr.Mute() 
	Wait(0.7)
	_00E_MQP02_HeadacheKnockout.Remove()
	FadeToBlackHoldImod.Apply()
	Game.EnablePlayerControls()
	PlayerREF.PlayIdle(TG05_GetUp)
	Game.SetPlayerAIDriven(False)
	PlayerREF.SetAlpha(1)
	Wait(3)
	Game.RequestAutoSave()
	Self.SetStage(115)
	MQP03.Start()

EndFunction

Function RemoveChargenMusic()

	_00E_Music_Special_WiegeDesLebens.Remove()

EndFunction
;=====================================================================================
;              							EVENTS                 					 
;=====================================================================================

State Chargen

	Event OnUpdate()

		If !(Utility.IsInMenuMode())
			Self.SetStage(17)
			Game.EnablePlayerControls()
			PlayerREF.PlayIdle(IdleForceDefaultState)
			Debug.SendAnimationEvent(PlayerREF, "IdleForceDefaultState")
		Else
			RegisterForSingleUpdate(1)
		EndIf

	EndEvent

EndState

State CombatTutorial

	Event OnBeginState()

		RegisterForSingleUpdate(2)

	EndEvent

	Event OnUpdate()

		If Self.GetStage() < 60

			If !(MQP02_RiqRef.GetCombatTarget() == PlayerREF || MQP02_SiriusREF) 
				MQP02_RiqRef.StartCombat(PlayerREF)
			EndIf
		
			If !(MQP02_SebaldREF.GetCombatTarget() == PlayerREF || MQP02_SiriusREF)	
				MQP02_SebaldREF.StartCombat(MQP02_SiriusREF)
			EndIf
		
			If !(MQP02_SiriusREF.GetCombatTarget() == MQP02_SebaldREF || MQP02_RiqRef)
				MQP02_SiriusREF.StartCombat(MQP02_SebaldREF)
			EndIf
	
			If !bSiriusInCombatZone
				MQP02_SiriusREF.MoveTo(MQP02_SC03_SiriusCombatFailsaveMarker)
			EndIf
		
			RegisterForSingleUpdate(1)

		Else
			; Do nothing
		EndIf
		
	EndEvent

EndState


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

_00E_QuestFunctions Property Levelsystem Auto

bool Property bRopeActivated Auto Conditional Hidden
bool Property bIsLevitating Auto Conditional Hidden
bool Property bTimeToScream Auto Conditional Hidden
bool Property bSiriusInCombatZone Auto Conditional Hidden

SoundCategory Property AudioCategoryAMB Auto
SoundCategory Property AudioCategoryAMB2x Auto
SoundCategory Property AudioCategoryAMBr Auto

Quest Property MQP03 Auto

Actor Property MQP02_RiqRef Auto 
Actor Property MQP02_SiriusREF Auto 
Actor Property MQP02_SebaldREF Auto 
Actor Property _00E_MC_VeiledWomanREF Auto 
Actor Property MQP02_SC5_RocioREF Auto 

ActorBase Property Player Auto

Bool LevitatingCorrect = False
Float RiqHealthMax 
Int CombatTutorialController
Int VeiledWomanSpellAMB
Int VeiledWomanSpellSound

MusicType Property _00E_MQP02_CombatMusic Auto
MusicType Property _00E_Music_Character_VeiledWomanT Auto
MusicType Property _00E_Music_Special_WiegeDesLebens Auto

Topic Property MQP02_D3_SebaldCombatCommentTopic Auto

GlobalVariable Property _00E_FS_IsForgottenStoriesActivated Auto

Actor Property PlayerREF Auto

Idle Property TG05_GetUp Auto
Idle Property TG05_KnockOut Auto
Idle Property IdleT02AscendMale Auto
Idle Property IdleForceDefaultState Auto
Idle Property pa_1HMKillMoveShortB Auto

Scene Property MQP02_SC1_PlayerWaksUp Auto
Scene Property MQP02_SC2_RiqAndSebald01 Auto
Scene Property MQP02_SC3_RiqAndSebald02 Auto
Scene Property MQP02_SC4_VeiledWomanAppears Auto
Scene Property MQP02_SC5_RocioExecutesSirius Auto

Message Property _00E_MQP02_Tutorial_Activate Auto
Message Property _00E_MQP02_Tutorial_CombatBasic Auto
Message Property _00E_MQP02_Tutorial_CombatDodge Auto
Message Property _00E_MQP02_Tutorial_CombatPowerAttack Auto

ImageSpaceModifier Property _00E_WakeUp Auto
ImageSpaceModifier Property FadeToBlackHOLDIMod Auto
ImageSpaceModifier Property _00E_P02_VeiledWomanSpellLoop Auto
ImageSpaceModifier Property _00E_MQP02_HeadacheKnockout Auto
ImageSpaceModifier Property _00E_MQP02_CrashMotionBlur Auto
ImageSpaceModifier Property _00E_MQP02_HeadacheAnimated Auto

Sound Property QSTMG07MagnusStormCollegeSmallLPM Auto
Sound Property _00E_MQP02_SpellThunderC Auto
Sound Property _00E_NPCKillBodyfall Auto
Sound Property _00E_MQP02_MassiveClubImpact Auto
Sound Property WPNSwingUnarmedM Auto

Race Property BretonRace Auto
Race Property HighElfRace Auto
Race Property NordRace Auto

ObjectReference Property MQP02_SC5_BloodDecal Auto
ObjectReference Property MQP02_SC2_SebaldStartMarker Auto
ObjectReference Property MQP02_SC2_RiqStartMarker Auto
ObjectReference Property MQP02_SC1_PlayerCollisionMarker Auto
ObjectReference Property MQP02_SC4_VeiledWomanAppear Auto
ObjectReference Property MQP02_SC4_JailDoor Auto
ObjectReference Property MQP02_SC4_VeiledWomanFXBox Auto
ObjectReference Property MQP02_SC3_Navcut Auto
ObjectReference Property MQP02_SC3_CombatNavcut Auto
ObjectReference Property MQP02_SC5_PlayerExecutionMarker Auto
ObjectReference Property MQP02_SC5_RocioStandMarker Auto
ObjectReference Property MQP02_SC5_SiriusKneelMarker Auto
ObjectReference Property MQP02_SC03_SiriusCombatFailsaveMarker Auto
ObjectReference Property MQP02_SC3_RiqSearchChestMarker Auto
ObjectReference Property SebaldMoveToMarkerFinal Auto
ObjectReference Property MQP02_D0_PlayerStartMarker Auto

VisualEffect Property FXCameraAttachEmbersHeavyEffect Auto
VisualEffect Property FXCameraAttachEmbersEffect Auto

ReferenceAlias Property Sirius Auto
ReferenceAlias Property PlayerStartMarker Auto
ReferenceAlias Property Riq Auto
ReferenceAlias Property Sebald Auto
ReferenceAlias Property FreightRoomDoor Auto
ReferenceAlias Property VeiledWoman Auto
ReferenceAlias Property Rocio Auto

Armor Property ClothesPrisonerRags Auto