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