PDA

View Full Version : Script for remove AI aircraft.



1lokos
Jun-03-2013, 18:43
I set a SP mission that require a AI plane landing and when "alerted" by player, done a fly-by at 50m above runway, take-off again.

The take-off part work OK (in game script).
But after landing the IA aircraft do a taxi for a far parking point, where I place a camouflage net to hide him, since the new respawn occurs in the next near spawn point, also hide by camouflage net. Crude solution but work. :D

The trouble is: the taxing to parking point and from the respawn point to runway end take 5 or 6 minutes... which undermines the mission progress.

I find this script that supposedly remove AI aircraft after landing.


public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftLanded

double DespawnAfter = 1 * 60; // seconds after landing
Timeout(DespawnAfter, (60) => // despawn after specified seconds
{
try { aircraft.Destroy(); }
catch { }
});
}

I save this code with mission name, it appears in the FMB script editor... but dont work..

What I need change in it to remove a specific aircraft, in case a Bf-108B-2?

Sokol1

Salmo
Jun-07-2013, 04:25
Try this as your script file.



using System;
using maddox.game;
using maddox.game.world;
using maddox.GP;

public class Mission : AMission
{
public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftLanded

double DespawnAfter = 1 * 60; // seconds after landing
Timeout(DespawnAfter, () => // despawn after specified seconds
{
try { aircraft.Destroy(); }
catch { }
});
}
}

1lokos
Jun-09-2013, 12:23
I try but still don't work, the plane (108) land and did a taxi to parking position where's remains.

These script (in game trigger) thing are crazy: I made crash landing on the area for spawn trigger, so the new plane spawn an take off, then another - with the same squadron letters spawn and take off... :doh:

Sokol1

Salmo
Jun-09-2013, 22:27
I try but still don't work, the plane (108) land and did a taxi to parking position where's remains.

These script (in game trigger) thing are crazy: I made crash landing on the area for spawn trigger, so the new plane spawn an take off, then another - with the same squadron letters spawn and take off... :doh:

Sokol1

The OnAircraftLanded event is not triggered when the aircraft actually lands. It's triggered when the aircraft finishes taxiing & comes to a stop. So in the script above, the aircraft will not despawn until 60 seconds after it finishes taxiing & comes to a stop.

To remove planes immediately after they finish taxiing to a spawn point .....


using System;
using maddox.game;
using maddox.game.world;
using maddox.GP;

public class Mission : AMission
{
public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftLanded

double DespawnAfter = 1; // 1 second after landing & taxiing to spawn point
Timeout(DespawnAfter, () => // despawn after specified seconds
{
try { aircraft.Destroy(); }
catch { }
});
}
}


Alternatively, here's a little more sophisticated script. Here's how it works ...

1. It records all AI aircraft that takeoff in a list (the AirborneAiPlanes list).
2. It tests each AI aircraft in the list every 30 seconds to see if the aircraft is on (just above) the ground.
3. If the Ai aircraft is on the ground AND it's in the list of aircraft that have taken off, then it's assumed to be a landing aircraft. The script will wait 10 seconds then remove the AI aircraft from the battle.
4. This should result in AI aircraft being despawned very soon after landing, and overcome the problem of the OnAircraftLanded event not being triggered until after the aircraft has taxied all the way to a spawn point.



using System;
using maddox.game;
using maddox.game.world;
using System.Collections;
using System.Collections.Generic;
using maddox.GP;


public class Mission : AMission
{
List<AiAircraft> AirborneAiPlanes = new List<AiAircraft>();

public override void OnAircraftTookOff(int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftTookOff(missionNumber, shortName, aircraft);

if (isAiControlledPlane(aircraft)) // aircraft that tookoff is an AI controlled plane
{
if (!AirborneAiPlanes.Contains(aircraft)) // plane is not in list of airborne AI planes
{
AirborneAiPlanes.Add(aircraft); // add aircraft to list of airborne AI planes
}
}
}

public override void OnTickGame()
{
base.OnTickGame();

if (Time.tickCounter() % (32 * 60 * 0.5) == 0) // every 30 seconds
{
if (AirborneAiPlanes.Count > 0) // there are airborne AI planes in the battle
{
foreach (AiAircraft aircraft in AirborneAiPlanes) // look at each AI aircraft that has taken off
{
if (aircraft.getParameter(ParameterTypes.Z_AltitudeAG L, -1) < 0.01) // what's the altitude Above Ground Level
{
if (AirborneAiPlanes.Contains(aircraft)) // the aircraft has previously taken off
{
Timeout(10.0, () => // wait 10 seconds
{
AirborneAiPlanes.Remove(aircraft); // remove aircraft from the airborne AI aircraft list
aircraft.Destroy(); // remove aircraft from battle
});
}
}
}
}
}
}

private bool isAiControlledPlane(AiAircraft aircraft)
#region Returns whether aircraft is an Ai plane (no humans in any seats)
{ // returns true if specified aircraft is AI controlled with no humans aboard, otherwise false
if (aircraft == null) return false;
//check if a player is in any of the "places"
for (int i = 0; i < aircraft.Places(); i++)
{
if (aircraft.Player(i) != null) return false;
}
return true;
}
#endregion
}

