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

// Item Datablocks
//    image = Name of mounted image datablock
//    onUse(%this,%object)

// Item Image Datablocks
//    item = Name of item inventory datablock

// ShapeBase Datablocks
//    max[Item] = Maximum amount that can be caried

// ShapeBase Objects
//    inv[Item] = Count of item in inventory
//----------------------------------------------------------------------------

$TestCheats = 0;

function serverCmdUse(%client, %Data)
{
   %Player = %client.getControlObject();
   %OldWeapon = %Player.getMountedImage($WeaponSlot).getName();

   if (%Data $= "TargetingLaser" && %OldWeapon $= "TargetingLaserImage")
      %Player.use(FlashLight);
   else
      %Player.use(%Data);
}

function serverCmdThrow(%client,%data)
{
   // Item names from the client must converted
   // into DataBlocks
   // %data = ItemDataBlock[%item];
   %client.getControlObject().throw(%data);
}

function serverCmdThrowWeapon(%client,%data)
{
   // Item names from the client must converted
   // into DataBlocks
   // %data = ItemDataBlock[%item];
   %client.getControlObject().throwWeapon();
}

function serverCmdThrowPack(%client,%data)
{
   %client.getControlObject().throwPack();
}

function serverCmdTogglePack(%client,%data)
{
   // this function is apparently never called
   %client.getControlObject().togglePack();
}

function serverCmdThrowFlag(%client)
{
   //Game.playerDroppedFlag(%client.player);
   Game.dropFlag(%client.player);
}

function serverCmdSelectWeaponSlot( %client, %data )
{
   %client.getControlObject().selectWeaponSlot( %data );
}

function serverCmdCycleWeapon( %client, %data )
{
   %client.getControlObject().cycleWeapon( %data );
}

function serverCmdStartThrowCount(%client, %data)
{
   %client.player.throwStart = getSimTime();
}

function serverCmdEndThrowCount(%client, %data)
{
   if(%client.player.throwStart == 0)
      return;

   // throwStrength will be how many seconds the key was held
   %throwStrength = (getSimTime() - %client.player.throwStart) / 300;
   // trim the time to fit between 0.5 and 1.5
   if(%throwStrength > 1.5)
      %throwStrength = 1.5;
   else if(%throwStrength < 0.5)
      %throwStrength = 0.5;

   %throwScale = %throwStrength / 2;
   %client.player.throwStrength = %throwScale;

   %client.player.throwStart = 0;
}

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

function ShapeBase::throwWeapon(%this)
{
   if (Game.shapeThrowWeapon(%this))
   {
      %image = %this.getMountedImage($WeaponSlot);
      %this.throw(%image.item);
      %this.client.setWeaponsHudItem(%image.item, 0, 0);
   }
}

function ShapeBase::throwPack(%this)
{
   %image = %this.getMountedImage($BackpackSlot);
   %this.throw(%image.item);
   %this.client.setBackpackHudItem(%image.item, 0);
}

function ShapeBase::throw(%this, %Data)
{
   if (%this.inv[%Data.getName()] <= 0)
      return(false);

   // Throw item first...
   %Item = %this.throwItem(%Data);
   %MassChange = %Item.trueMass;

   %IncrementAmount = $AmmoIncrement[%Data.getName()];
   if (%IncrementAmount !$= "")
   {
      %this.decInventory(%Data, %IncrementAmount);
      %MassChange *= %IncrementAmount;
   }
   else
      %this.decInventory(%Data, 1);

   %this.ThrownItem = %Item;

   // If a player, we try to lower thier weight by what they have thrown
   if (%MassChange > 0 && %this.getDataBlock().className $= "Armor")
   {
      %this.ExtraMass -= %MassChange;
      %Weight = "Mass" @ mFloor(%this.ExtraMass / 4) @ "Image";
      %this.mountImage(%Weight, 7);
   }

   return true;
}

