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";
367 WorldMap::~WorldMap()
373 delete leveldot_green;
380 lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
382 st_abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
384 std::cout << "Loading map: " << datadir + "/levels/worldmap/" + map_filename << std::endl;
386 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
388 lisp_object_t* cur = lisp_cdr(root_obj);
390 while(!lisp_nil_p(cur))
392 lisp_object_t* element = lisp_car(cur);
394 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
396 LispReader reader(lisp_cdr(element));
397 reader.read_int("width", width);
398 reader.read_int("height", height);
399 reader.read_int_vector("data", tilemap);
401 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
403 LispReader reader(lisp_cdr(element));
404 reader.read_string("name", name);
405 reader.read_string("music", music);
407 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
409 lisp_object_t* cur = lisp_cdr(element);
411 while(!lisp_nil_p(cur))
413 lisp_object_t* element = lisp_car(cur);
415 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
418 LispReader reader(lisp_cdr(element));
419 level.solved = false;
426 reader.read_string("extro-filename", level.extro_filename);
427 reader.read_string("name", level.name);
428 reader.read_int("x", level.x);
429 reader.read_int("y", level.y);
431 levels.push_back(level);
449 void WorldMap::get_level_title(Level& level)
451 /** get level's title */
452 level.title = "<no title>";
455 lisp_object_t* root_obj = 0;
456 fi = fopen((datadir + "/levels/" + level.name).c_str(), "r");
459 perror((datadir + "/levels/" + level.name).c_str());
463 lisp_stream_t stream;
464 lisp_stream_init_file (&stream, fi);
465 root_obj = lisp_read (&stream);
467 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
469 printf("World: Parse Error in file %s", level.name.c_str());
472 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
474 LispReader reader(lisp_cdr(root_obj));
475 reader.read_string("name", level.title);
484 WorldMap::on_escape_press()
486 // Show or hide the menu
488 Menu::set_current(worldmap_menu);
490 Menu::set_current(0);
494 WorldMap::get_input()
497 input_direction = D_NONE;
500 while (SDL_PollEvent(&event))
504 Menu::current()->event(event);
511 st_abort("Received window close", "");
515 switch(event.key.keysym.sym)
529 case SDL_JOYAXISMOTION:
530 if (event.jaxis.axis == joystick_keymap.x_axis)
532 if (event.jaxis.value < -joystick_keymap.dead_zone)
533 input_direction = D_WEST;
534 else if (event.jaxis.value > joystick_keymap.dead_zone)
535 input_direction = D_EAST;
537 else if (event.jaxis.axis == joystick_keymap.y_axis)
539 if (event.jaxis.value > joystick_keymap.dead_zone)
540 input_direction = D_SOUTH;
541 else if (event.jaxis.value < -joystick_keymap.dead_zone)
542 input_direction = D_NORTH;
546 case SDL_JOYBUTTONDOWN:
547 if (event.jbutton.button == joystick_keymap.b_button)
549 else if (event.jbutton.button == joystick_keymap.start_button)
559 if (!Menu::current())
561 Uint8 *keystate = SDL_GetKeyState(NULL);
563 if (keystate[SDLK_LEFT])
564 input_direction = D_WEST;
565 else if (keystate[SDLK_RIGHT])
566 input_direction = D_EAST;
567 else if (keystate[SDLK_UP])
568 input_direction = D_NORTH;
569 else if (keystate[SDLK_DOWN])
570 input_direction = D_SOUTH;
575 WorldMap::get_next_tile(Vector pos, Direction direction)
598 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
600 *new_pos = get_next_tile(old_pos, direction);
602 if (!(new_pos->x >= 0 && new_pos->x < width
603 && new_pos->y >= 0 && new_pos->y < height))
604 { // New position is outsite the tilemap
608 { // Check if we the tile allows us to go to new_pos
612 return (at(old_pos)->west && at(*new_pos)->east);
615 return (at(old_pos)->east && at(*new_pos)->west);
618 return (at(old_pos)->north && at(*new_pos)->south);
621 return (at(old_pos)->south && at(*new_pos)->north);
624 assert(!"path_ok() can't work if direction is NONE");
631 WorldMap::update(float delta)
633 if (enter_level && !tux->is_moving())
635 Level* level = at_level();
638 if (level->x == tux->get_tile_pos().x &&
639 level->y == tux->get_tile_pos().y)
641 PlayerStatus old_player_status = player_status;
643 std::cout << "Enter the current level: " << level->name << std::endl;
644 // do a shriking fade to the level
645 shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
647 GameSession session(datadir + "/levels/" + level->name,
648 ST_GL_LOAD_LEVEL_FILE);
650 switch (session.run())
652 case GameSession::ES_LEVEL_FINISHED:
654 bool old_level_state = level->solved;
655 level->solved = true;
657 if (session.get_current_sector()->player->got_power !=
658 session.get_current_sector()->player->NONE_POWER)
659 player_status.bonus = PlayerStatus::FLOWER_BONUS;
660 else if (session.get_current_sector()->player->size == BIG)
661 player_status.bonus = PlayerStatus::GROWUP_BONUS;
663 player_status.bonus = PlayerStatus::NO_BONUS;
665 if (old_level_state != level->solved)
666 { // Try to detect the next direction to which we should walk
667 // FIXME: Mostly a hack
668 Direction dir = D_NONE;
670 Tile* tile = at(tux->get_tile_pos());
672 if (tile->north && tux->back_direction != D_NORTH)
674 else if (tile->south && tux->back_direction != D_SOUTH)
676 else if (tile->east && tux->back_direction != D_EAST)
678 else if (tile->west && tux->back_direction != D_WEST)
683 tux->set_direction(dir);
684 //tux->update(delta);
687 std::cout << "Walk to dir: " << dir << std::endl;
690 if (!level->extro_filename.empty())
693 sound_manager->load_music(datadir + "/music/theme.mod");
694 sound_manager->play_music(theme);
695 // Display final credits and go back to the main menu
696 display_text_file(level->extro_filename,
697 "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
698 display_text_file("CREDITS",
699 "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
705 case GameSession::ES_LEVEL_ABORT:
706 /* In case the player's abort the level, keep it using the old
708 player_status = old_player_status;
710 case GameSession::ES_GAME_OVER:
712 /* draw an end screen */
713 /* in the future, this should make a dialog a la SuperMario, asking
714 if the player wants to restart the world map with no score and from
718 DrawingContext context;
719 context.draw_gradient(Color (0, 255, 0), Color (255, 0, 255),
722 context.draw_text_center(blue_text, _("GAMEOVER"),
723 Vector(0, 200), LAYER_FOREGROUND1);
725 sprintf(str, _("SCORE: %d"), player_status.score);
726 context.draw_text_center(gold_text, str,
727 Vector(0, 224), LAYER_FOREGROUND1);
729 sprintf(str, _("COINS: %d"), player_status.distros);
730 context.draw_text_center(gold_text, str,
731 Vector(0, screen->w - 32), LAYER_FOREGROUND1);
733 context.do_drawing();
736 wait_for_event(event,2000,5000,true);
739 player_status.reset();
742 case GameSession::ES_NONE:
744 // Should never be reached
748 sound_manager->play_music(song);
749 Menu::set_current(0);
750 if (!savegame_file.empty())
751 savegame(savegame_file);
757 std::cout << "Nothing to enter at: "
758 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
764 tux->set_direction(input_direction);
767 Menu* menu = Menu::current();
772 if(menu == worldmap_menu)
774 switch (worldmap_menu->check())
776 case MNID_RETURNWORLDMAP: // Return to game
778 case MNID_QUITWORLDMAP: // Quit Worldmap
783 else if(menu == options_menu)
785 process_options_menu();
791 WorldMap::at(Vector p)
800 return tile_manager->get(tilemap[width * y + x]);
806 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
808 if (i->x == tux->get_tile_pos().x &&
809 i->y == tux->get_tile_pos().y)
818 WorldMap::draw(DrawingContext& context, const Vector& offset)
820 for(int y = 0; y < height; ++y)
821 for(int x = 0; x < width; ++x)
823 Tile* tile = at(Vector(x, y));
824 context.draw_surface(tile->sprite,
825 Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
828 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
831 context.draw_surface(leveldot_green,
832 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
834 context.draw_surface(leveldot_red,
835 Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
838 tux->draw(context, offset);
839 draw_status(context);
843 WorldMap::draw_status(DrawingContext& context)
846 sprintf(str, " %d", player_status.score);
848 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
849 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
851 sprintf(str, "%d", player_status.distros);
852 context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
854 context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
857 if (player_status.lives >= 5)
859 sprintf(str, "%dx", player_status.lives);
860 context.draw_text(gold_text, str,
861 Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
863 context.draw_surface(tux_life, Vector(screen->w -
864 gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
868 for(int i= 0; i < player_status.lives; ++i)
869 context.draw_surface(tux_life,
870 Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
873 context.draw_text(white_text, _("LIVES"),
874 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 0),
877 if (!tux->is_moving())
879 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
881 if (i->x == tux->get_tile_pos().x &&
882 i->y == tux->get_tile_pos().y)
887 context.draw_text(white_text, i->title,
888 Vector(screen->w/2 - white_text->get_text_width(i->title)/2,
889 screen->h - white_text->get_height() - 30),
900 Menu::set_current(0);
904 song = sound_manager->load_music(datadir + "/music/" + music);
905 sound_manager->play_music(song);
907 unsigned int last_update_time;
908 unsigned int update_time;
910 last_update_time = update_time = st_get_ticks();
912 DrawingContext context;
915 float delta = ((float)(update_time-last_update_time))/100.0;
922 last_update_time = update_time;
923 update_time = st_get_ticks();
925 Vector tux_pos = tux->get_pos();
928 offset.x = -tux_pos.x + screen->w/2;
929 offset.y = -tux_pos.y + screen->h/2;
931 if (offset.x > 0) offset.x = 0;
932 if (offset.y > 0) offset.y = 0;
934 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
935 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
938 draw(context, offset);
944 Menu::current()->draw(context);
945 mouse_cursor->draw(context);
948 context.do_drawing();
955 WorldMap::savegame(const std::string& filename)
957 std::cout << "savegame: " << filename << std::endl;
958 std::ofstream out(filename.c_str());
960 int nb_solved_levels = 0;
961 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
967 out << "(supertux-savegame\n"
969 << " (title \"" << name << " - " << nb_solved_levels << "/" << levels.size() << "\")\n"
970 << " (map \"" << map_filename << "\")\n"
971 << " (lives " << player_status.lives << ")\n"
972 << " (score " << player_status.score << ")\n"
973 << " (distros " << player_status.distros << ")\n"
974 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
975 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
976 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
979 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
983 out << " (level (name \"" << i->name << "\")\n"
984 << " (solved #t))\n";
989 << " )\n\n;; EOF ;;" << std::endl;
993 WorldMap::loadgame(const std::string& filename)
995 std::cout << "loadgame: " << filename << std::endl;
996 savegame_file = filename;
997 map_filename = "icyisland.stwm";
999 if (access(filename.c_str(), F_OK) != 0)
1005 lisp_object_t* savegame = lisp_read_from_file(filename);
1008 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1013 lisp_object_t* cur = savegame;
1015 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1021 cur = lisp_cdr(cur);
1022 LispReader reader(cur);
1024 /* Get the Map filename and then load it before setting level settings */
1025 reader.read_string("map", map_filename);
1028 reader.read_int("lives", player_status.lives);
1029 reader.read_int("score", player_status.score);
1030 reader.read_int("distros", player_status.distros);
1032 if (player_status.lives < 0)
1033 player_status.lives = START_LIVES;
1035 lisp_object_t* tux_cur = 0;
1036 if (reader.read_lisp("tux", tux_cur))
1039 std::string back_str = "none";
1040 std::string bonus_str = "none";
1042 LispReader tux_reader(tux_cur);
1043 tux_reader.read_float("x", p.x);
1044 tux_reader.read_float("y", p.y);
1045 tux_reader.read_string("back", back_str);
1046 tux_reader.read_string("bonus", bonus_str);
1048 player_status.bonus = string_to_bonus(bonus_str);
1049 tux->back_direction = string_to_direction(back_str);
1050 tux->set_tile_pos(p);
1053 lisp_object_t* level_cur = 0;
1054 if (reader.read_lisp("levels", level_cur))
1058 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1059 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1061 if (strcmp(lisp_symbol(sym), "level") == 0)
1064 bool solved = false;
1066 LispReader level_reader(data);
1067 level_reader.read_string("name", name);
1068 level_reader.read_bool("solved", solved);
1070 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1072 if (name == i->name)
1077 level_cur = lisp_cdr(level_cur);
1081 lisp_free(savegame);
1084 } // namespace WorldMapNS
1086 /* Local Variables: */