
$HandInvThrowTimeout = 1200; // 1.2 second between throwing grenades or mines

$WeaponsHudData[0, bitmapName] = "gui/hud_blaster.png";
$WeaponsHudData[0, itemDataName] = "Blaster";
$WeaponsHudData[1, bitmapName] = "gui/hud_plasma.png";
$WeaponsHudData[1, itemDataName] = "Plasma";
$WeaponsHudData[1, ammoDataName] = "PlasmaAmmo";
$WeaponsHudData[2, bitmapName] = "gui/hud_chaingun.png";
$WeaponsHudData[2, itemDataName] = "Chaingun";
$WeaponsHudData[2, ammoDataName] = "ChaingunAmmo";
$WeaponsHudData[3, bitmapName] = "gui/hud_disc.png";
$WeaponsHudData[3, itemDataName] = "Disc";
$WeaponsHudData[3, ammoDataName] = "DiscAmmo";
$WeaponsHudData[4, bitmapName] = "gui/hud_grenlaunch.png";
$WeaponsHudData[4, itemDataName] = "GrenadeLauncher";
$WeaponsHudData[4, ammoDataName] = "GrenadeLauncherAmmo";
$WeaponsHudData[5, bitmapName] = "gui/hud_sniper.png";
$WeaponsHudData[5, itemDataName] = "SniperRifle";
$WeaponsHudData[6, bitmapName] = "gui/hud_elfgun.png";
$WeaponsHudData[6, itemDataName] = "ELFGun";
$WeaponsHudData[7, bitmapName] = "gui/hud_mortor.png";
$WeaponsHudData[7, itemDataName] = "Mortar";
$WeaponsHudData[7, ammoDataName] = "MortarAmmo";
$WeaponsHudData[8, bitmapName] = "gui/hud_missiles.png";
$WeaponsHudData[8, itemDataName] = "MissileLauncher";
$WeaponsHudData[8, ammoDataName] = "MissileLauncherAmmo";
$WeaponsHudData[9, bitmapName]   = "gui/hud_targetlaser.png";
$WeaponsHudData[9, itemDataName] = "TargetingLaser";
$WeaponsHudData[10, bitmapName]   = "gui/hud_shocklance.png";
$WeaponsHudData[10, itemDataName] = "ShockLance";
$WeaponsHudData[11, bitmapName] = "gui/hud_sniper.png";
$WeaponsHudData[11, itemDataName] = "Rifle";
$WeaponsHudData[11, ammoDataName] = "RifleAmmo";
$WeaponsHudData[12, bitmapName] = "gui/hud_chaingun.png";
$WeaponsHudData[12, itemDataName] = "Shotgun";
$WeaponsHudData[12, ammoDataName] = "ShotgunAmmo";
$WeaponsHudData[13, bitmapName] = "gui/hud_plasma.png";
$WeaponsHudData[13, itemDataName] = "ParticleCannon";
$WeaponsHudData[14, itemDataName] = "Magnum";
$WeaponsHudData[14, ammoDataName] = "MagnumAmmo";

$WeaponsHudCount = 15;

$ammoType[0] = "PlasmaAmmo";
$ammoType[1] = "DiscAmmo";
$ammoType[2] = "GrenadeLauncherAmmo";
$ammoType[3] = "MortarAmmo";
$ammoType[4] = "MissileLauncherAmmo";
$ammoType[5] = "ChaingunAmmo";
$ammoType[6] = "RifleAmmo";
$ammoType[7] = "ShotgunAmmo";
$ammoType[8] = "MagnumAmmo";

