First step towards multiple tilesets per tilemap. Code is very inefficient for now...
[supertux.git] / src / worldmap / worldmap.cpp
index 65cb1f0..d1d7dc8 100644 (file)
@@ -36,6 +36,7 @@
 #include "shrinkfade.hpp"
 #include "video/surface.hpp"
 #include "video/drawing_context.hpp"
+#include "sprite/sprite.hpp"
 #include "sprite/sprite_manager.hpp"
 #include "audio/sound_manager.hpp"
 #include "lisp/parser.hpp"
@@ -133,9 +134,10 @@ string_to_direction(const std::string& directory)
 //---------------------------------------------------------------------------
 
 WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint)
-  : tux(0), solids(0), ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), force_spawnpoint(force_spawnpoint), in_level(false)
+  : tux(0), ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), force_spawnpoint(force_spawnpoint), in_level(false)
 {
-  tile_manager.reset(new TileManager("images/worldmap.strf"));
+  tile_manager.reset(new TileManager());
+  //"images/worldmap.strf");
 
   tux = new Tux(this);
   add_object(tux);
@@ -153,8 +155,6 @@ WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpo
   worldmap_menu->add_hl();
   worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
 
-  load(filename);
-
   // create a new squirrel table for the worldmap
   using namespace Scripting;
 
@@ -170,13 +170,28 @@ WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpo
 
   sq_addref(global_vm, &worldmap_table);
   sq_pop(global_vm, 1);
+
+  sound_manager->preload("sounds/warp.wav");
+  
+  // load worldmap objects
+  load(filename);
 }
 
 WorldMap::~WorldMap()
 {
   using namespace Scripting;
 
-  save_state();
+  for(GameObjects::iterator i = game_objects.begin();
+      i != game_objects.end(); ++i) {
+    GameObject* object = *i;
+    try_unexpose(object);
+    object->unref();
+  }
+
+  for(SpawnPoints::iterator i = spawn_points.begin();
+      i != spawn_points.end(); ++i) {
+    delete *i;
+  }
 
   for(ScriptList::iterator i = scripts.begin();
       i != scripts.end(); ++i) {
@@ -189,17 +204,6 @@ WorldMap::~WorldMap()
 
   if(current_ == this)
     current_ = NULL;
-
-  for(GameObjects::iterator i = game_objects.begin();
-      i != game_objects.end(); ++i) {
-    GameObject* object = *i;
-    object->unref();
-  }
-
-  for(SpawnPoints::iterator i = spawn_points.begin();
-      i != spawn_points.end(); ++i) {
-    delete *i;
-  }
 }
 
 void
@@ -207,14 +211,44 @@ WorldMap::add_object(GameObject* object)
 {
   TileMap* tilemap = dynamic_cast<TileMap*> (object);
   if(tilemap != 0 && tilemap->is_solid()) {
-    solids = tilemap;
+    solid_tilemaps.push_back(tilemap);
   }
 
   object->ref();
+  try_expose(object);
   game_objects.push_back(object);
 }
 
 void
+WorldMap::try_expose(GameObject* object)
+{
+  ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
+  if(interface != NULL) {
+    HSQUIRRELVM vm = Scripting::global_vm;
+    sq_pushobject(vm, worldmap_table);
+    interface->expose(vm, -1);
+    sq_pop(vm, 1);
+  }
+}
+
+void
+WorldMap::try_unexpose(GameObject* object)
+{
+  ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
+  if(interface != NULL) {
+    HSQUIRRELVM vm = Scripting::global_vm;
+    SQInteger oldtop = sq_gettop(vm);
+    sq_pushobject(vm, worldmap_table);
+    try {
+      interface->unexpose(vm, -1);
+    } catch(std::exception& e) {
+      log_warning << "Couldn't unregister object: " << e.what() << std::endl;
+    }
+    sq_settop(vm, oldtop);
+  }
+}
+
+void
 WorldMap::move_to_spawnpoint(const std::string& spawnpoint)
 {
   for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
@@ -262,7 +296,10 @@ WorldMap::load(const std::string& filename)
     lisp::ListIterator iter(sector);
     while(iter.next()) {
       if(iter.item() == "tilemap") {
-        add_object(new TileMap(*(iter.lisp()), tile_manager.get()));
+
+        TileMap::loading_worldmap = true;
+
+        add_object(new TileMap(*(iter.lisp())));
       } else if(iter.item() == "background") {
         add_object(new Background(*(iter.lisp())));
       } else if(iter.item() == "music") {
@@ -302,7 +339,7 @@ WorldMap::load(const std::string& filename)
         log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
       }
     }
-    if(solids == 0)
+    if(solid_tilemaps.size() == 0)
       throw std::runtime_error("No solid tilemap specified");
 
     move_to_spawnpoint("main");
@@ -338,7 +375,7 @@ WorldMap::get_level_title(LevelTile& level)
 
 void WorldMap::calculate_total_stats()
 {
-  total_stats.reset();
+  total_stats.zero();
   for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
     LevelTile* level = *i;
     if (level->solved) {
@@ -386,30 +423,32 @@ WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos)
 {
   *new_pos = get_next_tile(old_pos, direction);
 
-  if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
-        && new_pos->y >= 0 && new_pos->y < solids->get_height()))
+  if (!(new_pos->x >= 0 && new_pos->x < get_width()
+        && new_pos->y >= 0 && new_pos->y < get_height()))
     { // New position is outsite the tilemap
       return false;
     }
   else
     { // Check if the tile allows us to go to new_pos
+      int old_tile_data = tile_data_at(old_pos);
+      int new_tile_data = tile_data_at(*new_pos);
       switch(direction)
         {
         case D_WEST:
-          return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
-              && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
+          return (old_tile_data & Tile::WORLDMAP_WEST
+              && new_tile_data & Tile::WORLDMAP_EAST);
 
         case D_EAST:
-          return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
-              && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
+          return (old_tile_data & Tile::WORLDMAP_EAST
+              && new_tile_data & Tile::WORLDMAP_WEST);
 
         case D_NORTH:
-          return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
-              && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
+          return (old_tile_data & Tile::WORLDMAP_NORTH
+              && new_tile_data & Tile::WORLDMAP_SOUTH);
 
         case D_SOUTH:
-          return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
-              && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
+          return (old_tile_data & Tile::WORLDMAP_SOUTH
+              && new_tile_data & Tile::WORLDMAP_NORTH);
 
         case D_NONE:
           assert(!"path_ok() can't walk if direction is NONE");
@@ -439,9 +478,7 @@ WorldMap::finished_level(Level* gamelevel)
     // FIXME: Mostly a hack
     Direction dir = D_NONE;
 
-    const Tile* tile = at(tux->get_tile_pos());
-
-       int dirdata = tile->getData() & Tile::WORLDMAP_DIR_MASK;
+    int dirdata = available_directions_at(tux->get_tile_pos());
     // first, test for crossroads
     if (dirdata == Tile::WORLDMAP_CNSE ||
                dirdata == Tile::WORLDMAP_CNSW ||
@@ -511,6 +548,7 @@ WorldMap::update(float delta)
         i != game_objects.end(); ) {
       GameObject* object = *i;
       if(!object->is_valid()) {
+        try_unexpose(object);
         object->unref();
         i = game_objects.erase(i);
       } else {
@@ -518,6 +556,17 @@ WorldMap::update(float delta)
       }
     }
 
+    /* update solid_tilemaps list */
+    //FIXME: this could be more efficient
+    solid_tilemaps.clear();
+    for(std::vector<GameObject*>::iterator i = game_objects.begin();
+        i != game_objects.end(); ++i)
+    {
+      TileMap* tm = dynamic_cast<TileMap*>(*i);
+      if (!tm) continue;
+      if (tm->is_solid()) solid_tilemaps.push_back(tm);
+    }
+
     // position "camera"
     Vector tux_pos = tux->get_pos();
     camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
@@ -528,15 +577,15 @@ WorldMap::update(float delta)
     if (camera_offset.y < 0)
       camera_offset.y = 0;
 
-    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 (camera_offset.x > (int)get_width()*32 - SCREEN_WIDTH)
+      camera_offset.x = (int)get_width()*32 - SCREEN_WIDTH;
+    if (camera_offset.y > (int)get_height()*32 - SCREEN_HEIGHT)
+      camera_offset.y = (int)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;
+    if (int(get_width()*32) < SCREEN_WIDTH)
+      camera_offset.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
+    if (int(get_height()*32) < SCREEN_HEIGHT)
+      camera_offset.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
 
     // handle input
     bool enter_level = false;
@@ -576,8 +625,8 @@ WorldMap::update(float delta)
         LevelTile* level = at_level();
         if (!level) {
           //Respawn if player on a tile with no level and nowhere to go.
-          const Tile* tile = at(tux->get_tile_pos());
-          if(!( tile->getData() & ( Tile::WORLDMAP_NORTH |  Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
+          int tile_data = tile_data_at(tux->get_tile_pos());
+          if(!( tile_data & ( Tile::WORLDMAP_NORTH |  Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
             log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
             move_to_spawnpoint("main");
             return;
@@ -610,10 +659,25 @@ WorldMap::update(float delta)
   }
 }
 
-const Tile*
-WorldMap::at(Vector p)
+int
+WorldMap::tile_data_at(Vector p)
 {
-  return solids->get_tile((int) p.x, (int) p.y);
+  int dirs = 0;
+
+  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+    TileMap* tilemap = *i;
+    const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
+    int dirdata = tile->getData();
+    dirs |= dirdata;
+  }
+
+  return dirs;
+}
+
+int
+WorldMap::available_directions_at(Vector p)
+{
+  return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
 }
 
 LevelTile*
@@ -668,7 +732,7 @@ 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)
+  if (int(get_width()*32) < SCREEN_WIDTH || int(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);
 
@@ -682,6 +746,27 @@ WorldMap::draw(DrawingContext& context)
     object->draw(context);
   }
 
+/*
+  // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
+  // draw visual indication of possible walk directions
+  static int flipme = 0; 
+  if (flipme++ & 0x04)
+  for (int x = 0; x < get_width(); x++) {
+    for (int y = 0; y < get_height(); y++) {
+      int data = tile_data_at(Vector(x,y));
+      int px = x * 32;
+      int py = y * 32;
+      const int W = 4;
+      if (data & Tile::WORLDMAP_NORTH)    context.draw_filled_rect(Rect(px + 16-W, py       , px + 16+W, py + 16-W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
+      if (data & Tile::WORLDMAP_SOUTH)    context.draw_filled_rect(Rect(px + 16-W, py + 16+W, px + 16+W, py + 32  ), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
+      if (data & Tile::WORLDMAP_EAST)     context.draw_filled_rect(Rect(px + 16+W, py + 16-W, px + 32  , py + 16+W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
+      if (data & Tile::WORLDMAP_WEST)     context.draw_filled_rect(Rect(px       , py + 16-W, px + 16-W, py + 16+W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
+      if (data & Tile::WORLDMAP_DIR_MASK) context.draw_filled_rect(Rect(px + 16-W, py + 16-W, px + 16+W, py + 16+W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
+      if (data & Tile::WORLDMAP_STOP)     context.draw_filled_rect(Rect(px + 4   , py + 4   , px + 28  , py + 28  ), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
+    }
+  }
+*/
+
   draw_status(context);
   context.pop_transform();
 }
@@ -794,9 +879,12 @@ WorldMap::setup()
 void
 WorldMap::leave()
 {
-  // remove worldmap_table from roottable
   using namespace Scripting;
 
+  // save state of world and player
+  save_state();
+
+  // remove worldmap_table from roottable
   sq_pushroottable(global_vm);
   sq_pushstring(global_vm, "worldmap", -1);
   if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
@@ -804,100 +892,6 @@ WorldMap::leave()
   sq_pop(global_vm, 1);
 }
 
-static void store_float(HSQUIRRELVM vm, const char* name, float val)
-{
-  sq_pushstring(vm, name, -1);
-  sq_pushfloat(vm, val);
-  if(SQ_FAILED(sq_createslot(vm, -3)))
-    throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-
-/*
-static void store_int(HSQUIRRELVM vm, const char* name, int val)
-{
-  sq_pushstring(vm, name, -1);
-  sq_pushinteger(vm, val);
-  if(SQ_FAILED(sq_createslot(vm, -3)))
-    throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-*/
-
-static void store_string(HSQUIRRELVM vm, const char* name, const std::string& val)
-{
-  sq_pushstring(vm, name, -1);
-  sq_pushstring(vm, val.c_str(), val.length());
-  if(SQ_FAILED(sq_createslot(vm, -3)))
-    throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-
-static void store_bool(HSQUIRRELVM vm, const char* name, bool val)
-{
-  sq_pushstring(vm, name, -1);
-  sq_pushbool(vm, val ? SQTrue : SQFalse);
-  if(SQ_FAILED(sq_createslot(vm, -3)))
-    throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-
-static float read_float(HSQUIRRELVM vm, const char* name)
-{
-  sq_pushstring(vm, name, -1);
-  if(SQ_FAILED(sq_get(vm, -2))) {
-    std::ostringstream msg;
-    msg << "Couldn't get float value for '" << name << "' from table";
-    throw Scripting::SquirrelError(vm, msg.str());
-  }
-
-  float result;
-  if(SQ_FAILED(sq_getfloat(vm, -1, &result))) {
-    std::ostringstream msg;
-    msg << "Couldn't get float value for '" << name << "' from table";
-    throw Scripting::SquirrelError(vm, msg.str());
-  }
-  sq_pop(vm, 1);
-
-  return result;
-}
-
-static std::string read_string(HSQUIRRELVM vm, const char* name)
-{
-  sq_pushstring(vm, name, -1);
-  if(SQ_FAILED(sq_get(vm, -2))) {
-    std::ostringstream msg;
-    msg << "Couldn't get string value for '" << name << "' from table";
-    throw Scripting::SquirrelError(vm, msg.str());
-  }
-
-  const char* result;
-  if(SQ_FAILED(sq_getstring(vm, -1, &result))) {
-    std::ostringstream msg;
-    msg << "Couldn't get string value for '" << name << "' from table";
-    throw Scripting::SquirrelError(vm, msg.str());
-  }
-  sq_pop(vm, 1);
-
-  return std::string(result);
-}
-
-static bool read_bool(HSQUIRRELVM vm, const char* name)
-{
-  sq_pushstring(vm, name, -1);
-  if(SQ_FAILED(sq_get(vm, -2))) {
-    std::ostringstream msg;
-    msg << "Couldn't get bool value for '" << name << "' from table";
-    throw Scripting::SquirrelError(vm, msg.str());
-  }
-
-  SQBool result;
-  if(SQ_FAILED(sq_getbool(vm, -1, &result))) {
-    std::ostringstream msg;
-    msg << "Couldn't get bool value for '" << name << "' from table";
-    throw Scripting::SquirrelError(vm, msg.str());
-  }
-  sq_pop(vm, 1);
-
-  return result == SQTrue;
-}
-
 void
 WorldMap::save_state()
 {
@@ -955,14 +949,16 @@ WorldMap::save_state()
          sq_newtable(vm);
 
          store_bool(vm, "solved", level->solved);
-         // TODO write statistics
-         // i->statistics.write(writer);
+         level->statistics.serialize_to_squirrel(vm);
 
          sq_createslot(vm, -3);
     }
 
     sq_createslot(vm, -3);
 
+    // overall statistics...
+    total_stats.serialize_to_squirrel(vm);
+
     // push world into worlds table
     sq_createslot(vm, -3);
   } catch(std::exception& ) {
@@ -1025,12 +1021,17 @@ WorldMap::load_state()
       if(SQ_SUCCEEDED(sq_get(vm, -2))) {
         level->solved = read_bool(vm, "solved");
         level->sprite->set_action(level->solved ? "solved" : "default");
-        // i->statistics.parse(*level);
+        level->statistics.unserialize_from_squirrel(vm);
         sq_pop(vm, 1);
       }
     }
+
+    // leave state table
     sq_pop(vm, 1);
 
+    // load overall statistics
+    total_stats.unserialize_from_squirrel(vm);
+
   } catch(std::exception& e) {
     log_debug << "Not loading worldmap state: " << e.what() << std::endl;
   }
@@ -1093,4 +1094,26 @@ WorldMap::run_script(std::istream& in, const std::string& sourcename)
   return vm;
 }
 
+float
+WorldMap::get_width() const
+{
+  float width = 0;
+  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+    TileMap* solids = *i;
+    if (solids->get_width() > width) width = solids->get_width();
+  }
+  return width;
+}
+
+float
+WorldMap::get_height() const
+{
+  float height = 0;
+  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+    TileMap* solids = *i;
+    if (solids->get_height() > height) height = solids->get_height();
+  }
+  return height;
+}
+
 } // namespace WorldMapNS