Welcome, Guest! Login | Register

Creating a Dash key [Print this Article]
Posted by: X-0ut
Date posted: Apr 28 2003
User Rating: 5 out of 5.0
Number of views: 6719
Number of comments: 2
Description:
This tutorial will show you how to setup a new key and console command, which can then be used in game to make the player sprint when holding the key, for simplicity I did not add stamina to the code, this is something you should work on to stop the player from always sprinting.
Maybe I will make an extension to this tutorial in the future for stamina with clientside hud sprite depicting the stamina status, and perhaps even a pm_shared version.

First we can't just go and create new keys like IN_JUMP and IN_DUCK because there is a limit to 16 bits, to work around this we have to override a key thats not in use, for this tutorial we will override the IN_ALT1 key.

Open up in_buttons.h (common folder) and comment out the IN_ALT1 key, your code should look something like this:
 CODE  

#define IN_RELOAD   (1 << 13)
////////////////////////////
// we comment out IN_ALT1 //
// because we cant define //
// new keys               //
////////////////////////////
//#define IN_ALT1  (1 << 14)
#define IN_DASH  (1 << 14)
////////////////////////////
#define IN_SCORE    (1 << 15)   // Used by client.dll for when scoreboard is held down

Client DLL Specific Changes:
Now in input.cpp we must comment out every reference to the IN_ALT1 key and replace it with our new IN_DASH key.
Near the top of the file you will see a list of all the default keys like in_jump, in_duck etc, you need to alter the code so its like this:
 CODE  

kbutton_t                 in_reload;
//remember we are using alt1 as our new key//
//kbutton_t               in_alt1;
kbutton_t                 in_dash;
///////////////////
kbutton_t                 in_score;

A little further down the file you will see a bunch of functions that detect if the key is down or up, alter it so its like this:
 CODE  

void IN_ReloadDown(void) {KeyDown(&in_reload);}
void IN_ReloadUp(void) {KeyUp(&in_reload);}
//we must replace every alt1 with our dash//
//void IN_Alt1Down(void) {KeyDown(&in_alt1);}
//void IN_Alt1Up(void) {KeyUp(&in_alt1);}
void IN_DashDown(void) {KeyDown(&in_dash);}
void IN_DashUp(void) {KeyUp(&in_dash);}
////////////////////////////////////////
void IN_GraphDown(void) {KeyDown(&in_graph);}
void IN_GraphUp(void) {KeyUp(&in_graph);}

We need to make two more adjustments in the CL_ButtonBits function, firstly:
 CODE  

//again just replacing alt1 with dash//
/*  if (in_alt1.state & 3)
    {
                  bits |= IN_ALT1;
    }
*/
    if (in_dash.state & 3)
    {
                  bits |= IN_DASH;
    }
//////////////////////////////
    if ( in_score.state & 3 )
    {
                  bits |= IN_SCORE;
    }

And secondly at the bottom of the function:
 CODE  

  in_reload.state &= ~2;
  ///nearly there//
//  in_alt1.state &= ~2;
  in_dash.state &= ~2;
  ////////////////////
  in_score.state &= ~2;

Finally we have one more piece of code to alter, go down to the bottom of the file , in the InitInput function where the console commands are added, make it like so:
 CODE  

    gEngfuncs.pfnAddCommand ("-reload", IN_ReloadUp);
    ///here we make the +dash command so we can bind it ingame//
//  gEngfuncs.pfnAddCommand ("+alt1", IN_Alt1Down);
//  gEngfuncs.pfnAddCommand ("-alt1", IN_Alt1Up);
    gEngfuncs.pfnAddCommand ("+dash", IN_DashDown);
    gEngfuncs.pfnAddCommand ("-dash", IN_DashUp);
//////////////////////////////////////////////////
    gEngfuncs.pfnAddCommand ("+score", IN_ScoreDown);

