I tried to make my own script for locating an enemy flight within 10km and setting an airgroup to attack it. With the idea to try and use it to stop the non-attacking after attacking once. But whilst it seems to be ok at gettign an enemy flight withn 10km, when they are triggered to attack they just do weird stuff, diving to the floor and flying straight. Can anyone wiser see what I am doing wrong please?
Code:
using System;
using System.IO;
using maddox.game;
using maddox.game.world;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel.Design;
using System.Diagnostics;
using maddox.GP;
public class Mission : AMission
{
AiAirGroup[] AirgroupsAllied;
AiAirGroup[] AirgroupsAxis;
private double timerTime = 0;
//airGroup.changeGoalTarget(newAG);
//if (airGroup.getTask() == AiAirGroupTask.ATTACK_AIR)
public override void OnBattleStarted()
{
base.OnBattleStarted();
MissionNumberListener = -1;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}
public override void OnTickGame()
{
base.OnTickGame();
if (Time.current() > timerTime)
{
timerTime = Time.current() + 30.0; // 30 seconds to next
AirgroupsAllied = GamePlay.gpAirGroups(1);
AirgroupsAxis = GamePlay.gpAirGroups(2);
//Console.WriteLine(AirgroupsAllied[0].Name());
//Console.WriteLine(AirgroupsAxis[0].Name());
foreach (AiAirGroup airGroup in AirgroupsAllied)
{
Console.WriteLine("Allied: " + airGroup.Name());
Console.WriteLine("Task is: " + airGroup.getTask());
if (airGroup.getTask() == AiAirGroupTask.FLY_WAYPOINT || airGroup.getTask() == AiAirGroupTask.RETURN)
{
AiAirGroup nearestIs = getNearestEnemyAirgroupWithinTen(airGroup);
if (nearestIs != null)
{
Console.WriteLine("Nearest enemy" + nearestIs.Name());
Console.WriteLine("Ping!");
airGroup.setTask(AiAirGroupTask.ATTACK_AIR, airGroup);
airGroup.changeGoalTarget(nearestIs);
}
else
Console.WriteLine("No nearest enemy");
}
}
foreach (AiAirGroup airGroup in AirgroupsAxis)
{
Console.WriteLine("Axis: " + airGroup.Name());
Console.WriteLine("Task is: " + airGroup.getTask());
if (airGroup.getTask() == AiAirGroupTask.FLY_WAYPOINT || airGroup.getTask() == AiAirGroupTask.RETURN)
{
AiAirGroup nearestIs = getNearestEnemyAirgroupWithinTen(airGroup);
if (nearestIs != null)
{
Console.WriteLine("Nearest enemy" + nearestIs.Name());
Console.WriteLine("Ping!");
airGroup.setTask(AiAirGroupTask.ATTACK_AIR, airGroup);
airGroup.changeGoalTarget(nearestIs);
}
else
Console.WriteLine("No nearest enemy");
}
}
}
}
public AiAirGroup getNearestEnemyAirgroupWithinTen(AiGroup from)
{
AiAirGroup NearestAirgroup = null;
AiAirGroup[] EnemyAirgroups;
Point3d StartPos = from.Pos();
EnemyAirgroups = GamePlay.gpAirGroups((from.Army() == 1) ? 2 : 1);
if (EnemyAirgroups != null) // enemy airgroups present
{
foreach (AiAirGroup airgroup in EnemyAirgroups) // for each of the groups in the array
{
double dist = (airgroup.Pos()).distance(ref StartPos);
Console.WriteLine("Distance is" + dist);
if (dist < 10000.0)
{
Console.WriteLine("Distance less than 10,000");
if (NearestAirgroup != null) // if there is a current nearest airgroup check the current with array airgroup
{
Console.WriteLine("Existing NearestAirgroup present");
if (NearestAirgroup.Pos().distance(ref StartPos) > airgroup.Pos().distance(ref StartPos))
{
NearestAirgroup = airgroup; // replace the current nearest with array airgroup
}
else
{
// keep existing NearestAirgroup
}
}
else
Console.WriteLine("Nearest Airgroup is null");
NearestAirgroup = airgroup; // set airgroup as nearest because NearestAirgroup was null
}
}
}
return NearestAirgroup;
}
}
Edit: Just to be clear I took some parts from other people's work elsewhere that I couldn't get to work.
Bookmarks