]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/hud.qc
Purge autocvars.qh from the client-side codebase, cvars are defined in the headers...
[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, 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
404         if (vertical)
405         {
406                 if(mySize.y/mySize.x > 2)
407                 {
408                         newSize.y = 2 * mySize.x;
409                         newSize.x = mySize.x;
410
411                         newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
412                         newPos.x = myPos.x;
413                 }
414                 else
415                 {
416                         newSize.x = 1/2 * mySize.y;
417                         newSize.y = mySize.y;
418
419                         newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
420                         newPos.y = myPos.y;
421                 }
422
423                 if(icon_right_align)
424                 {
425                         numpos = newPos;
426                         picpos = newPos + eY * newSize.x;
427                 }
428                 else
429                 {
430                         picpos = newPos;
431                         numpos = newPos + eY * newSize.x;
432                 }
433
434                 newSize.y /= 2;
435                 drawpic_aspect_skin(picpos, icon, newSize, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
436                 // make number smaller than icon, it looks better
437                 // reduce only y to draw numbers with different number of digits with the same y size
438                 numpos.y += newSize.y * ((1 - 0.7) / 2);
439                 newSize.y *= 0.7;
440                 drawstring_aspect(numpos, ftos(theTime), newSize, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL);
441                 return;
442         }
443
444         if(mySize.x/mySize.y > 3)
445         {
446                 newSize.x = 3 * mySize.y;
447                 newSize.y = mySize.y;
448
449                 newPos.x = myPos.x + (mySize.x - newSize.x) / 2;
450                 newPos.y = myPos.y;
451         }
452         else
453         {
454                 newSize.y = 1/3 * mySize.x;
455                 newSize.x = mySize.x;
456
457                 newPos.y = myPos.y + (mySize.y - newSize.y) / 2;
458                 newPos.x = myPos.x;
459         }
460
461         if(icon_right_align) // right align
462         {
463                 numpos = newPos;
464                 picpos = newPos + eX * 2 * newSize.y;
465         }
466         else // left align
467         {
468                 numpos = newPos + eX * newSize.y;
469                 picpos = newPos;
470         }
471
472         // NOTE: newSize_x is always equal to 3 * mySize_y so we can use
473         // '2 1 0' * newSize_y instead of eX * (2/3) * newSize_x + eY * newSize_y
474         drawstring_aspect_expanding(numpos, ftos(theTime), '2 1 0' * newSize.y, color, panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
475         drawpic_aspect_skin_expanding(picpos, icon, '1 1 0' * newSize.y, '1 1 1', panel_fg_alpha * theAlpha, DRAWFLAG_NORMAL, fadelerp);
476 }
477
478 void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha)
479 {
480         TC(bool, vertical); TC(int, icon_right_align);
481         DrawNumIcon_expanding(myPos, mySize, theTime, icon, vertical, icon_right_align, color, theAlpha, 0);
482 }
483
484 /*
485 ==================
486 Main HUD system
487 ==================
488 */
489
490 float lasthud;
491 float vh_notice_time;
492 void HUD_Vehicle()
493 {
494         if(autocvar__hud_configure) return;
495         if(intermission == 2) return;
496
497         if(hud == HUD_BUMBLEBEE_GUN)
498                 CSQC_BUMBLE_GUN_HUD();
499         else {
500                 Vehicle info = REGISTRY_GET(Vehicles, hud);
501                 info.vr_hud(info);
502         }
503
504         if(hud != HUD_NORMAL && lasthud == HUD_NORMAL)
505                 vh_notice_time = time + autocvar_cl_vehicles_notify_time;
506
507         lasthud = hud;
508 }
509
510 void HUD_Panel_Draw(entity panent)
511 {
512         panel = panent;
513         if (autocvar__hud_configure)
514         {
515                 if (!(panel.panel_configflags & PANEL_CONFIG_MAIN))
516                         return;
517                 panel_fade_alpha = 1;
518                 Hud_Panel_GetPanelEnabled();
519                 panel.panel_draw();
520                 return;
521         }
522
523         bool draw_allowed = false;
524         if (scoreboard_fade_alpha && panel.panel_showflags & PANEL_SHOW_WITH_SB)
525         {
526                 draw_allowed = true;
527         }
528         else if (active_minigame && HUD_MinigameMenu_IsOpened())
529         {
530                 if (panel.panel_showflags & PANEL_SHOW_MINIGAME)
531                         draw_allowed = true;
532         }
533         else if(intermission == 2)
534         {
535                 if(panel.panel_showflags & PANEL_SHOW_MAPVOTE)
536                         draw_allowed = true;
537         }
538         else if (panel.panel_showflags & PANEL_SHOW_MAINGAME)
539                 draw_allowed = true;
540
541         if (draw_allowed)
542         {
543                 if (panel.panel_showflags & PANEL_SHOW_WITH_SB)
544                 {
545                         if (scoreboard_fade_alpha && intermission == 2 && !(panel.panel_showflags & PANEL_SHOW_MAPVOTE))
546                                 panel_fade_alpha = scoreboard_fade_alpha;
547                         else
548                                 panel_fade_alpha = 1;
549                 }
550                 else
551                 {
552                         panel_fade_alpha = 1 - scoreboard_fade_alpha;
553                         if(!panel_fade_alpha)
554                                 return;
555                 }
556                 panel.panel_draw();
557         }
558 }
559
560 void HUD_Reset()
561 {
562         // reset gametype specific icons
563         if(gametype.m_modicons_reset)
564                 gametype.m_modicons_reset();
565 }
566
567 float autocvar_hud_dynamic_shake = 1;
568 float autocvar_hud_dynamic_shake_damage_max = 130;
569 float autocvar_hud_dynamic_shake_damage_min = 10;
570 float autocvar_hud_dynamic_shake_scale = 0.2;
571 float hud_dynamic_shake_x[10] = {0,    1, -0.7,  0.5, -0.3,  0.2, -0.1,  0.1,  0.0, 0};
572 float hud_dynamic_shake_y[10] = {0,  0.4,  0.8, -0.2, -0.6,  0.0,  0.3,  0.1, -0.1, 0};
573 bool Hud_Shake_Update()
574 {
575         if(time - hud_dynamic_shake_time < 0)
576                 return false;
577
578         float anim_speed = 17 + 9 * hud_dynamic_shake_factor;
579         float elapsed_time = (time - hud_dynamic_shake_time) * anim_speed;
580         int i = floor(elapsed_time);
581         if(i >= 9)
582                 return false;
583
584         float f = elapsed_time - i;
585         hud_dynamic_shake_realofs.x = (1 - f) * hud_dynamic_shake_x[i] + f * hud_dynamic_shake_x[i+1];
586         hud_dynamic_shake_realofs.y = (1 - f) * hud_dynamic_shake_y[i] + f * hud_dynamic_shake_y[i+1];
587         hud_dynamic_shake_realofs.z = 0;
588         hud_dynamic_shake_realofs *= hud_dynamic_shake_factor * autocvar_hud_dynamic_shake_scale;
589         hud_dynamic_shake_realofs.x = bound(-0.1, hud_dynamic_shake_realofs.x, 0.1) * vid_conwidth;
590         hud_dynamic_shake_realofs.y = bound(-0.1, hud_dynamic_shake_realofs.y, 0.1) * vid_conheight;
591         return true;
592 }
593
594 void Hud_Dynamic_Frame()
595 {
596         vector ofs = '0 0 0';
597         hud_scale_current = '1 1 0';
598         hud_shift_current = '0 0 0';
599
600         if (autocvar_hud_dynamic_follow)
601         {
602                 entity view = CSQCModel_server2csqc(player_localentnum - 1);
603                 calc_followmodel_ofs(view);
604                 ofs = -cl_followmodel_ofs * autocvar_hud_dynamic_follow_scale;
605                 ofs.x *= autocvar_hud_dynamic_follow_scale_xyz.z;
606                 ofs.y *= autocvar_hud_dynamic_follow_scale_xyz.x;
607                 ofs.z *= autocvar_hud_dynamic_follow_scale_xyz.y;
608
609                 if (fabs(ofs.x) < 0.001) ofs.x = 0;
610                 if (fabs(ofs.y) < 0.001) ofs.y = 0;
611                 if (fabs(ofs.z) < 0.001) ofs.z = 0;
612                 ofs.x = bound(-0.1, ofs.x, 0.1);
613                 ofs.y = bound(-0.1, ofs.y, 0.1);
614                 ofs.z = bound(-0.1, ofs.z, 0.1);
615
616                 hud_shift_current.x = ofs.y * vid_conwidth;
617                 hud_shift_current.y = ofs.z * vid_conheight;
618                 hud_shift_current.z = ofs.x;
619
620                 hud_scale_current.x = (1 + hud_shift_current.z);
621                 hud_scale_current.y = hud_scale_current.x;
622         }
623
624         if(autocvar_hud_dynamic_shake > 0)
625         {
626                 static float old_health = 0;
627                 float health = max(-1, STAT(HEALTH));
628                 if(hud_dynamic_shake_factor == -1) // don't allow the effect for this frame
629                 {
630                         hud_dynamic_shake_factor = 0;
631                         old_health = health;
632                 }
633                 else
634                 {
635                         float new_hud_dynamic_shake_factor = 0;
636                         if (old_health - health >= autocvar_hud_dynamic_shake_damage_min
637                                 && autocvar_hud_dynamic_shake_damage_max > autocvar_hud_dynamic_shake_damage_min
638                                 && old_health > 0 && !intermission)
639                         {
640                                 float m = max(autocvar_hud_dynamic_shake_damage_min, 1);
641                                 new_hud_dynamic_shake_factor = (old_health - health - m) / (autocvar_hud_dynamic_shake_damage_max - m);
642                                 if(new_hud_dynamic_shake_factor >= 1)
643                                         new_hud_dynamic_shake_factor = 1;
644                                 if(new_hud_dynamic_shake_factor >= hud_dynamic_shake_factor)
645                                 {
646                                         hud_dynamic_shake_factor = new_hud_dynamic_shake_factor;
647                                         hud_dynamic_shake_time = time;
648                                 }
649                         }
650                         old_health = health;
651                         if(hud_dynamic_shake_factor)
652                                 if(!Hud_Shake_Update())
653                                         hud_dynamic_shake_factor = 0;
654                 }
655
656                 if(hud_dynamic_shake_factor > 0)
657                 {
658                         hud_shift_current.x += hud_dynamic_shake_realofs.x;
659                         hud_shift_current.y += hud_dynamic_shake_realofs.y;
660                 }
661         }
662
663         hud_scale_center.x = 0.5 * vid_conwidth;
664         hud_scale_center.y = 0.5 * vid_conheight;
665
666         HUD_Scale_Disable();
667 }
668
669 bool HUD_WouldShowCursor()
670 {
671         if(autocvar__hud_configure)
672                 return true;
673         if(mv_active)
674                 return true;
675         //entity local_player = ((csqcplayer) ? csqcplayer : CSQCModel_server2csqc(player_localentnum - 1)); // TODO: doesn't use regular cursor handling
676         //if(local_player.viewloc && (local_player.viewloc.spawnflags & VIEWLOC_FREEAIM))
677                 //return true;
678         if(HUD_Radar_Clickable())
679                 return true;
680         if(HUD_MinigameMenu_IsOpened())
681                 return true;
682         if(QuickMenu_IsOpened())
683                 return true;
684         return false;
685 }
686
687 float prev_myteam;
688 void HUD_Main()
689 {
690         int i;
691         if(hud_configure_menu_open == 1)
692                 hud_fade_alpha = 1;
693         else
694                 hud_fade_alpha = 1 - autocvar__menu_alpha;
695
696         if(myteam != prev_myteam)
697         {
698                 myteamcolors = colormapPaletteColor(myteam, 1);
699                 FOREACH(hud_panels, true, it.update_time = time);
700                 prev_myteam = myteam;
701         }
702
703         HUD_Configure_Frame();
704
705         if(scoreboard_fade_alpha == 1)
706                 if(autocvar__menu_alpha == 1)
707                         return;
708
709         // Drawing stuff
710         if (hud_skin_prev != autocvar_hud_skin)
711         {
712                 strcpy(hud_skin_path, strcat("gfx/hud/", autocvar_hud_skin));
713                 strcpy(hud_skin_prev, autocvar_hud_skin);
714         }
715
716         // draw the dock
717         if(autocvar_hud_dock != "" && autocvar_hud_dock != "0")
718         {
719                 int f;
720                 vector color;
721                 float hud_dock_color_team = autocvar_hud_dock_color_team;
722                 if((teamplay) && hud_dock_color_team) {
723                         if(autocvar__hud_configure && myteam == NUM_SPECTATOR)
724                                 color = '1 0 0' * hud_dock_color_team;
725                         else
726                                 color = myteamcolors * hud_dock_color_team;
727                 }
728                 else if(autocvar_hud_configure_teamcolorforced && autocvar__hud_configure && hud_dock_color_team) {
729                         color = '1 0 0' * hud_dock_color_team;
730                 }
731                 else
732                 {
733                         string hud_dock_color = autocvar_hud_dock_color;
734                         if(hud_dock_color == "shirt") {
735                                 f = entcs_GetClientColors(current_player);
736                                 color = colormapPaletteColor(floor(f / 16), 0);
737                         }
738                         else if(hud_dock_color == "pants") {
739                                 f = entcs_GetClientColors(current_player);
740                                 color = colormapPaletteColor(f % 16, 1);
741                         }
742                         else
743                                 color = stov(hud_dock_color);
744                 }
745
746                 string pic;
747                 pic = strcat(hud_skin_path, "/", autocvar_hud_dock);
748                 if(precache_pic(pic) == "") {
749                         pic = strcat(hud_skin_path, "/dock_medium");
750                         if(precache_pic(pic) == "") {
751                                 pic = "gfx/hud/default/dock_medium";
752                         }
753                 }
754                 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...
755         }
756
757         // cache the panel order into the panel_order array
758         if(autocvar__hud_panelorder != hud_panelorder_prev) {
759                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i)
760                         panel_order[i] = -1;
761                 string s = "";
762                 int p_num;
763                 bool warning = false;
764                 int argc = tokenize_console(autocvar__hud_panelorder);
765                 if (argc > REGISTRY_COUNT(hud_panels))
766                         warning = true;
767                 //first detect wrong/missing panel numbers
768                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
769                         p_num = stoi(argv(i));
770                         if (p_num >= 0 && p_num < REGISTRY_COUNT(hud_panels)) { //correct panel number?
771                                 if (panel_order[p_num] == -1) //found for the first time?
772                                         s = strcat(s, ftos(p_num), " ");
773                                 panel_order[p_num] = 1; //mark as found
774                         }
775                         else
776                                 warning = true;
777                 }
778                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
779                         if (panel_order[i] == -1) {
780                                 warning = true;
781                                 s = strcat(s, ftos(i), " "); //add missing panel number
782                         }
783                 }
784                 if (warning)
785                         LOG_TRACE("Automatically fixed wrong/missing panel numbers in _hud_panelorder");
786
787                 cvar_set("_hud_panelorder", s);
788                 strcpy(hud_panelorder_prev, s);
789
790                 //now properly set panel_order
791                 tokenize_console(s);
792                 for(i = 0; i < REGISTRY_COUNT(hud_panels); ++i) {
793                         panel_order[i] = stof(argv(i));
794                 }
795         }
796
797         hud_draw_maximized = 0;
798         // draw panels in the order specified by panel_order array
799         for(i = REGISTRY_COUNT(hud_panels) - 1; i >= 0; --i)
800                 HUD_Panel_Draw(REGISTRY_GET(hud_panels, panel_order[i]));
801
802         HUD_Vehicle();
803
804         hud_draw_maximized = 1; // panels that may be maximized must check this var
805         // draw maximized panels on top
806         if(hud_panel_radar_maximized)
807                 HUD_Panel_Draw(HUD_PANEL(RADAR));
808         if(autocvar__con_chat_maximized)
809                 HUD_Panel_Draw(HUD_PANEL(CHAT));
810         if (QuickMenu_IsOpened())
811                 HUD_Panel_Draw(HUD_PANEL(QUICKMENU));
812         HUD_Panel_Draw(HUD_PANEL(SCOREBOARD));
813
814         int cursor_active_prev = cursor_active;
815         cursor_active = HUD_WouldShowCursor();
816         if (cursor_active_prev != cursor_active && autocvar_hud_cursormode)
817         {
818                 setcursormode(cursor_active);
819                 // cursor inactive this frame, will be set to 1 the next frame
820                 if (cursor_active)
821                         cursor_active = -1;
822         }
823
824         if (intermission == 2)
825                 HUD_Reset();
826
827         HUD_Configure_PostDraw();
828
829         hud_configure_prev = autocvar__hud_configure;
830 }