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