30a654a7b79943530c76c76cf812a31645b0f0e6
[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 = {0, "edgefriction", "2"};
27 cvar_t  sv_predict = {0, "sv_predict", "1"};
28 cvar_t  sv_deltacompress = {0, "sv_deltacompress", "1"};
29 cvar_t  sv_idealpitchscale = {0, "sv_idealpitchscale","0.8"};
30 cvar_t  sv_maxspeed = {CVAR_NOTIFY, "sv_maxspeed", "320"};
31 cvar_t  sv_accelerate = {0, "sv_accelerate", "10"};
32
33 static  vec3_t          forward, right, up;
34
35 vec3_t  wishdir;
36 float   wishspeed;
37
38 // world
39 float   *angles;
40 float   *origin;
41 float   *velocity;
42
43 qboolean        onground;
44
45 usercmd_t       cmd;
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, MOVE_NOMONSTERS, 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, MOVE_NOMONSTERS, 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 - sv.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 void SV_Accelerate (void)
170 {
171         int                     i;
172         float           addspeed, accelspeed, currentspeed;
173
174         currentspeed = DotProduct (velocity, wishdir);
175         addspeed = wishspeed - currentspeed;
176         if (addspeed <= 0)
177                 return;
178         accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
179         if (accelspeed > addspeed)
180                 accelspeed = addspeed;
181         
182         for (i=0 ; i<3 ; i++)
183                 velocity[i] += accelspeed*wishdir[i];   
184 }
185
186 void SV_AirAccelerate (vec3_t wishveloc)
187 {
188         int                     i;
189         float           addspeed, wishspd, accelspeed, currentspeed;
190                 
191         wishspd = VectorNormalizeLength (wishveloc);
192         if (wishspd > 30)
193                 wishspd = 30;
194         currentspeed = DotProduct (velocity, wishveloc);
195         addspeed = wishspd - currentspeed;
196         if (addspeed <= 0)
197                 return;
198         accelspeed = sv_accelerate.value*wishspeed * sv.frametime;
199         if (accelspeed > addspeed)
200                 accelspeed = addspeed;
201         
202         for (i=0 ; i<3 ; i++)
203                 velocity[i] += accelspeed*wishveloc[i]; 
204 }
205
206
207 void DropPunchAngle (void)
208 {
209         float   len;
210         eval_t  *val;
211         
212         len = VectorNormalizeLength (sv_player->v.punchangle);
213
214         len -= 10*sv.frametime;
215         if (len < 0)
216                 len = 0;
217         VectorScale (sv_player->v.punchangle, len, sv_player->v.punchangle);
218         
219         if ((val = GETEDICTFIELDVALUE(sv_player, eval_punchvector)))
220         {
221                 len = VectorNormalizeLength (val->vector);
222                 
223                 len -= 20*sv.frametime;
224                 if (len < 0)
225                         len = 0;
226                 VectorScale (val->vector, len, val->vector);
227         }
228 }
229
230 /*
231 ===================
232 SV_WaterMove
233
234 ===================
235 */
236 void SV_WaterMove (void)
237 {
238         int             i;
239         vec3_t  wishvel;
240         float   speed, newspeed, wishspeed, addspeed, accelspeed, temp;
241
242 //
243 // user intentions
244 //
245         AngleVectors (sv_player->v.v_angle, forward, right, up);
246
247         for (i=0 ; i<3 ; i++)
248                 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
249
250         if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
251                 wishvel[2] -= 60;               // drift towards bottom
252         else
253                 wishvel[2] += cmd.upmove;
254
255         wishspeed = VectorLength(wishvel);
256         if (wishspeed > sv_maxspeed.value)
257         {
258                 temp = sv_maxspeed.value/wishspeed;
259                 VectorScale (wishvel, temp, wishvel);
260                 wishspeed = sv_maxspeed.value;
261         }
262         wishspeed *= 0.7;
263
264 //
265 // water friction
266 //
267         speed = VectorLength (velocity);
268         if (speed)
269         {
270                 newspeed = speed - sv.frametime * speed * sv_friction.value;
271                 if (newspeed < 0)
272                         newspeed = 0;
273                 temp = newspeed/speed;
274                 VectorScale (velocity, temp, velocity);
275         }
276         else
277                 newspeed = 0;
278         
279 //
280 // water acceleration
281 //
282         if (!wishspeed)
283                 return;
284
285         addspeed = wishspeed - newspeed;
286         if (addspeed <= 0)
287                 return;
288
289         VectorNormalize (wishvel);
290         accelspeed = sv_accelerate.value * wishspeed * sv.frametime;
291         if (accelspeed > addspeed)
292                 accelspeed = addspeed;
293
294         for (i=0 ; i<3 ; i++)
295                 velocity[i] += accelspeed * wishvel[i];
296 }
297
298 void SV_WaterJump (void)
299 {
300         if (sv.time > sv_player->v.teleport_time
301         || !sv_player->v.waterlevel)
302         {
303                 sv_player->v.flags = (int)sv_player->v.flags & ~FL_WATERJUMP;
304                 sv_player->v.teleport_time = 0;
305         }
306         sv_player->v.velocity[0] = sv_player->v.movedir[0];
307         sv_player->v.velocity[1] = sv_player->v.movedir[1];
308 }
309
310
311 /*
312 ===================
313 SV_AirMove
314
315 ===================
316 */
317 void SV_AirMove (void)
318 {
319         int                     i;
320         vec3_t          wishvel;
321         float           fmove, smove, temp;
322
323         // LordHavoc: correct quake movement speed bug when looking up/down
324         wishvel[0] = wishvel[2] = 0;
325         wishvel[1] = sv_player->v.angles[1];
326         AngleVectors (wishvel, forward, right, up);
327
328         fmove = cmd.forwardmove;
329         smove = cmd.sidemove;
330         
331 // hack to not let you back into teleporter
332         if (sv.time < sv_player->v.teleport_time && fmove < 0)
333                 fmove = 0;
334                 
335         for (i=0 ; i<3 ; i++)
336                 wishvel[i] = forward[i]*fmove + right[i]*smove;
337
338         if ( (int)sv_player->v.movetype != MOVETYPE_WALK)
339                 wishvel[2] = cmd.upmove;
340         else
341                 wishvel[2] = 0;
342
343         VectorCopy (wishvel, wishdir);
344         wishspeed = VectorNormalizeLength(wishdir);
345         if (wishspeed > sv_maxspeed.value)
346         {
347                 temp = sv_maxspeed.value/wishspeed;
348                 VectorScale (wishvel, temp, wishvel);
349                 wishspeed = sv_maxspeed.value;
350         }
351         
352         if ( sv_player->v.movetype == MOVETYPE_NOCLIP)
353         {       // noclip
354                 VectorCopy (wishvel, velocity);
355         }
356         else if ( onground )
357         {
358                 SV_UserFriction ();
359                 SV_Accelerate ();
360         }
361         else
362         {       // not on ground, so little effect on velocity
363                 SV_AirAccelerate (wishvel);
364         }
365 }
366
367 /*
368 ===================
369 SV_ClientThink
370
371 the move fields specify an intended velocity in pix/sec
372 the angle fields specify an exact angular motion in degrees
373 ===================
374 */
375 void SV_ClientThink (void)
376 {
377         vec3_t          v_angle;
378
379         if (sv_player->v.movetype == MOVETYPE_NONE)
380                 return;
381         
382         onground = (int)sv_player->v.flags & FL_ONGROUND;
383
384         origin = sv_player->v.origin;
385         velocity = sv_player->v.velocity;
386
387         DropPunchAngle ();
388         
389 //
390 // if dead, behave differently
391 //
392         if (sv_player->v.health <= 0)
393                 return;
394
395 //
396 // angles
397 // show 1/3 the pitch angle and all the roll angle
398         cmd = host_client->cmd;
399         angles = sv_player->v.angles;
400         
401         VectorAdd (sv_player->v.v_angle, sv_player->v.punchangle, v_angle);
402         angles[ROLL] = V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4;
403         if (!sv_player->v.fixangle)
404         {
405                 // LordHavoc: pitch was ugly to begin with...  removed except in water
406                 if (sv_player->v.waterlevel >= 2)
407                         angles[PITCH] = -v_angle[PITCH]/3;
408                 else
409                         angles[PITCH] = 0;
410                 angles[YAW] = v_angle[YAW];
411         }
412
413         if ( (int)sv_player->v.flags & FL_WATERJUMP )
414         {
415                 SV_WaterJump ();
416                 return;
417         }
418 //
419 // walk
420 //
421         if ( (sv_player->v.waterlevel >= 2)     && (sv_player->v.movetype != MOVETYPE_NOCLIP) )
422         {
423                 SV_WaterMove ();
424                 return;
425         }
426
427         SV_AirMove ();
428 }
429
430
431 /*
432 ===================
433 SV_ReadClientMove
434 ===================
435 */
436 void SV_ReadClientMove (usercmd_t *move)
437 {
438         int             i;
439         vec3_t  angle;
440         int             bits;
441         eval_t  *val;
442         float   total;
443
444 // read ping time
445         host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = sv.time - MSG_ReadFloat ();
446         host_client->num_pings++;
447         for (i=0, total = 0;i < NUM_PING_TIMES;i++)
448                 total += host_client->ping_times[i];
449         host_client->ping = total / NUM_PING_TIMES; // can be used for prediction
450         host_client->latency = 0;
451         if (sv_predict.integer && (svs.maxclients > 1) && (!sv.paused)) // if paused or a local game, don't predict
452                 host_client->latency = host_client->ping;
453         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_ping)))
454                 val->_float = host_client->ping * 1000.0;
455
456 // read current angles
457         // dpprotocol version 2
458         for (i = 0;i < 3;i++)
459                 angle[i] = MSG_ReadFloat ();
460
461         VectorCopy (angle, host_client->edict->v.v_angle);
462
463 // read movement
464         move->forwardmove = MSG_ReadShort ();
465         move->sidemove = MSG_ReadShort ();
466         move->upmove = MSG_ReadShort ();
467         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_movement)))
468         {
469                 val->vector[0] = move->forwardmove;
470                 val->vector[1] = move->sidemove;
471                 val->vector[2] = move->upmove;
472         }
473
474 // read buttons
475         bits = MSG_ReadByte ();
476         host_client->edict->v.button0 = bits & 1;
477         host_client->edict->v.button2 = (bits & 2)>>1;
478         // LordHavoc: added 6 new buttons
479         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((bits >> 2) & 1);
480         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((bits >> 3) & 1);
481         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((bits >> 4) & 1);
482         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((bits >> 5) & 1);
483         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((bits >> 6) & 1);
484         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((bits >> 7) & 1);
485
486         i = MSG_ReadByte ();
487         if (i)
488                 host_client->edict->v.impulse = i;
489 }
490
491 /*
492 ===================
493 SV_ReadClientMessage
494
495 Returns false if the client should be killed
496 ===================
497 */
498 void SV_SendServerinfo (client_t *client);
499 qboolean SV_ReadClientMessage (void)
500 {
501         int             ret;
502         int             cmd;
503         char            *s;
504
505         do
506         {
507 nextmsg:
508                 ret = NET_GetMessage (host_client->netconnection);
509                 if (ret == -1)
510                 {
511                         Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n");
512                         return false;
513                 }
514                 if (!ret)
515                         return true;
516
517                 MSG_BeginReading ();
518
519                 while (1)
520                 {
521                         if (!host_client->active)
522                                 return false;   // a command caused an error
523
524                         if (msg_badread)
525                         {
526                                 Sys_Printf ("SV_ReadClientMessage: badread\n");
527                                 return false;
528                         }
529
530                         cmd = MSG_ReadChar ();
531
532 #ifndef NOROUTINGFIX
533                         if (cmd != -1 && host_client->waitingforconnect)
534                         {
535                                 host_client->waitingforconnect = false;
536                                 host_client->sendserverinfo = true;
537                         }
538 #endif
539
540                         switch (cmd)
541                         {
542                         case -1:
543                                 goto nextmsg;           // end of message
544
545                         default:
546                                 Sys_Printf ("SV_ReadClientMessage: unknown command char %i\n", cmd);
547                                 return false;
548
549                         case clc_nop:
550                                 break;
551
552                         case clc_stringcmd:
553                                 s = MSG_ReadString ();
554                                 ret = 0;
555                                 if (Q_strncasecmp(s, "status", 6) == 0
556                                  || Q_strncasecmp(s, "name", 4) == 0
557                                  || Q_strncasecmp(s, "say", 3) == 0
558                                  || Q_strncasecmp(s, "say_team", 8) == 0
559                                  || Q_strncasecmp(s, "tell", 4) == 0
560                                  || Q_strncasecmp(s, "color", 5) == 0
561                                  || Q_strncasecmp(s, "kill", 4) == 0
562                                  || Q_strncasecmp(s, "pause", 5) == 0
563                                  || Q_strncasecmp(s, "spawn", 5) == 0
564                                  || Q_strncasecmp(s, "begin", 5) == 0
565                                  || Q_strncasecmp(s, "prespawn", 8) == 0
566                                  || Q_strncasecmp(s, "kick", 4) == 0
567                                  || Q_strncasecmp(s, "ping", 4) == 0
568                                  || Q_strncasecmp(s, "ban", 3) == 0
569                                  || Q_strncasecmp(s, "pmodel", 6) == 0
570                                  || (gamemode == GAME_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))
571                                  || (gamemode != GAME_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)))
572                                 {
573                                         ret = 1;
574                                         Cmd_ExecuteString (s, src_client);
575                                 }
576                                 else
577                                         Con_DPrintf("%s tried to %s\n", host_client->name, s);
578                                 break;
579
580                         case clc_disconnect:
581                                 return false;
582
583                         case clc_move:
584                                 SV_ReadClientMove (&host_client->cmd);
585                                 break;
586
587                         case clc_ackentities:
588                                 EntityFrame_AckFrame(&host_client->entitydatabase, MSG_ReadLong());
589                                 break;
590                         }
591                 }
592         } while (ret == 1);
593
594         return true;
595 }
596
597
598 /*
599 ==================
600 SV_RunClients
601 ==================
602 */
603 void SV_RunClients (void)
604 {
605         int                             i;
606
607         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
608         {
609                 if (!host_client->active)
610                         continue;
611         
612                 sv_player = host_client->edict;
613
614                 if (!SV_ReadClientMessage ())
615                 {
616                         SV_DropClient (false);  // client misbehaved...
617                         continue;
618                 }
619
620                 if (!host_client->spawned)
621                 {
622                 // clear client movement until a new packet is received
623                         memset (&host_client->cmd, 0, sizeof(host_client->cmd));
624                         continue;
625                 }
626
627 // always pause in single player if in console or menus
628                 if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
629                 {
630                         // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
631                         if (SV_PlayerPhysicsQC)
632                         {
633                                 pr_global_struct->time = sv.time;
634                                 pr_global_struct->self = EDICT_TO_PROG(sv_player);
635                                 PR_ExecuteProgram ((func_t)(SV_PlayerPhysicsQC - pr_functions), "");
636                         }
637                         else
638                                 SV_ClientThink ();
639                 }
640         }
641 }
642