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("worldmaptux");
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);
532 level_lisp->get("x", level.pos.x);
533 level_lisp->get("y", level.pos.y);
535 level.auto_path = true;
536 level_lisp->get("auto-path", level.auto_path);
538 level.vertical_flip = false;
539 level_lisp->get("vertical-flip", level.vertical_flip);
541 levels.push_back(level);
545 WorldMap::get_level_title(Level& level)
547 /** get special_tile's title */
548 level.title = "<no title>";
552 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
554 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
558 level_lisp->get("name", level.title);
559 } catch(std::exception& e) {
560 std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
565 void WorldMap::calculate_total_stats()
568 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
572 total_stats += i->statistics;
578 WorldMap::on_escape_press()
580 // Show or hide the menu
581 if(!Menu::current()) {
582 Menu::set_current(worldmap_menu);
583 tux->set_direction(D_NONE); // stop tux movement when menu is called
585 Menu::set_current(0);
590 WorldMap::get_input()
592 main_controller->update();
595 while (SDL_PollEvent(&event)) {
597 Menu::current()->event(event);
598 main_controller->process_event(event);
599 if(event.type == SDL_QUIT)
600 throw graceful_shutdown();
605 WorldMap::get_next_tile(Vector pos, Direction direction)
627 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
629 *new_pos = get_next_tile(old_pos, direction);
631 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
632 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
633 { // New position is outsite the tilemap
637 { // Check if the tile allows us to go to new_pos
641 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
642 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
645 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
646 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
649 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
650 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
653 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
654 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
657 assert(!"path_ok() can't work if direction is NONE");
664 WorldMap::update(float delta)
666 Menu* menu = Menu::current();
670 if(menu == worldmap_menu) {
671 switch (worldmap_menu->check())
673 case MNID_RETURNWORLDMAP: // Return to game
674 Menu::set_current(0);
676 case MNID_QUITWORLDMAP: // Quit Worldmap
680 } else if(menu == options_menu) {
681 process_options_menu();
687 // update GameObjects
688 for(GameObjects::iterator i = game_objects.begin();
689 i != game_objects.end(); ++i) {
690 GameObject* object = *i;
691 object->update(delta);
693 // remove old GameObjects
694 for(GameObjects::iterator i = game_objects.begin();
695 i != game_objects.end(); ) {
696 GameObject* object = *i;
697 if(!object->is_valid()) {
699 i = game_objects.erase(i);
705 bool enter_level = false;
706 if(main_controller->pressed(Controller::ACTION)
707 || main_controller->pressed(Controller::JUMP)
708 || main_controller->pressed(Controller::MENU_SELECT))
710 if(main_controller->pressed(Controller::PAUSE_MENU))
713 if (enter_level && !tux->is_moving())
715 /* Check special tile action */
716 SpecialTile* special_tile = at_special_tile();
719 if (special_tile->teleport_dest != Vector(-1,-1))
721 // TODO: an animation, camera scrolling or a fading would be a nice touch
722 sound_manager->play("sounds/warp.wav");
723 tux->back_direction = D_NONE;
724 tux->set_tile_pos(special_tile->teleport_dest);
729 /* Check level action */
730 bool level_finished = true;
731 Level* level = at_level();
734 std::cout << "No level to enter at: "
735 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
741 if (level->pos == tux->get_tile_pos())
743 sound_manager->stop_music();
744 PlayerStatus old_player_status;
745 old_player_status = *player_status;
747 // do a shriking fade to the level
748 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
749 (level->pos.y*32 + 16 + offset.y)), 500);
750 GameSession session(levels_path + level->name,
751 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
753 switch (session.run())
755 case GameSession::ES_LEVEL_FINISHED:
757 level_finished = true;
758 bool old_level_state = level->solved;
759 level->solved = true;
760 level->sprite->set_action("solved");
762 // deal with statistics
763 level->statistics.merge(global_stats);
764 calculate_total_stats();
766 if (old_level_state != level->solved && level->auto_path)
767 { // Try to detect the next direction to which we should walk
768 // FIXME: Mostly a hack
769 Direction dir = D_NONE;
771 const Tile* tile = at(tux->get_tile_pos());
773 if (tile->getData() & Tile::WORLDMAP_NORTH
774 && tux->back_direction != D_NORTH)
776 else if (tile->getData() & Tile::WORLDMAP_SOUTH
777 && tux->back_direction != D_SOUTH)
779 else if (tile->getData() & Tile::WORLDMAP_EAST
780 && tux->back_direction != D_EAST)
782 else if (tile->getData() & Tile::WORLDMAP_WEST
783 && tux->back_direction != D_WEST)
788 tux->set_direction(dir);
794 case GameSession::ES_LEVEL_ABORT:
795 level_finished = false;
796 /* In case the player's abort the level, keep it using the old
797 status. But the minimum lives and no bonus. */
798 player_status->coins = old_player_status.coins;
799 player_status->lives = std::min(old_player_status.lives, player_status->lives);
800 player_status->bonus = NO_BONUS;
803 case GameSession::ES_GAME_OVER:
805 level_finished = false;
806 /* draw an end screen */
807 /* TODO: in the future, this should make a dialog a la SuperMario, asking
808 if the player wants to restart the world map with no score and from
812 DrawingContext context;
813 context.draw_gradient(Color (200,240,220), Color(200,200,220),
816 context.draw_text(blue_text, _("GAMEOVER"),
817 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
819 sprintf(str, _("COINS: %d"), player_status->coins);
820 context.draw_text(gold_text, str,
821 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
824 total_stats.draw_message_info(context, _("Total Statistics"));
826 context.do_drawing();
828 wait_for_event(2.0, 6.0);
831 player_status->reset();
834 case GameSession::ES_NONE:
836 // Should never be reached
840 sound_manager->play_music(std::string("music/") + music);
841 Menu::set_current(0);
842 if (!savegame_file.empty())
843 savegame(savegame_file);
845 /* The porpose of the next checking is that if the player lost
846 the level (in case there is one), don't show anything */
848 if (level->extro_script != "") {
850 std::auto_ptr<ScriptInterpreter> interpreter
851 (new ScriptInterpreter(levels_path));
852 std::istringstream in(level->extro_script);
853 interpreter->run_script(in, "level-extro-script");
854 add_object(interpreter.release());
855 } catch(std::exception& e) {
856 std::cerr << "Couldn't run level-extro-script:" << e.what() << "\n";
860 if (!level->next_worldmap.empty())
862 // Load given worldmap
863 loadmap(level->next_worldmap);
865 if (level->quit_worldmap)
871 // tux->set_direction(input_direction);
876 WorldMap::at(Vector p)
878 return solids->get_tile((int) p.x, (int) p.y);
884 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
886 if (i->pos == tux->get_tile_pos())
893 WorldMap::SpecialTile*
894 WorldMap::at_special_tile()
896 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
898 if (i->pos == tux->get_tile_pos())
906 WorldMap::draw(DrawingContext& context)
908 for(GameObjects::iterator i = game_objects.begin();
909 i != game_objects.end(); ++i) {
910 GameObject* object = *i;
911 object->draw(context);
914 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
916 const Level& level = *i;
917 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
920 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
925 if (i->teleport_dest != Vector(-1, -1))
926 context.draw_surface(teleporterdot,
927 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
929 else if (!i->map_message.empty() && !i->passive_message)
930 context.draw_surface(messagedot,
931 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
934 draw_status(context);
938 WorldMap::draw_status(DrawingContext& context)
940 context.push_transform();
941 context.set_translation(Vector(0, 0));
943 player_status->draw(context);
945 if (!tux->is_moving())
947 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
949 if (i->pos == tux->get_tile_pos())
954 context.draw_text(white_text, i->title,
955 Vector(SCREEN_WIDTH/2,
956 SCREEN_HEIGHT - white_text->get_height() - 30),
957 CENTER_ALLIGN, LAYER_FOREGROUND1);
959 i->statistics.draw_worldmap_info(context);
963 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
965 if (i->pos == tux->get_tile_pos())
967 /* Display an in-map message in the map, if any as been selected */
968 if(!i->map_message.empty() && !i->passive_message)
969 context.draw_text(gold_text, i->map_message,
970 Vector(SCREEN_WIDTH/2,
971 SCREEN_HEIGHT - white_text->get_height() - 60),
972 CENTER_ALLIGN, LAYER_FOREGROUND1);
977 /* Display a passive message in the map, if needed */
978 if(passive_message_timer.started())
979 context.draw_text(gold_text, passive_message,
980 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
981 CENTER_ALLIGN, LAYER_FOREGROUND1);
983 context.pop_transform();
989 Menu::set_current(0);
993 sound_manager->play_music(std::string("music/") + music);
995 if(!intro_displayed && intro_script != "") {
997 std::auto_ptr<ScriptInterpreter> interpreter
998 (new ScriptInterpreter(levels_path));
999 std::istringstream in(intro_script);
1000 interpreter->run_script(in, "worldmap-intro-script");
1001 add_object(interpreter.release());
1002 } catch(std::exception& e) {
1003 std::cerr << "Couldn't execute worldmap-intro-script: "
1004 << e.what() << "\n";
1007 intro_displayed = true;
1010 Uint32 lastticks = SDL_GetTicks();
1011 DrawingContext context;
1013 Uint32 ticks = SDL_GetTicks();
1014 float elapsed_time = float(ticks - lastticks) / 1000;
1015 game_time += elapsed_time;
1018 // 40 fps minimum // TODO use same code as in GameSession here
1019 if(elapsed_time > .025)
1020 elapsed_time = .025;
1022 Vector tux_pos = tux->get_pos();
1023 offset.x = tux_pos.x - SCREEN_WIDTH/2;
1024 offset.y = tux_pos.y - SCREEN_HEIGHT/2;
1031 if (offset.x > solids->get_width()*32 - SCREEN_WIDTH)
1032 offset.x = solids->get_width()*32 - SCREEN_WIDTH;
1033 if (offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
1034 offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
1036 context.push_transform();
1037 context.set_translation(offset);
1039 context.pop_transform();
1041 update(elapsed_time);
1042 sound_manager->update();
1044 if(Menu::current()) {
1045 Menu::current()->draw(context);
1048 context.do_drawing();
1053 WorldMap::savegame(const std::string& filename)
1058 lisp::Writer writer(filename);
1060 int nb_solved_levels = 0, total_levels = 0;
1061 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1066 char nb_solved_levels_str[80], total_levels_str[80];
1067 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1068 sprintf(total_levels_str, "%d", total_levels);
1070 writer.write_comment("Worldmap save file");
1072 writer.start_list("supertux-savegame");
1074 writer.write_int("version", 1);
1075 writer.write_string("title",
1076 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1077 writer.write_string("map", map_filename);
1078 writer.write_bool("intro-displayed", intro_displayed);
1080 writer.start_list("tux");
1082 writer.write_float("x", tux->get_tile_pos().x);
1083 writer.write_float("y", tux->get_tile_pos().y);
1084 writer.write_string("back", direction_to_string(tux->back_direction));
1085 player_status->write(writer);
1086 writer.write_string("back", direction_to_string(tux->back_direction));
1088 writer.end_list("tux");
1090 writer.start_list("levels");
1092 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1096 writer.start_list("level");
1098 writer.write_string("name", i->name);
1099 writer.write_bool("solved", true);
1100 i->statistics.write(writer);
1102 writer.end_list("level");
1106 writer.end_list("levels");
1108 writer.end_list("supertux-savegame");
1112 WorldMap::loadgame(const std::string& filename)
1114 std::cout << "loadgame: " << filename << std::endl;
1115 savegame_file = filename;
1117 if (PHYSFS_exists(filename.c_str())) // savegame exists
1120 lisp::Parser parser;
1122 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1124 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1126 throw std::runtime_error("File is not a supertux-savegame file.");
1128 /* Get the Map filename and then load it before setting level settings */
1129 std::string cur_map_filename = map_filename;
1130 savegame->get("map", map_filename);
1133 savegame->get("intro-displayed", intro_displayed);
1134 savegame->get("lives", player_status->lives);
1135 savegame->get("coins", player_status->coins);
1136 savegame->get("max-score-multiplier", player_status->max_score_multiplier);
1137 if (player_status->lives < 0)
1138 player_status->reset();
1140 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1144 std::string back_str = "none";
1146 tux_lisp->get("x", p.x);
1147 tux_lisp->get("y", p.y);
1148 tux_lisp->get("back", back_str);
1149 player_status->read(*tux_lisp);
1151 tux->back_direction = string_to_direction(back_str);
1152 tux->set_tile_pos(p);
1155 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1157 lisp::ListIterator iter(levels_lisp);
1158 while(iter.next()) {
1159 if(iter.item() == "level") {
1161 bool solved = false;
1163 const lisp::Lisp* level = iter.lisp();
1164 level->get("name", name);
1165 level->get("solved", solved);
1167 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1169 if (name == i->name)
1172 i->sprite->set_action(solved ? "solved" : "default");
1173 i->statistics.parse(*level);
1178 std::cerr << "Unknown token '" << iter.item()
1179 << "' in levels block in worldmap.\n";
1183 } catch(std::exception& e) {
1184 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1187 player_status->reset();
1193 player_status->reset();
1196 calculate_total_stats();
1200 WorldMap::loadmap(const std::string& filename)
1203 map_filename = filename;
1207 } // namespace WorldMapNS