core

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadNumberToInt64

func ReadNumberToInt64(v interface{}) int64

func ReadNumberToUInt64

func ReadNumberToUInt64(v interface{}) uint64

func SetSubTreeLoadFunc

func SetSubTreeLoadFunc(f func(string) *BehaviorTree)

获取子树的方法

Types

type Action

type Action struct {
	BaseNode
	BaseWorker
}

*

  • Action is the base class for all action nodes. Thus, if you want to create
  • new custom action nodes, you need to inherit from this class. For example,
  • take a look at the Runner action: *
  • var Runner = b3.Class(b3.Action, {
  • name: 'Runner', *
  • tick: function(tick) {
  • return b3.RUNNING;
  • }
  • }); *
  • @module b3
  • @class Action
  • @extends BaseNode

*

func (*Action) Ctor

func (this *Action) Ctor()

func (*Action) Initialize

func (this *Action) Initialize(params *BTNodeCfg)

type BaseNode

type BaseNode struct {
	IBaseWorker
	// contains filtered or unexported fields
}

*

  • The BaseNode class is used as super class to all nodes in BehaviorJS. It
  • comprises all common variables and methods that a node must have to
  • execute. *
  • **IMPORTANT:** Do not inherit from this class, use `b3.Composite`,
  • `b3.Decorator`, `b3.Action` or `b3.Condition`, instead. *
  • The attributes are specially designed to serialization of the node in a
  • JSON format. In special, the `parameters` attribute can be set into the
  • visual editor (thus, in the JSON file), and it will be used as parameter
  • on the node initialization at `BehaviorTree.load`. *
  • BaseNode also provide 5 callback methods, which the node implementations
  • can override. They are `enter`, `open`, `tick`, `close` and `exit`. See
  • their documentation to know more. These callbacks are called inside the
  • `_execute` method, which is called in the tree traversal. *
  • @module b3
  • @class BaseNode

*

func (*BaseNode) Ctor

func (this *BaseNode) Ctor()

func (*BaseNode) Execute

func (this *BaseNode) Execute(tick *Tick) b3.Status

func (*BaseNode) GetBaseNodeWorker

func (this *BaseNode) GetBaseNodeWorker() IBaseWorker

func (*BaseNode) GetCategory

func (this *BaseNode) GetCategory() string

func (*BaseNode) GetID

func (this *BaseNode) GetID() string

func (*BaseNode) GetName

func (this *BaseNode) GetName() string

func (*BaseNode) GetTitle

func (this *BaseNode) GetTitle() string

func (*BaseNode) Initialize

func (this *BaseNode) Initialize(params *BTNodeCfg)

*

  • Initialization method.
  • @method Initialize
  • @construCtor

*

func (*BaseNode) SetBaseNodeWorker

func (this *BaseNode) SetBaseNodeWorker(worker IBaseWorker)

func (*BaseNode) SetName

func (this *BaseNode) SetName(name string)

func (*BaseNode) SetTitle

func (this *BaseNode) SetTitle(name string)

type BaseWorker

type BaseWorker struct {
}

func (*BaseWorker) OnClose

func (this *BaseWorker) OnClose(tick *Tick)

*

  • Close method, override this to use. This method is called after the tick
  • callback, and only if the tick return a state different from
  • `b3.RUNNING`. *
  • @method close
  • @param {Tick} tick A tick instance.

*

func (*BaseWorker) OnEnter

func (this *BaseWorker) OnEnter(tick *Tick)

*

  • Enter method, override this to use. It is called every time a node is
  • asked to execute, before the tick itself. *
  • @method enter
  • @param {Tick} tick A tick instance.

*

func (*BaseWorker) OnExit

func (this *BaseWorker) OnExit(tick *Tick)

*

  • Exit method, override this to use. Called every time in the end of the
  • execution. *
  • @method exit
  • @param {Tick} tick A tick instance.

*

func (*BaseWorker) OnOpen

func (this *BaseWorker) OnOpen(tick *Tick)

