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.
25 #include "video/drawing_context.hpp"
26 #include "gettext.hpp"
27 #include "lisp/lisp.hpp"
28 #include "resources.hpp"
30 #include "statistics.hpp"
34 const int nv_coins = std::numeric_limits<int>::min();
35 const int nv_badguys = std::numeric_limits<int>::min();
36 const float nv_time = std::numeric_limits<float>::max();
37 const int nv_secrets = std::numeric_limits<int>::min();
40 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), display_stat(0)
44 Statistics::~Statistics()
49 Statistics::parse(const lisp::Lisp& reader)
51 reader.get("coins-collected", coins);
52 reader.get("coins-collected-total", total_coins);
53 reader.get("badguys-killed", badguys);
54 reader.get("badguys-killed-total", total_badguys);
55 reader.get("time-needed", time);
56 reader.get("secrets-found", secrets);
57 reader.get("secrets-found-total", total_secrets);
61 Statistics::write(lisp::Writer& writer)
63 writer.write_int("coins-collected", coins);
64 writer.write_int("coins-collected-total", total_coins);
65 writer.write_int("badguys-killed", badguys);
66 writer.write_int("badguys-killed-total", total_badguys);
67 writer.write_float("time-needed", time);
68 writer.write_int("secrets-found", secrets);
69 writer.write_int("secrets-found-total", total_secrets);
72 //define TOTAL_DISPLAY_TIME 3400
73 //define FADING_TIME 600
75 #define TOTAL_DISPLAY_TIME 5
78 const float WMAP_INFO_LEFT_X = (800 - 320) + 32;
79 const float WMAP_INFO_RIGHT_X = 800 - 32;
80 const float WMAP_INFO_TOP_Y1 = 600 - 128 - 16;
81 const float WMAP_INFO_TOP_Y2 = 600 - 128;
84 inline const char* chain(const char* c1, const char* c2) {
85 return (std::string(c1) + std::string(c2)).c_str();
87 inline const char* chain(const char* c1, const char* c2, const char* c3) {
88 return (std::string(c1) + std::string(c2) + std::string(c3)).c_str();
93 Statistics::draw_worldmap_info(DrawingContext& context)
95 // skip draw if level was never played
96 if (coins == nv_coins) return;
98 context.draw_text(white_small_text, ::chain("- ", _("Best Level Statistics"), " -"), Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1), CENTER_ALLIGN, LAYER_GUI);
101 if(timer.get_timegone() < FADING_TIME)
102 alpha = (timer.get_timegone() * 1.0f / FADING_TIME);
103 else if(timer.get_timeleft() < FADING_TIME)
104 alpha = (timer.get_timeleft() * 1.0f / FADING_TIME);
108 context.push_transform();
109 context.set_alpha(alpha);
111 char caption_buf[128];
113 switch (display_stat)
116 sprintf(caption_buf, _("Max coins collected:"));
117 sprintf(stat_buf, "%d/%d", coins, total_coins);
120 sprintf(caption_buf, _("Max fragging:"));
121 sprintf(stat_buf, "%d/%d", badguys, total_badguys);
124 sprintf(caption_buf, _("Min time needed:"));
126 int csecs = (int)(time * 100);
127 int mins = (int)(csecs / 6000);
128 int secs = (csecs % 6000) / 100;
129 sprintf(stat_buf, "%02d:%02d", mins,secs);
133 sprintf(caption_buf, _("Max secrets found:"));
134 sprintf(stat_buf, "%d/%d", secrets, total_secrets);
137 log_debug << "Invalid stat requested to be drawn" << std::endl;
141 if (!timer.started())
143 timer.start(TOTAL_DISPLAY_TIME);
145 if (display_stat > 3) display_stat = 0;
148 context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, WMAP_INFO_TOP_Y2), LEFT_ALLIGN, LAYER_GUI);
149 context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, WMAP_INFO_TOP_Y2), RIGHT_ALLIGN, LAYER_GUI);
150 context.pop_transform();
154 Statistics::draw_message_info(DrawingContext& context, std::string title)
156 // skip draw if level was never played
157 // TODO: do we need this?
158 if (coins == nv_coins) return;
160 context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
165 sprintf(str, _("Max coins collected: %d / %d"), coins, total_coins);
166 context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI);
169 sprintf(str, _("Max fragging: %d / %d"), badguys, total_badguys);
170 context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI);
173 int csecs = (int)(time * 100);
174 int mins = (int)(csecs / 6000);
175 int secs = (csecs % 6000) / 100;
176 sprintf(str, _("Min time needed: %02d:%02d"), mins,secs);
177 context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI);
180 sprintf(str, _("Max secrets found: %d / %d"), secrets, total_secrets);
181 context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI);
186 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
188 // skip draw if level was never played
189 // TODO: do we need this?
190 if (coins == nv_coins) return;
192 // abort if we have no backdrop
193 if (!backdrop) return;
195 int box_w = 160+110+110;
196 int box_h = 30+20+20+20;
197 int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
198 int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
200 int bd_w = (int)backdrop->get_width();
201 int bd_h = (int)backdrop->get_height();
202 int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
203 int bd_y = box_y + (box_h / 2) - (bd_h / 2);
206 int col2_x = col1_x+160;
207 int col3_x = col2_x+110;
210 int row2_y = row1_y+30;
211 int row3_y = row2_y+20;
212 int row4_y = row3_y+20;
214 context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
217 context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
218 context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
220 context.draw_text(white_text, _("Coins"), Vector(col1_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
221 snprintf(buf, 128, "%d/%d", coins, total_coins);
222 context.draw_text(gold_text, buf, Vector(col2_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
223 if (best_stats && (best_stats->coins > coins)) {
224 snprintf(buf, 128, "%d/%d", best_stats->coins, best_stats->total_coins);
226 context.draw_text(gold_text, buf, Vector(col3_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
228 context.draw_text(white_text, _("Secrets"), Vector(col1_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
229 snprintf(buf, 128, "%d/%d", secrets, total_secrets);
230 context.draw_text(gold_text, buf, Vector(col2_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
231 if (best_stats && (best_stats->secrets > secrets)) {
232 snprintf(buf, 128, "%d/%d", best_stats->secrets, best_stats->total_secrets);
234 context.draw_text(gold_text, buf, Vector(col3_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
236 context.draw_text(white_text, _("Time"), Vector(col1_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
237 int csecs = (int)(time * 100);
238 int mins = (int)(csecs / 6000);
239 int secs = (csecs % 6000) / 100;
240 snprintf(buf, 128, "%02d:%02d", mins,secs);
241 context.draw_text(gold_text, buf, Vector(col2_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
242 if (best_stats && (best_stats->time < time)) {
243 int csecs = (int)(best_stats->time * 100);
244 int mins = (int)(csecs / 6000);
245 int secs = (csecs % 6000) / 100;
246 snprintf(buf, 128, "%02d:%02d", mins,secs);
248 context.draw_text(gold_text, buf, Vector(col3_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
261 Statistics::merge(Statistics& s2)
263 coins = std::max(coins, s2.coins);
264 total_coins = s2.total_coins;
265 badguys = std::max(badguys, s2.badguys);
266 total_badguys = s2.total_badguys;
267 time = std::min(time, s2.time);
268 secrets = std::max(secrets, s2.secrets);
269 total_secrets = s2.total_secrets;
273 Statistics::operator+=(const Statistics& s2)
275 if (s2.coins != nv_coins) coins += s2.coins;
276 if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
277 if (s2.badguys != nv_badguys) badguys += s2.badguys;
278 if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
279 if (s2.time != nv_time) time += s2.time;
280 if (s2.secrets != nv_secrets) secrets += s2.secrets;
281 if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;