重构黑板数据结构

This commit is contained in:
gongxh
2025-09-03 10:54:07 +08:00
parent 7cd19a373b
commit e9a0a15035
11 changed files with 379 additions and 305 deletions

View File

@@ -156,8 +156,8 @@ enum Status {
```typescript ```typescript
// 在节点中使用黑板 // 在节点中使用黑板
class CustomAction extends BaseNode { class CustomAction extends BTNode {
tick<T>(tree: BehaviorTree<T>): Status { tick: Status {
// 获取数据 - 使用节点实例作为命名空间 // 获取数据 - 使用节点实例作为命名空间
const data = tree.blackboard.get<string>("key", this); const data = tree.blackboard.get<string>("key", this);

View File

@@ -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;
} }
} }

View File

@@ -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;

View 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;
}
}

View File

@@ -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();
}
}
}

View File

@@ -1,4 +1,3 @@
import type { BehaviorTree } from "../BehaviorTree";
import { Status } from "../header"; import { Status } from "../header";
import { Composite, MemoryComposite } from "./AbstractNodes"; import { Composite, MemoryComposite } from "./AbstractNodes";
@@ -8,12 +7,13 @@ import { Composite, MemoryComposite } from "./AbstractNodes";
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态 * 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
*/ */
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++) {
let status = this.children[i]!._execute();
if (status !== Status.FAILURE) { if (status !== Status.FAILURE) {
if (status === Status.RUNNING) { if (status === Status.RUNNING) {
this.runningIndex = i; this.set(`__nMemoryRunningIndex`, i);
} }
return status; return status;
} }
@@ -30,12 +30,13 @@ export class MemSelector extends MemoryComposite {
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS * 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
*/ */
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++) {
let status = this.children[i]!._execute();
if (status !== Status.SUCCESS) { if (status !== Status.SUCCESS) {
if (status === Status.RUNNING) { if (status === Status.RUNNING) {
this.runningIndex = i; this.set(`__nMemoryRunningIndex`, i);
} }
return status; return status;
} }
@@ -49,13 +50,13 @@ export class MemSequence extends MemoryComposite {
* 从Child Node中随机选择一个执行 * 从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;
} }
} }
@@ -66,9 +67,9 @@ export class RandomSelector extends Composite {
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING那停止迭代本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING * 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING那停止迭代本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
*/ */
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;
} }
@@ -84,9 +85,9 @@ export class Selector extends Composite {
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS * 所有节点都返回 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; return status;
} }
@@ -96,21 +97,24 @@ export class Sequence extends Composite {
} }
/** /**
* 并行节点 每次进入全部重新执行一遍 * 并行节点 每次进入全部执行一遍
* 当执行本类型Node时它将从begin到end迭代执行自己的Child Node * 它将从begin到end迭代执行自己的Child Node
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE * 1. 任意子节点返回 FAILURE, 返回 FAILURE
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING * 2. 否则 任意子节点返回 RUNNING, 返回 RUNNING
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS * 3. 全部成功, 才返回 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) { continue;
}
if (status === Status.RUNNING) {
result = Status.RUNNING; result = Status.RUNNING;
continue;
} }
} }
return result; return result;
@@ -119,20 +123,23 @@ export class Parallel extends Composite {
/** /**
* 并行节点 每次进入全部重新执行一遍 * 并行节点 每次进入全部重新执行一遍
* 当执行本类型Node时它将从begin到end迭代执行自己的Child Node * 它将从begin到end迭代执行自己的Child Node
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE * 1. 任意子节点返回 SUCCESS, 返回 SUCCESS
* 2. 任意 Child Node 返回 SUCCESS, 本节点返回 SUCCESS * 2. 否则, 任意子节点返回 FAILURE, 返回 FAILURE
* 否则返回 RUNNING * 否则返回 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.RUNNING;
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;
continue;
}
if (status === Status.FAILURE) {
result = Status.FAILURE;
continue;
} }
} }
return result; return result;

View File

@@ -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;
} }
} }

View File

@@ -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;
} }

View File

@@ -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.blackboard : this._blackboard);
// 递归设置所有子节点ID // 递归设置所有子节点ID
for (const child of node.children) { for (const child of node.children) {
this._initializeAllNodeIds(child); this._initializeAllNodeIds(child, node);
} }
} }
@@ -84,14 +62,4 @@ export class BehaviorTree<T> {
// 重置所有节点的状态 // 重置所有节点的状态
this._root.cleanupAll(); this._root.cleanupAll();
} }
/**
* 重置指定记忆节点的记忆状态
* 用于精确控制记忆节点的重置,而不影响其他状态
* @param node 记忆节点
*/
public resetMemoryNode(node: BaseNode): void {
// 通过黑板标记该节点需要重置记忆
this._blackboard.set(`reset_memory`, true, node);
}
} }

View File

@@ -4,67 +4,90 @@
* @Description: 行为树共享数据 * @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 { 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(); 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);
} }
/** // 全局共享的黑板实例
* 获取数据 export const globalBlackboard = new Blackboard();
* @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;
}
}

View File

@@ -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";