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)
118 std::string filename = "<invalid>";
120 Tile* tile = new Tile;
121 tile->north = tile->east = tile->south = tile->west = true;
123 tile->auto_walk = false;
125 LispReader reader(lisp_cdr(element));
126 reader.read_int("id", id);
129 reader.read_string("possible-directions", temp);
132 tile->north = tile->east = tile->south = tile->west = false;
133 if(temp.find("north") != std::string::npos)
135 if(temp.find("south") != std::string::npos)
137 if(temp.find("east") != std::string::npos)
139 if(temp.find("west") != std::string::npos)
143 /* For backward compatibility */
144 reader.read_bool("north", tile->north);
145 reader.read_bool("south", tile->south);
146 reader.read_bool("west", tile->west);
147 reader.read_bool("east", tile->east);
149 reader.read_bool("stop", tile->stop);
150 reader.read_bool("auto-walk", tile->auto_walk);
151 reader.read_string("image", filename);
153 reader.read_string("one-way", temp);
154 tile->one_way = BOTH_WAYS;
157 if(temp == "north-south")
158 tile->one_way = NORTH_SOUTH_WAY;
159 else if(temp == "south-north")
160 tile->one_way = SOUTH_NORTH_WAY;
161 else if(temp == "east-west")
162 tile->one_way = EAST_WEST_WAY;
163 else if(temp == "west-east")
164 tile->one_way = WEST_EAST_WAY;
167 tile->sprite = new Surface(
168 datadir + "/images/worldmap/" + filename,
171 if (id >= int(tiles.size()))
178 puts("Unhandled symbol");
192 TileManager::~TileManager()
194 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
199 TileManager::get(int i)
201 assert(i >=0 && i < int(tiles.size()));
205 //---------------------------------------------------------------------------
207 Tux::Tux(WorldMap* worldmap_)
208 : worldmap(worldmap_)
210 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true);
211 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true);
212 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true);
216 tile_pos.x = worldmap->get_start_x();
217 tile_pos.y = worldmap->get_start_y();
219 input_direction = D_NONE;
224 delete smalltux_sprite;
225 delete firetux_sprite;
226 delete largetux_sprite;
230 Tux::draw(DrawingContext& context, const Vector& offset)
232 Vector pos = get_pos();
233 switch (player_status.bonus)
235 case PlayerStatus::GROWUP_BONUS:
236 context.draw_surface(largetux_sprite,
237 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
239 case PlayerStatus::FLOWER_BONUS:
240 context.draw_surface(firetux_sprite,
241 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
243 case PlayerStatus::NO_BONUS:
244 context.draw_surface(smalltux_sprite,
245 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
254 float x = tile_pos.x * 32;
255 float y = tile_pos.y * 32;
275 return Vector((int)x, (int)y);
283 input_direction = D_NONE;
288 Tux::set_direction(Direction dir)
290 input_direction = dir;
294 Tux::action(float delta)
298 if (input_direction != D_NONE)
300 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
302 // We got a new direction, so lets start walking when possible
304 if ((!special_tile || special_tile->solved || special_tile->level_name.empty())
305 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
307 tile_pos = next_tile;
309 direction = input_direction;
310 back_direction = reverse_dir(direction);
312 else if (input_direction == back_direction)
315 direction = input_direction;
316 tile_pos = worldmap->get_next_tile(tile_pos, direction);
317 back_direction = reverse_dir(direction);
323 // Let tux walk a few pixels (20 pixel/sec)
324 offset += 20.0f * delta;
327 { // We reached the next tile, so we check what to do now
330 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
331 if(special_tile && special_tile->passive_message)
332 { // direction and the apply_action_ are opposites, since they "see"
333 // directions in a different way
334 if((direction == D_NORTH && special_tile->apply_action_south) ||
335 (direction == D_SOUTH && special_tile->apply_action_north) ||
336 (direction == D_WEST && special_tile->apply_action_east) ||
337 (direction == D_EAST && special_tile->apply_action_west))
339 worldmap->passive_message = special_tile->map_message;
340 worldmap->passive_message_timer.start(map_message_TIME);
344 if (worldmap->at(tile_pos)->stop || (special_tile &&
345 !special_tile->passive_message))
347 if(special_tile && !special_tile->map_message.empty() &&
348 !special_tile->passive_message)
349 worldmap->passive_message_timer.stop();
354 if (worldmap->at(tile_pos)->auto_walk || direction != input_direction)
355 { // Turn to a new direction
356 Tile* tile = worldmap->at(tile_pos);
358 if(direction != input_direction &&
359 ((tile->north && input_direction == D_NORTH) ||
360 (tile->south && input_direction == D_SOUTH) ||
361 (tile->east && input_direction == D_EAST) ||
362 (tile->west && input_direction == D_WEST)))
363 { // player has changed direction during auto-movement
364 direction = input_direction;
365 back_direction = reverse_dir(direction);
367 else if(direction != input_direction)
368 { // player has changed to impossible tile
369 back_direction = reverse_dir(direction);
374 Direction dir = D_NONE;
376 if (tile->north && back_direction != D_NORTH)
378 else if (tile->south && back_direction != D_SOUTH)
380 else if (tile->east && back_direction != D_EAST)
382 else if (tile->west && back_direction != D_WEST)
388 input_direction = direction;
389 back_direction = reverse_dir(direction);
393 // Should never be reached if tiledata is good
400 // Walk automatically to the next tile
401 if(direction != D_NONE)
404 if (worldmap->path_ok(direction, tile_pos, &next_tile))
406 tile_pos = next_tile;
410 puts("Tilemap data is buggy");
419 //---------------------------------------------------------------------------
429 //---------------------------------------------------------------------------
433 tile_manager = new TileManager();
434 //tux = new Tux(this);
442 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
443 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
444 messagedot = new Surface(datadir + "/images/worldmap/messagedot.png", true);
445 teleporterdot = new Surface(datadir + "/images/worldmap/teleporterdot.png", true);
450 music = "SALCON.MOD";
453 WorldMap::~WorldMap()
458 delete leveldot_green;
461 delete teleporterdot;
467 lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
469 Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
471 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
473 lisp_object_t* cur = lisp_cdr(root_obj);
475 while(!lisp_nil_p(cur))
477 lisp_object_t* element = lisp_car(cur);
479 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
481 LispReader reader(lisp_cdr(element));
482 reader.read_int("width", width);
483 reader.read_int("height", height);
484 reader.read_int_vector("data", tilemap);
486 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
488 LispReader reader(lisp_cdr(element));
489 reader.read_string("name", name, true);
490 reader.read_string("music", music);
491 reader.read_int("start_pos_x", start_x);
492 reader.read_int("start_pos_y", start_y);
494 else if (strcmp(lisp_symbol(lisp_car(element)), "special-tiles") == 0 ||
495 strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
497 lisp_object_t* cur = lisp_cdr(element);
499 while(!lisp_nil_p(cur))
501 lisp_object_t* element = lisp_car(cur);
503 if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
505 SpecialTile special_tile;
506 LispReader reader(lisp_cdr(element));
507 special_tile.solved = false;
509 special_tile.north = true;
510 special_tile.east = true;
511 special_tile.south = true;
512 special_tile.west = true;
514 reader.read_int("x", special_tile.x);
515 reader.read_int("y", special_tile.y);
516 reader.read_string("level", special_tile.level_name, false);
518 special_tile.vertical_flip = false;
519 reader.read_bool("vertical-flip", special_tile.vertical_flip);
521 special_tile.map_message.erase();
522 reader.read_string("map-message", special_tile.map_message);
523 special_tile.passive_message = false;
524 reader.read_bool("passive-message", special_tile.passive_message);
526 special_tile.teleport_dest_x = special_tile.teleport_dest_y = -1;
527 reader.read_int("teleport-to-x", special_tile.teleport_dest_x);
528 reader.read_int("teleport-to-y", special_tile.teleport_dest_y);
530 special_tile.invisible = false;
531 reader.read_bool("invisible-tile", special_tile.invisible);
533 special_tile.apply_action_north = special_tile.apply_action_south =
534 special_tile.apply_action_east = special_tile.apply_action_west =
536 std::string apply_direction;
537 reader.read_string("apply-to-direction", apply_direction);
538 if(!apply_direction.empty())
540 special_tile.apply_action_north = special_tile.apply_action_south =
541 special_tile.apply_action_east = special_tile.apply_action_west =
543 if(apply_direction.find("north") != std::string::npos)
544 special_tile.apply_action_north = true;
545 if(apply_direction.find("south") != std::string::npos)
546 special_tile.apply_action_south = true;
547 if(apply_direction.find("east") != std::string::npos)
548 special_tile.apply_action_east = true;
549 if(apply_direction.find("west") != std::string::npos)
550 special_tile.apply_action_west = true;
553 reader.read_string("extro-filename", special_tile.extro_filename);
554 reader.read_string("next-world", special_tile.next_worldmap);
555 special_tile.quit_worldmap = false;
556 reader.read_bool("exit-game", special_tile.quit_worldmap);
558 special_tile.auto_path = true;
559 reader.read_bool("auto-path", special_tile.auto_path);
561 special_tiles.push_back(special_tile);
564 /* Kept for backward compability */
565 else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
567 SpecialTile special_tile;
568 LispReader reader(lisp_cdr(element));
569 special_tile.solved = false;
571 special_tile.north = true;
572 special_tile.east = true;
573 special_tile.south = true;
574 special_tile.west = true;
576 special_tile.invisible = false;
578 special_tile.apply_action_north = special_tile.apply_action_south =
579 special_tile.apply_action_east = special_tile.apply_action_west =
581 special_tile.vertical_flip = false;
582 special_tile.teleport_dest_x = special_tile.teleport_dest_y = -1;
584 reader.read_string("extro-filename", special_tile.extro_filename);
585 if(!special_tile.extro_filename.empty())
586 special_tile.quit_worldmap = true;
587 reader.read_string("name", special_tile.level_name, true);
588 reader.read_int("x", special_tile.x);
589 reader.read_int("y", special_tile.y);
592 special_tiles.push_back(special_tile);
611 void WorldMap::get_level_title(SpecialTile& special_tile)
613 /** get special_tile's title */
614 special_tile.title = "<no title>";
616 LispReader* reader = LispReader::load(datadir + "/levels/" + special_tile.level_name, "supertux-level");
619 std::cerr << "Error: Could not open level file. Ignoring...\n";
623 reader->read_string("name", special_tile.title, true);
627 void WorldMap::calculate_total_stats()
630 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
632 if (!i->level_name.empty() && i->solved)
634 total_stats += i->statistics;
640 WorldMap::on_escape_press()
642 // Show or hide the menu
645 Menu::set_current(worldmap_menu);
646 tux->set_direction(D_NONE); // stop tux movement when menu is called
649 Menu::set_current(0);
653 WorldMap::get_input()
658 while (SDL_PollEvent(&event))
662 Menu::current()->event(event);
669 Termination::abort("Received window close", "");
673 switch(event.key.keysym.sym)
684 tux->set_direction(D_WEST);
687 tux->set_direction(D_EAST);
690 tux->set_direction(D_NORTH);
693 tux->set_direction(D_SOUTH);
701 case SDL_JOYHATMOTION:
702 if(event.jhat.value & SDL_HAT_UP) {
703 tux->set_direction(D_NORTH);
704 } else if(event.jhat.value & SDL_HAT_DOWN) {
705 tux->set_direction(D_SOUTH);
706 } else if(event.jhat.value & SDL_HAT_LEFT) {
707 tux->set_direction(D_WEST);
708 } else if(event.jhat.value & SDL_HAT_RIGHT) {
709 tux->set_direction(D_EAST);
713 case SDL_JOYAXISMOTION:
714 if (event.jaxis.axis == joystick_keymap.x_axis)
716 if (event.jaxis.value < -joystick_keymap.dead_zone)
717 tux->set_direction(D_WEST);
718 else if (event.jaxis.value > joystick_keymap.dead_zone)
719 tux->set_direction(D_EAST);
721 else if (event.jaxis.axis == joystick_keymap.y_axis)
723 if (event.jaxis.value > joystick_keymap.dead_zone)
724 tux->set_direction(D_SOUTH);
725 else if (event.jaxis.value < -joystick_keymap.dead_zone)
726 tux->set_direction(D_NORTH);
730 case SDL_JOYBUTTONDOWN:
731 if (event.jbutton.button == joystick_keymap.b_button)
733 else if (event.jbutton.button == joystick_keymap.start_button)
745 WorldMap::get_next_tile(Vector pos, Direction direction)
768 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
770 *new_pos = get_next_tile(old_pos, direction);
772 if (!(new_pos->x >= 0 && new_pos->x < width
773 && new_pos->y >= 0 && new_pos->y < height))
774 { // New position is outsite the tilemap
777 else if(at(*new_pos)->one_way != BOTH_WAYS)
779 std::cerr << "one way only\n";
780 if((at(*new_pos)->one_way == NORTH_SOUTH_WAY && direction != D_SOUTH) ||
781 (at(*new_pos)->one_way == SOUTH_NORTH_WAY && direction != D_NORTH) ||
782 (at(*new_pos)->one_way == EAST_WEST_WAY && direction != D_WEST) ||
783 (at(*new_pos)->one_way == WEST_EAST_WAY && direction != D_EAST))
788 { // Check if we the tile allows us to go to new_pos
792 return (at(old_pos)->west && at(*new_pos)->east);
795 return (at(old_pos)->east && at(*new_pos)->west);
798 return (at(old_pos)->north && at(*new_pos)->south);
801 return (at(old_pos)->south && at(*new_pos)->north);
804 assert(!"path_ok() can't work if direction is NONE");
811 WorldMap::update(float delta)
813 if (enter_level && !tux->is_moving())
815 bool level_finished = true;
816 SpecialTile* special_tile = at_special_tile();
819 std::cout << "Nothing to enter at: "
820 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
825 if(!special_tile->level_name.empty())
827 if (special_tile->x == tux->get_tile_pos().x &&
828 special_tile->y == tux->get_tile_pos().y)
830 PlayerStatus old_player_status = player_status;
832 std::cout << "Enter the current level: " << special_tile->level_name << std::endl;
833 // do a shriking fade to the special_tile
834 shrink_fade(Vector((special_tile->x*32 + 16 + offset.x),(special_tile->y*32 + 16
836 GameSession session(datadir + "/levels/" + special_tile->level_name,
837 ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip);
839 switch (session.run())
841 case GameSession::ES_LEVEL_FINISHED:
843 level_finished = true;
844 bool old_level_state = special_tile->solved;
845 special_tile->solved = true;
847 // deal with statistics
848 special_tile->statistics.merge(global_stats);
849 calculate_total_stats();
851 if (session.get_current_sector()->player->got_power !=
852 session.get_current_sector()->player->NONE_POWER)
853 player_status.bonus = PlayerStatus::FLOWER_BONUS;
854 else if (session.get_current_sector()->player->size == BIG)
855 player_status.bonus = PlayerStatus::GROWUP_BONUS;
857 player_status.bonus = PlayerStatus::NO_BONUS;
859 if (old_level_state != special_tile->solved && special_tile->auto_path)
860 { // Try to detect the next direction to which we should walk
861 // FIXME: Mostly a hack
862 Direction dir = D_NONE;
864 Tile* tile = at(tux->get_tile_pos());
866 if (tile->north && tux->back_direction != D_NORTH)
868 else if (tile->south && tux->back_direction != D_SOUTH)
870 else if (tile->east && tux->back_direction != D_EAST)
872 else if (tile->west && tux->back_direction != D_WEST)
877 tux->set_direction(dir);
878 //tux->update(delta);
881 std::cout << "Walk to dir: " << dir << std::endl;
886 case GameSession::ES_LEVEL_ABORT:
887 level_finished = false;
888 /* In case the player's abort the special_tile, keep it using the old
889 status. But the minimum lives and no bonus. */
890 player_status.distros = old_player_status.distros;
891 player_status.lives = std::min(old_player_status.lives, player_status.lives);
892 player_status.bonus = player_status.NO_BONUS;
895 case GameSession::ES_GAME_OVER:
897 level_finished = false;
898 /* draw an end screen */
899 /* TODO: in the future, this should make a dialog a la SuperMario, asking
900 if the player wants to restart the world map with no score and from
904 DrawingContext context;
905 context.draw_gradient(Color (200,240,220), Color(200,200,220),
908 context.draw_text_center(blue_text, _("GAMEOVER"),
909 Vector(0, 200), LAYER_FOREGROUND1);
911 sprintf(str, _("SCORE: %d"), total_stats.get_points(SCORE_STAT));
912 context.draw_text_center(gold_text, str,
913 Vector(0, 230), LAYER_FOREGROUND1);
915 sprintf(str, _("COINS: %d"), player_status.distros);
916 context.draw_text_center(gold_text, str,
917 Vector(0, screen->w - 32), LAYER_FOREGROUND1);
919 context.do_drawing();
922 wait_for_event(event,2000,5000,true);
925 player_status.reset();
928 case GameSession::ES_NONE:
930 // Should never be reached
934 SoundManager::get()->play_music(song);
935 Menu::set_current(0);
936 if (!savegame_file.empty())
937 savegame(savegame_file);
940 /* The porpose of the next checking is that if the player lost
941 the special_tile (in case there is one), don't show anything */
944 if (!special_tile->extro_filename.empty())
946 // Display a text file
947 display_text_file(special_tile->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
949 if (special_tile->teleport_dest_x != -1 && special_tile->teleport_dest_y != -1)
951 // TODO: an animation, camera scrolling or a fading would be a nice touch
952 SoundManager::get()->play_sound(IDToSound(SND_WARP));
953 tux->back_direction = D_NONE;
954 tux->set_tile_pos(Vector(special_tile->teleport_dest_x, special_tile->teleport_dest_y));
957 if (!special_tile->next_worldmap.empty())
959 // Load given worldmap
960 loadmap(special_tile->next_worldmap);
962 if (special_tile->quit_worldmap)
969 // tux->set_direction(input_direction);
972 Menu* menu = Menu::current();
977 if(menu == worldmap_menu)
979 switch (worldmap_menu->check())
981 case MNID_RETURNWORLDMAP: // Return to game
983 case MNID_QUITWORLDMAP: // Quit Worldmap
988 else if(menu == options_menu)
990 process_options_menu();
996 WorldMap::at(Vector p)
1005 return tile_manager->get(tilemap[width * y + x]);
1008 WorldMap::SpecialTile*
1009 WorldMap::at_special_tile()
1011 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1013 if (i->x == tux->get_tile_pos().x &&
1014 i->y == tux->get_tile_pos().y)
1023 WorldMap::draw(DrawingContext& context, const Vector& offset)
1025 for(int y = 0; y < height; ++y)
1026 for(int x = 0; x < width; ++x)
1028 Tile* tile = at(Vector(x, y));
1029 context.draw_surface(tile->sprite,
1030 Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
1033 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1037 if(i->level_name.empty())
1039 if (i->teleport_dest_x != -1 && i->teleport_dest_y != -1)
1040 context.draw_surface(teleporterdot,
1041 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1043 else if (!i->map_message.empty() && !i->passive_message)
1044 context.draw_surface(messagedot,
1045 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1051 context.draw_surface(leveldot_green,
1052 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1054 context.draw_surface(leveldot_red,
1055 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1058 tux->draw(context, offset);
1059 draw_status(context);
1063 WorldMap::draw_status(DrawingContext& context)
1066 sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
1068 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
1069 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
1071 sprintf(str, "%d", player_status.distros);
1072 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
1074 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
1077 if (player_status.lives >= 5)
1079 sprintf(str, "%dx", player_status.lives);
1080 context.draw_text(gold_text, str,
1081 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
1083 context.draw_surface(tux_life, Vector(screen->w -
1084 gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
1088 for(int i= 0; i < player_status.lives; ++i)
1089 context.draw_surface(tux_life,
1090 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
1093 context.draw_text(white_text, _("LIVES"),
1094 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
1097 if (!tux->is_moving())
1099 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1101 if (i->x == tux->get_tile_pos().x &&
1102 i->y == tux->get_tile_pos().y)
1104 if(!i->level_name.empty())
1107 get_level_title(*i);
1109 context.draw_text_center(white_text, i->title,
1110 Vector(0, screen->h - white_text->get_height() - 30),
1114 /* Display an in-map message in the map, if any as been selected */
1115 if(!i->map_message.empty() && !i->passive_message)
1116 context.draw_text_center(gold_text, i->map_message,
1117 Vector(0, screen->h - white_text->get_height() - 60),
1123 /* Display a passive message in the map, if needed */
1124 if(passive_message_timer.check())
1125 context.draw_text_center(gold_text, passive_message,
1126 Vector(0, screen->h - white_text->get_height() - 60),
1133 Menu::set_current(0);
1137 song = SoundManager::get()->load_music(datadir + "/music/" + music);
1138 SoundManager::get()->play_music(song);
1140 FrameRate frame_rate(10);
1141 frame_rate.set_frame_limit(false);
1145 DrawingContext context;
1148 float delta = frame_rate.get();
1155 frame_rate.update();
1157 Vector tux_pos = tux->get_pos();
1160 offset.x = -tux_pos.x + screen->w/2;
1161 offset.y = -tux_pos.y + screen->h/2;
1163 if (offset.x > 0) offset.x = 0;
1164 if (offset.y > 0) offset.y = 0;
1166 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
1167 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
1170 draw(context, offset);
1176 Menu::current()->draw(context);
1177 mouse_cursor->draw(context);
1180 context.do_drawing();
1187 WorldMap::savegame(const std::string& filename)
1192 std::cout << "savegame: " << filename << std::endl;
1194 std::ofstream file(filename.c_str(), std::ios::out);
1195 LispWriter* writer = new LispWriter(file);
1197 int nb_solved_levels = 0, total_levels = 0;
1198 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1200 if(!i->level_name.empty())
1205 char nb_solved_levels_str[80], total_levels_str[80];
1206 sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
1207 sprintf(total_levels_str, "%d", total_levels);
1209 writer->write_comment("Worldmap save file");
1211 writer->start_list("supertux-savegame");
1213 writer->write_int("version", 1);
1214 writer->write_string("title", std::string(name + " - " + nb_solved_levels_str + "/" + total_levels_str));
1215 writer->write_string("map", map_filename);
1216 writer->write_int("lives", player_status.lives);
1217 writer->write_int("distros", player_status.lives);
1219 writer->start_list("tux");
1221 writer->write_float("x", tux->get_tile_pos().x);
1222 writer->write_float("y", tux->get_tile_pos().y);
1223 writer->write_string("back", direction_to_string(tux->back_direction));
1224 writer->write_string("bonus", bonus_to_string(player_status.bonus));
1226 writer->end_list("tux");
1228 writer->start_list("levels");
1230 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1232 if (i->solved && !i->level_name.empty())
1234 writer->start_list("level");
1236 writer->write_string("name", i->level_name);
1237 writer->write_bool("solved", true);
1238 i->statistics.write(*writer);
1240 writer->end_list("level");
1244 writer->end_list("levels");
1246 writer->end_list("supertux-savegame");
1250 WorldMap::loadgame(const std::string& filename)
1252 std::cout << "loadgame: " << filename << std::endl;
1253 savegame_file = filename;
1254 map_filename = "icyisland.stwm";
1256 if (access(filename.c_str(), F_OK) != 0)
1262 lisp_object_t* savegame = lisp_read_from_file(filename);
1265 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1270 lisp_object_t* cur = savegame;
1272 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1278 cur = lisp_cdr(cur);
1279 LispReader reader(cur);
1281 /* Get the Map filename and then load it before setting special_tile settings */
1282 reader.read_string("map", map_filename);
1285 reader.read_int("lives", player_status.lives);
1286 reader.read_int("distros", player_status.distros);
1288 if (player_status.lives < 0)
1289 player_status.lives = START_LIVES;
1291 lisp_object_t* tux_cur = 0;
1292 if (reader.read_lisp("tux", tux_cur))
1295 std::string back_str = "none";
1296 std::string bonus_str = "none";
1298 LispReader tux_reader(tux_cur);
1299 tux_reader.read_float("x", p.x);
1300 tux_reader.read_float("y", p.y);
1301 tux_reader.read_string("back", back_str);
1302 tux_reader.read_string("bonus", bonus_str);
1304 player_status.bonus = string_to_bonus(bonus_str);
1305 tux->back_direction = string_to_direction(back_str);
1306 tux->set_tile_pos(p);
1309 lisp_object_t* level_cur = 0;
1310 if (reader.read_lisp("levels", level_cur))
1314 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1315 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1317 if (strcmp(lisp_symbol(sym), "level") == 0)
1320 bool solved = false;
1322 LispReader level_reader(data);
1323 level_reader.read_string("name", name);
1324 level_reader.read_bool("solved", solved);
1326 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1328 if (name == i->level_name)
1331 i->statistics.parse(level_reader);
1337 level_cur = lisp_cdr(level_cur);
1341 lisp_free(savegame);
1343 calculate_total_stats();
1347 WorldMap::loadmap(const std::string& filename)
1350 map_filename = filename;
1354 } // namespace WorldMapNS
1356 /* Local Variables: */