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