Despawner, I have taken the original code and added reset timers, many of the current users use hard code, if you leave the aircraft the plane despawns. The issue for me with this is in the case of controlling a flight of planes. Eg taking a flight of light bombers to a target and swap from plane to plane to bomb the area then take them home again. Why not!
Anyway I set a timer running on creation, on place leave and added on task completed. I actually have 3 timer periods, set via the editable inputs near start in constants and parameters section. I also added ground object removal for objects after the first base mission. The default is 45mins so be aware of what is going on.
Code:
/*=========================================*/
#region Despawner
/*
despawner timer added to original code, sets overall time period for actors,
Advantage is, if delayed, say trying to land behind masses of planes, actors despawn at timer period set anyway.
allows for players to fly groups of planes,
Player can re eneter flights or groups, as bombers take them to target, use each at target then fly group home
despawn only triggers once player leaves whole group
*/
public override void OnActorCreated(int missionNumber, string shortName, AiActor actor)
{
base.OnActorCreated(missionNumber, shortName, actor);
// note despawntime (set as varible at start of cs)
if (actor is AiGroundActor && missionNumber > 0)//main mission is left alone
//if ((actor as AiGroundActor).Type() != maddox.game.world.AiGroundActorType.AAGun)//all ai ground except aa guns
Timeout(despawntime3, () =>
{
if (actor != null)
{ (actor as AiGroundActor).Destroy(); }
});
if (actor is AiAircraft)
Timeout(despawntime, () => { damageAiControlledPlane(actor); });
}
public override void OnPlaceLeave(Player player, AiActor actor, int placeIndex)
{
base.OnPlaceLeave(player, actor, placeIndex);
Timeout(despawntime, () => { damageAiControlledPlane(actor); });
}
// Path completion despawner, when flight path ends despawner action triggered
// note:final despawner detects players in group
public override void OnActorTaskCompleted(int missionNumber, string shortName, AiActor actor)
{
base.OnActorTaskCompleted(missionNumber, shortName, actor);
if (actor is AiGroundActor)
if (actor != null)
Timeout(despawntime2, () => { (actor as AiGroundActor).Destroy(); });
Timeout(despawntime2, () => { damageAiControlledPlane(actor); });
}
//check if a player is in any of the "places"
private bool isAiControlledPlane(AiAircraft aircraft)
{
if (aircraft == null) return false;
for (int i = 0; i < aircraft.Places(); i++)
if (aircraft.Player(i) != null)
return false;
return true;
}
private void destroyPlane(AiAircraft aircraft)
{
if (aircraft != null)
aircraft.Destroy();
}
//actual Aiaircraft despawner, note it polls player detection
private void damageAiControlledPlane(AiActor actorMain)
{
//Timeout(1, () => { STxA(-1, "Despawn time completes for Actor, Despawning"); });//indicates action about to happen, rem out after mission checks complete
foreach (AiActor actor in actorMain.Group().GetItems())
{
if (actor == null || !(actor is AiAircraft))
return;
AiAircraft aircraft = (actor as AiAircraft);
if (!isAiControlledPlane(aircraft))
return;
if (aircraft == null)
return;
(actor as AiAircraft).Destroy();
}
}
#endregion
/*=========================================*/