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