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