Welcome, Guest! Login | Register

How to add Ammocrates for missing ammo type and/or create new ammo types [Print this Article]
Posted by: Starbreaker
Date posted: Mar 31 2005
User Rating: 5 out of 5.0
Number of views: 8373
Number of comments: 2
Description: This tutorial will cover the code needed to add Slams and SGM1 Grenades as Ammocrate types, and also fix the graphics for two of the existing ones.
This tutorial will cover how to add 2 more ammo crates to your mod, and fix the model/skin for 2 others. It can also be used to create new ammo types.

After doing a bit of mapping, I could not figure out why after adding an item_ammo_crate I was getting an error model instead of the ammo crate model. Well it appears that Valve added a few to the code and the FGD, but failed to include the models for them. I also wanted to add an ammo crate for Slams and the Grenades for the SMG, so I looked into what this would be, and that is what follows.

I know people do not like/care for cut -> paste tutorials, but really there is nothing here to learn except how the crates themselves were designed, which could be very important if you want to create new ammo types for your mod. The real hard work was making the models and skins.

Ok, here is goes:

The ammo crate code starts at about line 592 in the item_ammo.cpp file.

For this to work correctly we have to add in our 2 new ammo types (the Slam and the Alt fire for the SMG1) around line 607. These have to go after everything else because of the way the crates are setup.

 CODE (C++) 

// Ammo types
enum
{
    AMMOCRATE_SMALL_ROUNDS,     //Pistol
    AMMOCRATE_MEDIUM_ROUNDS,    //SMG1
    AMMOCRATE_LARGE_ROUNDS,     //AR2 (IRifle)
    AMMOCRATE_RPG_ROUNDS,       //RPG
    AMMOCRATE_BUCKSHOT,         //Shotgun
    AMMOCRATE_GRENADES,         //Grenades
    AMMOCRATE_357,              //357
    AMMOCRATE_CROSSBOW,         //Bolts
    AMMOCRATE_AR2_ALTFIRE,      //Combine Ball
    //Begin Add - Starbreaker 23 March 2005
    AMMOCRATE_SLAM,             //Slams
    AMMOCRATE_SMG1_ALTFIRE,     //SMG1 Grenades
    //End Add - Starbreaker 23 March 2005
    NUM_AMMO_CRATE_TYPES,       //How many types do we have?
};


Next we set the model info at around line 684; we will fix a few names and add some new ones.
Here is where I discovered the main problem with the fgd vs. code vs. models. Valve included only 4 ammo crate models, the smg1, ar2, rockets, and the grenades models.

 CODE (C++) 

// Models names
const char *CItem_AmmoCrate::m_lpzModelNames[NUM_AMMO_CRATE_TYPES] =
{
    "models/items/ammocrate_bullets.mdl",   // Small rounds
    "models/items/ammocrate_smg1.mdl",      // Medium rounds
    "models/items/ammocrate_ar2.mdl",       // Large rounds
    "models/items/ammocrate_rockets.mdl",   // RPG rounds
    "models/items/ammocrate_buckshot.mdl",  // Buckshot
    "models/items/ammocrate_grenade.mdl",   // Grenades
    "models/items/ammocrate_357.mdl",       // 357

//Begin Add/Change - Starbreaker 23 March 2005
// Here we change the model names of the Crossbow and AR2 Alt Fire models
// to reflect the new ones that I made, that have the correct graphics on the sides

    //"models/items/ammocrate_bullets.mdl", // Crossbow -Old
    "models/items/ammocrate_bolt.mdl",  // Crossbow -New
   
    //FIXME: This model is incorrect! - Fixed now 23 March 2005 by Starbreaker
    //"models/items/ammocrate_ar2.mdl",     // Combine Ball -Old
    "models/items/ammocrate_ar2_ball.mdl",      // Combine Ball -New

    //Now we add in the Slams and SMG1 Grenades
    "models/items/ammocrate_slam.mdl",  // Slam
    "models/items/ammocrate_smg1_grenade.mdl",  // SMG1 Grenades
//End Add/Change - Starbreaker 23 March 2005
};


Ok, a side note about these new models I made. I used Cannon fodders Model Decompiler to rip apart valves SMG1 ammo crate model. Now I am no modeler, so all the models I have, have the ammo boxes for the SMG ammo type in them unlike the original 4 models, that have grenades, rockets, and AR2 magazines. Maybe some kind soul out there will point me to a way to fix this, but for now they all just have big boxes in them.

Ok, here we setup the ammo types names for giving ammo, one thing I did notice, is that they all have to be named the same as the ammo type they are with the exception of the frag and the slam due to the way they are given later.

 CODE (C++) 

// Ammo type names
const char *CItem_AmmoCrate::m_lpzAmmoNames[NUM_AMMO_CRATE_TYPES] =
{
    "Pistol",      
    "SMG1",        
    "AR2",          
    "RPG_Round",    
    "Buckshot",    
    "Grenade",
    "357",
    "XBowBolt",
    "AR2AltFire",
//Begin Add - Starbreaker 23 March 2005
    "Slam",
    "SMG1_Grenade",
//End Add - Starbreaker 23 March 2005
};


Here we set how many you get. If your mod allows for more ammo of each type, change these numbers as needed.

 CODE (C++) 

