Getting rid of nasty tabs
[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 <iomanip>
27 #include <limits>
28 #include "video/drawing_context.hpp"
29 #include "gettext.hpp"
30 #include "lisp/writer.hpp"
31 #include "lisp/lisp.hpp"
32 #include "resources.hpp"
33 #include "main.hpp"
34 #include "statistics.hpp"
35 #include "log.hpp"
36 #include "scripting/squirrel_util.hpp"
37
38 namespace {
39   const int nv_coins = std::numeric_limits<int>::min();
40   const int nv_badguys = std::numeric_limits<int>::min();
41   const float nv_time = std::numeric_limits<float>::max();
42   const int nv_secrets = std::numeric_limits<int>::min();
43 }
44
45 float WMAP_INFO_LEFT_X;
46 float WMAP_INFO_RIGHT_X;
47 float WMAP_INFO_TOP_Y1;
48 float WMAP_INFO_TOP_Y2;
49
50 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) 
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 lisp::Lisp& 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_int("coins-collected", coins);
79   writer.write_int("coins-collected-total", total_coins);
80   writer.write_int("badguys-killed", badguys);
81   writer.write_int("badguys-killed-total", total_badguys);
82   writer.write_float("time-needed", time);
83   writer.write_int("secrets-found", secrets);
84   writer.write_int("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(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);
133
134   std::string caption_buf;
135   std::string stat_buf;
136   float posy = WMAP_INFO_TOP_Y2;
137   for (int stat_no = 0; stat_no < 4; stat_no++) {
138     switch (stat_no)
139     {
140       case 0:
141           caption_buf = _("Max coins collected:");
142           stat_buf = coins_to_string(coins, total_coins);
143           break;
144       case 1:
145           caption_buf = _("Max fragging:");
146           stat_buf = frags_to_string(badguys, total_badguys);
147           break;
148       case 2:
149           caption_buf = _("Min time needed:");
150           stat_buf = time_to_string(time);
151           break;
152       case 3:
153           caption_buf = _("Max secrets found:");
154           stat_buf = secrets_to_string(secrets, total_secrets);
155           break;
156       default:
157           log_debug << "Invalid stat requested to be drawn" << std::endl;
158           break;
159     }
160
161     context.draw_text(white_small_text, caption_buf, Vector(WMAP_INFO_LEFT_X, posy), ALIGN_LEFT, LAYER_GUI);
162     context.draw_text(white_small_text, stat_buf, Vector(WMAP_INFO_RIGHT_X, posy), ALIGN_RIGHT, LAYER_GUI);
163     posy += white_small_text->get_height() + 2;
164   }
165
166 }
167
168 void
169 Statistics::draw_message_info(DrawingContext& context, std::string title)
170 {
171   // skip draw if level was never played
172   // TODO: do we need this?
173   if (coins == nv_coins) return;
174
175   // skip draw if stats were declared invalid
176   if (!valid) return;
177
178   const float width = white_small_text->get_text_width("Max coins collected: 1111 / 1111");
179   const float left = (SCREEN_WIDTH - width) / 2;
180   const float right = (SCREEN_WIDTH + width) / 2;
181
182   context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), ALIGN_CENTER, LAYER_GUI);
183
184   char stat_buf[128];
185   int py = 450 + 18;
186
187   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", coins, total_coins);
188   context.draw_text(white_small_text, _("Max coins collected:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
189   context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
190   py+=18;
191
192   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", badguys, total_badguys);
193   context.draw_text(white_small_text, _("Max fragging:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
194   context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
195   py+=18;
196
197   int csecs = (int)(time * 100);
198   int mins = (int)(csecs / 6000);
199   int secs = (csecs % 6000) / 100;
200   snprintf(stat_buf, sizeof(stat_buf), "%02d:%02d", mins,secs);
201   context.draw_text(white_small_text, _("Min time needed:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
202   context.draw_text(white_small_text, "%02d:%02d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
203   py+=18;
204
205   snprintf(stat_buf, sizeof(stat_buf), "%d/%d", secrets, total_secrets);
206   context.draw_text(white_small_text, _("Max secrets found:"), Vector(left, py), ALIGN_LEFT, LAYER_GUI);
207   context.draw_text(white_small_text, "%d / %d", Vector(right, py), ALIGN_RIGHT, LAYER_GUI);
208   py+=18;
209 }
210
211 void
212 Statistics::draw_endseq_panel(DrawingContext& context, Statistics* best_stats, Surface* backdrop)
213 {
214   // skip draw if level was never played
215   // TODO: do we need this?
216   if (coins == nv_coins) return;
217
218   // skip draw if stats were declared invalid
219   if (!valid) return;
220
221   // abort if we have no backdrop
222   if (!backdrop) return;
223
224   int box_w = 220+110+110;
225   int box_h = 30+20+20+20;
226   int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
227   int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
228
229   int bd_w = (int)backdrop->get_width();
230   int bd_h = (int)backdrop->get_height();
231   int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
232   int bd_y = box_y + (box_h / 2) - (bd_h / 2);
233
234   int col1_x = box_x;
235   int col2_x = col1_x+200;
236   int col3_x = col2_x+130;
237
238   int row1_y = box_y;
239   int row2_y = row1_y+30;
240   int row3_y = row2_y+20;
241   int row4_y = row3_y+20;
242
243   context.push_transform();
244   context.set_alpha(0.5);
245   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_GUI);
246   context.pop_transform();
247
248   context.draw_text(white_text, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_GUI);
249   context.draw_text(white_text, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_GUI);
250
251   context.draw_text(white_text, _("Coins"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_GUI);
252   int coins_best = (best_stats && (best_stats->coins > coins)) ? best_stats->coins : coins;
253   int total_coins_best = (best_stats && (best_stats->total_coins > total_coins)) ? best_stats->total_coins : total_coins;
254   context.draw_text(gold_text, coins_to_string(coins, total_coins), Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_GUI);
255   context.draw_text(gold_text, coins_to_string(coins_best, total_coins_best), Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_GUI);
256
257   context.draw_text(white_text, _("Secrets"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_GUI);
258   int secrets_best = (best_stats && (best_stats->secrets > secrets)) ? best_stats->secrets : secrets;
259   int total_secrets_best = (best_stats && (best_stats->total_secrets > total_secrets)) ? best_stats->total_secrets : total_secrets;
260   context.draw_text(gold_text, secrets_to_string(secrets, total_secrets), Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_GUI);
261   context.draw_text(gold_text, secrets_to_string(secrets_best, total_secrets_best), Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_GUI);
262
263   context.draw_text(white_text, _("Time"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_GUI);
264   float time_best = (best_stats && (best_stats->time < time)) ? best_stats->time : time;
265   context.draw_text(gold_text, time_to_string(time), Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_GUI);
266   context.draw_text(gold_text, time_to_string(time_best), Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_GUI);
267 }
268
269 void
270 Statistics::zero()
271 {
272   reset();
273   total_coins = 0;
274   total_badguys = 0;
275   total_secrets = 0;
276 }
277
278 void
279 Statistics::reset()
280 {
281   coins = 0;
282   badguys = 0;
283   time = 0;
284   secrets = 0;
285 }
286
287 void
288 Statistics::merge(Statistics& s2)
289 {
290   if (!s2.valid) return;
291   coins = std::max(coins, s2.coins);
292   total_coins = s2.total_coins;
293   badguys = std::max(badguys, s2.badguys);
294   total_badguys = s2.total_badguys;
295   time = std::min(time, s2.time);
296   secrets = std::max(secrets, s2.secrets);
297   total_secrets = s2.total_secrets;
298 }
299
300 void
301 Statistics::operator+=(const Statistics& s2)
302 {
303   if (!s2.valid) return;
304   if (s2.coins != nv_coins) coins += s2.coins;
305   if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
306   if (s2.badguys != nv_badguys) badguys += s2.badguys;
307   if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
308   if (s2.time != nv_time) time += s2.time;
309   if (s2.secrets != nv_secrets) secrets += s2.secrets;
310   if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
311 }
312
313 void
314 Statistics::declare_invalid()
315 {
316   valid = false;
317 }
318
319 std::string
320 Statistics::coins_to_string(int coins, int total_coins) const {
321   std::ostringstream os;
322   os << std::min(coins, 999) << "/" << std::min(total_coins, 999);
323   return os.str();
324 }
325
326 std::string
327 Statistics::frags_to_string(int badguys, int total_badguys) const {
328   std::ostringstream os;
329   os << std::min(badguys, 999) << "/" << std::min(total_badguys, 999);
330   return os.str();
331 }
332
333 std::string 
334 Statistics::time_to_string(float time) const {
335   int time_csecs = std::min(static_cast<int>(time * 100), 99 * 6000 + 9999);
336   int mins = (time_csecs / 6000);
337   int secs = (time_csecs % 6000) / 100;
338   int cscs = (time_csecs % 6000) % 100;
339
340   std::ostringstream os;
341   os << std::setw(2) << std::setfill('0') << mins << ":" << std::setw(2) << std::setfill('0') << secs << "." << std::setw(2) << std::setfill('0') << cscs;
342   return os.str();
343 }
344
345 std::string
346 Statistics::secrets_to_string(int secrets, int total_secrets) const {
347   std::ostringstream os;
348   os << std::min(secrets, 999) << "/" << std::min(total_secrets, 999);
349   return os.str();
350 }
351