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;
114 LispReader reader(lisp_cdr(element));
115 reader.read_int("id", &id);
116 reader.read_bool("north", &tile->north);
117 reader.read_bool("south", &tile->south);
118 reader.read_bool("west", &tile->west);
119 reader.read_bool("east", &tile->east);
120 reader.read_bool("stop", &tile->stop);
121 reader.read_string("image", &filename);
123 tile->sprite = new Surface(
124 datadir + "/images/worldmap/" + filename,
127 if (id >= int(tiles.size()))
134 puts("Unhandled symbol");
148 TileManager::~TileManager()
150 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
155 TileManager::get(int i)
157 assert(i >=0 && i < int(tiles.size()));
161 //---------------------------------------------------------------------------
163 Tux::Tux(WorldMap* worldmap_)
164 : worldmap(worldmap_)
166 sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
172 input_direction = NONE;
181 Tux::draw(const Point& offset)
183 Point pos = get_pos();
184 sprite->draw(pos.x + offset.x,
185 pos.y + offset.y - 10);
192 float x = tile_pos.x * 32;
193 float y = tile_pos.y * 32;
213 return Point((int)x, (int)y);
225 Tux::update(float delta)
229 if (input_direction != NONE)
231 WorldMap::Level* level = worldmap->at_level();
233 // We got a new direction, so lets start walking when possible
235 if ((!level || level->solved)
236 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
238 tile_pos = next_tile;
240 direction = input_direction;
241 back_direction = reverse_dir(direction);
243 else if (input_direction == back_direction)
245 std::cout << "Back triggered" << std::endl;
247 direction = input_direction;
248 tile_pos = worldmap->get_next_tile(tile_pos, direction);
249 back_direction = reverse_dir(direction);
255 // Let tux walk a few pixels (20 pixel/sec)
256 offset += 20.0f * delta;
259 { // We reached the next tile, so we check what to do now
262 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
268 // Walk automatically to the next tile
270 if (worldmap->path_ok(direction, tile_pos, &next_tile))
272 tile_pos = next_tile;
276 puts("Tilemap data is buggy");
284 //---------------------------------------------------------------------------
294 //---------------------------------------------------------------------------
298 tile_manager = new TileManager();
304 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
305 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
306 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
308 input_direction = NONE;
312 music = "SALCON.MOD";
317 WorldMap::~WorldMap()
323 delete leveldot_green;
330 std::string filename = datadir + "levels/default/worldmap.stwm";
332 lisp_object_t* root_obj = lisp_read_from_file(filename);
334 st_abort("Couldn't load file", filename);
336 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
338 lisp_object_t* cur = lisp_cdr(root_obj);
340 while(!lisp_nil_p(cur))
342 lisp_object_t* element = lisp_car(cur);
344 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
346 LispReader reader(lisp_cdr(element));
347 reader.read_int("width", &width);
348 reader.read_int("height", &height);
349 reader.read_int_vector("data", &tilemap);
351 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
353 LispReader reader(lisp_cdr(element));
354 reader.read_string("name", &name);
355 reader.read_string("music", &music);
357 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
359 lisp_object_t* cur = lisp_cdr(element);
361 while(!lisp_nil_p(cur))
363 lisp_object_t* element = lisp_car(cur);
365 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
368 LispReader reader(lisp_cdr(element));
369 level.solved = false;
376 reader.read_string("name", &level.name);
377 reader.read_int("x", &level.x);
378 reader.read_int("y", &level.y);
380 get_level_title(&level); // get level's title
382 levels.push_back(level);
400 void WorldMap::get_level_title(Levels::pointer level)
402 /** get level's title */
403 level->title = "<no title>";
406 lisp_object_t* root_obj = 0;
407 fi = fopen((datadir + "levels/" + level->name).c_str(), "r");
410 perror((datadir + "levels/" + level->name).c_str());
414 lisp_stream_t stream;
415 lisp_stream_init_file (&stream, fi);
416 root_obj = lisp_read (&stream);
418 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
420 printf("World: Parse Error in file %s", level->name.c_str());
423 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
425 LispReader reader(lisp_cdr(root_obj));
426 reader.read_string("name", &level->title);
435 WorldMap::on_escape_press()
437 // Show or hide the menu
439 Menu::set_current(worldmap_menu);
441 Menu::set_current(0);
445 WorldMap::get_input()
448 input_direction = NONE;
451 while (SDL_PollEvent(&event))
455 Menu::current()->event(event);
462 st_abort("Received window close", "");
466 switch(event.key.keysym.sym)
480 case SDL_JOYAXISMOTION:
481 if (event.jaxis.axis == joystick_keymap.x_axis)
483 if (event.jaxis.value < -joystick_keymap.dead_zone)
484 input_direction = WEST;
485 else if (event.jaxis.value > joystick_keymap.dead_zone)
486 input_direction = EAST;
488 else if (event.jaxis.axis == joystick_keymap.y_axis)
490 if (event.jaxis.value > joystick_keymap.dead_zone)
491 input_direction = SOUTH;
492 else if (event.jaxis.value < -joystick_keymap.dead_zone)
493 input_direction = NORTH;
497 case SDL_JOYBUTTONDOWN:
498 if (event.jbutton.button == joystick_keymap.b_button)
500 else if (event.jbutton.button == joystick_keymap.start_button)
510 if (!Menu::current())
512 Uint8 *keystate = SDL_GetKeyState(NULL);
514 if (keystate[SDLK_LEFT])
515 input_direction = WEST;
516 else if (keystate[SDLK_RIGHT])
517 input_direction = EAST;
518 else if (keystate[SDLK_UP])
519 input_direction = NORTH;
520 else if (keystate[SDLK_DOWN])
521 input_direction = SOUTH;
526 WorldMap::get_next_tile(Point pos, Direction direction)
549 WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
551 *new_pos = get_next_tile(old_pos, direction);
553 if (!(new_pos->x >= 0 && new_pos->x < width
554 && new_pos->y >= 0 && new_pos->y < height))
555 { // New position is outsite the tilemap
559 { // Check if we the tile allows us to go to new_pos
563 return (at(old_pos)->west && at(*new_pos)->east);
566 return (at(old_pos)->east && at(*new_pos)->west);
569 return (at(old_pos)->north && at(*new_pos)->south);
572 return (at(old_pos)->south && at(*new_pos)->north);
575 assert(!"path_ok() can't work if direction is NONE");
584 if (enter_level && !tux->is_moving())
586 Level* level = at_level();
589 if (level->x == tux->get_tile_pos().x &&
590 level->y == tux->get_tile_pos().y)
592 std::cout << "Enter the current level: " << level->name << std::endl;;
593 GameSession session(datadir + "levels/" + level->name,
594 1, ST_GL_LOAD_LEVEL_FILE);
596 switch (session.run())
598 case GameSession::LEVEL_FINISHED:
600 bool old_level_state = level->solved;
601 level->solved = true;
603 if (session.get_world()->get_tux()->got_coffee)
604 player_status.bonus = PlayerStatus::FLOWER_BONUS;
605 else if (session.get_world()->get_tux()->size == BIG)
606 player_status.bonus = PlayerStatus::GROWUP_BONUS;
608 player_status.bonus = PlayerStatus::NO_BONUS;
610 if (old_level_state != level->solved)
611 { // Try to detect the next direction to which we should walk
612 // FIXME: Mostly a hack
613 Direction dir = NONE;
615 Tile* tile = at(tux->get_tile_pos());
617 if (tile->north && tux->back_direction != NORTH)
619 else if (tile->south && tux->back_direction != SOUTH)
621 else if (tile->east && tux->back_direction != EAST)
623 else if (tile->west && tux->back_direction != WEST)
628 tux->set_direction(dir);
632 std::cout << "Walk to dir: " << dir << std::endl;
637 case GameSession::LEVEL_ABORT:
638 // Reseting the player_status might be a worthy
639 // consideration, but I don't think we need it
640 // 'cause only the bad players will use it to
641 // 'cheat' a few items and that isn't necesarry a
642 // bad thing (ie. better they continue that way,
643 // then stop playing the game all together since it
646 case GameSession::GAME_OVER:
648 player_status.bonus = PlayerStatus::NO_BONUS;
650 case GameSession::NONE:
651 // Should never be reached
655 music_manager->play_music(song);
656 Menu::set_current(0);
657 if (!savegame_file.empty())
658 savegame(savegame_file);
664 std::cout << "Nothing to enter at: "
665 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
670 tux->set_direction(input_direction);
674 Menu* menu = Menu::current();
679 if(menu == worldmap_menu)
681 switch (worldmap_menu->check())
683 case MNID_RETURNWORLDMAP: // Return to game
686 if (!savegame_file.empty())
687 savegame(savegame_file);
690 case MNID_QUITWORLDMAP: // Quit Worldmap
695 else if(menu == options_menu)
697 process_options_menu();
703 WorldMap::at(Point p)
710 return tile_manager->get(tilemap[width * p.y + p.x]);
716 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
718 if (i->x == tux->get_tile_pos().x &&
719 i->y == tux->get_tile_pos().y)
728 WorldMap::draw(const Point& offset)
730 for(int y = 0; y < height; ++y)
731 for(int x = 0; x < width; ++x)
733 Tile* tile = at(Point(x, y));
734 tile->sprite->draw(x*32 + offset.x,
738 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
741 leveldot_green->draw(i->x*32 + offset.x,
744 leveldot_red->draw(i->x*32 + offset.x,
753 WorldMap::draw_status()
756 sprintf(str, "%d", player_status.score);
757 white_text->draw("SCORE", 0, 0);
758 gold_text->draw(str, 96, 0);
760 sprintf(str, "%d", player_status.distros);
761 white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP);
762 gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP);
764 white_text->draw("LIVES", 480, 0);
765 if (player_status.lives >= 5)
767 sprintf(str, "%dx", player_status.lives);
768 gold_text->draw(str, 585, 0);
769 tux_life->draw(565+(18*3), 0);
773 for(int i= 0; i < player_status.lives; ++i)
774 tux_life->draw(565+(18*i),0);
777 if (!tux->is_moving())
779 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
781 if (i->x == tux->get_tile_pos().x &&
782 i->y == tux->get_tile_pos().y)
784 white_text->draw_align(i->title.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM);
794 Menu::set_current(0);
798 song = music_manager->load_music(datadir + "/music/" + music);
799 music_manager->play_music(song);
802 Point tux_pos = tux->get_pos();
805 offset.x = -tux_pos.x + screen->w/2;
806 offset.y = -tux_pos.y + screen->h/2;
808 if (offset.x > 0) offset.x = 0;
809 if (offset.y > 0) offset.y = 0;
811 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
812 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
821 Menu::current()->draw();
822 mouse_cursor->draw();
831 WorldMap::savegame(const std::string& filename)
833 std::cout << "savegame: " << filename << std::endl;
834 std::ofstream out(filename.c_str());
836 int nb_solved_levels = 0;
837 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
843 out << "(supertux-savegame\n"
845 << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
846 << " (lives " << player_status.lives << ")\n"
847 << " (score " << player_status.score << ")\n"
848 << " (distros " << player_status.distros << ")\n"
849 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
850 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
851 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
854 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
858 out << " (level (name \"" << i->name << "\")\n"
859 << " (solved #t))\n";
864 << " )\n\n;; EOF ;;" << std::endl;
868 WorldMap::loadgame(const std::string& filename)
870 std::cout << "loadgame: " << filename << std::endl;
871 savegame_file = filename;
873 if (access(filename.c_str(), F_OK) != 0)
876 lisp_object_t* savegame = lisp_read_from_file(filename);
877 lisp_object_t* cur = savegame;
879 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
883 LispReader reader(cur);
885 reader.read_int("lives", &player_status.lives);
886 reader.read_int("score", &player_status.score);
887 reader.read_int("distros", &player_status.distros);
889 if (player_status.lives < 0)
890 player_status.lives = START_LIVES;
892 lisp_object_t* tux_cur = 0;
893 if (reader.read_lisp("tux", &tux_cur))
896 std::string back_str = "none";
897 std::string bonus_str = "none";
899 LispReader tux_reader(tux_cur);
900 tux_reader.read_int("x", &p.x);
901 tux_reader.read_int("y", &p.y);
902 tux_reader.read_string("back", &back_str);
903 tux_reader.read_string("bonus", &bonus_str);
905 player_status.bonus = string_to_bonus(bonus_str);
906 tux->back_direction = string_to_direction(back_str);
907 tux->set_tile_pos(p);
910 lisp_object_t* level_cur = 0;
911 if (reader.read_lisp("levels", &level_cur))
915 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
916 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
918 if (strcmp(lisp_symbol(sym), "level") == 0)
923 LispReader level_reader(data);
924 level_reader.read_string("name", &name);
925 level_reader.read_bool("solved", &solved);
927 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
934 level_cur = lisp_cdr(level_cur);
941 } // namespace WorldMapNS
943 /* Local Variables: */