# ****************************************************************************
# 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 -> alt347.f
// Replicates the 3,4,7,2 alternative behavior
// alt3472_slow_pull_right

behavior alt3472_slow_pull_right

   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 mAlternative3472Enabled = true;
      // Alternative ID
      int ilevel = 3;
      int kalt   = 4;
      int icall  = 7;
      int lcall  = 2;

      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 alt3472 slow_pull_right, 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 (!mAlternative3472Enabled)
      {
         writeln_d("behavior not enabled!");
         return Failure("behavior alternative not enabled");
      }

      ##alt347.f line 108    //initialize value
      #call vnorm(vp(1,me),myvdir)
      ##alt347.f line 135 - 148
      #iactn=4
      #acc0(1)=0.
      #acc0(2)=sroll
      #acc0(3)=-croll
      #call vxfrmc(rwep,dir0,acc0,2)
      #gmx=gmxsu
      #spd0=spdnow(me)+almin*tproj3*1.1
      #spd0 = amax1(200.,spd0)
      #spdmod = desspd

      Vec3 myvdir = PLATFORM.VelocityNED().Normal();
      int iactn = 4;
      double sroll = 0.2588190;   //sin of roll angle (15 deg)
      double croll = 0.9659258;   //cos of roll angle (15 deg)
      Vec3 acc0 = Vec3.Construct(0.0, sroll, -croll);
      dir0 = brawlerPlatform.ConvertWindtoNED(acc0);
      double gmxsu = brawlerPlatform.MaxTacticalGs();
      gmx = gmxsu;
      double almin  = brawlerPlatform.MinForwardAccelWithGravity();
      spd0 = PLATFORM.Speed() * MATH.FT_PER_M() + almin * brawlerPlatform.ProjectedTimeDelta() * 1.1;
      spd0 = MATH.Max(200.0, spd0);
      spd0 *= MATH.M_PER_FT();
      int spdmod = 1;   //desspd   // spdmod = thrttl (desspd = 1, thrttl = 2, desacc = 3)

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

   end_precondition

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

end_behavior

