EG/plugins/user/behavior_tree/README.md
2025-12-12 16:16:15 +08:00

87 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 行为树插件使用文档
## 简介
行为树插件提供了一套完整的行为树系统用于实现复杂的AI决策逻辑。行为树是一种广泛应用于游戏开发中的AI编程模式它通过树状结构组织和管理AI的行为逻辑。
## 核心概念
### 节点类型
1. **控制节点**:控制行为树的执行流程
- 选择节点Selector从左到右执行子节点直到有一个成功
- 序列节点Sequence从左到右执行子节点直到有一个失败
- 并行节点Parallel并行执行所有子节点
2. **装饰节点**:修改子节点行为的节点
- 重复节点Repeater重复执行子节点指定次数
- 逆变节点Inverter反转子节点的执行结果
- 成功节点Succeeder总是返回成功
- 失败节点Failure总是返回失败
3. **叶子节点**:执行具体行为的节点
- 条件节点Condition检查条件是否满足
- 动作节点Action执行具体行为
- 等待节点Wait等待指定时间
### 黑板系统
黑板是行为树节点间共享数据的机制,类似于全局变量存储区。
## 使用方法
### 1. 加载和启用插件
```python
# 在主程序中加载插件
plugin_manager.load_plugin("behavior_tree")
plugin_manager.enable_plugin("behavior_tree")
# 获取插件实例
bt_plugin = plugin_manager.get_plugin("behavior_tree")
```
### 2. 创建行为树
```python
# 创建根节点
root = SelectorNode("Root")
# 创建序列节点
sequence = SequenceNode("Patrol")
# 创建条件节点
condition = ConditionNode("NeedPatrol", lambda bb: bb.get("need_patrol", True))
# 创建动作节点
action = ActionNode("PatrolAction", lambda bb: print("执行巡逻") or True)
# 组装行为树结构
sequence.add_child(condition)
sequence.add_child(action)
root.add_child(sequence)
# 创建行为树
tree = bt_plugin.create_behavior_tree("patrol_tree", root)
# 创建黑板
blackboard = bt_plugin.create_blackboard("patrol_blackboard")
# 设置黑板数据
blackboard.set("need_patrol", True)
# 关联黑板到行为树
tree.set_blackboard(blackboard)
```
### 3. 执行行为树
```python
# 执行行为树
result = tree.run()
print(f"执行结果: {result.value}")
```
## 示例
查看 `examples/patrol_example.py` 文件了解完整的使用示例。