Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

553 lines
14 KiB

9 years ago
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* This source code contains proprietary and confidential information of
* Valve LLC and its suppliers. Access to this code is restricted to
* persons who have executed a written SDK license with Valve. Any access,
* use or distribution of this code by or to any unlicensed person is illegal.
*
****/
//=========================================================
// headcrab.cpp - tiny, jumpy alien parasite
//=========================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "schedule.h"
#include "game.h"
//=========================================================
// Monster's Anim Events Go Here
//=========================================================
#define HC_AE_JUMPATTACK ( 2 )
9 years ago
Task_t tlHCRangeAttack1[] =
{
{ TASK_STOP_MOVING, (float)0 },
{ TASK_FACE_IDEAL, (float)0 },
{ TASK_RANGE_ATTACK1, (float)0 },
{ TASK_SET_ACTIVITY, (float)ACT_IDLE },
{ TASK_FACE_IDEAL, (float)0 },
{ TASK_WAIT_RANDOM, (float)0.5 },
9 years ago
};
9 years ago
Schedule_t slHCRangeAttack1[] =
9 years ago
{
9 years ago
{
9 years ago
tlHCRangeAttack1,
9 years ago
ARRAYSIZE( tlHCRangeAttack1 ),
bits_COND_ENEMY_OCCLUDED |
9 years ago
bits_COND_NO_AMMO_LOADED,
0,
"HCRangeAttack1"
},
};
9 years ago
Task_t tlHCRangeAttack1Fast[] =
9 years ago
{
9 years ago
{ TASK_STOP_MOVING, (float)0 },
{ TASK_FACE_IDEAL, (float)0 },
{ TASK_RANGE_ATTACK1, (float)0 },
{ TASK_SET_ACTIVITY, (float)ACT_IDLE },
9 years ago
};
9 years ago
Schedule_t slHCRangeAttack1Fast[] =
9 years ago
{
9 years ago
{
9 years ago
tlHCRangeAttack1Fast,
9 years ago
ARRAYSIZE( tlHCRangeAttack1Fast ),
bits_COND_ENEMY_OCCLUDED |
9 years ago
bits_COND_NO_AMMO_LOADED,
0,
"HCRAFast"
},
};
class CHeadCrab : public CBaseMonster
{
public:
void Spawn( void );
void Precache( void );
void RunTask ( Task_t *pTask );
void StartTask ( Task_t *pTask );
void SetYawSpeed ( void );
void EXPORT LeapTouch ( CBaseEntity *pOther );
Vector Center( void );
Vector BodyTarget( const Vector &posSrc );
void PainSound( void );
void DeathSound( void );
void IdleSound( void );
void AlertSound( void );
void PrescheduleThink( void );
int Classify ( void );
void HandleAnimEvent( MonsterEvent_t *pEvent );
BOOL CheckRangeAttack1 ( float flDot, float flDist );
BOOL CheckRangeAttack2 ( float flDot, float flDist );
int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType );
virtual float GetDamageAmount( void ) { return gSkillData.headcrabDmgBite; }
virtual int GetVoicePitch( void ) { return 100; }
virtual float GetSoundVolue( void ) { return 1.0; }
Schedule_t* GetScheduleOfType ( int Type );
CUSTOM_SCHEDULES
9 years ago
static const char *pIdleSounds[];
static const char *pAlertSounds[];
static const char *pPainSounds[];
static const char *pAttackSounds[];
static const char *pDeathSounds[];
static const char *pBiteSounds[];
};
LINK_ENTITY_TO_CLASS( monster_headcrab, CHeadCrab )
9 years ago
DEFINE_CUSTOM_SCHEDULES( CHeadCrab )
{
slHCRangeAttack1,
slHCRangeAttack1Fast,
};
IMPLEMENT_CUSTOM_SCHEDULES( CHeadCrab, CBaseMonster )
9 years ago
const char *CHeadCrab::pIdleSounds[] =
9 years ago
{
"headcrab/hc_idle1.wav",
"headcrab/hc_idle2.wav",
"headcrab/hc_idle3.wav",
};
const char *CHeadCrab::pAlertSounds[] =
9 years ago
{
"headcrab/hc_alert1.wav",
};
const char *CHeadCrab::pPainSounds[] =
9 years ago
{
"headcrab/hc_pain1.wav",
"headcrab/hc_pain2.wav",
"headcrab/hc_pain3.wav",
};
9 years ago
const char *CHeadCrab::pAttackSounds[] =
9 years ago
{
"headcrab/hc_attack1.wav",
"headcrab/hc_attack2.wav",
"headcrab/hc_attack3.wav",
};
9 years ago
const char *CHeadCrab::pDeathSounds[] =
9 years ago
{
"headcrab/hc_die1.wav",
"headcrab/hc_die2.wav",
};
9 years ago
const char *CHeadCrab::pBiteSounds[] =
9 years ago
{
"headcrab/hc_headbite.wav",
};
//=========================================================
// Classify - indicates this monster's place in the
// relationship table.
//=========================================================
9 years ago
int CHeadCrab::Classify( void )
9 years ago
{
9 years ago
return CLASS_ALIEN_PREY;
9 years ago
}
//=========================================================
// Center - returns the real center of the headcrab. The
// bounding box is much larger than the actual creature so
// this is needed for targeting
//=========================================================
9 years ago
Vector CHeadCrab::Center( void )
9 years ago
{
return Vector( pev->origin.x, pev->origin.y, pev->origin.z + 6 );
}
9 years ago
Vector CHeadCrab::BodyTarget( const Vector &posSrc )
9 years ago
{
9 years ago
return Center();
9 years ago
}
//=========================================================
// SetYawSpeed - allows each sequence to have a different
// turn rate associated with it.
//=========================================================
9 years ago
void CHeadCrab::SetYawSpeed( void )
9 years ago
{
int ys;
9 years ago
switch( m_Activity )
9 years ago
{
9 years ago
case ACT_IDLE:
9 years ago
ys = 30;
break;
9 years ago
case ACT_RUN:
case ACT_WALK:
9 years ago
ys = 20;
break;
case ACT_TURN_LEFT:
case ACT_TURN_RIGHT:
ys = 60;
break;
9 years ago
case ACT_RANGE_ATTACK1:
9 years ago
ys = 30;
break;
default:
ys = 30;
break;
}
pev->yaw_speed = ys;
}
//=========================================================
// HandleAnimEvent - catches the monster-specific messages
// that occur when tagged animation frames are played.
//=========================================================
9 years ago
void CHeadCrab::HandleAnimEvent( MonsterEvent_t *pEvent )
9 years ago
{
switch( pEvent->event )
{
case HC_AE_JUMPATTACK:
{
ClearBits( pev->flags, FL_ONGROUND );
9 years ago
UTIL_SetOrigin( pev, pev->origin + Vector( 0, 0, 1 ) );// take him off ground so engine doesn't instantly reset onground
UTIL_MakeVectors( pev->angles );
9 years ago
Vector vecJumpDir;
if( m_hEnemy != 0 )
9 years ago
{
float gravity = g_psv_gravity->value;
9 years ago
if( gravity <= 1 )
9 years ago
gravity = 1;
// How fast does the headcrab need to travel to reach that height given gravity?
9 years ago
float height = m_hEnemy->pev->origin.z + m_hEnemy->pev->view_ofs.z - pev->origin.z;
if( height < 16 )
9 years ago
height = 16;
float speed = sqrt( 2 * gravity * height );
float time = speed / gravity;
// Scale the sideways velocity to get there at the right time
9 years ago
vecJumpDir = m_hEnemy->pev->origin + m_hEnemy->pev->view_ofs - pev->origin;
9 years ago
vecJumpDir = vecJumpDir * ( 1.0 / time );
// Speed to offset gravity at the desired height
vecJumpDir.z = speed;
// Don't jump too far/fast
float distance = vecJumpDir.Length();
9 years ago
if( distance > 650 )
9 years ago
{
vecJumpDir = vecJumpDir * ( 650.0 / distance );
}
}
else
{
// jump hop, don't care where
vecJumpDir = Vector( gpGlobals->v_forward.x, gpGlobals->v_forward.y, gpGlobals->v_up.z ) * 350;
}
int iSound = RANDOM_LONG(0,2);
9 years ago
if( iSound != 0 )
9 years ago
EMIT_SOUND_DYN( edict(), CHAN_VOICE, pAttackSounds[iSound], GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() );
pev->velocity = vecJumpDir;
m_flNextAttack = gpGlobals->time + 2;
}
break;
default:
CBaseMonster::HandleAnimEvent( pEvent );
break;
}
}
//=========================================================
// Spawn
//=========================================================
9 years ago
void CHeadCrab::Spawn()
9 years ago
{
9 years ago
Precache();
9 years ago
9 years ago
SET_MODEL( ENT( pev ), "models/headcrab.mdl" );
UTIL_SetSize( pev, Vector( -12, -12, 0 ), Vector( 12, 12, 24 ) );
9 years ago
9 years ago
pev->solid = SOLID_SLIDEBOX;
9 years ago
pev->movetype = MOVETYPE_STEP;
m_bloodColor = BLOOD_COLOR_GREEN;
pev->effects = 0;
9 years ago
pev->health = gSkillData.headcrabHealth;
pev->view_ofs = Vector( 0, 0, 20 );// position of the eyes relative to monster's origin.
9 years ago
pev->yaw_speed = 5;//!!! should we put this in the monster's changeanim function since turn rates may vary with state/anim?
m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result )
m_MonsterState = MONSTERSTATE_NONE;
MonsterInit();
}
//=========================================================
// Precache - precaches all resources this monster needs
//=========================================================
9 years ago
void CHeadCrab::Precache()
9 years ago
{
9 years ago
PRECACHE_SOUND_ARRAY( pIdleSounds );
PRECACHE_SOUND_ARRAY( pAlertSounds );
PRECACHE_SOUND_ARRAY( pPainSounds );
PRECACHE_SOUND_ARRAY( pAttackSounds );
PRECACHE_SOUND_ARRAY( pDeathSounds );
PRECACHE_SOUND_ARRAY( pBiteSounds );
9 years ago
9 years ago
PRECACHE_MODEL( "models/headcrab.mdl" );
}
9 years ago
//=========================================================
// RunTask
//=========================================================
9 years ago
void CHeadCrab::RunTask( Task_t *pTask )
9 years ago
{
9 years ago
switch( pTask->iTask )
9 years ago
{
case TASK_RANGE_ATTACK1:
case TASK_RANGE_ATTACK2:
{
9 years ago
if( m_fSequenceFinished )
9 years ago
{
TaskComplete();
SetTouch( NULL );
m_IdealActivity = ACT_IDLE;
}
break;
}
default:
{
9 years ago
CBaseMonster::RunTask( pTask );
9 years ago
}
}
}
//=========================================================
// LeapTouch - this is the headcrab's touch function when it
// is in the air
//=========================================================
9 years ago
void CHeadCrab::LeapTouch( CBaseEntity *pOther )
9 years ago
{
9 years ago
if( !pOther->pev->takedamage )
9 years ago
{
return;
}
9 years ago
if( pOther->Classify() == Classify() )
9 years ago
{
return;
}
// Don't hit if back on ground
9 years ago
if( !FBitSet( pev->flags, FL_ONGROUND ) )
9 years ago
{
9 years ago
EMIT_SOUND_DYN( edict(), CHAN_WEAPON, RANDOM_SOUND_ARRAY( pBiteSounds ), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() );
9 years ago
pOther->TakeDamage( pev, pev, GetDamageAmount(), DMG_SLASH );
}
SetTouch( NULL );
}
//=========================================================
// PrescheduleThink
//=========================================================
9 years ago
void CHeadCrab::PrescheduleThink( void )
9 years ago
{
// make the crab coo a little bit in combat state
9 years ago
if( m_MonsterState == MONSTERSTATE_COMBAT && RANDOM_FLOAT( 0, 5 ) < 0.1 )
9 years ago
{
IdleSound();
}
}
9 years ago
void CHeadCrab::StartTask( Task_t *pTask )
9 years ago
{
m_iTaskStatus = TASKSTATUS_RUNNING;
9 years ago
switch( pTask->iTask )
9 years ago
{
case TASK_RANGE_ATTACK1:
{
EMIT_SOUND_DYN( edict(), CHAN_WEAPON, pAttackSounds[0], GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() );
m_IdealActivity = ACT_RANGE_ATTACK1;
SetTouch( &CHeadCrab::LeapTouch );
break;
}
default:
{
9 years ago
CBaseMonster::StartTask( pTask );
9 years ago
}
}
}
//=========================================================
// CheckRangeAttack1
//=========================================================
9 years ago
BOOL CHeadCrab::CheckRangeAttack1( float flDot, float flDist )
9 years ago
{
9 years ago
if( FBitSet( pev->flags, FL_ONGROUND ) && flDist <= 256 && flDot >= 0.65 )
9 years ago
{
return TRUE;
}
return FALSE;
}
//=========================================================
// CheckRangeAttack2
//=========================================================
9 years ago
BOOL CHeadCrab::CheckRangeAttack2( float flDot, float flDist )
9 years ago
{
return FALSE;
// BUGBUG: Why is this code here? There is no ACT_RANGE_ATTACK2 animation. I've disabled it for now.
#if 0
9 years ago
if( FBitSet( pev->flags, FL_ONGROUND ) && flDist > 64 && flDist <= 256 && flDot >= 0.5 )
9 years ago
{
return TRUE;
}
return FALSE;
#endif
}
9 years ago
int CHeadCrab::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
9 years ago
{
// Don't take any acid damage -- BigMomma's mortar is acid
9 years ago
if( bitsDamageType & DMG_ACID )
9 years ago
flDamage = 0;
return CBaseMonster::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType );
}
9 years ago
#define CRAB_ATTN_IDLE (float)1.5
9 years ago
//=========================================================
// IdleSound
//=========================================================
9 years ago
void CHeadCrab::IdleSound( void )
9 years ago
{
9 years ago
EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY( pIdleSounds ), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() );
9 years ago
}
//=========================================================
// AlertSound
//=========================================================
9 years ago
void CHeadCrab::AlertSound( void )
9 years ago
{
9 years ago
EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY( pAlertSounds ), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() );
9 years ago
}
//=========================================================
// AlertSound
//=========================================================
9 years ago
void CHeadCrab::PainSound( void )
9 years ago
{
9 years ago
EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY( pPainSounds ), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() );
9 years ago
}
//=========================================================
// DeathSound
//=========================================================
9 years ago
void CHeadCrab::DeathSound( void )
9 years ago
{
9 years ago
EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY( pDeathSounds ), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() );
9 years ago
}
9 years ago
Schedule_t *CHeadCrab::GetScheduleOfType( int Type )
9 years ago
{
9 years ago
switch( Type )
9 years ago
{
case SCHED_RANGE_ATTACK1:
{
9 years ago
return &slHCRangeAttack1[0];
9 years ago
}
break;
}
return CBaseMonster::GetScheduleOfType( Type );
}
class CBabyCrab : public CHeadCrab
{
public:
void Spawn( void );
void Precache( void );
9 years ago
void SetYawSpeed( void );
9 years ago
float GetDamageAmount( void ) { return gSkillData.headcrabDmgBite * 0.3; }
9 years ago
BOOL CheckRangeAttack1( float flDot, float flDist );
Schedule_t *GetScheduleOfType ( int Type );
virtual int GetVoicePitch( void ) { return PITCH_NORM + RANDOM_LONG( 40, 50 ); }
9 years ago
virtual float GetSoundVolue( void ) { return 0.8; }
};
LINK_ENTITY_TO_CLASS( monster_babycrab, CBabyCrab )
9 years ago
9 years ago
void CBabyCrab::Spawn( void )
9 years ago
{
CHeadCrab::Spawn();
9 years ago
SET_MODEL( ENT( pev ), "models/baby_headcrab.mdl" );
9 years ago
pev->rendermode = kRenderTransTexture;
pev->renderamt = 192;
9 years ago
UTIL_SetSize( pev, Vector( -12, -12, 0 ), Vector( 12, 12, 24 ) );
9 years ago
9 years ago
pev->health = gSkillData.headcrabHealth * 0.25; // less health than full grown
9 years ago
}
9 years ago
void CBabyCrab::Precache( void )
9 years ago
{
PRECACHE_MODEL( "models/baby_headcrab.mdl" );
CHeadCrab::Precache();
}
9 years ago
void CBabyCrab::SetYawSpeed( void )
9 years ago
{
pev->yaw_speed = 120;
}
9 years ago
BOOL CBabyCrab::CheckRangeAttack1( float flDot, float flDist )
9 years ago
{
9 years ago
if( pev->flags & FL_ONGROUND )
9 years ago
{
9 years ago
if( pev->groundentity && ( pev->groundentity->v.flags & ( FL_CLIENT | FL_MONSTER ) ) )
9 years ago
return TRUE;
// A little less accurate, but jump from closer
9 years ago
if( flDist <= 180 && flDot >= 0.55 )
9 years ago
return TRUE;
}
return FALSE;
}
9 years ago
Schedule_t *CBabyCrab::GetScheduleOfType( int Type )
9 years ago
{
switch( Type )
{
case SCHED_FAIL: // If you fail, try to jump!
if( m_hEnemy != 0 )
9 years ago
return slHCRangeAttack1Fast;
break;
case SCHED_RANGE_ATTACK1:
{
return slHCRangeAttack1Fast;
}
break;
}
return CHeadCrab::GetScheduleOfType( Type );
}