Scriptname _00E_Ark_GenericDialogueFunctions extends Quest Conditional

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

Event OnUpdateGameTime()
	If PlayerREF.GetWorldSpace() == CapitalCityUpperCity
		RegisterForSingleUpdateGameTime(1.0)
		Return
	EndIf

	; Failsafes
	bPlayerWithinHearingDistance = False

	Int Index = 0
	While Index < arrayPoemScenes.Length
		If arrayPoemScenes[Index].IsPlaying()
			arrayPoemScenes[Index].Stop()
		EndIf
		Index += 1
	EndWhile

	bPoemCurrentlyPlaying = False
EndEvent

Event OnUpdate()
	StartRandomPoemScene()
EndEvent

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

bool Function NoPoemPlaying()
	Int Index = 0
	While Index < arrayPoemScenes.Length
		If arrayPoemScenes[Index].IsPlaying()
			Return False
		EndIf
		Index += 1
	EndWhile

	Return True
EndFunction

Function StartRandomPoemScene()

	If _00E_UpperCity_PrinceMithREF.GetCurrentPackage() != _00E_CapitalCity_UpperCity_Mith_ReadPoems
		Return
	EndIf

	If PlayerREF.GetWorldSpace() != CapitalCityUpperCity
		bPlayerWithinHearingDistance = False
	EndIf

	If bPlayerWithinHearingDistance == False
		Return
	EndIf

	If NoPoemPlaying() == False
		Return
	EndIf

	; If the prince is away from his marker or in dialogue with the player, try again later
	If _00E_UpperCity_PrinceMithREF.GetDistance(PrinceMithReadPoemsMarker) > 64.0 || _00E_UpperCity_PrinceMithREF.IsInDialogueWithPlayer()
		RegisterForSingleUpdate(2.0)
		Return
	EndIf

	If !bPoemCurrentlyPlaying
		bPoemCurrentlyPlaying = True
		UnregisterForUpdate()
		; Pick a poem, but not the same as the last time
		Int poemIndex = Utility.RandomInt(0, arrayPoemScenes.Length - 1)
		While poemIndex == lastPoemPlayed
			poemIndex = Utility.RandomInt(0, arrayPoemScenes.Length - 1)
		EndWhile
		arrayPoemScenes[poemIndex].ForceStart()
		lastPoemPlayed = poemIndex
		RegisterForSingleUpdateGameTime(1.0)
	EndIf

EndFunction

Function OnPoemSceneEnd()
	bPoemCurrentlyPlaying = False
	RegisterForSingleUpdate(1.0)
EndFunction

Function RemoveAmulets()

	PlayerREF.RemoveItem(_00E_MQ01_FinnCarbosAmulet, 2)
	bPlayerHasGivenAmulets = True
	PlayerREF.AddItem(Gold001, 150)
	PlayerREF.AddItem(_00E_Eispranke, 1)
	_00E_EPHandler.GiveEP(150)
	
EndFunction

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

bool Property bPlayerHasHadMithConversation Auto Conditional Hidden
bool Property bPlayerHasHadToriusConversation Auto Conditional Hidden
bool Property bPlayerHasGivenAmulets Auto Conditional Hidden

bool Property bPlayerWithinHearingDistance Auto Hidden
bool Property bPoemCurrentlyPlaying Auto Hidden

Actor Property _00E_UpperCity_PrinceMithREF Auto
Actor Property PlayerREF Auto

Worldspace Property CapitalCityUpperCity Auto

MiscObject Property Gold001 Auto

Armor Property _00E_MQ01_FinnCarbosAmulet Auto
Potion Property _00E_Eispranke Auto

Package Property _00E_CapitalCity_UpperCity_Mith_ReadPoems Auto
ObjectReference Property PrinceMithReadPoemsMarker Auto

Scene[] Property arrayPoemScenes Auto
Int Property lastPoemPlayed = -1 Auto Hidden