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