
datablock TracerProjectileData(MagnumBullet)
{
   doDynamicClientHits = true;

   directDamage        = 0.4;
   directDamageType    = $DamageType::Bullet;
   explosion           = "ChaingunExplosion";
   splash              = ChaingunSplash;

   rearDamageFactor    = 1.35;

   sound  = MagnumProjectile;

   dryVelocity       = 525.0;
   wetVelocity       = 125.0;
   velInheritFactor  = 1.0;
   fizzleTimeMS      = 200;
   lifetimeMS        = 1000;
   explodeOnDeath    = false;
   reflectOnWaterImpactAngle = 0.0;
   explodeOnWaterImpact      = false;
   deflectionOnWaterImpact   = 0.0;
   fizzleUnderwaterMS        = 3500;

   tracerLength    = 20.0;
   tracerAlpha     = false;
   tracerMinPixels = 6;
   tracerColor     = "0.93 0.93 0.8 0.75";
   tracerTex[0]    = "special/tracer00";
   tracerTex[1]    = "special/tracercross";
   tracerWidth     = 0.12;
   crossSize       = 0.18;
   crossViewAng    = 0.990;
   renderCross     = true;

   decalData[0] = ChaingunDecal1;
   decalData[1] = ChaingunDecal2;
   decalData[2] = ChaingunDecal3;
   decalData[3] = ChaingunDecal4;
   decalData[4] = ChaingunDecal5;
   decalData[5] = ChaingunDecal6;
};

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

datablock ItemData(MagnumAmmo)
{
   className = Ammo;
   catagory = "Ammo";
 
   shapeFile = "ammo_chaingun.dts";
   mass = 1;
   trueMass = 0.022;

   elasticity = 0.2;
   friction = 0.6;

   scale = "0.8 0.8 0.8";

   pickupRadius = 2;
   pickUpName = "some magnum ammo";

   computeCRC = true;

};

datablock ItemData(Magnum)
{
   className    = Weapon;
   catagory     = "Spawn Items";
   shapeFile    = "weapon_elf.dts";

   image        = MagnumImage;
   mass         = 1;
   trueMass     = 2.5;

   elasticity   = 0.2;
   friction     = 0.6;

   pickupRadius = 2;
   pickUpName   = "a magnum";

   computeCRC = true;
   emap = true;
};

//--------------------------------------------------------------------------
// Weapon
//--------------------------------------
datablock ShapeBaseImageData(MagnumImage)
{
   className = WeaponImage;
   shapeFile = "weapon_elf.dts";

   item      = Magnum;
   ammo      = MagnumAmmo;

   projectile = MagnumBullet;
   projectileType = TracerProjectile;

   emap = true;

   casing = ShellDebris;

   UseCrossHair = Chaingun;

   shellExitDir       = "1.0 0.3 1.0";
   shellExitOffset    = "0.15 -0.56 -0.1";
   shellExitVariance  = 10.0;
   shellVelocity      = 4.0;

   WCS = true;
   WCSFirePattern = true;

   WCSProjectileCount = 3;

   projectileSpread = 4.0 / 1000.0;

   //---------------------------------------------
   stateName[0] = "Activate";
   stateTransitionOnTimeout[0] = "ActivateReady";
   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.1;
   stateRecoil[3] = LightRecoil;
   stateAllowImageChange[3] = false;
   stateScript[3] = "onFire";

   stateName[4] = "Reload";
   stateTransitionOnNoAmmo[4] = "NoAmmo";
   stateTransitionOnTimeout[4] = "Ready";
   stateTimeoutValue[4] = 0.8;
   stateAllowImageChange[4] = false;
   stateSequence[4] = "Reload";

   stateName[5] = "NoAmmo";
   stateTransitionOnAmmo[5] = "Reload";
   stateSequence[5] = "NoAmmo";
   stateTransitionOnTriggerDown[5] = "DryFire";

   stateName[6]       = "DryFire";
   stateSound[6]      = ChaingunDryFireSound;
   stateTimeoutValue[6]        = 1.5;
   stateTransitionOnTimeout[6] = "NoAmmo";
};

function MagnumImage::onMount(%this, %Player, %Slot)
{
   Parent::onMount(%this, %Player, %Slot);

   %Player.mountImage(MagnumImagePair, 1);
}

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

   %Player.unmountImage(1);
}

