b3core.0.1.0module.js 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104
  1. /**
  2. * b3
  3. *
  4. * Copyright (c) 2014 Renato de Pontes Pereira.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. **/
  24. //this.b3 = this.b3 || {};
  25. let b3 = {};
  26. /**
  27. * Behavior3JS
  28. * ===========
  29. *
  30. * * * *
  31. *
  32. * **Behavior3JS** is a Behavior Tree library written in JavaScript. It
  33. * provides structures and algorithms that assist you in the task of creating
  34. * intelligent agents for your game or application. Check it out some features
  35. * of Behavior3JS:
  36. *
  37. * - Based on the work of (Marzinotto et al., 2014), in which they propose a
  38. * **formal**, **consistent** and **general** definition of Behavior Trees;
  39. * - **Optimized to control multiple agents**: you can use a single behavior
  40. * tree instance to handle hundreds of agents;
  41. * - It was **designed to load and save trees in a JSON format**, in order to
  42. * use, edit and test it in multiple environments, tools and languages;
  43. * - A **cool visual editor** which you can access online;
  44. * - Several **composite, decorator and action nodes** available within the
  45. * library. You still can define your own nodes, including composites and
  46. * decorators;
  47. * - **Completely free**, the core module and the visual editor are all published
  48. * under the MIT License, which means that you can use them for your open source
  49. * and commercial projects;
  50. * - **Lightweight**, only 11.5KB!
  51. *
  52. * Visit http://behavior3js.guineashots.com to know more!
  53. *
  54. *
  55. * Core Classes and Functions
  56. * --------------------------
  57. *
  58. * This library include the following core structures...
  59. *
  60. * **Public:**
  61. *
  62. * - **BehaviorTree**: the structure that represents a Behavior Tree;
  63. * - **Blackboard**: represents a "memory" in an agent and is required to to
  64. * run a `BehaviorTree`;
  65. * - **Composite**: base class for all composite nodes;
  66. * - **Decorator**: base class for all decorator nodes;
  67. * - **Action**: base class for all action nodes;
  68. * - **Condition**: base class for all condition nodes;
  69. *
  70. * **Internal:**
  71. *
  72. *
  73. * - **Tick**: used as container and tracking object through the tree during
  74. * the tick signal;
  75. * - **BaseNode**: the base class that provide all common node features;
  76. *
  77. * *Some classes are used internally on Behavior3JS, but you may need to access
  78. * its functionalities eventually, specially the `Tick` object.*
  79. *
  80. *
  81. * Nodes Included
  82. * --------------
  83. *
  84. * **Composite Nodes**:
  85. *
  86. * - Sequence;
  87. * - Priority;
  88. * - MemSequence;
  89. * - MemPriority;
  90. *
  91. *
  92. * **Decorators**:
  93. *
  94. * - Inverter;
  95. * - Limiter
  96. * - MaxTime;
  97. * - Repeater;
  98. * - RepeaterUntilFailure;
  99. * - RepeaterUntilSuccess;
  100. *
  101. *
  102. * **Actions**:
  103. *
  104. * - Succeeder;
  105. * - Failer;
  106. * - Error;
  107. * - Runner;
  108. * - Wait.
  109. *
  110. * @module Behavior3JS
  111. * @main Behavior3JS
  112. **/
  113. (function() {
  114. "use strict";
  115. /**
  116. * List of all constants in Behavior3JS.
  117. *
  118. * @class Constants
  119. **/
  120. /**
  121. * Version of the library.
  122. *
  123. * @property VERSION
  124. * @type {String}
  125. */
  126. b3.VERSION = '0.1.0';
  127. /**
  128. * Returned when a criterion has been met by a condition node or an action node
  129. * has been completed successfully.
  130. *
  131. * @property SUCCESS
  132. * @type {Integer}
  133. */
  134. b3.SUCCESS = 1;
  135. /**
  136. * Returned when a criterion has not been met by a condition node or an action
  137. * node could not finish its execution for any reason.
  138. *
  139. * @property FAILURE
  140. * @type {Integer}
  141. */
  142. b3.FAILURE = 2;
  143. /**
  144. * Returned when an action node has been initialized but is still waiting the
  145. * its resolution.
  146. *
  147. * @property FAILURE
  148. * @type {Integer}
  149. */
  150. b3.RUNNING = 3;
  151. /**
  152. * Returned when some unexpected error happened in the tree, probably by a
  153. * programming error (trying to verify an undefined variable). Its use depends
  154. * on the final implementation of the leaf nodes.
  155. *
  156. * @property ERROR
  157. * @type {Integer}
  158. */
  159. b3.ERROR = 4;
  160. /**
  161. * Describes the node category as Composite.
  162. *
  163. * @property COMPOSITE
  164. * @type {String}
  165. */
  166. b3.COMPOSITE = 'composite';
  167. /**
  168. * Describes the node category as Decorator.
  169. *
  170. * @property DECORATOR
  171. * @type {String}
  172. */
  173. b3.DECORATOR = 'decorator';
  174. /**
  175. * Describes the node category as Action.
  176. *
  177. * @property ACTION
  178. * @type {String}
  179. */
  180. b3.ACTION = 'action';
  181. /**
  182. * Describes the node category as Condition.
  183. *
  184. * @property CONDITION
  185. * @type {String}
  186. */
  187. b3.CONDITION = 'condition';
  188. /**
  189. * List of internal and helper functions in Behavior3JS.
  190. *
  191. * @class Utils
  192. **/
  193. /**
  194. * This function is used to create unique IDs for trees and nodes.
  195. *
  196. * (consult http://www.ietf.org/rfc/rfc4122.txt).
  197. *
  198. * @method createUUID
  199. * @return {String} A unique ID.
  200. **/
  201. b3.createUUID = function() {
  202. var s = [];
  203. var hexDigits = "0123456789abcdef";
  204. for (var i = 0; i < 36; i++) {
  205. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  206. }
  207. // bits 12-15 of the time_hi_and_version field to 0010
  208. s[14] = "4";
  209. // bits 6-7 of the clock_seq_hi_and_reserved to 01
  210. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  211. s[8] = s[13] = s[18] = s[23] = "-";
  212. var uuid = s.join("");
  213. return uuid;
  214. }
  215. /**
  216. * Class is a meta-factory function to create classes in JavaScript. It is a
  217. * shortcut for the CreateJS syntax style. By default, the class created by
  218. * this function have an initialize function (the constructor). Optionally, you
  219. * can specify the inheritance by passing another class as parameter.
  220. *
  221. * By default, all classes created using this function, may receives only a
  222. * settings parameter as argument. This pattern is commonly used by jQuery and
  223. * its plugins.
  224. *
  225. * Usage
  226. * -----
  227. *
  228. * // Creating a simple class
  229. * var BaseClass = b3.Class();
  230. *
  231. * // Using inheritance
  232. * var ChildClass = b3.Class(BaseClass);
  233. *
  234. * // Defining the constructor
  235. * ChildClass.prototype.initialize = function(settings) { ... }
  236. *
  237. * @method Class
  238. * @param {Object} [baseClass] The super class.
  239. * @return {Object} A new class.
  240. **/
  241. b3.Class = function(baseClass) {
  242. // create a new class
  243. var cls = function(params) {
  244. this.initialize(params);
  245. }
  246. // if base class is provided, inherit
  247. if (baseClass) {
  248. cls.prototype = new baseClass();
  249. cls.prototype.constructor = cls;
  250. }
  251. // create initialize if does not exist on baseClass
  252. cls.prototype.initialize = cls.prototype.initialize || function() {};
  253. return cls;
  254. }
  255. })();/**
  256. * Blackboard
  257. *
  258. * Copyright (c) 2014 Renato de Pontes Pereira.
  259. *
  260. * Permission is hereby granted, free of charge, to any person obtaining a copy
  261. * of this software and associated documentation files (the "Software"), to
  262. * deal in the Software without restriction, including without limitation the
  263. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  264. * sell copies of the Software, and to permit persons to whom the Software is
  265. * furnished to do so, subject to the following conditions:
  266. *
  267. * The above copyright notice and this permission notice shall be included in
  268. * all copies or substantial portions of the Software.
  269. *
  270. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  271. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  272. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  273. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  274. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  275. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  276. * IN THE SOFTWARE.
  277. **/
  278. /**
  279. * @module Behavior3JS
  280. **/
  281. // namespace:
  282. //this.b3 = this.b3 || {};
  283. (function() {
  284. "use strict";
  285. /**
  286. * The Blackboard is the memory structure required by `BehaviorTree` and its
  287. * nodes. It only have 2 public methods: `set` and `get`. These methods works
  288. * in 3 different contexts: global, per tree, and per node per tree.
  289. *
  290. * Suppose you have two different trees controlling a single object with a
  291. * single blackboard, then:
  292. *
  293. * - In the global context, all nodes will access the stored information.
  294. * - In per tree context, only nodes sharing the same tree share the stored
  295. * information.
  296. * - In per node per tree context, the information stored in the blackboard can
  297. * only be accessed by the same node that wrote the data.
  298. *
  299. * The context is selected indirectly by the parameters provided to these
  300. * methods, for example:
  301. *
  302. * // getting/setting variable in global context
  303. * blackboard.set('testKey', 'value');
  304. * var value = blackboard.get('testKey');
  305. *
  306. * // getting/setting variable in per tree context
  307. * blackboard.set('testKey', 'value', tree.id);
  308. * var value = blackboard.get('testKey', tree.id);
  309. *
  310. * // getting/setting variable in per node per tree context
  311. * blackboard.set('testKey', 'value', tree.id, node.id);
  312. * var value = blackboard.get('testKey', tree.id, node.id);
  313. *
  314. * Note: Internally, the blackboard store these memories in different objects,
  315. * being the global on `_baseMemory`, the per tree on `_treeMemory` and the
  316. * per node per tree dynamically create inside the per tree memory (it is
  317. * accessed via `_treeMemory[id].nodeMemory`). Avoid to use these variables
  318. * manually, use `get` and `set` instead.
  319. *
  320. * @class Blackboard
  321. **/
  322. var Blackboard = b3.Class();
  323. var p = Blackboard.prototype;
  324. /**
  325. * Initialization method.
  326. *
  327. * @method initialize
  328. * @constructor
  329. **/
  330. p.initialize = function() {
  331. this._baseMemory = {};
  332. this._treeMemory = {};
  333. }
  334. /**
  335. * Internal method to retrieve the tree context memory. If the memory does
  336. * not exist, this method creates it.
  337. *
  338. * @method _getTreeMemory
  339. * @param {string} treeScope The id of the tree in scope.
  340. * @returns {Object} The tree memory.
  341. * @protected
  342. **/
  343. p._getTreeMemory = function(treeScope) {
  344. if (!this._treeMemory[treeScope]) {
  345. this._treeMemory[treeScope] = {
  346. 'nodeMemory' : {},
  347. 'openNodes' : [],
  348. 'traversalDepth' : 0,
  349. 'traversalCycle' : 0,
  350. };
  351. }
  352. return this._treeMemory[treeScope];
  353. };
  354. /**
  355. * Internal method to retrieve the node context memory, given the tree
  356. * memory. If the memory does not exist, this method creates is.
  357. *
  358. * @method _getNodeMemory
  359. * @param {String} treeMemory the tree memory.
  360. * @param {String} nodeScope The id of the node in scope.
  361. * @returns {Object} The node memory.
  362. * @protected
  363. **/
  364. p._getNodeMemory = function(treeMemory, nodeScope) {
  365. var memory = treeMemory['nodeMemory'];
  366. if (!memory[nodeScope]) {
  367. memory[nodeScope] = {};
  368. }
  369. return memory[nodeScope];
  370. };
  371. /**
  372. * Internal method to retrieve the context memory. If treeScope and
  373. * nodeScope are provided, this method returns the per node per tree
  374. * memory. If only the treeScope is provided, it returns the per tree
  375. * memory. If no parameter is provided, it returns the global memory.
  376. * Notice that, if only nodeScope is provided, this method will still
  377. * return the global memory.
  378. *
  379. * @method _getMemory
  380. * @param {String} treeScope The id of the tree scope.
  381. * @param {String} nodeScope The id of the node scope.
  382. * @returns {Object} A memory object.
  383. * @protected
  384. **/
  385. p._getMemory = function(treeScope, nodeScope) {
  386. var memory = this._baseMemory;
  387. if (treeScope) {
  388. memory = this._getTreeMemory(treeScope);
  389. if (nodeScope) {
  390. memory = this._getNodeMemory(memory, nodeScope);
  391. }
  392. }
  393. return memory;
  394. };
  395. /**
  396. * Stores a value in the blackboard. If treeScope and nodeScope are
  397. * provided, this method will save the value into the per node per tree
  398. * memory. If only the treeScope is provided, it will save the value into
  399. * the per tree memory. If no parameter is provided, this method will save
  400. * the value into the global memory. Notice that, if only nodeScope is
  401. * provided (but treeScope not), this method will still save the value into
  402. * the global memory.
  403. *
  404. * @method set
  405. * @param {String} key The key to be stored.
  406. * @param {String} value The value to be stored.
  407. * @param {String} treeScope The tree id if accessing the tree or node
  408. * memory.
  409. * @param {String} nodeScope The node id if accessing the node memory.
  410. **/
  411. p.set = function(key, value, treeScope, nodeScope) {
  412. var memory = this._getMemory(treeScope, nodeScope);
  413. memory[key] = value;
  414. };
  415. /**
  416. * Retrieves a value in the blackboard. If treeScope and nodeScope are
  417. * provided, this method will retrieve the value from the per node per tree
  418. * memory. If only the treeScope is provided, it will retrieve the value
  419. * from the per tree memory. If no parameter is provided, this method will
  420. * retrieve from the global memory. If only nodeScope is provided (but
  421. * treeScope not), this method will still try to retrieve from the global
  422. * memory.
  423. *
  424. * @method get
  425. * @param {String} key The key to be retrieved.
  426. * @param {String} treeScope The tree id if accessing the tree or node
  427. * memory.
  428. * @param {String} nodeScope The node id if accessing the node memory.
  429. * @returns {Object} The value stored or undefined.
  430. **/
  431. p.get = function(key, treeScope, nodeScope) {
  432. var memory = this._getMemory(treeScope, nodeScope);
  433. return memory[key];
  434. };
  435. b3.Blackboard = Blackboard;
  436. })();/**
  437. * Tick
  438. *
  439. * Copyright (c) 2014 Renato de Pontes Pereira.
  440. *
  441. * Permission is hereby granted, free of charge, to any person obtaining a copy
  442. * of this software and associated documentation files (the "Software"), to
  443. * deal in the Software without restriction, including without limitation the
  444. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  445. * sell copies of the Software, and to permit persons to whom the Software is
  446. * furnished to do so, subject to the following conditions:
  447. *
  448. * The above copyright notice and this permission notice shall be included in
  449. * all copies or substantial portions of the Software.
  450. *
  451. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  452. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  453. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  454. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  455. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  456. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  457. * IN THE SOFTWARE.
  458. **/
  459. /**
  460. * @module Behavior3JS
  461. **/
  462. // namespace:
  463. //this.b3 = this.b3 || {};
  464. (function() {
  465. "use strict";
  466. /**
  467. * A new Tick object is instantiated every tick by BehaviorTree. It is passed
  468. * as parameter to the nodes through the tree during the traversal.
  469. *
  470. * The role of the Tick class is to store the instances of tree, debug, target
  471. * and blackboard. So, all nodes can access these informations.
  472. *
  473. * For internal uses, the Tick also is useful to store the open node after the
  474. * tick signal, in order to let `BehaviorTree` to keep track and close them
  475. * when necessary.
  476. *
  477. * This class also makes a bridge between nodes and the debug, passing the node
  478. * state to the debug if the last is provided.
  479. *
  480. * @class Tick
  481. **/
  482. var Tick = b3.Class();
  483. var p = Tick.prototype;
  484. /**
  485. * The tree reference.
  486. *
  487. * @property tree
  488. * @type {b3.BehaviorTree}
  489. * @readOnly
  490. */
  491. /**
  492. * The debug reference.
  493. *
  494. * @property debug
  495. * @type {Object}
  496. * @readOnly
  497. */
  498. /**
  499. * The target object reference.
  500. *
  501. * @property target
  502. * @type {Object}
  503. * @readOnly
  504. */
  505. /**
  506. * The blackboard reference.
  507. *
  508. * @property blackboard
  509. * @type {Blackboard}
  510. * @readOnly
  511. */
  512. /**
  513. * The list of open nodes. Update during the tree traversal.
  514. *
  515. * @property _openNodes
  516. * @type {Array}
  517. * @protected
  518. * @readOnly
  519. */
  520. /**
  521. * The number of nodes entered during the tick. Update during the tree
  522. * traversal.
  523. *
  524. * @property _nodeCount
  525. * @type {Integer}
  526. * @protected
  527. * @readOnly
  528. */
  529. /**
  530. * Initialization method.
  531. *
  532. * @method initialize
  533. * @constructor
  534. **/
  535. p.initialize = function() {
  536. // set by BehaviorTree
  537. this.tree = null;
  538. this.debug = null;
  539. this.target = null;
  540. this.blackboard = null;
  541. // updated during the tick signal
  542. this._openNodes = [];
  543. this._nodeCount = 0;
  544. }
  545. /**
  546. * Called when entering a node (called by BaseNode).
  547. *
  548. * @method _enterNode
  549. * @param {Object} node The node that called this method.
  550. * @protected
  551. **/
  552. p._enterNode = function(node) {
  553. this._nodeCount++;
  554. this._openNodes.push(node);
  555. // TODO: call debug here
  556. }
  557. /**
  558. * Callback when opening a node (called by BaseNode).
  559. *
  560. * @method _openNode
  561. * @param {Object} node The node that called this method.
  562. * @protected
  563. **/
  564. p._openNode = function(node) {
  565. // TODO: call debug here
  566. }
  567. /**
  568. * Callback when ticking a node (called by BaseNode).
  569. *
  570. * @method _tickNode
  571. * @param {Object} node The node that called this method.
  572. * @protected
  573. **/
  574. p._tickNode = function(node) {
  575. // TODO: call debug here
  576. }
  577. /**
  578. * Callback when closing a node (called by BaseNode).
  579. *
  580. * @method _closeNode
  581. * @param {Object} node The node that called this method.
  582. * @protected
  583. **/
  584. p._closeNode = function(node) {
  585. // TODO: call debug here
  586. this._openNodes.pop();
  587. }
  588. /**
  589. * Callback when exiting a node (called by BaseNode).
  590. *
  591. * @method _exitNode
  592. * @param {Object} node The node that called this method.
  593. * @protected
  594. **/
  595. p._exitNode = function(node) {
  596. // TODO: call debug here
  597. }
  598. b3.Tick = Tick;
  599. })();/**
  600. * BehaviorTree
  601. *
  602. * Copyright (c) 2014 Renato de Pontes Pereira.
  603. *
  604. * Permission is hereby granted, free of charge, to any person obtaining a copy
  605. * of this software and associated documentation files (the "Software"), to
  606. * deal in the Software without restriction, including without limitation the
  607. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  608. * sell copies of the Software, and to permit persons to whom the Software is
  609. * furnished to do so, subject to the following conditions:
  610. *
  611. * The above copyright notice and this permission notice shall be included in
  612. * all copies or substantial portions of the Software.
  613. *
  614. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  615. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  616. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  617. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  618. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  619. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  620. * IN THE SOFTWARE.
  621. **/
  622. /**
  623. * @module Behavior3JS
  624. **/
  625. // namespace:
  626. //this.b3 = this.b3 || {};
  627. (function() {
  628. "use strict";
  629. /**
  630. * The BehaviorTree class, as the name implies, represents the Behavior Tree
  631. * structure.
  632. *
  633. * There are two ways to construct a Behavior Tree: by manually setting the
  634. * root node, or by loading it from a data structure (which can be loaded from
  635. * a JSON). Both methods are shown in the examples below and better explained
  636. * in the user guide.
  637. *
  638. * The tick method must be called periodically, in order to send the tick
  639. * signal to all nodes in the tree, starting from the root. The method
  640. * `BehaviorTree.tick` receives a target object and a blackboard as parameters.
  641. * The target object can be anything: a game agent, a system, a DOM object,
  642. * etc. This target is not used by any piece of Behavior3JS, i.e., the target
  643. * object will only be used by custom nodes.
  644. *
  645. * The blackboard is obligatory and must be an instance of `Blackboard`. This
  646. * requirement is necessary due to the fact that neither `BehaviorTree` or any
  647. * node will store the execution variables in its own object (e.g., the BT does
  648. * not store the target, information about opened nodes or number of times the
  649. * tree was called). But because of this, you only need a single tree instance
  650. * to control multiple (maybe hundreds) objects.
  651. *
  652. * Manual construction of a Behavior Tree
  653. * --------------------------------------
  654. *
  655. * var tree = new b3.BehaviorTree();
  656. *
  657. * tree.root = new b3.Sequence({children:[
  658. * new b3.Priority({children:[
  659. * new MyCustomNode(),
  660. * new MyCustomNode()
  661. * ]}),
  662. * ...
  663. * ]});
  664. *
  665. *
  666. * Loading a Behavior Tree from data structure
  667. * -------------------------------------------
  668. *
  669. * var tree = new b3.BehaviorTree();
  670. *
  671. * tree.load({
  672. * 'title' : 'Behavior Tree title'
  673. * 'description' : 'My description'
  674. * 'root' : 'node-id-1'
  675. * 'nodes' : {
  676. * 'node-id-1' : {
  677. * 'name' : 'Priority', // this is the node type
  678. * 'title' : 'Root Node',
  679. * 'description' : 'Description',
  680. * 'children' : ['node-id-2', 'node-id-3'],
  681. * },
  682. * ...
  683. * }
  684. * })
  685. *
  686. *
  687. * @class BehaviorTree
  688. **/
  689. var BehaviorTree = b3.Class();
  690. var p = BehaviorTree.prototype;
  691. /**
  692. * The tree id, must be unique. By default, created with `b3.createUUID`.
  693. *
  694. * @property id
  695. * @type {String}
  696. * @readOnly
  697. */
  698. /**
  699. * The tree title.
  700. *
  701. * @property title
  702. * @type {String}
  703. */
  704. /**
  705. * Description of the tree.
  706. *
  707. * @property description
  708. * @type {String}
  709. */
  710. /**
  711. * A dictionary with (key-value) properties. Useful to define custom
  712. * variables in the visual editor.
  713. *
  714. * @property properties
  715. * @type {Object}
  716. */
  717. /**
  718. * The reference to the root node. Must be an instance of `b3.BaseNode`.
  719. *
  720. * @property root
  721. * @type {BaseNode}
  722. */
  723. /**
  724. * The reference to the debug instance.
  725. *
  726. * @property debug
  727. * @type {Object}
  728. */
  729. /**
  730. * Initialization method.
  731. *
  732. * @method initialize
  733. * @constructor
  734. **/
  735. p.initialize = function() {
  736. this.id = b3.createUUID();
  737. this.title = 'The behavior tree';
  738. this.description = 'Default description';
  739. this.properties = {};
  740. this.root = null;
  741. this.debug = null;
  742. }
  743. /**
  744. * This method loads a Behavior Tree from a data structure, populating this
  745. * object with the provided data. Notice that, the data structure must
  746. * follow the format specified by Behavior3JS. Consult the guide to know
  747. * more about this format.
  748. *
  749. * You probably want to use custom nodes in your BTs, thus, you need to
  750. * provide the `names` object, in which this method can find the nodes by
  751. * `names[NODE_NAME]`. This variable can be a namespace or a dictionary,
  752. * as long as this method can find the node by its name, for example:
  753. *
  754. * //json
  755. * ...
  756. * 'node1': {
  757. * 'name': MyCustomNode,
  758. * 'title': ...
  759. * }
  760. * ...
  761. *
  762. * //code
  763. * var bt = new b3.BehaviorTree();
  764. * bt.load(data, {'MyCustomNode':MyCustomNode})
  765. *
  766. *
  767. * @method load
  768. * @param {Object} data The data structure representing a Behavior Tree.
  769. * @param {Object} [names] A namespace or dict containing custom nodes.
  770. **/
  771. p.load = function(data, names) {
  772. names = names || {};
  773. this.title = data.title || this.title;
  774. this.description = data.description || this.description;
  775. this.properties = data.properties || this.properties;
  776. var nodes = {};
  777. // Create the node list (without connection between them)
  778. for (var id in data.nodes) {
  779. var spec = data.nodes[id];
  780. if (spec.name in names) {
  781. // Look for the name in custom nodes
  782. var cls = names[spec.name];
  783. } else if (spec.name in b3) {
  784. // Look for the name in default nodes
  785. var cls = b3[spec.name];
  786. } else {
  787. // Invalid node name
  788. throw EvalError('BehaviorTree.load: Invalid node name + "'+
  789. spec.name+'".');
  790. }
  791. var node = new cls(spec.parameters);
  792. node.id = spec.id || node.id;
  793. node.title = spec.title || node.title;
  794. node.description = spec.description || node.description;
  795. node.properties = spec.properties || node.properties;
  796. node.parameters = spec.parameters || node.parameters;
  797. nodes[id] = node;
  798. }
  799. // Connect the nodes
  800. for (var id in data.nodes) {
  801. var spec = data.nodes[id];
  802. var node = nodes[id];
  803. if (node.category === b3.COMPOSITE && spec.children) {
  804. for (var i=0; i<spec.children.length; i++) {
  805. var cid = spec.children[i];
  806. node.children.push(nodes[cid]);
  807. }
  808. } else if (node.category === b3.DECORATOR && spec.child) {
  809. node.child = nodes[spec.child];
  810. }
  811. }
  812. this.root = nodes[data.root];
  813. };
  814. /**
  815. * This method dump the current BT into a data structure.
  816. *
  817. * Note: This method does not record the current node parameters. Thus,
  818. * it may not be compatible with load for now.
  819. *
  820. * @method dump
  821. * @returns {Object} A data object representing this tree.
  822. **/
  823. p.dump = function() {
  824. var data = {};
  825. data.title = this.title;
  826. data.description = this.description;
  827. data.root = (this.root)? this.root.id:null;
  828. data.properties = this.properties;
  829. data.nodes = {};
  830. if (!this.root) return data;
  831. var stack = [this.root];
  832. while (stack.length > 0) {
  833. var node = stack.pop();
  834. var spec = {};
  835. spec.id = node.id;
  836. spec.name = node.name;
  837. spec.title = node.title;
  838. spec.description = node.description;
  839. spec.properties = node.properties;
  840. spec.parameters = node.parameters;
  841. if (node.category === b3.COMPOSITE && node.children) {
  842. var children = []
  843. for (var i=node.children.length-1; i>=0; i--) {
  844. children.push(node.children[i].id);
  845. stack.push(node.children[i]);
  846. }
  847. spec.children = children;
  848. } else if (node.category === b3.DECORATOR && node.child) {
  849. stack.push(node.child);
  850. spec.child = node.child.id;
  851. }
  852. data.nodes[node.id] = spec;
  853. }
  854. return data;
  855. };
  856. /**
  857. * Propagates the tick signal through the tree, starting from the root.
  858. *
  859. * This method receives a target object of any type (Object, Array,
  860. * DOMElement, whatever) and a `Blackboard` instance. The target object has
  861. * no use at all for all Behavior3JS components, but surely is important
  862. * for custom nodes. The blackboard instance is used by the tree and nodes
  863. * to store execution variables (e.g., last node running) and is obligatory
  864. * to be a `Blackboard` instance (or an object with the same interface).
  865. *
  866. * Internally, this method creates a Tick object, which will store the
  867. * target and the blackboard objects.
  868. *
  869. * Note: BehaviorTree stores a list of open nodes from last tick, if these
  870. * nodes weren't called after the current tick, this method will close them
  871. * automatically.
  872. *
  873. * @method tick
  874. * @param {Object} target A target object.
  875. * @param {Blackboard} blackboard An instance of blackboard object.
  876. * @returns {Constant} The tick signal state.
  877. **/
  878. p.tick = function(target, blackboard) {
  879. if (!blackboard) {
  880. throw 'The blackboard parameter is obligatory and must be an ' +
  881. 'instance of b3.Blackboard';
  882. }
  883. /* CREATE A TICK OBJECT */
  884. var tick = new b3.Tick();
  885. tick.debug = this.debug;
  886. tick.target = target;
  887. tick.blackboard = blackboard;
  888. tick.tree = this;
  889. /* TICK NODE */
  890. var state = this.root._execute(tick);
  891. /* CLOSE NODES FROM LAST TICK, IF NEEDED */
  892. var lastOpenNodes = blackboard.get('openNodes', this.id);
  893. var currOpenNodes = tick._openNodes.slice(0);
  894. // does not close if it is still open in this tick
  895. var start = 0;
  896. for (var i=0; i<Math.min(lastOpenNodes.length, currOpenNodes.length); i++) {
  897. start = i+1;
  898. if (lastOpenNodes[i] !== currOpenNodes[i]) {
  899. break;
  900. }
  901. }
  902. // close the nodes
  903. for (var i=lastOpenNodes.length-1; i>=start; i--) {
  904. lastOpenNodes[i]._close(tick);
  905. }
  906. /* POPULATE BLACKBOARD */
  907. blackboard.set('openNodes', currOpenNodes, this.id);
  908. blackboard.set('nodeCount', tick._nodeCount, this.id);
  909. return state;
  910. };
  911. b3.BehaviorTree = BehaviorTree;
  912. })();/**
  913. * BaseNode
  914. *
  915. * Copyright (c) 2014 Renato de Pontes Pereira.
  916. *
  917. * Permission is hereby granted, free of charge, to any person obtaining a copy
  918. * of this software and associated documentation files (the "Software"), to
  919. * deal in the Software without restriction, including without limitation the
  920. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  921. * sell copies of the Software, and to permit persons to whom the Software is
  922. * furnished to do so, subject to the following conditions:
  923. *
  924. * The above copyright notice and this permission notice shall be included in
  925. * all copies or substantial portions of the Software.
  926. *
  927. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  928. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  929. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  930. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  931. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  932. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  933. * IN THE SOFTWARE.
  934. **/
  935. /**
  936. * @module Behavior3JS
  937. **/
  938. // namespace:
  939. //this.b3 = this.b3 || {};
  940. (function() {
  941. "use strict";
  942. /**
  943. * The BaseNode class is used as super class to all nodes in BehaviorJS. It
  944. * comprises all common variables and methods that a node must have to execute.
  945. *
  946. * **IMPORTANT:** Do not inherit from this class, use `b3.Composite`,
  947. * `b3.Decorator`, `b3.Action` or `b3.Condition`, instead.
  948. *
  949. * The attributes are specially designed to serialization of the node in a JSON
  950. * format. In special, the `parameters` attribute can be set into the visual
  951. * editor (thus, in the JSON file), and it will be used as parameter on the
  952. * node initialization at `BehaviorTree.load`.
  953. *
  954. * BaseNode also provide 5 callback methods, which the node implementations can
  955. * override. They are `enter`, `open`, `tick`, `close` and `exit`. See their
  956. * documentation to know more. These callbacks are called inside the `_execute`
  957. * method, which is called in the tree traversal.
  958. *
  959. * @class BaseNode
  960. **/
  961. var BaseNode = b3.Class();
  962. var p = BaseNode.prototype;
  963. /**
  964. * Node ID.
  965. *
  966. * @property id
  967. * @type {String}
  968. * @readonly
  969. **/
  970. /**
  971. * Node name. Must be a unique identifier, preferable the same name of the
  972. * class. You have to set the node name in the prototype.
  973. *
  974. * @property name
  975. * @type {String}
  976. * @readonly
  977. **/
  978. p.name = null;
  979. /**
  980. * Node category. Must be `b3.COMPOSITE`, `b3.DECORATOR`, `b3.ACTION` or
  981. * `b3.CONDITION`. This is defined automatically be inheriting the
  982. * correspondent class.
  983. *
  984. * @property category
  985. * @type constant
  986. * @readonly
  987. **/
  988. p.category = null;
  989. /**
  990. * Node title.
  991. *
  992. * @property title
  993. * @type {String}
  994. * @optional
  995. * @readonly
  996. **/
  997. p.title = null;
  998. /**
  999. * Node description.
  1000. *
  1001. * @property description
  1002. * @type {String}
  1003. * @optional
  1004. * @readonly
  1005. **/
  1006. p.description = null;
  1007. /**
  1008. * A dictionary (key, value) describing the node parameters. Useful for
  1009. * defining parameter values in the visual editor. Note: this is only
  1010. * useful for nodes when loading trees from JSON files.
  1011. *
  1012. * @property parameters
  1013. * @type {Object}
  1014. * @readonly
  1015. **/
  1016. p.parameters = null;
  1017. /**
  1018. * A dictionary (key, value) describing the node properties. Useful for
  1019. * defining custom variables inside the visual editor.
  1020. *
  1021. * @property properties
  1022. * @type {Object}
  1023. * @readonly
  1024. **/
  1025. p.properties = null;
  1026. /**
  1027. * Initialization method.
  1028. *
  1029. * @method initialize
  1030. * @constructor
  1031. **/
  1032. p.initialize = function() {
  1033. this.id = b3.createUUID();
  1034. this.title = this.title || this.name;
  1035. this.description = '';
  1036. this.parameters = {};
  1037. this.properties = {};
  1038. }
  1039. /**
  1040. * This is the main method to propagate the tick signal to this node. This
  1041. * method calls all callbacks: `enter`, `open`, `tick`, `close`, and
  1042. * `exit`. It only opens a node if it is not already open. In the same
  1043. * way, this method only close a node if the node returned a status
  1044. * different of `b3.RUNNING`.
  1045. *
  1046. * @method _execute
  1047. * @param {Tick} tick A tick instance.
  1048. * @returns {Constant} The tick state.
  1049. * @protected
  1050. **/
  1051. p._execute = function(tick) {
  1052. /* ENTER */
  1053. this._enter(tick);
  1054. /* OPEN */
  1055. if (!tick.blackboard.get('isOpen', tick.tree.id, this.id)) {
  1056. this._open(tick);
  1057. }
  1058. /* TICK */
  1059. var status = this._tick(tick);
  1060. /* CLOSE */
  1061. if (status !== b3.RUNNING) {
  1062. this._close(tick);
  1063. }
  1064. /* EXIT */
  1065. this._exit(tick);
  1066. return status;
  1067. }
  1068. /**
  1069. * Wrapper for enter method.
  1070. *
  1071. * @method _enter
  1072. * @param {Tick} tick A tick instance.
  1073. * @protected
  1074. **/
  1075. p._enter = function(tick) {
  1076. tick._enterNode(this);
  1077. this.enter(tick);
  1078. }
  1079. /**
  1080. * Wrapper for open method.
  1081. *
  1082. * @method _open
  1083. * @param {Tick} tick A tick instance.
  1084. * @protected
  1085. **/
  1086. p._open = function(tick) {
  1087. tick._openNode(this);
  1088. tick.blackboard.set('isOpen', true, tick.tree.id, this.id);
  1089. this.open(tick);
  1090. }
  1091. /**
  1092. * Wrapper for tick method.
  1093. *
  1094. * @method _tick
  1095. * @param {Tick} tick A tick instance.
  1096. * @returns {Constant} A state constant.
  1097. * @protected
  1098. **/
  1099. p._tick = function(tick) {
  1100. tick._tickNode(this);
  1101. return this.tick(tick);
  1102. }
  1103. /**
  1104. * Wrapper for close method.
  1105. *
  1106. * @method _close
  1107. * @param {Tick} tick A tick instance.
  1108. * @protected
  1109. **/
  1110. p._close = function(tick) {
  1111. tick._closeNode(this);
  1112. tick.blackboard.set('isOpen', false, tick.tree.id, this.id);
  1113. this.close(tick);
  1114. }
  1115. /**
  1116. * Wrapper for exit method.
  1117. *
  1118. * @method _exit
  1119. * @param {Tick} tick A tick instance.
  1120. * @protected
  1121. **/
  1122. p._exit = function(tick) {
  1123. tick._exitNode(this);
  1124. this.exit(tick);
  1125. }
  1126. /**
  1127. * Enter method, override this to use. It is called every time a node is
  1128. * asked to execute, before the tick itself.
  1129. *
  1130. * @method enter
  1131. * @param {Tick} tick A tick instance.
  1132. **/
  1133. p.enter = function(tick) {}
  1134. /**
  1135. * Open method, override this to use. It is called only before the tick
  1136. * callback and only if the not isn't closed.
  1137. *
  1138. * Note: a node will be closed if it returned `b3.RUNNING` in the tick.
  1139. *
  1140. * @method open
  1141. * @param {Tick} tick A tick instance.
  1142. **/
  1143. p.open = function(tick) {}
  1144. /**
  1145. * Tick method, override this to use. This method must contain the real
  1146. * execution of node (perform a task, call children, etc.). It is called
  1147. * every time a node is asked to execute.
  1148. *
  1149. * @method tick
  1150. * @param {Tick} tick A tick instance.
  1151. **/
  1152. p.tick = function(tick) {}
  1153. /**
  1154. * Close method, override this to use. This method is called after the tick
  1155. * callback, and only if the tick return a state different from
  1156. * `b3.RUNNING`.
  1157. *
  1158. * @method close
  1159. * @param {Tick} tick A tick instance.
  1160. **/
  1161. p.close = function(tick) {}
  1162. /**
  1163. * Exit method, override this to use. Called every time in the end of the
  1164. * execution.
  1165. *
  1166. * @method exit
  1167. * @param {Tick} tick A tick instance.
  1168. **/
  1169. p.exit = function(tick) {}
  1170. b3.BaseNode = BaseNode;
  1171. })();/**
  1172. * Action
  1173. *
  1174. * Copyright (c) 2014 Renato de Pontes Pereira.
  1175. *
  1176. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1177. * of this software and associated documentation files (the "Software"), to
  1178. * deal in the Software without restriction, including without limitation the
  1179. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1180. * sell copies of the Software, and to permit persons to whom the Software is
  1181. * furnished to do so, subject to the following conditions:
  1182. *
  1183. * The above copyright notice and this permission notice shall be included in
  1184. * all copies or substantial portions of the Software.
  1185. *
  1186. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1187. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1188. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1189. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1190. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1191. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1192. * IN THE SOFTWARE.
  1193. **/
  1194. /**
  1195. * @module Behavior3JS
  1196. **/
  1197. // namespace:
  1198. //this.b3 = this.b3 || {};
  1199. (function() {
  1200. "use strict";
  1201. /**
  1202. * Action is the base class for all action nodes. Thus, if you want to
  1203. * create new custom action nodes, you need to inherit from this class.
  1204. *
  1205. * For example, take a look at the Runner action:
  1206. *
  1207. * var Runner = b3.Class(b3.Action);
  1208. * var p = Runner.prototype;
  1209. *
  1210. * p.name = 'Runner';
  1211. *
  1212. * p.tick = function(tick) {
  1213. * return b3.RUNNING;
  1214. * }
  1215. *
  1216. * @class Action
  1217. * @extends BaseNode
  1218. **/
  1219. var Action = b3.Class(b3.BaseNode);
  1220. var p = Action.prototype;
  1221. /**
  1222. * Node category. Default to `b3.ACTION`.
  1223. *
  1224. * @property category
  1225. * @type {String}
  1226. * @readonly
  1227. **/
  1228. p.category = b3.ACTION;
  1229. p.__BaseNode_initialize = p.initialize;
  1230. /**
  1231. * Initialization method.
  1232. *
  1233. * @method initialize
  1234. * @constructor
  1235. **/
  1236. p.initialize = function() {
  1237. this.__BaseNode_initialize();
  1238. }
  1239. b3.Action = Action;
  1240. })();/**
  1241. * Composite
  1242. *
  1243. * Copyright (c) 2014 Renato de Pontes Pereira.
  1244. *
  1245. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1246. * of this software and associated documentation files (the "Software"), to
  1247. * deal in the Software without restriction, including without limitation the
  1248. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1249. * sell copies of the Software, and to permit persons to whom the Software is
  1250. * furnished to do so, subject to the following conditions:
  1251. *
  1252. * The above copyright notice and this permission notice shall be included in
  1253. * all copies or substantial portions of the Software.
  1254. *
  1255. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1256. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1257. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1258. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1259. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1260. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1261. * IN THE SOFTWARE.
  1262. **/
  1263. /**
  1264. * @module Behavior3JS
  1265. **/
  1266. // namespace:
  1267. //this.b3 = this.b3 || {};
  1268. (function() {
  1269. "use strict";
  1270. /**
  1271. * Composite is the base class for all composite nodes. Thus, if you want to
  1272. * create new custom composite nodes, you need to inherit from this class.
  1273. *
  1274. * When creating composite nodes, you will need to propagate the tick signal to
  1275. * the children nodes manually. To do that, override the `tick` method and call
  1276. * the `_execute` method on all nodes. For instance, take a look at how the
  1277. * Sequence node inherit this class and how it call its children:
  1278. *
  1279. *
  1280. * // Inherit from Composite, using the util function Class.
  1281. * var Sequence = b3.Class(b3.Composite);
  1282. * var p = Sequence.prototype;
  1283. *
  1284. * // Remember to set the name of the node.
  1285. * p.name = 'Sequence';
  1286. *
  1287. * // Override the tick function
  1288. * p.tick = function(tick) {
  1289. *
  1290. * // Iterates over the children
  1291. * for (var i=0; i<this.children.length; i++) {
  1292. *
  1293. * // Propagate the tick
  1294. * var status = this.children[i]._execute(tick);
  1295. *
  1296. * if (status !== b3.SUCCESS) {
  1297. * return status;
  1298. * }
  1299. * }
  1300. *
  1301. * return b3.SUCCESS;
  1302. * }
  1303. *
  1304. * @class Composite
  1305. * @extends BaseNode
  1306. **/
  1307. var Composite = b3.Class(b3.z);
  1308. var p = Composite.prototype;
  1309. /**
  1310. * Node category. Default to `b3.COMPOSITE`.
  1311. *
  1312. * @property category
  1313. * @type {String}
  1314. * @readonly
  1315. **/
  1316. p.category = b3.COMPOSITE;
  1317. p.__BaseNode_initialize = p.initialize;
  1318. /**
  1319. * Initialization method.
  1320. *
  1321. * @method initialize
  1322. * @constructor
  1323. **/
  1324. p.initialize = function(settings) {
  1325. settings = settings || {};
  1326. this.__BaseNode_initialize();
  1327. this.children = (settings.children || []).slice(0);
  1328. };
  1329. b3.Composite = Composite;
  1330. })();/**
  1331. * Decorator
  1332. *
  1333. * Copyright (c) 2014 Renato de Pontes Pereira.
  1334. *
  1335. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1336. * of this software and associated documentation files (the "Software"), to
  1337. * deal in the Software without restriction, including without limitation the
  1338. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1339. * sell copies of the Software, and to permit persons to whom the Software is
  1340. * furnished to do so, subject to the following conditions:
  1341. *
  1342. * The above copyright notice and this permission notice shall be included in
  1343. * all copies or substantial portions of the Software.
  1344. *
  1345. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1346. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1347. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1348. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1349. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1350. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1351. * IN THE SOFTWARE.
  1352. **/
  1353. /**
  1354. * @module Behavior3JS
  1355. **/
  1356. // namespace:
  1357. //this.b3 = this.b3 || {};
  1358. (function() {
  1359. "use strict";
  1360. /**
  1361. * Decorator is the base class for all decorator nodes. Thus, if you want to
  1362. * create new custom decorator nodes, you need to inherit from this class.
  1363. *
  1364. * When creating decorator nodes, you will need to propagate the tick signal to
  1365. * the child node manually, just like the composite nodes. To do that, override
  1366. * the `tick` method and call the `_execute` method on the child node. For
  1367. * instance, take a look at how the Inverter node inherit this class and how it
  1368. * call its children:
  1369. *
  1370. *
  1371. * // Inherit from Decorator, using the util function Class.
  1372. * var Inverter = b3.Class(b3.Decorator);
  1373. * var p = Inverter.prototype;
  1374. *
  1375. * // Remember to set the name of the node.
  1376. * p.name = 'Inverter';
  1377. *
  1378. * // Override the tick function
  1379. * p.tick = function(tick) {
  1380. * if (!this.child) {
  1381. * return b3.ERROR;
  1382. * }
  1383. *
  1384. * // Propagate the tick
  1385. * var status = this.child._execute(tick);
  1386. *
  1387. * if (status == b3.SUCCESS)
  1388. * status = b3.FAILURE;
  1389. * else if (status == b3.FAILURE)
  1390. * status = b3.SUCCESS;
  1391. *
  1392. * return status;
  1393. * };
  1394. *
  1395. * @class Decorator
  1396. * @extends BaseNode
  1397. **/
  1398. var Decorator = b3.Class(b3.BaseNode);
  1399. var p = Decorator.prototype;
  1400. /**
  1401. * Node category. Default to b3.DECORATOR.
  1402. *
  1403. * @property category
  1404. * @type {String}
  1405. * @readonly
  1406. **/
  1407. p.category = b3.DECORATOR;
  1408. p.__BaseNode_initialize = p.initialize;
  1409. /**
  1410. * Initialization method.
  1411. *
  1412. * @method initialize
  1413. * @constructor
  1414. **/
  1415. p.initialize = function(settings) {
  1416. settings = settings || {};
  1417. this.__BaseNode_initialize();
  1418. this.child = settings.child || null;
  1419. };
  1420. b3.Decorator = Decorator;
  1421. })();/**
  1422. * Condition
  1423. *
  1424. * Copyright (c) 2014 Renato de Pontes Pereira.
  1425. *
  1426. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1427. * of this software and associated documentation files (the "Software"), to
  1428. * deal in the Software without restriction, including without limitation the
  1429. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1430. * sell copies of the Software, and to permit persons to whom the Software is
  1431. * furnished to do so, subject to the following conditions:
  1432. *
  1433. * The above copyright notice and this permission notice shall be included in
  1434. * all copies or substantial portions of the Software.
  1435. *
  1436. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1437. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1438. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1439. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1440. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1441. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1442. * IN THE SOFTWARE.
  1443. **/
  1444. /**
  1445. * @module Behavior3JS
  1446. **/
  1447. // namespace:
  1448. //this.b3 = this.b3 || {};
  1449. (function() {
  1450. "use strict";
  1451. /**
  1452. * Condition is the base class for all condition nodes. Thus, if you want to
  1453. * create new custom condition nodes, you need to inherit from this class.
  1454. *
  1455. * @class Condition
  1456. * @extends BaseNode
  1457. **/
  1458. var Condition = b3.Class(b3.BaseNode);
  1459. var p = Condition.prototype;
  1460. /**
  1461. * Node category. Default to `b3.CONDITION`.
  1462. *
  1463. * @property category
  1464. * @type {String}
  1465. * @readonly
  1466. **/
  1467. p.category = b3.CONDITION;
  1468. p.__BaseNode_initialize = p.initialize;
  1469. /**
  1470. * Initialization method.
  1471. *
  1472. * @method initialize
  1473. * @constructor
  1474. **/
  1475. p.initialize = function() {
  1476. this.__BaseNode_initialize();
  1477. }
  1478. b3.Condition = Condition;
  1479. })();/**
  1480. * Sequence
  1481. *
  1482. * Copyright (c) 2014 Renato de Pontes Pereira.
  1483. *
  1484. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1485. * of this software and associated documentation files (the "Software"), to
  1486. * deal in the Software without restriction, including without limitation the
  1487. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1488. * sell copies of the Software, and to permit persons to whom the Software is
  1489. * furnished to do so, subject to the following conditions:
  1490. *
  1491. * The above copyright notice and this permission notice shall be included in
  1492. * all copies or substantial portions of the Software.
  1493. *
  1494. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1495. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1496. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1497. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1498. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1499. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1500. * IN THE SOFTWARE.
  1501. **/
  1502. /**
  1503. * @module Behavior3JS
  1504. **/
  1505. // namespace:
  1506. //this.b3 = this.b3 || {};
  1507. (function() {
  1508. "use strict";
  1509. /**
  1510. * The Sequence node ticks its children sequentially until one of them returns
  1511. * `FAILURE`, `RUNNING` or `ERROR`. If all children return the success state,
  1512. * the sequence also returns `SUCCESS`.
  1513. *
  1514. * @class Sequence
  1515. * @extends Composite
  1516. **/
  1517. var Sequence = b3.Class(b3.Composite);
  1518. var p = Sequence.prototype;
  1519. /**
  1520. * Node name. Default to `Sequence`.
  1521. *
  1522. * @property name
  1523. * @type {String}
  1524. * @readonly
  1525. **/
  1526. p.name = 'Sequence';
  1527. /**
  1528. * Tick method.
  1529. *
  1530. * @method tick
  1531. * @param {b3.Tick} tick A tick instance.
  1532. * @returns {Constant} A state constant.
  1533. **/
  1534. p.tick = function(tick) {
  1535. for (var i=0; i<this.children.length; i++) {
  1536. var status = this.children[i]._execute(tick);
  1537. if (status !== b3.SUCCESS) {
  1538. return status;
  1539. }
  1540. }
  1541. return b3.SUCCESS;
  1542. }
  1543. b3.Sequence = Sequence;
  1544. })();/**
  1545. * Priority
  1546. *
  1547. * Copyright (c) 2014 Renato de Pontes Pereira.
  1548. *
  1549. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1550. * of this software and associated documentation files (the "Software"), to
  1551. * deal in the Software without restriction, including without limitation the
  1552. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1553. * sell copies of the Software, and to permit persons to whom the Software is
  1554. * furnished to do so, subject to the following conditions:
  1555. *
  1556. * The above copyright notice and this permission notice shall be included in
  1557. * all copies or substantial portions of the Software.
  1558. *
  1559. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1560. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1561. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1562. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1563. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1564. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1565. * IN THE SOFTWARE.
  1566. **/
  1567. /**
  1568. * @module Behavior3JS
  1569. **/
  1570. // namespace:
  1571. //this.b3 = this.b3 || {};
  1572. (function() {
  1573. "use strict";
  1574. /**
  1575. * Priority ticks its children sequentially until one of them returns
  1576. * `SUCCESS`, `RUNNING` or `ERROR`. If all children return the failure state,
  1577. * the priority also returns `FAILURE`.
  1578. *
  1579. * @class Priority
  1580. * @extends Composite
  1581. **/
  1582. var Priority = b3.Class(b3.Composite);
  1583. var p = Priority.prototype
  1584. /**
  1585. * Node name. Default to `Priority`.
  1586. *
  1587. * @property name
  1588. * @type String
  1589. * @readonly
  1590. **/
  1591. p.name = 'Priority';
  1592. /**
  1593. * Tick method.
  1594. *
  1595. * @method tick
  1596. * @param {Tick} tick A tick instance.
  1597. * @returns {Constant} A state constant.
  1598. **/
  1599. p.tick = function(tick) {
  1600. for (var i=0; i<this.children.length; i++) {
  1601. var status = this.children[i]._execute(tick);
  1602. if (status !== b3.FAILURE) {
  1603. return status;
  1604. }
  1605. }
  1606. return b3.FAILURE;
  1607. }
  1608. b3.Priority = Priority;
  1609. })();/**
  1610. * MemSequence
  1611. *
  1612. * Copyright (c) 2014 Renato de Pontes Pereira.
  1613. *
  1614. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1615. * of this software and associated documentation files (the "Software"), to
  1616. * deal in the Software without restriction, including without limitation the
  1617. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1618. * sell copies of the Software, and to permit persons to whom the Software is
  1619. * furnished to do so, subject to the following conditions:
  1620. *
  1621. * The above copyright notice and this permission notice shall be included in
  1622. * all copies or substantial portions of the Software.
  1623. *
  1624. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1625. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1626. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1627. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1628. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1629. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1630. * IN THE SOFTWARE.
  1631. **/
  1632. /**
  1633. * @module Behavior3JS
  1634. **/
  1635. // namespace:
  1636. //this.b3 = this.b3 || {};
  1637. (function() {
  1638. "use strict";
  1639. /**
  1640. * MemSequence is similar to Sequence node, but when a child returns a
  1641. * `RUNNING` state, its index is recorded and in the next tick the MemPriority
  1642. * call the child recorded directly, without calling previous children again.
  1643. *
  1644. * @class MemPriority
  1645. * @extends Composite
  1646. **/
  1647. var MemSequence = b3.Class(b3.Composite);
  1648. var p = MemSequence.prototype
  1649. /**
  1650. * Node name. Default to `MemSequence`.
  1651. *
  1652. * @property name
  1653. * @type {String}
  1654. * @readonly
  1655. **/
  1656. p.name = 'MemSequence';
  1657. /**
  1658. * Open method.
  1659. *
  1660. * @method open
  1661. * @param {b3.Tick} tick A tick instance.
  1662. **/
  1663. p.open = function(tick) {
  1664. tick.blackboard.set('runningChild', 0, tick.tree.id, this.id);
  1665. }
  1666. /**
  1667. * Tick method.
  1668. *
  1669. * @method tick
  1670. * @param {b3.Tick} tick A tick instance.
  1671. * @returns {Constant} A state constant.
  1672. **/
  1673. p.tick = function(tick) {
  1674. var child = tick.blackboard.get('runningChild', tick.tree.id, this.id);
  1675. for (var i=child; i<this.children.length; i++) {
  1676. var status = this.children[i]._execute(tick);
  1677. if (status !== b3.SUCCESS) {
  1678. if (status === b3.RUNNING) {
  1679. tick.blackboard.set('runningChild', i, tick.tree.id, this.id);
  1680. }
  1681. return status;
  1682. }
  1683. }
  1684. return b3.SUCCESS;
  1685. }
  1686. b3.MemSequence = MemSequence;
  1687. })();/**
  1688. * MemPriority
  1689. *
  1690. * Copyright (c) 2014 Renato de Pontes Pereira.
  1691. *
  1692. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1693. * of this software and associated documentation files (the "Software"), to
  1694. * deal in the Software without restriction, including without limitation the
  1695. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1696. * sell copies of the Software, and to permit persons to whom the Software is
  1697. * furnished to do so, subject to the following conditions:
  1698. *
  1699. * The above copyright notice and this permission notice shall be included in
  1700. * all copies or substantial portions of the Software.
  1701. *
  1702. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1703. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1704. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1705. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1706. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1707. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1708. * IN THE SOFTWARE.
  1709. **/
  1710. /**
  1711. * @module Behavior3JS
  1712. **/
  1713. // namespace:
  1714. //this.b3 = this.b3 || {};
  1715. (function() {
  1716. "use strict";
  1717. /**
  1718. * MemPriority is similar to Priority node, but when a child returns a
  1719. * `RUNNING` state, its index is recorded and in the next tick the, MemPriority
  1720. * calls the child recorded directly, without calling previous children again.
  1721. *
  1722. * @class MemPriority
  1723. * @extends Composite
  1724. **/
  1725. var MemPriority = b3.Class(b3.Composite);
  1726. var p = MemPriority.prototype;
  1727. /**
  1728. * Node name. Default to `MemPriority`.
  1729. *
  1730. * @property name
  1731. * @type {String}
  1732. * @readonly
  1733. **/
  1734. p.name = 'MemPriority';
  1735. /**
  1736. * Open method.
  1737. *
  1738. * @method open
  1739. * @param {b3.Tick} tick A tick instance.
  1740. **/
  1741. p.open = function(tick) {
  1742. tick.blackboard.set('runningChild', 0, tick.tree.id, this.id);
  1743. }
  1744. /**
  1745. * Tick method.
  1746. *
  1747. * @method tick
  1748. * @param {b3.Tick} tick A tick instance.
  1749. * @returns {Constant} A state constant.
  1750. **/
  1751. p.tick = function(tick) {
  1752. var child = tick.blackboard.get('runningChild', tick.tree.id, this.id);
  1753. for (var i=child; i<this.children.length; i++) {
  1754. var status = this.children[i]._execute(tick);
  1755. if (status !== b3.FAILURE) {
  1756. if (status === b3.RUNNING) {
  1757. tick.blackboard.set('runningChild', i, tick.tree.id, this.id);
  1758. }
  1759. return status;
  1760. }
  1761. }
  1762. return b3.FAILURE;
  1763. }
  1764. b3.MemPriority = MemPriority;
  1765. })();/**
  1766. * Inverter
  1767. *
  1768. * Copyright (c) 2014 Renato de Pontes Pereira.
  1769. *
  1770. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1771. * of this software and associated documentation files (the "Software"), to
  1772. * deal in the Software without restriction, including without limitation the
  1773. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1774. * sell copies of the Software, and to permit persons to whom the Software is
  1775. * furnished to do so, subject to the following conditions:
  1776. *
  1777. * The above copyright notice and this permission notice shall be included in
  1778. * all copies or substantial portions of the Software.
  1779. *
  1780. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1781. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1782. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1783. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1784. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1785. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1786. * IN THE SOFTWARE.
  1787. **/
  1788. /**
  1789. * @module Behavior3JS
  1790. **/
  1791. // namespace:
  1792. //this.b3 = this.b3 || {};
  1793. (function() {
  1794. "use strict";
  1795. /**
  1796. * The Inverter decorator inverts the result of the child, returning `SUCCESS`
  1797. * for `FAILURE` and `FAILURE` for `SUCCESS`.
  1798. *
  1799. * @class Inverter
  1800. * @extends Decorator
  1801. **/
  1802. var Inverter = b3.Class(b3.Decorator);
  1803. var p = Inverter.prototype;
  1804. /**
  1805. * Node name. Default to `Inverter`.
  1806. *
  1807. * @property name
  1808. * @type {String}
  1809. * @readonly
  1810. **/
  1811. p.name = 'Inverter';
  1812. /**
  1813. * Tick method.
  1814. *
  1815. * @method tick
  1816. * @param {Tick} tick A tick instance.
  1817. * @returns {Constant} A state constant.
  1818. **/
  1819. p.tick = function(tick) {
  1820. if (!this.child) {
  1821. return b3.ERROR;
  1822. }
  1823. var status = this.child._execute(tick);
  1824. if (status == b3.SUCCESS)
  1825. status = b3.FAILURE;
  1826. else if (status == b3.FAILURE)
  1827. status = b3.SUCCESS;
  1828. return status;
  1829. };
  1830. b3.Inverter = Inverter;
  1831. })();/**
  1832. * Limiter
  1833. *
  1834. * Copyright (c) 2014 Renato de Pontes Pereira.
  1835. *
  1836. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1837. * of this software and associated documentation files (the "Software"), to
  1838. * deal in the Software without restriction, including without limitation the
  1839. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1840. * sell copies of the Software, and to permit persons to whom the Software is
  1841. * furnished to do so, subject to the following conditions:
  1842. *
  1843. * The above copyright notice and this permission notice shall be included in
  1844. * all copies or substantial portions of the Software.
  1845. *
  1846. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1847. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1848. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1849. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1850. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1851. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1852. * IN THE SOFTWARE.
  1853. **/
  1854. /**
  1855. * @module Behavior3JS
  1856. **/
  1857. // namespace:
  1858. //this.b3 = this.b3 || {};
  1859. (function() {
  1860. "use strict";
  1861. /**
  1862. * This decorator limit the number of times its child can be called. After a
  1863. * certain number of times, the Limiter decorator returns `FAILURE` without
  1864. * executing the child.
  1865. *
  1866. * @class Limiter
  1867. * @extends Decorator
  1868. **/
  1869. var Limiter = b3.Class(b3.Decorator);
  1870. var p = Limiter.prototype;
  1871. /**
  1872. * Node name. Default to `Limiter`.
  1873. *
  1874. * @property name
  1875. * @type {String}
  1876. * @readonly
  1877. **/
  1878. p.name = 'Limiter';
  1879. /**
  1880. * Node parameters.
  1881. *
  1882. * @property parameters
  1883. * @type {String}
  1884. * @readonly
  1885. **/
  1886. p.parameters = {'maxLoop': null};
  1887. p.__Decorator_initialize = p.initialize;
  1888. /**
  1889. * Initialization method.
  1890. *
  1891. * Settings parameters:
  1892. *
  1893. * - **maxLoop** (*Integer*) Maximum number of repetitions.
  1894. * - **child** (*BaseNode*) The child node.
  1895. *
  1896. * @method initialize
  1897. * @param {Object} settings Object with parameters.
  1898. * @constructor
  1899. **/
  1900. p.initialize = function(settings) {
  1901. settings = settings || {};
  1902. this.__Decorator_initialize(settings);
  1903. if (!settings.maxLoop) {
  1904. throw "maxLoop parameter in Limiter decorator is an obligatory " +
  1905. "parameter";
  1906. }
  1907. this.maxLoop = settings.maxLoop;
  1908. }
  1909. /**
  1910. * Open method.
  1911. *
  1912. * @method open
  1913. * @param {Tick} tick A tick instance.
  1914. **/
  1915. p.open = function(tick) {
  1916. //[20170319]# 修复limiter因为返回success或failure而重置i
  1917. var i = tick.blackboard.get('i',tick.tree.id, this.id);
  1918. if(!i){
  1919. tick.blackboard.set('i', 0, tick.tree.id, this.id);
  1920. }
  1921. }
  1922. /**
  1923. * Tick method.
  1924. *
  1925. * @method tick
  1926. * @param {Tick} tick A tick instance.
  1927. * @returns {Constant} A state constant.
  1928. **/
  1929. p.tick = function(tick) {
  1930. if (!this.child) {
  1931. return b3.ERROR;
  1932. }
  1933. var i = tick.blackboard.get('i', tick.tree.id, this.id);
  1934. if (i < this.maxLoop) {
  1935. var status = this.child._execute(tick);
  1936. if (status == b3.SUCCESS || status == b3.FAILURE)
  1937. tick.blackboard.set('i', i+1, tick.tree.id, this.id);
  1938. return status;
  1939. }
  1940. return b3.FAILURE;
  1941. }
  1942. b3.Limiter = Limiter;
  1943. })();/**
  1944. * MaxTime
  1945. *
  1946. * Copyright (c) 2014 Renato de Pontes Pereira.
  1947. *
  1948. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1949. * of this software and associated documentation files (the "Software"), to
  1950. * deal in the Software without restriction, including without limitation the
  1951. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1952. * sell copies of the Software, and to permit persons to whom the Software is
  1953. * furnished to do so, subject to the following conditions:
  1954. *
  1955. * The above copyright notice and this permission notice shall be included in
  1956. * all copies or substantial portions of the Software.
  1957. *
  1958. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1959. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1960. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1961. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1962. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1963. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1964. * IN THE SOFTWARE.
  1965. **/
  1966. /**
  1967. * @module Behavior3JS
  1968. **/
  1969. // namespace:
  1970. //this.b3 = this.b3 || {};
  1971. (function() {
  1972. "use strict";
  1973. /**
  1974. * The MaxTime decorator limits the maximum time the node child can execute.
  1975. * Notice that it does not interrupt the execution itself (i.e., the child must
  1976. * be non-preemptive), it only interrupts the node after a `RUNNING` status.
  1977. *
  1978. * @class MaxTime
  1979. * @extends Decorator
  1980. **/
  1981. var MaxTime = b3.Class(b3.Decorator);
  1982. var p = MaxTime.prototype;
  1983. /**
  1984. * Node name. Default to `MaxTime`.
  1985. *
  1986. * @property name
  1987. * @type {String}
  1988. * @readonly
  1989. **/
  1990. p.name = 'MaxTime';
  1991. /**
  1992. * Node title. Default to `Max Time`.
  1993. *
  1994. * @property title
  1995. * @type {String}
  1996. * @readonly
  1997. **/
  1998. p.title = 'Max Time';
  1999. /**
  2000. * Node parameters.
  2001. *
  2002. * @property parameters
  2003. * @type {String}
  2004. * @readonly
  2005. **/
  2006. p.parameters = {'maxTime': null};
  2007. p.__Decorator_initialize = p.initialize;
  2008. /**
  2009. * Initialization method.
  2010. *
  2011. * Settings parameters:
  2012. *
  2013. * - **maxTime** (*Integer*) Maximum time a child can execute.
  2014. * - **child** (*BaseNode*) The child node.
  2015. *
  2016. * @method initialize
  2017. * @param {Object} settings Object with parameters.
  2018. * @constructor
  2019. **/
  2020. p.initialize = function(settings) {
  2021. settings = settings || {};
  2022. this.__Decorator_initialize(settings);
  2023. if (!settings.maxTime) {
  2024. throw "maxTime parameter in MaxTime decorator is an obligatory " +
  2025. "parameter";
  2026. }
  2027. this.maxTime = settings.maxTime;
  2028. };
  2029. /**
  2030. * Open method.
  2031. *
  2032. * @method open
  2033. * @param {Tick} tick A tick instance.
  2034. **/
  2035. p.open = function(tick) {
  2036. var startTime = (new Date()).getTime();
  2037. tick.blackboard.set('startTime', startTime, tick.tree.id, this.id);
  2038. };
  2039. /**
  2040. * Tick method.
  2041. *
  2042. * @method tick
  2043. * @param {Tick} tick A tick instance.
  2044. * @returns {Constant} A state constant.
  2045. **/
  2046. p.tick = function(tick) {
  2047. if (!this.child) {
  2048. return b3.ERROR;
  2049. }
  2050. var currTime = (new Date()).getTime();
  2051. var startTime = tick.blackboard.get('startTime', tick.tree.id, this.id);
  2052. var status = this.child._execute(tick);
  2053. if (currTime - startTime > this.maxTime) {
  2054. return b3.FAILURE;
  2055. }
  2056. return status;
  2057. };
  2058. b3.MaxTime = MaxTime;
  2059. })();/**
  2060. * Repeater
  2061. *
  2062. * Copyright (c) 2014 Renato de Pontes Pereira.
  2063. *
  2064. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2065. * of this software and associated documentation files (the "Software"), to
  2066. * deal in the Software without restriction, including without limitation the
  2067. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2068. * sell copies of the Software, and to permit persons to whom the Software is
  2069. * furnished to do so, subject to the following conditions:
  2070. *
  2071. * The above copyright notice and this permission notice shall be included in
  2072. * all copies or substantial portions of the Software.
  2073. *
  2074. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2075. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2076. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2077. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2078. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2079. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2080. * IN THE SOFTWARE.
  2081. **/
  2082. /**
  2083. * @module Behavior3JS
  2084. **/
  2085. // namespace:
  2086. //this.b3 = this.b3 || {};
  2087. (function() {
  2088. "use strict";
  2089. /**
  2090. * Repeater is a decorator that repeats the tick signal until the child node
  2091. * return `RUNNING` or `ERROR`. Optionally, a maximum number of repetitions
  2092. * can be defined.
  2093. *
  2094. * @class Repeater
  2095. * @extends Decorator
  2096. **/
  2097. var Repeater = b3.Class(b3.Decorator);
  2098. var p = Repeater.prototype;
  2099. /**
  2100. * Node name. Default to `Repeater`.
  2101. *
  2102. * @property name
  2103. * @type {String}
  2104. * @readonly
  2105. **/
  2106. p.name = 'Repeater';
  2107. /**
  2108. * Node parameters.
  2109. *
  2110. * @property parameters
  2111. * @type {String}
  2112. * @readonly
  2113. **/
  2114. p.parameters = {'maxLoop': -1};
  2115. p.__Decorator_initialize = p.initialize;
  2116. /**
  2117. * Initialization method.
  2118. *
  2119. * Settings parameters:
  2120. *
  2121. * - **maxLoop** (*Integer*) Maximum number of repetitions. Default to -1
  2122. * (infinite).
  2123. * - **child** (*BaseNode*) The child node.
  2124. *
  2125. * @method initialize
  2126. * @param {Object} settings Object with parameters.
  2127. * @constructor
  2128. **/
  2129. p.initialize = function(settings) {
  2130. settings = settings || {};
  2131. this.__Decorator_initialize(settings);
  2132. this.maxLoop = settings.maxLoop || -1;
  2133. };
  2134. /**
  2135. * Open method.
  2136. *
  2137. * @method open
  2138. * @param {Tick} tick A tick instance.
  2139. **/
  2140. p.open = function(tick) {
  2141. tick.blackboard.set('i', 0, tick.tree.id, this.id);
  2142. };
  2143. /**
  2144. * Tick method.
  2145. *
  2146. * @method tick
  2147. * @param {Tick} tick A tick instance.
  2148. **/
  2149. p.tick = function(tick) {
  2150. if (!this.child) {
  2151. return b3.ERROR;
  2152. }
  2153. var i = tick.blackboard.get('i', tick.tree.id, this.id);
  2154. var status = b3.SUCCESS;
  2155. while (this.maxLoop < 0 || i < this.maxLoop) {
  2156. status = this.child._execute(tick);
  2157. if (status == b3.SUCCESS || status == b3.FAILURE)
  2158. i++;
  2159. else
  2160. break;
  2161. }
  2162. i = tick.blackboard.set('i', i, tick.tree.id, this.id);
  2163. return status;
  2164. };
  2165. b3.Repeater = Repeater;
  2166. })();/**
  2167. * RepeatUntilFailure
  2168. *
  2169. * Copyright (c) 2014 Renato de Pontes Pereira.
  2170. *
  2171. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2172. * of this software and associated documentation files (the "Software"), to
  2173. * deal in the Software without restriction, including without limitation the
  2174. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2175. * sell copies of the Software, and to permit persons to whom the Software is
  2176. * furnished to do so, subject to the following conditions:
  2177. *
  2178. * The above copyright notice and this permission notice shall be included in
  2179. * all copies or substantial portions of the Software.
  2180. *
  2181. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2182. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2183. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2184. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2185. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2186. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2187. * IN THE SOFTWARE.
  2188. **/
  2189. /**
  2190. * @module Behavior3JS
  2191. **/
  2192. // namespace:
  2193. //this.b3 = this.b3 || {};
  2194. (function() {
  2195. "use strict";
  2196. /**
  2197. * RepeatUntilFailure is a decorator that repeats the tick signal until the
  2198. * node child returns `FAILURE`, `RUNNING` or `ERROR`. Optionally, a maximum
  2199. * number of repetitions can be defined.
  2200. *
  2201. * @class RepeatUntilFailure
  2202. * @extends Decorator
  2203. **/
  2204. var RepeatUntilFailure = b3.Class(b3.Decorator);
  2205. var p = RepeatUntilFailure.prototype;
  2206. /**
  2207. * Node name. Default to `RepeatUntilFailure`.
  2208. *
  2209. * @property name
  2210. * @type {String}
  2211. * @readonly
  2212. **/
  2213. p.name = 'RepeatUntilFailure';
  2214. /**
  2215. * Node title. Default to `Repeat Until Failure`.
  2216. *
  2217. * @property title
  2218. * @type {String}
  2219. * @readonly
  2220. **/
  2221. p.title = 'Repeat Until Failure';
  2222. /**
  2223. * Node parameters.
  2224. *
  2225. * @property parameters
  2226. * @type {String}
  2227. * @readonly
  2228. **/
  2229. p.parameters = {'maxLoop': -1};
  2230. p.__Decorator_initialize = p.initialize;
  2231. /**
  2232. * Initialization method.
  2233. *
  2234. * Settings parameters:
  2235. *
  2236. * - **maxLoop** (*Integer*) Maximum number of repetitions. Default to -1
  2237. * (infinite).
  2238. * - **child** (*BaseNode*) The child node.
  2239. *
  2240. * @method initialize
  2241. * @param {Object} settings Object with parameters.
  2242. * @constructor
  2243. **/
  2244. p.initialize = function(settings) {
  2245. settings = settings || {};
  2246. this.__Decorator_initialize(settings);
  2247. this.maxLoop = settings.maxLoop || -1;
  2248. }
  2249. /**
  2250. * Open method.
  2251. *
  2252. * @method open
  2253. * @param {Tick} tick A tick instance.
  2254. **/
  2255. p.open = function(tick) {
  2256. tick.blackboard.set('i', 0, tick.tree.id, this.id);
  2257. }
  2258. /**
  2259. * Tick method.
  2260. *
  2261. * @method tick
  2262. * @param {Tick} tick A tick instance.
  2263. * @returns {Constant} A state constant.
  2264. **/
  2265. p.tick = function(tick) {
  2266. if (!this.child) {
  2267. return b3.ERROR;
  2268. }
  2269. var i = tick.blackboard.get('i', tick.tree.id, this.id);
  2270. while (this.maxLoop < 0 || i < this.maxLoop) {
  2271. var status = this.child._execute(tick);
  2272. if (status == b3.SUCCESS)
  2273. i++;
  2274. else
  2275. break;
  2276. }
  2277. var i = tick.blackboard.set('i', i, tick.tree.id, this.id);
  2278. return status;
  2279. }
  2280. b3.RepeatUntilFailure = RepeatUntilFailure;
  2281. })();/**
  2282. * RepeatUntilSuccess
  2283. *
  2284. * Copyright (c) 2014 Renato de Pontes Pereira.
  2285. *
  2286. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2287. * of this software and associated documentation files (the "Software"), to
  2288. * deal in the Software without restriction, including without limitation the
  2289. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2290. * sell copies of the Software, and to permit persons to whom the Software is
  2291. * furnished to do so, subject to the following conditions:
  2292. *
  2293. * The above copyright notice and this permission notice shall be included in
  2294. * all copies or substantial portions of the Software.
  2295. *
  2296. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2297. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2298. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2299. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2300. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2301. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2302. * IN THE SOFTWARE.
  2303. **/
  2304. /**
  2305. * @module Behavior3JS
  2306. **/
  2307. // namespace:
  2308. //this.b3 = this.b3 || {};
  2309. (function() {
  2310. "use strict";
  2311. /**
  2312. * RepeatUntilSuccess is a decorator that repeats the tick signal until the
  2313. * node child returns `SUCCESS`, `RUNNING` or `ERROR`. Optionally, a maximum
  2314. * number of repetitions can be defined.
  2315. *
  2316. * @class RepeatUntilSuccess
  2317. * @extends Decorator
  2318. **/
  2319. var RepeatUntilSuccess = b3.Class(b3.Decorator);
  2320. var p = RepeatUntilSuccess.prototype;
  2321. /**
  2322. * Node name. Default to `RepeatUntilSuccess`.
  2323. *
  2324. * @property name
  2325. * @type {String}
  2326. * @readonly
  2327. **/
  2328. p.name = 'RepeatUntilSuccess';
  2329. /**
  2330. * Node title. Default to `Repeat Until Success`.
  2331. *
  2332. * @property title
  2333. * @type {String}
  2334. * @readonly
  2335. **/
  2336. p.title = 'Repeat Until Success';
  2337. /**
  2338. * Node parameters.
  2339. *
  2340. * @property parameters
  2341. * @type {String}
  2342. * @readonly
  2343. **/
  2344. p.parameters = {'maxLoop': -1};
  2345. p.__Decorator_initialize = p.initialize;
  2346. /**
  2347. * Initialization method.
  2348. *
  2349. * Settings parameters:
  2350. *
  2351. * - **maxLoop** (*Integer*) Maximum number of repetitions. Default to -1
  2352. * (infinite).
  2353. * - **child** (*BaseNode*) The child node.
  2354. *
  2355. * @method initialize
  2356. * @param {Object} settings Object with parameters.
  2357. * @constructor
  2358. **/
  2359. p.initialize = function(settings) {
  2360. settings = settings || {};
  2361. this.__Decorator_initialize(settings);
  2362. this.maxLoop = settings.maxLoop || -1;
  2363. }
  2364. /**
  2365. * Open method.
  2366. *
  2367. * @method open
  2368. * @param {Tick} tick A tick instance.
  2369. **/
  2370. p.open = function(tick) {
  2371. tick.blackboard.set('i', 0, tick.tree.id, this.id);
  2372. }
  2373. /**
  2374. * Tick method.
  2375. *
  2376. * @method tick
  2377. * @param {Tick} tick A tick instance.
  2378. * @returns {Constant} A state constant.
  2379. **/
  2380. p.tick = function(tick) {
  2381. if (!this.child) {
  2382. return b3.ERROR;
  2383. }
  2384. var i = tick.blackboard.get('i', tick.tree.id, this.id);
  2385. while (this.maxLoop < 0 || i < this.maxLoop) {
  2386. var status = this.child._execute(tick);
  2387. if (status == b3.FAILURE)
  2388. i++;
  2389. else
  2390. break;
  2391. }
  2392. var i = tick.blackboard.set('i', i, tick.tree.id, this.id);
  2393. return status;
  2394. }
  2395. b3.RepeatUntilSuccess = RepeatUntilSuccess;
  2396. })();/**
  2397. * Error
  2398. *
  2399. * Copyright (c) 2014 Renato de Pontes Pereira.
  2400. *
  2401. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2402. * of this software and associated documentation files (the "Software"), to
  2403. * deal in the Software without restriction, including without limitation the
  2404. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2405. * sell copies of the Software, and to permit persons to whom the Software is
  2406. * furnished to do so, subject to the following conditions:
  2407. *
  2408. * The above copyright notice and this permission notice shall be included in
  2409. * all copies or substantial portions of the Software.
  2410. *
  2411. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2412. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2413. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2414. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2415. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2416. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2417. * IN THE SOFTWARE.
  2418. **/
  2419. /**
  2420. * @module Behavior3JS
  2421. **/
  2422. // namespace:
  2423. //this.b3 = this.b3 || {};
  2424. (function() {
  2425. "use strict";
  2426. /**
  2427. * This action node returns `ERROR` always.
  2428. *
  2429. * @class Error
  2430. * @extends Action
  2431. **/
  2432. var Error = b3.Class(b3.Action);
  2433. var p = Error.prototype;
  2434. /**
  2435. * Node name. Default to `Error`.
  2436. *
  2437. * @property name
  2438. * @type {String}
  2439. * @readonly
  2440. **/
  2441. p.name = 'Error';
  2442. /**
  2443. * Tick method.
  2444. *
  2445. * @method tick
  2446. * @param {b3.Tick} tick A tick instance.
  2447. * @returns {Constant} Always return `b3.ERROR`.
  2448. **/
  2449. p.tick = function(tick) {
  2450. return b3.ERROR;
  2451. }
  2452. b3.Error = Error;
  2453. })();/**
  2454. * Failer
  2455. *
  2456. * Copyright (c) 2014 Renato de Pontes Pereira.
  2457. *
  2458. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2459. * of this software and associated documentation files (the "Software"), to
  2460. * deal in the Software without restriction, including without limitation the
  2461. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2462. * sell copies of the Software, and to permit persons to whom the Software is
  2463. * furnished to do so, subject to the following conditions:
  2464. *
  2465. * The above copyright notice and this permission notice shall be included in
  2466. * all copies or substantial portions of the Software.
  2467. *
  2468. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2469. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2470. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2471. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2472. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2473. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2474. * IN THE SOFTWARE.
  2475. **/
  2476. /**
  2477. * @module Behavior3JS
  2478. **/
  2479. // namespace:
  2480. //this.b3 = this.b3 || {};
  2481. (function() {
  2482. "use strict";
  2483. /**
  2484. * This action node returns `FAILURE` always.
  2485. *
  2486. * @class Failer
  2487. * @extends Action
  2488. **/
  2489. var Failer = b3.Class(b3.Action);
  2490. var p = Failer.prototype;
  2491. /**
  2492. * Node name. Default to `Failer`.
  2493. *
  2494. * @property name
  2495. * @type {String}
  2496. * @readonly
  2497. **/
  2498. p.name = 'Failer';
  2499. /**
  2500. * Tick method.
  2501. *
  2502. * @method tick
  2503. * @param {b3.Tick} tick A tick instance.
  2504. * @returns {Constant} Always return `b3.FAILURE`.
  2505. **/
  2506. p.tick = function(tick) {
  2507. return b3.FAILURE;
  2508. }
  2509. b3.Failer = Failer;
  2510. })();/**
  2511. * Runner
  2512. *
  2513. * Copyright (c) 2014 Renato de Pontes Pereira.
  2514. *
  2515. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2516. * of this software and associated documentation files (the "Software"), to
  2517. * deal in the Software without restriction, including without limitation the
  2518. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2519. * sell copies of the Software, and to permit persons to whom the Software is
  2520. * furnished to do so, subject to the following conditions:
  2521. *
  2522. * The above copyright notice and this permission notice shall be included in
  2523. * all copies or substantial portions of the Software.
  2524. *
  2525. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2526. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2527. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2528. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2529. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2530. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2531. * IN THE SOFTWARE.
  2532. **/
  2533. /**
  2534. * @module Behavior3JS
  2535. **/
  2536. // namespace:
  2537. //this.b3 = this.b3 || {};
  2538. (function() {
  2539. "use strict";
  2540. /**
  2541. * This action node returns RUNNING always.
  2542. *
  2543. * @class Runner
  2544. * @extends Action
  2545. **/
  2546. var Runner = b3.Class(b3.Action);
  2547. var p = Runner.prototype;
  2548. /**
  2549. * Node name. Default to `Runner`.
  2550. *
  2551. * @property name
  2552. * @type {String}
  2553. * @readonly
  2554. **/
  2555. p.name = 'Runner';
  2556. /**
  2557. * Tick method.
  2558. *
  2559. * @method tick
  2560. * @param {b3.Tick} tick A tick instance.
  2561. * @returns {Constant} Always return `b3.RUNNING`.
  2562. **/
  2563. p.tick = function(tick) {
  2564. return b3.RUNNING;
  2565. }
  2566. b3.Runner = Runner;
  2567. })();/**
  2568. * Succeeder
  2569. *
  2570. * Copyright (c) 2014 Renato de Pontes Pereira.
  2571. *
  2572. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2573. * of this software and associated documentation files (the "Software"), to
  2574. * deal in the Software without restriction, including without limitation the
  2575. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2576. * sell copies of the Software, and to permit persons to whom the Software is
  2577. * furnished to do so, subject to the following conditions:
  2578. *
  2579. * The above copyright notice and this permission notice shall be included in
  2580. * all copies or substantial portions of the Software.
  2581. *
  2582. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2583. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2584. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2585. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2586. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2587. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2588. * IN THE SOFTWARE.
  2589. **/
  2590. /**
  2591. * @module Behavior3JS
  2592. **/
  2593. // namespace:
  2594. //this.b3 = this.b3 || {};
  2595. (function() {
  2596. "use strict";
  2597. /**
  2598. * This action node returns `SUCCESS` always.
  2599. *
  2600. * @class Succeeder
  2601. * @extends Action
  2602. **/
  2603. var Succeeder = b3.Class(b3.Action);
  2604. var p = Succeeder.prototype;
  2605. /**
  2606. * Node name. Default to `Succeeder`.
  2607. *
  2608. * @property name
  2609. * @type String
  2610. * @readonly
  2611. **/
  2612. p.name = 'Succeeder';
  2613. /**
  2614. * Tick method.
  2615. *
  2616. * @method tick
  2617. * @param {b3.Tick} tick A tick instance.
  2618. * @returns {Constant} Always return `b3.SUCCESS`.
  2619. **/
  2620. p.tick = function(tick) {
  2621. return b3.SUCCESS;
  2622. }
  2623. b3.Succeeder = Succeeder;
  2624. })();/**
  2625. * Wait
  2626. *
  2627. * Copyright (c) 2014 Renato de Pontes Pereira.
  2628. *
  2629. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2630. * of this software and associated documentation files (the "Software"), to
  2631. * deal in the Software without restriction, including without limitation the
  2632. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2633. * sell copies of the Software, and to permit persons to whom the Software is
  2634. * furnished to do so, subject to the following conditions:
  2635. *
  2636. * The above copyright notice and this permission notice shall be included in
  2637. * all copies or substantial portions of the Software.
  2638. *
  2639. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2640. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2641. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2642. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2643. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2644. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2645. * IN THE SOFTWARE.
  2646. **/
  2647. /**
  2648. * @module Behavior3JS
  2649. **/
  2650. // namespace:
  2651. //this.b3 = this.b3 || {};
  2652. (function() {
  2653. "use strict";
  2654. /**
  2655. * Wait a few seconds.
  2656. *
  2657. * @class Wait
  2658. * @extends Action
  2659. **/
  2660. var Wait = b3.Class(b3.Action);
  2661. var p = Wait.prototype;
  2662. /**
  2663. * Node name. Default to `Wait`.
  2664. *
  2665. * @property name
  2666. * @type {String}
  2667. * @readonly
  2668. **/
  2669. p.name = 'Wait';
  2670. /**
  2671. * Node parameters.
  2672. *
  2673. * @property parameters
  2674. * @type {String}
  2675. * @readonly
  2676. **/
  2677. p.parameters = {'milliseconds': 0};
  2678. p.__Action_initialize = p.initialize;
  2679. /**
  2680. * Initialization method.
  2681. *
  2682. * Settings parameters:
  2683. *
  2684. * - **milliseconds** (*Integer*) Maximum time, in milliseconds, a child
  2685. * can execute.
  2686. *
  2687. * @method initialize
  2688. * @param {Object} settings Object with parameters.
  2689. * @constructor
  2690. **/
  2691. p.initialize = function(settings) {
  2692. settings = settings || {};
  2693. this.__Action_initialize();
  2694. this.endTime = settings.milliseconds || 0;
  2695. }
  2696. /**
  2697. * Open method.
  2698. *
  2699. * @method open
  2700. * @param {Tick} tick A tick instance.
  2701. **/
  2702. p.open = function(tick) {
  2703. var startTime = (new Date()).getTime();
  2704. tick.blackboard.set('startTime', startTime, tick.tree.id, this.id);
  2705. }
  2706. /**
  2707. * Tick method.
  2708. *
  2709. * @method tick
  2710. * @param {Tick} tick A tick instance.
  2711. * @returns {Constant} A state constant.
  2712. **/
  2713. p.tick = function(tick) {
  2714. var currTime = (new Date()).getTime();
  2715. var startTime = tick.blackboard.get('startTime', tick.tree.id, this.id);
  2716. if (currTime - startTime > this.endTime) {
  2717. return b3.SUCCESS;
  2718. }
  2719. return b3.RUNNING;
  2720. }
  2721. b3.Wait = Wait;
  2722. })();
  2723. module.exports = b3;