added sv_clmovement_* cvars to disable movement prediction of players, or disable...
[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
24 cvar_t sv_edgefriction = {0, "edgefriction", "2", "how much you slow down when nearing a ledge you might fall off"};
25 cvar_t sv_idealpitchscale = {0, "sv_idealpitchscale","0.8", "how much to look up/down slopes and stairs when not using freelook"};
26 cvar_t sv_maxspeed = {CVAR_NOTIFY, "sv_maxspeed", "320", "maximum speed a player can accelerate to when on ground (can be exceeded by tricks)"};
27 cvar_t sv_maxairspeed = {0, "sv_maxairspeed", "30", "maximum speed a player can accelerate to when airborn (note that it is possible to completely stop by moving the opposite direction)"};
28 cvar_t sv_accelerate = {0, "sv_accelerate", "10", "rate at which a player accelerates to sv_maxspeed"};
29 cvar_t sv_airaccelerate = {0, "sv_airaccelerate", "-1", "rate at which a player accelerates to sv_maxairspeed while in the air, if less than 0 the sv_accelerate variable is used instead"};
30 cvar_t sv_wateraccelerate = {0, "sv_wateraccelerate", "-1", "rate at which a player accelerates to sv_maxspeed while in the air, if less than 0 the sv_accelerate variable is used instead"};
31 cvar_t sv_clmovement_enable = {0, "sv_clmovement_enable", "1", "whether to allow clients to use cl_movement prediction, which can cause choppy movement on the server which may annoy other players"};
32 cvar_t sv_clmovement_minping = {0, "sv_clmovement_minping", "100", "if client ping is below this time in milliseconds, then their ability to use cl_movement prediction is disabled for a while (as they don't need it)"};
33 cvar_t sv_clmovement_minping_disabletime = {0, "sv_clmovement_minping_disabletime", "1000", "when client falls below minping, disable their prediction for this many milliseconds (should be at least 1000 or else their prediction may turn on/off frequently)"};
34
35 static usercmd_t cmd;
36
37
38 /*
39 ===============
40 SV_SetIdealPitch
41 ===============
42 */
43 #define MAX_FORWARD     6
44 void SV_SetIdealPitch (void)
45 {
46         float   angleval, sinval, cosval, step, dir;
47         trace_t tr;
48         vec3_t  top, bottom;
49         float   z[MAX_FORWARD];
50         int             i, j;
51         int             steps;
52
53         if (!((int)host_client->edict->fields.server->flags & FL_ONGROUND))
54                 return;
55
56         angleval = host_client->edict->fields.server->angles[YAW] * M_PI*2 / 360;
57         sinval = sin(angleval);
58         cosval = cos(angleval);
59
60         for (i=0 ; i<MAX_FORWARD ; i++)
61         {
62                 top[0] = host_client->edict->fields.server->origin[0] + cosval*(i+3)*12;
63                 top[1] = host_client->edict->fields.server->origin[1] + sinval*(i+3)*12;
64                 top[2] = host_client->edict->fields.server->origin[2] + host_client->edict->fields.server->view_ofs[2];
65
66                 bottom[0] = top[0];
67                 bottom[1] = top[1];
68                 bottom[2] = top[2] - 160;
69
70                 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, MOVE_NOMONSTERS, host_client->edict);
71                 // if looking at a wall, leave ideal the way is was
72                 if (tr.startsolid)
73                         return;
74
75                 // near a dropoff
76                 if (tr.fraction == 1)
77                         return;
78
79                 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
80         }
81
82         dir = 0;
83         steps = 0;
84         for (j=1 ; j<i ; j++)
85         {
86                 step = z[j] - z[j-1];
87                 if (step > -ON_EPSILON && step < ON_EPSILON)
88                         continue;
89
90                 // mixed changes
91                 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
92                         return;
93
94                 steps++;
95                 dir = step;
96         }
97
98         if (!dir)
99         {
100                 host_client->edict->fields.server->idealpitch = 0;
101                 return;
102         }
103
104         if (steps < 2)
105                 return;
106         host_client->edict->fields.server->idealpitch = -dir * sv_idealpitchscale.value;
107 }
108
109 static vec3_t wishdir, forward, right, up;
110 static float wishspeed;
111
112 static qboolean onground;
113
114 /*
115 ==================
116 SV_UserFriction
117
118 ==================
119 */
120 void SV_UserFriction (void)
121 {
122         float speed, newspeed, control, friction;
123         vec3_t start, stop;
124         trace_t trace;
125
126         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]);
127         if (!speed)
128                 return;
129
130         // if the leading edge is over a dropoff, increase friction
131         start[0] = stop[0] = host_client->edict->fields.server->origin[0] + host_client->edict->fields.server->velocity[0]/speed*16;
132         start[1] = stop[1] = host_client->edict->fields.server->origin[1] + host_client->edict->fields.server->velocity[1]/speed*16;
133         start[2] = host_client->edict->fields.server->origin[2] + host_client->edict->fields.server->mins[2];
134         stop[2] = start[2] - 34;
135
136         trace = SV_Move (start, vec3_origin, vec3_origin, stop, MOVE_NOMONSTERS, host_client->edict);
137
138         if (trace.fraction == 1.0)
139                 friction = sv_friction.value*sv_edgefriction.value;
140         else
141                 friction = sv_friction.value;
142
143         // apply friction
144         control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
145         newspeed = speed - sv.frametime*control*friction;
146
147         if (newspeed < 0)
148                 newspeed = 0;
149         else
150                 newspeed /= speed;
151
152         VectorScale(host_client->edict->fields.server->velocity, newspeed, host_client->edict->fields.server->velocity);
153 }
154
155 /*
156 ==============
157 SV_Accelerate
158 ==============
159 */
160 void SV_Accelerate (void)
161 {
162         int i;
163         float addspeed, accelspeed, currentspeed;
164
165         currentspeed = DotProduct (host_client->edict->fields.server->velocity, wishdir);
166         addspeed = wishspeed - currentspeed;
167         if (addspeed <= 0)
168                 return;
169         accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
170         if (accelspeed > addspeed)
171                 accelspeed = addspeed;
172
173         for (i=0 ; i<3 ; i++)
174                 host_client->edict->fields.server->velocity[i] += accelspeed*wishdir[i];
175 }
176
177 void SV_AirAccelerate (vec3_t wishveloc)
178 {
179         int i;
180         float addspeed, wishspd, accelspeed, currentspeed;
181
182         wishspd = VectorNormalizeLength (wishveloc);
183         if (wishspd > sv_maxairspeed.value)
184                 wishspd = sv_maxairspeed.value;
185         currentspeed = DotProduct (host_client->edict->fields.server->velocity, wishveloc);
186         addspeed = wishspd - currentspeed;
187         if (addspeed <= 0)
188                 return;
189         accelspeed = (sv_airaccelerate.value < 0 ? sv_accelerate.value : sv_airaccelerate.value)*wishspeed * sv.frametime;
190         if (accelspeed > addspeed)
191                 accelspeed = addspeed;
192
193         for (i=0 ; i<3 ; i++)
194                 host_client->edict->fields.server->velocity[i] += accelspeed*wishveloc[i];
195 }
196
197
198 void DropPunchAngle (void)
199 {
200         float len;
201         prvm_eval_t *val;
202
203         len = VectorNormalizeLength (host_client->edict->fields.server->punchangle);
204
205         len -= 10*sv.frametime;
206         if (len < 0)
207                 len = 0;
208         VectorScale (host_client->edict->fields.server->punchangle, len, host_client->edict->fields.server->punchangle);
209
210         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_punchvector)))
211         {
212                 len = VectorNormalizeLength (val->vector);
213
214                 len -= 20*sv.frametime;
215                 if (len < 0)
216                         len = 0;
217                 VectorScale (val->vector, len, val->vector);
218         }
219 }
220
221 /*
222 ===================
223 SV_FreeMove
224 ===================
225 */
226 void SV_FreeMove (void)
227 {
228         int i;
229         float wishspeed;
230
231         AngleVectors (host_client->edict->fields.server->v_angle, forward, right, up);
232
233         for (i = 0; i < 3; i++)
234                 host_client->edict->fields.server->velocity[i] = forward[i] * cmd.forwardmove + right[i] * cmd.sidemove;
235
236         host_client->edict->fields.server->velocity[2] += cmd.upmove;
237
238         wishspeed = VectorLength(host_client->edict->fields.server->velocity);
239         if (wishspeed > sv_maxspeed.value)
240                 VectorScale(host_client->edict->fields.server->velocity, sv_maxspeed.value / wishspeed, host_client->edict->fields.server->velocity);
241 }
242
243 /*
244 ===================
245 SV_WaterMove
246
247 ===================
248 */
249 void SV_WaterMove (void)
250 {
251         int i;
252         vec3_t wishvel;
253         float speed, newspeed, wishspeed, addspeed, accelspeed, temp;
254
255         // user intentions
256         AngleVectors (host_client->edict->fields.server->v_angle, forward, right, up);
257
258         for (i=0 ; i<3 ; i++)
259                 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
260
261         if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
262                 wishvel[2] -= 60;               // drift towards bottom
263         else
264                 wishvel[2] += cmd.upmove;
265
266         wishspeed = VectorLength(wishvel);
267         if (wishspeed > sv_maxspeed.value)
268         {
269                 temp = sv_maxspeed.value/wishspeed;
270                 VectorScale (wishvel, temp, wishvel);
271                 wishspeed = sv_maxspeed.value;
272         }
273         wishspeed *= 0.7;
274
275         // water friction
276         speed = VectorLength(host_client->edict->fields.server->velocity);
277         if (speed)
278         {
279                 newspeed = speed - sv.frametime * speed * sv_waterfriction.value;
280                 if (newspeed < 0)
281                         newspeed = 0;
282                 temp = newspeed/speed;
283                 VectorScale(host_client->edict->fields.server->velocity, temp, host_client->edict->fields.server->velocity);
284         }
285         else
286                 newspeed = 0;
287
288         // water acceleration
289         if (!wishspeed)
290                 return;
291
292         addspeed = wishspeed - newspeed;
293         if (addspeed <= 0)
294                 return;
295
296         VectorNormalize (wishvel);
297         accelspeed = (sv_wateraccelerate.value < 0 ? sv_accelerate.value : sv_wateraccelerate.value) * wishspeed * sv.frametime;
298         if (accelspeed > addspeed)
299                 accelspeed = addspeed;
300
301         for (i=0 ; i<3 ; i++)
302                 host_client->edict->fields.server->velocity[i] += accelspeed * wishvel[i];
303 }
304
305 void SV_WaterJump (void)
306 {
307         if (sv.time > host_client->edict->fields.server->teleport_time || !host_client->edict->fields.server->waterlevel)
308         {
309                 host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags & ~FL_WATERJUMP;
310                 host_client->edict->fields.server->teleport_time = 0;
311         }
312         host_client->edict->fields.server->velocity[0] = host_client->edict->fields.server->movedir[0];
313         host_client->edict->fields.server->velocity[1] = host_client->edict->fields.server->movedir[1];
314 }
315
316
317 /*
318 ===================
319 SV_AirMove
320
321 ===================
322 */
323 void SV_AirMove (void)
324 {
325         int i;
326         vec3_t wishvel;
327         float fmove, smove, temp;
328
329         // LordHavoc: correct quake movement speed bug when looking up/down
330         wishvel[0] = wishvel[2] = 0;
331         wishvel[1] = host_client->edict->fields.server->angles[1];
332         AngleVectors (wishvel, forward, right, up);
333
334         fmove = cmd.forwardmove;
335         smove = cmd.sidemove;
336
337 // hack to not let you back into teleporter
338         if (sv.time < host_client->edict->fields.server->teleport_time && fmove < 0)
339                 fmove = 0;
340
341         for (i=0 ; i<3 ; i++)
342                 wishvel[i] = forward[i]*fmove + right[i]*smove;
343
344         if ((int)host_client->edict->fields.server->movetype != MOVETYPE_WALK)
345                 wishvel[2] += cmd.upmove;
346
347         VectorCopy (wishvel, wishdir);
348         wishspeed = VectorNormalizeLength(wishdir);
349         if (wishspeed > sv_maxspeed.value)
350         {
351                 temp = sv_maxspeed.value/wishspeed;
352                 VectorScale (wishvel, temp, wishvel);
353                 wishspeed = sv_maxspeed.value;
354         }
355
356         if (host_client->edict->fields.server->movetype == MOVETYPE_NOCLIP)
357         {
358                 // noclip
359                 VectorCopy (wishvel, host_client->edict->fields.server->velocity);
360         }
361         else if (onground && (!sv_gameplayfix_qwplayerphysics.integer || !(host_client->edict->fields.server->button2 || !((int)host_client->edict->fields.server->flags & FL_JUMPRELEASED))))
362         {
363                 SV_UserFriction ();
364                 SV_Accelerate ();
365         }
366         else
367         {
368                 // not on ground, so little effect on velocity
369                 SV_AirAccelerate (wishvel);
370         }
371 }
372
373 /*
374 ===================
375 SV_ClientThink
376
377 the move fields specify an intended velocity in pix/sec
378 the angle fields specify an exact angular motion in degrees
379 ===================
380 */
381 void SV_ClientThink (void)
382 {
383         vec3_t v_angle;
384
385         if (host_client->edict->fields.server->movetype == MOVETYPE_NONE)
386                 return;
387
388         onground = (int)host_client->edict->fields.server->flags & FL_ONGROUND;
389
390         DropPunchAngle ();
391
392         // if dead, behave differently
393         if (host_client->edict->fields.server->health <= 0)
394                 return;
395
396         cmd = host_client->cmd;
397
398         // angles
399         // show 1/3 the pitch angle and all the roll angle
400         VectorAdd (host_client->edict->fields.server->v_angle, host_client->edict->fields.server->punchangle, v_angle);
401         host_client->edict->fields.server->angles[ROLL] = V_CalcRoll (host_client->edict->fields.server->angles, host_client->edict->fields.server->velocity)*4;
402         if (!host_client->edict->fields.server->fixangle)
403         {
404                 host_client->edict->fields.server->angles[PITCH] = -v_angle[PITCH]/3;
405                 host_client->edict->fields.server->angles[YAW] = v_angle[YAW];
406         }
407
408         if ( (int)host_client->edict->fields.server->flags & FL_WATERJUMP )
409         {
410                 SV_WaterJump ();
411                 return;
412         }
413
414         /*
415         // Player is (somehow) outside of the map, or flying, or noclipping
416         if (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP && (host_client->edict->fields.server->movetype == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict)))
417         //if (host_client->edict->fields.server->movetype == MOVETYPE_NOCLIP || host_client->edict->fields.server->movetype == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict))
418         {
419                 SV_FreeMove ();
420                 return;
421         }
422         */
423
424         // walk
425         if ((host_client->edict->fields.server->waterlevel >= 2) && (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP))
426         {
427                 SV_WaterMove ();
428                 return;
429         }
430
431         SV_AirMove ();
432 }
433
434 /*
435 ===================
436 SV_ReadClientMove
437 ===================
438 */
439 qboolean SV_ReadClientMove (void)
440 {
441         qboolean kickplayer = false;
442         int i;
443         double oldmovetime;
444 #ifdef NUM_PING_TIMES
445         double total;
446 #endif
447         usercmd_t *move = &host_client->cmd;
448
449         oldmovetime = move->time;
450
451         // if this move has been applied, clear it, and start accumulating new data
452         if (move->applied)
453                 memset(move, 0, sizeof(*move));
454
455         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
456
457         // read ping time
458         if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5 && sv.protocol != PROTOCOL_DARKPLACES6)
459                 move->sequence = MSG_ReadLong ();
460         move->time = MSG_ReadFloat ();
461         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
462         move->receivetime = sv.time;
463
464         // calculate average ping time
465         host_client->ping = move->receivetime - move->time;
466 #ifdef NUM_PING_TIMES
467         host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = move->receivetime - move->time;
468         host_client->num_pings++;
469         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
470                 total += host_client->ping_times[i];
471         host_client->ping = total / NUM_PING_TIMES;
472 #endif
473
474         // read current angles
475         for (i = 0;i < 3;i++)
476         {
477                 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
478                         move->viewangles[i] = MSG_ReadAngle8i();
479                 else if (sv.protocol == PROTOCOL_DARKPLACES1)
480                         move->viewangles[i] = MSG_ReadAngle16i();
481                 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
482                         move->viewangles[i] = MSG_ReadAngle32f();
483                 else
484                         move->viewangles[i] = MSG_ReadAngle16i();
485         }
486         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
487
488         // read movement
489         move->forwardmove = MSG_ReadCoord16i ();
490         move->sidemove = MSG_ReadCoord16i ();
491         move->upmove = MSG_ReadCoord16i ();
492         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
493
494         // read buttons
495         // be sure to bitwise OR them into the move->buttons because we want to
496         // accumulate button presses from multiple packets per actual move
497         if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
498                 move->buttons |= MSG_ReadByte ();
499         else
500                 move->buttons |= MSG_ReadLong ();
501         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
502
503         // read impulse
504         i = MSG_ReadByte ();
505         if (i)
506                 move->impulse = i;
507         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
508
509         // PRYDON_CLIENTCURSOR
510         if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5)
511         {
512                 // 30 bytes
513                 move->cursor_screen[0] = MSG_ReadShort() * (1.0f / 32767.0f);
514                 move->cursor_screen[1] = MSG_ReadShort() * (1.0f / 32767.0f);
515                 move->cursor_start[0] = MSG_ReadFloat();
516                 move->cursor_start[1] = MSG_ReadFloat();
517                 move->cursor_start[2] = MSG_ReadFloat();
518                 move->cursor_impact[0] = MSG_ReadFloat();
519                 move->cursor_impact[1] = MSG_ReadFloat();
520                 move->cursor_impact[2] = MSG_ReadFloat();
521                 move->cursor_entitynumber = (unsigned short)MSG_ReadShort();
522                 if (move->cursor_entitynumber >= prog->max_edicts)
523                 {
524                         Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
525                         move->cursor_entitynumber = 0;
526                 }
527                 // as requested by FrikaC, cursor_trace_ent is reset to world if the
528                 // entity is free at time of receipt
529                 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
530                         move->cursor_entitynumber = 0;
531                 if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
532         }
533
534         // disable clientside movement prediction in some cases
535         if (ceil((move->receivetime - move->time) * 1000.0) < sv_clmovement_minping.integer)
536                 host_client->clmovement_disable_minpingtimeout = realtime + sv_clmovement_minping_disabletime.value / 1000.0;
537         if (!sv_clmovement_enable.integer || host_client->clmovement_disable_minpingtimeout > realtime)
538                 move->sequence = 0;
539
540         if (!host_client->spawned)
541                 memset(move, 0, sizeof(*move));
542         else if (move->sequence && (float)move->time > (float)sv.time + 0.125f) // add a little fuzz factor due to float precision issues
543         {
544                 Con_DPrintf("client move->time %f > sv.time %f, kicking\n", (float)move->time, (float)sv.time);
545                 // if the client is lying about time, we have definitively detected a
546                 // speed cheat attempt of the worst sort, and we can immediately kick
547                 // the offending player off.
548                 // this fixes the timestamp to prevent a speed cheat from working
549                 move->time = sv.time;
550                 // but we kick the player for good measure
551                 kickplayer = true;
552         }
553         else
554         {
555                 // apply the latest accepted move to the entity fields
556                 host_client->movesequence = move->sequence;
557                 if (host_client->movesequence)
558                 {
559                         double frametime = bound(0, move->time - oldmovetime, 0.1);
560                         double oldframetime = prog->globals.server->frametime;
561                         //if (move->time - oldmovetime >= 0.1001)
562                         //      Con_DPrintf("client move exceeds 100ms!  (time %f -> time %f)\n", oldmovetime, move->time);
563                         prog->globals.server->frametime = frametime;
564                         SV_Physics_ClientEntity(host_client->edict);
565                         prog->globals.server->frametime = oldframetime;
566                 }
567         }
568         return kickplayer;
569 }
570
571 void SV_ApplyClientMove (void)
572 {
573         prvm_eval_t *val;
574         usercmd_t *move = &host_client->cmd;
575
576         if (!move->receivetime)
577                 return;
578
579         // note: a move can be applied multiple times if the client packets are
580         // not coming as often as the physics is executed, and the move must be
581         // applied before running qc each time because the id1 qc had a bug where
582         // it clears self.button2 in PlayerJump, causing pogostick behavior if
583         // moves are not applied every time before calling qc
584         move->applied = true;
585
586         // set the edict fields
587         host_client->edict->fields.server->button0 = move->buttons & 1;
588         host_client->edict->fields.server->button2 = (move->buttons & 2)>>1;
589         if (move->impulse)
590                 host_client->edict->fields.server->impulse = move->impulse;
591         // only send the impulse to qc once
592         move->impulse = 0;
593         VectorCopy(move->viewangles, host_client->edict->fields.server->v_angle);
594         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((move->buttons >> 2) & 1);
595         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((move->buttons >> 3) & 1);
596         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((move->buttons >> 4) & 1);
597         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((move->buttons >> 5) & 1);
598         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((move->buttons >> 6) & 1);
599         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((move->buttons >> 7) & 1);
600         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button9))) val->_float = ((move->buttons >> 11) & 1);
601         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button10))) val->_float = ((move->buttons >> 12) & 1);
602         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button11))) val->_float = ((move->buttons >> 13) & 1);
603         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button12))) val->_float = ((move->buttons >> 14) & 1);
604         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button13))) val->_float = ((move->buttons >> 15) & 1);
605         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button14))) val->_float = ((move->buttons >> 16) & 1);
606         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button15))) val->_float = ((move->buttons >> 17) & 1);
607         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button16))) val->_float = ((move->buttons >> 18) & 1);
608         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_buttonuse))) val->_float = ((move->buttons >> 8) & 1);
609         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_buttonchat))) val->_float = ((move->buttons >> 9) & 1);
610         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_active))) val->_float = ((move->buttons >> 10) & 1);
611         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_movement))) VectorSet(val->vector, move->forwardmove, move->sidemove, move->upmove);
612         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_screen))) VectorCopy(move->cursor_screen, val->vector);
613         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_trace_start))) VectorCopy(move->cursor_start, val->vector);
614         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_trace_endpos))) VectorCopy(move->cursor_impact, val->vector);
615         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_trace_ent))) val->edict = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
616         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ping))) val->_float = host_client->ping * 1000.0;
617 }
618
619 void SV_FrameLost(int framenum)
620 {
621         if (host_client->entitydatabase5)
622                 EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
623 }
624
625 void SV_FrameAck(int framenum)
626 {
627         if (host_client->entitydatabase)
628                 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
629         else if (host_client->entitydatabase4)
630                 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
631         else if (host_client->entitydatabase5)
632                 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
633 }
634
635 /*
636 ===================
637 SV_ReadClientMessage
638 ===================
639 */
640 extern void SV_SendServerinfo(client_t *client);
641 void SV_ReadClientMessage(void)
642 {
643         int cmd, num;
644         char *s;
645
646         //MSG_BeginReading ();
647
648         for(;;)
649         {
650                 if (!host_client->active)
651                 {
652                         // a command caused an error
653                         SV_DropClient (false);
654                         return;
655                 }
656
657                 if (msg_badread)
658                 {
659                         Con_Print("SV_ReadClientMessage: badread\n");
660                         SV_DropClient (false);
661                         return;
662                 }
663
664                 cmd = MSG_ReadByte ();
665                 if (cmd == -1)
666                 {
667                         // end of message
668                         break;
669                 }
670
671                 switch (cmd)
672                 {
673                 default:
674                         Con_Printf("SV_ReadClientMessage: unknown command char %i\n", cmd);
675                         SV_DropClient (false);
676                         return;
677
678                 case clc_nop:
679                         break;
680
681                 case clc_stringcmd:
682                         s = MSG_ReadString ();
683                         if (strncasecmp(s, "spawn", 5) == 0
684                          || strncasecmp(s, "begin", 5) == 0
685                          || strncasecmp(s, "prespawn", 8) == 0)
686                                 Cmd_ExecuteString (s, src_client);
687                         else if (SV_ParseClientCommandQC)
688                         {
689                                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(s);
690                                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
691                                 PRVM_ExecuteProgram ((func_t)(SV_ParseClientCommandQC - prog->functions), "QC function SV_ParseClientCommand is missing");
692                         }
693                         else if (strncasecmp(s, "status", 6) == 0
694                          || strncasecmp(s, "name", 4) == 0
695                          || strncasecmp(s, "say", 3) == 0
696                          || strncasecmp(s, "say_team", 8) == 0
697                          || strncasecmp(s, "tell", 4) == 0
698                          || strncasecmp(s, "color", 5) == 0
699                          || strncasecmp(s, "kill", 4) == 0
700                          || strncasecmp(s, "pause", 5) == 0
701                          || strncasecmp(s, "kick", 4) == 0
702                          || strncasecmp(s, "ping", 4) == 0
703                          || strncasecmp(s, "ban", 3) == 0
704                          || strncasecmp(s, "pmodel", 6) == 0
705                          || strncasecmp(s, "rate", 4) == 0
706                          || strncasecmp(s, "playermodel", 11) == 0
707                          || strncasecmp(s, "playerskin", 10) == 0
708                          || (gamemode == GAME_NEHAHRA && (strncasecmp(s, "max", 3) == 0 || strncasecmp(s, "monster", 7) == 0 || strncasecmp(s, "scrag", 5) == 0 || strncasecmp(s, "gimme", 5) == 0 || strncasecmp(s, "wraith", 6) == 0))
709                          || (gamemode != GAME_NEHAHRA && (strncasecmp(s, "god", 3) == 0 || strncasecmp(s, "notarget", 8) == 0 || strncasecmp(s, "fly", 3) == 0 || strncasecmp(s, "give", 4) == 0 || strncasecmp(s, "noclip", 6) == 0)))
710                                 Cmd_ExecuteString (s, src_client);
711                         else
712                                 Con_Printf("%s tried to %s\n", host_client->name, s);
713                         break;
714
715                 case clc_disconnect:
716                         SV_DropClient (false); // client wants to disconnect
717                         return;
718
719                 case clc_move:
720                         // if ReadClientMove returns true, the client tried to speed cheat
721                         if (SV_ReadClientMove ())
722                                 SV_DropClient (false);
723                         break;
724
725                 case clc_ackframe:
726                         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
727                         num = MSG_ReadLong();
728                         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
729                         if (developer_networkentities.integer >= 1)
730                                 Con_Printf("recv clc_ackframe %i\n", num);
731                         // if the client hasn't progressed through signons yet,
732                         // ignore any clc_ackframes we get (they're probably from the
733                         // previous level)
734                         if (host_client->spawned && host_client->latestframenum < num)
735                         {
736                                 int i;
737                                 for (i = host_client->latestframenum + 1;i < num;i++)
738                                         SV_FrameLost(i);
739                                 SV_FrameAck(num);
740                                 host_client->latestframenum = num;
741                         }
742                         break;
743                 }
744         }
745 }
746