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.
28 #include "app/globals.h"
29 #include "video/surface.h"
30 #include "video/screen.h"
31 #include "video/drawing_context.h"
32 #include "utils/lispreader.h"
33 #include "utils/lispwriter.h"
34 #include "special/frame_rate.h"
36 #include "app/setup.h"
39 #include "audio/sound_manager.h"
40 #include "resources.h"
41 #include "app/gettext.h"
45 #define map_message_TIME 2.8
47 Menu* worldmap_menu = 0;
49 namespace WorldMapNS {
51 Direction reverse_dir(Direction direction)
70 direction_to_string(Direction direction)
88 string_to_direction(const std::string& directory)
90 if (directory == "west")
92 else if (directory == "east")
94 else if (directory == "north")
96 else if (directory == "south")
102 TileManager::TileManager()
104 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
105 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
108 Termination::abort("Couldn't load file", stwt_filename);
110 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
112 lisp_object_t* cur = lisp_cdr(root_obj);
114 while(!lisp_nil_p(cur))
116 lisp_object_t* element = lisp_car(cur);
118 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
122 Tile* tile = new Tile;
123 tile->north = tile->east = tile->south = tile->west = true;
125 tile->auto_walk = false;
127 LispReader reader(lisp_cdr(element));
128 reader.read_int("id", id);
131 reader.read_string("possible-directions", temp);
134 tile->north = tile->east = tile->south = tile->west = false;
135 if(temp.find("north") != std::string::npos)
137 if(temp.find("south") != std::string::npos)
139 if(temp.find("east") != std::string::npos)
141 if(temp.find("west") != std::string::npos)
145 /* For backward compatibility */
146 reader.read_bool("north", tile->north);
147 reader.read_bool("south", tile->south);
148 reader.read_bool("west", tile->west);
149 reader.read_bool("east", tile->east);
151 reader.read_bool("stop", tile->stop);
152 reader.read_bool("auto-walk", tile->auto_walk);
154 reader.read_string("one-way", temp);
155 tile->one_way = BOTH_WAYS;
158 if(temp == "north-south")
159 tile->one_way = NORTH_SOUTH_WAY;
160 else if(temp == "south-north")
161 tile->one_way = SOUTH_NORTH_WAY;
162 else if(temp == "east-west")
163 tile->one_way = EAST_WEST_WAY;
164 else if(temp == "west-east")
165 tile->one_way = WEST_EAST_WAY;
168 std::vector<std::string> filenames;
169 reader.read_string_vector("image", filenames);
171 if(filenames.size() == 0)
172 std::cerr << "Warning: no image specified for tile " << id
173 << ".\nIgnoring...\n" << std::endl;
175 for(int i = 0; static_cast<unsigned int>(i) < filenames.size(); i++)
177 Surface* image = new Surface(
178 datadir + "/images/worldmap/" + filenames[i], true);
179 tile->images.push_back(image);
183 reader.read_float("anim-fps", tile->anim_fps);
186 if (id >= int(tiles.size()))
193 puts("Unhandled symbol");
207 TileManager::~TileManager()
209 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
214 TileManager::get(int i)
216 assert(i >=0 && i < int(tiles.size()));
220 //---------------------------------------------------------------------------
222 Tux::Tux(WorldMap* worldmap_)
223 : worldmap(worldmap_)
225 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true);
226 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true);
227 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true);
231 tile_pos.x = worldmap->get_start_x();
232 tile_pos.y = worldmap->get_start_y();
234 input_direction = D_NONE;
239 delete smalltux_sprite;
240 delete firetux_sprite;
241 delete largetux_sprite;
245 Tux::draw(DrawingContext& context, const Vector& offset)
247 Vector pos = get_pos();
248 switch (player_status.bonus)
250 case PlayerStatus::GROWUP_BONUS:
251 context.draw_surface(largetux_sprite,
252 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
254 case PlayerStatus::FLOWER_BONUS:
255 context.draw_surface(firetux_sprite,
256 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
258 case PlayerStatus::NO_BONUS:
259 context.draw_surface(smalltux_sprite,
260 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
269 float x = tile_pos.x * 32;
270 float y = tile_pos.y * 32;
290 return Vector((int)x, (int)y);
298 input_direction = D_NONE;
303 Tux::set_direction(Direction dir)
305 input_direction = dir;
309 Tux::action(float delta)
313 if (input_direction != D_NONE)
315 WorldMap::Level* level = worldmap->at_level();
317 // We got a new direction, so lets start walking when possible
319 if ((!level || level->solved)
320 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
322 tile_pos = next_tile;
324 direction = input_direction;
325 back_direction = reverse_dir(direction);
327 else if (input_direction == back_direction)
330 direction = input_direction;
331 tile_pos = worldmap->get_next_tile(tile_pos, direction);
332 back_direction = reverse_dir(direction);
338 // Let tux walk a few pixels (20 pixel/sec)
339 offset += 20.0f * delta;
342 { // We reached the next tile, so we check what to do now
345 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
346 if(special_tile && special_tile->passive_message)
347 { // direction and the apply_action_ are opposites, since they "see"
348 // directions in a different way
349 if((direction == D_NORTH && special_tile->apply_action_south) ||
350 (direction == D_SOUTH && special_tile->apply_action_north) ||
351 (direction == D_WEST && special_tile->apply_action_east) ||
352 (direction == D_EAST && special_tile->apply_action_west))
354 worldmap->passive_message = special_tile->map_message;
355 worldmap->passive_message_timer.start(map_message_TIME);
359 if (worldmap->at(tile_pos)->stop ||
360 (special_tile && !special_tile->passive_message) ||
361 worldmap->at_level())
363 if(special_tile && !special_tile->map_message.empty() &&
364 !special_tile->passive_message)
365 worldmap->passive_message_timer.start(0);
370 if (worldmap->at(tile_pos)->auto_walk || direction != input_direction)
371 { // Turn to a new direction
372 Tile* tile = worldmap->at(tile_pos);
374 if(direction != input_direction &&
375 ((tile->north && input_direction == D_NORTH) ||
376 (tile->south && input_direction == D_SOUTH) ||
377 (tile->east && input_direction == D_EAST) ||
378 (tile->west && input_direction == D_WEST)))
379 { // player has changed direction during auto-movement
380 direction = input_direction;
381 back_direction = reverse_dir(direction);
383 else if(direction != input_direction)
384 { // player has changed to impossible tile
385 back_direction = reverse_dir(direction);
390 Direction dir = D_NONE;
392 if (tile->north && back_direction != D_NORTH)
394 else if (tile->south && back_direction != D_SOUTH)
396 else if (tile->east && back_direction != D_EAST)
398 else if (tile->west && back_direction != D_WEST)
404 input_direction = direction;
405 back_direction = reverse_dir(direction);
409 // Should never be reached if tiledata is good
416 // Walk automatically to the next tile
417 if(direction != D_NONE)
420 if (worldmap->path_ok(direction, tile_pos, &next_tile))
422 tile_pos = next_tile;
426 puts("Tilemap data is buggy");
435 //---------------------------------------------------------------------------
442 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end(); i++)
448 Tile::draw(DrawingContext& context, Vector pos)
450 // same code as from tile_manager.cpp draw_tile()
455 if(images.size() > 1)
457 size_t frame = size_t(global_time * anim_fps) % images.size();
459 context.draw_surface(images[frame], pos, LAYER_TILES);
461 else if (images.size() == 1)
463 context.draw_surface(images[0], pos, LAYER_TILES);
467 //---------------------------------------------------------------------------
471 tile_manager = new TileManager();
472 //tux = new Tux(this);
482 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
483 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
484 messagedot = new Surface(datadir + "/images/worldmap/messagedot.png", true);
485 teleporterdot = new Surface(datadir + "/images/worldmap/teleporterdot.png", true);
490 music = "salcon.mod";
495 WorldMap::~WorldMap()
500 delete leveldot_green;
503 delete teleporterdot;
506 // Don't forget to set map_filename before calling this
510 lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
512 Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
514 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
516 lisp_object_t* cur = lisp_cdr(root_obj);
518 while(!lisp_nil_p(cur))
520 lisp_object_t* element = lisp_car(cur);
522 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
524 LispReader reader(lisp_cdr(element));
525 reader.read_int("width", width);
526 reader.read_int("height", height);
527 reader.read_int_vector("data", tilemap);
529 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
531 LispReader reader(lisp_cdr(element));
532 reader.read_string("name", name, true);
533 reader.read_string("music", music);
534 reader.read_int("start_pos_x", start_x);
535 reader.read_int("start_pos_y", start_y);
537 else if (strcmp(lisp_symbol(lisp_car(element)), "special-tiles") == 0 ||
538 strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
540 lisp_object_t* cur = lisp_cdr(element);
542 while(!lisp_nil_p(cur))
544 lisp_object_t* element = lisp_car(cur);
546 if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
548 SpecialTile special_tile;
549 LispReader reader(lisp_cdr(element));
551 reader.read_float("x", special_tile.pos.x);
552 reader.read_float("y", special_tile.pos.y);
554 special_tile.map_message.erase();
555 reader.read_string("map-message", special_tile.map_message);
556 special_tile.passive_message = false;
557 reader.read_bool("passive-message", special_tile.passive_message);
559 special_tile.teleport_dest = Vector(-1,-1);
560 reader.read_float("teleport-to-x", special_tile.teleport_dest.x);
561 reader.read_float("teleport-to-y", special_tile.teleport_dest.y);
563 special_tile.invisible = false;
564 reader.read_bool("invisible-tile", special_tile.invisible);
566 special_tile.apply_action_north = special_tile.apply_action_south =
567 special_tile.apply_action_east = special_tile.apply_action_west =
570 std::string apply_direction;
571 reader.read_string("apply-to-direction", apply_direction);
572 if(!apply_direction.empty())
574 special_tile.apply_action_north = special_tile.apply_action_south =
575 special_tile.apply_action_east = special_tile.apply_action_west =
577 if(apply_direction.find("north") != std::string::npos)
578 special_tile.apply_action_north = true;
579 if(apply_direction.find("south") != std::string::npos)
580 special_tile.apply_action_south = true;
581 if(apply_direction.find("east") != std::string::npos)
582 special_tile.apply_action_east = true;
583 if(apply_direction.find("west") != std::string::npos)
584 special_tile.apply_action_west = true;
587 special_tiles.push_back(special_tile);
590 else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
593 LispReader reader(lisp_cdr(element));
594 level.solved = false;
601 reader.read_string("extro-filename", level.extro_filename);
602 reader.read_string("next-worldmap", level.next_worldmap);
604 level.quit_worldmap = false;
605 reader.read_bool("quit-worldmap", level.quit_worldmap);
607 reader.read_string("name", level.name, true);
608 reader.read_float("x", level.pos.x);
609 reader.read_float("y", level.pos.y);
611 level.auto_path = true;
612 reader.read_bool("auto-path", level.auto_path);
614 level.vertical_flip = false;
615 reader.read_bool("vertical-flip", level.vertical_flip);
617 levels.push_back(level);
638 void WorldMap::get_level_title(Level& level)
640 /** get special_tile's title */
641 level.title = "<no title>";
643 LispReader* reader = LispReader::load(datadir + "/levels/" + level.name, "supertux-level");
646 std::cerr << "Error: Could not open level file. Ignoring...\n";
650 reader->read_string("name", level.title, true);
654 void WorldMap::calculate_total_stats()
657 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
661 total_stats += i->statistics;
667 WorldMap::on_escape_press()
669 // Show or hide the menu
672 Menu::set_current(worldmap_menu);
673 tux->set_direction(D_NONE); // stop tux movement when menu is called
676 Menu::set_current(0);
680 WorldMap::get_input()
686 while (SDL_PollEvent(&event))
690 Menu::current()->event(event);
697 Termination::abort("Received window close", "");
701 key = event.key.keysym.sym;
703 if(key == SDLK_ESCAPE)
705 else if(key == SDLK_RETURN || key == keymap.power)
707 else if(key == SDLK_LEFT || key == keymap.power)
708 tux->set_direction(D_WEST);
709 else if(key == SDLK_RIGHT || key == keymap.right)
710 tux->set_direction(D_EAST);
711 else if(key == SDLK_UP || key == keymap.up ||
713 // there might be ppl that use jump as up key
714 tux->set_direction(D_NORTH);
715 else if(key == SDLK_DOWN || key == keymap.down)
716 tux->set_direction(D_SOUTH);
719 case SDL_JOYHATMOTION:
720 if(event.jhat.value & SDL_HAT_UP) {
721 tux->set_direction(D_NORTH);
722 } else if(event.jhat.value & SDL_HAT_DOWN) {
723 tux->set_direction(D_SOUTH);
724 } else if(event.jhat.value & SDL_HAT_LEFT) {
725 tux->set_direction(D_WEST);
726 } else if(event.jhat.value & SDL_HAT_RIGHT) {
727 tux->set_direction(D_EAST);
731 case SDL_JOYAXISMOTION:
732 if (event.jaxis.axis == joystick_keymap.x_axis)
734 if (event.jaxis.value < -joystick_keymap.dead_zone)
735 tux->set_direction(D_WEST);
736 else if (event.jaxis.value > joystick_keymap.dead_zone)
737 tux->set_direction(D_EAST);
739 else if (event.jaxis.axis == joystick_keymap.y_axis)
741 if (event.jaxis.value > joystick_keymap.dead_zone)
742 tux->set_direction(D_SOUTH);
743 else if (event.jaxis.value < -joystick_keymap.dead_zone)
744 tux->set_direction(D_NORTH);
748 case SDL_JOYBUTTONDOWN:
749 if (event.jbutton.button == joystick_keymap.b_button)
751 else if (event.jbutton.button == joystick_keymap.start_button)
763 WorldMap::get_next_tile(Vector pos, Direction direction)
786 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
788 *new_pos = get_next_tile(old_pos, direction);
790 if (!(new_pos->x >= 0 && new_pos->x < width
791 && new_pos->y >= 0 && new_pos->y < height))
792 { // New position is outsite the tilemap
795 else if(at(*new_pos)->one_way != BOTH_WAYS)
797 std::cerr << "one way only\n";
798 if((at(*new_pos)->one_way == NORTH_SOUTH_WAY && direction != D_SOUTH) ||
799 (at(*new_pos)->one_way == SOUTH_NORTH_WAY && direction != D_NORTH) ||
800 (at(*new_pos)->one_way == EAST_WEST_WAY && direction != D_WEST) ||
801 (at(*new_pos)->one_way == WEST_EAST_WAY && direction != D_EAST))
806 { // Check if we the tile allows us to go to new_pos
810 return (at(old_pos)->west && at(*new_pos)->east);
813 return (at(old_pos)->east && at(*new_pos)->west);
816 return (at(old_pos)->north && at(*new_pos)->south);
819 return (at(old_pos)->south && at(*new_pos)->north);
822 assert(!"path_ok() can't work if direction is NONE");
829 WorldMap::update(float delta)
831 if (enter_level && !tux->is_moving())
833 /* Check special tile action */
834 SpecialTile* special_tile = at_special_tile();
837 if (special_tile->teleport_dest != Vector(-1,-1))
839 // TODO: an animation, camera scrolling or a fading would be a nice touch
840 SoundManager::get()->play_sound(IDToSound(SND_WARP));
841 tux->back_direction = D_NONE;
842 tux->set_tile_pos(special_tile->teleport_dest);
847 /* Check level action */
848 bool level_finished = true;
849 Level* level = at_level();
852 std::cout << "No level to enter at: "
853 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
858 if (level->pos == tux->get_tile_pos())
860 PlayerStatus old_player_status = player_status;
862 std::cout << "Enter the current level: " << level->name << std::endl;
863 // do a shriking fade to the level
864 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),(level->pos.y*32 + 16
866 GameSession session(level->name,
867 ST_GL_LOAD_LEVEL_FILE, level->vertical_flip,
870 switch (session.run())
872 case GameSession::ES_LEVEL_FINISHED:
874 level_finished = true;
875 bool old_level_state = level->solved;
876 level->solved = true;
878 // deal with statistics
879 level->statistics.merge(global_stats);
880 calculate_total_stats();
882 if (session.get_current_sector()->player->got_power !=
883 session.get_current_sector()->player->NONE_POWER)
884 player_status.bonus = PlayerStatus::FLOWER_BONUS;
885 else if (session.get_current_sector()->player->size == BIG)
886 player_status.bonus = PlayerStatus::GROWUP_BONUS;
888 player_status.bonus = PlayerStatus::NO_BONUS;
890 if (old_level_state != level->solved && level->auto_path)
891 { // Try to detect the next direction to which we should walk
892 // FIXME: Mostly a hack
893 Direction dir = D_NONE;
895 Tile* tile = at(tux->get_tile_pos());
897 if (tile->north && tux->back_direction != D_NORTH)
899 else if (tile->south && tux->back_direction != D_SOUTH)
901 else if (tile->east && tux->back_direction != D_EAST)
903 else if (tile->west && tux->back_direction != D_WEST)
908 tux->set_direction(dir);
909 //tux->update(delta);
912 std::cout << "Walk to dir: " << dir << std::endl;
917 case GameSession::ES_LEVEL_ABORT:
918 level_finished = false;
919 /* In case the player's abort the level, keep it using the old
920 status. But the minimum lives and no bonus. */
921 player_status.distros = old_player_status.distros;
922 player_status.lives = std::min(old_player_status.lives, player_status.lives);
923 player_status.bonus = player_status.NO_BONUS;
926 case GameSession::ES_GAME_OVER:
928 level_finished = false;
929 /* draw an end screen */
930 /* TODO: in the future, this should make a dialog a la SuperMario, asking
931 if the player wants to restart the world map with no score and from
935 DrawingContext context;
936 context.draw_gradient(Color (200,240,220), Color(200,200,220),
939 context.draw_text(blue_text, _("GAMEOVER"),
940 Vector(screen->w/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
942 sprintf(str, _("COINS: %d"), player_status.distros);
943 context.draw_text(gold_text, str,
944 Vector(screen->w/2, screen->w - 32), CENTER_ALLIGN, LAYER_FOREGROUND1);
946 total_stats.draw_message_info(context, _("Total Statistics"));
948 context.do_drawing();
951 wait_for_event(event,2000,6000,true);
954 player_status.reset();
957 case GameSession::ES_NONE:
959 // Should never be reached
963 SoundManager::get()->play_music(song);
964 Menu::set_current(0);
965 if (!savegame_file.empty())
966 savegame(savegame_file);
968 /* The porpose of the next checking is that if the player lost
969 the level (in case there is one), don't show anything */
972 if (!level->extro_filename.empty())
974 // Display a text file
975 display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
978 if (!level->next_worldmap.empty())
980 // Load given worldmap
981 loadmap(level->next_worldmap);
983 if (level->quit_worldmap)
990 // tux->set_direction(input_direction);
993 Menu* menu = Menu::current();
998 if(menu == worldmap_menu)
1000 switch (worldmap_menu->check())
1002 case MNID_RETURNWORLDMAP: // Return to game
1004 case MNID_QUITWORLDMAP: // Quit Worldmap
1009 else if(menu == options_menu)
1011 process_options_menu();
1017 WorldMap::at(Vector p)
1026 return tile_manager->get(tilemap[width * y + x]);
1030 WorldMap::at_level()
1032 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1034 if (i->pos == tux->get_tile_pos())
1041 WorldMap::SpecialTile*
1042 WorldMap::at_special_tile()
1044 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1046 if (i->pos == tux->get_tile_pos())
1055 WorldMap::draw(DrawingContext& context, const Vector& offset)
1057 for(int y = 0; y < height; ++y)
1058 for(int x = 0; x < width; ++x)
1060 Tile* tile = at(Vector(x, y));
1061 tile->draw(context, Vector(x*32 + offset.x, y*32 + offset.y));
1064 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1067 context.draw_surface(leveldot_green,
1068 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1070 context.draw_surface(leveldot_red,
1071 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1074 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1079 if (i->teleport_dest != Vector(-1, -1))
1080 context.draw_surface(teleporterdot,
1081 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1083 else if (!i->map_message.empty() && !i->passive_message)
1084 context.draw_surface(messagedot,
1085 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1088 tux->draw(context, offset);
1089 draw_status(context);
1093 WorldMap::draw_status(DrawingContext& context)
1096 sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
1098 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
1099 context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
1101 sprintf(str, "%d", player_status.distros);
1102 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
1103 LEFT_ALLIGN, LAYER_FOREGROUND1);
1104 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
1105 LEFT_ALLIGN, LAYER_FOREGROUND1);
1107 if (player_status.lives >= 5)
1109 sprintf(str, "%dx", player_status.lives);
1110 context.draw_text(gold_text, str,
1111 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
1112 LEFT_ALLIGN, LAYER_FOREGROUND1);
1113 context.draw_surface(tux_life, Vector(screen->w -
1114 gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
1118 for(int i= 0; i < player_status.lives; ++i)
1119 context.draw_surface(tux_life,
1120 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
1123 context.draw_text(white_text, _("LIVES"),
1124 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
1125 LEFT_ALLIGN, LAYER_FOREGROUND1);
1127 if (!tux->is_moving())
1129 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1131 if (i->pos == tux->get_tile_pos())
1134 get_level_title(*i);
1136 context.draw_text(white_text, i->title,
1138 screen->h - white_text->get_height() - 30),
1139 CENTER_ALLIGN, LAYER_FOREGROUND1);
1141 i->statistics.draw_worldmap_info(context);
1145 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1147 if (i->pos == tux->get_tile_pos())
1149 /* Display an in-map message in the map, if any as been selected */
1150 if(!i->map_message.empty() && !i->passive_message)
1151 context.draw_text(gold_text, i->map_message,
1153 screen->h - white_text->get_height() - 60),
1154 CENTER_ALLIGN, LAYER_FOREGROUND1);
1159 /* Display a passive message in the map, if needed */
1160 if(passive_message_timer.check())
1161 context.draw_text(gold_text, passive_message,
1162 Vector(screen->w/2, screen->h - white_text->get_height() - 60),
1163 CENTER_ALLIGN, LAYER_FOREGROUND1);
1169 Menu::set_current(0);
1173 song = SoundManager::get()->load_music(datadir + "/music/" + music);
1174 SoundManager::get()->play_music(song);
1176 FrameRate frame_rate(10);
1177 frame_rate.set_frame_limit(false);
1181 DrawingContext context;
1184 float delta = frame_rate.get();
1191 frame_rate.update();
1193 Vector tux_pos = tux->get_pos();
1196 offset.x = -tux_pos.x + screen->w/2;
1197 offset.y = -tux_pos.y + screen->h/2;
1199 if (offset.x > 0) offset.x = 0;
1200 if (offset.y > 0) offset.y = 0;
1202 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
1203 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
1206 draw(context, offset);
1212 Menu::current()->draw(context);
1213 mouse_cursor->draw(context);
1216 context.do_drawing();
1223 WorldMap::savegame(const std::string& filename)
1228 std::cout << "savegame: " << filename << std::endl;
1230 std::ofstream file(filename.c_str(), std::ios::out);
1231 LispWriter* writer = new LispWriter(file);
1233 int nb_solved_levels = 0, total_levels = 0;
1234 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1240 char nb_solved_levels_str[80], total_levels_str[80];
1241 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1242 sprintf(total_levels_str, "%d", total_levels);
1244 writer->write_comment("Worldmap save file");
1246 writer->start_list("supertux-savegame");
1248 writer->write_int("version", 1);
1249 writer->write_string("title", std::string(name + " - " + nb_solved_levels_str + "/" + total_levels_str));
1250 writer->write_string("map", map_filename);
1251 writer->write_int("lives", player_status.lives);
1252 writer->write_int("distros", player_status.lives);
1253 writer->write_int("max-score-multiplier", player_status.max_score_multiplier);
1255 writer->start_list("tux");
1257 writer->write_float("x", tux->get_tile_pos().x);
1258 writer->write_float("y", tux->get_tile_pos().y);
1259 writer->write_string("back", direction_to_string(tux->back_direction));
1260 writer->write_string("bonus", bonus_to_string(player_status.bonus));
1262 writer->end_list("tux");
1264 writer->start_list("levels");
1266 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1270 writer->start_list("level");
1272 writer->write_string("name", i->name);
1273 writer->write_bool("solved", true);
1274 i->statistics.write(*writer);
1276 writer->end_list("level");
1280 writer->end_list("levels");
1282 writer->end_list("supertux-savegame");
1286 WorldMap::loadgame(const std::string& filename)
1288 std::cout << "loadgame: " << filename << std::endl;
1289 savegame_file = filename;
1291 if (access(filename.c_str(), F_OK) != 0)
1295 player_status.reset();
1300 lisp_object_t* savegame = lisp_read_from_file(filename);
1303 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1308 lisp_object_t* cur = savegame;
1310 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1316 cur = lisp_cdr(cur);
1317 LispReader reader(cur);
1319 /* Get the Map filename and then load it before setting level settings */
1320 std::string cur_map_filename = map_filename;
1321 reader.read_string("map", map_filename);
1322 // if(cur_map_filename != map_filename)
1325 reader.read_int("lives", player_status.lives);
1326 reader.read_int("distros", player_status.distros);
1327 reader.read_int("max-score-multiplier", player_status.max_score_multiplier);
1329 if (player_status.lives < 0)
1330 player_status.lives = START_LIVES;
1332 lisp_object_t* tux_cur = 0;
1333 if (reader.read_lisp("tux", tux_cur))
1336 std::string back_str = "none";
1337 std::string bonus_str = "none";
1339 LispReader tux_reader(tux_cur);
1340 tux_reader.read_float("x", p.x);
1341 tux_reader.read_float("y", p.y);
1342 tux_reader.read_string("back", back_str);
1343 tux_reader.read_string("bonus", bonus_str);
1345 player_status.bonus = string_to_bonus(bonus_str);
1346 tux->back_direction = string_to_direction(back_str);
1347 tux->set_tile_pos(p);
1350 lisp_object_t* level_cur = 0;
1351 if (reader.read_lisp("levels", level_cur))
1355 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1356 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1358 if (strcmp(lisp_symbol(sym), "level") == 0)
1361 bool solved = false;
1363 LispReader level_reader(data);
1364 level_reader.read_string("name", name);
1365 level_reader.read_bool("solved", solved);
1367 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1369 if (name == i->name)
1372 i->statistics.parse(level_reader);
1378 level_cur = lisp_cdr(level_cur);
1382 lisp_free(savegame);
1384 calculate_total_stats();
1388 WorldMap::loadmap(const std::string& filename)
1391 map_filename = filename;
1395 } // namespace WorldMapNS
1397 /* Local Variables: */