Skip to content

Commit

Permalink
chore: prettier auto-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabeler-lee-6rs committed Jan 18, 2023
1 parent eff54fe commit 15124b7
Show file tree
Hide file tree
Showing 75 changed files with 1,754 additions and 1,554 deletions.
20 changes: 5 additions & 15 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
{
"extension": [
".ts",
".tsx"
],
"include": [
"lib/**/*.js",
"lib/**/*.ts"
],
"exclude": [
"**/*.d.ts"
],
"reporter": [
"html"
],
"extension": [".ts", ".tsx"],
"include": ["lib/**/*.js", "lib/**/*.ts"],
"exclude": ["**/*.d.ts"],
"reporter": ["html"],
"temp-directory": "./coverage/.nyc_output",
"report-dir": "./coverage/.nyc_output",
"report-dir": "./coverage/.nyc_output",
"all": true
}
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"outFiles": ["${workspaceFolder}/dist/**/*"],
"preLaunchTask": "npm run build",
"internalConsoleOptions": "openOnSessionStart"
},
}
],
"inputs": [
{
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"base": "$tsc",
"fileLocation": ["relative"]
}
},
}
]
}
70 changes: 35 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,104 +12,104 @@ It is synchronous for performance - asynchronous operations can be handled by re

#### Action

* Handles the event normally and must return a result
- Handles the event normally and must return a result

#### Predicate

* Executes a provided synchronous function (`(state, event) => boolean`) and returns `SUCCESS` or `FAILURE` based upon the boolean result of such function.
- Executes a provided synchronous function (`(state, event) => boolean`) and returns `SUCCESS` or `FAILURE` based upon the boolean result of such function.

#### SideEffect

* Executes a provided synchronous function (`(state, event) => void`) and always returns `SUCCESS`.
- Executes a provided synchronous function (`(state, event) => void`) and always returns `SUCCESS`.

#### Success

* Immediately returns `SUCCESS` when activated.
- Immediately returns `SUCCESS` when activated.

### Composites (aka Control Flow Nodes)

#### Selector

(aka Priority or Fallback)

* Sends an event to each child until one of them returns `SUCCESS` or `RUNNING`, then returns that value.
* If we exhaust all the children, return `FAILURE`.
- Sends an event to each child until one of them returns `SUCCESS` or `RUNNING`, then returns that value.
- If we exhaust all the children, return `FAILURE`.

#### LatchedSelector

* Sends an event to each child until one of them returns `SUCCESS` or `RUNNING`, then returns that value.
* If we exhaust all the children, return `FAILURE`.
* If a child returns `RUNNING`, subsequent events start at that child.
- Sends an event to each child until one of them returns `SUCCESS` or `RUNNING`, then returns that value.
- If we exhaust all the children, return `FAILURE`.
- If a child returns `RUNNING`, subsequent events start at that child.

#### Sequence

* Sends an event to each child until one of the returns `FAILURE`, or `RUNNING`, then returns that value.
* If all children return `SUCCESS`, return `SUCCESS`.
- Sends an event to each child until one of the returns `FAILURE`, or `RUNNING`, then returns that value.
- If all children return `SUCCESS`, return `SUCCESS`.

#### LatchedSequence

* Sends an event to each child until one of the returns `FAILURE`, or `RUNNING`, then returns that value.
* If all children return `SUCCESS`, return `SUCCESS`.
* If a child returns `RUNNING`, subsequent events start at that child.
- Sends an event to each child until one of the returns `FAILURE`, or `RUNNING`, then returns that value.
- If all children return `SUCCESS`, return `SUCCESS`.
- If a child returns `RUNNING`, subsequent events start at that child.

#### IfElse

* Accepts a `conditional` function a `consequent` node, and an optional `alternative` node.
* If `conditional(state, event)` returns true, will return the result of activating the `consequent` node.
* If `conditional(state, event)` returns false, will return the result of activating the `alternative` node, if one is provided.
* If `conditional(state, event)` returns false, will return `FAILURE` if no `alternative` node is provided.
- Accepts a `conditional` function a `consequent` node, and an optional `alternative` node.
- If `conditional(state, event)` returns true, will return the result of activating the `consequent` node.
- If `conditional(state, event)` returns false, will return the result of activating the `alternative` node, if one is provided.
- If `conditional(state, event)` returns false, will return `FAILURE` if no `alternative` node is provided.

### Decorators

Decorators intercept and can modify the event sent to or the result from the child.

#### Not

* Returns 'FAILURE' when the child returns 'SUCCESS', and vice-versa
- Returns 'FAILURE' when the child returns 'SUCCESS', and vice-versa

#### RepeatWhen

* Repeats the child when an evaluation function returns true.
- Repeats the child when an evaluation function returns true.

#### RepeatOnResult

