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