behaviortree

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// Running indicates that the Tick for a given Node is currently running
	Running
	// Success indicates that the Tick for a given Node completed successfully
	Success
	// Failure indicates that the Tick for a given Node failed to complete successfully
	Failure
)

Variables

View Source
var (
	// ErrManagerStopped is returned by the manager implementation in this package (see also NewManager) in the case
	// that Manager.Add is attempted after the manager has already started to stop. Use errors.Is to check this case.
	ErrManagerStopped error = errManagerStopped{/* contains filtered or unexported fields */}
)

Functions

func DefaultPrinterInspector

func DefaultPrinterInspector(node Node, tick Tick) ([]interface{}, interface{})

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

func (*Context) Cancel

func (c *Context) Cancel([]Node) (Status, error)

Cancel implements a tick that will cancel the receiver's context (noop if it has none) then succeed

func (*Context) Done

func (c *Context) Done([]Node) (Status, error)

Done implements a tick that will block on the receiver's context being canceled (noop if it has none) then succeed

func (*Context) Err

func (c *Context) Err([]Node) (Status, error)

Err implements a tick that will succeed if the receiver does not have a context or it has been canceled

func (*Context) Init

func (c *Context) Init([]Node) (Status, error)

Init implements a tick that will cancel existing context, (re)initialise the context, then succeed, note that it must not be called concurrently with any other method, and it must be ticked prior to any Context.Tick tick

func (*Context) Tick

func (c *Context) Tick(fn func(ctx context.Context, children []Node) (Status, error)) Tick

Tick returns a tick that will call fn with the receiver's context, returning nil if fn is nil (for consistency with other implementations in this package), note that a Init node must have already been ticked on all possible execution paths, or a panic may occur, due to fn being passed a nil context.Context

func (*Context) WithCancel

func (c *Context) WithCancel(parent context.Context) *Context

func (*Context) WithDeadline

func (c *Context) WithDeadline(parent context.Context, deadline time.Time) *Context

func (*Context) WithTimeout

func (c *Context) WithTimeout(parent context.Context, timeout time.Duration) *Context

WithTimeout configures the receiver to initialise context like context.WithTimeout(parent, timeout), returning the receiver

type Frame

type Frame struct {
	PC uintptr

	Function string

	File string

	Line int

	Entry uintptr
}

type Manager

type Manager interface {
	Ticker
	Add(ticker Ticker) error
}

func NewManager

func NewManager() Manager

type Node

type Node func() (Tick, []Node)

Node represents an node in a tree, that can be ticked

func New

func New(tick Tick, children ...Node) Node

New constructs a new behavior tree and is equivalent to NewNode with vararg support for less indentation

func NewNode

func NewNode(tick Tick, children []Node) Node

func Sync

func Sync(nodes []Node) []Node

func (Node) Frame

func (n Node) Frame() *Frame

func (Node) String

func (n Node) String() string

func (Node) Tick

func (n Node) Tick() (Status, error)

func (Node) Value

func (n Node) Value(key interface{}) interface{}

func (Node) WithValue

func (n Node) WithValue(key, value interface{}) Node

type Printer

type Printer interface {
	Fprint(output io.Writer, node Node) error
}
var (
	DefaultPrinter Printer = TreePrinter{
		Inspector: DefaultPrinterInspector,
		Formatter: DefaultPrinterFormatter,
	}
)

type Status

type Status int

Status is a type with three valid values, Running, Success, and Failure, the three possible states for BTs

func All

func All(children []Node) (Status, error)

func Selector

func Selector(children []Node) (Status, error)

func Sequence

func Sequence(children []Node) (Status, error)

func Switch

func Switch(children []Node) (Status, error)

func (Status) Status

func (s Status) Status() Status

func (Status) String

func (s Status) String() string

type Tick

type Tick func(children []Node) (Status, error)

Tick represents the logic for a node, which may or may not be stateful

func Any

func Any(tick Tick) Tick

Any wraps a tick such that non-error non-running statuses will be overridden with a success if at least one child succeeded - which is achieved by encapsulation of children, before passing them into the wrapped tick. Nil will be returned if tick is nil, and nil children will be passed through as such.

func Async

func Async(tick Tick) Tick

func Background

func Background(tick func() Tick) Tick

func Fork

func Fork() Tick

func Memorize

func Memorize(tick Tick) Tick

func Not

func Not(tick Tick) Tick

func RateLimit

func RateLimit(d time.Duration) Tick

RateLimit generates a stateful Tick that will return success at most once per a given duration

func Shuffle

func Shuffle(tick Tick, source rand.Source) Tick

func (Tick) Frame

func (t Tick) Frame() *Frame

type Ticker

type Ticker interface {
	Done() <-chan struct{}

	Err() error

	Stop()
}

func NewTicker

func NewTicker(ctx context.Context, duration time.Duration, node Node) Ticker

func NewTickerStopOnFailure

func NewTickerStopOnFailure(ctx context.Context, duration time.Duration, node Node) Ticker

type TreePrinter

type TreePrinter struct {
	Inspector func(node Node, tick Tick) (meta []interface{}, value interface{})
	Formatter func() TreePrinterNode
}

func (TreePrinter) Fprint

func (p TreePrinter) Fprint(output io.Writer, node Node) error

type TreePrinterNode

type TreePrinterNode interface {
	Add(meta []interface{}, value interface{}) TreePrinterNode

	Bytes() []byte
}

func DefaultPrinterFormatter

func DefaultPrinterFormatter() TreePrinterNode

DefaultPrinterFormatter is used by DefaultPrinter

Jump to

Keyboard shortcuts

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