Scriptname swordQuestLockOnScript extends Quest

Actor Property target  Auto

GlobalVariable Property swordLockOnRadius  Auto const

Event onInit()                       ; This event will run once, when the script is initialized

	Actor PlayerActor = Game.GetPlayer()

	float MaxDistance = 50000
	float Distance = 0
	float Radius = swordLockOnRadius.getValue() as float

	target = none
	Actor randomTarget = none

	while 1 ;RegisterForSingleUpdate()
		if target != none && !target.isDead()
			if  PlayerActor.GetHeadingAngle(target) > 80 || PlayerActor.GetHeadingAngle(target) < -80
				target = none
				MaxDistance = 50000
			endif
		endif

		randomTarget = Game.FindRandomActorFromRef(PlayerActor , Radius)

		if randomTarget != Target && PlayerActor.GetHeadingAngle(randomTarget) < 80 && PlayerActor.GetHeadingAngle(randomTarget) > -80
	
			if randomTarget != PlayerActor && randomTarget.IsHostileToActor(PlayerActor)

				Distance = PlayerActor.GetDistance(randomTarget)

				if Target != none && Target != PlayerActor && !Target.IsDead()
					MaxDistance =  PlayerActor.GetDistance(Target)		
				else
					MaxDistance =  50000
				endif

				if Distance < MaxDistance

					MaxDistance = Distance
					Target = randomTarget

				endif

			endif

		endif

	endWhile

endEvent
