Scriptname _00E_MQ11a_Functions extends Quest Conditional

Import Utility
Import Game
Import Debug
Import _00E_QuestFunctions

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

State DefaultState

Event OnUpdateGameTime()

    If bStartCaliaCQ

        If !CQC03.IsCompleted() && CQC03.IsRunning()
            CQC03.SetStage(20)
            CQC03.FailAllObjectives()
        Elseif CQC03.IsCompleted()
            CQC04.SetStage(5)
        EndIf
    
        bStartCaliaCQ = False
    
    EndIf
    
EndEvent

Event OnUpdate()

    If PlayerREF.GetParentCell() == MQ11CaliaCastleMain

		_00E_MC_CaliaREF.Say(MQ11a_D14_Remark_SigilsTopic)
		WaitForCompanionToStopTalking(6.0)
        CompanionIsTalking.SetValueInt(0)
        
    EndIf
	
	If PlayerREF.GetParentCell() == MQ11DR
	
		_00E_MC_CaliaREF.StopCombat()
		_00E_MC_CaliaREF.StopCombatAlarm()
	
	EndIf

    if GetStage() == 95 && (MQ11a_TravelState02.GetValueInt() > 0)
    
        if !PlayerREF.IsInCombat()
            SetStage(100)
        Else
            RegisterForSingleUpdate(15)
        EndIf
        
    EndIf
    
    If MQ11a_SC14_LeftTowerVision02.IsPlaying()
        StopMayaVision()
    EndIf

EndEvent

EndState

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

Function CaliaAppears()

    If !CQC03.IsCompleted() && CQC03.IsRunning()
        CQC03.SetStage(20)
        CQC03.FailAllObjectives()
    EndIf
    
    _00E_MC_CaliaREF.SetOutfit(_00E_MC_Calia_Outfit)
    
    If _00E_MC_CaliaREF.IsDisabled()
        _00E_MC_CaliaREF.Enable()
    EndIf
    _00E_MC_CaliaREF.MoveTo(MQ11a_CaliaAppearMarker)
    MQ11a_SC1_CaliaTalksToPlayer.ForceStart()
    
EndFunction

Function _SafeMoveNPCtoFurniture(Actor akNPC, ObjectReference refFurniture)
	akNPC.Disable()
	akNPC.MoveTo(refFurniture)
	akNPC.Enable()
EndFunction

Function MoveCalia()

    _00E_MC_CaliaREF.SetOutfit(_00E_MC_Calia_Outfit)

    _00E_MC_CaliaREF.MoveTo(MQ11a_SC2_CaliaStartMarker)
    Debug.SendAnimationEvent(MQ11a_SC02_Guest_02, "IdleForceDefaultState")
    _SafeMoveNPCtoFurniture(MQ11a_SC02_Guest_01, MQ11a_SC02_Guest_01_Chair)
    _SafeMoveNPCtoFurniture(MQ11a_SC02_Guest_02, MQ11a_SC02_Guest_02_Chair)
    _SafeMoveNPCtoFurniture(_00E_MC_CaliaREF, MQ11a_SC2_CaliaWallMarker)
    MQ11a_SC2_CaliaWallMarker = None ; Don't need it anymore
    _00E_MC_CaliaREF.SetOutfit(_00E_MC_Calia_Outfit)
    MQ11a_SC02_CaliaFrostcliffTavern.ForceStart()

EndFunction

Function MoveGuestsFailsave()

    MQ11a_SC02_Guest_01.MoveTo(MQ11a_SC02_Guest_01_Chair)
    MQ11a_SC02_Guest_02.MoveTo(MQ11a_SC02_Guest_02_Chair)

EndFunction

Function CaliaConversationScene()

    MQ11a_SC3_CaliaFrostCliffLoop.ForceStart()

EndFunction

Function CheckSpokenWith()

    Spokenwith += 1

    if Spokenwith < 3
        _00E_MQ11a_TavernGuestsTalkedTo.SetValue(_00E_MQ11a_TavernGuestsTalkedTo.GetValue() + 1)
        Self.UpdateCurrentInstanceGlobal(_00E_MQ11a_TavernGuestsTalkedTo)
        Self.SetObjectiveCompleted(15, 0)
        Self.SetObjectiveDisplayed(15, True, True)
    Elseif Spokenwith == 3
        _00E_MQ11a_TavernGuestsTalkedTo.SetValue(_00E_MQ11a_TavernGuestsTalkedTo.GetValue() + 1)
        Self.UpdateCurrentInstanceGlobal(_00E_MQ11a_TavernGuestsTalkedTo)
        Self.SetObjectiveCompleted(15)
        Self.SetStage(37)
    EndIf

    Levelsystem.GiveEP(__Config_RewardEXPSpeakWithPeople)

EndFunction

Function StartJourneyScene()
	
	NQ34_EnableMarkerREF.Enable()
    MQ11a_Nehrimese_Patrol_Linker.Enable()
    MQ11a_CaliaFollowtrigger_StartBoxREF.Enable()
    CaliaAddTorch()
    _00E_MC_CaliaREF.EvaluatePackage()
    SetCaliaAsCompanion(True)

EndFunction

;=====================================SCENE6================================================

Function StartCastleGateScene()

    If _00E_MC_CaliaREF.GetDistance(PlayerREF) >= 1500
        ObjectReference objSpawnMarker = PlayerREF.PlaceAtMe(XMarkerHeading, 1)
        objSpawnMarker.MoveTo(PlayerRef, -250.0 * Math.Sin(PlayerRef.GetAngleZ()), -250.0 * Math.Cos(PlayerRef.GetAngleZ()), PlayerRef.GetHeight())
        _00E_MC_CaliaREF.MoveTo(objSpawnMarker)
        objSpawnMarker.Delete()
    EndIf
	
    SetCaliaAsCompanion(False)
    MQ11a_SC6_CaliaAtGate.ForceStart()
    
EndFunction

;=====================================SCENE7================================================

Function SetUpExplosion()

    If _00E_MC_CaliaREF.IsDisabled()
        _00E_MC_CaliaREF.Enable()
    EndIf
    
    CompanionIsTalking.SetValueInt(0)
    _00E_SilenceTransitionHighPriority.Add()
    Levelsystem.FadeToBlack()
    Levelsystem.SkipTimeToHour(22)
    MQ11a_SC6_SnowdriftParent.Disable()
    MQ11a_SC6_ParentBarrel.Enable()
    DisableWolfPack()       
    DisablePlayerControls()
    PlayerREF.MoveTo(MQ11a_SC6_PlayerExplodeMarker)
    _00E_MC_CaliaREF.MoveTo(MQ11a_SC6_CaliaBarrelMarker)
    CaliaEquipTorch(True)
    Wait(2)
    MQ11a_SC7_CaliaExplodeBarrels.ForceStart()
    
EndFunction

Function DisableWolfPack()          ; Disable Wolf pack if still alive, to make sure they don't interrupt the scene

    Form[] wolfRefs = MQ11a_SC6_WolfPackGates.ToArray()
    Int iIndex = wolfRefs.Length
    While iIndex > 0
        iIndex -= 1
        Actor WolfToDisable = wolfRefs[iIndex] as Actor
        If WolfToDisable.IsDead() == False
            WolfToDisable.DisableNoWait()
        EndIf
    EndWhile

EndFunction

Function FadeBack()

    FadeToBlackBackImod.Apply()
    FadeToBlackHoldImod.Remove()

EndFunction

Function CaliaIgniteOil()

    MQ11a_SC6_FirstOilPool.DamageObject(100)
    _00E_MC_CaliaREF.SetLookAt(MQ11a_SC7_CastleDoor, True)
    
EndFunction

Function CaliaReturnToDefault()

    SendAnimationEvent(_00E_MC_CaliaREF, "IdleForceDefaultState")

EndFunction

Function BlowUpBarrels()

    MQ11a_SC6_ParentBarrel.PlaceAtMe(_00E_MQ11a_SC7_BarrelExplosionVFX01)
    MQ11a_SC6_ParentBarrel.PlaceAtMe(_00E_MQ11a_SC7_BarrelExplosionVFX02)
    FXExplosionCatapultNearM.Play(MQ11a_SC6_ParentBarrel)
    MQ11a_SC6_ParentBarrel.DisableNoWait()
    Game.ShakeCamera()
    MAGFireBallExpImod.Apply()
    MQ11a_SC7_CastleDoor.PlayGamebryoAnimation("Open")
    MQ11a_SC7_DebrisParent.Enable()
    Wait(3)
    _00E_MC_CaliaREF.ClearLookAt()
    Self.SetStage(75)
    Wait(2)
    EnablePlayerControls()

EndFunction

Function CaliaCollapse()

    _00E_SilenceTransitionHighPriority.Remove()
    _00E_MC_CaliaREF.DamageActorValue("health", 1000)
     CaliaTransformSound = _00E_MC_Calia_ItTransformLPDM.Play(_00E_MC_CaliaREF)
    _00E_MC_CaliaItFXS.Play(_00E_MC_CaliaREF)
    _00E_MC_CaliaREF.SetEyeTexture(_00E_MC_CaliaEyesIt)

EndFunction

Function CaliaGetUp()

    _00E_MC_CaliaREF.SetEyeTexture(EyesMaleHumanHazelBrown)
    _00E_MC_CaliaItFXS.Stop(_00E_MC_CaliaREF)
    _00E_MC_CaliaREF.RestoreActorValue("health", 450)
    Sound.StopInstance(CaliaTransformSound)

EndFunction

Function StartFirstVisionScene()

    int iIndex = _00E_MQ11a_SC8_VisionRefs.GetSize()

    while iIndex > 0
        iIndex -= 1
        Actor VisionREF = _00E_MQ11a_SC8_VisionRefs.GetAt(iIndex) as Actor
        if VisionREF.IsDisabled()
            VisionREF.Enable()
        EndIf
        VisionREF.SetAlpha(0)
    EndWhile

    Game.DisablePlayerControls()
    
    If _00E_MC_CaliaREF.GetDistance(PlayerREF) >= 800
        _00E_MC_CaliaREF.MoveTo(MQ11a_SC8_CaliaFailsaveMarkerREF)
    EndIf
    
    MQ11a_SC8_FirstVision.ForceStart()

EndFunction

