Ил-2 Штурмовик: Битва за Британию. Скрипты. Удаление самолетов
This code removes landed and crashed AI and player aircraft and destroys abandoned player aircraft. If the player had some AI in his/her group, they are also destroyed. First, we need the following private methods in our mission's script:
Автор: TheEnlightenedFlorist
private bool isAiControlledPlane(AiAircraft aircraft) { 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; } private void destroyPlane(AiAircraft aircraft) { if (aircraft != null && isAiControlledPlane(aircraft)) aircraft.Destroy(); } private void damageAiControlledPlane(AiActor actorMain) { 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; aircraft.hitNamed(part.NamedDamageTypes.ControlsElevatorDisabled); aircraft.hitNamed(part.NamedDamageTypes.ControlsAileronsDisabled); aircraft.hitNamed(part.NamedDamageTypes.ControlsRudderDisabled); aircraft.hitNamed(part.NamedDamageTypes.FuelPumpFailure); int iNumOfEngines = (aircraft.Group() as AiAirGroup).aircraftEnginesNum(); for (int i = 0; i < iNumOfEngines; i++) { aircraft.hitNamed((part.NamedDamageTypes)Enum.Parse(typeof(part.NamedDamageTypes), "Eng" + i.ToString() + "TotalFailure")); } Timeout(300, () => { destroyPlane(aircraft); } ); } }
Putting the following code in your OnPlaceLeave() method will, when a player abandons an aircraft, disable controls and engines and remove the plane after 300 seconds. Hopefully, it has crashed by then.
Timeout(1, () => { damageAiControlledPlane(actor); } );
After one of the patches, the dev team made it so that crashed and landed planes would disappear without needing to put anything in a script. This makes the next block of code redundant, but you can still use it if you want to decrease the time before aircraft disappear. Put the following code in your OnAircraftLanded() and OnAircraftCrashLanded() methods. The 300 is the number of seconds before the aircraft is removed.
Timeout(300, () => { destroyPlane(aircraft); } );
Here is what it should look like all together. Barring any other code you have in your script, of course. Again, the OnAircraftLanded() and OnAircraftCrashLanded() methods are redundant and not necessary.
using System;
using maddox.game; using maddox.game.world; using System.Collections.Generic;
public class Mission : AMission {
public override void OnBattleStarted() { base.OnBattleStarted(); //listen to events from all missions. MissionNumberListener = -1; } public override void OnPlaceLeave(Player player, AiActor actor, int placeIndex) { base.OnPlaceLeave(player, actor, placeIndex); Timeout(1, () => { damageAiControlledPlane(actor); } ); } public override void OnAircraftCrashLanded(int missionNumber, string shortName, AiAircraft aircraft) { base.OnAircraftCrashLanded(missionNumber, shortName, aircraft); Timeout(300, () => { destroyPlane(aircraft); } ); } public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft) { base.OnAircraftLanded(missionNumber, shortName, aircraft); Timeout(300, () => { destroyPlane(aircraft); } ); } private bool isAiControlledPlane(AiAircraft aircraft) { 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; } private void destroyPlane(AiAircraft aircraft) { if (aircraft != null) aircraft.Destroy(); } private void damageAiControlledPlane(AiActor actorMain) { 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; aircraft.hitNamed(part.NamedDamageTypes.ControlsElevatorDisabled); aircraft.hitNamed(part.NamedDamageTypes.ControlsAileronsDisabled); aircraft.hitNamed(part.NamedDamageTypes.ControlsRudderDisabled); aircraft.hitNamed(part.NamedDamageTypes.FuelPumpFailure); int iNumOfEngines = (aircraft.Group() as AiAirGroup).aircraftEnginesNum(); for (int i = 0; i < iNumOfEngines; i++) { aircraft.hitNamed((part.NamedDamageTypes)Enum.Parse(typeof(part.NamedDamageTypes), "Eng" + i.ToString() + "TotalFailure")); } Timeout(300, () => { destroyPlane(aircraft); } ); } } }