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