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