| 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_predict.c -- this file generates cg.predictedPlayerState by either |
| 24 |
// interpolating between snapshots from the server or locally predicting |
| 25 |
// ahead the client's movement. |
| 26 |
// It also handles local physics interaction, like fragments bouncing off walls |
| 27 |
|
| 28 |
#include "cg_local.h" |
| 29 |
|
| 30 |
static pmove_t cg_pmove; |
| 31 |
|
| 32 |
static int cg_numSolidEntities; |
| 33 |
static centity_t *cg_solidEntities[MAX_ENTITIES_IN_SNAPSHOT]; |
| 34 |
static int cg_numTriggerEntities; |
| 35 |
static centity_t *cg_triggerEntities[MAX_ENTITIES_IN_SNAPSHOT]; |
| 36 |
|
| 37 |
/* |
| 38 |
==================== |
| 39 |
CG_BuildSolidList |
| 40 |
|
| 41 |
When a new cg.snap has been set, this function builds a sublist |
| 42 |
of the entities that are actually solid, to make for more |
| 43 |
efficient collision detection |
| 44 |
==================== |
| 45 |
*/ |
| 46 |
void CG_BuildSolidList( void ) { |
| 47 |
int i; |
| 48 |
centity_t *cent; |
| 49 |
snapshot_t *snap; |
| 50 |
entityState_t *ent; |
| 51 |
|
| 52 |
cg_numSolidEntities = 0; |
| 53 |
cg_numTriggerEntities = 0; |
| 54 |
|
| 55 |
if ( cg.nextSnap && !cg.nextFrameTeleport && !cg.thisFrameTeleport ) { |
| 56 |
snap = cg.nextSnap; |
| 57 |
} else { |
| 58 |
snap = cg.snap; |
| 59 |
} |
| 60 |
|
| 61 |
for ( i = 0 ; i < snap->numEntities ; i++ ) { |
| 62 |
cent = &cg_entities[ snap->entities[ i ].number ]; |
| 63 |
ent = ¢->currentState; |
| 64 |
|
| 65 |
if ( ent->eType == ET_ITEM || ent->eType == ET_PUSH_TRIGGER || ent->eType == ET_TELEPORT_TRIGGER ) { |
| 66 |
cg_triggerEntities[cg_numTriggerEntities] = cent; |
| 67 |
cg_numTriggerEntities++; |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
if ( cent->nextState.solid ) { |
| 72 |
cg_solidEntities[cg_numSolidEntities] = cent; |
| 73 |
cg_numSolidEntities++; |
| 74 |
continue; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
==================== |
| 81 |
CG_ClipMoveToEntities |
| 82 |
|
| 83 |
==================== |
| 84 |
*/ |
| 85 |
static void CG_ClipMoveToEntities ( const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, |
| 86 |
int skipNumber, int mask, trace_t *tr ) { |
| 87 |
int i, x, zd, zu; |
| 88 |
trace_t trace; |
| 89 |
entityState_t *ent; |
| 90 |
clipHandle_t cmodel; |
| 91 |
vec3_t bmins, bmaxs; |
| 92 |
vec3_t origin, angles; |
| 93 |
centity_t *cent; |
| 94 |
|
| 95 |
for ( i = 0 ; i < cg_numSolidEntities ; i++ ) { |
| 96 |
cent = cg_solidEntities[ i ]; |
| 97 |
ent = ¢->currentState; |
| 98 |
|
| 99 |
if ( ent->number == skipNumber ) { |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
if ( ent->solid == SOLID_BMODEL ) { |
| 104 |
// special value for bmodel |
| 105 |
cmodel = trap_CM_InlineModel( ent->modelindex ); |
| 106 |
VectorCopy( cent->lerpAngles, angles ); |
| 107 |
BG_EvaluateTrajectory( ¢->currentState.pos, cg.physicsTime, origin ); |
| 108 |
} else { |
| 109 |
// encoded bbox |
| 110 |
x = (ent->solid & 255); |
| 111 |
zd = ((ent->solid>>8) & 255); |
| 112 |
zu = ((ent->solid>>16) & 255) - 32; |
| 113 |
|
| 114 |
bmins[0] = bmins[1] = -x; |
| 115 |
bmaxs[0] = bmaxs[1] = x; |
| 116 |
bmins[2] = -zd; |
| 117 |
bmaxs[2] = zu; |
| 118 |
|
| 119 |
cmodel = trap_CM_TempBoxModel( bmins, bmaxs ); |
| 120 |
VectorCopy( vec3_origin, angles ); |
| 121 |
VectorCopy( cent->lerpOrigin, origin ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
trap_CM_TransformedBoxTrace ( &trace, start, end, |
| 126 |
mins, maxs, cmodel, mask, origin, angles); |
| 127 |
|
| 128 |
if (trace.allsolid || trace.fraction < tr->fraction) { |
| 129 |
trace.entityNum = ent->number; |
| 130 |
*tr = trace; |
| 131 |
} else if (trace.startsolid) { |
| 132 |
tr->startsolid = qtrue; |
| 133 |
} |
| 134 |
if ( tr->allsolid ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
================ |
| 142 |
CG_Trace |
| 143 |
================ |
| 144 |
*/ |
| 145 |
void CG_Trace( trace_t *result, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, |
| 146 |
int skipNumber, int mask ) { |
| 147 |
trace_t t; |
| 148 |
|
| 149 |
trap_CM_BoxTrace ( &t, start, end, mins, maxs, 0, mask); |
| 150 |
t.entityNum = t.fraction != 1.0 ? ENTITYNUM_WORLD : ENTITYNUM_NONE; |
| 151 |
// check all other solid models |
| 152 |
CG_ClipMoveToEntities (start, mins, maxs, end, skipNumber, mask, &t); |
| 153 |
|
| 154 |
*result = t; |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
================ |
| 159 |
CG_PointContents |
| 160 |
================ |
| 161 |
*/ |
| 162 |
int CG_PointContents( const vec3_t point, int passEntityNum ) { |
| 163 |
int i; |
| 164 |
entityState_t *ent; |
| 165 |
centity_t *cent; |
| 166 |
clipHandle_t cmodel; |
| 167 |
int contents; |
| 168 |
|
| 169 |
contents = trap_CM_PointContents (point, 0); |
| 170 |
|
| 171 |
for ( i = 0 ; i < cg_numSolidEntities ; i++ ) { |
| 172 |
cent = cg_solidEntities[ i ]; |
| 173 |
|
| 174 |
ent = ¢->currentState; |
| 175 |
|
| 176 |
if ( ent->number == passEntityNum ) { |
| 177 |
continue; |
| 178 |
} |
| 179 |
|
| 180 |
if (ent->solid != SOLID_BMODEL) { // special value for bmodel |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
cmodel = trap_CM_InlineModel( ent->modelindex ); |
| 185 |
if ( !cmodel ) { |
| 186 |
continue; |
| 187 |
} |
| 188 |
|
| 189 |
contents |= trap_CM_TransformedPointContents( point, cmodel, cent->lerpOrigin, cent->lerpAngles ); |
| 190 |
} |
| 191 |
|
| 192 |
return contents; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/* |
| 197 |
======================== |
| 198 |
CG_InterpolatePlayerState |
| 199 |
|
| 200 |
Generates cg.predictedPlayerState by interpolating between |
| 201 |
cg.snap->player_state and cg.nextFrame->player_state |
| 202 |
======================== |
| 203 |
*/ |
| 204 |
static void CG_InterpolatePlayerState( qboolean grabAngles ) { |
| 205 |
float f; |
| 206 |
int i; |
| 207 |
playerState_t *out; |
| 208 |
snapshot_t *prev, *next; |
| 209 |
|
| 210 |
out = &cg.predictedPlayerState; |
| 211 |
prev = cg.snap; |
| 212 |
next = cg.nextSnap; |
| 213 |
|
| 214 |
*out = cg.snap->ps; |
| 215 |
|
| 216 |
// if we are still allowing local input, short circuit the view angles |
| 217 |
if ( grabAngles ) { |
| 218 |
usercmd_t cmd; |
| 219 |
int cmdNum; |
| 220 |
|
| 221 |
cmdNum = trap_GetCurrentCmdNumber(); |
| 222 |
trap_GetUserCmd( cmdNum, &cmd ); |
| 223 |
|
| 224 |
PM_UpdateViewAngles( out, &cmd ); |
| 225 |
} |
| 226 |
|
| 227 |
// if the next frame is a teleport, we can't lerp to it |
| 228 |
if ( cg.nextFrameTeleport ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
if ( !next || next->serverTime <= prev->serverTime ) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
f = (float)( cg.time - prev->serverTime ) / ( next->serverTime - prev->serverTime ); |
| 237 |
|
| 238 |
i = next->ps.bobCycle; |
| 239 |
if ( i < prev->ps.bobCycle ) { |
| 240 |
i += 256; // handle wraparound |
| 241 |
} |
| 242 |
out->bobCycle = prev->ps.bobCycle + f * ( i - prev->ps.bobCycle ); |
| 243 |
|
| 244 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 245 |
out->origin[i] = prev->ps.origin[i] + f * (next->ps.origin[i] - prev->ps.origin[i] ); |
| 246 |
if ( !grabAngles ) { |
| 247 |
out->viewangles[i] = LerpAngle( |
| 248 |
prev->ps.viewangles[i], next->ps.viewangles[i], f ); |
| 249 |
} |
| 250 |
out->velocity[i] = prev->ps.velocity[i] + |
| 251 |
f * (next->ps.velocity[i] - prev->ps.velocity[i] ); |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
/* |
| 257 |
=================== |
| 258 |
CG_TouchItem |
| 259 |
=================== |
| 260 |
*/ |
| 261 |
static void CG_TouchItem( centity_t *cent ) { |
| 262 |
gitem_t *item; |
| 263 |
//For instantgib |
| 264 |
qboolean canBePicked; |
| 265 |
|
| 266 |
if(cgs.gametype == GT_ELIMINATION || cgs.gametype == GT_LMS) |
| 267 |
return; //No weapon pickup in elimination |
| 268 |
|
| 269 |
//normally we can |
| 270 |
canBePicked = qtrue; |
| 271 |
|
| 272 |
//But in instantgib, rocket arena, and CTF_ELIMINATION we normally can't: |
| 273 |
if(cgs.nopickup || cgs.gametype == GT_CTF_ELIMINATION) |
| 274 |
canBePicked = qfalse; |
| 275 |
|
| 276 |
if ( !cg_predictItems.integer ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
if ( !BG_PlayerTouchesItem( &cg.predictedPlayerState, ¢->currentState, cg.time ) ) { |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
// never pick an item up twice in a prediction |
| 284 |
if ( cent->miscTime == cg.time ) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
if ( !BG_CanItemBeGrabbed( cgs.gametype, ¢->currentState, &cg.predictedPlayerState ) ) { |
| 289 |
return; // can't hold it |
| 290 |
} |
| 291 |
|
| 292 |
item = &bg_itemlist[ cent->currentState.modelindex ]; |
| 293 |
|
| 294 |
// Special case for flags. |
| 295 |
// We don't predict touching our own flag |
| 296 |
#if 1 //MISSIONPACK |
| 297 |
if( cgs.gametype == GT_1FCTF ) { |
| 298 |
if( item->giTag != PW_NEUTRALFLAG ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
} |
| 302 |
if( cgs.gametype == GT_CTF || cgs.gametype == GT_CTF_ELIMINATION || cgs.gametype == GT_HARVESTER ) { |
| 303 |
#else |
| 304 |
if( cgs.gametype == GT_CTF || cgs.gametype == GT_CTF_ELIMINATION ) { |
| 305 |
#endif |
| 306 |
if (cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_RED && |
| 307 |
item->giTag == PW_REDFLAG) |
| 308 |
return; |
| 309 |
if (cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_BLUE && |
| 310 |
item->giTag == PW_BLUEFLAG) |
| 311 |
return; |
| 312 |
//Even in instantgib, we can predict our enemy flag |
| 313 |
if (cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_RED && |
| 314 |
item->giTag == PW_BLUEFLAG && (!(cgs.elimflags&EF_ONEWAY) || cgs.attackingTeam == TEAM_RED)) |
| 315 |
canBePicked = qtrue; |
| 316 |
if (cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_BLUE && |
| 317 |
item->giTag == PW_REDFLAG && (!(cgs.elimflags&EF_ONEWAY) || cgs.attackingTeam == TEAM_BLUE)) |
| 318 |
canBePicked = qtrue; |
| 319 |
if (item->giTag == WP_RAILGUN) |
| 320 |
canBePicked = qfalse; |
| 321 |
if (item->giTag == WP_PLASMAGUN) |
| 322 |
canBePicked = qfalse; |
| 323 |
} |
| 324 |
|
| 325 |
//Currently we don't predict anything in Double Domination because it looks like we take a flag |
| 326 |
if( cgs.gametype == GT_DOUBLE_D ) { |
| 327 |
if(cgs.redflag == TEAM_NONE) |
| 328 |
return; //Can never pick if just one flag is NONE (because then the other is too) |
| 329 |
if(item->giTag == PW_REDFLAG){ //at point A |
| 330 |
if(cgs.redflag != cg.predictedPlayerState.persistant[PERS_TEAM]) //not already taken |
| 331 |
trap_S_StartLocalSound( cgs.media.hitSound , CHAN_ANNOUNCER ); |
| 332 |
return; |
| 333 |
} |
| 334 |
if(item->giTag == PW_BLUEFLAG){ //at point B |
| 335 |
if(cgs.blueflag != cg.predictedPlayerState.persistant[PERS_TEAM]) //already taken |
| 336 |
trap_S_StartLocalSound( cgs.media.hitSound , CHAN_ANNOUNCER ); |
| 337 |
return; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// grab it |
| 342 |
if(canBePicked) |
| 343 |
{ |
| 344 |
BG_AddPredictableEventToPlayerstate( EV_ITEM_PICKUP, cent->currentState.modelindex , &cg.predictedPlayerState); |
| 345 |
|
| 346 |
// remove it from the frame so it won't be drawn |
| 347 |
cent->currentState.eFlags |= EF_NODRAW; |
| 348 |
|
| 349 |
// don't touch it again this prediction |
| 350 |
cent->miscTime = cg.time; |
| 351 |
|
| 352 |
// if its a weapon, give them some predicted ammo so the autoswitch will work |
| 353 |
if ( item->giType == IT_WEAPON ) { |
| 354 |
cg.predictedPlayerState.stats[ STAT_WEAPONS ] |= 1 << item->giTag; |
| 355 |
if ( !cg.predictedPlayerState.ammo[ item->giTag ] ) { |
| 356 |
cg.predictedPlayerState.ammo[ item->giTag ] = 1; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
/* |
| 364 |
========================= |
| 365 |
CG_TouchTriggerPrediction |
| 366 |
|
| 367 |
Predict push triggers and items |
| 368 |
========================= |
| 369 |
*/ |
| 370 |
static void CG_TouchTriggerPrediction( void ) { |
| 371 |
int i; |
| 372 |
trace_t trace; |
| 373 |
entityState_t *ent; |
| 374 |
clipHandle_t cmodel; |
| 375 |
centity_t *cent; |
| 376 |
qboolean spectator; |
| 377 |
|
| 378 |
// dead clients don't activate triggers |
| 379 |
if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) { |
| 380 |
return; |
| 381 |
} |
| 382 |
|
| 383 |
spectator = ( cg.predictedPlayerState.pm_type == PM_SPECTATOR ); |
| 384 |
|
| 385 |
if ( cg.predictedPlayerState.pm_type != PM_NORMAL && !spectator ) { |
| 386 |
return; |
| 387 |
} |
| 388 |
|
| 389 |
for ( i = 0 ; i < cg_numTriggerEntities ; i++ ) { |
| 390 |
cent = cg_triggerEntities[ i ]; |
| 391 |
ent = ¢->currentState; |
| 392 |
|
| 393 |
if ( ent->eType == ET_ITEM && !spectator ) { |
| 394 |
CG_TouchItem( cent ); |
| 395 |
continue; |
| 396 |
} |
| 397 |
|
| 398 |
if ( ent->solid != SOLID_BMODEL ) { |
| 399 |
continue; |
| 400 |
} |
| 401 |
|
| 402 |
cmodel = trap_CM_InlineModel( ent->modelindex ); |
| 403 |
if ( !cmodel ) { |
| 404 |
continue; |
| 405 |
} |
| 406 |
|
| 407 |
trap_CM_BoxTrace( &trace, cg.predictedPlayerState.origin, cg.predictedPlayerState.origin, |
| 408 |
cg_pmove.mins, cg_pmove.maxs, cmodel, -1 ); |
| 409 |
|
| 410 |
if ( !trace.startsolid ) { |
| 411 |
continue; |
| 412 |
} |
| 413 |
|
| 414 |
if ( ent->eType == ET_TELEPORT_TRIGGER ) { |
| 415 |
cg.hyperspace = qtrue; |
| 416 |
} else if ( ent->eType == ET_PUSH_TRIGGER ) { |
| 417 |
BG_TouchJumpPad( &cg.predictedPlayerState, ent ); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
// if we didn't touch a jump pad this pmove frame |
| 422 |
if ( cg.predictedPlayerState.jumppad_frame != cg.predictedPlayerState.pmove_framecount ) { |
| 423 |
cg.predictedPlayerState.jumppad_frame = 0; |
| 424 |
cg.predictedPlayerState.jumppad_ent = 0; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
//unlagged - optimized prediction |
| 429 |
#define ABS(x) ((x) < 0 ? (-(x)) : (x)) |
| 430 |
|
| 431 |
static int IsUnacceptableError( playerState_t *ps, playerState_t *pps ) { |
| 432 |
vec3_t delta; |
| 433 |
int i; |
| 434 |
|
| 435 |
if ( pps->pm_type != ps->pm_type || |
| 436 |
pps->pm_flags != ps->pm_flags || |
| 437 |
pps->pm_time != ps->pm_time ) { |
| 438 |
return 1; |
| 439 |
} |
| 440 |
|
| 441 |
VectorSubtract( pps->origin, ps->origin, delta ); |
| 442 |
if ( VectorLengthSquared( delta ) > 0.1f * 0.1f ) { |
| 443 |
if ( cg_showmiss.integer ) { |
| 444 |
CG_Printf("delta: %.2f ", VectorLength(delta) ); |
| 445 |
} |
| 446 |
return 2; |
| 447 |
} |
| 448 |
|
| 449 |
VectorSubtract( pps->velocity, ps->velocity, delta ); |
| 450 |
if ( VectorLengthSquared( delta ) > 0.1f * 0.1f ) { |
| 451 |
if ( cg_showmiss.integer ) { |
| 452 |
CG_Printf("delta: %.2f ", VectorLength(delta) ); |
| 453 |
} |
| 454 |
return 3; |
| 455 |
} |
| 456 |
|
| 457 |
if ( pps->weaponTime != ps->weaponTime || |
| 458 |
pps->gravity != ps->gravity || |
| 459 |
pps->speed != ps->speed || |
| 460 |
pps->delta_angles[0] != ps->delta_angles[0] || |
| 461 |
pps->delta_angles[1] != ps->delta_angles[1] || |
| 462 |
pps->delta_angles[2] != ps->delta_angles[2] || |
| 463 |
pps->groundEntityNum != ps->groundEntityNum ) { |
| 464 |
return 4; |
| 465 |
} |
| 466 |
|
| 467 |
if ( pps->legsTimer != ps->legsTimer || |
| 468 |
pps->legsAnim != ps->legsAnim || |
| 469 |
pps->torsoTimer != ps->torsoTimer || |
| 470 |
pps->torsoAnim != ps->torsoAnim || |
| 471 |
pps->movementDir != ps->movementDir ) { |
| 472 |
return 5; |
| 473 |
} |
| 474 |
|
| 475 |
VectorSubtract( pps->grapplePoint, ps->grapplePoint, delta ); |
| 476 |
if ( VectorLengthSquared( delta ) > 0.1f * 0.1f ) { |
| 477 |
return 6; |
| 478 |
} |
| 479 |
|
| 480 |
if ( pps->eFlags != ps->eFlags ) { |
| 481 |
return 7; |
| 482 |
} |
| 483 |
|
| 484 |
if ( pps->eventSequence != ps->eventSequence ) { |
| 485 |
return 8; |
| 486 |
} |
| 487 |
|
| 488 |
for ( i = 0; i < MAX_PS_EVENTS; i++ ) { |
| 489 |
if ( pps->events[i] != ps->events[i] || |
| 490 |
pps->eventParms[i] != ps->eventParms[i] ) { |
| 491 |
return 9; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
if ( pps->externalEvent != ps->externalEvent || |
| 496 |
pps->externalEventParm != ps->externalEventParm || |
| 497 |
pps->externalEventTime != ps->externalEventTime ) { |
| 498 |
return 10; |
| 499 |
} |
| 500 |
|
| 501 |
if ( pps->clientNum != ps->clientNum || |
| 502 |
pps->weapon != ps->weapon || |
| 503 |
pps->weaponstate != ps->weaponstate ) { |
| 504 |
return 11; |
| 505 |
} |
| 506 |
|
| 507 |
if ( ABS(pps->viewangles[0] - ps->viewangles[0]) > 1.0f || |
| 508 |
ABS(pps->viewangles[1] - ps->viewangles[1]) > 1.0f || |
| 509 |
ABS(pps->viewangles[2] - ps->viewangles[2]) > 1.0f ) { |
| 510 |
return 12; |
| 511 |
} |
| 512 |
|
| 513 |
if ( pps->viewheight != ps->viewheight ) { |
| 514 |
return 13; |
| 515 |
} |
| 516 |
|
| 517 |
if ( pps->damageEvent != ps->damageEvent || |
| 518 |
pps->damageYaw != ps->damageYaw || |
| 519 |
pps->damagePitch != ps->damagePitch || |
| 520 |
pps->damageCount != ps->damageCount ) { |
| 521 |
return 14; |
| 522 |
} |
| 523 |
|
| 524 |
for ( i = 0; i < MAX_STATS; i++ ) { |
| 525 |
if ( pps->stats[i] != ps->stats[i] ) { |
| 526 |
return 15; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
for ( i = 0; i < MAX_PERSISTANT; i++ ) { |
| 531 |
if ( pps->persistant[i] != ps->persistant[i] ) { |
| 532 |
return 16; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
for ( i = 0; i < MAX_POWERUPS; i++ ) { |
| 537 |
if ( pps->powerups[i] != ps->powerups[i] ) { |
| 538 |
return 17; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
for ( i = 0; i < MAX_WEAPONS; i++ ) { |
| 543 |
if ( pps->ammo[i] != ps->ammo[i] ) { |
| 544 |
return 18; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
if ( pps->generic1 != ps->generic1 || |
| 549 |
pps->loopSound != ps->loopSound || |
| 550 |
pps->jumppad_ent != ps->jumppad_ent ) { |
| 551 |
return 19; |
| 552 |
} |
| 553 |
|
| 554 |
return 0; |
| 555 |
} |
| 556 |
//unlagged - optimized prediction |
| 557 |
|
| 558 |
/* |
| 559 |
================= |
| 560 |
CG_PredictPlayerState |
| 561 |
|
| 562 |
Generates cg.predictedPlayerState for the current cg.time |
| 563 |
cg.predictedPlayerState is guaranteed to be valid after exiting. |
| 564 |
|
| 565 |
For demo playback, this will be an interpolation between two valid |
| 566 |
playerState_t. |
| 567 |
|
| 568 |
For normal gameplay, it will be the result of predicted usercmd_t on |
| 569 |
top of the most recent playerState_t received from the server. |
| 570 |
|
| 571 |
Each new snapshot will usually have one or more new usercmd over the last, |
| 572 |
but we simulate all unacknowledged commands each time, not just the new ones. |
| 573 |
This means that on an internet connection, quite a few pmoves may be issued |
| 574 |
each frame. |
| 575 |
|
| 576 |
OPTIMIZE: don't re-simulate unless the newly arrived snapshot playerState_t |
| 577 |
differs from the predicted one. Would require saving all intermediate |
| 578 |
playerState_t during prediction. |
| 579 |
|
| 580 |
We detect prediction errors and allow them to be decayed off over several frames |
| 581 |
to ease the jerk. |
| 582 |
================= |
| 583 |
*/ |
| 584 |
void CG_PredictPlayerState( void ) { |
| 585 |
int cmdNum, current; |
| 586 |
playerState_t oldPlayerState; |
| 587 |
qboolean moved; |
| 588 |
usercmd_t oldestCmd; |
| 589 |
usercmd_t latestCmd; |
| 590 |
//unlagged - optimized prediction |
| 591 |
int stateIndex = 0, predictCmd = 0; //Sago: added initializing |
| 592 |
int numPredicted = 0, numPlayedBack = 0; // debug code |
| 593 |
//unlagged - optimized prediction |
| 594 |
|
| 595 |
cg.hyperspace = qfalse; // will be set if touching a trigger_teleport |
| 596 |
|
| 597 |
// if this is the first frame we must guarantee |
| 598 |
// predictedPlayerState is valid even if there is some |
| 599 |
// other error condition |
| 600 |
if ( !cg.validPPS ) { |
| 601 |
cg.validPPS = qtrue; |
| 602 |
cg.predictedPlayerState = cg.snap->ps; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
// demo playback just copies the moves |
| 607 |
if ( cg.demoPlayback || (cg.snap->ps.pm_flags & PMF_FOLLOW) ) { |
| 608 |
CG_InterpolatePlayerState( qfalse ); |
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
// non-predicting local movement will grab the latest angles |
| 613 |
if ( cg_nopredict.integer || cg_synchronousClients.integer ) { |
| 614 |
CG_InterpolatePlayerState( qtrue ); |
| 615 |
return; |
| 616 |
} |
| 617 |
|
| 618 |
// prepare for pmove |
| 619 |
cg_pmove.ps = &cg.predictedPlayerState; |
| 620 |
cg_pmove.trace = CG_Trace; |
| 621 |
cg_pmove.pointcontents = CG_PointContents; |
| 622 |
if ( cg_pmove.ps->pm_type == PM_DEAD ) { |
| 623 |
cg_pmove.tracemask = MASK_PLAYERSOLID & ~CONTENTS_BODY; |
| 624 |
} |
| 625 |
else { |
| 626 |
cg_pmove.tracemask = MASK_PLAYERSOLID; |
| 627 |
} |
| 628 |
if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR ) { |
| 629 |
cg_pmove.tracemask &= ~CONTENTS_BODY; // spectators can fly through bodies |
| 630 |
} |
| 631 |
cg_pmove.noFootsteps = ( cgs.dmflags & DF_NO_FOOTSTEPS ) > 0; |
| 632 |
|
| 633 |
// save the state before the pmove so we can detect transitions |
| 634 |
oldPlayerState = cg.predictedPlayerState; |
| 635 |
|
| 636 |
current = trap_GetCurrentCmdNumber(); |
| 637 |
|
| 638 |
// if we don't have the commands right after the snapshot, we |
| 639 |
// can't accurately predict a current position, so just freeze at |
| 640 |
// the last good position we had |
| 641 |
cmdNum = current - CMD_BACKUP + 1; |
| 642 |
trap_GetUserCmd( cmdNum, &oldestCmd ); |
| 643 |
if ( oldestCmd.serverTime > cg.snap->ps.commandTime |
| 644 |
&& oldestCmd.serverTime < cg.time ) { // special check for map_restart |
| 645 |
if ( cg_showmiss.integer ) { |
| 646 |
CG_Printf ("exceeded PACKET_BACKUP on commands\n"); |
| 647 |
} |
| 648 |
return; |
| 649 |
} |
| 650 |
|
| 651 |
// get the latest command so we can know which commands are from previous map_restarts |
| 652 |
trap_GetUserCmd( current, &latestCmd ); |
| 653 |
|
| 654 |
// get the most recent information we have, even if |
| 655 |
// the server time is beyond our current cg.time, |
| 656 |
// because predicted player positions are going to |
| 657 |
// be ahead of everything else anyway |
| 658 |
if ( cg.nextSnap && !cg.nextFrameTeleport && !cg.thisFrameTeleport ) { |
| 659 |
cg.predictedPlayerState = cg.nextSnap->ps; |
| 660 |
cg.physicsTime = cg.nextSnap->serverTime; |
| 661 |
} else { |
| 662 |
cg.predictedPlayerState = cg.snap->ps; |
| 663 |
cg.physicsTime = cg.snap->serverTime; |
| 664 |
} |
| 665 |
|
| 666 |
if ( pmove_msec.integer < 8 ) { |
| 667 |
trap_Cvar_Set("pmove_msec", "8"); |
| 668 |
} |
| 669 |
else if (pmove_msec.integer > 33) { |
| 670 |
trap_Cvar_Set("pmove_msec", "33"); |
| 671 |
} |
| 672 |
|
| 673 |
cg_pmove.pmove_fixed = pmove_fixed.integer;// | cg_pmove_fixed.integer; |
| 674 |
cg_pmove.pmove_msec = pmove_msec.integer; |
| 675 |
cg_pmove.pmove_float = pmove_float.integer; |
| 676 |
cg_pmove.pmove_flags = cgs.dmflags; |
| 677 |
|
| 678 |
|
| 679 |
//unlagged - optimized prediction |
| 680 |
// Like the comments described above, a player's state is entirely |
| 681 |
// re-predicted from the last valid snapshot every client frame, which |
| 682 |
// can be really, really, really slow. Every old command has to be |
| 683 |
// run again. For every client frame that is *not* directly after a |
| 684 |
// snapshot, this is unnecessary, since we have no new information. |
| 685 |
// For those, we'll play back the predictions from the last frame and |
| 686 |
// predict only the newest commands. Essentially, we'll be doing |
| 687 |
// an incremental predict instead of a full predict. |
| 688 |
// |
| 689 |
// If we have a new snapshot, we can compare its player state's command |
| 690 |
// time to the command times in the queue to find a match. If we find |
| 691 |
// a matching state, and the predicted version has not deviated, we can |
| 692 |
// use the predicted state as a base - and also do an incremental predict. |
| 693 |
// |
| 694 |
// With this method, we get incremental predicts on every client frame |
| 695 |
// except a frame following a new snapshot in which there was a prediction |
| 696 |
// error. This yeilds anywhere from a 15% to 40% performance increase, |
| 697 |
// depending on how much of a bottleneck the CPU is. |
| 698 |
|
| 699 |
if ( cg_optimizePrediction.integer ) { |
| 700 |
if ( cg.nextFrameTeleport || cg.thisFrameTeleport ) { |
| 701 |
// do a full predict |
| 702 |
cg.lastPredictedCommand = 0; |
| 703 |
cg.stateTail = cg.stateHead; |
| 704 |
predictCmd = current - CMD_BACKUP + 1; |
| 705 |
} |
| 706 |
// cg.physicsTime is the current snapshot's serverTime |
| 707 |
// if it's the same as the last one |
| 708 |
else if ( cg.physicsTime == cg.lastServerTime ) { |
| 709 |
// we have no new information, so do an incremental predict |
| 710 |
predictCmd = cg.lastPredictedCommand + 1; |
| 711 |
} |
| 712 |
else { |
| 713 |
// we have a new snapshot |
| 714 |
|
| 715 |
int i; |
| 716 |
qboolean error = qtrue; |
| 717 |
|
| 718 |
// loop through the saved states queue |
| 719 |
for ( i = cg.stateHead; i != cg.stateTail; i = (i + 1) % NUM_SAVED_STATES ) { |
| 720 |
// if we find a predicted state whose commandTime matches the snapshot player state's commandTime |
| 721 |
if ( cg.savedPmoveStates[i].commandTime == cg.predictedPlayerState.commandTime ) { |
| 722 |
// make sure the state differences are acceptable |
| 723 |
int errorcode = IsUnacceptableError( &cg.predictedPlayerState, &cg.savedPmoveStates[i] ); |
| 724 |
|
| 725 |
// too much change? |
| 726 |
if ( errorcode ) { |
| 727 |
if ( cg_showmiss.integer ) { |
| 728 |
CG_Printf("errorcode %d at %d\n", errorcode, cg.time); |
| 729 |
} |
| 730 |
// yeah, so do a full predict |
| 731 |
break; |
| 732 |
} |
| 733 |
|
| 734 |
// this one is almost exact, so we'll copy it in as the starting point |
| 735 |
*cg_pmove.ps = cg.savedPmoveStates[i]; |
| 736 |
// advance the head |
| 737 |
cg.stateHead = (i + 1) % NUM_SAVED_STATES; |
| 738 |
|
| 739 |
// set the next command to predict |
| 740 |
predictCmd = cg.lastPredictedCommand + 1; |
| 741 |
|
| 742 |
// a saved state matched, so flag it |
| 743 |
error = qfalse; |
| 744 |
break; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
// if no saved states matched |
| 749 |
if ( error ) { |
| 750 |
// do a full predict |
| 751 |
cg.lastPredictedCommand = 0; |
| 752 |
cg.stateTail = cg.stateHead; |
| 753 |
predictCmd = current - CMD_BACKUP + 1; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
// keep track of the server time of the last snapshot so we |
| 758 |
// know when we're starting from a new one in future calls |
| 759 |
cg.lastServerTime = cg.physicsTime; |
| 760 |
stateIndex = cg.stateHead; |
| 761 |
} |
| 762 |
//unlagged - optimized prediction |
| 763 |
|
| 764 |
// run cmds |
| 765 |
moved = qfalse; |
| 766 |
for ( cmdNum = current - CMD_BACKUP + 1 ; cmdNum <= current ; cmdNum++ ) { |
| 767 |
// get the command |
| 768 |
trap_GetUserCmd( cmdNum, &cg_pmove.cmd ); |
| 769 |
|
| 770 |
if ( cg_pmove.pmove_fixed ) { |
| 771 |
PM_UpdateViewAngles( cg_pmove.ps, &cg_pmove.cmd ); |
| 772 |
} |
| 773 |
|
| 774 |
// don't do anything if the time is before the snapshot player time |
| 775 |
if ( cg_pmove.cmd.serverTime <= cg.predictedPlayerState.commandTime ) { |
| 776 |
continue; |
| 777 |
} |
| 778 |
|
| 779 |
// don't do anything if the command was from a previous map_restart |
| 780 |
if ( cg_pmove.cmd.serverTime > latestCmd.serverTime ) { |
| 781 |
continue; |
| 782 |
} |
| 783 |
|
| 784 |
// check for a prediction error from last frame |
| 785 |
// on a lan, this will often be the exact value |
| 786 |
// from the snapshot, but on a wan we will have |
| 787 |
// to predict several commands to get to the point |
| 788 |
// we want to compare |
| 789 |
if ( cg.predictedPlayerState.commandTime == oldPlayerState.commandTime ) { |
| 790 |
vec3_t delta; |
| 791 |
float len; |
| 792 |
|
| 793 |
if ( cg.thisFrameTeleport ) { |
| 794 |
// a teleport will not cause an error decay |
| 795 |
VectorClear( cg.predictedError ); |
| 796 |
if ( cg_showmiss.integer ) { |
| 797 |
CG_Printf( "PredictionTeleport\n" ); |
| 798 |
} |
| 799 |
cg.thisFrameTeleport = qfalse; |
| 800 |
} else { |
| 801 |
vec3_t adjusted; |
| 802 |
CG_AdjustPositionForMover( cg.predictedPlayerState.origin, |
| 803 |
cg.predictedPlayerState.groundEntityNum, cg.physicsTime, cg.oldTime, adjusted ); |
| 804 |
|
| 805 |
if ( cg_showmiss.integer ) { |
| 806 |
if (!VectorCompare( oldPlayerState.origin, adjusted )) { |
| 807 |
CG_Printf("prediction error\n"); |
| 808 |
} |
| 809 |
} |
| 810 |
VectorSubtract( oldPlayerState.origin, adjusted, delta ); |
| 811 |
len = VectorLength( delta ); |
| 812 |
if ( len > 0.1 ) { |
| 813 |
if ( cg_showmiss.integer ) { |
| 814 |
CG_Printf("Prediction miss: %f\n", len); |
| 815 |
} |
| 816 |
if ( cg_errorDecay.integer ) { |
| 817 |
int t; |
| 818 |
float f; |
| 819 |
|
| 820 |
t = cg.time - cg.predictedErrorTime; |
| 821 |
f = ( cg_errorDecay.value - t ) / cg_errorDecay.value; |
| 822 |
if ( f < 0 ) { |
| 823 |
f = 0; |
| 824 |
} |
| 825 |
if ( f > 0 && cg_showmiss.integer ) { |
| 826 |
CG_Printf("Double prediction decay: %f\n", f); |
| 827 |
} |
| 828 |
VectorScale( cg.predictedError, f, cg.predictedError ); |
| 829 |
} else { |
| 830 |
VectorClear( cg.predictedError ); |
| 831 |
} |
| 832 |
VectorAdd( delta, cg.predictedError, cg.predictedError ); |
| 833 |
cg.predictedErrorTime = cg.oldTime; |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
// don't predict gauntlet firing, which is only supposed to happen |
| 839 |
// when it actually inflicts damage |
| 840 |
cg_pmove.gauntletHit = qfalse; |
| 841 |
|
| 842 |
if ( cg_pmove.pmove_fixed ) { |
| 843 |
cg_pmove.cmd.serverTime = ((cg_pmove.cmd.serverTime + pmove_msec.integer-1) / pmove_msec.integer) * pmove_msec.integer; |
| 844 |
} |
| 845 |
|
| 846 |
//unlagged - optimized prediction |
| 847 |
if ( cg_optimizePrediction.integer ) { |
| 848 |
// if we need to predict this command, or we've run out of space in the saved states queue |
| 849 |
if ( cmdNum >= predictCmd || (stateIndex + 1) % NUM_SAVED_STATES == cg.stateHead ) { |
| 850 |
// run the Pmove |
| 851 |
Pmove (&cg_pmove); |
| 852 |
|
| 853 |
numPredicted++; // debug code |
| 854 |
|
| 855 |
// record the last predicted command |
| 856 |
cg.lastPredictedCommand = cmdNum; |
| 857 |
|
| 858 |
// if we haven't run out of space in the saved states queue |
| 859 |
if ( (stateIndex + 1) % NUM_SAVED_STATES != cg.stateHead ) { |
| 860 |
// save the state for the false case (of cmdNum >= predictCmd) |
| 861 |
// in later calls to this function |
| 862 |
cg.savedPmoveStates[stateIndex] = *cg_pmove.ps; |
| 863 |
stateIndex = (stateIndex + 1) % NUM_SAVED_STATES; |
| 864 |
cg.stateTail = stateIndex; |
| 865 |
} |
| 866 |
} |
| 867 |
else { |
| 868 |
numPlayedBack++; // debug code |
| 869 |
|
| 870 |
if ( cg_showmiss.integer && |
| 871 |
cg.savedPmoveStates[stateIndex].commandTime != cg_pmove.cmd.serverTime) { |
| 872 |
// this should ONLY happen just after changing the value of pmove_fixed |
| 873 |
CG_Printf( "saved state miss\n" ); |
| 874 |
} |
| 875 |
|
| 876 |
// play back the command from the saved states |
| 877 |
*cg_pmove.ps = cg.savedPmoveStates[stateIndex]; |
| 878 |
|
| 879 |
// go to the next element in the saved states array |
| 880 |
stateIndex = (stateIndex + 1) % NUM_SAVED_STATES; |
| 881 |
} |
| 882 |
} |
| 883 |
else { |
| 884 |
// run the Pmove |
| 885 |
Pmove (&cg_pmove); |
| 886 |
|
| 887 |
numPredicted++; // debug code |
| 888 |
} |
| 889 |
//unlagged - optimized prediction |
| 890 |
|
| 891 |
moved = qtrue; |
| 892 |
|
| 893 |
// add push trigger movement effects |
| 894 |
CG_TouchTriggerPrediction(); |
| 895 |
|
| 896 |
// check for predictable events that changed from previous predictions |
| 897 |
//CG_CheckChangedPredictableEvents(&cg.predictedPlayerState); |
| 898 |
} |
| 899 |
|
| 900 |
//unlagged - optimized prediction |
| 901 |
// do a /condump after a few seconds of this |
| 902 |
//CG_Printf("cg.time: %d, numPredicted: %d, numPlayedBack: %d\n", cg.time, numPredicted, numPlayedBack); // debug code |
| 903 |
// if everything is working right, numPredicted should be 1 more than 98% |
| 904 |
// of the time, meaning only ONE predicted move was done in the frame |
| 905 |
// you should see other values for numPredicted after IsUnacceptableError |
| 906 |
// returns nonzero, and that's it |
| 907 |
//unlagged - optimized prediction |
| 908 |
|
| 909 |
if ( cg_showmiss.integer > 1 ) { |
| 910 |
CG_Printf( "[%i : %i] ", cg_pmove.cmd.serverTime, cg.time ); |
| 911 |
} |
| 912 |
|
| 913 |
if ( !moved ) { |
| 914 |
if ( cg_showmiss.integer ) { |
| 915 |
CG_Printf( "not moved\n" ); |
| 916 |
} |
| 917 |
return; |
| 918 |
} |
| 919 |
|
| 920 |
// adjust for the movement of the groundentity |
| 921 |
CG_AdjustPositionForMover( cg.predictedPlayerState.origin, |
| 922 |
cg.predictedPlayerState.groundEntityNum, |
| 923 |
cg.physicsTime, cg.time, cg.predictedPlayerState.origin ); |
| 924 |
|
| 925 |
if ( cg_showmiss.integer ) { |
| 926 |
if (cg.predictedPlayerState.eventSequence > oldPlayerState.eventSequence + MAX_PS_EVENTS) { |
| 927 |
CG_Printf("WARNING: dropped event\n"); |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
// fire events and other transition triggered things |
| 932 |
CG_TransitionPlayerState( &cg.predictedPlayerState, &oldPlayerState ); |
| 933 |
|
| 934 |
if ( cg_showmiss.integer ) { |
| 935 |
if (cg.eventSequence > cg.predictedPlayerState.eventSequence) { |
| 936 |
CG_Printf("WARNING: double event\n"); |
| 937 |
cg.eventSequence = cg.predictedPlayerState.eventSequence; |
| 938 |
} |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
|