# ****************************************************************************
# 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 alt34.f -> alt345.f
// Replicates the 3,4,5,1 alternative behavior
// alt3451_pull_gmax_sust

include_once BrawlerScriptUtil.txt

behavior alt3451_pull_gmax_sust

   script_debug_writes off

   script_variables

      WsfQuantumTaskerProcessor processor;
      WsfPerceptionProcessor    perception;
      WsfBrawlerPlatform        brawlerPlatform;

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

      //**********************************************************************//
      //** alternative parameters                                           **//
      //**********************************************************************//
      // Flag used to enable/disable this alternative
      bool mAlternative3451Enabled = true;
      bool mCheckRollOverTopValue = true;   #this alternative assumes a different one has already evaluated and flown (to set the roll over the top value)
      // Alternative ID
      int ilevel = 3;
      int kalt   = 4;
      int icall  = 5;
      int lcall  = 1;

      Vec3 a0;      # maneuver plane
      Vec3 dir0;    # direction vector
      double gmx;   # max Gs
      double spd0;  # throttle setting

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

   end_script_variables

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


   precondition
      #writeln_d(PLATFORM.Name(), " precondition alt3451 pull_gmax_sust, 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 (!mAlternative3451Enabled)
      {
         writeln_d("behavior not enabled!");
         return Failure("behavior alternative not enabled");
      }

      ##alt34.f line 160    //filter out this alternative?
      double valrot = brawlerPlatform.RollOverTopValue();
      if(mCheckRollOverTopValue == true && valrot == 0.0)
      {
         string msg = write_str("T=",TIME_NOW,", alt ", ilevel, kalt, icall, lcall, " valrot == 0");
         writeln_d(msg);
         return Failure(msg);
      }

      ##alt345.f line 104 - 119
      #iactn = 4
      #call makeh(vp(1,me),rot)
      #dir0(1) = 0.001
      #dir0(2) = 0.0
      #dir0(3) = -1.000
      #call vxfrmc(rot,dir0,dir0,2)
      #call vnorm(dir0,dir0)
      #gmx = gmxsu
      #spd0 = 3.
      #spdmod = thrttl

      int iactn = 4;
      Vec3 vp = PLATFORM.VelocityNED();
      Array<double> rot = makeh(vp);
      dir0 = Vec3.Construct(0.001, 0.0, -1.0);
      dir0 = vxfrmc2(rot, dir0);
      dir0.Normalize();
      double gmxsu = brawlerPlatform.MaxTacticalGs();
      gmx = gmxsu;
      spd0 = 3.0;
      int spdmod = 2;   //thrttl   // spdmod = thrttl (desspd = 1, thrttl = 2, desacc = 3)

      ### Project and Evaluate (Score) Maneuver Alternative
      double score = brawlerPlatform.EvaluateVectorWithThrottle(dir0, gmx, spd0, ilevel, kalt, icall, lcall);

      return score;

   end_precondition

   execute
      ## what was evaluated should be actually performed now
      PLATFORM.FlyVectorWithThrottle(dir0, gmx, spd0);
   end_execute

end_behavior

