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