]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/timer.qc
Display untimed Warmup or min players requirement (g_warmup -1) in HUD timer and...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / timer.qc
1 #include "timer.qh"
2 #include "scoreboard.qh"
3
4 #include <client/draw.qh>
5 #include <client/view.qh>
6
7 // Timer (#5)
8
9 void HUD_Timer_Export(int fh)
10 {
11         // allow saving cvars that aesthetically change the panel into hud skin files
12 }
13
14 vector HUD_Timer_Color(float timeleft)
15 {
16         if(timeleft <= 60)
17                 return '1 0 0'; // red
18         else if(timeleft <= 300)
19                 return '1 1 0'; // yellow
20         else
21                 return '1 1 1'; // white
22 }
23
24 float HUD_Timer_TimeElapsed(float curtime, float starttime)
25 {
26         float time_elapsed = curtime - starttime;
27         if (!autocvar_hud_panel_timer_unbound)
28                 time_elapsed = max(0, time_elapsed);
29         return floor(time_elapsed);
30 }
31
32 float HUD_Timer_TimeLeft(float curtime, float starttime, float timelimit)
33 {
34         float timeleft = timelimit + starttime - curtime;
35         if (!autocvar_hud_panel_timer_unbound)
36                 timeleft = bound(0, timeleft, timelimit);
37         return ceil(timeleft);
38 }
39
40 void HUD_Timer()
41 {
42         if(!autocvar__hud_configure)
43         {
44                 if(!autocvar_hud_panel_timer) return;
45         }
46
47         HUD_Panel_LoadCvars();
48
49         draw_beginBoldFont();
50
51         vector pos, mySize;
52         pos = panel_pos;
53         mySize = panel_size;
54
55         if (autocvar_hud_panel_timer_dynamichud)
56                 HUD_Scale_Enable();
57         else
58                 HUD_Scale_Disable();
59         if(panel_bg_padding)
60         {
61                 pos += '1 1 0' * panel_bg_padding;
62                 mySize -= '2 2 0' * panel_bg_padding;
63         }
64
65         string timer;
66         string subtimer = string_null;
67         string subtext = string_null;
68         float curtime, timelimit, timeleft;
69         vector timer_size, subtext_size, subtimer_size;
70         vector timer_color = '1 1 1';
71         vector subtimer_color = '1 1 1';
72         bool swap = (autocvar_hud_panel_timer_secondary == 2 && STAT(ROUNDSTARTTIME));
73
74         // Use real or frozen time and get the time limit
75         curtime = (intermission_time ? intermission_time : time);
76         if(warmup_stage)
77                 timelimit = STAT(WARMUP_TIMELIMIT);
78         else
79                 timelimit = STAT(TIMELIMIT) * 60;
80
81         // Calculate time left
82         timeleft = HUD_Timer_TimeLeft(curtime, STAT(GAMESTARTTIME), timelimit);
83
84         // Timer color
85         if(!intermission_time && !warmup_stage && timelimit > 0)
86                 timer_color = HUD_Timer_Color(timeleft);
87
88         // Timer text
89         if (autocvar_hud_panel_timer_increment || timelimit <= 0)
90                 timer = seconds_tostring(HUD_Timer_TimeElapsed(curtime, STAT(GAMESTARTTIME)));
91         else
92                 timer = seconds_tostring(timeleft);
93
94         // Secondary timer for round-based game modes
95         if(STAT(ROUNDSTARTTIME) && autocvar_hud_panel_timer_secondary)
96         {
97                 if(STAT(ROUNDSTARTTIME) == -1) {
98                         // Round can't start
99                         subtimer = "--:--";
100                         subtimer_color = '1 0 0';
101                 } else {
102                         float round_curtime, round_timelimit, round_timeleft;
103
104                         // Use real or frozen time and get the time limit
105                         round_curtime = (game_stopped_time ? game_stopped_time : time);
106                         round_timelimit = STAT(ROUND_TIMELIMIT);
107
108                         // Calculate time left
109                         round_timeleft = HUD_Timer_TimeLeft(round_curtime, STAT(ROUNDSTARTTIME), round_timelimit);
110
111                         // Subtimer color
112                         if(!intermission_time && round_timelimit > 0)
113                                 subtimer_color = HUD_Timer_Color(round_timeleft);
114
115                         // Subtimer text
116                         if (autocvar_hud_panel_timer_increment || round_timelimit <= 0)
117                                 subtimer = seconds_tostring(HUD_Timer_TimeElapsed(round_curtime, STAT(ROUNDSTARTTIME)));
118                         else
119                                 subtimer = seconds_tostring(round_timeleft);
120                 }
121         }
122
123         // Subtext
124         int overtimes = STAT(OVERTIMES);
125
126         if(warmup_stage || autocvar__hud_configure)
127         {
128                 if (STAT(WARMUP_TIMELIMIT) > 0)
129                         subtext = _("Warmup");
130                 else
131                         subtext = srv_minplayers ? _("Warmup: too few players") : _("Warmup: no time limit");
132         }
133         else if(STAT(TIMEOUT_STATUS) == 2)
134                 subtext = _("Timeout");
135         else if (overtimes == -1)
136                 subtext = _("Sudden Death");
137         else if(overtimes == 1)
138                 subtext = _("Overtime");
139         else if (overtimes >= 2)
140                 subtext = sprintf(_("Overtime #%d"), overtimes);
141
142         subtext_size  = vec2(mySize.x, mySize.y / 3);
143         timer_size    = vec2(mySize.x, mySize.y - subtext_size.y);
144         subtimer_size = vec2(mySize.x / 3, mySize.y - subtext_size.y);
145
146         panel_size.y -= subtext_size.y;
147         HUD_Panel_DrawBg();
148
149         if(subtimer) {
150                 float subtimer_padding = subtimer_size.y / 5;
151                 timer_size.x -= subtimer_size.x;
152                 drawstring_aspect(pos + eX * timer_size.x + eY * subtimer_padding, (swap ? timer : subtimer), subtimer_size - eY * subtimer_padding * 2, (swap ? timer_color : subtimer_color), panel_fg_alpha, DRAWFLAG_NORMAL);
153         }
154
155         drawstring_aspect(pos, (swap ? subtimer : timer), timer_size, (swap ? subtimer_color : timer_color), panel_fg_alpha, DRAWFLAG_NORMAL);
156
157         if(subtext)
158                 drawstring_aspect(pos + eY * timer_size.y, subtext, subtext_size, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
159
160         draw_endBoldFont();
161 }