Scriptname _00E_NQ34_StartSC extends ObjectReference

Event OnCellLoad()
	If MQ11a.GetStage() >= 45 && MQ11a.GetStage() <= 55 && NQ34.IsRunning() == False && NQ34.GetStage() < 20 && !bDone
		TryStartQuest()
	EndIf
EndEvent

Event OnUpdate()
	If Is3DLoaded()
		TryStartQuest()
	EndIf
EndEvent


Function TryStartQuest()
	Float triggerDistance = 3072.0
	Float distance = PlayerREF.GetDistance(self)

	If distance <= triggerDistance
		bDone = True

		; Aggro the lost ones
		Int Index = 0
		While Index < LostOnes.Length
			If LostOnes[Index].IsDead() == False
				LostOnes[Index].StartCombat(PlayerREF)
			EndIf
			Index += 1
		EndWhile

        ; If the entropist (the target of NQ34) has been killed
        ; before the quest, he is resurrected.
        if NQ34_WildMageREF.IsDead()
            NQ34_WildMageREF.Resurrect()
        endif

		NQ34.Start();SetStage(5)
	Else
		; Try again a bit later
		If distance <= (triggerDistance + 1024.0)
			; Check more often if the player is close to the trigger distance
			RegisterForSingleUpdate(0.1)
		Else
			RegisterForSingleUpdate(1)
		EndIf
	EndIf
EndFunction

bool bDone

Actor Property NQ34_WildMageREF Auto
Actor Property PlayerREF Auto
Actor[] Property LostOnes Auto

Quest Property NQ34 Auto
Quest Property MQ11a Auto