Shared Changes
Open up pm_shared.c and find the PM_WalkMove function, at the top of the function you'll find a bunch of varaible declarations C style, we need to add a bool, and check if the client has pressed the IN_DASH button, we can do that like so:
 CODE  

    float downdist, updist;
  
    pmtrace_t trace;
    ///if we are dashing (ooo)///
    qboolean  bIsDashing;
    if ( pmove->cmd.buttons & IN_DASH )
    {
         pmove->oldbuttons |= IN_DASH;
         bIsDashing = true;
    }
    else
    {
         pmove->oldbuttons &= ~IN_DASH;
         bIsDashing = false;
    }
    /////////////////////////////
    // Copy movement amounts
    fmove = pmove->cmd.forwardmove;
    smove = pmove->cmd.sidemove;

Im sure you can see whats happening there, if the key is pressed, the boolean is set true, otherwise its set false.
Now a little further down the function you will find this code:
 CODE  

    for (i=0; i<2; i++)       // Determine x and y parts of velocity
         wishvel[i] = pmove->forward[i]*fmove + pmove->right[i]*smove;

What that is doing is copying the velocity into wishvel, we just need to alter it slightly so that it adds more speed if our boolean is true, like so:
 CODE  

    for (i=0; i<2; i++){       // Determine x and y parts of velocity
        if(bIsDashing && (! ( pmove->flags & FL_DUCKING)))
            wishvel[i] = pmove->forward[i]*(fmove*200)+ pmove->right[i]*smove;
    else
        wishvel[i] = pmove->forward[i]*fmove + pmove->right[i]*smove;
    }

Ok, so same as before the velocity is being copied into wishvel, except now if our boolean is true it copies a greater amount ((fmove*200)) .
Still one more thing to do, even further down the function the speed is capped off to the server maxspeed, we need to alter it to allow for our dash to work, find this code:
 CODE  

    if (wishspeed > pmove->maxspeed)
    {
        VectorScale (wishvel, pmove->maxspeed/wishspeed, wishvel);
        wishspeed = pmove->maxspeed;
    }

And alter it like so:
 CODE  

    if(bIsDashing && (! ( pmove->flags & FL_DUCKING)))
    {
        if (wishspeed > pmove->maxspeed + 200)
        {
            VectorScale (wishvel, (pmove->maxspeed+200)/wishspeed, wishvel);
            wishspeed = pmove->maxspeed + 200;
        }
    }
    else
    {
        if (wishspeed > pmove->maxspeed)
        {
            VectorScale (wishvel, pmove->maxspeed/wishspeed, wishvel);
            wishspeed = pmove->maxspeed;
        }
    }

What that is doing is checking our boolean once again, if its true it checks the wishspeed against the maxspeed + 200 and caps it off, if not the wishspeed is checked against the normal maxspeed - and capped.

Thats it we are all done, you will need to compile both server and client workspaces for the effect to work, you will also need a delta.lst (found in the valve folder)
You'll need to bind the dash command which you can do by opening up the console and binding it to a key like this:
 CODE  

bind "f" "+dash"

Also, you will need to alter the servers maxspeed which you can do with the sv_maxspeed cvar:
 CODE  

sv_maxspeed 1000

Thats it for this tutorial.

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: sluggo on Sep 15 2003 at 04:45:03
I was playing with keys recently and discovered that IN_RUN 1 << 12 also appears unused.. every now and then it's nice to find we have ONE extra bit we need as modders.. :p.

Also, you can subjugate IN_SCORE if you'd like, anything IMPORTANT to do with scoreboards are clientside only, so no need to use up precious pev->button bits for that. (you'll have to modify one bit of code in the PlayerDeathThink for this to work).

Nice little tutorial btw, succinct and to the point, explained well :).Edited by sluggo on Sep 15 2003, 04:45:40

Posted By: Kuja on Aug 21 2004 at 22:57:38
Messing around with my mod (we'd used all the available bits) I was able to get some extra manuvering room by making one of our bits a "LEFT/RIGHT" bit. Ie, if the bit was set it meant that motions were supposed to go left, otherwise right. This turned MOVELEFT/MOVERIGHT into MOVE, and LEFT/RIGHT into TURN. (giving one extra bit). We also had LEANLEFT/LEANRIGHT, which turned into LEAN (giving another extra bit). This was enough for dash and prone.


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!