Welcome, Guest! Login | Register

Smoke Grenade [Print this Article]
Posted by: Mercalli
Date posted: Aug 07 2005
User Rating: 4.3 out of 5.0
Number of views: 4734
Number of comments: 4
Description: Change the frag grenade in MP HL to a smoke grenade
This is a fairly simple snippet as the main chunk of code we use to make a smoke grenade is already avaible in the SDK. (Look for "test_smokeGrenade")

First thing we need to do is add a variable so that only the frag grenade releases smoke when it explodes. It will also help us tell the engine to not do damage to the player when it explodes.

open up basegrenade_shared.h and after the line
 CODE (C++) 
float               m_flWarnAITime;             // Time at which to warn the AI

and add a bool variable
 CODE (C++) 

bool                m_bIsSmokeGrenade;          // Are we a smoke grenade?

Next thing we need to do is go into the file grenade_frag.cpp and scroll down to the ::Spawn function
and find the line:
 CODE (C++) 

CreateEffects();

We need to say that we want this grenade to be a smoke grenade so we set our bool to true. So after the CreateEffects(); line add this:
 CODE (C++) 

m_bIsSmokeGrenade = true;


Open up basegrenade_shared.cpp and find the line void CBaseGrenade::Explode( trace_t *pTrace, int bitsDamageType )
This is the function where we will put our code to actually create the smoke cloud.
It should look like this:
 CODE (C++) 

void CBaseGrenade::Explode( trace_t *pTrace, int bitsDamageType )
{
#if !defined( CLIENT_DLL )
   
    SetModelName( NULL_STRING );//invisible
    AddSolidFlags( FSOLID_NOT_SOLID );

    m_takedamage = DAMAGE_NO;

    // Pull out of the wall a bit
    if ( pTrace->fraction != 1.0 )
    {
        SetLocalOrigin( pTrace->endpos + (pTrace->plane.normal * 0.6) );
    }
       
        Vector vecAbsOrigin = GetAbsOrigin();
        int contents = UTIL_PointContents ( vecAbsOrigin );

    if ( pTrace->fraction != 1.0 )
    {
        Vector vecNormal = pTrace->plane.normal;
        surfacedata_t *pdata = physprops->GetSurfaceData( pTrace->surface.surfaceProps );
        CPASFilter filter( vecAbsOrigin );
        te->Explosion( filter, -1.0, // don't apply cl_interp delay
            &vecAbsOrigin,
            !( contents & MASK_WATER ) ? g_sModelIndexFireball : g_sModelIndexWExplosion,
            m_DmgRadius * .03,
            25,
            TE_EXPLFLAG_NONE,
            m_DmgRadius,
            m_flDamage,
            &vecNormal,
            (char) pdata->game.material );
       
    }
    else
    {
        CPASFilter filter( vecAbsOrigin );
        te->Explosion( filter, -1.0, // don't apply cl_interp delay
            &vecAbsOrigin,
            !( contents & MASK_WATER ) ? g_sModelIndexFireball : g_sModelIndexWExplosion,
            m_DmgRadius * .03,
            25,
            TE_EXPLFLAG_NONE,
            m_DmgRadius,
            m_flDamage );
    }
#if !defined( CLIENT_DLL )
    CSoundEnt::InsertSound ( SOUND_COMBAT, GetAbsOrigin(), BASEGRENADE_EXPLOSION_VOLUME, 3.0 );
#endif

    // Use the thrower's position as the reported position
    Vector vecReported = m_ hThrower ? m_hThrower->GetAbsOrigin() : vec3_origin;
...


We need to check to see if the grenade that is exploding is a smoke grenade and if so create a smoke cloud. So after the line
 CODE (C++) 

 int contents = UTIL_PointContents ( vecAbsOrigin );

we are going to add our check and the actual smoke creating code.
 CODE (C++) 

if ( m_bIsSmokeGrenade == true )
{
   ParticleSmokeGrenade *pSmoke = dynamic_cast<ParticleSmokeGrenade*>( CreateEntityByName(PARTICLESMOKEGRENADE_ENTITYNAME) );

        Vector vForward;
        AngleVectors( GetLocalAngles(), &vForward );
        vForward.z = 0;
        VectorNormalize( vForward );

        pSmoke->SetLocalOrigin( GetLocalOrigin() + vForward * 75 );
        pSmoke->SetFadeTime(25, 30); // Fade out between 25 seconds and 30 seconds.
        pSmoke->Activate();
        pSmoke->SetLifetime(30);
        pSmoke->FillVolume();
}

else
{

And don't forget the else { bit at the end. So that if it's not a smoke grenade we need to just explode. To close that else add a closing bracket "}") at the end of
 CODE (C++) 

else
        {
        CPASFilter filter( vecAbsOrigin );
        te->Explosion( filter, -1.0, // don't apply cl_interp delay
            &vecAbsOrigin,
            !( contents & MASK_WATER ) ? g_sModelIndexFireball : g_sModelIndexWExplosion,
            m_DmgRadius * .03,
            25,
            TE_EXPLFLAG_NONE,
            m_DmgRadius,
            m_flDamage );
        }
}  <--the new closing bracket


