项目重构,破坏性更新

This commit is contained in:
gongxh
2025-09-02 17:05:46 +08:00
parent 0b6b6c0be3
commit 7cd19a373b
18 changed files with 1059 additions and 989 deletions

View File

@@ -1,48 +0,0 @@
import { BehaviorTree } from "./BehaviorTree";
import { Blackboard } from "./Blackboard";
import { Ticker } from "./Ticker";
/** 代理 */
export class Agent {
/** 行为树 */
public tree: BehaviorTree;
/** 黑板 */
public blackboard: Blackboard;
/** 更新器 */
public ticker: Ticker;
/**
* constructor
* @param subject // 主体
* @param tree 行为树
*/
constructor(subject: any, tree: BehaviorTree) {
this.tree = tree;
this.blackboard = new Blackboard();
this.ticker = new Ticker(subject, this.blackboard, tree);
}
/**
* 执行
*/
public tick(): void {
this.tree.tick(this.ticker.subject, this.blackboard, this.ticker);
if (this.blackboard.interrupt) {
this.blackboard.interrupt = false;
let ticker = this.ticker;
ticker.openNodes.length = 0;
ticker.nodeCount = 0;
this.blackboard.clear();
}
}
/**
* 打断行为树,重新开始执行(如果当前在节点中,下一帧才会清理)
*/
public interruptBTree(): void {
if (!this.blackboard.interruptDefend) {
this.blackboard.interrupt = true;
}
}
}

View File

