behavior

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: MIT Imports: 21 Imported by: 0

README

behavior

golang event driver behavior tree features:

  • 事件驱动
  • 共享实例的行为数:所有节点无状态,状态由黑板管理
  • 并发:各个AI在独立子纤程执行互不干扰
  • 脚本:任务节点和条件节点的委托支持使用脚本

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultSharedBlackboard

func DefaultSharedBlackboard() *bcore.Blackboard

DefaultSharedBlackboard 获取全局默认共享黑板单例

@return *Blackboard

func GlobalHandlerPool

func GlobalHandlerPool() *handle.HandlerPool

GlobalHandlerPool 全局反射代理缓存池

@return *handle.HandlerPool

func InitSystem

func InitSystem(opts ...Option)

InitSystem 系统初始化

要使用该库必须先初始化
@param option
@panic 若发生异常将主动panic

func NewBrain

func NewBrain(blackboard bcore.IBlackboard, delegates map[string]any, finishChan chan *bcore.FinishEvent) bcore.IBrain

NewBrain bcore.IBrain 实例

@param blackboard
@param delegates 要注册的委托对象
@return bcore.IBrain

func RegisterDelegatorType

func RegisterDelegatorType(name string, target any) error

RegisterDelegatorType 注册代理类的反射信息

@param name
@param target

func SharedBlackboard

func SharedBlackboard(key string) *bcore.Blackboard

SharedBlackboard 获取全局共享黑板单例,若不存在会创建一个

@param key 指定key
@return *Blackboard

Types

type Brain

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

func (*Brain) Blackboard

func (b *Brain) Blackboard() bcore.IBlackboard

func (*Brain) FinishChan added in v1.0.3

func (b *Brain) FinishChan() <-chan *bcore.FinishEvent

func (*Brain) ForceRun added in v1.0.3

func (b *Brain) ForceRun(root bcore.IRoot)

func (*Brain) GetDelegate added in v1.0.2

func (b *Brain) GetDelegate(name string) (delegate any, ok bool)

func (*Brain) GetDelegates added in v1.0.2

func (b *Brain) GetDelegates() map[string]any

GetDelegates 获取委托map拷贝

@receiver b
@return map[string]any

func (*Brain) Go added in v1.0.3

func (b *Brain) Go(task func())

func (*Brain) OnNodeUpdate added in v1.0.2

func (b *Brain) OnNodeUpdate(target string, method string, brain bcore.IBrain, eventType bcore.EventType, delta time.Duration) bcore.Result

OnNodeUpdate 供节点回调执行委托 会在 Brain 的独立线程里运行

@receiver b
@param target
@param method
@param brain
@param eventType
@param delta
@return bcore.Result

func (*Brain) RWFinishChan added in v1.0.3

func (b *Brain) RWFinishChan() chan *bcore.FinishEvent

func (*Brain) RegisterDelegate

func (b *Brain) RegisterDelegate(name string, delegate any)

RegisterDelegate 注册委托对象

@receiver b
@param name
@param delegate

func (*Brain) Running added in v1.0.3

func (b *Brain) Running() bool

func (*Brain) RunningTree added in v1.0.3

func (b *Brain) RunningTree() bcore.IRoot

func (*Brain) SetDelegates

func (b *Brain) SetDelegates(delegates map[string]any)

SetDelegates 注册委托对象

@receiver b
@param delegatesMeta

func (*Brain) SetFinishChan added in v1.0.3

func (b *Brain) SetFinishChan(finishChan chan *bcore.FinishEvent)

func (*Brain) SetRunningTree added in v1.0.3

func (b *Brain) SetRunningTree(root bcore.IRoot)

type ClassLoader

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

ClassLoader 节点类加加载器 作用 1.缓存节点类2.使用时找到缓存类并实例化出来

func GlobalClassLoader

func GlobalClassLoader() *ClassLoader

GlobalClassLoader 全局类加载器

@return *ClassLoader

func NewClassLoader

func NewClassLoader() *ClassLoader

func (*ClassLoader) Contains

func (l *ClassLoader) Contains(name string) bool

Contains 检查注册器中是否包含指定节点类

@receiver l
@param name
@return bool

func (*ClassLoader) New

func (l *ClassLoader) New(name string, cfg *config.NodeCfg) (bcore.INode, error)

New 根据 name 实例化节点

@receiver l
@param name
@param cfg
@return INode
@return error

func (*ClassLoader) Register

func (l *ClassLoader) Register(node bcore.INode)

