forked from ermiyaeskandary/Slither.io-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree-complete.js
491 lines (447 loc) · 13.7 KB
/
btree-complete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
* BehaviorTree.js
* https://github.com/Calamari/BehaviorTree.js
*
* Copyright 2013-2104, Georg Tavonius
* Licensed under the MIT license.
*
* Includes Dean Edward's Base.js, version 1.1a
* Copyright 2006-2010, Dean Edwards
* License: http://www.opensource.org/licenses/mit-license.php
*
* Version: 1.0.3
*/
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.BehaviorTree=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"/hvVbF":[function(_dereq_,module,exports){
(function (global){
(function browserifyShim(module, exports, define, browserify_shim__define__module__export__) {
/*
Base.js, version 1.1a
Copyright 2006-2010, Dean Edwards
License: http://www.opensource.org/licenses/mit-license.php
*/
var Base = function() {
// dummy
};
Base.extend = function(_instance, _static) { // subclass
var extend = Base.prototype.extend;
// build the prototype
Base._prototyping = true;
var proto = new this;
extend.call(proto, _instance);
proto.base = function() {
// call this method from any other method to invoke that method's ancestor
};
delete Base._prototyping;
// create the wrapper for the constructor function
//var constructor = proto.constructor.valueOf(); //-dean
var constructor = proto.constructor;
var klass = proto.constructor = function() {
if (!Base._prototyping) {
if (this._constructing || this.constructor == klass) { // instantiation
this._constructing = true;
constructor.apply(this, arguments);
delete this._constructing;
} else if (arguments[0] != null) { // casting
return (arguments[0].extend || extend).call(arguments[0], proto);
}
}
};
// build the class interface
klass.ancestor = this;
klass.extend = this.extend;
klass.forEach = this.forEach;
klass.implement = this.implement;
klass.prototype = proto;
klass.toString = this.toString;
klass.valueOf = function(type) {
//return (type == "object") ? klass : constructor; //-dean
return (type == "object") ? klass : constructor.valueOf();
};
extend.call(klass, _static);
// class initialisation
if (typeof klass.init == "function") klass.init();
return klass;
};
Base.prototype = {
extend: function(source, value) {
if (arguments.length > 1) { // extending with a name/value pair
var ancestor = this[source];
if (ancestor && (typeof value == "function") && // overriding a method?
// the valueOf() comparison is to avoid circular references
(!ancestor.valueOf || ancestor.valueOf() != value.valueOf()) &&
/\bbase\b/.test(value)) {
// get the underlying method
var method = value.valueOf();
// override
value = function() {
var previous = this.base || Base.prototype.base;
this.base = ancestor;
var returnValue = method.apply(this, arguments);
this.base = previous;
return returnValue;
};
// point to the underlying method
value.valueOf = function(type) {
return (type == "object") ? value : method;
};
value.toString = Base.toString;
}
this[source] = value;
} else if (source) { // extending with an object literal
var extend = Base.prototype.extend;
// if this object has a customised extend method then use it
if (!Base._prototyping && typeof this != "function") {
extend = this.extend || extend;
}
var proto = {toSource: null};
// do the "toString" and other methods manually
var hidden = ["constructor", "toString", "valueOf"];
// if we are prototyping then include the constructor
var i = Base._prototyping ? 0 : 1;
while (key = hidden[i++]) {
if (source[key] != proto[key]) {
extend.call(this, key, source[key]);
}
}
// copy each of the source object's properties to this object
for (var key in source) {
if (!proto[key]) extend.call(this, key, source[key]);
}
}
return this;
}
};
// initialise
Base = Base.extend({
constructor: function() {
this.extend(arguments[0]);
}
}, {
ancestor: Object,
version: "1.1",
forEach: function(object, block, context) {
for (var key in object) {
if (this.prototype[key] === undefined) {
block.call(context, object[key], key, object);
}
}
},
implement: function() {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == "function") {
// if it's a function, call it
arguments[i](this.prototype);
} else {
// add the interface using the extend method
this.prototype.extend(arguments[i]);
}
}
return this;
},
toString: function() {
return String(this.valueOf());
}
});
; browserify_shim__define__module__export__(typeof Base != "undefined" ? Base : window.Base);
}).call(global, undefined, undefined, undefined, function defineExport(ex) { module.exports = ex; });
}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],"Base":[function(_dereq_,module,exports){
module.exports=_dereq_('/hvVbF');
},{}],3:[function(_dereq_,module,exports){
module.exports = _dereq_('./decorator').extend({
success: function() {
this._control.fail();
},
fail: function() {
this._control.fail();
},
});
},{"./decorator":8}],4:[function(_dereq_,module,exports){
module.exports = _dereq_('./decorator').extend({
success: function() {
this._control.success();
},
fail: function() {
this._control.success();
},
});
},{"./decorator":8}],5:[function(_dereq_,module,exports){
/**
* BehaviorTree.js
* https://github.com/Calamari/BehaviorTree.js
*
* Copyright 2013, Georg Tavonius
* Licensed under the MIT license.
*
* Version: 0.9.3
*/
var countUnnamed = 0,
BehaviorTree;
BehaviorTree = _dereq_('../lib/base').extend({
constructor: function(config) {
countUnnamed += 1;
this.title = config.title || 'btree' + (countUnnamed);
this._rootNode = config.tree;
this._object = config.object;
this._debug = config.debug;
},
setObject: function(obj) {
this._object = obj;
},
step: function() {
if (this._started) {
throw new Error('the BehaviorTree "' + this.title + '" did call step but one Task did not finish on last call of step.');
}
this._started = true;
var node = BehaviorTree.getNode(this._rootNode);
this._actualNode = node;
node.setControl(this);
node.start(this._object);
node.run(this._object);
},
running: function() {
this._started = false;
},
success: function() {
this._actualNode.end(this._object);
this._started = false;
},
fail: function() {
this._actualNode.end(this._object);
this._started = false;
}
});
BehaviorTree._registeredNodes = {};
BehaviorTree.register = function(name, node) {
if (typeof name === 'string') {
this._registeredNodes[name] = node;
} else {
// name is the node
this._registeredNodes[name.title] = name;
}
};
BehaviorTree.getNode = function(name) {
var node = name instanceof BehaviorTree.Node ? name : this._registeredNodes[name];
//f( this._debug ) {
// console.log("getNode -> " + name);
//}
if (!node) {
throw new Error('The node "' + name + '" could not be looked up. Maybe it was never registered?');
}
return node;
};
module.exports = BehaviorTree;
},{"../lib/base":"/hvVbF"}],6:[function(_dereq_,module,exports){
var BehaviorTree = _dereq_('./behavior_tree');
module.exports = _dereq_('./node').extend({
constructor: function(config) {
this.base(config);
this.children = this.nodes || [];
},
start: function() {
this._actualTask = 0;
},
run: function(object) {
this._object = object;
this.start();
if (this._actualTask < this.children.length) {
this._run();
}
this.end();
},
_run: function() {
var node = BehaviorTree.getNode(this.children[this._actualTask]);
this._runningNode = node;
node.setControl(this);
node.start(this._object);
node.run(this._object);
},
running: function(node) {
this._nodeRunning = node;
this._control.running(node);
},
success: function() {
this._nodeRunning = null;
this._runningNode.end(this._object);
},
fail: function() {
this._nodeRunning = null;
this._runningNode.end(this._object);
}
});
},{"./behavior_tree":5,"./node":11}],7:[function(_dereq_,module,exports){
module.exports = _dereq_('./branch_node').extend({
_run: function() {
if (this._nodeRunning) {
this._nodeRunning.run(this._object);
} else {
this.base();
}
},
success: function() {
this.base();
if( this._actualTask == 0 ) {
this._actualTask = 1;
if (this._actualTask < this.children.length) {
this._run(this._object);
} else {
this._control.success();
}
}
else
this._control.success();
},
fail: function() {
this.base();
if( this._actualTask == 0 ) {
this._actualTask = 2;
if (this._actualTask < this.children.length) {
this._run(this._object);
} else {
this._control.success();
}
}
else// if( this._actualTask == 2 ) { //forward if our fail node fails
this._control.fail();
//} else {
// this._control.success();
//}
}
});
},{"./branch_node":6}],8:[function(_dereq_,module,exports){
var BehaviorTree = _dereq_('./behavior_tree');
module.exports = _dereq_('./node').extend({
constructor: function(config) {
// let config override instance properties
this.base(config);
if (this.node) {
this.node = BehaviorTree.getNode(this.node);
}
},
setNode: function(node) {
this.node = BehaviorTree.getNode(node);
},
start: function() {
this.node.setControl(this);
this.node.start();
},
end: function() {
this.node.end();
},
run: function(blackboard) {
this.node.run(blackboard);
},
});
},{"./behavior_tree":5,"./node":11}],9:[function(_dereq_,module,exports){
var BehaviorTree = _dereq_('./behavior_tree');
BehaviorTree.Node = _dereq_('./node');
BehaviorTree.Task = _dereq_('./task');
BehaviorTree.BranchNode = _dereq_('./branch_node');
BehaviorTree.Priority = _dereq_('./priority');
BehaviorTree.Sequence = _dereq_('./sequence');
BehaviorTree.Random = _dereq_('./random');
BehaviorTree.Condition = _dereq_('./condition');
BehaviorTree.Decorator = _dereq_('./decorator');
BehaviorTree.InvertDecorator = _dereq_('./invert_decorator');
BehaviorTree.AlwaysFailDecorator = _dereq_('./always_fail_decorator');
BehaviorTree.AlwaysSucceedDecorator = _dereq_('./always_succeed_decorator');
module.exports = BehaviorTree;
},{"./always_fail_decorator":3,"./always_succeed_decorator":4,"./behavior_tree":5,"./branch_node":6,"./condition":7,"./decorator":8,"./invert_decorator":10,"./node":11,"./priority":12,"./random":13,"./sequence":14,"./task":15}],10:[function(_dereq_,module,exports){
module.exports = _dereq_('./decorator').extend({
success: function() {
this._control.fail();
},
fail: function() {
this._control.success();
},
});
},{"./decorator":8}],11:[function(_dereq_,module,exports){
module.exports = _dereq_('../lib/base').extend({
constructor: function(config) {
// let config override instance properties
this.base(config);
},
start: function() {},
end: function() {},
run: function() { throw new Error('Warning: run of ' + this.title + ' not implemented!'); },
setControl: function(control) {
this._control = control;
},
running: function() {
this._control.running(this);
},
success: function() {
this._control.success();
},
fail: function() {
this._control.fail();
}
});
},{"../lib/base":"/hvVbF"}],12:[function(_dereq_,module,exports){
module.exports = _dereq_('./branch_node').extend({
success: function() {
this.base();
this._control.success();
},
fail: function() {
this.base();
this._actualTask += 1;
if (this._actualTask < this.children.length) {
this._run(this._object);
} else {
this._control.fail();
}
}
});
},{"./branch_node":6}],13:[function(_dereq_,module,exports){
module.exports = _dereq_('./branch_node').extend({
start: function() {
this.base();
if (!this._nodeRunning) {
this._actualTask = Math.floor(Math.random()*this.children.length);
}
},
success: function() {
this.base();
this._control.success();
},
fail: function() {
this.base();
this._control.fail();
},
_run: function() {
if (!this._runningNode) {
this.base();
} else {
this._runningNode.run(this._object);
}
}
});
},{"./branch_node":6}],14:[function(_dereq_,module,exports){
module.exports = _dereq_('./branch_node').extend({
_run: function() {
if (this._nodeRunning) {
this._nodeRunning.run(this._object);
} else {
this.base();
}
},
success: function() {
this.base();
this._actualTask += 1;
if (this._actualTask < this.children.length) {
this._run(this._object);
} else {
this._control.success();
}
},
fail: function() {
this.base();
this._control.fail();
}
});
},{"./branch_node":6}],15:[function(_dereq_,module,exports){
module.exports = _dereq_('./node').extend({});
},{"./node":11}]},{},[9])
(9)
});