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.

1618 lines
37 KiB

7 years ago
/***
*
9 years ago
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
7 years ago
*
* 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.
//=========================================================
7 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 )
7 years ago
{
8 years ago
if( m_pSchedule == NULL )
7 years ago
{
return FALSE;
}
return TRUE;
}
//=========================================================
// ClearSchedule - blanks out the caller's schedule pointer
// and index.
//=========================================================
8 years ago
void CBaseMonster::ClearSchedule( void )
7 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 )
7 years ago
{
ASSERT( m_pSchedule != NULL );
8 years ago
if( m_iScheduleIndex == m_pSchedule->cTasks )
7 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 )
7 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;
7 years ago
8 years ago
if( m_pSchedule->iInterruptMask & bits_COND_HEAR_SOUND && !(m_pSchedule->iSoundMask) )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "COND_HEAR_SOUND with no sound mask!\n" );
7 years ago
}
8 years ago
else if( m_pSchedule->iSoundMask && !(m_pSchedule->iInterruptMask & bits_COND_HEAR_SOUND) )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "Sound mask without COND_HEAR_SOUND!\n" );
7 years ago
}
#if _DEBUG
8 years ago
if( !ScheduleFromName( pNewSchedule->pName ) )
7 years ago
{
9 years ago
ALERT( at_console, "Schedule %s not in table!!!\n", pNewSchedule->pName );
7 years ago
}
#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" ) )
7 years ago
{
Task_t *pTask = GetTask();
8 years ago
if( pTask )
7 years ago
{
const char *pName = NULL;
8 years ago
if( m_pSchedule )
7 years ago
{
pName = m_pSchedule->pName;
}
else
{
pName = "No Schedule";
}
8 years ago
if( !pName )
7 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 )
7 years ago
{
ASSERT( m_pSchedule != NULL );
m_iTaskStatus = TASKSTATUS_NEW;
m_iScheduleIndex++;
8 years ago
if( FScheduleDone() )
7 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 )
7 years ago
{
if( !m_pSchedule )
{
return 0;
}
7 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 )
7 years ago
{
8 years ago
if( m_pSchedule == NULL )
7 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 ) )
7 years ago
{
#ifdef DEBUG
8 years ago
if( HasConditions( bits_COND_TASK_FAILED ) && m_failSchedule == SCHED_NONE )
7 years ago
{
// fail! Send a visual indicator.
8 years ago
ALERT( at_aiconsole, "Schedule: %s Failed\n", m_pSchedule->pName );
9 years ago
7 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 )
7 years ago
{
8 years ago
Schedule_t *pNewSchedule;
int i;
7 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++ )
7 years ago
{
8 years ago
if( m_pSchedule != NULL && TaskIsComplete() )
7 years ago
{
NextScheduledTask();
}
// validate existing schedule
8 years ago
if( !FScheduleValid() || m_MonsterState != m_IdealMonsterState )
7 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 ) )
7 years ago
{
// if we're here, then either we're being told to do something (besides dying or playing a script)
// or our current schedule (besides dying) is invalid. -- LRC
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 == 0 ) ) )
7 years ago
{
GetIdealState();
}
}
8 years ago
if( HasConditions( bits_COND_TASK_FAILED ) && m_MonsterState == m_IdealMonsterState )
7 years ago
{
8 years ago
if( m_failSchedule != SCHED_NONE )
7 years ago
pNewSchedule = GetScheduleOfType( m_failSchedule );
else
pNewSchedule = GetScheduleOfType( SCHED_FAIL );
7 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 );
7 years ago
ChangeSchedule( pNewSchedule );
}
else
{
SetState( m_IdealMonsterState );
8 years ago
if( m_MonsterState == MONSTERSTATE_SCRIPT || m_MonsterState == MONSTERSTATE_DEAD )
7 years ago
pNewSchedule = CBaseMonster::GetSchedule();
else
pNewSchedule = GetSchedule();
ChangeSchedule( pNewSchedule );
}
}
8 years ago
if( m_iTaskStatus == TASKSTATUS_NEW )
7 years ago
{
Task_t *pTask = GetTask();
ASSERT( pTask != NULL );
TaskBegin();
StartTask( pTask );
}
// UNDONE: Twice?!!!
8 years ago
if( m_Activity != m_IdealActivity )
7 years ago
{
8 years ago
SetActivity( m_IdealActivity );
7 years ago
}
8 years ago
if( !TaskIsComplete() && m_iTaskStatus != TASKSTATUS_NEW )
7 years ago
break;
}
8 years ago
if( TaskIsRunning() )
7 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 )
7 years ago
{
8 years ago
SetActivity( m_IdealActivity );
7 years ago
}
}
//=========================================================
// RunTask
//=========================================================
8 years ago
void CBaseMonster::RunTask( Task_t *pTask )
7 years ago
{
8 years ago
switch( pTask->iTask )
7 years ago
{
case TASK_TURN_RIGHT:
case TASK_TURN_LEFT:
{
ChangeYaw( pev->yaw_speed );
8 years ago
if( FacingIdeal() )
7 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 )
7 years ago
pTarget = m_hTargetEnt;
else
pTarget = m_hEnemy;
8 years ago
if( pTarget )
7 years ago
{
pev->ideal_yaw = UTIL_VecToYaw( pTarget->pev->origin - pev->origin );
ChangeYaw( pev->yaw_speed );
}
8 years ago
if( m_fSequenceFinished )
7 years ago
TaskComplete();
}
break;
case TASK_PLAY_SEQUENCE:
case TASK_PLAY_ACTIVE_IDLE:
{
8 years ago
if( m_fSequenceFinished )
7 years ago
{
TaskComplete();
}
break;
}
case TASK_FACE_ENEMY:
{
MakeIdealYaw( m_vecEnemyLKP );
ChangeYaw( pev->yaw_speed );
8 years ago
if( FacingIdeal() )
7 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() )
7 years ago
{
TaskComplete();
}
break;
}
case TASK_WAIT_PVS:
{
8 years ago
if( !FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) )
7 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 )
7 years ago
{
TaskComplete();
}
break;
}
case TASK_WAIT_FACE_ENEMY:
{
8 years ago
MakeIdealYaw( m_vecEnemyLKP );
7 years ago
ChangeYaw( pev->yaw_speed );
8 years ago
if( gpGlobals->time >= m_flWaitFinished )
7 years ago
{
TaskComplete();
}
break;
}
case TASK_MOVE_TO_TARGET_RANGE:
{
float distance;
if( m_hTargetEnt == 0 )
7 years ago
TaskFail();
else
{
distance = ( m_vecMoveGoal - pev->origin ).Length2D();
7 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 )
7 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 )
7 years ago
{
TaskComplete();
RouteClear(); // Stop moving
}
8 years ago
else if( distance < 190 && m_movementActivity != ACT_WALK )
7 years ago
m_movementActivity = ACT_WALK;
8 years ago
else if( distance >= 270 && m_movementActivity != ACT_RUN )
7 years ago
m_movementActivity = ACT_RUN;
}
break;
}
case TASK_WAIT_FOR_MOVEMENT:
{
8 years ago
if( MovementIsComplete() )
7 years ago
{
TaskComplete();
RouteClear(); // Stop moving
}
break;
}
case TASK_DIE:
{
8 years ago
if( m_fSequenceFinished && pev->frame >= 255 )
7 years ago
{
pev->deadflag = DEAD_DEAD;
9 years ago
SetThink( NULL );
7 years ago
StopAnimation();
8 years ago
if( !BBoxFlat() )
7 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 ) );
7 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 ) );
7 years ago
8 years ago
if( ShouldFadeOnDeath() )
7 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 );
7 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 )
7 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 );
7 years ago
8 years ago
if( m_fSequenceFinished )
7 years ago
{
m_Activity = ACT_RESET;
TaskComplete();
}
break;
}
case TASK_SMALL_FLINCH:
{
8 years ago
if( m_fSequenceFinished )
7 years ago
{
TaskComplete();
}
}
break;
case TASK_WAIT_FOR_SCRIPT:
{
8 years ago
if( m_pCine->m_iDelay <= 0 && gpGlobals->time >= m_pCine->m_startTime )
7 years ago
{
TaskComplete();
}
break;
}
case TASK_PLAY_SCRIPT:
{
8 years ago
if( m_fSequenceFinished )
7 years ago
{
ALERT( at_console, "Anim Finished\n" );
if( m_pCine->m_iRepeatsLeft > 0 )
{
//ALERT( at_console, "Frame %f; Repeat %d from %f\n", pev->frame, m_pCine->m_iRepeatsLeft, m_pCine->m_fRepeatFrame );
m_pCine->m_iRepeatsLeft--;
pev->frame = m_pCine->m_fRepeatFrame;
ResetSequenceInfo();
}
else
{
TaskComplete();
}
7 years ago
}
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 )
7 years ago
{
float flYD;
flYD = FlYawDiff();
8 years ago
if( flYD <= -45 && LookupActivity( ACT_TURN_RIGHT ) != ACTIVITY_NOT_AVAILABLE )
{
// big right turn
7 years ago
m_IdealActivity = ACT_TURN_RIGHT;
}
8 years ago
else if( flYD > 45 && LookupActivity( ACT_TURN_LEFT ) != ACTIVITY_NOT_AVAILABLE )
{
// big left turn
7 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 )
7 years ago
{
8 years ago
switch( pTask->iTask )
7 years ago
{
case TASK_TURN_RIGHT:
{
float flCurrentYaw;
7 years ago
flCurrentYaw = UTIL_AngleMod( pev->angles.y );
pev->ideal_yaw = UTIL_AngleMod( flCurrentYaw - pTask->flData );
SetTurnActivity();
break;
}
case TASK_TURN_LEFT:
{
float flCurrentYaw;
7 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 );
7 years ago
TaskComplete();
break;
}
case TASK_FORGET:
{
8 years ago
Forget( (int)pTask->flData );
7 years ago
TaskComplete();
break;
}
case TASK_FIND_HINTNODE:
{
m_iHintNode = FindHintNode();
8 years ago
if( m_iHintNode != NO_NODE )
7 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 )
7 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;
7 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;
7 years ago
break;
}
case TASK_SET_SCHEDULE:
{
Schedule_t *pNewSchedule;
pNewSchedule = GetScheduleOfType( (int)pTask->flData );
8 years ago
if( pNewSchedule )
7 years ago
{
ChangeSchedule( pNewSchedule );
}
else
{
TaskFail();
}
break;
}
case TASK_FIND_NEAR_NODE_COVER_FROM_ENEMY:
{
if( m_hEnemy == 0 )
7 years ago
{
TaskFail();
return;
}
8 years ago
if( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, 0, pTask->flData ) )
7 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:
{
if( m_hEnemy == 0 )
7 years ago
{
TaskFail();
return;
}
8 years ago
if( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, pTask->flData, CoverRadius() ) )
7 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:
{
if( m_hEnemy == 0 )
7 years ago
{
TaskFail();
return;
}
8 years ago
if( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, 0, CoverRadius() ) )
7 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;
if( m_hEnemy == 0 )
7 years ago
{
// Find cover from self if no enemy available
pevCover = pev;
//TaskFail();
//return;
7 years ago
}
else
pevCover = m_hEnemy->pev;
8 years ago
if( FindLateralCover( pevCover->origin, pevCover->view_ofs ) )
7 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() ) )
7 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() ) )
7 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 ) )
7 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() ) )
7 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;
7 years ago
SetTurnActivity();
break;
}
case TASK_FACE_LASTPOSITION:
8 years ago
MakeIdealYaw( m_vecLastPosition );
7 years ago
SetTurnActivity();
break;
case TASK_FACE_TARGET:
if( m_hTargetEnt != 0 )
7 years ago
{
8 years ago
MakeIdealYaw( m_hTargetEnt->pev->origin );
7 years ago
SetTurnActivity();
}
else
TaskFail();
break;
case TASK_FACE_ENEMY:
{
8 years ago
MakeIdealYaw( m_vecEnemyLKP );
7 years ago
SetTurnActivity();
break;
}
case TASK_FACE_IDEAL:
{
SetTurnActivity();
break;
}
case TASK_FACE_ROUTE:
{
8 years ago
if( FRouteClear() )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "No route to face!\n" );
7 years ago
TaskFail();
}
else
{
8 years ago
MakeIdealYaw( m_Route[m_iRouteIndex].vecLocation );
7 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.
7 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.
7 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 )
7 years ago
TaskComplete();
else
{
m_vecMoveGoal = m_hTargetEnt->pev->origin;
8 years ago
if( !MoveToTarget( ACT_WALK, 2 ) )
7 years ago
TaskFail();
}
break;
}
case TASK_RUN_TO_SCRIPT:
case TASK_WALK_TO_SCRIPT:
{
Activity newActivity;
if( !m_pGoalEnt || ( m_pGoalEnt->pev->origin - pev->origin ).Length() < 1 )
7 years ago
TaskComplete();
else
{
if( pTask->iTask == TASK_WALK_TO_SCRIPT )
7 years ago
newActivity = ACT_WALK;
else
newActivity = ACT_RUN;
7 years ago
// This monster can't do this!
8 years ago
if( LookupActivity( newActivity ) == ACTIVITY_NOT_AVAILABLE )
7 years ago
TaskComplete();
else
{
if( m_pGoalEnt != NULL )
7 years ago
{
Vector vecDest;
vecDest = m_pGoalEnt->pev->origin;
if( !MoveToLocation( newActivity, 2, vecDest ) )
{
TaskFail();
ALERT( at_aiconsole, "%s Failed to reach script!!!\n", STRING( pev->classname ) );
RouteClear();
}
7 years ago
}
else
{
TaskFail();
ALERT( at_aiconsole, "%s: MoveTarget is missing!?!\n", STRING( pev->classname ) );
RouteClear();
}
7 years ago
}
}
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 ) )
7 years ago
{
TaskComplete();
}
8 years ago
else if( BuildNearestRoute( m_vecEnemyLKP, pev->view_ofs, 0, ( m_vecEnemyLKP - pev->origin ).Length() ) )
7 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToEnemyLKP failed!!\n" );
7 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_ENEMY:
{
CBaseEntity *pEnemy = m_hEnemy;
8 years ago
if( pEnemy == NULL )
7 years ago
{
TaskFail();
return;
}
8 years ago
if( BuildRoute( pEnemy->pev->origin, bits_MF_TO_ENEMY, pEnemy ) )
7 years ago
{
TaskComplete();
}
8 years ago
else if( BuildNearestRoute( pEnemy->pev->origin, pEnemy->pev->view_ofs, 0, ( pEnemy->pev->origin - pev->origin ).Length() ) )
7 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToEnemy failed!!\n" );
7 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 ) )
7 years ago
{
TaskComplete();
}
else
{
8 years ago
ALERT( at_aiconsole, "GetPathToEnemyCorpse failed!!\n" );
7 years ago
TaskFail();
}
}
break;
case TASK_GET_PATH_TO_SPOT:
{
CBaseEntity *pPlayer = UTIL_FindEntityByClassname( 0, "player" );
8 years ago
if( BuildRoute( m_vecMoveGoal, bits_MF_TO_LOCATION, pPlayer ) )
7 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToSpot failed!!\n" );
7 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_TARGET:
{
RouteClear();
if( m_hTargetEnt != 0 && MoveToTarget( m_movementActivity, 1 ) )
7 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToSpot failed!!\n" );
7 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_SCRIPT:
{
RouteClear();
if( m_pCine != 0 && MoveToLocation( m_movementActivity, 1, m_pCine->pev->origin ) )
{
TaskComplete();
}
else
{
// no way to get there =(
ALERT( at_aiconsole, "GetPathToSpot failed!!\n" );
TaskFail();
}
break;
}
8 years ago
case TASK_GET_PATH_TO_HINTNODE:
// for active idles!
7 years ago
{
8 years ago
if( MoveToLocation( m_movementActivity, 2, WorldGraph.m_pNodes[m_iHintNode].m_vecOrigin ) )
7 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
8 years ago
ALERT( at_aiconsole, "GetPathToHintNode failed!!\n" );
7 years ago
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_LASTPOSITION:
{
m_vecMoveGoal = m_vecLastPosition;
8 years ago
if( MoveToLocation( m_movementActivity, 2, m_vecMoveGoal ) )
7 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 ) )
7 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
ALERT ( at_aiconsole, "GetPathToBestSound failed!!\n" );
TaskFail();
}
break;
}
case TASK_GET_PATH_TO_BESTSCENT:
7 years ago
{
CSound *pScent;
pScent = PBestScent();
8 years ago
if( pScent && MoveToLocation( m_movementActivity, 2, pScent->m_vecOrigin ) )
7 years ago
{
TaskComplete();
}
else
{
// no way to get there =(
ALERT ( at_aiconsole, "GetPathToBestScent failed!!\n" );
7 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 )
7 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 )
7 years ago
{
m_movementActivity = ACT_FLY;
}
8 years ago
if( LookupActivity( ACT_WALK ) != ACTIVITY_NOT_AVAILABLE )
7 years ago
{
m_movementActivity = ACT_WALK;
}
else
{
m_movementActivity = ACT_RUN;
}
TaskComplete();
break;
}
case TASK_STRAFE_PATH:
{
8 years ago
Vector2D vec2DirToPoint;
Vector2D vec2RightSide;
7 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 );
7 years ago
8 years ago
vec2DirToPoint = ( m_Route[0].vecLocation - pev->origin ).Make2D().Normalize();
7 years ago
vec2RightSide = gpGlobals->v_right.Make2D().Normalize();
8 years ago
if( DotProduct ( vec2DirToPoint, vec2RightSide ) > 0 )
7 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() )
7 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();
7 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" );
7 years ago
TaskComplete();
break;
}
case TASK_WAIT_FOR_SCRIPT:
{
if( m_pCine->m_iDelay <= 0 && gpGlobals->time >= m_pCine->m_startTime )
7 years ago
{
TaskComplete(); //LRC - start playing immediately
}
else if( !m_pCine->IsAction() && m_pCine->m_iszIdle )
7 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 ) ) )
7 years ago
{
pev->framerate = 0;
}
}
else
m_IdealActivity = ACT_IDLE;
break;
}
case TASK_PLAY_SCRIPT:
{
if( m_pCine->IsAction() )
{
// ALERT( at_console, "PlayScript: setting idealactivity %d\n",m_pCine->m_fAction );
switch( m_pCine->m_fAction )
{
case 0:
m_IdealActivity = ACT_RANGE_ATTACK1;
break;
case 1:
m_IdealActivity = ACT_RANGE_ATTACK2;
break;
case 2:
m_IdealActivity = ACT_MELEE_ATTACK1;
break;
case 3:
m_IdealActivity = ACT_MELEE_ATTACK2;
break;
case 4:
m_IdealActivity = ACT_SPECIAL_ATTACK1;
break;
case 5:
m_IdealActivity = ACT_SPECIAL_ATTACK2;
break;
case 6:
m_IdealActivity = ACT_RELOAD;
break;
case 7:
m_IdealActivity = ACT_HOP;
break;
}
pev->framerate = 1.0; // shouldn't be needed, but just in case
pev->movetype = MOVETYPE_FLY;
ClearBits( pev->flags, FL_ONGROUND );
}
else
{
m_pCine->StartSequence( (CBaseMonster *)this, m_pCine->m_iszPlay, TRUE );
if( m_fSequenceFinished )
ClearSchedule();
pev->framerate = 1.0;
//ALERT( at_aiconsole, "Script %s has begun for %s\n", STRING( m_pCine->m_iszPlay ), STRING( pev->classname ) );
}
m_scriptState = SCRIPT_PLAYING;
break;
7 years ago
}
//LRC
case TASK_END_SCRIPT:
7 years ago
{
m_pCine->SequenceDone( this );
7 years ago
TaskComplete();
break;
}
9 years ago
case TASK_ENABLE_SCRIPT:
7 years ago
{
9 years ago
m_pCine->DelayStart( 0 );
7 years ago
TaskComplete();
break;
}
case TASK_PLANT_ON_SCRIPT:
{
if ( m_pCine != NULL )
{
// Plant on script
// LRC - if it's a teleport script, do the turn too
if (m_pCine->m_fMoveTo == 4 || m_pCine->m_fMoveTo == 6)
{
7 years ago
if (m_pCine->m_fTurnType == 0) //LRC
pev->angles.y = m_hTargetEnt->pev->angles.y;
else if (m_pCine->m_fTurnType == 1)
pev->angles.y = UTIL_VecToYaw(m_hTargetEnt->pev->origin - pev->origin);
pev->ideal_yaw = pev->angles.y;
pev->avelocity = Vector( 0, 0, 0 );
pev->velocity = Vector( 0, 0, 0 );
pev->effects |= EF_NOINTERP;
}
if (m_pCine->m_fMoveTo != 6)
pev->origin = m_pGoalEnt->pev->origin;
}
TaskComplete();
break;
}
case TASK_FACE_SCRIPT:
{
if ( m_pCine != NULL && m_pCine->m_fMoveTo != 0) // movetype "no move" makes us ignore turntype
{
switch (m_pCine->m_fTurnType)
{
7 years ago
case 0:
pev->ideal_yaw = UTIL_AngleMod( m_pCine->pev->angles.y );
break;
case 1:
// yes, this is inconsistent- turn to face uses the "target" and turn to angle uses the "cine".
if (m_hTargetEnt)
MakeIdealYaw ( m_hTargetEnt->pev->origin );
else
MakeIdealYaw ( m_pCine->pev->origin );
break;
// default: don't turn
}
}
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 );
7 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 )
7 years ago
{
8 years ago
if( m_iScheduleIndex < 0 || m_iScheduleIndex >= m_pSchedule->cTasks )
7 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];
7 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 )
7 years ago
{
8 years ago
switch( m_MonsterState )
7 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" );
7 years ago
break;
}
case MONSTERSTATE_IDLE:
{
8 years ago
if( HasConditions( bits_COND_HEAR_SOUND ) )
7 years ago
{
return GetScheduleOfType( SCHED_ALERT_FACE );
}
8 years ago
else if( FRouteClear() )
7 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 )
7 years ago
{
8 years ago
return GetScheduleOfType( SCHED_VICTORY_DANCE );
7 years ago
}
8 years ago
if( HasConditions( bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE ) )
7 years ago
{
8 years ago
if( fabs( FlYawDiff() ) < ( 1.0 - m_flFieldOfView ) * 60 ) // roughly in the correct direction
7 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 ) )
7 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 ) )
7 years ago
{
// clear the current (dead) enemy and try to find another.
m_hEnemy = NULL;
8 years ago
if( GetEnemy() )
7 years ago
{
ClearConditions( bits_COND_ENEMY_DEAD );
return GetSchedule();
}
else
{
SetState( MONSTERSTATE_ALERT );
return GetSchedule();
}
}
8 years ago
if( HasConditions( bits_COND_NEW_ENEMY ) )
7 years ago
{
8 years ago
return GetScheduleOfType( SCHED_WAKE_ANGRY );
7 years ago
}
8 years ago
else if( HasConditions( bits_COND_LIGHT_DAMAGE ) && !HasMemory( bits_MEMORY_FLINCHED ) )
7 years ago
{
return GetScheduleOfType( SCHED_SMALL_FLINCH );
}
8 years ago
else if( !HasConditions( bits_COND_SEE_ENEMY ) )
7 years ago
{
// we can't see the enemy
8 years ago
if( !HasConditions( bits_COND_ENEMY_OCCLUDED ) )
7 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 ) )
7 years ago
{
return GetScheduleOfType( SCHED_RANGE_ATTACK1 );
}
8 years ago
if( HasConditions( bits_COND_CAN_RANGE_ATTACK2 ) )
7 years ago
{
return GetScheduleOfType( SCHED_RANGE_ATTACK2 );
}
8 years ago
if( HasConditions( bits_COND_CAN_MELEE_ATTACK1 ) )
7 years ago
{
return GetScheduleOfType( SCHED_MELEE_ATTACK1 );
}
8 years ago
if( HasConditions( bits_COND_CAN_MELEE_ATTACK2 ) )
7 years ago
{
return GetScheduleOfType( SCHED_MELEE_ATTACK2 );
}
8 years ago
if( !HasConditions( bits_COND_CAN_RANGE_ATTACK1 | bits_COND_CAN_MELEE_ATTACK1 ) )
7 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() )
7 years ago
{
//turn
return GetScheduleOfType( SCHED_COMBAT_FACE );
}
else
{
8 years ago
ALERT( at_aiconsole, "No suitable combat schedule!\n" );
7 years ago
}
}
break;
}
case MONSTERSTATE_DEAD:
{
return GetScheduleOfType( SCHED_DIE );
break;
}
case MONSTERSTATE_SCRIPT:
{
ASSERT( m_pCine != NULL );
8 years ago
if( !m_pCine )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "Script failed for %s\n", STRING( pev->classname ) );
7 years ago
CineCleanup();
return GetScheduleOfType( SCHED_IDLE_STAND );
}
return GetScheduleOfType( SCHED_AISCRIPT );
}
default:
{
8 years ago
ALERT( at_aiconsole, "Invalid State for GetSchedule!\n" );
7 years ago
break;
}
}
8 years ago
return &slError[0];
7 years ago
}