Scriptname nAssaultUpdQSTScrpt extends Quest  

import utility
import debug

actor player
float fTimer
bool restartrequest=false
location myloc

Event onInit()
player = Game.GetPlayer()
nEventsQST.stop()
If !(restartrequest)
	restartrequest = true
	gotoState("DefaultState")
Endif
endevent

state DefaultState
Event onBeginState()
	wait(0.2)
trace("<<< Deadly Dragons: Assault system running. >>>")
	restartrequest = false
	RegisterForUpdate(5.0)
Endevent

Event onUpdate()
	If mq104.IsCompleted() && DeventsTimer.Value > 0
		If RandomFloat(0.0, 100.0) <= 50.0
			fTimer = dEventsTimer.Value + dEventsRnd.Value
		Else
			fTimer = dEventsTimer.Value - dEventsRnd.Value
		Endif
		If fTimer <= 0
			fTimer = dEventsTimer.Value
		Endif
		trace("<<< Deadly Dragons: Assault timer set to " + fTimer as int + " >>>")
		UnregisterForUpdate()
		RegisterForSingleUpdateGameTime(fTimer)
	Endif
Endevent

Event onUpdateGameTime()
		gotoState("Waiting")
Endevent
Endstate

state Waiting
Event onBeginState()
If  !(player.IsInInterior()) && weather.GetSkyMode() == 3
	If DEventsSZ.Value > 0
		myloc = game.getplayer().GetCurrentLocation()
		If !myloc.HasKeyword(LocTypeCity)
			gotoState("EventRun")
		Else
			registerForUpdateGameTime(1.0)
			trace("<<< Deadly Dragons: Assault - pending. >>>")
		Endif
	Else
		gotoState("EventRun")
	Endif
Else
	registerForUpdateGameTime(1.0)
	trace("<<< Deadly Dragons: Assault <[interior]>. Pending. >>>")
Endif
Endevent

Event OnUpdateGameTime()
If  !(player.IsInInterior())
	If DEventsSZ.Value > 0
		myloc = game.getplayer().GetCurrentLocation()
		If !myloc.HasKeyword(LocTypeCity)
			UnregisterForUpdateGameTime()
			gotoState("EventRun")
		Else
			trace("<<< Deadly Dragons: Assault <[safe location]>. Pending. >>>")
		Endif
	Else
		UnregisterForUpdateGameTime()
		gotoState("EventRun")
	Endif
Else
	trace("<<< Deadly Dragons: Assault <[interrior]>. Pending. >>>")
Endif
Endevent
Endstate

state EventRun
Event onBeginState()
nAssaultQST.start()
stop()
Endevent
Endstate

GlobalVariable Property DEventsTimer  Auto
GlobalVariable Property DEventsRnd  Auto
GlobalVariable Property DEventsUncapper  Auto
GlobalVariable Property DEventsSZ  Auto
FormList Property DDDragonsList  Auto  
Keyword Property LocTypeCity  Auto  
Quest Property MQ104  Auto  
Spell Property DDMenuTestLP  Auto  
Quest Property nEventsQST  Auto  
Quest Property nAssaultQST  Auto 
