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