scriptname _00E_MQ08_Questfunctions extends Quest Conditional

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

Function JesparGivePresent()

   PlayerREF.AddItem(_00E_Lehrbuch_Plus2Skillpoints, 1)

EndFunction

Function PortFollowerToNebelhaim()

    _00E_PlayerFunctions.GetSoundControl().RemoveSilence()
    _00E_MC_JesparREF.MoveTo(MQ08PortMarker)
    
    If _00E_MC_JesparREF.IsDisabled()
        _00E_MC_JesparREF.Enable()
    EndIf

    _00E_MC_JesparREF.EvaluatePackage()

EndFunction

Function TeleportJesparToTemple()

    if _00E_MC_JesparREF.GetDistance(PlayerREF) >= 800
    
        _00E_MC_JesparREF.MoveTo(MQ09a_JesparTemplePortMarkerREF)
        
    EndIf
    
EndFunction

Function PortKontantinToNebelaim()

    _00E_MC_KonstantinREF.MoveTO(MQ08PortMarker)
    
    _00E_MC_JesparREF.EvaluatePackage()
    _00E_MC_KonstantinREF.EvaluatePackage()
	
EndFunction

Function JesparAndConstantineNebelhaimFailsave()

    _00E_MC_JesparREF.MoveTo(MQ08PortMarker)
    _00E_MC_KonstantinREF.MoveTO(MQ08PortMarker)
    
EndFunction


Function EnableFollower()

    MQ09a_FollowStartBoxREF.Enable()
	_00E_Func_SetNPCAsCompanion.SetNPCAsCompanion(_00E_MC_JesparREF, True, 350, 400, 100)
	
EndFunction

Function FollowerStopBeforeTemple()

	_00E_Func_SetNPCAsCompanion.SetNPCAsCompanion(_00E_MC_JesparREF, False)
	
EndFunction

Function OpenDoor()

    MQ09aDoorRathaus.Lock(False)
    MQ09aDoorRathaus.BlockActivation(False)

EndFunction

Function KonstantinMadnessSceneFailsave()

    _00E_MC_KonstantinREF.Reset()
    
    _00E_MC_KonstantinREF.MoveTo(MQ08_KonstantinPortMarker02REF)
    
    MQ08_IntoTheDeep_SceneTempleOutside.Stop()
    
    If !MQ08_IntoTheDeep_SceneTempleInside.IsPlaying()
    
        MQ08_IntoTheDeep_SceneTempleInside.ForceStart()
        
    EndIf

EndFunction

Function PortKonstantin()

    _00E_MC_KonstantinREF.MoveTo(MQ08PortKonstantinMarker)
    
EndFunction

Function AddMadnessMusic()

    _00E_Music_Special_Event_Dark.Add()
    _00E_SilenceTransitionHighPriority.Add()

EndFunction

Function JesparEVP()

    Utility.Wait(2.0)
    _00E_MC_JesparREF.EvaluatePackage()
	
EndFunction

Function GoToTrain()

    Game.DisablePlayerControls()
    Game.FadeOutGame(true, true, 1, 4)
    Utility.Wait(2)
    FadeToBlackHoldImod.Apply()
    MQ08TombKonstantin.Enable()
    _00E_MC_KonstantinREF.Disable()
    PlayerREF.MoveTo(MQ08PlayerSpawnTomb)
    Utility.Wait(2)
    
    _00E_MC_JesparREF.Disable()
    _00E_MC_JesparREF.MoveTo(MQ08JesparSpawnTomb)
    _00E_MC_JesparREF.Enable()

    Utility.Wait(3)
    FadeToBlackBackImod.Apply()
    FadeToBlackHoldImod.Remove()
    Utility.Wait(3)
    Game.EnablePlayerControls()
	
EndFunction

Function SetJesparCombatGlobal(int iValue)

    JesparNoCombatComments.SetValue(iValue)

EndFunction

Function AddMusic()

    _00E_Music_Special_Character_Jespar_Long.Add()

EndFunction

Function RemoveMusic()

    _00E_Music_Special_Character_Jespar_Long.Remove()

EndFunction

