extendedGameProcess.cpp 11 KB

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