added in_pitch_min and in_pitch_max cvars to limit pitch (default: -90 to +90)
[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 edict_t *sv_player;
25
26 cvar_t  sv_edgefriction = {0, "edgefriction", "2"};
27 cvar_t  sv_predict = {0, "sv_predict", "1"};
28 cvar_t  sv_deltacompress = {0, "sv_deltacompress", "1"};
29 cvar_t  sv_idealpitchscale = {0, "sv_idealpitchscale","0.8"};
30 cvar_t  sv_maxspeed = {CVAR_NOTIFY, "sv_maxspeed", "320"};
31 cvar_t  sv_accelerate = {0, "sv_accelerate", "10"};
32
33 static  vec3_t          forward, right, up;
34
35 vec3_t  wishdir;
36 float   wishspeed;
37
38 // world
39 float   *angles;
40 float   *origin;
41 float   *velocity;
42
43 qboolean        onground;
44
45 usercmd_t       cmd;
46
47
48 /*
49 ===============
50 SV_SetIdealPitch
51 ===============
52 */
53 #define MAX_FORWARD     6
54 void SV_SetIdealPitch (void)
55 {
56         float   angleval, sinval, cosval;
57         trace_t tr;
58         vec3_t  top, bottom;
59         float   z[MAX_FORWARD];
60         int             i, j;
61         int             step, dir, steps;
62
63         if (!((int)sv_player->v.flags & FL_ONGROUND))
64                 return;
65                 
66         angleval = sv_player->v.angles[YAW] * M_PI*2 / 360;
67         sinval = sin(angleval);
68         cosval = cos(angleval);
69
70         for (i=0 ; i<MAX_FORWARD ; i++)
71         {
72                 top[0] = sv_player->v.origin[0] + cosval*(i+3)*12;
73                 top[1] = sv_player->v.origin[1] + sinval*(i+3)*12;
74                 top[2] = sv_player->v.origin[2] + sv_player->v.view_ofs[2];
75                 
76                 bottom[0] = top[0];
77                 bottom[1] = top[1];
78                 bottom[2] = top[2] - 160;
79
80                 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, MOVE_NOMONSTERS, sv_player);
81                 if (tr.allsolid)
82                         return; // looking at a wall, leave ideal the way is was
83
84                 if (tr.fraction == 1)
85                         return; // near a dropoff
86                 
87                 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
88         }
89         
90         dir = 0;
91         steps = 0;
92         for (j=1 ; j<i ; j++)
93         {
94                 step = z[j] - z[j-1];
95                 if (step > -ON_EPSILON && step < ON_EPSILON)
96                         continue;
97
98                 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
99                         return;         // mixed changes
100
101                 steps++;        
102                 dir = step;
103         }
104         
105         if (!dir)
106         {
107                 sv_player->v.idealpitch = 0;
108                 return;
109         }
110         
111         if (steps < 2)
112                 return;
113         sv_player->v.idealpitch = -dir * sv_idealpitchscale.value;
114 }
115
116
117 /*
118 ==================
119 SV_UserFriction
120
121 ==================
122 */
123 void SV_UserFriction (void)
124 {
125         float   *vel;
126         float   speed, newspeed, control;
127         vec3_t  start, stop;
128         float   friction;
129         trace_t trace;
130         
131         vel = velocity;
132         
133         speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1]);
134         if (!speed)
135                 return;
136
137 // if the leading edge is over a dropoff, increase friction
138         start[0] = stop[0] = origin[0] + vel[0]/speed*16;
139         start[1] = stop[1] = origin[1] + vel[1]/speed*16;
140         start[2] = origin[2] + sv_player->v.mins[2];
141         stop[2] = start[2] - 34;
142
143         trace = SV_Move (start, vec3_origin, vec3_origin, stop, MOVE_NOMONSTERS, sv_player);
144
145         if (trace.fraction == 1.0)
146                 friction = sv_friction.value*sv_edgefriction.value;
147         else
148                 friction = sv_friction.value;
149
150 // apply friction       
151         control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
152         newspeed = speed - sv.frametime*control*friction;
153         
154         if (newspeed < 0)
155                 newspeed = 0;
156         else
157                 newspeed /= speed;
158
159         vel[0] = vel[0] * newspeed;
160         vel[1] = vel[1] * newspeed;
161         vel[2] = vel[2] * newspeed;
162 }
163
164 /*
165 ==============
166 SV_Accelerate
167 ==============
168 */
169 #if 0
170 void SV_Accelerate (vec3_t wishvel)
171 {
172         int                     i;
173         float           addspeed, accelspeed;
174         vec3_t          pushvec;
175
176         if (wishspeed == 0)
177                 return;
178
179         VectorSubtract (wishvel, velocity, pushvec);
180         addspeed = VectorNormalize (pushvec);
181
182         accelspeed = sv_accelerate.value*sv.frametime*addspeed;
183         if (accelspeed > addspeed)
184                 accelspeed = addspeed;
185         
186         for (i=0 ; i<3 ; i++)
187                 velocity[i] += accelspeed*pushvec[i];   
188 }
189 #endif
190 void SV_Accelerate (void)
191 {
192         int                     i;
193         float           addspeed, accelspeed, currentspeed;
194
195         currentspeed = DotProduct (velocity, wishdir);
196         addspeed = wishspeed - currentspeed;
197         if (addspeed <= 0)
198                 return;
199         accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
200         if (accelspeed > addspeed)
201                 accelspeed = addspeed;
202         
203         for (i=0 ; i<3 ; i++)
204                 velocity[i] += accelspeed*wishdir[i];   
205 }
206
207 void SV_AirAccelerate (vec3_t wishveloc)
208 {
209         int                     i;
210         float           addspeed, wishspd, accelspeed, currentspeed;
211                 
212         wishspd = VectorNormalizeLength (wishveloc);
213         if (wishspd > 30)
214                 wishspd = 30;
215         currentspeed = DotProduct (velocity, wishveloc);
216         addspeed = wishspd - currentspeed;
217         if (addspeed <= 0)
218                 return;
219 //      accelspeed = sv_accelerate.value * sv.frametime;
220         accelspeed = sv_accelerate.value*wishspeed * sv.frametime;
221         if (accelspeed > addspeed)
222                 accelspeed = addspeed;
223         
224         for (i=0 ; i<3 ; i++)
225                 velocity[i] += accelspeed*wishveloc[i]; 
226 }
227
228
229 void DropPunchAngle (void)
230 {
231         float   len;
232         eval_t  *val;
233         
234         len = VectorNormalizeLength (sv_player->v.punchangle);
235
236         len -= 10*sv.frametime;
237         if (len < 0)
238                 len = 0;
239         VectorScale (sv_player->v.punchangle, len, sv_player->v.punchangle);
240         
241         if ((val = GETEDICTFIELDVALUE(sv_player, eval_punchvector)))
242         {
243                 len = VectorNormalizeLength (val->vector);
244                 
245                 len -= 20*sv.frametime;
246                 if (len < 0)
247                         len = 0;
248                 VectorScale (val->vector, len, val->vector);
249         }
250 }
251
252 /*
253 ===================
254 SV_WaterMove
255
256 ===================
257 */
258 void SV_WaterMove (void)
259 {
260         int             i;
261         vec3_t  wishvel;
262         float   speed, newspeed, wishspeed, addspeed, accelspeed, temp;
263
264 //
265 // user intentions
266 //
267         AngleVectors (sv_player->v.v_angle, forward, right, up);
268
269         for (i=0 ; i<3 ; i++)
270                 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
271
272         if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
273                 wishvel[2] -= 60;               // drift towards bottom
274         else
275                 wishvel[2] += cmd.upmove;
276
277         wishspeed = Length(wishvel);
278         if (wishspeed > sv_maxspeed.value)
279         {
280                 temp = sv_maxspeed.value/wishspeed;
281                 VectorScale (wishvel, temp, wishvel);
282                 wishspeed = sv_maxspeed.value;
283         }
284         wishspeed *= 0.7;
285
286 //
287 // water friction
288 //
289         speed = Length (velocity);
290         if (speed)
291         {
292                 newspeed = speed - sv.frametime * speed * sv_friction.value;
293                 if (newspeed < 0)
294                         newspeed = 0;
295                 temp = newspeed/speed;
296                 VectorScale (velocity, temp, velocity);
297         }
298         else
299                 newspeed = 0;
300         
301 //
302 // water acceleration
303 //
304         if (!wishspeed)
305                 return;
306
307         addspeed = wishspeed - newspeed;
308         if (addspeed <= 0)
309                 return;
310
311         VectorNormalize (wishvel);
312         accelspeed = sv_accelerate.value * wishspeed * sv.frametime;
313         if (accelspeed > addspeed)
314                 accelspeed = addspeed;
315
316         for (i=0 ; i<3 ; i++)
317                 velocity[i] += accelspeed * wishvel[i];
318 }
319
320 void SV_WaterJump (void)
321 {
322         if (sv.time > sv_player->v.teleport_time
323         || !sv_player->v.waterlevel)
324         {
325                 sv_player->v.flags = (int)sv_player->v.flags & ~FL_WATERJUMP;
326                 sv_player->v.teleport_time = 0;
327         }
328         sv_player->v.velocity[0] = sv_player->v.movedir[0];
329         sv_player->v.velocity[1] = sv_player->v.movedir[1];
330 }
331
332
333 /*
334 ===================
335 SV_AirMove
336
337 ===================
338 */
339 void SV_AirMove (void)
340 {
341         int                     i;
342         vec3_t          wishvel;
343         float           fmove, smove, temp;
344
345         // LordHavoc: correct quake movement speed bug when looking up/down
346         wishvel[0] = wishvel[2] = 0;
347         wishvel[1] = sv_player->v.angles[1];
348         AngleVectors (wishvel, forward, right, up);
349 //      AngleVectors (sv_player->v.angles, forward, right, up);
350
351         fmove = cmd.forwardmove;
352         smove = cmd.sidemove;
353         
354 // hack to not let you back into teleporter
355         if (sv.time < sv_player->v.teleport_time && fmove < 0)
356                 fmove = 0;
357                 
358         for (i=0 ; i<3 ; i++)
359                 wishvel[i] = forward[i]*fmove + right[i]*smove;
360
361         if ( (int)sv_player->v.movetype != MOVETYPE_WALK)
362                 wishvel[2] = cmd.upmove;
363         else
364                 wishvel[2] = 0;
365
366         VectorCopy (wishvel, wishdir);
367         wishspeed = VectorNormalizeLength(wishdir);
368         if (wishspeed > sv_maxspeed.value)
369         {
370                 temp = sv_maxspeed.value/wishspeed;
371                 VectorScale (wishvel, temp, wishvel);
372                 wishspeed = sv_maxspeed.value;
373         }
374         
375         if ( sv_player->v.movetype == MOVETYPE_NOCLIP)
376         {       // noclip
377                 VectorCopy (wishvel, velocity);
378         }
379         else if ( onground )
380         {
381                 SV_UserFriction ();
382                 SV_Accelerate ();
383         }
384         else
385         {       // not on ground, so little effect on velocity
386                 SV_AirAccelerate (wishvel);
387         }               
388 }
389
390 /*
391 ===================
392 SV_ClientThink
393
394 the move fields specify an intended velocity in pix/sec
395 the angle fields specify an exact angular motion in degrees
396 ===================
397 */
398 void SV_ClientThink (void)
399 {
400         vec3_t          v_angle;
401
402         if (sv_player->v.movetype == MOVETYPE_NONE)
403                 return;
404         
405         onground = (int)sv_player->v.flags & FL_ONGROUND;
406
407         origin = sv_player->v.origin;
408         velocity = sv_player->v.velocity;
409
410         DropPunchAngle ();
411         
412 //
413 // if dead, behave differently
414 //
415         if (sv_player->v.health <= 0)
416                 return;
417
418 //
419 // angles
420 // show 1/3 the pitch angle and all the roll angle
421         cmd = host_client->cmd;
422         angles = sv_player->v.angles;
423         
424         VectorAdd (sv_player->v.v_angle, sv_player->v.punchangle, v_angle);
425         angles[ROLL] = V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4;
426         if (!sv_player->v.fixangle)
427         {
428                 // LordHavoc: pitch was ugly to begin with...  removed except in water
429                 if (sv_player->v.waterlevel >= 2)
430                         angles[PITCH] = -v_angle[PITCH]/3;
431                 else
432                         angles[PITCH] = 0;
433                 angles[YAW] = v_angle[YAW];
434         }
435
436         if ( (int)sv_player->v.flags & FL_WATERJUMP )
437         {
438                 SV_WaterJump ();
439                 return;
440         }
441 //
442 // walk
443 //
444         if ( (sv_player->v.waterlevel >= 2)     && (sv_player->v.movetype != MOVETYPE_NOCLIP) )
445         {
446                 SV_WaterMove ();
447                 return;
448         }
449
450         SV_AirMove ();
451 }
452
453
454 /*
455 ===================
456 SV_ReadClientMove
457 ===================
458 */
459 void SV_ReadClientMove (usercmd_t *move)
460 {
461         int             i;
462         vec3_t  angle;
463         int             bits;
464         eval_t  *val;
465         float   total;
466
467 // read ping time
468         host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = sv.time - MSG_ReadFloat ();
469         host_client->num_pings++;
470         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
471                 total += host_client->ping_times[i];
472         host_client->ping = total / NUM_PING_TIMES; // can be used for prediction
473         host_client->latency = 0;
474         if (sv_predict.integer && (svs.maxclients > 1) && (!sv.paused)) // if paused or a local game, don't predict
475                 host_client->latency = host_client->ping;
476         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_ping)))
477                 val->_float = host_client->ping * 1000.0;
478
479 // read current angles
480         // dpprotocol version 2
481         for (i = 0;i < 3;i++)
482                 angle[i] = MSG_ReadFloat ();
483
484         VectorCopy (angle, host_client->edict->v.v_angle);
485
486 // read movement
487         move->forwardmove = MSG_ReadShort ();
488         move->sidemove = MSG_ReadShort ();
489         move->upmove = MSG_ReadShort ();
490         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_movement)))
491         {
492                 val->vector[0] = move->forwardmove;
493                 val->vector[1] = move->sidemove;
494                 val->vector[2] = move->upmove;
495         }
496
497 // read buttons
498         bits = MSG_ReadByte ();
499         host_client->edict->v.button0 = bits & 1;
500         host_client->edict->v.button2 = (bits & 2)>>1;
501         // LordHavoc: added 6 new buttons
502         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((bits >> 2) & 1);
503         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((bits >> 3) & 1);
504         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((bits >> 4) & 1);
505         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((bits >> 5) & 1);
506         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((bits >> 6) & 1);
507         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((bits >> 7) & 1);
508
509         i = MSG_ReadByte ();
510         if (i)
511                 host_client->edict->v.impulse = i;
512 }
513
514 /*
515 ===================
516 SV_ReadClientMessage
517
518 Returns false if the client should be killed
519 ===================
520 */
521 void SV_SendServerinfo (client_t *client);
522 qboolean SV_ReadClientMessage (void)
523 {
524         int             ret;
525         int             cmd;
526         char            *s;
527
528         do
529         {
530 nextmsg:
531                 ret = NET_GetMessage (host_client->netconnection);
532                 if (ret == -1)
533                 {
534                         Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n");
535                         return false;
536                 }
537                 if (!ret)
538                         return true;
539
540                 MSG_BeginReading ();
541
542                 while (1)
543                 {
544                         if (!host_client->active)
545                                 return false;   // a command caused an error
546
547                         if (msg_badread)
548                         {
549                                 Sys_Printf ("SV_ReadClientMessage: badread\n");
550                                 return false;
551                         }
552
553                         cmd = MSG_ReadChar ();
554
555                         if (cmd != -1 && host_client->waitingforconnect)
556                         {
557                                 host_client->waitingforconnect = false;
558                                 host_client->sendserverinfo = true;
559                         }
560
561                         switch (cmd)
562                         {
563                         case -1:
564                                 goto nextmsg;           // end of message
565
566                         default:
567                                 Sys_Printf ("SV_ReadClientMessage: unknown command char %i\n", cmd);
568                                 return false;
569
570                         case clc_nop:
571 //                              Sys_Printf ("clc_nop\n");
572                                 break;
573
574                         case clc_stringcmd:
575                                 s = MSG_ReadString ();
576                                 ret = 0;
577                                 if (Q_strncasecmp(s, "status", 6) == 0
578                                  || Q_strncasecmp(s, "name", 4) == 0
579                                  || Q_strncasecmp(s, "say", 3) == 0
580                                  || Q_strncasecmp(s, "say_team", 8) == 0
581                                  || Q_strncasecmp(s, "tell", 4) == 0
582                                  || Q_strncasecmp(s, "color", 5) == 0
583                                  || Q_strncasecmp(s, "kill", 4) == 0
584                                  || Q_strncasecmp(s, "pause", 5) == 0
585                                  || Q_strncasecmp(s, "spawn", 5) == 0
586                                  || Q_strncasecmp(s, "begin", 5) == 0
587                                  || Q_strncasecmp(s, "prespawn", 8) == 0
588                                  || Q_strncasecmp(s, "kick", 4) == 0
589                                  || Q_strncasecmp(s, "ping", 4) == 0
590                                  || Q_strncasecmp(s, "ban", 3) == 0
591                                  || Q_strncasecmp(s, "pmodel", 6) == 0
592                                  || (gamemode == GAME_NEHAHRA && (Q_strncasecmp(s, "max", 3) == 0 || Q_strncasecmp(s, "monster", 7) == 0 || Q_strncasecmp(s, "scrag", 5) == 0 || Q_strncasecmp(s, "gimme", 5) == 0 || Q_strncasecmp(s, "wraith", 6) == 0))
593                                  || (gamemode != GAME_NEHAHRA && (Q_strncasecmp(s, "god", 3) == 0 || Q_strncasecmp(s, "notarget", 8) == 0 || Q_strncasecmp(s, "fly", 3) == 0 || Q_strncasecmp(s, "give", 4) == 0 || Q_strncasecmp(s, "noclip", 6) == 0)))
594                                 {
595                                         ret = 1;
596                                         Cmd_ExecuteString (s, src_client);
597                                 }
598                                 else
599                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
600                                 /*
601                                 if (ret == 2)
602                                         Cbuf_InsertText (s);
603                                 else if (ret == 1)
604                                         Cmd_ExecuteString (s, src_client);
605                                 else
606                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
607                                 */
608                                 break;
609
610                         case clc_disconnect:
611 //                              Sys_Printf ("SV_ReadClientMessage: client disconnected\n");
612                                 return false;
613
614                         case clc_move:
615                                 SV_ReadClientMove (&host_client->cmd);
616                                 break;
617
618                         case clc_ackentities:
619                                 EntityFrame_AckFrame(&host_client->entitydatabase, MSG_ReadLong());
620                                 break;
621                         }
622                 }
623         } while (ret == 1);
624
625         return true;
626 }
627
628
629 /*
630 ==================
631 SV_RunClients
632 ==================
633 */
634 void SV_RunClients (void)
635 {
636         int                             i;
637
638         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
639         {
640                 if (!host_client->active)
641                         continue;
642         
643                 sv_player = host_client->edict;
644
645                 if (!SV_ReadClientMessage ())
646                 {
647                         SV_DropClient (false);  // client misbehaved...
648                         continue;
649                 }
650
651                 if (!host_client->spawned)
652                 {
653                 // clear client movement until a new packet is received
654                         memset (&host_client->cmd, 0, sizeof(host_client->cmd));
655                         continue;
656                 }
657
658 // always pause in single player if in console or menus
659                 if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
660                 {
661                         // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
662                         if (SV_PlayerPhysicsQC)
663                         {
664                                 pr_global_struct->time = sv.time;
665                                 pr_global_struct->self = EDICT_TO_PROG(sv_player);
666                                 PR_ExecuteProgram ((func_t)(SV_PlayerPhysicsQC - pr_functions), "");
667                         }
668                         else
669                                 SV_ClientThink ();
670                 }
671         }
672 }
673