Sods
Jul-30-2013, 19:34
Hi Salmo i just saw this script and at the moment not able to test my qestion, but if the line ....

if (aircraft.getParameter(ParameterTypes.Z_AltitudeAG L, -1) < 0.01) // what's the altitude Above Ground Level

... were to read....

if (aircraft.getParameter(ParameterTypes.Z_AltitudeAG L, -1) <> 0.00) // what's the altitude, Above or Below Ground Level?

would it have the same effect, the reason why i ask is if an aircraft land on water thats un damaged, when the player leaves their aircraft, the script i have the aircraft remins but not always :(.
was just wondering if its a go before i tried :)

92 Sqn Sods

Salmo
Jul-31-2013, 05:38
Hi Salmo i just saw this script and at the moment not able to test my qestion, but if the line ....

if (aircraft.getParameter(ParameterTypes.Z_AltitudeAG L, -1) < 0.01) // what's the altitude Above Ground Level

... were to read....

if (aircraft.getParameter(ParameterTypes.Z_AltitudeAG L, -1) <> 0.00) // what's the altitude, Above or Below Ground Level?

would it have the same effect, the reason why i ask is if an aircraft land on water thats un damaged, when the player leaves their aircraft, the script i have the aircraft remins but not always :(.
was just wondering if its a go before i tried :)

92 Sqn Sods

Firsty, you could try if (aircraft.getParameter(ParameterTypes.Z_AltitudeAG L, -1) == 0.00) // what's the altitude Above Ground Level in the existing script. It really shouldn't make any difference, since this will test if the aircraft altitude above ground level (AGL) is exactly zero.

However, if i read your question correctly you want to remove a human piloted aircraft if it lands on water & the human pilot bails out? Is this correct? This is a different problem to solve.

Sods
Aug-02-2013, 19:29
However, if i read your question correctly you want to remove a human piloted aircraft if it lands on water & the human pilot bails out? Is this correct? This is a different problem to solve.

Hi Salmo, yes the above is correct. any help would be greatly appreciated. :)


92 Sqn Sods.

Sods
Aug-05-2013, 19:01
I have tried this and seems to work for non AI..


public override void OnPlaceLeave(Player p, AiActor actor, int placeIndex)
{
base.OnPlaceLeave(p, actor, placeIndex);
if (isSafeDestroyOnPlaceLeave)
{
AiAircraft aircraft = actor as AiAircraft;
DestroyPLAircraft(aircraft);
}
}

and for AI I have added the line

if (aircraft.getParameter(ParameterTypes.Z_AltitudeAG L, -1) == 0.00

and seems ok!

Thx Salmo you made me think LOL

1lokos
Sep-13-2017, 13:27
LOL - Four years after I look at this again, and this time manage do make things work. :-)

AI Aircraft despawn 1 second after landing and taxi for parking area. After the "trigger1" spawn a "copy" of this aircraft for takeoff.



using System;
using maddox.game;
using maddox.game.world;
using System.Collections;
using System.Collections.Generic;
using maddox.GP;

public class Mission : AMission
{
public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftLanded(missionNumber, shortName, aircraft);

double DespawnAfter = 1; // seconds after landing
Timeout(DespawnAfter, () => // despawn after specified seconds
{
try { aircraft.Destroy(); }
catch { }
});
}
public override void OnTrigger(int missionNumber, string shortName, bool active)
{
base.OnTrigger(missionNumber, shortName, active);

if ("trigger1".Equals(shortName) && active)
{
AiAction action = GamePlay.gpGetAction("trigger1"); //same as YourTriggerName
GamePlay.gpLogServer(null, "Triggered ", new object[] { }); //Testmessage
if (action != null)
{
action.Do();
}
GamePlay.gpGetTrigger(shortName).Enable = false;
}
}
}

