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