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"
33 #include "resources.h"
35 namespace WorldMapNS {
37 Direction reverse_dir(Direction direction)
56 direction_to_string(Direction direction)
74 string_to_direction(const std::string& directory)
76 if (directory == "west")
78 else if (directory == "east")
80 else if (directory == "north")
82 else if (directory == "south")
88 TileManager::TileManager()
90 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
91 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
94 st_abort("Couldn't load file", stwt_filename);
96 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
98 lisp_object_t* cur = lisp_cdr(root_obj);
100 while(!lisp_nil_p(cur))
102 lisp_object_t* element = lisp_car(cur);
104 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
107 std::string filename = "<invalid>";
109 Tile* tile = new Tile;
115 tile->auto_walk = false;
117 LispReader reader(lisp_cdr(element));
118 reader.read_int("id", &id);
119 reader.read_bool("north", &tile->north);
120 reader.read_bool("south", &tile->south);
121 reader.read_bool("west", &tile->west);
122 reader.read_bool("east", &tile->east);
123 reader.read_bool("stop", &tile->stop);
124 reader.read_bool("auto-walk", &tile->auto_walk);
125 reader.read_string("image", &filename);
127 tile->sprite = new Surface(
128 datadir + "/images/worldmap/" + filename,
131 if (id >= int(tiles.size()))
138 puts("Unhandled symbol");
152 TileManager::~TileManager()
154 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
159 TileManager::get(int i)
161 assert(i >=0 && i < int(tiles.size()));
165 //---------------------------------------------------------------------------
167 Tux::Tux(WorldMap* worldmap_)
168 : worldmap(worldmap_)
170 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
171 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", USE_ALPHA);
172 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", USE_ALPHA);
179 input_direction = D_NONE;
184 delete smalltux_sprite;
185 delete firetux_sprite;
186 delete largetux_sprite;
190 Tux::draw(DrawingContext& context, const Vector& offset)
192 Vector pos = get_pos();
193 switch (player_status.bonus)
195 case PlayerStatus::GROWUP_BONUS:
196 context.draw_surface(largetux_sprite,
197 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
199 case PlayerStatus::FLOWER_BONUS:
200 context.draw_surface(firetux_sprite,
201 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
203 case PlayerStatus::NO_BONUS:
204 context.draw_surface(smalltux_sprite,
205 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
214 float x = tile_pos.x * 32;
215 float y = tile_pos.y * 32;
235 return Vector((int)x, (int)y);
247 Tux::action(float delta)
251 if (input_direction != D_NONE)
253 WorldMap::Level* level = worldmap->at_level();
255 // We got a new direction, so lets start walking when possible
257 if ((!level || level->solved)
258 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
260 tile_pos = next_tile;
262 direction = input_direction;
263 back_direction = reverse_dir(direction);
265 else if (input_direction == back_direction)
267 std::cout << "Back triggered" << std::endl;
269 direction = input_direction;
270 tile_pos = worldmap->get_next_tile(tile_pos, direction);
271 back_direction = reverse_dir(direction);
277 // Let tux walk a few pixels (20 pixel/sec)
278 offset += 20.0f * delta;
281 { // We reached the next tile, so we check what to do now
284 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
290 if (worldmap->at(tile_pos)->auto_walk)
291 { // Turn to a new direction
292 Tile* tile = worldmap->at(tile_pos);
293 Direction dir = D_NONE;
295 if (tile->north && back_direction != D_NORTH)
297 else if (tile->south && back_direction != D_SOUTH)
299 else if (tile->east && back_direction != D_EAST)
301 else if (tile->west && back_direction != D_WEST)
307 back_direction = reverse_dir(direction);
311 // Should never be reached if tiledata is good
317 // Walk automatically to the next tile
319 if (worldmap->path_ok(direction, tile_pos, &next_tile))
321 tile_pos = next_tile;
325 puts("Tilemap data is buggy");
333 //---------------------------------------------------------------------------
343 //---------------------------------------------------------------------------
347 tile_manager = new TileManager();
353 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
354 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
355 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
357 input_direction = D_NONE;
361 music = "SALCON.MOD";
366 WorldMap::~WorldMap()
372 delete leveldot_green;
379 std::string filename = datadir + "/levels/default/worldmap.stwm";
381 lisp_object_t* root_obj = lisp_read_from_file(filename);
383 st_abort("Couldn't load file", filename);
385 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
387 lisp_object_t* cur = lisp_cdr(root_obj);
389 while(!lisp_nil_p(cur))
391 lisp_object_t* element = lisp_car(cur);
393 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
395 LispReader reader(lisp_cdr(element));
396 reader.read_int("width", &width);
397 reader.read_int("height", &height);
398 reader.read_int_vector("data", &tilemap);
400 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
402 LispReader reader(lisp_cdr(element));
403 reader.read_string("name", &name);
404 reader.read_string("music", &music);
406 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
408 lisp_object_t* cur = lisp_cdr(element);
410 while(!lisp_nil_p(cur))
412 lisp_object_t* element = lisp_car(cur);
414 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
417 LispReader reader(lisp_cdr(element));
418 level.solved = false;
425 reader.read_string("extro-filename", &level.extro_filename);
426 reader.read_string("name", &level.name);
427 reader.read_int("x", &level.x);
428 reader.read_int("y", &level.y);
430 levels.push_back(level);
448 void WorldMap::get_level_title(Level& level)
450 /** get level's title */
451 level.title = "<no title>";
454 lisp_object_t* root_obj = 0;
455 fi = fopen((datadir + "/levels/" + level.name).c_str(), "r");
458 perror((datadir + "/levels/" + level.name).c_str());
462 lisp_stream_t stream;
463 lisp_stream_init_file (&stream, fi);
464 root_obj = lisp_read (&stream);
466 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
468 printf("World: Parse Error in file %s", level.name.c_str());
471 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
473 LispReader reader(lisp_cdr(root_obj));
474 reader.read_string("name", &level.title);
483 WorldMap::on_escape_press()
485 // Show or hide the menu
487 Menu::set_current(worldmap_menu);
489 Menu::set_current(0);
493 WorldMap::get_input()
496 input_direction = D_NONE;
499 while (SDL_PollEvent(&event))
503 Menu::current()->event(event);
510 st_abort("Received window close", "");
514 switch(event.key.keysym.sym)
528 case SDL_JOYAXISMOTION:
529 if (event.jaxis.axis == joystick_keymap.x_axis)
531 if (event.jaxis.value < -joystick_keymap.dead_zone)
532 input_direction = D_WEST;
533 else if (event.jaxis.value > joystick_keymap.dead_zone)
534 input_direction = D_EAST;
536 else if (event.jaxis.axis == joystick_keymap.y_axis)
538 if (event.jaxis.value > joystick_keymap.dead_zone)
539 input_direction = D_SOUTH;
540 else if (event.jaxis.value < -joystick_keymap.dead_zone)
541 input_direction = D_NORTH;
545 case SDL_JOYBUTTONDOWN:
546 if (event.jbutton.button == joystick_keymap.b_button)
548 else if (event.jbutton.button == joystick_keymap.start_button)
558 if (!Menu::current())
560 Uint8 *keystate = SDL_GetKeyState(NULL);
562 if (keystate[SDLK_LEFT])
563 input_direction = D_WEST;
564 else if (keystate[SDLK_RIGHT])
565 input_direction = D_EAST;
566 else if (keystate[SDLK_UP])
567 input_direction = D_NORTH;
568 else if (keystate[SDLK_DOWN])
569 input_direction = D_SOUTH;
574 WorldMap::get_next_tile(Vector pos, Direction direction)
597 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
599 *new_pos = get_next_tile(old_pos, direction);
601 if (!(new_pos->x >= 0 && new_pos->x < width
602 && new_pos->y >= 0 && new_pos->y < height))
603 { // New position is outsite the tilemap
607 { // Check if we the tile allows us to go to new_pos
611 return (at(old_pos)->west && at(*new_pos)->east);
614 return (at(old_pos)->east && at(*new_pos)->west);
617 return (at(old_pos)->north && at(*new_pos)->south);
620 return (at(old_pos)->south && at(*new_pos)->north);
623 assert(!"path_ok() can't work if direction is NONE");
630 WorldMap::update(float delta)
632 if (enter_level && !tux->is_moving())
634 Level* level = at_level();
637 if (level->x == tux->get_tile_pos().x &&
638 level->y == tux->get_tile_pos().y)
640 PlayerStatus old_player_status = player_status;
642 std::cout << "Enter the current level: " << level->name << std::endl;
643 // do a shriking fade to the level
644 shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
646 GameSession session(datadir + "/levels/" + level->name,
647 1, ST_GL_LOAD_LEVEL_FILE);
649 switch (session.run())
651 case GameSession::ES_LEVEL_FINISHED:
653 bool old_level_state = level->solved;
654 level->solved = true;
656 if (session.get_world()->get_tux()->got_power !=
657 session.get_world()->get_tux()->NONE_POWER)
658 player_status.bonus = PlayerStatus::FLOWER_BONUS;
659 else if (session.get_world()->get_tux()->size == BIG)
660 player_status.bonus = PlayerStatus::GROWUP_BONUS;
662 player_status.bonus = PlayerStatus::NO_BONUS;
664 if (old_level_state != level->solved)
665 { // Try to detect the next direction to which we should walk
666 // FIXME: Mostly a hack
667 Direction dir = D_NONE;
669 Tile* tile = at(tux->get_tile_pos());
671 if (tile->north && tux->back_direction != D_NORTH)
673 else if (tile->south && tux->back_direction != D_SOUTH)
675 else if (tile->east && tux->back_direction != D_EAST)
677 else if (tile->west && tux->back_direction != D_WEST)
682 tux->set_direction(dir);
683 //tux->update(delta);
686 std::cout << "Walk to dir: " << dir << std::endl;
689 if (!level->extro_filename.empty())
692 music_manager->load_music(datadir + "/music/theme.mod");
693 music_manager->play_music(theme);
694 // Display final credits and go back to the main menu
695 display_text_file(level->extro_filename,
696 "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
697 display_text_file("CREDITS",
698 "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
704 case GameSession::ES_LEVEL_ABORT:
705 /* In case the player's abort the level, keep it using the old
707 player_status = old_player_status;
709 case GameSession::ES_GAME_OVER:
711 /* draw an end screen */
712 /* in the future, this should make a dialog a la SuperMario, asking
713 if the player wants to restart the world map with no score and from
717 DrawingContext context;
718 context.draw_gradient(Color (0, 255, 0), Color (255, 0, 255),
721 context.draw_text_center(blue_text, "GAMEOVER",
722 Vector(0, 200), LAYER_FOREGROUND1);
724 sprintf(str, "SCORE: %d", player_status.score);
725 context.draw_text_center(gold_text, str,
726 Vector(0, 224), LAYER_FOREGROUND1);
728 sprintf(str, "COINS: %d", player_status.distros);
729 context.draw_text_center(gold_text, str,
730 Vector(0, screen->w - 32), LAYER_FOREGROUND1);
732 context.do_drawing();
735 wait_for_event(event,2000,5000,true);
738 player_status.reset();
741 case GameSession::ES_NONE:
743 // Should never be reached
747 music_manager->play_music(song);
748 Menu::set_current(0);
749 if (!savegame_file.empty())
750 savegame(savegame_file);
756 std::cout << "Nothing to enter at: "
757 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
763 tux->set_direction(input_direction);
766 Menu* menu = Menu::current();
771 if(menu == worldmap_menu)
773 switch (worldmap_menu->check())
775 case MNID_RETURNWORLDMAP: // Return to game
777 case MNID_QUITWORLDMAP: // Quit Worldmap
782 else if(menu == options_menu)
784 process_options_menu();
790 WorldMap::at(Vector p)
799 return tile_manager->get(tilemap[width * y + x]);
805 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
807 if (i->x == tux->get_tile_pos().x &&
808 i->y == tux->get_tile_pos().y)
817 WorldMap::draw(DrawingContext& context, const Vector& offset)
819 for(int y = 0; y < height; ++y)
820 for(int x = 0; x < width; ++x)
822 Tile* tile = at(Vector(x, y));
823 context.draw_surface(tile->sprite,
824 Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
827 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
830 context.draw_surface(leveldot_green,
831 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
833 context.draw_surface(leveldot_red,
834 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
837 tux->draw(context, offset);
838 draw_status(context);
842 WorldMap::draw_status(DrawingContext& context)
844 context.push_transform();
845 context.set_translation(Vector(0, 0));
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 context.draw_text(white_text, "LIVES",
860 Vector(screen->w - white_text->get_text_width("LIVES")*2, 0),
862 if (player_status.lives >= 5)
864 sprintf(str, "%dx", player_status.lives);
865 context.draw_text(gold_text, str,
866 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
868 context.draw_surface(tux_life, Vector(screen->w - gold_text->w, 0),
873 for(int i= 0; i < player_status.lives; ++i)
874 context.draw_surface(tux_life,
875 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 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() - 50),
902 Menu::set_current(0);
906 song = music_manager->load_music(datadir + "/music/" + music);
907 music_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: */