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 "app/globals.h"
30 #include "app/gettext.h"
31 #include "app/setup.h"
32 #include "video/surface.h"
33 #include "video/screen.h"
34 #include "video/drawing_context.h"
35 #include "special/frame_rate.h"
36 #include "sprite/sprite_manager.h"
37 #include "audio/sound_manager.h"
38 #include "lisp/parser.h"
39 #include "lisp/lisp.h"
40 #include "lisp/list_iterator.h"
41 #include "lisp/writer.h"
42 #include "game_session.h"
45 #include "resources.h"
47 #include "player_status.h"
48 #include "textscroller.h"
50 #include "control/joystickkeyboardcontroller.h"
52 Menu* worldmap_menu = 0;
54 static const float TUXSPEED = 200;
55 static const float map_message_TIME = 2.8;
57 namespace WorldMapNS {
59 Direction reverse_dir(Direction direction)
78 direction_to_string(Direction direction)
96 string_to_direction(const std::string& directory)
98 if (directory == "west")
100 else if (directory == "east")
102 else if (directory == "north")
104 else if (directory == "south")
110 //---------------------------------------------------------------------------
112 Tux::Tux(WorldMap* worldmap_)
113 : worldmap(worldmap_)
115 tux_sprite = sprite_manager->create("worldmaptux");
119 tile_pos.x = worldmap->get_start_x();
120 tile_pos.y = worldmap->get_start_y();
122 input_direction = D_NONE;
131 Tux::draw(DrawingContext& context)
133 switch (player_status.bonus) {
135 tux_sprite->set_action("large");
138 tux_sprite->set_action("fire");
141 tux_sprite->set_action("small");
145 std::cerr << "Bonus type not handled in worldmap.\n";
147 tux_sprite->set_action("large");
151 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
158 float x = tile_pos.x * 32;
159 float y = tile_pos.y * 32;
179 return Vector((int)x, (int)y);
187 input_direction = D_NONE;
192 Tux::set_direction(Direction dir)
194 input_direction = dir;
198 Tux::action(float delta)
201 if(main_controller->pressed(Controller::UP))
202 input_direction = D_NORTH;
203 else if(main_controller->pressed(Controller::DOWN))
204 input_direction = D_SOUTH;
205 else if(main_controller->pressed(Controller::LEFT))
206 input_direction = D_WEST;
207 else if(main_controller->pressed(Controller::RIGHT))
208 input_direction = D_EAST;
212 if (input_direction != D_NONE)
214 WorldMap::Level* level = worldmap->at_level();
216 // We got a new direction, so lets start walking when possible
218 if ((!level || level->solved)
219 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
221 tile_pos = next_tile;
223 direction = input_direction;
224 back_direction = reverse_dir(direction);
226 else if (input_direction == back_direction)
229 direction = input_direction;
230 tile_pos = worldmap->get_next_tile(tile_pos, direction);
231 back_direction = reverse_dir(direction);
238 offset += TUXSPEED * delta;
241 { // We reached the next tile, so we check what to do now
244 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
245 if(special_tile && special_tile->passive_message)
246 { // direction and the apply_action_ are opposites, since they "see"
247 // directions in a different way
248 if((direction == D_NORTH && special_tile->apply_action_south) ||
249 (direction == D_SOUTH && special_tile->apply_action_north) ||
250 (direction == D_WEST && special_tile->apply_action_east) ||
251 (direction == D_EAST && special_tile->apply_action_west))
253 worldmap->passive_message = special_tile->map_message;
254 worldmap->passive_message_timer.start(map_message_TIME);
258 if (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP ||
259 (special_tile && !special_tile->passive_message) ||
260 worldmap->at_level())
262 if(special_tile && !special_tile->map_message.empty() &&
263 !special_tile->passive_message)
264 worldmap->passive_message_timer.start(0);
269 const Tile* tile = worldmap->at(tile_pos);
270 if (direction != input_direction)
272 // Turn to a new direction
273 const Tile* tile = worldmap->at(tile_pos);
275 if((tile->getData() & Tile::WORLDMAP_NORTH
276 && input_direction == D_NORTH) ||
277 (tile->getData() & Tile::WORLDMAP_SOUTH
278 && input_direction == D_SOUTH) ||
279 (tile->getData() & Tile::WORLDMAP_EAST
280 && input_direction == D_EAST) ||
281 (tile->getData() & Tile::WORLDMAP_WEST
282 && input_direction == D_WEST))
283 { // player has changed direction during auto-movement
284 direction = input_direction;
285 back_direction = reverse_dir(direction);
288 { // player has changed to impossible tile
289 back_direction = reverse_dir(direction);
295 Direction dir = D_NONE;
297 if (tile->getData() & Tile::WORLDMAP_NORTH
298 && back_direction != D_NORTH)
300 else if (tile->getData() & Tile::WORLDMAP_SOUTH
301 && back_direction != D_SOUTH)
303 else if (tile->getData() & Tile::WORLDMAP_EAST
304 && back_direction != D_EAST)
306 else if (tile->getData() & Tile::WORLDMAP_WEST
307 && back_direction != D_WEST)
313 input_direction = direction;
314 back_direction = reverse_dir(direction);
318 // Should never be reached if tiledata is good
324 // Walk automatically to the next tile
325 if(direction != D_NONE)
328 if (worldmap->path_ok(direction, tile_pos, &next_tile))
330 tile_pos = next_tile;
334 puts("Tilemap data is buggy");
343 //---------------------------------------------------------------------------
347 tile_manager = new TileManager("images/worldmap/antarctica.stwt");
357 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
358 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
359 messagedot = new Surface(datadir + "/images/worldmap/messagedot.png", true);
360 teleporterdot = new Surface(datadir + "/images/worldmap/teleporterdot.png", true);
363 music = "salcon.mod";
364 intro_displayed = false;
369 WorldMap::~WorldMap()
374 delete leveldot_green;
377 delete teleporterdot;
380 // Don't forget to set map_filename before calling this
384 levels_path = FileSystem::dirname(map_filename);
388 std::string filename = get_resource_filename(map_filename);
389 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
391 const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
393 throw new std::runtime_error("file isn't a supertux-worldmap file.");
395 lisp::ListIterator iter(lisp);
397 if(iter.item() == "tilemap") {
398 if(tilemap.size() > 0)
399 throw new std::runtime_error("multiple tilemaps specified");
401 const lisp::Lisp* tilemap_lisp = iter.lisp();
402 tilemap_lisp->get("width", width);
403 tilemap_lisp->get("height", height);
404 tilemap_lisp->get_vector("data", tilemap);
405 } else if(iter.item() == "properties") {
406 const lisp::Lisp* props = iter.lisp();
407 props->get("name", name);
408 props->get("music", music);
409 props->get("intro-filename", intro_filename);
410 props->get("start_pos_x", start_x);
411 props->get("start_pos_y", start_y);
412 } else if(iter.item() == "special-tiles") {
413 parse_special_tiles(iter.lisp());
415 std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
421 } catch(std::exception& e) {
422 std::stringstream msg;
423 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
425 throw std::runtime_error(msg.str());
430 WorldMap::parse_special_tiles(const lisp::Lisp* lisp)
432 lisp::ListIterator iter(lisp);
434 if(iter.item() == "special-tile") {
435 SpecialTile special_tile;
437 const lisp::Lisp* lisp = iter.lisp();
438 lisp->get("x", special_tile.pos.x);
439 lisp->get("y", special_tile.pos.y);
440 lisp->get("map-message", special_tile.map_message);
441 special_tile.passive_message = false;
442 lisp->get("passive-message", special_tile.passive_message);
443 special_tile.teleport_dest = Vector(-1,-1);
444 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
445 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
446 special_tile.invisible = false;
447 lisp->get("invisible-tile", special_tile.invisible);
449 special_tile.apply_action_north = true;
450 special_tile.apply_action_south = true;
451 special_tile.apply_action_east = true;
452 special_tile.apply_action_west = true;
454 std::string apply_direction;
455 lisp->get("apply-to-direction", apply_direction);
456 if(!apply_direction.empty()) {
457 special_tile.apply_action_north = false;
458 special_tile.apply_action_south = false;
459 special_tile.apply_action_east = false;
460 special_tile.apply_action_west = false;
461 if(apply_direction.find("north") != std::string::npos)
462 special_tile.apply_action_north = true;
463 if(apply_direction.find("south") != std::string::npos)
464 special_tile.apply_action_south = true;
465 if(apply_direction.find("east") != std::string::npos)
466 special_tile.apply_action_east = true;
467 if(apply_direction.find("west") != std::string::npos)
468 special_tile.apply_action_west = true;
471 special_tiles.push_back(special_tile);
472 } else if(iter.item() == "level") {
475 lisp::Lisp* level_lisp = iter.lisp();
476 level.solved = false;
483 level_lisp->get("extro-filename", level.extro_filename);
484 level_lisp->get("next-worldmap", level.next_worldmap);
486 level.quit_worldmap = false;
487 level_lisp->get("quit-worldmap", level.quit_worldmap);
489 level_lisp->get("name", level.name);
490 level_lisp->get("x", level.pos.x);
491 level_lisp->get("y", level.pos.y);
493 level.auto_path = true;
494 level_lisp->get("auto-path", level.auto_path);
496 level.vertical_flip = false;
497 level_lisp->get("vertical-flip", level.vertical_flip);
499 levels.push_back(level);
501 std::cerr << "Unknown token '" << iter.item() <<
502 "' in worldmap special-tiles list.";
508 WorldMap::get_level_title(Level& level)
510 /** get special_tile's title */
511 level.title = "<no title>";
515 std::auto_ptr<lisp::Lisp> root (
516 parser.parse(get_resource_filename(levels_path + level.name)));
518 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
522 level_lisp->get("name", level.title);
523 } catch(std::exception& e) {
524 std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
529 void WorldMap::calculate_total_stats()
532 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
536 total_stats += i->statistics;
542 WorldMap::on_escape_press()
544 // Show or hide the menu
545 if(!Menu::current()) {
546 Menu::set_current(worldmap_menu);
547 tux->set_direction(D_NONE); // stop tux movement when menu is called
549 Menu::set_current(0);
554 WorldMap::get_input()
556 main_controller->update();
559 while (SDL_PollEvent(&event)) {
561 Menu::current()->event(event);
562 main_controller->process_event(event);
563 if(event.type == SDL_QUIT)
564 throw std::runtime_error("Received window close");
569 WorldMap::get_next_tile(Vector pos, Direction direction)
591 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
593 *new_pos = get_next_tile(old_pos, direction);
595 if (!(new_pos->x >= 0 && new_pos->x < width
596 && new_pos->y >= 0 && new_pos->y < height))
597 { // New position is outsite the tilemap
601 { // Check if the tile allows us to go to new_pos
605 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
606 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
609 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
610 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
613 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
614 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
617 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
618 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
621 assert(!"path_ok() can't work if direction is NONE");
628 WorldMap::update(float delta)
630 Menu* menu = Menu::current();
634 if(menu == worldmap_menu) {
635 switch (worldmap_menu->check())
637 case MNID_RETURNWORLDMAP: // Return to game
638 Menu::set_current(0);
640 case MNID_QUITWORLDMAP: // Quit Worldmap
644 } else if(menu == options_menu) {
645 process_options_menu();
651 bool enter_level = false;
652 if(main_controller->pressed(Controller::ACTION)
653 || main_controller->pressed(Controller::JUMP)
654 || main_controller->pressed(Controller::MENU_SELECT))
656 if(main_controller->pressed(Controller::PAUSE_MENU))
659 if (enter_level && !tux->is_moving())
661 /* Check special tile action */
662 SpecialTile* special_tile = at_special_tile();
665 if (special_tile->teleport_dest != Vector(-1,-1))
667 // TODO: an animation, camera scrolling or a fading would be a nice touch
668 sound_manager->play_sound("warp");
669 tux->back_direction = D_NONE;
670 tux->set_tile_pos(special_tile->teleport_dest);
675 /* Check level action */
676 bool level_finished = true;
677 Level* level = at_level();
680 std::cout << "No level to enter at: "
681 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
687 if (level->pos == tux->get_tile_pos())
689 PlayerStatus old_player_status = player_status;
691 std::cout << "Enter the current level: " << level->name << std::endl;
692 // do a shriking fade to the level
693 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
694 (level->pos.y*32 + 16 + offset.y)), 500);
695 GameSession session(get_resource_filename(levels_path + level->name),
696 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
698 switch (session.run())
700 case GameSession::ES_LEVEL_FINISHED:
702 level_finished = true;
703 bool old_level_state = level->solved;
704 level->solved = true;
706 // deal with statistics
707 level->statistics.merge(global_stats);
708 calculate_total_stats();
710 if (old_level_state != level->solved && level->auto_path)
711 { // Try to detect the next direction to which we should walk
712 // FIXME: Mostly a hack
713 Direction dir = D_NONE;
715 const Tile* tile = at(tux->get_tile_pos());
717 if (tile->getData() & Tile::WORLDMAP_NORTH
718 && tux->back_direction != D_NORTH)
720 else if (tile->getData() & Tile::WORLDMAP_SOUTH
721 && tux->back_direction != D_SOUTH)
723 else if (tile->getData() & Tile::WORLDMAP_EAST
724 && tux->back_direction != D_EAST)
726 else if (tile->getData() & Tile::WORLDMAP_WEST
727 && tux->back_direction != D_WEST)
732 tux->set_direction(dir);
733 //tux->update(delta);
736 std::cout << "Walk to dir: " << dir << std::endl;
741 case GameSession::ES_LEVEL_ABORT:
742 level_finished = false;
743 /* In case the player's abort the level, keep it using the old
744 status. But the minimum lives and no bonus. */
745 player_status.coins = old_player_status.coins;
746 player_status.lives = std::min(old_player_status.lives, player_status.lives);
747 player_status.bonus = NO_BONUS;
750 case GameSession::ES_GAME_OVER:
752 level_finished = false;
753 /* draw an end screen */
754 /* TODO: in the future, this should make a dialog a la SuperMario, asking
755 if the player wants to restart the world map with no score and from
759 DrawingContext context;
760 context.draw_gradient(Color (200,240,220), Color(200,200,220),
763 context.draw_text(blue_text, _("GAMEOVER"),
764 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
766 sprintf(str, _("COINS: %d"), player_status.coins);
767 context.draw_text(gold_text, str,
768 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
771 total_stats.draw_message_info(context, _("Total Statistics"));
773 context.do_drawing();
776 wait_for_event(event,2000,6000,true);
779 player_status.reset();
782 case GameSession::ES_NONE:
784 // Should never be reached
788 sound_manager->play_music(song);
789 Menu::set_current(0);
790 if (!savegame_file.empty())
791 savegame(savegame_file);
793 /* The porpose of the next checking is that if the player lost
794 the level (in case there is one), don't show anything */
796 if (!level->extro_filename.empty()) {
797 // Display a text file
798 std::string filename = levels_path + level->extro_filename;
799 display_text_file(filename);
802 if (!level->next_worldmap.empty())
804 // Load given worldmap
805 loadmap(level->next_worldmap);
807 if (level->quit_worldmap)
814 // tux->set_direction(input_direction);
819 WorldMap::at(Vector p)
828 return tile_manager->get(tilemap[width * y + x]);
834 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
836 if (i->pos == tux->get_tile_pos())
843 WorldMap::SpecialTile*
844 WorldMap::at_special_tile()
846 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
848 if (i->pos == tux->get_tile_pos())
856 WorldMap::draw(DrawingContext& context)
858 for(int y = 0; y < height; ++y)
859 for(int x = 0; x < width; ++x)
861 const Tile* tile = at(Vector(x, y));
862 tile->draw(context, Vector(x*32, y*32), LAYER_TILES);
865 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
868 context.draw_surface(leveldot_green,
869 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
871 context.draw_surface(leveldot_red,
872 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
875 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
880 if (i->teleport_dest != Vector(-1, -1))
881 context.draw_surface(teleporterdot,
882 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
884 else if (!i->map_message.empty() && !i->passive_message)
885 context.draw_surface(messagedot,
886 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
890 draw_status(context);
894 WorldMap::draw_status(DrawingContext& context)
896 context.push_transform();
897 context.set_translation(Vector(0, 0));
900 sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
902 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
903 context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
905 sprintf(str, "%d", player_status.coins);
906 context.draw_text(white_text, _("COINS"), Vector(SCREEN_WIDTH/2 - 16*5, 0),
907 LEFT_ALLIGN, LAYER_FOREGROUND1);
908 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2 + (16*5)/2, 0),
909 LEFT_ALLIGN, LAYER_FOREGROUND1);
911 if (player_status.lives >= 5)
913 sprintf(str, "%dx", player_status.lives);
914 context.draw_text(gold_text, str,
915 Vector(SCREEN_WIDTH - gold_text->get_text_width(str) - tux_life->w, 0),
916 LEFT_ALLIGN, LAYER_FOREGROUND1);
917 context.draw_surface(tux_life, Vector(SCREEN_WIDTH -
918 gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
922 for(int i= 0; i < player_status.lives; ++i)
923 context.draw_surface(tux_life,
924 Vector(SCREEN_WIDTH - tux_life->w*4 + (tux_life->w*i), 0),
927 context.draw_text(white_text, _("LIVES"),
928 Vector(SCREEN_WIDTH - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
929 LEFT_ALLIGN, LAYER_FOREGROUND1);
931 if (!tux->is_moving())
933 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
935 if (i->pos == tux->get_tile_pos())
940 context.draw_text(white_text, i->title,
941 Vector(SCREEN_WIDTH/2,
942 SCREEN_HEIGHT - white_text->get_height() - 30),
943 CENTER_ALLIGN, LAYER_FOREGROUND1);
945 i->statistics.draw_worldmap_info(context);
949 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
951 if (i->pos == tux->get_tile_pos())
953 /* Display an in-map message in the map, if any as been selected */
954 if(!i->map_message.empty() && !i->passive_message)
955 context.draw_text(gold_text, i->map_message,
956 Vector(SCREEN_WIDTH/2,
957 SCREEN_HEIGHT - white_text->get_height() - 60),
958 CENTER_ALLIGN, LAYER_FOREGROUND1);
963 /* Display a passive message in the map, if needed */
964 if(passive_message_timer.check())
965 context.draw_text(gold_text, passive_message,
966 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
967 CENTER_ALLIGN, LAYER_FOREGROUND1);
969 context.pop_transform();
975 Menu::set_current(0);
979 song = sound_manager->load_music(datadir + "/music/" + music);
980 sound_manager->play_music(song);
982 if(!intro_displayed && intro_filename != "") {
983 std::string filename = levels_path + intro_filename;
984 display_text_file(filename);
985 intro_displayed = true;
988 Uint32 lastticks = SDL_GetTicks();
989 DrawingContext context;
991 Uint32 ticks = SDL_GetTicks();
992 float elapsed_time = float(ticks - lastticks) / 1000;
993 global_time += elapsed_time;
997 if(elapsed_time > .025)
1000 Vector tux_pos = tux->get_pos();
1002 offset.x = -tux_pos.x + SCREEN_WIDTH/2;
1003 offset.y = -tux_pos.y + SCREEN_HEIGHT/2;
1005 if (offset.x > 0) offset.x = 0;
1006 if (offset.y > 0) offset.y = 0;
1008 if (offset.x < SCREEN_WIDTH - width*32) offset.x = SCREEN_WIDTH - width*32;
1009 if (offset.y < SCREEN_HEIGHT - height*32) offset.y = SCREEN_HEIGHT - height*32;
1011 context.push_transform();
1012 context.set_translation(-offset);
1014 context.pop_transform();
1016 update(elapsed_time);
1018 if(Menu::current()) {
1019 Menu::current()->draw(context);
1020 mouse_cursor->draw(context);
1023 context.do_drawing();
1028 WorldMap::savegame(const std::string& filename)
1033 std::ofstream file(filename.c_str(), std::ios::out);
1034 lisp::Writer writer(file);
1036 int nb_solved_levels = 0, total_levels = 0;
1037 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1042 char nb_solved_levels_str[80], total_levels_str[80];
1043 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1044 sprintf(total_levels_str, "%d", total_levels);
1046 writer.write_comment("Worldmap save file");
1048 writer.start_list("supertux-savegame");
1050 writer.write_int("version", 1);
1051 writer.write_string("title",
1052 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1053 writer.write_string("map", map_filename);
1054 writer.write_bool("intro-displayed", intro_displayed);
1056 writer.start_list("tux");
1058 writer.write_float("x", tux->get_tile_pos().x);
1059 writer.write_float("y", tux->get_tile_pos().y);
1060 writer.write_string("back", direction_to_string(tux->back_direction));
1061 player_status.write(writer);
1062 writer.write_string("back", direction_to_string(tux->back_direction));
1064 writer.end_list("tux");
1066 writer.start_list("levels");
1068 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1072 writer.start_list("level");
1074 writer.write_string("name", i->name);
1075 writer.write_bool("solved", true);
1076 i->statistics.write(writer);
1078 writer.end_list("level");
1082 writer.end_list("levels");
1084 writer.end_list("supertux-savegame");
1088 WorldMap::loadgame(const std::string& filename)
1090 std::cout << "loadgame: " << filename << std::endl;
1091 savegame_file = filename;
1094 lisp::Parser parser;
1095 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1097 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1099 throw std::runtime_error("File is not a supertux-savegame file.");
1101 /* Get the Map filename and then load it before setting level settings */
1102 std::string cur_map_filename = map_filename;
1103 savegame->get("map", map_filename);
1106 savegame->get("intro-displayed", intro_displayed);
1107 savegame->get("lives", player_status.lives);
1108 savegame->get("coins", player_status.coins);
1109 savegame->get("max-score-multiplier", player_status.max_score_multiplier);
1110 if (player_status.lives < 0)
1111 player_status.reset();
1113 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1117 std::string back_str = "none";
1119 tux_lisp->get("x", p.x);
1120 tux_lisp->get("y", p.y);
1121 tux_lisp->get("back", back_str);
1122 player_status.read(*tux_lisp);
1124 tux->back_direction = string_to_direction(back_str);
1125 tux->set_tile_pos(p);
1128 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1130 lisp::ListIterator iter(levels_lisp);
1131 while(iter.next()) {
1132 if(iter.item() == "level") {
1134 bool solved = false;
1136 const lisp::Lisp* level = iter.lisp();
1137 level->get("name", name);
1138 level->get("solved", solved);
1140 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1142 if (name == i->name)
1145 i->statistics.parse(*level);
1150 std::cerr << "Unknown token '" << iter.item()
1151 << "' in levels block in worldmap.\n";
1155 } catch(std::exception& e) {
1156 std::cerr << "Problem loading game '" << filename << "': " << e.what()
1159 player_status.reset();
1162 calculate_total_stats();
1166 WorldMap::loadmap(const std::string& filename)
1169 map_filename = filename;
1173 } // namespace WorldMapNS