AI Framework: Difference between revisions

From The DarkMod Wiki
Jump to navigationJump to search
No edit summary
Line 1: Line 1:
== AI Priority Queue Basics ==
== AI Priority Queue Basics ==
Each AI state (idle, combat, etc.) corresponds to a task with an associated priority. For instance, the idle task has a priority of 1000. It's possible to push several different tasks into an AI's task queue, but only the task with the highest priority value will get executed (this is handled by the SDK).
Each AI state (idle, combat, etc.) corresponds to a task with an associated priority. For instance, the idle task has a priority of 1000. It's possible to push several different tasks into an AI's task queue, but only the task with the highest priority value will get executed (this is handled by the SDK).


Let's say our AI is in "idle" state (which means that no higher-prioritized tasks are pending): the SDK will lookup the according method from the AI's scriptobject, in this case, the method is '''void ai_darkmod_base::task_Idle()'''.
Let's say our AI is in "idle" state (which means that no higher-prioritized tasks are pending): the SDK will lookup the according method from the AI's scriptobject, in this case, the method is  
'''void ai_darkmod_base::task_Idle()'''.


A task is considered finished or done when the script method is returning. As long as the method is running (e.g. with an '''eachFrame''' loop), the task is not finished. It's up to the '''task_Idle()''' method to switch the state to something else: the idle script checks in regular intervals whether  something suspicious is seen by the AI and switches to another state.
A task is considered finished or done when the script method is returning. As long as the method is running (e.g. with an '''eachFrame''' loop), the task is not finished. It's up to the '''task_Idle()''' method to switch the state to something else: the idle script checks in regular intervals whether  something suspicious is seen by the AI and switches to another state.

Revision as of 09:45, 24 September 2007

AI Priority Queue Basics

Each AI state (idle, combat, etc.) corresponds to a task with an associated priority. For instance, the idle task has a priority of 1000. It's possible to push several different tasks into an AI's task queue, but only the task with the highest priority value will get executed (this is handled by the SDK).

Let's say our AI is in "idle" state (which means that no higher-prioritized tasks are pending): the SDK will lookup the according method from the AI's scriptobject, in this case, the method is

void ai_darkmod_base::task_Idle().

A task is considered finished or done when the script method is returning. As long as the method is running (e.g. with an eachFrame loop), the task is not finished. It's up to the task_Idle() method to switch the state to something else: the idle script checks in regular intervals whether something suspicious is seen by the AI and switches to another state.

How to add/switch Tasks

Let's say an AI is in idle state and encounters an enemy which it likes to fight. To switch to "fighting mode", the task_Combat must be pushed to the priority queue, which can be done like this (pseudocode):

void task_Idle()
{
  eachFrame {
    if (enemyCanBeSeen) {
      pushTask("task_Combat", PRIORITY_COMBAT); // Register a new task (with the default combat priority)
      return; // return means to end this "idle" task
    }
    
    // Perform regular idle tasks (patrolling, head turning, talking, whatever).
  }
}