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:
599 level->solved = true;
600 if (session.get_world()->get_tux()->got_coffee)
601 player_status.bonus = PlayerStatus::FLOWER_BONUS;
602 else if (session.get_world()->get_tux()->size == BIG)
603 player_status.bonus = PlayerStatus::GROWUP_BONUS;
605 player_status.bonus = PlayerStatus::NO_BONUS;
607 case GameSession::LEVEL_ABORT:
608 // Reseting the player_status might be a worthy
609 // consideration, but I don't think we need it
610 // 'cause only the bad players will use it to
611 // 'cheat' a few items and that isn't necesarry a
612 // bad thing (ie. better they continue that way,
613 // then stop playing the game all together since it
616 case GameSession::GAME_OVER:
618 player_status.bonus = PlayerStatus::NO_BONUS;
620 case GameSession::NONE:
621 // Should never be reached
625 music_manager->play_music(song);
626 Menu::set_current(0);
627 if (!savegame_file.empty())
628 savegame(savegame_file);
634 std::cout << "Nothing to enter at: "
635 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
640 tux->set_direction(input_direction);
644 Menu* menu = Menu::current();
649 if(menu == worldmap_menu)
651 switch (worldmap_menu->check())
653 case MNID_RETURNWORLDMAP: // Return to game
656 if (!savegame_file.empty())
657 savegame(savegame_file);
660 case MNID_QUITWORLDMAP: // Quit Worldmap
665 else if(menu == options_menu)
667 process_options_menu();
673 WorldMap::at(Point p)
680 return tile_manager->get(tilemap[width * p.y + p.x]);
686 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
688 if (i->x == tux->get_tile_pos().x &&
689 i->y == tux->get_tile_pos().y)
698 WorldMap::draw(const Point& offset)
700 for(int y = 0; y < height; ++y)
701 for(int x = 0; x < width; ++x)
703 Tile* tile = at(Point(x, y));
704 tile->sprite->draw(x*32 + offset.x,
708 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
711 leveldot_green->draw(i->x*32 + offset.x,
714 leveldot_red->draw(i->x*32 + offset.x,
723 WorldMap::draw_status()
726 sprintf(str, "%d", player_status.score);
727 white_text->draw("SCORE", 0, 0);
728 gold_text->draw(str, 96, 0);
730 sprintf(str, "%d", player_status.distros);
731 white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP);
732 gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP);
734 white_text->draw("LIVES", 480, 0);
735 if (player_status.lives >= 5)
737 sprintf(str, "%dx", player_status.lives);
738 gold_text->draw(str, 585, 0);
739 tux_life->draw(565+(18*3), 0);
743 for(int i= 0; i < player_status.lives; ++i)
744 tux_life->draw(565+(18*i),0);
747 if (!tux->is_moving())
749 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
751 if (i->x == tux->get_tile_pos().x &&
752 i->y == tux->get_tile_pos().y)
754 white_text->draw_align(i->title.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM);
764 Menu::set_current(0);
768 song = music_manager->load_music(datadir + "/music/" + music);
769 music_manager->play_music(song);
772 Point tux_pos = tux->get_pos();
775 offset.x = -tux_pos.x + screen->w/2;
776 offset.y = -tux_pos.y + screen->h/2;
778 if (offset.x > 0) offset.x = 0;
779 if (offset.y > 0) offset.y = 0;
781 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
782 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
791 Menu::current()->draw();
792 mouse_cursor->draw();
801 WorldMap::savegame(const std::string& filename)
803 std::cout << "savegame: " << filename << std::endl;
804 std::ofstream out(filename.c_str());
806 int nb_solved_levels = 0;
807 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
813 out << "(supertux-savegame\n"
815 << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
816 << " (lives " << player_status.lives << ")\n"
817 << " (score " << player_status.score << ")\n"
818 << " (distros " << player_status.distros << ")\n"
819 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
820 << " (back \"" << direction_to_string(tux->back_direction) << "\")\n"
821 << " (bonus \"" << bonus_to_string(player_status.bonus) << "\"))\n"
824 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
828 out << " (level (name \"" << i->name << "\")\n"
829 << " (solved #t))\n";
834 << " )\n\n;; EOF ;;" << std::endl;
838 WorldMap::loadgame(const std::string& filename)
840 std::cout << "loadgame: " << filename << std::endl;
841 savegame_file = filename;
843 if (access(filename.c_str(), F_OK) != 0)
846 lisp_object_t* savegame = lisp_read_from_file(filename);
847 lisp_object_t* cur = savegame;
849 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
853 LispReader reader(cur);
855 reader.read_int("lives", &player_status.lives);
856 reader.read_int("score", &player_status.score);
857 reader.read_int("distros", &player_status.distros);
859 if (player_status.lives < 0)
860 player_status.lives = START_LIVES;
862 lisp_object_t* tux_cur = 0;
863 if (reader.read_lisp("tux", &tux_cur))
866 std::string back_str = "none";
867 std::string bonus_str = "none";
869 LispReader tux_reader(tux_cur);
870 tux_reader.read_int("x", &p.x);
871 tux_reader.read_int("y", &p.y);
872 tux_reader.read_string("back", &back_str);
873 tux_reader.read_string("bonus", &bonus_str);
875 player_status.bonus = string_to_bonus(bonus_str);
876 tux->back_direction = string_to_direction(back_str);
877 tux->set_tile_pos(p);
880 lisp_object_t* level_cur = 0;
881 if (reader.read_lisp("levels", &level_cur))
885 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
886 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
888 if (strcmp(lisp_symbol(sym), "level") == 0)
893 LispReader level_reader(data);
894 level_reader.read_string("name", &name);
895 level_reader.read_bool("solved", &solved);
897 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
904 level_cur = lisp_cdr(level_cur);
911 } // namespace WorldMapNS
913 /* Local Variables: */