]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/cl_model.qc
Merge branch 'terencehill/strzone_fixes' into 'master'
[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(this)) InterpolateOrigin_Do(this);
192
193         CSQCModel_InterpolateAnimation_Do();
194
195         { CSQCMODEL_HOOK_PREDRAW }
196
197         // inherit draw flags easily
198         entity root = self;
199         while(root.tag_entity)
200                 root = root.tag_entity;
201         if(self != root)
202         {
203                 self.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
204                 self.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
205         }
206
207         // we're drawn, now teleporting is over
208         self.csqcmodel_teleported = 0;
209 }
210
211 entity CSQCModel_players[255]; // 255 is engine limit on maxclients
212
213 void CSQCModel_remove()
214 {
215         SELFPARAM();
216         CSQCModel_players[this.entnum - 1] = NULL;
217 }
218
219 NET_HANDLE(ENT_CLIENT_MODEL, bool isnew)
220 {
221         int sf = ReadInt24_t();
222
223         // some nice flags for CSQCMODEL_IF and the hooks
224         bool isplayer = (this.entnum >= 1 && this.entnum <= maxclients);
225         if (isnew && isplayer)
226         {
227                 CSQCModel_players[this.entnum - 1] = this;
228                 this.entremove = CSQCModel_remove;
229         }
230         bool islocalplayer = (this.entnum == player_localnum + 1);
231         noref bool isnolocalplayer = (isplayer && (this.entnum != player_localnum + 1));
232
233         this.classname = "csqcmodel";
234         this.iflags |= IFLAG_ORIGIN; // interpolate origin too
235         this.iflags |= IFLAG_ANGLES; // interpolate angles too
236         this.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
237
238         { CSQCMODEL_HOOK_PREUPDATE }
239
240         CSQCPlayer_PreUpdate(this);
241         InterpolateOrigin_Undo(this);
242         CSQCModel_InterpolateAnimation_PreNote(sf);
243
244 #define CSQCMODEL_IF(cond) if(cond) {
245 #define CSQCMODEL_ENDIF }
246 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
247         if(sf & flag) \
248                 this.f = r();
249 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
250         if(sf & flag) \
251                 this.f = (r() + mi) / s;
252         ALLPROPERTIES
253 #undef CSQCMODEL_PROPERTY_SCALED
254 #undef CSQCMODEL_PROPERTY
255 #undef CSQCMODEL_ENDIF
256 #undef CSQCMODEL_IF
257
258         if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
259         {
260                 vector pmin = this.mins, pmax = this.maxs;
261                 setmodelindex(this, this.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
262                 setsize(this, pmin, pmax);
263         }
264
265         if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
266         {
267                 this.iflags |= IFLAG_TELEPORTED;
268                 this.csqcmodel_teleported = 1;
269         }
270
271         CSQCModel_InterpolateAnimation_Note(sf);
272         InterpolateOrigin_Note(this);
273         CSQCPlayer_PostUpdate(this);
274
275         { CSQCMODEL_HOOK_POSTUPDATE }
276
277 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
278         InterpolateOrigin_Do(this);
279         CSQCModel_InterpolateAnimation_Do();
280 #endif
281
282         // relink
283         setorigin(this, this.origin);
284
285         // set obvious render flags
286         if(this.entnum == player_localentnum)
287                 this.renderflags |= RF_EXTERNALMODEL;
288         else
289                 this.renderflags &= ~RF_EXTERNALMODEL;
290
291         // draw it
292         this.drawmask = MASK_NORMAL;
293         this.predraw = CSQCModel_Draw;
294         return true;
295 }
296
297 /**
298  * @param i zero indexed player
299  */
300 entity CSQCModel_server2csqc(int i)
301 {
302         if (i < maxclients) return CSQCModel_players[i];
303         ++i;
304         LOG_WARNINGF("player out of bounds: %d\n", i);
305         return findfloat(NULL, entnum, i);
306 }