1 /*
2 Copyright (C) 1996-2001 Id Software, Inc.
3 Copyright (C) 2002-2009 John Fitzgibbons and others
4 Copyright (C) 2010-2014 QuakeSpasm developers
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21 */
22 // sv_user.c -- server code for moving users
23
24 #include "quakedef.h"
25
26 edict_t *sv_player;
27
28 extern cvar_t sv_friction;
29 cvar_t sv_edgefriction = {"edgefriction", "2", CVAR_NONE};
30 extern cvar_t sv_stopspeed;
31
32 static vec3_t forward, right, up;
33
34 // world
35 float *angles;
36 float *origin;
37 float *velocity;
38
39 qboolean onground;
40
41 usercmd_t cmd;
42
43 cvar_t sv_idealpitchscale = {"sv_idealpitchscale","0.8",CVAR_NONE};
44 cvar_t sv_altnoclip = {"sv_altnoclip","1",CVAR_ARCHIVE}; //johnfitz
45
46 /*
47 ===============
48 SV_SetIdealPitch
49 ===============
50 */
51 #define MAX_FORWARD 6
SV_SetIdealPitch(void)52 void SV_SetIdealPitch (void)
53 {
54 float angleval, sinval, cosval;
55 trace_t tr;
56 vec3_t top, bottom;
57 float z[MAX_FORWARD];
58 int i, j;
59 int step, dir, steps;
60
61 if (!((int)sv_player->v.flags & FL_ONGROUND))
62 return;
63
64 angleval = sv_player->v.angles[YAW] * M_PI*2 / 360;
65 sinval = sin(angleval);
66 cosval = cos(angleval);
67
68 for (i=0 ; i<MAX_FORWARD ; i++)
69 {
70 top[0] = sv_player->v.origin[0] + cosval*(i+3)*12;
71 top[1] = sv_player->v.origin[1] + sinval*(i+3)*12;
72 top[2] = sv_player->v.origin[2] + sv_player->v.view_ofs[2];
73
74 bottom[0] = top[0];
75 bottom[1] = top[1];
76 bottom[2] = top[2] - 160;
77
78 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, 1, sv_player);
79 if (tr.allsolid)
80 return; // looking at a wall, leave ideal the way is was
81
82 if (tr.fraction == 1)
83 return; // near a dropoff
84
85 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
86 }
87
88 dir = 0;
89 steps = 0;
90 for (j=1 ; j<i ; j++)
91 {
92 step = z[j] - z[j-1];
93 if (step > -ON_EPSILON && step < ON_EPSILON)
94 continue;
95
96 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
97 return; // mixed changes
98
99 steps++;
100 dir = step;
101 }
102
103 if (!dir)
104 {
105 sv_player->v.idealpitch = 0;
106 return;
107 }
108
109 if (steps < 2)
110 return;
111 sv_player->v.idealpitch = -dir * sv_idealpitchscale.value;
112 }
113
114
115 /*
116 ==================
117 SV_UserFriction
118
119 ==================
120 */
SV_UserFriction(void)121 void SV_UserFriction (void)
122 {
123 float *vel;
124 float speed, newspeed, control;
125 vec3_t start, stop;
126 float friction;
127 trace_t trace;
128
129 vel = velocity;
130
131 speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1]);
132 if (!speed)
133 return;
134
135 // if the leading edge is over a dropoff, increase friction
136 start[0] = stop[0] = origin[0] + vel[0]/speed*16;
137 start[1] = stop[1] = origin[1] + vel[1]/speed*16;
138 start[2] = origin[2] + sv_player->v.mins[2];
139 stop[2] = start[2] - 34;
140
141 trace = SV_Move (start, vec3_origin, vec3_origin, stop, true, sv_player);
142
143 if (trace.fraction == 1.0)
144 friction = sv_friction.value*sv_edgefriction.value;
145 else
146 friction = sv_friction.value;
147
148 // apply friction
149 control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
150 newspeed = speed - host_frametime*control*friction;
151
152 if (newspeed < 0)
153 newspeed = 0;
154 newspeed /= speed;
155
156 vel[0] = vel[0] * newspeed;
157 vel[1] = vel[1] * newspeed;
158 vel[2] = vel[2] * newspeed;
159 }
160
161 /*
162 ==============
163 SV_Accelerate
164 ==============
165 */
166 cvar_t sv_maxspeed = {"sv_maxspeed", "320", CVAR_NOTIFY|CVAR_SERVERINFO};
167 cvar_t sv_accelerate = {"sv_accelerate", "10", CVAR_NONE};
SV_Accelerate(float wishspeed,const vec3_t wishdir)168 void SV_Accelerate (float wishspeed, const vec3_t wishdir)
169 {
170 int i;
171 float addspeed, accelspeed, currentspeed;
172
173 currentspeed = DotProduct (velocity, wishdir);
174 addspeed = wishspeed - currentspeed;
175 if (addspeed <= 0)
176 return;
177 accelspeed = sv_accelerate.value*host_frametime*wishspeed;
178 if (accelspeed > addspeed)
179 accelspeed = addspeed;
180
181 for (i=0 ; i<3 ; i++)
182 velocity[i] += accelspeed*wishdir[i];
183 }
184
SV_AirAccelerate(float wishspeed,vec3_t wishveloc)185 void SV_AirAccelerate (float wishspeed, vec3_t wishveloc)
186 {
187 int i;
188 float addspeed, wishspd, accelspeed, currentspeed;
189
190 wishspd = VectorNormalize (wishveloc);
191 if (wishspd > 30)
192 wishspd = 30;
193 currentspeed = DotProduct (velocity, wishveloc);
194 addspeed = wishspd - currentspeed;
195 if (addspeed <= 0)
196 return;
197 // accelspeed = sv_accelerate.value * host_frametime;
198 accelspeed = sv_accelerate.value*wishspeed * host_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
DropPunchAngle(void)207 void DropPunchAngle (void)
208 {
209 float len;
210
211 len = VectorNormalize (sv_player->v.punchangle);
212
213 len -= 10*host_frametime;
214 if (len < 0)
215 len = 0;
216 VectorScale (sv_player->v.punchangle, len, sv_player->v.punchangle);
217 }
218
219 /*
220 ===================
221 SV_WaterMove
222
223 ===================
224 */
SV_WaterMove(void)225 void SV_WaterMove (void)
226 {
227 int i;
228 vec3_t wishvel;
229 float speed, newspeed, wishspeed, addspeed, accelspeed;
230
231 //
232 // user intentions
233 //
234 AngleVectors (sv_player->v.v_angle, forward, right, up);
235
236 for (i=0 ; i<3 ; i++)
237 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
238
239 if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
240 wishvel[2] -= 60; // drift towards bottom
241 else
242 wishvel[2] += cmd.upmove;
243
244 wishspeed = VectorLength(wishvel);
245 if (wishspeed > sv_maxspeed.value)
246 {
247 VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel);
248 wishspeed = sv_maxspeed.value;
249 }
250 wishspeed *= 0.7;
251
252 //
253 // water friction
254 //
255 speed = VectorLength (velocity);
256 if (speed)
257 {
258 newspeed = speed - host_frametime * speed * sv_friction.value;
259 if (newspeed < 0)
260 newspeed = 0;
261 VectorScale (velocity, newspeed/speed, velocity);
262 }
263 else
264 newspeed = 0;
265
266 //
267 // water acceleration
268 //
269 if (!wishspeed)
270 return;
271
272 addspeed = wishspeed - newspeed;
273 if (addspeed <= 0)
274 return;
275
276 VectorNormalize (wishvel);
277 accelspeed = sv_accelerate.value * wishspeed * host_frametime;
278 if (accelspeed > addspeed)
279 accelspeed = addspeed;
280
281 for (i=0 ; i<3 ; i++)
282 velocity[i] += accelspeed * wishvel[i];
283 }
284
SV_WaterJump(void)285 void SV_WaterJump (void)
286 {
287 if (qcvm->time > sv_player->v.teleport_time
288 || !sv_player->v.waterlevel)
289 {
290 sv_player->v.flags = (int)sv_player->v.flags & ~FL_WATERJUMP;
291 sv_player->v.teleport_time = 0;
292 }
293 sv_player->v.velocity[0] = sv_player->v.movedir[0];
294 sv_player->v.velocity[1] = sv_player->v.movedir[1];
295 }
296
297 /*
298 ===================
299 SV_NoclipMove -- johnfitz
300
301 new, alternate noclip. old noclip is still handled in SV_AirMove
302 ===================
303 */
SV_NoclipMove(void)304 void SV_NoclipMove (void)
305 {
306 AngleVectors (sv_player->v.v_angle, forward, right, up);
307
308 velocity[0] = forward[0]*cmd.forwardmove + right[0]*cmd.sidemove;
309 velocity[1] = forward[1]*cmd.forwardmove + right[1]*cmd.sidemove;
310 velocity[2] = forward[2]*cmd.forwardmove + right[2]*cmd.sidemove;
311 velocity[2] += cmd.upmove*2; //doubled to match running speed
312
313 if (VectorLength (velocity) > sv_maxspeed.value)
314 {
315 VectorNormalize (velocity);
316 VectorScale (velocity, sv_maxspeed.value, velocity);
317 }
318 }
319
320 /*
321 ===================
322 SV_AirMove
323 ===================
324 */
SV_AirMove(void)325 void SV_AirMove (void)
326 {
327 int i;
328 vec3_t wishvel, wishdir;
329 float wishspeed;
330 float fmove, smove;
331
332 AngleVectors (sv_player->v.angles, forward, right, up);
333
334 fmove = cmd.forwardmove;
335 smove = cmd.sidemove;
336
337 // hack to not let you back into teleporter
338 if (qcvm->time < sv_player->v.teleport_time && fmove < 0)
339 fmove = 0;
340
341 for (i=0 ; i<3 ; i++)
342 wishvel[i] = forward[i]*fmove + right[i]*smove;
343
344 if ( (int)sv_player->v.movetype != MOVETYPE_WALK)
345 wishvel[2] = cmd.upmove;
346 else
347 wishvel[2] = 0;
348
349 VectorCopy (wishvel, wishdir);
350 wishspeed = VectorNormalize(wishdir);
351 if (wishspeed > sv_maxspeed.value)
352 {
353 VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel);
354 wishspeed = sv_maxspeed.value;
355 }
356
357 if ( sv_player->v.movetype == MOVETYPE_NOCLIP)
358 { // noclip
359 VectorCopy (wishvel, velocity);
360 }
361 else if ( onground )
362 {
363 SV_UserFriction ();
364 SV_Accelerate (wishspeed, wishdir);
365 }
366 else
367 { // not on ground, so little effect on velocity
368 SV_AirAccelerate (wishspeed, wishvel);
369 }
370 }
371
372 /*
373 ===================
374 SV_ClientThink
375
376 the move fields specify an intended velocity in pix/sec
377 the angle fields specify an exact angular motion in degrees
378 ===================
379 */
SV_ClientThink(void)380 void SV_ClientThink (void)
381 {
382 vec3_t v_angle;
383
384 if (sv_player->v.movetype == MOVETYPE_NONE)
385 return;
386
387 onground = (int)sv_player->v.flags & FL_ONGROUND;
388
389 origin = sv_player->v.origin;
390 velocity = sv_player->v.velocity;
391
392 DropPunchAngle ();
393
394 //
395 // if dead, behave differently
396 //
397 if (sv_player->v.health <= 0)
398 return;
399
400 //
401 // angles
402 // show 1/3 the pitch angle and all the roll angle
403 cmd = host_client->cmd;
404 angles = sv_player->v.angles;
405
406 VectorAdd (sv_player->v.v_angle, sv_player->v.punchangle, v_angle);
407 angles[ROLL] = V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4;
408 if (!sv_player->v.fixangle)
409 {
410 angles[PITCH] = -v_angle[PITCH]/3;
411 angles[YAW] = v_angle[YAW];
412 }
413
414 if ( (int)sv_player->v.flags & FL_WATERJUMP )
415 {
416 SV_WaterJump ();
417 return;
418 }
419 //
420 // walk
421 //
422 //johnfitz -- alternate noclip
423 if (sv_player->v.movetype == MOVETYPE_NOCLIP && sv_altnoclip.value)
424 SV_NoclipMove ();
425 else if (sv_player->v.waterlevel >= 2 && sv_player->v.movetype != MOVETYPE_NOCLIP)
426 SV_WaterMove ();
427 else
428 SV_AirMove ();
429 //johnfitz
430 }
431
432
433 /*
434 ===================
435 SV_ReadClientMove
436 ===================
437 */
SV_ReadClientMove(usercmd_t * move)438 void SV_ReadClientMove (usercmd_t *move)
439 {
440 int i;
441 vec3_t angle;
442 int buttonbits;
443 int newimpulse;
444 qboolean drop = false;
445 vec3_t movevalues;
446 int sequence;
447
448 if (host_client->protocol_pext2 & PEXT2_PREDINFO)
449 {
450 i = (unsigned short)MSG_ReadShort();
451 sequence = (host_client->lastmovemessage & 0xffff0000) | (i&0xffff);
452
453 //tollerance of a few old frames, so we can have redundancy for packetloss
454 if (sequence+0x100 < host_client->lastmovemessage)
455 sequence += 0x10000;
456
457 if (sequence <= host_client->lastmovemessage)
458 drop = true;
459 }
460 else
461 sequence = 0;
462
463 // read ping time
464 host_client->ping_times[host_client->num_pings%NUM_PING_TIMES]
465 = qcvm->time - MSG_ReadFloat ();
466 host_client->num_pings++;
467
468 for (i=0 ; i<3 ; i++)
469 {
470 if (sv.protocol == PROTOCOL_NETQUAKE && !(host_client->protocol_pext2 & PEXT2_PREDINFO))
471 angle[i] = MSG_ReadAngle (sv.protocolflags);
472 else
473 angle[i] = MSG_ReadAngle16 (sv.protocolflags); //johnfitz -- 16-bit angles for PROTOCOL_FITZQUAKE
474 }
475 movevalues[0] = MSG_ReadShort ();
476 movevalues[1] = MSG_ReadShort ();
477 movevalues[2] = MSG_ReadShort ();
478 buttonbits = MSG_ReadByte();
479 newimpulse = MSG_ReadByte();
480
481 if (drop)
482 return; //okay, we don't care about that then
483
484 // calc ping times
485 host_client->lastmovemessage = sequence;
486
487 // read movement
488 VectorCopy (angle, host_client->edict->v.v_angle);
489 move->forwardmove = movevalues[0];
490 move->sidemove = movevalues[1];
491 move->upmove = movevalues[2];
492
493 // read buttons
494 host_client->edict->v.button0 = (buttonbits & 1)>>0;
495 //button1 was meant to be 'use', but got reused by too many mods to get implemented now
496 host_client->edict->v.button2 = (buttonbits & 2)>>1;
497
498 if (newimpulse)
499 host_client->edict->v.impulse = newimpulse;
500 }
501
502 /*
503 ===================
504 SV_ReadClientMessage
505
506 Returns false if the client should be killed
507 ===================
508 */
SV_ReadClientMessage(void)509 qboolean SV_ReadClientMessage (void)
510 {
511 int ccmd;
512 const char *s;
513
514 MSG_BeginReading ();
515
516 while (1)
517 {
518 if (!host_client->active)
519 return false; // a command caused an error
520
521 if (msg_badread)
522 {
523 Sys_Printf ("SV_ReadClientMessage: badread\n");
524 return false;
525 }
526
527 ccmd = MSG_ReadChar ();
528
529 switch (ccmd)
530 {
531 case -1:
532 return true; //msg_badread, meaning we just hit eof.
533
534 default:
535 Sys_Printf ("SV_ReadClientMessage: unknown command char\n");
536 return false;
537
538 case clc_nop:
539 // Sys_Printf ("clc_nop\n");
540 break;
541
542 case clc_stringcmd:
543 s = MSG_ReadString ();
544 if (q_strncasecmp(s, "spawn", 5) && q_strncasecmp(s, "begin", 5) && q_strncasecmp(s, "prespawn", 8) && qcvm->extfuncs.SV_ParseClientCommand)
545 { //the spawn/begin/prespawn are because of numerous mods that disobey the rules.
546 //at a minimum, we must be able to join the server, so that we can see any sprints/bprints (because dprint sucks, yes there's proper ways to deal with this, but moders don't always know them).
547 client_t *ohc = host_client;
548 G_INT(OFS_PARM0) = PR_SetEngineString(s);
549 pr_global_struct->time = qcvm->time;
550 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
551 PR_ExecuteProgram(qcvm->extfuncs.SV_ParseClientCommand);
552 host_client = ohc;
553 }
554 else
555 Cmd_ExecuteString (s, src_client);
556 break;
557
558 case clc_disconnect:
559 // Sys_Printf ("SV_ReadClientMessage: client disconnected\n");
560 return false;
561
562 case clc_move:
563 if (!host_client->spawned)
564 return true; //this is to suck up any stale moves on map changes, so we don't get confused (quite so easily) when protocols are changed between maps
565 SV_ReadClientMove (&host_client->cmd);
566 break;
567 case clcdp_ackframe:
568 SVFTE_Ack(host_client, MSG_ReadLong());
569 break;
570 }
571 }
572
573 return true;
574 }
575
576
577 /*
578 ==================
579 SV_RunClients
580 ==================
581 */
SV_RunClients(void)582 void SV_RunClients (void)
583 {
584 int i;
585
586 //receive from clients first
587 //Spike -- reworked this to query the network code for an active connection.
588 //this allows the network code to serve multiple clients with the same listening port.
589 //this solves server-side nats, which is important for coop etc.
590 while(1)
591 {
592 struct qsocket_s *sock = NET_GetServerMessage();
593 if (!sock)
594 break; //no more this frame
595
596 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
597 {
598 if (host_client->netconnection == sock)
599 {
600 sv_player = host_client->edict;
601 if (!SV_ReadClientMessage ())
602 {
603 SV_DropClient (false); // client misbehaved...
604 break;
605 }
606 }
607 }
608 }
609
610 //then do the per-frame stuff
611 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
612 {
613 if (!host_client->active)
614 continue;
615
616 sv_player = host_client->edict;
617
618 if (!host_client->spawned)
619 {
620 // clear client movement until a new packet is received
621 memset (&host_client->cmd, 0, sizeof(host_client->cmd));
622 continue;
623 }
624
625 if (!host_client->netconnection)
626 {
627 host_client->cmd.viewangles[0] = host_client->edict->v.v_angle[0];
628 host_client->cmd.viewangles[1] = host_client->edict->v.v_angle[1];
629 host_client->cmd.viewangles[2] = host_client->edict->v.v_angle[2];
630 }
631
632 // always pause in single player if in console or menus
633 if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
634 SV_ClientThink ();
635 }
636 }
637
638