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