]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodellib/cl_player.qc
Replace all direct assignments to self with setself(e)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / csqcmodellib / cl_player.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  * Copyright (c) 2015 Micah Talkiewicz
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #if defined(CSQC)
24         #include "../dpdefs/csprogsdefs.qh"
25         #include "../client/defs.qh"
26         #include "../common/constants.qh"
27         #include "../common/stats.qh"
28         #include "../common/util.qh"
29         #include "interpolate.qh"
30         #include "../client/main.qh"
31         #include "common.qh"
32         #include "cl_model.qh"
33         #include "cl_player.qh"
34         #include "../common/triggers/trigger/viewloc.qh"
35         #include "../common/viewloc.qh"
36 #elif defined(MENUQC)
37 #elif defined(SVQC)
38 #endif
39
40 float autocvar_cl_movement_errorcompensation = 0;
41 int autocvar_cl_movement = 1;
42
43 // engine stuff
44 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
45
46 vector csqcplayer_origin, csqcplayer_velocity;
47 float csqcplayer_sequence;
48 int player_pmflags;
49 float csqcplayer_moveframe;
50 vector csqcplayer_predictionerroro;
51 vector csqcplayer_predictionerrorv;
52 float csqcplayer_predictionerrortime;
53 float csqcplayer_predictionerrorfactor;
54
55 vector CSQCPlayer_GetPredictionErrorO()
56 {
57         if(time >= csqcplayer_predictionerrortime)
58                 return '0 0 0';
59         return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
60 }
61
62 vector CSQCPlayer_GetPredictionErrorV()
63 {
64         if(time >= csqcplayer_predictionerrortime)
65                 return '0 0 0';
66         return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
67 }
68
69 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
70 {
71         // error too big to compensate, we LIKELY hit a teleport or a
72         // jumppad, or it's a jump time disagreement that'll get fixed
73         // next frame
74
75         // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
76         /*
77         // commented out as this one did not help
78         if(onground_diff)
79         {
80                 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
81                 return;
82         }
83         */
84         if(vlen(o) > 32 || vlen(v) > 192)
85         {
86                 //printf("TOO BIG: x=%v v=%v\n", o, v);
87                 return;
88         }
89
90         if(!autocvar_cl_movement_errorcompensation)
91         {
92                 csqcplayer_predictionerrorfactor = 0;
93                 return;
94         }
95
96         csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
97         csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
98         csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
99         csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
100 }
101
102 void CSQCPlayer_Unpredict()
103 {SELFPARAM();
104         if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
105                 return;
106         if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
107                 error("Cannot unpredict in current status");
108         self.origin = csqcplayer_origin;
109         self.velocity = csqcplayer_velocity;
110         csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
111         self.flags = player_pmflags;
112 }
113
114 void CSQCPlayer_SetMinsMaxs()
115 {SELFPARAM();
116         if(self.flags & FL_DUCKED)
117         {
118                 self.mins = PL_CROUCH_MIN;
119                 self.maxs = PL_CROUCH_MAX;
120                 self.view_ofs = PL_CROUCH_VIEW_OFS;
121         }
122         else
123         {
124                 self.mins = PL_MIN;
125                 self.maxs = PL_MAX;
126                 self.view_ofs = PL_VIEW_OFS;
127         }
128 }
129
130 void CSQCPlayer_SavePrediction()
131 {SELFPARAM();
132         player_pmflags = self.flags;
133         csqcplayer_origin = self.origin;
134         csqcplayer_velocity = self.velocity;
135         csqcplayer_sequence = servercommandframe;
136         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
137 }
138
139 void CSQC_ClientMovement_PlayerMove_Frame();
140
141 void PM_Movement_Move()
142 {SELFPARAM();
143         runstandardplayerphysics(self);
144 #ifdef CSQC
145         self.flags =
146                         ((self.pmove_flags & PMF_DUCKED) ? FL_DUCKED : 0) |
147                         (!(self.pmove_flags & PMF_JUMP_HELD) ? FL_JUMPRELEASED : 0) |
148                         ((self.pmove_flags & PMF_ONGROUND) ? FL_ONGROUND : 0);
149 #endif
150 }
151
152 void CSQCPlayer_Physics(void)
153 {
154         switch(autocvar_cl_movement)
155         {
156                 case 1: CSQC_ClientMovement_PlayerMove_Frame(); break;
157                 case 2: PM_Movement_Move(); break;
158         }
159 }
160
161 void CSQCPlayer_PredictTo(float endframe, float apply_error)
162 {SELFPARAM();
163         CSQCPlayer_Unpredict();
164         if(apply_error)
165         {
166                 self.origin += CSQCPlayer_GetPredictionErrorO();
167                 self.velocity += CSQCPlayer_GetPredictionErrorV();
168         }
169         CSQCPlayer_SetMinsMaxs();
170
171         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
172
173 #if 0
174         // we don't need this
175         // darkplaces makes servercommandframe == 0 in these cases anyway
176         if (getstatf(STAT_HEALTH) <= 0)
177         {
178                 csqcplayer_moveframe = clientcommandframe;
179                 getinputstate(csqcplayer_moveframe-1);
180                 LOG_INFO("the Weird code path got hit\n");
181                 return;
182         }
183 #endif
184
185         if(csqcplayer_moveframe >= endframe)
186         {
187                 getinputstate(csqcplayer_moveframe - 1);
188         }
189         else
190         {
191                 do
192                 {
193                         if (!getinputstate(csqcplayer_moveframe))
194                                 break;
195                         CSQCPlayer_Physics();
196                         CSQCPlayer_SetMinsMaxs();
197                         csqcplayer_moveframe++;
198                 }
199                 while(csqcplayer_moveframe < endframe);
200         }
201
202         //add in anything that was applied after (for low packet rate protocols)
203         input_angles = view_angles;
204 }
205
206 bool CSQCPlayer_IsLocalPlayer()
207 {SELFPARAM();
208         return (self == csqcplayer);
209 }
210
211 void CSQCPlayer_SetViewLocation()
212 {
213         viewloc_SetViewLocation();
214 }
215
216 void CSQCPlayer_SetCamera()
217 {SELFPARAM();
218         vector v0;
219         v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
220
221         if(csqcplayer)
222         {
223                 entity oldself = self;
224                 setself(csqcplayer);
225
226                 if(servercommandframe == 0 || clientcommandframe == 0)
227                 {
228                         InterpolateOrigin_Do();
229                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
230
231                         // get crouch state from the server
232                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
233                                 self.flags &= ~FL_DUCKED;
234                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
235                                 self.flags |= FL_DUCKED;
236
237                         // get onground state from the server
238                         if(pmove_onground)
239                                 self.flags |= FL_ONGROUND;
240                         else
241                                 self.flags &= ~FL_ONGROUND;
242
243                         CSQCPlayer_SetMinsMaxs();
244
245                         // override it back just in case
246                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
247
248                         // set velocity
249                         self.velocity = v0;
250                 }
251                 else
252                 {
253                         float flg = self.iflags;
254                         self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
255                         InterpolateOrigin_Do();
256                         self.iflags = flg;
257
258                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
259                         {
260                                 vector o, v;
261                                 o = self.origin;
262                                 v = v0;
263                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
264                                 CSQCPlayer_PredictTo(servercommandframe + 1, false);
265                                 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.flags & FL_ONGROUND));
266                                 self.origin = o;
267                                 self.velocity = v;
268
269                                 // get crouch state from the server
270                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
271                                         self.flags &= ~FL_DUCKED;
272                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
273                                         self.flags |= FL_DUCKED;
274
275                                 // get onground state from the server
276                                 if(pmove_onground)
277                                         self.flags |= FL_ONGROUND;
278                                 else
279                                         self.flags &= ~FL_ONGROUND;
280
281                                 CSQCPlayer_SavePrediction();
282                         }
283                         CSQCPlayer_PredictTo(clientcommandframe + 1, true);
284
285 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
286                         // get crouch state from the server (LAG)
287                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
288                                 self.flags &= ~FL_DUCKED;
289                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
290                                 self.flags |= FL_DUCKED;
291 #endif
292
293                         CSQCPlayer_SetMinsMaxs();
294
295                         self.angles_y = input_angles.y;
296                 }
297
298                 // relink
299                 setorigin(self, self.origin);
300
301                 setself(oldself);
302         }
303
304         entity view;
305         view = CSQCModel_server2csqc(player_localentnum);
306
307         if(view && view != csqcplayer)
308         {
309                 SELFCALL(view, InterpolateOrigin_Do());
310                 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
311                 SELFCALL_DONE();
312         }
313
314         if(view)
315         {
316                 int refdefflags = 0;
317
318                 if(view.csqcmodel_teleported)
319                         refdefflags |= REFDEFFLAG_TELEPORTED;
320
321                 if(input_buttons & 4)
322                         refdefflags |= REFDEFFLAG_JUMPING;
323
324                 // note: these two only work in WIP2, but are harmless in WIP1
325                 if(getstati(STAT_HEALTH) <= 0)
326                         refdefflags |= REFDEFFLAG_DEAD;
327
328                 if(intermission)
329                         refdefflags |= REFDEFFLAG_INTERMISSION;
330
331                 V_CalcRefdef(view, refdefflags);
332         }
333         else
334         {
335                 // FIXME by CSQC spec we have to do this:
336                 // but it breaks chase cam
337                 /*
338                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
339                 setproperty(VF_ANGLES, view_angles);
340                 */
341         }
342
343         { CSQCPLAYER_HOOK_POSTCAMERASETUP }
344 }
345
346 void CSQCPlayer_Remove()
347 {
348         csqcplayer = world;
349         cvar_settemp("cl_movement_replay", "1");
350 }
351
352 float CSQCPlayer_PreUpdate()
353 {SELFPARAM();
354         if(self != csqcplayer)
355                 return 0;
356         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
357                 CSQCPlayer_Unpredict();
358         return 1;
359 }
360
361 float CSQCPlayer_PostUpdate()
362 {SELFPARAM();
363         if(self.entnum != player_localnum + 1)
364                 return 0;
365         csqcplayer = self;
366         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
367         cvar_settemp("cl_movement_replay", "0");
368         self.entremove = CSQCPlayer_Remove;
369         return 1;
370 }