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