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