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 "screen/texture.h"
27 #include "screen/screen.h"
28 #include "screen/drawing_context.h"
29 #include "lispreader.h"
34 #include "sound_manager.h"
35 #include "resources.h"
38 namespace WorldMapNS {
40 Direction reverse_dir(Direction direction)
59 direction_to_string(Direction direction)
77 string_to_direction(const std::string& directory)
79 if (directory == "west")
81 else if (directory == "east")
83 else if (directory == "north")
85 else if (directory == "south")
91 TileManager::TileManager()
93 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
94 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
97 st_abort("Couldn't load file", stwt_filename);
99 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
101 lisp_object_t* cur = lisp_cdr(root_obj);
103 while(!lisp_nil_p(cur))
105 lisp_object_t* element = lisp_car(cur);
107 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
110 std::string filename = "<invalid>";
112 Tile* tile = new Tile;
118 tile->auto_walk = false;
120 LispReader reader(lisp_cdr(element));
121 reader.read_int("id", id);
122 reader.read_bool("north", tile->north);
123 reader.read_bool("south", tile->south);
124 reader.read_bool("west", tile->west);
125 reader.read_bool("east", tile->east);
126 reader.read_bool("stop", tile->stop);
127 reader.read_bool("auto-walk", tile->auto_walk);
128 reader.read_string("image", filename);
130 tile->sprite = new Surface(
131 datadir + "/images/worldmap/" + filename,
134 if (id >= int(tiles.size()))
141 puts("Unhandled symbol");
155 TileManager::~TileManager()
157 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
162 TileManager::get(int i)
164 assert(i >=0 && i < int(tiles.size()));
168 //---------------------------------------------------------------------------
170 Tux::Tux(WorldMap* worldmap_)
171 : worldmap(worldmap_)
173 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
174 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", USE_ALPHA);
175 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", USE_ALPHA);
182 input_direction = D_NONE;
187 delete smalltux_sprite;
188 delete firetux_sprite;
189 delete largetux_sprite;
193 Tux::draw(DrawingContext& context, const Vector& offset)
195 Vector pos = get_pos();
196 switch (player_status.bonus)
198 case PlayerStatus::GROWUP_BONUS:
199 context.draw_surface(largetux_sprite,
200 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
202 case PlayerStatus::FLOWER_BONUS:
203 context.draw_surface(firetux_sprite,
204 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
206 case PlayerStatus::NO_BONUS:
207 context.draw_surface(smalltux_sprite,
208 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
217 float x = tile_pos.x * 32;
218 float y = tile_pos.y * 32;
238 return Vector((int)x, (int)y);
250 Tux::action(float delta)
254 if (input_direction != D_NONE)
256 WorldMap::Level* level = worldmap->at_level();
258 // We got a new direction, so lets start walking when possible
260 if ((!level || level->solved)
261 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
263 tile_pos = next_tile;
265 direction = input_direction;
266 back_direction = reverse_dir(direction);
268 else if (input_direction == back_direction)
270 std::cout << "Back triggered" << std::endl;
272 direction = input_direction;
273 tile_pos = worldmap->get_next_tile(tile_pos, direction);
274 back_direction = reverse_dir(direction);
280 // Let tux walk a few pixels (20 pixel/sec)
281 offset += 20.0f * delta;
284 { // We reached the next tile, so we check what to do now
287 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
293 if (worldmap->at(tile_pos)->auto_walk)
294 { // Turn to a new direction
295 Tile* tile = worldmap->at(tile_pos);
296 Direction dir = D_NONE;
298 if (tile->north && back_direction != D_NORTH)
300 else if (tile->south && back_direction != D_SOUTH)
302 else if (tile->east && back_direction != D_EAST)
304 else if (tile->west && back_direction != D_WEST)
310 back_direction = reverse_dir(direction);
314 // Should never be reached if tiledata is good
320 // Walk automatically to the next tile
322 if (worldmap->path_ok(direction, tile_pos, &next_tile))
324 tile_pos = next_tile;
328 puts("Tilemap data is buggy");
336 //---------------------------------------------------------------------------
346 //---------------------------------------------------------------------------
350 tile_manager = new TileManager();
356 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
357 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
358 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
360 input_direction = D_NONE;
364 music = "SALCON.MOD";
369 WorldMap::~WorldMap()
375 delete leveldot_green;
382 std::string filename = datadir + "/levels/default/worldmap.stwm";
384 lisp_object_t* root_obj = lisp_read_from_file(filename);
386 st_abort("Couldn't load file", filename);
388 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
390 lisp_object_t* cur = lisp_cdr(root_obj);
392 while(!lisp_nil_p(cur))
394 lisp_object_t* element = lisp_car(cur);
396 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
398 LispReader reader(lisp_cdr(element));
399 reader.read_int("width", width);
400 reader.read_int("height", height);
401 reader.read_int_vector("data", tilemap);
403 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
405 LispReader reader(lisp_cdr(element));
406 reader.read_string("name", name);
407 reader.read_string("music", music);
409 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
411 lisp_object_t* cur = lisp_cdr(element);
413 while(!lisp_nil_p(cur))
415 lisp_object_t* element = lisp_car(cur);
417 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
420 LispReader reader(lisp_cdr(element));
421 level.solved = false;
428 reader.read_string("extro-filename", level.extro_filename);
429 reader.read_string("name", level.name);
430 reader.read_int("x", level.x);
431 reader.read_int("y", level.y);
433 levels.push_back(level);
451 void WorldMap::get_level_title(Level& level)
453 /** get level's title */
454 level.title = "<no title>";
457 lisp_object_t* root_obj = 0;
458 fi = fopen((datadir + "/levels/" + level.name).c_str(), "r");
461 perror((datadir + "/levels/" + level.name).c_str());
465 lisp_stream_t stream;
466 lisp_stream_init_file (&stream, fi);
467 root_obj = lisp_read (&stream);
469 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
471 printf("World: Parse Error in file %s", level.name.c_str());
474 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
476 LispReader reader(lisp_cdr(root_obj));
477 reader.read_string("name", level.title);
486 WorldMap::on_escape_press()
488 // Show or hide the menu
490 Menu::set_current(worldmap_menu);
492 Menu::set_current(0);
496 WorldMap::get_input()
499 input_direction = D_NONE;
502 while (SDL_PollEvent(&event))
506 Menu::current()->event(event);
513 st_abort("Received window close", "");
517 switch(event.key.keysym.sym)
531 case SDL_JOYAXISMOTION:
532 if (event.jaxis.axis == joystick_keymap.x_axis)
534 if (event.jaxis.value < -joystick_keymap.dead_zone)
535 input_direction = D_WEST;
536 else if (event.jaxis.value > joystick_keymap.dead_zone)
537 input_direction = D_EAST;
539 else if (event.jaxis.axis == joystick_keymap.y_axis)
541 if (event.jaxis.value > joystick_keymap.dead_zone)
542 input_direction = D_SOUTH;
543 else if (event.jaxis.value < -joystick_keymap.dead_zone)
544 input_direction = D_NORTH;
548 case SDL_JOYBUTTONDOWN:
549 if (event.jbutton.button == joystick_keymap.b_button)
551 else if (event.jbutton.button == joystick_keymap.start_button)
561 if (!Menu::current())
563 Uint8 *keystate = SDL_GetKeyState(NULL);
565 if (keystate[SDLK_LEFT])
566 input_direction = D_WEST;
567 else if (keystate[SDLK_RIGHT])
568 input_direction = D_EAST;
569 else if (keystate[SDLK_UP])
570 input_direction = D_NORTH;
571 else if (keystate[SDLK_DOWN])
572 input_direction = D_SOUTH;
577 WorldMap::get_next_tile(Vector pos, Direction direction)
600 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
602 *new_pos = get_next_tile(old_pos, direction);
604 if (!(new_pos->x >= 0 && new_pos->x < width
605 && new_pos->y >= 0 && new_pos->y < height))
606 { // New position is outsite the tilemap
610 { // Check if we the tile allows us to go to new_pos
614 return (at(old_pos)->west && at(*new_pos)->east);
617 return (at(old_pos)->east && at(*new_pos)->west);
620 return (at(old_pos)->north && at(*new_pos)->south);
623 return (at(old_pos)->south && at(*new_pos)->north);
626 assert(!"path_ok() can't work if direction is NONE");
633 WorldMap::update(float delta)
635 if (enter_level && !tux->is_moving())
637 Level* level = at_level();
640 if (level->x == tux->get_tile_pos().x &&
641 level->y == tux->get_tile_pos().y)
643 PlayerStatus old_player_status = player_status;
645 std::cout << "Enter the current level: " << level->name << std::endl;
646 // do a shriking fade to the level
647 shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
649 GameSession session(datadir + "/levels/" + level->name,
650 ST_GL_LOAD_LEVEL_FILE);
652 switch (session.run())
654 case GameSession::ES_LEVEL_FINISHED:
656 bool old_level_state = level->solved;
657 level->solved = true;
659 if (session.get_current_sector()->player->got_power !=
660 session.get_current_sector()->player->NONE_POWER)
661 player_status.bonus = PlayerStatus::FLOWER_BONUS;
662 else if (session.get_current_sector()->player->size == BIG)
663 player_status.bonus = PlayerStatus::GROWUP_BONUS;
665 player_status.bonus = PlayerStatus::NO_BONUS;
667 if (old_level_state != level->solved)
668 { // Try to detect the next direction to which we should walk
669 // FIXME: Mostly a hack
670 Direction dir = D_NONE;
672 Tile* tile = at(tux->get_tile_pos());
674 if (tile->north && tux->back_direction != D_NORTH)
676 else if (tile->south && tux->back_direction != D_SOUTH)
678 else if (tile->east && tux->back_direction != D_EAST)
680 else if (tile->west && tux->back_direction != D_WEST)
685 tux->set_direction(dir);
686 //tux->update(delta);
689 std::cout << "Walk to dir: " << dir << std::endl;
692 if (!level->extro_filename.empty())
695 sound_manager->load_music(datadir + "/music/theme.mod");
696 sound_manager->play_music(theme);
697 // Display final credits and go back to the main menu
698 display_text_file(level->extro_filename,
699 "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
700 display_text_file("CREDITS",
701 "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
707 case GameSession::ES_LEVEL_ABORT:
708 /* In case the player's abort the level, keep it using the old
710 player_status = old_player_status;
712 case GameSession::ES_GAME_OVER:
714 /* draw an end screen */
715 /* in the future, this should make a dialog a la SuperMario, asking
716 if the player wants to restart the world map with no score and from
720 DrawingContext context;
721 context.draw_gradient(Color (0, 255, 0), Color (255, 0, 255),
724 context.draw_text_center(blue_text, _("GAMEOVER"),
725 Vector(0, 200), LAYER_FOREGROUND1);
727 sprintf(str, _("SCORE: %d"), player_status.score);
728 context.draw_text_center(gold_text, str,
729 Vector(0, 224), LAYER_FOREGROUND1);
731 sprintf(str, _("COINS: %d"), player_status.distros);
732 context.draw_text_center(gold_text, str,
733 Vector(0, screen->w - 32), LAYER_FOREGROUND1);
735 context.do_drawing();
738 wait_for_event(event,2000,5000,true);
741 player_status.reset();
744 case GameSession::ES_NONE:
746 // Should never be reached
750 sound_manager->play_music(song);
751 Menu::set_current(0);
752 if (!savegame_file.empty())
753 savegame(savegame_file);
759 std::cout << "Nothing to enter at: "
760 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
766 tux->set_direction(input_direction);
769 Menu* menu = Menu::current();
774 if(menu == worldmap_menu)
776 switch (worldmap_menu->check())
778 case MNID_RETURNWORLDMAP: // Return to game
780 case MNID_QUITWORLDMAP: // Quit Worldmap
785 else if(menu == options_menu)
787 process_options_menu();
793 WorldMap::at(Vector p)
802 return tile_manager->get(tilemap[width * y + x]);
808 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
810 if (i->x == tux->get_tile_pos().x &&
811 i->y == tux->get_tile_pos().y)
820 WorldMap::draw(DrawingContext& context, const Vector& offset)
822 for(int y = 0; y < height; ++y)
823 for(int x = 0; x < width; ++x)
825 Tile* tile = at(Vector(x, y));
826 context.draw_surface(tile->sprite,
827 Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
830 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
833 context.draw_surface(leveldot_green,
834 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
836 context.draw_surface(leveldot_red,
837 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
840 tux->draw(context, offset);
841 draw_status(context);
845 WorldMap::draw_status(DrawingContext& context)
848 sprintf(str, " %d", player_status.score);
850 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
851 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
853 sprintf(str, "%d", player_status.distros);
854 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
856 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
859 if (player_status.lives >= 5)
861 sprintf(str, "%dx", player_status.lives);
862 context.draw_text(gold_text, str,
863 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
865 context.draw_surface(tux_life, Vector(screen->w -
866 gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
870 for(int i= 0; i < player_status.lives; ++i)
871 context.draw_surface(tux_life,
872 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
875 context.draw_text(white_text, _("LIVES"),
876 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
879 if (!tux->is_moving())
881 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
883 if (i->x == tux->get_tile_pos().x &&
884 i->y == tux->get_tile_pos().y)
889 context.draw_text(white_text, i->title,
890 Vector(screen->w/2 - white_text->get_text_width(i->title)/2,
891 screen->h - white_text->get_height() - 30),
902 Menu::set_current(0);
906 song = sound_manager->load_music(datadir + "/music/" + music);
907 sound_manager->play_music(song);
909 unsigned int last_update_time;
910 unsigned int update_time;
912 last_update_time = update_time = st_get_ticks();
914 DrawingContext context;
917 float delta = ((float)(update_time-last_update_time))/100.0;
924 last_update_time = update_time;
925 update_time = st_get_ticks();
927 Vector tux_pos = tux->get_pos();
930 offset.x = -tux_pos.x + screen->w/2;
931 offset.y = -tux_pos.y + screen->h/2;
933 if (offset.x > 0) offset.x = 0;
934 if (offset.y > 0) offset.y = 0;
936 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
937 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
940 draw(context, offset);
946 Menu::current()->draw(context);
947 mouse_cursor->draw(context);
950 context.do_drawing();
957 WorldMap::savegame(const std::string& filename)
959 std::cout << "savegame: " << filename << std::endl;
960 std::ofstream out(filename.c_str());
962 int nb_solved_levels = 0;
963 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
969 out << "(supertux-savegame\n"
971 << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
972 << " (lives " << player_status.lives << ")\n"
973 << " (score " << player_status.score << ")\n"
974 << " (distros " << player_status.distros << ")\n"
975 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
976 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
977 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
980 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
984 out << " (level (name \"" << i->name << "\")\n"
985 << " (solved #t))\n";
990 << " )\n\n;; EOF ;;" << std::endl;
994 WorldMap::loadgame(const std::string& filename)
996 std::cout << "loadgame: " << filename << std::endl;
997 savegame_file = filename;
999 if (access(filename.c_str(), F_OK) != 0)
1002 lisp_object_t* savegame = lisp_read_from_file(filename);
1005 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1009 lisp_object_t* cur = savegame;
1011 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1014 cur = lisp_cdr(cur);
1015 LispReader reader(cur);
1017 reader.read_int("lives", player_status.lives);
1018 reader.read_int("score", player_status.score);
1019 reader.read_int("distros", player_status.distros);
1021 if (player_status.lives < 0)
1022 player_status.lives = START_LIVES;
1024 lisp_object_t* tux_cur = 0;
1025 if (reader.read_lisp("tux", tux_cur))
1028 std::string back_str = "none";
1029 std::string bonus_str = "none";
1031 LispReader tux_reader(tux_cur);
1032 tux_reader.read_float("x", p.x);
1033 tux_reader.read_float("y", p.y);
1034 tux_reader.read_string("back", back_str);
1035 tux_reader.read_string("bonus", bonus_str);
1037 player_status.bonus = string_to_bonus(bonus_str);
1038 tux->back_direction = string_to_direction(back_str);
1039 tux->set_tile_pos(p);
1042 lisp_object_t* level_cur = 0;
1043 if (reader.read_lisp("levels", level_cur))
1047 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1048 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1050 if (strcmp(lisp_symbol(sym), "level") == 0)
1053 bool solved = false;
1055 LispReader level_reader(data);
1056 level_reader.read_string("name", name);
1057 level_reader.read_bool("solved", solved);
1059 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1061 if (name == i->name)
1066 level_cur = lisp_cdr(level_cur);
1070 lisp_free(savegame);
1073 } // namespace WorldMapNS
1075 /* Local Variables: */