]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/miscfunctions.qc
improved the shot origin adjuster:
[divverent/nexuiz.git] / data / qcsrc / client / miscfunctions.qc
1 var float(string text, float handleColors) stringwidth;
2
3 entity players;
4 entity teams;
5
6 void AuditLists()
7 {
8         entity e;
9         entity prev;
10
11         prev = players;
12         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
13         {
14                 if(prev != e.sort_prev)
15                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
16         }
17
18         prev = teams;
19         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
20         {
21                 if(prev != e.sort_prev)
22                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
23         }
24 }
25
26
27 float RegisterPlayer(entity player)
28 {
29         entity pl;
30         AuditLists();
31         for(pl = players.sort_next; pl; pl = pl.sort_next)
32                 if(pl == player)
33                         error("Player already registered!");
34         player.sort_next = players.sort_next;
35         player.sort_prev = players;
36         if(players.sort_next)
37                 players.sort_next.sort_prev = player;
38         players.sort_next = player;
39         AuditLists();
40         return true;
41 }
42
43 void RemovePlayer(entity player)
44 {
45         entity pl, parent;
46         AuditLists();
47         parent = players;
48         for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
49                 parent = pl;
50
51         if(!pl)
52         {
53                 error("Trying to remove a player which is not in the playerlist!");
54                 return;
55         }
56         parent.sort_next = player.sort_next;
57         if(player.sort_next)
58                 player.sort_next.sort_prev = parent;
59         AuditLists();
60 }
61
62 void MoveToLast(entity e)
63 {
64         AuditLists();
65         other = e.sort_next;
66         while(other)
67         {
68                 SORT_SWAP(other, e);
69                 other = e.sort_next;
70         }
71         AuditLists();
72 }
73
74 float RegisterTeam(entity Team)
75 {
76         entity tm;
77         AuditLists();
78         for(tm = teams.sort_next; tm; tm = tm.sort_next)
79                 if(tm == Team)
80                         error("Team already registered!");
81         Team.sort_next = teams.sort_next;
82         Team.sort_prev = teams;
83         if(teams.sort_next)
84                 teams.sort_next.sort_prev = Team;
85         teams.sort_next = Team;
86         AuditLists();
87         return true;
88 }
89
90 void RemoveTeam(entity Team)
91 {
92         entity tm, parent;
93         AuditLists();
94         parent = teams;
95         for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
96                 parent = tm;
97
98         if(!tm)
99         {
100                 print("Trying to remove a team which is not in the teamlist!");
101                 return;
102         }
103         parent.sort_next = Team.sort_next;
104         if(Team.sort_next)
105                 Team.sort_next.sort_prev = parent;
106         AuditLists();
107 }
108
109 entity GetTeam(float Team, float add)
110 {
111         float num;
112         entity tm;
113         num = (Team == COLOR_SPECTATOR) ? 16 : Team;
114         if(teamslots[num])
115                 return teamslots[num];
116         if not(add)
117                 return NULL;
118         tm = spawn();
119         tm.team = Team;
120         teamslots[num] = tm;
121         RegisterTeam(tm);
122         return tm;
123 }
124
125 float stringwidth_oldfont(string text, float handleColors)
126 {
127         float i, len, ch, width;
128         len = strlen(text);
129         if(!handleColors)
130                 return len;
131         width = 0;
132         for(i = 0; i < len; ++i)
133         {
134                 if(substring(text, i, 1) == "^")
135                 {
136                         ch = str2chr(text, i+1);
137                         if(ch >= '0' && ch <= '9')
138                                 ++i;
139                         else if(i+4 < len && ch == 'x')
140                         {
141                                 ch = str2chr(text, i+2);
142                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
143                                 {
144                                         ch = str2chr(text, i+3);
145                                         if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
146                                         {
147                                                 ch = str2chr(text, i+4);
148                                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
149                                                         i+=4;
150                                                 else ++width;
151                                         } else ++width;
152                                 } else ++width;
153                         } else ++width;
154                 } else ++width;
155         }
156         return width;
157 }
158
159 void CSQC_CheckEngine()
160 {
161         /*
162         registercvar("csqc_flags", "0");
163         csqc_flags = cvar("csqc_flags");
164         */
165
166         csqc_flags = 0;
167         
168         if(checkextension("DP_SV_WRITEPICTURE"))
169         {
170                 stringwidth = stringwidth_engine;
171                 sbar_font = FONT_USER+1;
172                 sbar_bigfont = FONT_USER+2;
173                 csqc_flags |= CSQC_FLAG_READPICTURE;
174         } else {
175                 stringwidth = stringwidth_oldfont;
176                 sbar_font = FONT_DEFAULT;
177                 sbar_bigfont = FONT_DEFAULT;
178         }
179
180         if(strlennocol("^xFFF") == 0)
181                 csqc_flags |= CSQC_FLAG_COLORCODES;
182 }
183
184 vector Sbar_GetFontsize()
185 {
186         if(csqc_flags & CSQC_FLAG_READPICTURE)
187         {
188                 vector v;
189                 v = stov(cvar_string("sbar_fontsize"));
190                 if(v_x == 0)
191                         v = '8 8 0';
192                 if(v_y == 0)
193                         v_y = v_x;
194                 v_z = 0;
195                 return v;
196         }
197         return '8 8 0' ;
198 }
199
200 float Sbar_GetWidth(float teamcolumnwidth)
201 {
202         if(csqc_flags & CSQC_FLAG_READPICTURE)
203         {
204                 float f;
205                 f = stof(cvar_string("sbar_width"));
206                 if(f == 0)
207                         f = 640;
208                 if(f < 320)
209                         f = 320;
210                 if(f > vid_conwidth - 2 * teamcolumnwidth)
211                         f = vid_conwidth - 2 * teamcolumnwidth;
212                 return f;
213         }
214         return 640;
215 }
216
217 float PreviewExists(string name)
218 {
219         float f;
220         string file;
221
222         if(cvar("cl_readpicture_force"))
223                 return false;
224
225         file = strcat(name, ".tga");
226         f = fopen(file, FILE_READ);
227         if(f >= 0)
228         {
229                 fclose(f);
230                 return true;
231         }
232         file = strcat(name, ".png");
233         f = fopen(file, FILE_READ);
234         if(f >= 0)
235         {
236                 fclose(f);
237                 return true;
238         }
239         file = strcat(name, ".jpg");
240         f = fopen(file, FILE_READ);
241         if(f >= 0)
242         {
243                 fclose(f);
244                 return true;
245         }
246         file = strcat(name, ".pcx");
247         f = fopen(file, FILE_READ);
248         if(f >= 0)
249         {
250                 fclose(f);
251                 return true;
252         }
253         return false;
254 }
255
256 float PI      = 3.14159265359;
257 float DEG2RAD = 0.01745329252;
258 vector rotate(vector v, float a)
259 {
260         vector w;
261         // FTEQCC SUCKS AGAIN
262         w_x =      v_x * cos(a) + v_y * sin(a);
263         w_y = -1 * v_x * sin(a) + v_y * cos(a);
264         return w;
265 }
266
267 float ColorTranslateMode;
268
269 string ColorTranslateRGB(string s)
270 {
271         if not(ColorTranslateMode & 2)
272         if(csqc_flags & CSQC_FLAG_COLORCODES)
273         {
274                 if(ColorTranslateMode & 1)
275                         return strdecolorize(s);
276                 else
277                         return s;
278         }
279         
280         // running on an OLD engine!
281         // must translate ^xRGB codes to regular color codes
282         float i, n;
283         string s2, ch, ch2;
284         vector theTempColor, hsv;
285         float component;
286
287         s2 = "";
288
289         n = strlen(s);
290         for(i = 0; i < n; ++i)
291         {
292                 ch = substring(s, i, 1);
293                 if(ch == "^")
294                 {
295                         ch2 = substring(s, i+1, 1);
296                         if(ch2 == "^")
297                         {
298                                 s2 = strcat(s2, ch, ch2);
299                         }
300                         else if(ch2 == "0" || stof(ch2)) // digit?
301                         {
302                                 if not(ColorTranslateMode & 1)
303                                         s2 = strcat(s2, ch, ch2);
304                         }
305                         else if(ch2 == "x") // ^x found
306                         {
307                                 theTempColor = '0 0 0';
308                                 
309                                 component = HEXDIGIT_TO_DEC(substring(s, i+2, 1));
310                                 if (component >= 0) // ^xr found
311                                 {
312                                         theTempColor_x = component/15;
313                                         
314                                         component = HEXDIGIT_TO_DEC(substring(s, i+3, 1));
315                                         if (component >= 0) // ^xrg found
316                                         {
317                                                 theTempColor_y = component/15;
318                                                 
319                                                 component = HEXDIGIT_TO_DEC(substring(s, i+4, 1));
320                                                 if (component >= 0) // ^xrgb found
321                                                 {
322                                                         theTempColor_z = component/15;
323
324                                                         if not(ColorTranslateMode & 1)
325                                                         {
326                                                                 hsv = rgb_to_hsv(theTempColor);
327
328                                                                 if(hsv_y < 0.2)
329                                                                 {
330                                                                         if(hsv_z < 0.5)
331                                                                                 s2 = strcat(s2, "^0");
332                                                                         else
333                                                                                 s2 = strcat(s2, "^7");
334                                                                 }
335                                                                 else
336                                                                 {
337                                                                         if(hsv_x < 0.6)
338                                                                                 s2 = strcat(s2, "^1");
339                                                                         else if(hsv_x < 1.33333333333333333333)
340                                                                                 s2 = strcat(s2, "^3");
341                                                                         else if(hsv_x < 2.5)
342                                                                                 s2 = strcat(s2, "^2");
343                                                                         else if(hsv_x < 3.33333333333333333333)
344                                                                                 s2 = strcat(s2, "^5");
345                                                                         else if(hsv_x < 4.5)
346                                                                                 s2 = strcat(s2, "^4");
347                                                                         else if(hsv_x < 5.5)
348                                                                                 s2 = strcat(s2, "^6");
349                                                                         else
350                                                                                 s2 = strcat(s2, "^1");
351                                                                 }
352                                                         }
353
354                                                         i += 3;
355                                                 }
356                                                 else
357                                                 {
358                                                         // blue missing
359                                                         s2 = strcat(s2, substring(s, i, 4));
360                                                         i += 2;
361                                                 }
362                                         }
363                                         else
364                                         {
365                                                 // green missing
366                                                 s2 = strcat(s, substring(s2, i, 3));
367                                                 i += 1;
368                                         }
369                                 }
370                                 else
371                                 {
372                                         // red missing
373                                         s2 = strcat(s, substring(s2, i, 2));
374                                 }
375                         }
376                         else
377                         {
378                                 s2 = strcat(s2, ch, ch2);
379                         }
380                         ++i;
381                         continue;
382                 }
383                 s2 = strcat(s2, ch);
384         }
385
386         return s2;
387 }
388
389 float cvar_or(string cv, float v)
390 {
391         string s;
392         s = cvar_string(cv);
393         if(s == "")
394                 return v;
395         else
396                 return stof(s);
397 }
398
399 vector project_3d_to_2d(vector vec)
400
401         vec = cs_project(vec);
402         if(cs_project_is_b0rked)
403         {
404                 vec_x += vid_width / 2;
405                 vec_y += vid_height / 2;
406         }
407         vec_x *= vid_conwidth / vid_width;
408         vec_y *= vid_conheight / vid_height;
409         return vec;
410 }