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