]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/cl_model.qc
Merge branch 'master' into Mario/stats_eloranking
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / csqcmodel / cl_model.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 #include "cl_model.qh"
23
24 #include "cl_player.qh"
25 #include "common.qh"
26 #include "interpolate.qh"
27 #include <common/csqcmodel_settings.qh>
28
29 float autocvar_cl_lerpanim_maxdelta_framegroups = 0.1;
30 float autocvar_cl_nolerp = 0;
31
32 .float csqcmodel_lerpfrac;
33 .float csqcmodel_lerpfrac2;
34 .float csqcmodel_lerpfractime;
35 .float csqcmodel_lerpfrac2time;
36
37 void CSQCModel_InterpolateAnimation_2To4_PreNote(entity this, int sf)
38 {
39         if(sf & CSQCMODEL_PROPERTY_FRAME)
40         {
41                 this.frame3 = this.frame;
42                 this.frame3time = this.frame1time;
43         }
44         if(sf & CSQCMODEL_PROPERTY_FRAME2)
45         {
46                 this.frame4 = this.frame2;
47                 this.frame4time = this.frame2time;
48         }
49         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
50         {
51                 this.csqcmodel_lerpfrac2 = this.csqcmodel_lerpfrac;
52                 this.csqcmodel_lerpfrac2time = this.csqcmodel_lerpfractime;
53                 this.lerpfrac = this.csqcmodel_lerpfrac;
54         }
55 }
56 void CSQCModel_InterpolateAnimation_1To2_PreNote(entity this, int sf)
57 {
58         if(sf & CSQCMODEL_PROPERTY_FRAME)
59         {
60                 this.frame2 = this.frame;
61                 this.frame2time = this.frame1time;
62         }
63 }
64 void CSQCModel_InterpolateAnimation_PreNote(entity this, int sf)
65 {
66 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
67         CSQCModel_InterpolateAnimation_2To4_PreNote(this, sf);
68 #else
69         CSQCModel_InterpolateAnimation_1To2_PreNote(this, sf);
70 #endif
71 }
72
73 void CSQCModel_InterpolateAnimation_2To4_Note(entity this, int sf, bool set_times)
74 {
75         if(sf & CSQCMODEL_PROPERTY_FRAME)
76         {
77                 if(set_times)
78                         this.frame1time = time;
79         }
80         if(sf & CSQCMODEL_PROPERTY_FRAME2)
81         {
82                 if(set_times)
83                         this.frame2time = time;
84         }
85         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
86         {
87                 this.csqcmodel_lerpfrac = this.lerpfrac;
88                 if(set_times)
89                         this.csqcmodel_lerpfractime = time;
90         }
91 }
92 void CSQCModel_InterpolateAnimation_1To2_Note(entity this, int sf, bool set_times)
93 {
94         if(sf & CSQCMODEL_PROPERTY_FRAME)
95         {
96                 if(set_times)
97                         this.frame1time = time;
98         }
99 }
100 void CSQCModel_InterpolateAnimation_Note(entity this, int sf)
101 {
102 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
103         CSQCModel_InterpolateAnimation_2To4_Note(this, sf, true);
104 #else
105         CSQCModel_InterpolateAnimation_1To2_Note(this, sf, true);
106 #endif
107 }
108
109 void CSQCModel_InterpolateAnimation_2To4_Do(entity this)
110 {
111         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
112         {
113                 this.lerpfrac = this.csqcmodel_lerpfrac;
114                 this.lerpfrac3 = 0;
115                 this.lerpfrac4 = 0;
116         }
117         else
118         {
119                 float l13, l24, llf;
120                 float l24_13;
121
122                 if(this.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
123                         l13 = 1;
124                 else
125                         l13 = bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
126
127                 if(this.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
128                         l24 = 1;
129                 else
130                         l24 = bound(0, (time - this.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
131
132                 if(this.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
133                         llf = 1;
134                 else
135                         llf = bound(0, (time - this.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
136
137                 l24_13 = this.csqcmodel_lerpfrac * llf + this.csqcmodel_lerpfrac2 * (1 - llf);
138
139                 this.lerpfrac  = l24 * l24_13;
140                 this.lerpfrac4 = (1 - l24) * l24_13;
141                 this.lerpfrac3 = (1 - l13) * (1 - l24_13);
142
143                 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
144                 {
145                         this.frame2time = 0;
146                         this.frame4time = 0;
147                 }
148
149                 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
150                 {
151                         this.frame1time = 0;
152                         this.frame3time = 0;
153                 }
154         }
155 }
156 void CSQCModel_InterpolateAnimation_1To2_Do(entity this)
157 {
158         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
159         {
160                 this.lerpfrac = 0;
161         }
162         else
163         {
164                 if(this.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
165                         this.lerpfrac = 0;
166                 else
167                         this.lerpfrac = 1 - bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
168         }
169 }
170 void CSQCModel_InterpolateAnimation_Do(entity this)
171 {
172 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
173         CSQCModel_InterpolateAnimation_2To4_Do(this);
174 #else
175         CSQCModel_InterpolateAnimation_1To2_Do(this);
176 #endif
177 }
178
179 void CSQCModel_Draw(entity this)
180 {
181         // some nice flags for CSQCMODEL_IF and the hooks
182         bool isplayer = (this.isplayermodel & ISPLAYER_CLIENT);
183         noref bool islocalplayer = (this.entnum == player_localnum + 1);
184         noref bool isnolocalplayer = (isplayer && (this.entnum != player_localnum + 1));
185
186         // we don't do this for the local player as that one is already handled
187         // by CSQCPlayer_SetCamera()
188         if (!CSQCPlayer_IsLocalPlayer(this)) InterpolateOrigin_Do(this);
189
190         CSQCModel_InterpolateAnimation_Do(this);
191
192         CSQCModel_Hook_PreDraw(this, isplayer);
193
194         if(isplayer)
195         {
196                 if(this.entnum == player_localentnum)
197                         this.renderflags |= RF_EXTERNALMODEL;
198                 else
199                         this.renderflags &= ~RF_EXTERNALMODEL;
200         }
201
202         // inherit draw flags easily
203         entity root = this;
204         while(root.tag_entity)
205                 root = root.tag_entity;
206         if(this != root)
207         {
208                 this.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
209                 this.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
210         }
211
212         // we're drawn, now teleporting is over
213         this.csqcmodel_teleported = 0;
214 }
215
216 entity CSQCModel_players[255]; // 255 is engine limit on maxclients
217
218 void CSQCModel_remove(entity this)
219 {
220         CSQCModel_players[this.entnum - 1] = NULL;
221 }
222
223 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
224 {
225         int sf = ReadInt24_t();
226         int psf = ReadByte();
227
228         // some nice flags for CSQCMODEL_IF and the hooks
229         bool isplayer = (psf & ISPLAYER_CLIENT) || (this.entnum >= 1 && this.entnum <= maxclients);
230         if (isnew && isplayer)
231         {
232                 CSQCModel_players[this.entnum - 1] = this;
233                 this.entremove = CSQCModel_remove;
234         }
235         bool islocalplayer = (this.entnum == player_localnum + 1);
236         noref bool isnolocalplayer = (isplayer && !islocalplayer);
237
238         this.isplayermodel = BITSET(this.isplayermodel, ISPLAYER_CLIENT, isplayer);
239         this.isplayermodel = BITSET(this.isplayermodel, ISPLAYER_LOCAL, islocalplayer);
240         this.isplayermodel = BITSET(this.isplayermodel, ISPLAYER_PLAYER, (psf & ISPLAYER_PLAYER));
241
242         this.classname = "csqcmodel";
243         this.iflags |= IFLAG_ORIGIN; // interpolate origin too
244         this.iflags |= IFLAG_ANGLES; // interpolate angles too
245         this.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
246
247         CSQCModel_Hook_PreUpdate(this, isnew, isplayer, islocalplayer);
248
249         CSQCPlayer_PreUpdate(this);
250         InterpolateOrigin_Undo(this);
251         CSQCModel_InterpolateAnimation_PreNote(this, sf);
252
253 #define CSQCMODEL_IF(cond) if(cond) {
254 #define CSQCMODEL_ENDIF }
255 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
256         if(sf & flag) \
257                 this.f = r();
258 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
259         if(sf & flag) \
260                 this.f = (r() + mi) / s;
261         ALLPROPERTIES
262 #undef CSQCMODEL_PROPERTY_SCALED
263 #undef CSQCMODEL_PROPERTY
264 #undef CSQCMODEL_ENDIF
265 #undef CSQCMODEL_IF
266
267         if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
268         {
269                 vector pmin = this.mins, pmax = this.maxs;
270                 setmodelindex(this, this.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
271                 setsize(this, pmin, pmax);
272         }
273
274         if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
275         {
276                 this.iflags |= IFLAG_TELEPORTED;
277                 this.csqcmodel_teleported = 1;
278         }
279
280         if(sf & BIT(3))
281                 this.alpha = this.m_alpha;
282
283         if(sf & BIT(14))
284                 viewloc_SetTags(this);
285
286         CSQCModel_InterpolateAnimation_Note(this, sf);
287         InterpolateOrigin_Note(this);
288         CSQCPlayer_PostUpdate(this);
289
290         CSQCModel_Hook_PostUpdate(this, isnew, isplayer, islocalplayer);
291
292 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
293         InterpolateOrigin_Do(this);
294         CSQCModel_InterpolateAnimation_Do(this);
295 #endif
296
297         // relink
298         setorigin(this, this.origin);
299
300         // set obvious render flags
301         if(this.entnum == player_localentnum)
302                 this.renderflags |= RF_EXTERNALMODEL;
303         else
304                 this.renderflags &= ~RF_EXTERNALMODEL;
305
306         // draw it
307         this.drawmask = MASK_NORMAL;
308         setpredraw(this, CSQCModel_Draw);
309         return true;
310 }
311
312 /**
313  * @param i zero indexed player
314  */
315 entity CSQCModel_server2csqc(int i)
316 {
317         if (i < maxclients) return CSQCModel_players[i];
318         ++i;
319         LOG_DEBUGF("player out of bounds: %d", i);
320         return findfloat(NULL, entnum, i);
321 }