fix cr/lfs and remove trailing whitespaces...
[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 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)
42 {
43 }
44
45 Statistics::~Statistics()
46 {
47 }
48
49 void
50 Statistics::parse(const lisp::Lisp& reader)
51 {
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);
59 }
60
61 void
62 Statistics::write(lisp::Writer& writer)
63 {
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);
71 }
72
73 //define TOTAL_DISPLAY_TIME  3400
74 //define FADING_TIME          600
75
76 #define TOTAL_DISPLAY_TIME  5
77 #define FADING_TIME         1
78
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;
83
84 void
85 Statistics::draw_worldmap_info(DrawingContext& context)
86 {
87   // skip draw if level was never played
88   if (coins == nv_coins) return;
89
90   // skip draw if stats were declared invalid
91   if (!valid) return;
92
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);
94
95   float alpha;
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);
100   else
101     alpha = 1.0f;
102
103   context.push_transform();
104   context.set_alpha(alpha);
105
106   char caption_buf[128];
107   char stat_buf[128];
108   switch (display_stat)
109   {
110     case 0:
111       snprintf(caption_buf, sizeof(caption_buf), _("Max coins collected:"));
112       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
113       break;
114     case 1:
115       snprintf(caption_buf, sizeof(caption_buf), _("Max fragging:"));
116       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
117       break;
118     case 2:
119       snprintf(caption_buf, sizeof(caption_buf), _("Min time needed:"));
120       {
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);
125       }
126       break;
127     case 3:
128       snprintf(caption_buf, sizeof(caption_buf), _("Max secrets found:"));
129       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
130       break;
131     default:
132       log_debug << "Invalid stat requested to be drawn" << std::endl;
133       break;
134   }
135
136   if (!timer.started())
137   {
138     timer.start(TOTAL_DISPLAY_TIME);
139     display_stat++;
140     if (display_stat > 3) display_stat = 0;
141   }
142
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();
146 }
147
148 void
149 Statistics::draw_message_info(DrawingContext& context, std::string title)
150 {
151   // skip draw if level was never played
152   // TODO: do we need this?
153   if (coins == nv_coins) return;
154
155   // skip draw if stats were declared invalid
156   if (!valid) return;
157
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;
161
162   context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
163
164   char stat_buf[128];
165   int py = 450 + 18;
166
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);
170   py+=18;
171
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);
175   py+=18;
176
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);
183   py+=18;
184
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);
188   py+=18;
189 }
190
191 void
192 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
193 {
194   // skip draw if level was never played
195   // TODO: do we need this?
196   if (coins == nv_coins) return;
197
198   // skip draw if stats were declared invalid
199   if (!valid) return;
200
201   // abort if we have no backdrop
202   if (!backdrop) return;
203
204   int box_w = 160+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;
208
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);
213
214   int col1_x = box_x;
215   int col2_x = col1_x+160;
216   int col3_x = col2_x+110;
217
218   int row1_y = box_y;
219   int row2_y = row1_y+30;
220   int row3_y = row2_y+20;
221   int row4_y = row3_y+20;
222
223   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
224
225   char buf[129];
226   context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
227   context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
228
229   context.draw_text(white_text, _("Coins"), Vector(col1_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
230   snprintf(buf, sizeof(buf), "%d/%d", coins, total_coins);
231   context.draw_text(gold_text, buf, Vector(col2_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
232   if (best_stats && (best_stats->coins > coins)) {
233     snprintf(buf, sizeof(buf), "%d/%d", best_stats->coins, best_stats->total_coins);
234   }
235   context.draw_text(gold_text, buf, Vector(col3_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
236
237   context.draw_text(white_text, _("Secrets"), Vector(col1_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
238   snprintf(buf, sizeof(buf), "%d/%d", secrets, total_secrets);
239   context.draw_text(gold_text, buf, Vector(col2_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
240   if (best_stats && (best_stats->secrets > secrets)) {
241     snprintf(buf, sizeof(buf), "%d/%d", best_stats->secrets, best_stats->total_secrets);
242   }
243   context.draw_text(gold_text, buf, Vector(col3_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
244
245   context.draw_text(white_text, _("Time"), Vector(col1_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
246   int csecs = (int)(time * 100);
247   int mins = (int)(csecs / 6000);
248   int secs = (csecs % 6000) / 100;
249   snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
250   context.draw_text(gold_text, buf, Vector(col2_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
251   if (best_stats && (best_stats->time < time)) {
252     int csecs = (int)(best_stats->time * 100);
253     int mins = (int)(csecs / 6000);
254     int secs = (csecs % 6000) / 100;
255     snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
256   }
257   context.draw_text(gold_text, buf, Vector(col3_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
258 }
259
260 void
261 Statistics::reset()
262 {
263   coins = 0;
264   badguys = 0;
265   time = 0;
266   secrets = 0;
267 }
268
269 void
270 Statistics::merge(Statistics& s2)
271 {
272   if (!s2.valid) return;
273   coins = std::max(coins, s2.coins);
274   total_coins = s2.total_coins;
275   badguys = std::max(badguys, s2.badguys);
276   total_badguys = s2.total_badguys;
277   time = std::min(time, s2.time);
278   secrets = std::max(secrets, s2.secrets);
279   total_secrets = s2.total_secrets;
280 }
281
282 void
283 Statistics::operator+=(const Statistics& s2)
284 {
285   if (!s2.valid) return;
286   if (s2.coins != nv_coins) coins += s2.coins;
287   if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
288   if (s2.badguys != nv_badguys) badguys += s2.badguys;
289   if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
290   if (s2.time != nv_time) time += s2.time;
291   if (s2.secrets != nv_secrets) secrets += s2.secrets;
292   if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
293 }
294
295 void
296 Statistics::declare_invalid()
297 {
298   valid = false;
299 }