ai

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2015 License: BSD-2-Clause, Zlib Imports: 2 Imported by: 0

Documentation

Overview

Package ai provides support for application unit behaviour. This is an experimental package that currently provides a behaviour tree implementation.

Package ai is provided as part of the vu (virtual universe) 3D engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Behaviour

type Behaviour interface {
	Status() (status BehaviourState) // Current behaviour status.
	Update() (status BehaviourState) // Update behaviour status.
	Init()                           // Called once on first behaviour update.
	Reset()                          // Set status to INVALID.

	// Observer listens for completed behaviours.
	Observer() (bo BehaviourObserver) // Gets and
	SetObserver(bo BehaviourObserver) // ...sets behaviour observer.
}

Behaviour: controls actions for a period of time. These are provided by the application and are the building blocks of a behaviour tree. Status is maintained by the behaviour as follows:

  • Uninitialized behaviours should have status INVALID.
  • Initialized and uncompleted behaviours should have status RUNNING.
  • Completed behaviours should have status FAILURE or SUCCESS.

func NewSelector

func NewSelector(bt BehaviourTree, behaviours []Behaviour) Behaviour

NewSelector creates a Behaviour and adds it to the BehaviourTree. A selector runs its list of behaviours until one succeeds, in which case the selector succeeds. The selector fails if all of its behaviours fail. The list of selector behaviours is processed from lowest index to highest index.

func NewSequence

func NewSequence(bt BehaviourTree, behaviours []Behaviour) Behaviour

NewSequence creates a Behaviour and adds it to the BehaviourTree. A sequence runs its list of behaviours until one fails, in which case the sequence fails. The sequence succeeds if all of its behaviours succeed. The list of sequence behaviours is processed from lowest index to highest index.

type BehaviourBase

type BehaviourBase struct {
	State BehaviourState    // Needed to trigger Init.
	Obs   BehaviourObserver // Observer for this behaviour.
}

BehaviourBase holds the two fields that every behaviour needs and is expected to be embedded in every Behaviour implementation. Eg.

type someBehaviour struct {
   BehaviourBase
   // some behaviour specific fields.
}

func (*BehaviourBase) Observer

func (bb *BehaviourBase) Observer() (bo BehaviourObserver)

Observer returns the behaviour observer. Nil is returned if there is no current observer.

func (*BehaviourBase) Reset

func (bb *BehaviourBase) Reset()

Reset sets the state to INVALID. Allows behaviours to be reset and reused.

func (*BehaviourBase) SetObserver

func (bb *BehaviourBase) SetObserver(bo BehaviourObserver)

SetObserver sets the behaviour observer. Use nil to clear the observer.

func (*BehaviourBase) Status

func (bb *BehaviourBase) Status() (status BehaviourState)

Status returns the behaviour state.

type BehaviourObserver

type BehaviourObserver interface {
	Complete(b Behaviour) // Called when behaviour completes.
}

BehaviourObserver is the listener interface for behaviour completion. It is injected in the BehaviourTree.Start method.

type BehaviourState

type BehaviourState int

BehaviourState is a custom type for behaviour state constants.

const (
	INVALID BehaviourState = iota // Behaviour is not initialized.
	SUCCESS                       // Behaviour has succeeded.
	FAILURE                       // Behaviour has failed.
	RUNNING                       // Behaviour is still processing.
)

BehaviourState values.

type BehaviourTree

type BehaviourTree interface {

	// Start processing behaviour b and associate its completion status
	// with behaviour observer bo.
	Start(b Behaviour, bo BehaviourObserver) // Process a behaviour.

	// Stop informs the given behaviours observer of completion without
	// waiting for the next update tick. The behaviour itself will be
	// removed next update tick.
	Stop(b Behaviour) // Stop processing a behaviour

	// Tick updates each active behaviour. Completed behaviours
	// send notifications through their observers.
	Tick() // Expected to be called each regular update cycle.
}

BehaviourTree processes behaviours. Multiple behaviours may be started where each is a tree of behaviours composed of Sequences and/or Selectors.

func NewBehaviourTree

func NewBehaviourTree() BehaviourTree

NewBehaviourTree creates an empty behaviour tree. It must be initialized with behaviours using the Start method and then updated regularly using the Tick method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL