View Single Post
  #1  
Old 11-01-2011, 02:38 AM
hc_wolf hc_wolf is offline
Approved Member
 
Join Date: Jul 2010
Posts: 439
Default Use OnTickTime to re-evaluate AI air group waypoints

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;
    }
__________________
__________________
Win7, 64bit Ultra
Asus P8P67Pro MB
Intel i7-2600K
Coursair 16GB (4x 4GB), DDR3-1600MHz
Gainward Nvidia 580GTX 3GB DDR5
850-Watt Modular Power Supply
WIN7 and COD on Gskill SSD 240GB
40" Panasonic LCD
TrackIR5 +
Thrustmaster Warthog stick, throttle & pedals
Reply With Quote