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("coins-collected", coins);
79 writer.write("coins-collected-total", total_coins);
80 writer.write("badguys-killed", badguys);
81 writer.write("badguys-killed-total", total_badguys);
82 writer.write("time-needed", time);
83 writer.write("secrets-found", secrets);
84 writer.write("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(small_font, std::string("- ") + _("Best Level Statistics") + " -", Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1), ALIGN_CENTER, LAYER_GUI,Statistics::header_color);
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(small_font, caption_buf, Vector(WMAP_INFO_LEFT_X, posy), ALIGN_LEFT, LAYER_GUI, Statistics::header_color);
162 context.draw_text(small_font, stat_buf, Vector(WMAP_INFO_RIGHT_X, posy), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
163 posy += small_font->get_height() + 2;
169 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
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 // abort if we have no backdrop
179 if (!backdrop) return;
181 int box_w = 220+110+110;
182 int box_h = 30+20+20+20;
183 int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
184 int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
186 int bd_w = (int)backdrop->get_width();
187 int bd_h = (int)backdrop->get_height();
188 int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
189 int bd_y = box_y + (box_h / 2) - (bd_h / 2);
192 int col2_x = col1_x+200;
193 int col3_x = col2_x+130;
196 int row2_y = row1_y+30;
197 int row3_y = row2_y+20;
198 int row4_y = row3_y+20;
200 context.push_transform();
201 context.set_alpha(0.5);
202 context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
203 context.pop_transform();
205 context.draw_text(normal_font, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_GUI, Statistics::header_color);
206 context.draw_text(normal_font, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_GUI, Statistics::header_color);
208 context.draw_text(normal_font, _("Coins"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
209 int coins_best = (best_stats && (best_stats->coins > coins)) ? best_stats->coins : coins;
210 int total_coins_best = (best_stats && (best_stats->total_coins > total_coins)) ? best_stats->total_coins : total_coins;
211 context.draw_text(normal_font, coins_to_string(coins, total_coins), Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
212 context.draw_text(normal_font, coins_to_string(coins_best, total_coins_best), Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
214 context.draw_text(normal_font, _("Secrets"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
215 int secrets_best = (best_stats && (best_stats->secrets > secrets)) ? best_stats->secrets : secrets;
216 int total_secrets_best = (best_stats && (best_stats->total_secrets > total_secrets)) ? best_stats->total_secrets : total_secrets;
217 context.draw_text(normal_font, secrets_to_string(secrets, total_secrets), Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
218 context.draw_text(normal_font, secrets_to_string(secrets_best, total_secrets_best), Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
220 context.draw_text(normal_font, _("Time"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
221 float time_best = (best_stats && (best_stats->time < time)) ? best_stats->time : time;
222 context.draw_text(normal_font, time_to_string(time), Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
223 context.draw_text(normal_font, time_to_string(time_best), Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
245 Statistics::merge(const Statistics& s2)
247 if (!s2.valid) return;
248 coins = std::max(coins, s2.coins);
249 total_coins = s2.total_coins;
250 badguys = std::max(badguys, s2.badguys);
251 total_badguys = s2.total_badguys;
252 time = std::min(time, s2.time);
253 secrets = std::max(secrets, s2.secrets);
254 total_secrets = s2.total_secrets;
258 Statistics::operator+=(const Statistics& s2)
260 if (!s2.valid) return;
261 if (s2.coins != nv_coins) coins += s2.coins;
262 if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
263 if (s2.badguys != nv_badguys) badguys += s2.badguys;
264 if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
265 if (s2.time != nv_time) time += s2.time;
266 if (s2.secrets != nv_secrets) secrets += s2.secrets;
267 if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
271 Statistics::declare_invalid()
277 Statistics::coins_to_string(int coins, int total_coins) {
278 std::ostringstream os;
279 os << std::min(coins, 999) << "/" << std::min(total_coins, 999);
284 Statistics::frags_to_string(int badguys, int total_badguys) {
285 std::ostringstream os;
286 os << std::min(badguys, 999) << "/" << std::min(total_badguys, 999);
291 Statistics::time_to_string(float time) {
292 int time_csecs = std::min(static_cast<int>(time * 100), 99 * 6000 + 9999);
293 int mins = (time_csecs / 6000);
294 int secs = (time_csecs % 6000) / 100;
295 int cscs = (time_csecs % 6000) % 100;
297 std::ostringstream os;
298 os << std::setw(2) << std::setfill('0') << mins << ":" << std::setw(2) << std::setfill('0') << secs << "." << std::setw(2) << std::setfill('0') << cscs;
303 Statistics::secrets_to_string(int secrets, int total_secrets) {
304 std::ostringstream os;
305 os << std::min(secrets, 999) << "/" << std::min(total_secrets, 999);