Statistics Class Reference

This class is a layer between level and worldmap to keep track of stuff like scores, and minor, but funny things, like number of jumps and stuff. More...

#include <statistics.hpp>

List of all members.

Public Member Functions

 Statistics ()
 Creates new statistics, call reset() before counting.
 ~Statistics ()
void serialize_to_squirrel (HSQUIRRELVM vm)
 serialize statistics object as squirrel table "statistics"
void unserialize_from_squirrel (HSQUIRRELVM vm)
 unserialize statistics object from squirrel table "statistics"
void draw_worldmap_info (DrawingContext &context)
 draw worldmap stat HUD
void draw_endseq_panel (DrawingContext &context, Statistics *best_stats, SurfacePtr backdrop)
 draw panel shown during level's end sequence
void zero ()
 Set stats to zero.
void reset ()
 Set stats (but not totals) to zero.
void merge (const Statistics &stats)
 Given another Statistics object finds the best of each one.
void operator+= (const Statistics &o)
 Add two Statistics objects.
void declare_invalid ()
 marks statistics as invalid for their entire lifetime (e.g.

Static Public Member Functions

static std::string coins_to_string (int coins, int total_coins)
static std::string frags_to_string (int badguys, int total_badguys)
static std::string time_to_string (float time)
static std::string secrets_to_string (int secrets, int total_secrets)

Public Attributes

int coins
 coins collected
int total_coins
 coins in level
int badguys
 badguys actively killed
int total_badguys
 (vincible) badguys in level
float time
 seconds needed
int secrets
 secret areas found
int total_secrets
 secret areas in level

Private Attributes

bool valid
 stores whether these statistics can be trusted

Static Private Attributes

static Color header_color
static Color text_color


Detailed Description

This class is a layer between level and worldmap to keep track of stuff like scores, and minor, but funny things, like number of jumps and stuff.

Definition at line 34 of file statistics.hpp.


Constructor & Destructor Documentation

Statistics::Statistics (  ) 

Creates new statistics, call reset() before counting.

Definition at line 42 of file statistics.cpp.

References SCREEN_HEIGHT, SCREEN_WIDTH, WMAP_INFO_LEFT_X, WMAP_INFO_RIGHT_X, WMAP_INFO_TOP_Y1, and WMAP_INFO_TOP_Y2.

00042                        : 
00043   coins(nv_coins), 
00044   total_coins(nv_coins), 
00045   badguys(nv_badguys), 
00046   total_badguys(nv_badguys), 
00047   time(nv_time), 
00048   secrets(nv_secrets), 
00049   total_secrets(nv_secrets), 
00050   valid(true) 
00051 {
00052   WMAP_INFO_LEFT_X = (SCREEN_WIDTH/2 + 80) + 32;
00053   WMAP_INFO_RIGHT_X = SCREEN_WIDTH/2 + 368;
00054   WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT/2 + 172 - 16;
00055   WMAP_INFO_TOP_Y2 = SCREEN_HEIGHT/2 + 172;
00056 }

Statistics::~Statistics (  ) 

Definition at line 58 of file statistics.cpp.

00059 {
00060 }


Member Function Documentation

void Statistics::serialize_to_squirrel ( HSQUIRRELVM  vm  ) 

serialize statistics object as squirrel table "statistics"

Definition at line 89 of file statistics.cpp.

References badguys, coins, nv_badguys, nv_coins, nv_secrets, nv_time, secrets, scripting::store_float(), scripting::store_int(), time, total_badguys, total_coins, and total_secrets.

Referenced by worldmap::WorldMap::save_state().

00090 {
00091   // TODO: there's some bug in the unserialization routines that breaks stuff when an empty statistics table is written, so -- as a workaround -- let's make sure we will actually write something first
00092   if (!((coins != nv_coins) || (total_coins != nv_coins) || (badguys != nv_badguys) || (total_badguys != nv_badguys) || (time != nv_time) || (secrets != nv_secrets) || (total_secrets != nv_secrets))) return;
00093 
00094   sq_pushstring(vm, "statistics", -1);
00095   sq_newtable(vm);
00096   if (coins != nv_coins) scripting::store_int(vm, "coins-collected", coins);
00097   if (total_coins != nv_coins) scripting::store_int(vm, "coins-collected-total", total_coins);
00098   if (badguys != nv_badguys) scripting::store_int(vm, "badguys-killed", badguys);
00099   if (total_badguys != nv_badguys) scripting::store_int(vm, "badguys-killed-total", total_badguys);
00100   if (time != nv_time) scripting::store_float(vm, "time-needed", time);
00101   if (secrets != nv_secrets) scripting::store_int(vm, "secrets-found", secrets);
00102   if (total_secrets != nv_secrets) scripting::store_int(vm, "secrets-found-total", total_secrets);
00103   sq_createslot(vm, -3);
00104 }

void Statistics::unserialize_from_squirrel ( HSQUIRRELVM  vm  ) 

unserialize statistics object from squirrel table "statistics"

Definition at line 107 of file statistics.cpp.

References badguys, coins, scripting::get_float(), scripting::get_int(), secrets, time, total_badguys, total_coins, and total_secrets.

Referenced by worldmap::WorldMap::load_state().

00108 {
00109   sq_pushstring(vm, "statistics", -1);
00110   if(SQ_FAILED(sq_get(vm, -2))) {
00111     return;
00112   }
00113   scripting::get_int(vm, "coins-collected", coins);
00114   scripting::get_int(vm, "coins-collected-total", total_coins);
00115   scripting::get_int(vm, "badguys-killed", badguys);
00116   scripting::get_int(vm, "badguys-killed-total", total_badguys);
00117   scripting::get_float(vm, "time-needed", time);
00118   scripting::get_int(vm, "secrets-found", secrets);
00119   scripting::get_int(vm, "secrets-found-total", total_secrets);
00120   sq_pop(vm, 1);
00121 }

void Statistics::draw_worldmap_info ( DrawingContext context  ) 

draw worldmap stat HUD

Definition at line 124 of file statistics.cpp.

References _(), ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, badguys, coins, coins_to_string(), DrawingContext::draw_text(), frags_to_string(), header_color, LAYER_HUD, log_debug, nv_coins, secrets, secrets_to_string(), Resources::small_font, time, time_to_string(), total_badguys, total_coins, total_secrets, valid, WMAP_INFO_LEFT_X, WMAP_INFO_RIGHT_X, WMAP_INFO_TOP_Y1, and WMAP_INFO_TOP_Y2.

00125 {
00126   // skip draw if level was never played
00127   if (coins == nv_coins) return;
00128 
00129   // skip draw if stats were declared invalid
00130   if (!valid) return;
00131 
00132   context.draw_text(Resources::small_font, std::string("- ") + _("Best Level Statistics") + " -", 
00133                     Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1), 
00134                     ALIGN_CENTER, LAYER_HUD,Statistics::header_color);
00135 
00136   std::string caption_buf;
00137   std::string stat_buf;
00138   float posy = WMAP_INFO_TOP_Y2;
00139   for (int stat_no = 0; stat_no < 4; stat_no++) {
00140     switch (stat_no)
00141     {
00142       case 0:
00143         caption_buf = _("Max coins collected:");
00144         stat_buf = coins_to_string(coins, total_coins);
00145         break;
00146       case 1:
00147         caption_buf = _("Max fragging:");
00148         stat_buf = frags_to_string(badguys, total_badguys);
00149         break;
00150       case 2:
00151         caption_buf = _("Min time needed:");
00152         stat_buf = time_to_string(time);
00153         break;
00154       case 3:
00155         caption_buf = _("Max secrets found:");
00156         stat_buf = secrets_to_string(secrets, total_secrets);
00157         break;
00158       default:
00159         log_debug << "Invalid stat requested to be drawn" << std::endl;
00160         break;
00161     }
00162 
00163     context.draw_text(Resources::small_font, caption_buf, Vector(WMAP_INFO_LEFT_X, posy), ALIGN_LEFT, LAYER_HUD, Statistics::header_color);
00164     context.draw_text(Resources::small_font, stat_buf, Vector(WMAP_INFO_RIGHT_X, posy), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
00165     posy += Resources::small_font->get_height() + 2;
00166   }
00167 
00168 }

void Statistics::draw_endseq_panel ( DrawingContext context,
Statistics best_stats,
SurfacePtr  backdrop 
)

draw panel shown during level's end sequence

Definition at line 171 of file statistics.cpp.

References _(), ALIGN_LEFT, ALIGN_RIGHT, coins, coins_to_string(), DrawingContext::draw_surface(), DrawingContext::draw_text(), header_color, LAYER_HUD, Resources::normal_font, nv_coins, DrawingContext::pop_transform(), DrawingContext::push_transform(), SCREEN_HEIGHT, SCREEN_WIDTH, secrets, secrets_to_string(), DrawingContext::set_alpha(), text_color, time, time_to_string(), total_coins, total_secrets, and valid.

00172 {
00173   // skip draw if level was never played
00174   // TODO: do we need this?
00175   if (coins == nv_coins) return;
00176 
00177   // skip draw if stats were declared invalid
00178   if (!valid) return;
00179 
00180   // abort if we have no backdrop
00181   if (!backdrop) return;
00182 
00183   int box_w = 220+110+110;
00184   int box_h = 30+20+20+20;
00185   int box_x = (int)((SCREEN_WIDTH - box_w) / 2);
00186   int box_y = (int)(SCREEN_HEIGHT / 2) - box_h;
00187 
00188   int bd_w = (int)backdrop->get_width();
00189   int bd_h = (int)backdrop->get_height();
00190   int bd_x = (int)((SCREEN_WIDTH - bd_w) / 2);
00191   int bd_y = box_y + (box_h / 2) - (bd_h / 2);
00192 
00193   int col1_x = box_x;
00194   int col2_x = col1_x+200;
00195   int col3_x = col2_x+130;
00196 
00197   int row1_y = box_y;
00198   int row2_y = row1_y+30;
00199   int row3_y = row2_y+20;
00200   int row4_y = row3_y+20;
00201 
00202   context.push_transform();
00203   context.set_alpha(0.5);
00204   context.draw_surface(backdrop, Vector(bd_x, bd_y), LAYER_HUD);
00205   context.pop_transform();
00206 
00207   context.draw_text(Resources::normal_font, _("You"), Vector(col2_x, row1_y), ALIGN_LEFT, LAYER_HUD, Statistics::header_color);
00208   if (best_stats)
00209     context.draw_text(Resources::normal_font, _("Best"), Vector(col3_x, row1_y), ALIGN_LEFT, LAYER_HUD, Statistics::header_color);
00210 
00211   context.draw_text(Resources::normal_font, _("Coins"), Vector(col2_x-16, row3_y), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
00212   context.draw_text(Resources::normal_font, coins_to_string(coins, total_coins), Vector(col2_x, row3_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
00213   if (best_stats) {
00214     int coins_best = (best_stats->coins > coins) ? best_stats->coins : coins;
00215     int total_coins_best = (best_stats->total_coins > total_coins) ? best_stats->total_coins : total_coins;
00216     context.draw_text(Resources::normal_font, coins_to_string(coins_best, total_coins_best), Vector(col3_x, row3_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
00217   }
00218 
00219   context.draw_text(Resources::normal_font, _("Secrets"), Vector(col2_x-16, row4_y), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
00220   context.draw_text(Resources::normal_font, secrets_to_string(secrets, total_secrets), Vector(col2_x, row4_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
00221   if (best_stats) {
00222     int secrets_best = (best_stats->secrets > secrets) ? best_stats->secrets : secrets;
00223     int total_secrets_best = (best_stats->total_secrets > total_secrets) ? best_stats->total_secrets : total_secrets;
00224     context.draw_text(Resources::normal_font, secrets_to_string(secrets_best, total_secrets_best), Vector(col3_x, row4_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
00225   }
00226 
00227   context.draw_text(Resources::normal_font, _("Time"), Vector(col2_x-16, row2_y), ALIGN_RIGHT, LAYER_HUD, Statistics::header_color);
00228   context.draw_text(Resources::normal_font, time_to_string(time), Vector(col2_x, row2_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
00229   if (best_stats) {
00230     float time_best = (best_stats->time < time) ? best_stats->time : time;
00231     context.draw_text(Resources::normal_font, time_to_string(time_best), Vector(col3_x, row2_y), ALIGN_LEFT, LAYER_HUD, Statistics::text_color);
00232   }
00233 }

void Statistics::zero (  ) 

Set stats to zero.

Definition at line 236 of file statistics.cpp.

References reset(), total_badguys, total_coins, and total_secrets.

Referenced by worldmap::WorldMap::calculate_total_stats().

00237 {
00238   reset();
00239   total_coins = 0;
00240   total_badguys = 0;
00241   total_secrets = 0;
00242 }

void Statistics::reset (  ) 

Set stats (but not totals) to zero.

Definition at line 245 of file statistics.cpp.

References badguys, coins, secrets, and time.

Referenced by worldmap::WorldMap::WorldMap(), and zero().

00246 {
00247   coins = 0;
00248   badguys = 0;
00249   time = 0;
00250   secrets = 0;
00251 }

void Statistics::merge ( const Statistics stats  ) 

Given another Statistics object finds the best of each one.

Definition at line 254 of file statistics.cpp.

References badguys, coins, secrets, time, total_badguys, total_coins, total_secrets, and valid.

Referenced by worldmap::WorldMap::finished_level().

00255 {
00256   if (!s2.valid) return;
00257   coins = std::max(coins, s2.coins);
00258   total_coins = s2.total_coins;
00259   badguys = std::max(badguys, s2.badguys);
00260   total_badguys = s2.total_badguys;
00261   time = std::min(time, s2.time);
00262   secrets = std::max(secrets, s2.secrets);
00263   total_secrets = s2.total_secrets;
00264 }

void Statistics::operator+= ( const Statistics o  ) 

Add two Statistics objects.

Definition at line 267 of file statistics.cpp.

References badguys, coins, nv_badguys, nv_coins, nv_secrets, nv_time, secrets, time, total_badguys, total_coins, total_secrets, and valid.

00268 {
00269   if (!s2.valid) return;
00270   if (s2.coins != nv_coins) coins += s2.coins;
00271   if (s2.total_coins != nv_coins) total_coins += s2.total_coins;
00272   if (s2.badguys != nv_badguys) badguys += s2.badguys;
00273   if (s2.total_badguys != nv_badguys) total_badguys += s2.total_badguys;
00274   if (s2.time != nv_time) time += s2.time;
00275   if (s2.secrets != nv_secrets) secrets += s2.secrets;
00276   if (s2.total_secrets != nv_secrets) total_secrets += s2.total_secrets;
00277 }

void Statistics::declare_invalid (  ) 

marks statistics as invalid for their entire lifetime (e.g.

after cheating). Invalid statistics will not be merged or drawn.

Definition at line 280 of file statistics.cpp.

References valid.

00281 {
00282   valid = false;
00283 }

std::string Statistics::coins_to_string ( int  coins,
int  total_coins 
) [static]

Definition at line 286 of file statistics.cpp.

Referenced by LevelIntro::draw(), draw_endseq_panel(), and draw_worldmap_info().

00286                                                       {
00287   std::ostringstream os;
00288   os << std::min(coins, 999) << "/" << std::min(total_coins, 999);
00289   return os.str();
00290 }

std::string Statistics::frags_to_string ( int  badguys,
int  total_badguys 
) [static]

Definition at line 293 of file statistics.cpp.

Referenced by draw_worldmap_info().

00293                                                           {
00294   std::ostringstream os;
00295   os << std::min(badguys, 999) << "/" << std::min(total_badguys, 999);
00296   return os.str();
00297 }

std::string Statistics::time_to_string ( float  time  )  [static]

Definition at line 300 of file statistics.cpp.

Referenced by LevelIntro::draw(), draw_endseq_panel(), and draw_worldmap_info().

00300                                      {
00301   int time_csecs = std::min(static_cast<int>(time * 100), 99 * 6000 + 9999);
00302   int mins = (time_csecs / 6000);
00303   int secs = (time_csecs % 6000) / 100;
00304   int cscs = (time_csecs % 6000) % 100;
00305 
00306   std::ostringstream os;
00307   os << std::setw(2) << std::setfill('0') << mins << ":" << std::setw(2) << std::setfill('0') << secs << "." << std::setw(2) << std::setfill('0') << cscs;
00308   return os.str();
00309 }

std::string Statistics::secrets_to_string ( int  secrets,
int  total_secrets 
) [static]

Definition at line 312 of file statistics.cpp.

Referenced by LevelIntro::draw(), draw_endseq_panel(), and draw_worldmap_info().

00312                                                             {
00313   std::ostringstream os;
00314   os << std::min(secrets, 999) << "/" << std::min(total_secrets, 999);
00315   return os.str();
00316 }


Member Data Documentation

Color Statistics::header_color [static, private]

Definition at line 36 of file statistics.hpp.

Referenced by draw_endseq_panel(), and draw_worldmap_info().

Color Statistics::text_color [static, private]

Definition at line 37 of file statistics.hpp.

Referenced by draw_endseq_panel().

int Statistics::coins

coins collected

Definition at line 39 of file statistics.hpp.

Referenced by Coin::collect(), OneUp::collision(), LevelIntro::draw(), draw_endseq_panel(), draw_worldmap_info(), merge(), operator+=(), reset(), serialize_to_squirrel(), BonusBlock::try_open(), and unserialize_from_squirrel().

int Statistics::total_coins

coins in level

Definition at line 40 of file statistics.hpp.

Referenced by LevelIntro::draw(), draw_endseq_panel(), draw_worldmap_info(), merge(), operator+=(), serialize_to_squirrel(), unserialize_from_squirrel(), and zero().

int Statistics::badguys

badguys actively killed

Definition at line 41 of file statistics.hpp.

Referenced by draw_worldmap_info(), merge(), operator+=(), reset(), BadGuy::run_dead_script(), serialize_to_squirrel(), and unserialize_from_squirrel().

int Statistics::total_badguys

(vincible) badguys in level

Definition at line 42 of file statistics.hpp.

Referenced by draw_worldmap_info(), merge(), operator+=(), serialize_to_squirrel(), unserialize_from_squirrel(), and zero().

float Statistics::time

seconds needed

Definition at line 43 of file statistics.hpp.

Referenced by LevelIntro::draw(), draw_endseq_panel(), draw_worldmap_info(), merge(), operator+=(), reset(), serialize_to_squirrel(), and unserialize_from_squirrel().

int Statistics::secrets

secret areas found

Definition at line 44 of file statistics.hpp.

Referenced by LevelIntro::draw(), draw_endseq_panel(), draw_worldmap_info(), SecretAreaTrigger::event(), merge(), operator+=(), reset(), serialize_to_squirrel(), and unserialize_from_squirrel().

int Statistics::total_secrets

secret areas in level

Definition at line 45 of file statistics.hpp.

Referenced by LevelIntro::draw(), draw_endseq_panel(), draw_worldmap_info(), merge(), operator+=(), serialize_to_squirrel(), unserialize_from_squirrel(), and zero().

bool Statistics::valid [private]

stores whether these statistics can be trusted

Definition at line 82 of file statistics.hpp.

Referenced by declare_invalid(), draw_endseq_panel(), draw_worldmap_info(), merge(), and operator+=().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:36 2014 for SuperTux by  doxygen 1.5.1