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