// Ammo amount given per +use
int CItem_AmmoCrate::m_nAmmoAmounts[NUM_AMMO_CRATE_TYPES] =
{
    300,    // Pistol
    300,    // SMG1
    300,    // AR2
    3,      // RPG rounds
    120,    // Buckshot
    5,      // Grenades
    50,     // 357
    50,     // Crossbow
    3,      // AR2 alt-fire
//Begin Add - Starbreaker 23 March 2005
    5,      //Slams
    3,      //SMG1 Grenades
//End Add - Starbreaker 23 March 2005
};


Ok, now here because of the nature of the grenades and slams, you have to give a weapon vs. ammo incase the player does not have one to begin with. If you wanted the player to receive a weapon when he opens the ammo crate, you would just add the name of the weapon to the appropriate slot.

 CODE (C++) 

const char *CItem_AmmoCrate::m_pGiveWeapon[NUM_AMMO_CRATE_TYPES] =
{
    NULL,   // Pistol
    NULL,   // SMG1
    NULL,   // AR2
    NULL,       // RPG rounds
    NULL,   // Buckshot
    "weapon_frag",      // Grenades
    NULL,       // 357
    NULL,       // Crossbow
    NULL,       // AR2 alt-fire
//Begin Add - Starbreaker 23 March 2005
    "weapon_slam",  //Slams
    NULL,       //SMG1 Grenade
//End Add - Starbreaker 23 March 2005
};


Ok, now we are going to change something to reflect a new weapon. It appears that you can open the crate by hitting them with the crowbar, but not the stun stick, and here is the fix for that:

 CODE (C++) 

//-----------------------------------------------------------------------------
// Purpose: allows the crate to open up when hit by a crowbar
//-----------------------------------------------------------------------------
int CItem_AmmoCrate::OnTakeDamage( const CTakeDamageInfo &info )
{
    // if it's the player hitting us with a crowbar, open up
    CBasePlayer *player = ToBasePlayer(info.GetAttacker());
    if (player)
    {
        CBaseCombatWeapon *weapon = player->GetActiveWeapon();

//Begin Change - Starbreaker 23 March 2005
// Just add a logical or operator and the stunstick string name.
        if (weapon && (!stricmp(weapon->GetName(), "weapon_crowbar")) || (!stricmp(weapon->GetName(), "weapon_stunstick")))
//End Change - Starbreaker 23 March 2005
        {
            // play the normal use sound
            player->EmitSound( "HL2Player.Use" );
            // open the crate
            Use(info.GetAttacker(), info.GetAttacker(), USE_TOGGLE, 0.0f);
        }
    }

    // don't actually take any damage
    return 0;
}


Ok, that ends the code part.
All we need to do now if modify the FGD to add are 2 new ammo crate types.

 CODE  

@PointClass base(Targetname, Angles) studio("models/items/ammocrate_rockets.mdl") = item_ammo_crate : "Ammo Crate"
[
    AmmoType(choices) : "Ammo Type" : 0 =
    [
        0 : "Pistol"
        1 : "SMG1"
        2 : "AR2"
        3 : "RPG Rounds"
        4 : "Buckshot"
        5 : "Grenades"
        6 : "357"
        7 : "XBowBolt"
        8 : "AR2 Alt-Fire Round"
//Added slams and SMG1 Grenades - Starbreaker 23 - March - 2005
        9 : "Slams"
        10 : "SMG1 Grenades"
    ]

    // Inputs
    input Kill(void) : "Remove the ammo crate"
    output OnUsed(void) : "Fires when +used by the player."
]


And that is all there is to it.

Click here Ammocrate.zip to get the models and skins for the new Ammocrate type and the fixed skins.

Hope you enjoy it.

Dave R. Meyers
Starbreaker
OZ Deathmatch

Rate This Article
This article is currently rated: 5 out of 5.0 (1 Votes)

You have to register to rate this article.
User Comments Showing comments 1-2

Posted By: rookie on Jul 19 2006 at 14:06:42
I cannot use the modifyed fgd ,help. :(
here is my fgd:

@include "hl2mp.fgd"
@PointClass base(Targetname, Angles) studio("models/items/ammocrate_rockets.mdl") = item_ammo_crate : "Ammo Crate"
[
AmmoType(choices) : "Ammo Type" : 0 =
[
0 : "Pistol"
1 : "SMG1"
2 : "AR2"
3 : "RPG Rounds"
4 : "Buckshot"
5 : "Grenades"
6 : "357"
7 : "XBowBolt"
8 : "AR2 Alt-Fire Round"
9 : "Slams"
10 : "SMG1 Grenades"
]

// Inputs
input Kill(void) : "Remove the ammo crate"
output OnUsed(void) : "Fires when +used by the player."
]


What i have done was wrong?Edited by rookie on Jul 19 2006, 14:08:15

Posted By: EoD on Aug 15 2007 at 15:15:56
In the "int CItem_AmmoCrate::OnTakeDamage( const CTakeDamageInfo &info )" function, do you really mean the following line:
if (weapon && (!stricmp(weapon->GetName(), "weapon_crowbar")) || (!stricmp(weapon->GetName(), "weapon_stunstick")))

I think, some brackets went wrong, and it should look like this:
if (weapon && (!stricmp(weapon->GetName(), "weapon_crowbar") || !stricmp(weapon->GetName(), "weapon_stunstick")))


You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 05 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
knock knock
General | News
By: omega | Dec 22 2016
 
knock knock
General | News
By: MIFUNE | Oct 10 2015
 
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
 
knock knock
General | News
By: Whistler | Nov 05 2011
 
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
 
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Persuter | Aug 02 2009
 

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (5 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!