THE HANDY VANDAL'S ALMANAC
Game Design Resources for Half-Life
Home : Topics : Menu : Reviews
Almanac 2 : Xen Rebels

Site no longer actively maintained ... HV

"More Fun Than Fragging Monster_Scientist"
 

Coding Barney's Weapon
See also: Coding - Mods - Programming - Monsters
Updated June 17, 2002 - 12:30 PM

 

Coding Barney's Weapon

Say you want to change Barney's weapon from the 9mm pistol to a 357. Here's how.

Examine this code from Barney.cpp -

//=========================================================
// BarneyFirePistol - shoots one round from the pistol at
// the enemy barney is facing.
//=========================================================
void CBarney :: BarneyFirePistol ( void )
{
Vector vecShootOrigin;

UTIL_MakeVectors(pev->angles);
vecShootOrigin = pev->origin + Vector( 0, 0, 55 );
Vector vecShootDir = ShootAtEnemy( vecShootOrigin );

Vector angDir = UTIL_VecToAngles( vecShootDir );
SetBlending( 0, angDir.x );
pev->effects = EF_MUZZLEFLASH;

FireBullets(1, vecShootOrigin, vecShootDir, VECTOR_CONE_2DEGREES, 1024, BULLET_MONSTER_9MM );

int pitchShift = RANDOM_LONG( 0, 20 );

// Only shift about half the time
if ( pitchShift > 10 )
pitchShift = 0;
else
pitchShift -= 5;
EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, "barney/ba_attack2.wav", 1, ATTN_NORM, 0, 100 + pitchShift );

CSoundEnt::InsertSound ( bits_SOUND_COMBAT, pev->origin, 384, 0.3 );

// UNDONE: Reload?
m_cAmmoLoaded--;// take away a bullet!
}

This is the line of code that makes him shoot the bullet:

FireBullets(1, vecShootOrigin, vecShootDir, VECTOR_CONE_2DEGREES, 1024, BULLET_MONSTER_9MM );

See how BULLET_PLAYER_9MM is Barney's regular bullet?

 

 

Now look in weapons.h, find this code:

// bullet types
typedef enum
{
BULLET_NONE = 0,
BULLET_PLAYER_9MM, // glock
BULLET_PLAYER_MP5, // mp5
BULLET_PLAYER_357, // python
BULLET_PLAYER_BUCKSHOT, // shotgun
BULLET_PLAYER_CROWBAR, // crowbar swipe

BULLET_MONSTER_9MM,
BULLET_MONSTER_MP5,
BULLET_MONSTER_12MM,
} Bullet;

The above "typedef enum" is a list of the various types of bullets.

Modify Barney.cpp like this, using the bullet type of your choice:

FireBullets(1, vecShootOrigin, vecShootDir, VECTOR_CONE_2DEGREES, 1024, BULLET_PLAYER_357 );

Now he's firing 357 ammo.

You might also want to change this line:

EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, "barney/ba_attack2.wav", 1, ATTN_NORM, 0, 100 + pitchShift );

Instead of "barney/ba_attack2.wav", substitute the 357 sound. (I don't know the name of the .wav file, you'll have to look it up.)

 

Rate of fire -- I'm not certain how to set the rate at which Barney fires, but I think it's in here:

//=========================================================
// CheckRangeAttack1
//=========================================================
BOOL CBarney :: CheckRangeAttack1 ( float flDot, float flDist )
{
if ( flDist <= 1024 && flDot >= 0.5 )
{
if ( gpGlobals->time > m_checkAttackTime )
{
TraceResult tr;

Vector shootOrigin = pev->origin + Vector( 0, 0, 55 );
CBaseEntity *pEnemy = m_hEnemy;
Vector shootTarget = ( (pEnemy->BodyTarget( shootOrigin ) - pEnemy->pev->origin) + m_vecEnemyLKP );
UTIL_TraceLine( shootOrigin, shootTarget, dont_ignore_monsters, ENT(pev), &tr );
m_checkAttackTime = gpGlobals->time + 1;
if ( tr.flFraction == 1.0 || (tr.pHit != NULL && CBaseEntity::Instance(tr.pHit) == pEnemy) )
m_lastAttackCheck = TRUE;
else
m_lastAttackCheck = FALSE;
m_checkAttackTime = gpGlobals->time + 1.5;
}
return m_lastAttackCheck;
}
return FALSE;
}

Notice this particular line of code:

m_checkAttackTime = gpGlobals->time + 1.5;

The value 1.5 indicates number of seconds between shots (I think). The 357 fires slower than the 9mm pistol, so use a value larger than 1.5 to indicate the delay between shots.

Having made your changes, compile HL.DLL, put it in the "dlls" directory, and run the game.

 

Barney Model

The standard Barney model uses the 9mm pistol. If you want the Barney model to use a 357 or some other weapon, you'll have to modify the model. For more info, see Models.

 
Changing Barney's Weapon
Handy Vandal's Almanac Logo

"Handy Vandal" and "Handy Vandal's Almanac" copyright 2008 by Karl Gregory Jones
Almanac: Half-Life : Half-Life 2
Xen Rebels
karljones.com
Contact the Handy Vandal

Half-Life © 1998-99 Sierra On-line and Valve L.L.C. All rights reserved. Half-Life and the Half-Life logo are trademarks of Sierra On-Line. Valve and the Valve logo are trademarks of Valve L.L.C. Half-Life images, textures, music, sound effects, and other graphic or audio content © 1998-99 Valve L.L.C. All rights reserved.