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