]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/timer.qc
Update default video settings
[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_str = string_null;
66         string subtimer_str = 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         timelimit = (warmup_stage ? STAT(WARMUP_TIMELIMIT) : STAT(TIMELIMIT) * 60);
77
78         // Calculate time left
79         timeleft = HUD_Timer_TimeLeft(curtime, STAT(GAMESTARTTIME), timelimit);
80
81         // Timer color
82         if(!intermission_time && !warmup_stage && timelimit > 0)
83                 timer_color = HUD_Timer_Color(timeleft);
84
85         // Timer text
86         if (autocvar_hud_panel_timer_increment || timelimit <= 0)
87                 timer = HUD_Timer_TimeElapsed(curtime, STAT(GAMESTARTTIME));
88         else
89                 timer = timeleft;
90
91         // Secondary timer for round-based game modes
92         if(STAT(ROUNDSTARTTIME) && autocvar_hud_panel_timer_secondary)
93         {
94                 if(STAT(ROUNDSTARTTIME) == -1) {
95                         // Round can't start
96                         subtimer_str = "--:--";
97                         subtimer_color = '1 0 0';
98                 } else {
99                         float round_curtime, round_timelimit, round_timeleft;
100
101                         // Use real or frozen time and get the time limit
102                         round_curtime = (game_stopped_time ? game_stopped_time : time);
103                         round_timelimit = STAT(ROUND_TIMELIMIT);
104
105                         // Calculate time left
106                         round_timeleft = HUD_Timer_TimeLeft(round_curtime, STAT(ROUNDSTARTTIME), round_timelimit);
107
108                         // Subtimer color
109                         if(!intermission_time && round_timelimit > 0)
110                                 subtimer_color = HUD_Timer_Color(round_timeleft);
111
112                         // Subtimer text
113                         if (autocvar_hud_panel_timer_increment || round_timelimit <= 0)
114                                 subtimer_str = seconds_tostring(HUD_Timer_TimeElapsed(round_curtime, STAT(ROUNDSTARTTIME)));
115                         else
116                                 subtimer_str = seconds_tostring(round_timeleft);
117                 }
118         }
119
120         // Subtext
121         int overtimes = STAT(OVERTIMES);
122
123         if(warmup_stage || autocvar__hud_configure)
124         {
125                 if (STAT(WARMUP_TIMELIMIT) > 0)
126                         subtext = _("Warmup");
127                 else
128                 {
129                         Scoreboard_UpdatePlayerTeams(); // ensure numplayers is current
130                         if (srv_minplayers - numplayers > 0)
131                                 subtext = _("Warmup: too few players");
132                         else
133                                 subtext = _("Warmup: no time limit");
134                 }
135         }
136         else if(STAT(TIMEOUT_STATUS) == 2)
137                 subtext = _("Timeout");
138         else if (overtimes == -1)
139                 subtext = _("Sudden Death");
140         else if(overtimes == 1)
141                 subtext = _("Overtime");
142         else if (overtimes >= 2)
143                 subtext = sprintf(_("Overtime #%d"), overtimes);
144
145         subtext_size  = vec2(mySize.x, mySize.y / 3);
146         timer_size    = vec2(mySize.x, mySize.y - subtext_size.y);
147         subtimer_size = vec2(mySize.x / 3, mySize.y - subtext_size.y);
148         timer_str     = seconds_tostring(timer);
149
150         panel_size.y -= subtext_size.y;
151         HUD_Panel_DrawBg();
152
153         if(subtimer_str) {
154                 float subtimer_padding = subtimer_size.y / 5;
155                 timer_size.x -= subtimer_size.x;
156                 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);
157         }
158
159         drawstring_aspect(pos, (swap ? subtimer_str : timer_str), timer_size, (swap ? subtimer_color : timer_color), panel_fg_alpha, DRAWFLAG_NORMAL);
160
161         if(subtext)
162                 drawstring_aspect(pos + eY * timer_size.y, subtext, subtext_size, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
163
164         draw_endBoldFont();
165 }