Debian Codegraph @

code/cgame/cg_snapshot.c

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