3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include "gettext.hpp"
33 #include "video/surface.hpp"
34 #include "video/screen.hpp"
35 #include "video/drawing_context.hpp"
36 #include "sprite/sprite_manager.hpp"
37 #include "audio/sound_manager.hpp"
38 #include "lisp/parser.hpp"
39 #include "lisp/lisp.hpp"
40 #include "lisp/list_iterator.hpp"
41 #include "lisp/writer.hpp"
42 #include "game_session.hpp"
44 #include "worldmap.hpp"
45 #include "resources.hpp"
47 #include "player_status.hpp"
48 #include "textscroller.hpp"
50 #include "spawn_point.hpp"
51 #include "file_system.hpp"
52 #include "gui/menu.hpp"
53 #include "gui/mousecursor.hpp"
54 #include "control/joystickkeyboardcontroller.hpp"
55 #include "object/background.hpp"
56 #include "object/tilemap.hpp"
57 #include "scripting/script_interpreter.hpp"
58 #include "exceptions.hpp"
60 Menu* worldmap_menu = 0;
62 static const float TUXSPEED = 200;
63 static const float map_message_TIME = 2.8;
65 namespace WorldMapNS {
67 Direction reverse_dir(Direction direction)
86 direction_to_string(Direction direction)
104 string_to_direction(const std::string& directory)
106 if (directory == "west")
108 else if (directory == "east")
110 else if (directory == "north")
112 else if (directory == "south")
118 //---------------------------------------------------------------------------
120 Tux::Tux(WorldMap* worldmap_)
121 : worldmap(worldmap_)
123 tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
128 input_direction = D_NONE;
137 Tux::draw(DrawingContext& context)
139 switch (player_status->bonus) {
141 tux_sprite->set_action(moving ? "large-walking" : "large-stop");
144 tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
147 tux_sprite->set_action(moving ? "small-walking" : "small-stop");
150 msg_debug("Bonus type not handled in worldmap.");
151 tux_sprite->set_action("large-stop");
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::tryStartWalking()
205 if (input_direction == D_NONE) return;
207 WorldMap::Level* level = worldmap->at_level();
209 // We got a new direction, so lets start walking when possible
211 if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile))
213 tile_pos = next_tile;
215 direction = input_direction;
216 back_direction = reverse_dir(direction);
218 else if (input_direction == back_direction)
221 direction = input_direction;
222 tile_pos = worldmap->get_next_tile(tile_pos, direction);
223 back_direction = reverse_dir(direction);
229 Tux::canWalk(const Tile* tile, Direction dir)
231 return ((tile->getData() & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
232 (tile->getData() & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
233 (tile->getData() & Tile::WORLDMAP_EAST && dir == D_EAST) ||
234 (tile->getData() & Tile::WORLDMAP_WEST && dir == D_WEST));
238 Tux::tryContinueWalking(float elapsed_time)
243 offset += TUXSPEED * elapsed_time;
245 // Do nothing if we have not yet reached the next tile
246 if (offset <= 32) return;
250 // if this is a special_tile with passive_message, display it
251 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
252 if(special_tile && special_tile->passive_message)
254 // direction and the apply_action_ are opposites, since they "see"
255 // directions in a different way
256 if((direction == D_NORTH && special_tile->apply_action_south) ||
257 (direction == D_SOUTH && special_tile->apply_action_north) ||
258 (direction == D_WEST && special_tile->apply_action_east) ||
259 (direction == D_EAST && special_tile->apply_action_west))
261 worldmap->passive_message = special_tile->map_message;
262 worldmap->passive_message_timer.start(map_message_TIME);
266 // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
267 if ((worldmap->at_level()) || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP) || (special_tile && !special_tile->passive_message))
269 if(special_tile && !special_tile->map_message.empty() && !special_tile->passive_message) worldmap->passive_message_timer.start(0);
274 // if user wants to change direction, try changing, else guess the direction in which to walk next
275 const Tile* tile = worldmap->at(tile_pos);
276 if (direction != input_direction)
278 if(canWalk(tile, input_direction))
280 direction = input_direction;
281 back_direction = reverse_dir(direction);
286 Direction dir = D_NONE;
287 if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH) dir = D_NORTH;
288 else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH) dir = D_SOUTH;
289 else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST) dir = D_EAST;
290 else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST) dir = D_WEST;
294 // Should never be reached if tiledata is good
295 msg_warning("Could not determine where to walk next");
301 input_direction = direction;
302 back_direction = reverse_dir(direction);
305 // Walk automatically to the next tile
306 if(direction != D_NONE)
309 if (worldmap->path_ok(direction, tile_pos, &next_tile))
311 tile_pos = next_tile;
315 msg_warning("Tilemap data is buggy");
322 Tux::updateInputDirection()
324 if(main_controller->hold(Controller::UP)) input_direction = D_NORTH;
325 else if(main_controller->hold(Controller::DOWN)) input_direction = D_SOUTH;
326 else if(main_controller->hold(Controller::LEFT)) input_direction = D_WEST;
327 else if(main_controller->hold(Controller::RIGHT)) input_direction = D_EAST;
332 Tux::update(float elapsed_time)
334 updateInputDirection();
335 if (moving) tryContinueWalking(elapsed_time); else tryStartWalking();
338 //---------------------------------------------------------------------------
343 tile_manager = new TileManager("images/worldmap.strf");
348 messagedot = new Surface("images/worldmap/common/messagedot.png");
349 teleporterdot = sprite_manager->create("images/worldmap/common/teleporter.sprite");
352 music = "music/salcon.ogg";
353 intro_displayed = false;
358 WorldMap::~WorldMap()
361 for(SpawnPoints::iterator i = spawn_points.begin();
362 i != spawn_points.end(); ++i) {
365 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
369 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i) {
376 delete teleporterdot;
380 WorldMap::add_object(GameObject* object)
382 TileMap* tilemap = dynamic_cast<TileMap*> (object);
383 if(tilemap != 0 && tilemap->is_solid()) {
387 game_objects.push_back(object);
391 WorldMap::clear_objects()
393 for(GameObjects::iterator i = game_objects.begin();
394 i != game_objects.end(); ++i)
396 game_objects.clear();
402 // Don't forget to set map_filename before calling this
406 levels_path = FileSystem::dirname(map_filename);
410 std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
412 const lisp::Lisp* lisp = root->get_lisp("supertux-level");
414 throw std::runtime_error("file isn't a supertux-level file.");
416 lisp->get("name", name);
418 const lisp::Lisp* sector = lisp->get_lisp("sector");
420 throw std::runtime_error("No sector sepcified in worldmap file.");
423 lisp::ListIterator iter(sector);
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() == "music") {
430 iter.value()->get(music);
431 } else if(iter.item() == "intro-script") {
432 iter.value()->get(intro_script);
433 } else if(iter.item() == "worldmap-spawnpoint") {
434 SpawnPoint* sp = new SpawnPoint(iter.lisp());
435 spawn_points.push_back(sp);
436 } else if(iter.item() == "level") {
437 parse_level_tile(iter.lisp());
438 } else if(iter.item() == "special-tile") {
439 parse_special_tile(iter.lisp());
441 msg_warning("Unknown token '" << iter.item() << "' in worldmap");
445 throw std::runtime_error("No solid tilemap specified");
447 // search for main spawnpoint
448 for(SpawnPoints::iterator i = spawn_points.begin();
449 i != spawn_points.end(); ++i) {
451 if(sp->name == "main") {
453 tux->set_tile_pos(p);
458 } catch(std::exception& e) {
459 std::stringstream msg;
460 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
462 throw std::runtime_error(msg.str());
467 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
469 SpecialTile special_tile;
471 lisp->get("x", special_tile.pos.x);
472 lisp->get("y", special_tile.pos.y);
475 if (lisp->get("sprite", sprite)) {
476 special_tile.sprite = sprite_manager->create(sprite);
478 special_tile.sprite = 0;
481 lisp->get("map-message", special_tile.map_message);
482 special_tile.passive_message = false;
483 lisp->get("passive-message", special_tile.passive_message);
484 special_tile.teleport_dest = Vector(-1,-1);
485 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
486 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
487 special_tile.invisible = false;
488 lisp->get("invisible-tile", special_tile.invisible);
490 special_tile.apply_action_north = true;
491 special_tile.apply_action_south = true;
492 special_tile.apply_action_east = true;
493 special_tile.apply_action_west = true;
495 std::string apply_direction;
496 lisp->get("apply-to-direction", apply_direction);
497 if(!apply_direction.empty()) {
498 special_tile.apply_action_north = false;
499 special_tile.apply_action_south = false;
500 special_tile.apply_action_east = false;
501 special_tile.apply_action_west = false;
502 if(apply_direction.find("north") != std::string::npos)
503 special_tile.apply_action_north = true;
504 if(apply_direction.find("south") != std::string::npos)
505 special_tile.apply_action_south = true;
506 if(apply_direction.find("east") != std::string::npos)
507 special_tile.apply_action_east = true;
508 if(apply_direction.find("west") != std::string::npos)
509 special_tile.apply_action_west = true;
512 special_tiles.push_back(special_tile);
516 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
520 level.solved = false;
527 std::string sprite = "images/worldmap/common/leveldot.sprite";
528 level_lisp->get("sprite", sprite);
529 level.sprite = sprite_manager->create(sprite);
531 level_lisp->get("extro-script", level.extro_script);
532 level_lisp->get("next-worldmap", level.next_worldmap);
534 level.quit_worldmap = false;
535 level_lisp->get("quit-worldmap", level.quit_worldmap);
537 level_lisp->get("name", level.name);
539 if (!PHYSFS_exists((levels_path + level.name).c_str()))
541 // Do we want to bail out instead...? We might get messages from modders
542 // who can't make their levels run because they're too dumb to watch
543 // their terminals...
544 msg_warning("level file '" << level.name
545 << "' does not exist and will not be added to the worldmap");
549 level_lisp->get("x", level.pos.x);
550 level_lisp->get("y", level.pos.y);
552 level.auto_path = true;
553 level_lisp->get("auto-path", level.auto_path);
555 level.vertical_flip = false;
556 level_lisp->get("vertical-flip", level.vertical_flip);
558 levels.push_back(level);
562 WorldMap::get_level_title(Level& level)
564 /** get special_tile's title */
565 level.title = "<no title>";
569 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
571 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
575 level_lisp->get("name", level.title);
576 } catch(std::exception& e) {
577 msg_warning("Problem when reading leveltitle: " << e.what());
582 void WorldMap::calculate_total_stats()
585 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
589 total_stats += i->statistics;
595 WorldMap::on_escape_press()
597 // Show or hide the menu
598 if(!Menu::current()) {
599 Menu::set_current(worldmap_menu);
600 tux->set_direction(D_NONE); // stop tux movement when menu is called
602 Menu::set_current(0);
607 WorldMap::get_input()
609 main_controller->update();
612 while (SDL_PollEvent(&event)) {
614 Menu::current()->event(event);
615 main_controller->process_event(event);
616 if(event.type == SDL_QUIT)
617 throw graceful_shutdown();
622 WorldMap::get_next_tile(Vector pos, Direction direction)
644 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
646 *new_pos = get_next_tile(old_pos, direction);
648 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
649 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
650 { // New position is outsite the tilemap
654 { // Check if the tile allows us to go to new_pos
658 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
659 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
662 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
663 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
666 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
667 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
670 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
671 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
674 assert(!"path_ok() can't work if direction is NONE");
681 WorldMap::update(float delta)
683 Menu* menu = Menu::current();
687 if(menu == worldmap_menu) {
688 switch (worldmap_menu->check())
690 case MNID_RETURNWORLDMAP: // Return to game
691 Menu::set_current(0);
693 case MNID_QUITWORLDMAP: // Quit Worldmap
697 } else if(menu == options_menu) {
698 process_options_menu();
704 // update GameObjects
705 for(GameObjects::iterator i = game_objects.begin();
706 i != game_objects.end(); ++i) {
707 GameObject* object = *i;
708 object->update(delta);
710 // remove old GameObjects
711 for(GameObjects::iterator i = game_objects.begin();
712 i != game_objects.end(); ) {
713 GameObject* object = *i;
714 if(!object->is_valid()) {
716 i = game_objects.erase(i);
722 bool enter_level = false;
723 if(main_controller->pressed(Controller::ACTION)
724 || main_controller->pressed(Controller::JUMP)
725 || main_controller->pressed(Controller::MENU_SELECT))
727 if(main_controller->pressed(Controller::PAUSE_MENU))
730 if (enter_level && !tux->is_moving())
732 /* Check special tile action */
733 SpecialTile* special_tile = at_special_tile();
736 if (special_tile->teleport_dest != Vector(-1,-1))
738 // TODO: an animation, camera scrolling or a fading would be a nice touch
739 sound_manager->play("sounds/warp.wav");
740 tux->back_direction = D_NONE;
741 tux->set_tile_pos(special_tile->teleport_dest);
746 /* Check level action */
747 bool level_finished = true;
748 Level* level = at_level();
751 msg_warning("No level to enter at: "
752 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y);
757 if (level->pos == tux->get_tile_pos())
759 sound_manager->stop_music();
760 PlayerStatus old_player_status;
761 old_player_status = *player_status;
763 // do a shriking fade to the level
764 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
765 (level->pos.y*32 + 16 + offset.y)), 500);
766 GameSession session(levels_path + level->name,
767 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
769 switch (session.run())
771 case GameSession::ES_LEVEL_FINISHED:
773 level_finished = true;
774 bool old_level_state = level->solved;
775 level->solved = true;
776 level->sprite->set_action("solved");
778 // deal with statistics
779 level->statistics.merge(global_stats);
780 calculate_total_stats();
782 if (old_level_state != level->solved && level->auto_path)
783 { // Try to detect the next direction to which we should walk
784 // FIXME: Mostly a hack
785 Direction dir = D_NONE;
787 const Tile* tile = at(tux->get_tile_pos());
789 if (tile->getData() & Tile::WORLDMAP_NORTH
790 && tux->back_direction != D_NORTH)
792 else if (tile->getData() & Tile::WORLDMAP_SOUTH
793 && tux->back_direction != D_SOUTH)
795 else if (tile->getData() & Tile::WORLDMAP_EAST
796 && tux->back_direction != D_EAST)
798 else if (tile->getData() & Tile::WORLDMAP_WEST
799 && tux->back_direction != D_WEST)
804 tux->set_direction(dir);
810 case GameSession::ES_LEVEL_ABORT:
811 level_finished = false;
812 /* In case the player's abort the level, keep it using the old
813 status. But the minimum lives and no bonus. */
814 player_status->coins = old_player_status.coins;
815 player_status->lives = std::min(old_player_status.lives, player_status->lives);
816 player_status->bonus = NO_BONUS;
819 case GameSession::ES_GAME_OVER:
821 level_finished = false;
822 /* draw an end screen */
823 /* TODO: in the future, this should make a dialog a la SuperMario, asking
824 if the player wants to restart the world map with no score and from
828 DrawingContext context;
829 context.draw_gradient(Color (200,240,220), Color(200,200,220),
832 context.draw_text(blue_text, _("GAMEOVER"),
833 Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
835 sprintf(str, _("COINS: %d"), player_status->coins);
836 context.draw_text(gold_text, str,
837 Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
840 total_stats.draw_message_info(context, _("Total Statistics"));
842 context.do_drawing();
844 wait_for_event(2.0, 6.0);
847 player_status->reset();
850 case GameSession::ES_NONE:
852 // Should never be reached
856 sound_manager->play_music(music);
857 Menu::set_current(0);
858 if (!savegame_file.empty())
859 savegame(savegame_file);
861 /* The porpose of the next checking is that if the player lost
862 the level (in case there is one), don't show anything */
864 if (level->extro_script != "") {
866 std::auto_ptr<ScriptInterpreter> interpreter
867 (new ScriptInterpreter(levels_path));
868 std::istringstream in(level->extro_script);
869 interpreter->run_script(in, "level-extro-script");
870 add_object(interpreter.release());
871 } catch(std::exception& e) {
872 msg_warning("Couldn't run level-extro-script:" << e.what());
876 if (!level->next_worldmap.empty())
878 // Load given worldmap
879 loadmap(level->next_worldmap);
881 if (level->quit_worldmap)
887 // tux->set_direction(input_direction);
892 WorldMap::at(Vector p)
894 return solids->get_tile((int) p.x, (int) p.y);
900 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
902 if (i->pos == tux->get_tile_pos())
909 WorldMap::SpecialTile*
910 WorldMap::at_special_tile()
912 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
914 if (i->pos == tux->get_tile_pos())
922 WorldMap::draw(DrawingContext& context)
924 for(GameObjects::iterator i = game_objects.begin();
925 i != game_objects.end(); ++i) {
926 GameObject* object = *i;
927 object->draw(context);
930 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
932 const Level& level = *i;
933 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
936 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
942 i->sprite->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
944 else if (i->teleport_dest != Vector(-1, -1))
945 teleporterdot->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
947 else if (!i->map_message.empty() && !i->passive_message)
948 context.draw_surface(messagedot,
949 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
952 draw_status(context);
956 WorldMap::draw_status(DrawingContext& context)
958 context.push_transform();
959 context.set_translation(Vector(0, 0));
961 player_status->draw(context);
963 if (!tux->is_moving())
965 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
967 if (i->pos == tux->get_tile_pos())
972 context.draw_text(white_text, i->title,
973 Vector(SCREEN_WIDTH/2,
974 SCREEN_HEIGHT - white_text->get_height() - 30),
975 CENTER_ALLIGN, LAYER_FOREGROUND1);
977 i->statistics.draw_worldmap_info(context);
981 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
983 if (i->pos == tux->get_tile_pos())
985 /* Display an in-map message in the map, if any as been selected */
986 if(!i->map_message.empty() && !i->passive_message)
987 context.draw_text(gold_text, i->map_message,
988 Vector(SCREEN_WIDTH/2,
989 SCREEN_HEIGHT - white_text->get_height() - 60),
990 CENTER_ALLIGN, LAYER_FOREGROUND1);
995 /* Display a passive message in the map, if needed */
996 if(passive_message_timer.started())
997 context.draw_text(gold_text, passive_message,
998 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
999 CENTER_ALLIGN, LAYER_FOREGROUND1);
1001 context.pop_transform();
1007 Menu::set_current(0);
1011 sound_manager->play_music(music);
1013 if(!intro_displayed && intro_script != "") {
1015 std::auto_ptr<ScriptInterpreter> interpreter
1016 (new ScriptInterpreter(levels_path));
1017 std::istringstream in(intro_script);
1018 interpreter->run_script(in, "worldmap-intro-script");
1019 add_object(interpreter.release());
1020 } catch(std::exception& e) {
1021 msg_warning("Couldn't execute worldmap-intro-script: "
1025 intro_displayed = true;
1028 Uint32 lastticks = SDL_GetTicks();
1029 DrawingContext context;
1030 Console* console = new Console(&context);
1032 Uint32 ticks = SDL_GetTicks();
1033 float elapsed_time = float(ticks - lastticks) / 1000;
1034 game_time += elapsed_time;
1037 // 40 fps minimum // TODO use same code as in GameSession here
1038 if(elapsed_time > .025)
1039 elapsed_time = .025;
1041 Vector tux_pos = tux->get_pos();
1042 offset.x = tux_pos.x - SCREEN_WIDTH/2;
1043 offset.y = tux_pos.y - SCREEN_HEIGHT/2;
1050 if (offset.x > solids->get_width()*32 - SCREEN_WIDTH)
1051 offset.x = solids->get_width()*32 - SCREEN_WIDTH;
1052 if (offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
1053 offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
1055 context.push_transform();
1056 context.set_translation(offset);
1058 context.pop_transform();
1060 update(elapsed_time);
1061 sound_manager->update();
1063 if(Menu::current()) {
1064 Menu::current()->draw(context);
1068 context.do_drawing();
1075 WorldMap::savegame(const std::string& filename)
1080 std::string dir = FileSystem::dirname(filename);
1081 if(PHYSFS_exists(dir.c_str()) == 0 && PHYSFS_mkdir(dir.c_str()) != 0) {
1082 std::ostringstream msg;
1083 msg << "Couldn't create directory '" << dir << "' for savegame:"
1084 << PHYSFS_getLastError();
1085 throw std::runtime_error(msg.str());
1087 if(!PHYSFS_isDirectory(dir.c_str())) {
1088 std::ostringstream msg;
1089 msg << "'" << dir << "' is not a directory.";
1090 throw std::runtime_error(msg.str());
1093 lisp::Writer writer(filename);
1095 int nb_solved_levels = 0, total_levels = 0;
1096 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1101 char nb_solved_levels_str[80], total_levels_str[80];
1102 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1103 sprintf(total_levels_str, "%d", total_levels);
1105 writer.write_comment("Worldmap save file");
1107 writer.start_list("supertux-savegame");
1109 writer.write_int("version", 1);
1110 writer.write_string("title",
1111 std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
1112 writer.write_string("map", map_filename);
1113 writer.write_bool("intro-displayed", intro_displayed);
1115 writer.start_list("tux");
1117 writer.write_float("x", tux->get_tile_pos().x);
1118 writer.write_float("y", tux->get_tile_pos().y);
1119 writer.write_string("back", direction_to_string(tux->back_direction));
1120 player_status->write(writer);
1121 writer.write_string("back", direction_to_string(tux->back_direction));
1123 writer.end_list("tux");
1125 writer.start_list("levels");
1127 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1131 writer.start_list("level");
1133 writer.write_string("name", i->name);
1134 writer.write_bool("solved", true);
1135 i->statistics.write(writer);
1137 writer.end_list("level");
1141 writer.end_list("levels");
1143 writer.end_list("supertux-savegame");
1147 WorldMap::loadgame(const std::string& filename)
1149 msg_debug("loadgame: " << filename);
1150 savegame_file = filename;
1152 if (PHYSFS_exists(filename.c_str())) // savegame exists
1155 lisp::Parser parser;
1157 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
1159 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
1161 throw std::runtime_error("File is not a supertux-savegame file.");
1163 /* Get the Map filename and then load it before setting level settings */
1164 std::string cur_map_filename = map_filename;
1165 savegame->get("map", map_filename);
1168 savegame->get("intro-displayed", intro_displayed);
1169 savegame->get("lives", player_status->lives);
1170 savegame->get("coins", player_status->coins);
1171 savegame->get("max-score-multiplier", player_status->max_score_multiplier);
1172 if (player_status->lives < 0)
1173 player_status->reset();
1175 const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
1179 std::string back_str = "none";
1181 tux_lisp->get("x", p.x);
1182 tux_lisp->get("y", p.y);
1183 tux_lisp->get("back", back_str);
1184 player_status->read(*tux_lisp);
1186 tux->back_direction = string_to_direction(back_str);
1187 tux->set_tile_pos(p);
1190 const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
1192 lisp::ListIterator iter(levels_lisp);
1193 while(iter.next()) {
1194 if(iter.item() == "level") {
1196 bool solved = false;
1198 const lisp::Lisp* level = iter.lisp();
1199 level->get("name", name);
1200 level->get("solved", solved);
1202 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1204 if (name == i->name)
1207 i->sprite->set_action(solved ? "solved" : "default");
1208 i->statistics.parse(*level);
1213 msg_warning("Unknown token '" << iter.item()
1214 << "' in levels block in worldmap");
1218 } catch(std::exception& e) {
1219 msg_warning("Problem loading game '" << filename << "': " << e.what());
1221 player_status->reset();
1227 player_status->reset();
1230 calculate_total_stats();
1234 WorldMap::loadmap(const std::string& filename)
1237 map_filename = filename;
1241 } // namespace WorldMapNS