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