function ShapeBase::use(%this, %data)
{
   if(%data $= Grenade)
   {
      // figure out which grenade type you're using
      for(%x = 0; $InvGrenade[%x] !$= ""; %x++) {
         if(%this.inv[$NameToInv[$InvGrenade[%x]]] > 0)
         {
            %data = $NameToInv[$InvGrenade[%x]];   
            break;
         }
      }

   } else if(%data $= Mine) {

      // figure out which mine type you're using
      %x = 0;
      while ($InvMine[%x] !$= "")
      {
         %MineType = $InvMine[%x];
         if (%this.inv[$NameToInv[%MineType]] > 0)
         {
            %data = $NameToInv[%MineType];
            break;
         }

         %x++;
      }

      // default case
      if (%this.inv[%data.getName()] > 0)
      {
         %Target = %this.searchView(4, $EverythingMask);
         if (%Target == 0)
            %data.onUse(%this);
         else
            %this.deployMine(%Target, %Data.DeployedItem);

         return true;
      }

   } else if(%data $= "Backpack") {

      %pack = %this.getMountedImage($BackpackSlot);
      if (%Pack $= "" || %Pack $= "0")
      {
         // If we threw a satchel, blow it now
         if((%this.thrownChargeId > 0) && %this.thrownChargeId.armed == true)
         {
            %this.playAudio(0, SatchelChargeExplosionSound);
            %this.schedule(800, "detonateSatchelCharge");
            return true;
         }

         // If we are engineer, try to repack
         if (%this.getArmorSize() $= "Engineer")
         {
            // Locate the Deployables Group
            %DeployablesGroup = nameToID("MissionCleanup/Deployables");
            if (%DeployablesGroup $= "")
               %DeployablesGroup = 0;

            %Failed = false;

            // Check for an object in the players Field of View
            %Mask = $TypeMasks::StaticShapeObjectType | $TypeMasks::TurretObjectType |
                    $TypeMasks::ItemObjectType;

            %TargetObject = %this.searchView(6, %Mask);
            %Object = getWord(%TargetObject, 0);

            if (%TargetObject $= "0") 
               return;

            if (%Failed == false && %TargetObject.team != %this.team)
               return;

            // Is the Object in the Deployables Group or is it a Deployable
            %Group = %TargetObject.getGroup();
            if (%Failed == false && %TargetObject.getDataBlock().deployedObject != true && %Group != %DeployablesGroup)
               return;

            // If we aren't looking at anything valid, then we go ahead and try a remote
            // repack.
            if (%Failed == true)
            {
               if (%this.EngObject $= "" || isObject(%this.EngObject) == false)
               {
                  messageClient(%this.client, "", "You are not uplinked with any friendly deployables.");
                  return;
               }

               %this.EngObject.RepackObject(%this.client);
            } else {
               %Object.RepackObject(%this.client);
            }            
         }
      }
      return false;
   }
   else if(%data $= Beacon) 
   {
      %data.onUse(%this);
      if (%this.inv[%data.getName()] > 0 || %this.Special != "TargetingBeacon")
         return true;
   }

   // default case
   if (%this.inv[%data.getName()] > 0)
   {
      %data.onUse(%this);
      return true;
   }
   return false;
}

function ShapeBase::pickup(%this, %Object, %Amount)
{
   %Data = %Object.getDataBlock();
   if (%Data.AlternateObject !$= "")
      %Data = %Data.AlternateObject;

   %Delta = %this.incInventory(%Data, %Amount);
   if (%Delta == true)
      %Data.onPickup(%Object, %this, %Delta);

   return(%Delta);
}

function ShapeBase::hasInventory(%this, %data)
{
   // changed because it was preventing weapons cycling correctly (MES)
   return (%this.inv[%data] > 0);
}

function ShapeBase::maxInventory(%this,%data)
{
   if ($TestCheats)
      return 999;
   else
      return %this.getDatablock().max[%data.getName()];
}

