behavior

package module
v1.1.15 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2023 License: MIT Imports: 24 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 SetGlobalTreeRegistry added in v1.0.7

func SetGlobalTreeRegistry(registry *TreeRegistry)

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) Abort added in v1.0.7

func (b *Brain) Abort(abortChan chan *bcore.FinishEvent)

Abort @implement bcore.IBrain .Abort

@receiver b @param abortChan

func (*Brain) After added in v1.1.3

func (b *Brain) After(interval time.Duration, randomDeviation time.Duration, task func()) *timingwheel.Timer

After wrap timingwheel.TimingWheel .AfterFunc

@param interval 间隔
@param randomDeviation 随机方差范围 interval = interval + randomDeviation*[-0.5,0.5)
@param task
@param opts

func (*Brain) Blackboard

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

func (*Brain) Context added in v1.1.2

func (b *Brain) Context() context.Context

func (*Brain) Cron added in v1.1.3

func (b *Brain) Cron(interval time.Duration, randomDeviation time.Duration, task func()) *timingwheel.Timer

Cron wrap timingwheel.TimingWheel .Cron

@param interval 间隔
@param randomDeviation 随机方差范围,interval=interval+randomDeviation*[-0.5,0.5)
@param task
@param opts

func (*Brain) DynamicDecorate added in v1.0.7

func (b *Brain) DynamicDecorate(containerTag string, subtreeTag string) error

DynamicDecorate 给正在运行的树动态挂载子树.暂时只支持主树上的动态容器

非线程安全,调用方自己保证

@receiver b @param containerTag 动态子树容器的tag @param subtreeTag 子树的tag @return error

func (*Brain) FinishChan added in v1.0.3

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

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) ID added in v1.1.12

func (b *Brain) ID() int

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) Run added in v1.0.7

func (b *Brain) Run(tag string, force bool)

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) SetContext added in v1.1.2

func (b *Brain) SetContext(ctx context.Context)

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) Clone added in v1.0.7

func (l *ClassLoader) Clone(node bcore.INode) (bcore.INode, error)

Clone 克隆一个节点

@receiver l @param node @return bcore.INode @return error

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
	StaticSubtrees    map[string]task.ISubtree        // 所有静态子树容器,索引为id
	DynamicSubtrees   map[string]task.IDynamicSubtree // 所有动态子树容器,key为tag
	AllSubtreeMounted bool                            // 是否所有子树已经全部挂载完(不包括childTag为空的)
}

type TreeRegistry

type TreeRegistry struct {
	TreesByID  map[string]*Tree   // 所有树,索引为 IRoot.ID
	TreesByTag map[string][]*Tree // 所有树,索引为 IRoot.Tag
}

TreeRegistry 行为树注册器

func GlobalTreeRegistry

func GlobalTreeRegistry() *TreeRegistry

func NewTreeRegistry

func NewTreeRegistry() *TreeRegistry

func (*TreeRegistry) CloneAndReg added in v1.0.9

func (r *TreeRegistry) CloneAndReg(src *Tree) (*Tree, error)

CloneAndReg 拷贝树并注册

@receiver r @param src @return error

func (*TreeRegistry) GetNotParentTreeWithoutClone added in v1.0.7

func (r *TreeRegistry) GetNotParentTreeWithoutClone(tag string) *Tree

GetNotParentTreeWithoutClone 获取一个还未分配静态父节点的树,多用于获取该tag的主树.

@receiver r @param tag @return *Tree

func (*TreeRegistry) Load

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

Load 加载树,加载前请务必:1.停止使用该树运行的AI 2.移除该树旧版及其关联树(该树的静态子树和动态子树)

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

func (*TreeRegistry) LoadFromJson

func (r *TreeRegistry) LoadFromJson(cfgJson []byte) error

func (*TreeRegistry) LoadFromJsons

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

func (*TreeRegistry) LoadFromPaths

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

func (*TreeRegistry) MountAll added in v1.0.7

func (r *TreeRegistry) MountAll() error

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

@receiver r
@return error

func (*TreeRegistry) Remove added in v1.0.7

func (r *TreeRegistry) Remove(tag string)

Remove 根据tag移除树,移除前请务必:1.停止使用该树运行的AI 2.同时移除关联树(该树的静态子树和动态子树)

@receiver r @param tag

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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