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.
27 #include "screen/surface.h"
28 #include "screen/screen.h"
29 #include "screen/drawing_context.h"
30 #include "lispreader.h"
35 #include "sound_manager.h"
36 #include "resources.h"
39 namespace WorldMapNS {
41 Direction reverse_dir(Direction direction)
60 direction_to_string(Direction direction)
78 string_to_direction(const std::string& directory)
80 if (directory == "west")
82 else if (directory == "east")
84 else if (directory == "north")
86 else if (directory == "south")
92 TileManager::TileManager()
94 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
95 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
98 st_abort("Couldn't load file", stwt_filename);
100 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
102 lisp_object_t* cur = lisp_cdr(root_obj);
104 while(!lisp_nil_p(cur))
106 lisp_object_t* element = lisp_car(cur);
108 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
111 std::string filename = "<invalid>";
113 Tile* tile = new Tile;
119 tile->auto_walk = false;
121 LispReader reader(lisp_cdr(element));
122 reader.read_int("id", id);
123 reader.read_bool("north", tile->north);
124 reader.read_bool("south", tile->south);
125 reader.read_bool("west", tile->west);
126 reader.read_bool("east", tile->east);
127 reader.read_bool("stop", tile->stop);
128 reader.read_bool("auto-walk", tile->auto_walk);
129 reader.read_string("image", filename);
131 tile->sprite = new Surface(
132 datadir + "/images/worldmap/" + filename,
135 if (id >= int(tiles.size()))
142 puts("Unhandled symbol");
156 TileManager::~TileManager()
158 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
163 TileManager::get(int i)
165 assert(i >=0 && i < int(tiles.size()));
169 //---------------------------------------------------------------------------
171 Tux::Tux(WorldMap* worldmap_)
172 : worldmap(worldmap_)
174 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", true);
175 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", true);
176 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", true);
180 tile_pos.x = worldmap->get_start_x();
181 tile_pos.y = worldmap->get_start_y();
183 input_direction = D_NONE;
188 delete smalltux_sprite;
189 delete firetux_sprite;
190 delete largetux_sprite;
194 Tux::draw(DrawingContext& context, const Vector& offset)
196 Vector pos = get_pos();
197 switch (player_status.bonus)
199 case PlayerStatus::GROWUP_BONUS:
200 context.draw_surface(largetux_sprite,
201 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
203 case PlayerStatus::FLOWER_BONUS:
204 context.draw_surface(firetux_sprite,
205 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
207 case PlayerStatus::NO_BONUS:
208 context.draw_surface(smalltux_sprite,
209 Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
218 float x = tile_pos.x * 32;
219 float y = tile_pos.y * 32;
239 return Vector((int)x, (int)y);
251 Tux::action(float delta)
255 if (input_direction != D_NONE)
257 WorldMap::Level* level = worldmap->at_level();
259 // We got a new direction, so lets start walking when possible
261 if ((!level || level->solved)
262 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
264 tile_pos = next_tile;
266 direction = input_direction;
267 back_direction = reverse_dir(direction);
269 else if (input_direction == back_direction)
271 std::cout << "Back triggered" << std::endl;
273 direction = input_direction;
274 tile_pos = worldmap->get_next_tile(tile_pos, direction);
275 back_direction = reverse_dir(direction);
281 // Let tux walk a few pixels (20 pixel/sec)
282 offset += 20.0f * delta;
285 { // We reached the next tile, so we check what to do now
288 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
294 if (worldmap->at(tile_pos)->auto_walk)
295 { // Turn to a new direction
296 Tile* tile = worldmap->at(tile_pos);
297 Direction dir = D_NONE;
299 if (tile->north && back_direction != D_NORTH)
301 else if (tile->south && back_direction != D_SOUTH)
303 else if (tile->east && back_direction != D_EAST)
305 else if (tile->west && back_direction != D_WEST)
311 back_direction = reverse_dir(direction);
315 // Should never be reached if tiledata is good
321 // Walk automatically to the next tile
323 if (worldmap->path_ok(direction, tile_pos, &next_tile))
325 tile_pos = next_tile;
329 puts("Tilemap data is buggy");
337 //---------------------------------------------------------------------------
347 //---------------------------------------------------------------------------
351 tile_manager = new TileManager();
352 //tux = new Tux(this);
360 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", true);
361 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", true);
362 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", true);
364 input_direction = D_NONE;
368 music = "SALCON.MOD";
371 WorldMap::~WorldMap()
377 delete leveldot_green;
384 lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
386 st_abort("Couldn't load file", datadir + "/levels/worldmap/" + map_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, true);
407 reader.read_string("music", music);
408 reader.read_int("start_pos_x", start_x);
409 reader.read_int("start_pos_y", start_y);
411 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
413 lisp_object_t* cur = lisp_cdr(element);
415 while(!lisp_nil_p(cur))
417 lisp_object_t* element = lisp_car(cur);
419 if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
422 LispReader reader(lisp_cdr(element));
423 level.solved = false;
430 reader.read_string("extro-filename", level.extro_filename);
431 reader.read_string("map-message", level.display_map_message);
432 reader.read_string("goto-world", level.goto_worldmap);
433 reader.read_string("level", level.name, true);
434 reader.read_int("x", level.x);
435 reader.read_int("y", level.y);
436 level.swap_x = level.swap_y = -1;
437 reader.read_int("swap-x", level.swap_x);
438 reader.read_int("swap-y", level.swap_y);
439 level.vertical_flip = false;
440 reader.read_bool("flip-level", level.vertical_flip);
441 level.quit_worldmap = false;
442 reader.read_bool("exit-game", level.quit_worldmap);
444 levels.push_back(level);
447 /* Kept for backward compability */
448 else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
451 LispReader reader(lisp_cdr(element));
452 level.solved = false;
459 reader.read_string("extro-filename", level.extro_filename);
460 if(!level.extro_filename.empty())
461 level.quit_worldmap = true;
462 reader.read_string("name", level.name, true);
463 reader.read_int("x", level.x);
464 reader.read_int("y", level.y);
465 level.vertical_flip = false;
466 level.swap_x = level.swap_y = -1;
468 levels.push_back(level);
487 void WorldMap::get_level_title(Level& level)
489 /** get level's title */
490 level.title = "<no title>";
493 lisp_object_t* root_obj = 0;
494 fi = fopen((datadir + "/levels/" + level.name).c_str(), "r");
497 perror((datadir + "/levels/" + level.name).c_str());
501 lisp_stream_t stream;
502 lisp_stream_init_file (&stream, fi);
503 root_obj = lisp_read (&stream);
505 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
507 printf("World: Parse Error in file %s", level.name.c_str());
510 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
512 LispReader reader(lisp_cdr(root_obj));
513 reader.read_string("name", level.title, true);
522 WorldMap::on_escape_press()
524 // Show or hide the menu
526 Menu::set_current(worldmap_menu);
528 Menu::set_current(0);
532 WorldMap::get_input()
535 input_direction = D_NONE;
538 while (SDL_PollEvent(&event))
542 Menu::current()->event(event);
549 st_abort("Received window close", "");
553 switch(event.key.keysym.sym)
567 case SDL_JOYAXISMOTION:
568 if (event.jaxis.axis == joystick_keymap.x_axis)
570 if (event.jaxis.value < -joystick_keymap.dead_zone)
571 input_direction = D_WEST;
572 else if (event.jaxis.value > joystick_keymap.dead_zone)
573 input_direction = D_EAST;
575 else if (event.jaxis.axis == joystick_keymap.y_axis)
577 if (event.jaxis.value > joystick_keymap.dead_zone)
578 input_direction = D_SOUTH;
579 else if (event.jaxis.value < -joystick_keymap.dead_zone)
580 input_direction = D_NORTH;
584 case SDL_JOYBUTTONDOWN:
585 if (event.jbutton.button == joystick_keymap.b_button)
587 else if (event.jbutton.button == joystick_keymap.start_button)
597 if (!Menu::current())
599 Uint8 *keystate = SDL_GetKeyState(NULL);
601 if (keystate[SDLK_LEFT])
602 input_direction = D_WEST;
603 else if (keystate[SDLK_RIGHT])
604 input_direction = D_EAST;
605 else if (keystate[SDLK_UP])
606 input_direction = D_NORTH;
607 else if (keystate[SDLK_DOWN])
608 input_direction = D_SOUTH;
613 WorldMap::get_next_tile(Vector pos, Direction direction)
636 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
638 *new_pos = get_next_tile(old_pos, direction);
640 if (!(new_pos->x >= 0 && new_pos->x < width
641 && new_pos->y >= 0 && new_pos->y < height))
642 { // New position is outsite the tilemap
646 { // Check if we the tile allows us to go to new_pos
650 return (at(old_pos)->west && at(*new_pos)->east);
653 return (at(old_pos)->east && at(*new_pos)->west);
656 return (at(old_pos)->north && at(*new_pos)->south);
659 return (at(old_pos)->south && at(*new_pos)->north);
662 assert(!"path_ok() can't work if direction is NONE");
669 WorldMap::update(float delta)
671 if (enter_level && !tux->is_moving())
673 bool level_finished = true;
674 Level* level = at_level();
677 std::cout << "Nothing to enter at: "
678 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
683 if(!level->name.empty())
685 if (level->x == tux->get_tile_pos().x &&
686 level->y == tux->get_tile_pos().y)
688 PlayerStatus old_player_status = player_status;
690 std::cout << "Enter the current level: " << level->name << std::endl;
691 // do a shriking fade to the level
692 shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
694 GameSession session(datadir + "/levels/" + level->name,
695 ST_GL_LOAD_LEVEL_FILE, level->vertical_flip);
697 switch (session.run())
699 case GameSession::ES_LEVEL_FINISHED:
701 level_finished = true;
702 bool old_level_state = level->solved;
703 level->solved = true;
705 if (session.get_current_sector()->player->got_power !=
706 session.get_current_sector()->player->NONE_POWER)
707 player_status.bonus = PlayerStatus::FLOWER_BONUS;
708 else if (session.get_current_sector()->player->size == BIG)
709 player_status.bonus = PlayerStatus::GROWUP_BONUS;
711 player_status.bonus = PlayerStatus::NO_BONUS;
713 if (old_level_state != level->solved)
714 { // Try to detect the next direction to which we should walk
715 // FIXME: Mostly a hack
716 Direction dir = D_NONE;
718 Tile* tile = at(tux->get_tile_pos());
720 if (tile->north && tux->back_direction != D_NORTH)
722 else if (tile->south && tux->back_direction != D_SOUTH)
724 else if (tile->east && tux->back_direction != D_EAST)
726 else if (tile->west && tux->back_direction != D_WEST)
731 tux->set_direction(dir);
732 //tux->update(delta);
735 std::cout << "Walk to dir: " << dir << std::endl;
740 case GameSession::ES_LEVEL_ABORT:
741 level_finished = false;
742 /* In case the player's abort the level, keep it using the old
743 status. But the minimum lives and no bonus. */
744 player_status.score = old_player_status.score;
745 player_status.distros = old_player_status.distros;
746 player_status.lives = std::min(old_player_status.lives, player_status.lives);
747 player_status.bonus = player_status.NO_BONUS;
750 case GameSession::ES_GAME_OVER:
752 level_finished = false;
753 /* draw an end screen */
754 /* in the future, this should make a dialog a la SuperMario, asking
755 if the player wants to restart the world map with no score and from
759 DrawingContext context;
760 context.draw_gradient(Color (200,240,220), Color(200,200,220),
763 context.draw_text_center(blue_text, _("GAMEOVER"),
764 Vector(0, 200), LAYER_FOREGROUND1);
766 sprintf(str, _("SCORE: %d"), player_status.score);
767 context.draw_text_center(gold_text, str,
768 Vector(0, 230), LAYER_FOREGROUND1);
770 sprintf(str, _("COINS: %d"), player_status.distros);
771 context.draw_text_center(gold_text, str,
772 Vector(0, screen->w - 32), LAYER_FOREGROUND1);
774 context.do_drawing();
777 wait_for_event(event,2000,5000,true);
780 player_status.reset();
783 case GameSession::ES_NONE:
785 // Should never be reached
789 sound_manager->play_music(song);
790 Menu::set_current(0);
791 if (!savegame_file.empty())
792 savegame(savegame_file);
795 /* The porpose of the next checking is that if the player lost
796 the level (in case there is one), don't show anything */
799 if (!level->extro_filename.empty())
801 // Display a text file
802 display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE);
804 if (level->swap_x != -1 && level->swap_y != -1)
806 // TODO: add an effect, like a camera scrolling, or at least, a fading
807 tux->set_tile_pos(Vector(level->swap_x, level->swap_y));
809 if (!level->goto_worldmap.empty())
811 // Load given worldmap
812 loadmap(level->goto_worldmap);
814 if (level->quit_worldmap)
821 tux->set_direction(input_direction);
824 Menu* menu = Menu::current();
829 if(menu == worldmap_menu)
831 switch (worldmap_menu->check())
833 case MNID_RETURNWORLDMAP: // Return to game
835 case MNID_QUITWORLDMAP: // Quit Worldmap
840 else if(menu == options_menu)
842 process_options_menu();
848 WorldMap::at(Vector p)
857 return tile_manager->get(tilemap[width * y + x]);
863 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
865 if (i->x == tux->get_tile_pos().x &&
866 i->y == tux->get_tile_pos().y)
875 WorldMap::draw(DrawingContext& context, const Vector& offset)
877 for(int y = 0; y < height; ++y)
878 for(int x = 0; x < width; ++x)
880 Tile* tile = at(Vector(x, y));
881 context.draw_surface(tile->sprite,
882 Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
885 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
891 context.draw_surface(leveldot_green,
892 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
894 context.draw_surface(leveldot_red,
895 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
898 tux->draw(context, offset);
899 draw_status(context);
903 WorldMap::draw_status(DrawingContext& context)
906 sprintf(str, " %d", player_status.score);
908 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
909 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
911 sprintf(str, "%d", player_status.distros);
912 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
914 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
917 if (player_status.lives >= 5)
919 sprintf(str, "%dx", player_status.lives);
920 context.draw_text(gold_text, str,
921 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
923 context.draw_surface(tux_life, Vector(screen->w -
924 gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
928 for(int i= 0; i < player_status.lives; ++i)
929 context.draw_surface(tux_life,
930 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
933 context.draw_text(white_text, _("LIVES"),
934 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
937 if (!tux->is_moving())
939 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
941 if (i->x == tux->get_tile_pos().x &&
942 i->y == tux->get_tile_pos().y)
949 context.draw_text_center(white_text, i->title,
950 Vector(0, screen->h - white_text->get_height() - 30),
954 /* Display a message in the map, if any as been selected */
955 if(!i->display_map_message.empty())
956 context.draw_text_center(gold_text, i->display_map_message,
957 Vector(0, screen->h - white_text->get_height() - 60),
968 Menu::set_current(0);
972 song = sound_manager->load_music(datadir + "/music/" + music);
973 sound_manager->play_music(song);
975 unsigned int last_update_time;
976 unsigned int update_time;
978 last_update_time = update_time = st_get_ticks();
980 DrawingContext context;
983 float delta = ((float)(update_time-last_update_time))/100.0;
990 last_update_time = update_time;
991 update_time = st_get_ticks();
993 Vector tux_pos = tux->get_pos();
996 offset.x = -tux_pos.x + screen->w/2;
997 offset.y = -tux_pos.y + screen->h/2;
999 if (offset.x > 0) offset.x = 0;
1000 if (offset.y > 0) offset.y = 0;
1002 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
1003 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
1006 draw(context, offset);
1012 Menu::current()->draw(context);
1013 mouse_cursor->draw(context);
1016 context.do_drawing();
1023 WorldMap::savegame(const std::string& filename)
1028 std::cout << "savegame: " << filename << std::endl;
1029 std::ofstream out(filename.c_str());
1031 int nb_solved_levels = 0;
1032 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1038 out << "(supertux-savegame\n"
1040 << " (title \"" << name << " - " << nb_solved_levels << "/" << levels.size() << "\")\n"
1041 << " (map \"" << map_filename << "\")\n"
1042 << " (lives " << player_status.lives << ")\n"
1043 << " (score " << player_status.score << ")\n"
1044 << " (distros " << player_status.distros << ")\n"
1045 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
1046 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
1047 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
1050 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1054 out << " (level (name \"" << i->name << "\")\n"
1055 << " (solved #t))\n";
1060 << " )\n\n;; EOF ;;" << std::endl;
1064 WorldMap::loadgame(const std::string& filename)
1066 std::cout << "loadgame: " << filename << std::endl;
1067 savegame_file = filename;
1068 map_filename = "icyisland.stwm";
1070 if (access(filename.c_str(), F_OK) != 0)
1076 lisp_object_t* savegame = lisp_read_from_file(filename);
1079 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1084 lisp_object_t* cur = savegame;
1086 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1092 cur = lisp_cdr(cur);
1093 LispReader reader(cur);
1095 /* Get the Map filename and then load it before setting level settings */
1096 reader.read_string("map", map_filename);
1099 reader.read_int("lives", player_status.lives);
1100 reader.read_int("score", player_status.score);
1101 reader.read_int("distros", player_status.distros);
1103 if (player_status.lives < 0)
1104 player_status.lives = START_LIVES;
1106 lisp_object_t* tux_cur = 0;
1107 if (reader.read_lisp("tux", tux_cur))
1110 std::string back_str = "none";
1111 std::string bonus_str = "none";
1113 LispReader tux_reader(tux_cur);
1114 tux_reader.read_float("x", p.x);
1115 tux_reader.read_float("y", p.y);
1116 tux_reader.read_string("back", back_str);
1117 tux_reader.read_string("bonus", bonus_str);
1119 player_status.bonus = string_to_bonus(bonus_str);
1120 tux->back_direction = string_to_direction(back_str);
1121 tux->set_tile_pos(p);
1124 lisp_object_t* level_cur = 0;
1125 if (reader.read_lisp("levels", level_cur))
1129 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1130 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1132 if (strcmp(lisp_symbol(sym), "level") == 0)
1135 bool solved = false;
1137 LispReader level_reader(data);
1138 level_reader.read_string("name", name, true);
1139 level_reader.read_bool("solved", solved);
1141 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1143 if (name == i->name)
1148 level_cur = lisp_cdr(level_cur);
1152 lisp_free(savegame);
1156 WorldMap::loadmap(const std::string& filename)
1159 map_filename = filename;
1163 } // namespace WorldMapNS
1165 /* Local Variables: */