Register 注册节点类,注册名为节点的struct名称

@receiver l
@param node

func (*ClassLoader) RegisterWithName

func (l *ClassLoader) RegisterWithName(name string, node bcore.INode)

RegisterWithName 根据名字注册节点类

@receiver rsm
@param name 可以为空,为空时使用节点的struct名称
@param c

type ExecutorFun

type ExecutorFun = func(eventType bcore.EventType, delta time.Duration) bcore.Result

ExecutorFun 委托方法签名例

type InitialOption

type InitialOption struct {
	ThreadPool        *ants.Pool     // 线程池 为空则使用默认
	TimerPoolSize     int            // 时间轮池子容量 为0则使用默认
	TimerInterval     time.Duration  // 时间轮帧间隔 为0则使用默认
	TimerNumSlots     int            // 时间槽数量 时间轮第一层总时长=interval*numSlots 为0则使用默认
	LogLevel          zapcore.Level  // 日志级别
	LogDevelopment    bool           // 日志模式是否开发模式
	CustomNodeClass   []bcore.INode  // 用于注册自定义节点类
	ScriptPoolMinSize int            // 脚本引擎池子最小容量
	ScriptPoolMaxSize int            // 脚本引擎池子最大容量
	ScriptPoolApiLib  map[string]any // 需注入到脚本引擎池的api库,最好仅注入一些公共的无状态函数或参数,避免状态副作用
}

type Option

type Option func(option *InitialOption)

func WithCustomNodes

func WithCustomNodes(nodes []bcore.INode) Option

func WithLogDevelopment

func WithLogDevelopment(development bool) Option

func WithLogLevel

func WithLogLevel(level zapcore.Level) Option

func WithScriptPoolApiLib added in v1.0.2

func WithScriptPoolApiLib(api map[string]any) Option

func WithScriptPoolMaxSize added in v1.0.2

func WithScriptPoolMaxSize(size int) Option

func WithScriptPoolMinSize added in v1.0.2

func WithScriptPoolMinSize(size int) Option

func WithThreadPool

func WithThreadPool(pool *ants.Pool) Option

func WithTimerInterval

func WithTimerInterval(interval time.Duration) Option

func WithTimerNumSlots

func WithTimerNumSlots(slots int) Option

func WithTimerPoolSize

func WithTimerPoolSize(size int) Option

type Tree

type Tree struct {
	Root            bcore.IRoot
	Ver             string
	Tag             string
	HaveSubtree     bool
	DynamicSubtrees map[string]task.IDynamicSubtree // 所有动态子树容器,key为tag
}

func (*Tree) Abort added in v1.0.2

func (t *Tree) Abort(brain bcore.IBrain) error

Abort 终止树

若 Root 状态错误将抛出异常,注意调用方应保证 Abort 线程安全以避免 Root 状态读取时为脏数据
@receiver t
@param brain

func (*Tree) Start added in v1.0.2

func (t *Tree) Start(brain bcore.IBrain) error

Start 启动行为树,内部会派发到 bcore.IBrain 实例化时指定的线程

若 Root 状态错误将抛出异常,注意调用方应保证 Start 线程安全以避免 Root 状态读取时为脏数据
@receiver t
@param brain

type TreeRegistry

type TreeRegistry struct {
	TreesByID  map[string]*Tree // 所有树,索引为ID
	TreesByTag map[string]*Tree // 所有树,索引为Tag
	// contains filtered or unexported fields
}

TreeRegistry 行为树注册器

func GlobalTreeRegistry

func GlobalTreeRegistry() *TreeRegistry

func NewTreeRegistry

func NewTreeRegistry() *TreeRegistry

func (*TreeRegistry) Load

func (r *TreeRegistry) Load(cfg *config.TreeCfg) error

nolint:gocyclo Load

@receiver r
@param cfg
@return *Tree
@return error

func (*TreeRegistry) LoadFromJson

func (r *TreeRegistry) LoadFromJson(cfgJson string) error

func (*TreeRegistry) LoadFromJsons

func (r *TreeRegistry) LoadFromJsons(cfgJson []string) error

func (*TreeRegistry) LoadFromPaths

func (r *TreeRegistry) LoadFromPaths(paths []string) error

func (*TreeRegistry) MountSubtree added in v1.0.3

func (r *TreeRegistry) MountSubtree() error

MountSubtree 遍历所有未挂载子树的子树容器,挂载子树

@receiver r
@return error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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