mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-26 16:48:56 +00:00
修改节点注释,重写文档
This commit is contained in:
604
README.md
604
README.md
@@ -1,75 +1,77 @@
|
|||||||
# 行为树
|
# 行为树
|
||||||
|
|
||||||
一个轻量级、高性能的 TypeScript 行为树库,专为游戏AI和决策系统设计。
|
> 一个简洁、高效的 TypeScript 行为树库。遵循"好品味"设计原则:简单数据结构,消除特殊情况,直接暴露问题。
|
||||||
|
|
||||||
|
[](https://badge.fury.io/js/kunpocc-behaviortree)
|
||||||
|
[](https://opensource.org/licenses/ISC)
|
||||||
|
|
||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
- 🚀 **高性能**: 优化的节点执行机制,最小化运行时开销
|
- 🎯 **简洁设计**: 零废话,直接解决问题
|
||||||
- 🎯 **类型安全**: 完整的 TypeScript 支持,严格的类型检查
|
- 🔧 **类型安全**: 完整 TypeScript 支持
|
||||||
- 🧩 **模块化**: 清晰的节点类型体系,易于扩展
|
- 🚀 **高性能**: 优化的执行机制,最小开销
|
||||||
- 🔄 **记忆节点**: 支持记忆型组合节点,优化复杂决策流程
|
- 🧠 **记忆节点**: 智能状态记忆,避免重复计算
|
||||||
- 📦 **零依赖**: 不依赖任何第三方库
|
- 📦 **零依赖**: 纯净实现,无第三方依赖
|
||||||
- 🎮 **游戏优化**: 专为游戏场景优化的黑板系统和状态管理
|
- 🔄 **状态管理**: 分层黑板系统,数据隔离清晰
|
||||||
|
|
||||||
## 安装
|
## 快速开始
|
||||||
|
|
||||||
|
### 安装
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install kunpocc-behaviortree
|
npm install kunpocc-behaviortree
|
||||||
```
|
```
|
||||||
|
|
||||||
## 快速开始
|
### 基础示例
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import {
|
import {
|
||||||
BehaviorTree,
|
BehaviorTree, Status, Action, Condition,
|
||||||
Action,
|
Sequence, Selector
|
||||||
Condition,
|
|
||||||
Sequence,
|
|
||||||
Selector,
|
|
||||||
Status
|
|
||||||
} from 'kunpocc-behaviortree';
|
} from 'kunpocc-behaviortree';
|
||||||
|
|
||||||
// 定义AI角色
|
// 定义实体
|
||||||
interface Character {
|
interface Enemy {
|
||||||
health: number;
|
health: number;
|
||||||
hasWeapon: boolean;
|
hasWeapon: boolean;
|
||||||
|
position: { x: number, y: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
const character: Character = {
|
const enemy: Enemy = {
|
||||||
health: 80,
|
health: 30,
|
||||||
hasWeapon: true
|
hasWeapon: true,
|
||||||
|
position: { x: 100, y: 200 }
|
||||||
};
|
};
|
||||||
|
|
||||||
// 创建条件节点
|
// 创建行为树
|
||||||
const isHealthLow = new Condition((char: Character) => char.health < 30);
|
const tree = new BehaviorTree(enemy,
|
||||||
const hasWeapon = new Condition((char: Character) => char.hasWeapon);
|
|
||||||
|
|
||||||
// 创建行动节点
|
|
||||||
const flee = new Action(() => {
|
|
||||||
console.log("逃跑!");
|
|
||||||
return Status.SUCCESS;
|
|
||||||
});
|
|
||||||
|
|
||||||
const attack = new Action(() => {
|
|
||||||
console.log("攻击!");
|
|
||||||
return Status.SUCCESS;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 构建行为树:生命值低时逃跑,否则攻击
|
|
||||||
const tree = new BehaviorTree(character,
|
|
||||||
new Selector(
|
new Selector(
|
||||||
new Sequence(isHealthLow, flee),
|
// 生命值低时逃跑
|
||||||
new Sequence(hasWeapon, attack)
|
new Sequence(
|
||||||
|
new Condition((node) => {
|
||||||
|
const entity = node.getEntity<Enemy>();
|
||||||
|
return entity.health < 50;
|
||||||
|
}),
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("血量低,逃跑!");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
})
|
||||||
|
),
|
||||||
|
// 否则攻击
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("发起攻击!");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// 执行行为树
|
// 执行
|
||||||
tree.tick(); // 输出: "攻击!"
|
tree.tick(); // 输出: "血量低,逃跑!"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 基本概念
|
## 核心概念
|
||||||
|
|
||||||
1. 节点状态
|
### 状态类型
|
||||||
```typescript
|
```typescript
|
||||||
enum Status {
|
enum Status {
|
||||||
SUCCESS, // 成功
|
SUCCESS, // 成功
|
||||||
@@ -78,110 +80,460 @@ enum Status {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 节点类型
|
### 节点类型
|
||||||
- **动作节点 (Action)**:执行具体行为的叶子节点
|
- **组合节点**: 控制子节点执行逻辑(Sequence、Selector、Parallel等)
|
||||||
- **组合节点 (Composite)**:控制子节点执行顺序的节点
|
- **装饰节点**: 修饰单个子节点(Inverter、Repeat、Limit等)
|
||||||
- **条件节点 (Condition)**:判断条件的节点
|
- **叶子节点**: 执行具体逻辑(Action、Condition、Wait等)
|
||||||
- **装饰节点 (Decorator)**:修饰其他节点行为的节点
|
|
||||||
|
|
||||||
#### 常用节点
|
## 节点详解
|
||||||
|
|
||||||
1. 组合节点
|
### 组合节点 (Composite)
|
||||||
|
|
||||||
|
#### Sequence - 顺序节点
|
||||||
|
按顺序执行子节点,全部成功才成功:
|
||||||
```typescript
|
```typescript
|
||||||
// 顺序节点:按顺序执行所有子节点,直到遇到失败或运行中的节点
|
new Sequence(
|
||||||
new Sequence(childNode1, childNode2, childNode3);
|
checkAmmo, // 检查弹药
|
||||||
|
aim, // 瞄准
|
||||||
// 选择节点:选择第一个成功或运行中的子节点
|
shoot // 射击
|
||||||
new Selector(childNode1, childNode2, childNode3);
|
)
|
||||||
|
// 只有全部成功才返回SUCCESS
|
||||||
// 并行节点:同时执行所有子节点,全部成功才成功
|
|
||||||
new Parallel(childNode1, childNode2, childNode3);
|
|
||||||
|
|
||||||
// 并行任一成功节点:同时执行所有子节点,任一成功即成功
|
|
||||||
new ParallelAnySuccess(childNode1, childNode2, childNode3);
|
|
||||||
|
|
||||||
// 记忆顺序节点:记住上次执行的位置
|
|
||||||
new MemSequence(childNode1, childNode2, childNode3);
|
|
||||||
|
|
||||||
// 记忆选择节点:记住上次执行的位置
|
|
||||||
new MemSelector(childNode1, childNode2, childNode3);
|
|
||||||
|
|
||||||
// 随机选择节点:随机选择一个子节点执行
|
|
||||||
new RandomSelector(childNode1, childNode2, childNode3);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 动作节点
|
#### Selector - 选择节点
|
||||||
|
选择第一个成功的子节点:
|
||||||
```typescript
|
```typescript
|
||||||
// 行动节点 - 返回指定状态
|
new Selector(
|
||||||
new Action(() => {
|
tryMeleeAttack, // 尝试近战
|
||||||
console.log("执行动作");
|
tryRangedAttack, // 尝试远程
|
||||||
return Status.SUCCESS; // 或 Status.FAILURE, Status.RUNNING
|
retreat // 撤退
|
||||||
});
|
)
|
||||||
|
// 任一成功就返回SUCCESS
|
||||||
// 条件节点 - 检查条件返回成功或失败
|
|
||||||
new Condition((subject) => {
|
|
||||||
return subject.health > 50; // 返回 true 表示成功,false 表示失败
|
|
||||||
});
|
|
||||||
|
|
||||||
// 等待节点
|
|
||||||
new WaitTime(2); // 等待2秒
|
|
||||||
new WaitTicks(5); // 等待5个tick
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 装饰节点
|
#### Parallel - 并行节点
|
||||||
|
同时执行所有子节点,全部成功才成功:
|
||||||
```typescript
|
```typescript
|
||||||
// 反转节点 - 反转子节点的成功/失败状态
|
new Parallel(
|
||||||
new Inverter(childNode);
|
moveToTarget, // 移动到目标
|
||||||
|
playAnimation, // 播放动画
|
||||||
// 重复节点 - 重复执行子节点指定次数
|
updateUI // 更新UI
|
||||||
new Repeat(childNode, 3);
|
)
|
||||||
|
// 任一失败返回FAILURE,有RUNNING返回RUNNING,全部SUCCESS才返回SUCCESS
|
||||||
// 重复直到失败 - 重复执行直到子节点失败
|
|
||||||
new RepeatUntilFailure(childNode, 5);
|
|
||||||
|
|
||||||
// 重复直到成功 - 重复执行直到子节点成功
|
|
||||||
new RepeatUntilSuccess(childNode, 5);
|
|
||||||
|
|
||||||
// 时间限制节点 - 限制子节点执行时间
|
|
||||||
new LimitTime(childNode, 5); // 5秒
|
|
||||||
|
|
||||||
// 次数限制节点 - 限制子节点执行次数
|
|
||||||
new LimitTimes(childNode, 3);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
4. 使用黑板共享数据
|
#### ParallelAnySuccess - 并行任一成功
|
||||||
|
同时执行所有子节点,任一成功就成功:
|
||||||
|
```typescript
|
||||||
|
new ParallelAnySuccess(
|
||||||
|
findCover, // 寻找掩体
|
||||||
|
callForHelp, // 呼叫支援
|
||||||
|
counterAttack // 反击
|
||||||
|
)
|
||||||
|
// 任一SUCCESS就返回SUCCESS
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Memory节点 - 状态记忆
|
||||||
|
记忆节点会记住上次执行位置,避免重复执行:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// MemSequence - 记忆顺序节点
|
||||||
|
new MemSequence(
|
||||||
|
longTask1, // 第一次:SUCCESS,继续下一个
|
||||||
|
longTask2, // 第一次:RUNNING,记住这个位置; 第二次:从longTask2开始继续执行
|
||||||
|
longTask3
|
||||||
|
)
|
||||||
|
|
||||||
|
// MemSelector - 记忆选择节点
|
||||||
|
new MemSelector(
|
||||||
|
expensiveCheck1, // 第一次:FAILURE,继续下一个
|
||||||
|
expensiveCheck2, // 第一次:RUNNING,记住这个位置; 第二次:从expensiveCheck2开始执行
|
||||||
|
fallback // 如果前面都是FAILURE才会执行到这里
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### RandomSelector - 随机选择
|
||||||
|
随机选择一个子节点执行:
|
||||||
|
```typescript
|
||||||
|
new RandomSelector(
|
||||||
|
idleBehavior1,
|
||||||
|
idleBehavior2,
|
||||||
|
idleBehavior3
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 装饰节点 (Decorator)
|
||||||
|
|
||||||
|
#### Inverter - 反转节点
|
||||||
|
反转子节点的成功/失败状态:
|
||||||
|
```typescript
|
||||||
|
new Inverter(
|
||||||
|
new Condition((node) => {
|
||||||
|
const enemy = node.getEntity<Enemy>();
|
||||||
|
return enemy.isAlive;
|
||||||
|
})
|
||||||
|
) // 敌人死亡时返回SUCCESS
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Repeat - 重复节点
|
||||||
|
重复执行子节点指定次数:
|
||||||
|
```typescript
|
||||||
|
new Repeat(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("射击");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
3 // 射击3次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### RepeatUntilSuccess - 重复直到成功
|
||||||
|
```typescript
|
||||||
|
new RepeatUntilSuccess(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("尝试开门");
|
||||||
|
return Math.random() > 0.5 ? Status.SUCCESS : Status.FAILURE;
|
||||||
|
}),
|
||||||
|
5 // 最多尝试5次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### RepeatUntilFailure - 重复直到失败
|
||||||
|
```typescript
|
||||||
|
new RepeatUntilFailure(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("收集资源");
|
||||||
|
return Status.SUCCESS; // 持续收集直到失败
|
||||||
|
}),
|
||||||
|
10 // 最多收集10次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### LimitTime - 时间限制
|
||||||
|
```typescript
|
||||||
|
new LimitTime(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("执行复杂计算");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
2.0 // 最多执行2秒
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### LimitTicks - 次数限制
|
||||||
|
```typescript
|
||||||
|
new LimitTicks(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("尝试操作");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
5 // 最多执行5次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 叶子节点 (Leaf)
|
||||||
|
|
||||||
|
#### Action - 动作节点
|
||||||
|
执行自定义逻辑:
|
||||||
|
```typescript
|
||||||
|
new Action((node) => {
|
||||||
|
// 直接获取实体
|
||||||
|
const target = node.getEntity<Character>();
|
||||||
|
|
||||||
|
// 访问黑板数据
|
||||||
|
const ammo = node.get<number>('ammo');
|
||||||
|
|
||||||
|
if (target && ammo > 0) {
|
||||||
|
console.log("攻击目标");
|
||||||
|
node.set('ammo', ammo - 1);
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}
|
||||||
|
return Status.FAILURE;
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Condition - 条件节点
|
||||||
|
检查条件:
|
||||||
|
```typescript
|
||||||
|
new Condition((node) => {
|
||||||
|
const player = node.getEntity<Player>();
|
||||||
|
const health = player.health;
|
||||||
|
return health > 50; // true->SUCCESS, false->FAILURE
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### WaitTime - 时间等待
|
||||||
|
```typescript
|
||||||
|
new WaitTime(2.5) // 等待2.5秒
|
||||||
|
```
|
||||||
|
|
||||||
|
#### WaitTicks - 帧数等待
|
||||||
|
```typescript
|
||||||
|
new WaitTicks(60) // 等待60帧
|
||||||
|
```
|
||||||
|
|
||||||
|
## 黑板系统
|
||||||
|
|
||||||
|
黑板系统提供分层数据存储,支持数据隔离和查找链:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 在节点中使用黑板
|
// 在节点中使用黑板
|
||||||
class CustomAction extends BTNode {
|
new Action((node) => {
|
||||||
tick: Status {
|
// 直接获取实体
|
||||||
// 获取数据 - 使用节点实例作为命名空间
|
const entity = node.getEntity<Character>();
|
||||||
const data = tree.blackboard.get<string>("key", this);
|
|
||||||
|
|
||||||
// 设置数据 - 使用节点实例作为命名空间
|
// 本地数据(仅当前节点可见)
|
||||||
tree.blackboard.set("key", "value", this);
|
node.set('local_count', 1);
|
||||||
|
const count = node.get<number>('local_count');
|
||||||
|
|
||||||
|
// 树级数据(整棵树可见)
|
||||||
|
node.setRoot('tree_data', 'shared');
|
||||||
|
const shared = node.getRoot<string>('tree_data');
|
||||||
|
|
||||||
|
// 全局数据(所有树可见)
|
||||||
|
node.setGlobal('global_config', config);
|
||||||
|
const config = node.getGlobal<Config>('global_config');
|
||||||
|
|
||||||
return Status.SUCCESS;
|
return Status.SUCCESS;
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### 数据查找链
|
||||||
|
黑板数据按以下顺序查找:
|
||||||
|
1. 当前节点的本地黑板
|
||||||
|
2. 父节点的黑板
|
||||||
|
3. 递归向上查找到根节点
|
||||||
|
|
||||||
|
### Memory节点的数据隔离
|
||||||
|
Memory节点会创建独立的子黑板,确保状态隔离:
|
||||||
|
```typescript
|
||||||
|
const mem1 = new MemSequence(/* ... */);
|
||||||
|
const mem2 = new MemSequence(/* ... */);
|
||||||
|
// mem1 和 mem2 的记忆状态完全独立
|
||||||
|
```
|
||||||
|
|
||||||
|
## 完整示例
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import {
|
||||||
|
BehaviorTree, Status, Action, Condition,
|
||||||
|
Sequence, Selector, MemSelector, Parallel,
|
||||||
|
Inverter, RepeatUntilSuccess, LimitTime
|
||||||
|
} from 'kunpocc-behaviortree';
|
||||||
|
|
||||||
|
interface Character {
|
||||||
|
health: number;
|
||||||
|
mana: number;
|
||||||
|
hasWeapon: boolean;
|
||||||
|
isInCombat: boolean;
|
||||||
|
position: { x: number, y: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const character: Character = {
|
||||||
|
health: 80,
|
||||||
|
mana: 50,
|
||||||
|
hasWeapon: true,
|
||||||
|
isInCombat: false,
|
||||||
|
position: { x: 0, y: 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
// 构建复杂行为树
|
||||||
|
const behaviorTree = new BehaviorTree(character,
|
||||||
|
new Selector(
|
||||||
|
// 战斗行为
|
||||||
|
new Sequence(
|
||||||
|
new Condition((node) => {
|
||||||
|
const char = node.getEntity<Character>();
|
||||||
|
return char.isInCombat;
|
||||||
|
}),
|
||||||
|
new Selector(
|
||||||
|
// 生命值低时治疗
|
||||||
|
new Sequence(
|
||||||
|
new Condition((node) => {
|
||||||
|
const char = node.getEntity<Character>();
|
||||||
|
return char.health < 30;
|
||||||
|
}),
|
||||||
|
new RepeatUntilSuccess(
|
||||||
|
new Action((node) => {
|
||||||
|
const char = node.getEntity<Character>();
|
||||||
|
if (char.mana >= 10) {
|
||||||
|
char.health += 20;
|
||||||
|
char.mana -= 10;
|
||||||
|
console.log("治疗完成");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}
|
||||||
|
return Status.FAILURE;
|
||||||
|
}),
|
||||||
|
3 // 最多尝试3次
|
||||||
|
)
|
||||||
|
),
|
||||||
|
// 正常攻击
|
||||||
|
new Sequence(
|
||||||
|
new Condition((node) => {
|
||||||
|
const char = node.getEntity<Character>();
|
||||||
|
return char.hasWeapon;
|
||||||
|
}),
|
||||||
|
new LimitTime(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("发起攻击");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
1.0 // 攻击最多1秒
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
// 非战斗行为 - 巡逻
|
||||||
|
new MemSelector(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("巡逻点A");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("巡逻点B");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("巡逻点C");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 执行行为树
|
||||||
|
console.log("=== 执行行为树 ===");
|
||||||
|
behaviorTree.tick(); // 输出: "巡逻点A"
|
||||||
|
|
||||||
|
// 进入战斗状态
|
||||||
|
character.isInCombat = true;
|
||||||
|
character.health = 20; // 低血量
|
||||||
|
|
||||||
|
behaviorTree.tick(); // 输出: "治疗完成"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 最佳实践
|
||||||
|
|
||||||
|
### 1. 节点设计原则
|
||||||
|
- **单一职责**: 每个节点只做一件事
|
||||||
|
- **状态明确**: 明确定义SUCCESS/FAILURE/RUNNING的含义
|
||||||
|
- **避免副作用**: 尽量避免节点间的隐式依赖
|
||||||
|
|
||||||
|
### 2. 性能优化
|
||||||
|
```typescript
|
||||||
|
// ✅ 好的做法 - 使用记忆节点避免重复计算
|
||||||
|
new MemSelector(
|
||||||
|
expensivePathfinding, // 复杂寻路只计算一次
|
||||||
|
fallbackBehavior
|
||||||
|
)
|
||||||
|
|
||||||
|
// ❌ 避免 - 每次都重新计算
|
||||||
|
new Selector(
|
||||||
|
expensivePathfinding, // 每次tick都会重新计算
|
||||||
|
fallbackBehavior
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 黑板使用
|
||||||
|
```typescript
|
||||||
|
// ✅ 好的做法 - 合理使用数据层级
|
||||||
|
new Action((node) => {
|
||||||
|
// 获取实体
|
||||||
|
const player = node.getEntity<Player>();
|
||||||
|
|
||||||
|
// 临时数据用本地黑板
|
||||||
|
node.set('temp_result', calculation());
|
||||||
|
|
||||||
|
// 共享数据用树级黑板
|
||||||
|
node.setRoot('current_target', target);
|
||||||
|
|
||||||
|
// 配置数据用全局黑板
|
||||||
|
node.setGlobal('game_config', config);
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. 错误处理
|
||||||
|
```typescript
|
||||||
|
// ✅ 明确的错误处理
|
||||||
|
new Action((node) => {
|
||||||
|
try {
|
||||||
|
const result = riskyOperation();
|
||||||
|
return result ? Status.SUCCESS : Status.FAILURE;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Operation failed:', error);
|
||||||
|
return Status.FAILURE;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试覆盖
|
||||||
|
|
||||||
|
本库包含全面的测试用例,覆盖:
|
||||||
|
- ✅ 17种节点类型 (100%覆盖)
|
||||||
|
- ✅ Memory节点状态管理
|
||||||
|
- ✅ 黑板数据隔离
|
||||||
|
- ✅ 边界条件处理
|
||||||
|
- ✅ 复杂嵌套场景
|
||||||
|
|
||||||
|
运行测试:
|
||||||
|
```bash
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
## API 参考
|
||||||
|
|
||||||
|
### 核心类
|
||||||
|
|
||||||
|
#### `BehaviorTree<T>`
|
||||||
|
```typescript
|
||||||
|
constructor(entity: T, root: IBTNode)
|
||||||
|
tick(): Status // 执行一次行为树
|
||||||
|
reset(): void // 重置所有状态
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `Status`
|
||||||
|
```typescript
|
||||||
|
enum Status {
|
||||||
|
SUCCESS = 0,
|
||||||
|
FAILURE = 1,
|
||||||
|
RUNNING = 2
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 节点接口
|
||||||
|
```typescript
|
||||||
|
interface IBTNode {
|
||||||
|
readonly children: IBTNode[];
|
||||||
|
// 节点黑板
|
||||||
|
local: IBlackboard;
|
||||||
|
tick(): Status;
|
||||||
|
|
||||||
#### 注意事项
|
// 优先写入自己的黑板数据, 如果没有则写入父节点的黑板数据
|
||||||
|
set<T>(key: string, value: T): void;
|
||||||
|
get<T>(key: string): T;
|
||||||
|
// 写入树根节点的黑板数据
|
||||||
|
setRoot<T>(key: string, value: T): void;
|
||||||
|
getRoot<T>(key: string): T;
|
||||||
|
// 写入全局黑板数据
|
||||||
|
setGlobal<T>(key: string, value: T): void;
|
||||||
|
getGlobal<T>(key: string): T;
|
||||||
|
|
||||||
1. 节点状态说明:
|
// 实体访问
|
||||||
- `SUCCESS`:节点执行成功
|
getEntity<T>(): T;
|
||||||
- `FAILURE`:节点执行失败
|
}
|
||||||
- `RUNNING`:节点正在执行中
|
```
|
||||||
2. 组合节点特性:
|
|
||||||
- `Sequence`:所有子节点返回 SUCCESS 才返回 SUCCESS
|
## 许可证
|
||||||
- `Selector`:任一子节点返回 SUCCESS 就返回 SUCCESS
|
|
||||||
- `Parallel`:并行执行所有子节点
|
ISC License - 详见 [LICENSE](LICENSE) 文件
|
||||||
- `MemSequence/MemSelector`:会记住上次执行位置
|
|
||||||
3. 性能优化:
|
## 贡献
|
||||||
- 使用黑板共享数据,避免重复计算
|
|
||||||
- 合理使用记忆节点,减少重复执行
|
欢迎提交 Issue 和 Pull Request。请确保:
|
||||||
- 控制行为树的深度,避免过于复杂
|
1. 代码风格一致
|
||||||
|
2. 添加适当的测试
|
||||||
|
3. 更新相关文档
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*"好的程序员关心数据结构,而不是代码。"* - 这个库遵循简洁设计原则,专注于解决实际问题。
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kunpocc-behaviortree",
|
"name": "kunpocc-behaviortree",
|
||||||
"version": "0.0.3",
|
"version": "0.0.4",
|
||||||
"description": "行为树",
|
"description": "行为树",
|
||||||
"main": "./dist/kunpocc-behaviortree.cjs",
|
"main": "./dist/kunpocc-behaviortree.cjs",
|
||||||
"module": "./dist/kunpocc-behaviortree.mjs",
|
"module": "./dist/kunpocc-behaviortree.mjs",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Status } from "../header";
|
|||||||
export interface IBTNode {
|
export interface IBTNode {
|
||||||
readonly children: IBTNode[];
|
readonly children: IBTNode[];
|
||||||
/** 本节点的的黑板引用 */
|
/** 本节点的的黑板引用 */
|
||||||
blackboard: IBlackboard;
|
local: IBlackboard;
|
||||||
/**
|
/**
|
||||||
* 初始化节点
|
* 初始化节点
|
||||||
* @param root 树根节点的黑板
|
* @param root 树根节点的黑板
|
||||||
@@ -33,6 +33,9 @@ export interface IBTNode {
|
|||||||
*/
|
*/
|
||||||
setGlobal<T>(key: string, value: T): void;
|
setGlobal<T>(key: string, value: T): void;
|
||||||
getGlobal<T>(key: string): T;
|
getGlobal<T>(key: string): T;
|
||||||
|
|
||||||
|
/** 获取关联的实体 */
|
||||||
|
getEntity<T>(): T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -126,6 +129,10 @@ export abstract class BTNode implements IBTNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getEntity<T>(): T {
|
||||||
|
return this._local.getEntity();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置获取全局黑板数据
|
* 设置获取全局黑板数据
|
||||||
*/
|
*/
|
||||||
@@ -162,8 +169,4 @@ export abstract class BTNode implements IBTNode {
|
|||||||
public get local(): IBlackboard {
|
public get local(): IBlackboard {
|
||||||
return this._local;
|
return this._local;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get blackboard(): IBlackboard {
|
|
||||||
return this._local;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,44 +2,50 @@ import { Status } from "../header";
|
|||||||
import { Composite, MemoryComposite } from "./AbstractNodes";
|
import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记忆选择节点
|
* 记忆选择节点 从上到下执行
|
||||||
* 选择不为 FAILURE 的节点,记住上次运行的子节点位置
|
* 遇到 FAILURE 继续下一个
|
||||||
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
|
* 遇到 SUCCESS 返回 SUCCESS 下次重新开始
|
||||||
|
*
|
||||||
|
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||||
*/
|
*/
|
||||||
export class MemSelector extends MemoryComposite {
|
export class MemSelector extends MemoryComposite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||||
for (let i = index; i < this.children.length; i++) {
|
for (let i = index; i < this.children.length; i++) {
|
||||||
let status = this.children[i]!._execute();
|
let status = this.children[i]!._execute();
|
||||||
if (status !== Status.FAILURE) {
|
if (status === Status.FAILURE) {
|
||||||
if (status === Status.RUNNING) {
|
continue;
|
||||||
this.set(`__nMemoryRunningIndex`, i);
|
|
||||||
}
|
}
|
||||||
|
if (status === Status.SUCCESS) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
this.set(`__nMemoryRunningIndex`, i);
|
||||||
|
return Status.RUNNING;
|
||||||
}
|
}
|
||||||
return Status.FAILURE;
|
return Status.FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记忆顺序节点
|
* 记忆顺序节点 从上到下执行
|
||||||
* 如果上次执行到 RUNNING 的节点, 下次进入节点后, 直接从 RUNNING 节点开始
|
* 遇到 SUCCESS 继续下一个
|
||||||
* 遇到 SUCCESS 或者 FAILURE 停止迭代
|
* 遇到 FAILURE 停止迭代 返回 FAILURE 下次重新开始
|
||||||
* 任意一个Child Node返回不为 SUCCESS, 本Node向自己的Parent Node也返回Child Node状态
|
*
|
||||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||||
*/
|
*/
|
||||||
export class MemSequence extends MemoryComposite {
|
export class MemSequence extends MemoryComposite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||||
for (let i = index; i < this.children.length; i++) {
|
for (let i = index; i < this.children.length; i++) {
|
||||||
let status = this.children[i]!._execute();
|
let status = this.children[i]!._execute();
|
||||||
if (status !== Status.SUCCESS) {
|
if (status === Status.SUCCESS) {
|
||||||
if (status === Status.RUNNING) {
|
continue;
|
||||||
|
}
|
||||||
|
if (status === Status.FAILURE) {
|
||||||
|
return Status.FAILURE;
|
||||||
|
}
|
||||||
this.set(`__nMemoryRunningIndex`, i);
|
this.set(`__nMemoryRunningIndex`, i);
|
||||||
}
|
return Status.RUNNING;
|
||||||
return status;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Status.SUCCESS;
|
return Status.SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -47,7 +53,8 @@ export class MemSequence extends MemoryComposite {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机选择节点
|
* 随机选择节点
|
||||||
* 从Child Node中随机选择一个执行
|
* 随机选择一个子节点执行
|
||||||
|
* 返回子节点状态
|
||||||
*/
|
*/
|
||||||
export class RandomSelector extends Composite {
|
export class RandomSelector extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
@@ -62,9 +69,9 @@ export class RandomSelector extends Composite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 选择节点,选择不为 FAILURE 的节点
|
* 选择节点 从上到下执行
|
||||||
* 当执行本Node时,它将从begin到end迭代执行自己的Child Node:
|
* 返回第一个不为 FAILURE 的子节点状态
|
||||||
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
|
* 否则返回 FAILURE
|
||||||
*/
|
*/
|
||||||
export class Selector extends Composite {
|
export class Selector extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
@@ -79,29 +86,26 @@ export class Selector extends Composite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 顺序节点
|
* 顺序节点 从上到下执行
|
||||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
* 遇到 SUCCESS 继续下一个
|
||||||
* 遇到 FAILURE 或 RUNNING, 那停止迭代,返回FAILURE 或 RUNNING
|
* 否则返回子节点状态
|
||||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
|
||||||
*/
|
*/
|
||||||
export class Sequence extends Composite {
|
export class Sequence extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
for (let i = 0; i < this.children.length; i++) {
|
for (let i = 0; i < this.children.length; i++) {
|
||||||
let status = this.children[i]!._execute();
|
let status = this.children[i]!._execute();
|
||||||
if (status !== Status.SUCCESS) {
|
if (status === Status.SUCCESS) {
|
||||||
return status;
|
continue;
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
return Status.SUCCESS;
|
return Status.SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 并行节点 每次进入全部执行一遍
|
* 并行节点 从上到下执行 全部执行一遍
|
||||||
* 它将从begin到end迭代执行自己的Child Node:
|
* 返回优先级 FAILURE > RUNNING > SUCCESS
|
||||||
* 1. 任意子节点返回 FAILURE, 返回 FAILURE
|
|
||||||
* 2. 否则 任意子节点返回 RUNNING, 返回 RUNNING
|
|
||||||
* 3. 全部成功, 才返回 SUCCESS
|
|
||||||
*/
|
*/
|
||||||
export class Parallel extends Composite {
|
export class Parallel extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
@@ -110,11 +114,8 @@ export class Parallel extends Composite {
|
|||||||
let status = this.children[i]!._execute();
|
let status = this.children[i]!._execute();
|
||||||
if (result === Status.FAILURE || status === Status.FAILURE) {
|
if (result === Status.FAILURE || status === Status.FAILURE) {
|
||||||
result = Status.FAILURE;
|
result = Status.FAILURE;
|
||||||
continue;
|
} else if (status === Status.RUNNING) {
|
||||||
}
|
|
||||||
if (status === Status.RUNNING) {
|
|
||||||
result = Status.RUNNING;
|
result = Status.RUNNING;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -122,24 +123,18 @@ export class Parallel extends Composite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 并行节点 每次进入全部重新执行一遍
|
* 并行节点 从上到下执行 全部执行一遍
|
||||||
* 它将从begin到end迭代执行自己的Child Node:
|
* 返回优先级 SUCCESS > RUNNING > FAILURE
|
||||||
* 1. 任意子节点返回 SUCCESS, 返回 SUCCESS
|
|
||||||
* 2. 否则, 任意子节点返回 FAILURE, 返回 FAILURE
|
|
||||||
* 否则返回 RUNNING
|
|
||||||
*/
|
*/
|
||||||
export class ParallelAnySuccess extends Composite {
|
export class ParallelAnySuccess extends Composite {
|
||||||
public tick(): Status {
|
public tick(): Status {
|
||||||
let result = Status.RUNNING;
|
let result = Status.FAILURE;
|
||||||
for (let i = 0; i < this.children.length; i++) {
|
for (let i = 0; i < this.children.length; i++) {
|
||||||
let status = this.children[i]!._execute();
|
let status = this.children[i]!._execute();
|
||||||
if (result === Status.SUCCESS || status === Status.SUCCESS) {
|
if (result === Status.SUCCESS || status === Status.SUCCESS) {
|
||||||
result = Status.SUCCESS;
|
result = Status.SUCCESS;
|
||||||
continue;
|
} else if (status === Status.RUNNING) {
|
||||||
}
|
result = Status.RUNNING;
|
||||||
if (status === Status.FAILURE) {
|
|
||||||
result = Status.FAILURE;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export class BehaviorTree<T> {
|
|||||||
*/
|
*/
|
||||||
private _initializeAllNodeIds(node: IBTNode, parent?: IBTNode): void {
|
private _initializeAllNodeIds(node: IBTNode, parent?: IBTNode): void {
|
||||||
// 设置当前节点ID
|
// 设置当前节点ID
|
||||||
node._initialize(this._blackboard, parent ? parent.blackboard : this._blackboard);
|
node._initialize(this._blackboard, parent ? parent.local : this._blackboard);
|
||||||
// 递归设置所有子节点ID
|
// 递归设置所有子节点ID
|
||||||
for (const child of node.children) {
|
for (const child of node.children) {
|
||||||
this._initializeAllNodeIds(child, node);
|
this._initializeAllNodeIds(child, node);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
* 黑板数据接口
|
* 黑板数据接口
|
||||||
*/
|
*/
|
||||||
export interface IBlackboard {
|
export interface IBlackboard {
|
||||||
|
getEntity<T>(): T;
|
||||||
get<T>(key: string): T;
|
get<T>(key: string): T;
|
||||||
set<T>(key: string, value: T): void;
|
set<T>(key: string, value: T): void;
|
||||||
delete(key: string): void;
|
delete(key: string): void;
|
||||||
@@ -29,8 +30,8 @@ export class Blackboard implements IBlackboard {
|
|||||||
|
|
||||||
/** 实体 */
|
/** 实体 */
|
||||||
private readonly _entity: any;
|
private readonly _entity: any;
|
||||||
public get entity(): any {
|
public getEntity<T>(): T {
|
||||||
return this._entity || this.parent?.entity;
|
return this._entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(parent?: Blackboard, entity?: any) {
|
constructor(parent?: Blackboard, entity?: any) {
|
||||||
@@ -38,8 +39,8 @@ export class Blackboard implements IBlackboard {
|
|||||||
if (parent) {
|
if (parent) {
|
||||||
parent.children.add(this);
|
parent.children.add(this);
|
||||||
}
|
}
|
||||||
|
// 优先使用传入的 entity,如果没有则从父级继承
|
||||||
this._entity = entity;
|
this._entity = entity !== undefined ? entity : (parent?._entity ?? null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 核心: 查找链实现 */
|
/** 核心: 查找链实现 */
|
||||||
|
|||||||
Reference in New Issue
Block a user