Added a Jump 'n Bump like way to show statistics.
[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 Statistics::Statistics()
48 {
49   timer.init(true);
50   display_stat = 1;
51
52   for(int i = 0; i < NUM_STATS; i++)
53     stats[i] = -1;
54 }
55
56 Statistics::~Statistics()
57 {
58 }
59
60 void
61 Statistics::parse(LispReader& reader)
62 {
63   for(int i = 0; i < NUM_STATS; i++)
64     reader.read_int(stat_name_to_string(i).c_str(), stats[i]);
65 }
66
67 void
68 Statistics::write(LispWriter& writer)
69 {
70   for(int i = 0; i < NUM_STATS; i++)
71     writer.write_int(stat_name_to_string(i), stats[i]);
72 }
73
74 #define TOTAL_DISPLAY_TIME 3400
75 #define FADING_TIME         600
76
77 void
78 Statistics::draw_worldmap_info(DrawingContext& context)
79 {
80   if(!timer.check())
81     {
82     timer.start(TOTAL_DISPLAY_TIME);
83     display_stat++;
84     if(display_stat >= NUM_STATS)
85       display_stat = 1;
86     }
87
88   int alpha;
89   if(timer.get_gone() < FADING_TIME)
90     alpha = timer.get_gone() * 255 / FADING_TIME;
91   else if(timer.get_left() < FADING_TIME)
92     alpha = timer.get_left() * 255 / FADING_TIME;
93   else
94     alpha = 255;
95
96   char str[128];
97
98   context.draw_text(white_small_text, _("Level Statistics"), Vector(550, 490), LAYER_GUI);
99
100   sprintf(str, _("Max score: %d"), stats[SCORE_STAT]);
101   context.draw_text(white_small_text, str, Vector(560, 506), LAYER_GUI);
102
103   if(display_stat == BADGUYS_SQUISHED_STAT)
104     sprintf(str, _("Max fragging: %d"), stats[BADGUYS_SQUISHED_STAT]);
105   else if(display_stat == SHOTS_STAT)
106     sprintf(str, _("Min shots: %d"), stats[SHOTS_STAT]);
107   else if(display_stat == TIME_NEEDED_STAT)
108     sprintf(str, _("Min time needed: %d"), stats[TIME_NEEDED_STAT]);
109   else// if(display_stat == JUMPS_STAT)
110     sprintf(str, _("Min jumps: %d"), stats[JUMPS_STAT]);
111
112   context.draw_text(white_small_text, str, Vector(560, 522), LAYER_GUI, NONE_EFFECT, alpha);
113 }
114
115 void
116 Statistics::draw_message_info(DrawingContext& context)
117 {
118   // TODO
119 }
120
121 void
122 Statistics::add_points(int stat, int points)
123 {
124   stats[stat] += points;
125 }
126
127 int
128 Statistics::get_points(int stat)
129 {
130   return stats[stat];
131 }
132
133 void
134 Statistics::set_points(int stat, int points)
135 {
136   stats[stat] = points;
137 }
138
139 void
140 Statistics::reset()
141 {
142   for(int i = 0; i < NUM_STATS; i++)
143     stats[i] = 0;
144 }
145
146 void
147 Statistics::merge(Statistics& stats_)
148 {
149   stats[SCORE_STAT] = std::max(stats[SCORE_STAT], stats_.stats[SCORE_STAT]);
150   stats[JUMPS_STAT] = std::min(stats[JUMPS_STAT], stats_.stats[JUMPS_STAT]);
151   stats[BADGUYS_SQUISHED_STAT] =
152     std::max(stats[BADGUYS_SQUISHED_STAT], stats_.stats[BADGUYS_SQUISHED_STAT]);
153   stats[SHOTS_STAT] = std::min(stats[SHOTS_STAT], stats_.stats[SHOTS_STAT]);
154   stats[TIME_NEEDED_STAT] =
155     std::min(stats[TIME_NEEDED_STAT], stats_.stats[TIME_NEEDED_STAT]);
156 }
157
158 void
159 Statistics::operator+=(const Statistics& stats_)
160 {
161   for(int i = 0; i < NUM_STATS; i++)
162     stats[i] += stats_.stats[i];
163 }