*

  • Open method, override this to use. It is called only before the tick
  • callback and only if the not isn't closed. *
  • Note: a node will be closed if it returned `b3.RUNNING` in the tick. *
  • @method open
  • @param {Tick} tick A tick instance.

*

func (*BaseWorker) OnTick

func (this *BaseWorker) OnTick(tick *Tick) b3.Status

*

  • Tick method, override this to use. This method must contain the real
  • execution of node (perform a task, call children, etc.). It is called
  • every time a node is asked to execute. *
  • @method tick
  • @param {Tick} tick A tick instance.

*

type BehaviorTree

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

*

  • The BehaviorTree class, as the name implies, represents the Behavior Tree
  • structure. *
  • There are two ways to construct a Behavior Tree: by manually setting the
  • root node, or by loading it from a data structure (which can be loaded
  • from a JSON). Both methods are shown in the examples below and better
  • explained in the user guide. *
  • The tick method must be called periodically, in order to send the tick
  • signal to all nodes in the tree, starting from the root. The method
  • `BehaviorTree.tick` receives a target object and a blackboard as
  • parameters. The target object can be anything: a game agent, a system, a
  • DOM object, etc. This target is not used by any piece of Behavior3JS,
  • i.e., the target object will only be used by custom nodes. *
  • The blackboard is obligatory and must be an instance of `Blackboard`. This
  • requirement is necessary due to the fact that neither `BehaviorTree` or
  • any node will store the execution variables in its own object (e.g., the
  • BT does not store the target, information about opened nodes or number of
  • times the tree was called). But because of this, you only need a single
  • tree instance to control multiple (maybe hundreds) objects. *
  • Manual construction of a Behavior Tree
  • -------------------------------------- *
  • var tree = new b3.BehaviorTree(); *
  • tree.root = new b3.Sequence({children:[
  • new b3.Priority({children:[
  • new MyCustomNode(),
  • new MyCustomNode()
  • ]}),
  • ...
  • ]}); * *
  • Loading a Behavior Tree from data structure
  • ------------------------------------------- *
  • var tree = new b3.BehaviorTree(); *
  • tree.load({
  • 'title' : 'Behavior Tree title'
  • 'description' : 'My description'
  • 'root' : 'node-id-1'
  • 'nodes' : {
  • 'node-id-1' : {
  • 'name' : 'Priority', // this is the node type
  • 'title' : 'Root Node',
  • 'description' : 'Description',
  • 'children' : ['node-id-2', 'node-id-3'],
  • },
  • ...
  • }
  • }) * *
  • @module b3
  • @class BehaviorTree

*

func NewBeTree

func NewBeTree() *BehaviorTree

func (*BehaviorTree) GetID

func (this *BehaviorTree) GetID() string

func (*BehaviorTree) GetRoot

func (this *BehaviorTree) GetRoot() IBaseNode

func (*BehaviorTree) GetTitile

func (this *BehaviorTree) GetTitile() string

func (*BehaviorTree) Initialize

func (this *BehaviorTree) Initialize()

*

  • Initialization method.
  • @method Initialize
  • @construCtor

*

func (*BehaviorTree) Load

func (this *BehaviorTree) Load(data *config.BTTreeCfg, maps *b3.RegisterStructMaps, extMaps *b3.RegisterStructMaps)

*

  • This method loads a Behavior Tree from a data structure, populating this
  • object with the provided data. Notice that, the data structure must
  • follow the format specified by Behavior3JS. Consult the guide to know
  • more about this format. *
  • You probably want to use custom nodes in your BTs, thus, you need to
  • provide the `names` object, in which this method can find the nodes by
  • `names[NODE_NAME]`. This variable can be a namespace or a dictionary,
  • as long as this method can find the node by its name, for example: *
  • //json
  • ...
  • 'node1': {
  • 'name': MyCustomNode,
  • 'title': ...
  • }
  • ... *
  • //code
  • var bt = new b3.BehaviorTree();
  • bt.load(data, {'MyCustomNode':MyCustomNode}) * *
  • @method load
  • @param {Object} data The data structure representing a Behavior Tree.
  • @param {Object} [names] A namespace or dict containing custom nodes.

*

