pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r4157 - in branches/pingus-hanusz/src: . screen


From: grumbel
Subject: [Pingus-CVS] r4157 - in branches/pingus-hanusz/src: . screen
Date: Wed, 18 May 2011 14:18:55 +0200

Author: grumbel
Date: 2011-05-18 14:18:55 +0200 (Wed, 18 May 2011)
New Revision: 4157

Modified:
   branches/pingus-hanusz/src/game_session.cpp
   branches/pingus-hanusz/src/goal_manager.cpp
   branches/pingus-hanusz/src/goal_manager.hpp
   branches/pingus-hanusz/src/level_menu.cpp
   branches/pingus-hanusz/src/level_menu.hpp
   branches/pingus-hanusz/src/result.hpp
   branches/pingus-hanusz/src/screen/screen_manager.cpp
   branches/pingus-hanusz/src/screen/screen_manager.hpp
   branches/pingus-hanusz/src/server.cpp
   branches/pingus-hanusz/src/server.hpp
   branches/pingus-hanusz/src/statistics.cpp
   branches/pingus-hanusz/src/statistics.hpp
Log:
Moved the timit limit into the screen manager and let the game abort a level 
when limit is reached


Modified: branches/pingus-hanusz/src/game_session.cpp
===================================================================
--- branches/pingus-hanusz/src/game_session.cpp 2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/game_session.cpp 2011-05-18 12:18:55 UTC (rev 
4157)
@@ -116,6 +116,8 @@
       result.max_time  = server->get_plf().get_time();
       result.used_time = server->get_time();
 
