- added missing include
[supertux.git] / src / worldmap.cpp
index d0b378b..617795c 100644 (file)
@@ -21,6 +21,7 @@
 #include <fstream>
 #include <vector>
 #include <assert.h>
+#include <unistd.h>
 #include "globals.h"
 #include "texture.h"
 #include "screen.h"
@@ -36,18 +37,18 @@ Direction reverse_dir(Direction direction)
 {
   switch(direction)
     {
-    case WEST:
-      return EAST;
-    case EAST:
-      return WEST;
-    case NORTH:
-      return SOUTH;
-    case SOUTH:
-      return NORTH;
-    case NONE:
-      return NONE;
+    case D_WEST:
+      return D_EAST;
+    case D_EAST:
+      return D_WEST;
+    case D_NORTH:
+      return D_SOUTH;
+    case D_SOUTH:
+      return D_NORTH;
+    case D_NONE:
+      return D_NONE;
     }
-  return NONE;
+  return D_NONE;
 }
 
 std::string
@@ -55,13 +56,13 @@ direction_to_string(Direction direction)
 {
   switch(direction)
     {
-    case WEST:
+    case D_WEST:
       return "west";
-    case EAST:
+    case D_EAST:
       return "east";
-    case NORTH:
+    case D_NORTH:
       return "north";
-    case SOUTH:
+    case D_SOUTH:
       return "south";
     default:
       return "none";
@@ -72,15 +73,15 @@ Direction
 string_to_direction(const std::string& directory)
 {
   if (directory == "west")
-    return WEST;
+    return D_WEST;
   else if (directory == "east")
-    return EAST;
+    return D_EAST;
   else if (directory == "north")
-    return NORTH;
+    return D_NORTH;
   else if (directory == "south")
-    return SOUTH;
+    return D_SOUTH;
   else
-    return NONE;
+    return D_NONE;
 }
 
 TileManager::TileManager()
@@ -173,8 +174,8 @@ Tux::Tux(WorldMap* worldmap_)
   moving = false;
   tile_pos.x = 4;
   tile_pos.y = 5;
-  direction = NONE;
-  input_direction = NONE;
+  direction = D_NONE;
+  input_direction = D_NONE;
 }
 
 Tux::~Tux()
@@ -214,19 +215,19 @@ Tux::get_pos()
 
   switch(direction)
     {
-    case WEST:
+    case D_WEST:
       x -= offset - 32;
       break;
-    case EAST:
+    case D_EAST:
       x += offset - 32;
       break;
-    case NORTH:
+    case D_NORTH:
       y -= offset - 32;
       break;
-    case SOUTH:
+    case D_SOUTH:
       y += offset - 32;
       break;
-    case NONE:
+    case D_NONE:
       break;
     }
   
@@ -237,7 +238,7 @@ void
 Tux::stop()
 {
   offset = 0;
-  direction = NONE;
+  direction = D_NONE;
   moving = false;
 }
 
@@ -246,7 +247,7 @@ Tux::update(float delta)
 {
   if (!moving)
     {
-      if (input_direction != NONE)
+      if (input_direction != D_NONE)
         { 
           WorldMap::Level* level = worldmap->at_level();
 
@@ -288,18 +289,18 @@ Tux::update(float delta)
               if (worldmap->at(tile_pos)->auto_walk)
                 { // Turn to a new direction
                   Tile* tile = worldmap->at(tile_pos);
-                  Direction dir = NONE;
+                  Direction dir = D_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)
+                  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;
                       back_direction = reverse_dir(direction);
@@ -352,7 +353,7 @@ WorldMap::WorldMap()
   leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", USE_ALPHA);
   leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", USE_ALPHA);
 
-  input_direction = NONE;
+  input_direction = D_NONE;
   enter_level = false;
 
   name = "<no file>";
@@ -420,6 +421,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);
@@ -492,7 +494,7 @@ void
 WorldMap::get_input()
 {
   enter_level = false;
-  input_direction = NONE;
+  input_direction = D_NONE;
    
   SDL_Event event;
   while (SDL_PollEvent(&event))
@@ -528,16 +530,16 @@ WorldMap::get_input()
               if (event.jaxis.axis == joystick_keymap.x_axis)
                 {
                   if (event.jaxis.value < -joystick_keymap.dead_zone)
-                    input_direction = WEST;
+                    input_direction = D_WEST;
                   else if (event.jaxis.value > joystick_keymap.dead_zone)
-                    input_direction = EAST;
+                    input_direction = D_EAST;
                 }
               else if (event.jaxis.axis == joystick_keymap.y_axis)
                 {
                   if (event.jaxis.value > joystick_keymap.dead_zone)
-                    input_direction = SOUTH;
+                    input_direction = D_SOUTH;
                   else if (event.jaxis.value < -joystick_keymap.dead_zone)
-                    input_direction = NORTH;
+                    input_direction = D_NORTH;
                 }
               break;
 
@@ -559,13 +561,13 @@ WorldMap::get_input()
       Uint8 *keystate = SDL_GetKeyState(NULL);
   
       if (keystate[SDLK_LEFT])
-        input_direction = WEST;
+        input_direction = D_WEST;
       else if (keystate[SDLK_RIGHT])
-        input_direction = EAST;
+        input_direction = D_EAST;
       else if (keystate[SDLK_UP])
-        input_direction = NORTH;
+        input_direction = D_NORTH;
       else if (keystate[SDLK_DOWN])
-        input_direction = SOUTH;
+        input_direction = D_SOUTH;
     }
 }
 
@@ -574,19 +576,19 @@ WorldMap::get_next_tile(Point pos, Direction direction)
 {
   switch(direction)
     {
-    case WEST:
+    case D_WEST:
       pos.x -= 1;
       break;
-    case EAST:
+    case D_EAST:
       pos.x += 1;
       break;
-    case NORTH:
+    case D_NORTH:
       pos.y -= 1;
       break;
-    case SOUTH:
+    case D_SOUTH:
       pos.y += 1;
       break;
-    case NONE:
+    case D_NONE:
       break;
     }
   return pos;
@@ -606,19 +608,19 @@ WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
     { // Check if we the tile allows us to go to new_pos
       switch(direction)
         {
-        case WEST:
+        case D_WEST:
           return (at(old_pos)->west && at(*new_pos)->east);
 
-        case EAST:
+        case D_EAST:
           return (at(old_pos)->east && at(*new_pos)->west);
 
-        case NORTH:
+        case D_NORTH:
           return (at(old_pos)->north && at(*new_pos)->south);
 
-        case SOUTH:
+        case D_SOUTH:
           return (at(old_pos)->south && at(*new_pos)->north);
 
-        case NONE:
+        case D_NONE:
           assert(!"path_ok() can't work if direction is NONE");
         }
       return false;
@@ -626,7 +628,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())
     {
@@ -642,12 +644,13 @@ WorldMap::update()
 
               switch (session.run())
                 {
-                case GameSession::LEVEL_FINISHED:
+                case GameSession::ES_LEVEL_FINISHED:
                   {
                     bool old_level_state = level->solved;
                     level->solved = true;
 
-                    if (session.get_world()->get_tux()->got_coffee)
+                    if (session.get_world()->get_tux()->got_power !=
+                          session.get_world()->get_tux()->NONE_POWER)
                       player_status.bonus = PlayerStatus::FLOWER_BONUS;
                     else if (session.get_world()->get_tux()->size == BIG)
                       player_status.bonus = PlayerStatus::GROWUP_BONUS;
@@ -657,31 +660,44 @@ WorldMap::update()
                     if (old_level_state != level->solved)
                       { // Try to detect the next direction to which we should walk
                         // FIXME: Mostly a hack
-                        Direction dir = NONE;
+                        Direction dir = D_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 (tile->north && tux->back_direction != D_NORTH)
+                          dir = D_NORTH;
+                        else if (tile->south && tux->back_direction != D_SOUTH)
+                          dir = D_SOUTH;
+                        else if (tile->east && tux->back_direction != D_EAST)
+                          dir = D_EAST;
+                        else if (tile->west && tux->back_direction != D_WEST)
+                          dir = D_WEST;
 
-                        if (dir != NONE)
+                        if (dir != D_NONE)
                           {
                             tux->set_direction(dir);
-                            tux->update(0.33f);
+                            //tux->update(delta);
                           }
 
                         std::cout << "Walk to dir: " << dir << std::endl;
                       }
+
+                    if (!level->extro_filename.empty())
+                      { 
+                        MusicRef theme =
+                          music_manager->load_music(datadir + "/music/theme.mod");
+                        music_manager->play_music(theme);
+                        // Display final credits and go back to the main menu
+                        display_text_file(level->extro_filename,
+                                          "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
+                        display_text_file("CREDITS",
+                                          "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
+                        quit = true;
+                      }
                   }
 
                   break;
-                case GameSession::LEVEL_ABORT:
+                case GameSession::ES_LEVEL_ABORT:
                   // Reseting the player_status might be a worthy
                   // consideration, but I don't think we need it
                   // 'cause only the bad players will use it to
@@ -690,11 +706,32 @@ WorldMap::update()
                   // then stop playing the game all together since it
                   // is to hard)
                   break;
-                case GameSession::GAME_OVER:
+                case GameSession::ES_GAME_OVER:
+                  /* 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 */
+                  char str[80];
+
+                  drawgradient(Color (0, 255, 0), Color (255, 0, 255));
+
+                  blue_text->drawf("GAMEOVER", 0, 200, A_HMIDDLE, A_TOP, 1);
+
+                  sprintf(str, "SCORE: %d", player_status.score);
+                  gold_text->drawf(str, 0, 224, A_HMIDDLE, A_TOP, 1);
+
+                  sprintf(str, "COINS: %d", player_status.distros);
+                  gold_text->drawf(str, 0, screen->w - gold_text->w*2, A_HMIDDLE, A_TOP, 1);
+
+                  flipscreen();
+  
+                  SDL_Event event;
+                  wait_for_event(event,2000,5000,true);
+
                   quit = true;
-                  player_status.bonus = PlayerStatus::NO_BONUS;
+                  player_status.reset();
                   break;
-                case GameSession::NONE:
+                case GameSession::ES_NONE:
                   // Should never be reached 
                   break;
                 }
@@ -714,8 +751,8 @@ WorldMap::update()
     }
   else
     {
+      tux->update(delta);
       tux->set_direction(input_direction);
-      tux->update(0.33f);
     }
   
   Menu* menu = Menu::current();
@@ -729,11 +766,6 @@ WorldMap::update()
             {
             case MNID_RETURNWORLDMAP: // Return to game
               break;
-            case MNID_SAVEGAME:
-              if (!savegame_file.empty())
-                savegame(savegame_file);
-              break;
-                
             case MNID_QUITWORLDMAP: // Quit Worldmap
               quit = true;
               break;
@@ -805,20 +837,20 @@ WorldMap::draw_status()
   gold_text->draw(str, 96, 0);
 
   sprintf(str, "%d", player_status.distros);
-  white_text->draw_align("COINS", 320-64, 0,  A_LEFT, A_TOP);
-  gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP);
+  white_text->draw_align("COINS", screen->w/2 - white_text->w*5, 0,  A_LEFT, A_TOP);
+  gold_text->draw_align(str, screen->w/2 + (white_text->w*5)/2, 0, A_RIGHT, A_TOP);
 
-  white_text->draw("LIVES", 480, 0);
+  white_text->draw("LIVES", screen->w - white_text->w*9, 0);
   if (player_status.lives >= 5)
     {
       sprintf(str, "%dx", player_status.lives);
-      gold_text->draw(str, 585, 0);
-      tux_life->draw(565+(18*3), 0);
+      gold_text->draw_align(str, screen->w - gold_text->w, 0, A_RIGHT, A_TOP);
+      tux_life->draw(screen->w - gold_text->w, 0);
     }
   else
     {
       for(int i= 0; i < player_status.lives; ++i)
-        tux_life->draw(565+(18*i),0);
+        tux_life->draw(screen->w - tux_life->w*4 +(tux_life->w*i),0);
     }
 
   if (!tux->is_moving())
@@ -845,33 +877,49 @@ 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();
+      if (delta > 10.0f)
+        delta = .3f;
+      
+      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
@@ -921,7 +969,7 @@ WorldMap::loadgame(const std::string& filename)
     return;
   
   lisp_object_t* savegame = lisp_read_from_file(filename);
-  if (savegame)
+  if (!savegame)
     {
       std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
       return;