class AIRandomTasks extends AIbehaviortaskScript
{

	protected override function Activate( context : ScriptExecutionContext ) {}

	protected function RollInteger( Max : Int32, Min : Int32 ) : Int32
	{
		return RandRange( Min, Max );
	}

}

class SetRandomIntArgument extends AIRandomTasks
{
	editable var m_MaxValue : Int32;
	editable var m_MinValue : Int32;
	editable var m_ArgumentName : CName;

	protected override function Activate( context : ScriptExecutionContext )
	{
		SetArgument( context, m_ArgumentName, RollInteger( m_MaxValue, m_MinValue ) );
	}

	protected function SetArgument( context : ScriptExecutionContext, argumentName : CName, intValue : Int32 )
	{
		ScriptExecutionContext.SetArgumentInt( context, m_ArgumentName, intValue );
	}

}

class GetRandomThreat extends AIRandomTasks
{
	editable inlined var m_outThreatArgument : AIArgumentMapping;

	protected override function Activate( context : ScriptExecutionContext )
	{
		var threats : array< TrackedLocation >;
		var chosenIndex : Int32;
		var chosenThreat : weak< GameObject >;
		threats = ScriptExecutionContext.GetOwner( context ).GetTargetTracker().GetThreats( false );
		if( threats.Size() > 0 )
		{
			chosenIndex = RollInteger( threats.Size(), 0 );
			chosenThreat = ( ( GameObject )( threats[ chosenIndex ].entity ) );
			ScriptExecutionContext.SetMappingValue( context, m_outThreatArgument, chosenThreat );
		}
	}

}

class GetRandomPositionAroundPoint extends AIRandomTasks
{
	editable inlined var m_originPoint : AIArgumentMapping;
	editable inlined var m_distanceMin : AIArgumentMapping;
	editable inlined var m_distanceMax : AIArgumentMapping;
	editable inlined var m_angleMin : AIArgumentMapping;
	editable inlined var m_angleMax : AIArgumentMapping;
	editable inlined var m_outPositionArgument : AIArgumentMapping;
	protected var m_finalPosition : Vector4;

	protected override function Activate( context : ScriptExecutionContext )
	{
		var originObject : weak< GameObject >;
		var originPosition : Vector4;
		var originOrientation : Quaternion;
		var finalPosition : Vector4;
		var chosenAngle : Float;
		var chosenDistance : Float;
		var chosenOrientation : Quaternion;
		originObject = ( ( GameObject )( ScriptExecutionContext.GetMappingValue( context, m_originPoint ) ) );
		if( originObject )
		{
			originOrientation = originObject.GetWorldOrientation();
		}
		else
		{
			originPosition = ( ( Vector4 )( ScriptExecutionContext.GetMappingValue( context, m_originPoint ) ) );
			Quaternion.SetIdentity( originOrientation );
		}
		if( !( originObject ) && Vector4.IsZero( originPosition ) )
		{
			return;
		}
		chosenAngle = RandRangeF( ( ( Float )( ScriptExecutionContext.GetMappingValue( context, m_angleMin ) ) ), ( ( Float )( ScriptExecutionContext.GetMappingValue( context, m_angleMax ) ) ) );
		chosenDistance = RandRangeF( ( ( Float )( ScriptExecutionContext.GetMappingValue( context, m_distanceMin ) ) ), ( ( Float )( ScriptExecutionContext.GetMappingValue( context, m_distanceMax ) ) ) );
		Quaternion.SetIdentity( chosenOrientation );
		Quaternion.SetZRot( chosenOrientation, Deg2Rad( chosenAngle ) );
		chosenOrientation *= originOrientation;
		if( originObject )
		{
			finalPosition = originObject.GetWorldPosition() + ( Quaternion.GetForward( chosenOrientation ) * chosenDistance );
		}
		else
		{
			finalPosition = originPosition + ( Quaternion.GetForward( chosenOrientation ) * chosenDistance );
		}
		ScriptExecutionContext.SetMappingValue( context, m_outPositionArgument, finalPosition );
	}

}

