Scriptname _00E_FS_NQR04_Functions extends Quest  

Import _00E_QuestFunctions
Import Utility

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

Event OnUpdate()

	; spawns in the messenger kid that gives the player the quest
	If PlayerREF.GetCurrentLocation() == _00E_UEMAsterLocation && Self.GetStage() == 5 && (akMessengerKid.GetParentCell() != PlayerREF.GetParentCell())

		akMessengerKid = SC01_MessengerKid.GetActorReference()
	
		akMessengerKid.Enable()
		; makes sure that the messenger kid spawns outside of the players vision, removes the spawn marker afterwards
		objSpawnMarker = PlayerREF.PlaceAtMe(XMarkerHeading, 1)
		objSpawnMarker.MoveTo(PlayerRef, -250.0 * Math.Sin(PlayerRef.GetAngleZ()), -250.0 * Math.Cos(PlayerRef.GetAngleZ()), PlayerRef.GetHeight())
		akMessengerKid.MoveTo(objSpawnMarker)
		objSpawnMarker.Delete()
		
		; a failsafe in case the player was changing cells in the very moment the messenger kid spawned
		If akMessengerKid.GetParentCell() != PlayerREF.GetParentCell()
			akMessengerKid.MoveTo(PlayerREF)
		EndIf
		
		SC01_MessengerKid.ForceRefTo(akMessengerKid)

		FS_NQR04_SC01_Messenger.ForceStart()
		
		SetObjectiveDisplayed(7)
		
	EndIf

EndEvent

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

Function SetUp()

	SetObjectiveDisplayed(5)
	RegisterForSingleUpdate(7)

EndFunction

Function SpawnMessengerKid()
	
	; just a failsafe function
	RegisterForSingleUpdate(7)

EndFunction

Function GiveNote()
	
	; updating the objectives after receiving a note
	PlayerREF.AddItem(_00E_FS_NQR04_Note)
	SetObjectiveCompleted(5)
	SetObjectiveCompleted(7)
	SetObjectiveDisplayed(10)

EndFunction

Function EnableTharael()

	SetObjectiveCompleted(10)
	SetObjectiveDisplayed(15)
	FS_NQR03_DoorREF.Lock(False)
	_00E_FS_NQR04_TharaelREF.Enable()
	_00E_FS_NQR04_TharaelREF.SetOutfit(_00E_FS_NQR_Rhalata_Voice_OutfitMask)

EndFunction

Function DisableKid()
	
	If akMessengerKid.IsEnabled()
		akMessengerKid.Disable()
	EndIf
	akMessengerKid.Delete()
	FS_NQR04_SC02_TharaelZorkban.ForceStart()
	SetObjectiveCompleted(15)
	SetObjectiveDisplayed(20)

EndFunction

Function SetUpSC02()
	
	; setting the undercover outfit for Tharael
	_00E_FS_NQR04_TharaelREF.SetOutfit(_00E_FeinlederrstungCowl)
	_00E_FS_NQR04_TharaelREF.MoveTo(FS_NQR04_SC02_TharaelMarker)

EndFunction

Function StartIntitalCombatSC02()
	
	; starts the combat in the alleyway, gives a few seconds time if Tharael needs to finish his voicelines if needed
	FS_NQR04_SC02_LostOne01REF.StartCombat(_00E_FS_NQR04_TharaelREF)
	utility.Wait(3)
	_00E_FS_NQR04_TharaelREF.StartCombat(FS_NQR04_SC02_LostOne01REF)
EndFunction

Function UpdateObjectives01()
	
	; set Tharael as ally for the following encounters, removing ghost flag and but making sure hes still essential
	_00E_QuestFunctions.SetNPCAsCompanion(_00E_FS_NQR04_TharaelREF, True, 550, 500, 100)
	SetObjectiveCompleted(20)
	SetObjectiveDisplayed(25)
	FS_NQR04_DoorREF.Lock(False)

EndFunction

Function UpdateObjectives02()

	SetObjectiveCompleted(25)
	SetObjectiveDisplayed(30)
	FS_NQR04_SC03_TharaelOrphanage.ForceStart()

EndFunction

Function UpdateObjectives03()
	
	; once the alley is cleared, update objectives and enable enemies inside the orphanage, unlocking the door
	SetObjectiveCompleted(30)
	SetObjectiveDisplayed(35)
	SetObjectiveDisplayed(40)
	FS_NQR04_ButtonParentREF.Enable()
	FS_NQR04_BarrelREF.AddItem(_00E_FS_NQR04_OrphanageKey, 1)
	
	int iIndex = FS_NQR04_OrphanageEnemiesList.GetSize()
	
	While iIndex > 0
	
		iIndex -= 1
		
		Actor akActorToEnable = FS_NQR04_OrphanageEnemiesList.GetAt(iIndex) as Actor
		akActorToEnable.Enable()
		
	EndWhile

	FS_NQR04_DoorCourtyardREF.Lock(False)
	

