Fix csqcmodel display for cloned/dead players. Also clone aiming direction :)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / csqcmodel_hooks.qc
1 void CSQCModel_Hook_PreDraw(float isplayer);
2
3 .float isplayermodel;
4
5 // FEATURE: LOD
6 .float lodmodelindex0;
7 .float lodmodelindex1;
8 .float 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 float forceplayermodels_modelisgoodmodel;
75 float forceplayermodels_modelindex;
76 float forceplayermodels_skin;
77
78 string forceplayermodels_mymodel;
79 float forceplayermodels_myisgoodmodel;
80 float forceplayermodels_mymodelindex;
81
82 float forceplayermodels_attempted;
83
84 .string forceplayermodels_savemodel;
85 .float forceplayermodels_savemodelindex;
86 .float forceplayermodels_saveskin;
87 .float forceplayermodels_savecolormap;
88
89 .string forceplayermodels_isgoodmodel_mdl;
90 .float forceplayermodels_isgoodmodel;
91
92 string forceplayermodels_goodmodel;
93 float 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                         print(sprintf("Warning: missing model %s has been used\n", self.forceplayermodels_savemodel));
118         }
119 }
120 void CSQCPlayer_ModelAppearance_Apply(float 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         float isfriend;
181         float 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                 float forcecolor_friend = 0;
220                 float forcecolor_enemy = 0;
221                 float teams_count = 0;
222                 entity tm;
223
224                 for(tm = teams.sort_next; tm; tm = tm.sort_next)
225                         if(tm.team != NUM_SPECTATOR)
226                                 ++teams_count;
227
228                 if(autocvar_cl_forcemyplayercolors)
229                         forcecolor_friend = 1024 + autocvar_cl_forcemyplayercolors;
230                 if(autocvar_cl_forceplayercolors && teams_count == 2)
231                         forcecolor_enemy = 1024 + autocvar__cl_color;
232
233                 if(forcecolor_enemy && !forcecolor_friend)
234                 {
235                         // only enemy color is forced?
236                         // verify it is not equal to the friend color
237                         if(forcecolor_enemy == 1024 + 17 * myteam)
238                                 forcecolor_enemy = 0;
239                 }
240
241                 if(forcecolor_friend && !forcecolor_enemy)
242                 {
243                         // only friend color is forced?
244                         // verify it is not equal to the enemy color
245                         for(tm = teams.sort_next; tm; tm = tm.sort_next)
246                                 // note: we even compare against our own team.
247                                 // if we rejected because we matched our OWN team color,
248                                 // this is not bad; we then simply keep our color as is
249                                 // anyway.
250                                 if(forcecolor_friend == 1024 + 17 * tm.team)
251                                         forcecolor_friend = 0;
252                 }
253
254                 if(cm == 1024 + 17 * myteam)
255                 {
256                         if(forcecolor_friend)
257                                 self.colormap = forcecolor_friend;
258                 }
259                 else
260                 {
261                         if(forcecolor_enemy)
262                                 self.colormap = forcecolor_enemy;
263                 }
264         }
265         else
266         {
267                 if(autocvar_cl_forcemyplayercolors && islocalplayer)
268                         self.colormap = 1024 + autocvar_cl_forcemyplayercolors;
269                 else if(autocvar_cl_forceplayercolors)
270                         self.colormap = player_localnum + 1;
271         }
272
273         // GLOWMOD AND DEATH FADING
274         if(self.colormap > 0)
275                 self.glowmod = colormapPaletteColor(((self.colormap >= 1024) ? self.colormap : stof(getplayerkeyvalue(self.colormap - 1, "colors"))) & 0x0F, TRUE) * 2;
276         else
277                 self.glowmod = '1 1 1';
278
279         if(autocvar_cl_deathglow > 0)
280         {
281                 if(self.csqcmodel_isdead)
282                 {
283                         // Fade out to black now...
284                         if(self.old_glowmod == '0 0 0') { self.old_glowmod = self.glowmod; }
285                         self.colormap = 0;
286
287                         self.glowmod = self.old_glowmod * bound(0, 1 - (time - self.death_time) / autocvar_cl_deathglow, 1);
288                         self.glowmod_x = max(self.glowmod_x, 0.0001);
289                         self.glowmod_y = max(self.glowmod_y, 0.0001);
290                         self.glowmod_z = max(self.glowmod_z, 0.0001);
291                 }
292                 else if(self.old_glowmod != '0 0 0') { self.old_glowmod = '0 0 0'; }
293         }
294
295         //print(sprintf("CSQCPlayer_ModelAppearance_Apply(): state = %s, colormap = %f, glowmod = %s\n", (self.csqcmodel_isdead ? "DEAD" : "ALIVE"), self.colormap, vtos(self.glowmod)));
296 }
297
298 // FEATURE: fallback frames
299 .float csqcmodel_saveframe;
300 .float csqcmodel_saveframe2;
301 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
302 .float csqcmodel_saveframe3;
303 .float csqcmodel_saveframe4;
304 #endif
305 .float csqcmodel_framecount;
306
307 #define IS_DEAD_FRAME(f) ((f) == 0 || (f) == 1)
308 void CSQCPlayer_FallbackFrame_PreUpdate(void)
309 {
310         self.frame = self.csqcmodel_saveframe;
311         self.frame2 = self.csqcmodel_saveframe2;
312 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
313         self.frame3 = self.csqcmodel_saveframe3;
314         self.frame4 = self.csqcmodel_saveframe4;
315 #endif
316 }
317 void CSQCPlayer_FallbackFrame_PostUpdate(float isnew)
318 {
319         self.csqcmodel_saveframe = self.frame;
320         self.csqcmodel_saveframe2 = self.frame2;
321 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
322         self.csqcmodel_saveframe3 = self.frame3;
323         self.csqcmodel_saveframe4 = self.frame4;
324 #endif
325
326         // hack for death animations: set their frametime to zero in case a
327         // player "pops in"
328         if(isnew)
329         {
330 #define FIX_FRAMETIME(f,ft) \
331                 if(IS_DEAD_FRAME(self.f) && self.ft != 0 && self.death_time != 0) \
332                 { \
333                         self.ft = self.death_time; \
334                 }
335                 FIX_FRAMETIME(frame, frame1time)
336                 FIX_FRAMETIME(frame2, frame2time)
337 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
338                 FIX_FRAMETIME(frame3, frame3time)
339                 FIX_FRAMETIME(frame4, frame4time)
340 #endif
341         }
342         self.csqcmodel_isdead = IS_DEAD_FRAME(self.frame);
343 }
344 void CSQCPlayer_AnimDecide_PostUpdate(float isnew)
345 {
346         self.csqcmodel_isdead = !!(self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
347 }
348 float CSQCPlayer_FallbackFrame(float f)
349 {
350         if(frameduration(self.modelindex, f) > 0)
351                 return f; // goooooood
352         if(frameduration(self.modelindex, 1) <= 0)
353                 return f; // this is a static model. We can't fix it if we wanted to
354         switch(f)
355         {
356                 case 23: return 11; // anim_melee -> anim_shoot
357                 case 24: return 4; // anim_duckwalkbackwards -> anim_duckwalk
358                 case 25: return 4; // anim_duckwalkstrafeleft -> anim_duckwalk
359                 case 26: return 4; // anim_duckwalkstraferight -> anim_duckwalk
360                 case 27: return 4; // anim_duckwalkforwardright -> anim_duckwalk
361                 case 28: return 4; // anim_duckwalkforwardleft -> anim_duckwalk
362                 case 29: return 4; // anim_duckwalkbackright -> anim_duckwalk
363                 case 30: return 4; // anim_duckwalkbackleft -> anim_duckwalk
364         }
365         print(sprintf("Frame %d missing in model %s, and we have no fallback - FAIL!\n", f, self.model));
366         return f;
367 }
368 void CSQCPlayer_FallbackFrame_Apply(void)
369 {
370         self.frame = CSQCPlayer_FallbackFrame(self.frame);
371         self.frame2 = CSQCPlayer_FallbackFrame(self.frame2);
372 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
373         self.frame3 = CSQCPlayer_FallbackFrame(self.frame3);
374         self.frame4 = CSQCPlayer_FallbackFrame(self.frame4);
375 #endif
376 }
377
378 // FEATURE: auto tag_index
379 .entity tag_entity;
380 .float tag_entity_lastmodelindex;
381 .float tag_index;
382 void CSQCModel_AutoTagIndex_Apply(void)
383 {
384         if(self.tag_entity && wasfreed(self.tag_entity))
385                 self.tag_entity = world;
386
387         if(self.tag_networkentity)
388         {
389                 // we are ATTACHED!
390                 float changed = 0;
391                 if(self.tag_entity.entnum != self.tag_networkentity)
392                 {
393                         self.tag_entity = findfloat(world, entnum, self.tag_networkentity);
394                         changed = 1;
395                 }
396
397                 // recursive predraw call to fix issues with forcemodels and LOD if bone indexes mismatch
398                 if(self.tag_entity.classname == "csqcmodel")
399                 {
400                         entity oldself = self;
401                         self = self.tag_entity;
402                         CSQCModel_Hook_PreDraw((self.entnum >= 1 && self.entnum <= maxclients));
403                         self = oldself;
404                 }
405
406                 if(self.tag_entity.modelindex != self.tag_entity_lastmodelindex)
407                 {
408                         self.tag_entity_lastmodelindex = self.tag_entity.modelindex;
409                         changed = 1;
410                 }
411                 if(changed)
412                 {
413                         if(self.tag_entity)
414                         {
415                                 // the best part is: IT EXISTS
416                                 if(substring(self.model, 0, 17) == "models/weapons/v_")
417                                 {
418                                         if(substring(self.tag_entity.model, 0, 17) == "models/weapons/h_")
419                                         {
420                                                 self.tag_index = gettagindex(self.tag_entity, "weapon");
421                                                 if(!self.tag_index)
422                                                         self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
423                                                 if(!self.tag_index)
424                                                 {
425                                                         // we need to prevent this from 'appening
426                                                         self.tag_entity = world;
427                                                         self.drawmask = 0;
428                                                         dprint("h_ model lacks weapon attachment, but v_ model is attached to it\n");
429                                                 }
430                                         }
431                                         else if(self.tag_entity.isplayermodel)
432                                         {
433                                                 skeleton_loadinfo(self.tag_entity);
434                                                 self.tag_index = self.tag_entity.bone_weapon;
435                                         }
436                                 }
437
438                                 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/v_")
439                                 {
440                                         self.tag_index = gettagindex(self.tag_entity, "shot");
441                                         if(!self.tag_index)
442                                                 self.tag_index = gettagindex(self.tag_entity, "tag_shot");
443                                 }
444                         }
445                         else
446                         {
447                                 // damn, see you next frame
448                                 self.drawmask = 0;
449                         }
450                 }
451         }
452 }
453
454 // FEATURE: EF_NODRAW workalike
455 const float EF_BRIGHTFIELD      = 1;
456 const float EF_BRIGHTLIGHT      = 4;
457 const float EF_DIMLIGHT         = 8;
458 const float EF_DOUBLESIDED = 32768;
459 const float EF_NOSELFSHADOW = 65536;
460 const float EF_DYNAMICMODELLIGHT = 131072;
461 const float EF_RESTARTANIM_BIT = 1048576;
462 const float EF_TELEPORT_BIT = 2097152;
463 const float MF_ROCKET  =   1; // leave a trail
464 const float MF_GRENADE =   2; // leave a trail
465 const float MF_GIB     =   4; // leave a trail
466 const float MF_ROTATE  =   8; // rotate (bonus items)
467 const float MF_TRACER  =  16; // green split trail
468 const float MF_ZOMGIB  =  32; // small blood trail
469 const float MF_TRACER2 =  64; // orange split trail
470 const float MF_TRACER3 = 128; // purple trail
471 .float csqcmodel_effects;
472 .float csqcmodel_modelflags;
473 void CSQCModel_Effects_PreUpdate(void)
474 {
475         self.effects = self.csqcmodel_effects;
476         self.modelflags = self.csqcmodel_modelflags;
477 }
478 void CSQCModel_Effects_PostUpdate(void)
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 .float snd_looping;
488 void CSQCModel_Effects_Apply(void)
489 {
490         float eff = self.csqcmodel_effects;
491         eff &= ~CSQCMODEL_EF_RESPAWNGHOST;
492
493         self.renderflags &= ~(RF_DEPTHHACK | RF_ADDITIVE | RF_FULLBRIGHT | EF_NOSHADOW | RF_USEAXIS);
494         self.effects = 0;
495         self.traileffect = 0;
496
497         if(eff & EF_BRIGHTFIELD)
498                 self.traileffect = particleeffectnum("TR_NEXUIZPLASMA");
499         // ignoring EF_MUZZLEFLASH
500         if(eff & EF_BRIGHTLIGHT)
501                 adddynamiclight(self.origin, 400, '3 3 3');
502         if(eff & EF_DIMLIGHT)
503                 adddynamiclight(self.origin, 200, '1.5 1.5 1.5');
504         if((eff & EF_NODRAW) || (self.alpha < 0))
505                 self.drawmask = 0;
506         if(eff & EF_ADDITIVE)
507                 self.renderflags |= RF_ADDITIVE;
508         if(eff & EF_BLUE)
509                 adddynamiclight(self.origin, 200, '0.15 0.15 1.5');
510         if(eff & EF_RED)
511                 adddynamiclight(self.origin, 200, '1.5 0.15 0.15');
512         // ignoring EF_NOGUNBOB
513         if(eff & EF_FULLBRIGHT)
514                 self.renderflags |= RF_FULLBRIGHT;
515         if(eff & EF_FLAME)
516                 pointparticles(particleeffectnum("EF_FLAME"), self.origin, '0 0 0', bound(0, frametime, 0.1));
517         if(eff & EF_STARDUST)
518                 pointparticles(particleeffectnum("EF_STARDUST"), self.origin, '0 0 0', bound(0, frametime, 0.1));
519         if(eff & EF_NOSHADOW)
520                 self.renderflags |= RF_NOSHADOW;
521         if(eff & EF_NODEPTHTEST)
522                 self.renderflags |= RF_DEPTHHACK;
523         // ignoring EF_SELECTABLE
524         if(eff & EF_DOUBLESIDED)
525                 self.effects |= EF_DOUBLESIDED;
526         if(eff & EF_NOSELFSHADOW)
527                 self.effects |= EF_NOSELFSHADOW;
528         if(eff & EF_DYNAMICMODELLIGHT)
529                 self.renderflags |= RF_DYNAMICMODELLIGHT;
530         // ignoring EF_UNUSED18, EF_UNUSED19, EF_RESTARTANIM_BIT, EF_TELEPORT_BIT, EF_LOWPRECISION
531         if(self.csqcmodel_modelflags & MF_ROCKET)
532                 self.traileffect = particleeffectnum("TR_ROCKET");
533         if(self.csqcmodel_modelflags & MF_GRENADE)
534                 self.traileffect = particleeffectnum("TR_GRENADE");
535         if(self.csqcmodel_modelflags & MF_GIB)
536                 self.traileffect = particleeffectnum("TR_BLOOD");
537         if(self.csqcmodel_modelflags & MF_ROTATE)
538         {
539                 self.renderflags |= RF_USEAXIS;
540                 makevectors(self.angles + '0 100 0' * fmod(time, 3.6));
541         }
542         if(self.csqcmodel_modelflags & MF_TRACER)
543                 self.traileffect = particleeffectnum("TR_WIZSPIKE");
544         if(self.csqcmodel_modelflags & MF_ZOMGIB)
545                 self.traileffect = particleeffectnum("TR_SLIGHTBLOOD");
546         if(self.csqcmodel_modelflags & MF_TRACER2)
547                 self.traileffect = particleeffectnum("TR_KNIGHTSPIKE");
548         if(self.csqcmodel_modelflags & MF_TRACER3)
549                 self.traileffect = particleeffectnum("TR_VORESPIKE");
550
551         if(self.drawmask)
552                 Projectile_DrawTrail(self.origin);
553         else
554                 Projectile_ResetTrail(self.origin);
555
556         if(self.csqcmodel_effects & CSQCMODEL_EF_RESPAWNGHOST)
557                 self.renderflags |= RF_ADDITIVE;
558                 // also special in CSQCPlayer_GlowMod_Apply
559
560         if(self.csqcmodel_modelflags & MF_ROCKET)
561         {
562                 if(!self.snd_looping)
563                 {
564                         sound(self, CH_TRIGGER_SINGLE, "misc/jetpack_fly.wav", VOL_BASE, autocvar_g_jetpack_attenuation);
565                         self.snd_looping = CH_TRIGGER_SINGLE;
566                 }
567         }
568         else
569         {
570                 if(self.snd_looping)
571                 {
572                         sound(self, self.snd_looping, "misc/null.wav", VOL_BASE, autocvar_g_jetpack_attenuation);
573                         self.snd_looping = 0;
574                 }
575         }
576 }
577
578 void CSQCPlayer_Precache()
579 {
580         precache_sound("misc/jetpack_fly.wav");
581 }
582
583 // general functions
584 .float csqcmodel_predraw_run;
585 .float anim_frame;
586 .float anim_frame1time;
587 .float anim_frame2;
588 .float anim_frame2time;
589 .float anim_saveframe;
590 .float anim_saveframe1time;
591 .float anim_saveframe2;
592 .float anim_saveframe2time;
593 .float anim_prev_pmove_flags;
594 void CSQCModel_Hook_PreDraw(float isplayer)
595 {
596         if(self.csqcmodel_predraw_run == framecount)
597                 return;
598         self.csqcmodel_predraw_run = framecount;
599
600         if(!self.modelindex || self.model == "null")
601         {
602                 self.drawmask = 0;
603                 return;
604         }
605         else
606                 self.drawmask = MASK_NORMAL;
607
608         if(self.isplayermodel) // this checks if it's a player MODEL!
609         {
610                 CSQCPlayer_ModelAppearance_Apply(self.entnum == player_localnum + 1);
611                 CSQCPlayer_LOD_Apply();
612                 if(!isplayer)
613                 {
614                         skeleton_loadinfo(self);
615                         float doblend = (self.bone_upperbody >= 0);
616                         CSQCPlayer_FallbackFrame_Apply();
617                         if(doblend)
618                         {
619                                 skeleton_from_frames(self, self.csqcmodel_isdead);
620                         }
621                         else
622                         {
623                                 free_skeleton_from_frames(self);
624                                 // just in case, clear these (we're animating in frame and frame3)
625                                 self.lerpfrac = 0;
626                                 self.lerpfrac4 = 0;
627                         }
628                 }
629                 else
630                 {
631                         // we know that frame3 and frame4 fields, used by InterpolateAnimation, are left alone - but that is all we know!
632                         skeleton_loadinfo(self);
633                         float doblend = (self.bone_upperbody >= 0);
634                         float onground = 0;
635                         if(self == csqcplayer)
636                         {
637                                 if(self.pmove_flags & PMF_ONGROUND)
638                                         onground = 1;
639                                 self.anim_prev_pmove_flags = self.pmove_flags;
640                                 if(self.pmove_flags & PMF_DUCKED)
641                                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DUCK, FALSE);
642                                 else if(self.anim_state & ANIMSTATE_DUCK)
643                                         animdecide_setstate(self, self.anim_state - ANIMSTATE_DUCK, FALSE);
644                         }
645                         else
646                         {
647                                 tracebox(self.origin + '0 0 1', self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
648                                 if(trace_startsolid || trace_fraction < 1)
649                                         onground = 1;
650                         }
651                         animdecide_init(self);
652                         animdecide_setimplicitstate(self, onground);
653                         animdecide_setframes(self, doblend, anim_frame, anim_frame1time, anim_frame2, anim_frame2time);
654                         float sf = 0;
655                         if(self.anim_saveframe != self.anim_frame || self.anim_saveframe1time != self.anim_frame1time)
656                                 sf |= CSQCMODEL_PROPERTY_FRAME;
657                         if(self.anim_saveframe2 != self.anim_frame2 || self.anim_saveframe2time != self.anim_frame2time)
658                                 sf |= CSQCMODEL_PROPERTY_FRAME2;
659                         self.anim_saveframe = self.anim_frame;
660                         self.anim_saveframe1time = self.anim_frame1time;
661                         self.anim_saveframe2 = self.anim_frame2;
662                         self.anim_saveframe2time = self.anim_frame2time;
663                         if(sf)
664                         {
665                                 CSQCModel_InterpolateAnimation_2To4_PreNote(sf | CSQCMODEL_PROPERTY_LERPFRAC);
666                                 self.lerpfrac = (doblend ? 0.5 : 0);
667                                 self.frame = self.anim_frame;
668                                 self.frame1time = self.anim_frame1time;
669                                 self.frame2 = self.anim_frame2;
670                                 self.frame2time = self.anim_frame2time;
671                                 CSQCModel_InterpolateAnimation_2To4_Note(sf | CSQCMODEL_PROPERTY_LERPFRAC, FALSE);
672                         }
673                         CSQCModel_InterpolateAnimation_2To4_Do();
674                         if(doblend)
675                         {
676                                 skeleton_from_frames(self, self.csqcmodel_isdead);
677                         }
678                         else
679                         {
680                                 free_skeleton_from_frames(self);
681                                 // just in case, clear these (we're animating in frame and frame3)
682                                 self.lerpfrac = 0;
683                                 self.lerpfrac4 = 0;
684                         }
685                 }
686         }
687
688         CSQCModel_AutoTagIndex_Apply();
689
690         CSQCModel_Effects_Apply();
691 }
692
693 void CSQCModel_Hook_PreUpdate(float isnew, float isplayer, float islocalplayer)
694 {
695         // interpolate v_angle
696         self.iflags |= IFLAG_V_ANGLE_X;
697         // revert to values from server
698         CSQCModel_Effects_PreUpdate();
699         if(self.isplayermodel)
700         {
701                 if(!isplayer)
702                         CSQCPlayer_FallbackFrame_PreUpdate();
703                 CSQCPlayer_ModelAppearance_PreUpdate();
704         }
705 }
706
707 void CSQCModel_Hook_PostUpdate(float isnew, float isplayer, float islocalplayer)
708 {
709         // is it a player model? (shared state)
710         self.isplayermodel = (substring(self.model, 0, 14) == "models/player/" || substring(self.model, 0, 17) == "models/ok_player/");
711
712         // save values set by server
713         if(self.isplayermodel)
714         {
715                 CSQCPlayer_ModelAppearance_PostUpdate();
716                 if(isplayer)
717                         CSQCPlayer_AnimDecide_PostUpdate(isnew);
718                 else
719                         CSQCPlayer_FallbackFrame_PostUpdate(isnew);
720         }
721         CSQCModel_Effects_PostUpdate();
722 }