We've been using the below in our servers for some years. It seems to work (?!) but I wouldn't put any stock in the idea that this is the "right" way to do it etc.
You just run this every 1 minute or 2 minutes or 4 minutes or however often you want, and it will change the a/c target to a different one at that point, if there is a different nearby logical target, and/or give it a target & make it attack a nearby aircraft if there is a logical one it should be attacking.
Note that I haven't included the function it calls a couple of times,
getRandomNearbyEnemyAirGroup(airGroup, 5000, 1000, 1000);, but you can pretty well guess from the name exactly what it does (it just steps through all a/c in game, just like the routine below does, and looks for enemy airGroups within the given distance. Parameters are XY distance, vertical distance (below) and vertical distance (above)).
Code:
public void Stb_changeTargetToDifferentNearbyAircraft()
{
try
{
if (GamePlay != null && GamePlay.gpArmies() != null && GamePlay.gpArmies().Length > 0)
{
foreach (int army in GamePlay.gpArmies())
{
if (GamePlay.gpAirGroups(army) != null && GamePlay.gpAirGroups(army).Length > 0)
{
foreach (AiAirGroup airGroup in GamePlay.gpAirGroups(army))
{
if (airGroup != null && airGroup.GetItems() != null && airGroup.GetItems().Length > 0)
{
foreach (AiActor actor in airGroup.GetItems())
{
if (actor != null && actor is AiAircraft)
{
AiAircraft aircraft = actor as AiAircraft;
if (aircraft != null)
{
if (Stb_isAiControlledPlane(aircraft) && aircraft.IsAirborne() &&
(airGroup.getTask() == AiAirGroupTask.ATTACK_AIR ||
airGroup.getTask() == AiAirGroupTask.FLY_WAYPOINT ||
airGroup.getTask() == AiAirGroupTask.DO_NOTHING))
{
AiAirGroup newAG = null;
//If they are already attacking we select a (possibly) new target within reasonable distance
//If they are just flying straight we only select a target if they are basically right on top of it
//and even a dufus pilot would notice it
//In theory this leaves CloD's native piloting in place for the most part, but just 'improves' it a bit
if (airGroup.getTask() == AiAirGroupTask.ATTACK_AIR)
newAG = getRandomNearbyEnemyAirGroup(airGroup, 5000, 1000, 1000);
else if (airGroup.getTask() == AiAirGroupTask.FLY_WAYPOINT || airGroup.getTask() == AiAirGroupTask.DO_NOTHING)
newAG = getRandomNearbyEnemyAirGroup(airGroup, 500, 300, 300);
if (newAG == null) break;
airGroup.setTask(AiAirGroupTask.ATTACK_AIR, newAG);
airGroup.changeGoalTarget(newAG);
AiAircraft newAircraft = newAG.GetItems()[0] as AiAircraft;
string playername = newAG.Name();
if (aircraft != null && aircraft.Player(0) != null) playername = aircraft.Player(0).Name();
Console.WriteLine("Change Target to different nearby enemy aircraft: " + actor.Name() + " to " + playername);
break; //each airGroup has only one target so no need to do this more than once.
}
}
}
}
}
}
}
}
}
}
catch (Exception ex) { Console.WriteLine("ChangeTargetToDifferentNearbyAircraft: ERROR! " + ex.ToString()); }
}
Bookmarks