2057c881e53bacef2128325dffb6a76be2c5ec5c
[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 entity CSQCModel_players[255]; // 255 is engine limit on maxclients
213
214 void CSQCModel_remove()
215 {
216         SELFPARAM();
217         CSQCModel_players[this.entnum - 1] = NULL;
218 }
219
220 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
221 {
222         int sf = ReadInt24_t();
223
224         // some nice flags for CSQCMODEL_IF and the hooks
225         bool isplayer = (this.entnum >= 1 && this.entnum <= maxclients);
226         if (isnew && isplayer)
227         {
228                 CSQCModel_players[this.entnum - 1] = this;
229                 this.entremove = CSQCModel_remove;
230         }
231         bool islocalplayer = (this.entnum == player_localnum + 1);
232         noref bool isnolocalplayer = (isplayer && (this.entnum != player_localnum + 1));
233
234         this.classname = "csqcmodel";
235         this.iflags |= IFLAG_ORIGIN; // interpolate origin too
236         this.iflags |= IFLAG_ANGLES; // interpolate angles too
237         this.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
238
239         { CSQCMODEL_HOOK_PREUPDATE }
240
241         CSQCPlayer_PreUpdate();
242         InterpolateOrigin_Undo();
243         CSQCModel_InterpolateAnimation_PreNote(sf);
244
245 #define CSQCMODEL_IF(cond) if(cond) {
246 #define CSQCMODEL_ENDIF }
247 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
248         if(sf & flag) \
249                 this.f = r();
250 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
251         if(sf & flag) \
252                 this.f = (r() + mi) / s;
253         ALLPROPERTIES
254 #undef CSQCMODEL_PROPERTY_SCALED
255 #undef CSQCMODEL_PROPERTY
256 #undef CSQCMODEL_ENDIF
257 #undef CSQCMODEL_IF
258
259         if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
260         {
261                 vector pmin = this.mins, pmax = this.maxs;
262                 setmodelindex(this, this.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
263                 setsize(this, pmin, pmax);
264         }
265
266         if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
267         {
268                 this.iflags |= IFLAG_TELEPORTED;
269                 this.csqcmodel_teleported = 1;
270         }
271
272         CSQCModel_InterpolateAnimation_Note(sf);
273         InterpolateOrigin_Note();
274         CSQCPlayer_PostUpdate();
275
276         { CSQCMODEL_HOOK_POSTUPDATE }
277
278 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
279         InterpolateOrigin_Do();
280         CSQCModel_InterpolateAnimation_Do();
281 #endif
282
283         // relink
284         setorigin(this, this.origin);
285
286         // set obvious render flags
287         if(this.entnum == player_localentnum)
288                 this.renderflags |= RF_EXTERNALMODEL;
289         else
290                 this.renderflags &= ~RF_EXTERNALMODEL;
291
292         // draw it
293         this.drawmask = MASK_NORMAL;
294         this.predraw = CSQCModel_Draw;
295         return true;
296 }
297
298 entity CSQCModel_server2csqc(int pl)
299 {
300         if (pl <= maxclients) return CSQCModel_players[pl - 1];
301         LOG_WARNINGF("player out of bounds: %d\n", pl);
302         return findfloat(NULL, entnum, pl);
303 }