3 #include <client/defs.qh>
4 #include <client/miscfunctions.qh>
5 #include "panel/scoreboard.qh"
6 #include "hud_config.qh"
7 #include "../mapvoting.qh"
8 #include "../teamradar.qh"
9 #include <common/minigames/cl_minigames.qh>
10 #include <common/t_items.qh>
11 #include <common/deathtypes/all.qh>
12 #include <common/ent_cs.qh>
13 #include <common/items/_mod.qh>
14 #include <common/mapinfo.qh>
15 #include <common/vehicles/all.qh>
16 #include <common/mutators/mutator/waypoints/all.qh>
17 #include <common/stats.qh>
18 #include <lib/csqcmodel/cl_player.qh>
19 #include <server/mutators/mutator/gamemode_ctf.qh> // TODO: remove
28 vector HUD_Get_Num_Color (float hp, float maxvalue)
33 color.x = sin(2*M_PI*time);
35 color.z = sin(2*M_PI*time);
37 else if(hp > maxvalue * 0.75) {
38 color.x = 0.4 - (hp-150)*0.02 * 0.4; //red value between 0.4 -> 0
39 color.y = 0.9 + (hp-150)*0.02 * 0.1; // green value between 0.9 -> 1
42 else if(hp > maxvalue * 0.5) {
43 color.x = 1 - (hp-100)*0.02 * 0.6; //red value between 1 -> 0.4
44 color.y = 1 - (hp-100)*0.02 * 0.1; // green value between 1 -> 0.9
45 color.z = 1 - (hp-100)*0.02; // blue value between 1 -> 0
47 else if(hp > maxvalue * 0.25) {
50 color.z = 0.2 + (hp-50)*0.02 * 0.8; // blue value between 0.2 -> 1
52 else if(hp > maxvalue * 0.1) {
54 color.y = (hp-20)*90/27/100; // green value between 0 -> 1
55 color.z = (hp-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
63 blinkingamt = (1 - hp/maxvalue/0.25);
66 color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
67 color.y = color.y - color.y * blinkingamt * sin(2*M_PI*time);
68 color.z = color.z - color.z * blinkingamt * sin(2*M_PI*time);
73 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
76 float aspect = size_y / size_x;
77 return bound(1, floor((sqrt(4 * item_aspect * aspect * item_count + aspect * aspect) + aspect + 0.5) / 2), item_count);
80 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
84 float ratio, best_ratio = 0;
85 float best_columns = 1, best_rows = 1;
86 bool vertical = (psize.x / psize.y >= item_aspect);
89 psize = eX * psize.y + eY * psize.x;
90 item_aspect = 1 / item_aspect;
93 rows = ceil(sqrt(item_count));
94 columns = ceil(item_count/rows);
97 ratio = (psize.x/columns) / (psize.y/rows);
98 if(ratio > item_aspect)
99 ratio = item_aspect * item_aspect / ratio;
101 if(ratio <= best_ratio)
102 break; // ratio starts decreasing by now, skip next configurations
104 best_columns = columns;
112 rows = ceil(item_count/columns);
115 return (vertical) ? vec2(best_rows, best_columns) : vec2(best_columns, best_rows);
124 void HUD_Panel_LoadCvars()
126 // NOTE: in hud_configure mode cvars must be reloaded every frame
127 if (panel.update_time <= time)
129 panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));
130 panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));
131 HUD_Panel_ScalePosSize();
132 panel_bg_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg"));
133 panel_bg_color_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color"));
134 panel_bg_color_team_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color_team"));
135 panel_bg_alpha_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_alpha"));
136 panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));
137 panel_bg_padding_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_padding"));
139 if (panel.current_panel_bg != "0")
141 HUD_Panel_GetBgAlpha();
142 HUD_Panel_GetBorder();
144 HUD_Panel_GetColorTeam();
145 HUD_Panel_GetColor();
146 HUD_Panel_GetFgAlpha();
147 HUD_Panel_GetPadding();
148 panel.current_panel_bg_alpha = panel_bg_alpha;
149 panel.current_panel_fg_alpha = panel_fg_alpha;
150 if (hud_configure_menu_open == 2 && panel == highlightedPanel)
151 HUD_Panel_UpdatePosSize_ForMenu();
154 panel_bg_alpha *= hud_fade_alpha * panel_fade_alpha;
155 panel_fg_alpha *= hud_fade_alpha * panel_fade_alpha;
157 panel.current_panel_pos = panel_pos;
158 panel.current_panel_size = panel_size;
159 panel.current_panel_bg_border = panel_bg_border;
160 panel.current_panel_bg_color = panel_bg_color;
161 panel.current_panel_bg_color_team = panel_bg_color_team;
162 panel.current_panel_bg_padding = panel_bg_padding;
163 panel.update_time = (autocvar__hud_configure) ? time : time + autocvar_hud_panel_update_interval;
167 panel_pos = panel.current_panel_pos;
168 panel_size = panel.current_panel_size;
169 panel_bg_alpha = panel.current_panel_bg_alpha * hud_fade_alpha * panel_fade_alpha;
170 panel_bg_border = panel.current_panel_bg_border;
171 panel_bg_color = panel.current_panel_bg_color;
172 panel_bg_color_team = panel.current_panel_bg_color_team;
173 panel_bg_padding = panel.current_panel_bg_padding;
174 panel_fg_alpha = panel.current_panel_fg_alpha * hud_fade_alpha * panel_fade_alpha;
177 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
178 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
180 TC(bool, vertical); TC(int, drawflag);
181 if(!length_ratio || !theAlpha)
187 if(length_ratio < -1)
190 else if(length_ratio < 0)
193 theOrigin = HUD_Shift(theOrigin);
194 theSize = HUD_Scale(theSize);
197 vector width, height;
199 pic = strcat(hud_skin_path, "/", pic, "_vertical");
200 if(precache_pic(pic) == "") {
201 pic = "gfx/hud/default/progressbar_vertical";
204 if (baralign == 1) // bottom align
205 theOrigin.y += (1 - length_ratio) * theSize.y;
206 else if (baralign == 2) // center align
207 theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
208 else if (baralign == 3) // center align, positive values down, negative up
211 if (length_ratio > 0)
212 theOrigin.y += theSize.y;
215 theOrigin.y += (1 + length_ratio) * theSize.y;
216 length_ratio = -length_ratio;
219 theSize.y *= length_ratio;
222 width = eX * theSize.x;
223 height = eY * theSize.y;
224 if(theSize.y <= theSize.x * 2)
226 // button not high enough
227 // draw just upper and lower part then
228 square = eY * theSize.y * 0.5;
229 bH = eY * (0.25 * theSize.y / (theSize.x * 2));
230 drawsubpic(theOrigin, square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
231 drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
235 square = eY * theSize.x;
236 drawsubpic(theOrigin, width + square, pic, '0 0 0', '1 0.25 0', theColor, theAlpha, drawflag);
237 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5 0', theColor, theAlpha, drawflag);
238 drawsubpic(theOrigin + height - square, width + square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
241 pic = strcat(hud_skin_path, "/", pic);
242 if(precache_pic(pic) == "") {
243 pic = "gfx/hud/default/progressbar";
246 if (baralign == 1) // right align
247 theOrigin.x += (1 - length_ratio) * theSize.x;
248 else if (baralign == 2) // center align
249 theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
250 else if (baralign == 3) // center align, positive values on the right, negative on the left
253 if (length_ratio > 0)
254 theOrigin.x += theSize.x;
257 theOrigin.x += (1 + length_ratio) * theSize.x;
258 length_ratio = -length_ratio;
261 theSize.x *= length_ratio;
264 width = eX * theSize.x;
265 height = eY * theSize.y;
266 if(theSize.x <= theSize.y * 2)
268 // button not wide enough
269 // draw just left and right part then
270 square = eX * theSize.x * 0.5;
271 bW = eX * (0.25 * theSize.x / (theSize.y * 2));
272 drawsubpic(theOrigin, square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
273 drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
277 square = eX * theSize.y;
278 drawsubpic(theOrigin, height + square, pic, '0 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
279 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0.25 0 0', '0.5 1 0', theColor, theAlpha, drawflag);
280 drawsubpic(theOrigin + width - square, height + square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
285 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
291 pos = HUD_Shift(pos);
292 mySize = HUD_Scale(mySize);
295 pic = strcat(hud_skin_path, "/num_leading");
296 if(precache_pic(pic) == "") {
297 pic = "gfx/hud/default/num_leading";
300 drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
301 if(mySize.x/mySize.y > 2)
302 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);
303 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);
306 void DrawNumIcon_expanding(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha, float fadelerp)
308 TC(bool, vertical); TC(int, icon_right_align);
309 vector newPos = '0 0 0', newSize = '0 0 0';
310 vector picpos, numpos;
314 if(mySize.y/mySize.x > 2)
316 newSize.y = 2 * mySize.x;
317 newSize.x = mySize.x;
319 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
324 newSize.x = 1/2 * mySize.y;
325 newSize.y = mySize.y;
327 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
334 picpos = newPos + eY * newSize.x;
339 numpos = newPos + eY * newSize.x;
343 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
344 // make number smaller than icon, it looks better
345 // reduce only y to draw numbers with different number of digits with the same y size
346 numpos.y += newSize.y * ((1 - 0.7) / 2);
348 drawstring_aspect(numpos, ftos(theTime), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
352 if(mySize.x/mySize.y > 3)
354 newSize.x = 3 * mySize.y;
355 newSize.y = mySize.y;
357 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
362 newSize.y = 1/3 * mySize.x;
363 newSize.x = mySize.x;
365 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
369 if(icon_right_align) // right align
372 picpos = newPos + eX * 2 * newSize.y;
376 numpos = newPos + eX * newSize.y;
380 // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
381 // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
382 drawstring_aspect_expanding(numpos, ftos(theTime), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
383 drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
386 void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha)
388 TC(bool, vertical); TC(int, icon_right_align);
389 DrawNumIcon_expanding(myPos, mySize, theTime, icon, vertical, icon_right_align, color, theAlpha, 0);
398 void CSQC_BUMBLE_GUN_HUD();
402 if(autocvar__hud_configure) return;
403 if(intermission == 2) return;
405 if(hud == HUD_BUMBLEBEE_GUN)
406 CSQC_BUMBLE_GUN_HUD();
408 Vehicle info = Vehicles_from(hud);
413 void HUD_Panel_Draw(entity panent)
416 if (autocvar__hud_configure)
418 if (!(panel.panel_configflags & PANEL_CONFIG_MAIN))
420 panel_fade_alpha = 1;
421 Hud_Panel_GetPanelEnabled();
426 bool draw_allowed = false;
427 if (active_minigame && HUD_MinigameMenu_IsOpened())
429 if (panel.panel_showflags & PANEL_SHOW_MINIGAME)
432 else if(intermission == 2)
434 if(panel.panel_showflags & PANEL_SHOW_MAPVOTE)
437 else if (panel.panel_showflags & PANEL_SHOW_MAINGAME)
442 if (panel.panel_showflags & PANEL_SHOW_WITH_SB)
443 panel_fade_alpha = 1;
446 panel_fade_alpha = 1 - scoreboard_fade_alpha;
447 if(!panel_fade_alpha)
456 // reset gametype specific icons
457 if(gametype.m_modicons_reset)
458 gametype.m_modicons_reset();
461 float autocvar_hud_dynamic_shake = 1;
462 float autocvar_hud_dynamic_shake_damage_max = 130;
463 float autocvar_hud_dynamic_shake_damage_min = 10;
464 float autocvar_hud_dynamic_shake_scale = 0.2;
465 float hud_dynamic_shake_x[10] = {0, 1, -0.7, 0.5, -0.3, 0.2, -0.1, 0.1, 0.0, 0};
466 float hud_dynamic_shake_y[10] = {0, 0.4, 0.8, -0.2, -0.6, 0.0, 0.3, 0.1, -0.1, 0};
467 bool Hud_Shake_Update()
469 if(time - hud_dynamic_shake_time < 0)
472 float anim_speed = 17 + 9 * hud_dynamic_shake_factor;
473 float elapsed_time = (time - hud_dynamic_shake_time) * anim_speed;
474 int i = floor(elapsed_time);
478 float f = elapsed_time - i;
479 hud_dynamic_shake_realofs.x = (1 - f) * hud_dynamic_shake_x[i] + f * hud_dynamic_shake_x[i+1];
480 hud_dynamic_shake_realofs.y = (1 - f) * hud_dynamic_shake_y[i] + f * hud_dynamic_shake_y[i+1];
481 hud_dynamic_shake_realofs.z = 0;
482 hud_dynamic_shake_realofs *= hud_dynamic_shake_factor * autocvar_hud_dynamic_shake_scale;
483 hud_dynamic_shake_realofs.x = bound(-0.1, hud_dynamic_shake_realofs.x, 0.1) * vid_conwidth;
484 hud_dynamic_shake_realofs.y = bound(-0.1, hud_dynamic_shake_realofs.y, 0.1) * vid_conheight;
488 entity CSQCModel_server2csqc(int i);
489 void calc_followmodel_ofs(entity view);
490 void Hud_Dynamic_Frame()
492 vector ofs = '0 0 0';
493 hud_scale_current = '1 1 0';
494 hud_shift_current = '0 0 0';
496 if (autocvar_hud_dynamic_follow)
498 entity view = CSQCModel_server2csqc(player_localentnum - 1);
499 calc_followmodel_ofs(view);
500 ofs = -cl_followmodel_ofs * autocvar_hud_dynamic_follow_scale;
501 ofs.x *= autocvar_hud_dynamic_follow_scale_xyz.z;
502 ofs.y *= autocvar_hud_dynamic_follow_scale_xyz.x;
503 ofs.z *= autocvar_hud_dynamic_follow_scale_xyz.y;
505 if (fabs(ofs.x) < 0.001) ofs.x = 0;
506 if (fabs(ofs.y) < 0.001) ofs.y = 0;
507 if (fabs(ofs.z) < 0.001) ofs.z = 0;
508 ofs.x = bound(-0.1, ofs.x, 0.1);
509 ofs.y = bound(-0.1, ofs.y, 0.1);
510 ofs.z = bound(-0.1, ofs.z, 0.1);
512 hud_shift_current.x = ofs.y * vid_conwidth;
513 hud_shift_current.y = ofs.z * vid_conheight;
514 hud_shift_current.z = ofs.x;
516 hud_scale_current.x = (1 + hud_shift_current.z);
517 hud_scale_current.y = hud_scale_current.x;
520 if(autocvar_hud_dynamic_shake > 0)
522 static float old_health = 0;
523 float health = max(-1, STAT(HEALTH));
524 if(hud_dynamic_shake_factor == -1) // don't allow the effect for this frame
526 hud_dynamic_shake_factor = 0;
531 float new_hud_dynamic_shake_factor = 0;
532 if (old_health - health >= autocvar_hud_dynamic_shake_damage_min
533 && autocvar_hud_dynamic_shake_damage_max > autocvar_hud_dynamic_shake_damage_min
534 && old_health > 0 && !intermission)
536 float m = max(autocvar_hud_dynamic_shake_damage_min, 1);
537 new_hud_dynamic_shake_factor = (old_health - health - m) / (autocvar_hud_dynamic_shake_damage_max - m);
538 if(new_hud_dynamic_shake_factor >= 1)
539 new_hud_dynamic_shake_factor = 1;
540 if(new_hud_dynamic_shake_factor >= hud_dynamic_shake_factor)
542 hud_dynamic_shake_factor = new_hud_dynamic_shake_factor;
543 hud_dynamic_shake_time = time;
547 if(hud_dynamic_shake_factor)
548 if(!Hud_Shake_Update())
549 hud_dynamic_shake_factor = 0;
552 if(hud_dynamic_shake_factor > 0)
554 hud_shift_current.x += hud_dynamic_shake_realofs.x;
555 hud_shift_current.y += hud_dynamic_shake_realofs.y;
559 hud_scale_center.x = 0.5 * vid_conwidth;
560 hud_scale_center.y = 0.5 * vid_conheight;
568 if(hud_configure_menu_open == 1)
571 hud_fade_alpha = 1 - autocvar__menu_alpha;
573 HUD_Configure_Frame();
575 if(scoreboard_fade_alpha == 1)
576 if(autocvar__menu_alpha == 1)
580 if (hud_skin_prev != autocvar_hud_skin)
583 strunzone(hud_skin_path);
584 hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
586 strunzone(hud_skin_prev);
587 hud_skin_prev = strzone(autocvar_hud_skin);
591 if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
595 float hud_dock_color_team = autocvar_hud_dock_color_team;
596 if((teamplay) && hud_dock_color_team) {
597 if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
598 color = '1 0 0' * hud_dock_color_team;
600 color = myteamcolors * hud_dock_color_team;
602 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
603 color = '1 0 0' * hud_dock_color_team;
607 string hud_dock_color = autocvar_hud_dock_color;
608 if(hud_dock_color == "shirt") {
609 f = entcs_GetClientColors(current_player);
610 color = colormapPaletteColor(floor(f / 16), 0);
612 else if(hud_dock_color == "pants") {
613 f = entcs_GetClientColors(current_player);
614 color = colormapPaletteColor(f % 16, 1);
617 color = stov(hud_dock_color);
621 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
622 if(precache_pic(pic) == "") {
623 pic = strcat(hud_skin_path, "/dock_medium");
624 if(precache_pic(pic) == "") {
625 pic = "gfx/hud/default/dock_medium";
628 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...
631 // cache the panel order into the panel_order array
632 if(autocvar__hud_panelorder != hud_panelorder_prev) {
633 for(i = 0; i < hud_panels_COUNT; ++i)
637 bool warning = false;
638 int argc = tokenize_console(autocvar__hud_panelorder);
639 if (argc > hud_panels_COUNT)
641 //first detect wrong/missing panel numbers
642 for(i = 0; i < hud_panels_COUNT; ++i) {
643 p_num = stoi(argv(i));
644 if (p_num >= 0 && p_num < hud_panels_COUNT) { //correct panel number?
645 if (panel_order[p_num] == -1) //found for the first time?
646 s = strcat(s, ftos(p_num), " ");
647 panel_order[p_num] = 1; //mark as found
652 for(i = 0; i < hud_panels_COUNT; ++i) {
653 if (panel_order[i] == -1) {
655 s = strcat(s, ftos(i), " "); //add missing panel number
659 LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder");
661 cvar_set("_hud_panelorder", s);
662 if(hud_panelorder_prev)
663 strunzone(hud_panelorder_prev);
664 hud_panelorder_prev = strzone(s);
666 //now properly set panel_order
668 for(i = 0; i < hud_panels_COUNT; ++i) {
669 panel_order[i] = stof(argv(i));
673 hud_draw_maximized = 0;
674 // draw panels in the order specified by panel_order array
675 for(i = hud_panels_COUNT - 1; i >= 0; --i)
676 HUD_Panel_Draw(hud_panels_from(panel_order[i]));
680 hud_draw_maximized = 1; // panels that may be maximized must check this var
681 // draw maximized panels on top
682 if(hud_panel_radar_maximized)
683 HUD_Panel_Draw(HUD_PANEL(RADAR));
684 if(autocvar__con_chat_maximized)
685 HUD_Panel_Draw(HUD_PANEL(CHAT));
686 if(hud_panel_quickmenu)
687 HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
688 HUD_Panel_Draw(HUD_PANEL(SCOREBOARD));
690 if (intermission == 2)
693 HUD_Configure_PostDraw();
695 hud_configure_prev = autocvar__hud_configure;