Onliners

Affiliates

Creating New Weapons in the 2.2 SDK

By Robert Prouse

There are plenty of tutorials out there on how to create new weapons,
but, while we were working on our mod,
Tour of Duty, we found that all of the tutorials were missing some steps
that you must take, didn't explain how things like weapon accuracy are
adjusted, and most importantly, none explained how to get client side
prediction working in the 2.2 SDK. Judging by the number of posts I see
on the mailing lists, client side events are causing a lot of problems.

I am assuming that you have the 2.2 Half-life SDK installed and you can
compile both the client dll and the server dll. If not, then you better
head out and read a few other tutorials first. And now for a few conventions:

Filenames are in green,Original code is in blue,Added code is in red.

To demonstrate everything you will need to add a new weapon to your mod,
I am going to show you how we added an M16 to
Tour of Duty. To keep things simple, I am going to simplify the weapon a
bit here and try to work with the original 2.2 SDK.

Please note though that you cannot just drop this weapon directly into
your mod, you will need to add models and sounds, or modify the code to use
your models and sounds. I also may have forgotten some of the includes
at the start of some of the files. Our mod is fairly different, so I had to
improvise here and there. If this is the case, it should be pretty
easy for you to figure out what is missing.

Okay, let's get at it...

Server Side Modifications

We will start working on the server dll, so open up the hl project and begin
by adding a .cpp
and .h file to the project for your weapon. These files should be in the
dlls directory of the source, for this project, we will call
them m16.cpp and m16.h. Open up m16.h and add the following code which will
define the CM16 class;

Notice that we have two classes here, CM16 and CM16AmmoClip. The first
is for the weapon itself and the second is for ammo clips for the weapon. I am
also using the SecondaryAttack method to switch the weapon between single shot
and automatic fire. You can also use SecondaryAttack to do things like
zoom in on sniper rifles or fire the weapon a different way, such as launching
a grenade from an M203 mounted to the weapon.

These are your models animations and must match those in your model. The
names can be changed, but they must match the order of animations in the model.
To check a weapon's animations, you can open it up in a program like
Half-Life Model Viewer and view the animations. Remember, it is the order
of the animations that is important, not what they are called in the model.

This is where we load any of the models or sounds that our weapon uses. All
of these must exist in your mod's directory or you can use ones in the valve
directory. Also notice the "events/m16.sc", this allows us to link this weapon
to a client side event. More on this later.

Here we set the information for the weapon. The secondary ammo has been
set to NULL and -1 because the M16 does not have a secondary ammo type (like
M203 grenades). Notice that the iSlot is zero based here, but in the method
iItemSlot() in the header, it is 1 based. There are five slots and five
positions under each slot. Every weapon in your mod (including any half-life
weapons you leave in) must have a unique slot/position combination, and must
have a unique id.

There isn't anything you will want to change in AddToPlayer. The only interesting
things in Deploy are the last two parameters. M16_DEPLOY is the deploy animation
and "mp5" is the series of animations in the player model that this weapon will use.
Valid values for standard HL player models are; "crowbar", "trip", "onehanded", "python",
"shotgun", "guass", "mp5", "rpg", "egon", "squeak", "hive" and "bow". Choose one
that suits your weapon.

This is the meat of the weapon, it fires off one round when the player presses
the left mouse button. Most of this is explained by the comments in the code.
If you want to make the weapon more accurate, make the number of degrees of accuracy
smaller. If you set tracer frequency to anything other than 0, the weapon will fire
tracers. If you set damage to 0, the weapon will use the default damage for the
bullet type.

This adds our new bullet type to the game. Now lets set up the damage this
bullet does. Open up combat.cpp and find the function
CBaseEntity::FireBulletsPlayer.
Now scroll down a little bit further and add the following line;

This gives the M16 the same damage as the MP5. If you want it to have it's own
damage, you will have to create your own damage variable like gSkillData.plrDmgMP5.
Just do a search for plrDmgMP5 and everywhere you find it, add in code for a
plrDmgM16.

Now we need to do some tedious work to allow your weapon to be used in the
game. Most of it doesn't require much thought,
so let's just do it. Start by opening func_break.cpp

Client Side Modifications

That's it for server side changes, now lets move on to the client. Open up
the cl_dll workspace and add in the m16.h and m16.cpp files from the server project.
Now create the script file in your mod's events directory. This should just be
an empty file called m16.sc. While you are at it, you will also want to
add sprites and a hud text file to the sprites directory. These are the sprites
that appear at the top of the screen when you select the weapon. There are a
few
good tutorials on how to do that, so I will leave that to them...

Now it is time to add in the client side events. Open up ev_hldm.cpp and add an
include for m16.h to the top after the other includes.