2 // SuperTux - A Jump'n Run
3 // Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
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.
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.
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
22 #include "video/drawing_context.hpp"
23 #include "gettext.hpp"
24 #include "lisp/lisp.hpp"
25 #include "resources.hpp"
27 #include "statistics.hpp"
29 Statistics global_stats;
32 stat_name_to_string(int stat_enum)
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";;
55 return std::min(a, b);
58 Statistics::Statistics()
62 for(int i = 0; i < NUM_STATS; i++)
63 for(int j = 0; j < 2; j++)
67 Statistics::~Statistics()
72 Statistics::parse(const lisp::Lisp& reader)
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]);
81 Statistics::write(lisp::Writer& writer)
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]);
89 #define TOTAL_DISPLAY_TIME 3400
90 #define FADING_TIME 600
92 #define WMAP_INFO_LEFT_X 520
93 #define WMAP_INFO_RIGHT_X 740
96 Statistics::draw_worldmap_info(DrawingContext& context)
98 if(stats[SCORE_STAT][SPLAYER] == -1) // not initialized yet
103 timer.start(TOTAL_DISPLAY_TIME);
105 if(display_stat >= NUM_STATS)
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);
115 sprintf(str, _("Max score:"));
116 context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 490), LEFT_ALLIGN, LAYER_GUI);
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);
121 // draw other small info
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);
131 context.push_transform();
132 context.set_alpha(alpha);
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:"));
141 context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 508), LEFT_ALLIGN, LAYER_GUI);
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]);
153 context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 508), RIGHT_ALLIGN, LAYER_GUI);
155 context.pop_transform();
159 Statistics::draw_message_info(DrawingContext& context, std::string title)
161 if(stats[SCORE_STAT][SPLAYER] == -1) // not initialized yet
164 context.draw_text(gold_text, title, Vector(SCREEN_WIDTH/2, 410), CENTER_ALLIGN, LAYER_GUI);
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);
171 for(int i = 1; i < NUM_STATS; i++)
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]);
187 context.draw_text(white_small_text, str, Vector(SCREEN_WIDTH/2, 462 + i*18), CENTER_ALLIGN, LAYER_GUI);
192 Statistics::add_points(int stat, int points)
194 stats[stat][SPLAYER] += points;
198 Statistics::get_points(int stat)
200 return stats[stat][SPLAYER];
204 Statistics::set_points(int stat, int points)
206 stats[stat][SPLAYER] = points;
210 Statistics::set_total_points(int stat, int points)
212 stats[stat][STOTAL] = points;
218 for(int i = 0; i < NUM_STATS; i++)
219 stats[i][SPLAYER] = 0;
223 Statistics::merge(Statistics& stats_)
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]);
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];
238 Statistics::operator+=(const Statistics& stats_)
240 for(int i = 0; i < NUM_STATS; i++)
242 if(stats_.stats[i][SPLAYER] == -1)
244 stats[i][SPLAYER] += stats_.stats[i][SPLAYER];
245 if(stats_.stats[i][STOTAL] != -1)
246 stats[i][STOTAL] += stats_.stats[i][STOTAL];