# ****************************************************************************
# CUI
#
# The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
#
# The use, dissemination or disclosure of data in this file is subject to
# limitation or restriction. See accompanying README and LICENSE for details.
# ****************************************************************************




behavior radar-control

   script_debug_writes off

   script_variables
      bool                   mTurnOnFiringRadarInRange = false;
      string                 mFiringRadarName          = "radar";    // replace with sensor name on platform
      double                 mFiringRadarRange         = 1852 * 50;  // 50 nautical miles (units: meters)
      bool                   mTurnOnFiringRadarIfRWR   = true;
   end_script_variables



   precondition
      writeln_d("precondition radar-control");
      if (!PROCESSOR.IsA_TypeOf("WSF_RIPR_PROCESSOR"))
      {
         return Failure("behavior not attached to a RIPR processor!");
      }

      WsfTrack curTarget = ((WsfRIPRProcessor)PROCESSOR).GetTarget();
      if ((mTurnOnFiringRadarInRange && !curTarget.IsNull() && curTarget.IsValid()) || mTurnOnFiringRadarIfRWR)
      {
         return true;
      }
      return false;
   end_precondition



   script bool RWR_ShowsBeingTracked()
      for (int i=0; i < PLATFORM.SensorCount(); i=i+1)
      {
         WsfSensor sensor = PLATFORM.SensorEntry(i);
         if (sensor.IsA_TypeOf("WSF_ESM_SENSOR"))
         {
            foreach (WsfLocalTrack t in PLATFORM.MasterTrackList())
            {
               if (t.ContributorOf(PLATFORM, sensor))
               {
                  return true;
               }
            }
         }
      }
      return false;
   end_script



   execute
      writeln_d(PLATFORM.Name(), " executing radar-control, T=", TIME_NOW);

      WsfTrack curTarget = ((WsfRIPRProcessor)PROCESSOR).GetTarget();

      bool turnOnFiringRadar = false;
      //turn on firing radar if in range
      if (mTurnOnFiringRadarInRange && !curTarget.IsNull() && curTarget.IsValid())
      {
         //check range against radar range
         if (PLATFORM.SlantRangeTo(curTarget) < mFiringRadarRange)
         {
            #writeln_d(PLATFORM.Name()," within range of curTarget!");

            turnOnFiringRadar = true;
         }
         else
         {
            #writeln_d(PLATFORM.Name()," NOT within range of curTarget!");
         }
      }

      //turn on firing radar if being tracked
      if (mTurnOnFiringRadarIfRWR && !turnOnFiringRadar)
      {
         turnOnFiringRadar = RWR_ShowsBeingTracked();
      }

      if (turnOnFiringRadar)
      {
         //make sure radar is on
         WsfSensor firingRadar = PLATFORM.Sensor(mFiringRadarName);
         if (firingRadar.IsValid())
         {
            if (!firingRadar.IsTurnedOn())
            {
               firingRadar.TurnOn();
               writeln_d(PLATFORM.Name(), " turned on active radar ", mFiringRadarName);
            }
            else
            {
               writeln_d("radar already turned on");
            }
         }
      }

   end_execute

end_behavior

