AI Perception Behavior Enhancement: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Pretty sure that condition merging requires bitwise operators, if it even exists and works as intended.)
m (Syntax highlighting and some cleanup)
Line 3: Line 3:
With the modification below the NPC will investigate the source of the sound instead.
With the modification below the NPC will investigate the source of the sound instead.


Open the <code>ai_basenpc_schedule.cpp</code> file and look for the function <code>SelectAlertSchedule()</code>. Look for the following code:
Open the '''ai_basenpc_schedule.cpp''' file and look for the function <code>SelectAlertSchedule()</code>. Look for the following code:
 
<source lang=cpp>
if ( HasCondition ( COND_HEAR_DANGER ) ||
if ( HasCondition ( COND_HEAR_DANGER ) ||
  HasCondition ( COND_HEAR_PLAYER ) ||
  HasCondition ( COND_HEAR_PLAYER ) ||
  HasCondition ( COND_HEAR_WORLD  ) ||
  HasCondition ( COND_HEAR_WORLD  ) ||
  HasCondition ( COND_HEAR_BULLET_IMPACT ) ||
  HasCondition ( COND_HEAR_BULLET_IMPACT ) ||
  HasCondition ( COND_HEAR_COMBAT ) )
  HasCondition ( COND_HEAR_COMBAT ) )
{
{
return SCHED_ALERT_FACE_BESTSOUND;
return SCHED_ALERT_FACE_BESTSOUND;
}
}
</source>


Replace this code with:
Replace this code with:
 
<source lang=cpp highlight=7>
if ( HasCondition ( COND_HEAR_DANGER ) ||
if ( HasCondition( COND_HEAR_DANGER ) ||
  HasCondition ( COND_HEAR_PLAYER ) ||
HasCondition( COND_HEAR_PLAYER ) ||
  HasCondition ( COND_HEAR_WORLD  ) ||
HasCondition( COND_HEAR_WORLD  ) ||
  HasCondition ( COND_HEAR_BULLET_IMPACT ) ||
HasCondition( COND_HEAR_BULLET_IMPACT ) ||
  HasCondition ( COND_HEAR_COMBAT ) )
HasCondition( COND_HEAR_COMBAT ) )
{
{
return SCHED_INVESTIGATE_SOUND;
return SCHED_INVESTIGATE_SOUND;
}
}
</source>


Alternate Code:
Alternate Code:
 
<source lang=cpp>
if ( HasCondition ( COND_HEAR_DANGER | COND_HEAR_PLAYER | COND_HEAR_WORLD | COND_HEAR_BULLET_IMPACT | COND_HEAR_COMBAT ) )
if ( HasCondition( COND_HEAR_DANGER | COND_HEAR_PLAYER | COND_HEAR_WORLD | COND_HEAR_BULLET_IMPACT | COND_HEAR_COMBAT ) )
{
{
return SCHED_INVESTIGATE_SOUND;
return SCHED_INVESTIGATE_SOUND;
}
}
 
</source>
 


==Conclusion==
==Conclusion==

Revision as of 17:00, 12 August 2021

In Half-Life 2, if an idle NPC hears a sound but doesn't see its emitter, it will only turn in the direction of the sound for a bit before returning to its previous action.

With the modification below the NPC will investigate the source of the sound instead.

Open the ai_basenpc_schedule.cpp file and look for the function SelectAlertSchedule(). Look for the following code:

	if ( HasCondition ( COND_HEAR_DANGER ) ||
			  HasCondition ( COND_HEAR_PLAYER ) ||
			  HasCondition ( COND_HEAR_WORLD  ) ||
			  HasCondition ( COND_HEAR_BULLET_IMPACT ) ||
			  HasCondition ( COND_HEAR_COMBAT ) )
	{
		return SCHED_ALERT_FACE_BESTSOUND;
	}

Replace this code with:

	if ( HasCondition( COND_HEAR_DANGER ) ||
		HasCondition( COND_HEAR_PLAYER ) ||
		HasCondition( COND_HEAR_WORLD  ) ||
		HasCondition( COND_HEAR_BULLET_IMPACT ) ||
		HasCondition( COND_HEAR_COMBAT ) )
	{
		return SCHED_INVESTIGATE_SOUND;
	}

Alternate Code:

	if ( HasCondition( COND_HEAR_DANGER | COND_HEAR_PLAYER | COND_HEAR_WORLD | COND_HEAR_BULLET_IMPACT | COND_HEAR_COMBAT ) )
	{
		return SCHED_INVESTIGATE_SOUND;
	}

Conclusion

With this code, as soon as NPC hears sounds that meet conditions, they will investigate it.