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
20 #include "utils/lispreader.h"
21 #include "utils/lispwriter.h"
22 #include "video/drawing_context.h"
23 #include "app/gettext.h"
24 #include "app/globals.h"
25 #include "resources.h"
26 #include "statistics.h"
28 Statistics global_stats;
31 stat_name_to_string(int stat_enum)
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";;
54 return std::min(a, b);
57 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(LispReader& reader)
74 for(int i = 0; i < NUM_STATS; i++)
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]);
82 Statistics::write(LispWriter& writer)
84 for(int i = 0; i < NUM_STATS; i++)
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]);
91 #define TOTAL_DISPLAY_TIME 3400
92 #define FADING_TIME 600
94 #define WMAP_INFO_LEFT_X 520
95 #define WMAP_INFO_RIGHT_X 740
98 Statistics::draw_worldmap_info(DrawingContext& context)
100 if(stats[SCORE_STAT][SPLAYER] == -1) // not initialized yet
105 timer.start(TOTAL_DISPLAY_TIME);
107 if(display_stat >= NUM_STATS)
113 context.draw_text(white_small_text, _("- Best Level Statistics -"),
114 Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, 470),
115 CENTER_ALLIGN, LAYER_GUI);
117 sprintf(str, _("Max score:"));
118 context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 490), LEFT_ALLIGN, LAYER_GUI);
120 sprintf(str, "%d", stats[SCORE_STAT][SPLAYER]);
121 context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 490), RIGHT_ALLIGN, LAYER_GUI);
123 // draw other small info
126 if(timer.get_gone() < FADING_TIME)
127 alpha = timer.get_gone() * 255 / FADING_TIME;
128 else if(timer.get_left() < FADING_TIME)
129 alpha = timer.get_left() * 255 / FADING_TIME;
133 context.push_transform();
134 context.set_alpha(alpha);
136 if(display_stat == COINS_COLLECTED_STAT)
137 sprintf(str, _("Max coins collected:"));
138 else if(display_stat == BADGUYS_KILLED_STAT)
139 sprintf(str, _("Max fragging:"));
140 else// if(display_stat == TIME_NEEDED_STAT)
141 sprintf(str, _("Min time needed:"));
143 context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 508), LEFT_ALLIGN, LAYER_GUI);
145 if(display_stat == COINS_COLLECTED_STAT)
146 sprintf(str, "%d/%d", stats[COINS_COLLECTED_STAT][SPLAYER],
147 stats[COINS_COLLECTED_STAT][STOTAL]);
148 else if(display_stat == BADGUYS_KILLED_STAT)
149 sprintf(str, "%d/%d", stats[BADGUYS_KILLED_STAT][SPLAYER],
150 stats[BADGUYS_KILLED_STAT][STOTAL]);
151 else// if(display_stat == TIME_NEEDED_STAT)
152 sprintf(str, "%d/%d", stats[TIME_NEEDED_STAT][SPLAYER],
153 stats[TIME_NEEDED_STAT][STOTAL]);
155 context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 508), RIGHT_ALLIGN, LAYER_GUI);
157 context.pop_transform();
161 Statistics::draw_message_info(DrawingContext& context, std::string title)
163 if(stats[SCORE_STAT][SPLAYER] == -1) // not initialized yet
166 context.draw_text(gold_text, title, Vector(screen->w/2, 410), CENTER_ALLIGN, LAYER_GUI);
170 sprintf(str, _( "Max score: %d"), stats[SCORE_STAT][SPLAYER]);
171 context.draw_text(white_text, str, Vector(screen->w/2, 450), CENTER_ALLIGN, LAYER_GUI);
173 for(int i = 1; i < NUM_STATS; i++)
175 if(i == COINS_COLLECTED_STAT)
176 sprintf(str, _("Max coins collected: %d / %d"),
177 stats[COINS_COLLECTED_STAT][SPLAYER],
178 stats[COINS_COLLECTED_STAT][STOTAL]);
179 else if(i == BADGUYS_KILLED_STAT)
180 sprintf(str, _("Max fragging: %d / %d"),
181 stats[BADGUYS_KILLED_STAT][SPLAYER],
182 stats[BADGUYS_KILLED_STAT][STOTAL]);
183 else// if(i == TIME_NEEDED_STAT)
184 sprintf(str, _("Min time needed: %d / %d"),
185 stats[TIME_NEEDED_STAT][SPLAYER],
186 stats[TIME_NEEDED_STAT][STOTAL]);
189 context.draw_text(white_small_text, str, Vector(screen->w/2, 462 + i*18), CENTER_ALLIGN, LAYER_GUI);
194 Statistics::add_points(int stat, int points)
196 stats[stat][SPLAYER] += points;
200 Statistics::get_points(int stat)
202 return stats[stat][SPLAYER];
206 Statistics::set_points(int stat, int points)
208 stats[stat][SPLAYER] = points;
212 Statistics::set_total_points(int stat, int points)
214 stats[stat][STOTAL] = points;
220 for(int i = 0; i < NUM_STATS; i++)
221 stats[i][SPLAYER] = 0;
225 Statistics::merge(Statistics& stats_)
227 stats[SCORE_STAT][SPLAYER] = std::max(stats[SCORE_STAT][SPLAYER], stats_.stats[SCORE_STAT][SPLAYER]);
228 stats[COINS_COLLECTED_STAT][SPLAYER] = std::max(stats[COINS_COLLECTED_STAT][SPLAYER], stats_.stats[COINS_COLLECTED_STAT][SPLAYER]);
229 stats[BADGUYS_KILLED_STAT][SPLAYER] =
230 std::max(stats[BADGUYS_KILLED_STAT][SPLAYER], stats_.stats[BADGUYS_KILLED_STAT][SPLAYER]);
231 stats[TIME_NEEDED_STAT][SPLAYER] =
232 my_min(stats[TIME_NEEDED_STAT][SPLAYER], stats_.stats[TIME_NEEDED_STAT][SPLAYER]);
234 stats[COINS_COLLECTED_STAT][STOTAL] = stats_.stats[COINS_COLLECTED_STAT][STOTAL];
235 stats[BADGUYS_KILLED_STAT][STOTAL] = stats_.stats[BADGUYS_KILLED_STAT][STOTAL];
236 stats[TIME_NEEDED_STAT][STOTAL] = stats_.stats[TIME_NEEDED_STAT][STOTAL];
240 Statistics::operator+=(const Statistics& stats_)
242 for(int i = 0; i < NUM_STATS; i++)
244 if(stats_.stats[i][SPLAYER] == -1)
246 stats[i][SPLAYER] += stats_.stats[i][SPLAYER];
247 if(stats_.stats[i][STOTAL] != -1)
248 stats[i][STOTAL] += stats_.stats[i][STOTAL];