Merge remote-tracking branch 'origin/master' into samual/notification_rewrite
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / miscfunctions.qc
1 entity players;
2 entity teams;
3
4 void AuditLists()
5 {
6         entity e;
7         entity prev;
8
9         prev = players;
10         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
11         {
12                 if(prev != e.sort_prev)
13                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
14         }
15
16         prev = teams;
17         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
18         {
19                 if(prev != e.sort_prev)
20                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
21         }
22 }
23
24
25 float RegisterPlayer(entity player)
26 {
27         entity pl;
28         AuditLists();
29         for(pl = players.sort_next; pl; pl = pl.sort_next)
30                 if(pl == player)
31                         error("Player already registered!");
32         player.sort_next = players.sort_next;
33         player.sort_prev = players;
34         if(players.sort_next)
35                 players.sort_next.sort_prev = player;
36         players.sort_next = player;
37         AuditLists();
38         return true;
39 }
40
41 void RemovePlayer(entity player)
42 {
43         entity pl, parent;
44         AuditLists();
45         parent = players;
46         for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
47                 parent = pl;
48
49         if(!pl)
50         {
51                 error("Trying to remove a player which is not in the playerlist!");
52                 return;
53         }
54         parent.sort_next = player.sort_next;
55         if(player.sort_next)
56                 player.sort_next.sort_prev = parent;
57         AuditLists();
58 }
59
60 void MoveToLast(entity e)
61 {
62         AuditLists();
63         other = e.sort_next;
64         while(other)
65         {
66                 SORT_SWAP(other, e);
67                 other = e.sort_next;
68         }
69         AuditLists();
70 }
71
72 float RegisterTeam(entity Team)
73 {
74         entity tm;
75         AuditLists();
76         for(tm = teams.sort_next; tm; tm = tm.sort_next)
77                 if(tm == Team)
78                         error("Team already registered!");
79         Team.sort_next = teams.sort_next;
80         Team.sort_prev = teams;
81         if(teams.sort_next)
82                 teams.sort_next.sort_prev = Team;
83         teams.sort_next = Team;
84         AuditLists();
85         return true;
86 }
87
88 void RemoveTeam(entity Team)
89 {
90         entity tm, parent;
91         AuditLists();
92         parent = teams;
93         for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
94                 parent = tm;
95
96         if(!tm)
97         {
98                 print(_("Trying to remove a team which is not in the teamlist!"));
99                 return;
100         }
101         parent.sort_next = Team.sort_next;
102         if(Team.sort_next)
103                 Team.sort_next.sort_prev = parent;
104         AuditLists();
105 }
106
107 entity GetTeam(float Team, float add)
108 {
109         float num;
110         entity tm;
111         num = (Team == FL_SPECTATOR) ? 16 : Team;
112         if(teamslots[num])
113                 return teamslots[num];
114         if not(add)
115                 return world;
116         tm = spawn();
117         tm.team = Team;
118         teamslots[num] = tm;
119         RegisterTeam(tm);
120         return tm;
121 }
122
123 vector HUD_GetFontsize(string cvarname)
124 {
125         vector v;
126         v = stov(cvar_string(cvarname));
127         if(v_x == 0)
128                 v = '8 8 0';
129         if(v_y == 0)
130                 v_y = v_x;
131         v_z = 0;
132         return v;
133 }
134
135 float PreviewExists(string name)
136 {
137         if(autocvar_cl_readpicture_force)
138                 return false;
139
140         if (fexists(strcat(name, ".tga"))) return true;
141         if (fexists(strcat(name, ".png"))) return true;
142         if (fexists(strcat(name, ".jpg"))) return true;
143         if (fexists(strcat(name, ".pcx"))) return true;
144
145         return false;
146 }
147
148 vector rotate(vector v, float a)
149 {
150         vector w = '0 0 0';
151         // FTEQCC SUCKS AGAIN
152         w_x =      v_x * cos(a) + v_y * sin(a);
153         w_y = -1 * v_x * sin(a) + v_y * cos(a);
154         return w;
155 }
156
157 float ColorTranslateMode;
158
159 string ColorTranslateRGB(string s)
160 {
161         if(ColorTranslateMode & 1)
162                 return strdecolorize(s);
163         else
164                 return s;
165 }
166
167 // decolorizes and team colors the player name when needed
168 string playername(string thename, float teamid)
169 {
170     string t;
171     if (teamplay)
172     {
173         t = Team_ColorCode(teamid);
174         return strcat(t, strdecolorize(thename));
175     }
176     else
177         return strdecolorize(thename);
178 }
179
180 float cvar_or(string cv, float v)
181 {
182         string s;
183         s = cvar_string(cv);
184         if(s == "")
185                 return v;
186         else
187                 return stof(s);
188 }
189
190 vector project_3d_to_2d(vector vec)
191 {
192         vec = cs_project(vec);
193         if(cs_project_is_b0rked > 0)
194         {
195                 vec_x *= vid_conwidth / vid_width;
196                 vec_y *= vid_conheight / vid_height;
197         }
198         return vec;
199 }
200
201 void dummyfunction(float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
202 {
203 }
204
205 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
206 {
207         return 1.2 / (1.2 - fadelerp);
208 }
209
210 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
211 {
212         boxsize_x *= boxxsizefactor; // easier interface for text
213         return boxsize * (0.5 * (1 - sz));
214 }
215
216 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
217 {
218         vector line_dim = '0 0 0';
219
220         // left and right lines
221         pos_x -= thickness;
222         line_dim_x = thickness;
223         line_dim_y = dim_y;
224         drawfill(pos, line_dim, color, theAlpha, drawflag);
225         drawfill(pos + (dim_x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
226
227         // upper and lower lines
228         pos_y -= thickness;
229         line_dim_x = dim_x + thickness * 2; // make upper and lower lines longer
230         line_dim_y = thickness;
231         drawfill(pos, line_dim, color, theAlpha, drawflag);
232         drawfill(pos + (dim_y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
233 }
234
235 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
236 {
237         vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
238         end_pos = pos + area;
239
240         current_pos_y = pos_y;
241         while (current_pos_y < end_pos_y)
242         {
243                 current_pos_x = pos_x;
244                 while (current_pos_x < end_pos_x)
245                 {
246                         new_size_x = min(sz_x, end_pos_x - current_pos_x);
247                         new_size_y = min(sz_y, end_pos_y - current_pos_y);
248                         ratio_x = new_size_x / sz_x;
249                         ratio_y = new_size_y / sz_y;
250                         drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
251                         current_pos_x += sz_x;
252                 }
253                 current_pos_y += sz_y;
254         }
255 }
256
257 // drawpic wrapper to draw an image as large as possible with preserved aspect ratio into a box
258 var float _drawpic_imgaspect;
259 var vector _drawpic_imgsize;
260 var vector _drawpic_sz;
261 var float _drawpic_oldsz;
262 var string _drawpic_picpath;
263 #define drawpic_aspect(pos,pic,mySize,color,theAlpha,drawflag)\
264         do {\
265                 _drawpic_imgsize = draw_getimagesize(pic);\
266                 if(_drawpic_imgsize != '0 0 0') {\
267                         _drawpic_imgaspect = _drawpic_imgsize_x/_drawpic_imgsize_y;\
268                         _drawpic_sz = mySize;\
269                         if(_drawpic_sz_x/_drawpic_sz_y > _drawpic_imgaspect) {\
270                                 _drawpic_oldsz = _drawpic_sz_x;\
271                                 _drawpic_sz_x = _drawpic_sz_y * _drawpic_imgaspect;\
272                                 if(_drawpic_sz_x)\
273                                         drawpic(pos + eX * (_drawpic_oldsz - _drawpic_sz_x) * 0.5, pic, _drawpic_sz, color, theAlpha, drawflag);\
274                         } else {\
275                                 _drawpic_oldsz = _drawpic_sz_y;\
276                                 _drawpic_sz_y = _drawpic_sz_x / _drawpic_imgaspect;\
277                                 if(_drawpic_sz_y)\
278                                         drawpic(pos + eY * (_drawpic_oldsz - _drawpic_sz_y) * 0.5, pic, _drawpic_sz, color, theAlpha, drawflag);\
279                         }\
280                 }\
281         } while(0)
282
283 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
284 #define drawpic_aspect_skin(pos,pic,sz,color,theAlpha,drawflag)\
285         do{\
286                 _drawpic_picpath = strcat(hud_skin_path, "/", pic);\
287                 if(precache_pic(_drawpic_picpath) == "") {\
288                         _drawpic_picpath = strcat("gfx/hud/default/", pic);\
289                 }\
290                 drawpic_aspect(pos, _drawpic_picpath, sz, color, theAlpha, drawflag);\
291                 _drawpic_picpath = string_null;\
292         } while(0)
293
294 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
295 #define drawpic_skin(pos,pic,sz,color,theAlpha,drawflag)\
296         do{\
297                 _drawpic_picpath = strcat(hud_skin_path, "/", pic);\
298                 if(precache_pic(_drawpic_picpath) == "") {\
299                         _drawpic_picpath = strcat("gfx/hud/default/", pic);\
300                 }\
301                 drawpic(pos, _drawpic_picpath, sz, color, theAlpha, drawflag);\
302                 _drawpic_picpath = string_null;\
303         } while(0)
304
305 void drawpic_aspect_skin_expanding(vector position, string pic, vector scale, vector rgb, float theAlpha, float flag, float fadelerp)
306 {
307         float sz;
308         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
309
310         drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, scale, 1), pic, scale * sz, rgb, theAlpha * (1 - fadelerp), flag);
311 }
312
313 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector scale, vector rgb, float theAlpha, float flag, float fadelerp)
314 {
315         drawpic_aspect_skin_expanding(position, pic, scale, rgb, theAlpha, flag, fadelerp);
316         drawpic_skin(position, pic, scale, rgb, theAlpha * fadelerp, flag);
317 }
318 #define SET_POS_AND_SZ_Y_ASPECT(allow_colors)\
319         float textaspect, oldsz;\
320         textaspect = stringwidth(text, allow_colors, '1 1 1' * sz_y) / sz_y;\
321         if(sz_x/sz_y > textaspect) {\
322                 oldsz = sz_x;\
323                 sz_x = sz_y * textaspect;\
324                 pos_x += (oldsz - sz_x) * 0.5;\
325         } else {\
326                 oldsz = sz_y;\
327                 sz_y = sz_x / textaspect; \
328                 pos_y += (oldsz - sz_y) * 0.5;\
329         }
330
331 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
332 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
333         SET_POS_AND_SZ_Y_ASPECT(FALSE)
334         drawstring(pos, text, '1 1 0' * sz_y, color, theAlpha, drawflag);
335 }
336
337 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
338 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
339         SET_POS_AND_SZ_Y_ASPECT(TRUE)
340         drawcolorcodedstring(pos, text, '1 1 0' * sz_y, theAlpha, drawflag);
341 }
342
343 vector drawfontscale;
344 void drawstring_expanding(vector position, string text, vector scale, vector rgb, float theAlpha, float flag, float fadelerp)
345 {
346         float sz;
347         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
348
349         drawfontscale = sz * '1 1 0';
350         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
351         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, theAlpha * (1 - fadelerp), flag);
352         // width parameter:
353         //    (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
354         //    SIZE1
355         drawfontscale = '1 1 0';
356 }
357
358 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
359 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
360         SET_POS_AND_SZ_Y_ASPECT(FALSE)
361         drawstring_expanding(pos, text, '1 1 0' * sz_y, color, theAlpha, drawflag, fadelerp);
362 }
363
364 void drawcolorcodedstring_expanding(vector position, string text, vector scale, float theAlpha, float flag, float fadelerp)
365 {
366         float sz;
367         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
368
369         drawfontscale = sz * '1 1 0';
370         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
371         drawcolorcodedstring(position + expandingbox_resize_centered_box_offset(sz, scale, stringwidth(text, TRUE, scale * (sz / drawfontscale_x)) / (scale_x * sz)), text, scale * (sz / drawfontscale_x), theAlpha * (1 - fadelerp), flag);
372         drawfontscale = '1 1 0';
373 }
374
375 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
376         SET_POS_AND_SZ_Y_ASPECT(TRUE)
377         drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz_y, theAlpha, drawflag, fadelerp);
378 }
379
380 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
381 float PolyDrawModelSurface(entity e, float i_s)
382 {
383         float i_t;
384         float n_t;
385         vector tri;
386         string tex;
387         tex = getsurfacetexture(e, i_s);
388         if not(tex)
389                 return 0; // this is beyond the last one
390         n_t = getsurfacenumtriangles(e, i_s);
391         for(i_t = 0; i_t < n_t; ++i_t)
392         {
393                 tri = getsurfacetriangle(e, i_s, i_t);
394                 R_BeginPolygon(tex, 0);
395                 R_PolygonVertex(getsurfacepoint(e, i_s, tri_x), getsurfacepointattribute(e, i_s, tri_x, SPA_TEXCOORDS0), '1 1 1', 1);
396                 R_PolygonVertex(getsurfacepoint(e, i_s, tri_y), getsurfacepointattribute(e, i_s, tri_y, SPA_TEXCOORDS0), '1 1 1', 1);
397                 R_PolygonVertex(getsurfacepoint(e, i_s, tri_z), getsurfacepointattribute(e, i_s, tri_z, SPA_TEXCOORDS0), '1 1 1', 1);
398                 R_EndPolygon();
399         }
400         return 1;
401 }
402 void PolyDrawModel(entity e)
403 {
404         float i_s;
405         for(i_s = 0; ; ++i_s)
406                 if(!PolyDrawModelSurface(e, i_s))
407                         break;
408 }
409
410 void DrawCircleClippedPic(vector centre, float radius, string pic, float f, vector rgb, float a, float drawflag)
411 {
412         float x, y, q, d;
413         vector ringsize, v, t;
414         ringsize = radius * '1 1 0';
415
416         x = cos(f * 2 * M_PI);
417         y = sin(f * 2 * M_PI);
418         q = fabs(x) + fabs(y);
419         x /= q;
420         y /= q;
421
422         if(f >= 1)
423         {
424                 // draw full rectangle
425                 R_BeginPolygon(pic, drawflag);
426                         v = centre;                     t = '0.5 0.5 0';
427                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
428                         R_PolygonVertex(v, t, rgb, a);
429
430                         v = centre;                     t = '0.5 0.5 0';
431                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
432                         R_PolygonVertex(v, t, rgb, a);
433
434                         v = centre;                     t = '0.5 0.5 0';
435                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
436                         R_PolygonVertex(v, t, rgb, a);
437
438                         v = centre;                     t = '0.5 0.5 0';
439                         v_y -= 0.5 * ringsize_y;        t -= '0.5 -0.5 0';
440                         R_PolygonVertex(v, t, rgb, a);
441                 R_EndPolygon();
442
443                 d = q - 1;
444                 if(d > 0)
445                 {
446                         R_BeginPolygon(pic, drawflag);
447                                 v = centre;                     t = '0.5 0.5 0';
448                                 R_PolygonVertex(v, t, rgb, a);
449
450                                 v = centre;                     t = '0.5 0.5 0';
451                                 v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
452                                 R_PolygonVertex(v, t, rgb, a);
453                 }
454         }
455         else if(f > 0.75)
456         {
457                 // draw upper and first triangle
458                 R_BeginPolygon(pic, drawflag);
459                         v = centre;                     t = '0.5 0.5 0';
460                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
461                         R_PolygonVertex(v, t, rgb, a);
462
463                         v = centre;                     t = '0.5 0.5 0';
464                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
465                         R_PolygonVertex(v, t, rgb, a);
466
467                         v = centre;                     t = '0.5 0.5 0';
468                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
469                         R_PolygonVertex(v, t, rgb, a);
470                 R_EndPolygon();
471                 R_BeginPolygon(pic, drawflag);
472                         v = centre;                     t = '0.5 0.5 0';
473                         R_PolygonVertex(v, t, rgb, a);
474
475                         v = centre;                     t = '0.5 0.5 0';
476                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
477                         R_PolygonVertex(v, t, rgb, a);
478
479                         v = centre;                     t = '0.5 0.5 0';
480                         v_y -= 0.5 * ringsize_y;        t -= '0.5 -0.5 0';
481                         R_PolygonVertex(v, t, rgb, a);
482
483                 d = q - 0.75;
484                 if(d <= 0)
485                         R_EndPolygon();
486         }
487         else if(f > 0.5)
488         {
489                 // draw upper triangle
490                 R_BeginPolygon(pic, drawflag);
491                         v = centre;                     t = '0.5 0.5 0';
492                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
493                         R_PolygonVertex(v, t, rgb, a);
494
495                         v = centre;                     t = '0.5 0.5 0';
496                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
497                         R_PolygonVertex(v, t, rgb, a);
498
499                         v = centre;                     t = '0.5 0.5 0';
500                         v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
501                         R_PolygonVertex(v, t, rgb, a);
502                 R_EndPolygon();
503
504                 d = q - 0.5;
505                 if(d > 0)
506                 {
507                         R_BeginPolygon(pic, drawflag);
508                                 v = centre;                     t = '0.5 0.5 0';
509                                 R_PolygonVertex(v, t, rgb, a);
510
511                                 v = centre;                     t = '0.5 0.5 0';
512                                 v_x -= 0.5 * ringsize_x;        t -= '0.5 0.5 0';
513                                 R_PolygonVertex(v, t, rgb, a);
514                 }
515         }
516         else if(f > 0.25)
517         {
518                 // draw first triangle
519                 R_BeginPolygon(pic, drawflag);
520                         v = centre;                     t = '0.5 0.5 0';
521                         R_PolygonVertex(v, t, rgb, a);
522
523                         v = centre;                     t = '0.5 0.5 0';
524                         v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
525                         R_PolygonVertex(v, t, rgb, a);
526
527                         v = centre;                     t = '0.5 0.5 0';
528                         v_y += 0.5 * ringsize_y;        t += '0.5 -0.5 0';
529                         R_PolygonVertex(v, t, rgb, a);
530
531                 d = q - 0.25;
532                 if(d <= 0)
533                         R_EndPolygon();
534         }
535         else
536         {
537                 d = q;
538                 if(d > 0)
539                 {
540                         R_BeginPolygon(pic, drawflag);
541                                 v = centre;                     t = '0.5 0.5 0';
542                                 R_PolygonVertex(v, t, rgb, a);
543
544                                 v = centre;                     t = '0.5 0.5 0';
545                                 v_x += 0.5 * ringsize_x;        t += '0.5 0.5 0';
546                                 R_PolygonVertex(v, t, rgb, a);
547                 }
548         }
549
550         if(d > 0)
551         {
552                         v = centre;                     t = '0.5 0.5 0';
553                         v_x += x * 0.5 * ringsize_x;    t += x * '0.5 0.5 0';
554                         v_y += y * 0.5 * ringsize_y;    t += y * '0.5 -0.5 0';
555                         R_PolygonVertex(v, t, rgb, a);
556                 R_EndPolygon();
557         }
558 }
559
560 const vector GETPLAYERORIGIN_ERROR = '1123581321 2357111317 3141592653'; // way out of bounds for anything on the map
561 vector getplayerorigin(float pl)
562 {
563         string s;
564         entity e;
565
566         e = CSQCModel_server2csqc(pl + 1);
567         if(e)
568                 return e.origin;
569
570 #ifndef NO_LEGACY_NETWORKING
571         s = getplayerkeyvalue(pl, "TEMPHACK_origin");
572         if(s != "")
573                 return stov(s);
574 #endif
575
576         e = entcs_receiver[pl];
577         if(e)
578                 return e.origin;
579
580         return GETPLAYERORIGIN_ERROR;
581 }
582
583 float getplayerisdead(float pl)
584 {
585         entity e;
586         
587         e = CSQCModel_server2csqc(pl + 1);
588         if(e)
589                 return e.csqcmodel_isdead;
590         
591         return FALSE;
592 }
593
594 void URI_Get_Callback(float id, float status, string data)
595 {
596         if(url_URI_Get_Callback(id, status, data))
597         {
598                 // handled
599         }
600         else if (id == URI_GET_DISCARD)
601         {
602                 // discard
603         }
604         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
605         {
606                 // sv_cmd curl
607                 Curl_URI_Get_Callback(id, status, data);
608         }
609         else
610         {
611                 print(sprintf(_("Received HTTP request data for an invalid id %d.\n"), id));
612         }
613 }
614
615 void draw_beginBoldFont()
616 {
617         drawfont = FONT_USER+2;
618 }
619
620 void draw_endBoldFont()
621 {
622         drawfont = FONT_USER+1;
623 }
624
625
626 #define MAX_ACCURACY_LEVELS 10
627 float acc_lev[MAX_ACCURACY_LEVELS];
628 vector acc_col[MAX_ACCURACY_LEVELS];
629 float acc_col_loadtime;
630 float acc_levels;
631 string acc_color_levels;
632 void Accuracy_LoadLevels()
633 {
634         float i;
635         if(autocvar_accuracy_color_levels != acc_color_levels)
636         {
637                 if(acc_color_levels)
638                         strunzone(acc_color_levels);
639                 acc_color_levels = strzone(autocvar_accuracy_color_levels);
640                 acc_levels = tokenize_console(acc_color_levels);
641                 if(acc_levels > MAX_ACCURACY_LEVELS)
642                         acc_levels = MAX_ACCURACY_LEVELS;
643                 if(acc_levels < 2)
644                         print("Warning: accuracy_color_levels must contain at least 2 values\n");
645
646                 for(i = 0; i < acc_levels; ++i)
647                         acc_lev[i] = stof(argv(i)) / 100.0;
648         }
649 }
650
651 void Accuracy_LoadColors()
652 {
653         float i;
654         if(time > acc_col_loadtime)
655         if(acc_levels >= 2)
656         {
657                 for(i = 0; i < acc_levels; ++i)
658                         acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
659                 acc_col_loadtime = time + 2;
660         }
661 }
662
663 vector Accuracy_GetColor(float accuracy)
664 {
665         float j, factor;
666         vector color;
667         if(acc_levels < 2)
668                 return '0 0 0'; // return black, can't determine the right color
669
670         // find the max level lower than acc
671         j = acc_levels-1;
672         while(j && accuracy < acc_lev[j])
673                 --j;
674
675         // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
676         factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
677         color = acc_col[j];
678         color = color + factor * (acc_col[j+1] - color);
679         return color;
680 }
681