$AmmoIncrement[PlasmaAmmo]          = 12;
$AmmoIncrement[ChaingunAmmo]        = 25;
$AmmoIncrement[DiscAmmo]            = 5;
$AmmoIncrement[GrenadeLauncherAmmo] = 6;
$AmmoIncrement[MortarAmmo]          = 4;
$AmmoIncrement[MissileLauncherAmmo] = 2;
$AmmoIncrement[Mine]                = 2;
$AmmoIncrement[EMPMine]             = 2;
$AmmoIncrement[IncendiaryMine]      = 2;
$AmmoIncrement[StunMine]            = 2;
$AmmoIncrement[ConcussionMine]      = 2;
$AmmoIncrement[Grenade]             = 3;
$AmmoIncrement[CameraGrenade]       = 2;
$AmmoIncrement[ECMGrenade]          = 2;
$AmmoIncrement[FlashGrenade]        = 2;
$AmmoIncrement[FlareGrenade]        = 3;
$AmmoIncrement[PlasticExplosive]    = 2;
$AmmoIncrement[ConcussionGrenade]   = 2;
$AmmoIncrement[IncendiaryGrenade]   = 2;
$AmmoIncrement[EMPGrenade]          = 2;
$AmmoIncrement[RepairKit]           = 2;
$AmmoIncrement[RifleAmmo]           = 8;
$AmmoIncrement[ShotgunAmmo]         = 12;
$AmmoIncrement[MagnumAmmo]          = 9;

//----------------------------------------------------------------------------
// Weapons scripts
//--------------------------------------


exec("scripts/weapons/blaster.cs");
exec("scripts/weapons/plasma.cs");
exec("scripts/weapons/chaingun.cs");
exec("scripts/weapons/disc.cs");
exec("scripts/weapons/missileLauncher.cs");
exec("scripts/weapons/grenadeLauncher.cs");
exec("scripts/weapons/mortar.cs");
exec("scripts/weapons/sniperRifle.cs");
exec("scripts/weapons/Rifle.cs");
exec("scripts/weapons/Shotgun.cs");
exec("scripts/weapons/ELFGun.cs");
exec("scripts/weapons/targetingLaser.cs");
exec("scripts/weapons/FlashLight.cs");
exec("scripts/weapons/shockLance.cs");
exec("scripts/weapons/particlecannon.cs");
exec("scripts/weapons/magnum.cs");

exec("scripts/weapons/Grenade.cs");
exec("scripts/weapons/Mine.cs");
exec("scripts/weapons/CameraGrenade.cs");
exec("scripts/weapons/ConcussionGrenade.cs");
exec("scripts/weapons/EMPGrenade.cs");
exec("scripts/weapons/FlareGrenade.cs");
exec("scripts/weapons/IncendiaryGrenade.cs");
exec("scripts/weapons/StunGrenade.cs");
exec("scripts/weapons/ECMGrenade.cs");
exec("scripts/weapons/EMPMine.cs");
exec("scripts/weapons/IncendiaryMine.cs");
exec("scripts/weapons/StunMine.cs");
exec("scripts/weapons/ConcussionMine.cs");
exec("scripts/weapons/plasticexplosive.cs");

//----------------------------------------------------------------------------

function Weapon::onUse(%data, %Player)
{
   if (Game.weaponOnUse(%data, %Player))
      if (%Player.getDataBlock().className $= Armor)
         %Player.mountImage(%data.image, $WeaponSlot);
}

