70eb08128dea3e621bb0cc4d0225879f5b442fce
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud.qh
1 const int HUD_PANEL_MAX = 24;
2 entity hud_panel[HUD_PANEL_MAX];
3 const int HUD_PANEL_FIRST = 0;
4 int HUD_PANEL_NUM;
5 int HUD_PANEL_LAST;
6
7 int panel_order[HUD_PANEL_MAX];
8 string hud_panelorder_prev;
9
10 float hud_draw_maximized;
11 float hud_panel_radar_maximized;
12 float chat_panel_modified;
13 float radar_panel_modified;
14
15 vector mousepos;
16 vector panel_click_distance; // mouse cursor distance from the top left corner of the panel (saved only upon a click)
17 vector panel_click_resizeorigin; // coordinates for opposite point when resizing
18 float resizeCorner; // 1 = topleft, 2 = topright, 3 = bottomleft, 4 = bottomright
19 entity highlightedPanel;
20 float highlightedAction; // 0 = nothing, 1 = move, 2 = resize
21
22 const float BORDER_MULTIPLIER = 0.25;
23 float scoreboard_bottom;
24 int weapon_accuracy[WEP_MAXCOUNT];
25
26 float complain_weapon;
27 string complain_weapon_name;
28 float complain_weapon_type;
29 float complain_weapon_time;
30
31 int ps_primary, ps_secondary;
32 int ts_primary, ts_secondary;
33
34 float last_switchweapon;
35 float last_activeweapon;
36 float weapontime;
37 float weaponprevtime;
38
39 float teamnagger;
40
41 float hud_configure_checkcollisions;
42 float hud_configure_prev;
43 vector hud_configure_gridSize;
44 vector hud_configure_realGridSize;
45
46 int hudShiftState;
47 const int S_SHIFT = 1;
48 const int S_CTRL = 2;
49 const int S_ALT = 4;
50
51 float menu_enabled; // 1 showing the entire HUD, 2 showing only the clicked panel
52
53 float hud_fade_alpha;
54
55 string hud_skin_path;
56 string hud_skin_prev;
57
58 vector myteamcolors;
59
60 entity highlightedPanel_backup;
61 vector panel_pos_backup;
62 vector panel_size_backup;
63
64 vector panel_size_copied;
65
66 entity panel;
67 .string panel_name;
68 .int panel_id;
69 .vector current_panel_pos;
70 .vector current_panel_size;
71 .string current_panel_bg;
72 .float current_panel_bg_alpha;
73 .float current_panel_bg_border;
74 .vector current_panel_bg_color;
75 .float current_panel_bg_color_team;
76 .float current_panel_bg_padding;
77 .float current_panel_fg_alpha;
78 .float update_time;
79 float panel_enabled;
80 vector panel_pos;
81 vector panel_size;
82 string panel_bg_str; // "_str" vars contain the raw value of the cvar, non-"_str" contains what hud.qc code should use
83 vector panel_bg_color;
84 string panel_bg_color_str;
85 float panel_bg_color_team;
86 string panel_bg_color_team_str;
87 float panel_fg_alpha;
88 float panel_bg_alpha;
89 string panel_bg_alpha_str;
90 float panel_bg_border;
91 string panel_bg_border_str;
92 float panel_bg_padding;
93 string panel_bg_padding_str;
94
95 .void() panel_draw;
96
97 float current_player;
98
99
100 #define HUD_PANELS(HUD_PANEL)                                                                                                                                                                           \
101         HUD_PANEL(WEAPONS      , HUD_Weapons      , weapons)                                                                                                                    \
102         HUD_PANEL(AMMO         , HUD_Ammo         , ammo)                                                                                                                               \
103         HUD_PANEL(POWERUPS     , HUD_Powerups     , powerups)                                                                                                                   \
104         HUD_PANEL(HEALTHARMOR  , HUD_HealthArmor  , healtharmor)                                                                                                                \
105         HUD_PANEL(NOTIFY       , HUD_Notify       , notify)                                                                                                                     \
106         HUD_PANEL(TIMER        , HUD_Timer        , timer)                                                                                                                              \
107         HUD_PANEL(RADAR        , HUD_Radar        , radar)                                                                                                                              \
108         HUD_PANEL(SCORE        , HUD_Score        , score)                                                                                                                              \
109         HUD_PANEL(RACETIMER    , HUD_RaceTimer    , racetimer)                                                                                                                  \
110         HUD_PANEL(VOTE         , HUD_Vote         , vote)                                                                                                                               \
111         HUD_PANEL(MODICONS     , HUD_ModIcons     , modicons)                                                                                                                   \
112         HUD_PANEL(PRESSEDKEYS  , HUD_PressedKeys  , pressedkeys)                                                                                                                \
113         HUD_PANEL(CHAT         , HUD_Chat         , chat)                                                                                                                               \
114         HUD_PANEL(ENGINEINFO   , HUD_EngineInfo   , engineinfo)                                                                                                                 \
115         HUD_PANEL(INFOMESSAGES , HUD_InfoMessages , infomessages)                                                                                                               \
116         HUD_PANEL(PHYSICS      , HUD_Physics      , physics)                                                                                                                    \
117         HUD_PANEL(CENTERPRINT  , HUD_CenterPrint  , centerprint)                                                                                                                \
118         HUD_PANEL(BUFFS        , HUD_Buffs        , buffs)
119
120 #define HUD_PANEL(NAME, draw_func, name)                                                                                                                                                        \
121         int HUD_PANEL_##NAME;                                                                                                                                                                                   \
122         void draw_func(void);                                                                                                                                                                                   \
123         void RegisterHUD_Panel_##NAME() {                                                                                                                                                               \
124                 HUD_PANEL_LAST = HUD_PANEL_##NAME = HUD_PANEL_NUM;                                                                                                                      \
125                 entity hud_panelent = spawn();                                                                                                                                                          \
126                 hud_panel[HUD_PANEL_##NAME] = hud_panelent;                                                                                                                             \
127                 hud_panelent.classname = "hud_panel";                                                                                                                                           \
128                 hud_panelent.panel_name = #name;                                                                                                                                                        \
129                 hud_panelent.panel_id = HUD_PANEL_##NAME;                                                                                                                                       \
130                 hud_panelent.panel_draw = draw_func;                                                                                                                                            \
131                 HUD_PANEL_NUM++;                                                                                                                                                                                        \
132         }                                                                                                                                                                                                                               \
133         ACCUMULATE_FUNCTION(RegisterHUD_Panels, RegisterHUD_Panel_##NAME);
134
135 HUD_PANELS(HUD_PANEL)
136 #undef HUD_PANEL
137
138 #define HUD_PANEL(NAME) hud_panel[HUD_PANEL_##NAME]
139
140
141 // Because calling lots of functions in QC apparently cuts fps in half on many machines:
142 // ----------------------
143 // MACRO HELL STARTS HERE
144 // ----------------------
145 // Little help for the poor people who have to make sense of this: Start from the bottom ;)
146
147 // Get value for panel.current_panel_bg: if "" fetch default, else use panel_bg_str
148 // comment on last line of macro: // we probably want to see a background in config mode at all times...
149 #define HUD_Panel_GetBg() do {                                                                                      \
150         string panel_bg;                                                                                                \
151         if (!autocvar__hud_configure && panel_bg_str == "0") {                                                          \
152                 panel_bg = "0";                                                                                             \
153         } else {                                                                                                        \
154                 if (panel_bg_str == "") {                                                                                   \
155                         panel_bg_str = autocvar_hud_panel_bg;                                                                   \
156                 }                                                                                                           \
157                 if (panel_bg_str == "0" && !autocvar__hud_configure) {                                                      \
158                         panel_bg = "0";                                                                                         \
159                 } else {                                                                                                    \
160                         if (panel_bg_str == "0" && autocvar__hud_configure)                                                     \
161                                 panel_bg_alpha_str = "0";                                                                           \
162                         panel_bg = strcat(hud_skin_path, "/", panel_bg_str);                                                    \
163                         if (precache_pic(panel_bg) == "") {                                                                     \
164                                 panel_bg = strcat(hud_skin_path, "/", "border_default");                                            \
165                                 if (precache_pic(panel_bg) == "") {                                                                 \
166                                         panel_bg = strcat("gfx/hud/default/", "border_default");                                        \
167                                 }                                                                                                   \
168                         }                                                                                                       \
169                 }                                                                                                           \
170         }                                                                                                               \
171         if (panel.current_panel_bg)                                                                                     \
172                 strunzone(panel.current_panel_bg);                                                                          \
173         panel.current_panel_bg = strzone(panel_bg);                                                                     \
174 } while(0)
175
176 // Get value for panel_bg_color: if "" fetch default, else use panel_bg_color. Convert pants, shirt or teamcolor into a vector.
177 #define HUD_Panel_GetColor() do {                                                                                   \
178         if ((teamplay) && panel_bg_color_team) {                                                                        \
179                 if (autocvar__hud_configure && myteam == NUM_SPECTATOR)                                                     \
180                         panel_bg_color = '1 0 0' * panel_bg_color_team;                                                         \
181                 else                                                                                                        \
182                         panel_bg_color = myteamcolors * panel_bg_color_team;                                                    \
183         } else if (autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && panel_bg_color_team) {          \
184                 panel_bg_color = '1 0 0' * panel_bg_color_team;                                                             \
185         } else {                                                                                                        \
186                 if (panel_bg_color_str == "") {                                                                             \
187                         panel_bg_color = autocvar_hud_panel_bg_color;                                                           \
188                 } else {                                                                                                    \
189                         if (panel_bg_color_str == "shirt") {                                                                    \
190                                 panel_bg_color = colormapPaletteColor(floor(stof(getplayerkeyvalue(current_player - 1, "colors")) / 16), 0);\
191                         } else if (panel_bg_color_str == "pants") {                                                             \
192                                 panel_bg_color = colormapPaletteColor(stof(getplayerkeyvalue(current_player - 1, "colors")) % 16, 1);\
193                         } else {                                                                                                \
194                                 panel_bg_color = stov(panel_bg_color_str);                                                          \
195                         }                                                                                                       \
196                 }                                                                                                           \
197         }                                                                                                               \
198 } while(0)
199
200 // Get value for panel_bg_color_team: if "" fetch default, else use panel_bg_color_team_str
201 #define HUD_Panel_GetColorTeam() do {                                                                               \
202         if (panel_bg_color_team_str == "") {                                                                            \
203                 panel_bg_color_team = autocvar_hud_panel_bg_color_team;                                                     \
204         } else {                                                                                                        \
205                 panel_bg_color_team = stof(panel_bg_color_team_str);                                                        \
206         }                                                                                                               \
207 } while(0)
208
209 // Get value for panel_bg_alpha: if "" fetch default, else use panel_bg_alpha. Also do various menu dialog fadeout/in checks, and minalpha checks
210 // comment on line 3 of macro: // do not set a minalpha cap when showing the config dialog for this panel
211 #define HUD_Panel_GetBgAlpha() do {                                                                                 \
212         if (panel_bg_alpha_str == "") {                                                                                 \
213                 panel_bg_alpha_str = ftos(autocvar_hud_panel_bg_alpha);                                                     \
214         }                                                                                                               \
215         panel_bg_alpha = stof(panel_bg_alpha_str);                                                                      \
216         if (autocvar__hud_configure) {                                                                                  \
217                 if (!panel_enabled)                                                                                         \
218                         panel_bg_alpha = 0.25;                                                                                  \
219                 else if (menu_enabled == 2 && panel == highlightedPanel)                                                    \
220                         panel_bg_alpha = (1 - autocvar__menu_alpha) * max(cvar("hud_configure_bg_minalpha"), panel_bg_alpha) + autocvar__menu_alpha * panel_bg_alpha;\
221                 else                                                                                                        \
222                         panel_bg_alpha = max(cvar("hud_configure_bg_minalpha"), panel_bg_alpha);                                \
223         }                                                                                                               \
224 } while(0)
225
226 // Get value for panel_fg_alpha. Also do various minalpha checks
227 // comment on line 2 of macro: // ALWAYS show disabled panels at 0.25 alpha when in config mode
228 #define HUD_Panel_GetFgAlpha() do {                                                                                 \
229         panel_fg_alpha = autocvar_hud_panel_fg_alpha;                                                                   \
230         if (autocvar__hud_configure && !panel_enabled)                                                                  \
231                 panel_fg_alpha = 0.25;                                                                                      \
232 } while(0)
233
234 // Get border. See comments above, it's similar.
235 #define HUD_Panel_GetBorder() do {                                                                                  \
236         if (panel_bg_border_str == "") {                                                                                \
237                 panel_bg_border = autocvar_hud_panel_bg_border;                                                             \
238         } else {                                                                                                        \
239                 panel_bg_border = stof(panel_bg_border_str);                                                                \
240         }                                                                                                               \
241 } while(0)
242
243 // Get padding. See comments above, it's similar.
244 // last line is a port of the old function, basically always make sure the panel contents are at least 5 pixels tall/wide, to disallow extreme padding values
245 #define HUD_Panel_GetPadding() do {                                                                                 \
246         if (panel_bg_padding_str == "") {                                                                               \
247                 panel_bg_padding = autocvar_hud_panel_bg_padding;                                                           \
248         } else {                                                                                                        \
249                 panel_bg_padding = stof(panel_bg_padding_str);                                                              \
250         }                                                                                                               \
251         panel_bg_padding = min(min(panel_size_x, panel_size_y)/2 - 5, panel_bg_padding);                                \
252 } while(0)
253
254 // return smoothly faded pos and size of given panel when a dialog is active
255 #define HUD_Panel_UpdatePosSize_ForMenu() do {                                                                      \
256         vector menu_enable_pos;                                                                                         \
257         vector menu_enable_size = '0 0 0';                                                                              \
258         float menu_enable_maxsize_x = 0.3 * vid_conwidth;                                                               \
259         float menu_enable_maxsize_y = 0.18 * vid_conheight;                                                             \
260         if (panel_size_x > panel_size_y) {                                                                              \
261                 if (panel_size_y > menu_enable_maxsize_y) {                                                                 \
262                         menu_enable_size_y = menu_enable_maxsize_y;                                                             \
263                         menu_enable_size_x = panel_size_x * (menu_enable_maxsize_y/panel_size_y);                               \
264                         panel_size = (1 - autocvar__menu_alpha) * panel_size + (autocvar__menu_alpha) * menu_enable_size;       \
265                 }                                                                                                           \
266                 menu_enable_pos = eX * 0.5 * vid_conwidth - eX * 0.5 * panel_size_x + eY * (vid_conheight - menu_enable_maxsize_y);\
267         } else {                                                                                                        \
268                 if (panel_size_x > menu_enable_maxsize_x) {                                                                 \
269                         menu_enable_size_x = menu_enable_maxsize_x;                                                             \
270                         menu_enable_size_y = panel_size_y * (menu_enable_maxsize_x/panel_size_x);                               \
271                         panel_size = (1 - autocvar__menu_alpha) * panel_size + (autocvar__menu_alpha) * menu_enable_size;       \
272                 }                                                                                                           \
273                 menu_enable_pos = eY * 0.5 * vid_conheight - eY * 0.5 * panel_size_y + eX * (vid_conwidth - menu_enable_maxsize_x);\
274         }                                                                                                               \
275         panel_pos = (1 - autocvar__menu_alpha) * panel_pos + (autocvar__menu_alpha) * menu_enable_pos;                  \
276 } while(0)
277
278 // Scale the pos and size vectors to absolute coordinates
279 #define HUD_Panel_ScalePosSize() do {                                                                               \
280         panel_pos_x *= vid_conwidth;  panel_pos_y *= vid_conheight;                                                     \
281         panel_size_x *= vid_conwidth; panel_size_y *= vid_conheight;                                                    \
282 } while(0)
283
284 // NOTE: in hud_configure mode cvars must be reloaded every frame
285 #define HUD_Panel_UpdateCvars() do {                                                                                \
286         if (panel.update_time <= time) {                                                                                \
287                 if (autocvar__hud_configure) panel_enabled = cvar(strcat("hud_panel_", panel.panel_name));                  \
288                 panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));                              \
289                 panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));                            \
290                 HUD_Panel_ScalePosSize();                                                                                   \
291                 panel_bg_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg"));                                  \
292                 panel_bg_color_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color"));                      \
293                 panel_bg_color_team_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color_team"));            \
294                 panel_bg_alpha_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_alpha"));                      \
295                 panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));                    \
296                 panel_bg_padding_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_padding"));                  \
297                 HUD_Panel_GetBg();                                                                                          \
298                 if (panel.current_panel_bg != "0") {                                                                        \
299                         HUD_Panel_GetColorTeam();                                                                               \
300                         HUD_Panel_GetColor();                                                                                   \
301                         HUD_Panel_GetBgAlpha();                                                                                 \
302                         HUD_Panel_GetBorder();                                                                                  \
303                 }                                                                                                           \
304                 HUD_Panel_GetFgAlpha();                                                                                     \
305                 HUD_Panel_GetPadding();                                                                                     \
306                 panel.current_panel_bg_alpha = panel_bg_alpha;                                                              \
307                 panel.current_panel_fg_alpha = panel_fg_alpha;                                                              \
308                 if (menu_enabled == 2 && panel == highlightedPanel) {                                                       \
309                         HUD_Panel_UpdatePosSize_ForMenu();                                                                      \
310                 } else {                                                                                                    \
311                         panel_bg_alpha *= hud_fade_alpha;                                                                       \
312                         panel_fg_alpha *= hud_fade_alpha;                                                                       \
313                 }                                                                                                           \
314                 panel.current_panel_pos = panel_pos;                                                                        \
315                 panel.current_panel_size = panel_size;                                                                      \
316                 panel.current_panel_bg_border = panel_bg_border;                                                            \
317                 panel.current_panel_bg_color = panel_bg_color;                                                              \
318                 panel.current_panel_bg_color_team = panel_bg_color_team;                                                    \
319                 panel.current_panel_bg_padding = panel_bg_padding;                                                          \
320                 panel.update_time = (autocvar__hud_configure) ? time : time + autocvar_hud_panel_update_interval;           \
321         } else {                                                                                                        \
322                 panel_pos = panel.current_panel_pos;                                                                        \
323                 panel_size = panel.current_panel_size;                                                                      \
324                 panel_bg_alpha = panel.current_panel_bg_alpha * hud_fade_alpha;                                             \
325                 panel_bg_border = panel.current_panel_bg_border;                                                            \
326                 panel_bg_color = panel.current_panel_bg_color;                                                              \
327                 panel_bg_color_team = panel.current_panel_bg_color_team;                                                    \
328                 panel_bg_padding = panel.current_panel_bg_padding;                                                          \
329                 panel_fg_alpha = panel.current_panel_fg_alpha * hud_fade_alpha;                                             \
330         }                                                                                                               \
331 } while(0)
332
333 #define HUD_Panel_UpdatePosSize() do {                                                                              \
334         panel_enabled = cvar(strcat("hud_panel_", panel.panel_name));                                                   \
335         panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));                                  \
336         panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));                                \
337         HUD_Panel_ScalePosSize();                                                                                       \
338         if (menu_enabled == 2 && panel == highlightedPanel) {                                                           \
339                 HUD_Panel_UpdatePosSize_ForMenu();                                                                          \
340         }                                                                                                               \
341         panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));                        \
342         HUD_Panel_GetBorder();                                                                                          \
343 } while(0)
344
345 const float NOTIFY_MAX_ENTRIES = 10;
346 const float NOTIFY_ICON_MARGIN = 0.02;
347
348 int notify_index;
349 int notify_count;
350 float notify_times[NOTIFY_MAX_ENTRIES];
351 string notify_attackers[NOTIFY_MAX_ENTRIES];
352 string notify_victims[NOTIFY_MAX_ENTRIES];
353 string notify_icons[NOTIFY_MAX_ENTRIES];
354
355 void HUD_Notify_Push(string icon, string attacker, string victim);
356
357 var void HUD_ModIcons_GameType(vector pos, vector size);
358 void HUD_ModIcons_SetFunc();