Fix coverity #29357
[supertux.git] / src / supertux / statistics.cpp
1 //  SuperTux (Statistics module)
2 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
3 //  Copyright (C) 2006 Ondrej Hosek <ondra.hosek@gmail.com>
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software: you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation, either version 3 of the License, or
9 //  (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 #include "supertux/statistics.hpp"
20
21 #include <iomanip>
22 #include <limits>
23
24 #include "scripting/squirrel_util.hpp"
25 #include "supertux/globals.hpp"
26 #include "supertux/resources.hpp"
27 #include "util/gettext.hpp"
28 #include "video/drawing_context.hpp"
29
30 namespace {
31 const int nv_coins = std::numeric_limits<int>::min();
32 const int nv_badguys = std::numeric_limits<int>::min();
33 const float nv_time = std::numeric_limits<float>::max();
34 const int nv_secrets = std::numeric_limits<int>::min();
35 }
36
37 float WMAP_INFO_LEFT_X;
38 float WMAP_INFO_RIGHT_X;
39 float WMAP_INFO_TOP_Y1;
40 float WMAP_INFO_TOP_Y2;
41
42 Statistics::Statistics() :
43   coins(nv_coins),
44   total_coins(nv_coins),
45   badguys(nv_badguys),
46   total_badguys(nv_badguys),
47   time(nv_time),
48   secrets(nv_secrets),
49   total_secrets(nv_secrets),
50   valid(true)
51 {
52   WMAP_INFO_LEFT_X = SCREEN_WIDTH - 32 - 256;
53   WMAP_INFO_RIGHT_X = WMAP_INFO_LEFT_X + 256;
54   WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT - 100;
55   WMAP_INFO_TOP_Y2 = WMAP_INFO_TOP_Y1 + 16;
56 }
57
58 Statistics::~Statistics()
59 {
60 }
61
62 void
63 Statistics::serialize_to_squirrel(HSQUIRRELVM vm)
64 {
65   // TODO: there's some bug in the unserialization routines that breaks stuff when an empty statistics table is written, so -- as a workaround -- let's make sure we will actually write something first
66   if (!((coins != nv_coins) || (total_coins != nv_coins) || (badguys != nv_badguys) || (total_badguys != nv_badguys) || (time != nv_time) || (secrets != nv_secrets) || (total_secrets != nv_secrets))) return;
67
68   sq_pushstring(vm, "statistics", -1);
69   sq_newtable(vm);
70   if (coins != nv_coins) scripting::store_int(vm, "coins-collected", coins);
71   if (total_coins != nv_coins) scripting::store_int(vm, "coins-collected-total", total_coins);
72   if (badguys != nv_badguys) scripting::store_int(vm, "badguys-killed", badguys);
73   if (total_badguys != nv_badguys) scripting::store_int(vm, "badguys-killed-total", total_badguys);
74   if (time != nv_time) scripting::store_float(vm, "time-needed", time);
75   if (secrets != nv_secrets) scripting::store_int(vm, "secrets-found", secrets);
76   if (total_secrets != nv_secrets) scripting::store_int(vm, "secrets-found-total", total_secrets);
77   if(SQ_FAILED(sq_createslot(vm, -3)))
78     throw scripting::SquirrelError(vm, "Couldn't create statistics table");
79 }
80
81 void
82 Statistics::unserialize_from_squirrel(HSQUIRRELVM vm)
83 {
84   sq_pushstring(vm, "statistics", -1);
85   if(SQ_FAILED(sq_get(vm, -2))) {
86     return;
87   }
88   scripting::get_int(vm, "coins-collected", coins);
89   scripting::get_int(vm, "coins-collected-total", total_coins);
90   scripting::get_int(vm, "badguys-killed", badguys);
91   scripting::get_int(vm, "badguys-killed-total", total_badguys);
92   scripting::get_float(vm, "time-needed", time);
93   scripting::get_int(vm, "secrets-found", secrets);
94   scripting::get_int(vm, "secrets-found-total", total_secrets);
95   sq_pop(vm, 1);
96 }
97
98 void
99 Statistics::draw_worldmap_info(DrawingContext& context, float target_time)
100 {
101   // skip draw if level was never played
102   if (coins == nv_coins) return;
103
104   // skip draw if stats were declared invalid
105   if (!valid) return;
106
107   // no sense drawing stats if there are none
108   if (total_coins + total_badguys + total_secrets == 0) return;
109
110   // check to see if screen size has been changed
111   if (!(WMAP_INFO_TOP_Y1 == SCREEN_HEIGHT - 100)) {
112     WMAP_INFO_LEFT_X = SCREEN_WIDTH - 32 - 256;
113     WMAP_INFO_RIGHT_X = WMAP_INFO_LEFT_X + 256;
114     WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT - 100;
115     WMAP_INFO_TOP_Y2 = WMAP_INFO_TOP_Y1 + 16;
116   }
117
118   context.draw_text(Resources::small_font, std::string("- ") + _("Best Level Statistics") + " -",
119                     Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1),
120                     ALIGN_CENTER, LAYER_HUD,Statistics::header_color);
121
122   std::string caption_buf;
123   std::string stat_buf;
124   float posy = WMAP_INFO_TOP_Y2;
125   for (int stat_no = 0; stat_no < 5; stat_no++) {
126     switch (stat_no)
127     {
128       case 0:
129         caption_buf = _("Max coins collected:");
130         stat_buf = coins_to_string(coins, total_coins);
131         break;
132       case 1:
133         caption_buf = _("Max fragging:");
134         stat_buf = frags_to_string(badguys, total_badguys);
135         break;
136       case 2:
137         caption_buf = _("Max secrets found:");
138         stat_buf = secrets_to_string(secrets, total_secrets);
139         break;
140       case 3:
141         caption_buf = _("Best time completed:");
142         stat_buf = time_to_string(time);
143         break;
144       case 4:
145         if(target_time){ // display target time only if defined for level
146           caption_buf = _("Level target time:");
147           stat_buf = time_to_string(target_time);
148         } else {
149           caption_buf = "";
150           stat_buf = "";
151         }
152         break;
153       default:
154         log_debug << "Invalid stat requested to be drawn" << std::endl;
155         break;
156     }
157
158     context.draw_text(Resources::small_font, caption_buf, Vector(WMAP_INFO_LEFT_X, posy), ALIGN_LEFT, LAYER_HUD, Statistics::header_color);
159     context.draw_text(Resources::small_font, stat_buf, Vector(WMAP_INFO_RIGHT_X, posy), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
160     posy += Resources::small_font->get_height() + 2;
161   }
162
163 }
164
165 void
166 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, SurfacePtr backdrop)
167 {
168   // skip draw if stats were declared invalid
169   if (!valid) return;
170
171   // abort if we have no backdrop
172   if (!backdrop) return;
173
174   // no sense drawing stats if there are none
175   if (total_coins + total_badguys + total_secrets == 0) return;
176
177   int box_w = 220+110+110;
178   int box_h = 30+20+20+20;
179   int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
180   int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
181
182   int bd_w = (int)backdrop->get_width();
183   int bd_h = (int)backdrop->get_height();
184   int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
185   int bd_y = box_y + (box_h / 2) - (bd_h / 2);
186
187   int col1_x = box_x;
188   int col2_x = col1_x+200;
189   int col3_x = col2_x+130;
190
191   int row1_y = box_y;
192   int row2_y = row1_y+30;
193   int row3_y = row2_y+20;
194   int row4_y = row3_y+20;
195   int row5_y = row4_y+20;
196
197   context.push_transform();
198   context.set_alpha(0.5);
199   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_HUD);
200   context.pop_transform();
201
202   context.draw_text(Resources::normal_font, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_HUD, Statistics::header_color);
203   if (best_stats)
204     context.draw_text(Resources::normal_font, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_HUD, Statistics::header_color);
205
206   context.draw_text(Resources::normal_font, _("Coins"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
207   context.draw_text(Resources::normal_font, coins_to_string(coins, total_coins), Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
208   if (best_stats) {
209     int coins_best = (best_stats->coins > coins) ? best_stats->coins : coins;
210     int total_coins_best = (best_stats->total_coins > total_coins) ? best_stats->total_coins : total_coins;
211     context.draw_text(Resources::normal_font, coins_to_string(coins_best, total_coins_best), Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
212   }
213
214   context.draw_text(Resources::normal_font, _("Badguys"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
215   context.draw_text(Resources::normal_font, frags_to_string(badguys, total_badguys), Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
216   if (best_stats) {
217         int badguys_best = (best_stats->badguys > badguys) ? best_stats->badguys : badguys;
218         int total_badguys_best = (best_stats->total_badguys > total_badguys) ? best_stats->total_badguys : total_badguys;
219         context.draw_text(Resources::normal_font, frags_to_string(badguys_best, total_badguys_best), Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
220   }
221
222   context.draw_text(Resources::normal_font, _("Secrets"), Vector(col2_x-16, row5_y), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
223   context.draw_text(Resources::normal_font, secrets_to_string(secrets, total_secrets), Vector(col2_x, row5_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
224   if (best_stats) {
225     int secrets_best = (best_stats->secrets > secrets) ? best_stats->secrets : secrets;
226     int total_secrets_best = (best_stats->total_secrets > total_secrets) ? best_stats->total_secrets : total_secrets;
227     context.draw_text(Resources::normal_font, secrets_to_string(secrets_best, total_secrets_best), Vector(col3_x, row5_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
228   }
229
230   context.draw_text(Resources::normal_font, _("Time"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
231   context.draw_text(Resources::normal_font, time_to_string(time), Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
232   if (best_stats) {
233     float time_best = (best_stats->time < time) ? best_stats->time : time;
234     context.draw_text(Resources::normal_font, time_to_string(time_best), Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
235   }
236 }
237
238 void
239 Statistics::zero()
240 {
241   reset();
242   total_coins = 0;
243   total_badguys = 0;
244   total_secrets = 0;
245 }
246
247 void
248 Statistics::reset()
249 {
250   coins = 0;
251   badguys = 0;
252   time = 0;
253   secrets = 0;
254 }
255
256 void
257 Statistics::merge(const Statistics& s2)
258 {
259   if (!s2.valid) return;
260   coins = std::max(coins, s2.coins);
261   total_coins = s2.total_coins;
262   coins = std::min(coins, total_coins);
263   badguys = std::max(badguys, s2.badguys);
264   total_badguys = s2.total_badguys;
265   badguys = std::min(badguys, total_badguys);
266   time = std::min(time, s2.time);
267   secrets = std::max(secrets, s2.secrets);
268   total_secrets = s2.total_secrets;
269   secrets = std::min(secrets, total_secrets);
270 }
271
272 void
273 Statistics::operator+=(const Statistics& s2)
274 {
275   if (!s2.valid) return;
276   if (s2.coins != nv_coins) coins += s2.coins;
277   if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
278   if (s2.badguys != nv_badguys) badguys += s2.badguys;
279   if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
280   if (s2.time != nv_time) time += s2.time;
281   if (s2.secrets != nv_secrets) secrets += s2.secrets;
282   if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
283 }
284
285 bool
286 Statistics::completed(const Statistics& stats, const float target_time)
287 {
288   return (stats.coins == stats.total_coins &&
289       stats.badguys == stats.total_badguys &&
290       stats.secrets == stats.total_secrets &&
291       ((!target_time) || (stats.time <= target_time)));
292 }
293
294 void
295 Statistics::declare_invalid()
296 {
297   valid = false;
298 }
299
300 std::string
301 Statistics::coins_to_string(int coins, int total_coins) {
302   std::ostringstream os;
303   os << std::min(coins, 999) << "/" << std::min(total_coins, 999);
304   return os.str();
305 }
306
307 std::string
308 Statistics::frags_to_string(int badguys, int total_badguys) {
309   std::ostringstream os;
310   os << std::min(badguys, 999) << "/" << std::min(total_badguys, 999);
311   return os.str();
312 }
313
314 std::string
315 Statistics::time_to_string(float time) {
316   int time_csecs = std::min(static_cast<int>(time * 100), 99 * 6000 + 9999);
317   int mins = (time_csecs / 6000);
318   int secs = (time_csecs % 6000) / 100;
319   int cscs = (time_csecs % 6000) % 100;
320
321   std::ostringstream os;
322   os << std::setw(2) << std::setfill('0') << mins << ":" << std::setw(2) << std::setfill('0') << secs << "." << std::setw(2) << std::setfill('0') << cscs;
323   return os.str();
324 }
325
326 std::string
327 Statistics::secrets_to_string(int secrets, int total_secrets) {
328   std::ostringstream os;
329   os << std::min(secrets, 999) << "/" << std::min(total_secrets, 999);
330   return os.str();
331 }
332
333 /* EOF */