Fix coverity #29357
[supertux.git] / src / supertux / statistics.cpp
index 0dd695d..94d6325 100644 (file)
@@ -39,15 +39,15 @@ float WMAP_INFO_RIGHT_X;
 float WMAP_INFO_TOP_Y1;
 float WMAP_INFO_TOP_Y2;
 
-Statistics::Statistics() : 
-  coins(nv_coins), 
-  total_coins(nv_coins), 
-  badguys(nv_badguys), 
-  total_badguys(nv_badguys), 
-  time(nv_time), 
-  secrets(nv_secrets), 
-  total_secrets(nv_secrets), 
-  valid(true) 
+Statistics::Statistics() :
+  coins(nv_coins),
+  total_coins(nv_coins),
+  badguys(nv_badguys),
+  total_badguys(nv_badguys),
+  time(nv_time),
+  secrets(nv_secrets),
+  total_secrets(nv_secrets),
+  valid(true)
 {
   WMAP_INFO_LEFT_X = SCREEN_WIDTH - 32 - 256;
   WMAP_INFO_RIGHT_X = WMAP_INFO_LEFT_X + 256;
@@ -74,7 +74,8 @@ Statistics::serialize_to_squirrel(HSQUIRRELVM vm)
   if (time != nv_time) scripting::store_float(vm, "time-needed", time);
   if (secrets != nv_secrets) scripting::store_int(vm, "secrets-found", secrets);
   if (total_secrets != nv_secrets) scripting::store_int(vm, "secrets-found-total", total_secrets);
-  sq_createslot(vm, -3);
+  if(SQ_FAILED(sq_createslot(vm, -3)))
+    throw scripting::SquirrelError(vm, "Couldn't create statistics table");
 }
 
 void
@@ -95,7 +96,7 @@ Statistics::unserialize_from_squirrel(HSQUIRRELVM vm)
 }
 
 void
-Statistics::draw_worldmap_info(DrawingContext& context)
+Statistics::draw_worldmap_info(DrawingContext& context, float target_time)
 {
   // skip draw if level was never played
   if (coins == nv_coins) return;
@@ -113,15 +114,15 @@ Statistics::draw_worldmap_info(DrawingContext& context)
     WMAP_INFO_TOP_Y1 = SCREEN_HEIGHT - 100;
     WMAP_INFO_TOP_Y2 = WMAP_INFO_TOP_Y1 + 16;
   }
-  
-  context.draw_text(Resources::small_font, std::string("- ") + _("Best Level Statistics") + " -", 
-                    Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1), 
+
+  context.draw_text(Resources::small_font, std::string("- ") + _("Best Level Statistics") + " -",
+                    Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, WMAP_INFO_TOP_Y1),
                     ALIGN_CENTER, LAYER_HUD,Statistics::header_color);
 
   std::string caption_buf;
   std::string stat_buf;
   float posy = WMAP_INFO_TOP_Y2;
-  for (int stat_no = 0; stat_no < 4; stat_no++) {
+  for (int stat_no = 0; stat_no < 5; stat_no++) {
     switch (stat_no)
     {
       case 0:
@@ -133,13 +134,22 @@ Statistics::draw_worldmap_info(DrawingContext& context)
         stat_buf = frags_to_string(badguys, total_badguys);
         break;
       case 2:
-        caption_buf = _("Min time needed:");
-        stat_buf = time_to_string(time);
-        break;
-      case 3:
         caption_buf = _("Max secrets found:");
         stat_buf = secrets_to_string(secrets, total_secrets);
         break;
+      case 3:
+        caption_buf = _("Best time completed:");
+        stat_buf = time_to_string(time);
+        break;
+      case 4:
+        if(target_time){ // display target time only if defined for level
+          caption_buf = _("Level target time:");
+          stat_buf = time_to_string(target_time);
+        } else {
+          caption_buf = "";
+          stat_buf = "";
+        }
+        break;
       default:
         log_debug << "Invalid stat requested to be drawn" << std::endl;
         break;
@@ -273,11 +283,12 @@ Statistics::operator+=(const Statistics& s2)
 }
 
 bool
-Statistics::completed(const Statistics& stats)
+Statistics::completed(const Statistics& stats, const float target_time)
 {
-  return (stats.coins == stats.total_coins && 
-      stats.badguys == stats.total_badguys && 
-      stats.secrets == stats.total_secrets);
+  return (stats.coins == stats.total_coins &&
+      stats.badguys == stats.total_badguys &&
+      stats.secrets == stats.total_secrets &&
+      ((!target_time) || (stats.time <= target_time)));
 }
 
 void
@@ -300,7 +311,7 @@ Statistics::frags_to_string(int badguys, int total_badguys) {
   return os.str();
 }
 
-std::string 
+std::string
 Statistics::time_to_string(float time) {
   int time_csecs = std::min(static_cast<int>(time * 100), 99 * 6000 + 9999);
   int mins = (time_csecs / 6000);