4e32a1b7c01f31d986758e8e042123bcd7a4a0f7
[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 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <config.h>
21
22 #include <assert.h>
23 #include "video/drawing_context.hpp"
24 #include "gettext.hpp"
25 #include "lisp/lisp.hpp"
26 #include "resources.hpp"
27 #include "main.hpp"
28 #include "statistics.hpp"
29
30 std::string
31 stat_name_to_string(int stat_enum)
32 {
33   switch(stat_enum)
34     {
35 //    case SCORE_STAT:
36 //      return "score";
37     case COINS_COLLECTED_STAT:
38       return "coins-collected";
39     case BADGUYS_KILLED_STAT:
40       return "badguys-killed";
41     case TIME_NEEDED_STAT:
42       return "time-needed";;
43     }
44   return "";
45 }
46
47 int
48 my_min(int a, int b)
49 {
50 if(a == -1)
51   return b;
52 if(b == -1)
53   return a;
54 return std::min(a, b);
55 }
56
57 Statistics::Statistics()
58 {
59   display_stat = 1;
60
61   for(int i = 0; i < NUM_STATS; i++)
62     for(int j = 0; j < 2; j++)
63       stats[i][j] = -1;
64 }
65
66 Statistics::~Statistics()
67 {
68 }
69
70 void
71 Statistics::parse(const lisp::Lisp& reader)
72 {
73   for(int i = 0; i < NUM_STATS; i++) {
74     reader.get(stat_name_to_string(i).c_str(), stats[i][SPLAYER]);
75     reader.get((stat_name_to_string(i) + "-total").c_str(), stats[i][STOTAL]);
76   }
77 }
78
79 void
80 Statistics::write(lisp::Writer& writer)
81 {
82   for(int i = 0; i < NUM_STATS; i++) {
83     writer.write_int(stat_name_to_string(i), stats[i][SPLAYER]);
84     writer.write_int(stat_name_to_string(i) + "-total", stats[i][STOTAL]);
85   }
86 }
87
88 //define TOTAL_DISPLAY_TIME  3400
89 //define FADING_TIME          600
90
91 #define TOTAL_DISPLAY_TIME  5
92 #define FADING_TIME         1
93
94 #define WMAP_INFO_LEFT_X  520
95 #define WMAP_INFO_RIGHT_X 740
96
97 void
98 Statistics::draw_worldmap_info(DrawingContext& context)
99 {
100   if(stats[COINS_COLLECTED_STAT][SPLAYER] == -1)  // not initialized yet
101     return;
102
103 //  if(timer.check())
104   if (!timer.started())
105   {
106     timer.start(TOTAL_DISPLAY_TIME);
107     display_stat++;
108     if(display_stat >= NUM_STATS)
109       display_stat = 0;
110
111     if((display_stat == TIME_NEEDED_STAT) && (stats[TIME_NEEDED_STAT][STOTAL] == -1))
112     { // no timer in level
113       display_stat++;
114       if(display_stat >= NUM_STATS)
115         display_stat = 0;
116     }
117   }
118
119   char str[128];
120
121   context.draw_text(white_small_text, _("- Best Level Statistics -"),
122                     Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, 470),
123                     CENTER_ALLIGN, LAYER_GUI);
124
125   // Score has been removed
126   //sprintf(str, _("Max score:"));
127   //context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 490), LEFT_ALLIGN, LAYER_GUI);
128
129   //sprintf(str, "%d", stats[SCORE_STAT][SPLAYER]);
130   //context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 490), RIGHT_ALLIGN, LAYER_GUI);
131
132   float alpha;
133   if(timer.get_timegone() < FADING_TIME)
134     alpha = (timer.get_timegone() * 1.0f / FADING_TIME);
135   else if(timer.get_timeleft() < FADING_TIME)
136     alpha = (timer.get_timeleft() * 1.0f / FADING_TIME);
137   else
138     alpha = 1.0f;
139
140   context.push_transform();
141   context.set_alpha(alpha);
142
143   if(display_stat == COINS_COLLECTED_STAT)
144     sprintf(str, _("Max coins collected:"));
145   else if(display_stat == BADGUYS_KILLED_STAT)
146     sprintf(str, _("Max fragging:"));
147   else// if(display_stat == TIME_NEEDED_STAT)
148     sprintf(str, _("Min time needed:"));
149
150   // y == 508 before score was removed
151   context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 490), LEFT_ALLIGN, LAYER_GUI);
152
153   if(display_stat == COINS_COLLECTED_STAT)
154     sprintf(str, "%d/%d", stats[COINS_COLLECTED_STAT][SPLAYER],
155                           stats[COINS_COLLECTED_STAT][STOTAL]);
156   else if(display_stat == BADGUYS_KILLED_STAT)
157     sprintf(str, "%d/%d", stats[BADGUYS_KILLED_STAT][SPLAYER],
158                           stats[BADGUYS_KILLED_STAT][STOTAL]);
159   else// if(display_stat == TIME_NEEDED_STAT)
160     sprintf(str, "%d/%d", stats[TIME_NEEDED_STAT][SPLAYER],
161                           stats[TIME_NEEDED_STAT][STOTAL]);
162
163   context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 490), RIGHT_ALLIGN, LAYER_GUI);
164
165   context.pop_transform();
166 }
167
168 void
169 Statistics::draw_message_info(DrawingContext& context, std::string title)
170 {
171   if(stats[COINS_COLLECTED_STAT][SPLAYER] == -1)  // not initialized yet
172     return;
173
174   context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
175
176   char str[128];
177
178   //sprintf(str, _(    "Max score:             %d"), stats[SCORE_STAT][SPLAYER]);
179   //context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 450), CENTER_ALLIGN, LAYER_GUI);
180
181   for(int i = 0; i < NUM_STATS; i++)
182     {
183     if(i == COINS_COLLECTED_STAT)
184       sprintf(str, _("Max coins collected:   %d / %d"),
185               stats[COINS_COLLECTED_STAT][SPLAYER],
186               stats[COINS_COLLECTED_STAT][STOTAL]);
187     else if(i == BADGUYS_KILLED_STAT)
188       sprintf(str, _("Max fragging:          %d / %d"),
189               stats[BADGUYS_KILLED_STAT][SPLAYER],
190               stats[BADGUYS_KILLED_STAT][STOTAL]);
191     else if((i == TIME_NEEDED_STAT) && (stats[TIME_NEEDED_STAT][STOTAL] != -1))
192       sprintf(str, _("Min time needed:       %d / %d"),
193               stats[TIME_NEEDED_STAT][SPLAYER],
194               stats[TIME_NEEDED_STAT][STOTAL]);
195     else
196       continue;
197
198
199     // y == (462 + i*18) before score removal
200     context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, 450 + (i+1)*18), CENTER_ALLIGN, LAYER_GUI);
201     }
202 }
203
204 void 
205 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
206 {
207   // abort if statistics are not yet initialized
208   if(stats[COINS_COLLECTED_STAT][SPLAYER] == -1) return;
209
210   // abort if we have no backdrop
211   if (!backdrop) return;
212   
213   int box_w = 130+130+130;
214   int box_h = 30+20+20+20;
215   int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
216   int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
217
218   int bd_w = (int)backdrop->get_width();
219   int bd_h = (int)backdrop->get_height();
220   int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
221   int bd_y = box_y + (box_h / 2) - (bd_h / 2);
222
223   int col1_x = box_x;
224   int col2_x = col1_x+130;
225   int col3_x = col2_x+130;
226
227   int row1_y = box_y;
228   int row2_y = row1_y+30;
229   int row3_y = row2_y+20;
230   int row4_y = row3_y+20;
231
232   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
233
234   char buf[129];
235   context.draw_text(white_text, "You", Vector(col2_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
236   context.draw_text(white_text, "Best", Vector(col3_x, row1_y), LEFT_ALLIGN, LAYER_GUI);
237
238   context.draw_text(white_text, "Coins", Vector(col1_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
239   snprintf(buf, 128, "%d/%d", stats[COINS_COLLECTED_STAT][SPLAYER], stats[COINS_COLLECTED_STAT][STOTAL]);
240   context.draw_text(gold_text, buf, Vector(col2_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
241   if (best_stats && (best_stats->stats[COINS_COLLECTED_STAT][SPLAYER] > stats[COINS_COLLECTED_STAT][SPLAYER])) {
242     snprintf(buf, 128, "%d/%d", best_stats->stats[COINS_COLLECTED_STAT][SPLAYER], best_stats->stats[COINS_COLLECTED_STAT][STOTAL]);
243   }
244   context.draw_text(gold_text, buf, Vector(col3_x, row2_y), LEFT_ALLIGN, LAYER_GUI);
245
246   context.draw_text(white_text, "Time", Vector(col1_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
247   snprintf(buf, 128, "%d:%02d", stats[TIME_NEEDED_STAT][SPLAYER] / 60, stats[TIME_NEEDED_STAT][SPLAYER] % 60);
248   context.draw_text(gold_text, buf, Vector(col2_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
249   if (best_stats && (best_stats->stats[TIME_NEEDED_STAT][SPLAYER] < stats[TIME_NEEDED_STAT][SPLAYER])) {
250     snprintf(buf, 128, "%d:%02d", best_stats->stats[TIME_NEEDED_STAT][SPLAYER] / 60, best_stats->stats[TIME_NEEDED_STAT][SPLAYER] % 60);
251   }
252   context.draw_text(gold_text, buf, Vector(col3_x, row3_y), LEFT_ALLIGN, LAYER_GUI);
253   
254   context.draw_text(white_text, "Badguys", Vector(col1_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
255   snprintf(buf, 128, "%d/%d", stats[BADGUYS_KILLED_STAT][SPLAYER], stats[BADGUYS_KILLED_STAT][STOTAL]);
256   context.draw_text(gold_text, buf, Vector(col2_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
257   if (best_stats && (best_stats->stats[BADGUYS_KILLED_STAT][SPLAYER] > stats[BADGUYS_KILLED_STAT][SPLAYER])) {
258     snprintf(buf, 128, "%d/%d", best_stats->stats[BADGUYS_KILLED_STAT][SPLAYER], best_stats->stats[BADGUYS_KILLED_STAT][STOTAL]);
259   }
260   context.draw_text(gold_text, buf, Vector(col3_x, row4_y), LEFT_ALLIGN, LAYER_GUI);
261
262 }
263
264 void
265 Statistics::add_points(int stat, int points)
266 {
267   stats[stat][SPLAYER] += points;
268 }
269
270 int
271 Statistics::get_points(int stat)
272 {
273   return stats[stat][SPLAYER];
274 }
275
276 void
277 Statistics::set_points(int stat, int points)
278 {
279   stats[stat][SPLAYER] = points;
280 }
281
282 void
283 Statistics::set_total_points(int stat, int points)
284 {
285   stats[stat][STOTAL] = points;
286 }
287
288 void
289 Statistics::reset()
290 {
291   for(int i = 0; i < NUM_STATS; i++)
292     stats[i][SPLAYER] = 0;
293 }
294
295 void
296 Statistics::merge(Statistics& stats_)
297 {
298 //  stats[SCORE_STAT][SPLAYER] = std::max(stats[SCORE_STAT][SPLAYER], stats_.stats[SCORE_STAT][SPLAYER]);
299   stats[COINS_COLLECTED_STAT][SPLAYER] = std::max(stats[COINS_COLLECTED_STAT][SPLAYER], stats_.stats[COINS_COLLECTED_STAT][SPLAYER]);
300   stats[BADGUYS_KILLED_STAT][SPLAYER] =
301     std::max(stats[BADGUYS_KILLED_STAT][SPLAYER], stats_.stats[BADGUYS_KILLED_STAT][SPLAYER]);
302   stats[TIME_NEEDED_STAT][SPLAYER] =
303     my_min(stats[TIME_NEEDED_STAT][SPLAYER], stats_.stats[TIME_NEEDED_STAT][SPLAYER]);
304
305   stats[COINS_COLLECTED_STAT][STOTAL] = stats_.stats[COINS_COLLECTED_STAT][STOTAL];
306   stats[BADGUYS_KILLED_STAT][STOTAL] = stats_.stats[BADGUYS_KILLED_STAT][STOTAL];
307   stats[TIME_NEEDED_STAT][STOTAL] = stats_.stats[TIME_NEEDED_STAT][STOTAL];
308 }
309
310 void
311 Statistics::operator+=(const Statistics& stats_)
312 {
313   for(int i = 0; i < NUM_STATS; i++)
314     {
315     if(stats_.stats[i][SPLAYER] == -1)
316       continue;
317     stats[i][SPLAYER] += stats_.stats[i][SPLAYER];
318     if(stats_.stats[i][STOTAL] != -1)
319       stats[i][STOTAL] += stats_.stats[i][STOTAL];
320     }
321 }