Function StartVisionFX()

    _00E_MQ11a_SkyrimOvercastSnowSlow.ForceActive(True)
    MQ11a_DalGalarREF.MoveTo(MQ11a_SC8_DalGalarTalkMarker)
    MQ11a_DalGalarREF.SetAlpha(0.0)
	MQ11a_DiscipleREF.MoveTo(Mq11a_SC8_DiscipleTalkMarker)
    MQ11a_DalGalarREF.SetAlpha(0.0)
	Levelsystem.VisionEffectNoTimestop(_00E_MQ11a_SC8_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
    
EndFunction

Function StopVisionFX()

    _00E_SilenceTransitionHighPriority.Add()
    Weather.ReleaseOverride()
    SkyrimOvercastSnow.ForceActive()
    Levelsystem.VisionEffectNoTimestopStop(_00E_MQ11a_SC8_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
    MQ11a_DiscipleREF.MoveTo(MQ11a_DiscipleOrigin)
    MQ11a_DalGalarREF.MoveTo(MQ11a_DalGalarOrigin)
    
EndFunction

Function StartCastleCourtyardFollow()

    MQ11a_SC07_FollowStartBox.Enable()
    _00E_MC_CaliaREF.EvaluatePackage()

EndFunction

Function PlayScream()

    _00E_MQ11a_MagicAnomalyScreamAM.Play(Mq11a_SC8_CorpseExplosion)

EndFunction

Function ReanimateCorpses()

	SwitchAllTeleportation(False)
    MQ11a_LeftTowerDoorREF.BlockActivation(True)
    MQ11a_RightTowerDoorREF.BlockActivation(True)
    Game.RequestAutoSave()
    SetCaliaAsCompanion(True)
    _00E_SilenceTransitionHighPriority.Remove()
    _00E_Music_Combat_ShieldbrothersNoCond.Add()
    _00E_MC_CaliaREF.SetLookAt(Mq11a_SC8_CorpseExplosion)
    Mq11a_SC8_CorpseExplosion.PlaceAtMe(_00E_MQ11a_MagAnExpl)
    Wait(2.1)

    StartTrackingFight(FIGHT_ID_SC08, 100, _00E_MQ11a_SC8_ReanimationList, MQ11a_CL_Location, MQ11a_SC08_Zombie, False)
    RaiseDeadServants(_00E_MQ11a_SC8_ReanimationList, Mq11a_SC8_CorpseExplosion)
    
    RegisterForSingleUpdate(45)

    _00E_MC_CaliaREF.ClearLookAt()
    
EndFunction

Function StopMusic()

    CompanionIsTalking.SetValueInt(0)
    MQ11a_SC8_KeyRef.Enable()
    _00E_Music_Combat_ShieldbrothersNoCond.Remove()
    SwitchAllTeleportation(True)

EndFunction

Function UnblockTowerDoors()

    MQ11a_LeftTowerDoorREF.BlockActivation(False)
    MQ11a_RightTowerDoorREF.BlockActivation(False)
    SetCaliaAsCompanion(True)

EndFunction

Function CaliaRemark()

    CaliaEquipTorch(True)
    If MQ11a_TravelState02.GetValue() <= 0
        MQ11a_TravelState02.Mod(1)
    EndIf
    Levelsystem.SkipTimeToHour(0)
    If _00E_MC_CaliaREF.IsDisabled()
        _00E_MC_CaliaREF.Enable()
    EndIf
    _00E_MC_CaliaREF.MoveTo(playerREF)
    _00E_MC_CaliaREF.Say(MQ11a_D6_JourneyRemarkTopic)

EndFunction

bool Function ShowTowerRiddleBox(String sCodeWord)

    String sRiddleInput = (((Self as QUEST) as FORM) as UILIB_1).ShowTextInput("$Say the password:")

    if sRiddleInput == sCodeWord
        Return True
    Else
        Return False
    EndIf

EndFunction

;=====================================SCENE10================================================

Function SpawnAnomaly()

    CaliaNoCombatStateChangeComments.SetValueInt(1)
    MagicalAnomaly.ForceRefTo(MQ11a_AnomalyREF)
    MQ11a_AnomalyREF.Enable()
    Game.ShakeCamera()
    ObjectReference XPlosionMarker = MQ11a_AnomalyREF.PlaceAtMe(XMarker, 1)
    XPlosionMarker.MoveTo(MQ11a_AnomalyREF, 0.0, 0.0, 100)
    XPlosionMarker.PlaceAtMe(_00E_MQ11a_AnomalySpawnExplosion, 1)

    _00E_QuestFunctions.WaitForReferenceToLoad(MQ11a_AnomalyREF)
    int ScreamSound = _00E_NPCMagicAnomalyScreamM.Play(MQ11a_AnomalyREF)
    Sound.SetInstanceVolume(ScreamSound, 3.0)   
    
EndFunction

;=====================================SCENE11================================================

Function PrepareForLeftTowerVision()
    SetCaliaAsCompanion(False)
    SwitchCaliaNormalDialogue(False)
    ; Lock the player in the left tower until the puzzle is solved.
    ; This should prevent the scene from stopping prematurely
    ; ( http://sureai.net:9898/browse/ERB-1238 ).
    MQ11a_LeftTowerDoorOut.SetLockLevel(255)
    MQ11a_LeftTowerDoorOut.Lock()
    _00E_TeleportGlobal.SetvalueInt(1)
    ; A few failsaves that *really* shouldn't be necessary. But one
    ; aspect of defensive programming for Skyrim is that every time
    ; you lock a door, you should ensure that the player and other
    ; relevant NPCs are on the right side of it.
    If PlayerREF.GetParentCell() != MQ11a_SC11_DalGalarStart.GetParentCell()
        PlayerREF.MoveTo(MQ11a_SC11_DalGalarStart)
    EndIf
    If _00E_MC_CaliaREF.GetParentCell() != MQ11a_SC11_DalGalarStart.GetParentCell()
        _00E_MC_CaliaREF.MoveTo(MQ11a_SC11_DalGalarStart)
    EndIf
    Levelsystem.StopRandomHeadTracking()

EndFunction

Function StartLeftTowerVision01()

    If MQ11a_SC11_XMarkerGoblets == None
        MQ11a_SC11_XMarkerGoblets = (Game.GetFormFromFile(0x000EA843, "Skyrim.esm") as ObjectReference)
    EndIf
    CompanionIsTalking.SetValueInt(0)    
    MQ11a_DalGalarREF.MoveTo(MQ11a_SC11_DalGalarStart)
    MQ11a_DalGalarREF.SetAlpha(0.0)
    MQ11a_StarlingsTinkerer.MoveTo(MQ11a_SC11_StarlingStart)
    MQ11a_StarlingsTinkerer.SetAlpha(0.0)
	Levelsystem.VisionEffectNoTimestop(_00E_MQ11a_SC11_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)

    MQ11a_SC11_SilverJugGoblets.MoveTo(MQ11a_SC11_XMarkerGoblets)
    _00E_QuestFunctions.WaitForReferenceToLoad(MQ11a_SC11_SilverJugGoblets)
    MS04MemoryFXBody01VFX.Play(MQ11a_SC11_SilverJugGoblets)

EndFunction

Function TinkererEnterBleedOut()

    MQ11a_StarlingsTinkerer.Kill(PlayerREF)

EndFunction

Function TinkererKill()

    MQ11a_StarlingsTinkerer.GetActorBase().SetEssential(False)
    MQ11a_StarlingsTinkerer.SetGhost(False)
    MQ11a_StarlingsTinkerer.DamageAV("Health", 500)
    
EndFunction

Function StopTowerVisionFX()

    Levelsystem.VisionEffectNoTimestopStop(_00E_MQ11a_SC11_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
    MQ11a_StarlingsTinkerer.Disable()
    MQ11a_DalGalarREF.MoveTo(MQ11a_DalGalarOrigin)
    MQ11a_SC11_SilverJugGoblets.Disable()
    Levelsystem.ResumeRandomHeadTracking()
    StartRiddleScene()
    
EndFunction

Function CaliaLookAtBreach()

    _00E_MC_CaliaREF.SetLookAt(MQ11a_SC12_ExplosionMarker)

EndFunction

Function CaliaClearLookAt()

    _00E_MC_CaliaREF.ClearLookAt()

EndFunction

Function StartRiddleScene()

	MQ11a_LeftTower.SetValueInt(1)
    MQ11a_SC12_LeftTowerRiddle.ForceStart()

EndFunction

;=====================================BARRIER RIDDLE================================================

Function SetVariable() 
    
    MQ11a_LeftTower.SetValueInt(2)

EndFunction

Function ShowTutorialMessage()

    SetObjectiveDisplayed(50)
    MQ11a_LeftTower.SetValueInt(3)
    If _00E_DisableQuestTutorials.GetValueInt() == 0
		_00E_MQ11a_TutorialCrosshair.ShowAsHelpMessage("MQ11a_TutorialCrosshair", 5, 5, 1)
    EndIf
	PlayerREF.AddItem(_00E_MQ11a_SC9_GunpowderSatchel, 1, false)
    PlayerREF.AddItem(_00E_MQ11a_SC12_FireArrows, 5, false)
    If PlayerREF.HasSpell(_10E_SpellTelekinesis) == False
        PlayerREF.AddSpell(_00E_MQ11a_SC12_Telekinesis, true)
    EndIf
    MQ11a_SC11_TriggerREF.Enable()
    MQ11a_SC12_CollisionPlane.Disable()
    RegisterForSingleUpdate(7)
    GoToState("BarrierRiddle")

EndFunction

Function ShowTutorialMessage02()

    Message.ResetHelpMessage("MQ11a_TutorialCrosshair")
	If _00E_DisableQuestTutorials.GetValueInt() == 0
		_00E_MQ11a_TutorialArrows.ShowAsHelpMessage("MQ11a_TutorialArrows", 5, 5, 1)
	EndIf

EndFunction

Function RefillFireArrows()
    If PlayerREF.GetItemCount(_00E_MQ11a_SC12_FireArrows) <= 0
    	PlayerREF.AddItem(_00E_MQ11a_SC12_FireArrows, 5, false)
    EndIf
EndFunction

Function PrepareForShootingFireArrows1()
	UnregisterForUpdate()
	SwitchCaliaNormalDialogue(False)
	RefillFireArrows()
EndFunction

Function PrepareForShootingFireArrows2()
    RegisterForSingleUpdate(4)
	ShowTutorialMessage02()
    GoToState("BarrierRiddle")
	SwitchCaliaNormalDialogue(True)    
EndFunction

Function RegisterD10Remark()
	bSaidD10Remark = True
EndFunction

Function ExplodeGears()

    UnregisterForUpdate()
	
    Message.ResetHelpMessage("MQ11a_TutorialArrows")
    Message.ResetHelpMessage("MQ11a_TutorialCrosshair")
    Message.ResetHelpMessage("Telekinesis")

    Wait(1)
    MQ11a_SC12_ExplosionMarker.PlaceAtMe(_00E_HarmlessFireExplosion, 1)
    Game.ShakeCamera()
    Wait(0.7)
    FXExplosionCatapultNearM.Play(MQ11a_SC12_ExplosionMarker)
    Game.ShakeCamera(afStrength = 1.0)
    MAGShockExpMassImod.Apply()
    Wait(0.4)
    MQ11a_SC12_StarlingBarrierREF.PlaceAtMe(ExplosionShockMassNoEffects, 1)
    TRPDwemerPistonOutM.Play(MQ11a_SC12_ExplosionMarker)
    MQ11a_SC12_BarrierSound.Disable()
    MQ11a_SC12_StarlingBarrierREF.PlayAnimation("playanim01")
    SetObjectiveCompleted(50)
    Levelsystem.GiveEP(__Config_RewardEXPBarrierRiddle)
    bLeftTowerRiddleSolved = True
    MQ11a_SC13_DalGalarREF.GetActorBase().SetName("")
    MQ11a_SC13_DalGalarREF.SetName("")
    
    PlayerREF.UnequipSpell(_00E_MQ11a_SC12_Telekinesis, 0)
    PlayerREF.UnequipSpell(_00E_MQ11a_SC12_Telekinesis, 1)
    PlayerREF.RemoveSpell(_00E_MQ11a_SC12_Telekinesis)
    MQ11a_LeftTowerDoorOut.SetLockLevel(0)
    MQ11a_LeftTowerDoorOut.Lock(false)
    _00E_TeleportGlobal.SetvalueInt(0)
    
EndFunction


Function SetMQ11a_LeftTower(int Value)

    MQ11a_LeftTower.SetValueInt(Value)

EndFunction

Function SetMQ11a_LeftTower_Inc(int Value)

    ; Same as SetMQ11a_LeftTower, but do it only if
    ; the current value of MQ11a_LeftTower is < Value.
    ; This is not 100% guaranteed to be correct, because the
    ; GetValueInt and the SetValueInt don't form an atomic block.

    if MQ11a_LeftTower.GetValueInt() < Value
        MQ11a_LeftTower.SetValueInt(Value)
    endif

EndFunction

Function SetD11SayOnce(int Value)

    ; Set the quest variable _00E_MQ11a_D11_Branch_SayOnce to Value.
    ; This ensures that the topic MQ11a_D11_Start is said only at the
    ; appropriate point (namely, in Maya's room), and only once.
    _00E_MQ11a_D11_Branch_SayOnce = Value

EndFunction

;=====================================MAYA VISION================================================

Function StartMayaVision()

    MQ11a_SC14_DalGalarLinkMarker.Disable()
    MQ11a_SC12_StarlingBarrierREF.Disable()

    MQ11a_SC13_MayaREF.MoveTo(MQ11a_SC13_MayaMoveToMarker)    
    _00E_QuestFunctions.WaitForReferenceToLoad(MQ11a_SC13_MayaREF)
    MS04MemoryFXBody01VFX.Play(MQ11a_SC13_MayaREF)

    Levelsystem.VisionEffectNoTimestop(_00E_MQ11a_SC13_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
    MQ11a_SC13_DalGalarREF.GetActorBase().SetName("Dal'Galar")    

    RegisterForSingleUpdate(90)
    
EndFunction

Function StopMayaVision()

    MQ11a_SC13_MayaREF.Disable()
    Levelsystem.VisionEffectNoTimestopStop(_00E_MQ11a_SC13_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
    MQ11a_SC13_DalGalarREF.Disable()
    Levelsystem.ResumeRandomHeadTracking()
    SetObjectiveDisplayed(41)
    SetMQ11a_LeftTower(8)
    CompanionIsTalking.SetValueInt(0)
    
EndFunction

Function PortPlayerBack()

	UnregisterForUpdate()
    GoToState("DefaultState")
    CompanionIsTalking.SetValueInt(1)
    Game.DisablePlayerControls()
    Levelsystem.FadeToBlack()
    PlayerREF.MoveTo(MQ11a_PlayerMainHallMarker)
    _00E_MC_CaliaREF.MoveTo(PlayerREF, 72.0) ; Port Calia to the right of the player
    CaliaEquipTorch(True)
    FadeToBlackBackIMOD.Apply() 
    FadeToBlackHoldIMOD.Remove()
    Game.EnablePlayerControls()
    RegisterForSingleUpdate(1)
    
EndFunction

Function SayOnEnteringMainHall()
    GoToState("DefaultState")
    RegisterForSingleUpdate(1)
EndFunction


;=====================================RIGHT TOWER, DISSECTING ROOM================================================

Function SetUpSpyScene()

    CompanionIsTalking.SetValueInt(0)
    MQ11a_SC15_SpyStartMarker002.MoveTo(PlayerRef, -(120.0 * Math.Sin(PlayerRef.GetAngleZ())), -(120.0 * Math.Cos(PlayerRef.GetAngleZ())), PlayerRef.GetHeight() + 50.0)
    MQ11a_SC15_SpyREF.Enable()
    MQ11a_SC15_SpyREF.SetAlpha(0.0)
    MQ11a_SC15_SpyREF.MoveTo(MQ11a_SC15_SpyStartMarker)

EndFunction

Function CaliaEquipTorch(bool Equipped = True)

    if Equipped == True 
        CaliaAddTorch()
        _00E_MC_CaliaREF.EquipItem(Torch01, False, True)
    Else
        _00E_MC_CaliaREF.UnequipItem(Torch01)
        CaliaRemoveTorch()
    EndIf   

EndFunction

Function CaliaAddTorch()

    If _00E_MC_CaliaREF.GetItemCount(Torch01) <= 0
        _00E_MC_CaliaREF.AddItem(Torch01, 1)
    EndIf

EndFunction

Function CaliaRemoveTorch()

    Int nTorchCount = _00E_MC_CaliaREF.GetItemCount(Torch01)
    If nTorchCount > 0
        _00E_MC_CaliaREF.RemoveItem(Torch01, nTorchCount)
    EndIf

EndFunction

Function DisableSpy()

    MQ11a_SC15_SpyREF.Disable()
    
EndFunction

Function SetMQ11a_RightTower(int Value)

	If MQ11a_RightTower.GetValueInt() < Value
	    MQ11a_RightTower.SetValueInt(Value)
	EndIf

EndFunction

Function ReanimateDissectingRoom()

	SwitchAllTeleportation(False)
    CaliaNoCombatStateChangeComments.SetValueInt(1)

    SetCaliaAsCompanion(True)
    
	_00E_Music_Combat_ShieldbrothersNoCond.Add()
    _00E_MC_CaliaREF.SetLookAt(MQ11a_SC15_ExplosionMarker)
    MQ11a_SC15_ExplosionMarker.PlaceAtMe(_00E_MQ11a_MagAnExpl)
    Wait(2.1)

    StartTrackingFight(FIGHT_ID_SC15, 0, _00E_MQ11a_SC15_ReanimationList, SchlossDalGalar, MQ11a_SC15_Zombie, False)
    RaiseDeadServants(_00E_MQ11a_SC15_ReanimationList, MQ11a_SC15_ExplosionMarker)
	
	RegisterForSingleUpdate(90)

	_00E_MC_CaliaREF.ClearLookAt()

EndFunction

Function EndDissectingRoomFight()

    MQ11a_SC15_RightTowerSpy.Stop()
    MQ11a_SC13_KeyREF.Enable()
    Wait(0.5)
    _00E_Music_Combat_ShieldbrothersNoCond.Remove()
    Wait(1.25)
    SetCaliaAsCompanion(False)
    MQ11a_SC16_Comment.ForceStart()

	SwitchAllTeleportation(True)

EndFunction

Function StartDissectingRoomVision()

    MQ11a_DiscipleREF02.MoveTo(MQ11a_SC16_DalGalarDiscipleMarker)
    MQ11a_DiscipleREF02.SetAlpha(0.0)

    MQ11a_SC16_DalGalarREF.MoveTo(MQ11a_SC16_DalGalarStartMarker)
    MQ11a_SC16_DalGalarREF.SetAlpha(0.0)
    MQ11a_SC16_DalGalarREF.SetAV("Variable01", 1.0) 
    MQ11a_SC16_DalGalarREF.EvaluatePackage()

    Wait(0.25)
    Levelsystem.StopRandomHeadTracking()

    Levelsystem.VisionEffectNoTimestop(_00E_MQ11a_SC16_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
    MQ11a_SC17_DissectingRoomVision.ForceStart()

EndFunction

Function StopDissectingRoomVision()

	Levelsystem.VisionEffectNoTimestopStop(_00E_MQ11a_SC16_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
	MQ11a_SC16_DalGalarREF.DisableNoWait()
	MQ11a_DiscipleREF02.DisableNoWait()
	MQ11a_SC16_DalGalarREF.SetAV("Variable01", 0.0) 
	MQ11a_SC16_DalGalarREF.MoveTo(MQ11a_DalGalarOrigin)
	Levelsystem.ResumeRandomHeadTracking()
    
EndFunction

Function StartStoneRoomVision()

    MQ11a_SC19_DalGalarREF.MoveTo(MQ11a_SC19_DalGalarStartMarker)
    MQ11a_SC19_DalGalarREF.SetAlpha(0.0)
    MQ11a_SC19_DogREF.Enable()
    MQ11a_SC19_DogREF.SetAlpha(0.0)
    Levelsystem.StopRandomHeadTracking()
    Levelsystem.VisionEffectNoTimestop(_00E_MQ11a_SC19_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)

EndFunction

Function CrystalChannelEnergy()

    ObjectReference ExplosionMarker = MQ11a_SC19_CrystalMarker.PlaceAtMe(XMarker)
    ExplosionMarker.MoveTo(MQ11a_SC19_CrystalMarker, 0.0, 0.0, 90.0)
    GhostFXShaderNightingaleCorrupt.Play(MQ11a_SC19_CrystalMarker) 
    _00E_A2_ChaosOrbSpawnSound.Play(MQ11a_SC19_CrystalMarker)
    Wait(3)
    ExplosionMarker.PlaceAtMe(DA16SkullAreaExplosion)
    Wait(2)
    _00E_MAGEldritchFireM.Play(MQ11a_SC19_CrystalMarker)
    _00E_MQ11a_SC19_BlackStoneChannelBeam.Cast(MQ11a_SC19_CrystalMarker, MQ11a_SC19_DogREF)
    Wait(1.5)
    NPCDogInjuredM.Play(MQ11a_SC19_DogREF)
    Wait(1.5)
    MQ11a_SC19_CrystalMarker.InterruptCast()
    NPCDogDeathM.Play(MQ11a_SC19_DogREF)
    MQ11a_SC19_DogREF.PlaceAtMe(_00E_MQ11a_SC19_BlackStoneDissolve)
    MQ11a_SC19_DogREF.AttachAshPile(DefaultAshPileGhostBlack)
    MQ11a_SC19_DogREF.Disable()
    GhostFXShaderNightingaleCorrupt.Stop(MQ11a_SC19_CrystalMarker)
    Wait(1)
    SetMQ11a_RightTower(6)

EndFunction

Function StopStoneRoomVision()

    MQ11a_SC19_DalGalarREF.MoveTo(MQ11a_DalGalarOrigin)
    Levelsystem.VisionEffectNoTimestopStop(_00E_MQ11a_SC19_VisionRefs, _00E_MQ11a_SC8_FreezeRefs)
    Levelsystem.ResumeRandomHeadTracking()
    
EndFunction

Function EnableLetter()

    MQ11a_SC19_LetterREF.Enable()

EndFunction

;=====================================DOOR RIDDLE================================================

Function CheckForRiddleDoor()

    if (MQ11a_DoorRiddle.GetValueInt() == 2)
        DRSStoneLabyrinthianOpenM.Play(PlayerREF)
        Wait(1)
        Self.SetStage(125)
        SetObjectiveCompleted(37)
        SetObjectiveCompleted(47)
        SetObjectiveCompleted(42)
        DA02BoethiahFXS.Stop(MQ11a_SC20_RiddleDoorMain)

        WaitForCompanionToStopTalking()
        CompanionIsTalking.SetValueInt(1)
        _00E_MC_CaliaREF.Say(MQ11a_D14_RemarksTopic)
        WaitForCompanionToStopTalking(7.0)
        CompanionIsTalking.SetValueInt(0)
    Else

        WaitForCompanionToStopTalking()
        CompanionIsTalking.SetValueInt(1)
        _00E_MC_CaliaREF.Say(MQ11a_D14_RemarksTopic)
        WaitForCompanionToStopTalking(3.0)

        Int nWordsFound = 0
        If MQ11a_LeftTower.GetValueInt() >= 9
        	nWordsFound += 1
        EndIf
        If MQ11a_RightTower.GetValueInt() >= 9
        	nWordsFound += 1
        EndIf

        If nWordsFound == 1
	        CompanionIsTalking.SetValueInt(1)

	        Wait(1)
	        CompanionIsTalking.SetValueInt(1)
	        _00E_MC_CaliaREF.Say(MQ11a_Hello)
	        WaitForCompanionToStopTalking(7.0)
	    EndIf

        CompanionIsTalking.SetValueInt(0)

    EndIf   

EndFunction


;=====================================LIBRARY SCENE================================================

Bool bPorcullisFightPrepared = False

Function PreparePortcullisFight()

    If bPorcullisFightPrepared
        Return
    EndIf
    bPorcullisFightPrepared = True

    Int myStage = GetStage()
    If myStage != 125
        If myStage < 125
            bPorcullisFightPrepared = False
        EndIf
        Return
    EndIf

    StartTrackingFight(FIGHT_ID_SC20, 130, MQ11a_SC20_LibraryEnemies, SchlossDalGalar, MQ11a_SC20_Zombie, True)
    SetCaliaAsCompanion(True)
    MQ11a_SC20_Portcullis.SetOpen(False)
    MQ11a_MQ11CL_ZombieParentREF.Activate(MQ11a_MQ11CL_ZombieParentREF) ; Switch the zombies to the aggressive mode

EndFunction

Function EndPortcullisFight()

	_00E_MC_CaliaREF.AddSpell(AbCanSayNotInCombat, False)

    MQ11a_SC20_Portcullis.SetOpen(True)

	; Wait for AbCanSayNotInCombat to warm up (or for timeout)
	Int nCountdown = 20
    Int nCanSpeakCounter = 0
	While (nCanSpeakCounter < 1) && (nCountdown > 0)
        If _00E_MC_CaliaREF.IsInFaction(CanSayFaction)
            nCanSpeakCounter += 1
        Else
            nCanSpeakCounter = 0
        EndIf
		Utility.Wait(1.0)
		nCountdown -= 1
	EndWhile

    If nCountdown > 0 && GetStage() < 130
        CompanionIsTalking.SetValueInt(1)
    	_00E_MC_CaliaREF.Say(MQ11a_D14_Remarks02Topic)
    EndIf
	_00E_MC_CaliaREF.RemoveSpell(AbCanSayNotInCombat)
	_00E_MC_CaliaREF.RemoveFromFaction(CanSayFaction)

EndFunction

Function SetUpAndStartLibraryScene()
	
	UnsummonApparitionIfExists()
    SetCaliaAsCompanion(False)
    MQ11a_SC21_WrongDoor002.BlockActivation(True)
    Game.RequestAutoSave()
	MQ11a_SC20_Portcullis.SetOpen(False)
    Levelsystem.StopRandomHeadTracking()
    MQ11a_SC20_LibraryScene.ForceStart()

EndFunction

Function EnableMercenaryLeader()
	
	Levelsystem.SetAllowIdleChatter(False)
    _00E_TeleportGlobal.SetvalueInt(1)
    MQ11a_SC20_MercenaryLeader.MoveTo(MQ11a_SC20_MercenaryLeader.GetLinkedRef())
    MQ11a_SC20_MercenaryLeader.SetAlpha(0.0)
    MQ11a_LeaderSpawned.SetValueInt(1)

    ; Failsave for the case when the player quickly runs back through
    ; the closing portcullis. This is to fix
    ; http://forum.sureai.net//tracker.php?p=8&t=11005 .
	float PlayerY = PlayerREF.GetPositionY()
	float CorrectY = MQ11a_SC20_Portcullis.GetPositionY() + 32
	if PlayerY < CorrectY
		float PlayerX = PlayerREF.GetPositionX()
		float PlayerZ = PlayerREF.GetPositionZ()
		PlayerREF.SetPosition(PlayerX, CorrectY, PlayerZ)
    endif

EndFunction

Function FadeInMercenaryLeader()

    CaliaEquipTorch(False)
    MGTeleportInEffect.Play(MQ11a_SC20_MercenaryLeader)
    MAGMysticismSoulTrapCapture.Play(MQ11a_SC20_MercenaryLeader)
    ; Wait(0.2)
    MQ11a_SC20_MercenaryLeader.SetAlpha(1, True)

EndFunction

Function EnableMercenaries()

    int iIndex = _00E_MQ11a_SC20_Mercenaries.GetSize()
    
    while iIndex > 0
        iIndex -= 1
        Actor iMercenary = _00E_MQ11a_SC20_Mercenaries.GetAt(iIndex) as Actor
        iMercenary.SetAlpha(0.0)
        iMercenary.MoveTo(iMercenary.GetLinkedRef())
        MGTeleportInEffect.Play(iMercenary)
        MAGMysticismSoulTrapCapture.Play(iMercenary)
        iMercenary.SetAlpha(1)
    EndWhile

EndFunction

Function KnockBackCalia()

    MQ11a_SC20_EldritchBarrierREF.MoveTo(MQ11a_SC20_MercenaryLeader)
    MQ11a_SC20_EldritchBarrierREF.PlayAnimation("PlayAnim02")
    _00E_MAGEldritchCharge_050M.Play(MQ11a_SC20_EldritchBarrierREF)
    MQ11a_SC20_EldritchBarrierREF.PlaceAtMe(_00E_MagicLightEldritch01, 1)
    MQ11a_SC20_BarrierSound = QSTDA16MagicBarrierLPM.Play(MQ11a_SC20_EldritchBarrierREF)
    Wait(0.8)
    _00E_MC_CaliaREF.DamageActorValue("Health", 1000)
    ParalyzeFxShader.Play(_00E_MC_CaliaREF)
    _00E_MAGEldritchFireSSVar01M.Play(_00E_MC_CaliaREF)
    _00E_MAGEldritchHitImod.Apply()
    Self.SetStage(135)
    
EndFunction

Function CaliaSetHeadTracking()

    _00E_MC_CaliaREF.SetLookAt(MQ11a_SC20_MercenaryLeader, True)
    MQ11a_SC20_MercenaryLeader.SetLookAt(_00E_MC_CaliaREF, True)

EndFunction

Function LeaderCastSpell()

    MQ11a_SC20_MercenaryLeader.PlayIdle(IdleMagic_01)
    Wait(1)
    _00E_MC_CaliaREF.RestoreActorValue("Health", 100)
    Wait(2.3)
    _00E_MC_CaliaREF.SetGhost(True)
    Debug.SendAnimationEvent(_00E_MC_CaliaREF, "returnToDefault")
    Debug.SendAnimationEvent(_00E_MC_CaliaREF, "IdleDA02BoethiahPostEnter")
    ParalyzeFxShader.Play(_00E_MC_CaliaREF)
    _00E_FXEldritchShader03.Play(_00E_MC_CaliaREF)
    QSTDA16MagicBarrierOffM.Play(MQ11a_SC20_EldritchBarrierREF)
    MQ11a_SC20_EldritchBarrierREF.PlayAnimation("PlayAnim01")
    Wait(2)
    _00E_MC_CaliaREF.ClearLookAt()
    MQ11a_SC20_MercenaryLeader.ClearLookAt()
    _00E_MC_CaliaREF.SetHeadTracking(True)
    Self.SetStage(145)

EndFunction

Function StartLibraryScene02()

    MQ11a_SC21_LibraryScene02.ForceStart()

EndFunction

Function CloseDoor()

    Game.RequestAutoSave()
    CaliaNoCombatComments.SetValueInt(1)
    MQ11a_SC22_LibraryZombieREF.Enable()
    StartTrackingFight(FIGHT_ID_SC22, 160, _00E_MQ11a_SC22_Enemies, SchlossDalGalar, MQ11a_SC22_Zombie, True)

    MQ11a_SC22_CaliaRescueScene.ForceStart()
    
    MQ11a_SC211_DoubleDoorREF.SetOpen(False)
    MQ11a_SC211_DoubleDoorREF.BlockActivation(True)
    MQ11a_SC21_WrongDoor002.BlockActivation(False)
    MQ11a_SC20_Portcullis.BlockActivation(True)
	MQ11a_SC20_MercenaryLeader.StopCombat()
    _00E_Music_Combat_Epic.Add()
    Levelsystem.ResumeRandomHeadTracking()
    GoToState("CaliaRescueScene")
    RegisterForSingleUpdate(5)

EndFunction

Function EndRescueKeyFight()
	GiveKeyToEnemies(_00E_MQ11a_SC22_Enemies, _00E_MQ11a_DalGalarLibraryKey)
EndFunction

Function StopRescueSequence()

    Game.RequestAutoSave()
    UnregisterForUpdate()
    Self.SetObjectiveDisplayed(70, 0)
    
    MQ11a_LeaderSpawned.SetValueInt(5)
    
    If _00E_MC_CaliaREF.IsDisabled()
        _00E_MC_CaliaREF.Enable()
    EndIf

    MQ11a_SC20_Portcullis.BlockActivation(False)
    _00E_MC_CaliaREF.SetGhost(True)
    _00E_Music_Combat_Epic.Remove()
    _00E_SilenceTransitionHighPriority.Add()

EndFunction

Function StartTransformSound()

    MQ11a_MQ11CL_ZombieParentREF.Disable()
    Int transformSound = _00E_MC_Calia_ItTransformLPDM.Play(PlayerREF)
    Sound.SetInstanceVolume(transformSound, 5.0)
    Wait(4)
    MQ11a_LeaderSpawned.SetValueInt(6)
    Sound.StopInstance(transformSound)

EndFunction

Function PlayTransformScream()

    _00E_MC_Calia_ItScream01M.Play(MQ11a_SC22_CaliaTransformSound)

EndFunction

Function PlayMassacreSound()
    
    _00E_MC_Calia_ItTearofHallysLeg.Play(MQ11a_SC22_CaliaTransformSound)

EndFunction

Function PlayFlySound()

    _00E_MC_Calia_ItFlyTohallysAndKill.Play(MQ11a_SC22_CaliaTransformSound)
    Wait(7)
    Game.ShakeCamera()

EndFunction

Function PlayScream02()

    _00E_MC_Calia_ItScream02M.Play(MQ11a_SC22_CaliaTransformSound)

EndFunction

Function PlayFinalExplosion()

    Game.DisablePlayerControls()
    ; The brittle wall connecting the hidden room with the main hall
    ; is now going to collapse... almost. In fact, there are three
    ; brittle walls... One (the "real" one) stands in the player's
    ; path, whereas the other two (the "fake" ones) are invisible
    ; (they are mostly hidden inside other walls). The "real" brittle
    ; wall is going to be disabled, while the "fake" brittle walls
    ; are going to collapse (leaving behind ruins). The problem with
    ; collapsing the real brittle wall would be that its collapsed
    ; state is not really savegame-persistent, so the player would
    ; get trapped in the hidden room unless if he restarts the game.
    MQ11a_SC22_BrittleWall.PlayAnimation("openStart")
    MQ11a_SC22_BrittleWallFake1.PlayAnimation("openStart")
    MQ11a_SC22_BrittleWallFake2.PlayAnimation("openStart")
    Game.ShakeCamera(afStrength = 1.0)
    _00E_MQ11a_SC22_CaliaTransformCompleteEXP.Apply()
    PlayerREF.SplineTranslateToRef(MQ11a_SC22_PlayerBoomStick, 200.0, 1000)
    Wait(0.1)
    _00E_NPCKillBodyfall.Play(PlayerREF)
    FadeToBlackHoldIMOD.Apply()
    MQ11a_SC22_BrittleWall.Disable()
    Self.SetStage(165)
    
EndFunction

Function SetUpBloodyScene()

    MQ11a_SC22_BrittleWall.PlayAnimation("openStart")
    
    If _00E_MC_CaliaREF.IsDisabled()
        _00E_MC_CaliaREF.Enable()
    EndIf

    _00E_TeleportGlobal.SetValueInt(1)
    MQ11a_MQ11CL_CameraEffectsREF.EnableNoWait()
    MQ11a_SC20_Mercenary03.DisableNoWait()
    MQ11a_SC20_Mercenary02.DisableNoWait()
    MQ11a_SC20_Mercenary01.DisableNoWait()
    MQ11a_SC20_MercenaryLeader.DisableNoWait()
    Debug.SendAnimationEvent(_00E_MC_CaliaREF, "IdleForceDefaultState")
    _00E_MC_CaliaREF.MoveTo(MQ11a_SC23_CaliaWoundedMarker)
    MQ11a_SC20_EnableREF001.Enable()

    MQ11a_SC23_CaliaWakeUp.ForceStart()

    _00E_FXEldritchShader03.Stop(_00E_MC_CaliaREF)
    ParalyzeFxShader.Stop(_00E_MC_CaliaREF)

    _00E_MC_CaliaREF.BlockActivation(True)    

    MQ11a_SC211_DoubleDoorREF.SetOpen(True)
    MQ11a_SC23_FinalGate.SetOpen(True)
    MQ11a_SC20_Portcullis.SetOpen(True)
    _00E_BloodyFXShader.Play(_00E_MC_CaliaREF)

    MQ11a_SC23_CaliaTalkingActivator.MoveTo(MQ11a_SC23_Bedroll, afZOffset = -64.0)
    MQ11a_SC23_CaliaTalkingActivator.EnableNoWait()

EndFunction

Function WakeUpPlayer()

    _00E_MC_CaliaREF.UnequipAll()
    UpdateCaliaAliasScriptState()
    PlayerREF.MoveTo(MQ11a_SC23_PlayerWakeUpMarker)
    Game.DisablePlayerControls()
    Game.SetPlayerAIDriven(True)
    PlayerREF.PlayIdle(TG05_GetUp)
    Wait(1)
    FadeToBlackHOLDIMod.PopTo(_00E_WakeUp)
    Wait(4)

    Game.EnablePlayerControls()
    Game.SetPlayerAIDriven(False)
    MQ11aDoorDisabler.Disable()
    MQ11a_SC24_DalGalarRef.SetAlpha(0.0)

    StartTrackingFight(FIGHT_ID_SC24, 182, _00E_MQ11a_SC24_FreezeRefs, SchlossDalGalar, MQ11a_SC24_Zombie, True)

EndFunction

Function AddMusic()

    _00E_SilenceTransitionHighPriority.Remove()
    _00E_Music_Special_Character_Calia.Add()

EndFunction

Function RemoveCaliaTheme()

    _00E_Music_Special_Character_Calia.Remove()
    _00E_Music_Special_Character_CaliaShort.Remove() ; Left for compatibility with old saves

EndFunction

Function PrepareBed()

    Game.DisablePlayerControls()
    FadeToBlackImod.Apply()
    Wait(2) 
    FadeToBlackImod.PopTo(FadeToBlackHoldIMOD)
    MQ11a_CaliaCamp.SetValueInt(1)
    MQ11a_SC23_CaliaMadeBed.Enable()

EndFunction

Function MoveCaliaToMadeBed()

	_SafeMoveNPCtoFurniture(_00E_MC_CaliaREF, MQ11a_SC23_TravelMadeBedMarker)
	_00E_MC_CaliaREF.Reset(MQ11a_SC23_TravelMadeBedMarker) ; fix for https://sureai.net/tracker/view.php?id=1877

EndFunction

Function EnableBed()

    MQ11a_SC23_CaliaTalkingActivator.MoveTo(MQ11a_SC23_CaliaMadeBed, afZOffset = -64.0)
    PlayerREF.MoveTo(MQ11a_SC23_PlayerCampMarker)
    _00E_BloodyFXShader.Stop(_00E_MC_CaliaREF)
    MQ11a_CaliaCamp.SetValueInt(2)
    
EndFunction

Function FadeBackEverything()

    _00E_MC_CaliaREF.UnequipAll()
    FadeToBlackHoldIMOD.PopTo(FadeToBlackBackImod)
    Wait(2)
    EnablePlayerControls()

EndFunction

;=====================================SANCTUARIUM================================================

Function PrepareTableVision()

    MQ11a_SC24_MayaDinner.ForceStart()
    GiveKeyToEnemies(_00E_MQ11a_SC24_FreezeRefs, _00E_MQ11a_SC24_SanctuariumKey)

EndFunction

Function CallTableVision()

    RemoveCaliaTheme()  
    MQ11a_SC24_DalGalarRef.SetName("Dal'Galar")
    MQ11a_SC23_MayaAtTable.Enable()

    Levelsystem.VisionEffectNoTimestop(_00E_MQ11a_SC24_VisionRefs, _00E_MQ11a_SC24_FreezeRefs)

    _00E_QuestFunctions.WaitForReferenceToLoad(MQ11a_SC23_MayaAtTable)
    MS04MemoryFXBody01VFX.Play(MQ11a_SC23_MayaAtTable)

    Levelsystem.StopRandomHeadTracking()
   
EndFunction

Function StopVision()

    Levelsystem.VisionEffectNoTimestopStop(_00E_MQ11a_SC24_VisionRefs, _00E_MQ11a_SC24_FreezeRefs)
    MQ11a_SC24_DalGalarRef.DisableNoWait()
    MQ11a_SC23_MayaAtTable.DisableNoWait()
    Levelsystem.ResumeRandomHeadTracking()
    MQ11a_SC25_MayasREF.SetAlpha(0.0) ; Some kind of failsave inherited from old _00E_MQ11a_SC25_FailsaveSC
    Wait(3)
    Message.ResetHelpMessage("MQ11a_TutorialArrows")
    _00E_MQ11a_SC23_MayaMessage.ShowAsHelpMessage("MQ11a_TutorialMaya", 5, 1, 1)
    
EndFunction

;=====================================FINAL ROOM================================================

Function StartLastFight()

    Message.ResetHelpMessage("MQ11a_TutorialMaya")
    SwitchAllTeleportation(False)
    
    Game.RequestAutoSave()
    StartTrackingFight(FIGHT_ID_SC25, 195, _00E_MQ11a_SC25_Enemies, SchlossDalGalar, MQ11a_SC25_Zombie, False)

    Game.ShakeCamera()
    ObjectReference XPlosionMarker = MQ11a_SC25_AnomalyStartREF.PlaceAtMe(XMarker, 1)  
    XPlosionMarker.MoveTo(MQ11a_SC25_AnomalyStartREF, 0.0, 0.0, 100)
    XPlosionMarker.PlaceAtMe(_00E_MQ11a_AnomalySpawnExplosion, 1)
    XPlosionMarker.PlaceAtMe(_00E_MQ11a_MagAnExpl, 1)
    int ScreamSound = _00E_NPCMagicAnomalyScreamM.Play(MQ11a_SC25_AnomalyStartREF)
    Sound.SetInstanceVolume(ScreamSound, 3.0)   
    MQ11a_SC25_AnomalyFinal.Enable()
    MQ11a_SC25_AnomalyFinal.MoveTo(MQ11a_SC25_AnomalyStartREF)
    RaiseDeadServants(_00E_MQ11a_SC25_Enemies, MQ11a_SC25_AnomalyStartREF)

EndFunction

Function EndLastFight(Int postFightQuestStage)

    SetStage(postFightQuestStage)
    _00E_Meditate_Allowed.SetValueInt(1) ; Allow only meditation, other teleports are still forbidden

EndFunction

Function StartMayaReanimationScene()

    Levelsystem.StopRandomHeadTracking()
    _00E_Music_Special_MQ11a_Reanimation.Add()
    MQ11a_SC25_DalGalarREF.MoveTo(MQ11a_SC25_DalGalarREF.GetLinkedRef())
    MQ11a_SC25_DalGalarREF.SetAlpha(0.0)
    MQ11a_SC25_DalGalarREF.AddSpell(_00E_AbPosessed)
    MQ11a_SC25_BeamREF.EnableNoWait()
    Levelsystem.VisionEffectNoTimestop(_00E_MQ11a_SC25_VisionRefs, _00E_MQ11a_SC24_FreezeRefs)
    MQ11a_SC25_MayaReanimation01.ForceStart()

EndFunction

Function StartBeamFX()

    MQ11a_SC25_BeamREF.PlaceAtMe(TG08BShockwaveExplosion, 1)
    MQ11a_SC25_BeamREF.PlayGamebryoAnimation("animIdle")
    BeamLPM = _00E_MAGEldritchBreathFireLPMSDM.Play(MQ11a_SC25_BeamREF)
    _00E_MQ11a_SC25_DalGalarScreamM.Play(MQ11a_SC25_DalGalarREF)
    Wait(3)
    MQ11a_SC25_BeamREF.PlayGamebryoAnimation("AnimPlay")
    QSTAlduinDeathExplosionC.Play(MQ11a_SC25_BeamREF)
    Game.ShakeCamera()
    _00E_MQ11a_BlackStoneSP.Cast(MQ11a_SC25_CastMarker, MQ11a_SC25_TargetMarker)
    Wait(4)
    Sound.StopInstance(BeamLPM)
    MQ11a_SC25_CastMarker.InterruptCast()
    MQ11a_SC25_DalGalarREF.SetGhost(False)
    MQ11a_SC25_DalGalarREF.DamageActorValue("Health", 1000)
    Self.SetStage(205)
    
EndFunction

Function HealDalGalarSC25()

    MQ11a_SC25_DalGalarREF.RestoreActorValue("Health", 100)

EndFunction

Function KnockOutMaya()

    MQ11a_SC25_MayasREF.Kill(MQ11a_SC25_MayasREF)

EndFunction

Function StartITFX()

    MayaTransformSound = _00E_MC_Calia_ItTransformLPDM.Play(MQ11a_SC25_MayasREF)
    _00E_MC_CaliaItFXS.Play(MQ11a_SC25_MayasREF)
    MQ11a_SC25_MayasREF.SetEyeTexture(_00E_MC_CaliaEyesIt)

EndFunction

Function MoveVeiledWoman()

    _SafeMoveNPCtoFurniture(MQ11a_SC25_VeiledWomanREF, MQ11a_SC25_VeiledWomanStart)
    MQ11a_SC25_VeiledWomanREF.SetAlpha(0.0)
    _00E_QuestFunctions.WaitForReferenceToLoad(MQ11a_SC25_VeiledWomanREF)
    MS04MemoryFXBody01VFX.Play(MQ11a_SC25_VeiledWomanREF)
    MQ11a_SC25_VeiledWomanREF.SetAlpha(0.5, True)
    
EndFunction

Function DissolveDalGalar()

    ShockDisintegrate01FXS.Play(MQ11a_SC25_DalGalarREF)
	Wait(1)
	MQ11a_SC25_DalGalarREF.PlaceAtMe(ExplosionRuneShockSmallNoEffects)
	MQ11a_SC25_DalGalarREF.PlaceAtMe(_00E_FS_MAGGoreExplosion)
	MQ11a_SC25_DalGalarREF.Disable()
	Game.TriggerScreenBlood(10)

EndFunction

Function StopITFX()

    Sound.StopInstance(MayaTransformSound)
    _00E_MC_CaliaItFXS.Stop(MQ11a_SC25_MayasREF)
    MQ11a_SC25_MayasREF.SetGhost(False)
    MQ11a_SC25_MayasREF.GetActorBase().SetEssential(False)
    MQ11a_SC25_MayasREF.Kill(MQ11a_SC25_MayasREF)
    
EndFunction

Function CaliaAndVeiledWomanDisappear()

    _00E_Music_Special_Event_Dark.Remove()
    Levelsystem.VisionEffectNoTimestopStop(_00E_MQ11a_SC25_VisionRefs, _00E_MQ11a_SC24_FreezeRefs)
    MQ11a_SC25_VeiledWomanREF.DisableNoWait()
    MQ11a_SC25_MayasREF.DisableNoWait()
    MQ11a_SC25_BeamREF.DisableNoWait()
    Levelsystem.ResumeRandomHeadTracking()
    Self.SetStage(210)
    MQ11a_SC26_DoorREF.BlockActivation(False)
    _00E_MQ11a_SC23_Calia.ShowAsHelpMessage("MQ11aFinalCalia", 5, 1, 1)

EndFunction

Function GoToFrostcliffTavern()

    ; Remove combat music (failsave for
    ; http://forum.sureai.net//tracker.php?p=8&t=10216 ).
    _00E_Music_Combat_ShieldbrothersNoCond.Remove()
    _00E_Music_Combat_Epic.Remove()

    CaliaNoCombatComments.SetValueInt(0)
    _00E_MC_CaliaREF.BlockActivation(False)
    UpdateCaliaAliasScriptState()
    MQ11a_SC23_CaliaTalkingActivator.DisableNoWait()
    SetObjectiveCompleted(37)
    Game.RequestAutoSave()

    MQ11a_Failsave_BlackStoneChestREF.RemoveItem(_00E_MQ11a_BlackStoneMisc, 1, false, PlayerREF)
    
    if PlayerREF.GetItemCount(_00E_MQ11a_BlackStoneMisc) < 1
        PlayerREF.AddItem(_00E_MQ11a_BlackStoneMisc, 1)
    EndIf
    
    Game.DisablePlayerControls()
    Levelsystem.FadeToBlack()

    _00E_Music_Special_MQ11a_Reanimation.Remove()
    RemoveCaliaTheme()

    PlayerREF.MoveTo(MQ11a_SC26_PlayerPortMarker)

    
    If _00E_MC_CaliaREF.IsDisabled()
        _00E_MC_CaliaREF.Enable()
    EndIf
    
    _00E_MC_CaliaREF.SetOutfit(_00E_MC_Calia_SimpleClothingBarefeet)
    _00E_MC_CaliaREF.UnEquipItem(_01E_20Huetergrossschwert)
    _00E_MC_CaliaREF.MoveTo(MQ11a_SC26_CaliaPortMarker)
    MQ11a_SC26_LastConversationCalia.ForceStart()
    MQ11a_SC26_Chair.Activate(PlayerREF)
    ; Hold the door! Calia's forcegreet might fail, and then we want
    ; the player to talk to her.
    MQ11a_SC26_Door.SetLockLevel(255)
    MQ11a_SC26_Door.Lock()
    _00E_QuestFunctions.EnableReferenceFormList(MQ11a_SC6_WolfPackGates)
    ; Failsave to fix a bug in which the player fails to sit down
    ; and instead levitates through the level
    ; ( http://forum.sureai.net//tracker.php?p=8&t=10396
    ; = http://forum.sureai.net//tracker.php?p=8&t=11007 ).
    Wait(13)
    if PlayerREF.GetSitState() < 3
        ;Debug.Notification("Restless butt syndrome detected! Trying to activate chair again.")
        MQ11a_SC26_Chair.Activate(PlayerREF)
    endif
    
EndFunction

Function SC26_FadeBack()

    if _00E_MC_CaliaREF.GetSitState() < 3
        ; http://sureai.net:9898/browse/ERB-1460
        _00E_MC_CaliaREF.Disable()
        _00E_MC_CaliaREF.Enable()
        Wait(1)
    endif

    Weapon caliaWeapon = _00E_MC_CaliaREF.GetEquippedWeapon()
    If caliaWeapon
        _00E_MC_CaliaREF.UnequipItem(caliaWeapon)
    EndIf

    FadeToBlackHoldIMOD.PopTo(FadeToBlackBackImod)
    Game.EnablePlayerControls()

EndFunction

Function SC26_OpenDoorAgain()

    ; Done again in FinishQuest() function:
    MQ11a_SC26_Door.SetLockLevel(30)
    MQ11a_SC26_Door.Lock(false)

EndFunction

Function FinishQuest()

    ResetCaliaDialogueFlags()
    SwitchAllTeleportation(True)
    Levelsystem.SetAllowIdleChatter(True)
    Levelsystem.RemoveSilence()
    RemoveCaliaTheme()
    RegisterForSingleUpdateGameTime(9)
    bStartCaliaCQ = True
    Levelsystem.GiveEP(1500)
    MQ11a_SC26_Door.SetLockLevel(30)
    MQ11a_SC26_Door.Lock(false)

EndFunction

Function RemoveTaniyshaLetter()

    PlayerREF.RemoveItem(_00E_MQ11a_SC19_TaniyshaLetter, 1)

EndFunction

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

Bool bSkipNextRescueSceneMessage

State CaliaRescueScene

Event OnUpdateGameTime()

    If bStartCaliaCQ

        If !CQC03.IsCompleted() && CQC03.IsRunning()
            CQC03.SetStage(20)
            CQC03.FailAllObjectives()
        Elseif CQC03.IsCompleted()
            CQC04.SetStage(5)
        EndIf
    
        bStartCaliaCQ = False
    
    EndIf
    
EndEvent

Event OnUpdate()

    if __Config_RescueTimer > 0 && GetStage() < 160
        __Config_RescueTimer -= 5
        ; Show message every 10 seconds, or every 5 seconds if 30 or less seconds left
        If (bSkipNextRescueSceneMessage == False) || (__Config_RescueTimer <= 30)
            _00E_MQ11a_RescueTimer.Show(__Config_RescueTimer)
            bSkipNextRescueSceneMessage = True
        Else
            bSkipNextRescueSceneMessage = False
        EndIf
        RegisterForSingleUpdate(5)
    Elseif GetStage() < 160
        MUSSpecialDeath.Add()
        FadeToBlackHoldImod.Apply()
        Wait(2)
        Game.QuitToMainMenu()
    Else
        UnregisterForUpdate()
    EndIf

EndEvent

EndState

State BarrierRiddle

Event OnUpdate()

    int MQ11a_LeftTowerLocal = MQ11a_LeftTower.GetValueInt()

    If MQ11a_LeftTowerLocal == 3

    	If PlayerREF.GetItemCount(_00E_MQ11a_SC9_GunpowderSatchel) == 0
	        Message.ResetHelpMessage("MQ11a_TutorialCrosshair")
	        If _00E_DisableQuestTutorials.GetValueInt() == 0
				_00E_MQ11a_Tutorial_Telekinesis.ShowAsHelpMessage("Telekinesis", 5, 1, 1)
			EndIf

			Return
		EndIf

		RegisterForSingleUpdate(3)

	ElseIf MQ11a_LeftTowerLocal == 4

		If CaliaNormalDialogueAllowed.GetValueInt() > 0 && (bSaidD10Remark == False)
			bSaidD10Remark = True
	        _00E_MC_CaliaREF.Say(MQ11a_D10_remarkTopic)
	    EndIf

	    RefillFireArrows()

	    RegisterForSingleUpdate(3)

	EndIf

EndEvent

EndState


;=====================================================================================
; FIGHT TRACKING FUNCTIONS
;=====================================================================================

Int Property FIGHT_ID_SC08 = 1 AutoReadOnly
Int Property FIGHT_ID_SC15 = 2 AutoReadOnly
Int Property FIGHT_ID_SC20 = 3 AutoReadOnly
Int Property FIGHT_ID_SC22 = 4 AutoReadOnly
Int Property FIGHT_ID_SC24 = 5 AutoReadOnly
Int Property FIGHT_ID_SC25 = 6 AutoReadOnly

Function StartTrackingFight(Int fightId, Int postFightQuestStage, FormList enemyList, Location enemyLocation, LocationRefType enemyLocRefType, Bool bNoReanimation)
    (Alias_Player as _00E_MQ11a_PlayerAliasScript).StartTrackingFight(fightId, postFightQuestStage, enemyList, enemyLocation, enemyLocRefType, bNoReanimation)
EndFunction

Function RaiseDeadServants(Formlist corpseList, ObjectReference corpseActivatorRef)
    Form[] corpses = corpseList.ToArray()
    Int Index = 0
    While Index < corpses.Length
        (corpses[Index] as ObjectReference).Activate(corpseActivatorRef)
        index += 1
    EndWhile
EndFunction

Function RegisterKilledEnemy()
    Int curStage = GetStage()
    If curStage >= 95 && curStage < 210
        (Alias_Player as _00E_MQ11a_PlayerAliasScript).RegisterEnemyDeath()
    EndIf
EndFunction

Function RegisterFightEnd(Int fightId, Int postFightQuestStage)
    If (fightId == FIGHT_ID_SC08) || (fightId == FIGHT_ID_SC24)
        SetStage(postFightQuestStage)
    ElseIf fightId == FIGHT_ID_SC15
        EndDissectingRoomFight()
    ElseIf fightId == FIGHT_ID_SC20
    	EndPortcullisFight()
    ElseIf fightId == FIGHT_ID_SC22
        EndRescueKeyFight()
    ElseIf fightId == FIGHT_ID_SC25
        EndLastFight(postFightQuestStage)
    ElseIf fightId != 0
        Debug.Trace(self + ", RegisterFightEnd: unsupported fightId " + fightId)
    EndIf
EndFunction

Function GiveKeyToEnemies(FormList enemyList, Key keyToGive)
    Form[] enemies = enemyList.ToArray()
    Int Index

    Index = 0
    While Index < enemies.Length
        (enemies[Index] as ObjectReference).AddItem(keyToGive)
        Index += 1
    EndWhile

    Index = 0
    While Index < enemies.Length
    	_00E_Questshader.Play(enemies[Index] as ObjectReference)
    	Index += 1
    EndWhile
EndFunction


Function SwitchCaliaNormalDialogue(Bool bAllow)
    If bAllow
        CaliaNormalDialogueAllowed.SetValueInt(1)
    Else
        CaliaNormalDialogueAllowed.SetValueInt(0)
    EndIf
EndFunction

Function ResetCaliaDialogueFlags()
	SwitchCaliaNormalDialogue(True)
	CaliaNoCombatComments.SetValueInt(0)
	CaliaNoCombatStateChangeComments.SetValueInt(0)
	CompanionIsTalking.SetValueInt(0)
EndFunction

Function WaitForCompanionToStopTalking(Float fMaxTimeInSeconds = 2.0)
	Int nCountdown = (fMaxTimeInSeconds * 10.0) as Int
	If nCountdown < 10
		nCountdown = 10
	EndIf
	While (CompanionIsTalking.GetValueInt() > 0) && (nCountdown > 0)
		Wait(0.1)
		nCountdown -= 1
	EndWhile
EndFunction

Function SwitchAllTeleportation(Bool bAllow)
	If bAllow
		_00E_Meditate_Allowed.SetValueInt(1)
		_00E_TeleportGlobal.SetvalueInt(0)
	Else
		_00E_Meditate_Allowed.SetValueInt(0)
		_00E_TeleportGlobal.SetvalueInt(1)
	EndIf
EndFunction

Function SetCaliaAsCompanion(Bool bCompanion)
    If bCompanion
        _00E_QuestFunctions.SetNPCAsCompanion(_00E_MC_CaliaREF, True, 450, 600, 150)    
    Else
        _00E_QuestFunctions.SetNPCAsCompanion(_00E_MC_CaliaREF, False)
    EndIf
EndFunction

Function UpdateCaliaAliasScriptState()
    (Alias_Calia as _00E_MQ11a_CaliaAliasScript).UpdateState()
EndFunction


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

float FailsaveSpeedMult = 90.0

int BeamLPM 
int CaliaTransformSound
int MayaTransformSound
int SpokenWith
int MQ11a_SC20_BarrierSound

bool Property bLeftTowerRiddleSolved Auto Conditional Hidden

int Property __Config_RescueTimer Auto
{How much time the player has to save Calia}

bool AllMercenariesEnabled
bool bStartCaliaCQ
Bool bSaidD10Remark

int Property __Config_RewardEXPQuestCompletion = 1000 Auto
{How much EP the player gets after solving the barrier riddle}

int Property __Config_RewardEXPSpeakWithPeople = 50 Auto
{How much EP the player gets each time he obtains a new information in the Frostcliff tavern}

int Property __Config_RewardEXPBarrierRiddle = 300 Auto
{How much EP the player gets after solving the barrier riddle}

int Property _00E_MQ11a_D11_Branch_SayOnce = 0 Auto Conditional

Activator Property DefaultAshPileGhostBlack Auto

Quest Property CQC04 Auto
Quest Property CQC03 Auto

Actor Property _00E_MC_CaliaREF Auto
Actor Property PlayerREF Auto
Actor Property MQ11a_DiscipleREF Auto
Actor Property MQ11a_DiscipleREF02 Auto
Actor Property MQ11a_DalGalarREF Auto
Actor Property MQ11a_AnomalyREF Auto
Actor Property MQ11a_StarlingsTinkerer Auto
Actor Property MQ11a_SC13_DalGalarREF Auto
Actor Property MQ11a_SC16_DalGalarREF Auto
Actor Property MQ11a_SC19_DalGalarREF Auto
Actor Property MQ11a_SC15_SpyREF Auto
Actor Property MQ11a_SC19_DogRef Auto
Actor Property MQ11a_SC20_Mercenary03 Auto
Actor Property MQ11a_SC20_Mercenary02 Auto
Actor Property MQ11a_SC20_Mercenary01 Auto
Actor Property MQ11a_SC20_MercenaryLeader Auto
Actor Property MQ11a_SC24_DalGalarRef Auto
Actor Property MQ11a_SC25_DalGalarREF Auto
Actor Property MQ11a_SC25_MayasREF Auto
Actor Property MQ11a_SC25_VeiledWomanREF Auto
Actor Property MQ11a_SC02_Guest_01 Auto
Actor Property MQ11a_SC02_Guest_02 Auto

Book Property _00E_MQ11a_SC19_TaniyshaLetter Auto

Light Property _00E_MagicLightEldritch01 Auto
Light Property Torch01 Auto

Ammo Property _00E_MQ11a_SC12_FireArrows Auto

Idle Property IdleMagic_01 Auto
Idle Property IdleForceDefaultState Auto
Idle Property TG05_GetUp Auto

MusicType Property _00E_Music_Combat_ShieldbrothersNoCond Auto
MusicType Property _00E_SilenceTransitionHighPriority Auto
MusicType Property _00E_Music_Combat_Epic Auto
MusicType Property MUSSpecialDeath Auto
MusicType Property _00E_Music_Special_Character_Calia Auto
MusicType Property _00E_Music_Special_Character_CaliaShort Auto
MusicType Property _00E_Music_Special_Event_Dark Auto
MusicType Property _00E_Music_Special_MQ11a_Reanimation Auto

EffectShader Property _00E_MC_CaliaItFXS Auto
EffectShader Property GhostFXShaderNightingaleCorrupt Auto
EffectShader Property DA02BoethiahFXS Auto
EffectShader Property ParalyzeFxShader Auto
EffectShader Property _00E_FXEldritchShader03 Auto
EffectShader Property ShockDisintegrate01FXS Auto
EffectShader Property _00E_BloodyFXShader Auto

Outfit Property _00E_MC_Calia_Outfit Auto
Outfit Property _00E_MC_Calia_SimpleClothingBarefeet Auto

_00E_QuestFunctions Property Levelsystem Auto

Sound Property FXExplosionCatapultNearM Auto
Sound Property _00E_MQ11a_SC25_DalGalarScreamM Auto
Sound Property _00E_MC_Calia_ItTransformLPDM Auto
Sound Property _00E_MQ11a_MagicAnomalyScreamAM Auto
Sound Property _00E_NPCMagicAnomalyScreamM Auto
Sound Property TRPDwemerPistonOutM Auto
Sound Property _00E_A2_ChaosOrbSpawnSound Auto
Sound Property NPCDogInjuredM Auto
Sound Property NPCDogDeathM Auto
Sound Property _00E_MAGEldritchFireM Auto
Sound Property DRSStoneLabyrinthianOpenM Auto
Sound Property MAGMysticismSoulTrapCapture Auto
Sound Property QSTDA16MagicBarrierLPM Auto
Sound Property _00E_MAGEldritchCharge_050M Auto
Sound Property _00E_MAGEldritchFireSSVar01M Auto
Sound Property QSTDA16MagicBarrierOffM Auto
Sound Property _00E_MC_Calia_ItScream01M Auto
Sound Property _00E_MC_Calia_ItScream02M Auto
Sound Property _00E_MC_Calia_ItFlyTohallysAndKill Auto
Sound Property _00E_MC_Calia_ItTearofHallysLeg Auto
Sound Property _00E_NPCKillBodyfall Auto
Sound Property QSTAlduinDeathExplosionC Auto
Sound Property _00E_MAGEldritchBreathFireLPMSDM Auto

Static Property XMarkerHeading Auto
Static Property XMarker Auto

Topic Property MQ11a_D10_remarkTopic Auto
Topic Property MQ11a_D14_RemarksTopic Auto
Topic Property MQ11a_D14_Remark_SigilsTopic Auto

Message Property _00E_MQ11a_TutorialCrosshair Auto
Message Property _00E_MQ11a_TutorialArrows Auto
Message Property _00E_MQ11a_Tutorial_Telekinesis Auto
Message Property _00E_MQ11a_SC23_MayaMessage Auto
Message Property _00E_MQ11a_SC23_Calia Auto
Message Property _00E_MQ11a_RescueTimer Auto

VisualEffect Property MS04MemoryFXBody01VFX Auto
VisualEffect Property MGTeleportInEffect Auto

Spell Property _10E_SpellTelekinesis Auto
Spell Property _00E_MQ11a_SC12_Telekinesis Auto
Spell Property _00E_MQ11a_SC19_BlackStoneChannelBeam Auto
Spell Property _00E_MQ11a_BlackStoneSP Auto
Spell Property _00E_AbPosessed Auto

Topic Property MQ11a_D6_JourneyRemarkTopic Auto
Topic Property MQ11a_Hello Auto

TextureSet Property EyesMaleHumanHazelBrown Auto
TextureSet Property _00E_MC_CaliaEyesIt Auto

Weather Property _00E_MQ11a_SkyrimOvercastSnowSlow Auto
Weather Property SkyrimOvercastSnow Auto

ReferenceAlias Property Alias_Player Auto
ReferenceAlias Property Alias_Calia Auto
ReferenceAlias Property MagicalAnomaly Auto
ReferenceAlias Property TavernGuest02 Auto

ObjectReference Property MQ11a_CaliaAppearMarker Auto
ObjectReference Property MQ11a_SC2_CaliaStartMarker Auto
ObjectReference Property MQ11a_SC2_CaliaWallMarker Auto
ObjectReference Property MQ11a_SC6_SnowdriftParent Auto
ObjectReference Property MQ11a_SC6_ParentBarrel Auto
ObjectReference Property MQ11a_SC6_PlayerExplodeMarker Auto
ObjectReference Property MQ11a_SC6_CaliaBarrelMarker Auto
ObjectReference Property MQ11a_SC6_FirstOilPool Auto
ObjectReference Property MQ11a_SC7_CastleDoor Auto
ObjectReference Property MQ11a_SC7_DebrisParent Auto
ObjectReference Property MQ11a_SC8_DalGalarTalkMarker Auto
ObjectReference Property Mq11a_SC8_DiscipleTalkMarker Auto
ObjectReference Property Mq11a_SC8_CorpseExplosion Auto
ObjectReference Property MQ11a_SC8_KeyRef Auto
ObjectReference Property MQ11a_SC11_DalGalarStart Auto
ObjectReference Property MQ11a_SC11_StarlingStart Auto
ObjectReference Property MQ11a_DalGalarOrigin Auto
ObjectReference Property MQ11a_DiscipleOrigin Auto
ObjectReference Property MQ11a_SC11_SilverJugGoblets Auto
ObjectReference Property MQ11a_SC11_XMarkerGoblets Auto
ObjectReference Property MQ11a_SC11_TriggerREF Auto
ObjectReference Property MQ11a_SC12_ExplosionMarker Auto
ObjectReference Property MQ11a_LeftTowerDoorOut Auto
ObjectReference Property MQ11a_SC12_BarrierSound Auto
ObjectReference Property MQ11a_SC12_StarlingBarrierREF Auto
ObjectReference Property MQ11a_SC13_MayaREF Auto
ObjectReference Property MQ11a_SC13_MayaMoveToMarker Auto
ObjectReference Property MQ11a_Failsave_BlackStoneChestREF Auto
ObjectReference Property MQ11a_PlayerMainHallMarker Auto
ObjectReference Property MQ11a_SC15_SpyStartMarker Auto
ObjectReference Property MQ11a_SC15_ExplosionMarker Auto
ObjectReference Property MQ11a_SC13_KeyREF Auto
ObjectReference Property MQ11a_SC16_DalGalarDiscipleMarker Auto
ObjectReference Property MQ11a_SC16_DalGalarStartMarker Auto
ObjectReference Property MQ11a_SC19_DalGalarStartMarker Auto
ObjectReference Property MQ11a_SC19_CrystalMarker Auto
ObjectReference Property MQ11a_SC19_LetterREF Auto
ObjectReference Property MQ11a_SC15_SpyStartMarker002 Auto
ObjectReference Property MQ11a_SC26_DoorREF Auto
ObjectReference Property MQ11a_SC12_CollisionPlane Auto
ObjectReference Property MQ11a_SC20_RiddleDoorMain Auto
ObjectReference Property MQ11a_SC20_Portcullis Auto
ObjectReference Property MQ11a_SC20_EldritchBarrierREF Auto
ObjectReference Property MQ11a_SC20_CaliaForceMarker Auto
ObjectReference Property MQ11a_SC211_DoubleDoorREF Auto
ObjectReference Property MQ11a_SC22_CaliaTransformSound Auto 
ObjectReference Property MQ11a_SC22_BrittleWall Auto 
ObjectReference Property MQ11a_SC22_BrittleWallFake1 Auto
ObjectReference Property MQ11a_SC22_BrittleWallFake2 Auto
ObjectReference Property MQ11a_SC22_PlayerBoomStick Auto 
ObjectReference Property MQ11a_SC20_EnableREF001 Auto 
ObjectReference Property MQ11a_SC23_CaliaWoundedMarker Auto 
ObjectReference Property MQ11a_SC23_PlayerWakeUpMarker Auto 
ObjectReference Property MQ11a_SC23_CaliaTalkingActivator Auto
ObjectReference Property MQ11a_SC23_CaliaMadeBed Auto
ObjectReference Property MQ11a_SC23_PlayerCampMarker Auto
ObjectReference Property MQ11a_SC23_FinalGate Auto
ObjectReference Property MQ11a_SC25_AnomalyFinal Auto
ObjectReference Property MQ11a_Nehrimese_Patrol_Linker Auto
ObjectReference Property MQ11a_SC25_AnomalyStartREF Auto
ObjectReference Property MQ11a_SC25_CastMarker Auto
ObjectReference Property MQ11a_SC25_TargetMarker Auto
ObjectReference Property MQ11a_SC25_BeamREF Auto
ObjectReference Property MQ11a_SC25_VeiledWomanStart Auto
ObjectReference Property MQ11a_SC26_PlayerPortMarker Auto
ObjectReference Property MQ11a_SC26_CaliaPortMarker Auto
ObjectReference Property MQ11a_SC26_Chair Auto
ObjectReference Property MQ11a_SC23_MayaAtTable Auto
ObjectReference Property MQ11a_SC26_Door Auto
ObjectReference Property MQ11a_SC21_WrongDoor002 Auto
ObjectReference Property MQ11a_SC22_LibraryZombieREF Auto
ObjectReference Property MQ11a_MQ11CL_ZombieParentREF Auto
ObjectReference Property MQ11a_MQ11CL_CameraEffectsREF Auto
ObjectReference Property MQ11aDoorDisabler Auto
ObjectReference Property MQ11a_CaliaFollowtrigger_StartBoxREF Auto
ObjectReference Property MQ11a_SC02_Guest_01_Chair Auto
ObjectReference Property MQ11a_SC02_Guest_02_Chair Auto
ObjectReference Property MQ11a_SC07_FollowStartBox Auto
ObjectReference Property MQ11a_SC14_DalGalarLinkMarker Auto
ObjectReference Property MQ11a_LeftTowerDoorREF Auto
ObjectReference Property MQ11a_RightTowerDoorREF Auto
ObjectReference Property MQ11a_SC8_CaliaFailsaveMarkerREF Auto

Formlist Property MQ11a_SC6_WolfPackGates Auto
Formlist Property _00E_MQ11a_SC8_FreezeRefs Auto
Formlist Property _00E_MQ11a_SC8_VisionRefs Auto
Formlist Property _00E_MQ11a_SC8_ReanimationList Auto
Formlist Property _00E_MQ11a_SC11_VisionRefs Auto
Formlist Property _00E_MQ11a_SC13_VisionRefs Auto
Formlist Property _00E_MQ11a_SC15_ReanimationList Auto
Formlist Property _00E_MQ11a_SC16_VisionRefs Auto
Formlist Property _00E_MQ11a_SC19_VisionRefs Auto
Formlist Property _00E_MQ11a_SC20_Mercenaries Auto
Formlist Property _00E_MQ11a_SC22_Enemies Auto
Formlist Property _00E_MQ11a_SC24_FreezeRefs Auto
Formlist Property _00E_MQ11a_SC24_VisionRefs Auto
Formlist Property _00E_MQ11a_SC25_Enemies Auto
Formlist Property _00E_MQ11a_SC25_VisionRefs Auto
Formlist Property MQ11a_SC20_LibraryEnemies Auto

Scene Property MQ11a_SC1_CaliaTalksToPlayer Auto
Scene Property MQ11a_SC02_CaliaFrostcliffTavern Auto
Scene Property MQ11a_SC3_CaliaFrostCliffLoop Auto
Scene Property MQ11a_SC6_CaliaAtGate Auto
Scene Property MQ11a_SC7_CaliaExplodeBarrels Auto
Scene Property MQ11a_SC8_FirstVision Auto
Scene Property MQ11a_SC12_LeftTowerRiddle Auto
Scene Property MQ11a_SC14_LeftTowerVision02 Auto
Scene Property MQ11a_SC15_RightTowerSpy Auto
Scene Property MQ11a_SC16_Comment Auto
Scene Property MQ11a_SC17_DissectingRoomVision Auto
Scene Property MQ11a_SC20_LibraryScene Auto
Scene Property MQ11a_SC21_LibraryScene02 Auto
Scene Property MQ11a_SC22_CaliaRescueScene Auto
Scene Property MQ11a_SC23_CaliaWakeUp Auto
Scene Property MQ11a_SC24_MayaDinner Auto
Scene Property MQ11a_SC25_MayaReanimation01 Auto
Scene Property MQ11a_SC26_LastConversationCalia Auto

Explosion Property _00E_MQ11a_SC7_BarrelExplosionVFX01 Auto
Explosion Property _00E_MQ11a_SC7_BarrelExplosionVFX02 Auto
Explosion Property DA16SkullAreaExplosion Auto
Explosion Property _00E_MQ11a_SC19_BlackStoneDissolve Auto
Explosion Property TG08BShockwaveExplosion Auto
Explosion Property _00E_FS_MAGGoreExplosion Auto
Explosion Property ExplosionRuneShockSmallNoEffects Auto
Explosion Property _00E_MQ11a_MagAnExpl Auto
Explosion Property _00E_MQ11a_AnomalySpawnExplosion Auto
Explosion Property _00E_HarmlessFireExplosion Auto
Explosion Property ExplosionShockMassNoEffects Auto

MiscObject Property _00E_MQ11a_SC9_GunpowderSatchel Auto
MiscObject Property _00E_MQ11a_BlackStoneMisc Auto 

Cell Property MQ11CaliaCastleMain Auto
Cell Property MQ11DR Auto

Weapon Property _01E_20Huetergrossschwert Auto

ImageSpaceModifier Property FadeToBlackImod Auto
ImageSpaceModifier Property FadeToBlackBackImod Auto
ImageSpaceModifier Property FadeToBlackHoldImod Auto
ImageSpaceModifier Property MAGFireBallExpImod Auto
ImageSpaceModifier Property MAGShockExpMassImod Auto
ImageSpaceModifier Property _00E_MAGEldritchHitImod Auto
ImageSpaceModifier Property _00E_MQ11a_SC22_CaliaTransformCompleteEXP Auto
ImageSpaceModifier Property _00E_WakeUp Auto

GlobalVariable Property _00E_MQ11a_TavernGuestsTalkedTo Auto
GlobalVariable Property MQ11a_TravelState02 Auto
GlobalVariable Property MQ11a_LeftTower Auto
GlobalVariable Property MQ11a_RightTower Auto
GlobalVariable Property MQ11a_DoorRiddle Auto
GlobalVariable Property MQ11a_LeaderSpawned Auto
GlobalVariable Property CaliaNormalDialogueAllowed Auto
GlobalVariable Property CaliaNoCombatComments Auto
GlobalVariable Property CaliaNoCombatStateChangeComments Auto
GlobalVariable Property _00E_TeleportGlobal Auto
GlobalVariable Property _00E_Meditate_Allowed Auto
GlobalVariable Property MQ11a_CaliaCamp Auto
GlobalVariable Property CompanionIsTalking Auto
GlobalVariable Property _00E_DisableQuestTutorials Auto

Location Property MQ11a_CL_Location Auto
LocationRefType Property MQ11a_SC08_Zombie Auto
Location Property SchlossDalGalar Auto
LocationRefType Property MQ11a_SC15_Zombie Auto
LocationRefType Property MQ11a_SC20_Zombie Auto
LocationRefType Property MQ11a_SC22_Zombie Auto
LocationRefType Property MQ11a_SC24_Zombie Auto
LocationRefType Property MQ11a_SC25_Zombie Auto

Key Property _00E_MQ11a_DalGalarLibraryKey Auto
Topic Property MQ11a_D14_Remarks02Topic Auto
Spell Property AbCanSayNotInCombat Auto
Faction Property CanSayFaction Auto
EffectShader Property _00E_Questshader Auto
Key Property _00E_MQ11a_SC24_SanctuariumKey Auto

ObjectReference Property MQ11a_SC23_TravelMadeBedMarker Auto
ObjectReference Property MQ11a_SC23_Bedroll Auto
ObjectReference Property NQ34_EnableMarkerREF Auto