(function() { "use strict"; var AIREADY = 0; var AISUCCESS = 1; var AIFAILURE = 2; var AIRUNNING = 3; var AIERROR = 4; var _AINEXTID = 1; // BASE BEHAVIOR TREE ========================================================= var AIBehaviorTree = function() { this.initialize(); } AIBehaviorTree.prototype.root = null; AIBehaviorTree.prototype.initialize = function() { } AIBehaviorTree.prototype.interrupt = function(target, fdelta) { target.memory.tree.interrupted = true; } AIBehaviorTree.prototype.update = function(target, fdelta) { var status = AIERROR; target.memory.tree.executionCount++; if (this.root) { status = this.root._update(target, fdelta); } target.memory.tree.interrupted = false; return status; } // ============================================================================ // BASE NODES ================================================================= var AIBaseNode = function() { this.initialize(); } AIBaseNode.prototype.initialize = function() {} AIBaseNode.prototype._update = function(target, fdelta) { target.memory.tree.lastNode = this; var status = this.update(target, fdelta); // var text = ['READY', 'SUCCESS', 'FAILURE', 'RUNNING', 'ERROR'][status] // if (target.memory.tree.executionCount < 10) { // console.log("["+target.name+"] "+this.name+": " + text); // } return status } AIBaseNode.prototype.setNodeVariable = function(target, key, value) { if (target.memory.tree.node[this.id] === undefined) { target.memory.tree.node[this.id] = {}; } target.memory.tree.node[this.id][key] = value; } AIBaseNode.prototype.getNodeVariable = function(target, key) { if (target.memory.tree.node[this.id] === undefined) { target.memory.tree.node[this.id] = {}; } return target.memory.tree.node[this.id][key]; } // Internal Node var AIInternalNode = function() { this.initialize(arguments); } AIInternalNode.prototype = new AIBaseNode(); AIInternalNode.prototype.id = null; AIInternalNode.prototype.name = 'untitled'; AIInternalNode.prototype.parent = null; AIInternalNode.prototype.children = null; AIInternalNode.prototype.isLeaf = false; AIInternalNode.prototype.initialize = function(args) { this.id = _AINEXTID++; this.children = []; for (var i=0; i= 0) { this.children.splice(index, 1); node.parent = null; } } // Leaf Node var AILeafNode = function() { this.initialize(arguments); } AILeafNode.prototype = new AIBaseNode(); AILeafNode.prototype.id = null; AILeafNode.prototype.name = 'untitled'; AILeafNode.prototype.btree = null; AILeafNode.prototype.parent = null; AILeafNode.prototype.isLeaf = true; AILeafNode.prototype.initialize = function() { this.id = _AINEXTID++; } AILeafNode.prototype.update = function(target, fdelta) { } // ============================================================================ // INTERNAL INSTANCES ========================================================= // Sequence var AISequence = function() { this.initialize(arguments); } AISequence.prototype = new AIInternalNode(); AISequence.prototype.name = 'Sequence'; AISequence.prototype.update = function(target, fdelta) { for (var i=0; i