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