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