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 "lispreader.h"
31 #include "resources.h"
33 namespace WorldMapNS {
35 Direction reverse_dir(Direction direction)
54 direction_to_string(Direction direction)
72 string_to_direction(const std::string& directory)
74 if (directory == "west")
76 else if (directory == "east")
78 else if (directory == "north")
80 else if (directory == "south")
86 TileManager::TileManager()
88 std::string stwt_filename = datadir + "/images/worldmap/antarctica.stwt";
89 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
92 st_abort("Couldn't load file", stwt_filename);
94 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
96 lisp_object_t* cur = lisp_cdr(root_obj);
98 while(!lisp_nil_p(cur))
100 lisp_object_t* element = lisp_car(cur);
102 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
105 std::string filename = "<invalid>";
107 Tile* tile = new Tile;
113 tile->auto_walk = false;
115 LispReader reader(lisp_cdr(element));
116 reader.read_int("id", &id);
117 reader.read_bool("north", &tile->north);
118 reader.read_bool("south", &tile->south);
119 reader.read_bool("west", &tile->west);
120 reader.read_bool("east", &tile->east);
121 reader.read_bool("stop", &tile->stop);
122 reader.read_bool("auto-walk", &tile->auto_walk);
123 reader.read_string("image", &filename);
125 tile->sprite = new Surface(
126 datadir + "/images/worldmap/" + filename,
129 if (id >= int(tiles.size()))
136 puts("Unhandled symbol");
150 TileManager::~TileManager()
152 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
157 TileManager::get(int i)
159 assert(i >=0 && i < int(tiles.size()));
163 //---------------------------------------------------------------------------
165 Tux::Tux(WorldMap* worldmap_)
166 : worldmap(worldmap_)
168 largetux_sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
169 firetux_sprite = new Surface(datadir + "/images/worldmap/firetux.png", USE_ALPHA);
170 smalltux_sprite = new Surface(datadir + "/images/worldmap/smalltux.png", USE_ALPHA);
177 input_direction = NONE;
182 delete smalltux_sprite;
183 delete firetux_sprite;
184 delete largetux_sprite;
188 Tux::draw(const Point& offset)
190 Point pos = get_pos();
191 switch (player_status.bonus)
193 case PlayerStatus::GROWUP_BONUS:
194 largetux_sprite->draw(pos.x + offset.x,
195 pos.y + offset.y - 10);
197 case PlayerStatus::FLOWER_BONUS:
198 firetux_sprite->draw(pos.x + offset.x,
199 pos.y + offset.y - 10);
201 case PlayerStatus::NO_BONUS:
202 smalltux_sprite->draw(pos.x + offset.x,
203 pos.y + offset.y - 10);
212 float x = tile_pos.x * 32;
213 float y = tile_pos.y * 32;
233 return Point((int)x, (int)y);
245 Tux::update(float delta)
249 if (input_direction != NONE)
251 WorldMap::Level* level = worldmap->at_level();
253 // We got a new direction, so lets start walking when possible
255 if ((!level || level->solved)
256 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
258 tile_pos = next_tile;
260 direction = input_direction;
261 back_direction = reverse_dir(direction);
263 else if (input_direction == back_direction)
265 std::cout << "Back triggered" << std::endl;
267 direction = input_direction;
268 tile_pos = worldmap->get_next_tile(tile_pos, direction);
269 back_direction = reverse_dir(direction);
275 // Let tux walk a few pixels (20 pixel/sec)
276 offset += 20.0f * delta;
279 { // We reached the next tile, so we check what to do now
282 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
288 if (worldmap->at(tile_pos)->auto_walk)
289 { // Turn to a new direction
290 Tile* tile = worldmap->at(tile_pos);
291 Direction dir = NONE;
293 if (tile->north && back_direction != NORTH)
295 else if (tile->south && back_direction != SOUTH)
297 else if (tile->east && back_direction != EAST)
299 else if (tile->west && back_direction != WEST)
305 back_direction = reverse_dir(direction);
309 // Should never be reached if tiledata is good
315 // Walk automatically to the next tile
317 if (worldmap->path_ok(direction, tile_pos, &next_tile))
319 tile_pos = next_tile;
323 puts("Tilemap data is buggy");
331 //---------------------------------------------------------------------------
341 //---------------------------------------------------------------------------
345 tile_manager = new TileManager();
351 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
352 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
353 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
355 input_direction = NONE;
359 music = "SALCON.MOD";
364 WorldMap::~WorldMap()
370 delete leveldot_green;
377 std::string filename = datadir + "/levels/default/worldmap.stwm";
379 lisp_object_t* root_obj = lisp_read_from_file(filename);
381 st_abort("Couldn't load file", filename);
383 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
385 lisp_object_t* cur = lisp_cdr(root_obj);
387 while(!lisp_nil_p(cur))
389 lisp_object_t* element = lisp_car(cur);
391 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
393 LispReader reader(lisp_cdr(element));
394 reader.read_int("width", &width);
395 reader.read_int("height", &height);
396 reader.read_int_vector("data", &tilemap);
398 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
400 LispReader reader(lisp_cdr(element));
401 reader.read_string("name", &name);
402 reader.read_string("music", &music);
404 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
406 lisp_object_t* cur = lisp_cdr(element);
408 while(!lisp_nil_p(cur))
410 lisp_object_t* element = lisp_car(cur);
412 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
415 LispReader reader(lisp_cdr(element));
416 level.solved = false;
423 reader.read_string("extro-filename", &level.extro_filename);
424 reader.read_string("name", &level.name);
425 reader.read_int("x", &level.x);
426 reader.read_int("y", &level.y);
428 get_level_title(&level); // get level's title
430 levels.push_back(level);
448 void WorldMap::get_level_title(Levels::pointer 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 = 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 = WEST;
533 else if (event.jaxis.value > joystick_keymap.dead_zone)
534 input_direction = EAST;
536 else if (event.jaxis.axis == joystick_keymap.y_axis)
538 if (event.jaxis.value > joystick_keymap.dead_zone)
539 input_direction = SOUTH;
540 else if (event.jaxis.value < -joystick_keymap.dead_zone)
541 input_direction = 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 = WEST;
564 else if (keystate[SDLK_RIGHT])
565 input_direction = EAST;
566 else if (keystate[SDLK_UP])
567 input_direction = NORTH;
568 else if (keystate[SDLK_DOWN])
569 input_direction = SOUTH;
574 WorldMap::get_next_tile(Point pos, Direction direction)
597 WorldMap::path_ok(Direction direction, Point old_pos, Point* 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 std::cout << "Enter the current level: " << level->name << std::endl;;
641 GameSession session(datadir + "/levels/" + level->name,
642 1, ST_GL_LOAD_LEVEL_FILE);
644 switch (session.run())
646 case GameSession::LEVEL_FINISHED:
648 bool old_level_state = level->solved;
649 level->solved = true;
651 if (session.get_world()->get_tux()->got_coffee)
652 player_status.bonus = PlayerStatus::FLOWER_BONUS;
653 else if (session.get_world()->get_tux()->size == BIG)
654 player_status.bonus = PlayerStatus::GROWUP_BONUS;
656 player_status.bonus = PlayerStatus::NO_BONUS;
658 if (old_level_state != level->solved)
659 { // Try to detect the next direction to which we should walk
660 // FIXME: Mostly a hack
661 Direction dir = NONE;
663 Tile* tile = at(tux->get_tile_pos());
665 if (tile->north && tux->back_direction != NORTH)
667 else if (tile->south && tux->back_direction != SOUTH)
669 else if (tile->east && tux->back_direction != EAST)
671 else if (tile->west && tux->back_direction != WEST)
676 tux->set_direction(dir);
677 //tux->update(delta);
680 std::cout << "Walk to dir: " << dir << std::endl;
683 if (!level->extro_filename.empty())
686 music_manager->load_music(datadir + "/music/theme.mod");
687 music_manager->play_music(theme);
688 // Display final credits and go back to the main menu
689 display_text_file(level->extro_filename,
690 "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
691 display_text_file("CREDITS",
692 "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
698 case GameSession::LEVEL_ABORT:
699 // Reseting the player_status might be a worthy
700 // consideration, but I don't think we need it
701 // 'cause only the bad players will use it to
702 // 'cheat' a few items and that isn't necesarry a
703 // bad thing (ie. better they continue that way,
704 // then stop playing the game all together since it
707 case GameSession::GAME_OVER:
709 player_status.bonus = PlayerStatus::NO_BONUS;
711 case GameSession::NONE:
712 // Should never be reached
716 music_manager->play_music(song);
717 Menu::set_current(0);
718 if (!savegame_file.empty())
719 savegame(savegame_file);
725 std::cout << "Nothing to enter at: "
726 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
732 tux->set_direction(input_direction);
735 Menu* menu = Menu::current();
740 if(menu == worldmap_menu)
742 switch (worldmap_menu->check())
744 case MNID_RETURNWORLDMAP: // Return to game
747 if (!savegame_file.empty())
748 savegame(savegame_file);
751 case MNID_QUITWORLDMAP: // Quit Worldmap
756 else if(menu == options_menu)
758 process_options_menu();
764 WorldMap::at(Point p)
771 return tile_manager->get(tilemap[width * p.y + p.x]);
777 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
779 if (i->x == tux->get_tile_pos().x &&
780 i->y == tux->get_tile_pos().y)
789 WorldMap::draw(const Point& offset)
791 for(int y = 0; y < height; ++y)
792 for(int x = 0; x < width; ++x)
794 Tile* tile = at(Point(x, y));
795 tile->sprite->draw(x*32 + offset.x,
799 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
802 leveldot_green->draw(i->x*32 + offset.x,
805 leveldot_red->draw(i->x*32 + offset.x,
814 WorldMap::draw_status()
817 sprintf(str, "%d", player_status.score);
818 white_text->draw("SCORE", 0, 0);
819 gold_text->draw(str, 96, 0);
821 sprintf(str, "%d", player_status.distros);
822 white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP);
823 gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP);
825 white_text->draw("LIVES", 480, 0);
826 if (player_status.lives >= 5)
828 sprintf(str, "%dx", player_status.lives);
829 gold_text->draw_align(str, 617, 0, A_RIGHT, A_TOP);
830 tux_life->draw(565+(18*3), 0);
834 for(int i= 0; i < player_status.lives; ++i)
835 tux_life->draw(565+(18*i),0);
838 if (!tux->is_moving())
840 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
842 if (i->x == tux->get_tile_pos().x &&
843 i->y == tux->get_tile_pos().y)
845 white_text->draw_align(i->title.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM);
855 Menu::set_current(0);
859 song = music_manager->load_music(datadir + "/music/" + music);
860 music_manager->play_music(song);
862 unsigned int last_update_time;
863 unsigned int update_time;
865 last_update_time = update_time = st_get_ticks();
869 float delta = ((float)(update_time-last_update_time))/100.0;
876 last_update_time = update_time;
877 update_time = st_get_ticks();
879 Point tux_pos = tux->get_pos();
882 offset.x = -tux_pos.x + screen->w/2;
883 offset.y = -tux_pos.y + screen->h/2;
885 if (offset.x > 0) offset.x = 0;
886 if (offset.y > 0) offset.y = 0;
888 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
889 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
898 Menu::current()->draw();
899 mouse_cursor->draw();
908 WorldMap::savegame(const std::string& filename)
910 std::cout << "savegame: " << filename << std::endl;
911 std::ofstream out(filename.c_str());
913 int nb_solved_levels = 0;
914 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
920 out << "(supertux-savegame\n"
922 << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
923 << " (lives " << player_status.lives << ")\n"
924 << " (score " << player_status.score << ")\n"
925 << " (distros " << player_status.distros << ")\n"
926 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
927 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
928 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
931 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
935 out << " (level (name \"" << i->name << "\")\n"
936 << " (solved #t))\n";
941 << " )\n\n;; EOF ;;" << std::endl;
945 WorldMap::loadgame(const std::string& filename)
947 std::cout << "loadgame: " << filename << std::endl;
948 savegame_file = filename;
950 if (access(filename.c_str(), F_OK) != 0)
953 lisp_object_t* savegame = lisp_read_from_file(filename);
956 std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
960 lisp_object_t* cur = savegame;
962 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
966 LispReader reader(cur);
968 reader.read_int("lives", &player_status.lives);
969 reader.read_int("score", &player_status.score);
970 reader.read_int("distros", &player_status.distros);
972 if (player_status.lives < 0)
973 player_status.lives = START_LIVES;
975 lisp_object_t* tux_cur = 0;
976 if (reader.read_lisp("tux", &tux_cur))
979 std::string back_str = "none";
980 std::string bonus_str = "none";
982 LispReader tux_reader(tux_cur);
983 tux_reader.read_int("x", &p.x);
984 tux_reader.read_int("y", &p.y);
985 tux_reader.read_string("back", &back_str);
986 tux_reader.read_string("bonus", &bonus_str);
988 player_status.bonus = string_to_bonus(bonus_str);
989 tux->back_direction = string_to_direction(back_str);
990 tux->set_tile_pos(p);
993 lisp_object_t* level_cur = 0;
994 if (reader.read_lisp("levels", &level_cur))
998 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
999 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1001 if (strcmp(lisp_symbol(sym), "level") == 0)
1004 bool solved = false;
1006 LispReader level_reader(data);
1007 level_reader.read_string("name", &name);
1008 level_reader.read_bool("solved", &solved);
1010 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1012 if (name == i->name)
1017 level_cur = lisp_cdr(level_cur);
1021 lisp_free(savegame);
1024 } // namespace WorldMapNS
1026 /* Local Variables: */