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