gameBase.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "T3D/gameBase/gameBase.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/engineAPI.h"
  30. #include "console/consoleInternal.h"
  31. #include "core/stream/bitStream.h"
  32. #include "sim/netConnection.h"
  33. #include "T3D/gameBase/gameConnection.h"
  34. #include "math/mathIO.h"
  35. #include "T3D/gameBase/moveManager.h"
  36. #include "T3D/gameBase/gameProcess.h"
  37. #ifdef TORQUE_DEBUG_NET_MOVES
  38. #include "T3D/aiConnection.h"
  39. #endif
  40. #include "afx/arcaneFX.h"
  41. //----------------------------------------------------------------------------
  42. // Ghost update relative priority values
  43. static F32 sUpFov = 1.0;
  44. static F32 sUpDistance = 0.4f;
  45. static F32 sUpVelocity = 0.4f;
  46. static F32 sUpSkips = 0.2f;
  47. static F32 sUpInterest = 0.2f;
  48. //----------------------------------------------------------------------------
  49. IMPLEMENT_CO_DATABLOCK_V1(GameBaseData);
  50. ConsoleDocClass( GameBaseData,
  51. "@brief Scriptable, demo-able datablock. Used by GameBase objects.\n\n"
  52. "@see GameBase\n"
  53. "@ingroup gameObjects\n"
  54. );
  55. IMPLEMENT_CALLBACK( GameBaseData, onAdd, void, ( GameBase* obj ), ( obj ),
  56. "@brief Called when the object is added to the scene.\n\n"
  57. "@param obj the GameBase object\n\n"
  58. "@tsexample\n"
  59. "datablock GameBaseData(MyObjectData)\n"
  60. "{\n"
  61. " category = \"Misc\";\n"
  62. "};\n\n"
  63. "function MyObjectData::onAdd( %this, %obj )\n"
  64. "{\n"
  65. " echo( \"Added \" @ %obj.getName() @ \" to the scene.\" );\n"
  66. "}\n\n"
  67. "function MyObjectData::onNewDataBlock( %this, %obj )\n"
  68. "{\n"
  69. " echo( \"Assign \" @ %this.getName() @ \" datablock to \" %obj.getName() );\n"
  70. "}\n\n"
  71. "function MyObjectData::onRemove( %this, %obj )\n"
  72. "{\n"
  73. " echo( \"Removed \" @ %obj.getName() @ \" to the scene.\" );\n"
  74. "}\n\n"
  75. "function MyObjectData::onMount( %this, %obj, %mountObj, %node )\n"
  76. "{\n"
  77. " echo( %obj.getName() @ \" mounted to \" @ %mountObj.getName() );\n"
  78. "}\n\n"
  79. "function MyObjectData::onUnmount( %this, %obj, %mountObj, %node )\n"
  80. "{\n"
  81. " echo( %obj.getName() @ \" unmounted from \" @ %mountObj.getName() );\n"
  82. "}\n\n"
  83. "@endtsexample\n" );
  84. IMPLEMENT_CALLBACK( GameBaseData, onNewDataBlock, void, ( GameBase* obj ), ( obj ),
  85. "@brief Called when the object has a new datablock assigned.\n\n"
  86. "@param obj the GameBase object\n\n"
  87. "@see onAdd for an example\n" );
  88. IMPLEMENT_CALLBACK( GameBaseData, onRemove, void, ( GameBase* obj ), ( obj ),
  89. "@brief Called when the object is removed from the scene.\n\n"
  90. "@param obj the GameBase object\n\n"
  91. "@see onAdd for an example\n" );
  92. IMPLEMENT_CALLBACK( GameBaseData, onMount, void, ( SceneObject* obj, SceneObject* mountObj, S32 node ), ( obj, mountObj, node ),
  93. "@brief Called when the object is mounted to another object in the scene.\n\n"
  94. "@param obj the GameBase object being mounted\n"
  95. "@param mountObj the object we are mounted to\n"
  96. "@param node the mountObj node we are mounted to\n\n"
  97. "@see onAdd for an example\n" );
  98. IMPLEMENT_CALLBACK( GameBaseData, onUnmount, void, ( SceneObject* obj, SceneObject* mountObj, S32 node ), ( obj, mountObj, node ),
  99. "@brief Called when the object is unmounted from another object in the scene.\n\n"
  100. "@param obj the GameBase object being unmounted\n"
  101. "@param mountObj the object we are unmounted from\n"
  102. "@param node the mountObj node we are unmounted from\n\n"
  103. "@see onAdd for an example\n" );
  104. IMPLEMENT_CALLBACK( GameBase, setControl, void, ( bool controlled ), ( controlled ),
  105. "@brief Called when the client controlling the object changes.\n\n"
  106. "@param controlled true if a client now controls this object, false if no "
  107. "client controls this object.\n" );
  108. GameBaseData::GameBaseData()
  109. {
  110. category = "";
  111. packed = false;
  112. }
  113. GameBaseData::GameBaseData(const GameBaseData& other, bool temp_clone) : SimDataBlock(other, temp_clone)
  114. {
  115. packed = other.packed;
  116. category = other.category;
  117. //mReloadSignal = other.mReloadSignal; // DO NOT copy the mReloadSignal member.
  118. }
  119. void GameBaseData::inspectPostApply()
  120. {
  121. Parent::inspectPostApply();
  122. // Tell interested parties ( like objects referencing this datablock )
  123. // that we have been modified and they might want to rebuild...
  124. mReloadSignal.trigger();
  125. }
  126. bool GameBaseData::onAdd()
  127. {
  128. if (!Parent::onAdd())
  129. return false;
  130. return true;
  131. }
  132. void GameBaseData::initPersistFields()
  133. {
  134. addGroup("Scripting");
  135. addField( "category", TypeCaseString, Offset( category, GameBaseData ),
  136. "The group that this datablock will show up in under the \"Scripted\" "
  137. "tab in the World Editor Library." );
  138. endGroup("Scripting");
  139. Parent::initPersistFields();
  140. }
  141. bool GameBaseData::preload(bool server, String &errorStr)
  142. {
  143. if (!Parent::preload(server, errorStr))
  144. return false;
  145. packed = false;
  146. return true;
  147. }
  148. void GameBaseData::unpackData(BitStream* stream)
  149. {
  150. Parent::unpackData(stream);
  151. packed = true;
  152. }
  153. //----------------------------------------------------------------------------
  154. bool UNPACK_DB_ID(BitStream * stream, U32 & id)
  155. {
  156. if (stream->readFlag())
  157. {
  158. id = stream->readRangedU32(DataBlockObjectIdFirst,DataBlockObjectIdLast);
  159. return true;
  160. }
  161. return false;
  162. }
  163. bool PACK_DB_ID(BitStream * stream, U32 id)
  164. {
  165. if (stream->writeFlag(id))
  166. {
  167. stream->writeRangedU32(id,DataBlockObjectIdFirst,DataBlockObjectIdLast);
  168. return true;
  169. }
  170. return false;
  171. }
  172. bool PRELOAD_DB(U32 & id, SimDataBlock ** data, bool server, const char * clientMissing, const char * serverMissing)
  173. {
  174. if (server)
  175. {
  176. if (*data)
  177. id = (*data)->getId();
  178. else if (server && serverMissing)
  179. {
  180. Con::errorf(ConsoleLogEntry::General,serverMissing);
  181. return false;
  182. }
  183. }
  184. else
  185. {
  186. if (id && !Sim::findObject(id,*data) && clientMissing)
  187. {
  188. Con::errorf(ConsoleLogEntry::General,clientMissing);
  189. return false;
  190. }
  191. }
  192. return true;
  193. }
  194. //----------------------------------------------------------------------------
  195. bool GameBase::gShowBoundingBox = false;
  196. //----------------------------------------------------------------------------
  197. IMPLEMENT_CO_NETOBJECT_V1(GameBase);
  198. ConsoleDocClass( GameBase,
  199. "@brief Base class for game objects which use datablocks, networking, are "
  200. "editable, and need to process ticks.\n\n"
  201. "@ingroup gameObjects\n"
  202. );
  203. GameBase::GameBase()
  204. : mDataBlock( NULL ),
  205. mControllingClient( NULL ),
  206. mCurrentWaterObject( NULL )
  207. {
  208. mNetFlags.set(Ghostable);
  209. mTypeMask |= GameBaseObjectType;
  210. mProcessTag = 0;
  211. // From ProcessObject
  212. mIsGameBase = true;
  213. #ifdef TORQUE_DEBUG_NET_MOVES
  214. mLastMoveId = 0;
  215. mTicksSinceLastMove = 0;
  216. mIsAiControlled = false;
  217. #endif
  218. }
  219. GameBase::~GameBase()
  220. {
  221. if (scope_registered)
  222. arcaneFX::unregisterScopedObject(this);
  223. }
  224. //----------------------------------------------------------------------------
  225. bool GameBase::onAdd()
  226. {
  227. if ( !Parent::onAdd() )
  228. return false;
  229. // Datablock must be initialized on the server.
  230. // Client datablock are initialized by the initial update.
  231. if (isClientObject())
  232. {
  233. if (scope_id > 0 && !scope_registered)
  234. arcaneFX::registerScopedObject(this);
  235. }
  236. else
  237. {
  238. if ( mDataBlock && !onNewDataBlock( mDataBlock, false ) )
  239. return false;
  240. }
  241. setProcessTick( true );
  242. return true;
  243. }
  244. void GameBase::onRemove()
  245. {
  246. if (scope_registered)
  247. arcaneFX::unregisterScopedObject(this);
  248. // EDITOR FEATURE: Remove us from the reload signal of our datablock.
  249. if ( mDataBlock )
  250. mDataBlock->mReloadSignal.remove( this, &GameBase::_onDatablockModified );
  251. Parent::onRemove();
  252. }
  253. bool GameBase::onNewDataBlock( GameBaseData *dptr, bool reload )
  254. {
  255. // EDITOR FEATURE: Remove us from old datablock's reload signal and
  256. // add us to the new one.
  257. if ( !reload )
  258. {
  259. if ( mDataBlock )
  260. mDataBlock->mReloadSignal.remove( this, &GameBase::_onDatablockModified );
  261. if ( dptr )
  262. dptr->mReloadSignal.notify( this, &GameBase::_onDatablockModified );
  263. }
  264. mDataBlock = dptr;
  265. if ( !mDataBlock )
  266. return false;
  267. // Don't set mask when new datablock is a temp-clone.
  268. if (mDataBlock->isTempClone())
  269. return true;
  270. setMaskBits(DataBlockMask);
  271. return true;
  272. }
  273. void GameBase::_onDatablockModified()
  274. {
  275. AssertFatal( mDataBlock, "GameBase::onDatablockModified - mDataBlock is NULL." );
  276. onNewDataBlock( mDataBlock, true );
  277. }
  278. void GameBase::inspectPostApply()
  279. {
  280. Parent::inspectPostApply();
  281. setMaskBits(ExtendedInfoMask);
  282. }
  283. //----------------------------------------------------------------------------
  284. void GameBase::processTick(const Move * move)
  285. {
  286. #ifdef TORQUE_DEBUG_NET_MOVES
  287. if (!move)
  288. mTicksSinceLastMove++;
  289. const char * srv = isClientObject() ? "client" : "server";
  290. const char * who = "";
  291. if (isClientObject())
  292. {
  293. if (this == (GameBase*)GameConnection::getConnectionToServer()->getControlObject())
  294. who = " player";
  295. else
  296. who = " ghost";
  297. if (mIsAiControlled)
  298. who = " ai";
  299. }
  300. if (isServerObject())
  301. {
  302. if (dynamic_cast<AIConnection*>(getControllingClient()))
  303. {
  304. who = " ai";
  305. mIsAiControlled = true;
  306. }
  307. else if (getControllingClient())
  308. {
  309. who = " player";
  310. mIsAiControlled = false;
  311. }
  312. else
  313. {
  314. who = "";
  315. mIsAiControlled = false;
  316. }
  317. }
  318. U32 moveid = mLastMoveId+mTicksSinceLastMove;
  319. if (move)
  320. moveid = move->id;
  321. if (getTypeMask() & GameBaseHiFiObjectType)
  322. {
  323. if (move)
  324. Con::printf("Processing (%s%s id %i) move %i",srv,who,getId(), move->id);
  325. else
  326. Con::printf("Processing (%s%s id %i) move %i (%i)",srv,who,getId(),mLastMoveId+mTicksSinceLastMove,mTicksSinceLastMove);
  327. }
  328. if (move)
  329. {
  330. mLastMoveId = move->id;
  331. mTicksSinceLastMove=0;
  332. }
  333. #endif
  334. }
  335. //----------------------------------------------------------------------------
  336. F32 GameBase::getUpdatePriority(CameraScopeQuery *camInfo, U32 updateMask, S32 updateSkips)
  337. {
  338. TORQUE_UNUSED(updateMask);
  339. // Calculate a priority used to decide if this object
  340. // will be updated on the client. All the weights
  341. // are calculated 0 -> 1 Then weighted together at the
  342. // end to produce a priority.
  343. Point3F pos;
  344. getWorldBox().getCenter(&pos);
  345. pos -= camInfo->pos;
  346. F32 dist = pos.len();
  347. if (dist == 0.0f) dist = 0.001f;
  348. pos *= 1.0f / dist;
  349. // Weight based on linear distance, the basic stuff.
  350. F32 wDistance = (dist < camInfo->visibleDistance)?
  351. 1.0f - (dist / camInfo->visibleDistance): 0.0f;
  352. // Weight by field of view, objects directly in front
  353. // will be weighted 1, objects behind will be 0
  354. F32 dot = mDot(pos,camInfo->orientation);
  355. bool inFov = dot > camInfo->cosFov * 1.5f;
  356. F32 wFov = inFov? 1.0f: 0;
  357. // Weight by linear velocity parallel to the viewing plane
  358. // (if it's the field of view, 0 if it's not).
  359. F32 wVelocity = 0.0f;
  360. if (inFov)
  361. {
  362. Point3F vec;
  363. mCross(camInfo->orientation,getVelocity(),&vec);
  364. wVelocity = (vec.len() * camInfo->fov) /
  365. (camInfo->fov * camInfo->visibleDistance);
  366. if (wVelocity > 1.0f)
  367. wVelocity = 1.0f;
  368. }
  369. // Weight by interest.
  370. F32 wInterest;
  371. if (getTypeMask() & (PlayerObjectType || VehicleObjectType ))
  372. wInterest = 0.75f;
  373. else if (getTypeMask() & ProjectileObjectType)
  374. {
  375. // Projectiles are more interesting if they
  376. // are heading for us.
  377. wInterest = 0.30f;
  378. F32 dot = -mDot(pos,getVelocity());
  379. if (dot > 0.0f)
  380. wInterest += 0.20 * dot;
  381. }
  382. else
  383. {
  384. if (getTypeMask() & ItemObjectType)
  385. wInterest = 0.25f;
  386. else
  387. // Everything else is less interesting.
  388. wInterest = 0.0f;
  389. }
  390. // Weight by updateSkips
  391. F32 wSkips = updateSkips * 0.5;
  392. // Calculate final priority, should total to about 1.0f
  393. //
  394. return
  395. wFov * sUpFov +
  396. wDistance * sUpDistance +
  397. wVelocity * sUpVelocity +
  398. wSkips * sUpSkips +
  399. wInterest * sUpInterest;
  400. }
  401. //----------------------------------------------------------------------------
  402. bool GameBase::setDataBlock(GameBaseData* dptr)
  403. {
  404. if (isGhost() || isProperlyAdded()) {
  405. if (mDataBlock != dptr)
  406. return onNewDataBlock(dptr,false);
  407. }
  408. else
  409. mDataBlock = dptr;
  410. return true;
  411. }
  412. //--------------------------------------------------------------------------
  413. void GameBase::scriptOnAdd()
  414. {
  415. // Script onAdd() must be called by the leaf class after
  416. // everything is ready.
  417. if (mDataBlock && !isGhost())
  418. mDataBlock->onAdd_callback( this );
  419. }
  420. void GameBase::scriptOnNewDataBlock()
  421. {
  422. // Script onNewDataBlock() must be called by the leaf class
  423. // after everything is loaded.
  424. if (mDataBlock && !isGhost())
  425. mDataBlock->onNewDataBlock_callback( this );
  426. }
  427. void GameBase::scriptOnRemove()
  428. {
  429. // Script onRemove() must be called by leaf class while
  430. // the object state is still valid.
  431. if (!isGhost() && mDataBlock)
  432. mDataBlock->onRemove_callback( this );
  433. }
  434. //----------------------------------------------------------------------------
  435. void GameBase::setControllingClient(GameConnection* client)
  436. {
  437. if (isClientObject())
  438. {
  439. if (mControllingClient)
  440. setControl_callback( 0 );
  441. if (client)
  442. setControl_callback( 1 );
  443. }
  444. mControllingClient = client;
  445. }
  446. U32 GameBase::getPacketDataChecksum(GameConnection * connection)
  447. {
  448. // just write the packet data into a buffer
  449. // then we can CRC the buffer. This should always let us
  450. // know when there is a checksum problem.
  451. static U8 buffer[1500] = { 0, };
  452. BitStream stream(buffer, sizeof(buffer));
  453. writePacketData(connection, &stream);
  454. U32 byteCount = stream.getPosition();
  455. U32 ret = CRC::calculateCRC(buffer, byteCount, 0xFFFFFFFF);
  456. dMemset(buffer, 0, byteCount);
  457. return ret;
  458. }
  459. void GameBase::writePacketData(GameConnection*, BitStream*)
  460. {
  461. }
  462. void GameBase::readPacketData(GameConnection*, BitStream*)
  463. {
  464. }
  465. U32 GameBase::packUpdate( NetConnection *connection, U32 mask, BitStream *stream )
  466. {
  467. U32 retMask = Parent::packUpdate( connection, mask, stream );
  468. if ( stream->writeFlag( mask & ScaleMask ) )
  469. {
  470. // Only write one bit if the scale is one.
  471. if ( stream->writeFlag( mObjScale != Point3F::One ) )
  472. mathWrite( *stream, mObjScale );
  473. }
  474. if ( stream->writeFlag( ( mask & DataBlockMask ) && mDataBlock != NULL ) )
  475. {
  476. stream->writeRangedU32( mDataBlock->getId(),
  477. DataBlockObjectIdFirst,
  478. DataBlockObjectIdLast );
  479. if ( stream->writeFlag( mNetFlags.test( NetOrdered ) ) )
  480. stream->writeInt( mOrderGUID, 16 );
  481. }
  482. #ifdef TORQUE_DEBUG_NET_MOVES
  483. stream->write(mLastMoveId);
  484. stream->writeFlag(mIsAiControlled);
  485. #endif
  486. if (stream->writeFlag(mask & ScopeIdMask))
  487. {
  488. if (stream->writeFlag(scope_refs > 0))
  489. stream->writeInt(scope_id, SCOPE_ID_BITS);
  490. }
  491. return retMask;
  492. }
  493. void GameBase::unpackUpdate(NetConnection *con, BitStream *stream)
  494. {
  495. Parent::unpackUpdate( con, stream );
  496. // ScaleMask
  497. if ( stream->readFlag() )
  498. {
  499. if ( stream->readFlag() )
  500. {
  501. VectorF scale;
  502. mathRead( *stream, &scale );
  503. setScale( scale );
  504. }
  505. else
  506. setScale( Point3F::One );
  507. }
  508. // DataBlockMask
  509. if ( stream->readFlag() )
  510. {
  511. GameBaseData *dptr = 0;
  512. SimObjectId id = stream->readRangedU32( DataBlockObjectIdFirst,
  513. DataBlockObjectIdLast );
  514. if ( stream->readFlag() )
  515. mOrderGUID = stream->readInt( 16 );
  516. if ( !Sim::findObject( id, dptr ) || !setDataBlock( dptr ) )
  517. con->setLastError( "Invalid packet GameBase::unpackUpdate()" );
  518. }
  519. #ifdef TORQUE_DEBUG_NET_MOVES
  520. stream->read(&mLastMoveId);
  521. mTicksSinceLastMove = 0;
  522. mIsAiControlled = stream->readFlag();
  523. #endif
  524. if (stream->readFlag())
  525. {
  526. scope_id = (stream->readFlag()) ? (U16) stream->readInt(SCOPE_ID_BITS) : 0;
  527. scope_refs = 0;
  528. }
  529. }
  530. void GameBase::onMount( SceneObject *obj, S32 node )
  531. {
  532. deleteNotify( obj );
  533. // Are we mounting to a GameBase object?
  534. GameBase *gbaseObj = dynamic_cast<GameBase*>( obj );
  535. if ( gbaseObj && gbaseObj->getControlObject() != this && gbaseObj->getControllingObject() != this)
  536. processAfter( gbaseObj );
  537. if (!isGhost()) {
  538. setMaskBits(MountedMask);
  539. mDataBlock->onMount_callback( this, obj, node );
  540. }
  541. }
  542. void GameBase::onUnmount( SceneObject *obj, S32 node )
  543. {
  544. clearNotify(obj);
  545. GameBase *gbaseObj = dynamic_cast<GameBase*>( obj );
  546. if ( gbaseObj && gbaseObj->getControlObject() != this )
  547. clearProcessAfter();
  548. if (!isGhost()) {
  549. setMaskBits(MountedMask);
  550. mDataBlock->onUnmount_callback( this, obj, node );
  551. }
  552. }
  553. bool GameBase::setDataBlockProperty( void *obj, const char *index, const char *db)
  554. {
  555. if( db == NULL || !db[ 0 ] )
  556. {
  557. Con::errorf( "GameBase::setDataBlockProperty - Can't unset datablock on GameBase objects" );
  558. return false;
  559. }
  560. GameBase* object = static_cast< GameBase* >( obj );
  561. GameBaseData* data;
  562. if( Sim::findObject( db, data ) )
  563. return object->setDataBlock( data );
  564. Con::errorf( "GameBase::setDatablockProperty - Could not find data block \"%s\"", db );
  565. return false;
  566. }
  567. MoveList* GameBase::getMoveList()
  568. {
  569. return mControllingClient ? mControllingClient->mMoveList : NULL;
  570. }
  571. //----------------------------------------------------------------------------
  572. DefineEngineMethod( GameBase, getDataBlock, S32, (),,
  573. "@brief Get the datablock used by this object.\n\n"
  574. "@return the datablock this GameBase is using."
  575. "@see setDataBlock()\n")
  576. {
  577. return object->getDataBlock()? object->getDataBlock()->getId(): 0;
  578. }
  579. //----------------------------------------------------------------------------
  580. DefineEngineMethod( GameBase, setDataBlock, bool, ( GameBaseData* data ),,
  581. "@brief Assign this GameBase to use the specified datablock.\n\n"
  582. "@param data new datablock to use\n"
  583. "@return true if successful, false if failed."
  584. "@see getDataBlock()\n")
  585. {
  586. return ( data && object->setDataBlock(data) );
  587. }
  588. //----------------------------------------------------------------------------
  589. void GameBase::initPersistFields()
  590. {
  591. addGroup( "Game" );
  592. addProtectedField( "dataBlock", TYPEID< GameBaseData >(), Offset(mDataBlock, GameBase),
  593. &setDataBlockProperty, &defaultProtectedGetFn,
  594. "Script datablock used for game objects." );
  595. endGroup( "Game" );
  596. Parent::initPersistFields();
  597. }
  598. void GameBase::consoleInit()
  599. {
  600. #ifdef TORQUE_DEBUG
  601. Con::addVariable( "GameBase::boundingBox", TypeBool, &gShowBoundingBox,
  602. "@brief Toggles on the rendering of the bounding boxes for certain types of objects in scene.\n\n"
  603. "@ingroup GameBase" );
  604. #endif
  605. }
  606. DefineEngineMethod( GameBase, applyImpulse, bool, ( Point3F pos, VectorF vel ),,
  607. "@brief Apply an impulse to this object as defined by a world position and velocity vector.\n\n"
  608. "@param pos impulse world position\n"
  609. "@param vel impulse velocity (impulse force F = m * v)\n"
  610. "@return Always true\n"
  611. "@note Not all objects that derrive from GameBase have this defined.\n")
  612. {
  613. object->applyImpulse(pos,vel);
  614. return true;
  615. }
  616. DefineEngineMethod( GameBase, applyRadialImpulse, void, ( Point3F origin, F32 radius, F32 magnitude ),,
  617. "@brief Applies a radial impulse to the object using the given origin and force.\n\n"
  618. "@param origin World point of origin of the radial impulse.\n"
  619. "@param radius The radius of the impulse area.\n"
  620. "@param magnitude The strength of the impulse.\n"
  621. "@note Not all objects that derrive from GameBase have this defined.\n")
  622. {
  623. object->applyRadialImpulse( origin, radius, magnitude );
  624. }