| 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 |
//unlagged - early transitioning |
| 59 |
// used to be static, now needed to transition entities from within cg_ents.c |
| 60 |
void CG_TransitionEntity( centity_t *cent ) { |
| 61 |
cent->currentState = cent->nextState; |
| 62 |
cent->currentValid = qtrue; |
| 63 |
|
| 64 |
// reset if the entity wasn't in the last frame or was teleported |
| 65 |
if ( !cent->interpolate ) { |
| 66 |
CG_ResetEntity( cent ); |
| 67 |
} |
| 68 |
|
| 69 |
// clear the next state. if will be set by the next CG_SetNextSnap |
| 70 |
cent->interpolate = qfalse; |
| 71 |
|
| 72 |
// check for events |
| 73 |
CG_CheckEvents( cent ); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/* |
| 78 |
================== |
| 79 |
CG_SetInitialSnapshot |
| 80 |
|
| 81 |
This will only happen on the very first snapshot, or |
| 82 |
on tourney restarts. All other times will use |
| 83 |
CG_TransitionSnapshot instead. |
| 84 |
|
| 85 |
FIXME: Also called by map_restart? |
| 86 |
================== |
| 87 |
*/ |
| 88 |
void CG_SetInitialSnapshot( snapshot_t *snap ) { |
| 89 |
int i; |
| 90 |
centity_t *cent; |
| 91 |
entityState_t *state; |
| 92 |
|
| 93 |
cg.snap = snap; |
| 94 |
|
| 95 |
BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].currentState, qfalse ); |
| 96 |
|
| 97 |
// sort out solid entities |
| 98 |
CG_BuildSolidList(); |
| 99 |
|
| 100 |
CG_ExecuteNewServerCommands( snap->serverCommandSequence ); |
| 101 |
|
| 102 |
// set our local weapon selection pointer to |
| 103 |
// what the server has indicated the current weapon is |
| 104 |
CG_Respawn(); |
| 105 |
|
| 106 |
for ( i = 0 ; i < cg.snap->numEntities ; i++ ) { |
| 107 |
state = &cg.snap->entities[ i ]; |
| 108 |
cent = &cg_entities[ state->number ]; |
| 109 |
|
| 110 |
memcpy(¢->currentState, state, sizeof(entityState_t)); |
| 111 |
//cent->currentState = *state; |
| 112 |
cent->interpolate = qfalse; |
| 113 |
cent->currentValid = qtrue; |
| 114 |
|
| 115 |
CG_ResetEntity( cent ); |
| 116 |
|
| 117 |
// check for events |
| 118 |
CG_CheckEvents( cent ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
/* |
| 124 |
=================== |
| 125 |
CG_TransitionSnapshot |
| 126 |
|
| 127 |
The transition point from snap to nextSnap has passed |
| 128 |
=================== |
| 129 |
*/ |
| 130 |
static void CG_TransitionSnapshot( void ) { |
| 131 |
centity_t *cent; |
| 132 |
snapshot_t *oldFrame; |
| 133 |
int i; |
| 134 |
|
| 135 |
if ( !cg.snap ) { |
| 136 |
CG_Error( "CG_TransitionSnapshot: NULL cg.snap" ); |
| 137 |
} |
| 138 |
if ( !cg.nextSnap ) { |
| 139 |
CG_Error( "CG_TransitionSnapshot: NULL cg.nextSnap" ); |
| 140 |
} |
| 141 |
|
| 142 |
// execute any server string commands before transitioning entities |
| 143 |
CG_ExecuteNewServerCommands( cg.nextSnap->serverCommandSequence ); |
| 144 |
|
| 145 |
// if we had a map_restart, set everthing with initial |
| 146 |
if ( !cg.snap ) { |
| 147 |
} |
| 148 |
|
| 149 |
// clear the currentValid flag for all entities in the existing snapshot |
| 150 |
for ( i = 0 ; i < cg.snap->numEntities ; i++ ) { |
| 151 |
cent = &cg_entities[ cg.snap->entities[ i ].number ]; |
| 152 |
cent->currentValid = qfalse; |
| 153 |
} |
| 154 |
|
| 155 |
// move nextSnap to snap and do the transitions |
| 156 |
oldFrame = cg.snap; |
| 157 |
cg.snap = cg.nextSnap; |
| 158 |
|
| 159 |
BG_PlayerStateToEntityState( &cg.snap->ps, &cg_entities[ cg.snap->ps.clientNum ].currentState, qfalse ); |
| 160 |
cg_entities[ cg.snap->ps.clientNum ].interpolate = qfalse; |
| 161 |
|
| 162 |
for ( i = 0 ; i < cg.snap->numEntities ; i++ ) { |
| 163 |
cent = &cg_entities[ cg.snap->entities[ i ].number ]; |
| 164 |
CG_TransitionEntity( cent ); |
| 165 |
|
| 166 |
// remember time of snapshot this entity was last updated in |
| 167 |
cent->snapShotTime = cg.snap->serverTime; |
| 168 |
} |
| 169 |
|
| 170 |
cg.nextSnap = NULL; |
| 171 |
|
| 172 |
// check for playerstate transition events |
| 173 |
if ( oldFrame ) { |
| 174 |
playerState_t *ops, *ps; |
| 175 |
|
| 176 |
ops = &oldFrame->ps; |
| 177 |
ps = &cg.snap->ps; |
| 178 |
// teleporting checks are irrespective of prediction |
| 179 |
if ( ( ps->eFlags ^ ops->eFlags ) & EF_TELEPORT_BIT ) { |
| 180 |
cg.thisFrameTeleport = qtrue; // will be cleared by prediction code |
| 181 |
} |
| 182 |
|
| 183 |
// if we are not doing client side movement prediction for any |
| 184 |
// reason, then the client events and view changes will be issued now |
| 185 |
if ( cg.demoPlayback || (cg.snap->ps.pm_flags & PMF_FOLLOW) |
| 186 |
|| cg_nopredict.integer || cg_synchronousClients.integer ) { |
| 187 |
CG_TransitionPlayerState( ps, ops ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/* |
| 195 |
=================== |
| 196 |
CG_SetNextSnap |
| 197 |
|
| 198 |
A new snapshot has just been read in from the client system. |
| 199 |
=================== |
| 200 |
*/ |
| 201 |
static void CG_SetNextSnap( snapshot_t *snap ) { |
| 202 |
int num; |
| 203 |
entityState_t *es; |
| 204 |
centity_t *cent; |
| 205 |
|
| 206 |
cg.nextSnap = snap; |
| 207 |
|
| 208 |
BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].nextState, qfalse ); |
| 209 |
cg_entities[ cg.snap->ps.clientNum ].interpolate = qtrue; |
| 210 |
|
| 211 |
// check for extrapolation errors |
| 212 |
for ( num = 0 ; num < snap->numEntities ; num++ ) { |
| 213 |
es = &snap->entities[num]; |
| 214 |
cent = &cg_entities[ es->number ]; |
| 215 |
|
| 216 |
memcpy(¢->nextState, es, sizeof(entityState_t)); |
| 217 |
//cent->nextState = *es; |
| 218 |
|
| 219 |
// if this frame is a teleport, or the entity wasn't in the |
| 220 |
// previous frame, don't interpolate |
| 221 |
if ( !cent->currentValid || ( ( cent->currentState.eFlags ^ es->eFlags ) & EF_TELEPORT_BIT ) ) { |
| 222 |
cent->interpolate = qfalse; |
| 223 |
} else { |
| 224 |
cent->interpolate = qtrue; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
// if the next frame is a teleport for the playerstate, we |
| 229 |
// can't interpolate during demos |
| 230 |
if ( cg.snap && ( ( snap->ps.eFlags ^ cg.snap->ps.eFlags ) & EF_TELEPORT_BIT ) ) { |
| 231 |
cg.nextFrameTeleport = qtrue; |
| 232 |
} else { |
| 233 |
cg.nextFrameTeleport = qfalse; |
| 234 |
} |
| 235 |
|
| 236 |
// if changing follow mode, don't interpolate |
| 237 |
if ( cg.nextSnap->ps.clientNum != cg.snap->ps.clientNum ) { |
| 238 |
cg.nextFrameTeleport = qtrue; |
| 239 |
} |
| 240 |
|
| 241 |
// if changing server restarts, don't interpolate |
| 242 |
if ( ( cg.nextSnap->snapFlags ^ cg.snap->snapFlags ) & SNAPFLAG_SERVERCOUNT ) { |
| 243 |
cg.nextFrameTeleport = qtrue; |
| 244 |
} |
| 245 |
|
| 246 |
// sort out solid entities |
| 247 |
CG_BuildSolidList(); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
/* |
| 252 |
======================== |
| 253 |
CG_ReadNextSnapshot |
| 254 |
|
| 255 |
This is the only place new snapshots are requested |
| 256 |
This may increment cgs.processedSnapshotNum multiple |
| 257 |
times if the client system fails to return a |
| 258 |
valid snapshot. |
| 259 |
======================== |
| 260 |
*/ |
| 261 |
static snapshot_t *CG_ReadNextSnapshot( void ) { |
| 262 |
qboolean r; |
| 263 |
snapshot_t *dest; |
| 264 |
|
| 265 |
if ( cg.latestSnapshotNum > cgs.processedSnapshotNum + 1000 ) { |
| 266 |
CG_Printf( "WARNING: CG_ReadNextSnapshot: way out of range, %i > %i", |
| 267 |
cg.latestSnapshotNum, cgs.processedSnapshotNum ); |
| 268 |
} |
| 269 |
|
| 270 |
while ( cgs.processedSnapshotNum < cg.latestSnapshotNum ) { |
| 271 |
// decide which of the two slots to load it into |
| 272 |
if ( cg.snap == &cg.activeSnapshots[0] ) { |
| 273 |
dest = &cg.activeSnapshots[1]; |
| 274 |
} else { |
| 275 |
dest = &cg.activeSnapshots[0]; |
| 276 |
} |
| 277 |
|
| 278 |
// try to read the snapshot from the client system |
| 279 |
cgs.processedSnapshotNum++; |
| 280 |
r = trap_GetSnapshot( cgs.processedSnapshotNum, dest ); |
| 281 |
|
| 282 |
// FIXME: why would trap_GetSnapshot return a snapshot with the same server time |
| 283 |
if ( cg.snap && r && dest->serverTime == cg.snap->serverTime ) { |
| 284 |
//continue; |
| 285 |
} |
| 286 |
|
| 287 |
// if it succeeded, return |
| 288 |
if ( r ) { |
| 289 |
CG_AddLagometerSnapshotInfo( dest ); |
| 290 |
return dest; |
| 291 |
} |
| 292 |
|
| 293 |
// a GetSnapshot will return failure if the snapshot |
| 294 |
// never arrived, or is so old that its entities |
| 295 |
// have been shoved off the end of the circular |
| 296 |
// buffer in the client system. |
| 297 |
|
| 298 |
// record as a dropped packet |
| 299 |
CG_AddLagometerSnapshotInfo( NULL ); |
| 300 |
|
| 301 |
// If there are additional snapshots, continue trying to |
| 302 |
// read them. |
| 303 |
} |
| 304 |
|
| 305 |
// nothing left to read |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/* |
| 311 |
============ |
| 312 |
CG_ProcessSnapshots |
| 313 |
|
| 314 |
We are trying to set up a renderable view, so determine |
| 315 |
what the simulated time is, and try to get snapshots |
| 316 |
both before and after that time if available. |
| 317 |
|
| 318 |
If we don't have a valid cg.snap after exiting this function, |
| 319 |
then a 3D game view cannot be rendered. This should only happen |
| 320 |
right after the initial connection. After cg.snap has been valid |
| 321 |
once, it will never turn invalid. |
| 322 |
|
| 323 |
Even if cg.snap is valid, cg.nextSnap may not be, if the snapshot |
| 324 |
hasn't arrived yet (it becomes an extrapolating situation instead |
| 325 |
of an interpolating one) |
| 326 |
|
| 327 |
============ |
| 328 |
*/ |
| 329 |
void CG_ProcessSnapshots( void ) { |
| 330 |
snapshot_t *snap; |
| 331 |
int n; |
| 332 |
|
| 333 |
// see what the latest snapshot the client system has is |
| 334 |
trap_GetCurrentSnapshotNumber( &n, &cg.latestSnapshotTime ); |
| 335 |
if ( n != cg.latestSnapshotNum ) { |
| 336 |
if ( n < cg.latestSnapshotNum ) { |
| 337 |
// this should never happen |
| 338 |
CG_Error( "CG_ProcessSnapshots: n < cg.latestSnapshotNum" ); |
| 339 |
} |
| 340 |
cg.latestSnapshotNum = n; |
| 341 |
} |
| 342 |
|
| 343 |
// If we have yet to receive a snapshot, check for it. |
| 344 |
// Once we have gotten the first snapshot, cg.snap will |
| 345 |
// always have valid data for the rest of the game |
| 346 |
while ( !cg.snap ) { |
| 347 |
snap = CG_ReadNextSnapshot(); |
| 348 |
if ( !snap ) { |
| 349 |
// we can't continue until we get a snapshot |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
// set our weapon selection to what |
| 354 |
// the playerstate is currently using |
| 355 |
if ( !( snap->snapFlags & SNAPFLAG_NOT_ACTIVE ) ) { |
| 356 |
CG_SetInitialSnapshot( snap ); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// loop until we either have a valid nextSnap with a serverTime |
| 361 |
// greater than cg.time to interpolate towards, or we run |
| 362 |
// out of available snapshots |
| 363 |
do { |
| 364 |
// if we don't have a nextframe, try and read a new one in |
| 365 |
if ( !cg.nextSnap ) { |
| 366 |
snap = CG_ReadNextSnapshot(); |
| 367 |
|
| 368 |
// if we still don't have a nextframe, we will just have to |
| 369 |
// extrapolate |
| 370 |
if ( !snap ) { |
| 371 |
break; |
| 372 |
} |
| 373 |
|
| 374 |
CG_SetNextSnap( snap ); |
| 375 |
|
| 376 |
|
| 377 |
// if time went backwards, we have a level restart |
| 378 |
if ( cg.nextSnap->serverTime < cg.snap->serverTime ) { |
| 379 |
CG_Error( "CG_ProcessSnapshots: Server time went backwards" ); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
// if our time is < nextFrame's, we have a nice interpolating state |
| 384 |
if ( cg.time >= cg.snap->serverTime && cg.time < cg.nextSnap->serverTime ) { |
| 385 |
break; |
| 386 |
} |
| 387 |
|
| 388 |
// we have passed the transition from nextFrame to frame |
| 389 |
CG_TransitionSnapshot(); |
| 390 |
} while ( 1 ); |
| 391 |
|
| 392 |
// assert our valid conditions upon exiting |
| 393 |
if ( cg.snap == NULL ) { |
| 394 |
CG_Error( "CG_ProcessSnapshots: cg.snap == NULL" ); |
| 395 |
} |
| 396 |
if ( cg.time < cg.snap->serverTime ) { |
| 397 |
// this can happen right after a vid_restart |
| 398 |
cg.time = cg.snap->serverTime; |
| 399 |
} |
| 400 |
if ( cg.nextSnap != NULL && cg.nextSnap->serverTime <= cg.time ) { |
| 401 |
CG_Error( "CG_ProcessSnapshots: cg.nextSnap->serverTime <= cg.time" ); |
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|