mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-27 00:58:18 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10ca8fd2a8 | ||
|
|
2ab47b2a7b | ||
|
|
7ed015c6bf | ||
|
|
e9a0a15035 |
640
README.md
640
README.md
@@ -1,187 +1,539 @@
|
||||
# 行为树
|
||||
|
||||
一个轻量级、高性能的 TypeScript 行为树库,专为游戏AI和决策系统设计。
|
||||
> 一个简洁、高效的 TypeScript 行为树库。遵循"好品味"设计原则:简单数据结构,消除特殊情况,直接暴露问题。
|
||||
|
||||
[](https://badge.fury.io/js/kunpocc-behaviortree)
|
||||
[](https://opensource.org/licenses/ISC)
|
||||
|
||||
## 特性
|
||||
|
||||
- 🚀 **高性能**: 优化的节点执行机制,最小化运行时开销
|
||||
- 🎯 **类型安全**: 完整的 TypeScript 支持,严格的类型检查
|
||||
- 🧩 **模块化**: 清晰的节点类型体系,易于扩展
|
||||
- 🔄 **记忆节点**: 支持记忆型组合节点,优化复杂决策流程
|
||||
- 📦 **零依赖**: 不依赖任何第三方库
|
||||
- 🎮 **游戏优化**: 专为游戏场景优化的黑板系统和状态管理
|
||||
- 🎯 **简洁设计**: 零废话,直接解决问题
|
||||
- 🔧 **类型安全**: 完整 TypeScript 支持
|
||||
- 🚀 **高性能**: 优化的执行机制,最小开销
|
||||
- 🧠 **记忆节点**: 智能状态记忆,避免重复计算
|
||||
- 📦 **零依赖**: 纯净实现,无第三方依赖
|
||||
- 🔄 **状态管理**: 分层黑板系统,数据隔离清晰
|
||||
|
||||
## 安装
|
||||
## 快速开始
|
||||
|
||||
### 安装
|
||||
|
||||
```bash
|
||||
npm install kunpocc-behaviortree
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
### 基础示例
|
||||
|
||||
```typescript
|
||||
import {
|
||||
BehaviorTree,
|
||||
Action,
|
||||
Condition,
|
||||
Sequence,
|
||||
Selector,
|
||||
Status
|
||||
BehaviorTree, Status, Action, Condition,
|
||||
Sequence, Selector
|
||||
} from 'kunpocc-behaviortree';
|
||||
|
||||
// 定义AI角色
|
||||
interface Character {
|
||||
// 定义实体
|
||||
interface Enemy {
|
||||
health: number;
|
||||
hasWeapon: boolean;
|
||||
position: { x: number, y: number };
|
||||
}
|
||||
|
||||
const enemy: Enemy = {
|
||||
health: 30,
|
||||
hasWeapon: true,
|
||||
position: { x: 100, y: 200 }
|
||||
};
|
||||
|
||||
// 创建行为树
|
||||
const tree = new BehaviorTree(enemy,
|
||||
new Selector(
|
||||
// 生命值低时逃跑
|
||||
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(); // 输出: "血量低,逃跑!"
|
||||
```
|
||||
|
||||
## 核心概念
|
||||
|
||||
### 状态类型
|
||||
```typescript
|
||||
enum Status {
|
||||
SUCCESS, // 成功
|
||||
FAILURE, // 失败
|
||||
RUNNING // 运行中
|
||||
}
|
||||
```
|
||||
|
||||
### 节点类型
|
||||
- **组合节点**: 控制子节点执行逻辑(Sequence、Selector、Parallel等)
|
||||
- **装饰节点**: 修饰单个子节点(Inverter、Repeat、Limit等)
|
||||
- **叶子节点**: 执行具体逻辑(Action、Condition、Wait等)
|
||||
|
||||
## 节点详解
|
||||
|
||||
### 组合节点 (Composite)
|
||||
|
||||
#### Sequence - 顺序节点
|
||||
按顺序执行子节点,全部成功才成功:
|
||||
```typescript
|
||||
new Sequence(
|
||||
checkAmmo, // 检查弹药
|
||||
aim, // 瞄准
|
||||
shoot // 射击
|
||||
)
|
||||
// 只有全部成功才返回SUCCESS
|
||||
```
|
||||
|
||||
#### Selector - 选择节点
|
||||
选择第一个成功的子节点:
|
||||
```typescript
|
||||
new Selector(
|
||||
tryMeleeAttack, // 尝试近战
|
||||
tryRangedAttack, // 尝试远程
|
||||
retreat // 撤退
|
||||
)
|
||||
// 任一成功就返回SUCCESS
|
||||
```
|
||||
|
||||
#### Parallel - 并行节点
|
||||
同时执行所有子节点,全部成功才成功:
|
||||
```typescript
|
||||
new Parallel(
|
||||
moveToTarget, // 移动到目标
|
||||
playAnimation, // 播放动画
|
||||
updateUI // 更新UI
|
||||
)
|
||||
// 任一失败返回FAILURE,有RUNNING返回RUNNING,全部SUCCESS才返回SUCCESS
|
||||
```
|
||||
|
||||
#### 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
|
||||
// 在节点中使用黑板
|
||||
new Action((node) => {
|
||||
// 直接获取实体
|
||||
const entity = node.getEntity<Character>();
|
||||
|
||||
// 本地数据(仅当前节点可见)
|
||||
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;
|
||||
})
|
||||
```
|
||||
|
||||
### 数据查找链
|
||||
黑板数据按以下顺序查找:
|
||||
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,
|
||||
hasWeapon: true
|
||||
mana: 50,
|
||||
hasWeapon: true,
|
||||
isInCombat: false,
|
||||
position: { x: 0, y: 0 }
|
||||
};
|
||||
|
||||
// 创建条件节点
|
||||
const isHealthLow = new Condition((char: Character) => char.health < 30);
|
||||
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,
|
||||
// 构建复杂行为树
|
||||
const behaviorTree = new BehaviorTree(character,
|
||||
new Selector(
|
||||
new Sequence(isHealthLow, flee),
|
||||
new Sequence(hasWeapon, attack)
|
||||
// 战斗行为
|
||||
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;
|
||||
})
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// 执行行为树
|
||||
tree.tick(); // 输出: "攻击!"
|
||||
console.log("=== 执行行为树 ===");
|
||||
behaviorTree.tick(); // 输出: "巡逻点A"
|
||||
|
||||
// 进入战斗状态
|
||||
character.isInCombat = true;
|
||||
character.health = 20; // 低血量
|
||||
|
||||
behaviorTree.tick(); // 输出: "治疗完成"
|
||||
```
|
||||
|
||||
#### 基本概念
|
||||
## 最佳实践
|
||||
|
||||
1. 节点状态
|
||||
### 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, // 成功
|
||||
FAILURE, // 失败
|
||||
RUNNING // 运行中
|
||||
SUCCESS = 0,
|
||||
FAILURE = 1,
|
||||
RUNNING = 2
|
||||
}
|
||||
```
|
||||
|
||||
2. 节点类型
|
||||
- **动作节点 (Action)**:执行具体行为的叶子节点
|
||||
- **组合节点 (Composite)**:控制子节点执行顺序的节点
|
||||
- **条件节点 (Condition)**:判断条件的节点
|
||||
- **装饰节点 (Decorator)**:修饰其他节点行为的节点
|
||||
### 节点接口
|
||||
```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;
|
||||
|
||||
// 实体访问
|
||||
getEntity<T>(): T;
|
||||
}
|
||||
```
|
||||
|
||||
#### 常用节点
|
||||
## 许可证
|
||||
|
||||
1. 组合节点
|
||||
ISC License - 详见 [LICENSE](LICENSE) 文件
|
||||
|
||||
```typescript
|
||||
// 顺序节点:按顺序执行所有子节点,直到遇到失败或运行中的节点
|
||||
new Sequence(childNode1, childNode2, childNode3);
|
||||
|
||||
// 选择节点:选择第一个成功或运行中的子节点
|
||||
new Selector(childNode1, childNode2, childNode3);
|
||||
|
||||
// 并行节点:同时执行所有子节点,全部成功才成功
|
||||
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. 动作节点
|
||||
欢迎提交 Issue 和 Pull Request。请确保:
|
||||
1. 代码风格一致
|
||||
2. 添加适当的测试
|
||||
3. 更新相关文档
|
||||
|
||||
```typescript
|
||||
// 行动节点 - 返回指定状态
|
||||
new Action(() => {
|
||||
console.log("执行动作");
|
||||
return Status.SUCCESS; // 或 Status.FAILURE, Status.RUNNING
|
||||
});
|
||||
|
||||
// 条件节点 - 检查条件返回成功或失败
|
||||
new Condition((subject) => {
|
||||
return subject.health > 50; // 返回 true 表示成功,false 表示失败
|
||||
});
|
||||
|
||||
// 等待节点
|
||||
new WaitTime(2); // 等待2秒
|
||||
new WaitTicks(5); // 等待5个tick
|
||||
```
|
||||
---
|
||||
|
||||
3. 装饰节点
|
||||
|
||||
```typescript
|
||||
// 反转节点 - 反转子节点的成功/失败状态
|
||||
new Inverter(childNode);
|
||||
|
||||
// 重复节点 - 重复执行子节点指定次数
|
||||
new Repeat(childNode, 3);
|
||||
|
||||
// 重复直到失败 - 重复执行直到子节点失败
|
||||
new RepeatUntilFailure(childNode, 5);
|
||||
|
||||
// 重复直到成功 - 重复执行直到子节点成功
|
||||
new RepeatUntilSuccess(childNode, 5);
|
||||
|
||||
// 时间限制节点 - 限制子节点执行时间
|
||||
new LimitTime(childNode, 5); // 5秒
|
||||
|
||||
// 次数限制节点 - 限制子节点执行次数
|
||||
new LimitTimes(childNode, 3);
|
||||
```
|
||||
|
||||
4. 使用黑板共享数据
|
||||
|
||||
```typescript
|
||||
// 在节点中使用黑板
|
||||
class CustomAction extends BaseNode {
|
||||
tick<T>(tree: BehaviorTree<T>): Status {
|
||||
// 获取数据 - 使用节点实例作为命名空间
|
||||
const data = tree.blackboard.get<string>("key", this);
|
||||
|
||||
// 设置数据 - 使用节点实例作为命名空间
|
||||
tree.blackboard.set("key", "value", this);
|
||||
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
#### 注意事项
|
||||
|
||||
1. 节点状态说明:
|
||||
- `SUCCESS`:节点执行成功
|
||||
- `FAILURE`:节点执行失败
|
||||
- `RUNNING`:节点正在执行中
|
||||
2. 组合节点特性:
|
||||
- `Sequence`:所有子节点返回 SUCCESS 才返回 SUCCESS
|
||||
- `Selector`:任一子节点返回 SUCCESS 就返回 SUCCESS
|
||||
- `Parallel`:并行执行所有子节点
|
||||
- `MemSequence/MemSelector`:会记住上次执行位置
|
||||
3. 性能优化:
|
||||
- 使用黑板共享数据,避免重复计算
|
||||
- 合理使用记忆节点,减少重复执行
|
||||
- 控制行为树的深度,避免过于复杂
|
||||
*"好的程序员关心数据结构,而不是代码。"* - 这个库遵循简洁设计原则,专注于解决实际问题。
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kunpocc-behaviortree",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.6",
|
||||
"description": "行为树",
|
||||
"main": "./dist/kunpocc-behaviortree.cjs",
|
||||
"module": "./dist/kunpocc-behaviortree.mjs",
|
||||
|
||||
@@ -5,7 +5,7 @@ import dts from 'rollup-plugin-dts';
|
||||
export default [
|
||||
{
|
||||
// 生成未压缩的 JS 文件
|
||||
input: 'src/kunpocc-behaviortree.ts',
|
||||
input: 'src/index.ts',
|
||||
external: ['cc', 'fairygui-cc'],
|
||||
output: [
|
||||
{
|
||||
@@ -38,7 +38,7 @@ export default [
|
||||
},
|
||||
{
|
||||
// 生成压缩的 JS 文件
|
||||
input: 'src/kunpocc-behaviortree.ts',
|
||||
input: 'src/index.ts',
|
||||
external: ['cc', 'fairygui-cc'],
|
||||
output: [
|
||||
{
|
||||
@@ -72,7 +72,7 @@ export default [
|
||||
},
|
||||
{
|
||||
// 生成声明文件的配置
|
||||
input: 'src/kunpocc-behaviortree.ts',
|
||||
input: 'src/index.ts',
|
||||
output: {
|
||||
file: 'dist/kunpocc-behaviortree.d.ts',
|
||||
format: 'es'
|
||||
|
||||
@@ -4,67 +4,70 @@
|
||||
* @Description: 抽象节点基类
|
||||
*/
|
||||
|
||||
import { BehaviorTree } from "../BehaviorTree";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { IBlackboard } from "../Blackboard";
|
||||
import { BTNode, IBTNode } from "./BTNode";
|
||||
|
||||
/**
|
||||
* 可以包含多个节点的集合装饰器基类
|
||||
* 叶子节点 基类
|
||||
* 没有子节点
|
||||
*/
|
||||
export abstract class Composite extends BaseNode {
|
||||
constructor(...children: BaseNode[]) {
|
||||
super(children);
|
||||
export abstract class LeafNode extends BTNode {
|
||||
constructor() {
|
||||
super([]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修饰节点基类
|
||||
* 只能包含一个子节点
|
||||
* 修饰节点 基类
|
||||
* 有且仅有一个子节点
|
||||
*/
|
||||
export abstract class Decorator extends BaseNode {
|
||||
constructor(child: BaseNode) {
|
||||
export abstract class Decorator extends BTNode {
|
||||
constructor(child: IBTNode) {
|
||||
super([child]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数值型装饰节点基类
|
||||
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的装饰节点
|
||||
* 组合节点 基类
|
||||
* 多个子节点
|
||||
*/
|
||||
export abstract class Composite extends BTNode {
|
||||
constructor(...children: IBTNode[]) {
|
||||
super(children);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数值型修饰节点 基类
|
||||
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的修饰节点
|
||||
*/
|
||||
export abstract class NumericDecorator extends Decorator {
|
||||
protected readonly _max: number;
|
||||
protected _value: number = 0;
|
||||
|
||||
constructor(child: BaseNode, max: number = 1) {
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记忆装饰节点基类
|
||||
* 记忆修饰节点基类
|
||||
* 只有记忆节点才需要设置局部数据
|
||||
*/
|
||||
export abstract class MemoryComposite extends Composite {
|
||||
protected runningIndex = 0;
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
// 检查是否需要重置记忆
|
||||
const shouldReset = tree.blackboard.get(`reset_memory`, this);
|
||||
if (shouldReset) {
|
||||
this.runningIndex = 0;
|
||||
tree.blackboard.delete(`reset_memory`, this);
|
||||
}
|
||||
public override _initialize(global: IBlackboard, branch: IBlackboard): void {
|
||||
super._initialize(global, branch);
|
||||
this._local = branch.createChild();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置记忆状态,下次执行时将从第一个子节点开始
|
||||
*/
|
||||
public resetMemory(): void {
|
||||
this.runningIndex = 0;
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this.set(`__nMemoryRunningIndex`, 0);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { LeafNode } from "./AbstractNodes";
|
||||
import { IBTNode } from "./BTNode";
|
||||
|
||||
export class Action extends BaseNode {
|
||||
protected _func: (subject?: any) => Status;
|
||||
constructor(func: (subject?: any) => Status) {
|
||||
export class Action extends LeafNode {
|
||||
protected _func: (node: IBTNode) => Status;
|
||||
constructor(func: (node: IBTNode) => Status) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
return this._func?.(tree.subject) ?? Status.SUCCESS;
|
||||
public tick(): Status {
|
||||
return this._func?.(this) ?? Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export class Action extends BaseNode {
|
||||
* 次数内,返回RUNNING
|
||||
* 超次,返回SUCCESS
|
||||
*/
|
||||
export class WaitTicks extends BaseNode {
|
||||
export class WaitTicks extends LeafNode {
|
||||
private _max: number;
|
||||
private _value: number;
|
||||
|
||||
@@ -29,12 +29,12 @@ export class WaitTicks extends BaseNode {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
if (++this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
@@ -46,7 +46,7 @@ export class WaitTicks extends BaseNode {
|
||||
* 时间等待节点 时间(秒)
|
||||
* 时间到后返回SUCCESS,否则返回RUNNING
|
||||
*/
|
||||
export class WaitTime extends BaseNode {
|
||||
export class WaitTime extends LeafNode {
|
||||
private _max: number;
|
||||
private _value: number = 0;
|
||||
constructor(duration: number = 0) {
|
||||
@@ -54,12 +54,12 @@ export class WaitTime extends BaseNode {
|
||||
this._max = duration * 1000;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = new Date().getTime();
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
const currTime = new Date().getTime();
|
||||
if (currTime - this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
|
||||
149
src/behaviortree/BTNode/BTNode.ts
Normal file
149
src/behaviortree/BTNode/BTNode.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import { globalBlackboard, IBlackboard } from "../Blackboard";
|
||||
import { Status } from "../header";
|
||||
|
||||
export interface IBTNode {
|
||||
readonly children: IBTNode[];
|
||||
/** 本节点的的黑板引用 */
|
||||
local: IBlackboard;
|
||||
/**
|
||||
* 初始化节点
|
||||
* @param root 树根节点的黑板
|
||||
* @param parent 父节点的黑板
|
||||
*/
|
||||
_initialize(root: IBlackboard, parent: IBlackboard): void;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
_execute(): Status;
|
||||
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;
|
||||
|
||||
/** 获取关联的实体 */
|
||||
getEntity<T>(): T;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 基础节点
|
||||
* 每个节点只管理自己需要的状态
|
||||
*/
|
||||
export abstract class BTNode implements IBTNode {
|
||||
public readonly children: IBTNode[];
|
||||
|
||||
/** 树根节点的黑板引用 */
|
||||
protected _root!: IBlackboard;
|
||||
|
||||
/** 本节点的的黑板引用 可能等于 _parent */
|
||||
protected _local!: IBlackboard;
|
||||
|
||||
constructor(children?: IBTNode[]) {
|
||||
this.children = children ? [...children] : [];
|
||||
}
|
||||
|
||||
public _initialize(root: IBlackboard, parent: IBlackboard): void {
|
||||
this._root = root;
|
||||
// 在需要的节点中重写,创建新的local
|
||||
this._local = parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public _execute(): Status {
|
||||
// 首次执行时初始化
|
||||
const isRunning = this._local.openNodes.get(this) || false;
|
||||
if (!isRunning) {
|
||||
this._local.openNodes.set(this, true);
|
||||
this.open();
|
||||
}
|
||||
|
||||
// 执行核心逻辑
|
||||
const status = this.tick();
|
||||
|
||||
// 执行完成时清理
|
||||
if (status !== Status.RUNNING) {
|
||||
this._local.openNodes.delete(this);
|
||||
this.close();
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化节点(首次执行时调用)
|
||||
* 子类重写此方法进行状态初始化
|
||||
*/
|
||||
protected open(): void { }
|
||||
|
||||
/**
|
||||
* 执行节点逻辑
|
||||
* 子类必须实现此方法
|
||||
* @returns 执行状态
|
||||
*/
|
||||
public abstract tick(): Status;
|
||||
|
||||
/**
|
||||
* 清理节点(执行完成时调用)
|
||||
* 子类重写此方法进行状态清理
|
||||
*/
|
||||
protected close(): void { }
|
||||
|
||||
public getEntity<T>(): T {
|
||||
return this._local.getEntity();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置获取全局黑板数据
|
||||
*/
|
||||
public set<T>(key: string, value: T): void {
|
||||
this._local.set(key, value);
|
||||
}
|
||||
|
||||
public get<T>(key: string): T {
|
||||
return this._local.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置获取树根节点的黑板数据
|
||||
*/
|
||||
public setRoot<T>(key: string, value: T): void {
|
||||
this._root.set(key, value);
|
||||
}
|
||||
|
||||
public getRoot<T>(key: string): T {
|
||||
return this._root.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局黑板数据
|
||||
*/
|
||||
public setGlobal<T>(key: string, value: T): void {
|
||||
globalBlackboard.set(key, value);
|
||||
}
|
||||
|
||||
public getGlobal<T>(key: string): T {
|
||||
return globalBlackboard.get(key);
|
||||
}
|
||||
|
||||
public get local(): IBlackboard {
|
||||
return this._local;
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
import { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
|
||||
|
||||
/**
|
||||
* 基础节点
|
||||
* 每个节点只管理自己需要的状态
|
||||
*/
|
||||
export abstract class BaseNode {
|
||||
public readonly children: BaseNode[];
|
||||
private _id: string;
|
||||
private _isRunning: boolean;
|
||||
|
||||
set id(id: string) { this._id = id; }
|
||||
get id(): string { return this._id }
|
||||
|
||||
/**
|
||||
* 创建
|
||||
* @param children 子节点列表
|
||||
*/
|
||||
constructor(children?: BaseNode[]) {
|
||||
this._id = ""; // 临时值,将在树构造时被正确设置
|
||||
this.children = children ? [...children] : [];
|
||||
this._isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行节点
|
||||
* @param tree 行为树
|
||||
* @returns 状态
|
||||
*/
|
||||
public _execute<T>(tree: BehaviorTree<T>): Status {
|
||||
// 首次执行时初始化
|
||||
if (!this._isRunning) {
|
||||
this._isRunning = true;
|
||||
this.initialize(tree);
|
||||
}
|
||||
|
||||
// 执行核心逻辑
|
||||
const status = this.tick(tree);
|
||||
|
||||
// 执行完成时清理
|
||||
if (status !== Status.RUNNING) {
|
||||
this._isRunning = false;
|
||||
this.cleanup(tree);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化节点(首次执行时调用)
|
||||
* 子类重写此方法进行状态初始化
|
||||
* @param tree 行为树
|
||||
*/
|
||||
protected initialize<T>(tree: BehaviorTree<T>): void { }
|
||||
|
||||
/**
|
||||
* 清理节点(执行完成时调用)
|
||||
* 子类重写此方法进行状态清理
|
||||
* @param tree 行为树
|
||||
*/
|
||||
protected cleanup<T>(tree: BehaviorTree<T>): void { }
|
||||
|
||||
/**
|
||||
* 执行节点逻辑
|
||||
* 子类必须实现此方法
|
||||
* @param tree 行为树
|
||||
* @returns 执行状态
|
||||
*/
|
||||
public abstract tick<T>(tree: BehaviorTree<T>): Status;
|
||||
|
||||
/**
|
||||
* 递归清理节点及其所有子节点的状态
|
||||
* 用于行为树中断时清理所有节点状态
|
||||
*/
|
||||
public cleanupAll(): void {
|
||||
// 清理基础状态
|
||||
this._isRunning = false;
|
||||
|
||||
// 递归清理所有子节点
|
||||
for (const child of this.children) {
|
||||
child.cleanupAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +1,51 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||
|
||||
/**
|
||||
* 记忆选择节点
|
||||
* 选择不为 FAILURE 的节点,记住上次运行的子节点位置
|
||||
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
|
||||
* 记忆选择节点 从上到下执行
|
||||
* 遇到 FAILURE 继续下一个
|
||||
* 遇到 SUCCESS 返回 SUCCESS 下次重新开始
|
||||
*
|
||||
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||
*/
|
||||
export class MemSelector extends MemoryComposite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.FAILURE) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
}
|
||||
public tick(): Status {
|
||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||
for (let i = index; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (status === Status.FAILURE) {
|
||||
continue;
|
||||
}
|
||||
if (status === Status.SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
this.set(`__nMemoryRunningIndex`, i);
|
||||
return Status.RUNNING;
|
||||
}
|
||||
return Status.FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记忆顺序节点
|
||||
* 如果上次执行到 RUNNING 的节点, 下次进入节点后, 直接从 RUNNING 节点开始
|
||||
* 遇到 SUCCESS 或者 FAILURE 停止迭代
|
||||
* 任意一个Child Node返回不为 SUCCESS, 本Node向自己的Parent Node也返回Child Node状态
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
* 记忆顺序节点 从上到下执行
|
||||
* 遇到 SUCCESS 继续下一个
|
||||
* 遇到 FAILURE 停止迭代 返回 FAILURE 下次重新开始
|
||||
*
|
||||
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||
*/
|
||||
export class MemSequence extends MemoryComposite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.SUCCESS) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
}
|
||||
return status;
|
||||
public tick(): Status {
|
||||
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||
for (let i = index; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (status === Status.SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
if (status === Status.FAILURE) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
this.set(`__nMemoryRunningIndex`, i);
|
||||
return Status.RUNNING;
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
@@ -46,29 +53,30 @@ export class MemSequence extends MemoryComposite {
|
||||
|
||||
/**
|
||||
* 随机选择节点
|
||||
* 从Child Node中随机选择一个执行
|
||||
* 随机选择一个子节点执行
|
||||
* 返回子节点状态
|
||||
*/
|
||||
export class RandomSelector extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
if (this.children.length === 0) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
const childIndex = Math.floor(Math.random() * this.children.length);
|
||||
const status = this.children[childIndex]!._execute(tree);
|
||||
const status = this.children[childIndex]!._execute();
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择节点,选择不为 FAILURE 的节点
|
||||
* 当执行本Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
|
||||
* 选择节点 从上到下执行
|
||||
* 返回第一个不为 FAILURE 的子节点状态
|
||||
* 否则返回 FAILURE
|
||||
*/
|
||||
export class Selector extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
let status = this.children[i]!._execute();
|
||||
if (status !== Status.FAILURE) {
|
||||
return status;
|
||||
}
|
||||
@@ -78,38 +86,35 @@ export class Selector extends Composite {
|
||||
}
|
||||
|
||||
/**
|
||||
* 顺序节点
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 遇到 FAILURE 或 RUNNING, 那停止迭代,返回FAILURE 或 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
* 顺序节点 从上到下执行
|
||||
* 遇到 SUCCESS 继续下一个
|
||||
* 否则返回子节点状态
|
||||
*/
|
||||
export class Sequence extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.SUCCESS) {
|
||||
return status;
|
||||
let status = this.children[i]!._execute();
|
||||
if (status === Status.SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
* 并行节点 从上到下执行 全部执行一遍
|
||||
* 返回优先级 FAILURE > RUNNING > SUCCESS
|
||||
*/
|
||||
export class Parallel extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
let result = Status.SUCCESS;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (result === Status.FAILURE || status === Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.SUCCESS && status == Status.RUNNING) {
|
||||
} else if (status === Status.RUNNING) {
|
||||
result = Status.RUNNING;
|
||||
}
|
||||
}
|
||||
@@ -118,21 +123,18 @@ export class Parallel extends Composite {
|
||||
}
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 任意 Child Node 返回 SUCCESS, 本节点返回 SUCCESS
|
||||
* 否则返回 RUNNING
|
||||
* 并行节点 从上到下执行 全部执行一遍
|
||||
* 返回优先级 SUCCESS > RUNNING > FAILURE
|
||||
*/
|
||||
export class ParallelAnySuccess extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
let result = Status.RUNNING;
|
||||
public tick(): Status {
|
||||
let result = Status.FAILURE;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.RUNNING && status == Status.SUCCESS) {
|
||||
let status = this.children[i]!._execute();
|
||||
if (result === Status.SUCCESS || status === Status.SUCCESS) {
|
||||
result = Status.SUCCESS;
|
||||
} else if (status === Status.RUNNING) {
|
||||
result = Status.RUNNING;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { LeafNode } from "./AbstractNodes";
|
||||
import { IBTNode } from "./BTNode";
|
||||
|
||||
/**
|
||||
* 条件节点
|
||||
* 根据条件函数返回SUCCESS或FAILURE
|
||||
*/
|
||||
export class Condition extends BaseNode {
|
||||
export class Condition extends LeafNode {
|
||||
/** 执行函数 @internal */
|
||||
private readonly _func: (subject: any) => boolean;
|
||||
constructor(func: (subject: any) => boolean) {
|
||||
private readonly _func: (node: IBTNode) => boolean;
|
||||
constructor(func: (node: IBTNode) => boolean) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
return this._func?.(tree.subject) ? Status.SUCCESS : Status.FAILURE;
|
||||
public tick(): Status {
|
||||
return this._func?.(this) ? Status.SUCCESS : Status.FAILURE;
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,9 @@
|
||||
* @Description: 装饰节点 装饰节点下必须包含子节点
|
||||
*/
|
||||
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { Decorator, NumericDecorator } from "./AbstractNodes";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { IBTNode } from "./BTNode";
|
||||
|
||||
/**
|
||||
* 结果反转节点
|
||||
@@ -16,8 +15,8 @@ import { BaseNode } from "./BaseNode";
|
||||
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
|
||||
*/
|
||||
export class Inverter extends Decorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const status = this.children[0]!._execute(tree);
|
||||
public tick(): Status {
|
||||
const status = this.children[0]!._execute();
|
||||
|
||||
if (status === Status.SUCCESS) {
|
||||
return Status.FAILURE;
|
||||
@@ -41,44 +40,35 @@ export class LimitTime extends NumericDecorator {
|
||||
* @param child 子节点
|
||||
* @param max 最大时间 (秒) 默认1秒
|
||||
*/
|
||||
constructor(child: BaseNode, max: number = 1) {
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child, max * 1000);
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
this._value = Date.now();
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
const currentTime = Date.now();
|
||||
|
||||
if (currentTime - this._value > this._max) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
return this.children[0]!._execute(tree);
|
||||
return this.children[0]!._execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 次数限制节点
|
||||
* 必须且只能包含一个子节点
|
||||
* 次数限制内, 返回子节点的状态, 次数达到后, 直接返回失败
|
||||
* 次数超过后, 直接返回失败; 次数未超过, 返回子节点状态
|
||||
*/
|
||||
export class LimitTimes extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
if (this._value >= this._max) {
|
||||
export class LimitTicks extends NumericDecorator {
|
||||
public tick(): Status {
|
||||
this._value++;
|
||||
if (this._value > this._max) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
const status = this.children[0]!._execute(tree);
|
||||
if (status !== Status.RUNNING) {
|
||||
this._value++;
|
||||
if (this._value < this._max) {
|
||||
return Status.RUNNING;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
return this.children[0]!._execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,9 +79,9 @@ export class LimitTimes extends NumericDecorator {
|
||||
* 次数超过之后返回子节点状态,否则返回 RUNNING
|
||||
*/
|
||||
export class Repeat extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
// 执行子节点
|
||||
const status = this.children[0]!._execute(tree);
|
||||
const status = this.children[0]!._execute();
|
||||
// 如果子节点完成(成功或失败),增加计数
|
||||
if (status === Status.SUCCESS || status === Status.FAILURE) {
|
||||
this._value++;
|
||||
@@ -112,8 +102,8 @@ export class Repeat extends NumericDecorator {
|
||||
* 子节点成功 计数+1
|
||||
*/
|
||||
export class RepeatUntilFailure extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const status = this.children[0]!._execute(tree);
|
||||
public tick(): Status {
|
||||
const status = this.children[0]!._execute();
|
||||
if (status === Status.FAILURE) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
@@ -136,9 +126,9 @@ export class RepeatUntilFailure extends NumericDecorator {
|
||||
* 子节点失败, 计数+1
|
||||
*/
|
||||
export class RepeatUntilSuccess extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
// 执行子节点
|
||||
const status = this.children[0]!._execute(tree);
|
||||
const status = this.children[0]!._execute();
|
||||
if (status === Status.SUCCESS) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Blackboard } from "./Blackboard";
|
||||
import { BaseNode } from "./BTNode/BaseNode";
|
||||
import { Blackboard, IBlackboard } from "./Blackboard";
|
||||
import { IBTNode } from "./BTNode/BTNode";
|
||||
import { Status } from "./header";
|
||||
|
||||
/**
|
||||
* 行为树
|
||||
@@ -9,36 +10,23 @@ export class BehaviorTree<T> {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private _root: BaseNode;
|
||||
private _root: IBTNode;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private _blackboard: Blackboard;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private _subject: T;
|
||||
private _blackboard: IBlackboard;
|
||||
|
||||
/**
|
||||
* 节点ID计数器,每个树实例独立管理
|
||||
* @internal
|
||||
*/
|
||||
private _nodeIdCounter: number = 0;
|
||||
|
||||
get root(): BaseNode { return this._root; }
|
||||
get blackboard() { return this._blackboard }
|
||||
get subject(): T { return this._subject; }
|
||||
get root(): IBTNode { return this._root; }
|
||||
get blackboard(): IBlackboard { return this._blackboard }
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param subject 主体
|
||||
* @param entity 实体
|
||||
* @param root 根节点
|
||||
*/
|
||||
constructor(subject: T, root: BaseNode) {
|
||||
constructor(entity: T, root: IBTNode) {
|
||||
this._root = root;
|
||||
this._blackboard = new Blackboard();
|
||||
this._subject = subject;
|
||||
|
||||
this._blackboard = new Blackboard(undefined, entity);
|
||||
// 构造时就初始化所有节点ID,避免运行时检查
|
||||
this._initializeAllNodeIds(this._root);
|
||||
}
|
||||
@@ -46,17 +34,8 @@ export class BehaviorTree<T> {
|
||||
/**
|
||||
* 执行行为树
|
||||
*/
|
||||
public tick(): void {
|
||||
this._root._execute(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成节点ID
|
||||
* 每个树实例独立管理节点ID,避免全局状态污染
|
||||
* @internal
|
||||
*/
|
||||
private _generateNodeId(): string {
|
||||
return `${++this._nodeIdCounter}`;
|
||||
public tick(): Status {
|
||||
return this._root._execute();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,13 +44,12 @@ export class BehaviorTree<T> {
|
||||
* @param node 要初始化的节点
|
||||
* @internal
|
||||
*/
|
||||
private _initializeAllNodeIds(node: BaseNode): void {
|
||||
private _initializeAllNodeIds(node: IBTNode, parent?: IBTNode): void {
|
||||
// 设置当前节点ID
|
||||
node.id = this._generateNodeId();
|
||||
|
||||
node._initialize(this._blackboard, parent ? parent.local : this._blackboard);
|
||||
// 递归设置所有子节点ID
|
||||
for (const child of node.children) {
|
||||
this._initializeAllNodeIds(child);
|
||||
this._initializeAllNodeIds(child, node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,18 +58,6 @@ export class BehaviorTree<T> {
|
||||
* 清空黑板并重置所有节点状态
|
||||
*/
|
||||
public reset(): void {
|
||||
this._blackboard.clear();
|
||||
// 重置所有节点的状态
|
||||
this._root.cleanupAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置指定记忆节点的记忆状态
|
||||
* 用于精确控制记忆节点的重置,而不影响其他状态
|
||||
* @param node 记忆节点
|
||||
*/
|
||||
public resetMemoryNode(node: BaseNode): void {
|
||||
// 通过黑板标记该节点需要重置记忆
|
||||
this._blackboard.set(`reset_memory`, true, node);
|
||||
this._blackboard.clean();
|
||||
}
|
||||
}
|
||||
@@ -4,67 +4,93 @@
|
||||
* @Description: 行为树共享数据
|
||||
*
|
||||
* 专门用于存储和管理行为树执行过程中的共享数据
|
||||
* 使用 Symbol 作为键实现高性能且安全的键值存储
|
||||
*/
|
||||
|
||||
// 为了避免循环依赖,我们定义一个最小接口
|
||||
interface IBlackboardNode {
|
||||
readonly id: string;
|
||||
import { IBTNode } from "./BTNode/BTNode";
|
||||
|
||||
/**
|
||||
* 黑板数据接口
|
||||
*/
|
||||
export interface IBlackboard {
|
||||
getEntity<T>(): T;
|
||||
get<T>(key: string): T;
|
||||
set<T>(key: string, value: T): void;
|
||||
delete(key: string): void;
|
||||
has(key: string): boolean;
|
||||
clean(): void;
|
||||
createChild(scope?: number): IBlackboard;
|
||||
/** @internal */
|
||||
openNodes: WeakMap<IBTNode, boolean>;
|
||||
}
|
||||
|
||||
export class Blackboard {
|
||||
private readonly _data = new Map<IBlackboardNode, Map<string, any>>();
|
||||
/**
|
||||
* 黑板类
|
||||
*/
|
||||
export class Blackboard implements IBlackboard {
|
||||
private readonly _data = new Map<string, any>();
|
||||
public parent?: Blackboard | undefined;
|
||||
public children = new Set<Blackboard>();
|
||||
|
||||
public clear(): void {
|
||||
/**
|
||||
* 正在运行中的节点
|
||||
* @internal
|
||||
*/
|
||||
public openNodes = new WeakMap<IBTNode, boolean>();
|
||||
|
||||
/** 实体 */
|
||||
private readonly _entity: any;
|
||||
public getEntity<T>(): T {
|
||||
return this._entity;
|
||||
}
|
||||
|
||||
constructor(parent?: Blackboard, entity?: any) {
|
||||
this.parent = parent;
|
||||
if (parent) {
|
||||
parent.children.add(this);
|
||||
}
|
||||
// 优先使用传入的 entity,如果没有则从父级继承
|
||||
this._entity = entity !== undefined ? entity : (parent?._entity ?? null);
|
||||
}
|
||||
|
||||
/** 核心: 查找链实现 */
|
||||
public get<T>(key: string): T {
|
||||
if (this._data.has(key)) {
|
||||
return this._data.get(key) as T;
|
||||
}
|
||||
return this.parent?.get(key) as T;
|
||||
}
|
||||
|
||||
/** 写入: 只在当前层 */
|
||||
public set<T>(key: string, value: T): void {
|
||||
this._data.set(key, value);
|
||||
}
|
||||
|
||||
/** 检查: 沿链查找 */
|
||||
public has(key: string): boolean {
|
||||
return this._data.has(key) || (this.parent?.has(key) ?? false);
|
||||
}
|
||||
|
||||
public delete(key: string): void {
|
||||
this._data.delete(key);
|
||||
}
|
||||
|
||||
public createChild(): Blackboard {
|
||||
return new Blackboard(this);
|
||||
}
|
||||
|
||||
public clean(): void {
|
||||
// 清空当前黑板数据
|
||||
this._data.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据
|
||||
* @param key 键名
|
||||
* @param value 值
|
||||
* @param node 节点实例(用于生成唯一 Symbol)
|
||||
*/
|
||||
public set<T>(key: string, value: T, node: IBlackboardNode): void {
|
||||
let map = this._data.get(node);
|
||||
if (!map) {
|
||||
map = new Map();
|
||||
this._data.set(node, map);
|
||||
// 重置运行状态
|
||||
this.openNodes = new WeakMap<IBTNode, boolean>();
|
||||
|
||||
// 递归清理所有子黑板
|
||||
for (const child of this.children) {
|
||||
child.clean();
|
||||
}
|
||||
map.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param key 键名
|
||||
* @param node 节点实例
|
||||
* @returns 值
|
||||
*/
|
||||
public get<T>(key: string, node: IBlackboardNode): T | undefined {
|
||||
return this._data.get(node)?.get(key) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在指定键
|
||||
* @param key 键名
|
||||
* @param node 节点实例
|
||||
* @returns 是否存在
|
||||
*/
|
||||
public has(key: string, node: IBlackboardNode): boolean {
|
||||
return this._data.has(node) ? this._data.get(node)?.has(key) || false : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定键的数据
|
||||
* @param key 键名
|
||||
* @param node 节点实例
|
||||
* @returns 是否删除成功
|
||||
*/
|
||||
public delete(key: string, node: IBlackboardNode): boolean {
|
||||
if (this.has(key, node)) {
|
||||
this._data.get(node)?.delete(key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 全局共享的黑板实例
|
||||
export const globalBlackboard = new Blackboard();
|
||||
@@ -4,7 +4,7 @@ export { BehaviorTree } from "./behaviortree/BehaviorTree";
|
||||
export { Blackboard } from "./behaviortree/Blackboard";
|
||||
export * from "./behaviortree/BTNode/AbstractNodes";
|
||||
export * from "./behaviortree/BTNode/Action";
|
||||
export { BaseNode as Node } from "./behaviortree/BTNode/BaseNode";
|
||||
export { IBTNode } from "./behaviortree/BTNode/BTNode";
|
||||
export * from "./behaviortree/BTNode/Composite";
|
||||
export { Condition } from "./behaviortree/BTNode/Condition";
|
||||
export * from "./behaviortree/BTNode/Decorator";
|
||||
581
test/simple-runner.ts
Normal file
581
test/simple-runner.ts
Normal file
@@ -0,0 +1,581 @@
|
||||
/**
|
||||
* 简单的测试运行器 - 无需外部依赖
|
||||
* 验证所有行为树功能
|
||||
*/
|
||||
|
||||
import { BehaviorTree } from '../src/behaviortree/BehaviorTree';
|
||||
import { Blackboard, globalBlackboard } from '../src/behaviortree/Blackboard';
|
||||
import { Status } from '../src/behaviortree/header';
|
||||
|
||||
// 导入所有节点类型
|
||||
import { Action, WaitTicks, WaitTime } from '../src/behaviortree/BTNode/Action';
|
||||
import { Condition } from '../src/behaviortree/BTNode/Condition';
|
||||
import {
|
||||
Selector, Sequence, Parallel, ParallelAnySuccess,
|
||||
MemSelector, MemSequence, RandomSelector
|
||||
} from '../src/behaviortree/BTNode/Composite';
|
||||
import {
|
||||
Inverter, LimitTime, LimitTicks, Repeat,
|
||||
RepeatUntilFailure, RepeatUntilSuccess
|
||||
} from '../src/behaviortree/BTNode/Decorator';
|
||||
|
||||
interface TestEntity {
|
||||
name: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
// 简单断言函数
|
||||
function assert(condition: boolean, message: string) {
|
||||
if (!condition) {
|
||||
console.error(`❌ FAIL: ${message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function assertEqual<T>(actual: T, expected: T, message: string) {
|
||||
if (actual !== expected) {
|
||||
console.error(`❌ FAIL: ${message}`);
|
||||
console.error(` Expected: ${expected}`);
|
||||
console.error(` Actual: ${actual}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
// 测试计数器
|
||||
let totalTests = 0;
|
||||
let passedTests = 0;
|
||||
|
||||
function runTest(testName: string, testFn: () => void | Promise<void>) {
|
||||
totalTests++;
|
||||
try {
|
||||
const result = testFn();
|
||||
if (result instanceof Promise) {
|
||||
return result.then(() => {
|
||||
console.log(`✅ ${testName}`);
|
||||
passedTests++;
|
||||
}).catch(error => {
|
||||
console.error(`❌ FAIL: ${testName} - ${error.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
console.log(`✅ ${testName}`);
|
||||
passedTests++;
|
||||
return Promise.resolve();
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error(`❌ FAIL: ${testName} - ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log('🚀 开始行为树全面功能测试...\n');
|
||||
|
||||
const testEntity: TestEntity = { name: 'test', value: 0 };
|
||||
|
||||
// 重置函数
|
||||
function reset() {
|
||||
testEntity.name = 'test';
|
||||
testEntity.value = 0;
|
||||
globalBlackboard.clean();
|
||||
}
|
||||
|
||||
console.log('📋 1. Action节点测试');
|
||||
|
||||
runTest('Action节点 - 成功执行', () => {
|
||||
reset();
|
||||
let executed = false;
|
||||
const action = new Action(() => {
|
||||
executed = true;
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
const tree = new BehaviorTree(testEntity, action);
|
||||
const result = tree.tick();
|
||||
assertEqual(result, Status.SUCCESS, 'Action应该返回SUCCESS');
|
||||
assert(executed, 'Action函数应该被执行');
|
||||
});
|
||||
|
||||
runTest('Action节点 - 失败执行', () => {
|
||||
reset();
|
||||
let executed = false;
|
||||
const action = new Action(() => {
|
||||
executed = true;
|
||||
return Status.FAILURE;
|
||||
});
|
||||
const tree = new BehaviorTree(testEntity, action);
|
||||
const result = tree.tick();
|
||||
assertEqual(result, Status.FAILURE, 'Action应该返回FAILURE');
|
||||
assert(executed, 'Action函数应该被执行');
|
||||
});
|
||||
|
||||
runTest('WaitTicks节点 - 等待指定次数', () => {
|
||||
reset();
|
||||
const waitNode = new WaitTicks(3);
|
||||
const tree = new BehaviorTree(testEntity, waitNode);
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第1次tick应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第2次tick应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.SUCCESS, '第3次tick应该返回SUCCESS');
|
||||
});
|
||||
|
||||
await runTest('WaitTime节点 - 时间等待', async () => {
|
||||
reset();
|
||||
const waitNode = new WaitTime(0.1); // 100ms
|
||||
const tree = new BehaviorTree(testEntity, waitNode);
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '时间未到应该返回RUNNING');
|
||||
|
||||
await sleep(150);
|
||||
assertEqual(tree.tick(), Status.SUCCESS, '时间到了应该返回SUCCESS');
|
||||
});
|
||||
|
||||
console.log('\n📋 2. Condition节点测试');
|
||||
|
||||
runTest('Condition节点 - 条件为真', () => {
|
||||
reset();
|
||||
const condition = new Condition(() => true);
|
||||
const tree = new BehaviorTree(testEntity, condition);
|
||||
assertEqual(tree.tick(), Status.SUCCESS, 'true条件应该返回SUCCESS');
|
||||
});
|
||||
|
||||
runTest('Condition节点 - 条件为假', () => {
|
||||
reset();
|
||||
const condition = new Condition(() => false);
|
||||
const tree = new BehaviorTree(testEntity, condition);
|
||||
assertEqual(tree.tick(), Status.FAILURE, 'false条件应该返回FAILURE');
|
||||
});
|
||||
|
||||
console.log('\n📋 3. Composite节点测试');
|
||||
|
||||
runTest('Selector节点 - 第一个成功', () => {
|
||||
reset();
|
||||
const selector = new Selector(
|
||||
new Action(() => Status.SUCCESS),
|
||||
new Action(() => Status.FAILURE)
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, selector);
|
||||
assertEqual(tree.tick(), Status.SUCCESS, 'Selector第一个成功应该返回SUCCESS');
|
||||
});
|
||||
|
||||
runTest('Selector节点 - 全部失败', () => {
|
||||
reset();
|
||||
const selector = new Selector(
|
||||
new Action(() => Status.FAILURE),
|
||||
new Action(() => Status.FAILURE)
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, selector);
|
||||
assertEqual(tree.tick(), Status.FAILURE, 'Selector全部失败应该返回FAILURE');
|
||||
});
|
||||
|
||||
runTest('Sequence节点 - 全部成功', () => {
|
||||
reset();
|
||||
const sequence = new Sequence(
|
||||
new Action(() => Status.SUCCESS),
|
||||
new Action(() => Status.SUCCESS)
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, sequence);
|
||||
assertEqual(tree.tick(), Status.SUCCESS, 'Sequence全部成功应该返回SUCCESS');
|
||||
});
|
||||
|
||||
runTest('Sequence节点 - 中途失败', () => {
|
||||
reset();
|
||||
let firstExecuted = false;
|
||||
let secondExecuted = false;
|
||||
|
||||
const sequence = new Sequence(
|
||||
new Action(() => {
|
||||
firstExecuted = true;
|
||||
return Status.SUCCESS;
|
||||
}),
|
||||
new Action(() => {
|
||||
secondExecuted = true;
|
||||
return Status.FAILURE;
|
||||
})
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, sequence);
|
||||
|
||||
assertEqual(tree.tick(), Status.FAILURE, 'Sequence中途失败应该返回FAILURE');
|
||||
assert(firstExecuted, '第一个Action应该被执行');
|
||||
assert(secondExecuted, '第二个Action也应该被执行');
|
||||
});
|
||||
|
||||
runTest('MemSelector节点 - 记忆运行状态', () => {
|
||||
reset();
|
||||
let firstCallCount = 0;
|
||||
let secondCallCount = 0;
|
||||
|
||||
const memSelector = new MemSelector(
|
||||
new Action(() => {
|
||||
firstCallCount++;
|
||||
return Status.RUNNING;
|
||||
}),
|
||||
new Action(() => {
|
||||
secondCallCount++;
|
||||
return Status.SUCCESS;
|
||||
})
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, memSelector);
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第一次tick应该返回RUNNING');
|
||||
assertEqual(firstCallCount, 1, '第一个Action应该执行1次');
|
||||
assertEqual(secondCallCount, 0, '第二个Action不应该执行');
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第二次tick应该从第一个节点继续');
|
||||
assertEqual(firstCallCount, 2, '第一个Action应该执行2次');
|
||||
assertEqual(secondCallCount, 0, '第二个Action仍不应该执行');
|
||||
});
|
||||
|
||||
runTest('Parallel节点 - FAILURE优先级最高', () => {
|
||||
reset();
|
||||
const parallel = new Parallel(
|
||||
new Action(() => Status.SUCCESS),
|
||||
new Action(() => Status.FAILURE),
|
||||
new Action(() => Status.RUNNING)
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, parallel);
|
||||
assertEqual(tree.tick(), Status.FAILURE, 'Parallel有FAILURE应该返回FAILURE');
|
||||
});
|
||||
|
||||
runTest('Parallel节点 - RUNNING次优先级', () => {
|
||||
reset();
|
||||
const parallel = new Parallel(
|
||||
new Action(() => Status.SUCCESS),
|
||||
new Action(() => Status.RUNNING),
|
||||
new Action(() => Status.SUCCESS)
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, parallel);
|
||||
assertEqual(tree.tick(), Status.RUNNING, 'Parallel有RUNNING无FAILURE应该返回RUNNING');
|
||||
});
|
||||
|
||||
runTest('ParallelAnySuccess节点 - SUCCESS优先级最高', () => {
|
||||
reset();
|
||||
const parallel = new ParallelAnySuccess(
|
||||
new Action(() => Status.SUCCESS),
|
||||
new Action(() => Status.FAILURE),
|
||||
new Action(() => Status.RUNNING)
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, parallel);
|
||||
assertEqual(tree.tick(), Status.SUCCESS, 'ParallelAnySuccess有SUCCESS应该返回SUCCESS');
|
||||
});
|
||||
|
||||
console.log('\n📋 4. Decorator节点测试');
|
||||
|
||||
runTest('Inverter节点 - 反转SUCCESS', () => {
|
||||
reset();
|
||||
const inverter = new Inverter(new Action(() => Status.SUCCESS));
|
||||
const tree = new BehaviorTree(testEntity, inverter);
|
||||
assertEqual(tree.tick(), Status.FAILURE, 'Inverter应该将SUCCESS反转为FAILURE');
|
||||
});
|
||||
|
||||
runTest('Inverter节点 - 反转FAILURE', () => {
|
||||
reset();
|
||||
const inverter = new Inverter(new Action(() => Status.FAILURE));
|
||||
const tree = new BehaviorTree(testEntity, inverter);
|
||||
assertEqual(tree.tick(), Status.SUCCESS, 'Inverter应该将FAILURE反转为SUCCESS');
|
||||
});
|
||||
|
||||
runTest('LimitTicks节点 - 次数限制内成功', () => {
|
||||
reset();
|
||||
let executeCount = 0;
|
||||
const limitTicks = new LimitTicks(
|
||||
new Action(() => {
|
||||
executeCount++;
|
||||
return Status.SUCCESS;
|
||||
}),
|
||||
3
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, limitTicks);
|
||||
|
||||
assertEqual(tree.tick(), Status.SUCCESS, '限制内应该返回SUCCESS');
|
||||
assertEqual(executeCount, 1, '应该执行1次');
|
||||
});
|
||||
|
||||
runTest('LimitTicks节点 - 超过次数限制', () => {
|
||||
reset();
|
||||
let executeCount = 0;
|
||||
const limitTicks = new LimitTicks(
|
||||
new Action(() => {
|
||||
executeCount++;
|
||||
return Status.RUNNING;
|
||||
}),
|
||||
2
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, limitTicks);
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第1次应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第2次应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.FAILURE, '第3次超限应该返回FAILURE');
|
||||
assertEqual(executeCount, 2, '应该只执行2次');
|
||||
});
|
||||
|
||||
runTest('Repeat节点 - 指定次数重复', () => {
|
||||
reset();
|
||||
let executeCount = 0;
|
||||
const repeat = new Repeat(
|
||||
new Action(() => {
|
||||
executeCount++;
|
||||
return Status.SUCCESS;
|
||||
}),
|
||||
3
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, repeat);
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第1次应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第2次应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.SUCCESS, '第3次应该完成返回SUCCESS');
|
||||
assertEqual(executeCount, 3, '应该执行3次');
|
||||
});
|
||||
|
||||
runTest('RepeatUntilSuccess节点 - 直到成功', () => {
|
||||
reset();
|
||||
let attempts = 0;
|
||||
const repeatUntilSuccess = new RepeatUntilSuccess(
|
||||
new Action(() => {
|
||||
attempts++;
|
||||
return attempts >= 3 ? Status.SUCCESS : Status.FAILURE;
|
||||
}),
|
||||
5
|
||||
);
|
||||
const tree = new BehaviorTree(testEntity, repeatUntilSuccess);
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第1次失败应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第2次失败应该返回RUNNING');
|
||||
assertEqual(tree.tick(), Status.SUCCESS, '第3次成功应该返回SUCCESS');
|
||||
assertEqual(attempts, 3, '应该尝试3次');
|
||||
});
|
||||
|
||||
console.log('\n📋 5. 黑板数据存储与隔离测试');
|
||||
|
||||
runTest('黑板基本读写功能', () => {
|
||||
reset();
|
||||
const action = new Action((node) => {
|
||||
node.set('test_key', 'test_value');
|
||||
const value = node.get<string>('test_key');
|
||||
assertEqual(value, 'test_value', '应该能读取设置的值');
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
const tree = new BehaviorTree(testEntity, action);
|
||||
tree.tick();
|
||||
});
|
||||
|
||||
runTest('黑板层级数据隔离 - 树级黑板', () => {
|
||||
reset();
|
||||
let rootValue: string | undefined;
|
||||
let localValue: string | undefined;
|
||||
|
||||
const action = new Action((node) => {
|
||||
node.setRoot('root_key', 'root_value');
|
||||
node.set('local_key', 'local_value');
|
||||
|
||||
rootValue = node.getRoot<string>('root_key');
|
||||
localValue = node.get<string>('local_key');
|
||||
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
const tree = new BehaviorTree(testEntity, action);
|
||||
tree.tick();
|
||||
|
||||
assertEqual(rootValue, 'root_value', '应该能读取树级数据');
|
||||
assertEqual(localValue, 'local_value', '应该能读取本地数据');
|
||||
assertEqual(tree.blackboard.get<string>('root_key'), 'root_value', '树级黑板应该有数据');
|
||||
});
|
||||
|
||||
runTest('黑板层级数据隔离 - 全局黑板', () => {
|
||||
reset();
|
||||
const action1 = new Action((node) => {
|
||||
node.setGlobal('global_key', 'global_value');
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
|
||||
const action2 = new Action((node) => {
|
||||
const value = node.getGlobal<string>('global_key');
|
||||
assertEqual(value, 'global_value', '应该能读取全局数据');
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
|
||||
const tree1 = new BehaviorTree(testEntity, action1);
|
||||
const tree2 = new BehaviorTree({ name: 'test2', value: 1 }, action2);
|
||||
|
||||
tree1.tick();
|
||||
tree2.tick();
|
||||
});
|
||||
|
||||
runTest('实体数据关联', () => {
|
||||
reset();
|
||||
const action = new Action((node) => {
|
||||
const entity = node.getEntity<TestEntity>();
|
||||
assertEqual(entity.name, 'test', '实体name应该正确');
|
||||
assertEqual(entity.value, 0, '实体value应该正确');
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
const tree = new BehaviorTree(testEntity, action);
|
||||
tree.tick();
|
||||
});
|
||||
|
||||
console.log('\n📋 6. 行为树重置逻辑测试');
|
||||
|
||||
runTest('行为树重置清空黑板数据', () => {
|
||||
reset();
|
||||
const action = new Action((node) => {
|
||||
node.set('test_key', 'test_value');
|
||||
node.setRoot('root_key', 'root_value');
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
|
||||
const tree = new BehaviorTree(testEntity, action);
|
||||
tree.tick();
|
||||
|
||||
// 确认数据存在
|
||||
assertEqual(tree.blackboard.get<string>('test_key'), 'test_value', '数据应该存在');
|
||||
assertEqual(tree.blackboard.get<string>('root_key'), 'root_value', '根数据应该存在');
|
||||
|
||||
// 重置
|
||||
tree.reset();
|
||||
|
||||
// 确认数据被清空
|
||||
assertEqual(tree.blackboard.get<string>('test_key'), undefined, '数据应该被清空');
|
||||
assertEqual(tree.blackboard.get<string>('root_key'), undefined, '根数据应该被清空');
|
||||
});
|
||||
|
||||
runTest('Memory节点重置后内存索引重置', () => {
|
||||
reset();
|
||||
let firstCallCount = 0;
|
||||
let secondCallCount = 0;
|
||||
|
||||
const memSequence = new MemSequence(
|
||||
new Action(() => {
|
||||
firstCallCount++;
|
||||
return Status.SUCCESS;
|
||||
}),
|
||||
new Action(() => {
|
||||
secondCallCount++;
|
||||
return Status.RUNNING;
|
||||
})
|
||||
);
|
||||
|
||||
const tree = new BehaviorTree(testEntity, memSequence);
|
||||
|
||||
// 第一次运行
|
||||
assertEqual(tree.tick(), Status.RUNNING, '应该返回RUNNING');
|
||||
assertEqual(firstCallCount, 1, '第一个节点应该执行1次');
|
||||
assertEqual(secondCallCount, 1, '第二个节点应该执行1次');
|
||||
|
||||
// 第二次运行,应该从第二个节点继续
|
||||
assertEqual(tree.tick(), Status.RUNNING, '应该继续返回RUNNING');
|
||||
assertEqual(firstCallCount, 1, '第一个节点不应该再执行');
|
||||
assertEqual(secondCallCount, 2, '第二个节点应该执行2次');
|
||||
|
||||
// 重置
|
||||
tree.reset();
|
||||
|
||||
// 重置后运行,应该从第一个节点重新开始
|
||||
assertEqual(tree.tick(), Status.RUNNING, '重置后应该返回RUNNING');
|
||||
assertEqual(firstCallCount, 2, '第一个节点应该重新执行');
|
||||
assertEqual(secondCallCount, 3, '第二个节点应该再次执行');
|
||||
});
|
||||
|
||||
console.log('\n📋 7. 其他关键功能测试');
|
||||
|
||||
runTest('复杂行为树结构测试', () => {
|
||||
reset();
|
||||
// 构建复杂嵌套结构
|
||||
const complexTree = new Selector(
|
||||
new Sequence(
|
||||
new Condition(() => false), // 导致Sequence失败
|
||||
new Action(() => Status.SUCCESS)
|
||||
),
|
||||
new MemSelector(
|
||||
new Inverter(new Action(() => Status.SUCCESS)), // 反转为FAILURE
|
||||
new Repeat(
|
||||
new Action(() => Status.SUCCESS),
|
||||
2
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
const tree = new BehaviorTree(testEntity, complexTree);
|
||||
|
||||
assertEqual(tree.tick(), Status.RUNNING, '第一次应该返回RUNNING(Repeat第1次)');
|
||||
assertEqual(tree.tick(), Status.SUCCESS, '第二次应该返回SUCCESS(Repeat完成)');
|
||||
});
|
||||
|
||||
runTest('边界情况 - 空子节点', () => {
|
||||
reset();
|
||||
const emptySelector = new Selector();
|
||||
const emptySequence = new Sequence();
|
||||
const emptyParallel = new Parallel();
|
||||
|
||||
const tree1 = new BehaviorTree(testEntity, emptySelector);
|
||||
const tree2 = new BehaviorTree(testEntity, emptySequence);
|
||||
const tree3 = new BehaviorTree(testEntity, emptyParallel);
|
||||
|
||||
assertEqual(tree1.tick(), Status.FAILURE, '空Selector应该返回FAILURE');
|
||||
assertEqual(tree2.tick(), Status.SUCCESS, '空Sequence应该返回SUCCESS');
|
||||
assertEqual(tree3.tick(), Status.SUCCESS, '空Parallel应该返回SUCCESS');
|
||||
});
|
||||
|
||||
runTest('黑板数据类型测试', () => {
|
||||
reset();
|
||||
const action = new Action((node) => {
|
||||
// 测试各种数据类型
|
||||
node.set('string', 'hello');
|
||||
node.set('number', 42);
|
||||
node.set('boolean', true);
|
||||
node.set('array', [1, 2, 3]);
|
||||
node.set('object', { a: 1, b: 'test' });
|
||||
node.set('null', null);
|
||||
|
||||
assertEqual(node.get<string>('string'), 'hello', 'string类型');
|
||||
assertEqual(node.get<number>('number'), 42, 'number类型');
|
||||
assertEqual(node.get<boolean>('boolean'), true, 'boolean类型');
|
||||
|
||||
const arr = node.get<number[]>('array');
|
||||
assert(Array.isArray(arr) && arr.length === 3, 'array类型');
|
||||
|
||||
const obj = node.get<any>('object');
|
||||
assertEqual(obj.a, 1, 'object属性a');
|
||||
assertEqual(obj.b, 'test', 'object属性b');
|
||||
|
||||
assertEqual(node.get<any>('null'), null, 'null值');
|
||||
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
|
||||
const tree = new BehaviorTree(testEntity, action);
|
||||
tree.tick();
|
||||
});
|
||||
|
||||
console.log(`\n🎉 测试完成! 通过 ${passedTests}/${totalTests} 个测试`);
|
||||
|
||||
if (passedTests === totalTests) {
|
||||
console.log('✅ 所有测试通过!');
|
||||
|
||||
console.log('\n📊 测试覆盖总结:');
|
||||
console.log('✅ Action节点: 4个测试 (Action, WaitTicks, WaitTime)');
|
||||
console.log('✅ Condition节点: 2个测试 (true/false条件)');
|
||||
console.log('✅ Composite节点: 7个测试 (Selector, Sequence, MemSelector, Parallel等)');
|
||||
console.log('✅ Decorator节点: 6个测试 (Inverter, LimitTicks, Repeat等)');
|
||||
console.log('✅ 黑板功能: 4个测试 (数据存储、隔离、实体关联)');
|
||||
console.log('✅ 重置逻辑: 2个测试 (数据清空、状态重置)');
|
||||
console.log('✅ 其他功能: 3个测试 (复杂结构、边界情况、数据类型)');
|
||||
|
||||
console.log('\n🔍 验证的核心功能:');
|
||||
console.log('• 所有节点类型的正确行为');
|
||||
console.log('• 黑板三级数据隔离 (本地/树级/全局)');
|
||||
console.log('• Memory节点的状态记忆');
|
||||
console.log('• 行为树重置的完整清理');
|
||||
console.log('• 复杂嵌套结构的正确执行');
|
||||
console.log('• 各种数据类型的存储支持');
|
||||
|
||||
process.exit(0);
|
||||
} else {
|
||||
console.log('❌ 有测试失败!');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user