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.

614 lines
16 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.
*
****/
//=========================================================
// Squadmonster functions
//=========================================================
9 years ago
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "nodes.h"
#include "monsters.h"
#include "animation.h"
#include "saverestore.h"
#include "squadmonster.h"
#include "plane.h"
//=========================================================
// Save/Restore
//=========================================================
TYPEDESCRIPTION CSquadMonster::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CSquadMonster, m_hSquadLeader, FIELD_EHANDLE ),
DEFINE_ARRAY( CSquadMonster, m_hSquadMember, FIELD_EHANDLE, MAX_SQUAD_MEMBERS - 1 ),
// DEFINE_FIELD( CSquadMonster, m_afSquadSlots, FIELD_INTEGER ), // these need to be reset after transitions!
DEFINE_FIELD( CSquadMonster, m_fEnemyEluded, FIELD_BOOLEAN ),
DEFINE_FIELD( CSquadMonster, m_flLastEnemySightTime, FIELD_TIME ),
DEFINE_FIELD( CSquadMonster, m_iMySlot, FIELD_INTEGER ),
};
IMPLEMENT_SAVERESTORE( CSquadMonster, CBaseMonster )
9 years ago
//=========================================================
// OccupySlot - if any slots of the passed slots are
// available, the monster will be assigned to one.
//=========================================================
9 years ago
BOOL CSquadMonster::OccupySlot( int iDesiredSlots )
9 years ago
{
int i;
int iMask;
int iSquadSlots;
9 years ago
if( !InSquad() )
9 years ago
{
return TRUE;
}
9 years ago
if( SquadEnemySplit() )
9 years ago
{
// if the squad members aren't all fighting the same enemy, slots are disabled
// so that a squad member doesn't get stranded unable to engage his enemy because
// all of the attack slots are taken by squad members fighting other enemies.
m_iMySlot = bits_SLOT_SQUAD_SPLIT;
return TRUE;
}
CSquadMonster *pSquadLeader = MySquadLeader();
9 years ago
if( !( iDesiredSlots ^ pSquadLeader->m_afSquadSlots ) )
9 years ago
{
// none of the desired slots are available.
return FALSE;
}
iSquadSlots = pSquadLeader->m_afSquadSlots;
9 years ago
for( i = 0; i < NUM_SLOTS; i++ )
9 years ago
{
9 years ago
iMask = 1 << i;
if( iDesiredSlots & iMask ) // am I looking for this bit?
9 years ago
{
9 years ago
if( !( iSquadSlots & iMask ) ) // Is it already taken?
9 years ago
{
// No, use this bit
pSquadLeader->m_afSquadSlots |= iMask;
m_iMySlot = iMask;
9 years ago
//ALERT( at_aiconsole, "Took slot %d - %d\n", i, m_hSquadLeader->m_afSquadSlots );
9 years ago
return TRUE;
}
}
}
return FALSE;
}
//=========================================================
9 years ago
// VacateSlot
9 years ago
//=========================================================
9 years ago
void CSquadMonster::VacateSlot()
9 years ago
{
9 years ago
if( m_iMySlot != bits_NO_SLOT && InSquad() )
9 years ago
{
9 years ago
//ALERT( at_aiconsole, "Vacated Slot %d - %d\n", m_iMySlot, m_hSquadLeader->m_afSquadSlots );
9 years ago
MySquadLeader()->m_afSquadSlots &= ~m_iMySlot;
m_iMySlot = bits_NO_SLOT;
}
}
//=========================================================
// ScheduleChange
//=========================================================
9 years ago
void CSquadMonster::ScheduleChange ( void )
9 years ago
{
VacateSlot();
}
//=========================================================
// Killed
//=========================================================
9 years ago
void CSquadMonster::Killed( entvars_t *pevAttacker, int iGib )
9 years ago
{
VacateSlot();
9 years ago
if( InSquad() )
9 years ago
{
MySquadLeader()->SquadRemove( this );
}
9 years ago
CBaseMonster::Killed( pevAttacker, iGib );
9 years ago
}
// These functions are still awaiting conversion to CSquadMonster
//=========================================================
//
// SquadRemove(), remove pRemove from my squad.
// If I am pRemove, promote m_pSquadNext to leader
//
//=========================================================
9 years ago
void CSquadMonster::SquadRemove( CSquadMonster *pRemove )
9 years ago
{
ASSERT( pRemove!=NULL );
ASSERT( this->IsLeader() );
ASSERT( pRemove->m_hSquadLeader == this );
// If I'm the leader, get rid of my squad
9 years ago
if( pRemove == MySquadLeader() )
9 years ago
{
9 years ago
for( int i = 0; i < MAX_SQUAD_MEMBERS - 1; i++ )
9 years ago
{
9 years ago
CSquadMonster *pMember = MySquadMember( i );
if( pMember )
9 years ago
{
pMember->m_hSquadLeader = NULL;
m_hSquadMember[i] = NULL;
}
}
}
else
{
CSquadMonster *pSquadLeader = MySquadLeader();
9 years ago
if( pSquadLeader )
9 years ago
{
9 years ago
for( int i = 0; i < MAX_SQUAD_MEMBERS - 1; i++ )
9 years ago
{
9 years ago
if( pSquadLeader->m_hSquadMember[i] == this )
9 years ago
{
pSquadLeader->m_hSquadMember[i] = NULL;
break;
}
}
}
}
pRemove->m_hSquadLeader = NULL;
}
//=========================================================
//
// SquadAdd(), add pAdd to my squad
//
//=========================================================
9 years ago
BOOL CSquadMonster::SquadAdd( CSquadMonster *pAdd )
9 years ago
{
9 years ago
ASSERT( pAdd != NULL );
9 years ago
ASSERT( !pAdd->InSquad() );
ASSERT( this->IsLeader() );
9 years ago
for( int i = 0; i < MAX_SQUAD_MEMBERS - 1; i++ )
9 years ago
{
if( m_hSquadMember[i] == 0 )
9 years ago
{
m_hSquadMember[i] = pAdd;
pAdd->m_hSquadLeader = this;
return TRUE;
}
}
return FALSE;
// should complain here
}
//=========================================================
//
// SquadPasteEnemyInfo - called by squad members that have
// current info on the enemy so that it can be stored for
// members who don't have current info.
//
//=========================================================
9 years ago
void CSquadMonster::SquadPasteEnemyInfo( void )
9 years ago
{
9 years ago
CSquadMonster *pSquadLeader = MySquadLeader();
if( pSquadLeader )
9 years ago
pSquadLeader->m_vecEnemyLKP = m_vecEnemyLKP;
}
//=========================================================
//
// SquadCopyEnemyInfo - called by squad members who don't
// have current info on the enemy. Reads from the same fields
// in the leader's data that other squad members write to,
// so the most recent data is always available here.
//
//=========================================================
9 years ago
void CSquadMonster::SquadCopyEnemyInfo( void )
9 years ago
{
9 years ago
CSquadMonster *pSquadLeader = MySquadLeader();
if( pSquadLeader )
9 years ago
m_vecEnemyLKP = pSquadLeader->m_vecEnemyLKP;
}
//=========================================================
//
// SquadMakeEnemy - makes everyone in the squad angry at
// the same entity.
//
//=========================================================
9 years ago
void CSquadMonster::SquadMakeEnemy( CBaseEntity *pEnemy )
9 years ago
{
9 years ago
if( !InSquad() )
9 years ago
return;
9 years ago
if( !pEnemy )
9 years ago
{
9 years ago
ALERT( at_console, "ERROR: SquadMakeEnemy() - pEnemy is NULL!\n" );
9 years ago
return;
}
9 years ago
CSquadMonster *pSquadLeader = MySquadLeader();
for( int i = 0; i < MAX_SQUAD_MEMBERS; i++ )
9 years ago
{
9 years ago
CSquadMonster *pMember = pSquadLeader->MySquadMember( i );
if( pMember )
9 years ago
{
// reset members who aren't activly engaged in fighting
9 years ago
if( pMember->m_hEnemy != pEnemy && !pMember->HasConditions( bits_COND_SEE_ENEMY ) )
9 years ago
{
if( pMember->m_hEnemy != 0 )
9 years ago
{
// remember their current enemy
pMember->PushEnemy( pMember->m_hEnemy, pMember->m_vecEnemyLKP );
}
// give them a new enemy
pMember->m_hEnemy = pEnemy;
pMember->m_vecEnemyLKP = pEnemy->pev->origin;
9 years ago
pMember->SetConditions( bits_COND_NEW_ENEMY );
9 years ago
}
}
}
}
//=========================================================
//
// SquadCount(), return the number of members of this squad
// callable from leaders & followers
//
//=========================================================
9 years ago
int CSquadMonster::SquadCount( void )
9 years ago
{
9 years ago
if( !InSquad() )
9 years ago
return 0;
CSquadMonster *pSquadLeader = MySquadLeader();
int squadCount = 0;
9 years ago
for( int i = 0; i < MAX_SQUAD_MEMBERS; i++ )
9 years ago
{
9 years ago
if( pSquadLeader->MySquadMember( i ) != NULL )
9 years ago
squadCount++;
}
return squadCount;
}
//=========================================================
//
// SquadRecruit(), get some monsters of my classification and
// link them as a group. returns the group size
//
//=========================================================
9 years ago
int CSquadMonster::SquadRecruit( int searchRadius, int maxMembers )
9 years ago
{
int squadCount;
int iMyClass = Classify();// cache this monster's class
// Don't recruit if I'm already in a group
9 years ago
if( InSquad() )
9 years ago
return 0;
9 years ago
if( maxMembers < 2 )
9 years ago
return 0;
// I am my own leader
m_hSquadLeader = this;
squadCount = 1;
CBaseEntity *pEntity = NULL;
9 years ago
if( !FStringNull( pev->netname ) )
9 years ago
{
// I have a netname, so unconditionally recruit everyone else with that name.
pEntity = UTIL_FindEntityByString( pEntity, "netname", STRING( pev->netname ) );
9 years ago
while( pEntity )
9 years ago
{
CSquadMonster *pRecruit = pEntity->MySquadMonsterPointer();
9 years ago
if( pRecruit )
9 years ago
{
9 years ago
if( !pRecruit->InSquad() && pRecruit->Classify() == iMyClass && pRecruit != this )
9 years ago
{
// minimum protection here against user error.in worldcraft.
9 years ago
if( !SquadAdd( pRecruit ) )
9 years ago
break;
squadCount++;
}
}
9 years ago
pEntity = UTIL_FindEntityByString( pEntity, "netname", STRING( pev->netname ) );
}
}
else
{
9 years ago
while( ( pEntity = UTIL_FindEntityInSphere( pEntity, pev->origin, searchRadius ) ) != NULL )
9 years ago
{
9 years ago
CSquadMonster *pRecruit = pEntity->MySquadMonsterPointer();
9 years ago
9 years ago
if( pRecruit && pRecruit != this && pRecruit->IsAlive() && !pRecruit->m_pCine )
9 years ago
{
// Can we recruit this guy?
9 years ago
if( !pRecruit->InSquad() && pRecruit->Classify() == iMyClass &&
( ( iMyClass != CLASS_ALIEN_MONSTER ) || FStrEq( STRING( pev->classname ), STRING( pRecruit->pev->classname ) ) ) &&
9 years ago
FStringNull( pRecruit->pev->netname ) )
{
TraceResult tr;
UTIL_TraceLine( pev->origin + pev->view_ofs, pRecruit->pev->origin + pev->view_ofs, ignore_monsters, pRecruit->edict(), &tr );// try to hit recruit with a traceline.
if( tr.flFraction == 1.0f )
9 years ago
{
9 years ago
if( !SquadAdd( pRecruit ) )
9 years ago
break;
squadCount++;
}
}
}
}
}
// no single member squads
9 years ago
if( squadCount == 1 )
9 years ago
{
m_hSquadLeader = NULL;
}
return squadCount;
}
//=========================================================
// CheckEnemy
//=========================================================
9 years ago
int CSquadMonster::CheckEnemy( CBaseEntity *pEnemy )
9 years ago
{
int iUpdatedLKP;
9 years ago
iUpdatedLKP = CBaseMonster::CheckEnemy( m_hEnemy );
9 years ago
// communicate with squad members about the enemy IF this individual has the same enemy as the squad leader.
9 years ago
if( InSquad() && (CBaseEntity *)m_hEnemy == MySquadLeader()->m_hEnemy )
9 years ago
{
9 years ago
if( iUpdatedLKP )
9 years ago
{
// have new enemy information, so paste to the squad.
SquadPasteEnemyInfo();
}
else
{
// enemy unseen, copy from the squad knowledge.
SquadCopyEnemyInfo();
}
}
return iUpdatedLKP;
}
//=========================================================
// StartMonster
//=========================================================
9 years ago
void CSquadMonster::StartMonster( void )
9 years ago
{
9 years ago
CBaseMonster::StartMonster();
9 years ago
9 years ago
if( ( m_afCapability & bits_CAP_SQUAD ) && !InSquad() )
9 years ago
{
9 years ago
if( !FStringNull( pev->netname ) )
9 years ago
{
// if I have a groupname, I can only recruit if I'm flagged as leader
9 years ago
if( !( pev->spawnflags & SF_SQUADMONSTER_LEADER ) )
9 years ago
{
return;
}
}
// try to form squads now.
int iSquadSize = SquadRecruit( 1024, 4 );
9 years ago
if( iSquadSize )
9 years ago
{
9 years ago
ALERT( at_aiconsole, "Squad of %d %s formed\n", iSquadSize, STRING( pev->classname ) );
9 years ago
}
9 years ago
if( IsLeader() && FClassnameIs( pev, "monster_human_grunt" ) )
9 years ago
{
SetBodygroup( 1, 1 ); // UNDONE: truly ugly hack
pev->skin = 0;
}
}
}
//=========================================================
// NoFriendlyFire - checks for possibility of friendly fire
//
// Builds a large box in front of the grunt and checks to see
// if any squad members are in that box.
//=========================================================
9 years ago
BOOL CSquadMonster::NoFriendlyFire( void )
9 years ago
{
9 years ago
if( !InSquad() )
9 years ago
{
return TRUE;
}
9 years ago
CPlane backPlane;
CPlane leftPlane;
CPlane rightPlane;
9 years ago
9 years ago
Vector vecLeftSide;
Vector vecRightSide;
Vector v_left;
Vector v_dir;
9 years ago
//!!!BUGBUG - to fix this, the planes must be aligned to where the monster will be firing its gun, not the direction it is facing!!!
if( m_hEnemy != 0 )
9 years ago
{
9 years ago
UTIL_MakeVectors( UTIL_VecToAngles( m_hEnemy->Center() - pev->origin ) );
9 years ago
}
else
{
// if there's no enemy, pretend there's a friendly in the way, so the grunt won't shoot.
return FALSE;
}
9 years ago
//UTIL_MakeVectors( pev->angles );
// vecLeftSide = pev->origin - ( gpGlobals->v_right * ( pev->size.x * 1.5f ) );
// vecRightSide = pev->origin + ( gpGlobals->v_right * ( pev->size.x * 1.5f ) );
v_dir = gpGlobals->v_right * ( pev->size.x * 1.5f );
vecLeftSide = pev->origin - v_dir;
vecRightSide = pev->origin + v_dir;
v_left = gpGlobals->v_right * -1.0f;
9 years ago
9 years ago
leftPlane.InitializePlane( gpGlobals->v_right, vecLeftSide );
rightPlane.InitializePlane( v_left, vecRightSide );
backPlane.InitializePlane( gpGlobals->v_forward, pev->origin );
9 years ago
/*
9 years ago
ALERT( at_console, "LeftPlane: %f %f %f : %f\n", leftPlane.m_vecNormal.x, leftPlane.m_vecNormal.y, leftPlane.m_vecNormal.z, leftPlane.m_flDist );
ALERT( at_console, "RightPlane: %f %f %f : %f\n", rightPlane.m_vecNormal.x, rightPlane.m_vecNormal.y, rightPlane.m_vecNormal.z, rightPlane.m_flDist );
ALERT( at_console, "BackPlane: %f %f %f : %f\n", backPlane.m_vecNormal.x, backPlane.m_vecNormal.y, backPlane.m_vecNormal.z, backPlane.m_flDist );
9 years ago
*/
CSquadMonster *pSquadLeader = MySquadLeader();
9 years ago
for( int i = 0; i < MAX_SQUAD_MEMBERS; i++ )
9 years ago
{
9 years ago
CSquadMonster *pMember = pSquadLeader->MySquadMember( i );
if( pMember && pMember != this )
9 years ago
{
9 years ago
if( backPlane.PointInFront( pMember->pev->origin ) &&
leftPlane.PointInFront( pMember->pev->origin ) &&
rightPlane.PointInFront( pMember->pev->origin ) )
9 years ago
{
// this guy is in the check volume! Don't shoot!
return FALSE;
}
}
}
return TRUE;
}
//=========================================================
// GetIdealState - surveys the Conditions information available
// and finds the best new state for a monster.
//=========================================================
9 years ago
MONSTERSTATE CSquadMonster::GetIdealState ( void )
9 years ago
{
IScheduleFlags();
9 years ago
// If no schedule conditions, the new ideal state is probably the reason we're in here.
9 years ago
switch( m_MonsterState )
9 years ago
{
case MONSTERSTATE_IDLE:
case MONSTERSTATE_ALERT:
9 years ago
if( HasConditions( bits_COND_NEW_ENEMY ) && InSquad() )
9 years ago
{
9 years ago
SquadMakeEnemy( m_hEnemy );
9 years ago
}
break;
default:
break;
9 years ago
}
9 years ago
return CBaseMonster::GetIdealState();
9 years ago
}
//=========================================================
// FValidateCover - determines whether or not the chosen
// cover location is a good one to move to. (currently based
// on proximity to others in the squad)
//=========================================================
9 years ago
BOOL CSquadMonster::FValidateCover( const Vector &vecCoverLocation )
9 years ago
{
9 years ago
if( !InSquad() )
9 years ago
{
return TRUE;
}
9 years ago
if( SquadMemberInRange( vecCoverLocation, 128 ) )
9 years ago
{
// another squad member is too close to this piece of cover.
return FALSE;
}
return TRUE;
}
//=========================================================
// SquadEnemySplit- returns TRUE if not all squad members
// are fighting the same enemy.
//=========================================================
9 years ago
BOOL CSquadMonster::SquadEnemySplit( void )
9 years ago
{
9 years ago
if( !InSquad() )
9 years ago
return FALSE;
9 years ago
CSquadMonster *pSquadLeader = MySquadLeader();
CBaseEntity *pEnemy = pSquadLeader->m_hEnemy;
9 years ago
9 years ago
for( int i = 0; i < MAX_SQUAD_MEMBERS; i++ )
9 years ago
{
9 years ago
CSquadMonster *pMember = pSquadLeader->MySquadMember( i );
if( pMember != NULL && pMember->m_hEnemy != 0 && pMember->m_hEnemy != pEnemy )
9 years ago
{
return TRUE;
}
}
return FALSE;
}
//=========================================================
// FValidateCover - determines whether or not the chosen
// cover location is a good one to move to. (currently based
// on proximity to others in the squad)
//=========================================================
9 years ago
BOOL CSquadMonster::SquadMemberInRange( const Vector &vecLocation, float flDist )
9 years ago
{
9 years ago
if( !InSquad() )
9 years ago
return FALSE;
CSquadMonster *pSquadLeader = MySquadLeader();
9 years ago
for( int i = 0; i < MAX_SQUAD_MEMBERS; i++ )
9 years ago
{
9 years ago
CSquadMonster *pSquadMember = pSquadLeader->MySquadMember( i );
if( pSquadMember && ( vecLocation - pSquadMember->pev->origin ).Length2D() <= flDist )
9 years ago
return TRUE;
}
return FALSE;
}
9 years ago
extern Schedule_t slChaseEnemyFailed[];
9 years ago
Schedule_t *CSquadMonster::GetScheduleOfType( int iType )
{
9 years ago
switch( iType )
9 years ago
{
case SCHED_CHASE_ENEMY_FAILED:
{
9 years ago
return &slChaseEnemyFailed[0];
9 years ago
}
default:
return CBaseMonster::GetScheduleOfType( iType );
}
}