@@ -0,0 +1,70 @@
/**
* @Author: Gongxh
* @Date: 2025-09-01
* @Description: 抽象节点基类
*/
import { BehaviorTree } from "../BehaviorTree";
import { BaseNode } from "./BaseNode";
/**
* 可以包含多个节点的集合装饰器基类
*/
export abstract class Composite extends BaseNode {
constructor(...children: BaseNode[]) {
super(children);
}
}
/**
* 修饰节点基类
* 只能包含一个子节点
*/
export abstract class Decorator extends BaseNode {
constructor(child: BaseNode) {
super([child]);
}
}
/**
* 数值型装饰节点基类
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的装饰节点
*/
export abstract class NumericDecorator extends Decorator {
protected readonly _max: number;
protected _value: number = 0;
constructor(child: BaseNode, max: number = 1) {
super(child);
this._max = max;
}
protected override initialize<T>(tree: BehaviorTree<T>): void {
super.initialize(tree);
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 resetMemory(): void {
this.runningIndex = 0;
}
}

View File

@@ -1,117 +1,41 @@
import type { BehaviorTree } from "../BehaviorTree";
import { Status } from "../header";
import { Ticker } from "../Ticker";
import { BaseNode } from "./BaseNode";
/**
* 动作节点
* 没有子节点
*/
export abstract class Action extends BaseNode {
constructor() {
super();
}
}
/**
* 失败节点(无子节点)
* 直接返回FAILURE
*/
export class Failure extends Action {
/** 执行函数 @internal */
private _func: () => void;
constructor(func: () => void) {
export class Action extends BaseNode {
protected _func: (subject?: any) => Status;
constructor(func: (subject?: any) => Status) {
super();
this._func = func;
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
this._func();
return Status.FAILURE;
public tick<T>(tree: BehaviorTree<T>): Status {
return this._func?.(tree.subject) ?? Status.SUCCESS;
}
}
/**
* 逻辑节点,一直执行 (无子节点)
* 直接返回RUNING
*/
export class Running extends Action {
/** 执行函数 @internal */
private _func: () => void;
constructor(func: () => void) {
super();
this._func = func;
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
this._func();
return Status.RUNNING;
}
}
/**
* 成功节点 无子节点
* 直接返回SUCCESS
*/
export class Success extends Action {
/** 执行函数 @internal */
private _func: () => void;
constructor(func: () => void) {
super();
this._func = func;
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
this._func();
return Status.SUCCESS;
}
}
/**
* 次数等待节点(无子节点)
* 次数内返回RUNING
* 次数内返回RUNNING
* 超次返回SUCCESS
*/
export class WaitTicks extends Action {
/** 最大次数 @internal */
private _maxTicks: number;
/** 经过的次数 @internal */
private _elapsedTicks: number;
export class WaitTicks extends BaseNode {
private _max: number;
private _value: number;
constructor(maxTicks: number = 0) {
super();
this._maxTicks = maxTicks;
this._elapsedTicks = 0;
this._max = maxTicks;
this._value = 0;
}
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
this._elapsedTicks = 0;
protected override initialize<T>(tree: BehaviorTree<T>): void {
super.initialize(tree);
this._value = 0;
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (++this._elapsedTicks >= this._maxTicks) {
this._elapsedTicks = 0;
public tick<T>(tree: BehaviorTree<T>): Status {
if (++this._value >= this._max) {
return Status.SUCCESS;
}
return Status.RUNNING;
@@ -119,71 +43,27 @@ export class WaitTicks extends Action {
}
/**
* 时间等待节点(无子节点)
* 时间到后返回SUCCESS否则返回RUNING
* 时间等待节点 时间(秒)
* 时间到后返回SUCCESS否则返回RUNNING
*/
export class WaitTime extends Action {
/** 等待时间(秒 s) @internal */
private _duration: number;
export class WaitTime extends BaseNode {
private _max: number;
private _value: number = 0;
constructor(duration: number = 0) {
super();
this._duration = duration * 1000;
this._max = duration * 1000;
}
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
let startTime = new Date().getTime();
ticker.blackboard.set("startTime", startTime, ticker.tree.id, this.id);
protected override initialize<T>(tree: BehaviorTree<T>): void {
super.initialize(tree);
this._value = new Date().getTime();
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
let currTime = new Date().getTime();
let startTime = ticker.blackboard.get("startTime", ticker.tree.id, this.id);
if (currTime - startTime >= this._duration) {
public tick<T>(tree: BehaviorTree<T>): Status {
const currTime = new Date().getTime();
if (currTime - this._value >= this._max) {
return Status.SUCCESS;
}
return Status.RUNNING;
}
}
/**
* 行为树防止被打断节点
* 直接返回 SUCCESS
* 和 InterruptDefendCancel 必须成对出现
*/
export class InterruptDefend extends Action {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
ticker.blackboard.interruptDefend = true;
return Status.SUCCESS;
}
}
/**
* 行为树被打断取消节点
* 直接返回 SUCCESS
* 和 InterruptDefend 必须成对出现
*/
export class InterruptDefendCancel extends Action {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
ticker.blackboard.interruptDefend = false;
return Status.SUCCESS;
}
}

View File

@@ -1,113 +1,86 @@
import { createUUID, Status } from "../header";
import { Ticker } from "../Ticker";
import { BehaviorTree } from "../BehaviorTree";
import { Status } from "../header";
/**
* 基础节点
* 所有节点全部继承自 BaseNode
* 每个节点只管理自己需要的状态
*/
export abstract class BaseNode {
/** 唯一标识 */
public id: string;
/** 子节点 */
public children: 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 = createUUID();
this.children = [];
if (!children) {
return;
}
for (let i = 0; i < children.length; i++) {
this.children.push(children[i]);
}
this._id = ""; // 临时值,将在树构造时被正确设置
this.children = children ? [...children] : [];
this._isRunning = false;
}
/**
* 执行节点
* @param ticker 更新器
* @returns {Status} 状态
* @param tree 行为树
* @returns 状态
*/
public _execute(ticker: Ticker): Status {
/* ENTER */
this._enter(ticker);
if (!ticker.blackboard.get("isOpen", ticker.tree.id, this.id)) {
this._open(ticker);
public _execute<T>(tree: BehaviorTree<T>): Status {
// 首次执行时初始化
if (!this._isRunning) {
this._isRunning = true;
this.initialize(tree);
}
let status = this._tick(ticker);
// 执行核心逻辑
const status = this.tick(tree);
// 执行完成时清理
if (status !== Status.RUNNING) {
this._close(ticker);
this._isRunning = false;
this.cleanup(tree);
}
this._exit(ticker);
return status;
}
/**
* 进入节点
* @param ticker 更新器
* @internal
* 初始化节点(首次执行时调用)
* 子类重写此方法进行状态初始化
* @param tree 行为树
*/
public _enter(ticker: Ticker): void {
ticker.enterNode(this);
this.enter(ticker);
}
protected initialize<T>(tree: BehaviorTree<T>): void { }
/**
* 打开节点
* @param ticker 更新器
* @internal
* 清理节点(执行完成时调用)
* 子类重写此方法进行状态清理
* @param tree 行为树
*/
public _open(ticker: Ticker): void {
ticker.openNode(this);
ticker.blackboard.set("isOpen", true, ticker.tree.id, this.id);
this.open(ticker);
}
protected cleanup<T>(tree: BehaviorTree<T>): void { }
/**
* 更新节点
* @param ticker 更新器
* @internal
* 执行节点逻辑
* 子类必须实现此方法
* @param tree 行为树
* @returns 执行状态
*/
public _tick(ticker: Ticker): Status {
ticker.tickNode(this);
return this.tick(ticker);
}
public abstract tick<T>(tree: BehaviorTree<T>): Status;
/**
* 关闭节点
* @param ticker 更新器
* @internal
* 递归清理节点及其所有子节点的状态
* 用于行为树中断时清理所有节点状态
*/
public _close(ticker: Ticker): void {
ticker.closeNode(this);
ticker.blackboard.set("isOpen", false, ticker.tree.id, this.id);
this.close(ticker);
}
public cleanupAll(): void {
// 清理基础状态
this._isRunning = false;
/**
* 退出节点
* @param ticker 更新器
* @internal
*/
public _exit(ticker: Ticker): void {
ticker.exitNode(this);
this.exit(ticker);
// 递归清理所有子节点
for (const child of this.children) {
child.cleanupAll();
}
}
enter(ticker: Ticker): void {
}
open(ticker: Ticker): void {
}
close(ticker: Ticker): void {
}
exit(ticker: Ticker): void {
}
abstract tick(ticker: Ticker): Status;
}

View File

@@ -1,84 +1,41 @@
import type { BehaviorTree } from "../BehaviorTree";
import { Status } from "../header";
import { Ticker } from "../Ticker";
import { BaseNode } from "./BaseNode";
/**
* 可以包含多个节点的集合装饰器基类
*
*/
export abstract class Composite extends BaseNode {
constructor(...children: BaseNode[]) {
super(children);
}
}
import { Composite, MemoryComposite } from "./AbstractNodes";
/**
* 记忆选择节点
* 选择不为 FAILURE 的节点
* 选择不为 FAILURE 的节点,记住上次运行的子节点位置
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
*/
export class MemSelector extends Composite {
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
super.open(ticker);
ticker.blackboard.set("runningChild", 0, ticker.tree.id, this.id);
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
let childIndex = ticker.blackboard.get("runningChild", ticker.tree.id, this.id) as number;
for (let i = childIndex; i < this.children.length; i++) {
let status = this.children[i]._execute(ticker);
export class MemSelector extends MemoryComposite {
public tick<T>(tree: BehaviorTree<T>): Status {
for (let i = this.runningIndex; i < this.children.length; i++) {
let status = this.children[i]!._execute(tree);
if (status !== Status.FAILURE) {
if (status === Status.RUNNING) {
ticker.blackboard.set("runningChild", i, ticker.tree.id, this.id);
this.runningIndex = i;
}
return status;
}
}
return Status.FAILURE;
}
}
/**
* 记忆顺序节点
* 如果上次执行到 RUNING 的节点, 下次进入节点后, 直接从 RUNING 节点开始
* 遇到 RUNING 或者 FAILURE 停止迭代
* 如果上次执行到 RUNNING 的节点, 下次进入节点后, 直接从 RUNNING 节点开始
* 遇到 SUCCESS 或者 FAILURE 停止迭代
* 任意一个Child Node返回不为 SUCCESS, 本Node向自己的Parent Node也返回Child Node状态
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
*/
export class MemSequence extends Composite {
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
super.open(ticker);
ticker.blackboard.set("runningChild", 0, ticker.tree.id, this.id);
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
let childIndex = ticker.blackboard.get("runningChild", ticker.tree.id, this.id) as number;
for (let i = childIndex; i < this.children.length; i++) {
let status = this.children[i]._execute(ticker);
export class MemSequence extends MemoryComposite {
public tick<T>(tree: BehaviorTree<T>): Status {
for (let i = this.runningIndex; i < this.children.length; i++) {
let status = this.children[i]!._execute(tree);
if (status !== Status.SUCCESS) {
if (status === Status.RUNNING) {
ticker.blackboard.set("runningChild", i, ticker.tree.id, this.id);
this.runningIndex = i;
}
return status;
}
@@ -92,34 +49,26 @@ export class MemSequence extends Composite {
* 从Child Node中随机选择一个执行
*/
export class RandomSelector extends Composite {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
let childIndex = (Math.random() * this.children.length) | 0;
let child = this.children[childIndex];
let status = child._execute(ticker);
public tick<T>(tree: BehaviorTree<T>): 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);
return status;
}
}
/**
* 选择节点,选择不为 FAILURE 的节点
* 当执行本类型Node时它将从begin到end迭代执行自己的Child Node
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNING那停止迭代本Node向自己的Parent Node也返回 SUCCESS 或 RUNING
* 当执行本Node时它将从begin到end迭代执行自己的Child Node
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING那停止迭代本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
*/
export class Selector extends Composite {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
public tick<T>(tree: BehaviorTree<T>): Status {
for (let i = 0; i < this.children.length; i++) {
let status = this.children[i]._execute(ticker);
let status = this.children[i]!._execute(tree);
if (status !== Status.FAILURE) {
return status;
}
@@ -131,18 +80,13 @@ export class Selector extends Composite {
/**
* 顺序节点
* 当执行本类型Node时它将从begin到end迭代执行自己的Child Node
* 遇到 FAILURE 或 RUNING, 那停止迭代返回FAILURE 或 RUNING
* 遇到 FAILURE 或 RUNNING, 那停止迭代返回FAILURE 或 RUNNING
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
*/
export class Sequence extends Composite {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
public tick<T>(tree: BehaviorTree<T>): Status {
for (let i = 0; i < this.children.length; i++) {
let status = this.children[i]._execute(ticker);
let status = this.children[i]!._execute(tree);
if (status !== Status.SUCCESS) {
return status;
}
@@ -155,19 +99,14 @@ export class Sequence extends Composite {
* 并行节点 每次进入全部重新执行一遍
* 当执行本类型Node时它将从begin到end迭代执行自己的Child Node
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
* 2. 当存在Child Node执行后返回 RUNING, 本节点返回 RUNING
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
*/
export class Parallel extends Composite {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
public tick<T>(tree: BehaviorTree<T>): Status {
let result = Status.SUCCESS;
for (let i = 0; i < this.children.length; i++) {
let status = this.children[i]._execute(ticker);
let status = this.children[i]!._execute(tree);
if (status == Status.FAILURE) {
result = Status.FAILURE;
} else if (result == Status.SUCCESS && status == Status.RUNNING) {
@@ -186,15 +125,10 @@ export class Parallel extends Composite {
* 否则返回 RUNNING
*/
export class ParallelAnySuccess extends Composite {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
public tick<T>(tree: BehaviorTree<T>): Status {
let result = Status.RUNNING;
for (let i = 0; i < this.children.length; i++) {
let status = this.children[i]._execute(ticker);
let status = this.children[i]!._execute(tree);
if (status == Status.FAILURE) {
result = Status.FAILURE;
} else if (result == Status.RUNNING && status == Status.SUCCESS) {

View File

@@ -1,24 +1,20 @@
import type { BehaviorTree } from "../BehaviorTree";
import { Status } from "../header";
import { Ticker } from "../Ticker";
import { Action } from "./Action";
import { BaseNode } from "./BaseNode";
/**
* 条件节点
* 根据条件函数返回SUCCESS或FAILURE
*/
export class Condition extends Action {
export class Condition extends BaseNode {
/** 执行函数 @internal */
private _func: (subject: any) => boolean = null;
private readonly _func: (subject: any) => boolean;
constructor(func: (subject: any) => boolean) {
super();
this._func = func;
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
return this._func(ticker.subject) ? Status.SUCCESS : Status.FAILURE;
public tick<T>(tree: BehaviorTree<T>): Status {
return this._func?.(tree.subject) ? Status.SUCCESS : Status.FAILURE;
}
}

View File

@@ -1,39 +1,14 @@
/**
* @Author: Gongxh
* @Date: 2025-09-01
* @Description: 装饰节点 装饰节点下必须包含子节点
*/
import type { BehaviorTree } from "../BehaviorTree";
import { Status } from "../header";
import { Ticker } from "../Ticker";
import { Decorator, NumericDecorator } from "./AbstractNodes";
import { BaseNode } from "./BaseNode";
/**
* 修饰节点基类
* 只能包含一个子节点
*/
export abstract class Decorator extends BaseNode {
constructor(child: BaseNode) {
super([child]);
}
}
/**
* 失败节点
* 必须且只能包含一个子节点
* 直接返回 FAILURE
* @extends Decorator
*/
export class Failer extends Decorator {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(Failer)节点必须包含一个子节点");
}
let child = this.children[0];
child._execute(ticker);
return Status.FAILURE;
}
}
/**
* 结果反转节点
* 必须且只能包含一个子节点
@@ -41,327 +16,139 @@ export class Failer extends Decorator {
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
*/
export class Inverter extends Decorator {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(Inverter)节点必须包含一个子节点");
}
let child = this.children[0];
let status = child._execute(ticker);
public tick<T>(tree: BehaviorTree<T>): Status {
const status = this.children[0]!._execute(tree);
if (status === Status.SUCCESS) {
status = Status.FAILURE;
return Status.FAILURE;
} else if (status === Status.FAILURE) {
status = Status.SUCCESS;
return Status.SUCCESS;
}
return status;
return status; // RUNNING 保持不变
}
}
/**
* 时间限制节点
* 只能包含一个子节点
* 规定时间内, 根据Child Node的结果, 本节点向自己的父节点也返回相同的结果
* 超时后, 直接返回 FAILURE
*/
export class LimitTime extends NumericDecorator {
/**
* 时间限制节点
* @param child 子节点
* @param max 最大时间 (秒) 默认1秒
*/
constructor(child: BaseNode, max: number = 1) {
super(child, max * 1000);
}
protected override initialize<T>(tree: BehaviorTree<T>): void {
super.initialize(tree);
this._value = Date.now();
}
public tick<T>(tree: BehaviorTree<T>): Status {
const currentTime = Date.now();
if (currentTime - this._value > this._max) {
return Status.FAILURE;
}
return this.children[0]!._execute(tree);
}
}
/**
* 次数限制节点
* 必须且只能包含一个子节点
* 次数限制内, 根据Child Node的结果, 本Node向自己的Parent Node也返回相同的结果
* 次数超过后, 直接返回 FAILURE
* 次数限制内, 返回子节点的状态, 次数达到后, 直接返回失败
*/
export class LimiterTicks extends Decorator {
/** 最大次数 @internal */
private _maxTicks: number;
/** 当前执行过的次数 @internal */
private _elapsedTicks: number;
/**
* 创建
* @param maxTicks 最大次数
* @param child 子节点
*/
constructor(maxTicks: number, child: BaseNode) {
super(child);
this._maxTicks = maxTicks;
this._elapsedTicks = 0;
}
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
super.open(ticker);
this._elapsedTicks = 0;
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(LimiterTicks)节点必须包含一个子节点");
}
let child = this.children[0];
if (++this._elapsedTicks > this._maxTicks) {
this._elapsedTicks = 0;
export class LimitTimes extends NumericDecorator {
public tick<T>(tree: BehaviorTree<T>): Status {
if (this._value >= this._max) {
return Status.FAILURE;
}
return child._execute(ticker);
const status = this.children[0]!._execute(tree);
if (status !== Status.RUNNING) {
this._value++;
if (this._value < this._max) {
return Status.RUNNING;
}
}
return status;
}
}
/**
* 时间限制节点
* 只能包含一个子节点
* 规定时间内, 根据Child Node的结果, 本Node向自己的Parent Node也返回相同的结果
* 超时后, 直接返回 FAILURE
*/
export class LimiterTime extends Decorator {
/** 最大时间 (毫秒 ms) @internal */
private _maxTime: number;
/**
* 时间限制节点
* @param maxTime 最大时间 (微秒ms)
* @param child 子节点
*/
constructor(maxTime: number, child: BaseNode) {
super(child);
this._maxTime = maxTime * 1000;
}
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
super.open(ticker);
let startTime = new Date().getTime();
ticker.blackboard.set("startTime", startTime, ticker.tree.id, this.id);
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(LimiterTime)节点必须包含一个子节点");
}
let child = this.children[0];
let currTime = new Date().getTime();
let startTime = ticker.blackboard.get("startTime", ticker.tree.id, this.id);
if (currTime - startTime > this._maxTime) {
return Status.FAILURE;
}
return child._execute(ticker);
}
}
/**
* 循环节点
* 循环节点 最大次数必须大于0
* 必须且只能包含一个子节点
* 如果maxLoop < 0, 直接返回成功
* 否则等待次数超过之后, 返回Child Node的结果RUNING的次数不计算在内)
* 子节点是成功或失败,累加计数
* 次数超过之后返回子节点状态,否则返回 RUNNING
*/
export class Repeater extends Decorator {
/** 最大循环次数 @internal */
private _maxLoop: number;
/**
* 创建
* @param child 子节点
* @param maxLoop 最大循环次数
*/
constructor(child: BaseNode, maxLoop: number = -1) {
super(child);
this._maxLoop = maxLoop;
}
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
ticker.blackboard.set("i", 0, ticker.tree.id, this.id);
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(Repeater)节点必须包含一个子节点");
}
let child = this.children[0];
let i = ticker.blackboard.get("i", ticker.tree.id, this.id);
let status = Status.SUCCESS;
while (this._maxLoop < 0 || i < this._maxLoop) {
status = child._execute(ticker);
if (status === Status.SUCCESS || status === Status.FAILURE) {
i++;
} else {
break;
export class Repeat extends NumericDecorator {
public tick<T>(tree: BehaviorTree<T>): Status {
// 执行子节点
const status = this.children[0]!._execute(tree);
// 如果子节点完成(成功或失败),增加计数
if (status === Status.SUCCESS || status === Status.FAILURE) {
this._value++;
// 检查是否达到最大次数
if (this._value >= this._max) {
return status;
}
}
ticker.blackboard.set("i", i, ticker.tree.id, this.id);
return status;
}
}
/**
* 循环节点
* 只能包含一个子节点
* 如果maxLoop < 0, 直接返回成功
* 当Child Node返回 FAILURE, 本Node向自己的Parent Node返回 FAILURE
* 循环次数大于等于maxLoop时, 返回Child Node的结果
*/
export class RepeatUntilFailure extends Decorator {
/** 最大循环次数 @internal */
private _maxLoop: number;
constructor(child: BaseNode, maxLoop: number = -1) {
super(child);
this._maxLoop = maxLoop;
}
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
ticker.blackboard.set("i", 0, ticker.tree.id, this.id);
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(RepeatUntilFailure)节点必须包含一个子节点");
}
let child = this.children[0];
let i = ticker.blackboard.get("i", ticker.tree.id, this.id);
let status = Status.SUCCESS;
while (this._maxLoop < 0 || i < this._maxLoop) {
status = child._execute(ticker);
if (status === Status.SUCCESS) {
i++;
} else {
break;
}
}
ticker.blackboard.set("i", i, ticker.tree.id, this.id);
return status;
}
}
/**
* 循环节点(只能包含一个子节点)
* 如果maxLoop < 0, 直接返回失败
* 当Child Node返回 SUCCESS, 本Node向自己的Parent Node返回 SUCCESS
* 循环次数大于等于maxLoop时, 返回Child Node的结果
*/
export class RepeatUntilSuccess extends Decorator {
/** 最大循环次数 @internal */
private _maxLoop: number;
/**
* 创建
* @param child 子节点
* @param maxLoop 最大循环次数
*/
constructor(child: BaseNode, maxLoop: number = -1) {
super(child);
this._maxLoop = maxLoop;
}
/**
* 打开
* @param {Ticker} ticker
*/
public open(ticker: Ticker): void {
ticker.blackboard.set("i", 0, ticker.tree.id, this.id);
}
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(RepeatUntilSuccess)节点必须包含一个子节点");
}
let child = this.children[0];
let i = ticker.blackboard.get("i", ticker.tree.id, this.id);
let status = Status.FAILURE;
while (this._maxLoop < 0 || i < this._maxLoop) {
status = child._execute(ticker);
if (status === Status.FAILURE) {
i++;
} else {
break;
}
}
ticker.blackboard.set("i", i, ticker.tree.id, this.id);
return status;
}
}
/**
* 逻辑节点, 一直执行(只能包含一个子节点)
* 直接返回 RUNING
*/
export class Runner extends Decorator {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(Runner)节点必须包含一个子节点");
}
let child = this.children[0];
child._execute(ticker);
return Status.RUNNING;
}
}
/**
* 成功节点(包含一个子节点)
* 直接返回 SUCCESS
* 重复 -- 直到失败
* 节点含义重复执行直到失败但最多重试max次
* 必须且只能包含一个子节点
*
* 子节点成功 计数+1
*/
export class Succeeder extends Decorator {
/**
* 执行
* @param {Ticker} ticker
* @returns {Status}
*/
public tick(ticker: Ticker): Status {
if (this.children.length !== 1) {
throw new Error("(Succeeder)节点必须包含一个子节点");
export class RepeatUntilFailure extends NumericDecorator {
public tick<T>(tree: BehaviorTree<T>): Status {
const status = this.children[0]!._execute(tree);
if (status === Status.FAILURE) {
return Status.FAILURE;
}
let child = this.children[0];
child._execute(ticker);
return Status.SUCCESS;
if (status === Status.SUCCESS) {
this._value++;
if (this._value >= this._max) {
// 重试次数耗尽了,但是子节点一直返回成功 就返回成功
return Status.SUCCESS;
}
}
return Status.RUNNING;
}
}
/**
* 重复 -- 直到成功
* 节点含义重复执行直到成功但最多重试max次
* 必须且只能包含一个子节点
*
* 子节点失败, 计数+1
*/
export class RepeatUntilSuccess extends NumericDecorator {
public tick<T>(tree: BehaviorTree<T>): Status {
// 执行子节点
const status = this.children[0]!._execute(tree);
if (status === Status.SUCCESS) {
return Status.SUCCESS;
}
if (status === Status.FAILURE) {
this._value++;
if (this._value >= this._max) {
// 重试次数耗尽了,但是子节点一直返回失败
return Status.FAILURE;
}
}
return Status.RUNNING;
}
}

View File

@@ -1,61 +1,97 @@
import { Blackboard } from "./Blackboard";
import { BaseNode } from "./BTNode/BaseNode";
import { createUUID } from "./header";
import { Ticker } from "./Ticker";
/**
* 行为树
* 所有节点全部添加到树中
*/
export class BehaviorTree {
/** 行为树ID @internal */
private _id: string;
/** 行为树跟节点 @internal */
export class BehaviorTree<T> {
/**
* @internal
*/
private _root: BaseNode;
/**
* @internal
*/
private _blackboard: Blackboard;
/**
* @internal
*/
private _subject: T;
/**
* 节点ID计数器每个树实例独立管理
* @internal
*/
private _nodeIdCounter: number = 0;
get root(): BaseNode { return this._root; }
get blackboard() { return this._blackboard }
get subject(): T { return this._subject; }
/**
* constructor
* @param subject 主体
* @param root 根节点
*/
constructor(root: BaseNode) {
this._id = createUUID();
constructor(subject: T, root: BaseNode) {
this._root = root;
this._blackboard = new Blackboard();
this._subject = subject;
// 构造时就初始化所有节点ID避免运行时检查
this._initializeAllNodeIds(this._root);
}
/**
* 执行
* @param subject 主体
* @param blackboard 黑板
* @param ticker 更新器
* 执行行为树
*/
public tick(subject: any, blackboard: Blackboard, ticker?: Ticker): void {
ticker = ticker || new Ticker(subject, blackboard, this);
ticker.openNodes.length = 0;
this._root._execute(ticker);
// 上次打开的节点
let lastOpenNodes = (blackboard.get("openNodes", this._id) || []) as BaseNode[];
// 当前打开的节点
let currOpenNodes = ticker.openNodes;
let start = 0;
for (let i = 0; i < Math.min(lastOpenNodes.length, currOpenNodes.length); i++) {
start = i + 1;
if (lastOpenNodes[i] !== currOpenNodes[i]) {
break;
}
}
// 关闭不需要的节点
for (let i = lastOpenNodes.length - 1; i >= start; i--) {
lastOpenNodes[i]._close(ticker);
}
/* POPULATE BLACKBOARD */
blackboard.set("openNodes", currOpenNodes, this._id);
blackboard.set("nodeCount", ticker.nodeCount, this._id);
public tick(): void {
this._root._execute(this);
}
get id(): string {
return this._id;
/**
* 生成节点ID
* 每个树实例独立管理节点ID避免全局状态污染
* @internal
*/
private _generateNodeId(): string {
return `${++this._nodeIdCounter}`;
}
get root(): BaseNode {
return this._root;
/**
* 递归初始化所有节点ID
* 在构造时一次性完成,避免运行时检查
* @param node 要初始化的节点
* @internal
*/
private _initializeAllNodeIds(node: BaseNode): void {
// 设置当前节点ID
node.id = this._generateNodeId();
// 递归设置所有子节点ID
for (const child of node.children) {
this._initializeAllNodeIds(child);
}
}
/**
* 完全重置行为树(核武器级别的重置)
* 清空黑板并重置所有节点状态
*/
public reset(): void {
this._blackboard.clear();
// 重置所有节点的状态
this._root.cleanupAll();
}
/**
* 重置指定记忆节点的记忆状态
* 用于精确控制记忆节点的重置,而不影响其他状态
* @param node 记忆节点
*/
public resetMemoryNode(node: BaseNode): void {
// 通过黑板标记该节点需要重置记忆
this._blackboard.set(`reset_memory`, true, node);
}
}

View File

@@ -1,105 +1,70 @@
/**
* 行为树数据
* @Author: Gongxh
* @Date: 2025-09-02
* @Description: 行为树共享数据
*
* 专门用于存储和管理行为树执行过程中的共享数据
* 使用 Symbol 作为键实现高性能且安全的键值存储
*/
interface ITreeData {
nodeMemory: { [nodeScope: string]: any };
openNodes: any[];
// 为了避免循环依赖,我们定义一个最小接口
interface IBlackboardNode {
readonly id: string;
}
/** 平台 */
export class Blackboard {
/** 行为树打断保护 */
public interruptDefend: boolean = false;
/** 打断行为树的标记 */
public interrupt: boolean = false;
/** 基础记忆 @internal */
private _baseMemory: any;
/** 树记忆 @internal */
private _treeMemory: { [treeScope: string]: ITreeData };
private readonly _data = new Map<IBlackboardNode, Map<string, any>>();
constructor() {
this._baseMemory = {};
this._treeMemory = {};
}
/**
* 清除
*/
public clear(): void {
this._baseMemory = {};
this._treeMemory = {};
this._data.clear();
}
/**
* 设置
* @param key 键
* 设置数据
* @param key 键
* @param value 值
* @param treeScope 树范围
* @param nodeScope 节点范围
* @param node 节点实例(用于生成唯一 Symbol
*/
public set(key: string, value: any, treeScope?: string, nodeScope?: string): void {
let memory = this._getMemory(treeScope, nodeScope);
memory[key] = value;
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 treeScope 树范围
* @param nodeScope 节点范围
* 获取数据
* @param key 键
* @param node 节点实例
* @returns 值
*/
public get(key: string, treeScope?: string, nodeScope?: string): any {
let memory = this._getMemory(treeScope, nodeScope);
return memory[key];
public get<T>(key: string, node: IBlackboardNode): T | undefined {
return this._data.get(node)?.get(key) as T;
}
/**
* 获取树记忆
* @param treeScope 树范围
* @returns 树记忆
* @internal
* 检查是否存在指定键
* @param key 键名
* @param node 节点实例
* @returns 是否存在
*/
private _getTreeMemory(treeScope: string): ITreeData {
if (!this._treeMemory[treeScope]) {
this._treeMemory[treeScope] = {
nodeMemory: {},
openNodes: [],
};
}
return this._treeMemory[treeScope];
public has(key: string, node: IBlackboardNode): boolean {
return this._data.has(node) ? this._data.get(node)?.has(key) || false : false;
}
/**
* 获取节点记忆
* @param treeMemory 树记忆
* @param nodeScope 节点范围
* @returns 节点记忆
* @internal
* 删除指定键的数据
* @param key 键名
* @param node 节点实例
* @returns 是否删除成功
*/
private _getNodeMemory(treeMemory: ITreeData, nodeScope: string): { [key: string]: any } {
let memory = treeMemory.nodeMemory;
if (!memory[nodeScope]) {
memory[nodeScope] = {};
public delete(key: string, node: IBlackboardNode): boolean {
if (this.has(key, node)) {
this._data.get(node)?.delete(key);
return true;
}
return memory[nodeScope];
}
/**
* 获取记忆
* @param treeScope 树范围
* @param nodeScope 节点范围
* @returns 记忆
* @internal
*/
private _getMemory(treeScope?: string, nodeScope?: string): { [key: string]: any } {
let memory = this._baseMemory;
if (treeScope) {
memory = this._getTreeMemory(treeScope);
if (nodeScope) {
memory = this._getNodeMemory(memory, nodeScope);
}
}
return memory;
return false;
}
}

View File

@@ -1,59 +0,0 @@
import { BehaviorTree } from "./BehaviorTree";
import { Blackboard } from "./Blackboard";
import { BaseNode } from "./BTNode/BaseNode";
export class Ticker {
tree: BehaviorTree; // 行为树跟节点
openNodes: BaseNode[]; // 当前打开的节点
nodeCount: number; // 当前打开的节点数量
blackboard: Blackboard; // 数据容器
debug: any;
subject: any;
constructor(subject: any, blackboard: Blackboard, tree: BehaviorTree) {
this.tree = tree;
this.openNodes = [];
this.nodeCount = 0;
this.debug = null;
this.subject = subject;
this.blackboard = blackboard;
}
/**
* 进入节点
* @param node 节点
*/
public enterNode(node: BaseNode): void {
this.nodeCount++;
this.openNodes.push(node);
}
/**
* 打开节点
* @param node 节点
*/
public openNode(node: BaseNode): void { }
/**
* 更新节点
* @param node 节点
*/
public tickNode(node: BaseNode): void { }
/**
* 关闭节点
* @param node 节点
*/
public closeNode(node: BaseNode): void {
// 查找并移除指定节点而不是简单地pop
const index = this.openNodes.lastIndexOf(node);
if (index !== -1) {
this.openNodes.splice(index, 1);
}
}
/**
* 退出节点
* @param node 节点
*/
public exitNode(node: BaseNode): void { }
}

View File

@@ -1,27 +1,5 @@
export const enum Status {
export enum Status {
FAILURE,
SUCCESS,
RUNNING,
}
/**
* 创建UUID
* @returns UUID
* @internal
*/
export function createUUID(): string {
let s: string[] = Array(36);
let hexDigits = "0123456789abcdef";
for (let i = 0; i < 36; i++) {
let start = Math.floor(Math.random() * 0x10);
s[i] = hexDigits.substring(start, start + 1);
}
// bits 12-15 of the time_hi_and_version field to 0010
s[14] = "4";
// bits 6-7 of the clock_seq_hi_and_reserved to 01
let start = (parseInt(s[19], 16) & 0x3) | 0x8;
s[19] = hexDigits.substring(start, start + 1);
s[8] = s[13] = s[18] = s[23] = "-";
let uuid = s.join("");
return uuid;
}