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