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