+ // several conditions govern whether clientside movement prediction is allowed
+ if (sv_readmoves[sv_numreadmoves-1].sequence && sv_clmovement_enable.integer && sv_clmovement_inputtimeout.value > 0 && host_client->clmovement_disabletimeout <= realtime && host_client->edict->fields.server->movetype == MOVETYPE_WALK && (!(val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.disableclientprediction)) || !val->_float))
+ {
+ // process the moves in order and ignore old ones
+ // but always trust the latest move
+ // (this deals with bogus initial move sequences after level change,
+ // where the client will eventually catch up with the level change
+ // and reset its move sequence)
+ for (moveindex = 0;moveindex < sv_numreadmoves;moveindex++)
+ {
+ usercmd_t *move = sv_readmoves + moveindex;
+ if (host_client->movesequence < move->sequence || moveindex == sv_numreadmoves - 1)
+ {
+#if DEBUGMOVES
+ Con_Printf("%smove #%i %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);
+#endif
+ // this is a new move
+ move->time = bound(sv.time - 1, move->time, sv.time); // prevent slowhack/speedhack combos
+ move->time = max(move->time, host_client->cmd.time); // prevent backstepping of time
+ moveframetime = bound(0, move->time - host_client->cmd.time, min(0.1, sv_clmovement_inputtimeout.value));
+
+ // discard (treat like lost) moves with too low distance from
+ // the previous one to prevent hacks using float inaccuracy
+ // clients will see this as packet loss in the netgraph
+ // this should also apply if a move cannot get
+ // executed because it came too late and
+ // already was performed serverside
+ if(moveframetime < 0.0005)
+ {
+ // count the move as LOST if we don't
+ // execute it but it has higher
+ // sequence count
+ if(host_client->movesequence)
+ if(move->sequence > host_client->movesequence)
+ host_client->movement_count[(move->sequence) % NETGRAPH_PACKETS] = -1;
+ continue;
+ }
+
+ //Con_Printf("movesequence = %i (%i lost), moveframetime = %f\n", move->sequence, move->sequence ? move->sequence - host_client->movesequence - 1 : 0, moveframetime);
+ host_client->cmd = *move;
+ host_client->movesequence = move->sequence;
+
+ // if using prediction, we need to perform moves when packets are
+ // received, even if multiple occur in one frame
+ // (they can't go beyond the current time so there is no cheat issue
+ // with this approach, and if they don't send input for a while they
+ // start moving anyway, so the longest 'lagaport' possible is
+ // determined by the sv_clmovement_inputtimeout cvar)
+ if (moveframetime <= 0)
+ continue;
+ oldframetime = prog->globals.server->frametime;
+ oldframetime2 = sv.frametime;
+ // update ping time for qc to see while executing this move
+ host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
+ // the server and qc frametime values must be changed temporarily
+ prog->globals.server->frametime = sv.frametime = moveframetime;
+ // if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
+ if (sv.frametime > 0.05)
+ {
+ prog->globals.server->frametime = sv.frametime = moveframetime * 0.5f;
+ SV_Physics_ClientMove();
+ }
+ SV_Physics_ClientMove();
+ sv.frametime = oldframetime2;
+ prog->globals.server->frametime = oldframetime;
+ host_client->clmovement_inputtimeout = sv_clmovement_inputtimeout.value;
+ }
+ }
+ }
+ else
+ {
+ // try to gather button bits from old moves, but only if their time is
+ // advancing (ones with the same timestamp can't be trusted)
+ for (moveindex = 0;moveindex < sv_numreadmoves-1;moveindex++)
+ {
+ usercmd_t *move = sv_readmoves + moveindex;
+ if (host_client->cmd.time < move->time)
+ {
+ sv_readmoves[sv_numreadmoves-1].buttons |= move->buttons;
+ if (move->impulse)
+ sv_readmoves[sv_numreadmoves-1].impulse = move->impulse;
+ }
+ }
+ // now copy the new move
+ host_client->cmd = sv_readmoves[sv_numreadmoves-1];
+ host_client->cmd.time = max(host_client->cmd.time, sv.time);
+ // physics will run up to sv.time, so allow no predicted moves
+ // before that otherwise, there is a speedhack by turning
+ // prediction on and off repeatedly on client side because the
+ // engine would run BOTH client and server physics for the same
+ // time
+ host_client->movesequence = 0;
+ // make sure that normal physics takes over immediately
+ host_client->clmovement_inputtimeout = 0;
+ }