]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/csqcmodel_hooks.qc
refactor csqcplayers code; move frame forcing clientside
[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
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         if(self.forceplayermodels_savemodelindex && self.forceplayermodels_savemodel != "null")
97         {
98                 if(islocalplayer)
99                 {
100                         // trust server's idea of "own player model"
101                         forceplayermodels_model = self.model;
102                         forceplayermodels_modelindex = self.modelindex;
103                         forceplayermodels_skin = self.skin;
104                         forceplayermodels_attempted = 1;
105                 }
106         }
107
108         // forcemodel finding
109         if(!forceplayermodels_attempted)
110         {
111                 // only if this failed, find it out on our own
112                 entity e;
113                 e = spawn();
114                 setmodel(e, autocvar__cl_playermodel); // this is harmless, see below
115                 forceplayermodels_model = e.model;
116                 forceplayermodels_modelindex = e.modelindex;
117                 forceplayermodels_skin = autocvar__cl_playerskin;
118                 forceplayermodels_attempted = 1;
119                 remove(e);
120         }
121
122         // apply it
123         if(autocvar_cl_forceplayermodels && forceplayermodels_modelindex)
124         {
125                 self.model = forceplayermodels_model;
126                 self.modelindex = forceplayermodels_modelindex;
127                 self.skin = forceplayermodels_skin;
128         }
129 }
130
131 // FEATURE: fallback frames
132 .float csqcmodel_saveframe;
133 .float csqcmodel_saveframe2;
134 .float csqcmodel_saveframe3;
135 .float csqcmodel_saveframe4;
136 .float csqcmodel_framecount;
137 void CSQCPlayer_FallbackFrame_PreUpdate(void)
138 {
139         self.frame = self.csqcmodel_saveframe;
140         self.frame2 = self.csqcmodel_saveframe2;
141         self.frame3 = self.csqcmodel_saveframe3;
142         self.frame4 = self.csqcmodel_saveframe4;
143 }
144 void CSQCPlayer_FallbackFrame_PostUpdate(void)
145 {
146         self.csqcmodel_saveframe = self.frame;
147         self.csqcmodel_saveframe2 = self.frame2;
148         self.csqcmodel_saveframe3 = self.frame3;
149         self.csqcmodel_saveframe4 = self.frame4;
150 }
151 float CSQCPlayer_FallbackFrame(float f)
152 {
153         if(frameduration(self.modelindex, f) > 0)
154                 return f; // goooooood
155         switch(f)
156         {
157                 case 23: return 11; // anim_melee -> anim_shoot
158                 case 24: return 4; // anim_duckwalkbackwards -> anim_duckwalk
159                 case 25: return 4; // anim_duckwalkstrafeleft -> anim_duckwalk
160                 case 26: return 4; // anim_duckwalkstraferight -> anim_duckwalk
161                 case 27: return 4; // anim_duckwalkforwardright -> anim_duckwalk
162                 case 28: return 4; // anim_duckwalkforwardleft -> anim_duckwalk
163                 case 29: return 4; // anim_duckwalkbackright -> anim_duckwalk
164                 case 30: return 4; // anim_duckwalkbackleft -> anim_duckwalk
165         }
166         print(sprintf("Frame %d missing in model %s, and we have no fallback - FAIL!\n", f, self.model));
167         return f;
168 }
169 void CSQCPlayer_FallbackFrame_Apply(void)
170 {
171         self.frame = CSQCPlayer_FallbackFrame(self.frame);
172         self.frame2 = CSQCPlayer_FallbackFrame(self.frame2);
173         self.frame3 = CSQCPlayer_FallbackFrame(self.frame3);
174         self.frame4 = CSQCPlayer_FallbackFrame(self.frame4);
175 }
176
177 // FEATURE: auto glowmod
178 .vector glowmod;
179 void CSQCPlayer_GlowMod_Apply(void)
180 {
181         if(self.colormap > 0)
182                 self.glowmod = colormapPaletteColor(((self.colormap >= 1024) ? self.colormap : stof(getplayerkeyvalue(self.colormap - 1, "colors"))) & 0x0F, TRUE) * 2;
183         else
184                 self.glowmod = '1 1 1';
185 }
186
187 // FEATURE: auto tag_index
188 .entity tag_entity;
189 .float tag_index;
190 void CSQCModel_AutoTagIndex_Apply(void)
191 {
192         if(self.tag_entity && wasfreed(self.tag_entity))
193                 self.tag_entity = world;
194
195         if(self.tag_networkentity)
196         {
197                 // we are ATTACHED!
198                 if(self.tag_entity.entnum != self.tag_networkentity)
199                 {
200                         // to something NEW NEW NEW NEW!
201                         self.tag_entity = findfloat(world, entnum, self.tag_networkentity);
202                         if(self.tag_entity)
203                         {
204                                 // the best part is: IT EXISTS
205                                 if(substring(self.model, 0, 17) == "models/weapons/v_")
206                                         if(substring(self.tag_entity.model, 0, 17) == "models/weapons/h_")
207                                         {
208                                                 self.tag_index = gettagindex(self.tag_entity, "weapon");
209                                                 if(!self.tag_index)
210                                                         self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
211                                                 if(!self.tag_index)
212                                                 {
213                                                         // we need to prevent this from 'appening
214                                                         self.tag_entity = world;
215                                                         self.drawmask = 0;
216                                                         dprint("h_ model lacks weapon attachment, but v_ model is attached to it\n");
217                                                 }
218                                         }
219
220                                 if(substring(self.model, 0, 17) == "models/weapons/v_")
221                                         if(substring(self.tag_entity.model, 0, 14) == "models/player/")
222                                         {
223                                                 self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
224                                                 if(!self.tag_index)
225                                                         self.tag_index = gettagindex(self.tag_entity, "bip01 r hand");
226                                         }
227
228                                 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/v_")
229                                 {
230                                         self.tag_index = gettagindex(self.tag_entity, "shot");
231                                         if(!self.tag_index)
232                                                 self.tag_index = gettagindex(self.tag_entity, "tag_shot");
233                                 }
234                         }
235                         else
236                         {
237                                 // damn, see you next frame
238                                 self.drawmask = 0;
239                         }
240                 }
241         }
242 }
243
244 // general functions
245 void CSQCModel_Hook_PreDraw(float isplayer, float islocalplayer)
246 {
247         if(!self.modelindex || self.model == "null")
248         {
249                 self.drawmask = 0;
250                 return;
251         }
252         else
253                 self.drawmask = MASK_NORMAL;
254
255         if(isplayer)
256         {
257                 CSQCPlayer_GlowMod_Apply();
258                 CSQCPlayer_ForceModel_Apply(islocalplayer);
259                 CSQCPlayer_LOD_Apply();
260                 CSQCPlayer_FallbackFrame_Apply();
261         }
262
263         if(!isplayer)
264                 CSQCModel_AutoTagIndex_Apply();
265 }
266
267 void CSQCModel_Hook_PreUpdate(float isplayer, float islocalplayer)
268 {
269         if(isplayer)
270         {
271                 // revert to values from server
272                 CSQCPlayer_ForceModel_PreUpdate();
273                 CSQCPlayer_FallbackFrame_PreUpdate();
274         }
275 }
276
277 void CSQCModel_Hook_PostUpdate(float isplayer, float islocalplayer)
278 {
279         if(isplayer)
280         {
281                 // save values set by server
282                 CSQCPlayer_ForceModel_PostUpdate();
283                 CSQCPlayer_FallbackFrame_PostUpdate();
284         }
285 }