#processor aa_intercept WSF_TASK_PROCESSOR
#
#   update_interval 5 sec             
#
#   script_variables 
#      double launchRange = 20 * MATH.M_PER_NM();  
#      double salvoSize = 2;        
#      string weaponName = "aa_mrm";  
#      WsfWeapon aa_mrm = PLATFORM.Weapon(weaponName);     
#      bool canPursue = false;   
#      Map<string, int> aa_mrmUseful = Map<string, int>();        
#      WsfTrack currentTarget;     
#   end_script_variables
#
#   //
#   //@ pursueCondition
#   //
#   script bool pursueCondition()
#      return (
#         aa_mrm.QuantityRemaining() > 0 &&      
#         TRACK.Target().IsValid()        
#      );
#   end_script
#
#   //
#   //@ Pursue
#   //
#   script bool Pursue()
#      if (pursueCondition())
#      {
#         if(PLATFORM.GroundRangeTo(TRACK.CurrentLocation()) >= launchRange)
#         {
#            PLATFORM.GoToLocation(TRACK.CurrentLocation());
#         }               
#         canPursue = true;
#         return canPursue;
#      }
#      canPursue = false;
#      return canPursue;
#   end_script
#
#   //
#   //@ CanEngage
#   //
#   script bool CanEngage()
#      bool canEngage = false;
#      if (aa_mrm.IsValid() &&   
#         aa_mrm.QuantityRemaining() >= salvoSize &&    
#         TRACK.Target().IsValid() && 
#         PLATFORM.GroundRangeTo(TRACK) < launchRange &&       
#         WeaponsActiveFor(TRACK.TrackId()) < 1        
#      )
#      {
#         aa_mrmUseful[weaponName] = 1;        
#         canEngage = true;
#      }
#      return canEngage;
#   end_script
#
#   //
#   //@ Engage
#   //
#   script void Engage()
#      if (CanEngage())
#      {
#         if(aa_mrmUseful.Exists(weaponName))
#         {
#            if(aa_mrmUseful[weaponName] == 1)
#            {
#               Fire(TRACK,"FIRE",weaponName,salvoSize,PLATFORM);
#               aa_mrmUseful[weaponName] == 0;
#            }
#         }
#      }
#      else
#      {
#         writeln_d("Cannot engage: Conditions not met.");
#      }
#   end_script
#
#   //
#   //@ TrackAndEngage
#   //
#   script void TrackAndEngage()
#      if (TRACK.Target().IsValid())
#      {
#         if (CanEngage())
#         {
#            Engage();
#         }
#         else
#         {
#            Pursue();
#         }
#      }
#      else
#      {
#         writeln_d("No valid target to track.");
#      }
#   end_script
#
#   //
#   //@ WeaponReplenishment
#   //
##   script void ReplenishWeapons()
##      if (aa_mrm.QuantityRemaining() < salvoSize)
##      {
##         writeln_d("Replenishing weapons: ", weaponName);
##         PLATFORM.ReplenishWeapon(weaponName);  //           
##      }
##   end_script
#
#/////////////////////state////////////////////////
#evaluation_interval IDLE 1 sec
#
#   state IDLE
#      on_entry
#         writeln_d("Entering IDLE state.");
#      end_on_entry
#
#      next_state TRACKING
#         return TRACK.Target().IsValid();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting IDLE state.");
#      end_on_exit
#   end_state
#evaluation_interval TRACKING 1 sec
#   state TRACKING
#      on_entry
#         writeln_d("Entering TRACKING state.");
#      end_on_entry
#
#      next_state ENGAGING
#         return CanEngage();
#      end_next_state
#
#      next_state PURSUING
#         return pursueCondition() && !CanEngage();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting TRACKING state.");
#      end_on_exit
#   end_state
#evaluation_interval ENGAGING 1 sec
#   state ENGAGING
#      on_entry
#         writeln_d("Entering ENGAGING state.");
#         Engage();
#      end_on_entry
#
#      next_state IDLE
#         return !TRACK.Target().IsValid() || aa_mrm.QuantityRemaining() < salvoSize;
#      end_next_state
#
#      next_state TRACKING
#         return TRACK.Target().IsValid() && !CanEngage();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting ENGAGING state.");
#      end_on_exit
#   end_state
#evaluation_interval PURSUING 1 sec
#   state PURSUING
#      on_entry
#         writeln_d("Entering PURSUING state.");
#         Pursue();
#      end_on_entry
#
#      next_state TRACKING
#         return TRACK.Target().IsValid() && PLATFORM.GroundRangeTo(TRACK) < launchRange;
#      end_next_state
#
#      next_state IDLE
#         return !pursueCondition();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting PURSUING state.");
#      end_on_exit
#   end_state
#
#end_processor



