WBT
Node.js Behavior Tree, this a Node.js library to design a Behavior Tree. This is inspired from http://guineashots.com/2014/09/24/implementing-a-behavior-tree-part-1/
Example
First let's define some Actions. We start by creating a DoRandom
action and then we will simulate a success action SuccessAction
and a fail action FailAction
.
// dorandom.js"use strict"; let BaseNode = ;let Status = ; let { thistext = text; // this is to simulate a long time execution task thisendTime = time; this;} DoRandomprototype = ;DoRandomprototype { let startTime = ; // Save the starttime value tickblackboard; // Check if event variable exists if not set it to 0 if tickblackboard == undefined tickblackboard; console;} DoRandomprototype { let currTime = ; let startTime = tickblackboard; if currTime - startTime > thisendTime console; // Increment event variable when the execution succeed let e = tickblackboard + 1; tickblackboard; return StatusSUCCESS; return StatusRUNNING;} moduleexports = DoRandom;
// successaction.js"use strict"; let BaseNode = ;let Status = ; let { thistext = text; this;} SuccessActionprototype = ;SuccessActionprototype { console; // reinitialize event variable tickblackboard; return StatusSUCCESS;} moduleexports = SuccessAction;
// failaction.js"use strict"; let BaseNode = ;let Status = ; let { thistext = text; this;} FailActionprototype = ;FailActionprototype { console; return StatusFAILURE;} moduleexports = FailAction;
Now let's create a condition node that check whether the value of the variable event
in blackboard is greater than 5 or not.
// iseventreceived.js"use strict"; let BaseNode = ;let Status = ; let { this;} IsEventReceivedprototype = ;IsEventReceivedprototype { let e = tickblackboard; if e && e > 5 console; return StatusSUCCESS; console; return StatusFAILURE;} moduleexports = IsEventReceived;
And the main file
// main.js"use strict"; let Blackboard = ;let BehaviorTree = ; let Selector = ;let Sequence = ;let MemSelector = ;let Random = ; let IsEventReceived = ;let FailAction = ;let SuccessAction = ;let DoRandom = ; let blackboard tree; blackboard = ;tree = ; treeroot = "Fail 1" "Success 1" "Fail 2" "Random 1" 2000 // Simulate a 2 seconds task "Random 2" 500 // Simulate a 0.5 second task "Random 3" 4000 // Simulate a 4 seconds task "Random 4" 6000 // Simulate a 6 seconds task ; // Run the Behavior Tree each 300ms (this can be changed to what the designer want)var intervalID = ;