AI Task System - UQcsse3200/2024-studio-1 GitHub Wiki

Overview

The AI task system controls NPC behaviors in the game using a priority-based approach to decide which task an NPC should execute at any given time.

AITaskComponent

The AITaskComponent is the core of the AI system. It manages a list of PriorityTasks and selects which task to execute based on their priorities.

public class AITaskComponent extends Component {
  private final List<PriorityTask> priorityTasks = new ArrayList<>();
  private PriorityTask currentTask;
  // ...
}

Key functionalities:

  • Adding Tasks: Tasks are added to the AI component, each with its own priority.
  • Task Selection: On each update, the AI component selects the task with the highest priority.
  • Task Switching: If a new task has a higher priority than the current one, the AI component switches to the new task.

PriorityTask Interface

All tasks implement the PriorityTask interface:

public interface PriorityTask {
  int getPriority();
  void start();
  void update();
  void stop();
}
  • Dynamic Priorities: Task priorities can change based on game conditions.
  • Negative Priority: A negative priority indicates that the task should not be run.

Task Selection and Execution Logic

The AITaskComponent uses the following logic:

  1. Task Selection:

    • Get the priority of each task
    • Select the task with the highest priority
  2. Task Execution:

    • If the selected task is different from the current task:
      • Stop the current task
      • Start the new task
    • Update the current task

UML Diagrams

Class Diagram

classDiagram
    class AITaskComponent {
        -List~PriorityTask~ priorityTasks
        -PriorityTask currentTask
        +addTask(PriorityTask)
        +update()
        -getHighestPriorityTask()
        -changeTask(PriorityTask)
    }

    class PriorityTask {
        <<interface>>
        +getPriority() int
        +start()
        +update()
        +stop()
    }

    class ConcreteTask {
        -int priority
        +getPriority() int
        +start()
        +update()
        +stop()
    }

    AITaskComponent o-- PriorityTask
    PriorityTask <|.. ConcreteTask

Loading

This diagram shows the main components of the AI Task System:

  • AITaskComponent: Manages and executes tasks
  • PriorityTask: Interface for all tasks
  • ConcreteTask: An example of a specific task implementation

Sequence Diagrams

Task Selection

sequenceDiagram
    participant AITaskComponent
    participant PriorityTask

    AITaskComponent->>AITaskComponent: update()
    AITaskComponent->>AITaskComponent: getHighestPriorityTask()
    loop for each task
        AITaskComponent->>PriorityTask: getPriority()
        PriorityTask-->>AITaskComponent: priority
    end
    Note over AITaskComponent: Select highest priority task

Loading

This diagram illustrates how the AITaskComponent selects the highest priority task.

Task Execution

sequenceDiagram
    participant AITaskComponent
    participant CurrentTask
    participant NewTask

    alt new task != current task
        AITaskComponent->>CurrentTask: stop()
        AITaskComponent->>AITaskComponent: changeTask(newTask)
        AITaskComponent->>NewTask: start()
    end
    AITaskComponent->>NewTask: update()

Loading

This diagram shows how the AITaskComponent switches to and executes a new task.

Tasks

For a detailed overview of all available tasks, refer to the Animal Tasks page.


⬅ Back to Animal Overview

⚠️ **GitHub.com Fallback** ⚠️