Scriptname nDragonTempestOrbScript extends activemagiceffect  

import utility

Activator Property nactivatortempest Auto  
SPELL Property xBeam Auto  
SPELL Property ThisSpell Auto 
float Property fRand Auto  
float Property fDelay Auto  
Keyword Property actortypedragon Auto  

objectreference obj
objectreference obc
objectreference obt
actor me
actor he
actor victim
float fX
float fY
float fZ
float fP

Event OnEffectStart(Actor akTarget, Actor akCaster)
	me = akCaster
	he = akTarget
	fX = he.GetPositionX()+ RandomFloat(-fRand, fRand)
	fY = he.GetPositionY()+ RandomFloat(-fRand, fRand)
	fZ = he.GetPositionZ() + 700.0
	obj = he.placeatme(nactivatortempest)
	obt = he.placeatme(fxemptyactivator)
	obj.setposition(fx, fy, fz)
	obj.enable()
	obj.Setscale(2.5)
	RegisterForSingleUpdate(0.5)
Endevent

Event onUpdate()
If !(me.IsDead())
	victim = Game.FindRandomActorFromRef(he, 700.0)
	If victim.HasKeyword(ActorTypeDragon) || victim == none
		while victim.HasKeyword(ActorTypeDragon)
			victim = Game.FindRandomActorFromRef(he, 700.0)
		Endwhile
		If victim != none
			obt.moveto(victim)
			wait(1)
			xBeam.remotecast(obj, me, obt)
		Else
			obt.moveto(victim)
			wait(1)
			xBeam.remotecast(obj, me, obt)
		Endif
		RegisterForSingleUpdate(2.0 + RandomFloat(-fDelay, fDelay))
	Else
		If victim != none
			obt.moveto(victim)
			wait(1)
			xBeam.remotecast(obj, me, obt)
		Else
			obt.moveto(victim)
			wait(1)
			xBeam.remotecast(obj, me, obt)
		Endif
		RegisterForSingleUpdate(2.0 + RandomFloat(-fDelay, fDelay))
	Endif
Else
	he.dispelspell(ThisSpell)
Endif
Endevent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
obj.disable(true)
obt.disable(true)
wait(0.5)
obj.delete()
obt.delete()
Endevent
Activator Property FXEmptyActivator  Auto  
