2025-09-08 16:10:08 +08:00
|
|
|
|
import { BT } from "../BT";
|
2025-06-04 23:10:59 +08:00
|
|
|
|
import { Status } from "../header";
|
2025-09-02 17:05:46 +08:00
|
|
|
|
import { Composite, MemoryComposite } from "./AbstractNodes";
|
2025-09-08 16:10:08 +08:00
|
|
|
|
import { IBTNode } from "./BTNode";
|
|
|
|
|
|
import { WeightDecorator } from "./Decorator";
|
2025-06-04 23:10:59 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-04 14:08:19 +08:00
|
|
|
|
* 记忆选择节点 从上到下执行
|
|
|
|
|
|
* 遇到 FAILURE 继续下一个
|
|
|
|
|
|
* 遇到 SUCCESS 返回 SUCCESS 下次重新开始
|
|
|
|
|
|
*
|
|
|
|
|
|
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
2025-06-04 23:10:59 +08:00
|
|
|
|
*/
|
2025-09-09 09:45:51 +08:00
|
|
|
|
@BT.CompositeNode("MemSelector", {
|
2025-09-08 16:10:08 +08:00
|
|
|
|
name: "记忆选择节点",
|
|
|
|
|
|
group: "基础组合节点",
|
2025-09-09 09:45:51 +08:00
|
|
|
|
desc: "记住上次运行位置的选择节点,从记忆位置开始执行",
|
2025-09-08 16:10:08 +08:00
|
|
|
|
})
|
2025-09-02 17:05:46 +08:00
|
|
|
|
export class MemSelector extends MemoryComposite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
|
|
|
|
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
|
|
|
|
|
for (let i = index; i < this.children.length; i++) {
|
|
|
|
|
|
let status = this.children[i]!._execute();
|
2025-09-04 14:08:19 +08:00
|
|
|
|
if (status === Status.FAILURE) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status === Status.SUCCESS) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
return status;
|
|
|
|
|
|
}
|
2025-09-04 14:08:19 +08:00
|
|
|
|
this.set(`__nMemoryRunningIndex`, i);
|
|
|
|
|
|
return Status.RUNNING;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Status.FAILURE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-04 14:08:19 +08:00
|
|
|
|
* 记忆顺序节点 从上到下执行
|
|
|
|
|
|
* 遇到 SUCCESS 继续下一个
|
|
|
|
|
|
* 遇到 FAILURE 停止迭代 返回 FAILURE 下次重新开始
|
|
|
|
|
|
*
|
|
|
|
|
|
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
2025-06-04 23:10:59 +08:00
|
|
|
|
*/
|
2025-09-09 09:45:51 +08:00
|
|
|
|
@BT.CompositeNode("MemSequence", {
|
2025-09-08 16:10:08 +08:00
|
|
|
|
name: "记忆顺序节点",
|
|
|
|
|
|
group: "基础组合节点",
|
2025-09-09 09:45:51 +08:00
|
|
|
|
desc: "记住上次运行位置的序列节点,从记忆位置开始执行",
|
2025-09-08 16:10:08 +08:00
|
|
|
|
})
|
2025-09-02 17:05:46 +08:00
|
|
|
|
export class MemSequence extends MemoryComposite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
|
|
|
|
|
let index = this.get<number>(`__nMemoryRunningIndex`);
|
|
|
|
|
|
for (let i = index; i < this.children.length; i++) {
|
|
|
|
|
|
let status = this.children[i]!._execute();
|
2025-09-04 14:08:19 +08:00
|
|
|
|
if (status === Status.SUCCESS) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status === Status.FAILURE) {
|
|
|
|
|
|
return Status.FAILURE;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
2025-09-04 14:08:19 +08:00
|
|
|
|
this.set(`__nMemoryRunningIndex`, i);
|
|
|
|
|
|
return Status.RUNNING;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Status.SUCCESS;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-04 14:08:19 +08:00
|
|
|
|
* 选择节点 从上到下执行
|
|
|
|
|
|
* 返回第一个不为 FAILURE 的子节点状态
|
|
|
|
|
|
* 否则返回 FAILURE
|
2025-06-04 23:10:59 +08:00
|
|
|
|
*/
|
2025-09-09 09:45:51 +08:00
|
|
|
|
@BT.CompositeNode("Selector", {
|
2025-09-08 16:10:08 +08:00
|
|
|
|
name: "选择节点",
|
|
|
|
|
|
group: "基础组合节点",
|
2025-09-09 09:45:51 +08:00
|
|
|
|
desc: "依次执行子节点,直到找到成功或运行中的节点",
|
2025-09-08 16:10:08 +08:00
|
|
|
|
})
|
2025-06-04 23:10:59 +08:00
|
|
|
|
export class Selector extends Composite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
for (let i = 0; i < this.children.length; i++) {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
let status = this.children[i]!._execute();
|
2025-06-04 23:10:59 +08:00
|
|
|
|
if (status !== Status.FAILURE) {
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return Status.FAILURE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-08 16:10:08 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 随机选择节点
|
|
|
|
|
|
* 随机选择一个子节点执行
|
|
|
|
|
|
* 返回子节点状态
|
|
|
|
|
|
*/
|
2025-09-09 09:45:51 +08:00
|
|
|
|
@BT.CompositeNode("RandomSelector", {
|
2025-09-08 16:10:08 +08:00
|
|
|
|
name: "随机选择节点",
|
|
|
|
|
|
group: "基础组合节点",
|
2025-09-09 09:45:51 +08:00
|
|
|
|
desc: "随机选择一个子节点执行",
|
2025-09-08 16:10:08 +08:00
|
|
|
|
})
|
|
|
|
|
|
export class RandomSelector extends Composite {
|
|
|
|
|
|
private _totalWeight: number = 0;
|
|
|
|
|
|
private _weights: number[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
constructor(...children: IBTNode[]) {
|
|
|
|
|
|
super(...children);
|
|
|
|
|
|
this._totalWeight = 0;
|
|
|
|
|
|
this._weights = [];
|
|
|
|
|
|
|
|
|
|
|
|
for (const child of children) {
|
|
|
|
|
|
const weight = this.getChildWeight(child);
|
|
|
|
|
|
this._totalWeight += weight;
|
|
|
|
|
|
this._weights.push(this._totalWeight);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private getChildWeight(child: IBTNode): number {
|
|
|
|
|
|
return (child instanceof WeightDecorator) ? (child.weight) : 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public tick(): Status {
|
|
|
|
|
|
if (this.children.length === 0) {
|
|
|
|
|
|
return Status.FAILURE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 基于权重的随机选择
|
|
|
|
|
|
const randomValue = Math.random() * this._totalWeight;
|
|
|
|
|
|
|
|
|
|
|
|
// 使用二分查找找到对应的子节点索引(O(log n)复杂度)
|
|
|
|
|
|
let left = 0;
|
|
|
|
|
|
let right = this._weights.length - 1;
|
|
|
|
|
|
let childIndex = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while (left <= right) {
|
|
|
|
|
|
const mid = Math.floor((left + right) / 2);
|
|
|
|
|
|
if (this._weights[mid]! > randomValue) {
|
|
|
|
|
|
childIndex = mid;
|
|
|
|
|
|
right = mid - 1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
left = mid + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
const status = this.children[childIndex]!._execute();
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-04 23:10:59 +08:00
|
|
|
|
/**
|
2025-09-04 14:08:19 +08:00
|
|
|
|
* 顺序节点 从上到下执行
|
|
|
|
|
|
* 遇到 SUCCESS 继续下一个
|
|
|
|
|
|
* 否则返回子节点状态
|
2025-06-04 23:10:59 +08:00
|
|
|
|
*/
|
2025-09-09 09:45:51 +08:00
|
|
|
|
@BT.CompositeNode("Sequence", {
|
2025-09-08 16:10:08 +08:00
|
|
|
|
name: "顺序节点",
|
|
|
|
|
|
group: "基础组合节点",
|
2025-09-09 09:45:51 +08:00
|
|
|
|
desc: "依次执行所有子节点,全部成功才返回成功",
|
2025-09-08 16:10:08 +08:00
|
|
|
|
})
|
2025-06-04 23:10:59 +08:00
|
|
|
|
export class Sequence extends Composite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
for (let i = 0; i < this.children.length; i++) {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
let status = this.children[i]!._execute();
|
2025-09-04 14:08:19 +08:00
|
|
|
|
if (status === Status.SUCCESS) {
|
|
|
|
|
|
continue;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
2025-09-04 14:08:19 +08:00
|
|
|
|
return status;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Status.SUCCESS;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-04 14:08:19 +08:00
|
|
|
|
* 并行节点 从上到下执行 全部执行一遍
|
|
|
|
|
|
* 返回优先级 FAILURE > RUNNING > SUCCESS
|
2025-06-04 23:10:59 +08:00
|
|
|
|
*/
|
2025-09-09 09:45:51 +08:00
|
|
|
|
@BT.CompositeNode("Parallel", {
|
2025-09-08 16:10:08 +08:00
|
|
|
|
name: "并行节点",
|
|
|
|
|
|
group: "基础组合节点",
|
2025-09-09 09:45:51 +08:00
|
|
|
|
desc: "同时执行所有子节点,全部成功才返回成功",
|
2025-09-08 16:10:08 +08:00
|
|
|
|
})
|
2025-06-04 23:10:59 +08:00
|
|
|
|
export class Parallel extends Composite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
let result = Status.SUCCESS;
|
|
|
|
|
|
for (let i = 0; i < this.children.length; i++) {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
let status = this.children[i]!._execute();
|
|
|
|
|
|
if (result === Status.FAILURE || status === Status.FAILURE) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
result = Status.FAILURE;
|
2025-09-04 14:08:19 +08:00
|
|
|
|
} else if (status === Status.RUNNING) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
result = Status.RUNNING;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-04 14:08:19 +08:00
|
|
|
|
* 并行节点 从上到下执行 全部执行一遍
|
|
|
|
|
|
* 返回优先级 SUCCESS > RUNNING > FAILURE
|
2025-06-04 23:10:59 +08:00
|
|
|
|
*/
|
2025-09-09 09:45:51 +08:00
|
|
|
|
@BT.CompositeNode("ParallelAnySuccess", {
|
2025-09-08 16:10:08 +08:00
|
|
|
|
name: "并行任意成功",
|
|
|
|
|
|
group: "基础组合节点",
|
2025-09-09 09:45:51 +08:00
|
|
|
|
desc: "同时执行所有子节点,任意一个成功即返回成功",
|
2025-09-08 16:10:08 +08:00
|
|
|
|
})
|
2025-06-04 23:10:59 +08:00
|
|
|
|
export class ParallelAnySuccess extends Composite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
2025-09-04 14:08:19 +08:00
|
|
|
|
let result = Status.FAILURE;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
for (let i = 0; i < this.children.length; i++) {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
let status = this.children[i]!._execute();
|
|
|
|
|
|
if (result === Status.SUCCESS || status === Status.SUCCESS) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
result = Status.SUCCESS;
|
2025-09-04 14:08:19 +08:00
|
|
|
|
} else if (status === Status.RUNNING) {
|
|
|
|
|
|
result = Status.RUNNING;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|