//------------------------------------------------------------------------------
// requestNextToShoot() -- Requests a specific track to be the next-to-shoot;
//                         Returns true if successful
//------------------------------------------------------------------------------
bool OnboardComputer::requestNextToShoot(const Track* const nts)
{
   bool ok = false;
   if (nts != 0) {
       
      // First, let's get the active track list
      const unsigned int MAX_TRKS = 20;
      SPtr<Track> trackList[MAX_TRKS];

      int n = 0;
      TrackManager* tm = getTrackManagerByType(typeid(AirTrkMgr));
      // fall back to whatever TM we have, if we don't have an AirTrkMgr
      if (tm == 0) tm = getTrackManagerByType(typeid(TrackManager));
      if (tm != 0) n = tm->getTrackList(trackList,MAX_TRKS);
       
      if (n > 0) {

         // try to find the requested track and save its index
         int nNTS = -1;
         for (int i = 0; i < n && nNTS < 0; i++) {
            if (nts == trackList[i]) nNTS = i;
         }

         // update the shoot list index values in the tracks
         if (nNTS >= 0) {
            // Start at the next-to-shoot
            int idx = nNTS;
            for (int i = 0; i < n; i++) {
               trackList[idx++]->setShootListIndex(i+1);
               if (idx >= n) idx = 0;
            }
            ok = true;
         }

         // Update the next to shoot
         if (ok) setNextToShoot(trackList[nNTS]);
      }
   }
   return ok;
}
//------------------------------------------------------------------------------
// updateShootList() -- Update the shoot list.  When the step flag is true,
//                      force step to the next target.
//------------------------------------------------------------------------------
void OnboardComputer::updateShootList(const bool step)
{
   // Temporary next to shoot indexes
   int cNTS = -1;  // Current
   int nNTS = -1;  // New

   // First, let's get the active track list
   const unsigned int MAX_TRKS = 20;
   SPtr<Track> trackList[MAX_TRKS];

   int n = 0;
   TrackManager* tm = getTrackManagerByType(typeid(AirTrkMgr));
   // fall back to whatever TM we have, if we don't have an AirTrkMgr
   if (tm == 0) tm = getTrackManagerByType(typeid(TrackManager));
   if (tm != 0) n = tm->getTrackList(trackList,MAX_TRKS);
    
   if (n > 0) {
       
      // ---
      // Find the current next-to-shoot index
      // ---
      Track* nts = nextToShoot;
      if (nextToShoot != 0) {
         for (int i = 0; i < n && cNTS < 0; i++) {
            if (nts == trackList[i]) cNTS = i;
         }
      }
        
      // ---
      // Update the next to shoot?
      // ---
      if (cNTS < 0) {
         // 1) When we don't have or couldn't find our NTS, pick the closest track
         //    that has a valid ground speed.
         int i = 0;
         do {
            if (trackList[i]->getGroundSpeed() >= 1.0f) {
               if (nNTS >= 0) {
                  // is this one closer?
                  if (trackList[i]->getRange() < trackList[nNTS]->getRange()) {
                     nNTS = i;
                  }
               }
               else {
                  // only one so far
                  nNTS = i;
               }
            }
            i = i + 1;
            if (i >= n) i = 0;
         } while (i != 0);
      }
      else if (step) {
         // 2) When a target step has been requested ...
         int i = cNTS + 1;
         if (i >= n) i = 0;
         do {
            if (trackList[i]->getGroundSpeed() >= 50.0f) {
               nNTS = i;
            }
            i = i + 1;
            if (i >= n) i = 0;
         } while (nNTS < 0 && i != cNTS);
      }
      else {
         // 3) Keep the same next-to-shoot track
         nNTS = cNTS;
      }
        
      // ---
      // update the shoot list index values in the tracks
      // ---
      if (nNTS >= 0) {
         // Start at the next-to-shoot
         int idx = nNTS;
         for (int i = 0; i < n; i++) {
            trackList[idx++]->setShootListIndex(i+1);
            if (idx >= n) idx = 0;
         }
      }
      else {
         // When there isn't a next to shoot, clear all
         for (int i = 0; i < n; i++) trackList[i]->setShootListIndex(0);
      }
   }

   // Update the next to shoot
   if (nNTS >= 0) setNextToShoot( trackList[nNTS] );
   else setNextToShoot(0);
}