PDA

View Full Version : Randomise number Aigroup planes


salmo
09-19-2012, 02:43 PM
RANDOM NUMBER OF AI AIRGROUP PLANES

Purpose:
Randomises the number of Ai aircraft in an airgroup each time they spawn.

How does it work?
(1) Rename this script the same as your mission file name.
(2) In FMB, place a Static/environment/Banner Marker - x red object (the red cross) anywhere on an airfield, or close to an airspawn point, where you want to randomise Ai airgroup numbers.
(3) Red crosses are removed from the mission when the battle starts.
(4) Any Ai airgroup that spawn within 1km of where a red cross was placed will spawn with a random number of aircraft. Number of aircraft vary from 2 (minimum) up to the number that you specified in the FMB group properties (maximum).
(5) The number of aircraft in the airgroup will vary every time the group spawns.
(6) Does not affect number of ai aircraft that spawn where there is not a red-cross placed.


#region System classes
using System;
using maddox.game;
using maddox.game.world;
using System.Collections.Generic;
using System.Diagnostics;
using maddox.GP;
using System.Threading;
#endregion

public class Mission : AMission
{
#region salObjects
Dictionary<Point2d, string> salRandomiserObjects = new Dictionary<Point2d, string>();
List<AiAirGroup> AirGroupQue = new List<AiAirGroup>();
double ClearAirGroupQueEvery = 2;
Stopwatch AirGroupQueTimer = new Stopwatch();
Random random = new Random();
#endregion

public override void OnBattleStarted()
{
base.OnBattleStarted();
#region salObjects
foreach (GroundStationary stationary in GamePlay.gpGroundStationarys()) // look for salRandomiser objects
{
if (stationary.Title.Contains("Polotnische_X_red_1")) // we found a salRandomiser object
{
Point2d p = new Point2d();
p.x = stationary.pos.x;
p.y = stationary.pos.y;
try
{
salRandomiserObjects.Add(p, "RandomAiGroupNumber"); // remember the salRandomiser object position & type
stationary.Destroy(); // remove the salRandomiser object from the battle
}
catch (Exception e)
{
Console.WriteLine("Unexpected error loading SalObjects [OnBattleStarted]");
Console.WriteLine(e.Message);
}
}
}
#endregion
}

public override void OnActorCreated(int missionNumber, string shortName, AiActor actor)
{
base.OnActorCreated(missionNumber, shortName, actor);
#region salObjects
// we need a short pause while any human player is loaded into the aircraft
Timeout(1, () =>
{
AiAircraft aircraft = actor as AiAircraft;
if (aircraft != null && isAiControlledPlane(aircraft)) // do we have an Ai (not human) airgroup?
{
foreach (var pair in salRandomiserObjects)
{
if (pair.Value == "RandomAiGroupNumber")
{
double CircleRadius = 1000.00; // check of spawning within x metres of the salObject
Point2d p = pair.Key;
if (Math.Sqrt(Math.Pow((p.x - aircraft.Pos().x), 2) + (Math.Pow((p.y - aircraft.Pos().y), 2))) <= CircleRadius)
{
AiAirGroup ag = aircraft.AirGroup() as AiAirGroup;
if (ag != null && !AirGroupQue.Contains(ag))
{
AirGroupQue.Add(ag); // add the airgroup to a que for processing
AirGroupQueTimer.Restart();
}
}
}
}
}
});
#endregion
}

public override void OnTickGame()
{
base.OnTickGame();
#region salObjects
if (AirGroupQueTimer.Elapsed.Seconds >= ClearAirGroupQueEvery) // time to process Ai airgroups
{
if (AirGroupQue.Count > 0)
{
AirGroupQueTimer.Reset();
AirGroupQueTimer.Stop();
foreach (AiAirGroup ag in AirGroupQue) // one airgroup at a time
{
int ACstart = ag.NOfAirc;
List<AiAircraft> aircraftlist = new List<AiAircraft>(); // to hold a list of aircraft in each airgroup
List<AiAircraft> randomaircraftlist = new List<AiAircraft>(); // to hold a randomised list of aircraft in each airgroup
foreach (AiActor actor in ag.GetItems()) // make list of aircraft in the airgroup
{
if (actor as AiAircraft != null) aircraftlist.Add(actor as AiAircraft);
}
while (aircraftlist.Count > 0) // now shuffle (randomise) the aircraft list
{
int j = random.Next(0, aircraftlist.Count);
randomaircraftlist.Add(aircraftlist[j]);
aircraftlist.RemoveAt(j);
}
int limit = 2; // no less than 2 aircraft in an airgroup
if (ag.NOfAirc > limit) limit = random.Next(limit, ag.NOfAirc); // pick a random number of aircraft for this airgroup
while (randomaircraftlist.Count > limit) // now pick at random from the shuffled the aircraft list
{
int j = random.Next(0, randomaircraftlist.Count);
randomaircraftlist[j].Destroy();
randomaircraftlist.RemoveAt(j);
}
int ACend = ag.NOfAirc;
Console.WriteLine("Airgroup " + ag.Name() + " AC number changed from " + ACstart.ToString() + " to " + ACend.ToString() + " [salObjects]");
}
AirGroupQue.Clear(); // done processing so remove the aigroup from the list
}
}
#endregion
}

private bool isAiControlledPlane(AiAircraft aircraft)
#region is plane an Ai plane
{ // 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
}