

//--------------------------------------
// Projectile
//--------------------------------------
datablock SniperProjectileData(BasicSniperShot)
{
   directDamage        = 0.54;
   directDamageType    = $DamageType::Laser;
   hasDamageRadius     = false;
   indirectDamage      = 0.0;
   damageRadius        = 0.0;

   rearDamageFactor    = 1.22;

   velInheritFactor    = 1.0;
   sound               = SniperRifleProjectileSound;
   explosion           = "SniperExplosion";
   splash              = SniperSplash;

   maxRifleRange       = 1200;
   rifleHeadMultiplier = 1.2;
   beamColor           = "1 0.1 0.1";
   fadeTime            = 1.5;
   startBeamWidth      = 0.145;
   endBeamWidth        = 0.25;
   pulseBeamWidth      = 0.5;
   beamFlareAngle      = 3.0;
   minFlareSize        = 0.0;
   maxFlareSize        = 400.0;
   pulseSpeed          = 6.0;
   pulseLength         = 0.150;

   lightRadius         = 4.0;
   lightColor          = "0.3 0.0 0.0 0.8";

   textureName[0]      = "special/flare";
   textureName[1]      = "special/nonlingradient";
   textureName[2]      = "special/laserrip01";
   textureName[3]      = "special/laserrip02";
   textureName[4]      = "special/laserrip03";
   textureName[5]      = "special/laserrip04";
   textureName[6]      = "special/laserrip05";
   textureName[7]      = "special/laserrip06";
   textureName[8]      = "special/laserrip07";
   textureName[9]      = "special/laserrip08";
   textureName[10]     = "special/laserrip09";
   textureName[11]     = "special/sniper00";

};


//--------------------------------------
// Rifle and item...
//--------------------------------------
datablock ItemData(SniperRifle)
{
   className    = Weapon;
   catagory     = "Spawn Items";

   shapeFile    = "weapon_sniper.dts";
   image        = SniperRifleImage;

   mass         = 1;
   trueMass     = 4;

   elasticity   = 0.2;
   friction     = 0.6;
   pickupRadius = 2;
   pickUpName = "a laser rifle";

   computeCRC = true;

};

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

   mass = 1;

   item = SniperRifle;
   armThread = looksn;

   projectile = BasicSniperShot;
   projectileType = SniperProjectile;

   projectileCount = 3;

   WCS = true;
   WCSFirePattern = true;

   usesEnergy = true;
   minEnergy = 6;

   stateName[0]                     = "Activate";
   stateTransitionOnTimeout[0]      = "ActivateReady";
   stateSound[0]                    = SniperRifleSwitchSound;
   stateTimeoutValue[0]             = 0.5;
   stateSequence[0]                 = "Activate";

   stateName[1]                     = "ActivateReady";
   stateTransitionOnLoaded[1]       = "Ready";
   stateTransitionOnNoAmmo[1]       = "NoAmmo";

   stateName[2]                     = "Ready";
   stateTransitionOnNoAmmo[2]       = "NoAmmo";
   stateTransitionOnTriggerDown[2]  = "Fire";

   stateName[3]                     = "Fire";
   stateTransitionOnTimeout[3]      = "Reload";
   stateTimeoutValue[3]             = 0.5;
   stateFire[3]                     = true;
   stateAllowImageChange[3]         = false;
   stateSequence[3]                 = "Fire";
   stateScript[3]                   = "onFire";

   stateName[4]                     = "Reload";
   stateTransitionOnTimeout[4]      = "Ready";
   stateTimeoutValue[4]             = 0.75;
   stateAllowImageChange[4]         = false;
   
   stateName[5]                     = "NoAmmo";
   stateTransitionOnAmmo[5]         = "Reload";
   stateTransitionOnTriggerDown[5]  = "DryFire";
   stateSequence[5]                 = "NoAmmo";
   
   stateName[6]                     = "DryFire";
   stateSound[6]                    = SniperRifleDryFireSound;
   stateTimeoutValue[6]             = 0.75;
   stateTransitionOnTimeout[6]      = "Ready";
};

