mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-27 00:58:18 +00:00
项目重构,破坏性更新
This commit is contained in:
70
src/behaviortree/BTNode/AbstractNodes.ts
Normal file
70
src/behaviortree/BTNode/AbstractNodes.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user