stdGameProcess.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 "T3D/gameBase/std/stdGameProcess.h"
  24. #include "platform/profiler.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/dnet.h"
  27. #include "core/stream/bitStream.h"
  28. #include "core/frameAllocator.h"
  29. #include "core/util/refBase.h"
  30. #include "math/mPoint3.h"
  31. #include "math/mMatrix.h"
  32. #include "math/mathUtils.h"
  33. #include "T3D/gameBase/gameBase.h"
  34. #include "T3D/gameBase/gameConnection.h"
  35. #include "T3D/gameBase/std/stdMoveList.h"
  36. #include "T3D/fx/cameraFXMgr.h"
  37. MODULE_BEGIN( ProcessList )
  38. MODULE_INIT
  39. {
  40. StdServerProcessList::init();
  41. StdClientProcessList::init();
  42. }
  43. MODULE_SHUTDOWN
  44. {
  45. StdServerProcessList::shutdown();
  46. StdClientProcessList::shutdown();
  47. }
  48. MODULE_END;
  49. void StdServerProcessList::init()
  50. {
  51. smServerProcessList = new StdServerProcessList();
  52. }
  53. void StdServerProcessList::shutdown()
  54. {
  55. delete smServerProcessList;
  56. }
  57. void StdClientProcessList::init()
  58. {
  59. smClientProcessList = new StdClientProcessList();
  60. }
  61. void StdClientProcessList::shutdown()
  62. {
  63. delete smClientProcessList;
  64. }
  65. //----------------------------------------------------------------------------
  66. namespace
  67. {
  68. // local work class
  69. struct GameBaseListNode
  70. {
  71. GameBaseListNode()
  72. {
  73. mPrev=this;
  74. mNext=this;
  75. mObject=NULL;
  76. }
  77. GameBaseListNode * mPrev;
  78. GameBaseListNode * mNext;
  79. GameBase * mObject;
  80. void linkBefore(GameBaseListNode * obj)
  81. {
  82. // Link this before obj
  83. mNext = obj;
  84. mPrev = obj->mPrev;
  85. obj->mPrev = this;
  86. mPrev->mNext = this;
  87. }
  88. };
  89. } // namespace
  90. //--------------------------------------------------------------------------
  91. // ClientProcessList
  92. //--------------------------------------------------------------------------
  93. StdClientProcessList::StdClientProcessList()
  94. {
  95. }
  96. bool StdClientProcessList::advanceTime( SimTime timeDelta )
  97. {
  98. PROFILE_SCOPE( StdClientProcessList_AdvanceTime );
  99. if ( doBacklogged( timeDelta ) )
  100. return false;
  101. bool ret = Parent::advanceTime( timeDelta );
  102. ProcessObject *obj = NULL;
  103. AssertFatal( mLastDelta >= 0.0f && mLastDelta <= 1.0f, "mLastDelta is not zero to one.");
  104. obj = mHead.mProcessLink.next;
  105. while ( obj != &mHead )
  106. {
  107. if ( obj->isTicking() )
  108. obj->interpolateTick( mLastDelta );
  109. obj = obj->mProcessLink.next;
  110. }
  111. // Inform objects of total elapsed delta so they can advance
  112. // client side animations.
  113. F32 dt = F32(timeDelta) / 1000;
  114. // Update camera FX.
  115. gCamFXMgr.update( dt );
  116. obj = mHead.mProcessLink.next;
  117. while ( obj != &mHead )
  118. {
  119. obj->advanceTime( dt );
  120. obj = obj->mProcessLink.next;
  121. }
  122. return ret;
  123. }
  124. //----------------------------------------------------------------------------
  125. void StdClientProcessList::onAdvanceObjects()
  126. {
  127. PROFILE_SCOPE( StdClientProcessList_OnAdvanceObjects );
  128. GameConnection* connection = GameConnection::getConnectionToServer();
  129. if ( connection )
  130. {
  131. // process any demo blocks that are NOT moves, and exactly one move
  132. // we advance time in the demo stream by a move inserted on
  133. // each tick. So before doing the tick processing we advance
  134. // the demo stream until a move is ready
  135. if ( connection->isPlayingBack() )
  136. {
  137. U32 blockType;
  138. do
  139. {
  140. blockType = connection->getNextBlockType();
  141. bool res = connection->processNextBlock();
  142. // if there are no more blocks, exit out of this function,
  143. // as no more client time needs to process right now - we'll
  144. // get it all on the next advanceClientTime()
  145. if(!res)
  146. return;
  147. }
  148. while ( blockType != GameConnection::BlockTypeMove );
  149. }
  150. connection->mMoveList->collectMove();
  151. advanceObjects();
  152. }
  153. else
  154. advanceObjects();
  155. }
  156. void StdClientProcessList::onTickObject( ProcessObject *obj )
  157. {
  158. PROFILE_SCOPE( StdClientProcessList_OnTickObject );
  159. // In case the object deletes itself during its processTick.
  160. SimObjectPtr<SceneObject> safePtr = static_cast<SceneObject*>( obj );
  161. // Each object is either advanced a single tick, or if it's
  162. // being controlled by a client, ticked once for each pending move.
  163. Move* movePtr;
  164. U32 numMoves;
  165. GameConnection* con = obj->getControllingClient();
  166. if ( con && con->getControlObject() == obj )
  167. {
  168. con->mMoveList->getMoves( &movePtr, &numMoves );
  169. if ( numMoves )
  170. {
  171. // Note: should only have a single move at this point
  172. AssertFatal(numMoves==1,"ClientProccessList::onTickObject: more than one move in queue");
  173. #ifdef TORQUE_DEBUG_NET_MOVES
  174. U32 sum = Move::ChecksumMask & obj->getPacketDataChecksum(obj->getControllingClient());
  175. #endif
  176. if ( obj->isTicking() )
  177. obj->processTick( movePtr );
  178. if ( bool(safePtr) && obj->getControllingClient() )
  179. {
  180. U32 newsum = Move::ChecksumMask & obj->getPacketDataChecksum( obj->getControllingClient() );
  181. // set checksum if not set or check against stored value if set
  182. movePtr->checksum = newsum;
  183. #ifdef TORQUE_DEBUG_NET_MOVES
  184. Con::printf("move checksum: %i, (start %i), (move %f %f %f)",
  185. movePtr->checksum,sum,movePtr->yaw,movePtr->y,movePtr->z);
  186. #endif
  187. }
  188. con->mMoveList->clearMoves( 1 );
  189. }
  190. }
  191. else if ( obj->isTicking() )
  192. obj->processTick( 0 );
  193. }
  194. void StdClientProcessList::advanceObjects()
  195. {
  196. PROFILE_SCOPE( StdClientProcessList_AdvanceObjects );
  197. #ifdef TORQUE_DEBUG_NET_MOVES
  198. Con::printf("Advance client time...");
  199. #endif
  200. Parent::advanceObjects();
  201. #ifdef TORQUE_DEBUG_NET_MOVES
  202. Con::printf("---------");
  203. #endif
  204. }
  205. void StdClientProcessList::clientCatchup( GameConnection * connection )
  206. {
  207. SimObjectPtr<GameBase> control = connection->getControlObject();
  208. if ( control )
  209. {
  210. Move * movePtr;
  211. U32 numMoves;
  212. U32 m = 0;
  213. connection->mMoveList->getMoves( &movePtr, &numMoves );
  214. #ifdef TORQUE_DEBUG_NET_MOVES
  215. Con::printf("client catching up... (%i)", numMoves);
  216. #endif
  217. preTickSignal().trigger();
  218. if ( control->isTicking() )
  219. for ( m = 0; m < numMoves; m++ )
  220. control->processTick( movePtr++ );
  221. connection->mMoveList->clearMoves( m );
  222. }
  223. #ifdef TORQUE_DEBUG_NET_MOVES
  224. Con::printf("---------");
  225. #endif
  226. }
  227. //--------------------------------------------------------------------------
  228. // ServerProcessList
  229. //--------------------------------------------------------------------------
  230. StdServerProcessList::StdServerProcessList()
  231. {
  232. }
  233. void StdServerProcessList::onPreTickObject( ProcessObject *pobj )
  234. {
  235. if ( pobj->mIsGameBase )
  236. {
  237. SimObjectPtr<GameBase> obj = getGameBase( pobj );
  238. // Each object is either advanced a single tick, or if it's
  239. // being controlled by a client, ticked once for each pending move.
  240. GameConnection *con = obj->getControllingClient();
  241. if ( con && con->getControlObject() == obj )
  242. {
  243. Move* movePtr;
  244. U32 numMoves;
  245. con->mMoveList->getMoves( &movePtr, &numMoves );
  246. if ( numMoves == 0 )
  247. {
  248. #ifdef TORQUE_DEBUG_NET_MOVES
  249. Con::printf("no moves on object %i, skip tick",obj->getId());
  250. #endif
  251. return;
  252. }
  253. }
  254. }
  255. Parent::onPreTickObject (pobj );
  256. }
  257. void StdServerProcessList::onTickObject( ProcessObject *pobj )
  258. {
  259. PROFILE_SCOPE( StdServerProcessList_OnTickObject );
  260. // Each object is either advanced a single tick, or if it's
  261. // being controlled by a client, ticked once for each pending move.
  262. GameConnection *con = pobj->getControllingClient();
  263. if ( pobj->mIsGameBase && con && con->getControlObject() == pobj )
  264. {
  265. // In case the object is deleted during its own tick.
  266. SimObjectPtr<GameBase> obj = getGameBase( pobj );
  267. Move* movePtr;
  268. U32 m, numMoves;
  269. con->mMoveList->getMoves( &movePtr, &numMoves );
  270. // For debugging it can be useful to know when this happens.
  271. //if ( numMoves > 1 )
  272. // Con::printf( "numMoves: %i", numMoves );
  273. // Do we really need to test the control object each iteration? Does it change?
  274. for ( m = 0; m < numMoves && con && con->getControlObject() == obj; m++, movePtr++ )
  275. {
  276. #ifdef TORQUE_DEBUG_NET_MOVES
  277. U32 sum = Move::ChecksumMask & obj->getPacketDataChecksum(obj->getControllingClient());
  278. #endif
  279. if ( obj->isTicking() )
  280. obj->processTick( movePtr );
  281. if ( con && con->getControlObject() == obj )
  282. {
  283. U32 newsum = Move::ChecksumMask & obj->getPacketDataChecksum( obj->getControllingClient() );
  284. // check move checksum
  285. if ( movePtr->checksum != newsum )
  286. {
  287. #ifdef TORQUE_DEBUG_NET_MOVES
  288. if( !obj->isAIControlled() )
  289. Con::printf("move %i checksum disagree: %i != %i, (start %i), (move %f %f %f)",
  290. movePtr->id, movePtr->checksum,newsum,sum,movePtr->yaw,movePtr->y,movePtr->z);
  291. #endif
  292. movePtr->checksum = Move::ChecksumMismatch;
  293. }
  294. else
  295. {
  296. #ifdef TORQUE_DEBUG_NET_MOVES
  297. Con::printf("move %i checksum agree: %i == %i, (start %i), (move %f %f %f)",
  298. movePtr->id, movePtr->checksum,newsum,sum,movePtr->yaw,movePtr->y,movePtr->z);
  299. #endif
  300. }
  301. }
  302. }
  303. con->mMoveList->clearMoves( m );
  304. }
  305. else if ( pobj->isTicking() )
  306. pobj->processTick( 0 );
  307. }
  308. void StdServerProcessList::advanceObjects()
  309. {
  310. #ifdef TORQUE_DEBUG_NET_MOVES
  311. Con::printf("Advance server time...");
  312. #endif
  313. Parent::advanceObjects();
  314. // Credit all connections with the elapsed tick
  315. SimGroup *clientGroup = Sim::getClientGroup();
  316. for(SimGroup::iterator i = clientGroup->begin(); i != clientGroup->end(); i++)
  317. {
  318. if (GameConnection *con = dynamic_cast<GameConnection *>(*i))
  319. {
  320. con->mMoveList->advanceMove();
  321. }
  322. }
  323. #ifdef TORQUE_DEBUG_NET_MOVES
  324. Con::printf("---------");
  325. #endif
  326. }