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