]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/client/hud/hud.qc
Registry API: add REGISTRY_GET
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / hud.qc
index 9210b86634ca1e551fb93f0e17f990570cfdf952..91770fd724ecc9b3b477ce5d279a88077e0448d0 100644 (file)
@@ -1,17 +1,25 @@
 #include "hud.qh"
 
+#include <client/defs.qh>
+#include <client/miscfunctions.qh>
+#include <client/view.qh>
+#include "panel/scoreboard.qh"
 #include "hud_config.qh"
 #include "../mapvoting.qh"
 #include "../teamradar.qh"
+#include <common/minigames/cl_minigames.qh>
 #include <common/t_items.qh>
 #include <common/deathtypes/all.qh>
-#include <common/items/all.qc>
+#include <common/ent_cs.qh>
+#include <common/items/_mod.qh>
 #include <common/mapinfo.qh>
 #include <common/vehicles/all.qh>
+#include <common/vehicles/vehicle/bumblebee.qh>
 #include <common/mutators/mutator/waypoints/all.qh>
 #include <common/stats.qh>
 #include <lib/csqcmodel/cl_player.qh>
-#include <server/mutators/mutator/gamemode_ctf.qh> // TODO: remove
+#include <lib/csqcmodel/cl_model.qh>
+#include <common/gamemodes/_mod.qh>
 
 
 /*
@@ -20,34 +28,34 @@ Misc HUD functions
 ==================
 */
 
