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"
32 #include "app/setup.h"
35 #include "audio/sound_manager.h"
36 #include "resources.h"
37 #include "app/gettext.h"
40 Menu* worldmap_menu = 0;
42 namespace WorldMapNS {
44 Direction reverse_dir(Direction direction)
63 direction_to_string(Direction direction)
81 string_to_direction(const std::string& directory)
83 if (directory == "west")
85 else if (directory == "east")
87 else if (directory == "north")
89 else if (directory == "south")
95 TileManager::TileManager()
97 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
98 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
101 Termination::abort("Couldn't load file", stwt_filename);
103 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
105 lisp_object_t* cur = lisp_cdr(root_obj);
107 while(!lisp_nil_p(cur))
109 lisp_object_t* element = lisp_car(cur);
111 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
114 std::string filename = "<invalid>";
116 Tile* tile = new Tile;
122 tile->auto_walk = false;
124 LispReader reader(lisp_cdr(element));
125 reader.read_int("id", id);
126 reader.read_bool("north", tile->north);
127 reader.read_bool("south", tile->south);
128 reader.read_bool("west", tile->west);
129 reader.read_bool("east", tile->east);
130 reader.read_bool("stop", tile->stop);
131 reader.read_bool("auto-walk", tile->auto_walk);
132 reader.read_string("image", filename);
134 tile->sprite = new Surface(
135 datadir + "/images/worldmap/" + filename,
138 if (id >= int(tiles.size()))
145 puts("Unhandled symbol");
159 TileManager::~TileManager()
161 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
166 TileManager::get(int i)
168 assert(i >=0 && i < int(tiles.size()));
172 //---------------------------------------------------------------------------
174 Tux::Tux(WorldMap* worldmap_)
175 : worldmap(worldmap_)
177 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true);
178 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true);
179 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true);
183 tile_pos.x = worldmap->get_start_x();
184 tile_pos.y = worldmap->get_start_y();
186 input_direction = D_NONE;
191 delete smalltux_sprite;
192 delete firetux_sprite;
193 delete largetux_sprite;
197 Tux::draw(DrawingContext& context, const Vector& offset)
199 Vector pos = get_pos();
200 switch (player_status.bonus)
202 case PlayerStatus::GROWUP_BONUS:
203 context.draw_surface(largetux_sprite,
204 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
206 case PlayerStatus::FLOWER_BONUS:
207 context.draw_surface(firetux_sprite,
208 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
210 case PlayerStatus::NO_BONUS:
211 context.draw_surface(smalltux_sprite,
212 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
221 float x = tile_pos.x * 32;
222 float y = tile_pos.y * 32;
242 return Vector((int)x, (int)y);
254 Tux::action(float delta)
258 if (input_direction != D_NONE)
260 WorldMap::Level* level = worldmap->at_level();
262 // We got a new direction, so lets start walking when possible
264 if ((!level || level->solved)
265 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
267 tile_pos = next_tile;
269 direction = input_direction;
270 back_direction = reverse_dir(direction);
272 else if (input_direction == back_direction)
274 std::cout << "Back triggered" << std::endl;
276 direction = input_direction;
277 tile_pos = worldmap->get_next_tile(tile_pos, direction);
278 back_direction = reverse_dir(direction);
284 // Let tux walk a few pixels (20 pixel/sec)
285 offset += 20.0f * delta;
288 { // We reached the next tile, so we check what to do now
291 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
297 if (worldmap->at(tile_pos)->auto_walk)
298 { // Turn to a new direction
299 Tile* tile = worldmap->at(tile_pos);
300 Direction dir = D_NONE;
302 if (tile->north && back_direction != D_NORTH)
304 else if (tile->south && back_direction != D_SOUTH)
306 else if (tile->east && back_direction != D_EAST)
308 else if (tile->west && back_direction != D_WEST)
314 back_direction = reverse_dir(direction);
318 // Should never be reached if tiledata is good
324 // Walk automatically to the next tile
326 if (worldmap->path_ok(direction, tile_pos, &next_tile))
328 tile_pos = next_tile;
332 puts("Tilemap data is buggy");
340 //---------------------------------------------------------------------------
350 //---------------------------------------------------------------------------
354 tile_manager = new TileManager();
355 //tux = new Tux(this);
363 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", true);
364 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
365 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
367 input_direction = D_NONE;
371 music = "SALCON.MOD";
374 WorldMap::~WorldMap()
380 delete leveldot_green;
387 lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
389 Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
391 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
393 lisp_object_t* cur = lisp_cdr(root_obj);
395 while(!lisp_nil_p(cur))
397 lisp_object_t* element = lisp_car(cur);
399 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
401 LispReader reader(lisp_cdr(element));
402 reader.read_int("width", width);
403 reader.read_int("height", height);
404 reader.read_int_vector("data", tilemap);
406 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
408 LispReader reader(lisp_cdr(element));
409 reader.read_string("name", name, true);
410 reader.read_string("music", music);
411 reader.read_int("start_pos_x", start_x);
412 reader.read_int("start_pos_y", start_y);
414 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
416 lisp_object_t* cur = lisp_cdr(element);
418 while(!lisp_nil_p(cur))
420 lisp_object_t* element = lisp_car(cur);
422 if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
425 LispReader reader(lisp_cdr(element));
426 level.solved = false;
433 reader.read_string("extro-filename", level.extro_filename);
434 reader.read_string("map-message", level.display_map_message);
435 reader.read_string("next-world", level.next_worldmap);
436 reader.read_string("level", level.name, true);
437 reader.read_int("x", level.x);
438 reader.read_int("y", level.y);
439 reader.read_bool("auto-path", &level.auto_path);
440 level.swap_x = level.swap_y = -1;
441 reader.read_int("swap-x", level.swap_x);
442 reader.read_int("swap-y", level.swap_y);
443 level.vertical_flip = false;
444 reader.read_bool("flip-level", level.vertical_flip);
445 level.quit_worldmap = false;
446 reader.read_bool("exit-game", level.quit_worldmap);
448 levels.push_back(level);
451 /* Kept for backward compability */
452 else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
455 LispReader reader(lisp_cdr(element));
456 level.solved = false;
463 reader.read_string("extro-filename", level.extro_filename);
464 if(!level.extro_filename.empty())
465 level.quit_worldmap = true;
466 reader.read_string("name", level.name, true);
467 reader.read_int("x", level.x);
468 reader.read_int("y", level.y);
469 level.vertical_flip = false;
470 level.swap_x = level.swap_y = -1;
472 levels.push_back(level);
491 void WorldMap::get_level_title(Level& level)
493 /** get level's title */
494 level.title = "<no title>";
496 LispReader* reader = LispReader::load(datadir + "/levels/" + level.name, "supertux-level");
499 std::cerr << "Error: Could not open level file. Ignoring...\n";
503 reader->read_string("name", level.title, true);
508 WorldMap::on_escape_press()
510 // Show or hide the menu
512 Menu::set_current(worldmap_menu);
514 Menu::set_current(0);
518 WorldMap::get_input()
521 input_direction = D_NONE;
524 while (SDL_PollEvent(&event))
528 Menu::current()->event(event);
535 Termination::abort("Received window close", "");
539 switch(event.key.keysym.sym)
553 case SDL_JOYAXISMOTION:
554 if (event.jaxis.axis == joystick_keymap.x_axis)
556 if (event.jaxis.value < -joystick_keymap.dead_zone)
557 input_direction = D_WEST;
558 else if (event.jaxis.value > joystick_keymap.dead_zone)
559 input_direction = D_EAST;
561 else if (event.jaxis.axis == joystick_keymap.y_axis)
563 if (event.jaxis.value > joystick_keymap.dead_zone)
564 input_direction = D_SOUTH;
565 else if (event.jaxis.value < -joystick_keymap.dead_zone)
566 input_direction = D_NORTH;
570 case SDL_JOYBUTTONDOWN:
571 if (event.jbutton.button == joystick_keymap.b_button)
573 else if (event.jbutton.button == joystick_keymap.start_button)
583 if (!Menu::current())
585 Uint8 *keystate = SDL_GetKeyState(NULL);
587 if (keystate[SDLK_LEFT])
588 input_direction = D_WEST;
589 else if (keystate[SDLK_RIGHT])
590 input_direction = D_EAST;
591 else if (keystate[SDLK_UP])
592 input_direction = D_NORTH;
593 else if (keystate[SDLK_DOWN])
594 input_direction = D_SOUTH;
599 WorldMap::get_next_tile(Vector pos, Direction direction)
622 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
624 *new_pos = get_next_tile(old_pos, direction);
626 if (!(new_pos->x >= 0 && new_pos->x < width
627 && new_pos->y >= 0 && new_pos->y < height))
628 { // New position is outsite the tilemap
632 { // Check if we the tile allows us to go to new_pos
636 return (at(old_pos)->west && at(*new_pos)->east);
639 return (at(old_pos)->east && at(*new_pos)->west);
642 return (at(old_pos)->north && at(*new_pos)->south);
645 return (at(old_pos)->south && at(*new_pos)->north);
648 assert(!"path_ok() can't work if direction is NONE");
655 WorldMap::update(float delta)
657 if (enter_level && !tux->is_moving())
659 bool level_finished = true;
660 Level* level = at_level();
663 std::cout << "Nothing to enter at: "
664 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
669 if(!level->name.empty())
671 if (level->x == tux->get_tile_pos().x &&
672 level->y == tux->get_tile_pos().y)
674 PlayerStatus old_player_status = player_status;
676 std::cout << "Enter the current level: " << level->name << std::endl;
677 // do a shriking fade to the level
678 shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
680 GameSession session(datadir + "/levels/" + level->name,
681 ST_GL_LOAD_LEVEL_FILE, level->vertical_flip);
683 switch (session.run())
685 case GameSession::ES_LEVEL_FINISHED:
687 level_finished = true;
688 bool old_level_state = level->solved;
689 level->solved = true;
691 if (session.get_current_sector()->player->got_power !=
692 session.get_current_sector()->player->NONE_POWER)
693 player_status.bonus = PlayerStatus::FLOWER_BONUS;
694 else if (session.get_current_sector()->player->size == BIG)
695 player_status.bonus = PlayerStatus::GROWUP_BONUS;
697 player_status.bonus = PlayerStatus::NO_BONUS;
699 if (old_level_state != level->solved && level->auto_path)
700 { // Try to detect the next direction to which we should walk
701 // FIXME: Mostly a hack
702 Direction dir = D_NONE;
704 Tile* tile = at(tux->get_tile_pos());
706 if (tile->north && tux->back_direction != D_NORTH)
708 else if (tile->south && tux->back_direction != D_SOUTH)
710 else if (tile->east && tux->back_direction != D_EAST)
712 else if (tile->west && tux->back_direction != D_WEST)
717 tux->set_direction(dir);
718 //tux->update(delta);
721 std::cout << "Walk to dir: " << dir << std::endl;
726 case GameSession::ES_LEVEL_ABORT:
727 level_finished = false;
728 /* In case the player's abort the level, keep it using the old
729 status. But the minimum lives and no bonus. */
730 player_status.score = old_player_status.score;
731 player_status.distros = old_player_status.distros;
732 player_status.lives = std::min(old_player_status.lives, player_status.lives);
733 player_status.bonus = player_status.NO_BONUS;
736 case GameSession::ES_GAME_OVER:
738 level_finished = false;
739 /* draw an end screen */
740 /* in the future, this should make a dialog a la SuperMario, asking
741 if the player wants to restart the world map with no score and from
745 DrawingContext context;
746 context.draw_gradient(Color (200,240,220), Color(200,200,220),
749 context.draw_text_center(blue_text, _("GAMEOVER"),
750 Vector(0, 200), LAYER_FOREGROUND1);
752 sprintf(str, _("SCORE: %d"), player_status.score);
753 context.draw_text_center(gold_text, str,
754 Vector(0, 230), LAYER_FOREGROUND1);
756 sprintf(str, _("COINS: %d"), player_status.distros);
757 context.draw_text_center(gold_text, str,
758 Vector(0, screen->w - 32), LAYER_FOREGROUND1);
760 context.do_drawing();
763 wait_for_event(event,2000,5000,true);
766 player_status.reset();
769 case GameSession::ES_NONE:
771 // Should never be reached
775 SoundManager::get()->play_music(song);
776 Menu::set_current(0);
777 if (!savegame_file.empty())
778 savegame(savegame_file);
781 /* The porpose of the next checking is that if the player lost
782 the level (in case there is one), don't show anything */
785 if (!level->extro_filename.empty())
787 // Display a text file
788 display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
790 if (level->swap_x != -1 && level->swap_y != -1)
792 // TODO: add an effect, like a camera scrolling, or at least, a fading
793 tux->set_tile_pos(Vector(level->swap_x, level->swap_y));
795 if (!level->next_worldmap.empty())
797 // Load given worldmap
798 loadmap(level->next_worldmap);
800 if (level->quit_worldmap)
807 tux->set_direction(input_direction);
810 Menu* menu = Menu::current();
815 if(menu == worldmap_menu)
817 switch (worldmap_menu->check())
819 case MNID_RETURNWORLDMAP: // Return to game
821 case MNID_QUITWORLDMAP: // Quit Worldmap
826 else if(menu == options_menu)
828 process_options_menu();
834 WorldMap::at(Vector p)
843 return tile_manager->get(tilemap[width * y + x]);
849 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
851 if (i->x == tux->get_tile_pos().x &&
852 i->y == tux->get_tile_pos().y)
861 WorldMap::draw(DrawingContext& context, const Vector& offset)
863 for(int y = 0; y < height; ++y)
864 for(int x = 0; x < width; ++x)
866 Tile* tile = at(Vector(x, y));
867 context.draw_surface(tile->sprite,
868 Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
871 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
877 context.draw_surface(leveldot_green,
878 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
880 context.draw_surface(leveldot_red,
881 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
884 tux->draw(context, offset);
885 draw_status(context);
889 WorldMap::draw_status(DrawingContext& context)
892 sprintf(str, " %d", player_status.score);
894 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
895 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
897 sprintf(str, "%d", player_status.distros);
898 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
900 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
903 if (player_status.lives >= 5)
905 sprintf(str, "%dx", player_status.lives);
906 context.draw_text(gold_text, str,
907 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
909 context.draw_surface(tux_life, Vector(screen->w -
910 gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
914 for(int i= 0; i < player_status.lives; ++i)
915 context.draw_surface(tux_life,
916 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
919 context.draw_text(white_text, _("LIVES"),
920 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
923 if (!tux->is_moving())
925 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
927 if (i->x == tux->get_tile_pos().x &&
928 i->y == tux->get_tile_pos().y)
935 context.draw_text_center(white_text, i->title,
936 Vector(0, screen->h - white_text->get_height() - 30),
940 /* Display a message in the map, if any as been selected */
941 if(!i->display_map_message.empty())
942 context.draw_text_center(gold_text, i->display_map_message,
943 Vector(0, screen->h - white_text->get_height() - 60),
954 Menu::set_current(0);
958 song = SoundManager::get()->load_music(datadir + "/music/" + music);
959 SoundManager::get()->play_music(song);
961 unsigned int last_update_time;
962 unsigned int update_time;
964 last_update_time = update_time = Ticks::get();
966 DrawingContext context;
969 float delta = ((float)(update_time-last_update_time))/100.0;
976 last_update_time = update_time;
977 update_time = Ticks::get();
979 Vector tux_pos = tux->get_pos();
982 offset.x = -tux_pos.x + screen->w/2;
983 offset.y = -tux_pos.y + screen->h/2;
985 if (offset.x > 0) offset.x = 0;
986 if (offset.y > 0) offset.y = 0;
988 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
989 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
992 draw(context, offset);
998 Menu::current()->draw(context);
999 mouse_cursor->draw(context);
1002 context.do_drawing();
1009 WorldMap::savegame(const std::string& filename)
1014 std::cout << "savegame: " << filename << std::endl;
1015 std::ofstream out(filename.c_str());
1017 int nb_solved_levels = 0;
1018 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1024 out << "(supertux-savegame\n"
1026 << " (title \"" << name << " - " << nb_solved_levels << "/" << levels.size() << "\")\n"
1027 << " (map \"" << map_filename << "\")\n"
1028 << " (lives " << player_status.lives << ")\n"
1029 << " (score " << player_status.score << ")\n"
1030 << " (distros " << player_status.distros << ")\n"
1031 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
1032 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
1033 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
1036 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1040 out << " (level (name \"" << i->name << "\")\n"
1041 << " (solved #t))\n";
1046 << " )\n\n;; EOF ;;" << std::endl;
1050 WorldMap::loadgame(const std::string& filename)
1052 std::cout << "loadgame: " << filename << std::endl;
1053 savegame_file = filename;
1054 map_filename = "icyisland.stwm";
1056 if (access(filename.c_str(), F_OK) != 0)
1062 lisp_object_t* savegame = lisp_read_from_file(filename);
1065 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1070 lisp_object_t* cur = savegame;
1072 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1078 cur = lisp_cdr(cur);
1079 LispReader reader(cur);
1081 /* Get the Map filename and then load it before setting level settings */
1082 reader.read_string("map", map_filename);
1085 reader.read_int("lives", player_status.lives);
1086 reader.read_int("score", player_status.score);
1087 reader.read_int("distros", player_status.distros);
1089 if (player_status.lives < 0)
1090 player_status.lives = START_LIVES;
1092 lisp_object_t* tux_cur = 0;
1093 if (reader.read_lisp("tux", tux_cur))
1096 std::string back_str = "none";
1097 std::string bonus_str = "none";
1099 LispReader tux_reader(tux_cur);
1100 tux_reader.read_float("x", p.x);
1101 tux_reader.read_float("y", p.y);
1102 tux_reader.read_string("back", back_str);
1103 tux_reader.read_string("bonus", bonus_str);
1105 player_status.bonus = string_to_bonus(bonus_str);
1106 tux->back_direction = string_to_direction(back_str);
1107 tux->set_tile_pos(p);
1110 lisp_object_t* level_cur = 0;
1111 if (reader.read_lisp("levels", level_cur))
1115 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1116 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1118 if (strcmp(lisp_symbol(sym), "level") == 0)
1121 bool solved = false;
1123 LispReader level_reader(data);
1124 level_reader.read_string("name", name, true);
1125 level_reader.read_bool("solved", solved);
1127 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1129 if (name == i->name)
1134 level_cur = lisp_cdr(level_cur);
1138 lisp_free(savegame);
1142 WorldMap::loadmap(const std::string& filename)
1145 map_filename = filename;
1149 } // namespace WorldMapNS
1151 /* Local Variables: */