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 |
598
README.md
598
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)
|
||||||
|
|
||||||
```typescript
|
#### Sequence - 顺序节点
|
||||||
// 顺序节点:按顺序执行所有子节点,直到遇到失败或运行中的节点
|
按顺序执行子节点,全部成功才成功:
|
||||||
new Sequence(childNode1, childNode2, childNode3);
|
```typescript
|
||||||
|
new Sequence(
|
||||||
|
checkAmmo, // 检查弹药
|
||||||
|
aim, // 瞄准
|
||||||
|
shoot // 射击
|
||||||
|
)
|
||||||
|
// 只有全部成功才返回SUCCESS
|
||||||
|
```
|
||||||
|
|
||||||
// 选择节点:选择第一个成功或运行中的子节点
|
#### Selector - 选择节点
|
||||||
new Selector(childNode1, childNode2, childNode3);
|
选择第一个成功的子节点:
|
||||||
|
```typescript
|
||||||
|
new Selector(
|
||||||
|
tryMeleeAttack, // 尝试近战
|
||||||
|
tryRangedAttack, // 尝试远程
|
||||||
|
retreat // 撤退
|
||||||
|
)
|
||||||
|
// 任一成功就返回SUCCESS
|
||||||
|
```
|
||||||
|
|
||||||
// 并行节点:同时执行所有子节点,全部成功才成功
|
#### Parallel - 并行节点
|
||||||
new Parallel(childNode1, childNode2, childNode3);
|
同时执行所有子节点,全部成功才成功:
|
||||||
|
```typescript
|
||||||
|
new Parallel(
|
||||||
|
moveToTarget, // 移动到目标
|
||||||
|
playAnimation, // 播放动画
|
||||||
|
updateUI // 更新UI
|
||||||
|
)
|
||||||
|
// 任一失败返回FAILURE,有RUNNING返回RUNNING,全部SUCCESS才返回SUCCESS
|
||||||
|
```
|
||||||
|
|
||||||
// 并行任一成功节点:同时执行所有子节点,任一成功即成功
|
#### ParallelAnySuccess - 并行任一成功
|
||||||
new ParallelAnySuccess(childNode1, childNode2, childNode3);
|
同时执行所有子节点,任一成功就成功:
|
||||||
|
```typescript
|
||||||
|
new ParallelAnySuccess(
|
||||||
|
findCover, // 寻找掩体
|
||||||
|
callForHelp, // 呼叫支援
|
||||||
|
counterAttack // 反击
|
||||||
|
)
|
||||||
|
// 任一SUCCESS就返回SUCCESS
|
||||||
|
```
|
||||||
|
|
||||||
// 记忆顺序节点:记住上次执行的位置
|
#### Memory节点 - 状态记忆
|
||||||
new MemSequence(childNode1, childNode2, childNode3);
|
记忆节点会记住上次执行位置,避免重复执行:
|
||||||
|
|
||||||
// 记忆选择节点:记住上次执行的位置
|
```typescript
|
||||||
new MemSelector(childNode1, childNode2, childNode3);
|
// MemSequence - 记忆顺序节点
|
||||||
|
new MemSequence(
|
||||||
|
longTask1, // 第一次:SUCCESS,继续下一个
|
||||||
|
longTask2, // 第一次:RUNNING,记住这个位置; 第二次:从longTask2开始继续执行
|
||||||
|
longTask3
|
||||||
|
)
|
||||||
|
|
||||||
// 随机选择节点:随机选择一个子节点执行
|
// MemSelector - 记忆选择节点
|
||||||
new RandomSelector(childNode1, childNode2, childNode3);
|
new MemSelector(
|
||||||
```
|
expensiveCheck1, // 第一次:FAILURE,继续下一个
|
||||||
|
expensiveCheck2, // 第一次:RUNNING,记住这个位置; 第二次:从expensiveCheck2开始执行
|
||||||
|
fallback // 如果前面都是FAILURE才会执行到这里
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
2. 动作节点
|
#### RandomSelector - 随机选择
|
||||||
|
随机选择一个子节点执行:
|
||||||
|
```typescript
|
||||||
|
new RandomSelector(
|
||||||
|
idleBehavior1,
|
||||||
|
idleBehavior2,
|
||||||
|
idleBehavior3
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
```typescript
|
### 装饰节点 (Decorator)
|
||||||
// 行动节点 - 返回指定状态
|
|
||||||
new Action(() => {
|
|
||||||
console.log("执行动作");
|
|
||||||
return Status.SUCCESS; // 或 Status.FAILURE, Status.RUNNING
|
|
||||||
});
|
|
||||||
|
|
||||||
// 条件节点 - 检查条件返回成功或失败
|
#### Inverter - 反转节点
|
||||||
new Condition((subject) => {
|
反转子节点的成功/失败状态:
|
||||||
return subject.health > 50; // 返回 true 表示成功,false 表示失败
|
```typescript
|
||||||
});
|
new Inverter(
|
||||||
|
new Condition((node) => {
|
||||||
|
const enemy = node.getEntity<Enemy>();
|
||||||
|
return enemy.isAlive;
|
||||||
|
})
|
||||||
|
) // 敌人死亡时返回SUCCESS
|
||||||
|
```
|
||||||
|
|
||||||
// 等待节点
|
#### Repeat - 重复节点
|
||||||
new WaitTime(2); // 等待2秒
|
重复执行子节点指定次数:
|
||||||
new WaitTicks(5); // 等待5个tick
|
```typescript
|
||||||
```
|
new Repeat(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("射击");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
3 // 射击3次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
3. 装饰节点
|
#### RepeatUntilSuccess - 重复直到成功
|
||||||
|
```typescript
|
||||||
|
new RepeatUntilSuccess(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("尝试开门");
|
||||||
|
return Math.random() > 0.5 ? Status.SUCCESS : Status.FAILURE;
|
||||||
|
}),
|
||||||
|
5 // 最多尝试5次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
```typescript
|
#### RepeatUntilFailure - 重复直到失败
|
||||||
// 反转节点 - 反转子节点的成功/失败状态
|
```typescript
|
||||||
new Inverter(childNode);
|
new RepeatUntilFailure(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("收集资源");
|
||||||
|
return Status.SUCCESS; // 持续收集直到失败
|
||||||
|
}),
|
||||||
|
10 // 最多收集10次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
// 重复节点 - 重复执行子节点指定次数
|
#### LimitTime - 时间限制
|
||||||
new Repeat(childNode, 3);
|
```typescript
|
||||||
|
new LimitTime(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("执行复杂计算");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
2.0 // 最多执行2秒
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
// 重复直到失败 - 重复执行直到子节点失败
|
#### LimitTicks - 次数限制
|
||||||
new RepeatUntilFailure(childNode, 5);
|
```typescript
|
||||||
|
new LimitTicks(
|
||||||
|
new Action((node) => {
|
||||||
|
console.log("尝试操作");
|
||||||
|
return Status.SUCCESS;
|
||||||
|
}),
|
||||||
|
5 // 最多执行5次
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
// 重复直到成功 - 重复执行直到子节点成功
|
### 叶子节点 (Leaf)
|
||||||
new RepeatUntilSuccess(childNode, 5);
|
|
||||||
|
|
||||||
// 时间限制节点 - 限制子节点执行时间
|
#### Action - 动作节点
|
||||||
new LimitTime(childNode, 5); // 5秒
|
执行自定义逻辑:
|
||||||
|
```typescript
|
||||||
|
new Action((node) => {
|
||||||
|
// 直接获取实体
|
||||||
|
const target = node.getEntity<Character>();
|
||||||
|
|
||||||
// 次数限制节点 - 限制子节点执行次数
|
// 访问黑板数据
|
||||||
new LimitTimes(childNode, 3);
|
const ammo = node.get<number>('ammo');
|
||||||
```
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
|
if (target && ammo > 0) {
|
||||||
|
console.log("攻击目标");
|
||||||
|
node.set('ammo', ammo - 1);
|
||||||
return Status.SUCCESS;
|
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,
|
||||||
|
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; // 低血量
|
||||||
|
|
||||||
1. 节点状态说明:
|
behaviorTree.tick(); // 输出: "治疗完成"
|
||||||
- `SUCCESS`:节点执行成功
|
```
|
||||||
- `FAILURE`:节点执行失败
|
|
||||||
- `RUNNING`:节点正在执行中
|
## 最佳实践
|
||||||
2. 组合节点特性:
|
|
||||||
- `Sequence`:所有子节点返回 SUCCESS 才返回 SUCCESS
|
### 1. 节点设计原则
|
||||||
- `Selector`:任一子节点返回 SUCCESS 就返回 SUCCESS
|
- **单一职责**: 每个节点只做一件事
|
||||||
- `Parallel`:并行执行所有子节点
|
- **状态明确**: 明确定义SUCCESS/FAILURE/RUNNING的含义
|
||||||
- `MemSequence/MemSelector`:会记住上次执行位置
|
- **避免副作用**: 尽量避免节点间的隐式依赖
|
||||||
3. 性能优化:
|
|
||||||
- 使用黑板共享数据,避免重复计算
|
### 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;
|
||||||
|
|
||||||
|
// 实体访问
|
||||||
|
getEntity<T>(): T;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 许可证
|
||||||
|
|
||||||
|
ISC License - 详见 [LICENSE](LICENSE) 文件
|
||||||
|
|
||||||
|
## 贡献
|
||||||
|
|
||||||
|
欢迎提交 Issue 和 Pull Request。请确保:
|
||||||
|
1. 代码风格一致
|
||||||
|
2. 添加适当的测试
|
||||||
|
3. 更新相关文档
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*"好的程序员关心数据结构,而不是代码。"* - 这个库遵循简洁设计原则,专注于解决实际问题。
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kunpocc-behaviortree",
|
"name": "kunpocc-behaviortree",
|
||||||
"version": "0.0.3",
|
"version": "0.0.6",
|
||||||
"description": "行为树",
|
"description": "行为树",
|
||||||
"main": "./dist/kunpocc-behaviortree.cjs",
|
"main": "./dist/kunpocc-behaviortree.cjs",
|
||||||
"module": "./dist/kunpocc-behaviortree.mjs",
|
"module": "./dist/kunpocc-behaviortree.mjs",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import dts from 'rollup-plugin-dts';
|
|||||||
export default [
|
export default [
|
||||||
{
|
{
|
||||||
// 生成未压缩的 JS 文件
|
// 生成未压缩的 JS 文件
|
||||||
input: 'src/kunpocc-behaviortree.ts',
|
input: 'src/index.ts',
|
||||||
external: ['cc', 'fairygui-cc'],
|
external: ['cc', 'fairygui-cc'],
|
||||||
output: [
|
output: [
|
||||||
{
|
{
|
||||||
@@ -38,7 +38,7 @@ export default [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
// 生成压缩的 JS 文件
|
// 生成压缩的 JS 文件
|
||||||
input: 'src/kunpocc-behaviortree.ts',
|
input: 'src/index.ts',
|
||||||
external: ['cc', 'fairygui-cc'],
|
external: ['cc', 'fairygui-cc'],
|
||||||
output: [
|
output: [
|
||||||
{
|
{
|
||||||
@@ -72,7 +72,7 @@ export default [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
// 生成声明文件的配置
|
// 生成声明文件的配置
|
||||||
input: 'src/kunpocc-behaviortree.ts',
|
input: 'src/index.ts',
|
||||||
output: {
|
output: {
|
||||||
file: 'dist/kunpocc-behaviortree.d.ts',
|
file: 'dist/kunpocc-behaviortree.d.ts',
|
||||||
format: 'es'
|
format: 'es'
|
||||||
|
|||||||
@@ -4,67 +4,70 @@
|
|||||||
* @Description: 抽象节点基类
|
* @Description: 抽象节点基类
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { BehaviorTree } from "../BehaviorTree";
|
import { IBlackboard } from "../Blackboard";
|
||||||
import { BaseNode } from "./BaseNode";
|
import { BTNode, IBTNode } from "./BTNode";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 可以包含多个节点的集合装饰器基类
|
* 叶子节点 基类
|
||||||
|
* 没有子节点
|
||||||
*/
|
*/
|
||||||
export abstract class Composite extends BaseNode {
|
export abstract class LeafNode extends BTNode {
|
||||||
constructor(...children: BaseNode[]) {
|
constructor() {
|
||||||
super(children);
|
super([]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修饰节点基类
|
* 修饰节点 基类
|
||||||
* 只能包含一个子节点
|
* 有且仅有一个子节点
|
||||||
*/
|
*/
|
||||||
export abstract class Decorator extends BaseNode {
|
export abstract class Decorator extends BTNode {
|
||||||
constructor(child: BaseNode) {
|
constructor(child: IBTNode) {
|
||||||
super([child]);
|
super([child]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数值型装饰节点基类
|
* 组合节点 基类
|
||||||
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的装饰节点
|
* 多个子节点
|
||||||
|
*/
|
||||||
|
export abstract class Composite extends BTNode {
|
||||||
|
constructor(...children: IBTNode[]) {
|
||||||
|
super(children);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数值型修饰节点 基类
|
||||||
|
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的修饰节点
|
||||||
*/
|
*/
|
||||||
export abstract class NumericDecorator extends Decorator {
|
export abstract class NumericDecorator extends Decorator {
|
||||||
protected readonly _max: number;
|
protected readonly _max: number;
|
||||||
protected _value: number = 0;
|
protected _value: number = 0;
|
||||||
|
|
||||||
constructor(child: BaseNode, max: number = 1) {
|
constructor(child: IBTNode, max: number = 1) {
|
||||||
super(child);
|
super(child);
|
||||||
this._max = max;
|
this._max = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
protected override open(): void {
|
||||||
super.initialize(tree);
|
super.open();
|
||||||
this._value = 0;
|
this._value = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记忆装饰节点基类
|
* 记忆修饰节点基类
|
||||||
|
* 只有记忆节点才需要设置局部数据
|
||||||
*/
|
*/
|
||||||
export abstract class MemoryComposite extends Composite {
|
export abstract class MemoryComposite extends Composite {
|
||||||
protected runningIndex = 0;
|
public override _initialize(global: IBlackboard, branch: IBlackboard): void {
|
||||||
|
super._initialize(global, branch);
|
||||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
this._local = branch.createChild();
|
||||||
super.initialize(tree);
|
|
||||||
// 检查是否需要重置记忆
|
|
||||||
const shouldReset = tree.blackboard.get(`reset_memory`, this);
|
|
||||||
if (shouldReset) {
|
|
||||||
this.runningIndex = 0;
|
|
||||||
tree.blackboard.delete(`reset_memory`, this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected override open(): void {
|
||||||
* 重置记忆状态,下次执行时将从第一个子节点开始
|
super.open();
|
||||||
*/
|
this.set(`__nMemoryRunningIndex`, 0);
|
||||||
public resetMemory(): void {
|
|
||||||
this.runningIndex = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
import type { BehaviorTree } from "../BehaviorTree";
|
|
||||||
import { Status } from "../header";
|
import { Status } from "../header";
|
||||||
import { BaseNode } from "./BaseNode";
|
import { LeafNode } from "./AbstractNodes";
|
||||||
|
import { IBTNode } from "./BTNode";
|
||||||
|
|
||||||
export class Action extends BaseNode {
|
export class Action extends LeafNode {
|
||||||
protected _func: (subject?: any) => Status;
|
protected _func: (node: IBTNode) => Status;
|
||||||
constructor(func: (subject?: any) => Status) {
|
constructor(func: (node: IBTNode) => Status) {
|
||||||
super();
|
super();
|
||||||
this._func = func;
|
this._func = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
return this._func?.(tree.subject) ?? Status.SUCCESS;
|
return this._func?.(this) ?? Status.SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ export class Action extends BaseNode {
|
|||||||
* 次数内,返回RUNNING
|
* 次数内,返回RUNNING
|
||||||
* 超次,返回SUCCESS
|
* 超次,返回SUCCESS
|
||||||
*/
|
*/
|
||||||
export class WaitTicks extends BaseNode {
|
export class WaitTicks extends LeafNode {
|
||||||
private _max: number;
|
private _max: number;
|
||||||
private _value: number;
|
private _value: number;
|
||||||
|
|
||||||
@@ -29,12 +29,12 @@ export class WaitTicks extends BaseNode {
|
|||||||
this._value = 0;
|
this._value = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
protected override open(): void {
|
||||||
super.initialize(tree);
|
super.open();
|
||||||
this._value = 0;
|
this._value = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
if (++this._value >= this._max) {
|
if (++this._value >= this._max) {
|
||||||
return Status.SUCCESS;
|
return Status.SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ export class WaitTicks extends BaseNode {
|
|||||||
* 时间等待节点 时间(秒)
|
* 时间等待节点 时间(秒)
|
||||||
* 时间到后返回SUCCESS,否则返回RUNNING
|
* 时间到后返回SUCCESS,否则返回RUNNING
|
||||||
*/
|
*/
|
||||||
export class WaitTime extends BaseNode {
|
export class WaitTime extends LeafNode {
|
||||||
private _max: number;
|
private _max: number;
|
||||||
private _value: number = 0;
|
private _value: number = 0;
|
||||||
constructor(duration: number = 0) {
|
constructor(duration: number = 0) {
|
||||||
@@ -54,12 +54,12 @@ export class WaitTime extends BaseNode {
|
|||||||
this._max = duration * 1000;
|
this._max = duration * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
protected override open(): void {
|
||||||
super.initialize(tree);
|
super.open();
|
||||||
this._value = new Date().getTime();
|
this._value = new Date().getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
const currTime = new Date().getTime();
|
const currTime = new Date().getTime();
|
||||||
if (currTime - this._value >= this._max) {
|
if (currTime - this._value >= this._max) {
|
||||||
return Status.SUCCESS;
|
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 { 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<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||||
let status = this.children[i]!._execute(tree);
|
for (let i = index; i < this.children.length; i++) {
|
||||||
if (status !== Status.FAILURE) {
|
let status = this.children[i]!._execute();
|
||||||
if (status === Status.RUNNING) {
|
if (status === Status.FAILURE) {
|
||||||
this.runningIndex = i;
|
continue;
|
||||||
}
|
}
|
||||||
|
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<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
||||||
let status = this.children[i]!._execute(tree);
|
for (let i = index; i < this.children.length; i++) {
|
||||||
if (status !== Status.SUCCESS) {
|
let status = this.children[i]!._execute();
|
||||||
if (status === Status.RUNNING) {
|
if (status === Status.SUCCESS) {
|
||||||
this.runningIndex = i;
|
continue;
|
||||||
}
|
}
|
||||||
return status;
|
if (status === Status.FAILURE) {
|
||||||
|
return Status.FAILURE;
|
||||||
}
|
}
|
||||||
|
this.set(`__nMemoryRunningIndex`, i);
|
||||||
|
return Status.RUNNING;
|
||||||
}
|
}
|
||||||
return Status.SUCCESS;
|
return Status.SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -46,29 +53,30 @@ export class MemSequence extends MemoryComposite {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机选择节点
|
* 随机选择节点
|
||||||
* 从Child Node中随机选择一个执行
|
* 随机选择一个子节点执行
|
||||||
|
* 返回子节点状态
|
||||||
*/
|
*/
|
||||||
export class RandomSelector extends Composite {
|
export class RandomSelector extends Composite {
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
if (this.children.length === 0) {
|
if (this.children.length === 0) {
|
||||||
return Status.FAILURE;
|
return Status.FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const childIndex = Math.floor(Math.random() * this.children.length);
|
const childIndex = Math.floor(Math.random() * this.children.length);
|
||||||
const status = this.children[childIndex]!._execute(tree);
|
const status = this.children[childIndex]!._execute();
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 选择节点,选择不为 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<T>(tree: BehaviorTree<T>): 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(tree);
|
let status = this.children[i]!._execute();
|
||||||
if (status !== Status.FAILURE) {
|
if (status !== Status.FAILURE) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -78,38 +86,35 @@ 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<T>(tree: BehaviorTree<T>): 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(tree);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 并行节点 每次进入全部重新执行一遍
|
* 并行节点 从上到下执行 全部执行一遍
|
||||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
* 返回优先级 FAILURE > RUNNING > SUCCESS
|
||||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
|
||||||
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING
|
|
||||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
|
||||||
*/
|
*/
|
||||||
export class Parallel extends Composite {
|
export class Parallel extends Composite {
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
let result = Status.SUCCESS;
|
let result = Status.SUCCESS;
|
||||||
for (let i = 0; i < this.children.length; i++) {
|
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) {
|
if (result === Status.FAILURE || status === Status.FAILURE) {
|
||||||
result = Status.FAILURE;
|
result = Status.FAILURE;
|
||||||
} else if (result == Status.SUCCESS && status == Status.RUNNING) {
|
} else if (status === Status.RUNNING) {
|
||||||
result = Status.RUNNING;
|
result = Status.RUNNING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,21 +123,18 @@ export class Parallel extends Composite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 并行节点 每次进入全部重新执行一遍
|
* 并行节点 从上到下执行 全部执行一遍
|
||||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
* 返回优先级 SUCCESS > RUNNING > FAILURE
|
||||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
|
||||||
* 2. 任意 Child Node 返回 SUCCESS, 本节点返回 SUCCESS
|
|
||||||
* 否则返回 RUNNING
|
|
||||||
*/
|
*/
|
||||||
export class ParallelAnySuccess extends Composite {
|
export class ParallelAnySuccess extends Composite {
|
||||||
public tick<T>(tree: BehaviorTree<T>): 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(tree);
|
let status = this.children[i]!._execute();
|
||||||
if (status == Status.FAILURE) {
|
if (result === Status.SUCCESS || status === Status.SUCCESS) {
|
||||||
result = Status.FAILURE;
|
|
||||||
} else if (result == Status.RUNNING && status == Status.SUCCESS) {
|
|
||||||
result = Status.SUCCESS;
|
result = Status.SUCCESS;
|
||||||
|
} else if (status === Status.RUNNING) {
|
||||||
|
result = Status.RUNNING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
import type { BehaviorTree } from "../BehaviorTree";
|
|
||||||
import { Status } from "../header";
|
import { Status } from "../header";
|
||||||
import { BaseNode } from "./BaseNode";
|
import { LeafNode } from "./AbstractNodes";
|
||||||
|
import { IBTNode } from "./BTNode";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件节点
|
* 条件节点
|
||||||
* 根据条件函数返回SUCCESS或FAILURE
|
* 根据条件函数返回SUCCESS或FAILURE
|
||||||
*/
|
*/
|
||||||
export class Condition extends BaseNode {
|
export class Condition extends LeafNode {
|
||||||
/** 执行函数 @internal */
|
/** 执行函数 @internal */
|
||||||
private readonly _func: (subject: any) => boolean;
|
private readonly _func: (node: IBTNode) => boolean;
|
||||||
constructor(func: (subject: any) => boolean) {
|
constructor(func: (node: IBTNode) => boolean) {
|
||||||
super();
|
super();
|
||||||
this._func = func;
|
this._func = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
return this._func?.(tree.subject) ? Status.SUCCESS : Status.FAILURE;
|
return this._func?.(this) ? Status.SUCCESS : Status.FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,10 +4,9 @@
|
|||||||
* @Description: 装饰节点 装饰节点下必须包含子节点
|
* @Description: 装饰节点 装饰节点下必须包含子节点
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { BehaviorTree } from "../BehaviorTree";
|
|
||||||
import { Status } from "../header";
|
import { Status } from "../header";
|
||||||
import { Decorator, NumericDecorator } from "./AbstractNodes";
|
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
|
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
|
||||||
*/
|
*/
|
||||||
export class Inverter extends Decorator {
|
export class Inverter extends Decorator {
|
||||||
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) {
|
if (status === Status.SUCCESS) {
|
||||||
return Status.FAILURE;
|
return Status.FAILURE;
|
||||||
@@ -41,44 +40,35 @@ export class LimitTime extends NumericDecorator {
|
|||||||
* @param child 子节点
|
* @param child 子节点
|
||||||
* @param max 最大时间 (秒) 默认1秒
|
* @param max 最大时间 (秒) 默认1秒
|
||||||
*/
|
*/
|
||||||
constructor(child: BaseNode, max: number = 1) {
|
constructor(child: IBTNode, max: number = 1) {
|
||||||
super(child, max * 1000);
|
super(child, max * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
protected override open(): void {
|
||||||
super.initialize(tree);
|
|
||||||
this._value = Date.now();
|
this._value = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
const currentTime = Date.now();
|
const currentTime = Date.now();
|
||||||
|
|
||||||
if (currentTime - this._value > this._max) {
|
if (currentTime - this._value > this._max) {
|
||||||
return Status.FAILURE;
|
return Status.FAILURE;
|
||||||
}
|
}
|
||||||
|
return this.children[0]!._execute();
|
||||||
return this.children[0]!._execute(tree);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 次数限制节点
|
* 次数限制节点
|
||||||
* 必须且只能包含一个子节点
|
* 必须且只能包含一个子节点
|
||||||
* 次数限制内, 返回子节点的状态, 次数达到后, 直接返回失败
|
* 次数超过后, 直接返回失败; 次数未超过, 返回子节点状态
|
||||||
*/
|
*/
|
||||||
export class LimitTimes extends NumericDecorator {
|
export class LimitTicks extends NumericDecorator {
|
||||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
public tick(): Status {
|
||||||
if (this._value >= this._max) {
|
this._value++;
|
||||||
|
if (this._value > this._max) {
|
||||||
return Status.FAILURE;
|
return Status.FAILURE;
|
||||||
}
|
}
|
||||||
const status = this.children[0]!._execute(tree);
|
return this.children[0]!._execute();
|
||||||
if (status !== Status.RUNNING) {
|
|
||||||
this._value++;
|
|
||||||
if (this._value < this._max) {
|
|
||||||
return Status.RUNNING;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,9 +79,9 @@ export class LimitTimes extends NumericDecorator {
|
|||||||
* 次数超过之后返回子节点状态,否则返回 RUNNING
|
* 次数超过之后返回子节点状态,否则返回 RUNNING
|
||||||
*/
|
*/
|
||||||
export class Repeat extends NumericDecorator {
|
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) {
|
if (status === Status.SUCCESS || status === Status.FAILURE) {
|
||||||
this._value++;
|
this._value++;
|
||||||
@@ -112,8 +102,8 @@ export class Repeat extends NumericDecorator {
|
|||||||
* 子节点成功 计数+1
|
* 子节点成功 计数+1
|
||||||
*/
|
*/
|
||||||
export class RepeatUntilFailure extends NumericDecorator {
|
export class RepeatUntilFailure 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.FAILURE) {
|
if (status === Status.FAILURE) {
|
||||||
return Status.FAILURE;
|
return Status.FAILURE;
|
||||||
}
|
}
|
||||||
@@ -136,9 +126,9 @@ export class RepeatUntilFailure extends NumericDecorator {
|
|||||||
* 子节点失败, 计数+1
|
* 子节点失败, 计数+1
|
||||||
*/
|
*/
|
||||||
export class RepeatUntilSuccess extends NumericDecorator {
|
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) {
|
if (status === Status.SUCCESS) {
|
||||||
return Status.SUCCESS;
|
return Status.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Blackboard } from "./Blackboard";
|
import { Blackboard, IBlackboard } from "./Blackboard";
|
||||||
import { BaseNode } from "./BTNode/BaseNode";
|
import { IBTNode } from "./BTNode/BTNode";
|
||||||
|
import { Status } from "./header";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 行为树
|
* 行为树
|
||||||
@@ -9,36 +10,23 @@ export class BehaviorTree<T> {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
private _root: BaseNode;
|
private _root: IBTNode;
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
private _blackboard: Blackboard;
|
private _blackboard: IBlackboard;
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
private _subject: T;
|
|
||||||
|
|
||||||
/**
|
get root(): IBTNode { return this._root; }
|
||||||
* 节点ID计数器,每个树实例独立管理
|
get blackboard(): IBlackboard { return this._blackboard }
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
private _nodeIdCounter: number = 0;
|
|
||||||
|
|
||||||
get root(): BaseNode { return this._root; }
|
|
||||||
get blackboard() { return this._blackboard }
|
|
||||||
get subject(): T { return this._subject; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* constructor
|
* constructor
|
||||||
* @param subject 主体
|
* @param entity 实体
|
||||||
* @param root 根节点
|
* @param root 根节点
|
||||||
*/
|
*/
|
||||||
constructor(subject: T, root: BaseNode) {
|
constructor(entity: T, root: IBTNode) {
|
||||||
this._root = root;
|
this._root = root;
|
||||||
this._blackboard = new Blackboard();
|
this._blackboard = new Blackboard(undefined, entity);
|
||||||
this._subject = subject;
|
|
||||||
|
|
||||||
// 构造时就初始化所有节点ID,避免运行时检查
|
// 构造时就初始化所有节点ID,避免运行时检查
|
||||||
this._initializeAllNodeIds(this._root);
|
this._initializeAllNodeIds(this._root);
|
||||||
}
|
}
|
||||||
@@ -46,17 +34,8 @@ export class BehaviorTree<T> {
|
|||||||
/**
|
/**
|
||||||
* 执行行为树
|
* 执行行为树
|
||||||
*/
|
*/
|
||||||
public tick(): void {
|
public tick(): Status {
|
||||||
this._root._execute(this);
|
return this._root._execute();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成节点ID
|
|
||||||
* 每个树实例独立管理节点ID,避免全局状态污染
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
private _generateNodeId(): string {
|
|
||||||
return `${++this._nodeIdCounter}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,13 +44,12 @@ export class BehaviorTree<T> {
|
|||||||
* @param node 要初始化的节点
|
* @param node 要初始化的节点
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
private _initializeAllNodeIds(node: BaseNode): void {
|
private _initializeAllNodeIds(node: IBTNode, parent?: IBTNode): void {
|
||||||
// 设置当前节点ID
|
// 设置当前节点ID
|
||||||
node.id = this._generateNodeId();
|
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);
|
this._initializeAllNodeIds(child, node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,18 +58,6 @@ export class BehaviorTree<T> {
|
|||||||
* 清空黑板并重置所有节点状态
|
* 清空黑板并重置所有节点状态
|
||||||
*/
|
*/
|
||||||
public reset(): void {
|
public reset(): void {
|
||||||
this._blackboard.clear();
|
this._blackboard.clean();
|
||||||
// 重置所有节点的状态
|
|
||||||
this._root.cleanupAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重置指定记忆节点的记忆状态
|
|
||||||
* 用于精确控制记忆节点的重置,而不影响其他状态
|
|
||||||
* @param node 记忆节点
|
|
||||||
*/
|
|
||||||
public resetMemoryNode(node: BaseNode): void {
|
|
||||||
// 通过黑板标记该节点需要重置记忆
|
|
||||||
this._blackboard.set(`reset_memory`, true, node);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,67 +4,93 @@
|
|||||||
* @Description: 行为树共享数据
|
* @Description: 行为树共享数据
|
||||||
*
|
*
|
||||||
* 专门用于存储和管理行为树执行过程中的共享数据
|
* 专门用于存储和管理行为树执行过程中的共享数据
|
||||||
* 使用 Symbol 作为键实现高性能且安全的键值存储
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// 为了避免循环依赖,我们定义一个最小接口
|
import { IBTNode } from "./BTNode/BTNode";
|
||||||
interface IBlackboardNode {
|
|
||||||
readonly id: string;
|
/**
|
||||||
|
* 黑板数据接口
|
||||||
|
*/
|
||||||
|
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();
|
this._data.clear();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// 重置运行状态
|
||||||
* 设置数据
|
this.openNodes = new WeakMap<IBTNode, boolean>();
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
map.set(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// 递归清理所有子黑板
|
||||||
* 获取数据
|
for (const child of this.children) {
|
||||||
* @param key 键名
|
child.clean();
|
||||||
* @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 { Blackboard } from "./behaviortree/Blackboard";
|
||||||
export * from "./behaviortree/BTNode/AbstractNodes";
|
export * from "./behaviortree/BTNode/AbstractNodes";
|
||||||
export * from "./behaviortree/BTNode/Action";
|
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 * from "./behaviortree/BTNode/Composite";
|
||||||
export { Condition } from "./behaviortree/BTNode/Condition";
|
export { Condition } from "./behaviortree/BTNode/Condition";
|
||||||
export * from "./behaviortree/BTNode/Decorator";
|
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