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