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