class SimpleCombatConditon extends AIbehaviorconditionScript
{
	var m_firstCoverEvaluationDone : Bool;
	var m_takeCoverAbility : GameplayAbility_Record;
	var m_quickhackAbility : GameplayAbility_Record;

	protected export override function Activate( context : ScriptExecutionContext )
	{
		m_takeCoverAbility = TweakDBInterface.GetGameplayAbilityRecord( T"Ability.CanUseCovers" );
		m_quickhackAbility = TweakDBInterface.GetGameplayAbilityRecord( T"Ability.CanQuickhack" );
		SetUpdateInterval( context, 2.0 );
	}

	protected export override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var player : GameObject;
		var squadInterface : weak< SquadScriptInterface >;
		var squadMembers : array< weak< Entity > >;
		var squadMember : ScriptedPuppet;
		var aiComponent : AIHumanComponent;
		var activeCommandCount : Int32;
		var squadMatesInSimpleCombat : Int32;
		var puppet : ScriptedPuppet;
		var playerPos : Vector4;
		var membersCount : Int32;
		var distanceSqr : Float;
		var minDistSqr1 : Float;
		var minDistSqr2 : Float;
		var minDist1Index : Int32;
		var minDist2Index : Int32;
		var i : Int32;
		puppet = GetPuppet( context );
		minDistSqr1 = 999999.0;
		minDistSqr2 = 999999.0;
		minDist1Index = -1;
		minDist2Index = -1;
		player = GameInstance.GetPlayerSystem( ScriptExecutionContext.GetOwner( context ).GetGame() ).GetLocalPlayerControlledGameObject();
		if( !( player ) )
		{
			return true;
		}
		if( !( puppet.IsActive() ) )
		{
			return false;
		}
		if( ScriptedPuppet.IsPlayerCompanion( puppet ) )
		{
			return false;
		}
		if( AnimationInProgress( puppet ) )
		{
			return false;
		}
		aiComponent = puppet.GetAIControllerComponent();
		if( AIActionHelper.CheckAbility( puppet, m_takeCoverAbility ) )
		{
			if( !( m_firstCoverEvaluationDone ) && AICoverHelper.GetCoverBlackboard( puppet ).GetBool( GetAllBlackboardDefs().AICover.firstCoverEvaluationDone ) )
			{
				m_firstCoverEvaluationDone = true;
			}
			if( !( m_firstCoverEvaluationDone ) )
			{
				return false;
			}
			else if( HasAvailableCover( context ) && !( AICoverHelper.IsCurrentlyInSmartObject( puppet ) ) )
			{
				return false;
			}
			if( AIActionHelper.IsCurrentlyInCoverAttackAction( puppet ) )
			{
				if( aiComponent.GetCombatMode() == AIbehaviorCombatModes.Default )
				{
					return false;
				}
				else
				{
					return true;
				}
			}
		}
		if( AIActionHelper.CheckAbility( puppet, m_quickhackAbility ) )
		{
			return false;
		}
		if( aiComponent )
		{
			if( aiComponent.IsCommandActive( 'AICommand' ) )
			{
				activeCommandCount = aiComponent.GetActiveCommandsCount();
				if( aiComponent.IsCommandActive( 'AIInjectCombatTargetCommand' ) )
				{
					activeCommandCount -= 1;
				}
				if( aiComponent.IsCommandActive( 'AIInjectCombatThreatCommand' ) )
				{
					activeCommandCount -= 1;
				}
				if( activeCommandCount > 0 )
				{
					return false;
				}
			}
		}
		squadInterface = puppet.GetSquadMemberComponent().MySquad( AISquadType.Combat );
		if( !( squadInterface ) )
		{
			return true;
		}
		squadMembers = squadInterface.ListMembersWeak();
		playerPos = player.GetWorldPosition();
		membersCount = squadMembers.Size();
		for( i = 0; i < membersCount; i += 1 )
		{
			squadMember = ( ( ScriptedPuppet )( squadMembers[ i ] ) );
			if( !( squadMember ) )
			{
				continue;
			}
			if( AIScriptSquad.HasOrder( squadMember, 'SimpleCombat' ) )
			{
				squadMatesInSimpleCombat += 1;
			}
			distanceSqr = Vector4.DistanceSquared( squadMembers[ i ].GetWorldPosition(), playerPos );
			if( distanceSqr < minDistSqr2 )
			{
				if( distanceSqr < minDistSqr1 )
				{
					minDistSqr2 = minDistSqr1;
					minDist2Index = minDist1Index;
					minDistSqr1 = distanceSqr;
					minDist1Index = i;
				}
				else
				{
					minDistSqr2 = distanceSqr;
					minDist2Index = i;
				}
			}
		}
		if( ( ( minDist1Index >= 0 ) && ( squadMembers[ minDist1Index ] == puppet ) ) || ( ( minDist2Index >= 0 ) && ( squadMembers[ minDist2Index ] == puppet ) ) )
		{
			return false;
		}
		if( ( ( membersCount - squadMatesInSimpleCombat ) < 2 ) && !( AIScriptSquad.HasOrder( puppet, 'SimpleCombat' ) ) )
		{
			return false;
		}
		return true;
	}

	private function AnimationInProgress( puppet : weak< ScriptedPuppet > ) : Bool
	{
		if( puppet.GetMovePolicesComponent().IsOnOffMeshLink() )
		{
			return true;
		}
		if( GameInstance.GetCoverManager( puppet.GetGame() ).IsEnteringOrLeavingCover( puppet ) )
		{
			return true;
		}
		return false;
	}

	public static function HasAvailableCover( const context : ScriptExecutionContext ) : Bool
	{
		var puppet : weak< ScriptedPuppet >;
		var msc : weak< MultiSelectCovers >;
		var currentRing : gamedataAIRingType;
		var i : Int32;
		puppet = ( ( weak< weak< ScriptedPuppet > > )( ScriptExecutionContext.GetOwner( context ) ) );
		if( !( puppet ) )
		{
			return false;
		}
		msc = ( ( weak< weak< MultiSelectCovers > > )( ScriptExecutionContext.GetArgumentScriptable( context, 'MultiCoverID' ) ) );
		if( !( msc ) )
		{
			return false;
		}
		currentRing = AISquadHelper.GetCurrentSquadRing( ( ( weak< weak< ScriptedPuppet > > )( ScriptExecutionContext.GetOwner( context ) ) ) );
		if( currentRing == gamedataAIRingType.Invalid )
		{
			return true;
		}
		for( i = 0; i < msc.selectedCovers.Size(); i += 1 )
		{
			if( !( msc.coversUseLOS[ i ] ) )
			{
				continue;
			}
			if( currentRing != msc.coverRingTypes[ i ] )
			{
				continue;
			}
			if( msc.selectedCovers[ i ] > ( ( Uint64 )( 0 ) ) )
			{
				return true;
			}
		}
		return false;
	}

}

