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.
26 #include "video/drawing_context.hpp"
27 #include "gettext.hpp"
28 #include "lisp/lisp.hpp"
29 #include "resources.hpp"
31 #include "statistics.hpp"
35 const int nv_coins = std::numeric_limits<int>::min();
36 const int nv_badguys = std::numeric_limits<int>::min();
37 const float nv_time = std::numeric_limits<float>::max();
38 const int nv_secrets = std::numeric_limits<int>::min();
41 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)
45 Statistics::~Statistics()
50 Statistics::parse(const lisp::Lisp& reader)
52 reader.get("coins-collected", coins);
53 reader.get("coins-collected-total", total_coins);
54 reader.get("badguys-killed", badguys);
55 reader.get("badguys-killed-total", total_badguys);
56 reader.get("time-needed", time);
57 reader.get("secrets-found", secrets);
58 reader.get("secrets-found-total", total_secrets);
62 Statistics::write(lisp::Writer& writer)
64 writer.write_int("coins-collected", coins);
65 writer.write_int("coins-collected-total", total_coins);
66 writer.write_int("badguys-killed", badguys);
67 writer.write_int("badguys-killed-total", total_badguys);
68 writer.write_float("time-needed", time);
69 writer.write_int("secrets-found", secrets);
70 writer.write_int("secrets-found-total", total_secrets);
73 //define TOTAL_DISPLAY_TIME 3400
74 //define FADING_TIME 600
76 #define TOTAL_DISPLAY_TIME 5
79 const float WMAP_INFO_LEFT_X = (800 - 320) + 32;
80 const float WMAP_INFO_RIGHT_X = 800 - 32;
81 const float WMAP_INFO_TOP_Y1 = 600 - 128 - 16;
82 const float WMAP_INFO_TOP_Y2 = 600 - 128;
85 Statistics::draw_worldmap_info(DrawingContext& context)
87 // skip draw if level was never played
88 if (coins == nv_coins) return;
90 // skip draw if stats were declared invalid
93 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), CENTER_ALLIGN, LAYER_GUI);
96 if(timer.get_timegone() < FADING_TIME)
97 alpha = (timer.get_timegone() * 1.0f / FADING_TIME);
98 else if(timer.get_timeleft() < FADING_TIME)
99 alpha = (timer.get_timeleft() * 1.0f / FADING_TIME);
103 context.push_transform();
104 context.set_alpha(alpha);
106 char caption_buf[128];
108 switch (display_stat)
111 snprintf(caption_buf, sizeof(caption_buf), _("Max coins collected:"));
112 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
115 snprintf(caption_buf, sizeof(caption_buf), _("Max fragging:"));
116 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
119 snprintf(caption_buf, sizeof(caption_buf), _("Min time needed:"));
121 int csecs = (int)(time * 100);
122 int mins = (int)(csecs / 6000);
123 int secs = (csecs % 6000) / 100;
124 snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
128 snprintf(caption_buf, sizeof(caption_buf), _("Max secrets found:"));
129 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
132 log_debug << "Invalid stat requested to be drawn" << std::endl;
136 if (!timer.started())
138 timer.start(TOTAL_DISPLAY_TIME);
140 if (display_stat > 3) display_stat = 0;
143 context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, WMAP_INFO_TOP_Y2), LEFT_ALLIGN, LAYER_GUI);
144 context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, WMAP_INFO_TOP_Y2), RIGHT_ALLIGN, LAYER_GUI);
145 context.pop_transform();
149 Statistics::draw_message_info(DrawingContext& context, std::string title)
151 // skip draw if level was never played
152 // TODO: do we need this?
153 if (coins == nv_coins) return;
155 // skip draw if stats were declared invalid
158 const float width = white_small_text->get_text_width("Max coins collected: 1111 / 1111");
159 const float left = (SCREEN_WIDTH - width) / 2;
160 const float right = (SCREEN_WIDTH + width) / 2;
162 context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
167 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
168 context.draw_text(white_small_text, _("Max coins collected:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
169 context.draw_text(white_small_text, "%d / %d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
172 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
173 context.draw_text(white_small_text, _("Max fragging:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
174 context.draw_text(white_small_text, "%d / %d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
177 int csecs = (int)(time * 100);
178 int mins = (int)(csecs / 6000);
179 int secs = (csecs % 6000) / 100;
180 snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
181 context.draw_text(white_small_text, _("Min time needed:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
182 context.draw_text(white_small_text, "%02d:%02d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
185 snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
186 context.draw_text(white_small_text, _("Max secrets found:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
187 context.draw_text(white_small_text, "%d / %d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
192 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
194 // skip draw if level was never played
195 // TODO: do we need this?
196 if (coins == nv_coins) return;
198 // skip draw if stats were declared invalid
201 // abort if we have no backdrop
202 if (!backdrop) return;
204 int box_w = 220+110+110;
205 int box_h = 30+20+20+20;
206 int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
207 int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
209 int bd_w = (int)backdrop->get_width();
210 int bd_h = (int)backdrop->get_height();
211 int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
212 int bd_y = box_y + (box_h / 2) - (bd_h / 2);
215 int col2_x = col1_x+200;
216 int col3_x = col2_x+130;
219 int row2_y = row1_y+30;
220 int row3_y = row2_y+20;
221 int row4_y = row3_y+20;
223 context.push_transform();
224 context.set_alpha(0.5);
225 context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
226 context.pop_transform();
229 context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
230 context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
232 context.draw_text(white_text, _("Coins"), Vector(col2_x-16, row2_y), RIGHT_ALLIGN, LAYER_GUI);
233 snprintf(buf, sizeof(buf), "%d/%d", std::min(coins, 999), std::min(total_coins, 999));
234 context.draw_text(gold_text, buf, Vector(col2_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
235 if (best_stats && (best_stats->coins > coins)) {
236 snprintf(buf, sizeof(buf), "%d/%d", std::min(best_stats->coins, 999), std::min(best_stats->total_coins, 999));
238 context.draw_text(gold_text, buf, Vector(col3_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
240 context.draw_text(white_text, _("Secrets"), Vector(col2_x-16, row4_y), RIGHT_ALLIGN, LAYER_GUI);
241 snprintf(buf, sizeof(buf), "%d/%d", secrets, total_secrets);
242 context.draw_text(gold_text, buf, Vector(col2_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
243 if (best_stats && (best_stats->secrets > secrets)) {
244 snprintf(buf, sizeof(buf), "%d/%d", best_stats->secrets, best_stats->total_secrets);
246 context.draw_text(gold_text, buf, Vector(col3_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
248 context.draw_text(white_text, _("Time"), Vector(col2_x-16, row3_y), RIGHT_ALLIGN, LAYER_GUI);
249 int csecs = (int)(time * 100);
250 int mins = (int)(csecs / 6000);
251 int secs = (csecs % 6000) / 100;
252 snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
253 context.draw_text(gold_text, buf, Vector(col2_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
254 if (best_stats && (best_stats->time < time)) {
255 int csecs = (int)(best_stats->time * 100);
256 int mins = (int)(csecs / 6000);
257 int secs = (csecs % 6000) / 100;
258 snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
260 context.draw_text(gold_text, buf, Vector(col3_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
273 Statistics::merge(Statistics& s2)
275 if (!s2.valid) return;
276 coins = std::max(coins, s2.coins);
277 total_coins = s2.total_coins;
278 badguys = std::max(badguys, s2.badguys);
279 total_badguys = s2.total_badguys;
280 time = std::min(time, s2.time);
281 secrets = std::max(secrets, s2.secrets);
282 total_secrets = s2.total_secrets;
286 Statistics::operator+=(const Statistics& s2)
288 if (!s2.valid) return;
289 if (s2.coins != nv_coins) coins += s2.coins;
290 if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
291 if (s2.badguys != nv_badguys) badguys += s2.badguys;
292 if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
293 if (s2.time != nv_time) time += s2.time;
294 if (s2.secrets != nv_secrets) secrets += s2.secrets;
295 if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
299 Statistics::declare_invalid()