# ****************************************************************************
# 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.
# ****************************************************************************
###############################################################################
#                          ------ UNCLASSIFIED ------
###############################################################################
#
# ***** WARNING ***** WARNING ***** WARNING ***** WARNING ***** WARNING *****
#
# This definition is intended to be EXPORTABLE. It is defined using publicly
# available, attributable sources. DO NOT ADD ANYTHING BASED ON OTHER SOURCES!
#
# ***** WARNING ***** WARNING ***** WARNING ***** WARNING ***** WARNING *****
#
###############################################################################
#
# References: (PDF files contained in the subdirectory blue_sr_sam_ref)
#
# 1) Jane's JLAD Patriot PAC-3, 21-May-2009
#    Diameter: .255 m, Length 5.205
#    Mass: 315 kg for BLUE_SR_SAM, 312 kg for BLUE_SR_SAM MSE
#    Max Speed: 1700 m/s for BLUE_SR_SAM, >1700 m/s for BLUE_SR_SAM MSE
#    Max Range: 15 km for BLUE_SR_SAM, 22 km for BLUE_SR_SAM MSE
#
# 2) http://en.wikipedia.org/wiki/MIM-104_Patriot
#    'The system's missiles are transported on and launched from the M901
#    Launching Station, which can carry up to four PAC-2 missiles or up to
#    sixteen PAC-3 missiles.'
#
###############################################################################
#
# NOTE: If you modify any parameter in this file, you must rerun 'weapon_tools'
#       with 'blue_sr_sam_lcg.txt' as input to regenerate the launch computer data.
#
###############################################################################

# Signature data is unknown...

infrared_signature BLUE_SR_SAM_INFRARED_SIGNATURE
   constant 1 watts/steradian
end_infrared_signature

optical_signature  BLUE_SR_SAM_OPTICAL_SIGNATURE
   constant 1 m^2
end_optical_signature

radar_signature    BLUE_SR_SAM_RADAR_SIGNATURE
   constant 1 m^2
end_radar_signature

# Nominal lethality data.
weapon_effects BLUE_SR_SAM_EFFECT WSF_GRADUATED_LETHALITY
   radius_and_pk 100.0 m 0.9

   # This script will reduce the effectiveness if the impact geometry is not desirable.

   script void on_weapon_target_engagement(WsfPlatform aTarget)
      WsfPlatform weapon = WeaponPlatform();
      if (! (aTarget.IsValid() && weapon.IsValid())) return;

      // Compute the angle between the velocity vectors. A head-on hit would be 180 degrees.
      // (velocity vectors in opposite directions).
      Vec3 weaponVel = weapon.VelocityWCS();
      Vec3 targetVel = aTarget.VelocityWCS();
      double weaponVelMag = weaponVel.Magnitude();
      double targetVelMag = targetVel.Magnitude();
      if ((weaponVelMag > 1.0) && (targetVelMag > 1.0))
      {
         double cosAngle = Vec3.Dot(weaponVel, targetVel) / (weaponVelMag * targetVelMag);
         if (cosAngle < -1.0) cosAngle = -1.0;
         if (cosAngle >  1.0) cosAngle =  1.0;
         double angle = Math.ACos(cosAngle);
         writeln("Angle=", angle);
         //TODO if (angle < xxx) SetPkDegrade(0.6);
      }
   end_script

end_weapon_effects

###############################################################################

aero BLUE_SR_SAM_AERO WSF_AERO
   reference_area            0.0510705 m2        # Ref 1, 0.255 m diameter
   aspect_ratio              2.0
   cl_max                    4.5
   cl_max                    20.0                # TODO-TEST

   # Values estimated from Hoerner's Drag book
   cd_zero_subsonic          0.10
   cd_zero_supersonic        0.35
   mach_begin_cd_rise        0.95
   mach_end_cd_rise          1.30
   mach_max_supersonic       6.00
end_aero

mover BLUE_SR_SAM_MOVER WSF_GUIDED_MOVER
   integration_timestep      0.001 secs
   update_interval           0.01 secs

   stage 1
      aero                   BLUE_SR_SAM_AERO
      total_mass             315 kg              # Ref 1

