]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodellib/cl_player.qc
#include this
[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 #if defined(CSQC)
23         #include "../dpdefs/csprogsdefs.qc"
24         #include "../client/Defs.qc"
25         #include "../common/constants.qh"
26         #include "../common/stats.qh"
27         #include "../common/util.qh"
28         #include "interpolate.qh"
29         #include "../client/main.qh"
30         #include "common.qh"
31         #include "cl_model.qh"
32         #include "cl_player.qh"
33 #elif defined(MENUQC)
34 #elif defined(SVQC)
35 #endif
36
37 float autocvar_cl_movement_errorcompensation = 0;
38
39 // engine stuff
40 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
41
42 vector csqcplayer_origin, csqcplayer_velocity;
43 float csqcplayer_sequence, player_pmflags;
44 float csqcplayer_moveframe;
45 vector csqcplayer_predictionerroro;
46 vector csqcplayer_predictionerrorv;
47 float csqcplayer_predictionerrortime;
48 float csqcplayer_predictionerrorfactor;
49
50 vector CSQCPlayer_GetPredictionErrorO()
51 {
52         if(time >= csqcplayer_predictionerrortime)
53                 return '0 0 0';
54         return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
55 }
56
57 vector CSQCPlayer_GetPredictionErrorV()
58 {
59         if(time >= csqcplayer_predictionerrortime)
60                 return '0 0 0';
61         return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
62 }
63
64 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
65 {
66         // error too big to compensate, we LIKELY hit a teleport or a
67         // jumppad, or it's a jump time disagreement that'll get fixed
68         // next frame
69
70         // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
71         /*
72         // commented out as this one did not help
73         if(onground_diff)
74         {
75                 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
76                 return;
77         }
78         */
79         if(vlen(o) > 32 || vlen(v) > 192)
80         {
81                 //printf("TOO BIG: x=%v v=%v\n", o, v);
82                 return;
83         }
84
85         if(!autocvar_cl_movement_errorcompensation)
86         {
87                 csqcplayer_predictionerrorfactor = 0;
88                 return;
89         }
90
91         csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
92         csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
93         csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
94         csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
95 }
96
97 void CSQCPlayer_Unpredict()
98 {
99         if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
100                 return;
101         if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
102                 error("Cannot unpredict in current status");
103         self.origin = csqcplayer_origin;
104         self.velocity = csqcplayer_velocity;
105         csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
106         self.pmove_flags = player_pmflags;
107 }
108
109 void CSQCPlayer_SetMinsMaxs()
110 {
111         if(self.pmove_flags & PMF_DUCKED)
112         {
113                 self.mins = PL_CROUCH_MIN;
114                 self.maxs = PL_CROUCH_MAX;
115                 self.view_ofs = PL_CROUCH_VIEW_OFS;
116         }
117         else
118         {
119                 self.mins = PL_MIN;
120                 self.maxs = PL_MAX;
121                 self.view_ofs = PL_VIEW_OFS;
122         }
123 }
124
125 void CSQCPlayer_SavePrediction()
126 {
127         player_pmflags = self.pmove_flags;
128         csqcplayer_origin = self.origin;
129         csqcplayer_velocity = self.velocity;
130         csqcplayer_sequence = servercommandframe;
131         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
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                         runstandardplayerphysics(self);
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 CSQCPlayer_SetCamera()
185 {
186         vector v0;
187         v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
188
189         if(csqcplayer)
190         {
191                 entity oldself;
192                 oldself = self;
193                 self = csqcplayer;
194
195                 if(servercommandframe == 0 || clientcommandframe == 0)
196                 {
197                         InterpolateOrigin_Do();
198                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
199
200                         // get crouch state from the server
201                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
202                                 self.pmove_flags &= ~PMF_DUCKED;
203                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
204                                 self.pmove_flags |= PMF_DUCKED;
205
206                         // get onground state from the server
207                         if(pmove_onground)
208                                 self.pmove_flags |= PMF_ONGROUND;
209                         else
210                                 self.pmove_flags &= ~PMF_ONGROUND;
211
212                         CSQCPlayer_SetMinsMaxs();
213
214                         // override it back just in case
215                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
216
217                         // set velocity
218                         self.velocity = v0;
219                 }
220                 else
221                 {
222                         float flg = self.iflags;
223                         self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
224                         InterpolateOrigin_Do();
225                         self.iflags = flg;
226
227                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
228                         {
229                                 vector o, v;
230                                 o = self.origin;
231                                 v = v0;
232                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
233                                 CSQCPlayer_PredictTo(servercommandframe + 1, false);
234                                 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.pmove_flags & PMF_ONGROUND));
235                                 self.origin = o;
236                                 self.velocity = v;
237
238                                 // get crouch state from the server
239                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
240                                         self.pmove_flags &= ~PMF_DUCKED;
241                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
242                                         self.pmove_flags |= PMF_DUCKED;
243
244                                 // get onground state from the server
245                                 if(pmove_onground)
246                                         self.pmove_flags |= PMF_ONGROUND;
247                                 else
248                                         self.pmove_flags &= ~PMF_ONGROUND;
249
250                                 CSQCPlayer_SavePrediction();
251                         }
252                         CSQCPlayer_PredictTo(clientcommandframe + 1, true);
253
254 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
255                         // get crouch state from the server (LAG)
256                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
257                                 self.pmove_flags &= ~PMF_DUCKED;
258                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
259                                 self.pmove_flags |= PMF_DUCKED;
260 #endif
261
262                         CSQCPlayer_SetMinsMaxs();
263
264                         self.angles_y = input_angles.y;
265                 }
266
267                 // relink
268                 setorigin(self, self.origin);
269
270                 self = oldself;
271         }
272
273         entity view;
274         view = CSQCModel_server2csqc(player_localentnum);
275
276         if(view && view != csqcplayer)
277         {
278                 entity oldself = self;
279                 self = view;
280                 InterpolateOrigin_Do();
281                 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
282                 self = oldself;
283         }
284
285         if(view)
286         {
287                 int refdefflags = 0;
288
289                 if(view.csqcmodel_teleported)
290                         refdefflags |= REFDEFFLAG_TELEPORTED;
291
292                 if(input_buttons & 4)
293                         refdefflags |= REFDEFFLAG_JUMPING;
294
295                 // note: these two only work in WIP2, but are harmless in WIP1
296                 if(getstati(STAT_HEALTH) <= 0)
297                         refdefflags |= REFDEFFLAG_DEAD;
298
299                 if(intermission)
300                         refdefflags |= REFDEFFLAG_INTERMISSION;
301
302                 V_CalcRefdef(view, refdefflags);
303         }
304         else
305         {
306                 // FIXME by CSQC spec we have to do this:
307                 // but it breaks chase cam
308                 /*
309                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
310                 setproperty(VF_ANGLES, view_angles);
311                 */
312         }
313
314         { CSQCPLAYER_HOOK_POSTCAMERASETUP }
315 }
316
317 void CSQCPlayer_Remove()
318 {
319         csqcplayer = world;
320         cvar_settemp("cl_movement_replay", "1");
321 }
322
323 float CSQCPlayer_PreUpdate()
324 {
325         if(self != csqcplayer)
326                 return 0;
327         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
328                 CSQCPlayer_Unpredict();
329         return 1;
330 }
331
332 float CSQCPlayer_PostUpdate()
333 {
334         if(self.entnum != player_localnum + 1)
335                 return 0;
336         csqcplayer = self;
337         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
338         cvar_settemp("cl_movement_replay", "0");
339         self.entremove = CSQCPlayer_Remove;
340         return 1;
341 }