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