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