]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/csqcmodel_hooks.qc
Merge remote branch 'origin/master' into samual/updatecommands
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / csqcmodel_hooks.qc
1
2
3 // FEATURE: LOD
4 .float lodmodelindex0;
5 .float lodmodelindex1;
6 .float lodmodelindex2;
7 void CSQCPlayer_LOD_Apply(void)
8 {
9         // LOD model loading
10         if(self.lodmodelindex0 != self.modelindex)
11         {
12                 string modelname = self.model;
13                 string s;
14
15                 if(!fexists(modelname))
16                 {
17                         print(sprintf(_("Trying to use non existing model %s. "), modelname));
18                         modelname = cvar_defstring("_cl_playermodel");
19                         print(sprintf(_("Reverted to %s."), modelname));
20                 }
21
22                 // set modelindex
23                 self.lodmodelindex0 = self.modelindex;
24                 self.lodmodelindex1 = self.modelindex;
25                 self.lodmodelindex2 = self.modelindex;
26
27                 // FIXME: this only supports 3-letter extensions
28                 s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod1", substring(modelname, -4, 4));
29                 if(fexists(s))
30                 {
31                         precache_model(s);
32                         setmodel(self, s);
33                         if(self.modelindex)
34                                 self.lodmodelindex1 = self.modelindex;
35                 }
36
37                 s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod2", substring(modelname, -4, 4));
38                 if(fexists(s))
39                 {
40                         precache_model(s);
41                         setmodel(self, s);
42                         if(self.modelindex)
43                                 self.lodmodelindex2 = self.modelindex;
44                 }
45
46                 setmodel(self, modelname); // make everything normal again
47         }
48
49         // apply LOD
50         if(autocvar_cl_playerdetailreduction <= 0)
51         {
52                 if(autocvar_cl_playerdetailreduction <= -2)
53                         self.modelindex = self.lodmodelindex2;
54                 else if(autocvar_cl_playerdetailreduction <= -1)
55                         self.modelindex = self.lodmodelindex1;
56                 else
57                         self.modelindex = self.lodmodelindex0;
58         }
59         else
60         {
61                 float distance = vlen(self.origin - other.origin);
62                 float f = (distance + 100.0) * autocvar_cl_playerdetailreduction;
63                 f *= 1.0 / bound(0.01, view_quality, 1);
64                 if(f > autocvar_cl_loddistance2)
65                         self.modelindex = self.lodmodelindex2;
66                 else if(f > autocvar_cl_loddistance1)
67                         self.modelindex = self.lodmodelindex1;
68                 else
69                         self.modelindex = self.lodmodelindex0;
70         }
71 }
72
73 // FEATURE: forcemodel (MUST be called BEFORE LOD!)
74 string forceplayermodels_model;
75 float forceplayermodels_modelindex;
76 float forceplayermodels_skin;
77 float forceplayermodels_attempted;
78 .string forceplayermodels_savemodel;
79 .float forceplayermodels_savemodelindex;
80 .float forceplayermodels_saveskin;
81 void CSQCPlayer_ForceModel_PreUpdate(void)
82 {
83         self.model = self.forceplayermodels_savemodel;
84         self.modelindex = self.forceplayermodels_savemodelindex;
85         self.skin = self.forceplayermodels_saveskin;
86 }
87 void CSQCPlayer_ForceModel_PostUpdate(void)
88 {
89         self.forceplayermodels_savemodel = self.model;
90         self.forceplayermodels_savemodelindex = self.modelindex;
91         self.forceplayermodels_saveskin = self.skin;
92 }
93 void CSQCPlayer_ForceModel_Apply(float islocalplayer)
94 {
95         // first, try finding it from the server
96
97         if(self.forceplayermodels_savemodelindex && self.forceplayermodels_savemodel != "null")
98         {
99                 if(islocalplayer)
100                 {
101                         // trust server's idea of "own player model"
102                         forceplayermodels_model = self.model;
103                         forceplayermodels_modelindex = self.modelindex;
104                         forceplayermodels_skin = self.skin;
105                         forceplayermodels_attempted = 1;
106                 }
107         }
108
109         // forcemodel finding
110         if(!forceplayermodels_attempted)
111         {
112                 // only if this failed, find it out on our own
113                 entity e;
114                 e = spawn();
115                 setmodel(e, autocvar__cl_playermodel); // this is harmless, see below
116                 forceplayermodels_model = e.model;
117                 forceplayermodels_modelindex = e.modelindex;
118                 forceplayermodels_skin = autocvar__cl_playerskin;
119                 forceplayermodels_attempted = 1;
120                 remove(e);
121         }
122
123         // apply it
124         if(autocvar_cl_forceplayermodels && forceplayermodels_modelindex)
125         {
126                 self.model = forceplayermodels_model;
127                 self.modelindex = forceplayermodels_modelindex;
128                 self.skin = forceplayermodels_skin;
129         }
130         else
131         {
132                 self.model = self.forceplayermodels_savemodel;
133                 self.modelindex = self.forceplayermodels_savemodelindex;
134                 self.skin = self.forceplayermodels_saveskin;
135         }
136 }
137
138 // FEATURE: fallback frames
139 .float csqcmodel_saveframe;
140 .float csqcmodel_saveframe2;
141 .float csqcmodel_saveframe3;
142 .float csqcmodel_saveframe4;
143 .float csqcmodel_framecount;
144 void CSQCPlayer_FallbackFrame_PreUpdate(void)
145 {
146         self.frame = self.csqcmodel_saveframe;
147         self.frame2 = self.csqcmodel_saveframe2;
148         self.frame3 = self.csqcmodel_saveframe3;
149         self.frame4 = self.csqcmodel_saveframe4;
150 }
151 void CSQCPlayer_FallbackFrame_PostUpdate(void)
152 {
153         self.csqcmodel_saveframe = self.frame;
154         self.csqcmodel_saveframe2 = self.frame2;
155         self.csqcmodel_saveframe3 = self.frame3;
156         self.csqcmodel_saveframe4 = self.frame4;
157 }
158 float CSQCPlayer_FallbackFrame(float f)
159 {
160         if(frameduration(self.modelindex, f) > 0)
161                 return f; // goooooood
162         switch(f)
163         {
164                 case 23: return 11; // anim_melee -> anim_shoot
165                 case 24: return 4; // anim_duckwalkbackwards -> anim_duckwalk
166                 case 25: return 4; // anim_duckwalkstrafeleft -> anim_duckwalk
167                 case 26: return 4; // anim_duckwalkstraferight -> anim_duckwalk
168                 case 27: return 4; // anim_duckwalkforwardright -> anim_duckwalk
169                 case 28: return 4; // anim_duckwalkforwardleft -> anim_duckwalk
170                 case 29: return 4; // anim_duckwalkbackright -> anim_duckwalk
171                 case 30: return 4; // anim_duckwalkbackleft -> anim_duckwalk
172         }
173         print(sprintf("Frame %d missing in model %s, and we have no fallback - FAIL!\n", f, self.model));
174         return f;
175 }
176 void CSQCPlayer_FallbackFrame_Apply(void)
177 {
178         self.frame = CSQCPlayer_FallbackFrame(self.frame);
179         self.frame2 = CSQCPlayer_FallbackFrame(self.frame2);
180         self.frame3 = CSQCPlayer_FallbackFrame(self.frame3);
181         self.frame4 = CSQCPlayer_FallbackFrame(self.frame4);
182 }
183
184 // FEATURE: auto glowmod
185 .vector glowmod;
186 void CSQCPlayer_GlowMod_Apply(void)
187 {
188         if(self.colormap > 0)
189                 self.glowmod = colormapPaletteColor(((self.colormap >= 1024) ? self.colormap : stof(getplayerkeyvalue(self.colormap - 1, "colors"))) & 0x0F, TRUE) * 2;
190         else
191                 self.glowmod = '1 1 1';
192 }
193
194 // FEATURE: auto tag_index
195 .entity tag_entity;
196 .float tag_entity_lastmodelindex;
197 .float tag_index;
198 void CSQCModel_AutoTagIndex_Apply(void)
199 {
200         if(self.tag_entity && wasfreed(self.tag_entity))
201                 self.tag_entity = world;
202
203         if(self.tag_networkentity)
204         {
205                 // we are ATTACHED!
206                 float changed = 0;
207                 if(self.tag_entity.entnum != self.tag_networkentity)
208                 {
209                         self.tag_entity = findfloat(world, entnum, self.tag_networkentity);
210                         changed = 1;
211                 }
212                 if(self.tag_entity.modelindex != self.tag_entity_lastmodelindex)
213                 {
214                         self.tag_entity_lastmodelindex = self.tag_entity.modelindex;
215                         changed = 1;
216                 }
217                 if(changed)
218                 {
219                         if(self.tag_entity)
220                         {
221                                 // the best part is: IT EXISTS
222                                 if(substring(self.model, 0, 17) == "models/weapons/v_")
223                                         if(substring(self.tag_entity.model, 0, 17) == "models/weapons/h_")
224                                         {
225                                                 self.tag_index = gettagindex(self.tag_entity, "weapon");
226                                                 if(!self.tag_index)
227                                                         self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
228                                                 if(!self.tag_index)
229                                                 {
230                                                         // we need to prevent this from 'appening
231                                                         self.tag_entity = world;
232                                                         self.drawmask = 0;
233                                                         dprint("h_ model lacks weapon attachment, but v_ model is attached to it\n");
234                                                 }
235                                         }
236
237                                 if(substring(self.model, 0, 17) == "models/weapons/v_")
238                                         if(substring(self.tag_entity.model, 0, 14) == "models/player/")
239                                         {
240                                                 self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
241                                                 if(!self.tag_index)
242                                                         self.tag_index = gettagindex(self.tag_entity, "bip01 r hand");
243                                         }
244
245                                 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/v_")
246                                 {
247                                         self.tag_index = gettagindex(self.tag_entity, "shot");
248                                         if(!self.tag_index)
249                                                 self.tag_index = gettagindex(self.tag_entity, "tag_shot");
250                                 }
251                         }
252                         else
253                         {
254                                 // damn, see you next frame
255                                 self.drawmask = 0;
256                         }
257                 }
258         }
259 }
260
261 // general functions
262 void CSQCModel_Hook_PreDraw(float isplayer, float islocalplayer)
263 {
264         if(!self.modelindex || self.model == "null")
265         {
266                 self.drawmask = 0;
267                 return;
268         }
269         else
270                 self.drawmask = MASK_NORMAL;
271
272         if(isplayer)
273         {
274                 CSQCPlayer_GlowMod_Apply();
275                 CSQCPlayer_ForceModel_Apply(islocalplayer);
276                 CSQCPlayer_LOD_Apply();
277                 CSQCPlayer_FallbackFrame_Apply();
278         }
279
280         if(!isplayer)
281                 CSQCModel_AutoTagIndex_Apply();
282 }
283
284 void CSQCModel_Hook_PreUpdate(float isplayer, float islocalplayer)
285 {
286         if(isplayer)
287         {
288                 // revert to values from server
289                 CSQCPlayer_ForceModel_PreUpdate();
290                 CSQCPlayer_FallbackFrame_PreUpdate();
291         }
292 }
293
294 void CSQCModel_Hook_PostUpdate(float isplayer, float islocalplayer)
295 {
296         if(isplayer)
297         {
298                 // save values set by server
299                 CSQCPlayer_ForceModel_PostUpdate();
300                 CSQCPlayer_FallbackFrame_PostUpdate();
301         }
302 }