function ShapeBase::incInventory(%this, %data, %amount)
{
   // Special Case for Medium Armors trying to handle the mortar
   if (%data.getName() $= "Mortar" && %this.client.armor $= "Medium" && %this.Special != "WCS")
      return(0);

   %max = %this.maxInventory(%data);
   %cv = %this.inv[%data.getName()];

   if (%cv < %max)
   {
      if (%cv + %amount > %max)
         %amount = %max - %cv;
      %this.setInventory(%data,%cv + %amount);
      %data.incCatagory(%this); // Inc the players weapon count
      return %amount;
   }
   return(0);
}

function ShapeBase::decInventory(%this,%data,%amount)
{
   %name = %data.getName();
   %cv = %this.inv[%name];
   if (%cv > 0) {
      if (%cv < %amount)
         %amount = %cv;
      %this.setInventory(%data,%cv - %amount, true);
      %data.decCatagory(%this); // Dec the players weapon count
      return %amount;
   }
   return 0;
}

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

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

function ShapeBase::setInventory(%this,%data,%value,%force)
{
   if (!isObject(%data))
      return;

   %name = %data.getName();
   if (%value < 0)
      %value = 0;
   else 
   {
      if (!%force) 
      {
         // Impose inventory limits
         %max = %this.maxInventory(%data);
         if (%value > %max)
            %value = %max;
      }
   }
   if (%this.inv[%name] != %value) 
   {
      %this.inv[%name] = %value;
      %data.onInventory(%this,%value);

      if ( %data.className $= "Weapon" )
      {
         if ( %this.weaponSlotCount $= "" )
            %this.weaponSlotCount = 0;

         %cur = -1;
         for ( %slot = 0; %slot < %this.weaponSlotCount; %slot++ )
         {
            if ( %this.weaponSlot[%slot] $= %name )
            {
               %cur = %slot;
               break;
            }
         }

         if ( %cur == -1 )
         {
            // Put this weapon in the next weapon slot:
            if (%this.weaponSlot[%this.weaponSlotCount - 1] $= "TargetingLaser")
            {
               %this.weaponSlot[%this.weaponSlotCount - 1] = %name;
               %this.weaponSlot[%this.weaponSlotCount] = "TargetingLaser";
            }
               else if (%this.weaponSlot[%this.weaponSlotCount - 1] $= "FlashLight")
            {
               %this.weaponSlot[%this.weaponSlotCount - 1] = %name;
               %this.weaponSlot[%this.weaponSlotCount] = "FlashLight";
            } else {
               %this.weaponSlot[%this.weaponSlotCount] = %name;

            }
            %this.weaponSlotCount++;
         }
         else
         {
            // Remove the weapon from the weapon slot:
            for ( %i = %cur; %i < %this.weaponSlotCount - 1; %i++ )
               %this.weaponSlot[%i] = %this.weaponSlot[%i + 1];
            %this.weaponSlot[%i] = "";
            %this.weaponSlotCount--;
         }
      }

      %this.getDataBlock().onInventory(%data,%value);
   }
   return %value;
}

function ShapeBase::getInventory(%this,%data)
{
   if ( isObject( %data ) )
      return( %this.inv[%data.getName()] );
   else
      return( 0 );
}

function ShapeBase::hasAmmo(%Player, %Weapon)
{
   if (%Weapon $= "TargetingLaser" || %Weapon $= "FlashLight")
      return(false);

   %WeaponImage = %Weapon.Image;
   if (%WeaponImage.usesEnergy == true)
      return(true);

   %Ammo = %Player.getInventory(%WeaponImage.ammo);
   return(%Ammo > 0);
}

function SimObject::onInventory(%this, %obj)
{
   //function was added to reduce console error msg spam
}

