behaviorComponent.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "io/stream.h"
  23. #include "string/stringUnit.h"
  24. #include "memory/frameAllocator.h"
  25. #include "component/behaviors/behaviorComponent.h"
  26. #include "component/behaviors/behaviorTemplate.h"
  27. #ifndef _ASSET_FIELD_TYPES_H_
  28. #include "assets/assetFieldTypes.h"
  29. #endif
  30. #ifndef _TAML_CUSTOM_H_
  31. #include "persistence/taml/tamlCustom.h"
  32. #endif
  33. // Script bindings.
  34. #include "behaviorComponent_ScriptBinding.h"
  35. //-----------------------------------------------------------------------------
  36. #define BEHAVIOR_FIELDNAME "Behavior"
  37. #define BEHAVIOR_CONNECTION_FIELDNAME "BehaviorConnection"
  38. //-----------------------------------------------------------------------------
  39. IMPLEMENT_CONOBJECT( BehaviorComponent );
  40. //-----------------------------------------------------------------------------
  41. BehaviorComponent::BehaviorComponent() :
  42. mMasterBehaviorId( 1 ),
  43. mpBehaviorFieldNames( NULL )
  44. {
  45. SIMSET_SET_ASSOCIATION( mBehaviors );
  46. }
  47. //-----------------------------------------------------------------------------
  48. bool BehaviorComponent::onAdd()
  49. {
  50. if( !Parent::onAdd() )
  51. return false;
  52. return true;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void BehaviorComponent::onRemove()
  56. {
  57. // Remove all behaviors and notify.
  58. clearBehaviors();
  59. // Call parent.
  60. Parent::onRemove();
  61. }
  62. //-----------------------------------------------------------------------------
  63. void BehaviorComponent::onDeleteNotify( SimObject *object )
  64. {
  65. // Cast to a behavior instance.
  66. BehaviorInstance* pInstance = dynamic_cast<BehaviorInstance*>( object );
  67. // Ignore if not appropriate.
  68. if ( pInstance == NULL )
  69. return;
  70. // Is the behavior instance owned by this component?
  71. if ( pInstance->getBehaviorOwner() == this )
  72. {
  73. // Yes, so remove.
  74. removeBehavior( pInstance, false );
  75. }
  76. // Destroy any input connections to the instance.
  77. destroyBehaviorInputConnections( pInstance );
  78. }
  79. //-----------------------------------------------------------------------------
  80. void BehaviorComponent::copyTo(SimObject* obj)
  81. {
  82. // Call parent.
  83. Parent::copyTo(obj);
  84. // Fetch object.
  85. BehaviorComponent* pObject = dynamic_cast<BehaviorComponent*>(obj);
  86. // Sanity!
  87. AssertFatal(pObject != NULL, "BehaviorComponent::copyTo() - Object is not the correct type.");
  88. // Clear behaviors.
  89. pObject->clearBehaviors();
  90. // Behaviors
  91. U32 behaviorCount = getBehaviorCount();
  92. // Finish if no behaviors.
  93. if ( behaviorCount == 0 )
  94. return;
  95. // Initialize a clone map.
  96. typedef HashMap<BehaviorInstance*,BehaviorInstance*> typeBehaviorCloneHash;
  97. typeBehaviorCloneHash behaviorInstanceCloneMap;
  98. // Iterate behaviors.
  99. for ( U32 index = 0; index < behaviorCount; ++index )
  100. {
  101. // Clone the behavior instance.
  102. BehaviorInstance* pFromInstance = getBehavior(index);
  103. BehaviorTemplate* pFromTemplate = pFromInstance->getTemplate();
  104. BehaviorInstance* pToInstance = pFromTemplate->createInstance();
  105. // Assign dynamic fields from behavior instance.
  106. pToInstance->assignDynamicFieldsFrom( pFromInstance );
  107. // Add the behavior instance.
  108. pObject->addBehavior(pToInstance);
  109. // Add to the clone map.
  110. behaviorInstanceCloneMap.insert( pFromInstance, pToInstance );
  111. }
  112. // Iterate instance connections.
  113. for( typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.begin(); instanceItr != mBehaviorConnections.end(); ++instanceItr )
  114. {
  115. // Fetch output name connection(s).
  116. typeOutputNameConnectionHash* pOutputNameConnection = instanceItr->value;
  117. // Iterate output name connections.
  118. for( typeOutputNameConnectionHash::iterator outputItr = pOutputNameConnection->begin(); outputItr != pOutputNameConnection->end(); ++outputItr )
  119. {
  120. // Fetch port connection(s).
  121. typePortConnectionVector* pPortConnections = outputItr->value;
  122. // Iterate input connections.
  123. for( typePortConnectionVector::iterator connectionItr = pPortConnections->begin(); connectionItr != pPortConnections->end(); ++connectionItr )
  124. {
  125. // Fetch connection.
  126. BehaviorPortConnection* pConnection = connectionItr;
  127. // Find behavior instance mappings.
  128. typeBehaviorCloneHash::iterator toOutputItr = behaviorInstanceCloneMap.find( pConnection->mOutputInstance );
  129. typeBehaviorCloneHash::iterator toInputItr = behaviorInstanceCloneMap.find( pConnection->mInputInstance );
  130. // Sanity!
  131. AssertFatal( toOutputItr != behaviorInstanceCloneMap.end(), "Failed to find output behavior instance mapping during copy." );
  132. AssertFatal( toInputItr != behaviorInstanceCloneMap.end(), "Failed to find input behavior instance mapping during copy." );
  133. // Fetch behavior instance mappings.
  134. BehaviorInstance* pToInstanceOutput = toOutputItr->value;
  135. BehaviorInstance* pToInstanceInput = toInputItr->value;
  136. // Make cloned connection.
  137. pObject->connect( pToInstanceOutput, pToInstanceInput, pConnection->mOutputName, pConnection->mInputName );
  138. }
  139. }
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. bool BehaviorComponent::addBehavior( BehaviorInstance* bi )
  144. {
  145. if( bi == NULL || !bi->isProperlyAdded() )
  146. return false;
  147. // Store behavior.
  148. mBehaviors.pushObject( bi );
  149. // Notify if the behavior instance is destroyed.
  150. deleteNotify( bi );
  151. // Set the behavior owner.
  152. bi->setBehaviorOwner( this );
  153. // Allocate a behavior Id.
  154. bi->setBehaviorId( mMasterBehaviorId++ );
  155. if( bi->isMethod("onBehaviorAdd") )
  156. Con::executef( bi , 1, "onBehaviorAdd" );
  157. return true;
  158. }
  159. //-----------------------------------------------------------------------------
  160. bool BehaviorComponent::removeBehavior( BehaviorInstance *bi, bool deleteBehavior )
  161. {
  162. for( SimSet::iterator itr = mBehaviors.begin(); itr != mBehaviors.end(); ++itr )
  163. {
  164. if( *itr == bi )
  165. {
  166. mBehaviors.removeObject( *itr );
  167. // Perform callback if allowed.
  168. if( bi->isProperlyAdded() && bi->isMethod("onBehaviorRemove") )
  169. Con::executef( bi , 1, "onBehaviorRemove" );
  170. // Destroy any output connections.
  171. destroyBehaviorOutputConnections( bi );
  172. if ( deleteBehavior && bi->isProperlyAdded() )
  173. {
  174. bi->deleteObject();
  175. }
  176. else
  177. {
  178. bi->setBehaviorOwner( NULL );
  179. bi->setBehaviorId( 0 );
  180. // Remove delete notification.
  181. clearNotify( bi );
  182. }
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. //-----------------------------------------------------------------------------
  189. void BehaviorComponent::clearBehaviors()
  190. {
  191. while( mBehaviors.size() > 0 )
  192. {
  193. BehaviorInstance *bi = dynamic_cast<BehaviorInstance *>( mBehaviors.first() );
  194. removeBehavior( bi );
  195. }
  196. }
  197. //-----------------------------------------------------------------------------
  198. BehaviorInstance *BehaviorComponent::getBehavior( StringTableEntry behaviorTemplateName )
  199. {
  200. for( SimSet::iterator itr = mBehaviors.begin(); itr != mBehaviors.end(); ++itr )
  201. {
  202. // Fetch behavior.
  203. BehaviorInstance* pBehaviorInstance = dynamic_cast<BehaviorInstance*>(*itr);
  204. if ( !pBehaviorInstance || pBehaviorInstance->getTemplateName() != behaviorTemplateName )
  205. continue;
  206. return pBehaviorInstance;
  207. }
  208. return NULL;
  209. }
  210. //-----------------------------------------------------------------------------
  211. bool BehaviorComponent::reOrder( BehaviorInstance *obj, U32 desiredIndex /* = 0 */ )
  212. {
  213. if( desiredIndex > (U32)mBehaviors.size() )
  214. return false;
  215. SimObject *target = mBehaviors.at( desiredIndex );
  216. return mBehaviors.reOrder( obj, target );
  217. }
  218. //-----------------------------------------------------------------------------
  219. void BehaviorComponent::destroyBehaviorOutputConnections( BehaviorInstance* pOutputBehavior )
  220. {
  221. // Sanity!
  222. AssertFatal( pOutputBehavior != NULL, "Output behavior cannot be NULL." );
  223. // Is the output behavior owned by this behavior component?
  224. if ( pOutputBehavior->getBehaviorOwner() != this )
  225. {
  226. // No, so warn.
  227. Con::warnf(
  228. "Could not destroy output behavior connection for behavior '%s' as the output behavior is not owned by this component.",
  229. pOutputBehavior->getTemplateName()
  230. );
  231. return;
  232. }
  233. // Find behavior instance connections.
  234. typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.find( pOutputBehavior->getId() );
  235. // Finish if there are no outbound connections for this output behavior.
  236. if ( instanceItr == mBehaviorConnections.end() )
  237. return;
  238. // Fetch output name hash.
  239. typeOutputNameConnectionHash* pOutputNameHash = instanceItr->value;
  240. // Iterate all outputs.
  241. for ( typeOutputNameConnectionHash::iterator outputItr = pOutputNameHash->begin(); outputItr != pOutputNameHash->end(); ++outputItr )
  242. {
  243. // Fetch port connection(s).
  244. typePortConnectionVector* pPortConnections = outputItr->value;
  245. // Destroy port connections.
  246. delete pPortConnections;
  247. }
  248. // Destroy outputs.
  249. delete pOutputNameHash;
  250. // Remove connection.
  251. mBehaviorConnections.erase( pOutputBehavior->getId() );
  252. }
  253. //-----------------------------------------------------------------------------
  254. void BehaviorComponent::destroyBehaviorInputConnections( BehaviorInstance* pInputBehavior )
  255. {
  256. // Sanity!
  257. AssertFatal( pInputBehavior != NULL, "Input behavior cannot be NULL." );
  258. // Iterate connections.
  259. for ( typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.begin(); instanceItr != mBehaviorConnections.end(); ++instanceItr )
  260. {
  261. // Fetch output name hash.
  262. typeOutputNameConnectionHash* pOutputNameHash = instanceItr->value;
  263. // Iterate all outputs.
  264. for ( typeOutputNameConnectionHash::iterator outputItr = pOutputNameHash->begin(); outputItr != pOutputNameHash->end(); ++outputItr )
  265. {
  266. // Fetch port connection(s).
  267. typePortConnectionVector* pPortConnections = outputItr->value;
  268. bool connectionFound;
  269. do
  270. {
  271. // Flag connection as 'not found' initially.
  272. connectionFound = false;
  273. // Look for an existing connection to the specified input instance.
  274. for ( typePortConnectionVector::iterator connectionItr = pPortConnections->begin(); connectionItr != pPortConnections->end(); ++connectionItr )
  275. {
  276. // Is this the input behavior?
  277. if ( connectionItr->mInputInstance == pInputBehavior )
  278. {
  279. // Yes, so destroy it.
  280. pPortConnections->erase_fast( connectionItr );
  281. // Flag connection as 'found'.
  282. connectionFound = true;
  283. break;
  284. }
  285. }
  286. } while (connectionFound);
  287. }
  288. }
  289. }
  290. //-----------------------------------------------------------------------------
  291. BehaviorInstance* BehaviorComponent::getBehaviorByInstanceId( const U32 behaviorId )
  292. {
  293. for( SimSet::iterator instanceItr = mBehaviors.begin(); instanceItr != mBehaviors.end(); ++instanceItr )
  294. {
  295. // Fetch behavior instance.
  296. BehaviorInstance* pInstance = static_cast<BehaviorInstance*>( *instanceItr );
  297. // Return instance if it has the same behavior Id.
  298. if ( pInstance->getBehaviorId() == behaviorId )
  299. return pInstance;
  300. }
  301. // Not found.
  302. return NULL;
  303. }
  304. //-----------------------------------------------------------------------------
  305. bool BehaviorComponent::connect( BehaviorInstance* pOutputBehavior, BehaviorInstance* pInputBehavior, StringTableEntry pOutputName, StringTableEntry pInputName )
  306. {
  307. // Sanity!
  308. AssertFatal( pOutputBehavior != NULL, "Output behavior cannot be NULL." );
  309. AssertFatal( pInputBehavior != NULL, "Input behavior cannot be NULL." );
  310. AssertFatal( pOutputName != NULL, "Output name cannot be NULL." );
  311. AssertFatal( pInputName != NULL, "Input name cannot be NULL." );
  312. // Is the output behavior owned by this behavior component?
  313. if ( pOutputBehavior->getBehaviorOwner() != this )
  314. {
  315. // No, so warn.
  316. Con::warnf(
  317. "Could not connect output '%s' on behavior '%s' to input '%s' on behavior '%s' as the output behavior is not owned by this component.",
  318. pOutputName,
  319. pOutputBehavior->getTemplateName(),
  320. pInputName,
  321. pInputBehavior->getTemplateName()
  322. );
  323. return false;
  324. }
  325. // Is the input behavior owned by this behavior component?
  326. if ( pInputBehavior->getBehaviorOwner() != this )
  327. {
  328. // No, so warn.
  329. Con::warnf(
  330. "Could not connect output '%s' on behavior '%s' to input '%s' on behavior '%s' as the input behavior is not owned by this component.",
  331. pOutputName,
  332. pOutputBehavior->getTemplateName(),
  333. pInputName,
  334. pInputBehavior->getTemplateName()
  335. );
  336. return false;
  337. }
  338. // Does the output behavior have the specified output?
  339. if ( !pOutputBehavior->getTemplate()->hasBehaviorOutput( pOutputName ) )
  340. {
  341. // No, so warn.
  342. Con::warnf(
  343. "Could not connect output '%s' on behavior '%s' to input '%s' on behavior '%s' as the output behavior does not have such an output.",
  344. pOutputName,
  345. pOutputBehavior->getTemplateName(),
  346. pInputName,
  347. pInputBehavior->getTemplateName()
  348. );
  349. return false;
  350. }
  351. // Does the input behavior have the specified input?
  352. if ( !pInputBehavior->getTemplate()->hasBehaviorInput( pInputName ) )
  353. {
  354. // No, so warn.
  355. Con::warnf(
  356. "Could not connect output '%s' on behavior '%s' to input '%s' on behavior '%s' as the input behavior does not have such an input.",
  357. pOutputName,
  358. pOutputBehavior->getTemplateName(),
  359. pInputName,
  360. pInputBehavior->getTemplateName()
  361. );
  362. return false;
  363. }
  364. // Find behavior instance connections.
  365. typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.find( pOutputBehavior->getId() );
  366. // Are there currently any outbound connections for this output instance?
  367. if ( instanceItr == mBehaviorConnections.end() )
  368. {
  369. // No, so create an entry for this instance.
  370. typeOutputNameConnectionHash* pOutputHash = new typeOutputNameConnectionHash();
  371. // Insert new output hash.
  372. instanceItr = mBehaviorConnections.insert( pOutputBehavior->getId(), pOutputHash );
  373. }
  374. // Fetch output name hash.
  375. typeOutputNameConnectionHash* pOutputNameHash = instanceItr->value;
  376. // Find instance output connection.
  377. typeOutputNameConnectionHash::iterator outputItr = pOutputNameHash->find( pOutputName );
  378. // Are there currently any outbound connections for this specific output?
  379. if ( outputItr == pOutputNameHash->end() )
  380. {
  381. // No, so create an entry for this output.
  382. typePortConnectionVector* pPortConnections = new typePortConnectionVector();
  383. // Insert new port connections.
  384. outputItr = pOutputNameHash->insert( pOutputName, pPortConnections );
  385. }
  386. // Fetch port connection(s).
  387. typePortConnectionVector* pPortConnections = outputItr->value;
  388. // Look for an identical connection.
  389. for ( typePortConnectionVector::iterator connectionItr = pPortConnections->begin(); connectionItr != pPortConnections->end(); ++connectionItr )
  390. {
  391. // Is this an identical connection?
  392. if ( connectionItr->mInputInstance == pInputBehavior && connectionItr->mInputName == pInputName )
  393. {
  394. // Yes, so warn.
  395. Con::warnf(
  396. "Could not connect output '%s' on behavior '%s' to input '%s' on behavior '%s' as the connection already exists.",
  397. pOutputName,
  398. pOutputBehavior->getTemplateName(),
  399. pInputName,
  400. pInputBehavior->getTemplateName()
  401. );
  402. return false;
  403. }
  404. }
  405. // Populate port connection.
  406. BehaviorPortConnection portConnection( pOutputBehavior, pInputBehavior, pOutputName, pInputName );
  407. // Add port connection.
  408. pPortConnections->push_back( portConnection );
  409. // Notify if the input behavior instance is destroyed.
  410. deleteNotify( pInputBehavior );
  411. return true;
  412. }
  413. //-----------------------------------------------------------------------------
  414. bool BehaviorComponent::disconnect( BehaviorInstance* pOutputBehavior, BehaviorInstance* pInputBehavior, StringTableEntry pOutputName, StringTableEntry pInputName )
  415. {
  416. // Sanity!
  417. AssertFatal( pOutputBehavior != NULL, "Output behavior cannot be NULL." );
  418. AssertFatal( pInputBehavior != NULL, "Input behavior cannot be NULL." );
  419. AssertFatal( pOutputName != NULL, "Output name cannot be NULL." );
  420. AssertFatal( pInputName != NULL, "Input name cannot be NULL." );
  421. // Is the output behavior owned by this behavior component?
  422. if ( pOutputBehavior->getBehaviorOwner() != this )
  423. {
  424. // No, so warn.
  425. Con::warnf(
  426. "Could not disconnect output '%s' on behavior '%s' from input '%s' on behavior '%s' as the output behavior is not owned by this component.",
  427. pOutputName,
  428. pOutputBehavior->getTemplateName(),
  429. pInputName,
  430. pInputBehavior->getTemplateName()
  431. );
  432. return false;
  433. }
  434. // Is the input behavior owned by this behavior component?
  435. if ( pInputBehavior->getBehaviorOwner() != this )
  436. {
  437. // No, so warn.
  438. Con::warnf(
  439. "Could not disconnect output '%s' on behavior '%s' from input '%s' on behavior '%s' as the input behavior is not owned by this component.",
  440. pOutputName,
  441. pOutputBehavior->getTemplateName(),
  442. pInputName,
  443. pInputBehavior->getTemplateName()
  444. );
  445. return false;
  446. }
  447. // Does the output behavior have the specified output?
  448. if ( !pOutputBehavior->getTemplate()->hasBehaviorOutput( pOutputName ) )
  449. {
  450. // No, so warn.
  451. Con::warnf(
  452. "Could not disconnect output '%s' on behavior '%s' to input '%s' on behavior '%s' as the output behavior does not have such an output.",
  453. pOutputName,
  454. pOutputBehavior->getTemplateName(),
  455. pInputName,
  456. pInputBehavior->getTemplateName()
  457. );
  458. return false;
  459. }
  460. // Does the input behavior have the specified input?
  461. if ( !pInputBehavior->getTemplate()->hasBehaviorInput( pInputName ) )
  462. {
  463. // No, so warn.
  464. Con::warnf(
  465. "Could not disconnect output '%s' on behavior '%s' to input '%s' on behavior '%s' as the input behavior does not have such an input.",
  466. pOutputName,
  467. pOutputBehavior->getTemplateName(),
  468. pInputName,
  469. pInputBehavior->getTemplateName()
  470. );
  471. return false;
  472. }
  473. // Find behavior instance connections.
  474. typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.find( pOutputBehavior->getId() );
  475. // Are there currently any outbound connections for this output instance?
  476. if ( instanceItr == mBehaviorConnections.end() )
  477. {
  478. // No, so warn.
  479. Con::warnf(
  480. "Could not disconnect output '%s' on behavior '%s' from input '%s' on behavior '%s' as the behavior does not have any connections.",
  481. pOutputName,
  482. pOutputBehavior->getTemplateName(),
  483. pInputName,
  484. pInputBehavior->getTemplateName()
  485. );
  486. return false;
  487. }
  488. // Fetch output name hash.
  489. typeOutputNameConnectionHash* pOutputNameHash = instanceItr->value;
  490. // Find instance output connection.
  491. typeOutputNameConnectionHash::iterator outputItr = pOutputNameHash->find( pOutputName );
  492. // Are there currently any outbound connections for this specific output?
  493. if ( outputItr == pOutputNameHash->end() )
  494. {
  495. // No, so warn.
  496. Con::warnf(
  497. "Could not disconnect output '%s' on behavior '%s' from input '%s' on behavior '%s' as the specified output does not have any connections.",
  498. pOutputName,
  499. pOutputBehavior->getTemplateName(),
  500. pInputName,
  501. pInputBehavior->getTemplateName()
  502. );
  503. return false;
  504. }
  505. // Fetch port connection(s).
  506. typePortConnectionVector* pPortConnections = outputItr->value;
  507. // Look for an existing connection to the specified input.
  508. for ( typePortConnectionVector::iterator connectionItr = pPortConnections->begin(); connectionItr != pPortConnections->end(); ++connectionItr )
  509. {
  510. // Is this the requested disconnection?
  511. if ( connectionItr->mInputInstance == pInputBehavior && connectionItr->mInputName == pInputName )
  512. {
  513. // Yes, so remove connection.
  514. pPortConnections->erase_fast( connectionItr );
  515. return true;
  516. }
  517. }
  518. // Not found so warn.
  519. Con::warnf(
  520. "Could not disconnect output '%s' on behavior '%s' from input '%s' on behavior '%s' as the connection does not exist.",
  521. pOutputName,
  522. pOutputBehavior->getTemplateName(),
  523. pInputName,
  524. pInputBehavior->getTemplateName()
  525. );
  526. return false;
  527. }
  528. //-----------------------------------------------------------------------------
  529. bool BehaviorComponent::raise( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName )
  530. {
  531. // Sanity!
  532. AssertFatal( pOutputBehavior != NULL, "Output behavior cannot be NULL." );
  533. AssertFatal( pOutputBehavior->isProperlyAdded(), "Output behavior is not registered." );
  534. AssertFatal( pOutputName != NULL, "Output name cannot be NULL." );
  535. // Is the output behavior owned by this behavior component?
  536. if ( pOutputBehavior->getBehaviorOwner() != this )
  537. {
  538. // No, so warn.
  539. Con::warnf(
  540. "Could not raise output '%s' on behavior '%s' as the output behavior is not owned by this component.",
  541. pOutputName,
  542. pOutputBehavior->getTemplateName()
  543. );
  544. return false;
  545. }
  546. // Does the behavior have the specified output?
  547. if ( !pOutputBehavior->getTemplate()->hasBehaviorOutput( pOutputName ) )
  548. {
  549. // No, so warn.
  550. Con::warnf(
  551. "Could not raise output '%s' on behavior '%s' as the behavior does not have such an output.",
  552. pOutputName,
  553. pOutputBehavior->getTemplateName()
  554. );
  555. return false;
  556. }
  557. // Execute a callback for the output.
  558. // NOTE: This callback should not delete behaviors otherwise strange things can happen!
  559. Con::executef( this, 2, pOutputName, pOutputBehavior->getIdString() );
  560. // Find behavior instance connections.
  561. typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.find( pOutputBehavior->getId() );
  562. // Finish if there are no outbound connections for this output behavior.
  563. if ( instanceItr == mBehaviorConnections.end() )
  564. return true;
  565. // Find instance output connection.
  566. typeOutputNameConnectionHash::iterator outputItr = instanceItr->value->find( pOutputName );
  567. // Finish if there are no outbound connections for this output.
  568. if ( outputItr == instanceItr->value->end() )
  569. return true;
  570. // Fetch port connection(s).
  571. typePortConnectionVector* pPortConnections = outputItr->value;
  572. // Process output connection(s).
  573. for ( typePortConnectionVector::iterator connectionItr = pPortConnections->begin(); connectionItr != pPortConnections->end(); ++connectionItr )
  574. {
  575. // Fetch input behavior.
  576. BehaviorInstance* pInputBehavior = connectionItr->mInputInstance;
  577. // Fetch input name.
  578. StringTableEntry pInputName = connectionItr->mInputName;
  579. #ifdef TORQUE_DEBUG
  580. // Sanity!
  581. AssertFatal( pInputBehavior->isProperlyAdded(), "Input behavior is not registered." );
  582. // Does the behavior have the specified input?
  583. if ( !pInputBehavior->getTemplate()->hasBehaviorInput( pInputName ) )
  584. {
  585. // No, so warn.
  586. Con::warnf(
  587. "Could not raise input '%s' on behavior '%s' as the behavior does not have such an input.",
  588. pInputName,
  589. pInputBehavior->getTemplateName()
  590. );
  591. return false;
  592. }
  593. #endif
  594. // Execute a callback for the input.
  595. // NOTE: This callback should not delete behaviors otherwise strange things can happen!
  596. Con::executef( pInputBehavior, 3, pInputName, pOutputBehavior->getIdString(), pOutputName );
  597. }
  598. return true;
  599. }
  600. //-----------------------------------------------------------------------------
  601. U32 BehaviorComponent::getBehaviorConnectionCount( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName )
  602. {
  603. // Sanity!
  604. AssertFatal( pOutputBehavior != NULL, "Output behavior cannot be NULL." );
  605. AssertFatal( pOutputName != NULL, "Output name cannot be NULL." );
  606. // Is the output behavior owned by this behavior component?
  607. if ( pOutputBehavior->getBehaviorOwner() != this )
  608. {
  609. // No, so warn.
  610. Con::warnf(
  611. "Could not get behavior connection count on output '%s' on behavior '%s' as the output behavior is not owned by this component.",
  612. pOutputName,
  613. pOutputBehavior->getTemplateName()
  614. );
  615. return 0;
  616. }
  617. // Does the behavior have the specified output?
  618. if ( !pOutputBehavior->getTemplate()->hasBehaviorOutput( pOutputName ) )
  619. {
  620. // No, so warn.
  621. Con::warnf(
  622. "Could not get behavior connection count for output '%s' on behavior '%s' as the behavior does not have such an output.",
  623. pOutputName,
  624. pOutputBehavior->getTemplateName()
  625. );
  626. return 0;
  627. }
  628. // Find behavior instance connections.
  629. typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.find( pOutputBehavior->getId() );
  630. // Finish if there are no outbound connections for this output behavior.
  631. if ( instanceItr == mBehaviorConnections.end() )
  632. return 0;
  633. // Find instance output connection.
  634. typeOutputNameConnectionHash::iterator outputItr = instanceItr->value->find( pOutputName );
  635. // Finish if there are no outbound connections for this output.
  636. if ( outputItr == instanceItr->value->end() )
  637. return 0;
  638. // Fetch port connection(s).
  639. typePortConnectionVector* pPortConnections = outputItr->value;
  640. // Return number of connections.
  641. return pPortConnections->size();
  642. }
  643. //-----------------------------------------------------------------------------
  644. const BehaviorComponent::BehaviorPortConnection* BehaviorComponent::getBehaviorConnection( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName, const U32 connectionIndex )
  645. {
  646. // Sanity!
  647. AssertFatal( pOutputBehavior != NULL, "Output behavior cannot be NULL." );
  648. AssertFatal( pOutputName != NULL, "Output name cannot be NULL." );
  649. // Fetch behavior connection count.
  650. const U32 behaviorConnectionCount = getBehaviorConnectionCount( pOutputBehavior, pOutputName );
  651. // Finish if there are no connections.
  652. if ( behaviorConnectionCount == 0 )
  653. return NULL;
  654. // Is the connection index valid?
  655. if ( connectionIndex >= behaviorConnectionCount )
  656. {
  657. // No, so warn.
  658. Con::warnf(
  659. "Could not get behavior the behavior connection index '%d' on output '%s' on behavior '%s' as the output behavior only has '%d' connections",
  660. connectionIndex,
  661. pOutputName,
  662. pOutputBehavior->getTemplateName(),
  663. behaviorConnectionCount
  664. );
  665. return NULL;
  666. }
  667. // Fetch behavior connections.
  668. const typePortConnectionVector* pConnections = getBehaviorConnections( pOutputBehavior, pOutputName );
  669. // Fetch behavior connection.
  670. const BehaviorComponent::BehaviorPortConnection* pBehaviorConnection = &((*pConnections)[connectionIndex]);
  671. // Return behavior connection.
  672. return pBehaviorConnection;
  673. }
  674. //-----------------------------------------------------------------------------
  675. const BehaviorComponent::typePortConnectionVector* BehaviorComponent::getBehaviorConnections( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName )
  676. {
  677. // Sanity!
  678. AssertFatal( pOutputBehavior != NULL, "Output behavior cannot be NULL." );
  679. AssertFatal( pOutputName != NULL, "Output name cannot be NULL." );
  680. // Is the output behavior owned by this behavior component?
  681. if ( pOutputBehavior->getBehaviorOwner() != this )
  682. {
  683. // No, so warn.
  684. Con::warnf(
  685. "Could not get behavior connections on output '%s' on behavior '%s' as the output behavior is not owned by this component.",
  686. pOutputName,
  687. pOutputBehavior->getTemplateName()
  688. );
  689. return NULL;
  690. }
  691. // Does the behavior have the specified output?
  692. if ( !pOutputBehavior->getTemplate()->hasBehaviorOutput( pOutputName ) )
  693. {
  694. // No, so warn.
  695. Con::warnf(
  696. "Could not get behavior connections for output '%s' on behavior '%s' as the behavior does not have such an output.",
  697. pOutputName,
  698. pOutputBehavior->getTemplateName()
  699. );
  700. return NULL;
  701. }
  702. // Find behavior instance connections.
  703. typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.find( pOutputBehavior->getId() );
  704. // Finish if there are no outbound connections for this output behavior.
  705. if ( instanceItr == mBehaviorConnections.end() )
  706. return NULL;
  707. // Find instance output connection.
  708. typeOutputNameConnectionHash::iterator outputItr = instanceItr->value->find( pOutputName );
  709. // Finish if there are no outbound connections for this output.
  710. if ( outputItr == instanceItr->value->end() )
  711. return NULL;
  712. // Fetch port connection(s).
  713. typePortConnectionVector* pPortConnections = outputItr->value;
  714. // Return number of connections.
  715. return pPortConnections;
  716. }
  717. //-----------------------------------------------------------------------------
  718. void BehaviorComponent::onTamlCustomWrite( TamlCustomNodes& customNodes )
  719. {
  720. // Call parent.
  721. Parent::onTamlCustomWrite( customNodes );
  722. // Fetch behavior count.
  723. const U32 behaviorCount = (U32)mBehaviors.size();
  724. // Finish if no behaviors.
  725. if( behaviorCount == 0 )
  726. return;
  727. // Fetch behavior template asset field type.
  728. StringTableEntry behaviorTemplateAssetFieldType = StringTable->insert( BEHAVIORTEMPLATE_ASSET_FIELDTYPE );
  729. // Add behavior property.
  730. TamlCustomProperty* pBehaviorProperty = customProperties.addProperty( BEHAVIOR_CUSTOMPROPERTY_NAME );
  731. // Iterate behaviors.
  732. for( SimSet::iterator behaviorItr = mBehaviors.begin(); behaviorItr != mBehaviors.end(); ++behaviorItr )
  733. {
  734. // Fetch behavior.
  735. BehaviorInstance* pBehaviorInstance = dynamic_cast<BehaviorInstance*>( *behaviorItr );
  736. // Fetch template.
  737. BehaviorTemplate* pBehaviorTemplate = pBehaviorInstance->getTemplate();
  738. // Add behavior alias.
  739. TamlPropertyAlias* pBehaviorAlias = pBehaviorProperty->addAlias( pBehaviorInstance->getTemplateName() );
  740. // Add behavior Id field.
  741. pBehaviorAlias->addField( BEHAVIOR_ID_FIELD_NAME, pBehaviorInstance->getBehaviorId() );
  742. // Fetch field count,
  743. const U32 behaviorFieldCount = pBehaviorTemplate->getBehaviorFieldCount();
  744. // Write out the fields which the behavior template knows about.
  745. for( U32 fieldIndex = 0; fieldIndex < behaviorFieldCount; ++fieldIndex )
  746. {
  747. // Fetch field.
  748. BehaviorTemplate::BehaviorField* pBehaviorField = pBehaviorTemplate->getBehaviorField( fieldIndex );
  749. // Set default field type.
  750. S32 fieldType = -1;
  751. // Is this an asset field type?
  752. if ( pBehaviorField != NULL && pBehaviorField->mType == behaviorTemplateAssetFieldType )
  753. {
  754. // Yes, so update field type.
  755. fieldType = TypeAssetId;
  756. }
  757. // Fetch field value.
  758. const char* pFieldValue = pBehaviorInstance->getPrefixedDynamicDataField( pBehaviorField->mName, NULL, fieldType );
  759. // Add behavior field.
  760. pBehaviorAlias->addField( pBehaviorField->mName, pFieldValue );
  761. }
  762. }
  763. // Fetch behavior connection count.
  764. const U32 behaviorConnectionCount = (U32)mBehaviorConnections.size();
  765. // Finish if no behavior connections.
  766. if ( behaviorConnectionCount == 0 )
  767. return;
  768. // Add behavior connection property.
  769. TamlCustomProperty* pConnectionProperty = customProperties.addProperty( BEHAVIOR_CONNECTION_CUSTOMPROPERTY_NAME );
  770. // Iterate instance connections.
  771. for( typeInstanceConnectionHash::iterator instanceItr = mBehaviorConnections.begin(); instanceItr != mBehaviorConnections.end(); ++instanceItr )
  772. {
  773. // Fetch output name connection(s).
  774. typeOutputNameConnectionHash* pOutputNameConnection = instanceItr->value;
  775. // Iterate output name connections.
  776. for( typeOutputNameConnectionHash::iterator outputItr = pOutputNameConnection->begin(); outputItr != pOutputNameConnection->end(); ++outputItr )
  777. {
  778. // Fetch port connection(s).
  779. typePortConnectionVector* pPortConnections = outputItr->value;
  780. // Iterate input connections.
  781. for( typePortConnectionVector::iterator connectionItr = pPortConnections->begin(); connectionItr != pPortConnections->end(); ++connectionItr )
  782. {
  783. // Fetch connection.
  784. BehaviorPortConnection* pConnection = connectionItr;
  785. // Add connection alias.
  786. TamlPropertyAlias* pConnectionAlias = pConnectionProperty->addAlias( BEHAVIOR_CONNECTION_TYPE_NAME );
  787. // Add behavior field.
  788. pConnectionAlias->addField( pConnection->mOutputName, pConnection->mOutputInstance->getBehaviorId() );
  789. pConnectionAlias->addField( pConnection->mInputName, pConnection->mInputInstance->getBehaviorId() );
  790. }
  791. }
  792. }
  793. }
  794. //-----------------------------------------------------------------------------
  795. void BehaviorComponent::onTamlCustomRead( const TamlCustomNodes& customNodes )
  796. {
  797. // Call parent.
  798. Parent::onTamlCustomRead( customProperties );
  799. // Find behavior custom property name.
  800. const TamlCustomProperty* pCustomProperty = customProperties.findProperty( BEHAVIOR_CUSTOMPROPERTY_NAME );
  801. // Do we have the property?
  802. if ( pCustomProperty != NULL )
  803. {
  804. // Yes, so reset maximum behavior Id.
  805. S32 maximumBehaviorId = 0;
  806. // Fetch behavior Id field name.
  807. StringTableEntry behaviorFieldIdName = StringTable->insert( BEHAVIOR_ID_FIELD_NAME );
  808. // Fetch behavior template asset field type.
  809. StringTableEntry behaviorTemplateAssetFieldType = StringTable->insert( BEHAVIORTEMPLATE_ASSET_FIELDTYPE );
  810. // Iterate property alias.
  811. for( TamlCustomProperty::const_iterator propertyAliasItr = pCustomProperty->begin(); propertyAliasItr != pCustomProperty->end(); ++propertyAliasItr )
  812. {
  813. // Fetch property alias.
  814. TamlPropertyAlias* pPropertyAlias = *propertyAliasItr;
  815. // Fetch template.
  816. BehaviorTemplate* pTemplate = dynamic_cast<BehaviorTemplate *>( Sim::findObject( pPropertyAlias->mAliasName ) );
  817. // Find template?
  818. if( pTemplate == NULL )
  819. {
  820. // No, so warn appropriately.
  821. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Missing Behavior '%s'", pPropertyAlias->mAliasName );
  822. if( isMethod( "onBehaviorMissing" ) )
  823. Con::executef( this, 2, "onBehaviorMissing", pPropertyAlias->mAliasName );
  824. // Skip it.
  825. continue;
  826. }
  827. // Create an instance of the template.
  828. BehaviorInstance* pBehaviorInstance = pTemplate->createInstance();
  829. // Did we create a behavior instance?
  830. if ( pBehaviorInstance == NULL )
  831. {
  832. // No, so warn appropriately.
  833. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Found behavior could not create an instance '%s'", pPropertyAlias->mAliasName );
  834. if( isMethod( "onBehaviorMissing" ) )
  835. Con::executef( this, 2, "onBehaviorMissing", pPropertyAlias->mAliasName );
  836. // Skip it.
  837. continue;
  838. }
  839. S32 behaviorId = 0;
  840. // Iterate property fields.
  841. for ( TamlPropertyAlias::const_iterator propertyFieldItr = pPropertyAlias->begin(); propertyFieldItr != pPropertyAlias->end(); ++propertyFieldItr )
  842. {
  843. // Fetch property field.
  844. TamlCustomNodeField* pPropertyField = *propertyFieldItr;
  845. // Fetch field name.
  846. const char* pFieldName = pPropertyField->getFieldName();
  847. // Fetch field value.
  848. const char* pFieldValue = pPropertyField->getFieldValue();
  849. // Is this the behavior field Id name?
  850. if ( pFieldName == behaviorFieldIdName )
  851. {
  852. // Yes, so assign it.
  853. behaviorId = dAtoi( pFieldValue );
  854. // Is the behavior Id valid?
  855. if ( behaviorId < 1 )
  856. {
  857. // No, so warn.
  858. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Encountered an invalid behavior Id of '%d' on behavior '%s'.",
  859. behaviorId,
  860. pPropertyAlias->mAliasName );
  861. }
  862. // Update maximum behavior Id found.
  863. if ( behaviorId > maximumBehaviorId )
  864. maximumBehaviorId = behaviorId;
  865. /// Skip it.
  866. continue;
  867. }
  868. // Fetch behavior field.
  869. BehaviorTemplate::BehaviorField* pBehaviorField = pTemplate->getBehaviorField( pFieldName );
  870. // Set default field type.
  871. S32 fieldType = -1;
  872. // Is this an asset field type?
  873. if ( pBehaviorField != NULL && pBehaviorField->mType == behaviorTemplateAssetFieldType )
  874. {
  875. // Yes, so update field type.
  876. fieldType = TypeAssetId;
  877. }
  878. // Set field.
  879. pBehaviorInstance->setPrefixedDynamicDataField( pPropertyField->getFieldName(), NULL, pPropertyField->getFieldValue(), fieldType );
  880. }
  881. // Add behavior.
  882. addBehavior( pBehaviorInstance );
  883. // Override the automatically allocated behavior Id when adding the behavior.
  884. // NOTE: This must be done after adding the behavior.
  885. pBehaviorInstance->setBehaviorId( behaviorId );
  886. }
  887. // Set master as next behavior id.
  888. mMasterBehaviorId = (U32)maximumBehaviorId+1;
  889. }
  890. // Find behavior connections custom property name.
  891. const TamlCustomProperty* pConnectionProperty = customProperties.findProperty( BEHAVIOR_CONNECTION_CUSTOMPROPERTY_NAME );
  892. // Do we have the property?
  893. if ( pConnectionProperty != NULL )
  894. {
  895. // Yes, so insert connection alias.
  896. StringTableEntry connectionAlias = StringTable->insert( BEHAVIOR_CONNECTION_TYPE_NAME );
  897. // Iterate property alias.
  898. for( TamlCustomProperty::const_iterator propertyAliasItr = pConnectionProperty->begin(); propertyAliasItr != pConnectionProperty->end(); ++propertyAliasItr )
  899. {
  900. // Fetch property alias.
  901. TamlPropertyAlias* pPropertyAlias = *propertyAliasItr;
  902. // Skip if the alias isn't a connection.
  903. if ( pPropertyAlias->mAliasName != connectionAlias )
  904. continue;
  905. // Are there two properties?
  906. if ( pPropertyAlias->size() != 2 )
  907. {
  908. // No, so warn.
  909. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Encountered a behavior connection with more than two connection fields." );
  910. continue;
  911. }
  912. // Fetch property field #1.
  913. TamlCustomNodeField* pPropertyField1 = *pPropertyAlias->begin();
  914. TamlCustomNodeField* pPropertyField2 = *(pPropertyAlias->begin()+1);
  915. // Fetch behavior instances #1.
  916. BehaviorInstance* pBehaviorInstance1 = getBehaviorByInstanceId( dAtoi( pPropertyField1->getFieldValue() ) );
  917. // Did we find the behavior?
  918. if ( pBehaviorInstance1 == NULL )
  919. {
  920. // No, so warn.
  921. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Could not find a behavior instance Id '%s=%s'.",
  922. pPropertyField1->getFieldName(), pPropertyField1->getFieldValue() );
  923. continue;
  924. }
  925. // Fetch behavior instances #2.
  926. BehaviorInstance* pBehaviorInstance2 = getBehaviorByInstanceId( dAtoi( pPropertyField2->getFieldValue() ) );
  927. // Did we find the behavior?
  928. if ( pBehaviorInstance2 == NULL )
  929. {
  930. // No, so warn.
  931. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Could not find a behavior instance Id '%s=%s'.",
  932. pPropertyField2->getFieldName(), pPropertyField2->getFieldValue() );
  933. continue;
  934. }
  935. // Fetch behavior fields.
  936. StringTableEntry behaviorFieldName1 = pPropertyField1->getFieldName();
  937. StringTableEntry behaviorFieldName2 = pPropertyField2->getFieldName();
  938. BehaviorInstance* pOutputInstance;
  939. BehaviorInstance* pInputInstance;
  940. StringTableEntry outputName;
  941. StringTableEntry inputName;
  942. // Is the output on behavior instance #1?
  943. if ( pBehaviorInstance1->getTemplate()->hasBehaviorOutput( behaviorFieldName1 ) )
  944. {
  945. // Yes, so has behavior instance #2 got the input?
  946. if ( !pBehaviorInstance2->getTemplate()->hasBehaviorInput( behaviorFieldName2 ) )
  947. {
  948. // No, so warn.
  949. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Could not find a behavior input '%s' on behavior '%s'.",
  950. behaviorFieldName2, pBehaviorInstance2->getTemplateName() );
  951. continue;
  952. }
  953. // Assign output/input appropriately.
  954. pOutputInstance = pBehaviorInstance1;
  955. pInputInstance = pBehaviorInstance2;
  956. outputName = behaviorFieldName1;
  957. inputName = behaviorFieldName2;
  958. }
  959. // Is the output on behavior instance #2?
  960. else if ( pBehaviorInstance2->getTemplate()->hasBehaviorOutput( behaviorFieldName2 ) )
  961. {
  962. // Yes, so has behavior instance #1 got the input?
  963. if ( !pBehaviorInstance1->getTemplate()->hasBehaviorInput( behaviorFieldName1 ) )
  964. {
  965. // No, so warn.
  966. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Could not find a behavior input '%s' on behavior '%s'.",
  967. behaviorFieldName1, pBehaviorInstance1->getTemplateName() );
  968. continue;
  969. }
  970. // Assign output/input appropriately.
  971. pOutputInstance = pBehaviorInstance2;
  972. pInputInstance = pBehaviorInstance1;
  973. outputName = behaviorFieldName2;
  974. inputName = behaviorFieldName1;
  975. }
  976. else
  977. {
  978. // Warn.
  979. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Invalid output/input on behavior connection '%s=%s' & '%s=%s''.",
  980. behaviorFieldName1, pBehaviorInstance1->getTemplateName(),
  981. behaviorFieldName2, pBehaviorInstance2->getTemplateName() );
  982. continue;
  983. }
  984. // Can we connect?
  985. if ( !connect( pOutputInstance, pInputInstance, outputName, inputName ) )
  986. {
  987. // No, so warn.
  988. Con::warnf( "BehaviorComponent::onTamlCustomRead() - Failed to connect behaviors '%s=%s' & '%s=%s''.",
  989. behaviorFieldName1, pBehaviorInstance1->getTemplateName(),
  990. behaviorFieldName2, pBehaviorInstance2->getTemplateName() );
  991. continue;
  992. }
  993. }
  994. }
  995. }
  996. //-----------------------------------------------------------------------------
  997. void BehaviorComponent::write( Stream &stream, U32 tabStop, U32 flags /* = 0 */ )
  998. {
  999. // Export selected only?
  1000. if( ( flags & SelectedOnly ) && !isSelected() )
  1001. {
  1002. return;
  1003. }
  1004. if( mBehaviors.size() == 0 )
  1005. {
  1006. Parent::write( stream, tabStop, flags );
  1007. return;
  1008. }
  1009. // The work we want to perform here is in the Taml callback.
  1010. onTamlPreWrite();
  1011. // Write object.
  1012. Parent::write( stream, tabStop, flags );
  1013. // The work we want to perform here is in the Taml callback.
  1014. onTamlPostWrite();
  1015. }
  1016. //-----------------------------------------------------------------------------
  1017. bool BehaviorComponent::handlesConsoleMethod( const char *fname, S32 *routingId )
  1018. {
  1019. // CodeReview [6/25/2007 justind]
  1020. // If we're deleting the BehaviorComponent, don't forward the call to the
  1021. // behaviors, the parent onRemove will handle freeing them
  1022. // This should really be handled better, and is in the Parent implementation
  1023. // but behaviors are a special case because they always want to be called BEFORE
  1024. // the parent to act.
  1025. if( dStricmp( fname, "delete" ) == 0 )
  1026. return Parent::handlesConsoleMethod( fname, routingId );
  1027. for( SimSet::iterator nItr = mBehaviors.begin(); nItr != mBehaviors.end(); nItr++ )
  1028. {
  1029. SimObject *pComponent = dynamic_cast<SimObject *>(*nItr);
  1030. if( pComponent != NULL && pComponent->isMethod( fname ) )
  1031. {
  1032. *routingId = -2; // -2 denotes method on component
  1033. return true;
  1034. }
  1035. }
  1036. // Let parent handle it
  1037. return Parent::handlesConsoleMethod( fname, routingId );
  1038. }
  1039. //-----------------------------------------------------------------------------
  1040. // Needed to be able to directly call execute on a Namespace::Entry
  1041. extern ExprEvalState gEvalState;
  1042. const char *BehaviorComponent::callOnBehaviors( U32 argc, const char *argv[] )
  1043. {
  1044. if( mBehaviors.empty() )
  1045. return Parent::callOnBehaviors( argc, argv );
  1046. const char *cbName = StringTable->insert(argv[0]);
  1047. // Copy the arguments to avoid weird clobbery situations.
  1048. FrameTemp<char *> argPtrs (argc);
  1049. U32 strdupWatermark = FrameAllocator::getWaterMark();
  1050. for( U32 i = 0; i < argc; i++ )
  1051. {
  1052. argPtrs[i] = reinterpret_cast<char *>( FrameAllocator::alloc( dStrlen( argv[i] ) + 1 ) );
  1053. dStrcpy( argPtrs[i], argv[i] );
  1054. }
  1055. // Walk backwards through the list just as with components
  1056. const char* result = "";
  1057. bool handled = false;
  1058. for( SimSet::iterator i = (mBehaviors.end()-1); i >= mBehaviors.begin(); i-- )
  1059. {
  1060. BehaviorInstance *pBehavior = dynamic_cast<BehaviorInstance *>( *i );
  1061. AssertFatal( pBehavior, "BehaviorComponent::callOnBehaviors - Bad behavior instance in list." );
  1062. AssertFatal( pBehavior->getId() > 0, "Invalid id for behavior component" );
  1063. // Use the BehaviorInstance's namespace
  1064. Namespace *pNamespace = pBehavior->getNamespace();
  1065. if(!pNamespace)
  1066. continue;
  1067. // Lookup the Callback Namespace entry and then splice callback
  1068. Namespace::Entry *pNSEntry = pNamespace->lookup(cbName);
  1069. if( pNSEntry )
  1070. {
  1071. // Set %this to our BehaviorInstance's Object ID
  1072. argPtrs[1] = const_cast<char *>( pBehavior->getIdString() );
  1073. // Change the Current Console object, execute, restore Object
  1074. SimObject *save = gEvalState.thisObject;
  1075. gEvalState.thisObject = pBehavior;
  1076. result = pNSEntry->execute(argc, const_cast<const char **>( ~argPtrs ), &gEvalState);
  1077. gEvalState.thisObject = save;
  1078. handled = true;
  1079. break;
  1080. }
  1081. }
  1082. // If this wasn't handled by a behavior above then pass along to the parent DynamicConsoleMethodComponent
  1083. // to deal with it. If the parent cannot handle the message it will return an error string.
  1084. if (!handled)
  1085. {
  1086. result = Parent::callOnBehaviors( argc, argv );
  1087. }
  1088. // Clean up.
  1089. FrameAllocator::setWaterMark( strdupWatermark );
  1090. return result;
  1091. }
  1092. //-----------------------------------------------------------------------------
  1093. const char *BehaviorComponent::_callMethod( U32 argc, const char *argv[], bool callThis /* = true */ )
  1094. {
  1095. if( mBehaviors.empty() )
  1096. return Parent::_callMethod( argc, argv, callThis );
  1097. const char *cbName = StringTable->insert(argv[0]);
  1098. // Copy the arguments to avoid weird clobbery situations.
  1099. FrameTemp<char *> argPtrs (argc);
  1100. U32 strdupWatermark = FrameAllocator::getWaterMark();
  1101. for( U32 i = 0; i < argc; i++ )
  1102. {
  1103. argPtrs[i] = reinterpret_cast<char *>( FrameAllocator::alloc( dStrlen( argv[i] ) + 1 ) );
  1104. dStrcpy( argPtrs[i], argv[i] );
  1105. }
  1106. for( SimSet::iterator i = mBehaviors.begin(); i != mBehaviors.end(); i++ )
  1107. {
  1108. BehaviorInstance *pBehavior = dynamic_cast<BehaviorInstance *>( *i );
  1109. AssertFatal( pBehavior, "BehaviorComponent::_callMethod - Bad behavior instance in list." );
  1110. AssertFatal( pBehavior->getId() > 0, "Invalid id for behavior component" );
  1111. // Use the BehaviorInstance's namespace
  1112. Namespace *pNamespace = pBehavior->getNamespace();
  1113. if(!pNamespace)
  1114. continue;
  1115. // Lookup the Callback Namespace entry and then splice callback
  1116. Namespace::Entry *pNSEntry = pNamespace->lookup(cbName);
  1117. if( pNSEntry )
  1118. {
  1119. // Set %this to our BehaviorInstance's Object ID
  1120. argPtrs[1] = const_cast<char *>( pBehavior->getIdString() );
  1121. // Change the Current Console object, execute, restore Object
  1122. SimObject *save = gEvalState.thisObject;
  1123. gEvalState.thisObject = pBehavior;
  1124. pNSEntry->execute(argc, const_cast<const char **>( ~argPtrs ), &gEvalState);
  1125. gEvalState.thisObject = save;
  1126. }
  1127. }
  1128. // Pass this up to the parent since a BehaviorComponent is still a DynamicConsoleMethodComponent
  1129. // it needs to be able to contain other components and behave properly
  1130. const char* fnRet = Parent::_callMethod( argc, argv, callThis );
  1131. // Clean up.
  1132. FrameAllocator::setWaterMark( strdupWatermark );
  1133. return fnRet;
  1134. }