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);
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));
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;
200 len = VectorNormalizeLength (PRVM_serveredictvector(host_client->edict, punchangle));
202 len -= 10*sv.frametime;
205 VectorScale (PRVM_serveredictvector(host_client->edict, punchangle), len, PRVM_serveredictvector(host_client->edict, punchangle));
207 VectorCopy(PRVM_serveredictvector(host_client->edict, punchvector), v);
208 len = VectorNormalizeLength(v);
211 len -= 20*sv.frametime;
214 VectorScale(v, len, v);
216 VectorCopy(v, PRVM_serveredictvector(host_client->edict, punchvector));
225 static void SV_WaterMove (void)
227 prvm_prog_t *prog = SVVM_prog;
230 float speed, newspeed, wishspeed, addspeed, accelspeed, temp;
233 AngleVectors (PRVM_serveredictvector(host_client->edict, v_angle), forward, right, up);
235 for (i=0 ; i<3 ; i++)
236 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
238 if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
239 wishvel[2] -= 60; // drift towards bottom
241 wishvel[2] += cmd.upmove;
243 wishspeed = VectorLength(wishvel);
244 if (wishspeed > sv_maxspeed.value)
246 temp = sv_maxspeed.value/wishspeed;
247 VectorScale (wishvel, temp, wishvel);
248 wishspeed = sv_maxspeed.value;
253 speed = VectorLength(PRVM_serveredictvector(host_client->edict, velocity));
256 newspeed = speed - sv.frametime * speed * (sv_waterfriction.value < 0 ? sv_friction.value : sv_waterfriction.value);
259 temp = newspeed/speed;
260 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), temp, PRVM_serveredictvector(host_client->edict, velocity));
265 // water acceleration
269 addspeed = wishspeed - newspeed;
273 VectorNormalize (wishvel);
274 accelspeed = (sv_wateraccelerate.value < 0 ? sv_accelerate.value : sv_wateraccelerate.value) * wishspeed * sv.frametime;
275 if (accelspeed > addspeed)
276 accelspeed = addspeed;
278 for (i=0 ; i<3 ; i++)
279 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed * wishvel[i];
282 static void SV_WaterJump (void)
284 prvm_prog_t *prog = SVVM_prog;
285 if (sv.time > PRVM_serveredictfloat(host_client->edict, teleport_time) || !PRVM_serveredictfloat(host_client->edict, waterlevel))
287 PRVM_serveredictfloat(host_client->edict, flags) = (int)PRVM_serveredictfloat(host_client->edict, flags) & ~FL_WATERJUMP;
288 PRVM_serveredictfloat(host_client->edict, teleport_time) = 0;
290 PRVM_serveredictvector(host_client->edict, velocity)[0] = PRVM_serveredictvector(host_client->edict, movedir)[0];
291 PRVM_serveredictvector(host_client->edict, velocity)[1] = PRVM_serveredictvector(host_client->edict, movedir)[1];
301 static void SV_AirMove (void)
303 prvm_prog_t *prog = SVVM_prog;
306 float fmove, smove, temp;
308 // LordHavoc: correct quake movement speed bug when looking up/down
309 wishvel[0] = wishvel[2] = 0;
310 wishvel[1] = PRVM_serveredictvector(host_client->edict, angles)[1];
311 AngleVectors (wishvel, forward, right, up);
313 fmove = cmd.forwardmove;
314 smove = cmd.sidemove;
316 // hack to not let you back into teleporter
317 if (sv.time < PRVM_serveredictfloat(host_client->edict, teleport_time) && fmove < 0)
320 for (i=0 ; i<3 ; i++)
321 wishvel[i] = forward[i]*fmove + right[i]*smove;
323 if ((int)PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_WALK)
324 wishvel[2] += cmd.upmove;
326 VectorCopy (wishvel, wishdir);
327 wishspeed = VectorNormalizeLength(wishdir);
328 if (wishspeed > sv_maxspeed.value)
330 temp = sv_maxspeed.value/wishspeed;
331 VectorScale (wishvel, temp, wishvel);
332 wishspeed = sv_maxspeed.value;
335 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP)
338 VectorCopy (wishvel, PRVM_serveredictvector(host_client->edict, velocity));
347 // not on ground, so little effect on velocity
348 SV_AirAccelerate (wishvel);
356 the move fields specify an intended velocity in pix/sec
357 the angle fields specify an exact angular motion in degrees
360 void SV_ClientThink (void)
362 prvm_prog_t *prog = SVVM_prog;
365 //Con_Printf("clientthink for %ims\n", (int) (sv.frametime * 1000));
367 SV_ApplyClientMove();
368 // make sure the velocity is sane (not a NaN)
369 SV_CheckVelocity(host_client->edict);
371 // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
372 if (PRVM_serverfunction(SV_PlayerPhysics) && sv_playerphysicsqc.integer)
374 PRVM_serverglobalfloat(time) = sv.time;
375 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
376 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PlayerPhysics), "QC function SV_PlayerPhysics is missing");
377 SV_CheckVelocity(host_client->edict);
381 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NONE)
384 onground = ((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND) != 0;
388 // if dead, behave differently
389 if (PRVM_serveredictfloat(host_client->edict, health) <= 0)
392 cmd = host_client->cmd;
395 // show 1/3 the pitch angle and all the roll angle
396 VectorAdd (PRVM_serveredictvector(host_client->edict, v_angle), PRVM_serveredictvector(host_client->edict, punchangle), v_angle);
397 PRVM_serveredictvector(host_client->edict, angles)[ROLL] = V_CalcRoll (PRVM_serveredictvector(host_client->edict, angles), PRVM_serveredictvector(host_client->edict, velocity))*4;
398 if (!PRVM_serveredictfloat(host_client->edict, fixangle))
400 PRVM_serveredictvector(host_client->edict, angles)[PITCH] = -v_angle[PITCH]/3;
401 PRVM_serveredictvector(host_client->edict, angles)[YAW] = v_angle[YAW];
404 if ( (int)PRVM_serveredictfloat(host_client->edict, flags) & FL_WATERJUMP )
407 SV_CheckVelocity(host_client->edict);
412 // Player is (somehow) outside of the map, or flying, or noclipping
413 if (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP && (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict)))
414 //if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP || PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict))
422 if ((PRVM_serveredictfloat(host_client->edict, waterlevel) >= 2) && (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP))
425 SV_CheckVelocity(host_client->edict);
430 SV_CheckVelocity(host_client->edict);
438 int sv_numreadmoves = 0;
439 usercmd_t sv_readmoves[CL_MAX_USERCMDS];
440 static void SV_ReadClientMove (void)
442 prvm_prog_t *prog = SVVM_prog;
445 usercmd_t *move = &newmove;
447 memset(move, 0, sizeof(*move));
449 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
452 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)
453 move->sequence = MSG_ReadLong(&sv_message);
454 move->time = move->clienttime = MSG_ReadFloat(&sv_message);
455 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
456 move->receivetime = (float)sv.time;
459 Con_Printf("%s move%i #%i %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);
461 // limit reported time to current time
462 // (incase the client is trying to cheat)
463 move->time = min(move->time, move->receivetime + sv.frametime);
465 // read current angles
466 for (i = 0;i < 3;i++)
468 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)
469 move->viewangles[i] = MSG_ReadAngle8i(&sv_message);
470 else if (sv.protocol == PROTOCOL_DARKPLACES1)
471 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
472 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
473 move->viewangles[i] = MSG_ReadAngle32f(&sv_message);
475 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
477 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
480 move->forwardmove = MSG_ReadCoord16i(&sv_message);
481 move->sidemove = MSG_ReadCoord16i(&sv_message);
482 move->upmove = MSG_ReadCoord16i(&sv_message);
483 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
486 // be sure to bitwise OR them into the move->buttons because we want to
487 // accumulate button presses from multiple packets per actual move
488 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)
489 move->buttons = MSG_ReadByte(&sv_message);
491 move->buttons = MSG_ReadLong(&sv_message);
492 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
495 move->impulse = MSG_ReadByte(&sv_message);
496 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
498 // PRYDON_CLIENTCURSOR
499 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)
502 move->cursor_screen[0] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
503 move->cursor_screen[1] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
504 move->cursor_start[0] = MSG_ReadFloat(&sv_message);
505 move->cursor_start[1] = MSG_ReadFloat(&sv_message);
506 move->cursor_start[2] = MSG_ReadFloat(&sv_message);
507 move->cursor_impact[0] = MSG_ReadFloat(&sv_message);
508 move->cursor_impact[1] = MSG_ReadFloat(&sv_message);
509 move->cursor_impact[2] = MSG_ReadFloat(&sv_message);
510 move->cursor_entitynumber = (unsigned short)MSG_ReadShort(&sv_message);
511 if (move->cursor_entitynumber >= prog->max_edicts)
513 Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
514 move->cursor_entitynumber = 0;
516 // as requested by FrikaC, cursor_trace_ent is reset to world if the
517 // entity is free at time of receipt
518 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
519 move->cursor_entitynumber = 0;
520 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
523 // if the previous move has not been applied yet, we need to accumulate
524 // the impulse/buttons from it
525 if (!host_client->cmd.applied)
528 move->impulse = host_client->cmd.impulse;
529 move->buttons |= host_client->cmd.buttons;
532 // now store this move for later execution
533 // (we have to buffer the moves because of old ones being repeated)
534 if (sv_numreadmoves < CL_MAX_USERCMDS)
535 sv_readmoves[sv_numreadmoves++] = *move;
537 // movement packet loss tracking
540 if(move->sequence > host_client->movement_highestsequence_seen)
542 if(host_client->movement_highestsequence_seen)
544 // mark moves in between as lost
545 if(move->sequence - host_client->movement_highestsequence_seen - 1 < NETGRAPH_PACKETS)
546 for(i = host_client->movement_highestsequence_seen + 1; i < move->sequence; ++i)
547 host_client->movement_count[i % NETGRAPH_PACKETS] = -1;
549 memset(host_client->movement_count, -1, sizeof(host_client->movement_count));
551 // mark THIS move as seen for the first time
552 host_client->movement_count[move->sequence % NETGRAPH_PACKETS] = 1;
553 // update highest sequence seen
554 host_client->movement_highestsequence_seen = move->sequence;
557 if(host_client->movement_count[move->sequence % NETGRAPH_PACKETS] >= 0)
558 ++host_client->movement_count[move->sequence % NETGRAPH_PACKETS];
562 host_client->movement_highestsequence_seen = 0;
563 memset(host_client->movement_count, 0, sizeof(host_client->movement_count));
567 static void SV_ExecuteClientMoves(void)
569 prvm_prog_t *prog = SVVM_prog;
573 double oldframetime2;
574 #ifdef NUM_PING_TIMES
577 if (sv_numreadmoves < 1)
579 // only start accepting input once the player is spawned
580 if (!host_client->spawned)
583 Con_Printf("SV_ExecuteClientMoves: read %i moves at sv.time %f\n", sv_numreadmoves, (float)sv.time);
585 // disable clientside movement prediction in some cases
586 if (ceil(max(sv_readmoves[sv_numreadmoves-1].receivetime - sv_readmoves[sv_numreadmoves-1].time, 0) * 1000.0) < sv_clmovement_minping.integer)
587 host_client->clmovement_disabletimeout = realtime + sv_clmovement_minping_disabletime.value / 1000.0;
588 // several conditions govern whether clientside movement prediction is allowed
589 if (sv_readmoves[sv_numreadmoves-1].sequence && sv_clmovement_enable.integer && sv_clmovement_inputtimeout.value > 0 && host_client->clmovement_disabletimeout <= realtime && PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_WALK && (!PRVM_serveredictfloat(host_client->edict, disableclientprediction)))
591 // process the moves in order and ignore old ones
592 // but always trust the latest move
593 // (this deals with bogus initial move sequences after level change,
594 // where the client will eventually catch up with the level change
595 // and reset its move sequence)
596 for (moveindex = 0;moveindex < sv_numreadmoves;moveindex++)
598 usercmd_t *move = sv_readmoves + moveindex;
599 if (host_client->movesequence < move->sequence || moveindex == sv_numreadmoves - 1)
602 Con_Printf("%smove #%i %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);
604 // this is a new move
605 move->time = bound(sv.time - 1, move->time, sv.time); // prevent slowhack/speedhack combos
606 move->time = max(move->time, host_client->cmd.time); // prevent backstepping of time
607 moveframetime = bound(0, move->time - host_client->cmd.time, min(0.1, sv_clmovement_inputtimeout.value));
609 // discard (treat like lost) moves with too low distance from
610 // the previous one to prevent hacks using float inaccuracy
611 // clients will see this as packet loss in the netgraph
612 // this should also apply if a move cannot get
613 // executed because it came too late and
614 // already was performed serverside
615 if(moveframetime < 0.0005)
617 // count the move as LOST if we don't
618 // execute it but it has higher
620 if(host_client->movesequence)
621 if(move->sequence > host_client->movesequence)
622 host_client->movement_count[(move->sequence) % NETGRAPH_PACKETS] = -1;
626 //Con_Printf("movesequence = %i (%i lost), moveframetime = %f\n", move->sequence, move->sequence ? move->sequence - host_client->movesequence - 1 : 0, moveframetime);
627 host_client->cmd = *move;
628 host_client->movesequence = move->sequence;
630 // if using prediction, we need to perform moves when packets are
631 // received, even if multiple occur in one frame
632 // (they can't go beyond the current time so there is no cheat issue
633 // with this approach, and if they don't send input for a while they
634 // start moving anyway, so the longest 'lagaport' possible is
635 // determined by the sv_clmovement_inputtimeout cvar)
636 if (moveframetime <= 0)
638 oldframetime = PRVM_serverglobalfloat(frametime);
639 oldframetime2 = sv.frametime;
640 // update ping time for qc to see while executing this move
641 host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
642 // the server and qc frametime values must be changed temporarily
643 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime;
644 // if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
645 if (sv.frametime > 0.05)
647 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime * 0.5f;
648 SV_Physics_ClientMove();
650 SV_Physics_ClientMove();
651 sv.frametime = oldframetime2;
652 PRVM_serverglobalfloat(frametime) = oldframetime;
653 host_client->clmovement_inputtimeout = sv_clmovement_inputtimeout.value;
659 // try to gather button bits from old moves, but only if their time is
660 // advancing (ones with the same timestamp can't be trusted)
661 for (moveindex = 0;moveindex < sv_numreadmoves-1;moveindex++)
663 usercmd_t *move = sv_readmoves + moveindex;
664 if (host_client->cmd.time < move->time)
666 sv_readmoves[sv_numreadmoves-1].buttons |= move->buttons;
668 sv_readmoves[sv_numreadmoves-1].impulse = move->impulse;
671 // now copy the new move
672 host_client->cmd = sv_readmoves[sv_numreadmoves-1];
673 host_client->cmd.time = max(host_client->cmd.time, sv.time);
674 // physics will run up to sv.time, so allow no predicted moves
675 // before that otherwise, there is a speedhack by turning
676 // prediction on and off repeatedly on client side because the
677 // engine would run BOTH client and server physics for the same
679 host_client->movesequence = 0;
680 // make sure that normal physics takes over immediately
681 host_client->clmovement_inputtimeout = 0;
684 // calculate average ping time
685 host_client->ping = host_client->cmd.receivetime - host_client->cmd.clienttime;
686 #ifdef NUM_PING_TIMES
687 host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = host_client->cmd.receivetime - host_client->cmd.clienttime;
688 host_client->num_pings++;
689 for (i=0, total = 0;i < NUM_PING_TIMES;i++)
690 total += host_client->ping_times[i];
691 host_client->ping = total / NUM_PING_TIMES;
695 void SV_ApplyClientMove (void)
697 prvm_prog_t *prog = SVVM_prog;
698 usercmd_t *move = &host_client->cmd;
699 int j, movementloss, packetloss;
701 if (!move->receivetime)
704 // note: a move can be applied multiple times if the client packets are
705 // not coming as often as the physics is executed, and the move must be
706 // applied before running qc each time because the id1 qc had a bug where
707 // it clears self.button2 in PlayerJump, causing pogostick behavior if
708 // moves are not applied every time before calling qc
709 move->applied = true;
711 // set the edict fields
712 PRVM_serveredictfloat(host_client->edict, button0) = move->buttons & 1;
713 PRVM_serveredictfloat(host_client->edict, button2) = (move->buttons & 2)>>1;
715 PRVM_serveredictfloat(host_client->edict, impulse) = move->impulse;
716 // only send the impulse to qc once
719 movementloss = packetloss = 0;
720 if(host_client->netconnection)
722 for (j = 0;j < NETGRAPH_PACKETS;j++)
723 if (host_client->netconnection->incoming_netgraph[j].unreliablebytes == NETGRAPH_LOSTPACKET)
725 for (j = 0;j < NETGRAPH_PACKETS;j++)
726 if (host_client->movement_count[j] < 0)
730 VectorCopy(move->viewangles, PRVM_serveredictvector(host_client->edict, v_angle));
731 PRVM_serveredictfloat(host_client->edict, button3) = ((move->buttons >> 2) & 1);
732 PRVM_serveredictfloat(host_client->edict, button4) = ((move->buttons >> 3) & 1);
733 PRVM_serveredictfloat(host_client->edict, button5) = ((move->buttons >> 4) & 1);
734 PRVM_serveredictfloat(host_client->edict, button6) = ((move->buttons >> 5) & 1);
735 PRVM_serveredictfloat(host_client->edict, button7) = ((move->buttons >> 6) & 1);
736 PRVM_serveredictfloat(host_client->edict, button8) = ((move->buttons >> 7) & 1);
737 PRVM_serveredictfloat(host_client->edict, button9) = ((move->buttons >> 11) & 1);
738 PRVM_serveredictfloat(host_client->edict, button10) = ((move->buttons >> 12) & 1);
739 PRVM_serveredictfloat(host_client->edict, button11) = ((move->buttons >> 13) & 1);
740 PRVM_serveredictfloat(host_client->edict, button12) = ((move->buttons >> 14) & 1);
741 PRVM_serveredictfloat(host_client->edict, button13) = ((move->buttons >> 15) & 1);
742 PRVM_serveredictfloat(host_client->edict, button14) = ((move->buttons >> 16) & 1);
743 PRVM_serveredictfloat(host_client->edict, button15) = ((move->buttons >> 17) & 1);
744 PRVM_serveredictfloat(host_client->edict, button16) = ((move->buttons >> 18) & 1);
745 PRVM_serveredictfloat(host_client->edict, buttonuse) = ((move->buttons >> 8) & 1);
746 PRVM_serveredictfloat(host_client->edict, buttonchat) = ((move->buttons >> 9) & 1);
747 PRVM_serveredictfloat(host_client->edict, cursor_active) = ((move->buttons >> 10) & 1);
748 VectorSet(PRVM_serveredictvector(host_client->edict, movement), move->forwardmove, move->sidemove, move->upmove);
749 VectorCopy(move->cursor_screen, PRVM_serveredictvector(host_client->edict, cursor_screen));
750 VectorCopy(move->cursor_start, PRVM_serveredictvector(host_client->edict, cursor_trace_start));
751 VectorCopy(move->cursor_impact, PRVM_serveredictvector(host_client->edict, cursor_trace_endpos));
752 PRVM_serveredictedict(host_client->edict, cursor_trace_ent) = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
753 PRVM_serveredictfloat(host_client->edict, ping) = host_client->ping * 1000.0;
754 PRVM_serveredictfloat(host_client->edict, ping_packetloss) = packetloss / (float) NETGRAPH_PACKETS;
755 PRVM_serveredictfloat(host_client->edict, ping_movementloss) = movementloss / (float) NETGRAPH_PACKETS;
758 static void SV_FrameLost(int framenum)
760 if (host_client->entitydatabase5)
762 EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
763 EntityFrameCSQC_LostFrame(host_client, framenum);
767 static void SV_FrameAck(int framenum)
769 if (host_client->entitydatabase)
770 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
771 else if (host_client->entitydatabase4)
772 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
773 else if (host_client->entitydatabase5)
774 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
782 void SV_ReadClientMessage(void)
784 prvm_prog_t *prog = SVVM_prog;
788 if(sv_autodemo_perclient.integer >= 2)
789 SV_WriteDemoMessage(host_client, &(host_client->netconnection->message), true);
791 //MSG_BeginReading ();
796 if (!host_client->active)
798 // a command caused an error
799 SV_DropClient (false);
803 if (sv_message.badread)
805 Con_Print("SV_ReadClientMessage: badread\n");
806 SV_DropClient (false);
810 cmd = MSG_ReadByte(&sv_message);
814 // apply the moves that were read this frame
815 SV_ExecuteClientMoves();
822 Con_Printf("SV_ReadClientMessage: unknown command char %i (at offset 0x%x)\n", cmd, sv_message.readcount);
823 if (developer_networking.integer)
824 Com_HexDumpToConsole(sv_message.data, sv_message.cursize);
825 SV_DropClient (false);
832 // allow reliable messages now as the client is done with initial loading
833 if (host_client->sendsignon == 2)
834 host_client->sendsignon = 0;
835 s = MSG_ReadString(&sv_message, sv_readstring, sizeof(sv_readstring));
837 for(p = s; *p; ++p) switch(*p)
846 goto clc_stringcmd_invalid; // newline seen, THEN something else -> possible exploit
851 if (strncasecmp(s, "spawn", 5) == 0
852 || strncasecmp(s, "begin", 5) == 0
853 || strncasecmp(s, "prespawn", 8) == 0)
854 Cmd_ExecuteString (s, src_client, true);
855 else if (PRVM_serverfunction(SV_ParseClientCommand))
857 int restorevm_tempstringsbuf_cursize;
858 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
859 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, s);
860 PRVM_serverglobalfloat(time) = sv.time;
861 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
862 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_ParseClientCommand), "QC function SV_ParseClientCommand is missing");
863 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
866 Cmd_ExecuteString (s, src_client, true);
869 clc_stringcmd_invalid:
870 Con_Printf("Received invalid stringcmd from %s\n", host_client->name);
871 if(developer.integer > 0)
872 Com_HexDumpToConsole((unsigned char *) s, strlen(s));
876 SV_DropClient (false); // client wants to disconnect
883 case clc_ackdownloaddata:
884 start = MSG_ReadLong(&sv_message);
885 num = MSG_ReadShort(&sv_message);
886 if (host_client->download_file && host_client->download_started)
888 if (host_client->download_expectedposition == start)
890 int size = (int)FS_FileSize(host_client->download_file);
891 // a data block was successfully received by the client,
892 // update the expected position on the next data block
893 host_client->download_expectedposition = start + num;
894 // if this was the last data block of the file, it's done
895 if (host_client->download_expectedposition >= FS_FileSize(host_client->download_file))
897 // tell the client that the download finished
898 // we need to calculate the crc now
900 // note: at this point the OS probably has the file
901 // entirely in memory, so this is a faster operation
902 // now than it was when the download started.
904 // it is also preferable to do this at the end of the
905 // download rather than the start because it reduces
906 // potential for Denial Of Service attacks against the
910 FS_Seek(host_client->download_file, 0, SEEK_SET);
911 temp = (unsigned char *) Mem_Alloc(tempmempool, size);
912 FS_Read(host_client->download_file, temp, size);
913 crc = CRC_Block(temp, size);
915 // calculated crc, send the file info to the client
916 // (so that it can verify the data)
917 Host_ClientCommands("\ncl_downloadfinished %i %i %s\n", size, crc, host_client->download_name);
918 Con_DPrintf("Download of %s by %s has finished\n", host_client->download_name, host_client->name);
919 FS_Close(host_client->download_file);
920 host_client->download_file = NULL;
921 host_client->download_name[0] = 0;
922 host_client->download_expectedposition = 0;
923 host_client->download_started = false;
928 // a data block was lost, reset to the expected position
929 // and resume sending from there
930 FS_Seek(host_client->download_file, host_client->download_expectedposition, SEEK_SET);
936 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
937 num = MSG_ReadLong(&sv_message);
938 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
939 if (developer_networkentities.integer >= 10)
940 Con_Printf("recv clc_ackframe %i\n", num);
941 // if the client hasn't progressed through signons yet,
942 // ignore any clc_ackframes we get (they're probably from the
944 if (host_client->spawned && host_client->latestframenum < num)
947 for (i = host_client->latestframenum + 1;i < num;i++)
950 host_client->latestframenum = num;