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