Hi,
I have good coding where AI planes on spawn will locate an enemy location and fly to it and attack. The problem is. If the enemy is a plane. By the time the AI reach their waypoint where they are to attack. the enemey planes have of course flowen away.
What is the best code to insert that will have the AI check their current waypoint to reset if the target has moved to new location or if there is a closer enemy they decide to attack?
Code:
public override void OnTickGame()
{
base.OnTickGame();
if (koefDiskr == 0) koefDiskr = 640;
if (Time.tickCounter() % koefDiskr == 0)
AiActor result = null;
AiAirGroup[] enemyAG = GamePlay.gpAirGroups((myGroup.Army() == 1) ? 2 : 1);
Point3d attackerPos = myGroup.Pos();
if (enemyAG != null)
{
foreach (AiAirGroup eGroup in enemyAG)
{
if (CurTankGroups.Count != 0)
{
int i = (int)(Time.tickCounter() / koefDiskr) % CurTankGroups.Count;
{
if ((CurTankGroups[i].TankGroup != null) && (CurTankGroups[i].cur_rp != null))
if ((CurTankGroups[i].cur_rp.State == RecalcPathState.SUCCESS) )
{
CurTankGroups[i].TankGroup.SetWay((CurTankGroups[i].cur_rp.Path));
CurTankGroups[i].cur_rp = null;
}
}
}
if (eGroup.isAircraftType(AircraftType.Bomber) || eGroup.isAircraftType(AircraftType.DiveBomber))
{
if (result != null)
{
if (result.Pos().distance(ref attackerPos) > eGroup.Pos().distance(ref attackerPos))
result = eGroup;
}
else result = eGroup;
}
}
if (result == null)
{
foreach (AiAirGroup eGroup in enemyAG)
{
if (result != null)
{
if (result.Pos().distance(ref attackerPos) > eGroup.Pos().distance(ref attackerPos))
result = eGroup;
}
else result = eGroup;
}
}
}
return result;
}
Below are the two Waypoints I want the AI to check every minute.
Code:
public override void OnMissionLoaded(int missionNumber)
{
base.OnMissionLoaded(missionNumber);
MissionsCount++;
}
internal AiActor FindGroundTarget(int army, Point3d attackerPos)
{
AiActor result = null;
AiGroundGroup[] enemyGG = GamePlay.gpGroundGroups((army == 1) ? 2 : 1);
if (enemyGG != null)
{
result = enemyGG[0];
for (int i = 1; i < enemyGG.Length; i++)
{
if (enemyGG[i] != null)
if (result.Pos().distance(ref attackerPos) > enemyGG[i].Pos().distance(ref attackerPos))
{ result = enemyGG[i]; }
}
}
return result;
}
internal AiActor FindAirTarget(AiAirGroup myGroup)
{
AiActor result = null;
AiAirGroup[] enemyAG = GamePlay.gpAirGroups((myGroup.Army() == 1) ? 2 : 1);
Point3d attackerPos = myGroup.Pos();
if (enemyAG != null)
{
foreach (AiAirGroup eGroup in enemyAG)
{
if (eGroup.isAircraftType(AircraftType.Bomber) || eGroup.isAircraftType(AircraftType.DiveBomber))
{
if (result != null)
{
if (result.Pos().distance(ref attackerPos) > eGroup.Pos().distance(ref attackerPos))
result = eGroup;
}
else result = eGroup;
}
}
if (result == null)
{
foreach (AiAirGroup eGroup in enemyAG)
{
if (result != null)
{
if (result.Pos().distance(ref attackerPos) > eGroup.Pos().distance(ref attackerPos))
result = eGroup;
}
else result = eGroup;
}
}
}
return result;
}