2 * Copyright (c) 2011 Rudolf Polzer
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 var float autocvar_cl_predictionerrorcompensation = 0;
24 var float autocvar_cl_movement_replay;
25 var float autocvar_chase_active;
26 var float autocvar_chase_back;
31 vector csqcplayer_origin, csqcplayer_velocity;
32 float csqcplayer_sequence, player_pmflags;
33 float csqcplayer_moveframe;
34 vector csqcplayer_predictionerror;
35 float csqcplayer_predictionerrortime;
37 vector CSQCPlayer_GetPredictionError()
39 if(!autocvar_cl_predictionerrorcompensation)
41 if(time < csqcplayer_predictionerrortime)
42 return csqcplayer_predictionerror * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
46 void CSQCPlayer_SetPredictionError(vector v)
48 if(!autocvar_cl_predictionerrorcompensation)
50 csqcplayer_predictionerror = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerror + v;
51 csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
54 void CSQCPlayer_Unpredict()
56 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
58 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
59 error("Cannot unpredict in current status");
60 self.origin = csqcplayer_origin;
61 self.velocity = csqcplayer_velocity;
62 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
63 self.pmove_flags = player_pmflags;
66 void CSQCPlayer_SavePrediction()
68 player_pmflags = self.pmove_flags;
69 csqcplayer_origin = self.origin;
70 csqcplayer_velocity = self.velocity;
71 csqcplayer_sequence = servercommandframe;
72 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
75 void CSQCPlayer_PredictTo(float endframe)
77 CSQCPlayer_Unpredict();
79 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
81 if (getstatf(STAT_HEALTH) <= 0)
83 csqcplayer_moveframe = clientcommandframe;
84 getinputstate(csqcplayer_moveframe-1);
88 while(csqcplayer_moveframe < endframe)
90 if (!getinputstate(csqcplayer_moveframe))
94 runstandardplayerphysics(self);
96 csqcplayer_moveframe++;
99 //add in anything that was applied after (for low packet rate protocols)
100 input_angles = view_angles;
103 float CSQCPlayer_IsLocalPlayer()
105 return (self == csqcplayer);
108 void CSQCPlayer_SetCamera()
117 if(servercommandframe == 0)
119 InterpolateOrigin_Do();
123 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
127 v = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
128 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
129 CSQCPlayer_PredictTo(servercommandframe + 1);
130 CSQCPlayer_SetPredictionError(o - self.origin);
133 CSQCPlayer_SavePrediction();
135 CSQCPlayer_PredictTo(clientcommandframe);
140 org = csqcplayer.origin + '0 0 1' * getstatf(STAT_VIEWHEIGHT) + CSQCPlayer_GetPredictionError();
141 ang = R_SetView3fv(VF_ANGLES);
143 // simulate missing engine features
144 if(autocvar_chase_active)
148 dist = -autocvar_chase_back - 8;
150 chase_dest = org + v_forward * dist;
151 traceline(org, chase_dest, MOVE_NOMONSTERS, csqcplayer);
152 org = trace_endpos + 8 * v_forward + 4 * trace_plane_normal;
155 R_SetView3fv(VF_ORIGIN, org);
156 R_SetView3fv(VF_ANGLES, ang);
160 float CSQCPlayer_PreUpdate()
162 if(self.entnum != player_localentnum)
164 cvar_clientsettemp("cl_movement_replay", "0");
165 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
166 CSQCPlayer_Unpredict();
170 float CSQCPlayer_PostUpdate()
172 if(self.entnum <= maxclients)
173 self.renderflags |= RF_EXTERNALMODEL;
175 self.renderflags &~= RF_EXTERNALMODEL;
176 if(self.entnum != player_localentnum)
178 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
183 entity CSQCPlayer_GetPlayer(float pl)
185 return findfloat(world, entnum, pl); // FIXME optimize this using an array