AI Programming Overview

From Valve Developer Community
Revision as of 07:10, 4 August 2005 by Fluxtah (talk | contribs) (formatting,)
Jump to navigation Jump to search


An NPC will react to it's environment in specific ways, depending on what mood it is in, at that given time. The depth of the AI implementation allows the programmer to create dumb bots that perform a minimal set of tasks, to complex characters that simulate true to life behaviour.


For us to understand how to code our own custom NPC's we must understand the programming logic that drives Source's AI. Most of this logic is driven by the code in CAI_BaseNPC, in order to start making a custom NPC, this is the best place to subclass from.


Each time the NPC thinks with NPCThink() a core method is executed RunAI().


RunAI() gathers the NPC's current Conditions according to its environment and determines the best State it should be in, any kind of logic can influence the NPC's conditions and state including the things it sees or the sounds it hears.


The NPC will then determine the best Schedule to run for the given conditions and state, it will maintain any current schedules or interrupt them and run more appropriate schedules to those given conditions and state.


The following links describe states, conditions, schedules and tasks in more detail:

For convenience a stripped down version of the RunAI() method is shown below in order to give you an idea of how an NPC's core logic works.

void CAI_BaseNPC::RunAI( void )
{
	g_AIRunTimer.Start();

	GatherConditions();

	TryRestoreHull();

	g_AIPrescheduleThinkTimer.Start();

	PrescheduleThink();

	g_AIPrescheduleThinkTimer.End();

	MaintainSchedule();

	PostscheduleThink();

	ClearTransientConditions();

	g_AIRunTimer.End();
}

Stub

This article or section is a stub. You can help by expanding it.