Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
counting.qh
Go to the documentation of this file.
1#pragma once
2
3#include "i18n.qh"
4
5// ===============================================
6// Time processing and counting functions/macros
7// ===============================================
8
9#define count_shells(num) \
10 count_fill(num, \
11 _("ZER^%d shells"), \
12 _("ONE^%d shell"), \
13 _("TWO^%d shells"), \
14 _("THR^%d shells"), \
15 _("MUL^%d shells"))
16
17#define count_bullets(num) \
18 count_fill(num, \
19 _("ZER^%d bullets"), \
20 _("ONE^%d bullet"), \
21 _("TWO^%d bullets"), \
22 _("THR^%d bullets"), \
23 _("MUL^%d bullets"))
24
25#define count_rockets(num) \
26 count_fill(num, \
27 _("ZER^%d rockets"), \
28 _("ONE^%d rocket"), \
29 _("TWO^%d rockets"), \
30 _("THR^%d rockets"), \
31 _("MUL^%d rockets"))
32
33#define count_cells(num) \
34 count_fill(num, \
35 _("ZER^%d cells"), \
36 _("ONE^%d cell"), \
37 _("TWO^%d cells"), \
38 _("THR^%d cells"), \
39 _("MUL^%d cells"))
40
41
42#define count_votes(num) \
43 count_fill(num, \
44 _("ZER^%d votes"), \
45 _("ONE^%d vote"), \
46 _("TWO^%d votes"), \
47 _("THR^%d votes"), \
48 _("MUL^%d votes"))
49
50#define count_matches(num) \
51 count_fill(num, \
52 _("ZER^%d matches"), \
53 _("ONE^%d match"), \
54 _("TWO^%d matches"), \
55 _("THR^%d matches"), \
56 _("MUL^%d matches"))
57
58#define count_points_decs(time, decs) sprintf(CTX(_("DEC^%s points")), ftos_decimals(time, decs))
59#define count_points(num) \
60 count_fill(num, \
61 _("ZER^%d points"), \
62 _("ONE^%d point"), \
63 _("TWO^%d points"), \
64 _("THR^%d points"), \
65 _("MUL^%d points"))
66
67#define count_results(num) \
68 count_fill(num, \
69 _("ZER^%d results"), \
70 _("ONE^%d result"), \
71 _("TWO^%d results"), \
72 _("THR^%d results"), \
73 _("MUL^%d results"))
74
75
76#define count_years_decs(time, decs) sprintf(CTX(_("DEC^%s years")), ftos_decimals(time, decs))
77#define count_years(time) \
78 count_fill(time, \
79 _("ZER^%d years"), \
80 _("ONE^%d year"), \
81 _("TWO^%d years"), \
82 _("THR^%d years"), \
83 _("MUL^%d years"))
84
85#define count_weeks_decs(time, decs) sprintf(CTX(_("DEC^%s weeks")), ftos_decimals(time, decs))
86#define count_weeks(time) \
87 count_fill(time, \
88 _("ZER^%d weeks"), \
89 _("ONE^%d week"), \
90 _("TWO^%d weeks"), \
91 _("THR^%d weeks"), \
92 _("MUL^%d weeks"))
93
94#define count_days_decs(time, decs) sprintf(CTX(_("DEC^%s days")), ftos_decimals(time, decs))
95#define count_days(time) \
96 count_fill(time, \
97 _("ZER^%d days"), \
98 _("ONE^%d day"), \
99 _("TWO^%d days"), \
100 _("THR^%d days"), \
101 _("MUL^%d days"))
102
103#define count_hours_decs(time, decs) sprintf(CTX(_("DEC^%s hours")), ftos_decimals(time, decs))
104#define count_hours(time) \
105 count_fill(time, \
106 _("ZER^%d hours"), \
107 _("ONE^%d hour"), \
108 _("TWO^%d hours"), \
109 _("THR^%d hours"), \
110 _("MUL^%d hours"))
111
112#define count_minutes_decs(time, decs) sprintf(CTX(_("DEC^%s minutes")), ftos_decimals(time, decs))
113#define count_minutes(time) \
114 count_fill(time, \
115 _("ZER^%d minutes"), \
116 _("ONE^%d minute"), \
117 _("TWO^%d minutes"), \
118 _("THR^%d minutes"), \
119 _("MUL^%d minutes"))
120
121#define count_seconds_decs(time, decs) sprintf(CTX(_("DEC^%s seconds")), ftos_decimals(time, decs))
122#define count_seconds(time) \
123 count_fill(time, \
124 _("ZER^%d seconds"), \
125 _("ONE^%d second"), \
126 _("TWO^%d seconds"), \
127 _("THR^%d seconds"), \
128 _("MUL^%d seconds"))
129
130// returns 1st, 2nd, 3rd, nth ordinal number from a cardinal number (integer)
132string count_ordinal(int num)
133{
134 // This function is designed primarily for the English language, it's impossible
135 // to accomodate all languages unless we do a specific function for each one...
136 // and since that's not technically feasible/practical, this is all we've got folks.
137
138 int last2digits = num % 100;
139
140 // numbers ending with 11, 12 and 13 don't follow the standard pattern
141 if (last2digits < 4 || last2digits > 20)
142 switch (last2digits % 10)
143 {
144 case 1: return sprintf(_("%dst"), num);
145 case 2: return sprintf(_("%dnd"), num);
146 case 3: return sprintf(_("%drd"), num);
147 }
148
149 return sprintf(_("%dth"), num);
150}
151
153string count_fill(float num, string zero, string one, string two, string three, string multi)
154{
155 // This function is designed primarily for the English language, it's impossible
156 // to accomodate all languages unless we do a specific function for each one...
157 // and since that's not technically feasible/practical, this is all we've got folks.
158
159 // Here you can insert specific strings based on the input number, so you could do
160 // e.g. count_seconds which outputs like this:
161 // 0 seconds
162 // 1 second
163 // 2 seconds
164 // 3 seconds
165 // etc... minutes, hours, days, etc.
166
167 switch (floor(num))
168 {
169 case 0: return sprintf(CTX(zero), num);
170 case 1:
171 if (num == 1) // EXACTLY value of 1
172 return sprintf(CTX(one), num);
173 else
174 return sprintf(CTX(multi), num);
175 case 2: return sprintf(CTX(two), num);
176 case 3: return sprintf(CTX(three), num);
177 default: return sprintf(CTX(multi), num);
178 }
179 return "";
180}
181
183string process_time(float outputtype, int seconds)
184{
185 int tmp_hours = 0, tmp_minutes = 0, tmp_seconds;
186 int tmp_years = 0, tmp_weeks = 0, tmp_days = 0;
187
188 tmp_seconds = floor(seconds);
189
190 if (tmp_seconds)
191 {
192 tmp_minutes = floor(tmp_seconds / 60);
193
194 if (tmp_minutes)
195 {
196 tmp_seconds -= tmp_minutes * 60;
197 tmp_hours = floor(tmp_minutes / 60);
198
199 if (tmp_hours)
200 {
201 tmp_minutes -= tmp_hours * 60;
202 tmp_days = floor(tmp_hours / 24);
203
204 if (tmp_days)
205 {
206 tmp_hours -= tmp_days * 24;
207 tmp_weeks = floor(tmp_days / 7);
208
209 if (tmp_weeks)
210 {
211 tmp_days -= tmp_weeks * 7;
212 tmp_years = floor(tmp_weeks / 52);
213 }
214 }
215 }
216 }
217 }
218
219 switch (outputtype)
220 {
221 case 1: return sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds);
222 case 2:
223 {
224 string output = "";
225 #define APPEND_TIME(unit) \
226 if (tmp_##unit) \
227 output = strcat(output, ((output != "") ? ", " : ""), count_##unit(tmp_##unit))
228 APPEND_TIME(years);
229 APPEND_TIME(weeks);
230 APPEND_TIME(days);
231 APPEND_TIME(hours);
232 APPEND_TIME(minutes);
233 APPEND_TIME(seconds);
234 #undef APPEND_TIME
235 if (output == "")
236 return count_seconds(0);
237 return output;
238 }
239 case 3:
240 {
241 string output = count_hours(tmp_hours);
242
243 if (tmp_weeks)
244 tmp_days += (tmp_weeks * 7);
245 if (tmp_years)
246 tmp_days += (tmp_years * 365);
247 if (tmp_days)
248 output = sprintf(
249 count_days(tmp_days),
250 ((output != "") ? strcat(", ", output) : "")
251 );
252
253 return output;
254 }
255 }
256 return "";
257}
#define APPEND_TIME(unit)
#define count_hours(time)
Definition counting.qh:104
ERASEABLE string count_fill(float num, string zero, string one, string two, string three, string multi)
Definition counting.qh:153
#define count_days(time)
Definition counting.qh:95
ERASEABLE string count_ordinal(int num)
Definition counting.qh:132
#define count_seconds(time)
Definition counting.qh:122
ERASEABLE string process_time(float outputtype, int seconds)
Definition counting.qh:183
ERASEABLE string CTX(string s)
Definition i18n.qh:46
#define ERASEABLE
Definition _all.inc:37
float floor(float f)
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))