]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/counting.qh
c5c95671cf9ae2ec4600bf0fe304a496d46ff296
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / counting.qh
1 #ifndef COUNTING_H
2 #define COUNTING_H
3
4 #if defined(CSQC)
5         #include "util-pre.qh"
6         #include "../client/sys-pre.qh"
7         #include "../dpdefs/csprogsdefs.qh"
8         #include "../client/sys-post.qh"
9         #include "../client/defs.qh"
10         #include "../dpdefs/keycodes.qh"
11         #include "constants.qh"
12         #include "stats.qh"
13         #include "../warpzonelib/anglestransform.qh"
14         #include "../warpzonelib/mathlib.qh"
15         #include "../warpzonelib/common.qh"
16         #include "../warpzonelib/client.qh"
17         #include "playerstats.qh"
18         #include "teams.qh"
19         #include "util.qh"
20         #include "nades.qh"
21         #include "buffs.qh"
22         #include "test.qh"
23 #elif defined(MENUQC)
24         #include "util-pre.qh"
25     #include "../menu/sys-pre.qh"
26     #include "../dpdefs/menudefs.qh"
27     #include "../dpdefs/keycodes.qh"
28     #include "../menu/sys-post.qh"
29     #include "../menu/config.qh"
30     #include "../warpzonelib/mathlib.qh"
31     #include "util.qh"
32     #include "test.qh"
33     #include "../menu/oo/base.qh"
34     #include "playerstats.qh"
35     #include "teams.qh"
36     #include "constants.qh"
37     #include "mapinfo.qh"
38     #include "campaign_common.qh"
39     #include "weapons/weapons.qh"
40 #elif defined(SVQC)
41         #include "util-pre.qh"
42     #include "../server/sys-pre.qh"
43     #include "../dpdefs/progsdefs.qh"
44     #include "../dpdefs/dpextensions.qh"
45     #include "../server/sys-post.qh"
46     #include "../warpzonelib/anglestransform.qh"
47     #include "../warpzonelib/mathlib.qh"
48     #include "../warpzonelib/common.qh"
49     #include "../warpzonelib/util_server.qh"
50     #include "../warpzonelib/server.qh"
51     #include "constants.qh"
52     #include "stats.qh"
53     #include "teams.qh"
54     #include "util.qh"
55     #include "nades.qh"
56     #include "buffs.qh"
57     #include "test.qh"
58 #endif
59
60 // ===============================================
61 //  Time processing and counting functions/macros
62 // ===============================================
63
64 #define count_years_decs(time,decs) sprintf(ZCTX(_("CI_DEC^%s years")), ftos_decimals(time, decs))
65 #define count_years(time) count_fill(time, \
66                 ZCTX(_("CI_ZER^%d years")), /* zeroth */ \
67                 ZCTX(_("CI_FIR^%d year")),  /* first */ \
68                 ZCTX(_("CI_SEC^%d years")), /* year */ \
69                 ZCTX(_("CI_THI^%d years")), /* third */ \
70                 ZCTX(_("CI_MUL^%d years"))) /* multi */
71
72 #define count_weeks_decs(time,decs) sprintf(ZCTX(_("CI_DEC^%s weeks")), ftos_decimals(time, decs))
73 #define count_weeks(time) count_fill(time, \
74                 ZCTX(_("CI_ZER^%d weeks")), /* zeroth */ \
75                 ZCTX(_("CI_FIR^%d week")),  /* first */ \
76                 ZCTX(_("CI_SEC^%d weeks")), /* week */ \
77                 ZCTX(_("CI_THI^%d weeks")), /* third */ \
78                 ZCTX(_("CI_MUL^%d weeks"))) /* multi */
79
80 #define count_days_decs(time,decs) sprintf(ZCTX(_("CI_DEC^%s days")), ftos_decimals(time, decs))
81 #define count_days(time) count_fill(time, \
82                 ZCTX(_("CI_ZER^%d days")), /* zeroth */ \
83                 ZCTX(_("CI_FIR^%d day")),  /* first */ \
84                 ZCTX(_("CI_SEC^%d days")), /* day */ \
85                 ZCTX(_("CI_THI^%d days")), /* third */ \
86                 ZCTX(_("CI_MUL^%d days"))) /* multi */
87
88 #define count_hours_decs(time,decs) sprintf(ZCTX(_("CI_DEC^%s hours")), ftos_decimals(time, decs))
89 #define count_hours(time) count_fill(time, \
90                 ZCTX(_("CI_ZER^%d hours")), /* zeroth */ \
91                 ZCTX(_("CI_FIR^%d hour")),  /* first */ \
92                 ZCTX(_("CI_SEC^%d hours")), /* hour */ \
93                 ZCTX(_("CI_THI^%d hours")), /* third */ \
94                 ZCTX(_("CI_MUL^%d hours"))) /* multi */
95
96
97 #define count_minutes_decs(time,decs) sprintf(ZCTX(_("CI_DEC^%s minutes")), ftos_decimals(time, decs))
98 #define count_minutes(time) count_fill(time, \
99                 ZCTX(_("CI_ZER^%d minutes")), /* zeroth */ \
100                 ZCTX(_("CI_FIR^%d minute")),  /* first */ \
101                 ZCTX(_("CI_SEC^%d minutes")), /* minute */ \
102                 ZCTX(_("CI_THI^%d minutes")), /* third */ \
103                 ZCTX(_("CI_MUL^%d minutes"))) /* multi */
104
105 #define count_seconds_decs(time,decs) sprintf(ZCTX(_("CI_DEC^%s seconds")), ftos_decimals(time, decs))
106 #define count_seconds(time) count_fill(time, \
107                 ZCTX(_("CI_ZER^%d seconds")), /* zeroth */ \
108                 ZCTX(_("CI_FIR^%d second")),  /* first */ \
109                 ZCTX(_("CI_SEC^%d seconds")), /* second */ \
110                 ZCTX(_("CI_THI^%d seconds")), /* third */ \
111                 ZCTX(_("CI_MUL^%d seconds"))) /* multi */
112
113 string count_ordinal(int interval)
114 {
115         // This function is designed primarily for the English language, it's impossible
116         // to accomodate all languages unless we do a specific function for each one...
117         // and since that's not technically feasible/practical, this is all we've got folks.
118
119         // Basically, it just allows you to represent a number or count in different ways
120         // depending on the number... like, with count_ordinal you can provide integers
121         // and retrieve 1st, 2nd, 3rd, nth ordinal numbers in a clean and simple way.
122         if(floor((interval % 100)/10) * 10 != 10) // examples: 12th, 111th, 213th will not execute this block
123         {
124                 // otherwise, check normally for 1st,2nd,3rd insertions
125                 switch(interval % 10)
126                 {
127                         case 1: return sprintf(_("%dst"), interval);
128                         case 2: return sprintf(_("%dnd"), interval);
129                         case 3: return sprintf(_("%drd"), interval);
130                         default: return sprintf(_("%dth"), interval);
131                 }
132         }
133         else { return sprintf(_("%dth"), interval); }
134
135         return "";
136 }
137
138 string count_fill(float interval, string zeroth, string first, string second, string third, string multi)
139 {
140         // This function is designed primarily for the English language, it's impossible
141         // to accomodate all languages unless we do a specific function for each one...
142         // and since that's not technically feasible/practical, this is all we've got folks.
143
144         // Here you can insert specific strings based on the interval number, so you could do
145         // i.e. count_seconds which outputs like this:
146         //   0 seconds
147         //   1 second
148         //   2 seconds
149         //   3 seconds
150         //   etc... minutes, hours, days, etc.
151
152         switch(floor(interval))
153         {
154                 case 0: return sprintf(zeroth, interval);
155                 case 1:
156                 {
157                         if(interval == 1) // EXACTLY value of 1
158                                 return sprintf(first, interval);
159                         else
160                                 return sprintf(multi, interval);
161                 }
162                 case 2: return sprintf(second, interval);
163                 case 3: return sprintf(third, interval);
164                 default: return sprintf(multi, interval);
165         }
166         return "";
167 }
168
169 string process_time(float outputtype, float seconds)
170 {
171         float tmp_hours = 0, tmp_minutes = 0, tmp_seconds = 0;
172         float tmp_years = 0, tmp_weeks = 0, tmp_days = 0;
173
174         tmp_seconds = floor(seconds);
175
176         if(tmp_seconds)
177         {
178                 tmp_minutes = floor(tmp_seconds / 60);
179
180                 if(tmp_minutes)
181                 {
182                         tmp_seconds -= (tmp_minutes * 60);
183                         tmp_hours = floor(tmp_minutes / 60);
184
185                         if(tmp_hours)
186                         {
187                                 tmp_minutes -= (tmp_hours * 60);
188                                 tmp_days = floor(tmp_hours / 24);
189
190                                 if(tmp_days)
191                                 {
192                                         tmp_hours -= (tmp_days * 24);
193                                         tmp_weeks = floor(tmp_days / 7);
194
195                                         if(tmp_weeks)
196                                         {
197                                                 tmp_days -= (tmp_weeks * 7);
198                                                 tmp_years = floor(tmp_weeks / 52);
199                                         }
200                                 }
201                         }
202                 }
203         }
204
205         switch(outputtype)
206         {
207                 case 1: return sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds);
208                 case 2:
209                 {
210                         string output = "";
211
212                         output = count_seconds(tmp_seconds);
213
214                         if(tmp_minutes)
215                         {
216                                 output = sprintf(
217                                         "%s%s",
218                                         count_minutes(tmp_minutes),
219                                         ((output != "") ? sprintf(", %s", output) : ""));
220                         }
221
222                         if(tmp_hours)
223                         {
224                                 output = sprintf(
225                                         "%s%s",
226                                         count_hours(tmp_hours),
227                                         ((output != "") ? sprintf(", %s", output) : ""));
228                         }
229
230                         if(tmp_days)
231                         {
232                                 output = sprintf(
233                                         "%s%s",
234                                         count_days(tmp_days),
235                                         ((output != "") ? sprintf(", %s", output) : ""));
236                         }
237
238                         if(tmp_weeks)
239                         {
240                                 output = sprintf(
241                                         "%s%s",
242                                         count_weeks(tmp_weeks),
243                                         ((output != "") ? sprintf(", %s", output) : ""));
244                         }
245
246                         if(tmp_years)
247                         {
248                                 output = sprintf(
249                                         "%s%s",
250                                         count_years(tmp_years),
251                                         ((output != "") ? sprintf(", %s", output) : ""));
252                         }
253
254                         return output;
255                 }
256                 case 3:
257                 {
258                         string output = "";
259
260                         output = count_hours(tmp_hours);
261
262                         if(tmp_weeks) { tmp_days += (tmp_weeks * 7); }
263                         if(tmp_years) { tmp_days += (tmp_years * 365); }
264                         if(tmp_days)
265                         {
266                                 output = sprintf(
267                                         "%s%s",
268                                         count_days(tmp_days),
269                                         ((output != "") ? sprintf(", %s", output) : ""));
270                         }
271
272                         return output;
273                 }
274         }
275         return "";
276 }
277 #endif