]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/csqcmodel_hooks.qc
5bef3cc9c9d67a81c11737961d9d58c2fb053f19
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / csqcmodel_hooks.qc
1 #include "csqcmodel_hooks.qh"
2
3 #include "autocvars.qh"
4 #include "defs.qh"
5 #include "gibs.qh"
6 #include "main.qh"
7 #include "miscfunctions.qh"
8 #include "player_skeleton.qh"
9 #include "sortlist.qh"
10
11 #include "weapons/projectile.qh"
12
13 #include "../common/animdecide.qh"
14 #include "../common/csqcmodel_settings.qh"
15 #include "../common/teams.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.tag_networkentity)
407         {
408                 // we are ATTACHED!
409                 bool changed = 0;
410                 if(self.tag_entity.entnum != self.tag_networkentity)
411                 {
412                         self.tag_entity = findfloat(world, entnum, self.tag_networkentity);
413                         changed = 1;
414                 }
415
416                 // recursive predraw call to fix issues with forcemodels and LOD if bone indexes mismatch
417                 if(self.tag_entity.classname == "csqcmodel")
418                 {
419                         entity oldself = self;
420                         self = self.tag_entity;
421                         CSQCModel_Hook_PreDraw((self.entnum >= 1 && self.entnum <= maxclients));
422                         self = oldself;
423                 }
424
425                 if(self.tag_entity.modelindex != self.tag_entity_lastmodelindex)
426                 {
427                         self.tag_entity_lastmodelindex = self.tag_entity.modelindex;
428                         changed = 1;
429                 }
430                 if(changed)
431                 {
432                         if(self.tag_entity)
433                         {
434                                 // the best part is: IT EXISTS
435                                 if(substring(self.model, 0, 17) == "models/weapons/v_")
436                                 {
437                                         if(substring(self.tag_entity.model, 0, 17) == "models/weapons/h_")
438                                         {
439                                                 self.tag_index = gettagindex(self.tag_entity, "weapon");
440                                                 if(!self.tag_index)
441                                                         self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
442                                                 if(!self.tag_index)
443                                                 {
444                                                         // we need to prevent this from 'appening
445                                                         self.tag_entity = world;
446                                                         self.drawmask = 0;
447                                                         dprint("h_ model lacks weapon attachment, but v_ model is attached to it\n");
448                                                 }
449                                         }
450                                         else if(self.tag_entity.isplayermodel)
451                                         {
452                                                 skeleton_loadinfo(self.tag_entity);
453                                                 self.tag_index = self.tag_entity.bone_weapon;
454                                         }
455                                 }
456
457                                 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/v_")
458                                 {
459                                         self.tag_index = gettagindex(self.tag_entity, "shot");
460                                         if(!self.tag_index)
461                                                 self.tag_index = gettagindex(self.tag_entity, "tag_shot");
462                                 }
463                         }
464                         else
465                         {
466                                 // damn, see you next frame
467                                 self.drawmask = 0;
468                         }
469                 }
470         }
471 }
472
473 // FEATURE: EF_NODRAW workalike
474 const int EF_BRIGHTFIELD        = 1;
475 const int EF_BRIGHTLIGHT        = 4;
476 const int EF_DIMLIGHT           = 8;
477 const int EF_DOUBLESIDED        = 32768;
478 const int EF_NOSELFSHADOW       = 65536;
479 const int EF_DYNAMICMODELLIGHT = 131072;
480 const int EF_RESTARTANIM_BIT = 1048576;
481 const int EF_TELEPORT_BIT = 2097152;
482 const int MF_ROCKET  =   1; // leave a trail
483 const int MF_GRENADE =   2; // leave a trail
484 const int MF_GIB     =   4; // leave a trail
485 const int MF_ROTATE  =   8; // rotate (bonus items)
486 const int MF_TRACER  =  16; // green split trail
487 const int MF_ZOMGIB  =  32; // small blood trail
488 const int MF_TRACER2 =  64; // orange split trail
489 const int MF_TRACER3 = 128; // purple trail
490 .int csqcmodel_effects;
491 .int csqcmodel_modelflags;
492 void CSQCModel_Effects_PreUpdate(void)
493 {
494         self.effects = self.csqcmodel_effects;
495         self.modelflags = self.csqcmodel_modelflags;
496 }
497 void Reset_ArcBeam(void);
498 void CSQCModel_Effects_PostUpdate(void)
499 {
500         if (self == csqcplayer) {
501                 if (self.csqcmodel_teleported) {
502                         Reset_ArcBeam();
503                 }
504         }
505         self.csqcmodel_effects = self.effects;
506         self.csqcmodel_modelflags = self.modelflags;
507         self.effects = 0;
508         self.modelflags = 0;
509         if(self.csqcmodel_teleported)
510                 Projectile_ResetTrail(self.origin);
511 }
512 .int snd_looping;
513 void CSQCModel_Effects_Apply(void)
514 {
515         int eff = self.csqcmodel_effects & ~CSQCMODEL_EF_RESPAWNGHOST;
516
517         self.renderflags &= ~(RF_DEPTHHACK | RF_ADDITIVE | RF_FULLBRIGHT | EF_NOSHADOW | RF_USEAXIS);
518         self.effects = 0;
519         self.traileffect = 0;
520
521         if(eff & EF_BRIGHTFIELD)
522                 self.traileffect = particleeffectnum("TR_NEXUIZPLASMA");
523         // ignoring EF_MUZZLEFLASH
524         if(eff & EF_BRIGHTLIGHT)
525                 adddynamiclight(self.origin, 400, '3 3 3');
526         if(eff & EF_DIMLIGHT)
527                 adddynamiclight(self.origin, 200, '1.5 1.5 1.5');
528         if((eff & EF_NODRAW) || (self.alpha < 0))
529                 self.drawmask = 0;
530         if(eff & EF_ADDITIVE)
531                 self.renderflags |= RF_ADDITIVE;
532         if(eff & EF_BLUE)
533                 adddynamiclight(self.origin, 200, '0.15 0.15 1.5');
534         if(eff & EF_RED)
535                 adddynamiclight(self.origin, 200, '1.5 0.15 0.15');
536         // ignoring EF_NOGUNBOB
537         if(eff & EF_FULLBRIGHT)
538                 self.renderflags |= RF_FULLBRIGHT;
539         if(eff & EF_FLAME)
540                 pointparticles(particleeffectnum("EF_FLAME"), self.origin, '0 0 0', bound(0, frametime, 0.1));
541         if(eff & EF_STARDUST)
542                 pointparticles(particleeffectnum("EF_STARDUST"), self.origin, '0 0 0', bound(0, frametime, 0.1));
543         if(eff & EF_NOSHADOW)
544                 self.renderflags |= RF_NOSHADOW;
545         if(eff & EF_NODEPTHTEST)
546                 self.renderflags |= RF_DEPTHHACK;
547         // ignoring EF_SELECTABLE
548         if(eff & EF_DOUBLESIDED)
549                 self.effects |= EF_DOUBLESIDED;
550         if(eff & EF_NOSELFSHADOW)
551                 self.effects |= EF_NOSELFSHADOW;
552         if(eff & EF_DYNAMICMODELLIGHT)
553                 self.renderflags |= RF_DYNAMICMODELLIGHT;
554         // ignoring EF_UNUSED18, EF_UNUSED19, EF_RESTARTANIM_BIT, EF_TELEPORT_BIT, EF_LOWPRECISION
555         if(self.csqcmodel_modelflags & MF_ROCKET)
556                 self.traileffect = particleeffectnum("TR_ROCKET");
557         if(self.csqcmodel_modelflags & MF_GRENADE)
558                 self.traileffect = particleeffectnum("TR_GRENADE");
559         if(self.csqcmodel_modelflags & MF_GIB)
560                 self.traileffect = particleeffectnum("TR_BLOOD");
561         if(self.csqcmodel_modelflags & MF_ROTATE)
562         {
563                 self.renderflags |= RF_USEAXIS;
564                 makevectors(self.angles + '0 100 0' * fmod(time, 3.6));
565         }
566         if(self.csqcmodel_modelflags & MF_TRACER)
567                 self.traileffect = particleeffectnum("TR_WIZSPIKE");
568         if(self.csqcmodel_modelflags & MF_ZOMGIB)
569                 self.traileffect = particleeffectnum("TR_SLIGHTBLOOD");
570         if(self.csqcmodel_modelflags & MF_TRACER2)
571                 self.traileffect = particleeffectnum("TR_KNIGHTSPIKE");
572         if(self.csqcmodel_modelflags & MF_TRACER3)
573                 self.traileffect = particleeffectnum("TR_VORESPIKE");
574
575         if(self.drawmask)
576                 Projectile_DrawTrail(self.origin);
577         else
578                 Projectile_ResetTrail(self.origin);
579
580         if(self.csqcmodel_effects & CSQCMODEL_EF_RESPAWNGHOST)
581                 self.renderflags |= RF_ADDITIVE;
582                 // also special in CSQCPlayer_GlowMod_Apply
583
584         if(self.csqcmodel_modelflags & MF_ROCKET)
585         {
586                 if(!self.snd_looping)
587                 {
588                         sound(self, CH_TRIGGER_SINGLE, "misc/jetpack_fly.wav", VOL_BASE, autocvar_g_jetpack_attenuation);
589                         self.snd_looping = CH_TRIGGER_SINGLE;
590                 }
591         }
592         else
593         {
594                 if(self.snd_looping)
595                 {
596                         sound(self, self.snd_looping, "misc/null.wav", VOL_BASE, autocvar_g_jetpack_attenuation);
597                         self.snd_looping = 0;
598                 }
599         }
600 }
601
602 void CSQCPlayer_Precache()
603 {
604         precache_sound("misc/jetpack_fly.wav");
605 }
606
607 // general functions
608 .int csqcmodel_predraw_run;
609 .int anim_frame;
610 .int anim_frame1time;
611 .int anim_frame2;
612 .int anim_frame2time;
613 .int anim_saveframe;
614 .int anim_saveframe1time;
615 .int anim_saveframe2;
616 .int anim_saveframe2time;
617 .int anim_prev_pmove_flags;
618 void CSQCModel_Hook_PreDraw(bool isplayer)
619 {
620         if(self.csqcmodel_predraw_run == framecount)
621                 return;
622         self.csqcmodel_predraw_run = framecount;
623
624         if(!self.modelindex || self.model == "null")
625         {
626                 self.drawmask = 0;
627                 return;
628         }
629         else
630                 self.drawmask = MASK_NORMAL;
631
632         if(self.isplayermodel) // this checks if it's a player MODEL!
633         {
634                 CSQCPlayer_ModelAppearance_Apply(self.entnum == player_localnum + 1);
635                 CSQCPlayer_LOD_Apply();
636                 if(!isplayer)
637                 {
638                         skeleton_loadinfo(self);
639                         bool doblend = (self.bone_upperbody >= 0);
640                         CSQCPlayer_FallbackFrame_Apply();
641                         if(doblend)
642                         {
643                                 skeleton_from_frames(self, self.csqcmodel_isdead);
644                         }
645                         else
646                         {
647                                 free_skeleton_from_frames(self);
648                                 // just in case, clear these (we're animating in frame and frame3)
649                                 self.lerpfrac = 0;
650                                 self.lerpfrac4 = 0;
651                         }
652                 }
653                 else
654                 {
655                         // we know that frame3 and frame4 fields, used by InterpolateAnimation, are left alone - but that is all we know!
656                         skeleton_loadinfo(self);
657                         bool doblend = (self.bone_upperbody >= 0);
658                         bool onground = 0;
659                         if(self == csqcplayer)
660                         {
661                                 if(self.pmove_flags & PMF_ONGROUND)
662                                         onground = 1;
663                                 self.anim_prev_pmove_flags = self.pmove_flags;
664                                 if(self.pmove_flags & PMF_DUCKED)
665                                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DUCK, false);
666                                 else if(self.anim_state & ANIMSTATE_DUCK)
667                                         animdecide_setstate(self, self.anim_state - ANIMSTATE_DUCK, false);
668                         }
669                         else
670                         {
671                                 tracebox(self.origin + '0 0 1', self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
672                                 if(trace_startsolid || trace_fraction < 1)
673                                         onground = 1;
674                         }
675                         animdecide_load_if_needed(self);
676                         animdecide_setimplicitstate(self, onground);
677                         animdecide_setframes(self, doblend, anim_frame, anim_frame1time, anim_frame2, anim_frame2time);
678                         int sf = 0;
679                         if(self.anim_saveframe != self.anim_frame || self.anim_saveframe1time != self.anim_frame1time)
680                                 sf |= CSQCMODEL_PROPERTY_FRAME;
681                         if(self.anim_saveframe2 != self.anim_frame2 || self.anim_saveframe2time != self.anim_frame2time)
682                                 sf |= CSQCMODEL_PROPERTY_FRAME2;
683                         self.anim_saveframe = self.anim_frame;
684                         self.anim_saveframe1time = self.anim_frame1time;
685                         self.anim_saveframe2 = self.anim_frame2;
686                         self.anim_saveframe2time = self.anim_frame2time;
687                         // Note: we always consider lerpfrac "changed", as it uses fixed values every time anyway.
688                         // This ensures that .frame etc. are always written.
689                         CSQCModel_InterpolateAnimation_2To4_PreNote(sf | CSQCMODEL_PROPERTY_LERPFRAC);
690                         self.lerpfrac = (doblend ? 0.5 : 0);
691                         self.frame = self.anim_frame;
692                         self.frame1time = self.anim_frame1time;
693                         self.frame2 = self.anim_frame2;
694                         self.frame2time = self.anim_frame2time;
695                         CSQCModel_InterpolateAnimation_2To4_Note(sf | CSQCMODEL_PROPERTY_LERPFRAC, false);
696                         CSQCModel_InterpolateAnimation_2To4_Do();
697                         if(doblend)
698                         {
699                                 skeleton_from_frames(self, self.csqcmodel_isdead);
700                         }
701                         else
702                         {
703                                 free_skeleton_from_frames(self);
704                                 // just in case, clear these (we're animating in frame and frame3)
705                                 self.lerpfrac = 0;
706                                 self.lerpfrac4 = 0;
707                         }
708                 }
709         }
710
711         CSQCModel_AutoTagIndex_Apply();
712
713         CSQCModel_Effects_Apply();
714 }
715
716 void CSQCModel_Hook_PreUpdate(bool isnew, bool isplayer, bool islocalplayer)
717 {
718         // interpolate v_angle
719         self.iflags |= IFLAG_V_ANGLE_X;
720         // revert to values from server
721         CSQCModel_Effects_PreUpdate();
722         if(self.isplayermodel)
723         {
724                 if(!isplayer)
725                         CSQCPlayer_FallbackFrame_PreUpdate();
726                 CSQCPlayer_ModelAppearance_PreUpdate();
727         }
728 }
729
730 void CSQCModel_Hook_PostUpdate(bool isnew, bool isplayer, bool islocalplayer)
731 {
732         // is it a player model? (shared state)
733         self.isplayermodel = (substring(self.model, 0, 14) == "models/player/" || substring(self.model, 0, 17) == "models/ok_player/");
734
735         // save values set by server
736         if(self.isplayermodel)
737         {
738                 CSQCPlayer_ModelAppearance_PostUpdate();
739                 if(isplayer)
740                         CSQCPlayer_AnimDecide_PostUpdate(isnew);
741                 else
742                         CSQCPlayer_FallbackFrame_PostUpdate(isnew);
743         }
744         CSQCModel_Effects_PostUpdate();
745 }