2ea2e49c3f39cb6d90c4f91c4575e35628aadaed
[supertux.git] / src / statistics.cpp
1 //
2 //  SuperTux -  A Jump'n Run
3 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 2
8 //  of the License, or (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 // 
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 //  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "video/drawing_context.h"
23 #include "app/gettext.h"
24 #include "app/globals.h"
25 #include "lisp/lisp.h"
26 #include "resources.h"
27 #include "statistics.h"
28
29 Statistics global_stats;
30
31 std::string
32 stat_name_to_string(int stat_enum)
33 {
34   switch(stat_enum)
35     {
36     case SCORE_STAT:
37       return "score";
38     case COINS_COLLECTED_STAT:
39       return "coins-collected";
40     case BADGUYS_KILLED_STAT:
41       return "badguys-killed";
42     case TIME_NEEDED_STAT:
43       return "time-needed";;
44     }
45   return "";
46 }
47
48 int
49 my_min(int a, int b)
50 {
51 if(a == -1)
52   return b;
53 if(b == -1)
54   return a;
55 return std::min(a, b);
56 }
57
58 Statistics::Statistics()
59 {
60   display_stat = 1;
61
62   for(int i = 0; i < NUM_STATS; i++)
63     for(int j = 0; j < 2; j++)
64       stats[i][j] = -1;
65 }
66
67 Statistics::~Statistics()
68 {
69 }
70
71 void
72 Statistics::parse(const lisp::Lisp& reader)
73 {
74   for(int i = 0; i < NUM_STATS; i++) {
75     reader.get(stat_name_to_string(i).c_str(), stats[i][SPLAYER]);
76     reader.get((stat_name_to_string(i) + "-total").c_str(), stats[i][STOTAL]);
77   }
78 }
79
80 void
81 Statistics::write(lisp::Writer& writer)
82 {
83   for(int i = 0; i < NUM_STATS; i++) {
84     writer.write_int(stat_name_to_string(i), stats[i][SPLAYER]);
85     writer.write_int(stat_name_to_string(i) + "-total", stats[i][STOTAL]);
86   }
87 }
88
89 #define TOTAL_DISPLAY_TIME 3400
90 #define FADING_TIME         600
91
92 #define WMAP_INFO_LEFT_X  520
93 #define WMAP_INFO_RIGHT_X 740
94
95 void
96 Statistics::draw_worldmap_info(DrawingContext& context)
97 {
98   if(stats[SCORE_STAT][SPLAYER] == -1)  // not initialized yet
99     return;
100
101   if(timer.check())
102     {
103     timer.start(TOTAL_DISPLAY_TIME);
104     display_stat++;
105     if(display_stat >= NUM_STATS)
106       display_stat = 1;
107     }
108
109   char str[128];
110
111   context.draw_text(white_small_text, _("- Best Level Statistics -"),
112                     Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, 470),
113                     CENTER_ALLIGN, LAYER_GUI);
114
115   sprintf(str, _("Max score:"));
116   context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 490), LEFT_ALLIGN, LAYER_GUI);
117
118   sprintf(str, "%d", stats[SCORE_STAT][SPLAYER]);
119   context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 490), RIGHT_ALLIGN, LAYER_GUI);
120
121   // draw other small info
122
123   int alpha;
124   if(timer.get_timegone() < FADING_TIME)
125     alpha = int(timer.get_timegone() * 255 / FADING_TIME);
126   else if(timer.get_timeleft() < FADING_TIME)
127     alpha = int(timer.get_timeleft() * 255 / FADING_TIME);
128   else
129     alpha = 255;
130
131   context.push_transform();
132   context.set_alpha(alpha);
133
134   if(display_stat == COINS_COLLECTED_STAT)
135     sprintf(str, _("Max coins collected:"));
136   else if(display_stat == BADGUYS_KILLED_STAT)
137     sprintf(str, _("Max fragging:"));
138   else// if(display_stat == TIME_NEEDED_STAT)
139     sprintf(str, _("Min time needed:"));
140
141   context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 508), LEFT_ALLIGN, LAYER_GUI);
142
143   if(display_stat == COINS_COLLECTED_STAT)
144     sprintf(str, "%d/%d", stats[COINS_COLLECTED_STAT][SPLAYER],
145                           stats[COINS_COLLECTED_STAT][STOTAL]);
146   else if(display_stat == BADGUYS_KILLED_STAT)
147     sprintf(str, "%d/%d", stats[BADGUYS_KILLED_STAT][SPLAYER],
148                           stats[BADGUYS_KILLED_STAT][STOTAL]);
149   else// if(display_stat == TIME_NEEDED_STAT)
150     sprintf(str, "%d/%d", stats[TIME_NEEDED_STAT][SPLAYER],
151                           stats[TIME_NEEDED_STAT][STOTAL]);
152
153   context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 508), RIGHT_ALLIGN, LAYER_GUI);
154
155   context.pop_transform();
156 }
157
158 void
159 Statistics::draw_message_info(DrawingContext& context, std::string title)
160 {
161   if(stats[SCORE_STAT][SPLAYER] == -1)  // not initialized yet
162     return;
163
164   context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
165
166   char str[128];
167
168   sprintf(str, _(    "Max score:             %d"), stats[SCORE_STAT][SPLAYER]);
169   context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 450), CENTER_ALLIGN, LAYER_GUI);
170
171   for(int i = 1; i < NUM_STATS; i++)
172     {
173     if(i == COINS_COLLECTED_STAT)
174       sprintf(str, _("Max coins collected:   %d / %d"),
175               stats[COINS_COLLECTED_STAT][SPLAYER],
176               stats[COINS_COLLECTED_STAT][STOTAL]);
177     else if(i == BADGUYS_KILLED_STAT)
178       sprintf(str, _("Max fragging:          %d / %d"),
179               stats[BADGUYS_KILLED_STAT][SPLAYER],
180               stats[BADGUYS_KILLED_STAT][STOTAL]);
181     else// if(i == TIME_NEEDED_STAT)
182       sprintf(str, _("Min time needed:       %d / %d"),
183               stats[TIME_NEEDED_STAT][SPLAYER],
184               stats[TIME_NEEDED_STAT][STOTAL]);
185
186
187     context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, 462 + i*18), CENTER_ALLIGN, LAYER_GUI);
188     }
189 }
190
191 void
192 Statistics::add_points(int stat, int points)
193 {
194   stats[stat][SPLAYER] += points;
195 }
196
197 int
198 Statistics::get_points(int stat)
199 {
200   return stats[stat][SPLAYER];
201 }
202
203 void
204 Statistics::set_points(int stat, int points)
205 {
206   stats[stat][SPLAYER] = points;
207 }
208
209 void
210 Statistics::set_total_points(int stat, int points)
211 {
212   stats[stat][STOTAL] = points;
213 }
214
215 void
216 Statistics::reset()
217 {
218   for(int i = 0; i < NUM_STATS; i++)
219     stats[i][SPLAYER] = 0;
220 }
221
222 void
223 Statistics::merge(Statistics& stats_)
224 {
225   stats[SCORE_STAT][SPLAYER] = std::max(stats[SCORE_STAT][SPLAYER], stats_.stats[SCORE_STAT][SPLAYER]);
226   stats[COINS_COLLECTED_STAT][SPLAYER] = std::max(stats[COINS_COLLECTED_STAT][SPLAYER], stats_.stats[COINS_COLLECTED_STAT][SPLAYER]);
227   stats[BADGUYS_KILLED_STAT][SPLAYER] =
228     std::max(stats[BADGUYS_KILLED_STAT][SPLAYER], stats_.stats[BADGUYS_KILLED_STAT][SPLAYER]);
229   stats[TIME_NEEDED_STAT][SPLAYER] =
230     my_min(stats[TIME_NEEDED_STAT][SPLAYER], stats_.stats[TIME_NEEDED_STAT][SPLAYER]);
231
232   stats[COINS_COLLECTED_STAT][STOTAL] = stats_.stats[COINS_COLLECTED_STAT][STOTAL];
233   stats[BADGUYS_KILLED_STAT][STOTAL] = stats_.stats[BADGUYS_KILLED_STAT][STOTAL];
234   stats[TIME_NEEDED_STAT][STOTAL] = stats_.stats[TIME_NEEDED_STAT][STOTAL];
235 }
236
237 void
238 Statistics::operator+=(const Statistics& stats_)
239 {
240   for(int i = 0; i < NUM_STATS; i++)
241     {
242     if(stats_.stats[i][SPLAYER] == -1)
243       continue;
244     stats[i][SPLAYER] += stats_.stats[i][SPLAYER];
245     if(stats_.stats[i][STOTAL] != -1)
246       stats[i][STOTAL] += stats_.stats[i][STOTAL];
247     }
248 }