]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sv_user.c
Make V_CalcRoll a common function. Add sv_rollangle and sv_rollspeed cvars.
[xonotic/darkplaces.git] / sv_user.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // sv_user.c -- server code for moving users
21
22 #include "quakedef.h"
23 #include "sv_demo.h"
24 #define DEBUGMOVES 0
25
26 static usercmd_t usercmd;
27 extern cvar_t sv_autodemo_perclient;
28 extern cvar_t sv_rollangle;
29 extern cvar_t sv_rollspeed;
30
31 /*
32 ==================
33 SV_PreSpawn_f
34 ==================
35 */
36 void SV_PreSpawn_f(cmd_state_t *cmd)
37 {
38         if (host_client->prespawned)
39         {
40                 Con_Print("prespawn not valid -- already prespawned\n");
41                 return;
42         }
43         host_client->prespawned = true;
44
45         if (host_client->netconnection)
46         {
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
51         }
52
53         // reset the name change timer because the client will send name soon
54         host_client->nametime = 0;
55 }
56
57 /*
58 ==================
59 SV_Spawn_f
60 ==================
61 */
62 void SV_Spawn_f(cmd_state_t *cmd)
63 {
64         prvm_prog_t *prog = SVVM_prog;
65         int i;
66         client_t *client;
67         int stats[MAX_CL_STATS];
68
69         if (!host_client->prespawned)
70         {
71                 Con_Print("Spawn not valid -- not yet prespawned\n");
72                 return;
73         }
74         if (host_client->spawned)
75         {
76                 Con_Print("Spawn not valid -- already spawned\n");
77                 return;
78         }
79         host_client->spawned = true;
80
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;
84
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);
89
90         // run the entrance script
91         if (sv.loadgame)
92         {
93                 // loaded games are fully initialized already
94                 if (PRVM_serverfunction(RestoreGame))
95                 {
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");
100                 }
101         }
102         else
103         {
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);
105
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];
109
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");
115
116                 if (cls.state == ca_dedicated)
117                         Con_Printf("%s connected\n", host_client->name);
118
119                 PRVM_serverglobalfloat(time) = sv.time;
120                 prog->ExecuteProgram(prog, PRVM_serverfunction(PutClientInServer), "QC function PutClientInServer is missing");
121         }
122
123         if (!host_client->netconnection)
124                 return;
125
126         // send time of update
127         MSG_WriteByte (&host_client->netconnection->message, svc_time);
128         MSG_WriteFloat (&host_client->netconnection->message, sv.time);
129
130         // send all current names, colors, and frag counts
131         for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
132         {
133                 if (!client->active)
134                         continue;
135                 MSG_WriteByte (&host_client->netconnection->message, svc_updatename);
136                 MSG_WriteByte (&host_client->netconnection->message, i);
137                 MSG_WriteString (&host_client->netconnection->message, client->name);
138                 MSG_WriteByte (&host_client->netconnection->message, svc_updatefrags);
139                 MSG_WriteByte (&host_client->netconnection->message, i);
140                 MSG_WriteShort (&host_client->netconnection->message, client->frags);
141                 MSG_WriteByte (&host_client->netconnection->message, svc_updatecolors);
142                 MSG_WriteByte (&host_client->netconnection->message, i);
143                 MSG_WriteByte (&host_client->netconnection->message, client->colors);
144         }
145
146         // send all current light styles
147         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
148         {
149                 if (sv.lightstyles[i][0])
150                 {
151                         MSG_WriteByte (&host_client->netconnection->message, svc_lightstyle);
152                         MSG_WriteByte (&host_client->netconnection->message, (char)i);
153                         MSG_WriteString (&host_client->netconnection->message, sv.lightstyles[i]);
154                 }
155         }
156
157         // send some stats
158         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
159         MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALSECRETS);
160         MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(total_secrets));
161
162         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
163         MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALMONSTERS);
164         MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(total_monsters));
165
166         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
167         MSG_WriteByte (&host_client->netconnection->message, STAT_SECRETS);
168         MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(found_secrets));
169
170         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
171         MSG_WriteByte (&host_client->netconnection->message, STAT_MONSTERS);
172         MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(killed_monsters));
173
174         // send a fixangle
175         // Never send a roll angle, because savegames can catch the server
176         // in a state where it is expecting the client to correct the angle
177         // and it won't happen if the game was just loaded, so you wind up
178         // with a permanent head tilt
179         if (sv.loadgame)
180         {
181                 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
182                 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, v_angle)[0], sv.protocol);
183                 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, v_angle)[1], sv.protocol);
184                 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
185         }
186         else
187         {
188                 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
189                 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, angles)[0], sv.protocol);
190                 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, angles)[1], sv.protocol);
191                 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
192         }
193
194         SV_WriteClientdataToMessage (host_client, host_client->edict, &host_client->netconnection->message, stats);
195
196         MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
197         MSG_WriteByte (&host_client->netconnection->message, 3);
198 }
199
200 /*
201 ==================
202 SV_Begin_f
203 ==================
204 */
205 void SV_Begin_f(cmd_state_t *cmd)
206 {
207         if (!host_client->spawned)
208         {
209                 Con_Print("Begin not valid -- not yet spawned\n");
210                 return;
211         }
212         if (host_client->begun)
213         {
214                 Con_Print("Begin not valid -- already begun\n");
215                 return;
216         }
217         host_client->begun = true;
218
219         // LadyHavoc: note: this code also exists in SV_DropClient
220         if (sv.loadgame)
221         {
222                 int i;
223                 for (i = 0;i < svs.maxclients;i++)
224                         if (svs.clients[i].active && !svs.clients[i].spawned)
225                                 break;
226                 if (i == svs.maxclients)
227                 {
228                         Con_Printf("Loaded game, everyone rejoined - unpausing\n");
229                         sv.paused = sv.loadgame = false; // we're basically done with loading now
230                 }
231         }
232 }
233
234 /*
235 ===============
236 SV_SetIdealPitch
237 ===============
238 */
239 #define MAX_FORWARD     6
240 void SV_SetIdealPitch (void)
241 {
242         prvm_prog_t *prog = SVVM_prog;
243         float   angleval, sinval, cosval, step, dir;
244         trace_t tr;
245         vec3_t  top, bottom;
246         float   z[MAX_FORWARD];
247         int             i, j;
248         int             steps;
249
250         if (!((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND))
251                 return;
252
253         angleval = PRVM_serveredictvector(host_client->edict, angles)[YAW] * M_PI*2 / 360;
254         sinval = sin(angleval);
255         cosval = cos(angleval);
256
257         for (i=0 ; i<MAX_FORWARD ; i++)
258         {
259                 top[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + cosval*(i+3)*12;
260                 top[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + sinval*(i+3)*12;
261                 top[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, view_ofs)[2];
262
263                 bottom[0] = top[0];
264                 bottom[1] = top[1];
265                 bottom[2] = top[2] - 160;
266
267                 tr = SV_TraceLine(top, bottom, MOVE_NOMONSTERS, host_client->edict, SUPERCONTENTS_SOLID, 0, 0, collision_extendmovelength.value);
268                 // if looking at a wall, leave ideal the way is was
269                 if (tr.startsolid)
270                         return;
271
272                 // near a dropoff
273                 if (tr.fraction == 1)
274                         return;
275
276                 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
277         }
278
279         dir = 0;
280         steps = 0;
281         for (j=1 ; j<i ; j++)
282         {
283                 step = z[j] - z[j-1];
284                 if (step > -ON_EPSILON && step < ON_EPSILON)
285                         continue;
286
287                 // mixed changes
288                 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
289                         return;
290
291                 steps++;
292                 dir = step;
293         }
294
295         if (!dir)
296         {
297                 PRVM_serveredictfloat(host_client->edict, idealpitch) = 0;
298                 return;
299         }
300
301         if (steps < 2)
302                 return;
303         PRVM_serveredictfloat(host_client->edict, idealpitch) = -dir * sv_idealpitchscale.value;
304 }
305
306 static vec3_t wishdir, forward, right, up;
307 static float wishspeed;
308
309 static qboolean onground;
310
311 /*
312 ==================
313 SV_UserFriction
314
315 ==================
316 */
317 static void SV_UserFriction (void)
318 {
319         prvm_prog_t *prog = SVVM_prog;
320         float speed, newspeed, control, friction;
321         vec3_t start, stop;
322         trace_t trace;
323
324         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]);
325         if (!speed)
326                 return;
327
328         // if the leading edge is over a dropoff, increase friction
329         start[0] = stop[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + PRVM_serveredictvector(host_client->edict, velocity)[0]/speed*16;
330         start[1] = stop[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + PRVM_serveredictvector(host_client->edict, velocity)[1]/speed*16;
331         start[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, mins)[2];
332         stop[2] = start[2] - 34;
333
334         trace = SV_TraceLine(start, stop, MOVE_NOMONSTERS, host_client->edict, SV_GenericHitSuperContentsMask(host_client->edict), 0, 0, collision_extendmovelength.value);
335
336         if (trace.fraction == 1.0)
337                 friction = sv_friction.value*sv_edgefriction.value;
338         else
339                 friction = sv_friction.value;
340
341         // apply friction
342         control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
343         newspeed = speed - sv.frametime*control*friction;
344
345         if (newspeed < 0)
346                 newspeed = 0;
347         else
348                 newspeed /= speed;
349
350         VectorScale(PRVM_serveredictvector(host_client->edict, velocity), newspeed, PRVM_serveredictvector(host_client->edict, velocity));
351 }
352
353 /*
354 ==============
355 SV_Accelerate
356 ==============
357 */
358 static void SV_Accelerate (void)
359 {
360         prvm_prog_t *prog = SVVM_prog;
361         int i;
362         float addspeed, accelspeed, currentspeed;
363
364         currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishdir);
365         addspeed = wishspeed - currentspeed;
366         if (addspeed <= 0)
367                 return;
368         accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
369         if (accelspeed > addspeed)
370                 accelspeed = addspeed;
371
372         for (i=0 ; i<3 ; i++)
373                 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishdir[i];
374 }
375
376 extern cvar_t sv_gameplayfix_q2airaccelerate;
377 static void SV_AirAccelerate (vec3_t wishveloc)
378 {
379         prvm_prog_t *prog = SVVM_prog;
380         int i;
381         float addspeed, wishspd, accelspeed, currentspeed;
382
383         wishspd = VectorNormalizeLength (wishveloc);
384         if (wishspd > sv_maxairspeed.value)
385                 wishspd = sv_maxairspeed.value;
386         currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishveloc);
387         addspeed = wishspd - currentspeed;
388         if (addspeed <= 0)
389                 return;
390         accelspeed = (sv_airaccelerate.value < 0 ? sv_accelerate.value : sv_airaccelerate.value)*(sv_gameplayfix_q2airaccelerate.integer ? wishspd : wishspeed) * sv.frametime;
391         if (accelspeed > addspeed)
392                 accelspeed = addspeed;
393
394         for (i=0 ; i<3 ; i++)
395                 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishveloc[i];
396 }
397
398
399 static void DropPunchAngle (void)
400 {
401         prvm_prog_t *prog = SVVM_prog;
402         vec_t len;
403         vec3_t punchangle, punchvector;
404
405         VectorCopy(PRVM_serveredictvector(host_client->edict, punchangle), punchangle);
406         VectorCopy(PRVM_serveredictvector(host_client->edict, punchvector), punchvector);
407
408         len = VectorNormalizeLength(punchangle);
409         if (len > 0)
410         {
411                 len -= 10*sv.frametime;
412                 if (len < 0)
413                         len = 0;
414                 VectorScale(punchangle, len, punchangle);
415         }
416
417         len = VectorNormalizeLength(punchvector);
418         if (len > 0)
419         {
420                 len -= 20*sv.frametime;
421                 if (len < 0)
422                         len = 0;
423                 VectorScale(punchvector, len, punchvector);
424         }
425
426         VectorCopy(punchangle, PRVM_serveredictvector(host_client->edict, punchangle));
427         VectorCopy(punchvector, PRVM_serveredictvector(host_client->edict, punchvector));
428 }
429
430 /*
431 ===================
432 SV_WaterMove
433
434 ===================
435 */
436 static void SV_WaterMove (void)
437 {
438         prvm_prog_t *prog = SVVM_prog;
439         int i;
440         vec3_t wishvel, v_angle;
441         vec_t speed, newspeed, fwishspeed, addspeed, accelspeed, temp;
442
443         // user intentions
444         VectorCopy(PRVM_serveredictvector(host_client->edict, v_angle), v_angle);
445         AngleVectors(v_angle, forward, right, up);
446
447         for (i=0 ; i<3 ; i++)
448                 wishvel[i] = forward[i]*usercmd.forwardmove + right[i]*usercmd.sidemove;
449
450         if (!usercmd.forwardmove && !usercmd.sidemove && !usercmd.upmove)
451                 wishvel[2] -= 60;               // drift towards bottom
452         else
453                 wishvel[2] += usercmd.upmove;
454
455         fwishspeed = VectorLength(wishvel);
456         if (fwishspeed > sv_maxspeed.value)
457         {
458                 temp = sv_maxspeed.value/fwishspeed;
459                 VectorScale (wishvel, temp, wishvel);
460                 fwishspeed = sv_maxspeed.value;
461         }
462         fwishspeed *= 0.7;
463
464         // water friction
465         speed = VectorLength(PRVM_serveredictvector(host_client->edict, velocity));
466         if (speed)
467         {
468                 newspeed = speed - sv.frametime * speed * (sv_waterfriction.value < 0 ? sv_friction.value : sv_waterfriction.value);
469                 if (newspeed < 0)
470                         newspeed = 0;
471                 temp = newspeed/speed;
472                 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), temp, PRVM_serveredictvector(host_client->edict, velocity));
473         }
474         else
475                 newspeed = 0;
476
477         // water acceleration
478         if (!fwishspeed)
479                 return;
480
481         addspeed = fwishspeed - newspeed;
482         if (addspeed <= 0)
483                 return;
484
485         VectorNormalize (wishvel);
486         accelspeed = (sv_wateraccelerate.value < 0 ? sv_accelerate.value : sv_wateraccelerate.value) * fwishspeed * sv.frametime;
487         if (accelspeed > addspeed)
488                 accelspeed = addspeed;
489
490         for (i=0 ; i<3 ; i++)
491                 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed * wishvel[i];
492 }
493
494 static void SV_WaterJump (void)
495 {
496         prvm_prog_t *prog = SVVM_prog;
497         if (sv.time > PRVM_serveredictfloat(host_client->edict, teleport_time) || !PRVM_serveredictfloat(host_client->edict, waterlevel))
498         {
499                 PRVM_serveredictfloat(host_client->edict, flags) = (int)PRVM_serveredictfloat(host_client->edict, flags) & ~FL_WATERJUMP;
500                 PRVM_serveredictfloat(host_client->edict, teleport_time) = 0;
501         }
502         PRVM_serveredictvector(host_client->edict, velocity)[0] = PRVM_serveredictvector(host_client->edict, movedir)[0];
503         PRVM_serveredictvector(host_client->edict, velocity)[1] = PRVM_serveredictvector(host_client->edict, movedir)[1];
504 }
505
506
507 /*
508 ===================
509 SV_AirMove
510
511 ===================
512 */
513 static void SV_AirMove (void)
514 {
515         prvm_prog_t *prog = SVVM_prog;
516         int i;
517         vec3_t wishvel;
518         float fmove, smove, temp;
519
520         // LadyHavoc: correct quake movement speed bug when looking up/down
521         wishvel[0] = wishvel[2] = 0;
522         wishvel[1] = PRVM_serveredictvector(host_client->edict, angles)[1];
523         AngleVectors (wishvel, forward, right, up);
524
525         fmove = usercmd.forwardmove;
526         smove = usercmd.sidemove;
527
528 // hack to not let you back into teleporter
529         if (sv.time < PRVM_serveredictfloat(host_client->edict, teleport_time) && fmove < 0)
530                 fmove = 0;
531
532         for (i=0 ; i<3 ; i++)
533                 wishvel[i] = forward[i]*fmove + right[i]*smove;
534
535         if ((int)PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_WALK)
536                 wishvel[2] += usercmd.upmove;
537
538         VectorCopy (wishvel, wishdir);
539         wishspeed = VectorNormalizeLength(wishdir);
540         if (wishspeed > sv_maxspeed.value)
541         {
542                 temp = sv_maxspeed.value/wishspeed;
543                 VectorScale (wishvel, temp, wishvel);
544                 wishspeed = sv_maxspeed.value;
545         }
546
547         if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP)
548         {
549                 // noclip
550                 VectorCopy (wishvel, PRVM_serveredictvector(host_client->edict, velocity));
551         }
552         else if (onground)
553         {
554                 SV_UserFriction ();
555                 SV_Accelerate ();
556         }
557         else
558         {
559                 // not on ground, so little effect on velocity
560                 SV_AirAccelerate (wishvel);
561         }
562 }
563
564 /*
565 ===================
566 SV_ClientThink
567
568 the move fields specify an intended velocity in pix/sec
569 the angle fields specify an exact angular motion in degrees
570 ===================
571 */
572 void SV_ClientThink (void)
573 {
574         prvm_prog_t *prog = SVVM_prog;
575         vec3_t v_angle, angles, velocity;
576
577         //Con_Printf("clientthink for %ims\n", (int) (sv.frametime * 1000));
578
579         SV_ApplyClientMove();
580         // make sure the velocity is sane (not a NaN)
581         SV_CheckVelocity(host_client->edict);
582
583         // LadyHavoc: QuakeC replacement for SV_ClientThink (player movement)
584         if (PRVM_serverfunction(SV_PlayerPhysics) && sv_playerphysicsqc.integer)
585         {
586                 PRVM_serverglobalfloat(time) = sv.time;
587                 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
588                 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PlayerPhysics), "QC function SV_PlayerPhysics is missing");
589                 SV_CheckVelocity(host_client->edict);
590                 return;
591         }
592
593         if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NONE)
594                 return;
595
596         onground = ((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND) != 0;
597
598         DropPunchAngle ();
599
600         // if dead, behave differently
601         if (PRVM_serveredictfloat(host_client->edict, health) <= 0)
602                 return;
603
604         usercmd = host_client->cmd;
605
606         // angles
607         // show 1/3 the pitch angle and all the roll angle
608         VectorAdd (PRVM_serveredictvector(host_client->edict, v_angle), PRVM_serveredictvector(host_client->edict, punchangle), v_angle);
609         VectorCopy(PRVM_serveredictvector(host_client->edict, angles), angles);
610         VectorCopy(PRVM_serveredictvector(host_client->edict, velocity), velocity);
611         PRVM_serveredictvector(host_client->edict, angles)[ROLL] = Com_CalcRoll (angles, velocity, sv_rollangle.value, sv_rollspeed.value)*4;
612         if (!PRVM_serveredictfloat(host_client->edict, fixangle))
613         {
614                 PRVM_serveredictvector(host_client->edict, angles)[PITCH] = -v_angle[PITCH]/3;
615                 PRVM_serveredictvector(host_client->edict, angles)[YAW] = v_angle[YAW];
616         }
617
618         if ( (int)PRVM_serveredictfloat(host_client->edict, flags) & FL_WATERJUMP )
619         {
620                 SV_WaterJump ();
621                 SV_CheckVelocity(host_client->edict);
622                 return;
623         }
624
625         /*
626         // Player is (somehow) outside of the map, or flying, or noclipping
627         if (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP && (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict)))
628         //if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP || PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict))
629         {
630                 SV_FreeMove ();
631                 return;
632         }
633         */
634
635         // walk
636         if ((PRVM_serveredictfloat(host_client->edict, waterlevel) >= 2) && (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP))
637         {
638                 SV_WaterMove ();
639                 SV_CheckVelocity(host_client->edict);
640                 return;
641         }
642
643         SV_AirMove ();
644         SV_CheckVelocity(host_client->edict);
645 }
646
647 /*
648 ===================
649 SV_ReadClientMove
650 ===================
651 */
652 int sv_numreadmoves = 0;
653 usercmd_t sv_readmoves[CL_MAX_USERCMDS];
654 static void SV_ReadClientMove (void)
655 {
656         prvm_prog_t *prog = SVVM_prog;
657         int i;
658         usercmd_t newmove;
659         usercmd_t *move = &newmove;
660
661         memset(move, 0, sizeof(*move));
662
663         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
664
665         // read ping time
666         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)
667                 move->sequence = MSG_ReadLong(&sv_message);
668         move->time = move->clienttime = MSG_ReadFloat(&sv_message);
669         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
670         move->receivetime = (float)sv.time;
671
672 #if DEBUGMOVES
673         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 #endif
675         // limit reported time to current time
676         // (incase the client is trying to cheat)
677         move->time = min(move->time, move->receivetime + sv.frametime);
678
679         // read current angles
680         for (i = 0;i < 3;i++)
681         {
682                 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)
683                         move->viewangles[i] = MSG_ReadAngle8i(&sv_message);
684                 else if (sv.protocol == PROTOCOL_DARKPLACES1)
685                         move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
686                 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
687                         move->viewangles[i] = MSG_ReadAngle32f(&sv_message);
688                 else
689                         move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
690         }
691         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
692
693         // read movement
694         move->forwardmove = MSG_ReadCoord16i(&sv_message);
695         move->sidemove = MSG_ReadCoord16i(&sv_message);
696         move->upmove = MSG_ReadCoord16i(&sv_message);
697         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
698
699         // read buttons
700         // be sure to bitwise OR them into the move->buttons because we want to
701         // accumulate button presses from multiple packets per actual move
702         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)
703                 move->buttons = MSG_ReadByte(&sv_message);
704         else
705                 move->buttons = MSG_ReadLong(&sv_message);
706         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
707
708         // read impulse
709         move->impulse = MSG_ReadByte(&sv_message);
710         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
711
712         // PRYDON_CLIENTCURSOR
713         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)
714         {
715                 // 30 bytes
716                 move->cursor_screen[0] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
717                 move->cursor_screen[1] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
718                 move->cursor_start[0] = MSG_ReadFloat(&sv_message);
719                 move->cursor_start[1] = MSG_ReadFloat(&sv_message);
720                 move->cursor_start[2] = MSG_ReadFloat(&sv_message);
721                 move->cursor_impact[0] = MSG_ReadFloat(&sv_message);
722                 move->cursor_impact[1] = MSG_ReadFloat(&sv_message);
723                 move->cursor_impact[2] = MSG_ReadFloat(&sv_message);
724                 move->cursor_entitynumber = (unsigned short)MSG_ReadShort(&sv_message);
725                 if (move->cursor_entitynumber >= prog->max_edicts)
726                 {
727                         Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
728                         move->cursor_entitynumber = 0;
729                 }
730                 // as requested by FrikaC, cursor_trace_ent is reset to world if the
731                 // entity is free at time of receipt
732                 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
733                         move->cursor_entitynumber = 0;
734                 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
735         }
736
737         // if the previous move has not been applied yet, we need to accumulate
738         // the impulse/buttons from it
739         if (!host_client->cmd.applied)
740         {
741                 if (!move->impulse)
742                         move->impulse = host_client->cmd.impulse;
743                 move->buttons |= host_client->cmd.buttons;
744         }
745
746         // now store this move for later execution
747         // (we have to buffer the moves because of old ones being repeated)
748         if (sv_numreadmoves < CL_MAX_USERCMDS)
749                 sv_readmoves[sv_numreadmoves++] = *move;
750
751         // movement packet loss tracking
752         if(move->sequence)
753         {
754                 if(move->sequence > host_client->movement_highestsequence_seen)
755                 {
756                         if(host_client->movement_highestsequence_seen)
757                         {
758                                 // mark moves in between as lost
759                                 unsigned int delta = move->sequence - host_client->movement_highestsequence_seen - 1;
760                                 if(delta < NETGRAPH_PACKETS)
761                                 {
762                                         unsigned int u;
763                                         for(u = 0; u < delta; ++u)
764                                                 host_client->movement_count[(host_client->movement_highestsequence_seen + 1 + u) % NETGRAPH_PACKETS] = -1;
765                                 }
766                                 else
767                                         memset(host_client->movement_count, -1, sizeof(host_client->movement_count));
768                         }
769                         // mark THIS move as seen for the first time
770                         host_client->movement_count[move->sequence % NETGRAPH_PACKETS] = 1;
771                         // update highest sequence seen
772                         host_client->movement_highestsequence_seen = move->sequence;
773                 }
774                 else
775                         if(host_client->movement_count[move->sequence % NETGRAPH_PACKETS] >= 0)
776                                 ++host_client->movement_count[move->sequence % NETGRAPH_PACKETS];
777         }
778         else
779         {
780                 host_client->movement_highestsequence_seen = 0;
781                 memset(host_client->movement_count, 0, sizeof(host_client->movement_count));
782         }
783 }
784
785 static void SV_ExecuteClientMoves(void)
786 {
787         prvm_prog_t *prog = SVVM_prog;
788         int moveindex;
789         float moveframetime;
790         double oldframetime;
791         double oldframetime2;
792 #ifdef NUM_PING_TIMES
793         double total;
794 #endif
795         if (sv_numreadmoves < 1)
796                 return;
797         // only start accepting input once the player is spawned
798         if (!host_client->begun)
799                 return;
800 #if DEBUGMOVES
801         Con_Printf("SV_ExecuteClientMoves: read %i moves at sv.time %f\n", sv_numreadmoves, (float)sv.time);
802 #endif
803         // disable clientside movement prediction in some cases
804         if (ceil(max(sv_readmoves[sv_numreadmoves-1].receivetime - sv_readmoves[sv_numreadmoves-1].time, 0) * 1000.0) < sv_clmovement_minping.integer)
805                 host_client->clmovement_disabletimeout = host.realtime + sv_clmovement_minping_disabletime.value / 1000.0;
806         // several conditions govern whether clientside movement prediction is allowed
807         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         {
809                 // process the moves in order and ignore old ones
810                 // but always trust the latest move
811                 // (this deals with bogus initial move sequences after level change,
812                 //  where the client will eventually catch up with the level change
813                 //  and reset its move sequence)
814                 for (moveindex = 0;moveindex < sv_numreadmoves;moveindex++)
815                 {
816                         usercmd_t *move = sv_readmoves + moveindex;
817                         if (host_client->movesequence < move->sequence || moveindex == sv_numreadmoves - 1)
818                         {
819 #if DEBUGMOVES
820                                 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 #endif
822                                 // this is a new move
823                                 move->time = bound(sv.time - 1, move->time, sv.time); // prevent slowhack/speedhack combos
824                                 move->time = max(move->time, host_client->cmd.time); // prevent backstepping of time
825                                 moveframetime = bound(0, move->time - host_client->cmd.time, min(0.1, sv_clmovement_inputtimeout.value));
826
827                                 // discard (treat like lost) moves with too low distance from
828                                 // the previous one to prevent hacks using float inaccuracy
829                                 // clients will see this as packet loss in the netgraph
830                                 // this should also apply if a move cannot get
831                                 // executed because it came too late and
832                                 // already was performed serverside
833                                 if(moveframetime < 0.0005)
834                                 {
835                                         // count the move as LOST if we don't
836                                         // execute it but it has higher
837                                         // sequence count
838                                         if(host_client->movesequence)
839                                                 if(move->sequence > host_client->movesequence)
840                                                         host_client->movement_count[(move->sequence) % NETGRAPH_PACKETS] = -1;
841                                         continue;
842                                 }
843
844                                 //Con_Printf("movesequence = %i (%i lost), moveframetime = %f\n", move->sequence, move->sequence ? move->sequence - host_client->movesequence - 1 : 0, moveframetime);
845                                 host_client->cmd = *move;
846                                 host_client->movesequence = move->sequence;
847
848                                 // if using prediction, we need to perform moves when packets are
849                                 // received, even if multiple occur in one frame
850                                 // (they can't go beyond the current time so there is no cheat issue
851                                 //  with this approach, and if they don't send input for a while they
852                                 //  start moving anyway, so the longest 'lagaport' possible is
853                                 //  determined by the sv_clmovement_inputtimeout cvar)
854                                 if (moveframetime <= 0)
855                                         continue;
856                                 oldframetime = PRVM_serverglobalfloat(frametime);
857                                 oldframetime2 = sv.frametime;
858                                 // update ping time for qc to see while executing this move
859                                 host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
860                                 // the server and qc frametime values must be changed temporarily
861                                 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime;
862                                 // if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
863                                 if (sv.frametime > 0.05)
864                                 {
865                                         PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime * 0.5f;
866                                         SV_Physics_ClientMove();
867                                 }
868                                 SV_Physics_ClientMove();
869                                 sv.frametime = oldframetime2;
870                                 PRVM_serverglobalfloat(frametime) = oldframetime;
871                                 host_client->clmovement_inputtimeout = sv_clmovement_inputtimeout.value;
872                         }
873                 }
874         }
875         else
876         {
877                 // try to gather button bits from old moves, but only if their time is
878                 // advancing (ones with the same timestamp can't be trusted)
879                 for (moveindex = 0;moveindex < sv_numreadmoves-1;moveindex++)
880                 {
881                         usercmd_t *move = sv_readmoves + moveindex;
882                         if (host_client->cmd.time < move->time)
883                         {
884                                 sv_readmoves[sv_numreadmoves-1].buttons |= move->buttons;
885                                 if (move->impulse)
886                                         sv_readmoves[sv_numreadmoves-1].impulse = move->impulse;
887                         }
888                 }
889                 // now copy the new move
890                 host_client->cmd = sv_readmoves[sv_numreadmoves-1];
891                 host_client->cmd.time = max(host_client->cmd.time, sv.time);
892                         // physics will run up to sv.time, so allow no predicted moves
893                         // before that otherwise, there is a speedhack by turning
894                         // prediction on and off repeatedly on client side because the
895                         // engine would run BOTH client and server physics for the same
896                         // time
897                 host_client->movesequence = 0;
898                 // make sure that normal physics takes over immediately
899                 host_client->clmovement_inputtimeout = 0;
900         }
901
902         // calculate average ping time
903         host_client->ping = host_client->cmd.receivetime - host_client->cmd.clienttime;
904 #ifdef NUM_PING_TIMES
905         host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = host_client->cmd.receivetime - host_client->cmd.clienttime;
906         host_client->num_pings++;
907         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
908                 total += host_client->ping_times[i];
909         host_client->ping = total / NUM_PING_TIMES;
910 #endif
911 }
912
913 void SV_ApplyClientMove (void)
914 {
915         prvm_prog_t *prog = SVVM_prog;
916         usercmd_t *move = &host_client->cmd;
917         int j, movementloss, packetloss;
918
919         if (!move->receivetime)
920                 return;
921
922         // note: a move can be applied multiple times if the client packets are
923         // not coming as often as the physics is executed, and the move must be
924         // applied before running qc each time because the id1 qc had a bug where
925         // it clears self.button2 in PlayerJump, causing pogostick behavior if
926         // moves are not applied every time before calling qc
927         move->applied = true;
928
929         // set the edict fields
930         PRVM_serveredictfloat(host_client->edict, button0) = move->buttons & 1;
931         PRVM_serveredictfloat(host_client->edict, button2) = (move->buttons & 2)>>1;
932         if (move->impulse)
933                 PRVM_serveredictfloat(host_client->edict, impulse) = move->impulse;
934         // only send the impulse to qc once
935         move->impulse = 0;
936
937         movementloss = packetloss = 0;
938         if(host_client->netconnection)
939         {
940                 for (j = 0;j < NETGRAPH_PACKETS;j++)
941                         if (host_client->netconnection->incoming_netgraph[j].unreliablebytes == NETGRAPH_LOSTPACKET)
942                                 packetloss++;
943                 for (j = 0;j < NETGRAPH_PACKETS;j++)
944                         if (host_client->movement_count[j] < 0)
945                                 movementloss++;
946         }
947
948         VectorCopy(move->viewangles, PRVM_serveredictvector(host_client->edict, v_angle));
949         PRVM_serveredictfloat(host_client->edict, button3) = ((move->buttons >> 2) & 1);
950         PRVM_serveredictfloat(host_client->edict, button4) = ((move->buttons >> 3) & 1);
951         PRVM_serveredictfloat(host_client->edict, button5) = ((move->buttons >> 4) & 1);
952         PRVM_serveredictfloat(host_client->edict, button6) = ((move->buttons >> 5) & 1);
953         PRVM_serveredictfloat(host_client->edict, button7) = ((move->buttons >> 6) & 1);
954         PRVM_serveredictfloat(host_client->edict, button8) = ((move->buttons >> 7) & 1);
955         PRVM_serveredictfloat(host_client->edict, button9) = ((move->buttons >> 11) & 1);
956         PRVM_serveredictfloat(host_client->edict, button10) = ((move->buttons >> 12) & 1);
957         PRVM_serveredictfloat(host_client->edict, button11) = ((move->buttons >> 13) & 1);
958         PRVM_serveredictfloat(host_client->edict, button12) = ((move->buttons >> 14) & 1);
959         PRVM_serveredictfloat(host_client->edict, button13) = ((move->buttons >> 15) & 1);
960         PRVM_serveredictfloat(host_client->edict, button14) = ((move->buttons >> 16) & 1);
961         PRVM_serveredictfloat(host_client->edict, button15) = ((move->buttons >> 17) & 1);
962         PRVM_serveredictfloat(host_client->edict, button16) = ((move->buttons >> 18) & 1);
963         PRVM_serveredictfloat(host_client->edict, buttonuse) = ((move->buttons >> 8) & 1);
964         PRVM_serveredictfloat(host_client->edict, buttonchat) = ((move->buttons >> 9) & 1);
965         PRVM_serveredictfloat(host_client->edict, cursor_active) = ((move->buttons >> 10) & 1);
966         VectorSet(PRVM_serveredictvector(host_client->edict, movement), move->forwardmove, move->sidemove, move->upmove);
967         VectorCopy(move->cursor_screen, PRVM_serveredictvector(host_client->edict, cursor_screen));
968         VectorCopy(move->cursor_start, PRVM_serveredictvector(host_client->edict, cursor_trace_start));
969         VectorCopy(move->cursor_impact, PRVM_serveredictvector(host_client->edict, cursor_trace_endpos));
970         PRVM_serveredictedict(host_client->edict, cursor_trace_ent) = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
971         PRVM_serveredictfloat(host_client->edict, ping) = host_client->ping * 1000.0;
972         PRVM_serveredictfloat(host_client->edict, ping_packetloss) = packetloss / (float) NETGRAPH_PACKETS;
973         PRVM_serveredictfloat(host_client->edict, ping_movementloss) = movementloss / (float) NETGRAPH_PACKETS;
974 }
975
976 static qboolean SV_FrameLost(int framenum)
977 {
978         if (host_client->entitydatabase5)
979         {
980                 if (framenum <= host_client->entitydatabase5->latestframenum)
981                 {
982                         EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
983                         EntityFrameCSQC_LostFrame(host_client, framenum);
984                         return true;
985                 }
986         }
987         return false;
988 }
989
990 static void SV_FrameAck(int framenum)
991 {
992         if (host_client->entitydatabase)
993                 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
994         else if (host_client->entitydatabase4)
995                 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
996         else if (host_client->entitydatabase5)
997                 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
998 }
999
1000 /*
1001 ===================
1002 SV_ReadClientMessage
1003 ===================
1004 */
1005 void SV_ReadClientMessage(void)
1006 {
1007         prvm_prog_t *prog = SVVM_prog;
1008         int netcmd, num, start;
1009         char *s, *p, *q;
1010
1011         if(sv_autodemo_perclient.integer >= 2)
1012                 SV_WriteDemoMessage(host_client, &(host_client->netconnection->message), true);
1013
1014         //MSG_BeginReading ();
1015         sv_numreadmoves = 0;
1016
1017         for(;;)
1018         {
1019                 if (!host_client->active)
1020                 {
1021                         // a command caused an error
1022                         SV_DropClient (false);
1023                         return;
1024                 }
1025
1026                 if (sv_message.badread)
1027                 {
1028                         Con_Print("SV_ReadClientMessage: badread\n");
1029                         SV_DropClient (false);
1030                         return;
1031                 }
1032
1033                 netcmd = MSG_ReadByte(&sv_message);
1034                 if (netcmd == -1)
1035                 {
1036                         // end of message
1037                         // apply the moves that were read this frame
1038                         SV_ExecuteClientMoves();
1039                         break;
1040                 }
1041
1042                 switch (netcmd)
1043                 {
1044                 default:
1045                         Con_Printf("SV_ReadClientMessage: unknown command char %i (at offset 0x%x)\n", netcmd, sv_message.readcount);
1046                         if (developer_networking.integer)
1047                                 Com_HexDumpToConsole(sv_message.data, sv_message.cursize);
1048                         SV_DropClient (false);
1049                         return;
1050
1051                 case clc_nop:
1052                         break;
1053
1054                 case clc_stringcmd:
1055                         // allow reliable messages now as the client is done with initial loading
1056                         if (host_client->sendsignon == 2)
1057                                 host_client->sendsignon = 0;
1058                         s = MSG_ReadString(&sv_message, sv_readstring, sizeof(sv_readstring));
1059                         q = NULL;
1060                         for(p = s; *p; ++p) switch(*p)
1061                         {
1062                                 case 10:
1063                                 case 13:
1064                                         if(!q)
1065                                                 q = p;
1066                                         break;
1067                                 default:
1068                                         if(q)
1069                                                 goto clc_stringcmd_invalid; // newline seen, THEN something else -> possible exploit
1070                                         break;
1071                         }
1072                         if(q)
1073                                 *q = 0;
1074                         if (strncasecmp(s, "spawn", 5) == 0
1075                          || strncasecmp(s, "begin", 5) == 0
1076                          || strncasecmp(s, "prespawn", 8) == 0)
1077                                 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
1078                         else if (PRVM_serverfunction(SV_ParseClientCommand))
1079                         {
1080                                 int restorevm_tempstringsbuf_cursize;
1081                                 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
1082                                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, s);
1083                                 PRVM_serverglobalfloat(time) = sv.time;
1084                                 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
1085                                 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_ParseClientCommand), "QC function SV_ParseClientCommand is missing");
1086                                 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1087                         }
1088                         else
1089                                 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
1090                         break;
1091
1092 clc_stringcmd_invalid:
1093                         Con_Printf("Received invalid stringcmd from %s\n", host_client->name);
1094                         if(developer.integer > 0)
1095                                 Com_HexDumpToConsole((unsigned char *) s, (int)strlen(s));
1096                         break;
1097
1098                 case clc_disconnect:
1099                         SV_DropClient (false); // client wants to disconnect
1100                         return;
1101
1102                 case clc_move:
1103                         SV_ReadClientMove();
1104                         break;
1105
1106                 case clc_ackdownloaddata:
1107                         start = MSG_ReadLong(&sv_message);
1108                         num = MSG_ReadShort(&sv_message);
1109                         if (host_client->download_file && host_client->download_started)
1110                         {
1111                                 if (host_client->download_expectedposition == start)
1112                                 {
1113                                         int size = (int)FS_FileSize(host_client->download_file);
1114                                         // a data block was successfully received by the client,
1115                                         // update the expected position on the next data block
1116                                         host_client->download_expectedposition = start + num;
1117                                         // if this was the last data block of the file, it's done
1118                                         if (host_client->download_expectedposition >= FS_FileSize(host_client->download_file))
1119                                         {
1120                                                 // tell the client that the download finished
1121                                                 // we need to calculate the crc now
1122                                                 //
1123                                                 // note: at this point the OS probably has the file
1124                                                 // entirely in memory, so this is a faster operation
1125                                                 // now than it was when the download started.
1126                                                 //
1127                                                 // it is also preferable to do this at the end of the
1128                                                 // download rather than the start because it reduces
1129                                                 // potential for Denial Of Service attacks against the
1130                                                 // server.
1131                                                 int crc;
1132                                                 unsigned char *temp;
1133                                                 FS_Seek(host_client->download_file, 0, SEEK_SET);
1134                                                 temp = (unsigned char *) Mem_Alloc(tempmempool, size);
1135                                                 FS_Read(host_client->download_file, temp, size);
1136                                                 crc = CRC_Block(temp, size);
1137                                                 Mem_Free(temp);
1138                                                 // calculated crc, send the file info to the client
1139                                                 // (so that it can verify the data)
1140                                                 SV_ClientCommands("\ncl_downloadfinished %i %i %s\n", size, crc, host_client->download_name);
1141                                                 Con_DPrintf("Download of %s by %s has finished\n", host_client->download_name, host_client->name);
1142                                                 FS_Close(host_client->download_file);
1143                                                 host_client->download_file = NULL;
1144                                                 host_client->download_name[0] = 0;
1145                                                 host_client->download_expectedposition = 0;
1146                                                 host_client->download_started = false;
1147                                         }
1148                                 }
1149                                 else
1150                                 {
1151                                         // a data block was lost, reset to the expected position
1152                                         // and resume sending from there
1153                                         FS_Seek(host_client->download_file, host_client->download_expectedposition, SEEK_SET);
1154                                 }
1155                         }
1156                         break;
1157
1158                 case clc_ackframe:
1159                         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
1160                         num = MSG_ReadLong(&sv_message);
1161                         if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
1162                         if (developer_networkentities.integer >= 10)
1163                                 Con_Printf("recv clc_ackframe %i\n", num);
1164                         // if the client hasn't progressed through signons yet,
1165                         // ignore any clc_ackframes we get (they're probably from the
1166                         // previous level)
1167                         if (host_client->begun && host_client->latestframenum < num)
1168                         {
1169                                 int i;
1170                                 for (i = host_client->latestframenum + 1;i < num;i++)
1171                                         if (!SV_FrameLost(i))
1172                                                 break;
1173                                 SV_FrameAck(num);
1174                                 host_client->latestframenum = num;
1175                         }
1176                         break;
1177                 }
1178         }
1179 }
1180