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