Makefile: use `-I.`
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / hud.qc
1 #include "hud.qh"
2
3 #include "hud_config.qh"
4 #include "../mapvoting.qh"
5 #include "../scoreboard.qh"
6 #include "../teamradar.qh"
7 #include <common/t_items.qh>
8 #include <common/deathtypes/all.qh>
9 #include <common/items/all.qc>
10 #include <common/mapinfo.qh>
11 #include <common/mutators/mutator/waypoints/all.qh>
12 #include <common/stats.qh>
13 #include <lib/csqcmodel/cl_player.qh>
14 // TODO: remove
15 #include <server/mutators/mutator/gamemode_ctf.qc>
16
17
18 /*
19 ==================
20 Misc HUD functions
21 ==================
22 */
23
24 vector HUD_Get_Num_Color (float x, float maxvalue)
25 {
26         float blinkingamt;
27         vector color;
28         if(x >= maxvalue) {
29                 color.x = sin(2*M_PI*time);
30                 color.y = 1;
31                 color.z = sin(2*M_PI*time);
32         }
33         else if(x > maxvalue * 0.75) {
34                 color.x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
35                 color.y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
36                 color.z = 0;
37         }
38         else if(x > maxvalue * 0.5) {
39                 color.x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
40                 color.y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
41                 color.z = 1 - (x-100)*0.02; // blue value between 1 -> 0
42         }
43         else if(x > maxvalue * 0.25) {
44                 color.x = 1;
45                 color.y = 1;
46                 color.z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
47         }
48         else if(x > maxvalue * 0.1) {
49                 color.x = 1;
50                 color.y = (x-20)*90/27/100; // green value between 0 -> 1
51                 color.z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
52         }
53         else {
54                 color.x = 1;
55                 color.y = 0;
56                 color.z = 0;
57         }
58
59         blinkingamt = (1 - x/maxvalue/0.25);
60         if(blinkingamt > 0)
61         {
62                 color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
63                 color.y = color.y - color.y * blinkingamt * sin(2*M_PI*time);
64                 color.z = color.z - color.z * blinkingamt * sin(2*M_PI*time);
65         }
66         return color;
67 }
68
69 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
70 {
71         float aspect = size_y / size_x;
72         return bound(1, floor((sqrt(4 * item_aspect * aspect * item_count + aspect * aspect) + aspect + 0.5) / 2), item_count);
73 }
74
75 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
76 {
77         float columns, rows;
78         float ratio, best_ratio = 0;
79         float best_columns = 1, best_rows = 1;
80         bool vertical = (psize.x / psize.y >= item_aspect);
81         if(vertical)
82         {
83                 psize = eX * psize.y + eY * psize.x;
84                 item_aspect = 1 / item_aspect;
85         }
86
87         rows = ceil(sqrt(item_count));
88         columns = ceil(item_count/rows);
89         while(columns >= 1)
90         {
91                 ratio = (psize.x/columns) / (psize.y/rows);
92                 if(ratio > item_aspect)
93                         ratio = item_aspect * item_aspect / ratio;
94
95                 if(ratio <= best_ratio)
96                         break; // ratio starts decreasing by now, skip next configurations
97
98                 best_columns = columns;
99                 best_rows = rows;
100                 best_ratio = ratio;
101
102                 if(columns == 1)
103                         break;
104
105                 --columns;
106                 rows = ceil(item_count/columns);
107         }
108
109         if(vertical)
110                 return eX * best_rows + eY * best_columns;
111         else
112                 return eX * best_columns + eY * best_rows;
113 }
114
115 // return the string of the onscreen race timer
116 string MakeRaceString(int cp, float mytime, float theirtime, float lapdelta, string theirname)
117 {
118         string col;
119         string timestr;
120         string cpname;
121         string lapstr;
122         lapstr = "";
123
124         if(theirtime == 0) // goal hit
125         {
126                 if(mytime > 0)
127                 {
128                         timestr = strcat("+", ftos_decimals(+mytime, TIME_DECIMALS));
129                         col = "^1";
130                 }
131                 else if(mytime == 0)
132                 {
133                         timestr = "+0.0";
134                         col = "^3";
135                 }
136                 else
137                 {
138                         timestr = strcat("-", ftos_decimals(-mytime, TIME_DECIMALS));
139                         col = "^2";
140                 }
141
142                 if(lapdelta > 0)
143                 {
144                         lapstr = sprintf(_(" (-%dL)"), lapdelta);
145                         col = "^2";
146                 }
147                 else if(lapdelta < 0)
148                 {
149                         lapstr = sprintf(_(" (+%dL)"), -lapdelta);
150                         col = "^1";
151                 }
152         }
153         else if(theirtime > 0) // anticipation
154         {
155                 if(mytime >= theirtime)
156                         timestr = strcat("+", ftos_decimals(mytime - theirtime, TIME_DECIMALS));
157                 else
158                         timestr = TIME_ENCODED_TOSTRING(TIME_ENCODE(theirtime));
159                 col = "^3";
160         }
161         else
162         {
163                 col = "^7";
164                 timestr = "";
165         }
166
167         if(cp == 254)
168                 cpname = _("Start line");
169         else if(cp == 255)
170                 cpname = _("Finish line");
171         else if(cp)
172                 cpname = sprintf(_("Intermediate %d"), cp);
173         else
174                 cpname = _("Finish line");
175
176         if(theirtime < 0)
177                 return strcat(col, cpname);
178         else if(theirname == "")
179                 return strcat(col, sprintf("%s (%s)", cpname, timestr));
180         else
181                 return strcat(col, sprintf("%s (%s %s)", cpname, timestr, strcat(theirname, col, lapstr)));
182 }
183
184 // Check if the given name already exist in race rankings? In that case, where? (otherwise return 0)
185 int race_CheckName(string net_name)
186 {
187         int i;
188         for (i=RANKINGS_CNT-1;i>=0;--i)
189                 if(grecordholder[i] == net_name)
190                         return i+1;
191         return 0;
192 }
193
194 /*
195 ==================
196 HUD panels
197 ==================
198 */
199
200 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
201 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
202 {
203         if(!length_ratio || !theAlpha)
204                 return;
205         if(length_ratio > 1)
206                 length_ratio = 1;
207         if (baralign == 3)
208         {
209                 if(length_ratio < -1)
210                         length_ratio = -1;
211         }
212         else if(length_ratio < 0)
213                 return;
214
215         vector square;
216         vector width, height;
217         if(vertical) {
218                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
219                 if(precache_pic(pic) == "") {
220                         pic = "gfx/hud/default/progressbar_vertical";
221                 }
222
223         if (baralign == 1) // bottom align
224                         theOrigin.y += (1 - length_ratio) * theSize.y;
225         else if (baralign == 2) // center align
226             theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
227         else if (baralign == 3) // center align, positive values down, negative up
228                 {
229                         theSize.y *= 0.5;
230                         if (length_ratio > 0)
231                                 theOrigin.y += theSize.y;
232                         else
233                         {
234                                 theOrigin.y += (1 + length_ratio) * theSize.y;
235                                 length_ratio = -length_ratio;
236                         }
237                 }
238                 theSize.y *= length_ratio;
239
240                 vector bH;
241                 width = eX * theSize.x;
242                 height = eY * theSize.y;
243                 if(theSize.y <= theSize.x * 2)
244                 {
245                         // button not high enough
246                         // draw just upper and lower part then
247                         square = eY * theSize.y * 0.5;
248                         bH = eY * (0.25 * theSize.y / (theSize.x * 2));
249                         drawsubpic(theOrigin,          square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
250                         drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
251                 }
252                 else
253                 {
254                         square = eY * theSize.x;
255                         drawsubpic(theOrigin,                   width   +     square, pic, '0 0    0', '1 0.25 0', theColor, theAlpha, drawflag);
256                         drawsubpic(theOrigin +          square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5  0', theColor, theAlpha, drawflag);
257                         drawsubpic(theOrigin + height - square, width   +     square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
258                 }
259         } else {
260                 pic = strcat(hud_skin_path, "/", pic);
261                 if(precache_pic(pic) == "") {
262                         pic = "gfx/hud/default/progressbar";
263                 }
264
265                 if (baralign == 1) // right align
266                         theOrigin.x += (1 - length_ratio) * theSize.x;
267         else if (baralign == 2) // center align
268             theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
269         else if (baralign == 3) // center align, positive values on the right, negative on the left
270                 {
271                         theSize.x *= 0.5;
272                         if (length_ratio > 0)
273                                 theOrigin.x += theSize.x;
274                         else
275                         {
276                                 theOrigin.x += (1 + length_ratio) * theSize.x;
277                                 length_ratio = -length_ratio;
278                         }
279                 }
280                 theSize.x *= length_ratio;
281
282                 vector bW;
283                 width = eX * theSize.x;
284                 height = eY * theSize.y;
285                 if(theSize.x <= theSize.y * 2)
286                 {
287                         // button not wide enough
288                         // draw just left and right part then
289                         square = eX * theSize.x * 0.5;
290                         bW = eX * (0.25 * theSize.x / (theSize.y * 2));
291                         drawsubpic(theOrigin,          square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
292                         drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
293                 }
294                 else
295                 {
296                         square = eX * theSize.y;
297                         drawsubpic(theOrigin,                  height  +     square, pic, '0    0 0', '0.25 1 0', theColor, theAlpha, drawflag);
298                         drawsubpic(theOrigin +         square, theSize - 2 * square, pic, '0.25 0 0', '0.5  1 0', theColor, theAlpha, drawflag);
299                         drawsubpic(theOrigin + width - square, height  +     square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
300                 }
301         }
302 }
303
304 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
305 {
306         if(!theAlpha)
307                 return;
308
309         string pic;
310         pic = strcat(hud_skin_path, "/num_leading");
311         if(precache_pic(pic) == "") {
312                 pic = "gfx/hud/default/num_leading";
313         }
314
315         drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
316         if(mySize.x/mySize.y > 2)
317                 drawsubpic(pos + eX * mySize.y, eX * (mySize.x - 2 * mySize.y) + eY * mySize.y, pic, '0.25 0 0', '0.5 1 0', color, theAlpha, drawflag);
318         drawsubpic(pos + eX * mySize.x - eX * min(mySize.x * 0.5, mySize.y), eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0.75 0 0', '0.25 1 0', color, theAlpha, drawflag);
319 }
320
321 void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, bool vertical, bool icon_right_align, vector color, float theAlpha, float fadelerp)
322 {
323         vector newPos = '0 0 0', newSize = '0 0 0';
324         vector picpos, numpos;
325
326         if (vertical)
327         {
328                 if(mySize.y/mySize.x > 2)
329                 {
330                         newSize.y = 2 * mySize.x;
331                         newSize.x = mySize.x;
332
333                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
334                         newPos.x = myPos.x;
335                 }
336                 else
337                 {
338                         newSize.x = 1/2 * mySize.y;
339                         newSize.y = mySize.y;
340
341                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
342                         newPos.y = myPos.y;
343                 }
344
345                 if(icon_right_align)
346                 {
347                         numpos = newPos;
348                         picpos = newPos + eY * newSize.x;
349                 }
350                 else
351                 {
352                         picpos = newPos;
353                         numpos = newPos + eY * newSize.x;
354                 }
355
356                 newSize.y /= 2;
357                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
358                 // make number smaller than icon, it looks better
359                 // reduce only y to draw numbers with different number of digits with the same y size
360                 numpos.y += newSize.y * ((1 - 0.7) / 2);
361                 newSize.y *= 0.7;
362                 drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
363                 return;
364         }
365
366         if(mySize.x/mySize.y > 3)
367         {
368                 newSize.x = 3 * mySize.y;
369                 newSize.y = mySize.y;
370
371                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
372                 newPos.y = myPos.y;
373         }
374         else
375         {
376                 newSize.y = 1/3 * mySize.x;
377                 newSize.x = mySize.x;
378
379                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
380                 newPos.x = myPos.x;
381         }
382
383         if(icon_right_align) // right align
384         {
385                 numpos = newPos;
386                 picpos = newPos + eX * 2 * newSize.y;
387         }
388         else // left align
389         {
390                 numpos = newPos + eX * newSize.y;
391                 picpos = newPos;
392         }
393
394         // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
395         // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
396         drawstring_aspect_expanding(numpos, ftos(x), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
397         drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
398 }
399
400 void DrawNumIcon(vector myPos, vector mySize, float x, string icon, bool vertical, bool icon_right_align, vector color, float theAlpha)
401 {
402         DrawNumIcon_expanding(myPos, mySize, x, icon, vertical, icon_right_align, color, theAlpha, 0);
403 }
404
405 #include "all.inc"
406
407 /*
408 ==================
409 Main HUD system
410 ==================
411 */
412
413 void HUD_Vehicle()
414 {
415         if(autocvar__hud_configure) return;
416         if(intermission == 2) return;
417
418         if(hud == HUD_BUMBLEBEE_GUN)
419                 CSQC_BUMBLE_GUN_HUD();
420         else {
421                 Vehicle info = get_vehicleinfo(hud);
422                 info.vr_hud(info);
423         }
424 }
425
426 bool HUD_Panel_CheckFlags(int showflags)
427 {
428         if ( HUD_Minigame_Showpanels() )
429                 return showflags & PANEL_SHOW_MINIGAME;
430         if(intermission == 2)
431                 return showflags & PANEL_SHOW_MAPVOTE;
432         return showflags & PANEL_SHOW_MAINGAME;
433 }
434
435 void HUD_Panel_Draw(entity panent)
436 {
437         panel = panent;
438         if(autocvar__hud_configure)
439         {
440                 if(panel.panel_configflags & PANEL_CONFIG_MAIN)
441                         panel.panel_draw();
442         }
443         else if(HUD_Panel_CheckFlags(panel.panel_showflags))
444                 panel.panel_draw();
445 }
446
447 void HUD_Reset()
448 {
449         // reset gametype specific icons
450         if(gametype == MAPINFO_TYPE_CTF)
451                 HUD_Mod_CTF_Reset();
452 }
453
454 void HUD_Main()
455 {
456         int i;
457         // global hud theAlpha fade
458         if(menu_enabled == 1)
459                 hud_fade_alpha = 1;
460         else
461                 hud_fade_alpha = (1 - autocvar__menu_alpha);
462
463         if(scoreboard_fade_alpha)
464                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
465
466         HUD_Configure_Frame();
467
468         // panels that we want to be active together with the scoreboard
469         // they must fade only when the menu does
470         if(scoreboard_fade_alpha == 1)
471         {
472                 HUD_Panel_Draw(HUD_PANEL(CENTERPRINT));
473                 return;
474         }
475
476         if(!autocvar__hud_configure && !hud_fade_alpha)
477         {
478                 hud_fade_alpha = 1;
479                 HUD_Panel_Draw(HUD_PANEL(VOTE));
480                 hud_fade_alpha = 0;
481                 return;
482         }
483
484         // Drawing stuff
485         if (hud_skin_prev != autocvar_hud_skin)
486         {
487                 if (hud_skin_path)
488                         strunzone(hud_skin_path);
489                 hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
490                 if (hud_skin_prev)
491                         strunzone(hud_skin_prev);
492                 hud_skin_prev = strzone(autocvar_hud_skin);
493         }
494
495         // draw the dock
496         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
497         {
498                 int f;
499                 vector color;
500                 float hud_dock_color_team = autocvar_hud_dock_color_team;
501                 if((teamplay) && hud_dock_color_team) {
502                         if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
503                                 color = '1 0 0' * hud_dock_color_team;
504                         else
505                                 color = myteamcolors * hud_dock_color_team;
506                 }
507                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
508                         color = '1 0 0' * hud_dock_color_team;
509                 }
510                 else
511                 {
512                         string hud_dock_color = autocvar_hud_dock_color;
513                         if(hud_dock_color == "shirt") {
514                                 f = stof(getplayerkeyvalue(current_player, "colors"));
515                                 color = colormapPaletteColor(floor(f / 16), 0);
516                         }
517                         else if(hud_dock_color == "pants") {
518                                 f = stof(getplayerkeyvalue(current_player, "colors"));
519                                 color = colormapPaletteColor(f % 16, 1);
520                         }
521                         else
522                                 color = stov(hud_dock_color);
523                 }
524
525                 string pic;
526                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
527                 if(precache_pic(pic) == "") {
528                         pic = strcat(hud_skin_path, "/dock_medium");
529                         if(precache_pic(pic) == "") {
530                                 pic = "gfx/hud/default/dock_medium";
531                         }
532                 }
533                 drawpic('0 0 0', pic, eX * vid_conwidth + eY * vid_conheight, color, autocvar_hud_dock_alpha * hud_fade_alpha, DRAWFLAG_NORMAL); // no aspect ratio forcing on dock...
534         }
535
536         // cache the panel order into the panel_order array
537         if(autocvar__hud_panelorder != hud_panelorder_prev) {
538                 for(i = 0; i < hud_panels_COUNT; ++i)
539                         panel_order[i] = -1;
540                 string s = "";
541                 int p_num;
542                 bool warning = false;
543                 int argc = tokenize_console(autocvar__hud_panelorder);
544                 if (argc > hud_panels_COUNT)
545                         warning = true;
546                 //first detect wrong/missing panel numbers
547                 for(i = 0; i < hud_panels_COUNT; ++i) {
548                         p_num = stoi(argv(i));
549                         if (p_num >= 0 && p_num < hud_panels_COUNT) { //correct panel number?
550                                 if (panel_order[p_num] == -1) //found for the first time?
551                                         s = strcat(s, ftos(p_num), " ");
552                                 panel_order[p_num] = 1; //mark as found
553                         }
554                         else
555                                 warning = true;
556                 }
557                 for(i = 0; i < hud_panels_COUNT; ++i) {
558                         if (panel_order[i] == -1) {
559                                 warning = true;
560                                 s = strcat(s, ftos(i), " "); //add missing panel number
561                         }
562                 }
563                 if (warning)
564                         LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder\n");
565
566                 cvar_set("_hud_panelorder", s);
567                 if(hud_panelorder_prev)
568                         strunzone(hud_panelorder_prev);
569                 hud_panelorder_prev = strzone(s);
570
571                 //now properly set panel_order
572                 tokenize_console(s);
573                 for(i = 0; i < hud_panels_COUNT; ++i) {
574                         panel_order[i] = stof(argv(i));
575                 }
576         }
577
578         hud_draw_maximized = 0;
579         // draw panels in the order specified by panel_order array
580         for(i = hud_panels_COUNT - 1; i >= 0; --i)
581                 HUD_Panel_Draw(hud_panels_from(panel_order[i]));
582
583         HUD_Vehicle();
584
585         hud_draw_maximized = 1; // panels that may be maximized must check this var
586         // draw maximized panels on top
587         if(hud_panel_radar_maximized)
588                 HUD_Panel_Draw(HUD_PANEL(RADAR));
589         if(autocvar__con_chat_maximized)
590                 HUD_Panel_Draw(HUD_PANEL(CHAT));
591         if(hud_panel_quickmenu)
592                 HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
593
594         if (scoreboard_active || intermission == 2)
595                 HUD_Reset();
596
597         HUD_Configure_PostDraw();
598
599         hud_configure_prev = autocvar__hud_configure;
600 }