Quote:
Originally Posted by Flashman
I have a simple request: I want to be able to set a command to an AI object or group (be it aircraft, vehicles or ships.... anything AI) that will automatically despawn it. This could after a set time and/or when it reaches a set waypoint.
|
I think I've got something pretty similar to this working. This whole thing is base on a method called OnActorTaskCompleted(). This is called whenever an actor completes it's well... task. I can't tell you exactly what an actor is, if it includes human players or just AI, and I can't tell you exactly what a task is.
I can tell you that this method is called when a Spitfire and a tank reach their final waypoints.
Anyway, here's the script. This is the code that I wrote. You won't have this in your script, although you might have some code in the OnActorTaskCompleted() method.
Code:
public override void OnActorTaskCompleted(int missionNumber, string shortName, AiActor actor)
{
base.OnActorTaskCompleted(missionNumber, shortName, actor);
//destroy it if it's an aircraft
damageAiControlledPlane(actor);
//destroy it if it's a grond object
destroyGroundActor(actor);
}
private void destroyGroundActor(AiActor actor)
{
//loop through all of the ground groups from both armies
foreach (AiGroundGroup g in GamePlay.gpGroundGroups(1))
{
//if we find our actor in the ground group...
if (g.Name() == actor.Name())
{
//Destroy them! Destroy them all!
foreach (AiActor a in g.GetItems())
(a as AiGroundActor).Destroy();
}
}
foreach (AiGroundGroup g in GamePlay.gpGroundGroups(2))
{
//if we find our actor in the ground group...
if (g.Name() == actor.Name())
{
//Destroy them! Destroy them all!
foreach (AiActor a in g.GetItems())
(a as AiGroundActor).Destroy();
}
}
}
My code also depends on some of the code in this script.
http://forum.1cpublishing.eu/showpos...0&postcount=92
You probably already have this because this is the code people are using to destroy aircraft that players abandon.
And finally, here's the script in it's entirety so you can see how it all fits together.
Code:
using System;
using System.Collections;
using maddox.game;
using maddox.game.world;
using System.Diagnostics;
public class Mission : AMission
{
public override void OnActorTaskCompleted(int missionNumber, string shortName, AiActor actor)
{
base.OnActorTaskCompleted(missionNumber, shortName, actor);
//destroy it if it's an aircraft
damageAiControlledPlane(actor);
//destroy it if it's a grond object
destroyGroundActor(actor);
}
private void destroyGroundActor(AiActor actor)
{
//loop through all of the ground groups from both armies
foreach (AiGroundGroup g in GamePlay.gpGroundGroups(1))
{
//if we find our actor in the ground group...
if (g.Name() == actor.Name())
{
//Destroy them! Destroy them all!
foreach (AiActor a in g.GetItems())
(a as AiGroundActor).Destroy();
}
}
foreach (AiGroundGroup g in GamePlay.gpGroundGroups(2))
{
//if we find our actor in the ground group...
if (g.Name() == actor.Name())
{
//Destroy them! Destroy them all!
foreach (AiActor a in g.GetItems())
(a as AiGroundActor).Destroy();
}
}
}
private bool isAiControlledPlane(AiAircraft aircraft)
{
if (aircraft == null)
{
return false;
}
Player[] players = GamePlay.gpRemotePlayers();
foreach (Player p in players)
{
if (p != null && (p.Place() is AiAircraft) && (p.Place() as AiAircraft) == aircraft)
{
return false;
}
}
return true;
}
private void damageAiControlledPlane(AiActor actor)
{
if (actor == null || !(actor is AiAircraft))
{
return;
}
AiAircraft aircraft = (actor as AiAircraft);
if (!isAiControlledPlane(aircraft))
{
return;
}
if (aircraft == null)
{
return;
}
aircraft.hitNamed(part.NamedDamageTypes.ControlsElevatorDisabled);
aircraft.hitNamed(part.NamedDamageTypes.ControlsAileronsDisabled);
aircraft.hitNamed(part.NamedDamageTypes.ControlsRudderDisabled);
aircraft.hitNamed(part.NamedDamageTypes.FuelPumpFailure);
aircraft.hitNamed(part.NamedDamageTypes.Eng0TotalFailure);
aircraft.hitNamed(part.NamedDamageTypes.Eng1TotalFailure);
Timeout(300, () =>
{ destroyPlane(aircraft); }
);
}
private void destroyPlane(AiAircraft aircraft)
{
if (aircraft != null)
{
aircraft.Destroy();
}
}
}
I hope this helps.