]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sv_user.c
9bf6c2189efca730953da76f9b5726296965e395
[xonotic/darkplaces.git] / sv_user.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // sv_user.c -- server code for moving users
21
22 #include "quakedef.h"
23
24 edict_t *sv_player;
25
26 cvar_t  sv_edgefriction = {"edgefriction", "2"};
27 cvar_t  sv_predict = {"sv_predict", "1"};
28 cvar_t  sv_deltacompress = {"sv_deltacompress", "1"};
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 - sv.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*sv.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*sv.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 * sv.frametime;
221         accelspeed = sv_accelerate.value*wishspeed * sv.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         eval_t  *val;
234         
235         len = VectorNormalizeLength (sv_player->v.punchangle);
236         
237         len -= 10*sv.frametime;
238         if (len < 0)
239                 len = 0;
240         VectorScale (sv_player->v.punchangle, len, sv_player->v.punchangle);
241         
242         if ((val = GETEDICTFIELDVALUE(sv_player, eval_punchvector)))
243         {
244                 len = VectorNormalizeLength (val->vector);
245                 
246                 len -= 20*sv.frametime;
247                 if (len < 0)
248                         len = 0;
249                 VectorScale (val->vector, len, val->vector);
250         }
251 }
252
253 /*
254 ===================
255 SV_WaterMove
256
257 ===================
258 */
259 void SV_WaterMove (void)
260 {
261         int             i;
262         vec3_t  wishvel;
263         float   speed, newspeed, wishspeed, addspeed, accelspeed, temp;
264
265 //
266 // user intentions
267 //
268         AngleVectors (sv_player->v.v_angle, forward, right, up);
269
270         for (i=0 ; i<3 ; i++)
271                 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
272
273         if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
274                 wishvel[2] -= 60;               // drift towards bottom
275         else
276                 wishvel[2] += cmd.upmove;
277
278         wishspeed = Length(wishvel);
279         if (wishspeed > sv_maxspeed.value)
280         {
281                 temp = sv_maxspeed.value/wishspeed;
282                 VectorScale (wishvel, temp, wishvel);
283                 wishspeed = sv_maxspeed.value;
284         }
285         wishspeed *= 0.7;
286
287 //
288 // water friction
289 //
290         speed = Length (velocity);
291         if (speed)
292         {
293                 newspeed = speed - sv.frametime * speed * sv_friction.value;
294                 if (newspeed < 0)
295                         newspeed = 0;
296                 temp = newspeed/speed;
297                 VectorScale (velocity, temp, velocity);
298         }
299         else
300                 newspeed = 0;
301         
302 //
303 // water acceleration
304 //
305         if (!wishspeed)
306                 return;
307
308         addspeed = wishspeed - newspeed;
309         if (addspeed <= 0)
310                 return;
311
312         VectorNormalize (wishvel);
313         accelspeed = sv_accelerate.value * wishspeed * sv.frametime;
314         if (accelspeed > addspeed)
315                 accelspeed = addspeed;
316
317         for (i=0 ; i<3 ; i++)
318                 velocity[i] += accelspeed * wishvel[i];
319 }
320
321 void SV_WaterJump (void)
322 {
323         if (sv.time > sv_player->v.teleport_time
324         || !sv_player->v.waterlevel)
325         {
326                 sv_player->v.flags = (int)sv_player->v.flags & ~FL_WATERJUMP;
327                 sv_player->v.teleport_time = 0;
328         }
329         sv_player->v.velocity[0] = sv_player->v.movedir[0];
330         sv_player->v.velocity[1] = sv_player->v.movedir[1];
331 }
332
333
334 /*
335 ===================
336 SV_AirMove
337
338 ===================
339 */
340 void SV_AirMove (void)
341 {
342         int                     i;
343         vec3_t          wishvel;
344         float           fmove, smove, temp;
345
346         // LordHavoc: correct quake movement speed bug when looking up/down
347         wishvel[0] = wishvel[2] = 0;
348         wishvel[1] = sv_player->v.angles[1];
349         AngleVectors (wishvel, forward, right, up);
350 //      AngleVectors (sv_player->v.angles, forward, right, up);
351
352         fmove = cmd.forwardmove;
353         smove = cmd.sidemove;
354         
355 // hack to not let you back into teleporter
356         if (sv.time < sv_player->v.teleport_time && fmove < 0)
357                 fmove = 0;
358                 
359         for (i=0 ; i<3 ; i++)
360                 wishvel[i] = forward[i]*fmove + right[i]*smove;
361
362         if ( (int)sv_player->v.movetype != MOVETYPE_WALK)
363                 wishvel[2] = cmd.upmove;
364         else
365                 wishvel[2] = 0;
366
367         VectorCopy (wishvel, wishdir);
368         wishspeed = VectorNormalizeLength(wishdir);
369         if (wishspeed > sv_maxspeed.value)
370         {
371                 temp = sv_maxspeed.value/wishspeed;
372                 VectorScale (wishvel, temp, wishvel);
373                 wishspeed = sv_maxspeed.value;
374         }
375         
376         if ( sv_player->v.movetype == MOVETYPE_NOCLIP)
377         {       // noclip
378                 VectorCopy (wishvel, velocity);
379         }
380         else if ( onground )
381         {
382                 SV_UserFriction ();
383                 SV_Accelerate ();
384         }
385         else
386         {       // not on ground, so little effect on velocity
387                 SV_AirAccelerate (wishvel);
388         }               
389 }
390
391 /*
392 ===================
393 SV_ClientThink
394
395 the move fields specify an intended velocity in pix/sec
396 the angle fields specify an exact angular motion in degrees
397 ===================
398 */
399 void SV_ClientThink (void)
400 {
401         vec3_t          v_angle;
402
403         if (sv_player->v.movetype == MOVETYPE_NONE)
404                 return;
405         
406         onground = (int)sv_player->v.flags & FL_ONGROUND;
407
408         origin = sv_player->v.origin;
409         velocity = sv_player->v.velocity;
410
411         DropPunchAngle ();
412         
413 //
414 // if dead, behave differently
415 //
416         if (sv_player->v.health <= 0)
417                 return;
418
419 //
420 // angles
421 // show 1/3 the pitch angle and all the roll angle
422         cmd = host_client->cmd;
423         angles = sv_player->v.angles;
424         
425         VectorAdd (sv_player->v.v_angle, sv_player->v.punchangle, v_angle);
426         angles[ROLL] = V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4;
427         if (!sv_player->v.fixangle)
428         {
429                 // LordHavoc: pitch was ugly to begin with...  removed except in water
430                 if (sv_player->v.waterlevel >= 2)
431                         angles[PITCH] = -v_angle[PITCH]/3;
432                 else
433                         angles[PITCH] = 0;
434                 angles[YAW] = v_angle[YAW];
435         }
436
437         if ( (int)sv_player->v.flags & FL_WATERJUMP )
438         {
439                 SV_WaterJump ();
440                 return;
441         }
442 //
443 // walk
444 //
445         if ( (sv_player->v.waterlevel >= 2)     && (sv_player->v.movetype != MOVETYPE_NOCLIP) )
446         {
447                 SV_WaterMove ();
448                 return;
449         }
450
451         SV_AirMove ();  
452 }
453
454
455 /*
456 ===================
457 SV_ReadClientMove
458 ===================
459 */
460 void SV_ReadClientMove (usercmd_t *move)
461 {
462         int             i;
463         vec3_t  angle;
464         int             bits;
465         eval_t  *val;
466         float   total;
467         
468 // read ping time
469         host_client->ping_times[host_client->num_pings%NUM_PING_TIMES]
470                 = sv.time - MSG_ReadFloat ();
471         host_client->num_pings++;
472         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
473                 total += host_client->ping_times[i];
474         host_client->ping = total / NUM_PING_TIMES; // can be used for prediction
475         host_client->latency = 0;
476         if (sv_predict.value && (svs.maxclients > 1) && (!sv.paused)) // if paused or a local game, don't predict
477                 host_client->latency = host_client->ping;
478         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_ping)))
479                 val->_float = host_client->ping * 1000.0;
480
481 // read current angles
482         if (dpprotocol)
483         {
484                 for (i=0 ; i<3 ; i++)
485                         angle[i] = MSG_ReadPreciseAngle ();
486         }
487         else
488         {
489                 for (i=0 ; i<3 ; i++)
490                         angle[i] = MSG_ReadAngle ();
491         }
492
493         VectorCopy (angle, host_client->edict->v.v_angle);
494                 
495 // read movement
496         move->forwardmove = MSG_ReadShort ();
497         move->sidemove = MSG_ReadShort ();
498         move->upmove = MSG_ReadShort ();
499         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_movement)))
500         {
501                 val->vector[0] = move->forwardmove;
502                 val->vector[1] = move->sidemove;
503                 val->vector[2] = move->upmove;
504         }
505         
506 // read buttons
507         bits = MSG_ReadByte ();
508         host_client->edict->v.button0 = bits & 1;
509         host_client->edict->v.button2 = (bits & 2)>>1;
510         // LordHavoc: added 6 new buttons
511         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((bits >> 2) & 1);
512         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((bits >> 3) & 1);
513         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((bits >> 4) & 1);
514         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((bits >> 5) & 1);
515         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((bits >> 6) & 1);
516         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((bits >> 7) & 1);
517
518         i = MSG_ReadByte ();
519         if (i)
520                 host_client->edict->v.impulse = i;
521 }
522
523 /*
524 ===================
525 SV_ReadClientMessage
526
527 Returns false if the client should be killed
528 ===================
529 */
530 qboolean SV_ReadClientMessage (void)
531 {
532         int             ret;
533         int             cmd;
534         char            *s;
535
536         do
537         {
538 nextmsg:
539                 ret = NET_GetMessage (host_client->netconnection);
540                 if (ret == -1)
541                 {
542                         Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n");
543                         return false;
544                 }
545                 if (!ret)
546                         return true;
547
548                 MSG_BeginReading ();
549
550                 while (1)
551                 {
552                         if (!host_client->active)
553                                 return false;   // a command caused an error
554
555                         if (msg_badread)
556                         {
557                                 Sys_Printf ("SV_ReadClientMessage: badread\n");
558                                 return false;
559                         }       
560         
561                         cmd = MSG_ReadChar ();
562                         
563                         switch (cmd)
564                         {
565                         case -1:
566                                 goto nextmsg;           // end of message
567                                 
568                         default:
569                                 Sys_Printf ("SV_ReadClientMessage: unknown command char\n");
570                                 return false;
571                                                         
572                         case clc_nop:
573 //                              Sys_Printf ("clc_nop\n");
574                                 break;
575                                 
576                         case clc_stringcmd:     
577                                 s = MSG_ReadString ();
578                                 ret = 0;
579                                 if (Q_strncasecmp(s, "status", 6) == 0
580                                  || Q_strncasecmp(s, "name", 4) == 0
581                                  || Q_strncasecmp(s, "say", 3) == 0
582                                  || Q_strncasecmp(s, "say_team", 8) == 0
583                                  || Q_strncasecmp(s, "tell", 4) == 0
584                                  || Q_strncasecmp(s, "color", 5) == 0
585                                  || Q_strncasecmp(s, "kill", 4) == 0
586                                  || Q_strncasecmp(s, "pause", 5) == 0
587                                  || Q_strncasecmp(s, "spawn", 5) == 0
588                                  || Q_strncasecmp(s, "begin", 5) == 0
589                                  || Q_strncasecmp(s, "prespawn", 8) == 0
590                                  || Q_strncasecmp(s, "kick", 4) == 0
591                                  || Q_strncasecmp(s, "ping", 4) == 0
592                                  || Q_strncasecmp(s, "ban", 3) == 0
593                                  || Q_strncasecmp(s, "pmodel", 6) == 0
594                                  || (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))
595                                  || (!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)))
596                                 {
597                                         ret = 1;
598                                         Cmd_ExecuteString (s, src_client);
599                                 }
600                                 else
601                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
602                                 /*
603                                 if (ret == 2)
604                                         Cbuf_InsertText (s);
605                                 else if (ret == 1)
606                                         Cmd_ExecuteString (s, src_client);
607                                 else
608                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
609                                 */
610                                 break;
611
612                         case clc_disconnect:
613 //                              Sys_Printf ("SV_ReadClientMessage: client disconnected\n");
614                                 return false;
615
616                         case clc_move:
617                                 SV_ReadClientMove (&host_client->cmd);
618                                 break;
619                         }
620                 }
621         } while (ret == 1);
622
623         return true;
624 }
625
626
627 /*
628 ==================
629 SV_RunClients
630 ==================
631 */
632 void SV_RunClients (void)
633 {
634         int                             i;
635
636         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
637         {
638                 if (!host_client->active)
639                         continue;
640         
641                 sv_player = host_client->edict;
642
643                 if (!SV_ReadClientMessage ())
644                 {
645                         SV_DropClient (false);  // client misbehaved...
646                         continue;
647                 }
648
649                 if (!host_client->spawned)
650                 {
651                 // clear client movement until a new packet is received
652                         memset (&host_client->cmd, 0, sizeof(host_client->cmd));
653                         continue;
654                 }
655
656 // always pause in single player if in console or menus
657                 if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
658                 {
659                         // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
660                         if (SV_PlayerPhysicsQC)
661                         {
662                                 pr_global_struct->time = sv.time;
663                                 pr_global_struct->self = EDICT_TO_PROG(sv_player);
664                                 PR_ExecuteProgram ((func_t)(SV_PlayerPhysicsQC - pr_functions), "");
665                         }
666                         else
667                                 SV_ClientThink ();
668                 }
669         }
670 }
671