function MagnumImage::onFire(%data, %Player, %slot)
{
   if (%Player.PlayerNoFire == true)
      return;

   // If the clip is not reloaded, dryfire it and stop
   if (%Player.MagnumShots > 2 || %Player.IgnoreMagnumFire == true)
   {
      if (%Player.IgnoreMagnumFire != true)
         serverPlay3D(ChaingunDryFireSound, %Player.getMuzzlePoint($WeaponSlot));
      return;
   }

   %Data.lightStart = getSimTime();

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

      } else {
         if (%Player.getEnergyLevel() > 20)
         {   
            %Player.setCloaked(false);
            %Player.reCloak = %Player.schedule(650, "setCloaked", true);
         }
      }   
   }

   if (%Player.WCSFireMode !$= "")
   {
      %Pi = 3.1415926;
      %ProjectileCount = %Data.WCSProjectileCount;

      %Spread = 2 * %Pi * %Data.projectileSpread;

      %i = 0;
      while (%i < %ProjectileCount)
      {
         %Player.schedule(%i * 650, MagnumBurstFire, %Spread);

         %Spread *= 1.1;
         %i++;
      }

      %Player.IgnoreMagnumFire = true;
      %Player.schedule(%i * 750, IgnoreMagnumReset);
   } else {
      %Position = %Player.getMuzzlePoint($WeaponSlot);

      %Projectile = new TracerProjectile()
      {
         dataBlock        = MagnumBullet;
         initialDirection = %Player.getMuzzleVector($WeaponSlot);
         initialPosition  = %Position;
         sourceObject     = %Player;
         sourceSlot       = $WeaponSlot;
      };
      MissionCleanUp.Add(%Projectile);

      serverPlay3D(MagnumFireSound, %Position);

      %Player.schedule(250, decInventory, %Data.ammo, 1);

      %Player.MagnumShots++;
      %Player.schedule(6000, MagnumReload);
   }
}

function MagnumImage::WCS(%WCS, %Player, %Direction)
{
   if (%Player.MagnumShots > 0)
   {
      %Message = "Magnum is currently reloading, you must wait to change firing modes.";
      messageClient(%Player.client, '', %Message);
      return;
   }

   // If not moving backwards, then move forward
   if (%Direction != -1)
      %Direction = 1;

   if (%Player.WCSFireMode !$= "Burst")
   {
      %Player.WCSFireMode = "Burst";
      %Message = "3 Shot Burst firing mode selected.";
      %Player.WCSAlternating = 0;
   } else {
      %Player.WCSFireMode = "";
      %Message = "Single Shot firing mode selected.";
      %Player.WCSAlternating = 1;
   }

   %Client = %Player.client;

   %Lines = 1;

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

function Player::MagnumFire(%Player, %Spread)
{
   // Check to make sure the ammo situation is still good
   if (%Player.inv[MagnumAmmo] <= 0)
      return;
   else
      %Player.schedule(250, decInventory, MagnumAmmo, 1);

   %Player.MagnumShots++;

   %Vector = %Player.getMuzzleVector($WeaponSlot);
   %VectorX = (getRandom() - 0.5) * %Spread;
   %VectorY = (getRandom() - 0.5) * %Spread;
   %VectorZ = (getRandom() - 0.5) * %Spread;
   %Matrix = MatrixCreateFromEuler(%VectorX @ " " @ %VectorY @ " " @ %VectorZ);
   %FiringVector = MatrixMulVector(%Matrix, %Vector);

   %Position = %Player.getMuzzlePoint($WeaponSlot);

   %Projectile = new TracerProjectile()
   {
      dataBlock        = MagnumBullet;
      initialDirection = %FiringVector;
      initialPosition  = %Position;
      sourceObject     = %Player;
      sourceSlot       = $WeaponSlot;
   };
   MissionCleanUp.Add(%Projectile);

   serverPlay3D(MagnumFireSound, %Position);

   %Player.schedule(5800, MagnumReload);

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

}

datablock ShapeBaseImageData(MagnumImagePair)
{
   className = WeaponImage;
   shapeFile = "ammo_mortar.dts";

   offset = "-0.12 1.13 0.2";
   rotation = "0 0 1 90";

   emap = true;
};

function Player::MagnumReload(%Player)
{
   %Player.MagnumShots--;
}

function Player::IgnoreMagnumReset(%Player)
{
   %Player.IgnoreMagnumFire = "";
}

