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