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
26 static usercmd_t usercmd;
27 extern cvar_t sv_autodemo_perclient;
28 extern cvar_t sv_rollangle;
29 extern cvar_t sv_rollspeed;
36 void SV_PreSpawn_f(cmd_state_t *cmd)
38 if (host_client->prespawned)
40 Con_Print("prespawn not valid -- already prespawned\n");
43 host_client->prespawned = true;
45 if (host_client->netconnection)
47 SZ_Write (&host_client->netconnection->message, sv.signon.data, sv.signon.cursize);
48 MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
49 MSG_WriteByte (&host_client->netconnection->message, 2);
50 host_client->sendsignon = 0; // enable unlimited sends again
53 // reset the name change timer because the client will send name soon
54 host_client->nametime = 0;
62 void SV_Spawn_f(cmd_state_t *cmd)
64 prvm_prog_t *prog = SVVM_prog;
67 int stats[MAX_CL_STATS];
69 if (!host_client->prespawned)
71 Con_Print("Spawn not valid -- not yet prespawned\n");
74 if (host_client->spawned)
76 Con_Print("Spawn not valid -- already spawned\n");
79 host_client->spawned = true;
81 // reset name change timer again because they might want to change name
82 // again in the first 5 seconds after connecting
83 host_client->nametime = 0;
85 // LadyHavoc: moved this above the QC calls at FrikaC's request
86 // LadyHavoc: commented this out
87 //if (host_client->netconnection)
88 // SZ_Clear (&host_client->netconnection->message);
90 // run the entrance script
93 // loaded games are fully initialized already
94 if (PRVM_serverfunction(RestoreGame))
96 Con_DPrint("Calling RestoreGame\n");
97 PRVM_serverglobalfloat(time) = sv.time;
98 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
99 prog->ExecuteProgram(prog, PRVM_serverfunction(RestoreGame), "QC function RestoreGame is missing");
104 //Con_Printf("SV_Spawn_f: host_client->edict->netname = %s, host_client->edict->netname = %s, host_client->name = %s\n", PRVM_GetString(PRVM_serveredictstring(host_client->edict, netname)), PRVM_GetString(PRVM_serveredictstring(host_client->edict, netname)), host_client->name);
106 // copy spawn parms out of the client_t
107 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
108 (&PRVM_serverglobalfloat(parm1))[i] = host_client->spawn_parms[i];
110 // call the spawn function
111 host_client->clientconnectcalled = true;
112 PRVM_serverglobalfloat(time) = sv.time;
113 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
114 prog->ExecuteProgram(prog, PRVM_serverfunction(ClientConnect), "QC function ClientConnect is missing");
116 Con_Printf("%s connected\n", host_client->name);
118 PRVM_serverglobalfloat(time) = sv.time;
119 prog->ExecuteProgram(prog, PRVM_serverfunction(PutClientInServer), "QC function PutClientInServer is missing");
122 if (!host_client->netconnection)
125 // send time of update
126 MSG_WriteByte (&host_client->netconnection->message, svc_time);
127 MSG_WriteFloat (&host_client->netconnection->message, sv.time);
129 // send all current names, colors, and frag counts
130 for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
134 MSG_WriteByte (&host_client->netconnection->message, svc_updatename);
135 MSG_WriteByte (&host_client->netconnection->message, i);
136 MSG_WriteString (&host_client->netconnection->message, client->name);
137 MSG_WriteByte (&host_client->netconnection->message, svc_updatefrags);
138 MSG_WriteByte (&host_client->netconnection->message, i);
139 MSG_WriteShort (&host_client->netconnection->message, client->frags);
140 MSG_WriteByte (&host_client->netconnection->message, svc_updatecolors);
141 MSG_WriteByte (&host_client->netconnection->message, i);
142 MSG_WriteByte (&host_client->netconnection->message, client->colors);
145 // send all current light styles
146 for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
148 if (sv.lightstyles[i][0])
150 MSG_WriteByte (&host_client->netconnection->message, svc_lightstyle);
151 MSG_WriteByte (&host_client->netconnection->message, (char)i);
152 MSG_WriteString (&host_client->netconnection->message, sv.lightstyles[i]);
157 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
158 MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALSECRETS);
159 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(total_secrets));
161 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
162 MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALMONSTERS);
163 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(total_monsters));
165 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
166 MSG_WriteByte (&host_client->netconnection->message, STAT_SECRETS);
167 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(found_secrets));
169 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
170 MSG_WriteByte (&host_client->netconnection->message, STAT_MONSTERS);
171 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(killed_monsters));
174 // Never send a roll angle, because savegames can catch the server
175 // in a state where it is expecting the client to correct the angle
176 // and it won't happen if the game was just loaded, so you wind up
177 // with a permanent head tilt
180 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
181 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, v_angle)[0], sv.protocol);
182 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, v_angle)[1], sv.protocol);
183 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
187 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
188 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, angles)[0], sv.protocol);
189 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, angles)[1], sv.protocol);
190 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
193 SV_WriteClientdataToMessage (host_client, host_client->edict, &host_client->netconnection->message, stats);
195 MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
196 MSG_WriteByte (&host_client->netconnection->message, 3);
204 void SV_Begin_f(cmd_state_t *cmd)
206 if (!host_client->spawned)
208 Con_Print("Begin not valid -- not yet spawned\n");
211 if (host_client->begun)
213 Con_Print("Begin not valid -- already begun\n");
216 host_client->begun = true;
218 // LadyHavoc: note: this code also exists in SV_DropClient
222 for (i = 0;i < svs.maxclients;i++)
223 if (svs.clients[i].active && !svs.clients[i].spawned)
225 if (i == svs.maxclients)
227 Con_Printf("Loaded game, everyone rejoined - unpausing\n");
228 sv.paused = sv.loadgame = false; // we're basically done with loading now
238 #define MAX_FORWARD 6
239 void SV_SetIdealPitch (void)
241 prvm_prog_t *prog = SVVM_prog;
242 float angleval, sinval, cosval, step, dir;
245 float z[MAX_FORWARD];
249 if (!((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND))
252 angleval = PRVM_serveredictvector(host_client->edict, angles)[YAW] * M_PI*2 / 360;
253 sinval = sin(angleval);
254 cosval = cos(angleval);
256 for (i=0 ; i<MAX_FORWARD ; i++)
258 top[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + cosval*(i+3)*12;
259 top[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + sinval*(i+3)*12;
260 top[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, view_ofs)[2];
264 bottom[2] = top[2] - 160;
266 tr = SV_TraceLine(top, bottom, MOVE_NOMONSTERS, host_client->edict, SUPERCONTENTS_SOLID, 0, 0, collision_extendmovelength.value);
267 // if looking at a wall, leave ideal the way is was
272 if (tr.fraction == 1)
275 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
280 for (j=1 ; j<i ; j++)
282 step = z[j] - z[j-1];
283 if (step > -ON_EPSILON && step < ON_EPSILON)
287 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
296 PRVM_serveredictfloat(host_client->edict, idealpitch) = 0;
302 PRVM_serveredictfloat(host_client->edict, idealpitch) = -dir * sv_idealpitchscale.value;
305 static vec3_t wishdir, forward, right, up;
306 static float wishspeed;
308 static qbool onground;
316 static void SV_UserFriction (void)
318 prvm_prog_t *prog = SVVM_prog;
319 float speed, newspeed, control, friction;
323 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]);
327 // if the leading edge is over a dropoff, increase friction
328 start[0] = stop[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + PRVM_serveredictvector(host_client->edict, velocity)[0]/speed*16;
329 start[1] = stop[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + PRVM_serveredictvector(host_client->edict, velocity)[1]/speed*16;
330 start[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, mins)[2];
331 stop[2] = start[2] - 34;
333 trace = SV_TraceLine(start, stop, MOVE_NOMONSTERS, host_client->edict, SV_GenericHitSuperContentsMask(host_client->edict), 0, 0, collision_extendmovelength.value);
335 if (trace.fraction == 1.0)
336 friction = sv_friction.value*sv_edgefriction.value;
338 friction = sv_friction.value;
341 control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
342 newspeed = speed - sv.frametime*control*friction;
349 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), newspeed, PRVM_serveredictvector(host_client->edict, velocity));
357 static void SV_Accelerate (void)
359 prvm_prog_t *prog = SVVM_prog;
361 float addspeed, accelspeed, currentspeed;
363 currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishdir);
364 addspeed = wishspeed - currentspeed;
367 accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
368 if (accelspeed > addspeed)
369 accelspeed = addspeed;
371 for (i=0 ; i<3 ; i++)
372 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishdir[i];
375 extern cvar_t sv_gameplayfix_q2airaccelerate;
376 static void SV_AirAccelerate (vec3_t wishveloc)
378 prvm_prog_t *prog = SVVM_prog;
380 float addspeed, wishspd, accelspeed, currentspeed;
382 wishspd = VectorNormalizeLength (wishveloc);
383 if (wishspd > sv_maxairspeed.value)
384 wishspd = sv_maxairspeed.value;
385 currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishveloc);
386 addspeed = wishspd - currentspeed;
389 accelspeed = (sv_airaccelerate.value < 0 ? sv_accelerate.value : sv_airaccelerate.value)*(sv_gameplayfix_q2airaccelerate.integer ? wishspd : wishspeed) * sv.frametime;
390 if (accelspeed > addspeed)
391 accelspeed = addspeed;
393 for (i=0 ; i<3 ; i++)
394 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishveloc[i];
398 static void DropPunchAngle (void)
400 prvm_prog_t *prog = SVVM_prog;
402 vec3_t punchangle, punchvector;
404 VectorCopy(PRVM_serveredictvector(host_client->edict, punchangle), punchangle);
405 VectorCopy(PRVM_serveredictvector(host_client->edict, punchvector), punchvector);
407 len = VectorNormalizeLength(punchangle);
410 len -= 10*sv.frametime;
413 VectorScale(punchangle, len, punchangle);
416 len = VectorNormalizeLength(punchvector);
419 len -= 20*sv.frametime;
422 VectorScale(punchvector, len, punchvector);
425 VectorCopy(punchangle, PRVM_serveredictvector(host_client->edict, punchangle));
426 VectorCopy(punchvector, PRVM_serveredictvector(host_client->edict, punchvector));
435 static void SV_WaterMove (void)
437 prvm_prog_t *prog = SVVM_prog;
439 vec3_t wishvel, v_angle;
440 vec_t speed, newspeed, fwishspeed, addspeed, accelspeed, temp;
443 VectorCopy(PRVM_serveredictvector(host_client->edict, v_angle), v_angle);
444 AngleVectors(v_angle, forward, right, up);
446 for (i=0 ; i<3 ; i++)
447 wishvel[i] = forward[i]*usercmd.forwardmove + right[i]*usercmd.sidemove;
449 if (!usercmd.forwardmove && !usercmd.sidemove && !usercmd.upmove)
450 wishvel[2] -= 60; // drift towards bottom
452 wishvel[2] += usercmd.upmove;
454 fwishspeed = VectorLength(wishvel);
455 if (fwishspeed > sv_maxspeed.value)
457 temp = sv_maxspeed.value/fwishspeed;
458 VectorScale (wishvel, temp, wishvel);
459 fwishspeed = sv_maxspeed.value;
464 speed = VectorLength(PRVM_serveredictvector(host_client->edict, velocity));
467 newspeed = speed - sv.frametime * speed * (sv_waterfriction.value < 0 ? sv_friction.value : sv_waterfriction.value);
470 temp = newspeed/speed;
471 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), temp, PRVM_serveredictvector(host_client->edict, velocity));
476 // water acceleration
480 addspeed = fwishspeed - newspeed;
484 VectorNormalize (wishvel);
485 accelspeed = (sv_wateraccelerate.value < 0 ? sv_accelerate.value : sv_wateraccelerate.value) * fwishspeed * sv.frametime;
486 if (accelspeed > addspeed)
487 accelspeed = addspeed;
489 for (i=0 ; i<3 ; i++)
490 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed * wishvel[i];
493 static void SV_WaterJump (void)
495 prvm_prog_t *prog = SVVM_prog;
496 if (sv.time > PRVM_serveredictfloat(host_client->edict, teleport_time) || !PRVM_serveredictfloat(host_client->edict, waterlevel))
498 PRVM_serveredictfloat(host_client->edict, flags) = (int)PRVM_serveredictfloat(host_client->edict, flags) & ~FL_WATERJUMP;
499 PRVM_serveredictfloat(host_client->edict, teleport_time) = 0;
501 PRVM_serveredictvector(host_client->edict, velocity)[0] = PRVM_serveredictvector(host_client->edict, movedir)[0];
502 PRVM_serveredictvector(host_client->edict, velocity)[1] = PRVM_serveredictvector(host_client->edict, movedir)[1];
512 static void SV_AirMove (void)
514 prvm_prog_t *prog = SVVM_prog;
517 float fmove, smove, temp;
519 // LadyHavoc: correct quake movement speed bug when looking up/down
520 wishvel[0] = wishvel[2] = 0;
521 wishvel[1] = PRVM_serveredictvector(host_client->edict, angles)[1];
522 AngleVectors (wishvel, forward, right, up);
524 fmove = usercmd.forwardmove;
525 smove = usercmd.sidemove;
527 // hack to not let you back into teleporter
528 if (sv.time < PRVM_serveredictfloat(host_client->edict, teleport_time) && fmove < 0)
531 for (i=0 ; i<3 ; i++)
532 wishvel[i] = forward[i]*fmove + right[i]*smove;
534 if ((int)PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_WALK)
535 wishvel[2] += usercmd.upmove;
537 VectorCopy (wishvel, wishdir);
538 wishspeed = VectorNormalizeLength(wishdir);
539 if (wishspeed > sv_maxspeed.value)
541 temp = sv_maxspeed.value/wishspeed;
542 VectorScale (wishvel, temp, wishvel);
543 wishspeed = sv_maxspeed.value;
546 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP)
549 VectorCopy (wishvel, PRVM_serveredictvector(host_client->edict, velocity));
558 // not on ground, so little effect on velocity
559 SV_AirAccelerate (wishvel);
567 the move fields specify an intended velocity in pix/sec
568 the angle fields specify an exact angular motion in degrees
571 void SV_ClientThink (void)
573 prvm_prog_t *prog = SVVM_prog;
574 vec3_t v_angle, angles, velocity;
576 //Con_Printf("clientthink for %ims\n", (int) (sv.frametime * 1000));
578 SV_ApplyClientMove();
579 // make sure the velocity is sane (not a NaN)
580 SV_CheckVelocity(host_client->edict);
582 // LadyHavoc: QuakeC replacement for SV_ClientThink (player movement)
583 if (PRVM_serverfunction(SV_PlayerPhysics) && sv_playerphysicsqc.integer)
585 PRVM_serverglobalfloat(time) = sv.time;
586 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
587 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PlayerPhysics), "QC function SV_PlayerPhysics is missing");
588 SV_CheckVelocity(host_client->edict);
592 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NONE)
595 onground = ((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND) != 0;
599 // if dead, behave differently
600 if (PRVM_serveredictfloat(host_client->edict, health) <= 0)
603 usercmd = host_client->cmd;
606 // show 1/3 the pitch angle and all the roll angle
607 VectorAdd (PRVM_serveredictvector(host_client->edict, v_angle), PRVM_serveredictvector(host_client->edict, punchangle), v_angle);
608 VectorCopy(PRVM_serveredictvector(host_client->edict, angles), angles);
609 VectorCopy(PRVM_serveredictvector(host_client->edict, velocity), velocity);
610 PRVM_serveredictvector(host_client->edict, angles)[ROLL] = Com_CalcRoll (angles, velocity, sv_rollangle.value, sv_rollspeed.value)*4;
611 if (!PRVM_serveredictfloat(host_client->edict, fixangle))
613 PRVM_serveredictvector(host_client->edict, angles)[PITCH] = -v_angle[PITCH]/3;
614 PRVM_serveredictvector(host_client->edict, angles)[YAW] = v_angle[YAW];
617 if ( (int)PRVM_serveredictfloat(host_client->edict, flags) & FL_WATERJUMP )
620 SV_CheckVelocity(host_client->edict);
625 // Player is (somehow) outside of the map, or flying, or noclipping
626 if (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP && (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict)))
627 //if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP || PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict))
635 if ((PRVM_serveredictfloat(host_client->edict, waterlevel) >= 2) && (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP))
638 SV_CheckVelocity(host_client->edict);
643 SV_CheckVelocity(host_client->edict);
651 int sv_numreadmoves = 0;
652 usercmd_t sv_readmoves[CL_MAX_USERCMDS];
653 static void SV_ReadClientMove (void)
655 prvm_prog_t *prog = SVVM_prog;
658 usercmd_t *move = &newmove;
660 memset(move, 0, sizeof(*move));
662 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
665 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)
666 move->sequence = MSG_ReadLong(&sv_message);
667 move->time = move->clienttime = MSG_ReadFloat(&sv_message);
668 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
669 move->receivetime = (float)sv.time;
672 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);
674 // limit reported time to current time
675 // (incase the client is trying to cheat)
676 move->time = min(move->time, move->receivetime + sv.frametime);
678 // read current angles
679 for (i = 0;i < 3;i++)
681 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)
682 move->viewangles[i] = MSG_ReadAngle8i(&sv_message);
683 else if (sv.protocol == PROTOCOL_DARKPLACES1)
684 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
685 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
686 move->viewangles[i] = MSG_ReadAngle32f(&sv_message);
688 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
690 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
693 move->forwardmove = MSG_ReadCoord16i(&sv_message);
694 move->sidemove = MSG_ReadCoord16i(&sv_message);
695 move->upmove = MSG_ReadCoord16i(&sv_message);
696 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
699 // be sure to bitwise OR them into the move->buttons because we want to
700 // accumulate button presses from multiple packets per actual move
701 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)
702 move->buttons = MSG_ReadByte(&sv_message);
704 move->buttons = MSG_ReadLong(&sv_message);
705 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
708 move->impulse = MSG_ReadByte(&sv_message);
709 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
711 // PRYDON_CLIENTCURSOR
712 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)
715 move->cursor_screen[0] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
716 move->cursor_screen[1] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
717 move->cursor_start[0] = MSG_ReadFloat(&sv_message);
718 move->cursor_start[1] = MSG_ReadFloat(&sv_message);
719 move->cursor_start[2] = MSG_ReadFloat(&sv_message);
720 move->cursor_impact[0] = MSG_ReadFloat(&sv_message);
721 move->cursor_impact[1] = MSG_ReadFloat(&sv_message);
722 move->cursor_impact[2] = MSG_ReadFloat(&sv_message);
723 move->cursor_entitynumber = (unsigned short)MSG_ReadShort(&sv_message);
724 if (move->cursor_entitynumber >= prog->max_edicts)
726 Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
727 move->cursor_entitynumber = 0;
729 // as requested by FrikaC, cursor_trace_ent is reset to world if the
730 // entity is free at time of receipt
731 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
732 move->cursor_entitynumber = 0;
733 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
736 // if the previous move has not been applied yet, we need to accumulate
737 // the impulse/buttons from it
738 if (!host_client->cmd.applied)
741 move->impulse = host_client->cmd.impulse;
742 move->buttons |= host_client->cmd.buttons;
745 // now store this move for later execution
746 // (we have to buffer the moves because of old ones being repeated)
747 if (sv_numreadmoves < CL_MAX_USERCMDS)
748 sv_readmoves[sv_numreadmoves++] = *move;
750 // movement packet loss tracking
753 if(move->sequence > host_client->movement_highestsequence_seen)
755 if(host_client->movement_highestsequence_seen)
757 // mark moves in between as lost
758 unsigned int delta = move->sequence - host_client->movement_highestsequence_seen - 1;
759 if(delta < NETGRAPH_PACKETS)
762 for(u = 0; u < delta; ++u)
763 host_client->movement_count[(host_client->movement_highestsequence_seen + 1 + u) % NETGRAPH_PACKETS] = -1;
766 memset(host_client->movement_count, -1, sizeof(host_client->movement_count));
768 // mark THIS move as seen for the first time
769 host_client->movement_count[move->sequence % NETGRAPH_PACKETS] = 1;
770 // update highest sequence seen
771 host_client->movement_highestsequence_seen = move->sequence;
774 if(host_client->movement_count[move->sequence % NETGRAPH_PACKETS] >= 0)
775 ++host_client->movement_count[move->sequence % NETGRAPH_PACKETS];
779 host_client->movement_highestsequence_seen = 0;
780 memset(host_client->movement_count, 0, sizeof(host_client->movement_count));
784 static void SV_ExecuteClientMoves(void)
786 prvm_prog_t *prog = SVVM_prog;
790 double oldframetime2;
791 #ifdef NUM_PING_TIMES
794 if (sv_numreadmoves < 1)
796 // only start accepting input once the player is spawned
797 if (!host_client->begun)
800 Con_Printf("SV_ExecuteClientMoves: read %i moves at sv.time %f\n", sv_numreadmoves, (float)sv.time);
802 // disable clientside movement prediction in some cases
803 if (ceil(max(sv_readmoves[sv_numreadmoves-1].receivetime - sv_readmoves[sv_numreadmoves-1].time, 0) * 1000.0) < sv_clmovement_minping.integer)
804 host_client->clmovement_disabletimeout = host.realtime + sv_clmovement_minping_disabletime.value / 1000.0;
805 // several conditions govern whether clientside movement prediction is allowed
806 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)))))
808 // process the moves in order and ignore old ones
809 // but always trust the latest move
810 // (this deals with bogus initial move sequences after level change,
811 // where the client will eventually catch up with the level change
812 // and reset its move sequence)
813 for (moveindex = 0;moveindex < sv_numreadmoves;moveindex++)
815 usercmd_t *move = sv_readmoves + moveindex;
816 if (host_client->movesequence < move->sequence || moveindex == sv_numreadmoves - 1)
819 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);
821 // this is a new move
822 move->time = bound(sv.time - 1, move->time, sv.time); // prevent slowhack/speedhack combos
823 move->time = max(move->time, host_client->cmd.time); // prevent backstepping of time
824 moveframetime = bound(0, move->time - host_client->cmd.time, min(0.1, sv_clmovement_inputtimeout.value));
826 // discard (treat like lost) moves with too low distance from
827 // the previous one to prevent hacks using float inaccuracy
828 // clients will see this as packet loss in the netgraph
829 // this should also apply if a move cannot get
830 // executed because it came too late and
831 // already was performed serverside
832 if(moveframetime < 0.0005)
834 // count the move as LOST if we don't
835 // execute it but it has higher
837 if(host_client->movesequence)
838 if(move->sequence > host_client->movesequence)
839 host_client->movement_count[(move->sequence) % NETGRAPH_PACKETS] = -1;
843 //Con_Printf("movesequence = %i (%i lost), moveframetime = %f\n", move->sequence, move->sequence ? move->sequence - host_client->movesequence - 1 : 0, moveframetime);
844 host_client->cmd = *move;
845 host_client->movesequence = move->sequence;
847 // if using prediction, we need to perform moves when packets are
848 // received, even if multiple occur in one frame
849 // (they can't go beyond the current time so there is no cheat issue
850 // with this approach, and if they don't send input for a while they
851 // start moving anyway, so the longest 'lagaport' possible is
852 // determined by the sv_clmovement_inputtimeout cvar)
853 if (moveframetime <= 0)
855 oldframetime = PRVM_serverglobalfloat(frametime);
856 oldframetime2 = sv.frametime;
857 // update ping time for qc to see while executing this move
858 host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
859 // the server and qc frametime values must be changed temporarily
860 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime;
861 // if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
862 if (sv.frametime > 0.05)
864 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime * 0.5f;
865 SV_Physics_ClientMove();
867 SV_Physics_ClientMove();
868 sv.frametime = oldframetime2;
869 PRVM_serverglobalfloat(frametime) = oldframetime;
870 host_client->clmovement_inputtimeout = sv_clmovement_inputtimeout.value;
876 // try to gather button bits from old moves, but only if their time is
877 // advancing (ones with the same timestamp can't be trusted)
878 for (moveindex = 0;moveindex < sv_numreadmoves-1;moveindex++)
880 usercmd_t *move = sv_readmoves + moveindex;
881 if (host_client->cmd.time < move->time)
883 sv_readmoves[sv_numreadmoves-1].buttons |= move->buttons;
885 sv_readmoves[sv_numreadmoves-1].impulse = move->impulse;
888 // now copy the new move
889 host_client->cmd = sv_readmoves[sv_numreadmoves-1];
890 host_client->cmd.time = max(host_client->cmd.time, sv.time);
891 // physics will run up to sv.time, so allow no predicted moves
892 // before that otherwise, there is a speedhack by turning
893 // prediction on and off repeatedly on client side because the
894 // engine would run BOTH client and server physics for the same
896 host_client->movesequence = 0;
897 // make sure that normal physics takes over immediately
898 host_client->clmovement_inputtimeout = 0;
901 // calculate average ping time
902 host_client->ping = host_client->cmd.receivetime - host_client->cmd.clienttime;
903 #ifdef NUM_PING_TIMES
904 host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = host_client->cmd.receivetime - host_client->cmd.clienttime;
905 host_client->num_pings++;
906 for (i=0, total = 0;i < NUM_PING_TIMES;i++)
907 total += host_client->ping_times[i];
908 host_client->ping = total / NUM_PING_TIMES;
912 void SV_ApplyClientMove (void)
914 prvm_prog_t *prog = SVVM_prog;
915 usercmd_t *move = &host_client->cmd;
916 int j, movementloss, packetloss;
918 if (!move->receivetime)
921 // note: a move can be applied multiple times if the client packets are
922 // not coming as often as the physics is executed, and the move must be
923 // applied before running qc each time because the id1 qc had a bug where
924 // it clears self.button2 in PlayerJump, causing pogostick behavior if
925 // moves are not applied every time before calling qc
926 move->applied = true;
928 // set the edict fields
929 PRVM_serveredictfloat(host_client->edict, button0) = move->buttons & 1;
930 PRVM_serveredictfloat(host_client->edict, button2) = (move->buttons & 2)>>1;
932 PRVM_serveredictfloat(host_client->edict, impulse) = move->impulse;
933 // only send the impulse to qc once
936 movementloss = packetloss = 0;
937 if(host_client->netconnection)
939 for (j = 0;j < NETGRAPH_PACKETS;j++)
940 if (host_client->netconnection->incoming_netgraph[j].unreliablebytes == NETGRAPH_LOSTPACKET)
942 for (j = 0;j < NETGRAPH_PACKETS;j++)
943 if (host_client->movement_count[j] < 0)
947 VectorCopy(move->viewangles, PRVM_serveredictvector(host_client->edict, v_angle));
948 PRVM_serveredictfloat(host_client->edict, button3) = ((move->buttons >> 2) & 1);
949 PRVM_serveredictfloat(host_client->edict, button4) = ((move->buttons >> 3) & 1);
950 PRVM_serveredictfloat(host_client->edict, button5) = ((move->buttons >> 4) & 1);
951 PRVM_serveredictfloat(host_client->edict, button6) = ((move->buttons >> 5) & 1);
952 PRVM_serveredictfloat(host_client->edict, button7) = ((move->buttons >> 6) & 1);
953 PRVM_serveredictfloat(host_client->edict, button8) = ((move->buttons >> 7) & 1);
954 PRVM_serveredictfloat(host_client->edict, button9) = ((move->buttons >> 11) & 1);
955 PRVM_serveredictfloat(host_client->edict, button10) = ((move->buttons >> 12) & 1);
956 PRVM_serveredictfloat(host_client->edict, button11) = ((move->buttons >> 13) & 1);
957 PRVM_serveredictfloat(host_client->edict, button12) = ((move->buttons >> 14) & 1);
958 PRVM_serveredictfloat(host_client->edict, button13) = ((move->buttons >> 15) & 1);
959 PRVM_serveredictfloat(host_client->edict, button14) = ((move->buttons >> 16) & 1);
960 PRVM_serveredictfloat(host_client->edict, button15) = ((move->buttons >> 17) & 1);
961 PRVM_serveredictfloat(host_client->edict, button16) = ((move->buttons >> 18) & 1);
962 PRVM_serveredictfloat(host_client->edict, buttonuse) = ((move->buttons >> 8) & 1);
963 PRVM_serveredictfloat(host_client->edict, buttonchat) = ((move->buttons >> 9) & 1);
964 PRVM_serveredictfloat(host_client->edict, cursor_active) = ((move->buttons >> 10) & 1);
965 VectorSet(PRVM_serveredictvector(host_client->edict, movement), move->forwardmove, move->sidemove, move->upmove);
966 VectorCopy(move->cursor_screen, PRVM_serveredictvector(host_client->edict, cursor_screen));
967 VectorCopy(move->cursor_start, PRVM_serveredictvector(host_client->edict, cursor_trace_start));
968 VectorCopy(move->cursor_impact, PRVM_serveredictvector(host_client->edict, cursor_trace_endpos));
969 PRVM_serveredictedict(host_client->edict, cursor_trace_ent) = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
970 PRVM_serveredictfloat(host_client->edict, ping) = host_client->ping * 1000.0;
971 PRVM_serveredictfloat(host_client->edict, ping_packetloss) = packetloss / (float) NETGRAPH_PACKETS;
972 PRVM_serveredictfloat(host_client->edict, ping_movementloss) = movementloss / (float) NETGRAPH_PACKETS;
975 static qbool SV_FrameLost(int framenum)
977 if (host_client->entitydatabase5)
979 if (framenum <= host_client->entitydatabase5->latestframenum)
981 EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
982 EntityFrameCSQC_LostFrame(host_client, framenum);
989 static void SV_FrameAck(int framenum)
991 if (host_client->entitydatabase)
992 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
993 else if (host_client->entitydatabase4)
994 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
995 else if (host_client->entitydatabase5)
996 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
1001 SV_ReadClientMessage
1004 void SV_ReadClientMessage(void)
1006 prvm_prog_t *prog = SVVM_prog;
1007 int netcmd, num, start;
1010 if(sv_autodemo_perclient.integer >= 2)
1011 SV_WriteDemoMessage(host_client, &(host_client->netconnection->message), true);
1013 //MSG_BeginReading ();
1014 sv_numreadmoves = 0;
1018 if (!host_client->active)
1020 // a command caused an error
1021 SV_DropClient (false);
1025 if (sv_message.badread)
1027 Con_Print("SV_ReadClientMessage: badread\n");
1028 SV_DropClient (false);
1032 netcmd = MSG_ReadByte(&sv_message);
1036 // apply the moves that were read this frame
1037 SV_ExecuteClientMoves();
1044 Con_Printf("SV_ReadClientMessage: unknown command char %i (at offset 0x%x)\n", netcmd, sv_message.readcount);
1045 if (developer_networking.integer)
1046 Com_HexDumpToConsole(sv_message.data, sv_message.cursize);
1047 SV_DropClient (false);
1054 // allow reliable messages now as the client is done with initial loading
1055 if (host_client->sendsignon == 2)
1056 host_client->sendsignon = 0;
1057 s = MSG_ReadString(&sv_message, sv_readstring, sizeof(sv_readstring));
1059 for(p = s; *p; ++p) switch(*p)
1068 goto clc_stringcmd_invalid; // newline seen, THEN something else -> possible exploit
1073 if (strncasecmp(s, "spawn", 5) == 0
1074 || strncasecmp(s, "begin", 5) == 0
1075 || strncasecmp(s, "prespawn", 8) == 0)
1076 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
1077 else if (PRVM_serverfunction(SV_ParseClientCommand))
1079 int restorevm_tempstringsbuf_cursize;
1080 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
1081 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, s);
1082 PRVM_serverglobalfloat(time) = sv.time;
1083 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
1084 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_ParseClientCommand), "QC function SV_ParseClientCommand is missing");
1085 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1088 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
1091 clc_stringcmd_invalid:
1092 Con_Printf("Received invalid stringcmd from %s\n", host_client->name);
1093 if(developer.integer > 0)
1094 Com_HexDumpToConsole((unsigned char *) s, (int)strlen(s));
1097 case clc_disconnect:
1098 SV_DropClient (false); // client wants to disconnect
1102 SV_ReadClientMove();
1105 case clc_ackdownloaddata:
1106 start = MSG_ReadLong(&sv_message);
1107 num = MSG_ReadShort(&sv_message);
1108 if (host_client->download_file && host_client->download_started)
1110 if (host_client->download_expectedposition == start)
1112 int size = (int)FS_FileSize(host_client->download_file);
1113 // a data block was successfully received by the client,
1114 // update the expected position on the next data block
1115 host_client->download_expectedposition = start + num;
1116 // if this was the last data block of the file, it's done
1117 if (host_client->download_expectedposition >= FS_FileSize(host_client->download_file))
1119 // tell the client that the download finished
1120 // we need to calculate the crc now
1122 // note: at this point the OS probably has the file
1123 // entirely in memory, so this is a faster operation
1124 // now than it was when the download started.
1126 // it is also preferable to do this at the end of the
1127 // download rather than the start because it reduces
1128 // potential for Denial Of Service attacks against the
1131 unsigned char *temp;
1132 FS_Seek(host_client->download_file, 0, SEEK_SET);
1133 temp = (unsigned char *) Mem_Alloc(tempmempool, size);
1134 FS_Read(host_client->download_file, temp, size);
1135 crc = CRC_Block(temp, size);
1137 // calculated crc, send the file info to the client
1138 // (so that it can verify the data)
1139 SV_ClientCommands("\ncl_downloadfinished %i %i %s\n", size, crc, host_client->download_name);
1140 Con_DPrintf("Download of %s by %s has finished\n", host_client->download_name, host_client->name);
1141 FS_Close(host_client->download_file);
1142 host_client->download_file = NULL;
1143 host_client->download_name[0] = 0;
1144 host_client->download_expectedposition = 0;
1145 host_client->download_started = false;
1150 // a data block was lost, reset to the expected position
1151 // and resume sending from there
1152 FS_Seek(host_client->download_file, host_client->download_expectedposition, SEEK_SET);
1158 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
1159 num = MSG_ReadLong(&sv_message);
1160 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
1161 if (developer_networkentities.integer >= 10)
1162 Con_Printf("recv clc_ackframe %i\n", num);
1163 // if the client hasn't progressed through signons yet,
1164 // ignore any clc_ackframes we get (they're probably from the
1166 if (host_client->begun && host_client->latestframenum < num)
1169 for (i = host_client->latestframenum + 1;i < num;i++)
1170 if (!SV_FrameLost(i))
1173 host_client->latestframenum = num;