2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // sv_user.c -- server code for moving users
26 cvar_t sv_edgefriction = {0, "edgefriction", "2"};
27 cvar_t sv_deltacompress = {0, "sv_deltacompress", "1"};
28 cvar_t sv_idealpitchscale = {0, "sv_idealpitchscale","0.8"};
29 cvar_t sv_maxspeed = {CVAR_NOTIFY, "sv_maxspeed", "320"};
30 cvar_t sv_accelerate = {0, "sv_accelerate", "10"};
41 void SV_SetIdealPitch (void)
43 float angleval, sinval, cosval;
50 if (!((int)sv_player->v->flags & FL_ONGROUND))
53 angleval = sv_player->v->angles[YAW] * M_PI*2 / 360;
54 sinval = sin(angleval);
55 cosval = cos(angleval);
57 for (i=0 ; i<MAX_FORWARD ; i++)
59 top[0] = sv_player->v->origin[0] + cosval*(i+3)*12;
60 top[1] = sv_player->v->origin[1] + sinval*(i+3)*12;
61 top[2] = sv_player->v->origin[2] + sv_player->v->view_ofs[2];
65 bottom[2] = top[2] - 160;
67 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, MOVE_NOMONSTERS, sv_player);
68 // if looking at a wall, leave ideal the way is was
76 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
84 if (step > -ON_EPSILON && step < ON_EPSILON)
88 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
97 sv_player->v->idealpitch = 0;
103 sv_player->v->idealpitch = -dir * sv_idealpitchscale.value;
107 static vec3_t wishdir, forward, right, up;
108 static float wishspeed;
110 static qboolean onground;
118 void SV_UserFriction (void)
120 float speed, newspeed, control, friction;
124 speed = sqrt(sv_player->v->velocity[0]*sv_player->v->velocity[0]+sv_player->v->velocity[1]*sv_player->v->velocity[1]);
128 // if the leading edge is over a dropoff, increase friction
129 start[0] = stop[0] = sv_player->v->origin[0] + sv_player->v->velocity[0]/speed*16;
130 start[1] = stop[1] = sv_player->v->origin[1] + sv_player->v->velocity[1]/speed*16;
131 start[2] = sv_player->v->origin[2] + sv_player->v->mins[2];
132 stop[2] = start[2] - 34;
134 trace = SV_Move (start, vec3_origin, vec3_origin, stop, MOVE_NOMONSTERS, sv_player);
136 if (trace.fraction == 1.0)
137 friction = sv_friction.value*sv_edgefriction.value;
139 friction = sv_friction.value;
142 control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
143 newspeed = speed - sv.frametime*control*friction;
150 VectorScale(sv_player->v->velocity, newspeed, sv_player->v->velocity);
158 void SV_Accelerate (void)
161 float addspeed, accelspeed, currentspeed;
163 currentspeed = DotProduct (sv_player->v->velocity, wishdir);
164 addspeed = wishspeed - currentspeed;
167 accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
168 if (accelspeed > addspeed)
169 accelspeed = addspeed;
171 for (i=0 ; i<3 ; i++)
172 sv_player->v->velocity[i] += accelspeed*wishdir[i];
175 void SV_AirAccelerate (vec3_t wishveloc)
178 float addspeed, wishspd, accelspeed, currentspeed;
180 wishspd = VectorNormalizeLength (wishveloc);
183 currentspeed = DotProduct (sv_player->v->velocity, wishveloc);
184 addspeed = wishspd - currentspeed;
187 accelspeed = sv_accelerate.value*wishspeed * sv.frametime;
188 if (accelspeed > addspeed)
189 accelspeed = addspeed;
191 for (i=0 ; i<3 ; i++)
192 sv_player->v->velocity[i] += accelspeed*wishveloc[i];
196 void DropPunchAngle (void)
201 len = VectorNormalizeLength (sv_player->v->punchangle);
203 len -= 10*sv.frametime;
206 VectorScale (sv_player->v->punchangle, len, sv_player->v->punchangle);
208 if ((val = GETEDICTFIELDVALUE(sv_player, eval_punchvector)))
210 len = VectorNormalizeLength (val->vector);
212 len -= 20*sv.frametime;
215 VectorScale (val->vector, len, val->vector);
224 void SV_FreeMove (void)
229 AngleVectors (sv_player->v->v_angle, forward, right, up);
231 for (i = 0; i < 3; i++)
232 sv_player->v->velocity[i] = forward[i] * cmd.forwardmove + right[i] * cmd.sidemove;
234 sv_player->v->velocity[2] += cmd.upmove;
236 wishspeed = VectorLength(sv_player->v->velocity);
237 if (wishspeed > sv_maxspeed.value)
238 VectorScale(sv_player->v->velocity, sv_maxspeed.value / wishspeed, sv_player->v->velocity);
247 void SV_WaterMove (void)
251 float speed, newspeed, wishspeed, addspeed, accelspeed, temp;
254 AngleVectors (sv_player->v->v_angle, forward, right, up);
256 for (i=0 ; i<3 ; i++)
257 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
259 if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
260 wishvel[2] -= 60; // drift towards bottom
262 wishvel[2] += cmd.upmove;
264 wishspeed = VectorLength(wishvel);
265 if (wishspeed > sv_maxspeed.value)
267 temp = sv_maxspeed.value/wishspeed;
268 VectorScale (wishvel, temp, wishvel);
269 wishspeed = sv_maxspeed.value;
274 speed = VectorLength(sv_player->v->velocity);
277 newspeed = speed - sv.frametime * speed * sv_friction.value;
280 temp = newspeed/speed;
281 VectorScale(sv_player->v->velocity, temp, sv_player->v->velocity);
286 // water acceleration
290 addspeed = wishspeed - newspeed;
294 VectorNormalize (wishvel);
295 accelspeed = sv_accelerate.value * wishspeed * sv.frametime;
296 if (accelspeed > addspeed)
297 accelspeed = addspeed;
299 for (i=0 ; i<3 ; i++)
300 sv_player->v->velocity[i] += accelspeed * wishvel[i];
303 void SV_WaterJump (void)
305 if (sv.time > sv_player->v->teleport_time || !sv_player->v->waterlevel)
307 sv_player->v->flags = (int)sv_player->v->flags & ~FL_WATERJUMP;
308 sv_player->v->teleport_time = 0;
310 sv_player->v->velocity[0] = sv_player->v->movedir[0];
311 sv_player->v->velocity[1] = sv_player->v->movedir[1];
321 void SV_AirMove (void)
325 float fmove, smove, temp;
327 // LordHavoc: correct quake movement speed bug when looking up/down
328 wishvel[0] = wishvel[2] = 0;
329 wishvel[1] = sv_player->v->angles[1];
330 AngleVectors (wishvel, forward, right, up);
332 fmove = cmd.forwardmove;
333 smove = cmd.sidemove;
335 // hack to not let you back into teleporter
336 if (sv.time < sv_player->v->teleport_time && fmove < 0)
339 for (i=0 ; i<3 ; i++)
340 wishvel[i] = forward[i]*fmove + right[i]*smove;
342 if ((int)sv_player->v->movetype != MOVETYPE_WALK)
343 wishvel[2] += cmd.upmove;
345 VectorCopy (wishvel, wishdir);
346 wishspeed = VectorNormalizeLength(wishdir);
347 if (wishspeed > sv_maxspeed.value)
349 temp = sv_maxspeed.value/wishspeed;
350 VectorScale (wishvel, temp, wishvel);
351 wishspeed = sv_maxspeed.value;
354 if (sv_player->v->movetype == MOVETYPE_NOCLIP)
357 VectorCopy (wishvel, sv_player->v->velocity);
366 // not on ground, so little effect on velocity
367 SV_AirAccelerate (wishvel);
375 the move fields specify an intended velocity in pix/sec
376 the angle fields specify an exact angular motion in degrees
379 void SV_ClientThink (void)
383 if (sv_player->v->movetype == MOVETYPE_NONE)
386 onground = (int)sv_player->v->flags & FL_ONGROUND;
390 // if dead, behave differently
391 if (sv_player->v->health <= 0)
394 cmd = host_client->cmd;
397 // show 1/3 the pitch angle and all the roll angle
398 VectorAdd (sv_player->v->v_angle, sv_player->v->punchangle, v_angle);
399 sv_player->v->angles[ROLL] = V_CalcRoll (sv_player->v->angles, sv_player->v->velocity)*4;
400 if (!sv_player->v->fixangle)
402 sv_player->v->angles[PITCH] = -v_angle[PITCH]/3;
403 sv_player->v->angles[YAW] = v_angle[YAW];
406 if ( (int)sv_player->v->flags & FL_WATERJUMP )
413 // Player is (somehow) outside of the map, or flying, or noclipping
414 if (sv_player->v->movetype != MOVETYPE_NOCLIP && (sv_player->v->movetype == MOVETYPE_FLY || SV_TestEntityPosition (sv_player)))
415 //if (sv_player->v->movetype == MOVETYPE_NOCLIP || sv_player->v->movetype == MOVETYPE_FLY || SV_TestEntityPosition (sv_player))
423 if ((sv_player->v->waterlevel >= 2) && (sv_player->v->movetype != MOVETYPE_NOCLIP))
434 extern cvar_t cl_rollspeed;
435 extern cvar_t cl_rollangle;
436 void SV_ClientThink(void)
439 vec3_t wishvel, wishdir, v, v_forward, v_right, v_up, start, stop;
440 float wishspeed, f, limit;
443 if (sv_player->v->movetype == MOVETYPE_NONE)
446 f = DotProduct(sv_player->v->punchangle, sv_player->v->punchangle);
450 f = (limit - 10 * sv.frametime);
453 VectorScale(sv_player->v->punchangle, f, sv_player->v->punchangle);
456 // if dead, behave differently
457 if (sv_player->v->health <= 0)
460 AngleVectors(sv_player->v->v_angle, v_forward, v_right, v_up);
461 // show 1/3 the pitch angle and all the roll angle
462 f = DotProduct(sv_player->v->velocity, v_right) * (1.0 / cl_rollspeed.value);
463 sv_player->v->angles[2] = bound(-1, f, 1) * cl_rollangle.value * 4;
464 if (!sv_player->v->fixangle)
466 sv_player->v->angles[0] = (sv_player->v->v_angle[0] + sv_player->v->punchangle[0]) * -0.333;
467 sv_player->v->angles[1] = sv_player->v->v_angle[1] + sv_player->v->punchangle[1];
470 if ((int)sv_player->v->flags & FL_WATERJUMP)
472 sv_player->v->velocity[0] = sv_player->v->movedir[0];
473 sv_player->v->velocity[1] = sv_player->v->movedir[1];
474 if (sv.time > sv_player->v->teleport_time || sv_player->v->waterlevel == 0)
476 sv_player->v->flags = (int)sv_player->v->flags - ((int)sv_player->v->flags & FL_WATERJUMP);
477 sv_player->v->teleport_time = 0;
483 if (sv_player->v->waterlevel >= 2)
484 if (sv_player->v->movetype != MOVETYPE_NOCLIP)
486 if (host_client->cmd.forwardmove == 0 && host_client->cmd.sidemove == 0 && host_client->cmd.upmove == 0)
488 // drift towards bottom
495 for (j = 0;j < 3;j++)
496 wishvel[j] = v_forward[j] * host_client->cmd.forwardmove + v_right[j] * host_client->cmd.sidemove;
497 wishvel[2] += host_client->cmd.upmove;
500 wishspeed = VectorLength(wishvel);
501 wishspeed = min(wishspeed, sv_maxspeed.value) * 0.7;
504 f = VectorLength(sv_player->v->velocity) * (1 - sv.frametime * sv_friction.value);
506 f /= VectorLength(sv_player->v->velocity);
509 VectorScale(sv_player->v->velocity, f, sv_player->v->velocity);
511 // water acceleration
516 limit = sv_accelerate.value * wishspeed * sv.frametime;
519 limit = VectorLength(wishvel);
522 VectorMA(sv_player->v->velocity, f, wishvel, sv_player->v->velocity);
526 // if not flying, move horizontally only
527 if (sv_player->v->movetype != MOVETYPE_FLY)
529 VectorClear(wishvel);
530 wishvel[1] = sv_player->v->v_angle[1];
531 AngleVectors(wishvel, v_forward, v_right, v_up);
534 // hack to not let you back into teleporter
535 VectorScale(v_right, host_client->cmd.sidemove, wishvel);
536 if (sv.time >= sv_player->v->teleport_time || host_client->cmd.forwardmove > 0)
537 VectorMA(wishvel, host_client->cmd.forwardmove, v_forward, wishvel);
538 if (sv_player->v->movetype != MOVETYPE_WALK)
539 wishvel[2] += cmd.upmove;
541 VectorCopy(wishvel, wishdir);
542 VectorNormalize(wishdir);
543 wishspeed = VectorLength(wishvel);
544 if (wishspeed > sv_maxspeed.value)
545 wishspeed = sv_maxspeed.value;
547 if (sv_player->v->movetype == MOVETYPE_NOCLIP || sv_player->v->movetype == MOVETYPE_FLY)
549 VectorScale(wishdir, wishspeed, sv_player->v->velocity);
553 if ((int)sv_player->v->flags & FL_ONGROUND) // walking
556 f = sv_player->v->velocity[0] * sv_player->v->velocity[0] + sv_player->v->velocity[1] * sv_player->v->velocity[1];
560 VectorCopy(sv_player->v->velocity, v);
563 // if the leading edge is over a dropoff, increase friction
565 VectorMA(sv_player->v->origin, limit, v, v);
566 v[2] += sv_player->v->mins[2];
568 VectorCopy(v, start);
571 trace = SV_Move(start, vec3_origin, vec3_origin, stop, MOVE_NOMONSTERS, sv_player);
574 if (f < sv_stopspeed.value)
575 f = sv_stopspeed.value / f;
578 if (trace.fraction == 1)
579 f *= sv_edgefriction.value;
580 f = 1 - sv.frametime * f * sv_friction.value;
584 VectorScale(sv_player->v->velocity, f, sv_player->v->velocity);
588 wishspeed = min(wishspeed, 30);
590 // ground or air acceleration
591 f = wishspeed - DotProduct(sv_player->v->velocity, wishdir);
594 limit = sv_accelerate.value * sv.frametime * wishspeed;
597 VectorMA(sv_player->v->velocity, f, wishdir, sv_player->v->velocity);
607 void SV_ReadClientMove (usercmd_t *move)
616 host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = sv.time - MSG_ReadFloat ();
617 host_client->num_pings++;
618 for (i=0, total = 0;i < NUM_PING_TIMES;i++)
619 total += host_client->ping_times[i];
620 // can be used for prediction
621 host_client->ping = total / NUM_PING_TIMES;
622 if ((val = GETEDICTFIELDVALUE(sv_player, eval_ping)))
623 val->_float = host_client->ping * 1000.0;
625 // read current angles
626 // PROTOCOL_DARKPLACES4
627 for (i = 0;i < 3;i++)
628 angle[i] = MSG_ReadPreciseAngle();
630 VectorCopy (angle, sv_player->v->v_angle);
633 move->forwardmove = MSG_ReadShort ();
634 move->sidemove = MSG_ReadShort ();
635 move->upmove = MSG_ReadShort ();
636 if ((val = GETEDICTFIELDVALUE(sv_player, eval_movement)))
638 val->vector[0] = move->forwardmove;
639 val->vector[1] = move->sidemove;
640 val->vector[2] = move->upmove;
644 bits = MSG_ReadByte ();
645 sv_player->v->button0 = bits & 1;
646 sv_player->v->button2 = (bits & 2)>>1;
647 // LordHavoc: added 6 new buttons
648 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button3))) val->_float = ((bits >> 2) & 1);
649 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button4))) val->_float = ((bits >> 3) & 1);
650 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button5))) val->_float = ((bits >> 4) & 1);
651 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button6))) val->_float = ((bits >> 5) & 1);
652 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button7))) val->_float = ((bits >> 6) & 1);
653 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button8))) val->_float = ((bits >> 7) & 1);
657 sv_player->v->impulse = i;
665 extern void SV_SendServerinfo(client_t *client);
666 void SV_ReadClientMessage(void)
671 //MSG_BeginReading ();
675 if (!host_client->active)
677 // a command caused an error
678 SV_DropClient (false);
684 Con_Printf ("SV_ReadClientMessage: badread\n");
685 SV_DropClient (false);
689 cmd = MSG_ReadChar ();
699 Con_Printf ("SV_ReadClientMessage: unknown command char %i\n", cmd);
700 SV_DropClient (false);
707 s = MSG_ReadString ();
708 if (strncasecmp(s, "spawn", 5) == 0
709 || strncasecmp(s, "begin", 5) == 0
710 || strncasecmp(s, "prespawn", 8) == 0)
711 Cmd_ExecuteString (s, src_client);
712 else if (SV_ParseClientCommandQC)
714 G_INT(OFS_PARM0) = PR_SetString(s);
715 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
716 PR_ExecuteProgram ((func_t)(SV_ParseClientCommandQC - pr_functions), "");
718 else if (strncasecmp(s, "status", 6) == 0
719 || strncasecmp(s, "name", 4) == 0
720 || strncasecmp(s, "say", 3) == 0
721 || strncasecmp(s, "say_team", 8) == 0
722 || strncasecmp(s, "tell", 4) == 0
723 || strncasecmp(s, "color", 5) == 0
724 || strncasecmp(s, "kill", 4) == 0
725 || strncasecmp(s, "pause", 5) == 0
726 || strncasecmp(s, "kick", 4) == 0
727 || strncasecmp(s, "ping", 4) == 0
728 || strncasecmp(s, "ban", 3) == 0
729 || strncasecmp(s, "pmodel", 6) == 0
730 || (gamemode == GAME_NEHAHRA && (strncasecmp(s, "max", 3) == 0 || strncasecmp(s, "monster", 7) == 0 || strncasecmp(s, "scrag", 5) == 0 || strncasecmp(s, "gimme", 5) == 0 || strncasecmp(s, "wraith", 6) == 0))
731 || (gamemode != GAME_NEHAHRA && (strncasecmp(s, "god", 3) == 0 || strncasecmp(s, "notarget", 8) == 0 || strncasecmp(s, "fly", 3) == 0 || strncasecmp(s, "give", 4) == 0 || strncasecmp(s, "noclip", 6) == 0)))
732 Cmd_ExecuteString (s, src_client);
734 Con_Printf("%s tried to %s\n", host_client->name, s);
738 SV_DropClient (false); // client wants to disconnect
742 SV_ReadClientMove (&host_client->cmd);
745 case clc_ackentities:
746 host_client->entitydatabase4->ackframenum = MSG_ReadLong();
747 if (developer_networkentities.integer >= 1)
748 Con_Printf("recv clc_ackentities %i\n", host_client->entitydatabase4->ackframenum);
749 EntityFrame4_AckFrame(host_client->entitydatabase4, host_client->entitydatabase4->ackframenum);
760 void SV_RunClients (void)
764 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
766 if (!host_client->active)
769 if (!host_client->spawned)
771 // clear client movement until a new packet is received
772 memset (&host_client->cmd, 0, sizeof(host_client->cmd));
778 sv_player = host_client->edict;
780 // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
781 if (SV_PlayerPhysicsQC)
783 pr_global_struct->time = sv.time;
784 pr_global_struct->self = EDICT_TO_PROG(sv_player);
785 PR_ExecuteProgram ((func_t)(SV_PlayerPhysicsQC - pr_functions), "");
790 SV_CheckVelocity(sv_player);
792 // LordHavoc: a hack to ensure that the (rather silly) id1 quakec
793 // player_run/player_stand1 does not horribly malfunction if the
794 // velocity becomes a number that is both == 0 and != 0
795 // (sounds to me like NaN but to be absolutely safe...)
796 if (DotProduct(sv_player->v->velocity, sv_player->v->velocity) < 0.0001)
797 VectorClear(sv_player->v->velocity);