]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/hud.qc
dd91dbd687894840efea279dd9c87899ef037334
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / hud.qc
1 #include "hud.qh"
2
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>
23
24
25 /*
26 ==================
27 Misc HUD functions
28 ==================
29 */
30
31 vector HUD_Get_Num_Color (float hp, float maxvalue)
32 {
33         float blinkingamt;
34         vector color;
35         if(hp >= maxvalue) {
36                 color.x = sin(2*M_PI*time);
37                 color.y = 1;
38                 color.z = sin(2*M_PI*time);
39         }
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
43                 color.z = 0;
44         }
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
49         }
50         else if(hp > maxvalue * 0.25) {
51                 color.x = 1;
52                 color.y = 1;
53                 color.z = 0.2 + (hp-50)*0.02 * 0.8; // blue value between 0.2 -> 1
54         }
55         else if(hp > maxvalue * 0.1) {
56                 color.x = 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
59         }
60         else {
61                 color.x = 1;
62                 color.y = 0;
63                 color.z = 0;
64         }
65
66         blinkingamt = (1 - hp/maxvalue/0.25);
67         if(blinkingamt > 0)
68         {
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);
72         }
73         return color;
74 }
75
76 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
77 {
78         TC(int, item_count);
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);
81 }
82
83 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
84 {
85         TC(int, item_count);
86         float columns, rows;
87         float ratio, best_ratio = 0;
88         float best_columns = 1, best_rows = 1;
89         bool vertical = (psize.x / psize.y >= item_aspect);
90         if(vertical)
91         {
92                 psize = eX * psize.y + eY * psize.x;
93                 item_aspect = 1 / item_aspect;
94         }
95
96         rows = ceil(sqrt(item_count));
97         columns = ceil(item_count/rows);
98         while(columns >= 1)
99         {
100                 ratio = (psize.x/columns) / (psize.y/rows);
101                 if(ratio > item_aspect)
102                         ratio = item_aspect * item_aspect / ratio;
103
104                 if(ratio <= best_ratio)
105                         break; // ratio starts decreasing by now, skip next configurations
106
107                 best_columns = columns;
108                 best_rows = rows;
109                 best_ratio = ratio;
110
111                 if(columns == 1)
112                         break;
113
114                 --columns;
115                 rows = ceil(item_count/columns);
116         }
117
118         return (vertical) ? vec2(best_rows, best_columns) : vec2(best_columns, best_rows);
119 }
120
121 /*
122 ==================
123 HUD panels
124 ==================
125 */
126
127 void HUD_Panel_LoadCvars()
128 {
129         // NOTE: in hud_configure mode cvars must be reloaded every frame
130         if (panel.update_time <= time)
131         {
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"));
141                 HUD_Panel_GetBg();
142                 if (panel.current_panel_bg != "0")
143                 {
144                         HUD_Panel_GetBgAlpha();
145                         HUD_Panel_GetBorder();
146                 }
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();
155                 else
156                 {
157                         panel_bg_alpha *= hud_fade_alpha * panel_fade_alpha;
158                         panel_fg_alpha *= hud_fade_alpha * panel_fade_alpha;
159                 }
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;
167                 return;
168         }
169
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;
178 }
179
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)
182 {
183         TC(bool, vertical); TC(int, drawflag);
184         if(!length_ratio || !theAlpha)
185                 return;
186         if(length_ratio > 1)
187                 length_ratio = 1;
188         if (baralign == 3)
189         {
190                 if(length_ratio < -1)
191                         length_ratio = -1;
192         }
193         else if(length_ratio < 0)
194                 return;
195
196         theOrigin = HUD_Shift(theOrigin);
197         theSize = HUD_Scale(theSize);
198
199         vector square;
200         vector width, height;
201         if(vertical) {
202                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
203                 if(precache_pic(pic) == "") {
204                         pic = "gfx/hud/default/progressbar_vertical";
205                 }
206
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
212                 {
213                         theSize.y *= 0.5;
214                         if (length_ratio > 0)
215                                 theOrigin.y += theSize.y;
216                         else
217                         {
218                                 theOrigin.y += (1 + length_ratio) * theSize.y;
219                                 length_ratio = -length_ratio;
220                         }
221                 }
222                 theSize.y *= length_ratio;
223
224                 vector bH;
225                 width = eX * theSize.x;
226                 height = eY * theSize.y;
227                 if(theSize.y <= theSize.x * 2)
228                 {
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);
235                 }
236                 else
237                 {
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);
242                 }
243         } else {
244                 pic = strcat(hud_skin_path, "/", pic);
245                 if(precache_pic(pic) == "") {
246                         pic = "gfx/hud/default/progressbar";
247                 }
248
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
254                 {
255                         theSize.x *= 0.5;
256                         if (length_ratio > 0)
257                                 theOrigin.x += theSize.x;
258                         else
259                         {
260                                 theOrigin.x += (1 + length_ratio) * theSize.x;
261                                 length_ratio = -length_ratio;
262                         }
263                 }
264                 theSize.x *= length_ratio;
265
266                 vector bW;
267                 width = eX * theSize.x;
268                 height = eY * theSize.y;
269                 if(theSize.x <= theSize.y * 2)
270                 {
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);
277                 }
278                 else
279                 {
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);
284                 }
285         }
286 }
287
288 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
289 {
290         TC(int, drawflag);
291         if(!theAlpha)
292                 return;
293
294         pos = HUD_Shift(pos);
295         mySize = HUD_Scale(mySize);
296
297         string pic;
298         pic = strcat(hud_skin_path, "/num_leading");
299         if(precache_pic(pic) == "") {
300                 pic = "gfx/hud/default/num_leading";
301         }
302
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);
307 }
308
309 void DrawNumIcon_expanding(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha, float fadelerp)
310 {
311         TC(bool, vertical); TC(int, icon_right_align);
312         vector newPos = '0 0 0', newSize = '0 0 0';
313         vector picpos, numpos;
314
315         if (vertical)
316         {
317                 if(mySize.y/mySize.x > 2)
318                 {
319                         newSize.y = 2 * mySize.x;
320                         newSize.x = mySize.x;
321
322                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
323                         newPos.x = myPos.x;
324                 }
325                 else
326                 {
327                         newSize.x = 1/2 * mySize.y;
328                         newSize.y = mySize.y;
329
330                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
331                         newPos.y = myPos.y;
332                 }
333
334                 if(icon_right_align)
335                 {
336                         numpos = newPos;
337                         picpos = newPos + eY * newSize.x;
338                 }
339                 else
340                 {
341                         picpos = newPos;
342                         numpos = newPos + eY * newSize.x;
343                 }
344
345                 newSize.y /= 2;
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);
350                 newSize.y *= 0.7;
351                 drawstring_aspect(numpos, ftos(theTime), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
352                 return;
353         }
354
355         if(mySize.x/mySize.y > 3)
356         {
357                 newSize.x = 3 * mySize.y;
358                 newSize.y = mySize.y;
359
360                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
361                 newPos.y = myPos.y;
362         }
363         else
364         {
365                 newSize.y = 1/3 * mySize.x;
366                 newSize.x = mySize.x;
367
368                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
369                 newPos.x = myPos.x;
370         }
371
372         if(icon_right_align) // right align
373         {
374                 numpos = newPos;
375                 picpos = newPos + eX * 2 * newSize.y;
376         }
377         else // left align
378         {
379                 numpos = newPos + eX * newSize.y;
380                 picpos = newPos;
381         }
382
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);
387 }
388
389 void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha)
390 {
391         TC(bool, vertical); TC(int, icon_right_align);
392         DrawNumIcon_expanding(myPos, mySize, theTime, icon, vertical, icon_right_align, color, theAlpha, 0);
393 }
394
395 /*
396 ==================
397 Main HUD system
398 ==================
399 */
400
401 float lasthud;
402 float vh_notice_time;
403 void HUD_Vehicle()
404 {
405         if(autocvar__hud_configure) return;
406         if(intermission == 2) return;
407
408         if(hud == HUD_BUMBLEBEE_GUN)
409                 CSQC_BUMBLE_GUN_HUD();
410         else {
411                 Vehicle info = Vehicles_from(hud);
412                 info.vr_hud(info);
413         }
414
415         if(hud != HUD_NORMAL && lasthud == HUD_NORMAL)
416                 vh_notice_time = time + autocvar_cl_vehicles_notify_time;
417
418         lasthud = hud;
419 }
420
421 void HUD_Panel_Draw(entity panent)
422 {
423         panel = panent;
424         if (autocvar__hud_configure)
425         {
426                 if (!(panel.panel_configflags & PANEL_CONFIG_MAIN))
427                         return;
428                 panel_fade_alpha = 1;
429                 Hud_Panel_GetPanelEnabled();
430                 panel.panel_draw();
431                 return;
432         }
433
434         bool draw_allowed = false;
435         if (scoreboard_fade_alpha && panel.panel_showflags & PANEL_SHOW_WITH_SB)
436         {
437                 draw_allowed = true;
438         }
439         else if (active_minigame && HUD_MinigameMenu_IsOpened())
440         {
441                 if (panel.panel_showflags & PANEL_SHOW_MINIGAME)
442                         draw_allowed = true;
443         }
444         else if(intermission == 2)
445         {
446                 if(panel.panel_showflags & PANEL_SHOW_MAPVOTE)
447                         draw_allowed = true;
448         }
449         else if (panel.panel_showflags & PANEL_SHOW_MAINGAME)
450                 draw_allowed = true;
451
452         if (draw_allowed)
453         {
454                 if (panel.panel_showflags & PANEL_SHOW_WITH_SB)
455                 {
456                         if (scoreboard_fade_alpha && intermission == 2 && !(panel.panel_showflags & PANEL_SHOW_MAPVOTE))
457                                 panel_fade_alpha = scoreboard_fade_alpha;
458                         else
459                                 panel_fade_alpha = 1;
460                 }
461                 else
462                 {
463                         panel_fade_alpha = 1 - scoreboard_fade_alpha;
464                         if(!panel_fade_alpha)
465                                 return;
466                 }
467                 panel.panel_draw();
468         }
469 }
470
471 void HUD_Reset()
472 {
473         // reset gametype specific icons
474         if(gametype.m_modicons_reset)
475                 gametype.m_modicons_reset();
476 }
477
478 float autocvar_hud_dynamic_shake = 1;
479 float autocvar_hud_dynamic_shake_damage_max = 130;
480 float autocvar_hud_dynamic_shake_damage_min = 10;
481 float autocvar_hud_dynamic_shake_scale = 0.2;
482 float hud_dynamic_shake_x[10] = {0,    1, -0.7,  0.5, -0.3,  0.2, -0.1,  0.1,  0.0, 0};
483 float hud_dynamic_shake_y[10] = {0,  0.4,  0.8, -0.2, -0.6,  0.0,  0.3,  0.1, -0.1, 0};
484 bool Hud_Shake_Update()
485 {
486         if(time - hud_dynamic_shake_time < 0)
487                 return false;
488
489         float anim_speed = 17 + 9 * hud_dynamic_shake_factor;
490         float elapsed_time = (time - hud_dynamic_shake_time) * anim_speed;
491         int i = floor(elapsed_time);
492         if(i >= 9)
493                 return false;
494
495         float f = elapsed_time - i;
496         hud_dynamic_shake_realofs.x = (1 - f) * hud_dynamic_shake_x[i] + f * hud_dynamic_shake_x[i+1];
497         hud_dynamic_shake_realofs.y = (1 - f) * hud_dynamic_shake_y[i] + f * hud_dynamic_shake_y[i+1];
498         hud_dynamic_shake_realofs.z = 0;
499         hud_dynamic_shake_realofs *= hud_dynamic_shake_factor * autocvar_hud_dynamic_shake_scale;
500         hud_dynamic_shake_realofs.x = bound(-0.1, hud_dynamic_shake_realofs.x, 0.1) * vid_conwidth;
501         hud_dynamic_shake_realofs.y = bound(-0.1, hud_dynamic_shake_realofs.y, 0.1) * vid_conheight;
502         return true;
503 }
504
505 void Hud_Dynamic_Frame()
506 {
507         vector ofs = '0 0 0';
508         hud_scale_current = '1 1 0';
509         hud_shift_current = '0 0 0';
510
511         if (autocvar_hud_dynamic_follow)
512         {
513                 entity view = CSQCModel_server2csqc(player_localentnum - 1);
514                 calc_followmodel_ofs(view);
515                 ofs = -cl_followmodel_ofs * autocvar_hud_dynamic_follow_scale;
516                 ofs.x *= autocvar_hud_dynamic_follow_scale_xyz.z;
517                 ofs.y *= autocvar_hud_dynamic_follow_scale_xyz.x;
518                 ofs.z *= autocvar_hud_dynamic_follow_scale_xyz.y;
519
520                 if (fabs(ofs.x) < 0.001) ofs.x = 0;
521                 if (fabs(ofs.y) < 0.001) ofs.y = 0;
522                 if (fabs(ofs.z) < 0.001) ofs.z = 0;
523                 ofs.x = bound(-0.1, ofs.x, 0.1);
524                 ofs.y = bound(-0.1, ofs.y, 0.1);
525                 ofs.z = bound(-0.1, ofs.z, 0.1);
526
527                 hud_shift_current.x = ofs.y * vid_conwidth;
528                 hud_shift_current.y = ofs.z * vid_conheight;
529                 hud_shift_current.z = ofs.x;
530
531                 hud_scale_current.x = (1 + hud_shift_current.z);
532                 hud_scale_current.y = hud_scale_current.x;
533         }
534
535         if(autocvar_hud_dynamic_shake > 0)
536         {
537                 static float old_health = 0;
538                 float health = max(-1, STAT(HEALTH));
539                 if(hud_dynamic_shake_factor == -1) // don't allow the effect for this frame
540                 {
541                         hud_dynamic_shake_factor = 0;
542                         old_health = health;
543                 }
544                 else
545                 {
546                         float new_hud_dynamic_shake_factor = 0;
547                         if (old_health - health >= autocvar_hud_dynamic_shake_damage_min
548                                 && autocvar_hud_dynamic_shake_damage_max > autocvar_hud_dynamic_shake_damage_min
549                                 && old_health > 0 && !intermission)
550                         {
551                                 float m = max(autocvar_hud_dynamic_shake_damage_min, 1);
552                                 new_hud_dynamic_shake_factor = (old_health - health - m) / (autocvar_hud_dynamic_shake_damage_max - m);
553                                 if(new_hud_dynamic_shake_factor >= 1)
554                                         new_hud_dynamic_shake_factor = 1;
555                                 if(new_hud_dynamic_shake_factor >= hud_dynamic_shake_factor)
556                                 {
557                                         hud_dynamic_shake_factor = new_hud_dynamic_shake_factor;
558                                         hud_dynamic_shake_time = time;
559                                 }
560                         }
561                         old_health = health;
562                         if(hud_dynamic_shake_factor)
563                                 if(!Hud_Shake_Update())
564                                         hud_dynamic_shake_factor = 0;
565                 }
566
567                 if(hud_dynamic_shake_factor > 0)
568                 {
569                         hud_shift_current.x += hud_dynamic_shake_realofs.x;
570                         hud_shift_current.y += hud_dynamic_shake_realofs.y;
571                 }
572         }
573
574         hud_scale_center.x = 0.5 * vid_conwidth;
575         hud_scale_center.y = 0.5 * vid_conheight;
576
577         HUD_Scale_Disable();
578 }
579
580 bool HUD_WouldShowCursor()
581 {
582         if(autocvar__hud_configure)
583                 return true;
584         if(mv_active)
585                 return true;
586         //entity local_player = ((csqcplayer) ? csqcplayer : CSQCModel_server2csqc(player_localentnum - 1)); // TODO: doesn't use regular cursor handling
587         //if(local_player.viewloc && (local_player.viewloc.spawnflags & VIEWLOC_FREEAIM))
588                 //return true;
589         if(HUD_Radar_Clickable())
590                 return true;
591         if(HUD_MinigameMenu_IsOpened())
592                 return true;
593         if(QuickMenu_IsOpened())
594                 return true;
595         return false;
596 }
597
598 float prev_myteam;
599 void HUD_Main()
600 {
601         int i;
602         if(hud_configure_menu_open == 1)
603                 hud_fade_alpha = 1;
604         else
605                 hud_fade_alpha = 1 - autocvar__menu_alpha;
606
607         if(myteam != prev_myteam)
608         {
609                 myteamcolors = colormapPaletteColor(myteam, 1);
610                 FOREACH(hud_panels, true, it.update_time = time);
611                 prev_myteam = myteam;
612         }
613
614         HUD_Configure_Frame();
615
616         if(scoreboard_fade_alpha == 1)
617                 if(autocvar__menu_alpha == 1)
618                         return;
619
620         // Drawing stuff
621         if (hud_skin_prev != autocvar_hud_skin)
622         {
623                 strcpy(hud_skin_path, strcat("gfx/hud/", autocvar_hud_skin));
624                 strcpy(hud_skin_prev, autocvar_hud_skin);
625         }
626
627         // draw the dock
628         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
629         {
630                 int f;
631                 vector color;
632                 float hud_dock_color_team = autocvar_hud_dock_color_team;
633                 if((teamplay) && hud_dock_color_team) {
634                         if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
635                                 color = '1 0 0' * hud_dock_color_team;
636                         else
637                                 color = myteamcolors * hud_dock_color_team;
638                 }
639                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
640                         color = '1 0 0' * hud_dock_color_team;
641                 }
642                 else
643                 {
644                         string hud_dock_color = autocvar_hud_dock_color;
645                         if(hud_dock_color == "shirt") {
646                                 f = entcs_GetClientColors(current_player);
647                                 color = colormapPaletteColor(floor(f / 16), 0);
648                         }
649                         else if(hud_dock_color == "pants") {
650                                 f = entcs_GetClientColors(current_player);
651                                 color = colormapPaletteColor(f % 16, 1);
652                         }
653                         else
654                                 color = stov(hud_dock_color);
655                 }
656
657                 string pic;
658                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
659                 if(precache_pic(pic) == "") {
660                         pic = strcat(hud_skin_path, "/dock_medium");
661                         if(precache_pic(pic) == "") {
662                                 pic = "gfx/hud/default/dock_medium";
663                         }
664                 }
665                 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...
666         }
667
668         // cache the panel order into the panel_order array
669         if(autocvar__hud_panelorder != hud_panelorder_prev) {
670                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i)
671                         panel_order[i] = -1;
672                 string s = "";
673                 int p_num;
674                 bool warning = false;
675                 int argc = tokenize_console(autocvar__hud_panelorder);
676                 if (argc > REGISTRY_COUNT(hud_panels))
677                         warning = true;
678                 //first detect wrong/missing panel numbers
679                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
680                         p_num = stoi(argv(i));
681                         if (p_num >= 0 && p_num < REGISTRY_COUNT(hud_panels)) { //correct panel number?
682                                 if (panel_order[p_num] == -1) //found for the first time?
683                                         s = strcat(s, ftos(p_num), " ");
684                                 panel_order[p_num] = 1; //mark as found
685                         }
686                         else
687                                 warning = true;
688                 }
689                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
690                         if (panel_order[i] == -1) {
691                                 warning = true;
692                                 s = strcat(s, ftos(i), " "); //add missing panel number
693                         }
694                 }
695                 if (warning)
696                         LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder");
697
698                 cvar_set("_hud_panelorder", s);
699                 strcpy(hud_panelorder_prev, s);
700
701                 //now properly set panel_order
702                 tokenize_console(s);
703                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
704                         panel_order[i] = stof(argv(i));
705                 }
706         }
707
708         hud_draw_maximized = 0;
709         // draw panels in the order specified by panel_order array
710         for(i = REGISTRY_COUNT(hud_panels) - 1; i >= 0; --i)
711                 HUD_Panel_Draw(hud_panels_from(panel_order[i]));
712
713         HUD_Vehicle();
714
715         hud_draw_maximized = 1; // panels that may be maximized must check this var
716         // draw maximized panels on top
717         if(hud_panel_radar_maximized)
718                 HUD_Panel_Draw(HUD_PANEL(RADAR));
719         if(autocvar__con_chat_maximized)
720                 HUD_Panel_Draw(HUD_PANEL(CHAT));
721         if (QuickMenu_IsOpened())
722                 HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
723         HUD_Panel_Draw(HUD_PANEL(SCOREBOARD));
724
725         int cursor_active_prev = cursor_active;
726         cursor_active = HUD_WouldShowCursor();
727         if (cursor_active_prev != cursor_active && autocvar_hud_cursormode)
728         {
729                 setcursormode(cursor_active);
730                 // cursor inactive this frame, will be set to 1 the next frame
731                 if (cursor_active)
732                         cursor_active = -1;
733         }
734
735         if (intermission == 2)
736                 HUD_Reset();
737
738         HUD_Configure_PostDraw();
739
740         hud_configure_prev = autocvar__hud_configure;
741 }