#processor aa_intercept WSF_TASK_PROCESSOR
#
#   update_interval 5 sec
#
#   script_variables 
#      double launchRange = 20 * MATH.M_PER_NM();  //     
#      double salvoSize = 2;  //         
#      string weaponName = "aa_mrm";  //     
#      WsfWeapon aa_mrm = PLATFORM.Weapon(weaponName);  //     
#      bool canPursue = false;  //       
#      Map<string, int> aa_mrmUseful = Map<string, int>();  //         
#      WsfTrack currentTarget;  //        
#   end_script_variables
#
#   //
#   //@ SelectClosestTrack
#   //
#   script WsfTrack SelectClosestTrack()
#      WsfTrack closestTarget;
#      double closestDistance = 100000000000000000000000;  //           
#      foreach (WsfTrack track in PLATFORM.MasterTrackList())
#      {
#         if (track.IsValid() && PLATFORM.GroundRangeTo(track) < closestDistance)
#         {
#            closestTarget = track;
#            closestDistance = PLATFORM.GroundRangeTo(track);
#         }
#      }
#      return closestTarget;
#   end_script
#
#   //
#   //@ pursueCondition
#   //
#   script bool pursueCondition()
#      return (
#         aa_mrm.QuantityRemaining() > 0 &&      
#         currentTarget.IsValid()        
#      );
#   end_script
#
#   //
#   //@ Pursue
#   //
#   script bool Pursue()
#      if (pursueCondition())
#      {
#         double distanceToTarget = PLATFORM.GroundRangeTo(currentTarget);
#         if (distanceToTarget >= launchRange)
#         {
#            PLATFORM.GoToLocation(currentTarget.CurrentLocation());
#         }               
#         canPursue = true;
#         return canPursue;
#      }
#      canPursue = false;
#      return canPursue;
#   end_script
#
#   //
#   //@ CanEngage
#   //
#   script bool CanEngage()
#      bool canEngage = false;
#      if (aa_mrm.IsValid() &&   
#         aa_mrm.QuantityRemaining() >= salvoSize &&    
#         currentTarget.IsValid() && 
#         PLATFORM.GroundRangeTo(currentTarget) < launchRange &&       
#         WeaponsActiveFor(currentTarget.TrackId()) < 1        
#      )
#      {
#         aa_mrmUseful[weaponName] = 1;        
#         canEngage = true;
#      }
#      return canEngage;
#   end_script
#
#   //
#   //@ Engage
#   //
#   script void Engage()
#      if (CanEngage())
#      {
#         if (aa_mrmUseful.Exists(weaponName))
#         {
#            if (aa_mrmUseful[weaponName] == 1)
#            {
#               Fire(currentTarget, "FIRE", weaponName, salvoSize, PLATFORM);
#               aa_mrmUseful[weaponName] = 0;  //         
##               EvadeAfterEngage();  //       
#            }
#         }
#      }
#      else
#      {
#         writeln_d("Cannot engage: Conditions not met.");
#      }
#   end_script
#
#   //
#   //@ EvadeAfterEngage
#   //
##   script void EvadeAfterEngage()
##      writeln_d("Executing evade maneuver after engage.");
##      double evadeDistance = launchRange * 0.3;  //           30%
##      Location evadeLocation = PLATFORM.CurrentLocation().OffsetBy(evadeDistance, MATH.RandomAngle());
##      PLATFORM.GoToLocation(RANDOM.loevadeLocation);  //         
##   end_script
#
#   //
#   //@ TrackAndEngage
#   //
#   script void TrackAndEngage()
#      currentTarget = SelectClosestTrack();
#      if (currentTarget.IsValid())
#      {
#         writeln_d("Selected target ID: ", currentTarget.TrackId());
#         if (CanEngage())
#         {
#            Engage();
#         }
#         else
#         {
#            Pursue();
#         }
#      }
#      else
#      {
#         writeln_d("No valid targets available.");
#      }
#   end_script
#
#/////////////////////state////////////////////////
#evaluation_interval IDLE 1 sec
#
#   state IDLE
#      on_entry
#         writeln_d("Entering IDLE state.");
#         currentTarget = SelectClosestTrack();
#      end_on_entry
#
#      next_state TRACKING
#         return currentTarget.IsValid();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting IDLE state.");
#      end_on_exit
#   end_state
#
#evaluation_interval TRACKING 1 sec
#
#   state TRACKING
#      on_entry
#         writeln_d("Entering TRACKING state.");
#         currentTarget = SelectClosestTrack();
#      end_on_entry
#
#      next_state ENGAGING
#         return CanEngage();
#      end_next_state
#
#      next_state PURSUING
#         return pursueCondition() && !CanEngage();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting TRACKING state.");
#      end_on_exit
#   end_state
#
#evaluation_interval ENGAGING 1 sec
#
#   state ENGAGING
#      on_entry
#         writeln_d("Entering ENGAGING state.");
#         Engage();
#      end_on_entry
#
#      next_state IDLE
#         return !currentTarget.IsValid() || aa_mrm.QuantityRemaining() < salvoSize;
#      end_next_state
#
#      next_state TRACKING
#         return currentTarget.IsValid() && !CanEngage();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting ENGAGING state.");
#      end_on_exit
#   end_state
#
#evaluation_interval PURSUING 1 sec
#
#   state PURSUING
#      on_entry
#         writeln_d("Entering PURSUING state.");
#         Pursue();
#      end_on_entry
#
#      next_state TRACKING
#         return currentTarget.IsValid() && PLATFORM.GroundRangeTo(currentTarget) < launchRange;
#      end_next_state
#
#      next_state IDLE
#         return !pursueCondition();
#      end_next_state
#
#      on_exit
#         writeln_d("Exiting PURSUING state.");
#      end_on_exit
#   end_state
#
#end_processor


