eliminated qbyte type, now uses unsigned char throughout the engine for this purpose
[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"};
25 cvar_t sv_deltacompress = {0, "sv_deltacompress", "1"};
26 cvar_t sv_idealpitchscale = {0, "sv_idealpitchscale","0.8"};
27 cvar_t sv_maxspeed = {CVAR_NOTIFY, "sv_maxspeed", "320"};
28 cvar_t sv_maxairspeed = {0, "sv_maxairspeed", "30"};
29 cvar_t sv_accelerate = {0, "sv_accelerate", "10"};
30
31 static usercmd_t cmd;
32
33
34 /*
35 ===============
36 SV_SetIdealPitch
37 ===============
38 */
39 #define MAX_FORWARD     6
40 void SV_SetIdealPitch (void)
41 {
42         float   angleval, sinval, cosval;
43         trace_t tr;
44         vec3_t  top, bottom;
45         float   z[MAX_FORWARD];
46         int             i, j;
47         int             step, dir, steps;
48
49         if (!((int)host_client->edict->fields.server->flags & FL_ONGROUND))
50                 return;
51
52         angleval = host_client->edict->fields.server->angles[YAW] * M_PI*2 / 360;
53         sinval = sin(angleval);
54         cosval = cos(angleval);
55
56         for (i=0 ; i<MAX_FORWARD ; i++)
57         {
58                 top[0] = host_client->edict->fields.server->origin[0] + cosval*(i+3)*12;
59                 top[1] = host_client->edict->fields.server->origin[1] + sinval*(i+3)*12;
60                 top[2] = host_client->edict->fields.server->origin[2] + host_client->edict->fields.server->view_ofs[2];
61
62                 bottom[0] = top[0];
63                 bottom[1] = top[1];
64                 bottom[2] = top[2] - 160;
65
66                 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, MOVE_NOMONSTERS, host_client->edict);
67                 // if looking at a wall, leave ideal the way is was
68                 if (tr.startsolid)
69                         return;
70
71                 // near a dropoff
72                 if (tr.fraction == 1)
73                         return;
74
75                 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
76         }
77
78         dir = 0;
79         steps = 0;
80         for (j=1 ; j<i ; j++)
81         {
82                 step = z[j] - z[j-1];
83                 if (step > -ON_EPSILON && step < ON_EPSILON)
84                         continue;
85
86                 // mixed changes
87                 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
88                         return;
89
90                 steps++;
91                 dir = step;
92         }
93
94         if (!dir)
95         {
96                 host_client->edict->fields.server->idealpitch = 0;
97                 return;
98         }
99
100         if (steps < 2)
101                 return;
102         host_client->edict->fields.server->idealpitch = -dir * sv_idealpitchscale.value;
103 }
104
105 #if 1
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_accelerate.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_friction.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_accelerate.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 #else
432
433 extern cvar_t cl_rollspeed;
434 extern cvar_t cl_rollangle;
435 void SV_ClientThink(void)
436 {
437         int j;
438         vec3_t wishvel, wishdir, v, v_forward, v_right, v_up, start, stop;
439         float wishspeed, f, limit;
440         trace_t trace;
441
442         if (host_client->edict->fields.server->movetype == MOVETYPE_NONE)
443                 return;
444
445         f = DotProduct(host_client->edict->fields.server->punchangle, host_client->edict->fields.server->punchangle);
446         if (f)
447         {
448                 limit = sqrt(f);
449                 f = (limit - 10 * sv.frametime);
450                 f /= limit;
451                 f = max(0, f);
452                 VectorScale(host_client->edict->fields.server->punchangle, f, host_client->edict->fields.server->punchangle);
453         }
454
455         // if dead, behave differently
456         if (host_client->edict->fields.server->health <= 0)
457                 return;
458
459         AngleVectors(host_client->edict->fields.server->v_angle, v_forward, v_right, v_up);
460         // show 1/3 the pitch angle and all the roll angle
461         f = DotProduct(host_client->edict->fields.server->velocity, v_right) * (1.0 / cl_rollspeed.value);
462         host_client->edict->fields.server->angles[2] = bound(-1, f, 1) * cl_rollangle.value * 4;
463         if (!host_client->edict->fields.server->fixangle)
464         {
465                 host_client->edict->fields.server->angles[0] = (host_client->edict->fields.server->v_angle[0] + host_client->edict->fields.server->punchangle[0]) * -0.333;
466                 host_client->edict->fields.server->angles[1] = host_client->edict->fields.server->v_angle[1] + host_client->edict->fields.server->punchangle[1];
467         }
468
469         if ((int)host_client->edict->fields.server->flags & FL_WATERJUMP)
470         {
471                 host_client->edict->fields.server->velocity[0] = host_client->edict->fields.server->movedir[0];
472                 host_client->edict->fields.server->velocity[1] = host_client->edict->fields.server->movedir[1];
473                 if (sv.time > host_client->edict->fields.server->teleport_time || host_client->edict->fields.server->waterlevel == 0)
474                 {
475                         host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags - ((int)host_client->edict->fields.server->flags & FL_WATERJUMP);
476                         host_client->edict->fields.server->teleport_time = 0;
477                 }
478                 return;
479         }
480
481         // swim
482         if (host_client->edict->fields.server->waterlevel >= 2)
483         if (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP)
484         {
485                 if (host_client->cmd.forwardmove == 0 && host_client->cmd.sidemove == 0 && host_client->cmd.upmove == 0)
486                 {
487                         // drift towards bottom
488                         wishvel[0] = 0;
489                         wishvel[1] = 0;
490                         wishvel[2] = -60;
491                 }
492                 else
493                 {
494                         for (j = 0;j < 3;j++)
495                                 wishvel[j] = v_forward[j] * host_client->cmd.forwardmove + v_right[j] * host_client->cmd.sidemove;
496                         wishvel[2] += host_client->cmd.upmove;
497                 }
498
499                 wishspeed = VectorLength(wishvel);
500                 wishspeed = min(wishspeed, sv_maxspeed.value) * 0.7;
501
502                 // water friction
503                 f = VectorLength(host_client->edict->fields.server->velocity) * (1 - sv.frametime * sv_friction.value);
504                 if (f > 0)
505                         f /= VectorLength(host_client->edict->fields.server->velocity);
506                 else
507                         f = 0;
508                 VectorScale(host_client->edict->fields.server->velocity, f, host_client->edict->fields.server->velocity);
509
510                 // water acceleration
511                 if (wishspeed <= f)
512                         return;
513
514                 f = wishspeed - f;
515                 limit = sv_accelerate.value * wishspeed * sv.frametime;
516                 if (f > limit)
517                         f = limit;
518                 limit = VectorLength(wishvel);
519                 if (limit)
520                         f /= limit;
521                 VectorMA(host_client->edict->fields.server->velocity, f, wishvel, host_client->edict->fields.server->velocity);
522                 return;
523         }
524
525         // if not flying, move horizontally only
526         if (host_client->edict->fields.server->movetype != MOVETYPE_FLY)
527         {
528                 VectorClear(wishvel);
529                 wishvel[1] = host_client->edict->fields.server->v_angle[1];
530                 AngleVectors(wishvel, v_forward, v_right, v_up);
531         }
532
533         // hack to not let you back into teleporter
534         VectorScale(v_right, host_client->cmd.sidemove, wishvel);
535         if (sv.time >= host_client->edict->fields.server->teleport_time || host_client->cmd.forwardmove > 0)
536                 VectorMA(wishvel, host_client->cmd.forwardmove, v_forward, wishvel);
537         if (host_client->edict->fields.server->movetype != MOVETYPE_WALK)
538                 wishvel[2] += cmd.upmove;
539
540         VectorCopy(wishvel, wishdir);
541         VectorNormalize(wishdir);
542         wishspeed = VectorLength(wishvel);
543         if (wishspeed > sv_maxspeed.value)
544                 wishspeed = sv_maxspeed.value;
545
546         if (host_client->edict->fields.server->movetype == MOVETYPE_NOCLIP || host_client->edict->fields.server->movetype == MOVETYPE_FLY)
547         {
548                 VectorScale(wishdir, wishspeed, host_client->edict->fields.server->velocity);
549                 return;
550         }
551
552         if ((int)host_client->edict->fields.server->flags & FL_ONGROUND) // walking
553         {
554                 // friction
555                 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];
556                 if (f)
557                 {
558                         f = sqrt(f);
559                         VectorCopy(host_client->edict->fields.server->velocity, v);
560                         v[2] = 0;
561
562                         // if the leading edge is over a dropoff, increase friction
563                         limit = 16.0f / f;
564                         VectorMA(host_client->edict->fields.server->origin, limit, v, v);
565                         v[2] += host_client->edict->fields.server->mins[2];
566
567                         VectorCopy(v, start);
568                         VectorCopy(v, stop);
569                         stop[2] -= 34;
570                         trace = SV_Move(start, vec3_origin, vec3_origin, stop, MOVE_NOMONSTERS, host_client->edict);
571
572                         // apply friction
573                         if (f < sv_stopspeed.value)
574                                 f = sv_stopspeed.value / f;
575                         else
576                                 f = 1;
577                         if (trace.fraction == 1)
578                                 f *= sv_edgefriction.value;
579                         f = 1 - sv.frametime * f * sv_friction.value;
580
581                         if (f < 0)
582                                 f = 0;
583                         VectorScale(host_client->edict->fields.server->velocity, f, host_client->edict->fields.server->velocity);
584                 }
585         }
586         else // airborn
587                 wishspeed = min(wishspeed, 30);
588
589         // ground or air acceleration
590         f = wishspeed - DotProduct(host_client->edict->fields.server->velocity, wishdir);
591         if (f > 0)
592         {
593                 limit = sv_accelerate.value * sv.frametime * wishspeed;
594                 if (f > limit)
595                         f = limit;
596                 VectorMA(host_client->edict->fields.server->velocity, f, wishdir, host_client->edict->fields.server->velocity);
597         }
598 }
599 #endif
600
601 /*
602 ===================
603 SV_ReadClientMove
604 ===================
605 */
606 void SV_ApplyClientMove (void);
607 void SV_ReadClientMove (void)
608 {
609         int i;
610         double oldmovetime;
611         usercmd_t *move = &host_client->cmd;
612
613         oldmovetime = move->time;
614         memset(move, 0, sizeof(usercmd_t));
615
616         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
617
618         // read ping time
619         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)
620                 move->sequence = MSG_ReadLong ();
621         move->time = MSG_ReadFloat ();
622         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
623         move->receivetime = sv.time;
624
625         // read current angles
626         for (i = 0;i < 3;i++)
627         {
628                 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
629                         move->viewangles[i] = MSG_ReadAngle8i();
630                 else if (sv.protocol == PROTOCOL_DARKPLACES1)
631                         move->viewangles[i] = MSG_ReadAngle16i();
632                 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
633                         move->viewangles[i] = MSG_ReadAngle32f();
634                 else
635                         move->viewangles[i] = MSG_ReadAngle16i();
636         }
637         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
638
639         // read movement
640         move->forwardmove = MSG_ReadCoord16i ();
641         move->sidemove = MSG_ReadCoord16i ();
642         move->upmove = MSG_ReadCoord16i ();
643         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
644
645         // read buttons
646         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)
647                 move->buttons = MSG_ReadByte ();
648         else
649                 move->buttons = MSG_ReadLong ();
650         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
651
652         // read impulse
653         move->impulse = MSG_ReadByte ();
654         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
655
656         // PRYDON_CLIENTCURSOR
657         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)
658         {
659                 // 30 bytes
660                 move->cursor_screen[0] = MSG_ReadShort() * (1.0f / 32767.0f);
661                 move->cursor_screen[1] = MSG_ReadShort() * (1.0f / 32767.0f);
662                 move->cursor_start[0] = MSG_ReadFloat();
663                 move->cursor_start[1] = MSG_ReadFloat();
664                 move->cursor_start[2] = MSG_ReadFloat();
665                 move->cursor_impact[0] = MSG_ReadFloat();
666                 move->cursor_impact[1] = MSG_ReadFloat();
667                 move->cursor_impact[2] = MSG_ReadFloat();
668                 move->cursor_entitynumber = (unsigned short)MSG_ReadShort();
669                 if (move->cursor_entitynumber >= prog->max_edicts)
670                 {
671                         Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
672                         move->cursor_entitynumber = 0;
673                 }
674                 // as requested by FrikaC, cursor_trace_ent is reset to world if the
675                 // entity is free at time of receipt
676                 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
677                         move->cursor_entitynumber = 0;
678                 if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
679         }
680
681         if (!host_client->spawned)
682                 memset(move, 0, sizeof(*move));
683         else
684         {
685                 // apply the latest accepted move to the entity fields
686                 SV_ApplyClientMove();
687                 host_client->movesequence = move->sequence;
688                 if (host_client->movesequence)
689                 {
690                         double frametime = move->time - oldmovetime;
691                         double oldframetime = prog->globals.server->frametime;
692                         if (frametime > 0.1)
693                                 frametime = 0.1;
694                         prog->globals.server->frametime = frametime;
695                         SV_Physics_ClientEntity(host_client->edict);
696                         prog->globals.server->frametime = oldframetime;
697                 }
698         }
699 }
700
701 void SV_ApplyClientMove (void)
702 {
703 #ifdef NUM_PING_TIMES
704         int i;
705         float total;
706 #endif
707         prvm_eval_t *val;
708         usercmd_t *move = &host_client->cmd;
709
710         // calculate average ping time
711         host_client->ping = move->receivetime - move->time;
712 #ifdef NUM_PING_TIMES
713         host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = move->receivetime - move->time;
714         host_client->num_pings++;
715         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
716                 total += host_client->ping_times[i];
717         host_client->ping = total / NUM_PING_TIMES;
718 #endif
719
720         // set the edict fields
721         host_client->edict->fields.server->button0 = move->buttons & 1;
722         host_client->edict->fields.server->button2 = (move->buttons & 2)>>1;
723         if (move->impulse)
724                 host_client->edict->fields.server->impulse = move->impulse;
725         VectorCopy(move->viewangles, host_client->edict->fields.server->v_angle);
726         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((move->buttons >> 2) & 1);
727         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((move->buttons >> 3) & 1);
728         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((move->buttons >> 4) & 1);
729         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((move->buttons >> 5) & 1);
730         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((move->buttons >> 6) & 1);
731         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((move->buttons >> 7) & 1);
732         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_buttonuse))) val->_float = ((move->buttons >> 8) & 1);
733         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_buttonchat))) val->_float = ((move->buttons >> 9) & 1);
734         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_active))) val->_float = ((move->buttons >> 10) & 1);
735         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_movement))) VectorSet(val->vector, move->forwardmove, move->sidemove, move->upmove);
736         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_screen))) VectorCopy(move->cursor_screen, val->vector);
737         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_trace_start))) VectorCopy(move->cursor_start, val->vector);
738         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_trace_endpos))) VectorCopy(move->cursor_impact, val->vector);
739         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_cursor_trace_ent))) val->edict = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
740         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ping))) val->_float = host_client->ping * 1000.0;
741 }
742
743 void SV_FrameLost(int framenum)
744 {
745         if (host_client->entitydatabase5)
746                 EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
747 }
748
749 void SV_FrameAck(int framenum)
750 {
751         if (host_client->entitydatabase)
752                 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
753         else if (host_client->entitydatabase4)
754                 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
755         else if (host_client->entitydatabase5)
756                 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
757 }
758
759 /*
760 ===================
761 SV_ReadClientMessage
762 ===================
763 */
764 extern void SV_SendServerinfo(client_t *client);
765 void SV_ReadClientMessage(void)
766 {
767         int cmd, num;
768         char *s;
769
770         //MSG_BeginReading ();
771
772         for(;;)
773         {
774                 if (!host_client->active)
775                 {
776                         // a command caused an error
777                         SV_DropClient (false);
778                         return;
779                 }
780
781                 if (msg_badread)
782                 {
783                         Con_Print("SV_ReadClientMessage: badread\n");
784                         SV_DropClient (false);
785                         return;
786                 }
787
788                 cmd = MSG_ReadChar ();
789                 if (cmd == -1)
790                 {
791                         // end of message
792                         break;
793                 }
794
795                 switch (cmd)
796                 {
797                 default:
798                         Con_Printf("SV_ReadClientMessage: unknown command char %i\n", cmd);
799                         SV_DropClient (false);
800                         return;
801
802                 case clc_nop:
803                         break;
804
805                 case clc_stringcmd:
806                         s = MSG_ReadString ();
807                         if (strncasecmp(s, "spawn", 5) == 0
808                          || strncasecmp(s, "begin", 5) == 0
809                          || strncasecmp(s, "prespawn", 8) == 0)
810                                 Cmd_ExecuteString (s, src_client);
811                         else if (SV_ParseClientCommandQC)
812                         {
813                                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(s);
814                                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
815                                 PRVM_ExecuteProgram ((func_t)(SV_ParseClientCommandQC - prog->functions), "QC function SV_ParseClientCommand is missing");
816                         }
817                         else if (strncasecmp(s, "status", 6) == 0
818                          || strncasecmp(s, "name", 4) == 0
819                          || strncasecmp(s, "say", 3) == 0
820                          || strncasecmp(s, "say_team", 8) == 0
821                          || strncasecmp(s, "tell", 4) == 0
822                          || strncasecmp(s, "color", 5) == 0
823                          || strncasecmp(s, "kill", 4) == 0
824                          || strncasecmp(s, "pause", 5) == 0
825                          || strncasecmp(s, "kick", 4) == 0
826                          || strncasecmp(s, "ping", 4) == 0
827                          || strncasecmp(s, "ban", 3) == 0
828                          || strncasecmp(s, "pmodel", 6) == 0
829                          || strncasecmp(s, "rate", 4) == 0
830                          || strncasecmp(s, "playermodel", 11) == 0
831                          || strncasecmp(s, "playerskin", 10) == 0
832                          || (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))
833                          || (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)))
834                                 Cmd_ExecuteString (s, src_client);
835                         else
836                                 Con_Printf("%s tried to %s\n", host_client->name, s);
837                         break;
838
839                 case clc_disconnect:
840                         SV_DropClient (false); // client wants to disconnect
841                         return;
842
843                 case clc_move:
844                         SV_ReadClientMove ();
845                         break;
846
847                 case clc_ackframe:
848                         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
849                         num = MSG_ReadLong();
850                         if (msg_badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
851                         if (developer_networkentities.integer >= 1)
852                                 Con_Printf("recv clc_ackframe %i\n", num);
853                         if (host_client->latestframenum < num)
854                         {
855                                 int i;
856                                 for (i = host_client->latestframenum + 1;i < num;i++)
857                                         SV_FrameLost(i);
858                                 SV_FrameAck(num);
859                                 host_client->latestframenum = num;
860                         }
861                         break;
862                 }
863         }
864 }
865