Electrial Shock Gun

I was asked to work up some code for an electrial weapon of sorts. So here is the tutorial for shuch a weapon. It's fairly easy and I wrote this while working up the code for it. It makes the person glow, while shaking their screen and making it blueish.

First add a timer to the client structure. This will keep track of how long you will be effected by the weapon.

Add

float          elec_shock_framenum;

just after

qboolean       update_chase;          // need to update chase info?

In g_local.h This will be incremented when you get shot by a blaster or hyperblaster

Next you have to have a handling function for each frame. The best place to put this is in ClientEndServerFrame in p_view.c. Add the following function call in at the end of that function.

if (ent->client->elec_shock_framenum)  // checks that it is greater than 0
        elecshock(ent);                // The function for handling electrial shocks.

If you didn't know, in c if a number is greater or less than 0 then it is logically True, if it is 0 then it is logically False.

Add a function prototype at the top of p_view.c for elecshock. Just add

void elecshock(edict_t *ent);

At the top of p_view.c just after #include "g_local.h"

Next, Create a new file and call it something like (elecshock.c) Add this to your project/makefile.

Within this file we will handle all the effects of being shocked. You could have put this in one of the original files, but I think its a good idea to keep your code as separated from the basic code as possible.

Paste the code below into this file.

#include "g_local.h"
 
 
void elecshock(edict_t *ent)
{
int i;
 
ent->client->elec_shock_framenum-=1; // subtracts 1 from elec_shock_framenum
if (ent->client->elec_shock_framenum>0)  // in case I screwed up and this gets called without the person being shocked.
{
        ent->s.effects |= EF_COLOR_SHELL;
        ent->s.renderfx |= RF_SHELL_BLUE;  // these two lines add a blue shell to the person.
        for (i=0 ; i<3 ; i++)
        {
                 ent->client->kick_origin[i] = crandom() * 0.35;
                 ent->client->kick_angles[i] = crandom() * 0.7;             // make the screen shake increase the multiplier fo the angles for a greater shaking effect
        }
        // unfortunatly you have to add the blueness to thier screen elsewhere (in p_view.c)
}
else
{
// the last frame will clean up all the effects
        ent->s.effects &= ~EF_COLOR_SHELL;
        ent->s.renderfx &= ~RF_SHELL_BLUE;
}
}

The code is commented so you should be able to understand what it is doing. Unfortunatly making the players screen blue has to be done in sv_calcblend in p_view.c

Add the code below into sv_calcblend at the bottom

        if (ent->client->elec_shock_framenum > 0)
               SV_AddBlend (0, 0, 1, 0.08, ent->client->ps.blend);

Now that is done, all you have to do is make the blaster activate the timer. Add this code into blaster_touch in g_weapons.c at the bottom on the function

        if (other->client)
               other->client->elec_shock_framenum+=80;

That will make the blaster make a player blue and shake for 8 seconds

Compile and try it out. as it is it will effect both the blaster and the hyperblaster, and the monsters blasters. It would be fairly simple to change that so only a players blaster is effected by adding a couple more checks into the if statement above.[ent->owner->client and !(self->spawnflags & 1)]


Skunkworks Tutorials