Fixed Score = -1 on worldmap display when no level has been played in a fresh slot.
[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 "utils/lispreader.h"
21 #include "utils/lispwriter.h"
22 #include "statistics.h"
23 #include "video/drawing_context.h"
24 #include "app/gettext.h"
25 #include "app/globals.h"
26 #include "resources.h"
27
28 Statistics global_stats;
29
30 std::string
31 stat_name_to_string(int stat_enum)
32 {
33   switch(stat_enum)
34     {
35     case SCORE_STAT:
36       return "score";
37     case COINS_COLLECTED_STAT:
38       return "coins-collected";
39     case BADGUYS_KILLED_STAT:
40       return "badguys-killed";
41     case TIME_NEEDED_STAT:
42       return "time-needed";;
43     }
44   return "";
45 }
46
47 int
48 my_min(int a, int b)
49 {
50 if(a == -1)
51   return b;
52 if(b == -1)
53   return a;
54 return std::min(a, b);
55 }
56
57 Statistics::Statistics()
58 {
59   timer.init(true);
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(LispReader& reader)
73 {
74   for(int i = 0; i < NUM_STATS; i++)
75     {
76     reader.read_int(stat_name_to_string(i).c_str(), stats[i][SPLAYER]);
77     reader.read_int((stat_name_to_string(i) + "-total").c_str(), stats[i][STOTAL]);
78     }
79 }
80
81 void
82 Statistics::write(LispWriter& writer)
83 {
84   for(int i = 0; i < NUM_STATS; i++)
85     {
86     writer.write_int(stat_name_to_string(i), stats[i][SPLAYER]);
87     writer.write_int(stat_name_to_string(i) + "-total", stats[i][STOTAL]);
88     }
89 }
90
91 #define TOTAL_DISPLAY_TIME 3400
92 #define FADING_TIME         600
93
94 #define WMAP_INFO_LEFT_X  540
95 #define WMAP_INFO_RIGHT_X 720
96
97 void
98 Statistics::draw_worldmap_info(DrawingContext& context)
99 {
100   if(stats[SCORE_STAT][SPLAYER] == -1)  // not initialized yet
101     return;
102
103   if(!timer.check())
104     {
105     timer.start(TOTAL_DISPLAY_TIME);
106     display_stat++;
107     if(display_stat >= NUM_STATS)
108       display_stat = 1;
109     }
110
111   int alpha;
112   if(timer.get_gone() < FADING_TIME)
113     alpha = timer.get_gone() * 255 / FADING_TIME;
114   else if(timer.get_left() < FADING_TIME)
115     alpha = timer.get_left() * 255 / FADING_TIME;
116   else
117     alpha = 255;
118
119   char str[128];
120
121   context.draw_text(white_small_text, _("Best Level Statistics"),
122                     Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, 490),
123                     CENTER_ALLIGN, LAYER_GUI);
124
125   sprintf(str, _("Max score:"));
126   context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 506), LEFT_ALLIGN, LAYER_GUI);
127
128   sprintf(str, "%d", stats[SCORE_STAT][SPLAYER]);
129   context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 506), RIGHT_ALLIGN, LAYER_GUI);
130
131   // draw other small info
132
133   if(display_stat == COINS_COLLECTED_STAT)
134     sprintf(str, _("Max coins collected:"));
135   else if(display_stat == BADGUYS_KILLED_STAT)
136     sprintf(str, _("Max fragging:"));
137   else// if(display_stat == TIME_NEEDED_STAT)
138     sprintf(str, _("Min time needed:"));
139
140   context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 522), LEFT_ALLIGN, LAYER_GUI, NONE_EFFECT, alpha);
141
142   if(display_stat == COINS_COLLECTED_STAT)
143     sprintf(str, "%d/%d", stats[COINS_COLLECTED_STAT][SPLAYER],
144                           stats[COINS_COLLECTED_STAT][STOTAL]);
145   else if(display_stat == BADGUYS_KILLED_STAT)
146     sprintf(str, "%d/%d", stats[BADGUYS_KILLED_STAT][SPLAYER],
147                           stats[BADGUYS_KILLED_STAT][STOTAL]);
148   else// if(display_stat == TIME_NEEDED_STAT)
149     sprintf(str, "%d/%d", stats[TIME_NEEDED_STAT][SPLAYER],
150                           stats[TIME_NEEDED_STAT][STOTAL]);
151
152   context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 522), RIGHT_ALLIGN, LAYER_GUI, NONE_EFFECT, alpha);
153 }
154
155 void
156 Statistics::draw_message_info(DrawingContext& context, std::string title)
157 {
158   if(stats[SCORE_STAT][SPLAYER] == -1)  // not initialized yet
159     return;
160
161   context.draw_text(gold_text, title, Vector(screen->w/2, 410), CENTER_ALLIGN, LAYER_GUI);
162
163   char str[128];
164
165   sprintf(str, _(    "Max score:           %d"), stats[SCORE_STAT][SPLAYER]);
166   context.draw_text(white_text, str, Vector(screen->w/2, 450), CENTER_ALLIGN, LAYER_GUI);
167
168   for(int i = 1; i < NUM_STATS; i++)
169     {
170     if(i == COINS_COLLECTED_STAT)
171       sprintf(str, _("Max coins collected: %d"), ((float)stats[COINS_COLLECTED_STAT][SPLAYER] /
172                                                  (float)stats[COINS_COLLECTED_STAT][STOTAL]) * 100);
173     else if(i == BADGUYS_KILLED_STAT)
174       sprintf(str, _("Max fragging:        %d"), ((float)stats[BADGUYS_KILLED_STAT][SPLAYER] /
175                                                  (float)stats[BADGUYS_KILLED_STAT][STOTAL]) * 100);
176     else// if(i == TIME_NEEDED_STAT)
177       sprintf(str, _("Min time needed:     %d"), ((float)stats[TIME_NEEDED_STAT][SPLAYER] /
178                                                  (float)stats[TIME_NEEDED_STAT][STOTAL]) * 100);
179
180     context.draw_text(white_small_text, str, Vector(screen->w/2, 462 + i*18), CENTER_ALLIGN, LAYER_GUI);
181     }
182 }
183
184 void
185 Statistics::add_points(int stat, int points)
186 {
187   stats[stat][SPLAYER] += points;
188 }
189
190 int
191 Statistics::get_points(int stat)
192 {
193   return stats[stat][SPLAYER];
194 }
195
196 void
197 Statistics::set_points(int stat, int points)
198 {
199   stats[stat][SPLAYER] = points;
200 }
201
202 void
203 Statistics::set_total_points(int stat, int points)
204 {
205   stats[stat][STOTAL] = points;
206 }
207
208 void
209 Statistics::reset()
210 {
211   for(int i = 0; i < NUM_STATS; i++)
212     stats[i][SPLAYER] = 0;
213 }
214
215 void
216 Statistics::merge(Statistics& stats_)
217 {
218   stats[SCORE_STAT][SPLAYER] = std::max(stats[SCORE_STAT][SPLAYER], stats_.stats[SCORE_STAT][SPLAYER]);
219   stats[COINS_COLLECTED_STAT][SPLAYER] = std::max(stats[COINS_COLLECTED_STAT][SPLAYER], stats_.stats[COINS_COLLECTED_STAT][SPLAYER]);
220   stats[BADGUYS_KILLED_STAT][SPLAYER] =
221     std::max(stats[BADGUYS_KILLED_STAT][SPLAYER], stats_.stats[BADGUYS_KILLED_STAT][SPLAYER]);
222   stats[TIME_NEEDED_STAT][SPLAYER] =
223     my_min(stats[TIME_NEEDED_STAT][SPLAYER], stats_.stats[TIME_NEEDED_STAT][SPLAYER]);
224
225   stats[COINS_COLLECTED_STAT][STOTAL] = stats_.stats[COINS_COLLECTED_STAT][STOTAL];
226   stats[BADGUYS_KILLED_STAT][STOTAL] = stats_.stats[BADGUYS_KILLED_STAT][STOTAL];
227   stats[TIME_NEEDED_STAT][STOTAL] = stats_.stats[TIME_NEEDED_STAT][STOTAL];
228 }
229
230 void
231 Statistics::operator+=(const Statistics& stats_)
232 {
233   for(int i = 0; i < NUM_STATS; i++)
234     {
235     if(stats_.stats[i][STOTAL] == -1)
236       continue;
237     stats[i][SPLAYER] += stats_.stats[i][SPLAYER];
238     if(stats_.stats[i][STOTAL] != -1)
239       stats[i][STOTAL] += stats_.stats[i][STOTAL];
240     }
241 }