Fixed the not saving bug.
[supertux.git] / src / worldmap.cpp
index 8c11af9..68ad20b 100644 (file)
@@ -247,21 +247,28 @@ Tux::stop()
 {
   offset = 0;
   direction = D_NONE;
+  input_direction = D_NONE;
   moving = false;
 }
 
 void
+Tux::set_direction(Direction dir)
+{
+input_direction = dir;
+}
+
+void
 Tux::action(float delta)
 {
   if (!moving)
     {
       if (input_direction != D_NONE)
         { 
-          WorldMap::Level* level = worldmap->at_level();
+          WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
 
           // We got a new direction, so lets start walking when possible
           Vector next_tile;
-          if ((!level || level->solved)
+          if ((!special_tile || special_tile->solved || special_tile->level_name.empty())
               && worldmap->path_ok(input_direction, tile_pos, &next_tile))
             {
               tile_pos = next_tile;
@@ -271,7 +278,6 @@ Tux::action(float delta)
             }
           else if (input_direction == back_direction)
             {
-              std::cout << "Back triggered" << std::endl;
               moving = true;
               direction = input_direction;
               tile_pos = worldmap->get_next_tile(tile_pos, direction);
@@ -288,49 +294,71 @@ Tux::action(float delta)
         { // We reached the next tile, so we check what to do now
           offset -= 32;
 
-          if (worldmap->at(tile_pos)->stop || worldmap->at_level())
+          if (worldmap->at(tile_pos)->stop || worldmap->at_special_tile())
             {
               stop();
             }
           else
             {
-              if (worldmap->at(tile_pos)->auto_walk)
+              if (worldmap->at(tile_pos)->auto_walk || direction != input_direction)
                 { // Turn to a new direction
                   Tile* tile = worldmap->at(tile_pos);
-                  Direction dir = D_NONE;
-                  
-                  if (tile->north && back_direction != D_NORTH)
-                    dir = D_NORTH;
-                  else if (tile->south && back_direction != D_SOUTH)
-                    dir = D_SOUTH;
-                  else if (tile->east && back_direction != D_EAST)
-                    dir = D_EAST;
-                  else if (tile->west && back_direction != D_WEST)
-                    dir = D_WEST;
-
-                  if (dir != D_NONE)
-                    {
-                      direction = dir;
+
+                  if(direction != input_direction && 
+                     ((tile->north && input_direction == D_NORTH) ||
+                     (tile->south && input_direction == D_SOUTH) ||
+                     (tile->east && input_direction == D_EAST) ||
+                     (tile->west && input_direction == D_WEST)))
+                    {  // player has changed direction during auto-movement
+                    direction = input_direction;
+                    back_direction = reverse_dir(direction);
+                    }
+                  else if(direction != input_direction)
+                    {  // player has changed to impossible tile
                       back_direction = reverse_dir(direction);
+                      stop();
                     }
                   else
                     {
+                    Direction dir = D_NONE;
+                  
+                    if (tile->north && back_direction != D_NORTH)
+                      dir = D_NORTH;
+                    else if (tile->south && back_direction != D_SOUTH)
+                      dir = D_SOUTH;
+                    else if (tile->east && back_direction != D_EAST)
+                      dir = D_EAST;
+                    else if (tile->west && back_direction != D_WEST)
+                      dir = D_WEST;
+
+                    if (dir != D_NONE)
+                      {
+                      direction = dir;
+                      input_direction = direction;
+                      back_direction = reverse_dir(direction);
+                      }
+                    else
+                      {
                       // Should never be reached if tiledata is good
                       stop();
                       return;
+                      }
                     }
-                }
+                  }
 
               // Walk automatically to the next tile
-              Vector next_tile;
-              if (worldmap->path_ok(direction, tile_pos, &next_tile))
+              if(direction != D_NONE)
                 {
+                Vector next_tile;
+                if (worldmap->path_ok(direction, tile_pos, &next_tile))
+                  {
                   tile_pos = next_tile;
-                }
-              else
-                {
+                  }
+                else
+                  {
                   puts("Tilemap data is buggy");
                   stop();
+                  }
                 }
             }
         }
@@ -364,7 +392,6 @@ WorldMap::WorldMap()
   leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", true);
   leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", true);
 
-  input_direction = D_NONE;
   enter_level = false;
 
   name = "<no title>";
@@ -411,7 +438,8 @@ WorldMap::load_map()
              reader.read_int("start_pos_x", start_x);
              reader.read_int("start_pos_y", start_y);
             }
-          else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
+          else if (strcmp(lisp_symbol(lisp_car(element)), "special-tiles") == 0 ||
+                   strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
             {
               lisp_object_t* cur = lisp_cdr(element);
               
@@ -421,54 +449,56 @@ WorldMap::load_map()
 
                   if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
                     {
-                      Level level;
+                      SpecialTile special_tile;
                       LispReader reader(lisp_cdr(element));
-                      level.solved = false;
+                      special_tile.solved = false;
                       
-                      level.north = true;
-                      level.east  = true;
-                      level.south = true;
-                      level.west  = true;
-
-                      reader.read_string("extro-filename", level.extro_filename);
-                      reader.read_string("map-message", level.display_map_message);
-                      reader.read_string("next-world", level.next_worldmap);
-                      reader.read_string("level", level.name, true);
-                      reader.read_int("x", level.x);
-                      reader.read_int("y", level.y);
-                      level.swap_x = level.swap_y = -1;
-                      reader.read_int("swap-x", level.swap_x);
-                      reader.read_int("swap-y", level.swap_y);
-                      level.vertical_flip = false;
-                      reader.read_bool("flip-level", level.vertical_flip);
-                      level.quit_worldmap = false;
-                      reader.read_bool("exit-game", level.quit_worldmap);
-
-                      levels.push_back(level);
+                      special_tile.north = true;
+                      special_tile.east  = true;
+                      special_tile.south = true;
+                      special_tile.west  = true;
+
+                      reader.read_string("extro-filename", special_tile.extro_filename);
+                      reader.read_string("map-message", special_tile.display_map_message);
+                      reader.read_string("next-world", special_tile.next_worldmap);
+                      reader.read_string("level", special_tile.level_name, true);
+                      reader.read_int("x", special_tile.x);
+                      reader.read_int("y", special_tile.y);
+                      special_tile.auto_path = true;
+                      reader.read_bool("auto-path", special_tile.auto_path);
+                      special_tile.swap_x = special_tile.swap_y = -1;
+                      reader.read_int("swap-x", special_tile.swap_x);
+                      reader.read_int("swap-y", special_tile.swap_y);
+                      special_tile.vertical_flip = false;
+                      reader.read_bool("flip-special_tile", special_tile.vertical_flip);
+                      special_tile.quit_worldmap = false;
+                      reader.read_bool("exit-game", special_tile.quit_worldmap);
+
+                      special_tiles.push_back(special_tile);
                     }
 
                   /* Kept for backward compability */
                   else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
                     {
-                      Level level;
+                      SpecialTile special_tile;
                       LispReader reader(lisp_cdr(element));
-                      level.solved = false;
+                      special_tile.solved = false;
                       
-                      level.north = true;
-                      level.east  = true;
-                      level.south = true;
-                      level.west  = true;
-
-                      reader.read_string("extro-filename", level.extro_filename);
-                      if(!level.extro_filename.empty())
-                        level.quit_worldmap = true;
-                      reader.read_string("name", level.name, true);
-                      reader.read_int("x", level.x);
-                      reader.read_int("y", level.y);
-                      level.vertical_flip = false;
-                      level.swap_x = level.swap_y = -1;
-
-                      levels.push_back(level);
+                      special_tile.north = true;
+                      special_tile.east  = true;
+                      special_tile.south = true;
+                      special_tile.west  = true;
+
+                      reader.read_string("extro-filename", special_tile.extro_filename);
+                      if(!special_tile.extro_filename.empty())
+                        special_tile.quit_worldmap = true;
+                      reader.read_string("name", special_tile.level_name, true);
+                      reader.read_int("x", special_tile.x);
+                      reader.read_int("y", special_tile.y);
+                      special_tile.vertical_flip = false;
+                      special_tile.swap_x = special_tile.swap_y = -1;
+
+                      special_tiles.push_back(special_tile);
                     }
                   
                   cur = lisp_cdr(cur);      
@@ -487,19 +517,19 @@ WorldMap::load_map()
     tux = new Tux(this);
 }
 
-void WorldMap::get_level_title(Level& level)
+void WorldMap::get_level_title(SpecialTile& special_tile)
 {
-  /** get level's title */
-  level.title = "<no title>";
+  /** get special_tile's title */
+  special_tile.title = "<no title>";
 
-  LispReader* reader = LispReader::load(datadir + "/levels/" + level.name, "supertux-level");
+  LispReader* reader = LispReader::load(datadir + "/levels/" + special_tile.level_name, "supertux-level");
   if(!reader)
     {
-    std::cerr << "Error: Could not open level file. Ignoring...\n";
+    std::cerr << "Error: Could not open special_tile file. Ignoring...\n";
     return;
     }
 
-  reader->read_string("name", level.title, true);
+  reader->read_string("name", special_tile.title, true);
   delete reader;
 }
 
@@ -508,7 +538,10 @@ WorldMap::on_escape_press()
 {
   // Show or hide the menu
   if(!Menu::current())
+    {
     Menu::set_current(worldmap_menu); 
+    tux->set_direction(D_NONE);  // stop tux movement when menu is called
+    }
   else
     Menu::set_current(0); 
 }
@@ -517,7 +550,6 @@ void
 WorldMap::get_input()
 {
   enter_level = false;
-  input_direction = D_NONE;
    
   SDL_Event event;
   while (SDL_PollEvent(&event))
@@ -544,6 +576,20 @@ WorldMap::get_input()
                 case SDLK_RETURN:
                   enter_level = true;
                   break;
+
+                case SDLK_LEFT:
+                  tux->set_direction(D_WEST);
+                  break;
+                case SDLK_RIGHT:
+                  tux->set_direction(D_EAST);
+                  break;
+                case SDLK_UP:
+                  tux->set_direction(D_NORTH);
+                  break;
+                case SDLK_DOWN:
+                  tux->set_direction(D_SOUTH);
+                  break;
+
                 default:
                   break;
                 }
@@ -553,16 +599,16 @@ WorldMap::get_input()
               if (event.jaxis.axis == joystick_keymap.x_axis)
                 {
                   if (event.jaxis.value < -joystick_keymap.dead_zone)
-                    input_direction = D_WEST;
+                    tux->set_direction(D_WEST);
                   else if (event.jaxis.value > joystick_keymap.dead_zone)
-                    input_direction = D_EAST;
+                    tux->set_direction(D_EAST);
                 }
               else if (event.jaxis.axis == joystick_keymap.y_axis)
                 {
                   if (event.jaxis.value > joystick_keymap.dead_zone)
-                    input_direction = D_SOUTH;
+                    tux->set_direction(D_SOUTH);
                   else if (event.jaxis.value < -joystick_keymap.dead_zone)
-                    input_direction = D_NORTH;
+                    tux->set_direction(D_NORTH);
                 }
               break;
 
@@ -578,20 +624,6 @@ WorldMap::get_input()
             }
         }
     }
-
-  if (!Menu::current())
-    {
-      Uint8 *keystate = SDL_GetKeyState(NULL);
-  
-      if (keystate[SDLK_LEFT])
-        input_direction = D_WEST;
-      else if (keystate[SDLK_RIGHT])
-        input_direction = D_EAST;
-      else if (keystate[SDLK_UP])
-        input_direction = D_NORTH;
-      else if (keystate[SDLK_DOWN])
-        input_direction = D_SOUTH;
-    }
 }
 
 Vector
@@ -656,8 +688,8 @@ WorldMap::update(float delta)
   if (enter_level && !tux->is_moving())
     {
       bool level_finished = true;
-      Level* level = at_level();
-      if (!level)
+      SpecialTile* special_tile = at_special_tile();
+      if (!special_tile)
         {
         std::cout << "Nothing to enter at: "
           << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
@@ -665,27 +697,27 @@ WorldMap::update(float delta)
         }
 
 
-      if(!level->name.empty())
+      if(!special_tile->level_name.empty())
         {
-          if (level->x == tux->get_tile_pos().x && 
-              level->y == tux->get_tile_pos().y)
+          if (special_tile->x == tux->get_tile_pos().x && 
+              special_tile->y == tux->get_tile_pos().y)
             {
               PlayerStatus old_player_status = player_status;
 
-              std::cout << "Enter the current level: " << level->name << std::endl;
-              // do a shriking fade to the level
-              shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
+              std::cout << "Enter the current level: " << special_tile->level_name << std::endl;
+              // do a shriking fade to the special_tile
+              shrink_fade(Vector((special_tile->x*32 + 16 + offset.x),(special_tile->y*32 + 16
                       + offset.y)), 500);
-              GameSession session(datadir +  "/levels/" + level->name,
-                                  ST_GL_LOAD_LEVEL_FILE, level->vertical_flip);
+              GameSession session(datadir +  "/levels/" + special_tile->level_name,
+                                  ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip);
 
               switch (session.run())
                 {
                 case GameSession::ES_LEVEL_FINISHED:
                   {
                     level_finished = true;
-                    bool old_level_state = level->solved;
-                    level->solved = true;
+                    bool old_level_state = special_tile->solved;
+                    special_tile->solved = true;
 
                     if (session.get_current_sector()->player->got_power !=
                           session.get_current_sector()->player->NONE_POWER)
@@ -695,7 +727,7 @@ WorldMap::update(float delta)
                     else
                       player_status.bonus = PlayerStatus::NO_BONUS;
 
-                    if (old_level_state != level->solved)
+                    if (old_level_state != special_tile->solved && special_tile->auto_path)
                       { // Try to detect the next direction to which we should walk
                         // FIXME: Mostly a hack
                         Direction dir = D_NONE;
@@ -724,7 +756,7 @@ WorldMap::update(float delta)
                   break;
                 case GameSession::ES_LEVEL_ABORT:
                   level_finished = false;
-                  /* In case the player's abort the level, keep it using the old
+                  /* In case the player's abort the special_tile, keep it using the old
                       status. But the minimum lives and no bonus. */
                   player_status.score = old_player_status.score;
                   player_status.distros = old_player_status.distros;
@@ -738,7 +770,7 @@ WorldMap::update(float delta)
                   /* draw an end screen */
                   /* in the future, this should make a dialog a la SuperMario, asking
                   if the player wants to restart the world map with no score and from
-                  level 1 */
+                  special_tile 1 */
                   char str[80];
 
                   DrawingContext context;
@@ -778,32 +810,32 @@ WorldMap::update(float delta)
             }
         }
       /* The porpose of the next checking is that if the player lost
-         the level (in case there is one), don't show anything */
+         the special_tile (in case there is one), don't show anything */
       if(level_finished)
         {
-        if (!level->extro_filename.empty())
+        if (!special_tile->extro_filename.empty())
           {
           // Display a text file
-          display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE);
+          display_text_file(special_tile->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
           }
-        if (level->swap_x != -1 && level->swap_y != -1)
+        if (special_tile->swap_x != -1 && special_tile->swap_y != -1)
           {
           // TODO: add an effect, like a camera scrolling, or at least, a fading
-          tux->set_tile_pos(Vector(level->swap_x, level->swap_y));
+          tux->set_tile_pos(Vector(special_tile->swap_x, special_tile->swap_y));
           }
-        if (!level->next_worldmap.empty())
+        if (!special_tile->next_worldmap.empty())
           {
           // Load given worldmap
-          loadmap(level->next_worldmap);
+          loadmap(special_tile->next_worldmap);
           }
-        if (level->quit_worldmap)
+        if (special_tile->quit_worldmap)
           quit = true;
         }
     }
   else
     {
       tux->action(delta);
-      tux->set_direction(input_direction);
+//      tux->set_direction(input_direction);
     }
   
   Menu* menu = Menu::current();
@@ -842,10 +874,10 @@ WorldMap::at(Vector p)
   return tile_manager->get(tilemap[width * y + x]);
 }
 
-WorldMap::Level*
-WorldMap::at_level()
+WorldMap::SpecialTile*
+WorldMap::at_special_tile()
 {
-  for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
+  for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
       if (i->x == tux->get_tile_pos().x && 
           i->y == tux->get_tile_pos().y)
@@ -867,9 +899,9 @@ WorldMap::draw(DrawingContext& context, const Vector& offset)
             Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
       }
   
-  for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
+  for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
-      if(i->name.empty())
+      if(i->level_name.empty())
         continue;
 
       if (i->solved)
@@ -921,12 +953,12 @@ WorldMap::draw_status(DrawingContext& context)
 
   if (!tux->is_moving())
     {
-      for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
+      for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
         {
           if (i->x == tux->get_tile_pos().x && 
               i->y == tux->get_tile_pos().y)
             {
-              if(!i->name.empty())
+              if(!i->level_name.empty())
                 {
                 if(i->title == "")
                   get_level_title(*i);
@@ -960,7 +992,7 @@ WorldMap::display()
   unsigned int last_update_time;
   unsigned int update_time;
 
-  last_update_time = update_time = st_get_ticks();
+  last_update_time = update_time = Ticks::get();
 
   DrawingContext context;
   while(!quit)
@@ -973,7 +1005,7 @@ WorldMap::display()
         delta = .3f;
       
       last_update_time = update_time;
-      update_time      = st_get_ticks();
+      update_time      = Ticks::get();
 
       Vector tux_pos = tux->get_pos();
       if (1)
@@ -1007,14 +1039,14 @@ WorldMap::display()
 void
 WorldMap::savegame(const std::string& filename)
 {
-  if(filename != "")
+  if(filename == "")
     return;
 
   std::cout << "savegame: " << filename << std::endl;
   std::ofstream out(filename.c_str());
 
   int nb_solved_levels = 0;
-  for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
+  for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
       if (i->solved)
         ++nb_solved_levels;
@@ -1022,7 +1054,7 @@ WorldMap::savegame(const std::string& filename)
 
   out << "(supertux-savegame\n"
       << "  (version 1)\n"
-      << "  (title  \"" << name << " - " << nb_solved_levels << "/" << levels.size() << "\")\n"
+      << "  (title  \"" << name << " - " << nb_solved_levels << "/" << special_tiles.size() << "\")\n"
       << "  (map    \"" << map_filename << "\")\n"
       << "  (lives   " << player_status.lives << ")\n"
       << "  (score   " << player_status.score << ")\n"
@@ -1032,11 +1064,11 @@ WorldMap::savegame(const std::string& filename)
       << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
       << "  (levels\n";
   
-  for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
+  for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
-      if (i->solved)
+      if (i->solved && !i->level_name.empty())
         {
-          out << "     (level (name \"" << i->name << "\")\n"
+          out << "     (level (name \"" << i->level_name << "\")\n"
               << "            (solved #t))\n";
         }
     }  
@@ -1077,7 +1109,7 @@ WorldMap::loadgame(const std::string& filename)
   cur = lisp_cdr(cur);
   LispReader reader(cur);
 
-  /* Get the Map filename and then load it before setting level settings */
+  /* Get the Map filename and then load it before setting special_tile settings */
   reader.read_string("map", map_filename);
   load_map(); 
 
@@ -1123,9 +1155,9 @@ WorldMap::loadgame(const std::string& filename)
               level_reader.read_string("name", name, true);
               level_reader.read_bool("solved", solved);
 
-              for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
+              for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
                 {
-                  if (name == i->name)
+                  if (name == i->level_name)
                     i->solved = solved;
                 }
             }