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-06-04 23:10:59 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 记忆选择节点
|
2025-09-02 17:05:46 +08:00
|
|
|
|
* 选择不为 FAILURE 的节点,记住上次运行的子节点位置
|
2025-06-04 23:10:59 +08:00
|
|
|
|
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
|
|
|
|
|
|
*/
|
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-06-04 23:10:59 +08:00
|
|
|
|
if (status !== Status.FAILURE) {
|
|
|
|
|
|
if (status === Status.RUNNING) {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
this.set(`__nMemoryRunningIndex`, i);
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return Status.FAILURE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 记忆顺序节点
|
2025-09-02 17:05:46 +08:00
|
|
|
|
* 如果上次执行到 RUNNING 的节点, 下次进入节点后, 直接从 RUNNING 节点开始
|
|
|
|
|
|
* 遇到 SUCCESS 或者 FAILURE 停止迭代
|
2025-06-04 23:10:59 +08:00
|
|
|
|
* 任意一个Child Node返回不为 SUCCESS, 本Node向自己的Parent Node也返回Child Node状态
|
|
|
|
|
|
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
|
|
|
|
|
*/
|
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-06-04 23:10:59 +08:00
|
|
|
|
if (status !== Status.SUCCESS) {
|
|
|
|
|
|
if (status === Status.RUNNING) {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
this.set(`__nMemoryRunningIndex`, i);
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return Status.SUCCESS;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 随机选择节点
|
|
|
|
|
|
* 从Child Node中随机选择一个执行
|
|
|
|
|
|
*/
|
|
|
|
|
|
export class RandomSelector extends Composite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
2025-09-02 17:05:46 +08:00
|
|
|
|
if (this.children.length === 0) {
|
|
|
|
|
|
return Status.FAILURE;
|
|
|
|
|
|
}
|
2025-06-04 23:10:59 +08:00
|
|
|
|
|
2025-09-02 17:05:46 +08:00
|
|
|
|
const childIndex = Math.floor(Math.random() * this.children.length);
|
2025-09-03 10:54:07 +08:00
|
|
|
|
const status = this.children[childIndex]!._execute();
|
2025-06-04 23:10:59 +08:00
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 选择节点,选择不为 FAILURE 的节点
|
2025-09-02 17:05:46 +08:00
|
|
|
|
* 当执行本Node时,它将从begin到end迭代执行自己的Child Node:
|
|
|
|
|
|
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 顺序节点
|
|
|
|
|
|
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
2025-09-02 17:05:46 +08:00
|
|
|
|
* 遇到 FAILURE 或 RUNNING, 那停止迭代,返回FAILURE 或 RUNNING
|
2025-06-04 23:10:59 +08:00
|
|
|
|
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
|
|
|
|
|
*/
|
|
|
|
|
|
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-06-04 23:10:59 +08:00
|
|
|
|
if (status !== Status.SUCCESS) {
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return Status.SUCCESS;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-03 10:54:07 +08:00
|
|
|
|
* 并行节点 每次进入全部执行一遍
|
|
|
|
|
|
* 它将从begin到end迭代执行自己的Child Node:
|
|
|
|
|
|
* 1. 任意子节点返回 FAILURE, 返回 FAILURE
|
|
|
|
|
|
* 2. 否则 任意子节点返回 RUNNING, 返回 RUNNING
|
|
|
|
|
|
* 3. 全部成功, 才返回 SUCCESS
|
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-03 10:54:07 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status === Status.RUNNING) {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
result = Status.RUNNING;
|
2025-09-03 10:54:07 +08:00
|
|
|
|
continue;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 并行节点 每次进入全部重新执行一遍
|
2025-09-03 10:54:07 +08:00
|
|
|
|
* 它将从begin到end迭代执行自己的Child Node:
|
|
|
|
|
|
* 1. 任意子节点返回 SUCCESS, 返回 SUCCESS
|
|
|
|
|
|
* 2. 否则, 任意子节点返回 FAILURE, 返回 FAILURE
|
2025-06-04 23:10:59 +08:00
|
|
|
|
* 否则返回 RUNNING
|
|
|
|
|
|
*/
|
|
|
|
|
|
export class ParallelAnySuccess extends Composite {
|
2025-09-03 10:54:07 +08:00
|
|
|
|
public tick(): Status {
|
2025-06-04 23:10:59 +08:00
|
|
|
|
let result = Status.RUNNING;
|
|
|
|
|
|
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-03 10:54:07 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status === Status.FAILURE) {
|
|
|
|
|
|
result = Status.FAILURE;
|
|
|
|
|
|
continue;
|
2025-06-04 23:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|