Bouncing Rockets

From ioquake3 wiki
Revision as of 10:19, 28 September 2010 by ZTurtleMan (talk | contribs) (Fix header)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Tutorial 6 - Bouncing Rockets

By [AssKicka]

This tutorial will show you how to make rockets bounce off walls by using a console command to avtivate/deactivate the rocket bounce feature.

Add a new Entity Flag

Open g_local.h and add the line in red after line 32

// gentity->flags
#define FL_GODMODE              0x00000010
#define FL_NOTARGET             0x00000020
#define FL_TEAMSLAVE            0x00000400      // not the first on the team
#define FL_NO_KNOCKBACK         0x00000800
#define FL_DROPPED_ITEM         0x00001000
#define FL_NO_BOTS              0x00002000      // spawn point not for bot use
#define FL_NO_HUMANS            0x00004000      // spawn point just for bots
#define FL_ROCKETBOUNCE         0x00008000

If you're used to working with 'flags' or 'bits' then this stuff should look familiar to you. If it looks like Greek, a bit of an explanation about 'hexadecimal numbers' and 'bitwise operators' might be useful... have a read of our article on flags'n'stuff.

Implement an 'Rbounce' command

Now Open g_cmds.c and add the lines in red after line 1022 :

void Cmd_RBounce_f( gentity_t *ent ) {

       char *msg; // message to player

       ent->flags ^= FL_ROCKETBOUNCE;

       if (!(ent->flags & FL_ROCKETBOUNCE))
               msg = "Rocket Bounce OFF\n";
               msg = "Rocket Bounce ON\n";
       trap_SendServerCommand( ent-g_entities, va("print \"%s\"", msg));

This allows a player to 'toggle' bouncing rockets on and off. Now goto line 1155 and add the lines in red :

       else if (Q_stricmp (cmd, "setviewpos") == 0)
       Cmd_SetViewpos_f( ent );
       else if (Q_stricmp (cmd, "rbounce") == 0)
       Cmd_RBounce_f( ent );

This part simply catches a command that a player enters on the console (type "\rbounce", for example) and calls the function Cmd_RBounce_f(). We like to think of this part of code as our 'hook' to the outside world :)

Modify the Rocket's Physics

       bolt = G_Spawn();
       bolt->classname = "rocket";
       if (self->flags & FL_ROCKETBOUNCE)
               bolt->nextthink = level.time + 2500;
               bolt->nextthink = level.time + 10000;
       bolt->think = G_ExplodeMissile;
       bolt->s.eType = ET_MISSILE;
       bolt->r.svFlags = SVF_USE_CURRENT_ORIGIN;
       bolt->s.weapon = WP_ROCKET_LAUNCHER;
       if (self->flags & FL_ROCKETBOUNCE)
               bolt->s.eFlags = EF_BOUNCE;
       bolt->r.ownerNum = self->s.number;

The if (self->flags & FL_ROCKETBOUNCE) bit tests if the particular client has FL_ROCKETBOUNCE turned on. If so, the rocket will explode after 2.5 seconds (instead of 10). The magic part is adding EF_BOUNCE to the rocket's eFlags (which I suppose stands for 'effects flags'... browse bg_public.h line 195 for a list of all the EF_ flags).

Re-Build your game.qvm file and enjoy !

« Tutorial 5 - Armor Piercing Rails | Tutorial 6 - Bouncing Rockets| Tutorial 7 - Cloaking »