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