]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/timer.qc
Merge branch 'master' into z411/bai-server
[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 float last_timeleft;
9 int autocvar_cl_timer_countdown = 3; // 0 = disabled, 1 = always on, 2 = only spec, 3 = as dictated by server
10
11 void HUD_Timer_Export(int fh)
12 {
13         // allow saving cvars that aesthetically change the panel into hud skin files
14 }
15
16 vector HUD_Timer_Color(float timeleft)
17 {
18         if(timeleft <= 60)
19                 return '1 0 0'; // red
20         else if(timeleft <= 300)
21                 return '1 1 0'; // yellow
22         else
23                 return '1 1 1'; // white
24 }
25
26 float HUD_Timer_TimeElapsed(float curtime, float starttime)
27 {
28         float time_elapsed = curtime - starttime;
29         if (!autocvar_hud_panel_timer_unbound)
30                 time_elapsed = max(0, time_elapsed);
31         return floor(time_elapsed);
32 }
33
34 float HUD_Timer_TimeLeft(float curtime, float starttime, float timelimit)
35 {
36         float timeleft = timelimit + starttime - curtime;
37         if (!autocvar_hud_panel_timer_unbound)
38                 timeleft = bound(0, timeleft, timelimit);
39         return ceil(timeleft);
40 }
41
42 void HUD_Timer()
43 {
44         if(!autocvar__hud_configure)
45         {
46                 if(!autocvar_hud_panel_timer) return;
47         }
48
49         HUD_Panel_LoadCvars();
50
51         draw_beginBoldFont();
52
53         vector pos, mySize;
54         pos = panel_pos;
55         mySize = panel_size;
56
57         if (autocvar_hud_panel_timer_dynamichud)
58                 HUD_Scale_Enable();
59         else
60                 HUD_Scale_Disable();
61         if(panel_bg_padding)
62         {
63                 pos += '1 1 0' * panel_bg_padding;
64                 mySize -= '2 2 0' * panel_bg_padding;
65         }
66
67         string timer;
68         string subtimer = string_null;
69         string subtext = string_null;
70         float curtime, timelimit, timeleft;
71         vector timer_size, subtext_size, subtimer_size;
72         vector timer_color = '1 1 1';
73         vector subtimer_color = '1 1 1';
74         bool swap = (autocvar_hud_panel_timer_secondary == 2 && STAT(ROUNDSTARTTIME));
75         float timeout_last = STAT(TIMEOUT_LAST);
76
77         // Use real or frozen time and get the time limit
78         if(intermission_time)
79                 curtime = intermission_time;
80         else if(timeout_last)
81                 curtime = timeout_last;
82         else
83                 curtime = time;
84
85         if(warmup_stage)
86                 timelimit = STAT(WARMUP_TIMELIMIT);
87         else
88                 timelimit = STAT(TIMELIMIT) * 60;
89
90         // Calculate time left
91         timeleft = HUD_Timer_TimeLeft(curtime, STAT(GAMESTARTTIME), timelimit);
92
93         // Timer color
94         if(!intermission_time && !warmup_stage && timelimit > 0)
95                 timer_color = HUD_Timer_Color(timeleft);
96
97         // countdown sound
98         // if 3 use server dictated option, otherwise the client's
99         int countdown_type;
100         if(autocvar_cl_timer_countdown == 3)
101                 countdown_type = sv_timer_countdown;
102         else
103                 countdown_type = autocvar_cl_timer_countdown;
104         
105         if(countdown_type && !warmup_stage && timeleft > 0 && timeleft != last_timeleft && timeleft <= 10 && !intermission_time)
106         {
107                 if(countdown_type == 1 || (countdown_type == 2 && spectatee_status))
108                         sound(NULL, CH_INFO, SND_ENDCOUNT, VOL_BASE, ATTN_NONE);
109                 
110                 last_timeleft = timeleft;
111         }
112
113         // Timer text
114         if (warmup_stage && timelimit == -1)
115         {
116                 // Lock timer in infinite warmup
117                 if (autocvar_hud_panel_timer_increment)
118                         timer = seconds_tostring(0);
119                 else
120                         timer = seconds_tostring(STAT(TIMELIMIT) * 60);
121         }
122         else
123         {
124                 if (autocvar_hud_panel_timer_increment || timelimit <= 0)
125                         timer = seconds_tostring(HUD_Timer_TimeElapsed(curtime, STAT(GAMESTARTTIME)));
126                 else
127                         timer = seconds_tostring(timeleft);
128         }
129
130         // Secondary timer for round-based game modes
131         if(STAT(ROUNDSTARTTIME) && autocvar_hud_panel_timer_secondary)
132         {
133                 if(STAT(ROUNDSTARTTIME) == -1) {
134                         // Round can't start
135                         subtimer = "--:--";
136                         subtimer_color = '1 0 0';
137                 } else {
138                         float round_curtime, round_endtime, round_timelimit, round_timeleft;
139
140                         // Use real or frozen time and get the time limit
141                         round_endtime = STAT(ROUNDENDTIME);
142                         round_timelimit = STAT(ROUND_TIMELIMIT);
143
144                         if(round_endtime)
145                                 round_curtime = round_endtime;
146                         else if(timeout_last)
147                                 round_curtime = timeout_last;
148                         else
149                                 round_curtime = time;
150
151                         // Calculate time left
152                         round_timeleft = HUD_Timer_TimeLeft(round_curtime, STAT(ROUNDSTARTTIME), round_timelimit);
153
154                         // Subtimer color
155                         if(!intermission_time && round_timelimit > 0)
156                                 subtimer_color = HUD_Timer_Color(round_timeleft);
157
158                         // Subtimer text
159                         if (autocvar_hud_panel_timer_increment || round_timelimit <= 0)
160                                 subtimer = seconds_tostring(HUD_Timer_TimeElapsed(round_curtime, STAT(ROUNDSTARTTIME)));
161                         else
162                                 subtimer = seconds_tostring(round_timeleft);
163                 }
164         }
165
166         // Subtext
167         int overtimes = STAT(OVERTIMES);
168
169         if(warmup_stage || autocvar__hud_configure)
170         {
171                 if (STAT(WARMUP_TIMELIMIT) > 0)
172                         subtext = _("Warmup");
173                 else
174                         subtext = srv_minplayers ? _("Warmup: too few players") : _("Warmup: no time limit");
175         }
176         else if(STAT(TIMEOUT_STATUS) == 2)
177                 subtext = _("Timeout");
178         else if(overtimes >= 2)
179                 subtext = sprintf(_("Overtime #%d"), overtimes);
180         else if(overtimes != 0)
181                 subtext = _("Overtime");
182
183         subtext_size  = vec2(mySize.x, mySize.y / 3);
184         timer_size    = vec2(mySize.x, mySize.y - subtext_size.y);
185         subtimer_size = vec2(mySize.x / 3, mySize.y - subtext_size.y);
186
187         panel_size.y -= subtext_size.y;
188         HUD_Panel_DrawBg();
189
190         if(subtimer) {
191                 float subtimer_padding = subtimer_size.y / 5;
192                 timer_size.x -= subtimer_size.x;
193                 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);
194         }
195
196         drawstring_aspect(pos, (swap ? subtimer : timer), timer_size, (swap ? subtimer_color : timer_color), panel_fg_alpha, DRAWFLAG_NORMAL);
197
198         if(subtext)
199                 drawstring_aspect(pos + eY * timer_size.y, subtext, subtext_size, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
200
201         draw_endBoldFont();
202 }