]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodel/cl_player.qc
bcb0c08311c9e98f77121e46ef973e459996d517
[xonotic/xonotic-data.pk3dir.git] / qcsrc / csqcmodel / 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_predictionerrorcompensation = 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_DUCKED 4
29 #define PMF_ONGROUND 8
30 #define REFDEFFLAG_TELEPORTED 1
31 #define REFDEFFLAG_JUMPING 2
32
33 entity csqcplayer;
34 vector csqcplayer_origin, csqcplayer_velocity;
35 float csqcplayer_sequence, player_pmflags;
36 float csqcplayer_moveframe;
37 vector csqcplayer_predictionerror;
38 float csqcplayer_predictionerrortime;
39
40 vector CSQCPlayer_GetPredictionError()
41 {
42         if(!autocvar_cl_predictionerrorcompensation)
43                 return '0 0 0';
44         if(time < csqcplayer_predictionerrortime)
45                 return csqcplayer_predictionerror * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
46         return '0 0 0';
47 }
48
49 void CSQCPlayer_SetPredictionError(vector v)
50 {
51         if(!autocvar_cl_predictionerrorcompensation)
52                 return;
53         csqcplayer_predictionerror = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerror + v;
54         csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
55 }
56
57 void CSQCPlayer_Unpredict()
58 {
59         if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
60                 return;
61         if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
62                 error("Cannot unpredict in current status");
63         self.origin = csqcplayer_origin;
64         self.velocity = csqcplayer_velocity;
65         csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
66         self.pmove_flags = player_pmflags;
67 }
68
69 void CSQCPlayer_SetMinsMaxs()
70 {
71         if(self.pmove_flags & PMF_DUCKED)
72         {
73                 self.mins = PL_CROUCH_MIN;
74                 self.maxs = PL_CROUCH_MAX;
75                 self.view_ofs = PL_CROUCH_VIEW_OFS;
76         }
77         else
78         {
79                 self.mins = PL_MIN;
80                 self.maxs = PL_MAX;
81                 self.view_ofs = PL_VIEW_OFS;
82         }
83 }
84
85 void CSQCPlayer_SavePrediction()
86 {
87         player_pmflags = self.pmove_flags;
88         csqcplayer_origin = self.origin;
89         csqcplayer_velocity = self.velocity;
90         csqcplayer_sequence = servercommandframe;
91         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
92 }
93
94 void CSQCPlayer_PredictTo(float endframe)
95 {
96         CSQCPlayer_Unpredict();
97         CSQCPlayer_SetMinsMaxs();
98
99         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
100
101         if (getstatf(STAT_HEALTH) <= 0)
102         {
103                 csqcplayer_moveframe = clientcommandframe;
104                 getinputstate(csqcplayer_moveframe-1);
105                 return;
106         }
107
108         while(csqcplayer_moveframe < endframe)
109         {
110                 if (!getinputstate(csqcplayer_moveframe))
111                 {
112                         break;
113                 }
114                 runstandardplayerphysics(self);
115                 CSQCPlayer_SetMinsMaxs();
116                 csqcplayer_moveframe++;
117         }
118
119         //add in anything that was applied after (for low packet rate protocols)
120         input_angles = view_angles;
121 }
122
123 float CSQCPlayer_IsLocalPlayer()
124 {
125         return (self == csqcplayer);
126 }
127
128 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
129
130 void CSQCPlayer_SetCamera()
131 {
132         if(csqcplayer)
133         {
134                 entity oldself;
135                 oldself = self;
136                 self = csqcplayer;
137
138 #ifdef COMPAT_XON050_ENGINE
139                 if(servercommandframe == 0 || !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
140 #else
141                 if(servercommandframe == 0)
142 #endif
143                 {
144                         InterpolateOrigin_Do();
145                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
146
147                         // get crouch state from the server
148                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
149                                 self.pmove_flags &~= PMF_DUCKED;
150                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
151                                 self.pmove_flags |= PMF_DUCKED;
152
153                         // get onground state from the server
154                         if(pmove_onground)
155                                 self.pmove_flags |= PMF_ONGROUND;
156                         else
157                                 self.pmove_flags &~= PMF_ONGROUND;
158
159                         CSQCPlayer_SetMinsMaxs();
160
161                         // override it back just in case
162                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
163                 }
164                 else
165                 {
166                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
167                         {
168                                 vector o, v;
169                                 o = self.origin;
170                                 v = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
171                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
172                                 CSQCPlayer_PredictTo(servercommandframe + 1);
173                                 CSQCPlayer_SetPredictionError(o - self.origin);
174                                 self.origin = o;
175                                 self.velocity = v;
176
177                                 // get crouch state from the server
178                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
179                                         self.pmove_flags &~= PMF_DUCKED;
180                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
181                                         self.pmove_flags |= PMF_DUCKED;
182
183                                 // get onground state from the server
184                                 if(pmove_onground)
185                                         self.pmove_flags |= PMF_ONGROUND;
186                                 else
187                                         self.pmove_flags &~= PMF_ONGROUND;
188
189                                 CSQCPlayer_SavePrediction();
190                         }
191                         CSQCPlayer_PredictTo(clientcommandframe);
192
193                         CSQCPlayer_SetMinsMaxs();
194                 }
195
196                 // relink
197                 setorigin(self, self.origin);
198
199                 self.angles_y = input_angles_y;
200
201                 self = oldself;
202         }
203
204         entity view;
205 #ifdef COMPAT_XON050_ENGINE
206         view = CSQCModel_server2csqc((spectatee_status > 0) ? spectatee_status : player_localentnum);
207 #else
208         view = CSQCModel_server2csqc(player_localentnum);
209 #endif
210
211 #ifdef COMPAT_XON050_ENGINE
212         if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
213         {
214                 // legacy code, not totally correct, but good enough for not having V_CalcRefdef
215                 setproperty(VF_ORIGIN, view.origin + '0 0 1' * getstati(STAT_VIEWHEIGHT));
216                 setproperty(VF_ANGLES, view.angles);
217         }
218         else
219 #endif
220         if(view)
221         {
222                 var float refdefflags = 0;
223
224                 if(view.csqcmodel_teleported)
225                 {
226                         refdefflags |= REFDEFFLAG_TELEPORTED;
227                         view.csqcmodel_teleported = 0;
228                 }
229
230                 if(input_buttons & 4)
231                         refdefflags |= REFDEFFLAG_JUMPING;
232
233                 V_CalcRefdef(view, refdefflags);
234         }
235         else
236         {
237                 // no setup, keep engine provided values
238                 // to support a legacy entity being the view
239         }
240
241         { CSQCPLAYER_HOOK_POSTCAMERASETUP }
242 }
243
244 void CSQCPlayer_Remove()
245 {
246         if(self.entnum != player_localnum + 1)
247                 return;
248         csqcplayer = world;
249         cvar_clientsettemp("cl_movement_replay", "1");
250 }
251
252 float CSQCPlayer_PreUpdate()
253 {
254         if(self.entnum != player_localnum + 1)
255                 return 0;
256         cvar_clientsettemp("cl_movement_replay", "0");
257         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
258                 CSQCPlayer_Unpredict();
259         return 1;
260 }
261
262 float CSQCPlayer_PostUpdate()
263 {
264         if(self.entnum != player_localentnum)
265                 return 0;
266         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
267         csqcplayer = self;
268         self.entremove = CSQCPlayer_Remove;
269         return 1;
270 }