<MatzeB> -cleanup in resource management functions
[supertux.git] / src / worldmap.cpp
index dbcc71a..415e8a4 100644 (file)
@@ -85,7 +85,7 @@ string_to_direction(const std::string& directory)
 
 TileManager::TileManager()
 {
-  std::string stwt_filename = datadir +  "images/worldmap/antarctica.stwt";
+  std::string stwt_filename = datadir +  "/images/worldmap/antarctica.stwt";
   lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
  
   if (!root_obj)
@@ -327,7 +327,7 @@ WorldMap::~WorldMap()
 void
 WorldMap::load_map()
 {
-  std::string filename = datadir +  "levels/default/worldmap.stwm";
+  std::string filename = datadir +  "/levels/default/worldmap.stwm";
   
   lisp_object_t* root_obj = lisp_read_from_file(filename);
   if (!root_obj)
@@ -404,10 +404,10 @@ void WorldMap::get_level_title(Levels::pointer level)
 
   FILE * fi;
   lisp_object_t* root_obj = 0;
-  fi = fopen((datadir +  "levels/" + level->name).c_str(), "r");
+  fi = fopen((datadir +  "/levels/" + level->name).c_str(), "r");
   if (fi == NULL)
   {
-    perror((datadir +  "levels/" + level->name).c_str());
+    perror((datadir +  "/levels/" + level->name).c_str());
     return;
   }
 
@@ -590,13 +590,49 @@ WorldMap::update()
               level->y == tux->get_tile_pos().y)
             {
               std::cout << "Enter the current level: " << level->name << std::endl;;
-              GameSession session(datadir +  "levels/" + level->name,
+              GameSession session(datadir +  "/levels/" + level->name,
                                   1, ST_GL_LOAD_LEVEL_FILE);
 
               switch (session.run())
                 {
                 case GameSession::LEVEL_FINISHED:
-                  level->solved = true;
+                  {
+                    bool old_level_state = level->solved;
+                    level->solved = true;
+
+                    if (session.get_world()->get_tux()->got_coffee)
+                      player_status.bonus = PlayerStatus::FLOWER_BONUS;
+                    else if (session.get_world()->get_tux()->size == BIG)
+                      player_status.bonus = PlayerStatus::GROWUP_BONUS;
+                    else
+                      player_status.bonus = PlayerStatus::NO_BONUS;
+
+                    if (old_level_state != level->solved)
+                      { // Try to detect the next direction to which we should walk
+                        // FIXME: Mostly a hack
+                        Direction dir = NONE;
+                    
+                        Tile* tile = at(tux->get_tile_pos());
+
+                        if (tile->north && tux->back_direction != NORTH)
+                          dir = NORTH;
+                        else if (tile->south && tux->back_direction != SOUTH)
+                          dir = SOUTH;
+                        else if (tile->east && tux->back_direction != EAST)
+                          dir = EAST;
+                        else if (tile->west && tux->back_direction != WEST)
+                          dir = WEST;
+
+                        if (dir != NONE)
+                          {
+                            tux->set_direction(dir);
+                            tux->update(0.33f);
+                          }
+
+                        std::cout << "Walk to dir: " << dir << std::endl;
+                      }
+                  }
+
                   break;
                 case GameSession::LEVEL_ABORT:
                   // Reseting the player_status might be a worthy
@@ -609,6 +645,7 @@ WorldMap::update()
                   break;
                 case GameSession::GAME_OVER:
                   quit = true;
+                  player_status.bonus = PlayerStatus::NO_BONUS;
                   break;
                 case GameSession::NONE:
                   // Should never be reached 
@@ -809,8 +846,9 @@ WorldMap::savegame(const std::string& filename)
       << "  (lives   " << player_status.lives << ")\n"
       << "  (score   " << player_status.score << ")\n"
       << "  (distros " << player_status.distros << ")\n"
-      << "  (tux     (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")"
-      << " (back \"" << direction_to_string(tux->back_direction) << "\"))\n"
+      << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
+      << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
+      << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
       << "  (levels\n";
   
   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
@@ -856,12 +894,15 @@ WorldMap::loadgame(const std::string& filename)
     {
       Point p;
       std::string back_str = "none";
+      std::string bonus_str = "none";
 
       LispReader tux_reader(tux_cur);
       tux_reader.read_int("x", &p.x);
       tux_reader.read_int("y", &p.y);
       tux_reader.read_string("back", &back_str);
+      tux_reader.read_string("bonus", &bonus_str);
       
+      player_status.bonus = string_to_bonus(bonus_str);
       tux->back_direction = string_to_direction(back_str);      
       tux->set_tile_pos(p);
     }