]> 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_str = string_null;
68         string subtimer_str = 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         curtime = (intermission_time ? intermission_time : time);
79         timelimit = (warmup_stage ? STAT(WARMUP_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         // countdown sound
89         // if 3 use server dictated option, otherwise the client's
90         int countdown_type;
91         if(autocvar_cl_timer_countdown == 3)
92                 countdown_type = sv_timer_countdown;
93         else
94                 countdown_type = autocvar_cl_timer_countdown;
95         
96         if(countdown_type && !warmup_stage && timeleft > 0 && timeleft != last_timeleft && timeleft <= 10 && !intermission_time)
97         {
98                 if(countdown_type == 1 || (countdown_type == 2 && spectatee_status))
99                         sound(NULL, CH_INFO, SND_ENDCOUNT, VOL_BASE, ATTN_NONE);
100                 
101                 last_timeleft = timeleft;
102         }
103
104         // Timer text
105         if (timelimit == -1)
106                 timer = (autocvar_hud_panel_timer_increment ? 0 : STAT(TIMELIMIT) * 60);
107         else if (autocvar_hud_panel_timer_increment || timelimit <= 0)
108                 timer = HUD_Timer_TimeElapsed(curtime, STAT(GAMESTARTTIME));
109         else
110                 timer = timeleft;
111
112         // Secondary timer for round-based game modes
113         if(STAT(ROUNDSTARTTIME) && autocvar_hud_panel_timer_secondary)
114         {
115                 if(STAT(ROUNDSTARTTIME) == -1) {
116                         // Round can't start
117                         subtimer_str = "--:--";
118                         subtimer_color = '1 0 0';
119                 } else {
120                         float round_curtime, round_endtime, round_timelimit, round_timeleft;
121
122                         // Use real or frozen time and get the time limit
123                         round_endtime = STAT(ROUNDENDTIME);
124                         round_timelimit = STAT(ROUND_TIMELIMIT);
125
126                         if(round_endtime)
127                                 round_curtime = round_endtime;
128                         else if(timeout_last)
129                                 round_curtime = timeout_last;
130                         else
131                                 round_curtime = time;
132
133                         // Calculate time left
134                         round_timeleft = HUD_Timer_TimeLeft(round_curtime, STAT(ROUNDSTARTTIME), round_timelimit);
135
136                         // Subtimer color
137                         if(!intermission_time && round_timelimit > 0)
138                                 subtimer_color = HUD_Timer_Color(round_timeleft);
139
140                         // Subtimer text
141                         if (autocvar_hud_panel_timer_increment || round_timelimit <= 0)
142                                 subtimer_str = seconds_tostring(HUD_Timer_TimeElapsed(round_curtime, STAT(ROUNDSTARTTIME)));
143                         else
144                                 subtimer_str = seconds_tostring(round_timeleft);
145                 }
146         }
147
148         // Subtext
149         int overtimes = STAT(OVERTIMES);
150
151         if(warmup_stage || autocvar__hud_configure)
152                 subtext = _("Warmup");
153         else if(STAT(TIMEOUT_STATUS) == 2)
154                 subtext = _("Timeout");
155         else if(overtimes >= 2)
156                 subtext = sprintf(_("Overtime #%d"), overtimes);
157         else if(overtimes != 0)
158                 subtext = _("Overtime");
159
160         subtext_size  = vec2(mySize.x, mySize.y / 3);
161         timer_size    = vec2(mySize.x, mySize.y - subtext_size.y);
162         subtimer_size = vec2(mySize.x / 3, mySize.y - subtext_size.y);
163         timer_str     = seconds_tostring(timer);
164
165         panel_size.y -= subtext_size.y;
166         HUD_Panel_DrawBg();
167
168         if(subtimer_str) {
169                 float subtimer_padding = subtimer_size.y / 5;
170                 timer_size.x -= subtimer_size.x;
171                 drawstring_aspect(pos + eX * timer_size.x + eY * subtimer_padding, (swap ? timer_str : subtimer_str), subtimer_size - eY * subtimer_padding * 2, (swap ? timer_color : subtimer_color), panel_fg_alpha, DRAWFLAG_NORMAL);
172         }
173
174         drawstring_aspect(pos, (swap ? subtimer_str : timer_str), timer_size, (swap ? subtimer_color : timer_color), panel_fg_alpha, DRAWFLAG_NORMAL);
175
176         if(subtext)
177                 drawstring_aspect(pos + eY * timer_size.y, subtext, subtext_size, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
178
179         draw_endBoldFont();
180 }