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