* Repeats the child if it returns the specified status.
- Repeats the child if it returns the specified status.

#### ResultSwap

* Allows you to swap one result of a child node for another.
* For example, you can use this to mask `FAILURE` as `SUCCESS`.
- Allows you to swap one result of a child node for another.
- For example, you can use this to mask `FAILURE` as `SUCCESS`.

### Base Classes

#### Base

* The base of all nodes.
- The base of all nodes.

#### Composite

* The base class for all nodes which have children.
- The base class for all nodes which have children.

#### Decorator

* The base class for decorators.
* Can only have one child.
- The base class for decorators.
- Can only have one child.

## Publishing

* The publisher can be registered with the tree which will log each transition.
- The publisher can be registered with the tree which will log each transition.

## Inspiration and Further Reading

The following are sources used when designing this library

* [Handling Node Traversal](http:https://stackoverflow.com/a/15725129/1017787)
* [Difference between a Decision Tree and a Behavior Tree](http:https://gamedev.stackexchange.com/questions/51693/decision-tree-vs-behavior-tree)
* [Behavior Trees for AI](http:https://www.gamasutra.com/blogs/ChrisSimpson/20140717/221339/Behavior_trees_for_AI_How_they_work.php)
* [Wikipedia Article](https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control))
* [What is a Behavior Tree?](https://opsive.com/support/documentation/behavior-designer/what-is-a-behavior-tree/)
* [Halo 2's AI](http:https://www.gamasutra.com/view/feature/130663/gdc_2005_proceeding_handling_.php)
* Halo 2 was the first game to popularize Behavior Trees.
- [Handling Node Traversal](http:https://stackoverflow.com/a/15725129/1017787)
- [Difference between a Decision Tree and a Behavior Tree](http:https://gamedev.stackexchange.com/questions/51693/decision-tree-vs-behavior-tree)
- [Behavior Trees for AI](http:https://www.gamasutra.com/blogs/ChrisSimpson/20140717/221339/Behavior_trees_for_AI_How_they_work.php)
- [Wikipedia Article](<https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control)>)
- [What is a Behavior Tree?](https://opsive.com/support/documentation/behavior-designer/what-is-a-behavior-tree/)
- [Halo 2's AI](http:https://www.gamasutra.com/view/feature/130663/gdc_2005_proceeding_handling_.php)
- Halo 2 was the first game to popularize Behavior Trees.

## Name

Expand Down
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = {extends: ['@6river/commitlint-config-6river']};
module.exports = { extends: ['@6river/commitlint-config-6river'] };
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NodeManager} from './utils';
import { NodeManager } from './utils';

export * from './models';
export * from './nodes';
Expand Down
8 changes: 4 additions & 4 deletions lib/models/BaseNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BlueshellState, NodeStorage, ResultCode} from '../models';
import { BlueshellState, NodeStorage, ResultCode } from '../models';

/**
* Base interface for all Nodes
Expand All @@ -11,9 +11,9 @@ export interface BaseNode<S extends BlueshellState, E> {
symbol: string;

handleEvent(state: S, event: E): ResultCode;
getLastEventSeen(state: S): number|undefined;
getTreeEventCounter(state: S): number|undefined;
getLastResult(state: S): string|undefined;
getLastEventSeen(state: S): number | undefined;
getTreeEventCounter(state: S): number | undefined;
getLastResult(state: S): string | undefined;
getNodeStorage(state: S): NodeStorage;
resetNodeStorage(state: S): void;
}
2 changes: 1 addition & 1 deletion lib/models/ConditionalWithResult.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ResultCode} from './ResultCode';
import { ResultCode } from './ResultCode';

/**
* Used in several components for flow control purposes. Must be a pure function with no side-effects.
Expand Down
6 changes: 3 additions & 3 deletions lib/models/NodeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Note: a node is free to store additional properties by casting this as an any.
*/
export interface NodeStorage {
lastEventSeen: number|undefined;
lastResult: string|undefined;
running: number|undefined;
lastEventSeen: number | undefined;
lastResult: string | undefined;
running: number | undefined;
}
8 changes: 3 additions & 5 deletions lib/models/ParentNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BaseNode} from './BaseNode';
import {BlueshellState} from './BlueshellState';
import { BaseNode } from './BaseNode';
import { BlueshellState } from './BlueshellState';

/**
* NOTE: this file is separate from Parent.ts so that Base can reference items from here
Expand All @@ -20,8 +20,6 @@ export interface ParentNode<S extends BlueshellState, E> extends BaseNode<S, E>
* Checks if the passed in object exposes a list of children.
* @param node Node to check
*/
export function isParentNode<S extends BlueshellState, E>(
node: any
): node is ParentNode<S, E> {
export function isParentNode<S extends BlueshellState, E>(node: any): node is ParentNode<S, E> {
return !!node.getChildren;
}
2 changes: 1 addition & 1 deletion lib/models/ResultCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* 5/30/16
* @author Joshua Chaitin-Pollak
*/
export const resultCodes: {[K in ResultCode]: ResultCode} = {
export const resultCodes: { [K in ResultCode]: ResultCode } = {
SUCCESS: 'SUCCESS',
FAILURE: 'FAILURE',
RUNNING: 'RUNNING',
Expand Down
12 changes: 6 additions & 6 deletions lib/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export {BlueshellState} from './BlueshellState';
export {NodeStorage} from './NodeStorage';
export { BlueshellState } from './BlueshellState';
export { NodeStorage } from './NodeStorage';

export * from './ResultCode';
import {resultCodes} from './ResultCode';
import { resultCodes } from './ResultCode';

export {resultCodes as rc};
export { resultCodes as rc };

export {BaseNode} from './BaseNode';
export {ParentNode, isParentNode} from './ParentNode';
export { BaseNode } from './BaseNode';
export { ParentNode, isParentNode } from './ParentNode';

export * from './Conditional';
export * from './ConditionalWithResult';
25 changes: 10 additions & 15 deletions lib/nodes/Base.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
/**
* Created by josh on 1/10/16.
*/
import {v4} from 'uuid';

import {
BlueshellState,
NodeStorage,
rc,
ResultCode,
BaseNode,
isParentNode,
} from '../models';
import {TreePublisher, TreeNonPublisher, NodeManager} from '../utils';
import { v4 } from 'uuid';

import { BlueshellState, NodeStorage, rc, ResultCode, BaseNode, isParentNode } from '../models';
import { TreePublisher, TreeNonPublisher, NodeManager } from '../utils';

/**
* Interface that defines what is stored in private node storage at the root
*/
export interface PrivateNodeStorage {
debug: boolean;
eventCounter: number|undefined;
eventCounter: number | undefined;
}

/**
Expand All @@ -36,7 +29,9 @@ export class Base<S extends BlueshellState, E> implements BaseNode<S, E> {
// marginally better than a global
static treePublisher: TreePublisher<any, any> = new TreeNonPublisher();

public static registerTreePublisher<S extends BlueshellState, E>(publisher: TreePublisher<S, E>): void {
public static registerTreePublisher<S extends BlueshellState, E>(
publisher: TreePublisher<S, E>,
): void {
Base.treePublisher = publisher;
}

Expand Down Expand Up @@ -247,5 +242,5 @@ export class Base<S extends BlueshellState, E> implements BaseNode<S, E> {
}
}

export {Base as Action};
export {Base as Condition};
export { Base as Action };
export { Base as Condition };
16 changes: 9 additions & 7 deletions lib/nodes/Composite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Parent, setEventCounter} from './Parent';
import {BlueshellState, ResultCode, resultCodes, BaseNode} from '../models';
import { Parent, setEventCounter } from './Parent';
import { BlueshellState, ResultCode, resultCodes, BaseNode } from '../models';

/**
* Base class for all Composite Nodes (nodes which have an array of children).
Expand All @@ -13,9 +13,11 @@ export abstract class Composite<S extends BlueshellState, E> extends Parent<S, E
* @param _children Children Nodes.
* @param _latched
*/
constructor(name: string,
private _children: BaseNode<S, E>[],
private _latched: boolean = false) {
constructor(
name: string,
private _children: BaseNode<S, E>[],
private _latched: boolean = false,
) {
super(name);
this.initChildren(_children);
}
Expand All @@ -38,13 +40,13 @@ export abstract class Composite<S extends BlueshellState, E> extends Parent<S, E
const res = super._beforeEvent(state, event);
const nodeStorage = this.getNodeStorage(state);
const pStorage = this._privateStorage(state);
const running = (nodeStorage.running !== undefined) ? nodeStorage.running : -1;
const running = nodeStorage.running !== undefined ? nodeStorage.running : -1;
this.getChildren().forEach((child, i) => {
if (i < running) {
// set event counter for each child before our latch point so they are registered
// as participating in this event (using the previous results)
setEventCounter(pStorage, state, child);
} // else do nothing with the running node or nodes after latch point
} // else do nothing with the running node or nodes after latch point
});
return res;
}
Expand Down
9 changes: 3 additions & 6 deletions lib/nodes/Constant.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {Action} from './Base';
import {ResultCode, BlueshellState} from '../models';
import { Action } from './Base';
import { ResultCode, BlueshellState } from '../models';

export class Constant<S extends BlueshellState, E> extends Action<S, E> {
constructor(
private readonly result: ResultCode,
name: string = result,
) {
constructor(private readonly result: ResultCode, name: string = result) {
super(name);
}

Expand Down
Loading

0 comments on commit 15124b7

Please sign in to comment.