I have seperate patrol mission loading and was looking at this script anyone complete this and get it to work? I am trying to get the patrols to reengage once breaking of so they do not go stupid...
Code:
Random stb_random = new Random();
public System.Threading.Timer changeTargetToDifferentNearbyAircraftTimer;
//Experiment with changingn the airgroup target to player, seeing if they will attack better
//Not actually used for now
public void changeTargetToDifferentNearbyAircraft_recurs()
{
changeTargetToDifferentNearbyAircraftTimer = new System.Threading.Timer(
new TimerCallback(changeTargetToDifferentNearbyAircraft),
null,
30000, //wait time @ startup
52340); //periodically call the callback at this interval
//Timeout(52.34, () => { Stb_changeTargetToDifferentNearbyAircraft_recurs(); });
//Timeout(5, () => { Console.WriteLine("CHANGETARGET: Just changed for all"); });
//Task.Run(() => Stb_changeTargetToDifferentNearbyAircraft());
//Timeout(28, () => { Stb_changeTargetToPlayerRecurs(player); });
}
public void changeTargetToDifferentNearbyAircraft(object o)
{
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)
{
//For heavy bombers, do this only SOMETIMES
if (airGroup.NOfAirc > 2 && Calcs.isHeavyBomber(airGroup) && stb_random.NextDouble() > 0.02) continue;
else if (airGroup.NOfAirc == 2 && Calcs.isHeavyBomber(airGroup) && stb_random.NextDouble() > 0.2) continue;
else if (airGroup.NOfAirc == 1 && Calcs.isHeavyBomber(airGroup) && stb_random.NextDouble() > 0.75) continue;
AiAirGroup newAG = null;
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("Stb_changeTargetToDifferentNearbyAircraft: ERROR! " + ex.ToString()); }
}
public AiAirGroup getRandomNearbyEnemyAirGroup(AiAirGroup from, double distance_m, double lowAlt_m, double highAlt_m)
{
try
{
Point3d startPos = from.Pos();
List<AiAirGroup> airGroups = getNearbyEnemyAirGroups(from, distance_m, lowAlt_m, highAlt_m);
if (airGroups == null || airGroups.Count == 0) return null;
int choice = stb_random.Next(airGroups.Count);
if (airGroups[choice].Pos().distance(ref startPos) <= distance_m / 2) //We'll somewhat favor airgroups closer to the from airgroup
choice = stb_random.Next(airGroups.Count);
return airGroups[choice];
}
catch (Exception ex) { Console.WriteLine("-stats getRandomNearbyEnemyAirGroup ERROR: " + ex.ToString()); return null; }
}
//Gets all nearby enemy airgroup within distance_m (meters) and between alt - lowAlt_m & alt-highAlt_m altitude of the target
public List<AiAirGroup> getNearbyEnemyAirGroups(AiAirGroup from, double distance_m, double lowAlt_m, double highAlt_m)
{
try
{
if (GamePlay == null) return null;
if (from == null) return null;
List<AiAirGroup> returnAirGroups = new List<AiAirGroup>() { };
AiAirGroup[] Airgroups;
Point3d StartPos = from.Pos();
Airgroups = GamePlay.gpAirGroups((from.Army() == 1) ? 2 : 1);
if (Airgroups != null)
{
foreach (AiAirGroup airGroup in Airgroups)
{
if (airGroup == null || airGroup.GetItems().Length == 0) continue;
//AiAircraft a = airGroup.GetItems()[0] as AiAircraft;
if (airGroup.Pos().z > StartPos.z - lowAlt_m && airGroup.Pos().z < StartPos.z + highAlt_m && airGroup.Pos().distance(ref StartPos) <= distance_m)
returnAirGroups.Add(airGroup);
}
return returnAirGroups;
}
else
return new List<AiAirGroup>() { };
}
catch (Exception ex) { Console.WriteLine("-stats getNearbyEnemyAirGroups ERROR: " + ex.ToString()); return new List<AiAirGroup>() { }; }
}
Bookmarks