Welcome, Guest! Login | Register

Booby trap satchel [Print this Article]
Posted by: ScoBra7
Date posted: Apr 07 2003
User Rating: 5 out of 5.0
Number of views: 2366
Number of comments: 0
Description: How nasty can you get!
In this tutorial we will add a new type of Satchel using the weapons box as a model. What is the first thing people run to pick up? The weapons box! So we are going to equip this booby trap with a proximity fuse.

First open the mp.dll workspace. Open the satchel.cpp and add the following after this CSatchelCharge class
 CODE  

class CBoobyTrap: public CSatchelCharge
{
public:
    void Spawn( void );
    void Precache( void );
    void EXPORT BoobyThink( void );
private:
 float m_flActivateProximity;
 bool m_bPlaySound;
 bool CheckProx(int iRaduis);
 float m_flProximityCheckInterval;

};
LINK_ENTITY_TO_CLASS( monster_booby, CBoobyTrap );

void CBoobyTrap::Spawn(void)
{
 Precache( );
    // motor
    pev->movetype = MOVETYPE_BOUNCE;
    pev->solid = SOLID_BBOX;

    SET_MODEL(ENT(pev), "models/w_weaponbox.mdl");
   
    //UTIL_SetSize(pev, Vector( -16, -16, -4), Vector(16, 16, 32)); // Old box -- size of headcrab monsters/players get blocked by this
    UTIL_SetSize(pev, Vector( -4, -4, -4), Vector(4, 4, 4));    // Uses point-sized, and can be stepped over
    UTIL_SetOrigin( pev, pev->origin );

    SetTouch( SatchelSlide );
    SetUse( DetonateUse );
    SetThink( BoobyThink );
    pev->nextthink = gpGlobals->time + 0.1;

    pev->gravity = 0.5;
    pev->friction = 0.8;

    pev->dmg = gSkillData.plrDmgSatchel;
    // ResetSequenceInfo( );
    pev->sequence = 1;

    m_flActivateProximity = gpGlobals->time + 5.0; //The 5.0 is the # of Seconds
    m_flProximityCheckInterval = m_flActivateProximity; //First check when activated.
    m_bPlaySound = false;
}

void CBoobyTrap::Precache( void )
{
    PRECACHE_MODEL("models/w_weaponbox.mdl"); //booby trap
    PRECACHE_SOUND("weapons/g_bounce1.wav");
    PRECACHE_SOUND("weapons/g_bounce2.wav");
    PRECACHE_SOUND("weapons/g_bounce3.wav");
}

bool CBoobyTrap::CheckProx(int iRaduis)
{
    CBaseEntity *pOther = NULL; //Just declare this variable
    if( gpGlobals->time >= m_flActivateProximity)
    {
 if(!m_bPlaySound)
 {
   m_bPlaySound = true;
   EMIT_SOUND( ENT(pev), CHAN_VOICE, "buttons/blip2.wav", 1.0, ATTN_NORM );
 }
 if(gpGlobals->time >= m_flProximityCheckInterval)
 {
   m_flProximityCheckInterval = gpGlobals->time + 0.5; //Check every 0.5 seconds
   while((pOther = UTIL_FindEntityInSphere( pOther, pev->origin , iRaduis)) != NULL)
   {
  if ( pOther->pev->takedamage != DAMAGE_NO )
  {
     return true;
  }
   }
 }
    }
    return false;
}

void CBoobyTrap :: BoobyThink( void )
{
    StudioFrameAdvance( );
    pev->nextthink = gpGlobals->time + 0.1;

    if (!IsInWorld())
    {
 UTIL_Remove( this );
 return;
    }

    if (pev->waterlevel == 3)
    {
 pev->movetype = MOVETYPE_FLY;
 pev->velocity = pev->velocity * 0.8;
 pev->avelocity = pev->avelocity * 0.9;
 pev->velocity.z += 8;
    }
    else if (pev->waterlevel == 0)
    {
 pev->movetype = MOVETYPE_BOUNCE;
    }
    else
    {
 pev->velocity.z -= 8;
    }
    if(CheckProx(32))
    {
 SetThink( Detonate );
    }
}


