]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/cl_model.qc
Merge branch 'TimePath/globalforces' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / csqcmodel / cl_model.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * IN THE SOFTWARE.
21  */
22 #include "cl_model.qh"
23
24 #include "cl_player.qh"
25 #include "common.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>
31
32 float autocvar_cl_lerpanim_maxdelta_framegroups = 0.1;
33 float autocvar_cl_nolerp = 0;
34
35 .float csqcmodel_lerpfrac;
36 .float csqcmodel_lerpfrac2;
37 .float csqcmodel_lerpfractime;
38 .float csqcmodel_lerpfrac2time;
39
40 void CSQCModel_InterpolateAnimation_2To4_PreNote(entity this, int sf)
41 {
42         if(sf & CSQCMODEL_PROPERTY_FRAME)
43         {
44                 this.frame3 = this.frame;
45                 this.frame3time = this.frame1time;
46         }
47         if(sf & CSQCMODEL_PROPERTY_FRAME2)
48         {
49                 this.frame4 = this.frame2;
50                 this.frame4time = this.frame2time;
51         }
52         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
53         {
54                 this.csqcmodel_lerpfrac2 = this.csqcmodel_lerpfrac;
55                 this.csqcmodel_lerpfrac2time = this.csqcmodel_lerpfractime;
56                 this.lerpfrac = this.csqcmodel_lerpfrac;
57         }
58 }
59 void CSQCModel_InterpolateAnimation_1To2_PreNote(entity this, int sf)
60 {
61         if(sf & CSQCMODEL_PROPERTY_FRAME)
62         {
63                 this.frame2 = this.frame;
64                 this.frame2time = this.frame1time;
65         }
66 }
67 void CSQCModel_InterpolateAnimation_PreNote(entity this, int sf)
68 {
69 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
70         CSQCModel_InterpolateAnimation_2To4_PreNote(this, sf);
71 #else
72         CSQCModel_InterpolateAnimation_1To2_PreNote(this, sf);
73 #endif
74 }
75
76 void CSQCModel_InterpolateAnimation_2To4_Note(entity this, int sf, bool set_times)
77 {
78         if(sf & CSQCMODEL_PROPERTY_FRAME)
79         {
80                 if(set_times)
81                         this.frame1time = time;
82         }
83         if(sf & CSQCMODEL_PROPERTY_FRAME2)
84         {
85                 if(set_times)
86                         this.frame2time = time;
87         }
88         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
89         {
90                 this.csqcmodel_lerpfrac = this.lerpfrac;
91                 if(set_times)
92                         this.csqcmodel_lerpfractime = time;
93         }
94 }
95 void CSQCModel_InterpolateAnimation_1To2_Note(entity this, int sf, bool set_times)
96 {
97         if(sf & CSQCMODEL_PROPERTY_FRAME)
98         {
99                 if(set_times)
100                         this.frame1time = time;
101         }
102 }
103 void CSQCModel_InterpolateAnimation_Note(entity this, int sf)
104 {
105 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
106         CSQCModel_InterpolateAnimation_2To4_Note(this, sf, true);
107 #else
108         CSQCModel_InterpolateAnimation_1To2_Note(this, sf, true);
109 #endif
110 }
111
112 void CSQCModel_InterpolateAnimation_2To4_Do(entity this)
113 {
114         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
115         {
116                 this.lerpfrac = this.csqcmodel_lerpfrac;
117                 this.lerpfrac3 = 0;
118                 this.lerpfrac4 = 0;
119         }
120         else
121         {
122                 float l13, l24, llf;
123                 float l24_13;
124
125                 if(this.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
126                         l13 = 1;
127                 else
128                         l13 = bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
129
130                 if(this.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
131                         l24 = 1;
132                 else
133                         l24 = bound(0, (time - this.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
134
135                 if(this.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
136                         llf = 1;
137                 else
138                         llf = bound(0, (time - this.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
139
140                 l24_13 = this.csqcmodel_lerpfrac * llf + this.csqcmodel_lerpfrac2 * (1 - llf);
141
142                 this.lerpfrac  = l24 * l24_13;
143                 this.lerpfrac4 = (1 - l24) * l24_13;
144                 this.lerpfrac3 = (1 - l13) * (1 - l24_13);
145
146                 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
147                 {
148                         this.frame2time = 0;
149                         this.frame4time = 0;
150                 }
151
152                 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
153                 {
154                         this.frame1time = 0;
155                         this.frame3time = 0;
156                 }
157         }
158 }
159 void CSQCModel_InterpolateAnimation_1To2_Do(entity this)
160 {
161         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
162         {
163                 this.lerpfrac = 0;
164         }
165         else
166         {
167                 if(this.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
168                         this.lerpfrac = 0;
169                 else
170                         this.lerpfrac = 1 - bound(0, (time - this.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
171         }
172 }
173 void CSQCModel_InterpolateAnimation_Do(entity this)
174 {
175 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
176         CSQCModel_InterpolateAnimation_2To4_Do(this);
177 #else
178         CSQCModel_InterpolateAnimation_1To2_Do(this);
179 #endif
180 }
181
182 void CSQCModel_Draw(entity this)
183 {
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));
188
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);
192
193         CSQCModel_InterpolateAnimation_Do(this);
194
195         CSQCModel_Hook_PreDraw(this, isplayer);
196
197         // inherit draw flags easily
198         entity root = this;
199         while(root.tag_entity)
200                 root = root.tag_entity;
201         if(this != root)
202         {
203                 this.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
204                 this.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
205         }
206
207         // we're drawn, now teleporting is over
208         this.csqcmodel_teleported = 0;
209 }
210
211 entity CSQCModel_players[255]; // 255 is engine limit on maxclients
212
213 void CSQCModel_remove(entity this)
214 {
215         CSQCModel_players[this.entnum - 1] = NULL;
216 }
217
218 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
219 {
220         int sf = ReadInt24_t();
221
222         // some nice flags for CSQCMODEL_IF and the hooks
223         bool isplayer = ReadByte() || (this.entnum >= 1 && this.entnum <= maxclients);
224         if (isnew && isplayer)
225         {
226                 CSQCModel_players[this.entnum - 1] = this;
227                 this.entremove = CSQCModel_remove;
228         }
229         bool islocalplayer = (this.entnum == player_localnum + 1);
230         noref bool isnolocalplayer = (isplayer && !islocalplayer);
231
232         this.classname = "csqcmodel";
233         this.iflags |= IFLAG_ORIGIN; // interpolate origin too
234         this.iflags |= IFLAG_ANGLES; // interpolate angles too
235         this.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
236
237         CSQCModel_Hook_PreUpdate(this, isnew, isplayer, islocalplayer);
238
239         CSQCPlayer_PreUpdate(this);
240         InterpolateOrigin_Undo(this);
241         CSQCModel_InterpolateAnimation_PreNote(this, sf);
242
243 #define CSQCMODEL_IF(cond) if(cond) {
244 #define CSQCMODEL_ENDIF }
245 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
246         if(sf & flag) \
247                 this.f = r();
248 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
249         if(sf & flag) \
250                 this.f = (r() + mi) / s;
251         ALLPROPERTIES
252 #undef CSQCMODEL_PROPERTY_SCALED
253 #undef CSQCMODEL_PROPERTY
254 #undef CSQCMODEL_ENDIF
255 #undef CSQCMODEL_IF
256
257         if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
258         {
259                 vector pmin = this.mins, pmax = this.maxs;
260                 setmodelindex(this, this.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
261                 setsize(this, pmin, pmax);
262         }
263
264         if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
265         {
266                 this.iflags |= IFLAG_TELEPORTED;
267                 this.csqcmodel_teleported = 1;
268         }
269
270         CSQCModel_InterpolateAnimation_Note(this, sf);
271         InterpolateOrigin_Note(this);
272         CSQCPlayer_PostUpdate(this);
273
274         CSQCModel_Hook_PostUpdate(this, isnew, isplayer, islocalplayer);
275
276 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
277         InterpolateOrigin_Do(this);
278         CSQCModel_InterpolateAnimation_Do(this);
279 #endif
280
281         // relink
282         setorigin(this, this.origin);
283
284         // set obvious render flags
285         if(this.entnum == player_localentnum)
286                 this.renderflags |= RF_EXTERNALMODEL;
287         else
288                 this.renderflags &= ~RF_EXTERNALMODEL;
289
290         // draw it
291         this.drawmask = MASK_NORMAL;
292         setpredraw(this, CSQCModel_Draw);
293         return true;
294 }
295
296 /**
297  * @param i zero indexed player
298  */
299 entity CSQCModel_server2csqc(int i)
300 {
301         if (i < maxclients) return CSQCModel_players[i];
302         ++i;
303         LOG_DEBUGF("player out of bounds: %d", i);
304         return findfloat(NULL, entnum, i);
305 }