Scriptname _00E_MQ12a_Functions extends Quest  

Import _00E_QuestFunctions

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

Event OnUpdate()

    If Self.GetStage() >= 20 && Self.GetStage() < 25
        
        Self.SetStage(25)
        Return
        
    EndIf

    ; Failsave for the case when MQ12b fails to start
    ; ( http://sureai.net:9898/browse/ERB-165 ).
    If IsCompleted() && (MQ12b.GetStage() < 5 || !MQ12b.IsRunning())
    
        MQ12b.SetStage(5)
        
        Utility.Wait(1)
        
        if MQ12b.GetStage() < 5
        
            _00E_MQ12aQuestBugMSG.Show()
            RegisterForSingleUpdate(6)

        endif
        
    EndIf

EndEvent

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

Function EnableBonfire()

    If MQ10b_SC2_Temple.IsPlaying()
        MQ10b_SC2_Temple.Stop()
    EndIf
    
    if MQ10b_SC4_Tealor.IsPlaying()
        MQ10b_SC2_Temple.Stop()
    EndIf

    _00E_TeleportGlobal.SetValueInt(0)
    LeuchtfeuerSwitcher.Enable()
    
EndFunction

Function UpdateStoneCount()

    int iStoneCount = MQ12a_BlackStonesInserted.GetValueInt()

    if iStoneCount < 3
        Self.UpdateCurrentInstanceGlobal(MQ12a_BlackStonesInserted)
        Self.SetObjectiveCompleted(5, 0)
        Self.SetObjectiveDisplayed(5, True, True)
    Elseif iStoneCount == 3
        Self.UpdateCurrentInstanceGlobal(MQ12a_BlackStonesInserted)
        Self.SetObjectiveCompleted(5)
    EndIf

EndFunction

Function StartSC01()

    MQ12a_SC1_Insert1stStone.ForceStart()
    
EndFunction

Function InsertFirstStoneFX()

    PlayLexInsertStoneAnimation(MQ12a_SC1_BlackStone01)
    
    Bool bLexIsGhost = _00E_MC_LexREF.IsGhost()
    If bLexIsGhost
    	_00E_MC_LexREF.SetGhost(False) ; For _00E_Ability_StaggerSelfSpell to work on Lex
    EndIf
    MQ07a_SC14_SigilstoneActivationExpIMOD.Apply()
    _00E_Ability_StaggerSelfSpell.Cast(_00E_MC_LexREF, _00E_MC_LexREF)
    MQ12a_SC1_BlackStone01.Enable()
    MQ12a_SC1_BlackStone01.PlaceAtMe(ExplosionShockMassNoEffects, 1)
    If bLexIsGhost
	    _00E_MC_LexREF.SetGhost(True)
	EndIf

EndFunction

Function InsertFirstStone02FX()

    PlayLexInsertStoneAnimation(MQ12a_SC1_BlackStone02)
    MQ07a_SC14_SigilstoneActivationExpIMOD.Apply()
    MQ12a_SC1_BlackStone02.Enable()
    MQ12a_SC1_BlackStone02.PlaceAtMe(ExplosionShockMassNoEffects, 1)

EndFunction

Function InsertFirstStone03FX()
    
    PlayLexInsertStoneAnimation(MQ12a_SC3_BlackStone03)
    MQ07a_SC14_SigilstoneActivationExpIMOD.Apply()
    MQ12a_SC3_BlackStone03.Enable()
    MQ12a_SC3_BlackStone03.PlaceAtMe(ExplosionShockMassNoEffects, 1)
    Utility.Wait(0.5)
    _00E_MQ12a_ActivateBeaconIMOD.Apply()
    _00E_MQ12a_BeaconMagicREF.KnockAreaEffect(1, 2048)
    Game.ShakeCamera(afStrength = 0.5)
    _00E_MQ12a_BeaconMagicREF.EnableNoWait()
    _00E_MQ12a_BeaconMagicREF.PlaceAtMe(PowerRacialWoodElfCommandAnimalExplosion01)
    
EndFunction

Function PlayLexInsertStoneAnimation(ObjectReference refStone)

    _00E_MC_LexREF.SetLookAt(refStone)
    Utility.Wait(0.25)
    _00E_MC_LexREF.PlayIdle(IdleGive)
    Utility.Wait(2.0)

EndFunction

Function LexLookAtStone03()

    _00E_MC_LexREF.SetLookAt(MQ12a_SC3_BlackStone03)

EndFunction

Function ClearLexLookAt()

    _00E_MC_LexREF.ClearLookAt()

EndFunction

Function StartSC02()

    MQ12a_SC2_Insert2ndStone.ForceStart()
    
EndFunction

Function StartSC03()

    MQ12a_SC3_Insert3rdStone.ForceStart()
    
EndFunction

Function FinishQuestAndStartMQ12b()

    MQ12a_SC3_Insert3rdStone.Stop()

    Levelsystem.GiveEP(__Config_iRewardExp)
    Self.CompleteQuest()
    Self.SetObjectiveCompleted(5)
    MQ12b.SetStage(5)
    RegisterForSingleUpdate(4)
	
    ; Failsafe in case SC3 fails to do this
    MQ12a_SC3_BlackStone03.EnableNoWait()
    _00E_MQ12a_BeaconMagicREF.EnableNoWait()

	If _00E_AchievementsEnabled.GetValueInt() == 1
		Game.UnlockAchievement("END_BLACK_STONES_01")
	EndIf

	;added in FS
	If FS_NQ03 == NONE
		FS_NQ03 = Game.GetFormFromFile(0x000438AD, "Skyrim.esm") as Quest
	EndIf
	if FS_NQ03.GetStage() < 280
		FS_NQ03.FailAllObjectives()
		FS_NQ03.SetStage(1000)
	endif

EndFunction

Function StartCQC04()

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

EndFunction

Function StartCQJ04()

    If CQJ02.IsCompleted()
        CQJ04.SetStage(5)
    Elseif !(CQJ02.IsCompleted()) && CQJ02.IsRunning()
        CQJ04.FailAllObjectives()
        CQJ04.SetStage(40)
    EndIf
    
EndFunction

Function StartMQ12bFailsave()

    RegisterForSingleUpdate(45)

EndFunction

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

bool bStartJesparCQ
bool bStartCaliaCQ

int Property __Config_iRewardExp = 500 Auto
{How much EP are to be granted upon quest completion}

Quest Property CQC04 Auto
Quest Property CQC03 Auto
Quest Property CQJ04 Auto
Quest Property CQJ02 Auto
Quest Property FS_NQ03 Auto
Quest Property MQ12b Auto

_00E_QuestFunctions Property Levelsystem Auto

Actor Property _00E_MC_LexREF Auto

ObjectReference Property LeuchtfeuerSwitcher Auto
ObjectReference Property MQ12a_SC1_BlackStone01 Auto
ObjectReference Property MQ12a_SC1_BlackStone02 Auto
ObjectReference Property MQ12a_SC3_BlackStone03 Auto
ObjectReference Property _00E_MQ12a_BeaconMagicREF Auto

ImageSpaceModifier Property MQ07a_SC14_SigilstoneActivationExpIMOD Auto
ImageSpaceModifier Property _00E_MQ12a_ActivateBeaconIMOD Auto

GlobalVariable Property MQ12a_BlackStonesInserted Auto
GlobalVariable Property _00E_TeleportGlobal Auto
GlobalVariable Property _00E_AchievementsEnabled Auto

Spell Property _00E_Ability_StaggerSelfSpell Auto

Scene Property MQ12a_SC1_Insert1stStone Auto
Scene Property MQ12a_SC2_Insert2ndStone Auto
Scene Property MQ12a_SC3_Insert3rdStone Auto
Scene Property MQ10b_SC4_Tealor Auto
Scene Property MQ10b_SC2_Temple Auto

Explosion Property ExplosionShockMassNoEffects Auto
Explosion Property PowerRacialWoodElfCommandAnimalExplosion01 Auto

Idle Property IdleGive Auto

Message Property _00E_MQ12aQuestBugMSG Auto