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