#processor aa_intercept WSF_TASK_PROCESSOR
#
## Update interval
#update_interval 5 sec
#
## Script variables
#script_variables 
#   double launchRange = 20 * MATH.M_PER_NM();  //     
#   double salvoSize = 2;  //         
#   string weaponName = "aa_mrm";  //     
#   WsfWeapon aa_mrm = PLATFORM.Weapon(weaponName);  //     
#   Map<string, int> aa_mrmUseful = Map<string, int>();  //         
#   WsfTrack currentTarget;  //        
#end_script_variables
#
## SelectClosestTrack
#script WsfTrack SelectClosestTrack()
#   WsfTrack closestTarget;
#   double closestDistance = 1000000000;  //         
#   foreach (WsfTrack track in PLATFORM.MasterTrackList())
#   {
#      if (track.IsValid() && PLATFORM.GroundRangeTo(track) < closestDistance)
#      {
#         closestTarget = track;
#         closestDistance = PLATFORM.GroundRangeTo(track);
#      }
#   }
#   return closestTarget;
#end_script
#
## CanEngage
#script bool CanEngage()
#   bool canEngage = false;
#   if (aa_mrm.IsValid() &&   
#      aa_mrm.QuantityRemaining() >= salvoSize &&    
#      currentTarget.IsValid() && 
#      PLATFORM.GroundRangeTo(currentTarget) < launchRange &&       
#      WeaponsActiveFor(currentTarget.TrackId()) < 1        
#   )
#   {
#      aa_mrmUseful[weaponName] = 1;        
#      canEngage = true;
#   }
#   return canEngage;
#end_script
#
## Engage
#script void Engage()
#   if (CanEngage())
#   {
#      if (aa_mrmUseful.Exists(weaponName))
#      {
#         if (aa_mrmUseful[weaponName] == 1)
#         {
#            Fire(currentTarget, "FIRE", weaponName, salvoSize, PLATFORM);
#            aa_mrmUseful[weaponName] = 0;  //         
#         }
#      }
#   }
#   else
#   {
#      writeln_d("Cannot engage: Conditions not met.");
#   }
#end_script
#
## Pursue
#script bool Pursue()
#   if (currentTarget.IsValid() && aa_mrm.QuantityRemaining() > 0)
#   {
#      double distanceToTarget = PLATFORM.GroundRangeTo(currentTarget);
#      if (distanceToTarget >= launchRange)
#      {
#         writeln_d("Target is out of range. Moving closer...");
#         PLATFORM.GoToLocation(currentTarget.CurrentLocation());
#         return true;
#      }
#   }
#   return false;
#end_script
#
## TrackAndEngage
#script void TrackAndEngage()
#   currentTarget = SelectClosestTrack();
#   if (currentTarget.IsValid())
#   {
#      writeln_d("Selected target ID: ", currentTarget.TrackId());
#      if (CanEngage())
#      {
#         Engage();
#      }
#      else
#      {
#         Pursue();
#      }
#   }
#   else
#   {
#      writeln_d("No valid targets available.");
#   }
#end_script
#
## States
#
#evaluation_interval IDLE 1 sec
#state IDLE
#   on_entry
#      writeln_d("Entering IDLE state.");
#      currentTarget = SelectClosestTrack();
#   end_on_entry
#
#   next_state TRACKING
#      return currentTarget.IsValid();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting IDLE state.");
#   end_on_exit
#end_state
#
#evaluation_interval TRACKING 1 sec
#state TRACKING
#   on_entry
#      writeln_d("Entering TRACKING state.");
#      currentTarget = SelectClosestTrack();
#   end_on_entry
#
#   next_state ENGAGING
#      return CanEngage();
#   end_next_state
#
#   next_state PURSUING
#      return Pursue() && !CanEngage();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting TRACKING state.");
#   end_on_exit
#end_state
#
#evaluation_interval ENGAGING 1 sec
#state ENGAGING
#   on_entry
#      writeln_d("Entering ENGAGING state.");
#      Engage();
#   end_on_entry
#
#   next_state IDLE
#      return !currentTarget.IsValid() || aa_mrm.QuantityRemaining() < salvoSize;
#   end_next_state
#
#   next_state TRACKING
#      return currentTarget.IsValid() && !CanEngage();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting ENGAGING state.");
#   end_on_exit
#end_state
#
#evaluation_interval PURSUING 1 sec
#state PURSUING
#   on_entry
#      writeln_d("Entering PURSUING state.");
#   end_on_entry
#
#   next_state MOVE_TO_TARGET
#      return PLATFORM.GroundRangeTo(currentTarget) >= launchRange;
#   end_next_state
#
#   next_state TRACKING
#      return PLATFORM.GroundRangeTo(currentTarget) < launchRange;
#   end_next_state
#
#   next_state IDLE
#      return !currentTarget.IsValid();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting PURSUING state.");
#   end_on_exit
#end_state
#
#evaluation_interval MOVE_TO_TARGET 1 sec
#state MOVE_TO_TARGET
#   on_entry
#      writeln_d("Entering MOVE_TO_TARGET state.");
#      WsfGeoPoint targetLocation = currentTarget.CurrentLocation();
#      PLATFORM.GoToLocation(targetLocation);
#   end_on_entry
#
#   next_state PURSUING
#      return PLATFORM.GroundRangeTo(currentTarget) < launchRange;
#   end_next_state
#
#   next_state IDLE
#      return !currentTarget.IsValid();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting MOVE_TO_TARGET state.");
#   end_on_exit
#end_state
#
#end_processor




