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.

1504 lines
34 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.
*
****/
//=========================================================
// schedule.cpp - functions and data pertaining to the
// monsters' AI scheduling system.
//=========================================================
9 years ago
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "animation.h"
#include "scripted.h"
#include "nodes.h"
#include "defaultai.h"
#include "soundent.h"
extern CGraph WorldGraph;
//=========================================================
// FHaveSchedule - Returns TRUE if monster's m_pSchedule
// is anything other than NULL.
//=========================================================
8 years ago
BOOL CBaseMonster::FHaveSchedule( void )
9 years ago
{
8 years ago
if( m_pSchedule == NULL )
9 years ago
{
return FALSE;
}
return TRUE;
}
//=========================================================
// ClearSchedule - blanks out the caller's schedule pointer
// and index.
//=========================================================
8 years ago
void CBaseMonster::ClearSchedule( void )
9 years ago
{
m_iTaskStatus = TASKSTATUS_NEW;
m_pSchedule = NULL;
m_iScheduleIndex = 0;
}
//=========================================================
// FScheduleDone - Returns TRUE if the caller is on the
// last task in the schedule
//=========================================================
8 years ago
BOOL CBaseMonster::FScheduleDone( void )
9 years ago
{
ASSERT( m_pSchedule != NULL );
8 years ago
if( m_iScheduleIndex == m_pSchedule->cTasks )
9 years ago
{
return TRUE;
}
return FALSE;
}
//=========================================================
// ChangeSchedule - replaces the monster's schedule pointer
// with the passed pointer, and sets the ScheduleIndex back
// to 0
//=========================================================
8 years ago
void CBaseMonster::ChangeSchedule( Schedule_t *pNewSchedule )
9 years ago
{
ASSERT( pNewSchedule != NULL );
8 years ago
m_pSchedule = pNewSchedule;
m_iScheduleIndex = 0;
m_iTaskStatus = TASKSTATUS_NEW;
m_afConditions = 0;// clear all of the conditions
m_failSchedule = SCHED_NONE;
9 years ago
8 years ago
if( m_pSchedule->iInterruptMask & bits_COND_HEAR_SOUND && !(m_pSchedule->iSoundMask) )
9 years ago
{
8 years ago
ALERT( at_aiconsole, "COND_HEAR_SOUND with no sound mask!\n" );
9 years ago
}
8 years ago
else if( m_pSchedule->iSoundMask && !(m_pSchedule->iInterruptMask & bits_COND_HEAR_SOUND) )
9 years ago
{
8 years ago
ALERT( at_aiconsole, "Sound mask without COND_HEAR_SOUND!\n" );
9 years ago
}
#if _DEBUG
8 years ago
if( !ScheduleFromName( pNewSchedule->pName ) )
9 years ago
{
ALERT( at_console, "Schedule %s not in table!!!\n", pNewSchedule->pName );
}
#endif
// this is very useful code if you can isolate a test case in a level with a single monster. It will notify
// you of every schedule selection the monster makes.
#if 0
8 years ago
if( FClassnameIs( pev, "monster_human_grunt" ) )
9 years ago
{
Task_t *pTask = GetTask();
8 years ago
if( pTask )
9 years ago
{
const char *pName = NULL;
8 years ago
if( m_pSchedule )
9 years ago
{
pName = m_pSchedule->pName;
}
else
{
pName = "No Schedule";
}
8 years ago
if( !pName )
9 years ago
{
pName = "Unknown";
}
ALERT( at_aiconsole, "%s: picked schedule %s\n", STRING( pev->classname ), pName );
}
}
#endif// 0
}
//=========================================================
// NextScheduledTask - increments the ScheduleIndex
//=========================================================
8 years ago
void CBaseMonster::NextScheduledTask( void )
9 years ago
{
ASSERT( m_pSchedule != NULL );
m_iTaskStatus = TASKSTATUS_NEW;
m_iScheduleIndex++;
8 years ago
if( FScheduleDone() )
9 years ago
{
// just completed last task in schedule, so make it invalid by clearing it.
SetConditions( bits_COND_SCHEDULE_DONE );
//ClearSchedule();
}
}
//=========================================================
// IScheduleFlags - returns an integer with all Conditions
// bits that are currently set and also set in the current
// schedule's Interrupt mask.
//=========================================================
8 years ago
int CBaseMonster::IScheduleFlags( void )
9 years ago
{
if( !m_pSchedule )
{
return 0;
}
9 years ago
// strip off all bits excepts the ones capable of breaking this schedule.
return m_afConditions & m_pSchedule->iInterruptMask;
}
//=========================================================
// FScheduleValid - returns TRUE as long as the current
// schedule is still the proper schedule to be executing,
// taking into account all conditions
//=========================================================
8 years ago
BOOL CBaseMonster::FScheduleValid( void )
9 years ago
{
8 years ago
if( m_pSchedule == NULL )
9 years ago
{
// schedule is empty, and therefore not valid.
return FALSE;
}
8 years ago
if( HasConditions( m_pSchedule->iInterruptMask | bits_COND_SCHEDULE_DONE | bits_COND_TASK_FAILED ) )
9 years ago
{
#ifdef DEBUG
8 years ago
if( HasConditions( bits_COND_TASK_FAILED ) && m_failSchedule == SCHED_NONE )
9 years ago
{
// fail! Send a visual indicator.
8 years ago
ALERT( at_aiconsole, "Schedule: %s Failed\n", m_pSchedule->pName );
9 years ago
Vector tmp = pev->origin;
tmp.z = pev->absmax.z + 16;
UTIL_Sparks( tmp );
}
#endif // DEBUG
// some condition has interrupted the schedule, or the schedule is done
return FALSE;
}
return TRUE;
}
//=========================================================
// MaintainSchedule - does all the per-think schedule maintenance.
// ensures that the monster leaves this function with a valid
// schedule!
//=========================================================
8 years ago
void CBaseMonster::MaintainSchedule( void )
9 years ago
{
8 years ago
Schedule_t *pNewSchedule;
int i;
9 years ago
// UNDONE: Tune/fix this 10... This is just here so infinite loops are impossible
8 years ago
for( i = 0; i < 10; i++ )
9 years ago
{
8 years ago
if( m_pSchedule != NULL && TaskIsComplete() )
9 years ago
{
NextScheduledTask();
}
// validate existing schedule
8 years ago
if( !FScheduleValid() || m_MonsterState != m_IdealMonsterState )
9 years ago
{
// if we come into this block of code, the schedule is going to have to be changed.
// if the previous schedule was interrupted by a condition, GetIdealState will be
// called. Else, a schedule finished normally.
// Notify the monster that his schedule is changing
ScheduleChange();
// Call GetIdealState if we're not dead and one or more of the following...
// - in COMBAT state with no enemy (it died?)
// - conditions bits (excluding SCHEDULE_DONE) indicate interruption,
// - schedule is done but schedule indicates it wants GetIdealState called
// after successful completion (by setting bits_COND_SCHEDULE_DONE in iInterruptMask)
// DEAD & SCRIPT are not suggestions, they are commands!
8 years ago
if( m_IdealMonsterState != MONSTERSTATE_DEAD &&
( m_IdealMonsterState != MONSTERSTATE_SCRIPT || m_IdealMonsterState == m_MonsterState ) )
9 years ago
{
8 years ago
if( (m_afConditions && !HasConditions( bits_COND_SCHEDULE_DONE ) ) ||
( m_pSchedule && (m_pSchedule->iInterruptMask & bits_COND_SCHEDULE_DONE ) ) ||
( ( m_MonsterState == MONSTERSTATE_COMBAT ) && ( m_hEnemy == NULL ) ) )
9 years ago
{
GetIdealState();
}
}
8 years ago
if( HasConditions( bits_COND_TASK_FAILED ) && m_MonsterState == m_IdealMonsterState )
9 years ago
{
8 years ago
if( m_failSchedule != SCHED_NONE )
9 years ago
pNewSchedule = GetScheduleOfType( m_failSchedule );
else
pNewSchedule = GetScheduleOfType( SCHED_FAIL );
9 years ago
// schedule was invalid because the current task failed to start or complete
8 years ago
ALERT( at_aiconsole, "Schedule Failed at %d!\n", m_iScheduleIndex );
9 years ago
ChangeSchedule( pNewSchedule );
}
else
{
SetState( m_IdealMonsterState );
8 years ago
if( m_MonsterState == MONSTERSTATE_SCRIPT || m_MonsterState == MONSTERSTATE_DEAD )
9 years ago
pNewSchedule = CBaseMonster::GetSchedule();
else
pNewSchedule = GetSchedule();
ChangeSchedule( pNewSchedule );
}
}
8 years ago
if( m_iTaskStatus == TASKSTATUS_NEW )
9 years ago
{
Task_t *pTask = GetTask();
ASSERT( pTask != NULL );
TaskBegin();
StartTask( pTask );
}
// UNDONE: Twice?!!!
8 years ago
if( m_Activity != m_IdealActivity )
9 years ago
{
8 years ago
SetActivity( m_IdealActivity );
9 years ago
}
8 years ago
if( !TaskIsComplete() && m_iTaskStatus != TASKSTATUS_NEW )
9 years ago
break;
}
8 years ago
if( TaskIsRunning() )
9 years ago
{
Task_t *pTask = GetTask();
ASSERT( pTask != NULL );
RunTask( pTask );
}
// UNDONE: We have to do this so that we have an animation set to blend to if RunTask changes the animation
// RunTask() will always change animations at the end of a script!
// Don't do this twice
8 years ago
if( m_Activity != m_IdealActivity )
9 years ago
{
8 years ago
SetActivity( m_IdealActivity );
9 years ago
}
}
//=========================================================
// RunTask
//=========================================================
8 years ago
void CBaseMonster::RunTask( Task_t *pTask )
9 years ago
{
8 years ago
switch( pTask->iTask )
9 years ago
{
case TASK_TURN_RIGHT:
case TASK_TURN_LEFT:
{
ChangeYaw( pev->yaw_speed );
8 years ago
if( FacingIdeal() )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_PLAY_SEQUENCE_FACE_ENEMY:
case TASK_PLAY_SEQUENCE_FACE_TARGET:
{
CBaseEntity *pTarget;
8 years ago
if( pTask->iTask == TASK_PLAY_SEQUENCE_FACE_TARGET )
9 years ago
pTarget = m_hTargetEnt;
else
pTarget = m_hEnemy;
8 years ago
if( pTarget )
9 years ago
{
pev->ideal_yaw = UTIL_VecToYaw( pTarget->pev->origin - pev->origin );
ChangeYaw( pev->yaw_speed );
}
8 years ago
if( m_fSequenceFinished )
9 years ago
TaskComplete();
}
break;
case TASK_PLAY_SEQUENCE:
case TASK_PLAY_ACTIVE_IDLE:
{
8 years ago
if( m_fSequenceFinished )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_FACE_ENEMY:
{
MakeIdealYaw( m_vecEnemyLKP );
ChangeYaw( pev->yaw_speed );
8 years ago
if( FacingIdeal() )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_FACE_HINTNODE:
case TASK_FACE_LASTPOSITION:
case TASK_FACE_TARGET:
case TASK_FACE_IDEAL:
case TASK_FACE_ROUTE:
{
ChangeYaw( pev->yaw_speed );
8 years ago
if( FacingIdeal() )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_WAIT_PVS:
{
8 years ago
if( !FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_WAIT_INDEFINITE:
{
// don't do anything.
break;
}
case TASK_WAIT:
case TASK_WAIT_RANDOM:
{
8 years ago
if( gpGlobals->time >= m_flWaitFinished )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_WAIT_FACE_ENEMY:
{
8 years ago
MakeIdealYaw( m_vecEnemyLKP );
9 years ago
ChangeYaw( pev->yaw_speed );
8 years ago
if( gpGlobals->time >= m_flWaitFinished )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_MOVE_TO_TARGET_RANGE:
{
float distance;
8 years ago
if( m_hTargetEnt == NULL )
9 years ago
TaskFail();
else
{
distance = ( m_vecMoveGoal - pev->origin ).Length2D();
9 years ago
// Re-evaluate when you think your finished, or the target has moved too far
8 years ago
if( ( distance < pTask->flData ) || ( m_vecMoveGoal - m_hTargetEnt->pev->origin ).Length() > pTask->flData * 0.5 )
9 years ago
{
m_vecMoveGoal = m_hTargetEnt->pev->origin;
distance = ( m_vecMoveGoal - pev->origin ).Length2D();
FRefreshRoute();
}
// Set the appropriate activity based on an overlapping range
// overlap the range to prevent oscillation
8 years ago
if( distance < pTask->flData )
9 years ago
{
TaskComplete();
RouteClear(); // Stop moving
}
8 years ago
else if( distance < 190 && m_movementActivity != ACT_WALK )
9 years ago
m_movementActivity = ACT_WALK;
8 years ago
else if( distance >= 270 && m_movementActivity != ACT_RUN )
9 years ago
m_movementActivity = ACT_RUN;
}
break;
}
case TASK_WAIT_FOR_MOVEMENT:
{
8 years ago
if( MovementIsComplete() )
9 years ago
{
TaskComplete();
RouteClear(); // Stop moving
}
break;
}
case TASK_DIE:
{
8 years ago
if( m_fSequenceFinished && pev->frame >= 255 )
9 years ago
{
pev->deadflag = DEAD_DEAD;
9 years ago
SetThink( NULL );
StopAnimation();
8 years ago
if( !BBoxFlat() )
9 years ago
{
// a bit of a hack. If a corpses' bbox is positioned such that being left solid so that it can be attacked will
// block the player on a slope or stairs, the corpse is made nonsolid.
//pev->solid = SOLID_NOT;
8 years ago
UTIL_SetSize( pev, Vector( -4, -4, 0 ), Vector( 4, 4, 1 ) );
9 years ago
}
else // !!!HACKHACK - put monster in a thin, wide bounding box until we fix the solid type/bounding volume problem
8 years ago
UTIL_SetSize( pev, Vector( pev->mins.x, pev->mins.y, pev->mins.z ), Vector( pev->maxs.x, pev->maxs.y, pev->mins.z + 1 ) );
9 years ago
8 years ago
if( ShouldFadeOnDeath() )
9 years ago
{
// this monster was created by a monstermaker... fade the corpse out.
SUB_StartFadeOut();
}
else
{
// body is gonna be around for a while, so have it stink for a bit.
8 years ago
CSoundEnt::InsertSound( bits_SOUND_CARCASS, pev->origin, 384, 30 );
9 years ago
}
}
break;
}
case TASK_RANGE_ATTACK1_NOTURN:
case TASK_MELEE_ATTACK1_NOTURN:
case TASK_MELEE_ATTACK2_NOTURN:
case TASK_RANGE_ATTACK2_NOTURN:
case TASK_RELOAD_NOTURN:
{
8 years ago
if( m_fSequenceFinished )
9 years ago
{
m_Activity = ACT_RESET;
TaskComplete();
}
break;
}
case TASK_RANGE_ATTACK1:
case TASK_MELEE_ATTACK1:
case TASK_MELEE_ATTACK2:
case TASK_RANGE_ATTACK2:
case TASK_SPECIAL_ATTACK1:
case TASK_SPECIAL_ATTACK2:
case TASK_RELOAD:
{
8 years ago
MakeIdealYaw( m_vecEnemyLKP );
ChangeYaw( pev->yaw_speed );
9 years ago
8 years ago
if( m_fSequenceFinished )
9 years ago
{
m_Activity = ACT_RESET;
TaskComplete();
}
break;
}
case TASK_SMALL_FLINCH:
{
8 years ago
if( m_fSequenceFinished )
9 years ago
{
TaskComplete();
}
}
break;
case TASK_WAIT_FOR_SCRIPT:
{
8 years ago
if( m_pCine->m_iDelay <= 0 && gpGlobals->time >= m_pCine->m_startTime )
9 years ago
{
TaskComplete();
m_pCine->StartSequence( (CBaseMonster *)this, m_pCine->m_iszPlay, TRUE );
8 years ago
if( m_fSequenceFinished )
9 years ago
ClearSchedule();
pev->framerate = 1.0;
8 years ago
//ALERT( at_aiconsole, "Script %s has begun for %s\n", STRING( m_pCine->m_iszPlay ), STRING( pev->classname ) );
9 years ago
}
break;
}
case TASK_PLAY_SCRIPT:
{
8 years ago
if( m_fSequenceFinished )
9 years ago
{
m_pCine->SequenceDone( this );
}
break;
}
}
}
//=========================================================
// SetTurnActivity - measures the difference between the way
// the monster is facing and determines whether or not to
// select one of the 180 turn animations.
//=========================================================
8 years ago
void CBaseMonster::SetTurnActivity( void )
9 years ago
{
float flYD;
flYD = FlYawDiff();
8 years ago
if( flYD <= -45 && LookupActivity( ACT_TURN_RIGHT ) != ACTIVITY_NOT_AVAILABLE )
{
// big right turn
9 years ago
m_IdealActivity = ACT_TURN_RIGHT;
}
8 years ago
else if( flYD > 45 && LookupActivity( ACT_TURN_LEFT ) != ACTIVITY_NOT_AVAILABLE )
{
// big left turn
9 years ago
m_IdealActivity = ACT_TURN_LEFT;
}
}
//=========================================================
// Start task - selects the correct activity and performs
// any necessary calculations to start the next task on the
// schedule.
//=========================================================
8 years ago
void CBaseMonster::StartTask( Task_t *pTask )
9 years ago
{
8 years ago
switch( pTask->iTask )
9 years ago
{
case TASK_TURN_RIGHT:
{
float flCurrentYaw;
9 years ago
flCurrentYaw = UTIL_AngleMod( pev->angles.y );
pev->ideal_yaw = UTIL_AngleMod( flCurrentYaw - pTask->flData );
SetTurnActivity();
break;
}
case TASK_TURN_LEFT:
{
float flCurrentYaw;
9 years ago
flCurrentYaw = UTIL_AngleMod( pev->angles.y );
pev->ideal_yaw = UTIL_AngleMod( flCurrentYaw + pTask->flData );
SetTurnActivity();
break;
}
case TASK_REMEMBER:
{
8 years ago
Remember( (int)pTask->flData );
9 years ago
TaskComplete();
break;
}
case TASK_FORGET:
{
8 years ago
Forget( (int)pTask->flData );
9 years ago
TaskComplete();
break;
}
case TASK_FIND_HINTNODE:
{
m_iHintNode = FindHintNode();
8 years ago
if( m_iHintNode != NO_NODE )
9 years ago
{
TaskComplete();
}
else
{
TaskFail();
}
break;
}
case TASK_STORE_LASTPOSITION:
{
m_vecLastPosition = pev->origin;
TaskComplete();
break;
}
case TASK_CLEAR_LASTPOSITION:
{
m_vecLastPosition = g_vecZero;
TaskComplete();
break;
}
case TASK_CLEAR_HINTNODE:
{
m_iHintNode = NO_NODE;
TaskComplete();
break;
}
case TASK_STOP_MOVING:
{
8 years ago
if( m_IdealActivity == m_movementActivity )
9 years ago
{
m_IdealActivity = GetStoppedActivity();
}
RouteClear();
TaskComplete();
break;
}
case TASK_PLAY_SEQUENCE_FACE_ENEMY:
case TASK_PLAY_SEQUENCE_FACE_TARGET:
case TASK_PLAY_SEQUENCE:
{
8 years ago
m_IdealActivity = (Activity)(int)pTask->flData;
9 years ago
break;
}
case TASK_PLAY_ACTIVE_IDLE:
{
// monsters verify that they have a sequence for the node's activity BEFORE
// moving towards the node, so it's ok to just set the activity without checking here.
8 years ago
m_IdealActivity = (Activity)WorldGraph.m_pNodes[m_iHintNode].m_sHintActivity;
9 years ago
break;
}
case TASK_SET_SCHEDULE:
{
Schedule_t *pNewSchedule;
pNewSchedule = GetScheduleOfType( (int)pTask->flData );
8 years ago
if( pNewSchedule )
9 years ago
{
ChangeSchedule( pNewSchedule );
}
else
{
TaskFail();
}
break;
}
case TASK_FIND_NEAR_NODE_COVER_FROM_ENEMY:
{
8 years ago
if( m_hEnemy == NULL )
9 years ago
{
TaskFail();
return;
}
8 years ago
if( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, 0, pTask->flData ) )
9 years ago
{
// try for cover farther than the FLData from the schedule.
TaskComplete();
}
else
{
// no coverwhatsoever.
TaskFail();
}
break;
}
case TASK_FIND_FAR_NODE_COVER_FROM_ENEMY:
{
8 years ago
if( m_hEnemy == NULL )
9 years ago
{
TaskFail();
return;
}
8 years ago
if( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, pTask->flData, CoverRadius() ) )
9 years ago
{
// try for cover farther than the FLData from the schedule.
TaskComplete();
}
else
{
// no coverwhatsoever.
TaskFail();
}
break;
}
case TASK_FIND_NODE_COVER_FROM_ENEMY:
{
8 years ago
if( m_hEnemy == NULL )
9 years ago
{
TaskFail();
return;
}
8 years ago
if( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, 0, CoverRadius() ) )
9 years ago
{
// try for cover farther than the FLData from the schedule.
TaskComplete();
}
else
{
// no coverwhatsoever.
TaskFail();
}
break;
}
case TASK_FIND_COVER_FROM_ENEMY:
{
entvars_t *pevCover;
8 years ago
if( m_hEnemy == NULL )
9 years ago
{
// Find cover from self if no enemy available
pevCover = pev;
//TaskFail();
//return;
9 years ago
}
else
pevCover = m_hEnemy->pev;
8 years ago
if( FindLateralCover( pevCover->origin, pevCover->view_ofs ) )
9 years ago
{
// try lateral first
m_flMoveWaitFinished = gpGlobals->time + pTask->flData;
TaskComplete();
}
8 years ago
else if( FindCover( pevCover->origin, pevCover->view_ofs, 0, CoverRadius() ) )
9 years ago
{
// then try for plain ole cover
m_flMoveWaitFinished = gpGlobals->time + pTask->flData;
TaskComplete();
}
else
{
// no coverwhatsoever.
TaskFail();
}
break;
}
case TASK_FIND_COVER_FROM_ORIGIN:
{
8 years ago
if( FindCover( pev->origin, pev->view_ofs, 0, CoverRadius() ) )
9 years ago
{
// then try for plain ole cover
m_flMoveWaitFinished = gpGlobals->time + pTask->flData;
TaskComplete();
}
else
{
// no cover!
TaskFail();
}
}
break;
case TASK_FIND_COVER_FROM_BEST_SOUND:
{
CSound *pBestSound;
pBestSound = PBestSound();
ASSERT( pBestSound != NULL );
/*
8 years ago
if( pBestSound && FindLateralCover( pBestSound->m_vecOrigin, g_vecZero ) )
9 years ago
{
// try lateral first
m_flMoveWaitFinished = gpGlobals->time + pTask->flData;
TaskComplete();
}
*/
8 years ago
if( pBestSound && FindCover( pBestSound->m_vecOrigin, g_vecZero, pBestSound->m_iVolume, CoverRadius() ) )
9 years ago
{
// then try for plain ole cover
m_flMoveWaitFinished = gpGlobals->time + pTask->flData;
TaskComplete();
}
else
{
// no coverwhatsoever. or no sound in list
TaskFail();
}
break;
}
case TASK_FACE_HINTNODE:
{
8 years ago
pev->ideal_yaw = WorldGraph.m_pNodes[m_iHintNode].m_flHintYaw;
9 years ago
SetTurnActivity();
break;
}
case TASK_FACE_LASTPOSITION:
8 years ago
MakeIdealYaw( m_vecLastPosition );
9 years ago
SetTurnActivity();
break;
case TASK_FACE_TARGET:
8 years ago
if( m_hTargetEnt != NULL )
9 years ago
{
8 years ago
MakeIdealYaw( m_hTargetEnt->pev->origin );
9 years ago
SetTurnActivity();
}
else
TaskFail();
break;
case TASK_FACE_ENEMY:
{
8 years ago
MakeIdealYaw( m_vecEnemyLKP );
9 years ago
SetTurnActivity();
break;
}
case TASK_FACE_IDEAL:
{
SetTurnActivity();
break;
}
case TASK_FACE_ROUTE:
{
8 years ago
if( FRouteClear() )
9 years ago
{
8 years ago
ALERT( at_aiconsole, "No route to face!\n" );
9 years ago
TaskFail();
}
else
{
8 years ago
MakeIdealYaw( m_Route[m_iRouteIndex].vecLocation );
9 years ago
SetTurnActivity();
}
break;
}
case TASK_WAIT_PVS:
case TASK_WAIT_INDEFINITE:
{
// don't do anything.
break;
}
case TASK_WAIT:
case TASK_WAIT_FACE_ENEMY:
{
// set a future time that tells us when the wait is over.
9 years ago
m_flWaitFinished = gpGlobals->time + pTask->flData;
break;
}
case TASK_WAIT_RANDOM:
{
// set a future time that tells us when the wait is over.
9 years ago
m_flWaitFinished = gpGlobals->time + RANDOM_FLOAT( 0.1, pTask->flData );
break;
}
case TASK_MOVE_TO_TARGET_RANGE:
{
8 years ago
if( ( m_hTargetEnt->pev->origin - pev->origin ).Length() < 1 )
9 years ago
TaskComplete();
else
{
m_vecMoveGoal = m_hTargetEnt->pev->origin;
8 years ago
if( !MoveToTarget( ACT_WALK, 2 ) )
9 years ago
TaskFail();
}
break;
}
case TASK_RUN_TO_TARGET:
case TASK_WALK_TO_TARGET:
{
Activity newActivity;
8 years ago
if( ( m_hTargetEnt->pev->origin - pev->origin ).Length() < 1 )
9 years ago
TaskComplete();
else
{
8 years ago
if( pTask->iTask == TASK_WALK_TO_TARGET )
9 years ago
newActivity = ACT_WALK;
else
newActivity = ACT_RUN;
9 years ago
// This monster can't do this!
8 years ago
if( LookupActivity( newActivity ) == ACTIVITY_NOT_AVAILABLE )
9 years ago
TaskComplete();
else
{
8 years ago
if( m_hTargetEnt == NULL || !MoveToTarget( newActivity, 2 ) )
9 years ago
{
TaskFail();
8 years ago
ALERT( at_aiconsole, "%s Failed to reach target!!!\n", STRING( pev->classname ) );
9 years ago
RouteClear();
}
}
}
TaskComplete();
break;
}
case TASK_CLEAR_MOVE_WAIT:
{
m_flMoveWaitFinished = gpGlobals->time;
TaskComplete();
break;
}
case TASK_MELEE_ATTACK1_NOTURN:
case TASK_MELEE_ATTACK1:
{
m_IdealActivity = ACT_MELEE_ATTACK1;
break;
}
case TASK_MELEE_ATTACK2_NOTURN:
case TASK_MELEE_ATTACK2:
{
m_IdealActivity = ACT_MELEE_ATTACK2;
break;
}
case TASK_RANGE_ATTACK1_NOTURN:
case TASK_RANGE_ATTACK1:
{
m_IdealActivity = ACT_RANGE_ATTACK1;
break;
}
case TASK_RANGE_ATTACK2_NOTURN:
case TASK_RANGE_ATTACK2:
{
m_IdealActivity = ACT_RANGE_ATTACK2;
break;
}
case TASK_RELOAD_NOTURN:
case TASK_RELOAD:
{
m_IdealActivity = ACT_RELOAD;
break;
}
case TASK_SPECIAL_ATTACK1:
{
m_IdealActivity = ACT_SPECIAL_ATTACK1;
break;
}
case TASK_SPECIAL_ATTACK2:
{
m_IdealActivity = ACT_SPECIAL_ATTACK2;
break;
}
case TASK_SET_ACTIVITY:
{
m_IdealActivity = (Activity)(int)pTask->flData;
TaskComplete();
break;
}
case TASK_GET_PATH_TO_ENEMY_LKP:
{
8 years ago
if( BuildRoute( m_vecEnemyLKP, bits_MF_TO_LOCATION, NULL ) )
9 years ago
{
TaskComplete();
}
8 years ago
else if( BuildNearestRoute( m_vecEnemyLKP, pev->view_ofs, 0, ( m_vecEnemyLKP - pev->origin ).Length() ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToEnemyLKP failed!!\n" );
9 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_ENEMY:
{
CBaseEntity *pEnemy = m_hEnemy;
8 years ago
if( pEnemy == NULL )
9 years ago
{
TaskFail();
return;
}
8 years ago
if( BuildRoute( pEnemy->pev->origin, bits_MF_TO_ENEMY, pEnemy ) )
9 years ago
{
TaskComplete();
}
8 years ago
else if( BuildNearestRoute( pEnemy->pev->origin, pEnemy->pev->view_ofs, 0, ( pEnemy->pev->origin - pev->origin ).Length() ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToEnemy failed!!\n" );
9 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_ENEMY_CORPSE:
{
UTIL_MakeVectors( pev->angles );
8 years ago
if( BuildRoute( m_vecEnemyLKP - gpGlobals->v_forward * 64, bits_MF_TO_LOCATION, NULL ) )
9 years ago
{
TaskComplete();
}
else
{
8 years ago
ALERT( at_aiconsole, "GetPathToEnemyCorpse failed!!\n" );
9 years ago
TaskFail();
}
}
break;
case TASK_GET_PATH_TO_SPOT:
{
CBaseEntity *pPlayer = CBaseEntity::Instance( FIND_ENTITY_BY_CLASSNAME( NULL, "player" ) );
8 years ago
if( BuildRoute( m_vecMoveGoal, bits_MF_TO_LOCATION, pPlayer ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToSpot failed!!\n" );
9 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_TARGET:
{
RouteClear();
8 years ago
if( m_hTargetEnt != NULL && MoveToTarget( m_movementActivity, 1 ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToSpot failed!!\n" );
9 years ago
TaskFail();
}
break;
}
8 years ago
case TASK_GET_PATH_TO_HINTNODE:
// for active idles!
9 years ago
{
8 years ago
if( MoveToLocation( m_movementActivity, 2, WorldGraph.m_pNodes[m_iHintNode].m_vecOrigin ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToHintNode failed!!\n" );
9 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_LASTPOSITION:
{
m_vecMoveGoal = m_vecLastPosition;
8 years ago
if( MoveToLocation( m_movementActivity, 2, m_vecMoveGoal ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
ALERT ( at_aiconsole, "GetPathToLastPosition failed!!\n" );
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_BESTSOUND:
{
CSound *pSound;
pSound = PBestSound();
8 years ago
if( pSound && MoveToLocation( m_movementActivity, 2, pSound->m_vecOrigin ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
ALERT ( at_aiconsole, "GetPathToBestSound failed!!\n" );
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_BESTSCENT:
9 years ago
{
CSound *pScent;
pScent = PBestScent();
8 years ago
if( pScent && MoveToLocation( m_movementActivity, 2, pScent->m_vecOrigin ) )
9 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
ALERT ( at_aiconsole, "GetPathToBestScent failed!!\n" );
9 years ago
TaskFail();
}
break;
}
case TASK_RUN_PATH:
{
// UNDONE: This is in some default AI and some monsters can't run? -- walk instead?
8 years ago
if( LookupActivity( ACT_RUN ) != ACTIVITY_NOT_AVAILABLE )
9 years ago
{
m_movementActivity = ACT_RUN;
}
else
{
m_movementActivity = ACT_WALK;
}
TaskComplete();
break;
}
case TASK_WALK_PATH:
{
8 years ago
if( pev->movetype == MOVETYPE_FLY )
9 years ago
{
m_movementActivity = ACT_FLY;
}
8 years ago
if( LookupActivity( ACT_WALK ) != ACTIVITY_NOT_AVAILABLE )
9 years ago
{
m_movementActivity = ACT_WALK;
}
else
{
m_movementActivity = ACT_RUN;
}
TaskComplete();
break;
}
case TASK_STRAFE_PATH:
{
8 years ago
Vector2D vec2DirToPoint;
Vector2D vec2RightSide;
9 years ago
// to start strafing, we have to first figure out if the target is on the left side or right side
8 years ago
UTIL_MakeVectors( pev->angles );
9 years ago
8 years ago
vec2DirToPoint = ( m_Route[0].vecLocation - pev->origin ).Make2D().Normalize();
9 years ago
vec2RightSide = gpGlobals->v_right.Make2D().Normalize();
8 years ago
if( DotProduct ( vec2DirToPoint, vec2RightSide ) > 0 )
9 years ago
{
// strafe right
m_movementActivity = ACT_STRAFE_RIGHT;
}
else
{
// strafe left
m_movementActivity = ACT_STRAFE_LEFT;
}
TaskComplete();
break;
}
case TASK_WAIT_FOR_MOVEMENT:
{
8 years ago
if( FRouteClear() )
9 years ago
{
TaskComplete();
}
break;
}
case TASK_EAT:
{
Eat( pTask->flData );
TaskComplete();
break;
}
case TASK_SMALL_FLINCH:
{
m_IdealActivity = GetSmallFlinchActivity();
break;
}
case TASK_DIE:
{
8 years ago
RouteClear();
9 years ago
m_IdealActivity = GetDeathActivity();
pev->deadflag = DEAD_DYING;
break;
}
case TASK_SOUND_WAKE:
{
AlertSound();
TaskComplete();
break;
}
case TASK_SOUND_DIE:
{
DeathSound();
TaskComplete();
break;
}
case TASK_SOUND_IDLE:
{
IdleSound();
TaskComplete();
break;
}
case TASK_SOUND_PAIN:
{
PainSound();
TaskComplete();
break;
}
case TASK_SOUND_DEATH:
{
DeathSound();
TaskComplete();
break;
}
case TASK_SOUND_ANGRY:
{
// sounds are complete as soon as we get here, cause we've already played them.
8 years ago
ALERT( at_aiconsole, "SOUND\n" );
9 years ago
TaskComplete();
break;
}
case TASK_WAIT_FOR_SCRIPT:
{
8 years ago
if( m_pCine->m_iszIdle )
9 years ago
{
m_pCine->StartSequence( (CBaseMonster *)this, m_pCine->m_iszIdle, FALSE );
8 years ago
if( FStrEq( STRING( m_pCine->m_iszIdle ), STRING( m_pCine->m_iszPlay ) ) )
9 years ago
{
pev->framerate = 0;
}
}
else
m_IdealActivity = ACT_IDLE;
break;
}
case TASK_PLAY_SCRIPT:
{
pev->movetype = MOVETYPE_FLY;
8 years ago
ClearBits( pev->flags, FL_ONGROUND );
9 years ago
m_scriptState = SCRIPT_PLAYING;
break;
}
case TASK_ENABLE_SCRIPT:
{
m_pCine->DelayStart( 0 );
TaskComplete();
break;
}
case TASK_PLANT_ON_SCRIPT:
{
8 years ago
if( m_hTargetEnt != NULL )
9 years ago
{
pev->origin = m_hTargetEnt->pev->origin; // Plant on target
}
TaskComplete();
break;
}
case TASK_FACE_SCRIPT:
{
8 years ago
if( m_hTargetEnt != NULL )
9 years ago
{
pev->ideal_yaw = UTIL_AngleMod( m_hTargetEnt->pev->angles.y );
}
TaskComplete();
m_IdealActivity = ACT_IDLE;
RouteClear();
break;
}
case TASK_SUGGEST_STATE:
{
m_IdealMonsterState = (MONSTERSTATE)(int)pTask->flData;
TaskComplete();
break;
}
case TASK_SET_FAIL_SCHEDULE:
m_failSchedule = (int)pTask->flData;
TaskComplete();
break;
case TASK_CLEAR_FAIL_SCHEDULE:
m_failSchedule = SCHED_NONE;
TaskComplete();
break;
default:
{
8 years ago
ALERT( at_aiconsole, "No StartTask entry for %d\n", (SHARED_TASKS)pTask->iTask );
9 years ago
break;
}
}
}
//=========================================================
// GetTask - returns a pointer to the current
// scheduled task. NULL if there's a problem.
//=========================================================
8 years ago
Task_t *CBaseMonster::GetTask( void )
9 years ago
{
8 years ago
if( m_iScheduleIndex < 0 || m_iScheduleIndex >= m_pSchedule->cTasks )
9 years ago
{
// m_iScheduleIndex is not within valid range for the monster's current schedule.
return NULL;
}
else
{
8 years ago
return &m_pSchedule->pTasklist[m_iScheduleIndex];
9 years ago
}
}
//=========================================================
// GetSchedule - Decides which type of schedule best suits
// the monster's current state and conditions. Then calls
// monster's member function to get a pointer to a schedule
// of the proper type.
//=========================================================
8 years ago
Schedule_t *CBaseMonster::GetSchedule( void )
9 years ago
{
8 years ago
switch( m_MonsterState )
9 years ago
{
case MONSTERSTATE_PRONE:
{
return GetScheduleOfType( SCHED_BARNACLE_VICTIM_GRAB );
break;
}
case MONSTERSTATE_NONE:
{
8 years ago
ALERT( at_aiconsole, "MONSTERSTATE IS NONE!\n" );
9 years ago
break;
}
case MONSTERSTATE_IDLE:
{
8 years ago
if( HasConditions( bits_COND_HEAR_SOUND ) )
9 years ago
{
return GetScheduleOfType( SCHED_ALERT_FACE );
}
8 years ago
else if( FRouteClear() )
9 years ago
{
// no valid route!
return GetScheduleOfType( SCHED_IDLE_STAND );
}
else
{
// valid route. Get moving
return GetScheduleOfType( SCHED_IDLE_WALK );
}
break;
}
case MONSTERSTATE_ALERT:
{
8 years ago
if( HasConditions( bits_COND_ENEMY_DEAD ) && LookupActivity( ACT_VICTORY_DANCE ) != ACTIVITY_NOT_AVAILABLE )
9 years ago
{
8 years ago
return GetScheduleOfType( SCHED_VICTORY_DANCE );
9 years ago
}
8 years ago
if( HasConditions( bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE ) )
9 years ago
{
8 years ago
if( fabs( FlYawDiff() ) < ( 1.0 - m_flFieldOfView ) * 60 ) // roughly in the correct direction
9 years ago
{
return GetScheduleOfType( SCHED_TAKE_COVER_FROM_ORIGIN );
}
else
{
return GetScheduleOfType( SCHED_ALERT_SMALL_FLINCH );
}
}
8 years ago
else if( HasConditions ( bits_COND_HEAR_SOUND ) )
9 years ago
{
return GetScheduleOfType( SCHED_ALERT_FACE );
}
else
{
return GetScheduleOfType( SCHED_ALERT_STAND );
}
break;
}
case MONSTERSTATE_COMBAT:
{
8 years ago
if( HasConditions( bits_COND_ENEMY_DEAD ) )
9 years ago
{
// clear the current (dead) enemy and try to find another.
m_hEnemy = NULL;
8 years ago
if( GetEnemy() )
9 years ago
{
ClearConditions( bits_COND_ENEMY_DEAD );
return GetSchedule();
}
else
{
SetState( MONSTERSTATE_ALERT );
return GetSchedule();
}
}
8 years ago
if( HasConditions( bits_COND_NEW_ENEMY ) )
9 years ago
{
8 years ago
return GetScheduleOfType( SCHED_WAKE_ANGRY );
9 years ago
}
8 years ago
else if( HasConditions( bits_COND_LIGHT_DAMAGE ) && !HasMemory( bits_MEMORY_FLINCHED ) )
9 years ago
{
return GetScheduleOfType( SCHED_SMALL_FLINCH );
}
8 years ago
else if( !HasConditions( bits_COND_SEE_ENEMY ) )
9 years ago
{
// we can't see the enemy
8 years ago
if( !HasConditions( bits_COND_ENEMY_OCCLUDED ) )
9 years ago
{
// enemy is unseen, but not occluded!
// turn to face enemy
return GetScheduleOfType( SCHED_COMBAT_FACE );
}
else
{
// chase!
return GetScheduleOfType( SCHED_CHASE_ENEMY );
}
}
else
{
// we can see the enemy
8 years ago
if( HasConditions( bits_COND_CAN_RANGE_ATTACK1 ) )
9 years ago
{
return GetScheduleOfType( SCHED_RANGE_ATTACK1 );
}
8 years ago
if( HasConditions( bits_COND_CAN_RANGE_ATTACK2 ) )
9 years ago
{
return GetScheduleOfType( SCHED_RANGE_ATTACK2 );
}
8 years ago
if( HasConditions( bits_COND_CAN_MELEE_ATTACK1 ) )
9 years ago
{
return GetScheduleOfType( SCHED_MELEE_ATTACK1 );
}
8 years ago
if( HasConditions( bits_COND_CAN_MELEE_ATTACK2 ) )
9 years ago
{
return GetScheduleOfType( SCHED_MELEE_ATTACK2 );
}
8 years ago
if( !HasConditions( bits_COND_CAN_RANGE_ATTACK1 | bits_COND_CAN_MELEE_ATTACK1 ) )
9 years ago
{
// if we can see enemy but can't use either attack type, we must need to get closer to enemy
return GetScheduleOfType( SCHED_CHASE_ENEMY );
}
8 years ago
else if( !FacingIdeal() )
9 years ago
{
//turn
return GetScheduleOfType( SCHED_COMBAT_FACE );
}
else
{
8 years ago
ALERT( at_aiconsole, "No suitable combat schedule!\n" );
9 years ago
}
}
break;
}
case MONSTERSTATE_DEAD:
{
return GetScheduleOfType( SCHED_DIE );
break;
}
case MONSTERSTATE_SCRIPT:
{
ASSERT( m_pCine != NULL );
8 years ago
if( !m_pCine )
9 years ago
{
8 years ago
ALERT( at_aiconsole, "Script failed for %s\n", STRING( pev->classname ) );
9 years ago
CineCleanup();
return GetScheduleOfType( SCHED_IDLE_STAND );
}
return GetScheduleOfType( SCHED_AISCRIPT );
}
default:
{
8 years ago
ALERT( at_aiconsole, "Invalid State for GetSchedule!\n" );
9 years ago
break;
}
}
8 years ago
return &slError[0];
9 years ago
}