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_PreNote(float sf)
33 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
34 if(sf & CSQCMODEL_PROPERTY_FRAME)
36 self.frame3 = self.frame;
37 self.frame3time = self.frame1time;
39 if(sf & CSQCMODEL_PROPERTY_FRAME2)
41 self.frame4 = self.frame2;
42 self.frame4time = self.frame2time;
44 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
46 self.csqcmodel_lerpfrac2 = self.csqcmodel_lerpfrac;
47 self.csqcmodel_lerpfrac2time = self.csqcmodel_lerpfractime;
48 self.lerpfrac = self.csqcmodel_lerpfrac;
51 if(sf & CSQCMODEL_PROPERTY_FRAME)
53 self.frame2 = self.frame;
54 self.frame2time = self.frame1time;
59 void CSQCModel_InterpolateAnimation_Note(float sf)
61 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
62 if(sf & CSQCMODEL_PROPERTY_FRAME)
64 self.frame1time = time;
66 if(sf & CSQCMODEL_PROPERTY_FRAME2)
68 self.frame2time = time;
70 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
72 self.csqcmodel_lerpfrac = self.lerpfrac;
73 self.csqcmodel_lerpfractime = time;
76 if(sf & CSQCMODEL_PROPERTY_FRAME)
78 self.frame1time = time;
83 void CSQCModel_InterpolateAnimation_Do()
85 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
86 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
88 self.lerpfrac = self.csqcmodel_lerpfrac;
97 if(self.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
100 l13 = bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
102 if(self.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
105 l24 = bound(0, (time - self.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
107 if(self.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
110 llf = bound(0, (time - self.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
112 l24_13 = self.csqcmodel_lerpfrac * llf + self.csqcmodel_lerpfrac2 * (1 - llf);
114 self.lerpfrac = l24 * l24_13;
115 self.lerpfrac4 = (1 - l24) * l24_13;
116 self.lerpfrac3 = (1 - l13) * (1 - l24_13);
118 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
124 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
131 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
137 self.lerpfrac = 1 - bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
142 void CSQCModel_Draw()
144 // some nice flags for CSQCMODEL_IF and the hooks
145 float isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
146 float islocalplayer = (self.entnum == player_localnum + 1);
147 float isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
149 // we don't do this for the local player as that one is already handled
150 // by CSQCPlayer_SetCamera()
151 if(!CSQCPlayer_IsLocalPlayer())
152 InterpolateOrigin_Do();
154 // TODO csqcplayers: run prediction here too
155 CSQCModel_InterpolateAnimation_Do();
157 { CSQCMODEL_HOOK_PREDRAW }
159 // inherit draw flags easily
161 while(root.tag_entity)
162 root = root.tag_entity;
165 self.renderflags &~= RF_EXTERNALMODEL | RF_VIEWMODEL;
166 self.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
169 // we're drawn, now teleporting is over
170 self.csqcmodel_teleported = 0;
173 void CSQCModel_Read()
178 // some nice flags for CSQCMODEL_IF and the hooks
179 float isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
180 float islocalplayer = (self.entnum == player_localnum + 1);
181 float isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
183 self.iflags |= IFLAG_ANGLES; // interpolate angles too
185 { CSQCMODEL_HOOK_PREUPDATE }
187 CSQCPlayer_PreUpdate();
188 InterpolateOrigin_Undo();
189 CSQCModel_InterpolateAnimation_PreNote(sf);
191 #define CSQCMODEL_IF(cond) if(cond) {
192 #define CSQCMODEL_ENDIF }
193 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
196 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
200 #undef CSQCMODEL_PROPERTY_SCALED
201 #undef CSQCMODEL_PROPERTY
202 #undef CSQCMODEL_ENDIF
205 if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
206 setmodelindex(self, self.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
208 if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
210 self.iflags |= IFLAG_TELEPORTED;
211 self.csqcmodel_teleported = 1;
214 CSQCModel_InterpolateAnimation_Note(sf);
215 InterpolateOrigin_Note();
216 CSQCPlayer_PostUpdate();
218 { CSQCMODEL_HOOK_POSTUPDATE }
220 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
221 InterpolateOrigin_Do();
222 CSQCModel_InterpolateAnimation_Do();
226 setorigin(self, self.origin);
228 // set obvious render flags
229 #ifdef COMPAT_XON050_ENGINE
230 if(self.entnum == player_localentnum || self.entnum == spectatee_status)
232 if(self.entnum == player_localentnum)
234 self.renderflags |= RF_EXTERNALMODEL;
236 self.renderflags &~= RF_EXTERNALMODEL;
239 self.drawmask = MASK_NORMAL;
240 self.predraw = CSQCModel_Draw;
243 entity CSQCModel_server2csqc(float pl)
245 return findfloat(world, entnum, pl); // FIXME optimize this using an array