+//---------------------------------------------------------------------------
+
+WorldMap::WorldMap()
+{
+ tile_manager = new TileManager("images/worldmap.strf");
+
+ width = 20;
+ height = 15;
+
+ start_x = 4;
+ start_y = 5;
+
+ tux = new Tux(this);
+
+ leveldot_green = new Surface(datadir + "/images/tiles/worldmap/leveldot_green.png", true);
+ leveldot_red = new Surface(datadir + "/images/tiles/worldmap/leveldot_red.png", true);
+ messagedot = new Surface(datadir + "/images/tiles/worldmap/messagedot.png", true);
+ teleporterdot = new Surface(datadir + "/images/tiles/worldmap/teleporterdot.png", true);
+
+ name = "<no title>";
+ music = "salcon.mod";
+ intro_displayed = false;
+
+ total_stats.reset();
+}
+
+WorldMap::~WorldMap()
+{
+ delete tux;
+ delete tile_manager;
+
+ delete leveldot_green;
+ delete leveldot_red;
+ delete messagedot;
+ delete teleporterdot;
+}
+
+// Don't forget to set map_filename before calling this
+void
+WorldMap::load_map()
+{
+ levels_path = FileSystem::dirname(map_filename);
+
+ try {
+ lisp::Parser parser;
+ std::string filename = get_resource_filename(map_filename);
+ std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
+
+ const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
+ if(!lisp)
+ throw new std::runtime_error("file isn't a supertux-worldmap file.");
+
+ lisp::ListIterator iter(lisp);
+ while(iter.next()) {
+ if(iter.item() == "tilemap") {
+ if(tilemap.size() > 0)
+ throw new std::runtime_error("multiple tilemaps specified");
+
+ const lisp::Lisp* tilemap_lisp = iter.lisp();
+ tilemap_lisp->get("width", width);
+ tilemap_lisp->get("height", height);
+ tilemap_lisp->get_vector("data", tilemap);
+ } else if(iter.item() == "properties") {
+ const lisp::Lisp* props = iter.lisp();
+ props->get("name", name);
+ props->get("music", music);
+ props->get("intro-filename", intro_filename);
+ props->get("start_pos_x", start_x);
+ props->get("start_pos_y", start_y);
+ } else if(iter.item() == "special-tiles") {
+ parse_special_tiles(iter.lisp());
+ } else {
+ std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
+ }
+ }
+
+ delete tux;
+ tux = new Tux(this);
+ } catch(std::exception& e) {
+ std::stringstream msg;
+ msg << "Problem when parsing worldmap '" << map_filename << "': " <<
+ e.what();
+ throw std::runtime_error(msg.str());
+ }
+}
+
+void
+WorldMap::parse_special_tiles(const lisp::Lisp* lisp)
+{
+ lisp::ListIterator iter(lisp);
+ while(iter.next()) {
+ if(iter.item() == "special-tile") {
+ SpecialTile special_tile;
+
+ const lisp::Lisp* lisp = iter.lisp();
+ lisp->get("x", special_tile.pos.x);
+ lisp->get("y", special_tile.pos.y);
+ lisp->get("map-message", special_tile.map_message);
+ special_tile.passive_message = false;
+ lisp->get("passive-message", special_tile.passive_message);
+ special_tile.teleport_dest = Vector(-1,-1);
+ lisp->get("teleport-to-x", special_tile.teleport_dest.x);
+ lisp->get("teleport-to-y", special_tile.teleport_dest.y);
+ special_tile.invisible = false;
+ lisp->get("invisible-tile", special_tile.invisible);
+
+ special_tile.apply_action_north = true;
+ special_tile.apply_action_south = true;
+ special_tile.apply_action_east = true;
+ special_tile.apply_action_west = true;
+
+ std::string apply_direction;
+ lisp->get("apply-to-direction", apply_direction);
+ if(!apply_direction.empty()) {
+ special_tile.apply_action_north = false;
+ special_tile.apply_action_south = false;
+ special_tile.apply_action_east = false;
+ special_tile.apply_action_west = false;
+ if(apply_direction.find("north") != std::string::npos)
+ special_tile.apply_action_north = true;
+ if(apply_direction.find("south") != std::string::npos)
+ special_tile.apply_action_south = true;
+ if(apply_direction.find("east") != std::string::npos)
+ special_tile.apply_action_east = true;
+ if(apply_direction.find("west") != std::string::npos)
+ special_tile.apply_action_west = true;
+ }
+
+ special_tiles.push_back(special_tile);
+ } else if(iter.item() == "level") {
+ Level level;
+
+ lisp::Lisp* level_lisp = iter.lisp();
+ level.solved = false;
+
+ level.north = true;
+ level.east = true;
+ level.south = true;
+ level.west = true;
+
+ level_lisp->get("extro-filename", level.extro_filename);
+ level_lisp->get("next-worldmap", level.next_worldmap);
+
+ level.quit_worldmap = false;
+ level_lisp->get("quit-worldmap", level.quit_worldmap);
+
+ level_lisp->get("name", level.name);
+ level_lisp->get("x", level.pos.x);
+ level_lisp->get("y", level.pos.y);
+
+ level.auto_path = true;
+ level_lisp->get("auto-path", level.auto_path);
+
+ level.vertical_flip = false;
+ level_lisp->get("vertical-flip", level.vertical_flip);
+
+ levels.push_back(level);
+ } else {
+ std::cerr << "Unknown token '" << iter.item() <<
+ "' in worldmap special-tiles list.";
+ }
+ }
+}
+
+void
+WorldMap::get_level_title(Level& level)
+{
+ /** get special_tile's title */
+ level.title = "<no title>";
+
+ try {
+ lisp::Parser parser;
+ std::auto_ptr<lisp::Lisp> root (
+ parser.parse(get_resource_filename(levels_path + level.name)));
+
+ const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
+ if(!level_lisp)
+ return;
+
+ level_lisp->get("name", level.title);
+ } catch(std::exception& e) {
+ std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
+ return;
+ }
+}
+
+void WorldMap::calculate_total_stats()
+{
+ total_stats.reset();
+ for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
+ {
+ if (i->solved)
+ {
+ total_stats += i->statistics;
+ }
+ }
+}
+
+void
+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);
+ }
+}
+