3 #include <client/defs.qh>
4 #include <client/miscfunctions.qh>
5 #include <client/view.qh>
6 #include "panel/scoreboard.qh"
7 #include "hud_config.qh"
8 #include "../mapvoting.qh"
9 #include "../teamradar.qh"
10 #include <common/minigames/cl_minigames.qh>
11 #include <common/t_items.qh>
12 #include <common/deathtypes/all.qh>
13 #include <common/ent_cs.qh>
14 #include <common/items/_mod.qh>
15 #include <common/mapinfo.qh>
16 #include <common/vehicles/all.qh>
17 #include <common/vehicles/vehicle/bumblebee.qh>
18 #include <common/mutators/mutator/waypoints/all.qh>
19 #include <common/stats.qh>
20 #include <lib/csqcmodel/cl_player.qh>
21 #include <lib/csqcmodel/cl_model.qh>
22 #include <common/gamemodes/_mod.qh>
31 vector HUD_Get_Num_Color (float hp, float maxvalue)
36 color.x = sin(2*M_PI*time);
38 color.z = sin(2*M_PI*time);
40 else if(hp > maxvalue * 0.75) {
41 color.x = 0.4 - (hp-150)*0.02 * 0.4; //red value between 0.4 -> 0
42 color.y = 0.9 + (hp-150)*0.02 * 0.1; // green value between 0.9 -> 1
45 else if(hp > maxvalue * 0.5) {
46 color.x = 1 - (hp-100)*0.02 * 0.6; //red value between 1 -> 0.4
47 color.y = 1 - (hp-100)*0.02 * 0.1; // green value between 1 -> 0.9
48 color.z = 1 - (hp-100)*0.02; // blue value between 1 -> 0
50 else if(hp > maxvalue * 0.25) {
53 color.z = 0.2 + (hp-50)*0.02 * 0.8; // blue value between 0.2 -> 1
55 else if(hp > maxvalue * 0.1) {
57 color.y = (hp-20)*90/27/100; // green value between 0 -> 1
58 color.z = (hp-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
66 blinkingamt = (1 - hp/maxvalue/0.25);
69 color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
70 color.y = color.y - color.y * blinkingamt * sin(2*M_PI*time);
71 color.z = color.z - color.z * blinkingamt * sin(2*M_PI*time);
76 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
79 float aspect = size_y / size_x;
80 return bound(1, floor((sqrt(4 * item_aspect * aspect * item_count + aspect * aspect) + aspect + 0.5) / 2), item_count);
83 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
87 float ratio, best_ratio = 0;
88 float best_columns = 1, best_rows = 1;
89 bool vertical = (psize.x / psize.y >= item_aspect);
92 psize = eX * psize.y + eY * psize.x;
93 item_aspect = 1 / item_aspect;
96 rows = ceil(sqrt(item_count));
97 columns = ceil(item_count/rows);
100 ratio = (psize.x/columns) / (psize.y/rows);
101 if(ratio > item_aspect)
102 ratio = item_aspect * item_aspect / ratio;
104 if(ratio <= best_ratio)
105 break; // ratio starts decreasing by now, skip next configurations
107 best_columns = columns;
115 rows = ceil(item_count/columns);
118 return (vertical) ? vec2(best_rows, best_columns) : vec2(best_columns, best_rows);
127 void HUD_Panel_LoadCvars()
129 // NOTE: in hud_configure mode cvars must be reloaded every frame
130 if (panel.update_time <= time)
132 panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));
133 panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));
134 HUD_Panel_ScalePosSize();
135 panel_bg_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg"));
136 panel_bg_color_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color"));
137 panel_bg_color_team_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color_team"));
138 panel_bg_alpha_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_alpha"));
139 panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));
140 panel_bg_padding_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_padding"));
142 if (panel.current_panel_bg != "0")
144 HUD_Panel_GetBgAlpha();
145 HUD_Panel_GetBorder();
147 HUD_Panel_GetColorTeam();
148 HUD_Panel_GetColor();
149 HUD_Panel_GetFgAlpha();
150 HUD_Panel_GetPadding();
151 panel.current_panel_bg_alpha = panel_bg_alpha;
152 panel.current_panel_fg_alpha = panel_fg_alpha;
153 if (hud_configure_menu_open == 2 && panel == highlightedPanel)
154 HUD_Panel_UpdatePosSize_ForMenu();
157 panel_bg_alpha *= hud_fade_alpha * panel_fade_alpha;
158 panel_fg_alpha *= hud_fade_alpha * panel_fade_alpha;
160 panel.current_panel_pos = panel_pos;
161 panel.current_panel_size = panel_size;
162 panel.current_panel_bg_border = panel_bg_border;
163 panel.current_panel_bg_color = panel_bg_color;
164 panel.current_panel_bg_color_team = panel_bg_color_team;
165 panel.current_panel_bg_padding = panel_bg_padding;
166 panel.update_time = (autocvar__hud_configure) ? time : time + autocvar_hud_panel_update_interval;
170 panel_pos = panel.current_panel_pos;
171 panel_size = panel.current_panel_size;
172 panel_bg_alpha = panel.current_panel_bg_alpha * hud_fade_alpha * panel_fade_alpha;
173 panel_bg_border = panel.current_panel_bg_border;
174 panel_bg_color = panel.current_panel_bg_color;
175 panel_bg_color_team = panel.current_panel_bg_color_team;
176 panel_bg_padding = panel.current_panel_bg_padding;
177 panel_fg_alpha = panel.current_panel_fg_alpha * hud_fade_alpha * panel_fade_alpha;
180 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
181 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
183 TC(bool, vertical); TC(int, drawflag);
184 if(!length_ratio || !theAlpha)
190 if(length_ratio < -1)
193 else if(length_ratio < 0)
196 theOrigin = HUD_Shift(theOrigin);
197 theSize = HUD_Scale(theSize);
200 vector width, height;
202 pic = strcat(hud_skin_path, "/", pic, "_vertical");
203 if(precache_pic(pic) == "") {
204 pic = "gfx/hud/default/progressbar_vertical";
207 if (baralign == 1) // bottom align
208 theOrigin.y += (1 - length_ratio) * theSize.y;
209 else if (baralign == 2) // center align
210 theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
211 else if (baralign == 3) // center align, positive values down, negative up
214 if (length_ratio > 0)
215 theOrigin.y += theSize.y;
218 theOrigin.y += (1 + length_ratio) * theSize.y;
219 length_ratio = -length_ratio;
222 theSize.y *= length_ratio;
225 width = eX * theSize.x;
226 height = eY * theSize.y;
227 if(theSize.y <= theSize.x * 2)
229 // button not high enough
230 // draw just upper and lower part then
231 square = eY * theSize.y * 0.5;
232 bH = eY * (0.25 * theSize.y / (theSize.x * 2));
233 drawsubpic(theOrigin, square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
234 drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
238 square = eY * theSize.x;
239 drawsubpic(theOrigin, width + square, pic, '0 0 0', '1 0.25 0', theColor, theAlpha, drawflag);
240 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5 0', theColor, theAlpha, drawflag);
241 drawsubpic(theOrigin + height - square, width + square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
244 pic = strcat(hud_skin_path, "/", pic);
245 if(precache_pic(pic) == "") {
246 pic = "gfx/hud/default/progressbar";
249 if (baralign == 1) // right align
250 theOrigin.x += (1 - length_ratio) * theSize.x;
251 else if (baralign == 2) // center align
252 theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
253 else if (baralign == 3) // center align, positive values on the right, negative on the left
256 if (length_ratio > 0)
257 theOrigin.x += theSize.x;
260 theOrigin.x += (1 + length_ratio) * theSize.x;
261 length_ratio = -length_ratio;
264 theSize.x *= length_ratio;
267 width = eX * theSize.x;
268 height = eY * theSize.y;
269 if(theSize.x <= theSize.y * 2)
271 // button not wide enough
272 // draw just left and right part then
273 square = eX * theSize.x * 0.5;
274 bW = eX * (0.25 * theSize.x / (theSize.y * 2));
275 drawsubpic(theOrigin, square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
276 drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
280 square = eX * theSize.y;
281 drawsubpic(theOrigin, height + square, pic, '0 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
282 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0.25 0 0', '0.5 1 0', theColor, theAlpha, drawflag);
283 drawsubpic(theOrigin + width - square, height + square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
288 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
294 pos = HUD_Shift(pos);
295 mySize = HUD_Scale(mySize);
298 pic = strcat(hud_skin_path, "/num_leading");
299 if(precache_pic(pic) == "") {
300 pic = "gfx/hud/default/num_leading";
303 drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
304 if(mySize.x/mySize.y > 2)
305 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);
306 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);
309 void DrawNumIcon_expanding(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha, float fadelerp)
311 TC(bool, vertical); TC(int, icon_right_align);
312 vector newPos = '0 0 0', newSize = '0 0 0';
313 vector picpos, numpos;
317 if(mySize.y/mySize.x > 2)
319 newSize.y = 2 * mySize.x;
320 newSize.x = mySize.x;
322 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
327 newSize.x = 1/2 * mySize.y;
328 newSize.y = mySize.y;
330 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
337 picpos = newPos + eY * newSize.x;
342 numpos = newPos + eY * newSize.x;
346 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
347 // make number smaller than icon, it looks better
348 // reduce only y to draw numbers with different number of digits with the same y size
349 numpos.y += newSize.y * ((1 - 0.7) / 2);
351 drawstring_aspect(numpos, ftos(theTime), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
355 if(mySize.x/mySize.y > 3)
357 newSize.x = 3 * mySize.y;
358 newSize.y = mySize.y;
360 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
365 newSize.y = 1/3 * mySize.x;
366 newSize.x = mySize.x;
368 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
372 if(icon_right_align) // right align
375 picpos = newPos + eX * 2 * newSize.y;
379 numpos = newPos + eX * newSize.y;
383 // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
384 // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
385 drawstring_aspect_expanding(numpos, ftos(theTime), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
386 drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
389 void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha)
391 TC(bool, vertical); TC(int, icon_right_align);
392 DrawNumIcon_expanding(myPos, mySize, theTime, icon, vertical, icon_right_align, color, theAlpha, 0);
403 if(autocvar__hud_configure) return;
404 if(intermission == 2) return;
406 if(hud == HUD_BUMBLEBEE_GUN)
407 CSQC_BUMBLE_GUN_HUD();
409 Vehicle info = Vehicles_from(hud);
414 void HUD_Panel_Draw(entity panent)
417 if (autocvar__hud_configure)
419 if (!(panel.panel_configflags & PANEL_CONFIG_MAIN))
421 panel_fade_alpha = 1;
422 Hud_Panel_GetPanelEnabled();
427 bool draw_allowed = false;
428 if (active_minigame && HUD_MinigameMenu_IsOpened())
430 if (panel.panel_showflags & PANEL_SHOW_MINIGAME)
433 else if(intermission == 2)
435 if(panel.panel_showflags & PANEL_SHOW_MAPVOTE)
438 else if (panel.panel_showflags & PANEL_SHOW_MAINGAME)
443 if (panel.panel_showflags & PANEL_SHOW_WITH_SB)
444 panel_fade_alpha = 1;
447 panel_fade_alpha = 1 - scoreboard_fade_alpha;
448 if(!panel_fade_alpha)
457 // reset gametype specific icons
458 if(gametype.m_modicons_reset)
459 gametype.m_modicons_reset();
462 float autocvar_hud_dynamic_shake = 1;
463 float autocvar_hud_dynamic_shake_damage_max = 130;
464 float autocvar_hud_dynamic_shake_damage_min = 10;
465 float autocvar_hud_dynamic_shake_scale = 0.2;
466 float hud_dynamic_shake_x[10] = {0, 1, -0.7, 0.5, -0.3, 0.2, -0.1, 0.1, 0.0, 0};
467 float hud_dynamic_shake_y[10] = {0, 0.4, 0.8, -0.2, -0.6, 0.0, 0.3, 0.1, -0.1, 0};
468 bool Hud_Shake_Update()
470 if(time - hud_dynamic_shake_time < 0)
473 float anim_speed = 17 + 9 * hud_dynamic_shake_factor;
474 float elapsed_time = (time - hud_dynamic_shake_time) * anim_speed;
475 int i = floor(elapsed_time);
479 float f = elapsed_time - i;
480 hud_dynamic_shake_realofs.x = (1 - f) * hud_dynamic_shake_x[i] + f * hud_dynamic_shake_x[i+1];
481 hud_dynamic_shake_realofs.y = (1 - f) * hud_dynamic_shake_y[i] + f * hud_dynamic_shake_y[i+1];
482 hud_dynamic_shake_realofs.z = 0;
483 hud_dynamic_shake_realofs *= hud_dynamic_shake_factor * autocvar_hud_dynamic_shake_scale;
484 hud_dynamic_shake_realofs.x = bound(-0.1, hud_dynamic_shake_realofs.x, 0.1) * vid_conwidth;
485 hud_dynamic_shake_realofs.y = bound(-0.1, hud_dynamic_shake_realofs.y, 0.1) * vid_conheight;
489 void Hud_Dynamic_Frame()
491 vector ofs = '0 0 0';
492 hud_scale_current = '1 1 0';
493 hud_shift_current = '0 0 0';
495 if (autocvar_hud_dynamic_follow)
497 entity view = CSQCModel_server2csqc(player_localentnum - 1);
498 calc_followmodel_ofs(view);
499 ofs = -cl_followmodel_ofs * autocvar_hud_dynamic_follow_scale;
500 ofs.x *= autocvar_hud_dynamic_follow_scale_xyz.z;
501 ofs.y *= autocvar_hud_dynamic_follow_scale_xyz.x;
502 ofs.z *= autocvar_hud_dynamic_follow_scale_xyz.y;
504 if (fabs(ofs.x) < 0.001) ofs.x = 0;
505 if (fabs(ofs.y) < 0.001) ofs.y = 0;
506 if (fabs(ofs.z) < 0.001) ofs.z = 0;
507 ofs.x = bound(-0.1, ofs.x, 0.1);
508 ofs.y = bound(-0.1, ofs.y, 0.1);
509 ofs.z = bound(-0.1, ofs.z, 0.1);
511 hud_shift_current.x = ofs.y * vid_conwidth;
512 hud_shift_current.y = ofs.z * vid_conheight;
513 hud_shift_current.z = ofs.x;
515 hud_scale_current.x = (1 + hud_shift_current.z);
516 hud_scale_current.y = hud_scale_current.x;
519 if(autocvar_hud_dynamic_shake > 0)
521 static float old_health = 0;
522 float health = max(-1, STAT(HEALTH));
523 if(hud_dynamic_shake_factor == -1) // don't allow the effect for this frame
525 hud_dynamic_shake_factor = 0;
530 float new_hud_dynamic_shake_factor = 0;
531 if (old_health - health >= autocvar_hud_dynamic_shake_damage_min
532 && autocvar_hud_dynamic_shake_damage_max > autocvar_hud_dynamic_shake_damage_min
533 && old_health > 0 && !intermission)
535 float m = max(autocvar_hud_dynamic_shake_damage_min, 1);
536 new_hud_dynamic_shake_factor = (old_health - health - m) / (autocvar_hud_dynamic_shake_damage_max - m);
537 if(new_hud_dynamic_shake_factor >= 1)
538 new_hud_dynamic_shake_factor = 1;
539 if(new_hud_dynamic_shake_factor >= hud_dynamic_shake_factor)
541 hud_dynamic_shake_factor = new_hud_dynamic_shake_factor;
542 hud_dynamic_shake_time = time;
546 if(hud_dynamic_shake_factor)
547 if(!Hud_Shake_Update())
548 hud_dynamic_shake_factor = 0;
551 if(hud_dynamic_shake_factor > 0)
553 hud_shift_current.x += hud_dynamic_shake_realofs.x;
554 hud_shift_current.y += hud_dynamic_shake_realofs.y;
558 hud_scale_center.x = 0.5 * vid_conwidth;
559 hud_scale_center.y = 0.5 * vid_conheight;
564 bool HUD_WouldShowCursor()
566 if(autocvar__hud_configure)
570 //entity local_player = ((csqcplayer) ? csqcplayer : CSQCModel_server2csqc(player_localentnum - 1)); // TODO: doesn't use regular cursor handling
571 //if(local_player.viewloc && (local_player.viewloc.spawnflags & VIEWLOC_FREEAIM))
573 if(HUD_Radar_Clickable())
575 if(HUD_MinigameMenu_IsOpened())
577 if(QuickMenu_IsOpened())
585 if(hud_configure_menu_open == 1)
588 hud_fade_alpha = 1 - autocvar__menu_alpha;
590 HUD_Configure_Frame();
592 if(scoreboard_fade_alpha == 1)
593 if(autocvar__menu_alpha == 1)
597 if (hud_skin_prev != autocvar_hud_skin)
599 strcpy(hud_skin_path, strcat("gfx/hud/", autocvar_hud_skin));
600 strcpy(hud_skin_prev, autocvar_hud_skin);
604 if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
608 float hud_dock_color_team = autocvar_hud_dock_color_team;
609 if((teamplay) && hud_dock_color_team) {
610 if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
611 color = '1 0 0' * hud_dock_color_team;
613 color = myteamcolors * hud_dock_color_team;
615 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
616 color = '1 0 0' * hud_dock_color_team;
620 string hud_dock_color = autocvar_hud_dock_color;
621 if(hud_dock_color == "shirt") {
622 f = entcs_GetClientColors(current_player);
623 color = colormapPaletteColor(floor(f / 16), 0);
625 else if(hud_dock_color == "pants") {
626 f = entcs_GetClientColors(current_player);
627 color = colormapPaletteColor(f % 16, 1);
630 color = stov(hud_dock_color);
634 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
635 if(precache_pic(pic) == "") {
636 pic = strcat(hud_skin_path, "/dock_medium");
637 if(precache_pic(pic) == "") {
638 pic = "gfx/hud/default/dock_medium";
641 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...
644 // cache the panel order into the panel_order array
645 if(autocvar__hud_panelorder != hud_panelorder_prev) {
646 for(i = 0; i < hud_panels_COUNT; ++i)
650 bool warning = false;
651 int argc = tokenize_console(autocvar__hud_panelorder);
652 if (argc > hud_panels_COUNT)
654 //first detect wrong/missing panel numbers
655 for(i = 0; i < hud_panels_COUNT; ++i) {
656 p_num = stoi(argv(i));
657 if (p_num >= 0 && p_num < hud_panels_COUNT) { //correct panel number?
658 if (panel_order[p_num] == -1) //found for the first time?
659 s = strcat(s, ftos(p_num), " ");
660 panel_order[p_num] = 1; //mark as found
665 for(i = 0; i < hud_panels_COUNT; ++i) {
666 if (panel_order[i] == -1) {
668 s = strcat(s, ftos(i), " "); //add missing panel number
672 LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder");
674 cvar_set("_hud_panelorder", s);
675 strcpy(hud_panelorder_prev, s);
677 //now properly set panel_order
679 for(i = 0; i < hud_panels_COUNT; ++i) {
680 panel_order[i] = stof(argv(i));
684 hud_draw_maximized = 0;
685 // draw panels in the order specified by panel_order array
686 for(i = hud_panels_COUNT - 1; i >= 0; --i)
687 HUD_Panel_Draw(hud_panels_from(panel_order[i]));
691 hud_draw_maximized = 1; // panels that may be maximized must check this var
692 // draw maximized panels on top
693 if(hud_panel_radar_maximized)
694 HUD_Panel_Draw(HUD_PANEL(RADAR));
695 if(autocvar__con_chat_maximized)
696 HUD_Panel_Draw(HUD_PANEL(CHAT));
697 if (QuickMenu_IsOpened())
698 HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
699 HUD_Panel_Draw(HUD_PANEL(SCOREBOARD));
701 int cursor_active_prev = cursor_active;
702 cursor_active = HUD_WouldShowCursor();
703 if (cursor_active_prev != cursor_active && autocvar_hud_cursormode)
705 setcursormode(cursor_active);
706 // cursor inactive this frame, will be set to 1 the next frame
711 if (intermission == 2)
714 HUD_Configure_PostDraw();
716 hud_configure_prev = autocvar__hud_configure;