function ShapeBase::throwItem(%this,%data)
{
   %Scale = %Data.Scale;
   if (%Scale $= "")
      %Scale = "1 1 1";

   %item = new Item()
   {
      dataBlock = %data;
      rotation = "0 0 1 " @ (getRandom() * 360);
      scale = %Scale;
   };
   MissionCleanup.add(%item);

   %AmmoType = %Data.Image.Ammo;
   if (%AmmoType !$= "")
   {
      %AmmoLimit = LightMaleHumanArmor.max[%AmmoType];
      if (%AmmoLimit $= "")
         %AmmoLimit = EngineerMaleHumanArmor.max[%AmmoType];

      if (%AmmoLimit $= "")
         %AmmoLimit = MediumMaleHumanArmor.max[%AmmoType];

      if (%AmmoLimit $= "")
         %AmmoLimit = HeavyMaleHumanArmor.max[%AmmoType];

      if (%this.inv[%AmmoType] >= %AmmoLimit)
      {
         %Item.Sensors = %AmmoLimit;
         %this.inv[%AmmoType] -= %AmmoLimit;
      } else {
         %item.sensors = %this.inv[%AmmoType];
         %this.inv[%AmmoType] = "";
      }

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

   %this.throwObject(%item);

   return(%item);
}

function ShapeBase::NameObject(%this, %Client, %Name)
{
   if (%this.Owner != %Client && isAdmin(%Client) != true)
   {
      messageClient(%Client, '', 'You do not own this deployable.');
      return;
   }

   %this.setName(%Name);

   if (%Name $= "")
      %Message = "Reverting to original designation.";
   else
      %Message = "New designation is now '" @ %Name @ "'.";

   messageClient(%Client, '', %Message);
}

function ShapeBase::RepackObject(%this, %Client)
{
   if (%this.NoRepack == true)
      return;

   if (%this.getDataBlock().CantRepack == true)
   {
      messageClient(%Client, '', 'This can not be repacked.');
      return;
   }

   if (%this.Owner != %Client && isAdmin(%Client) != true)
   {
      messageClient(%Client, '', 'You do not own this deployable.');
      return;
   }

   if (%this.getDamageLevel() > 0.05)
   {
      messageClient(%Client, '', 'Damage prevents repacking.');
      return;
   }

   %this.getDataBlock().schedule(1500, ItemSwap, %this);

   %Name = %this.getName();
   if (%Name $= "")
      %Name = %this.getDataBlock().name;

   %Message = %Name @ " has been ordered to repack.";

   messageClient(%Client, '', %Message);    
}

// This is the function to throw objects, %Force was added and if it
// is not passed the old default of 20 is used.
function ShapeBase::throwObject(%this, %Object, %Force)
{
   if (%Force == 0 || %Force $= '')
      %Force = 20;
      
   //if the object is being thrown by a corpse, use a random vector
   if (%this.getState() $= "Dead")
   {
      %Vector = (-1.0 + getRandom() * 2.0) @ " " @ (-1.0 + getRandom() * 2.0) @ " " @ getRandom();
      %Vector = vectorScale(%Vector, 10);
   } else {
      %EyeVector = %this.getMuzzleVector($WeaponSlot);
      %Vector = vectorScale(%EyeVector, %Force);
   }

   // Add a vertical component to give the item a better arc
   %DotProduct = vectorDot("0 0 1", %EyeVector);
   if (%DotProduct < 0)
      %DotProduct *= -1;

   // Merge the vertical component based on facing off the horizon
   %Vector = vectorAdd(%Vector, vectorScale("0 0 8", 1 - %DotProduct));

   // Add in the player's velocity
   %Vector = vectorAdd(%Vector, %this.getVelocity());
   %Position = getBoxCenter(%this.getWorldBox());

   //since flags have a huge mass (so when you shoot them, they don't bounce too far)
   //we need to up the %vec so that you can still throw them...
   if (%Object.getDataBlock().getName() $= "Flag")
      %Vector = vectorScale(%Vector, 40);

   // Move the thrown object to player and then apply the throwing force to it
   %Object.setTransform(%Position);
   %Object.applyImpulse(%Position, %Vector);
   %Object.setCollisionTimeout(%this);

   // Call any special code now
   %data = %Object.getDatablock();
   %data.onThrow(%Object, %this);

   // Let the AI know it is there
   AIThrowObject(%Object);
}

function ShapeBase::ChangeTurretMode(%this, %Client, %NewMode)
{
   if (%this.Owner != %Client && isAdmin(%Client) != true)
   {
      messageClient(%Client, '', 'You do not own this deployable.');
      return;
   }

   if(%this.getDataBlock().SmallTurretBase != true)
   {
      messageClient(%Client, 'MsgNotTurretBase', "\c2Can only change the firing mode of Land Spike and Spider Clamp turrets.");
      return;
   }

   // Turret must be functional to trade out the barrel
   if(%this.getDamageState() !$= "Enabled")
   {
      messageClient(%Client, 'MsgBaseDestroyed', "\c2Small Turret base is too heavily damaged to change firing mode.");
      return;
   }

   switch$ (strUpr(%NewMode))
   {
      case "EMP" :
         %NewBarrel = "EMPTurretBarrel";
         %NewBarrelName = "EMP";

      case "FLAME" :
         %NewBarrel = "FlameTurretBarrel";
         %NewBarrelName = "Flame";

      case "FUSION" :
         %NewBarrel = "FusionTurretBarrel";
         %NewBarrelName = "Fusion";

      case "IMPULSE" :
         %NewBarrel = "ImpulseTurretBarrel";
         %NewBarrelName = "Impulse";

      case "ION" :
         %NewBarrel = "IonTurretBarrel";
         %NewBarrelName = "Ion";

      case "LASER" :
         %NewBarrel = "LaserTurretBarrel";
         %NewBarrelName = "Laser";

      case "TRACTOR" :
         %NewBarrel = "TractorTurretBarrel";
         %NewBarrelName = "Tractor";

      default :
         messageClient(%Client, 'MsgBaseDestroyed', "\c2Invalid Small Turret Mode:");
         messageClient(%Client, 'MsgBaseDestroyed', "\c2Valid Modes are EMP, Flame, Fusion, Impulse, Ion, Laser, and Tractor.");
         return;
   }

   %OldBarrel = %this.getMountedImage(0).getName();

   if (%OldBarrel $= %NewBarrel)
   {
      %Message = "This small turret is already in " @ %NewBarrelName @ " mode.";
      messageClient(%Client, '', %Message);
      return;
   } else {
      %Message = "This small turret has been changed to " @ %NewBarrelName @ " mode.";
   }

   messageClient(%Client, '', %Message);    

   %this.mountImage(%NewBarrel, 0, false);
   
   serverPlay3D(TurretPackActivateSound, %this.getTransform());

   %this.NoRepack = true;
   schedule(2000, 0, ClearRepack, %this);
}

function ShapeBase::ChangeFFOrientation(%Generator, %NewOrientation)
{
   echo("Generator: " @ %Generator);
   echo("Changing to orientation: " @ %NewOrientation);
   echo("Current Orientation: " @ %Generator.CurrentFFTransform);

   // If the requested orientation is the same as the current one, ignore the player
   if (%Generator.CurrentFFTransform == %NewOrientation)
      return;

   %Generator.CurrentUpDown = 0;
   %Generator.CurrentLeftRight = 0;
   %Generator.CurrentFFTransform = %NewOrientation;

   %NewTransform = %Generator.BaseFFTransform[%NewOrientation];
   %NewScale = %Generator.BaseFFScale[%NewOrientation];

   // Scale and Move the ForceField
   %ForceField = %Generator.ForceField;   
   %ForceField.setScale(%NewScale);
   %ForceField.setTransform(%NewTransform);

   // Move the Physical Zone with the Field
   %PZone = %ForceField.PZone;
   %PZone.setScale(%NewScale);
   %PZone.setTransform(%NewTransform);

//   %Generator.getDataBlock().ForceFieldAdjust(%Generator);
}

function ShapeBase::ChangeFFUpDown(%this, %Change, %Percise)
{
   if (%Percise !$= "")
      %NewUpDown = %Percise;
   else
      %NewUpDown = %Generator.CurrentUpDown + %Change;

   if (%NewUpDown < -8)
      %NewUpDown = -8;

   if (%NewUpDown > 8)
      %NewUpDown = 8;

   %Generator.CurrentUpDown = %NewUpDown;

   %BaseTransform = %Generator.BaseFFTransform[%Generator.CurrentFFTransform];
   %BaseRotation = rotFromTransform(%BaseTransform);
   %BasePosition = posFromTransform(%BaseTransform);

   %BasePositionXY = getWords(%BasePosition, 0, 1);
   %BasePositionZ = getWord(%BasePosition, 2);

   %NewTransform = %BasePositionXY @ " " @ %BasePositionZ + (0.5 * %NewUpDown) @ " " @ %BaseRotation;

   %ForceField = %Generator.ForceField;   
   %ForceField.setTransform(%NewTransform);

   %Generator.getDataBlock().ForceFieldAdjust(%ForceField);
}

function ShapeBase::ChangeFFLeftRight(%this, %Change, %Percise)
{
   if (%Percise !$= "")
      %NewLeftRight = %Percise;
   else
      %NewLeftRight = %Generator.CurrentLeftRight + %Change;

   if (%NewLeftRight < -8)
      %NewLeftRight = -8;

   if (%NewLeftRight > 8)
      %NewLeftRight = 8;

   %Generator.CurrentLeftRight = %NewLeftRight;

   %BaseTransform = %Generator.BaseFFTransform[%Generator.CurrentFFTransform];
   %BaseRotation = rotFromTransform(%BaseTransform);
   %BasePosition = posFromTransform(%BaseTransform);

   %ForceField = %Generator.ForceField;   

   %BasePosition2 = VectorAdd(%BasePosition, VectorScale(%ForceField.rotatedVector, %NewLeftRight / 2));
   %NewTransform = %BasePosition2 @ " " @ %BaseRotation;

   %ForceField.setTransform(%NewTransform);

   %Generator.getDataBlock().ForceFieldAdjust(%ForceField);
}

// Take all the Equipment from a player.
function ShapeBase::clearInventory(%Player)
{
   %Player.setInventory(RepairKit,0);

   // Mines
   %Player.setInventory(Mine,0);
   %Player.setInventory(EMPMine,0);
   %Player.setInventory(IncendiaryMine,0);
   %Player.setInventory(StunMine,0);
   %Player.setInventory(ConcussionMine,0);

   // Grenades
   %Player.setInventory(Grenade,0);
   %Player.setInventory(FlashGrenade,0);
   %Player.setInventory(ConcussionGrenade,0);
   %Player.setInventory(FlareGrenade,0);
   %Player.setInventory(CameraGrenade, 0);
   %Player.setInventory(IncendiaryGrenade,0);
   %Player.setInventory(EMPGrenade,0);
   %Player.setInventory(ECMGrenade,0);
   %Player.setInventory(PlasticExplosive,0);

   // Weapons
   %Player.setInventory(Blaster,0);
   %Player.setInventory(Plasma,0);
   %Player.setInventory(Disc,0);
   %Player.setInventory(Chaingun, 0);
   %Player.setInventory(Mortar, 0);
   %Player.setInventory(Flamer, 0);
   %Player.setInventory(GrenadeLauncher, 0);
   %Player.setInventory(MissileLauncher, 0);
   %Player.setInventory(SniperRifle, 0);
   %Player.setInventory(TargetingLaser, 0);
   %Player.setInventory(ELFGun, 0);
   %Player.setInventory(ShockLance, 0);
   %Player.setInventory(Rifle, 0);
   %Player.setInventory(Shotgun, 0);
   %Player.setInventory(ParticleCannon, 0);
   %Player.setInventory(Magnum, 0);

   // Ammo
   %Player.setInventory(PlasmaAmmo,0);  
   %Player.setInventory(ChaingunAmmo, 0);
   %Player.setInventory(DiscAmmo, 0);
   %Player.setInventory(GrenadeLauncherAmmo, 0);
   %Player.setInventory(MissileLauncherAmmo, 0);
   %Player.setInventory(MortarAmmo, 0);
   %Player.setInventory(Beacon, 0);
   %Player.setInventory(TargetingBeacon, 0);
   %Player.setInventory(RifleAmmo, 0);
   %Player.setInventory(ShotgunAmmo, 0);

   // Whipe the Special
   %Player.Special = "";

   // take away any pack the player has
   %Pack = %Player.getMountedImage($BackpackSlot);
   if(%Pack > 0)
      %Player.setInventory(%Pack.item, 0);

}

//----------------------------------------------------------------------------
function ShapeBase::cycleWeapon( %this, %data )
{
   if ( %this.weaponSlotCount == 0 )
      return;

   %slot = -1;
   if ( %this.getMountedImage($WeaponSlot) != 0 )
   {
      %curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
      for ( %i = 0; %i < %this.weaponSlotCount; %i++ )
      {
         //error("curWeaponName == " @ %curWeaponName);
         if ( %curWeapon $= %this.weaponSlot[%i] )
         {
            %slot = %i;
            break;
         }
      }
   }

   if ( %data $= "prev" )
   {
      // Previous weapon...
      if ( %slot == 0 || %slot == -1 )
      {
         %i = %this.weaponSlotCount - 1;
         %slot = 0;
      }
      else
         %i = %slot - 1;
   }
   else
   {
      // Next weapon...
      if ( %slot == ( %this.weaponSlotCount - 1 ) || %slot == -1 )
      {
         %i = 0;
         %slot = ( %this.weaponSlotCount - 1 );
      }
      else
         %i = %slot + 1;
   }

   %newSlot = -1;
   while ( %i != %slot )
   {
      if ( %this.weaponSlot[%i] !$= ""
        && %this.hasInventory( %this.weaponSlot[%i] ) 
        && %this.hasAmmo( %this.weaponSlot[%i] ) )
      {
         // player has this weapon and it has ammo or doesn't need ammo
         %newSlot = %i;
         break;
      }

      if ( %data $= "prev" )
      {
         if ( %i == 0 )
            %i = %this.weaponSlotCount - 1;
         else
            %i--;
      }
      else
      {
         if ( %i == ( %this.weaponSlotCount - 1 ) )
            %i = 0;
         else
            %i++;
      }
   }

   if ( %newSlot != -1 )
      %this.use( %this.weaponSlot[%newSlot] );
}

//----------------------------------------------------------------------------
function ShapeBase::selectWeaponSlot( %this, %data )
{
   if ( %data < 0 || %data > %this.weaponSlotCount 
     || %this.weaponSlot[%data] $= "" || %this.weaponSlot[%data] $= "TargetingLaser" )
      return;

   %this.use( %this.weaponSlot[%data] );
}

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

function GiveAll(%client)
{
      %player = %client.player;
      %player.setInventory(RepairKit,9999);
      %player.setInventory(Mine,9999);
      %player.setInventory(Grenade,9999);
      %player.setInventory(FlashGrenade,9999);
      %player.setInventory(FlareGrenade,9999);
      %player.setInventory(ConcussionGrenade,9999);
      %player.setInventory(CameraGrenade, 9999);
      %player.setInventory(Blaster,1);
      %player.setInventory(Plasma,1);
      %player.setInventory(Chaingun, 1);
      %player.setInventory(Disc,1);
      %player.setInventory(GrenadeLauncher, 1);
      %player.setInventory(SniperRifle, 1);
      %player.setInventory(Rifle, 1);
      %player.setInventory(ELFGun, 1);
      %player.setInventory(Mortar, 1);
      %player.setInventory(MissileLauncher, 1);
      %player.setInventory(ShockLance, 1);
      %player.setInventory(ParticleCannon, 1);
      %player.setInventory(TargetingLaser, 1);
      %player.setInventory(MissileLauncherAmmo, 9999);
      %player.setInventory(GrenadeLauncherAmmo, 9999);
      %player.setInventory(MortarAmmo, 9999);
      %player.setInventory(PlasmaAmmo,9999);  
      %player.setInventory(ChaingunAmmo, 9999);
      %player.setInventory(DiscAmmo, 9999);
      %player.setInventory(Beacon, 9999);
}

function ClearRepack(%Item)
{
   %Item.NoRepack = "";
}

