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