Skip to content

Commit

Permalink
Update frame-independent yaw speed calculation code. (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekonomicon authored Sep 18, 2024
1 parent 9212d51 commit 13a83d1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dlls/basemonster.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class CBaseMonster : public CBaseToggle
virtual BOOL ShouldFadeOnDeath( void );

// Basic Monster AI functions
virtual float ChangeYaw( int speed );
virtual float ChangeYaw( int yawSpeed );
float VecToYaw( Vector vecDir );
float FlYawDiff( void );

Expand Down
23 changes: 20 additions & 3 deletions dlls/flyingmonster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "game.h"
#include "monsters.h"
#include "schedule.h"
#include "flyingmonster.h"
Expand Down Expand Up @@ -83,7 +84,7 @@ void CFlyingMonster::Stop( void )
m_vecTravel = g_vecZero;
}

float CFlyingMonster::ChangeYaw( int speed )
float CFlyingMonster::ChangeYaw( int yawSpeed )
{
if( pev->movetype == MOVETYPE_FLY )
{
Expand All @@ -97,9 +98,25 @@ float CFlyingMonster::ChangeYaw( int speed )
else if( diff > 20.0f )
target = -90.0f;
}
pev->angles.z = UTIL_Approach( target, pev->angles.z, 220.0f * gpGlobals->frametime );

float speed = 220.f;

if( monsteryawspeedfix.value )
{
if( m_flLastZYawTime == 0.f )
m_flLastZYawTime = gpGlobals->time - gpGlobals->frametime;

float delta = Q_min( gpGlobals->time - m_flLastZYawTime, 0.25f );
m_flLastZYawTime = gpGlobals->time;

speed *= delta;
}
else
speed *= gpGlobals->frametime;

pev->angles.z = UTIL_Approach( target, pev->angles.z, speed );
}
return CBaseMonster::ChangeYaw( speed );
return CBaseMonster::ChangeYaw( yawSpeed );
}

void CFlyingMonster::Killed( entvars_t *pevAttacker, int iGib )
Expand Down
3 changes: 2 additions & 1 deletion dlls/flyingmonster.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CFlyingMonster : public CBaseMonster
Activity GetStoppedActivity( void );
void Killed( entvars_t *pevAttacker, int iGib );
void Stop( void );
float ChangeYaw( int speed );
float ChangeYaw( int yawSpeed );
void HandleAnimEvent( MonsterEvent_t *pEvent );
void MoveExecute( CBaseEntity *pTargetEnt, const Vector &vecDir, float flInterval );
void Move( float flInterval = 0.1 );
Expand All @@ -45,5 +45,6 @@ class CFlyingMonster : public CBaseMonster
float m_stopTime; // Last time we stopped (to avoid switching states too soon)
float m_momentum; // Weight for desired vs. momentum velocity
const char *m_pFlapSound;
float m_flLastZYawTime; // Last frame time Z was changed when yaw was changed
};
#endif //FLYINGMONSTER_H
50 changes: 43 additions & 7 deletions dlls/ichthyosaur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "game.h"
#include "monsters.h"
#include "schedule.h"
#include "flyingmonster.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ class CIchthyosaur : public CFlyingMonster
BOOL CheckMeleeAttack1( float flDot, float flDist );
BOOL CheckRangeAttack1( float flDot, float flDist );

float ChangeYaw( int speed );
float ChangeYaw( int yawSpeed );
Activity GetStoppedActivity( void );

void Move( float flInterval );
Expand All @@ -88,7 +89,7 @@ class CIchthyosaur : public CFlyingMonster

float VectorToPitch( const Vector &vec );
float FlPitchDiff( void );
float ChangePitch( int speed );
float ChangePitch( int pitchSpeed );

Vector m_SaveVelocity;
float m_idealDist;
Expand All @@ -106,6 +107,9 @@ class CIchthyosaur : public CFlyingMonster

float m_flNextAlert;

float m_flLastPitchTime; // Last frame time pitch was changed
float m_flLastZYawTime; // Last frame time Z was changed when yaw was changed