EndFunction

Function UpdateSlotsFound(int iFoundSlot)
	
	; function for the three hidden stashes, 
	; only number 3 is necessary to process the quest, other onces are optional objectives
	; 1 = _00E_FS_NQR04_Casket
	; 2 = button ceiling
	; 3 = button door frame
	If !(IsObjectiveCompleted(40))
		FS_NQR04_SecretSlotsFound.SetValue(FS_NQR04_SecretSlotsFound.GetValue() + 1)
		UpdateCurrentInstanceGlobal(FS_NQR04_SecretSlotsFound)
		int CurrentCount = FS_NQR04_SecretSlotsFound.GetValueInt()

		If CurrentCount >= 3
			SetObjectiveCompleted(40)
			Levelsystem.GiveEP(iSecretSlotsRewardEXP)
			If _00E_AchievementsEnabled.GetValueInt() == 1
				Game.UnlockAchievement("END_ORPHANAGE_01")
			EndIf
			
		ElseIf CurrentCount < 3
			SetObjectiveCompleted(40,0)
			SetObjectiveDisplayed(40, True, True)
		EndIf
		
		If iFoundSlot == 1
			FS_NQR04_SecretSlotMarker01REF.Disable()
		ElseIf iFoundSlot == 2
			FS_NQR04_SecretSlotMarker02REF.Disable()
		ElseIf iFoundSlot == 3
			FS_NQR04_SecretSlotMarker03REF.Disable()
		EndIf
		
	EndIf
	
EndFunction

Function ActivateSecondSlot()

	If !bSecondSlotFound

		bSecondSlotFound = True
		_00E_DRSElevatorM.Play(FS_NQr04_SecretSlot_02_DoorREF)
		FS_NQr04_SecretSlot_02_DoorREF.TranslateTo(FS_NQr04_SecretSlot_02_DoorREF.GetPositionX(), FS_NQr04_SecretSlot_02_DoorREF.GetPositionY(), 7.2906, -90, 0.0, 90, 50.0, 50.0)
		; added collision plane because it could be interacted through through a slit in the wood mesh
		FS_NQR04_SecretSlot_02_CollisonREF.Disable()
		UpdateSlotsFound(2)
		
	EndIf
		
EndFunction

Function StartSC04()
	
	; update objectives after finding the hidden stash, prepare and  start tharael's seizure scene
	UnflagTharaelAsCompanion()
	Levelsystem.SetAllowIdleChatter(False)
	FS_NQR04_ParentCandleREF.Enable()
	_00E_FS_NQR04_TharaelREF.UnequipItem(_00E_FS_NQR05_TharaelTorch)
	_00E_FS_NQR04_TharaelREF.RemoveItem(_00E_FS_NQR05_TharaelTorch)
	_00E_FS_NQR04_TharaelREF.Disable()
	_00E_FS_NQR04_TharaelREF.MoveTo(FS_NQR04_SC04_TharaelJumpingMarkerREF)
	_00E_FS_NQR04_TharaelREF.Enable()
	SetObjectiveCompleted(35)
	SetObjectiveDisplayed(45)
	FS_NQR04_SC04_TharaelSeizure.ForceStart()
	_00E_FS_NQR04_TharaelREF.SetGhost(True)

EndFunction

Function AddSpecialTharaelTrack()
	; adding special music for Tharael's seizure
	_00E_SilenceLongTransitionHighPriority.Add()
	_00E_FS_Music_Special_TharaelRefugeShort.Add()
EndFunction

Function RemoveSpecialTharaelTrack()
	_00E_FS_Music_Special_TharaelRefugeShort.Remove()
EndFunction

Function BlockActivationTharael()

	_00E_FS_NQR04_TharaelREF.BlockActivation(True)

EndFunction

Function EquipTorch()

	UnflagTharaelAsCompanion()
	_00E_FS_NQR04_TharaelREF.EquipItem(_00E_FS_NQR05_TharaelTorch, true)

EndFunction

Function UnflagTharaelAsCompanion()
	; don't remove this function, it also gets called from quest and dialogue fragments
	_00E_QuestFunctions.SetNPCAsCompanion(_00E_FS_NQR04_TharaelREF, false)

EndFunction

Function FadeToBlackAndBackReal()
	
	FadeToBlackIMOD.Apply()
	Wait(2.5)
	; PopTo() makes more sense then ApplyCrossFade() otherwise the player would see Tharael travel
	FadeToBlackImod.PopTo(FadeToBlackHoldImod)
	RemoveSpecialTharaelTrack()
	Wait(3)
	FadeToBlackHoldImod.PopTo(FadeToBlackBackImod)

EndFunction

Function FadeToBlackAndBack()

	FadeToBlackIMOD.Apply()
	Wait(2.7)
	FadeToBlackHoldIMOD.ApplyCrossFade(afFadeDuration = 1.0)
	Wait(1)
	_00E_FS_NQR04_TharaelREF.MoveTo(FS_NQR04_SC04_TharaelLeanMarkerREF)

