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