+      result.aborted = server->was_aborted();
+
       { // Write the savegame
         Savegame savegame(result.plf.get_resname(),
                           (result.saved >= result.needed) ? Savegame::FINISHED 
: Savegame::ACCESSIBLE,

Modified: branches/pingus-hanusz/src/goal_manager.cpp
===================================================================
--- branches/pingus-hanusz/src/goal_manager.cpp 2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/goal_manager.cpp 2011-05-18 12:18:55 UTC (rev 
4157)
@@ -23,8 +23,8 @@
 #include "globals.hpp"
 #include "pingu_holder.hpp"
 #include "goal_manager.hpp"
+#include "screen/screen_manager.hpp"
 
-
 GoalManager::GoalManager(Server* s)
   : server(s), goal(GT_NONE), exit_time(0)
 {
@@ -34,48 +34,57 @@
 GoalManager::is_finished()
 {
   if (goal == GT_NONE)
-    {
-      return false;
-    }
+  {
+    return false;
+  }
+  else if (goal == GT_GLOBAL_TIME_LIMIT)
+  {
+    return true;
+  }
   else if (exit_time == 0)
-    {
-      // we are finished, now wait a few second so that everybody can
-      // see the particles, etc.
-      if (maintainer_mode)
-        std::cout << "XXXX goal reached: " << goal << std::endl;
-      exit_time = server->get_time() + 125;
-      return false;
-    }
+  {
+    // we are finished, now wait a few second so that everybody can
+    // see the particles, etc.
+    if (maintainer_mode)
+      std::cout << "XXXX goal reached: " << goal << std::endl;
+    exit_time = server->get_time() + 125;
+    return false;
+  }
   else
-    {
-      return (exit_time < server->get_time());
-    }
+  {
+    return (exit_time < server->get_time());
+  }
 }
 
 void
 GoalManager::update()
 {
   if (exit_time == 0)
+  {
+    World*       world  = server->get_world();
+    PinguHolder* pingus = world->get_pingus();
+    const PingusLevel& plf    = server->get_plf();
+
+    if (pingus->get_number_of_allowed() == pingus->get_number_of_released()
+        && pingus->get_number_of_alive() == 0)
     {
-      World*       world  = server->get_world();
-      PinguHolder* pingus = world->get_pingus();
-      const PingusLevel& plf    = server->get_plf();
+      goal = GT_NO_PINGUS_IN_WORLD;
+    }
+    else if (pingus->get_number_of_alive() == 0 && world->check_armageddon())
+    {
+      goal = GT_ARMAGEDDON;
+    }
+    else if (plf.get_time() != -1
+             && plf.get_time() <= server->get_time())
+    {
+      goal = GT_OUT_OF_TIME;
+    }
+  }
 
-      if (pingus->get_number_of_allowed() == pingus->get_number_of_released()
-          && pingus->get_number_of_alive() == 0)
-        {
-          goal = GT_NO_PINGUS_IN_WORLD;
-        }
-      else if (pingus->get_number_of_alive() == 0 && world->check_armageddon())
-        {
-          goal = GT_ARMAGEDDON;
-        }
-      else if (plf.get_time() != -1
-               && plf.get_time() <= server->get_time())
-        {
-          goal = GT_OUT_OF_TIME;
-        }
-    }
+  if (ScreenManager::instance()->time_limit_over())
+  {
+    goal = GT_GLOBAL_TIME_LIMIT;
+  }
 }
 
 void

Modified: branches/pingus-hanusz/src/goal_manager.hpp
===================================================================
--- branches/pingus-hanusz/src/goal_manager.hpp 2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/goal_manager.hpp 2011-05-18 12:18:55 UTC (rev 
4157)
@@ -30,11 +30,17 @@
 private:
   Server* server;
 
-  enum GoalType { GT_NONE,        // No goal is reached
-                  GT_OUT_OF_TIME, // if the timelimit has passed
-                  GT_NO_PINGUS_IN_WORLD, // if all pingus are released and 
exited/killed
-                  GT_ARMAGEDDON, // if armageddon as destroyed all pingus
-                  GT_GAME_ABORTED }; // if the user pressed Escape to exit the 
level };
+public:
+  enum GoalType { 
+    GT_NONE,        // No goal is reached
+    GT_OUT_OF_TIME, // if the timelimit has passed
+    GT_NO_PINGUS_IN_WORLD, // if all pingus are released and exited/killed
+    GT_ARMAGEDDON, // if armageddon as destroyed all pingus
+    GT_GAME_ABORTED, // if the user pressed Escape to exit the level
+    GT_GLOBAL_TIME_LIMIT
+  };
+
+private:
   GoalType goal;
 
   /** time at which is_finished() will return true */
@@ -49,6 +55,8 @@
   /** Abort the level */
   void set_abort_goal();
 
+  GoalType get_goal() const { return GT_GLOBAL_TIME_LIMIT; }
+
   /** Check for goal conditions and set finished accordingly */
   void update();
 

Modified: branches/pingus-hanusz/src/level_menu.cpp
===================================================================
--- branches/pingus-hanusz/src/level_menu.cpp   2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/level_menu.cpp   2011-05-18 12:18:55 UTC (rev 
4157)
@@ -261,7 +261,6 @@
   start_time(SDL_GetTicks()),
   x_pos((Display::get_width()  - 800)/2),
   y_pos((Display::get_height() - 600)/2),
-  m_time_limit(15),
   m_mode(kLevelsetSelector)
 {
   //background = Resource::load_sprite("core/menu/filedialog");
@@ -296,10 +295,7 @@
 
   if (m_mode == kLevelSelector)
   {
-    int total_time = m_time_limit;
-    int sec = total_time - ((SDL_GetTicks() - start_time) / 1000);
-
-    if (sec <= 0)
+    if (ScreenManager::instance()->time_limit_over())
     {
       // time limit reached
       gc.print_center(Fonts::chalk_large, gc.get_width()/2, gc.get_height()/2 
- 60,
@@ -308,17 +304,6 @@
       m_abort_button->show();
       level_selector->hide();
     }
-    else
-    {
-      if (maintainer_mode)
-      {
-        int min = sec / 60;
-        sec = sec % 60;
-
-        gc.print_center(Fonts::chalk_small, gc.get_width()/2, 
gc.get_height()/2 + 180, 
-                        (boost::format("%2d:%02d") % min % sec).str());
-      }
-    }
   }
   else if (m_mode == kLevelsetSelector)
   {
@@ -363,13 +348,16 @@
     
       Statistics::instance()->set_username(m_username_inputbox->get_string());
 
-      if (!StringUtil::from_string(m_time_inputbox->get_string(), 
m_time_limit))
+      int time_limit;
+      if (!StringUtil::from_string(m_time_inputbox->get_string(), time_limit))
       {
         m_time_inputbox->set_string("err");
       }
       else
       {
-        m_time_limit *= 60; // convert from min to sec
+        time_limit *= 60 * 1000; // convert from min to msec
+        
+        ScreenManager::instance()->set_time_limit(time_limit);
 
         level_selector->set_levelset(levelset);
         levelset_selector->hide();
@@ -388,6 +376,8 @@
   {
     m_mode = kLevelsetSelector;
 
+    ScreenManager::instance()->set_time_limit(-1);
+
     levelset_selector->show();
     level_selector->hide();   
     m_abort_button->show();

Modified: branches/pingus-hanusz/src/level_menu.hpp
===================================================================
--- branches/pingus-hanusz/src/level_menu.hpp   2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/level_menu.hpp   2011-05-18 12:18:55 UTC (rev 
4157)
@@ -46,8 +46,6 @@
   GUI::InputBox* m_username_inputbox;
   GUI::InputBox* m_time_inputbox;
 
-  int m_time_limit;
-
   enum { kLevelsetSelector, kLevelSelector} m_mode;
 
 public:

Modified: branches/pingus-hanusz/src/result.hpp
===================================================================
--- branches/pingus-hanusz/src/result.hpp       2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/result.hpp       2011-05-18 12:18:55 UTC (rev 
4157)
@@ -48,6 +48,8 @@
   /** Number of Pingus needed to save */
   int needed;
 
+  bool aborted;
+
   bool success() const {
     return (saved >= needed);
   }

Modified: branches/pingus-hanusz/src/screen/screen_manager.cpp
===================================================================
--- branches/pingus-hanusz/src/screen/screen_manager.cpp        2011-05-16 
13:24:23 UTC (rev 4156)
+++ branches/pingus-hanusz/src/screen/screen_manager.cpp        2011-05-18 
12:18:55 UTC (rev 4157)
@@ -19,6 +19,8 @@
 
 #include "SDL.h"
 #include <iostream>
+#include <boost/format.hpp>
+
 #include "../globals.hpp"
 #include "math/size.hpp"
 #include "pathname.hpp"
@@ -30,10 +32,13 @@
 #include "../display/drawing_context.hpp"
 #include "../input/controller.hpp"
 #include "../input/manager.hpp"
+#include "../fonts.hpp"
 
 ScreenManager* ScreenManager::instance_ = 0;
 
-ScreenManager::ScreenManager()
+ScreenManager::ScreenManager() :
+  m_time_limit_msec(-1),
+  m_time_passed(0)
 {
   display_gc = new DrawingContext();
 
@@ -84,6 +89,8 @@
 
       input_manager.update(time_delta);
 
+      m_time_passed += static_cast<int>(time_delta * 1000.0f);
+
       // Fill the delta with values
       GameDelta delta(time_delta, delta_manager.get_absolute(),  
                       input_controller->poll_events());
@@ -131,8 +138,10 @@
        {
          if (get_current_screen()->draw(*display_gc))
             {
+              draw_time_limit(*display_gc);
               display_gc->render(Display::get_screen(), Rect(Vector2i(0,0), 
Size(Display::get_width(),
-                                                                               
      Display::get_height())));
+                                                                               
  Display::get_height())));
+
               Display::flip_display ();
               display_gc->clear();
             }
@@ -141,6 +150,7 @@
        {
          //std::cout << "ScreenManager: fading screens" << std::endl;
          fade_over(last_screen, get_current_screen());
+          Display::flip_display();
        }
 
       // Stupid hack to make this thing take less CPU
@@ -152,6 +162,33 @@
   delete input_controller;
 }
 
+void
+ScreenManager::draw_time_limit(DrawingContext& gc)
+{
+  if (maintainer_mode)
+  {
+    if (m_time_limit_msec == -1)
+    {
+      gc.print_center(Fonts::chalk_large, gc.get_width()/2, gc.get_height()/2 
+ 250, 
+                      "no time limit");
+    }
+    else if (time_limit_over())
+    {
+      gc.print_center(Fonts::chalk_large, gc.get_width()/2, gc.get_height()/2 
+ 250, 
+                      "time limit over");
+    }
+    else
+    {
+      int sec = (m_time_limit_msec - m_time_passed) / 1000;
+      int min = sec / 60;
+      sec = sec % 60;
+
+      gc.print_center(Fonts::chalk_large, gc.get_width()/2, gc.get_height()/2 
+ 250, 
+                      (boost::format("%2d:%02d") % min % sec).str());
+    }
+  }
+}
+
 ScreenPtr&
 ScreenManager::get_current_screen()
 {
@@ -273,6 +310,7 @@
                                   Size(screen_width  - 2*border_x, 
screen_height - 2*border_y)));
 
       new_screen->draw(*display_gc);
+      draw_time_limit(*display_gc);
       display_gc->render(Display::get_screen(), Rect(Vector2i(0,0), 
Size(Display::get_width(),
                                                                          
Display::get_height())));
       display_gc->clear();
@@ -283,7 +321,6 @@
       //old_screen->update (delta);
       
       Display::pop_cliprect();
-      Display::flip_display ();
       display_gc->clear();
       
       progress = passed_time/1.0f;
@@ -320,4 +357,30 @@
   instance_ = 0;
 }
 
+void
+ScreenManager::set_time_limit(int msec)
+{
+  m_time_limit_msec = msec;
+  m_time_passed = 0;
+}
+
+int
+ScreenManager::get_time_limit() const
+{
+  return m_time_limit_msec;
+}
+
+bool
+ScreenManager::time_limit_over() const
+{
+  if (m_time_limit_msec == -1)
+  {
+    return false;
+  }
+  else
+  {
+    return m_time_limit_msec < m_time_passed;
+  }
+}
+
 /* EOF */

Modified: branches/pingus-hanusz/src/screen/screen_manager.hpp
===================================================================
--- branches/pingus-hanusz/src/screen/screen_manager.hpp        2011-05-16 
13:24:23 UTC (rev 4156)
+++ branches/pingus-hanusz/src/screen/screen_manager.hpp        2011-05-18 
12:18:55 UTC (rev 4157)
@@ -48,6 +48,9 @@
   enum { CA_NONE, CA_POP, CA_POP_ALL, CA_REPLACE, CA_CLEAR } cached_action;
   ScreenPtr replace_screen_arg;
 
+  int m_time_limit_msec;
+  int m_time_passed;
+
 protected:
   ScreenManager ();
 public:
@@ -80,6 +83,12 @@
   /** @return a pointer to the current Screen */
   Screen* get_screen();
 
+  void set_time_limit(int msec);
+  int get_time_limit() const;
+  bool time_limit_over() const;
+
+  void draw_time_limit(DrawingContext& gc);
+
 private:
   void real_clear();
 

Modified: branches/pingus-hanusz/src/server.cpp
===================================================================
--- branches/pingus-hanusz/src/server.cpp       2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/server.cpp       2011-05-18 12:18:55 UTC (rev 
4157)
@@ -104,5 +104,10 @@
   return get_world()->get_game_time()->get_ticks();
 }
 
+bool
+Server::was_aborted() const
+{
+  return goal_manager->get_goal() == GoalManager::GT_GLOBAL_TIME_LIMIT;
+}
 
 /* EOF */

Modified: branches/pingus-hanusz/src/server.hpp
===================================================================
--- branches/pingus-hanusz/src/server.hpp       2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/server.hpp       2011-05-18 12:18:55 UTC (rev 
4157)
@@ -69,6 +69,8 @@
   void send_armageddon_event();
   void send_pingu_action_event(Pingu* pingu, Actions::ActionName action);
 
+  bool was_aborted() const;
+
 private:
   Server (const Server&);
   Server& operator= (const Server&);

Modified: branches/pingus-hanusz/src/statistics.cpp
===================================================================
--- branches/pingus-hanusz/src/statistics.cpp   2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/statistics.cpp   2011-05-18 12:18:55 UTC (rev 
4157)
@@ -61,8 +61,36 @@
           << result.killed << ";"
           << result.used_time << ";"
           << actions_used << ";"
-          << (result.success()?"success":"failure") << std::endl;
+          << (result.aborted ? "aborted" : 
(result.success()?"success":"failure")) << std::endl;
   }
 }
 
