- made worldmap CPU independend, needs testing
[supertux.git] / src / worldmap.cpp
index dbcc71a..961b637 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)
@@ -110,6 +110,7 @@ TileManager::TileManager()
               tile->south = true;
               tile->west  = true;
               tile->stop  = true;
+              tile->auto_walk = false;
   
               LispReader reader(lisp_cdr(element));
               reader.read_int("id",  &id);
@@ -118,6 +119,7 @@ TileManager::TileManager()
               reader.read_bool("west",  &tile->west);
               reader.read_bool("east",  &tile->east);
               reader.read_bool("stop",  &tile->stop);
+              reader.read_bool("auto-walk",  &tile->auto_walk);
               reader.read_string("image",  &filename);
 
               tile->sprite = new Surface(
@@ -163,7 +165,10 @@ TileManager::get(int i)
 Tux::Tux(WorldMap* worldmap_)
   : worldmap(worldmap_)
 {
-  sprite = new Surface(datadir +  "/images/worldmap/tux.png", USE_ALPHA);
+  largetux_sprite = new Surface(datadir +  "/images/worldmap/tux.png", USE_ALPHA);
+  firetux_sprite = new Surface(datadir +  "/images/worldmap/firetux.png", USE_ALPHA);
+  smalltux_sprite = new Surface(datadir +  "/images/worldmap/smalltux.png", USE_ALPHA);
+
   offset = 0;
   moving = false;
   tile_pos.x = 4;
@@ -174,15 +179,30 @@ Tux::Tux(WorldMap* worldmap_)
 
 Tux::~Tux()
 {
-  delete sprite;
+  delete smalltux_sprite;
+  delete firetux_sprite;
+  delete largetux_sprite;
 }
 
 void
 Tux::draw(const Point& offset)
 {
   Point pos = get_pos();
-  sprite->draw(pos.x + offset.x, 
-               pos.y + offset.y - 10);
+  switch (player_status.bonus)
+    {
+    case PlayerStatus::GROWUP_BONUS:
+      largetux_sprite->draw(pos.x + offset.x, 
+                            pos.y + offset.y - 10);
+      break;
+    case PlayerStatus::FLOWER_BONUS:
+      firetux_sprite->draw(pos.x + offset.x, 
+                           pos.y + offset.y - 10);
+      break;
+    case PlayerStatus::NO_BONUS:
+      smalltux_sprite->draw(pos.x + offset.x, 
+                            pos.y + offset.y - 10);
+      break;
+    }
 }
 
 
@@ -265,6 +285,33 @@ Tux::update(float delta)
             }
           else
             {
+              if (worldmap->at(tile_pos)->auto_walk)
+                { // Turn to a new direction
+                  Tile* tile = worldmap->at(tile_pos);
+                  Direction dir = NONE;
+                  
+                  if (tile->north && back_direction != NORTH)
+                    dir = NORTH;
+                  else if (tile->south && back_direction != SOUTH)
+                    dir = SOUTH;
+                  else if (tile->east && back_direction != EAST)
+                    dir = EAST;
+                  else if (tile->west && back_direction != WEST)
+                    dir = WEST;
+
+                  if (dir != NONE)
+                    {
+                      direction = dir;
+                      back_direction = reverse_dir(direction);
+                    }
+                  else
+                    {
+                      // Should never be reached if tiledata is good
+                      stop();
+                      return;
+                    }
+                }
+
               // Walk automatically to the next tile
               Point next_tile;
               if (worldmap->path_ok(direction, tile_pos, &next_tile))
@@ -327,7 +374,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)
@@ -373,6 +420,7 @@ WorldMap::load_map()
                       level.south = true;
                       level.west  = true;
 
+                      reader.read_string("extro-filename",  &level.extro_filename);
                       reader.read_string("name",  &level.name);
                       reader.read_int("x", &level.x);
                       reader.read_int("y", &level.y);
@@ -404,10 +452,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;
   }
 
@@ -579,7 +627,7 @@ WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
 }
 
 void
-WorldMap::update()
+WorldMap::update(float delta)
 {
   if (enter_level && !tux->is_moving())
     {
@@ -590,13 +638,56 @@ 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(delta);
+                          }
+
+                        std::cout << "Walk to dir: " << dir << std::endl;
+                      }
+
+                    if (!level->extro_filename.empty())
+                      { // Display final credits and go back to the main menu
+                        display_text_file(level->extro_filename,
+                                          "/images/background/arctis2.jpg");
+                        quit = true;
+                      }
+                  }
+
                   break;
                 case GameSession::LEVEL_ABORT:
                   // Reseting the player_status might be a worthy
@@ -609,6 +700,7 @@ WorldMap::update()
                   break;
                 case GameSession::GAME_OVER:
                   quit = true;
+                  player_status.bonus = PlayerStatus::NO_BONUS;
                   break;
                 case GameSession::NONE:
                   // Should never be reached 
@@ -631,7 +723,7 @@ WorldMap::update()
   else
     {
       tux->set_direction(input_direction);
-      tux->update(0.33f);
+      tux->update(delta);
     }
   
   Menu* menu = Menu::current();
@@ -761,33 +853,46 @@ WorldMap::display()
   song = music_manager->load_music(datadir +  "/music/" + music);
   music_manager->play_music(song);
 
-  while(!quit) {
-    Point tux_pos = tux->get_pos();
-    if (1)
-      {
-        offset.x = -tux_pos.x + screen->w/2;
-        offset.y = -tux_pos.y + screen->h/2;
+  unsigned int last_update_time;
+  unsigned int update_time;
 
-        if (offset.x > 0) offset.x = 0;
-        if (offset.y > 0) offset.y = 0;
+  last_update_time = update_time = st_get_ticks();
 
-        if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
-        if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
-      } 
+  while(!quit)
+    {
+      float delta = ((float)(update_time-last_update_time))/100.0;
 
-    draw(offset);
-    get_input();
-    update();
+      delta *= 1.3f;
 
-  if(Menu::current())
-    {
-      Menu::current()->draw();
-      mouse_cursor->draw();
-    }
-    flipscreen();
+      last_update_time = update_time;
+      update_time      = st_get_ticks();
 
-    SDL_Delay(20);
-  }
+      Point tux_pos = tux->get_pos();
+      if (1)
+        {
+          offset.x = -tux_pos.x + screen->w/2;
+          offset.y = -tux_pos.y + screen->h/2;
+
+          if (offset.x > 0) offset.x = 0;
+          if (offset.y > 0) offset.y = 0;
+
+          if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
+          if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
+        } 
+
+      draw(offset);
+      get_input();
+      update(delta);
+
+      if(Menu::current())
+        {
+          Menu::current()->draw();
+          mouse_cursor->draw();
+        }
+      flipscreen();
+
+      SDL_Delay(20);
+    }
 }
 
 void
@@ -809,8 +914,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)
@@ -836,6 +942,12 @@ WorldMap::loadgame(const std::string& filename)
     return;
   
   lisp_object_t* savegame = lisp_read_from_file(filename);
+  if (!savegame)
+    {
+      std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
+      return;
+    }
+
   lisp_object_t* cur = savegame;
 
   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
@@ -856,12 +968,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);
     }