]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/hud.qc
Merge branch 'master' into terencehill/lms_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / hud.qc
1 #include "hud.qh"
2
3 #include <client/draw.qh>
4 #include <client/hud/hud_config.qh>
5 #include <client/hud/panel/chat.qh>
6 #include <client/hud/panel/scoreboard.qh>
7 #include <client/items/items.qh>
8 #include <client/mapvoting.qh>
9 #include <client/teamradar.qh>
10 #include <client/view.qh>
11 #include <common/deathtypes/all.qh>
12 #include <common/ent_cs.qh>
13 #include <common/gamemodes/_mod.qh>
14 #include <common/items/_mod.qh>
15 #include <common/mapinfo.qh>
16 #include <common/minigames/cl_minigames.qh>
17 #include <common/mutators/mutator/waypoints/all.qh>
18 #include <common/stats.qh>
19 #include <common/vehicles/all.qh>
20 #include <common/vehicles/vehicle/bumblebee.qh>
21 #include <lib/csqcmodel/cl_model.qh>
22 #include <lib/csqcmodel/cl_player.qh>
23
24
25 /*
26 ==================
27 Misc HUD functions
28 ==================
29 */
30
31 void draw_cursor(vector pos, vector ofs, string img, vector col, float a)
32 {
33         ofs = vec2(ofs.x * SIZE_CURSOR.x, ofs.y * SIZE_CURSOR.y);
34         drawpic(pos - ofs, strcat(draw_currentSkin, img), SIZE_CURSOR, col, a, DRAWFLAG_NORMAL);
35 }
36
37 void draw_cursor_normal(vector pos, vector col, float a)
38 {
39         draw_cursor(pos, OFFSET_CURSOR, "/cursor", col, a);
40 }
41
42 void LoadMenuSkinValues()
43 {
44         int fh = -1;
45         if(cvar_string("menu_skin") != "")
46         {
47                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
48                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
49         }
50         if(fh < 0 && cvar_defstring("menu_skin") != "")
51         {
52                 cvar_set("menu_skin", cvar_defstring("menu_skin"));
53                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
54                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
55         }
56         if(fh < 0)
57         {
58                 draw_currentSkin = "gfx/menu/default";
59                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
60         }
61
62         draw_currentSkin = strzone(draw_currentSkin);
63
64         if(fh >= 0)
65         {
66                 string s;
67                 while((s = fgets(fh)))
68                 {
69                         int n = tokenize_console(s);
70                         if (n < 2)
71                                 continue;
72                         if(substring(argv(0), 0, 2) == "//")
73                                 continue;
74                         if(argv(0) == "SIZE_CURSOR")
75                                 SIZE_CURSOR = stov(substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
76                         else if(argv(0) == "OFFSET_CURSOR")
77                                 OFFSET_CURSOR = stov(substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
78                 }
79                 fclose(fh);
80         }
81 }
82
83 void HUD_Scale_Disable()
84 {
85         hud_scale = '1 1 0';
86         hud_shift = '0 0 0';
87         drawfontscale = hud_scale;
88 }
89
90 void HUD_Scale_Enable()
91 {
92         hud_scale = hud_scale_current;
93         hud_shift = hud_shift_current;
94         drawfontscale = hud_scale;
95 }
96
97 vector HUD_Scale(vector v)
98 {
99         v.x = HUD_ScaleX(v.x);
100         v.y = HUD_ScaleY(v.y);
101         return v;
102 }
103
104 vector HUD_Shift(vector v)
105 {
106         v.x = HUD_ShiftX(v.x);
107         v.y = HUD_ShiftY(v.y);
108         return v;
109 }
110
111 vector HUD_GetFontsize(string cvarname)
112 {
113         vector v;
114         v = stov(cvar_string(cvarname));
115         if(v.x == 0)
116                 v = '8 8 0';
117         if(v.y == 0)
118                 v.y = v.x;
119         v.z = 0;
120         return v;
121 }
122
123 vector HUD_Get_Num_Color(float hp, float maxvalue, bool blink)
124 {
125         const vector COLOR100 = '0 1 0'; // green
126         const vector COLOR75 = '0.4 0.9 0'; // lightgreen
127         const vector COLOR50 = '1 1 1'; // white
128         const vector COLOR25 = '1 1 0.2'; // lightyellow
129         const vector COLOR10 = '1 0 0'; // red
130         vector color;
131
132         float hp_percent = hp / maxvalue * 100;
133         #define CASE_COLOR_BETWEEN(min, max) \
134                 if(hp_percent > min) \
135                         color = COLOR##min + (COLOR##max - COLOR##min) * ((hp_percent - min) / (max - min))
136
137         if(hp_percent > 100) color = COLOR100;
138         else CASE_COLOR_BETWEEN(75, 100);
139         else CASE_COLOR_BETWEEN(50, 75);
140         else CASE_COLOR_BETWEEN(25, 50);
141         else CASE_COLOR_BETWEEN(10, 25);
142         else color = COLOR10;
143
144         #undef CASE_COLOR_BETWEEN
145
146         if (blink)
147         {
148                 if(hp_percent >= 100)
149                 {
150                         float f = sin(2*M_PI*time);
151                         if (color.x == 0) color.x = f;
152                         if (color.y == 0) color.y = f;
153                         if (color.z == 0) color.z = f;
154                 }
155                 else if(hp_percent < 25)
156                 {
157                         float f = (1 - hp_percent / 25) * sin(2*M_PI*time);
158                         color -= color * f;
159                 }
160         }
161
162         return color;
163 }
164
165 float HUD_GetRowCount(int item_count, vector size, float item_aspect)
166 {
167         TC(int, item_count);
168         float aspect = size_y / size_x;
169         return bound(1, floor((sqrt(4 * item_aspect * aspect * item_count + aspect * aspect) + aspect + 0.5) / 2), item_count);
170 }
171
172 vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
173 {
174         TC(int, item_count);
175         float columns, rows;
176         float ratio, best_ratio = 0;
177         float best_columns = 1, best_rows = 1;
178         bool vertical = (psize.x / psize.y >= item_aspect);
179         if(vertical)
180         {
181                 psize = eX * psize.y + eY * psize.x;
182                 item_aspect = 1 / item_aspect;
183         }
184
185         rows = ceil(sqrt(item_count));
186         columns = ceil(item_count/rows);
187         while(columns >= 1)
188         {
189                 ratio = (psize.x/columns) / (psize.y/rows);
190                 if(ratio > item_aspect)
191                         ratio = item_aspect * item_aspect / ratio;
192
193                 if(ratio <= best_ratio)
194                         break; // ratio starts decreasing by now, skip next configurations
195
196                 best_columns = columns;
197                 best_rows = rows;
198                 best_ratio = ratio;
199
200                 if(columns == 1)
201                         break;
202
203                 --columns;
204                 rows = ceil(item_count/columns);
205         }
206
207         return (vertical) ? vec2(best_rows, best_columns) : vec2(best_columns, best_rows);
208 }
209
210 /*
211 ==================
212 HUD panels
213 ==================
214 */
215
216 void HUD_Panel_LoadCvars()
217 {
218         // NOTE: in hud_configure mode cvars must be reloaded every frame
219         if (panel.update_time <= time)
220         {
221                 panel_pos = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_pos")));
222                 panel_size = stov(cvar_string(strcat("hud_panel_", panel.panel_name, "_size")));
223                 HUD_Panel_ScalePosSize();
224                 panel_bg_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg"));
225                 panel_bg_color_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color"));
226                 panel_bg_color_team_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_color_team"));
227                 panel_bg_alpha_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_alpha"));
228                 panel_bg_border_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_border"));
229                 panel_bg_padding_str = cvar_string(strcat("hud_panel_", panel.panel_name, "_bg_padding"));
230                 HUD_Panel_GetBg();
231                 if (panel.current_panel_bg != "0")
232                 {
233                         HUD_Panel_GetBgAlpha();
234                         HUD_Panel_GetBorder();
235                 }
236                 HUD_Panel_GetColorTeam();
237                 HUD_Panel_GetColor();
238                 HUD_Panel_GetFgAlpha();
239                 HUD_Panel_GetPadding();
240                 panel.current_panel_bg_alpha = panel_bg_alpha;
241                 panel.current_panel_fg_alpha = panel_fg_alpha;
242                 if (hud_configure_menu_open == 2 && panel == highlightedPanel)
243                         HUD_Panel_UpdatePosSize_ForMenu();
244                 else
245                 {
246                         panel_bg_alpha *= hud_fade_alpha * panel_fade_alpha;
247                         panel_fg_alpha *= hud_fade_alpha * panel_fade_alpha;
248                 }
249                 panel.current_panel_pos = panel_pos;
250                 panel.current_panel_size = panel_size;
251                 panel.current_panel_bg_border = panel_bg_border;
252                 panel.current_panel_bg_color = panel_bg_color;
253                 panel.current_panel_bg_color_team = panel_bg_color_team;
254                 panel.current_panel_bg_padding = panel_bg_padding;
255                 panel.update_time = (autocvar__hud_configure) ? time : time + autocvar_hud_panel_update_interval;
256                 return;
257         }
258
259         panel_pos = panel.current_panel_pos;
260         panel_size = panel.current_panel_size;
261         panel_bg_alpha = panel.current_panel_bg_alpha * hud_fade_alpha * panel_fade_alpha;
262         panel_bg_border = panel.current_panel_bg_border;
263         panel_bg_color = panel.current_panel_bg_color;
264         panel_bg_color_team = panel.current_panel_bg_color_team;
265         panel_bg_padding = panel.current_panel_bg_padding;
266         panel_fg_alpha = panel.current_panel_fg_alpha * hud_fade_alpha * panel_fade_alpha;
267 }
268
269 //basically the same code of draw_ButtonPicture and draw_VertButtonPicture for the menu
270 void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
271 {
272         TC(bool, vertical); TC(int, drawflag);
273         if(!length_ratio || !theAlpha)
274                 return;
275         if(length_ratio > 1)
276                 length_ratio = 1;
277         if (baralign == 3)
278         {
279                 if(length_ratio < -1)
280                         length_ratio = -1;
281         }
282         else if(length_ratio < 0)
283                 return;
284
285         theOrigin = HUD_Shift(theOrigin);
286         theSize = HUD_Scale(theSize);
287
288         vector square;
289         vector width, height;
290         if(vertical) {
291                 pic = strcat(hud_skin_path, "/", pic, "_vertical");
292                 if(precache_pic(pic) == "") {
293                         pic = "gfx/hud/default/progressbar_vertical";
294                 }
295
296                 if (baralign == 1) // bottom align
297                         theOrigin.y += (1 - length_ratio) * theSize.y;
298                 else if (baralign == 2) // center align
299                         theOrigin.y += 0.5 * (1 - length_ratio) * theSize.y;
300                 else if (baralign == 3) // center align, positive values down, negative up
301                 {
302                         theSize.y *= 0.5;
303                         if (length_ratio > 0)
304                                 theOrigin.y += theSize.y;
305                         else
306                         {
307                                 theOrigin.y += (1 + length_ratio) * theSize.y;
308                                 length_ratio = -length_ratio;
309                         }
310                 }
311                 theSize.y *= length_ratio;
312
313                 vector bH;
314                 width = eX * theSize.x;
315                 height = eY * theSize.y;
316                 if(theSize.y <= theSize.x * 2)
317                 {
318                         // button not high enough
319                         // draw just upper and lower part then
320                         square = eY * theSize.y * 0.5;
321                         bH = eY * (0.25 * theSize.y / (theSize.x * 2));
322                         drawsubpic(theOrigin,          square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, drawflag);
323                         drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, drawflag);
324                 }
325                 else
326                 {
327                         square = eY * theSize.x;
328                         drawsubpic(theOrigin,                   width   +     square, pic, '0 0    0', '1 0.25 0', theColor, theAlpha, drawflag);
329                         drawsubpic(theOrigin +          square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5  0', theColor, theAlpha, drawflag);
330                         drawsubpic(theOrigin + height - square, width   +     square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, drawflag);
331                 }
332         } else {
333                 pic = strcat(hud_skin_path, "/", pic);
334                 if(precache_pic(pic) == "") {
335                         pic = "gfx/hud/default/progressbar";
336                 }
337
338                 if (baralign == 1) // right align
339                         theOrigin.x += (1 - length_ratio) * theSize.x;
340                 else if (baralign == 2) // center align
341                         theOrigin.x += 0.5 * (1 - length_ratio) * theSize.x;
342                 else if (baralign == 3) // center align, positive values on the right, negative on the left
343                 {
344                         theSize.x *= 0.5;
345                         if (length_ratio > 0)
346                                 theOrigin.x += theSize.x;
347                         else
348                         {
349                                 theOrigin.x += (1 + length_ratio) * theSize.x;
350                                 length_ratio = -length_ratio;
351                         }
352                 }
353                 theSize.x *= length_ratio;
354
355                 vector bW;
356                 width = eX * theSize.x;
357                 height = eY * theSize.y;
358                 if(theSize.x <= theSize.y * 2)
359                 {
360                         // button not wide enough
361                         // draw just left and right part then
362                         square = eX * theSize.x * 0.5;
363                         bW = eX * (0.25 * theSize.x / (theSize.y * 2));
364                         drawsubpic(theOrigin,          square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, drawflag);
365                         drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, drawflag);
366                 }
367                 else
368                 {
369                         square = eX * theSize.y;
370                         drawsubpic(theOrigin,                  height  +     square, pic, '0    0 0', '0.25 1 0', theColor, theAlpha, drawflag);
371                         drawsubpic(theOrigin +         square, theSize - 2 * square, pic, '0.25 0 0', '0.5  1 0', theColor, theAlpha, drawflag);
372                         drawsubpic(theOrigin + width - square, height  +     square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, drawflag);
373                 }
374         }
375 }
376
377 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag)
378 {
379         TC(int, drawflag);
380         if(!theAlpha)
381                 return;
382
383         pos = HUD_Shift(pos);
384         mySize = HUD_Scale(mySize);
385
386         string pic;
387         pic = strcat(hud_skin_path, "/num_leading");
388         if(precache_pic(pic) == "") {
389                 pic = "gfx/hud/default/num_leading";
390         }
391
392         drawsubpic(pos, eX * min(mySize.x * 0.5, mySize.y) + eY * mySize.y, pic, '0 0 0', '0.25 1 0', color, theAlpha, drawflag);
393         if(mySize.x/mySize.y > 2)
394                 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);
395         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);
396 }
397
398 void DrawNumIcon_expanding(vector myPos, vector mySize, float theTime, string icon, bool vertical, bool isInfinite, int icon_right_align, vector color, float theAlpha, float fadelerp)
399 {
400         TC(bool, vertical); TC(int, icon_right_align);
401         vector newPos = '0 0 0', newSize = '0 0 0';
402         vector picpos, numpos;
403         string text = isInfinite ? "\xE2\x88\x9E" : ftos(theTime); // Use infinity symbol (U+221E)
404
405         if (vertical)
406         {
407                 if(mySize.y/mySize.x > 2)
408                 {
409                         newSize.y = 2 * mySize.x;
410                         newSize.x = mySize.x;
411
412                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
413                         newPos.x = myPos.x;
414                 }
415                 else
416                 {
417                         newSize.x = 1/2 * mySize.y;
418                         newSize.y = mySize.y;
419
420                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
421                         newPos.y = myPos.y;
422                 }
423
424                 if(icon_right_align)
425                 {
426                         numpos = newPos;
427                         picpos = newPos + eY * newSize.x;
428                 }
429                 else
430                 {
431                         picpos = newPos;
432                         numpos = newPos + eY * newSize.x;
433                 }
434
435                 newSize.y /= 2;
436                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
437                 // make number smaller than icon, it looks better
438                 // reduce only y to draw numbers with different number of digits with the same y size
439                 numpos.y += newSize.y * ((1 - 0.7) / 2);
440                 newSize.y *= 0.7;
441                 drawstring_aspect(numpos, text, newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
442                 return;
443         }
444
445         if(mySize.x/mySize.y > 3)
446         {
447                 newSize.x = 3 * mySize.y;
448                 newSize.y = mySize.y;
449
450                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
451                 newPos.y = myPos.y;
452         }
453         else
454         {
455                 newSize.y = 1/3 * mySize.x;
456                 newSize.x = mySize.x;
457
458                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
459                 newPos.x = myPos.x;
460         }
461
462         if(icon_right_align) // right align
463         {
464                 numpos = newPos;
465                 picpos = newPos + eX * 2 * newSize.y;
466         }
467         else // left align
468         {
469                 numpos = newPos + eX * newSize.y;
470                 picpos = newPos;
471         }
472
473         // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
474         // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
475         drawstring_aspect_expanding(numpos, text, '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
476         drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
477 }
478
479 void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, bool isInfinite, int icon_right_align, vector color, float theAlpha)
480 {
481         TC(bool, vertical); TC(int, icon_right_align);
482         DrawNumIcon_expanding(myPos, mySize, theTime, icon, vertical, isInfinite, icon_right_align, color, theAlpha, 0);
483 }
484
485 /*
486 ==================
487 Main HUD system
488 ==================
489 */
490
491 float lasthud;
492 float vh_notice_time;
493 void HUD_Vehicle()
494 {
495         if(autocvar__hud_configure) return;
496         if(intermission == 2) return;
497
498         if(hud == HUD_BUMBLEBEE_GUN)
499                 CSQC_BUMBLE_GUN_HUD();
500         else {
501                 Vehicle info = REGISTRY_GET(Vehicles, hud);
502                 info.vr_hud(info);
503         }
504
505         if(hud != HUD_NORMAL && lasthud == HUD_NORMAL)
506                 vh_notice_time = time + autocvar_cl_vehicles_notify_time;
507
508         lasthud = hud;
509 }
510
511 void HUD_Panel_Draw(entity panent)
512 {
513         panel = panent;
514         if (autocvar__hud_configure)
515         {
516                 if (!(panel.panel_configflags & PANEL_CONFIG_MAIN))
517                         return;
518                 panel_fade_alpha = 1;
519                 Hud_Panel_GetPanelEnabled();
520                 panel.panel_draw();
521                 return;
522         }
523
524         bool draw_allowed = false;
525         if (scoreboard_fade_alpha && panel.panel_showflags & PANEL_SHOW_WITH_SB)
526         {
527                 draw_allowed = true;
528         }
529         else if (active_minigame && HUD_MinigameMenu_IsOpened())
530         {
531                 if (panel.panel_showflags & PANEL_SHOW_MINIGAME)
532                         draw_allowed = true;
533         }
534         else if(intermission == 2)
535         {
536                 if(panel.panel_showflags & PANEL_SHOW_MAPVOTE)
537                         draw_allowed = true;
538         }
539         else if (panel.panel_showflags & PANEL_SHOW_MAINGAME)
540                 draw_allowed = true;
541
542         if (draw_allowed)
543         {
544                 if (panel.panel_showflags & PANEL_SHOW_WITH_SB)
545                 {
546                         if (scoreboard_fade_alpha && intermission == 2 && !(panel.panel_showflags & PANEL_SHOW_MAPVOTE))
547                                 panel_fade_alpha = scoreboard_fade_alpha;
548                         else
549                                 panel_fade_alpha = 1;
550                 }
551                 else
552                 {
553                         panel_fade_alpha = 1 - scoreboard_fade_alpha;
554                         if(!panel_fade_alpha)
555                                 return;
556                 }
557                 panel.panel_draw();
558         }
559 }
560
561 void HUD_Reset()
562 {
563         // reset gametype specific icons
564         if(gametype.m_modicons_reset)
565                 gametype.m_modicons_reset();
566 }
567
568 float autocvar_hud_dynamic_shake = 1;
569 float autocvar_hud_dynamic_shake_damage_max = 130;
570 float autocvar_hud_dynamic_shake_damage_min = 10;
571 float autocvar_hud_dynamic_shake_scale = 0.2;
572 float hud_dynamic_shake_x[10] = {0,    1, -0.7,  0.5, -0.3,  0.2, -0.1,  0.1,  0.0, 0};
573 float hud_dynamic_shake_y[10] = {0,  0.4,  0.8, -0.2, -0.6,  0.0,  0.3,  0.1, -0.1, 0};
574 bool Hud_Shake_Update()
575 {
576         if(time - hud_dynamic_shake_time < 0)
577                 return false;
578
579         float anim_speed = 17 + 9 * hud_dynamic_shake_factor;
580         float elapsed_time = (time - hud_dynamic_shake_time) * anim_speed;
581         int i = floor(elapsed_time);
582         if(i >= 9)
583                 return false;
584
585         float f = elapsed_time - i;
586         hud_dynamic_shake_realofs.x = (1 - f) * hud_dynamic_shake_x[i] + f * hud_dynamic_shake_x[i+1];
587         hud_dynamic_shake_realofs.y = (1 - f) * hud_dynamic_shake_y[i] + f * hud_dynamic_shake_y[i+1];
588         hud_dynamic_shake_realofs.z = 0;
589         hud_dynamic_shake_realofs *= hud_dynamic_shake_factor * autocvar_hud_dynamic_shake_scale;
590         hud_dynamic_shake_realofs.x = bound(-0.1, hud_dynamic_shake_realofs.x, 0.1) * vid_conwidth;
591         hud_dynamic_shake_realofs.y = bound(-0.1, hud_dynamic_shake_realofs.y, 0.1) * vid_conheight;
592         return true;
593 }
594
595 void Hud_Dynamic_Frame()
596 {
597         vector ofs = '0 0 0';
598         hud_scale_current = '1 1 0';
599         hud_shift_current = '0 0 0';
600
601         if (autocvar_hud_dynamic_follow)
602         {
603                 entity view = CSQCModel_server2csqc(player_localentnum - 1);
604                 calc_followmodel_ofs(view);
605                 ofs = -cl_followmodel_ofs * autocvar_hud_dynamic_follow_scale;
606                 ofs.x *= autocvar_hud_dynamic_follow_scale_xyz.z;
607                 ofs.y *= autocvar_hud_dynamic_follow_scale_xyz.x;
608                 ofs.z *= autocvar_hud_dynamic_follow_scale_xyz.y;
609
610                 if (fabs(ofs.x) < 0.001) ofs.x = 0;
611                 if (fabs(ofs.y) < 0.001) ofs.y = 0;
612                 if (fabs(ofs.z) < 0.001) ofs.z = 0;
613                 ofs.x = bound(-0.1, ofs.x, 0.1);
614                 ofs.y = bound(-0.1, ofs.y, 0.1);
615                 ofs.z = bound(-0.1, ofs.z, 0.1);
616
617                 hud_shift_current.x = ofs.y * vid_conwidth;
618                 hud_shift_current.y = ofs.z * vid_conheight;
619                 hud_shift_current.z = ofs.x;
620
621                 hud_scale_current.x = (1 + hud_shift_current.z);
622                 hud_scale_current.y = hud_scale_current.x;
623         }
624
625         if(autocvar_hud_dynamic_shake > 0)
626         {
627                 static float old_health = 0;
628                 float health = max(-1, STAT(HEALTH));
629                 if(hud_dynamic_shake_factor == -1) // don't allow the effect for this frame
630                 {
631                         hud_dynamic_shake_factor = 0;
632                         old_health = health;
633                 }
634                 else
635                 {
636                         float new_hud_dynamic_shake_factor = 0;
637                         if (old_health - health >= autocvar_hud_dynamic_shake_damage_min
638                                 && autocvar_hud_dynamic_shake_damage_max > autocvar_hud_dynamic_shake_damage_min
639                                 && old_health > 0 && !intermission)
640                         {
641                                 float m = max(autocvar_hud_dynamic_shake_damage_min, 1);
642                                 new_hud_dynamic_shake_factor = (old_health - health - m) / (autocvar_hud_dynamic_shake_damage_max - m);
643                                 if(new_hud_dynamic_shake_factor >= 1)
644                                         new_hud_dynamic_shake_factor = 1;
645                                 if(new_hud_dynamic_shake_factor >= hud_dynamic_shake_factor)
646                                 {
647                                         hud_dynamic_shake_factor = new_hud_dynamic_shake_factor;
648                                         hud_dynamic_shake_time = time;
649                                 }
650                         }
651                         old_health = health;
652                         if(hud_dynamic_shake_factor)
653                                 if(!Hud_Shake_Update())
654                                         hud_dynamic_shake_factor = 0;
655                 }
656
657                 if(hud_dynamic_shake_factor > 0)
658                 {
659                         hud_shift_current.x += hud_dynamic_shake_realofs.x;
660                         hud_shift_current.y += hud_dynamic_shake_realofs.y;
661                 }
662         }
663
664         hud_scale_center.x = 0.5 * vid_conwidth;
665         hud_scale_center.y = 0.5 * vid_conheight;
666
667         HUD_Scale_Disable();
668 }
669
670 bool HUD_WouldShowCursor()
671 {
672         if(autocvar__hud_configure)
673                 return true;
674         if(mv_active)
675                 return true;
676         //entity local_player = ((csqcplayer) ? csqcplayer : CSQCModel_server2csqc(player_localentnum - 1)); // TODO: doesn't use regular cursor handling
677         //if(local_player.viewloc && (local_player.viewloc.spawnflags & VIEWLOC_FREEAIM))
678                 //return true;
679         if(HUD_Radar_Clickable())
680                 return true;
681         if(HUD_MinigameMenu_IsOpened())
682                 return true;
683         if(QuickMenu_IsOpened())
684                 return true;
685         return false;
686 }
687
688 float prev_myteam;
689 void HUD_Main()
690 {
691         int i;
692         if(hud_configure_menu_open == 1)
693                 hud_fade_alpha = 1;
694         else
695                 hud_fade_alpha = 1 - autocvar__menu_alpha;
696
697         if(myteam != prev_myteam)
698         {
699                 myteamcolors = colormapPaletteColor(myteam, 1);
700                 FOREACH(hud_panels, true, it.update_time = time);
701                 prev_myteam = myteam;
702         }
703
704         HUD_Configure_Frame();
705
706         if(scoreboard_fade_alpha == 1)
707                 if(autocvar__menu_alpha == 1)
708                         return;
709
710         // Drawing stuff
711         if (hud_skin_prev != autocvar_hud_skin)
712         {
713                 strcpy(hud_skin_path, strcat("gfx/hud/", autocvar_hud_skin));
714                 strcpy(hud_skin_prev, autocvar_hud_skin);
715         }
716
717         // draw the dock
718         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
719         {
720                 int f;
721                 vector color;
722                 float hud_dock_color_team = autocvar_hud_dock_color_team;
723                 if((teamplay) && hud_dock_color_team) {
724                         if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
725                                 color = '1 0 0' * hud_dock_color_team;
726                         else
727                                 color = myteamcolors * hud_dock_color_team;
728                 }
729                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
730                         color = '1 0 0' * hud_dock_color_team;
731                 }
732                 else
733                 {
734                         string hud_dock_color = autocvar_hud_dock_color;
735                         if(hud_dock_color == "shirt") {
736                                 f = entcs_GetClientColors(current_player);
737                                 color = colormapPaletteColor(floor(f / 16), 0);
738                         }
739                         else if(hud_dock_color == "pants") {
740                                 f = entcs_GetClientColors(current_player);
741                                 color = colormapPaletteColor(f % 16, 1);
742                         }
743                         else
744                                 color = stov(hud_dock_color);
745                 }
746
747                 string pic;
748                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
749                 if(precache_pic(pic) == "") {
750                         pic = strcat(hud_skin_path, "/dock_medium");
751                         if(precache_pic(pic) == "") {
752                                 pic = "gfx/hud/default/dock_medium";
753                         }
754                 }
755                 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...
756         }
757
758         // cache the panel order into the panel_order array
759         if(autocvar__hud_panelorder != hud_panelorder_prev) {
760                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i)
761                         panel_order[i] = -1;
762                 string s = "";
763                 int p_num;
764                 bool warning = false;
765                 int argc = tokenize_console(autocvar__hud_panelorder);
766                 if (argc > REGISTRY_COUNT(hud_panels))
767                         warning = true;
768                 //first detect wrong/missing panel numbers
769                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
770                         p_num = stoi(argv(i));
771                         if (p_num >= 0 && p_num < REGISTRY_COUNT(hud_panels)) { //correct panel number?
772                                 if (panel_order[p_num] == -1) //found for the first time?
773                                         s = strcat(s, ftos(p_num), " ");
774                                 panel_order[p_num] = 1; //mark as found
775                         }
776                         else
777                                 warning = true;
778                 }
779                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
780                         if (panel_order[i] == -1) {
781                                 warning = true;
782                                 s = strcat(s, ftos(i), " "); //add missing panel number
783                         }
784                 }
785                 if (warning)
786                         LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder");
787
788                 cvar_set("_hud_panelorder", s);
789                 strcpy(hud_panelorder_prev, s);
790
791                 //now properly set panel_order
792                 tokenize_console(s);
793                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
794                         panel_order[i] = stof(argv(i));
795                 }
796         }
797
798         hud_draw_maximized = 0;
799         // draw panels in the order specified by panel_order array
800         for(i = REGISTRY_COUNT(hud_panels) - 1; i >= 0; --i)
801                 HUD_Panel_Draw(REGISTRY_GET(hud_panels, panel_order[i]));
802
803         HUD_Vehicle();
804
805         hud_draw_maximized = 1; // panels that may be maximized must check this var
806         // draw maximized panels on top
807         if(hud_panel_radar_maximized)
808                 HUD_Panel_Draw(HUD_PANEL(RADAR));
809         if(autocvar__con_chat_maximized)
810                 HUD_Panel_Draw(HUD_PANEL(CHAT));
811         if (QuickMenu_IsOpened())
812                 HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
813         HUD_Panel_Draw(HUD_PANEL(SCOREBOARD));
814
815         int cursor_active_prev = cursor_active;
816         cursor_active = HUD_WouldShowCursor();
817         if (cursor_active_prev != cursor_active && autocvar_hud_cursormode)
818         {
819                 setcursormode(cursor_active);
820                 // cursor inactive this frame, will be set to 1 the next frame
821                 if (cursor_active)
822                         cursor_active = -1;
823         }
824
825         if (intermission == 2)
826                 HUD_Reset();
827
828         HUD_Configure_PostDraw();
829
830         hud_configure_prev = autocvar__hud_configure;
831 }