Advanced Clip System

Hey there, Do you want to have weapon clips in your mod? Short on Datablocks for Clip ItemObjects. Do not fret anymore. Here is my advanced clip system that uses 0 Datablocks, that's right, NONE.

1. Create a New Script, Name It Whatever You Please
function AttemptReload(%WImg, %Plyr, %slot) {
   if(!isObject(%Plyr) || %Plyr.getState() $= "dead") {
      return;
   }
   %client = %Plyr.client;
   if(%Plyr.Reloading[%WImg]) {     //no double reloads :P
      return;
   }
   if(%WImg.ClipName $= "") {
      return;
   }
   %ClipsLeft = %plyr.ClipCount[%WImg.ClipName];
   if(%ClipsLeft == 0) {
      BottomPrint(%client, "Out Of Ammo", 2, 1);
   }
   else {
      %Plyr.setInventory(%WImg.ammo, 0, true);
      if(%ClipsLeft >= 900) {
//         echo(""@%plyr.client@" reloading unlimited clip gun");
         //Unlimited Clip Guns
         //Pistols, Ect.
      }
      else {
         %plyr.ClipCount[%WImg.ClipName]--;
      }
      %Plyr.Reloading[%WImg] = 1;
      %ReloadTime = %WImg.ClipReloadTime;
      BottomPrint(%client, "Reloading", %ReloadTime, 1);
      schedule(%ReloadTime * 1000, 0, "ReloadWeapon", %WImg, %Plyr);
   }
}

function ReloadWeapon(%WImg, %Plyr) {
   if(!isObject(%Plyr) || %Plyr.getState() $= "dead") {
      return;
   }
   %client = %Plyr.client;
   BottomPrint(%client, "Weapon Reloaded", 2, 1);
   %ammoGiven = %WImg.ClipReturn;
   %Plyr.setInventory(%WImg.ammo, %ammoGiven, true);
   %Plyr.Reloading[%WImg] = 0;
}
This script contains the functions to check for weapon reload, and actually do so. Now onto getting your clips to show in the weapons hud

2. Hud.cs Modifications

Replace the entire function: function GameConnection::setWeaponsHudItem, with this:
function GameConnection::setWeaponsHudItem(%client, %name, %ammoAmount, %addItem)
{
   for(%i = 0; %i < $WeaponsHudCount; %i++)
      if($WeaponsHudData[%i, itemDataName] $= %name)
      {
         if($WeaponsHudData[%i, ammoDataName] !$= "") {
            %ammoInv = %client.player.inv[$WeaponsHudData[%i, ammoDataName]];
            %plyr = %client.player;
            if(%name.Image.ShowsClipInHud) {
               %ClipInv = %plyr.ClipCount[%name.Image.ClipName];
               commandToClient(%client, 'setWeaponsHudItem', %i, %ClipInv, %addItem);
            }
            else {
               commandToClient(%client, 'setWeaponsHudItem',%i,%ammoInv, %addItem);
            }
         }
         else {
            commandToClient(%client, 'setWeaponsHudItem',%i,-1, %addItem);
         }
         break;
      }
}

And Replace function GameConnection::setWeaponsHudAmmo, with this:
function GameConnection::setWeaponsHudAmmo(%client, %name, %ammoAmount)
{
   for(%i = 0; %i < $WeaponsHudCount; %i++)
      if($WeaponsHudData[%i, ammoDataName] $= %name)
      {
         %WName = strReplace(%name, "Ammo", "");
         %plyr = %client.player;
         if(%WName.Image.ShowsClipInHud) {
            %clipAmount = %plyr.ClipCount[%WName.Image.ClipName];
            commandToClient(%client, 'setWeaponsHudAmmo',%i, %clipAmount);
            break;
         }
         else {
            commandToClient(%client, 'setWeaponsHudAmmo',%i, %ammoAmount);
            break;
         }
      }
}
That's all it takes for hud modifications, now lets get to making the magic happen

3. Projectiles.cs

Towards the bottom of ShapeBaseImageData::onFire, you will see this:
   if(%data.usesEnergy)
   {
      if(%data.useMountEnergy)
      {   
         if( %data.useCapacitor )
         {   
            %vehicle.turretObject.setCapacitorLevel( %vehicle.turretObject.getCapacitorLevel() - %data.fireEnergy );
         }
         else
            %useEnergyObj.setEnergyLevel(%energy - %data.fireEnergy);
      }
      else
         %obj.setEnergyLevel(%energy - %data.fireEnergy);
   }
   else
      %obj.decInventory(%data.ammo,1);
   return %p;

Replace that with this:
   if(%data.usesEnergy)
   {
      if(%data.useMountEnergy)
      {
         if( %data.useCapacitor )
         {
            %vehicle.turretObject.setCapacitorLevel( %vehicle.turretObject.getCapacitorLevel() - %data.fireEnergy );
         }
         else
            %useEnergyObj.setEnergyLevel(%energy - %data.fireEnergy);
      }
      else
         %obj.setEnergyLevel(%energy - %data.fireEnergy);
   }
   else {
      %obj.decInventory(%data.ammo,1);
      if(%obj.inv[%data.ammo] == 0) { //Added Phantom139, TWM2
         AttemptReload(%data, %obj, %slot);
      }
   }
   return %p;

