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 static usercmd_t usercmd;
27 extern cvar_t sv_autodemo_perclient;
28 extern cvar_t sv_rollangle;
29 extern cvar_t sv_rollspeed;
36 void SV_PreSpawn_f(cmd_state_t *cmd)
38 if (host_client->prespawned)
40 Con_Print("prespawn not valid -- already prespawned\n");
43 host_client->prespawned = true;
45 if (host_client->netconnection)
47 SZ_Write (&host_client->netconnection->message, sv.signon.data, sv.signon.cursize);
48 MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
49 MSG_WriteByte (&host_client->netconnection->message, 2);
50 host_client->sendsignon = 0; // enable unlimited sends again
53 // reset the name change timer because the client will send name soon
54 host_client->nametime = 0;
62 void SV_Spawn_f(cmd_state_t *cmd)
64 prvm_prog_t *prog = SVVM_prog;
67 int stats[MAX_CL_STATS];
69 if (!host_client->prespawned)
71 Con_Print("Spawn not valid -- not yet prespawned\n");
74 if (host_client->spawned)
76 Con_Print("Spawn not valid -- already spawned\n");
79 host_client->spawned = true;
81 // reset name change timer again because they might want to change name
82 // again in the first 5 seconds after connecting
83 host_client->nametime = 0;
85 // LadyHavoc: moved this above the QC calls at FrikaC's request
86 // LadyHavoc: commented this out
87 //if (host_client->netconnection)
88 // SZ_Clear (&host_client->netconnection->message);
90 // run the entrance script
93 // loaded games are fully initialized already
94 if (PRVM_serverfunction(RestoreGame))
96 Con_DPrint("Calling RestoreGame\n");
97 PRVM_serverglobalfloat(time) = sv.time;
98 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
99 prog->ExecuteProgram(prog, PRVM_serverfunction(RestoreGame), "QC function RestoreGame is missing");
104 //Con_Printf("SV_Spawn_f: host_client->edict->netname = %s, host_client->edict->netname = %s, host_client->name = %s\n", PRVM_GetString(PRVM_serveredictstring(host_client->edict, netname)), PRVM_GetString(PRVM_serveredictstring(host_client->edict, netname)), host_client->name);
106 // copy spawn parms out of the client_t
107 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
108 (&PRVM_serverglobalfloat(parm1))[i] = host_client->spawn_parms[i];
110 // call the spawn function
111 host_client->clientconnectcalled = true;
112 PRVM_serverglobalfloat(time) = sv.time;
113 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
114 prog->ExecuteProgram(prog, PRVM_serverfunction(ClientConnect), "QC function ClientConnect is missing");
116 if (cls.state == ca_dedicated)
117 Con_Printf("%s connected\n", host_client->name);
119 PRVM_serverglobalfloat(time) = sv.time;
120 prog->ExecuteProgram(prog, PRVM_serverfunction(PutClientInServer), "QC function PutClientInServer is missing");
123 if (!host_client->netconnection)
126 // send time of update
127 MSG_WriteByte (&host_client->netconnection->message, svc_time);
128 MSG_WriteFloat (&host_client->netconnection->message, sv.time);
130 // send all current names, colors, and frag counts
131 for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
135 MSG_WriteByte (&host_client->netconnection->message, svc_updatename);
136 MSG_WriteByte (&host_client->netconnection->message, i);
137 MSG_WriteString (&host_client->netconnection->message, client->name);
138 MSG_WriteByte (&host_client->netconnection->message, svc_updatefrags);
139 MSG_WriteByte (&host_client->netconnection->message, i);
140 MSG_WriteShort (&host_client->netconnection->message, client->frags);
141 MSG_WriteByte (&host_client->netconnection->message, svc_updatecolors);
142 MSG_WriteByte (&host_client->netconnection->message, i);
143 MSG_WriteByte (&host_client->netconnection->message, client->colors);
146 // send all current light styles
147 for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
149 if (sv.lightstyles[i][0])
151 MSG_WriteByte (&host_client->netconnection->message, svc_lightstyle);
152 MSG_WriteByte (&host_client->netconnection->message, (char)i);
153 MSG_WriteString (&host_client->netconnection->message, sv.lightstyles[i]);
158 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
159 MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALSECRETS);
160 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(total_secrets));
162 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
163 MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALMONSTERS);
164 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(total_monsters));
166 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
167 MSG_WriteByte (&host_client->netconnection->message, STAT_SECRETS);
168 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(found_secrets));
170 MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
171 MSG_WriteByte (&host_client->netconnection->message, STAT_MONSTERS);
172 MSG_WriteLong (&host_client->netconnection->message, (int)PRVM_serverglobalfloat(killed_monsters));
175 // Never send a roll angle, because savegames can catch the server
176 // in a state where it is expecting the client to correct the angle
177 // and it won't happen if the game was just loaded, so you wind up
178 // with a permanent head tilt
181 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
182 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, v_angle)[0], sv.protocol);
183 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, v_angle)[1], sv.protocol);
184 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
188 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
189 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, angles)[0], sv.protocol);
190 MSG_WriteAngle (&host_client->netconnection->message, PRVM_serveredictvector(host_client->edict, angles)[1], sv.protocol);
191 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
194 SV_WriteClientdataToMessage (host_client, host_client->edict, &host_client->netconnection->message, stats);
196 MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
197 MSG_WriteByte (&host_client->netconnection->message, 3);
205 void SV_Begin_f(cmd_state_t *cmd)
207 if (!host_client->spawned)
209 Con_Print("Begin not valid -- not yet spawned\n");
212 if (host_client->begun)
214 Con_Print("Begin not valid -- already begun\n");
217 host_client->begun = true;
219 // LadyHavoc: note: this code also exists in SV_DropClient
223 for (i = 0;i < svs.maxclients;i++)
224 if (svs.clients[i].active && !svs.clients[i].spawned)
226 if (i == svs.maxclients)
228 Con_Printf("Loaded game, everyone rejoined - unpausing\n");
229 sv.paused = sv.loadgame = false; // we're basically done with loading now
239 #define MAX_FORWARD 6
240 void SV_SetIdealPitch (void)
242 prvm_prog_t *prog = SVVM_prog;
243 float angleval, sinval, cosval, step, dir;
246 float z[MAX_FORWARD];
250 if (!((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND))
253 angleval = PRVM_serveredictvector(host_client->edict, angles)[YAW] * M_PI*2 / 360;
254 sinval = sin(angleval);
255 cosval = cos(angleval);
257 for (i=0 ; i<MAX_FORWARD ; i++)
259 top[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + cosval*(i+3)*12;
260 top[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + sinval*(i+3)*12;
261 top[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, view_ofs)[2];
265 bottom[2] = top[2] - 160;
267 tr = SV_TraceLine(top, bottom, MOVE_NOMONSTERS, host_client->edict, SUPERCONTENTS_SOLID, 0, 0, collision_extendmovelength.value);
268 // if looking at a wall, leave ideal the way is was
273 if (tr.fraction == 1)
276 z[i] = top[2] + tr.fraction*(bottom[2]-top[2]);
281 for (j=1 ; j<i ; j++)
283 step = z[j] - z[j-1];
284 if (step > -ON_EPSILON && step < ON_EPSILON)
288 if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) )
297 PRVM_serveredictfloat(host_client->edict, idealpitch) = 0;
303 PRVM_serveredictfloat(host_client->edict, idealpitch) = -dir * sv_idealpitchscale.value;
306 static vec3_t wishdir, forward, right, up;
307 static float wishspeed;
309 static qbool onground;
317 static void SV_UserFriction (void)
319 prvm_prog_t *prog = SVVM_prog;
320 float speed, newspeed, control, friction;
324 speed = sqrt(PRVM_serveredictvector(host_client->edict, velocity)[0]*PRVM_serveredictvector(host_client->edict, velocity)[0]+PRVM_serveredictvector(host_client->edict, velocity)[1]*PRVM_serveredictvector(host_client->edict, velocity)[1]);
328 // if the leading edge is over a dropoff, increase friction
329 start[0] = stop[0] = PRVM_serveredictvector(host_client->edict, origin)[0] + PRVM_serveredictvector(host_client->edict, velocity)[0]/speed*16;
330 start[1] = stop[1] = PRVM_serveredictvector(host_client->edict, origin)[1] + PRVM_serveredictvector(host_client->edict, velocity)[1]/speed*16;
331 start[2] = PRVM_serveredictvector(host_client->edict, origin)[2] + PRVM_serveredictvector(host_client->edict, mins)[2];
332 stop[2] = start[2] - 34;
334 trace = SV_TraceLine(start, stop, MOVE_NOMONSTERS, host_client->edict, SV_GenericHitSuperContentsMask(host_client->edict), 0, 0, collision_extendmovelength.value);
336 if (trace.fraction == 1.0)
337 friction = sv_friction.value*sv_edgefriction.value;
339 friction = sv_friction.value;
342 control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed;
343 newspeed = speed - sv.frametime*control*friction;
350 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), newspeed, PRVM_serveredictvector(host_client->edict, velocity));
358 static void SV_Accelerate (void)
360 prvm_prog_t *prog = SVVM_prog;
362 float addspeed, accelspeed, currentspeed;
364 currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishdir);
365 addspeed = wishspeed - currentspeed;
368 accelspeed = sv_accelerate.value*sv.frametime*wishspeed;
369 if (accelspeed > addspeed)
370 accelspeed = addspeed;
372 for (i=0 ; i<3 ; i++)
373 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishdir[i];
376 extern cvar_t sv_gameplayfix_q2airaccelerate;
377 static void SV_AirAccelerate (vec3_t wishveloc)
379 prvm_prog_t *prog = SVVM_prog;
381 float addspeed, wishspd, accelspeed, currentspeed;
383 wishspd = VectorNormalizeLength (wishveloc);
384 if (wishspd > sv_maxairspeed.value)
385 wishspd = sv_maxairspeed.value;
386 currentspeed = DotProduct (PRVM_serveredictvector(host_client->edict, velocity), wishveloc);
387 addspeed = wishspd - currentspeed;
390 accelspeed = (sv_airaccelerate.value < 0 ? sv_accelerate.value : sv_airaccelerate.value)*(sv_gameplayfix_q2airaccelerate.integer ? wishspd : wishspeed) * sv.frametime;
391 if (accelspeed > addspeed)
392 accelspeed = addspeed;
394 for (i=0 ; i<3 ; i++)
395 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed*wishveloc[i];
399 static void DropPunchAngle (void)
401 prvm_prog_t *prog = SVVM_prog;
403 vec3_t punchangle, punchvector;
405 VectorCopy(PRVM_serveredictvector(host_client->edict, punchangle), punchangle);
406 VectorCopy(PRVM_serveredictvector(host_client->edict, punchvector), punchvector);
408 len = VectorNormalizeLength(punchangle);
411 len -= 10*sv.frametime;
414 VectorScale(punchangle, len, punchangle);
417 len = VectorNormalizeLength(punchvector);
420 len -= 20*sv.frametime;
423 VectorScale(punchvector, len, punchvector);
426 VectorCopy(punchangle, PRVM_serveredictvector(host_client->edict, punchangle));
427 VectorCopy(punchvector, PRVM_serveredictvector(host_client->edict, punchvector));
436 static void SV_WaterMove (void)
438 prvm_prog_t *prog = SVVM_prog;
440 vec3_t wishvel, v_angle;
441 vec_t speed, newspeed, fwishspeed, addspeed, accelspeed, temp;
444 VectorCopy(PRVM_serveredictvector(host_client->edict, v_angle), v_angle);
445 AngleVectors(v_angle, forward, right, up);
447 for (i=0 ; i<3 ; i++)
448 wishvel[i] = forward[i]*usercmd.forwardmove + right[i]*usercmd.sidemove;
450 if (!usercmd.forwardmove && !usercmd.sidemove && !usercmd.upmove)
451 wishvel[2] -= 60; // drift towards bottom
453 wishvel[2] += usercmd.upmove;
455 fwishspeed = VectorLength(wishvel);
456 if (fwishspeed > sv_maxspeed.value)
458 temp = sv_maxspeed.value/fwishspeed;
459 VectorScale (wishvel, temp, wishvel);
460 fwishspeed = sv_maxspeed.value;
465 speed = VectorLength(PRVM_serveredictvector(host_client->edict, velocity));
468 newspeed = speed - sv.frametime * speed * (sv_waterfriction.value < 0 ? sv_friction.value : sv_waterfriction.value);
471 temp = newspeed/speed;
472 VectorScale(PRVM_serveredictvector(host_client->edict, velocity), temp, PRVM_serveredictvector(host_client->edict, velocity));
477 // water acceleration
481 addspeed = fwishspeed - newspeed;
485 VectorNormalize (wishvel);
486 accelspeed = (sv_wateraccelerate.value < 0 ? sv_accelerate.value : sv_wateraccelerate.value) * fwishspeed * sv.frametime;
487 if (accelspeed > addspeed)
488 accelspeed = addspeed;
490 for (i=0 ; i<3 ; i++)
491 PRVM_serveredictvector(host_client->edict, velocity)[i] += accelspeed * wishvel[i];
494 static void SV_WaterJump (void)
496 prvm_prog_t *prog = SVVM_prog;
497 if (sv.time > PRVM_serveredictfloat(host_client->edict, teleport_time) || !PRVM_serveredictfloat(host_client->edict, waterlevel))
499 PRVM_serveredictfloat(host_client->edict, flags) = (int)PRVM_serveredictfloat(host_client->edict, flags) & ~FL_WATERJUMP;
500 PRVM_serveredictfloat(host_client->edict, teleport_time) = 0;
502 PRVM_serveredictvector(host_client->edict, velocity)[0] = PRVM_serveredictvector(host_client->edict, movedir)[0];
503 PRVM_serveredictvector(host_client->edict, velocity)[1] = PRVM_serveredictvector(host_client->edict, movedir)[1];
513 static void SV_AirMove (void)
515 prvm_prog_t *prog = SVVM_prog;
518 float fmove, smove, temp;
520 // LadyHavoc: correct quake movement speed bug when looking up/down
521 wishvel[0] = wishvel[2] = 0;
522 wishvel[1] = PRVM_serveredictvector(host_client->edict, angles)[1];
523 AngleVectors (wishvel, forward, right, up);
525 fmove = usercmd.forwardmove;
526 smove = usercmd.sidemove;
528 // hack to not let you back into teleporter
529 if (sv.time < PRVM_serveredictfloat(host_client->edict, teleport_time) && fmove < 0)
532 for (i=0 ; i<3 ; i++)
533 wishvel[i] = forward[i]*fmove + right[i]*smove;
535 if ((int)PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_WALK)
536 wishvel[2] += usercmd.upmove;
538 VectorCopy (wishvel, wishdir);
539 wishspeed = VectorNormalizeLength(wishdir);
540 if (wishspeed > sv_maxspeed.value)
542 temp = sv_maxspeed.value/wishspeed;
543 VectorScale (wishvel, temp, wishvel);
544 wishspeed = sv_maxspeed.value;
547 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP)
550 VectorCopy (wishvel, PRVM_serveredictvector(host_client->edict, velocity));
559 // not on ground, so little effect on velocity
560 SV_AirAccelerate (wishvel);
568 the move fields specify an intended velocity in pix/sec
569 the angle fields specify an exact angular motion in degrees
572 void SV_ClientThink (void)
574 prvm_prog_t *prog = SVVM_prog;
575 vec3_t v_angle, angles, velocity;
577 //Con_Printf("clientthink for %ims\n", (int) (sv.frametime * 1000));
579 SV_ApplyClientMove();
580 // make sure the velocity is sane (not a NaN)
581 SV_CheckVelocity(host_client->edict);
583 // LadyHavoc: QuakeC replacement for SV_ClientThink (player movement)
584 if (PRVM_serverfunction(SV_PlayerPhysics) && sv_playerphysicsqc.integer)
586 PRVM_serverglobalfloat(time) = sv.time;
587 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
588 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PlayerPhysics), "QC function SV_PlayerPhysics is missing");
589 SV_CheckVelocity(host_client->edict);
593 if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NONE)
596 onground = ((int)PRVM_serveredictfloat(host_client->edict, flags) & FL_ONGROUND) != 0;
600 // if dead, behave differently
601 if (PRVM_serveredictfloat(host_client->edict, health) <= 0)
604 usercmd = host_client->cmd;
607 // show 1/3 the pitch angle and all the roll angle
608 VectorAdd (PRVM_serveredictvector(host_client->edict, v_angle), PRVM_serveredictvector(host_client->edict, punchangle), v_angle);
609 VectorCopy(PRVM_serveredictvector(host_client->edict, angles), angles);
610 VectorCopy(PRVM_serveredictvector(host_client->edict, velocity), velocity);
611 PRVM_serveredictvector(host_client->edict, angles)[ROLL] = Com_CalcRoll (angles, velocity, sv_rollangle.value, sv_rollspeed.value)*4;
612 if (!PRVM_serveredictfloat(host_client->edict, fixangle))
614 PRVM_serveredictvector(host_client->edict, angles)[PITCH] = -v_angle[PITCH]/3;
615 PRVM_serveredictvector(host_client->edict, angles)[YAW] = v_angle[YAW];
618 if ( (int)PRVM_serveredictfloat(host_client->edict, flags) & FL_WATERJUMP )
621 SV_CheckVelocity(host_client->edict);
626 // Player is (somehow) outside of the map, or flying, or noclipping
627 if (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP && (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict)))
628 //if (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_NOCLIP || PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_FLY || SV_TestEntityPosition (host_client->edict))
636 if ((PRVM_serveredictfloat(host_client->edict, waterlevel) >= 2) && (PRVM_serveredictfloat(host_client->edict, movetype) != MOVETYPE_NOCLIP))
639 SV_CheckVelocity(host_client->edict);
644 SV_CheckVelocity(host_client->edict);
652 int sv_numreadmoves = 0;
653 usercmd_t sv_readmoves[CL_MAX_USERCMDS];
654 static void SV_ReadClientMove (void)
656 prvm_prog_t *prog = SVVM_prog;
659 usercmd_t *move = &newmove;
661 memset(move, 0, sizeof(*move));
663 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
666 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_NEHAHRABJP && sv.protocol != PROTOCOL_NEHAHRABJP2 && sv.protocol != PROTOCOL_NEHAHRABJP3 && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5 && sv.protocol != PROTOCOL_DARKPLACES6)
667 move->sequence = MSG_ReadLong(&sv_message);
668 move->time = move->clienttime = MSG_ReadFloat(&sv_message);
669 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
670 move->receivetime = (float)sv.time;
673 Con_Printf("%s move%i #%u %ims (%ims) %i %i '%i %i %i' '%i %i %i'\n", move->time > move->receivetime ? "^3read future" : "^4read normal", sv_numreadmoves + 1, move->sequence, (int)floor((move->time - host_client->cmd.time) * 1000.0 + 0.5), (int)floor(move->time * 1000.0 + 0.5), move->impulse, move->buttons, (int)move->viewangles[0], (int)move->viewangles[1], (int)move->viewangles[2], (int)move->forwardmove, (int)move->sidemove, (int)move->upmove);
675 // limit reported time to current time
676 // (incase the client is trying to cheat)
677 move->time = min(move->time, move->receivetime + sv.frametime);
679 // read current angles
680 for (i = 0;i < 3;i++)
682 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3)
683 move->viewangles[i] = MSG_ReadAngle8i(&sv_message);
684 else if (sv.protocol == PROTOCOL_DARKPLACES1)
685 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
686 else if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
687 move->viewangles[i] = MSG_ReadAngle32f(&sv_message);
689 move->viewangles[i] = MSG_ReadAngle16i(&sv_message);
691 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
694 move->forwardmove = MSG_ReadCoord16i(&sv_message);
695 move->sidemove = MSG_ReadCoord16i(&sv_message);
696 move->upmove = MSG_ReadCoord16i(&sv_message);
697 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
700 // be sure to bitwise OR them into the move->buttons because we want to
701 // accumulate button presses from multiple packets per actual move
702 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3 || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
703 move->buttons = MSG_ReadByte(&sv_message);
705 move->buttons = MSG_ReadLong(&sv_message);
706 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
709 move->impulse = MSG_ReadByte(&sv_message);
710 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
712 // PRYDON_CLIENTCURSOR
713 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_NEHAHRABJP && sv.protocol != PROTOCOL_NEHAHRABJP2 && sv.protocol != PROTOCOL_NEHAHRABJP3 && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5)
716 move->cursor_screen[0] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
717 move->cursor_screen[1] = MSG_ReadShort(&sv_message) * (1.0f / 32767.0f);
718 move->cursor_start[0] = MSG_ReadFloat(&sv_message);
719 move->cursor_start[1] = MSG_ReadFloat(&sv_message);
720 move->cursor_start[2] = MSG_ReadFloat(&sv_message);
721 move->cursor_impact[0] = MSG_ReadFloat(&sv_message);
722 move->cursor_impact[1] = MSG_ReadFloat(&sv_message);
723 move->cursor_impact[2] = MSG_ReadFloat(&sv_message);
724 move->cursor_entitynumber = (unsigned short)MSG_ReadShort(&sv_message);
725 if (move->cursor_entitynumber >= prog->max_edicts)
727 Con_DPrintf("SV_ReadClientMessage: client send bad cursor_entitynumber\n");
728 move->cursor_entitynumber = 0;
730 // as requested by FrikaC, cursor_trace_ent is reset to world if the
731 // entity is free at time of receipt
732 if (PRVM_EDICT_NUM(move->cursor_entitynumber)->priv.server->free)
733 move->cursor_entitynumber = 0;
734 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
737 // if the previous move has not been applied yet, we need to accumulate
738 // the impulse/buttons from it
739 if (!host_client->cmd.applied)
742 move->impulse = host_client->cmd.impulse;
743 move->buttons |= host_client->cmd.buttons;
746 // now store this move for later execution
747 // (we have to buffer the moves because of old ones being repeated)
748 if (sv_numreadmoves < CL_MAX_USERCMDS)
749 sv_readmoves[sv_numreadmoves++] = *move;
751 // movement packet loss tracking
754 if(move->sequence > host_client->movement_highestsequence_seen)
756 if(host_client->movement_highestsequence_seen)
758 // mark moves in between as lost
759 unsigned int delta = move->sequence - host_client->movement_highestsequence_seen - 1;
760 if(delta < NETGRAPH_PACKETS)
763 for(u = 0; u < delta; ++u)
764 host_client->movement_count[(host_client->movement_highestsequence_seen + 1 + u) % NETGRAPH_PACKETS] = -1;
767 memset(host_client->movement_count, -1, sizeof(host_client->movement_count));
769 // mark THIS move as seen for the first time
770 host_client->movement_count[move->sequence % NETGRAPH_PACKETS] = 1;
771 // update highest sequence seen
772 host_client->movement_highestsequence_seen = move->sequence;
775 if(host_client->movement_count[move->sequence % NETGRAPH_PACKETS] >= 0)
776 ++host_client->movement_count[move->sequence % NETGRAPH_PACKETS];
780 host_client->movement_highestsequence_seen = 0;
781 memset(host_client->movement_count, 0, sizeof(host_client->movement_count));
785 static void SV_ExecuteClientMoves(void)
787 prvm_prog_t *prog = SVVM_prog;
791 double oldframetime2;
792 #ifdef NUM_PING_TIMES
795 if (sv_numreadmoves < 1)
797 // only start accepting input once the player is spawned
798 if (!host_client->begun)
801 Con_Printf("SV_ExecuteClientMoves: read %i moves at sv.time %f\n", sv_numreadmoves, (float)sv.time);
803 // disable clientside movement prediction in some cases
804 if (ceil(max(sv_readmoves[sv_numreadmoves-1].receivetime - sv_readmoves[sv_numreadmoves-1].time, 0) * 1000.0) < sv_clmovement_minping.integer)
805 host_client->clmovement_disabletimeout = host.realtime + sv_clmovement_minping_disabletime.value / 1000.0;
806 // several conditions govern whether clientside movement prediction is allowed
807 if (sv_readmoves[sv_numreadmoves-1].sequence && sv_clmovement_enable.integer && sv_clmovement_inputtimeout.value > 0 && host_client->clmovement_disabletimeout <= host.realtime && (PRVM_serveredictfloat(host_client->edict, disableclientprediction) == -1 || (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_WALK && (!PRVM_serveredictfloat(host_client->edict, disableclientprediction)))))
809 // process the moves in order and ignore old ones
810 // but always trust the latest move
811 // (this deals with bogus initial move sequences after level change,
812 // where the client will eventually catch up with the level change
813 // and reset its move sequence)
814 for (moveindex = 0;moveindex < sv_numreadmoves;moveindex++)
816 usercmd_t *move = sv_readmoves + moveindex;
817 if (host_client->movesequence < move->sequence || moveindex == sv_numreadmoves - 1)
820 Con_Printf("%smove #%u %ims (%ims) %i %i '%i %i %i' '%i %i %i'\n", (move->time - host_client->cmd.time) > sv.frametime * 1.01 ? "^1" : "^2", move->sequence, (int)floor((move->time - host_client->cmd.time) * 1000.0 + 0.5), (int)floor(move->time * 1000.0 + 0.5), move->impulse, move->buttons, (int)move->viewangles[0], (int)move->viewangles[1], (int)move->viewangles[2], (int)move->forwardmove, (int)move->sidemove, (int)move->upmove);
822 // this is a new move
823 move->time = bound(sv.time - 1, move->time, sv.time); // prevent slowhack/speedhack combos
824 move->time = max(move->time, host_client->cmd.time); // prevent backstepping of time
825 moveframetime = bound(0, move->time - host_client->cmd.time, min(0.1, sv_clmovement_inputtimeout.value));
827 // discard (treat like lost) moves with too low distance from
828 // the previous one to prevent hacks using float inaccuracy
829 // clients will see this as packet loss in the netgraph
830 // this should also apply if a move cannot get
831 // executed because it came too late and
832 // already was performed serverside
833 if(moveframetime < 0.0005)
835 // count the move as LOST if we don't
836 // execute it but it has higher
838 if(host_client->movesequence)
839 if(move->sequence > host_client->movesequence)
840 host_client->movement_count[(move->sequence) % NETGRAPH_PACKETS] = -1;
844 //Con_Printf("movesequence = %i (%i lost), moveframetime = %f\n", move->sequence, move->sequence ? move->sequence - host_client->movesequence - 1 : 0, moveframetime);
845 host_client->cmd = *move;
846 host_client->movesequence = move->sequence;
848 // if using prediction, we need to perform moves when packets are
849 // received, even if multiple occur in one frame
850 // (they can't go beyond the current time so there is no cheat issue
851 // with this approach, and if they don't send input for a while they
852 // start moving anyway, so the longest 'lagaport' possible is
853 // determined by the sv_clmovement_inputtimeout cvar)
854 if (moveframetime <= 0)
856 oldframetime = PRVM_serverglobalfloat(frametime);
857 oldframetime2 = sv.frametime;
858 // update ping time for qc to see while executing this move
859 host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
860 // the server and qc frametime values must be changed temporarily
861 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime;
862 // if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
863 if (sv.frametime > 0.05)
865 PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime * 0.5f;
866 SV_Physics_ClientMove();
868 SV_Physics_ClientMove();
869 sv.frametime = oldframetime2;
870 PRVM_serverglobalfloat(frametime) = oldframetime;
871 host_client->clmovement_inputtimeout = sv_clmovement_inputtimeout.value;
877 // try to gather button bits from old moves, but only if their time is
878 // advancing (ones with the same timestamp can't be trusted)
879 for (moveindex = 0;moveindex < sv_numreadmoves-1;moveindex++)
881 usercmd_t *move = sv_readmoves + moveindex;
882 if (host_client->cmd.time < move->time)
884 sv_readmoves[sv_numreadmoves-1].buttons |= move->buttons;
886 sv_readmoves[sv_numreadmoves-1].impulse = move->impulse;
889 // now copy the new move
890 host_client->cmd = sv_readmoves[sv_numreadmoves-1];
891 host_client->cmd.time = max(host_client->cmd.time, sv.time);
892 // physics will run up to sv.time, so allow no predicted moves
893 // before that otherwise, there is a speedhack by turning
894 // prediction on and off repeatedly on client side because the
895 // engine would run BOTH client and server physics for the same
897 host_client->movesequence = 0;
898 // make sure that normal physics takes over immediately
899 host_client->clmovement_inputtimeout = 0;
902 // calculate average ping time
903 host_client->ping = host_client->cmd.receivetime - host_client->cmd.clienttime;
904 #ifdef NUM_PING_TIMES
905 host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = host_client->cmd.receivetime - host_client->cmd.clienttime;
906 host_client->num_pings++;
907 for (i=0, total = 0;i < NUM_PING_TIMES;i++)
908 total += host_client->ping_times[i];
909 host_client->ping = total / NUM_PING_TIMES;
913 void SV_ApplyClientMove (void)
915 prvm_prog_t *prog = SVVM_prog;
916 usercmd_t *move = &host_client->cmd;
917 int j, movementloss, packetloss;
919 if (!move->receivetime)
922 // note: a move can be applied multiple times if the client packets are
923 // not coming as often as the physics is executed, and the move must be
924 // applied before running qc each time because the id1 qc had a bug where
925 // it clears self.button2 in PlayerJump, causing pogostick behavior if
926 // moves are not applied every time before calling qc
927 move->applied = true;
929 // set the edict fields
930 PRVM_serveredictfloat(host_client->edict, button0) = move->buttons & 1;
931 PRVM_serveredictfloat(host_client->edict, button2) = (move->buttons & 2)>>1;
933 PRVM_serveredictfloat(host_client->edict, impulse) = move->impulse;
934 // only send the impulse to qc once
937 movementloss = packetloss = 0;
938 if(host_client->netconnection)
940 for (j = 0;j < NETGRAPH_PACKETS;j++)
941 if (host_client->netconnection->incoming_netgraph[j].unreliablebytes == NETGRAPH_LOSTPACKET)
943 for (j = 0;j < NETGRAPH_PACKETS;j++)
944 if (host_client->movement_count[j] < 0)
948 VectorCopy(move->viewangles, PRVM_serveredictvector(host_client->edict, v_angle));
949 PRVM_serveredictfloat(host_client->edict, button3) = ((move->buttons >> 2) & 1);
950 PRVM_serveredictfloat(host_client->edict, button4) = ((move->buttons >> 3) & 1);
951 PRVM_serveredictfloat(host_client->edict, button5) = ((move->buttons >> 4) & 1);
952 PRVM_serveredictfloat(host_client->edict, button6) = ((move->buttons >> 5) & 1);
953 PRVM_serveredictfloat(host_client->edict, button7) = ((move->buttons >> 6) & 1);
954 PRVM_serveredictfloat(host_client->edict, button8) = ((move->buttons >> 7) & 1);
955 PRVM_serveredictfloat(host_client->edict, button9) = ((move->buttons >> 11) & 1);
956 PRVM_serveredictfloat(host_client->edict, button10) = ((move->buttons >> 12) & 1);
957 PRVM_serveredictfloat(host_client->edict, button11) = ((move->buttons >> 13) & 1);
958 PRVM_serveredictfloat(host_client->edict, button12) = ((move->buttons >> 14) & 1);
959 PRVM_serveredictfloat(host_client->edict, button13) = ((move->buttons >> 15) & 1);
960 PRVM_serveredictfloat(host_client->edict, button14) = ((move->buttons >> 16) & 1);
961 PRVM_serveredictfloat(host_client->edict, button15) = ((move->buttons >> 17) & 1);
962 PRVM_serveredictfloat(host_client->edict, button16) = ((move->buttons >> 18) & 1);
963 PRVM_serveredictfloat(host_client->edict, buttonuse) = ((move->buttons >> 8) & 1);
964 PRVM_serveredictfloat(host_client->edict, buttonchat) = ((move->buttons >> 9) & 1);
965 PRVM_serveredictfloat(host_client->edict, cursor_active) = ((move->buttons >> 10) & 1);
966 VectorSet(PRVM_serveredictvector(host_client->edict, movement), move->forwardmove, move->sidemove, move->upmove);
967 VectorCopy(move->cursor_screen, PRVM_serveredictvector(host_client->edict, cursor_screen));
968 VectorCopy(move->cursor_start, PRVM_serveredictvector(host_client->edict, cursor_trace_start));
969 VectorCopy(move->cursor_impact, PRVM_serveredictvector(host_client->edict, cursor_trace_endpos));
970 PRVM_serveredictedict(host_client->edict, cursor_trace_ent) = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
971 PRVM_serveredictfloat(host_client->edict, ping) = host_client->ping * 1000.0;
972 PRVM_serveredictfloat(host_client->edict, ping_packetloss) = packetloss / (float) NETGRAPH_PACKETS;
973 PRVM_serveredictfloat(host_client->edict, ping_movementloss) = movementloss / (float) NETGRAPH_PACKETS;
976 static qbool SV_FrameLost(int framenum)
978 if (host_client->entitydatabase5)
980 if (framenum <= host_client->entitydatabase5->latestframenum)
982 EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
983 EntityFrameCSQC_LostFrame(host_client, framenum);
990 static void SV_FrameAck(int framenum)
992 if (host_client->entitydatabase)
993 EntityFrame_AckFrame(host_client->entitydatabase, framenum);
994 else if (host_client->entitydatabase4)
995 EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
996 else if (host_client->entitydatabase5)
997 EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);
1002 SV_ReadClientMessage
1005 void SV_ReadClientMessage(void)
1007 prvm_prog_t *prog = SVVM_prog;
1008 int netcmd, num, start;
1011 if(sv_autodemo_perclient.integer >= 2)
1012 SV_WriteDemoMessage(host_client, &(host_client->netconnection->message), true);
1014 //MSG_BeginReading ();
1015 sv_numreadmoves = 0;
1019 if (!host_client->active)
1021 // a command caused an error
1022 SV_DropClient (false);
1026 if (sv_message.badread)
1028 Con_Print("SV_ReadClientMessage: badread\n");
1029 SV_DropClient (false);
1033 netcmd = MSG_ReadByte(&sv_message);
1037 // apply the moves that were read this frame
1038 SV_ExecuteClientMoves();
1045 Con_Printf("SV_ReadClientMessage: unknown command char %i (at offset 0x%x)\n", netcmd, sv_message.readcount);
1046 if (developer_networking.integer)
1047 Com_HexDumpToConsole(sv_message.data, sv_message.cursize);
1048 SV_DropClient (false);
1055 // allow reliable messages now as the client is done with initial loading
1056 if (host_client->sendsignon == 2)
1057 host_client->sendsignon = 0;
1058 s = MSG_ReadString(&sv_message, sv_readstring, sizeof(sv_readstring));
1060 for(p = s; *p; ++p) switch(*p)
1069 goto clc_stringcmd_invalid; // newline seen, THEN something else -> possible exploit
1074 if (strncasecmp(s, "spawn", 5) == 0
1075 || strncasecmp(s, "begin", 5) == 0
1076 || strncasecmp(s, "prespawn", 8) == 0)
1077 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
1078 else if (PRVM_serverfunction(SV_ParseClientCommand))
1080 int restorevm_tempstringsbuf_cursize;
1081 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
1082 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, s);
1083 PRVM_serverglobalfloat(time) = sv.time;
1084 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
1085 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_ParseClientCommand), "QC function SV_ParseClientCommand is missing");
1086 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1089 Cmd_ExecuteString (&cmd_serverfromclient, s, src_client, true);
1092 clc_stringcmd_invalid:
1093 Con_Printf("Received invalid stringcmd from %s\n", host_client->name);
1094 if(developer.integer > 0)
1095 Com_HexDumpToConsole((unsigned char *) s, (int)strlen(s));
1098 case clc_disconnect:
1099 SV_DropClient (false); // client wants to disconnect
1103 SV_ReadClientMove();
1106 case clc_ackdownloaddata:
1107 start = MSG_ReadLong(&sv_message);
1108 num = MSG_ReadShort(&sv_message);
1109 if (host_client->download_file && host_client->download_started)
1111 if (host_client->download_expectedposition == start)
1113 int size = (int)FS_FileSize(host_client->download_file);
1114 // a data block was successfully received by the client,
1115 // update the expected position on the next data block
1116 host_client->download_expectedposition = start + num;
1117 // if this was the last data block of the file, it's done
1118 if (host_client->download_expectedposition >= FS_FileSize(host_client->download_file))
1120 // tell the client that the download finished
1121 // we need to calculate the crc now
1123 // note: at this point the OS probably has the file
1124 // entirely in memory, so this is a faster operation
1125 // now than it was when the download started.
1127 // it is also preferable to do this at the end of the
1128 // download rather than the start because it reduces
1129 // potential for Denial Of Service attacks against the
1132 unsigned char *temp;
1133 FS_Seek(host_client->download_file, 0, SEEK_SET);
1134 temp = (unsigned char *) Mem_Alloc(tempmempool, size);
1135 FS_Read(host_client->download_file, temp, size);
1136 crc = CRC_Block(temp, size);
1138 // calculated crc, send the file info to the client
1139 // (so that it can verify the data)
1140 SV_ClientCommands("\ncl_downloadfinished %i %i %s\n", size, crc, host_client->download_name);
1141 Con_DPrintf("Download of %s by %s has finished\n", host_client->download_name, host_client->name);
1142 FS_Close(host_client->download_file);
1143 host_client->download_file = NULL;
1144 host_client->download_name[0] = 0;
1145 host_client->download_expectedposition = 0;
1146 host_client->download_started = false;
1151 // a data block was lost, reset to the expected position
1152 // and resume sending from there
1153 FS_Seek(host_client->download_file, host_client->download_expectedposition, SEEK_SET);
1159 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
1160 num = MSG_ReadLong(&sv_message);
1161 if (sv_message.badread) Con_Printf("SV_ReadClientMessage: badread at %s:%i\n", __FILE__, __LINE__);
1162 if (developer_networkentities.integer >= 10)
1163 Con_Printf("recv clc_ackframe %i\n", num);
1164 // if the client hasn't progressed through signons yet,
1165 // ignore any clc_ackframes we get (they're probably from the
1167 if (host_client->begun && host_client->latestframenum < num)
1170 for (i = host_client->latestframenum + 1;i < num;i++)
1171 if (!SV_FrameLost(i))
1174 host_client->latestframenum = num;