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. sentTransform = false;
  258. if (mat)
  259. {
  260. mTransform = *mat;
  261. sentTransform = true;
  262. }
  263. }
  264. void SimSoundAssetEvent::pack(NetConnection* con, BitStream* stream)
  265. {
  266. NetStringHandle assetIdStr = mAsset->getAssetId();
  267. con->packNetStringHandleU(stream, assetIdStr);
  268. SFXDescription* ad = mAsset->getSfxDescription();
  269. if (stream->writeFlag(sentTransform))
  270. {
  271. if (stream->writeFlag(ad->mConeInsideAngle || ad->mConeOutsideAngle))
  272. {
  273. QuatF q(mTransform);
  274. q.normalize();
  275. // LH - we can get a valid quat that's very slightly over 1 in and so
  276. // this fails (barely) check against zero. So use some error-
  277. AssertFatal((1.0 - ((q.x * q.x) + (q.y * q.y) + (q.z * q.z))) >= (0.0 - 0.001),
  278. "QuatF::normalize() is broken in Sim3DAudioEvent");
  279. stream->writeSignedFloat(q.x, SoundRotBits);
  280. stream->writeSignedFloat(q.y, SoundRotBits);
  281. stream->writeSignedFloat(q.z, SoundRotBits);
  282. stream->writeFlag(q.w < 0.0);
  283. }
  284. Point3F pos;
  285. mTransform.getColumn(3, &pos);
  286. stream->writeCompressedPoint(pos, SoundPosAccuracy);
  287. }
  288. }
  289. void SimSoundAssetEvent::write(NetConnection* con, BitStream* stream)
  290. {
  291. // Just do the normal pack...
  292. pack(con, stream);
  293. }
  294. void SimSoundAssetEvent::unpack(NetConnection* con, BitStream* stream)
  295. {
  296. StringTableEntry temp = StringTable->insert(con->unpackNetStringHandleU(stream).getString());
  297. if (AssetDatabase.isDeclaredAsset(temp))
  298. {
  299. AssetPtr<SoundAsset> tempSoundAsset;
  300. tempSoundAsset = temp;
  301. mAsset = temp;
  302. }
  303. sentTransform = stream->readFlag();
  304. if (sentTransform) {
  305. if (stream->readFlag()) {
  306. QuatF q;
  307. q.x = stream->readSignedFloat(SoundRotBits);
  308. q.y = stream->readSignedFloat(SoundRotBits);
  309. q.z = stream->readSignedFloat(SoundRotBits);
  310. F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z));
  311. // #ifdef __linux
  312. // Hmm, this should never happen, but it does...
  313. if (value > 1.f)
  314. value = 1.f;
  315. // #endif
  316. q.w = mSqrt(1.f - value);
  317. if (stream->readFlag())
  318. q.w = -q.w;
  319. q.setMatrix(&mTransform);
  320. }
  321. else
  322. mTransform.identity();
  323. Point3F pos;
  324. stream->readCompressedPoint(&pos, SoundPosAccuracy);
  325. mTransform.setColumn(3, pos);
  326. }
  327. else
  328. {
  329. mTransform = SFX->getListener(0).getTransform();
  330. }
  331. }
  332. void SimSoundAssetEvent::process(NetConnection* con)
  333. {
  334. SFX->playOnce(mAsset->getSFXTrack(), &mTransform);
  335. }
  336. Sim2DAudioEvent::Sim2DAudioEvent(SFXProfile *profile)
  337. {
  338. mProfile = profile;
  339. }
  340. void Sim2DAudioEvent::pack(NetConnection *, BitStream *bstream)
  341. {
  342. bstream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  343. }
  344. void Sim2DAudioEvent::write(NetConnection *, BitStream *bstream)
  345. {
  346. bstream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  347. }
  348. void Sim2DAudioEvent::unpack(NetConnection *, BitStream *bstream)
  349. {
  350. SimObjectId id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  351. Sim::findObject(id, mProfile);
  352. }
  353. void Sim2DAudioEvent::process(NetConnection *)
  354. {
  355. if (mProfile)
  356. SFX->playOnce( mProfile );
  357. }
  358. Sim3DAudioEvent::Sim3DAudioEvent(SFXProfile *profile,const MatrixF* mat)
  359. {
  360. mProfile = profile;
  361. if (mat)
  362. mTransform = *mat;
  363. }
  364. void Sim3DAudioEvent::pack(NetConnection *con, BitStream *bstream)
  365. {
  366. bstream->writeInt(mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  367. // If the sound has cone parameters, the orientation is
  368. // transmitted as well.
  369. SFXDescription* ad = mProfile->getDescription();
  370. if ( bstream->writeFlag( ad->mConeInsideAngle || ad->mConeOutsideAngle ) )
  371. {
  372. QuatF q(mTransform);
  373. q.normalize();
  374. // LH - we can get a valid quat that's very slightly over 1 in and so
  375. // this fails (barely) check against zero. So use some error-
  376. AssertFatal((1.0 - ((q.x * q.x) + (q.y * q.y) + (q.z * q.z))) >= (0.0 - 0.001),
  377. "QuatF::normalize() is broken in Sim3DAudioEvent");
  378. bstream->writeSignedFloat(q.x,SoundRotBits);
  379. bstream->writeSignedFloat(q.y,SoundRotBits);
  380. bstream->writeSignedFloat(q.z,SoundRotBits);
  381. bstream->writeFlag(q.w < 0.0);
  382. }
  383. Point3F pos;
  384. mTransform.getColumn(3,&pos);
  385. bstream->writeCompressedPoint(pos,SoundPosAccuracy);
  386. }
  387. void Sim3DAudioEvent::write(NetConnection *con, BitStream *bstream)
  388. {
  389. // Just do the normal pack...
  390. pack(con,bstream);
  391. }
  392. void Sim3DAudioEvent::unpack(NetConnection *con, BitStream *bstream)
  393. {
  394. SimObjectId id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  395. Sim::findObject(id, mProfile);
  396. if (bstream->readFlag()) {
  397. QuatF q;
  398. q.x = bstream->readSignedFloat(SoundRotBits);
  399. q.y = bstream->readSignedFloat(SoundRotBits);
  400. q.z = bstream->readSignedFloat(SoundRotBits);
  401. F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z));
  402. // #ifdef __linux
  403. // Hmm, this should never happen, but it does...
  404. if ( value > 1.f )
  405. value = 1.f;
  406. // #endif
  407. q.w = mSqrt(1.f - value);
  408. if (bstream->readFlag())
  409. q.w = -q.w;
  410. q.setMatrix(&mTransform);
  411. }
  412. else
  413. mTransform.identity();
  414. Point3F pos;
  415. bstream->readCompressedPoint(&pos,SoundPosAccuracy);
  416. mTransform.setColumn(3, pos);
  417. }
  418. void Sim3DAudioEvent::process(NetConnection *)
  419. {
  420. if (mProfile)
  421. SFX->playOnce( mProfile, &mTransform );
  422. }