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 <common/csqcmodel_settings.qh>
29 float autocvar_cl_lerpanim_maxdelta_framegroups = 0.1;
30 float autocvar_cl_nolerp = 0;
32 .float csqcmodel_lerpfrac;
33 .float csqcmodel_lerpfrac2;
34 .float csqcmodel_lerpfractime;
35 .float csqcmodel_lerpfrac2time;
37 void CSQCModel_InterpolateAnimation_2To4_PreNote(entity this, int sf)
39 if(sf & CSQCMODEL_PROPERTY_FRAME)
41 this.frame3 = this.frame;
42 this.frame3time = this.frame1time;
44 if(sf & CSQCMODEL_PROPERTY_FRAME2)
46 this.frame4 = this.frame2;
47 this.frame4time = this.frame2time;
49 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
51 this.csqcmodel_lerpfrac2 = this.csqcmodel_lerpfrac;
52 this.csqcmodel_lerpfrac2time = this.csqcmodel_lerpfractime;
53 this.lerpfrac = this.csqcmodel_lerpfrac;
56 void CSQCModel_InterpolateAnimation_1To2_PreNote(entity this, int sf)
58 if(sf & CSQCMODEL_PROPERTY_FRAME)
60 this.frame2 = this.frame;
61 this.frame2time = this.frame1time;
64 void CSQCModel_InterpolateAnimation_PreNote(entity this, int sf)
66 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
67 CSQCModel_InterpolateAnimation_2To4_PreNote(this, sf);
69 CSQCModel_InterpolateAnimation_1To2_PreNote(this, sf);
73 void CSQCModel_InterpolateAnimation_2To4_Note(entity this, int sf, bool set_times)
75 if(sf & CSQCMODEL_PROPERTY_FRAME)
78 this.frame1time = time;
80 if(sf & CSQCMODEL_PROPERTY_FRAME2)
83 this.frame2time = time;
85 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
87 this.csqcmodel_lerpfrac = this.lerpfrac;
89 this.csqcmodel_lerpfractime = time;
92 void CSQCModel_InterpolateAnimation_1To2_Note(entity this, int sf, bool set_times)
94 if(sf & CSQCMODEL_PROPERTY_FRAME)
97 this.frame1time = time;
100 void CSQCModel_InterpolateAnimation_Note(entity this, int sf)
102 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
103 CSQCModel_InterpolateAnimation_2To4_Note(this, sf, true);
105 CSQCModel_InterpolateAnimation_1To2_Note(this, sf, true);
109 void CSQCModel_InterpolateAnimation_2To4_Do(entity this)
111 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
113 this.lerpfrac = this.csqcmodel_lerpfrac;
122 if(this.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
125 l13 = bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
127 if(this.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
130 l24 = bound(0, (time - this.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
132 if(this.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
135 llf = bound(0, (time - this.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
137 l24_13 = this.csqcmodel_lerpfrac * llf + this.csqcmodel_lerpfrac2 * (1 - llf);
139 this.lerpfrac = l24 * l24_13;
140 this.lerpfrac4 = (1 - l24) * l24_13;
141 this.lerpfrac3 = (1 - l13) * (1 - l24_13);
143 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
149 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
156 void CSQCModel_InterpolateAnimation_1To2_Do(entity this)
158 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
164 if(this.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
167 this.lerpfrac = 1 - bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
170 void CSQCModel_InterpolateAnimation_Do(entity this)
172 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
173 CSQCModel_InterpolateAnimation_2To4_Do(this);
175 CSQCModel_InterpolateAnimation_1To2_Do(this);
179 void CSQCModel_Draw(entity this)
181 // some nice flags for CSQCMODEL_IF and the hooks
182 bool isplayer = (this.isplayermodel & ISPLAYER_CLIENT);
183 noref bool islocalplayer = (this.entnum == player_localnum + 1);
184 noref bool isnolocalplayer = (isplayer && (this.entnum != player_localnum + 1));
186 // we don't do this for the local player as that one is already handled
187 // by CSQCPlayer_SetCamera()
188 if (!CSQCPlayer_IsLocalPlayer(this)) InterpolateOrigin_Do(this);
190 CSQCModel_InterpolateAnimation_Do(this);
192 CSQCModel_Hook_PreDraw(this, isplayer);
196 if(this.entnum == player_localentnum)
197 this.renderflags |= RF_EXTERNALMODEL;
199 this.renderflags &= ~RF_EXTERNALMODEL;
202 // inherit draw flags easily
204 while(root.tag_entity)
205 root = root.tag_entity;
208 this.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
209 this.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
212 // we're drawn, now teleporting is over
213 this.csqcmodel_teleported = 0;
216 entity CSQCModel_players[255]; // 255 is engine limit on maxclients
218 void CSQCModel_remove(entity this)
220 CSQCModel_players[this.entnum - 1] = NULL;
223 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
225 int sf = ReadInt24_t();
226 int psf = ReadByte();
228 // some nice flags for CSQCMODEL_IF and the hooks
229 bool isplayer = (psf & ISPLAYER_CLIENT) || (this.entnum >= 1 && this.entnum <= maxclients);
230 if (isnew && isplayer)
232 CSQCModel_players[this.entnum - 1] = this;
233 this.entremove = CSQCModel_remove;
235 bool islocalplayer = (this.entnum == player_localnum + 1);
236 noref bool isnolocalplayer = (isplayer && !islocalplayer);
238 this.isplayermodel = BITSET(this.isplayermodel, ISPLAYER_CLIENT, isplayer);
239 this.isplayermodel = BITSET(this.isplayermodel, ISPLAYER_LOCAL, islocalplayer);
240 this.isplayermodel = BITSET(this.isplayermodel, ISPLAYER_PLAYER, (psf & ISPLAYER_PLAYER));
242 this.iflags |= IFLAG_ORIGIN; // interpolate origin too
243 this.iflags |= IFLAG_ANGLES; // interpolate angles too
244 this.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
246 CSQCModel_Hook_PreUpdate(this, isnew, isplayer, islocalplayer);
248 CSQCPlayer_PreUpdate(this);
249 InterpolateOrigin_Undo(this);
250 CSQCModel_InterpolateAnimation_PreNote(this, sf);
252 #define CSQCMODEL_IF(cond) if(cond) {
253 #define CSQCMODEL_ENDIF }
254 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
257 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
259 this.f = (r() + mi) / s;
261 #undef CSQCMODEL_PROPERTY_SCALED
262 #undef CSQCMODEL_PROPERTY
263 #undef CSQCMODEL_ENDIF
266 if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
268 vector pmin = this.mins, pmax = this.maxs;
269 setmodelindex(this, this.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
270 setsize(this, pmin, pmax);
273 if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
275 this.iflags |= IFLAG_TELEPORTED;
276 this.csqcmodel_teleported = 1;
280 this.alpha = this.m_alpha;
283 viewloc_SetTags(this);
285 CSQCModel_InterpolateAnimation_Note(this, sf);
286 InterpolateOrigin_Note(this);
287 CSQCPlayer_PostUpdate(this);
289 CSQCModel_Hook_PostUpdate(this, isnew, isplayer, islocalplayer);
291 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
292 InterpolateOrigin_Do(this);
293 CSQCModel_InterpolateAnimation_Do(this);
297 setorigin(this, this.origin);
299 // set obvious render flags
300 if(this.entnum == player_localentnum)
301 this.renderflags |= RF_EXTERNALMODEL;
303 this.renderflags &= ~RF_EXTERNALMODEL;
306 this.drawmask = MASK_NORMAL;
307 setpredraw(this, CSQCModel_Draw);
312 * @param i zero indexed player
314 entity CSQCModel_server2csqc(int i)
316 if (i < maxclients) return CSQCModel_players[i];
318 LOG_DEBUGF("player out of bounds: %d", i);
319 return findfloat(NULL, entnum, i);