static const char *pIdleSounds[];
static const char *pAlertSounds[];
static const char *pAttackSounds[];
Expand Down Expand Up @@ -780,7 +784,7 @@ float CIchthyosaur::FlPitchDiff( void )
return flPitchDiff;
}

float CIchthyosaur::ChangePitch( int speed )
float CIchthyosaur::ChangePitch( int pitchSpeed )
{
if( pev->movetype == MOVETYPE_FLY )
{
Expand All @@ -793,12 +797,28 @@ float CIchthyosaur::ChangePitch( int speed )
else if( diff > 20 )
target = -45;
}
pev->angles.x = UTIL_Approach(target, pev->angles.x, 220.0f * 0.1f );

float speed = 220.f;

if( monsteryawspeedfix.value )
{
if( m_flLastPitchTime == 0.f )
m_flLastPitchTime = gpGlobals->time - gpGlobals->frametime;

float delta = Q_min( gpGlobals->time - m_flLastPitchTime, 0.25f );
m_flLastPitchTime = gpGlobals->time;

speed *= delta;
}
else
speed *= 0.1f;

pev->angles.x = UTIL_Approach(target, pev->angles.x, speed );
}
return 0;
}

float CIchthyosaur::ChangeYaw( int speed )
float CIchthyosaur::ChangeYaw( int yawSpeed )
{
if( pev->movetype == MOVETYPE_FLY )
{
Expand All @@ -812,9 +832,25 @@ float CIchthyosaur::ChangeYaw( int speed )
else if( diff > 20 )
target = -20;
}
pev->angles.z = UTIL_Approach( target, pev->angles.z, 220.0f * 0.1f );

float speed = 220.f;

if( monsteryawspeedfix.value )
{
if( m_flLastZYawTime == 0.f )
m_flLastZYawTime = gpGlobals->time - gpGlobals->frametime;

float delta = Q_min( gpGlobals->time - m_flLastZYawTime, 0.25f );
m_flLastZYawTime = gpGlobals->time;

speed *= delta;
}
else
speed *= 0.1f;

pev->angles.z = UTIL_Approach( target, pev->angles.z, speed );
}
return CFlyingMonster::ChangeYaw( speed );
return CFlyingMonster::ChangeYaw( yawSpeed );
}

Activity CIchthyosaur::GetStoppedActivity( void )
Expand Down
11 changes: 5 additions & 6 deletions dlls/monsters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2023,8 +2023,6 @@ void CBaseMonster::MonsterInit( void )
SetThink( &CBaseMonster::MonsterInitThink );
pev->nextthink = gpGlobals->time + 0.1f;
SetUse( &CBaseMonster::MonsterUse );

m_flLastYawTime = gpGlobals->time;
}

//=========================================================
Expand Down Expand Up @@ -2509,9 +2507,12 @@ float CBaseMonster::ChangeYaw( int yawSpeed )
{
if( monsteryawspeedfix.value )
{
float delta;
if( m_flLastYawTime == 0.f )
m_flLastYawTime = gpGlobals->time - gpGlobals->frametime;

float delta = Q_min( gpGlobals->time - m_flLastYawTime, 0.25f );

delta = Q_min( gpGlobals->time - m_flLastYawTime, 0.25f );
m_flLastYawTime = gpGlobals->time;

speed = (float)yawSpeed * delta * 2;
}
Expand Down Expand Up @@ -2561,8 +2562,6 @@ float CBaseMonster::ChangeYaw( int yawSpeed )
else
move = 0;

m_flLastYawTime = gpGlobals->time;

return move;
}

Expand Down
2 changes: 1 addition & 1 deletion dlls/mpstubb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int CGraph::FindNearestNode( const Vector &vecOrigin, int afNodeTypes ) { return
/*********************************************************/

void CBaseMonster::ReportAIState( void ) { }
float CBaseMonster::ChangeYaw( int speed ) { return 0; }
float CBaseMonster::ChangeYaw( int yawSpeed ) { return 0; }
void CBaseMonster::MakeIdealYaw( Vector vecTarget ) { }

void CBaseMonster::CorpseFallThink( void )
Expand Down

0 comments on commit 13a83d1

Please sign in to comment.