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