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