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