stdGameProcess.cpp 12 KB

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