# ****************************************************************************
# 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.
# ****************************************************************************

// converted BRAWLER v7.5 alt39.f
// Replicates the evade missile 3,9,1,1 alternative behavior
// GENERATES MISSILE EVASION MANEUVER


behavior alt3911_evade_missile

   script_debug_writes off

   script_variables

      WsfQuantumTaskerProcessor processor;
      WsfThreatProcessor        threatProc;
      WsfBrawlerPlatform        brawlerPlatform;

      //**********************************************************************//
      //** debugging parameters                                             **//
      //**********************************************************************//
      bool     mDrawSteering     = false;

      //**********************************************************************//
      //** alternative parameters                                           **//
      //**********************************************************************//
      // Flag used to enable/disable this alternative
      bool mAlternative3911Enabled = true;

      // Alternative ID
      int ilevel = 3;
      int kalt   = 9;
      int icall  = 1;
      int lcall  = 1;

      // Maneuver Alternative flight values
      Vec3 mDir0 = Vec3();
      double mGMX = 1.0;
      double mSpd0 = 3.0;

      //**********************************************************************//
      //********* VARIABLES BELOW THIS LINE ARE NOT FOR USER EDITING *********//
      //**********************************************************************//
      WsfDraw  mDraw             = WsfDraw();
      double   mLastTime         = 0.0;

   end_script_variables

   on_init
      if (PROCESSOR.IsA_TypeOf("WSF_QUANTUM_TASKER_PROCESSOR"))
      {
         processor = (WsfQuantumTaskerProcessor)PROCESSOR;
      }
      threatProc = (WsfThreatProcessor)PLATFORM.Processor("incoming_threats");
      brawlerPlatform = (WsfBrawlerPlatform)PLATFORM;
   end_on_init

   precondition
      #writeln_d(PLATFORM.Name(), " precondition behavior_alt3911_straight_flight, T=", TIME_NOW);

      ### Evaluate conditions that would prevent behavior alternative from running

      if (!PROCESSOR.IsA_TypeOf("WSF_QUANTUM_TASKER_PROCESSOR"))
      {
         writeln_d("not a quantum tasker!");
         return Failure("behavior not attached to a WSF_QUANTUM_TASKER_PROCESSOR");
      }

      if (!mAlternative3911Enabled)
      {
         writeln_d("behavior not enabled!");
         return Failure("behavior alternative not enabled");
      }

      WsfTrack threat = threatProc.NearestThreat();
      if (!threat.IsValid())
      {
         // This check equivalent to noneed in alt39.f
         writeln_d("no incoming missiles");
         return Failure("no incoming missiles");
      }

      // ALL CONDITIONS PASS
      ### Generate Maneuver Alternative
#      iactn = 4
#      lenalt = lactn(iactn)
#      altdsc = altpk(3,9,1,1,0,0)
#      call xmit(3,direv,dir0)
#      gmx = gmxin
#      spd0 = throtm(iacid) <- limits throttle to 2 if there are IR missiles coming at me
#      spdmod = thrttl

      // Get stored evasion direction vector
      //Vec3 direvd = brawlerPlatform.EvasionDirection();
      Vec3 direvd = brawlerPlatform.EvasionDirection();
      mDir0 = direvd;
      mGMX = brawlerPlatform.MaxAvailableGs();
      mSpd0 = 2; // Always 2 for now

      ### Evaluate [Projected] Maneuver Alternative
      double score = brawlerPlatform.EvaluateVectorWithThrottle(mDir0, mGMX, mSpd0, ilevel, kalt, icall, lcall);
      return score;

   end_precondition


   execute
      ## what was evaluated should be actually performed now
      brawlerPlatform.FlyVectorWithThrottle(mDir0, mGMX, mSpd0);
   end_execute

end_behavior