function WeaponImage::onMount(%Weapon, %Player, %Slot)
{
   // If not a player, just ignore.
   if(%Player.getClassName() !$= "Player")
      return;

   %Client = %Player.Client;

   // Use the right Arm position (Either Look, LookMs, LookSn).
   if (%Weapon.armthread $= "")
      %Player.setArmThread(look);
   else
      %Player.setArmThread(%Weapon.armThread);

   // Initial ammo state
   if (%Player.getMountedImage($WeaponSlot).ammo !$= "")
   {
      if (%Player.getInventory(%Weapon.ammo) > 0)
         %Player.setImageAmmo(%Slot, true);
   }

   // Reset the WCS
   %Player.WCSProjectile = "";
   %Player.WCSFireMode = "";

   if (%Client.HavocClient == true)
   {
      %Item = %Weapon.item;
   } else {
      // Change the CrossHair to what is appropriate.  If a new weapon, use
      // the same as the old one.  If not, use the old one.
      %Item = %Weapon.UseCrossHair;
      if (%Item $= "")
         %Item = %Weapon.item;
   }
   %Client.setWeaponsHudActive(%Item);

   // Check the Ammo slot
   if (%Player.getMountedImage($WeaponSlot).ammo !$= "")
      %Client.setAmmoHudCount(%Player.getInventory(%Weapon.ammo));
   else
      %Client.setAmmoHudCount(-1);

   // If the Auto-Rocket Pack is on and the player has the missile launcher out,
   // start the checking; otherwise stop the checking.
   if (%Player.AutoRocketOn == true)
   {
      if (%Player.getMountedImage($WeaponSlot).getName() $= "MissileLauncherImage")
      {
         %Player.AutoRocketLockCheck = %Player.AutoRocketLockCheck();
         %Player.mountImage("AutoRocketExtra3Image", 6);
      } else {
         %Player.mountImage("AutoRocketExtra2Image", 6);
      }
   } else {
      if (%Player.AutoRocketLockCheck !$= "")
      {
         cancel(%Player.AutoRocketLockCheck);
         %Player.AutoRocketLockCheck = "";
      }
   }

   // If the player wants to see help, bottom print it for them.
   if (%Client.ShowHelp == true)
   {
      if (%Client.HelpSchedule !$= "")
         Cancel(%Client.HelpSchedule);

      %Name = %Weapon.getName();
      %Message = $HelpDesc[%Name];
      %Lines = $HelpLines[%Name];
      if (%Message $= "")
         return;

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

function WeaponImage::onUnmount(%this, %Player, %slot)
{
   %Player.unmountImage($OtherSlot1);
   %Player.client.setWeaponsHudActive(%this.item, 1);
   %Player.client.setAmmoHudCount(-1);

   %Player.setArmThread(look);
   Parent::onUnmount(%this, %Player, %slot);
}

function Ammo::onInventory(%this,%obj,%amount)
{
   // Loop through and make sure the images using this ammo have
   // their ammo states set.
   for (%i = 0; %i < 8; %i++) {
      %image = %obj.getMountedImage(%i);
      if (%image > 0)
      {
         if (isObject(%image.ammo) && %image.ammo.getId() == %this.getId())
            %obj.setImageAmmo(%i,%amount != 0);
      }
   }
   ItemData::onInventory(%this,%obj,%amount);
   if(%obj.getClassname() $= "Player")
   {
      %obj.client.setWeaponsHudAmmo(%this.getName(), %amount);
      if(%obj.getMountedImage($WeaponSlot).ammo $= %this.getName())
         %obj.client.setAmmoHudCount(%amount);
   }
}

function Weapon::onInventory(%this,%obj,%amount)
{
   if(Game.weaponOnInventory(%this, %obj, %amount))
   {
      %obj.client.setWeaponsHudItem(%this.getName(), 0, 1);   
      ItemData::onInventory(%this,%obj,%amount);
      // if a player threw a weapon (which means that player isn't currently
      // holding a weapon), set armthread to "no weapon"
		// MES - taken out to avoid v-menu animation problems (bug #4749)
      //if((%amount == 0) && (%obj.getClassName() $= "Player"))
      //   %obj.setArmThread(looknw);
   }
}

function Weapon::onPickup(%this, %Weapon, %Player, %amount)
{
   // If player doesn't have a weapon in hand, use this one...
   if (%Player.getClassName() $= "Player" && %Player.getMountedImage($WeaponSlot) == 0)
      %Player.use(%this.getName());

   if(%Weapon.sensors !$= "")
   {
      %AmmoType = %this.Image.Ammo;
      if (%AmmoType !$= "" && %Weapon.Sensors > 0)
      {
         %Player.incInventory(%AmmoType, %Weapon.Sensors);

         %Player.client.updateSensorPackText(%Player.inv[%AmmoType]);
      }
   }
}

function HandInventory::onInventory(%this,%obj,%amount)
{
   // prevent console errors when throwing ammo pack
   if(%obj.getClassName() $= "Player")
      %obj.client.setInventoryHudAmount(%this.getName(), %amount);
   ItemData::onInventory(%this,%obj,%amount);
}

function HandInventory::onUse(%data, %obj)
{
   // %obj = player  %data = datablock of what's being thrown
   if(Game.handInvOnUse(%data, %obj))
   {
      //AI HOOK - If you change the %throwStren, tell Tinman!!!
      //Or edit aiInventory.cs and search for: use(%grenadeType);

      %tossTimeout = getSimTime() - %obj.lastThrowTime[%data];
      if(%tossTimeout < $HandInvThrowTimeout)
         return;

      %throwStren = %obj.throwStrength;

      %Scale = %Data.thrownItem.scale;
      if (%Scale $= "")
         %Scale = "1 1 1";

      %obj.decInventory(%data, 1);
      %thrownItem = new Item()
      {
         dataBlock = %data.thrownItem;
         sourceObject = %obj;
         scale = %Scale;
      };
      MissionCleanup.add(%thrownItem);
      
      // throw it
      %eye = %obj.getEyeVector();
      %vec = vectorScale(%eye, (%throwStren * 20.0));
      
      // add a vertical component to give it a better arc
      %dot = vectorDot("0 0 1", %eye);
      if(%dot < 0)
         %dot = -%dot;
      %vec = vectorAdd(%vec, vectorScale("0 0 4", 1 - %dot));
      
      // add player's velocity
      %vec = vectorAdd(%vec, vectorScale(%obj.getVelocity(), 0.4));
      %pos = getBoxCenter(%obj.getWorldBox());
      

      %thrownItem.sourceObject = %obj;
      %thrownItem.setTransform(%pos);
      
      %thrownItem.applyImpulse(%pos, %vec);
      %thrownItem.setCollisionTimeout(%obj);
      serverPlay3D(GrenadeThrowSound, %pos);
      %obj.lastThrowTime[%data] = getSimTime();

      %thrownItem.getDataBlock().onThrow(%thrownItem, %obj);
      %obj.throwStrength = 0;
   }
}

function HandInventoryImage::onMount(%this,%obj,%slot)
{
   messageClient(%col.client, 'MsgHandInventoryMount', "", %this, %obj, %slot);
   // Looks arm position
   if (%this.armthread $= "")
      %obj.setArmThread(look);
   else
      %obj.setArmThread(%this.armThread);
   
   // Initial ammo state
   if (%obj.getInventory(%this.ammo))
      %obj.setImageAmmo(%slot,true);

   %obj.client.setWeaponsHudActive(%this.item);
}

function Weapon::incCatagory(%data, %obj)
{
   // Don't count the targeting laser as a weapon slot:
   if ( %data.getName() !$= "TargetingLaser" )
      %obj.weaponCount++;   
}

function Weapon::decCatagory(%data, %obj)
{
   // Don't count the targeting laser as a weapon slot:
   if ( %data.getName() !$= "TargetingLaser" )
      %obj.weaponCount--;   
}

function SimObject::damageObject(%data)
{
   //function was added to reduce console err msg spam
}

$InventoryHudData[0, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[0, itemDataName] = Grenade;
$InventoryHudData[0, ammoDataName] = Grenade;
$InventoryHudData[0, slot]         = 0;
$InventoryHudData[1, bitmapName]   = "gui/hud_mine";
$InventoryHudData[1, itemDataName] = Mine;
$InventoryHudData[1, ammoDataName] = Mine;
$InventoryHudData[1, slot]         = 1;
$InventoryHudData[2, bitmapName]   = "gui/hud_medpack";
$InventoryHudData[2, itemDataName] = RepairKit;
$InventoryHudData[2, ammoDataName] = RepairKit;
$InventoryHudData[2, slot]         = 3;
$InventoryHudData[3, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[3, itemDataName] = FlashGrenade;
$InventoryHudData[3, ammoDataName] = FlashGrenade;
$InventoryHudData[3, slot]         = 0;
$InventoryHudData[4, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[4, itemDataName] = ConcussionGrenade;
$InventoryHudData[4, ammoDataName] = ConcussionGrenade;
$InventoryHudData[4, slot]         = 0;
$InventoryHudData[5, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[5, itemDataName] = FlareGrenade;
$InventoryHudData[5, ammoDataName] = FlareGrenade;
$InventoryHudData[5, slot]         = 0;
$InventoryHudData[6, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[6, itemDataName] = CameraGrenade;
$InventoryHudData[6, ammoDataName] = CameraGrenade;
$InventoryHudData[6, slot]         = 0;
$InventoryHudData[7, bitmapName]   = "gui/hud_beacon";
$InventoryHudData[7, itemDataName] = Beacon;
$InventoryHudData[7, ammoDataName] = Beacon;
$InventoryHudData[7, slot]         = 2;
$InventoryHudData[8, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[8, itemDataName] = EMPGrenade;
$InventoryHudData[8, ammoDataName] = EMPGrenade;
$InventoryHudData[8, slot]         = 0;
$InventoryHudData[9, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[9, itemDataName] = PlasticExplosive;
$InventoryHudData[9, ammoDataName] = PlasticExplosive;
$InventoryHudData[9, slot]         = 0;
$InventoryHudData[10, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[10, itemDataName] = IncendiaryGrenade;
$InventoryHudData[10, ammoDataName] = IncendiaryGrenade;
$InventoryHudData[10, slot]         = 0;
$InventoryHudData[11, bitmapName]   = "gui/hud_handgren";
$InventoryHudData[11, itemDataName] = ECMGrenade;
$InventoryHudData[11, ammoDataName] = ECMGrenade;
$InventoryHudData[11, slot]         = 0;
$InventoryHudData[12, bitmapName]   = "gui/hud_mine";
$InventoryHudData[12, itemDataName] = EMPMine;
$InventoryHudData[12, ammoDataName] = EMPMine;
$InventoryHudData[12, slot]         = 1;
$InventoryHudData[13, bitmapName]   = "gui/hud_mine";
$InventoryHudData[13, itemDataName] = IncendiaryMine;
$InventoryHudData[13, ammoDataName] = IncendiaryMine;
$InventoryHudData[13, slot]         = 1;
$InventoryHudData[14, bitmapName]   = "gui/hud_mine";
$InventoryHudData[14, itemDataName] = ConcussionMine;
$InventoryHudData[14, ammoDataName] = ConcussionMine;
$InventoryHudData[14, slot]         = 1;
$InventoryHudData[15, bitmapName]   = "gui/hud_mine";
$InventoryHudData[15, itemDataName] = StunMine;
$InventoryHudData[15, ammoDataName] = StunMine;
$InventoryHudData[15, slot]         = 1;

$InventoryHudCount = 16;

datablock ItemData(AIAvoidObject)
{
   shapeFile = "turret_muzzlepoint.dts";

   mass = 0.0;
   elasticity = 0.0;
   friction = 1.0;

   maxDamage = 100;
   aiAvoidThis = true;
};

function MakeAvoidObject(%Position, %Time)
{
   %AvoidObject = new Item()
   {
      dataBlock = AIAvoidObject;
      position = %Position;
   };
   MissionCleanUp.Add(%AvoidObject);

   %AvoidObject.schedule(%Time, delete);
}

function AreaSmoke(%Position)
{
   %Item = new ParticleEmissionDummy()
   {
      position = %Position;
      scale = "1 1 1";
      dataBlock = "defaultEmissionDummy";
      emitter = "MissileLauncherExhaustEmitter";
   };
   MissionCleanup.add(%Item);

   %Item.schedule(1250, delete);
}

datablock GrenadeProjectileData(SmokeEffectProjectile)
{
   projectileShapeName = "turret_muzzlepoint.dts";
   directDamage        = 0.0;
   hasDamageRadius     = false;

   baseEmitter         = MissileLauncherExhaustEmitter;
   bubbleEmitter       = GrenadeBubbleEmitter;
   emitterDelay        = -1;

   grenadeElasticity = 0.35;
   grenadeFriction   = 0.2;
   armingDelayMS     = 6000;
};

function Player::PlayerSmokeEffect(%this)
{
   %Velocity = %this.getVelocity();
   %Transform = %this.getTransform();
   %Position = posFromTransform(%Transform);

   %Random = getRandom();
   %PositionX = getWord(%Position, 0) + (%Random - %Random / 2);
   %Random = getRandom();
   %PositionY = getWord(%Position, 1) + (%Random - %Random / 2);
   %PositionZ = getWord(%Position, 2) + 1;
   %Position = %PositionX @ " " @ %PositionY @ " " @ %PositionZ;

   %Item = new ParticleEmissionDummy()
   {
      position = %Position;
      scale = "1 1 1";
      dataBlock = "defaultEmissionDummy";
      emitter = "MissileLauncherExhaustEmitter";
      velocity = %Velocity;
   };

   %Item.schedule(1250, delete);
}