Difference between revisions of "Giving an NPC Memory"

From Valve Developer Community
Jump to: navigation, search
 
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{npc tut}}
 
{{npc tut}}
Memory bits (such as '''bits_MEMORY_HAD_ENEMY''') allow NPCs to be more responsive during task execution.
+
 
==Related Functions==
+
== Entity memory ==
*<code>int CAI_SchedulesManager::GetMemoryID(const char *state_name)</code><br>Modify this functions if you need to create more memory bits that will be used in schedule definitions.
+
 
*<code>inline void CAI_BaseNPC::Remember( int iMemory )</code>
+
=== Enemies ===
*<code>inline void CAI_BaseNPC::Forget( int iMemory )</code>
+
 
*<code>inline bool CAI_BaseNPC::HasMemory( int iMemory )</code>
+
==== Current ====
*<code>inline bool CAI_BaseNPC::HasAllMemories( int iMemory )</code>
+
 
==See Also==
+
An NPC's current target is managed with these functions:
*[[List of memory bits]]
+
 
{{navbar|Defining the NPC|Creating an NPC|Creating a task}}
+
; <code>[[bool]] ChooseEnemy()</code>
 +
; <code>void SetEnemy( [[CBaseEntity]] *pEnemy, [[bool]] bSetCondNewEnemy )</code>
 +
; <code>[[CBaseEntity]]* GetEnemy()</code>
 +
; <code>[[CBaseCombatCharacter]]* GetEnemyCombatCharacterPointer()</code>
 +
; <code>[[float]] GetTimeEnemyAcquired()</code>
 +
: Gets and sets the current enemy and the time at which they were acquired. Choosing is preferred over setting.
 +
; <code>void OnEnemyChanged( [[CBaseEntity]] *pOldEnemy, [[CBaseEntity]] *pNewEnemy )</code>
 +
: Callback, by default does nothing.
 +
 
 +
==== Available ====
 +
 
 +
The list of available enemies is managed by the <code>CAI_Enemies</code> class. ''Crucially, the same <code>CAI_Enemies</code> object is shared between all members of a squad.''
 +
 
 +
{{tip|The functions below are all in <code>CAI_BaseNPC</code>. Use <code>GetEnemies()</code> to access the enemy list directly.}}
 +
 
 +
; <code>void UpdateEnemyMemory( [[CBaseEntity]] *pEnemy, const [[Vector]] &position, [[CBaseEntity]] *pInformer )</code>
 +
: Adds or updates an enemy.
 +
; <code>[[Vector]] GetEnemyLKP()</code>
 +
; <code>[[float]] GetEnemyLastTimeSeen()</code>
 +
: The last position/time at which data on this enemy was updated.
 +
; <code>void MarkEnemyAsEluded()</code>
 +
; <code>[[bool]] EnemyHasEludedMe()</code>
 +
: Eluded enemies were seen, but are now at an unknown location. They are stored in enemy memory but not normally targeted.
 +
 
 +
=== Unreachable ===
 +
 
 +
; <code>RememberUnreachable( [[CBaseEntity]] *pEntity, [[float]] duration = -1 )</code>
 +
: Causes the NPC to ignore the entity for the specified number of seconds. -1 means the AI's default, which is usually 3 seconds.
 +
; <code>IsUnreachable([[CBaseEntity]] *pEntity)</code>
 +
: Test reachability.
 +
 
 +
== Memory Bits ==
 +
 
 +
'''Memory bits''' are [[flag]]s which relate to the NPC's current state (for example, <code>bits_MEMORY_SUSPICIOUS</code> and <code>bits_MEMORY_FLINCHED</code>). See <code>game/server/ai_basenpc.h</code> for a full list.
 +
 
 +
;<code>Remember([[int]] iMemory)</code>
 +
;<code>Forget([[int]] iMemory)</code>
 +
: Set/remove a particular bit.
 +
;<code>HasMemory([[int]] iMemory)</code>
 +
;<code>HasAllMemories([[int]] iMemory)</code>
 +
: Check whether one or more memory bits are set.
 +
;<code>CAI_SchedulesManager::GetMemoryID(const [[char]] *state_name)</code>
 +
: Modify this function to add support for extra memory bits in schedule definitions. You shouldn't need to call it yourself.
 +
 
 +
{{navbar|Defining the NPC|Creating an NPC|Creating a condition}}
 +
 
 +
[[Category:AI Programming]]

Latest revision as of 06:31, 12 September 2011

Entity memory

Enemies

Current

An NPC's current target is managed with these functions:

bool ChooseEnemy()
void SetEnemy( CBaseEntity *pEnemy, bool bSetCondNewEnemy )
CBaseEntity* GetEnemy()
CBaseCombatCharacter* GetEnemyCombatCharacterPointer()
float GetTimeEnemyAcquired()
Gets and sets the current enemy and the time at which they were acquired. Choosing is preferred over setting.
void OnEnemyChanged( CBaseEntity *pOldEnemy, CBaseEntity *pNewEnemy )
Callback, by default does nothing.

Available

The list of available enemies is managed by the CAI_Enemies class. Crucially, the same CAI_Enemies object is shared between all members of a squad.

Tip.png Tip: The functions below are all in CAI_BaseNPC. Use GetEnemies() to access the enemy list directly.
void UpdateEnemyMemory( CBaseEntity *pEnemy, const Vector &position, CBaseEntity *pInformer )
Adds or updates an enemy.
Vector GetEnemyLKP()
float GetEnemyLastTimeSeen()
The last position/time at which data on this enemy was updated.
void MarkEnemyAsEluded()
bool EnemyHasEludedMe()
Eluded enemies were seen, but are now at an unknown location. They are stored in enemy memory but not normally targeted.

Unreachable

RememberUnreachable( CBaseEntity *pEntity, float duration = -1 )
Causes the NPC to ignore the entity for the specified number of seconds. -1 means the AI's default, which is usually 3 seconds.
IsUnreachable(CBaseEntity *pEntity)
Test reachability.

Memory Bits

Memory bits are flags which relate to the NPC's current state (for example, bits_MEMORY_SUSPICIOUS and bits_MEMORY_FLINCHED). See game/server/ai_basenpc.h for a full list.

Remember(int iMemory)
Forget(int iMemory)
Set/remove a particular bit.
HasMemory(int iMemory)
HasAllMemories(int iMemory)
Check whether one or more memory bits are set.
CAI_SchedulesManager::GetMemoryID(const char *state_name)
Modify this function to add support for extra memory bits in schedule definitions. You shouldn't need to call it yourself.