cg_predict.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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_predict.c -- this file generates cg.predictedPlayerState by either
  20. // interpolating between snapshots from the server or locally predicting
  21. // ahead the client's movement.
  22. // It also handles local physics interaction, like fragments bouncing off walls
  23. #include "cg_local.h"
  24. static pmove_t cg_pmove;
  25. static int cg_numSolidEntities;
  26. static centity_t *cg_solidEntities[MAX_ENTITIES_IN_SNAPSHOT];
  27. static int cg_numTriggerEntities;
  28. static centity_t *cg_triggerEntities[MAX_ENTITIES_IN_SNAPSHOT];
  29. /*
  30. ====================
  31. CG_BuildSolidList
  32. When a new cg.snap has been set, this function builds a sublist
  33. of the entities that are actually solid, to make for more
  34. efficient collision detection
  35. ====================
  36. */
  37. void CG_BuildSolidList( void ) {
  38. int i;
  39. centity_t *cent;
  40. snapshot_t *snap;
  41. entityState_t *ent;
  42. cg_numSolidEntities = 0;
  43. cg_numTriggerEntities = 0;
  44. if ( cg.nextSnap && !cg.nextFrameTeleport && !cg.thisFrameTeleport ) {
  45. snap = cg.nextSnap;
  46. } else {
  47. snap = cg.snap;
  48. }
  49. for ( i = 0 ; i < snap->numEntities ; i++ ) {
  50. cent = &cg_entities[ snap->entities[ i ].number ];
  51. ent = &cent->currentState;
  52. if ( ent->eType == ET_ITEM || ent->eType == ET_PUSH_TRIGGER || ent->eType == ET_TELEPORT_TRIGGER ) {
  53. cg_triggerEntities[cg_numTriggerEntities] = cent;
  54. cg_numTriggerEntities++;
  55. continue;
  56. }
  57. if ( cent->nextState.solid ) {
  58. cg_solidEntities[cg_numSolidEntities] = cent;
  59. cg_numSolidEntities++;
  60. continue;
  61. }
  62. }
  63. }
  64. /*
  65. ====================
  66. CG_ClipMoveToEntities
  67. ====================
  68. */
  69. static void CG_ClipMoveToEntities ( const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end,
  70. int skipNumber, int mask, trace_t *tr ) {
  71. int i, x, zd, zu;
  72. trace_t trace;
  73. entityState_t *ent;
  74. clipHandle_t cmodel;
  75. vec3_t bmins, bmaxs;
  76. vec3_t origin, angles;
  77. centity_t *cent;
  78. for ( i = 0 ; i < cg_numSolidEntities ; i++ ) {
  79. cent = cg_solidEntities[ i ];
  80. ent = &cent->currentState;
  81. if ( ent->number == skipNumber ) {
  82. continue;
  83. }
  84. if ( ent->solid == SOLID_BMODEL ) {
  85. // special value for bmodel
  86. cmodel = trap_CM_InlineModel( ent->modelindex );
  87. VectorCopy( cent->lerpAngles, angles );
  88. BG_EvaluateTrajectory( &cent->currentState.pos, cg.physicsTime, origin );
  89. } else {
  90. // encoded bbox
  91. x = (ent->solid & 255);
  92. zd = ((ent->solid>>8) & 255);
  93. zu = ((ent->solid>>16) & 255) - 32;
  94. bmins[0] = bmins[1] = -x;
  95. bmaxs[0] = bmaxs[1] = x;
  96. bmins[2] = -zd;
  97. bmaxs[2] = zu;
  98. cmodel = trap_CM_TempBoxModel( bmins, bmaxs );
  99. VectorCopy( vec3_origin, angles );
  100. VectorCopy( cent->lerpOrigin, origin );
  101. }
  102. trap_CM_TransformedBoxTrace ( &trace, start, end,
  103. mins, maxs, cmodel, mask, origin, angles);
  104. if (trace.allsolid || trace.fraction < tr->fraction) {
  105. trace.entityNum = ent->number;
  106. *tr = trace;
  107. } else if (trace.startsolid) {
  108. tr->startsolid = qtrue;
  109. }
  110. if ( tr->allsolid ) {
  111. return;
  112. }
  113. }
  114. }
  115. /*
  116. ================
  117. CG_Trace
  118. ================
  119. */
  120. void CG_Trace( trace_t *result, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end,
  121. int skipNumber, int mask ) {
  122. trace_t t;
  123. trap_CM_BoxTrace ( &t, start, end, mins, maxs, 0, mask);
  124. t.entityNum = t.fraction != 1.0 ? ENTITYNUM_WORLD : ENTITYNUM_NONE;
  125. // check all other solid models
  126. CG_ClipMoveToEntities (start, mins, maxs, end, skipNumber, mask, &t);
  127. *result = t;
  128. }
  129. /*
  130. ================
  131. CG_PointContents
  132. ================
  133. */
  134. int CG_PointContents( const vec3_t point, int passEntityNum ) {
  135. int i;
  136. entityState_t *ent;
  137. centity_t *cent;
  138. clipHandle_t cmodel;
  139. int contents;
  140. contents = trap_CM_PointContents (point, 0);
  141. for ( i = 0 ; i < cg_numSolidEntities ; i++ ) {
  142. cent = cg_solidEntities[ i ];
  143. ent = &cent->currentState;
  144. if ( ent->number == passEntityNum ) {
  145. continue;
  146. }
  147. if (ent->solid != SOLID_BMODEL) { // special value for bmodel
  148. continue;
  149. }
  150. cmodel = trap_CM_InlineModel( ent->modelindex );
  151. if ( !cmodel ) {
  152. continue;
  153. }
  154. contents |= trap_CM_TransformedPointContents( point, cmodel, ent->origin, ent->angles );
  155. }
  156. return contents;
  157. }
  158. /*
  159. ========================
  160. CG_InterpolatePlayerState
  161. Generates cg.predictedPlayerState by interpolating between
  162. cg.snap->player_state and cg.nextFrame->player_state
  163. ========================
  164. */
  165. static void CG_InterpolatePlayerState( qboolean grabAngles ) {
  166. float f;
  167. int i;
  168. playerState_t *out;
  169. snapshot_t *prev, *next;
  170. out = &cg.predictedPlayerState;
  171. prev = cg.snap;
  172. next = cg.nextSnap;
  173. *out = cg.snap->ps;
  174. // if we are still allowing local input, short circuit the view angles
  175. if ( grabAngles ) {
  176. usercmd_t cmd;
  177. int cmdNum;
  178. cmdNum = trap_GetCurrentCmdNumber();
  179. trap_GetUserCmd( cmdNum, &cmd );
  180. PM_UpdateViewAngles( out, &cmd );
  181. }
  182. // if the next frame is a teleport, we can't lerp to it
  183. if ( cg.nextFrameTeleport ) {
  184. return;
  185. }
  186. if ( !next || next->serverTime <= prev->serverTime ) {
  187. return;
  188. }
  189. f = (float)( cg.time - prev->serverTime ) / ( next->serverTime - prev->serverTime );
  190. i = next->ps.bobCycle;
  191. if ( i < prev->ps.bobCycle ) {
  192. i += 256; // handle wraparound
  193. }
  194. out->bobCycle = prev->ps.bobCycle + f * ( i - prev->ps.bobCycle );
  195. for ( i = 0 ; i < 3 ; i++ ) {
  196. out->origin[i] = prev->ps.origin[i] + f * (next->ps.origin[i] - prev->ps.origin[i] );
  197. if ( !grabAngles ) {
  198. out->viewangles[i] = LerpAngle(
  199. prev->ps.viewangles[i], next->ps.viewangles[i], f );
  200. }
  201. out->velocity[i] = prev->ps.velocity[i] +
  202. f * (next->ps.velocity[i] - prev->ps.velocity[i] );
  203. }
  204. }
  205. /*
  206. ===================
  207. CG_TouchItem
  208. ===================
  209. */
  210. static void CG_TouchItem( centity_t *cent ) {
  211. gitem_t *item;
  212. if ( !cg_predictItems.integer ) {
  213. return;
  214. }
  215. if ( !BG_PlayerTouchesItem( &cg.predictedPlayerState, &cent->currentState, cg.time ) ) {
  216. return;
  217. }
  218. // never pick an item up twice in a prediction
  219. if ( cent->miscTime == cg.time ) {
  220. return;
  221. }
  222. if ( !BG_CanItemBeGrabbed( cgs.gametype, &cent->currentState, &cg.predictedPlayerState ) ) {
  223. return; // can't hold it
  224. }
  225. item = &bg_itemlist[ cent->currentState.modelindex ];
  226. // Special case for flags.
  227. // We don't predict touching our own flag
  228. #ifdef MISSIONPACK
  229. if( cgs.gametype == GT_1FCTF ) {
  230. if( item->giTag != PW_NEUTRALFLAG ) {
  231. return;
  232. }
  233. }
  234. if( cgs.gametype == GT_CTF || cgs.gametype == GT_HARVESTER ) {
  235. #else
  236. if( cgs.gametype == GT_CTF ) {
  237. #endif
  238. if (cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_RED &&
  239. item->giTag == PW_REDFLAG)
  240. return;
  241. if (cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_BLUE &&
  242. item->giTag == PW_BLUEFLAG)
  243. return;
  244. }
  245. // grab it
  246. BG_AddPredictableEventToPlayerstate( EV_ITEM_PICKUP, cent->currentState.modelindex , &cg.predictedPlayerState);
  247. // remove it from the frame so it won't be drawn
  248. cent->currentState.eFlags |= EF_NODRAW;
  249. // don't touch it again this prediction
  250. cent->miscTime = cg.time;
  251. // if its a weapon, give them some predicted ammo so the autoswitch will work
  252. if ( item->giType == IT_WEAPON ) {
  253. cg.predictedPlayerState.stats[ STAT_WEAPONS ] |= 1 << item->giTag;
  254. if ( !cg.predictedPlayerState.ammo[ item->giTag ] ) {
  255. cg.predictedPlayerState.ammo[ item->giTag ] = 1;
  256. }
  257. }
  258. }
  259. /*
  260. =========================
  261. CG_TouchTriggerPrediction
  262. Predict push triggers and items
  263. =========================
  264. */
  265. static void CG_TouchTriggerPrediction( void ) {
  266. int i;
  267. trace_t trace;
  268. entityState_t *ent;
  269. clipHandle_t cmodel;
  270. centity_t *cent;
  271. qboolean spectator;
  272. // dead clients don't activate triggers
  273. if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) {
  274. return;
  275. }
  276. spectator = ( cg.predictedPlayerState.pm_type == PM_SPECTATOR );
  277. if ( cg.predictedPlayerState.pm_type != PM_NORMAL && !spectator ) {
  278. return;
  279. }
  280. for ( i = 0 ; i < cg_numTriggerEntities ; i++ ) {
  281. cent = cg_triggerEntities[ i ];
  282. ent = &cent->currentState;
  283. if ( ent->eType == ET_ITEM && !spectator ) {
  284. CG_TouchItem( cent );
  285. continue;
  286. }
  287. if ( ent->solid != SOLID_BMODEL ) {
  288. continue;
  289. }
  290. cmodel = trap_CM_InlineModel( ent->modelindex );
  291. if ( !cmodel ) {
  292. continue;
  293. }
  294. trap_CM_BoxTrace( &trace, cg.predictedPlayerState.origin, cg.predictedPlayerState.origin,
  295. cg_pmove.mins, cg_pmove.maxs, cmodel, -1 );
  296. if ( !trace.startsolid ) {
  297. continue;
  298. }
  299. if ( ent->eType == ET_TELEPORT_TRIGGER ) {
  300. cg.hyperspace = qtrue;
  301. } else if ( ent->eType == ET_PUSH_TRIGGER ) {
  302. BG_TouchJumpPad( &cg.predictedPlayerState, ent );
  303. }
  304. }
  305. // if we didn't touch a jump pad this pmove frame
  306. if ( cg.predictedPlayerState.jumppad_frame != cg.predictedPlayerState.pmove_framecount ) {
  307. cg.predictedPlayerState.jumppad_frame = 0;
  308. cg.predictedPlayerState.jumppad_ent = 0;
  309. }
  310. }
  311. /*
  312. =================
  313. CG_PredictPlayerState
  314. Generates cg.predictedPlayerState for the current cg.time
  315. cg.predictedPlayerState is guaranteed to be valid after exiting.
  316. For demo playback, this will be an interpolation between two valid
  317. playerState_t.
  318. For normal gameplay, it will be the result of predicted usercmd_t on
  319. top of the most recent playerState_t received from the server.
  320. Each new snapshot will usually have one or more new usercmd over the last,
  321. but we simulate all unacknowledged commands each time, not just the new ones.
  322. This means that on an internet connection, quite a few pmoves may be issued
  323. each frame.
  324. OPTIMIZE: don't re-simulate unless the newly arrived snapshot playerState_t
  325. differs from the predicted one. Would require saving all intermediate
  326. playerState_t during prediction.
  327. We detect prediction errors and allow them to be decayed off over several frames
  328. to ease the jerk.
  329. =================
  330. */
  331. void CG_PredictPlayerState( void ) {
  332. int cmdNum, current;
  333. playerState_t oldPlayerState;
  334. qboolean moved;
  335. usercmd_t oldestCmd;
  336. usercmd_t latestCmd;
  337. cg.hyperspace = qfalse; // will be set if touching a trigger_teleport
  338. // if this is the first frame we must guarantee
  339. // predictedPlayerState is valid even if there is some
  340. // other error condition
  341. if ( !cg.validPPS ) {
  342. cg.validPPS = qtrue;
  343. cg.predictedPlayerState = cg.snap->ps;
  344. }
  345. // demo playback just copies the moves
  346. if ( cg.demoPlayback || (cg.snap->ps.pm_flags & PMF_FOLLOW) ) {
  347. CG_InterpolatePlayerState( qfalse );
  348. return;
  349. }
  350. // non-predicting local movement will grab the latest angles
  351. if ( cg_nopredict.integer || cg_synchronousClients.integer ) {
  352. CG_InterpolatePlayerState( qtrue );
  353. return;
  354. }
  355. // prepare for pmove
  356. cg_pmove.ps = &cg.predictedPlayerState;
  357. cg_pmove.trace = CG_Trace;
  358. cg_pmove.pointcontents = CG_PointContents;
  359. if ( cg_pmove.ps->pm_type == PM_DEAD ) {
  360. cg_pmove.tracemask = MASK_PLAYERSOLID & ~CONTENTS_BODY;
  361. }
  362. else {
  363. cg_pmove.tracemask = MASK_PLAYERSOLID;
  364. }
  365. if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR ) {
  366. cg_pmove.tracemask &= ~CONTENTS_BODY; // spectators can fly through bodies
  367. }
  368. cg_pmove.noFootsteps = ( cgs.dmflags & DF_NO_FOOTSTEPS ) > 0;
  369. // save the state before the pmove so we can detect transitions
  370. oldPlayerState = cg.predictedPlayerState;
  371. current = trap_GetCurrentCmdNumber();
  372. // if we don't have the commands right after the snapshot, we
  373. // can't accurately predict a current position, so just freeze at
  374. // the last good position we had
  375. cmdNum = current - CMD_BACKUP + 1;
  376. trap_GetUserCmd( cmdNum, &oldestCmd );
  377. if ( oldestCmd.serverTime > cg.snap->ps.commandTime
  378. && oldestCmd.serverTime < cg.time ) { // special check for map_restart
  379. if ( cg_showmiss.integer ) {
  380. CG_Printf ("exceeded PACKET_BACKUP on commands\n");
  381. }
  382. return;
  383. }
  384. // get the latest command so we can know which commands are from previous map_restarts
  385. trap_GetUserCmd( current, &latestCmd );
  386. // get the most recent information we have, even if
  387. // the server time is beyond our current cg.time,
  388. // because predicted player positions are going to
  389. // be ahead of everything else anyway
  390. if ( cg.nextSnap && !cg.nextFrameTeleport && !cg.thisFrameTeleport ) {
  391. cg.predictedPlayerState = cg.nextSnap->ps;
  392. cg.physicsTime = cg.nextSnap->serverTime;
  393. } else {
  394. cg.predictedPlayerState = cg.snap->ps;
  395. cg.physicsTime = cg.snap->serverTime;
  396. }
  397. if ( pmove_msec.integer < 8 ) {
  398. trap_Cvar_Set("pmove_msec", "8");
  399. }
  400. else if (pmove_msec.integer > 33) {
  401. trap_Cvar_Set("pmove_msec", "33");
  402. }
  403. cg_pmove.pmove_fixed = pmove_fixed.integer;// | cg_pmove_fixed.integer;
  404. cg_pmove.pmove_msec = pmove_msec.integer;
  405. // run cmds
  406. moved = qfalse;
  407. for ( cmdNum = current - CMD_BACKUP + 1 ; cmdNum <= current ; cmdNum++ ) {
  408. // get the command
  409. trap_GetUserCmd( cmdNum, &cg_pmove.cmd );
  410. if ( cg_pmove.pmove_fixed ) {
  411. PM_UpdateViewAngles( cg_pmove.ps, &cg_pmove.cmd );
  412. }
  413. // don't do anything if the time is before the snapshot player time
  414. if ( cg_pmove.cmd.serverTime <= cg.predictedPlayerState.commandTime ) {
  415. continue;
  416. }
  417. // don't do anything if the command was from a previous map_restart
  418. if ( cg_pmove.cmd.serverTime > latestCmd.serverTime ) {
  419. continue;
  420. }
  421. // check for a prediction error from last frame
  422. // on a lan, this will often be the exact value
  423. // from the snapshot, but on a wan we will have
  424. // to predict several commands to get to the point
  425. // we want to compare
  426. if ( cg.predictedPlayerState.commandTime == oldPlayerState.commandTime ) {
  427. vec3_t delta;
  428. float len;
  429. if ( cg.thisFrameTeleport ) {
  430. // a teleport will not cause an error decay
  431. VectorClear( cg.predictedError );
  432. if ( cg_showmiss.integer ) {
  433. CG_Printf( "PredictionTeleport\n" );
  434. }
  435. cg.thisFrameTeleport = qfalse;
  436. } else {
  437. vec3_t adjusted;
  438. CG_AdjustPositionForMover( cg.predictedPlayerState.origin,
  439. cg.predictedPlayerState.groundEntityNum, cg.physicsTime, cg.oldTime, adjusted );
  440. if ( cg_showmiss.integer ) {
  441. if (!VectorCompare( oldPlayerState.origin, adjusted )) {
  442. CG_Printf("prediction error\n");
  443. }
  444. }
  445. VectorSubtract( oldPlayerState.origin, adjusted, delta );
  446. len = VectorLength( delta );
  447. if ( len > 0.1 ) {
  448. if ( cg_showmiss.integer ) {
  449. CG_Printf("Prediction miss: %f\n", len);
  450. }
  451. if ( cg_errorDecay.integer ) {
  452. int t;
  453. float f;
  454. t = cg.time - cg.predictedErrorTime;
  455. f = ( cg_errorDecay.value - t ) / cg_errorDecay.value;
  456. if ( f < 0 ) {
  457. f = 0;
  458. }
  459. if ( f > 0 && cg_showmiss.integer ) {
  460. CG_Printf("Double prediction decay: %f\n", f);
  461. }
  462. VectorScale( cg.predictedError, f, cg.predictedError );
  463. } else {
  464. VectorClear( cg.predictedError );
  465. }
  466. VectorAdd( delta, cg.predictedError, cg.predictedError );
  467. cg.predictedErrorTime = cg.oldTime;
  468. }
  469. }
  470. }
  471. // don't predict gauntlet firing, which is only supposed to happen
  472. // when it actually inflicts damage
  473. cg_pmove.gauntletHit = qfalse;
  474. if ( cg_pmove.pmove_fixed ) {
  475. cg_pmove.cmd.serverTime = ((cg_pmove.cmd.serverTime + pmove_msec.integer-1) / pmove_msec.integer) * pmove_msec.integer;
  476. }
  477. Pmove (&cg_pmove);
  478. moved = qtrue;
  479. // add push trigger movement effects
  480. CG_TouchTriggerPrediction();
  481. // check for predictable events that changed from previous predictions
  482. //CG_CheckChangedPredictableEvents(&cg.predictedPlayerState);
  483. }
  484. if ( cg_showmiss.integer > 1 ) {
  485. CG_Printf( "[%i : %i] ", cg_pmove.cmd.serverTime, cg.time );
  486. }
  487. if ( !moved ) {
  488. if ( cg_showmiss.integer ) {
  489. CG_Printf( "not moved\n" );
  490. }
  491. return;
  492. }
  493. // adjust for the movement of the groundentity
  494. CG_AdjustPositionForMover( cg.predictedPlayerState.origin,
  495. cg.predictedPlayerState.groundEntityNum,
  496. cg.physicsTime, cg.time, cg.predictedPlayerState.origin );
  497. if ( cg_showmiss.integer ) {
  498. if (cg.predictedPlayerState.eventSequence > oldPlayerState.eventSequence + MAX_PS_EVENTS) {
  499. CG_Printf("WARNING: dropped event\n");
  500. }
  501. }
  502. // fire events and other transition triggered things
  503. CG_TransitionPlayerState( &cg.predictedPlayerState, &oldPlayerState );
  504. if ( cg_showmiss.integer ) {
  505. if (cg.eventSequence > cg.predictedPlayerState.eventSequence) {
  506. CG_Printf("WARNING: double event\n");
  507. cg.eventSequence = cg.predictedPlayerState.eventSequence;
  508. }
  509. }
  510. }