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