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