+ // if the previous move has not been applied yet, we need to accumulate
+ // the impulse/buttons from it
+ if (!host_client->cmd.applied)
+ {
+ if (!move->impulse)
+ move->impulse = host_client->cmd.impulse;
+ move->buttons |= host_client->cmd.buttons;
+ }
+
+ // now store this move for later execution
+ // (we have to buffer the moves because of old ones being repeated)
+ if (sv_numreadmoves < CL_MAX_USERCMDS)
+ sv_readmoves[sv_numreadmoves++] = *move;
+
+ // movement packet loss tracking
+ // bones_was_here: checking begun prevents heavy loss detection right after a map change
+ if(move->sequence && host_client->begun)
+ {
+ if(move->sequence > host_client->movement_highestsequence_seen)
+ {
+ if(host_client->movement_highestsequence_seen)
+ {
+ // mark moves in between as lost
+ unsigned int delta = move->sequence - host_client->movement_highestsequence_seen - 1;
+ if(delta < NETGRAPH_PACKETS)
+ {
+ unsigned int u;
+ for(u = 0; u < delta; ++u)
+ host_client->movement_count[(host_client->movement_highestsequence_seen + 1 + u) % NETGRAPH_PACKETS] = -1;
+ }
+ else
+ memset(host_client->movement_count, -1, sizeof(host_client->movement_count));
+ }
+ // mark THIS move as seen for the first time
+ host_client->movement_count[move->sequence % NETGRAPH_PACKETS] = 1;
+ // update highest sequence seen
+ host_client->movement_highestsequence_seen = move->sequence;
+ }
+ else
+ if(host_client->movement_count[move->sequence % NETGRAPH_PACKETS] >= 0)
+ ++host_client->movement_count[move->sequence % NETGRAPH_PACKETS];
+ }
+ else
+ {
+ host_client->movement_highestsequence_seen = 0;
+ memset(host_client->movement_count, 0, sizeof(host_client->movement_count));
+ }
+}
+
+static void SV_ExecuteClientMoves(void)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ int moveindex;
+ double moveframetime;
+ double oldframetime;
+ double oldframetime2;
+
+ if (sv_numreadmoves < 1)
+ return;
+ // only start accepting input once the player is spawned
+ if (!host_client->begun)
+ return;
+#if DEBUGMOVES
+ Con_Printf("SV_ExecuteClientMoves: read %i moves at sv.time %f\n", sv_numreadmoves, (float)sv.time);
+#endif
+ // disable clientside movement prediction in some cases
+ if (ceil(max(sv_readmoves[sv_numreadmoves-1].receivetime - sv_readmoves[sv_numreadmoves-1].time, 0) * 1000.0) < sv_clmovement_minping.integer)
+ host_client->clmovement_disabletimeout = host.realtime + sv_clmovement_minping_disabletime.value / 1000.0;
+ // 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 <= host.realtime && (PRVM_serveredictfloat(host_client->edict, disableclientprediction) == -1 || (PRVM_serveredictfloat(host_client->edict, movetype) == MOVETYPE_WALK && (!PRVM_serveredictfloat(host_client->edict, disableclientprediction)))))
+ {
+ // 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 #%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);
+#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
+ // bones_was_here: limit moveframetime to a multiple of sv.frametime to match inputtimeout behaviour
+ moveframetime = min(move->time - host_client->cmd.time, min(0.1, sys_ticrate.value > 0.0 && sv.frametime > 0.0 ? sv.frametime * ceil(sv_clmovement_inputtimeout.value / sv.frametime) : 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)
+ oldframetime = PRVM_serverglobalfloat(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
+ PRVM_serverglobalfloat(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)
+ {
+ PRVM_serverglobalfloat(frametime) = sv.frametime = moveframetime * 0.5f;
+ SV_Physics_ClientMove();
+ }
+ SV_Physics_ClientMove();
+ sv.frametime = oldframetime2;
+ PRVM_serverglobalfloat(frametime) = oldframetime;
+ host_client->clmovement_inputtimeout = min(0.1, 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;
+ // update ping time
+ host_client->ping = host_client->cmd.receivetime - sv_readmoves[sv_numreadmoves-1].time;
+ }
+}
+
+void SV_ApplyClientMove (void)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ usercmd_t *move = &host_client->cmd;
+ int j, movementloss, packetloss;
+
+ if (!move->receivetime)
+ return;
+
+ // note: a move can be applied multiple times if the client packets are
+ // not coming as often as the physics is executed, and the move must be
+ // applied before running qc each time because the id1 qc had a bug where
+ // it clears self.button2 in PlayerJump, causing pogostick behavior if
+ // moves are not applied every time before calling qc
+ move->applied = true;
+
+ // set the edict fields
+ PRVM_serveredictfloat(host_client->edict, button0) = move->buttons & 1;
+ PRVM_serveredictfloat(host_client->edict, button2) = (move->buttons & 2)>>1;
+ if (move->impulse)
+ PRVM_serveredictfloat(host_client->edict, impulse) = move->impulse;
+ // only send the impulse to qc once
+ move->impulse = 0;
+
+ movementloss = packetloss = 0;
+ if(host_client->netconnection)
+ {
+ for (j = 0;j < NETGRAPH_PACKETS;j++)
+ if (host_client->netconnection->incoming_netgraph[j].unreliablebytes == NETGRAPH_LOSTPACKET)
+ packetloss++;
+ for (j = 0;j < NETGRAPH_PACKETS;j++)
+ if (host_client->movement_count[j] < 0)
+ movementloss++;
+ }
+
+ VectorCopy(move->viewangles, PRVM_serveredictvector(host_client->edict, v_angle));
+ PRVM_serveredictfloat(host_client->edict, button3) = ((move->buttons >> 2) & 1);
+ PRVM_serveredictfloat(host_client->edict, button4) = ((move->buttons >> 3) & 1);
+ PRVM_serveredictfloat(host_client->edict, button5) = ((move->buttons >> 4) & 1);
+ PRVM_serveredictfloat(host_client->edict, button6) = ((move->buttons >> 5) & 1);
+ PRVM_serveredictfloat(host_client->edict, button7) = ((move->buttons >> 6) & 1);
+ PRVM_serveredictfloat(host_client->edict, button8) = ((move->buttons >> 7) & 1);
+ PRVM_serveredictfloat(host_client->edict, button9) = ((move->buttons >> 11) & 1);
+ PRVM_serveredictfloat(host_client->edict, button10) = ((move->buttons >> 12) & 1);
+ PRVM_serveredictfloat(host_client->edict, button11) = ((move->buttons >> 13) & 1);
+ PRVM_serveredictfloat(host_client->edict, button12) = ((move->buttons >> 14) & 1);
+ PRVM_serveredictfloat(host_client->edict, button13) = ((move->buttons >> 15) & 1);
+ PRVM_serveredictfloat(host_client->edict, button14) = ((move->buttons >> 16) & 1);
+ PRVM_serveredictfloat(host_client->edict, button15) = ((move->buttons >> 17) & 1);
+ PRVM_serveredictfloat(host_client->edict, button16) = ((move->buttons >> 18) & 1);
+ PRVM_serveredictfloat(host_client->edict, buttonuse) = ((move->buttons >> 8) & 1);
+ PRVM_serveredictfloat(host_client->edict, buttonchat) = ((move->buttons >> 9) & 1);
+ PRVM_serveredictfloat(host_client->edict, cursor_active) = ((move->buttons >> 10) & 1);
+ VectorSet(PRVM_serveredictvector(host_client->edict, movement), move->forwardmove, move->sidemove, move->upmove);
+ VectorCopy(move->cursor_screen, PRVM_serveredictvector(host_client->edict, cursor_screen));
+ VectorCopy(move->cursor_start, PRVM_serveredictvector(host_client->edict, cursor_trace_start));
+ VectorCopy(move->cursor_impact, PRVM_serveredictvector(host_client->edict, cursor_trace_endpos));
+ PRVM_serveredictedict(host_client->edict, cursor_trace_ent) = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
+ PRVM_serveredictfloat(host_client->edict, ping) = host_client->ping * 1000.0;
+ PRVM_serveredictfloat(host_client->edict, ping_packetloss) = packetloss / (float) NETGRAPH_PACKETS;
+ PRVM_serveredictfloat(host_client->edict, ping_movementloss) = movementloss / (float) NETGRAPH_PACKETS;
+}
+
+static qbool SV_FrameLost(int framenum)
+{
+ if (host_client->entitydatabase5)
+ {
+ if (framenum <= host_client->entitydatabase5->latestframenum)
+ {
+ EntityFrame5_LostFrame(host_client->entitydatabase5, framenum);
+ EntityFrameCSQC_LostFrame(host_client, framenum);
+ return true;
+ }
+ }
+ return false;
+}
+
+static void SV_FrameAck(int framenum)
+{
+ if (host_client->entitydatabase)
+ EntityFrame_AckFrame(host_client->entitydatabase, framenum);
+ else if (host_client->entitydatabase4)
+ EntityFrame4_AckFrame(host_client->entitydatabase4, framenum, true);
+ else if (host_client->entitydatabase5)
+ EntityFrame5_AckFrame(host_client->entitydatabase5, framenum);