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