Made statistics to keep track of also: bad guys squished, shots, time needed and...
[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 "resources.h"
25
26 Statistics global_stats;
27
28 std::string
29 stat_name_to_string(int stat_enum)
30 {
31   switch(stat_enum)
32     {
33     case SCORE_STAT:
34       return "score";
35     case BADGUYS_SQUISHED_STAT:
36       return "badguys-squished";
37     case SHOTS_STAT:
38       return "shots";
39     case TIME_NEEDED_STAT:
40       return "time-needed";
41     case JUMPS_STAT:
42       return "jumps";
43     }
44 }
45
46 Statistics::Statistics()
47 {
48   reset();
49 }
50
51 Statistics::~Statistics()
52 {
53 }
54
55 void
56 Statistics::parse(LispReader& reader)
57 {
58   for(int i = 0; i < NUM_STATS; i++)
59     reader.read_int(stat_name_to_string(i).c_str(), stats[i]);
60 }
61
62 void
63 Statistics::write(LispWriter& writer)
64 {
65   for(int i = 0; i < NUM_STATS; i++)
66     writer.write_int(stat_name_to_string(i), stats[i]);
67 }
68
69 void
70 Statistics::draw_worldmap_info(DrawingContext& context)
71 {
72   char str[128];
73
74   //TODO: this is just a simple message, will be imporved
75   sprintf(str, "Level Max Score: %d", stats[SCORE_STAT]);
76   context.draw_text(white_small_text, str, Vector(580, 580), LAYER_GUI);
77 }
78
79 void
80 Statistics::draw_message_info(DrawingContext& context)
81 {
82   // TODO
83 }
84
85 void
86 Statistics::add_points(int stat, int points)
87 {
88   stats[stat] += points;
89 }
90
91 int
92 Statistics::get_points(int stat)
93 {
94   return stats[stat];
95 }
96
97 void
98 Statistics::set_points(int stat, int points)
99 {
100   stats[stat] = points;
101 }
102
103 void
104 Statistics::reset()
105 {
106   for(int i = 0; i < NUM_STATS; i++)
107     stats[i] = 0;
108 }
109
110 void
111 Statistics::merge(Statistics& stats_)
112 {
113   stats[SCORE_STAT] = std::max(stats[SCORE_STAT], stats_.stats[SCORE_STAT]);
114   stats[JUMPS_STAT] = std::min(stats[JUMPS_STAT], stats_.stats[JUMPS_STAT]);
115   stats[BADGUYS_SQUISHED_STAT] =
116     std::max(stats[BADGUYS_SQUISHED_STAT], stats_.stats[BADGUYS_SQUISHED_STAT]);
117   stats[SHOTS_STAT] = std::min(stats[SHOTS_STAT], stats_.stats[SHOTS_STAT]);
118   stats[TIME_NEEDED_STAT] =
119     std::min(stats[TIME_NEEDED_STAT], stats_.stats[TIME_NEEDED_STAT]);
120 }
121
122 void
123 Statistics::operator+=(const Statistics& stats_)
124 {
125   for(int i = 0; i < NUM_STATS; i++)
126     stats[i] += stats_.stats[i];
127 }