We have inherited most of the functionality of the normal CSatchelCharge. We override the Spawn and the Precache functions. The Spawn function is overridden to set the model and the think for the booby trap. The Precache function is overridden to set the model for the booby trap. We create new functions, BoobyThink and CheckProx. The BoobyThink is much like the SatchelThink except that it calls CheckProx to see if it should detonate. The CheckProx first evaluate the time to see if it should activate the booby trap. When activating the booby trap a sound will play (once) to indicate that it activated. Next, this function looks for entities in a sphere that can take damage. The radius of the sphere is supplied as a parameter. We set the radius small (32) so that the satchel only explodes when a player steps on it.

We have to make the functions that we override from CSatchelCharge public. C++s default is not public. So find class CSatchelCharge : public CGrenade and move the public keyword to the top of the class definition. (From before Deactivate to before Spawn.)

In void CSatchel::Precache( void ) add the following:
 CODE  

PRECACHE_SOUND("buttons/blip2.wav");
.
NOTE: For some reason, Half-life does not want to precache this in the CBoobyTrap::Precache function.
Editors note: UTIL_PrecacheOther("monster_booby"); in weapons.cpp to make this entities precache function get called.

We will use the secondary fire to throw booby traps.
First find class CSatchel : public CBasePlayerWeapon and add
 CODE  
 void ThrowBT( void )
 


Next create the function:
 CODE  

void CSatchel::ThrowBT( void )
{
    if (m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType])
    {
 Vector vecSrc = m_pPlayer->pev->origin;

 Vector vecThrow = gpGlobals->v_forward * 274 + m_pPlayer->pev->velocity;

 CBaseEntity *pSatchel = Create( "monster_booby", vecSrc, Vector( 0, 0, 0), m_pPlayer->edict() );
 pSatchel->pev->velocity = vecThrow;
 pSatchel->pev->avelocity.y = 400;

 m_pPlayer->pev->viewmodel = MAKE_STRING("models/v_satchel_radio.mdl");
 m_pPlayer->pev->weaponmodel = MAKE_STRING("models/p_satchel_radio.mdl");
 SendWeaponAnim( SATCHEL_RADIO_DRAW );

 // player "shoot" animation
 m_pPlayer->SetAnimation( PLAYER_ATTACK1 );

 m_chargeReady = 1;
 
 m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]--;

 m_flNextPrimaryAttack = gpGlobals->time + 1.0;
 m_flNextSecondaryAttack = gpGlobals->time + 0.5;
    }
}


Now we can change the Secondary fire to throw booby traps. Find void CSatchel::SecondaryAttack( void ) and replace the body of the function with
 CODE  
ThrowBT();


To enable the booby trap to explode when pressing the radios button (primary attack after deploying) we need to change void CSatchel::PrimaryAttack(). Find in this function
 CODE  
 if (FClassnameIs( pSatchel->pev, "monster_satchel"))
and change it to
 CODE  
 if ((FClassnameIs( pSatchel->pev, "monster_satchel")) || (FClassnameIs( pSatchel->pev, "monster_booby")))


You can change or add new classes using different types of models. Save satchel.cpp

That's all, Enjoy! Thats all folks. You can e-mail me at scobra@ananzi.co.za for comments and questions.

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

No User Comments

You must register to post a comment. If you have already registered, you must login.
Donate
Any money that is donated will go towards the server costs that I incur for running our server. Don't feel you HAVE to donate, but the link is there if you want to help us out a little. Thanks :)

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
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
 
How to add Ammocrates for missing ammo type and/or create new ammo types
Half-Life 2 | Coding | Server Side Tutorials
By: EoD | Aug 15 2007
 
GameUI
Half-Life 2 | Coding | Client Side Tutorials
By: G.I. Jimbo | May 19 2007
 
Reading and Writing Entire Structures
General | General Coding
By: monokrome | Jan 27 2007
 
VGUI Scope
Half-Life | Coding | Client Side Tutorials
By: Pongles | Jan 19 2007
 
Where do we go from here
General | News
By: SilentSounD | Jan 18 2007
 

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

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!