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