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