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