Declare more ints as ints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / csqcmodel_hooks.qc
1 void CSQCModel_Hook_PreDraw(bool isplayer);
2
3 .bool isplayermodel;
4
5 // FEATURE: LOD
6 .int lodmodelindex0;
7 .int lodmodelindex1;
8 .int lodmodelindex2;
9 void CSQCPlayer_LOD_Apply(void)
10 {
11         // LOD model loading
12         if(self.lodmodelindex0 != self.modelindex)
13         {
14                 string modelname = self.model;
15                 string s;
16
17                 vector mi = self.mins;
18                 vector ma = self.maxs;
19
20                 // set modelindex
21                 self.lodmodelindex0 = self.modelindex;
22                 self.lodmodelindex1 = self.modelindex;
23                 self.lodmodelindex2 = self.modelindex;
24
25                 // FIXME: this only supports 3-letter extensions
26                 s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod1", substring(modelname, -4, 4));
27                 if(fexists(s))
28                 {
29                         precache_model(s);
30                         setmodel(self, s);
31                         if(self.modelindex)
32                                 self.lodmodelindex1 = self.modelindex;
33                 }
34
35                 s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod2", substring(modelname, -4, 4));
36                 if(fexists(s))
37                 {
38                         precache_model(s);
39                         setmodel(self, s);
40                         if(self.modelindex)
41                                 self.lodmodelindex2 = self.modelindex;
42                 }
43
44                 setmodel(self, modelname); // make everything normal again
45                 setsize(self, mi, ma);
46         }
47
48         // apply LOD
49         if(autocvar_cl_playerdetailreduction <= 0)
50         {
51                 if(autocvar_cl_playerdetailreduction <= -2)
52                         self.modelindex = self.lodmodelindex2;
53                 else if(autocvar_cl_playerdetailreduction <= -1)
54                         self.modelindex = self.lodmodelindex1;
55                 else
56                         self.modelindex = self.lodmodelindex0;
57         }
58         else
59         {
60                 float distance = vlen(self.origin - view_origin);
61                 float f = (distance * current_viewzoom + 100.0) * autocvar_cl_playerdetailreduction;
62                 f *= 1.0 / bound(0.01, view_quality, 1);
63                 if(f > autocvar_cl_loddistance2)
64                         self.modelindex = self.lodmodelindex2;
65                 else if(f > autocvar_cl_loddistance1)
66                         self.modelindex = self.lodmodelindex1;
67                 else
68                         self.modelindex = self.lodmodelindex0;
69         }
70 }
71
72 // FEATURE: forcemodel and model color selection (MUST be called BEFORE LOD!)
73 string forceplayermodels_model;
74 bool forceplayermodels_modelisgoodmodel;
75 int forceplayermodels_modelindex;
76 int forceplayermodels_skin;
77
78 string forceplayermodels_mymodel;
79 bool forceplayermodels_myisgoodmodel;
80 int forceplayermodels_mymodelindex;
81
82 bool forceplayermodels_attempted;
83
84 .string forceplayermodels_savemodel;
85 .int forceplayermodels_savemodelindex;
86 .int forceplayermodels_saveskin;
87 .int forceplayermodels_savecolormap;
88
89 .string forceplayermodels_isgoodmodel_mdl;
90 .bool forceplayermodels_isgoodmodel;
91
92 string forceplayermodels_goodmodel;
93 int forceplayermodels_goodmodelindex;
94
95 .vector glowmod;
96 .vector old_glowmod;
97
98 void CSQCPlayer_ModelAppearance_PreUpdate(void)
99 {
100         self.model = self.forceplayermodels_savemodel;
101         self.modelindex = self.forceplayermodels_savemodelindex;
102         self.skin = self.forceplayermodels_saveskin;
103         self.colormap = self.forceplayermodels_savecolormap;
104 }
105 void CSQCPlayer_ModelAppearance_PostUpdate(void)
106 {
107         self.forceplayermodels_savemodel = self.model;
108         self.forceplayermodels_savemodelindex = self.modelindex;
109         self.forceplayermodels_saveskin = self.skin;
110         self.forceplayermodels_savecolormap = self.colormap;
111
112         if(self.forceplayermodels_savemodel != self.forceplayermodels_isgoodmodel_mdl)
113         {
114                 self.forceplayermodels_isgoodmodel = fexists(self.forceplayermodels_savemodel);
115                 self.forceplayermodels_isgoodmodel_mdl = self.forceplayermodels_savemodel;
116                 if(!self.forceplayermodels_isgoodmodel)
117                         printf("Warning: missing model %s has been used\n", self.forceplayermodels_savemodel);
118         }
119 }
120 void CSQCPlayer_ModelAppearance_Apply(bool islocalplayer)
121 {
122         // FORCEMODEL
123         // which one is ALWAYS good?
124         if (!forceplayermodels_goodmodel)
125         {
126                 entity e;
127                 e = spawn();
128                 precache_model(cvar_defstring("_cl_playermodel"));
129                 setmodel(e, cvar_defstring("_cl_playermodel"));
130                 forceplayermodels_goodmodel = e.model;
131                 forceplayermodels_goodmodelindex = e.modelindex;
132                 remove(e);
133         }
134
135         // first, try finding it from the server
136         if(self.forceplayermodels_savemodelindex && self.forceplayermodels_savemodel != "null")
137         {
138                 if(islocalplayer)
139                 {
140                         if(!isdemo()) // this is mainly cheat protection; not needed for demos
141                         {
142                                 // trust server's idea of "own player model"
143                                 forceplayermodels_modelisgoodmodel = self.forceplayermodels_isgoodmodel;
144                                 forceplayermodels_model = self.forceplayermodels_savemodel;
145                                 forceplayermodels_modelindex = self.forceplayermodels_savemodelindex;
146                                 forceplayermodels_skin = self.forceplayermodels_saveskin;
147                                 forceplayermodels_attempted = 1;
148                         }
149                 }
150         }
151
152         // forcemodel finding
153         if(!forceplayermodels_attempted)
154         {
155                 forceplayermodels_attempted = 1;
156
157                 // only if this failed, find it out on our own
158                 entity e;
159                 e = spawn();
160                 setmodel(e, autocvar__cl_playermodel); // this is harmless, see below
161                 forceplayermodels_modelisgoodmodel = fexists(e.model);
162                 forceplayermodels_model = e.model;
163                 forceplayermodels_modelindex = e.modelindex;
164                 forceplayermodels_skin = autocvar__cl_playerskin;
165                 remove(e);
166         }
167
168         if(autocvar_cl_forcemyplayermodel != "" && autocvar_cl_forcemyplayermodel != forceplayermodels_mymodel)
169         {
170                 entity e;
171                 e = spawn();
172                 setmodel(e, autocvar_cl_forcemyplayermodel); // this is harmless, see below
173                 forceplayermodels_myisgoodmodel = fexists(e.model);
174                 forceplayermodels_mymodel = e.model;
175                 forceplayermodels_mymodelindex = e.modelindex;
176                 remove(e);
177         }
178
179         // apply it
180         bool isfriend;
181         int cm;
182         cm = self.forceplayermodels_savecolormap;
183         cm = (cm >= 1024) ? cm : (stof(getplayerkeyvalue(cm - 1, "colors")) + 1024);
184
185         if(teamplay)
186                 isfriend = (cm == 1024 + 17 * myteam);
187         else
188                 isfriend = islocalplayer;
189
190         if(autocvar_cl_forcemyplayermodel != "" && forceplayermodels_myisgoodmodel && isfriend)
191         {
192                 self.model = forceplayermodels_mymodel;
193                 self.modelindex = forceplayermodels_mymodelindex;
194                 self.skin = autocvar_cl_forcemyplayerskin;
195         }
196         else if(autocvar_cl_forceplayermodels && forceplayermodels_modelisgoodmodel)
197         {
198                 self.model = forceplayermodels_model;
199                 self.modelindex = forceplayermodels_modelindex;
200                 self.skin = forceplayermodels_skin;
201         }
202         else if(self.forceplayermodels_isgoodmodel)
203         {
204                 self.model = self.forceplayermodels_savemodel;
205                 self.modelindex = self.forceplayermodels_savemodelindex;
206                 self.skin = self.forceplayermodels_saveskin;
207         }
208         else
209         {
210                 self.model = forceplayermodels_goodmodel;
211                 self.modelindex = forceplayermodels_goodmodelindex;
212                 self.skin = self.forceplayermodels_saveskin;
213         }
214
215         // forceplayercolors too
216         if(teamplay)
217         {
218                 // own team's color is never forced
219                 int forcecolor_friend = 0;
220                 int forcecolor_enemy = 0;
221                 entity tm;
222
223                 if(autocvar_cl_forcemyplayercolors)
224                         forcecolor_friend = 1024 + autocvar_cl_forcemyplayercolors;
225                 if(autocvar_cl_forceplayercolors && team_count == 2)
226                         forcecolor_enemy = 1024 + autocvar__cl_color;
227
228                 if(forcecolor_enemy && !forcecolor_friend)
229                 {
230                         // only enemy color is forced?
231                         // verify it is not equal to the friend color
232                         if(forcecolor_enemy == 1024 + 17 * myteam)
233                                 forcecolor_enemy = 0;
234                 }
235
236                 if(forcecolor_friend && !forcecolor_enemy)
237                 {
238                         // only friend color is forced?
239                         // verify it is not equal to the enemy color
240                         for(tm = teams.sort_next; tm; tm = tm.sort_next)
241                                 // note: we even compare against our own team.
242                                 // if we rejected because we matched our OWN team color,
243                                 // this is not bad; we then simply keep our color as is
244                                 // anyway.
245                                 if(forcecolor_friend == 1024 + 17 * tm.team)
246                                         forcecolor_friend = 0;
247                 }
248
249                 if(cm == 1024 + 17 * myteam)
250                 {
251                         if(forcecolor_friend)
252                                 self.colormap = forcecolor_friend;
253                 }
254                 else
255                 {
256                         if(forcecolor_enemy)
257                                 self.colormap = forcecolor_enemy;
258                 }
259         }
260         else
261         {
262                 if(autocvar_cl_forcemyplayercolors && islocalplayer)
263                         self.colormap = 1024 + autocvar_cl_forcemyplayercolors;
264                 else if(autocvar_cl_forceplayercolors)
265                         self.colormap = player_localnum + 1;
266         }
267
268         // GLOWMOD AND DEATH FADING
269         if(self.colormap > 0)
270                 self.glowmod = colormapPaletteColor(((self.colormap >= 1024) ? self.colormap : stof(getplayerkeyvalue(self.colormap - 1, "colors"))) & 0x0F, true) * 2;
271         else
272                 self.glowmod = '1 1 1';
273
274         if(autocvar_cl_deathglow > 0)
275         {
276                 if(self.csqcmodel_isdead)
277                 {
278                         // Fade out to black now...
279                         if(self.old_glowmod == '0 0 0') { self.old_glowmod = self.glowmod; }
280                         self.colormap = 0;
281
282                         self.glowmod = self.old_glowmod * bound(0, 1 - (time - self.death_time) / autocvar_cl_deathglow, 1);
283                         self.glowmod_x = max(self.glowmod.x, 0.0001);
284                         self.glowmod_y = max(self.glowmod.y, 0.0001);
285                         self.glowmod_z = max(self.glowmod.z, 0.0001);
286                 }
287                 else if(self.old_glowmod != '0 0 0') { self.old_glowmod = '0 0 0'; }
288         }
289
290         //printf("CSQCPlayer_ModelAppearance_Apply(): state = %s, colormap = %f, glowmod = %s\n", (self.csqcmodel_isdead ? "DEAD" : "ALIVE"), self.colormap, vtos(self.glowmod));
291 }
292
293 // FEATURE: fallback frames
294 .int csqcmodel_saveframe;
295 .int csqcmodel_saveframe2;
296 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
297 .int csqcmodel_saveframe3;
298 .int csqcmodel_saveframe4;
299 #endif
300 .int csqcmodel_framecount;
301
302 #define IS_DEAD_FRAME(f) ((f) == 0 || (f) == 1)
303 void CSQCPlayer_FallbackFrame_PreUpdate(void)
304 {
305         self.frame = self.csqcmodel_saveframe;
306         self.frame2 = self.csqcmodel_saveframe2;
307 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
308         self.frame3 = self.csqcmodel_saveframe3;
309         self.frame4 = self.csqcmodel_saveframe4;
310 #endif
311 }
312 void CSQCPlayer_FallbackFrame_PostUpdate(bool isnew)
313 {
314         self.csqcmodel_saveframe = self.frame;
315         self.csqcmodel_saveframe2 = self.frame2;
316 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
317         self.csqcmodel_saveframe3 = self.frame3;
318         self.csqcmodel_saveframe4 = self.frame4;
319 #endif
320
321         // hack for death animations: set their frametime to zero in case a
322         // player "pops in"
323         if(isnew)
324         {
325 #define FIX_FRAMETIME(f,ft) do {                                                                                                                                                                        \
326                 if(IS_DEAD_FRAME(self.f) && self.ft != 0 && self.death_time != 0)                                                                                       \
327                         self.ft = self.death_time;                                                                                                                                                              \
328 } while(0)
329                 FIX_FRAMETIME(frame, frame1time);
330                 FIX_FRAMETIME(frame2, frame2time);
331 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
332                 FIX_FRAMETIME(frame3, frame3time);
333                 FIX_FRAMETIME(frame4, frame4time);
334 #endif
335         }
336         self.csqcmodel_isdead = IS_DEAD_FRAME(self.frame);
337 }
338 void CSQCPlayer_AnimDecide_PostUpdate(bool isnew)
339 {
340         self.csqcmodel_isdead = !!(self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
341 }
342 int CSQCPlayer_FallbackFrame(int f)
343 {
344         if(frameduration(self.modelindex, f) > 0)
345                 return f; // goooooood
346         if(frameduration(self.modelindex, 1) <= 0)
347                 return f; // this is a static model. We can't fix it if we wanted to
348         switch(f)
349         {
350                 case 23: return 11; // anim_melee -> anim_shoot
351                 case 24: return 4; // anim_duckwalkbackwards -> anim_duckwalk
352                 case 25: return 4; // anim_duckwalkstrafeleft -> anim_duckwalk
353                 case 26: return 4; // anim_duckwalkstraferight -> anim_duckwalk
354                 case 27: return 4; // anim_duckwalkforwardright -> anim_duckwalk
355                 case 28: return 4; // anim_duckwalkforwardleft -> anim_duckwalk
356                 case 29: return 4; // anim_duckwalkbackright -> anim_duckwalk
357                 case 30: return 4; // anim_duckwalkbackleft -> anim_duckwalk
358         }
359         printf("Frame %d missing in model %s, and we have no fallback - FAIL!\n", f, self.model);
360         return f;
361 }
362 void CSQCPlayer_FallbackFrame_Apply(void)
363 {
364         self.frame = CSQCPlayer_FallbackFrame(self.frame);
365         self.frame2 = CSQCPlayer_FallbackFrame(self.frame2);
366 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
367         self.frame3 = CSQCPlayer_FallbackFrame(self.frame3);
368         self.frame4 = CSQCPlayer_FallbackFrame(self.frame4);
369 #endif
370 }
371
372 // FEATURE: auto tag_index
373 .entity tag_entity;
374 .int tag_entity_lastmodelindex;
375 .int tag_index;
376 void CSQCModel_AutoTagIndex_Apply(void)
377 {
378         if(self.tag_entity && wasfreed(self.tag_entity))
379                 self.tag_entity = world;
380
381         if(self.tag_networkentity)
382         {
383                 // we are ATTACHED!
384                 bool changed = 0;
385                 if(self.tag_entity.entnum != self.tag_networkentity)
386                 {
387                         self.tag_entity = findfloat(world, entnum, self.tag_networkentity);
388                         changed = 1;
389                 }
390
391                 // recursive predraw call to fix issues with forcemodels and LOD if bone indexes mismatch
392                 if(self.tag_entity.classname == "csqcmodel")
393                 {
394                         entity oldself = self;
395                         self = self.tag_entity;
396                         CSQCModel_Hook_PreDraw((self.entnum >= 1 && self.entnum <= maxclients));
397                         self = oldself;
398                 }
399
400                 if(self.tag_entity.modelindex != self.tag_entity_lastmodelindex)
401                 {
402                         self.tag_entity_lastmodelindex = self.tag_entity.modelindex;
403                         changed = 1;
404                 }
405                 if(changed)
406                 {
407                         if(self.tag_entity)
408                         {
409                                 // the best part is: IT EXISTS
410                                 if(substring(self.model, 0, 17) == "models/weapons/v_")
411                                 {
412                                         if(substring(self.tag_entity.model, 0, 17) == "models/weapons/h_")
413                                         {
414                                                 self.tag_index = gettagindex(self.tag_entity, "weapon");
415                                                 if(!self.tag_index)
416                                                         self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
417                                                 if(!self.tag_index)
418                                                 {
419                                                         // we need to prevent this from 'appening
420                                                         self.tag_entity = world;
421                                                         self.drawmask = 0;
422                                                         dprint("h_ model lacks weapon attachment, but v_ model is attached to it\n");
423                                                 }
424                                         }
425                                         else if(self.tag_entity.isplayermodel)
426                                         {
427                                                 skeleton_loadinfo(self.tag_entity);
428                                                 self.tag_index = self.tag_entity.bone_weapon;
429                                         }
430                                 }
431
432                                 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/v_")
433                                 {
434                                         self.tag_index = gettagindex(self.tag_entity, "shot");
435                                         if(!self.tag_index)
436                                                 self.tag_index = gettagindex(self.tag_entity, "tag_shot");
437                                 }
438                         }
439                         else
440                         {
441                                 // damn, see you next frame
442                                 self.drawmask = 0;
443                         }
444                 }
445         }
446 }
447
448 // FEATURE: EF_NODRAW workalike
449 const int EF_BRIGHTFIELD        = 1;
450 const int EF_BRIGHTLIGHT        = 4;
451 const int EF_DIMLIGHT           = 8;
452 const int EF_DOUBLESIDED        = 32768;
453 const int EF_NOSELFSHADOW       = 65536;
454 const int EF_DYNAMICMODELLIGHT = 131072;
455 const int EF_RESTARTANIM_BIT = 1048576;
456 const int EF_TELEPORT_BIT = 2097152;
457 const int MF_ROCKET  =   1; // leave a trail
458 const int MF_GRENADE =   2; // leave a trail
459 const int MF_GIB     =   4; // leave a trail
460 const int MF_ROTATE  =   8; // rotate (bonus items)
461 const int MF_TRACER  =  16; // green split trail
462 const int MF_ZOMGIB  =  32; // small blood trail
463 const int MF_TRACER2 =  64; // orange split trail
464 const int MF_TRACER3 = 128; // purple trail
465 .int csqcmodel_effects;
466 .int csqcmodel_modelflags;
467 void CSQCModel_Effects_PreUpdate(void)
468 {
469         self.effects = self.csqcmodel_effects;
470         self.modelflags = self.csqcmodel_modelflags;
471 }
472 void Reset_ArcBeam(void);
473 void CSQCModel_Effects_PostUpdate(void)
474 {
475         if (self == csqcplayer) {
476                 if (self.csqcmodel_teleported) {
477                         Reset_ArcBeam();
478                 }
479         }
480         self.csqcmodel_effects = self.effects;
481         self.csqcmodel_modelflags = self.modelflags;
482         self.effects = 0;
483         self.modelflags = 0;
484         if(self.csqcmodel_teleported)
485                 Projectile_ResetTrail(self.origin);
486 }
487 .int snd_looping;
488 void CSQCModel_Effects_Apply(void)
489 {
490         int eff = self.csqcmodel_effects & ~CSQCMODEL_EF_RESPAWNGHOST;
491
492         self.renderflags &= ~(RF_DEPTHHACK | RF_ADDITIVE | RF_FULLBRIGHT | EF_NOSHADOW | RF_USEAXIS);
493         self.effects = 0;
494         self.traileffect = 0;
495
496         if(eff & EF_BRIGHTFIELD)
497                 self.traileffect = particleeffectnum("TR_NEXUIZPLASMA");
498         // ignoring EF_MUZZLEFLASH
499         if(eff & EF_BRIGHTLIGHT)
500                 adddynamiclight(self.origin, 400, '3 3 3');
501         if(eff & EF_DIMLIGHT)
502                 adddynamiclight(self.origin, 200, '1.5 1.5 1.5');
503         if((eff & EF_NODRAW) || (self.alpha < 0))
504                 self.drawmask = 0;
505         if(eff & EF_ADDITIVE)
506                 self.renderflags |= RF_ADDITIVE;
507         if(eff & EF_BLUE)
508                 adddynamiclight(self.origin, 200, '0.15 0.15 1.5');
509         if(eff & EF_RED)
510                 adddynamiclight(self.origin, 200, '1.5 0.15 0.15');
511         // ignoring EF_NOGUNBOB
512         if(eff & EF_FULLBRIGHT)
513                 self.renderflags |= RF_FULLBRIGHT;
514         if(eff & EF_FLAME)
515                 pointparticles(particleeffectnum("EF_FLAME"), self.origin, '0 0 0', bound(0, frametime, 0.1));
516         if(eff & EF_STARDUST)
517                 pointparticles(particleeffectnum("EF_STARDUST"), self.origin, '0 0 0', bound(0, frametime, 0.1));
518         if(eff & EF_NOSHADOW)
519                 self.renderflags |= RF_NOSHADOW;
520         if(eff & EF_NODEPTHTEST)
521                 self.renderflags |= RF_DEPTHHACK;
522         // ignoring EF_SELECTABLE
523         if(eff & EF_DOUBLESIDED)
524                 self.effects |= EF_DOUBLESIDED;
525         if(eff & EF_NOSELFSHADOW)
526                 self.effects |= EF_NOSELFSHADOW;
527         if(eff & EF_DYNAMICMODELLIGHT)
528                 self.renderflags |= RF_DYNAMICMODELLIGHT;
529         // ignoring EF_UNUSED18, EF_UNUSED19, EF_RESTARTANIM_BIT, EF_TELEPORT_BIT, EF_LOWPRECISION
530         if(self.csqcmodel_modelflags & MF_ROCKET)
531                 self.traileffect = particleeffectnum("TR_ROCKET");
532         if(self.csqcmodel_modelflags & MF_GRENADE)
533                 self.traileffect = particleeffectnum("TR_GRENADE");
534         if(self.csqcmodel_modelflags & MF_GIB)
535                 self.traileffect = particleeffectnum("TR_BLOOD");
536         if(self.csqcmodel_modelflags & MF_ROTATE)
537         {
538                 self.renderflags |= RF_USEAXIS;
539                 makevectors(self.angles + '0 100 0' * fmod(time, 3.6));
540         }
541         if(self.csqcmodel_modelflags & MF_TRACER)
542                 self.traileffect = particleeffectnum("TR_WIZSPIKE");
543         if(self.csqcmodel_modelflags & MF_ZOMGIB)
544                 self.traileffect = particleeffectnum("TR_SLIGHTBLOOD");
545         if(self.csqcmodel_modelflags & MF_TRACER2)
546                 self.traileffect = particleeffectnum("TR_KNIGHTSPIKE");
547         if(self.csqcmodel_modelflags & MF_TRACER3)
548                 self.traileffect = particleeffectnum("TR_VORESPIKE");
549
550         if(self.drawmask)
551                 Projectile_DrawTrail(self.origin);
552         else
553                 Projectile_ResetTrail(self.origin);
554
555         if(self.csqcmodel_effects & CSQCMODEL_EF_RESPAWNGHOST)
556                 self.renderflags |= RF_ADDITIVE;
557                 // also special in CSQCPlayer_GlowMod_Apply
558
559         if(self.csqcmodel_modelflags & MF_ROCKET)
560         {
561                 if(!self.snd_looping)
562                 {
563                         sound(self, CH_TRIGGER_SINGLE, "misc/jetpack_fly.wav", VOL_BASE, autocvar_g_jetpack_attenuation);
564                         self.snd_looping = CH_TRIGGER_SINGLE;
565                 }
566         }
567         else
568         {
569                 if(self.snd_looping)
570                 {
571                         sound(self, self.snd_looping, "misc/null.wav", VOL_BASE, autocvar_g_jetpack_attenuation);
572                         self.snd_looping = 0;
573                 }
574         }
575 }
576
577 void CSQCPlayer_Precache()
578 {
579         precache_sound("misc/jetpack_fly.wav");
580 }
581
582 // general functions
583 .int csqcmodel_predraw_run;
584 .int anim_frame;
585 .int anim_frame1time;
586 .int anim_frame2;
587 .int anim_frame2time;
588 .int anim_saveframe;
589 .int anim_saveframe1time;
590 .int anim_saveframe2;
591 .int anim_saveframe2time;
592 .int anim_prev_pmove_flags;
593 void CSQCModel_Hook_PreDraw(bool isplayer)
594 {
595         if(self.csqcmodel_predraw_run == framecount)
596                 return;
597         self.csqcmodel_predraw_run = framecount;
598
599         if(!self.modelindex || self.model == "null")
600         {
601                 self.drawmask = 0;
602                 return;
603         }
604         else
605                 self.drawmask = MASK_NORMAL;
606
607         if(self.isplayermodel) // this checks if it's a player MODEL!
608         {
609                 CSQCPlayer_ModelAppearance_Apply(self.entnum == player_localnum + 1);
610                 CSQCPlayer_LOD_Apply();
611                 if(!isplayer)
612                 {
613                         skeleton_loadinfo(self);
614                         bool doblend = (self.bone_upperbody >= 0);
615                         CSQCPlayer_FallbackFrame_Apply();
616                         if(doblend)
617                         {
618                                 skeleton_from_frames(self, self.csqcmodel_isdead);
619                         }
620                         else
621                         {
622                                 free_skeleton_from_frames(self);
623                                 // just in case, clear these (we're animating in frame and frame3)
624                                 self.lerpfrac = 0;
625                                 self.lerpfrac4 = 0;
626                         }
627                 }
628                 else
629                 {
630                         // we know that frame3 and frame4 fields, used by InterpolateAnimation, are left alone - but that is all we know!
631                         skeleton_loadinfo(self);
632                         bool doblend = (self.bone_upperbody >= 0);
633                         bool onground = 0;
634                         if(self == csqcplayer)
635                         {
636                                 if(self.pmove_flags & PMF_ONGROUND)
637                                         onground = 1;
638                                 self.anim_prev_pmove_flags = self.pmove_flags;
639                                 if(self.pmove_flags & PMF_DUCKED)
640                                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DUCK, false);
641                                 else if(self.anim_state & ANIMSTATE_DUCK)
642                                         animdecide_setstate(self, self.anim_state - ANIMSTATE_DUCK, false);
643                         }
644                         else
645                         {
646                                 tracebox(self.origin + '0 0 1', self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
647                                 if(trace_startsolid || trace_fraction < 1)
648                                         onground = 1;
649                         }
650                         animdecide_load_if_needed(self);
651                         animdecide_setimplicitstate(self, onground);
652                         animdecide_setframes(self, doblend, anim_frame, anim_frame1time, anim_frame2, anim_frame2time);
653                         int sf = 0;
654                         if(self.anim_saveframe != self.anim_frame || self.anim_saveframe1time != self.anim_frame1time)
655                                 sf |= CSQCMODEL_PROPERTY_FRAME;
656                         if(self.anim_saveframe2 != self.anim_frame2 || self.anim_saveframe2time != self.anim_frame2time)
657                                 sf |= CSQCMODEL_PROPERTY_FRAME2;
658                         self.anim_saveframe = self.anim_frame;
659                         self.anim_saveframe1time = self.anim_frame1time;
660                         self.anim_saveframe2 = self.anim_frame2;
661                         self.anim_saveframe2time = self.anim_frame2time;
662                         // Note: we always consider lerpfrac "changed", as it uses fixed values every time anyway.
663                         // This ensures that .frame etc. are always written.
664                         CSQCModel_InterpolateAnimation_2To4_PreNote(sf | CSQCMODEL_PROPERTY_LERPFRAC);
665                         self.lerpfrac = (doblend ? 0.5 : 0);
666                         self.frame = self.anim_frame;
667                         self.frame1time = self.anim_frame1time;
668                         self.frame2 = self.anim_frame2;
669                         self.frame2time = self.anim_frame2time;
670                         CSQCModel_InterpolateAnimation_2To4_Note(sf | CSQCMODEL_PROPERTY_LERPFRAC, false);
671                         CSQCModel_InterpolateAnimation_2To4_Do();
672                         if(doblend)
673                         {
674                                 skeleton_from_frames(self, self.csqcmodel_isdead);
675                         }
676                         else
677                         {
678                                 free_skeleton_from_frames(self);
679                                 // just in case, clear these (we're animating in frame and frame3)
680                                 self.lerpfrac = 0;
681                                 self.lerpfrac4 = 0;
682                         }
683                 }
684         }
685
686         CSQCModel_AutoTagIndex_Apply();
687
688         CSQCModel_Effects_Apply();
689 }
690
691 void CSQCModel_Hook_PreUpdate(bool isnew, bool isplayer, bool islocalplayer)
692 {
693         // interpolate v_angle
694         self.iflags |= IFLAG_V_ANGLE_X;
695         // revert to values from server
696         CSQCModel_Effects_PreUpdate();
697         if(self.isplayermodel)
698         {
699                 if(!isplayer)
700                         CSQCPlayer_FallbackFrame_PreUpdate();
701                 CSQCPlayer_ModelAppearance_PreUpdate();
702         }
703 }
704
705 void CSQCModel_Hook_PostUpdate(bool isnew, bool isplayer, bool islocalplayer)
706 {
707         // is it a player model? (shared state)
708         self.isplayermodel = (substring(self.model, 0, 14) == "models/player/" || substring(self.model, 0, 17) == "models/ok_player/");
709
710         // save values set by server
711         if(self.isplayermodel)
712         {
713                 CSQCPlayer_ModelAppearance_PostUpdate();
714                 if(isplayer)
715                         CSQCPlayer_AnimDecide_PostUpdate(isnew);
716                 else
717                         CSQCPlayer_FallbackFrame_PostUpdate(isnew);
718         }
719         CSQCModel_Effects_PostUpdate();
720 }