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