1 void CSQCModel_Hook_PreDraw();
9 void CSQCPlayer_LOD_Apply(void)
12 if(self.lodmodelindex0 != self.modelindex)
14 string modelname = self.model;
17 vector mi = self.mins;
18 vector ma = self.maxs;
21 self.lodmodelindex0 = self.modelindex;
22 self.lodmodelindex1 = self.modelindex;
23 self.lodmodelindex2 = self.modelindex;
25 // FIXME: this only supports 3-letter extensions
26 s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod1", substring(modelname, -4, 4));
32 self.lodmodelindex1 = self.modelindex;
35 s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod2", substring(modelname, -4, 4));
41 self.lodmodelindex2 = self.modelindex;
44 setmodel(self, modelname); // make everything normal again
45 setsize(self, mi, ma);
49 if(autocvar_cl_playerdetailreduction <= 0)
51 if(autocvar_cl_playerdetailreduction <= -2)
52 self.modelindex = self.lodmodelindex2;
53 else if(autocvar_cl_playerdetailreduction <= -1)
54 self.modelindex = self.lodmodelindex1;
56 self.modelindex = self.lodmodelindex0;
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;
68 self.modelindex = self.lodmodelindex0;
72 // FEATURE: forcemodel (MUST be called BEFORE LOD!)
73 string forceplayermodels_model;
74 float forceplayermodels_modelisgoodmodel;
75 float forceplayermodels_modelindex;
76 float forceplayermodels_skin;
78 string forceplayermodels_mymodel;
79 float forceplayermodels_myisgoodmodel;
80 float forceplayermodels_mymodelindex;
82 float forceplayermodels_attempted;
84 .string forceplayermodels_savemodel;
85 .float forceplayermodels_savemodelindex;
86 .float forceplayermodels_saveskin;
87 .float forceplayermodels_savecolormap;
89 .string forceplayermodels_isgoodmodel_mdl;
90 .float forceplayermodels_isgoodmodel;
92 string forceplayermodels_goodmodel;
93 float forceplayermodels_goodmodelindex;
95 void CSQCPlayer_ForceModel_PreUpdate(void)
97 self.model = self.forceplayermodels_savemodel;
98 self.modelindex = self.forceplayermodels_savemodelindex;
99 self.skin = self.forceplayermodels_saveskin;
100 self.colormap = self.forceplayermodels_savecolormap;
102 void CSQCPlayer_ForceModel_PostUpdate(void)
104 self.forceplayermodels_savemodel = self.model;
105 self.forceplayermodels_savemodelindex = self.modelindex;
106 self.forceplayermodels_saveskin = self.skin;
107 self.forceplayermodels_savecolormap = self.colormap;
109 if(self.forceplayermodels_savemodel != self.forceplayermodels_isgoodmodel_mdl)
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));
117 void CSQCPlayer_ForceModel_Apply(float islocalplayer)
119 // which one is ALWAYS good?
120 if not(forceplayermodels_goodmodel)
124 precache_model(cvar_defstring("_cl_playermodel"));
125 setmodel(e, cvar_defstring("_cl_playermodel"));
126 forceplayermodels_goodmodel = e.model;
127 forceplayermodels_goodmodelindex = e.modelindex;
131 // first, try finding it from the server
132 if(self.forceplayermodels_savemodelindex && self.forceplayermodels_savemodel != "null")
136 if(!isdemo()) // this is mainly cheat protection; not needed for demos
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;
148 // forcemodel finding
149 if(!forceplayermodels_attempted)
151 forceplayermodels_attempted = 1;
153 // only if this failed, find it out on our own
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;
164 if(autocvar_cl_forcemyplayermodel != "" && autocvar_cl_forcemyplayermodel != forceplayermodels_mymodel)
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;
178 cm = self.forceplayermodels_savecolormap;
179 cm = (cm >= 1024) ? cm : (stof(getplayerkeyvalue(self.colormap - 1, "colors")) + 1024);
182 isfriend = (cm == 1024 + 17 * myteam);
184 isfriend = islocalplayer;
186 if(autocvar_cl_forcemyplayermodel != "" && forceplayermodels_myisgoodmodel && isfriend)
188 self.model = forceplayermodels_mymodel;
189 self.modelindex = forceplayermodels_mymodelindex;
190 self.skin = autocvar_cl_forcemyplayerskin;
192 else if(autocvar_cl_forceplayermodels && forceplayermodels_modelisgoodmodel)
194 self.model = forceplayermodels_model;
195 self.modelindex = forceplayermodels_modelindex;
196 self.skin = forceplayermodels_skin;
198 else if(self.forceplayermodels_isgoodmodel)
200 self.model = self.forceplayermodels_savemodel;
201 self.modelindex = self.forceplayermodels_savemodelindex;
202 self.skin = self.forceplayermodels_saveskin;
206 self.model = forceplayermodels_goodmodel;
207 self.modelindex = forceplayermodels_goodmodelindex;
208 self.skin = self.forceplayermodels_saveskin;
211 // forceplayercolors too
214 // own team's color is never forced
215 float forcecolor_friend = 0;
216 float forcecolor_enemy = 0;
217 float teams_count = 0;
220 for(tm = teams.sort_next; tm; tm = tm.sort_next)
221 if(tm.team != FL_SPECTATOR)
224 if(autocvar_cl_forcemyplayercolors)
225 forcecolor_friend = 1024 + autocvar_cl_forcemyplayercolors;
226 if(autocvar_cl_forceplayercolors && teams_count == 2)
227 forcecolor_enemy = 1024 + autocvar__cl_color;
229 if(forcecolor_enemy && !forcecolor_friend)
231 // only enemy color is forced?
232 // verify it is not equal to the friend color
233 if(forcecolor_enemy == 1024 + 17 * myteam)
234 forcecolor_enemy = 0;
237 if(forcecolor_friend && !forcecolor_enemy)
239 // only friend color is forced?
240 // verify it is not equal to the enemy color
241 for(tm = teams.sort_next; tm; tm = tm.sort_next)
242 // note: we even compare against our own team.
243 // if we rejected because we matched our OWN team color,
244 // this is not bad; we then simply keep our color as is
246 if(forcecolor_friend == 1024 + 17 * tm.team)
247 forcecolor_friend = 0;
250 if(cm == 1024 + 17 * myteam)
252 if(forcecolor_friend)
253 self.colormap = forcecolor_friend;
258 self.colormap = forcecolor_enemy;
263 if(autocvar_cl_forcemyplayercolors && islocalplayer)
264 self.colormap = 1024 + autocvar_cl_forcemyplayercolors;
265 else if(autocvar_cl_forceplayercolors)
266 self.colormap = player_localnum + 1;
270 // FEATURE: fallback frames
271 .float csqcmodel_saveframe;
272 .float csqcmodel_saveframe2;
273 .float csqcmodel_saveframe3;
274 .float csqcmodel_saveframe4;
275 .float csqcmodel_framecount;
277 #define IS_DEAD_FRAME(f) ((f) == 0 || (f) == 1)
278 void CSQCPlayer_FallbackFrame_PreUpdate(void)
280 self.frame = self.csqcmodel_saveframe;
281 self.frame2 = self.csqcmodel_saveframe2;
282 self.frame3 = self.csqcmodel_saveframe3;
283 self.frame4 = self.csqcmodel_saveframe4;
285 void CSQCPlayer_FallbackFrame_PostUpdate(float isnew)
287 self.csqcmodel_saveframe = self.frame;
288 self.csqcmodel_saveframe2 = self.frame2;
289 self.csqcmodel_saveframe3 = self.frame3;
290 self.csqcmodel_saveframe4 = self.frame4;
292 // hack for death animations: set their frametime to zero in case a
296 #define FIX_FRAMETIME(f,ft) \
297 if(IS_DEAD_FRAME(self.f) && self.ft != 0 && self.death_time != 0) \
299 self.ft = self.death_time; \
301 FIX_FRAMETIME(frame, frame1time)
302 FIX_FRAMETIME(frame2, frame2time)
303 FIX_FRAMETIME(frame3, frame3time)
304 FIX_FRAMETIME(frame4, frame4time)
306 self.csqcmodel_isdead = IS_DEAD_FRAME(self.frame);
308 float CSQCPlayer_FallbackFrame(float f)
310 if(frameduration(self.modelindex, f) > 0)
311 return f; // goooooood
312 if(frameduration(self.modelindex, 1) <= 0)
313 return f; // this is a static model. We can't fix it if we wanted to
316 case 23: return 11; // anim_melee -> anim_shoot
317 case 24: return 4; // anim_duckwalkbackwards -> anim_duckwalk
318 case 25: return 4; // anim_duckwalkstrafeleft -> anim_duckwalk
319 case 26: return 4; // anim_duckwalkstraferight -> anim_duckwalk
320 case 27: return 4; // anim_duckwalkforwardright -> anim_duckwalk
321 case 28: return 4; // anim_duckwalkforwardleft -> anim_duckwalk
322 case 29: return 4; // anim_duckwalkbackright -> anim_duckwalk
323 case 30: return 4; // anim_duckwalkbackleft -> anim_duckwalk
325 print(sprintf("Frame %d missing in model %s, and we have no fallback - FAIL!\n", f, self.model));
328 void CSQCPlayer_FallbackFrame_Apply(void)
330 self.frame = CSQCPlayer_FallbackFrame(self.frame);
331 self.frame2 = CSQCPlayer_FallbackFrame(self.frame2);
332 self.frame3 = CSQCPlayer_FallbackFrame(self.frame3);
333 self.frame4 = CSQCPlayer_FallbackFrame(self.frame4);
336 // FEATURE: auto tag_index
338 .float tag_entity_lastmodelindex;
340 void CSQCModel_AutoTagIndex_Apply(void)
342 if(self.tag_entity && wasfreed(self.tag_entity))
343 self.tag_entity = world;
345 if(self.tag_networkentity)
349 if(self.tag_entity.entnum != self.tag_networkentity)
351 self.tag_entity = findfloat(world, entnum, self.tag_networkentity);
355 // recursive predraw call to fix issues with forcemodels and LOD if bone indexes mismatch
356 if(self.tag_entity.classname == "csqcmodel")
358 entity oldself = self;
359 self = self.tag_entity;
360 CSQCModel_Hook_PreDraw();
364 if(self.tag_entity.modelindex != self.tag_entity_lastmodelindex)
366 self.tag_entity_lastmodelindex = self.tag_entity.modelindex;
373 // the best part is: IT EXISTS
374 if(substring(self.model, 0, 17) == "models/weapons/v_")
376 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/h_")
378 self.tag_index = gettagindex(self.tag_entity, "weapon");
380 self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
383 // we need to prevent this from 'appening
384 self.tag_entity = world;
386 dprint("h_ model lacks weapon attachment, but v_ model is attached to it\n");
389 else if(self.tag_entity.isplayermodel)
391 self.tag_index = gettagindex(self.tag_entity, "weapon");
393 self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
395 self.tag_index = gettagindex(self.tag_entity, "bip01 r hand");
399 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/v_")
401 self.tag_index = gettagindex(self.tag_entity, "shot");
403 self.tag_index = gettagindex(self.tag_entity, "tag_shot");
408 // damn, see you next frame
415 // FEATURE: EF_NODRAW workalike
416 float EF_BRIGHTFIELD = 1;
417 float EF_BRIGHTLIGHT = 4;
418 float EF_DIMLIGHT = 8;
419 float EF_DOUBLESIDED = 32768;
420 float EF_NOSELFSHADOW = 65536;
421 float EF_DYNAMICMODELLIGHT = 131072;
422 float MF_ROCKET = 1; // leave a trail
423 float MF_GRENADE = 2; // leave a trail
424 float MF_GIB = 4; // leave a trail
425 float MF_ROTATE = 8; // rotate (bonus items)
426 float MF_TRACER = 16; // green split trail
427 float MF_ZOMGIB = 32; // small blood trail
428 float MF_TRACER2 = 64; // orange split trail
429 float MF_TRACER3 = 128; // purple trail
430 .float csqcmodel_effects;
431 .float csqcmodel_modelflags;
432 void CSQCModel_Effects_PreUpdate(void)
434 self.effects = self.csqcmodel_effects;
435 self.modelflags = self.csqcmodel_modelflags;
437 void CSQCModel_Effects_PostUpdate(void)
439 self.csqcmodel_effects = self.effects;
440 self.csqcmodel_modelflags = self.modelflags;
443 if(self.csqcmodel_teleported)
444 Projectile_ResetTrail(self.origin);
446 void CSQCModel_Effects_Apply(void)
448 float eff = self.csqcmodel_effects;
449 eff &~= CSQCMODEL_EF_RESPAWNGHOST;
451 self.renderflags &~= (RF_DEPTHHACK | RF_ADDITIVE | RF_FULLBRIGHT | EF_NOSHADOW | RF_USEAXIS);
453 self.traileffect = 0;
455 if(eff & EF_BRIGHTFIELD)
456 self.traileffect = particleeffectnum("TR_NEXUIZPLASMA");
457 // ignoring EF_MUZZLEFLASH
458 if(eff & EF_BRIGHTLIGHT)
459 adddynamiclight(self.origin, 400, '3 3 3');
460 if(eff & EF_DIMLIGHT)
461 adddynamiclight(self.origin, 200, '1.5 1.5 1.5');
462 if((eff & EF_NODRAW) || (self.alpha < 0))
464 if(eff & EF_ADDITIVE)
465 self.renderflags |= RF_ADDITIVE;
467 adddynamiclight(self.origin, 200, '0.15 0.15 1.5');
469 adddynamiclight(self.origin, 200, '1.5 0.15 0.15');
470 // ignoring EF_NOGUNBOB
471 if(eff & EF_FULLBRIGHT)
472 self.renderflags |= RF_FULLBRIGHT;
474 pointparticles(particleeffectnum("EF_FLAME"), self.origin, '0 0 0', bound(0, frametime, 0.1));
475 if(eff & EF_STARDUST)
476 pointparticles(particleeffectnum("EF_STARDUST"), self.origin, '0 0 0', bound(0, frametime, 0.1));
477 if(eff & EF_NOSHADOW)
478 self.renderflags |= RF_NOSHADOW;
479 if(eff & EF_NODEPTHTEST)
480 self.renderflags |= RF_DEPTHHACK;
481 // ignoring EF_SELECTABLE
482 if(eff & EF_DOUBLESIDED)
483 self.effects |= EF_DOUBLESIDED;
484 if(eff & EF_NOSELFSHADOW)
485 self.effects |= EF_NOSELFSHADOW;
486 if(eff & EF_DYNAMICMODELLIGHT)
487 self.renderflags |= RF_DYNAMICMODELLIGHT;
488 // ignoring EF_UNUSED18, EF_UNUSED19, EF_RESTARTANIM_BIT, EF_TELEPORT_BIT, EF_LOWPRECISION
489 if(self.csqcmodel_modelflags & MF_ROCKET)
490 self.traileffect = particleeffectnum("TR_ROCKET");
491 if(self.csqcmodel_modelflags & MF_GRENADE)
492 self.traileffect = particleeffectnum("TR_GRENADE");
493 if(self.csqcmodel_modelflags & MF_GIB)
494 self.traileffect = particleeffectnum("TR_BLOOD");
495 if(self.csqcmodel_modelflags & MF_ROTATE)
497 self.renderflags |= RF_USEAXIS;
498 makevectors(self.angles + '0 100 0' * fmod(time, 3.6));
500 if(self.csqcmodel_modelflags & MF_TRACER)
501 self.traileffect = particleeffectnum("TR_WIZSPIKE");
502 if(self.csqcmodel_modelflags & MF_ZOMGIB)
503 self.traileffect = particleeffectnum("TR_SLIGHTBLOOD");
504 if(self.csqcmodel_modelflags & MF_TRACER2)
505 self.traileffect = particleeffectnum("TR_KNIGHTSPIKE");
506 if(self.csqcmodel_modelflags & MF_TRACER3)
507 self.traileffect = particleeffectnum("TR_VORESPIKE");
510 Projectile_DrawTrail(self.origin);
512 Projectile_ResetTrail(self.origin);
514 if(self.csqcmodel_effects & CSQCMODEL_EF_RESPAWNGHOST)
515 self.renderflags |= RF_ADDITIVE;
516 // also special in CSQCPlayer_GlowMod_Apply
519 // FEATURE: auto glowmod
521 void CSQCPlayer_GlowMod_Apply(void)
523 float cm = self.colormap;
525 if(self.csqcmodel_effects & CSQCMODEL_EF_RESPAWNGHOST)
528 if(self.colormap > 0)
529 self.glowmod = colormapPaletteColor(((self.colormap >= 1024) ? self.colormap : stof(getplayerkeyvalue(self.colormap - 1, "colors"))) & 0x0F, TRUE) * 2;
531 self.glowmod = '1 1 1';
533 if(autocvar_cl_deathglow > 0)
534 if(self.csqcmodel_isdead)
536 self.glowmod = self.glowmod * bound(0, 1 - (time - self.death_time) / autocvar_cl_deathglow, 1);
537 // prevent the zero vector
538 self.glowmod_x = max(self.glowmod_x, 0.0001);
539 self.glowmod_y = max(self.glowmod_y, 0.0001);
540 self.glowmod_z = max(self.glowmod_z, 0.0001);
545 .float csqcmodel_predraw_run;
546 void CSQCModel_Hook_PreDraw()
548 if(self.csqcmodel_predraw_run == framecount)
550 self.csqcmodel_predraw_run = framecount;
552 if(!self.modelindex || self.model == "null")
558 self.drawmask = MASK_NORMAL;
560 if(self.isplayermodel) // this checks if it's a player MODEL!
562 CSQCPlayer_ForceModel_Apply(self.entnum == player_localnum + 1);
563 CSQCPlayer_GlowMod_Apply();
564 CSQCPlayer_LOD_Apply();
565 CSQCPlayer_FallbackFrame_Apply();
568 CSQCModel_AutoTagIndex_Apply();
570 CSQCModel_Effects_Apply();
573 void CSQCModel_Hook_PreUpdate(float isnew, float isplayer, float islocalplayer)
575 // revert to values from server
576 CSQCModel_Effects_PreUpdate();
577 if(self.isplayermodel)
579 CSQCPlayer_FallbackFrame_PreUpdate();
580 CSQCPlayer_ForceModel_PreUpdate();
584 void CSQCModel_Hook_PostUpdate(float isnew, float isplayer, float islocalplayer)
586 // is it a player model? (shared state)
587 self.isplayermodel = (substring(self.model, 0, 14) == "models/player/" || substring(self.model, 0, 17) == "models/ok_player/");
589 // save values set by server
590 if(self.isplayermodel)
592 CSQCPlayer_ForceModel_PostUpdate();
593 CSQCPlayer_FallbackFrame_PostUpdate(isnew);
595 CSQCModel_Effects_PostUpdate();