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