]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodel/cl_player.qc
final weapon fixes... for now
[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                 if(servercommandframe == 0)
139                 {
140                         InterpolateOrigin_Do();
141                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
142
143                         // get crouch state from the server
144                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
145                                 self.pmove_flags &~= PMF_DUCKED;
146                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
147                                 self.pmove_flags |= PMF_DUCKED;
148
149                         // get onground state from the server
150                         if(pmove_onground)
151                                 self.pmove_flags |= PMF_ONGROUND;
152                         else
153                                 self.pmove_flags &~= PMF_ONGROUND;
154
155                         CSQCPlayer_SetMinsMaxs();
156
157                         // override it back just in case
158                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
159                 }
160                 else
161                 {
162                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
163                         {
164                                 vector o, v;
165                                 o = self.origin;
166                                 v = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
167                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
168                                 CSQCPlayer_PredictTo(servercommandframe + 1);
169                                 CSQCPlayer_SetPredictionError(o - self.origin);
170                                 self.origin = o;
171                                 self.velocity = v;
172
173                                 // get crouch state from the server
174                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
175                                         self.pmove_flags &~= PMF_DUCKED;
176                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
177                                         self.pmove_flags |= PMF_DUCKED;
178
179                                 // get onground state from the server
180                                 if(pmove_onground)
181                                         self.pmove_flags |= PMF_ONGROUND;
182                                 else
183                                         self.pmove_flags &~= PMF_ONGROUND;
184
185                                 CSQCPlayer_SavePrediction();
186                         }
187                         CSQCPlayer_PredictTo(clientcommandframe);
188
189                         CSQCPlayer_SetMinsMaxs();
190                 }
191
192                 // relink
193                 setorigin(self, self.origin);
194
195                 // FIXME support svc_setview?
196
197                 if(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1"))
198                 {
199                         var float refdefflags = 0;
200
201                         if(self.csqcmodel_teleported)
202                         {
203                                 refdefflags |= REFDEFFLAG_TELEPORTED;
204                                 self.csqcmodel_teleported = 0;
205                         }
206
207                         if(input_buttons & 4)
208                                 refdefflags |= REFDEFFLAG_JUMPING;
209
210                         V_CalcRefdef(self, refdefflags);
211                 }
212                 else
213                         setproperty(VF_ORIGIN, self.origin + self.view_ofs);
214                 self.angles_y = input_angles_y;
215
216                 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
217
218                 self = oldself;
219         }
220 }
221
222 void CSQCPlayer_Remove()
223 {
224         if(self.entnum != player_localentnum)
225                 return;
226         csqcplayer = world;
227         cvar_clientsettemp("cl_movement_replay", "1");
228 }
229
230 float CSQCPlayer_PreUpdate()
231 {
232         if(self.entnum != player_localentnum)
233                 return 0;
234         cvar_clientsettemp("cl_movement_replay", "0");
235         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
236                 CSQCPlayer_Unpredict();
237         return 1;
238 }
239
240 float CSQCPlayer_PostUpdate()
241 {
242         /*
243         if(self.entnum == player_localentnum)
244                 self.renderflags |= RF_EXTERNALMODEL;
245         else
246                 self.renderflags &~= RF_EXTERNALMODEL;
247         */
248
249         if(self.entnum != player_localentnum)
250                 return 0;
251         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
252         csqcplayer = self;
253         self.entremove = CSQCPlayer_Remove;
254         return 1;
255 }
256
257 entity CSQCPlayer_GetPlayer(float pl)
258 {
259         return findfloat(world, entnum, pl); // FIXME optimize this using an array
260 }