func (*BehaviorTree) Print

func (this *BehaviorTree) Print()

func (*BehaviorTree) SetDebug

func (this *BehaviorTree) SetDebug(debug interface{})

func (*BehaviorTree) Tick

func (this *BehaviorTree) Tick(target interface{}, blackboard *Blackboard) b3.Status

*

  • Propagates the tick signal through the tree, starting from the root. *
  • This method receives a target object of any type (Object, Array,
  • DOMElement, whatever) and a `Blackboard` instance. The target object has
  • no use at all for all Behavior3JS components, but surely is important
  • for custom nodes. The blackboard instance is used by the tree and nodes
  • to store execution variables (e.g., last node running) and is obligatory
  • to be a `Blackboard` instance (or an object with the same interface). *
  • Internally, this method creates a Tick object, which will store the
  • target and the blackboard objects. *
  • Note: BehaviorTree stores a list of open nodes from last tick, if these
  • nodes weren't called after the current tick, this method will close them
  • automatically. *
  • @method tick
  • @param {Object} target A target object.
  • @param {Blackboard} blackboard An instance of blackboard object.
  • @return {Constant} The tick signal state.

*

type Blackboard

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

------------------------Blackboard-------------------------

func NewBlackboard

func NewBlackboard() *Blackboard

func (*Blackboard) Get

func (this *Blackboard) Get(key, treeScope, nodeScope string) interface{}

*

  • Retrieves a value in the blackboard. If treeScope and nodeScope are
  • provided, this method will retrieve the value from the per node per tree
  • memory. If only the treeScope is provided, it will retrieve the value
  • from the per tree memory. If no parameter is provided, this method will
  • retrieve from the global memory. If only nodeScope is provided (but
  • treeScope not), this method will still try to retrieve from the global
  • memory. *
  • @method get
  • @param {String} key The key to be retrieved.
  • @param {String} treeScope The tree id if accessing the tree or node
  • memory.
  • @param {String} nodeScope The node id if accessing the node memory.
  • @return {Object} The value stored or undefined.

*

func (*Blackboard) GetBool

func (this *Blackboard) GetBool(key, treeScope, nodeScope string) bool

func (*Blackboard) GetFloat64

func (this *Blackboard) GetFloat64(key, treeScope, nodeScope string) float64

func (*Blackboard) GetInt

func (this *Blackboard) GetInt(key, treeScope, nodeScope string) int

func (*Blackboard) GetInt32

func (this *Blackboard) GetInt32(key, treeScope, nodeScope string) int32

func (*Blackboard) GetInt64

func (this *Blackboard) GetInt64(key, treeScope, nodeScope string) int64

func (*Blackboard) GetInt64Safe

func (this *Blackboard) GetInt64Safe(key, treeScope, nodeScope string) int64

func (*Blackboard) GetMem

func (this *Blackboard) GetMem(key string) interface{}

func (*Blackboard) GetUInt64

func (this *Blackboard) GetUInt64(key, treeScope, nodeScope string) uint64

func (*Blackboard) GetUInt64Safe

func (this *Blackboard) GetUInt64Safe(key, treeScope, nodeScope string) uint64

func (*Blackboard) Initialize

func (this *Blackboard) Initialize()

func (*Blackboard) Remove

func (this *Blackboard) Remove(key string)

func (*Blackboard) Set

func (this *Blackboard) Set(key string, value interface{}, treeScope, nodeScope string)

*

  • Stores a value in the blackboard. If treeScope and nodeScope are
  • provided, this method will save the value into the per node per tree
  • memory. If only the treeScope is provided, it will save the value into
  • the per tree memory. If no parameter is provided, this method will save
  • the value into the global memory. Notice that, if only nodeScope is
  • provided (but treeScope not), this method will still save the value into
  • the global memory. *
  • @method set
  • @param {String} key The key to be stored.
  • @param {String} value The value to be stored.
  • @param {String} treeScope The tree id if accessing the tree or node
  • memory.
  • @param {String} nodeScope The node id if accessing the node memory.

*

func (*Blackboard) SetMem

