gameConnectionEvents.cpp 12 KB

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