Xonotic
timer.qc
Go to the documentation of this file.
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;
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;
35  timeleft = bound(0, timeleft, timelimit);
36  return ceil(timeleft);
37 }
38 
39 void HUD_Timer()
40 {
42  {
43  if(!autocvar_hud_panel_timer) return;
44  }
45 
47 
49 
50  vector pos, mySize;
51  pos = panel_pos;
52  mySize = panel_size;
53 
56  else
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
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 
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;
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 
161 }
vector HUD_Timer_Color(float timeleft)
Definition: timer.qc:13
#define draw_endBoldFont()
Definition: draw.qh:5
float panel_fg_alpha
Definition: hud.qh:166
string string_null
Definition: nil.qh:9
void HUD_Timer()
Definition: timer.qc:39
int autocvar_hud_panel_timer_secondary
Definition: timer.qh:7
void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag)
Definition: draw.qc:102
const vector eY
Definition: vector.qh:45
bool autocvar__hud_configure
Definition: hud_config.qh:3
#define draw_beginBoldFont()
Definition: draw.qh:4
vector panel_size
Definition: hud.qh:160
float game_stopped_time
Definition: view.qh:128
float intermission_time
Definition: view.qh:127
bool warmup_stage
Definition: main.qh:103
void HUD_Scale_Disable()
Definition: hud.qc:83
float HUD_Timer_TimeElapsed(float curtime, float starttime)
Definition: timer.qc:23
#define HUD_Panel_DrawBg()
Definition: hud.qh:54
bool autocvar_hud_panel_timer_dynamichud
Definition: timer.qh:5
float HUD_Timer_TimeLeft(float curtime, float starttime, float timelimit)
Definition: timer.qc:31
void HUD_Scale_Enable()
Definition: hud.qc:90
void HUD_Timer_Export(int fh)
Definition: timer.qc:8
float round_timelimit
Definition: round_handler.qh:9
const float DRAWFLAG_NORMAL
Definition: csprogsdefs.qc:317
vector panel_pos
Definition: hud.qh:159
vector(float skel, float bonenum) _skel_get_boneabs_hidden
const vector eX
Definition: vector.qh:44
#define vec2(...)
Definition: vector.qh:90
bool autocvar_hud_panel_timer
Definition: timer.qh:4
bool autocvar_hud_panel_timer_increment
Definition: timer.qh:6
float time
Definition: csprogsdefs.qc:16
float starttime
Definition: strafehud.qc:38
bool autocvar_hud_panel_timer_unbound
Definition: timer.qh:8
void HUD_Panel_LoadCvars()
Definition: hud.qc:216
float panel_bg_padding
Definition: hud.qh:171
ERASEABLE string seconds_tostring(float seconds)
Definition: string.qh:123