gameBase.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. mCategory = "";
  113. mPacked = false;
  114. }
  115. GameBaseData::GameBaseData(const GameBaseData& other, bool temp_clone) : SimDataBlock(other, temp_clone)
  116. {
  117. mPacked = other.mPacked;
  118. mCategory = other.mCategory;
  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(mCategory, 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. mPacked = false;
  148. return true;
  149. }
  150. void GameBaseData::unpackData(BitStream* stream)
  151. {
  152. Parent::unpackData(stream);
  153. mPacked = 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. mCameraFov = 90.f;
  221. }
  222. GameBase::~GameBase()
  223. {
  224. #ifdef TORQUE_AFX_ENABLED
  225. if (mScope_registered)
  226. arcaneFX::unregisterScopedObject(this);
  227. #endif
  228. }
  229. //----------------------------------------------------------------------------
  230. bool GameBase::onAdd()
  231. {
  232. if ( !Parent::onAdd() )
  233. return false;
  234. // Datablock must be initialized on the server.
  235. // Client datablock are initialized by the initial update.
  236. #ifdef TORQUE_AFX_ENABLED
  237. if (isClientObject())
  238. {
  239. if (mScope_id > 0 && !mScope_registered)
  240. arcaneFX::registerScopedObject(this);
  241. }
  242. else
  243. {
  244. if ( mDataBlock && !onNewDataBlock( mDataBlock, false ) )
  245. return false;
  246. }
  247. #else
  248. if ( isServerObject() && mDataBlock && !onNewDataBlock( mDataBlock, false ) )
  249. return false;
  250. #endif
  251. setProcessTick( true );
  252. return true;
  253. }
  254. void GameBase::onRemove()
  255. {
  256. #ifdef TORQUE_AFX_ENABLED
  257. if (mScope_registered)
  258. arcaneFX::unregisterScopedObject(this);
  259. #endif
  260. // EDITOR FEATURE: Remove us from the reload signal of our datablock.
  261. if ( mDataBlock )
  262. mDataBlock->mReloadSignal.remove( this, &GameBase::_onDatablockModified );
  263. Parent::onRemove();
  264. }
  265. bool GameBase::onNewDataBlock( GameBaseData *dptr, bool reload )
  266. {
  267. // EDITOR FEATURE: Remove us from old datablock's reload signal and
  268. // add us to the new one.
  269. if ( !reload )
  270. {
  271. if ( mDataBlock )
  272. mDataBlock->mReloadSignal.remove( this, &GameBase::_onDatablockModified );
  273. if ( dptr )
  274. dptr->mReloadSignal.notify( this, &GameBase::_onDatablockModified );
  275. }
  276. mDataBlock = dptr;
  277. if ( !mDataBlock )
  278. return false;
  279. #ifdef TORQUE_AFX_ENABLED
  280. // Don't set mask when new datablock is a temp-clone.
  281. if (mDataBlock->isTempClone())
  282. return true;
  283. #endif
  284. setMaskBits(DataBlockMask);
  285. return true;
  286. }
  287. void GameBase::_onDatablockModified()
  288. {
  289. AssertFatal( mDataBlock, "GameBase::onDatablockModified - mDataBlock is NULL." );
  290. onNewDataBlock( mDataBlock, true );
  291. }
  292. void GameBase::inspectPostApply()
  293. {
  294. Parent::inspectPostApply();
  295. setMaskBits(ExtendedInfoMask);
  296. }
  297. //----------------------------------------------------------------------------
  298. void GameBase::processTick(const Move * move)
  299. {
  300. #ifdef TORQUE_DEBUG_NET_MOVES
  301. if (!move)
  302. mTicksSinceLastMove++;
  303. const char * srv = isClientObject() ? "client" : "server";
  304. const char * who = "";
  305. if (isClientObject())
  306. {
  307. if (this == (GameBase*)GameConnection::getConnectionToServer()->getControlObject())
  308. who = " player";
  309. else
  310. who = " ghost";
  311. if (mIsAiControlled)
  312. who = " ai";
  313. }
  314. if (isServerObject())
  315. {
  316. if (dynamic_cast<AIConnection*>(getControllingClient()))
  317. {
  318. who = " ai";
  319. mIsAiControlled = true;
  320. }
  321. else if (getControllingClient())
  322. {
  323. who = " player";
  324. mIsAiControlled = false;
  325. }
  326. else
  327. {
  328. who = "";
  329. mIsAiControlled = false;
  330. }
  331. }
  332. U32 moveid = mLastMoveId+mTicksSinceLastMove;
  333. if (move)
  334. moveid = move->id;
  335. if (getTypeMask() & GameBaseHiFiObjectType)
  336. {
  337. if (move)
  338. Con::printf("Processing (%s%s id %i) move %i",srv,who,getId(), move->id);
  339. else
  340. Con::printf("Processing (%s%s id %i) move %i (%i)",srv,who,getId(),mLastMoveId+mTicksSinceLastMove,mTicksSinceLastMove);
  341. }
  342. if (move)
  343. {
  344. mLastMoveId = move->id;
  345. mTicksSinceLastMove=0;
  346. }
  347. #endif
  348. }
  349. void GameBase::interpolateTick(F32 dt)
  350. {
  351. // PATHSHAPE
  352. updateRenderChangesByParent();
  353. // PATHSHAPE END
  354. }
  355. //----------------------------------------------------------------------------
  356. F32 GameBase::getUpdatePriority(CameraScopeQuery *camInfo, U32 updateMask, S32 updateSkips)
  357. {
  358. TORQUE_UNUSED(updateMask);
  359. // Calculate a priority used to decide if this object
  360. // will be updated on the client. All the weights
  361. // are calculated 0 -> 1 Then weighted together at the
  362. // end to produce a priority.
  363. Point3F pos;
  364. getWorldBox().getCenter(&pos);
  365. pos -= camInfo->pos;
  366. F32 dist = pos.len();
  367. if (dist == 0.0f) dist = 0.001f;
  368. pos *= 1.0f / dist;
  369. // Weight based on linear distance, the basic stuff.
  370. F32 wDistance = (dist < camInfo->visibleDistance)?
  371. 1.0f - (dist / camInfo->visibleDistance): 0.0f;
  372. // Weight by field of view, objects directly in front
  373. // will be weighted 1, objects behind will be 0
  374. F32 dot = mDot(pos,camInfo->orientation);
  375. bool inFov = dot > camInfo->cosFov * 1.5f;
  376. F32 wFov = inFov? 1.0f: 0;
  377. // Weight by linear velocity parallel to the viewing plane
  378. // (if it's the field of view, 0 if it's not).
  379. F32 wVelocity = 0.0f;
  380. if (inFov)
  381. {
  382. Point3F vec;
  383. mCross(camInfo->orientation,getVelocity(),&vec);
  384. wVelocity = (vec.len() * camInfo->fov) /
  385. (camInfo->fov * camInfo->visibleDistance);
  386. if (wVelocity > 1.0f)
  387. wVelocity = 1.0f;
  388. }
  389. // Weight by interest.
  390. F32 wInterest;
  391. if (getTypeMask() & (PlayerObjectType || VehicleObjectType ))
  392. wInterest = 0.75f;
  393. else if (getTypeMask() & ProjectileObjectType)
  394. {
  395. // Projectiles are more interesting if they
  396. // are heading for us.
  397. wInterest = 0.30f;
  398. dot = -mDot(pos,getVelocity());
  399. if (dot > 0.0f)
  400. wInterest += 0.20 * dot;
  401. }
  402. else
  403. {
  404. if (getTypeMask() & ItemObjectType)
  405. wInterest = 0.25f;
  406. else
  407. // Everything else is less interesting.
  408. wInterest = 0.0f;
  409. }
  410. // Weight by updateSkips
  411. F32 wSkips = updateSkips * 0.5;
  412. // Calculate final priority, should total to about 1.0f (plus children)
  413. //
  414. return
  415. wFov * sUpFov +
  416. wDistance * sUpDistance +
  417. wVelocity * sUpVelocity +
  418. wSkips * sUpSkips +
  419. wInterest * sUpInterest +
  420. getNumChildren();
  421. }
  422. //----------------------------------------------------------------------------
  423. bool GameBase::setDataBlock(GameBaseData* dptr)
  424. {
  425. if (isGhost() || isProperlyAdded()) {
  426. if (mDataBlock != dptr)
  427. return onNewDataBlock(dptr,false);
  428. }
  429. else
  430. mDataBlock = dptr;
  431. return true;
  432. }
  433. //--------------------------------------------------------------------------
  434. void GameBase::scriptOnAdd()
  435. {
  436. // Script onAdd() must be called by the leaf class after
  437. // everything is ready.
  438. if (mDataBlock && !isGhost())
  439. mDataBlock->onAdd_callback( this );
  440. }
  441. void GameBase::scriptOnNewDataBlock()
  442. {
  443. // Script onNewDataBlock() must be called by the leaf class
  444. // after everything is loaded.
  445. if (mDataBlock && !isGhost())
  446. mDataBlock->onNewDataBlock_callback( this );
  447. }
  448. void GameBase::scriptOnRemove()
  449. {
  450. // Script onRemove() must be called by leaf class while
  451. // the object state is still valid.
  452. if (!isGhost() && mDataBlock)
  453. mDataBlock->onRemove_callback( this );
  454. }
  455. //----------------------------------------------------------------------------
  456. void GameBase::setControllingClient(GameConnection* client)
  457. {
  458. if (isClientObject())
  459. {
  460. if (mControllingClient)
  461. setControl_callback( 0 );
  462. if (client)
  463. setControl_callback( 1 );
  464. }
  465. mControllingClient = client;
  466. }
  467. U32 GameBase::getPacketDataChecksum(GameConnection * connection)
  468. {
  469. // just write the packet data into a buffer
  470. // then we can CRC the buffer. This should always let us
  471. // know when there is a checksum problem.
  472. static U8 buffer[1500] = { 0, };
  473. BitStream stream(buffer, sizeof(buffer));
  474. writePacketData(connection, &stream);
  475. U32 byteCount = stream.getPosition();
  476. U32 ret = CRC::calculateCRC(buffer, byteCount, 0xFFFFFFFF);
  477. dMemset(buffer, 0, byteCount);
  478. return ret;
  479. }
  480. void GameBase::writePacketData(GameConnection*, BitStream*)
  481. {
  482. }
  483. void GameBase::readPacketData(GameConnection*, BitStream*)
  484. {
  485. }
  486. U32 GameBase::packUpdate( NetConnection *connection, U32 mask, BitStream *stream )
  487. {
  488. U32 retMask = Parent::packUpdate( connection, mask, stream );
  489. if ( stream->writeFlag( mask & ScaleMask ) )
  490. {
  491. // Only write one bit if the scale is one.
  492. if ( stream->writeFlag( mObjScale != Point3F::One ) )
  493. mathWrite( *stream, mObjScale );
  494. }
  495. if ( stream->writeFlag( ( mask & DataBlockMask ) && mDataBlock != NULL ) )
  496. {
  497. stream->writeRangedU32( mDataBlock->getId(),
  498. DataBlockObjectIdFirst,
  499. DataBlockObjectIdLast );
  500. if ( stream->writeFlag( mNetFlags.test( NetOrdered ) ) )
  501. stream->writeInt( mOrderGUID, 16 );
  502. }
  503. #ifdef TORQUE_DEBUG_NET_MOVES
  504. stream->write(mLastMoveId);
  505. stream->writeFlag(mIsAiControlled);
  506. #endif
  507. #ifdef TORQUE_AFX_ENABLED
  508. if (stream->writeFlag(mask & ScopeIdMask))
  509. {
  510. if (stream->writeFlag(mScope_refs > 0))
  511. stream->writeInt(mScope_id, SCOPE_ID_BITS);
  512. }
  513. #endif
  514. return retMask;
  515. }
  516. void GameBase::unpackUpdate(NetConnection *con, BitStream *stream)
  517. {
  518. Parent::unpackUpdate( con, stream );
  519. // ScaleMask
  520. if ( stream->readFlag() )
  521. {
  522. if ( stream->readFlag() )
  523. {
  524. VectorF scale;
  525. mathRead( *stream, &scale );
  526. setScale( scale );
  527. }
  528. else
  529. setScale( Point3F::One );
  530. }
  531. // DataBlockMask
  532. if ( stream->readFlag() )
  533. {
  534. GameBaseData *dptr = 0;
  535. SimObjectId id = stream->readRangedU32( DataBlockObjectIdFirst,
  536. DataBlockObjectIdLast );
  537. if ( stream->readFlag() )
  538. mOrderGUID = stream->readInt( 16 );
  539. if ( !Sim::findObject( id, dptr ) || !setDataBlock( dptr ) )
  540. con->setLastError( "Invalid packet GameBase::unpackUpdate()" );
  541. }
  542. #ifdef TORQUE_DEBUG_NET_MOVES
  543. stream->read(&mLastMoveId);
  544. mTicksSinceLastMove = 0;
  545. mIsAiControlled = stream->readFlag();
  546. #endif
  547. #ifdef TORQUE_AFX_ENABLED
  548. if (stream->readFlag())
  549. {
  550. mScope_id = (stream->readFlag()) ? (U16) stream->readInt(SCOPE_ID_BITS) : 0;
  551. mScope_refs = 0;
  552. }
  553. #endif
  554. }
  555. void GameBase::onMount( SceneObject *obj, S32 node )
  556. {
  557. deleteNotify( obj );
  558. // Are we mounting to a GameBase object?
  559. GameBase *gbaseObj = dynamic_cast<GameBase*>( obj );
  560. if ( gbaseObj && gbaseObj->getControlObject() != this && gbaseObj->getControllingObject() != this)
  561. processAfter( gbaseObj );
  562. if (!isGhost()) {
  563. setMaskBits(MountedMask);
  564. mDataBlock->onMount_callback( this, obj, node );
  565. }
  566. }
  567. void GameBase::onUnmount( SceneObject *obj, S32 node )
  568. {
  569. clearNotify(obj);
  570. GameBase *gbaseObj = dynamic_cast<GameBase*>( obj );
  571. if ( gbaseObj && gbaseObj->getControlObject() != this )
  572. clearProcessAfter();
  573. if (!isGhost()) {
  574. setMaskBits(MountedMask);
  575. mDataBlock->onUnmount_callback( this, obj, node );
  576. }
  577. }
  578. bool GameBase::setDataBlockProperty( void *obj, const char *index, const char *db)
  579. {
  580. if( db == NULL || !db[ 0 ] )
  581. {
  582. Con::errorf( "GameBase::setDataBlockProperty - Can't unset datablock on GameBase objects" );
  583. return false;
  584. }
  585. GameBase* object = static_cast< GameBase* >( obj );
  586. GameBaseData* data;
  587. if( Sim::findObject( db, data ) )
  588. return object->setDataBlock( data );
  589. Con::errorf( "GameBase::setDatablockProperty - Could not find data block \"%s\"", db );
  590. return false;
  591. }
  592. MoveList* GameBase::getMoveList()
  593. {
  594. return mControllingClient ? mControllingClient->mMoveList : NULL;
  595. }
  596. //----------------------------------------------------------------------------
  597. DefineEngineMethod( GameBase, getDataBlock, S32, (),,
  598. "@brief Get the datablock used by this object.\n\n"
  599. "@return the datablock this GameBase is using."
  600. "@see setDataBlock()\n")
  601. {
  602. return object->getDataBlock()? object->getDataBlock()->getId(): 0;
  603. }
  604. //----------------------------------------------------------------------------
  605. DefineEngineMethod( GameBase, setDataBlock, bool, ( GameBaseData* data ),,
  606. "@brief Assign this GameBase to use the specified datablock.\n\n"
  607. "@param data new datablock to use\n"
  608. "@return true if successful, false if failed."
  609. "@see getDataBlock()\n")
  610. {
  611. return ( data && object->setDataBlock(data) );
  612. }
  613. //----------------------------------------------------------------------------
  614. void GameBase::initPersistFields()
  615. {
  616. addGroup( "Game" );
  617. addProtectedField( "dataBlock", TYPEID< GameBaseData >(), Offset(mDataBlock, GameBase),
  618. &setDataBlockProperty, &defaultProtectedGetFn,
  619. "Script datablock used for game objects." );
  620. endGroup( "Game" );
  621. Parent::initPersistFields();
  622. }
  623. void GameBase::consoleInit()
  624. {
  625. #ifdef TORQUE_DEBUG
  626. Con::addVariable( "GameBase::boundingBox", TypeBool, &gShowBoundingBox,
  627. "@brief Toggles on the rendering of the bounding boxes for certain types of objects in scene.\n\n"
  628. "@ingroup GameBase" );
  629. #endif
  630. }
  631. DefineEngineMethod( GameBase, applyImpulse, bool, ( Point3F pos, VectorF vel ),,
  632. "@brief Apply an impulse to this object as defined by a world position and velocity vector.\n\n"
  633. "@param pos impulse world position\n"
  634. "@param vel impulse velocity (impulse force F = m * v)\n"
  635. "@return Always true\n"
  636. "@note Not all objects that derrive from GameBase have this defined.\n")
  637. {
  638. object->applyImpulse(pos,vel);
  639. return true;
  640. }
  641. DefineEngineMethod( GameBase, applyRadialImpulse, void, ( Point3F origin, F32 radius, F32 magnitude ),,
  642. "@brief Applies a radial impulse to the object using the given origin and force.\n\n"
  643. "@param origin World point of origin of the radial impulse.\n"
  644. "@param radius The radius of the impulse area.\n"
  645. "@param magnitude The strength of the impulse.\n"
  646. "@note Not all objects that derrive from GameBase have this defined.\n")
  647. {
  648. object->applyRadialImpulse( origin, radius, magnitude );
  649. }
  650. // PATHSHAPE
  651. // Console Methods for attach children. can't put them in sceneobject because //
  652. // we want the processafter functions////////////////////////////////////////////
  653. DefineEngineMethod(GameBase, attachChild, bool, (GameBase* _subObject), (nullAsType<GameBase*>()), "(SceneObject subObject)"
  654. "attach an object to this one, preserving its present transform.")
  655. {
  656. if (_subObject != nullptr)
  657. {
  658. if (_subObject->getParent() != object){
  659. Con::errorf("Object is (%d)", _subObject->getId());
  660. _subObject->clearProcessAfter();
  661. _subObject->processAfter(object);
  662. return object->attachChild(_subObject);
  663. }
  664. else
  665. return false;
  666. }
  667. else
  668. {
  669. Con::errorf("Couldn't addObject()!");
  670. return false;
  671. }
  672. }
  673. DefineEngineMethod(GameBase, detachChild, bool, (GameBase* _subObject), (nullAsType<GameBase*>()), "(SceneObject subObject)"
  674. "attach an object to this one, preserving its present transform.")
  675. {
  676. if (_subObject != nullptr)
  677. {
  678. _subObject->clearProcessAfter();
  679. return _subObject->attachToParent(NULL);
  680. }
  681. else
  682. {
  683. return false;
  684. }
  685. }//end
  686. // PATHSHAPE END