function SniperRifleImage::onFire(%Data, %Player, %Slot)
{
   if (%Player.PlayerNoFire == true)
      return;

   %Position = posFromTransform(%Player.getTransform());

   if(%Player.hasEnergyPack $= "")
   {
      serverPlay3D(SniperRifleDryFireSound, %Position);
      return;
   }
   
   // Take 95% of the Players power for firing.
   %EnergyLevel = %Player.getEnergyLevel();

   // Determine the damage level based on Player Energy
   %MaxEnergy = %Player.getDataBlock().maxEnergy;
   %EnergyFactor = %EnergyLevel / %MaxEnergy;
   %DamageFactor  = (((%EnergyLevel * %EnergyLevel) / (%MaxEnergy * %MaxEnergy)) + (%EnergyFactor)) / 2 ;

   if (%Player.WCSFireMode $= "Pulse")
   {
      echo("Pulse Mode");

      %ProjectileCount = %Data.projectileCount;
      %DamageFactor *= 0.315;

      %EnergyStep = (%EnergyLevel  * 0.95) / %ProjectileCount;

      %i = 0;
      while (%i < %ProjectileCount)
      {
         %Player.schedule(%i * 280, LaserPulseFire, %DamageFactor);

         %NewLevel = %i * %EnergyStep;
         %Player.schedule(%i * 280, setEnergyLevel, %EnergyLevel - %NewLevel);

         %i++;
      }
   } else {
      echo("Normal Mode");

      // Spawn the New Projectile and set the power of it to the energy available
      %Projectile = new SniperProjectile()
      {
         dataBlock        = BasicSniperShot;
         initialDirection = %Player.getMuzzleVector(%Slot);
         initialPosition  = %Player.getMuzzlePoint(%Slot);
         sourceObject     = %Player;
         damageFactor     = %DamageFactor;
         sourceSlot       = %Slot;
      };
      %Projectile.setEnergyPercentage(%DamageFactor);
      MissionCleanup.add(%Projectile);

      %Player.setEnergyLevel(%EnergyLevel * 0.05);

      // Make Sound
      %Player.lastProjectile = %Projectile;
      serverPlay3D(SniperRifleFireSound, %Position);
   }

   // AI hook
   if(%Player.client)
      %Player.client.projectile = %Projectile;
}

function SniperRifleImage::WCS(%WCS, %Player, %Direction)
{
   // If not moving backwards, then move forward
   if (%Direction != -1)
      %Direction = 1;

   switch$ (%Player.WCSFireMode)
   {
      case "Pulse" :
         %Player.WCSFireMode = "";
         %Message = "Default firing mode selected.";

      default :
         %Player.WCSFireMode = "Pulse";
         %Message = "Pulse firing mode selected.";
   }

   %Client = %Player.client;

   %Lines = 1;

   bottomPrint(%Client, %Message, 10, %Lines);
      
   %Client.HelpSchedule = schedule(%Lines * 6000, %Client, clearBottomPrint, %Client);
}

function Player::LaserPulseFire(%Player, %DamageFactor)
{
   echo("Firing Pulse now");

   %Projectile = new SniperProjectile()
   {
      dataBlock        = BasicSniperShot;
      initialDirection = %Player.getMuzzleVector($WeaponSlot);
      initialPosition  = %Player.getMuzzlePoint($WeaponSlot);
      sourceObject     = %Player;
      damageFactor     = %DamageFactor;
      sourceSlot       = $WeaponSlot;

   };
   MissionCleanUp.Add(%Projectile);
   %Projectile.setEnergyPercentage(%DamageFactor / 0.4);

   // Make Sound
   %Player.lastProjectile = %Projectile;
   serverPlay3D(SniperRifleFireSound, %Position);

   // AI hook
   if(%Player.client)
      %Player.client.projectile = %Projectile;
}