gameConnectionEvents.cpp 16 KB

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