3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include "gettext.hpp"
33 #include "mainloop.hpp"
34 #include "video/surface.hpp"
35 #include "video/screen.hpp"
36 #include "video/drawing_context.hpp"
37 #include "sprite/sprite_manager.hpp"
38 #include "audio/sound_manager.hpp"
39 #include "lisp/parser.hpp"
40 #include "lisp/lisp.hpp"
41 #include "lisp/list_iterator.hpp"
42 #include "lisp/writer.hpp"
43 #include "game_session.hpp"
45 #include "worldmap.hpp"
46 #include "resources.hpp"
48 #include "player_status.hpp"
49 #include "textscroller.hpp"
51 #include "spawn_point.hpp"
52 #include "file_system.hpp"
53 #include "gui/menu.hpp"
54 #include "gui/mousecursor.hpp"
55 #include "control/joystickkeyboardcontroller.hpp"
56 #include "object/background.hpp"
57 #include "object/tilemap.hpp"
58 #include "scripting/script_interpreter.hpp"
59 #include "exceptions.hpp"
61 Menu* worldmap_menu = 0;
63 static const float TUXSPEED = 200;
64 static const float map_message_TIME = 2.8;
66 namespace WorldMapNS {
68 WorldMap* WorldMap::current_ = NULL;
70 Direction reverse_dir(Direction direction)
89 direction_to_string(Direction direction)
107 string_to_direction(const std::string& directory)
109 if (directory == "west")
111 else if (directory == "east")
113 else if (directory == "north")
115 else if (directory == "south")
121 //---------------------------------------------------------------------------
123 Tux::Tux(WorldMap* worldmap_)
124 : worldmap(worldmap_)
126 tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
131 input_direction = D_NONE;
140 Tux::draw(DrawingContext& context)
142 switch (player_status->bonus) {
144 tux_sprite->set_action(moving ? "large-walking" : "large-stop");
147 tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
150 tux_sprite->set_action(moving ? "small-walking" : "small-stop");
153 msg_debug("Bonus type not handled in worldmap.");
154 tux_sprite->set_action("large-stop");
158 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
165 float x = tile_pos.x * 32;
166 float y = tile_pos.y * 32;
194 input_direction = D_NONE;
199 Tux::set_direction(Direction dir)
201 input_direction = dir;
205 Tux::tryStartWalking()
208 if (input_direction == D_NONE) return;
210 WorldMap::Level* level = worldmap->at_level();
212 // We got a new direction, so lets start walking when possible
214 if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile))
216 tile_pos = next_tile;
218 direction = input_direction;
219 back_direction = reverse_dir(direction);
221 else if (input_direction == back_direction)
224 direction = input_direction;
225 tile_pos = worldmap->get_next_tile(tile_pos, direction);
226 back_direction = reverse_dir(direction);
232 Tux::canWalk(const Tile* tile, Direction dir)
234 return ((tile->getData() & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
235 (tile->getData() & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
236 (tile->getData() & Tile::WORLDMAP_EAST && dir == D_EAST) ||
237 (tile->getData() & Tile::WORLDMAP_WEST && dir == D_WEST));
241 Tux::tryContinueWalking(float elapsed_time)
246 offset += TUXSPEED * elapsed_time;
248 // Do nothing if we have not yet reached the next tile
249 if (offset <= 32) return;
253 // if this is a special_tile with passive_message, display it
254 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
255 if(special_tile && special_tile->passive_message)
257 // direction and the apply_action_ are opposites, since they "see"
258 // directions in a different way
259 if((direction == D_NORTH && special_tile->apply_action_south) ||
260 (direction == D_SOUTH && special_tile->apply_action_north) ||
261 (direction == D_WEST && special_tile->apply_action_east) ||
262 (direction == D_EAST && special_tile->apply_action_west))
264 worldmap->passive_message = special_tile->map_message;
265 worldmap->passive_message_timer.start(map_message_TIME);
269 // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
270 if ((worldmap->at_level()) || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP) || (special_tile && !special_tile->passive_message))
272 if(special_tile && !special_tile->map_message.empty() && !special_tile->passive_message) worldmap->passive_message_timer.start(0);
277 // if user wants to change direction, try changing, else guess the direction in which to walk next
278 const Tile* tile = worldmap->at(tile_pos);
279 if (direction != input_direction)
281 if(canWalk(tile, input_direction))
283 direction = input_direction;
284 back_direction = reverse_dir(direction);
289 Direction dir = D_NONE;
290 if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH) dir = D_NORTH;
291 else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH) dir = D_SOUTH;
292 else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST) dir = D_EAST;
293 else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST) dir = D_WEST;
297 // Should never be reached if tiledata is good
298 msg_warning("Could not determine where to walk next");
304 input_direction = direction;
305 back_direction = reverse_dir(direction);
308 // Walk automatically to the next tile
309 if(direction != D_NONE)
312 if (worldmap->path_ok(direction, tile_pos, &next_tile))
314 tile_pos = next_tile;
318 msg_warning("Tilemap data is buggy");
325 Tux::updateInputDirection()
327 if(main_controller->hold(Controller::UP)) input_direction = D_NORTH;
328 else if(main_controller->hold(Controller::DOWN)) input_direction = D_SOUTH;
329 else if(main_controller->hold(Controller::LEFT)) input_direction = D_WEST;
330 else if(main_controller->hold(Controller::RIGHT)) input_direction = D_EAST;
335 Tux::update(float elapsed_time)
337 updateInputDirection();
338 if (moving) tryContinueWalking(elapsed_time); else tryStartWalking();
341 //---------------------------------------------------------------------------
346 tile_manager = new TileManager("images/worldmap.strf");
351 messagedot = new Surface("images/worldmap/common/messagedot.png");
352 teleporterdot = sprite_manager->create("images/worldmap/common/teleporter.sprite");
355 music = "music/salcon.ogg";
356 intro_displayed = false;
361 WorldMap::~WorldMap()
364 for(SpawnPoints::iterator i = spawn_points.begin();
365 i != spawn_points.end(); ++i) {
368 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
372 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i) {
379 delete teleporterdot;
383 WorldMap::add_object(GameObject* object)
385 TileMap* tilemap = dynamic_cast<TileMap*> (object);
386 if(tilemap != 0 && tilemap->is_solid()) {
390 game_objects.push_back(object);
394 WorldMap::clear_objects()
396 for(GameObjects::iterator i = game_objects.begin();
397 i != game_objects.end(); ++i)
399 game_objects.clear();
405 // Don't forget to set map_filename before calling this
409 levels_path = FileSystem::dirname(map_filename);
413 std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
415 const lisp::Lisp* lisp = root->get_lisp("supertux-level");
417 throw std::runtime_error("file isn't a supertux-level file.");
419 lisp->get("name", name);
421 const lisp::Lisp* sector = lisp->get_lisp("sector");
423 throw std::runtime_error("No sector sepcified in worldmap file.");
426 lisp::ListIterator iter(sector);
428 if(iter.item() == "tilemap") {
429 add_object(new TileMap(*(iter.lisp()), tile_manager));
430 } else if(iter.item() == "background") {
431 add_object(new Background(*(iter.lisp())));
432 } else if(iter.item() == "music") {
433 iter.value()->get(music);
434 } else if(iter.item() == "intro-script") {
435 iter.value()->get(intro_script);
436 } else if(iter.item() == "worldmap-spawnpoint") {
437 SpawnPoint* sp = new SpawnPoint(iter.lisp());
438 spawn_points.push_back(sp);
439 } else if(iter.item() == "level") {
440 parse_level_tile(iter.lisp());
441 } else if(iter.item() == "special-tile") {
442 parse_special_tile(iter.lisp());
444 msg_warning("Unknown token '" << iter.item() << "' in worldmap");
448 throw std::runtime_error("No solid tilemap specified");
450 // search for main spawnpoint
451 for(SpawnPoints::iterator i = spawn_points.begin();
452 i != spawn_points.end(); ++i) {
454 if(sp->name == "main") {
456 tux->set_tile_pos(p);
461 } catch(std::exception& e) {
462 std::stringstream msg;
463 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
465 throw std::runtime_error(msg.str());
470 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
472 SpecialTile special_tile;
474 lisp->get("x", special_tile.pos.x);
475 lisp->get("y", special_tile.pos.y);
478 if (lisp->get("sprite", sprite)) {
479 special_tile.sprite = sprite_manager->create(sprite);
481 special_tile.sprite = 0;
484 lisp->get("map-message", special_tile.map_message);
485 special_tile.passive_message = false;
486 lisp->get("passive-message", special_tile.passive_message);
487 special_tile.teleport_dest = Vector(-1,-1);
488 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
489 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
490 special_tile.invisible = false;
491 lisp->get("invisible-tile", special_tile.invisible);
493 special_tile.apply_action_north = true;
494 special_tile.apply_action_south = true;
495 special_tile.apply_action_east = true;
496 special_tile.apply_action_west = true;
498 std::string apply_direction;
499 lisp->get("apply-to-direction", apply_direction);
500 if(!apply_direction.empty()) {
501 special_tile.apply_action_north = false;
502 special_tile.apply_action_south = false;
503 special_tile.apply_action_east = false;
504 special_tile.apply_action_west = false;
505 if(apply_direction.find("north") != std::string::npos)
506 special_tile.apply_action_north = true;
507 if(apply_direction.find("south") != std::string::npos)
508 special_tile.apply_action_south = true;
509 if(apply_direction.find("east") != std::string::npos)
510 special_tile.apply_action_east = true;
511 if(apply_direction.find("west") != std::string::npos)
512 special_tile.apply_action_west = true;
515 special_tiles.push_back(special_tile);
519 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
523 level.solved = false;
530 std::string sprite = "images/worldmap/common/leveldot.sprite";
531 level_lisp->get("sprite", sprite);
532 level.sprite = sprite_manager->create(sprite);
534 level_lisp->get("extro-script", level.extro_script);
535 level_lisp->get("next-worldmap", level.next_worldmap);
537 level.quit_worldmap = false;
538 level_lisp->get("quit-worldmap", level.quit_worldmap);
540 level_lisp->get("name", level.name);
542 if (!PHYSFS_exists((levels_path + level.name).c_str()))
544 // Do we want to bail out instead...? We might get messages from modders
545 // who can't make their levels run because they're too dumb to watch
546 // their terminals...
547 msg_warning("level file '" << level.name
548 << "' does not exist and will not be added to the worldmap");
552 level_lisp->get("x", level.pos.x);
553 level_lisp->get("y", level.pos.y);
555 level.auto_path = true;
556 level_lisp->get("auto-path", level.auto_path);
558 level.vertical_flip = false;
559 level_lisp->get("vertical-flip", level.vertical_flip);
561 levels.push_back(level);
565 WorldMap::get_level_title(Level& level)
567 /** get special_tile's title */
568 level.title = "<no title>";
572 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
574 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
578 level_lisp->get("name", level.title);
579 } catch(std::exception& e) {
580 msg_warning("Problem when reading leveltitle: " << e.what());
585 void WorldMap::calculate_total_stats()
588 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
592 total_stats += i->statistics;
598 WorldMap::on_escape_press()
600 // Show or hide the menu
601 if(!Menu::current()) {
602 Menu::set_current(worldmap_menu);
603 tux->set_direction(D_NONE); // stop tux movement when menu is called
605 Menu::set_current(0);
610 WorldMap::get_input()
612 main_controller->update();
615 while (SDL_PollEvent(&event)) {
617 Menu::current()->event(event);
618 main_controller->process_event(event);
619 if(event.type == SDL_QUIT)
620 throw graceful_shutdown();
625 WorldMap::get_next_tile(Vector pos, Direction direction)
647 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
649 *new_pos = get_next_tile(old_pos, direction);
651 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
652 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
653 { // New position is outsite the tilemap
657 { // Check if the tile allows us to go to new_pos
661 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
662 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
665 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
666 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
669 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
670 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
673 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
674 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
677 assert(!"path_ok() can't work if direction is NONE");
684 WorldMap::finished_level(const std::string& filename)
686 // TODO calculate level from filename?
688 Level* level = at_level();
690 bool old_level_state = level->solved;
691 level->solved = true;
692 level->sprite->set_action("solved");
694 // deal with statistics
695 level->statistics.merge(global_stats);
696 calculate_total_stats();
698 if(savegame_file != "")
699 savegame(savegame_file);
701 if (old_level_state != level->solved && level->auto_path) {
702 // Try to detect the next direction to which we should walk
703 // FIXME: Mostly a hack
704 Direction dir = D_NONE;
706 const Tile* tile = at(tux->get_tile_pos());
708 if (tile->getData() & Tile::WORLDMAP_NORTH
709 && tux->back_direction != D_NORTH)
711 else if (tile->getData() & Tile::WORLDMAP_SOUTH
712 && tux->back_direction != D_SOUTH)
714 else if (tile->getData() & Tile::WORLDMAP_EAST
715 && tux->back_direction != D_EAST)
717 else if (tile->getData() & Tile::WORLDMAP_WEST
718 && tux->back_direction != D_WEST)
722 tux->set_direction(dir);
728 WorldMap::update(float delta)
730 Menu* menu = Menu::current();
734 if(menu == worldmap_menu) {
735 switch (worldmap_menu->check())
737 case MNID_RETURNWORLDMAP: // Return to game
738 Menu::set_current(0);
740 case MNID_QUITWORLDMAP: // Quit Worldmap
741 main_loop->exit_screen();
744 } else if(menu == options_menu) {
745 process_options_menu();
751 // update GameObjects
752 for(GameObjects::iterator i = game_objects.begin();
753 i != game_objects.end(); ++i) {
754 GameObject* object = *i;
755 object->update(delta);
758 // remove old GameObjects
759 for(GameObjects::iterator i = game_objects.begin();
760 i != game_objects.end(); ) {
761 GameObject* object = *i;
762 if(!object->is_valid()) {
764 i = game_objects.erase(i);
771 Vector tux_pos = tux->get_pos();
772 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
773 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
775 if (camera_offset.x < 0)
777 if (camera_offset.y < 0)
780 if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
781 camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
782 if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
783 camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
786 bool enter_level = false;
787 if(main_controller->pressed(Controller::ACTION)
788 || main_controller->pressed(Controller::JUMP)
789 || main_controller->pressed(Controller::MENU_SELECT))
791 if(main_controller->pressed(Controller::PAUSE_MENU))
794 if (enter_level && !tux->is_moving())
796 /* Check special tile action */
797 SpecialTile* special_tile = at_special_tile();
800 if (special_tile->teleport_dest != Vector(-1,-1))
802 // TODO: an animation, camera scrolling or a fading would be a nice touch
803 sound_manager->play("sounds/warp.wav");
804 tux->back_direction = D_NONE;
805 tux->set_tile_pos(special_tile->teleport_dest);
810 /* Check level action */
811 bool level_finished = true;
812 Level* level = at_level();
814 msg_warning("No level to enter at: "
815 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y);
819 if (level->pos == tux->get_tile_pos())
821 // do a shriking fade to the level
822 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
823 (level->pos.y*32 + 16 + offset.y)), 500);
825 GameSession *session =
826 new GameSession(levels_path + level->name,
827 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
828 main_loop->push_screen(session);
830 /* The porpose of the next checking is that if the player lost
831 the level (in case there is one), don't show anything */
833 if (level->extro_script != "") {
835 std::auto_ptr<ScriptInterpreter> interpreter
836 (new ScriptInterpreter(levels_path));
837 std::istringstream in(level->extro_script);
838 interpreter->run_script(in, "level-extro-script");
839 add_object(interpreter.release());
840 } catch(std::exception& e) {
841 msg_warning("Couldn't run level-extro-script:" << e.what());
845 if (!level->next_worldmap.empty())
847 // Load given worldmap
848 loadmap(level->next_worldmap);
850 if (level->quit_worldmap)
851 main_loop->exit_screen();
856 // tux->set_direction(input_direction);
861 WorldMap::at(Vector p)
863 return solids->get_tile((int) p.x, (int) p.y);
869 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
871 if (i->pos == tux->get_tile_pos())
878 WorldMap::SpecialTile*
879 WorldMap::at_special_tile()
881 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
883 if (i->pos == tux->get_tile_pos())
891 WorldMap::draw(DrawingContext& context)
893 context.push_transform();
894 context.set_translation(camera_offset);
896 for(GameObjects::iterator i = game_objects.begin();
897 i != game_objects.end(); ++i) {
898 GameObject* object = *i;
899 object->draw(context);
902 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
904 const Level& level = *i;
905 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
908 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
914 i->sprite->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
916 else if (i->teleport_dest != Vector(-1, -1))
917 teleporterdot->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
919 else if (!i->map_message.empty() && !i->passive_message)
920 context.draw_surface(messagedot,
921 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
924 draw_status(context);
925 context.pop_transform();
929 WorldMap::draw_status(DrawingContext& context)
931 context.push_transform();
932 context.set_translation(Vector(0, 0));
934 player_status->draw(context);
936 if (!tux->is_moving())
938 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
940 if (i->pos == tux->get_tile_pos())
945 context.draw_text(white_text, i->title,
946 Vector(SCREEN_WIDTH/2,
947 SCREEN_HEIGHT - white_text->get_height() - 30),
948 CENTER_ALLIGN, LAYER_FOREGROUND1);
950 i->statistics.draw_worldmap_info(context);
954 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
956 if (i->pos == tux->get_tile_pos())
958 /* Display an in-map message in the map, if any as been selected */
959 if(!i->map_message.empty() && !i->passive_message)
960 context.draw_text(gold_text, i->map_message,
961 Vector(SCREEN_WIDTH/2,
962 SCREEN_HEIGHT - white_text->get_height() - 60),
963 CENTER_ALLIGN, LAYER_FOREGROUND1);
968 /* Display a passive message in the map, if needed */
969 if(passive_message_timer.started())
970 context.draw_text(gold_text, passive_message,
971 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
972 CENTER_ALLIGN, LAYER_FOREGROUND1);
974 context.pop_transform();
980 sound_manager->play_music(music);
981 Menu::set_current(NULL);
987 WorldMap::savegame(const std::string& filename)
992 std::string dir = FileSystem::dirname(filename);
993 if(PHYSFS_exists(dir.c_str()) == 0 && PHYSFS_mkdir(dir.c_str()) != 0) {
994 std::ostringstream msg;
995 msg << "Couldn't create directory '" << dir << "' for savegame:"
996 << PHYSFS_getLastError();
997 throw std::runtime_error(msg.str());
999 if(!PHYSFS_isDirectory(dir.c_str())) {
1000 std::ostringstream msg;
1001 msg << "'" << dir << "' is not a directory.";
1002 throw std::runtime_error(msg.str());
1005 lisp::Writer writer(filename);
1007 int nb_solved_levels = 0, total_levels = 0;
1008 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1013 char nb_solved_levels_str[80], total_levels_str[80];
1014 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1015 sprintf(total_levels_str, "%d", total_levels);
1017 writer.write_comment("Worldmap save file");
1019 writer.start_list("supertux-savegame");
1021 writer.write_int("version", 1);
1022 writer.write_string("title",
1023 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1024 writer.write_string("map", map_filename);
1025 writer.write_bool("intro-displayed", intro_displayed);
1027 writer.start_list("tux");
1029 writer.write_float("x", tux->get_tile_pos().x);
1030 writer.write_float("y", tux->get_tile_pos().y);
1031 writer.write_string("back", direction_to_string(tux->back_direction));
1032 player_status->write(writer);
1033 writer.write_string("back", direction_to_string(tux->back_direction));
1035 writer.end_list("tux");
1037 writer.start_list("levels");
1039 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1043 writer.start_list("level");
1045 writer.write_string("name", i->name);
1046 writer.write_bool("solved", true);
1047 i->statistics.write(writer);
1049 writer.end_list("level");
1053 writer.end_list("levels");
1055 writer.end_list("supertux-savegame");
1059 WorldMap::loadgame(const std::string& filename)
1061 msg_debug("loadgame: " << filename);
1062 savegame_file = filename;
1064 if (PHYSFS_exists(filename.c_str())) // savegame exists
1067 lisp::Parser parser;
1069 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1071 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1073 throw std::runtime_error("File is not a supertux-savegame file.");
1075 /* Get the Map filename and then load it before setting level settings */
1076 std::string cur_map_filename = map_filename;
1077 savegame->get("map", map_filename);
1080 savegame->get("intro-displayed", intro_displayed);
1082 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1086 std::string back_str = "none";
1088 tux_lisp->get("x", p.x);
1089 tux_lisp->get("y", p.y);
1090 tux_lisp->get("back", back_str);
1091 player_status->read(*tux_lisp);
1092 if (player_status->coins < 0) player_status->reset();
1093 tux->back_direction = string_to_direction(back_str);
1094 tux->set_tile_pos(p);
1097 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1099 lisp::ListIterator iter(levels_lisp);
1100 while(iter.next()) {
1101 if(iter.item() == "level") {
1103 bool solved = false;
1105 const lisp::Lisp* level = iter.lisp();
1106 level->get("name", name);
1107 level->get("solved", solved);
1109 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1111 if (name == i->name)
1114 i->sprite->set_action(solved ? "solved" : "default");
1115 i->statistics.parse(*level);
1120 msg_warning("Unknown token '" << iter.item()
1121 << "' in levels block in worldmap");
1125 } catch(std::exception& e) {
1126 msg_warning("Problem loading game '" << filename << "': " << e.what());
1128 player_status->reset();
1134 player_status->reset();
1137 calculate_total_stats();
1141 WorldMap::loadmap(const std::string& filename)
1144 map_filename = filename;
1148 } // namespace WorldMapNS