-vector HUD_Get_Num_Color (float x, float maxvalue)
+vector HUD_Get_Num_Color (float hp, float maxvalue)
 {
        float blinkingamt;
        vector color;
-       if(x >= maxvalue) {
+       if(hp >= maxvalue) {
                color.x = sin(2*M_PI*time);
                color.y = 1;
                color.z = sin(2*M_PI*time);
        }
-       else if(x > maxvalue * 0.75) {
-               color.x = 0.4 - (x-150)*0.02 * 0.4; //red value between 0.4 -> 0
-               color.y = 0.9 + (x-150)*0.02 * 0.1; // green value between 0.9 -> 1
+       else if(hp > maxvalue * 0.75) {
+               color.x = 0.4 - (hp-150)*0.02 * 0.4; //red value between 0.4 -> 0
+               color.y = 0.9 + (hp-150)*0.02 * 0.1; // green value between 0.9 -> 1
                color.z = 0;
        }
-       else if(x > maxvalue * 0.5) {
-               color.x = 1 - (x-100)*0.02 * 0.6; //red value between 1 -> 0.4
-               color.y = 1 - (x-100)*0.02 * 0.1; // green value between 1 -> 0.9
-               color.z = 1 - (x-100)*0.02; // blue value between 1 -> 0
+       else if(hp > maxvalue * 0.5) {
+               color.x = 1 - (hp-100)*0.02 * 0.6; //red value between 1 -> 0.4
+               color.y = 1 - (hp-100)*0.02 * 0.1; // green value between 1 -> 0.9
+               color.z = 1 - (hp-100)*0.02; // blue value between 1 -> 0
        }
-       else if(x > maxvalue * 0.25) {
+       else if(hp > maxvalue * 0.25) {
                color.x = 1;
                color.y = 1;
-               color.z = 0.2 + (x-50)*0.02 * 0.8; // blue value between 0.2 -> 1
+               color.z = 0.2 + (hp-50)*0.02 * 0.8; // blue value between 0.2 -> 1
        }
-       else if(x > maxvalue * 0.1) {
+       else if(hp > maxvalue * 0.1) {
                color.x = 1;
-               color.y = (x-20)*90/27/100; // green value between 0 -> 1
-               color.z = (x-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
+               color.y = (hp-20)*90/27/100; // green value between 0 -> 1
+               color.z = (hp-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
        }
        else {
                color.x = 1;
@@ -55,7 +63,7 @@ vector HUD_Get_Num_Color (float x, float maxvalue)
                color.z = 0;
        }
 
-       blinkingamt = (1 - x/maxvalue/0.25);
+       blinkingamt = (1 - hp/maxvalue/0.25);
        if(blinkingamt > 0)
        {
                color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
@@ -67,14 +75,14 @@ vector HUD_Get_Num_Color (float x, float maxvalue)
 
 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
 {
-    TC(int, item_count);
+       TC(int, item_count);
        float aspect = size_y / size_x;
        return bound(1, floor((sqrt(4 * item_aspect * aspect * item_count + aspect * aspect) + aspect + 0.5) / 2), item_count);
 }
 
 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
 {
-    TC(int, item_count);
+       TC(int, item_count);
        float columns, rows;
        float ratio, best_ratio = 0;
        float best_columns = 1, best_rows = 1;
@@ -107,10 +115,7 @@ vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspe
                rows = ceil(item_count/columns);
        }
 
-       if(vertical)
-               return eX * best_rows + eY * best_columns;
-       else
-               return eX * best_columns + eY * best_rows;
+       return (vertical) ? vec2(best_rows, best_columns) : vec2(best_columns, best_rows);
 }
 
 /*
@@ -119,10 +124,63 @@ HUD panels
 ==================
 */
 
+void HUD_Panel_LoadCvars()
+{
+       // NOTE: in hud_configure mode cvars must be reloaded every frame
+       if (panel.update_time <= time)
+       {
+               panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));
+               panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));
+               HUD_Panel_ScalePosSize();
+               panel_bg_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg"));
+               panel_bg_color_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color"));
+               panel_bg_color_team_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color_team"));
+               panel_bg_alpha_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_alpha"));
+               panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));
+               panel_bg_padding_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_padding"));
+               HUD_Panel_GetBg();
+               if (panel.current_panel_bg != "0")
+               {
+                       HUD_Panel_GetBgAlpha();
+                       HUD_Panel_GetBorder();
+               }
+               HUD_Panel_GetColorTeam();
+               HUD_Panel_GetColor();
+               HUD_Panel_GetFgAlpha();
+               HUD_Panel_GetPadding();
+               panel.current_panel_bg_alpha = panel_bg_alpha;
+               panel.current_panel_fg_alpha = panel_fg_alpha;
+               if (hud_configure_menu_open == 2 && panel == highlightedPanel)
+                       HUD_Panel_UpdatePosSize_ForMenu();
+               else
+               {
+                       panel_bg_alpha *= hud_fade_alpha * panel_fade_alpha;
+                       panel_fg_alpha *= hud_fade_alpha * panel_fade_alpha;
+               }
+               panel.current_panel_pos = panel_pos;
+               panel.current_panel_size = panel_size;
+               panel.current_panel_bg_border = panel_bg_border;
+               panel.current_panel_bg_color = panel_bg_color;
+               panel.current_panel_bg_color_team = panel_bg_color_team;
+               panel.current_panel_bg_padding = panel_bg_padding;
+               panel.update_time = (autocvar__hud_configure) ? time : time + autocvar_hud_panel_update_interval;
+               return;
+       }
+
+       panel_pos = panel.current_panel_pos;
+       panel_size = panel.current_panel_size;
+       panel_bg_alpha = panel.current_panel_bg_alpha * hud_fade_alpha * panel_fade_alpha;
+       panel_bg_border = panel.current_panel_bg_border;
+       panel_bg_color = panel.current_panel_bg_color;
+       panel_bg_color_team = panel.current_panel_bg_color_team;
+       panel_bg_padding = panel.current_panel_bg_padding;
+       panel_fg_alpha = panel.current_panel_fg_alpha * hud_fade_alpha * panel_fade_alpha;
+}
+
 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
 {
-    TC(bool, vertical); TC(int, drawflag);
+       TC(bool, vertical); TC(int, drawflag);
        if(!length_ratio || !theAlpha)
                return;
        if(length_ratio > 1)
@@ -229,7 +287,7 @@ void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, flo
 
 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
 {
-    TC(int, drawflag);
+       TC(int, drawflag);
        if(!theAlpha)
                return;
 
@@ -248,9 +306,9 @@ void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theA
        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);
 }
 
-void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, bool vertical, int icon_right_align, vector color, float theAlpha, float fadelerp)
+void DrawNumIcon_expanding(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha, float fadelerp)
 {
-    TC(bool, vertical); TC(int, icon_right_align);
+       TC(bool, vertical); TC(int, icon_right_align);
        vector newPos = '0 0 0', newSize = '0 0 0';
        vector picpos, numpos;
 
@@ -290,7 +348,7 @@ void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, bo
                // reduce only y to draw numbers with different number of digits with the same y size
                numpos.y += newSize.y * ((1 - 0.7) / 2);
                newSize.y *= 0.7;
-               drawstring_aspect(numpos, ftos(x), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
+               drawstring_aspect(numpos, ftos(theTime), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
                return;
        }
 
@@ -324,26 +382,24 @@ void DrawNumIcon_expanding(vector myPos, vector mySize, float x, string icon, bo
 
        // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
        // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
-       drawstring_aspect_expanding(numpos, ftos(x), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
+       drawstring_aspect_expanding(numpos, ftos(theTime), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
        drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
 }
 
-void DrawNumIcon(vector myPos, vector mySize, float x, string icon, bool vertical, int icon_right_align, vector color, float theAlpha)
+void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha)
 {
-    TC(bool, vertical); TC(int, icon_right_align);
-       DrawNumIcon_expanding(myPos, mySize, x, icon, vertical, icon_right_align, color, theAlpha, 0);
+       TC(bool, vertical); TC(int, icon_right_align);
+       DrawNumIcon_expanding(myPos, mySize, theTime, icon, vertical, icon_right_align, color, theAlpha, 0);
 }
 
-#include "all.inc"
-
 /*
 ==================
 Main HUD system
 ==================
 */
 
-void CSQC_BUMBLE_GUN_HUD();
-
+float lasthud;
+float vh_notice_time;
 void HUD_Vehicle()
 {
        if(autocvar__hud_configure) return;
@@ -352,31 +408,64 @@ void HUD_Vehicle()
        if(hud == HUD_BUMBLEBEE_GUN)
                CSQC_BUMBLE_GUN_HUD();
        else {
-               Vehicle info = Vehicles_from(hud);
+               Vehicle info = REGISTRY_GET(Vehicles, hud);
                info.vr_hud(info);
        }
-}
 
-bool HUD_Panel_CheckFlags(int showflags)
-{
-    TC(int, showflags);
-       if ( HUD_Minigame_Showpanels() )
-               return showflags & PANEL_SHOW_MINIGAME;
-       if(intermission == 2)
-               return showflags & PANEL_SHOW_MAPVOTE;
-       return showflags & PANEL_SHOW_MAINGAME;
+       if(hud != HUD_NORMAL && lasthud == HUD_NORMAL)
+               vh_notice_time = time + autocvar_cl_vehicles_notify_time;
+
+       lasthud = hud;
 }
 
 void HUD_Panel_Draw(entity panent)
 {
        panel = panent;
-       if(autocvar__hud_configure)
+       if (autocvar__hud_configure)
+       {
+               if (!(panel.panel_configflags & PANEL_CONFIG_MAIN))
+                       return;
+               panel_fade_alpha = 1;
+               Hud_Panel_GetPanelEnabled();
+               panel.panel_draw();
+               return;
+       }
+
+       bool draw_allowed = false;
+       if (scoreboard_fade_alpha && panel.panel_showflags & PANEL_SHOW_WITH_SB)
        {
-               if(panel.panel_configflags & PANEL_CONFIG_MAIN)
-                       panel.panel_draw();
+               draw_allowed = true;
        }
-       else if(HUD_Panel_CheckFlags(panel.panel_showflags))
+       else if (active_minigame && HUD_MinigameMenu_IsOpened())
+       {
+               if (panel.panel_showflags & PANEL_SHOW_MINIGAME)
+                       draw_allowed = true;
+       }
+       else if(intermission == 2)
+       {
+               if(panel.panel_showflags & PANEL_SHOW_MAPVOTE)
+                       draw_allowed = true;
+       }
+       else if (panel.panel_showflags & PANEL_SHOW_MAINGAME)
+               draw_allowed = true;
+
+       if (draw_allowed)
+       {
+               if (panel.panel_showflags & PANEL_SHOW_WITH_SB)
+               {
+                       if (scoreboard_fade_alpha && intermission == 2 && !(panel.panel_showflags & PANEL_SHOW_MAPVOTE))
+                               panel_fade_alpha = scoreboard_fade_alpha;
+                       else
+                               panel_fade_alpha = 1;
+               }
+               else
+               {
+                       panel_fade_alpha = 1 - scoreboard_fade_alpha;
+                       if(!panel_fade_alpha)
+                               return;
+               }
                panel.panel_draw();
+       }
 }
 
 void HUD_Reset()
@@ -413,12 +502,12 @@ bool Hud_Shake_Update()
        return true;
 }
 
-void calc_followmodel_ofs(entity view);
 void Hud_Dynamic_Frame()
 {
        vector ofs = '0 0 0';
-       hud_scale = '1 1 0';
-       hud_shift = '0 0 0';
+       hud_scale_current = '1 1 0';
+       hud_shift_current = '0 0 0';
+
        if (autocvar_hud_dynamic_follow)
        {
                entity view = CSQCModel_server2csqc(player_localentnum - 1);
@@ -435,12 +524,12 @@ void Hud_Dynamic_Frame()
                ofs.y = bound(-0.1, ofs.y, 0.1);
                ofs.z = bound(-0.1, ofs.z, 0.1);
 
-               hud_shift.x = ofs.y * vid_conwidth;
-               hud_shift.y = ofs.z * vid_conheight;
-               hud_shift.z = ofs.x;
+               hud_shift_current.x = ofs.y * vid_conwidth;
+               hud_shift_current.y = ofs.z * vid_conheight;
+               hud_shift_current.z = ofs.x;
 
-               hud_scale.x = (1 + hud_shift.z);
-               hud_scale.y = hud_scale.x;
+               hud_scale_current.x = (1 + hud_shift_current.z);
+               hud_scale_current.y = hud_scale_current.x;
        }
 
        if(autocvar_hud_dynamic_shake > 0)
@@ -477,18 +566,36 @@ void Hud_Dynamic_Frame()
 
                if(hud_dynamic_shake_factor > 0)
                {
-                       hud_shift.x += hud_dynamic_shake_realofs.x;
-                       hud_shift.y += hud_dynamic_shake_realofs.y;
+                       hud_shift_current.x += hud_dynamic_shake_realofs.x;
+                       hud_shift_current.y += hud_dynamic_shake_realofs.y;
                }
        }
 
        hud_scale_center.x = 0.5 * vid_conwidth;
        hud_scale_center.y = 0.5 * vid_conheight;
 
-       hud_scale_current = hud_scale;
-       hud_shift_current = hud_shift;
+       HUD_Scale_Disable();
+}
+
+bool HUD_WouldShowCursor()
+{
+       if(autocvar__hud_configure)
+               return true;
+       if(mv_active)
+               return true;
+       //entity local_player = ((csqcplayer) ? csqcplayer : CSQCModel_server2csqc(player_localentnum - 1)); // TODO: doesn't use regular cursor handling
+       //if(local_player.viewloc && (local_player.viewloc.spawnflags & VIEWLOC_FREEAIM))
+               //return true;
+       if(HUD_Radar_Clickable())
+               return true;
+       if(HUD_MinigameMenu_IsOpened())
+               return true;
+       if(QuickMenu_IsOpened())
+               return true;
+       return false;
 }
 
+float prev_myteam;
 void HUD_Main()
 {
        int i;
@@ -497,9 +604,14 @@ void HUD_Main()
        else
                hud_fade_alpha = 1 - autocvar__menu_alpha;
 
-       HUD_Configure_Frame();
+       if(myteam != prev_myteam)
+       {
+               myteamcolors = colormapPaletteColor(myteam, 1);
+               FOREACH(hud_panels, true, it.update_time = time);
+               prev_myteam = myteam;
+       }
 
-       Hud_Dynamic_Frame();
+       HUD_Configure_Frame();
 
        if(scoreboard_fade_alpha == 1)
                if(autocvar__menu_alpha == 1)
@@ -508,12 +620,8 @@ void HUD_Main()
        // Drawing stuff
        if (hud_skin_prev != autocvar_hud_skin)
        {
-               if (hud_skin_path)
-                       strunzone(hud_skin_path);
-               hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
-               if (hud_skin_prev)
-                       strunzone(hud_skin_prev);
-               hud_skin_prev = strzone(autocvar_hud_skin);
+               strcpy(hud_skin_path, strcat("gfx/hud/", autocvar_hud_skin));
+               strcpy(hud_skin_prev, autocvar_hud_skin);
        }
 
        // draw the dock
@@ -535,11 +643,11 @@ void HUD_Main()
                {
                        string hud_dock_color = autocvar_hud_dock_color;
                        if(hud_dock_color == "shirt") {
-                               f = stof(getplayerkeyvalue(current_player, "colors"));
+                               f = entcs_GetClientColors(current_player);
                                color = colormapPaletteColor(floor(f / 16), 0);
                        }
                        else if(hud_dock_color == "pants") {
-                               f = stof(getplayerkeyvalue(current_player, "colors"));
+                               f = entcs_GetClientColors(current_player);
                                color = colormapPaletteColor(f % 16, 1);
                        }
                        else
@@ -559,18 +667,18 @@ void HUD_Main()
 
        // cache the panel order into the panel_order array
        if(autocvar__hud_panelorder != hud_panelorder_prev) {
-               for(i = 0; i < hud_panels_COUNT; ++i)
+               for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i)
                        panel_order[i] = -1;
                string s = "";
                int p_num;
                bool warning = false;
                int argc = tokenize_console(autocvar__hud_panelorder);
-               if (argc > hud_panels_COUNT)
+               if (argc > REGISTRY_COUNT(hud_panels))
                        warning = true;
                //first detect wrong/missing panel numbers
-               for(i = 0; i < hud_panels_COUNT; ++i) {
+               for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
                        p_num = stoi(argv(i));
-                       if (p_num >= 0 && p_num < hud_panels_COUNT) { //correct panel number?
+                       if (p_num >= 0 && p_num < REGISTRY_COUNT(hud_panels)) { //correct panel number?
                                if (panel_order[p_num] == -1) //found for the first time?
                                        s = strcat(s, ftos(p_num), " ");
                                panel_order[p_num] = 1; //mark as found
@@ -578,7 +686,7 @@ void HUD_Main()
                        else
                                warning = true;
                }
-               for(i = 0; i < hud_panels_COUNT; ++i) {
+               for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
                        if (panel_order[i] == -1) {
                                warning = true;
                                s = strcat(s, ftos(i), " "); //add missing panel number
@@ -588,21 +696,19 @@ void HUD_Main()
                        LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder");
 
                cvar_set("_hud_panelorder", s);
-               if(hud_panelorder_prev)
-                       strunzone(hud_panelorder_prev);
-               hud_panelorder_prev = strzone(s);
+               strcpy(hud_panelorder_prev, s);
 
                //now properly set panel_order
                tokenize_console(s);
-               for(i = 0; i < hud_panels_COUNT; ++i) {
+               for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
                        panel_order[i] = stof(argv(i));
                }
        }
 
        hud_draw_maximized = 0;
        // draw panels in the order specified by panel_order array
-       for(i = hud_panels_COUNT - 1; i >= 0; --i)
-               HUD_Panel_Draw(hud_panels_from(panel_order[i]));
+       for(i = REGISTRY_COUNT(hud_panels) - 1; i >= 0; --i)
+               HUD_Panel_Draw(REGISTRY_GET(hud_panels, panel_order[i]));
 
        HUD_Vehicle();
 
@@ -612,10 +718,20 @@ void HUD_Main()
                HUD_Panel_Draw(HUD_PANEL(RADAR));
        if(autocvar__con_chat_maximized)
                HUD_Panel_Draw(HUD_PANEL(CHAT));
-       if(hud_panel_quickmenu)
+       if (QuickMenu_IsOpened())
                HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
        HUD_Panel_Draw(HUD_PANEL(SCOREBOARD));
 
+       int cursor_active_prev = cursor_active;
+       cursor_active = HUD_WouldShowCursor();
+       if (cursor_active_prev != cursor_active && autocvar_hud_cursormode)
+       {
+               setcursormode(cursor_active);
+               // cursor inactive this frame, will be set to 1 the next frame
+               if (cursor_active)
+                       cursor_active = -1;
+       }
+
        if (intermission == 2)
                HUD_Reset();