]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sv_user.c
279c036776399c9a82c9d1bbfc9d83114de896e6
[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 #define DEBUGMOVES 0
24
25 static usercmd_t cmd;
26
27 /*
28 ===============
29 SV_SetIdealPitch
30 ===============
31 */
32 #define MAX_FORWARD     6
33 void SV_SetIdealPitch (void)
34 {
35         float   angleval, sinval, cosval, step, dir;
36         trace_t tr;
37         vec3_t  top, bottom;
38         float   z[MAX_FORWARD];
39         int             i, j;
40         int             steps;
41
42         if (!((int)host_client->edict->fields.server->flags & FL_ONGROUND))
43                 return;
44
45         angleval = host_client->edict->fields.server->angles[YAW] * M_PI*2 / 360;
46         sinval = sin(angleval);
47         cosval = cos(angleval);
48
49         for (i=0 ; i<MAX_FORWARD ; i++)
50         {
51                 top[0] = host_client->edict->fields.server->origin[0] + cosval*(i+3)*12;
52                 top[1] = host_client->edict->fields.server->origin[1] + sinval*(i+3)*12;
53                 top[2] = host_client->edict->fields.server->origin[2] + host_client->edict->fields.server->view_ofs[2];
54
55                 bottom[0] = top[0];
56                 bottom[1] = top[1];
57                 bottom[2] = top[2] - 160;
58
59                 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, MOVE_NOMONSTERS, host_client->edict, SUPERCONTENTS_SOLID);
60                 // if looking at a wall, leave ideal the way is was
61                 if (tr.startsolid)
62                         return;
63
64                 // near a dropoff
65                 if (tr.fraction == 1)
66                         return;
67
68                 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
69         }
70
71         dir = 0;
72         steps = 0;
73         for (j=1 ; j<i ; j++)
74         {
75                 step = z[j] - z[j-1];
76                 if (step > -ON_EPSILON && step < ON_EPSILON)
77                         continue;
78
79                 // mixed changes
80                 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
81                         return;
82
83                 steps++;
84                 dir = step;
85         }
86
87         if (!dir)
88         {
89                 host_client->edict->fields.server->idealpitch = 0;
90                 return;
91         }
92
93         if (steps < 2)
94                 return;
95         host_client->edict->fields.server->idealpitch = -dir * sv_idealpitchscale.value;
96 }
97
98 static vec3_t wishdir, forward, right, up;
99 static float wishspeed;
100
101 static qboolean onground;
102
103 /*
104 ==================
105 SV_UserFriction
106
107 ==================
108 */
109 void SV_UserFriction (void)
110 {
111         float speed, newspeed, control, friction;
112         vec3_t start, stop;
113         trace_t trace;
114
115         speed = sqrt(host_client->edict->fields.server->velocity[0]*host_client->edict->fields.server->velocity[0]+host_client->edict->fields.server->velocity[1]*host_client->edict->fields.server->velocity[1]);
116         if (!speed)
117                 return;
118
119         // if the leading edge is over a dropoff, increase friction
120         start[0] = stop[0] = host_client->edict->fields.server->origin[0] + host_client->edict->fields.server->velocity[0]/speed*16;
121         start[1] = stop[1] = host_client->edict->fields.server->origin[1] + host_client->edict->fields.server->velocity[1]/speed*16;
122         start[2] = host_client->edict->fields.server->origin[2] + host_client->edict->fields.server->mins[2];
123         stop[2] = start[2] - 34;
124
125         trace = SV_Move (start, vec3_origin, vec3_origin, stop, MOVE_NOMONSTERS, host_client->edict, SV_GenericHitSuperContentsMask(host_client->edict));
126
127         if (trace.fraction == 1.0)
128                 friction = sv_friction.value*sv_edgefriction.value;
129         else
130                 friction = sv_friction.value;
131
132         // apply friction
133         control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
134         newspeed = speed - sv.frametime*control*friction;
135
136         if (newspeed < 0)
137                 newspeed = 0;
138         else
139                 newspeed /= speed;
140
141         VectorScale(host_client->edict->fields.server->velocity, newspeed, host_client->edict->fields.server->velocity);
142 }
143
144 /*
145 ==============
146 SV_Accelerate
147 ==============
148 */
149 void SV_Accelerate (void)
150 {
151         int i;
152         float addspeed, accelspeed, currentspeed;
153
154         currentspeed = DotProduct (host_client->edict->fields.server->velocity, wishdir);
155         addspeed = wishspeed - currentspeed;
156         if (addspeed <= 0)
157                 return;
158         accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
159         if (accelspeed > addspeed)
160                 accelspeed = addspeed;
161
162         for (i=0 ; i<3 ; i++)
163                 host_client->edict->fields.server->velocity[i] += accelspeed*wishdir[i];
164 }
165
166 void SV_AirAccelerate (vec3_t wishveloc)
167 {
168         int i;
169         float addspeed, wishspd, accelspeed, currentspeed;
170
171         wishspd = VectorNormalizeLength (wishveloc);
172         if (wishspd > sv_maxairspeed.value)
173                 wishspd = sv_maxairspeed.value;
174         currentspeed = DotProduct (host_client->edict->fields.server->velocity, wishveloc);
175         addspeed = wishspd - currentspeed;
176         if (addspeed <= 0)
177                 return;
178         accelspeed = (sv_airaccelerate.value < 0 ? sv_accelerate.value : sv_airaccelerate.value)*wishspeed * sv.frametime;
179         if (accelspeed > addspeed)
180                 accelspeed = addspeed;
181
182         for (i=0 ; i<3 ; i++)
183                 host_client->edict->fields.server->velocity[i] += accelspeed*wishveloc[i];
184 }
185
186
187 void DropPunchAngle (void)
188 {
189         float len;
190         prvm_eval_t *val;
191
192         len = VectorNormalizeLength (host_client->edict->fields.server->punchangle);
193
194         len -= 10*sv.frametime;
195         if (len < 0)
196                 len = 0;
197         VectorScale (host_client->edict->fields.server->punchangle, len, host_client->edict->fields.server->punchangle);
198
199         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.punchvector)))
200         {
201                 len = VectorNormalizeLength (val->vector);
202
203                 len -= 20*sv.frametime;
204                 if (len < 0)
205                         len = 0;
206                 VectorScale (val->vector, len, val->vector);
207         }
208 }
209
210 /*
211 ===================
212 SV_FreeMove
213 ===================
214 */
215 void SV_FreeMove (void)
216 {
217         int i;
218         float wishspeed;
219
220         AngleVectors (host_client->edict->fields.server->v_angle, forward, right, up);
221
222         for (i = 0; i < 3; i++)
223                 host_client->edict->fields.server->velocity[i] = forward[i] * cmd.forwardmove + right[i] * cmd.sidemove;
224
225         host_client->edict->fields.server->velocity[2] += cmd.upmove;
226
227         wishspeed = VectorLength(host_client->edict->fields.server->velocity);
228         if (wishspeed > sv_maxspeed.value)
229                 VectorScale(host_client->edict->fields.server->velocity, sv_maxspeed.value / wishspeed, host_client->edict->fields.server->velocity);
230 }
231
232 /*
233 ===================
234 SV_WaterMove
235
236 ===================
237 */
238 void SV_WaterMove (void)
239 {
240         int i;
241         vec3_t wishvel;
242         float speed, newspeed, wishspeed, addspeed, accelspeed, temp;
243
244         // user intentions
245         AngleVectors (host_client->edict->fields.server->v_angle, forward, right, up);
246
247         for (i=0 ; i<3 ; i++)
248                 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
249
250         if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
251                 wishvel[2] -= 60;               // drift towards bottom
252         else
253                 wishvel[2] += cmd.upmove;
254
255         wishspeed = VectorLength(wishvel);
256         if (wishspeed > sv_maxspeed.value)
257         {
258                 temp = sv_maxspeed.value/wishspeed;
259                 VectorScale (wishvel, temp, wishvel);
260                 wishspeed = sv_maxspeed.value;
261         }
262         wishspeed *= 0.7;
263
264         // water friction
265         speed = VectorLength(host_client->edict->fields.server->velocity);
266         if (speed)
267         {
268                 newspeed = speed - sv.frametime * speed * (sv_waterfriction.value < 0 ? sv_friction.value : sv_waterfriction.value);
269                 if (newspeed < 0)
270                         newspeed = 0;
271                 temp = newspeed/speed;
272                 VectorScale(host_client->edict->fields.server->velocity, temp, host_client->edict->fields.server->velocity);
273         }
274         else
275                 newspeed = 0;
276
277         // water acceleration
278         if (!wishspeed)
279                 return;
280
281         addspeed = wishspeed - newspeed;
282         if (addspeed <= 0)
283                 return;
284
285         VectorNormalize (wishvel);
286         accelspeed = (sv_wateraccelerate.value < 0 ? sv_accelerate.value : sv_wateraccelerate.value) * wishspeed * sv.frametime;
287         if (accelspeed > addspeed)
288                 accelspeed = addspeed;
289
290         for (i=0 ; i<3 ; i++)
291                 host_client->edict->fields.server->velocity[i] += accelspeed * wishvel[i];
292 }
293
294 void SV_WaterJump (void)
295 {
296         if (sv.time > host_client->edict->fields.server->teleport_time || !host_client->edict->fields.server->waterlevel)
297         {
298                 host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags & ~FL_WATERJUMP;
299                 host_client->edict->fields.server->teleport_time = 0;
300         }
301         host_client->edict->fields.server->velocity[0] = host_client->edict->fields.server->movedir[0];
302         host_client->edict->fields.server->velocity[1] = host_client->edict->fields.server->movedir[1];
303 }
304
305
306 /*
307 ===================
308 SV_AirMove
309
310 ===================
311 */
312 void SV_AirMove (void)
313 {
314         int i;
315         vec3_t wishvel;
316         float fmove, smove, temp;
317
318         // LordHavoc: correct quake movement speed bug when looking up/down
319         wishvel[0] = wishvel[2] = 0;
320         wishvel[1] = host_client->edict->fields.server->angles[1];
321         AngleVectors (wishvel, forward, right, up);
322
323         fmove = cmd.forwardmove;
324         smove = cmd.sidemove;
325
326 // hack to not let you back into teleporter
327         if (sv.time < host_client->edict->fields.server->teleport_time && fmove < 0)
328                 fmove = 0;
329
330         for (i=0 ; i<3 ; i++)
331                 wishvel[i] = forward[i]*fmove + right[i]*smove;
332
333         if ((int)host_client->edict->fields.server->movetype != MOVETYPE_WALK)
334                 wishvel[2] += cmd.upmove;
335
336         VectorCopy (wishvel, wishdir);
337         wishspeed = VectorNormalizeLength(wishdir);
338         if (wishspeed > sv_maxspeed.value)
339         {
340                 temp = sv_maxspeed.value/wishspeed;
341                 VectorScale (wishvel, temp, wishvel);
342                 wishspeed = sv_maxspeed.value;
343         }
344
345         if (host_client->edict->fields.server->movetype == MOVETYPE_NOCLIP)
346         {
347                 // noclip
348                 VectorCopy (wishvel, host_client->edict->fields.server->velocity);
349         }
350         else if (onground)
351         {
352                 SV_UserFriction ();
353                 SV_Accelerate ();
354         }
355         else
356         {
357                 // not on ground, so little effect on velocity
358                 SV_AirAccelerate (wishvel);
359         }
360 }
361
362 /*
363 ===================
364 SV_ClientThink
365
366 the move fields specify an intended velocity in pix/sec
367 the angle fields specify an exact angular motion in degrees
368 ===================
369 */
370 void SV_ClientThink (void)
371 {
372         vec3_t v_angle;
373
374         //Con_Printf("clientthink for %ims\n", (int) (sv.frametime * 1000));
375
376         SV_ApplyClientMove();
377         // make sure the velocity is sane (not a NaN)
378         SV_CheckVelocity(host_client->edict);
379
380         // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
381         if (prog->funcoffsets.SV_PlayerPhysics && sv_playerphysicsqc.integer)
382         {
383                 prog->globals.server->time = sv.time;
384                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
385                 PRVM_ExecuteProgram (prog->funcoffsets.SV_PlayerPhysics, "QC function SV_PlayerPhysics is missing");
386                 SV_CheckVelocity(host_client->edict);
387                 return;
388         }
389
390         if (host_client->edict->fields.server->movetype == MOVETYPE_NONE)
391                 return;
392
393         onground = (int)host_client->edict->fields.server->flags & FL_ONGROUND;
394
395         DropPunchAngle ();
396
397         // if dead, behave differently
398         if (host_client->edict->fields.server->health <= 0)
399                 return;
400
401         cmd = host_client->cmd;
402
403         // angles
404         // show 1/3 the pitch angle and all the roll angle
405         VectorAdd (host_client->edict->fields.server->v_angle, host_client->edict->fields.server->punchangle, v_angle);
406         host_client->edict->fields.server->angles[ROLL] = V_CalcRoll (host_client->edict->fields.server->angles, host_client->edict->fields.server->velocity)*4;
407         if (!host_client->edict->fields.server->fixangle)
408         {
409                 host_client->edict->fields.server->angles[PITCH] = -v_angle[PITCH]/3;
410                 host_client->edict->fields.server->angles[YAW] = v_angle[YAW];
411         }
412
413         if ( (int)host_client->edict->fields.server->flags & FL_WATERJUMP )
414         {
415                 SV_WaterJump ();
416                 SV_CheckVelocity(host_client->edict);
417                 return;
418         }
419
420         /*
421         // Player is (somehow) outside of the map, or flying, or noclipping
422         if (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP && (host_client->edict->fields.server->movetype == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict)))
423         //if (host_client->edict->fields.server->movetype == MOVETYPE_NOCLIP || host_client->edict->fields.server->movetype == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict))
424         {
425                 SV_FreeMove ();
426                 return;
427         }
428         */
429
430         // walk
431         if ((host_client->edict->fields.server->waterlevel >= 2) && (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP))
432         {
433                 SV_WaterMove ();
434                 SV_CheckVelocity(host_client->edict);
435                 return;
436         }
437
438         SV_AirMove ();
439         SV_CheckVelocity(host_client->edict);
440 }
441
442 /*
443 ===================
444 SV_ReadClientMove
445 ===================
446 */
447 int sv_numreadmoves = 0;
448 usercmd_t sv_readmoves[CL_MAX_USERCMDS];
449 void SV_ReadClientMove (void)
450 {
451         int i;
452         usercmd_t newmove;
453         usercmd_t *move = &newmove;
454
455         memset(move, 0, sizeof(*move));
456
457         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
458
459         // read ping time
460         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)
461                 move->sequence = MSG_ReadLong ();
462         move->time = MSG_ReadFloat ();
463         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
464         move->receivetime = (float)sv.time;
465
466 #if DEBUGMOVES
467         Con_Printf("%s move%i #%i %ims (%ims) %i %i '%i %i %i' '%i %i %i'\n", move->time > move->receivetime ? "^3read future" : "^4read normal", sv_numreadmoves + 1, move->sequence, (int)floor((move->time - host_client->cmd.time) * 1000.0 + 0.5), (int)floor(move->time * 1000.0 + 0.5), move->impulse, move->buttons, (int)move->viewangles[0], (int)move->viewangles[1], (int)move->viewangles[2], (int)move->forwardmove, (int)move->sidemove, (int)move->upmove);
468 #endif
469         // limit reported time to current time
470         // (incase the client is trying to cheat)
471         move->time = min(move->time, move->receivetime + sv.frametime);
472
473         // read current angles
474         for (i = 0;i < 3;i++)
475         {
476                 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)
477                         move->viewangles[i] = MSG_ReadAngle8i();
478                 else if (sv.protocol == PROTOCOL_DARKPLACES1)
479                         move->viewangles[i] = MSG_ReadAngle16i();
480                 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
481                         move->viewangles[i] = MSG_ReadAngle32f();
482                 else
483                         move->viewangles[i] = MSG_ReadAngle16i();
484         }
485         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
486
487         // read movement
488         move->forwardmove = MSG_ReadCoord16i ();
489         move->sidemove = MSG_ReadCoord16i ();
490         move->upmove = MSG_ReadCoord16i ();
491         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
492
493         // read buttons
494         // be sure to bitwise OR them into the move->buttons because we want to
495         // accumulate button presses from multiple packets per actual move
496         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)
497                 move->buttons = MSG_ReadByte ();
498         else
499                 move->buttons = MSG_ReadLong ();
500         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
501
502         // read impulse
503         move->impulse = MSG_ReadByte ();
504         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
505
506         // PRYDON_CLIENTCURSOR
507         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)
508         {
509                 // 30 bytes
510                 move->cursor_screen[0] = MSG_ReadShort() * (1.0f / 32767.0f);
511                 move->cursor_screen[1] = MSG_ReadShort() * (1.0f / 32767.0f);
512                 move->cursor_start[0] = MSG_ReadFloat();
513                 move->cursor_start[1] = MSG_ReadFloat();
514                 move->cursor_start[2] = MSG_ReadFloat();
515                 move->cursor_impact[0] = MSG_ReadFloat();
516                 move->cursor_impact[1] = MSG_ReadFloat();
517                 move->cursor_impact[2] = MSG_ReadFloat();
518                 move->cursor_entitynumber = (unsigned short)MSG_ReadShort();
519                 if (move->cursor_entitynumber >= prog->max_edicts)
520                 {
521                         Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
522                         move->cursor_entitynumber = 0;
523                 }
524                 // as requested by FrikaC, cursor_trace_ent is reset to world if the
525                 // entity is free at time of receipt
526                 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
527                         move->cursor_entitynumber = 0;
528                 if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
529         }
530
531         // if the previous move has not been applied yet, we need to accumulate
532         // the impulse/buttons from it
533         if (!host_client->cmd.applied)
534         {
535                 if (!move->impulse)
536                         move->impulse = host_client->cmd.impulse;
537                 move->buttons |= host_client->cmd.buttons;
538         }
539
540         // now store this move for later execution
541         // (we have to buffer the moves because of old ones being repeated)
542         if (sv_numreadmoves < CL_MAX_USERCMDS)
543                 sv_readmoves[sv_numreadmoves++] = *move;
544 }
545
546 void SV_ExecuteClientMoves(void)
547 {
548         int moveindex;
549         float moveframetime;
550         double oldframetime;
551         double oldframetime2;
552 #ifdef NUM_PING_TIMES
553         double total;
554 #endif
555         prvm_eval_t *val;
556         if (sv_numreadmoves < 1)
557                 return;
558         // only start accepting input once the player is spawned
559         if (!host_client->spawned)
560                 return;
561 #if DEBUGMOVES
562         Con_Printf("SV_ExecuteClientMoves: read %i moves at sv.time %f\n", sv_numreadmoves, (float)sv.time);
563 #endif
564         // disable clientside movement prediction in some cases
565         if (ceil(max(sv_readmoves[sv_numreadmoves-1].receivetime - sv_readmoves[sv_numreadmoves-1].time, 0) * 1000.0) < sv_clmovement_minping.integer)
566                 host_client->clmovement_disabletimeout = realtime + sv_clmovement_minping_disabletime.value / 1000.0;
567         // several conditions govern whether clientside movement prediction is allowed
568         if (sv_readmoves[sv_numreadmoves-1].sequence && sv_clmovement_enable.integer && sv_clmovement_waitforinput.integer > 0 && host_client->clmovement_disabletimeout <= realtime && host_client->edict->fields.server->movetype == MOVETYPE_WALK && (!(val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.disableclientprediction)) || !val->_float))
569         {
570                 // process the moves in order and ignore old ones
571                 // but always trust the latest move
572                 // (this deals with bogus initial move sequences after level change,
573                 //  where the client will eventually catch up with the level change
574                 //  and reset its move sequence)
575                 for (moveindex = 0;moveindex < sv_numreadmoves;moveindex++)
576                 {
577                         usercmd_t *move = sv_readmoves + moveindex;
578                         if (host_client->movesequence < move->sequence || moveindex == sv_numreadmoves - 1)
579                         {
580 #if DEBUGMOVES
581                                 Con_Printf("%smove #%i %ims (%ims) %i %i '%i %i %i' '%i %i %i'\n", (move->time - host_client->cmd.time) > sv.frametime * 1.01 ? "^1" : "^2", move->sequence, (int)floor((move->time - host_client->cmd.time) * 1000.0 + 0.5), (int)floor(move->time * 1000.0 + 0.5), move->impulse, move->buttons, (int)move->viewangles[0], (int)move->viewangles[1], (int)move->viewangles[2], (int)move->forwardmove, (int)move->sidemove, (int)move->upmove);
582 #endif
583                                 // this is a new move
584                                 move->time = bound(sv.time - 1, move->time, sv.time); // prevent slowhack/speedhack combos
585                                 move->time = max(move->time, host_client->cmd.time); // prevent backstepping of time
586                                 moveframetime = bound(0, move->time - host_client->cmd.time, 0.1);
587                                 //Con_Printf("movesequence = %i (%i lost), moveframetime = %f\n", move->sequence, move->sequence ? move->sequence - host_client->movesequence - 1 : 0, moveframetime);
588                                 host_client->cmd = *move;
589                                 host_client->movesequence = move->sequence;
590
591                                 // if using prediction, we need to perform moves when packets are
592                                 // received, even if multiple occur in one frame
593                                 // (they can't go beyond the current time so there is no cheat issue
594                                 //  with this approach, and if they don't send input for a while they
595                                 //  start moving anyway, so the longest 'lagaport' possible is
596                                 //  determined by the sv_clmovement_waitforinput cvar)
597                                 if (moveframetime <= 0)
598                                         continue;
599                                 oldframetime = prog->globals.server->frametime;
600                                 oldframetime2 = sv.frametime;
601                                 // update ping time for qc to see while executing this move
602                                 host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
603                                 // the server and qc frametime values must be changed temporarily
604                                 prog->globals.server->frametime = sv.frametime = moveframetime;
605                                 // if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
606                                 if (sv.frametime > 0.05)
607                                 {
608                                         prog->globals.server->frametime = sv.frametime = moveframetime * 0.5f;
609                                         SV_Physics_ClientMove();
610                                 }
611                                 SV_Physics_ClientMove();
612                                 sv.frametime = oldframetime2;
613                                 prog->globals.server->frametime = oldframetime;
614                                 host_client->clmovement_skipphysicsframes = sv_clmovement_waitforinput.integer;
615                         }
616                 }
617         }
618         else
619         {
620                 // try to gather button bits from old moves, but only if their time is
621                 // advancing (ones with the same timestamp can't be trusted)
622                 for (moveindex = 0;moveindex < sv_numreadmoves-1;moveindex++)
623                 {
624                         usercmd_t *move = sv_readmoves + moveindex;
625                         if (host_client->cmd.time < move->time)
626                         {
627                                 sv_readmoves[sv_numreadmoves-1].buttons |= move->buttons;
628                                 if (move->impulse)
629                                         sv_readmoves[sv_numreadmoves-1].impulse = move->impulse;
630                         }
631                 }
632                 // now copy the new move
633                 host_client->cmd = sv_readmoves[sv_numreadmoves-1];
634                 host_client->cmd.time = max(host_client->cmd.time, sv.time);
635                         // physics will run up to sv.time, so allow no predicted moves
636                         // before that otherwise, there is a speedhack by turning
637                         // prediction on and off repeatedly on client side because the
638                         // engine would run BOTH client and server physics for the same
639                         // time
640                 host_client->movesequence = 0;
641                 // make sure that normal physics takes over immediately
642                 host_client->clmovement_skipphysicsframes = 0;
643         }
644
645         // calculate average ping time
646         host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
647 #ifdef NUM_PING_TIMES
648         host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = host_client->cmd.receivetime - host_client->cmd.time;
649         host_client->num_pings++;
650         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
651                 total += host_client->ping_times[i];
652         host_client->ping = total / NUM_PING_TIMES;
653 #endif
654 }
655
656 void SV_ApplyClientMove (void)
657 {
658         prvm_eval_t *val;
659         usercmd_t *move = &host_client->cmd;
660
661         if (!move->receivetime)
662                 return;
663
664         // note: a move can be applied multiple times if the client packets are
665         // not coming as often as the physics is executed, and the move must be
666         // applied before running qc each time because the id1 qc had a bug where
667         // it clears self.button2 in PlayerJump, causing pogostick behavior if
668         // moves are not applied every time before calling qc
669         move->applied = true;
670
671         // set the edict fields
672         host_client->edict->fields.server->button0 = move->buttons & 1;
673         host_client->edict->fields.server->button2 = (move->buttons & 2)>>1;
674         if (move->impulse)
675                 host_client->edict->fields.server->impulse = move->impulse;
676         // only send the impulse to qc once
677         move->impulse = 0;
678         VectorCopy(move->viewangles, host_client->edict->fields.server->v_angle);
679         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button3))) val->_float = ((move->buttons >> 2) & 1);
680         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button4))) val->_float = ((move->buttons >> 3) & 1);
681         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button5))) val->_float = ((move->buttons >> 4) & 1);
682         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button6))) val->_float = ((move->buttons >> 5) & 1);
683         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button7))) val->_float = ((move->buttons >> 6) & 1);
684         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button8))) val->_float = ((move->buttons >> 7) & 1);
685         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button9))) val->_float = ((move->buttons >> 11) & 1);
686         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button10))) val->_float = ((move->buttons >> 12) & 1);
687         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button11))) val->_float = ((move->buttons >> 13) & 1);
688         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button12))) val->_float = ((move->buttons >> 14) & 1);
689         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button13))) val->_float = ((move->buttons >> 15) & 1);
690         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button14))) val->_float = ((move->buttons >> 16) & 1);
691         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button15))) val->_float = ((move->buttons >> 17) & 1);
692         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button16))) val->_float = ((move->buttons >> 18) & 1);
693         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.buttonuse))) val->_float = ((move->buttons >> 8) & 1);
694         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.buttonchat))) val->_float = ((move->buttons >> 9) & 1);
695         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_active))) val->_float = ((move->buttons >> 10) & 1);
696         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.movement))) VectorSet(val->vector, move->forwardmove, move->sidemove, move->upmove);
697         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_screen))) VectorCopy(move->cursor_screen, val->vector);
698         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_trace_start))) VectorCopy(move->cursor_start, val->vector);
699         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_trace_endpos))) VectorCopy(move->cursor_impact, val->vector);
700         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_trace_ent))) val->edict = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
701         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ping))) val->_float = host_client->ping * 1000.0;
702 }
703
704 void SV_FrameLost(int framenum)
705 {
706         if (host_client->entitydatabase5)
707                 EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
708 }
709
710 void SV_FrameAck(int framenum)
711 {
712         if (host_client->entitydatabase)
713                 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
714         else if (host_client->entitydatabase4)
715                 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
716         else if (host_client->entitydatabase5)
717                 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
718 }
719
720 /*
721 ===================
722 SV_ReadClientMessage
723 ===================
724 */
725 extern void SV_SendServerinfo(client_t *client);
726 extern sizebuf_t vm_tempstringsbuf;
727 void SV_ReadClientMessage(void)
728 {
729         int cmd, num, start;
730         char *s, *p, *q;
731
732         //MSG_BeginReading ();
733         sv_numreadmoves = 0;
734
735         for(;;)
736         {
737                 if (!host_client->active)
738                 {
739                         // a command caused an error
740                         SV_DropClient (false);
741                         return;
742                 }
743
744                 if (msg_badread)
745                 {
746                         Con_Print("SV_ReadClientMessage: badread\n");
747                         SV_DropClient (false);
748                         return;
749                 }
750
751                 cmd = MSG_ReadByte ();
752                 if (cmd == -1)
753                 {
754                         // end of message
755                         // apply the moves that were read this frame
756                         SV_ExecuteClientMoves();
757                         break;
758                 }
759
760                 switch (cmd)
761                 {
762                 default:
763                         Con_Printf("SV_ReadClientMessage: unknown command char %i\n", cmd);
764                         SV_DropClient (false);
765                         return;
766
767                 case clc_nop:
768                         break;
769
770                 case clc_stringcmd:
771                         // allow reliable messages now as the client is done with initial loading
772                         if (host_client->sendsignon == 2)
773                                 host_client->sendsignon = 0;
774                         s = MSG_ReadString ();
775                         q = NULL;
776                         for(p = s; *p; ++p) switch(*p)
777                         {
778                                 case 10:
779                                 case 13:
780                                         if(!q)
781                                                 q = p;
782                                         break;
783                                 default:
784                                         if(q)
785                                                 goto clc_stringcmd_invalid; // newline seen, THEN something else -> possible exploit
786                                         break;
787                         }
788                         if(q)
789                                 *q = 0;
790                         if (strncasecmp(s, "spawn", 5) == 0
791                          || strncasecmp(s, "begin", 5) == 0
792                          || strncasecmp(s, "prespawn", 8) == 0)
793                                 Cmd_ExecuteString (s, src_client);
794                         else if (prog->funcoffsets.SV_ParseClientCommand)
795                         {
796                                 int restorevm_tempstringsbuf_cursize;
797                                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
798                                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(s);
799                                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
800                                 PRVM_ExecuteProgram (prog->funcoffsets.SV_ParseClientCommand, "QC function SV_ParseClientCommand is missing");
801                                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
802                         }
803                         else
804                                 Cmd_ExecuteString (s, src_client);
805                         break;
806
807 clc_stringcmd_invalid:
808                         Con_Printf("Received invalid stringcmd from %s\n", host_client->name);
809                         if(developer.integer)
810                                 Com_HexDumpToConsole((unsigned char *) s, strlen(s));
811                         break;
812
813                 case clc_disconnect:
814                         SV_DropClient (false); // client wants to disconnect
815                         return;
816
817                 case clc_move:
818                         SV_ReadClientMove();
819                         break;
820
821                 case clc_ackdownloaddata:
822                         start = MSG_ReadLong();
823                         num = MSG_ReadShort();
824                         if (host_client->download_file && host_client->download_started)
825                         {
826                                 if (host_client->download_expectedposition == start)
827                                 {
828                                         int size = (int)FS_FileSize(host_client->download_file);
829                                         // a data block was successfully received by the client,
830                                         // update the expected position on the next data block
831                                         host_client->download_expectedposition = start + num;
832                                         // if this was the last data block of the file, it's done
833                                         if (host_client->download_expectedposition >= FS_FileSize(host_client->download_file))
834                                         {
835                                                 // tell the client that the download finished
836                                                 // we need to calculate the crc now
837                                                 //
838                                                 // note: at this point the OS probably has the file
839                                                 // entirely in memory, so this is a faster operation
840                                                 // now than it was when the download started.
841                                                 //
842                                                 // it is also preferable to do this at the end of the
843                                                 // download rather than the start because it reduces
844                                                 // potential for Denial Of Service attacks against the
845                                                 // server.
846                                                 int crc;
847                                                 unsigned char *temp;
848                                                 FS_Seek(host_client->download_file, 0, SEEK_SET);
849                                                 temp = Mem_Alloc(tempmempool, size);
850                                                 FS_Read(host_client->download_file, temp, size);
851                                                 crc = CRC_Block(temp, size);
852                                                 Mem_Free(temp);
853                                                 // calculated crc, send the file info to the client
854                                                 // (so that it can verify the data)
855                                                 Host_ClientCommands(va("\ncl_downloadfinished %i %i %s\n", size, crc, host_client->download_name));
856                                                 Con_DPrintf("Download of %s by %s has finished\n", host_client->download_name, host_client->name);
857                                                 FS_Close(host_client->download_file);
858                                                 host_client->download_file = NULL;
859                                                 host_client->download_name[0] = 0;
860                                                 host_client->download_expectedposition = 0;
861                                                 host_client->download_started = false;
862                                         }
863                                 }
864                                 else
865                                 {
866                                         // a data block was lost, reset to the expected position
867                                         // and resume sending from there
868                                         FS_Seek(host_client->download_file, host_client->download_expectedposition, SEEK_SET);
869                                 }
870                         }
871                         break;
872
873                 case clc_ackframe:
874                         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
875                         num = MSG_ReadLong();
876                         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
877                         if (developer_networkentities.integer >= 10)
878                                 Con_Printf("recv clc_ackframe %i\n", num);
879                         // if the client hasn't progressed through signons yet,
880                         // ignore any clc_ackframes we get (they're probably from the
881                         // previous level)
882                         if (host_client->spawned && host_client->latestframenum < num)
883                         {
884                                 int i;
885                                 for (i = host_client->latestframenum + 1;i < num;i++)
886                                         SV_FrameLost(i);
887                                 SV_FrameAck(num);
888                                 host_client->latestframenum = num;
889                         }
890                         break;
891                 }
892         }
893 }
894