]> icculus.org git repositories - divverent/darkplaces.git/blob - sv_user.c
cleaned up nearly all of the externs in .c files (moved to appropriate .h files)
[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 = {"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;
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                 VectorScale (wishvel, sv_maxspeed.value/wishspeed, 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                 VectorScale (velocity, newspeed/speed, velocity);
296         }
297         else
298                 newspeed = 0;
299         
300 //
301 // water acceleration
302 //
303         if (!wishspeed)
304                 return;
305
306         addspeed = wishspeed - newspeed;
307         if (addspeed <= 0)
308                 return;
309
310         VectorNormalize (wishvel);
311         accelspeed = sv_accelerate.value * wishspeed * sv.frametime;
312         if (accelspeed > addspeed)
313                 accelspeed = addspeed;
314
315         for (i=0 ; i<3 ; i++)
316                 velocity[i] += accelspeed * wishvel[i];
317 }
318
319 void SV_WaterJump (void)
320 {
321         if (sv.time > sv_player->v.teleport_time
322         || !sv_player->v.waterlevel)
323         {
324                 sv_player->v.flags = (int)sv_player->v.flags & ~FL_WATERJUMP;
325                 sv_player->v.teleport_time = 0;
326         }
327         sv_player->v.velocity[0] = sv_player->v.movedir[0];
328         sv_player->v.velocity[1] = sv_player->v.movedir[1];
329 }
330
331
332 /*
333 ===================
334 SV_AirMove
335
336 ===================
337 */
338 void SV_AirMove (void)
339 {
340         int                     i;
341         vec3_t          wishvel;
342         float           fmove, smove;
343
344         // LordHavoc: correct quake movement speed bug when looking up/down
345         wishvel[0] = wishvel[2] = 0;
346         wishvel[1] = sv_player->v.angles[1];
347         AngleVectors (wishvel, forward, right, up);
348 //      AngleVectors (sv_player->v.angles, forward, right, up);
349
350         fmove = cmd.forwardmove;
351         smove = cmd.sidemove;
352         
353 // hack to not let you back into teleporter
354         if (sv.time < sv_player->v.teleport_time && fmove < 0)
355                 fmove = 0;
356                 
357         for (i=0 ; i<3 ; i++)
358                 wishvel[i] = forward[i]*fmove + right[i]*smove;
359
360         if ( (int)sv_player->v.movetype != MOVETYPE_WALK)
361                 wishvel[2] = cmd.upmove;
362         else
363                 wishvel[2] = 0;
364
365         VectorCopy (wishvel, wishdir);
366         wishspeed = VectorNormalizeLength(wishdir);
367         if (wishspeed > sv_maxspeed.value)
368         {
369                 VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel);
370                 wishspeed = sv_maxspeed.value;
371         }
372         
373         if ( sv_player->v.movetype == MOVETYPE_NOCLIP)
374         {       // noclip
375                 VectorCopy (wishvel, velocity);
376         }
377         else if ( onground )
378         {
379                 SV_UserFriction ();
380                 SV_Accelerate ();
381         }
382         else
383         {       // not on ground, so little effect on velocity
384                 SV_AirAccelerate (wishvel);
385         }               
386 }
387
388 /*
389 ===================
390 SV_ClientThink
391
392 the move fields specify an intended velocity in pix/sec
393 the angle fields specify an exact angular motion in degrees
394 ===================
395 */
396 void SV_ClientThink (void)
397 {
398         vec3_t          v_angle;
399
400         if (sv_player->v.movetype == MOVETYPE_NONE)
401                 return;
402         
403         onground = (int)sv_player->v.flags & FL_ONGROUND;
404
405         origin = sv_player->v.origin;
406         velocity = sv_player->v.velocity;
407
408         DropPunchAngle ();
409         
410 //
411 // if dead, behave differently
412 //
413         if (sv_player->v.health <= 0)
414                 return;
415
416 //
417 // angles
418 // show 1/3 the pitch angle and all the roll angle
419         cmd = host_client->cmd;
420         angles = sv_player->v.angles;
421         
422         VectorAdd (sv_player->v.v_angle, sv_player->v.punchangle, v_angle);
423         angles[ROLL] = V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4;
424         if (!sv_player->v.fixangle)
425         {
426                 // LordHavoc: pitch was ugly to begin with...  removed except in water
427                 if (sv_player->v.waterlevel >= 2)
428                         angles[PITCH] = -v_angle[PITCH]/3;
429                 else
430                         angles[PITCH] = 0;
431                 angles[YAW] = v_angle[YAW];
432         }
433
434         if ( (int)sv_player->v.flags & FL_WATERJUMP )
435         {
436                 SV_WaterJump ();
437                 return;
438         }
439 //
440 // walk
441 //
442         if ( (sv_player->v.waterlevel >= 2)     && (sv_player->v.movetype != MOVETYPE_NOCLIP) )
443         {
444                 SV_WaterMove ();
445                 return;
446         }
447
448         SV_AirMove ();  
449 }
450
451
452 /*
453 ===================
454 SV_ReadClientMove
455 ===================
456 */
457 void SV_ReadClientMove (usercmd_t *move)
458 {
459         int             i;
460         vec3_t  angle;
461         int             bits;
462         eval_t  *val;
463         float   total;
464         
465 // read ping time
466         host_client->ping_times[host_client->num_pings%NUM_PING_TIMES]
467                 = sv.time - MSG_ReadFloat ();
468         host_client->num_pings++;
469         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
470                 total += host_client->ping_times[i];
471         host_client->ping = total / NUM_PING_TIMES; // can be used for prediction
472         host_client->latency = 0;
473         if (sv_predict.value && (svs.maxclients > 1) && (!sv.paused)) // if paused or a local game, don't predict
474                 host_client->latency = host_client->ping;
475         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_ping)))
476                 val->_float = host_client->ping * 1000.0;
477
478 // read current angles
479         if (dpprotocol)
480         {
481                 for (i=0 ; i<3 ; i++)
482                         angle[i] = MSG_ReadPreciseAngle ();
483         }
484         else
485         {
486                 for (i=0 ; i<3 ; i++)
487                         angle[i] = MSG_ReadAngle ();
488         }
489
490         VectorCopy (angle, host_client->edict->v.v_angle);
491                 
492 // read movement
493         move->forwardmove = MSG_ReadShort ();
494         move->sidemove = MSG_ReadShort ();
495         move->upmove = MSG_ReadShort ();
496         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_movement)))
497         {
498                 val->vector[0] = move->forwardmove;
499                 val->vector[1] = move->sidemove;
500                 val->vector[2] = move->upmove;
501         }
502         
503 // read buttons
504         bits = MSG_ReadByte ();
505         host_client->edict->v.button0 = bits & 1;
506         host_client->edict->v.button2 = (bits & 2)>>1;
507         // LordHavoc: added 6 new buttons
508         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((bits >> 2) & 1);
509         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((bits >> 3) & 1);
510         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((bits >> 4) & 1);
511         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((bits >> 5) & 1);
512         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((bits >> 6) & 1);
513         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((bits >> 7) & 1);
514
515         i = MSG_ReadByte ();
516         if (i)
517                 host_client->edict->v.impulse = i;
518 }
519
520 /*
521 ===================
522 SV_ReadClientMessage
523
524 Returns false if the client should be killed
525 ===================
526 */
527 qboolean SV_ReadClientMessage (void)
528 {
529         int             ret;
530         int             cmd;
531         char            *s;
532
533         do
534         {
535 nextmsg:
536                 ret = NET_GetMessage (host_client->netconnection);
537                 if (ret == -1)
538                 {
539                         Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n");
540                         return false;
541                 }
542                 if (!ret)
543                         return true;
544
545                 MSG_BeginReading ();
546
547                 while (1)
548                 {
549                         if (!host_client->active)
550                                 return false;   // a command caused an error
551
552                         if (msg_badread)
553                         {
554                                 Sys_Printf ("SV_ReadClientMessage: badread\n");
555                                 return false;
556                         }       
557         
558                         cmd = MSG_ReadChar ();
559                         
560                         switch (cmd)
561                         {
562                         case -1:
563                                 goto nextmsg;           // end of message
564                                 
565                         default:
566                                 Sys_Printf ("SV_ReadClientMessage: unknown command char\n");
567                                 return false;
568                                                         
569                         case clc_nop:
570 //                              Sys_Printf ("clc_nop\n");
571                                 break;
572                                 
573                         case clc_stringcmd:     
574                                 s = MSG_ReadString ();
575                                 ret = 0;
576                                 if (Q_strncasecmp(s, "status", 6) == 0
577                                  || Q_strncasecmp(s, "name", 4) == 0
578                                  || Q_strncasecmp(s, "say", 3) == 0
579                                  || Q_strncasecmp(s, "say_team", 8) == 0
580                                  || Q_strncasecmp(s, "tell", 4) == 0
581                                  || Q_strncasecmp(s, "color", 5) == 0
582                                  || Q_strncasecmp(s, "kill", 4) == 0
583                                  || Q_strncasecmp(s, "pause", 5) == 0
584                                  || Q_strncasecmp(s, "spawn", 5) == 0
585                                  || Q_strncasecmp(s, "begin", 5) == 0
586                                  || Q_strncasecmp(s, "prespawn", 8) == 0
587                                  || Q_strncasecmp(s, "kick", 4) == 0
588                                  || Q_strncasecmp(s, "ping", 4) == 0
589                                  || Q_strncasecmp(s, "ban", 3) == 0
590                                  || Q_strncasecmp(s, "pmodel", 6) == 0
591                                  || (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))
592                                  || (!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)))
593                                 {
594                                         ret = 1;
595                                         Cmd_ExecuteString (s, src_client);
596                                 }
597                                 else
598                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
599                                 /*
600                                 if (ret == 2)
601                                         Cbuf_InsertText (s);
602                                 else if (ret == 1)
603                                         Cmd_ExecuteString (s, src_client);
604                                 else
605                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
606                                 */
607                                 break;
608
609                         case clc_disconnect:
610 //                              Sys_Printf ("SV_ReadClientMessage: client disconnected\n");
611                                 return false;
612
613                         case clc_move:
614                                 SV_ReadClientMove (&host_client->cmd);
615                                 break;
616                         }
617                 }
618         } while (ret == 1);
619
620         return true;
621 }
622
623
624 /*
625 ==================
626 SV_RunClients
627 ==================
628 */
629 void SV_RunClients (void)
630 {
631         int                             i;
632
633         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
634         {
635                 if (!host_client->active)
636                         continue;
637         
638                 sv_player = host_client->edict;
639
640                 if (!SV_ReadClientMessage ())
641                 {
642                         SV_DropClient (false);  // client misbehaved...
643                         continue;
644                 }
645
646                 if (!host_client->spawned)
647                 {
648                 // clear client movement until a new packet is received
649                         memset (&host_client->cmd, 0, sizeof(host_client->cmd));
650                         continue;
651                 }
652
653 // always pause in single player if in console or menus
654                 if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
655                 {
656                         // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
657                         if (SV_PlayerPhysicsQC)
658                         {
659                                 pr_global_struct->time = sv.time;
660                                 pr_global_struct->self = EDICT_TO_PROG(sv_player);
661                                 PR_ExecuteProgram ((func_t)(SV_PlayerPhysicsQC - pr_functions), "");
662                         }
663                         else
664                                 SV_ClientThink ();
665                 }
666         }
667 }
668