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