ai

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2018 License: BSD-2-Clause, Zlib Imports: 4 Imported by: 0

Documentation

Overview

Package ai provides support for autonomous application unit behaviour. This is an experimental package that currently provides A-star and flow field path finding algorihms as well as 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

func Find added in v0.10.0

func Find(graph Graph, start, goal Point, path *[]Point)

Find is an A* path finder. Implementation based on:

https://en.wikipedia.org/wiki/A*_search_algorithm
https://www.redblobgames.com/pathfinding/a-star/implementation.html

The input path slice is reset to zero length and filled with the path results. An empty path is returned if no route was found.

Computation time increases proportional to the the size and number of locations in the graph.

Types

type Behaviour

type Behaviour interface {
	Status() (status BehaviourState) // Current behaviour status.
	Update() (status BehaviourState) // Update behaviour status.
	Init()                           // Called 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.

type Flow added in v0.10.0

type Flow interface {

	// Create a new flow field based on the given goal location.
	// This can be called to update maps as the goal changes.
	Create(goalx, goaly int) // Call once before calling Next.

	// Next, based on the current grid location gx, gy, returns the
	// next grid location nx, ny. 0, 0 is returned if the current
	// location is the goal. 9, 9 is returned if the given location
	// is invalid.
	Next(gx, gy int) (nx, ny int)
}

Flow creates a map to help units move towards their goal. The flow is initialized with a map and a goal. Afterwards each unit can query the flow for the best direction to move towards their goal.

func NewGridFlow added in v0.10.0

func NewGridFlow(g Grid) Flow

NewGridFlow creates a flow based on a grid.

type Graph added in v0.10.0

type Graph interface {

	// Neighbours returns the locations that can be reached
	// from the current location. The returned data may be
	// overwritten the next time this method is called.
	Neighbours(at Point) []Point

	// Cost for travelling from a to b. Used to value the move from
	// the current location to the next location. Lower cost is better.
	// Impassable areas have very high costs.
	Cost(a, b Point) float64

	// Estimate travel cost from a to b. Used to estimate cost
	// from current location to the goal. Lower cost is better.
	// Estimates that approximate the final path cost are best.
	Estimate(a, b Point) float64
}

Graph is a set of linked points that works with an A* pathing algorithm. A graph can be a grids or a set of waypoints or navigation meshes.

type Grid added in v0.10.0

type Grid interface {

	// Size returns the current size of the grid.
	Size() (width, depth int) // 0,0 for uninitialized or invalid grids.

	// Grid cells are either open or closed to passage.
	IsOpen(x, y int) bool // Return true if x,y is traversable.
}

Grid describes a 2D grid where each location in the grid is either traversable (passage/floor) or blocked (wall). See vu.grid package.

type Point added in v0.10.0

type Point interface {
	ID() int64 // Unique identifier for a location.
}

Point is a specific location in a Graph. Points with the same ID have the same location.

Jump to

Keyboard shortcuts

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