#processor aa_intercept WSF_TASK_PROCESSOR
#
## Update interval
#update_interval 5 sec
#
## Script variables
#script_variables 
#   double launchRange = 20 * MATH.M_PER_NM();  //       
#   double minRange = launchRange * 0.3;  //              30%
#   double speedFactor = 1.2;  //          20%
#   string weaponName = "aa_mrm";  //     
#   WsfWeapon aa_mrm = PLATFORM.Weapon(weaponName);  //     
#   WsfTrack currentTarget;  //     
#end_script_variables
#
## SelectClosestTrack
#script WsfTrack SelectClosestTrack()
#   WsfTrack closestTarget;
#   double closestDistance = 1000000000;  //         
#   foreach (WsfTrack track in PLATFORM.MasterTrackList())
#   {
#      if (track.IsValid() && PLATFORM.GroundRangeTo(track) < closestDistance)
#      {
#         closestTarget = track;
#         closestDistance = PLATFORM.GroundRangeTo(track);
#      }
#   }
#   return closestTarget;
#end_script
#
## AdjustSpeedTowardsTarget
#script void AdjustSpeedTowardsTarget()
#   if (currentTarget.IsValid())
#   {
#      double targetSpeed = currentTarget.Speed();
#      double platformSpeed = PLATFORM.Speed();
#      double adjustedSpeed = targetSpeed * speedFactor;
#
#      if (platformSpeed != adjustedSpeed)
#      {
#         writeln_d("Adjusting speed to match target...");
#         PLATFORM.GoToSpeed(adjustedSpeed);  //       
#      }
#   }
#end_script
#
## MaintainOptimalDistance
#script void MaintainOptimalDistance()
#   if (currentTarget.IsValid())
#   {
#      double distanceToTarget = PLATFORM.GroundRangeTo(currentTarget);
#      WsfGeoPoint targetLocation = currentTarget.CurrentLocation();
#
#      if (distanceToTarget > launchRange)  //            
#      {
#         writeln_d("Target out of range, moving closer...");
#         PLATFORM.GoToLocation(targetLocation);
#      }
#      else if (distanceToTarget < minRange)  //            
#      {
#         writeln_d("Too close to target, increasing distance...");
##         WsfGeoPoint evadeLocation = PLATFORM.Location().OffsetBy(minRange, MATH.OppositeAngle(PLATFORM.BearingTo(targetLocation)));
##         PLATFORM.GoToLocation(evadeLocation);
#      }
#      else
#      {
#         writeln_d("Optimal distance maintained.");
#      }
#   }
#end_script
#
## CanEngage
#script bool CanEngage()
#   bool canEngage = false;
#   if (aa_mrm.IsValid() &&   
#      aa_mrm.QuantityRemaining() >= 2 &&    
#      currentTarget.IsValid() && 
#      PLATFORM.GroundRangeTo(currentTarget) < launchRange &&       
#      WeaponsActiveFor(currentTarget.TrackId()) < 1        
#   )
#   {
#      canEngage = true;
#   }
#   return canEngage;
#end_script
#
## Engage
#script void Engage()
#   if (CanEngage())
#   {
#      Fire(currentTarget, "FIRE", weaponName, 2, PLATFORM);
#   }
#   else
#   {
#      writeln_d("Cannot engage: Conditions not met.");
#   }
#end_script
#
## States
#
#evaluation_interval IDLE 1 sec
#state IDLE
#   on_entry
#      writeln_d("Entering IDLE state.");
#      currentTarget = SelectClosestTrack();
#   end_on_entry
#
#   next_state TRACKING
#      return currentTarget.IsValid();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting IDLE state.");
#   end_on_exit
#end_state
#
#evaluation_interval TRACKING 1 sec
#state TRACKING
#   on_entry
#      writeln_d("Entering TRACKING state.");
#      currentTarget = SelectClosestTrack();
#   end_on_entry
#
#   next_state ENGAGING
#      return CanEngage();
#   end_next_state
#
#   next_state PURSUING
#      return currentTarget.IsValid() && !CanEngage();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting TRACKING state.");
#   end_on_exit
#end_state
#
#evaluation_interval ENGAGING 1 sec
#state ENGAGING
#   on_entry
#      writeln_d("Entering ENGAGING state.");
#      Engage();
#   end_on_entry
#
#   next_state IDLE
#      return !currentTarget.IsValid() || aa_mrm.QuantityRemaining() < 2;
#   end_next_state
#
#   next_state TRACKING
#      return currentTarget.IsValid() && !CanEngage();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting ENGAGING state.");
#   end_on_exit
#end_state
#
#evaluation_interval PURSUING 1 sec
#state PURSUING
#   on_entry
#      writeln_d("Entering PURSUING state.");
#   end_on_entry
#
#   next_state ADJUSTING_SPEED
#      return currentTarget.IsValid();
#   end_next_state
#
#   next_state IDLE
#      return !currentTarget.IsValid();
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting PURSUING state.");
#   end_on_exit
#end_state
#
#evaluation_interval ADJUSTING_SPEED 1 sec
#state ADJUSTING_SPEED
#   on_entry
#      writeln_d("Entering ADJUSTING_SPEED state.");
#      AdjustSpeedTowardsTarget();
#      MaintainOptimalDistance();
#   end_on_entry
#
#   next_state PURSUING
#      return true;  //    PURSUING         
#   end_next_state
#
#   on_exit
#      writeln_d("Exiting ADJUSTING_SPEED state.");
#   end_on_exit
#end_state
#
#end_processor





