Debian Codegraph @

code/game/g_unlagged.c

raw
1 /*
2 ===========================================================================
3 Copyright (C) 2006 Neil Toronto.
4
5 This file is part of the Unlagged source code.
6
7 Unlagged source code is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 Unlagged source code is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Unlagged 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 //Sago: For some reason the Niels version must use a different char set.
24 #include "g_local.h"
25
26 //#include "g_local.h"
27
28 /*
29 ============
30 G_ResetHistory
31
32 Clear out the given client's history (should be called when the teleport bit is flipped)
33 ============
34 */
35 void G_ResetHistory( gentity_t *ent ) {
36 int i, time;
37
38 // fill up the history with data (assume the current position)
39 ent->client->historyHead = NUM_CLIENT_HISTORY - 1;
40 for ( i = ent->client->historyHead, time = level.time; i >= 0; i--, time -= 50 ) {
41 VectorCopy( ent->r.mins, ent->client->history[i].mins );
42 VectorCopy( ent->r.maxs, ent->client->history[i].maxs );
43 VectorCopy( ent->r.currentOrigin, ent->client->history[i].currentOrigin );
44 ent->client->history[i].leveltime = time;
45 }
46 }
47
48
49 /*
50 ============
51 G_StoreHistory
52
53 Keep track of where the client's been
54 ============
55 */
56 void G_StoreHistory( gentity_t *ent ) {
57 int head, frametime;
58
59 frametime = level.time - level.previousTime;
60
61 ent->client->historyHead++;
62 if ( ent->client->historyHead >= NUM_CLIENT_HISTORY ) {
63 ent->client->historyHead = 0;
64 }
65
66 head = ent->client->historyHead;
67
68 // store all the collision-detection info and the time
69 VectorCopy( ent->r.mins, ent->client->history[head].mins );
70 VectorCopy( ent->r.maxs, ent->client->history[head].maxs );
71 VectorCopy( ent->s.pos.trBase, ent->client->history[head].currentOrigin );
72 SnapVector( ent->client->history[head].currentOrigin );
73 ent->client->history[head].leveltime = level.time;
74 }
75
76
77 /*
78 =============
79 TimeShiftLerp
80
81 Used below to interpolate between two previous vectors
82 Returns a vector "frac" times the distance between "start" and "end"
83 =============
84 */
85 static void TimeShiftLerp( float frac, vec3_t start, vec3_t end, vec3_t result ) {
86 // From CG_InterpolateEntityPosition in cg_ents.c:
87 /*
88 cent->lerpOrigin[0] = current[0] + f * ( next[0] - current[0] );
89 cent->lerpOrigin[1] = current[1] + f * ( next[1] - current[1] );
90 cent->lerpOrigin[2] = current[2] + f * ( next[2] - current[2] );
91 */
92 // Making these exactly the same should avoid floating-point error
93
94 result[0] = start[0] + frac * ( end[0] - start[0] );
95 result[1] = start[1] + frac * ( end[1] - start[1] );
96 result[2] = start[2] + frac * ( end[2] - start[2] );
97 }
98
99
100 /*
101 =================
102 G_TimeShiftClient
103
104 Move a client back to where he was at the specified "time"
105 =================
106 */
107 void G_TimeShiftClient( gentity_t *ent, int time, qboolean debug, gentity_t *debugger ) {
108 int j, k;
109 //char msg[2048];
110
111 // this will dump out the head index, and the time for all the stored positions
112 /*
113 if ( debug ) {
114 char str[MAX_STRING_CHARS];
115
116 Com_sprintf(str, sizeof(str), "print \"head: %d, %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n\"",
117 ent->client->historyHead,
118 ent->client->history[0].leveltime,
119 ent->client->history[1].leveltime,
120 ent->client->history[2].leveltime,
121 ent->client->history[3].leveltime,
122 ent->client->history[4].leveltime,
123 ent->client->history[5].leveltime,
124 ent->client->history[6].leveltime,
125 ent->client->history[7].leveltime,
126 ent->client->history[8].leveltime,
127 ent->client->history[9].leveltime,
128 ent->client->history[10].leveltime,
129 ent->client->history[11].leveltime,
130 ent->client->history[12].leveltime,
131 ent->client->history[13].leveltime,
132 ent->client->history[14].leveltime,
133 ent->client->history[15].leveltime,
134 ent->client->history[16].leveltime);
135
136 trap_SendServerCommand( debugger - g_entities, str );
137 }
138 */
139
140 // find two entries in the history whose times sandwich "time"
141 // assumes no two adjacent records have the same timestamp
142 j = k = ent->client->historyHead;
143 do {
144 if ( ent->client->history[j].leveltime <= time )
145 break;
146
147 k = j;
148 j--;
149 if ( j < 0 ) {
150 j = NUM_CLIENT_HISTORY - 1;
151 }
152 }
153 while ( j != ent->client->historyHead );
154
155 // if we got past the first iteration above, we've sandwiched (or wrapped)
156 if ( j != k ) {
157 // make sure it doesn't get re-saved
158 if ( ent->client->saved.leveltime != level.time ) {
159 // save the current origin and bounding box
160 VectorCopy( ent->r.mins, ent->client->saved.mins );
161 VectorCopy( ent->r.maxs, ent->client->saved.maxs );
162 VectorCopy( ent->r.currentOrigin, ent->client->saved.currentOrigin );
163 ent->client->saved.leveltime = level.time;
164 }
165
166 // if we haven't wrapped back to the head, we've sandwiched, so
167 // we shift the client's position back to where he was at "time"
168 if ( j != ent->client->historyHead ) {
169 float frac = (float)(time - ent->client->history[j].leveltime) /
170 (float)(ent->client->history[k].leveltime - ent->client->history[j].leveltime);
171
172 // interpolate between the two origins to give position at time index "time"
173 TimeShiftLerp( frac,
174 ent->client->history[j].currentOrigin, ent->client->history[k].currentOrigin,
175 ent->r.currentOrigin );
176
177 // lerp these too, just for fun (and ducking)
178 TimeShiftLerp( frac,
179 ent->client->history[j].mins, ent->client->history[k].mins,
180 ent->r.mins );
181
182 TimeShiftLerp( frac,
183 ent->client->history[j].maxs, ent->client->history[k].maxs,
184 ent->r.maxs );
185
186 /*if ( debug && debugger != NULL ) {
187 // print some debugging stuff exactly like what the client does
188
189 // it starts with "Rec:" to let you know it backward-reconciled
190 Com_sprintf( msg, sizeof(msg),
191 "print \"^1Rec: time: %d, j: %d, k: %d, origin: %0.2f %0.2f %0.2f\n"
192 "^2frac: %0.4f, origin1: %0.2f %0.2f %0.2f, origin2: %0.2f %0.2f %0.2f\n"
193 "^7level.time: %d, est time: %d, level.time delta: %d, est real ping: %d\n\"",
194 time, ent->client->history[j].leveltime, ent->client->history[k].leveltime,
195 ent->r.currentOrigin[0], ent->r.currentOrigin[1], ent->r.currentOrigin[2],
196 frac,
197 ent->client->history[j].currentOrigin[0],
198 ent->client->history[j].currentOrigin[1],
199 ent->client->history[j].currentOrigin[2],
200 ent->client->history[k].currentOrigin[0],
201 ent->client->history[k].currentOrigin[1],
202 ent->client->history[k].currentOrigin[2],
203 level.time, level.time + debugger->client->frameOffset,
204 level.time - time, level.time + debugger->client->frameOffset - time);
205
206 trap_SendServerCommand( debugger - g_entities, msg );
207 }*/
208
209 // this will recalculate absmin and absmax
210 trap_LinkEntity( ent );
211 } else {
212 // we wrapped, so grab the earliest
213 VectorCopy( ent->client->history[k].currentOrigin, ent->r.currentOrigin );
214 VectorCopy( ent->client->history[k].mins, ent->r.mins );
215 VectorCopy( ent->client->history[k].maxs, ent->r.maxs );
216
217 // this will recalculate absmin and absmax
218 trap_LinkEntity( ent );
219 }
220 }
221 else {
222 // this only happens when the client is using a negative timenudge, because that
223 // number is added to the command time
224
225 // print some debugging stuff exactly like what the client does
226
227 // it starts with "No rec:" to let you know it didn't backward-reconcile
228 //Sago: This code looks wierd
229
230 }
231 }
232
233
234 /*
235 =====================
236 G_TimeShiftAllClients
237
238 Move ALL clients back to where they were at the specified "time",
239 except for "skip"
240 =====================
241 */
242 void G_TimeShiftAllClients( int time, gentity_t *skip ) {
243 int i;
244 gentity_t *ent;
245 qboolean debug = ( skip != NULL && skip->client &&
246 /*skip->client->pers.debugDelag && */ skip->s.weapon == WP_RAILGUN );
247
248 // for every client
249 ent = &g_entities[0];
250 for ( i = 0; i < MAX_CLIENTS; i++, ent++ ) {
251 if ( ent->client && ent->inuse && ent->client->sess.sessionTeam < TEAM_SPECTATOR && ent != skip ) {
252 G_TimeShiftClient( ent, time, debug, skip );
253 }
254 }
255 }
256
257
258 /*
259 ================
260 G_DoTimeShiftFor
261
262 Decide what time to shift everyone back to, and do it
263 ================
264 */
265 void G_DoTimeShiftFor( gentity_t *ent ) {
266 int wpflags[WP_NUM_WEAPONS] = { 0, 0, 2, 4, 0, 0, 8, 16, 0, 0, 0, 32, 0, 64 };
267
268 int wpflag = wpflags[ent->client->ps.weapon];
269 int time;
270
271 // don't time shift for mistakes or bots
272 if ( !ent->inuse || !ent->client || (ent->r.svFlags & SVF_BOT) ) {
273 return;
274 }
275
276 // if it's enabled server-side and the client wants it or wants it for this weapon
277 if ( g_delagHitscan.integer && ( ent->client->pers.delag & 1 || ent->client->pers.delag & wpflag ) ) {
278 // do the full lag compensation, except what the client nudges
279 time = ent->client->attackTime + ent->client->pers.cmdTimeNudge;
280 //Give the lightning gun some handicap (lag was part of weapon balance in VQ3)
281 if(ent->client->ps.weapon == WP_LIGHTNING && g_lagLightning.integer)
282 time+=50;
283 }
284 else {
285 // do just 50ms
286 time = level.previousTime + ent->client->frameOffset;
287 }
288
289 G_TimeShiftAllClients( time, ent );
290 }
291
292
293 /*
294 ===================
295 G_UnTimeShiftClient
296
297 Move a client back to where he was before the time shift
298 ===================
299 */
300 void G_UnTimeShiftClient( gentity_t *ent ) {
301 // if it was saved
302 if ( ent->client->saved.leveltime == level.time ) {
303 // move it back
304 VectorCopy( ent->client->saved.mins, ent->r.mins );
305 VectorCopy( ent->client->saved.maxs, ent->r.maxs );
306 VectorCopy( ent->client->saved.currentOrigin, ent->r.currentOrigin );
307 ent->client->saved.leveltime = 0;
308
309 // this will recalculate absmin and absmax
310 trap_LinkEntity( ent );
311 }
312 }
313
314
315 /*
316 =======================
317 G_UnTimeShiftAllClients
318
319 Move ALL the clients back to where they were before the time shift,
320 except for "skip"
321 =======================
322 */
323 void G_UnTimeShiftAllClients( gentity_t *skip ) {
324 int i;
325 gentity_t *ent;
326
327 ent = &g_entities[0];
328 for ( i = 0; i < MAX_CLIENTS; i++, ent++) {
329 if ( ent->client && ent->inuse && ent->client->sess.sessionTeam < TEAM_SPECTATOR && ent != skip ) {
330 G_UnTimeShiftClient( ent );
331 }
332 }
333 }
334
335
336 /*
337 ==================
338 G_UndoTimeShiftFor
339
340 Put everyone except for this client back where they were
341 ==================
342 */
343 void G_UndoTimeShiftFor( gentity_t *ent ) {
344
345 // don't un-time shift for mistakes or bots
346 if ( !ent->inuse || !ent->client || (ent->r.svFlags & SVF_BOT) ) {
347 return;
348 }
349
350 G_UnTimeShiftAllClients( ent );
351 }
352
353
354 /*
355 ===========================
356 G_PredictPlayerClipVelocity
357
358 Slide on the impacting surface
359 ===========================
360 */
361
362 #define OVERCLIP 1.001f
363
364 void G_PredictPlayerClipVelocity( vec3_t in, vec3_t normal, vec3_t out ) {
365 float backoff;
366
367 // find the magnitude of the vector "in" along "normal"
368 backoff = DotProduct (in, normal);
369
370 // tilt the plane a bit to avoid floating-point error issues
371 if ( backoff < 0 ) {
372 backoff *= OVERCLIP;
373 } else {
374 backoff /= OVERCLIP;
375 }
376
377 // slide along
378 VectorMA( in, -backoff, normal, out );
379 }
380
381
382 /*
383 ========================
384 G_PredictPlayerSlideMove
385
386 Advance the given entity frametime seconds, sliding as appropriate
387 ========================
388 */
389 #define MAX_CLIP_PLANES 5
390
391 qboolean G_PredictPlayerSlideMove( gentity_t *ent, float frametime ) {
392 int bumpcount, numbumps;
393 vec3_t dir;
394 float d;
395 int numplanes;
396 vec3_t planes[MAX_CLIP_PLANES];
397 vec3_t primal_velocity, velocity, origin;
398 vec3_t clipVelocity;
399 int i, j, k;
400 trace_t trace;
401 vec3_t end;
402 float time_left;
403 float into;
404 vec3_t endVelocity;
405 vec3_t endClipVelocity;
406 // vec3_t worldUp = { 0.0f, 0.0f, 1.0f };
407
408 numbumps = 4;
409
410 VectorCopy( ent->s.pos.trDelta, primal_velocity );
411 VectorCopy( primal_velocity, velocity );
412 VectorCopy( ent->s.pos.trBase, origin );
413
414 VectorCopy( velocity, endVelocity );
415
416 time_left = frametime;
417
418 numplanes = 0;
419
420 for ( bumpcount = 0; bumpcount < numbumps; bumpcount++ ) {
421
422 // calculate position we are trying to move to
423 VectorMA( origin, time_left, velocity, end );
424
425 // see if we can make it there
426 trap_Trace( &trace, origin, ent->r.mins, ent->r.maxs, end, ent->s.number, ent->clipmask );
427
428 if (trace.allsolid) {
429 // entity is completely trapped in another solid
430 VectorClear( velocity );
431 VectorCopy( origin, ent->s.pos.trBase );
432 return qtrue;
433 }
434
435 if (trace.fraction > 0) {
436 // actually covered some distance
437 VectorCopy( trace.endpos, origin );
438 }
439
440 if (trace.fraction == 1) {
441 break; // moved the entire distance
442 }
443
444 time_left -= time_left * trace.fraction;
445
446 if ( numplanes >= MAX_CLIP_PLANES ) {
447 // this shouldn't really happen
448 VectorClear( velocity );
449 VectorCopy( origin, ent->s.pos.trBase );
450 return qtrue;
451 }
452
453 //
454 // if this is the same plane we hit before, nudge velocity
455 // out along it, which fixes some epsilon issues with
456 // non-axial planes
457 //
458 for ( i = 0; i < numplanes; i++ ) {
459 if ( DotProduct( trace.plane.normal, planes[i] ) > 0.99 ) {
460 VectorAdd( trace.plane.normal, velocity, velocity );
461 break;
462 }
463 }
464
465 if ( i < numplanes ) {
466 continue;
467 }
468
469 VectorCopy( trace.plane.normal, planes[numplanes] );
470 numplanes++;
471
472 //
473 // modify velocity so it parallels all of the clip planes
474 //
475
476 // find a plane that it enters
477 for ( i = 0; i < numplanes; i++ ) {
478 into = DotProduct( velocity, planes[i] );
479 if ( into >= 0.1 ) {
480 continue; // move doesn't interact with the plane
481 }
482
483 // slide along the plane
484 G_PredictPlayerClipVelocity( velocity, planes[i], clipVelocity );
485
486 // slide along the plane
487 G_PredictPlayerClipVelocity( endVelocity, planes[i], endClipVelocity );
488
489 // see if there is a second plane that the new move enters
490 for ( j = 0; j < numplanes; j++ ) {
491 if ( j == i ) {
492 continue;
493 }
494
495 if ( DotProduct( clipVelocity, planes[j] ) >= 0.1 ) {
496 continue; // move doesn't interact with the plane
497 }
498
499 // try clipping the move to the plane
500 G_PredictPlayerClipVelocity( clipVelocity, planes[j], clipVelocity );
501 G_PredictPlayerClipVelocity( endClipVelocity, planes[j], endClipVelocity );
502
503 // see if it goes back into the first clip plane
504 if ( DotProduct( clipVelocity, planes[i] ) >= 0 ) {
505 continue;
506 }
507
508 // slide the original velocity along the crease
509 CrossProduct( planes[i], planes[j], dir );
510 VectorNormalize( dir );
511 d = DotProduct( dir, velocity );
512 VectorScale( dir, d, clipVelocity );
513
514 CrossProduct( planes[i], planes[j], dir );
515 VectorNormalize( dir );
516 d = DotProduct( dir, endVelocity );
517 VectorScale( dir, d, endClipVelocity );
518
519 // see if there is a third plane the the new move enters
520 for ( k = 0; k < numplanes; k++ ) {
521 if ( k == i || k == j ) {
522 continue;
523 }
524
525 if ( DotProduct( clipVelocity, planes[k] ) >= 0.1 ) {
526 continue; // move doesn't interact with the plane
527 }
528
529 // stop dead at a tripple plane interaction
530 VectorClear( velocity );
531 VectorCopy( origin, ent->s.pos.trBase );
532 return qtrue;
533 }
534 }
535
536 // if we have fixed all interactions, try another move
537 VectorCopy( clipVelocity, velocity );
538 VectorCopy( endClipVelocity, endVelocity );
539 break;
540 }
541 }
542
543 VectorCopy( endVelocity, velocity );
544 VectorCopy( origin, ent->s.pos.trBase );
545
546 return (bumpcount != 0);
547 }
548
549
550 /*
551 ============================
552 G_PredictPlayerStepSlideMove
553
554 Advance the given entity frametime seconds, stepping and sliding as appropriate
555 ============================
556 */
557 #define STEPSIZE 18
558
559 void G_PredictPlayerStepSlideMove( gentity_t *ent, float frametime ) {
560 vec3_t start_o, start_v, down_o, down_v;
561 vec3_t down, up;
562 trace_t trace;
563 float stepSize;
564
565 VectorCopy (ent->s.pos.trBase, start_o);
566 VectorCopy (ent->s.pos.trDelta, start_v);
567
568 if ( !G_PredictPlayerSlideMove( ent, frametime ) ) {
569 // not clipped, so forget stepping
570 return;
571 }
572
573 VectorCopy( ent->s.pos.trBase, down_o);
574 VectorCopy( ent->s.pos.trDelta, down_v);
575
576 VectorCopy (start_o, up);
577 up[2] += STEPSIZE;
578
579 // test the player position if they were a stepheight higher
580 trap_Trace( &trace, start_o, ent->r.mins, ent->r.maxs, up, ent->s.number, ent->clipmask );
581 if ( trace.allsolid ) {
582 return; // can't step up
583 }
584
585 stepSize = trace.endpos[2] - start_o[2];
586
587 // try slidemove from this position
588 VectorCopy( trace.endpos, ent->s.pos.trBase );
589 VectorCopy( start_v, ent->s.pos.trDelta );
590
591 G_PredictPlayerSlideMove( ent, frametime );
592
593 // push down the final amount
594 VectorCopy( ent->s.pos.trBase, down );
595 down[2] -= stepSize;
596 trap_Trace( &trace, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, down, ent->s.number, ent->clipmask );
597 if ( !trace.allsolid ) {
598 VectorCopy( trace.endpos, ent->s.pos.trBase );
599 }
600 if ( trace.fraction < 1.0 ) {
601 G_PredictPlayerClipVelocity( ent->s.pos.trDelta, trace.plane.normal, ent->s.pos.trDelta );
602 }
603 }
604
605
606 /*
607 ===================
608 G_PredictPlayerMove
609
610 Advance the given entity frametime seconds, stepping and sliding as appropriate
611
612 This is the entry point to the server-side-only prediction code
613 ===================
614 */
615 void G_PredictPlayerMove( gentity_t *ent, float frametime ) {
616 G_PredictPlayerStepSlideMove( ent, frametime );
617 }