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