processor aa_intercept WSF_TASK_PROCESSOR

# Update interval
update_interval 5 sec

# Script variables
script_variables 
   double launchRange = 20 * MATH.M_PER_NM();  //       
   double minRange = launchRange * 0.3;  //       
   double speedFactor = 1.2;  //          20%
   string weaponName = "aa_mrm";  //     
   WsfWeapon aa_mrm = PLATFORM.Weapon(weaponName);  //     
   WsfTrack currentTarget;  //     
end_script_variables

# SelectClosestTrack
script WsfTrack SelectClosestTrack()
   WsfTrack closestTarget;
   double closestDistance = 1000000000;  //         
   foreach (WsfTrack track in PLATFORM.MasterTrackList())
   {
      if (track.IsValid() && PLATFORM.GroundRangeTo(track) < closestDistance)
      {
         closestTarget = track;
         closestDistance = PLATFORM.GroundRangeTo(track);
      }
   }
   return closestTarget;
end_script

# PursueLogic
script void PursueLogic()
   if (currentTarget.IsValid())
   {
      double distanceToTarget = PLATFORM.GroundRangeTo(currentTarget);
      double targetSpeed = currentTarget.Speed();
      double platformSpeed = PLATFORM.Speed();
      double adjustedSpeed = targetSpeed * speedFactor;

      //     
      if (platformSpeed != adjustedSpeed)
      {
         PLATFORM.GoToSpeed(adjustedSpeed);
      }

      //     
      if (distanceToTarget > launchRange)
      {
         PLATFORM.GoToLocation(currentTarget.CurrentLocation());
         writeln("Moving closer to target...");
      }
   }
