//=========================================================
// Opposing Forces Weapon knife (standart knife)
//
// Made by Demiurge
//
//FGD weapon_knife
//
//Примечания DIM@N
//=========================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "player.h"
#include "gamerules.h"
#define KNIFE_BODYHIT_VOLUME 128
#define KNIFE_WALLHIT_VOLUME 512
LINK_ENTITY_TO_CLASS( weapon_knife, CKnife ); //Создаём энетю
enum knife_e //Загружаем анимации
{
KNIFE_IDLE = 0,
KNIFE_DRAW,
KNIFE_HOLSTER,
KNIFE_ATTACK1HIT,
KNIFE_ATTACK1MISS,
KNIFE_ATTACK2MISS,
KNIFE_ATTACK2HIT,
KNIFE_ATTACK3MISS,
KNIFE_ATT ACK3HIT
};
void CKnife::Spawn( )
{
Precache( );
m_iId = WEAPON_KNIFE; //Имя энтити, которое внесем в fgd файл
SET_MODEL(ENT(pev), "models/w_knife.mdl");//Модель нашего ножа на карте
m_iClip = -1;
FallInit();
}
void CKnife::Precache( void )
{
//Загружаем модели и звуки
PRECACHE_MODEL("models/v_knife.mdl");//Модель у нас в руках
PRECACHE_MODEL("models/w_knife.mdl");//Модель на карте
PRECACHE_MODEL("models/p_knife.mdl");//Модель в руках у игрока и мобов
PRECACHE_SOUND("weapons/knife_hit_wall1.wav");
PRECACHE_SOUND("weapons/knife_hit_wall2.wav");
PRECACHE_SOUND("weapons/knife_hit_flesh1.wav");
PRECACHE_SOUND("weapons/knife_hit_flesh2.wav");
PRECACHE_SOUND("weapons/knife1.wav");
PRECACHE_SOUND("weapons/knife2.wav");
PRECACHE_SOUND("weapons/knife3.wav");
m_usKnife = PRECACHE_EVENT ( 1, "events/knife.sc" );
}
int CKnife::GetItemInfo(ItemInfo *p)
{
p->pszName = STRING(pev->classname);
p->pszAmmo1 = NULL;
p->iMaxAmmo1 = -1;
p->pszAmmo2 = NULL;
p->iMaxAmmo2 = -1;
p->iMaxClip = WEAPON_NOCLIP;
p->iSlot = 0;//Слот в худе, начиная с 0. В данном случае 1
p->iPosition = 1; //Позиция в слоте худа, начиная с 0. В данном случае 2
p->iId = WEAPON_KNIFE;
p->iWeight = CROWBAR_WEIGHT;
return 1;
}
//Остальное можно понять только со знанием языка С++
BOOL CKnife::Deploy( )
{
return DefaultDeploy( "models/v_knife.mdl", "models/p_knife.mdl", KNIFE_DRAW, "knife" );
}
void CKnife::Holster( int skiplocal /* = 0 */ )
{
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.5;
SendWeaponAnim( KNIFE_HOLSTER );
}
void FindHullIntersection2( const Vector &vecSrc, TraceResult &tr, float *mins, float *maxs, edict_t *pEntity )
{
int i, j, k;
float distance;
float *minmaxs[2] = {mins, maxs};
TraceResult tmpTrace;
Vector vecHullEnd = tr.vecEndPos;
Vector vecEnd;
distance = 1e6f;
vecHullEnd = vecSrc + ((vecHullEnd - vecSrc)*2);
UTIL_TraceLine( vecSrc, vecHullEnd, dont_ignore_monsters, pEntity, &tmpTrace );
if ( tmpTrace.flFraction < 1.0 )
{
tr = tmpTrace;
return;
}
for ( i = 0; i < 2; i++ )
{
for ( j = 0; j < 2; j++ )
{
for ( k = 0; k < 2; k++ )
{
vecEnd.x = vecHullEnd.x + minmaxs[l][0];
vecEnd.y = vecHullEnd.y + minmaxs[j][1];
vecEnd.z = vecHullEnd.z + minmaxs[k][2];
UTIL_TraceLine( vecSrc, vecEnd, dont_ignore_monsters, pEntity, &tmpTrace );
if ( tmpTrace.flFraction < 1.0 )
{
float thisDistance = (tmpTrace.vecEndPos - vecSrc).Length();
if ( thisDistance < distance )
{
tr = tmpTrace;
distance = thisDistance;
}
}
}
}
}
}
void CKnife::PrimaryAttack()
{
if (! Swing( 1 ))
{
SetThink( SwingAgain );
pev->nextthink = gpGlobals->time + 0.1;
}
}
void CKnife::Smack( )
{
DecalGunshot( &m_trHit, BULLET_PLAYER_CROWBAR );
}
void CKnife::SwingAgain( void )
{
Swing( 0 );
}
int CKnife::Swing( int fFirst )
{
int fDidHit = FALSE;
float flVol = 1.0;
TraceResult tr;
UTIL_MakeVectors (m_pPlayer->pev->v_angle);
Vector vecSrc = m_pPlayer->GetGunPosition( );
Vector vecEnd = vecSrc + gpGlobals->v_forward * 32;
UTIL_TraceLine( vecSrc, vecEnd, dont_ignore_monsters, ENT( m_pPlayer->pev ), &tr );
#ifndef CLIENT_DLL
if ( tr.flFraction >= 1.0 )
{
UTIL_TraceHull( vecSrc, vecEnd, dont_ignore_monsters, head_hull, ENT( m_pPlayer->pev ), &tr );
if ( tr.flFraction < 1.0 )
{
CBaseEntity *pHit = CBaseEntity::Instance( tr.pHit );
if ( !pHit || pHit->IsBSPModel() )
FindHullIntersection2( vecSrc, tr, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, m_pPlayer->edict() );
vecEnd = tr.vecEndPos;
}
}
#endif
if ( tr.flFraction >= 1.0 )
{
if (fFirst)
{
switch((m_iSwing++) % 3 )
{
case 0:
SendWeaponAnim( KNIFE_ATTACK1MISS ); break;
case 1:
SendWeaponAnim( KNIFE_ATTACK2MISS ); break;
case 2:
SendWeaponAnim( KNIFE_ATTACK3MISS ); break;
}
m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.5;
switch( RANDOM_LONG(0,2) )
{
case 0:
EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "weapons/knife1.wav", 1, ATTN_NORM);
break;
case 1:
EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "weapons/knife2.wav", 1, ATTN_NORM);
break;
default:
EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "weapons/knife3.wav", 1, ATTN_NORM);
break;
}
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
}
}
else
{
#ifndef CLIENT_DLL
fDidHit = TRUE;
CBaseEntity *pEntity = CBaseEntity::Instance(tr.pHit);
switch( ((m_iSwing++) % 2) + 1 )
{
case 0:
SendWeaponAnim( KNIFE_ATTACK1HIT ); break;
case 1:
SendWeaponAnim( KNIFE_ATTACK2HIT ); break;
case 2:
SendWeaponAnim( KNIFE_ATTACK3HIT ); break;
}
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
ClearMultiDamage( );
if ( (m_flNextPrimaryAttack + 1 < gpGlobals->time) || g_pGameRules->IsMultiplayer())
{
pEntity->TraceAttack(m_pPlayer->pev, gSkillData.plrDmgCrowbar, gpGlobals->v_forward, &tr, DMG_CLUB );
}
else
{
pEntity->TraceAttack(m_pPlayer->pev, gSkillData.plrDmgCrowbar / 2, gpGlobals->v_forward, &tr, DMG_CLUB );
}
ApplyMultiDamage( m_pPlayer->pev, m_pPlayer->pev );
m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.25;
int fHitWorld = TRUE;
if (pEntity)
{
if ( pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE )
{
switch( RANDOM_LONG(0,1) )
{
case 0:
EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/knife_hit_flesh1.wav", 1, ATTN_NORM); break;
case 1:
EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/knife_hit_flesh2.wav", 1, ATTN_NORM); break;
}
m_pPlayer->m_iWeaponVolume = KNIFE_BODYHIT_VOLUME;
if ( !pEntity->IsAlive() )
return TRUE;
else
flVol = 0.1;
fHitWorld = FALSE;
}
}
if (fHitWorld)
{
float fvolbar = TEXTURETYPE_PlaySound(&tr, vecSrc, vecSrc + (vecEnd-vecSrc)*2, BULLET_PLAYER_CROWBAR);
if ( g_pGameRules->IsMultiplayer() )
{
fvolbar = 1;
}
switch( RANDOM_LONG(0,1) )
{
case 0:
EMIT_SOUND_DYN(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/knife_hit_wall1.wav", fvolbar, ATTN_NORM, 0, 98 + RANDOM_LONG(0,3));
break;
case 1:
EMIT_SOUND_DYN(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/knife_hit_wall2.wav", fvolbar, ATTN_NORM, 0, 98 + RANDOM_LONG(0,3));
break;
}
}
m_trHit = tr;
SetThink( Smack );
pev->nextthink = UTIL_WeaponTimeBase() + 0.2;
m_pPlayer->m_iWeaponVolume = flVol * KNIFE_WALLHIT_VOLUME;
#endif
}
return fDidHit;
}