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