]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/miscfunctions.qc
Translate color codes for old clients
[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
181 vector Sbar_GetFontsize()
182 {
183         if(csqc_flags & CSQC_FLAG_READPICTURE)
184         {
185                 vector v;
186                 v = stov(cvar_string("sbar_fontsize"));
187                 if(v_x == 0)
188                         v = '8 8 0';
189                 if(v_y == 0)
190                         v_y = v_x;
191                 v_z = 0;
192                 return v;
193         }
194         return '8 8 0' ;
195 }
196
197 float Sbar_GetWidth(float teamcolumnwidth)
198 {
199         if(csqc_flags & CSQC_FLAG_READPICTURE)
200         {
201                 float f;
202                 f = stof(cvar_string("sbar_width"));
203                 if(f == 0)
204                         f = 640;
205                 if(f < 320)
206                         f = 320;
207                 if(f > vid_conwidth - 2 * teamcolumnwidth)
208                         f = vid_conwidth - 2 * teamcolumnwidth;
209                 return f;
210         }
211         return 640;
212 }
213
214 float PreviewExists(string name)
215 {
216         float f;
217         string file;
218
219         if(cvar("cl_readpicture_force"))
220                 return false;
221
222         file = strcat(name, ".tga");
223         f = fopen(file, FILE_READ);
224         if(f >= 0)
225         {
226                 fclose(f);
227                 return true;
228         }
229         file = strcat(name, ".png");
230         f = fopen(file, FILE_READ);
231         if(f >= 0)
232         {
233                 fclose(f);
234                 return true;
235         }
236         file = strcat(name, ".jpg");
237         f = fopen(file, FILE_READ);
238         if(f >= 0)
239         {
240                 fclose(f);
241                 return true;
242         }
243         file = strcat(name, ".pcx");
244         f = fopen(file, FILE_READ);
245         if(f >= 0)
246         {
247                 fclose(f);
248                 return true;
249         }
250         return false;
251 }
252
253 float PI      = 3.14159265359;
254 float DEG2RAD = 0.01745329252;
255 vector rotate(vector v, float a)
256 {
257         vector w;
258         // FTEQCC SUCKS AGAIN
259         w_x =      v_x * cos(a) + v_y * sin(a);
260         w_y = -1 * v_x * sin(a) + v_y * cos(a);
261         return w;
262 }
263
264 float ColorTranslateMode;
265
266 string ColorTranslateRGB(string s)
267 {
268         if not(ColorTranslateMode & 2)
269         if(csqc_flags & CSQC_FLAG_READPICTURE)
270         {
271                 if(ColorTranslateMode & 1)
272                         return strdecolorize(s);
273                 else
274                         return s;
275         }
276         
277         // running on an OLD engine!
278         // must translate ^xRGB codes to regular color codes
279         float i, n;
280         string s2, ch, ch2;
281         vector theTempColor, hsv;
282         float component;
283
284         s2 = "";
285
286         n = strlen(s);
287         for(i = 0; i < n; ++i)
288         {
289                 ch = substring(s, i, 1);
290                 if(ch == "^")
291                 {
292                         ch2 = substring(s, i+1, 1);
293                         if(ch2 == "^")
294                         {
295                                 s2 = strcat(s2, ch, ch2);
296                         }
297                         else if(ch2 == "0" || stof(ch2)) // digit?
298                         {
299                                 if not(ColorTranslateMode & 1)
300                                         s2 = strcat(s2, ch, ch2);
301                         }
302                         else if(ch2 == "x") // ^x found
303                         {
304                                 theTempColor = '0 0 0';
305                                 
306                                 component = HEXDIGIT_TO_DEC(substring(s, i+2, 1));
307                                 if (component >= 0) // ^xr found
308                                 {
309                                         theTempColor_x = component/15;
310                                         
311                                         component = HEXDIGIT_TO_DEC(substring(s, i+3, 1));
312                                         if (component >= 0) // ^xrg found
313                                         {
314                                                 theTempColor_y = component/15;
315                                                 
316                                                 component = HEXDIGIT_TO_DEC(substring(s, i+4, 1));
317                                                 if (component >= 0) // ^xrgb found
318                                                 {
319                                                         theTempColor_z = component/15;
320
321                                                         if not(ColorTranslateMode & 1)
322                                                         {
323                                                                 hsv = rgb_to_hsv(theTempColor);
324
325                                                                 if(hsv_y < 0.2)
326                                                                 {
327                                                                         if(hsv_z < 0.5)
328                                                                                 s2 = strcat(s2, "^0");
329                                                                         else
330                                                                                 s2 = strcat(s2, "^7");
331                                                                 }
332                                                                 else
333                                                                 {
334                                                                         if(hsv_x < 0.6)
335                                                                                 s2 = strcat(s2, "^1");
336                                                                         else if(hsv_x < 1.33333333333333333333)
337                                                                                 s2 = strcat(s2, "^3");
338                                                                         else if(hsv_x < 2.5)
339                                                                                 s2 = strcat(s2, "^2");
340                                                                         else if(hsv_x < 3.33333333333333333333)
341                                                                                 s2 = strcat(s2, "^5");
342                                                                         else if(hsv_x < 4.5)
343                                                                                 s2 = strcat(s2, "^4");
344                                                                         else if(hsv_x < 5.5)
345                                                                                 s2 = strcat(s2, "^6");
346                                                                         else
347                                                                                 s2 = strcat(s2, "^1");
348                                                                 }
349                                                         }
350
351                                                         i += 3;
352                                                 }
353                                                 else
354                                                 {
355                                                         // blue missing
356                                                         s2 = strcat(s2, substring(s, i, 4));
357                                                         i += 2;
358                                                 }
359                                         }
360                                         else
361                                         {
362                                                 // green missing
363                                                 s2 = strcat(s, substring(s2, i, 3));
364                                                 i += 1;
365                                         }
366                                 }
367                                 else
368                                 {
369                                         // red missing
370                                         s2 = strcat(s, substring(s2, i, 2));
371                                 }
372                         }
373                         else
374                         {
375                                 s2 = strcat(s2, ch, ch2);
376                         }
377                         ++i;
378                         continue;
379                 }
380                 s2 = strcat(s2, ch);
381         }
382
383         return s2;
384 }