what this does, is when your ammo weapon hits 0 ammo, it calls the reload check function you created in the first script, Now, Lets actually get the clips to be given to the player

4. InventoryHud.cs

Most of your changes will be done in this script, so be careful what you do here.

Function BuyFavorites

inside this code block
   // weapons
   for(%i = 0; %i < getFieldCount( %client.weaponIndex ); %i++)
   {
      %inv = $NameToInv[%client.favorites[getField( %client.weaponIndex, %i )]];

      if( %inv !$= "" )
      {
         %weaponCount++;
         %client.player.setInventory( %inv, 1 );
      }
      // z0dd - ZOD, 9/13/02. Streamlining.
      if ( %inv.image.ammo !$= "" )
         %client.player.setInventory( %inv.image.ammo, 400 );
   }
   %client.player.weaponCount = %weaponCount;

change it to this:
   // weapons
   for(%i = 0; %i < getFieldCount( %client.weaponIndex ); %i++)
   {
      %inv = $NameToInv[%client.favorites[getField( %client.weaponIndex, %i )]];

      if( %inv !$= "" )
      {
         %weaponCount++;
         %client.player.setInventory( %inv, 1 );
         %WImg = %inv.Image;
         if(%WImg.ClipName !$= "") { //apply clips
            %client.player.ClipCount[%WImg.ClipName] = %WImg.InitialClips;
         }
      }
      // z0dd - ZOD, 9/13/02. Streamlining.
      if ( %inv.image.ammo !$= "" )
         %client.player.setInventory( %inv.image.ammo, 400 );
   }
   %client.player.weaponCount = %weaponCount;

function buyDeployableFavorites

Same deal here, Replace:
   // players cannot buy armor from deployable inventory stations
   %weapCount = 0;
   for ( %i = 0; %i < getFieldCount( %client.weaponIndex ); %i++ )
   {
      %inv = $NameToInv[%client.favorites[getField( %client.weaponIndex, %i )]];
      if ( !($InvBanList[DeployInv, %inv]) )
      {
         %player.setInventory( %inv, 1 );
         // increment weapon count if current armor can hold this weapon
         if(%player.getDatablock().max[%inv] > 0)
            %weapCount++;
          
         // z0dd - ZOD, 9/13/02. Streamlining
         if ( %inv.image.ammo !$= "" )
            %player.setInventory( %inv.image.ammo, 400 );

         if(%weapCount >= %player.getDatablock().maxWeapons)
            break;
      }
   }
   %player.weaponCount = %weapCount;

with this:
   // players cannot buy armor from deployable inventory stations
   %weapCount = 0;
   for ( %i = 0; %i < getFieldCount( %client.weaponIndex ); %i++ )
   {
      %inv = $NameToInv[%client.favorites[getField( %client.weaponIndex, %i )]];
      if ( !($InvBanList[DeployInv, %inv]) )
      {
         %player.setInventory( %inv, 1 );
         // increment weapon count if current armor can hold this weapon
         if(%player.getDatablock().max[%inv] > 0)
            %weapCount++;
            
         %WImg = %inv.Image;
         if(%WImg.ClipName !$= "") { //apply clips
            %player.ClipCount[%WImg.ClipName] = %WImg.InitialClips;
         }

         // z0dd - ZOD, 9/13/02. Streamlining
         if ( %inv.image.ammo !$= "" )
            %player.setInventory( %inv.image.ammo, 400 );

         if(%weapCount >= %player.getDatablock().maxWeapons)
            break;
      }
   }
   %player.weaponCount = %weapCount;

This Step is Optional, if you want the beacon key to act as a reload key.
5. Optional, Item.cs

function Beacon::onUse

Add this right after that:
   if(%obj.getMountedImage($WeaponSlot) !$= "") {
      %WImg = %obj.getMountedImage($WeaponSlot);
      AttemptReload(%WImg, %obj, $WeaponSlot);
   }

And that does it!

Now, lets show you how to use it:

In the weapon file that you want to have a clip, Go to the weapon's IMAGE, not the Item

Add these to it somewhere, here is an example from my Colt Pistol:
   //ClipStuff
   ClipName = "ColtClip";
   ShowsClipInHud = 0;
   ClipReloadTime = 2;
   ClipReturn = 10;
   InitialClips = 950;
   //

ClipName - This can be whatever you want, I'd just make it relate to the weapon
ShowsClipInHud - If set to 1, your clip count will display in the weapons hud, if 0, the ammo
ClipReloadTime - How long it takes to 'Reload' your gun
ClipReturn - How much ammo is returned after a successful reload
InitialClips - How many clips you start with, NOTE: if this number is over 900, it will be unlimited

Enjoy, just credit me somewhere in your mod if you want to use this.

Comments

  • edited July 2009
    Sorry for Double Post:

    I will be adding functionality to this later so you can pick up the clips of a dead player object, without any DBs. I will use this post as the tutorial for that.
  • I have to be a dick here, sorry. Clips are used to load ammunition into magazines, magazines are used to load weapons. You got any clips for my deagle?
  • Most mods now a days call them clips when you 'Reload' your weapon, which is why it is named so.
Sign In or Register to comment.