end_script

# CanEngage
script bool CanEngage()
   bool canEngage = false;
   if (aa_mrm.IsValid() &&   
      aa_mrm.QuantityRemaining() >= 1 &&    
      currentTarget.IsValid() && 
      PLATFORM.GroundRangeTo(currentTarget) < launchRange &&       
      WeaponsActiveFor(currentTarget.TrackId()) < 1        
   )
   {
      canEngage = true;
   }
   return canEngage;
end_script

# Engage
script void Engage()
   if (CanEngage())
   {
      Fire(currentTarget, "FIRE", weaponName, 1, PLATFORM);
   }
   else
   {
      writeln("Cannot engage: Conditions not met.");
   }
end_script

# States

evaluation_interval IDLE 10 sec
state IDLE
   on_entry
      writeln("Entering IDLE state.");
      currentTarget = SelectClosestTrack();
   end_on_entry

   next_state TRACKING
      return currentTarget.IsValid();
   end_next_state

   on_exit
      writeln("Exiting IDLE state.");
   end_on_exit
end_state

evaluation_interval TRACKING 10 sec
time_to_evaluate TRACKING 30 s
state TRACKING
   on_entry
      writeln("Entering TRACKING state.");
      currentTarget = SelectClosestTrack();
   end_on_entry

   next_state ENGAGING
      return CanEngage();
   end_next_state

   next_state PURSUING
      return currentTarget.IsValid() && !CanEngage();
   end_next_state

   on_exit
      writeln("Exiting TRACKING state.");
   end_on_exit
end_state

evaluation_interval ENGAGING 10 sec
state ENGAGING
   on_entry
      writeln("Entering ENGAGING state.");
      Engage();
   end_on_entry

   next_state IDLE
      return !currentTarget.IsValid() || aa_mrm.QuantityRemaining() == 0;
   end_next_state

   next_state TRACKING
      return currentTarget.IsValid() && !CanEngage();
   end_next_state

   on_exit
      writeln("Exiting ENGAGING state.");
   end_on_exit
end_state

evaluation_interval PURSUING 10 sec
state PURSUING
   on_entry
      writeln("Entering PURSUING state.");
   end_on_entry

   next_state ENGAGING
      return CanEngage();  //         
   end_next_state

   next_state TRACKING
      return !currentTarget.IsValid();
   end_next_state



   on_exit
      writeln("Exiting PURSUING state.");
   end_on_exit
end_state


   script bool pursueCondition()
      return (
         aa_mrm.QuantityRemaining() > 0 &&      
         currentTarget.IsValid()        
      );
   end_script
update_interval 100 sec
   on_update
   if(pursueCondition())
   {
      PursueLogic();  
   }
   if(aa_mrm.QuantityRemaining() < 0 &&      
         !currentTarget.IsValid() )
         {
            PROCESSOR.TurnOff();
         }
   end_on_update
end_processor
