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"
56 Menu* worldmap_menu = 0;
58 static const float TUXSPEED = 200;
59 static const float map_message_TIME = 2.8;
61 namespace WorldMapNS {
63 Direction reverse_dir(Direction direction)
82 direction_to_string(Direction direction)
100 string_to_direction(const std::string& directory)
102 if (directory == "west")
104 else if (directory == "east")
106 else if (directory == "north")
108 else if (directory == "south")
114 //---------------------------------------------------------------------------
116 Tux::Tux(WorldMap* worldmap_)
117 : worldmap(worldmap_)
119 tux_sprite = sprite_manager->create("worldmaptux");
124 input_direction = D_NONE;
133 Tux::draw(DrawingContext& context)
135 switch (player_status.bonus) {
137 tux_sprite->set_action("large");
140 tux_sprite->set_action("fire");
143 tux_sprite->set_action("small");
147 std::cerr << "Bonus type not handled in worldmap.\n";
149 tux_sprite->set_action("large");
153 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
160 float x = tile_pos.x * 32;
161 float y = tile_pos.y * 32;
189 input_direction = D_NONE;
194 Tux::set_direction(Direction dir)
196 input_direction = dir;
200 Tux::update(float delta)
203 if(main_controller->pressed(Controller::UP))
204 input_direction = D_NORTH;
205 else if(main_controller->pressed(Controller::DOWN))
206 input_direction = D_SOUTH;
207 else if(main_controller->pressed(Controller::LEFT))
208 input_direction = D_WEST;
209 else if(main_controller->pressed(Controller::RIGHT))
210 input_direction = D_EAST;
214 if (input_direction != D_NONE)
216 WorldMap::Level* level = worldmap->at_level();
218 // We got a new direction, so lets start walking when possible
220 if ((!level || level->solved)
221 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
223 tile_pos = next_tile;
225 direction = input_direction;
226 back_direction = reverse_dir(direction);
228 else if (input_direction == back_direction)
231 direction = input_direction;
232 tile_pos = worldmap->get_next_tile(tile_pos, direction);
233 back_direction = reverse_dir(direction);
240 offset += TUXSPEED * delta;
243 { // We reached the next tile, so we check what to do now
246 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
247 if(special_tile && special_tile->passive_message)
248 { // direction and the apply_action_ are opposites, since they "see"
249 // directions in a different way
250 if((direction == D_NORTH && special_tile->apply_action_south) ||
251 (direction == D_SOUTH && special_tile->apply_action_north) ||
252 (direction == D_WEST && special_tile->apply_action_east) ||
253 (direction == D_EAST && special_tile->apply_action_west))
255 worldmap->passive_message = special_tile->map_message;
256 worldmap->passive_message_timer.start(map_message_TIME);
260 if (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP ||
261 (special_tile && !special_tile->passive_message) ||
262 worldmap->at_level())
264 if(special_tile && !special_tile->map_message.empty() &&
265 !special_tile->passive_message)
266 worldmap->passive_message_timer.start(0);
271 const Tile* tile = worldmap->at(tile_pos);
272 if (direction != input_direction)
274 // Turn to a new direction
275 const Tile* tile = worldmap->at(tile_pos);
277 if((tile->getData() & Tile::WORLDMAP_NORTH
278 && input_direction == D_NORTH) ||
279 (tile->getData() & Tile::WORLDMAP_SOUTH
280 && input_direction == D_SOUTH) ||
281 (tile->getData() & Tile::WORLDMAP_EAST
282 && input_direction == D_EAST) ||
283 (tile->getData() & Tile::WORLDMAP_WEST
284 && input_direction == D_WEST))
285 { // player has changed direction during auto-movement
286 direction = input_direction;
287 back_direction = reverse_dir(direction);
290 { // player has changed to impossible tile
291 back_direction = reverse_dir(direction);
297 Direction dir = D_NONE;
299 if (tile->getData() & Tile::WORLDMAP_NORTH
300 && back_direction != D_NORTH)
302 else if (tile->getData() & Tile::WORLDMAP_SOUTH
303 && back_direction != D_SOUTH)
305 else if (tile->getData() & Tile::WORLDMAP_EAST
306 && back_direction != D_EAST)
308 else if (tile->getData() & Tile::WORLDMAP_WEST
309 && back_direction != D_WEST)
315 input_direction = direction;
316 back_direction = reverse_dir(direction);
320 // Should never be reached if tiledata is good
326 // Walk automatically to the next tile
327 if(direction != D_NONE)
330 if (worldmap->path_ok(direction, tile_pos, &next_tile))
332 tile_pos = next_tile;
336 puts("Tilemap data is buggy");
345 //---------------------------------------------------------------------------
350 tile_manager = new TileManager("images/worldmap.strf");
355 leveldot_green= new Surface("images/tiles/worldmap/leveldot_green.png", true);
356 leveldot_red = new Surface("images/tiles/worldmap/leveldot_red.png", true);
357 messagedot = new Surface("images/tiles/worldmap/messagedot.png", true);
358 teleporterdot = new Surface("images/tiles/worldmap/teleporterdot.png", true);
361 music = "salcon.ogg";
362 intro_displayed = false;
367 WorldMap::~WorldMap()
370 for(SpawnPoints::iterator i = spawn_points.begin();
371 i != spawn_points.end(); ++i) {
377 delete leveldot_green;
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-worldmap");
418 throw std::runtime_error("file isn't a supertux-worldmap file.");
421 lisp::ListIterator iter(lisp);
423 if(iter.item() == "tilemap") {
424 add_object(new TileMap(*(iter.lisp()), tile_manager));
425 } else if(iter.item() == "background") {
426 add_object(new Background(*(iter.lisp())));
427 } else if(iter.item() == "properties") {
428 const lisp::Lisp* props = iter.lisp();
429 props->get("name", name);
430 props->get("music", music);
431 } else if(iter.item() == "intro-script") {
432 iter.value()->get(intro_script);
433 } else if(iter.item() == "spawnpoint") {
434 SpawnPoint* sp = new SpawnPoint(iter.lisp());
435 spawn_points.push_back(sp);
436 } else if(iter.item() == "level") {
437 parse_level_tile(iter.lisp());
438 } else if(iter.item() == "special-tile") {
439 parse_special_tile(iter.lisp());
441 std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
445 throw std::runtime_error("No solid tilemap specified");
447 // search for main spawnpoint
448 for(SpawnPoints::iterator i = spawn_points.begin();
449 i != spawn_points.end(); ++i) {
451 if(sp->name == "main") {
453 tux->set_tile_pos(p);
458 } catch(std::exception& e) {
459 std::stringstream msg;
460 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
462 throw std::runtime_error(msg.str());
467 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
469 SpecialTile special_tile;
471 lisp->get("x", special_tile.pos.x);
472 lisp->get("y", special_tile.pos.y);
473 lisp->get("map-message", special_tile.map_message);
474 special_tile.passive_message = false;
475 lisp->get("passive-message", special_tile.passive_message);
476 special_tile.teleport_dest = Vector(-1,-1);
477 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
478 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
479 special_tile.invisible = false;
480 lisp->get("invisible-tile", special_tile.invisible);
482 special_tile.apply_action_north = true;
483 special_tile.apply_action_south = true;
484 special_tile.apply_action_east = true;
485 special_tile.apply_action_west = true;
487 std::string apply_direction;
488 lisp->get("apply-to-direction", apply_direction);
489 if(!apply_direction.empty()) {
490 special_tile.apply_action_north = false;
491 special_tile.apply_action_south = false;
492 special_tile.apply_action_east = false;
493 special_tile.apply_action_west = false;
494 if(apply_direction.find("north") != std::string::npos)
495 special_tile.apply_action_north = true;
496 if(apply_direction.find("south") != std::string::npos)
497 special_tile.apply_action_south = true;
498 if(apply_direction.find("east") != std::string::npos)
499 special_tile.apply_action_east = true;
500 if(apply_direction.find("west") != std::string::npos)
501 special_tile.apply_action_west = true;
504 special_tiles.push_back(special_tile);
508 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
512 level.solved = false;
519 level_lisp->get("extro-script", level.extro_script);
520 level_lisp->get("next-worldmap", level.next_worldmap);
522 level.quit_worldmap = false;
523 level_lisp->get("quit-worldmap", level.quit_worldmap);
525 level_lisp->get("name", level.name);
526 level_lisp->get("x", level.pos.x);
527 level_lisp->get("y", level.pos.y);
529 level.auto_path = true;
530 level_lisp->get("auto-path", level.auto_path);
532 level.vertical_flip = false;
533 level_lisp->get("vertical-flip", level.vertical_flip);
535 levels.push_back(level);
539 WorldMap::get_level_title(Level& level)
541 /** get special_tile's title */
542 level.title = "<no title>";
546 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
548 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
552 level_lisp->get("name", level.title);
553 } catch(std::exception& e) {
554 std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
559 void WorldMap::calculate_total_stats()
562 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
566 total_stats += i->statistics;
572 WorldMap::on_escape_press()
574 // Show or hide the menu
575 if(!Menu::current()) {
576 Menu::set_current(worldmap_menu);
577 tux->set_direction(D_NONE); // stop tux movement when menu is called
579 Menu::set_current(0);
584 WorldMap::get_input()
586 main_controller->update();
589 while (SDL_PollEvent(&event)) {
591 Menu::current()->event(event);
592 main_controller->process_event(event);
593 if(event.type == SDL_QUIT)
594 throw std::runtime_error("Received window close");
599 WorldMap::get_next_tile(Vector pos, Direction direction)
621 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
623 *new_pos = get_next_tile(old_pos, direction);
625 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
626 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
627 { // New position is outsite the tilemap
631 { // Check if the tile allows us to go to new_pos
635 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
636 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
639 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
640 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
643 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
644 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
647 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
648 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
651 assert(!"path_ok() can't work if direction is NONE");
658 WorldMap::update(float delta)
660 Menu* menu = Menu::current();
664 if(menu == worldmap_menu) {
665 switch (worldmap_menu->check())
667 case MNID_RETURNWORLDMAP: // Return to game
668 Menu::set_current(0);
670 case MNID_QUITWORLDMAP: // Quit Worldmap
674 } else if(menu == options_menu) {
675 process_options_menu();
681 // update GameObjects
682 for(GameObjects::iterator i = game_objects.begin();
683 i != game_objects.end(); ++i) {
684 GameObject* object = *i;
685 object->update(delta);
687 // remove old GameObjects
688 for(GameObjects::iterator i = game_objects.begin();
689 i != game_objects.end(); ) {
690 GameObject* object = *i;
691 if(!object->is_valid()) {
693 i = game_objects.erase(i);
699 bool enter_level = false;
700 if(main_controller->pressed(Controller::ACTION)
701 || main_controller->pressed(Controller::JUMP)
702 || main_controller->pressed(Controller::MENU_SELECT))
704 if(main_controller->pressed(Controller::PAUSE_MENU))
707 if (enter_level && !tux->is_moving())
709 /* Check special tile action */
710 SpecialTile* special_tile = at_special_tile();
713 if (special_tile->teleport_dest != Vector(-1,-1))
715 // TODO: an animation, camera scrolling or a fading would be a nice touch
716 sound_manager->play("sounds/warp.ogg");
717 tux->back_direction = D_NONE;
718 tux->set_tile_pos(special_tile->teleport_dest);
723 /* Check level action */
724 bool level_finished = true;
725 Level* level = at_level();
728 std::cout << "No level to enter at: "
729 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
735 if (level->pos == tux->get_tile_pos())
737 sound_manager->stop_music();
738 PlayerStatus old_player_status = player_status;
740 // do a shriking fade to the level
741 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
742 (level->pos.y*32 + 16 + offset.y)), 500);
743 GameSession session(levels_path + level->name,
744 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
746 switch (session.run())
748 case GameSession::ES_LEVEL_FINISHED:
750 level_finished = true;
751 bool old_level_state = level->solved;
752 level->solved = true;
754 // deal with statistics
755 level->statistics.merge(global_stats);
756 calculate_total_stats();
758 if (old_level_state != level->solved && level->auto_path)
759 { // Try to detect the next direction to which we should walk
760 // FIXME: Mostly a hack
761 Direction dir = D_NONE;
763 const Tile* tile = at(tux->get_tile_pos());
765 if (tile->getData() & Tile::WORLDMAP_NORTH
766 && tux->back_direction != D_NORTH)
768 else if (tile->getData() & Tile::WORLDMAP_SOUTH
769 && tux->back_direction != D_SOUTH)
771 else if (tile->getData() & Tile::WORLDMAP_EAST
772 && tux->back_direction != D_EAST)
774 else if (tile->getData() & Tile::WORLDMAP_WEST
775 && tux->back_direction != D_WEST)
780 tux->set_direction(dir);
786 case GameSession::ES_LEVEL_ABORT:
787 level_finished = false;
788 /* In case the player's abort the level, keep it using the old
789 status. But the minimum lives and no bonus. */
790 player_status.coins = old_player_status.coins;
791 player_status.lives = std::min(old_player_status.lives, player_status.lives);
792 player_status.bonus = NO_BONUS;
795 case GameSession::ES_GAME_OVER:
797 level_finished = false;
798 /* draw an end screen */
799 /* TODO: in the future, this should make a dialog a la SuperMario, asking
800 if the player wants to restart the world map with no score and from
804 DrawingContext context;
805 context.draw_gradient(Color (200,240,220), Color(200,200,220),
808 context.draw_text(blue_text, _("GAMEOVER"),
809 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
811 sprintf(str, _("COINS: %d"), player_status.coins);
812 context.draw_text(gold_text, str,
813 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
816 total_stats.draw_message_info(context, _("Total Statistics"));
818 context.do_drawing();
820 wait_for_event(2.0, 6.0);
823 player_status.reset();
826 case GameSession::ES_NONE:
828 // Should never be reached
832 sound_manager->play_music(std::string("music/") + music);
833 Menu::set_current(0);
834 if (!savegame_file.empty())
835 savegame(savegame_file);
837 /* The porpose of the next checking is that if the player lost
838 the level (in case there is one), don't show anything */
840 if (level->extro_script != "") {
842 std::auto_ptr<ScriptInterpreter> interpreter
843 (new ScriptInterpreter(levels_path));
844 std::istringstream in(level->extro_script);
845 interpreter->run_script(in, "level-extro-script");
846 add_object(interpreter.release());
847 } catch(std::exception& e) {
848 std::cerr << "Couldn't run level-extro-script:" << e.what() << "\n";
852 if (!level->next_worldmap.empty())
854 // Load given worldmap
855 loadmap(level->next_worldmap);
857 if (level->quit_worldmap)
863 // tux->set_direction(input_direction);
868 WorldMap::at(Vector p)
870 return solids->get_tile((int) p.x, (int) p.y);
876 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
878 if (i->pos == tux->get_tile_pos())
885 WorldMap::SpecialTile*
886 WorldMap::at_special_tile()
888 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
890 if (i->pos == tux->get_tile_pos())
898 WorldMap::draw(DrawingContext& context)
900 for(GameObjects::iterator i = game_objects.begin();
901 i != game_objects.end(); ++i) {
902 GameObject* object = *i;
903 object->draw(context);
906 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
909 context.draw_surface(leveldot_green,
910 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
912 context.draw_surface(leveldot_red,
913 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
916 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
921 if (i->teleport_dest != Vector(-1, -1))
922 context.draw_surface(teleporterdot,
923 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
925 else if (!i->map_message.empty() && !i->passive_message)
926 context.draw_surface(messagedot,
927 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
930 draw_status(context);
934 WorldMap::draw_status(DrawingContext& context)
936 context.push_transform();
937 context.set_translation(Vector(0, 0));
939 player_status.draw(context);
941 if (!tux->is_moving())
943 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
945 if (i->pos == tux->get_tile_pos())
950 context.draw_text(white_text, i->title,
951 Vector(SCREEN_WIDTH/2,
952 SCREEN_HEIGHT - white_text->get_height() - 30),
953 CENTER_ALLIGN, LAYER_FOREGROUND1);
955 i->statistics.draw_worldmap_info(context);
959 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
961 if (i->pos == tux->get_tile_pos())
963 /* Display an in-map message in the map, if any as been selected */
964 if(!i->map_message.empty() && !i->passive_message)
965 context.draw_text(gold_text, i->map_message,
966 Vector(SCREEN_WIDTH/2,
967 SCREEN_HEIGHT - white_text->get_height() - 60),
968 CENTER_ALLIGN, LAYER_FOREGROUND1);
973 /* Display a passive message in the map, if needed */
974 if(passive_message_timer.started())
975 context.draw_text(gold_text, passive_message,
976 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
977 CENTER_ALLIGN, LAYER_FOREGROUND1);
979 context.pop_transform();
985 Menu::set_current(0);
989 sound_manager->play_music(std::string("music/") + music);
991 if(!intro_displayed && intro_script != "") {
993 std::auto_ptr<ScriptInterpreter> interpreter
994 (new ScriptInterpreter(levels_path));
995 std::istringstream in(intro_script);
996 interpreter->run_script(in, "worldmap-intro-script");
997 add_object(interpreter.release());
998 } catch(std::exception& e) {
999 std::cerr << "Couldn't execute worldmap-intro-script: "
1000 << e.what() << "\n";
1003 intro_displayed = true;
1006 Uint32 lastticks = SDL_GetTicks();
1007 DrawingContext context;
1009 Uint32 ticks = SDL_GetTicks();
1010 float elapsed_time = float(ticks - lastticks) / 1000;
1011 game_time += elapsed_time;
1014 // 40 fps minimum // TODO use same code as in GameSession here
1015 if(elapsed_time > .025)
1016 elapsed_time = .025;
1018 Vector tux_pos = tux->get_pos();
1019 offset.x = tux_pos.x - SCREEN_WIDTH/2;
1020 offset.y = tux_pos.y - SCREEN_HEIGHT/2;
1027 if (offset.x > solids->get_width()*32 - SCREEN_WIDTH)
1028 offset.x = solids->get_width()*32 - SCREEN_WIDTH;
1029 if (offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
1030 offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
1032 context.push_transform();
1033 context.set_translation(offset);
1035 context.pop_transform();
1037 update(elapsed_time);
1038 sound_manager->update();
1040 if(Menu::current()) {
1041 Menu::current()->draw(context);
1044 context.do_drawing();
1049 WorldMap::savegame(const std::string& filename)
1054 lisp::Writer writer(filename);
1056 int nb_solved_levels = 0, total_levels = 0;
1057 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1062 char nb_solved_levels_str[80], total_levels_str[80];
1063 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1064 sprintf(total_levels_str, "%d", total_levels);
1066 writer.write_comment("Worldmap save file");
1068 writer.start_list("supertux-savegame");
1070 writer.write_int("version", 1);
1071 writer.write_string("title",
1072 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1073 writer.write_string("map", map_filename);
1074 writer.write_bool("intro-displayed", intro_displayed);
1076 writer.start_list("tux");
1078 writer.write_float("x", tux->get_tile_pos().x);
1079 writer.write_float("y", tux->get_tile_pos().y);
1080 writer.write_string("back", direction_to_string(tux->back_direction));
1081 player_status.write(writer);
1082 writer.write_string("back", direction_to_string(tux->back_direction));
1084 writer.end_list("tux");
1086 writer.start_list("levels");
1088 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1092 writer.start_list("level");
1094 writer.write_string("name", i->name);
1095 writer.write_bool("solved", true);
1096 i->statistics.write(writer);
1098 writer.end_list("level");
1102 writer.end_list("levels");
1104 writer.end_list("supertux-savegame");
1108 WorldMap::loadgame(const std::string& filename)
1110 std::cout << "loadgame: " << filename << std::endl;
1111 savegame_file = filename;
1114 lisp::Parser parser;
1115 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1117 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1119 throw std::runtime_error("File is not a supertux-savegame file.");
1121 /* Get the Map filename and then load it before setting level settings */
1122 std::string cur_map_filename = map_filename;
1123 savegame->get("map", map_filename);
1126 savegame->get("intro-displayed", intro_displayed);
1127 savegame->get("lives", player_status.lives);
1128 savegame->get("coins", player_status.coins);
1129 savegame->get("max-score-multiplier", player_status.max_score_multiplier);
1130 if (player_status.lives < 0)
1131 player_status.reset();
1133 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1137 std::string back_str = "none";
1139 tux_lisp->get("x", p.x);
1140 tux_lisp->get("y", p.y);
1141 tux_lisp->get("back", back_str);
1142 player_status.read(*tux_lisp);
1144 tux->back_direction = string_to_direction(back_str);
1145 tux->set_tile_pos(p);
1148 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1150 lisp::ListIterator iter(levels_lisp);
1151 while(iter.next()) {
1152 if(iter.item() == "level") {
1154 bool solved = false;
1156 const lisp::Lisp* level = iter.lisp();
1157 level->get("name", name);
1158 level->get("solved", solved);
1160 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1162 if (name == i->name)
1165 i->statistics.parse(*level);
1170 std::cerr << "Unknown token '" << iter.item()
1171 << "' in levels block in worldmap.\n";
1175 } catch(std::exception& e) {
1176 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1179 player_status.reset();
1182 calculate_total_stats();
1186 WorldMap::loadmap(const std::string& filename)
1189 map_filename = filename;
1193 } // namespace WorldMapNS