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