1lokos
Sep-23-2020, 14:40
More three years goes by... :D

For remove an specific aircraft - an not all that landing, use 'shortName' (FMB > View > Object Name -1 plus a 0 at right).



using System;
using maddox.game;
using maddox.game.world;
using System.Collections;
using System.Collections.Generic;
using maddox.GP;

public class Mission : AMission
{
public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftLanded(missionNumber, shortName, aircraft);

if (shortName.Equals("BoB_LW_Wekusta_51.010")) //despawn just this specif AI plane
{
double DespawnAfter = 1; // seconds after landing
Timeout(DespawnAfter, () => // despawn after specified seconds
{
try { aircraft.Destroy(); }
catch { }
});
}
}
}

ATAG_Oskar
Sep-23-2020, 16:11
More three years goes by... :D

For remove an specific aircraft - an not all that landing, use 'shortName' (FMB > View > Object Name -1 plus a 0 at right).



using System;
using maddox.game;
using maddox.game.world;
using System.Collections;
using System.Collections.Generic;
using maddox.GP;

public class Mission : AMission
{
public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft)
{
base.OnAircraftLanded(missionNumber, shortName, aircraft);

if (shortName.Equals("BoB_LW_Wekusta_51.010")) //despawn just this specif AI plane
{
double DespawnAfter = 1; // seconds after landing
Timeout(DespawnAfter, () => // despawn after specified seconds
{
try { aircraft.Destroy(); }
catch { }
});
}
}
}



Remember you can set DespawnAfterLandingTimeout in the mission file to -1 so nothing despawns.

Don't call the base class method, it does nothing. base.OnAircraftLanded(missionNumber, shortName, aircraft);

Try shortName.Contains("Wekusta") or shortName.Contains("BoB_LW").

1lokos
Sep-23-2020, 16:56
I don't understand, because I set a mission with 2 Bf 108 with the above script and just one (the .010) are removed 1 second after the landing, the other is keep for the normal time, seems 5 minutes.

If I use this "shortName.Contains("Wekusta") or shortName.Contains("BoB_LW")" both will be removed, no?

BTW - With the above Salmo script (https://theairtacticalassaultgroup.com/forum/showthread.php?t=4641&p=47452&viewfull=1#post47452), compiler report error in "ParameterTypes", say that don't exist in actual context.

Perhaps because their code is too old?

ATAG_Oskar
Sep-23-2020, 16:59
I don't understand, because I set a mission with 2 Bf 108 with the above script and just one (the .010) are removed 1 second after the landing, the other is keep for the normal time, seems 5 minutes.

If I use this "shortName.Contains("Wekusta") or shortName.Contains("BoB_LW")" both will be removed, no?

Just pointing out that in simple missions you don't need the full name if the aircraft to grab a reference.

varrattu
Sep-25-2020, 04:22
Just a few aditional remarks to "Z_AltitudeAGL" ...

I guess that

(aircraft as AiAircraft).getParameter(part.ParameterTypes.Z_Alt itudeAGL, -1)

is equal to the height of the aircrafts Aerodynamic Center:

https://theairtacticalassaultgroup.com/forum/attachment.php?attachmentid=49984&d=1601021606


Maybe it's the position through which all the net lift increments act for a change in angle of attack:

https://theairtacticalassaultgroup.com/forum/attachment.php?attachmentid=49985&d=1601021674


The enclosed script gives you an idea, which values the Z_AltitudeAGL should return.

:salute:~V~



using maddox.game;
using maddox.game.world;

public class Mission : AMission
{
public override void OnTickGame()
{
base.OnTickGame();

AiAircraft aircraft = GamePlay.gpPlayer().Place() as AiAircraft;
if (aircraft == null) return;

double agl = aircraft.getParameter(part.ParameterTypes.Z_Altitu deAGL, -1);
double msl = aircraft.getParameter(part.ParameterTypes.Z_Altitu deMSL, -1);

GamePlay.gpHUDLogCenter(msl.ToString("0.00m") + " | " + agl.ToString("0.00m"));
}
}

fenbeiduo
Dec-02-2020, 17:17
Remember you can set DespawnAfterLandingTimeout in the mission file to -1 so nothing despawns.

...

Thank you @Oskar

To make it work, builder should not to write additional despawn script.