]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/timer.qc
Revert "Fixed timer in unlimited warmup"
[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
8 void HUD_Timer_Export(int fh)
9 {
10         // allow saving cvars that aesthetically change the panel into hud skin files
11 }
12
13 vector HUD_Timer_Color(float timeleft)
14 {
15         if(timeleft <= 60)
16                 return '1 0 0'; // red
17         else if(timeleft <= 300)
18                 return '1 1 0'; // yellow
19         else
20                 return '1 1 1'; // white
21 }
22
23 float HUD_Timer_TimeElapsed(float curtime, float starttime)
24 {
25         float time_elapsed = curtime - starttime;
26         if (!autocvar_hud_panel_timer_unbound)
27                 time_elapsed = max(0, time_elapsed);
28         return floor(time_elapsed);
29 }
30
31 float HUD_Timer_TimeLeft(float curtime, float starttime, float timelimit)
32 {
33         float timeleft = timelimit + starttime - curtime;
34         if (!autocvar_hud_panel_timer_unbound)
35                 timeleft = bound(0, timeleft, timelimit);
36         return ceil(timeleft);
37 }
38
39 void HUD_Timer()
40 {
41         if(!autocvar__hud_configure)
42         {
43                 if(!autocvar_hud_panel_timer) return;
44         }
45
46         HUD_Panel_LoadCvars();
47
48         draw_beginBoldFont();
49
50         vector pos, mySize;
51         pos = panel_pos;
52         mySize = panel_size;
53
54         if (autocvar_hud_panel_timer_dynamichud)
55                 HUD_Scale_Enable();
56         else
57                 HUD_Scale_Disable();
58         if(panel_bg_padding)
59         {
60                 pos += '1 1 0' * panel_bg_padding;
61                 mySize -= '2 2 0' * panel_bg_padding;
62         }
63
64         string timer;
65         string subtimer = string_null;
66         string subtext = string_null;
67         float curtime, timelimit, timeleft;
68         vector timer_size, subtext_size, subtimer_size;
69         vector timer_color = '1 1 1';
70         vector subtimer_color = '1 1 1';
71         bool swap = (autocvar_hud_panel_timer_secondary == 2 && STAT(ROUNDSTARTTIME));
72
73         // Use real or frozen time and get the time limit
74         curtime = (intermission_time ? intermission_time : time);
75         if(warmup_stage)
76         {
77                 timelimit = STAT(WARMUP_TIMELIMIT);
78                 if(timelimit == 0)
79                         timelimit = STAT(TIMELIMIT) * 60;
80         }
81         else
82         {
83                 timelimit = STAT(TIMELIMIT) * 60;
84         }
85
86         // Calculate time left
87         timeleft = HUD_Timer_TimeLeft(curtime, STAT(GAMESTARTTIME), timelimit);
88
89         // Timer color
90         if(!intermission_time && !warmup_stage && timelimit > 0)
91                 timer_color = HUD_Timer_Color(timeleft);
92
93         // Timer text
94         if (autocvar_hud_panel_timer_increment || timelimit == 0)
95                 timer = seconds_tostring(HUD_Timer_TimeElapsed(curtime, STAT(GAMESTARTTIME)));
96         else
97                 timer = seconds_tostring(timeleft);
98
99         // Secondary timer for round-based game modes
100         if(STAT(ROUNDSTARTTIME) && autocvar_hud_panel_timer_secondary)
101         {
102                 if(STAT(ROUNDSTARTTIME) == -1) {
103                         // Round can't start
104                         subtimer = "--:--";
105                         subtimer_color = '1 0 0';
106                 } else {
107                         float round_curtime, round_timelimit, round_timeleft;
108
109                         // Use real or frozen time and get the time limit
110                         round_curtime = (game_stopped_time ? game_stopped_time : time);
111                         round_timelimit = STAT(ROUND_TIMELIMIT);
112
113                         // Calculate time left
114                         round_timeleft = HUD_Timer_TimeLeft(round_curtime, STAT(ROUNDSTARTTIME), round_timelimit);
115
116                         // Subtimer color
117                         if(!intermission_time && round_timelimit > 0)
118                                 subtimer_color = HUD_Timer_Color(round_timeleft);
119
120                         // Subtimer text
121                         if (autocvar_hud_panel_timer_increment || round_timelimit <= 0)
122                                 subtimer = seconds_tostring(HUD_Timer_TimeElapsed(round_curtime, STAT(ROUNDSTARTTIME)));
123                         else
124                                 subtimer = seconds_tostring(round_timeleft);
125                 }
126         }
127
128         // Subtext
129         int overtimes = STAT(OVERTIMES);
130
131         if(warmup_stage || autocvar__hud_configure)
132                 subtext = _("Warmup");
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 }