class HorizontalLineTrace extends AIbehaviorconditionScript
{
	editable inlined var m_source : AIArgumentMapping;
	editable inlined var m_target : AIArgumentMapping;
	editable inlined var m_offset : AIArgumentMapping;
	editable inlined var m_length : AIArgumentMapping;
	editable inlined var m_azimuth : AIArgumentMapping;

	protected export override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var source : weak< GameObject >;
		var sourcePosition : Vector4;
		var sourceOrientation : Quaternion;
		var target : weak< GameObject >;
		var targetPosition : Vector4;
		var targetDirection : Vector4;
		var tracePosition : Vector4;
		var traceOffset : Vector4;
		var traceLength : Float;
		var traceAzimuth : Float;
		var traceOrientation : Quaternion;
		var traceAngles : EulerAngles;
		var sourceIsOwner : Bool;
		sourceIsOwner = false;
		source = ( ( weak< weak< GameObject > > )( ScriptExecutionContext.GetMappingValue( context, m_source ) ) );
		target = ( ( weak< weak< GameObject > > )( ScriptExecutionContext.GetMappingValue( context, m_target ) ) );
		traceOffset = ( ( Vector4 )( ScriptExecutionContext.GetMappingValue( context, m_offset ) ) );
		traceLength = ( ( Float )( ScriptExecutionContext.GetMappingValue( context, m_length ) ) );
		traceAzimuth = ( ( Float )( ScriptExecutionContext.GetMappingValue( context, m_azimuth ) ) );
		if( source )
		{
			sourcePosition = source.GetWorldPosition();
			sourceOrientation = source.GetWorldOrientation();
		}
		else if( m_source.GetArgumentType() == AIArgumentType.Vector )
		{
			sourcePosition = ( ( Vector4 )( ScriptExecutionContext.GetMappingValue( context, m_source ) ) );
		}
		else
		{
			sourcePosition = ScriptExecutionContext.GetOwner( context ).GetWorldPosition();
			sourceOrientation = ScriptExecutionContext.GetOwner( context ).GetWorldOrientation();
			sourceIsOwner = true;
		}
		targetPosition = sourcePosition;
		if( target )
		{
			targetPosition = target.GetWorldPosition();
		}
		else if( m_target.GetArgumentType() == AIArgumentType.Vector )
		{
			targetPosition = ( ( Vector4 )( ScriptExecutionContext.GetMappingValue( context, m_target ) ) );
		}
		else if( m_target.GetArgumentType() == AIArgumentType.Object && !( sourceIsOwner ) )
		{
			targetPosition = ScriptExecutionContext.GetOwner( context ).GetWorldPosition();
		}
		if( Vector4.Distance( sourcePosition, targetPosition ) > 0.001 )
		{
			targetDirection = targetPosition - sourcePosition;
			targetDirection.Z = 0.0;
			Vector4.Normalize( targetDirection );
			if( !( Vector4.IsZero( targetDirection ) ) )
			{
				sourceOrientation = Quaternion.BuildFromDirectionVector( targetDirection, Vector4( 0.0, 0.0, 1.0, 0.0 ) );
			}
			if( traceLength == 0.0 )
			{
				traceLength = Vector4.Distance( sourcePosition, targetPosition );
			}
		}
		if( traceLength == 0.0 )
		{
			return AIbehaviorConditionOutcomes.False;
		}
		traceAngles.Yaw = traceAzimuth;
		traceOrientation = sourceOrientation * EulerAngles.ToQuat( traceAngles );
		tracePosition = sourcePosition + ( traceOrientation * traceOffset );
		if( LineTrace( context, tracePosition, traceOrientation, traceLength ) )
		{
			return AIbehaviorConditionOutcomes.True;
		}
		return AIbehaviorConditionOutcomes.False;
	}

	private function LineTrace( context : ScriptExecutionContext, queryPosition : Vector4, queryOrientation : Quaternion, queryLength : Float ) : Bool
	{
		var spatialQueriesSystem : SpatialQueriesSystem;
		var queryFilter : QueryFilter;
		var queryEnd : Vector4;
		var queryResult : TraceResult;
		QueryFilter.AddGroup( queryFilter, 'Static' );
		QueryFilter.AddGroup( queryFilter, 'Dynamic' );
		QueryFilter.AddGroup( queryFilter, 'Terrain' );
		QueryFilter.AddGroup( queryFilter, 'Collider' );
		QueryFilter.AddGroup( queryFilter, 'Destructible' );
		spatialQueriesSystem = GameInstance.GetSpatialQueriesSystem( ScriptExecutionContext.GetOwner( context ).GetGame() );
		queryEnd = queryPosition + ( queryOrientation * Vector4( 0.0, queryLength, 0.0, 0.0 ) );
		return spatialQueriesSystem.SyncRaycastByQueryFilter( queryPosition, queryEnd, queryFilter, queryResult );
	}

}

