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.
26 #include "app/globals.h"
27 #include "video/surface.h"
28 #include "video/screen.h"
29 #include "video/drawing_context.h"
30 #include "utils/lispreader.h"
31 #include "utils/lispwriter.h"
32 #include "special/frame_rate.h"
34 #include "app/setup.h"
37 #include "audio/sound_manager.h"
38 #include "resources.h"
39 #include "app/gettext.h"
42 #define map_message_TIME 2800
44 Menu* worldmap_menu = 0;
46 namespace WorldMapNS {
48 Direction reverse_dir(Direction direction)
67 direction_to_string(Direction direction)
85 string_to_direction(const std::string& directory)
87 if (directory == "west")
89 else if (directory == "east")
91 else if (directory == "north")
93 else if (directory == "south")
99 TileManager::TileManager()
101 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
102 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
105 Termination::abort("Couldn't load file", stwt_filename);
107 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
109 lisp_object_t* cur = lisp_cdr(root_obj);
111 while(!lisp_nil_p(cur))
113 lisp_object_t* element = lisp_car(cur);
115 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
119 Tile* tile = new Tile;
120 tile->north = tile->east = tile->south = tile->west = true;
122 tile->auto_walk = false;
124 LispReader reader(lisp_cdr(element));
125 reader.read_int("id", id);
128 reader.read_string("possible-directions", temp);
131 tile->north = tile->east = tile->south = tile->west = false;
132 if(temp.find("north") != std::string::npos)
134 if(temp.find("south") != std::string::npos)
136 if(temp.find("east") != std::string::npos)
138 if(temp.find("west") != std::string::npos)
142 /* For backward compatibility */
143 reader.read_bool("north", tile->north);
144 reader.read_bool("south", tile->south);
145 reader.read_bool("west", tile->west);
146 reader.read_bool("east", tile->east);
148 reader.read_bool("stop", tile->stop);
149 reader.read_bool("auto-walk", tile->auto_walk);
151 reader.read_string("one-way", temp);
152 tile->one_way = BOTH_WAYS;
155 if(temp == "north-south")
156 tile->one_way = NORTH_SOUTH_WAY;
157 else if(temp == "south-north")
158 tile->one_way = SOUTH_NORTH_WAY;
159 else if(temp == "east-west")
160 tile->one_way = EAST_WEST_WAY;
161 else if(temp == "west-east")
162 tile->one_way = WEST_EAST_WAY;
165 std::vector<std::string> filenames;
166 reader.read_string_vector("image", filenames);
168 if(filenames.size() == 0)
169 std::cerr << "Warning: no image specified for tile " << id
170 << ".\nIgnoring...\n" << std::endl;
172 for(int i = 0; static_cast<unsigned int>(i) < filenames.size(); i++)
174 Surface* image = new Surface(
175 datadir + "/images/worldmap/" + filenames[i], true);
176 tile->images.push_back(image);
179 tile->anim_speed = 25;
180 reader.read_int("anim-speed", tile->anim_speed);
183 if (id >= int(tiles.size()))
190 puts("Unhandled symbol");
204 TileManager::~TileManager()
206 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
211 TileManager::get(int i)
213 assert(i >=0 && i < int(tiles.size()));
217 //---------------------------------------------------------------------------
219 Tux::Tux(WorldMap* worldmap_)
220 : worldmap(worldmap_)
222 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true);
223 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true);
224 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true);
228 tile_pos.x = worldmap->get_start_x();
229 tile_pos.y = worldmap->get_start_y();
231 input_direction = D_NONE;
236 delete smalltux_sprite;
237 delete firetux_sprite;
238 delete largetux_sprite;
242 Tux::draw(DrawingContext& context, const Vector& offset)
244 Vector pos = get_pos();
245 switch (player_status.bonus)
247 case PlayerStatus::GROWUP_BONUS:
248 context.draw_surface(largetux_sprite,
249 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
251 case PlayerStatus::FLOWER_BONUS:
252 context.draw_surface(firetux_sprite,
253 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
255 case PlayerStatus::NO_BONUS:
256 context.draw_surface(smalltux_sprite,
257 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
266 float x = tile_pos.x * 32;
267 float y = tile_pos.y * 32;
287 return Vector((int)x, (int)y);
295 input_direction = D_NONE;
300 Tux::set_direction(Direction dir)
302 input_direction = dir;
306 Tux::action(float delta)
310 if (input_direction != D_NONE)
312 WorldMap::Level* level = worldmap->at_level();
314 // We got a new direction, so lets start walking when possible
316 if ((!level || level->solved)
317 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
319 tile_pos = next_tile;
321 direction = input_direction;
322 back_direction = reverse_dir(direction);
324 else if (input_direction == back_direction)
327 direction = input_direction;
328 tile_pos = worldmap->get_next_tile(tile_pos, direction);
329 back_direction = reverse_dir(direction);
335 // Let tux walk a few pixels (20 pixel/sec)
336 offset += 20.0f * delta;
339 { // We reached the next tile, so we check what to do now
342 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
343 if(special_tile && special_tile->passive_message)
344 { // direction and the apply_action_ are opposites, since they "see"
345 // directions in a different way
346 if((direction == D_NORTH && special_tile->apply_action_south) ||
347 (direction == D_SOUTH && special_tile->apply_action_north) ||
348 (direction == D_WEST && special_tile->apply_action_east) ||
349 (direction == D_EAST && special_tile->apply_action_west))
351 worldmap->passive_message = special_tile->map_message;
352 worldmap->passive_message_timer.start(map_message_TIME);
356 if (worldmap->at(tile_pos)->stop ||
357 (special_tile && !special_tile->passive_message) ||
358 worldmap->at_level())
360 if(special_tile && !special_tile->map_message.empty() &&
361 !special_tile->passive_message)
362 worldmap->passive_message_timer.stop();
367 if (worldmap->at(tile_pos)->auto_walk || direction != input_direction)
368 { // Turn to a new direction
369 Tile* tile = worldmap->at(tile_pos);
371 if(direction != input_direction &&
372 ((tile->north && input_direction == D_NORTH) ||
373 (tile->south && input_direction == D_SOUTH) ||
374 (tile->east && input_direction == D_EAST) ||
375 (tile->west && input_direction == D_WEST)))
376 { // player has changed direction during auto-movement
377 direction = input_direction;
378 back_direction = reverse_dir(direction);
380 else if(direction != input_direction)
381 { // player has changed to impossible tile
382 back_direction = reverse_dir(direction);
387 Direction dir = D_NONE;
389 if (tile->north && back_direction != D_NORTH)
391 else if (tile->south && back_direction != D_SOUTH)
393 else if (tile->east && back_direction != D_EAST)
395 else if (tile->west && back_direction != D_WEST)
401 input_direction = direction;
402 back_direction = reverse_dir(direction);
406 // Should never be reached if tiledata is good
413 // Walk automatically to the next tile
414 if(direction != D_NONE)
417 if (worldmap->path_ok(direction, tile_pos, &next_tile))
419 tile_pos = next_tile;
423 puts("Tilemap data is buggy");
432 //---------------------------------------------------------------------------
439 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end(); i++)
445 Tile::draw(DrawingContext& context, Vector pos)
447 // same code as from tile_manager.cpp draw_tile()
452 if(images.size() > 1)
455 = ((global_frame_counter*25) / anim_speed) % images.size();
457 context.draw_surface(images[frame], pos, LAYER_TILES);
459 else if (images.size() == 1)
461 context.draw_surface(images[0], pos, LAYER_TILES);
465 //---------------------------------------------------------------------------
469 tile_manager = new TileManager();
470 //tux = new Tux(this);
480 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
481 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
482 messagedot = new Surface(datadir + "/images/worldmap/messagedot.png", true);
483 teleporterdot = new Surface(datadir + "/images/worldmap/teleporterdot.png", true);
488 music = "SALCON.MOD";
490 global_frame_counter = 0;
491 frame_timer.init(true);
496 WorldMap::~WorldMap()
501 delete leveldot_green;
504 delete teleporterdot;
507 // Don't forget to set map_filename before calling this
511 lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
513 Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
515 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
517 lisp_object_t* cur = lisp_cdr(root_obj);
519 while(!lisp_nil_p(cur))
521 lisp_object_t* element = lisp_car(cur);
523 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
525 LispReader reader(lisp_cdr(element));
526 reader.read_int("width", width);
527 reader.read_int("height", height);
528 reader.read_int_vector("data", tilemap);
530 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
532 LispReader reader(lisp_cdr(element));
533 reader.read_string("name", name, true);
534 reader.read_string("music", music);
535 reader.read_int("start_pos_x", start_x);
536 reader.read_int("start_pos_y", start_y);
538 else if (strcmp(lisp_symbol(lisp_car(element)), "special-tiles") == 0 ||
539 strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
541 lisp_object_t* cur = lisp_cdr(element);
543 while(!lisp_nil_p(cur))
545 lisp_object_t* element = lisp_car(cur);
547 if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
549 SpecialTile special_tile;
550 LispReader reader(lisp_cdr(element));
552 reader.read_float("x", special_tile.pos.x);
553 reader.read_float("y", special_tile.pos.y);
555 special_tile.map_message.erase();
556 reader.read_string("map-message", special_tile.map_message);
557 special_tile.passive_message = false;
558 reader.read_bool("passive-message", special_tile.passive_message);
560 special_tile.teleport_dest = Vector(-1,-1);
561 reader.read_float("teleport-to-x", special_tile.teleport_dest.x);
562 reader.read_float("teleport-to-y", special_tile.teleport_dest.y);
564 special_tile.invisible = false;
565 reader.read_bool("invisible-tile", special_tile.invisible);
567 special_tile.apply_action_north = special_tile.apply_action_south =
568 special_tile.apply_action_east = special_tile.apply_action_west =
571 std::string apply_direction;
572 reader.read_string("apply-to-direction", apply_direction);
573 if(!apply_direction.empty())
575 special_tile.apply_action_north = special_tile.apply_action_south =
576 special_tile.apply_action_east = special_tile.apply_action_west =
578 if(apply_direction.find("north") != std::string::npos)
579 special_tile.apply_action_north = true;
580 if(apply_direction.find("south") != std::string::npos)
581 special_tile.apply_action_south = true;
582 if(apply_direction.find("east") != std::string::npos)
583 special_tile.apply_action_east = true;
584 if(apply_direction.find("west") != std::string::npos)
585 special_tile.apply_action_west = true;
588 special_tiles.push_back(special_tile);
591 else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
594 LispReader reader(lisp_cdr(element));
595 level.solved = false;
602 reader.read_string("extro-filename", level.extro_filename);
603 reader.read_string("next-worldmap", level.next_worldmap);
605 level.quit_worldmap = false;
606 reader.read_bool("quit-worldmap", level.quit_worldmap);
608 reader.read_string("name", level.name, true);
609 reader.read_float("x", level.pos.x);
610 reader.read_float("y", level.pos.y);
612 level.auto_path = true;
613 reader.read_bool("auto-path", level.auto_path);
615 level.vertical_flip = false;
616 reader.read_bool("vertical-flip", level.vertical_flip);
618 levels.push_back(level);
639 void WorldMap::get_level_title(Level& level)
641 /** get special_tile's title */
642 level.title = "<no title>";
644 LispReader* reader = LispReader::load(datadir + "/levels/" + level.name, "supertux-level");
647 std::cerr << "Error: Could not open level file. Ignoring...\n";
651 reader->read_string("name", level.title, true);
655 void WorldMap::calculate_total_stats()
658 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
662 total_stats += i->statistics;
668 WorldMap::on_escape_press()
670 // Show or hide the menu
673 Menu::set_current(worldmap_menu);
674 tux->set_direction(D_NONE); // stop tux movement when menu is called
677 Menu::set_current(0);
681 WorldMap::get_input()
687 while (SDL_PollEvent(&event))
691 Menu::current()->event(event);
698 Termination::abort("Received window close", "");
702 key = event.key.keysym.sym;
704 if(key == SDLK_ESCAPE)
706 else if(key == SDLK_RETURN || key == keymap.power)
708 else if(key == SDLK_LEFT || key == keymap.power)
709 tux->set_direction(D_WEST);
710 else if(key == SDLK_RIGHT || key == keymap.right)
711 tux->set_direction(D_EAST);
712 else if(key == SDLK_UP || key == keymap.up ||
714 // there might be ppl that use jump as up key
715 tux->set_direction(D_NORTH);
716 else if(key == SDLK_DOWN || key == keymap.down)
717 tux->set_direction(D_SOUTH);
720 case SDL_JOYHATMOTION:
721 if(event.jhat.value & SDL_HAT_UP) {
722 tux->set_direction(D_NORTH);
723 } else if(event.jhat.value & SDL_HAT_DOWN) {
724 tux->set_direction(D_SOUTH);
725 } else if(event.jhat.value & SDL_HAT_LEFT) {
726 tux->set_direction(D_WEST);
727 } else if(event.jhat.value & SDL_HAT_RIGHT) {
728 tux->set_direction(D_EAST);
732 case SDL_JOYAXISMOTION:
733 if (event.jaxis.axis == joystick_keymap.x_axis)
735 if (event.jaxis.value < -joystick_keymap.dead_zone)
736 tux->set_direction(D_WEST);
737 else if (event.jaxis.value > joystick_keymap.dead_zone)
738 tux->set_direction(D_EAST);
740 else if (event.jaxis.axis == joystick_keymap.y_axis)
742 if (event.jaxis.value > joystick_keymap.dead_zone)
743 tux->set_direction(D_SOUTH);
744 else if (event.jaxis.value < -joystick_keymap.dead_zone)
745 tux->set_direction(D_NORTH);
749 case SDL_JOYBUTTONDOWN:
750 if (event.jbutton.button == joystick_keymap.b_button)
752 else if (event.jbutton.button == joystick_keymap.start_button)
764 WorldMap::get_next_tile(Vector pos, Direction direction)
787 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
789 *new_pos = get_next_tile(old_pos, direction);
791 if (!(new_pos->x >= 0 && new_pos->x < width
792 && new_pos->y >= 0 && new_pos->y < height))
793 { // New position is outsite the tilemap
796 else if(at(*new_pos)->one_way != BOTH_WAYS)
798 std::cerr << "one way only\n";
799 if((at(*new_pos)->one_way == NORTH_SOUTH_WAY && direction != D_SOUTH) ||
800 (at(*new_pos)->one_way == SOUTH_NORTH_WAY && direction != D_NORTH) ||
801 (at(*new_pos)->one_way == EAST_WEST_WAY && direction != D_WEST) ||
802 (at(*new_pos)->one_way == WEST_EAST_WAY && direction != D_EAST))
807 { // Check if we the tile allows us to go to new_pos
811 return (at(old_pos)->west && at(*new_pos)->east);
814 return (at(old_pos)->east && at(*new_pos)->west);
817 return (at(old_pos)->north && at(*new_pos)->south);
820 return (at(old_pos)->south && at(*new_pos)->north);
823 assert(!"path_ok() can't work if direction is NONE");
830 WorldMap::update(float delta)
832 if(!frame_timer.check())
834 frame_timer.start(25);
835 global_frame_counter++;
838 if (enter_level && !tux->is_moving())
840 /* Check special tile action */
841 SpecialTile* special_tile = at_special_tile();
844 if (special_tile->teleport_dest != Vector(-1,-1))
846 // TODO: an animation, camera scrolling or a fading would be a nice touch
847 SoundManager::get()->play_sound(IDToSound(SND_WARP));
848 tux->back_direction = D_NONE;
849 tux->set_tile_pos(special_tile->teleport_dest);
854 /* Check level action */
855 bool level_finished = true;
856 Level* level = at_level();
859 std::cout << "No level to enter at: "
860 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
865 if (level->pos == tux->get_tile_pos())
867 PlayerStatus old_player_status = player_status;
869 std::cout << "Enter the current level: " << level->name << std::endl;
870 // do a shriking fade to the level
871 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),(level->pos.y*32 + 16
873 GameSession session(level->name,
874 ST_GL_LOAD_LEVEL_FILE, level->vertical_flip,
877 switch (session.run())
879 case GameSession::ES_LEVEL_FINISHED:
881 level_finished = true;
882 bool old_level_state = level->solved;
883 level->solved = true;
885 // deal with statistics
886 level->statistics.merge(global_stats);
887 calculate_total_stats();
889 if (session.get_current_sector()->player->got_power !=
890 session.get_current_sector()->player->NONE_POWER)
891 player_status.bonus = PlayerStatus::FLOWER_BONUS;
892 else if (session.get_current_sector()->player->size == BIG)
893 player_status.bonus = PlayerStatus::GROWUP_BONUS;
895 player_status.bonus = PlayerStatus::NO_BONUS;
897 if (old_level_state != level->solved && level->auto_path)
898 { // Try to detect the next direction to which we should walk
899 // FIXME: Mostly a hack
900 Direction dir = D_NONE;
902 Tile* tile = at(tux->get_tile_pos());
904 if (tile->north && tux->back_direction != D_NORTH)
906 else if (tile->south && tux->back_direction != D_SOUTH)
908 else if (tile->east && tux->back_direction != D_EAST)
910 else if (tile->west && tux->back_direction != D_WEST)
915 tux->set_direction(dir);
916 //tux->update(delta);
919 std::cout << "Walk to dir: " << dir << std::endl;
924 case GameSession::ES_LEVEL_ABORT:
925 level_finished = false;
926 /* In case the player's abort the level, keep it using the old
927 status. But the minimum lives and no bonus. */
928 player_status.distros = old_player_status.distros;
929 player_status.lives = std::min(old_player_status.lives, player_status.lives);
930 player_status.bonus = player_status.NO_BONUS;
933 case GameSession::ES_GAME_OVER:
935 level_finished = false;
936 /* draw an end screen */
937 /* TODO: in the future, this should make a dialog a la SuperMario, asking
938 if the player wants to restart the world map with no score and from
942 DrawingContext context;
943 context.draw_gradient(Color (200,240,220), Color(200,200,220),
946 context.draw_text(blue_text, _("GAMEOVER"),
947 Vector(screen->w/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
949 sprintf(str, _("COINS: %d"), player_status.distros);
950 context.draw_text(gold_text, str,
951 Vector(screen->w/2, screen->w - 32), CENTER_ALLIGN, LAYER_FOREGROUND1);
953 total_stats.draw_message_info(context, _("Total Statistics"));
955 context.do_drawing();
958 wait_for_event(event,2000,6000,true);
961 player_status.reset();
964 case GameSession::ES_NONE:
966 // Should never be reached
970 SoundManager::get()->play_music(song);
971 Menu::set_current(0);
972 if (!savegame_file.empty())
973 savegame(savegame_file);
975 /* The porpose of the next checking is that if the player lost
976 the level (in case there is one), don't show anything */
979 if (!level->extro_filename.empty())
981 // Display a text file
982 display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
985 if (!level->next_worldmap.empty())
987 // Load given worldmap
988 loadmap(level->next_worldmap);
990 if (level->quit_worldmap)
997 // tux->set_direction(input_direction);
1000 Menu* menu = Menu::current();
1005 if(menu == worldmap_menu)
1007 switch (worldmap_menu->check())
1009 case MNID_RETURNWORLDMAP: // Return to game
1011 case MNID_QUITWORLDMAP: // Quit Worldmap
1016 else if(menu == options_menu)
1018 process_options_menu();
1024 WorldMap::at(Vector p)
1033 return tile_manager->get(tilemap[width * y + x]);
1037 WorldMap::at_level()
1039 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1041 if (i->pos == tux->get_tile_pos())
1048 WorldMap::SpecialTile*
1049 WorldMap::at_special_tile()
1051 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1053 if (i->pos == tux->get_tile_pos())
1062 WorldMap::draw(DrawingContext& context, const Vector& offset)
1064 for(int y = 0; y < height; ++y)
1065 for(int x = 0; x < width; ++x)
1067 Tile* tile = at(Vector(x, y));
1068 tile->draw(context, Vector(x*32 + offset.x, y*32 + offset.y));
1071 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1074 context.draw_surface(leveldot_green,
1075 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1077 context.draw_surface(leveldot_red,
1078 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1081 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1086 if (i->teleport_dest != Vector(-1, -1))
1087 context.draw_surface(teleporterdot,
1088 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1090 else if (!i->map_message.empty() && !i->passive_message)
1091 context.draw_surface(messagedot,
1092 Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
1095 tux->draw(context, offset);
1096 draw_status(context);
1100 WorldMap::draw_status(DrawingContext& context)
1103 sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
1105 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
1106 context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
1108 sprintf(str, "%d", player_status.distros);
1109 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
1110 LEFT_ALLIGN, LAYER_FOREGROUND1);
1111 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
1112 LEFT_ALLIGN, LAYER_FOREGROUND1);
1114 if (player_status.lives >= 5)
1116 sprintf(str, "%dx", player_status.lives);
1117 context.draw_text(gold_text, str,
1118 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
1119 LEFT_ALLIGN, LAYER_FOREGROUND1);
1120 context.draw_surface(tux_life, Vector(screen->w -
1121 gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
1125 for(int i= 0; i < player_status.lives; ++i)
1126 context.draw_surface(tux_life,
1127 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
1130 context.draw_text(white_text, _("LIVES"),
1131 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
1132 LEFT_ALLIGN, LAYER_FOREGROUND1);
1134 if (!tux->is_moving())
1136 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1138 if (i->pos == tux->get_tile_pos())
1141 get_level_title(*i);
1143 context.draw_text(white_text, i->title,
1145 screen->h - white_text->get_height() - 30),
1146 CENTER_ALLIGN, LAYER_FOREGROUND1);
1148 i->statistics.draw_worldmap_info(context);
1152 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1154 if (i->pos == tux->get_tile_pos())
1156 /* Display an in-map message in the map, if any as been selected */
1157 if(!i->map_message.empty() && !i->passive_message)
1158 context.draw_text(gold_text, i->map_message,
1160 screen->h - white_text->get_height() - 60),
1161 CENTER_ALLIGN, LAYER_FOREGROUND1);
1166 /* Display a passive message in the map, if needed */
1167 if(passive_message_timer.check())
1168 context.draw_text(gold_text, passive_message,
1169 Vector(screen->w/2, screen->h - white_text->get_height() - 60),
1170 CENTER_ALLIGN, LAYER_FOREGROUND1);
1176 Menu::set_current(0);
1180 song = SoundManager::get()->load_music(datadir + "/music/" + music);
1181 SoundManager::get()->play_music(song);
1183 FrameRate frame_rate(10);
1184 frame_rate.set_frame_limit(false);
1188 DrawingContext context;
1191 float delta = frame_rate.get();
1198 frame_rate.update();
1200 Vector tux_pos = tux->get_pos();
1203 offset.x = -tux_pos.x + screen->w/2;
1204 offset.y = -tux_pos.y + screen->h/2;
1206 if (offset.x > 0) offset.x = 0;
1207 if (offset.y > 0) offset.y = 0;
1209 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
1210 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
1213 draw(context, offset);
1219 Menu::current()->draw(context);
1220 mouse_cursor->draw(context);
1223 context.do_drawing();
1230 WorldMap::savegame(const std::string& filename)
1235 std::cout << "savegame: " << filename << std::endl;
1237 std::ofstream file(filename.c_str(), std::ios::out);
1238 LispWriter* writer = new LispWriter(file);
1240 int nb_solved_levels = 0, total_levels = 0;
1241 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1247 char nb_solved_levels_str[80], total_levels_str[80];
1248 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1249 sprintf(total_levels_str, "%d", total_levels);
1251 writer->write_comment("Worldmap save file");
1253 writer->start_list("supertux-savegame");
1255 writer->write_int("version", 1);
1256 writer->write_string("title", std::string(name + " - " + nb_solved_levels_str + "/" + total_levels_str));
1257 writer->write_string("map", map_filename);
1258 writer->write_int("lives", player_status.lives);
1259 writer->write_int("distros", player_status.lives);
1260 writer->write_int("max-score-multiplier", player_status.max_score_multiplier);
1262 writer->start_list("tux");
1264 writer->write_float("x", tux->get_tile_pos().x);
1265 writer->write_float("y", tux->get_tile_pos().y);
1266 writer->write_string("back", direction_to_string(tux->back_direction));
1267 writer->write_string("bonus", bonus_to_string(player_status.bonus));
1269 writer->end_list("tux");
1271 writer->start_list("levels");
1273 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1277 writer->start_list("level");
1279 writer->write_string("name", i->name);
1280 writer->write_bool("solved", true);
1281 i->statistics.write(*writer);
1283 writer->end_list("level");
1287 writer->end_list("levels");
1289 writer->end_list("supertux-savegame");
1293 WorldMap::loadgame(const std::string& filename)
1295 std::cout << "loadgame: " << filename << std::endl;
1296 savegame_file = filename;
1298 if (access(filename.c_str(), F_OK) != 0)
1302 player_status.reset();
1307 lisp_object_t* savegame = lisp_read_from_file(filename);
1310 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1315 lisp_object_t* cur = savegame;
1317 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1323 cur = lisp_cdr(cur);
1324 LispReader reader(cur);
1326 /* Get the Map filename and then load it before setting level settings */
1327 std::string cur_map_filename = map_filename;
1328 reader.read_string("map", map_filename);
1329 // if(cur_map_filename != map_filename)
1332 reader.read_int("lives", player_status.lives);
1333 reader.read_int("distros", player_status.distros);
1334 reader.read_int("max-score-multiplier", player_status.max_score_multiplier);
1336 if (player_status.lives < 0)
1337 player_status.lives = START_LIVES;
1339 lisp_object_t* tux_cur = 0;
1340 if (reader.read_lisp("tux", tux_cur))
1343 std::string back_str = "none";
1344 std::string bonus_str = "none";
1346 LispReader tux_reader(tux_cur);
1347 tux_reader.read_float("x", p.x);
1348 tux_reader.read_float("y", p.y);
1349 tux_reader.read_string("back", back_str);
1350 tux_reader.read_string("bonus", bonus_str);
1352 player_status.bonus = string_to_bonus(bonus_str);
1353 tux->back_direction = string_to_direction(back_str);
1354 tux->set_tile_pos(p);
1357 lisp_object_t* level_cur = 0;
1358 if (reader.read_lisp("levels", level_cur))
1362 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1363 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1365 if (strcmp(lisp_symbol(sym), "level") == 0)
1368 bool solved = false;
1370 LispReader level_reader(data);
1371 level_reader.read_string("name", name);
1372 level_reader.read_bool("solved", solved);
1374 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1376 if (name == i->name)
1379 i->statistics.parse(level_reader);
1385 level_cur = lisp_cdr(level_cur);
1389 lisp_free(savegame);
1391 calculate_total_stats();
1395 WorldMap::loadmap(const std::string& filename)
1398 map_filename = filename;
1402 } // namespace WorldMapNS
1404 /* Local Variables: */