gameConnectionEvents.cpp 12 KB

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