
//--------------------------------------------------------------------------
// Ammo Based Sniper rifle - the projectile drops slowly, the rifle is loud, 
// and it has a substantial impact to the target.
// 
//--------------------------------------------------------------------------

//--------------------------------------------------------------------------
// Projectile
//--------------------------------------------------------------------------

datablock GrenadeProjectileData(RifleBullet)
{
   projectileShapeName = "grenade_projectile.dts";
   directDamage        = 0.0;
   hasDamageRadius     = true;
   indirectDamage      = 0.5;
   damageRadius        = 0.5;
   kickBackStrength    = 1500;

   sound = ChaingunProjectile;
   explosion = ChaingunExplosion;
   splash = ChaingunSplash;
   velInheritFactor    = 1.0;

   scale = ("0.35 0.35 0.35");

   gravityMod        = 1.8;

   rearDamageFactor    = 1.2;

   grenadeElasticity = 0.0;
   grenadeFriction   = 1.0;
   armingDelayMS     = 250;
   muzzleVelocity    = 750;
   drag = 0.2;
};

datablock LinearProjectileData(LinearRifleBullet)
{
   projectileShapeName = "grenade_projectile.dts";
   directDamage        = 0.5;
   kickBackStrength    = 1500;

   sound = ChaingunProjectile;
   explosion = ChaingunExplosion;

   velInheritFactor    = 1.0;

   scale = ("0.35 0.35 0.35");

   rearDamageFactor    = 1.2;

   dryVelocity = 450;
   wetVelocity = 450;
};

//-------------------------------------------------------------
// Ammo
//-------------------------------------------------------------

datablock ItemData(RifleAmmo)
{
   className = Ammo;
   catagory = "Ammo";
   shapeFile = "ammo_chaingun.dts";

   mass = 1;
   trueMass = 0.01;

   elasticity = 0.2;
   friction = 0.6;
   pickupRadius = 1;
   pickUpName = "some sniper rifle shells";
};

//-------------------------------------------------------------
// Rifle and item...
//-------------------------------------------------------------
datablock ItemData(Rifle)
{
   className    = Weapon;
   catagory     = "Spawn Items";
   shapeFile    = "weapon_sniper.dts";
   image        = RifleImage;
   mass         = 1;
   trueMass     = 5;

   elasticity   = 0.1;
   friction     = 0.6;
   pickupRadius = 1;
   pickUpName = "a sniper rifle";
};

datablock ShapeBaseImageData(RifleImage)
{
   className = WeaponImage;
   shapeFile = "weapon_sniper.dts";

   item = Rifle;
   ammo = RifleAmmo;
   projectile = RifleBullet;
   offset = "0 0 0";
   armThread = looksn;

   UseCrossHair = SniperRifle;

   muzzleFlash = RifleMuzzleFlash;

   stateName[0] = "Activate";
   stateTransitionOnTimeout[0] = "Ready";
   stateTimeoutValue[0] = 1.0;
   stateSequence[0] = "Activate";

   stateName[1] = "Ready";
   stateTransitionOnNoAmmo[1]  = "NoAmmo";
   stateTransitionOnTriggerDown[1] = "Fire";
   stateSound[1] = DiscSwitchSound;

   stateName[2] = "Fire";
   stateSequence[2] = "Fire";
   stateSequenceRandomFlash[2] = true;
   stateSound[2] = RifleFireSound;
   stateRecoil[2] = LightRecoil;
   stateAllowImageChange[2] = false;
   stateScript[2] = "onFire";
   stateFire[2] = true;
   stateTimeoutValue[2] = 1.0;
   stateTransitionOnTimeout[2] = "Reload";
   stateTransitionOnNoAmmo[2] = "NoAmmo";

   stateName[3] = "Reload";
   stateSequence[3] = "Reload";
   stateTransitionOnNoAmmo[3] = "NoAmmo";
   stateTransitionOnTimeout[3] = "Ready";
   stateTimeoutValue[3] = 3;

   stateName[4] = "NoAmmo";
   stateTransitionOnTriggerDown[4] = "DryFire";
   stateTransitionOnAmmo[4] = "Ready";

   stateName[5] = "DryFire";
   stateTransitionOnTimeout[5] = "NoAmmo";
   stateTimeoutValue[5] = 1.5;
   stateTransitionOnAmmo[5] = "Ready";
   stateSound[5] = ChaingunDryFireSound;
};

function RifleImage::onFire(%Data, %Player, %Slot)
{
   %Data.lightStart = getSimTime();

   // If cloaked, uncloak the player for 0.5 seconds to cause the partial phasing effect
   if (%Player.station $= "" && %Player.isCloaked())
   {
      if (%Player.respawnCloakThread !$= "")
      {
         Cancel(%Player.respawnCloakThread);
      }   
      %Player.setCloaked(false);
   }

   // Search for an object in player's LOS to determine distance
   %MuzzlePosition = %Player.getMuzzlePoint(%Slot);
   %MuzzleVector = %Player.getMuzzleVector(%Slot);
   %ScaledVector = VectorScale(%MuzzleVector, 180);
   %End = VectorAdd(%MuzzlePosition, %ScaledVector);

   %Target = ContainerRayCast(%MuzzlePosition, %End, $EverythingMask);

   // If nothing was found, we fire like normal.  If something was, then 
   // fire the linear version of it.
   if(%Target $= "0")
   {
      echo("Firing Regular Bullet");
      %Projectile = new GrenadeProjectile()
      {
         dataBlock        = RifleBullet;
         initialDirection = %MuzzleVector;
         initialPosition  = %MuzzlePosition;
         sourceObject     = %Player;
         sourceSlot       = %Slot;
      };
   } else {
      echo("Firing Linear Bullet");
      %Projectile = new LinearProjectile()
      {
         dataBlock        = LinearRifleBullet;
         initialDirection = %MuzzleVector;
         initialPosition  = %MuzzlePosition;
         sourceObject     = %Player;
         sourceSlot       = %Slot;
      };
   }
   MissionCleanup.add(%Projectile);

   %Player.lastProjectile = %Projectile;
   %Player.deleteLastProjectile = %Data.deleteLastProjectile;
   
   // Take the ammo for firing
   %Player.decInventory(%Data.ammo,1);

   return(%Projectile);
}

function RifleImage::onMount(%this, %Player, %Slot)
{
   %Player.mountImage(RifleImagePair, 1);

   Parent::onMount(%this, %Player, %Slot);
}

function RifleImage::onunMount(%this, %Player, %Slot)
{
   Parent::onunMount(%this, %Player, %Slot);

   %Player.unmountImage(1);
}

datablock ShapeBaseImageData(RifleImagePair)
{
   className = WeaponImage;
   shapeFile = "weapon_energy.dts";

   item = Rifle;
   ammo = RifleAmmo;
   projectile = RifleBullet;

   offset = "0.0 0.0 -0.135";
   rotation = "0 0 0 90";

   emap = true;
};