While we are still working inside the ::Explode function we can make sure that when the smoke grenade explodes it doesn't do damage. So go to the line
 CODE (C++) 

RadiusDamage( info, GetAbsOrigin(), m_DmgRadius, CLASS_NONE, NULL );
and right above that add this:
 CODE (C++) 

if( !m_bIsSmokeGrenade )

if you want it to still do damage just negate this part. We also need to reset the bool we have back to false at the end of the ::Explode function right before the #endif add:
 CODE (C++) 

m_bIsSmokeGrenade = false;


We're almost done. Last thing we must do is add a header file so that the ParticleSmokeGrenade class functions are usuable.

Go up to the top and after #if !defined( CLIENT_DLL ) add in
 CODE (C++) 

#include "particle_smokegrenade.h"

and we're finished!

Now when the frag grenade explodes it will start releasing a smoke plume.

Known Issues:Sometimes when the grenade is thrown and explodes close to a wall the smoke cloud never materializes.
This code has also only been tested on the HL MP SDK.

Rate This Article
This article is currently rated: 4.3 out of 5.0 (3 Votes)

You have to register to rate this article.
User Comments Showing comments 1-4

Posted By: ts2do on Aug 21 2005 at 02:31:22
This is very hacky, shouldnt you make totally new classes and implement a new Explode function there?
....I also remember with cs 1.6, there were bubbles, think you're up to the challenge?
Does the smoke cloud make the player's screen fade?Edited by ts2do on Aug 21 2005, 02:32:22

Posted By: Mercalli on Aug 23 2005 at 09:27:34
-Yes, it is 'hacky'. that's why I submitted it to the snippet section, I think this gives someone with a basic knowledge of C++ enough information to code a new explode function on their own, and use it wherever they want..

-Bubbles? not sure what you mean there..

-Yes the screen will fade when you start to enter the smoke cloud.

Posted By: ruiner on Sep 11 2005 at 23:04:22
Remove:
Vector vForward;
AngleVectors( GetLocalAngles(), &vForward );
vForward.z = 0;
VectorNormalize( vForward );

pSmoke->SetLocalOrigin( GetLocalOrigin() + vForward * 75 );
Add
pSmoke->SetLocalOrigin( GetLocalOrigin() );

No more issues with smoke not showing up. You were changing the origin, while this could go easily into a wall and never materialize. I'm not sure what the purpose of the original code was?

Posted By: Nic2 on Jul 10 2006 at 13:46:28
Is there a way to change the color of the smoke? And how do I set my own sprite? :)


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 04 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
Where do we go from here
General | News
By: MIFUNE | Jun 09 2008
 
The Input/Output system
Half-Life 2 | Level Design
By: nazitaco | Dec 23 2007
 
Where do we go from here
General | News
By: Rob_F | Nov 22 2007
 
Rescaling Half-Life
Half-Life | Coding | Shared Tutorials
By: christoph | Nov 12 2007
 
GameUI
Half-Life 2 | Coding | Client Side Tutorials
By: Evil_j | Oct 29 2007
 
3 State Zoom For Any Weapon
Half-Life 2 | Coding | Server Side Tutorials
By: Ennuified | Oct 18 2007
 
Storing weapons on ladder
Half-Life 2 | Coding | Snippets
By: cct | Sep 07 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: DarkNight | Aug 28 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: deedok | Aug 20 2007
 
Debugging your half-life 2 mod
Half-Life 2 | Coding | Articles
By: blackshark | Aug 17 2007
 

Site Info
296 Approved Articless
5 Pending Articles
3940 Registered Members
0 People Online (13 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!