Scriptname _00E_CQJ02_Functions extends Quest Conditional

Import _00E_QuestFunctions


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

Event OnUpdate()
	
	If (PlayerREF.GetCurrentLocation() == CapitalCityLocation || PlayerREF.GetCurrentLocation() == _00E_SuntempleLocation) && (PlayerREF.GetWorldspace() != Akropolis && !(PlayerREF.IsInInterior()))
		objSpawnMarker = PlayerREF.PlaceAtMe(XMarkerHeading, 1)
		objSpawnMarker.MoveTo(PlayerRef, -250.0 * Math.Sin(PlayerRef.GetAngleZ()), -250.0 * Math.Cos(PlayerRef.GetAngleZ()), PlayerRef.GetHeight())
		akMessengerKid = objSpawnMarker.PlaceActorAtMe(_00E_CQJ02_Messenger, 1)
		MessengerKid.ForceRefTo(akMessengerKid)
		CQJ02_SC01_Messenger.ForceStart()
	Else
		RegisterForSingleUpdate(7)
    EndIf

EndEvent

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

Function SpawnMessengerKid()
    
    RegisterForSingleUpdate(7)
    
EndFunction

Function GiveLetter()

    int iPlayerSex = PlayerREF.GetActorBase().GetSex()

    If MQ07a.IsCompleted() 
        If iPlayerSex == 0
            if PlayerREF.GetItemCount(_00E_CQJ02_Letter01_Male) == 0
                PlayerREF.AddItem(_00E_CQJ02_Letter01_Male, 1)
            EndIf
        Else
            if PlayerREF.GetItemCount(_00E_CQJ02_Letter01_Female) == 0
                PlayerREF.AddItem(_00E_CQJ02_Letter01_Female, 1)
            EndIf
        EndIf
    Else
        If iPlayerSex == 0
            if PlayerREF.GetItemCount(_00E_CQJ02_Letter02_Male) == 0
                PlayerREF.AddItem(_00E_CQJ02_Letter02_Male, 1)
            EndIf
        Else
            If PlayerREF.GetItemCount(_00E_CQJ02_Letter02_Female) == 0 
                PlayerREF.AddItem(_00E_CQJ02_Letter02_Female, 1)
            EndIf
        EndIf
    EndIf
    
EndFunction

Function DespawnMessengerKid()

    ; This function is now being called multiple times to get rid of
    ; the "undying messenger kid" bug:
    ; https://www.reddit.com/r/enderal/comments/57kqee/all_hail_the_immortal_errand_boy_all_spoilers/
    ; Some defensive programming in here (thanks Arthmoor).

    MessengerKid.Clear()
    if akMessengerKid != None
        akMessengerKid.Disable()
        akMessengerKid.Delete()
        akMessengerKid = None
    endif
    if objSpawnMarker != None
        objSpawnMarker.Delete()
        objSpawnMarker = None
    endif

EndFunction

Function AddSilence()

    _00E_SilenceLongTransitionHighPriority.Add()

EndFunction

Function EnableJesparAndStartSC02()

    If _00E_MC_JesparREF.IsDisabled()
        _00E_MC_JesparREF.Enable()
    EndIf
    
    _00E_MC_JesparREF.SetAlpha(1)
    _00E_MC_JesparREF.MoveTo(CQJ02_JesparLeanMarker)
    
    CQJ02_SC02_Lysia_01.ForceStart()
    
EndFunction

Function StartScene02Failsave()

	CQJ02_TrapdoorREF.BlockActivation(False)

	if !CQJ02_SC02_Lysia_01.IsPlaying()
		CQJ02_SC02_Lysia_01.ForceStart()
	EndIf

EndFunction

Function SetTobbacoInt(int iInteger)

    iTobaccoInt = iInteger

EndFunction

Function DrinkOrSmoke()
    
    If iTobaccoInt == 1
        Game.ForceThirdPerson()
        Game.DisablePlayerControls()
        PlayerREF.PlayIdle(IdleDrink)
        Utility.Wait(3)
        Game.EnablePlayerControls()
    Elseif iTobaccoInt == 2
        PlayerREF.AddItem(FrostMirriam, 1)
    Else
        Return
    EndIf
    
EndFunction

Function AddMusic()

    _00E_SilenceLongTransitionHighPriority.Remove()
    _00E_Music_Special_Character_Jespar.Add()

EndFunction

Function StartSC03()

    CQJ02_SC03_Lysia_02.ForceStart()
    
EndFunction

Function FadeToBlackAndBack()

    FadeToBlackAndBackIMOD.Apply()

EndFunction

Function FinishQuest()

    Levelsystem.RemoveSilence()
    CQJ02_SC02_JesparTGBX.Disable()
    Levelsystem.GiveEP(__Config_iRewardEXP)
	Self.CompleteAllObjectives()

EndFunction

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

_00E_QuestFunctions Property Levelsystem Auto

int Property iTobaccoInt Auto Conditional Hidden
int Property __Config_iRewardEXP Auto Conditional 

Actor akMessengerKid

ObjectReference objSpawnMarker

Quest Property MQ07a Auto

Actor Property _00E_MC_JesparREF Auto
Actor Property PlayerREF Auto
ActorBase Property _00E_CQJ02_Messenger Auto

Ingredient Property FrostMirriam Auto

Location Property CapitalCityLocation Auto
Location Property _00E_SuntempleLocation Auto
Worldspace Property Akropolis Auto

MusicType Property _00E_Music_Special_Character_Jespar Auto

ReferenceAlias Property MessengerKid Auto

ImageSpaceModifier Property FadeToBlackAndBackIMOD Auto

Idle Property IdleDrink Auto

Scene Property CQJ02_SC01_Messenger Auto
Scene Property CQJ02_SC02_Lysia_01 Auto
Scene Property CQJ02_SC03_Lysia_02 Auto

MusicType Property _00E_SilenceLongTransitionHighPriority Auto

ObjectReference Property CQJ02_JesparLeanMarker Auto
ObjectReference Property CQJ02_SC02_JesparTGBX Auto
ObjectReference Property CQJ02_TrapdoorREF Auto

Static Property XMarkerHeading Auto

Book Property _00E_CQJ02_Letter01_Female Auto
Book Property _00E_CQJ02_Letter02_Female Auto
Book Property _00E_CQJ02_Letter01_Male Auto
Book Property _00E_CQJ02_Letter02_Male Auto