2 Star 6 Fork 1

xiyoo0812 / luabt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Lua 行为树

依赖

支持节点类型

  • Failed:总是返回FAIL
  • Invert:反转执行结果
  • Succeed: 总是返回成功
  • Random:随机选择子节点执行
  • Repeat:重复执行子节点,需要注意的是每次重复会跳帧
  • Select:顺序执行,遇到成功返回
  • Sequence:顺序执行,遇到失败返回
  • WSelect:顺序执行,遇到成功返回,子节点按权重先排序
  • WSequence:顺序执行,遇到失败返回,子节点按权重先排序
  • Condition:条件节点,根据条件执行子节点
  • Parallel:并行节点,根据设置的成功和失败条件返回

实现原理

  • 节点状态
luabt = {
    -- Node Status
    WAITING     = 0,    --等待,中间状态,展开子节点返回
    SUCCESS     = 1,    --成功
    FAIL        = 2,    --失败
    RUNNING     = 3,    --运行
}
  • 每次tick从root节点一次展开执行,遇到FAIL或者全部SUCCESS则reset
  • 遇到RUNNING,本次tick结束,下一次从RUNNING继续执行
  • 支持中断机制,中断达成后,回到中断节点继续执行

扩展节点

  • 修饰节点Succeed, Failed, Invert:子节点从框架节点继承,同时实现on_execute接口
function SucceedNode:on_execute(tree)
    return SUCCESS
end
  • 条件节点Condition:子节点从框架节点继承,同时实现on_check接口
function ConditionNode:on_check(tree)
    return true
end
  • 循环节点Repeat:子节点从框架节点继承,同时实现on_check接口控制循环
function RepeatNode:on_check(tree)
    return true
end
  • 普通节点Node:子节点从框架Node节点继承,同时实现run接口
function Flee:run(tree)
    tree.robot.hp = tree.robot.hp + 2
    print(tree.robot.hp, "Flee.....")
    return SUCCESS
end
  • 中断节点:任意节点可以成为中断节点,只需要实现on_interrupt接口
function BtNode:on_interrupt(tree)
    return true
end
  • 组合节点:直接使用系统提供的组合节点

Example

备注: 需要先下载luaoop

lua example/example.lua
MIT License Copyright (c) 2019 xiyoo0812 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于lua的AI行为树框架 展开 收起
Lua
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Lua
1
https://gitee.com/xiyoo0812/luabt.git
git@gitee.com:xiyoo0812/luabt.git
xiyoo0812
luabt
luabt
master

搜索帮助