3 // SuperTux (Statistics module)
4 // Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
5 // Copyright (C) 2006 Ondrej Hosek <ondra.hosek@gmail.com>
6 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "video/drawing_context.hpp"
29 #include "gettext.hpp"
30 #include "lisp/writer.hpp"
31 #include "lisp/lisp.hpp"
32 #include "resources.hpp"
34 #include "statistics.hpp"
36 #include "scripting/squirrel_util.hpp"
39 const int nv_coins = std::numeric_limits<int>::min();
40 const int nv_badguys = std::numeric_limits<int>::min();
41 const float nv_time = std::numeric_limits<float>::max();
42 const int nv_secrets = std::numeric_limits<int>::min();
45 float WMAP_INFO_LEFT_X;
46 float WMAP_INFO_RIGHT_X;
47 float WMAP_INFO_TOP_Y1;
48 float WMAP_INFO_TOP_Y2;
50 Statistics::Statistics() : coins(nv_coins), total_coins(nv_coins), badguys(nv_badguys), total_badguys(nv_badguys), time(nv_time), secrets(nv_secrets), total_secrets(nv_secrets), valid(true)
52 WMAP_INFO_LEFT_X = (SCREEN_WIDTH/2 + 80) + 32;
53 WMAP_INFO_RIGHT_X = SCREEN_WIDTH/2 + 368;
54 WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT/2 + 172 - 16;
55 WMAP_INFO_TOP_Y2 = SCREEN_HEIGHT/2 + 172;
58 Statistics::~Statistics()
64 Statistics::parse(const lisp::Lisp& reader)
66 reader.get("coins-collected", coins);
67 reader.get("coins-collected-total", total_coins);
68 reader.get("badguys-killed", badguys);
69 reader.get("badguys-killed-total", total_badguys);
70 reader.get("time-needed", time);
71 reader.get("secrets-found", secrets);
72 reader.get("secrets-found-total", total_secrets);
76 Statistics::write(lisp::Writer& writer)
78 writer.write_int("coins-collected", coins);
79 writer.write_int("coins-collected-total", total_coins);
80 writer.write_int("badguys-killed", badguys);
81 writer.write_int("badguys-killed-total", total_badguys);
82 writer.write_float("time-needed", time);
83 writer.write_int("secrets-found", secrets);
84 writer.write_int("secrets-found-total", total_secrets);
89 Statistics::serialize_to_squirrel(HSQUIRRELVM vm)
91 // 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
92 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;
94 sq_pushstring(vm, "statistics", -1);
96 if (coins != nv_coins) Scripting::store_int(vm, "coins-collected", coins);
97 if (total_coins != nv_coins) Scripting::store_int(vm, "coins-collected-total", total_coins);
98 if (badguys != nv_badguys) Scripting::store_int(vm, "badguys-killed", badguys);
99 if (total_badguys != nv_badguys) Scripting::store_int(vm, "badguys-killed-total", total_badguys);
100 if (time != nv_time) Scripting::store_float(vm, "time-needed", time);
101 if (secrets != nv_secrets) Scripting::store_int(vm, "secrets-found", secrets);
102 if (total_secrets != nv_secrets) Scripting::store_int(vm, "secrets-found-total", total_secrets);
103 sq_createslot(vm, -3);
107 Statistics::unserialize_from_squirrel(HSQUIRRELVM vm)
109 sq_pushstring(vm, "statistics", -1);
110 if(SQ_FAILED(sq_get(vm, -2))) {
113 Scripting::get_int(vm, "coins-collected", coins);
114 Scripting::get_int(vm, "coins-collected-total", total_coins);
115 Scripting::get_int(vm, "badguys-killed", badguys);
116 Scripting::get_int(vm, "badguys-killed-total", total_badguys);
117 Scripting::get_float(vm, "time-needed", time);
118 Scripting::get_int(vm, "secrets-found", secrets);
119 Scripting::get_int(vm, "secrets-found-total", total_secrets);
124 Statistics::draw_worldmap_info(DrawingContext& context)
126 // skip draw if level was never played
127 if (coins == nv_coins) return;
129 // skip draw if stats were declared invalid
132 context.draw_text(white_small_text, std::string("- ") + _("Best Level Statistics") + " -", Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1), ALIGN_CENTER, LAYER_GUI);
134 std::string caption_buf;
135 std::string stat_buf;
136 float posy = WMAP_INFO_TOP_Y2;
137 for (int stat_no = 0; stat_no < 4; stat_no++) {
141 caption_buf = _("Max coins collected:");
142 stat_buf = coins_to_string(coins, total_coins);
145 caption_buf = _("Max fragging:");
146 stat_buf = frags_to_string(badguys, total_badguys);
149 caption_buf = _("Min time needed:");
150 stat_buf = time_to_string(time);
153 caption_buf = _("Max secrets found:");
154 stat_buf = secrets_to_string(secrets, total_secrets);
157 log_debug << "Invalid stat requested to be drawn" << std::endl;
161 context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, posy), ALIGN_LEFT, LAYER_GUI);
162 context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, posy), ALIGN_RIGHT, LAYER_GUI);
163 posy += white_small_text->get_height() + 2;
169 Statistics::draw_message_info(DrawingContext& context, std::string title)
171 // skip draw if level was never played
172 // TODO: do we need this?
173 if (coins == nv_coins) return;
175 // skip draw if stats were declared invalid
178 const float width = white_small_text->get_text_width("Max coins collected: 1111 / 1111");
179 const float left = (SCREEN_WIDTH - width) / 2;
180 const float right = (SCREEN_WIDTH + width) / 2;
182 context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), ALIGN_CENTER, LAYER_GUI);
187 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
188 context.draw_text(white_small_text, _("Max coins collected:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
189 context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
192 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
193 context.draw_text(white_small_text, _("Max fragging:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
194 context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
197 int csecs = (int)(time * 100);
198 int mins = (int)(csecs / 6000);
199 int secs = (csecs % 6000) / 100;
200 snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
201 context.draw_text(white_small_text, _("Min time needed:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
202 context.draw_text(white_small_text, "%02d:%02d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
205 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
206 context.draw_text(white_small_text, _("Max secrets found:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
207 context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
212 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
214 // skip draw if level was never played
215 // TODO: do we need this?
216 if (coins == nv_coins) return;
218 // skip draw if stats were declared invalid
221 // abort if we have no backdrop
222 if (!backdrop) return;
224 int box_w = 220+110+110;
225 int box_h = 30+20+20+20;
226 int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
227 int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
229 int bd_w = (int)backdrop->get_width();
230 int bd_h = (int)backdrop->get_height();
231 int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
232 int bd_y = box_y + (box_h / 2) - (bd_h / 2);
235 int col2_x = col1_x+200;
236 int col3_x = col2_x+130;
239 int row2_y = row1_y+30;
240 int row3_y = row2_y+20;
241 int row4_y = row3_y+20;
243 context.push_transform();
244 context.set_alpha(0.5);
245 context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
246 context.pop_transform();
248 context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_GUI);
249 context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_GUI);
251 context.draw_text(white_text, _("Coins"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_GUI);
252 int coins_best = (best_stats && (best_stats->coins > coins)) ? best_stats->coins : coins;
253 int total_coins_best = (best_stats && (best_stats->total_coins > total_coins)) ? best_stats->total_coins : total_coins;
254 context.draw_text(gold_text, coins_to_string(coins, total_coins), Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_GUI);
255 context.draw_text(gold_text, coins_to_string(coins_best, total_coins_best), Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_GUI);
257 context.draw_text(white_text, _("Secrets"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_GUI);
258 int secrets_best = (best_stats && (best_stats->secrets > secrets)) ? best_stats->secrets : secrets;
259 int total_secrets_best = (best_stats && (best_stats->total_secrets > total_secrets)) ? best_stats->total_secrets : total_secrets;
260 context.draw_text(gold_text, secrets_to_string(secrets, total_secrets), Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_GUI);
261 context.draw_text(gold_text, secrets_to_string(secrets_best, total_secrets_best), Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_GUI);
263 context.draw_text(white_text, _("Time"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_GUI);
264 float time_best = (best_stats && (best_stats->time < time)) ? best_stats->time : time;
265 context.draw_text(gold_text, time_to_string(time), Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_GUI);
266 context.draw_text(gold_text, time_to_string(time_best), Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_GUI);
288 Statistics::merge(Statistics& s2)
290 if (!s2.valid) return;
291 coins = std::max(coins, s2.coins);
292 total_coins = s2.total_coins;
293 badguys = std::max(badguys, s2.badguys);
294 total_badguys = s2.total_badguys;
295 time = std::min(time, s2.time);
296 secrets = std::max(secrets, s2.secrets);
297 total_secrets = s2.total_secrets;
301 Statistics::operator+=(const Statistics& s2)
303 if (!s2.valid) return;
304 if (s2.coins != nv_coins) coins += s2.coins;
305 if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
306 if (s2.badguys != nv_badguys) badguys += s2.badguys;
307 if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
308 if (s2.time != nv_time) time += s2.time;
309 if (s2.secrets != nv_secrets) secrets += s2.secrets;
310 if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
314 Statistics::declare_invalid()
320 Statistics::coins_to_string(int coins, int total_coins) const {
321 std::ostringstream os;
322 os << std::min(coins, 999) << "/" << std::min(total_coins, 999);
327 Statistics::frags_to_string(int badguys, int total_badguys) const {
328 std::ostringstream os;
329 os << std::min(badguys, 999) << "/" << std::min(total_badguys, 999);
334 Statistics::time_to_string(float time) const {
335 int time_csecs = std::min(static_cast<int>(time * 100), 99 * 6000 + 9999);
336 int mins = (time_csecs / 6000);
337 int secs = (time_csecs % 6000) / 100;
338 int cscs = (time_csecs % 6000) % 100;
340 std::ostringstream os;
341 os << std::setw(2) << std::setfill('0') << mins << ":" << std::setw(2) << std::setfill('0') << secs << "." << std::setw(2) << std::setfill('0') << cscs;
346 Statistics::secrets_to_string(int secrets, int total_secrets) const {
347 std::ostringstream os;
348 os << std::min(secrets, 999) << "/" << std::min(total_secrets, 999);