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: 5155
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 radio’s 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! That’s 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.

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: MIFUNE | Dec 31 2017
 
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
 

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (7 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!