cg_snapshot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. //
  19. // cg_snapshot.c -- things that happen on snapshot transition,
  20. // not necessarily every single rendered frame
  21. #include "cg_local.h"
  22. /*
  23. ==================
  24. CG_ResetEntity
  25. ==================
  26. */
  27. static void CG_ResetEntity( centity_t *cent ) {
  28. // if the previous snapshot this entity was updated in is at least
  29. // an event window back in time then we can reset the previous event
  30. if ( cent->snapShotTime < cg.time - EVENT_VALID_MSEC ) {
  31. cent->previousEvent = 0;
  32. }
  33. cent->trailTime = cg.snap->serverTime;
  34. VectorCopy (cent->currentState.origin, cent->lerpOrigin);
  35. VectorCopy (cent->currentState.angles, cent->lerpAngles);
  36. if ( cent->currentState.eType == ET_PLAYER ) {
  37. CG_ResetPlayerEntity( cent );
  38. }
  39. }
  40. /*
  41. ===============
  42. CG_TransitionEntity
  43. cent->nextState is moved to cent->currentState and events are fired
  44. ===============
  45. */
  46. static void CG_TransitionEntity( centity_t *cent ) {
  47. cent->currentState = cent->nextState;
  48. cent->currentValid = qtrue;
  49. // reset if the entity wasn't in the last frame or was teleported
  50. if ( !cent->interpolate ) {
  51. CG_ResetEntity( cent );
  52. }
  53. // clear the next state. if will be set by the next CG_SetNextSnap
  54. cent->interpolate = qfalse;
  55. // check for events
  56. CG_CheckEvents( cent );
  57. }
  58. /*
  59. ==================
  60. CG_SetInitialSnapshot
  61. This will only happen on the very first snapshot, or
  62. on tourney restarts. All other times will use
  63. CG_TransitionSnapshot instead.
  64. FIXME: Also called by map_restart?
  65. ==================
  66. */
  67. void CG_SetInitialSnapshot( snapshot_t *snap ) {
  68. int i;
  69. centity_t *cent;
  70. entityState_t *state;
  71. cg.snap = snap;
  72. BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].currentState, qfalse );
  73. // sort out solid entities
  74. CG_BuildSolidList();
  75. CG_ExecuteNewServerCommands( snap->serverCommandSequence );
  76. // set our local weapon selection pointer to
  77. // what the server has indicated the current weapon is
  78. CG_Respawn();
  79. for ( i = 0 ; i < cg.snap->numEntities ; i++ ) {
  80. state = &cg.snap->entities[ i ];
  81. cent = &cg_entities[ state->number ];
  82. memcpy(&cent->currentState, state, sizeof(entityState_t));
  83. //cent->currentState = *state;
  84. cent->interpolate = qfalse;
  85. cent->currentValid = qtrue;
  86. CG_ResetEntity( cent );
  87. // check for events
  88. CG_CheckEvents( cent );
  89. }
  90. }
  91. /*
  92. ===================
  93. CG_TransitionSnapshot
  94. The transition point from snap to nextSnap has passed
  95. ===================
  96. */
  97. static void CG_TransitionSnapshot( void ) {
  98. centity_t *cent;
  99. snapshot_t *oldFrame;
  100. int i;
  101. if ( !cg.snap ) {
  102. CG_Error( "CG_TransitionSnapshot: NULL cg.snap" );
  103. }
  104. if ( !cg.nextSnap ) {
  105. CG_Error( "CG_TransitionSnapshot: NULL cg.nextSnap" );
  106. }
  107. // execute any server string commands before transitioning entities
  108. CG_ExecuteNewServerCommands( cg.nextSnap->serverCommandSequence );
  109. // if we had a map_restart, set everthing with initial
  110. if ( !cg.snap ) {
  111. }
  112. // clear the currentValid flag for all entities in the existing snapshot
  113. for ( i = 0 ; i < cg.snap->numEntities ; i++ ) {
  114. cent = &cg_entities[ cg.snap->entities[ i ].number ];
  115. cent->currentValid = qfalse;
  116. }
  117. // move nextSnap to snap and do the transitions
  118. oldFrame = cg.snap;
  119. cg.snap = cg.nextSnap;
  120. BG_PlayerStateToEntityState( &cg.snap->ps, &cg_entities[ cg.snap->ps.clientNum ].currentState, qfalse );
  121. cg_entities[ cg.snap->ps.clientNum ].interpolate = qfalse;
  122. for ( i = 0 ; i < cg.snap->numEntities ; i++ ) {
  123. cent = &cg_entities[ cg.snap->entities[ i ].number ];
  124. CG_TransitionEntity( cent );
  125. // remember time of snapshot this entity was last updated in
  126. cent->snapShotTime = cg.snap->serverTime;
  127. }
  128. cg.nextSnap = NULL;
  129. // check for playerstate transition events
  130. if ( oldFrame ) {
  131. playerState_t *ops, *ps;
  132. ops = &oldFrame->ps;
  133. ps = &cg.snap->ps;
  134. // teleporting checks are irrespective of prediction
  135. if ( ( ps->eFlags ^ ops->eFlags ) & EF_TELEPORT_BIT ) {
  136. cg.thisFrameTeleport = qtrue; // will be cleared by prediction code
  137. }
  138. // if we are not doing client side movement prediction for any
  139. // reason, then the client events and view changes will be issued now
  140. if ( cg.demoPlayback || (cg.snap->ps.pm_flags & PMF_FOLLOW)
  141. || cg_nopredict.integer || cg_synchronousClients.integer ) {
  142. CG_TransitionPlayerState( ps, ops );
  143. }
  144. }
  145. }
  146. /*
  147. ===================
  148. CG_SetNextSnap
  149. A new snapshot has just been read in from the client system.
  150. ===================
  151. */
  152. static void CG_SetNextSnap( snapshot_t *snap ) {
  153. int num;
  154. entityState_t *es;
  155. centity_t *cent;
  156. cg.nextSnap = snap;
  157. BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].nextState, qfalse );
  158. cg_entities[ cg.snap->ps.clientNum ].interpolate = qtrue;
  159. // check for extrapolation errors
  160. for ( num = 0 ; num < snap->numEntities ; num++ ) {
  161. es = &snap->entities[num];
  162. cent = &cg_entities[ es->number ];
  163. memcpy(&cent->nextState, es, sizeof(entityState_t));
  164. //cent->nextState = *es;
  165. // if this frame is a teleport, or the entity wasn't in the
  166. // previous frame, don't interpolate
  167. if ( !cent->currentValid || ( ( cent->currentState.eFlags ^ es->eFlags ) & EF_TELEPORT_BIT ) ) {
  168. cent->interpolate = qfalse;
  169. } else {
  170. cent->interpolate = qtrue;
  171. }
  172. }
  173. // if the next frame is a teleport for the playerstate, we
  174. // can't interpolate during demos
  175. if ( cg.snap && ( ( snap->ps.eFlags ^ cg.snap->ps.eFlags ) & EF_TELEPORT_BIT ) ) {
  176. cg.nextFrameTeleport = qtrue;
  177. } else {
  178. cg.nextFrameTeleport = qfalse;
  179. }
  180. // if changing follow mode, don't interpolate
  181. if ( cg.nextSnap->ps.clientNum != cg.snap->ps.clientNum ) {
  182. cg.nextFrameTeleport = qtrue;
  183. }
  184. // if changing server restarts, don't interpolate
  185. if ( ( cg.nextSnap->snapFlags ^ cg.snap->snapFlags ) & SNAPFLAG_SERVERCOUNT ) {
  186. cg.nextFrameTeleport = qtrue;
  187. }
  188. // sort out solid entities
  189. CG_BuildSolidList();
  190. }
  191. /*
  192. ========================
  193. CG_ReadNextSnapshot
  194. This is the only place new snapshots are requested
  195. This may increment cgs.processedSnapshotNum multiple
  196. times if the client system fails to return a
  197. valid snapshot.
  198. ========================
  199. */
  200. static snapshot_t *CG_ReadNextSnapshot( void ) {
  201. qboolean r;
  202. snapshot_t *dest;
  203. if ( cg.latestSnapshotNum > cgs.processedSnapshotNum + 1000 ) {
  204. CG_Printf( "WARNING: CG_ReadNextSnapshot: way out of range, %i > %i",
  205. cg.latestSnapshotNum, cgs.processedSnapshotNum );
  206. }
  207. while ( cgs.processedSnapshotNum < cg.latestSnapshotNum ) {
  208. // decide which of the two slots to load it into
  209. if ( cg.snap == &cg.activeSnapshots[0] ) {
  210. dest = &cg.activeSnapshots[1];
  211. } else {
  212. dest = &cg.activeSnapshots[0];
  213. }
  214. // try to read the snapshot from the client system
  215. cgs.processedSnapshotNum++;
  216. r = trap_GetSnapshot( cgs.processedSnapshotNum, dest );
  217. // FIXME: why would trap_GetSnapshot return a snapshot with the same server time
  218. if ( cg.snap && r && dest->serverTime == cg.snap->serverTime ) {
  219. //continue;
  220. }
  221. // if it succeeded, return
  222. if ( r ) {
  223. CG_AddLagometerSnapshotInfo( dest );
  224. return dest;
  225. }
  226. // a GetSnapshot will return failure if the snapshot
  227. // never arrived, or is so old that its entities
  228. // have been shoved off the end of the circular
  229. // buffer in the client system.
  230. // record as a dropped packet
  231. CG_AddLagometerSnapshotInfo( NULL );
  232. // If there are additional snapshots, continue trying to
  233. // read them.
  234. }
  235. // nothing left to read
  236. return NULL;
  237. }
  238. /*
  239. ============
  240. CG_ProcessSnapshots
  241. We are trying to set up a renderable view, so determine
  242. what the simulated time is, and try to get snapshots
  243. both before and after that time if available.
  244. If we don't have a valid cg.snap after exiting this function,
  245. then a 3D game view cannot be rendered. This should only happen
  246. right after the initial connection. After cg.snap has been valid
  247. once, it will never turn invalid.
  248. Even if cg.snap is valid, cg.nextSnap may not be, if the snapshot
  249. hasn't arrived yet (it becomes an extrapolating situation instead
  250. of an interpolating one)
  251. ============
  252. */
  253. void CG_ProcessSnapshots( void ) {
  254. snapshot_t *snap;
  255. int n;
  256. // see what the latest snapshot the client system has is
  257. trap_GetCurrentSnapshotNumber( &n, &cg.latestSnapshotTime );
  258. if ( n != cg.latestSnapshotNum ) {
  259. if ( n < cg.latestSnapshotNum ) {
  260. // this should never happen
  261. CG_Error( "CG_ProcessSnapshots: n < cg.latestSnapshotNum" );
  262. }
  263. cg.latestSnapshotNum = n;
  264. }
  265. // If we have yet to receive a snapshot, check for it.
  266. // Once we have gotten the first snapshot, cg.snap will
  267. // always have valid data for the rest of the game
  268. while ( !cg.snap ) {
  269. snap = CG_ReadNextSnapshot();
  270. if ( !snap ) {
  271. // we can't continue until we get a snapshot
  272. return;
  273. }
  274. // set our weapon selection to what
  275. // the playerstate is currently using
  276. if ( !( snap->snapFlags & SNAPFLAG_NOT_ACTIVE ) ) {
  277. CG_SetInitialSnapshot( snap );
  278. }
  279. }
  280. // loop until we either have a valid nextSnap with a serverTime
  281. // greater than cg.time to interpolate towards, or we run
  282. // out of available snapshots
  283. do {
  284. // if we don't have a nextframe, try and read a new one in
  285. if ( !cg.nextSnap ) {
  286. snap = CG_ReadNextSnapshot();
  287. // if we still don't have a nextframe, we will just have to
  288. // extrapolate
  289. if ( !snap ) {
  290. break;
  291. }
  292. CG_SetNextSnap( snap );
  293. // if time went backwards, we have a level restart
  294. if ( cg.nextSnap->serverTime < cg.snap->serverTime ) {
  295. CG_Error( "CG_ProcessSnapshots: Server time went backwards" );
  296. }
  297. }
  298. // if our time is < nextFrame's, we have a nice interpolating state
  299. if ( cg.time >= cg.snap->serverTime && cg.time < cg.nextSnap->serverTime ) {
  300. break;
  301. }
  302. // we have passed the transition from nextFrame to frame
  303. CG_TransitionSnapshot();
  304. } while ( 1 );
  305. // assert our valid conditions upon exiting
  306. if ( cg.snap == NULL ) {
  307. CG_Error( "CG_ProcessSnapshots: cg.snap == NULL" );
  308. }
  309. if ( cg.time < cg.snap->serverTime ) {
  310. // this can happen right after a vid_restart
  311. cg.time = cg.snap->serverTime;
  312. }
  313. if ( cg.nextSnap != NULL && cg.nextSnap->serverTime <= cg.time ) {
  314. CG_Error( "CG_ProcessSnapshots: cg.nextSnap->serverTime <= cg.time" );
  315. }
  316. }