]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/cl_model.qc
Cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / csqcmodel / cl_model.qc
1 /*
2  * Copyright (c) 2011 Rudolf PolzerCSQCModel_InterpolateAnimation_2To4_PreNote
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(int sf)
41 {SELFPARAM();
42         if(sf & CSQCMODEL_PROPERTY_FRAME)
43         {
44                 self.frame3 = self.frame;
45                 self.frame3time = self.frame1time;
46         }
47         if(sf & CSQCMODEL_PROPERTY_FRAME2)
48         {
49                 self.frame4 = self.frame2;
50                 self.frame4time = self.frame2time;
51         }
52         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
53         {
54                 self.csqcmodel_lerpfrac2 = self.csqcmodel_lerpfrac;
55                 self.csqcmodel_lerpfrac2time = self.csqcmodel_lerpfractime;
56                 self.lerpfrac = self.csqcmodel_lerpfrac;
57         }
58 }
59 void CSQCModel_InterpolateAnimation_1To2_PreNote(int sf)
60 {SELFPARAM();
61         if(sf & CSQCMODEL_PROPERTY_FRAME)
62         {
63                 self.frame2 = self.frame;
64                 self.frame2time = self.frame1time;
65         }
66 }
67 void CSQCModel_InterpolateAnimation_PreNote(int sf)
68 {
69 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
70         CSQCModel_InterpolateAnimation_2To4_PreNote(sf);
71 #else
72         CSQCModel_InterpolateAnimation_1To2_PreNote(sf);
73 #endif
74 }
75
76 void CSQCModel_InterpolateAnimation_2To4_Note(int sf, float set_times)
77 {SELFPARAM();
78         if(sf & CSQCMODEL_PROPERTY_FRAME)
79         {
80                 if(set_times)
81                         self.frame1time = time;
82         }
83         if(sf & CSQCMODEL_PROPERTY_FRAME2)
84         {
85                 if(set_times)
86                         self.frame2time = time;
87         }
88         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
89         {
90                 self.csqcmodel_lerpfrac = self.lerpfrac;
91                 if(set_times)
92                         self.csqcmodel_lerpfractime = time;
93         }
94 }
95 void CSQCModel_InterpolateAnimation_1To2_Note(int sf, float set_times)
96 {SELFPARAM();
97         if(sf & CSQCMODEL_PROPERTY_FRAME)
98         {
99                 if(set_times)
100                         self.frame1time = time;
101         }
102 }
103 void CSQCModel_InterpolateAnimation_Note(int sf)
104 {
105 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
106         CSQCModel_InterpolateAnimation_2To4_Note(sf, true);
107 #else
108         CSQCModel_InterpolateAnimation_1To2_Note(sf, true);
109 #endif
110 }
111
112 void CSQCModel_InterpolateAnimation_2To4_Do()
113 {SELFPARAM();
114         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
115         {
116                 self.lerpfrac = self.csqcmodel_lerpfrac;
117                 self.lerpfrac3 = 0;
118                 self.lerpfrac4 = 0;
119         }
120         else
121         {
122                 float l13, l24, llf;
123                 float l24_13;
124
125                 if(self.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
126                         l13 = 1;
127                 else
128                         l13 = bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
129
130                 if(self.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
131                         l24 = 1;
132                 else
133                         l24 = bound(0, (time - self.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
134
135                 if(self.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 - self.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
139
140                 l24_13 = self.csqcmodel_lerpfrac * llf + self.csqcmodel_lerpfrac2 * (1 - llf);
141
142                 self.lerpfrac  = l24 * l24_13;
143                 self.lerpfrac4 = (1 - l24) * l24_13;
144                 self.lerpfrac3 = (1 - l13) * (1 - l24_13);
145
146                 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
147                 {
148                         self.frame2time = 0;
149                         self.frame4time = 0;
150                 }
151
152                 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
153                 {
154                         self.frame1time = 0;
155                         self.frame3time = 0;
156                 }
157         }
158 }
159 void CSQCModel_InterpolateAnimation_1To2_Do()
160 {SELFPARAM();
161         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
162         {
163                 self.lerpfrac = 0;
164         }
165         else
166         {
167                 if(self.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
168                         self.lerpfrac = 0;
169                 else
170                         self.lerpfrac = 1 - bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
171         }
172 }
173 void CSQCModel_InterpolateAnimation_Do()
174 {
175 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
176         CSQCModel_InterpolateAnimation_2To4_Do();
177 #else
178         CSQCModel_InterpolateAnimation_1To2_Do();
179 #endif
180 }
181
182 void CSQCModel_Draw()
183 {SELFPARAM();
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));
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())
192                 InterpolateOrigin_Do();
193
194         CSQCModel_InterpolateAnimation_Do();
195
196         { CSQCMODEL_HOOK_PREDRAW }
197
198         // inherit draw flags easily
199         entity root = self;
200         while(root.tag_entity)
201                 root = root.tag_entity;
202         if(self != root)
203         {
204                 self.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
205                 self.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
206         }
207
208         // we're drawn, now teleporting is over
209         self.csqcmodel_teleported = 0;
210 }
211
212 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
213 {
214         int sf = ReadInt24_t();
215
216         // some nice flags for CSQCMODEL_IF and the hooks
217         bool isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
218         bool islocalplayer = (self.entnum == player_localnum + 1);
219         noref bool isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
220
221         self.classname = "csqcmodel";
222         self.iflags |= IFLAG_ORIGIN; // interpolate origin too
223         self.iflags |= IFLAG_ANGLES; // interpolate angles too
224         self.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
225
226         { CSQCMODEL_HOOK_PREUPDATE }
227
228         CSQCPlayer_PreUpdate();
229         InterpolateOrigin_Undo();
230         CSQCModel_InterpolateAnimation_PreNote(sf);
231
232 #define CSQCMODEL_IF(cond) if(cond) {
233 #define CSQCMODEL_ENDIF }
234 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
235         if(sf & flag) \
236                 self.f = r();
237 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
238         if(sf & flag) \
239                 self.f = (r() + mi) / s;
240         ALLPROPERTIES
241 #undef CSQCMODEL_PROPERTY_SCALED
242 #undef CSQCMODEL_PROPERTY
243 #undef CSQCMODEL_ENDIF
244 #undef CSQCMODEL_IF
245
246         if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
247         {
248                 vector pmin = self.mins, pmax = self.maxs;
249                 setmodelindex(self, self.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
250                 setsize(self, pmin, pmax);
251         }
252
253         if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
254         {
255                 self.iflags |= IFLAG_TELEPORTED;
256                 self.csqcmodel_teleported = 1;
257         }
258
259         CSQCModel_InterpolateAnimation_Note(sf);
260         InterpolateOrigin_Note();
261         CSQCPlayer_PostUpdate();
262
263         { CSQCMODEL_HOOK_POSTUPDATE }
264
265 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
266         InterpolateOrigin_Do();
267         CSQCModel_InterpolateAnimation_Do();
268 #endif
269
270         // relink
271         setorigin(self, self.origin);
272
273         // set obvious render flags
274         if(self.entnum == player_localentnum)
275                 self.renderflags |= RF_EXTERNALMODEL;
276         else
277                 self.renderflags &= ~RF_EXTERNALMODEL;
278
279         // draw it
280         self.drawmask = MASK_NORMAL;
281         self.predraw = CSQCModel_Draw;
282         return true;
283 }
284
285 entity CSQCModel_server2csqc(float pl)
286 {
287         return findfloat(world, entnum, pl); // FIXME optimize this using an array
288 }