Creating a schedule: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Robot: fixing template case.)
No edit summary
Line 6: Line 6:


=== Enumeration ===
=== Enumeration ===
Schedules are defined in the NPC's [[AI_BEGIN_CUSTOM_NPC]] [[macro]] block. Before you start to do that however, you will need to add a new item to the class' enum:


enum
Schedules are identified with a number. Naturally, enums are employed for human readability.
{
      SCHED_DODGE_ENEMY_FIRE = LAST_SHARED_SCHEDULE,  
};


{{TODO|Elsewhere <code>BaseClass::NEXT_SCHEDULE</code> is used, not <code>LAST_SHARED_SCHEDULE</code>. What's the difference?}}
<source lang=cpp>
enum
{
SCHED_DODGE_ENEMY_FIRE = LAST_SHARED_SCHEDULE,
LAST_MY_NPC_SCHEDULE,
};
</source>
 
The "last schedule" values are used to prevent collisions between enums when inheriting from existing classes. <code>LAST_SHARED_SCHEDULE</code> is the end of the standard list of schedules that are shared by all NPCs; if you are inheriting from something other than <code>[[CAI_BaseNPC]]</code> you will probably want to check your baseclass and get its own "last schedule" value.


=== Definition ===
=== Definition ===
Once the schedule is enumerated, we can start defining it within <code>AI_BEGIN_CUSTOM_NPC</code>. All schedules use the same structure:


DEFINE_SCHEDULE
Once the schedule is enumerated, we can start defining it within the <code>AI_BEGIN_CUSTOM_NPC</code> block. All schedules use the same structure:
(
 
SCHED_DODGE_ENEMY_FIRE,
<source lang=cpp>
AI_BEGIN_CUSTOM_NPC( npc_custom, CNPC_Custom )
" Tasks"
DEFINE_SCHEDULE
" TASK_FIND_DODGE_DIRECTION 3"
(
" TASK_JUMP 0"
SCHED_DODGE_ENEMY_FIRE,
""
 
" Interrupts"
" Tasks"
" COND_LIGHT_DAMAGE"
" TASK_FIND_DODGE_DIRECTION 3"
);
" TASK_JUMP 0"
""
" Interrupts"
" COND_LIGHT_DAMAGE"
)
AI_END_CUSTOM_SCHEDULE_PROVIDER()
</source>


{{warning|The first character of each string (except for the 'gap') must be a space or a tab. Your schedule will otherwise be invalid!}}
{{warning|The first character of each string (except for the 'gap') must be a space or a tab. Your schedule will otherwise be invalid!}}
Line 34: Line 43:
As you can see, there are two parts to the definition:
As you can see, there are two parts to the definition:


#'''<code>Tasks</code>''' is simply the list of sequential [[task]]s an NPC must perform in order to complete the schedule.
; <code>Tasks</code>
#*Every task requires a numeric value to be passed with it! If the task doesn't actually make use of one, just pass <code>0</code>.
: The list of sequential [[task]]s an NPC must perform in order to complete the schedule. {{note|Every task requires a numeric argument. If the task doesn't actually make use of one, just pass <code>0</code>.}}
#'''<code>Interrupts</code>''' is a list of [[conditions]] that will cause the schedule to be abandoned, and a new one chosen, if any are ever detected.
; <code>Interrupts</code>
: A list of [[conditions]] that will cause the schedule to be abandoned, and a new one chosen, if any are ever detected.


In the example schedule given above, the NPC will attempt to find a suitable direction to dodge in (checking in a maximum of three directions, judging by the parameter) before using the output of that task, stored somewhere in the class, to perform the movement itself. But if it encounters <code>COND_LIGHT_DAMAGE</code> (any damage greater than zero) during the process, it will abandon the schedule and select another.
In the example above the NPC will attempt to find a suitable direction to dodge in (checking in a maximum of three directions, judging by the parameter) before using the output of that task, stored somewhere in the class, to perform the movement itself. But if it encounters <code>COND_LIGHT_DAMAGE</code> (any damage greater than zero) during the process, it will abandon the schedule and select another.


The behavioural code that decides what the NPC actually ''does'' is defined in the component [[task]]s.
The behavioural code that decides what the NPC actually ''does'' is defined in the component [[task]]s.
Line 44: Line 54:
== SelectSchedule() ==
== SelectSchedule() ==


<code>SelectSchedule()</code> is called whenever an NPC finds itself without a schedule, and contains the logic that decides which should be selected to fill the gap. The NPC's current [[state]] and [[condition]]s usually play a large part in the decision.
<code>void SelectSchedule(void)</code> is called whenever an NPC finds itself without a schedule, and contains the logic that decides which should be selected to fill the gap. The NPC's current [[state]] and [[condition]]s usually play a large part in the decision.


A schedule is selected if the function returns it's name - e.g. <code>return SCHED_DODGE_ENEMY_FIRE;</code>.
A schedule is selected if the function returns it's name - e.g. <code>return SCHED_DODGE_ENEMY_FIRE;</code>.


{{tip|If you expect to write a lot of schedule selection logic, you may find it useful to split it into sub-functions of your own making. <code>CAI_BaseNPC</code> for instance has <code>SelectIdleSchedule()</code>, <code>SelectCombatSchedule()</code>, etc. that are called depending on the NPC's state.}}
{{tip|If you expect to write a lot of schedule selection logic, you may find it useful to split it into sub-functions of your own making. <code>CAI_BaseNPC</code> for instance has <code>SelectIdleSchedule()</code>, <code>SelectCombatSchedule()</code>, etc. which are called depending on the NPC's state.}}


=== Useful functions ===
=== Useful functions ===


;<code>HasCondition([[int]] condition)</code>
; <code>HasCondition([[int]] condition)</code>
:True if the specified [[condition]] has been set for the current think. Of course, you'd use your enumerated names instead of passing an integer directly!
: True if the specified [[condition]] has been set for the current think. Of course, you'd use your enumerated names instead of passing an integer directly!
;<code>GetState()</code>
; <code>GetState()</code>
:Returns the NPC's state. You can also access m_NPCState directly, but GetState() is read-only and therefore safer.
: Returns the NPC's state. You can also access m_NPCState directly, but GetState() is read-only and therefore safer.
;<code>return BaseClass<nowiki>::</nowiki>SelectSchedule()</code>
; <code>return BaseClass<nowiki>::</nowiki>SelectSchedule()</code>
:There are very few situations where you won't want to call the base class' function. Do so at the ''end'' of your function, since a return obviously takes precedence over any code that comes after it.
: If none of your own schedules were picked, there are very few situations where you won't want to pass through to the base class.


== TranslateSchedule() ==
== TranslateSchedule() ==
Line 63: Line 73:
<code>TranslateSchedule()</code> is called immediately after <code>SelectSchedule()</code>. It is designed to allow child classes to replace their parent's or parents' schedules with those of their own without having to duplicate selection logic.
<code>TranslateSchedule()</code> is called immediately after <code>SelectSchedule()</code>. It is designed to allow child classes to replace their parent's or parents' schedules with those of their own without having to duplicate selection logic.


int CNPC_Custom::TranslateSchedule( int scheduleType )
<source lang=cpp>
{
int CNPC_Custom::TranslateSchedule( int scheduleType )
switch( scheduleType )
{
{
switch( scheduleType )
case SCHED_IDLE_WALK:
{
{
case SCHED_IDLE_WALK:
return SCHED_CUSTOM_IDLE_WALK;
{
break;
return SCHED_CUSTOM_IDLE_WALK;
}
break;
}
}
}
return BaseClass::TranslateSchedule( scheduleType );
 
}
return BaseClass::TranslateSchedule( scheduleType );
}
</source>
 
{{navbar-last|Creating an interaction|Creating an NPC}}
{{navbar-last|Creating an interaction|Creating an NPC}}

Revision as of 08:01, 15 September 2011

A schedule is a list of tasks for an NPC to perform. A new schedule is chosen only when there is no active one; this might be because the NPC has only just spawned, because it has completed a schedule since it last thought, or because the schedule it was running previously encountered an interrupt condition.

Creating a new schedule

Enumeration

Schedules are identified with a number. Naturally, enums are employed for human readability.

enum
{
	SCHED_DODGE_ENEMY_FIRE = LAST_SHARED_SCHEDULE, 
	LAST_MY_NPC_SCHEDULE,
};

The "last schedule" values are used to prevent collisions between enums when inheriting from existing classes. LAST_SHARED_SCHEDULE is the end of the standard list of schedules that are shared by all NPCs; if you are inheriting from something other than CAI_BaseNPC you will probably want to check your baseclass and get its own "last schedule" value.

Definition

Once the schedule is enumerated, we can start defining it within the AI_BEGIN_CUSTOM_NPC block. All schedules use the same structure:

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_SCHEDULE_PROVIDER()
Warning.pngWarning:The first character of each string (except for the 'gap') must be a space or a tab. Your schedule will otherwise be invalid!

As you can see, there are two parts to the definition:

Tasks
The list of sequential tasks an NPC must perform in order to complete the schedule.
Note.pngNote:Every task requires a numeric argument. If the task doesn't actually make use of one, just pass 0.
Interrupts
A list of conditions that will cause the schedule to be abandoned, and a new one chosen, if any are ever detected.

In the example above the NPC will attempt to find a suitable direction to dodge in (checking in a maximum of three directions, judging by the parameter) before using the output of that task, stored somewhere in the class, to perform the movement itself. But if it encounters COND_LIGHT_DAMAGE (any damage greater than zero) during the process, it will abandon the schedule and select another.

The behavioural code that decides what the NPC actually does is defined in the component tasks.

SelectSchedule()

void SelectSchedule(void) is called whenever an NPC finds itself without a schedule, and contains the logic that decides which should be selected to fill the gap. The NPC's current state and conditions usually play a large part in the decision.

A schedule is selected if the function returns it's name - e.g. return SCHED_DODGE_ENEMY_FIRE;.

Tip.pngTip:If you expect to write a lot of schedule selection logic, you may find it useful to split it into sub-functions of your own making. CAI_BaseNPC for instance has SelectIdleSchedule(), SelectCombatSchedule(), etc. which are called depending on the NPC's state.

Useful functions

HasCondition(int condition)
True if the specified condition has been set for the current think. Of course, you'd use your enumerated names instead of passing an integer directly!
GetState()
Returns the NPC's state. You can also access m_NPCState directly, but GetState() is read-only and therefore safer.
return BaseClass::SelectSchedule()
If none of your own schedules were picked, there are very few situations where you won't want to pass through to the base class.

TranslateSchedule()

TranslateSchedule() is called immediately after SelectSchedule(). It is designed to allow child classes to replace their parent's or parents' schedules with those of their own without having to duplicate selection logic.

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

	return BaseClass::TranslateSchedule( scheduleType );
}
Creating an interaction Return to Creating an NPC