Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.

312 lines
8.7 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.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
/*
===== monsters.cpp ========================================================
Monster-related utility code
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "animation.h"
#include "saverestore.h"
8 years ago
TYPEDESCRIPTION CBaseAnimating::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CBaseMonster, m_flFrameRate, FIELD_FLOAT ),
DEFINE_FIELD( CBaseMonster, m_flGroundSpeed, FIELD_FLOAT ),
DEFINE_FIELD( CBaseMonster, m_flLastEventCheck, FIELD_TIME ),
DEFINE_FIELD( CBaseMonster, m_fSequenceFinished, FIELD_BOOLEAN ),
DEFINE_FIELD( CBaseMonster, m_fSequenceLoops, FIELD_BOOLEAN ),
};
IMPLEMENT_SAVERESTORE( CBaseAnimating, CBaseDelay )
9 years ago
//=========================================================
// StudioFrameAdvance - advance the animation frame up to the current time
// if an flInterval is passed in, only advance animation that number of seconds
//=========================================================
8 years ago
float CBaseAnimating::StudioFrameAdvance( float flInterval )
9 years ago
{
8 years ago
if( flInterval == 0.0 )
9 years ago
{
8 years ago
flInterval = gpGlobals->time - pev->animtime;
if( flInterval <= 0.001 )
9 years ago
{
pev->animtime = gpGlobals->time;
return 0.0;
}
}
8 years ago
if( !pev->animtime )
9 years ago
flInterval = 0.0;
8 years ago
9 years ago
pev->frame += flInterval * m_flFrameRate * pev->framerate;
pev->animtime = gpGlobals->time;
8 years ago
if( pev->frame < 0.0 || pev->frame >= 256.0 )
9 years ago
{
8 years ago
if( m_fSequenceLoops )
pev->frame -= (int)( pev->frame / 256.0 ) * 256.0;
9 years ago
else
8 years ago
pev->frame = ( pev->frame < 0.0 ) ? 0 : 255;
9 years ago
m_fSequenceFinished = TRUE; // just in case it wasn't caught in GetEvents
}
return flInterval;
}
//=========================================================
// LookupActivity
//=========================================================
8 years ago
int CBaseAnimating::LookupActivity( int activity )
9 years ago
{
ASSERT( activity != 0 );
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
return ::LookupActivity( pmodel, pev, activity );
}
//=========================================================
// LookupActivityHeaviest
//
// Get activity with highest 'weight'
//
//=========================================================
8 years ago
int CBaseAnimating::LookupActivityHeaviest( int activity )
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
return ::LookupActivityHeaviest( pmodel, pev, activity );
}
//=========================================================
//=========================================================
8 years ago
int CBaseAnimating::LookupSequence( const char *label )
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
return ::LookupSequence( pmodel, label );
}
//=========================================================
//=========================================================
8 years ago
void CBaseAnimating::ResetSequenceInfo()
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
GetSequenceInfo( pmodel, pev, &m_flFrameRate, &m_flGroundSpeed );
8 years ago
m_fSequenceLoops = ( ( GetSequenceFlags() & STUDIO_LOOPING ) != 0 );
9 years ago
pev->animtime = gpGlobals->time;
pev->framerate = 1.0;
m_fSequenceFinished = FALSE;
m_flLastEventCheck = gpGlobals->time;
}
//=========================================================
//=========================================================
8 years ago
BOOL CBaseAnimating::GetSequenceFlags()
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
return ::GetSequenceFlags( pmodel, pev );
}
//=========================================================
// DispatchAnimEvents
//=========================================================
8 years ago
void CBaseAnimating::DispatchAnimEvents( float flInterval )
9 years ago
{
MonsterEvent_t event;
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
8 years ago
if( !pmodel )
9 years ago
{
ALERT( at_aiconsole, "Gibbed monster is thinking!\n" );
return;
}
// FIXME: I have to do this or some events get missed, and this is probably causing the problem below
flInterval = 0.1;
// FIX: this still sometimes hits events twice
8 years ago
float flStart = pev->frame + ( m_flLastEventCheck - pev->animtime ) * m_flFrameRate * pev->framerate;
9 years ago
float flEnd = pev->frame + flInterval * m_flFrameRate * pev->framerate;
m_flLastEventCheck = pev->animtime + flInterval;
m_fSequenceFinished = FALSE;
8 years ago
if( flEnd >= 256 || flEnd <= 0.0 )
9 years ago
m_fSequenceFinished = TRUE;
int index = 0;
8 years ago
while( ( index = GetAnimationEvent( pmodel, pev, &event, flStart, flEnd, index ) ) != 0 )
9 years ago
{
HandleAnimEvent( &event );
}
}
//=========================================================
//=========================================================
8 years ago
float CBaseAnimating::SetBoneController( int iController, float flValue )
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
return SetController( pmodel, pev, iController, flValue );
}
//=========================================================
//=========================================================
8 years ago
void CBaseAnimating::InitBoneControllers( void )
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
SetController( pmodel, pev, 0, 0.0 );
SetController( pmodel, pev, 1, 0.0 );
SetController( pmodel, pev, 2, 0.0 );
SetController( pmodel, pev, 3, 0.0 );
}
//=========================================================
//=========================================================
8 years ago
float CBaseAnimating::SetBlending( int iBlender, float flValue )
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
9 years ago
return ::SetBlending( pmodel, pev, iBlender, flValue );
}
//=========================================================
//=========================================================
8 years ago
void CBaseAnimating::GetBonePosition( int iBone, Vector &origin, Vector &angles )
9 years ago
{
8 years ago
GET_BONE_POSITION( ENT( pev ), iBone, origin, angles );
9 years ago
}
//=========================================================
//=========================================================
8 years ago
void CBaseAnimating::GetAttachment( int iAttachment, Vector &origin, Vector &angles )
9 years ago
{
8 years ago
GET_ATTACHMENT( ENT( pev ), iAttachment, origin, angles );
9 years ago
}
//=========================================================
//=========================================================
8 years ago
int CBaseAnimating::FindTransition( int iEndingSequence, int iGoalSequence, int *piDir )
9 years ago
{
8 years ago
void *pmodel = GET_MODEL_PTR( ENT( pev ) );
if( piDir == NULL )
9 years ago
{
int iDir;
int sequence = ::FindTransition( pmodel, iEndingSequence, iGoalSequence, &iDir );
8 years ago
if( iDir != 1 )
9 years ago
return -1;
else
return sequence;
}
return ::FindTransition( pmodel, iEndingSequence, iGoalSequence, piDir );
}
//=========================================================
//=========================================================
8 years ago
void CBaseAnimating::GetAutomovement( Vector &origin, Vector &angles, float flInterval )
9 years ago
{
}
8 years ago
void CBaseAnimating::SetBodygroup( int iGroup, int iValue )
9 years ago
{
8 years ago
::SetBodygroup( GET_MODEL_PTR( ENT( pev ) ), pev, iGroup, iValue );
9 years ago
}
8 years ago
int CBaseAnimating::GetBodygroup( int iGroup )
9 years ago
{
8 years ago
return ::GetBodygroup( GET_MODEL_PTR( ENT( pev ) ), pev, iGroup );
9 years ago
}
8 years ago
int CBaseAnimating::ExtractBbox( int sequence, float *mins, float *maxs )
9 years ago
{
8 years ago
return ::ExtractBbox( GET_MODEL_PTR( ENT( pev ) ), sequence, mins, maxs );
9 years ago
}
//=========================================================
//=========================================================
8 years ago
void CBaseAnimating::SetSequenceBox( void )
9 years ago
{
Vector mins, maxs;
// Get sequence bbox
8 years ago
if( ExtractBbox( pev->sequence, mins, maxs ) )
9 years ago
{
// expand box for rotation
// find min / max for rotations
8 years ago
float yaw = pev->angles.y * ( M_PI / 180.0 );
9 years ago
Vector xvector, yvector;
8 years ago
xvector.x = cos( yaw );
xvector.y = sin( yaw );
yvector.x = -sin( yaw );
yvector.y = cos( yaw );
9 years ago
Vector bounds[2];
bounds[0] = mins;
bounds[1] = maxs;
8 years ago
9 years ago
Vector rmin( 9999, 9999, 9999 );
Vector rmax( -9999, -9999, -9999 );
Vector base, transformed;
8 years ago
for( int i = 0; i <= 1; i++ )
9 years ago
{
base.x = bounds[i].x;
8 years ago
for( int j = 0; j <= 1; j++ )
9 years ago
{
base.y = bounds[j].y;
8 years ago
for( int k = 0; k <= 1; k++ )
9 years ago
{
base.z = bounds[k].z;
8 years ago
// transform the point
transformed.x = xvector.x * base.x + yvector.x * base.y;
transformed.y = xvector.y * base.x + yvector.y * base.y;
9 years ago
transformed.z = base.z;
8 years ago
if( transformed.x < rmin.x )
9 years ago
rmin.x = transformed.x;
8 years ago
if( transformed.x > rmax.x )
9 years ago
rmax.x = transformed.x;
8 years ago
if( transformed.y < rmin.y )
9 years ago
rmin.y = transformed.y;
8 years ago
if( transformed.y > rmax.y )
9 years ago
rmax.y = transformed.y;
8 years ago
if( transformed.z < rmin.z )
9 years ago
rmin.z = transformed.z;
8 years ago
if( transformed.z > rmax.z )
9 years ago
rmax.z = transformed.z;
}
}
}
rmin.z = 0;
rmax.z = rmin.z + 1;
UTIL_SetSize( pev, rmin, rmax );
}
}