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