Changed the background feeding format for the new level format.
[supertux.git] / src / worldmap.cpp
index 37c4cd6..0258a66 100644 (file)
@@ -28,6 +28,7 @@
 #include "video/screen.h"
 #include "video/drawing_context.h"
 #include "utils/lispreader.h"
+#include "special/frame_rate.h"
 #include "gameloop.h"
 #include "app/setup.h"
 #include "sector.h"
@@ -37,6 +38,8 @@
 #include "app/gettext.h"
 #include "misc.h"
 
+#define DISPLAY_MAP_MESSAGE_TIME 2800
+
 Menu* worldmap_menu  = 0;
 
 namespace WorldMapNS {
@@ -247,10 +250,17 @@ 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)
@@ -261,7 +271,7 @@ Tux::action(float delta)
 
           // We got a new direction, so lets start walking when possible
           Vector next_tile;
-          if ((!special_tile || special_tile->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 +281,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 +297,79 @@ 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_special_tile())
+          WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
+          if(special_tile && special_tile->passive_message)
+            {
+            worldmap->passive_message = special_tile->display_map_message;
+            worldmap->passive_message_timer.start(DISPLAY_MAP_MESSAGE_TIME);
+            }
+
+          if (worldmap->at(tile_pos)->stop || (special_tile && 
+              !special_tile->passive_message))
             {
               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();
+                  }
                 }
             }
         }
@@ -360,11 +399,10 @@ WorldMap::WorldMap()
   start_x = 4;
   start_y = 5;
   
-  level_sprite = new Surface(datadir +  "/images/worldmap/levelmarker.png", true);
   leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", true);
   leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", true);
+  messagedot   = new Surface(datadir +  "/images/worldmap/messagedot.png", true);
 
-  input_direction = D_NONE;
   enter_level = false;
 
   name = "<no title>";
@@ -376,17 +414,17 @@ WorldMap::~WorldMap()
   delete tux;
   delete tile_manager;
 
-  delete level_sprite;
   delete leveldot_green;
   delete leveldot_red;
+  delete messagedot;
 }
 
 void
 WorldMap::load_map()
 {
-  lisp_object_t* root_obj = lisp_read_from_file(datadir + "/special_tiles/worldmap/" + map_filename);
+  lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
   if (!root_obj)
-    Termination::abort("Couldn't load file", datadir + "/special_tiles/worldmap/" + map_filename);
+    Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
 
   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
     {
@@ -411,7 +449,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)), "special_tiles") == 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);
               
@@ -431,6 +470,10 @@ WorldMap::load_map()
                       special_tile.west  = true;
 
                       reader.read_string("extro-filename", special_tile.extro_filename);
+                      reader.read_string("passive-message", special_tile.display_map_message);
+                      special_tile.passive_message = false;
+                      if(!special_tile.display_map_message.empty())
+                        special_tile.passive_message = true;
                       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);
@@ -494,10 +537,10 @@ void WorldMap::get_level_title(SpecialTile& special_tile)
   /** get special_tile's title */
   special_tile.title = "<no title>";
 
-  LispReader* reader = LispReader::load(datadir + "/special_tiles/" + special_tile.level_name, "supertux-special_tile");
+  LispReader* reader = LispReader::load(datadir + "/levels/" + special_tile.level_name, "supertux-level");
   if(!reader)
     {
-    std::cerr << "Error: Could not open special_tile file. Ignoring...\n";
+    std::cerr << "Error: Could not open level file. Ignoring...\n";
     return;
     }
 
@@ -510,7 +553,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); 
 }
@@ -519,7 +565,6 @@ void
 WorldMap::get_input()
 {
   enter_level = false;
-  input_direction = D_NONE;
    
   SDL_Event event;
   while (SDL_PollEvent(&event))
@@ -546,6 +591,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;
                 }
@@ -555,16 +614,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;
 
