importonly abstract class IDynamicSpawnSystem extends IGameSystem
{
}

importonly struct DSSSpawnRequestResult
{
	import var requestID : Uint32;
	import var success : Bool;
	import var spawnedObjects : array< weak< GameObject > >;
}

import class DynamicSpawnSystem extends IDynamicSpawnSystem
{
	public import function GetNumberOfSpawnedUnits() : Int32;
	public import function IsEntityRegistered( id : EntityID ) : Bool;
	public import function IsInUnmountingRange( position : Vector3 ) : Bool;

	protected function SpawnRequestFinished( requestResult : DSSSpawnRequestResult )
	{
		var i : Int32;
		var spawnedObject : GameObject;
		var nullArrayOfNames : array< CName >;
		var aiInjectCombatThreatCommand : AIInjectCombatThreatCommand;
		if( !( requestResult.success ) )
		{
			return;
		}
		for( i = 0; i < requestResult.spawnedObjects.Size(); i += 1 )
		{
			spawnedObject = requestResult.spawnedObjects[ i ];
			if( !( spawnedObject.IsPuppet() ) )
			{
				continue;
			}
			aiInjectCombatThreatCommand = new AIInjectCombatThreatCommand;
			aiInjectCombatThreatCommand.targetPuppetRef = CreateEntityReference( "#player", nullArrayOfNames );
			aiInjectCombatThreatCommand.duration = 120.0;
			AIComponent.SendCommand( ( ( ScriptedPuppet )( spawnedObject ) ), aiInjectCombatThreatCommand );
		}
	}

	protected function SpawnCallback( spawnedObject : GameObject )
	{
		var aiVehicleChaseCommand : AIVehicleChaseCommand;
		var aiCommandEvent : AICommandEvent;
		var wheeledObject : WheeledObject;
		var player : GameObject;
		player = GameInstance.GetPlayerSystem( GetGameInstance() ).GetLocalPlayerMainGameObject();
		if( !( spawnedObject ) )
		{
			return;
		}
		if( spawnedObject.IsPuppet() )
		{
			ChangeAttitude( spawnedObject, player, EAIAttitude.AIA_Hostile );
		}
		else if( spawnedObject.IsVehicle() )
		{
			aiVehicleChaseCommand = new AIVehicleChaseCommand;
			aiVehicleChaseCommand.target = player;
			aiVehicleChaseCommand.distanceMin = TDB.GetFloat( T"DynamicSpawnSystem.dynamic_vehicles_chase_setup.distanceMin", 3.0 );
			aiVehicleChaseCommand.distanceMax = TDB.GetFloat( T"DynamicSpawnSystem.dynamic_vehicles_chase_setup.distanceMax", 10.0 );
			aiVehicleChaseCommand.forcedStartSpeed = 10.0;
			aiVehicleChaseCommand.ignoreChaseVehiclesLimit = true;
			aiVehicleChaseCommand.boostDrivingStats = true;
			aiCommandEvent = new AICommandEvent;
			aiCommandEvent.command = aiVehicleChaseCommand;
			wheeledObject = ( ( WheeledObject )( spawnedObject ) );
			wheeledObject.SetPoliceStrategyDestination( player.GetWorldPosition() );
			wheeledObject.QueueEvent( aiCommandEvent );
			wheeledObject.GetAIComponent().SetInitCmd( aiVehicleChaseCommand );
		}
	}

	private final function ChangeAttitude( owner : weak< GameObject >, target : weak< GameObject >, desiredAttitude : EAIAttitude )
	{
		var attitudeOwner : AttitudeAgent;
		var attitudeTarget : AttitudeAgent;
		if( !( owner ) || !( target ) )
		{
			return;
		}
		attitudeOwner = owner.GetAttitudeAgent();
		attitudeTarget = target.GetAttitudeAgent();
		if( !( attitudeOwner ) || !( attitudeTarget ) )
		{
			return;
		}
		attitudeOwner.SetAttitudeGroup( 'hostile' );
		attitudeOwner.SetAttitudeTowards( attitudeTarget, desiredAttitude );
	}

}

class IsNPCInCourier extends AIbehaviorconditionScript
{

	protected override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var dynamicSpawnSystem : DynamicSpawnSystem;
		dynamicSpawnSystem = GameInstance.GetDynamicSpawnSystem( ScriptExecutionContext.GetOwner( context ).GetGame() );
		return dynamicSpawnSystem.IsEntityRegistered( ScriptExecutionContext.GetOwner( context ).GetEntityID() );
	}

}

