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