@@ -580,20 +639,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
@@ -674,11 +719,11 @@ WorldMap::update(float delta)
             {
               PlayerStatus old_player_status = player_status;
 
-              std::cout << "Enter the current special_tile: " << special_tile->level_name << std::endl;
+              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 +  "/special_tiles/" + special_tile->level_name,
+              GameSession session(datadir +  "/levels/" + special_tile->level_name,
                                   ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip);
 
               switch (session.run())
@@ -805,7 +850,7 @@ WorldMap::update(float delta)
   else
     {
       tux->action(delta);
-      tux->set_direction(input_direction);
+//      tux->set_direction(input_direction);
     }
   
   Menu* menu = Menu::current();
@@ -872,7 +917,13 @@ WorldMap::draw(DrawingContext& context, const Vector& offset)
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
       if(i->level_name.empty())
+        {
+        if (!i->display_map_message.empty() && !i->passive_message)
+          context.draw_surface(messagedot,
+              Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
+
         continue;
+        }
 
       if (i->solved)
         context.draw_surface(leveldot_green,
@@ -938,7 +989,7 @@ WorldMap::draw_status(DrawingContext& context)
                     LAYER_FOREGROUND1);
                 }
 
-              /* Display a message in the map, if any as been selected */
+              /* Display an in-map message in the map, if any as been selected */
               if(!i->display_map_message.empty())
                 context.draw_text_center(gold_text, i->display_map_message, 
                     Vector(0, screen->h - white_text->get_height() - 60),
@@ -947,6 +998,11 @@ WorldMap::draw_status(DrawingContext& context)
             }
         }
     }
+  /* Display a passive message in the map, if needed */
+  if(passive_message_timer.check())
+    context.draw_text_center(gold_text, passive_message, 
+            Vector(0, screen->h - white_text->get_height() - 60),
+            LAYER_FOREGROUND1);
 }
 
 void
@@ -958,24 +1014,23 @@ WorldMap::display()
 
   song = SoundManager::get()->load_music(datadir +  "/music/" + music);
   SoundManager::get()->play_music(song);
-  
-  unsigned int last_update_time;
-  unsigned int update_time;
 
-  last_update_time = update_time = Ticks::get();
+  FrameRate frame_rate(10);
+  frame_rate.set_frame_limit(false);
+
+  frame_rate.start();
 
   DrawingContext context;
   while(!quit)
     {
-      float delta = ((float)(update_time-last_update_time))/100.0;
+      float delta = frame_rate.get();
 
       delta *= 1.3f;
 
       if (delta > 10.0f)
         delta = .3f;
-      
-      last_update_time = update_time;
-      update_time      = Ticks::get();
+       
+      frame_rate.update();
 
       Vector tux_pos = tux->get_pos();
       if (1)
@@ -993,7 +1048,7 @@ WorldMap::display()
       draw(context, offset);
       get_input();
       update(delta);
-
+      
       if(Menu::current())
         {
           Menu::current()->draw(context);
@@ -1009,7 +1064,7 @@ WorldMap::display()
 void
 WorldMap::savegame(const std::string& filename)
 {
-  if(filename != "")
+  if(filename == "")
     return;
 
   std::cout << "savegame: " << filename << std::endl;
@@ -1032,13 +1087,13 @@ WorldMap::savegame(const std::string& filename)
       << "  (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"
-      << "  (special_tiles\n";
+      << "  (levels\n";
   
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
       if (i->solved && !i->level_name.empty())
         {
-          out << "     (special_tile (name \"" << i->level_name << "\")\n"
+          out << "     (level (name \"" << i->level_name << "\")\n"
               << "            (solved #t))\n";
         }
     }  
@@ -1109,14 +1164,14 @@ WorldMap::loadgame(const std::string& filename)
     }
 
   lisp_object_t* level_cur = 0;
-  if (reader.read_lisp("special_tiles", level_cur))
+  if (reader.read_lisp("levels", level_cur))
     {
       while(level_cur)
         {
           lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
           lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
 
-          if (strcmp(lisp_symbol(sym), "special_tile") == 0)
+          if (strcmp(lisp_symbol(sym), "level") == 0)
             {
               std::string name;
               bool solved = false;