func (this *Blackboard) SetMem(key string, value interface{})

func (*Blackboard) SetTree

func (this *Blackboard) SetTree(key string, value interface{}, treeScope string)

type Composite

type Composite struct {
	BaseNode
	BaseWorker
	// contains filtered or unexported fields
}

func (*Composite) AddChild

func (this *Composite) AddChild(child IBaseNode)

AddChild

func (*Composite) Ctor

func (this *Composite) Ctor()

func (*Composite) GetChild

func (this *Composite) GetChild(index int) IBaseNode

GetChild

func (*Composite) GetChildCount

func (this *Composite) GetChildCount() int

*

*
* @method GetChildCount
* @getChildCount

*

func (*Composite) Initialize

func (this *Composite) Initialize(params *BTNodeCfg)

*

  • Initialization method. *
  • @method Initialize
  • @construCtor

*

type Condition

type Condition struct {
	BaseNode
	BaseWorker
}

func (*Condition) Ctor

func (this *Condition) Ctor()

func (*Condition) Initialize

func (this *Condition) Initialize(params *BTNodeCfg)

*

  • Initialization method. *
  • @method Initialize
  • @construCtor

*

type Decorator

type Decorator struct {
	BaseNode
	BaseWorker
	// contains filtered or unexported fields
}

func (*Decorator) Ctor

func (this *Decorator) Ctor()

func (*Decorator) GetChild

func (this *Decorator) GetChild() IBaseNode

GetChild

func (*Decorator) Initialize

func (this *Decorator) Initialize(params *BTNodeCfg)

*

  • Initialization method. *
  • @method Initialize
  • @construCtor

*

func (*Decorator) SetChild

func (this *Decorator) SetChild(child IBaseNode)

type IAction

type IAction interface {
	IBaseNode
}

type IBaseNode

type IBaseNode interface {
	IBaseWrapper

	Ctor()
	Initialize(params *BTNodeCfg)
	GetCategory() string
	Execute(tick *Tick) b3.Status
	GetName() string
	GetTitle() string
	SetBaseNodeWorker(worker IBaseWorker)
	GetBaseNodeWorker() IBaseWorker
}

type IBaseWorker

type IBaseWorker interface {

	/**
	 * Enter method, override this to use. It is called every time a node is
	 * asked to execute, before the tick itself.
	 *
	 * @method enter
	 * @param {Tick} tick A tick instance.
	**/
	OnEnter(tick *Tick)
	/**
	 * Open method, override this to use. It is called only before the tick
	 * callback and only if the not isn't closed.
	 *
	 * Note: a node will be closed if it returned `b3.RUNNING` in the tick.
	 *
	 * @method open
	 * @param {Tick} tick A tick instance.
	**/
	OnOpen(tick *Tick)
	/**
	 * Tick method, override this to use. This method must contain the real
	 * execution of node (perform a task, call children, etc.). It is called
	 * every time a node is asked to execute.
	 *
	 * @method tick
	 * @param {Tick} tick A tick instance.
	**/
	OnTick(tick *Tick) b3.Status
	/**
	 * Close method, override this to use. This method is called after the tick
	 * callback, and only if the tick return a state different from
	 * `b3.RUNNING`.
	 *
	 * @method close
	 * @param {Tick} tick A tick instance.
	**/
	OnClose(tick *Tick)
	/**
	 * Exit method, override this to use. Called every time in the end of the
	 * execution.
	 *
	 * @method exit
	 * @param {Tick} tick A tick instance.
	**/
	OnExit(tick *Tick)
}

type IBaseWrapper

type IBaseWrapper interface {
	// contains filtered or unexported methods
}

type IComposite

type IComposite interface {
	IBaseNode
	GetChildCount() int
	GetChild(index int) IBaseNode
	AddChild(child IBaseNode)
}

type ICondition

type ICondition interface {
	IBaseNode
}

type IDecorator

type IDecorator interface {
	IBaseNode
	SetChild(child IBaseNode)
	GetChild() IBaseNode
}

type Memory

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

------------------------Memory-------------------------

func NewMemory

func NewMemory() *Memory

