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.
27 #include "video/drawing_context.hpp"
28 #include "gettext.hpp"
29 #include "lisp/writer.hpp"
30 #include "lisp/lisp.hpp"
31 #include "resources.hpp"
33 #include "statistics.hpp"
35 #include "scripting/squirrel_util.hpp"
38 const int nv_coins = std::numeric_limits<int>::min();
39 const int nv_badguys = std::numeric_limits<int>::min();
40 const float nv_time = std::numeric_limits<float>::max();
41 const int nv_secrets = std::numeric_limits<int>::min();
44 float WMAP_INFO_LEFT_X;
45 float WMAP_INFO_RIGHT_X;
46 float WMAP_INFO_TOP_Y1;
47 float WMAP_INFO_TOP_Y2;
49 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), display_stat(0)
51 WMAP_INFO_LEFT_X = (SCREEN_WIDTH/2 + 80) + 32;
52 WMAP_INFO_RIGHT_X = SCREEN_WIDTH/2 + 368;
53 WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT/2 + 172 - 16;
54 WMAP_INFO_TOP_Y2 = SCREEN_HEIGHT/2 + 172;
57 Statistics::~Statistics()
63 Statistics::parse(const lisp::Lisp& reader)
65 reader.get("coins-collected", coins);
66 reader.get("coins-collected-total", total_coins);
67 reader.get("badguys-killed", badguys);
68 reader.get("badguys-killed-total", total_badguys);
69 reader.get("time-needed", time);
70 reader.get("secrets-found", secrets);
71 reader.get("secrets-found-total", total_secrets);
75 Statistics::write(lisp::Writer& writer)
77 writer.write_int("coins-collected", coins);
78 writer.write_int("coins-collected-total", total_coins);
79 writer.write_int("badguys-killed", badguys);
80 writer.write_int("badguys-killed-total", total_badguys);
81 writer.write_float("time-needed", time);
82 writer.write_int("secrets-found", secrets);
83 writer.write_int("secrets-found-total", total_secrets);
88 Statistics::serialize_to_squirrel(HSQUIRRELVM vm)
90 // 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
91 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;
93 sq_pushstring(vm, "statistics", -1);
95 if (coins != nv_coins) Scripting::store_int(vm, "coins-collected", coins);
96 if (total_coins != nv_coins) Scripting::store_int(vm, "coins-collected-total", total_coins);
97 if (badguys != nv_badguys) Scripting::store_int(vm, "badguys-killed", badguys);
98 if (total_badguys != nv_badguys) Scripting::store_int(vm, "badguys-killed-total", total_badguys);
99 if (time != nv_time) Scripting::store_float(vm, "time-needed", time);
100 if (secrets != nv_secrets) Scripting::store_int(vm, "secrets-found", secrets);
101 if (total_secrets != nv_secrets) Scripting::store_int(vm, "secrets-found-total", total_secrets);
102 sq_createslot(vm, -3);
106 Statistics::unserialize_from_squirrel(HSQUIRRELVM vm)
108 sq_pushstring(vm, "statistics", -1);
109 if(SQ_FAILED(sq_get(vm, -2))) {
112 Scripting::get_int(vm, "coins-collected", coins);
113 Scripting::get_int(vm, "coins-collected-total", total_coins);
114 Scripting::get_int(vm, "badguys-killed", badguys);
115 Scripting::get_int(vm, "badguys-killed-total", total_badguys);
116 Scripting::get_float(vm, "time-needed", time);
117 Scripting::get_int(vm, "secrets-found", secrets);
118 Scripting::get_int(vm, "secrets-found-total", total_secrets);
122 //define TOTAL_DISPLAY_TIME 3400
123 //define FADING_TIME 600
125 #define TOTAL_DISPLAY_TIME 5
126 #define FADING_TIME 1
129 Statistics::draw_worldmap_info(DrawingContext& context)
131 // skip draw if level was never played
132 if (coins == nv_coins) return;
134 // skip draw if stats were declared invalid
137 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);
140 if(timer.get_timegone() < FADING_TIME)
141 alpha = (timer.get_timegone() * 1.0f / FADING_TIME);
142 else if(timer.get_timeleft() < FADING_TIME)
143 alpha = (timer.get_timeleft() * 1.0f / FADING_TIME);
147 context.push_transform();
148 context.set_alpha(alpha);
150 char caption_buf[128];
152 switch (display_stat)
155 snprintf(caption_buf, sizeof(caption_buf), _("Max coins collected:"));
156 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
159 snprintf(caption_buf, sizeof(caption_buf), _("Max fragging:"));
160 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
163 snprintf(caption_buf, sizeof(caption_buf), _("Min time needed:"));
165 int csecs = (int)(time * 100);
166 int mins = (int)(csecs / 6000);
167 int secs = (csecs % 6000) / 100;
168 snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
172 snprintf(caption_buf, sizeof(caption_buf), _("Max secrets found:"));
173 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
176 log_debug << "Invalid stat requested to be drawn" << std::endl;
180 if (!timer.started())
182 timer.start(TOTAL_DISPLAY_TIME);
184 if (display_stat > 3) display_stat = 0;
187 context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, WMAP_INFO_TOP_Y2), ALIGN_LEFT, LAYER_GUI);
188 context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, WMAP_INFO_TOP_Y2), ALIGN_RIGHT, LAYER_GUI);
189 context.pop_transform();
193 Statistics::draw_message_info(DrawingContext& context, std::string title)
195 // skip draw if level was never played
196 // TODO: do we need this?
197 if (coins == nv_coins) return;
199 // skip draw if stats were declared invalid
202 const float width = white_small_text->get_text_width("Max coins collected: 1111 / 1111");
203 const float left = (SCREEN_WIDTH - width) / 2;
204 const float right = (SCREEN_WIDTH + width) / 2;
206 context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), ALIGN_CENTER, LAYER_GUI);
211 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
212 context.draw_text(white_small_text, _("Max coins collected:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
213 context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
216 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
217 context.draw_text(white_small_text, _("Max fragging:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
218 context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
221 int csecs = (int)(time * 100);
222 int mins = (int)(csecs / 6000);
223 int secs = (csecs % 6000) / 100;
224 snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
225 context.draw_text(white_small_text, _("Min time needed:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
226 context.draw_text(white_small_text, "%02d:%02d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
229 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
230 context.draw_text(white_small_text, _("Max secrets found:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
231 context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
236 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
238 // skip draw if level was never played
239 // TODO: do we need this?
240 if (coins == nv_coins) return;
242 // skip draw if stats were declared invalid
245 // abort if we have no backdrop
246 if (!backdrop) return;
248 int box_w = 220+110+110;
249 int box_h = 30+20+20+20;
250 int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
251 int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
253 int bd_w = (int)backdrop->get_width();
254 int bd_h = (int)backdrop->get_height();
255 int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
256 int bd_y = box_y + (box_h / 2) - (bd_h / 2);
259 int col2_x = col1_x+200;
260 int col3_x = col2_x+130;
263 int row2_y = row1_y+30;
264 int row3_y = row2_y+20;
265 int row4_y = row3_y+20;
267 context.push_transform();
268 context.set_alpha(0.5);
269 context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
270 context.pop_transform();
273 context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_GUI);
274 context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_GUI);
276 context.draw_text(white_text, _("Coins"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_GUI);
277 snprintf(buf, sizeof(buf), "%d/%d", std::min(coins, 999), std::min(total_coins, 999));
278 context.draw_text(gold_text, buf, Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_GUI);
279 if (best_stats && (best_stats->coins > coins)) {
280 snprintf(buf, sizeof(buf), "%d/%d", std::min(best_stats->coins, 999), std::min(best_stats->total_coins, 999));
282 context.draw_text(gold_text, buf, Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_GUI);
284 context.draw_text(white_text, _("Secrets"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_GUI);
285 snprintf(buf, sizeof(buf), "%d/%d", secrets, total_secrets);
286 context.draw_text(gold_text, buf, Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_GUI);
287 if (best_stats && (best_stats->secrets > secrets)) {
288 snprintf(buf, sizeof(buf), "%d/%d", best_stats->secrets, best_stats->total_secrets);
290 context.draw_text(gold_text, buf, Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_GUI);
292 context.draw_text(white_text, _("Time"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_GUI);
293 int csecs = (int)(time * 100);
294 int mins = (int)(csecs / 6000);
295 int secs = (csecs % 6000) / 100;
296 snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
297 context.draw_text(gold_text, buf, Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_GUI);
298 if (best_stats && (best_stats->time < time)) {
299 int csecs = (int)(best_stats->time * 100);
300 int mins = (int)(csecs / 6000);
301 int secs = (csecs % 6000) / 100;
302 snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
304 context.draw_text(gold_text, buf, Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_GUI);
326 Statistics::merge(Statistics& s2)
328 if (!s2.valid) return;
329 coins = std::max(coins, s2.coins);
330 total_coins = s2.total_coins;
331 badguys = std::max(badguys, s2.badguys);
332 total_badguys = s2.total_badguys;
333 time = std::min(time, s2.time);
334 secrets = std::max(secrets, s2.secrets);
335 total_secrets = s2.total_secrets;
339 Statistics::operator+=(const Statistics& s2)
341 if (!s2.valid) return;
342 if (s2.coins != nv_coins) coins += s2.coins;
343 if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
344 if (s2.badguys != nv_badguys) badguys += s2.badguys;
345 if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
346 if (s2.time != nv_time) time += s2.time;
347 if (s2.secrets != nv_secrets) secrets += s2.secrets;
348 if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
352 Statistics::declare_invalid()