class SetFactBasedOnClearAreaEffector extends Effector
{
	var m_fact : CName;
	var m_factSuffixes : array< CName >;
	var m_vectorRotations : array< Float >;
	var m_value : Int32;
	var m_distance : Float;
	var m_width : Float;
	var m_fromHeight : Float;
	var m_height : Float;

	protected override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		m_fact = TDB.GetCName( record + T".fact" );
		m_factSuffixes = TDB.GetCNameArray( record + T".factSuffixes" );
		m_vectorRotations = TDB.GetFloatArray( record + T".vectorRotations" );
		m_fact = TDB.GetCName( record + T".fact" );
		m_value = TDB.GetInt( record + T".value" );
		m_distance = TDB.GetFloat( record + T".distance" );
		m_width = TDB.GetFloat( record + T".width" );
		m_fromHeight = TDB.GetFloat( record + T".fromHeight" );
		m_height = TDB.GetFloat( record + T".height" );
	}

	protected override function ActionOn( owner : GameObject )
	{
		var i : Int32;
		var factName : String;
		for( i = 0; ( i < m_factSuffixes.Size() ) && ( i < m_vectorRotations.Size() ); i += 1 )
		{
			factName = NameToString( m_fact ) + NameToString( m_factSuffixes[ i ] );
			GameInstance.GetQuestsSystem( owner.GetGame() ).SetFactStr( factName, 0 );
		}
		if( owner.IsPlayer() )
		{
			for( i = 0; ( i < m_factSuffixes.Size() ) && ( i < m_vectorRotations.Size() ); i += 1 )
			{
				factName = NameToString( m_fact ) + NameToString( m_factSuffixes[ i ] );
				if( HasSpaceInFront( owner, m_vectorRotations[ i ] ) )
				{
					GameInstance.GetQuestsSystem( owner.GetGame() ).SetFactStr( factName, m_value );
					return;
				}
			}
		}
		else
		{
			SetFactBasedOnObjectAndPlayerRelation( owner );
		}
	}

	private function HasSpaceInFront( owner : GameObject, rotation : Float ) : Bool
	{
		var currentDirection : Vector4;
		currentDirection = owner.GetWorldForward();
		currentDirection = Vector4.RotByAngleXY( currentDirection, rotation );
		return SpatialQueriesHelper.HasSpaceInFront( owner, currentDirection, m_fromHeight, m_width, m_distance, m_height );
	}

	private function SetFactBasedOnObjectAndPlayerRelation( owner : GameObject )
	{
		var playerPuppet : PlayerPuppet;
		var playerCurrentDir : Vector4;
		var playerCurrentPos : Vector4;
		var ownerCurrentPos : Vector4;
		var dirToTarget : Vector4;
		var angleBetweenObjects : Float;
		var angleResolution : Float;
		var angleIdx : Int32;
		var factName : String;
		var crossProd : Vector4;
		playerPuppet = ( ( PlayerPuppet )( GameInstance.GetPlayerSystem( owner.GetGame() ).GetLocalPlayerMainGameObject() ) );
		if( ( !( playerPuppet ) || ( m_vectorRotations.Size() < 2 ) ) || ( m_vectorRotations.Size() != m_factSuffixes.Size() ) )
		{
			return;
		}
		playerCurrentDir = playerPuppet.GetWorldForward();
		playerCurrentPos = playerPuppet.GetWorldPosition();
		playerCurrentPos.Z = 0.0;
		ownerCurrentPos = owner.GetWorldPosition();
		ownerCurrentPos.Z = 0.0;
		dirToTarget = ownerCurrentPos - playerCurrentPos;
		angleBetweenObjects = Vector4.GetAngleBetween( playerCurrentDir, dirToTarget );
		crossProd = Vector4.Cross( playerCurrentDir, dirToTarget );
		if( crossProd.Z > 0.0 )
		{
			angleBetweenObjects = 360.0 - angleBetweenObjects;
		}
		angleResolution = m_vectorRotations[ 1 ] - m_vectorRotations[ 0 ];
		angleIdx = RoundF( angleBetweenObjects / angleResolution ) % m_factSuffixes.Size();
		if( angleIdx < m_factSuffixes.Size() )
		{
			factName = NameToString( m_fact ) + NameToString( m_factSuffixes[ angleIdx ] );
			GameInstance.GetQuestsSystem( playerPuppet.GetGame() ).SetFactStr( factName, m_value );
		}
	}

}

