Statistics are now saved to disk
[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 <sstream>
26 #include <limits>
27 #include "video/drawing_context.hpp"
28 #include "gettext.hpp"
29 #include "lisp/writer.hpp"
30 #include "lisp/lisp.hpp"
31 #include "resources.hpp"
32 #include "main.hpp"
33 #include "statistics.hpp"
34 #include "log.hpp"
35 #include "scripting/squirrel_util.hpp"
36
37 namespace {
38   const int nv_coins = std::numeric_limits<int>::min();
39   const int nv_badguys = std::numeric_limits<int>::min();
40   const float nv_time = std::numeric_limits<float>::max();
41   const int nv_secrets = std::numeric_limits<int>::min();
42 }
43
44 float WMAP_INFO_LEFT_X;
45 float WMAP_INFO_RIGHT_X;
46 float WMAP_INFO_TOP_Y1;
47 float WMAP_INFO_TOP_Y2;
48
49 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), valid(true), display_stat(0)
50 {
51   WMAP_INFO_LEFT_X = (SCREEN_WIDTH/2 + 80) + 32;
52   WMAP_INFO_RIGHT_X = SCREEN_WIDTH/2 + 368;
53   WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT/2 + 172 - 16;
54   WMAP_INFO_TOP_Y2 = SCREEN_HEIGHT/2 + 172;
55 }
56
57 Statistics::~Statistics()
58 {
59 }
60
61 /*
62 void
63 Statistics::parse(const lisp::Lisp& reader)
64 {
65   reader.get("coins-collected", coins);
66   reader.get("coins-collected-total", total_coins);
67   reader.get("badguys-killed", badguys);
68   reader.get("badguys-killed-total", total_badguys);
69   reader.get("time-needed", time);
70   reader.get("secrets-found", secrets);
71   reader.get("secrets-found-total", total_secrets);
72 }
73
74 void
75 Statistics::write(lisp::Writer& writer)
76 {
77   writer.write_int("coins-collected", coins);
78   writer.write_int("coins-collected-total", total_coins);
79   writer.write_int("badguys-killed", badguys);
80   writer.write_int("badguys-killed-total", total_badguys);
81   writer.write_float("time-needed", time);
82   writer.write_int("secrets-found", secrets);
83   writer.write_int("secrets-found-total", total_secrets);
84 }
85 */
86
87 void
88 Statistics::serialize_to_squirrel(HSQUIRRELVM vm)
89 {
90   // 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
91   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;
92
93   sq_pushstring(vm, "statistics", -1);
94   sq_newtable(vm);
95   if (coins != nv_coins) Scripting::store_int(vm, "coins-collected", coins);
96   if (total_coins != nv_coins) Scripting::store_int(vm, "coins-collected-total", total_coins);
97   if (badguys != nv_badguys) Scripting::store_int(vm, "badguys-killed", badguys);
98   if (total_badguys != nv_badguys) Scripting::store_int(vm, "badguys-killed-total", total_badguys);
99   if (time != nv_time) Scripting::store_float(vm, "time-needed", time);
100   if (secrets != nv_secrets) Scripting::store_int(vm, "secrets-found", secrets);
101   if (total_secrets != nv_secrets) Scripting::store_int(vm, "secrets-found-total", total_secrets);
102   sq_createslot(vm, -3);
103 }
104
105 void
106 Statistics::unserialize_from_squirrel(HSQUIRRELVM vm)
107 {
108   sq_pushstring(vm, "statistics", -1);
109   if(SQ_FAILED(sq_get(vm, -2))) {
110     return;
111   }
112   Scripting::get_int(vm, "coins-collected", coins);
113   Scripting::get_int(vm, "coins-collected-total", total_coins);
114   Scripting::get_int(vm, "badguys-killed", badguys);
115   Scripting::get_int(vm, "badguys-killed-total", total_badguys);
116   Scripting::get_float(vm, "time-needed", time);
117   Scripting::get_int(vm, "secrets-found", secrets);
118   Scripting::get_int(vm, "secrets-found-total", total_secrets);
119   sq_pop(vm, 1);
120 }
121
122 //define TOTAL_DISPLAY_TIME  3400
123 //define FADING_TIME          600
124
125 #define TOTAL_DISPLAY_TIME  5
126 #define FADING_TIME         1
127
128 void
129 Statistics::draw_worldmap_info(DrawingContext& context)
130 {
131   // skip draw if level was never played
132   if (coins == nv_coins) return;
133
134   // skip draw if stats were declared invalid
135   if (!valid) return;
136
137   context.draw_text(white_small_text, std::string("- ") + _("Best Level Statistics") + " -", Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1), ALIGN_CENTER, LAYER_GUI);
138
139   float alpha;
140   if(timer.get_timegone() < FADING_TIME)
141     alpha = (timer.get_timegone() * 1.0f / FADING_TIME);
142   else if(timer.get_timeleft() < FADING_TIME)
143     alpha = (timer.get_timeleft() * 1.0f / FADING_TIME);
144   else
145     alpha = 1.0f;
146
147   context.push_transform();
148   context.set_alpha(alpha);
149
150   char caption_buf[128];
151   char stat_buf[128];
152   switch (display_stat)
153   {
154     case 0:
155       snprintf(caption_buf, sizeof(caption_buf), _("Max coins collected:"));
156       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
157       break;
158     case 1:
159       snprintf(caption_buf, sizeof(caption_buf), _("Max fragging:"));
160       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
161       break;
162     case 2:
163       snprintf(caption_buf, sizeof(caption_buf), _("Min time needed:"));
164       {
165         int csecs = (int)(time * 100);
166         int mins = (int)(csecs / 6000);
167         int secs = (csecs % 6000) / 100;
168         snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
169       }
170       break;
171     case 3:
172       snprintf(caption_buf, sizeof(caption_buf), _("Max secrets found:"));
173       snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
174       break;
175     default:
176       log_debug << "Invalid stat requested to be drawn" << std::endl;
177       break;
178   }
179
180   if (!timer.started())
181   {
182     timer.start(TOTAL_DISPLAY_TIME);
183     display_stat++;
184     if (display_stat > 3) display_stat = 0;
185   }
186
187   context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, WMAP_INFO_TOP_Y2), ALIGN_LEFT, LAYER_GUI);
188   context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, WMAP_INFO_TOP_Y2), ALIGN_RIGHT, LAYER_GUI);
189   context.pop_transform();
190 }
191
192 void
193 Statistics::draw_message_info(DrawingContext& context, std::string title)
194 {
195   // skip draw if level was never played
196   // TODO: do we need this?
197   if (coins == nv_coins) return;
198
199   // skip draw if stats were declared invalid
200   if (!valid) return;
201
202   const float width = white_small_text->get_text_width("Max coins collected: 1111 / 1111");
203   const float left = (SCREEN_WIDTH - width) / 2;
204   const float right = (SCREEN_WIDTH + width) / 2;
205
206   context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), ALIGN_CENTER, LAYER_GUI);
207
208   char stat_buf[128];
209   int py = 450 + 18;
210
211   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
212   context.draw_text(white_small_text, _("Max coins collected:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
213   context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
214   py+=18;
215
216   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
217   context.draw_text(white_small_text, _("Max fragging:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
218   context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
219   py+=18;
220
221   int csecs = (int)(time * 100);
222   int mins = (int)(csecs / 6000);
223   int secs = (csecs % 6000) / 100;
224   snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
225   context.draw_text(white_small_text, _("Min time needed:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
226   context.draw_text(white_small_text, "%02d:%02d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
227   py+=18;
228
229   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
230   context.draw_text(white_small_text, _("Max secrets found:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
231   context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
232   py+=18;
233 }
234
235 void
236 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
237 {
238   // skip draw if level was never played
239   // TODO: do we need this?
240   if (coins == nv_coins) return;
241
242   // skip draw if stats were declared invalid
243   if (!valid) return;
244
245   // abort if we have no backdrop
246   if (!backdrop) return;
247
248   int box_w = 220+110+110;
249   int box_h = 30+20+20+20;
250   int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
251   int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
252
253   int bd_w = (int)backdrop->get_width();
254   int bd_h = (int)backdrop->get_height();
255   int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
256   int bd_y = box_y + (box_h / 2) - (bd_h / 2);
257
258   int col1_x = box_x;
259   int col2_x = col1_x+200;
260   int col3_x = col2_x+130;
261
262   int row1_y = box_y;
263   int row2_y = row1_y+30;
264   int row3_y = row2_y+20;
265   int row4_y = row3_y+20;
266
267   context.push_transform();
268   context.set_alpha(0.5);
269   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
270   context.pop_transform();
271
272   char buf[129];
273   context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_GUI);
274   context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_GUI);
275
276   context.draw_text(white_text, _("Coins"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_GUI);
277   snprintf(buf, sizeof(buf), "%d/%d", std::min(coins, 999), std::min(total_coins, 999));
278   context.draw_text(gold_text, buf, Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_GUI);
279   if (best_stats && (best_stats->coins > coins)) {
280     snprintf(buf, sizeof(buf), "%d/%d", std::min(best_stats->coins, 999), std::min(best_stats->total_coins, 999));
281   }
282   context.draw_text(gold_text, buf, Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_GUI);
283
284   context.draw_text(white_text, _("Secrets"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_GUI);
285   snprintf(buf, sizeof(buf), "%d/%d", secrets, total_secrets);
286   context.draw_text(gold_text, buf, Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_GUI);
287   if (best_stats && (best_stats->secrets > secrets)) {
288     snprintf(buf, sizeof(buf), "%d/%d", best_stats->secrets, best_stats->total_secrets);
289   }
290   context.draw_text(gold_text, buf, Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_GUI);
291
292   context.draw_text(white_text, _("Time"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_GUI);
293   int csecs = (int)(time * 100);
294   int mins = (int)(csecs / 6000);
295   int secs = (csecs % 6000) / 100;
296   snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
297   context.draw_text(gold_text, buf, Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_GUI);
298   if (best_stats && (best_stats->time < time)) {
299     int csecs = (int)(best_stats->time * 100);
300     int mins = (int)(csecs / 6000);
301     int secs = (csecs % 6000) / 100;
302     snprintf(buf, sizeof(buf), "%02d:%02d", mins,secs);
303   }
304   context.draw_text(gold_text, buf, Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_GUI);
305 }
306
307 void
308 Statistics::zero()
309 {
310   reset();
311   total_coins = 0;
312   total_badguys = 0;
313   total_secrets = 0;
314 }
315
316 void
317 Statistics::reset()
318 {
319   coins = 0;
320   badguys = 0;
321   time = 0;
322   secrets = 0;
323 }
324
325 void
326 Statistics::merge(Statistics& s2)
327 {
328   if (!s2.valid) return;
329   coins = std::max(coins, s2.coins);
330   total_coins = s2.total_coins;
331   badguys = std::max(badguys, s2.badguys);
332   total_badguys = s2.total_badguys;
333   time = std::min(time, s2.time);
334   secrets = std::max(secrets, s2.secrets);
335   total_secrets = s2.total_secrets;
336 }
337
338 void
339 Statistics::operator+=(const Statistics& s2)
340 {
341   if (!s2.valid) return;
342   if (s2.coins != nv_coins) coins += s2.coins;
343   if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
344   if (s2.badguys != nv_badguys) badguys += s2.badguys;
345   if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
346   if (s2.time != nv_time) time += s2.time;
347   if (s2.secrets != nv_secrets) secrets += s2.secrets;
348   if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
349 }
350
351 void
352 Statistics::declare_invalid()
353 {
354   valid = false;
355 }