EndFunction

Function FadeBack()

	FadeToBlackBackIMOD.ApplyCrossFade(afFadeDuration = 1.0)

EndFunction

Function MoveTharaelInterlude()
	
	; setting up and starting the scene after the seizure
	FadeToBlackIMOD.Apply()
	Wait(2.7)
	FadeToBlackHoldIMOD.ApplyCrossFade(afFadeDuration = 1.0)
	_00E_FS_NQR04_TharaelREF.MoveTo(FS_NQR04_SC04_CrossedArms)
	Wait(2)
	FadeToBlackBackIMOD.ApplyCrossFade(afFadeDuration = 1.0)

EndFunction

Function StartCompanionQuest()
	; gets called from dialogue option, starts the character quest
	FS_CQT01.SetStage(5)

EndFunction

Function FinishUp()
	
	; complete the quest and start the last rhalata quest
	If !FS_CQT01.IsCompleted()
		; stage 100 now also displays a message why the quest was failed (low sympathy)
		FS_CQT01.SetStage(100)
	EndIf

	CompleteAllObjectives()
	
	Player.Clear()
	Levelsystem.GiveEP(iRewardEXP)
	Levelsystem.SetAllowIdleChatter(True)
	Levelsystem.RemoveSilence()
	Levelsystem.RemoveCombatSoundtracks()
	FS_NQR04_SC05_TharaelWalkAway.ForceStart()
	FS_NQR05.setStage(5)
	
EndFunction

Function DeleteTharael()

	_00E_FS_NQR04_TharaelREF.Delete()

EndFunction

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

bool bSecondSlotFound

int iRewardEXP = 1000
int iSecretSlotsRewardEXP = 300

_00E_QuestFunctions Property Levelsystem Auto

Actor akMessengerKid

ObjectReference objSpawnMarker

Quest Property FS_CQT01 Auto
Quest Property FS_NQR05 Auto

Actor Property PlayerREF Auto
Actor Property _00E_FS_NQR04_TharaelREF Auto
Actor Property FS_NQR04_SC02_LostOne01REF Auto

Book Property _00E_FS_NQR04_Note Auto

Formlist Property FS_NQR04_OrphanageEnemiesList Auto

ActorBase Property _00E_FS_NQR04_MessengerGirl Auto

ObjectReference Property FS_NQR04_SC02_TharaelMarker Auto
ObjectReference Property FS_NQR04_DoorREF Auto
ObjectReference Property FS_NQr04_SecretSlot_02_DoorREF Auto
ObjectReference Property FS_NQR04_SC04_TharaelJumpingMarkerREF Auto
ObjectReference Property FS_NQR04_ParentCandleREF Auto
ObjectReference Property FS_NQR04_SC04_CrossedArms Auto
ObjectReference Property FS_NQR04_ButtonParentREF Auto
ObjectReference Property FS_NQR04_BarrelREF Auto
ObjectReference Property FS_NQR04_DoorCourtyardREF Auto
ObjectReference Property FS_NQR03_DoorREF Auto
ObjectReference Property FS_NQR04_SC04_TharaelLeanMarkerREF Auto
ObjectReference Property FS_NQR04_SecretSlot_02_CollisonREF Auto
ObjectReference Property FS_NQR04_SecretSlotMarker01REF Auto
ObjectReference Property FS_NQR04_SecretSlotMarker02REF Auto
ObjectReference Property FS_NQR04_SecretSlotMarker03REF Auto

Location Property _00E_UEMAsterLocation Auto

Light Property _00E_FS_NQR05_TharaelTorch Auto

Key Property _00E_FS_NQR04_OrphanageKey Auto

ImageSpaceModifier Property FadeToBlackIMOD Auto
ImageSpaceModifier Property FadeToBlackHoldIMOD Auto
ImageSpaceModifier Property FadeToBlackBackIMOD Auto

GlobalVariable Property FS_NQR04_SecretSlotsFound Auto
GlobalVariable Property _00E_AchievementsEnabled Auto

Outfit Property _00E_FeinlederrstungCowl Auto
Outfit Property _00E_FS_NQR_Rhalata_Voice_OutfitMask Auto

ReferenceAlias Property SC01_MessengerKid Auto
ReferenceAlias Property Player Auto

Scene Property FS_NQR04_SC01_Messenger Auto
Scene Property FS_NQR04_SC02_TharaelZorkban Auto
Scene Property FS_NQR04_SC03_TharaelOrphanage Auto
Scene Property FS_NQR04_SC04_TharaelSeizure Auto
Scene Property FS_NQR04_SC05_TharaelWalkAway Auto

Sound Property _00E_DRSElevatorM Auto

MusicType Property _00E_SilenceLongTransitionHighPriority Auto
MusicType Property _00E_FS_Music_Special_TharaelRefugeShort Auto

Static Property XMarkerHeading Auto