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"
31 #include "video/surface.hpp"
32 #include "video/screen.hpp"
33 #include "video/drawing_context.hpp"
34 #include "sprite/sprite_manager.hpp"
35 #include "audio/sound_manager.hpp"
36 #include "lisp/parser.hpp"
37 #include "lisp/lisp.hpp"
38 #include "lisp/list_iterator.hpp"
39 #include "lisp/writer.hpp"
40 #include "game_session.hpp"
42 #include "worldmap.hpp"
43 #include "resources.hpp"
45 #include "player_status.hpp"
46 #include "textscroller.hpp"
48 #include "spawn_point.hpp"
49 #include "file_system.hpp"
50 #include "gui/menu.hpp"
51 #include "gui/mousecursor.hpp"
52 #include "control/joystickkeyboardcontroller.hpp"
53 #include "object/background.hpp"
54 #include "object/tilemap.hpp"
55 #include "scripting/script_interpreter.hpp"
56 #include "exceptions.hpp"
58 Menu* worldmap_menu = 0;
60 static const float TUXSPEED = 200;
61 static const float map_message_TIME = 2.8;
63 namespace WorldMapNS {
65 Direction reverse_dir(Direction direction)
84 direction_to_string(Direction direction)
102 string_to_direction(const std::string& directory)
104 if (directory == "west")
106 else if (directory == "east")
108 else if (directory == "north")
110 else if (directory == "south")
116 //---------------------------------------------------------------------------
118 Tux::Tux(WorldMap* worldmap_)
119 : worldmap(worldmap_)
121 tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
126 input_direction = D_NONE;
135 Tux::draw(DrawingContext& context)
137 switch (player_status->bonus) {
139 tux_sprite->set_action("large");
142 tux_sprite->set_action("fire");
145 tux_sprite->set_action("small");
149 std::cerr << "Bonus type not handled in worldmap.\n";
151 tux_sprite->set_action("large");
155 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
162 float x = tile_pos.x * 32;
163 float y = tile_pos.y * 32;
191 input_direction = D_NONE;
196 Tux::set_direction(Direction dir)
198 input_direction = dir;
202 Tux::update(float delta)
205 if(main_controller->pressed(Controller::UP))
206 input_direction = D_NORTH;
207 else if(main_controller->pressed(Controller::DOWN))
208 input_direction = D_SOUTH;
209 else if(main_controller->pressed(Controller::LEFT))
210 input_direction = D_WEST;
211 else if(main_controller->pressed(Controller::RIGHT))
212 input_direction = D_EAST;
216 if (input_direction != D_NONE)
218 WorldMap::Level* level = worldmap->at_level();
220 // We got a new direction, so lets start walking when possible
222 if ((!level || level->solved)
223 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
225 tile_pos = next_tile;
227 direction = input_direction;
228 back_direction = reverse_dir(direction);
230 else if (input_direction == back_direction)
233 direction = input_direction;
234 tile_pos = worldmap->get_next_tile(tile_pos, direction);
235 back_direction = reverse_dir(direction);
242 offset += TUXSPEED * delta;
245 { // We reached the next tile, so we check what to do now
248 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
249 if(special_tile && special_tile->passive_message)
250 { // direction and the apply_action_ are opposites, since they "see"
251 // directions in a different way
252 if((direction == D_NORTH && special_tile->apply_action_south) ||
253 (direction == D_SOUTH && special_tile->apply_action_north) ||
254 (direction == D_WEST && special_tile->apply_action_east) ||
255 (direction == D_EAST && special_tile->apply_action_west))
257 worldmap->passive_message = special_tile->map_message;
258 worldmap->passive_message_timer.start(map_message_TIME);
262 if (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP ||
263 (special_tile && !special_tile->passive_message) ||
264 worldmap->at_level())
266 if(special_tile && !special_tile->map_message.empty() &&
267 !special_tile->passive_message)
268 worldmap->passive_message_timer.start(0);
273 const Tile* tile = worldmap->at(tile_pos);
274 if (direction != input_direction)
276 // Turn to a new direction
277 const Tile* tile = worldmap->at(tile_pos);
279 if((tile->getData() & Tile::WORLDMAP_NORTH
280 && input_direction == D_NORTH) ||
281 (tile->getData() & Tile::WORLDMAP_SOUTH
282 && input_direction == D_SOUTH) ||
283 (tile->getData() & Tile::WORLDMAP_EAST
284 && input_direction == D_EAST) ||
285 (tile->getData() & Tile::WORLDMAP_WEST
286 && input_direction == D_WEST))
287 { // player has changed direction during auto-movement
288 direction = input_direction;
289 back_direction = reverse_dir(direction);
292 { // player has changed to impossible tile
293 back_direction = reverse_dir(direction);
299 Direction dir = D_NONE;
301 if (tile->getData() & Tile::WORLDMAP_NORTH
302 && back_direction != D_NORTH)
304 else if (tile->getData() & Tile::WORLDMAP_SOUTH
305 && back_direction != D_SOUTH)
307 else if (tile->getData() & Tile::WORLDMAP_EAST
308 && back_direction != D_EAST)
310 else if (tile->getData() & Tile::WORLDMAP_WEST
311 && back_direction != D_WEST)
317 input_direction = direction;
318 back_direction = reverse_dir(direction);
322 // Should never be reached if tiledata is good
328 // Walk automatically to the next tile
329 if(direction != D_NONE)
332 if (worldmap->path_ok(direction, tile_pos, &next_tile))
334 tile_pos = next_tile;
338 puts("Tilemap data is buggy");
347 //---------------------------------------------------------------------------
352 tile_manager = new TileManager("images/worldmap.strf");
357 messagedot = new Surface("images/worldmap/common/messagedot.png");
358 teleporterdot = new Surface("images/worldmap/common/teleporterdot.png");
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) {
374 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
382 delete teleporterdot;
386 WorldMap::add_object(GameObject* object)
388 TileMap* tilemap = dynamic_cast<TileMap*> (object);
389 if(tilemap != 0 && tilemap->is_solid()) {
393 game_objects.push_back(object);
397 WorldMap::clear_objects()
399 for(GameObjects::iterator i = game_objects.begin();
400 i != game_objects.end(); ++i)
402 game_objects.clear();
408 // Don't forget to set map_filename before calling this
412 levels_path = FileSystem::dirname(map_filename);
416 std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
418 const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
420 throw std::runtime_error("file isn't a supertux-worldmap file.");
423 lisp::ListIterator iter(lisp);
425 if(iter.item() == "tilemap") {
426 add_object(new TileMap(*(iter.lisp()), tile_manager));
427 } else if(iter.item() == "background") {
428 add_object(new Background(*(iter.lisp())));
429 } else if(iter.item() == "properties") {
430 const lisp::Lisp* props = iter.lisp();
431 props->get("name", name);
432 props->get("music", music);
433 } else if(iter.item() == "intro-script") {
434 iter.value()->get(intro_script);
435 } else if(iter.item() == "spawnpoint") {
436 SpawnPoint* sp = new SpawnPoint(iter.lisp());
437 spawn_points.push_back(sp);
438 } else if(iter.item() == "level") {
439 parse_level_tile(iter.lisp());
440 } else if(iter.item() == "special-tile") {
441 parse_special_tile(iter.lisp());
443 std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
447 throw std::runtime_error("No solid tilemap specified");
449 // search for main spawnpoint
450 for(SpawnPoints::iterator i = spawn_points.begin();
451 i != spawn_points.end(); ++i) {
453 if(sp->name == "main") {
455 tux->set_tile_pos(p);
460 } catch(std::exception& e) {
461 std::stringstream msg;
462 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
464 throw std::runtime_error(msg.str());
469 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
471 SpecialTile special_tile;
473 lisp->get("x", special_tile.pos.x);
474 lisp->get("y", special_tile.pos.y);
475 lisp->get("map-message", special_tile.map_message);
476 special_tile.passive_message = false;
477 lisp->get("passive-message", special_tile.passive_message);
478 special_tile.teleport_dest = Vector(-1,-1);
479 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
480 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
481 special_tile.invisible = false;
482 lisp->get("invisible-tile", special_tile.invisible);
484 special_tile.apply_action_north = true;
485 special_tile.apply_action_south = true;
486 special_tile.apply_action_east = true;
487 special_tile.apply_action_west = true;
489 std::string apply_direction;
490 lisp->get("apply-to-direction", apply_direction);
491 if(!apply_direction.empty()) {
492 special_tile.apply_action_north = false;
493 special_tile.apply_action_south = false;
494 special_tile.apply_action_east = false;
495 special_tile.apply_action_west = false;
496 if(apply_direction.find("north") != std::string::npos)
497 special_tile.apply_action_north = true;
498 if(apply_direction.find("south") != std::string::npos)
499 special_tile.apply_action_south = true;
500 if(apply_direction.find("east") != std::string::npos)
501 special_tile.apply_action_east = true;
502 if(apply_direction.find("west") != std::string::npos)
503 special_tile.apply_action_west = true;
506 special_tiles.push_back(special_tile);
510 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
514 level.solved = false;
521 std::string sprite = "images/worldmap/common/leveldot.sprite";
522 level_lisp->get("sprite", sprite);
523 level.sprite = sprite_manager->create(sprite);
525 level_lisp->get("extro-script", level.extro_script);
526 level_lisp->get("next-worldmap", level.next_worldmap);
528 level.quit_worldmap = false;
529 level_lisp->get("quit-worldmap", level.quit_worldmap);
531 level_lisp->get("name", level.name);
533 if (!PHYSFS_exists((levels_path + level.name).c_str()))
535 // Do we want to bail out instead...? We might get messages from modders
536 // who can't make their levels run because they're too dumb to watch
537 // their terminals...
538 std::cerr << "Error: level file '" << level.name
539 << "' does not exist and will not be added to the worldmap." << std::endl;
543 level_lisp->get("x", level.pos.x);
544 level_lisp->get("y", level.pos.y);
546 level.auto_path = true;
547 level_lisp->get("auto-path", level.auto_path);
549 level.vertical_flip = false;
550 level_lisp->get("vertical-flip", level.vertical_flip);
552 levels.push_back(level);
556 WorldMap::get_level_title(Level& level)
558 /** get special_tile's title */
559 level.title = "<no title>";
563 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
565 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
569 level_lisp->get("name", level.title);
570 } catch(std::exception& e) {
571 std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
576 void WorldMap::calculate_total_stats()
579 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
583 total_stats += i->statistics;
589 WorldMap::on_escape_press()
591 // Show or hide the menu
592 if(!Menu::current()) {
593 Menu::set_current(worldmap_menu);
594 tux->set_direction(D_NONE); // stop tux movement when menu is called
596 Menu::set_current(0);
601 WorldMap::get_input()
603 main_controller->update();
606 while (SDL_PollEvent(&event)) {
608 Menu::current()->event(event);
609 main_controller->process_event(event);
610 if(event.type == SDL_QUIT)
611 throw graceful_shutdown();
616 WorldMap::get_next_tile(Vector pos, Direction direction)
638 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
640 *new_pos = get_next_tile(old_pos, direction);
642 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
643 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
644 { // New position is outsite the tilemap
648 { // Check if the tile allows us to go to new_pos
652 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
653 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
656 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
657 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
660 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
661 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
664 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
665 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
668 assert(!"path_ok() can't work if direction is NONE");
675 WorldMap::update(float delta)
677 Menu* menu = Menu::current();
681 if(menu == worldmap_menu) {
682 switch (worldmap_menu->check())
684 case MNID_RETURNWORLDMAP: // Return to game
685 Menu::set_current(0);
687 case MNID_QUITWORLDMAP: // Quit Worldmap
691 } else if(menu == options_menu) {
692 process_options_menu();
698 // update GameObjects
699 for(GameObjects::iterator i = game_objects.begin();
700 i != game_objects.end(); ++i) {
701 GameObject* object = *i;
702 object->update(delta);
704 // remove old GameObjects
705 for(GameObjects::iterator i = game_objects.begin();
706 i != game_objects.end(); ) {
707 GameObject* object = *i;
708 if(!object->is_valid()) {
710 i = game_objects.erase(i);
716 bool enter_level = false;
717 if(main_controller->pressed(Controller::ACTION)
718 || main_controller->pressed(Controller::JUMP)
719 || main_controller->pressed(Controller::MENU_SELECT))
721 if(main_controller->pressed(Controller::PAUSE_MENU))
724 if (enter_level && !tux->is_moving())
726 /* Check special tile action */
727 SpecialTile* special_tile = at_special_tile();
730 if (special_tile->teleport_dest != Vector(-1,-1))
732 // TODO: an animation, camera scrolling or a fading would be a nice touch
733 sound_manager->play("sounds/warp.wav");
734 tux->back_direction = D_NONE;
735 tux->set_tile_pos(special_tile->teleport_dest);
740 /* Check level action */
741 bool level_finished = true;
742 Level* level = at_level();
745 std::cout << "No level to enter at: "
746 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
752 if (level->pos == tux->get_tile_pos())
754 sound_manager->stop_music();
755 PlayerStatus old_player_status;
756 old_player_status = *player_status;
758 // do a shriking fade to the level
759 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
760 (level->pos.y*32 + 16 + offset.y)), 500);
761 GameSession session(levels_path + level->name,
762 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
764 switch (session.run())
766 case GameSession::ES_LEVEL_FINISHED:
768 level_finished = true;
769 bool old_level_state = level->solved;
770 level->solved = true;
771 level->sprite->set_action("solved");
773 // deal with statistics
774 level->statistics.merge(global_stats);
775 calculate_total_stats();
777 if (old_level_state != level->solved && level->auto_path)
778 { // Try to detect the next direction to which we should walk
779 // FIXME: Mostly a hack
780 Direction dir = D_NONE;
782 const Tile* tile = at(tux->get_tile_pos());
784 if (tile->getData() & Tile::WORLDMAP_NORTH
785 && tux->back_direction != D_NORTH)
787 else if (tile->getData() & Tile::WORLDMAP_SOUTH
788 && tux->back_direction != D_SOUTH)
790 else if (tile->getData() & Tile::WORLDMAP_EAST
791 && tux->back_direction != D_EAST)
793 else if (tile->getData() & Tile::WORLDMAP_WEST
794 && tux->back_direction != D_WEST)
799 tux->set_direction(dir);
805 case GameSession::ES_LEVEL_ABORT:
806 level_finished = false;
807 /* In case the player's abort the level, keep it using the old
808 status. But the minimum lives and no bonus. */
809 player_status->coins = old_player_status.coins;
810 player_status->lives = std::min(old_player_status.lives, player_status->lives);
811 player_status->bonus = NO_BONUS;
814 case GameSession::ES_GAME_OVER:
816 level_finished = false;
817 /* draw an end screen */
818 /* TODO: in the future, this should make a dialog a la SuperMario, asking
819 if the player wants to restart the world map with no score and from
823 DrawingContext context;
824 context.draw_gradient(Color (200,240,220), Color(200,200,220),
827 context.draw_text(blue_text, _("GAMEOVER"),
828 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
830 sprintf(str, _("COINS: %d"), player_status->coins);
831 context.draw_text(gold_text, str,
832 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
835 total_stats.draw_message_info(context, _("Total Statistics"));
837 context.do_drawing();
839 wait_for_event(2.0, 6.0);
842 player_status->reset();
845 case GameSession::ES_NONE:
847 // Should never be reached
851 sound_manager->play_music(std::string("music/") + music);
852 Menu::set_current(0);
853 if (!savegame_file.empty())
854 savegame(savegame_file);
856 /* The porpose of the next checking is that if the player lost
857 the level (in case there is one), don't show anything */
859 if (level->extro_script != "") {
861 std::auto_ptr<ScriptInterpreter> interpreter
862 (new ScriptInterpreter(levels_path));
863 std::istringstream in(level->extro_script);
864 interpreter->run_script(in, "level-extro-script");
865 add_object(interpreter.release());
866 } catch(std::exception& e) {
867 std::cerr << "Couldn't run level-extro-script:" << e.what() << "\n";
871 if (!level->next_worldmap.empty())
873 // Load given worldmap
874 loadmap(level->next_worldmap);
876 if (level->quit_worldmap)
882 // tux->set_direction(input_direction);
887 WorldMap::at(Vector p)
889 return solids->get_tile((int) p.x, (int) p.y);
895 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
897 if (i->pos == tux->get_tile_pos())
904 WorldMap::SpecialTile*
905 WorldMap::at_special_tile()
907 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
909 if (i->pos == tux->get_tile_pos())
917 WorldMap::draw(DrawingContext& context)
919 for(GameObjects::iterator i = game_objects.begin();
920 i != game_objects.end(); ++i) {
921 GameObject* object = *i;
922 object->draw(context);
925 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
927 const Level& level = *i;
928 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
931 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
936 if (i->teleport_dest != Vector(-1, -1))
937 context.draw_surface(teleporterdot,
938 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
940 else if (!i->map_message.empty() && !i->passive_message)
941 context.draw_surface(messagedot,
942 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
945 draw_status(context);
949 WorldMap::draw_status(DrawingContext& context)
951 context.push_transform();
952 context.set_translation(Vector(0, 0));
954 player_status->draw(context);
956 if (!tux->is_moving())
958 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
960 if (i->pos == tux->get_tile_pos())
965 context.draw_text(white_text, i->title,
966 Vector(SCREEN_WIDTH/2,
967 SCREEN_HEIGHT - white_text->get_height() - 30),
968 CENTER_ALLIGN, LAYER_FOREGROUND1);
970 i->statistics.draw_worldmap_info(context);
974 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
976 if (i->pos == tux->get_tile_pos())
978 /* Display an in-map message in the map, if any as been selected */
979 if(!i->map_message.empty() && !i->passive_message)
980 context.draw_text(gold_text, i->map_message,
981 Vector(SCREEN_WIDTH/2,
982 SCREEN_HEIGHT - white_text->get_height() - 60),
983 CENTER_ALLIGN, LAYER_FOREGROUND1);
988 /* Display a passive message in the map, if needed */
989 if(passive_message_timer.started())
990 context.draw_text(gold_text, passive_message,
991 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
992 CENTER_ALLIGN, LAYER_FOREGROUND1);
994 context.pop_transform();
1000 Menu::set_current(0);
1004 sound_manager->play_music(std::string("music/") + music);
1006 if(!intro_displayed && intro_script != "") {
1008 std::auto_ptr<ScriptInterpreter> interpreter
1009 (new ScriptInterpreter(levels_path));
1010 std::istringstream in(intro_script);
1011 interpreter->run_script(in, "worldmap-intro-script");
1012 add_object(interpreter.release());
1013 } catch(std::exception& e) {
1014 std::cerr << "Couldn't execute worldmap-intro-script: "
1015 << e.what() << "\n";
1018 intro_displayed = true;
1021 Uint32 lastticks = SDL_GetTicks();
1022 DrawingContext context;
1024 Uint32 ticks = SDL_GetTicks();
1025 float elapsed_time = float(ticks - lastticks) / 1000;
1026 game_time += elapsed_time;
1029 // 40 fps minimum // TODO use same code as in GameSession here
1030 if(elapsed_time > .025)
1031 elapsed_time = .025;
1033 Vector tux_pos = tux->get_pos();
1034 offset.x = tux_pos.x - SCREEN_WIDTH/2;
1035 offset.y = tux_pos.y - SCREEN_HEIGHT/2;
1042 if (offset.x > solids->get_width()*32 - SCREEN_WIDTH)
1043 offset.x = solids->get_width()*32 - SCREEN_WIDTH;
1044 if (offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
1045 offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
1047 context.push_transform();
1048 context.set_translation(offset);
1050 context.pop_transform();
1052 update(elapsed_time);
1053 sound_manager->update();
1055 if(Menu::current()) {
1056 Menu::current()->draw(context);
1059 context.do_drawing();
1064 WorldMap::savegame(const std::string& filename)
1069 std::string dir = FileSystem::dirname(filename);
1070 if(PHYSFS_exists(dir.c_str()) == 0 && PHYSFS_mkdir(dir.c_str()) != 0) {
1071 std::ostringstream msg;
1072 msg << "Couldn't create directory '" << dir << "' for savegame:"
1073 << PHYSFS_getLastError();
1074 throw std::runtime_error(msg.str());
1076 if(!PHYSFS_isDirectory(dir.c_str())) {
1077 std::ostringstream msg;
1078 msg << "'" << dir << "' is not a directory.";
1079 throw std::runtime_error(msg.str());
1082 lisp::Writer writer(filename);
1084 int nb_solved_levels = 0, total_levels = 0;
1085 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1090 char nb_solved_levels_str[80], total_levels_str[80];
1091 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1092 sprintf(total_levels_str, "%d", total_levels);
1094 writer.write_comment("Worldmap save file");
1096 writer.start_list("supertux-savegame");
1098 writer.write_int("version", 1);
1099 writer.write_string("title",
1100 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1101 writer.write_string("map", map_filename);
1102 writer.write_bool("intro-displayed", intro_displayed);
1104 writer.start_list("tux");
1106 writer.write_float("x", tux->get_tile_pos().x);
1107 writer.write_float("y", tux->get_tile_pos().y);
1108 writer.write_string("back", direction_to_string(tux->back_direction));
1109 player_status->write(writer);
1110 writer.write_string("back", direction_to_string(tux->back_direction));
1112 writer.end_list("tux");
1114 writer.start_list("levels");
1116 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1120 writer.start_list("level");
1122 writer.write_string("name", i->name);
1123 writer.write_bool("solved", true);
1124 i->statistics.write(writer);
1126 writer.end_list("level");
1130 writer.end_list("levels");
1132 writer.end_list("supertux-savegame");
1136 WorldMap::loadgame(const std::string& filename)
1138 std::cout << "loadgame: " << filename << std::endl;
1139 savegame_file = filename;
1141 if (PHYSFS_exists(filename.c_str())) // savegame exists
1144 lisp::Parser parser;
1146 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1148 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1150 throw std::runtime_error("File is not a supertux-savegame file.");
1152 /* Get the Map filename and then load it before setting level settings */
1153 std::string cur_map_filename = map_filename;
1154 savegame->get("map", map_filename);
1157 savegame->get("intro-displayed", intro_displayed);
1158 savegame->get("lives", player_status->lives);
1159 savegame->get("coins", player_status->coins);
1160 savegame->get("max-score-multiplier", player_status->max_score_multiplier);
1161 if (player_status->lives < 0)
1162 player_status->reset();
1164 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1168 std::string back_str = "none";
1170 tux_lisp->get("x", p.x);
1171 tux_lisp->get("y", p.y);
1172 tux_lisp->get("back", back_str);
1173 player_status->read(*tux_lisp);
1175 tux->back_direction = string_to_direction(back_str);
1176 tux->set_tile_pos(p);
1179 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1181 lisp::ListIterator iter(levels_lisp);
1182 while(iter.next()) {
1183 if(iter.item() == "level") {
1185 bool solved = false;
1187 const lisp::Lisp* level = iter.lisp();
1188 level->get("name", name);
1189 level->get("solved", solved);
1191 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1193 if (name == i->name)
1196 i->sprite->set_action(solved ? "solved" : "default");
1197 i->statistics.parse(*level);
1202 std::cerr << "Unknown token '" << iter.item()
1203 << "' in levels block in worldmap.\n";
1207 } catch(std::exception& e) {
1208 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1211 player_status->reset();
1217 player_status->reset();
1220 calculate_total_stats();
1224 WorldMap::loadmap(const std::string& filename)
1227 map_filename = filename;
1231 } // namespace WorldMapNS