gameBase.cpp 21 KB

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