gameBase.cpp 22 KB

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