mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-26 16:48:56 +00:00
重构黑板数据结构
This commit is contained in:
@@ -156,8 +156,8 @@ enum Status {
|
||||
|
||||
```typescript
|
||||
// 在节点中使用黑板
|
||||
class CustomAction extends BaseNode {
|
||||
tick<T>(tree: BehaviorTree<T>): Status {
|
||||
class CustomAction extends BTNode {
|
||||
tick: Status {
|
||||
// 获取数据 - 使用节点实例作为命名空间
|
||||
const data = tree.blackboard.get<string>("key", this);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
169
src/behaviortree/BTNode/BTNode.ts
Normal file
169
src/behaviortree/BTNode/BTNode.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { globalBlackboard, IBlackboard } from "../Blackboard";
|
||||
import { Status } from "../header";
|
||||
|
||||
export interface IBTNode {
|
||||
readonly children: IBTNode[];
|
||||
/** 本节点的的黑板引用 */
|
||||
blackboard: IBlackboard;
|
||||
/**
|
||||
* 初始化节点
|
||||
* @param root 树根节点的黑板
|
||||
* @param parent 父节点的黑板
|
||||
*/
|
||||
_initialize(root: IBlackboard, parent: IBlackboard): void;
|
||||
|
||||
_execute(): Status;
|
||||
tick(): Status;
|
||||
cleanupAll(): void;
|
||||
|
||||
/**
|
||||
* 优先写入自己的黑板数据, 如果没有则写入父节点的黑板数据
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 基础节点
|
||||
* 每个节点只管理自己需要的状态
|
||||
*/
|
||||
export abstract class BTNode implements IBTNode {
|
||||
public readonly children: IBTNode[];
|
||||
|
||||
/** 树根节点的黑板引用 */
|
||||
protected _root!: IBlackboard;
|
||||
/** 本节点的的黑板引用 可能等于 _parent */
|
||||
protected _local!: IBlackboard;
|
||||
|
||||
private _isRunning: boolean;
|
||||
/**
|
||||
* 创建
|
||||
* @param children 子节点列表
|
||||
*/
|
||||
constructor(children?: IBTNode[]) {
|
||||
this.children = children ? [...children] : [];
|
||||
this._isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开节点
|
||||
* @param tree 行为树
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public _initialize(root: IBlackboard, parent: IBlackboard): void {
|
||||
this._root = root;
|
||||
// 在需要的节点中重写,创建新的local
|
||||
this._local = parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行节点
|
||||
* @internal
|
||||
*/
|
||||
public _execute(): Status {
|
||||
// 首次执行时初始化
|
||||
if (!this._isRunning) {
|
||||
this._isRunning = true;
|
||||
this.open();
|
||||
}
|
||||
|
||||
// 执行核心逻辑
|
||||
const status = this.tick();
|
||||
|
||||
// 执行完成时清理
|
||||
if (status !== Status.RUNNING) {
|
||||
this._isRunning = false;
|
||||
this.close();
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化节点(首次执行时调用)
|
||||
* 子类重写此方法进行状态初始化
|
||||
*/
|
||||
protected open(): void { }
|
||||
|
||||
/**
|
||||
* 执行节点逻辑
|
||||
* 子类必须实现此方法
|
||||
* @returns 执行状态
|
||||
*/
|
||||
public abstract tick(): Status;
|
||||
|
||||
/**
|
||||
* 清理节点(执行完成时调用)
|
||||
* 子类重写此方法进行状态清理
|
||||
*/
|
||||
protected close(): void { }
|
||||
|
||||
/**
|
||||
* 递归清理节点及其所有子节点的状态
|
||||
* 用于行为树中断时清理所有节点状态
|
||||
*/
|
||||
public cleanupAll(): void {
|
||||
// 清理基础状态
|
||||
this._isRunning = false;
|
||||
|
||||
// 递归清理所有子节点
|
||||
for (const child of this.children) {
|
||||
child.cleanupAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置获取全局黑板数据
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
public get blackboard(): 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,4 +1,3 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||
|
||||
@@ -8,12 +7,13 @@ import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
|
||||
*/
|
||||
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);
|
||||
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) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
this.set(`__nMemoryRunningIndex`, i);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -30,12 +30,13 @@ export class MemSelector extends MemoryComposite {
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
*/
|
||||
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);
|
||||
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) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
this.set(`__nMemoryRunningIndex`, i);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -49,13 +50,13 @@ 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;
|
||||
}
|
||||
}
|
||||
@@ -66,9 +67,9 @@ export class RandomSelector extends Composite {
|
||||
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
|
||||
*/
|
||||
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;
|
||||
}
|
||||
@@ -84,9 +85,9 @@ export class Selector extends Composite {
|
||||
* 所有节点都返回 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);
|
||||
let status = this.children[i]!._execute();
|
||||
if (status !== Status.SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
@@ -96,21 +97,24 @@ export class Sequence extends Composite {
|
||||
}
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
* 并行节点 每次进入全部执行一遍
|
||||
* 它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 任意子节点返回 FAILURE, 返回 FAILURE
|
||||
* 2. 否则 任意子节点返回 RUNNING, 返回 RUNNING
|
||||
* 3. 全部成功, 才返回 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) {
|
||||
continue;
|
||||
}
|
||||
if (status === Status.RUNNING) {
|
||||
result = Status.RUNNING;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -119,20 +123,23 @@ export class Parallel extends Composite {
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 任意 Child Node 返回 SUCCESS, 本节点返回 SUCCESS
|
||||
* 它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 任意子节点返回 SUCCESS, 返回 SUCCESS
|
||||
* 2. 否则, 任意子节点返回 FAILURE, 返回 FAILURE
|
||||
* 否则返回 RUNNING
|
||||
*/
|
||||
export class ParallelAnySuccess extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
let result = Status.RUNNING;
|
||||
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;
|
||||
continue;
|
||||
}
|
||||
if (status === Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
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.blackboard : this._blackboard);
|
||||
// 递归设置所有子节点ID
|
||||
for (const child of node.children) {
|
||||
this._initializeAllNodeIds(child);
|
||||
this._initializeAllNodeIds(child, node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,14 +62,4 @@ export class BehaviorTree<T> {
|
||||
// 重置所有节点的状态
|
||||
this._root.cleanupAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置指定记忆节点的记忆状态
|
||||
* 用于精确控制记忆节点的重置,而不影响其他状态
|
||||
* @param node 记忆节点
|
||||
*/
|
||||
public resetMemoryNode(node: BaseNode): void {
|
||||
// 通过黑板标记该节点需要重置记忆
|
||||
this._blackboard.set(`reset_memory`, true, node);
|
||||
}
|
||||
}
|
||||
@@ -4,67 +4,90 @@
|
||||
* @Description: 行为树共享数据
|
||||
*
|
||||
* 专门用于存储和管理行为树执行过程中的共享数据
|
||||
* 使用 Symbol 作为键实现高性能且安全的键值存储
|
||||
*/
|
||||
|
||||
// 为了避免循环依赖,我们定义一个最小接口
|
||||
interface IBlackboardNode {
|
||||
readonly id: string;
|
||||
|
||||
/**
|
||||
* 黑板数据接口
|
||||
*/
|
||||
export interface IBlackboard {
|
||||
get<T>(key: string): T;
|
||||
set<T>(key: string, value: T): void;
|
||||
delete(key: string): void;
|
||||
has(key: string): boolean;
|
||||
clear(): void;
|
||||
createChild(scope?: number): IBlackboard;
|
||||
}
|
||||
|
||||
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>();
|
||||
|
||||
/** 实体 */
|
||||
private readonly _entity: any;
|
||||
public get entity(): any {
|
||||
return this._entity || this.parent?.entity;
|
||||
}
|
||||
|
||||
constructor(parent?: Blackboard, entity?: any) {
|
||||
this.parent = parent;
|
||||
if (parent) {
|
||||
parent.children.add(this);
|
||||
}
|
||||
|
||||
this._entity = entity;
|
||||
}
|
||||
|
||||
/** 核心: 查找链实现 */
|
||||
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 clear(): void {
|
||||
// 从父黑板中删除自己
|
||||
if (this.parent) {
|
||||
this.parent.children.delete(this);
|
||||
}
|
||||
|
||||
// 清理所有子黑板
|
||||
this.children.forEach(child => {
|
||||
child.parent = undefined;
|
||||
});
|
||||
|
||||
this.children.clear();
|
||||
|
||||
// 断开父级引用
|
||||
this.parent = undefined;
|
||||
|
||||
// 清空当前黑板数据
|
||||
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);
|
||||
}
|
||||
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";
|
||||
|
||||
Reference in New Issue
Block a user