disabled hovering by default (needs tweaking!)
[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 "resources.h"
26
27 Statistics global_stats;
28
29 std::string
30 stat_name_to_string(int stat_enum)
31 {
32   switch(stat_enum)
33     {
34     case SCORE_STAT:
35       return "score";
36     case BADGUYS_SQUISHED_STAT:
37       return "badguys-squished";
38     case SHOTS_STAT:
39       return "shots";
40     case TIME_NEEDED_STAT:
41       return "time-needed";
42     case JUMPS_STAT:
43       return "jumps";
44     }
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     stats[i] = -1;
64 }
65
66 Statistics::~Statistics()
67 {
68 }
69
70 void
71 Statistics::parse(LispReader& reader)
72 {
73   for(int i = 0; i < NUM_STATS; i++)
74     reader.read_int(stat_name_to_string(i).c_str(), stats[i]);
75 }
76
77 void
78 Statistics::write(LispWriter& writer)
79 {
80   for(int i = 0; i < NUM_STATS; i++)
81     writer.write_int(stat_name_to_string(i), stats[i]);
82 }
83
84 #define TOTAL_DISPLAY_TIME 3400
85 #define FADING_TIME         600
86
87 void
88 Statistics::draw_worldmap_info(DrawingContext& context)
89 {
90   if(stats[SCORE_STAT] == -1)  // not initialized yet
91     return;
92
93   if(!timer.check())
94     {
95     timer.start(TOTAL_DISPLAY_TIME);
96     display_stat++;
97     if(display_stat >= NUM_STATS)
98       display_stat = 1;
99     }
100
101   int alpha;
102   if(timer.get_gone() < FADING_TIME)
103     alpha = timer.get_gone() * 255 / FADING_TIME;
104   else if(timer.get_left() < FADING_TIME)
105     alpha = timer.get_left() * 255 / FADING_TIME;
106   else
107     alpha = 255;
108
109   char str[128];
110
111   context.draw_text(white_small_text, _("Level Statistics"), Vector(550, 490), LAYER_GUI);
112
113   sprintf(str, _("Max score: %d"), stats[SCORE_STAT]);
114   context.draw_text(white_small_text, str, Vector(560, 506), LAYER_GUI);
115
116   if(display_stat == BADGUYS_SQUISHED_STAT)
117     sprintf(str, _("Max fragging: %d"), stats[BADGUYS_SQUISHED_STAT]);
118   else if(display_stat == SHOTS_STAT)
119     sprintf(str, _("Min shots: %d"), stats[SHOTS_STAT]);
120   else if(display_stat == TIME_NEEDED_STAT)
121     sprintf(str, _("Min time needed: %d"), stats[TIME_NEEDED_STAT]);
122   else// if(display_stat == JUMPS_STAT)
123     sprintf(str, _("Min jumps: %d"), stats[JUMPS_STAT]);
124
125   context.draw_text(white_small_text, str, Vector(560, 522), LAYER_GUI, NONE_EFFECT, alpha);
126 }
127
128 void
129 Statistics::draw_message_info(DrawingContext& context, std::string title)
130 {
131   if(stats[SCORE_STAT] == -1)  // not initialized yet
132     return;
133
134   context.draw_text_center(gold_text, title, Vector(0, 400), LAYER_GUI);
135
136   char str[128];
137   for(int i = 0; i < NUM_STATS; i++)
138     {
139     if(i == SCORE_STAT)
140       sprintf(str, _("Max score: %d"), stats[SCORE_STAT]);
141     else if(i == BADGUYS_SQUISHED_STAT)
142       sprintf(str, _("Max fragging: %d"), stats[BADGUYS_SQUISHED_STAT]);
143     else if(i == SHOTS_STAT)
144       sprintf(str, _("Min shots: %d"), stats[SHOTS_STAT]);
145     else if(i == TIME_NEEDED_STAT)
146       sprintf(str, _("Min time needed: %d"), stats[TIME_NEEDED_STAT]);
147     else// if(i == JUMPS_STAT)
148       sprintf(str, _("Min jumps: %d"), stats[JUMPS_STAT]);
149
150     context.draw_text_center(white_text, str, Vector(0, 430 + i*22), LAYER_GUI);
151     }
152 }
153
154 void
155 Statistics::add_points(int stat, int points)
156 {
157   stats[stat] += points;
158 }
159
160 int
161 Statistics::get_points(int stat)
162 {
163   return stats[stat];
164 }
165
166 void
167 Statistics::set_points(int stat, int points)
168 {
169   stats[stat] = points;
170 }
171
172 void
173 Statistics::reset()
174 {
175   for(int i = 0; i < NUM_STATS; i++)
176     stats[i] = 0;
177 }
178
179 void
180 Statistics::merge(Statistics& stats_)
181 {
182   stats[SCORE_STAT] = std::max(stats[SCORE_STAT], stats_.stats[SCORE_STAT]);
183   if(stats[JUMPS_STAT] != -1)
184     stats[JUMPS_STAT] = my_min(stats[JUMPS_STAT], stats_.stats[JUMPS_STAT]);
185   stats[BADGUYS_SQUISHED_STAT] =
186     std::max(stats[BADGUYS_SQUISHED_STAT], stats_.stats[BADGUYS_SQUISHED_STAT]);
187   stats[SHOTS_STAT] = my_min(stats[SHOTS_STAT], stats_.stats[SHOTS_STAT]);
188   stats[TIME_NEEDED_STAT] =
189     my_min(stats[TIME_NEEDED_STAT], stats_.stats[TIME_NEEDED_STAT]);
190 }
191
192 void
193 Statistics::operator+=(const Statistics& stats_)
194 {
195   for(int i = 0; i < NUM_STATS; i++)
196     stats[i] += stats_.stats[i];
197 }