stdGameProcess.cpp 12 KB

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