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