]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/csqcmodel_hooks.qc
New electricity effect
[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 unique colors
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 = this.entnum - 1;
317                         int c1 = num % 15;
318                         int q = floor(num / 15);
319                         int c2 = (c1 + 1 + q) % 15;
320                         this.colormap = 1024 + (c1 << 4) + c2;
321                 }
322                 else if(forceplayercolors_enabled)
323                         this.colormap = player_localnum + 1;
324         }
325
326         LABEL(skipforcecolors)
327
328         if((this.csqcmodel_effects & CSQCMODEL_EF_RESPAWNGHOST) && !autocvar_cl_respawn_ghosts_keepcolors)
329         {
330                 this.glowmod = '0 0 0';
331                 this.colormap = 0;
332                 return;
333         }
334
335         // GLOWMOD AND DEATH FADING
336         if(this.colormap > 0)
337                 this.glowmod = colormapPaletteColor(((this.colormap >= 1024) ? this.colormap : entcs_GetClientColors(this.colormap - 1)) & 0x0F, true);
338         else
339                 this.glowmod = '1 1 1';
340
341         if(autocvar_cl_deathglow > 0)
342         {
343                 if(this.csqcmodel_isdead)
344                 {
345                         float min_factor = bound(0, autocvar_cl_deathglow_min, 1);
346                         if(this.colormap > 0)
347                                 min_factor /= 2;
348                         float glow_fade = bound(0, 1 - (time - this.death_time) / autocvar_cl_deathglow, 1);
349                         this.glowmod *= (min_factor + glow_fade * (1 - min_factor));
350                         if (this.glowmod == '0 0 0')
351                                 this.glowmod.x = 0.000001;
352                 }
353         }
354
355         // don't let the engine increase player's glowmod
356         if (autocvar_r_hdr_glowintensity > 1)
357                 this.glowmod /= autocvar_r_hdr_glowintensity;
358
359         //printf("CSQCPlayer_ModelAppearance_Apply(): state = %s, colormap = %f, glowmod = %s\n", (this.csqcmodel_isdead ? "DEAD" : "ALIVE"), this.colormap, vtos(this.glowmod));
360 }
361
362 // FEATURE: fallback frames
363 .int csqcmodel_saveframe;
364 .int csqcmodel_saveframe2;
365 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
366 .int csqcmodel_saveframe3;
367 .int csqcmodel_saveframe4;
368 #endif
369 .int csqcmodel_framecount;
370
371 #define IS_DEAD_FRAME(f) ((f) == 0 || (f) == 1)
372 void CSQCPlayer_FallbackFrame_PreUpdate(entity this)
373 {
374         this.frame = this.csqcmodel_saveframe;
375         this.frame2 = this.csqcmodel_saveframe2;
376 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
377         this.frame3 = this.csqcmodel_saveframe3;
378         this.frame4 = this.csqcmodel_saveframe4;
379 #endif
380 }
381 void CSQCPlayer_FallbackFrame_PostUpdate(entity this, bool isnew)
382 {
383         this.csqcmodel_saveframe = this.frame;
384         this.csqcmodel_saveframe2 = this.frame2;
385 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
386         this.csqcmodel_saveframe3 = this.frame3;
387         this.csqcmodel_saveframe4 = this.frame4;
388 #endif
389
390         // hack for death animations: set their frametime to zero in case a
391         // player "pops in"
392         if(isnew)
393         {
394 #define FIX_FRAMETIME(f,ft) MACRO_BEGIN \
395                 if(IS_DEAD_FRAME(this.f) && this.ft != 0 && this.death_time != 0) \
396                         this.ft = this.death_time; \
397 MACRO_END
398                 FIX_FRAMETIME(frame, frame1time);
399                 FIX_FRAMETIME(frame2, frame2time);
400 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
401                 FIX_FRAMETIME(frame3, frame3time);
402                 FIX_FRAMETIME(frame4, frame4time);
403 #endif
404         }
405         this.csqcmodel_isdead = IS_DEAD_FRAME(this.frame);
406 }
407 void CSQCPlayer_AnimDecide_PostUpdate(entity this, bool isnew)
408 {
409         this.csqcmodel_isdead = boolean(this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
410 }
411 int CSQCPlayer_FallbackFrame(entity this, int f)
412 {
413         TC(int, f);
414         if(frameduration(this.modelindex, f) > 0)
415                 return f; // goooooood
416         if(frameduration(this.modelindex, 1) <= 0)
417                 return f; // this is a static model. We can't fix it if we wanted to
418         switch(f)
419         {
420                 case 23: return 11; // anim_melee -> anim_shoot
421                 case 24: return 4; // anim_duckwalkbackwards -> anim_duckwalk
422                 case 25: return 4; // anim_duckwalkstrafeleft -> anim_duckwalk
423                 case 26: return 4; // anim_duckwalkstraferight -> anim_duckwalk
424                 case 27: return 4; // anim_duckwalkforwardright -> anim_duckwalk
425                 case 28: return 4; // anim_duckwalkforwardleft -> anim_duckwalk
426                 case 29: return 4; // anim_duckwalkbackright -> anim_duckwalk
427                 case 30: return 4; // anim_duckwalkbackleft -> anim_duckwalk
428         }
429         LOG_DEBUGF("Frame %d missing in model %s, and we have no fallback - FAIL!", f, this.model);
430         return f;
431 }
432 void CSQCPlayer_FallbackFrame_Apply(entity this)
433 {
434         this.frame = CSQCPlayer_FallbackFrame(this, this.frame);
435         this.frame2 = CSQCPlayer_FallbackFrame(this, this.frame2);
436 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
437         this.frame3 = CSQCPlayer_FallbackFrame(this, this.frame3);
438         this.frame4 = CSQCPlayer_FallbackFrame(this, this.frame4);
439 #endif
440 }
441
442 // FEATURE: auto tag_index
443 .entity tag_entity;
444 .int tag_entity_lastmodelindex;
445 .int tag_index;
446 void CSQCModel_AutoTagIndex_Apply(entity this)
447 {
448         if(this.tag_entity && wasfreed(this.tag_entity))
449                 this.tag_entity = NULL;
450
451         MUTATOR_CALLHOOK(TagIndex_Update, this);
452
453         if(this.tag_networkentity)
454         {
455                 // we are ATTACHED!
456                 bool changed = 0;
457                 if(this.tag_entity.entnum != this.tag_networkentity)
458                 {
459                         this.tag_entity = findfloat(NULL, entnum, this.tag_networkentity);
460                         changed = 1;
461                 }
462
463                 // recursive predraw call to fix issues with forcemodels and LOD if bone indexes mismatch
464                 if(this.tag_entity.classname == "ENT_CLIENT_MODEL")
465                 {
466                         CSQCModel_Hook_PreDraw(this.tag_entity, (this.tag_entity.isplayermodel & ISPLAYER_CLIENT));
467                 }
468
469                 if(this.tag_entity.modelindex != this.tag_entity_lastmodelindex)
470                 {
471                         this.tag_entity_lastmodelindex = this.tag_entity.modelindex;
472                         changed = 1;
473                 }
474                 if(changed)
475                 {
476                         if(this.tag_entity)
477                         {
478                                 // the best part is: IT EXISTS
479                                 if(substring(this.model, 0, 14) == "models/weapons")
480                                 {
481                                         if(substring(this.tag_entity.model, 0, 14) == "models/weapons")
482                                         {
483                                                 this.tag_index = gettagindex(this.tag_entity, "weapon");
484                                                 if(!this.tag_index)
485                                                         this.tag_index = gettagindex(this.tag_entity, "tag_weapon");
486                                                 if(!this.tag_index)
487                                                 {
488                                                         // we need to prevent this from 'appening
489                                                         this.tag_entity = NULL;
490                                                         this.drawmask = 0;
491                                                         LOG_TRACE("h_ model lacks weapon attachment, but v_ model is attached to it");
492                                                 }
493                                         }
494                                         else if((this.tag_entity.isplayermodel & ISPLAYER_MODEL))
495                                         {
496                                                 skeleton_loadinfo(this.tag_entity);
497                                                 this.tag_index = this.tag_entity.bone_weapon;
498                                         }
499                                 }
500
501                                 if(substring(this.tag_entity.model, 0, 14) == "models/weapons")
502                                 {
503                                         this.tag_index = gettagindex(this.tag_entity, "shot");
504                                         if(!this.tag_index)
505                                                 this.tag_index = gettagindex(this.tag_entity, "tag_shot");
506                                 }
507
508                                 MUTATOR_CALLHOOK(TagIndex_Apply, this);
509                         }
510                         else
511                         {
512                                 // damn, see you next frame
513                                 this.drawmask = 0;
514                         }
515                 }
516         }
517 }
518
519 void CSQCModel_Effects_PreUpdate(entity this)
520 {
521         this.effects = this.csqcmodel_effects;
522         this.modelflags = this.csqcmodel_modelflags;
523         this.traileffect = this.csqcmodel_traileffect;
524 }
525 void Reset_ArcBeam();
526 void CSQCModel_Effects_PostUpdate(entity this)
527 {
528         if (this == csqcplayer) {
529                 if (this.csqcmodel_teleported) {
530                         Reset_ArcBeam();
531                 }
532         }
533         this.csqcmodel_effects = this.effects;
534         this.csqcmodel_modelflags = this.modelflags;
535         this.csqcmodel_traileffect = this.traileffect;
536         this.effects = 0;
537         this.modelflags = 0;
538         if(this.csqcmodel_teleported)
539                 Projectile_ResetTrail(this, this.origin);
540 }
541 .int snd_looping;
542 void CSQCModel_Effects_Apply(entity this)
543 {
544         int eff = this.csqcmodel_effects & ~CSQCMODEL_EF_RESPAWNGHOST;
545         int tref = this.csqcmodel_traileffect;
546
547         this.renderflags &= ~(RF_DEPTHHACK | RF_ADDITIVE | RF_FULLBRIGHT | EF_NOSHADOW | RF_USEAXIS);
548         this.effects = 0;
549         this.traileffect = 0;
550
551         if(eff & EF_BRIGHTFIELD)
552                 tref = EFFECT_TR_NEXUIZPLASMA.m_id;
553         // ignoring EF_MUZZLEFLASH
554         if(eff & EF_BRIGHTLIGHT)
555                 adddynamiclight(this.origin, 400, '3 3 3');
556         if(eff & EF_DIMLIGHT)
557                 adddynamiclight(this.origin, 200, '1.5 1.5 1.5');
558         if((eff & EF_NODRAW) || (this.alpha < 0))
559                 this.drawmask = 0;
560         if(eff & EF_ADDITIVE)
561                 this.renderflags |= RF_ADDITIVE;
562         if(eff & EF_BLUE)
563                 adddynamiclight(this.origin, 200, '0.15 0.15 1.5');
564         if(eff & EF_RED)
565                 adddynamiclight(this.origin, 200, '1.5 0.15 0.15');
566         // ignoring EF_NOGUNBOB
567         if(eff & EF_FULLBRIGHT)
568                 this.renderflags |= RF_FULLBRIGHT;
569         if(eff & EF_FLAME)
570         {
571                 boxparticles(particleeffectnum(EFFECT_EF_FLAME), this, this.absmin, this.absmax, this.velocity, this.velocity, bound(0, frametime, 0.1), 0);
572                 //pointparticles(EFFECT_EF_FLAME, this.origin, '0 0 0', bound(0, frametime, 0.1));
573         }
574         if(eff & EF_SHOCK)
575         {
576                 boxparticles(particleeffectnum(EFFECT_ARC_LIGHTNING), this, this.absmin, this.absmax, '0 0 0', '0 0 0', bound(0, frametime, 0.1), 0);
577                 //pointparticles(EFFECT_ARC_LIGHTNING, this.origin, '0 0 0', bound(0, frametime, 0.1));
578         }
579         if(eff & EF_STARDUST)
580         {
581                 boxparticles(particleeffectnum(EFFECT_EF_STARDUST), this, this.absmin, this.absmax, this.velocity, this.velocity, bound(0, frametime, 0.1), 0);
582                 //pointparticles(EFFECT_EF_STARDUST, this.origin, '0 0 0', bound(0, frametime, 0.1));
583         }
584         if(eff & EF_NOSHADOW)
585                 this.renderflags |= RF_NOSHADOW;
586         if(eff & EF_NODEPTHTEST)
587                 this.renderflags |= RF_DEPTHHACK;
588         // ignoring EF_SELECTABLE
589         if(eff & EF_DOUBLESIDED)
590                 this.effects |= EF_DOUBLESIDED;
591         if(eff & EF_NOSELFSHADOW)
592                 this.effects |= EF_NOSELFSHADOW;
593         if(eff & EF_DYNAMICMODELLIGHT)
594                 this.renderflags |= RF_DYNAMICMODELLIGHT;
595         // ignoring EF_UNUSED18, EF_UNUSED19, EF_RESTARTANIM_BIT, EF_TELEPORT_BIT, EF_LOWPRECISION
596         if(this.csqcmodel_modelflags & MF_ROCKET)
597                 tref = EFFECT_TR_ROCKET.m_id;
598         if(this.csqcmodel_modelflags & MF_GRENADE)
599                 tref = EFFECT_TR_GRENADE.m_id;
600         if(this.csqcmodel_modelflags & MF_GIB)
601                 tref = EFFECT_TR_BLOOD.m_id;
602         if(this.csqcmodel_modelflags & MF_ROTATE)
603         {
604                 // This will be hard to replace with MAKE_VECTORS because it's called as part of the predraw function
605                 // as documented in csprogs.h in the engine. The globals can then be read in many places in the engine.
606                 // However MF_ROTATE is currently only used in one place - might be possible to get rid of it entirely.
607                 this.renderflags |= RF_USEAXIS;
608                 makevectors(this.angles + '0 100 0' * fmod(time, 3.6));
609         }
610         if(this.csqcmodel_modelflags & MF_TRACER)
611                 tref = EFFECT_TR_WIZSPIKE.m_id;
612         if(this.csqcmodel_modelflags & MF_ZOMGIB)
613                 tref = EFFECT_TR_SLIGHTBLOOD.m_id;
614         if(this.csqcmodel_modelflags & MF_TRACER2)
615                 tref = EFFECT_TR_KNIGHTSPIKE.m_id;
616         if(this.csqcmodel_modelflags & MF_TRACER3)
617                 tref = EFFECT_TR_VORESPIKE.m_id;
618
619         this.traileffect = tref;
620
621         if(this.drawmask)
622                 Projectile_DrawTrail(this, this.origin);
623         else
624                 Projectile_ResetTrail(this, this.origin);
625
626         if(this.csqcmodel_effects & CSQCMODEL_EF_RESPAWNGHOST)
627                 this.renderflags |= RF_ADDITIVE;
628                 // also special in CSQCPlayer_GlowMod_Apply
629
630         if(this.csqcmodel_modelflags & MF_ROCKET)
631         {
632                 if(!this.snd_looping)
633                 {
634                         sound(this, CH_TRIGGER_SINGLE, SND_JETPACK_FLY, VOL_BASE, autocvar_cl_jetpack_attenuation);
635                         this.snd_looping = CH_TRIGGER_SINGLE;
636                 }
637         }
638         else
639         {
640                 if(this.snd_looping)
641                 {
642                         sound(this, this.snd_looping, SND_Null, VOL_BASE, autocvar_cl_jetpack_attenuation);
643                         this.snd_looping = 0;
644                 }
645         }
646 }
647
648 // general functions
649 .int csqcmodel_predraw_run;
650 .int anim_frame;
651 .int anim_frame1time;
652 .int anim_frame2;
653 .int anim_frame2time;
654 .int anim_saveframe;
655 .int anim_saveframe1time;
656 .int anim_saveframe2;
657 .int anim_saveframe2time;
658 .int anim_prev_pmove_flags;
659 void CSQCModel_Hook_PreDraw(entity this, bool isplayer)
660 {
661         if(this.csqcmodel_predraw_run == framecount)
662                 return;
663         this.csqcmodel_predraw_run = framecount;
664
665         if(!this.modelindex || this.model == "null")
666         {
667                 this.drawmask = 0;
668                 if(this.snd_looping > 0)
669                 {
670                         sound(this, this.snd_looping, SND_Null, VOL_BASE, autocvar_cl_jetpack_attenuation);
671                         this.snd_looping = 0;
672                 }
673                 return;
674         }
675         else
676                 this.drawmask = MASK_NORMAL;
677
678         if((this.isplayermodel & ISPLAYER_MODEL) && this.drawmask) // this checks if it's a player MODEL!
679         {
680                 CSQCPlayer_ModelAppearance_Apply(this, (this.isplayermodel & ISPLAYER_LOCAL));
681                 CSQCModel_LOD_Apply(this, true);
682
683                 if(!isplayer)
684                 {
685                         skeleton_loadinfo(this);
686                         bool doblend = (this.bone_upperbody >= 0);
687                         CSQCPlayer_FallbackFrame_Apply(this);
688                         if(doblend)
689                         {
690                                 skeleton_from_frames(this, this.csqcmodel_isdead);
691                         }
692                         else
693                         {
694                                 free_skeleton_from_frames(this);
695                                 // just in case, clear these (we're animating in frame and frame3)
696                                 this.lerpfrac = 0;
697                                 this.lerpfrac4 = 0;
698                         }
699                 }
700                 else
701                 {
702                         // we know that frame3 and frame4 fields, used by InterpolateAnimation, are left alone - but that is all we know!
703                         skeleton_loadinfo(this);
704                         bool doblend = (this.bone_upperbody >= 0);
705                         bool onground = 0;
706                         if(this == csqcplayer)
707                         {
708                                 if(IS_ONGROUND(this))
709                                         onground = 1;
710                                 this.anim_prev_pmove_flags = this.flags;
711                                 if(this.flags & FL_DUCKED)
712                                         animdecide_setstate(this, this.anim_state | ANIMSTATE_DUCK, false);
713                                 else if(this.anim_state & ANIMSTATE_DUCK)
714                                         animdecide_setstate(this, this.anim_state - ANIMSTATE_DUCK, false);
715                         }
716                         else
717                         {
718                                 tracebox(this.origin + '0 0 1', this.mins, this.maxs, this.origin - '0 0 4', MOVE_NORMAL, this);
719                                 if(trace_startsolid || trace_fraction < 1)
720                                         onground = 1;
721                                 // predicted clients handle smoothing in the prediction code
722                                 this.origin = CSQCModel_ApplyStairSmoothing(this, onground, this.origin);
723                         }
724                         animdecide_load_if_needed(this);
725                         animdecide_setimplicitstate(this, onground);
726                         animdecide_setframes(this, doblend, anim_frame, anim_frame1time, anim_frame2, anim_frame2time);
727                         int sf = 0;
728                         if(this.anim_saveframe != this.anim_frame || this.anim_saveframe1time != this.anim_frame1time)
729                                 sf |= CSQCMODEL_PROPERTY_FRAME;
730                         if(this.anim_saveframe2 != this.anim_frame2 || this.anim_saveframe2time != this.anim_frame2time)
731                                 sf |= CSQCMODEL_PROPERTY_FRAME2;
732                         this.anim_saveframe = this.anim_frame;
733                         this.anim_saveframe1time = this.anim_frame1time;
734                         this.anim_saveframe2 = this.anim_frame2;
735                         this.anim_saveframe2time = this.anim_frame2time;
736                         // Note: we always consider lerpfrac "changed", as it uses fixed values every time anyway.
737                         // This ensures that .frame etc. are always written.
738                         CSQCModel_InterpolateAnimation_2To4_PreNote(this, sf | CSQCMODEL_PROPERTY_LERPFRAC);
739                         this.lerpfrac = (doblend ? 0.5 : 0);
740                         this.frame = this.anim_frame;
741                         this.frame1time = this.anim_frame1time;
742                         this.frame2 = this.anim_frame2;
743                         this.frame2time = this.anim_frame2time;
744                         CSQCModel_InterpolateAnimation_2To4_Note(this, sf | CSQCMODEL_PROPERTY_LERPFRAC, false);
745                         CSQCModel_InterpolateAnimation_2To4_Do(this);
746                         if(doblend)
747                         {
748                                 skeleton_from_frames(this, this.csqcmodel_isdead);
749                         }
750                         else
751                         {
752                                 free_skeleton_from_frames(this);
753                                 // just in case, clear these (we're animating in frame and frame3)
754                                 this.lerpfrac = 0;
755                                 this.lerpfrac4 = 0;
756                         }
757                 }
758         }
759         else
760                 CSQCModel_LOD_Apply(this, false);
761
762         CSQCModel_AutoTagIndex_Apply(this);
763
764         CSQCModel_Effects_Apply(this);
765 }
766
767 void CSQCModel_Hook_PreUpdate(entity this, bool isnew, bool isplayer, bool islocalplayer)
768 {
769         // interpolate v_angle
770         this.iflags |= IFLAG_V_ANGLE_X;
771         // revert to values from server
772         CSQCModel_Effects_PreUpdate(this);
773         if((this.isplayermodel & ISPLAYER_MODEL))
774         {
775                 if(!isplayer)
776                         CSQCPlayer_FallbackFrame_PreUpdate(this);
777                 CSQCPlayer_ModelAppearance_PreUpdate(this);
778         }
779 }
780
781 void CSQCModel_Hook_PostUpdate(entity this, bool isnew, bool isplayer, bool islocalplayer)
782 {
783         // is it a player model? (shared state)
784         bool is_playermodel = (substring(this.model, 0, 14) == "models/player/" || substring(this.model, 0, 17) == "models/ok_player/" || 
785                                                         (substring(this.model, 0, 16) == "models/monsters/" && (this.isplayermodel & BIT(1))));
786         this.isplayermodel = BITSET(this.isplayermodel, ISPLAYER_MODEL, is_playermodel);
787         this.csqcmodel_isdead = false; // workaround for dead players who become a spectator
788
789         // save values set by server
790         if((this.isplayermodel & ISPLAYER_MODEL))
791         {
792                 CSQCPlayer_ModelAppearance_PostUpdate(this);
793                 if(isplayer)
794                         CSQCPlayer_AnimDecide_PostUpdate(this, isnew);
795                 else
796                         CSQCPlayer_FallbackFrame_PostUpdate(this, isnew);
797         }
798         CSQCModel_Effects_PostUpdate(this);
799 }