function PlayerAIWalk(bool abLockedSight = True)

	if abLockedSight == True
		Game.SetPlayerAIDriven(True)
		Game.DisablePlayerControls(true, true, true, true, true, true, true, true)
		NewPlayerRef.ApplyHavokImpulse(1.0, 1.0, 0.0, 25.0)
	ElseIf abLockedSight == False
		Game.SetPlayerAIDriven(True)
		Game.DisablePlayerControls(true, true, true, False, true, true, true, true)
	EndIf
	
endFunction

function PlayerStopAIWalk()

    Game.SetPlayerAIDriven(false)
    Game.EnablePlayerControls(true, true, true, true, true, true, true, true)
	
endFunction

Function KonstantinStartCombat()

	_00E_Func_SetNPCAsCompanion.SetNPCAsCompanion(_00E_MC_JesparREF, True, 350, 400, 100)
	;We also need to change Constanine's stats, since they were adjusted upon entering the Living Temple
	_00E_MC_KonstantinREF.SetActorValue("Health", 850)
	_00E_MC_KonstantinREF.SetActorValue("Stamina", 850)
	_00E_MC_KonstantinREF.SetActorValue("Magicka", 240)
		
	_00E_SilenceTransitionHighPriority.Remove()
    _00E_Music_Special_Event_Dark.Remove()
    _00E_Music_Combat_Epic.Add()
    Game.EnablePlayerControls()
    Utility.Wait(0.5)
    _00E_MC_KonstantinREF.SetGhost(False)
    _00E_MC_KonstantinREF.SetActorValue("Aggression", 3)
    _00E_MC_KonstantinREF.StartCombat(PlayerREF)
    _00E_MC_JesparREF.StartCombat(_00E_MC_KonstantinREF)

EndFunction

Function KillKonstantin()

    _00E_MC_KonstantinREF.GetActorBase().SetEssential(False)
    _00E_MC_KonstantinREF.Kill(PlayerREF)
	
	_00E_Func_SetNPCAsCompanion.SetNPCAsCompanion(_00E_MC_JesparREF, False)
	
	SetCurrentStageID(155)

EndFunction

Function RemoveCombatMusic()

    _00E_Music_Combat_Epic.Remove()

EndFunction

Function HouseSceneDisableControls()

    Game.DisablePlayerControls()

EndFunction

Function StartSC06()
    
    Game.EnablePlayerControls()

    MQ08_IntoTheDeep_GoToTrain.Stop()
    MQ08_IntoTheDeep_GoToTrain2.ForceStart()
    
    If !MQ08_IntoTheDeep_GoToTrain2.IsPlaying()
        MQ08_IntoTheDeep_GoToTrain2 = Game.GetForm(0x000E1A26) as Scene
        MQ08_IntoTheDeep_GoToTrain2.ForceStart()
    EndIf

EndFunction

Function StartSC06_Failsave()

    If !MQ08_IntoTheDeep_GoToTrain2.IsPlaying()
        MQ08_IntoTheDeep_GoToTrain2.ForceStart()
    EndIf

EndFunction

Function StartSC07()

    MQ08_IntoTheDeep_GoToTrain3.ForceStart()

    If !MQ08_IntoTheDeep_GoToTrain3.IsPlaying()
        MQ08_IntoTheDeep_GoToTrain3 = Game.GetForm(0x000E1A12) as Scene
        MQ08_IntoTheDeep_GoToTrain3.ForceStart()
    EndIf
    
EndFunction

Function CreatePlayerCopy()

    NewPlayer = MQ08PlayerCloneMarker.PlaceActorAtMe(Player)
    NewPlayer.RemoveAllItems()
    NewPlayerRef = NewPlayer as Actor
    NewPlayer.Kill(PlayerREF)
    NewPlayerRef.RemoveAllItems()
    NewPlayerRef.ApplyHavokImpulse(1.0, 0.5, 0.5, 350.0)
    NewPlayerRef.SetOutfit(PrisonerPantsOutfit)
    
EndFunction

Function PlayStartSound()

    _00E_MQ09a_RailwayStartM.Play(PlayerREF)

EndFunction

