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