From 13a83d12d6aacd7142af35a74e7270c81b4bd8e0 Mon Sep 17 00:00:00 2001 From: Andrey Akhmichin <15944199+nekonomicon@users.noreply.github.com> Date: Wed, 18 Sep 2024 20:18:58 +0000 Subject: [PATCH] Update frame-independent yaw speed calculation code. (#470) --- dlls/basemonster.h | 2 +- dlls/flyingmonster.cpp | 23 ++++++++++++++++--- dlls/flyingmonster.h | 3 ++- dlls/ichthyosaur.cpp | 50 ++++++++++++++++++++++++++++++++++++------ dlls/monsters.cpp | 11 +++++----- dlls/mpstubb.cpp | 2 +- 6 files changed, 72 insertions(+), 19 deletions(-) diff --git a/dlls/basemonster.h b/dlls/basemonster.h index dc642a6c96..e1bafc55b1 100644 --- a/dlls/basemonster.h +++ b/dlls/basemonster.h @@ -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 ); diff --git a/dlls/flyingmonster.cpp b/dlls/flyingmonster.cpp index 424f1dec1f..c33f3c8f2e 100644 --- a/dlls/flyingmonster.cpp +++ b/dlls/flyingmonster.cpp @@ -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" @@ -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 ) { @@ -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 ) diff --git a/dlls/flyingmonster.h b/dlls/flyingmonster.h index d043fa22ef..0fd999cb48 100644 --- a/dlls/flyingmonster.h +++ b/dlls/flyingmonster.h @@ -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 ); @@ -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 diff --git a/dlls/ichthyosaur.cpp b/dlls/ichthyosaur.cpp index 0f9fbf5ada..7a2b4fddd8 100644 --- a/dlls/ichthyosaur.cpp +++ b/dlls/ichthyosaur.cpp @@ -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" @@ -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 ); @@ -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; @@ -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[]; @@ -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 ) { @@ -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 ) { @@ -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 ) diff --git a/dlls/monsters.cpp b/dlls/monsters.cpp index a24a417a10..e41e795014 100644 --- a/dlls/monsters.cpp +++ b/dlls/monsters.cpp @@ -2023,8 +2023,6 @@ void CBaseMonster::MonsterInit( void ) SetThink( &CBaseMonster::MonsterInitThink ); pev->nextthink = gpGlobals->time + 0.1f; SetUse( &CBaseMonster::MonsterUse ); - - m_flLastYawTime = gpGlobals->time; } //========================================================= @@ -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; } @@ -2561,8 +2562,6 @@ float CBaseMonster::ChangeYaw( int yawSpeed ) else move = 0; - m_flLastYawTime = gpGlobals->time; - return move; } diff --git a/dlls/mpstubb.cpp b/dlls/mpstubb.cpp index 12b3a82f41..5a1b1d5b49 100644 --- a/dlls/mpstubb.cpp +++ b/dlls/mpstubb.cpp @@ -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 )