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