Function JesparMoveFailsave()

    _00E_MC_JesparREF.MoveTo(MQ09a_SC06_JesparFailsaveMarkerREF)

EndFunction

Function AnotherTrainFailsave()

    ; Prevent the player from leaving the train, but makes Jespar
    ; enter it.

    if _00E_MC_JesparREF.GetParentCell() != PlayerREF.GetParentCell()
        if MQ09a_SC06_JesparFailsaveMarkerREF == None
            MQ09a_SC06_JesparFailsaveMarkerREF = Game.GetForm(0x00153B4F) as ObjectReference
        endif
        _00E_MC_JesparREF.MoveTo(MQ09a_SC06_JesparFailsaveMarkerREF)
    endif
    MQ08TrainDoorRef.SetLockLevel(255)
    MQ08TrainDoorRef.Lock()
    KillTrainStationEnemies()

EndFunction

Function StartTrainScene()

    MQ08_IntoTheDeep_GoToTrain4.ForceStart()
        
    If !MQ08_IntoTheDeep_GoToTrain4.IsPlaying()
        MQ08_IntoTheDeep_GoToTrain4 = Game.GetForm(0x000EA944) as Scene
        MQ08_IntoTheDeep_GoToTrain4.ForceStart()
    EndIf

    MQ08TrainDoorRef.BlockActivation(True)
    MQ09StaticDoor.enable()
    ; Give some time for MQ09StaticDoor to appear
    While MQ09StaticDoor.Is3DLoaded() == False
    	; Wait...
    EndWhile
    MQ08TrainDoorRef.Disable()
    If MQ08DwemerSound ; Gavrant: don't know why, but MQ08DwemerSound is currently NONE in the properties
        MQ08DwemerSound.enable()
    EndIf
    iTrainSound = _00E_MQ09a_RailwayLPM.Play(PlayerREF)
    
    While !Done 
        Game.ShakeCamera(PlayerREF, Utility.RandomFloat(0.05, 0.1), 3)
        Utility.Wait(3)
        if GetCurrentStageID() >= 220
            Done = True
        EndIf
    EndWhile
    
	;Deleting the new player. Otherwise certain events in scripts attached to the ActorBase Player get triggered a second time (e.g. OnActorAction in _00E_Game_TalentControlSC)
	NewPlayer.Delete()
	
EndFunction

Function CheckTrainScene()

    ; Failsave. Not sure if this works.
    If !MQ08_IntoTheDeep_GoToTrain4.IsPlaying()
        MQ08_IntoTheDeep_GoToTrain4 = Game.GetForm(0x000EA944) as Scene
        MQ08_IntoTheDeep_GoToTrain4.ForceStart()
    EndIf

EndFunction

Function RefreshCombatState()

    While _00E_MC_KonstantinREF.IsDead() == 0 || GetCurrentStageID() >= 156
        _00E_MC_KonstantinREF.StartCombat(_00E_MC_JesparREF)
        Utility.Wait(4)
    EndWhile
    
EndFunction

Function RemoveSoundtrack()

    _00E_Music_Combat_Epic.Remove()
	
EndFunction

Function OpenGate()

    MQ09a_SC07_RailwayGateREF.TranslateTo(MQ09a_SC07_RailwayGateREF.GetPositionX(), MQ09a_SC07_RailwayGateREF.GetPositionY(), (MQ09a_SC07_RailwayGateREF.GetPositionZ() + 250), 0, 0, 0, 100.0)
    _00E_DRSElevatorM.Play(MQ09a_SC07_RailwayGateREF)

EndFunction

Function KillTrainStationEnemies()

    ; Kill all skeleta in cell MQ08TempleUnderground. This is to avoid
    ; Jespar running out of the train.
    int iIndex = _00E_MQ09a_TrainStationEnemies.GetSize()
    while iIndex > 0
        iIndex -= 1
        Actor akKillMe = _00E_MQ09a_TrainStationEnemies.getAt(iIndex) as Actor
        if !akKillMe.IsDead()
            akKillMe.kill()
        endif
    endwhile

EndFunction

