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