+void
+Statistics::mark_session_end()
+{
+  std::ofstream m_out(m_filename.c_str(), std::ios::app);
+  if (!m_out)
+  {
+    throw std::runtime_error(m_filename + ": couldn't open file for writing");
+  }
+  else
+  {
+    m_out << "### session end: " <<  m_username << " ###\n" << std::endl;
+  }
+}
+
+void
+Statistics::mark_session_start()
+{
+  std::ofstream m_out(m_filename.c_str(), std::ios::app);
+  if (!m_out)
+  {
+    throw std::runtime_error(m_filename + ": couldn't open file for writing");
+  }
+  else
+  {
+    m_out << "### session start: " <<  m_username << " ###\n" << std::endl;
+  }
+}
+
 /* EOF */

Modified: branches/pingus-hanusz/src/statistics.hpp
===================================================================
--- branches/pingus-hanusz/src/statistics.hpp   2011-05-16 13:24:23 UTC (rev 
4156)
+++ branches/pingus-hanusz/src/statistics.hpp   2011-05-18 12:18:55 UTC (rev 
4157)
@@ -44,6 +44,9 @@
   void set_username(const std::string& username);
   void save_result(const Result& result, int actions_used);
 
+  void mark_session_start();
+  void mark_session_end();
+
 private:
   Statistics(const Statistics&);
   Statistics& operator=(const Statistics&);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]