Skip to content

Commit 86d7dfa

Browse files
committed
Switch to "sound"-based detection for grenades
1 parent e636ca2 commit 86d7dfa

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

src/game/server/neo/bot/behavior/neo_bot_tactical_monitor.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,30 +176,32 @@ void CNEOBotTacticalMonitor::AvoidBumpingEnemies( CNEOBot *me )
176176
ActionResult< CNEOBot > CNEOBotTacticalMonitor::WatchForGrenades( CNEOBot *me )
177177
{
178178
const float flGrenadeCheckRadius = neo_bot_grenade_check_radius.GetFloat();
179-
CBaseEntity *pSearch = NULL;
180179
CBaseEntity *closestThreat = NULL;
181-
float closestThreatDistSqr = FLT_MAX;
182180
const char *pszGrenadeClass = "neo_grenade_frag";
183-
184-
while ((pSearch = gEntList.FindEntityInSphere(pSearch, me->GetAbsOrigin(), flGrenadeCheckRadius)) != NULL)
181+
182+
int iSound = CSoundEnt::ActiveList();
183+
while ( iSound != SOUNDLIST_EMPTY )
185184
{
186-
if ( !FClassnameIs( pSearch, pszGrenadeClass ) )
187-
continue;
185+
CSound *pSound = CSoundEnt::SoundPointerForIndex( iSound );
186+
if ( !pSound )
187+
break;
188188

189-
bool bIsVisible = me->GetVisionInterface()->IsLineOfSightClear( pSearch->WorldSpaceCenter() );
190-
if ( bIsVisible )
191-
{
192-
// Prioritize evading grenades that the bot can plausibly see
193-
closestThreat = pSearch;
194-
break; // doesn't need to be perfect, overlapping throws are rare
195-
}
196-
else if (!closestThreat)
189+
if ( (pSound->SoundType() & SOUND_DANGER) && pSound->ValidateOwner() )
197190
{
198-
// fallback for when sometimes too many players are in the way
199-
// rationale is that usually the reaction of other players will indicate a grenade as well
200-
// otherwise bots can seem unresponsive especially to the bounce sound
201-
closestThreat = pSearch;
191+
float distSqr = ( pSound->GetSoundOrigin() - me->GetAbsOrigin() ).LengthSqr();
192+
if ( distSqr <= (flGrenadeCheckRadius * flGrenadeCheckRadius) )
193+
{
194+
CBaseEntity *pOwner = pSound->m_hOwner.Get();
195+
if ( pOwner && FClassnameIs( pOwner, pszGrenadeClass ) )
196+
{
197+
// Found a dangerous grenade
198+
closestThreat = pOwner;
199+
break;
200+
}
201+
}
202202
}
203+
204+
iSound = pSound->NextSound();
203205
}
204206

205207
if ( closestThreat )

src/game/server/neo/neo_grenade.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void CNEOGrenadeFrag::Spawn(void)
4949

5050
SetThink(&CNEOGrenadeFrag::DelayThink);
5151
SetNextThink(gpGlobals->curtime);
52+
53+
m_flNextSoundTime = gpGlobals->curtime;
5254
}
5355

5456
void CNEOGrenadeFrag::Precache(void)
@@ -77,15 +79,17 @@ void CNEOGrenadeFrag::DelayThink()
7779
return;
7880
}
7981

80-
if (!m_bHasWarnedAI && gpGlobals->curtime >= m_flWarnAITime)
81-
{
8282
#if !defined( CLIENT_DLL )
83-
CSoundEnt::InsertSound(SOUND_DANGER, GetAbsOrigin(), 400, 1.5, this);
84-
#endif
85-
m_bHasWarnedAI = true;
83+
// Emit danger sound periodically for bots to hear
84+
if (gpGlobals->curtime >= m_flNextSoundTime)
85+
{
86+
// 400 radius, 0.5s duration. Bots need to react quickly.
87+
CSoundEnt::InsertSound(SOUND_DANGER, GetAbsOrigin(), 400, 0.5f, this, SOUNDENT_CHANNEL_REPEATED_DANGER);
88+
m_flNextSoundTime = gpGlobals->curtime + 0.35f;
8689
}
90+
#endif
8791

88-
SetNextThink(gpGlobals->curtime + 0.1);
92+
SetNextThink(gpGlobals->curtime + 0.1f);
8993
}
9094

9195
void CNEOGrenadeFrag::Explode(trace_t* pTrace, int bitsDamageType)

src/game/server/neo/neo_grenade.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CNEOGrenadeFrag : public CBaseGrenadeProjectile
4141
protected:
4242
bool m_inSolid;
4343
bool m_punted;
44+
float m_flNextSoundTime;
4445
};
4546

4647
CBaseGrenadeProjectile *NEOFraggrenade_Create(const Vector &position, const QAngle &angles, const Vector &velocity,

0 commit comments

Comments
 (0)