Onliners

Affiliates

Creating a Sniper Rifle in the 2.2 SDK

By Robert Prouse

In the last weapons tutorial we
used the SecondaryFire method in the weapon class to switch between full-
auto and semi-auto. In this tutorial, we are going to redo the same weapon,
but this time, we are going to use the secondary fire mode to switch between
zoomed in and zoomed out, like a sniper rifle. To keep it simple, we are
going to pretend that this is an M16 with a sniper scope on it so that you
can compare this tutorial with the last. You will notice that much of this
tutorial is the same as the last, this should give you a good idea what stays
the same for all weapons and what changes.

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.

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.

I will also only comment on the changes to the code between this and
the last tutorial.

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;

I am using the SecondaryAttack method to zoom in on like on a sniper rifle. The
only change here is that I have replaced the m_bSingleFire variable with
a InZoom() variable and added another event variable for the zoomed in event.
Also notice that we are overriding the Holster method from the base class, you
will see why later.

Starting at the top of this function, I have changed the code that makes
the weapon single shot so that it only does it when it is zoomed in. The only
other changes are in the accuracy, zoomed in is way more accurate and the
PLAYBACK_EVENT_FULL chooses which event to send based on whether or not it is
zoomed in.

Secondary attack is the biggest change. Here we change the field of
view (fov) for the player. This simulates looking through a rifle scope.
Changing m_pPlayer->pev->fov automatically gets sent to the client.

We must over-ride the Holster method so that we can reset the field of
view to normal before the weapon gets changed. If we don't do this, the next
weapon selected will still have the same fov (it will look zoomed in.)

These methods round out our weapon and are the same as before. Now make the
same changes as before to weapons.h. I will review them here for completeness,
but make sure you look through the last tutorial if you have any questions.

Client Side Modifications

That's it for server side changes, now lets move on to the client. The major
change on the client side is that you need a second event for the zoomed in fire.
All of the changes here will be towards that end.

You will also want to add a sprite and modify the hud text file for the zoomed
in crosshair. We use a sniper scope sprite and added lines like the following
to our HUD text file for this weapon;