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