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 = {"edgefriction", "2"};
27 cvar_t sv_predict = {"sv_predict", "1"};
28 cvar_t sv_deltacompress = {"sv_deltacompress", "1"};
30 static vec3_t forward, right, up;
44 cvar_t sv_idealpitchscale = {"sv_idealpitchscale","0.8"};
53 void SV_SetIdealPitch (void)
55 float angleval, sinval, cosval;
62 if (!((int)sv_player->v.flags & FL_ONGROUND))
65 angleval = sv_player->v.angles[YAW] * M_PI*2 / 360;
66 sinval = sin(angleval);
67 cosval = cos(angleval);
69 for (i=0 ; i<MAX_FORWARD ; i++)
71 top[0] = sv_player->v.origin[0] + cosval*(i+3)*12;
72 top[1] = sv_player->v.origin[1] + sinval*(i+3)*12;
73 top[2] = sv_player->v.origin[2] + sv_player->v.view_ofs[2];
77 bottom[2] = top[2] - 160;
79 tr = SV_Move (top, vec3_origin, vec3_origin, bottom, 1, sv_player);
81 return; // looking at a wall, leave ideal the way is was
84 return; // near a dropoff
86 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
94 if (step > -ON_EPSILON && step < ON_EPSILON)
97 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
98 return; // mixed changes
106 sv_player->v.idealpitch = 0;
112 sv_player->v.idealpitch = -dir * sv_idealpitchscale.value;
122 void SV_UserFriction (void)
125 float speed, newspeed, control;
132 speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1]);
136 // if the leading edge is over a dropoff, increase friction
137 start[0] = stop[0] = origin[0] + vel[0]/speed*16;
138 start[1] = stop[1] = origin[1] + vel[1]/speed*16;
139 start[2] = origin[2] + sv_player->v.mins[2];
140 stop[2] = start[2] - 34;
142 trace = SV_Move (start, vec3_origin, vec3_origin, stop, true, sv_player);
144 if (trace.fraction == 1.0)
145 friction = sv_friction.value*sv_edgefriction.value;
147 friction = sv_friction.value;
150 control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
151 newspeed = speed - sv.frametime*control*friction;
158 vel[0] = vel[0] * newspeed;
159 vel[1] = vel[1] * newspeed;
160 vel[2] = vel[2] * newspeed;
168 cvar_t sv_maxspeed = {"sv_maxspeed", "320", false, true};
169 cvar_t sv_accelerate = {"sv_accelerate", "10"};
171 void SV_Accelerate (vec3_t wishvel)
174 float addspeed, accelspeed;
180 VectorSubtract (wishvel, velocity, pushvec);
181 addspeed = VectorNormalize (pushvec);
183 accelspeed = sv_accelerate.value*sv.frametime*addspeed;
184 if (accelspeed > addspeed)
185 accelspeed = addspeed;
187 for (i=0 ; i<3 ; i++)
188 velocity[i] += accelspeed*pushvec[i];
191 void SV_Accelerate (void)
194 float addspeed, accelspeed, currentspeed;
196 currentspeed = DotProduct (velocity, wishdir);
197 addspeed = wishspeed - currentspeed;
200 accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
201 if (accelspeed > addspeed)
202 accelspeed = addspeed;
204 for (i=0 ; i<3 ; i++)
205 velocity[i] += accelspeed*wishdir[i];
208 void SV_AirAccelerate (vec3_t wishveloc)
211 float addspeed, wishspd, accelspeed, currentspeed;
213 wishspd = VectorNormalizeLength (wishveloc);
216 currentspeed = DotProduct (velocity, wishveloc);
217 addspeed = wishspd - currentspeed;
220 // accelspeed = sv_accelerate.value * sv.frametime;
221 accelspeed = sv_accelerate.value*wishspeed * sv.frametime;
222 if (accelspeed > addspeed)
223 accelspeed = addspeed;
225 for (i=0 ; i<3 ; i++)
226 velocity[i] += accelspeed*wishveloc[i];
230 void DropPunchAngle (void)
235 len = VectorNormalizeLength (sv_player->v.punchangle);
237 len -= 10*sv.frametime;
240 VectorScale (sv_player->v.punchangle, len, sv_player->v.punchangle);
242 if ((val = GETEDICTFIELDVALUE(sv_player, eval_punchvector)))
244 len = VectorNormalizeLength (val->vector);
246 len -= 20*sv.frametime;
249 VectorScale (val->vector, len, val->vector);
259 void SV_WaterMove (void)
263 float speed, newspeed, wishspeed, addspeed, accelspeed;
268 AngleVectors (sv_player->v.v_angle, forward, right, up);
270 for (i=0 ; i<3 ; i++)
271 wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove;
273 if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove)
274 wishvel[2] -= 60; // drift towards bottom
276 wishvel[2] += cmd.upmove;
278 wishspeed = Length(wishvel);
279 if (wishspeed > sv_maxspeed.value)
281 VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel);
282 wishspeed = sv_maxspeed.value;
289 speed = Length (velocity);
292 newspeed = speed - sv.frametime * speed * sv_friction.value;
295 VectorScale (velocity, newspeed/speed, velocity);
301 // water acceleration
306 addspeed = wishspeed - newspeed;
310 VectorNormalize (wishvel);
311 accelspeed = sv_accelerate.value * wishspeed * sv.frametime;
312 if (accelspeed > addspeed)
313 accelspeed = addspeed;
315 for (i=0 ; i<3 ; i++)
316 velocity[i] += accelspeed * wishvel[i];
319 void SV_WaterJump (void)
321 if (sv.time > sv_player->v.teleport_time
322 || !sv_player->v.waterlevel)
324 sv_player->v.flags = (int)sv_player->v.flags & ~FL_WATERJUMP;
325 sv_player->v.teleport_time = 0;
327 sv_player->v.velocity[0] = sv_player->v.movedir[0];
328 sv_player->v.velocity[1] = sv_player->v.movedir[1];
338 void SV_AirMove (void)
344 // LordHavoc: correct quake movement speed bug when looking up/down
345 wishvel[0] = wishvel[2] = 0;
346 wishvel[1] = sv_player->v.angles[1];
347 AngleVectors (wishvel, forward, right, up);
348 // AngleVectors (sv_player->v.angles, forward, right, up);
350 fmove = cmd.forwardmove;
351 smove = cmd.sidemove;
353 // hack to not let you back into teleporter
354 if (sv.time < sv_player->v.teleport_time && fmove < 0)
357 for (i=0 ; i<3 ; i++)
358 wishvel[i] = forward[i]*fmove + right[i]*smove;
360 if ( (int)sv_player->v.movetype != MOVETYPE_WALK)
361 wishvel[2] = cmd.upmove;
365 VectorCopy (wishvel, wishdir);
366 wishspeed = VectorNormalizeLength(wishdir);
367 if (wishspeed > sv_maxspeed.value)
369 VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel);
370 wishspeed = sv_maxspeed.value;
373 if ( sv_player->v.movetype == MOVETYPE_NOCLIP)
375 VectorCopy (wishvel, velocity);
383 { // not on ground, so little effect on velocity
384 SV_AirAccelerate (wishvel);
392 the move fields specify an intended velocity in pix/sec
393 the angle fields specify an exact angular motion in degrees
396 void SV_ClientThink (void)
400 if (sv_player->v.movetype == MOVETYPE_NONE)
403 onground = (int)sv_player->v.flags & FL_ONGROUND;
405 origin = sv_player->v.origin;
406 velocity = sv_player->v.velocity;
411 // if dead, behave differently
413 if (sv_player->v.health <= 0)
418 // show 1/3 the pitch angle and all the roll angle
419 cmd = host_client->cmd;
420 angles = sv_player->v.angles;
422 VectorAdd (sv_player->v.v_angle, sv_player->v.punchangle, v_angle);
423 angles[ROLL] = V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4;
424 if (!sv_player->v.fixangle)
426 // LordHavoc: pitch was ugly to begin with... removed except in water
427 if (sv_player->v.waterlevel >= 2)
428 angles[PITCH] = -v_angle[PITCH]/3;
431 angles[YAW] = v_angle[YAW];
434 if ( (int)sv_player->v.flags & FL_WATERJUMP )
442 if ( (sv_player->v.waterlevel >= 2) && (sv_player->v.movetype != MOVETYPE_NOCLIP) )
457 void SV_ReadClientMove (usercmd_t *move)
466 host_client->ping_times[host_client->num_pings%NUM_PING_TIMES]
467 = sv.time - MSG_ReadFloat ();
468 host_client->num_pings++;
469 for (i=0, total = 0;i < NUM_PING_TIMES;i++)
470 total += host_client->ping_times[i];
471 host_client->ping = total / NUM_PING_TIMES; // can be used for prediction
472 host_client->latency = 0;
473 if (sv_predict.value && (svs.maxclients > 1) && (!sv.paused)) // if paused or a local game, don't predict
474 host_client->latency = host_client->ping;
475 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_ping)))
476 val->_float = host_client->ping * 1000.0;
478 // read current angles
481 for (i=0 ; i<3 ; i++)
482 angle[i] = MSG_ReadPreciseAngle ();
486 for (i=0 ; i<3 ; i++)
487 angle[i] = MSG_ReadAngle ();
490 VectorCopy (angle, host_client->edict->v.v_angle);
493 move->forwardmove = MSG_ReadShort ();
494 move->sidemove = MSG_ReadShort ();
495 move->upmove = MSG_ReadShort ();
496 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_movement)))
498 val->vector[0] = move->forwardmove;
499 val->vector[1] = move->sidemove;
500 val->vector[2] = move->upmove;
504 bits = MSG_ReadByte ();
505 host_client->edict->v.button0 = bits & 1;
506 host_client->edict->v.button2 = (bits & 2)>>1;
507 // LordHavoc: added 6 new buttons
508 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button3))) val->_float = ((bits >> 2) & 1);
509 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button4))) val->_float = ((bits >> 3) & 1);
510 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button5))) val->_float = ((bits >> 4) & 1);
511 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button6))) val->_float = ((bits >> 5) & 1);
512 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button7))) val->_float = ((bits >> 6) & 1);
513 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_button8))) val->_float = ((bits >> 7) & 1);
517 host_client->edict->v.impulse = i;
524 Returns false if the client should be killed
527 qboolean SV_ReadClientMessage (void)
536 ret = NET_GetMessage (host_client->netconnection);
539 Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n");
549 if (!host_client->active)
550 return false; // a command caused an error
554 Sys_Printf ("SV_ReadClientMessage: badread\n");
558 cmd = MSG_ReadChar ();
563 goto nextmsg; // end of message
566 Sys_Printf ("SV_ReadClientMessage: unknown command char\n");
570 // Sys_Printf ("clc_nop\n");
574 s = MSG_ReadString ();
576 if (Q_strncasecmp(s, "status", 6) == 0
577 || Q_strncasecmp(s, "name", 4) == 0
578 || Q_strncasecmp(s, "say", 3) == 0
579 || Q_strncasecmp(s, "say_team", 8) == 0
580 || Q_strncasecmp(s, "tell", 4) == 0
581 || Q_strncasecmp(s, "color", 5) == 0
582 || Q_strncasecmp(s, "kill", 4) == 0
583 || Q_strncasecmp(s, "pause", 5) == 0
584 || Q_strncasecmp(s, "spawn", 5) == 0
585 || Q_strncasecmp(s, "begin", 5) == 0
586 || Q_strncasecmp(s, "prespawn", 8) == 0
587 || Q_strncasecmp(s, "kick", 4) == 0
588 || Q_strncasecmp(s, "ping", 4) == 0
589 || Q_strncasecmp(s, "ban", 3) == 0
590 || Q_strncasecmp(s, "pmodel", 6) == 0
591 || (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))
592 || (!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)))
595 Cmd_ExecuteString (s, src_client);
598 Con_DPrintf("%s tried to %s\n", host_client->name, s);
603 Cmd_ExecuteString (s, src_client);
605 Con_DPrintf("%s tried to %s\n", host_client->name, s);
610 // Sys_Printf ("SV_ReadClientMessage: client disconnected\n");
614 SV_ReadClientMove (&host_client->cmd);
629 void SV_RunClients (void)
633 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
635 if (!host_client->active)
638 sv_player = host_client->edict;
640 if (!SV_ReadClientMessage ())
642 SV_DropClient (false); // client misbehaved...
646 if (!host_client->spawned)
648 // clear client movement until a new packet is received
649 memset (&host_client->cmd, 0, sizeof(host_client->cmd));
653 // always pause in single player if in console or menus
654 if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
656 // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
657 if (SV_PlayerPhysicsQC)
659 pr_global_struct->time = sv.time;
660 pr_global_struct->self = EDICT_TO_PROG(sv_player);
661 PR_ExecuteProgram ((func_t)(SV_PlayerPhysicsQC - pr_functions), "");