Making a weapon that heal players
Jump to navigation
Jump to search

This article or section needs to be cleaned up to conform to a higher standard of quality because:
Spelling and grammar.
For help, see the VDC Editing Help and Wikipedia cleanup process. Also, remember to check for any notes left by the tagger at this article's talk page.
This article or section should be converted to third person to conform to wiki standards.
Purpose
Here i will show you how to make weapons heal Players, useful for class based mods/games.
Constructor
First of all add this to your weapon constructor.
m_bDropped = 0; // this is to make sure the medic drops it one at a time
Primaryfire
This right here is working code that launches one health vial per single fire.
if (!m_bDropped) { // if it hasnt been dropped...
#ifndef CLIENT_DLL
CBasePlayer *pPlayer = ToBasePlayer( GetOwner() ); // get the player
Vector vecEye = pPlayer->EyePosition(); // Get the eye vector
Vector vForward, vRight; // create 2 new vectors
pPlayer->EyeVectors( &vForward, &vRight, NULL ); // so we know what direction forward is
Vector vecSrc = vecEye + vForward * 18.0f + vRight * 8.0f; // do some crazy math to find the source for our spawning
trace_t tr; // we need a trace
UTIL_TraceHull( vecEye, vecSrc, -Vector(6,6,6), Vector(6,6,6),
pPlayer->PhysicsSolidMaskForEntity(), pPlayer, pPlayer->GetCollisionGroup(), &tr ); // to make sure we can spawn it without it being in anything else
if ( tr.DidHit() ) // if we hit...
{
vecSrc = tr.endpos; // than something happens
}
// CheckThrowPosition( pPlayer, vecEye, vecSrc );
// vForward[0] += 0.1f;
vForward[2] += 0.1f; // something else
Vector vecThrow;
AngleVectors( pPlayer->EyeAngles() + pPlayer->GetPunchAngle(), &vecThrow ); // get the vector fo the impulse on our health vial
// pPlayer->GetVelocity( &vecThrow, NULL );
VectorScale( vecThrow, 1000.0f, vecThrow ); // scale it down
// vecThrow += vForward * 1200;
CBaseEntity *pVial = NULL; // null and then!
pVial = CBaseEntity::Create( "item_healthvial", pPlayer->Weapon_ShootPosition(), QAngle(80,60,0), pPlayer ); create that shit!
pVial->AddSpawnFlags(SF_NORESPAWN); // DONT LET IT RESPAWN!
if (!pVial) // have to have created it for it to be thrown
{
DevMsg("unable to create item\n");
}else // YEA BIZZNATCH
{
DevMsg(2, "Starting item throw (Server)\n");
IPhysicsObject *pPhysicsObject = pVial->VPhysicsGetObject(); // get the physics object
if ( pPhysicsObject )
{
DevMsg(2, "Throwing item (Server)\n");
pPhysicsObject->SetVelocity( &vecThrow, NULL );// throw that fuckin thing.
}
}
m_bDropped = 1; // change the flag...
#endif
} else {
DevMsg(2, "Already dropped one. Release the button. (Server)\n"); // damnit gotta let go of that button.
}
ItemPostFrame
We will add this to the itempostframe in the weapon.
if (!((pOwner->m_nButtons & IN_ATTACK) || (pOwner->m_nButtons & IN_ATTACK2) || (pOwner->m_nButtons & IN_RELOAD)))
{
m_bDropped = 0;
}
Secondary Fire
If a player is too close from the medic/gun will health the player too.
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CWeaponHealer::SecondaryAttack( void )
{
if (CanHealPlayer())
HealPlayer();
}
bool CWeaponHealer::CanHealPlayer( void )
{
if (!m_bFired){
CHL2MP_Player *pOwner = ToHL2MPPlayer( GetOwner() );
if (!pOwner)
{
return false;
}
Vector vecSrc, vecAiming;
// Take the eye position and direction
vecSrc = pOwner->EyePosition();
QAngle angles = pOwner->GetLocalAngles();
AngleVectors( angles, &vecAiming );
trace_t tr;
Vector vecEnd = vecSrc + (vecAiming * 42);
UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pOwner, COLLISION_GROUP_NONE, &tr );
if (tr.fraction < 1.0)
{
// Don\'t attach to a living creature
if (tr.m_pEnt)
{
CBaseEntity *pEntity = tr.m_pEnt;
CBaseCombatCharacter *pBCC = ToBaseCombatCharacter( pEntity );
if (pBCC)
{
if (pBCC->GetHealth()<100)
return true;
}
}
return false;
}
else
{
return false;
}
}else {return false;}
}
bool CWeaponHealer::HealPlayer( void )
{
m_bFired = 1;
CHL2MP_Player *pOwner = ToHL2MPPlayer( GetOwner() );
if (!pOwner)
{
return false;
}
Vector vecSrc, vecAiming;
// Take the eye position and direction
vecSrc = pOwner->EyePosition();
QAngle angles = pOwner->GetLocalAngles();
AngleVectors( angles, &vecAiming );
trace_t tr;
Vector vecEnd = vecSrc + (vecAiming * 42);
UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pOwner, COLLISION_GROUP_NONE, &tr );
if (tr.fraction < 1.0)
{
// Don\'t attach to a living creature
if (tr.m_pEnt)
{
CBaseEntity *pEntity = tr.m_pEnt;
if (pEntity->IsPlayer())
{
if (pEntity->GetHealth()<pEntity->GetMaxHealth())
{
#ifndef CLIENT_DLL
CBasePlayer *pPlayer = ToBasePlayer(pEntity);
CPASAttenuationFilter filter( pPlayer, "HealthVial.Touch" );
EmitSound( filter, pPlayer->entindex(), "HealthVial.Touch" );
pEntity->TakeHealth( 20, DMG_GENERIC );
#endif
return true;
}
}
}
return false;
}
else
{
return false;
}
}