+
+ // 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;
+}
+
+void SV_ExecuteClientMoves(void)
+{
+ int moveindex;
+ float moveframetime;
+ double oldframetime;
+ double oldframetime2;
+#ifdef NUM_PING_TIMES
+ double total;
+#endif
+ prvm_eval_t *val;
+ if (sv_numreadmoves < 1)
+ return;
+ // only start accepting input once the player is spawned
+ if (!host_client->spawned)
+ 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 = 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_waitforinput.integer > 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 = max(move->time, host_client->cmd.time); // prevent backstepping of time
+ moveframetime = bound(0, move->time - host_client->cmd.time, 0.1);
+ //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_waitforinput 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_skipphysicsframes = sv_clmovement_waitforinput.integer;
+ }
+ }
+ }
+ 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
+ sv_readmoves[sv_numreadmoves-1].time = max(sv_readmoves[sv_numreadmoves-1].time, host_client->cmd.time); // prevent backstepping of time
+ host_client->cmd = sv_readmoves[sv_numreadmoves-1];
+ host_client->movesequence = 0;
+ // make sure that normal physics takes over immediately
+ host_client->clmovement_skipphysicsframes = 0;
+ }
+
+ // calculate average ping time
+ host_client->ping = host_client->cmd.receivetime - host_client->cmd.time;
+#ifdef NUM_PING_TIMES
+ host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] = host_client->cmd.receivetime - host_client->cmd.time;
+ host_client->num_pings++;
+ for (i=0, total = 0;i < NUM_PING_TIMES;i++)
+ total += host_client->ping_times[i];
+ host_client->ping = total / NUM_PING_TIMES;
+#endif
+}
+
+void SV_ApplyClientMove (void)
+{
+ prvm_eval_t *val;
+ usercmd_t *move = &host_client->cmd;
+
+ 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
+ host_client->edict->fields.server->button0 = move->buttons & 1;
+ host_client->edict->fields.server->button2 = (move->buttons & 2)>>1;
+ if (move->impulse)
+ host_client->edict->fields.server->impulse = move->impulse;
+ // only send the impulse to qc once
+ move->impulse = 0;
+ VectorCopy(move->viewangles, host_client->edict->fields.server->v_angle);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button3))) val->_float = ((move->buttons >> 2) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button4))) val->_float = ((move->buttons >> 3) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button5))) val->_float = ((move->buttons >> 4) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button6))) val->_float = ((move->buttons >> 5) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button7))) val->_float = ((move->buttons >> 6) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button8))) val->_float = ((move->buttons >> 7) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button9))) val->_float = ((move->buttons >> 11) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button10))) val->_float = ((move->buttons >> 12) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button11))) val->_float = ((move->buttons >> 13) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button12))) val->_float = ((move->buttons >> 14) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button13))) val->_float = ((move->buttons >> 15) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button14))) val->_float = ((move->buttons >> 16) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button15))) val->_float = ((move->buttons >> 17) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.button16))) val->_float = ((move->buttons >> 18) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.buttonuse))) val->_float = ((move->buttons >> 8) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.buttonchat))) val->_float = ((move->buttons >> 9) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_active))) val->_float = ((move->buttons >> 10) & 1);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.movement))) VectorSet(val->vector, move->forwardmove, move->sidemove, move->upmove);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_screen))) VectorCopy(move->cursor_screen, val->vector);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_trace_start))) VectorCopy(move->cursor_start, val->vector);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_trace_endpos))) VectorCopy(move->cursor_impact, val->vector);
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.cursor_trace_ent))) val->edict = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM(move->cursor_entitynumber));
+ if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ping))) val->_float = host_client->ping * 1000.0;