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"
49 #include "player_status.hpp"
50 #include "textscroller.hpp"
52 #include "spawn_point.hpp"
53 #include "file_system.hpp"
54 #include "gui/menu.hpp"
55 #include "gui/mousecursor.hpp"
56 #include "control/joystickkeyboardcontroller.hpp"
57 #include "object/background.hpp"
58 #include "object/tilemap.hpp"
59 #include "scripting/script_interpreter.hpp"
60 #include "exceptions.hpp"
62 Menu* worldmap_menu = 0;
64 static const float TUXSPEED = 200;
65 static const float map_message_TIME = 2.8;
67 namespace WorldMapNS {
69 WorldMap* WorldMap::current_ = NULL;
71 Direction reverse_dir(Direction direction)
90 direction_to_string(Direction direction)
108 string_to_direction(const std::string& directory)
110 if (directory == "west")
112 else if (directory == "east")
114 else if (directory == "north")
116 else if (directory == "south")
122 //---------------------------------------------------------------------------
124 Tux::Tux(WorldMap* worldmap_)
125 : worldmap(worldmap_)
127 tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
132 input_direction = D_NONE;
141 Tux::draw(DrawingContext& context)
143 switch (player_status->bonus) {
145 tux_sprite->set_action(moving ? "large-walking" : "large-stop");
148 tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
151 tux_sprite->set_action(moving ? "small-walking" : "small-stop");
154 msg_debug("Bonus type not handled in worldmap.");
155 tux_sprite->set_action("large-stop");
159 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
166 float x = tile_pos.x * 32;
167 float y = tile_pos.y * 32;
195 input_direction = D_NONE;
200 Tux::set_direction(Direction dir)
202 input_direction = dir;
206 Tux::tryStartWalking()
209 if (input_direction == D_NONE) return;
211 WorldMap::Level* level = worldmap->at_level();
213 // We got a new direction, so lets start walking when possible
215 if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile))
217 tile_pos = next_tile;
219 direction = input_direction;
220 back_direction = reverse_dir(direction);
222 else if (input_direction == back_direction)
225 direction = input_direction;
226 tile_pos = worldmap->get_next_tile(tile_pos, direction);
227 back_direction = reverse_dir(direction);
233 Tux::canWalk(const Tile* tile, Direction dir)
235 return ((tile->getData() & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
236 (tile->getData() & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
237 (tile->getData() & Tile::WORLDMAP_EAST && dir == D_EAST) ||
238 (tile->getData() & Tile::WORLDMAP_WEST && dir == D_WEST));
242 Tux::tryContinueWalking(float elapsed_time)
247 offset += TUXSPEED * elapsed_time;
249 // Do nothing if we have not yet reached the next tile
250 if (offset <= 32) return;
254 // if this is a special_tile with passive_message, display it
255 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
256 if(special_tile && special_tile->passive_message)
258 // direction and the apply_action_ are opposites, since they "see"
259 // directions in a different way
260 if((direction == D_NORTH && special_tile->apply_action_south) ||
261 (direction == D_SOUTH && special_tile->apply_action_north) ||
262 (direction == D_WEST && special_tile->apply_action_east) ||
263 (direction == D_EAST && special_tile->apply_action_west))
265 worldmap->passive_message = special_tile->map_message;
266 worldmap->passive_message_timer.start(map_message_TIME);
270 // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
271 if ((worldmap->at_level()) || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP) || (special_tile && !special_tile->passive_message))
273 if(special_tile && !special_tile->map_message.empty() && !special_tile->passive_message) worldmap->passive_message_timer.start(0);
278 // if user wants to change direction, try changing, else guess the direction in which to walk next
279 const Tile* tile = worldmap->at(tile_pos);
280 if (direction != input_direction)
282 if(canWalk(tile, input_direction))
284 direction = input_direction;
285 back_direction = reverse_dir(direction);
290 Direction dir = D_NONE;
291 if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH) dir = D_NORTH;
292 else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH) dir = D_SOUTH;
293 else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST) dir = D_EAST;
294 else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST) dir = D_WEST;
298 // Should never be reached if tiledata is good
299 msg_warning("Could not determine where to walk next");
305 input_direction = direction;
306 back_direction = reverse_dir(direction);
309 // Walk automatically to the next tile
310 if(direction != D_NONE)
313 if (worldmap->path_ok(direction, tile_pos, &next_tile))
315 tile_pos = next_tile;
319 msg_warning("Tilemap data is buggy");
326 Tux::updateInputDirection()
328 if(main_controller->hold(Controller::UP)) input_direction = D_NORTH;
329 else if(main_controller->hold(Controller::DOWN)) input_direction = D_SOUTH;
330 else if(main_controller->hold(Controller::LEFT)) input_direction = D_WEST;
331 else if(main_controller->hold(Controller::RIGHT)) input_direction = D_EAST;
336 Tux::update(float elapsed_time)
338 updateInputDirection();
339 if (moving) tryContinueWalking(elapsed_time); else tryStartWalking();
342 //---------------------------------------------------------------------------
347 tile_manager = new TileManager("images/worldmap.strf");
352 messagedot = new Surface("images/worldmap/common/messagedot.png");
353 teleporterdot = sprite_manager->create("images/worldmap/common/teleporter.sprite");
356 music = "music/salcon.ogg";
357 intro_displayed = false;
362 WorldMap::~WorldMap()
365 for(SpawnPoints::iterator i = spawn_points.begin();
366 i != spawn_points.end(); ++i) {
369 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
373 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i) {
380 delete teleporterdot;
384 WorldMap::add_object(GameObject* object)
386 TileMap* tilemap = dynamic_cast<TileMap*> (object);
387 if(tilemap != 0 && tilemap->is_solid()) {
391 game_objects.push_back(object);
395 WorldMap::clear_objects()
397 for(GameObjects::iterator i = game_objects.begin();
398 i != game_objects.end(); ++i)
400 game_objects.clear();
406 // Don't forget to set map_filename before calling this
410 levels_path = FileSystem::dirname(map_filename);
414 std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
416 const lisp::Lisp* lisp = root->get_lisp("supertux-level");
418 throw std::runtime_error("file isn't a supertux-level file.");
420 lisp->get("name", name);
422 const lisp::Lisp* sector = lisp->get_lisp("sector");
424 throw std::runtime_error("No sector sepcified in worldmap file.");
427 lisp::ListIterator iter(sector);
429 if(iter.item() == "tilemap") {
430 add_object(new TileMap(*(iter.lisp()), tile_manager));
431 } else if(iter.item() == "background") {
432 add_object(new Background(*(iter.lisp())));
433 } else if(iter.item() == "music") {
434 iter.value()->get(music);
435 } else if(iter.item() == "intro-script") {
436 iter.value()->get(intro_script);
437 } else if(iter.item() == "worldmap-spawnpoint") {
438 SpawnPoint* sp = new SpawnPoint(iter.lisp());
439 spawn_points.push_back(sp);
440 } else if(iter.item() == "level") {
441 parse_level_tile(iter.lisp());
442 } else if(iter.item() == "special-tile") {
443 parse_special_tile(iter.lisp());
444 } else if(iter.item() == "name") {
447 msg_warning("Unknown token '" << iter.item() << "' in worldmap");
451 throw std::runtime_error("No solid tilemap specified");
453 // search for main spawnpoint
454 for(SpawnPoints::iterator i = spawn_points.begin();
455 i != spawn_points.end(); ++i) {
457 if(sp->name == "main") {
459 tux->set_tile_pos(p);
464 } catch(std::exception& e) {
465 std::stringstream msg;
466 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
468 throw std::runtime_error(msg.str());
473 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
475 SpecialTile special_tile;
477 lisp->get("x", special_tile.pos.x);
478 lisp->get("y", special_tile.pos.y);
481 if (lisp->get("sprite", sprite)) {
482 special_tile.sprite = sprite_manager->create(sprite);
484 special_tile.sprite = 0;
487 lisp->get("map-message", special_tile.map_message);
488 special_tile.passive_message = false;
489 lisp->get("passive-message", special_tile.passive_message);
490 special_tile.teleport_dest = Vector(-1,-1);
491 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
492 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
493 special_tile.invisible = false;
494 lisp->get("invisible-tile", special_tile.invisible);
496 special_tile.apply_action_north = true;
497 special_tile.apply_action_south = true;
498 special_tile.apply_action_east = true;
499 special_tile.apply_action_west = true;
501 std::string apply_direction;
502 lisp->get("apply-to-direction", apply_direction);
503 if(!apply_direction.empty()) {
504 special_tile.apply_action_north = false;
505 special_tile.apply_action_south = false;
506 special_tile.apply_action_east = false;
507 special_tile.apply_action_west = false;
508 if(apply_direction.find("north") != std::string::npos)
509 special_tile.apply_action_north = true;
510 if(apply_direction.find("south") != std::string::npos)
511 special_tile.apply_action_south = true;
512 if(apply_direction.find("east") != std::string::npos)
513 special_tile.apply_action_east = true;
514 if(apply_direction.find("west") != std::string::npos)
515 special_tile.apply_action_west = true;
518 special_tiles.push_back(special_tile);
522 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
526 level.solved = false;
533 std::string sprite = "images/worldmap/common/leveldot.sprite";
534 level_lisp->get("sprite", sprite);
535 level.sprite = sprite_manager->create(sprite);
537 level_lisp->get("extro-script", level.extro_script);
538 level_lisp->get("next-worldmap", level.next_worldmap);
540 level.quit_worldmap = false;
541 level_lisp->get("quit-worldmap", level.quit_worldmap);
543 level_lisp->get("name", level.name);
545 if (!PHYSFS_exists((levels_path + level.name).c_str()))
547 // Do we want to bail out instead...? We might get messages from modders
548 // who can't make their levels run because they're too dumb to watch
549 // their terminals...
550 msg_warning("level file '" << level.name
551 << "' does not exist and will not be added to the worldmap");
555 level_lisp->get("x", level.pos.x);
556 level_lisp->get("y", level.pos.y);
558 level.auto_path = true;
559 level_lisp->get("auto-path", level.auto_path);
561 level.vertical_flip = false;
562 level_lisp->get("vertical-flip", level.vertical_flip);
564 levels.push_back(level);
568 WorldMap::get_level_title(Level& level)
570 /** get special_tile's title */
571 level.title = "<no title>";
575 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
577 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
581 level_lisp->get("name", level.title);
582 } catch(std::exception& e) {
583 msg_warning("Problem when reading leveltitle: " << e.what());
588 void WorldMap::calculate_total_stats()
591 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
595 total_stats += i->statistics;
601 WorldMap::on_escape_press()
603 // Show or hide the menu
604 if(!Menu::current()) {
605 Menu::set_current(worldmap_menu);
606 tux->set_direction(D_NONE); // stop tux movement when menu is called
608 Menu::set_current(0);
613 WorldMap::get_input()
615 main_controller->update();
618 while (SDL_PollEvent(&event)) {
620 Menu::current()->event(event);
621 main_controller->process_event(event);
622 if(event.type == SDL_QUIT)
623 throw graceful_shutdown();
628 WorldMap::get_next_tile(Vector pos, Direction direction)
650 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
652 *new_pos = get_next_tile(old_pos, direction);
654 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
655 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
656 { // New position is outsite the tilemap
660 { // Check if the tile allows us to go to new_pos
664 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
665 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
668 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
669 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
672 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
673 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
676 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
677 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
680 assert(!"path_ok() can't work if direction is NONE");
687 WorldMap::finished_level(const std::string& filename)
689 // TODO calculate level from filename?
691 Level* level = at_level();
693 bool old_level_state = level->solved;
694 level->solved = true;
695 level->sprite->set_action("solved");
697 // deal with statistics
698 level->statistics.merge(global_stats);
699 calculate_total_stats();
701 if(savegame_file != "")
702 savegame(savegame_file);
704 if (old_level_state != level->solved && level->auto_path) {
705 // Try to detect the next direction to which we should walk
706 // FIXME: Mostly a hack
707 Direction dir = D_NONE;
709 const Tile* tile = at(tux->get_tile_pos());
711 if (tile->getData() & Tile::WORLDMAP_NORTH
712 && tux->back_direction != D_NORTH)
714 else if (tile->getData() & Tile::WORLDMAP_SOUTH
715 && tux->back_direction != D_SOUTH)
717 else if (tile->getData() & Tile::WORLDMAP_EAST
718 && tux->back_direction != D_EAST)
720 else if (tile->getData() & Tile::WORLDMAP_WEST
721 && tux->back_direction != D_WEST)
725 tux->set_direction(dir);
729 if (level->extro_script != "") {
731 std::auto_ptr<ScriptInterpreter> interpreter
732 (new ScriptInterpreter(levels_path));
733 std::istringstream in(level->extro_script);
734 interpreter->run_script(in, "level-extro-script");
735 add_object(interpreter.release());
736 } catch(std::exception& e) {
737 msg_fatal("Couldn't run level-extro-script:" << e.what());
741 if (!level->next_worldmap.empty()) {
742 // Load given worldmap
743 loadmap(level->next_worldmap);
746 if (level->quit_worldmap)
747 main_loop->exit_screen();
751 WorldMap::update(float delta)
753 Menu* menu = Menu::current();
757 if(menu == worldmap_menu) {
758 switch (worldmap_menu->check())
760 case MNID_RETURNWORLDMAP: // Return to game
761 Menu::set_current(0);
763 case MNID_QUITWORLDMAP: // Quit Worldmap
764 main_loop->exit_screen();
767 } else if(menu == options_menu) {
768 process_options_menu();
774 // update GameObjects
775 for(GameObjects::iterator i = game_objects.begin();
776 i != game_objects.end(); ++i) {
777 GameObject* object = *i;
778 object->update(delta);
781 // remove old GameObjects
782 for(GameObjects::iterator i = game_objects.begin();
783 i != game_objects.end(); ) {
784 GameObject* object = *i;
785 if(!object->is_valid()) {
787 i = game_objects.erase(i);
794 Vector tux_pos = tux->get_pos();
795 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
796 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
798 if (camera_offset.x < 0)
800 if (camera_offset.y < 0)
803 if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
804 camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
805 if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
806 camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
809 bool enter_level = false;
810 if(main_controller->pressed(Controller::ACTION)
811 || main_controller->pressed(Controller::JUMP)
812 || main_controller->pressed(Controller::MENU_SELECT))
814 if(main_controller->pressed(Controller::PAUSE_MENU))
817 if (enter_level && !tux->is_moving())
819 /* Check special tile action */
820 SpecialTile* special_tile = at_special_tile();
823 if (special_tile->teleport_dest != Vector(-1,-1))
825 // TODO: an animation, camera scrolling or a fading would be a nice touch
826 sound_manager->play("sounds/warp.wav");
827 tux->back_direction = D_NONE;
828 tux->set_tile_pos(special_tile->teleport_dest);
833 /* Check level action */
834 Level* level = at_level();
836 msg_warning("No level to enter at: "
837 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y);
841 if (level->pos == tux->get_tile_pos()) {
842 // do a shriking fade to the level
843 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
844 (level->pos.y*32 + 16 + offset.y)), 500);
847 GameSession *session =
848 new GameSession(levels_path + level->name,
849 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
850 main_loop->push_screen(session);
851 } catch(std::exception& e) {
852 msg_fatal("Couldn't load level: " << e.what());
858 // tux->set_direction(input_direction);
863 WorldMap::at(Vector p)
865 return solids->get_tile((int) p.x, (int) p.y);
871 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
873 if (i->pos == tux->get_tile_pos())
880 WorldMap::SpecialTile*
881 WorldMap::at_special_tile()
883 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
885 if (i->pos == tux->get_tile_pos())
893 WorldMap::draw(DrawingContext& context)
895 context.push_transform();
896 context.set_translation(camera_offset);
898 for(GameObjects::iterator i = game_objects.begin();
899 i != game_objects.end(); ++i) {
900 GameObject* object = *i;
901 object->draw(context);
904 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
906 const Level& level = *i;
907 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
910 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
916 i->sprite->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
918 else if (i->teleport_dest != Vector(-1, -1))
919 teleporterdot->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
921 else if (!i->map_message.empty() && !i->passive_message)
922 context.draw_surface(messagedot,
923 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
926 draw_status(context);
927 context.pop_transform();
931 WorldMap::draw_status(DrawingContext& context)
933 context.push_transform();
934 context.set_translation(Vector(0, 0));
936 player_status->draw(context);
938 if (!tux->is_moving())
940 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
942 if (i->pos == tux->get_tile_pos())
947 context.draw_text(white_text, i->title,
948 Vector(SCREEN_WIDTH/2,
949 SCREEN_HEIGHT - white_text->get_height() - 30),
950 CENTER_ALLIGN, LAYER_FOREGROUND1);
952 i->statistics.draw_worldmap_info(context);
956 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
958 if (i->pos == tux->get_tile_pos())
960 /* Display an in-map message in the map, if any as been selected */
961 if(!i->map_message.empty() && !i->passive_message)
962 context.draw_text(gold_text, i->map_message,
963 Vector(SCREEN_WIDTH/2,
964 SCREEN_HEIGHT - white_text->get_height() - 60),
965 CENTER_ALLIGN, LAYER_FOREGROUND1);
970 /* Display a passive message in the map, if needed */
971 if(passive_message_timer.started())
972 context.draw_text(gold_text, passive_message,
973 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
974 CENTER_ALLIGN, LAYER_FOREGROUND1);
976 context.pop_transform();
982 sound_manager->play_music(music);
983 Menu::set_current(NULL);
989 WorldMap::savegame(const std::string& filename)
994 std::string dir = FileSystem::dirname(filename);
995 if(PHYSFS_exists(dir.c_str()) == 0 && PHYSFS_mkdir(dir.c_str()) != 0) {
996 std::ostringstream msg;
997 msg << "Couldn't create directory '" << dir << "' for savegame:"
998 << PHYSFS_getLastError();
999 throw std::runtime_error(msg.str());
1001 if(!PHYSFS_isDirectory(dir.c_str())) {
1002 std::ostringstream msg;
1003 msg << "'" << dir << "' is not a directory.";
1004 throw std::runtime_error(msg.str());
1007 lisp::Writer writer(filename);
1009 int nb_solved_levels = 0, total_levels = 0;
1010 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1015 char nb_solved_levels_str[80], total_levels_str[80];
1016 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1017 sprintf(total_levels_str, "%d", total_levels);
1019 writer.write_comment("Worldmap save file");
1021 writer.start_list("supertux-savegame");
1023 writer.write_int("version", 1);
1024 writer.write_string("title",
1025 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1026 writer.write_string("map", map_filename);
1027 writer.write_bool("intro-displayed", intro_displayed);
1029 writer.start_list("tux");
1031 writer.write_float("x", tux->get_tile_pos().x);
1032 writer.write_float("y", tux->get_tile_pos().y);
1033 writer.write_string("back", direction_to_string(tux->back_direction));
1034 player_status->write(writer);
1035 writer.write_string("back", direction_to_string(tux->back_direction));
1037 writer.end_list("tux");
1039 writer.start_list("levels");
1041 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1045 writer.start_list("level");
1047 writer.write_string("name", i->name);
1048 writer.write_bool("solved", true);
1049 i->statistics.write(writer);
1051 writer.end_list("level");
1055 writer.end_list("levels");
1057 writer.end_list("supertux-savegame");
1061 WorldMap::loadgame(const std::string& filename)
1063 msg_debug("loadgame: " << filename);
1064 savegame_file = filename;
1066 if (PHYSFS_exists(filename.c_str())) // savegame exists
1069 lisp::Parser parser;
1071 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1073 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1075 throw std::runtime_error("File is not a supertux-savegame file.");
1077 /* Get the Map filename and then load it before setting level settings */
1078 std::string cur_map_filename = map_filename;
1079 savegame->get("map", map_filename);
1082 savegame->get("intro-displayed", intro_displayed);
1084 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1088 std::string back_str = "none";
1090 tux_lisp->get("x", p.x);
1091 tux_lisp->get("y", p.y);
1092 tux_lisp->get("back", back_str);
1093 player_status->read(*tux_lisp);
1094 if (player_status->coins < 0) player_status->reset();
1095 tux->back_direction = string_to_direction(back_str);
1096 tux->set_tile_pos(p);
1099 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1101 lisp::ListIterator iter(levels_lisp);
1102 while(iter.next()) {
1103 if(iter.item() == "level") {
1105 bool solved = false;
1107 const lisp::Lisp* level = iter.lisp();
1108 level->get("name", name);
1109 level->get("solved", solved);
1111 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1113 if (name == i->name)
1116 i->sprite->set_action(solved ? "solved" : "default");
1117 i->statistics.parse(*level);
1122 msg_warning("Unknown token '" << iter.item()
1123 << "' in levels block in worldmap");
1127 } catch(std::exception& e) {
1128 msg_warning("Problem loading game '" << filename << "': " << e.what());
1130 player_status->reset();
1136 player_status->reset();
1139 calculate_total_stats();
1143 WorldMap::loadmap(const std::string& filename)
1146 map_filename = filename;
1150 } // namespace WorldMapNS