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"};
32 static vec3_t forward, right, up;
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, MOVE_NOMONSTERS, sv_player);
80 // if looking at a wall, leave ideal the way is was
88 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
96 if (step > -ON_EPSILON && step < ON_EPSILON)
100 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
109 sv_player->v->idealpitch = 0;
115 sv_player->v->idealpitch = -dir * sv_idealpitchscale.value;
125 void SV_UserFriction (void)
127 float *vel, speed, newspeed, control, friction;
133 speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1]);
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;
143 trace = SV_Move (start, vec3_origin, vec3_origin, stop, MOVE_NOMONSTERS, sv_player);
145 if (trace.fraction == 1.0)
146 friction = sv_friction.value*sv_edgefriction.value;
148 friction = sv_friction.value;
151 control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
152 newspeed = speed - sv.frametime*control*friction;
159 vel[0] = vel[0] * newspeed;
160 vel[1] = vel[1] * newspeed;
161 vel[2] = vel[2] * newspeed;
169 void SV_Accelerate (void)
172 float addspeed, accelspeed, currentspeed;
174 currentspeed = DotProduct (velocity, wishdir);
175 addspeed = wishspeed - currentspeed;
178 accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
179 if (accelspeed > addspeed)
180 accelspeed = addspeed;
182 for (i=0 ; i<3 ; i++)
183 velocity[i] += accelspeed*wishdir[i];
186 void SV_AirAccelerate (vec3_t wishveloc)
189 float addspeed, wishspd, accelspeed, currentspeed;
191 wishspd = VectorNormalizeLength (wishveloc);
194 currentspeed = DotProduct (velocity, wishveloc);
195 addspeed = wishspd - currentspeed;
198 accelspeed = sv_accelerate.value*wishspeed * sv.frametime;
199 if (accelspeed > addspeed)
200 accelspeed = addspeed;
202 for (i=0 ; i<3 ; i++)
203 velocity[i] += accelspeed*wishveloc[i];
207 void DropPunchAngle (void)
212 len = VectorNormalizeLength (sv_player->v->punchangle);
214 len -= 10*sv.frametime;
217 VectorScale (sv_player->v->punchangle, len, sv_player->v->punchangle);
219 if ((val = GETEDICTFIELDVALUE(sv_player, eval_punchvector)))
221 len = VectorNormalizeLength (val->vector);
223 len -= 20*sv.frametime;
226 VectorScale (val->vector, len, val->vector);
235 void SV_FreeMove (void)
240 AngleVectors (sv_player->v->v_angle, forward, right, up);
242 for (i = 0; i < 3; i++)
243 velocity[i] = forward[i] * cmd.forwardmove + right[i] * cmd.sidemove;
245 velocity[2] += cmd.upmove;
247 wishspeed = VectorLength (velocity);
248 if (wishspeed > sv_maxspeed.value)
250 VectorScale (velocity, sv_maxspeed.value / wishspeed, velocity);
251 wishspeed = sv_maxspeed.value;
261 void SV_WaterMove (void)
265 float speed, newspeed, wishspeed, addspeed, accelspeed, temp;
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 = VectorLength(wishvel);
279 if (wishspeed > sv_maxspeed.value)
281 temp = sv_maxspeed.value/wishspeed;
282 VectorScale (wishvel, temp, wishvel);
283 wishspeed = sv_maxspeed.value;
288 speed = VectorLength (velocity);
291 newspeed = speed - sv.frametime * speed * sv_friction.value;
294 temp = newspeed/speed;
295 VectorScale (velocity, temp, velocity);
300 // water acceleration
304 addspeed = wishspeed - newspeed;
308 VectorNormalize (wishvel);
309 accelspeed = sv_accelerate.value * wishspeed * sv.frametime;
310 if (accelspeed > addspeed)
311 accelspeed = addspeed;
313 for (i=0 ; i<3 ; i++)
314 velocity[i] += accelspeed * wishvel[i];
317 void SV_WaterJump (void)
319 if (sv.time > sv_player->v->teleport_time || !sv_player->v->waterlevel)
321 sv_player->v->flags = (int)sv_player->v->flags & ~FL_WATERJUMP;
322 sv_player->v->teleport_time = 0;
324 sv_player->v->velocity[0] = sv_player->v->movedir[0];
325 sv_player->v->velocity[1] = sv_player->v->movedir[1];
335 void SV_AirMove (void)
339 float fmove, smove, temp;
341 // LordHavoc: correct quake movement speed bug when looking up/down
342 wishvel[0] = wishvel[2] = 0;
343 wishvel[1] = sv_player->v->angles[1];
344 AngleVectors (wishvel, forward, right, up);
346 fmove = cmd.forwardmove;
347 smove = cmd.sidemove;
349 // hack to not let you back into teleporter
350 if (sv.time < sv_player->v->teleport_time && fmove < 0)
353 for (i=0 ; i<3 ; i++)
354 wishvel[i] = forward[i]*fmove + right[i]*smove;
356 if ((int)sv_player->v->movetype != MOVETYPE_WALK)
357 wishvel[2] += cmd.upmove;
359 VectorCopy (wishvel, wishdir);
360 wishspeed = VectorNormalizeLength(wishdir);
361 if (wishspeed > sv_maxspeed.value)
363 temp = sv_maxspeed.value/wishspeed;
364 VectorScale (wishvel, temp, wishvel);
365 wishspeed = sv_maxspeed.value;
368 if (sv_player->v->movetype == MOVETYPE_NOCLIP)
371 VectorCopy (wishvel, velocity);
380 // not on ground, so little effect on velocity
381 SV_AirAccelerate (wishvel);
389 the move fields specify an intended velocity in pix/sec
390 the angle fields specify an exact angular motion in degrees
393 void SV_ClientThink (void)
397 if (sv_player->v->movetype == MOVETYPE_NONE)
400 onground = (int)sv_player->v->flags & FL_ONGROUND;
402 origin = sv_player->v->origin;
403 velocity = sv_player->v->velocity;
407 // if dead, behave differently
408 if (sv_player->v->health <= 0)
412 // show 1/3 the pitch angle and all the roll angle
413 cmd = host_client->cmd;
414 angles = sv_player->v->angles;
416 VectorAdd (sv_player->v->v_angle, sv_player->v->punchangle, v_angle);
417 angles[ROLL] = V_CalcRoll (sv_player->v->angles, sv_player->v->velocity)*4;
418 if (!sv_player->v->fixangle)
420 // LordHavoc: pitch was ugly to begin with... removed except in water
421 if (sv_player->v->waterlevel >= 2)
422 angles[PITCH] = -v_angle[PITCH]/3;
425 angles[YAW] = v_angle[YAW];
428 if ( (int)sv_player->v->flags & FL_WATERJUMP )
434 // Player is (somehow) outside of the map, or flying, or noclipping
435 if (SV_TestEntityPosition (sv_player)
436 || sv_player->v->movetype == MOVETYPE_FLY
437 || sv_player->v->movetype == MOVETYPE_NOCLIP)
444 if ((sv_player->v->waterlevel >= 2) && (sv_player->v->movetype != MOVETYPE_NOCLIP))
459 void SV_ReadClientMove (usercmd_t *move)
468 host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = sv.time - MSG_ReadFloat ();
469 host_client->num_pings++;
470 for (i=0, total = 0;i < NUM_PING_TIMES;i++)
471 total += host_client->ping_times[i];
472 // can be used for prediction
473 host_client->ping = total / NUM_PING_TIMES;
474 if ((val = GETEDICTFIELDVALUE(sv_player, eval_ping)))
475 val->_float = host_client->ping * 1000.0;
477 // read current angles
478 // dpprotocol version 2
479 for (i = 0;i < 3;i++)
480 angle[i] = MSG_ReadFloat ();
482 VectorCopy (angle, sv_player->v->v_angle);
485 move->forwardmove = MSG_ReadShort ();
486 move->sidemove = MSG_ReadShort ();
487 move->upmove = MSG_ReadShort ();
488 if ((val = GETEDICTFIELDVALUE(sv_player, eval_movement)))
490 val->vector[0] = move->forwardmove;
491 val->vector[1] = move->sidemove;
492 val->vector[2] = move->upmove;
496 bits = MSG_ReadByte ();
497 sv_player->v->button0 = bits & 1;
498 sv_player->v->button2 = (bits & 2)>>1;
499 // LordHavoc: added 6 new buttons
500 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button3))) val->_float = ((bits >> 2) & 1);
501 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button4))) val->_float = ((bits >> 3) & 1);
502 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button5))) val->_float = ((bits >> 4) & 1);
503 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button6))) val->_float = ((bits >> 5) & 1);
504 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button7))) val->_float = ((bits >> 6) & 1);
505 if ((val = GETEDICTFIELDVALUE(sv_player, eval_button8))) val->_float = ((bits >> 7) & 1);
509 sv_player->v->impulse = i;
517 extern void SV_SendServerinfo(client_t *client);
518 void SV_ReadClientMessage(void)
523 //MSG_BeginReading ();
527 if (!host_client->active)
529 // a command caused an error
530 SV_DropClient (false);
536 Con_Printf ("SV_ReadClientMessage: badread\n");
537 SV_DropClient (false);
541 cmd = MSG_ReadChar ();
551 Con_Printf ("SV_ReadClientMessage: unknown command char %i\n", cmd);
552 SV_DropClient (false);
559 s = MSG_ReadString ();
560 if (strncasecmp(s, "spawn", 5) == 0
561 || strncasecmp(s, "begin", 5) == 0
562 || strncasecmp(s, "prespawn", 8) == 0)
563 Cmd_ExecuteString (s, src_client);
564 else if (SV_ParseClientCommandQC)
566 G_INT(OFS_PARM0) = PR_SetString(s);
567 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
568 PR_ExecuteProgram ((func_t)(SV_ParseClientCommandQC - pr_functions), "");
570 else if (strncasecmp(s, "status", 6) == 0
571 || strncasecmp(s, "name", 4) == 0
572 || strncasecmp(s, "say", 3) == 0
573 || strncasecmp(s, "say_team", 8) == 0
574 || strncasecmp(s, "tell", 4) == 0
575 || strncasecmp(s, "color", 5) == 0
576 || strncasecmp(s, "kill", 4) == 0
577 || strncasecmp(s, "pause", 5) == 0
578 || strncasecmp(s, "kick", 4) == 0
579 || strncasecmp(s, "ping", 4) == 0
580 || strncasecmp(s, "ban", 3) == 0
581 || strncasecmp(s, "pmodel", 6) == 0
582 || (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))
583 || (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)))
584 Cmd_ExecuteString (s, src_client);
586 Con_Printf("%s tried to %s\n", host_client->name, s);
590 SV_DropClient (false); // client wants to disconnect
594 SV_ReadClientMove (&host_client->cmd);
597 case clc_ackentities:
598 EntityFrame_AckFrame(&host_client->entitydatabase, MSG_ReadLong());
609 void SV_RunClients (void)
613 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
615 if (!host_client->active)
618 sv_player = host_client->edict;
620 if (!host_client->spawned)
622 // clear client movement until a new packet is received
623 memset (&host_client->cmd, 0, sizeof(host_client->cmd));
629 // LordHavoc: QuakeC replacement for SV_ClientThink (player movement)
630 if (SV_PlayerPhysicsQC)
632 pr_global_struct->time = sv.time;
633 pr_global_struct->self = EDICT_TO_PROG(sv_player);
634 PR_ExecuteProgram ((func_t)(SV_PlayerPhysicsQC - pr_functions), "");
639 SV_CheckVelocity (sv_player);
641 // LordHavoc: a hack to ensure that the (rather silly) id1 quakec
642 // player_run/player_stand1 does not horribly malfunction if the
643 // velocity becomes a number that is both == 0 and != 0
644 // (sounds to me like NaN but to be absolutely safe...)
645 if (DotProduct(sv_player->v->velocity, sv_player->v->velocity) < 0.0001)
646 VectorClear(sv_player->v->velocity);