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