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"
32 namespace WorldMapNS {
34 Direction reverse_dir(Direction direction)
53 direction_to_string(Direction direction)
71 string_to_direction(const std::string& directory)
73 if (directory == "west")
75 else if (directory == "east")
77 else if (directory == "north")
79 else if (directory == "south")
85 TileManager* TileManager::instance_ = 0;
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;
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_string("image", &filename);
124 tile->sprite = new Surface(
125 datadir + "/images/worldmap/" + filename,
128 if (id >= int(tiles.size()))
135 puts("Unhandled symbol");
148 TileManager::get(int i)
150 assert(i >=0 && i < int(tiles.size()));
154 Tux::Tux(WorldMap* worldmap_)
155 : worldmap(worldmap_)
157 sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
163 input_direction = NONE;
167 Tux::draw(const Point& offset)
169 Point pos = get_pos();
170 sprite->draw(pos.x + offset.x,
178 float x = tile_pos.x * 32;
179 float y = tile_pos.y * 32;
199 return Point((int)x, (int)y);
211 Tux::update(float delta)
215 if (input_direction != NONE)
217 WorldMap::Level* level = worldmap->at_level();
219 // We got a new direction, so lets start walking when possible
221 if ((!level || level->solved)
222 && worldmap->path_ok(input_direction, tile_pos, &next_tile))
224 tile_pos = next_tile;
226 direction = input_direction;
227 back_direction = reverse_dir(direction);
229 else if (input_direction == back_direction)
231 std::cout << "Back triggered" << std::endl;
233 direction = input_direction;
234 tile_pos = worldmap->get_next_tile(tile_pos, direction);
235 back_direction = reverse_dir(direction);
241 // Let tux walk a few pixels (20 pixel/sec)
242 offset += 20.0f * delta;
245 { // We reached the next tile, so we check what to do now
248 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
254 // Walk automatically to the next tile
256 if (worldmap->path_ok(direction, tile_pos, &next_tile))
258 tile_pos = next_tile;
262 puts("Tilemap data is buggy");
277 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
278 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
279 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
281 input_direction = NONE;
285 music = "SALCON.MOD";
291 WorldMap::~WorldMap()
299 std::string filename = datadir + "levels/default/worldmap.stwm";
301 lisp_object_t* root_obj = lisp_read_from_file(filename);
303 st_abort("Couldn't load file", filename);
305 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
307 lisp_object_t* cur = lisp_cdr(root_obj);
309 while(!lisp_nil_p(cur))
311 lisp_object_t* element = lisp_car(cur);
313 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
315 LispReader reader(lisp_cdr(element));
316 reader.read_int("width", &width);
317 reader.read_int("height", &height);
318 reader.read_int_vector("data", &tilemap);
320 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
322 LispReader reader(lisp_cdr(element));
323 reader.read_string("name", &name);
324 reader.read_string("music", &music);
326 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
328 lisp_object_t* cur = lisp_cdr(element);
330 while(!lisp_nil_p(cur))
332 lisp_object_t* element = lisp_car(cur);
334 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
337 LispReader reader(lisp_cdr(element));
338 level.solved = false;
345 reader.read_string("name", &level.name);
346 reader.read_int("x", &level.x);
347 reader.read_int("y", &level.y);
349 get_level_title(&level); // get level's title
351 levels.push_back(level);
367 void WorldMap::get_level_title(Levels::pointer level)
369 /** get level's title */
370 level->title = "<no title>";
373 lisp_object_t* root_obj = 0;
374 fi = fopen((datadir + "levels/" + level->name).c_str(), "r");
377 perror((datadir + "levels/" + level->name).c_str());
381 lisp_stream_t stream;
382 lisp_stream_init_file (&stream, fi);
383 root_obj = lisp_read (&stream);
385 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
387 printf("World: Parse Error in file %s", level->name.c_str());
390 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
392 LispReader reader(lisp_cdr(root_obj));
393 reader.read_string("name", &level->title);
400 WorldMap::on_escape_press()
402 // Show or hide the menu
404 Menu::set_current(worldmap_menu);
406 Menu::set_current(0);
410 WorldMap::get_input()
413 input_direction = NONE;
416 while (SDL_PollEvent(&event))
420 Menu::current()->event(event);
427 st_abort("Received window close", "");
431 switch(event.key.keysym.sym)
445 case SDL_JOYAXISMOTION:
446 if (event.jaxis.axis == joystick_keymap.x_axis)
448 if (event.jaxis.value < -joystick_keymap.dead_zone)
449 input_direction = WEST;
450 else if (event.jaxis.value > joystick_keymap.dead_zone)
451 input_direction = EAST;
453 else if (event.jaxis.axis == joystick_keymap.y_axis)
455 if (event.jaxis.value > joystick_keymap.dead_zone)
456 input_direction = SOUTH;
457 else if (event.jaxis.value < -joystick_keymap.dead_zone)
458 input_direction = NORTH;
462 case SDL_JOYBUTTONDOWN:
463 if (event.jbutton.button == joystick_keymap.b_button)
465 else if (event.jbutton.button == joystick_keymap.start_button)
475 if (!Menu::current())
477 Uint8 *keystate = SDL_GetKeyState(NULL);
479 if (keystate[SDLK_LEFT])
480 input_direction = WEST;
481 else if (keystate[SDLK_RIGHT])
482 input_direction = EAST;
483 else if (keystate[SDLK_UP])
484 input_direction = NORTH;
485 else if (keystate[SDLK_DOWN])
486 input_direction = SOUTH;
491 WorldMap::get_next_tile(Point pos, Direction direction)
514 WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
516 *new_pos = get_next_tile(old_pos, direction);
518 if (!(new_pos->x >= 0 && new_pos->x < width
519 && new_pos->y >= 0 && new_pos->y < height))
520 { // New position is outsite the tilemap
524 { // Check if we the tile allows us to go to new_pos
528 return (at(old_pos)->west && at(*new_pos)->east);
531 return (at(old_pos)->east && at(*new_pos)->west);
534 return (at(old_pos)->north && at(*new_pos)->south);
537 return (at(old_pos)->south && at(*new_pos)->north);
540 assert(!"path_ok() can't work if direction is NONE");
549 if (enter_level && !tux->is_moving())
551 Level* level = at_level();
554 if (level->x == tux->get_tile_pos().x &&
555 level->y == tux->get_tile_pos().y)
557 std::cout << "Enter the current level: " << level->name << std::endl;;
560 GameSession session(datadir + "levels/" + level->name,
561 1, ST_GL_LOAD_LEVEL_FILE);
563 switch (session.run())
565 case GameSession::LEVEL_FINISHED:
566 level->solved = true;
568 case GameSession::LEVEL_ABORT:
569 // Reseting the player_status might be a worthy
570 // consideration, but I don't think we need it
571 // 'cause only the bad players will use it to
572 // 'cheat' a few items and that isn't necesarry a
573 // bad thing (ie. better they continue that way,
574 // then stop playing the game all together since it
577 case GameSession::GAME_OVER:
580 case GameSession::NONE:
581 // Should never be reached
586 Menu::set_current(0);
587 if (!savegame_file.empty())
588 savegame(savegame_file);
594 std::cout << "Nothing to enter at: "
595 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
600 tux->set_direction(input_direction);
604 Menu* menu = Menu::current();
609 if(menu == worldmap_menu)
611 switch (worldmap_menu->check())
613 case 2: // Return to game
616 if (!savegame_file.empty())
617 savegame(savegame_file);
620 case 6: // Quit Worldmap
625 else if(menu == options_menu)
627 process_options_menu();
633 WorldMap::at(Point p)
640 return TileManager::instance()->get(tilemap[width * p.y + p.x]);
646 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
648 if (i->x == tux->get_tile_pos().x &&
649 i->y == tux->get_tile_pos().y)
658 WorldMap::draw(const Point& offset)
660 for(int y = 0; y < height; ++y)
661 for(int x = 0; x < width; ++x)
663 Tile* tile = at(Point(x, y));
664 tile->sprite->draw(x*32 + offset.x,
668 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
671 leveldot_green->draw(i->x*32 + offset.x,
674 leveldot_red->draw(i->x*32 + offset.x,
683 WorldMap::draw_status()
686 sprintf(str, "%d", player_status.score);
687 white_text->draw("SCORE", 0, 0);
688 gold_text->draw(str, 96, 0);
690 sprintf(str, "%d", player_status.distros);
691 white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP);
692 gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP);
694 white_text->draw("LIVES", 480, 0);
695 if (player_status.lives >= 5)
697 sprintf(str, "%dx", player_status.lives);
698 gold_text->draw(str, 585, 0);
699 tux_life->draw(565+(18*3), 0);
703 for(int i= 0; i < player_status.lives; ++i)
704 tux_life->draw(565+(18*i),0);
707 if (!tux->is_moving())
709 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
711 if (i->x == tux->get_tile_pos().x &&
712 i->y == tux->get_tile_pos().y)
714 white_text->draw_align(i->title.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM);
724 Menu::set_current(0);
728 song = load_song(datadir + "/music/" + music);
732 Point tux_pos = tux->get_pos();
735 offset.x = -tux_pos.x + screen->w/2;
736 offset.y = -tux_pos.y + screen->h/2;
738 if (offset.x > 0) offset.x = 0;
739 if (offset.y > 0) offset.y = 0;
741 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
742 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
751 Menu::current()->draw();
752 mouse_cursor->draw();
764 WorldMap::savegame(const std::string& filename)
766 std::cout << "savegame: " << filename << std::endl;
767 std::ofstream out(filename.c_str());
769 int nb_solved_levels = 0;
770 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
776 out << "(supertux-savegame\n"
778 << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
779 << " (lives " << player_status.lives << ")\n"
780 << " (score " << player_status.score << ")\n"
781 << " (distros " << player_status.distros << ")\n"
782 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")"
783 << " (back \"" << direction_to_string(tux->back_direction) << "\"))\n"
786 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
790 out << " (level (name \"" << i->name << "\")\n"
791 << " (solved #t))\n";
796 << " )\n\n;; EOF ;;" << std::endl;
800 WorldMap::loadgame(const std::string& filename)
802 std::cout << "loadgame: " << filename << std::endl;
803 savegame_file = filename;
805 if (access(filename.c_str(), F_OK) == 0)
807 lisp_object_t* cur = lisp_read_from_file(filename);
809 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
813 LispReader reader(cur);
815 reader.read_int("lives", &player_status.lives);
816 reader.read_int("score", &player_status.score);
817 reader.read_int("distros", &player_status.distros);
819 if (player_status.lives < 0)
820 player_status.lives = 3;
822 lisp_object_t* tux_cur = 0;
823 if (reader.read_lisp("tux", &tux_cur))
826 std::string back_str = "none";
828 LispReader tux_reader(tux_cur);
829 tux_reader.read_int("x", &p.x);
830 tux_reader.read_int("y", &p.y);
831 tux_reader.read_string("back", &back_str);
833 tux->back_direction = string_to_direction(back_str);
834 tux->set_tile_pos(p);
837 lisp_object_t* level_cur = 0;
838 if (reader.read_lisp("levels", &level_cur))
842 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
843 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
845 if (strcmp(lisp_symbol(sym), "level") == 0)
850 LispReader level_reader(data);
851 level_reader.read_string("name", &name);
852 level_reader.read_bool("solved", &solved);
854 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
861 level_cur = lisp_cdr(level_cur);
867 } // namespace WorldMapNS
869 /* Local Variables: */