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.
30 #include "gettext.hpp"
32 #include "video/surface.hpp"
33 #include "video/screen.hpp"
34 #include "video/drawing_context.hpp"
35 #include "sprite/sprite_manager.hpp"
36 #include "audio/sound_manager.hpp"
37 #include "lisp/parser.hpp"
38 #include "lisp/lisp.hpp"
39 #include "lisp/list_iterator.hpp"
40 #include "lisp/writer.hpp"
41 #include "game_session.hpp"
43 #include "worldmap.hpp"
44 #include "resources.hpp"
46 #include "player_status.hpp"
47 #include "textscroller.hpp"
49 #include "spawn_point.hpp"
50 #include "file_system.hpp"
51 #include "gui/menu.hpp"
52 #include "gui/mousecursor.hpp"
53 #include "control/joystickkeyboardcontroller.hpp"
54 #include "object/background.hpp"
55 #include "object/tilemap.hpp"
56 #include "scripting/script_interpreter.hpp"
57 #include "exceptions.hpp"
59 Menu* worldmap_menu = 0;
61 static const float TUXSPEED = 200;
62 static const float map_message_TIME = 2.8;
64 namespace WorldMapNS {
66 Direction reverse_dir(Direction direction)
85 direction_to_string(Direction direction)
103 string_to_direction(const std::string& directory)
105 if (directory == "west")
107 else if (directory == "east")
109 else if (directory == "north")
111 else if (directory == "south")
117 //---------------------------------------------------------------------------
119 Tux::Tux(WorldMap* worldmap_)
120 : worldmap(worldmap_)
122 tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
127 input_direction = D_NONE;
136 Tux::draw(DrawingContext& context)
138 switch (player_status->bonus) {
140 tux_sprite->set_action(moving ? "large-walking" : "large-stop");
143 tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
146 tux_sprite->set_action(moving ? "small-walking" : "small-stop");
149 msg_debug("Bonus type not handled in worldmap.");
150 tux_sprite->set_action("large-stop");
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 = "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-level");
419 throw std::runtime_error("file isn't a supertux-level file.");
421 lisp->get("name", name);
423 const lisp::Lisp* sector = lisp->get_lisp("sector");
425 throw std::runtime_error("No sector sepcified in worldmap file.");
428 lisp::ListIterator iter(sector);
430 if(iter.item() == "tilemap") {
431 add_object(new TileMap(*(iter.lisp()), tile_manager));
432 } else if(iter.item() == "background") {
433 add_object(new Background(*(iter.lisp())));
434 } else if(iter.item() == "music") {
435 iter.value()->get(music);
436 } else if(iter.item() == "intro-script") {
437 iter.value()->get(intro_script);
438 } else if(iter.item() == "worldmap-spawnpoint") {
439 SpawnPoint* sp = new SpawnPoint(iter.lisp());
440 spawn_points.push_back(sp);
441 } else if(iter.item() == "level") {
442 parse_level_tile(iter.lisp());
443 } else if(iter.item() == "special-tile") {
444 parse_special_tile(iter.lisp());
446 msg_warning("Unknown token '" << iter.item() << "' in worldmap");
450 throw std::runtime_error("No solid tilemap specified");
452 // search for main spawnpoint
453 for(SpawnPoints::iterator i = spawn_points.begin();
454 i != spawn_points.end(); ++i) {
456 if(sp->name == "main") {
458 tux->set_tile_pos(p);
463 } catch(std::exception& e) {
464 std::stringstream msg;
465 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
467 throw std::runtime_error(msg.str());
472 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
474 SpecialTile special_tile;
476 lisp->get("x", special_tile.pos.x);
477 lisp->get("y", special_tile.pos.y);
478 lisp->get("map-message", special_tile.map_message);
479 special_tile.passive_message = false;
480 lisp->get("passive-message", special_tile.passive_message);
481 special_tile.teleport_dest = Vector(-1,-1);
482 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
483 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
484 special_tile.invisible = false;
485 lisp->get("invisible-tile", special_tile.invisible);
487 special_tile.apply_action_north = true;
488 special_tile.apply_action_south = true;
489 special_tile.apply_action_east = true;
490 special_tile.apply_action_west = true;
492 std::string apply_direction;
493 lisp->get("apply-to-direction", apply_direction);
494 if(!apply_direction.empty()) {
495 special_tile.apply_action_north = false;
496 special_tile.apply_action_south = false;
497 special_tile.apply_action_east = false;
498 special_tile.apply_action_west = false;
499 if(apply_direction.find("north") != std::string::npos)
500 special_tile.apply_action_north = true;
501 if(apply_direction.find("south") != std::string::npos)
502 special_tile.apply_action_south = true;
503 if(apply_direction.find("east") != std::string::npos)
504 special_tile.apply_action_east = true;
505 if(apply_direction.find("west") != std::string::npos)
506 special_tile.apply_action_west = true;
509 special_tiles.push_back(special_tile);
513 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
517 level.solved = false;
524 std::string sprite = "images/worldmap/common/leveldot.sprite";
525 level_lisp->get("sprite", sprite);
526 level.sprite = sprite_manager->create(sprite);
528 level_lisp->get("extro-script", level.extro_script);
529 level_lisp->get("next-worldmap", level.next_worldmap);
531 level.quit_worldmap = false;
532 level_lisp->get("quit-worldmap", level.quit_worldmap);
534 level_lisp->get("name", level.name);
536 if (!PHYSFS_exists((levels_path + level.name).c_str()))
538 // Do we want to bail out instead...? We might get messages from modders
539 // who can't make their levels run because they're too dumb to watch
540 // their terminals...
541 msg_warning("level file '" << level.name
542 << "' does not exist and will not be added to the worldmap");
546 level_lisp->get("x", level.pos.x);
547 level_lisp->get("y", level.pos.y);
549 level.auto_path = true;
550 level_lisp->get("auto-path", level.auto_path);
552 level.vertical_flip = false;
553 level_lisp->get("vertical-flip", level.vertical_flip);
555 levels.push_back(level);
559 WorldMap::get_level_title(Level& level)
561 /** get special_tile's title */
562 level.title = "<no title>";
566 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
568 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
572 level_lisp->get("name", level.title);
573 } catch(std::exception& e) {
574 msg_warning("Problem when reading leveltitle: " << e.what());
579 void WorldMap::calculate_total_stats()
582 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
586 total_stats += i->statistics;
592 WorldMap::on_escape_press()
594 // Show or hide the menu
595 if(!Menu::current()) {
596 Menu::set_current(worldmap_menu);
597 tux->set_direction(D_NONE); // stop tux movement when menu is called
599 Menu::set_current(0);
604 WorldMap::get_input()
606 main_controller->update();
609 while (SDL_PollEvent(&event)) {
611 Menu::current()->event(event);
612 main_controller->process_event(event);
613 if(event.type == SDL_QUIT)
614 throw graceful_shutdown();
619 WorldMap::get_next_tile(Vector pos, Direction direction)
641 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
643 *new_pos = get_next_tile(old_pos, direction);
645 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
646 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
647 { // New position is outsite the tilemap
651 { // Check if the tile allows us to go to new_pos
655 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
656 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
659 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
660 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
663 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
664 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
667 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
668 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
671 assert(!"path_ok() can't work if direction is NONE");
678 WorldMap::update(float delta)
680 Menu* menu = Menu::current();
684 if(menu == worldmap_menu) {
685 switch (worldmap_menu->check())
687 case MNID_RETURNWORLDMAP: // Return to game
688 Menu::set_current(0);
690 case MNID_QUITWORLDMAP: // Quit Worldmap
694 } else if(menu == options_menu) {
695 process_options_menu();
701 // update GameObjects
702 for(GameObjects::iterator i = game_objects.begin();
703 i != game_objects.end(); ++i) {
704 GameObject* object = *i;
705 object->update(delta);
707 // remove old GameObjects
708 for(GameObjects::iterator i = game_objects.begin();
709 i != game_objects.end(); ) {
710 GameObject* object = *i;
711 if(!object->is_valid()) {
713 i = game_objects.erase(i);
719 bool enter_level = false;
720 if(main_controller->pressed(Controller::ACTION)
721 || main_controller->pressed(Controller::JUMP)
722 || main_controller->pressed(Controller::MENU_SELECT))
724 if(main_controller->pressed(Controller::PAUSE_MENU))
727 if (enter_level && !tux->is_moving())
729 /* Check special tile action */
730 SpecialTile* special_tile = at_special_tile();
733 if (special_tile->teleport_dest != Vector(-1,-1))
735 // TODO: an animation, camera scrolling or a fading would be a nice touch
736 sound_manager->play("sounds/warp.wav");
737 tux->back_direction = D_NONE;
738 tux->set_tile_pos(special_tile->teleport_dest);
743 /* Check level action */
744 bool level_finished = true;
745 Level* level = at_level();
748 msg_warning("No level to enter at: "
749 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y);
754 if (level->pos == tux->get_tile_pos())
756 sound_manager->stop_music();
757 PlayerStatus old_player_status;
758 old_player_status = *player_status;
760 // do a shriking fade to the level
761 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
762 (level->pos.y*32 + 16 + offset.y)), 500);
763 GameSession session(levels_path + level->name,
764 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
766 switch (session.run())
768 case GameSession::ES_LEVEL_FINISHED:
770 level_finished = true;
771 bool old_level_state = level->solved;
772 level->solved = true;
773 level->sprite->set_action("solved");
775 // deal with statistics
776 level->statistics.merge(global_stats);
777 calculate_total_stats();
779 if (old_level_state != level->solved && level->auto_path)
780 { // Try to detect the next direction to which we should walk
781 // FIXME: Mostly a hack
782 Direction dir = D_NONE;
784 const Tile* tile = at(tux->get_tile_pos());
786 if (tile->getData() & Tile::WORLDMAP_NORTH
787 && tux->back_direction != D_NORTH)
789 else if (tile->getData() & Tile::WORLDMAP_SOUTH
790 && tux->back_direction != D_SOUTH)
792 else if (tile->getData() & Tile::WORLDMAP_EAST
793 && tux->back_direction != D_EAST)
795 else if (tile->getData() & Tile::WORLDMAP_WEST
796 && tux->back_direction != D_WEST)
801 tux->set_direction(dir);
807 case GameSession::ES_LEVEL_ABORT:
808 level_finished = false;
809 /* In case the player's abort the level, keep it using the old
810 status. But the minimum lives and no bonus. */
811 player_status->coins = old_player_status.coins;
812 player_status->lives = std::min(old_player_status.lives, player_status->lives);
813 player_status->bonus = NO_BONUS;
816 case GameSession::ES_GAME_OVER:
818 level_finished = false;
819 /* draw an end screen */
820 /* TODO: in the future, this should make a dialog a la SuperMario, asking
821 if the player wants to restart the world map with no score and from
825 DrawingContext context;
826 context.draw_gradient(Color (200,240,220), Color(200,200,220),
829 context.draw_text(blue_text, _("GAMEOVER"),
830 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
832 sprintf(str, _("COINS: %d"), player_status->coins);
833 context.draw_text(gold_text, str,
834 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
837 total_stats.draw_message_info(context, _("Total Statistics"));
839 context.do_drawing();
841 wait_for_event(2.0, 6.0);
844 player_status->reset();
847 case GameSession::ES_NONE:
849 // Should never be reached
853 sound_manager->play_music(music);
854 Menu::set_current(0);
855 if (!savegame_file.empty())
856 savegame(savegame_file);
858 /* The porpose of the next checking is that if the player lost
859 the level (in case there is one), don't show anything */
861 if (level->extro_script != "") {
863 std::auto_ptr<ScriptInterpreter> interpreter
864 (new ScriptInterpreter(levels_path));
865 std::istringstream in(level->extro_script);
866 interpreter->run_script(in, "level-extro-script");
867 add_object(interpreter.release());
868 } catch(std::exception& e) {
869 msg_warning("Couldn't run level-extro-script:" << e.what());
873 if (!level->next_worldmap.empty())
875 // Load given worldmap
876 loadmap(level->next_worldmap);
878 if (level->quit_worldmap)
884 // tux->set_direction(input_direction);
889 WorldMap::at(Vector p)
891 return solids->get_tile((int) p.x, (int) p.y);
897 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
899 if (i->pos == tux->get_tile_pos())
906 WorldMap::SpecialTile*
907 WorldMap::at_special_tile()
909 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
911 if (i->pos == tux->get_tile_pos())
919 WorldMap::draw(DrawingContext& context)
921 for(GameObjects::iterator i = game_objects.begin();
922 i != game_objects.end(); ++i) {
923 GameObject* object = *i;
924 object->draw(context);
927 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
929 const Level& level = *i;
930 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
933 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
938 if (i->teleport_dest != Vector(-1, -1))
939 context.draw_surface(teleporterdot,
940 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
942 else if (!i->map_message.empty() && !i->passive_message)
943 context.draw_surface(messagedot,
944 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
947 draw_status(context);
951 WorldMap::draw_status(DrawingContext& context)
953 context.push_transform();
954 context.set_translation(Vector(0, 0));
956 player_status->draw(context);
958 if (!tux->is_moving())
960 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
962 if (i->pos == tux->get_tile_pos())
967 context.draw_text(white_text, i->title,
968 Vector(SCREEN_WIDTH/2,
969 SCREEN_HEIGHT - white_text->get_height() - 30),
970 CENTER_ALLIGN, LAYER_FOREGROUND1);
972 i->statistics.draw_worldmap_info(context);
976 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
978 if (i->pos == tux->get_tile_pos())
980 /* Display an in-map message in the map, if any as been selected */
981 if(!i->map_message.empty() && !i->passive_message)
982 context.draw_text(gold_text, i->map_message,
983 Vector(SCREEN_WIDTH/2,
984 SCREEN_HEIGHT - white_text->get_height() - 60),
985 CENTER_ALLIGN, LAYER_FOREGROUND1);
990 /* Display a passive message in the map, if needed */
991 if(passive_message_timer.started())
992 context.draw_text(gold_text, passive_message,
993 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
994 CENTER_ALLIGN, LAYER_FOREGROUND1);
996 context.pop_transform();
1002 Menu::set_current(0);
1006 sound_manager->play_music(music);
1008 if(!intro_displayed && intro_script != "") {
1010 std::auto_ptr<ScriptInterpreter> interpreter
1011 (new ScriptInterpreter(levels_path));
1012 std::istringstream in(intro_script);
1013 interpreter->run_script(in, "worldmap-intro-script");
1014 add_object(interpreter.release());
1015 } catch(std::exception& e) {
1016 msg_warning("Couldn't execute worldmap-intro-script: "
1020 intro_displayed = true;
1023 Uint32 lastticks = SDL_GetTicks();
1024 DrawingContext context;
1026 Uint32 ticks = SDL_GetTicks();
1027 float elapsed_time = float(ticks - lastticks) / 1000;
1028 game_time += elapsed_time;
1031 // 40 fps minimum // TODO use same code as in GameSession here
1032 if(elapsed_time > .025)
1033 elapsed_time = .025;
1035 Vector tux_pos = tux->get_pos();
1036 offset.x = tux_pos.x - SCREEN_WIDTH/2;
1037 offset.y = tux_pos.y - SCREEN_HEIGHT/2;
1044 if (offset.x > solids->get_width()*32 - SCREEN_WIDTH)
1045 offset.x = solids->get_width()*32 - SCREEN_WIDTH;
1046 if (offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
1047 offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
1049 context.push_transform();
1050 context.set_translation(offset);
1052 context.pop_transform();
1054 update(elapsed_time);
1055 sound_manager->update();
1057 if(Menu::current()) {
1058 Menu::current()->draw(context);
1061 context.do_drawing();
1066 WorldMap::savegame(const std::string& filename)
1071 std::string dir = FileSystem::dirname(filename);
1072 if(PHYSFS_exists(dir.c_str()) == 0 && PHYSFS_mkdir(dir.c_str()) != 0) {
1073 std::ostringstream msg;
1074 msg << "Couldn't create directory '" << dir << "' for savegame:"
1075 << PHYSFS_getLastError();
1076 throw std::runtime_error(msg.str());
1078 if(!PHYSFS_isDirectory(dir.c_str())) {
1079 std::ostringstream msg;
1080 msg << "'" << dir << "' is not a directory.";
1081 throw std::runtime_error(msg.str());
1084 lisp::Writer writer(filename);
1086 int nb_solved_levels = 0, total_levels = 0;
1087 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1092 char nb_solved_levels_str[80], total_levels_str[80];
1093 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1094 sprintf(total_levels_str, "%d", total_levels);
1096 writer.write_comment("Worldmap save file");
1098 writer.start_list("supertux-savegame");
1100 writer.write_int("version", 1);
1101 writer.write_string("title",
1102 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1103 writer.write_string("map", map_filename);
1104 writer.write_bool("intro-displayed", intro_displayed);
1106 writer.start_list("tux");
1108 writer.write_float("x", tux->get_tile_pos().x);
1109 writer.write_float("y", tux->get_tile_pos().y);
1110 writer.write_string("back", direction_to_string(tux->back_direction));
1111 player_status->write(writer);
1112 writer.write_string("back", direction_to_string(tux->back_direction));
1114 writer.end_list("tux");
1116 writer.start_list("levels");
1118 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1122 writer.start_list("level");
1124 writer.write_string("name", i->name);
1125 writer.write_bool("solved", true);
1126 i->statistics.write(writer);
1128 writer.end_list("level");
1132 writer.end_list("levels");
1134 writer.end_list("supertux-savegame");
1138 WorldMap::loadgame(const std::string& filename)
1140 msg_debug("loadgame: " << filename);
1141 savegame_file = filename;
1143 if (PHYSFS_exists(filename.c_str())) // savegame exists
1146 lisp::Parser parser;
1148 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1150 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1152 throw std::runtime_error("File is not a supertux-savegame file.");
1154 /* Get the Map filename and then load it before setting level settings */
1155 std::string cur_map_filename = map_filename;
1156 savegame->get("map", map_filename);
1159 savegame->get("intro-displayed", intro_displayed);
1160 savegame->get("lives", player_status->lives);
1161 savegame->get("coins", player_status->coins);
1162 savegame->get("max-score-multiplier", player_status->max_score_multiplier);
1163 if (player_status->lives < 0)
1164 player_status->reset();
1166 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1170 std::string back_str = "none";
1172 tux_lisp->get("x", p.x);
1173 tux_lisp->get("y", p.y);
1174 tux_lisp->get("back", back_str);
1175 player_status->read(*tux_lisp);
1177 tux->back_direction = string_to_direction(back_str);
1178 tux->set_tile_pos(p);
1181 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1183 lisp::ListIterator iter(levels_lisp);
1184 while(iter.next()) {
1185 if(iter.item() == "level") {
1187 bool solved = false;
1189 const lisp::Lisp* level = iter.lisp();
1190 level->get("name", name);
1191 level->get("solved", solved);
1193 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1195 if (name == i->name)
1198 i->sprite->set_action(solved ? "solved" : "default");
1199 i->statistics.parse(*level);
1204 msg_warning("Unknown token '" << iter.item()
1205 << "' in levels block in worldmap");
1209 } catch(std::exception& e) {
1210 msg_warning("Problem loading game '" << filename << "': " << e.what());
1212 player_status->reset();
1218 player_status->reset();
1221 calculate_total_stats();
1225 WorldMap::loadmap(const std::string& filename)
1228 map_filename = filename;
1232 } // namespace WorldMapNS