Schedule: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (clean up)
mNo edit summary
Line 1: Line 1:
[[Category:AI Programming]]
[[Category:AI Programming]]
A '''schedule''' is a list of [[Tasks]] the NPC is to perform, a schedule could be to go to a specific location, such as <code>SCHED_FORCED_GO</code> or a schedule to create an attack plan <code>SCHED_PLAN_ATTACK</code>. The NPC's schedule selection is governed by its current [[States|State]] and [[Conditions]].
A '''schedule''' is a list of [[task]] the NPC is to perform, a schedule could be to go to a specific location, such as <code>SCHED_FORCED_GO</code> or a schedule to create an attack plan <code>SCHED_PLAN_ATTACK</code>. The NPC's schedule selection is governed by its current [[States|State]] and [[Conditions]].


==Defining a Schedule==
==Defining a Schedule==

Revision as of 18:32, 10 June 2006

A schedule is a list of task the NPC is to perform, a schedule could be to go to a specific location, such as SCHED_FORCED_GO or a schedule to create an attack plan SCHED_PLAN_ATTACK. The NPC's schedule selection is governed by its current State and Conditions.

Defining a Schedule

Custom schedules can be added by first declaring a new enum that will define the custom schedules.

enum
{
     SCHED_DODGE_ENEMY_FIRE = LAST_SHARED_SCHEDULE, 
}

Then we would need to define our custom schedule using the DEFINE_SCHEDULE macro.

AI_BEGIN_CUSTOM_NPC( npc_custom, CNPC_Custom )
	DEFINE_SCHEDULE
	(
		SCHED_DODGE_ENEMY_FIRE,

		"	Tasks"
		"		TASK_FIND_DODGE_DIRECTION	3"
		"		TASK_JUMP	 		0"
		""
		"	Interrupts"
	        "               COND_LIGHT_DAMAGE"
	);
AI_END_CUSTOM_NPC()

When you define a schedule you provide two sections, Tasks and Interrupts.

An Interrupt is a condition that will cause the NPC to break out of the schedule and run a more appropriate one to the given condition and/or state. In this case if the NPC experiences COND_LIGHT_DAMAGE (any damage greater than zero) the NPC will break out of this schedule and play a more appropriate one which would be handled in SelectSchedule() or state specific variants.

Some interesting methods are described below that make up CAI_BaseNPC class.

SelectSchedule

In CAI_BaseNPC the function SelectSchedule() determines what schedule the NPC should run depending on its state and conditions.

In CAI_BaseNPC, all of the default States are considered, and additional methods are called to select the schedule based on condition, for example, when the NPC is in the NPC_STATE_IDLE, the method SelectIdleSchedule() is called, this method is similar to SelectSchedule(), all it does is select a schedule based on the NPC's state and conditions.

Child classes can implement these schedule selection methods to select their own custom schedules based on their own custom conditions.

For instance, we could create our own schedule SCHED_MEDICALPOINT_GO and a condition COND_NEEDS_MEDICAL_ASSISTANCE.

We could implement the schedule selection in SelectSchedule() or a schedule selection method variation as follows

if(HasCondition(COND_NEEDS_MEDICAL_ASSISTANCE)) 
     return SCHED_MEDICALPOINT_GO; 

The following switch statement has been taken from SelectSchedule() in CAI_BaseNPC, it shows how the different variations of schedule selection is chosen depending on the NPC's state.

switch( m_NPCState ) 
{ 
      case NPC_STATE_NONE: 
            DevWarning( 2, "NPC_STATE IS NONE!\n" ); 
            break; 
      
      case NPC_STATE_PRONE: 
            return SCHED_IDLE_STAND; 
      case NPC_STATE_IDLE: 
            AssertMsgOnce( GetEnemy() == NULL, "NPC has enemy but is not in combat state?" ); 
            return SelectIdleSchedule(); 

      case NPC_STATE_ALERT: 
            AssertMsgOnce( GetEnemy() == NULL, "NPC has enemy but is not in combat state?" ); 
            return SelectAlertSchedule(); 

      case NPC_STATE_COMBAT: 
            return SelectCombatSchedule(); 
      
      case NPC_STATE_DEAD: 
            return SelectDeadSchedule(); 

      case NPC_STATE_SCRIPT: 
            return SelectScriptSchedule(); 

      default: 
            DevWarning( 2, "Invalid State for SelectSchedule!\n" ); 
            break; 
} 

TranslateSchedule

This function is called after the schedule selection. It can be used to translate parent NPC schedules to child specific schedules. It's useful if you want to change the default schedules to run your own ones instead.

The following code demonstrates how to translate schedules into child specific schedules.

int CNPC_Custom::TranslateSchedule( int scheduleType )
{
	switch( scheduleType )
	{
		case SCHED_IDLE_WALK:
		{
			return SCHED_CUSTOM_IDLE_WALK;
			break;
		}
	}

	return BaseClass::TranslateSchedule( scheduleType );
}

See Also