func (*Memory) Get

func (this *Memory) Get(key string) interface{}

func (*Memory) Remove

func (this *Memory) Remove(key string)

func (*Memory) Set

func (this *Memory) Set(key string, val interface{})

type SubTree

type SubTree struct {
	Action
}

子树,通过Name关联树ID查找

func (*SubTree) Initialize

func (this *SubTree) Initialize(setting *BTNodeCfg)

func (*SubTree) OnTick

func (this *SubTree) OnTick(tick *Tick) b3.Status

*

*执行子树
*使用sTree.Tick(tar, tick.Blackboard)的方法会导致每个树有自己的tick。
*如果子树包含running状态,同时复用了子树会导致歧义。
*改为只使用一个树,一个tick上下文。

*

func (*SubTree) String

func (this *SubTree) String() string

type Tick

type Tick struct {

	/**
	 * The blackboard reference.
	 * @property {b3.Blackboard} blackboard
	 * @readOnly
	**/
	Blackboard *Blackboard
	// contains filtered or unexported fields
}

*

  • A new Tick object is instantiated every tick by BehaviorTree. It is passed
  • as parameter to the nodes through the tree during the traversal. *
  • The role of the Tick class is to store the instances of tree, debug,
  • target and blackboard. So, all nodes can access these informations. *
  • For internal uses, the Tick also is useful to store the open node after
  • the tick signal, in order to let `BehaviorTree` to keep track and close
  • them when necessary. *
  • This class also makes a bridge between nodes and the debug, passing the
  • node state to the debug if the last is provided. *
  • @module b3
  • @class Tick

*

func NewTick

func NewTick() *Tick

func (*Tick) GetLastSubTree

func (this *Tick) GetLastSubTree() *SubTree

*

  • return top subtree node.
  • return nil when it is runing at major tree *

*

func (*Tick) GetTarget

func (this *Tick) GetTarget() interface{}

func (*Tick) GetTree

func (this *Tick) GetTree() *BehaviorTree

func (*Tick) Initialize

func (this *Tick) Initialize()

*

  • Initialization method.
  • @method Initialize
  • @construCtor

*

type TreeData

type TreeData struct {
	NodeMemory     *Memory
	OpenNodes      []IBaseNode
	TraversalDepth int
	TraversalCycle int
}

*

  • The Blackboard is the memory structure required by `BehaviorTree` and its
  • nodes. It only have 2 public methods: `set` and `get`. These methods works
  • in 3 different contexts: global, per tree, and per node per tree. *
  • Suppose you have two different trees controlling a single object with a
  • single blackboard, then: *
  • - In the global context, all nodes will access the stored information.
  • - In per tree context, only nodes sharing the same tree share the stored
  • information.
  • - In per node per tree context, the information stored in the blackboard
  • can only be accessed by the same node that wrote the data. *
  • The context is selected indirectly by the parameters provided to these
  • methods, for example: *
  • // getting/setting variable in global context
  • blackboard.set('testKey', 'value');
  • var value = blackboard.get('testKey'); *
  • // getting/setting variable in per tree context
  • blackboard.set('testKey', 'value', tree.id);
  • var value = blackboard.get('testKey', tree.id); *
  • // getting/setting variable in per node per tree context
  • blackboard.set('testKey', 'value', tree.id, node.id);
  • var value = blackboard.get('testKey', tree.id, node.id); *
  • Note: Internally, the blackboard store these memories in different
  • objects, being the global on `_baseMemory`, the per tree on `_treeMemory`
  • and the per node per tree dynamically create inside the per tree memory
  • (it is accessed via `_treeMemory[id].nodeMemory`). Avoid to use these
  • variables manually, use `get` and `set` instead. *
  • @module b3
  • @class Blackboard

* ------------------------TreeData-------------------------

func NewTreeData

func NewTreeData() *TreeData

type TreeMemory

type TreeMemory struct {
	*Memory
	// contains filtered or unexported fields
}

------------------------TreeMemory-------------------------

func NewTreeMemory

func NewTreeMemory() *TreeMemory

Jump to

Keyboard shortcuts

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