Skip to content

Commit

Permalink
working on BehaviourTreeBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Howard committed Feb 6, 2024
1 parent 6e38ea4 commit d6f2c53
Show file tree
Hide file tree
Showing 42 changed files with 737 additions and 4,839 deletions.
3 changes: 3 additions & 0 deletions dist/Agent.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CompleteState } from "./State";
/**
* A type representing an agent that a behavior tree instance would operate on.
*/
export type Agent = {
[actionName: string]: AgentFunction;
};
Expand Down
19 changes: 7 additions & 12 deletions dist/BehaviourTree.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { AnyArgument } from "./RootAstNodesBuilder";
import { AnyState } from "./State";
import Root from "./nodes/decorator/Root";
import { Agent, GlobalFunction } from "./Agent";
import { CallbackAttributeDetails } from "./attributes/callbacks/Callback";
import { GuardAttributeDetails } from "./attributes/guards/Guard";
import { BehaviourTreeOptions } from "./BehaviourTreeOptions";
import { RootNodeDefinition } from "./BehaviourTreeDefinition";
export type FlattenedTreeNode = {
id: string;
type: string;
caption: string;
state: AnyState;
guards: GuardAttributeDetails[];
callbacks: CallbackAttributeDetails[];
args: AnyArgument[];
args: any[];
parentId: string | null;
};
/**
Expand All @@ -31,7 +31,7 @@ export declare class BehaviourTree {
* @param agent The agent instance that this behaviour tree is modelling behaviour for.
* @param options The behaviour tree options object.
*/
constructor(definition: string, agent: Agent, options?: BehaviourTreeOptions);
constructor(definition: string | RootNodeDefinition | RootNodeDefinition[], agent: Agent, options?: BehaviourTreeOptions);
/**
* Gets whether the tree is in the RUNNING state.
* @returns true if the tree is in the RUNNING state, otherwise false.
Expand Down Expand Up @@ -65,7 +65,7 @@ export declare class BehaviourTree {
* @param name The name of the function or subtree to register.
* @param value The function or subtree definition to register.
*/
static register(name: string, value: GlobalFunction | string): void;
static register(name: string, value: GlobalFunction | string | RootNodeDefinition): void;
/**
* Unregisters the registered action/condition/guard/callback function or subtree with the given name.
* @param name The name of the registered action/condition/guard/callback function or subtree to unregister.
Expand All @@ -76,14 +76,9 @@ export declare class BehaviourTree {
*/
static unregisterAll(): void;
/**
* Parses a behaviour tree definition and creates a tree of behaviour tree nodes.
* @param {string} definition The behaviour tree definition.
* Parses a behaviour tree definition and creates a tree of behaviour tree nodes populated at a root.
* @param {string | RootNodeDefinition | RootNodeDefinition[]} definition The behaviour tree definition.
* @returns The root behaviour tree node.
*/
private static _createRootNode;
/**
* Applies a guard path to every leaf of the tree to evaluate as part of each update.
* @param rootNode The main root tree node.
*/
private static _applyLeafNodeGuardPaths;
private _createRootNode;
}
8 changes: 8 additions & 0 deletions dist/BehaviourTreeBuilder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RootNodeDefinition } from "./BehaviourTreeDefinition";
import Root from "./nodes/decorator/Root";
/**
* Build and populate the root nodes based on the provided definition, assuming that the definition has been validated.
* @param definition The root node definitions.
* @returns The built and populated root node definitions.
*/
export default function buildRootNode(definition: RootNodeDefinition[]): Root;
10 changes: 5 additions & 5 deletions dist/BehaviourTreeDefinition.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface CompositeNodeDefinition extends NodeDefinition {
/**
* The child nodes of this composite node.
*/
children: AnyChildNode[];
children: AnyChildNodeDefinition[];
}
/**
* A decorator node, a composite with only a single child node.
Expand All @@ -56,7 +56,7 @@ export interface DecoratorNodeDefinition extends NodeDefinition {
/**
* The child node of this decorator node.
*/
child: AnyChildNode;
child: AnyChildNodeDefinition;
}
/**
* A branch node.
Expand Down Expand Up @@ -216,10 +216,10 @@ export interface FailNodeDefinition extends DecoratorNodeDefinition {
type: "fail";
}
/**
* A type defining any node type.
* A type defining any node definition.
*/
export type AnyNode = BranchNodeDefinition | ActionNodeDefinition | ConditionNodeDefinition | WaitNodeDefinition | SequenceNodeDefinition | SelectorNodeDefinition | LottoNodeDefinition | ParallelNodeDefinition | RootNodeDefinition | RepeatNodeDefinition | RetryNodeDefinition | FlipNodeDefinition | SucceedNodeDefinition | FailNodeDefinition;
export type AnyNodeDefinition = BranchNodeDefinition | ActionNodeDefinition | ConditionNodeDefinition | WaitNodeDefinition | SequenceNodeDefinition | SelectorNodeDefinition | LottoNodeDefinition | ParallelNodeDefinition | RootNodeDefinition | RepeatNodeDefinition | RetryNodeDefinition | FlipNodeDefinition | SucceedNodeDefinition | FailNodeDefinition;
/**
* A type defining any node type that can be a child of composite parent node.
*/
export type AnyChildNode = Exclude<AnyNode, RootNodeDefinition>;
export type AnyChildNodeDefinition = Exclude<AnyNodeDefinition, RootNodeDefinition>;
4 changes: 2 additions & 2 deletions dist/BehaviourTreeDefinitionUtilities.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeDefinition, RootNodeDefinition, DecoratorNodeDefinition, CompositeNodeDefinition, AnyNode, BranchNodeDefinition } from "./BehaviourTreeDefinition";
import { NodeDefinition, RootNodeDefinition, DecoratorNodeDefinition, CompositeNodeDefinition, AnyNodeDefinition, BranchNodeDefinition } from "./BehaviourTreeDefinition";
/**
* A type guard function that returns true if the specified node satisfies the RootNodeDefinition type.
* @param node The node.
Expand Down Expand Up @@ -34,7 +34,7 @@ export declare function isCompositeNode(node: NodeDefinition): node is Composite
* @param nodeDefinition The node definition to flatten.
* @returns An array of all of nested node definitions.
*/
export declare function flattenDefinition(nodeDefinition: AnyNode): AnyNode[];
export declare function flattenDefinition(nodeDefinition: AnyNodeDefinition): AnyNodeDefinition[];
/**
* Determines whether the passed value is an integer.
* @param value The value to check.
Expand Down
10 changes: 9 additions & 1 deletion dist/BehaviourTreeDefinitionValidator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type DefinitionValidationResult = {
errorMessage?: string;
};
/**
* Validates the specified behaviour tree definition in the form of JSON or MDSL.
* Validates the specified behaviour tree definition in the form of JSON or MDSL, not taking any globally registered subtrees into consideration.
* @param definition The behaviour tree definition in the form of JSON or MDSL.
* @returns An object representing the result of validating the given tree definition.
*/
Expand All @@ -30,3 +30,11 @@ export declare function validateMDSLDefinition(definition: string): DefinitionVa
* @returns An object representing the result of validating the given tree definition.
*/
export declare function validateJSONDefinition(definition: RootNodeDefinition | RootNodeDefinition[]): DefinitionValidationResult;
/**
* Validates the branch -> subtree links across all provided root node definitions.
* This will not consider branch nodes that reference any globally registered subtrees unless includesGlobalSubtrees
* is set to true, in which case we will also verify that there are no broken branch -> subtree links.
* @param rootNodeDefinitions The array of root node definitions.
* @param includesGlobalSubtrees A flag defining whether the array includes all global subtree root node definitions.
*/
export declare function validateBranchSubtreeLinks(rootNodeDefinitions: RootNodeDefinition[], includesGlobalSubtrees: boolean): void;
23 changes: 9 additions & 14 deletions dist/Lookup.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ActionResult, Agent, ExitFunctionArg, GlobalFunction } from "./Agent";
import { AnyArgument, RootAstNode } from "./RootAstNodesBuilder";
type ExitResultArg = {
value: ExitFunctionArg;
};
export type AnyExitArgument = AnyArgument | ExitResultArg;
export type InvokerFunction = (args: AnyExitArgument[]) => ActionResult;
import { ActionResult, Agent, GlobalFunction } from "./Agent";
import { RootNodeDefinition } from "./BehaviourTreeDefinition";
export type InvokerFunction = (args: any[]) => ActionResult;
/**
* A singleton used to store and lookup registered functions and subtrees.
*/
Expand All @@ -14,7 +10,7 @@ export default class Lookup {
*/
private static functionTable;
/**
* The object holding any registered sub-trees keyed on tree name.
* The object holding any registered subtree root node definitions keyed on tree name.
*/
private static subtreeTable;
/**
Expand All @@ -39,17 +35,17 @@ export default class Lookup {
*/
static getFuncInvoker(agent: Agent, name: string): InvokerFunction | null;
/**
* Gets the subtree with the specified name.
* @param name The name of the subtree.
* @returns The subtree with the specified name.
* Gets all registered subtree root node definitions.
*/
static getSubtree(name: string): RootAstNode;
static getSubtrees(): {
[key: string]: RootNodeDefinition;
};
/**
* Sets the subtree with the specified name for later lookup.
* @param name The name of the subtree.
* @param subtree The subtree.
*/
static setSubtree(name: string, subtree: RootAstNode): void;
static setSubtree(name: string, subtree: RootNodeDefinition): void;
/**
* Removes the registered function or subtree with the specified name.
* @param name The name of the registered function or subtree.
Expand All @@ -60,4 +56,3 @@ export default class Lookup {
*/
static empty(): void;
}
export {};
19 changes: 5 additions & 14 deletions dist/attributes/Attribute.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
import { AnyArgument } from "../RootAstNodesBuilder";
import Guard from "./guards/Guard";
export type AttributeDetails = {
/** The attribute type. */
type: string;
/** The attribute arguments. */
args: AnyArgument[];
args: any[];
};
/**
* A base node attribute.
*/
export default abstract class Attribute<TAttributeDetails extends AttributeDetails = AttributeDetails> {
protected type: string;
protected args: AnyArgument[];
type: string;
args: any[];
/**
* @param type The node attribute type.
* @param args The array of attribute argument definitions.
*/
constructor(type: string, args: AnyArgument[]);
/**
* Gets the type of the attribute.
*/
getType: () => string;
/**
* Gets the array of attribute argument definitions.
* @param args The array of attribute arguments.
*/
getArguments: () => AnyArgument[];
constructor(type: string, args: any[]);
/**
* Gets the attribute details.
*/
Expand Down
3 changes: 1 addition & 2 deletions dist/attributes/callbacks/Callback.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Agent } from "../../Agent";
import { AnyArgument } from "../../RootAstNodesBuilder";
import Attribute, { AttributeDetails } from "../Attribute";
export type CallbackAttributeDetails = {
/** The name of the agent function that is called. */
Expand All @@ -15,7 +14,7 @@ export default abstract class Callback extends Attribute<CallbackAttributeDetail
* @param args The array of decorator argument definitions.
* @param functionName The name of the agent function to call.
*/
constructor(type: string, args: AnyArgument[], functionName: string);
constructor(type: string, args: any[], functionName: string);
/**
* Gets the name of the agent function to call.
*/
Expand Down
3 changes: 1 addition & 2 deletions dist/attributes/callbacks/Entry.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Callback from "./Callback";
import { Agent } from "../../Agent";
import { AnyArgument } from "../../RootAstNodesBuilder";
/**
* An ENTRY callback which defines an agent function to call when the associated node is updated and moves out of running state.
*/
Expand All @@ -9,7 +8,7 @@ export default class Entry extends Callback {
* @param functionName The name of the agent function to call.
* @param args The array of callback argument definitions.
*/
constructor(functionName: string, args: AnyArgument[]);
constructor(functionName: string, args: any[]);
/**
* Attempt to call the agent function that this callback refers to.
* @param agent The agent.
Expand Down
3 changes: 1 addition & 2 deletions dist/attributes/callbacks/Exit.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Callback from "./Callback";
import { Agent } from "../../Agent";
import { AnyArgument } from "../../RootAstNodesBuilder";
/**
* An EXIT callback which defines an agent function to call when the associated node is updated and moves to a finished state or is aborted.
*/
Expand All @@ -9,7 +8,7 @@ export default class Exit extends Callback {
* @param functionName The name of the agent function to call.
* @param args The array of callback argument definitions.
*/
constructor(functionName: string, args: AnyArgument[]);
constructor(functionName: string, args: any[]);
/**
* Attempt to call the agent function that this callback refers to.
* @param agent The agent.
Expand Down
3 changes: 1 addition & 2 deletions dist/attributes/callbacks/Step.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Callback from "./Callback";
import { Agent } from "../../Agent";
import { AnyArgument } from "../../RootAstNodesBuilder";
/**
* A STEP callback which defines an agent function to call when the associated node is updated.
*/
Expand All @@ -9,7 +8,7 @@ export default class Step extends Callback {
* @param functionName The name of the agent function to call.
* @param args The array of callback argument definitions.
*/
constructor(functionName: string, args: AnyArgument[]);
constructor(functionName: string, args: any[]);
/**
* Attempt to call the agent function that this callback refers to.
* @param agent The agent.
Expand Down
3 changes: 1 addition & 2 deletions dist/attributes/guards/Guard.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Agent } from "../../Agent";
import { AnyArgument } from "../../RootAstNodesBuilder";
import Attribute, { AttributeDetails } from "../Attribute";
export type GuardAttributeDetails = {
/** The name of the condition function that determines whether the guard is satisfied. */
Expand All @@ -15,7 +14,7 @@ export default abstract class Guard extends Attribute<GuardAttributeDetails> {
* @param args The array of decorator argument definitions.
* @param condition The name of the condition function that determines whether the guard is satisfied.
*/
constructor(type: string, args: AnyArgument[], condition: string);
constructor(type: string, args: any[], condition: string);
/**
* Gets the name of the condition function that determines whether the guard is satisfied.
*/
Expand Down
3 changes: 1 addition & 2 deletions dist/attributes/guards/Until.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Guard from "./Guard";
import { Agent } from "../../Agent";
import { AnyArgument } from "../../RootAstNodesBuilder";
/**
* An UNTIL guard which is satisfied as long as the given condition remains false.
*/
Expand All @@ -9,7 +8,7 @@ export default class Until extends Guard {
* @param condition The name of the condition function that determines whether the guard is satisfied.
* @param args The array of decorator argument definitions.
*/
constructor(condition: string, args: AnyArgument[]);
constructor(condition: string, args: any[]);
/**
* Gets whether the guard is satisfied.
* @param agent The agent.
Expand Down
3 changes: 1 addition & 2 deletions dist/attributes/guards/While.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Guard from "./Guard";
import { Agent } from "../../Agent";
import { AnyArgument } from "../../RootAstNodesBuilder";
/**
* A WHILE guard which is satisfied as long as the given condition remains true.
*/
Expand All @@ -9,7 +8,7 @@ export default class While extends Guard {
* @param condition The name of the condition function that determines whether the guard is satisfied.
* @param args The array of decorator argument definitions.
*/
constructor(condition: string, args: AnyArgument[]);
constructor(condition: string, args: any[]);
/**
* Gets whether the guard is satisfied.
* @param agent The agent.
Expand Down
Loading

0 comments on commit d6f2c53

Please sign in to comment.