]> icculus.org git repositories - divverent/darkplaces.git/blob - sv_user.c
some whitespace changes
[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 cvar_t  sv_predict = {"sv_predict", "1"};
29 cvar_t  sv_deltacompress = {"sv_deltacompress", "1"};
30 extern  cvar_t  sv_stopspeed;
31
32 static  vec3_t          forward, right, up;
33
34 vec3_t  wishdir;
35 float   wishspeed;
36
37 // world
38 float   *angles;
39 float   *origin;
40 float   *velocity;
41
42 qboolean        onground;
43
44 usercmd_t       cmd;
45
46 cvar_t  sv_idealpitchscale = {"sv_idealpitchscale","0.8"};
47
48
49 /*
50 ===============
51 SV_SetIdealPitch
52 ===============
53 */
54 #define MAX_FORWARD     6
55 void SV_SetIdealPitch (void)
56 {
57         float   angleval, sinval, cosval;
58         trace_t tr;
59         vec3_t  top, bottom;
60         float   z[MAX_FORWARD];
61         int             i, j;
62         int             step, dir, steps;
63
64         if (!((int)sv_player->v.flags & FL_ONGROUND))
65                 return;
66                 
67         angleval = sv_player->v.angles[YAW] * M_PI*2 / 360;
68         sinval = sin(angleval);
69         cosval = cos(angleval);
70
71         for (i=0 ; i<MAX_FORWARD ; i++)
72         {
73                 top[0] = sv_player->v.origin[0] + cosval*(i+3)*12;
74                 top[1] = sv_player->v.origin[1] + sinval*(i+3)*12;
75                 top[2] = sv_player->v.origin[2] + sv_player->v.view_ofs[2];
76                 
77                 bottom[0] = top[0];
78                 bottom[1] = top[1];
79                 bottom[2] = top[2] - 160;
80                 
81                 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, 1, sv_player);
82                 if (tr.allsolid)
83                         return; // looking at a wall, leave ideal the way is was
84
85                 if (tr.fraction == 1)
86                         return; // near a dropoff
87                 
88                 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
89         }
90         
91         dir = 0;
92         steps = 0;
93         for (j=1 ; j<i ; j++)
94         {
95                 step = z[j] - z[j-1];
96                 if (step > -ON_EPSILON && step < ON_EPSILON)
97                         continue;
98
99                 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
100                         return;         // mixed changes
101
102                 steps++;        
103                 dir = step;
104         }
105         
106         if (!dir)
107         {
108                 sv_player->v.idealpitch = 0;
109                 return;
110         }
111         
112         if (steps < 2)
113                 return;
114         sv_player->v.idealpitch = -dir * sv_idealpitchscale.value;
115 }
116
117
118 /*
119 ==================
120 SV_UserFriction
121
122 ==================
123 */
124 void SV_UserFriction (void)
125 {
126         float   *vel;
127         float   speed, newspeed, control;
128         vec3_t  start, stop;
129         float   friction;
130         trace_t trace;
131         
132         vel = velocity;
133         
134         speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1]);
135         if (!speed)
136                 return;
137
138 // if the leading edge is over a dropoff, increase friction
139         start[0] = stop[0] = origin[0] + vel[0]/speed*16;
140         start[1] = stop[1] = origin[1] + vel[1]/speed*16;
141         start[2] = origin[2] + sv_player->v.mins[2];
142         stop[2] = start[2] - 34;
143
144         trace = SV_Move (start, vec3_origin, vec3_origin, stop, true, sv_player);
145
146         if (trace.fraction == 1.0)
147                 friction = sv_friction.value*sv_edgefriction.value;
148         else
149                 friction = sv_friction.value;
150
151 // apply friction       
152         control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
153         newspeed = speed - sv.frametime*control*friction;
154         
155         if (newspeed < 0)
156                 newspeed = 0;
157         else
158                 newspeed /= speed;
159
160         vel[0] = vel[0] * newspeed;
161         vel[1] = vel[1] * newspeed;
162         vel[2] = vel[2] * newspeed;
163 }
164
165 /*
166 ==============
167 SV_Accelerate
168 ==============
169 */
170 cvar_t  sv_maxspeed = {"sv_maxspeed", "320", false, true};
171 cvar_t  sv_accelerate = {"sv_accelerate", "10"};
172 #if 0
173 void SV_Accelerate (vec3_t wishvel)
174 {
175         int                     i;
176         float           addspeed, accelspeed;
177         vec3_t          pushvec;
178
179         if (wishspeed == 0)
180                 return;
181
182         VectorSubtract (wishvel, velocity, pushvec);
183         addspeed = VectorNormalize (pushvec);
184
185         accelspeed = sv_accelerate.value*sv.frametime*addspeed;
186         if (accelspeed > addspeed)
187                 accelspeed = addspeed;
188         
189         for (i=0 ; i<3 ; i++)
190                 velocity[i] += accelspeed*pushvec[i];   
191 }
192 #endif
193 void SV_Accelerate (void)
194 {
195         int                     i;
196         float           addspeed, accelspeed, currentspeed;
197
198         currentspeed = DotProduct (velocity, wishdir);
199         addspeed = wishspeed - currentspeed;
200         if (addspeed <= 0)
201                 return;
202         accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
203         if (accelspeed > addspeed)
204                 accelspeed = addspeed;
205         
206         for (i=0 ; i<3 ; i++)
207                 velocity[i] += accelspeed*wishdir[i];   
208 }
209
210 void SV_AirAccelerate (vec3_t wishveloc)
211 {
212         int                     i;
213         float           addspeed, wishspd, accelspeed, currentspeed;
214                 
215         wishspd = VectorNormalizeLength (wishveloc);
216         if (wishspd > 30)
217                 wishspd = 30;
218         currentspeed = DotProduct (velocity, wishveloc);
219         addspeed = wishspd - currentspeed;
220         if (addspeed <= 0)
221                 return;
222 //      accelspeed = sv_accelerate.value * sv.frametime;
223         accelspeed = sv_accelerate.value*wishspeed * sv.frametime;
224         if (accelspeed > addspeed)
225                 accelspeed = addspeed;
226         
227         for (i=0 ; i<3 ; i++)
228                 velocity[i] += accelspeed*wishveloc[i]; 
229 }
230
231
232 void DropPunchAngle (void)
233 {
234         float   len;
235         eval_t  *val;
236         
237         len = VectorNormalizeLength (sv_player->v.punchangle);
238         
239         len -= 10*sv.frametime;
240         if (len < 0)
241                 len = 0;
242         VectorScale (sv_player->v.punchangle, len, sv_player->v.punchangle);
243         
244         if ((val = GETEDICTFIELDVALUE(sv_player, eval_punchvector)))
245         {
246                 len = VectorNormalizeLength (val->vector);
247                 
248                 len -= 20*sv.frametime;
249                 if (len < 0)
250                         len = 0;
251                 VectorScale (val->vector, len, val->vector);
252         }
253 }
254
255 /*
256 ===================
257 SV_WaterMove
258
259 ===================
260 */
261 void SV_WaterMove (void)
262 {
263         int             i;
264         vec3_t  wishvel;
265         float   speed, newspeed, wishspeed, addspeed, accelspeed;
266
267 //
268 // user intentions
269 //
270         AngleVectors (sv_player->v.v_angle, forward, right, up);
271
272         for (i=0 ; i<3 ; i++)
273                 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
274
275         if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
276                 wishvel[2] -= 60;               // drift towards bottom
277         else
278                 wishvel[2] += cmd.upmove;
279
280         wishspeed = Length(wishvel);
281         if (wishspeed > sv_maxspeed.value)
282         {
283                 VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel);
284                 wishspeed = sv_maxspeed.value;
285         }
286         wishspeed *= 0.7;
287
288 //
289 // water friction
290 //
291         speed = Length (velocity);
292         if (speed)
293         {
294                 newspeed = speed - sv.frametime * speed * sv_friction.value;
295                 if (newspeed < 0)
296                         newspeed = 0;   
297                 VectorScale (velocity, newspeed/speed, 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;
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                 VectorScale (wishvel, sv_maxspeed.value/wishspeed, 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]
469                 = sv.time - MSG_ReadFloat ();
470         host_client->num_pings++;
471         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
472                 total += host_client->ping_times[i];
473         host_client->ping = total / NUM_PING_TIMES; // can be used for prediction
474         host_client->latency = 0;
475         if (sv_predict.value && (svs.maxclients > 1) && (!sv.paused)) // if paused or a local game, don't predict
476                 host_client->latency = host_client->ping;
477         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_ping)))
478                 val->_float = host_client->ping * 1000.0;
479
480 // read current angles
481         if (dpprotocol)
482         {
483                 for (i=0 ; i<3 ; i++)
484                         angle[i] = MSG_ReadPreciseAngle ();
485         }
486         else
487         {
488                 for (i=0 ; i<3 ; i++)
489                         angle[i] = MSG_ReadAngle ();
490         }
491
492         VectorCopy (angle, host_client->edict->v.v_angle);
493                 
494 // read movement
495         move->forwardmove = MSG_ReadShort ();
496         move->sidemove = MSG_ReadShort ();
497         move->upmove = MSG_ReadShort ();
498         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_movement)))
499         {
500                 val->vector[0] = move->forwardmove;
501                 val->vector[1] = move->sidemove;
502                 val->vector[2] = move->upmove;
503         }
504         
505 // read buttons
506         bits = MSG_ReadByte ();
507         host_client->edict->v.button0 = bits & 1;
508         host_client->edict->v.button2 = (bits & 2)>>1;
509         // LordHavoc: added 6 new buttons
510         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((bits >> 2) & 1);
511         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((bits >> 3) & 1);
512         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((bits >> 4) & 1);
513         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((bits >> 5) & 1);
514         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((bits >> 6) & 1);
515         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((bits >> 7) & 1);
516
517         i = MSG_ReadByte ();
518         if (i)
519                 host_client->edict->v.impulse = i;
520 }
521
522 /*
523 ===================
524 SV_ReadClientMessage
525
526 Returns false if the client should be killed
527 ===================
528 */
529 qboolean SV_ReadClientMessage (void)
530 {
531         int             ret;
532         int             cmd;
533         char            *s;
534
535         do
536         {
537 nextmsg:
538                 ret = NET_GetMessage (host_client->netconnection);
539                 if (ret == -1)
540                 {
541                         Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n");
542                         return false;
543                 }
544                 if (!ret)
545                         return true;
546
547                 MSG_BeginReading ();
548
549                 while (1)
550                 {
551                         if (!host_client->active)
552                                 return false;   // a command caused an error
553
554                         if (msg_badread)
555                         {
556                                 Sys_Printf ("SV_ReadClientMessage: badread\n");
557                                 return false;
558                         }       
559         
560                         cmd = MSG_ReadChar ();
561                         
562                         switch (cmd)
563                         {
564                         case -1:
565                                 goto nextmsg;           // end of message
566                                 
567                         default:
568                                 Sys_Printf ("SV_ReadClientMessage: unknown command char\n");
569                                 return false;
570                                                         
571                         case clc_nop:
572 //                              Sys_Printf ("clc_nop\n");
573                                 break;
574                                 
575                         case clc_stringcmd:     
576                                 s = MSG_ReadString ();
577                                 ret = 0;
578                                 if (Q_strncasecmp(s, "status", 6) == 0
579                                  || Q_strncasecmp(s, "name", 4) == 0
580                                  || Q_strncasecmp(s, "say", 3) == 0
581                                  || Q_strncasecmp(s, "say_team", 8) == 0
582                                  || Q_strncasecmp(s, "tell", 4) == 0
583                                  || Q_strncasecmp(s, "color", 5) == 0
584                                  || Q_strncasecmp(s, "kill", 4) == 0
585                                  || Q_strncasecmp(s, "pause", 5) == 0
586                                  || Q_strncasecmp(s, "spawn", 5) == 0
587                                  || Q_strncasecmp(s, "begin", 5) == 0
588                                  || Q_strncasecmp(s, "prespawn", 8) == 0
589                                  || Q_strncasecmp(s, "kick", 4) == 0
590                                  || Q_strncasecmp(s, "ping", 4) == 0
591                                  || Q_strncasecmp(s, "ban", 3) == 0
592                                  || Q_strncasecmp(s, "pmodel", 6) == 0
593                                  || (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))
594                                  || (!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)))
595                                 {
596                                         ret = 1;
597                                         Cmd_ExecuteString (s, src_client);
598                                 }
599                                 else
600                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
601                                 /*
602                                 if (ret == 2)
603                                         Cbuf_InsertText (s);
604                                 else if (ret == 1)
605                                         Cmd_ExecuteString (s, src_client);
606                                 else
607                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
608                                 */
609                                 break;
610
611                         case clc_disconnect:
612 //                              Sys_Printf ("SV_ReadClientMessage: client disconnected\n");
613                                 return false;
614
615                         case clc_move:
616                                 SV_ReadClientMove (&host_client->cmd);
617                                 break;
618                         }
619                 }
620         } while (ret == 1);
621
622         return true;
623 }
624
625
626 /*
627 ==================
628 SV_RunClients
629 ==================
630 */
631 extern dfunction_t *SV_PlayerPhysicsQC;
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