2 * Copyright (c) 2011 Rudolf PolzerCSQCModel_InterpolateAnimation_2To4_PreNote
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
22 #include "cl_model.qh"
24 #include "cl_player.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"
32 float autocvar_cl_lerpanim_maxdelta_framegroups = 0.1;
33 float autocvar_cl_nolerp = 0;
35 .float csqcmodel_lerpfrac;
36 .float csqcmodel_lerpfrac2;
37 .float csqcmodel_lerpfractime;
38 .float csqcmodel_lerpfrac2time;
40 void CSQCModel_InterpolateAnimation_2To4_PreNote(int sf)
42 if(sf & CSQCMODEL_PROPERTY_FRAME)
44 self.frame3 = self.frame;
45 self.frame3time = self.frame1time;
47 if(sf & CSQCMODEL_PROPERTY_FRAME2)
49 self.frame4 = self.frame2;
50 self.frame4time = self.frame2time;
52 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
54 self.csqcmodel_lerpfrac2 = self.csqcmodel_lerpfrac;
55 self.csqcmodel_lerpfrac2time = self.csqcmodel_lerpfractime;
56 self.lerpfrac = self.csqcmodel_lerpfrac;
59 void CSQCModel_InterpolateAnimation_1To2_PreNote(int sf)
61 if(sf & CSQCMODEL_PROPERTY_FRAME)
63 self.frame2 = self.frame;
64 self.frame2time = self.frame1time;
67 void CSQCModel_InterpolateAnimation_PreNote(int sf)
69 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
70 CSQCModel_InterpolateAnimation_2To4_PreNote(sf);
72 CSQCModel_InterpolateAnimation_1To2_PreNote(sf);
76 void CSQCModel_InterpolateAnimation_2To4_Note(int sf, float set_times)
78 if(sf & CSQCMODEL_PROPERTY_FRAME)
81 self.frame1time = time;
83 if(sf & CSQCMODEL_PROPERTY_FRAME2)
86 self.frame2time = time;
88 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
90 self.csqcmodel_lerpfrac = self.lerpfrac;
92 self.csqcmodel_lerpfractime = time;
95 void CSQCModel_InterpolateAnimation_1To2_Note(int sf, float set_times)
97 if(sf & CSQCMODEL_PROPERTY_FRAME)
100 self.frame1time = time;
103 void CSQCModel_InterpolateAnimation_Note(int sf)
105 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
106 CSQCModel_InterpolateAnimation_2To4_Note(sf, true);
108 CSQCModel_InterpolateAnimation_1To2_Note(sf, true);
112 void CSQCModel_InterpolateAnimation_2To4_Do()
114 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
116 self.lerpfrac = self.csqcmodel_lerpfrac;
125 if(self.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
128 l13 = bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
130 if(self.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
133 l24 = bound(0, (time - self.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
135 if(self.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
138 llf = bound(0, (time - self.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
140 l24_13 = self.csqcmodel_lerpfrac * llf + self.csqcmodel_lerpfrac2 * (1 - llf);
142 self.lerpfrac = l24 * l24_13;
143 self.lerpfrac4 = (1 - l24) * l24_13;
144 self.lerpfrac3 = (1 - l13) * (1 - l24_13);
146 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
152 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
159 void CSQCModel_InterpolateAnimation_1To2_Do()
161 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
167 if(self.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
170 self.lerpfrac = 1 - bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
173 void CSQCModel_InterpolateAnimation_Do()
175 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
176 CSQCModel_InterpolateAnimation_2To4_Do();
178 CSQCModel_InterpolateAnimation_1To2_Do();
182 void CSQCModel_Draw()
184 // some nice flags for CSQCMODEL_IF and the hooks
185 bool isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
186 noref bool islocalplayer = (self.entnum == player_localnum + 1);
187 noref bool isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
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);
193 CSQCModel_InterpolateAnimation_Do();
195 { CSQCMODEL_HOOK_PREDRAW }
197 // inherit draw flags easily
199 while(root.tag_entity)
200 root = root.tag_entity;
203 self.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
204 self.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
207 // we're drawn, now teleporting is over
208 self.csqcmodel_teleported = 0;
211 entity CSQCModel_players[255]; // 255 is engine limit on maxclients
213 void CSQCModel_remove()
216 CSQCModel_players[this.entnum - 1] = NULL;
219 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
221 int sf = ReadInt24_t();
223 // some nice flags for CSQCMODEL_IF and the hooks
224 bool isplayer = (this.entnum >= 1 && this.entnum <= maxclients);
225 if (isnew && isplayer)
227 CSQCModel_players[this.entnum - 1] = this;
228 this.entremove = CSQCModel_remove;
230 bool islocalplayer = (this.entnum == player_localnum + 1);
231 noref bool isnolocalplayer = (isplayer && (this.entnum != player_localnum + 1));
233 this.classname = "csqcmodel";
234 this.iflags |= IFLAG_ORIGIN; // interpolate origin too
235 this.iflags |= IFLAG_ANGLES; // interpolate angles too
236 this.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
238 { CSQCMODEL_HOOK_PREUPDATE }
240 CSQCPlayer_PreUpdate(this);
241 InterpolateOrigin_Undo(this);
242 CSQCModel_InterpolateAnimation_PreNote(sf);
244 #define CSQCMODEL_IF(cond) if(cond) {
245 #define CSQCMODEL_ENDIF }
246 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
249 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
251 this.f = (r() + mi) / s;
253 #undef CSQCMODEL_PROPERTY_SCALED
254 #undef CSQCMODEL_PROPERTY
255 #undef CSQCMODEL_ENDIF
258 if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
260 vector pmin = this.mins, pmax = this.maxs;
261 setmodelindex(this, this.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
262 setsize(this, pmin, pmax);
265 if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
267 this.iflags |= IFLAG_TELEPORTED;
268 this.csqcmodel_teleported = 1;
271 CSQCModel_InterpolateAnimation_Note(sf);
272 InterpolateOrigin_Note(this);
273 CSQCPlayer_PostUpdate(this);
275 { CSQCMODEL_HOOK_POSTUPDATE }
277 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
278 InterpolateOrigin_Do(this);
279 CSQCModel_InterpolateAnimation_Do();
283 setorigin(this, this.origin);
285 // set obvious render flags
286 if(this.entnum == player_localentnum)
287 this.renderflags |= RF_EXTERNALMODEL;
289 this.renderflags &= ~RF_EXTERNALMODEL;
292 this.drawmask = MASK_NORMAL;
293 this.predraw = CSQCModel_Draw;
298 * @param i zero indexed player
300 entity CSQCModel_server2csqc(int i)
302 if (i < maxclients) return CSQCModel_players[i];
304 LOG_DEBUGF("player out of bounds: %d\n", i);
305 return findfloat(NULL, entnum, i);