Merge remote branch 'refs/remotes/origin/fruitiex/racefixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / miscfunctions.qc
1 var float(string text, float handleColors, vector fontSize) stringwidth;
2
3 entity players;
4 entity teams;
5
6 void serverAnnouncer()
7 {
8         // check for pending announcement, play it and remove it
9         if(announce_snd != "")
10         {
11                 sound(world, CHAN_AUTO, strcat("announcer/", cvar_string("cl_announcer"), "/", announce_snd, ".wav"), VOL_BASEVOICE, ATTN_NONE);
12                 strunzone(announce_snd);
13                 announce_snd = "";
14         }
15 }
16
17 void restartAnnouncer_Think() {
18         float countdown_rounded, countdown;
19         countdown = getstatf(STAT_GAMESTARTTIME) - time;
20         countdown_rounded = floor(0.5 + countdown);
21         if(countdown <= 0) {
22                 if (!spectatee_status) //do cprint only for players
23                         centerprint("^1Begin!");
24
25                 sound(world, CHAN_AUTO, strcat("announcer/", cvar_string("cl_announcer"), "/begin.wav"), VOL_BASEVOICE, ATTN_NONE);
26                 //reset maptime announcers now as well
27                 announcer_5min = announcer_1min = FALSE;
28
29                 remove(self);
30                 return;
31         }
32         else {
33                 if (!spectatee_status) //do cprint only for players
34                         centerprint(strcat("^1Game starts in ", ftos(countdown_rounded), " seconds"));
35
36                 if(countdown_rounded <= 3 && countdown_rounded >= 1) {
37                         sound(world, CHAN_AUTO, strcat("announcer/", cvar_string("cl_announcer"), "/", ftos(countdown_rounded), ".wav"), VOL_BASEVOICE, ATTN_NONE);
38                 }
39
40                 self.nextthink = getstatf(STAT_GAMESTARTTIME) - (countdown - 1);
41         }
42 }
43
44 /**
45  * Plays the 1minute or 5 minutes (of maptime) remaining sound, if client wants it
46  */
47 void maptimeAnnouncer() {
48         float timelimit;
49         timelimit = getstatf(STAT_TIMELIMIT);
50         float timeleft;
51         timeleft = max(0, timelimit * 60 + getstatf(STAT_GAMESTARTTIME) - time);
52
53         float warmuplimit;
54         float warmuptimeleft;
55         if(warmup_stage) {
56                 warmuplimit = cvar("g_warmup_limit");
57                 if(warmuplimit > 0) {
58                         warmuptimeleft = max(0, warmuplimit + getstatf(STAT_GAMESTARTTIME) - time); 
59                 }
60         }
61
62         //5 minute check
63         if (cvar("cl_sound_maptime_warning") >= 2) {
64                 //make sure that after connect (and e.g. 4 minutes left) we will not get a wrong sound
65                 if(announcer_5min)
66                 {
67                         if(((!warmup_stage || warmuplimit == 0) && timeleft > 300) || (warmup_stage && warmuplimit > 0 && warmuptimeleft > 300))
68                                 announcer_5min = FALSE;
69                 }
70                 else if (((!warmup_stage || warmuplimit == 0) && timelimit > 0 && timeleft < 300 && timeleft > 299) || (warmup_stage && warmuplimit > 0 && warmuptimeleft < 300 && warmuptimeleft > 299))
71                         //if we're in warmup mode, check whether there's a warmup timelimit
72                         if not (warmuplimit == -1 && warmup_stage) {
73                                 announcer_5min = TRUE;
74                                 //dprint("i will play the sound, I promise!\n");
75                                 sound(world, CHAN_AUTO, strcat("announcer/", cvar_string("cl_announcer"), "/5minutesremain.wav"), VOL_BASEVOICE, ATTN_NONE);
76                         }
77         }
78
79         //1 minute check
80         if (cvar("cl_sound_maptime_warning") == 1 || cvar("cl_sound_maptime_warning") == 3) {
81                 if (announcer_1min)
82                 {
83                         if(((!warmup_stage || warmuplimit == 0) && timeleft > 60) || (warmup_stage && warmuplimit > 0 && warmuptimeleft > 60))
84                                 announcer_1min = FALSE;
85                 }
86                 else if (((!warmup_stage || warmuplimit == 0) && timelimit > 0 && timeleft < 60) || (warmup_stage && warmuplimit > 0 && warmuptimeleft < 60))
87                         //if we're in warmup mode, check whether there's a warmup timelimit
88                         if not (warmuplimit == -1 && warmup_stage) {
89                                 announcer_1min = TRUE;
90                                 sound(world, CHAN_AUTO, strcat("announcer/", cvar_string("cl_announcer"), "/1minuteremains.wav"), VOL_BASEVOICE, ATTN_NONE);
91                         }
92         }
93 }
94
95 /**
96  * Announce carried items (e.g. flags in CTF).
97  */
98 float redflag_prev;
99 float blueflag_prev;
100 void carrierAnnouncer() {
101         float stat_items, redflag, blueflag;
102         float pickup;
103         string item;
104
105         if not(cvar("cl_notify_carried_items"))
106                 return;
107
108         stat_items = getstati(STAT_ITEMS);
109
110         redflag = (stat_items/IT_RED_FLAG_TAKEN) & 3;
111         blueflag = (stat_items/IT_BLUE_FLAG_TAKEN) & 3;
112
113         if (redflag == 3 && redflag != redflag_prev) {
114                 item = "^1RED^7 flag";
115                 pickup = (redflag_prev == 2);
116         }
117
118         if (blueflag == 3 && blueflag != blueflag_prev) {
119                 item = "^4BLUE^7 flag";
120                 pickup = (blueflag_prev == 2);
121         }
122
123         if (item)
124         {
125                 if (pickup) {
126                         if (cvar("cl_notify_carried_items") & 2)
127                                 centerprint(strcat("You picked up the ", item, "!"));
128                 }
129                 else {
130                         if (cvar("cl_notify_carried_items") & 1)
131                                 centerprint(strcat("You got the ", item, "!"));
132                 }
133         }
134
135         blueflag_prev = blueflag;
136         redflag_prev = redflag;
137 }
138
139 /**
140  * Add all future announcer sounds precaches here.
141  * TODO: announcer queues
142  */
143 void Announcer_Precache () {
144         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/1minuteremains.wav"));
145         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/5minutesremain.wav"));
146
147         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/electrobitch.wav"));
148         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/airshot.wav"));
149         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/03kills.wav"));
150         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/05kills.wav"));
151         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/10kills.wav"));
152         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/15kills.wav"));
153         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/20kills.wav"));
154         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/25kills.wav"));
155         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/30kills.wav"));
156         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/botlike.wav"));
157         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/yoda.wav"));
158         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/amazing.wav"));
159         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/awesome.wav"));
160         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/headshot.wav"));
161         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/impressive.wav"));
162
163         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/prepareforbattle.wav"));
164         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/begin.wav"));
165         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/timeoutcalled.wav"));
166         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/1fragleft.wav"));
167         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/2fragsleft.wav"));
168         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/3fragsleft.wav"));
169         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/terminated.wav"));
170
171         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/1.wav"));
172         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/2.wav"));
173         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/3.wav"));
174         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/4.wav"));
175         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/5.wav"));
176         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/6.wav"));
177         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/7.wav"));
178         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/8.wav"));
179         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/9.wav"));
180         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/10.wav"));
181
182         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/lastsecond.wav"));
183         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/narrowly.wav"));
184
185         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/voteaccept.wav"));
186         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/votecall.wav"));
187         precache_sound (strcat("announcer/", cvar_string("cl_announcer"), "/votefail.wav"));
188 }
189
190 void AuditLists()
191 {
192         entity e;
193         entity prev;
194
195         prev = players;
196         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
197         {
198                 if(prev != e.sort_prev)
199                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
200         }
201
202         prev = teams;
203         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
204         {
205                 if(prev != e.sort_prev)
206                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
207         }
208 }
209
210
211 float RegisterPlayer(entity player)
212 {
213         entity pl;
214         AuditLists();
215         for(pl = players.sort_next; pl; pl = pl.sort_next)
216                 if(pl == player)
217                         error("Player already registered!");
218         player.sort_next = players.sort_next;
219         player.sort_prev = players;
220         if(players.sort_next)
221                 players.sort_next.sort_prev = player;
222         players.sort_next = player;
223         AuditLists();
224         return true;
225 }
226
227 void RemovePlayer(entity player)
228 {
229         entity pl, parent;
230         AuditLists();
231         parent = players;
232         for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
233                 parent = pl;
234
235         if(!pl)
236         {
237                 error("Trying to remove a player which is not in the playerlist!");
238                 return;
239         }
240         parent.sort_next = player.sort_next;
241         if(player.sort_next)
242                 player.sort_next.sort_prev = parent;
243         AuditLists();
244 }
245
246 void MoveToLast(entity e)
247 {
248         AuditLists();
249         other = e.sort_next;
250         while(other)
251         {
252                 SORT_SWAP(other, e);
253                 other = e.sort_next;
254         }
255         AuditLists();
256 }
257
258 float RegisterTeam(entity Team)
259 {
260         entity tm;
261         AuditLists();
262         for(tm = teams.sort_next; tm; tm = tm.sort_next)
263                 if(tm == Team)
264                         error("Team already registered!");
265         Team.sort_next = teams.sort_next;
266         Team.sort_prev = teams;
267         if(teams.sort_next)
268                 teams.sort_next.sort_prev = Team;
269         teams.sort_next = Team;
270         AuditLists();
271         return true;
272 }
273
274 void RemoveTeam(entity Team)
275 {
276         entity tm, parent;
277         AuditLists();
278         parent = teams;
279         for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
280                 parent = tm;
281
282         if(!tm)
283         {
284                 print("Trying to remove a team which is not in the teamlist!");
285                 return;
286         }
287         parent.sort_next = Team.sort_next;
288         if(Team.sort_next)
289                 Team.sort_next.sort_prev = parent;
290         AuditLists();
291 }
292
293 entity GetTeam(float Team, float add)
294 {
295         float num;
296         entity tm;
297         num = (Team == COLOR_SPECTATOR) ? 16 : Team;
298         if(teamslots[num])
299                 return teamslots[num];
300         if not(add)
301                 return NULL;
302         tm = spawn();
303         tm.team = Team;
304         teamslots[num] = tm;
305         RegisterTeam(tm);
306         return tm;
307 }
308
309 void CSQC_CheckEngine()
310 {
311         hud_font = FONT_USER+1;
312         hud_bigfont = FONT_USER+2;
313 }
314
315 vector HUD_GetFontsize(string cvarname)
316 {
317         vector v;
318         v = stov(cvar_string(cvarname));
319         if(v_x == 0)
320                 v = '8 8 0';
321         if(v_y == 0)
322                 v_y = v_x;
323         v_z = 0;
324         return v;
325 }
326
327 float PreviewExists(string name)
328 {
329         float f;
330         string file;
331
332         if(cvar("cl_readpicture_force"))
333                 return false;
334
335         file = strcat(name, ".tga");
336         f = fopen(file, FILE_READ);
337         if(f >= 0)
338         {
339                 fclose(f);
340                 return true;
341         }
342         file = strcat(name, ".png");
343         f = fopen(file, FILE_READ);
344         if(f >= 0)
345         {
346                 fclose(f);
347                 return true;
348         }
349         file = strcat(name, ".jpg");
350         f = fopen(file, FILE_READ);
351         if(f >= 0)
352         {
353                 fclose(f);
354                 return true;
355         }
356         file = strcat(name, ".pcx");
357         f = fopen(file, FILE_READ);
358         if(f >= 0)
359         {
360                 fclose(f);
361                 return true;
362         }
363         return false;
364 }
365
366 vector rotate(vector v, float a)
367 {
368         vector w;
369         // FTEQCC SUCKS AGAIN
370         w_x =      v_x * cos(a) + v_y * sin(a);
371         w_y = -1 * v_x * sin(a) + v_y * cos(a);
372         return w;
373 }
374
375 float ColorTranslateMode;
376
377 string ColorTranslateRGB(string s)
378 {
379         if(ColorTranslateMode & 1)
380                 return strdecolorize(s);
381         else
382                 return s;
383 }
384
385 float cvar_or(string cv, float v)
386 {
387         string s;
388         s = cvar_string(cv);
389         if(s == "")
390                 return v;
391         else
392                 return stof(s);
393 }
394
395 vector project_3d_to_2d(vector vec)
396 {
397         vec = cs_project(vec);
398         if(cs_project_is_b0rked > 0)
399         {
400                 vec_x *= vid_conwidth / vid_width;
401                 vec_y *= vid_conheight / vid_height;
402         }
403         return vec;
404 }
405
406 void dummyfunction(float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
407 {
408 }
409
410 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
411 {
412         return 1.2 / (1.2 - fadelerp);
413 }
414
415 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
416 {
417         boxsize_x *= boxxsizefactor; // easier interface for text
418         return boxsize * (0.5 * (1 - sz));
419 }
420
421 void drawborderlines(float thickness, vector pos, vector dim, vector color, float alpha, float drawflag)
422 {
423         vector line_dim;
424
425         // left and right lines
426         pos_x -= thickness;
427         line_dim_x = thickness;
428         line_dim_y = dim_y;
429         drawfill(pos, line_dim, color, alpha, drawflag);
430         drawfill(pos + (dim_x + thickness) * '1 0 0', line_dim, color, alpha, drawflag);
431
432         // upper and lower lines
433         pos_y -= thickness;
434         line_dim_x = dim_x + thickness * 2; // make upper and lower lines longer
435         line_dim_y = thickness;
436         drawfill(pos, line_dim, color, alpha, drawflag);
437         drawfill(pos + (dim_y + thickness) * '0 1 0', line_dim, color, alpha, drawflag);
438 }
439
440 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float alpha, float drawflag)
441 {
442         vector current_pos, end_pos, new_size, ratio;
443         end_pos = pos + area;
444
445         current_pos_y = pos_y;
446         while (current_pos_y < end_pos_y)
447         {
448                 current_pos_x = pos_x;
449                 while (current_pos_x < end_pos_x)
450                 {
451                         new_size_x = min(sz_x, end_pos_x - current_pos_x);
452                         new_size_y = min(sz_y, end_pos_y - current_pos_y);
453                         ratio_x = new_size_x / sz_x;
454                         ratio_y = new_size_y / sz_y;
455                         drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, alpha, drawflag);
456                         current_pos_x += sz_x;
457                 }
458                 current_pos_y += sz_y;
459         }
460 }
461
462 // drawpic wrapper to draw an image as large as possible with preserved aspect ratio into a box
463 var float _drawpic_imgaspect;
464 var float _drawpic_aspect;
465 var vector _drawpic_imgsize;
466 var vector _drawpic_sz;
467 var vector _drawpic_oldsz;
468 var string _drawpic_picpath;
469 #define drawpic_aspect(pos,pic,mySize,color,alpha,drawflag)\
470         do {\
471                 _drawpic_imgsize = drawgetimagesize(pic);\
472                 _drawpic_imgaspect = _drawpic_imgsize_x/_drawpic_imgsize_y;\
473                 _drawpic_oldsz = _drawpic_sz = mySize;\
474                 _drawpic_aspect = _drawpic_sz_x/_drawpic_sz_y;\
475                 if(_drawpic_aspect > _drawpic_imgaspect) {\
476                         _drawpic_sz_x = _drawpic_sz_y * _drawpic_imgaspect;\
477                         drawpic(pos + eX * (_drawpic_oldsz_x - _drawpic_sz_x) * 0.5, pic, _drawpic_sz, color, alpha, drawflag);\
478                 } else {\
479                         _drawpic_sz_y = _drawpic_sz_x / _drawpic_imgaspect;\
480                         drawpic(pos + eY * (_drawpic_oldsz_y - _drawpic_sz_y) * 0.5, pic, _drawpic_sz, color, alpha, drawflag);\
481                 }\
482         } while(0)
483
484 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
485 #define drawpic_aspect_skin(pos,pic,sz,color,alpha,drawflag)\
486         do{\
487                 _drawpic_picpath = strcat(hud_skin_path, "/", pic);\
488                 if(precache_pic(_drawpic_picpath) == "") {\
489                         _drawpic_picpath = strcat("gfx/hud/default/", pic);\
490                 }\
491                 drawpic_aspect(pos, _drawpic_picpath, sz, color, alpha, drawflag);\
492                 _drawpic_picpath = string_null;\
493         } while(0)
494
495 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
496 #define drawpic_skin(pos,pic,sz,color,alpha,drawflag)\
497         do{\
498                 _drawpic_picpath = strcat(hud_skin_path, "/", pic);\
499                 if(precache_pic(_drawpic_picpath) == "") {\
500                         _drawpic_picpath = strcat("gfx/hud/default/", pic);\
501                 }\
502                 drawpic(pos, _drawpic_picpath, sz, color, alpha, drawflag);\
503                 _drawpic_picpath = string_null;\
504         } while(0)
505
506 void drawpic_aspect_skin_expanding(vector position, string pic, vector scale, vector rgb, float alpha, float flag, float fadelerp)
507 {
508         float sz;
509         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
510
511         drawpic_skin(position + expandingbox_resize_centered_box_offset(sz, scale, 1), pic, scale * sz, rgb, alpha * (1 - fadelerp), flag);
512 }
513
514 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector scale, vector rgb, float alpha, float flag, float fadelerp)
515 {
516         drawpic_aspect_skin_expanding(position, pic, scale, rgb, alpha, flag, fadelerp);
517         drawpic_skin(position, pic, scale, rgb, alpha * fadelerp, flag);
518 }
519
520 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
521 void drawstring_aspect(vector pos, string text, vector sz, vector color, float alpha, float drawflag) {
522         vector textsize;
523         textsize = eX * stringwidth(text, FALSE, '1 1 1' * sz_y) + eY * sz_y;
524
525         float textaspect;
526         textaspect = textsize_x/textsize_y;
527
528         vector oldsz;
529         oldsz = sz;
530         float aspect;
531         aspect = sz_x/sz_y;
532
533         if(aspect > textaspect) {
534                 sz_x = sz_y * textaspect;
535                 drawstring(pos + eX * (oldsz_x - sz_x) * 0.5, text, '1 1 0' * sz_y, color, alpha, drawflag);
536         } else {
537                 sz_y = sz_x / textaspect; 
538                 drawstring(pos + eY * (oldsz_y - sz_y) * 0.5, text, '1 1 0' * sz_y, color, alpha, drawflag);
539         }
540 }
541
542 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
543 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float alpha, float drawflag) {
544         vector textsize;
545         textsize = eX * stringwidth(text, TRUE, '1 1 1' * sz_y) + eY * sz_y;
546
547         float textaspect;
548         textaspect = textsize_x/textsize_y;
549
550         vector oldsz;
551         oldsz = sz;
552         float aspect;
553         aspect = sz_x/sz_y;
554
555         if(aspect > textaspect) {
556                 sz_x = sz_y * textaspect;
557                 drawcolorcodedstring(pos + eX * (oldsz_x - sz_x) * 0.5, text, '1 1 0' * sz_y, alpha, drawflag);
558         } else {
559                 sz_y = sz_x / textaspect; 
560                 drawcolorcodedstring(pos + eY * (oldsz_y - sz_y) * 0.5, text, '1 1 0' * sz_y, alpha, drawflag);
561         }
562 }
563
564 vector drawfontscale;
565 void drawstring_expanding(vector position, string text, vector scale, vector rgb, float alpha, float flag, float fadelerp)
566 {
567         float sz;
568         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
569
570         drawfontscale = sz * '1 1 0';
571         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
572         drawstring(position + expandingbox_resize_centered_box_offset(sz, scale, stringwidth(text, FALSE, scale * (sz / drawfontscale_x)) / (scale_x * sz)), text, scale * (sz / drawfontscale_x), rgb, alpha * (1 - fadelerp), flag);
573         // width parameter:
574         //    (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
575         //    SIZE1
576         drawfontscale = '1 1 0';
577 }
578
579 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
580 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float alpha, float drawflag, float fadelerp) {
581         vector textsize;
582         textsize = eX * stringwidth(text, FALSE, '1 1 1' * sz_y) + eY * sz_y;
583
584         float textaspect;
585         textaspect = textsize_x/textsize_y;
586
587         vector oldsz;
588         oldsz = sz;
589         float aspect;
590         aspect = sz_x/sz_y;
591
592         if(aspect > textaspect) {
593                 sz_x = sz_y * textaspect;
594                 drawstring_expanding(pos + eX * (oldsz_x - sz_x) * 0.5, text, '1 1 0' * sz_y, color, alpha, drawflag, fadelerp);
595         } else {
596                 sz_y = sz_x / textaspect; 
597                 drawstring_expanding(pos + eY * (oldsz_y - sz_y) * 0.5, text, '1 1 0' * sz_y, color, alpha, drawflag, fadelerp);
598         }
599 }
600
601 void drawcolorcodedstring_expanding(vector position, string text, vector scale, float alpha, float flag, float fadelerp)
602 {
603         float sz;
604         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
605
606         drawfontscale = sz * '1 1 0';
607         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
608         drawcolorcodedstring(position + expandingbox_resize_centered_box_offset(sz, scale, stringwidth(text, TRUE, scale * (sz / drawfontscale_x)) / (scale_x * sz)), text, scale * (sz / drawfontscale_x), alpha * (1 - fadelerp), flag);
609         drawfontscale = '1 1 0';
610 }
611
612 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float alpha, float drawflag, float fadelerp) {
613         vector textsize;
614         textsize = eX * stringwidth(text, TRUE, '1 1 1' * sz_y) + eY * sz_y;
615         
616         float textaspect;
617         textaspect = textsize_x/textsize_y;
618
619         vector oldsz;
620         oldsz = sz;
621         float aspect;
622         aspect = sz_x/sz_y;
623
624         if(aspect > textaspect) {
625                 sz_x = sz_y * textaspect;
626                 drawcolorcodedstring_expanding(pos + eX * (oldsz_x - sz_x) * 0.5, text, '1 1 0' * sz_y, alpha, drawflag, fadelerp);
627         } else {
628                 sz_y = sz_x / textaspect; 
629                 drawcolorcodedstring_expanding(pos + eY * (oldsz_y - sz_y) * 0.5, text, '1 1 0' * sz_y, alpha, drawflag, fadelerp);
630         }
631 }
632
633 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
634 float PolyDrawModelSurface(entity e, float i_s)
635 {
636         float i_t;
637         float n_t;
638         vector tri;
639         string tex;
640         tex = getsurfacetexture(e, i_s);
641         if not(tex)
642                 return 0; // this is beyond the last one
643         n_t = getsurfacenumtriangles(e, i_s);
644         for(i_t = 0; i_t < n_t; ++i_t)
645         {
646                 tri = getsurfacetriangle(e, i_s, i_t);
647                 R_BeginPolygon(tex, 0);
648                 R_PolygonVertex(getsurfacepoint(e, i_s, tri_x), getsurfacepointattribute(e, i_s, tri_x, SPA_TEXCOORDS0), '1 1 1', 1);
649                 R_PolygonVertex(getsurfacepoint(e, i_s, tri_y), getsurfacepointattribute(e, i_s, tri_y, SPA_TEXCOORDS0), '1 1 1', 1);
650                 R_PolygonVertex(getsurfacepoint(e, i_s, tri_z), getsurfacepointattribute(e, i_s, tri_z, SPA_TEXCOORDS0), '1 1 1', 1);
651                 R_EndPolygon();
652         }
653         return 1;
654 }
655 void PolyDrawModel(entity e)
656 {
657         float i_s;
658         for(i_s = 0; ; ++i_s)
659                 if(!PolyDrawModelSurface(e, i_s))
660                         break;
661 }
662
663 void DrawCircleClippedPic(vector centre, float radius, string pic, float f, vector rgb, float a, float drawflag)
664 {
665         float x, y, q, d;
666         vector ringsize, v, t;
667         ringsize = radius * '1 1 0';
668
669         x = cos(f * 2 * M_PI);
670         y = sin(f * 2 * M_PI);
671         q = fabs(x) + fabs(y);
672         x /= q;
673         y /= q;
674
675         if(f >= 1)
676         {
677                 // draw full rectangle
678                 R_BeginPolygon(pic, drawflag);
679                         v = centre;                     t = '0.5 0.5 0';
680                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
681                         R_PolygonVertex(v, t, rgb, a);
682
683                         v = centre;                     t = '0.5 0.5 0';
684                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
685                         R_PolygonVertex(v, t, rgb, a);
686
687                         v = centre;                     t = '0.5 0.5 0';
688                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
689                         R_PolygonVertex(v, t, rgb, a);
690
691                         v = centre;                     t = '0.5 0.5 0';
692                         v_y -= 0.5 * ringsize_y;        t -= '0.5 -0.5 0';
693                         R_PolygonVertex(v, t, rgb, a);
694                 R_EndPolygon();
695
696                 d = q - 1;
697                 if(d > 0)
698                 {
699                         R_BeginPolygon(pic, drawflag);
700                                 v = centre;                     t = '0.5 0.5 0';
701                                 R_PolygonVertex(v, t, rgb, a);
702
703                                 v = centre;                     t = '0.5 0.5 0';
704                                 v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
705                                 R_PolygonVertex(v, t, rgb, a);
706                 }
707         }
708         else if(f > 0.75)
709         {
710                 // draw upper and first triangle
711                 R_BeginPolygon(pic, drawflag);
712                         v = centre;                     t = '0.5 0.5 0';
713                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
714                         R_PolygonVertex(v, t, rgb, a);
715
716                         v = centre;                     t = '0.5 0.5 0';
717                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
718                         R_PolygonVertex(v, t, rgb, a);
719
720                         v = centre;                     t = '0.5 0.5 0';
721                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
722                         R_PolygonVertex(v, t, rgb, a);
723                 R_EndPolygon();
724                 R_BeginPolygon(pic, drawflag);
725                         v = centre;                     t = '0.5 0.5 0';
726                         R_PolygonVertex(v, t, rgb, a);
727
728                         v = centre;                     t = '0.5 0.5 0';
729                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
730                         R_PolygonVertex(v, t, rgb, a);
731
732                         v = centre;                     t = '0.5 0.5 0';
733                         v_y -= 0.5 * ringsize_y;        t -= '0.5 -0.5 0';
734                         R_PolygonVertex(v, t, rgb, a);
735
736                 d = q - 0.75;
737                 if(d <= 0)
738                         R_EndPolygon();
739         }
740         else if(f > 0.5)
741         {
742                 // draw upper triangle
743                 R_BeginPolygon(pic, drawflag);
744                         v = centre;                     t = '0.5 0.5 0';
745                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
746                         R_PolygonVertex(v, t, rgb, a);
747
748                         v = centre;                     t = '0.5 0.5 0';
749                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
750                         R_PolygonVertex(v, t, rgb, a);
751
752                         v = centre;                     t = '0.5 0.5 0';
753                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
754                         R_PolygonVertex(v, t, rgb, a);
755                 R_EndPolygon();
756
757                 d = q - 0.5;
758                 if(d > 0)
759                 {
760                         R_BeginPolygon(pic, drawflag);
761                                 v = centre;                     t = '0.5 0.5 0';
762                                 R_PolygonVertex(v, t, rgb, a);
763
764                                 v = centre;                     t = '0.5 0.5 0';
765                                 v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
766                                 R_PolygonVertex(v, t, rgb, a);
767                 }
768         }
769         else if(f > 0.25)
770         {
771                 // draw first triangle
772                 R_BeginPolygon(pic, drawflag);
773                         v = centre;                     t = '0.5 0.5 0';
774                         R_PolygonVertex(v, t, rgb, a);
775
776                         v = centre;                     t = '0.5 0.5 0';
777                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
778                         R_PolygonVertex(v, t, rgb, a);
779
780                         v = centre;                     t = '0.5 0.5 0';
781                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
782                         R_PolygonVertex(v, t, rgb, a);
783
784                 d = q - 0.25;
785                 if(d <= 0)
786                         R_EndPolygon();
787         }
788         else
789         {
790                 d = q;
791                 if(d > 0)
792                 {
793                         R_BeginPolygon(pic, drawflag);
794                                 v = centre;                     t = '0.5 0.5 0';
795                                 R_PolygonVertex(v, t, rgb, a);
796
797                                 v = centre;                     t = '0.5 0.5 0';
798                                 v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
799                                 R_PolygonVertex(v, t, rgb, a);
800                 }
801         }
802
803         if(d > 0)
804         {
805                         v = centre;                     t = '0.5 0.5 0';
806                         v_x += x * 0.5 * ringsize_x;    t += x * '0.5 0.5 0';
807                         v_y += y * 0.5 * ringsize_y;    t += y * '0.5 -0.5 0';
808                         R_PolygonVertex(v, t, rgb, a);
809                 R_EndPolygon();
810         }
811 }