From e3366884d0487ca6e600f5d5dd5f0deabd782ea7 Mon Sep 17 00:00:00 2001 From: Ricardo Cruz Date: Fri, 17 Sep 2004 12:22:40 +0000 Subject: [PATCH] Added Total to statistics. Changes stuff that keeps track. Bug: percentage on draw_message_info() ain't working well. SVN-Revision: 1937 --- src/badguy.cpp | 6 +-- src/gameloop.cpp | 7 +++- src/level.cpp | 22 ++++++++++ src/level.h | 3 ++ src/player.cpp | 2 - src/sector.cpp | 19 ++++++++- src/sector.h | 3 ++ src/statistics.cpp | 121 ++++++++++++++++++++++++++++++----------------------- src/statistics.h | 13 ++++-- 9 files changed, 131 insertions(+), 65 deletions(-) diff --git a/src/badguy.cpp b/src/badguy.cpp index ac0e59b0e..1d9d19729 100644 --- a/src/badguy.cpp +++ b/src/badguy.cpp @@ -1072,7 +1072,6 @@ BadGuy::squish(Player* player) 25 * player_status.score_multiplier); SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos()); - global_stats.add_points(BADGUYS_SQUISHED_STAT, 1); player_status.score_multiplier++; return; @@ -1125,7 +1124,6 @@ BadGuy::squish(Player* player) Sector::current()->add_score(Vector(base.x, base.y), 25 * player_status.score_multiplier); - global_stats.add_points(BADGUYS_SQUISHED_STAT, 1); player_status.score_multiplier++; // simply remove the fish... @@ -1162,7 +1160,6 @@ BadGuy::squish(Player* player) player->bounce(this); base.y += 66 - base.height; - global_stats.add_points(BADGUYS_SQUISHED_STAT, 1); Sector::current()->add_score(Vector(base.x, base.y), 25 * player_status.score_multiplier); player_status.score_multiplier++; @@ -1180,6 +1177,9 @@ BadGuy::kill_me(int score) if(kind == BAD_BOMB) return; + if(mode != HELD) + global_stats.add_points(BADGUYS_KILLED_STAT, 1); + dying = DYING_FALLING; if(kind == BAD_MRICEBLOCK) { set_sprite(img_mriceblock_falling_left, img_mriceblock_falling_right); diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 5eb442969..a6c3a5fcb 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -129,6 +129,11 @@ GameSession::restart_level() if(flip_level) level->do_vertical_flip(); + global_stats.reset(); + global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins()); + global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys()); + global_stats.set_total_points(TIME_NEEDED_STAT, level->time_left); + currentsector = level->get_sector("main"); if(!currentsector) Termination::abort("Level has no main sector.", ""); @@ -152,8 +157,6 @@ GameSession::restart_level() levelintro(); } - global_stats.reset(); - time_left.init(true); start_timers(); currentsector->play_music(LEVEL_MUSIC); diff --git a/src/level.cpp b/src/level.cpp index 37ca6550e..570b4b4a5 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -39,6 +39,7 @@ #include "resources.h" #include "gameobjs.h" #include "utils/lispwriter.h" +#include "tilemap.h" using namespace std; @@ -156,3 +157,24 @@ Level::get_sector(const std::string& name) return i->second; } + +int +Level::get_total_badguys() +{ + int total_badguys = 0; + for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) + total_badguys += i->second->get_total_badguys(); + return total_badguys; +} + +int +Level::get_total_coins() +{ + int total_coins = 0; + for(Sectors::iterator it = sectors.begin(); it != sectors.end(); ++it) + for(int x = 0; x < it->second->solids->get_width(); x++) + for(int y = 0; y < it->second->solids->get_height(); y++) + if(it->second->solids->get_tile(x,y)->attributes & Tile::COIN) + total_coins++; + return total_coins; +} diff --git a/src/level.h b/src/level.h index 6f29434bc..9a5d3db6a 100644 --- a/src/level.h +++ b/src/level.h @@ -61,6 +61,9 @@ public: Sector* get_sector(const std::string& name); + int get_total_badguys(); + int get_total_coins(); + private: void load_old_format(LispReader& reader); }; diff --git a/src/player.cpp b/src/player.cpp index 90c78ae0a..20a05d4af 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -522,8 +522,6 @@ Player::handle_vertical_input() // Press jump key if(input.up == DOWN && can_jump && on_ground()) { - global_stats.add_points(JUMPS_STAT, 1); - if(duck) { // only jump a little bit when in duck mode { physic.set_velocity_y(3); } else { diff --git a/src/sector.cpp b/src/sector.cpp index 9290da48f..b980e8ed5 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -736,7 +736,6 @@ Sector::add_bullet(const Vector& pos, float xm, Direction dir) throw std::runtime_error("wrong bullet type."); add_object(new_bullet); - global_stats.add_points(SHOTS_STAT, 1); SoundManager::get()->play_sound(IDToSound(SND_SHOOT)); return true; @@ -795,6 +794,7 @@ Sector::trybreakbrick(const Vector& pos, bool small) SoundManager::get()->play_sound(IDToSound(SND_DISTRO)); global_stats.add_points(SCORE_STAT, SCORE_DISTRO); + global_stats.add_points(COINS_COLLECTED_STAT, 1); player_status.distros++; return true; } @@ -849,6 +849,7 @@ Sector::tryemptybox(const Vector& pos, Direction col_side) add_bouncy_distro(Vector(posx, posy)); SoundManager::get()->play_sound(IDToSound(SND_DISTRO)); global_stats.add_points(SCORE_STAT, SCORE_DISTRO); + global_stats.add_points(COINS_COLLECTED_STAT, 1); player_status.distros++; break; @@ -906,7 +907,7 @@ Sector::trygrabdistro(const Vector& pos, int bounciness) solids->change_at(pos, tile->next_tile); SoundManager::get()->play_sound(IDToSound(SND_DISTRO)); - + if (bounciness == BOUNCE) { add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32, @@ -914,6 +915,7 @@ Sector::trygrabdistro(const Vector& pos, int bounciness) } global_stats.add_points(SCORE_STAT, SCORE_DISTRO); + global_stats.add_points(COINS_COLLECTED_STAT, 1); player_status.distros++; } @@ -992,3 +994,16 @@ Sector::get_music_type() { return currentmusic; } + +int +Sector::get_total_badguys() +{ + int total_badguys = 0; + for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i) + { + BadGuy* badguy = dynamic_cast (*i); + if(badguy) + total_badguys++; + } + return total_badguys; +} diff --git a/src/sector.h b/src/sector.h index d4291c800..4485f0572 100644 --- a/src/sector.h +++ b/src/sector.h @@ -139,6 +139,9 @@ public: static Sector* current() { return _current; } + /** Get total number of some stuff */ + int get_total_badguys(); + private: void load_music(); diff --git a/src/statistics.cpp b/src/statistics.cpp index 52daaa195..82ed04ed2 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -34,14 +34,12 @@ stat_name_to_string(int stat_enum) { case SCORE_STAT: return "score"; - case BADGUYS_SQUISHED_STAT: - return "badguys-squished"; - case SHOTS_STAT: - return "shots"; + case COINS_COLLECTED_STAT: + return "coins-collected"; + case BADGUYS_KILLED_STAT: + return "badguys-killed"; case TIME_NEEDED_STAT: - return "time-needed"; - case JUMPS_STAT: - return "jumps"; + return "time-needed";; } } @@ -61,7 +59,8 @@ Statistics::Statistics() display_stat = 1; for(int i = 0; i < NUM_STATS; i++) - stats[i] = -1; + for(int j = 0; j < 2; j++) + stats[i][j] = -1; } Statistics::~Statistics() @@ -72,26 +71,32 @@ void Statistics::parse(LispReader& reader) { for(int i = 0; i < NUM_STATS; i++) - reader.read_int(stat_name_to_string(i).c_str(), stats[i]); + { + reader.read_int(stat_name_to_string(i).c_str(), stats[i][SPLAYER]); + reader.read_int((stat_name_to_string(i) + "-total").c_str(), stats[i][STOTAL]); + } } void Statistics::write(LispWriter& writer) { for(int i = 0; i < NUM_STATS; i++) - writer.write_int(stat_name_to_string(i), stats[i]); + { + writer.write_int(stat_name_to_string(i), stats[i][SPLAYER]); + writer.write_int(stat_name_to_string(i) + "-total", stats[i][STOTAL]); + } } #define TOTAL_DISPLAY_TIME 3400 #define FADING_TIME 600 -#define WMAP_INFO_LEFT_X 555 -#define WMAP_INFO_RIGHT_X 705 +#define WMAP_INFO_LEFT_X 540 +#define WMAP_INFO_RIGHT_X 720 void Statistics::draw_worldmap_info(DrawingContext& context) { - if(stats[SCORE_STAT] == -1) // not initialized yet + if(stats[SCORE_STAT][SPLAYER] == -1) // not initialized yet return; if(!timer.check()) @@ -112,37 +117,36 @@ Statistics::draw_worldmap_info(DrawingContext& context) char str[128]; - context.draw_text(white_small_text, _("Level Statistics"), + context.draw_text(white_small_text, _("Best Level Statistics"), Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, 490), CENTER_ALLIGN, LAYER_GUI); sprintf(str, _("Max score:")); context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 506), LEFT_ALLIGN, LAYER_GUI); - sprintf(str, "%d", stats[SCORE_STAT]); + sprintf(str, "%d", stats[SCORE_STAT][SPLAYER]); context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 506), RIGHT_ALLIGN, LAYER_GUI); // draw other small info - if(display_stat == BADGUYS_SQUISHED_STAT) + if(display_stat == COINS_COLLECTED_STAT) + sprintf(str, _("Max coins collected:")); + else if(display_stat == BADGUYS_KILLED_STAT) sprintf(str, _("Max fragging:")); - else if(display_stat == SHOTS_STAT) - sprintf(str, _("Min shots:")); - else if(display_stat == TIME_NEEDED_STAT) + else// if(display_stat == TIME_NEEDED_STAT) sprintf(str, _("Min time needed:")); - else// if(display_stat == JUMPS_STAT) - sprintf(str, _("Min jumps:")); context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 522), LEFT_ALLIGN, LAYER_GUI, NONE_EFFECT, alpha); - if(display_stat == BADGUYS_SQUISHED_STAT) - sprintf(str, "%d", stats[BADGUYS_SQUISHED_STAT]); - else if(display_stat == SHOTS_STAT) - sprintf(str, "%d", stats[SHOTS_STAT]); - else if(display_stat == TIME_NEEDED_STAT) - sprintf(str, "%d", stats[TIME_NEEDED_STAT]); - else// if(display_stat == JUMPS_STAT) - sprintf(str, "%d", stats[JUMPS_STAT]); + if(display_stat == COINS_COLLECTED_STAT) + sprintf(str, "%d/%d", stats[COINS_COLLECTED_STAT][SPLAYER], + stats[COINS_COLLECTED_STAT][STOTAL]); + else if(display_stat == BADGUYS_KILLED_STAT) + sprintf(str, "%d/%d", stats[BADGUYS_KILLED_STAT][SPLAYER], + stats[BADGUYS_KILLED_STAT][STOTAL]); + else// if(display_stat == TIME_NEEDED_STAT) + sprintf(str, "%d/%d", stats[TIME_NEEDED_STAT][SPLAYER], + stats[TIME_NEEDED_STAT][STOTAL]); context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 522), RIGHT_ALLIGN, LAYER_GUI, NONE_EFFECT, alpha); } @@ -150,26 +154,27 @@ Statistics::draw_worldmap_info(DrawingContext& context) void Statistics::draw_message_info(DrawingContext& context, std::string title) { - if(stats[SCORE_STAT] == -1) // not initialized yet + if(stats[SCORE_STAT][SPLAYER] == -1) // not initialized yet return; context.draw_text(gold_text, title, Vector(screen->w/2, 410), CENTER_ALLIGN, LAYER_GUI); char str[128]; - sprintf(str, _( "Max score: %d"), stats[SCORE_STAT]); + sprintf(str, _( "Max score: %d"), stats[SCORE_STAT][SPLAYER]); context.draw_text(white_text, str, Vector(screen->w/2, 450), CENTER_ALLIGN, LAYER_GUI); for(int i = 1; i < NUM_STATS; i++) { - if(i == BADGUYS_SQUISHED_STAT) - sprintf(str, _("Max fragging: %d"), stats[BADGUYS_SQUISHED_STAT]); - else if(i == SHOTS_STAT) - sprintf(str, _("Min shots: %d"), stats[SHOTS_STAT]); - else if(i == TIME_NEEDED_STAT) - sprintf(str, _("Min time needed: %d"), stats[TIME_NEEDED_STAT]); - else// if(i == JUMPS_STAT) - sprintf(str, _("Min jumps: %d"), stats[JUMPS_STAT]); + if(i == COINS_COLLECTED_STAT) + sprintf(str, _("Max coins collected: %d"), ((float)stats[COINS_COLLECTED_STAT][SPLAYER] / + (float)stats[COINS_COLLECTED_STAT][STOTAL]) * 100); + else if(i == BADGUYS_KILLED_STAT) + sprintf(str, _("Max fragging: %d"), ((float)stats[BADGUYS_KILLED_STAT][SPLAYER] / + (float)stats[BADGUYS_KILLED_STAT][STOTAL]) * 100); + else// if(i == TIME_NEEDED_STAT) + sprintf(str, _("Min time needed: %d"), ((float)stats[TIME_NEEDED_STAT][SPLAYER] / + (float)stats[TIME_NEEDED_STAT][STOTAL]) * 100); context.draw_text(white_small_text, str, Vector(screen->w/2, 462 + i*18), CENTER_ALLIGN, LAYER_GUI); } @@ -178,44 +183,56 @@ Statistics::draw_message_info(DrawingContext& context, std::string title) void Statistics::add_points(int stat, int points) { - stats[stat] += points; + stats[stat][SPLAYER] += points; } int Statistics::get_points(int stat) { - return stats[stat]; + return stats[stat][SPLAYER]; } void Statistics::set_points(int stat, int points) { - stats[stat] = points; + stats[stat][SPLAYER] = points; +} + +void +Statistics::set_total_points(int stat, int points) +{ + stats[stat][STOTAL] = points; } void Statistics::reset() { for(int i = 0; i < NUM_STATS; i++) - stats[i] = 0; + stats[i][SPLAYER] = 0; } void Statistics::merge(Statistics& stats_) { - stats[SCORE_STAT] = std::max(stats[SCORE_STAT], stats_.stats[SCORE_STAT]); - if(stats[JUMPS_STAT] != -1) - stats[JUMPS_STAT] = my_min(stats[JUMPS_STAT], stats_.stats[JUMPS_STAT]); - stats[BADGUYS_SQUISHED_STAT] = - std::max(stats[BADGUYS_SQUISHED_STAT], stats_.stats[BADGUYS_SQUISHED_STAT]); - stats[SHOTS_STAT] = my_min(stats[SHOTS_STAT], stats_.stats[SHOTS_STAT]); - stats[TIME_NEEDED_STAT] = - my_min(stats[TIME_NEEDED_STAT], stats_.stats[TIME_NEEDED_STAT]); + stats[SCORE_STAT][SPLAYER] = std::max(stats[SCORE_STAT][SPLAYER], stats_.stats[SCORE_STAT][SPLAYER]); + stats[COINS_COLLECTED_STAT][SPLAYER] = std::max(stats[COINS_COLLECTED_STAT][SPLAYER], stats_.stats[COINS_COLLECTED_STAT][SPLAYER]); + stats[BADGUYS_KILLED_STAT][SPLAYER] = + std::max(stats[BADGUYS_KILLED_STAT][SPLAYER], stats_.stats[BADGUYS_KILLED_STAT][SPLAYER]); + stats[TIME_NEEDED_STAT][SPLAYER] = + my_min(stats[TIME_NEEDED_STAT][SPLAYER], stats_.stats[TIME_NEEDED_STAT][SPLAYER]); + + stats[COINS_COLLECTED_STAT][STOTAL] = stats_.stats[COINS_COLLECTED_STAT][STOTAL]; + stats[BADGUYS_KILLED_STAT][STOTAL] = stats_.stats[BADGUYS_KILLED_STAT][STOTAL]; + stats[TIME_NEEDED_STAT][STOTAL] = stats_.stats[TIME_NEEDED_STAT][STOTAL]; } void Statistics::operator+=(const Statistics& stats_) { for(int i = 0; i < NUM_STATS; i++) - stats[i] += stats_.stats[i]; + { + stats[i][SPLAYER] += stats_.stats[i][SPLAYER]; + if(stats_.stats[i][STOTAL] != -1) + stats[i][STOTAL] += stats_.stats[i][STOTAL]; + } } diff --git a/src/statistics.h b/src/statistics.h index 72250c35b..bb123734b 100644 --- a/src/statistics.h +++ b/src/statistics.h @@ -30,12 +30,14 @@ class LispWriter; class DrawingContext; } +#define SPLAYER 0 +#define STOTAL 1 + enum { SCORE_STAT, - BADGUYS_SQUISHED_STAT, - SHOTS_STAT, + COINS_COLLECTED_STAT, + BADGUYS_KILLED_STAT, TIME_NEEDED_STAT, - JUMPS_STAT, NUM_STATS }; @@ -46,6 +48,7 @@ enum { class Statistics { public: + // don't forget to call reset() to init stat Statistics(); ~Statistics(); @@ -64,6 +67,8 @@ public: void set_points(int stat, int points); int get_points(int stat); + void set_total_points(int stat, int points); + /* Reset statistics */ void reset(); @@ -74,7 +79,7 @@ public: void operator+=(const Statistics& o); private: - int stats[NUM_STATS]; + int stats[NUM_STATS][2]; Timer timer; int display_stat; -- 2.11.0