2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sv_user.c -- server code for moving users
27 extern cvar_t sv_autodemo_perclient;
35 void SV_SetIdealPitch (void)
37 prvm_prog_t *prog = SVVM_prog;
38 float angleval, sinval, cosval, step, dir;
45 if (!((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND))
48 angleval = PRVM_serveredictvector(host_client->edict, angles)[YAW] * M_PI*2 / 360;
49 sinval = sin(angleval);
50 cosval = cos(angleval);
52 for (i=0 ; i<MAX_FORWARD ; i++)
54 top[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + cosval*(i+3)*12;
55 top[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + sinval*(i+3)*12;
56 top[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, view_ofs)[2];
60 bottom[2] = top[2] - 160;
62 tr = SV_TraceLine(top, bottom, MOVE_NOMONSTERS, host_client->edict, SUPERCONTENTS_SOLID, 0, 0, collision_extendmovelength.value);
63 // if looking at a wall, leave ideal the way is was
71 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
79 if (step > -ON_EPSILON && step < ON_EPSILON)
83 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
92 PRVM_serveredictfloat(host_client->edict, idealpitch) = 0;
98 PRVM_serveredictfloat(host_client->edict, idealpitch) = -dir * sv_idealpitchscale.value;
101 static vec3_t wishdir, forward, right, up;
102 static float wishspeed;
104 static qboolean onground;
112 static void SV_UserFriction (void)
114 prvm_prog_t *prog = SVVM_prog;
115 float speed, newspeed, control, friction;
119 speed = sqrt(PRVM_serveredictvector(host_client->edict, velocity)[0]*PRVM_serveredictvector(host_client->edict, velocity)[0]+PRVM_serveredictvector(host_client->edict, velocity)[1]*PRVM_serveredictvector(host_client->edict, velocity)[1]);
123 // if the leading edge is over a dropoff, increase friction
124 start[0] = stop[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + PRVM_serveredictvector(host_client->edict, velocity)[0]/speed*16;
125 start[1] = stop[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + PRVM_serveredictvector(host_client->edict, velocity)[1]/speed*16;
126 start[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, mins)[2];
127 stop[2] = start[2] - 34;
129 trace = SV_TraceLine(start, stop, MOVE_NOMONSTERS, host_client->edict, SV_GenericHitSuperContentsMask(host_client->edict), 0, 0, collision_extendmovelength.value);
131 if (trace.fraction == 1.0)
132 friction = sv_friction.value*sv_edgefriction.value;
134 friction = sv_friction.value;
137 control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
138 newspeed = speed - sv.frametime*control*friction;
145 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), newspeed, PRVM_serveredictvector(host_client->edict, velocity));
153 static void SV_Accelerate (void)
155 prvm_prog_t *prog = SVVM_prog;
157 float addspeed, accelspeed, currentspeed;
159 currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishdir);
160 addspeed = wishspeed - currentspeed;
163 accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
164 if (accelspeed > addspeed)
165 accelspeed = addspeed;
167 for (i=0 ; i<3 ; i++)
168 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishdir[i];
171 extern cvar_t sv_gameplayfix_q2airaccelerate;
172 static void SV_AirAccelerate (vec3_t wishveloc)
174 prvm_prog_t *prog = SVVM_prog;
176 float addspeed, wishspd, accelspeed, currentspeed;
178 wishspd = VectorNormalizeLength (wishveloc);
179 if (wishspd > sv_maxairspeed.value)
180 wishspd = sv_maxairspeed.value;
181 currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishveloc);
182 addspeed = wishspd - currentspeed;
185 accelspeed = (sv_airaccelerate.value < 0 ? sv_accelerate.value : sv_airaccelerate.value)*(sv_gameplayfix_q2airaccelerate.integer ? wishspd : wishspeed) * sv.frametime;
186 if (accelspeed > addspeed)
187 accelspeed = addspeed;
189 for (i=0 ; i<3 ; i++)
190 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishveloc[i];
194 static void DropPunchAngle (void)
196 prvm_prog_t *prog = SVVM_prog;
198 vec3_t punchangle, punchvector;
200 VectorCopy(PRVM_serveredictvector(host_client->edict, punchangle), punchangle);
201 VectorCopy(PRVM_serveredictvector(host_client->edict, punchvector), punchvector);
203 len = VectorNormalizeLength(punchangle);
206 len -= 10*sv.frametime;
209 VectorScale(punchangle, len, punchangle);
212 len = VectorNormalizeLength(punchvector);
215 len -= 20*sv.frametime;
218 VectorScale(punchvector, len, punchvector);
221 VectorCopy(punchangle, PRVM_serveredictvector(host_client->edict, punchangle));
222 VectorCopy(punchvector, PRVM_serveredictvector(host_client->edict, punchvector));
231 static void SV_WaterMove (void)
233 prvm_prog_t *prog = SVVM_prog;
235 vec3_t wishvel, v_angle;
236 vec_t speed, newspeed, fwishspeed, addspeed, accelspeed, temp;
239 VectorCopy(PRVM_serveredictvector(host_client->edict, v_angle), v_angle);
240 AngleVectors(v_angle, forward, right, up);
242 for (i=0 ; i<3 ; i++)
243 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
245 if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
246 wishvel[2] -= 60; // drift towards bottom
248 wishvel[2] += cmd.upmove;
250 fwishspeed = VectorLength(wishvel);
251 if (fwishspeed > sv_maxspeed.value)
253 temp = sv_maxspeed.value/fwishspeed;
254 VectorScale (wishvel, temp, wishvel);
255 fwishspeed = sv_maxspeed.value;
260 speed = VectorLength(PRVM_serveredictvector(host_client->edict, velocity));
263 newspeed = speed - sv.frametime * speed * (sv_waterfriction.value < 0 ? sv_friction.value : sv_waterfriction.value);
266 temp = newspeed/speed;
267 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), temp, PRVM_serveredictvector(host_client->edict, velocity));
272 // water acceleration
276 addspeed = fwishspeed - newspeed;
280 VectorNormalize (wishvel);
281 accelspeed = (sv_wateraccelerate.value < 0 ? sv_accelerate.value : sv_wateraccelerate.value) * fwishspeed * sv.frametime;
282 if (accelspeed > addspeed)
283 accelspeed = addspeed;
285 for (i=0 ; i<3 ; i++)
286 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed * wishvel[i];
289 static void SV_WaterJump (void)
291 prvm_prog_t *prog = SVVM_prog;
292 if (sv.time > PRVM_serveredictfloat(host_client->edict, teleport_time) || !PRVM_serveredictfloat(host_client->edict, waterlevel))
294 PRVM_serveredictfloat(host_client->edict, flags) = (int)PRVM_serveredictfloat(host_client->edict, flags) & ~FL_WATERJUMP;
295 PRVM_serveredictfloat(host_client->edict, teleport_time) = 0;
297 PRVM_serveredictvector(host_client->edict, velocity)[0] = PRVM_serveredictvector(host_client->edict, movedir)[0];
298 PRVM_serveredictvector(host_client->edict, velocity)[1] = PRVM_serveredictvector(host_client->edict, movedir)[1];
308 static void SV_AirMove (void)
310 prvm_prog_t *prog = SVVM_prog;
313 float fmove, smove, temp;
315 // LadyHavoc: correct quake movement speed bug when looking up/down
316 wishvel[0] = wishvel[2] = 0;
317 wishvel[1] = PRVM_serveredictvector(host_client->edict, angles)[1];
318 AngleVectors (wishvel, forward, right, up);
320 fmove = cmd.forwardmove;
321 smove = cmd.sidemove;
323 // hack to not let you back into teleporter
324 if (sv.time < PRVM_serveredictfloat(host_client->edict, teleport_time) && fmove < 0)
327 for (i=0 ; i<3 ; i++)
328 wishvel[i] = forward[i]*fmove + right[i]*smove;
330 if ((int)PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_WALK)
331 wishvel[2] += cmd.upmove;
333 VectorCopy (wishvel, wishdir);
334 wishspeed = VectorNormalizeLength(wishdir);
335 if (wishspeed > sv_maxspeed.value)
337 temp = sv_maxspeed.value/wishspeed;
338 VectorScale (wishvel, temp, wishvel);
339 wishspeed = sv_maxspeed.value;
342 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP)
345 VectorCopy (wishvel, PRVM_serveredictvector(host_client->edict, velocity));
354 // not on ground, so little effect on velocity
355 SV_AirAccelerate (wishvel);
363 the move fields specify an intended velocity in pix/sec
364 the angle fields specify an exact angular motion in degrees
367 void SV_ClientThink (void)
369 prvm_prog_t *prog = SVVM_prog;
370 vec3_t v_angle, angles, velocity;
372 //Con_Printf("clientthink for %ims\n", (int) (sv.frametime * 1000));
374 SV_ApplyClientMove();
375 // make sure the velocity is sane (not a NaN)
376 SV_CheckVelocity(host_client->edict);
378 // LadyHavoc: QuakeC replacement for SV_ClientThink (player movement)
379 if (PRVM_serverfunction(SV_PlayerPhysics) && sv_playerphysicsqc.integer)
381 PRVM_serverglobalfloat(time) = sv.time;
382 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
383 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PlayerPhysics), "QC function SV_PlayerPhysics is missing");
384 SV_CheckVelocity(host_client->edict);
388 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NONE)
391 onground = ((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND) != 0;
395 // if dead, behave differently
396 if (PRVM_serveredictfloat(host_client->edict, health) <= 0)
399 cmd = host_client->cmd;
402 // show 1/3 the pitch angle and all the roll angle
403 VectorAdd (PRVM_serveredictvector(host_client->edict, v_angle), PRVM_serveredictvector(host_client->edict, punchangle), v_angle);
404 VectorCopy(PRVM_serveredictvector(host_client->edict, angles), angles);
405 VectorCopy(PRVM_serveredictvector(host_client->edict, velocity), velocity);
406 PRVM_serveredictvector(host_client->edict, angles)[ROLL] = V_CalcRoll (angles, velocity)*4;
407 if (!PRVM_serveredictfloat(host_client->edict, fixangle))
409 PRVM_serveredictvector(host_client->edict, angles)[PITCH] = -v_angle[PITCH]/3;
410 PRVM_serveredictvector(host_client->edict, angles)[YAW] = v_angle[YAW];
413 if ( (int)PRVM_serveredictfloat(host_client->edict, flags) & FL_WATERJUMP )
416 SV_CheckVelocity(host_client->edict);
421 // Player is (somehow) outside of the map, or flying, or noclipping
422 if (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP && (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict)))
423 //if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP || PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict))
431 if ((PRVM_serveredictfloat(host_client->edict, waterlevel) >= 2) && (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP))
434 SV_CheckVelocity(host_client->edict);
439 SV_CheckVelocity(host_client->edict);
447 int sv_numreadmoves = 0;
448 usercmd_t sv_readmoves[CL_MAX_USERCMDS];
449 static void SV_ReadClientMove (void)
451 prvm_prog_t *prog = SVVM_prog;
454 usercmd_t *move = &newmove;
456 memset(move, 0, sizeof(*move));
458 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
461 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_NEHAHRABJP && sv.protocol != PROTOCOL_NEHAHRABJP2 && sv.protocol != PROTOCOL_NEHAHRABJP3 && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5 && sv.protocol != PROTOCOL_DARKPLACES6)
462 move->sequence = MSG_ReadLong(&sv_message);
463 move->time = move->clienttime = MSG_ReadFloat(&sv_message);
464 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
465 move->receivetime = (float)sv.time;
468 Con_Printf("%s move%i #%u %ims (%ims) %i %i '%i %i %i' '%i %i %i'\n", move->time > move->receivetime ? "^3read future" : "^4read normal", sv_numreadmoves + 1, move->sequence, (int)floor((move->time - host_client->cmd.time) * 1000.0 + 0.5), (int)floor(move->time * 1000.0 + 0.5), move->impulse, move->buttons, (int)move->viewangles[0], (int)move->viewangles[1], (int)move->viewangles[2], (int)move->forwardmove, (int)move->sidemove, (int)move->upmove);
470 // limit reported time to current time
471 // (incase the client is trying to cheat)
472 move->time = min(move->time, move->receivetime + sv.frametime);
474 // read current angles
475 for (i = 0;i < 3;i++)
477 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3)
478 move->viewangles[i] = MSG_ReadAngle8i(&sv_message);
479 else if (sv.protocol == PROTOCOL_DARKPLACES1)
480 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
481 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
482 move->viewangles[i] = MSG_ReadAngle32f(&sv_message);
484 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
486 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
489 move->forwardmove = MSG_ReadCoord16i(&sv_message);
490 move->sidemove = MSG_ReadCoord16i(&sv_message);
491 move->upmove = MSG_ReadCoord16i(&sv_message);
492 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
495 // be sure to bitwise OR them into the move->buttons because we want to
496 // accumulate button presses from multiple packets per actual move
497 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3 || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
498 move->buttons = MSG_ReadByte(&sv_message);
500 move->buttons = MSG_ReadLong(&sv_message);
501 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
504 move->impulse = MSG_ReadByte(&sv_message);
505 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
507 // PRYDON_CLIENTCURSOR
508 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_NEHAHRABJP && sv.protocol != PROTOCOL_NEHAHRABJP2 && sv.protocol != PROTOCOL_NEHAHRABJP3 && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5)
511 move->cursor_screen[0] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
512 move->cursor_screen[1] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
513 move->cursor_start[0] = MSG_ReadFloat(&sv_message);
514 move->cursor_start[1] = MSG_ReadFloat(&sv_message);
515 move->cursor_start[2] = MSG_ReadFloat(&sv_message);
516 move->cursor_impact[0] = MSG_ReadFloat(&sv_message);
517 move->cursor_impact[1] = MSG_ReadFloat(&sv_message);
518 move->cursor_impact[2] = MSG_ReadFloat(&sv_message);
519 move->cursor_entitynumber = (unsigned short)MSG_ReadShort(&sv_message);
520 if (move->cursor_entitynumber >= prog->max_edicts)
522 Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
523 move->cursor_entitynumber = 0;
525 // as requested by FrikaC, cursor_trace_ent is reset to world if the
526 // entity is free at time of receipt
527 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
528 move->cursor_entitynumber = 0;
529 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
532 // if the previous move has not been applied yet, we need to accumulate
533 // the impulse/buttons from it
534 if (!host_client->cmd.applied)
537 move->impulse = host_client->cmd.impulse;
538 move->buttons |= host_client->cmd.buttons;
541 // now store this move for later execution
542 // (we have to buffer the moves because of old ones being repeated)
543 if (sv_numreadmoves < CL_MAX_USERCMDS)
544 sv_readmoves[sv_numreadmoves++] = *move;
546 // movement packet loss tracking
549 if(move->sequence > host_client->movement_highestsequence_seen)
551 if(host_client->movement_highestsequence_seen)
553 // mark moves in between as lost
554 unsigned int delta = move->sequence - host_client->movement_highestsequence_seen - 1;
555 if(delta < NETGRAPH_PACKETS)
558 for(u = 0; u < delta; ++u)
559 host_client->movement_count[(host_client->movement_highestsequence_seen + 1 + u) % NETGRAPH_PACKETS] = -1;
562 memset(host_client->movement_count, -1, sizeof(host_client->movement_count));
564 // mark THIS move as seen for the first time
565 host_client->movement_count[move->sequence % NETGRAPH_PACKETS] = 1;
566 // update highest sequence seen
567 host_client->movement_highestsequence_seen = move->sequence;
570 if(host_client->movement_count[move->sequence % NETGRAPH_PACKETS] >= 0)
571 ++host_client->movement_count[move->sequence % NETGRAPH_PACKETS];
575 host_client->movement_highestsequence_seen = 0;
576 memset(host_client->movement_count, 0, sizeof(host_client->movement_count));
580 static void SV_ExecuteClientMoves(void)
582 prvm_prog_t *prog = SVVM_prog;
586 double oldframetime2;
587 #ifdef NUM_PING_TIMES
590 if (sv_numreadmoves < 1)
592 // only start accepting input once the player is spawned
593 if (!host_client->begun)
596 Con_Printf("SV_ExecuteClientMoves: read %i moves at sv.time %f\n", sv_numreadmoves, (float)sv.time);
598 // disable clientside movement prediction in some cases
599 if (ceil(max(sv_readmoves[sv_numreadmoves-1].receivetime - sv_readmoves[sv_numreadmoves-1].time, 0) * 1000.0) < sv_clmovement_minping.integer)
600 host_client->clmovement_disabletimeout = host.realtime + sv_clmovement_minping_disabletime.value / 1000.0;
601 // several conditions govern whether clientside movement prediction is allowed
602 if (sv_readmoves[sv_numreadmoves-1].sequence && sv_clmovement_enable.integer && sv_clmovement_inputtimeout.value > 0 && host_client->clmovement_disabletimeout <= host.realtime && (PRVM_serveredictfloat(host_client->edict, disableclientprediction) == -1 || (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_WALK && (!PRVM_serveredictfloat(host_client->edict, disableclientprediction)))))
604 // process the moves in order and ignore old ones
605 // but always trust the latest move
606 // (this deals with bogus initial move sequences after level change,
607 // where the client will eventually catch up with the level change
608 // and reset its move sequence)
609 for (moveindex = 0;moveindex < sv_numreadmoves;moveindex++)
611 usercmd_t *move = sv_readmoves + moveindex;
612 if (host_client->movesequence < move->sequence || moveindex == sv_numreadmoves - 1)
615 Con_Printf("%smove #%u %ims (%ims) %i %i '%i %i %i' '%i %i %i'\n", (move->time - host_client->cmd.time) > sv.frametime * 1.01 ? "^1" : "^2", move->sequence, (int)floor((move->time - host_client->cmd.time) * 1000.0 + 0.5), (int)floor(move->time * 1000.0 + 0.5), move->impulse, move->buttons, (int)move->viewangles[0], (int)move->viewangles[1], (int)move->viewangles[2], (int)move->forwardmove, (int)move->sidemove, (int)move->upmove);
617 // this is a new move
618 move->time = bound(sv.time - 1, move->time, sv.time); // prevent slowhack/speedhack combos
619 move->time = max(move->time, host_client->cmd.time); // prevent backstepping of time
620 moveframetime = bound(0, move->time - host_client->cmd.time, min(0.1, sv_clmovement_inputtimeout.value));
622 // discard (treat like lost) moves with too low distance from
623 // the previous one to prevent hacks using float inaccuracy
624 // clients will see this as packet loss in the netgraph
625 // this should also apply if a move cannot get
626 // executed because it came too late and
627 // already was performed serverside
628 if(moveframetime < 0.0005)
630 // count the move as LOST if we don't
631 // execute it but it has higher
633 if(host_client->movesequence)
634 if(move->sequence > host_client->movesequence)
635 host_client->movement_count[(move->sequence) % NETGRAPH_PACKETS] = -1;
639 //Con_Printf("movesequence = %i (%i lost), moveframetime = %f\n", move->sequence, move->sequence ? move->sequence - host_client->movesequence - 1 : 0, moveframetime);
640 host_client->cmd = *move;
641 host_client->movesequence = move->sequence;
643 // if using prediction, we need to perform moves when packets are
644 // received, even if multiple occur in one frame
645 // (they can't go beyond the current time so there is no cheat issue
646 // with this approach, and if they don't send input for a while they
647 // start moving anyway, so the longest 'lagaport' possible is
648 // determined by the sv_clmovement_inputtimeout cvar)
649 if (moveframetime <= 0)
651 oldframetime = PRVM_serverglobalfloat(frametime);
652 oldframetime2 = sv.frametime;
653 // update ping time for qc to see while executing this move
654 host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
655 // the server and qc frametime values must be changed temporarily
656 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime;
657 // if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
658 if (sv.frametime > 0.05)
660 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime * 0.5f;
661 SV_Physics_ClientMove();
663 SV_Physics_ClientMove();
664 sv.frametime = oldframetime2;
665 PRVM_serverglobalfloat(frametime) = oldframetime;
666 host_client->clmovement_inputtimeout = sv_clmovement_inputtimeout.value;
672 // try to gather button bits from old moves, but only if their time is
673 // advancing (ones with the same timestamp can't be trusted)
674 for (moveindex = 0;moveindex < sv_numreadmoves-1;moveindex++)
676 usercmd_t *move = sv_readmoves + moveindex;
677 if (host_client->cmd.time < move->time)
679 sv_readmoves[sv_numreadmoves-1].buttons |= move->buttons;
681 sv_readmoves[sv_numreadmoves-1].impulse = move->impulse;
684 // now copy the new move
685 host_client->cmd = sv_readmoves[sv_numreadmoves-1];
686 host_client->cmd.time = max(host_client->cmd.time, sv.time);
687 // physics will run up to sv.time, so allow no predicted moves
688 // before that otherwise, there is a speedhack by turning
689 // prediction on and off repeatedly on client side because the
690 // engine would run BOTH client and server physics for the same
692 host_client->movesequence = 0;
693 // make sure that normal physics takes over immediately
694 host_client->clmovement_inputtimeout = 0;
697 // calculate average ping time
698 host_client->ping = host_client->cmd.receivetime - host_client->cmd.clienttime;
699 #ifdef NUM_PING_TIMES
700 host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = host_client->cmd.receivetime - host_client->cmd.clienttime;
701 host_client->num_pings++;
702 for (i=0, total = 0;i < NUM_PING_TIMES;i++)
703 total += host_client->ping_times[i];
704 host_client->ping = total / NUM_PING_TIMES;
708 void SV_ApplyClientMove (void)
710 prvm_prog_t *prog = SVVM_prog;
711 usercmd_t *move = &host_client->cmd;
712 int j, movementloss, packetloss;
714 if (!move->receivetime)
717 // note: a move can be applied multiple times if the client packets are
718 // not coming as often as the physics is executed, and the move must be
719 // applied before running qc each time because the id1 qc had a bug where
720 // it clears self.button2 in PlayerJump, causing pogostick behavior if
721 // moves are not applied every time before calling qc
722 move->applied = true;
724 // set the edict fields
725 PRVM_serveredictfloat(host_client->edict, button0) = move->buttons & 1;
726 PRVM_serveredictfloat(host_client->edict, button2) = (move->buttons & 2)>>1;
728 PRVM_serveredictfloat(host_client->edict, impulse) = move->impulse;
729 // only send the impulse to qc once
732 movementloss = packetloss = 0;
733 if(host_client->netconnection)
735 for (j = 0;j < NETGRAPH_PACKETS;j++)
736 if (host_client->netconnection->incoming_netgraph[j].unreliablebytes == NETGRAPH_LOSTPACKET)
738 for (j = 0;j < NETGRAPH_PACKETS;j++)
739 if (host_client->movement_count[j] < 0)
743 VectorCopy(move->viewangles, PRVM_serveredictvector(host_client->edict, v_angle));
744 PRVM_serveredictfloat(host_client->edict, button3) = ((move->buttons >> 2) & 1);
745 PRVM_serveredictfloat(host_client->edict, button4) = ((move->buttons >> 3) & 1);
746 PRVM_serveredictfloat(host_client->edict, button5) = ((move->buttons >> 4) & 1);
747 PRVM_serveredictfloat(host_client->edict, button6) = ((move->buttons >> 5) & 1);
748 PRVM_serveredictfloat(host_client->edict, button7) = ((move->buttons >> 6) & 1);
749 PRVM_serveredictfloat(host_client->edict, button8) = ((move->buttons >> 7) & 1);
750 PRVM_serveredictfloat(host_client->edict, button9) = ((move->buttons >> 11) & 1);
751 PRVM_serveredictfloat(host_client->edict, button10) = ((move->buttons >> 12) & 1);
752 PRVM_serveredictfloat(host_client->edict, button11) = ((move->buttons >> 13) & 1);
753 PRVM_serveredictfloat(host_client->edict, button12) = ((move->buttons >> 14) & 1);
754 PRVM_serveredictfloat(host_client->edict, button13) = ((move->buttons >> 15) & 1);
755 PRVM_serveredictfloat(host_client->edict, button14) = ((move->buttons >> 16) & 1);
756 PRVM_serveredictfloat(host_client->edict, button15) = ((move->buttons >> 17) & 1);
757 PRVM_serveredictfloat(host_client->edict, button16) = ((move->buttons >> 18) & 1);
758 PRVM_serveredictfloat(host_client->edict, buttonuse) = ((move->buttons >> 8) & 1);
759 PRVM_serveredictfloat(host_client->edict, buttonchat) = ((move->buttons >> 9) & 1);
760 PRVM_serveredictfloat(host_client->edict, cursor_active) = ((move->buttons >> 10) & 1);
761 VectorSet(PRVM_serveredictvector(host_client->edict, movement), move->forwardmove, move->sidemove, move->upmove);
762 VectorCopy(move->cursor_screen, PRVM_serveredictvector(host_client->edict, cursor_screen));
763 VectorCopy(move->cursor_start, PRVM_serveredictvector(host_client->edict, cursor_trace_start));
764 VectorCopy(move->cursor_impact, PRVM_serveredictvector(host_client->edict, cursor_trace_endpos));
765 PRVM_serveredictedict(host_client->edict, cursor_trace_ent) = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
766 PRVM_serveredictfloat(host_client->edict, ping) = host_client->ping * 1000.0;
767 PRVM_serveredictfloat(host_client->edict, ping_packetloss) = packetloss / (float) NETGRAPH_PACKETS;
768 PRVM_serveredictfloat(host_client->edict, ping_movementloss) = movementloss / (float) NETGRAPH_PACKETS;
771 static qboolean SV_FrameLost(int framenum)
773 if (host_client->entitydatabase5)
775 if (framenum <= host_client->entitydatabase5->latestframenum)
777 EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
778 EntityFrameCSQC_LostFrame(host_client, framenum);
785 static void SV_FrameAck(int framenum)
787 if (host_client->entitydatabase)
788 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
789 else if (host_client->entitydatabase4)
790 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
791 else if (host_client->entitydatabase5)
792 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
800 void SV_ReadClientMessage(void)
802 prvm_prog_t *prog = SVVM_prog;
803 int netcmd, num, start;
806 if(sv_autodemo_perclient.integer >= 2)
807 SV_WriteDemoMessage(host_client, &(host_client->netconnection->message), true);
809 //MSG_BeginReading ();
814 if (!host_client->active)
816 // a command caused an error
817 SV_DropClient (false);
821 if (sv_message.badread)
823 Con_Print("SV_ReadClientMessage: badread\n");
824 SV_DropClient (false);
828 netcmd = MSG_ReadByte(&sv_message);
832 // apply the moves that were read this frame
833 SV_ExecuteClientMoves();
840 Con_Printf("SV_ReadClientMessage: unknown command char %i (at offset 0x%x)\n", netcmd, sv_message.readcount);
841 if (developer_networking.integer)
842 Com_HexDumpToConsole(sv_message.data, sv_message.cursize);
843 SV_DropClient (false);
850 // allow reliable messages now as the client is done with initial loading
851 if (host_client->sendsignon == 2)
852 host_client->sendsignon = 0;
853 s = MSG_ReadString(&sv_message, sv_readstring, sizeof(sv_readstring));
855 for(p = s; *p; ++p) switch(*p)
864 goto clc_stringcmd_invalid; // newline seen, THEN something else -> possible exploit
869 if (strncasecmp(s, "spawn", 5) == 0
870 || strncasecmp(s, "begin", 5) == 0
871 || strncasecmp(s, "prespawn", 8) == 0)
872 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
873 else if (PRVM_serverfunction(SV_ParseClientCommand))
875 int restorevm_tempstringsbuf_cursize;
876 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
877 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, s);
878 PRVM_serverglobalfloat(time) = sv.time;
879 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
880 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_ParseClientCommand), "QC function SV_ParseClientCommand is missing");
881 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
884 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
887 clc_stringcmd_invalid:
888 Con_Printf("Received invalid stringcmd from %s\n", host_client->name);
889 if(developer.integer > 0)
890 Com_HexDumpToConsole((unsigned char *) s, (int)strlen(s));
894 SV_DropClient (false); // client wants to disconnect
901 case clc_ackdownloaddata:
902 start = MSG_ReadLong(&sv_message);
903 num = MSG_ReadShort(&sv_message);
904 if (host_client->download_file && host_client->download_started)
906 if (host_client->download_expectedposition == start)
908 int size = (int)FS_FileSize(host_client->download_file);
909 // a data block was successfully received by the client,
910 // update the expected position on the next data block
911 host_client->download_expectedposition = start + num;
912 // if this was the last data block of the file, it's done
913 if (host_client->download_expectedposition >= FS_FileSize(host_client->download_file))
915 // tell the client that the download finished
916 // we need to calculate the crc now
918 // note: at this point the OS probably has the file
919 // entirely in memory, so this is a faster operation
920 // now than it was when the download started.
922 // it is also preferable to do this at the end of the
923 // download rather than the start because it reduces
924 // potential for Denial Of Service attacks against the
928 FS_Seek(host_client->download_file, 0, SEEK_SET);
929 temp = (unsigned char *) Mem_Alloc(tempmempool, size);
930 FS_Read(host_client->download_file, temp, size);
931 crc = CRC_Block(temp, size);
933 // calculated crc, send the file info to the client
934 // (so that it can verify the data)
935 Host_ClientCommands("\ncl_downloadfinished %i %i %s\n", size, crc, host_client->download_name);
936 Con_DPrintf("Download of %s by %s has finished\n", host_client->download_name, host_client->name);
937 FS_Close(host_client->download_file);
938 host_client->download_file = NULL;
939 host_client->download_name[0] = 0;
940 host_client->download_expectedposition = 0;
941 host_client->download_started = false;
946 // a data block was lost, reset to the expected position
947 // and resume sending from there
948 FS_Seek(host_client->download_file, host_client->download_expectedposition, SEEK_SET);
954 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
955 num = MSG_ReadLong(&sv_message);
956 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
957 if (developer_networkentities.integer >= 10)
958 Con_Printf("recv clc_ackframe %i\n", num);
959 // if the client hasn't progressed through signons yet,
960 // ignore any clc_ackframes we get (they're probably from the
962 if (host_client->begun && host_client->latestframenum < num)
965 for (i = host_client->latestframenum + 1;i < num;i++)
966 if (!SV_FrameLost(i))
969 host_client->latestframenum = num;