- Use obstacks for memory allocation for lispfiles and DrawingRequests,
[supertux.git] / src / worldmap / worldmap.cpp
index 28e39a7..65cb1f0 100644 (file)
@@ -176,6 +176,8 @@ WorldMap::~WorldMap()
 {
   using namespace Scripting;
 
+  save_state();
+
   for(ScriptList::iterator i = scripts.begin();
       i != scripts.end(); ++i) {
     HSQOBJECT& object = *i;
@@ -245,7 +247,7 @@ WorldMap::load(const std::string& filename)
 
   try {
     lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
+    const lisp::Lisp* root = parser.parse(map_filename);
 
     const lisp::Lisp* lisp = root->get_lisp("supertux-level");
     if(!lisp)
@@ -321,7 +323,7 @@ WorldMap::get_level_title(LevelTile& level)
 
   try {
     lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.get_name()));
+    const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
 
     const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
     if(!level_lisp)
@@ -432,28 +434,31 @@ WorldMap::finished_level(Level* gamelevel)
 
   save_state();
 
-  if (old_level_state != level->solved && level->auto_path) {
+  if (old_level_state != level->solved) {
     // Try to detect the next direction to which we should walk
     // FIXME: Mostly a hack
     Direction dir = D_NONE;
 
     const Tile* tile = at(tux->get_tile_pos());
 
+       int dirdata = tile->getData() & Tile::WORLDMAP_DIR_MASK;
     // first, test for crossroads
-    if (tile->getData() & Tile::WORLDMAP_CNSE || tile->getData() && Tile::WORLDMAP_CNSW
-     || tile->getData() & Tile::WORLDMAP_CNEW || tile->getData() && Tile::WORLDMAP_CSEW
-     || tile->getData() & Tile::WORLDMAP_CNSEW)
+    if (dirdata == Tile::WORLDMAP_CNSE ||
+               dirdata == Tile::WORLDMAP_CNSW ||
+               dirdata == Tile::WORLDMAP_CNEW ||
+               dirdata == Tile::WORLDMAP_CSEW ||
+               dirdata == Tile::WORLDMAP_CNSEW)
       dir = D_NONE;
-    else if (tile->getData() & Tile::WORLDMAP_NORTH
+    else if (dirdata & Tile::WORLDMAP_NORTH
         && tux->back_direction != D_NORTH)
       dir = D_NORTH;
-    else if (tile->getData() & Tile::WORLDMAP_SOUTH
+    else if (dirdata & Tile::WORLDMAP_SOUTH
         && tux->back_direction != D_SOUTH)
       dir = D_SOUTH;
-    else if (tile->getData() & Tile::WORLDMAP_EAST
+    else if (dirdata & Tile::WORLDMAP_EAST
         && tux->back_direction != D_EAST)
       dir = D_EAST;
-    else if (tile->getData() & Tile::WORLDMAP_WEST
+    else if (dirdata & Tile::WORLDMAP_WEST
         && tux->back_direction != D_WEST)
       dir = D_WEST;
 
@@ -523,17 +528,25 @@ WorldMap::update(float delta)
     if (camera_offset.y < 0)
       camera_offset.y = 0;
 
-    if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
-      camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
-    if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
-      camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
+    if (camera_offset.x > (int)solids->get_width()*32 - SCREEN_WIDTH)
+      camera_offset.x = (int)solids->get_width()*32 - SCREEN_WIDTH;
+    if (camera_offset.y > (int)solids->get_height()*32 - SCREEN_HEIGHT)
+      camera_offset.y = (int)solids->get_height()*32 - SCREEN_HEIGHT;
+
+    if (int(solids->get_width()*32) < SCREEN_WIDTH)
+      camera_offset.x = solids->get_width()*16.0 - SCREEN_WIDTH/2.0;
+    if (int(solids->get_height()*32) < SCREEN_HEIGHT)
+      camera_offset.y = solids->get_height()*16.0 - SCREEN_HEIGHT/2.0;
 
     // handle input
     bool enter_level = false;
     if(main_controller->pressed(Controller::ACTION)
         || main_controller->pressed(Controller::JUMP)
-        || main_controller->pressed(Controller::MENU_SELECT))
-      enter_level = true;
+        || main_controller->pressed(Controller::MENU_SELECT)) {
+      /* some people define UP and JUMP on the same key... */
+      if(!main_controller->pressed(Controller::UP))
+           enter_level = true;
+       }
     if(main_controller->pressed(Controller::PAUSE_MENU))
       on_escape_press();
 
@@ -555,7 +568,6 @@ WorldMap::update(float delta)
     LevelTile* level = at_level();
     if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
       enter_level = true;
-      level->solved = true;
     }
 
     if (enter_level && !tux->is_moving())
@@ -656,6 +668,10 @@ WorldMap::at_teleporter(const Vector& pos)
 void
 WorldMap::draw(DrawingContext& context)
 {
+  if (int(solids->get_width()*32) < SCREEN_WIDTH || int(solids->get_height()*32) < SCREEN_HEIGHT)
+    context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
+      Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
+
   context.set_ambient_color( ambient_light );
   context.push_transform();
   context.set_translation(camera_offset);
@@ -687,9 +703,9 @@ WorldMap::draw_status(DrawingContext& context)
           get_level_title(*level);
 
         context.draw_text(white_text, level->title,
-            Vector(SCREEN_WIDTH/2,
-              SCREEN_HEIGHT - white_text->get_height() - 30),
-            CENTER_ALLIGN, LAYER_FOREGROUND1);
+                          Vector(SCREEN_WIDTH/2,
+                                 SCREEN_HEIGHT - white_text->get_height() - 30),
+                          ALIGN_CENTER, LAYER_FOREGROUND1);
 
         // if level is solved, draw level picture behind stats
         /*
@@ -719,7 +735,7 @@ WorldMap::draw_status(DrawingContext& context)
           context.draw_text(gold_text, special_tile->map_message,
               Vector(SCREEN_WIDTH/2,
                 SCREEN_HEIGHT - white_text->get_height() - 60),
-              CENTER_ALLIGN, LAYER_FOREGROUND1);
+              ALIGN_CENTER, LAYER_FOREGROUND1);
         break;
       }
     }
@@ -728,7 +744,7 @@ WorldMap::draw_status(DrawingContext& context)
     Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
     if (teleporter && (teleporter->message != "")) {
       Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 30);
-      context.draw_text(white_text, teleporter->message, pos, CENTER_ALLIGN, LAYER_FOREGROUND1);
+      context.draw_text(white_text, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1);
     }
 
   }
@@ -737,7 +753,7 @@ WorldMap::draw_status(DrawingContext& context)
   if(passive_message_timer.started())
     context.draw_text(gold_text, passive_message,
             Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
-            CENTER_ALLIGN, LAYER_FOREGROUND1);
+            ALIGN_CENTER, LAYER_FOREGROUND1);
 
   context.pop_transform();
 }
@@ -935,28 +951,26 @@ WorldMap::save_state()
     for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
       LevelTile* level = *i;
 
-      if (level->solved) {
-        sq_pushstring(vm, level->get_name().c_str(), -1);
-        sq_newtable(vm);
+         sq_pushstring(vm, level->get_name().c_str(), -1);
+         sq_newtable(vm);
 
-        store_bool(vm, "solved", true);
-        // TODO write statistics
-        // i->statistics.write(writer);
+         store_bool(vm, "solved", level->solved);
+         // TODO write statistics
+         // i->statistics.write(writer);
 
-        sq_createslot(vm, -3);
-      }
+         sq_createslot(vm, -3);
     }
 
     sq_createslot(vm, -3);
 
     // push world into worlds table
     sq_createslot(vm, -3);
-  } catch(std::exception& e) {
+  } catch(std::exception& ) {
     sq_settop(vm, oldtop);
   }
 
   sq_settop(vm, oldtop);
-  
+
   if(World::current() != NULL)
     World::current()->save_state();
 }
@@ -984,7 +998,7 @@ WorldMap::load_state()
     // get table for our world
     sq_pushstring(vm, map_filename.c_str(), map_filename.length());
     if(SQ_FAILED(sq_get(vm, -2)))
-      throw Scripting::SquirrelError(vm, "Couldn't get state.world.mapfilename");
+      throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
 
     // load tux
     sq_pushstring(vm, "tux", -1);