Scriptname _00E_SermonTriggerBoxScript extends ObjectReference  


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

Event OnInit()
	bSermonsAllowed = True
EndEvent

Event OnTriggerEnter(ObjectReference akActionRef)
	If akActionRef == PlayerREF
		bPlayerIsInside = True
		TryStartSermonScene()
	EndIf
EndEvent

Event OnTriggerLeave(ObjectReference akActionRef)
	If akActionRef == PlayerREF
		bPlayerIsInside = False
		UnregisterForUpdate()
	EndIf
EndEvent

Event OnUpdateGameTime()
	bSermonsAllowed = True
	If bPlayerIsInside
		TryStartSermonScene()
	EndIf
EndEvent

Event OnUpdate()
	TryStartSermonScene()
EndEvent


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

Function TryStartSermonScene()
	Actor priestRef = (GetLinkedRef() as Actor)

	If _00E_GenericDialogues_PriestSpeech01.IsPlaying() || priestRef.IsDead() || priestRef.IsDisabled() || (bSermonsAllowed == False)
		Return
	EndIf

	ObjectReference preachMarkerRef = priestRef.GetLinkedRef()

	If (priestRef.GetDistance(preachMarkerRef) > 1000.0) || (_00E_TimeControl.HourIsInRange(GameHour.GetValue(), __Config_SermonHourStart, __Config_SermonHourEnd) == False)
		If bPlayerIsInside
			RegisterForSingleUpdate(5.0)
		EndIf
		Return
	EndIf

	; Start the scene
	If bPlayerIsInside && bSermonsAllowed
		bSermonsAllowed = False

		Alias_PreachMarker.ForceRefTo(preachMarkerRef)
		Alias_PreachLookMarker.ForceRefTo(priestRef.GetLinkedRef(PreachingMarkerKeyword))
		Alias_Preacher.ForceRefTo(priestRef)
		_00E_SermonTempleID.SetValueInt(__Config_TempleID)

		_00E_GenericDialogues_PriestSpeech01.ForceStart()

		bSermonsAllowed = False ; Just in case
		; Re-allow sermons 1 hour after __Config_SermonHourEnd
		Float fCooldownPeriod = __Config_SermonHourEnd + 1.0 - GameHour.GetValue()
		If fCooldownPeriod < 0.0
			fCooldownPeriod += 24.0
		EndIf
		If fCooldownPeriod < 1.0
			fCooldownPeriod = 1.0
		EndIf
		RegisterForSingleUpdateGameTime(fCooldownPeriod)
	EndIf

EndFunction


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

Bool bSermonsAllowed = True
Bool bPlayerIsInside = False

Float Property __Config_SermonHourStart Auto
{The minimum hour to launch the scene}
Float Property __Config_SermonHourEnd Auto
{The maximum hour to launch the scene}
Int Property __Config_TempleID Auto
{ 1 - Temple in Flusshaim, 2 - Malphas Temple in Ark }

Scene Property _00E_GenericDialogues_PriestSpeech01 Auto

Actor Property PlayerREF Auto
GlobalVariable Property GameHour Auto
GlobalVariable Property _00E_SermonTempleID Auto
Keyword Property PreachingMarkerKeyword Auto

ReferenceAlias Property Alias_Preacher Auto
ReferenceAlias Property Alias_PreachMarker Auto
ReferenceAlias Property Alias_PreachLookMarker Auto