Klaus Denker's widescreen_fixes.patch:
[supertux.git] / src / statistics.cpp
1 //  $Id$
2 //
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>
7 //
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.
12 //
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.
17 //
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.
21 #include <config.h>
22
23 #include <assert.h>
24 #include <math.h>
25 #include <sstream>
26 #include "video/drawing_context.hpp"
27 #include "gettext.hpp"
28 #include "lisp/lisp.hpp"
29 #include "resources.hpp"
30 #include "main.hpp"
31 #include "statistics.hpp"
32 #include "log.hpp"
33
34 namespace {
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();
39 }
40
41 float WMAP_INFO_LEFT_X;
42 float WMAP_INFO_RIGHT_X;
43 float WMAP_INFO_TOP_Y1;
44 float WMAP_INFO_TOP_Y2;
45
46 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)
47 {
48   WMAP_INFO_LEFT_X = (SCREEN_WIDTH/2 + 80) + 32;
49   WMAP_INFO_RIGHT_X = SCREEN_WIDTH/2 + 368;
50   WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT/2 + 172 - 16;
51   WMAP_INFO_TOP_Y2 = SCREEN_HEIGHT/2 + 172;
52 }
53
54 Statistics::~Statistics()
55 {
56 }
57
58 void
59 Statistics::parse(const lisp::Lisp& reader)
60 {
61   reader.get("coins-collected", coins);
62   reader.get("coins-collected-total", total_coins);
63   reader.get("badguys-killed", badguys);
64   reader.get("badguys-killed-total", total_badguys);
65   reader.get("time-needed", time);
66   reader.get("secrets-found", secrets);
67   reader.get("secrets-found-total", total_secrets);
68 }
69
70 void
71 Statistics::write(lisp::Writer& writer)
72 {
73   writer.write_int("coins-collected", coins);
74   writer.write_int("coins-collected-total", total_coins);
75   writer.write_int("badguys-killed", badguys);
76   writer.write_int("badguys-killed-total", total_badguys);
77   writer.write_float("time-needed", time);
78   writer.write_int("secrets-found", secrets);
79   writer.write_int("secrets-found-total", total_secrets);
80 }
81
82 //define TOTAL_DISPLAY_TIME  3400
83 //define FADING_TIME          600
84
85 #define TOTAL_DISPLAY_TIME  5
86 #define FADING_TIME         1
87
88 void
89 Statistics::draw_worldmap_info(DrawingContext& context)
90 {
91   // skip draw if level was never played
92   if (coins == nv_coins) return;
93
94   // skip draw if stats were declared invalid
95   if (!valid) return;
96
97   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);
98
99   float alpha;
100   if(timer.get_timegone() < FADING_TIME)
101     alpha = (timer.get_timegone() * 1.0f / FADING_TIME);
102   else if(timer.get_timeleft() < FADING_TIME)
103     alpha = (timer.get_timeleft() * 1.0f / FADING_TIME);
104   else
105     alpha = 1.0f;
106
107   context.push_transform();
108   context.set_alpha(alpha);
109
110   char caption_buf[128];
111   char stat_buf[128];
112   switch (display_stat)
113   {
114     case 0:
115       snprintf(caption_buf, sizeof(caption_buf), _("Max coins collected:"));
116       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
117       break;
118     case 1:
119       snprintf(caption_buf, sizeof(caption_buf), _("Max fragging:"));
120       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
121       break;
122     case 2:
123       snprintf(caption_buf, sizeof(caption_buf), _("Min time needed:"));
124       {
125         int csecs = (int)(time * 100);
126         int mins = (int)(csecs / 6000);
127         int secs = (csecs % 6000) / 100;
128         snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
129       }
130       break;
131     case 3:
132       snprintf(caption_buf, sizeof(caption_buf), _("Max secrets found:"));
133       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
134       break;
135     default:
136       log_debug << "Invalid stat requested to be drawn" << std::endl;
137       break;
138   }
139
140   if (!timer.started())
141   {
142     timer.start(TOTAL_DISPLAY_TIME);
143     display_stat++;
144     if (display_stat > 3) display_stat = 0;
145   }
146
147   context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, WMAP_INFO_TOP_Y2), LEFT_ALLIGN, LAYER_GUI);
148   context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, WMAP_INFO_TOP_Y2), RIGHT_ALLIGN, LAYER_GUI);
149   context.pop_transform();
150 }
151
152 void
153 Statistics::draw_message_info(DrawingContext& context, std::string title)
154 {
155   // skip draw if level was never played
156   // TODO: do we need this?
157   if (coins == nv_coins) return;
158
159   // skip draw if stats were declared invalid
160   if (!valid) return;
161
162   const float width = white_small_text->get_text_width("Max coins collected: 1111 / 1111");
163   const float left = (SCREEN_WIDTH - width) / 2;
164   const float right = (SCREEN_WIDTH + width) / 2;
165
166   context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
167
168   char stat_buf[128];
169   int py = 450 + 18;
170
171   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
172   context.draw_text(white_small_text, _("Max coins collected:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
173   context.draw_text(white_small_text, "%d / %d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
174   py+=18;
175
176   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
177   context.draw_text(white_small_text, _("Max fragging:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
178   context.draw_text(white_small_text, "%d / %d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
179   py+=18;
180
181   int csecs = (int)(time * 100);
182   int mins = (int)(csecs / 6000);
183   int secs = (csecs % 6000) / 100;
184   snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
185   context.draw_text(white_small_text, _("Min time needed:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
186   context.draw_text(white_small_text, "%02d:%02d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
187   py+=18;
188
189   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
190   context.draw_text(white_small_text, _("Max secrets found:"), Vector(left, py), LEFT_ALLIGN, LAYER_GUI);
191   context.draw_text(white_small_text, "%d / %d", Vector(right, py), RIGHT_ALLIGN, LAYER_GUI);
192   py+=18;
193 }
194
195 void
196 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
197 {
198   // skip draw if level was never played
199   // TODO: do we need this?
200   if (coins == nv_coins) return;
201
202   // skip draw if stats were declared invalid
203   if (!valid) return;
204
205   // abort if we have no backdrop
206   if (!backdrop) return;
207
208   int box_w = 220+110+110;
209   int box_h = 30+20+20+20;
210   int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
211   int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
212
213   int bd_w = (int)backdrop->get_width();
214   int bd_h = (int)backdrop->get_height();
215   int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
216   int bd_y = box_y + (box_h / 2) - (bd_h / 2);
217
218   int col1_x = box_x;
219   int col2_x = col1_x+200;
220   int col3_x = col2_x+130;
221
222   int row1_y = box_y;
223   int row2_y = row1_y+30;
224   int row3_y = row2_y+20;
225   int row4_y = row3_y+20;
226
227   context.push_transform();
228   context.set_alpha(0.5);
229   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
230   context.pop_transform();
231
232   char buf[129];
233   context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
234   context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
235
236   context.draw_text(white_text, _("Coins"), Vector(col2_x-16, row2_y), RIGHT_ALLIGN, LAYER_GUI);
237   snprintf(buf, sizeof(buf), "%d/%d", std::min(coins, 999), std::min(total_coins, 999));
238   context.draw_text(gold_text, buf, Vector(col2_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
239   if (best_stats && (best_stats->coins > coins)) {
240     snprintf(buf, sizeof(buf), "%d/%d", std::min(best_stats->coins, 999), std::min(best_stats->total_coins, 999));
241   }
242   context.draw_text(gold_text, buf, Vector(col3_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
243
244   context.draw_text(white_text, _("Secrets"), Vector(col2_x-16, row4_y), RIGHT_ALLIGN, LAYER_GUI);
245   snprintf(buf, sizeof(buf), "%d/%d", secrets, total_secrets);
246   context.draw_text(gold_text, buf, Vector(col2_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
247   if (best_stats && (best_stats->secrets > secrets)) {
248     snprintf(buf, sizeof(buf), "%d/%d", best_stats->secrets, best_stats->total_secrets);
249   }
250   context.draw_text(gold_text, buf, Vector(col3_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
251
252   context.draw_text(white_text, _("Time"), Vector(col2_x-16, row3_y), RIGHT_ALLIGN, LAYER_GUI);
253   int csecs = (int)(time * 100);
254   int mins = (int)(csecs / 6000);
255   int secs = (csecs % 6000) / 100;
256   snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
257   context.draw_text(gold_text, buf, Vector(col2_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
258   if (best_stats && (best_stats->time < time)) {
259     int csecs = (int)(best_stats->time * 100);
260     int mins = (int)(csecs / 6000);
261     int secs = (csecs % 6000) / 100;
262     snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
263   }
264   context.draw_text(gold_text, buf, Vector(col3_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
265 }
266
267 void
268 Statistics::reset()
269 {
270   coins = 0;
271   badguys = 0;
272   time = 0;
273   secrets = 0;
274 }
275
276 void
277 Statistics::merge(Statistics& s2)
278 {
279   if (!s2.valid) return;
280   coins = std::max(coins, s2.coins);
281   total_coins = s2.total_coins;
282   badguys = std::max(badguys, s2.badguys);
283   total_badguys = s2.total_badguys;
284   time = std::min(time, s2.time);
285   secrets = std::max(secrets, s2.secrets);
286   total_secrets = s2.total_secrets;
287 }
288
289 void
290 Statistics::operator+=(const Statistics& s2)
291 {
292   if (!s2.valid) return;
293   if (s2.coins != nv_coins) coins += s2.coins;
294   if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
295   if (s2.badguys != nv_badguys) badguys += s2.badguys;
296   if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
297   if (s2.time != nv_time) time += s2.time;
298   if (s2.secrets != nv_secrets) secrets += s2.secrets;
299   if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
300 }
301
302 void
303 Statistics::declare_invalid()
304 {
305   valid = false;
306 }