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