Function FadeToBlack()
    FadeToBlackImod.Apply()
    Utility.Wait(2)
    FadeToBlackHoldImod.Apply()
    FadeToBlackImod.Remove()
EndFunction

Function PrepareOstianTalk()

    _00E_Func_AIWalk.PlayerAIWalk()
    MQ09a_SC08_PlayerBenchMarker = Game.GetForm(0x00150B74) as ObjectReference
    PlayerREF.MoveTo(MQ09a_SC08_PlayerBenchMarker)

EndFunction

Function FadeBack()

    JesparNoCombatComments.SetValueInt(0)
    
    If MQ09a_SC08_JesparLoungeMarker == None
        MQ09a_SC08_JesparLoungeMarker = Game.GetForm(0x00150B73) as ObjectReference
    EndIf
    
    _00E_MC_JesparREF.MoveTo(MQ09a_SC08_JesparLoungeMarker)
    
    Utility.Wait(2)
    FadeToBlackBackImod.Apply()
    FadeToBlackHoldImod.Remove()

EndFunction

Function StopTrainSound()

    Sound.StopInstance(iTrainSound)

EndFunction

Function JesparResetCombatComments()

    JesparNoCombatComments.SetValueInt(0)

EndFunction

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

bool Done = false;

bool Property bAvoidedQuestion Auto Conditional Hidden

int iTrainSound

Actor NewPlayerRef
Actor NewPlayer

Potion Property _00E_Lehrbuch_Plus2Skillpoints Auto

ActorBase Property Player Auto

Actor Property _00E_MC_JesparREF Auto
Actor Property _00E_MC_KonstantinREF Auto
Actor Property PlayerREF Auto

FormList Property _00E_MQ09a_TrainStationEnemies Auto

ObjectReference Property MQ08PortMarker Auto
ObjectReference Property MQ08PortKonstantinMarker Auto
ObjectReference Property MQ08TombKonstantin Auto
ObjectReference Property MQ08PlayerSpawnTomb Auto
ObjectReference Property MQ08JesparSpawnTomb Auto
ObjectReference Property MQ08PlayerCloneMarker Auto
ObjectReference Property MQ08DwemerSound Auto
ObjectReference Property MQ09a_SC07_RailwayGateREF Auto
ObjectReference Property MQ09aDoorRathaus Auto
ObjectReference Property MQ09a_SC08_JesparLoungeMarker Auto
ObjectReference Property MQ09a_SC08_PlayerBenchMarker Auto
ObjectReference Property MQ09a_SC06_JesparFailsaveMarkerREF Auto
ObjectReference Property MQ08TrainDoorRef Auto 
ObjectReference Property MQ09StaticDoor Auto
ObjectReference Property MQ09a_FollowStartBoxREF Auto
ObjectReference Property MQ09a_JesparTemplePortMarkerREF Auto
ObjectReference Property MQ08_KonstantinPortMarker02REF Auto

GlobalVariable Property JesparNoCombatComments Auto

Sound Property _00E_DRSElevatorM Auto
Sound Property _00E_MQ09a_RailwayLPM Auto
Sound Property _00E_MQ09a_RailwayStartM Auto

Scene Property MQ08_IntoTheDeep_GoToTrain Auto
Scene Property MQ08_IntoTheDeep_GoToTrain2 Auto
Scene Property MQ08_IntoTheDeep_GoToTrain3 Auto
Scene Property MQ08_IntoTheDeep_GoToTrain4 Auto
Scene Property MQ08_IntoTheDeep_SceneTempleInside Auto
Scene Property MQ08_IntoTheDeep_SceneTempleOutside Auto

Outfit Property PrisonerPantsOutfit Auto

MusicType Property _00E_Music_Special_Event_Dark Auto
MusicType Property _00E_Music_Combat_Epic Auto
MusicType Property _00E_SilenceTransitionHighPriority Auto
MusicType Property _00E_Music_Special_Character_Jespar_Long Auto

ImageSpaceModifier Property FadeToBlackImod Auto
ImageSpaceModifier Property FadeToBlackHoldImod Auto
ImageSpaceModifier Property FadeToBlackBackImod Auto