]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/hud.qc
Merge branch 'master' into terencehill/translate_colors_2
[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 /*
118 ==================
119 HUD panels
120 ==================
121 */
122
123 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
124 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
125 {
126     TC(bool, vertical); TC(int, drawflag);
127         if(!length_ratio || !theAlpha)
128                 return;
129         if(length_ratio > 1)
130                 length_ratio = 1;
131         if (baralign == 3)
132         {
133                 if(length_ratio < -1)
134                         length_ratio = -1;
135         }
136         else if(length_ratio < 0)
137                 return;
138
139         vector square;
140         vector width, height;
141         if(vertical) {
142                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
143                 if(precache_pic(pic) == "") {
144                         pic = "gfx/hud/default/progressbar_vertical";
145                 }
146
147         if (baralign == 1) // bottom align
148                         theOrigin.y += (1 - length_ratio) * theSize.y;
149         else if (baralign == 2) // center align
150             theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
151         else if (baralign == 3) // center align, positive values down, negative up
152                 {
153                         theSize.y *= 0.5;
154                         if (length_ratio > 0)
155                                 theOrigin.y += theSize.y;
156                         else
157                         {
158                                 theOrigin.y += (1 + length_ratio) * theSize.y;
159                                 length_ratio = -length_ratio;
160                         }
161                 }
162                 theSize.y *= length_ratio;
163
164                 vector bH;
165                 width = eX * theSize.x;
166                 height = eY * theSize.y;
167                 if(theSize.y <= theSize.x * 2)
168                 {
169                         // button not high enough
170                         // draw just upper and lower part then
171                         square = eY * theSize.y * 0.5;
172                         bH = eY * (0.25 * theSize.y / (theSize.x * 2));
173                         drawsubpic(theOrigin,          square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
174                         drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
175                 }
176                 else
177                 {
178                         square = eY * theSize.x;
179                         drawsubpic(theOrigin,                   width   +     square, pic, '0 0    0', '1 0.25 0', theColor, theAlpha, drawflag);
180                         drawsubpic(theOrigin +          square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5  0', theColor, theAlpha, drawflag);
181                         drawsubpic(theOrigin + height - square, width   +     square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
182                 }
183         } else {
184                 pic = strcat(hud_skin_path, "/", pic);
185                 if(precache_pic(pic) == "") {
186                         pic = "gfx/hud/default/progressbar";
187                 }
188
189                 if (baralign == 1) // right align
190                         theOrigin.x += (1 - length_ratio) * theSize.x;
191         else if (baralign == 2) // center align
192             theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
193         else if (baralign == 3) // center align, positive values on the right, negative on the left
194                 {
195                         theSize.x *= 0.5;
196                         if (length_ratio > 0)
197                                 theOrigin.x += theSize.x;
198                         else
199                         {
200                                 theOrigin.x += (1 + length_ratio) * theSize.x;
201                                 length_ratio = -length_ratio;
202                         }
203                 }
204                 theSize.x *= length_ratio;
205
206                 vector bW;
207                 width = eX * theSize.x;
208                 height = eY * theSize.y;
209                 if(theSize.x <= theSize.y * 2)
210                 {
211                         // button not wide enough
212                         // draw just left and right part then
213                         square = eX * theSize.x * 0.5;
214                         bW = eX * (0.25 * theSize.x / (theSize.y * 2));
215                         drawsubpic(theOrigin,          square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
216                         drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
217                 }
218                 else
219                 {
220                         square = eX * theSize.y;
221                         drawsubpic(theOrigin,                  height  +     square, pic, '0    0 0', '0.25 1 0', theColor, theAlpha, drawflag);
222                         drawsubpic(theOrigin +         square, theSize - 2 * square, pic, '0.25 0 0', '0.5  1 0', theColor, theAlpha, drawflag);
223                         drawsubpic(theOrigin + width - square, height  +     square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
224                 }
225         }
226 }
227
228 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
229 {
230     TC(int, drawflag);
231         if(!theAlpha)
232                 return;
233
234         string pic;
235         pic = strcat(hud_skin_path, "/num_leading");
236         if(precache_pic(pic) == "") {
237                 pic = "gfx/hud/default/num_leading";
238         }
239
240         drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
241         if(mySize.x/mySize.y > 2)
242                 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);
243         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);
244 }
245
246 void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, bool vertical, int icon_right_align, vector color, float theAlpha, float fadelerp)
247 {
248     TC(bool, vertical); TC(int, icon_right_align);
249         vector newPos = '0 0 0', newSize = '0 0 0';
250         vector picpos, numpos;
251
252         if (vertical)
253         {
254                 if(mySize.y/mySize.x > 2)
255                 {
256                         newSize.y = 2 * mySize.x;
257                         newSize.x = mySize.x;
258
259                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
260                         newPos.x = myPos.x;
261                 }
262                 else
263                 {
264                         newSize.x = 1/2 * mySize.y;
265                         newSize.y = mySize.y;
266
267                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
268                         newPos.y = myPos.y;
269                 }
270
271                 if(icon_right_align)
272                 {
273                         numpos = newPos;
274                         picpos = newPos + eY * newSize.x;
275                 }
276                 else
277                 {
278                         picpos = newPos;
279                         numpos = newPos + eY * newSize.x;
280                 }
281
282                 newSize.y /= 2;
283                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
284                 // make number smaller than icon, it looks better
285                 // reduce only y to draw numbers with different number of digits with the same y size
286                 numpos.y += newSize.y * ((1 - 0.7) / 2);
287                 newSize.y *= 0.7;
288                 drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
289                 return;
290         }
291
292         if(mySize.x/mySize.y > 3)
293         {
294                 newSize.x = 3 * mySize.y;
295                 newSize.y = mySize.y;
296
297                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
298                 newPos.y = myPos.y;
299         }
300         else
301         {
302                 newSize.y = 1/3 * mySize.x;
303                 newSize.x = mySize.x;
304
305                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
306                 newPos.x = myPos.x;
307         }
308
309         if(icon_right_align) // right align
310         {
311                 numpos = newPos;
312                 picpos = newPos + eX * 2 * newSize.y;
313         }
314         else // left align
315         {
316                 numpos = newPos + eX * newSize.y;
317                 picpos = newPos;
318         }
319
320         // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
321         // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
322         drawstring_aspect_expanding(numpos, ftos(x), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
323         drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
324 }
325
326 void DrawNumIcon(vector myPos, vector mySize, float x, string icon, bool vertical, int icon_right_align, vector color, float theAlpha)
327 {
328     TC(bool, vertical); TC(int, icon_right_align);
329         DrawNumIcon_expanding(myPos, mySize, x, icon, vertical, icon_right_align, color, theAlpha, 0);
330 }
331
332 #include "all.inc"
333
334 /*
335 ==================
336 Main HUD system
337 ==================
338 */
339
340 void CSQC_BUMBLE_GUN_HUD();
341
342 void HUD_Vehicle()
343 {
344         if(autocvar__hud_configure) return;
345         if(intermission == 2) return;
346
347         if(hud == HUD_BUMBLEBEE_GUN)
348                 CSQC_BUMBLE_GUN_HUD();
349         else {
350                 Vehicle info = Vehicles_from(hud);
351                 info.vr_hud(info);
352         }
353 }
354
355 bool HUD_Panel_CheckFlags(int showflags)
356 {
357     TC(int, showflags);
358         if ( HUD_Minigame_Showpanels() )
359                 return showflags & PANEL_SHOW_MINIGAME;
360         if(intermission == 2)
361                 return showflags & PANEL_SHOW_MAPVOTE;
362         return showflags & PANEL_SHOW_MAINGAME;
363 }
364
365 void HUD_Panel_Draw(entity panent)
366 {
367         panel = panent;
368         if(autocvar__hud_configure)
369         {
370                 if(panel.panel_configflags & PANEL_CONFIG_MAIN)
371                         panel.panel_draw();
372         }
373         else if(HUD_Panel_CheckFlags(panel.panel_showflags))
374                 panel.panel_draw();
375 }
376
377 void HUD_Reset()
378 {
379         // reset gametype specific icons
380         if(gametype == MAPINFO_TYPE_CTF)
381                 HUD_Mod_CTF_Reset();
382 }
383
384 void HUD_Main()
385 {
386         int i;
387         // global hud theAlpha fade
388         if(menu_enabled == 1)
389                 hud_fade_alpha = 1;
390         else
391                 hud_fade_alpha = (1 - autocvar__menu_alpha);
392
393         if(scoreboard_fade_alpha)
394                 hud_fade_alpha = (1 - scoreboard_fade_alpha);
395
396         HUD_Configure_Frame();
397
398         // panels that we want to be active together with the scoreboard
399         // they must fade only when the menu does
400         if(scoreboard_fade_alpha == 1)
401         {
402                 HUD_Panel_Draw(HUD_PANEL(CENTERPRINT));
403                 return;
404         }
405
406         if(!autocvar__hud_configure && !hud_fade_alpha)
407         {
408                 hud_fade_alpha = 1;
409                 HUD_Panel_Draw(HUD_PANEL(VOTE));
410                 hud_fade_alpha = 0;
411                 return;
412         }
413
414         // Drawing stuff
415         if (hud_skin_prev != autocvar_hud_skin)
416         {
417                 if (hud_skin_path)
418                         strunzone(hud_skin_path);
419                 hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
420                 if (hud_skin_prev)
421                         strunzone(hud_skin_prev);
422                 hud_skin_prev = strzone(autocvar_hud_skin);
423         }
424
425         // draw the dock
426         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
427         {
428                 int f;
429                 vector color;
430                 float hud_dock_color_team = autocvar_hud_dock_color_team;
431                 if((teamplay) && hud_dock_color_team) {
432                         if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
433                                 color = '1 0 0' * hud_dock_color_team;
434                         else
435                                 color = myteamcolors * hud_dock_color_team;
436                 }
437                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
438                         color = '1 0 0' * hud_dock_color_team;
439                 }
440                 else
441                 {
442                         string hud_dock_color = autocvar_hud_dock_color;
443                         if(hud_dock_color == "shirt") {
444                                 f = stof(getplayerkeyvalue(current_player, "colors"));
445                                 color = colormapPaletteColor(floor(f / 16), 0);
446                         }
447                         else if(hud_dock_color == "pants") {
448                                 f = stof(getplayerkeyvalue(current_player, "colors"));
449                                 color = colormapPaletteColor(f % 16, 1);
450                         }
451                         else
452                                 color = stov(hud_dock_color);
453                 }
454
455                 string pic;
456                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
457                 if(precache_pic(pic) == "") {
458                         pic = strcat(hud_skin_path, "/dock_medium");
459                         if(precache_pic(pic) == "") {
460                                 pic = "gfx/hud/default/dock_medium";
461                         }
462                 }
463                 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...
464         }
465
466         // cache the panel order into the panel_order array
467         if(autocvar__hud_panelorder != hud_panelorder_prev) {
468                 for(i = 0; i < hud_panels_COUNT; ++i)
469                         panel_order[i] = -1;
470                 string s = "";
471                 int p_num;
472                 bool warning = false;
473                 int argc = tokenize_console(autocvar__hud_panelorder);
474                 if (argc > hud_panels_COUNT)
475                         warning = true;
476                 //first detect wrong/missing panel numbers
477                 for(i = 0; i < hud_panels_COUNT; ++i) {
478                         p_num = stoi(argv(i));
479                         if (p_num >= 0 && p_num < hud_panels_COUNT) { //correct panel number?
480                                 if (panel_order[p_num] == -1) //found for the first time?
481                                         s = strcat(s, ftos(p_num), " ");
482                                 panel_order[p_num] = 1; //mark as found
483                         }
484                         else
485                                 warning = true;
486                 }
487                 for(i = 0; i < hud_panels_COUNT; ++i) {
488                         if (panel_order[i] == -1) {
489                                 warning = true;
490                                 s = strcat(s, ftos(i), " "); //add missing panel number
491                         }
492                 }
493                 if (warning)
494                         LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder\n");
495
496                 cvar_set("_hud_panelorder", s);
497                 if(hud_panelorder_prev)
498                         strunzone(hud_panelorder_prev);
499                 hud_panelorder_prev = strzone(s);
500
501                 //now properly set panel_order
502                 tokenize_console(s);
503                 for(i = 0; i < hud_panels_COUNT; ++i) {
504                         panel_order[i] = stof(argv(i));
505                 }
506         }
507
508         hud_draw_maximized = 0;
509         // draw panels in the order specified by panel_order array
510         for(i = hud_panels_COUNT - 1; i >= 0; --i)
511                 HUD_Panel_Draw(hud_panels_from(panel_order[i]));
512
513         HUD_Vehicle();
514
515         hud_draw_maximized = 1; // panels that may be maximized must check this var
516         // draw maximized panels on top
517         if(hud_panel_radar_maximized)
518                 HUD_Panel_Draw(HUD_PANEL(RADAR));
519         if(autocvar__con_chat_maximized)
520                 HUD_Panel_Draw(HUD_PANEL(CHAT));
521         if(hud_panel_quickmenu)
522                 HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
523
524         if (scoreboard_active || intermission == 2)
525                 HUD_Reset();
526
527         HUD_Configure_PostDraw();
528
529         hud_configure_prev = autocvar__hud_configure;
530 }