gameConnectionEvents.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 "core/dnet.h"
  28. #include "core/stream/bitStream.h"
  29. #include "console/consoleTypes.h"
  30. #include "console/simBase.h"
  31. #include "scene/pathManager.h"
  32. #include "scene/sceneManager.h"
  33. #include "sfx/sfxSystem.h"
  34. #include "sfx/sfxDescription.h"
  35. #include "app/game.h"
  36. #include "T3D/gameBase/gameConnection.h"
  37. #include "T3D/gameBase/gameConnectionEvents.h"
  38. #include "console/engineAPI.h"
  39. #define DebugChecksum 0xF00DBAAD
  40. //#define DEBUG_SPEW
  41. //--------------------------------------------------------------------------
  42. IMPLEMENT_CO_CLIENTEVENT_V1(SimDataBlockEvent);
  43. IMPLEMENT_CO_CLIENTEVENT_V1(SimSoundAssetEvent);
  44. IMPLEMENT_CO_CLIENTEVENT_V1(Sim2DAudioEvent);
  45. IMPLEMENT_CO_CLIENTEVENT_V1(Sim3DAudioEvent);
  46. IMPLEMENT_CO_CLIENTEVENT_V1(SetMissionCRCEvent);
  47. ConsoleDocClass( SimDataBlockEvent,
  48. "@brief Use by GameConnection to process incoming datablocks.\n\n"
  49. "Not intended for game development, internal use only, but does expose onDataBlockObjectReceived.\n\n "
  50. "@internal");
  51. ConsoleDocClass( Sim2DAudioEvent,
  52. "@brief Use by GameConnection to send a 2D sound event over the network.\n\n"
  53. "Not intended for game development, internal use only, but does expose GameConnection::play2D.\n\n "
  54. "@internal");
  55. ConsoleDocClass( Sim3DAudioEvent,
  56. "@brief Use by GameConnection to send a 3D sound event over the network.\n\n"
  57. "Not intended for game development, internal use only, but does expose GameConnection::play3D.\n\n "
  58. "@internal");
  59. ConsoleDocClass( SetMissionCRCEvent,
  60. "@brief Use by GameConnection to send a 3D sound event over the network.\n\n"
  61. "Not intended for game development, internal use only, but does expose GameConnection::setMissionCRC.\n\n "
  62. "@internal");
  63. //----------------------------------------------------------------------------
  64. SimDataBlockEvent::SimDataBlockEvent(SimDataBlock* obj, U32 index, U32 total, U32 missionSequence)
  65. {
  66. mObj = NULL;
  67. mIndex = index;
  68. mTotal = total;
  69. mMissionSequence = missionSequence;
  70. mProcess = false;
  71. if(obj)
  72. {
  73. id = obj->getId();
  74. AssertFatal(id >= DataBlockObjectIdFirst && id <= DataBlockObjectIdLast,
  75. "Out of range event data block id... check simBase.h");
  76. #ifdef DEBUG_SPEW
  77. Con::printf("queuing data block: %d", mIndex);
  78. #endif
  79. }
  80. }
  81. SimDataBlockEvent::~SimDataBlockEvent()
  82. {
  83. if( mObj )
  84. delete mObj;
  85. }
  86. #ifdef TORQUE_DEBUG_NET
  87. const char *SimDataBlockEvent::getDebugName()
  88. {
  89. SimObject *obj = Sim::findObject(id);
  90. static char buffer[256];
  91. dSprintf(buffer, sizeof(buffer), "%s [%s - %s]",
  92. getClassName(),
  93. obj ? obj->getName() : "",
  94. obj ? obj->getClassName() : "NONE");
  95. return buffer;
  96. }
  97. #endif // TORQUE_DEBUG_NET
  98. void SimDataBlockEvent::notifyDelivered(NetConnection *conn, bool )
  99. {
  100. // if the modified key for this event is not the current one,
  101. // we've already resorted and resent some blocks, so fall out.
  102. if(conn->isRemoved())
  103. return;
  104. GameConnection *gc = (GameConnection *) conn;
  105. if(gc->getDataBlockSequence() != mMissionSequence)
  106. return;
  107. U32 nextIndex = mIndex + DataBlockQueueCount;
  108. SimDataBlockGroup *g = Sim::getDataBlockGroup();
  109. if(mIndex == g->size() - 1)
  110. {
  111. gc->setDataBlockModifiedKey(gc->getMaxDataBlockModifiedKey());
  112. gc->sendConnectionMessage(GameConnection::DataBlocksDone, mMissionSequence);
  113. }
  114. if(g->size() <= nextIndex)
  115. return;
  116. SimDataBlock *blk = (SimDataBlock *) (*g)[nextIndex];
  117. gc->postNetEvent(new SimDataBlockEvent(blk, nextIndex, g->size(), mMissionSequence));
  118. }
  119. void SimDataBlockEvent::pack(NetConnection *conn, BitStream *bstream)
  120. {
  121. #ifdef AFX_CAP_DATABLOCK_CACHE
  122. ((GameConnection *)conn)->tempDisableStringBuffering(bstream);
  123. #endif
  124. SimDataBlock* obj;
  125. Sim::findObject(id,obj);
  126. GameConnection *gc = (GameConnection *) conn;
  127. if(bstream->writeFlag(gc->getDataBlockModifiedKey() < obj->getModifiedKey()))
  128. {
  129. if(obj->getModifiedKey() > gc->getMaxDataBlockModifiedKey())
  130. gc->setMaxDataBlockModifiedKey(obj->getModifiedKey());
  131. AssertFatal(obj,
  132. "SimDataBlockEvent:: Data blocks cannot be deleted");
  133. bstream->writeInt(id - DataBlockObjectIdFirst,DataBlockObjectIdBitSize);
  134. S32 classId = obj->getClassId(conn->getNetClassGroup());
  135. bstream->writeClassId(classId, NetClassTypeDataBlock, conn->getNetClassGroup());
  136. bstream->writeInt(mIndex, DataBlockObjectIdBitSize);
  137. bstream->writeInt(mTotal, DataBlockObjectIdBitSize + 1);
  138. obj->packData(bstream);
  139. #ifdef TORQUE_DEBUG_NET
  140. bstream->writeInt(classId ^ DebugChecksum, 32);
  141. #endif
  142. }
  143. #ifdef AFX_CAP_DATABLOCK_CACHE
  144. ((GameConnection *)conn)->restoreStringBuffering(bstream);
  145. #endif
  146. }
  147. void SimDataBlockEvent::unpack(NetConnection *cptr, BitStream *bstream)
  148. {
  149. #ifdef AFX_CAP_DATABLOCK_CACHE
  150. // stash the stream position prior to unpacking
  151. S32 start_pos = bstream->getCurPos();
  152. ((GameConnection *)cptr)->tempDisableStringBuffering(bstream);
  153. #endif
  154. if(bstream->readFlag())
  155. {
  156. mProcess = true;
  157. id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  158. S32 classId = bstream->readClassId(NetClassTypeDataBlock, cptr->getNetClassGroup());
  159. mIndex = bstream->readInt(DataBlockObjectIdBitSize);
  160. mTotal = bstream->readInt(DataBlockObjectIdBitSize + 1);
  161. SimObject* ptr;
  162. if( Sim::findObject( id, ptr ) )
  163. {
  164. // An object with the given ID already exists. Make sure it has the right class.
  165. AbstractClassRep* classRep = AbstractClassRep::findClassRep( cptr->getNetClassGroup(), NetClassTypeDataBlock, classId );
  166. if( classRep && String::compare( classRep->getClassName(), ptr->getClassName() ) != 0 )
  167. {
  168. Con::warnf( "A '%s' datablock with id: %d already existed. "
  169. "Clobbering it with new '%s' datablock from server.",
  170. ptr->getClassName(), id, classRep->getClassName() );
  171. ptr->deleteObject();
  172. ptr = NULL;
  173. }
  174. }
  175. if( !ptr )
  176. ptr = ( SimObject* ) ConsoleObject::create( cptr->getNetClassGroup(), NetClassTypeDataBlock, classId );
  177. mObj = dynamic_cast< SimDataBlock* >( ptr );
  178. if( mObj != NULL )
  179. {
  180. #ifdef DEBUG_SPEW
  181. Con::printf(" - SimDataBlockEvent: unpacking event of type: %s", mObj->getClassName());
  182. #endif
  183. mObj->unpackData( bstream );
  184. }
  185. else
  186. {
  187. #ifdef DEBUG_SPEW
  188. Con::printf(" - SimDataBlockEvent: INVALID PACKET! Could not create class with classID: %d", classId);
  189. #endif
  190. delete ptr;
  191. cptr->setLastError("Invalid packet in SimDataBlockEvent::unpack()");
  192. }
  193. #ifdef TORQUE_DEBUG_NET
  194. U32 checksum = bstream->readInt(32);
  195. AssertISV( (checksum ^ DebugChecksum) == (U32)classId,
  196. avar("unpack did not match pack for event of class %s.",
  197. mObj->getClassName()) );
  198. #endif
  199. }
  200. #ifdef AFX_CAP_DATABLOCK_CACHE
  201. // rewind to stream position and then process raw bytes for caching
  202. ((GameConnection *)cptr)->repackClientDatablock(bstream, start_pos);
  203. ((GameConnection *)cptr)->restoreStringBuffering(bstream);
  204. #endif
  205. }
  206. void SimDataBlockEvent::write(NetConnection *cptr, BitStream *bstream)
  207. {
  208. if(bstream->writeFlag(mProcess))
  209. {
  210. bstream->writeInt(id - DataBlockObjectIdFirst,DataBlockObjectIdBitSize);
  211. S32 classId = mObj->getClassId(cptr->getNetClassGroup());
  212. bstream->writeClassId(classId, NetClassTypeDataBlock, cptr->getNetClassGroup());
  213. bstream->writeInt(mIndex, DataBlockObjectIdBitSize);
  214. bstream->writeInt(mTotal, DataBlockObjectIdBitSize + 1);
  215. mObj->packData(bstream);
  216. }
  217. }
  218. void SimDataBlockEvent::process(NetConnection *cptr)
  219. {
  220. if(mProcess)
  221. {
  222. //call the console function to set the number of blocks to be sent
  223. Con::executef("onDataBlockObjectReceived", mIndex, mTotal);
  224. String &errorBuffer = NetConnection::getErrorBuffer();
  225. // Register the datablock object if this is a new DB
  226. // and not for a modified datablock event.
  227. if( !mObj->isProperlyAdded() )
  228. {
  229. // This is a fresh datablock object.
  230. // Perform preload on datablock and register
  231. // the object.
  232. GameConnection* conn = dynamic_cast< GameConnection* >( cptr );
  233. if( conn )
  234. conn->preloadDataBlock( mObj );
  235. if( mObj->registerObject(id) )
  236. {
  237. cptr->addObject( mObj );
  238. mObj = NULL;
  239. }
  240. }
  241. else
  242. {
  243. // This is an update to an existing datablock. Preload
  244. // to finish this.
  245. mObj->preload( false, errorBuffer );
  246. mObj = NULL;
  247. }
  248. }
  249. }
  250. //----------------------------------------------------------------------------
  251. static F32 SoundPosAccuracy = 0.5;
  252. static S32 SoundRotBits = 8;
  253. SimSoundAssetEvent::SimSoundAssetEvent(StringTableEntry assetId, const MatrixF& mat)
  254. {
  255. // cant get here unless the asset is declared.
  256. mAsset = assetId;
  257. if (mat)
  258. mTransform = mat;
  259. }
  260. void SimSoundAssetEvent::pack(NetConnection* con, BitStream* stream)
  261. {
  262. NetStringHandle assetIdStr = mAsset->getAssetId();
  263. con->packNetStringHandleU(stream, assetIdStr);
  264. // only stream if this is a 3d sound asset.
  265. if (mAsset->is3D())
  266. {
  267. SFXDescription* ad = mAsset->getSfxDescription();
  268. if (stream->writeFlag(ad->mConeInsideAngle || ad->mConeOutsideAngle))
  269. {
  270. QuatF q(mTransform);
  271. q.normalize();
  272. // LH - we can get a valid quat that's very slightly over 1 in and so
  273. // this fails (barely) check against zero. So use some error-
  274. AssertFatal((1.0 - ((q.x * q.x) + (q.y * q.y) + (q.z * q.z))) >= (0.0 - 0.001),
  275. "QuatF::normalize() is broken in Sim3DAudioEvent");
  276. stream->writeSignedFloat(q.x, SoundRotBits);
  277. stream->writeSignedFloat(q.y, SoundRotBits);
  278. stream->writeSignedFloat(q.z, SoundRotBits);
  279. stream->writeFlag(q.w < 0.0);
  280. }
  281. Point3F pos;
  282. mTransform.getColumn(3, &pos);
  283. stream->writeCompressedPoint(pos, SoundPosAccuracy);
  284. }
  285. }
  286. void SimSoundAssetEvent::write(NetConnection* con, BitStream* stream)
  287. {
  288. // Just do the normal pack...
  289. pack(con, stream);
  290. }
  291. void SimSoundAssetEvent::unpack(NetConnection* con, BitStream* stream)
  292. {
  293. StringTableEntry temp = StringTable->insert(con->unpackNetStringHandleU(stream).getString());
  294. if (AssetDatabase.isDeclaredAsset(temp))
  295. {
  296. AssetPtr<SoundAsset> tempSoundAsset;
  297. tempSoundAsset = temp;
  298. mAsset = temp;
  299. }
  300. if (mAsset->is3D())
  301. {
  302. if (stream->readFlag()) {
  303. QuatF q;
  304. q.x = stream->readSignedFloat(SoundRotBits);
  305. q.y = stream->readSignedFloat(SoundRotBits);
  306. q.z = stream->readSignedFloat(SoundRotBits);
  307. F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z));
  308. // #ifdef __linux
  309. // Hmm, this should never happen, but it does...
  310. if (value > 1.f)
  311. value = 1.f;
  312. // #endif
  313. q.w = mSqrt(1.f - value);
  314. if (stream->readFlag())
  315. q.w = -q.w;
  316. q.setMatrix(&mTransform);
  317. }
  318. else
  319. mTransform.identity();
  320. Point3F pos;
  321. stream->readCompressedPoint(&pos, SoundPosAccuracy);
  322. mTransform.setColumn(3, pos);
  323. }
  324. }
  325. void SimSoundAssetEvent::process(NetConnection* con)
  326. {
  327. if (mAsset->is3D())
  328. SFX->playOnce(mAsset->getSfxProfile(), &mTransform);
  329. else
  330. SFX->playOnce(mAsset->getSfxProfile());
  331. }
  332. Sim2DAudioEvent::Sim2DAudioEvent(SFXProfile *profile)
  333. {
  334. mProfile = profile;
  335. }
  336. void Sim2DAudioEvent::pack(NetConnection *, BitStream *bstream)
  337. {
  338. bstream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  339. }
  340. void Sim2DAudioEvent::write(NetConnection *, BitStream *bstream)
  341. {
  342. bstream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  343. }
  344. void Sim2DAudioEvent::unpack(NetConnection *, BitStream *bstream)
  345. {
  346. SimObjectId id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  347. Sim::findObject(id, mProfile);
  348. }
  349. void Sim2DAudioEvent::process(NetConnection *)
  350. {
  351. if (mProfile)
  352. SFX->playOnce( mProfile );
  353. }
  354. Sim3DAudioEvent::Sim3DAudioEvent(SFXProfile *profile,const MatrixF* mat)
  355. {
  356. mProfile = profile;
  357. if (mat)
  358. mTransform = *mat;
  359. }
  360. void Sim3DAudioEvent::pack(NetConnection *con, BitStream *bstream)
  361. {
  362. bstream->writeInt(mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  363. // If the sound has cone parameters, the orientation is
  364. // transmitted as well.
  365. SFXDescription* ad = mProfile->getDescription();
  366. if ( bstream->writeFlag( ad->mConeInsideAngle || ad->mConeOutsideAngle ) )
  367. {
  368. QuatF q(mTransform);
  369. q.normalize();
  370. // LH - we can get a valid quat that's very slightly over 1 in and so
  371. // this fails (barely) check against zero. So use some error-
  372. AssertFatal((1.0 - ((q.x * q.x) + (q.y * q.y) + (q.z * q.z))) >= (0.0 - 0.001),
  373. "QuatF::normalize() is broken in Sim3DAudioEvent");
  374. bstream->writeSignedFloat(q.x,SoundRotBits);
  375. bstream->writeSignedFloat(q.y,SoundRotBits);
  376. bstream->writeSignedFloat(q.z,SoundRotBits);
  377. bstream->writeFlag(q.w < 0.0);
  378. }
  379. Point3F pos;
  380. mTransform.getColumn(3,&pos);
  381. bstream->writeCompressedPoint(pos,SoundPosAccuracy);
  382. }
  383. void Sim3DAudioEvent::write(NetConnection *con, BitStream *bstream)
  384. {
  385. // Just do the normal pack...
  386. pack(con,bstream);
  387. }
  388. void Sim3DAudioEvent::unpack(NetConnection *con, BitStream *bstream)
  389. {
  390. SimObjectId id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  391. Sim::findObject(id, mProfile);
  392. if (bstream->readFlag()) {
  393. QuatF q;
  394. q.x = bstream->readSignedFloat(SoundRotBits);
  395. q.y = bstream->readSignedFloat(SoundRotBits);
  396. q.z = bstream->readSignedFloat(SoundRotBits);
  397. F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z));
  398. // #ifdef __linux
  399. // Hmm, this should never happen, but it does...
  400. if ( value > 1.f )
  401. value = 1.f;
  402. // #endif
  403. q.w = mSqrt(1.f - value);
  404. if (bstream->readFlag())
  405. q.w = -q.w;
  406. q.setMatrix(&mTransform);
  407. }
  408. else
  409. mTransform.identity();
  410. Point3F pos;
  411. bstream->readCompressedPoint(&pos,SoundPosAccuracy);
  412. mTransform.setColumn(3, pos);
  413. }
  414. void Sim3DAudioEvent::process(NetConnection *)
  415. {
  416. if (mProfile)
  417. SFX->playOnce( mProfile, &mTransform );
  418. }