#     Ref 2 defines the warhead to be 73 kg
#     Add another 20% of remaining mass for structure. (Space Shuttle SRB is
#     about 15% structure, but we're making it 20% to account for higher G's.
#
#     315 kg - 73 kg = 242 kg
#     242 kg * 0.2   = 48.4 kg
#     So empty mass = 73 + 48 = 121.4 kg

      empty_mass             121.4 kg            # See calculations above
      specific_impulse       250 sec             # Nominal solid rocket
      burn_time              13 sec              # Estimated to get speed ~= 1700 m/s
   end_stage

end_mover

###############################################################################

processor BLUE_SR_SAM_GUIDANCE WSF_GUIDANCE_COMPUTER

   # No datalink or seeker is being used, so we must guide to the true target
   guide_to_truth                      true

   phase LAUNCH
      guidance_delay                   100.0 sec
      next_phase PITCH_OVER when phase_time >= 0.1 sec
   end_phase

   phase PITCH_OVER
      commanded_flight_path_angle      from_launch_computer
      proportional_navigation_gain     0.0                 # Don't guide to target
      velocity_pursuit_gain            0.0                 # Don't guide to target
      maximum_commanded_g              20.0 g
      next_phase MIDCOURSE when on_commanded_flight_path_angle
   end_phase

   phase MIDCOURSE
      # no guidance (just use gravity turn)
      guidance_delay                   1000.0 sec
      next_phase TERMINAL when time_to_intercept < 12.0 sec
   end_phase

   phase TERMINAL
      proportional_navigation_gain     10.0
      proportional_navigation_method   augmented

      # Disable velocity pursuit. The angle which controls the switch between
      # velocity pursuit and pronav are too sensitive at short ranges.
      velocity_pursuit_gain            0.0

      # Disable g-bias. The target is also falling, so we don't want to fight
      # against it! (this is assuming terminal intercept, not boost)
      g_bias                           0.0
   end_phase
end_processor

###############################################################################

platform_type BLUE_SR_SAM_BASE WSF_PLATFORM
  icon SA-10_Missile  

  infrared_signature BLUE_SR_SAM_INFRARED_SIGNATURE
  optical_signature  BLUE_SR_SAM_OPTICAL_SIGNATURE
  radar_signature    BLUE_SR_SAM_RADAR_SIGNATURE

  weapon_effects     BLUE_SR_SAM_EFFECT

  mover BLUE_SR_SAM_MOVER
     #show_status
     #time_history_path .
  end_mover

  processor guidance BLUE_SR_SAM_GUIDANCE
     #show_status
     #show_graphics
  end_processor

  processor fuse WSF_AIR_TARGET_FUSE
     detonate_below_mach 1.0

     # The following are used to limit the run time during launch computer
     # generation. They can be removed for real simulation use if one really
     # wants to see the missile do something after a big miss.

     detonate_above_height_msl      35 km
     max_time_of_flight_to_detonate 80 sec
  end_processor
end_platform_type

###############################################################################

launch_computer BLUE_SR_SAM_LAUNCH_COMPUTER WSF_BALLISTIC_MISSILE_LAUNCH_COMPUTER
   surface_to_air_table      weapons/sam/blue_sr_sam_launch_data.txt
   integration_time_step     0.05 sec

   # Disallow intercepts at unreasonable angles
   maximum_intercept_angle   60 deg
end_launch_computer

###############################################################################

weapon BLUE_SR_SAM WSF_EXPLICIT_WEAPON

   # The location offset puts the weapon center at half the length - 1 m.
   # This assumes the scenario creator puts the launching platform at 1 m agl.

   location 0.0 0.0 -1.6025 m                    # Ref 1, length = 5.205 m

   launched_platform_type BLUE_SR_SAM

   launch_computer BLUE_SR_SAM_LAUNCH_COMPUTER
   end_launch_computer

   tilt 35 deg

   # This causes a call to the launch computer to compute the predicted intercept.
   # The launcher will then be cued to the proper angles (Subject to the slewing
   # limits defined below). This is a bit of a hack until the guidance computer
   # is changed...

   cue_to_predicted_intercept true
   slew_mode azimuth
   azimuth_slew_limits -180 deg 180 deg

   quantity 16                                   # Ref 2

   # Abort the launch if the launch computer didn't provide the required data
   require_loft_angle
end_weapon
