3dea0f62a967bac22d430a98657ba931b12182ba
[supertux.git] / src / supertux / statistics.cpp
1 //  SuperTux (Statistics module)
2 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
3 //  Copyright (C) 2006 Ondrej Hosek <ondra.hosek@gmail.com>
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software: you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation, either version 3 of the License, or
9 //  (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 #include "supertux/statistics.hpp"
20
21 #include <iomanip>
22 #include <limits>
23
24 #include "scripting/squirrel_util.hpp"
25 #include "supertux/globals.hpp"
26 #include "supertux/resources.hpp"
27 #include "util/gettext.hpp"
28 #include "video/drawing_context.hpp"
29
30 namespace {
31 const int nv_coins = std::numeric_limits<int>::min();
32 const int nv_badguys = std::numeric_limits<int>::min();
33 const float nv_time = std::numeric_limits<float>::max();
34 const int nv_secrets = std::numeric_limits<int>::min();
35 }
36
37 float WMAP_INFO_LEFT_X;
38 float WMAP_INFO_RIGHT_X;
39 float WMAP_INFO_TOP_Y1;
40 float WMAP_INFO_TOP_Y2;
41
42 Statistics::Statistics() : 
43   coins(nv_coins), 
44   total_coins(nv_coins), 
45   badguys(nv_badguys), 
46   total_badguys(nv_badguys), 
47   time(nv_time), 
48   secrets(nv_secrets), 
49   total_secrets(nv_secrets), 
50   valid(true) 
51 {
52   WMAP_INFO_LEFT_X = (SCREEN_WIDTH/2 + 80) + 32;
53   WMAP_INFO_RIGHT_X = SCREEN_WIDTH/2 + 368;
54   WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT/2 + 172 - 16;
55   WMAP_INFO_TOP_Y2 = SCREEN_HEIGHT/2 + 172;
56 }
57
58 Statistics::~Statistics()
59 {
60 }
61
62 /*
63   void
64   Statistics::parse(const Reader& reader)
65   {
66   reader.get("coins-collected", coins);
67   reader.get("coins-collected-total", total_coins);
68   reader.get("badguys-killed", badguys);
69   reader.get("badguys-killed-total", total_badguys);
70   reader.get("time-needed", time);
71   reader.get("secrets-found", secrets);
72   reader.get("secrets-found-total", total_secrets);
73   }
74
75   void
76   Statistics::write(lisp::Writer& writer)
77   {
78   writer.write("coins-collected", coins);
79   writer.write("coins-collected-total", total_coins);
80   writer.write("badguys-killed", badguys);
81   writer.write("badguys-killed-total", total_badguys);
82   writer.write("time-needed", time);
83   writer.write("secrets-found", secrets);
84   writer.write("secrets-found-total", total_secrets);
85   }
86 */
87
88 void
89 Statistics::serialize_to_squirrel(HSQUIRRELVM vm)
90 {
91   // TODO: there's some bug in the unserialization routines that breaks stuff when an empty statistics table is written, so -- as a workaround -- let's make sure we will actually write something first
92   if (!((coins != nv_coins) || (total_coins != nv_coins) || (badguys != nv_badguys) || (total_badguys != nv_badguys) || (time != nv_time) || (secrets != nv_secrets) || (total_secrets != nv_secrets))) return;
93
94   sq_pushstring(vm, "statistics", -1);
95   sq_newtable(vm);
96   if (coins != nv_coins) scripting::store_int(vm, "coins-collected", coins);
97   if (total_coins != nv_coins) scripting::store_int(vm, "coins-collected-total", total_coins);
98   if (badguys != nv_badguys) scripting::store_int(vm, "badguys-killed", badguys);
99   if (total_badguys != nv_badguys) scripting::store_int(vm, "badguys-killed-total", total_badguys);
100   if (time != nv_time) scripting::store_float(vm, "time-needed", time);
101   if (secrets != nv_secrets) scripting::store_int(vm, "secrets-found", secrets);
102   if (total_secrets != nv_secrets) scripting::store_int(vm, "secrets-found-total", total_secrets);
103   sq_createslot(vm, -3);
104 }
105
106 void
107 Statistics::unserialize_from_squirrel(HSQUIRRELVM vm)
108 {
109   sq_pushstring(vm, "statistics", -1);
110   if(SQ_FAILED(sq_get(vm, -2))) {
111     return;
112   }
113   scripting::get_int(vm, "coins-collected", coins);
114   scripting::get_int(vm, "coins-collected-total", total_coins);
115   scripting::get_int(vm, "badguys-killed", badguys);
116   scripting::get_int(vm, "badguys-killed-total", total_badguys);
117   scripting::get_float(vm, "time-needed", time);
118   scripting::get_int(vm, "secrets-found", secrets);
119   scripting::get_int(vm, "secrets-found-total", total_secrets);
120   sq_pop(vm, 1);
121 }
122
123 void
124 Statistics::draw_worldmap_info(DrawingContext& context)
125 {
126   // skip draw if level was never played
127   if (coins == nv_coins) return;
128
129   // skip draw if stats were declared invalid
130   if (!valid) return;
131
132   context.draw_text(Resources::small_font, std::string("- ") + _("Best Level Statistics") + " -", 
133                     Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1), 
134                     ALIGN_CENTER, LAYER_GUI,Statistics::header_color);
135
136   std::string caption_buf;
137   std::string stat_buf;
138   float posy = WMAP_INFO_TOP_Y2;
139   for (int stat_no = 0; stat_no < 4; stat_no++) {
140     switch (stat_no)
141     {
142       case 0:
143         caption_buf = _("Max coins collected:");
144         stat_buf = coins_to_string(coins, total_coins);
145         break;
146       case 1:
147         caption_buf = _("Max fragging:");
148         stat_buf = frags_to_string(badguys, total_badguys);
149         break;
150       case 2:
151         caption_buf = _("Min time needed:");
152         stat_buf = time_to_string(time);
153         break;
154       case 3:
155         caption_buf = _("Max secrets found:");
156         stat_buf = secrets_to_string(secrets, total_secrets);
157         break;
158       default:
159         log_debug << "Invalid stat requested to be drawn" << std::endl;
160         break;
161     }
162
163     context.draw_text(Resources::small_font, caption_buf, Vector(WMAP_INFO_LEFT_X, posy), ALIGN_LEFT, LAYER_GUI, Statistics::header_color);
164     context.draw_text(Resources::small_font, stat_buf, Vector(WMAP_INFO_RIGHT_X, posy), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
165     posy += Resources::small_font->get_height() + 2;
166   }
167
168 }
169
170 void
171 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
172 {
173   // skip draw if level was never played
174   // TODO: do we need this?
175   if (coins == nv_coins) return;
176
177   // skip draw if stats were declared invalid
178   if (!valid) return;
179
180   // abort if we have no backdrop
181   if (!backdrop) return;
182
183   int box_w = 220+110+110;
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+200;
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.push_transform();
203   context.set_alpha(0.5);
204   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
205   context.pop_transform();
206
207   context.draw_text(Resources::normal_font, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_GUI, Statistics::header_color);
208   context.draw_text(Resources::normal_font, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_GUI, Statistics::header_color);
209
210   context.draw_text(Resources::normal_font, _("Coins"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
211   int coins_best = (best_stats && (best_stats->coins > coins)) ? best_stats->coins : coins;
212   int total_coins_best = (best_stats && (best_stats->total_coins > total_coins)) ? best_stats->total_coins : total_coins;
213   context.draw_text(Resources::normal_font, coins_to_string(coins, total_coins), Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
214   context.draw_text(Resources::normal_font, coins_to_string(coins_best, total_coins_best), Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
215
216   context.draw_text(Resources::normal_font, _("Secrets"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
217   int secrets_best = (best_stats && (best_stats->secrets > secrets)) ? best_stats->secrets : secrets;
218   int total_secrets_best = (best_stats && (best_stats->total_secrets > total_secrets)) ? best_stats->total_secrets : total_secrets;
219   context.draw_text(Resources::normal_font, secrets_to_string(secrets, total_secrets), Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
220   context.draw_text(Resources::normal_font, secrets_to_string(secrets_best, total_secrets_best), Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
221
222   context.draw_text(Resources::normal_font, _("Time"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_GUI, Statistics::header_color);
223   float time_best = (best_stats && (best_stats->time < time)) ? best_stats->time : time;
224   context.draw_text(Resources::normal_font, time_to_string(time), Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
225   context.draw_text(Resources::normal_font, time_to_string(time_best), Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_GUI, Statistics::text_color);
226 }
227
228 void
229 Statistics::zero()
230 {
231   reset();
232   total_coins = 0;
233   total_badguys = 0;
234   total_secrets = 0;
235 }
236
237 void
238 Statistics::reset()
239 {
240   coins = 0;
241   badguys = 0;
242   time = 0;
243   secrets = 0;
244 }
245
246 void
247 Statistics::merge(const Statistics& s2)
248 {
249   if (!s2.valid) return;
250   coins = std::max(coins, s2.coins);
251   total_coins = s2.total_coins;
252   badguys = std::max(badguys, s2.badguys);
253   total_badguys = s2.total_badguys;
254   time = std::min(time, s2.time);
255   secrets = std::max(secrets, s2.secrets);
256   total_secrets = s2.total_secrets;
257 }
258
259 void
260 Statistics::operator+=(const Statistics& s2)
261 {
262   if (!s2.valid) return;
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 }
271
272 void
273 Statistics::declare_invalid()
274 {
275   valid = false;
276 }
277
278 std::string
279 Statistics::coins_to_string(int coins, int total_coins) {
280   std::ostringstream os;
281   os << std::min(coins, 999) << "/" << std::min(total_coins, 999);
282   return os.str();
283 }
284
285 std::string
286 Statistics::frags_to_string(int badguys, int total_badguys) {
287   std::ostringstream os;
288   os << std::min(badguys, 999) << "/" << std::min(total_badguys, 999);
289   return os.str();
290 }
291
292 std::string 
293 Statistics::time_to_string(float time) {
294   int time_csecs = std::min(static_cast<int>(time * 100), 99 * 6000 + 9999);
295   int mins = (time_csecs / 6000);
296   int secs = (time_csecs % 6000) / 100;
297   int cscs = (time_csecs % 6000) % 100;
298
299   std::ostringstream os;
300   os << std::setw(2) << std::setfill('0') << mins << ":" << std::setw(2) << std::setfill('0') << secs << "." << std::setw(2) << std::setfill('0') << cscs;
301   return os.str();
302 }
303
304 std::string
305 Statistics::secrets_to_string(int secrets, int total_secrets) {
306   std::ostringstream os;
307   os << std::min(secrets, 999) << "/" << std::min(total_secrets, 999);
308   return os.str();
309 }
310
311 /* EOF */