d08f7b0d870059def62424a38ed1e1938531fd43
[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 "video/drawing_context.hpp"
26 #include "gettext.hpp"
27 #include "lisp/lisp.hpp"
28 #include "resources.hpp"
29 #include "main.hpp"
30 #include "statistics.hpp"
31 #include "log.hpp"
32
33 namespace {
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();
38 }
39
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)
41 {
42 }
43
44 Statistics::~Statistics()
45 {
46 }
47
48 void
49 Statistics::parse(const lisp::Lisp& reader)
50 {
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);
58 }
59
60 void
61 Statistics::write(lisp::Writer& writer)
62 {
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);
70 }
71
72 //define TOTAL_DISPLAY_TIME  3400
73 //define FADING_TIME          600
74
75 #define TOTAL_DISPLAY_TIME  5
76 #define FADING_TIME         1
77
78 #define WMAP_INFO_LEFT_X  520
79 #define WMAP_INFO_RIGHT_X 740
80
81 void
82 Statistics::draw_worldmap_info(DrawingContext& context)
83 {
84   // skip draw if level was never played
85   if (coins == nv_coins) return;
86
87   context.draw_text(white_small_text, _("- Best Level Statistics -"), Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, 470), CENTER_ALLIGN, LAYER_GUI);
88
89   float alpha;
90   if(timer.get_timegone() < FADING_TIME)
91     alpha = (timer.get_timegone() * 1.0f / FADING_TIME);
92   else if(timer.get_timeleft() < FADING_TIME)
93     alpha = (timer.get_timeleft() * 1.0f / FADING_TIME);
94   else
95     alpha = 1.0f;
96
97   context.push_transform();
98   context.set_alpha(alpha);
99
100   char caption_buf[128];
101   char stat_buf[128];
102   switch (display_stat) 
103   {
104     case 0:
105       sprintf(caption_buf, _("Max coins collected:"));
106       sprintf(stat_buf, "%d/%d", coins, total_coins);
107       break;
108     case 1:
109       sprintf(caption_buf, _("Max fragging:"));
110       sprintf(stat_buf, "%d/%d", badguys, total_badguys);
111       break;
112     case 2:
113       sprintf(caption_buf, _("Min time needed:"));
114       {
115         int csecs = (int)(time * 100);
116         int mins = (int)(csecs / 6000);
117         int secs = (csecs % 6000) / 100;
118         sprintf(stat_buf, "%02d:%02d", mins,secs); 
119       }
120       break;
121     case 3:
122       sprintf(caption_buf, _("Max secrets found:"));
123       sprintf(stat_buf, "%d/%d", secrets, total_secrets);
124     default:
125       log_debug << "Invalid stat requested to be drawn" << std::endl;
126       break;
127   }
128
129   if (!timer.started())
130   {
131     timer.start(TOTAL_DISPLAY_TIME);
132     display_stat++;
133     if (display_stat > 3) display_stat = 0;
134   }
135
136   context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, 490), LEFT_ALLIGN, LAYER_GUI);
137   context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, 490), RIGHT_ALLIGN, LAYER_GUI);
138   context.pop_transform();
139 }
140
141 void
142 Statistics::draw_message_info(DrawingContext& context, std::string title)
143 {
144   // skip draw if level was never played
145   // TODO: do we need this?
146   if (coins == nv_coins) return;
147
148   context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
149
150   char str[128];
151   int py = 450 + 18;
152
153   sprintf(str, _("Max coins collected:   %d / %d"), coins, total_coins);
154   context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI); 
155   py+=18;
156
157   sprintf(str, _("Max fragging:          %d / %d"), badguys, total_badguys);
158   context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI); 
159   py+=18;
160
161   int csecs = (int)(time * 100);
162   int mins = (int)(csecs / 6000);
163   int secs = (csecs % 6000) / 100;
164   sprintf(str, _("Min time needed:       %02d:%02d"), mins,secs);
165   context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI); 
166   py+=18;
167   
168   sprintf(str, _("Max secrets found:     %d / %d"), secrets, total_secrets);
169   context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, py), CENTER_ALLIGN, LAYER_GUI); 
170   py+=18;
171 }
172
173 void 
174 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
175 {
176   // skip draw if level was never played
177   // TODO: do we need this?
178   if (coins == nv_coins) return;
179
180   // abort if we have no backdrop
181   if (!backdrop) return;
182
183   int box_w = 130+130+130;
184   int box_h = 30+20+20+20;
185   int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
186   int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
187
188   int bd_w = (int)backdrop->get_width();
189   int bd_h = (int)backdrop->get_height();
190   int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
191   int bd_y = box_y + (box_h / 2) - (bd_h / 2);
192
193   int col1_x = box_x;
194   int col2_x = col1_x+130;
195   int col3_x = col2_x+130;
196
197   int row1_y = box_y;
198   int row2_y = row1_y+30;
199   int row3_y = row2_y+20;
200   int row4_y = row3_y+20;
201
202   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
203
204   char buf[129];
205   context.draw_text(white_text, "You", Vector(col2_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
206   context.draw_text(white_text, "Best", Vector(col3_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
207
208   context.draw_text(white_text, "Coins", Vector(col1_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
209   snprintf(buf, 128, "%d/%d", coins, total_coins);
210   context.draw_text(gold_text, buf, Vector(col2_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
211   if (best_stats && (best_stats->coins > coins)) {
212     snprintf(buf, 128, "%d/%d", best_stats->coins, best_stats->total_coins);
213   }
214   context.draw_text(gold_text, buf, Vector(col3_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
215
216   context.draw_text(white_text, "Secrets", Vector(col1_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
217   snprintf(buf, 128, "%d/%d", secrets, total_secrets);
218   context.draw_text(gold_text, buf, Vector(col2_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
219   if (best_stats && (best_stats->secrets > secrets)) {
220     snprintf(buf, 128, "%d/%d", best_stats->secrets, best_stats->total_secrets);
221   }
222   context.draw_text(gold_text, buf, Vector(col3_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
223
224   context.draw_text(white_text, "Time", Vector(col1_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
225   int csecs = (int)(time * 100);
226   int mins = (int)(csecs / 6000);
227   int secs = (csecs % 6000) / 100;
228   snprintf(buf, 128, "%02d:%02d", mins,secs);
229   context.draw_text(gold_text, buf, Vector(col2_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
230   if (best_stats && (best_stats->time < time)) {
231     int csecs = (int)(best_stats->time * 100);
232     int mins = (int)(csecs / 6000);
233     int secs = (csecs % 6000) / 100;
234     snprintf(buf, 128, "%02d:%02d", mins,secs);
235   }
236   context.draw_text(gold_text, buf, Vector(col3_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
237 }
238
239 void
240 Statistics::reset()
241 {
242   coins = 0; 
243   badguys = 0; 
244   time = 0; 
245   secrets = 0; 
246 }
247
248 void
249 Statistics::merge(Statistics& s2)
250 {
251   coins = std::max(coins, s2.coins);
252   total_coins = s2.total_coins;
253   badguys = std::max(badguys, s2.badguys);
254   total_badguys = s2.total_badguys;
255   time = std::min(time, s2.time);
256   secrets = std::max(secrets, s2.secrets);
257   total_secrets = s2.total_secrets;
258 }
259
260 void
261 Statistics::operator+=(const Statistics& s2)
262 {
263   if (s2.coins != nv_coins) coins += s2.coins;
264   if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
265   if (s2.badguys != nv_badguys) badguys += s2.badguys;
266   if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
267   if (s2.time != nv_time) time += s2.time;
268   if (s2.secrets != nv_secrets) secrets += s2.secrets;
269   if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
270 }