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