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