3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "gettext.hpp"
30 #include "video/surface.hpp"
31 #include "video/screen.hpp"
32 #include "video/drawing_context.hpp"
33 #include "sprite/sprite_manager.hpp"
34 #include "audio/sound_manager.hpp"
35 #include "lisp/parser.hpp"
36 #include "lisp/lisp.hpp"
37 #include "lisp/list_iterator.hpp"
38 #include "lisp/writer.hpp"
39 #include "game_session.hpp"
41 #include "worldmap.hpp"
42 #include "resources.hpp"
44 #include "player_status.hpp"
45 #include "textscroller.hpp"
47 #include "spawn_point.hpp"
48 #include "file_system.hpp"
49 #include "gui/menu.hpp"
50 #include "gui/mousecursor.hpp"
51 #include "control/joystickkeyboardcontroller.hpp"
52 #include "object/background.hpp"
53 #include "object/tilemap.hpp"
54 #include "scripting/script_interpreter.hpp"
55 #include "exceptions.hpp"
57 Menu* worldmap_menu = 0;
59 static const float TUXSPEED = 200;
60 static const float map_message_TIME = 2.8;
62 namespace WorldMapNS {
64 Direction reverse_dir(Direction direction)
83 direction_to_string(Direction direction)
101 string_to_direction(const std::string& directory)
103 if (directory == "west")
105 else if (directory == "east")
107 else if (directory == "north")
109 else if (directory == "south")
115 //---------------------------------------------------------------------------
117 Tux::Tux(WorldMap* worldmap_)
118 : worldmap(worldmap_)
120 tux_sprite = sprite_manager->create("worldmaptux");
125 input_direction = D_NONE;
134 Tux::draw(DrawingContext& context)
136 switch (player_status->bonus) {
138 tux_sprite->set_action("large");
141 tux_sprite->set_action("fire");
144 tux_sprite->set_action("small");
148 std::cerr << "Bonus type not handled in worldmap.\n";
150 tux_sprite->set_action("large");
154 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
161 float x = tile_pos.x * 32;
162 float y = tile_pos.y * 32;
190 input_direction = D_NONE;
195 Tux::set_direction(Direction dir)
197 input_direction = dir;
201 Tux::update(float delta)
204 if(main_controller->pressed(Controller::UP))
205 input_direction = D_NORTH;
206 else if(main_controller->pressed(Controller::DOWN))
207 input_direction = D_SOUTH;
208 else if(main_controller->pressed(Controller::LEFT))
209 input_direction = D_WEST;
210 else if(main_controller->pressed(Controller::RIGHT))
211 input_direction = D_EAST;
215 if (input_direction != D_NONE)
217 WorldMap::Level* level = worldmap->at_level();
219 // We got a new direction, so lets start walking when possible
221 if ((!level || level->solved)
222 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
224 tile_pos = next_tile;
226 direction = input_direction;
227 back_direction = reverse_dir(direction);
229 else if (input_direction == back_direction)
232 direction = input_direction;
233 tile_pos = worldmap->get_next_tile(tile_pos, direction);
234 back_direction = reverse_dir(direction);
241 offset += TUXSPEED * delta;
244 { // We reached the next tile, so we check what to do now
247 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
248 if(special_tile && special_tile->passive_message)
249 { // direction and the apply_action_ are opposites, since they "see"
250 // directions in a different way
251 if((direction == D_NORTH && special_tile->apply_action_south) ||
252 (direction == D_SOUTH && special_tile->apply_action_north) ||
253 (direction == D_WEST && special_tile->apply_action_east) ||
254 (direction == D_EAST && special_tile->apply_action_west))
256 worldmap->passive_message = special_tile->map_message;
257 worldmap->passive_message_timer.start(map_message_TIME);
261 if (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP ||
262 (special_tile && !special_tile->passive_message) ||
263 worldmap->at_level())
265 if(special_tile && !special_tile->map_message.empty() &&
266 !special_tile->passive_message)
267 worldmap->passive_message_timer.start(0);
272 const Tile* tile = worldmap->at(tile_pos);
273 if (direction != input_direction)
275 // Turn to a new direction
276 const Tile* tile = worldmap->at(tile_pos);
278 if((tile->getData() & Tile::WORLDMAP_NORTH
279 && input_direction == D_NORTH) ||
280 (tile->getData() & Tile::WORLDMAP_SOUTH
281 && input_direction == D_SOUTH) ||
282 (tile->getData() & Tile::WORLDMAP_EAST
283 && input_direction == D_EAST) ||
284 (tile->getData() & Tile::WORLDMAP_WEST
285 && input_direction == D_WEST))
286 { // player has changed direction during auto-movement
287 direction = input_direction;
288 back_direction = reverse_dir(direction);
291 { // player has changed to impossible tile
292 back_direction = reverse_dir(direction);
298 Direction dir = D_NONE;
300 if (tile->getData() & Tile::WORLDMAP_NORTH
301 && back_direction != D_NORTH)
303 else if (tile->getData() & Tile::WORLDMAP_SOUTH
304 && back_direction != D_SOUTH)
306 else if (tile->getData() & Tile::WORLDMAP_EAST
307 && back_direction != D_EAST)
309 else if (tile->getData() & Tile::WORLDMAP_WEST
310 && back_direction != D_WEST)
316 input_direction = direction;
317 back_direction = reverse_dir(direction);
321 // Should never be reached if tiledata is good
327 // Walk automatically to the next tile
328 if(direction != D_NONE)
331 if (worldmap->path_ok(direction, tile_pos, &next_tile))
333 tile_pos = next_tile;
337 puts("Tilemap data is buggy");
346 //---------------------------------------------------------------------------
351 tile_manager = new TileManager("images/worldmap.strf");
356 messagedot = new Surface("images/worldmap/common/messagedot.png");
357 teleporterdot = new Surface("images/worldmap/common/teleporterdot.png");
360 music = "salcon.ogg";
361 intro_displayed = false;
366 WorldMap::~WorldMap()
369 for(SpawnPoints::iterator i = spawn_points.begin();
370 i != spawn_points.end(); ++i) {
373 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
381 delete teleporterdot;
385 WorldMap::add_object(GameObject* object)
387 TileMap* tilemap = dynamic_cast<TileMap*> (object);
388 if(tilemap != 0 && tilemap->is_solid()) {
392 game_objects.push_back(object);
396 WorldMap::clear_objects()
398 for(GameObjects::iterator i = game_objects.begin();
399 i != game_objects.end(); ++i)
401 game_objects.clear();
407 // Don't forget to set map_filename before calling this
411 levels_path = FileSystem::dirname(map_filename);
415 std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
417 const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
419 throw std::runtime_error("file isn't a supertux-worldmap file.");
422 lisp::ListIterator iter(lisp);
424 if(iter.item() == "tilemap") {
425 add_object(new TileMap(*(iter.lisp()), tile_manager));
426 } else if(iter.item() == "background") {
427 add_object(new Background(*(iter.lisp())));
428 } else if(iter.item() == "properties") {
429 const lisp::Lisp* props = iter.lisp();
430 props->get("name", name);
431 props->get("music", music);
432 } else if(iter.item() == "intro-script") {
433 iter.value()->get(intro_script);
434 } else if(iter.item() == "spawnpoint") {
435 SpawnPoint* sp = new SpawnPoint(iter.lisp());
436 spawn_points.push_back(sp);
437 } else if(iter.item() == "level") {
438 parse_level_tile(iter.lisp());
439 } else if(iter.item() == "special-tile") {
440 parse_special_tile(iter.lisp());
442 std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
446 throw std::runtime_error("No solid tilemap specified");
448 // search for main spawnpoint
449 for(SpawnPoints::iterator i = spawn_points.begin();
450 i != spawn_points.end(); ++i) {
452 if(sp->name == "main") {
454 tux->set_tile_pos(p);
459 } catch(std::exception& e) {
460 std::stringstream msg;
461 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
463 throw std::runtime_error(msg.str());
468 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
470 SpecialTile special_tile;
472 lisp->get("x", special_tile.pos.x);
473 lisp->get("y", special_tile.pos.y);
474 lisp->get("map-message", special_tile.map_message);
475 special_tile.passive_message = false;
476 lisp->get("passive-message", special_tile.passive_message);
477 special_tile.teleport_dest = Vector(-1,-1);
478 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
479 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
480 special_tile.invisible = false;
481 lisp->get("invisible-tile", special_tile.invisible);
483 special_tile.apply_action_north = true;
484 special_tile.apply_action_south = true;
485 special_tile.apply_action_east = true;
486 special_tile.apply_action_west = true;
488 std::string apply_direction;
489 lisp->get("apply-to-direction", apply_direction);
490 if(!apply_direction.empty()) {
491 special_tile.apply_action_north = false;
492 special_tile.apply_action_south = false;
493 special_tile.apply_action_east = false;
494 special_tile.apply_action_west = false;
495 if(apply_direction.find("north") != std::string::npos)
496 special_tile.apply_action_north = true;
497 if(apply_direction.find("south") != std::string::npos)
498 special_tile.apply_action_south = true;
499 if(apply_direction.find("east") != std::string::npos)
500 special_tile.apply_action_east = true;
501 if(apply_direction.find("west") != std::string::npos)
502 special_tile.apply_action_west = true;
505 special_tiles.push_back(special_tile);
509 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
513 level.solved = false;
520 std::string sprite = "images/worldmap/common/leveldot.sprite";
521 level_lisp->get("sprite", sprite);
522 level.sprite = sprite_manager->create(sprite);
524 level_lisp->get("extro-script", level.extro_script);
525 level_lisp->get("next-worldmap", level.next_worldmap);
527 level.quit_worldmap = false;
528 level_lisp->get("quit-worldmap", level.quit_worldmap);
530 level_lisp->get("name", level.name);
531 level_lisp->get("x", level.pos.x);
532 level_lisp->get("y", level.pos.y);
534 level.auto_path = true;
535 level_lisp->get("auto-path", level.auto_path);
537 level.vertical_flip = false;
538 level_lisp->get("vertical-flip", level.vertical_flip);
540 levels.push_back(level);
544 WorldMap::get_level_title(Level& level)
546 /** get special_tile's title */
547 level.title = "<no title>";
551 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
553 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
557 level_lisp->get("name", level.title);
558 } catch(std::exception& e) {
559 std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
564 void WorldMap::calculate_total_stats()
567 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
571 total_stats += i->statistics;
577 WorldMap::on_escape_press()
579 // Show or hide the menu
580 if(!Menu::current()) {
581 Menu::set_current(worldmap_menu);
582 tux->set_direction(D_NONE); // stop tux movement when menu is called
584 Menu::set_current(0);
589 WorldMap::get_input()
591 main_controller->update();
594 while (SDL_PollEvent(&event)) {
596 Menu::current()->event(event);
597 main_controller->process_event(event);
598 if(event.type == SDL_QUIT)
599 throw graceful_shutdown();
604 WorldMap::get_next_tile(Vector pos, Direction direction)
626 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
628 *new_pos = get_next_tile(old_pos, direction);
630 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
631 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
632 { // New position is outsite the tilemap
636 { // Check if the tile allows us to go to new_pos
640 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
641 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
644 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
645 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
648 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
649 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
652 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
653 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
656 assert(!"path_ok() can't work if direction is NONE");
663 WorldMap::update(float delta)
665 Menu* menu = Menu::current();
669 if(menu == worldmap_menu) {
670 switch (worldmap_menu->check())
672 case MNID_RETURNWORLDMAP: // Return to game
673 Menu::set_current(0);
675 case MNID_QUITWORLDMAP: // Quit Worldmap
679 } else if(menu == options_menu) {
680 process_options_menu();
686 // update GameObjects
687 for(GameObjects::iterator i = game_objects.begin();
688 i != game_objects.end(); ++i) {
689 GameObject* object = *i;
690 object->update(delta);
692 // remove old GameObjects
693 for(GameObjects::iterator i = game_objects.begin();
694 i != game_objects.end(); ) {
695 GameObject* object = *i;
696 if(!object->is_valid()) {
698 i = game_objects.erase(i);
704 bool enter_level = false;
705 if(main_controller->pressed(Controller::ACTION)
706 || main_controller->pressed(Controller::JUMP)
707 || main_controller->pressed(Controller::MENU_SELECT))
709 if(main_controller->pressed(Controller::PAUSE_MENU))
712 if (enter_level && !tux->is_moving())
714 /* Check special tile action */
715 SpecialTile* special_tile = at_special_tile();
718 if (special_tile->teleport_dest != Vector(-1,-1))
720 // TODO: an animation, camera scrolling or a fading would be a nice touch
721 sound_manager->play("sounds/warp.wav");
722 tux->back_direction = D_NONE;
723 tux->set_tile_pos(special_tile->teleport_dest);
728 /* Check level action */
729 bool level_finished = true;
730 Level* level = at_level();
733 std::cout << "No level to enter at: "
734 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
740 if (level->pos == tux->get_tile_pos())
742 sound_manager->stop_music();
743 PlayerStatus old_player_status;
744 old_player_status = *player_status;
746 // do a shriking fade to the level
747 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
748 (level->pos.y*32 + 16 + offset.y)), 500);
749 GameSession session(levels_path + level->name,
750 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
752 switch (session.run())
754 case GameSession::ES_LEVEL_FINISHED:
756 level_finished = true;
757 bool old_level_state = level->solved;
758 level->solved = true;
759 level->sprite->set_action("solved");
761 // deal with statistics
762 level->statistics.merge(global_stats);
763 calculate_total_stats();
765 if (old_level_state != level->solved && level->auto_path)
766 { // Try to detect the next direction to which we should walk
767 // FIXME: Mostly a hack
768 Direction dir = D_NONE;
770 const Tile* tile = at(tux->get_tile_pos());
772 if (tile->getData() & Tile::WORLDMAP_NORTH
773 && tux->back_direction != D_NORTH)
775 else if (tile->getData() & Tile::WORLDMAP_SOUTH
776 && tux->back_direction != D_SOUTH)
778 else if (tile->getData() & Tile::WORLDMAP_EAST
779 && tux->back_direction != D_EAST)
781 else if (tile->getData() & Tile::WORLDMAP_WEST
782 && tux->back_direction != D_WEST)
787 tux->set_direction(dir);
793 case GameSession::ES_LEVEL_ABORT:
794 level_finished = false;
795 /* In case the player's abort the level, keep it using the old
796 status. But the minimum lives and no bonus. */
797 player_status->coins = old_player_status.coins;
798 player_status->lives = std::min(old_player_status.lives, player_status->lives);
799 player_status->bonus = NO_BONUS;
802 case GameSession::ES_GAME_OVER:
804 level_finished = false;
805 /* draw an end screen */
806 /* TODO: in the future, this should make a dialog a la SuperMario, asking
807 if the player wants to restart the world map with no score and from
811 DrawingContext context;
812 context.draw_gradient(Color (200,240,220), Color(200,200,220),
815 context.draw_text(blue_text, _("GAMEOVER"),
816 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
818 sprintf(str, _("COINS: %d"), player_status->coins);
819 context.draw_text(gold_text, str,
820 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
823 total_stats.draw_message_info(context, _("Total Statistics"));
825 context.do_drawing();
827 wait_for_event(2.0, 6.0);
830 player_status->reset();
833 case GameSession::ES_NONE:
835 // Should never be reached
839 sound_manager->play_music(std::string("music/") + music);
840 Menu::set_current(0);
841 if (!savegame_file.empty())
842 savegame(savegame_file);
844 /* The porpose of the next checking is that if the player lost
845 the level (in case there is one), don't show anything */
847 if (level->extro_script != "") {
849 std::auto_ptr<ScriptInterpreter> interpreter
850 (new ScriptInterpreter(levels_path));
851 std::istringstream in(level->extro_script);
852 interpreter->run_script(in, "level-extro-script");
853 add_object(interpreter.release());
854 } catch(std::exception& e) {
855 std::cerr << "Couldn't run level-extro-script:" << e.what() << "\n";
859 if (!level->next_worldmap.empty())
861 // Load given worldmap
862 loadmap(level->next_worldmap);
864 if (level->quit_worldmap)
870 // tux->set_direction(input_direction);
875 WorldMap::at(Vector p)
877 return solids->get_tile((int) p.x, (int) p.y);
883 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
885 if (i->pos == tux->get_tile_pos())
892 WorldMap::SpecialTile*
893 WorldMap::at_special_tile()
895 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
897 if (i->pos == tux->get_tile_pos())
905 WorldMap::draw(DrawingContext& context)
907 for(GameObjects::iterator i = game_objects.begin();
908 i != game_objects.end(); ++i) {
909 GameObject* object = *i;
910 object->draw(context);
913 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
915 const Level& level = *i;
916 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
919 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
924 if (i->teleport_dest != Vector(-1, -1))
925 context.draw_surface(teleporterdot,
926 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
928 else if (!i->map_message.empty() && !i->passive_message)
929 context.draw_surface(messagedot,
930 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
933 draw_status(context);
937 WorldMap::draw_status(DrawingContext& context)
939 context.push_transform();
940 context.set_translation(Vector(0, 0));
942 player_status->draw(context);
944 if (!tux->is_moving())
946 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
948 if (i->pos == tux->get_tile_pos())
953 context.draw_text(white_text, i->title,
954 Vector(SCREEN_WIDTH/2,
955 SCREEN_HEIGHT - white_text->get_height() - 30),
956 CENTER_ALLIGN, LAYER_FOREGROUND1);
958 i->statistics.draw_worldmap_info(context);
962 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
964 if (i->pos == tux->get_tile_pos())
966 /* Display an in-map message in the map, if any as been selected */
967 if(!i->map_message.empty() && !i->passive_message)
968 context.draw_text(gold_text, i->map_message,
969 Vector(SCREEN_WIDTH/2,
970 SCREEN_HEIGHT - white_text->get_height() - 60),
971 CENTER_ALLIGN, LAYER_FOREGROUND1);
976 /* Display a passive message in the map, if needed */
977 if(passive_message_timer.started())
978 context.draw_text(gold_text, passive_message,
979 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
980 CENTER_ALLIGN, LAYER_FOREGROUND1);
982 context.pop_transform();
988 Menu::set_current(0);
992 sound_manager->play_music(std::string("music/") + music);
994 if(!intro_displayed && intro_script != "") {
996 std::auto_ptr<ScriptInterpreter> interpreter
997 (new ScriptInterpreter(levels_path));
998 std::istringstream in(intro_script);
999 interpreter->run_script(in, "worldmap-intro-script");
1000 add_object(interpreter.release());
1001 } catch(std::exception& e) {
1002 std::cerr << "Couldn't execute worldmap-intro-script: "
1003 << e.what() << "\n";
1006 intro_displayed = true;
1009 Uint32 lastticks = SDL_GetTicks();
1010 DrawingContext context;
1012 Uint32 ticks = SDL_GetTicks();
1013 float elapsed_time = float(ticks - lastticks) / 1000;
1014 game_time += elapsed_time;
1017 // 40 fps minimum // TODO use same code as in GameSession here
1018 if(elapsed_time > .025)
1019 elapsed_time = .025;
1021 Vector tux_pos = tux->get_pos();
1022 offset.x = tux_pos.x - SCREEN_WIDTH/2;
1023 offset.y = tux_pos.y - SCREEN_HEIGHT/2;
1030 if (offset.x > solids->get_width()*32 - SCREEN_WIDTH)
1031 offset.x = solids->get_width()*32 - SCREEN_WIDTH;
1032 if (offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
1033 offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
1035 context.push_transform();
1036 context.set_translation(offset);
1038 context.pop_transform();
1040 update(elapsed_time);
1041 sound_manager->update();
1043 if(Menu::current()) {
1044 Menu::current()->draw(context);
1047 context.do_drawing();
1052 WorldMap::savegame(const std::string& filename)
1057 lisp::Writer writer(filename);
1059 int nb_solved_levels = 0, total_levels = 0;
1060 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1065 char nb_solved_levels_str[80], total_levels_str[80];
1066 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1067 sprintf(total_levels_str, "%d", total_levels);
1069 writer.write_comment("Worldmap save file");
1071 writer.start_list("supertux-savegame");
1073 writer.write_int("version", 1);
1074 writer.write_string("title",
1075 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1076 writer.write_string("map", map_filename);
1077 writer.write_bool("intro-displayed", intro_displayed);
1079 writer.start_list("tux");
1081 writer.write_float("x", tux->get_tile_pos().x);
1082 writer.write_float("y", tux->get_tile_pos().y);
1083 writer.write_string("back", direction_to_string(tux->back_direction));
1084 player_status->write(writer);
1085 writer.write_string("back", direction_to_string(tux->back_direction));
1087 writer.end_list("tux");
1089 writer.start_list("levels");
1091 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1095 writer.start_list("level");
1097 writer.write_string("name", i->name);
1098 writer.write_bool("solved", true);
1099 i->statistics.write(writer);
1101 writer.end_list("level");
1105 writer.end_list("levels");
1107 writer.end_list("supertux-savegame");
1111 WorldMap::loadgame(const std::string& filename)
1113 std::cout << "loadgame: " << filename << std::endl;
1114 savegame_file = filename;
1117 lisp::Parser parser;
1118 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1120 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1122 throw std::runtime_error("File is not a supertux-savegame file.");
1124 /* Get the Map filename and then load it before setting level settings */
1125 std::string cur_map_filename = map_filename;
1126 savegame->get("map", map_filename);
1129 savegame->get("intro-displayed", intro_displayed);
1130 savegame->get("lives", player_status->lives);
1131 savegame->get("coins", player_status->coins);
1132 savegame->get("max-score-multiplier", player_status->max_score_multiplier);
1133 if (player_status->lives < 0)
1134 player_status->reset();
1136 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1140 std::string back_str = "none";
1142 tux_lisp->get("x", p.x);
1143 tux_lisp->get("y", p.y);
1144 tux_lisp->get("back", back_str);
1145 player_status->read(*tux_lisp);
1147 tux->back_direction = string_to_direction(back_str);
1148 tux->set_tile_pos(p);
1151 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1153 lisp::ListIterator iter(levels_lisp);
1154 while(iter.next()) {
1155 if(iter.item() == "level") {
1157 bool solved = false;
1159 const lisp::Lisp* level = iter.lisp();
1160 level->get("name", name);
1161 level->get("solved", solved);
1163 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1165 if (name == i->name)
1168 i->sprite->set_action(solved ? "solved" : "default");
1169 i->statistics.parse(*level);
1174 std::cerr << "Unknown token '" << iter.item()
1175 << "' in levels block in worldmap.\n";
1179 } catch(std::exception& e) {
1180 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1183 player_status->reset();
1186 calculate_total_stats();
1190 WorldMap::loadmap(const std::string& filename)
1193 map_filename = filename;
1197 } // namespace WorldMapNS