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.
28 #include "lispreader.h"
32 #include "resources.h"
34 namespace WorldMapNS {
36 Direction reverse_dir(Direction direction)
55 direction_to_string(Direction direction)
73 string_to_direction(const std::string& directory)
75 if (directory == "west")
77 else if (directory == "east")
79 else if (directory == "north")
81 else if (directory == "south")
87 TileManager::TileManager()
89 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
90 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
93 st_abort("Couldn't load file", stwt_filename);
95 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
97 lisp_object_t* cur = lisp_cdr(root_obj);
99 while(!lisp_nil_p(cur))
101 lisp_object_t* element = lisp_car(cur);
103 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
106 std::string filename = "<invalid>";
108 Tile* tile = new Tile;
114 tile->auto_walk = false;
116 LispReader reader(lisp_cdr(element));
117 reader.read_int("id", &id);
118 reader.read_bool("north", &tile->north);
119 reader.read_bool("south", &tile->south);
120 reader.read_bool("west", &tile->west);
121 reader.read_bool("east", &tile->east);
122 reader.read_bool("stop", &tile->stop);
123 reader.read_bool("auto-walk", &tile->auto_walk);
124 reader.read_string("image", &filename);
126 tile->sprite = new Surface(
127 datadir + "/images/worldmap/" + filename,
130 if (id >= int(tiles.size()))
137 puts("Unhandled symbol");
151 TileManager::~TileManager()
153 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
158 TileManager::get(int i)
160 assert(i >=0 && i < int(tiles.size()));
164 //---------------------------------------------------------------------------
166 Tux::Tux(WorldMap* worldmap_)
167 : worldmap(worldmap_)
169 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
170 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", USE_ALPHA);
171 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", USE_ALPHA);
178 input_direction = D_NONE;
183 delete smalltux_sprite;
184 delete firetux_sprite;
185 delete largetux_sprite;
189 Tux::draw(const Point& offset)
191 Point pos = get_pos();
192 switch (player_status.bonus)
194 case PlayerStatus::GROWUP_BONUS:
195 largetux_sprite->draw(pos.x + offset.x,
196 pos.y + offset.y - 10);
198 case PlayerStatus::FLOWER_BONUS:
199 firetux_sprite->draw(pos.x + offset.x,
200 pos.y + offset.y - 10);
202 case PlayerStatus::NO_BONUS:
203 smalltux_sprite->draw(pos.x + offset.x,
204 pos.y + offset.y - 10);
213 float x = tile_pos.x * 32;
214 float y = tile_pos.y * 32;
234 return Point((int)x, (int)y);
246 Tux::update(float delta)
250 if (input_direction != D_NONE)
252 WorldMap::Level* level = worldmap->at_level();
254 // We got a new direction, so lets start walking when possible
256 if ((!level || level->solved)
257 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
259 tile_pos = next_tile;
261 direction = input_direction;
262 back_direction = reverse_dir(direction);
264 else if (input_direction == back_direction)
266 std::cout << "Back triggered" << std::endl;
268 direction = input_direction;
269 tile_pos = worldmap->get_next_tile(tile_pos, direction);
270 back_direction = reverse_dir(direction);
276 // Let tux walk a few pixels (20 pixel/sec)
277 offset += 20.0f * delta;
280 { // We reached the next tile, so we check what to do now
283 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
289 if (worldmap->at(tile_pos)->auto_walk)
290 { // Turn to a new direction
291 Tile* tile = worldmap->at(tile_pos);
292 Direction dir = D_NONE;
294 if (tile->north && back_direction != D_NORTH)
296 else if (tile->south && back_direction != D_SOUTH)
298 else if (tile->east && back_direction != D_EAST)
300 else if (tile->west && back_direction != D_WEST)
306 back_direction = reverse_dir(direction);
310 // Should never be reached if tiledata is good
316 // Walk automatically to the next tile
318 if (worldmap->path_ok(direction, tile_pos, &next_tile))
320 tile_pos = next_tile;
324 puts("Tilemap data is buggy");
332 //---------------------------------------------------------------------------
342 //---------------------------------------------------------------------------
346 tile_manager = new TileManager();
352 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
353 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
354 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
356 input_direction = D_NONE;
360 music = "SALCON.MOD";
365 WorldMap::~WorldMap()
371 delete leveldot_green;
378 std::string filename = datadir + "/levels/default/worldmap.stwm";
380 lisp_object_t* root_obj = lisp_read_from_file(filename);
382 st_abort("Couldn't load file", filename);
384 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
386 lisp_object_t* cur = lisp_cdr(root_obj);
388 while(!lisp_nil_p(cur))
390 lisp_object_t* element = lisp_car(cur);
392 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
394 LispReader reader(lisp_cdr(element));
395 reader.read_int("width", &width);
396 reader.read_int("height", &height);
397 reader.read_int_vector("data", &tilemap);
399 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
401 LispReader reader(lisp_cdr(element));
402 reader.read_string("name", &name);
403 reader.read_string("music", &music);
405 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
407 lisp_object_t* cur = lisp_cdr(element);
409 while(!lisp_nil_p(cur))
411 lisp_object_t* element = lisp_car(cur);
413 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
416 LispReader reader(lisp_cdr(element));
417 level.solved = false;
424 reader.read_string("extro-filename", &level.extro_filename);
425 reader.read_string("name", &level.name);
426 reader.read_int("x", &level.x);
427 reader.read_int("y", &level.y);
429 get_level_title(&level); // get level's title
431 levels.push_back(level);
449 void WorldMap::get_level_title(Levels::pointer 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(Point pos, Direction direction)
598 WorldMap::path_ok(Direction direction, Point old_pos, Point* 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 std::cout << "Enter the current level: " << level->name << std::endl;;
642 GameSession session(datadir + "/levels/" + level->name,
643 1, ST_GL_LOAD_LEVEL_FILE);
645 switch (session.run())
647 case GameSession::ES_LEVEL_FINISHED:
649 bool old_level_state = level->solved;
650 level->solved = true;
652 if (session.get_world()->get_tux()->got_power !=
653 session.get_world()->get_tux()->NONE_POWER)
654 player_status.bonus = PlayerStatus::FLOWER_BONUS;
655 else if (session.get_world()->get_tux()->size == BIG)
656 player_status.bonus = PlayerStatus::GROWUP_BONUS;
658 player_status.bonus = PlayerStatus::NO_BONUS;
660 if (old_level_state != level->solved)
661 { // Try to detect the next direction to which we should walk
662 // FIXME: Mostly a hack
663 Direction dir = D_NONE;
665 Tile* tile = at(tux->get_tile_pos());
667 if (tile->north && tux->back_direction != D_NORTH)
669 else if (tile->south && tux->back_direction != D_SOUTH)
671 else if (tile->east && tux->back_direction != D_EAST)
673 else if (tile->west && tux->back_direction != D_WEST)
678 tux->set_direction(dir);
679 //tux->update(delta);
682 std::cout << "Walk to dir: " << dir << std::endl;
685 if (!level->extro_filename.empty())
688 music_manager->load_music(datadir + "/music/theme.mod");
689 music_manager->play_music(theme);
690 // Display final credits and go back to the main menu
691 display_text_file(level->extro_filename,
692 "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
693 display_text_file("CREDITS",
694 "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
700 case GameSession::ES_LEVEL_ABORT:
701 // Reseting the player_status might be a worthy
702 // consideration, but I don't think we need it
703 // 'cause only the bad players will use it to
704 // 'cheat' a few items and that isn't necesarry a
705 // bad thing (ie. better they continue that way,
706 // then stop playing the game all together since it
709 case GameSession::ES_GAME_OVER:
710 /* draw an end screen */
711 /* in the future, this should make a dialog a la SuperMario, asking
712 if the player wants to restart the world map with no score and from
716 drawgradient(Color (0, 255, 0), Color (255, 0, 255));
718 blue_text->drawf("GAMEOVER", 0, 200, A_HMIDDLE, A_TOP, 1);
720 sprintf(str, "SCORE: %d", player_status.score);
721 gold_text->drawf(str, 0, 224, A_HMIDDLE, A_TOP, 1);
723 sprintf(str, "COINS: %d", player_status.distros);
724 gold_text->drawf(str, 0, screen->w - gold_text->w*2, A_HMIDDLE, A_TOP, 1);
729 wait_for_event(event,2000,5000,true);
732 player_status.reset();
734 case GameSession::ES_NONE:
735 // Should never be reached
739 music_manager->play_music(song);
740 Menu::set_current(0);
741 if (!savegame_file.empty())
742 savegame(savegame_file);
748 std::cout << "Nothing to enter at: "
749 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
755 tux->set_direction(input_direction);
758 Menu* menu = Menu::current();
763 if(menu == worldmap_menu)
765 switch (worldmap_menu->check())
767 case MNID_RETURNWORLDMAP: // Return to game
769 case MNID_QUITWORLDMAP: // Quit Worldmap
774 else if(menu == options_menu)
776 process_options_menu();
782 WorldMap::at(Point p)
789 return tile_manager->get(tilemap[width * p.y + p.x]);
795 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
797 if (i->x == tux->get_tile_pos().x &&
798 i->y == tux->get_tile_pos().y)
807 WorldMap::draw(const Point& offset)
809 for(int y = 0; y < height; ++y)
810 for(int x = 0; x < width; ++x)
812 Tile* tile = at(Point(x, y));
813 tile->sprite->draw(x*32 + offset.x,
817 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
820 leveldot_green->draw(i->x*32 + offset.x,
823 leveldot_red->draw(i->x*32 + offset.x,
832 WorldMap::draw_status()
835 sprintf(str, "%d", player_status.score);
836 white_text->draw("SCORE", 0, 0);
837 gold_text->draw(str, 96, 0);
839 sprintf(str, "%d", player_status.distros);
840 white_text->draw_align("COINS", screen->w/2 - white_text->w*5, 0, A_LEFT, A_TOP);
841 gold_text->draw_align(str, screen->w/2 + (white_text->w*5)/2, 0, A_RIGHT, A_TOP);
843 white_text->draw("LIVES", screen->w - white_text->w*9, 0);
844 if (player_status.lives >= 5)
846 sprintf(str, "%dx", player_status.lives);
847 gold_text->draw_align(str, screen->w - gold_text->w, 0, A_RIGHT, A_TOP);
848 tux_life->draw(screen->w - gold_text->w, 0);
852 for(int i= 0; i < player_status.lives; ++i)
853 tux_life->draw(screen->w - tux_life->w*4 +(tux_life->w*i),0);
856 if (!tux->is_moving())
858 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
860 if (i->x == tux->get_tile_pos().x &&
861 i->y == tux->get_tile_pos().y)
863 white_text->draw_align(i->title.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM);
873 Menu::set_current(0);
877 song = music_manager->load_music(datadir + "/music/" + music);
878 music_manager->play_music(song);
880 unsigned int last_update_time;
881 unsigned int update_time;
883 last_update_time = update_time = st_get_ticks();
887 float delta = ((float)(update_time-last_update_time))/100.0;
894 last_update_time = update_time;
895 update_time = st_get_ticks();
897 Point tux_pos = tux->get_pos();
900 offset.x = -tux_pos.x + screen->w/2;
901 offset.y = -tux_pos.y + screen->h/2;
903 if (offset.x > 0) offset.x = 0;
904 if (offset.y > 0) offset.y = 0;
906 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
907 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
916 Menu::current()->draw();
917 mouse_cursor->draw();
926 WorldMap::savegame(const std::string& filename)
928 std::cout << "savegame: " << filename << std::endl;
929 std::ofstream out(filename.c_str());
931 int nb_solved_levels = 0;
932 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
938 out << "(supertux-savegame\n"
940 << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
941 << " (lives " << player_status.lives << ")\n"
942 << " (score " << player_status.score << ")\n"
943 << " (distros " << player_status.distros << ")\n"
944 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
945 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
946 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
949 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
953 out << " (level (name \"" << i->name << "\")\n"
954 << " (solved #t))\n";
959 << " )\n\n;; EOF ;;" << std::endl;
963 WorldMap::loadgame(const std::string& filename)
965 std::cout << "loadgame: " << filename << std::endl;
966 savegame_file = filename;
968 if (access(filename.c_str(), F_OK) != 0)
971 lisp_object_t* savegame = lisp_read_from_file(filename);
974 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
978 lisp_object_t* cur = savegame;
980 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
984 LispReader reader(cur);
986 reader.read_int("lives", &player_status.lives);
987 reader.read_int("score", &player_status.score);
988 reader.read_int("distros", &player_status.distros);
990 if (player_status.lives < 0)
991 player_status.lives = START_LIVES;
993 lisp_object_t* tux_cur = 0;
994 if (reader.read_lisp("tux", &tux_cur))
997 std::string back_str = "none";
998 std::string bonus_str = "none";
1000 LispReader tux_reader(tux_cur);
1001 tux_reader.read_int("x", &p.x);
1002 tux_reader.read_int("y", &p.y);
1003 tux_reader.read_string("back", &back_str);
1004 tux_reader.read_string("bonus", &bonus_str);
1006 player_status.bonus = string_to_bonus(bonus_str);
1007 tux->back_direction = string_to_direction(back_str);
1008 tux->set_tile_pos(p);
1011 lisp_object_t* level_cur = 0;
1012 if (reader.read_lisp("levels", &level_cur))
1016 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
1017 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1019 if (strcmp(lisp_symbol(sym), "level") == 0)
1022 bool solved = false;
1024 LispReader level_reader(data);
1025 level_reader.read_string("name", &name);
1026 level_reader.read_bool("solved", &solved);
1028 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1030 if (name == i->name)
1035 level_cur = lisp_cdr(level_cur);
1039 lisp_free(savegame);
1042 } // namespace WorldMapNS
1044 /* Local Variables: */