]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodellib/cl_player.qc
Merge branch 'master' into TimePath/experiments/csqc_prediction
[xonotic/xonotic-data.pk3dir.git] / qcsrc / csqcmodellib / cl_player.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * IN THE SOFTWARE.
21  */
22
23 var float autocvar_cl_movement_errorcompensation = 0;
24 var float autocvar_cl_movement = 2; // testing purposes
25
26 // engine stuff
27 #define REFDEFFLAG_TELEPORTED 1
28 #define REFDEFFLAG_JUMPING 2
29 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
30
31 vector csqcplayer_origin, csqcplayer_velocity;
32 float csqcplayer_sequence, player_pmflags;
33 float csqcplayer_moveframe;
34 vector csqcplayer_predictionerroro;
35 vector csqcplayer_predictionerrorv;
36 float csqcplayer_predictionerrortime;
37 float csqcplayer_predictionerrorfactor;
38
39 vector CSQCPlayer_GetPredictionErrorO()
40 {
41         if(time >= csqcplayer_predictionerrortime)
42                 return '0 0 0';
43         return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
44 }
45
46 vector CSQCPlayer_GetPredictionErrorV()
47 {
48         if(time >= csqcplayer_predictionerrortime)
49                 return '0 0 0';
50         return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
51 }
52
53 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
54 {
55         // error too big to compensate, we LIKELY hit a teleport or a
56         // jumppad, or it's a jump time disagreement that'll get fixed
57         // next frame
58
59         // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
60         /*
61         // commented out as this one did not help
62         if(onground_diff)
63         {
64                 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
65                 return;
66         }
67         */
68         if(vlen(o) > 32 || vlen(v) > 192)
69         {
70                 //printf("TOO BIG: x=%v v=%v\n", o, v);
71                 return;
72         }
73
74         if(!autocvar_cl_movement_errorcompensation)
75         {
76                 csqcplayer_predictionerrorfactor = 0;
77                 return;
78         }
79
80         csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
81         csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
82         csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
83         csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
84 }
85
86 void CSQCPlayer_Unpredict()
87 {
88         if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
89                 return;
90         if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
91                 error("Cannot unpredict in current status");
92         self.origin = csqcplayer_origin;
93         self.velocity = csqcplayer_velocity;
94         csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
95         self.pmove_flags = player_pmflags;
96 }
97
98 void CSQCPlayer_SetMinsMaxs()
99 {
100         if(self.pmove_flags & PMF_DUCKED)
101         {
102                 self.mins = PL_CROUCH_MIN;
103                 self.maxs = PL_CROUCH_MAX;
104                 self.view_ofs = PL_CROUCH_VIEW_OFS;
105         }
106         else
107         {
108                 self.mins = PL_MIN;
109                 self.maxs = PL_MAX;
110                 self.view_ofs = PL_VIEW_OFS;
111         }
112 }
113
114 void CSQCPlayer_SavePrediction()
115 {
116         player_pmflags = self.pmove_flags;
117         csqcplayer_origin = self.origin;
118         csqcplayer_velocity = self.velocity;
119         csqcplayer_sequence = servercommandframe;
120         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
121 }
122
123 void CSQC_ClientMovement_PlayerMove_Frame();
124
125 void CSQCPlayer_Physics(void)
126 {
127         switch(autocvar_cl_movement)
128         {
129                 case 1: runstandardplayerphysics(self); break;
130                 case 2: CSQC_ClientMovement_PlayerMove_Frame(); break;
131         }
132 }
133
134 void CSQCPlayer_PredictTo(float endframe, float apply_error)
135 {
136         CSQCPlayer_Unpredict();
137         if(apply_error)
138         {
139                 self.origin += CSQCPlayer_GetPredictionErrorO();
140                 self.velocity += CSQCPlayer_GetPredictionErrorV();
141         }
142         CSQCPlayer_SetMinsMaxs();
143
144         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
145
146 #if 0
147         // we don't need this
148         // darkplaces makes servercommandframe == 0 in these cases anyway
149         if (getstatf(STAT_HEALTH) <= 0)
150         {
151                 csqcplayer_moveframe = clientcommandframe;
152                 getinputstate(csqcplayer_moveframe-1);
153                 print("the Weird code path got hit\n");
154                 return;
155         }
156 #endif
157
158         if(csqcplayer_moveframe >= endframe)
159         {
160                 getinputstate(csqcplayer_moveframe - 1);
161         }
162         else
163         {
164                 do
165                 {
166                         if (!getinputstate(csqcplayer_moveframe))
167                                 break;
168                         CSQCPlayer_Physics();
169                         CSQCPlayer_SetMinsMaxs();
170                         csqcplayer_moveframe++;
171                 }
172                 while(csqcplayer_moveframe < endframe);
173         }
174
175         //add in anything that was applied after (for low packet rate protocols)
176         input_angles = view_angles;
177 }
178
179 float CSQCPlayer_IsLocalPlayer()
180 {
181         return (self == csqcplayer);
182 }
183
184 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
185
186 void CSQCPlayer_SetCamera()
187 {
188         vector v0;
189         v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
190
191         if(csqcplayer)
192         {
193                 entity oldself;
194                 oldself = self;
195                 self = csqcplayer;
196
197                 if(servercommandframe == 0 || clientcommandframe == 0)
198                 {
199                         InterpolateOrigin_Do();
200                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
201
202                         // get crouch state from the server
203                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
204                                 self.pmove_flags &= ~PMF_DUCKED;
205                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
206                                 self.pmove_flags |= PMF_DUCKED;
207
208                         // get onground state from the server
209                         if(pmove_onground)
210                                 self.pmove_flags |= PMF_ONGROUND;
211                         else
212                                 self.pmove_flags &= ~PMF_ONGROUND;
213
214                         CSQCPlayer_SetMinsMaxs();
215
216                         // override it back just in case
217                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
218
219                         // set velocity
220                         self.velocity = v0;
221                 }
222                 else
223                 {
224                         float flg = self.iflags;
225                         self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
226                         InterpolateOrigin_Do();
227                         self.iflags = flg;
228
229                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
230                         {
231                                 vector o, v;
232                                 o = self.origin;
233                                 v = v0;
234                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
235                                 CSQCPlayer_PredictTo(servercommandframe + 1, FALSE);
236                                 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.pmove_flags & PMF_ONGROUND));
237                                 self.origin = o;
238                                 self.velocity = v;
239
240                                 // get crouch state from the server
241                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
242                                         self.pmove_flags &= ~PMF_DUCKED;
243                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
244                                         self.pmove_flags |= PMF_DUCKED;
245
246                                 // get onground state from the server
247                                 if(pmove_onground)
248                                         self.pmove_flags |= PMF_ONGROUND;
249                                 else
250                                         self.pmove_flags &= ~PMF_ONGROUND;
251
252                                 CSQCPlayer_SavePrediction();
253                         }
254                         CSQCPlayer_PredictTo(clientcommandframe + 1, TRUE);
255
256 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
257                         // get crouch state from the server (LAG)
258                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
259                                 self.pmove_flags &= ~PMF_DUCKED;
260                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
261                                 self.pmove_flags |= PMF_DUCKED;
262 #endif
263
264                         CSQCPlayer_SetMinsMaxs();
265
266                         self.angles_y = input_angles_y;
267                 }
268
269                 // relink
270                 setorigin(self, self.origin);
271
272                 self = oldself;
273         }
274
275         entity view;
276         view = CSQCModel_server2csqc(player_localentnum);
277
278         if(view && view != csqcplayer)
279         {
280                 entity oldself = self;
281                 self = view;
282                 InterpolateOrigin_Do();
283                 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
284                 self = oldself;
285         }
286
287         if(view)
288         {
289                 var float refdefflags = 0;
290
291                 if(view.csqcmodel_teleported)
292                         refdefflags |= REFDEFFLAG_TELEPORTED;
293
294                 if(input_buttons & 4)
295                         refdefflags |= REFDEFFLAG_JUMPING;
296
297                 // note: these two only work in WIP2, but are harmless in WIP1
298                 if(getstati(STAT_HEALTH) <= 0)
299                         refdefflags |= REFDEFFLAG_DEAD;
300
301                 if(intermission)
302                         refdefflags |= REFDEFFLAG_INTERMISSION;
303
304                 V_CalcRefdef(view, refdefflags);
305         }
306         else
307         {
308                 // FIXME by CSQC spec we have to do this:
309                 // but it breaks chase cam
310                 /*
311                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
312                 setproperty(VF_ANGLES, view_angles);
313                 */
314         }
315
316         { CSQCPLAYER_HOOK_POSTCAMERASETUP }
317 }
318
319 void CSQCPlayer_Remove()
320 {
321         csqcplayer = world;
322         cvar_settemp("cl_movement_replay", "1");
323 }
324
325 float CSQCPlayer_PreUpdate()
326 {
327         if(self != csqcplayer)
328                 return 0;
329         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
330                 CSQCPlayer_Unpredict();
331         return 1;
332 }
333
334 float CSQCPlayer_PostUpdate()
335 {
336         if(self.entnum != player_localnum + 1)
337                 return 0;
338         csqcplayer = self;
339         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
340         cvar_settemp("cl_movement_replay", "0");
341         self.entremove = CSQCPlayer_Remove;
342         return 1;
343 }