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
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(entity this, int sf)
42 if(sf & CSQCMODEL_PROPERTY_FRAME)
44 this.frame3 = this.frame;
45 this.frame3time = this.frame1time;
47 if(sf & CSQCMODEL_PROPERTY_FRAME2)
49 this.frame4 = this.frame2;
50 this.frame4time = this.frame2time;
52 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
54 this.csqcmodel_lerpfrac2 = this.csqcmodel_lerpfrac;
55 this.csqcmodel_lerpfrac2time = this.csqcmodel_lerpfractime;
56 this.lerpfrac = this.csqcmodel_lerpfrac;
59 void CSQCModel_InterpolateAnimation_1To2_PreNote(entity this, int sf)
61 if(sf & CSQCMODEL_PROPERTY_FRAME)
63 this.frame2 = this.frame;
64 this.frame2time = this.frame1time;
67 void CSQCModel_InterpolateAnimation_PreNote(entity this, int sf)
69 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
70 CSQCModel_InterpolateAnimation_2To4_PreNote(this, sf);
72 CSQCModel_InterpolateAnimation_1To2_PreNote(this, sf);
76 void CSQCModel_InterpolateAnimation_2To4_Note(entity this, int sf, bool set_times)
78 if(sf & CSQCMODEL_PROPERTY_FRAME)
81 this.frame1time = time;
83 if(sf & CSQCMODEL_PROPERTY_FRAME2)
86 this.frame2time = time;
88 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
90 this.csqcmodel_lerpfrac = this.lerpfrac;
92 this.csqcmodel_lerpfractime = time;
95 void CSQCModel_InterpolateAnimation_1To2_Note(entity this, int sf, bool set_times)
97 if(sf & CSQCMODEL_PROPERTY_FRAME)
100 this.frame1time = time;
103 void CSQCModel_InterpolateAnimation_Note(entity this, int sf)
105 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
106 CSQCModel_InterpolateAnimation_2To4_Note(this, sf, true);
108 CSQCModel_InterpolateAnimation_1To2_Note(this, sf, true);
112 void CSQCModel_InterpolateAnimation_2To4_Do(entity this)
114 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
116 this.lerpfrac = this.csqcmodel_lerpfrac;
125 if(this.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
128 l13 = bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
130 if(this.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
133 l24 = bound(0, (time - this.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
135 if(this.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
138 llf = bound(0, (time - this.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
140 l24_13 = this.csqcmodel_lerpfrac * llf + this.csqcmodel_lerpfrac2 * (1 - llf);
142 this.lerpfrac = l24 * l24_13;
143 this.lerpfrac4 = (1 - l24) * l24_13;
144 this.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(entity this)
161 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
167 if(this.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
170 this.lerpfrac = 1 - bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
173 void CSQCModel_InterpolateAnimation_Do(entity this)
175 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
176 CSQCModel_InterpolateAnimation_2To4_Do(this);
178 CSQCModel_InterpolateAnimation_1To2_Do(this);
182 void CSQCModel_Draw(entity this)
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));
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(this);
195 CSQCModel_Hook_PreDraw(this, isplayer);
199 if(this.entnum == player_localentnum)
200 this.renderflags |= RF_EXTERNALMODEL;
202 this.renderflags &= ~RF_EXTERNALMODEL;
205 // inherit draw flags easily
207 while(root.tag_entity)
208 root = root.tag_entity;
211 this.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
212 this.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
215 // we're drawn, now teleporting is over
216 this.csqcmodel_teleported = 0;
219 entity CSQCModel_players[255]; // 255 is engine limit on maxclients
221 void CSQCModel_remove(entity this)
223 CSQCModel_players[this.entnum - 1] = NULL;
226 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
228 int sf = ReadInt24_t();
230 // some nice flags for CSQCMODEL_IF and the hooks
231 bool isplayer = ReadByte() || (this.entnum >= 1 && this.entnum <= maxclients);
232 if (isnew && isplayer)
234 CSQCModel_players[this.entnum - 1] = this;
235 this.entremove = CSQCModel_remove;
237 bool islocalplayer = (this.entnum == player_localnum + 1);
238 noref bool isnolocalplayer = (isplayer && !islocalplayer);
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
245 CSQCModel_Hook_PreUpdate(this, isnew, isplayer, islocalplayer);
247 CSQCPlayer_PreUpdate(this);
248 InterpolateOrigin_Undo(this);
249 CSQCModel_InterpolateAnimation_PreNote(this, sf);
251 #define CSQCMODEL_IF(cond) if(cond) {
252 #define CSQCMODEL_ENDIF }
253 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
256 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
258 this.f = (r() + mi) / s;
260 #undef CSQCMODEL_PROPERTY_SCALED
261 #undef CSQCMODEL_PROPERTY
262 #undef CSQCMODEL_ENDIF
265 if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
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);
272 if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
274 this.iflags |= IFLAG_TELEPORTED;
275 this.csqcmodel_teleported = 1;
278 CSQCModel_InterpolateAnimation_Note(this, sf);
279 InterpolateOrigin_Note(this);
280 CSQCPlayer_PostUpdate(this);
282 CSQCModel_Hook_PostUpdate(this, isnew, isplayer, islocalplayer);
284 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
285 InterpolateOrigin_Do(this);
286 CSQCModel_InterpolateAnimation_Do(this);
290 setorigin(this, this.origin);
292 // set obvious render flags
293 if(this.entnum == player_localentnum)
294 this.renderflags |= RF_EXTERNALMODEL;
296 this.renderflags &= ~RF_EXTERNALMODEL;
299 this.drawmask = MASK_NORMAL;
300 setpredraw(this, CSQCModel_Draw);
305 * @param i zero indexed player
307 entity CSQCModel_server2csqc(int i)
309 if (i < maxclients) return CSQCModel_players[i];
311 LOG_DEBUGF("player out of bounds: %d", i);
312 return findfloat(NULL, entnum, i);