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