2 * Copyright (c) 2011 Rudolf Polzer
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 var float autocvar_cl_lerpanim_maxdelta_framegroups = 0.1;
24 var float autocvar_cl_nolerp = 0;
26 .float csqcmodel_lerpfrac;
27 .float csqcmodel_lerpfrac2;
28 .float csqcmodel_lerpfractime;
29 .float csqcmodel_lerpfrac2time;
31 void CSQCModel_InterpolateAnimation_2To4_PreNote(float sf)
33 if(sf & CSQCMODEL_PROPERTY_FRAME)
35 self.frame3 = self.frame;
36 self.frame3time = self.frame1time;
38 if(sf & CSQCMODEL_PROPERTY_FRAME2)
40 self.frame4 = self.frame2;
41 self.frame4time = self.frame2time;
43 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
45 self.csqcmodel_lerpfrac2 = self.csqcmodel_lerpfrac;
46 self.csqcmodel_lerpfrac2time = self.csqcmodel_lerpfractime;
47 self.lerpfrac = self.csqcmodel_lerpfrac;
50 void CSQCModel_InterpolateAnimation_1To2_PreNote(float sf)
52 if(sf & CSQCMODEL_PROPERTY_FRAME)
54 self.frame2 = self.frame;
55 self.frame2time = self.frame1time;
58 void CSQCModel_InterpolateAnimation_PreNote(float sf)
60 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
61 CSQCModel_InterpolateAnimation_2To4_PreNote(sf);
63 CSQCModel_InterpolateAnimation_1To2_PreNote(sf);
67 void CSQCModel_InterpolateAnimation_2To4_Note(float sf, float set_times)
69 if(sf & CSQCMODEL_PROPERTY_FRAME)
72 self.frame1time = time;
74 if(sf & CSQCMODEL_PROPERTY_FRAME2)
77 self.frame2time = time;
79 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
81 self.csqcmodel_lerpfrac = self.lerpfrac;
83 self.csqcmodel_lerpfractime = time;
86 void CSQCModel_InterpolateAnimation_1To2_Note(float sf, float set_times)
88 if(sf & CSQCMODEL_PROPERTY_FRAME)
91 self.frame1time = time;
94 void CSQCModel_InterpolateAnimation_Note(float sf)
96 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
97 CSQCModel_InterpolateAnimation_2To4_Note(sf, TRUE);
99 CSQCModel_InterpolateAnimation_1To2_Note(sf, TRUE);
103 void CSQCModel_InterpolateAnimation_2To4_Do()
105 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
107 self.lerpfrac = self.csqcmodel_lerpfrac;
116 if(self.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
119 l13 = bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
121 if(self.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
124 l24 = bound(0, (time - self.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
126 if(self.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
129 llf = bound(0, (time - self.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
131 l24_13 = self.csqcmodel_lerpfrac * llf + self.csqcmodel_lerpfrac2 * (1 - llf);
133 self.lerpfrac = l24 * l24_13;
134 self.lerpfrac4 = (1 - l24) * l24_13;
135 self.lerpfrac3 = (1 - l13) * (1 - l24_13);
136 print(sprintf("Xframe=%d frame2=%d frame3=%d frame4=%d lerp=%f lerp3=%f lerp4=%f\n", self.frame, self.frame2, self.frame3, self.frame4, self.lerpfrac, self.lerpfrac3, self.lerpfrac4));
138 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
144 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
151 void CSQCModel_InterpolateAnimation_1To2_Do()
153 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
159 if(self.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
162 self.lerpfrac = 1 - bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
165 void CSQCModel_InterpolateAnimation_Do()
167 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
168 CSQCModel_InterpolateAnimation_2To4_Do();
170 CSQCModel_InterpolateAnimation_1To2_Do();
174 void CSQCModel_Draw()
176 // some nice flags for CSQCMODEL_IF and the hooks
177 local noref float isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
178 local noref float islocalplayer = (self.entnum == player_localnum + 1);
179 local noref float isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
181 // we don't do this for the local player as that one is already handled
182 // by CSQCPlayer_SetCamera()
183 if(!CSQCPlayer_IsLocalPlayer())
184 InterpolateOrigin_Do();
186 // TODO csqcplayers: run prediction here too
187 CSQCModel_InterpolateAnimation_Do();
189 { CSQCMODEL_HOOK_PREDRAW }
191 // inherit draw flags easily
193 while(root.tag_entity)
194 root = root.tag_entity;
197 self.renderflags &~= RF_EXTERNALMODEL | RF_VIEWMODEL;
198 self.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
201 // we're drawn, now teleporting is over
202 self.csqcmodel_teleported = 0;
205 void CSQCModel_Read(float isnew)
210 // some nice flags for CSQCMODEL_IF and the hooks
211 local noref float isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
212 local noref float islocalplayer = (self.entnum == player_localnum + 1);
213 local noref float isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
215 self.classname = "csqcmodel";
216 self.iflags |= IFLAG_ANGLES; // interpolate angles too
217 self.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
219 { CSQCMODEL_HOOK_PREUPDATE }
221 CSQCPlayer_PreUpdate();
222 InterpolateOrigin_Undo();
223 CSQCModel_InterpolateAnimation_PreNote(sf);
225 #define CSQCMODEL_IF(cond) if(cond) {
226 #define CSQCMODEL_ENDIF }
227 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
230 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
232 self.f = (r() + mi) / s;
234 #undef CSQCMODEL_PROPERTY_SCALED
235 #undef CSQCMODEL_PROPERTY
236 #undef CSQCMODEL_ENDIF
239 if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
240 setmodelindex(self, self.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
241 // FIXME do we WANT this to override mins/maxs?
243 if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
245 self.iflags |= IFLAG_TELEPORTED;
246 self.csqcmodel_teleported = 1;
249 CSQCModel_InterpolateAnimation_Note(sf);
250 InterpolateOrigin_Note();
251 CSQCPlayer_PostUpdate();
253 { CSQCMODEL_HOOK_POSTUPDATE }
255 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
256 InterpolateOrigin_Do();
257 CSQCModel_InterpolateAnimation_Do();
261 setorigin(self, self.origin);
263 // set obvious render flags
264 #ifdef COMPAT_XON050_ENGINE
265 if(self.entnum == player_localentnum || self.entnum == spectatee_status)
267 if(self.entnum == player_localentnum)
269 self.renderflags |= RF_EXTERNALMODEL;
271 self.renderflags &~= RF_EXTERNALMODEL;
274 self.drawmask = MASK_NORMAL;
275 self.predraw = CSQCModel_Draw;
278 entity CSQCModel_server2csqc(float pl)
280 return findfloat(world, entnum, pl); // FIXME optimize this using an array